4 * Authorization routines for the CUPS scheduler.
6 * Copyright 2007-2012 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * This file contains Kerberos support code, copyright 2006 by
12 * These coded instructions, statements, and computer programs are the
13 * property of Apple Inc. and are protected by Federal copyright
14 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
15 * which should have been included with this file. If this file is
16 * file is missing or damaged, see the license at "http://www.cups.org/".
20 * cupsdAddIPMask() - Add an IP address authorization mask.
21 * cupsdAddLocation() - Add a location for authorization.
22 * cupsdAddName() - Add a name to a location...
23 * cupsdAddNameMask() - Add a host or interface name authorization
25 * cupsdAuthorize() - Validate any authorization credentials.
26 * cupsdCheckAccess() - Check whether the given address is allowed to
28 * cupsdCheckAuth() - Check authorization masks.
29 * cupsdCheckGroup() - Check for a user's group membership.
30 * cupsdCopyLocation() - Make a copy of a location...
31 * cupsdDeleteAllLocations() - Free all memory used for location
33 * cupsdFindBest() - Find the location entry that best matches the
35 * cupsdFindLocation() - Find the named location.
36 * cupsdFreeLocation() - Free all memory used by a location.
37 * cupsdIsAuthorized() - Check to see if the user is authorized...
38 * cupsdNewLocation() - Create a new location for authorization.
39 * check_authref() - Check if an authorization services reference
40 * has the supplied right.
41 * compare_locations() - Compare two locations.
42 * copy_authmask() - Copy function for auth masks.
43 * cups_crypt() - Encrypt the password using the DES or MD5
44 * algorithms, as needed.
45 * free_authmask() - Free function for auth masks.
46 * get_md5_password() - Get an MD5 password.
47 * pam_func() - PAM conversation function.
48 * to64() - Base64-encode an integer value...
52 * Include necessary headers...
59 #endif /* HAVE_SHADOW_H */
62 #endif /* HAVE_CRYPT_H */
64 # ifdef HAVE_PAM_PAM_APPL_H
65 # include <pam/pam_appl.h>
67 # include <security/pam_appl.h>
68 # endif /* HAVE_PAM_PAM_APPL_H */
69 #endif /* HAVE_LIBPAM */
72 #endif /* HAVE_USERSEC_H */
73 #ifdef HAVE_MEMBERSHIP_H
74 # include <membership.h>
75 #endif /* HAVE_MEMBERSHIP_H */
76 #ifdef HAVE_AUTHORIZATION_H
77 # include <Security/AuthorizationTags.h>
78 # ifdef HAVE_SECBASEPRIV_H
79 # include <Security/SecBasePriv.h>
81 extern const char *cssmErrorString(int error
);
82 # endif /* HAVE_SECBASEPRIV_H */
83 #endif /* HAVE_AUTHORIZATION_H */
84 #ifdef HAVE_SYS_PARAM_H
85 # include <sys/param.h>
86 #endif /* HAVE_SYS_PARAM_H */
87 #ifdef HAVE_SYS_UCRED_H
88 # include <sys/ucred.h>
89 typedef struct xucred cupsd_ucred_t
;
90 # define CUPSD_UCRED_UID(c) (c).cr_uid
93 typedef struct ucred cupsd_ucred_t
;
95 typedef struct sockpeercred cupsd_ucred_t
;
97 # define CUPSD_UCRED_UID(c) (c).uid
98 #endif /* HAVE_SYS_UCRED_H */
105 #ifdef HAVE_AUTHORIZATION_H
106 static int check_authref(cupsd_client_t
*con
, const char *right
);
107 #endif /* HAVE_AUTHORIZATION_H */
108 static int compare_locations(cupsd_location_t
*a
,
109 cupsd_location_t
*b
);
110 static cupsd_authmask_t
*copy_authmask(cupsd_authmask_t
*am
, void *data
);
111 #if !HAVE_LIBPAM && !defined(HAVE_USERSEC_H)
112 static char *cups_crypt(const char *pw
, const char *salt
);
113 #endif /* !HAVE_LIBPAM && !HAVE_USERSEC_H */
114 static void free_authmask(cupsd_authmask_t
*am
, void *data
);
115 static char *get_md5_password(const char *username
,
116 const char *group
, char passwd
[33]);
118 static int pam_func(int, const struct pam_message
**,
119 struct pam_response
**, void *);
120 #elif !defined(HAVE_USERSEC_H)
121 static void to64(char *s
, unsigned long v
, int n
);
122 #endif /* HAVE_LIBPAM */
126 * Local structures...
130 typedef struct cupsd_authdata_s
/**** Authentication data ****/
132 char username
[HTTP_MAX_VALUE
], /* Username string */
133 password
[HTTP_MAX_VALUE
]; /* Password string */
135 #endif /* HAVE_LIBPAM */
142 #if defined(__hpux) && HAVE_LIBPAM
143 static cupsd_authdata_t
*auth_data
; /* Current client being authenticated */
144 #endif /* __hpux && HAVE_LIBPAM */
148 * 'cupsdAddIPMask()' - Add an IP address authorization mask.
151 int /* O - 1 on success, 0 on failure */
153 cups_array_t
**masks
, /* IO - Masks array (created as needed) */
154 const unsigned address
[4], /* I - IP address */
155 const unsigned netmask
[4]) /* I - IP netmask */
157 cupsd_authmask_t temp
; /* New host/domain mask */
160 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
161 "cupsdAddIPMask(masks=%p(%p), address=%x:%x:%x:%x, "
162 "netmask=%x:%x:%x:%x)",
164 address
[0], address
[1], address
[2], address
[3],
165 netmask
[0], netmask
[1], netmask
[2], netmask
[3]);
167 temp
.type
= CUPSD_AUTH_IP
;
168 memcpy(temp
.mask
.ip
.address
, address
, sizeof(temp
.mask
.ip
.address
));
169 memcpy(temp
.mask
.ip
.netmask
, netmask
, sizeof(temp
.mask
.ip
.netmask
));
172 * Create the masks array as needed and add...
176 *masks
= cupsArrayNew3(NULL
, NULL
, NULL
, 0,
177 (cups_acopy_func_t
)copy_authmask
,
178 (cups_afree_func_t
)free_authmask
);
180 return (cupsArrayAdd(*masks
, &temp
));
185 * 'cupsdAddLocation()' - Add a location for authorization.
189 cupsdAddLocation(cupsd_location_t
*loc
) /* I - Location to add */
192 * Make sure the locations array is created...
196 Locations
= cupsArrayNew3((cups_array_func_t
)compare_locations
, NULL
,
197 (cups_ahash_func_t
)NULL
, 0,
198 (cups_acopy_func_t
)NULL
,
199 (cups_afree_func_t
)cupsdFreeLocation
);
203 cupsArrayAdd(Locations
, loc
);
205 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAddLocation: Added location \"%s\"",
206 loc
->location
? loc
->location
: "(null)");
212 * 'cupsdAddName()' - Add a name to a location...
216 cupsdAddName(cupsd_location_t
*loc
, /* I - Location to add to */
217 char *name
) /* I - Name to add */
219 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAddName(loc=%p, name=\"%s\")",
223 loc
->names
= cupsArrayNew3(NULL
, NULL
, NULL
, 0,
224 (cups_acopy_func_t
)_cupsStrAlloc
,
225 (cups_afree_func_t
)_cupsStrFree
);
227 if (!cupsArrayAdd(loc
->names
, name
))
229 cupsdLogMessage(CUPSD_LOG_ERROR
,
230 "Unable to duplicate name for location %s: %s",
231 loc
->location
? loc
->location
: "nil", strerror(errno
));
238 * 'cupsdAddNameMask()' - Add a host or interface name authorization mask.
241 int /* O - 1 on success, 0 on failure */
242 cupsdAddNameMask(cups_array_t
**masks
, /* IO - Masks array (created as needed) */
243 char *name
) /* I - Host or interface name */
245 cupsd_authmask_t temp
; /* New host/domain mask */
246 char ifname
[32], /* Interface name */
247 *ifptr
; /* Pointer to end of name */
250 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
251 "cupsdAddNameMask(masks=%p(%p), name=\"%s\")",
252 masks
, *masks
, name
);
254 if (!_cups_strcasecmp(name
, "@LOCAL"))
257 * Deny *interface*...
260 temp
.type
= CUPSD_AUTH_INTERFACE
;
261 temp
.mask
.name
.name
= (char *)"*";
263 else if (!_cups_strncasecmp(name
, "@IF(", 4))
266 * Deny *interface*...
269 strlcpy(ifname
, name
+ 4, sizeof(ifname
));
271 ifptr
= ifname
+ strlen(ifname
) - 1;
273 if (ifptr
>= ifname
&& *ifptr
== ')')
279 temp
.type
= CUPSD_AUTH_INTERFACE
;
280 temp
.mask
.name
.name
= ifname
;
291 temp
.type
= CUPSD_AUTH_NAME
;
292 temp
.mask
.name
.name
= (char *)name
;
296 * Set the name length...
299 temp
.mask
.name
.length
= strlen(temp
.mask
.name
.name
);
302 * Create the masks array as needed and add...
306 *masks
= cupsArrayNew3(NULL
, NULL
, NULL
, 0,
307 (cups_acopy_func_t
)copy_authmask
,
308 (cups_afree_func_t
)free_authmask
);
310 return (cupsArrayAdd(*masks
, &temp
));
315 * 'cupsdAuthorize()' - Validate any authorization credentials.
319 cupsdAuthorize(cupsd_client_t
*con
) /* I - Client connection */
321 int type
; /* Authentication type */
322 const char *authorization
; /* Pointer into Authorization string */
323 char *ptr
, /* Pointer into string */
324 username
[HTTP_MAX_VALUE
],
325 /* Username string */
326 password
[HTTP_MAX_VALUE
];
327 /* Password string */
328 cupsd_cert_t
*localuser
; /* Certificate username */
329 char nonce
[HTTP_MAX_VALUE
], /* Nonce value from client */
330 md5
[33], /* MD5 password */
331 basicmd5
[33]; /* MD5 of Basic password */
332 static const char * const states
[] = /* HTTP client states... */
352 * Locate the best matching location so we know what kind of
353 * authentication to expect...
356 con
->best
= cupsdFindBest(con
->uri
, con
->http
.state
);
357 con
->type
= CUPSD_AUTH_NONE
;
359 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
360 "[Client %d] con->uri=\"%s\", con->best=%p(%s)",
361 con
->http
.fd
, con
->uri
, con
->best
,
362 con
->best
? con
->best
->location
: "");
364 if (con
->best
&& con
->best
->type
!= CUPSD_AUTH_NONE
)
366 if (con
->best
->type
== CUPSD_AUTH_DEFAULT
)
367 type
= cupsdDefaultAuthType();
369 type
= con
->best
->type
;
372 type
= cupsdDefaultAuthType();
375 * Decode the Authorization string...
378 authorization
= httpGetField(&con
->http
, HTTP_FIELD_AUTHORIZATION
);
380 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Client %d] Authorization=\"%s\"",
381 con
->http
.fd
, authorization
);
388 #endif /* HAVE_GSSAPI */
390 #ifdef HAVE_AUTHORIZATION_H
393 AuthorizationFree(con
->authref
, kAuthorizationFlagDefaults
);
396 #endif /* HAVE_AUTHORIZATION_H */
401 * No authorization data provided, return early...
404 cupsdLogMessage(CUPSD_LOG_DEBUG
,
405 "[Client %d] No authentication data provided.",
409 #ifdef HAVE_AUTHORIZATION_H
410 else if (!strncmp(authorization
, "AuthRef ", 8) &&
411 !_cups_strcasecmp(con
->http
.hostname
, "localhost"))
413 OSStatus status
; /* Status */
414 int authlen
; /* Auth string length */
415 AuthorizationItemSet
*authinfo
; /* Authorization item set */
418 * Get the Authorization Services data...
422 while (isspace(*authorization
& 255))
425 authlen
= sizeof(nonce
);
426 httpDecode64_2(nonce
, &authlen
, authorization
);
428 if (authlen
!= kAuthorizationExternalFormLength
)
430 cupsdLogMessage(CUPSD_LOG_ERROR
,
431 "[Client %d] External Authorization reference size is "
432 "incorrect.", con
->http
.fd
);
436 if ((status
= AuthorizationCreateFromExternalForm(
437 (AuthorizationExternalForm
*)nonce
, &con
->authref
)) != 0)
439 cupsdLogMessage(CUPSD_LOG_ERROR
,
440 "[Client %d] AuthorizationCreateFromExternalForm "
441 "returned %d (%s)", con
->http
.fd
, (int)status
,
442 cssmErrorString(status
));
448 if (!AuthorizationCopyInfo(con
->authref
, kAuthorizationEnvironmentUsername
,
451 if (authinfo
->count
== 1 && authinfo
->items
[0].value
&&
452 authinfo
->items
[0].valueLength
>= 2)
454 strlcpy(username
, authinfo
->items
[0].value
, sizeof(username
));
456 cupsdLogMessage(CUPSD_LOG_DEBUG
,
457 "[Client %d] Authorized as \"%s\" using AuthRef",
458 con
->http
.fd
, username
);
461 AuthorizationFreeItemSet(authinfo
);
467 * No username in AuthRef, grab username using peer credentials...
470 struct passwd
*pwd
; /* Password entry for this user */
471 cupsd_ucred_t peercred
; /* Peer credentials */
472 socklen_t peersize
; /* Size of peer credentials */
474 peersize
= sizeof(peercred
);
476 if (getsockopt(con
->http
.fd
, 0, LOCAL_PEERCRED
, &peercred
, &peersize
))
478 cupsdLogMessage(CUPSD_LOG_ERROR
,
479 "[Client %d] Unable to get peer credentials - %s",
480 con
->http
.fd
, strerror(errno
));
484 if ((pwd
= getpwuid(CUPSD_UCRED_UID(peercred
))) == NULL
)
486 cupsdLogMessage(CUPSD_LOG_ERROR
,
487 "[Client %d] Unable to find UID %d for peer "
488 "credentials.", con
->http
.fd
,
489 (int)CUPSD_UCRED_UID(peercred
));
493 strlcpy(username
, pwd
->pw_name
, sizeof(username
));
495 cupsdLogMessage(CUPSD_LOG_DEBUG
,
496 "[Client %d] Authorized as \"%s\" using "
497 "AuthRef + PeerCred", con
->http
.fd
, username
);
500 con
->type
= CUPSD_AUTH_BASIC
;
502 #endif /* HAVE_AUTHORIZATION_H */
503 #if defined(SO_PEERCRED) && defined(AF_LOCAL)
504 else if (!strncmp(authorization
, "PeerCred ", 9) &&
505 con
->http
.hostaddr
->addr
.sa_family
== AF_LOCAL
)
508 * Use peer credentials from domain socket connection...
511 struct passwd
*pwd
; /* Password entry for this user */
512 cupsd_ucred_t peercred
; /* Peer credentials */
513 socklen_t peersize
; /* Size of peer credentials */
514 #ifdef HAVE_AUTHORIZATION_H
515 const char *name
; /* Authorizing name */
516 int no_peer
= 0; /* Don't allow peer credentials? */
519 * See if we should allow peer credentials...
522 for (name
= (char *)cupsArrayFirst(con
->best
->names
);
524 name
= (char *)cupsArrayNext(con
->best
->names
))
526 if (!_cups_strncasecmp(name
, "@AUTHKEY(", 9) ||
527 !_cups_strcasecmp(name
, "@SYSTEM"))
529 /* Normally don't want peer credentials if we need an auth key... */
532 else if (!_cups_strcasecmp(name
, "@OWNER"))
534 /* but if @OWNER is present then we allow it... */
542 cupsdLogMessage(CUPSD_LOG_ERROR
,
543 "[Client %d] PeerCred authentication not allowed for "
544 "resource per AUTHKEY policy.", con
->http
.fd
);
547 #endif /* HAVE_AUTHORIZATION_H */
549 if ((pwd
= getpwnam(authorization
+ 9)) == NULL
)
551 cupsdLogMessage(CUPSD_LOG_ERROR
,
552 "[Client %d] User \"%s\" does not exist.", con
->http
.fd
,
557 peersize
= sizeof(peercred
);
560 if (getsockopt(con
->http
.fd
, 0, LOCAL_PEERCRED
, &peercred
, &peersize
))
562 if (getsockopt(con
->http
.fd
, SOL_SOCKET
, SO_PEERCRED
, &peercred
, &peersize
))
563 # endif /* __APPLE__ */
565 cupsdLogMessage(CUPSD_LOG_ERROR
,
566 "[Client %d] Unable to get peer credentials - %s",
567 con
->http
.fd
, strerror(errno
));
571 if (pwd
->pw_uid
!= CUPSD_UCRED_UID(peercred
))
573 cupsdLogMessage(CUPSD_LOG_ERROR
,
574 "[Client %d] Invalid peer credentials for \"%s\" - got "
575 "%d, expected %d!", con
->http
.fd
, authorization
+ 9,
576 CUPSD_UCRED_UID(peercred
), pwd
->pw_uid
);
577 # ifdef HAVE_SYS_UCRED_H
578 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Client %d] cr_version=%d",
579 con
->http
.fd
, peercred
.cr_version
);
580 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Client %d] cr_uid=%d",
581 con
->http
.fd
, peercred
.cr_uid
);
582 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Client %d] cr_ngroups=%d",
583 con
->http
.fd
, peercred
.cr_ngroups
);
584 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Client %d] cr_groups[0]=%d",
585 con
->http
.fd
, peercred
.cr_groups
[0]);
586 # endif /* HAVE_SYS_UCRED_H */
590 strlcpy(username
, authorization
+ 9, sizeof(username
));
593 con
->gss_uid
= CUPSD_UCRED_UID(peercred
);
594 # endif /* HAVE_GSSAPI */
596 cupsdLogMessage(CUPSD_LOG_DEBUG
,
597 "[Client %d] Authorized as %s using PeerCred", con
->http
.fd
,
600 con
->type
= CUPSD_AUTH_BASIC
;
602 #endif /* SO_PEERCRED && AF_LOCAL */
603 else if (!strncmp(authorization
, "Local", 5) &&
604 !_cups_strcasecmp(con
->http
.hostname
, "localhost"))
607 * Get Local certificate authentication data...
611 while (isspace(*authorization
& 255))
614 if ((localuser
= cupsdFindCert(authorization
)) == NULL
)
616 cupsdLogMessage(CUPSD_LOG_ERROR
,
617 "[Client %d] Local authentication certificate not found.",
622 strlcpy(username
, localuser
->username
, sizeof(username
));
623 con
->type
= localuser
->type
;
625 cupsdLogMessage(CUPSD_LOG_DEBUG
,
626 "[Client %d] Authorized as %s using Local", con
->http
.fd
,
629 else if (!strncmp(authorization
, "Basic", 5))
632 * Get the Basic authentication data...
635 int userlen
; /* Username:password length */
639 while (isspace(*authorization
& 255))
642 userlen
= sizeof(username
);
643 httpDecode64_2(username
, &userlen
, authorization
);
646 * Pull the username and password out...
649 if ((ptr
= strchr(username
, ':')) == NULL
)
651 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Client %d] Missing Basic password.",
661 * Username must not be empty...
664 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Client %d] Empty Basic username.",
672 * Password must not be empty...
675 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Client %d] Empty Basic password.",
680 strlcpy(password
, ptr
, sizeof(password
));
683 * Validate the username and password...
689 case CUPSD_AUTH_BASIC
:
693 * Only use PAM to do authentication. This supports MD5
694 * passwords, among other things...
697 pam_handle_t
*pamh
; /* PAM authentication handle */
698 int pamerr
; /* PAM error code */
699 struct pam_conv pamdata
;/* PAM conversation data */
700 cupsd_authdata_t data
; /* Authentication data */
703 strlcpy(data
.username
, username
, sizeof(data
.username
));
704 strlcpy(data
.password
, password
, sizeof(data
.password
));
706 # if defined(__sun) || defined(__hpux)
707 pamdata
.conv
= (int (*)(int, struct pam_message
**,
708 struct pam_response
**,
711 pamdata
.conv
= pam_func
;
712 # endif /* __sun || __hpux */
713 pamdata
.appdata_ptr
= &data
;
717 * Workaround for HP-UX bug in pam_unix; see pam_func() below for
724 pamerr
= pam_start("cups", username
, &pamdata
, &pamh
);
725 if (pamerr
!= PAM_SUCCESS
)
727 cupsdLogMessage(CUPSD_LOG_ERROR
,
728 "[Client %d] pam_start() returned %d (%s)",
729 con
->http
.fd
, pamerr
, pam_strerror(pamh
, pamerr
));
733 # ifdef HAVE_PAM_SET_ITEM
735 pamerr
= pam_set_item(pamh
, PAM_RHOST
, con
->http
.hostname
);
736 if (pamerr
!= PAM_SUCCESS
)
737 cupsdLogMessage(CUPSD_LOG_WARN
,
738 "[Client %d] pam_set_item(PAM_RHOST) "
739 "returned %d (%s)", con
->http
.fd
, pamerr
,
740 pam_strerror(pamh
, pamerr
));
741 # endif /* PAM_RHOST */
744 pamerr
= pam_set_item(pamh
, PAM_TTY
, "cups");
745 if (pamerr
!= PAM_SUCCESS
)
746 cupsdLogMessage(CUPSD_LOG_WARN
,
747 "[Client %d] pam_set_item(PAM_TTY) "
748 "returned %d (%s)!", con
->http
.fd
, pamerr
,
749 pam_strerror(pamh
, pamerr
));
750 # endif /* PAM_TTY */
751 # endif /* HAVE_PAM_SET_ITEM */
753 pamerr
= pam_authenticate(pamh
, PAM_SILENT
);
754 if (pamerr
!= PAM_SUCCESS
)
756 cupsdLogMessage(CUPSD_LOG_ERROR
,
757 "[Client %d] pam_authenticate() returned %d (%s)",
758 con
->http
.fd
, pamerr
, pam_strerror(pamh
, pamerr
));
763 # ifdef HAVE_PAM_SETCRED
764 pamerr
= pam_setcred(pamh
, PAM_ESTABLISH_CRED
| PAM_SILENT
);
765 if (pamerr
!= PAM_SUCCESS
)
766 cupsdLogMessage(CUPSD_LOG_WARN
,
767 "[Client %d] pam_setcred() returned %d (%s)",
768 con
->http
.fd
, pamerr
,
769 pam_strerror(pamh
, pamerr
));
770 # endif /* HAVE_PAM_SETCRED */
772 pamerr
= pam_acct_mgmt(pamh
, PAM_SILENT
);
773 if (pamerr
!= PAM_SUCCESS
)
775 cupsdLogMessage(CUPSD_LOG_ERROR
,
776 "[Client %d] pam_acct_mgmt() returned %d (%s)",
777 con
->http
.fd
, pamerr
, pam_strerror(pamh
, pamerr
));
782 pam_end(pamh
, PAM_SUCCESS
);
784 #elif defined(HAVE_USERSEC_H)
786 * Use AIX authentication interface...
789 char *authmsg
; /* Authentication message */
790 int reenter
; /* ??? */
793 cupsdLogMessage(CUPSD_LOG_DEBUG
,
794 "[Client %d] AIX authenticate of username \"%s\"",
795 con
->http
.fd
, username
);
798 if (authenticate(username
, password
, &reenter
, &authmsg
) != 0)
800 cupsdLogMessage(CUPSD_LOG_DEBUG
,
801 "[Client %d] Unable to authenticate username "
802 "\"%s\": %s", con
->http
.fd
, username
,
809 * Use normal UNIX password file-based authentication...
812 char *pass
; /* Encrypted password */
813 struct passwd
*pw
; /* User password data */
814 # ifdef HAVE_SHADOW_H
815 struct spwd
*spw
; /* Shadow password data */
816 # endif /* HAVE_SHADOW_H */
819 pw
= getpwnam(username
); /* Get the current password */
820 endpwent(); /* Close the password file */
828 cupsdLogMessage(CUPSD_LOG_ERROR
,
829 "[Client %d] Unknown username \"%s\".",
830 con
->http
.fd
, username
);
834 # ifdef HAVE_SHADOW_H
835 spw
= getspnam(username
);
838 if (!spw
&& !strcmp(pw
->pw_passwd
, "x"))
841 * Don't allow blank passwords!
844 cupsdLogMessage(CUPSD_LOG_ERROR
,
845 "[Client %d] Username \"%s\" has no shadow "
846 "password.", con
->http
.fd
, username
);
850 if (spw
&& !spw
->sp_pwdp
[0] && !pw
->pw_passwd
[0])
852 if (!pw
->pw_passwd
[0])
853 # endif /* HAVE_SHADOW_H */
856 * Don't allow blank passwords!
859 cupsdLogMessage(CUPSD_LOG_ERROR
,
860 "[Client %d] Username \"%s\" has no password.",
861 con
->http
.fd
, username
);
866 * OK, the password isn't blank, so compare with what came from the
870 pass
= cups_crypt(password
, pw
->pw_passwd
);
872 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
873 "[Client %d] pw_passwd=\"%s\", crypt=\"%s\"",
874 con
->http
.fd
, pw
->pw_passwd
, pass
);
876 if (!pass
|| strcmp(pw
->pw_passwd
, pass
))
878 # ifdef HAVE_SHADOW_H
881 pass
= cups_crypt(password
, spw
->sp_pwdp
);
883 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
884 "[Client %d] sp_pwdp=\"%s\", crypt=\"%s\"",
885 con
->http
.fd
, spw
->sp_pwdp
, pass
);
887 if (pass
== NULL
|| strcmp(spw
->sp_pwdp
, pass
))
889 cupsdLogMessage(CUPSD_LOG_ERROR
,
890 "[Client %d] Authentication failed for user "
891 "\"%s\".", con
->http
.fd
, username
);
896 # endif /* HAVE_SHADOW_H */
898 cupsdLogMessage(CUPSD_LOG_ERROR
,
899 "[Client %d] Authentication failed for user "
900 "\"%s\".", con
->http
.fd
, username
);
904 #endif /* HAVE_LIBPAM */
907 cupsdLogMessage(CUPSD_LOG_DEBUG
,
908 "[Client %d] Authorized as %s using Basic",
909 con
->http
.fd
, username
);
912 case CUPSD_AUTH_BASICDIGEST
:
914 * Do Basic authentication with the Digest password file...
917 if (!get_md5_password(username
, NULL
, md5
))
919 cupsdLogMessage(CUPSD_LOG_ERROR
,
920 "[Client %d] Unknown MD5 username \"%s\".",
921 con
->http
.fd
, username
);
925 httpMD5(username
, "CUPS", password
, basicmd5
);
927 if (strcmp(md5
, basicmd5
))
929 cupsdLogMessage(CUPSD_LOG_ERROR
,
930 "[Client %d] Authentication failed for \"%s\".",
931 con
->http
.fd
, username
);
935 cupsdLogMessage(CUPSD_LOG_DEBUG
,
936 "[Client %d] Authorized as %s using BasicDigest",
937 con
->http
.fd
, username
);
943 else if (!strncmp(authorization
, "Digest", 6))
946 * Get the username, password, and nonce from the Digest attributes...
949 if (!httpGetSubField2(&(con
->http
), HTTP_FIELD_AUTHORIZATION
, "username",
950 username
, sizeof(username
)) || !username
[0])
953 * Username must not be empty...
956 cupsdLogMessage(CUPSD_LOG_ERROR
,
957 "[Client %d] Empty or missing Digest username.",
962 if (!httpGetSubField2(&(con
->http
), HTTP_FIELD_AUTHORIZATION
, "response",
963 password
, sizeof(password
)) || !password
[0])
966 * Password must not be empty...
969 cupsdLogMessage(CUPSD_LOG_ERROR
,
970 "[Client %d] Empty or missing Digest password.",
975 if (!httpGetSubField(&(con
->http
), HTTP_FIELD_AUTHORIZATION
, "nonce",
978 cupsdLogMessage(CUPSD_LOG_ERROR
,
979 "[Client %d] No nonce value for Digest authentication.",
984 if (strcmp(con
->http
.hostname
, nonce
))
986 cupsdLogMessage(CUPSD_LOG_ERROR
,
987 "[Client %d] Bad nonce value, expected \"%s\", "
988 "got \"%s\".", con
->http
.fd
, con
->http
.hostname
, nonce
);
993 * Validate the username and password...
996 if (!get_md5_password(username
, NULL
, md5
))
998 cupsdLogMessage(CUPSD_LOG_ERROR
,
999 "[Client %d] Unknown MD5 username \"%s\".",
1000 con
->http
.fd
, username
);
1004 httpMD5Final(nonce
, states
[con
->http
.state
], con
->uri
, md5
);
1006 if (strcmp(md5
, password
))
1008 cupsdLogMessage(CUPSD_LOG_ERROR
,
1009 "[Client %d] Authentication failed for \"%s\".",
1010 con
->http
.fd
, username
);
1014 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1015 "[Client %d] Authorized as %s using Digest", con
->http
.fd
,
1018 con
->type
= CUPSD_AUTH_DIGEST
;
1021 else if (!strncmp(authorization
, "Negotiate", 9))
1023 int len
; /* Length of authorization string */
1024 gss_ctx_id_t context
; /* Authorization context */
1025 OM_uint32 major_status
, /* Major status code */
1026 minor_status
; /* Minor status code */
1027 gss_buffer_desc input_token
= GSS_C_EMPTY_BUFFER
,
1028 /* Input token from string */
1029 output_token
= GSS_C_EMPTY_BUFFER
;
1030 /* Output token for username */
1031 gss_name_t client_name
; /* Client name */
1036 * If the weak-linked GSSAPI/Kerberos library is not present, don't try
1040 if (gss_init_sec_context
== NULL
)
1042 cupsdLogMessage(CUPSD_LOG_WARN
,
1043 "[Client %d] GSSAPI/Kerberos authentication failed "
1044 "because the Kerberos framework is not present.",
1048 # endif /* __APPLE__ */
1051 * Find the start of the Kerberos input token...
1055 while (isspace(*authorization
& 255))
1058 if (!*authorization
)
1060 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1061 "[Client %d] No authentication data specified.",
1067 * Decode the authorization string to get the input token...
1070 len
= strlen(authorization
);
1071 input_token
.value
= malloc(len
);
1072 input_token
.value
= httpDecode64_2(input_token
.value
, &len
,
1074 input_token
.length
= len
;
1077 * Accept the input token to get the authorization info...
1080 context
= GSS_C_NO_CONTEXT
;
1081 client_name
= GSS_C_NO_NAME
;
1082 major_status
= gss_accept_sec_context(&minor_status
,
1086 GSS_C_NO_CHANNEL_BINDINGS
,
1094 if (output_token
.length
> 0)
1095 gss_release_buffer(&minor_status
, &output_token
);
1097 if (GSS_ERROR(major_status
))
1099 cupsdLogGSSMessage(CUPSD_LOG_DEBUG
, major_status
, minor_status
,
1100 "[Client %d] Error accepting GSSAPI security context",
1103 if (context
!= GSS_C_NO_CONTEXT
)
1104 gss_delete_sec_context(&minor_status
, &context
, GSS_C_NO_BUFFER
);
1111 * Get the username associated with the client's credentials...
1114 if (major_status
== GSS_S_CONTINUE_NEEDED
)
1115 cupsdLogGSSMessage(CUPSD_LOG_DEBUG
, major_status
, minor_status
,
1116 "[Client %d] Credentials not complete", con
->http
.fd
);
1117 else if (major_status
== GSS_S_COMPLETE
)
1119 major_status
= gss_display_name(&minor_status
, client_name
,
1120 &output_token
, NULL
);
1122 if (GSS_ERROR(major_status
))
1124 cupsdLogGSSMessage(CUPSD_LOG_DEBUG
, major_status
, minor_status
,
1125 "[Client %d] Error getting username", con
->http
.fd
);
1126 gss_release_name(&minor_status
, &client_name
);
1127 gss_delete_sec_context(&minor_status
, &context
, GSS_C_NO_BUFFER
);
1131 strlcpy(username
, output_token
.value
, sizeof(username
));
1133 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1134 "[Client %d] Authorized as %s using Negotiate",
1135 con
->http
.fd
, username
);
1137 gss_release_name(&minor_status
, &client_name
);
1138 gss_release_buffer(&minor_status
, &output_token
);
1140 con
->type
= CUPSD_AUTH_NEGOTIATE
;
1143 gss_delete_sec_context(&minor_status
, &context
, GSS_C_NO_BUFFER
);
1145 # if defined(SO_PEERCRED) && defined(AF_LOCAL)
1147 * Get the client's UID if we are printing locally - that allows a backend
1148 * to run as the correct user to get Kerberos credentials of its own.
1151 if (_httpAddrFamily(con
->http
.hostaddr
) == AF_LOCAL
)
1153 cupsd_ucred_t peercred
; /* Peer credentials */
1154 socklen_t peersize
; /* Size of peer credentials */
1156 peersize
= sizeof(peercred
);
1159 if (getsockopt(con
->http
.fd
, 0, LOCAL_PEERCRED
, &peercred
, &peersize
))
1161 if (getsockopt(con
->http
.fd
, SOL_SOCKET
, SO_PEERCRED
, &peercred
,
1163 # endif /* __APPLE__ */
1165 cupsdLogMessage(CUPSD_LOG_ERROR
,
1166 "[Client %d] Unable to get peer credentials - %s",
1167 con
->http
.fd
, strerror(errno
));
1171 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1172 "[Client %d] Using credentials for UID %d.",
1173 con
->http
.fd
, CUPSD_UCRED_UID(peercred
));
1174 con
->gss_uid
= CUPSD_UCRED_UID(peercred
);
1177 # endif /* SO_PEERCRED && AF_LOCAL */
1179 #endif /* HAVE_GSSAPI */
1182 char scheme
[256]; /* Auth scheme... */
1185 if (sscanf(authorization
, "%255s", scheme
) != 1)
1186 strlcpy(scheme
, "UNKNOWN", sizeof(scheme
));
1188 cupsdLogMessage(CUPSD_LOG_ERROR
,
1189 "[Client %d] Bad authentication data \"%s ...\"",
1190 con
->http
.fd
, scheme
);
1195 * If we get here, then we were able to validate the username and
1196 * password - copy the validated username and password to the client
1197 * data and return...
1200 strlcpy(con
->username
, username
, sizeof(con
->username
));
1201 strlcpy(con
->password
, password
, sizeof(con
->password
));
1206 * 'cupsdCheckAccess()' - Check whether the given address is allowed to
1207 * access a location.
1210 int /* O - 1 if allowed, 0 otherwise */
1212 unsigned ip
[4], /* I - Client address */
1213 char *name
, /* I - Client hostname */
1214 int namelen
, /* I - Length of hostname */
1215 cupsd_location_t
*loc
) /* I - Location to check */
1217 int allow
; /* 1 if allowed, 0 otherwise */
1220 if (!_cups_strcasecmp(name
, "localhost"))
1223 * Access from localhost (127.0.0.1 or ::1) is always allowed...
1231 * Do authorization checks on the domain/address...
1234 switch (loc
->order_type
)
1237 allow
= 0; /* anti-compiler-warning-code */
1240 case CUPSD_AUTH_ALLOW
: /* Order Deny,Allow */
1243 if (cupsdCheckAuth(ip
, name
, namelen
, loc
->deny
))
1246 if (cupsdCheckAuth(ip
, name
, namelen
, loc
->allow
))
1250 case CUPSD_AUTH_DENY
: /* Order Allow,Deny */
1253 if (cupsdCheckAuth(ip
, name
, namelen
, loc
->allow
))
1256 if (cupsdCheckAuth(ip
, name
, namelen
, loc
->deny
))
1267 * 'cupsdCheckAuth()' - Check authorization masks.
1270 int /* O - 1 if mask matches, 0 otherwise */
1271 cupsdCheckAuth(unsigned ip
[4], /* I - Client address */
1272 char *name
, /* I - Client hostname */
1273 int name_len
, /* I - Length of hostname */
1274 cups_array_t
*masks
) /* I - Masks */
1276 int i
; /* Looping var */
1277 cupsd_authmask_t
*mask
; /* Current mask */
1278 cupsd_netif_t
*iface
; /* Network interface */
1279 unsigned netip4
; /* IPv4 network address */
1281 unsigned netip6
[4]; /* IPv6 network address */
1282 #endif /* AF_INET6 */
1285 for (mask
= (cupsd_authmask_t
*)cupsArrayFirst(masks
);
1287 mask
= (cupsd_authmask_t
*)cupsArrayNext(masks
))
1291 case CUPSD_AUTH_INTERFACE
:
1293 * Check for a match with a network interface...
1296 netip4
= htonl(ip
[3]);
1299 netip6
[0] = htonl(ip
[0]);
1300 netip6
[1] = htonl(ip
[1]);
1301 netip6
[2] = htonl(ip
[2]);
1302 netip6
[3] = htonl(ip
[3]);
1303 #endif /* AF_INET6 */
1305 if (!strcmp(mask
->mask
.name
.name
, "*"))
1309 * Allow Back-to-My-Mac addresses...
1312 if ((ip
[0] & 0xff000000) == 0xfd000000)
1314 #endif /* __APPLE__ */
1317 * Check against all local interfaces...
1322 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
1324 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
1327 * Only check local interfaces...
1330 if (!iface
->is_local
)
1333 if (iface
->address
.addr
.sa_family
== AF_INET
)
1336 * Check IPv4 address...
1339 if ((netip4
& iface
->mask
.ipv4
.sin_addr
.s_addr
) ==
1340 (iface
->address
.ipv4
.sin_addr
.s_addr
&
1341 iface
->mask
.ipv4
.sin_addr
.s_addr
))
1348 * Check IPv6 address...
1351 for (i
= 0; i
< 4; i
++)
1352 if ((netip6
[i
] & iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]) !=
1353 (iface
->address
.ipv6
.sin6_addr
.s6_addr32
[i
] &
1354 iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]))
1360 #endif /* AF_INET6 */
1366 * Check the named interface...
1369 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
1371 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
1373 if (strcmp(mask
->mask
.name
.name
, iface
->name
))
1376 if (iface
->address
.addr
.sa_family
== AF_INET
)
1379 * Check IPv4 address...
1382 if ((netip4
& iface
->mask
.ipv4
.sin_addr
.s_addr
) ==
1383 (iface
->address
.ipv4
.sin_addr
.s_addr
&
1384 iface
->mask
.ipv4
.sin_addr
.s_addr
))
1391 * Check IPv6 address...
1394 for (i
= 0; i
< 4; i
++)
1395 if ((netip6
[i
] & iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]) !=
1396 (iface
->address
.ipv6
.sin6_addr
.s6_addr32
[i
] &
1397 iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]))
1403 #endif /* AF_INET6 */
1408 case CUPSD_AUTH_NAME
:
1410 * Check for exact name match...
1413 if (!_cups_strcasecmp(name
, mask
->mask
.name
.name
))
1417 * Check for domain match...
1420 if (name_len
>= mask
->mask
.name
.length
&&
1421 mask
->mask
.name
.name
[0] == '.' &&
1422 !_cups_strcasecmp(name
+ name_len
- mask
->mask
.name
.length
,
1423 mask
->mask
.name
.name
))
1427 case CUPSD_AUTH_IP
:
1429 * Check for IP/network address match...
1432 for (i
= 0; i
< 4; i
++)
1433 if ((ip
[i
] & mask
->mask
.ip
.netmask
[i
]) !=
1434 mask
->mask
.ip
.address
[i
])
1448 * 'cupsdCheckGroup()' - Check for a user's group membership.
1451 int /* O - 1 if user is a member, 0 otherwise */
1453 const char *username
, /* I - User name */
1454 struct passwd
*user
, /* I - System user info */
1455 const char *groupname
) /* I - Group name */
1457 int i
; /* Looping var */
1458 struct group
*group
; /* System group info */
1459 char junk
[33]; /* MD5 password (not used) */
1460 #ifdef HAVE_MBR_UID_TO_UUID
1461 uuid_t useruuid
, /* UUID for username */
1462 groupuuid
; /* UUID for groupname */
1463 int is_member
; /* True if user is a member of group */
1464 #endif /* HAVE_MBR_UID_TO_UUID */
1467 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1468 "cupsdCheckGroup(username=\"%s\", user=%p, groupname=\"%s\")",
1469 username
, user
, groupname
);
1475 if (!username
|| !groupname
)
1479 * Check to see if the user is a member of the named group...
1482 group
= getgrnam(groupname
);
1488 * Group exists, check it...
1491 for (i
= 0; group
->gr_mem
[i
]; i
++)
1492 if (!_cups_strcasecmp(username
, group
->gr_mem
[i
]))
1497 * Group doesn't exist or user not in group list, check the group ID
1498 * against the user's group ID...
1501 if (user
&& group
&& group
->gr_gid
== user
->pw_gid
)
1504 #ifdef HAVE_MBR_UID_TO_UUID
1506 * Check group membership through MacOS X membership API...
1509 if (user
&& !mbr_uid_to_uuid(user
->pw_uid
, useruuid
))
1514 * Map group name to UUID and check membership...
1517 if (!mbr_gid_to_uuid(group
->gr_gid
, groupuuid
))
1518 if (!mbr_check_membership(useruuid
, groupuuid
, &is_member
))
1522 else if (groupname
[0] == '#')
1525 * Use UUID directly and check for equality (user UUID) and
1526 * membership (group UUID)...
1529 if (!uuid_parse((char *)groupname
+ 1, groupuuid
))
1531 if (!uuid_compare(useruuid
, groupuuid
))
1533 else if (!mbr_check_membership(useruuid
, groupuuid
, &is_member
))
1541 else if (groupname
[0] == '#')
1543 #endif /* HAVE_MBR_UID_TO_UUID */
1546 * Username not found, group not found, or user is not part of the
1547 * system group... Check for a user and group in the MD5 password
1551 if (get_md5_password(username
, groupname
, junk
) != NULL
)
1555 * If we get this far, then the user isn't part of the named group...
1563 * 'cupsdCopyLocation()' - Make a copy of a location...
1566 cupsd_location_t
* /* O - New location */
1568 cupsd_location_t
*loc
) /* I - Original location */
1570 cupsd_location_t
*temp
; /* New location */
1574 * Make a copy of the original location...
1577 if ((temp
= calloc(1, sizeof(cupsd_location_t
))) == NULL
)
1581 * Copy the information from the original location to the new one.
1588 temp
->location
= _cupsStrAlloc(loc
->location
);
1590 temp
->limit
= loc
->limit
;
1591 temp
->order_type
= loc
->order_type
;
1592 temp
->type
= loc
->type
;
1593 temp
->level
= loc
->level
;
1594 temp
->satisfy
= loc
->satisfy
;
1595 temp
->encryption
= loc
->encryption
;
1599 if ((temp
->names
= cupsArrayDup(loc
->names
)) == NULL
)
1601 cupsdLogMessage(CUPSD_LOG_ERROR
,
1602 "Unable to allocate memory for %d names: %s",
1603 cupsArrayCount(loc
->names
), strerror(errno
));
1605 cupsdFreeLocation(temp
);
1613 * Copy allow rules...
1616 if ((temp
->allow
= cupsArrayDup(loc
->allow
)) == NULL
)
1618 cupsdLogMessage(CUPSD_LOG_ERROR
,
1619 "Unable to allocate memory for %d allow rules: %s",
1620 cupsArrayCount(loc
->allow
), strerror(errno
));
1621 cupsdFreeLocation(temp
);
1629 * Copy deny rules...
1632 if ((temp
->deny
= cupsArrayDup(loc
->deny
)) == NULL
)
1634 cupsdLogMessage(CUPSD_LOG_ERROR
,
1635 "Unable to allocate memory for %d deny rules: %s",
1636 cupsArrayCount(loc
->deny
), strerror(errno
));
1637 cupsdFreeLocation(temp
);
1647 * 'cupsdDeleteAllLocations()' - Free all memory used for location authorization.
1651 cupsdDeleteAllLocations(void)
1654 * Free the location array, which will free all of the locations...
1657 cupsArrayDelete(Locations
);
1663 * 'cupsdFindBest()' - Find the location entry that best matches the resource.
1666 cupsd_location_t
* /* O - Location that matches */
1667 cupsdFindBest(const char *path
, /* I - Resource path */
1668 http_state_t state
) /* I - HTTP state/request */
1670 char uri
[HTTP_MAX_URI
],
1671 /* URI in request... */
1672 *uriptr
; /* Pointer into URI */
1673 cupsd_location_t
*loc
, /* Current location */
1674 *best
; /* Best match for location so far */
1675 int bestlen
; /* Length of best match */
1676 int limit
; /* Limit field */
1677 static const int limits
[] = /* Map http_status_t to CUPSD_AUTH_LIMIT_xyz */
1679 CUPSD_AUTH_LIMIT_ALL
,
1680 CUPSD_AUTH_LIMIT_OPTIONS
,
1681 CUPSD_AUTH_LIMIT_GET
,
1682 CUPSD_AUTH_LIMIT_GET
,
1683 CUPSD_AUTH_LIMIT_HEAD
,
1684 CUPSD_AUTH_LIMIT_POST
,
1685 CUPSD_AUTH_LIMIT_POST
,
1686 CUPSD_AUTH_LIMIT_POST
,
1687 CUPSD_AUTH_LIMIT_PUT
,
1688 CUPSD_AUTH_LIMIT_PUT
,
1689 CUPSD_AUTH_LIMIT_DELETE
,
1690 CUPSD_AUTH_LIMIT_TRACE
,
1691 CUPSD_AUTH_LIMIT_ALL
,
1692 CUPSD_AUTH_LIMIT_ALL
1697 * First copy the connection URI to a local string so we have drop
1698 * any .ppd extension from the pathname in /printers or /classes
1702 strlcpy(uri
, path
, sizeof(uri
));
1704 if (!strncmp(uri
, "/printers/", 10) ||
1705 !strncmp(uri
, "/classes/", 9))
1708 * Check if the URI has .ppd on the end...
1711 uriptr
= uri
+ strlen(uri
) - 4; /* len > 4 if we get here... */
1713 if (!strcmp(uriptr
, ".ppd"))
1717 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: uri = \"%s\"...", uri
);
1720 * Loop through the list of locations to find a match...
1723 limit
= limits
[state
];
1727 for (loc
= (cupsd_location_t
*)cupsArrayFirst(Locations
);
1729 loc
= (cupsd_location_t
*)cupsArrayNext(Locations
))
1731 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: Location %s Limit %x",
1732 loc
->location
? loc
->location
: "nil", loc
->limit
);
1734 if (!strncmp(uri
, "/printers/", 10) || !strncmp(uri
, "/classes/", 9))
1737 * Use case-insensitive comparison for queue names...
1740 if (loc
->length
> bestlen
&& loc
->location
&&
1741 !_cups_strncasecmp(uri
, loc
->location
, loc
->length
) &&
1742 loc
->location
[0] == '/' &&
1743 (limit
& loc
->limit
) != 0)
1746 bestlen
= loc
->length
;
1752 * Use case-sensitive comparison for other URIs...
1755 if (loc
->length
> bestlen
&& loc
->location
&&
1756 !strncmp(uri
, loc
->location
, loc
->length
) &&
1757 loc
->location
[0] == '/' &&
1758 (limit
& loc
->limit
) != 0)
1761 bestlen
= loc
->length
;
1767 * Return the match, if any...
1770 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: best = %s",
1771 best
? best
->location
: "NONE");
1778 * 'cupsdFindLocation()' - Find the named location.
1781 cupsd_location_t
* /* O - Location that matches */
1782 cupsdFindLocation(const char *location
) /* I - Connection */
1784 cupsd_location_t key
; /* Search key */
1787 key
.location
= (char *)location
;
1789 return ((cupsd_location_t
*)cupsArrayFind(Locations
, &key
));
1794 * 'cupsdFreeLocation()' - Free all memory used by a location.
1798 cupsdFreeLocation(cupsd_location_t
*loc
)/* I - Location to free */
1800 cupsArrayDelete(loc
->names
);
1801 cupsArrayDelete(loc
->allow
);
1802 cupsArrayDelete(loc
->deny
);
1804 _cupsStrFree(loc
->location
);
1810 * 'cupsdIsAuthorized()' - Check to see if the user is authorized...
1813 http_status_t
/* O - HTTP_OK if authorized or error code */
1814 cupsdIsAuthorized(cupsd_client_t
*con
, /* I - Connection */
1815 const char *owner
)/* I - Owner of object */
1817 int i
, /* Looping vars */
1818 auth
, /* Authorization status */
1819 type
; /* Type of authentication */
1820 unsigned address
[4]; /* Authorization address */
1821 cupsd_location_t
*best
; /* Best match for location so far */
1822 int hostlen
; /* Length of hostname */
1823 char *name
, /* Current username */
1824 username
[256], /* Username to authorize */
1825 ownername
[256], /* Owner name to authorize */
1826 *ptr
; /* Pointer into username */
1827 struct passwd
*pw
; /* User password data */
1828 static const char * const levels
[] = /* Auth levels */
1834 static const char * const types
[] = /* Auth types */
1844 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1845 "cupsdIsAuthorized: con->uri=\"%s\", con->best=%p(%s)",
1846 con
->uri
, con
->best
, con
->best
? con
->best
->location
?
1847 con
->best
->location
: "(null)" : "");
1849 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1850 "cupsdIsAuthorized: owner=\"%s\"", owner
);
1853 * If there is no "best" authentication rule for this request, then
1854 * access is allowed from the local system and denied from other
1860 if (!strcmp(con
->http
.hostname
, "localhost") ||
1861 !strcmp(con
->http
.hostname
, ServerName
))
1864 return (HTTP_FORBIDDEN
);
1869 if ((type
= best
->type
) == CUPSD_AUTH_DEFAULT
)
1870 type
= cupsdDefaultAuthType();
1872 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1873 "cupsdIsAuthorized: level=CUPSD_AUTH_%s, type=%s, "
1874 "satisfy=CUPSD_AUTH_SATISFY_%s, num_names=%d",
1875 levels
[best
->level
], types
[type
],
1876 best
->satisfy
? "ANY" : "ALL", cupsArrayCount(best
->names
));
1878 if (best
->limit
== CUPSD_AUTH_LIMIT_IPP
)
1879 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: op=%x(%s)",
1880 best
->op
, ippOpString(best
->op
));
1883 * Check host/ip-based accesses...
1887 if (con
->http
.hostaddr
->addr
.sa_family
== AF_INET6
)
1890 * Copy IPv6 address...
1893 address
[0] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[0]);
1894 address
[1] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[1]);
1895 address
[2] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[2]);
1896 address
[3] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[3]);
1899 #endif /* AF_INET6 */
1900 if (con
->http
.hostaddr
->addr
.sa_family
== AF_INET
)
1903 * Copy IPv4 address...
1909 address
[3] = ntohl(con
->http
.hostaddr
->ipv4
.sin_addr
.s_addr
);
1912 memset(address
, 0, sizeof(address
));
1914 hostlen
= strlen(con
->http
.hostname
);
1916 auth
= cupsdCheckAccess(address
, con
->http
.hostname
, hostlen
, best
)
1917 ? CUPSD_AUTH_ALLOW
: CUPSD_AUTH_DENY
;
1919 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: auth=CUPSD_AUTH_%s...",
1920 auth
? "DENY" : "ALLOW");
1922 if (auth
== CUPSD_AUTH_DENY
&& best
->satisfy
== CUPSD_AUTH_SATISFY_ALL
)
1923 return (HTTP_FORBIDDEN
);
1927 * See if encryption is required...
1930 if ((best
->encryption
>= HTTP_ENCRYPT_REQUIRED
&& !con
->http
.tls
&&
1931 _cups_strcasecmp(con
->http
.hostname
, "localhost") &&
1932 best
->satisfy
== CUPSD_AUTH_SATISFY_ALL
) &&
1933 !(type
== CUPSD_AUTH_NEGOTIATE
||
1934 (type
== CUPSD_AUTH_NONE
&&
1935 cupsdDefaultAuthType() == CUPSD_AUTH_NEGOTIATE
)))
1937 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1938 "cupsdIsAuthorized: Need upgrade to TLS...");
1939 return (HTTP_UPGRADE_REQUIRED
);
1941 #endif /* HAVE_SSL */
1944 * Now see what access level is required...
1947 if (best
->level
== CUPSD_AUTH_ANON
|| /* Anonymous access - allow it */
1948 (type
== CUPSD_AUTH_NONE
&& cupsArrayCount(best
->names
) == 0))
1951 if (!con
->username
[0] && type
== CUPSD_AUTH_NONE
&&
1952 best
->limit
== CUPSD_AUTH_LIMIT_IPP
)
1955 * Check for unauthenticated username...
1958 ipp_attribute_t
*attr
; /* requesting-user-name attribute */
1961 attr
= ippFindAttribute(con
->request
, "requesting-user-name", IPP_TAG_NAME
);
1964 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1965 "cupsdIsAuthorized: requesting-user-name=\"%s\"",
1966 attr
->values
[0].string
.text
);
1967 strlcpy(username
, attr
->values
[0].string
.text
, sizeof(username
));
1969 else if (best
->satisfy
== CUPSD_AUTH_SATISFY_ALL
|| auth
== CUPSD_AUTH_DENY
)
1970 return (HTTP_UNAUTHORIZED
); /* Non-anonymous needs user/pass */
1972 return (HTTP_OK
); /* unless overridden with Satisfy */
1976 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdIsAuthorized: username=\"%s\"",
1979 #ifdef HAVE_AUTHORIZATION_H
1980 if (!con
->username
[0] && !con
->authref
)
1982 if (!con
->username
[0])
1983 #endif /* HAVE_AUTHORIZATION_H */
1985 if (best
->satisfy
== CUPSD_AUTH_SATISFY_ALL
|| auth
== CUPSD_AUTH_DENY
)
1986 return (HTTP_UNAUTHORIZED
); /* Non-anonymous needs user/pass */
1988 return (HTTP_OK
); /* unless overridden with Satisfy */
1992 if (con
->type
!= type
&& type
!= CUPSD_AUTH_NONE
&&
1994 (type
!= CUPSD_AUTH_NEGOTIATE
|| con
->gss_uid
<= 0) &&
1995 #endif /* HAVE_GSSAPI */
1996 (con
->type
!= CUPSD_AUTH_BASIC
|| type
!= CUPSD_AUTH_BASICDIGEST
))
1998 cupsdLogMessage(CUPSD_LOG_ERROR
, "Authorized using %s, expected %s!",
1999 types
[con
->type
], types
[type
]);
2001 return (HTTP_UNAUTHORIZED
);
2004 strlcpy(username
, con
->username
, sizeof(username
));
2008 * OK, got a username. See if we need normal user access, or group
2009 * access... (root always matches)
2012 if (!strcmp(username
, "root"))
2016 * Strip any @domain or @KDC from the username and owner...
2019 if ((ptr
= strchr(username
, '@')) != NULL
)
2024 strlcpy(ownername
, owner
, sizeof(ownername
));
2026 if ((ptr
= strchr(ownername
, '@')) != NULL
)
2030 ownername
[0] = '\0';
2033 * Get the user info...
2038 pw
= getpwnam(username
);
2044 if (best
->level
== CUPSD_AUTH_USER
)
2047 * If there are no names associated with this location, then
2048 * any valid user is OK...
2051 if (cupsArrayCount(best
->names
) == 0)
2055 * Otherwise check the user list and return OK if this user is
2059 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2060 "cupsdIsAuthorized: Checking user membership...");
2062 #ifdef HAVE_AUTHORIZATION_H
2064 * If an authorization reference was supplied it must match a right name...
2069 for (name
= (char *)cupsArrayFirst(best
->names
);
2071 name
= (char *)cupsArrayNext(best
->names
))
2073 if (!_cups_strncasecmp(name
, "@AUTHKEY(", 9) && check_authref(con
, name
+ 9))
2075 else if (!_cups_strcasecmp(name
, "@SYSTEM") && SystemGroupAuthKey
&&
2076 check_authref(con
, SystemGroupAuthKey
))
2080 return (HTTP_FORBIDDEN
);
2082 #endif /* HAVE_AUTHORIZATION_H */
2084 for (name
= (char *)cupsArrayFirst(best
->names
);
2086 name
= (char *)cupsArrayNext(best
->names
))
2088 if (!_cups_strcasecmp(name
, "@OWNER") && owner
&&
2089 !_cups_strcasecmp(username
, ownername
))
2091 else if (!_cups_strcasecmp(name
, "@SYSTEM"))
2093 for (i
= 0; i
< NumSystemGroups
; i
++)
2094 if (cupsdCheckGroup(username
, pw
, SystemGroups
[i
]))
2097 else if (name
[0] == '@')
2099 if (cupsdCheckGroup(username
, pw
, name
+ 1))
2102 else if (!_cups_strcasecmp(username
, name
))
2106 return (con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
);
2110 * Check to see if this user is in any of the named groups...
2113 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2114 "cupsdIsAuthorized: Checking group membership...");
2117 * Check to see if this user is in any of the named groups...
2120 for (name
= (char *)cupsArrayFirst(best
->names
);
2122 name
= (char *)cupsArrayNext(best
->names
))
2124 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2125 "cupsdIsAuthorized: Checking group \"%s\" membership...",
2128 if (!_cups_strcasecmp(name
, "@SYSTEM"))
2130 for (i
= 0; i
< NumSystemGroups
; i
++)
2131 if (cupsdCheckGroup(username
, pw
, SystemGroups
[i
]))
2134 else if (cupsdCheckGroup(username
, pw
, name
))
2139 * The user isn't part of the specified group, so deny access...
2142 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2143 "cupsdIsAuthorized: User not in group(s)!");
2145 return (con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
);
2150 * 'cupsdNewLocation()' - Create a new location for authorization.
2152 * Note: Still need to call cupsdAddLocation() to add it to the list of global
2156 cupsd_location_t
* /* O - Pointer to new location record */
2157 cupsdNewLocation(const char *location
) /* I - Location path */
2159 cupsd_location_t
*temp
; /* New location */
2163 * Try to allocate memory for the new location.
2166 if ((temp
= calloc(1, sizeof(cupsd_location_t
))) == NULL
)
2170 * Initialize the record and copy the name over...
2173 if ((temp
->location
= _cupsStrAlloc(location
)) == NULL
)
2179 temp
->length
= strlen(temp
->location
);
2182 * Return the new record...
2189 #ifdef HAVE_AUTHORIZATION_H
2191 * 'check_authref()' - Check if an authorization services reference has the
2195 static int /* O - 1 if right is valid, 0 otherwise */
2196 check_authref(cupsd_client_t
*con
, /* I - Connection */
2197 const char *right
) /* I - Right name */
2199 OSStatus status
; /* OS Status */
2200 AuthorizationItem authright
; /* Authorization right */
2201 AuthorizationRights authrights
; /* Authorization rights */
2202 AuthorizationFlags authflags
; /* Authorization flags */
2206 * Check to see if the user is allowed to perform the task...
2212 authright
.name
= right
;
2213 authright
.valueLength
= 0;
2214 authright
.value
= NULL
;
2215 authright
.flags
= 0;
2217 authrights
.count
= 1;
2218 authrights
.items
= &authright
;
2220 authflags
= kAuthorizationFlagDefaults
|
2221 kAuthorizationFlagExtendRights
;
2223 if ((status
= AuthorizationCopyRights(con
->authref
, &authrights
,
2224 kAuthorizationEmptyEnvironment
,
2225 authflags
, NULL
)) != 0)
2227 cupsdLogMessage(CUPSD_LOG_ERROR
,
2228 "AuthorizationCopyRights(\"%s\") returned %d (%s)",
2229 authright
.name
, (int)status
, cssmErrorString(status
));
2233 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2234 "AuthorizationCopyRights(\"%s\") succeeded!",
2239 #endif /* HAVE_AUTHORIZATION_H */
2243 * 'compare_locations()' - Compare two locations.
2246 static int /* O - Result of comparison */
2247 compare_locations(cupsd_location_t
*a
, /* I - First location */
2248 cupsd_location_t
*b
) /* I - Second location */
2250 return (strcmp(b
->location
, a
->location
));
2255 * 'copy_authmask()' - Copy function for auth masks.
2258 static cupsd_authmask_t
* /* O - New auth mask */
2259 copy_authmask(cupsd_authmask_t
*mask
, /* I - Existing auth mask */
2260 void *data
) /* I - User data (unused) */
2262 cupsd_authmask_t
*temp
; /* New auth mask */
2267 if ((temp
= malloc(sizeof(cupsd_authmask_t
))) != NULL
)
2269 memcpy(temp
, mask
, sizeof(cupsd_authmask_t
));
2271 if (temp
->type
== CUPSD_AUTH_NAME
|| temp
->type
== CUPSD_AUTH_INTERFACE
)
2274 * Make a copy of the name...
2277 if ((temp
->mask
.name
.name
= _cupsStrAlloc(temp
->mask
.name
.name
)) == NULL
)
2280 * Failed to make copy...
2293 #if !HAVE_LIBPAM && !defined(HAVE_USERSEC_H)
2295 * 'cups_crypt()' - Encrypt the password using the DES or MD5 algorithms,
2299 static char * /* O - Encrypted password */
2300 cups_crypt(const char *pw
, /* I - Password string */
2301 const char *salt
) /* I - Salt (key) string */
2303 if (!strncmp(salt
, "$1$", 3))
2306 * Use MD5 passwords without the benefit of PAM; this is for
2307 * Slackware Linux, and the algorithm was taken from the
2308 * old shadow-19990827/lib/md5crypt.c source code... :(
2311 int i
; /* Looping var */
2312 unsigned long n
; /* Output number */
2313 int pwlen
; /* Length of password string */
2314 const char *salt_end
; /* End of "salt" data for MD5 */
2315 char *ptr
; /* Pointer into result string */
2316 _cups_md5_state_t state
; /* Primary MD5 state info */
2317 _cups_md5_state_t state2
; /* Secondary MD5 state info */
2318 unsigned char digest
[16]; /* MD5 digest result */
2319 static char result
[120]; /* Final password string */
2323 * Get the salt data between dollar signs, e.g. $1$saltdata$md5.
2324 * Get a maximum of 8 characters of salt data after $1$...
2327 for (salt_end
= salt
+ 3; *salt_end
&& (salt_end
- salt
) < 11; salt_end
++)
2328 if (*salt_end
== '$')
2332 * Compute the MD5 sum we need...
2337 _cupsMD5Init(&state
);
2338 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
2339 _cupsMD5Append(&state
, (unsigned char *)salt
, salt_end
- salt
);
2341 _cupsMD5Init(&state2
);
2342 _cupsMD5Append(&state2
, (unsigned char *)pw
, pwlen
);
2343 _cupsMD5Append(&state2
, (unsigned char *)salt
+ 3, salt_end
- salt
- 3);
2344 _cupsMD5Append(&state2
, (unsigned char *)pw
, pwlen
);
2345 _cupsMD5Finish(&state2
, digest
);
2347 for (i
= pwlen
; i
> 0; i
-= 16)
2348 _cupsMD5Append(&state
, digest
, i
> 16 ? 16 : i
);
2350 for (i
= pwlen
; i
> 0; i
>>= 1)
2351 _cupsMD5Append(&state
, (unsigned char *)((i
& 1) ? "" : pw
), 1);
2353 _cupsMD5Finish(&state
, digest
);
2355 for (i
= 0; i
< 1000; i
++)
2357 _cupsMD5Init(&state
);
2360 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
2362 _cupsMD5Append(&state
, digest
, 16);
2365 _cupsMD5Append(&state
, (unsigned char *)salt
+ 3, salt_end
- salt
- 3);
2368 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
2371 _cupsMD5Append(&state
, digest
, 16);
2373 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
2375 _cupsMD5Finish(&state
, digest
);
2379 * Copy the final sum to the result string and return...
2382 memcpy(result
, salt
, salt_end
- salt
);
2383 ptr
= result
+ (salt_end
- salt
);
2386 for (i
= 0; i
< 5; i
++, ptr
+= 4)
2388 n
= (((digest
[i
] << 8) | digest
[i
+ 6]) << 8);
2391 n
|= digest
[i
+ 12];
2398 to64(ptr
, digest
[11], 2);
2407 * Use the standard crypt() function...
2410 return (crypt(pw
, salt
));
2413 #endif /* !HAVE_LIBPAM && !HAVE_USERSEC_H */
2417 * 'free_authmask()' - Free function for auth masks.
2421 free_authmask(cupsd_authmask_t
*mask
, /* I - Auth mask to free */
2422 void *data
) /* I - User data (unused) */
2426 if (mask
->type
== CUPSD_AUTH_NAME
|| mask
->type
== CUPSD_AUTH_INTERFACE
)
2427 _cupsStrFree(mask
->mask
.name
.name
);
2434 * 'get_md5_password()' - Get an MD5 password.
2437 static char * /* O - MD5 password string */
2438 get_md5_password(const char *username
, /* I - Username */
2439 const char *group
, /* I - Group */
2440 char passwd
[33]) /* O - MD5 password string */
2442 cups_file_t
*fp
; /* passwd.md5 file */
2443 char filename
[1024], /* passwd.md5 filename */
2444 line
[256], /* Line from file */
2445 tempuser
[33], /* User from file */
2446 tempgroup
[33]; /* Group from file */
2449 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2450 "get_md5_password(username=\"%s\", group=\"%s\", passwd=%p)",
2451 username
, group
? group
: "(null)", passwd
);
2453 snprintf(filename
, sizeof(filename
), "%s/passwd.md5", ServerRoot
);
2454 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
2456 if (errno
!= ENOENT
)
2457 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open %s - %s", filename
,
2463 while (cupsFileGets(fp
, line
, sizeof(line
)) != NULL
)
2465 if (sscanf(line
, "%32[^:]:%32[^:]:%32s", tempuser
, tempgroup
, passwd
) != 3)
2467 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad MD5 password line: %s", line
);
2471 if (!strcmp(username
, tempuser
) &&
2472 (group
== NULL
|| !strcmp(group
, tempgroup
)))
2475 * Found the password entry!
2478 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Found MD5 user %s, group %s...",
2479 username
, tempgroup
);
2487 * Didn't find a password entry - return NULL!
2497 * 'pam_func()' - PAM conversation function.
2500 static int /* O - Success or failure */
2502 int num_msg
, /* I - Number of messages */
2503 const struct pam_message
**msg
, /* I - Messages */
2504 struct pam_response
**resp
, /* O - Responses */
2506 /* I - Pointer to connection */
2508 int i
; /* Looping var */
2509 struct pam_response
*replies
; /* Replies */
2510 cupsd_authdata_t
*data
; /* Pointer to auth data */
2514 * Allocate memory for the responses...
2517 if ((replies
= malloc(sizeof(struct pam_response
) * num_msg
)) == NULL
)
2518 return (PAM_CONV_ERR
);
2521 * Answer all of the messages...
2524 DEBUG_printf(("pam_func: appdata_ptr = %p\n", appdata_ptr
));
2528 * Apparently some versions of HP-UX 11 have a broken pam_unix security
2529 * module. This is a workaround...
2535 data
= (cupsd_authdata_t
*)appdata_ptr
;
2538 for (i
= 0; i
< num_msg
; i
++)
2540 DEBUG_printf(("pam_func: Message = \"%s\"\n", msg
[i
]->msg
));
2542 switch (msg
[i
]->msg_style
)
2544 case PAM_PROMPT_ECHO_ON
:
2545 DEBUG_printf(("pam_func: PAM_PROMPT_ECHO_ON, returning \"%s\"...\n",
2547 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2548 replies
[i
].resp
= strdup(data
->username
);
2551 case PAM_PROMPT_ECHO_OFF
:
2552 DEBUG_printf(("pam_func: PAM_PROMPT_ECHO_OFF, returning \"%s\"...\n",
2554 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2555 replies
[i
].resp
= strdup(data
->password
);
2559 DEBUG_puts("pam_func: PAM_TEXT_INFO...");
2560 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2561 replies
[i
].resp
= NULL
;
2565 DEBUG_puts("pam_func: PAM_ERROR_MSG...");
2566 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2567 replies
[i
].resp
= NULL
;
2571 DEBUG_printf(("pam_func: Unknown PAM message %d...\n",
2572 msg
[i
]->msg_style
));
2574 return (PAM_CONV_ERR
);
2579 * Return the responses back to PAM...
2584 return (PAM_SUCCESS
);
2586 #elif !defined(HAVE_USERSEC_H)
2590 * 'to64()' - Base64-encode an integer value...
2594 to64(char *s
, /* O - Output string */
2595 unsigned long v
, /* I - Value to encode */
2596 int n
) /* I - Number of digits */
2598 const char *itoa64
= "./0123456789"
2599 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2600 "abcdefghijklmnopqrstuvwxyz";
2603 for (; n
> 0; n
--, v
>>= 6)
2604 *s
++ = itoa64
[v
& 0x3f];
2606 #endif /* HAVE_LIBPAM */