2 * "$Id: auth.c 6949 2007-09-12 21:33:23Z 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
);
367 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
368 "cupsdAuthorize: con->uri=\"%s\", con->best=%p(%s)",
369 con
->uri
, con
->best
, con
->best
? con
->best
->location
: "");
371 if (con
->best
&& con
->best
->type
!= AUTH_NONE
)
373 if (con
->best
->type
== AUTH_DEFAULT
)
374 type
= DefaultAuthType
;
376 type
= con
->best
->type
;
379 type
= DefaultAuthType
;
382 * Decode the Authorization string...
385 authorization
= httpGetField(&con
->http
, HTTP_FIELD_AUTHORIZATION
);
387 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAuthorize: Authorization=\"%s\"",
393 #ifdef HAVE_AUTHORIZATION_H
396 AuthorizationFree(con
->authref
, kAuthorizationFlagDefaults
);
399 #endif /* HAVE_AUTHORIZATION_H */
401 if (type
== AUTH_NONE
)
404 * No authorization required, return early...
407 cupsdLogMessage(CUPSD_LOG_DEBUG
,
408 "cupsdAuthorize: No authentication required.");
411 else if (!*authorization
)
414 * No authorization data provided, return early...
417 cupsdLogMessage(CUPSD_LOG_DEBUG
,
418 "cupsdAuthorize: No authentication data provided.");
421 #ifdef HAVE_AUTHORIZATION_H
422 else if (!strncmp(authorization
, "AuthRef", 6) &&
423 !strcasecmp(con
->http
.hostname
, "localhost"))
425 OSStatus status
; /* Status */
426 int authlen
; /* Auth string length */
427 AuthorizationItemSet
*authinfo
; /* Authorization item set */
430 * Get the Authorization Services data...
434 while (isspace(*authorization
& 255))
437 authlen
= sizeof(nonce
);
438 httpDecode64_2(nonce
, &authlen
, authorization
);
440 if (authlen
!= kAuthorizationExternalFormLength
)
442 cupsdLogMessage(CUPSD_LOG_ERROR
,
443 "External Authorization reference size is incorrect!");
447 if ((status
= AuthorizationCreateFromExternalForm(
448 (AuthorizationExternalForm
*)nonce
, &con
->authref
)) != 0)
450 cupsdLogMessage(CUPSD_LOG_ERROR
,
451 "AuthorizationCreateFromExternalForm returned %d (%s)",
452 (int)status
, cssmErrorString(status
));
456 if ((status
= AuthorizationCopyInfo(con
->authref
,
457 kAuthorizationEnvironmentUsername
,
460 cupsdLogMessage(CUPSD_LOG_ERROR
,
461 "AuthorizationCopyInfo returned %d (%s)",
462 (int)status
, cssmErrorString(status
));
466 if (authinfo
->count
== 1)
467 strlcpy(username
, authinfo
->items
[0].value
, sizeof(username
));
469 cupsdLogMessage(CUPSD_LOG_DEBUG
,
470 "cupsdAuthorize: Authorized as %s using AuthRef",
473 AuthorizationFreeItemSet(authinfo
);
475 #endif /* HAVE_AUTHORIZATION_H */
476 #if defined(SO_PEERCRED) && defined(AF_LOCAL)
477 else if (!strncmp(authorization
, "PeerCred ", 9) &&
478 con
->http
.hostaddr
->addr
.sa_family
== AF_LOCAL
)
481 * Use peer credentials from domain socket connection...
484 struct passwd
*pwd
; /* Password entry for this user */
485 cupsd_ucred_t peercred
; /* Peer credentials */
486 socklen_t peersize
; /* Size of peer credentials */
489 if ((pwd
= getpwnam(authorization
+ 9)) == NULL
)
491 cupsdLogMessage(CUPSD_LOG_ERROR
, "User \"%s\" does not exist!",
496 peersize
= sizeof(peercred
);
498 if (getsockopt(con
->http
.fd
, SOL_SOCKET
, SO_PEERCRED
, &peercred
, &peersize
))
500 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to get peer credentials - %s",
505 if (pwd
->pw_uid
!= CUPSD_UCRED_UID(peercred
))
507 cupsdLogMessage(CUPSD_LOG_ERROR
,
508 "Invalid peer credentials for \"%s\" - got %d, "
509 "expected %d!", authorization
+ 9,
510 CUPSD_UCRED_UID(peercred
), pwd
->pw_uid
);
511 # ifdef HAVE_SYS_UCRED_H
512 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAuthorize: cr_version=%d",
513 peercred
.cr_version
);
514 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAuthorize: cr_uid=%d",
516 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAuthorize: cr_ngroups=%d",
517 peercred
.cr_ngroups
);
518 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAuthorize: cr_groups[0]=%d",
519 peercred
.cr_groups
[0]);
520 # endif /* HAVE_SYS_UCRED_H */
524 strlcpy(username
, authorization
+ 9, sizeof(username
));
526 cupsdLogMessage(CUPSD_LOG_DEBUG
,
527 "cupsdAuthorize: Authorized as %s using PeerCred",
530 #endif /* SO_PEERCRED && AF_LOCAL */
531 else if (!strncmp(authorization
, "Local", 5) &&
532 !strcasecmp(con
->http
.hostname
, "localhost"))
535 * Get Local certificate authentication data...
539 while (isspace(*authorization
& 255))
542 if ((localuser
= cupsdFindCert(authorization
)) != NULL
)
544 strlcpy(username
, localuser
, sizeof(username
));
546 cupsdLogMessage(CUPSD_LOG_DEBUG
,
547 "cupsdAuthorize: Authorized as %s using Local",
552 cupsdLogMessage(CUPSD_LOG_ERROR
,
553 "cupsdAuthorize: Local authentication certificate not "
558 else if (!strncmp(authorization
, "Basic", 5) &&
559 (type
== AUTH_BASIC
|| type
== AUTH_BASICDIGEST
))
562 * Get the Basic authentication data...
565 int userlen
; /* Username:password length */
569 while (isspace(*authorization
& 255))
572 userlen
= sizeof(username
);
573 httpDecode64_2(username
, &userlen
, authorization
);
576 * Pull the username and password out...
579 if ((ptr
= strchr(username
, ':')) == NULL
)
581 cupsdLogMessage(CUPSD_LOG_ERROR
,
582 "cupsdAuthorize: Missing Basic password!");
591 * Username must not be empty...
594 cupsdLogMessage(CUPSD_LOG_ERROR
,
595 "cupsdAuthorize: Empty Basic username!");
602 * Password must not be empty...
605 cupsdLogMessage(CUPSD_LOG_ERROR
,
606 "cupsdAuthorize: Empty Basic password!");
610 strlcpy(password
, ptr
, sizeof(password
));
613 * Validate the username and password...
622 * Only use PAM to do authentication. This supports MD5
623 * passwords, among other things...
626 pam_handle_t
*pamh
; /* PAM authentication handle */
627 int pamerr
; /* PAM error code */
628 struct pam_conv pamdata
;/* PAM conversation data */
629 cupsd_authdata_t data
; /* Authentication data */
632 strlcpy(data
.username
, username
, sizeof(data
.username
));
633 strlcpy(data
.password
, password
, sizeof(data
.password
));
635 # if defined(__sun) || defined(__hpux)
636 pamdata
.conv
= (int (*)(int, struct pam_message
**,
637 struct pam_response
**,
640 pamdata
.conv
= pam_func
;
641 # endif /* __sun || __hpux */
642 pamdata
.appdata_ptr
= &data
;
646 * Workaround for HP-UX bug in pam_unix; see pam_func() below for
653 pamerr
= pam_start("cups", username
, &pamdata
, &pamh
);
654 if (pamerr
!= PAM_SUCCESS
)
656 cupsdLogMessage(CUPSD_LOG_ERROR
,
657 "cupsdAuthorize: pam_start() returned %d (%s)!\n",
658 pamerr
, pam_strerror(pamh
, pamerr
));
663 pamerr
= pam_authenticate(pamh
, PAM_SILENT
);
664 if (pamerr
!= PAM_SUCCESS
)
666 cupsdLogMessage(CUPSD_LOG_ERROR
,
667 "cupsdAuthorize: pam_authenticate() returned %d "
669 pamerr
, pam_strerror(pamh
, pamerr
));
674 pamerr
= pam_acct_mgmt(pamh
, PAM_SILENT
);
675 if (pamerr
!= PAM_SUCCESS
)
677 cupsdLogMessage(CUPSD_LOG_ERROR
,
678 "cupsdAuthorize: pam_acct_mgmt() returned %d "
680 pamerr
, pam_strerror(pamh
, pamerr
));
685 pam_end(pamh
, PAM_SUCCESS
);
687 #elif defined(HAVE_USERSEC_H)
689 * Use AIX authentication interface...
692 char *authmsg
; /* Authentication message */
693 int reenter
; /* ??? */
696 cupsdLogMessage(CUPSD_LOG_DEBUG
,
697 "cupsdAuthorize: AIX authenticate of username \"%s\"",
701 if (authenticate(username
, password
, &reenter
, &authmsg
) != 0)
703 cupsdLogMessage(CUPSD_LOG_DEBUG
,
704 "cupsdAuthorize: Unable to authenticate username "
706 username
, strerror(errno
));
712 * Use normal UNIX password file-based authentication...
715 char *pass
; /* Encrypted password */
716 struct passwd
*pw
; /* User password data */
717 # ifdef HAVE_SHADOW_H
718 struct spwd
*spw
; /* Shadow password data */
719 # endif /* HAVE_SHADOW_H */
722 pw
= getpwnam(username
); /* Get the current password */
723 endpwent(); /* Close the password file */
731 cupsdLogMessage(CUPSD_LOG_ERROR
,
732 "cupsdAuthorize: Unknown username \"%s\"!",
737 # ifdef HAVE_SHADOW_H
738 spw
= getspnam(username
);
741 if (!spw
&& !strcmp(pw
->pw_passwd
, "x"))
744 * Don't allow blank passwords!
747 cupsdLogMessage(CUPSD_LOG_ERROR
,
748 "cupsdAuthorize: Username \"%s\" has no shadow "
749 "password!", username
);
753 if (spw
&& !spw
->sp_pwdp
[0] && !pw
->pw_passwd
[0])
755 if (!pw
->pw_passwd
[0])
756 # endif /* HAVE_SHADOW_H */
759 * Don't allow blank passwords!
762 cupsdLogMessage(CUPSD_LOG_ERROR
,
763 "cupsdAuthorize: Username \"%s\" has no password!",
769 * OK, the password isn't blank, so compare with what came from the
773 pass
= cups_crypt(password
, pw
->pw_passwd
);
775 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
776 "cupsdAuthorize: pw_passwd=\"%s\", crypt=\"%s\"",
777 pw
->pw_passwd
, pass
);
779 if (!pass
|| strcmp(pw
->pw_passwd
, pass
))
781 # ifdef HAVE_SHADOW_H
784 pass
= cups_crypt(password
, spw
->sp_pwdp
);
786 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
787 "cupsdAuthorize: sp_pwdp=\"%s\", crypt=\"%s\"",
790 if (pass
== NULL
|| strcmp(spw
->sp_pwdp
, pass
))
792 cupsdLogMessage(CUPSD_LOG_ERROR
,
793 "cupsdAuthorize: Authentication failed for "
800 # endif /* HAVE_SHADOW_H */
802 cupsdLogMessage(CUPSD_LOG_ERROR
,
803 "cupsdAuthorize: Authentication failed for "
809 #endif /* HAVE_LIBPAM */
812 cupsdLogMessage(CUPSD_LOG_DEBUG
,
813 "cupsdAuthorize: Authorized as %s using Basic",
817 case AUTH_BASICDIGEST
:
819 * Do Basic authentication with the Digest password file...
822 if (!get_md5_password(username
, NULL
, md5
))
824 cupsdLogMessage(CUPSD_LOG_ERROR
,
825 "cupsdAuthorize: Unknown MD5 username \"%s\"!",
830 httpMD5(username
, "CUPS", password
, basicmd5
);
832 if (strcmp(md5
, basicmd5
))
834 cupsdLogMessage(CUPSD_LOG_ERROR
,
835 "cupsdAuthorize: Authentication failed for \"%s\"!",
840 cupsdLogMessage(CUPSD_LOG_DEBUG
,
841 "cupsdAuthorize: Authorized as %s using BasicDigest",
846 else if (!strncmp(authorization
, "Digest", 6) && type
== AUTH_DIGEST
)
849 * Get the username, password, and nonce from the Digest attributes...
852 if (!httpGetSubField2(&(con
->http
), HTTP_FIELD_AUTHORIZATION
, "username",
853 username
, sizeof(username
)) || !username
[0])
856 * Username must not be empty...
859 cupsdLogMessage(CUPSD_LOG_ERROR
,
860 "cupsdAuthorize: Empty or missing Digest username!");
864 if (!httpGetSubField2(&(con
->http
), HTTP_FIELD_AUTHORIZATION
, "response",
865 password
, sizeof(password
)) || !password
[0])
868 * Password must not be empty...
871 cupsdLogMessage(CUPSD_LOG_ERROR
,
872 "cupsdAuthorize: Empty or missing Digest password!");
876 if (!httpGetSubField(&(con
->http
), HTTP_FIELD_AUTHORIZATION
, "nonce",
879 cupsdLogMessage(CUPSD_LOG_ERROR
,
880 "cupsdAuthorize: No nonce value for Digest "
885 if (strcmp(con
->http
.hostname
, nonce
))
887 cupsdLogMessage(CUPSD_LOG_ERROR
,
888 "cupsdAuthorize: Bad nonce value, expected \"%s\", "
889 "got \"%s\"!", con
->http
.hostname
, nonce
);
894 * Validate the username and password...
897 if (!get_md5_password(username
, NULL
, md5
))
899 cupsdLogMessage(CUPSD_LOG_ERROR
,
900 "cupsdAuthorize: Unknown MD5 username \"%s\"!",
905 httpMD5Final(nonce
, states
[con
->http
.state
], con
->uri
, md5
);
907 if (strcmp(md5
, password
))
909 cupsdLogMessage(CUPSD_LOG_ERROR
,
910 "cupsdAuthorize: Authentication failed for \"%s\"!",
915 cupsdLogMessage(CUPSD_LOG_DEBUG
,
916 "cupsdAuthorize: Authorized as %s using Digest",
920 else if (!strncmp(authorization
, "Negotiate", 9))
922 int len
; /* Length of authorization string */
923 gss_cred_id_t server_creds
; /* Server credentials */
924 gss_ctx_id_t context
; /* Authorization context */
925 OM_uint32 major_status
, /* Major status code */
926 minor_status
; /* Minor status code */
927 gss_buffer_desc input_token
= GSS_C_EMPTY_BUFFER
,
928 /* Input token from string */
929 output_token
= GSS_C_EMPTY_BUFFER
;
930 /* Output token for username */
931 gss_name_t client_name
; /* Client name */
932 unsigned int ret_flags
; /* Credential flags */
937 * If the weak-linked GSSAPI/Kerberos library is not present, don't try
941 if (gss_init_sec_context
== NULL
)
943 cupsdLogMessage(CUPSD_LOG_WARN
,
944 "GSSAPI/Kerberos authentication failed because the "
945 "Kerberos framework is not present.");
948 # endif /* __APPLE__ */
950 con
->gss_output_token
.length
= 0;
953 * Find the start of the Kerberos input token...
957 while (isspace(*authorization
& 255))
962 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
963 "cupsdAuthorize: No authentication data specified.");
968 * Get the server credentials...
971 if ((server_creds
= get_gss_creds(GSSServiceName
)) == NULL
)
975 * Decode the authorization string to get the input token...
978 len
= strlen(authorization
);
979 input_token
.value
= malloc(len
);
980 input_token
.value
= httpDecode64_2(input_token
.value
, &len
,
982 input_token
.length
= len
;
985 * Accept the input token to get the authorization info...
988 context
= GSS_C_NO_CONTEXT
;
989 client_name
= GSS_C_NO_NAME
;
990 major_status
= gss_accept_sec_context(&minor_status
,
994 GSS_C_NO_CHANNEL_BINDINGS
,
997 &con
->gss_output_token
,
1000 &con
->gss_delegated_cred
);
1002 if (GSS_ERROR(major_status
))
1004 cupsdLogGSSMessage(CUPSD_LOG_DEBUG
, major_status
, minor_status
,
1005 "cupsdAuthorize: Error accepting GSSAPI security "
1008 if (context
!= GSS_C_NO_CONTEXT
)
1009 gss_delete_sec_context(&minor_status
, &context
, GSS_C_NO_BUFFER
);
1011 gss_release_cred(&minor_status
, &server_creds
);
1016 * Release our credentials...
1019 gss_release_cred(&minor_status
, &server_creds
);
1022 * Get the username associated with the client's credentials...
1025 if (!con
->gss_delegated_cred
)
1026 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1027 "cupsdAuthorize: No delegated credentials!");
1029 if (major_status
== GSS_S_CONTINUE_NEEDED
)
1030 cupsdLogGSSMessage(CUPSD_LOG_DEBUG
, major_status
, minor_status
,
1031 "cupsdAuthorize: Credentials not complete");
1032 else if (major_status
== GSS_S_COMPLETE
)
1034 major_status
= gss_display_name(&minor_status
, client_name
,
1035 &output_token
, NULL
);
1037 if (GSS_ERROR(major_status
))
1039 cupsdLogGSSMessage(CUPSD_LOG_DEBUG
, major_status
, minor_status
,
1040 "cupsdAuthorize: Error getting username");
1041 gss_release_name(&minor_status
, &client_name
);
1042 gss_delete_sec_context(&minor_status
, &context
, GSS_C_NO_BUFFER
);
1046 gss_release_name(&minor_status
, &client_name
);
1047 strlcpy(username
, output_token
.value
, sizeof(username
));
1049 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1050 "cupsdAuthorize: Authorized as %s using Negotiate",
1053 gss_release_buffer(&minor_status
, &output_token
);
1054 gss_delete_sec_context(&minor_status
, &context
, GSS_C_NO_BUFFER
);
1056 con
->gss_have_creds
= 1;
1059 gss_release_name(&minor_status
, &client_name
);
1061 #endif /* HAVE_GSSAPI */
1062 else if (type
!= AUTH_NONE
)
1064 char scheme
[256]; /* Auth scheme... */
1065 static const char * const types
[] = /* Auth types */
1075 if (sscanf(authorization
, "%255s", scheme
) != 1)
1076 strcpy(scheme
, "UNKNOWN");
1078 cupsdLogMessage(CUPSD_LOG_ERROR
,
1079 "Bad authentication data \"%s ...\", expected \"%s ...\"",
1080 scheme
, types
[type
]);
1085 * If we get here, then we were able to validate the username and
1086 * password - copy the validated username and password to the client
1087 * data and return...
1090 strlcpy(con
->username
, username
, sizeof(con
->username
));
1091 strlcpy(con
->password
, password
, sizeof(con
->password
));
1096 * 'cupsdCheckAuth()' - Check authorization masks.
1099 int /* O - 1 if mask matches, 0 otherwise */
1101 unsigned ip
[4], /* I - Client address */
1102 char *name
, /* I - Client hostname */
1103 int name_len
, /* I - Length of hostname */
1104 int num_masks
, /* I - Number of masks */
1105 cupsd_authmask_t
*masks
) /* I - Masks */
1107 int i
; /* Looping var */
1108 cupsd_netif_t
*iface
; /* Network interface */
1109 unsigned netip4
; /* IPv4 network address */
1111 unsigned netip6
[4]; /* IPv6 network address */
1112 #endif /* AF_INET6 */
1114 while (num_masks
> 0)
1116 switch (masks
->type
)
1118 case AUTH_INTERFACE
:
1120 * Check for a match with a network interface...
1123 netip4
= htonl(ip
[3]);
1126 netip6
[0] = htonl(ip
[0]);
1127 netip6
[1] = htonl(ip
[1]);
1128 netip6
[2] = htonl(ip
[2]);
1129 netip6
[3] = htonl(ip
[3]);
1130 #endif /* AF_INET6 */
1132 if (!strcmp(masks
->mask
.name
.name
, "*"))
1135 * Check against all local interfaces...
1140 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
1142 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
1145 * Only check local interfaces...
1148 if (!iface
->is_local
)
1151 if (iface
->address
.addr
.sa_family
== AF_INET
)
1154 * Check IPv4 address...
1157 if ((netip4
& iface
->mask
.ipv4
.sin_addr
.s_addr
) ==
1158 (iface
->address
.ipv4
.sin_addr
.s_addr
&
1159 iface
->mask
.ipv4
.sin_addr
.s_addr
))
1166 * Check IPv6 address...
1169 for (i
= 0; i
< 4; i
++)
1170 if ((netip6
[i
] & iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]) !=
1171 (iface
->address
.ipv6
.sin6_addr
.s6_addr32
[i
] &
1172 iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]))
1178 #endif /* AF_INET6 */
1184 * Check the named interface...
1187 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
1189 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
1191 if (strcmp(masks
->mask
.name
.name
, iface
->name
))
1194 if (iface
->address
.addr
.sa_family
== AF_INET
)
1197 * Check IPv4 address...
1200 if ((netip4
& iface
->mask
.ipv4
.sin_addr
.s_addr
) ==
1201 (iface
->address
.ipv4
.sin_addr
.s_addr
&
1202 iface
->mask
.ipv4
.sin_addr
.s_addr
))
1209 * Check IPv6 address...
1212 for (i
= 0; i
< 4; i
++)
1213 if ((netip6
[i
] & iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]) !=
1214 (iface
->address
.ipv6
.sin6_addr
.s6_addr32
[i
] &
1215 iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]))
1221 #endif /* AF_INET6 */
1228 * Check for exact name match...
1231 if (!strcasecmp(name
, masks
->mask
.name
.name
))
1235 * Check for domain match...
1238 if (name_len
>= masks
->mask
.name
.length
&&
1239 masks
->mask
.name
.name
[0] == '.' &&
1240 !strcasecmp(name
+ name_len
- masks
->mask
.name
.length
,
1241 masks
->mask
.name
.name
))
1247 * Check for IP/network address match...
1250 for (i
= 0; i
< 4; i
++)
1251 if ((ip
[i
] & masks
->mask
.ip
.netmask
[i
]) !=
1252 masks
->mask
.ip
.address
[i
])
1269 * 'cupsdCheckGroup()' - Check for a user's group membership.
1272 int /* O - 1 if user is a member, 0 otherwise */
1274 const char *username
, /* I - User name */
1275 struct passwd
*user
, /* I - System user info */
1276 const char *groupname
) /* I - Group name */
1278 int i
; /* Looping var */
1279 struct group
*group
; /* System group info */
1280 char junk
[33]; /* MD5 password (not used) */
1281 #ifdef HAVE_MBR_UID_TO_UUID
1282 uuid_t useruuid
, /* UUID for username */
1283 groupuuid
; /* UUID for groupname */
1284 int is_member
; /* True if user is a member of group */
1285 #endif /* HAVE_MBR_UID_TO_UUID */
1288 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1289 "cupsdCheckGroup(username=\"%s\", user=%p, groupname=\"%s\")",
1290 username
, user
, groupname
);
1296 if (!username
|| !groupname
)
1300 * Check to see if the user is a member of the named group...
1303 group
= getgrnam(groupname
);
1309 * Group exists, check it...
1312 for (i
= 0; group
->gr_mem
[i
]; i
++)
1313 if (!strcasecmp(username
, group
->gr_mem
[i
]))
1318 * Group doesn't exist or user not in group list, check the group ID
1319 * against the user's group ID...
1322 if (user
&& group
&& group
->gr_gid
== user
->pw_gid
)
1325 #ifdef HAVE_MBR_UID_TO_UUID
1327 * Check group membership through MacOS X membership API...
1331 if (!mbr_uid_to_uuid(user
->pw_uid
, useruuid
))
1332 if (!mbr_gid_to_uuid(group
->gr_gid
, groupuuid
))
1333 if (!mbr_check_membership(useruuid
, groupuuid
, &is_member
))
1336 #endif /* HAVE_MBR_UID_TO_UUID */
1339 * Username not found, group not found, or user is not part of the
1340 * system group... Check for a user and group in the MD5 password
1344 if (get_md5_password(username
, groupname
, junk
) != NULL
)
1348 * If we get this far, then the user isn't part of the named group...
1356 * 'cupsdCopyLocation()' - Make a copy of a location...
1359 cupsd_location_t
* /* O - New location */
1361 cupsd_location_t
**loc
) /* IO - Original location */
1363 int i
; /* Looping var */
1364 cupsd_location_t
*temp
; /* New location */
1365 char location
[HTTP_MAX_URI
];
1366 /* Location of resource */
1370 * Use a local copy of location because cupsdAddLocation may cause
1371 * this memory to be moved...
1374 strlcpy(location
, (*loc
)->location
, sizeof(location
));
1376 if ((temp
= cupsdAddLocation(location
)) == NULL
)
1380 * Copy the information from the original location to the new one.
1383 temp
->limit
= (*loc
)->limit
;
1384 temp
->order_type
= (*loc
)->order_type
;
1385 temp
->type
= (*loc
)->type
;
1386 temp
->level
= (*loc
)->level
;
1387 temp
->satisfy
= (*loc
)->satisfy
;
1388 temp
->encryption
= (*loc
)->encryption
;
1390 if ((temp
->num_names
= (*loc
)->num_names
) > 0)
1393 * Copy the names array...
1396 if ((temp
->names
= calloc(temp
->num_names
, sizeof(char *))) == NULL
)
1398 cupsdLogMessage(CUPSD_LOG_ERROR
,
1399 "cupsdCopyLocation: Unable to allocate memory for %d names: %s",
1400 temp
->num_names
, strerror(errno
));
1402 cupsdDeleteLocation(temp
);
1406 for (i
= 0; i
< temp
->num_names
; i
++)
1407 if ((temp
->names
[i
] = strdup((*loc
)->names
[i
])) == NULL
)
1409 cupsdLogMessage(CUPSD_LOG_ERROR
,
1410 "cupsdCopyLocation: Unable to copy name \"%s\": %s",
1411 (*loc
)->names
[i
], strerror(errno
));
1413 cupsdDeleteLocation(temp
);
1418 if ((temp
->num_allow
= (*loc
)->num_allow
) > 0)
1421 * Copy allow rules...
1424 if ((temp
->allow
= calloc(temp
->num_allow
, sizeof(cupsd_authmask_t
))) == NULL
)
1426 cupsdLogMessage(CUPSD_LOG_ERROR
,
1427 "cupsdCopyLocation: Unable to allocate memory for %d allow rules: %s",
1428 temp
->num_allow
, strerror(errno
));
1429 cupsdDeleteLocation(temp
);
1433 for (i
= 0; i
< temp
->num_allow
; i
++)
1434 switch (temp
->allow
[i
].type
= (*loc
)->allow
[i
].type
)
1437 temp
->allow
[i
].mask
.name
.length
= (*loc
)->allow
[i
].mask
.name
.length
;
1438 temp
->allow
[i
].mask
.name
.name
= strdup((*loc
)->allow
[i
].mask
.name
.name
);
1440 if (temp
->allow
[i
].mask
.name
.name
== NULL
)
1442 cupsdLogMessage(CUPSD_LOG_ERROR
,
1443 "cupsdCopyLocation: Unable to copy allow name \"%s\": %s",
1444 (*loc
)->allow
[i
].mask
.name
.name
, strerror(errno
));
1445 cupsdDeleteLocation(temp
);
1450 memcpy(&(temp
->allow
[i
].mask
.ip
), &((*loc
)->allow
[i
].mask
.ip
),
1451 sizeof(cupsd_ipmask_t
));
1456 if ((temp
->num_deny
= (*loc
)->num_deny
) > 0)
1459 * Copy deny rules...
1462 if ((temp
->deny
= calloc(temp
->num_deny
, sizeof(cupsd_authmask_t
))) == NULL
)
1464 cupsdLogMessage(CUPSD_LOG_ERROR
,
1465 "cupsdCopyLocation: Unable to allocate memory for %d deny rules: %s",
1466 temp
->num_deny
, strerror(errno
));
1467 cupsdDeleteLocation(temp
);
1471 for (i
= 0; i
< temp
->num_deny
; i
++)
1472 switch (temp
->deny
[i
].type
= (*loc
)->deny
[i
].type
)
1475 temp
->deny
[i
].mask
.name
.length
= (*loc
)->deny
[i
].mask
.name
.length
;
1476 temp
->deny
[i
].mask
.name
.name
= strdup((*loc
)->deny
[i
].mask
.name
.name
);
1478 if (temp
->deny
[i
].mask
.name
.name
== NULL
)
1480 cupsdLogMessage(CUPSD_LOG_ERROR
,
1481 "cupsdCopyLocation: Unable to copy deny name \"%s\": %s",
1482 (*loc
)->deny
[i
].mask
.name
.name
, strerror(errno
));
1483 cupsdDeleteLocation(temp
);
1488 memcpy(&(temp
->deny
[i
].mask
.ip
), &((*loc
)->deny
[i
].mask
.ip
),
1489 sizeof(cupsd_ipmask_t
));
1499 * 'cupsdDeleteAllLocations()' - Free all memory used for location authorization.
1503 cupsdDeleteAllLocations(void)
1505 cupsd_location_t
*loc
; /* Current location */
1509 * Free all of the allow/deny records first...
1512 for (loc
= (cupsd_location_t
*)cupsArrayFirst(Locations
);
1514 loc
= (cupsd_location_t
*)cupsArrayNext(Locations
))
1515 cupsdDeleteLocation(loc
);
1518 * Then free the location array...
1521 cupsArrayDelete(Locations
);
1527 * 'cupsdDeleteLocation()' - Free all memory used by a location.
1531 cupsdDeleteLocation(
1532 cupsd_location_t
*loc
) /* I - Location to delete */
1534 int i
; /* Looping var */
1535 cupsd_authmask_t
*mask
; /* Current mask */
1538 cupsArrayRemove(Locations
, loc
);
1540 for (i
= loc
->num_names
- 1; i
>= 0; i
--)
1541 free(loc
->names
[i
]);
1543 if (loc
->num_names
> 0)
1546 for (i
= loc
->num_allow
, mask
= loc
->allow
; i
> 0; i
--, mask
++)
1547 if (mask
->type
== AUTH_NAME
|| mask
->type
== AUTH_INTERFACE
)
1548 free(mask
->mask
.name
.name
);
1550 if (loc
->num_allow
> 0)
1553 for (i
= loc
->num_deny
, mask
= loc
->deny
; i
> 0; i
--, mask
++)
1554 if (mask
->type
== AUTH_NAME
|| mask
->type
== AUTH_INTERFACE
)
1555 free(mask
->mask
.name
.name
);
1557 if (loc
->num_deny
> 0)
1560 free(loc
->location
);
1566 * 'cupsdDenyHost()' - Add a host name that is not allowed to access the
1571 cupsdDenyHost(cupsd_location_t
*loc
, /* I - Location to add to */
1572 char *name
) /* I - Name of host or domain to add */
1574 cupsd_authmask_t
*temp
; /* New host/domain mask */
1575 char ifname
[32], /* Interface name */
1576 *ifptr
; /* Pointer to end of name */
1579 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdDenyHost(loc=%p(%s), name=\"%s\")",
1580 loc
, loc
->location
? loc
->location
: "nil", name
);
1582 if ((temp
= add_deny(loc
)) == NULL
)
1585 if (!strcasecmp(name
, "@LOCAL"))
1588 * Deny *interface*...
1591 temp
->type
= AUTH_INTERFACE
;
1592 temp
->mask
.name
.name
= strdup("*");
1593 temp
->mask
.name
.length
= 1;
1595 else if (!strncasecmp(name
, "@IF(", 4))
1598 * Deny *interface*...
1601 strlcpy(ifname
, name
+ 4, sizeof(ifname
));
1603 ifptr
= ifname
+ strlen(ifname
);
1605 if (ifptr
[-1] == ')')
1611 temp
->type
= AUTH_INTERFACE
;
1612 temp
->mask
.name
.name
= strdup(ifname
);
1613 temp
->mask
.name
.length
= ifptr
- ifname
;
1621 temp
->type
= AUTH_NAME
;
1622 temp
->mask
.name
.name
= strdup(name
);
1623 temp
->mask
.name
.length
= strlen(name
);
1629 * 'cupsdDenyIP()' - Add an IP address or network that is not allowed to
1630 * access the location.
1634 cupsdDenyIP(cupsd_location_t
*loc
, /* I - Location to add to */
1635 unsigned address
[4],/* I - IP address to add */
1636 unsigned netmask
[4])/* I - Netmask of address */
1638 cupsd_authmask_t
*temp
; /* New host/domain mask */
1641 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1642 "cupsdDenyIP(loc=%p(%s), address=%x:%x:%x:%x, netmask=%x:%x:%x:%x)",
1643 loc
, loc
->location
? loc
->location
: "nil",
1644 address
[0], address
[1], address
[2], address
[3],
1645 netmask
[0], netmask
[1], netmask
[2], netmask
[3]);
1647 if ((temp
= add_deny(loc
)) == NULL
)
1650 temp
->type
= AUTH_IP
;
1651 memcpy(temp
->mask
.ip
.address
, address
, sizeof(temp
->mask
.ip
.address
));
1652 memcpy(temp
->mask
.ip
.netmask
, netmask
, sizeof(temp
->mask
.ip
.netmask
));
1657 * 'cupsdFindBest()' - Find the location entry that best matches the resource.
1660 cupsd_location_t
* /* O - Location that matches */
1661 cupsdFindBest(const char *path
, /* I - Resource path */
1662 http_state_t state
) /* I - HTTP state/request */
1664 char uri
[HTTP_MAX_URI
],
1665 /* URI in request... */
1666 *uriptr
; /* Pointer into URI */
1667 cupsd_location_t
*loc
, /* Current location */
1668 *best
; /* Best match for location so far */
1669 int bestlen
; /* Length of best match */
1670 int limit
; /* Limit field */
1671 static const int limits
[] = /* Map http_status_t to AUTH_LIMIT_xyz */
1691 * First copy the connection URI to a local string so we have drop
1692 * any .ppd extension from the pathname in /printers or /classes
1696 strlcpy(uri
, path
, sizeof(uri
));
1698 if (!strncmp(uri
, "/printers/", 10) ||
1699 !strncmp(uri
, "/classes/", 9))
1702 * Check if the URI has .ppd on the end...
1705 uriptr
= uri
+ strlen(uri
) - 4; /* len > 4 if we get here... */
1707 if (!strcmp(uriptr
, ".ppd"))
1711 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: uri = \"%s\"...", uri
);
1714 * Loop through the list of locations to find a match...
1717 limit
= limits
[state
];
1721 for (loc
= (cupsd_location_t
*)cupsArrayFirst(Locations
);
1723 loc
= (cupsd_location_t
*)cupsArrayNext(Locations
))
1725 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: Location %s Limit %x",
1726 loc
->location
? loc
->location
: "nil", loc
->limit
);
1728 if (!strncmp(uri
, "/printers/", 10) || !strncmp(uri
, "/classes/", 9))
1731 * Use case-insensitive comparison for queue names...
1734 if (loc
->length
> bestlen
&& loc
->location
&&
1735 !strncasecmp(uri
, loc
->location
, loc
->length
) &&
1736 loc
->location
[0] == '/' &&
1737 (limit
& loc
->limit
) != 0)
1740 bestlen
= loc
->length
;
1746 * Use case-sensitive comparison for other URIs...
1749 if (loc
->length
> bestlen
&& loc
->location
&&
1750 !strncmp(uri
, loc
->location
, loc
->length
) &&
1751 loc
->location
[0] == '/' &&
1752 (limit
& loc
->limit
) != 0)
1755 bestlen
= loc
->length
;
1761 * Return the match, if any...
1764 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: best = %s",
1765 best
? best
->location
: "NONE");
1772 * 'cupsdFindLocation()' - Find the named location.
1775 cupsd_location_t
* /* O - Location that matches */
1776 cupsdFindLocation(const char *location
) /* I - Connection */
1778 cupsd_location_t key
; /* Search key */
1781 key
.location
= (char *)location
;
1783 return ((cupsd_location_t
*)cupsArrayFind(Locations
, &key
));
1788 * 'cupsdIsAuthorized()' - Check to see if the user is authorized...
1791 http_status_t
/* O - HTTP_OK if authorized or error code */
1792 cupsdIsAuthorized(cupsd_client_t
*con
, /* I - Connection */
1793 const char *owner
)/* I - Owner of object */
1795 int i
, j
, /* Looping vars */
1796 auth
; /* Authorization status */
1797 unsigned address
[4]; /* Authorization address */
1798 cupsd_location_t
*best
; /* Best match for location so far */
1799 int hostlen
; /* Length of hostname */
1800 char username
[256], /* Username to authorize */
1801 ownername
[256], /* Owner name to authorize */
1802 *ptr
; /* Pointer into username */
1803 struct passwd
*pw
; /* User password data */
1804 static const char * const levels
[] = /* Auth levels */
1810 static const char * const types
[] = /* Auth types */
1820 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1821 "cupsdIsAuthorized: con->uri=\"%s\", con->best=%p(%s)",
1822 con
->uri
, con
->best
, con
->best
? con
->best
->location
?
1823 con
->best
->location
: "(null)" : "");
1825 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1826 "cupsdIsAuthorized: owner=\"%s\"", owner
);
1829 * If there is no "best" authentication rule for this request, then
1830 * access is allowed from the local system and denied from other
1836 if (!strcmp(con
->http
.hostname
, "localhost") ||
1837 !strcmp(con
->http
.hostname
, ServerName
))
1840 return (HTTP_FORBIDDEN
);
1845 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1846 "cupsdIsAuthorized: level=AUTH_%s, type=AUTH_%s, "
1847 "satisfy=AUTH_SATISFY_%s, num_names=%d",
1848 levels
[best
->level
], types
[best
->type
],
1849 best
->satisfy
? "ANY" : "ALL", best
->num_names
);
1851 if (best
->limit
== 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 (con
->http
.hostaddr
->addr
.sa_family
== AF_INET6
)
1863 * Copy IPv6 address...
1866 address
[0] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[0]);
1867 address
[1] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[1]);
1868 address
[2] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[2]);
1869 address
[3] = ntohl(con
->http
.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(con
->http
.hostaddr
->ipv4
.sin_addr
.s_addr
);
1885 memset(address
, 0, sizeof(address
));
1887 hostlen
= strlen(con
->http
.hostname
);
1889 if (!strcasecmp(con
->http
.hostname
, "localhost"))
1892 * Access from localhost (127.0.0.1 or ::1) is always allowed...
1900 * Do authorization checks on the domain/address...
1903 switch (best
->order_type
)
1906 auth
= AUTH_DENY
; /* anti-compiler-warning-code */
1909 case AUTH_ALLOW
: /* Order Deny,Allow */
1912 if (cupsdCheckAuth(address
, con
->http
.hostname
, hostlen
,
1913 best
->num_deny
, best
->deny
))
1916 if (cupsdCheckAuth(address
, con
->http
.hostname
, hostlen
,
1917 best
->num_allow
, best
->allow
))
1921 case AUTH_DENY
: /* Order Allow,Deny */
1924 if (cupsdCheckAuth(address
, con
->http
.hostname
, hostlen
,
1925 best
->num_allow
, best
->allow
))
1928 if (cupsdCheckAuth(address
, con
->http
.hostname
, hostlen
,
1929 best
->num_deny
, best
->deny
))
1935 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: auth=AUTH_%s...",
1936 auth
? "DENY" : "ALLOW");
1938 if (auth
== AUTH_DENY
&& best
->satisfy
== AUTH_SATISFY_ALL
)
1939 return (HTTP_FORBIDDEN
);
1943 * See if encryption is required...
1946 if ((best
->encryption
>= HTTP_ENCRYPT_REQUIRED
&& !con
->http
.tls
&&
1947 strcasecmp(con
->http
.hostname
, "localhost") &&
1948 best
->satisfy
== AUTH_SATISFY_ALL
) &&
1949 !(best
->type
== AUTH_NEGOTIATE
||
1950 (best
->type
== AUTH_NONE
&& DefaultAuthType
== AUTH_NEGOTIATE
)))
1952 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1953 "cupsdIsAuthorized: Need upgrade to TLS...");
1954 return (HTTP_UPGRADE_REQUIRED
);
1956 #endif /* HAVE_SSL */
1959 * Now see what access level is required...
1962 if (best
->level
== AUTH_ANON
|| /* Anonymous access - allow it */
1963 (best
->type
== AUTH_NONE
&& best
->num_names
== 0))
1966 if (!con
->username
[0] && best
->type
== AUTH_NONE
&&
1967 best
->limit
== AUTH_LIMIT_IPP
)
1970 * Check for unauthenticated username...
1973 ipp_attribute_t
*attr
; /* requesting-user-name attribute */
1976 attr
= ippFindAttribute(con
->request
, "requesting-user-name", IPP_TAG_NAME
);
1979 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1980 "cupsdIsAuthorized: requesting-user-name=\"%s\"",
1981 attr
->values
[0].string
.text
);
1982 strlcpy(username
, attr
->values
[0].string
.text
, sizeof(username
));
1984 else if (best
->satisfy
== AUTH_SATISFY_ALL
|| auth
== AUTH_DENY
)
1985 return (HTTP_UNAUTHORIZED
); /* Non-anonymous needs user/pass */
1987 return (HTTP_OK
); /* unless overridden with Satisfy */
1991 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdIsAuthorized: username=\"%s\"",
1994 #ifdef HAVE_AUTHORIZATION_H
1995 if (!con
->username
[0] && !con
->authref
)
1997 if (!con
->username
[0])
1998 #endif /* HAVE_AUTHORIZATION_H */
2000 if (best
->satisfy
== AUTH_SATISFY_ALL
|| auth
== AUTH_DENY
)
2001 return (HTTP_UNAUTHORIZED
); /* Non-anonymous needs user/pass */
2003 return (HTTP_OK
); /* unless overridden with Satisfy */
2006 strlcpy(username
, con
->username
, sizeof(username
));
2010 * OK, got a username. See if we need normal user access, or group
2011 * access... (root always matches)
2014 if (!strcmp(username
, "root"))
2018 * Strip any @domain or @KDC from the username and owner...
2021 if ((ptr
= strchr(username
, '@')) != NULL
)
2026 strlcpy(ownername
, owner
, sizeof(ownername
));
2028 if ((ptr
= strchr(ownername
, '@')) != NULL
)
2032 ownername
[0] = '\0';
2035 * Get the user info...
2040 pw
= getpwnam(username
);
2046 if (best
->level
== AUTH_USER
)
2049 * If there are no names associated with this location, then
2050 * any valid user is OK...
2053 if (best
->num_names
== 0)
2057 * Otherwise check the user list and return OK if this user is
2061 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2062 "cupsdIsAuthorized: Checking user membership...");
2064 #ifdef HAVE_AUTHORIZATION_H
2066 * If an authorization reference was supplied it must match a right name...
2071 for (i
= 0; i
< best
->num_names
; i
++)
2073 if (!strncasecmp(best
->names
[i
], "@AUTHKEY(", 9) &&
2074 check_authref(con
, best
->names
[i
] + 9))
2076 else if (!strcasecmp(best
->names
[i
], "@SYSTEM") &&
2077 SystemGroupAuthKey
&&
2078 check_authref(con
, SystemGroupAuthKey
))
2082 return (HTTP_UNAUTHORIZED
);
2084 #endif /* HAVE_AUTHORIZATION_H */
2086 for (i
= 0; i
< best
->num_names
; i
++)
2088 if (!strcasecmp(best
->names
[i
], "@OWNER") && owner
&&
2089 !strcasecmp(username
, ownername
))
2091 else if (!strcasecmp(best
->names
[i
], "@SYSTEM"))
2093 for (j
= 0; j
< NumSystemGroups
; j
++)
2094 if (cupsdCheckGroup(username
, pw
, SystemGroups
[j
]))
2097 else if (best
->names
[i
][0] == '@')
2099 if (cupsdCheckGroup(username
, pw
, best
->names
[i
] + 1))
2102 else if (!strcasecmp(username
, best
->names
[i
]))
2106 return (HTTP_UNAUTHORIZED
);
2110 * Check to see if this user is in any of the named groups...
2113 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2114 "cupsdIsAuthorized: Checking group membership...");
2117 * Check to see if this user is in any of the named groups...
2120 for (i
= 0; i
< best
->num_names
; i
++)
2122 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2123 "cupsdIsAuthorized: Checking group \"%s\" membership...",
2126 if (!strcasecmp(best
->names
[i
], "@SYSTEM"))
2128 for (j
= 0; j
< NumSystemGroups
; j
++)
2129 if (cupsdCheckGroup(username
, pw
, SystemGroups
[j
]))
2132 else if (cupsdCheckGroup(username
, pw
, best
->names
[i
]))
2137 * The user isn't part of the specified group, so deny access...
2140 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2141 "cupsdIsAuthorized: User not in group(s)!");
2143 return (HTTP_UNAUTHORIZED
);
2148 * 'add_allow()' - Add an allow mask to the location.
2151 static cupsd_authmask_t
* /* O - New mask record */
2152 add_allow(cupsd_location_t
*loc
) /* I - Location to add to */
2154 cupsd_authmask_t
*temp
; /* New mask record */
2165 * Try to allocate memory for the record...
2168 if (loc
->num_allow
== 0)
2169 temp
= malloc(sizeof(cupsd_authmask_t
));
2171 temp
= realloc(loc
->allow
, sizeof(cupsd_authmask_t
) * (loc
->num_allow
+ 1));
2177 temp
+= loc
->num_allow
;
2181 * Clear the mask record and return...
2184 memset(temp
, 0, sizeof(cupsd_authmask_t
));
2190 * 'add_deny()' - Add a deny mask to the location.
2193 static cupsd_authmask_t
* /* O - New mask record */
2194 add_deny(cupsd_location_t
*loc
) /* I - Location to add to */
2196 cupsd_authmask_t
*temp
; /* New mask record */
2207 * Try to allocate memory for the record...
2210 if (loc
->num_deny
== 0)
2211 temp
= malloc(sizeof(cupsd_authmask_t
));
2213 temp
= realloc(loc
->deny
, sizeof(cupsd_authmask_t
) * (loc
->num_deny
+ 1));
2219 temp
+= loc
->num_deny
;
2223 * Clear the mask record and return...
2226 memset(temp
, 0, sizeof(cupsd_authmask_t
));
2231 #ifdef HAVE_AUTHORIZATION_H
2233 * 'check_authref()' - Check if an authorization services reference has the
2237 static int /* O - 1 if right is valid, 0 otherwise */
2238 check_authref(cupsd_client_t
*con
, /* I - Connection */
2239 const char *right
) /* I - Right name */
2241 OSStatus status
; /* OS Status */
2242 AuthorizationItem authright
; /* Authorization right */
2243 AuthorizationRights authrights
; /* Authorization rights */
2244 AuthorizationFlags authflags
; /* Authorization flags */
2248 * Check to see if the user is allowed to perform the task...
2254 authright
.name
= right
;
2255 authright
.valueLength
= 0;
2256 authright
.value
= NULL
;
2257 authright
.flags
= 0;
2259 authrights
.count
= 1;
2260 authrights
.items
= &authright
;
2262 authflags
= kAuthorizationFlagDefaults
|
2263 kAuthorizationFlagExtendRights
;
2265 if ((status
= AuthorizationCopyRights(con
->authref
, &authrights
,
2266 kAuthorizationEmptyEnvironment
,
2267 authflags
, NULL
)) != 0)
2269 cupsdLogMessage(CUPSD_LOG_ERROR
,
2270 "AuthorizationCopyRights(\"%s\") returned %d (%s)",
2271 authright
.name
, (int)status
, cssmErrorString(status
));
2275 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2276 "AuthorizationCopyRights(\"%s\") succeeded!",
2281 #endif /* HAVE_AUTHORIZATION_H */
2285 * 'compare_locations()' - Compare two locations.
2288 static int /* O - Result of comparison */
2289 compare_locations(cupsd_location_t
*a
, /* I - First location */
2290 cupsd_location_t
*b
) /* I - Second location */
2292 return (strcmp(b
->location
, a
->location
));
2296 #if !HAVE_LIBPAM && !defined(HAVE_USERSEC_H)
2298 * 'cups_crypt()' - Encrypt the password using the DES or MD5 algorithms,
2302 static char * /* O - Encrypted password */
2303 cups_crypt(const char *pw
, /* I - Password string */
2304 const char *salt
) /* I - Salt (key) string */
2306 if (!strncmp(salt
, "$1$", 3))
2309 * Use MD5 passwords without the benefit of PAM; this is for
2310 * Slackware Linux, and the algorithm was taken from the
2311 * old shadow-19990827/lib/md5crypt.c source code... :(
2314 int i
; /* Looping var */
2315 unsigned long n
; /* Output number */
2316 int pwlen
; /* Length of password string */
2317 const char *salt_end
; /* End of "salt" data for MD5 */
2318 char *ptr
; /* Pointer into result string */
2319 _cups_md5_state_t state
; /* Primary MD5 state info */
2320 _cups_md5_state_t state2
; /* Secondary MD5 state info */
2321 unsigned char digest
[16]; /* MD5 digest result */
2322 static char result
[120]; /* Final password string */
2326 * Get the salt data between dollar signs, e.g. $1$saltdata$md5.
2327 * Get a maximum of 8 characters of salt data after $1$...
2330 for (salt_end
= salt
+ 3; *salt_end
&& (salt_end
- salt
) < 11; salt_end
++)
2331 if (*salt_end
== '$')
2335 * Compute the MD5 sum we need...
2340 _cupsMD5Init(&state
);
2341 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
2342 _cupsMD5Append(&state
, (unsigned char *)salt
, salt_end
- salt
);
2344 _cupsMD5Init(&state2
);
2345 _cupsMD5Append(&state2
, (unsigned char *)pw
, pwlen
);
2346 _cupsMD5Append(&state2
, (unsigned char *)salt
+ 3, salt_end
- salt
- 3);
2347 _cupsMD5Append(&state2
, (unsigned char *)pw
, pwlen
);
2348 _cupsMD5Finish(&state2
, digest
);
2350 for (i
= pwlen
; i
> 0; i
-= 16)
2351 _cupsMD5Append(&state
, digest
, i
> 16 ? 16 : i
);
2353 for (i
= pwlen
; i
> 0; i
>>= 1)
2354 _cupsMD5Append(&state
, (unsigned char *)((i
& 1) ? "" : pw
), 1);
2356 _cupsMD5Finish(&state
, digest
);
2358 for (i
= 0; i
< 1000; i
++)
2360 _cupsMD5Init(&state
);
2363 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
2365 _cupsMD5Append(&state
, digest
, 16);
2368 _cupsMD5Append(&state
, (unsigned char *)salt
+ 3, salt_end
- salt
- 3);
2371 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
2374 _cupsMD5Append(&state
, digest
, 16);
2376 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
2378 _cupsMD5Finish(&state
, digest
);
2382 * Copy the final sum to the result string and return...
2385 memcpy(result
, salt
, salt_end
- salt
);
2386 ptr
= result
+ (salt_end
- salt
);
2389 for (i
= 0; i
< 5; i
++, ptr
+= 4)
2391 n
= (((digest
[i
] << 8) | digest
[i
+ 6]) << 8);
2394 n
|= digest
[i
+ 12];
2401 to64(ptr
, digest
[11], 2);
2410 * Use the standard crypt() function...
2413 return (crypt(pw
, salt
));
2416 #endif /* !HAVE_LIBPAM && !HAVE_USERSEC_H */
2421 * 'get_gss_creds()' - Obtain GSS credentials.
2424 static gss_cred_id_t
/* O - Server credentials */
2425 get_gss_creds(const char *service_name
) /* I - Service name */
2427 OM_uint32 major_status
, /* Major status code */
2428 minor_status
; /* Minor status code */
2429 gss_name_t server_name
; /* Server name */
2430 gss_cred_id_t server_creds
; /* Server credentials */
2431 gss_buffer_desc token
= GSS_C_EMPTY_BUFFER
;
2432 /* Service name token */
2433 char buf
[1024], /* Service name buffer */
2434 fqdn
[HTTP_MAX_URI
]; /* Hostname of server */
2437 snprintf(buf
, sizeof(buf
), "%s@%s", service_name
,
2438 httpGetHostname(NULL
, fqdn
, sizeof(fqdn
)));
2441 token
.length
= strlen(buf
);
2442 server_name
= GSS_C_NO_NAME
;
2443 major_status
= gss_import_name(&minor_status
, &token
,
2444 GSS_C_NT_HOSTBASED_SERVICE
,
2447 memset(&token
, 0, sizeof(token
));
2449 if (GSS_ERROR(major_status
))
2451 cupsdLogGSSMessage(CUPSD_LOG_WARN
, major_status
, minor_status
,
2452 "gss_import_name() failed");
2456 major_status
= gss_display_name(&minor_status
, server_name
, &token
, NULL
);
2458 if (GSS_ERROR(major_status
))
2460 cupsdLogGSSMessage(CUPSD_LOG_WARN
, major_status
, minor_status
,
2461 "gss_display_name() failed");
2465 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2466 "get_gss_creds: Attempting to acquire credentials for %s...",
2467 (char *)token
.value
);
2469 server_creds
= GSS_C_NO_CREDENTIAL
;
2470 major_status
= gss_acquire_cred(&minor_status
, server_name
, GSS_C_INDEFINITE
,
2471 GSS_C_NO_OID_SET
, GSS_C_ACCEPT
,
2472 &server_creds
, NULL
, NULL
);
2473 if (GSS_ERROR(major_status
))
2475 cupsdLogGSSMessage(CUPSD_LOG_WARN
, major_status
, minor_status
,
2476 "gss_acquire_cred() failed");
2477 gss_release_name(&minor_status
, &server_name
);
2478 gss_release_buffer(&minor_status
, &token
);
2482 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2483 "get_gss_creds: Credentials acquired successfully for %s.",
2484 (char *)token
.value
);
2486 gss_release_name(&minor_status
, &server_name
);
2487 gss_release_buffer(&minor_status
, &token
);
2489 return (server_creds
);
2491 #endif /* HAVE_GSSAPI */
2495 * 'get_md5_password()' - Get an MD5 password.
2498 static char * /* O - MD5 password string */
2499 get_md5_password(const char *username
, /* I - Username */
2500 const char *group
, /* I - Group */
2501 char passwd
[33]) /* O - MD5 password string */
2503 cups_file_t
*fp
; /* passwd.md5 file */
2504 char filename
[1024], /* passwd.md5 filename */
2505 line
[256], /* Line from file */
2506 tempuser
[33], /* User from file */
2507 tempgroup
[33]; /* Group from file */
2510 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2511 "get_md5_password(username=\"%s\", group=\"%s\", passwd=%p)",
2512 username
, group
? group
: "(null)", passwd
);
2514 snprintf(filename
, sizeof(filename
), "%s/passwd.md5", ServerRoot
);
2515 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
2517 if (errno
!= ENOENT
)
2518 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open %s - %s", filename
,
2524 while (cupsFileGets(fp
, line
, sizeof(line
)) != NULL
)
2526 if (sscanf(line
, "%32[^:]:%32[^:]:%32s", tempuser
, tempgroup
, passwd
) != 3)
2528 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad MD5 password line: %s", line
);
2532 if (!strcmp(username
, tempuser
) &&
2533 (group
== NULL
|| !strcmp(group
, tempgroup
)))
2536 * Found the password entry!
2539 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Found MD5 user %s, group %s...",
2540 username
, tempgroup
);
2548 * Didn't find a password entry - return NULL!
2558 * 'pam_func()' - PAM conversation function.
2561 static int /* O - Success or failure */
2563 int num_msg
, /* I - Number of messages */
2564 const struct pam_message
**msg
, /* I - Messages */
2565 struct pam_response
**resp
, /* O - Responses */
2567 /* I - Pointer to connection */
2569 int i
; /* Looping var */
2570 struct pam_response
*replies
; /* Replies */
2571 cupsd_authdata_t
*data
; /* Pointer to auth data */
2575 * Allocate memory for the responses...
2578 if ((replies
= malloc(sizeof(struct pam_response
) * num_msg
)) == NULL
)
2579 return (PAM_CONV_ERR
);
2582 * Answer all of the messages...
2585 DEBUG_printf(("pam_func: appdata_ptr = %p\n", appdata_ptr
));
2589 * Apparently some versions of HP-UX 11 have a broken pam_unix security
2590 * module. This is a workaround...
2596 data
= (cupsd_authdata_t
*)appdata_ptr
;
2599 for (i
= 0; i
< num_msg
; i
++)
2601 DEBUG_printf(("pam_func: Message = \"%s\"\n", msg
[i
]->msg
));
2603 switch (msg
[i
]->msg_style
)
2605 case PAM_PROMPT_ECHO_ON
:
2606 DEBUG_printf(("pam_func: PAM_PROMPT_ECHO_ON, returning \"%s\"...\n",
2608 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2609 replies
[i
].resp
= strdup(data
->username
);
2612 case PAM_PROMPT_ECHO_OFF
:
2613 DEBUG_printf(("pam_func: PAM_PROMPT_ECHO_OFF, returning \"%s\"...\n",
2615 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2616 replies
[i
].resp
= strdup(data
->password
);
2620 DEBUG_puts("pam_func: PAM_TEXT_INFO...");
2621 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2622 replies
[i
].resp
= NULL
;
2626 DEBUG_puts("pam_func: PAM_ERROR_MSG...");
2627 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2628 replies
[i
].resp
= NULL
;
2632 DEBUG_printf(("pam_func: Unknown PAM message %d...\n",
2633 msg
[i
]->msg_style
));
2635 return (PAM_CONV_ERR
);
2640 * Return the responses back to PAM...
2645 return (PAM_SUCCESS
);
2647 #elif !defined(HAVE_USERSEC_H)
2651 * 'to64()' - Base64-encode an integer value...
2655 to64(char *s
, /* O - Output string */
2656 unsigned long v
, /* I - Value to encode */
2657 int n
) /* I - Number of digits */
2659 const char *itoa64
= "./0123456789"
2660 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2661 "abcdefghijklmnopqrstuvwxyz";
2664 for (; n
> 0; n
--, v
>>= 6)
2665 *s
++ = itoa64
[v
& 0x3f];
2667 #endif /* HAVE_LIBPAM */
2671 * End of "$Id: auth.c 6949 2007-09-12 21:33:23Z mike $".