2 * "$Id: auth.c 7830 2008-08-04 20:38:50Z mike $"
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
92 typedef struct ucred cupsd_ucred_t
;
93 # define CUPSD_UCRED_UID(c) (c).uid
94 #endif /* HAVE_SYS_UCRED_H */
95 #ifdef HAVE_KRB5_IPC_CLIENT_SET_TARGET_UID
96 /* Not in public headers... */
97 extern void krb5_ipc_client_set_target_uid(uid_t
);
98 extern void krb5_ipc_client_clear_target(void);
99 #endif /* HAVE_KRB5_IPC_CLIENT_SET_TARGET_UID */
106 #ifdef HAVE_AUTHORIZATION_H
107 static int check_authref(cupsd_client_t
*con
, const char *right
);
108 #endif /* HAVE_AUTHORIZATION_H */
109 static int compare_locations(cupsd_location_t
*a
,
110 cupsd_location_t
*b
);
111 static cupsd_authmask_t
*copy_authmask(cupsd_authmask_t
*am
, void *data
);
112 #if !HAVE_LIBPAM && !defined(HAVE_USERSEC_H)
113 static char *cups_crypt(const char *pw
, const char *salt
);
114 #endif /* !HAVE_LIBPAM && !HAVE_USERSEC_H */
115 static void free_authmask(cupsd_authmask_t
*am
, void *data
);
116 static char *get_md5_password(const char *username
,
117 const char *group
, char passwd
[33]);
119 static int pam_func(int, const struct pam_message
**,
120 struct pam_response
**, void *);
121 #elif !defined(HAVE_USERSEC_H)
122 static void to64(char *s
, unsigned long v
, int n
);
123 #endif /* HAVE_LIBPAM */
127 * Local structures...
131 typedef struct cupsd_authdata_s
/**** Authentication data ****/
133 char username
[HTTP_MAX_VALUE
], /* Username string */
134 password
[HTTP_MAX_VALUE
]; /* Password string */
136 #endif /* HAVE_LIBPAM */
143 #if defined(__hpux) && HAVE_LIBPAM
144 static cupsd_authdata_t
*auth_data
; /* Current client being authenticated */
145 #endif /* __hpux && HAVE_LIBPAM */
149 * 'cupsdAddIPMask()' - Add an IP address authorization mask.
152 int /* O - 1 on success, 0 on failure */
154 cups_array_t
**masks
, /* IO - Masks array (created as needed) */
155 const unsigned address
[4], /* I - IP address */
156 const unsigned netmask
[4]) /* I - IP netmask */
158 cupsd_authmask_t temp
; /* New host/domain mask */
161 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
162 "cupsdAddIPMask(masks=%p(%p), address=%x:%x:%x:%x, "
163 "netmask=%x:%x:%x:%x)",
165 address
[0], address
[1], address
[2], address
[3],
166 netmask
[0], netmask
[1], netmask
[2], netmask
[3]);
168 temp
.type
= CUPSD_AUTH_IP
;
169 memcpy(temp
.mask
.ip
.address
, address
, sizeof(temp
.mask
.ip
.address
));
170 memcpy(temp
.mask
.ip
.netmask
, netmask
, sizeof(temp
.mask
.ip
.netmask
));
173 * Create the masks array as needed and add...
177 *masks
= cupsArrayNew3(NULL
, NULL
, NULL
, 0,
178 (cups_acopy_func_t
)copy_authmask
,
179 (cups_afree_func_t
)free_authmask
);
181 return (cupsArrayAdd(*masks
, &temp
));
186 * 'cupsdAddLocation()' - Add a location for authorization.
190 cupsdAddLocation(cupsd_location_t
*loc
) /* I - Location to add */
193 * Make sure the locations array is created...
197 Locations
= cupsArrayNew3((cups_array_func_t
)compare_locations
, NULL
,
198 (cups_ahash_func_t
)NULL
, 0,
199 (cups_acopy_func_t
)NULL
,
200 (cups_afree_func_t
)cupsdFreeLocation
);
204 cupsArrayAdd(Locations
, loc
);
206 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAddLocation: Added location \"%s\"",
207 loc
->location
? loc
->location
: "(null)");
213 * 'cupsdAddName()' - Add a name to a location...
217 cupsdAddName(cupsd_location_t
*loc
, /* I - Location to add to */
218 char *name
) /* I - Name to add */
220 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAddName(loc=%p, name=\"%s\")",
224 loc
->names
= cupsArrayNew3(NULL
, NULL
, NULL
, 0,
225 (cups_acopy_func_t
)_cupsStrAlloc
,
226 (cups_afree_func_t
)_cupsStrFree
);
228 if (!cupsArrayAdd(loc
->names
, name
))
230 cupsdLogMessage(CUPSD_LOG_ERROR
,
231 "Unable to duplicate name for location %s: %s",
232 loc
->location
? loc
->location
: "nil", strerror(errno
));
239 * 'cupsdAddNameMask()' - Add a host or interface name authorization mask.
242 int /* O - 1 on success, 0 on failure */
243 cupsdAddNameMask(cups_array_t
**masks
, /* IO - Masks array (created as needed) */
244 char *name
) /* I - Host or interface name */
246 cupsd_authmask_t temp
; /* New host/domain mask */
247 char ifname
[32], /* Interface name */
248 *ifptr
; /* Pointer to end of name */
251 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
252 "cupsdAddNameMask(masks=%p(%p), name=\"%s\")",
253 masks
, *masks
, name
);
255 if (!_cups_strcasecmp(name
, "@LOCAL"))
258 * Deny *interface*...
261 temp
.type
= CUPSD_AUTH_INTERFACE
;
262 temp
.mask
.name
.name
= (char *)"*";
264 else if (!_cups_strncasecmp(name
, "@IF(", 4))
267 * Deny *interface*...
270 strlcpy(ifname
, name
+ 4, sizeof(ifname
));
272 ifptr
= ifname
+ strlen(ifname
) - 1;
274 if (ifptr
>= ifname
&& *ifptr
== ')')
280 temp
.type
= CUPSD_AUTH_INTERFACE
;
281 temp
.mask
.name
.name
= ifname
;
292 temp
.type
= CUPSD_AUTH_NAME
;
293 temp
.mask
.name
.name
= (char *)name
;
297 * Set the name length...
300 temp
.mask
.name
.length
= strlen(temp
.mask
.name
.name
);
303 * Create the masks array as needed and add...
307 *masks
= cupsArrayNew3(NULL
, NULL
, NULL
, 0,
308 (cups_acopy_func_t
)copy_authmask
,
309 (cups_afree_func_t
)free_authmask
);
311 return (cupsArrayAdd(*masks
, &temp
));
316 * 'cupsdAuthorize()' - Validate any authorization credentials.
320 cupsdAuthorize(cupsd_client_t
*con
) /* I - Client connection */
322 int type
; /* Authentication type */
323 const char *authorization
; /* Pointer into Authorization string */
324 char *ptr
, /* Pointer into string */
325 username
[HTTP_MAX_VALUE
],
326 /* Username string */
327 password
[HTTP_MAX_VALUE
];
328 /* Password string */
329 cupsd_cert_t
*localuser
; /* Certificate username */
330 char nonce
[HTTP_MAX_VALUE
], /* Nonce value from client */
331 md5
[33], /* MD5 password */
332 basicmd5
[33]; /* MD5 of Basic password */
333 static const char * const states
[] = /* HTTP client states... */
353 * Locate the best matching location so we know what kind of
354 * authentication to expect...
357 con
->best
= cupsdFindBest(con
->uri
, con
->http
.state
);
358 con
->type
= CUPSD_AUTH_NONE
;
360 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
361 "[Client %d] con->uri=\"%s\", con->best=%p(%s)",
362 con
->http
.fd
, con
->uri
, con
->best
,
363 con
->best
? con
->best
->location
: "");
365 if (con
->best
&& con
->best
->type
!= CUPSD_AUTH_NONE
)
367 if (con
->best
->type
== CUPSD_AUTH_DEFAULT
)
368 type
= cupsdDefaultAuthType();
370 type
= con
->best
->type
;
373 type
= cupsdDefaultAuthType();
376 * Decode the Authorization string...
379 authorization
= httpGetField(&con
->http
, HTTP_FIELD_AUTHORIZATION
);
381 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Client %d] Authorization=\"%s\"",
382 con
->http
.fd
, authorization
);
389 #endif /* HAVE_GSSAPI */
391 #ifdef HAVE_AUTHORIZATION_H
394 AuthorizationFree(con
->authref
, kAuthorizationFlagDefaults
);
397 #endif /* HAVE_AUTHORIZATION_H */
402 * No authorization data provided, return early...
405 cupsdLogMessage(CUPSD_LOG_DEBUG
,
406 "[Client %d] No authentication data provided.",
410 #ifdef HAVE_AUTHORIZATION_H
411 else if (!strncmp(authorization
, "AuthRef ", 8) &&
412 !_cups_strcasecmp(con
->http
.hostname
, "localhost"))
414 OSStatus status
; /* Status */
415 int authlen
; /* Auth string length */
416 AuthorizationItemSet
*authinfo
; /* Authorization item set */
419 * Get the Authorization Services data...
423 while (isspace(*authorization
& 255))
426 authlen
= sizeof(nonce
);
427 httpDecode64_2(nonce
, &authlen
, authorization
);
429 if (authlen
!= kAuthorizationExternalFormLength
)
431 cupsdLogMessage(CUPSD_LOG_ERROR
,
432 "[Client %d] External Authorization reference size is "
433 "incorrect.", con
->http
.fd
);
437 if ((status
= AuthorizationCreateFromExternalForm(
438 (AuthorizationExternalForm
*)nonce
, &con
->authref
)) != 0)
440 cupsdLogMessage(CUPSD_LOG_ERROR
,
441 "[Client %d] AuthorizationCreateFromExternalForm "
442 "returned %d (%s)", con
->http
.fd
, (int)status
,
443 cssmErrorString(status
));
449 if (!AuthorizationCopyInfo(con
->authref
, kAuthorizationEnvironmentUsername
,
452 if (authinfo
->count
== 1 && authinfo
->items
[0].value
&&
453 authinfo
->items
[0].valueLength
>= 2)
455 strlcpy(username
, authinfo
->items
[0].value
, sizeof(username
));
457 cupsdLogMessage(CUPSD_LOG_DEBUG
,
458 "[Client %d] Authorized as \"%s\" using AuthRef",
459 con
->http
.fd
, username
);
462 AuthorizationFreeItemSet(authinfo
);
468 * No username in AuthRef, grab username using peer credentials...
471 struct passwd
*pwd
; /* Password entry for this user */
472 cupsd_ucred_t peercred
; /* Peer credentials */
473 socklen_t peersize
; /* Size of peer credentials */
475 peersize
= sizeof(peercred
);
477 if (getsockopt(con
->http
.fd
, 0, LOCAL_PEERCRED
, &peercred
, &peersize
))
479 cupsdLogMessage(CUPSD_LOG_ERROR
,
480 "[Client %d] Unable to get peer credentials - %s",
481 con
->http
.fd
, strerror(errno
));
485 if ((pwd
= getpwuid(CUPSD_UCRED_UID(peercred
))) == NULL
)
487 cupsdLogMessage(CUPSD_LOG_ERROR
,
488 "[Client %d] Unable to find UID %d for peer "
489 "credentials.", con
->http
.fd
,
490 (int)CUPSD_UCRED_UID(peercred
));
494 strlcpy(username
, pwd
->pw_name
, sizeof(username
));
496 cupsdLogMessage(CUPSD_LOG_DEBUG
,
497 "[Client %d] Authorized as \"%s\" using "
498 "AuthRef + PeerCred", con
->http
.fd
, username
);
501 con
->type
= CUPSD_AUTH_BASIC
;
503 #endif /* HAVE_AUTHORIZATION_H */
504 #if defined(SO_PEERCRED) && defined(AF_LOCAL)
505 else if (!strncmp(authorization
, "PeerCred ", 9) &&
506 con
->http
.hostaddr
->addr
.sa_family
== AF_LOCAL
)
509 * Use peer credentials from domain socket connection...
512 struct passwd
*pwd
; /* Password entry for this user */
513 cupsd_ucred_t peercred
; /* Peer credentials */
514 socklen_t peersize
; /* Size of peer credentials */
515 #ifdef HAVE_AUTHORIZATION_H
516 const char *name
; /* Authorizing name */
518 for (name
= (char *)cupsArrayFirst(con
->best
->names
);
520 name
= (char *)cupsArrayNext(con
->best
->names
))
521 if (!_cups_strncasecmp(name
, "@AUTHKEY(", 9) ||
522 !_cups_strcasecmp(name
, "@SYSTEM"))
524 cupsdLogMessage(CUPSD_LOG_ERROR
,
525 "[Client %d] PeerCred authentication not allowed for "
526 "resource.", con
->http
.fd
);
529 #endif /* HAVE_AUTHORIZATION_H */
531 if ((pwd
= getpwnam(authorization
+ 9)) == NULL
)
533 cupsdLogMessage(CUPSD_LOG_ERROR
,
534 "[Client %d] User \"%s\" does not exist.", con
->http
.fd
,
539 peersize
= sizeof(peercred
);
542 if (getsockopt(con
->http
.fd
, 0, LOCAL_PEERCRED
, &peercred
, &peersize
))
544 if (getsockopt(con
->http
.fd
, SOL_SOCKET
, SO_PEERCRED
, &peercred
, &peersize
))
545 # endif /* __APPLE__ */
547 cupsdLogMessage(CUPSD_LOG_ERROR
,
548 "[Client %d] Unable to get peer credentials - %s",
549 con
->http
.fd
, strerror(errno
));
553 if (pwd
->pw_uid
!= CUPSD_UCRED_UID(peercred
))
555 cupsdLogMessage(CUPSD_LOG_ERROR
,
556 "[Client %d] Invalid peer credentials for \"%s\" - got "
557 "%d, expected %d!", con
->http
.fd
, authorization
+ 9,
558 CUPSD_UCRED_UID(peercred
), pwd
->pw_uid
);
559 # ifdef HAVE_SYS_UCRED_H
560 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Client %d] cr_version=%d",
561 con
->http
.fd
, peercred
.cr_version
);
562 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Client %d] cr_uid=%d",
563 con
->http
.fd
, peercred
.cr_uid
);
564 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Client %d] cr_ngroups=%d",
565 con
->http
.fd
, peercred
.cr_ngroups
);
566 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Client %d] cr_groups[0]=%d",
567 con
->http
.fd
, peercred
.cr_groups
[0]);
568 # endif /* HAVE_SYS_UCRED_H */
572 strlcpy(username
, authorization
+ 9, sizeof(username
));
575 con
->gss_uid
= CUPSD_UCRED_UID(peercred
);
576 # endif /* HAVE_GSSAPI */
578 cupsdLogMessage(CUPSD_LOG_DEBUG
,
579 "[Client %d] Authorized as %s using PeerCred", con
->http
.fd
,
582 con
->type
= CUPSD_AUTH_BASIC
;
584 #endif /* SO_PEERCRED && AF_LOCAL */
585 else if (!strncmp(authorization
, "Local", 5) &&
586 !_cups_strcasecmp(con
->http
.hostname
, "localhost"))
589 * Get Local certificate authentication data...
593 while (isspace(*authorization
& 255))
596 if ((localuser
= cupsdFindCert(authorization
)) != NULL
)
598 strlcpy(username
, localuser
->username
, sizeof(username
));
600 cupsdLogMessage(CUPSD_LOG_DEBUG
,
601 "[Client %d] Authorized as %s using Local", con
->http
.fd
,
606 cupsdLogMessage(CUPSD_LOG_ERROR
,
607 "[Client %d] Local authentication certificate not found.",
613 if (localuser
->ccache
)
614 con
->type
= CUPSD_AUTH_NEGOTIATE
;
616 #endif /* HAVE_GSSAPI */
617 con
->type
= CUPSD_AUTH_BASIC
;
619 else if (!strncmp(authorization
, "Basic", 5))
622 * Get the Basic authentication data...
625 int userlen
; /* Username:password length */
629 while (isspace(*authorization
& 255))
632 userlen
= sizeof(username
);
633 httpDecode64_2(username
, &userlen
, authorization
);
636 * Pull the username and password out...
639 if ((ptr
= strchr(username
, ':')) == NULL
)
641 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Client %d] Missing Basic password.",
651 * Username must not be empty...
654 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Client %d] Empty Basic username.",
662 * Password must not be empty...
665 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Client %d] Empty Basic password.",
670 strlcpy(password
, ptr
, sizeof(password
));
673 * Validate the username and password...
679 case CUPSD_AUTH_BASIC
:
683 * Only use PAM to do authentication. This supports MD5
684 * passwords, among other things...
687 pam_handle_t
*pamh
; /* PAM authentication handle */
688 int pamerr
; /* PAM error code */
689 struct pam_conv pamdata
;/* PAM conversation data */
690 cupsd_authdata_t data
; /* Authentication data */
693 strlcpy(data
.username
, username
, sizeof(data
.username
));
694 strlcpy(data
.password
, password
, sizeof(data
.password
));
696 # if defined(__sun) || defined(__hpux)
697 pamdata
.conv
= (int (*)(int, struct pam_message
**,
698 struct pam_response
**,
701 pamdata
.conv
= pam_func
;
702 # endif /* __sun || __hpux */
703 pamdata
.appdata_ptr
= &data
;
707 * Workaround for HP-UX bug in pam_unix; see pam_func() below for
714 pamerr
= pam_start("cups", username
, &pamdata
, &pamh
);
715 if (pamerr
!= PAM_SUCCESS
)
717 cupsdLogMessage(CUPSD_LOG_ERROR
,
718 "[Client %d] pam_start() returned %d (%s)",
719 con
->http
.fd
, pamerr
, pam_strerror(pamh
, pamerr
));
723 # ifdef HAVE_PAM_SET_ITEM
725 pamerr
= pam_set_item(pamh
, PAM_RHOST
, con
->http
.hostname
);
726 if (pamerr
!= PAM_SUCCESS
)
727 cupsdLogMessage(CUPSD_LOG_WARN
,
728 "[Client %d] pam_set_item(PAM_RHOST) "
729 "returned %d (%s)", con
->http
.fd
, pamerr
,
730 pam_strerror(pamh
, pamerr
));
731 # endif /* PAM_RHOST */
734 pamerr
= pam_set_item(pamh
, PAM_TTY
, "cups");
735 if (pamerr
!= PAM_SUCCESS
)
736 cupsdLogMessage(CUPSD_LOG_WARN
,
737 "[Client %d] pam_set_item(PAM_TTY) "
738 "returned %d (%s)!", con
->http
.fd
, pamerr
,
739 pam_strerror(pamh
, pamerr
));
740 # endif /* PAM_TTY */
741 # endif /* HAVE_PAM_SET_ITEM */
743 pamerr
= pam_authenticate(pamh
, PAM_SILENT
);
744 if (pamerr
!= PAM_SUCCESS
)
746 cupsdLogMessage(CUPSD_LOG_ERROR
,
747 "[Client %d] pam_authenticate() returned %d (%s)",
748 con
->http
.fd
, pamerr
, pam_strerror(pamh
, pamerr
));
753 # ifdef HAVE_PAM_SETCRED
754 pamerr
= pam_setcred(pamh
, PAM_ESTABLISH_CRED
| PAM_SILENT
);
755 if (pamerr
!= PAM_SUCCESS
)
756 cupsdLogMessage(CUPSD_LOG_WARN
,
757 "[Client %d] pam_setcred() returned %d (%s)",
758 con
->http
.fd
, pamerr
,
759 pam_strerror(pamh
, pamerr
));
760 # endif /* HAVE_PAM_SETCRED */
762 pamerr
= pam_acct_mgmt(pamh
, PAM_SILENT
);
763 if (pamerr
!= PAM_SUCCESS
)
765 cupsdLogMessage(CUPSD_LOG_ERROR
,
766 "[Client %d] pam_acct_mgmt() returned %d (%s)",
767 con
->http
.fd
, pamerr
, pam_strerror(pamh
, pamerr
));
772 pam_end(pamh
, PAM_SUCCESS
);
774 #elif defined(HAVE_USERSEC_H)
776 * Use AIX authentication interface...
779 char *authmsg
; /* Authentication message */
780 int reenter
; /* ??? */
783 cupsdLogMessage(CUPSD_LOG_DEBUG
,
784 "[Client %d] AIX authenticate of username \"%s\"",
785 con
->http
.fd
, username
);
788 if (authenticate(username
, password
, &reenter
, &authmsg
) != 0)
790 cupsdLogMessage(CUPSD_LOG_DEBUG
,
791 "[Client %d] Unable to authenticate username "
792 "\"%s\": %s", con
->http
.fd
, username
,
799 * Use normal UNIX password file-based authentication...
802 char *pass
; /* Encrypted password */
803 struct passwd
*pw
; /* User password data */
804 # ifdef HAVE_SHADOW_H
805 struct spwd
*spw
; /* Shadow password data */
806 # endif /* HAVE_SHADOW_H */
809 pw
= getpwnam(username
); /* Get the current password */
810 endpwent(); /* Close the password file */
818 cupsdLogMessage(CUPSD_LOG_ERROR
,
819 "[Client %d] Unknown username \"%s\".",
820 con
->http
.fd
, username
);
824 # ifdef HAVE_SHADOW_H
825 spw
= getspnam(username
);
828 if (!spw
&& !strcmp(pw
->pw_passwd
, "x"))
831 * Don't allow blank passwords!
834 cupsdLogMessage(CUPSD_LOG_ERROR
,
835 "[Client %d] Username \"%s\" has no shadow "
836 "password.", con
->http
.fd
, username
);
840 if (spw
&& !spw
->sp_pwdp
[0] && !pw
->pw_passwd
[0])
842 if (!pw
->pw_passwd
[0])
843 # endif /* HAVE_SHADOW_H */
846 * Don't allow blank passwords!
849 cupsdLogMessage(CUPSD_LOG_ERROR
,
850 "[Client %d] Username \"%s\" has no password.",
851 con
->http
.fd
, username
);
856 * OK, the password isn't blank, so compare with what came from the
860 pass
= cups_crypt(password
, pw
->pw_passwd
);
862 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
863 "[Client %d] pw_passwd=\"%s\", crypt=\"%s\"",
864 con
->http
.fd
, pw
->pw_passwd
, pass
);
866 if (!pass
|| strcmp(pw
->pw_passwd
, pass
))
868 # ifdef HAVE_SHADOW_H
871 pass
= cups_crypt(password
, spw
->sp_pwdp
);
873 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
874 "[Client %d] sp_pwdp=\"%s\", crypt=\"%s\"",
875 con
->http
.fd
, spw
->sp_pwdp
, pass
);
877 if (pass
== NULL
|| strcmp(spw
->sp_pwdp
, pass
))
879 cupsdLogMessage(CUPSD_LOG_ERROR
,
880 "[Client %d] Authentication failed for user "
881 "\"%s\".", con
->http
.fd
, username
);
886 # endif /* HAVE_SHADOW_H */
888 cupsdLogMessage(CUPSD_LOG_ERROR
,
889 "[Client %d] Authentication failed for user "
890 "\"%s\".", con
->http
.fd
, username
);
894 #endif /* HAVE_LIBPAM */
897 cupsdLogMessage(CUPSD_LOG_DEBUG
,
898 "[Client %d] Authorized as %s using Basic",
899 con
->http
.fd
, username
);
902 case CUPSD_AUTH_BASICDIGEST
:
904 * Do Basic authentication with the Digest password file...
907 if (!get_md5_password(username
, NULL
, md5
))
909 cupsdLogMessage(CUPSD_LOG_ERROR
,
910 "[Client %d] Unknown MD5 username \"%s\".",
911 con
->http
.fd
, username
);
915 httpMD5(username
, "CUPS", password
, basicmd5
);
917 if (strcmp(md5
, basicmd5
))
919 cupsdLogMessage(CUPSD_LOG_ERROR
,
920 "[Client %d] Authentication failed for \"%s\".",
921 con
->http
.fd
, username
);
925 cupsdLogMessage(CUPSD_LOG_DEBUG
,
926 "[Client %d] Authorized as %s using BasicDigest",
927 con
->http
.fd
, username
);
933 else if (!strncmp(authorization
, "Digest", 6))
936 * Get the username, password, and nonce from the Digest attributes...
939 if (!httpGetSubField2(&(con
->http
), HTTP_FIELD_AUTHORIZATION
, "username",
940 username
, sizeof(username
)) || !username
[0])
943 * Username must not be empty...
946 cupsdLogMessage(CUPSD_LOG_ERROR
,
947 "[Client %d] Empty or missing Digest username.",
952 if (!httpGetSubField2(&(con
->http
), HTTP_FIELD_AUTHORIZATION
, "response",
953 password
, sizeof(password
)) || !password
[0])
956 * Password must not be empty...
959 cupsdLogMessage(CUPSD_LOG_ERROR
,
960 "[Client %d] Empty or missing Digest password.",
965 if (!httpGetSubField(&(con
->http
), HTTP_FIELD_AUTHORIZATION
, "nonce",
968 cupsdLogMessage(CUPSD_LOG_ERROR
,
969 "[Client %d] No nonce value for Digest authentication.",
974 if (strcmp(con
->http
.hostname
, nonce
))
976 cupsdLogMessage(CUPSD_LOG_ERROR
,
977 "[Client %d] Bad nonce value, expected \"%s\", "
978 "got \"%s\".", con
->http
.fd
, con
->http
.hostname
, nonce
);
983 * Validate the username and password...
986 if (!get_md5_password(username
, NULL
, md5
))
988 cupsdLogMessage(CUPSD_LOG_ERROR
,
989 "[Client %d] Unknown MD5 username \"%s\".",
990 con
->http
.fd
, username
);
994 httpMD5Final(nonce
, states
[con
->http
.state
], con
->uri
, md5
);
996 if (strcmp(md5
, password
))
998 cupsdLogMessage(CUPSD_LOG_ERROR
,
999 "[Client %d] Authentication failed for \"%s\".",
1000 con
->http
.fd
, username
);
1004 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1005 "[Client %d] Authorized as %s using Digest", con
->http
.fd
,
1008 con
->type
= CUPSD_AUTH_DIGEST
;
1011 else if (!strncmp(authorization
, "Negotiate", 9))
1013 int len
; /* Length of authorization string */
1014 gss_ctx_id_t context
; /* Authorization context */
1015 OM_uint32 major_status
, /* Major status code */
1016 minor_status
; /* Minor status code */
1017 gss_buffer_desc input_token
= GSS_C_EMPTY_BUFFER
,
1018 /* Input token from string */
1019 output_token
= GSS_C_EMPTY_BUFFER
;
1020 /* Output token for username */
1021 gss_name_t client_name
; /* Client name */
1026 * If the weak-linked GSSAPI/Kerberos library is not present, don't try
1030 if (gss_init_sec_context
== NULL
)
1032 cupsdLogMessage(CUPSD_LOG_WARN
,
1033 "[Client %d] GSSAPI/Kerberos authentication failed "
1034 "because the Kerberos framework is not present.",
1038 # endif /* __APPLE__ */
1041 * Find the start of the Kerberos input token...
1045 while (isspace(*authorization
& 255))
1048 if (!*authorization
)
1050 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1051 "[Client %d] No authentication data specified.",
1057 * Decode the authorization string to get the input token...
1060 len
= strlen(authorization
);
1061 input_token
.value
= malloc(len
);
1062 input_token
.value
= httpDecode64_2(input_token
.value
, &len
,
1064 input_token
.length
= len
;
1067 * Accept the input token to get the authorization info...
1070 context
= GSS_C_NO_CONTEXT
;
1071 client_name
= GSS_C_NO_NAME
;
1072 major_status
= gss_accept_sec_context(&minor_status
,
1076 GSS_C_NO_CHANNEL_BINDINGS
,
1084 if (output_token
.length
> 0)
1085 gss_release_buffer(&minor_status
, &output_token
);
1087 if (GSS_ERROR(major_status
))
1089 cupsdLogGSSMessage(CUPSD_LOG_DEBUG
, major_status
, minor_status
,
1090 "[Client %d] Error accepting GSSAPI security context",
1093 if (context
!= GSS_C_NO_CONTEXT
)
1094 gss_delete_sec_context(&minor_status
, &context
, GSS_C_NO_BUFFER
);
1101 * Get the username associated with the client's credentials...
1104 if (major_status
== GSS_S_CONTINUE_NEEDED
)
1105 cupsdLogGSSMessage(CUPSD_LOG_DEBUG
, major_status
, minor_status
,
1106 "[Client %d] Credentials not complete", con
->http
.fd
);
1107 else if (major_status
== GSS_S_COMPLETE
)
1109 major_status
= gss_display_name(&minor_status
, client_name
,
1110 &output_token
, NULL
);
1112 if (GSS_ERROR(major_status
))
1114 cupsdLogGSSMessage(CUPSD_LOG_DEBUG
, major_status
, minor_status
,
1115 "[Client %d] Error getting username", con
->http
.fd
);
1116 gss_release_name(&minor_status
, &client_name
);
1117 gss_delete_sec_context(&minor_status
, &context
, GSS_C_NO_BUFFER
);
1121 strlcpy(username
, output_token
.value
, sizeof(username
));
1123 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1124 "[Client %d] Authorized as %s using Negotiate",
1125 con
->http
.fd
, username
);
1127 gss_release_name(&minor_status
, &client_name
);
1128 gss_release_buffer(&minor_status
, &output_token
);
1130 con
->type
= CUPSD_AUTH_NEGOTIATE
;
1133 gss_delete_sec_context(&minor_status
, &context
, GSS_C_NO_BUFFER
);
1135 # if defined(SO_PEERCRED) && defined(AF_LOCAL)
1137 * Get the client's UID if we are printing locally - that allows a backend
1138 * to run as the correct user to get Kerberos credentials of its own.
1141 if (_httpAddrFamily(con
->http
.hostaddr
) == AF_LOCAL
)
1143 cupsd_ucred_t peercred
; /* Peer credentials */
1144 socklen_t peersize
; /* Size of peer credentials */
1146 peersize
= sizeof(peercred
);
1149 if (getsockopt(con
->http
.fd
, 0, LOCAL_PEERCRED
, &peercred
, &peersize
))
1151 if (getsockopt(con
->http
.fd
, SOL_SOCKET
, SO_PEERCRED
, &peercred
,
1153 # endif /* __APPLE__ */
1155 cupsdLogMessage(CUPSD_LOG_ERROR
,
1156 "[Client %d] Unable to get peer credentials - %s",
1157 con
->http
.fd
, strerror(errno
));
1161 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1162 "[Client %d] Using credentials for UID %d.",
1163 con
->http
.fd
, CUPSD_UCRED_UID(peercred
));
1164 con
->gss_uid
= CUPSD_UCRED_UID(peercred
);
1167 # endif /* SO_PEERCRED && AF_LOCAL */
1169 #endif /* HAVE_GSSAPI */
1172 char scheme
[256]; /* Auth scheme... */
1175 if (sscanf(authorization
, "%255s", scheme
) != 1)
1176 strcpy(scheme
, "UNKNOWN");
1178 cupsdLogMessage(CUPSD_LOG_ERROR
,
1179 "[Client %d] Bad authentication data \"%s ...\"",
1180 con
->http
.fd
, scheme
);
1185 * If we get here, then we were able to validate the username and
1186 * password - copy the validated username and password to the client
1187 * data and return...
1190 strlcpy(con
->username
, username
, sizeof(con
->username
));
1191 strlcpy(con
->password
, password
, sizeof(con
->password
));
1196 * 'cupsdCheckAccess()' - Check whether the given address is allowed to
1197 * access a location.
1200 int /* O - 1 if allowed, 0 otherwise */
1202 unsigned ip
[4], /* I - Client address */
1203 char *name
, /* I - Client hostname */
1204 int namelen
, /* I - Length of hostname */
1205 cupsd_location_t
*loc
) /* I - Location to check */
1207 int allow
; /* 1 if allowed, 0 otherwise */
1210 if (!_cups_strcasecmp(name
, "localhost"))
1213 * Access from localhost (127.0.0.1 or ::1) is always allowed...
1221 * Do authorization checks on the domain/address...
1224 switch (loc
->order_type
)
1227 allow
= 0; /* anti-compiler-warning-code */
1230 case CUPSD_AUTH_ALLOW
: /* Order Deny,Allow */
1233 if (cupsdCheckAuth(ip
, name
, namelen
, loc
->deny
))
1236 if (cupsdCheckAuth(ip
, name
, namelen
, loc
->allow
))
1240 case CUPSD_AUTH_DENY
: /* Order Allow,Deny */
1243 if (cupsdCheckAuth(ip
, name
, namelen
, loc
->allow
))
1246 if (cupsdCheckAuth(ip
, name
, namelen
, loc
->deny
))
1257 * 'cupsdCheckAuth()' - Check authorization masks.
1260 int /* O - 1 if mask matches, 0 otherwise */
1261 cupsdCheckAuth(unsigned ip
[4], /* I - Client address */
1262 char *name
, /* I - Client hostname */
1263 int name_len
, /* I - Length of hostname */
1264 cups_array_t
*masks
) /* I - Masks */
1266 int i
; /* Looping var */
1267 cupsd_authmask_t
*mask
; /* Current mask */
1268 cupsd_netif_t
*iface
; /* Network interface */
1269 unsigned netip4
; /* IPv4 network address */
1271 unsigned netip6
[4]; /* IPv6 network address */
1272 #endif /* AF_INET6 */
1275 for (mask
= (cupsd_authmask_t
*)cupsArrayFirst(masks
);
1277 mask
= (cupsd_authmask_t
*)cupsArrayNext(masks
))
1281 case CUPSD_AUTH_INTERFACE
:
1283 * Check for a match with a network interface...
1286 netip4
= htonl(ip
[3]);
1289 netip6
[0] = htonl(ip
[0]);
1290 netip6
[1] = htonl(ip
[1]);
1291 netip6
[2] = htonl(ip
[2]);
1292 netip6
[3] = htonl(ip
[3]);
1293 #endif /* AF_INET6 */
1295 if (!strcmp(mask
->mask
.name
.name
, "*"))
1299 * Allow Back-to-My-Mac addresses...
1302 if ((ip
[0] & 0xff000000) == 0xfd000000)
1304 #endif /* __APPLE__ */
1307 * Check against all local interfaces...
1312 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
1314 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
1317 * Only check local interfaces...
1320 if (!iface
->is_local
)
1323 if (iface
->address
.addr
.sa_family
== AF_INET
)
1326 * Check IPv4 address...
1329 if ((netip4
& iface
->mask
.ipv4
.sin_addr
.s_addr
) ==
1330 (iface
->address
.ipv4
.sin_addr
.s_addr
&
1331 iface
->mask
.ipv4
.sin_addr
.s_addr
))
1338 * Check IPv6 address...
1341 for (i
= 0; i
< 4; i
++)
1342 if ((netip6
[i
] & iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]) !=
1343 (iface
->address
.ipv6
.sin6_addr
.s6_addr32
[i
] &
1344 iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]))
1350 #endif /* AF_INET6 */
1356 * Check the named interface...
1359 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
1361 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
1363 if (strcmp(mask
->mask
.name
.name
, iface
->name
))
1366 if (iface
->address
.addr
.sa_family
== AF_INET
)
1369 * Check IPv4 address...
1372 if ((netip4
& iface
->mask
.ipv4
.sin_addr
.s_addr
) ==
1373 (iface
->address
.ipv4
.sin_addr
.s_addr
&
1374 iface
->mask
.ipv4
.sin_addr
.s_addr
))
1381 * Check IPv6 address...
1384 for (i
= 0; i
< 4; i
++)
1385 if ((netip6
[i
] & iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]) !=
1386 (iface
->address
.ipv6
.sin6_addr
.s6_addr32
[i
] &
1387 iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]))
1393 #endif /* AF_INET6 */
1398 case CUPSD_AUTH_NAME
:
1400 * Check for exact name match...
1403 if (!_cups_strcasecmp(name
, mask
->mask
.name
.name
))
1407 * Check for domain match...
1410 if (name_len
>= mask
->mask
.name
.length
&&
1411 mask
->mask
.name
.name
[0] == '.' &&
1412 !_cups_strcasecmp(name
+ name_len
- mask
->mask
.name
.length
,
1413 mask
->mask
.name
.name
))
1417 case CUPSD_AUTH_IP
:
1419 * Check for IP/network address match...
1422 for (i
= 0; i
< 4; i
++)
1423 if ((ip
[i
] & mask
->mask
.ip
.netmask
[i
]) !=
1424 mask
->mask
.ip
.address
[i
])
1438 * 'cupsdCheckGroup()' - Check for a user's group membership.
1441 int /* O - 1 if user is a member, 0 otherwise */
1443 const char *username
, /* I - User name */
1444 struct passwd
*user
, /* I - System user info */
1445 const char *groupname
) /* I - Group name */
1447 int i
; /* Looping var */
1448 struct group
*group
; /* System group info */
1449 char junk
[33]; /* MD5 password (not used) */
1450 #ifdef HAVE_MBR_UID_TO_UUID
1451 uuid_t useruuid
, /* UUID for username */
1452 groupuuid
; /* UUID for groupname */
1453 int is_member
; /* True if user is a member of group */
1454 #endif /* HAVE_MBR_UID_TO_UUID */
1457 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1458 "cupsdCheckGroup(username=\"%s\", user=%p, groupname=\"%s\")",
1459 username
, user
, groupname
);
1465 if (!username
|| !groupname
)
1469 * Check to see if the user is a member of the named group...
1472 group
= getgrnam(groupname
);
1478 * Group exists, check it...
1481 for (i
= 0; group
->gr_mem
[i
]; i
++)
1482 if (!_cups_strcasecmp(username
, group
->gr_mem
[i
]))
1487 * Group doesn't exist or user not in group list, check the group ID
1488 * against the user's group ID...
1491 if (user
&& group
&& group
->gr_gid
== user
->pw_gid
)
1494 #ifdef HAVE_MBR_UID_TO_UUID
1496 * Check group membership through MacOS X membership API...
1499 if (user
&& !mbr_uid_to_uuid(user
->pw_uid
, useruuid
))
1504 * Map group name to UUID and check membership...
1507 if (!mbr_gid_to_uuid(group
->gr_gid
, groupuuid
))
1508 if (!mbr_check_membership(useruuid
, groupuuid
, &is_member
))
1512 else if (groupname
[0] == '#')
1515 * Use UUID directly and check for equality (user UUID) and
1516 * membership (group UUID)...
1519 if (!uuid_parse((char *)groupname
+ 1, groupuuid
))
1521 if (!uuid_compare(useruuid
, groupuuid
))
1523 else if (!mbr_check_membership(useruuid
, groupuuid
, &is_member
))
1531 else if (groupname
[0] == '#')
1533 #endif /* HAVE_MBR_UID_TO_UUID */
1536 * Username not found, group not found, or user is not part of the
1537 * system group... Check for a user and group in the MD5 password
1541 if (get_md5_password(username
, groupname
, junk
) != NULL
)
1545 * If we get this far, then the user isn't part of the named group...
1553 * 'cupsdCopyLocation()' - Make a copy of a location...
1556 cupsd_location_t
* /* O - New location */
1558 cupsd_location_t
*loc
) /* I - Original location */
1560 cupsd_location_t
*temp
; /* New location */
1564 * Make a copy of the original location...
1567 if ((temp
= calloc(1, sizeof(cupsd_location_t
))) == NULL
)
1571 * Copy the information from the original location to the new one.
1578 temp
->location
= _cupsStrAlloc(loc
->location
);
1580 temp
->limit
= loc
->limit
;
1581 temp
->order_type
= loc
->order_type
;
1582 temp
->type
= loc
->type
;
1583 temp
->level
= loc
->level
;
1584 temp
->satisfy
= loc
->satisfy
;
1585 temp
->encryption
= loc
->encryption
;
1589 if ((temp
->names
= cupsArrayDup(loc
->names
)) == NULL
)
1591 cupsdLogMessage(CUPSD_LOG_ERROR
,
1592 "Unable to allocate memory for %d names: %s",
1593 cupsArrayCount(loc
->names
), strerror(errno
));
1595 cupsdFreeLocation(temp
);
1603 * Copy allow rules...
1606 if ((temp
->allow
= cupsArrayDup(loc
->allow
)) == NULL
)
1608 cupsdLogMessage(CUPSD_LOG_ERROR
,
1609 "Unable to allocate memory for %d allow rules: %s",
1610 cupsArrayCount(loc
->allow
), strerror(errno
));
1611 cupsdFreeLocation(temp
);
1619 * Copy deny rules...
1622 if ((temp
->deny
= cupsArrayDup(loc
->deny
)) == NULL
)
1624 cupsdLogMessage(CUPSD_LOG_ERROR
,
1625 "Unable to allocate memory for %d deny rules: %s",
1626 cupsArrayCount(loc
->deny
), strerror(errno
));
1627 cupsdFreeLocation(temp
);
1637 * 'cupsdDeleteAllLocations()' - Free all memory used for location authorization.
1641 cupsdDeleteAllLocations(void)
1644 * Free the location array, which will free all of the locations...
1647 cupsArrayDelete(Locations
);
1653 * 'cupsdFindBest()' - Find the location entry that best matches the resource.
1656 cupsd_location_t
* /* O - Location that matches */
1657 cupsdFindBest(const char *path
, /* I - Resource path */
1658 http_state_t state
) /* I - HTTP state/request */
1660 char uri
[HTTP_MAX_URI
],
1661 /* URI in request... */
1662 *uriptr
; /* Pointer into URI */
1663 cupsd_location_t
*loc
, /* Current location */
1664 *best
; /* Best match for location so far */
1665 int bestlen
; /* Length of best match */
1666 int limit
; /* Limit field */
1667 static const int limits
[] = /* Map http_status_t to CUPSD_AUTH_LIMIT_xyz */
1669 CUPSD_AUTH_LIMIT_ALL
,
1670 CUPSD_AUTH_LIMIT_OPTIONS
,
1671 CUPSD_AUTH_LIMIT_GET
,
1672 CUPSD_AUTH_LIMIT_GET
,
1673 CUPSD_AUTH_LIMIT_HEAD
,
1674 CUPSD_AUTH_LIMIT_POST
,
1675 CUPSD_AUTH_LIMIT_POST
,
1676 CUPSD_AUTH_LIMIT_POST
,
1677 CUPSD_AUTH_LIMIT_PUT
,
1678 CUPSD_AUTH_LIMIT_PUT
,
1679 CUPSD_AUTH_LIMIT_DELETE
,
1680 CUPSD_AUTH_LIMIT_TRACE
,
1681 CUPSD_AUTH_LIMIT_ALL
,
1682 CUPSD_AUTH_LIMIT_ALL
1687 * First copy the connection URI to a local string so we have drop
1688 * any .ppd extension from the pathname in /printers or /classes
1692 strlcpy(uri
, path
, sizeof(uri
));
1694 if (!strncmp(uri
, "/printers/", 10) ||
1695 !strncmp(uri
, "/classes/", 9))
1698 * Check if the URI has .ppd on the end...
1701 uriptr
= uri
+ strlen(uri
) - 4; /* len > 4 if we get here... */
1703 if (!strcmp(uriptr
, ".ppd"))
1707 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: uri = \"%s\"...", uri
);
1710 * Loop through the list of locations to find a match...
1713 limit
= limits
[state
];
1717 for (loc
= (cupsd_location_t
*)cupsArrayFirst(Locations
);
1719 loc
= (cupsd_location_t
*)cupsArrayNext(Locations
))
1721 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: Location %s Limit %x",
1722 loc
->location
? loc
->location
: "nil", loc
->limit
);
1724 if (!strncmp(uri
, "/printers/", 10) || !strncmp(uri
, "/classes/", 9))
1727 * Use case-insensitive comparison for queue names...
1730 if (loc
->length
> bestlen
&& loc
->location
&&
1731 !_cups_strncasecmp(uri
, loc
->location
, loc
->length
) &&
1732 loc
->location
[0] == '/' &&
1733 (limit
& loc
->limit
) != 0)
1736 bestlen
= loc
->length
;
1742 * Use case-sensitive comparison for other URIs...
1745 if (loc
->length
> bestlen
&& loc
->location
&&
1746 !strncmp(uri
, loc
->location
, loc
->length
) &&
1747 loc
->location
[0] == '/' &&
1748 (limit
& loc
->limit
) != 0)
1751 bestlen
= loc
->length
;
1757 * Return the match, if any...
1760 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: best = %s",
1761 best
? best
->location
: "NONE");
1768 * 'cupsdFindLocation()' - Find the named location.
1771 cupsd_location_t
* /* O - Location that matches */
1772 cupsdFindLocation(const char *location
) /* I - Connection */
1774 cupsd_location_t key
; /* Search key */
1777 key
.location
= (char *)location
;
1779 return ((cupsd_location_t
*)cupsArrayFind(Locations
, &key
));
1784 * 'cupsdFreeLocation()' - Free all memory used by a location.
1788 cupsdFreeLocation(cupsd_location_t
*loc
)/* I - Location to free */
1790 cupsArrayDelete(loc
->names
);
1791 cupsArrayDelete(loc
->allow
);
1792 cupsArrayDelete(loc
->deny
);
1794 _cupsStrFree(loc
->location
);
1800 * 'cupsdIsAuthorized()' - Check to see if the user is authorized...
1803 http_status_t
/* O - HTTP_OK if authorized or error code */
1804 cupsdIsAuthorized(cupsd_client_t
*con
, /* I - Connection */
1805 const char *owner
)/* I - Owner of object */
1807 int i
, /* Looping vars */
1808 auth
, /* Authorization status */
1809 type
; /* Type of authentication */
1810 unsigned address
[4]; /* Authorization address */
1811 cupsd_location_t
*best
; /* Best match for location so far */
1812 int hostlen
; /* Length of hostname */
1813 char *name
, /* Current username */
1814 username
[256], /* Username to authorize */
1815 ownername
[256], /* Owner name to authorize */
1816 *ptr
; /* Pointer into username */
1817 struct passwd
*pw
; /* User password data */
1818 static const char * const levels
[] = /* Auth levels */
1824 static const char * const types
[] = /* Auth types */
1834 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1835 "cupsdIsAuthorized: con->uri=\"%s\", con->best=%p(%s)",
1836 con
->uri
, con
->best
, con
->best
? con
->best
->location
?
1837 con
->best
->location
: "(null)" : "");
1839 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1840 "cupsdIsAuthorized: owner=\"%s\"", owner
);
1843 * If there is no "best" authentication rule for this request, then
1844 * access is allowed from the local system and denied from other
1850 if (!strcmp(con
->http
.hostname
, "localhost") ||
1851 !strcmp(con
->http
.hostname
, ServerName
))
1854 return (HTTP_FORBIDDEN
);
1859 if ((type
= best
->type
) == CUPSD_AUTH_DEFAULT
)
1860 type
= cupsdDefaultAuthType();
1862 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1863 "cupsdIsAuthorized: level=CUPSD_AUTH_%s, type=%s, "
1864 "satisfy=CUPSD_AUTH_SATISFY_%s, num_names=%d",
1865 levels
[best
->level
], types
[type
],
1866 best
->satisfy
? "ANY" : "ALL", cupsArrayCount(best
->names
));
1868 if (best
->limit
== CUPSD_AUTH_LIMIT_IPP
)
1869 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: op=%x(%s)",
1870 best
->op
, ippOpString(best
->op
));
1873 * Check host/ip-based accesses...
1877 if (con
->http
.hostaddr
->addr
.sa_family
== AF_INET6
)
1880 * Copy IPv6 address...
1883 address
[0] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[0]);
1884 address
[1] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[1]);
1885 address
[2] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[2]);
1886 address
[3] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[3]);
1889 #endif /* AF_INET6 */
1890 if (con
->http
.hostaddr
->addr
.sa_family
== AF_INET
)
1893 * Copy IPv4 address...
1899 address
[3] = ntohl(con
->http
.hostaddr
->ipv4
.sin_addr
.s_addr
);
1902 memset(address
, 0, sizeof(address
));
1904 hostlen
= strlen(con
->http
.hostname
);
1906 auth
= cupsdCheckAccess(address
, con
->http
.hostname
, hostlen
, best
)
1907 ? CUPSD_AUTH_ALLOW
: CUPSD_AUTH_DENY
;
1909 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: auth=CUPSD_AUTH_%s...",
1910 auth
? "DENY" : "ALLOW");
1912 if (auth
== CUPSD_AUTH_DENY
&& best
->satisfy
== CUPSD_AUTH_SATISFY_ALL
)
1913 return (HTTP_FORBIDDEN
);
1917 * See if encryption is required...
1920 if ((best
->encryption
>= HTTP_ENCRYPT_REQUIRED
&& !con
->http
.tls
&&
1921 _cups_strcasecmp(con
->http
.hostname
, "localhost") &&
1922 best
->satisfy
== CUPSD_AUTH_SATISFY_ALL
) &&
1923 !(type
== CUPSD_AUTH_NEGOTIATE
||
1924 (type
== CUPSD_AUTH_NONE
&&
1925 cupsdDefaultAuthType() == CUPSD_AUTH_NEGOTIATE
)))
1927 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1928 "cupsdIsAuthorized: Need upgrade to TLS...");
1929 return (HTTP_UPGRADE_REQUIRED
);
1931 #endif /* HAVE_SSL */
1934 * Now see what access level is required...
1937 if (best
->level
== CUPSD_AUTH_ANON
|| /* Anonymous access - allow it */
1938 (type
== CUPSD_AUTH_NONE
&& cupsArrayCount(best
->names
) == 0))
1941 if (!con
->username
[0] && type
== CUPSD_AUTH_NONE
&&
1942 best
->limit
== CUPSD_AUTH_LIMIT_IPP
)
1945 * Check for unauthenticated username...
1948 ipp_attribute_t
*attr
; /* requesting-user-name attribute */
1951 attr
= ippFindAttribute(con
->request
, "requesting-user-name", IPP_TAG_NAME
);
1954 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1955 "cupsdIsAuthorized: requesting-user-name=\"%s\"",
1956 attr
->values
[0].string
.text
);
1957 strlcpy(username
, attr
->values
[0].string
.text
, sizeof(username
));
1959 else if (best
->satisfy
== CUPSD_AUTH_SATISFY_ALL
|| auth
== CUPSD_AUTH_DENY
)
1960 return (HTTP_UNAUTHORIZED
); /* Non-anonymous needs user/pass */
1962 return (HTTP_OK
); /* unless overridden with Satisfy */
1966 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdIsAuthorized: username=\"%s\"",
1969 #ifdef HAVE_AUTHORIZATION_H
1970 if (!con
->username
[0] && !con
->authref
)
1972 if (!con
->username
[0])
1973 #endif /* HAVE_AUTHORIZATION_H */
1975 if (best
->satisfy
== CUPSD_AUTH_SATISFY_ALL
|| auth
== CUPSD_AUTH_DENY
)
1976 return (HTTP_UNAUTHORIZED
); /* Non-anonymous needs user/pass */
1978 return (HTTP_OK
); /* unless overridden with Satisfy */
1982 if (con
->type
!= type
&& type
!= CUPSD_AUTH_NONE
&&
1984 (type
!= CUPSD_AUTH_NEGOTIATE
|| con
->gss_uid
<= 0) &&
1985 #endif /* HAVE_GSSAPI */
1986 (con
->type
!= CUPSD_AUTH_BASIC
|| type
!= CUPSD_AUTH_BASICDIGEST
))
1988 cupsdLogMessage(CUPSD_LOG_ERROR
, "Authorized using %s, expected %s!",
1989 types
[con
->type
], types
[type
]);
1991 return (HTTP_UNAUTHORIZED
);
1994 strlcpy(username
, con
->username
, sizeof(username
));
1998 * OK, got a username. See if we need normal user access, or group
1999 * access... (root always matches)
2002 if (!strcmp(username
, "root"))
2006 * Strip any @domain or @KDC from the username and owner...
2009 if ((ptr
= strchr(username
, '@')) != NULL
)
2014 strlcpy(ownername
, owner
, sizeof(ownername
));
2016 if ((ptr
= strchr(ownername
, '@')) != NULL
)
2020 ownername
[0] = '\0';
2023 * Get the user info...
2028 pw
= getpwnam(username
);
2034 if (best
->level
== CUPSD_AUTH_USER
)
2037 * If there are no names associated with this location, then
2038 * any valid user is OK...
2041 if (cupsArrayCount(best
->names
) == 0)
2045 * Otherwise check the user list and return OK if this user is
2049 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2050 "cupsdIsAuthorized: Checking user membership...");
2052 #ifdef HAVE_AUTHORIZATION_H
2054 * If an authorization reference was supplied it must match a right name...
2059 for (name
= (char *)cupsArrayFirst(best
->names
);
2061 name
= (char *)cupsArrayNext(best
->names
))
2063 if (!_cups_strncasecmp(name
, "@AUTHKEY(", 9) && check_authref(con
, name
+ 9))
2065 else if (!_cups_strcasecmp(name
, "@SYSTEM") && SystemGroupAuthKey
&&
2066 check_authref(con
, SystemGroupAuthKey
))
2070 return (HTTP_FORBIDDEN
);
2072 #endif /* HAVE_AUTHORIZATION_H */
2074 for (name
= (char *)cupsArrayFirst(best
->names
);
2076 name
= (char *)cupsArrayNext(best
->names
))
2078 if (!_cups_strcasecmp(name
, "@OWNER") && owner
&&
2079 !_cups_strcasecmp(username
, ownername
))
2081 else if (!_cups_strcasecmp(name
, "@SYSTEM"))
2083 for (i
= 0; i
< NumSystemGroups
; i
++)
2084 if (cupsdCheckGroup(username
, pw
, SystemGroups
[i
]))
2087 else if (name
[0] == '@')
2089 if (cupsdCheckGroup(username
, pw
, name
+ 1))
2092 else if (!_cups_strcasecmp(username
, name
))
2096 return (con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
);
2100 * Check to see if this user is in any of the named groups...
2103 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2104 "cupsdIsAuthorized: Checking group membership...");
2107 * Check to see if this user is in any of the named groups...
2110 for (name
= (char *)cupsArrayFirst(best
->names
);
2112 name
= (char *)cupsArrayNext(best
->names
))
2114 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2115 "cupsdIsAuthorized: Checking group \"%s\" membership...",
2118 if (!_cups_strcasecmp(name
, "@SYSTEM"))
2120 for (i
= 0; i
< NumSystemGroups
; i
++)
2121 if (cupsdCheckGroup(username
, pw
, SystemGroups
[i
]))
2124 else if (cupsdCheckGroup(username
, pw
, name
))
2129 * The user isn't part of the specified group, so deny access...
2132 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2133 "cupsdIsAuthorized: User not in group(s)!");
2135 return (con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
);
2140 * 'cupsdNewLocation()' - Create a new location for authorization.
2142 * Note: Still need to call cupsdAddLocation() to add it to the list of global
2146 cupsd_location_t
* /* O - Pointer to new location record */
2147 cupsdNewLocation(const char *location
) /* I - Location path */
2149 cupsd_location_t
*temp
; /* New location */
2153 * Try to allocate memory for the new location.
2156 if ((temp
= calloc(1, sizeof(cupsd_location_t
))) == NULL
)
2160 * Initialize the record and copy the name over...
2163 if ((temp
->location
= _cupsStrAlloc(location
)) == NULL
)
2169 temp
->length
= strlen(temp
->location
);
2172 * Return the new record...
2179 #ifdef HAVE_AUTHORIZATION_H
2181 * 'check_authref()' - Check if an authorization services reference has the
2185 static int /* O - 1 if right is valid, 0 otherwise */
2186 check_authref(cupsd_client_t
*con
, /* I - Connection */
2187 const char *right
) /* I - Right name */
2189 OSStatus status
; /* OS Status */
2190 AuthorizationItem authright
; /* Authorization right */
2191 AuthorizationRights authrights
; /* Authorization rights */
2192 AuthorizationFlags authflags
; /* Authorization flags */
2196 * Check to see if the user is allowed to perform the task...
2202 authright
.name
= right
;
2203 authright
.valueLength
= 0;
2204 authright
.value
= NULL
;
2205 authright
.flags
= 0;
2207 authrights
.count
= 1;
2208 authrights
.items
= &authright
;
2210 authflags
= kAuthorizationFlagDefaults
|
2211 kAuthorizationFlagExtendRights
;
2213 if ((status
= AuthorizationCopyRights(con
->authref
, &authrights
,
2214 kAuthorizationEmptyEnvironment
,
2215 authflags
, NULL
)) != 0)
2217 cupsdLogMessage(CUPSD_LOG_ERROR
,
2218 "AuthorizationCopyRights(\"%s\") returned %d (%s)",
2219 authright
.name
, (int)status
, cssmErrorString(status
));
2223 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2224 "AuthorizationCopyRights(\"%s\") succeeded!",
2229 #endif /* HAVE_AUTHORIZATION_H */
2233 * 'compare_locations()' - Compare two locations.
2236 static int /* O - Result of comparison */
2237 compare_locations(cupsd_location_t
*a
, /* I - First location */
2238 cupsd_location_t
*b
) /* I - Second location */
2240 return (strcmp(b
->location
, a
->location
));
2245 * 'copy_authmask()' - Copy function for auth masks.
2248 static cupsd_authmask_t
* /* O - New auth mask */
2249 copy_authmask(cupsd_authmask_t
*mask
, /* I - Existing auth mask */
2250 void *data
) /* I - User data (unused) */
2252 cupsd_authmask_t
*temp
; /* New auth mask */
2257 if ((temp
= malloc(sizeof(cupsd_authmask_t
))) != NULL
)
2259 memcpy(temp
, mask
, sizeof(cupsd_authmask_t
));
2261 if (temp
->type
== CUPSD_AUTH_NAME
|| temp
->type
== CUPSD_AUTH_INTERFACE
)
2264 * Make a copy of the name...
2267 if ((temp
->mask
.name
.name
= _cupsStrAlloc(temp
->mask
.name
.name
)) == NULL
)
2270 * Failed to make copy...
2283 #if !HAVE_LIBPAM && !defined(HAVE_USERSEC_H)
2285 * 'cups_crypt()' - Encrypt the password using the DES or MD5 algorithms,
2289 static char * /* O - Encrypted password */
2290 cups_crypt(const char *pw
, /* I - Password string */
2291 const char *salt
) /* I - Salt (key) string */
2293 if (!strncmp(salt
, "$1$", 3))
2296 * Use MD5 passwords without the benefit of PAM; this is for
2297 * Slackware Linux, and the algorithm was taken from the
2298 * old shadow-19990827/lib/md5crypt.c source code... :(
2301 int i
; /* Looping var */
2302 unsigned long n
; /* Output number */
2303 int pwlen
; /* Length of password string */
2304 const char *salt_end
; /* End of "salt" data for MD5 */
2305 char *ptr
; /* Pointer into result string */
2306 _cups_md5_state_t state
; /* Primary MD5 state info */
2307 _cups_md5_state_t state2
; /* Secondary MD5 state info */
2308 unsigned char digest
[16]; /* MD5 digest result */
2309 static char result
[120]; /* Final password string */
2313 * Get the salt data between dollar signs, e.g. $1$saltdata$md5.
2314 * Get a maximum of 8 characters of salt data after $1$...
2317 for (salt_end
= salt
+ 3; *salt_end
&& (salt_end
- salt
) < 11; salt_end
++)
2318 if (*salt_end
== '$')
2322 * Compute the MD5 sum we need...
2327 _cupsMD5Init(&state
);
2328 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
2329 _cupsMD5Append(&state
, (unsigned char *)salt
, salt_end
- salt
);
2331 _cupsMD5Init(&state2
);
2332 _cupsMD5Append(&state2
, (unsigned char *)pw
, pwlen
);
2333 _cupsMD5Append(&state2
, (unsigned char *)salt
+ 3, salt_end
- salt
- 3);
2334 _cupsMD5Append(&state2
, (unsigned char *)pw
, pwlen
);
2335 _cupsMD5Finish(&state2
, digest
);
2337 for (i
= pwlen
; i
> 0; i
-= 16)
2338 _cupsMD5Append(&state
, digest
, i
> 16 ? 16 : i
);
2340 for (i
= pwlen
; i
> 0; i
>>= 1)
2341 _cupsMD5Append(&state
, (unsigned char *)((i
& 1) ? "" : pw
), 1);
2343 _cupsMD5Finish(&state
, digest
);
2345 for (i
= 0; i
< 1000; i
++)
2347 _cupsMD5Init(&state
);
2350 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
2352 _cupsMD5Append(&state
, digest
, 16);
2355 _cupsMD5Append(&state
, (unsigned char *)salt
+ 3, salt_end
- salt
- 3);
2358 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
2361 _cupsMD5Append(&state
, digest
, 16);
2363 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
2365 _cupsMD5Finish(&state
, digest
);
2369 * Copy the final sum to the result string and return...
2372 memcpy(result
, salt
, salt_end
- salt
);
2373 ptr
= result
+ (salt_end
- salt
);
2376 for (i
= 0; i
< 5; i
++, ptr
+= 4)
2378 n
= (((digest
[i
] << 8) | digest
[i
+ 6]) << 8);
2381 n
|= digest
[i
+ 12];
2388 to64(ptr
, digest
[11], 2);
2397 * Use the standard crypt() function...
2400 return (crypt(pw
, salt
));
2403 #endif /* !HAVE_LIBPAM && !HAVE_USERSEC_H */
2407 * 'free_authmask()' - Free function for auth masks.
2411 free_authmask(cupsd_authmask_t
*mask
, /* I - Auth mask to free */
2412 void *data
) /* I - User data (unused) */
2416 if (mask
->type
== CUPSD_AUTH_NAME
|| mask
->type
== CUPSD_AUTH_INTERFACE
)
2417 _cupsStrFree(mask
->mask
.name
.name
);
2424 * 'get_md5_password()' - Get an MD5 password.
2427 static char * /* O - MD5 password string */
2428 get_md5_password(const char *username
, /* I - Username */
2429 const char *group
, /* I - Group */
2430 char passwd
[33]) /* O - MD5 password string */
2432 cups_file_t
*fp
; /* passwd.md5 file */
2433 char filename
[1024], /* passwd.md5 filename */
2434 line
[256], /* Line from file */
2435 tempuser
[33], /* User from file */
2436 tempgroup
[33]; /* Group from file */
2439 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2440 "get_md5_password(username=\"%s\", group=\"%s\", passwd=%p)",
2441 username
, group
? group
: "(null)", passwd
);
2443 snprintf(filename
, sizeof(filename
), "%s/passwd.md5", ServerRoot
);
2444 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
2446 if (errno
!= ENOENT
)
2447 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open %s - %s", filename
,
2453 while (cupsFileGets(fp
, line
, sizeof(line
)) != NULL
)
2455 if (sscanf(line
, "%32[^:]:%32[^:]:%32s", tempuser
, tempgroup
, passwd
) != 3)
2457 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad MD5 password line: %s", line
);
2461 if (!strcmp(username
, tempuser
) &&
2462 (group
== NULL
|| !strcmp(group
, tempgroup
)))
2465 * Found the password entry!
2468 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Found MD5 user %s, group %s...",
2469 username
, tempgroup
);
2477 * Didn't find a password entry - return NULL!
2487 * 'pam_func()' - PAM conversation function.
2490 static int /* O - Success or failure */
2492 int num_msg
, /* I - Number of messages */
2493 const struct pam_message
**msg
, /* I - Messages */
2494 struct pam_response
**resp
, /* O - Responses */
2496 /* I - Pointer to connection */
2498 int i
; /* Looping var */
2499 struct pam_response
*replies
; /* Replies */
2500 cupsd_authdata_t
*data
; /* Pointer to auth data */
2504 * Allocate memory for the responses...
2507 if ((replies
= malloc(sizeof(struct pam_response
) * num_msg
)) == NULL
)
2508 return (PAM_CONV_ERR
);
2511 * Answer all of the messages...
2514 DEBUG_printf(("pam_func: appdata_ptr = %p\n", appdata_ptr
));
2518 * Apparently some versions of HP-UX 11 have a broken pam_unix security
2519 * module. This is a workaround...
2525 data
= (cupsd_authdata_t
*)appdata_ptr
;
2528 for (i
= 0; i
< num_msg
; i
++)
2530 DEBUG_printf(("pam_func: Message = \"%s\"\n", msg
[i
]->msg
));
2532 switch (msg
[i
]->msg_style
)
2534 case PAM_PROMPT_ECHO_ON
:
2535 DEBUG_printf(("pam_func: PAM_PROMPT_ECHO_ON, returning \"%s\"...\n",
2537 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2538 replies
[i
].resp
= strdup(data
->username
);
2541 case PAM_PROMPT_ECHO_OFF
:
2542 DEBUG_printf(("pam_func: PAM_PROMPT_ECHO_OFF, returning \"%s\"...\n",
2544 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2545 replies
[i
].resp
= strdup(data
->password
);
2549 DEBUG_puts("pam_func: PAM_TEXT_INFO...");
2550 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2551 replies
[i
].resp
= NULL
;
2555 DEBUG_puts("pam_func: PAM_ERROR_MSG...");
2556 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2557 replies
[i
].resp
= NULL
;
2561 DEBUG_printf(("pam_func: Unknown PAM message %d...\n",
2562 msg
[i
]->msg_style
));
2564 return (PAM_CONV_ERR
);
2569 * Return the responses back to PAM...
2574 return (PAM_SUCCESS
);
2576 #elif !defined(HAVE_USERSEC_H)
2580 * 'to64()' - Base64-encode an integer value...
2584 to64(char *s
, /* O - Output string */
2585 unsigned long v
, /* I - Value to encode */
2586 int n
) /* I - Number of digits */
2588 const char *itoa64
= "./0123456789"
2589 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2590 "abcdefghijklmnopqrstuvwxyz";
2593 for (; n
> 0; n
--, v
>>= 6)
2594 *s
++ = itoa64
[v
& 0x3f];
2596 #endif /* HAVE_LIBPAM */
2600 * End of "$Id: auth.c 7830 2008-08-04 20:38:50Z mike $".