4 * Authorization routines for the CUPS scheduler.
6 * Copyright 2007-2013 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 * Include necessary headers...
27 #endif /* HAVE_SHADOW_H */
30 #endif /* HAVE_CRYPT_H */
32 # ifdef HAVE_PAM_PAM_APPL_H
33 # include <pam/pam_appl.h>
35 # include <security/pam_appl.h>
36 # endif /* HAVE_PAM_PAM_APPL_H */
37 #endif /* HAVE_LIBPAM */
40 #endif /* HAVE_USERSEC_H */
41 #ifdef HAVE_MEMBERSHIP_H
42 # include <membership.h>
43 #endif /* HAVE_MEMBERSHIP_H */
44 #ifdef HAVE_AUTHORIZATION_H
45 # include <Security/AuthorizationTags.h>
46 # ifdef HAVE_SECBASEPRIV_H
47 # include <Security/SecBasePriv.h>
49 extern const char *cssmErrorString(int error
);
50 # endif /* HAVE_SECBASEPRIV_H */
51 #endif /* HAVE_AUTHORIZATION_H */
52 #ifdef HAVE_SYS_PARAM_H
53 # include <sys/param.h>
54 #endif /* HAVE_SYS_PARAM_H */
55 #ifdef HAVE_SYS_UCRED_H
56 # include <sys/ucred.h>
57 typedef struct xucred cupsd_ucred_t
;
58 # define CUPSD_UCRED_UID(c) (c).cr_uid
61 typedef struct ucred cupsd_ucred_t
;
63 typedef struct sockpeercred cupsd_ucred_t
;
65 # define CUPSD_UCRED_UID(c) (c).uid
66 #endif /* HAVE_SYS_UCRED_H */
73 #ifdef HAVE_AUTHORIZATION_H
74 static int check_authref(cupsd_client_t
*con
, const char *right
);
75 #endif /* HAVE_AUTHORIZATION_H */
76 static int compare_locations(cupsd_location_t
*a
,
78 static cupsd_authmask_t
*copy_authmask(cupsd_authmask_t
*am
, void *data
);
79 #if !HAVE_LIBPAM && !defined(HAVE_USERSEC_H)
80 static char *cups_crypt(const char *pw
, const char *salt
);
81 #endif /* !HAVE_LIBPAM && !HAVE_USERSEC_H */
82 static void free_authmask(cupsd_authmask_t
*am
, void *data
);
83 static char *get_md5_password(const char *username
,
84 const char *group
, char passwd
[33]);
86 static int pam_func(int, const struct pam_message
**,
87 struct pam_response
**, void *);
88 #elif !defined(HAVE_USERSEC_H)
89 static void to64(char *s
, unsigned long v
, int n
);
90 #endif /* HAVE_LIBPAM */
98 typedef struct cupsd_authdata_s
/**** Authentication data ****/
100 char username
[HTTP_MAX_VALUE
], /* Username string */
101 password
[HTTP_MAX_VALUE
]; /* Password string */
103 #endif /* HAVE_LIBPAM */
110 #if defined(__hpux) && HAVE_LIBPAM
111 static cupsd_authdata_t
*auth_data
; /* Current client being authenticated */
112 #endif /* __hpux && HAVE_LIBPAM */
116 * 'cupsdAddIPMask()' - Add an IP address authorization mask.
119 int /* O - 1 on success, 0 on failure */
121 cups_array_t
**masks
, /* IO - Masks array (created as needed) */
122 const unsigned address
[4], /* I - IP address */
123 const unsigned netmask
[4]) /* I - IP netmask */
125 cupsd_authmask_t temp
; /* New host/domain mask */
128 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
129 "cupsdAddIPMask(masks=%p(%p), address=%x:%x:%x:%x, "
130 "netmask=%x:%x:%x:%x)",
132 address
[0], address
[1], address
[2], address
[3],
133 netmask
[0], netmask
[1], netmask
[2], netmask
[3]);
135 temp
.type
= CUPSD_AUTH_IP
;
136 memcpy(temp
.mask
.ip
.address
, address
, sizeof(temp
.mask
.ip
.address
));
137 memcpy(temp
.mask
.ip
.netmask
, netmask
, sizeof(temp
.mask
.ip
.netmask
));
140 * Create the masks array as needed and add...
144 *masks
= cupsArrayNew3(NULL
, NULL
, NULL
, 0,
145 (cups_acopy_func_t
)copy_authmask
,
146 (cups_afree_func_t
)free_authmask
);
148 return (cupsArrayAdd(*masks
, &temp
));
153 * 'cupsdAddLocation()' - Add a location for authorization.
157 cupsdAddLocation(cupsd_location_t
*loc
) /* I - Location to add */
160 * Make sure the locations array is created...
164 Locations
= cupsArrayNew3((cups_array_func_t
)compare_locations
, NULL
,
165 (cups_ahash_func_t
)NULL
, 0,
166 (cups_acopy_func_t
)NULL
,
167 (cups_afree_func_t
)cupsdFreeLocation
);
171 cupsArrayAdd(Locations
, loc
);
173 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAddLocation: Added location \"%s\"",
174 loc
->location
? loc
->location
: "(null)");
180 * 'cupsdAddName()' - Add a name to a location...
184 cupsdAddName(cupsd_location_t
*loc
, /* I - Location to add to */
185 char *name
) /* I - Name to add */
187 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAddName(loc=%p, name=\"%s\")",
191 loc
->names
= cupsArrayNew3(NULL
, NULL
, NULL
, 0,
192 (cups_acopy_func_t
)_cupsStrAlloc
,
193 (cups_afree_func_t
)_cupsStrFree
);
195 if (!cupsArrayAdd(loc
->names
, name
))
197 cupsdLogMessage(CUPSD_LOG_ERROR
,
198 "Unable to duplicate name for location %s: %s",
199 loc
->location
? loc
->location
: "nil", strerror(errno
));
206 * 'cupsdAddNameMask()' - Add a host or interface name authorization mask.
209 int /* O - 1 on success, 0 on failure */
210 cupsdAddNameMask(cups_array_t
**masks
, /* IO - Masks array (created as needed) */
211 char *name
) /* I - Host or interface name */
213 cupsd_authmask_t temp
; /* New host/domain mask */
214 char ifname
[32], /* Interface name */
215 *ifptr
; /* Pointer to end of name */
218 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
219 "cupsdAddNameMask(masks=%p(%p), name=\"%s\")",
220 masks
, *masks
, name
);
222 if (!_cups_strcasecmp(name
, "@LOCAL"))
225 * Deny *interface*...
228 temp
.type
= CUPSD_AUTH_INTERFACE
;
229 temp
.mask
.name
.name
= (char *)"*";
231 else if (!_cups_strncasecmp(name
, "@IF(", 4))
234 * Deny *interface*...
237 strlcpy(ifname
, name
+ 4, sizeof(ifname
));
239 ifptr
= ifname
+ strlen(ifname
) - 1;
241 if (ifptr
>= ifname
&& *ifptr
== ')')
247 temp
.type
= CUPSD_AUTH_INTERFACE
;
248 temp
.mask
.name
.name
= ifname
;
259 temp
.type
= CUPSD_AUTH_NAME
;
260 temp
.mask
.name
.name
= (char *)name
;
264 * Set the name length...
267 temp
.mask
.name
.length
= strlen(temp
.mask
.name
.name
);
270 * Create the masks array as needed and add...
274 *masks
= cupsArrayNew3(NULL
, NULL
, NULL
, 0,
275 (cups_acopy_func_t
)copy_authmask
,
276 (cups_afree_func_t
)free_authmask
);
278 return (cupsArrayAdd(*masks
, &temp
));
283 * 'cupsdAuthorize()' - Validate any authorization credentials.
287 cupsdAuthorize(cupsd_client_t
*con
) /* I - Client connection */
289 int type
; /* Authentication type */
290 const char *authorization
; /* Pointer into Authorization string */
291 char *ptr
, /* Pointer into string */
292 username
[HTTP_MAX_VALUE
],
293 /* Username string */
294 password
[HTTP_MAX_VALUE
];
295 /* Password string */
296 cupsd_cert_t
*localuser
; /* Certificate username */
297 char nonce
[HTTP_MAX_VALUE
], /* Nonce value from client */
298 md5
[33], /* MD5 password */
299 basicmd5
[33]; /* MD5 of Basic password */
300 static const char * const states
[] = /* HTTP client states... */
320 * Locate the best matching location so we know what kind of
321 * authentication to expect...
324 con
->best
= cupsdFindBest(con
->uri
, httpGetState(con
->http
));
325 con
->type
= CUPSD_AUTH_NONE
;
327 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
328 "[Client %d] con->uri=\"%s\", con->best=%p(%s)",
329 con
->number
, con
->uri
, con
->best
,
330 con
->best
? con
->best
->location
: "");
332 if (con
->best
&& con
->best
->type
!= CUPSD_AUTH_NONE
)
334 if (con
->best
->type
== CUPSD_AUTH_DEFAULT
)
335 type
= cupsdDefaultAuthType();
337 type
= con
->best
->type
;
340 type
= cupsdDefaultAuthType();
343 * Decode the Authorization string...
346 authorization
= httpGetField(con
->http
, HTTP_FIELD_AUTHORIZATION
);
348 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "[Client %d] Authorization=\"%s\"",
349 con
->number
, authorization
);
356 #endif /* HAVE_GSSAPI */
358 #ifdef HAVE_AUTHORIZATION_H
361 AuthorizationFree(con
->authref
, kAuthorizationFlagDefaults
);
364 #endif /* HAVE_AUTHORIZATION_H */
369 * No authorization data provided, return early...
372 cupsdLogMessage(CUPSD_LOG_DEBUG
,
373 "[Client %d] No authentication data provided.",
377 #ifdef HAVE_AUTHORIZATION_H
378 else if (!strncmp(authorization
, "AuthRef ", 8) &&
379 httpAddrLocalhost(httpGetAddress(con
->http
)))
381 OSStatus status
; /* Status */
382 int authlen
; /* Auth string length */
383 AuthorizationItemSet
*authinfo
; /* Authorization item set */
386 * Get the Authorization Services data...
390 while (isspace(*authorization
& 255))
393 authlen
= sizeof(nonce
);
394 httpDecode64_2(nonce
, &authlen
, authorization
);
396 if (authlen
!= kAuthorizationExternalFormLength
)
398 cupsdLogMessage(CUPSD_LOG_ERROR
,
399 "[Client %d] External Authorization reference size is "
400 "incorrect.", con
->number
);
404 if ((status
= AuthorizationCreateFromExternalForm(
405 (AuthorizationExternalForm
*)nonce
, &con
->authref
)) != 0)
407 cupsdLogMessage(CUPSD_LOG_ERROR
,
408 "[Client %d] AuthorizationCreateFromExternalForm "
409 "returned %d (%s)", con
->number
, (int)status
,
410 cssmErrorString(status
));
416 if (!AuthorizationCopyInfo(con
->authref
, kAuthorizationEnvironmentUsername
,
419 if (authinfo
->count
== 1 && authinfo
->items
[0].value
&&
420 authinfo
->items
[0].valueLength
>= 2)
422 strlcpy(username
, authinfo
->items
[0].value
, sizeof(username
));
424 cupsdLogMessage(CUPSD_LOG_DEBUG
,
425 "[Client %d] Authorized as \"%s\" using AuthRef",
426 con
->number
, username
);
429 AuthorizationFreeItemSet(authinfo
);
435 * No username in AuthRef, grab username using peer credentials...
438 struct passwd
*pwd
; /* Password entry for this user */
439 cupsd_ucred_t peercred
; /* Peer credentials */
440 socklen_t peersize
; /* Size of peer credentials */
442 peersize
= sizeof(peercred
);
444 if (getsockopt(httpGetFd(con
->http
), 0, LOCAL_PEERCRED
, &peercred
, &peersize
))
446 cupsdLogMessage(CUPSD_LOG_ERROR
,
447 "[Client %d] Unable to get peer credentials - %s",
448 con
->number
, strerror(errno
));
452 if ((pwd
= getpwuid(CUPSD_UCRED_UID(peercred
))) == NULL
)
454 cupsdLogMessage(CUPSD_LOG_ERROR
,
455 "[Client %d] Unable to find UID %d for peer "
456 "credentials.", con
->number
,
457 (int)CUPSD_UCRED_UID(peercred
));
461 strlcpy(username
, pwd
->pw_name
, sizeof(username
));
463 cupsdLogMessage(CUPSD_LOG_DEBUG
,
464 "[Client %d] Authorized as \"%s\" using "
465 "AuthRef + PeerCred", con
->number
, username
);
468 con
->type
= CUPSD_AUTH_BASIC
;
470 #endif /* HAVE_AUTHORIZATION_H */
471 #if defined(SO_PEERCRED) && defined(AF_LOCAL)
472 else if (!strncmp(authorization
, "PeerCred ", 9) &&
473 con
->http
->hostaddr
->addr
.sa_family
== AF_LOCAL
)
476 * Use peer credentials from domain socket connection...
479 struct passwd
*pwd
; /* Password entry for this user */
480 cupsd_ucred_t peercred
; /* Peer credentials */
481 socklen_t peersize
; /* Size of peer credentials */
482 #ifdef HAVE_AUTHORIZATION_H
483 const char *name
; /* Authorizing name */
484 int no_peer
= 0; /* Don't allow peer credentials? */
487 * See if we should allow peer credentials...
490 for (name
= (char *)cupsArrayFirst(con
->best
->names
);
492 name
= (char *)cupsArrayNext(con
->best
->names
))
494 if (!_cups_strncasecmp(name
, "@AUTHKEY(", 9) ||
495 !_cups_strcasecmp(name
, "@SYSTEM"))
497 /* Normally don't want peer credentials if we need an auth key... */
500 else if (!_cups_strcasecmp(name
, "@OWNER"))
502 /* but if @OWNER is present then we allow it... */
510 cupsdLogMessage(CUPSD_LOG_ERROR
,
511 "[Client %d] PeerCred authentication not allowed for "
512 "resource per AUTHKEY policy.", con
->number
);
515 #endif /* HAVE_AUTHORIZATION_H */
517 if ((pwd
= getpwnam(authorization
+ 9)) == NULL
)
519 cupsdLogMessage(CUPSD_LOG_ERROR
,
520 "[Client %d] User \"%s\" does not exist.", con
->number
,
525 peersize
= sizeof(peercred
);
528 if (getsockopt(httpGetFd(con
->http
), 0, LOCAL_PEERCRED
, &peercred
, &peersize
))
530 if (getsockopt(httpGetFd(con
->http
), SOL_SOCKET
, SO_PEERCRED
, &peercred
, &peersize
))
531 # endif /* __APPLE__ */
533 cupsdLogMessage(CUPSD_LOG_ERROR
,
534 "[Client %d] Unable to get peer credentials - %s",
535 con
->number
, strerror(errno
));
539 if (pwd
->pw_uid
!= CUPSD_UCRED_UID(peercred
))
541 cupsdLogMessage(CUPSD_LOG_ERROR
,
542 "[Client %d] Invalid peer credentials for \"%s\" - got "
543 "%d, expected %d!", con
->number
, authorization
+ 9,
544 CUPSD_UCRED_UID(peercred
), pwd
->pw_uid
);
545 # ifdef HAVE_SYS_UCRED_H
546 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Client %d] cr_version=%d",
547 con
->number
, peercred
.cr_version
);
548 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Client %d] cr_uid=%d",
549 con
->number
, peercred
.cr_uid
);
550 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Client %d] cr_ngroups=%d",
551 con
->number
, peercred
.cr_ngroups
);
552 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Client %d] cr_groups[0]=%d",
553 con
->number
, peercred
.cr_groups
[0]);
554 # endif /* HAVE_SYS_UCRED_H */
558 strlcpy(username
, authorization
+ 9, sizeof(username
));
561 con
->gss_uid
= CUPSD_UCRED_UID(peercred
);
562 # endif /* HAVE_GSSAPI */
564 cupsdLogMessage(CUPSD_LOG_DEBUG
,
565 "[Client %d] Authorized as %s using PeerCred", con
->number
,
568 con
->type
= CUPSD_AUTH_BASIC
;
570 #endif /* SO_PEERCRED && AF_LOCAL */
571 else if (!strncmp(authorization
, "Local", 5) &&
572 httpAddrLocalhost(httpGetAddress(con
->http
)))
575 * Get Local certificate authentication data...
579 while (isspace(*authorization
& 255))
582 if ((localuser
= cupsdFindCert(authorization
)) == NULL
)
584 cupsdLogMessage(CUPSD_LOG_ERROR
,
585 "[Client %d] Local authentication certificate not found.",
590 strlcpy(username
, localuser
->username
, sizeof(username
));
591 con
->type
= localuser
->type
;
593 cupsdLogMessage(CUPSD_LOG_DEBUG
,
594 "[Client %d] Authorized as %s using Local", con
->number
,
597 else if (!strncmp(authorization
, "Basic", 5))
600 * Get the Basic authentication data...
603 int userlen
; /* Username:password length */
607 while (isspace(*authorization
& 255))
610 userlen
= sizeof(username
);
611 httpDecode64_2(username
, &userlen
, authorization
);
614 * Pull the username and password out...
617 if ((ptr
= strchr(username
, ':')) == NULL
)
619 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Client %d] Missing Basic password.",
629 * Username must not be empty...
632 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Client %d] Empty Basic username.",
640 * Password must not be empty...
643 cupsdLogMessage(CUPSD_LOG_ERROR
, "[Client %d] Empty Basic password.",
648 strlcpy(password
, ptr
, sizeof(password
));
651 * Validate the username and password...
657 case CUPSD_AUTH_BASIC
:
661 * Only use PAM to do authentication. This supports MD5
662 * passwords, among other things...
665 pam_handle_t
*pamh
; /* PAM authentication handle */
666 int pamerr
; /* PAM error code */
667 struct pam_conv pamdata
;/* PAM conversation data */
668 cupsd_authdata_t data
; /* Authentication data */
671 strlcpy(data
.username
, username
, sizeof(data
.username
));
672 strlcpy(data
.password
, password
, sizeof(data
.password
));
674 # if defined(__sun) || defined(__hpux)
675 pamdata
.conv
= (int (*)(int, struct pam_message
**,
676 struct pam_response
**,
679 pamdata
.conv
= pam_func
;
680 # endif /* __sun || __hpux */
681 pamdata
.appdata_ptr
= &data
;
685 * Workaround for HP-UX bug in pam_unix; see pam_func() below for
692 pamerr
= pam_start("cups", username
, &pamdata
, &pamh
);
693 if (pamerr
!= PAM_SUCCESS
)
695 cupsdLogMessage(CUPSD_LOG_ERROR
,
696 "[Client %d] pam_start() returned %d (%s)",
697 con
->number
, pamerr
, pam_strerror(pamh
, pamerr
));
701 # ifdef HAVE_PAM_SET_ITEM
703 pamerr
= pam_set_item(pamh
, PAM_RHOST
, con
->http
->hostname
);
704 if (pamerr
!= PAM_SUCCESS
)
705 cupsdLogMessage(CUPSD_LOG_WARN
,
706 "[Client %d] pam_set_item(PAM_RHOST) "
707 "returned %d (%s)", con
->number
, pamerr
,
708 pam_strerror(pamh
, pamerr
));
709 # endif /* PAM_RHOST */
712 pamerr
= pam_set_item(pamh
, PAM_TTY
, "cups");
713 if (pamerr
!= PAM_SUCCESS
)
714 cupsdLogMessage(CUPSD_LOG_WARN
,
715 "[Client %d] pam_set_item(PAM_TTY) "
716 "returned %d (%s)!", con
->number
, pamerr
,
717 pam_strerror(pamh
, pamerr
));
718 # endif /* PAM_TTY */
719 # endif /* HAVE_PAM_SET_ITEM */
721 pamerr
= pam_authenticate(pamh
, PAM_SILENT
);
722 if (pamerr
!= PAM_SUCCESS
)
724 cupsdLogMessage(CUPSD_LOG_ERROR
,
725 "[Client %d] pam_authenticate() returned %d (%s)",
726 con
->number
, pamerr
, pam_strerror(pamh
, pamerr
));
731 # ifdef HAVE_PAM_SETCRED
732 pamerr
= pam_setcred(pamh
, PAM_ESTABLISH_CRED
| PAM_SILENT
);
733 if (pamerr
!= PAM_SUCCESS
)
734 cupsdLogMessage(CUPSD_LOG_WARN
,
735 "[Client %d] pam_setcred() returned %d (%s)",
737 pam_strerror(pamh
, pamerr
));
738 # endif /* HAVE_PAM_SETCRED */
740 pamerr
= pam_acct_mgmt(pamh
, PAM_SILENT
);
741 if (pamerr
!= PAM_SUCCESS
)
743 cupsdLogMessage(CUPSD_LOG_ERROR
,
744 "[Client %d] pam_acct_mgmt() returned %d (%s)",
745 con
->number
, pamerr
, pam_strerror(pamh
, pamerr
));
750 pam_end(pamh
, PAM_SUCCESS
);
752 #elif defined(HAVE_USERSEC_H)
754 * Use AIX authentication interface...
757 char *authmsg
; /* Authentication message */
758 int reenter
; /* ??? */
761 cupsdLogMessage(CUPSD_LOG_DEBUG
,
762 "[Client %d] AIX authenticate of username \"%s\"",
763 con
->number
, username
);
766 if (authenticate(username
, password
, &reenter
, &authmsg
) != 0)
768 cupsdLogMessage(CUPSD_LOG_DEBUG
,
769 "[Client %d] Unable to authenticate username "
770 "\"%s\": %s", con
->number
, username
,
777 * Use normal UNIX password file-based authentication...
780 char *pass
; /* Encrypted password */
781 struct passwd
*pw
; /* User password data */
782 # ifdef HAVE_SHADOW_H
783 struct spwd
*spw
; /* Shadow password data */
784 # endif /* HAVE_SHADOW_H */
787 pw
= getpwnam(username
); /* Get the current password */
788 endpwent(); /* Close the password file */
796 cupsdLogMessage(CUPSD_LOG_ERROR
,
797 "[Client %d] Unknown username \"%s\".",
798 con
->number
, username
);
802 # ifdef HAVE_SHADOW_H
803 spw
= getspnam(username
);
806 if (!spw
&& !strcmp(pw
->pw_passwd
, "x"))
809 * Don't allow blank passwords!
812 cupsdLogMessage(CUPSD_LOG_ERROR
,
813 "[Client %d] Username \"%s\" has no shadow "
814 "password.", con
->number
, username
);
818 if (spw
&& !spw
->sp_pwdp
[0] && !pw
->pw_passwd
[0])
820 if (!pw
->pw_passwd
[0])
821 # endif /* HAVE_SHADOW_H */
824 * Don't allow blank passwords!
827 cupsdLogMessage(CUPSD_LOG_ERROR
,
828 "[Client %d] Username \"%s\" has no password.",
829 con
->number
, username
);
834 * OK, the password isn't blank, so compare with what came from the
838 pass
= cups_crypt(password
, pw
->pw_passwd
);
840 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
841 "[Client %d] pw_passwd=\"%s\", crypt=\"%s\"",
842 con
->number
, pw
->pw_passwd
, pass
);
844 if (!pass
|| strcmp(pw
->pw_passwd
, pass
))
846 # ifdef HAVE_SHADOW_H
849 pass
= cups_crypt(password
, spw
->sp_pwdp
);
851 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
852 "[Client %d] sp_pwdp=\"%s\", crypt=\"%s\"",
853 con
->number
, spw
->sp_pwdp
, pass
);
855 if (pass
== NULL
|| strcmp(spw
->sp_pwdp
, pass
))
857 cupsdLogMessage(CUPSD_LOG_ERROR
,
858 "[Client %d] Authentication failed for user "
859 "\"%s\".", con
->number
, username
);
864 # endif /* HAVE_SHADOW_H */
866 cupsdLogMessage(CUPSD_LOG_ERROR
,
867 "[Client %d] Authentication failed for user "
868 "\"%s\".", con
->number
, username
);
872 #endif /* HAVE_LIBPAM */
875 cupsdLogMessage(CUPSD_LOG_DEBUG
,
876 "[Client %d] Authorized as %s using Basic",
877 con
->number
, username
);
880 case CUPSD_AUTH_BASICDIGEST
:
882 * Do Basic authentication with the Digest password file...
885 if (!get_md5_password(username
, NULL
, md5
))
887 cupsdLogMessage(CUPSD_LOG_ERROR
,
888 "[Client %d] Unknown MD5 username \"%s\".",
889 con
->number
, username
);
893 httpMD5(username
, "CUPS", password
, basicmd5
);
895 if (strcmp(md5
, basicmd5
))
897 cupsdLogMessage(CUPSD_LOG_ERROR
,
898 "[Client %d] Authentication failed for \"%s\".",
899 con
->number
, username
);
903 cupsdLogMessage(CUPSD_LOG_DEBUG
,
904 "[Client %d] Authorized as %s using BasicDigest",
905 con
->number
, username
);
911 else if (!strncmp(authorization
, "Digest", 6))
914 * Get the username, password, and nonce from the Digest attributes...
917 if (!httpGetSubField2(con
->http
, HTTP_FIELD_AUTHORIZATION
, "username",
918 username
, sizeof(username
)) || !username
[0])
921 * Username must not be empty...
924 cupsdLogMessage(CUPSD_LOG_ERROR
,
925 "[Client %d] Empty or missing Digest username.",
930 if (!httpGetSubField2(con
->http
, HTTP_FIELD_AUTHORIZATION
, "response",
931 password
, sizeof(password
)) || !password
[0])
934 * Password must not be empty...
937 cupsdLogMessage(CUPSD_LOG_ERROR
,
938 "[Client %d] Empty or missing Digest password.",
943 if (!httpGetSubField(con
->http
, HTTP_FIELD_AUTHORIZATION
, "nonce",
946 cupsdLogMessage(CUPSD_LOG_ERROR
,
947 "[Client %d] No nonce value for Digest authentication.",
952 if (strcmp(con
->http
->hostname
, nonce
))
954 cupsdLogMessage(CUPSD_LOG_ERROR
,
955 "[Client %d] Bad nonce value, expected \"%s\", "
956 "got \"%s\".", con
->number
, con
->http
->hostname
, nonce
);
961 * Validate the username and password...
964 if (!get_md5_password(username
, NULL
, md5
))
966 cupsdLogMessage(CUPSD_LOG_ERROR
,
967 "[Client %d] Unknown MD5 username \"%s\".",
968 con
->number
, username
);
972 httpMD5Final(nonce
, states
[httpGetState(con
->http
)], con
->uri
, md5
);
974 if (strcmp(md5
, password
))
976 cupsdLogMessage(CUPSD_LOG_ERROR
,
977 "[Client %d] Authentication failed for \"%s\".",
978 con
->number
, username
);
982 cupsdLogMessage(CUPSD_LOG_DEBUG
,
983 "[Client %d] Authorized as %s using Digest", con
->number
,
986 con
->type
= CUPSD_AUTH_DIGEST
;
989 else if (!strncmp(authorization
, "Negotiate", 9))
991 int len
; /* Length of authorization string */
992 gss_ctx_id_t context
; /* Authorization context */
993 OM_uint32 major_status
, /* Major status code */
994 minor_status
; /* Minor status code */
995 gss_buffer_desc input_token
= GSS_C_EMPTY_BUFFER
,
996 /* Input token from string */
997 output_token
= GSS_C_EMPTY_BUFFER
;
998 /* Output token for username */
999 gss_name_t client_name
; /* Client name */
1004 * If the weak-linked GSSAPI/Kerberos library is not present, don't try
1008 if (gss_init_sec_context
== NULL
)
1010 cupsdLogMessage(CUPSD_LOG_WARN
,
1011 "[Client %d] GSSAPI/Kerberos authentication failed "
1012 "because the Kerberos framework is not present.",
1016 # endif /* __APPLE__ */
1019 * Find the start of the Kerberos input token...
1023 while (isspace(*authorization
& 255))
1026 if (!*authorization
)
1028 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1029 "[Client %d] No authentication data specified.",
1035 * Decode the authorization string to get the input token...
1038 len
= strlen(authorization
);
1039 input_token
.value
= malloc(len
);
1040 input_token
.value
= httpDecode64_2(input_token
.value
, &len
,
1042 input_token
.length
= len
;
1045 * Accept the input token to get the authorization info...
1048 context
= GSS_C_NO_CONTEXT
;
1049 client_name
= GSS_C_NO_NAME
;
1050 major_status
= gss_accept_sec_context(&minor_status
,
1054 GSS_C_NO_CHANNEL_BINDINGS
,
1062 if (output_token
.length
> 0)
1063 gss_release_buffer(&minor_status
, &output_token
);
1065 if (GSS_ERROR(major_status
))
1067 cupsdLogGSSMessage(CUPSD_LOG_DEBUG
, major_status
, minor_status
,
1068 "[Client %d] Error accepting GSSAPI security context",
1071 if (context
!= GSS_C_NO_CONTEXT
)
1072 gss_delete_sec_context(&minor_status
, &context
, GSS_C_NO_BUFFER
);
1079 * Get the username associated with the client's credentials...
1082 if (major_status
== GSS_S_CONTINUE_NEEDED
)
1083 cupsdLogGSSMessage(CUPSD_LOG_DEBUG
, major_status
, minor_status
,
1084 "[Client %d] Credentials not complete", con
->number
);
1085 else if (major_status
== GSS_S_COMPLETE
)
1087 major_status
= gss_display_name(&minor_status
, client_name
,
1088 &output_token
, NULL
);
1090 if (GSS_ERROR(major_status
))
1092 cupsdLogGSSMessage(CUPSD_LOG_DEBUG
, major_status
, minor_status
,
1093 "[Client %d] Error getting username", con
->number
);
1094 gss_release_name(&minor_status
, &client_name
);
1095 gss_delete_sec_context(&minor_status
, &context
, GSS_C_NO_BUFFER
);
1099 strlcpy(username
, output_token
.value
, sizeof(username
));
1101 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1102 "[Client %d] Authorized as %s using Negotiate",
1103 con
->number
, username
);
1105 gss_release_name(&minor_status
, &client_name
);
1106 gss_release_buffer(&minor_status
, &output_token
);
1108 con
->type
= CUPSD_AUTH_NEGOTIATE
;
1111 gss_delete_sec_context(&minor_status
, &context
, GSS_C_NO_BUFFER
);
1113 # if defined(SO_PEERCRED) && defined(AF_LOCAL)
1115 * Get the client's UID if we are printing locally - that allows a backend
1116 * to run as the correct user to get Kerberos credentials of its own.
1119 if (httpAddrFamily(con
->http
->hostaddr
) == AF_LOCAL
)
1121 cupsd_ucred_t peercred
; /* Peer credentials */
1122 socklen_t peersize
; /* Size of peer credentials */
1124 peersize
= sizeof(peercred
);
1127 if (getsockopt(httpGetFd(con
->http
), 0, LOCAL_PEERCRED
, &peercred
, &peersize
))
1129 if (getsockopt(httpGetFd(con
->http
), SOL_SOCKET
, SO_PEERCRED
, &peercred
,
1131 # endif /* __APPLE__ */
1133 cupsdLogMessage(CUPSD_LOG_ERROR
,
1134 "[Client %d] Unable to get peer credentials - %s",
1135 con
->number
, strerror(errno
));
1139 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1140 "[Client %d] Using credentials for UID %d.",
1141 con
->number
, CUPSD_UCRED_UID(peercred
));
1142 con
->gss_uid
= CUPSD_UCRED_UID(peercred
);
1145 # endif /* SO_PEERCRED && AF_LOCAL */
1147 #endif /* HAVE_GSSAPI */
1150 char scheme
[256]; /* Auth scheme... */
1153 if (sscanf(authorization
, "%255s", scheme
) != 1)
1154 strlcpy(scheme
, "UNKNOWN", sizeof(scheme
));
1156 cupsdLogMessage(CUPSD_LOG_ERROR
,
1157 "[Client %d] Bad authentication data \"%s ...\"",
1158 con
->number
, scheme
);
1163 * If we get here, then we were able to validate the username and
1164 * password - copy the validated username and password to the client
1165 * data and return...
1168 strlcpy(con
->username
, username
, sizeof(con
->username
));
1169 strlcpy(con
->password
, password
, sizeof(con
->password
));
1174 * 'cupsdCheckAccess()' - Check whether the given address is allowed to
1175 * access a location.
1178 int /* O - 1 if allowed, 0 otherwise */
1180 unsigned ip
[4], /* I - Client address */
1181 const char *name
, /* I - Client hostname */
1182 int namelen
, /* I - Length of hostname */
1183 cupsd_location_t
*loc
) /* I - Location to check */
1185 int allow
; /* 1 if allowed, 0 otherwise */
1188 if (!_cups_strcasecmp(name
, "localhost"))
1191 * Access from localhost (127.0.0.1 or ::1) is always allowed...
1199 * Do authorization checks on the domain/address...
1202 switch (loc
->order_type
)
1205 allow
= 0; /* anti-compiler-warning-code */
1208 case CUPSD_AUTH_ALLOW
: /* Order Deny,Allow */
1211 if (cupsdCheckAuth(ip
, name
, namelen
, loc
->deny
))
1214 if (cupsdCheckAuth(ip
, name
, namelen
, loc
->allow
))
1218 case CUPSD_AUTH_DENY
: /* Order Allow,Deny */
1221 if (cupsdCheckAuth(ip
, name
, namelen
, loc
->allow
))
1224 if (cupsdCheckAuth(ip
, name
, namelen
, loc
->deny
))
1235 * 'cupsdCheckAuth()' - Check authorization masks.
1238 int /* O - 1 if mask matches, 0 otherwise */
1239 cupsdCheckAuth(unsigned ip
[4], /* I - Client address */
1240 const char *name
, /* I - Client hostname */
1241 int name_len
, /* I - Length of hostname */
1242 cups_array_t
*masks
) /* I - Masks */
1244 int i
; /* Looping var */
1245 cupsd_authmask_t
*mask
; /* Current mask */
1246 cupsd_netif_t
*iface
; /* Network interface */
1247 unsigned netip4
; /* IPv4 network address */
1249 unsigned netip6
[4]; /* IPv6 network address */
1250 #endif /* AF_INET6 */
1253 for (mask
= (cupsd_authmask_t
*)cupsArrayFirst(masks
);
1255 mask
= (cupsd_authmask_t
*)cupsArrayNext(masks
))
1259 case CUPSD_AUTH_INTERFACE
:
1261 * Check for a match with a network interface...
1264 netip4
= htonl(ip
[3]);
1267 netip6
[0] = htonl(ip
[0]);
1268 netip6
[1] = htonl(ip
[1]);
1269 netip6
[2] = htonl(ip
[2]);
1270 netip6
[3] = htonl(ip
[3]);
1271 #endif /* AF_INET6 */
1273 if (!strcmp(mask
->mask
.name
.name
, "*"))
1277 * Allow Back-to-My-Mac addresses...
1280 if ((ip
[0] & 0xff000000) == 0xfd000000)
1282 #endif /* __APPLE__ */
1285 * Check against all local interfaces...
1290 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
1292 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
1295 * Only check local interfaces...
1298 if (!iface
->is_local
)
1301 if (iface
->address
.addr
.sa_family
== AF_INET
)
1304 * Check IPv4 address...
1307 if ((netip4
& iface
->mask
.ipv4
.sin_addr
.s_addr
) ==
1308 (iface
->address
.ipv4
.sin_addr
.s_addr
&
1309 iface
->mask
.ipv4
.sin_addr
.s_addr
))
1316 * Check IPv6 address...
1319 for (i
= 0; i
< 4; i
++)
1320 if ((netip6
[i
] & iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]) !=
1321 (iface
->address
.ipv6
.sin6_addr
.s6_addr32
[i
] &
1322 iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]))
1328 #endif /* AF_INET6 */
1334 * Check the named interface...
1337 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
1339 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
1341 if (strcmp(mask
->mask
.name
.name
, iface
->name
))
1344 if (iface
->address
.addr
.sa_family
== AF_INET
)
1347 * Check IPv4 address...
1350 if ((netip4
& iface
->mask
.ipv4
.sin_addr
.s_addr
) ==
1351 (iface
->address
.ipv4
.sin_addr
.s_addr
&
1352 iface
->mask
.ipv4
.sin_addr
.s_addr
))
1359 * Check IPv6 address...
1362 for (i
= 0; i
< 4; i
++)
1363 if ((netip6
[i
] & iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]) !=
1364 (iface
->address
.ipv6
.sin6_addr
.s6_addr32
[i
] &
1365 iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]))
1371 #endif /* AF_INET6 */
1376 case CUPSD_AUTH_NAME
:
1378 * Check for exact name match...
1381 if (!_cups_strcasecmp(name
, mask
->mask
.name
.name
))
1385 * Check for domain match...
1388 if (name_len
>= mask
->mask
.name
.length
&&
1389 mask
->mask
.name
.name
[0] == '.' &&
1390 !_cups_strcasecmp(name
+ name_len
- mask
->mask
.name
.length
,
1391 mask
->mask
.name
.name
))
1395 case CUPSD_AUTH_IP
:
1397 * Check for IP/network address match...
1400 for (i
= 0; i
< 4; i
++)
1401 if ((ip
[i
] & mask
->mask
.ip
.netmask
[i
]) !=
1402 mask
->mask
.ip
.address
[i
])
1416 * 'cupsdCheckGroup()' - Check for a user's group membership.
1419 int /* O - 1 if user is a member, 0 otherwise */
1421 const char *username
, /* I - User name */
1422 struct passwd
*user
, /* I - System user info */
1423 const char *groupname
) /* I - Group name */
1425 int i
; /* Looping var */
1426 struct group
*group
; /* System group info */
1427 char junk
[33]; /* MD5 password (not used) */
1428 #ifdef HAVE_MBR_UID_TO_UUID
1429 uuid_t useruuid
, /* UUID for username */
1430 groupuuid
; /* UUID for groupname */
1431 int is_member
; /* True if user is a member of group */
1432 #endif /* HAVE_MBR_UID_TO_UUID */
1435 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1436 "cupsdCheckGroup(username=\"%s\", user=%p, groupname=\"%s\")",
1437 username
, user
, groupname
);
1443 if (!username
|| !groupname
)
1447 * Check to see if the user is a member of the named group...
1450 group
= getgrnam(groupname
);
1456 * Group exists, check it...
1459 for (i
= 0; group
->gr_mem
[i
]; i
++)
1460 if (!_cups_strcasecmp(username
, group
->gr_mem
[i
]))
1465 * Group doesn't exist or user not in group list, check the group ID
1466 * against the user's group ID...
1469 if (user
&& group
&& group
->gr_gid
== user
->pw_gid
)
1472 #ifdef HAVE_MBR_UID_TO_UUID
1474 * Check group membership through MacOS X membership API...
1477 if (user
&& !mbr_uid_to_uuid(user
->pw_uid
, useruuid
))
1482 * Map group name to UUID and check membership...
1485 if (!mbr_gid_to_uuid(group
->gr_gid
, groupuuid
))
1486 if (!mbr_check_membership(useruuid
, groupuuid
, &is_member
))
1490 else if (groupname
[0] == '#')
1493 * Use UUID directly and check for equality (user UUID) and
1494 * membership (group UUID)...
1497 if (!uuid_parse((char *)groupname
+ 1, groupuuid
))
1499 if (!uuid_compare(useruuid
, groupuuid
))
1501 else if (!mbr_check_membership(useruuid
, groupuuid
, &is_member
))
1509 else if (groupname
[0] == '#')
1511 #endif /* HAVE_MBR_UID_TO_UUID */
1514 * Username not found, group not found, or user is not part of the
1515 * system group... Check for a user and group in the MD5 password
1519 if (get_md5_password(username
, groupname
, junk
) != NULL
)
1523 * If we get this far, then the user isn't part of the named group...
1531 * 'cupsdCopyLocation()' - Make a copy of a location...
1534 cupsd_location_t
* /* O - New location */
1536 cupsd_location_t
*loc
) /* I - Original location */
1538 cupsd_location_t
*temp
; /* New location */
1542 * Make a copy of the original location...
1545 if ((temp
= calloc(1, sizeof(cupsd_location_t
))) == NULL
)
1549 * Copy the information from the original location to the new one.
1556 temp
->location
= _cupsStrAlloc(loc
->location
);
1558 temp
->limit
= loc
->limit
;
1559 temp
->order_type
= loc
->order_type
;
1560 temp
->type
= loc
->type
;
1561 temp
->level
= loc
->level
;
1562 temp
->satisfy
= loc
->satisfy
;
1563 temp
->encryption
= loc
->encryption
;
1567 if ((temp
->names
= cupsArrayDup(loc
->names
)) == NULL
)
1569 cupsdLogMessage(CUPSD_LOG_ERROR
,
1570 "Unable to allocate memory for %d names: %s",
1571 cupsArrayCount(loc
->names
), strerror(errno
));
1573 cupsdFreeLocation(temp
);
1581 * Copy allow rules...
1584 if ((temp
->allow
= cupsArrayDup(loc
->allow
)) == NULL
)
1586 cupsdLogMessage(CUPSD_LOG_ERROR
,
1587 "Unable to allocate memory for %d allow rules: %s",
1588 cupsArrayCount(loc
->allow
), strerror(errno
));
1589 cupsdFreeLocation(temp
);
1597 * Copy deny rules...
1600 if ((temp
->deny
= cupsArrayDup(loc
->deny
)) == NULL
)
1602 cupsdLogMessage(CUPSD_LOG_ERROR
,
1603 "Unable to allocate memory for %d deny rules: %s",
1604 cupsArrayCount(loc
->deny
), strerror(errno
));
1605 cupsdFreeLocation(temp
);
1615 * 'cupsdDeleteAllLocations()' - Free all memory used for location authorization.
1619 cupsdDeleteAllLocations(void)
1622 * Free the location array, which will free all of the locations...
1625 cupsArrayDelete(Locations
);
1631 * 'cupsdFindBest()' - Find the location entry that best matches the resource.
1634 cupsd_location_t
* /* O - Location that matches */
1635 cupsdFindBest(const char *path
, /* I - Resource path */
1636 http_state_t state
) /* I - HTTP state/request */
1638 char uri
[HTTP_MAX_URI
],
1639 /* URI in request... */
1640 *uriptr
; /* Pointer into URI */
1641 cupsd_location_t
*loc
, /* Current location */
1642 *best
; /* Best match for location so far */
1643 int bestlen
; /* Length of best match */
1644 int limit
; /* Limit field */
1645 static const int limits
[] = /* Map http_status_t to CUPSD_AUTH_LIMIT_xyz */
1647 CUPSD_AUTH_LIMIT_ALL
,
1648 CUPSD_AUTH_LIMIT_OPTIONS
,
1649 CUPSD_AUTH_LIMIT_GET
,
1650 CUPSD_AUTH_LIMIT_GET
,
1651 CUPSD_AUTH_LIMIT_HEAD
,
1652 CUPSD_AUTH_LIMIT_POST
,
1653 CUPSD_AUTH_LIMIT_POST
,
1654 CUPSD_AUTH_LIMIT_POST
,
1655 CUPSD_AUTH_LIMIT_PUT
,
1656 CUPSD_AUTH_LIMIT_PUT
,
1657 CUPSD_AUTH_LIMIT_DELETE
,
1658 CUPSD_AUTH_LIMIT_TRACE
,
1659 CUPSD_AUTH_LIMIT_ALL
,
1660 CUPSD_AUTH_LIMIT_ALL
1665 * First copy the connection URI to a local string so we have drop
1666 * any .ppd extension from the pathname in /printers or /classes
1670 strlcpy(uri
, path
, sizeof(uri
));
1672 if (!strncmp(uri
, "/printers/", 10) ||
1673 !strncmp(uri
, "/classes/", 9))
1676 * Check if the URI has .ppd on the end...
1679 uriptr
= uri
+ strlen(uri
) - 4; /* len > 4 if we get here... */
1681 if (!strcmp(uriptr
, ".ppd"))
1685 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: uri = \"%s\"...", uri
);
1688 * Loop through the list of locations to find a match...
1691 limit
= limits
[state
];
1695 for (loc
= (cupsd_location_t
*)cupsArrayFirst(Locations
);
1697 loc
= (cupsd_location_t
*)cupsArrayNext(Locations
))
1699 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: Location %s Limit %x",
1700 loc
->location
? loc
->location
: "nil", loc
->limit
);
1702 if (!strncmp(uri
, "/printers/", 10) || !strncmp(uri
, "/classes/", 9))
1705 * Use case-insensitive comparison for queue names...
1708 if (loc
->length
> bestlen
&& loc
->location
&&
1709 !_cups_strncasecmp(uri
, loc
->location
, loc
->length
) &&
1710 loc
->location
[0] == '/' &&
1711 (limit
& loc
->limit
) != 0)
1714 bestlen
= loc
->length
;
1720 * Use case-sensitive comparison for other URIs...
1723 if (loc
->length
> bestlen
&& loc
->location
&&
1724 !strncmp(uri
, loc
->location
, loc
->length
) &&
1725 loc
->location
[0] == '/' &&
1726 (limit
& loc
->limit
) != 0)
1729 bestlen
= loc
->length
;
1735 * Return the match, if any...
1738 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: best = %s",
1739 best
? best
->location
: "NONE");
1746 * 'cupsdFindLocation()' - Find the named location.
1749 cupsd_location_t
* /* O - Location that matches */
1750 cupsdFindLocation(const char *location
) /* I - Connection */
1752 cupsd_location_t key
; /* Search key */
1755 key
.location
= (char *)location
;
1757 return ((cupsd_location_t
*)cupsArrayFind(Locations
, &key
));
1762 * 'cupsdFreeLocation()' - Free all memory used by a location.
1766 cupsdFreeLocation(cupsd_location_t
*loc
)/* I - Location to free */
1768 cupsArrayDelete(loc
->names
);
1769 cupsArrayDelete(loc
->allow
);
1770 cupsArrayDelete(loc
->deny
);
1772 _cupsStrFree(loc
->location
);
1778 * 'cupsdIsAuthorized()' - Check to see if the user is authorized...
1781 http_status_t
/* O - HTTP_OK if authorized or error code */
1782 cupsdIsAuthorized(cupsd_client_t
*con
, /* I - Connection */
1783 const char *owner
)/* I - Owner of object */
1785 int i
, /* Looping vars */
1786 auth
, /* Authorization status */
1787 type
; /* Type of authentication */
1788 http_addr_t
*hostaddr
= httpGetAddress(con
->http
);
1789 /* Client address */
1790 const char *hostname
= httpGetHostname(con
->http
, NULL
, 0);
1791 /* Client hostname */
1792 unsigned address
[4]; /* Authorization address */
1793 cupsd_location_t
*best
; /* Best match for location so far */
1794 int hostlen
; /* Length of hostname */
1795 char *name
, /* Current username */
1796 username
[256], /* Username to authorize */
1797 ownername
[256], /* Owner name to authorize */
1798 *ptr
; /* Pointer into username */
1799 struct passwd
*pw
; /* User password data */
1800 static const char * const levels
[] = /* Auth levels */
1806 static const char * const types
[] = /* Auth types */
1816 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1817 "cupsdIsAuthorized: con->uri=\"%s\", con->best=%p(%s)",
1818 con
->uri
, con
->best
, con
->best
? con
->best
->location
?
1819 con
->best
->location
: "(null)" : "");
1821 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1822 "cupsdIsAuthorized: owner=\"%s\"", owner
);
1825 * If there is no "best" authentication rule for this request, then
1826 * access is allowed from the local system and denied from other
1832 if (httpAddrLocalhost(httpGetAddress(con
->http
)) ||
1833 !strcmp(hostname
, ServerName
) ||
1834 cupsArrayFind(ServerAlias
, (void *)hostname
))
1837 return (HTTP_FORBIDDEN
);
1842 if ((type
= best
->type
) == CUPSD_AUTH_DEFAULT
)
1843 type
= cupsdDefaultAuthType();
1845 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1846 "cupsdIsAuthorized: level=CUPSD_AUTH_%s, type=%s, "
1847 "satisfy=CUPSD_AUTH_SATISFY_%s, num_names=%d",
1848 levels
[best
->level
], types
[type
],
1849 best
->satisfy
? "ANY" : "ALL", cupsArrayCount(best
->names
));
1851 if (best
->limit
== CUPSD_AUTH_LIMIT_IPP
)
1852 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: op=%x(%s)",
1853 best
->op
, ippOpString(best
->op
));
1856 * Check host/ip-based accesses...
1860 if (httpAddrFamily(hostaddr
) == AF_INET6
)
1863 * Copy IPv6 address...
1866 address
[0] = ntohl(hostaddr
->ipv6
.sin6_addr
.s6_addr32
[0]);
1867 address
[1] = ntohl(hostaddr
->ipv6
.sin6_addr
.s6_addr32
[1]);
1868 address
[2] = ntohl(hostaddr
->ipv6
.sin6_addr
.s6_addr32
[2]);
1869 address
[3] = ntohl(hostaddr
->ipv6
.sin6_addr
.s6_addr32
[3]);
1872 #endif /* AF_INET6 */
1873 if (con
->http
->hostaddr
->addr
.sa_family
== AF_INET
)
1876 * Copy IPv4 address...
1882 address
[3] = ntohl(hostaddr
->ipv4
.sin_addr
.s_addr
);
1885 memset(address
, 0, sizeof(address
));
1887 hostlen
= strlen(hostname
);
1889 auth
= cupsdCheckAccess(address
, hostname
, hostlen
, best
)
1890 ? CUPSD_AUTH_ALLOW
: CUPSD_AUTH_DENY
;
1892 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: auth=CUPSD_AUTH_%s...",
1893 auth
? "DENY" : "ALLOW");
1895 if (auth
== CUPSD_AUTH_DENY
&& best
->satisfy
== CUPSD_AUTH_SATISFY_ALL
)
1896 return (HTTP_FORBIDDEN
);
1900 * See if encryption is required...
1903 if ((best
->encryption
>= HTTP_ENCRYPT_REQUIRED
&& !con
->http
->tls
&&
1904 _cups_strcasecmp(hostname
, "localhost") &&
1905 !httpAddrLocalhost(hostaddr
) &&
1906 best
->satisfy
== CUPSD_AUTH_SATISFY_ALL
) &&
1907 !(type
== CUPSD_AUTH_NEGOTIATE
||
1908 (type
== CUPSD_AUTH_NONE
&&
1909 cupsdDefaultAuthType() == CUPSD_AUTH_NEGOTIATE
)))
1911 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1912 "cupsdIsAuthorized: Need upgrade to TLS...");
1913 return (HTTP_UPGRADE_REQUIRED
);
1915 #endif /* HAVE_SSL */
1918 * Now see what access level is required...
1921 if (best
->level
== CUPSD_AUTH_ANON
|| /* Anonymous access - allow it */
1922 (type
== CUPSD_AUTH_NONE
&& cupsArrayCount(best
->names
) == 0))
1925 if (!con
->username
[0] && type
== CUPSD_AUTH_NONE
&&
1926 best
->limit
== CUPSD_AUTH_LIMIT_IPP
)
1929 * Check for unauthenticated username...
1932 ipp_attribute_t
*attr
; /* requesting-user-name attribute */
1935 attr
= ippFindAttribute(con
->request
, "requesting-user-name", IPP_TAG_NAME
);
1938 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1939 "cupsdIsAuthorized: requesting-user-name=\"%s\"",
1940 attr
->values
[0].string
.text
);
1941 strlcpy(username
, attr
->values
[0].string
.text
, sizeof(username
));
1943 else if (best
->satisfy
== CUPSD_AUTH_SATISFY_ALL
|| auth
== CUPSD_AUTH_DENY
)
1944 return (HTTP_UNAUTHORIZED
); /* Non-anonymous needs user/pass */
1946 return (HTTP_OK
); /* unless overridden with Satisfy */
1950 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdIsAuthorized: username=\"%s\"",
1953 #ifdef HAVE_AUTHORIZATION_H
1954 if (!con
->username
[0] && !con
->authref
)
1956 if (!con
->username
[0])
1957 #endif /* HAVE_AUTHORIZATION_H */
1959 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 if (con
->type
!= type
&& type
!= CUPSD_AUTH_NONE
&&
1968 (type
!= CUPSD_AUTH_NEGOTIATE
|| con
->gss_uid
<= 0) &&
1969 #endif /* HAVE_GSSAPI */
1970 (con
->type
!= CUPSD_AUTH_BASIC
|| type
!= CUPSD_AUTH_BASICDIGEST
))
1972 cupsdLogMessage(CUPSD_LOG_ERROR
, "Authorized using %s, expected %s!",
1973 types
[con
->type
], types
[type
]);
1975 return (HTTP_UNAUTHORIZED
);
1978 strlcpy(username
, con
->username
, sizeof(username
));
1982 * OK, got a username. See if we need normal user access, or group
1983 * access... (root always matches)
1986 if (!strcmp(username
, "root"))
1990 * Strip any @domain or @KDC from the username and owner...
1993 if ((ptr
= strchr(username
, '@')) != NULL
)
1998 strlcpy(ownername
, owner
, sizeof(ownername
));
2000 if ((ptr
= strchr(ownername
, '@')) != NULL
)
2004 ownername
[0] = '\0';
2007 * Get the user info...
2012 pw
= getpwnam(username
);
2018 if (best
->level
== CUPSD_AUTH_USER
)
2021 * If there are no names associated with this location, then
2022 * any valid user is OK...
2025 if (cupsArrayCount(best
->names
) == 0)
2029 * Otherwise check the user list and return OK if this user is
2033 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2034 "cupsdIsAuthorized: Checking user membership...");
2036 #ifdef HAVE_AUTHORIZATION_H
2038 * If an authorization reference was supplied it must match a right name...
2043 for (name
= (char *)cupsArrayFirst(best
->names
);
2045 name
= (char *)cupsArrayNext(best
->names
))
2047 if (!_cups_strncasecmp(name
, "@AUTHKEY(", 9) && check_authref(con
, name
+ 9))
2049 else if (!_cups_strcasecmp(name
, "@SYSTEM") && SystemGroupAuthKey
&&
2050 check_authref(con
, SystemGroupAuthKey
))
2054 return (HTTP_FORBIDDEN
);
2056 #endif /* HAVE_AUTHORIZATION_H */
2058 for (name
= (char *)cupsArrayFirst(best
->names
);
2060 name
= (char *)cupsArrayNext(best
->names
))
2062 if (!_cups_strcasecmp(name
, "@OWNER") && owner
&&
2063 !_cups_strcasecmp(username
, ownername
))
2065 else if (!_cups_strcasecmp(name
, "@SYSTEM"))
2067 for (i
= 0; i
< NumSystemGroups
; i
++)
2068 if (cupsdCheckGroup(username
, pw
, SystemGroups
[i
]))
2071 else if (name
[0] == '@')
2073 if (cupsdCheckGroup(username
, pw
, name
+ 1))
2076 else if (!_cups_strcasecmp(username
, name
))
2080 return (con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
);
2084 * Check to see if this user is in any of the named groups...
2087 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2088 "cupsdIsAuthorized: Checking group membership...");
2091 * Check to see if this user is in any of the named groups...
2094 for (name
= (char *)cupsArrayFirst(best
->names
);
2096 name
= (char *)cupsArrayNext(best
->names
))
2098 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2099 "cupsdIsAuthorized: Checking group \"%s\" membership...",
2102 if (!_cups_strcasecmp(name
, "@SYSTEM"))
2104 for (i
= 0; i
< NumSystemGroups
; i
++)
2105 if (cupsdCheckGroup(username
, pw
, SystemGroups
[i
]))
2108 else if (cupsdCheckGroup(username
, pw
, name
))
2113 * The user isn't part of the specified group, so deny access...
2116 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2117 "cupsdIsAuthorized: User not in group(s)!");
2119 return (con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
);
2124 * 'cupsdNewLocation()' - Create a new location for authorization.
2126 * Note: Still need to call cupsdAddLocation() to add it to the list of global
2130 cupsd_location_t
* /* O - Pointer to new location record */
2131 cupsdNewLocation(const char *location
) /* I - Location path */
2133 cupsd_location_t
*temp
; /* New location */
2137 * Try to allocate memory for the new location.
2140 if ((temp
= calloc(1, sizeof(cupsd_location_t
))) == NULL
)
2144 * Initialize the record and copy the name over...
2147 if ((temp
->location
= _cupsStrAlloc(location
)) == NULL
)
2153 temp
->length
= strlen(temp
->location
);
2156 * Return the new record...
2163 #ifdef HAVE_AUTHORIZATION_H
2165 * 'check_authref()' - Check if an authorization services reference has the
2169 static int /* O - 1 if right is valid, 0 otherwise */
2170 check_authref(cupsd_client_t
*con
, /* I - Connection */
2171 const char *right
) /* I - Right name */
2173 OSStatus status
; /* OS Status */
2174 AuthorizationItem authright
; /* Authorization right */
2175 AuthorizationRights authrights
; /* Authorization rights */
2176 AuthorizationFlags authflags
; /* Authorization flags */
2180 * Check to see if the user is allowed to perform the task...
2186 authright
.name
= right
;
2187 authright
.valueLength
= 0;
2188 authright
.value
= NULL
;
2189 authright
.flags
= 0;
2191 authrights
.count
= 1;
2192 authrights
.items
= &authright
;
2194 authflags
= kAuthorizationFlagDefaults
|
2195 kAuthorizationFlagExtendRights
;
2197 if ((status
= AuthorizationCopyRights(con
->authref
, &authrights
,
2198 kAuthorizationEmptyEnvironment
,
2199 authflags
, NULL
)) != 0)
2201 cupsdLogMessage(CUPSD_LOG_ERROR
,
2202 "AuthorizationCopyRights(\"%s\") returned %d (%s)",
2203 authright
.name
, (int)status
, cssmErrorString(status
));
2207 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2208 "AuthorizationCopyRights(\"%s\") succeeded!",
2213 #endif /* HAVE_AUTHORIZATION_H */
2217 * 'compare_locations()' - Compare two locations.
2220 static int /* O - Result of comparison */
2221 compare_locations(cupsd_location_t
*a
, /* I - First location */
2222 cupsd_location_t
*b
) /* I - Second location */
2224 return (strcmp(b
->location
, a
->location
));
2229 * 'copy_authmask()' - Copy function for auth masks.
2232 static cupsd_authmask_t
* /* O - New auth mask */
2233 copy_authmask(cupsd_authmask_t
*mask
, /* I - Existing auth mask */
2234 void *data
) /* I - User data (unused) */
2236 cupsd_authmask_t
*temp
; /* New auth mask */
2241 if ((temp
= malloc(sizeof(cupsd_authmask_t
))) != NULL
)
2243 memcpy(temp
, mask
, sizeof(cupsd_authmask_t
));
2245 if (temp
->type
== CUPSD_AUTH_NAME
|| temp
->type
== CUPSD_AUTH_INTERFACE
)
2248 * Make a copy of the name...
2251 if ((temp
->mask
.name
.name
= _cupsStrAlloc(temp
->mask
.name
.name
)) == NULL
)
2254 * Failed to make copy...
2267 #if !HAVE_LIBPAM && !defined(HAVE_USERSEC_H)
2269 * 'cups_crypt()' - Encrypt the password using the DES or MD5 algorithms,
2273 static char * /* O - Encrypted password */
2274 cups_crypt(const char *pw
, /* I - Password string */
2275 const char *salt
) /* I - Salt (key) string */
2277 if (!strncmp(salt
, "$1$", 3))
2280 * Use MD5 passwords without the benefit of PAM; this is for
2281 * Slackware Linux, and the algorithm was taken from the
2282 * old shadow-19990827/lib/md5crypt.c source code... :(
2285 int i
; /* Looping var */
2286 unsigned long n
; /* Output number */
2287 int pwlen
; /* Length of password string */
2288 const char *salt_end
; /* End of "salt" data for MD5 */
2289 char *ptr
; /* Pointer into result string */
2290 _cups_md5_state_t state
; /* Primary MD5 state info */
2291 _cups_md5_state_t state2
; /* Secondary MD5 state info */
2292 unsigned char digest
[16]; /* MD5 digest result */
2293 static char result
[120]; /* Final password string */
2297 * Get the salt data between dollar signs, e.g. $1$saltdata$md5.
2298 * Get a maximum of 8 characters of salt data after $1$...
2301 for (salt_end
= salt
+ 3; *salt_end
&& (salt_end
- salt
) < 11; salt_end
++)
2302 if (*salt_end
== '$')
2306 * Compute the MD5 sum we need...
2311 _cupsMD5Init(&state
);
2312 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
2313 _cupsMD5Append(&state
, (unsigned char *)salt
, salt_end
- salt
);
2315 _cupsMD5Init(&state2
);
2316 _cupsMD5Append(&state2
, (unsigned char *)pw
, pwlen
);
2317 _cupsMD5Append(&state2
, (unsigned char *)salt
+ 3, salt_end
- salt
- 3);
2318 _cupsMD5Append(&state2
, (unsigned char *)pw
, pwlen
);
2319 _cupsMD5Finish(&state2
, digest
);
2321 for (i
= pwlen
; i
> 0; i
-= 16)
2322 _cupsMD5Append(&state
, digest
, i
> 16 ? 16 : i
);
2324 for (i
= pwlen
; i
> 0; i
>>= 1)
2325 _cupsMD5Append(&state
, (unsigned char *)((i
& 1) ? "" : pw
), 1);
2327 _cupsMD5Finish(&state
, digest
);
2329 for (i
= 0; i
< 1000; i
++)
2331 _cupsMD5Init(&state
);
2334 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
2336 _cupsMD5Append(&state
, digest
, 16);
2339 _cupsMD5Append(&state
, (unsigned char *)salt
+ 3, salt_end
- salt
- 3);
2342 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
2345 _cupsMD5Append(&state
, digest
, 16);
2347 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
2349 _cupsMD5Finish(&state
, digest
);
2353 * Copy the final sum to the result string and return...
2356 memcpy(result
, salt
, salt_end
- salt
);
2357 ptr
= result
+ (salt_end
- salt
);
2360 for (i
= 0; i
< 5; i
++, ptr
+= 4)
2362 n
= (((digest
[i
] << 8) | digest
[i
+ 6]) << 8);
2365 n
|= digest
[i
+ 12];
2372 to64(ptr
, digest
[11], 2);
2381 * Use the standard crypt() function...
2384 return (crypt(pw
, salt
));
2387 #endif /* !HAVE_LIBPAM && !HAVE_USERSEC_H */
2391 * 'free_authmask()' - Free function for auth masks.
2395 free_authmask(cupsd_authmask_t
*mask
, /* I - Auth mask to free */
2396 void *data
) /* I - User data (unused) */
2400 if (mask
->type
== CUPSD_AUTH_NAME
|| mask
->type
== CUPSD_AUTH_INTERFACE
)
2401 _cupsStrFree(mask
->mask
.name
.name
);
2408 * 'get_md5_password()' - Get an MD5 password.
2411 static char * /* O - MD5 password string */
2412 get_md5_password(const char *username
, /* I - Username */
2413 const char *group
, /* I - Group */
2414 char passwd
[33]) /* O - MD5 password string */
2416 cups_file_t
*fp
; /* passwd.md5 file */
2417 char filename
[1024], /* passwd.md5 filename */
2418 line
[256], /* Line from file */
2419 tempuser
[33], /* User from file */
2420 tempgroup
[33]; /* Group from file */
2423 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2424 "get_md5_password(username=\"%s\", group=\"%s\", passwd=%p)",
2425 username
, group
? group
: "(null)", passwd
);
2427 snprintf(filename
, sizeof(filename
), "%s/passwd.md5", ServerRoot
);
2428 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
2430 if (errno
!= ENOENT
)
2431 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open %s - %s", filename
,
2437 while (cupsFileGets(fp
, line
, sizeof(line
)) != NULL
)
2439 if (sscanf(line
, "%32[^:]:%32[^:]:%32s", tempuser
, tempgroup
, passwd
) != 3)
2441 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad MD5 password line: %s", line
);
2445 if (!strcmp(username
, tempuser
) &&
2446 (group
== NULL
|| !strcmp(group
, tempgroup
)))
2449 * Found the password entry!
2452 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Found MD5 user %s, group %s...",
2453 username
, tempgroup
);
2461 * Didn't find a password entry - return NULL!
2471 * 'pam_func()' - PAM conversation function.
2474 static int /* O - Success or failure */
2476 int num_msg
, /* I - Number of messages */
2477 const struct pam_message
**msg
, /* I - Messages */
2478 struct pam_response
**resp
, /* O - Responses */
2480 /* I - Pointer to connection */
2482 int i
; /* Looping var */
2483 struct pam_response
*replies
; /* Replies */
2484 cupsd_authdata_t
*data
; /* Pointer to auth data */
2488 * Allocate memory for the responses...
2491 if ((replies
= malloc(sizeof(struct pam_response
) * num_msg
)) == NULL
)
2492 return (PAM_CONV_ERR
);
2495 * Answer all of the messages...
2498 DEBUG_printf(("pam_func: appdata_ptr = %p\n", appdata_ptr
));
2502 * Apparently some versions of HP-UX 11 have a broken pam_unix security
2503 * module. This is a workaround...
2509 data
= (cupsd_authdata_t
*)appdata_ptr
;
2512 for (i
= 0; i
< num_msg
; i
++)
2514 DEBUG_printf(("pam_func: Message = \"%s\"\n", msg
[i
]->msg
));
2516 switch (msg
[i
]->msg_style
)
2518 case PAM_PROMPT_ECHO_ON
:
2519 DEBUG_printf(("pam_func: PAM_PROMPT_ECHO_ON, returning \"%s\"...\n",
2521 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2522 replies
[i
].resp
= strdup(data
->username
);
2525 case PAM_PROMPT_ECHO_OFF
:
2526 DEBUG_printf(("pam_func: PAM_PROMPT_ECHO_OFF, returning \"%s\"...\n",
2528 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2529 replies
[i
].resp
= strdup(data
->password
);
2533 DEBUG_puts("pam_func: PAM_TEXT_INFO...");
2534 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2535 replies
[i
].resp
= NULL
;
2539 DEBUG_puts("pam_func: PAM_ERROR_MSG...");
2540 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2541 replies
[i
].resp
= NULL
;
2545 DEBUG_printf(("pam_func: Unknown PAM message %d...\n",
2546 msg
[i
]->msg_style
));
2548 return (PAM_CONV_ERR
);
2553 * Return the responses back to PAM...
2558 return (PAM_SUCCESS
);
2560 #elif !defined(HAVE_USERSEC_H)
2564 * 'to64()' - Base64-encode an integer value...
2568 to64(char *s
, /* O - Output string */
2569 unsigned long v
, /* I - Value to encode */
2570 int n
) /* I - Number of digits */
2572 const char *itoa64
= "./0123456789"
2573 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2574 "abcdefghijklmnopqrstuvwxyz";
2577 for (; n
> 0; n
--, v
>>= 6)
2578 *s
++ = itoa64
[v
& 0x3f];
2580 #endif /* HAVE_LIBPAM */