2 * "$Id: auth.c 6947 2007-09-12 21:09:49Z mike $"
4 * Authorization routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 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 * cupsdAddLocation() - Add a location for authorization.
21 * cupsdAddName() - Add a name to a location...
22 * cupsdAllowHost() - Add a host name that is allowed to access the
24 * cupsdAllowIP() - Add an IP address or network that is allowed
25 * to access the location.
26 * cupsdAuthorize() - Validate any authorization credentials.
27 * cupsdCheckAuth() - Check authorization masks.
28 * cupsdCheckGroup() - Check for a user's group membership.
29 * cupsdCopyLocation() - Make a copy of a location...
30 * cupsdDeleteAllLocations() - Free all memory used for location
32 * cupsdDeleteLocation() - Free all memory used by a location.
33 * cupsdDenyHost() - Add a host name that is not allowed to access
35 * cupsdDenyIP() - Add an IP address or network that is not
36 * allowed to access the location.
37 * cupsdFindBest() - Find the location entry that best matches the
39 * cupsdFindLocation() - Find the named location.
40 * cupsdIsAuthorized() - Check to see if the user is authorized...
41 * add_allow() - Add an allow mask to the location.
42 * add_deny() - Add a deny mask to the location.
43 * compare_locations() - Compare two locations.
44 * cups_crypt() - Encrypt the password using the DES or MD5
45 * algorithms, as needed.
46 * get_gss_creds() - Obtain GSS credentials.
47 * get_md5_password() - Get an MD5 password.
48 * pam_func() - PAM conversation function.
49 * to64() - Base64-encode an integer value...
50 * check_authref() - Check an authorization services reference.
54 * Include necessary headers...
61 #endif /* HAVE_SHADOW_H */
64 #endif /* HAVE_CRYPT_H */
66 # ifdef HAVE_PAM_PAM_APPL_H
67 # include <pam/pam_appl.h>
69 # include <security/pam_appl.h>
70 # endif /* HAVE_PAM_PAM_APPL_H */
71 #endif /* HAVE_LIBPAM */
74 #endif /* HAVE_USERSEC_H */
75 #ifdef HAVE_MEMBERSHIP_H
76 # include <membership.h>
77 #endif /* HAVE_MEMBERSHIP_H */
78 #ifdef HAVE_AUTHORIZATION_H
79 # include <Security/AuthorizationTags.h>
80 # ifdef HAVE_SECBASEPRIV_H
81 # include <Security/SecBasePriv.h>
83 extern const char *cssmErrorString(int error
);
84 # endif /* HAVE_SECBASEPRIV_H */
85 #endif /* HAVE_AUTHORIZATION_H */
86 #ifdef HAVE_SYS_PARAM_H
87 # include <sys/param.h>
88 #endif /* HAVE_SYS_PARAM_H */
89 #ifdef HAVE_SYS_UCRED_H
90 # include <sys/ucred.h>
91 typedef struct xucred cupsd_ucred_t
;
92 # define CUPSD_UCRED_UID(c) (c).cr_uid
94 typedef struct ucred cupsd_ucred_t
;
95 # define CUPSD_UCRED_UID(c) (c).uid
96 #endif /* HAVE_SYS_UCRED_H */
103 static cupsd_authmask_t
*add_allow(cupsd_location_t
*loc
);
104 static cupsd_authmask_t
*add_deny(cupsd_location_t
*loc
);
105 #ifdef HAVE_AUTHORIZATION_H
106 static int check_authref(cupsd_client_t
*con
, const char *right
);
107 #endif /* HAVE_AUTHORIZATION_H */
108 static int compare_locations(cupsd_location_t
*a
,
109 cupsd_location_t
*b
);
110 #if !HAVE_LIBPAM && !defined(HAVE_USERSEC_H)
111 static char *cups_crypt(const char *pw
, const char *salt
);
112 #endif /* !HAVE_LIBPAM && !HAVE_USERSEC_H */
114 static gss_cred_id_t
get_gss_creds(const char *service_name
);
115 #endif /* HAVE_GSSAPI */
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
[33], /* Username string */
134 password
[33]; /* 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 * 'cupsdAddLocation()' - Add a location for authorization.
152 cupsd_location_t
* /* O - Pointer to new location record */
153 cupsdAddLocation(const char *location
) /* I - Location path */
155 cupsd_location_t
*temp
; /* New location */
159 * Make sure the locations array is created...
163 Locations
= cupsArrayNew((cups_array_func_t
)compare_locations
, NULL
);
169 * Try to allocate memory for the new location.
172 if ((temp
= calloc(1, sizeof(cupsd_location_t
))) == NULL
)
176 * Initialize the record and copy the name over...
179 temp
->location
= strdup(location
);
180 temp
->length
= strlen(temp
->location
);
182 cupsArrayAdd(Locations
, temp
);
184 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAddLocation: added location \'%s\'",
188 * Return the new record...
196 * 'cupsdAddName()' - Add a name to a location...
200 cupsdAddName(cupsd_location_t
*loc
, /* I - Location to add to */
201 char *name
) /* I - Name to add */
203 char **temp
; /* Pointer to names array */
206 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAddName(loc=%p, name=\"%s\")",
209 if (loc
->num_names
== 0)
210 temp
= malloc(sizeof(char *));
212 temp
= realloc(loc
->names
, (loc
->num_names
+ 1) * sizeof(char *));
216 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to add name to location %s: %s",
217 loc
->location
? loc
->location
: "nil", strerror(errno
));
223 if ((temp
[loc
->num_names
] = strdup(name
)) == NULL
)
225 cupsdLogMessage(CUPSD_LOG_ERROR
,
226 "Unable to duplicate name for location %s: %s",
227 loc
->location
? loc
->location
: "nil", strerror(errno
));
236 * 'cupsdAllowHost()' - Add a host name that is allowed to access the location.
240 cupsdAllowHost(cupsd_location_t
*loc
, /* I - Location to add to */
241 char *name
) /* I - Name of host or domain to add */
243 cupsd_authmask_t
*temp
; /* New host/domain mask */
244 char ifname
[32], /* Interface name */
245 *ifptr
; /* Pointer to end of name */
248 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAllowHost(loc=%p(%s), name=\"%s\")",
249 loc
, loc
->location
? loc
->location
: "nil", name
);
251 if ((temp
= add_allow(loc
)) == NULL
)
254 if (!strcasecmp(name
, "@LOCAL"))
257 * Allow *interface*...
260 temp
->type
= AUTH_INTERFACE
;
261 temp
->mask
.name
.name
= strdup("*");
262 temp
->mask
.name
.length
= 1;
264 else if (!strncasecmp(name
, "@IF(", 4))
267 * Allow *interface*...
270 strlcpy(ifname
, name
+ 4, sizeof(ifname
));
272 ifptr
= ifname
+ strlen(ifname
);
274 if (ifptr
[-1] == ')')
280 temp
->type
= AUTH_INTERFACE
;
281 temp
->mask
.name
.name
= strdup(ifname
);
282 temp
->mask
.name
.length
= ifptr
- ifname
;
290 temp
->type
= AUTH_NAME
;
291 temp
->mask
.name
.name
= strdup(name
);
292 temp
->mask
.name
.length
= strlen(name
);
298 * 'cupsdAllowIP()' - Add an IP address or network that is allowed to access
303 cupsdAllowIP(cupsd_location_t
*loc
, /* I - Location to add to */
304 unsigned address
[4], /* I - IP address to add */
305 unsigned netmask
[4]) /* I - Netmask of address */
307 cupsd_authmask_t
*temp
; /* New host/domain mask */
310 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
311 "cupsdAllowIP(loc=%p(%s), address=%x:%x:%x:%x, netmask=%x:%x:%x:%x)",
312 loc
, loc
->location
? loc
->location
: "nil",
313 address
[0], address
[1], address
[2], address
[3],
314 netmask
[0], netmask
[1], netmask
[2], netmask
[3]);
316 if ((temp
= add_allow(loc
)) == NULL
)
319 temp
->type
= AUTH_IP
;
320 memcpy(temp
->mask
.ip
.address
, address
, sizeof(temp
->mask
.ip
.address
));
321 memcpy(temp
->mask
.ip
.netmask
, netmask
, sizeof(temp
->mask
.ip
.netmask
));
326 * 'cupsdAuthorize()' - Validate any authorization credentials.
330 cupsdAuthorize(cupsd_client_t
*con
) /* I - Client connection */
332 int type
; /* Authentication type */
333 const char *authorization
; /* Pointer into Authorization string */
334 char *ptr
, /* Pointer into string */
335 username
[256], /* Username string */
336 password
[33]; /* Password string */
337 const char *localuser
; /* Certificate username */
338 char nonce
[HTTP_MAX_VALUE
], /* Nonce value from client */
339 md5
[33], /* MD5 password */
340 basicmd5
[33]; /* MD5 of Basic password */
341 static const char * const states
[] = /* HTTP client states... */
361 * Locate the best matching location so we know what kind of
362 * authentication to expect...
365 con
->best
= cupsdFindBest(con
->uri
, con
->http
.state
);
366 con
->type
= AUTH_NONE
;
368 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
369 "cupsdAuthorize: con->uri=\"%s\", con->best=%p(%s)",
370 con
->uri
, con
->best
, con
->best
? con
->best
->location
: "");
372 if (con
->best
&& con
->best
->type
!= AUTH_NONE
)
374 if (con
->best
->type
== AUTH_DEFAULT
)
375 type
= DefaultAuthType
;
377 type
= con
->best
->type
;
380 type
= DefaultAuthType
;
383 * Decode the Authorization string...
386 authorization
= httpGetField(&con
->http
, HTTP_FIELD_AUTHORIZATION
);
388 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAuthorize: Authorization=\"%s\"",
394 #ifdef HAVE_AUTHORIZATION_H
397 AuthorizationFree(con
->authref
, kAuthorizationFlagDefaults
);
400 #endif /* HAVE_AUTHORIZATION_H */
405 * No authorization data provided, return early...
408 cupsdLogMessage(CUPSD_LOG_DEBUG
,
409 "cupsdAuthorize: No authentication data provided.");
412 #ifdef HAVE_AUTHORIZATION_H
413 else if (!strncmp(authorization
, "AuthRef", 6) &&
414 !strcasecmp(con
->http
.hostname
, "localhost"))
416 OSStatus status
; /* Status */
417 int authlen
; /* Auth string length */
418 AuthorizationItemSet
*authinfo
; /* Authorization item set */
421 * Get the Authorization Services data...
425 while (isspace(*authorization
& 255))
428 authlen
= sizeof(nonce
);
429 httpDecode64_2(nonce
, &authlen
, authorization
);
431 if (authlen
!= kAuthorizationExternalFormLength
)
433 cupsdLogMessage(CUPSD_LOG_ERROR
,
434 "External Authorization reference size is incorrect!");
438 if ((status
= AuthorizationCreateFromExternalForm(
439 (AuthorizationExternalForm
*)nonce
, &con
->authref
)) != 0)
441 cupsdLogMessage(CUPSD_LOG_ERROR
,
442 "AuthorizationCreateFromExternalForm returned %d (%s)",
443 (int)status
, cssmErrorString(status
));
447 if ((status
= AuthorizationCopyInfo(con
->authref
,
448 kAuthorizationEnvironmentUsername
,
451 cupsdLogMessage(CUPSD_LOG_ERROR
,
452 "AuthorizationCopyInfo returned %d (%s)",
453 (int)status
, cssmErrorString(status
));
457 if (authinfo
->count
== 1)
458 strlcpy(username
, authinfo
->items
[0].value
, sizeof(username
));
460 cupsdLogMessage(CUPSD_LOG_DEBUG
,
461 "cupsdAuthorize: Authorized as %s using AuthRef",
464 AuthorizationFreeItemSet(authinfo
);
466 con
->type
= AUTH_BASIC
;
468 #endif /* HAVE_AUTHORIZATION_H */
469 #if defined(SO_PEERCRED) && defined(AF_LOCAL)
470 else if (!strncmp(authorization
, "PeerCred ", 9) &&
471 con
->http
.hostaddr
->addr
.sa_family
== AF_LOCAL
)
474 * Use peer credentials from domain socket connection...
477 struct passwd
*pwd
; /* Password entry for this user */
478 cupsd_ucred_t peercred
; /* Peer credentials */
479 socklen_t peersize
; /* Size of peer credentials */
482 if ((pwd
= getpwnam(authorization
+ 9)) == NULL
)
484 cupsdLogMessage(CUPSD_LOG_ERROR
, "User \"%s\" does not exist!",
489 peersize
= sizeof(peercred
);
491 if (getsockopt(con
->http
.fd
, SOL_SOCKET
, SO_PEERCRED
, &peercred
, &peersize
))
493 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to get peer credentials - %s",
498 if (pwd
->pw_uid
!= CUPSD_UCRED_UID(peercred
))
500 cupsdLogMessage(CUPSD_LOG_ERROR
,
501 "Invalid peer credentials for \"%s\" - got %d, "
502 "expected %d!", authorization
+ 9,
503 CUPSD_UCRED_UID(peercred
), pwd
->pw_uid
);
504 # ifdef HAVE_SYS_UCRED_H
505 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAuthorize: cr_version=%d",
506 peercred
.cr_version
);
507 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAuthorize: cr_uid=%d",
509 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAuthorize: cr_ngroups=%d",
510 peercred
.cr_ngroups
);
511 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAuthorize: cr_groups[0]=%d",
512 peercred
.cr_groups
[0]);
513 # endif /* HAVE_SYS_UCRED_H */
517 strlcpy(username
, authorization
+ 9, sizeof(username
));
519 cupsdLogMessage(CUPSD_LOG_DEBUG
,
520 "cupsdAuthorize: Authorized as %s using PeerCred",
523 con
->type
= AUTH_BASIC
;
525 #endif /* SO_PEERCRED && AF_LOCAL */
526 else if (!strncmp(authorization
, "Local", 5) &&
527 !strcasecmp(con
->http
.hostname
, "localhost"))
530 * Get Local certificate authentication data...
534 while (isspace(*authorization
& 255))
537 if ((localuser
= cupsdFindCert(authorization
)) != NULL
)
539 strlcpy(username
, localuser
, sizeof(username
));
541 cupsdLogMessage(CUPSD_LOG_DEBUG
,
542 "cupsdAuthorize: Authorized as %s using Local",
547 cupsdLogMessage(CUPSD_LOG_ERROR
,
548 "cupsdAuthorize: Local authentication certificate not "
553 con
->type
= AUTH_BASIC
;
555 else if (!strncmp(authorization
, "Basic", 5))
558 * Get the Basic authentication data...
561 int userlen
; /* Username:password length */
565 while (isspace(*authorization
& 255))
568 userlen
= sizeof(username
);
569 httpDecode64_2(username
, &userlen
, authorization
);
572 * Pull the username and password out...
575 if ((ptr
= strchr(username
, ':')) == NULL
)
577 cupsdLogMessage(CUPSD_LOG_ERROR
,
578 "cupsdAuthorize: Missing Basic password!");
587 * Username must not be empty...
590 cupsdLogMessage(CUPSD_LOG_ERROR
,
591 "cupsdAuthorize: Empty Basic username!");
598 * Password must not be empty...
601 cupsdLogMessage(CUPSD_LOG_ERROR
,
602 "cupsdAuthorize: Empty Basic password!");
606 strlcpy(password
, ptr
, sizeof(password
));
609 * Validate the username and password...
619 * Only use PAM to do authentication. This supports MD5
620 * passwords, among other things...
623 pam_handle_t
*pamh
; /* PAM authentication handle */
624 int pamerr
; /* PAM error code */
625 struct pam_conv pamdata
;/* PAM conversation data */
626 cupsd_authdata_t data
; /* Authentication data */
629 strlcpy(data
.username
, username
, sizeof(data
.username
));
630 strlcpy(data
.password
, password
, sizeof(data
.password
));
632 # if defined(__sun) || defined(__hpux)
633 pamdata
.conv
= (int (*)(int, struct pam_message
**,
634 struct pam_response
**,
637 pamdata
.conv
= pam_func
;
638 # endif /* __sun || __hpux */
639 pamdata
.appdata_ptr
= &data
;
643 * Workaround for HP-UX bug in pam_unix; see pam_func() below for
650 pamerr
= pam_start("cups", username
, &pamdata
, &pamh
);
651 if (pamerr
!= PAM_SUCCESS
)
653 cupsdLogMessage(CUPSD_LOG_ERROR
,
654 "cupsdAuthorize: pam_start() returned %d (%s)!\n",
655 pamerr
, pam_strerror(pamh
, pamerr
));
660 # if defined(HAVE_PAM_SET_ITEM) && defined(PAM_RHOST)
661 pamerr
= pam_set_item(pamh
, PAM_RHOST
, con
->http
.hostname
);
662 if (pamerr
!= PAM_SUCCESS
)
663 cupsdLogMessage(CUPSD_LOG_WARN
,
664 "cupsdAuthorize: pam_set_item() returned %d "
665 "(%s)!\n", pamerr
, pam_strerror(pamh
, pamerr
));
666 # endif /* HAVE_PAM_SET_ITEM && PAM_RHOST */
668 pamerr
= pam_authenticate(pamh
, PAM_SILENT
);
669 if (pamerr
!= PAM_SUCCESS
)
671 cupsdLogMessage(CUPSD_LOG_ERROR
,
672 "cupsdAuthorize: pam_authenticate() returned %d "
674 pamerr
, pam_strerror(pamh
, pamerr
));
679 pamerr
= pam_acct_mgmt(pamh
, PAM_SILENT
);
680 if (pamerr
!= PAM_SUCCESS
)
682 cupsdLogMessage(CUPSD_LOG_ERROR
,
683 "cupsdAuthorize: pam_acct_mgmt() returned %d "
685 pamerr
, pam_strerror(pamh
, pamerr
));
690 pam_end(pamh
, PAM_SUCCESS
);
692 #elif defined(HAVE_USERSEC_H)
694 * Use AIX authentication interface...
697 char *authmsg
; /* Authentication message */
698 int reenter
; /* ??? */
701 cupsdLogMessage(CUPSD_LOG_DEBUG
,
702 "cupsdAuthorize: AIX authenticate of username \"%s\"",
706 if (authenticate(username
, password
, &reenter
, &authmsg
) != 0)
708 cupsdLogMessage(CUPSD_LOG_DEBUG
,
709 "cupsdAuthorize: Unable to authenticate username "
711 username
, strerror(errno
));
717 * Use normal UNIX password file-based authentication...
720 char *pass
; /* Encrypted password */
721 struct passwd
*pw
; /* User password data */
722 # ifdef HAVE_SHADOW_H
723 struct spwd
*spw
; /* Shadow password data */
724 # endif /* HAVE_SHADOW_H */
727 pw
= getpwnam(username
); /* Get the current password */
728 endpwent(); /* Close the password file */
736 cupsdLogMessage(CUPSD_LOG_ERROR
,
737 "cupsdAuthorize: Unknown username \"%s\"!",
742 # ifdef HAVE_SHADOW_H
743 spw
= getspnam(username
);
746 if (!spw
&& !strcmp(pw
->pw_passwd
, "x"))
749 * Don't allow blank passwords!
752 cupsdLogMessage(CUPSD_LOG_ERROR
,
753 "cupsdAuthorize: Username \"%s\" has no shadow "
754 "password!", username
);
758 if (spw
&& !spw
->sp_pwdp
[0] && !pw
->pw_passwd
[0])
760 if (!pw
->pw_passwd
[0])
761 # endif /* HAVE_SHADOW_H */
764 * Don't allow blank passwords!
767 cupsdLogMessage(CUPSD_LOG_ERROR
,
768 "cupsdAuthorize: Username \"%s\" has no password!",
774 * OK, the password isn't blank, so compare with what came from the
778 pass
= cups_crypt(password
, pw
->pw_passwd
);
780 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
781 "cupsdAuthorize: pw_passwd=\"%s\", crypt=\"%s\"",
782 pw
->pw_passwd
, pass
);
784 if (!pass
|| strcmp(pw
->pw_passwd
, pass
))
786 # ifdef HAVE_SHADOW_H
789 pass
= cups_crypt(password
, spw
->sp_pwdp
);
791 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
792 "cupsdAuthorize: sp_pwdp=\"%s\", crypt=\"%s\"",
795 if (pass
== NULL
|| strcmp(spw
->sp_pwdp
, pass
))
797 cupsdLogMessage(CUPSD_LOG_ERROR
,
798 "cupsdAuthorize: Authentication failed for "
805 # endif /* HAVE_SHADOW_H */
807 cupsdLogMessage(CUPSD_LOG_ERROR
,
808 "cupsdAuthorize: Authentication failed for "
814 #endif /* HAVE_LIBPAM */
817 cupsdLogMessage(CUPSD_LOG_DEBUG
,
818 "cupsdAuthorize: Authorized as %s using Basic",
822 case AUTH_BASICDIGEST
:
824 * Do Basic authentication with the Digest password file...
827 if (!get_md5_password(username
, NULL
, md5
))
829 cupsdLogMessage(CUPSD_LOG_ERROR
,
830 "cupsdAuthorize: Unknown MD5 username \"%s\"!",
835 httpMD5(username
, "CUPS", password
, basicmd5
);
837 if (strcmp(md5
, basicmd5
))
839 cupsdLogMessage(CUPSD_LOG_ERROR
,
840 "cupsdAuthorize: Authentication failed for \"%s\"!",
845 cupsdLogMessage(CUPSD_LOG_DEBUG
,
846 "cupsdAuthorize: Authorized as %s using BasicDigest",
853 else if (!strncmp(authorization
, "Digest", 6))
856 * Get the username, password, and nonce from the Digest attributes...
859 if (!httpGetSubField2(&(con
->http
), HTTP_FIELD_AUTHORIZATION
, "username",
860 username
, sizeof(username
)) || !username
[0])
863 * Username must not be empty...
866 cupsdLogMessage(CUPSD_LOG_ERROR
,
867 "cupsdAuthorize: Empty or missing Digest username!");
871 if (!httpGetSubField2(&(con
->http
), HTTP_FIELD_AUTHORIZATION
, "response",
872 password
, sizeof(password
)) || !password
[0])
875 * Password must not be empty...
878 cupsdLogMessage(CUPSD_LOG_ERROR
,
879 "cupsdAuthorize: Empty or missing Digest password!");
883 if (!httpGetSubField(&(con
->http
), HTTP_FIELD_AUTHORIZATION
, "nonce",
886 cupsdLogMessage(CUPSD_LOG_ERROR
,
887 "cupsdAuthorize: No nonce value for Digest "
892 if (strcmp(con
->http
.hostname
, nonce
))
894 cupsdLogMessage(CUPSD_LOG_ERROR
,
895 "cupsdAuthorize: Bad nonce value, expected \"%s\", "
896 "got \"%s\"!", con
->http
.hostname
, nonce
);
901 * Validate the username and password...
904 if (!get_md5_password(username
, NULL
, md5
))
906 cupsdLogMessage(CUPSD_LOG_ERROR
,
907 "cupsdAuthorize: Unknown MD5 username \"%s\"!",
912 httpMD5Final(nonce
, states
[con
->http
.state
], con
->uri
, md5
);
914 if (strcmp(md5
, password
))
916 cupsdLogMessage(CUPSD_LOG_ERROR
,
917 "cupsdAuthorize: Authentication failed for \"%s\"!",
922 cupsdLogMessage(CUPSD_LOG_DEBUG
,
923 "cupsdAuthorize: Authorized as %s using Digest",
926 con
->type
= AUTH_DIGEST
;
929 else if (!strncmp(authorization
, "Negotiate", 9))
931 int len
; /* Length of authorization string */
932 gss_cred_id_t server_creds
; /* Server credentials */
933 gss_ctx_id_t context
; /* Authorization context */
934 OM_uint32 major_status
, /* Major status code */
935 minor_status
; /* Minor status code */
936 gss_buffer_desc input_token
= GSS_C_EMPTY_BUFFER
,
937 /* Input token from string */
938 output_token
= GSS_C_EMPTY_BUFFER
;
939 /* Output token for username */
940 gss_name_t client_name
; /* Client name */
941 unsigned int ret_flags
; /* Credential flags */
946 * If the weak-linked GSSAPI/Kerberos library is not present, don't try
950 if (gss_init_sec_context
== NULL
)
952 cupsdLogMessage(CUPSD_LOG_WARN
,
953 "GSSAPI/Kerberos authentication failed because the "
954 "Kerberos framework is not present.");
957 # endif /* __APPLE__ */
959 con
->gss_output_token
.length
= 0;
962 * Find the start of the Kerberos input token...
966 while (isspace(*authorization
& 255))
971 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
972 "cupsdAuthorize: No authentication data specified.");
977 * Get the server credentials...
980 if ((server_creds
= get_gss_creds(GSSServiceName
)) == NULL
)
984 * Decode the authorization string to get the input token...
987 len
= strlen(authorization
);
988 input_token
.value
= malloc(len
);
989 input_token
.value
= httpDecode64_2(input_token
.value
, &len
,
991 input_token
.length
= len
;
994 * Accept the input token to get the authorization info...
997 context
= GSS_C_NO_CONTEXT
;
998 client_name
= GSS_C_NO_NAME
;
999 major_status
= gss_accept_sec_context(&minor_status
,
1003 GSS_C_NO_CHANNEL_BINDINGS
,
1006 &con
->gss_output_token
,
1009 &con
->gss_delegated_cred
);
1011 if (GSS_ERROR(major_status
))
1013 cupsdLogGSSMessage(CUPSD_LOG_DEBUG
, major_status
, minor_status
,
1014 "cupsdAuthorize: Error accepting GSSAPI security "
1017 if (context
!= GSS_C_NO_CONTEXT
)
1018 gss_delete_sec_context(&minor_status
, &context
, GSS_C_NO_BUFFER
);
1020 gss_release_cred(&minor_status
, &server_creds
);
1025 * Release our credentials...
1028 gss_release_cred(&minor_status
, &server_creds
);
1031 * Get the username associated with the client's credentials...
1034 if (!con
->gss_delegated_cred
)
1035 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1036 "cupsdAuthorize: No delegated credentials!");
1038 if (major_status
== GSS_S_CONTINUE_NEEDED
)
1039 cupsdLogGSSMessage(CUPSD_LOG_DEBUG
, major_status
, minor_status
,
1040 "cupsdAuthorize: Credentials not complete");
1041 else if (major_status
== GSS_S_COMPLETE
)
1043 major_status
= gss_display_name(&minor_status
, client_name
,
1044 &output_token
, NULL
);
1046 if (GSS_ERROR(major_status
))
1048 cupsdLogGSSMessage(CUPSD_LOG_DEBUG
, major_status
, minor_status
,
1049 "cupsdAuthorize: Error getting username");
1050 gss_release_name(&minor_status
, &client_name
);
1051 gss_delete_sec_context(&minor_status
, &context
, GSS_C_NO_BUFFER
);
1055 gss_release_name(&minor_status
, &client_name
);
1056 strlcpy(username
, output_token
.value
, sizeof(username
));
1058 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1059 "cupsdAuthorize: Authorized as %s using Negotiate",
1062 gss_release_buffer(&minor_status
, &output_token
);
1063 gss_delete_sec_context(&minor_status
, &context
, GSS_C_NO_BUFFER
);
1065 con
->gss_have_creds
= 1;
1067 con
->type
= AUTH_NEGOTIATE
;
1070 gss_release_name(&minor_status
, &client_name
);
1072 #endif /* HAVE_GSSAPI */
1075 char scheme
[256]; /* Auth scheme... */
1078 if (sscanf(authorization
, "%255s", scheme
) != 1)
1079 strcpy(scheme
, "UNKNOWN");
1081 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad authentication data \"%s ...\"",
1087 * If we get here, then we were able to validate the username and
1088 * password - copy the validated username and password to the client
1089 * data and return...
1092 strlcpy(con
->username
, username
, sizeof(con
->username
));
1093 strlcpy(con
->password
, password
, sizeof(con
->password
));
1098 * 'cupsdCheckAuth()' - Check authorization masks.
1101 int /* O - 1 if mask matches, 0 otherwise */
1103 unsigned ip
[4], /* I - Client address */
1104 char *name
, /* I - Client hostname */
1105 int name_len
, /* I - Length of hostname */
1106 int num_masks
, /* I - Number of masks */
1107 cupsd_authmask_t
*masks
) /* I - Masks */
1109 int i
; /* Looping var */
1110 cupsd_netif_t
*iface
; /* Network interface */
1111 unsigned netip4
; /* IPv4 network address */
1113 unsigned netip6
[4]; /* IPv6 network address */
1114 #endif /* AF_INET6 */
1116 while (num_masks
> 0)
1118 switch (masks
->type
)
1120 case AUTH_INTERFACE
:
1122 * Check for a match with a network interface...
1125 netip4
= htonl(ip
[3]);
1128 netip6
[0] = htonl(ip
[0]);
1129 netip6
[1] = htonl(ip
[1]);
1130 netip6
[2] = htonl(ip
[2]);
1131 netip6
[3] = htonl(ip
[3]);
1132 #endif /* AF_INET6 */
1134 if (!strcmp(masks
->mask
.name
.name
, "*"))
1137 * Check against all local interfaces...
1142 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
1144 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
1147 * Only check local interfaces...
1150 if (!iface
->is_local
)
1153 if (iface
->address
.addr
.sa_family
== AF_INET
)
1156 * Check IPv4 address...
1159 if ((netip4
& iface
->mask
.ipv4
.sin_addr
.s_addr
) ==
1160 (iface
->address
.ipv4
.sin_addr
.s_addr
&
1161 iface
->mask
.ipv4
.sin_addr
.s_addr
))
1168 * Check IPv6 address...
1171 for (i
= 0; i
< 4; i
++)
1172 if ((netip6
[i
] & iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]) !=
1173 (iface
->address
.ipv6
.sin6_addr
.s6_addr32
[i
] &
1174 iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]))
1180 #endif /* AF_INET6 */
1186 * Check the named interface...
1189 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
1191 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
1193 if (strcmp(masks
->mask
.name
.name
, iface
->name
))
1196 if (iface
->address
.addr
.sa_family
== AF_INET
)
1199 * Check IPv4 address...
1202 if ((netip4
& iface
->mask
.ipv4
.sin_addr
.s_addr
) ==
1203 (iface
->address
.ipv4
.sin_addr
.s_addr
&
1204 iface
->mask
.ipv4
.sin_addr
.s_addr
))
1211 * Check IPv6 address...
1214 for (i
= 0; i
< 4; i
++)
1215 if ((netip6
[i
] & iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]) !=
1216 (iface
->address
.ipv6
.sin6_addr
.s6_addr32
[i
] &
1217 iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]))
1223 #endif /* AF_INET6 */
1230 * Check for exact name match...
1233 if (!strcasecmp(name
, masks
->mask
.name
.name
))
1237 * Check for domain match...
1240 if (name_len
>= masks
->mask
.name
.length
&&
1241 masks
->mask
.name
.name
[0] == '.' &&
1242 !strcasecmp(name
+ name_len
- masks
->mask
.name
.length
,
1243 masks
->mask
.name
.name
))
1249 * Check for IP/network address match...
1252 for (i
= 0; i
< 4; i
++)
1253 if ((ip
[i
] & masks
->mask
.ip
.netmask
[i
]) !=
1254 masks
->mask
.ip
.address
[i
])
1271 * 'cupsdCheckGroup()' - Check for a user's group membership.
1274 int /* O - 1 if user is a member, 0 otherwise */
1276 const char *username
, /* I - User name */
1277 struct passwd
*user
, /* I - System user info */
1278 const char *groupname
) /* I - Group name */
1280 int i
; /* Looping var */
1281 struct group
*group
; /* System group info */
1282 char junk
[33]; /* MD5 password (not used) */
1283 #ifdef HAVE_MBR_UID_TO_UUID
1284 uuid_t useruuid
, /* UUID for username */
1285 groupuuid
; /* UUID for groupname */
1286 int is_member
; /* True if user is a member of group */
1287 #endif /* HAVE_MBR_UID_TO_UUID */
1290 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1291 "cupsdCheckGroup(username=\"%s\", user=%p, groupname=\"%s\")",
1292 username
, user
, groupname
);
1298 if (!username
|| !groupname
)
1302 * Check to see if the user is a member of the named group...
1305 group
= getgrnam(groupname
);
1311 * Group exists, check it...
1314 for (i
= 0; group
->gr_mem
[i
]; i
++)
1315 if (!strcasecmp(username
, group
->gr_mem
[i
]))
1320 * Group doesn't exist or user not in group list, check the group ID
1321 * against the user's group ID...
1324 if (user
&& group
&& group
->gr_gid
== user
->pw_gid
)
1327 #ifdef HAVE_MBR_UID_TO_UUID
1329 * Check group membership through MacOS X membership API...
1333 if (!mbr_uid_to_uuid(user
->pw_uid
, useruuid
))
1334 if (!mbr_gid_to_uuid(group
->gr_gid
, groupuuid
))
1335 if (!mbr_check_membership(useruuid
, groupuuid
, &is_member
))
1338 #endif /* HAVE_MBR_UID_TO_UUID */
1341 * Username not found, group not found, or user is not part of the
1342 * system group... Check for a user and group in the MD5 password
1346 if (get_md5_password(username
, groupname
, junk
) != NULL
)
1350 * If we get this far, then the user isn't part of the named group...
1358 * 'cupsdCopyLocation()' - Make a copy of a location...
1361 cupsd_location_t
* /* O - New location */
1363 cupsd_location_t
**loc
) /* IO - Original location */
1365 int i
; /* Looping var */
1366 cupsd_location_t
*temp
; /* New location */
1367 char location
[HTTP_MAX_URI
];
1368 /* Location of resource */
1372 * Use a local copy of location because cupsdAddLocation may cause
1373 * this memory to be moved...
1376 strlcpy(location
, (*loc
)->location
, sizeof(location
));
1378 if ((temp
= cupsdAddLocation(location
)) == NULL
)
1382 * Copy the information from the original location to the new one.
1385 temp
->limit
= (*loc
)->limit
;
1386 temp
->order_type
= (*loc
)->order_type
;
1387 temp
->type
= (*loc
)->type
;
1388 temp
->level
= (*loc
)->level
;
1389 temp
->satisfy
= (*loc
)->satisfy
;
1390 temp
->encryption
= (*loc
)->encryption
;
1392 if ((temp
->num_names
= (*loc
)->num_names
) > 0)
1395 * Copy the names array...
1398 if ((temp
->names
= calloc(temp
->num_names
, sizeof(char *))) == NULL
)
1400 cupsdLogMessage(CUPSD_LOG_ERROR
,
1401 "cupsdCopyLocation: Unable to allocate memory for %d names: %s",
1402 temp
->num_names
, strerror(errno
));
1404 cupsdDeleteLocation(temp
);
1408 for (i
= 0; i
< temp
->num_names
; i
++)
1409 if ((temp
->names
[i
] = strdup((*loc
)->names
[i
])) == NULL
)
1411 cupsdLogMessage(CUPSD_LOG_ERROR
,
1412 "cupsdCopyLocation: Unable to copy name \"%s\": %s",
1413 (*loc
)->names
[i
], strerror(errno
));
1415 cupsdDeleteLocation(temp
);
1420 if ((temp
->num_allow
= (*loc
)->num_allow
) > 0)
1423 * Copy allow rules...
1426 if ((temp
->allow
= calloc(temp
->num_allow
, sizeof(cupsd_authmask_t
))) == NULL
)
1428 cupsdLogMessage(CUPSD_LOG_ERROR
,
1429 "cupsdCopyLocation: Unable to allocate memory for %d allow rules: %s",
1430 temp
->num_allow
, strerror(errno
));
1431 cupsdDeleteLocation(temp
);
1435 for (i
= 0; i
< temp
->num_allow
; i
++)
1436 switch (temp
->allow
[i
].type
= (*loc
)->allow
[i
].type
)
1439 temp
->allow
[i
].mask
.name
.length
= (*loc
)->allow
[i
].mask
.name
.length
;
1440 temp
->allow
[i
].mask
.name
.name
= strdup((*loc
)->allow
[i
].mask
.name
.name
);
1442 if (temp
->allow
[i
].mask
.name
.name
== NULL
)
1444 cupsdLogMessage(CUPSD_LOG_ERROR
,
1445 "cupsdCopyLocation: Unable to copy allow name \"%s\": %s",
1446 (*loc
)->allow
[i
].mask
.name
.name
, strerror(errno
));
1447 cupsdDeleteLocation(temp
);
1452 memcpy(&(temp
->allow
[i
].mask
.ip
), &((*loc
)->allow
[i
].mask
.ip
),
1453 sizeof(cupsd_ipmask_t
));
1458 if ((temp
->num_deny
= (*loc
)->num_deny
) > 0)
1461 * Copy deny rules...
1464 if ((temp
->deny
= calloc(temp
->num_deny
, sizeof(cupsd_authmask_t
))) == NULL
)
1466 cupsdLogMessage(CUPSD_LOG_ERROR
,
1467 "cupsdCopyLocation: Unable to allocate memory for %d deny rules: %s",
1468 temp
->num_deny
, strerror(errno
));
1469 cupsdDeleteLocation(temp
);
1473 for (i
= 0; i
< temp
->num_deny
; i
++)
1474 switch (temp
->deny
[i
].type
= (*loc
)->deny
[i
].type
)
1477 temp
->deny
[i
].mask
.name
.length
= (*loc
)->deny
[i
].mask
.name
.length
;
1478 temp
->deny
[i
].mask
.name
.name
= strdup((*loc
)->deny
[i
].mask
.name
.name
);
1480 if (temp
->deny
[i
].mask
.name
.name
== NULL
)
1482 cupsdLogMessage(CUPSD_LOG_ERROR
,
1483 "cupsdCopyLocation: Unable to copy deny name \"%s\": %s",
1484 (*loc
)->deny
[i
].mask
.name
.name
, strerror(errno
));
1485 cupsdDeleteLocation(temp
);
1490 memcpy(&(temp
->deny
[i
].mask
.ip
), &((*loc
)->deny
[i
].mask
.ip
),
1491 sizeof(cupsd_ipmask_t
));
1501 * 'cupsdDeleteAllLocations()' - Free all memory used for location authorization.
1505 cupsdDeleteAllLocations(void)
1507 cupsd_location_t
*loc
; /* Current location */
1511 * Free all of the allow/deny records first...
1514 for (loc
= (cupsd_location_t
*)cupsArrayFirst(Locations
);
1516 loc
= (cupsd_location_t
*)cupsArrayNext(Locations
))
1517 cupsdDeleteLocation(loc
);
1520 * Then free the location array...
1523 cupsArrayDelete(Locations
);
1529 * 'cupsdDeleteLocation()' - Free all memory used by a location.
1533 cupsdDeleteLocation(
1534 cupsd_location_t
*loc
) /* I - Location to delete */
1536 int i
; /* Looping var */
1537 cupsd_authmask_t
*mask
; /* Current mask */
1540 cupsArrayRemove(Locations
, loc
);
1542 for (i
= loc
->num_names
- 1; i
>= 0; i
--)
1543 free(loc
->names
[i
]);
1545 if (loc
->num_names
> 0)
1548 for (i
= loc
->num_allow
, mask
= loc
->allow
; i
> 0; i
--, mask
++)
1549 if (mask
->type
== AUTH_NAME
|| mask
->type
== AUTH_INTERFACE
)
1550 free(mask
->mask
.name
.name
);
1552 if (loc
->num_allow
> 0)
1555 for (i
= loc
->num_deny
, mask
= loc
->deny
; i
> 0; i
--, mask
++)
1556 if (mask
->type
== AUTH_NAME
|| mask
->type
== AUTH_INTERFACE
)
1557 free(mask
->mask
.name
.name
);
1559 if (loc
->num_deny
> 0)
1562 free(loc
->location
);
1568 * 'cupsdDenyHost()' - Add a host name that is not allowed to access the
1573 cupsdDenyHost(cupsd_location_t
*loc
, /* I - Location to add to */
1574 char *name
) /* I - Name of host or domain to add */
1576 cupsd_authmask_t
*temp
; /* New host/domain mask */
1577 char ifname
[32], /* Interface name */
1578 *ifptr
; /* Pointer to end of name */
1581 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdDenyHost(loc=%p(%s), name=\"%s\")",
1582 loc
, loc
->location
? loc
->location
: "nil", name
);
1584 if ((temp
= add_deny(loc
)) == NULL
)
1587 if (!strcasecmp(name
, "@LOCAL"))
1590 * Deny *interface*...
1593 temp
->type
= AUTH_INTERFACE
;
1594 temp
->mask
.name
.name
= strdup("*");
1595 temp
->mask
.name
.length
= 1;
1597 else if (!strncasecmp(name
, "@IF(", 4))
1600 * Deny *interface*...
1603 strlcpy(ifname
, name
+ 4, sizeof(ifname
));
1605 ifptr
= ifname
+ strlen(ifname
);
1607 if (ifptr
[-1] == ')')
1613 temp
->type
= AUTH_INTERFACE
;
1614 temp
->mask
.name
.name
= strdup(ifname
);
1615 temp
->mask
.name
.length
= ifptr
- ifname
;
1623 temp
->type
= AUTH_NAME
;
1624 temp
->mask
.name
.name
= strdup(name
);
1625 temp
->mask
.name
.length
= strlen(name
);
1631 * 'cupsdDenyIP()' - Add an IP address or network that is not allowed to
1632 * access the location.
1636 cupsdDenyIP(cupsd_location_t
*loc
, /* I - Location to add to */
1637 unsigned address
[4],/* I - IP address to add */
1638 unsigned netmask
[4])/* I - Netmask of address */
1640 cupsd_authmask_t
*temp
; /* New host/domain mask */
1643 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1644 "cupsdDenyIP(loc=%p(%s), address=%x:%x:%x:%x, netmask=%x:%x:%x:%x)",
1645 loc
, loc
->location
? loc
->location
: "nil",
1646 address
[0], address
[1], address
[2], address
[3],
1647 netmask
[0], netmask
[1], netmask
[2], netmask
[3]);
1649 if ((temp
= add_deny(loc
)) == NULL
)
1652 temp
->type
= AUTH_IP
;
1653 memcpy(temp
->mask
.ip
.address
, address
, sizeof(temp
->mask
.ip
.address
));
1654 memcpy(temp
->mask
.ip
.netmask
, netmask
, sizeof(temp
->mask
.ip
.netmask
));
1659 * 'cupsdFindBest()' - Find the location entry that best matches the resource.
1662 cupsd_location_t
* /* O - Location that matches */
1663 cupsdFindBest(const char *path
, /* I - Resource path */
1664 http_state_t state
) /* I - HTTP state/request */
1666 char uri
[HTTP_MAX_URI
],
1667 /* URI in request... */
1668 *uriptr
; /* Pointer into URI */
1669 cupsd_location_t
*loc
, /* Current location */
1670 *best
; /* Best match for location so far */
1671 int bestlen
; /* Length of best match */
1672 int limit
; /* Limit field */
1673 static const int limits
[] = /* Map http_status_t to AUTH_LIMIT_xyz */
1693 * First copy the connection URI to a local string so we have drop
1694 * any .ppd extension from the pathname in /printers or /classes
1698 strlcpy(uri
, path
, sizeof(uri
));
1700 if (!strncmp(uri
, "/printers/", 10) ||
1701 !strncmp(uri
, "/classes/", 9))
1704 * Check if the URI has .ppd on the end...
1707 uriptr
= uri
+ strlen(uri
) - 4; /* len > 4 if we get here... */
1709 if (!strcmp(uriptr
, ".ppd"))
1713 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: uri = \"%s\"...", uri
);
1716 * Loop through the list of locations to find a match...
1719 limit
= limits
[state
];
1723 for (loc
= (cupsd_location_t
*)cupsArrayFirst(Locations
);
1725 loc
= (cupsd_location_t
*)cupsArrayNext(Locations
))
1727 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: Location %s Limit %x",
1728 loc
->location
? loc
->location
: "nil", loc
->limit
);
1730 if (!strncmp(uri
, "/printers/", 10) || !strncmp(uri
, "/classes/", 9))
1733 * Use case-insensitive comparison for queue names...
1736 if (loc
->length
> bestlen
&& loc
->location
&&
1737 !strncasecmp(uri
, loc
->location
, loc
->length
) &&
1738 loc
->location
[0] == '/' &&
1739 (limit
& loc
->limit
) != 0)
1742 bestlen
= loc
->length
;
1748 * Use case-sensitive comparison for other URIs...
1751 if (loc
->length
> bestlen
&& loc
->location
&&
1752 !strncmp(uri
, loc
->location
, loc
->length
) &&
1753 loc
->location
[0] == '/' &&
1754 (limit
& loc
->limit
) != 0)
1757 bestlen
= loc
->length
;
1763 * Return the match, if any...
1766 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: best = %s",
1767 best
? best
->location
: "NONE");
1774 * 'cupsdFindLocation()' - Find the named location.
1777 cupsd_location_t
* /* O - Location that matches */
1778 cupsdFindLocation(const char *location
) /* I - Connection */
1780 cupsd_location_t key
; /* Search key */
1783 key
.location
= (char *)location
;
1785 return ((cupsd_location_t
*)cupsArrayFind(Locations
, &key
));
1790 * 'cupsdIsAuthorized()' - Check to see if the user is authorized...
1793 http_status_t
/* O - HTTP_OK if authorized or error code */
1794 cupsdIsAuthorized(cupsd_client_t
*con
, /* I - Connection */
1795 const char *owner
)/* I - Owner of object */
1797 int i
, j
, /* Looping vars */
1798 auth
, /* Authorization status */
1799 type
; /* Type of authentication */
1800 unsigned address
[4]; /* Authorization address */
1801 cupsd_location_t
*best
; /* Best match for location so far */
1802 int hostlen
; /* Length of hostname */
1803 char username
[256], /* Username to authorize */
1804 ownername
[256], /* Owner name to authorize */
1805 *ptr
; /* Pointer into username */
1806 struct passwd
*pw
; /* User password data */
1807 static const char * const levels
[] = /* Auth levels */
1813 static const char * const types
[] = /* Auth types */
1823 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1824 "cupsdIsAuthorized: con->uri=\"%s\", con->best=%p(%s)",
1825 con
->uri
, con
->best
, con
->best
? con
->best
->location
?
1826 con
->best
->location
: "(null)" : "");
1828 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1829 "cupsdIsAuthorized: owner=\"%s\"", owner
);
1832 * If there is no "best" authentication rule for this request, then
1833 * access is allowed from the local system and denied from other
1839 if (!strcmp(con
->http
.hostname
, "localhost") ||
1840 !strcmp(con
->http
.hostname
, ServerName
))
1843 return (HTTP_FORBIDDEN
);
1848 if ((type
= best
->type
) == AUTH_DEFAULT
)
1849 type
= DefaultAuthType
;
1851 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1852 "cupsdIsAuthorized: level=AUTH_%s, type=%s, "
1853 "satisfy=AUTH_SATISFY_%s, num_names=%d",
1854 levels
[best
->level
], types
[type
],
1855 best
->satisfy
? "ANY" : "ALL", best
->num_names
);
1857 if (best
->limit
== AUTH_LIMIT_IPP
)
1858 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: op=%x(%s)",
1859 best
->op
, ippOpString(best
->op
));
1862 * Check host/ip-based accesses...
1866 if (con
->http
.hostaddr
->addr
.sa_family
== AF_INET6
)
1869 * Copy IPv6 address...
1872 address
[0] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[0]);
1873 address
[1] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[1]);
1874 address
[2] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[2]);
1875 address
[3] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[3]);
1878 #endif /* AF_INET6 */
1879 if (con
->http
.hostaddr
->addr
.sa_family
== AF_INET
)
1882 * Copy IPv4 address...
1888 address
[3] = ntohl(con
->http
.hostaddr
->ipv4
.sin_addr
.s_addr
);
1891 memset(address
, 0, sizeof(address
));
1893 hostlen
= strlen(con
->http
.hostname
);
1895 if (!strcasecmp(con
->http
.hostname
, "localhost"))
1898 * Access from localhost (127.0.0.1 or ::1) is always allowed...
1906 * Do authorization checks on the domain/address...
1909 switch (best
->order_type
)
1912 auth
= AUTH_DENY
; /* anti-compiler-warning-code */
1915 case AUTH_ALLOW
: /* Order Deny,Allow */
1918 if (cupsdCheckAuth(address
, con
->http
.hostname
, hostlen
,
1919 best
->num_deny
, best
->deny
))
1922 if (cupsdCheckAuth(address
, con
->http
.hostname
, hostlen
,
1923 best
->num_allow
, best
->allow
))
1927 case AUTH_DENY
: /* Order Allow,Deny */
1930 if (cupsdCheckAuth(address
, con
->http
.hostname
, hostlen
,
1931 best
->num_allow
, best
->allow
))
1934 if (cupsdCheckAuth(address
, con
->http
.hostname
, hostlen
,
1935 best
->num_deny
, best
->deny
))
1941 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: auth=AUTH_%s...",
1942 auth
? "DENY" : "ALLOW");
1944 if (auth
== AUTH_DENY
&& best
->satisfy
== AUTH_SATISFY_ALL
)
1945 return (HTTP_FORBIDDEN
);
1949 * See if encryption is required...
1952 if ((best
->encryption
>= HTTP_ENCRYPT_REQUIRED
&& !con
->http
.tls
&&
1953 strcasecmp(con
->http
.hostname
, "localhost") &&
1954 best
->satisfy
== AUTH_SATISFY_ALL
) &&
1955 !(type
== AUTH_NEGOTIATE
||
1956 (type
== AUTH_NONE
&& DefaultAuthType
== AUTH_NEGOTIATE
)))
1958 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1959 "cupsdIsAuthorized: Need upgrade to TLS...");
1960 return (HTTP_UPGRADE_REQUIRED
);
1962 #endif /* HAVE_SSL */
1965 * Now see what access level is required...
1968 if (best
->level
== AUTH_ANON
|| /* Anonymous access - allow it */
1969 (type
== AUTH_NONE
&& best
->num_names
== 0))
1972 if (!con
->username
[0] && type
== AUTH_NONE
&&
1973 best
->limit
== AUTH_LIMIT_IPP
)
1976 * Check for unauthenticated username...
1979 ipp_attribute_t
*attr
; /* requesting-user-name attribute */
1982 attr
= ippFindAttribute(con
->request
, "requesting-user-name", IPP_TAG_NAME
);
1985 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1986 "cupsdIsAuthorized: requesting-user-name=\"%s\"",
1987 attr
->values
[0].string
.text
);
1988 strlcpy(username
, attr
->values
[0].string
.text
, sizeof(username
));
1990 else if (best
->satisfy
== AUTH_SATISFY_ALL
|| auth
== AUTH_DENY
)
1991 return (HTTP_UNAUTHORIZED
); /* Non-anonymous needs user/pass */
1993 return (HTTP_OK
); /* unless overridden with Satisfy */
1997 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdIsAuthorized: username=\"%s\"",
2000 #ifdef HAVE_AUTHORIZATION_H
2001 if (!con
->username
[0] && !con
->authref
)
2003 if (!con
->username
[0])
2004 #endif /* HAVE_AUTHORIZATION_H */
2006 if (best
->satisfy
== AUTH_SATISFY_ALL
|| auth
== AUTH_DENY
)
2007 return (HTTP_UNAUTHORIZED
); /* Non-anonymous needs user/pass */
2009 return (HTTP_OK
); /* unless overridden with Satisfy */
2012 if (con
->type
!= type
&& type
!= AUTH_NONE
&&
2013 (con
->type
!= AUTH_BASIC
|| type
!= AUTH_BASICDIGEST
))
2015 cupsdLogMessage(CUPSD_LOG_ERROR
, "Authorized using %s, expected %s!",
2016 types
[con
->type
], types
[type
]);
2018 return (HTTP_UNAUTHORIZED
);
2021 strlcpy(username
, con
->username
, sizeof(username
));
2025 * OK, got a username. See if we need normal user access, or group
2026 * access... (root always matches)
2029 if (!strcmp(username
, "root"))
2033 * Strip any @domain or @KDC from the username and owner...
2036 if ((ptr
= strchr(username
, '@')) != NULL
)
2041 strlcpy(ownername
, owner
, sizeof(ownername
));
2043 if ((ptr
= strchr(ownername
, '@')) != NULL
)
2047 ownername
[0] = '\0';
2050 * Get the user info...
2055 pw
= getpwnam(username
);
2061 if (best
->level
== AUTH_USER
)
2064 * If there are no names associated with this location, then
2065 * any valid user is OK...
2068 if (best
->num_names
== 0)
2072 * Otherwise check the user list and return OK if this user is
2076 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2077 "cupsdIsAuthorized: Checking user membership...");
2079 #ifdef HAVE_AUTHORIZATION_H
2081 * If an authorization reference was supplied it must match a right name...
2086 for (i
= 0; i
< best
->num_names
; i
++)
2088 if (!strncasecmp(best
->names
[i
], "@AUTHKEY(", 9) &&
2089 check_authref(con
, best
->names
[i
] + 9))
2091 else if (!strcasecmp(best
->names
[i
], "@SYSTEM") &&
2092 SystemGroupAuthKey
&&
2093 check_authref(con
, SystemGroupAuthKey
))
2097 return (HTTP_UNAUTHORIZED
);
2099 #endif /* HAVE_AUTHORIZATION_H */
2101 for (i
= 0; i
< best
->num_names
; i
++)
2103 if (!strcasecmp(best
->names
[i
], "@OWNER") && owner
&&
2104 !strcasecmp(username
, ownername
))
2106 else if (!strcasecmp(best
->names
[i
], "@SYSTEM"))
2108 for (j
= 0; j
< NumSystemGroups
; j
++)
2109 if (cupsdCheckGroup(username
, pw
, SystemGroups
[j
]))
2112 else if (best
->names
[i
][0] == '@')
2114 if (cupsdCheckGroup(username
, pw
, best
->names
[i
] + 1))
2117 else if (!strcasecmp(username
, best
->names
[i
]))
2121 return (HTTP_UNAUTHORIZED
);
2125 * Check to see if this user is in any of the named groups...
2128 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2129 "cupsdIsAuthorized: Checking group membership...");
2132 * Check to see if this user is in any of the named groups...
2135 for (i
= 0; i
< best
->num_names
; i
++)
2137 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2138 "cupsdIsAuthorized: Checking group \"%s\" membership...",
2141 if (!strcasecmp(best
->names
[i
], "@SYSTEM"))
2143 for (j
= 0; j
< NumSystemGroups
; j
++)
2144 if (cupsdCheckGroup(username
, pw
, SystemGroups
[j
]))
2147 else if (cupsdCheckGroup(username
, pw
, best
->names
[i
]))
2152 * The user isn't part of the specified group, so deny access...
2155 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2156 "cupsdIsAuthorized: User not in group(s)!");
2158 return (HTTP_UNAUTHORIZED
);
2163 * 'add_allow()' - Add an allow mask to the location.
2166 static cupsd_authmask_t
* /* O - New mask record */
2167 add_allow(cupsd_location_t
*loc
) /* I - Location to add to */
2169 cupsd_authmask_t
*temp
; /* New mask record */
2180 * Try to allocate memory for the record...
2183 if (loc
->num_allow
== 0)
2184 temp
= malloc(sizeof(cupsd_authmask_t
));
2186 temp
= realloc(loc
->allow
, sizeof(cupsd_authmask_t
) * (loc
->num_allow
+ 1));
2192 temp
+= loc
->num_allow
;
2196 * Clear the mask record and return...
2199 memset(temp
, 0, sizeof(cupsd_authmask_t
));
2205 * 'add_deny()' - Add a deny mask to the location.
2208 static cupsd_authmask_t
* /* O - New mask record */
2209 add_deny(cupsd_location_t
*loc
) /* I - Location to add to */
2211 cupsd_authmask_t
*temp
; /* New mask record */
2222 * Try to allocate memory for the record...
2225 if (loc
->num_deny
== 0)
2226 temp
= malloc(sizeof(cupsd_authmask_t
));
2228 temp
= realloc(loc
->deny
, sizeof(cupsd_authmask_t
) * (loc
->num_deny
+ 1));
2234 temp
+= loc
->num_deny
;
2238 * Clear the mask record and return...
2241 memset(temp
, 0, sizeof(cupsd_authmask_t
));
2246 #ifdef HAVE_AUTHORIZATION_H
2248 * 'check_authref()' - Check if an authorization services reference has the
2252 static int /* O - 1 if right is valid, 0 otherwise */
2253 check_authref(cupsd_client_t
*con
, /* I - Connection */
2254 const char *right
) /* I - Right name */
2256 OSStatus status
; /* OS Status */
2257 AuthorizationItem authright
; /* Authorization right */
2258 AuthorizationRights authrights
; /* Authorization rights */
2259 AuthorizationFlags authflags
; /* Authorization flags */
2263 * Check to see if the user is allowed to perform the task...
2269 authright
.name
= right
;
2270 authright
.valueLength
= 0;
2271 authright
.value
= NULL
;
2272 authright
.flags
= 0;
2274 authrights
.count
= 1;
2275 authrights
.items
= &authright
;
2277 authflags
= kAuthorizationFlagDefaults
|
2278 kAuthorizationFlagExtendRights
;
2280 if ((status
= AuthorizationCopyRights(con
->authref
, &authrights
,
2281 kAuthorizationEmptyEnvironment
,
2282 authflags
, NULL
)) != 0)
2284 cupsdLogMessage(CUPSD_LOG_ERROR
,
2285 "AuthorizationCopyRights(\"%s\") returned %d (%s)",
2286 authright
.name
, (int)status
, cssmErrorString(status
));
2290 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2291 "AuthorizationCopyRights(\"%s\") succeeded!",
2296 #endif /* HAVE_AUTHORIZATION_H */
2300 * 'compare_locations()' - Compare two locations.
2303 static int /* O - Result of comparison */
2304 compare_locations(cupsd_location_t
*a
, /* I - First location */
2305 cupsd_location_t
*b
) /* I - Second location */
2307 return (strcmp(b
->location
, a
->location
));
2311 #if !HAVE_LIBPAM && !defined(HAVE_USERSEC_H)
2313 * 'cups_crypt()' - Encrypt the password using the DES or MD5 algorithms,
2317 static char * /* O - Encrypted password */
2318 cups_crypt(const char *pw
, /* I - Password string */
2319 const char *salt
) /* I - Salt (key) string */
2321 if (!strncmp(salt
, "$1$", 3))
2324 * Use MD5 passwords without the benefit of PAM; this is for
2325 * Slackware Linux, and the algorithm was taken from the
2326 * old shadow-19990827/lib/md5crypt.c source code... :(
2329 int i
; /* Looping var */
2330 unsigned long n
; /* Output number */
2331 int pwlen
; /* Length of password string */
2332 const char *salt_end
; /* End of "salt" data for MD5 */
2333 char *ptr
; /* Pointer into result string */
2334 _cups_md5_state_t state
; /* Primary MD5 state info */
2335 _cups_md5_state_t state2
; /* Secondary MD5 state info */
2336 unsigned char digest
[16]; /* MD5 digest result */
2337 static char result
[120]; /* Final password string */
2341 * Get the salt data between dollar signs, e.g. $1$saltdata$md5.
2342 * Get a maximum of 8 characters of salt data after $1$...
2345 for (salt_end
= salt
+ 3; *salt_end
&& (salt_end
- salt
) < 11; salt_end
++)
2346 if (*salt_end
== '$')
2350 * Compute the MD5 sum we need...
2355 _cupsMD5Init(&state
);
2356 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
2357 _cupsMD5Append(&state
, (unsigned char *)salt
, salt_end
- salt
);
2359 _cupsMD5Init(&state2
);
2360 _cupsMD5Append(&state2
, (unsigned char *)pw
, pwlen
);
2361 _cupsMD5Append(&state2
, (unsigned char *)salt
+ 3, salt_end
- salt
- 3);
2362 _cupsMD5Append(&state2
, (unsigned char *)pw
, pwlen
);
2363 _cupsMD5Finish(&state2
, digest
);
2365 for (i
= pwlen
; i
> 0; i
-= 16)
2366 _cupsMD5Append(&state
, digest
, i
> 16 ? 16 : i
);
2368 for (i
= pwlen
; i
> 0; i
>>= 1)
2369 _cupsMD5Append(&state
, (unsigned char *)((i
& 1) ? "" : pw
), 1);
2371 _cupsMD5Finish(&state
, digest
);
2373 for (i
= 0; i
< 1000; i
++)
2375 _cupsMD5Init(&state
);
2378 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
2380 _cupsMD5Append(&state
, digest
, 16);
2383 _cupsMD5Append(&state
, (unsigned char *)salt
+ 3, salt_end
- salt
- 3);
2386 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
2389 _cupsMD5Append(&state
, digest
, 16);
2391 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
2393 _cupsMD5Finish(&state
, digest
);
2397 * Copy the final sum to the result string and return...
2400 memcpy(result
, salt
, salt_end
- salt
);
2401 ptr
= result
+ (salt_end
- salt
);
2404 for (i
= 0; i
< 5; i
++, ptr
+= 4)
2406 n
= (((digest
[i
] << 8) | digest
[i
+ 6]) << 8);
2409 n
|= digest
[i
+ 12];
2416 to64(ptr
, digest
[11], 2);
2425 * Use the standard crypt() function...
2428 return (crypt(pw
, salt
));
2431 #endif /* !HAVE_LIBPAM && !HAVE_USERSEC_H */
2436 * 'get_gss_creds()' - Obtain GSS credentials.
2439 static gss_cred_id_t
/* O - Server credentials */
2440 get_gss_creds(const char *service_name
) /* I - Service name */
2442 OM_uint32 major_status
, /* Major status code */
2443 minor_status
; /* Minor status code */
2444 gss_name_t server_name
; /* Server name */
2445 gss_cred_id_t server_creds
; /* Server credentials */
2446 gss_buffer_desc token
= GSS_C_EMPTY_BUFFER
;
2447 /* Service name token */
2448 char buf
[1024], /* Service name buffer */
2449 fqdn
[HTTP_MAX_URI
]; /* Hostname of server */
2452 snprintf(buf
, sizeof(buf
), "%s@%s", service_name
,
2453 httpGetHostname(NULL
, fqdn
, sizeof(fqdn
)));
2456 token
.length
= strlen(buf
);
2457 server_name
= GSS_C_NO_NAME
;
2458 major_status
= gss_import_name(&minor_status
, &token
,
2459 GSS_C_NT_HOSTBASED_SERVICE
,
2462 memset(&token
, 0, sizeof(token
));
2464 if (GSS_ERROR(major_status
))
2466 cupsdLogGSSMessage(CUPSD_LOG_WARN
, major_status
, minor_status
,
2467 "gss_import_name() failed");
2471 major_status
= gss_display_name(&minor_status
, server_name
, &token
, NULL
);
2473 if (GSS_ERROR(major_status
))
2475 cupsdLogGSSMessage(CUPSD_LOG_WARN
, major_status
, minor_status
,
2476 "gss_display_name() failed");
2480 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2481 "get_gss_creds: Attempting to acquire credentials for %s...",
2482 (char *)token
.value
);
2484 server_creds
= GSS_C_NO_CREDENTIAL
;
2485 major_status
= gss_acquire_cred(&minor_status
, server_name
, GSS_C_INDEFINITE
,
2486 GSS_C_NO_OID_SET
, GSS_C_ACCEPT
,
2487 &server_creds
, NULL
, NULL
);
2488 if (GSS_ERROR(major_status
))
2490 cupsdLogGSSMessage(CUPSD_LOG_WARN
, major_status
, minor_status
,
2491 "gss_acquire_cred() failed");
2492 gss_release_name(&minor_status
, &server_name
);
2493 gss_release_buffer(&minor_status
, &token
);
2497 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2498 "get_gss_creds: Credentials acquired successfully for %s.",
2499 (char *)token
.value
);
2501 gss_release_name(&minor_status
, &server_name
);
2502 gss_release_buffer(&minor_status
, &token
);
2504 return (server_creds
);
2506 #endif /* HAVE_GSSAPI */
2510 * 'get_md5_password()' - Get an MD5 password.
2513 static char * /* O - MD5 password string */
2514 get_md5_password(const char *username
, /* I - Username */
2515 const char *group
, /* I - Group */
2516 char passwd
[33]) /* O - MD5 password string */
2518 cups_file_t
*fp
; /* passwd.md5 file */
2519 char filename
[1024], /* passwd.md5 filename */
2520 line
[256], /* Line from file */
2521 tempuser
[33], /* User from file */
2522 tempgroup
[33]; /* Group from file */
2525 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2526 "get_md5_password(username=\"%s\", group=\"%s\", passwd=%p)",
2527 username
, group
? group
: "(null)", passwd
);
2529 snprintf(filename
, sizeof(filename
), "%s/passwd.md5", ServerRoot
);
2530 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
2532 if (errno
!= ENOENT
)
2533 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open %s - %s", filename
,
2539 while (cupsFileGets(fp
, line
, sizeof(line
)) != NULL
)
2541 if (sscanf(line
, "%32[^:]:%32[^:]:%32s", tempuser
, tempgroup
, passwd
) != 3)
2543 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad MD5 password line: %s", line
);
2547 if (!strcmp(username
, tempuser
) &&
2548 (group
== NULL
|| !strcmp(group
, tempgroup
)))
2551 * Found the password entry!
2554 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Found MD5 user %s, group %s...",
2555 username
, tempgroup
);
2563 * Didn't find a password entry - return NULL!
2573 * 'pam_func()' - PAM conversation function.
2576 static int /* O - Success or failure */
2578 int num_msg
, /* I - Number of messages */
2579 const struct pam_message
**msg
, /* I - Messages */
2580 struct pam_response
**resp
, /* O - Responses */
2582 /* I - Pointer to connection */
2584 int i
; /* Looping var */
2585 struct pam_response
*replies
; /* Replies */
2586 cupsd_authdata_t
*data
; /* Pointer to auth data */
2590 * Allocate memory for the responses...
2593 if ((replies
= malloc(sizeof(struct pam_response
) * num_msg
)) == NULL
)
2594 return (PAM_CONV_ERR
);
2597 * Answer all of the messages...
2600 DEBUG_printf(("pam_func: appdata_ptr = %p\n", appdata_ptr
));
2604 * Apparently some versions of HP-UX 11 have a broken pam_unix security
2605 * module. This is a workaround...
2611 data
= (cupsd_authdata_t
*)appdata_ptr
;
2614 for (i
= 0; i
< num_msg
; i
++)
2616 DEBUG_printf(("pam_func: Message = \"%s\"\n", msg
[i
]->msg
));
2618 switch (msg
[i
]->msg_style
)
2620 case PAM_PROMPT_ECHO_ON
:
2621 DEBUG_printf(("pam_func: PAM_PROMPT_ECHO_ON, returning \"%s\"...\n",
2623 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2624 replies
[i
].resp
= strdup(data
->username
);
2627 case PAM_PROMPT_ECHO_OFF
:
2628 DEBUG_printf(("pam_func: PAM_PROMPT_ECHO_OFF, returning \"%s\"...\n",
2630 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2631 replies
[i
].resp
= strdup(data
->password
);
2635 DEBUG_puts("pam_func: PAM_TEXT_INFO...");
2636 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2637 replies
[i
].resp
= NULL
;
2641 DEBUG_puts("pam_func: PAM_ERROR_MSG...");
2642 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2643 replies
[i
].resp
= NULL
;
2647 DEBUG_printf(("pam_func: Unknown PAM message %d...\n",
2648 msg
[i
]->msg_style
));
2650 return (PAM_CONV_ERR
);
2655 * Return the responses back to PAM...
2660 return (PAM_SUCCESS
);
2662 #elif !defined(HAVE_USERSEC_H)
2666 * 'to64()' - Base64-encode an integer value...
2670 to64(char *s
, /* O - Output string */
2671 unsigned long v
, /* I - Value to encode */
2672 int n
) /* I - Number of digits */
2674 const char *itoa64
= "./0123456789"
2675 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2676 "abcdefghijklmnopqrstuvwxyz";
2679 for (; n
> 0; n
--, v
>>= 6)
2680 *s
++ = itoa64
[v
& 0x3f];
2682 #endif /* HAVE_LIBPAM */
2686 * End of "$Id: auth.c 6947 2007-09-12 21:09:49Z mike $".