2 * "$Id: auth.c 6758 2007-08-02 00:13:44Z 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_UCRED_H
87 # include <sys/ucred.h>
88 typedef struct xucred cupsd_ucred_t
;
89 # define CUPSD_UCRED_UID(c) (c).cr_uid
91 typedef struct ucred cupsd_ucred_t
;
92 # define CUPSD_UCRED_UID(c) (c).uid
93 #endif /* HAVE_SYS_UCRED_H */
100 static cupsd_authmask_t
*add_allow(cupsd_location_t
*loc
);
101 static cupsd_authmask_t
*add_deny(cupsd_location_t
*loc
);
102 #ifdef HAVE_AUTHORIZATION_H
103 static int check_authref(cupsd_client_t
*con
, const char *right
);
104 #endif /* HAVE_AUTHORIZATION_H */
105 static int compare_locations(cupsd_location_t
*a
,
106 cupsd_location_t
*b
);
107 #if !HAVE_LIBPAM && !defined(HAVE_USERSEC_H)
108 static char *cups_crypt(const char *pw
, const char *salt
);
109 #endif /* !HAVE_LIBPAM && !HAVE_USERSEC_H */
111 static gss_cred_id_t
get_gss_creds(const char *service_name
);
112 #endif /* HAVE_GSSAPI */
113 static char *get_md5_password(const char *username
,
114 const char *group
, char passwd
[33]);
116 static int pam_func(int, const struct pam_message
**,
117 struct pam_response
**, void *);
118 #elif !defined(HAVE_USERSEC_H)
119 static void to64(char *s
, unsigned long v
, int n
);
120 #endif /* HAVE_LIBPAM */
124 * Local structures...
128 typedef struct cupsd_authdata_s
/**** Authentication data ****/
130 char username
[33], /* Username string */
131 password
[33]; /* Password string */
133 #endif /* HAVE_LIBPAM */
140 #if defined(__hpux) && HAVE_LIBPAM
141 static cupsd_authdata_t
*auth_data
; /* Current client being authenticated */
142 #endif /* __hpux && HAVE_LIBPAM */
146 * 'cupsdAddLocation()' - Add a location for authorization.
149 cupsd_location_t
* /* O - Pointer to new location record */
150 cupsdAddLocation(const char *location
) /* I - Location path */
152 cupsd_location_t
*temp
; /* New location */
156 * Make sure the locations array is created...
160 Locations
= cupsArrayNew((cups_array_func_t
)compare_locations
, NULL
);
166 * Try to allocate memory for the new location.
169 if ((temp
= calloc(1, sizeof(cupsd_location_t
))) == NULL
)
173 * Initialize the record and copy the name over...
176 temp
->location
= strdup(location
);
177 temp
->length
= strlen(temp
->location
);
179 cupsArrayAdd(Locations
, temp
);
181 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAddLocation: added location \'%s\'",
185 * Return the new record...
193 * 'cupsdAddName()' - Add a name to a location...
197 cupsdAddName(cupsd_location_t
*loc
, /* I - Location to add to */
198 char *name
) /* I - Name to add */
200 char **temp
; /* Pointer to names array */
203 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAddName(loc=%p, name=\"%s\")",
206 if (loc
->num_names
== 0)
207 temp
= malloc(sizeof(char *));
209 temp
= realloc(loc
->names
, (loc
->num_names
+ 1) * sizeof(char *));
213 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to add name to location %s: %s",
214 loc
->location
? loc
->location
: "nil", strerror(errno
));
220 if ((temp
[loc
->num_names
] = strdup(name
)) == NULL
)
222 cupsdLogMessage(CUPSD_LOG_ERROR
,
223 "Unable to duplicate name for location %s: %s",
224 loc
->location
? loc
->location
: "nil", strerror(errno
));
233 * 'cupsdAllowHost()' - Add a host name that is allowed to access the location.
237 cupsdAllowHost(cupsd_location_t
*loc
, /* I - Location to add to */
238 char *name
) /* I - Name of host or domain to add */
240 cupsd_authmask_t
*temp
; /* New host/domain mask */
241 char ifname
[32], /* Interface name */
242 *ifptr
; /* Pointer to end of name */
245 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAllowHost(loc=%p(%s), name=\"%s\")",
246 loc
, loc
->location
? loc
->location
: "nil", name
);
248 if ((temp
= add_allow(loc
)) == NULL
)
251 if (!strcasecmp(name
, "@LOCAL"))
254 * Allow *interface*...
257 temp
->type
= AUTH_INTERFACE
;
258 temp
->mask
.name
.name
= strdup("*");
259 temp
->mask
.name
.length
= 1;
261 else if (!strncasecmp(name
, "@IF(", 4))
264 * Allow *interface*...
267 strlcpy(ifname
, name
+ 4, sizeof(ifname
));
269 ifptr
= ifname
+ strlen(ifname
);
271 if (ifptr
[-1] == ')')
277 temp
->type
= AUTH_INTERFACE
;
278 temp
->mask
.name
.name
= strdup(ifname
);
279 temp
->mask
.name
.length
= ifptr
- ifname
;
287 temp
->type
= AUTH_NAME
;
288 temp
->mask
.name
.name
= strdup(name
);
289 temp
->mask
.name
.length
= strlen(name
);
295 * 'cupsdAllowIP()' - Add an IP address or network that is allowed to access
300 cupsdAllowIP(cupsd_location_t
*loc
, /* I - Location to add to */
301 unsigned address
[4], /* I - IP address to add */
302 unsigned netmask
[4]) /* I - Netmask of address */
304 cupsd_authmask_t
*temp
; /* New host/domain mask */
307 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
308 "cupsdAllowIP(loc=%p(%s), address=%x:%x:%x:%x, netmask=%x:%x:%x:%x)",
309 loc
, loc
->location
? loc
->location
: "nil",
310 address
[0], address
[1], address
[2], address
[3],
311 netmask
[0], netmask
[1], netmask
[2], netmask
[3]);
313 if ((temp
= add_allow(loc
)) == NULL
)
316 temp
->type
= AUTH_IP
;
317 memcpy(temp
->mask
.ip
.address
, address
, sizeof(temp
->mask
.ip
.address
));
318 memcpy(temp
->mask
.ip
.netmask
, netmask
, sizeof(temp
->mask
.ip
.netmask
));
323 * 'cupsdAuthorize()' - Validate any authorization credentials.
327 cupsdAuthorize(cupsd_client_t
*con
) /* I - Client connection */
329 int type
; /* Authentication type */
330 const char *authorization
; /* Pointer into Authorization string */
331 char *ptr
, /* Pointer into string */
332 username
[65], /* Username string */
333 password
[33]; /* Password string */
334 const char *localuser
; /* Certificate username */
335 char nonce
[HTTP_MAX_VALUE
], /* Nonce value from client */
336 md5
[33], /* MD5 password */
337 basicmd5
[33]; /* MD5 of Basic password */
338 static const char * const states
[] = /* HTTP client states... */
358 * Locate the best matching location so we know what kind of
359 * authentication to expect...
362 con
->best
= cupsdFindBest(con
->uri
, con
->http
.state
);
364 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
365 "cupsdAuthorize: con->uri=\"%s\", con->best=%p(%s)",
366 con
->uri
, con
->best
, con
->best
? con
->best
->location
: "");
368 if (con
->best
&& con
->best
->type
!= AUTH_NONE
)
369 type
= con
->best
->type
;
371 type
= DefaultAuthType
;
374 * Decode the Authorization string...
377 authorization
= httpGetField(&con
->http
, HTTP_FIELD_AUTHORIZATION
);
379 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAuthorize: Authorization=\"%s\"",
385 #ifdef HAVE_AUTHORIZATION_H
388 AuthorizationFree(con
->authref
, kAuthorizationFlagDefaults
);
391 #endif /* HAVE_AUTHORIZATION_H */
393 if (type
== AUTH_NONE
)
396 * No authorization required, return early...
399 cupsdLogMessage(CUPSD_LOG_DEBUG
,
400 "cupsdAuthorize: No authentication required.");
403 else if (!*authorization
)
406 * No authorization data provided, return early...
409 cupsdLogMessage(CUPSD_LOG_DEBUG
,
410 "cupsdAuthorize: No authentication data provided.");
413 #ifdef HAVE_AUTHORIZATION_H
414 else if (!strncmp(authorization
, "AuthRef", 6) &&
415 !strcasecmp(con
->http
.hostname
, "localhost"))
417 OSStatus status
; /* Status */
418 int authlen
; /* Auth string length */
419 AuthorizationItemSet
*authinfo
; /* Authorization item set */
422 * Get the Authorization Services data...
426 while (isspace(*authorization
& 255))
429 authlen
= sizeof(nonce
);
430 httpDecode64_2(nonce
, &authlen
, authorization
);
432 if (authlen
!= kAuthorizationExternalFormLength
)
434 cupsdLogMessage(CUPSD_LOG_ERROR
,
435 "External Authorization reference size is incorrect!");
439 if ((status
= AuthorizationCreateFromExternalForm(
440 (AuthorizationExternalForm
*)nonce
, &con
->authref
)) != 0)
442 cupsdLogMessage(CUPSD_LOG_ERROR
,
443 "AuthorizationCreateFromExternalForm returned %d (%s)",
444 (int)status
, cssmErrorString(status
));
448 if ((status
= AuthorizationCopyInfo(con
->authref
,
449 kAuthorizationEnvironmentUsername
,
452 cupsdLogMessage(CUPSD_LOG_ERROR
,
453 "AuthorizationCopyInfo returned %d (%s)",
454 (int)status
, cssmErrorString(status
));
458 if (authinfo
->count
== 1)
459 strlcpy(username
, authinfo
->items
[0].value
, sizeof(username
));
461 cupsdLogMessage(CUPSD_LOG_DEBUG
,
462 "cupsdAuthorize: Authorized as %s using AuthRef",
465 AuthorizationFreeItemSet(authinfo
);
467 #endif /* HAVE_AUTHORIZATION_H */
468 #if defined(SO_PEERCRED) && defined(AF_LOCAL)
469 else if (!strncmp(authorization
, "PeerCred ", 9) &&
470 con
->http
.hostaddr
->addr
.sa_family
== AF_LOCAL
)
473 * Use peer credentials from domain socket connection...
476 struct passwd
*pwd
; /* Password entry for this user */
477 cupsd_ucred_t peercred
; /* Peer credentials */
478 socklen_t peersize
; /* Size of peer credentials */
481 if ((pwd
= getpwnam(authorization
+ 9)) == NULL
)
483 cupsdLogMessage(CUPSD_LOG_ERROR
, "User \"%s\" does not exist!",
488 peersize
= sizeof(peercred
);
490 if (getsockopt(con
->http
.fd
, SOL_SOCKET
, SO_PEERCRED
, &peercred
, &peersize
))
492 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to get peer credentials - %s",
497 if (pwd
->pw_uid
!= CUPSD_UCRED_UID(peercred
))
499 cupsdLogMessage(CUPSD_LOG_ERROR
,
500 "Invalid peer credentials for \"%s\" - got %d, "
501 "expected %d!", authorization
+ 9,
502 CUPSD_UCRED_UID(peercred
), pwd
->pw_uid
);
503 # ifdef HAVE_SYS_UCRED_H
504 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAuthorize: cr_version=%d",
505 peercred
.cr_version
);
506 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAuthorize: cr_uid=%d",
508 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAuthorize: cr_ngroups=%d",
509 peercred
.cr_ngroups
);
510 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAuthorize: cr_groups[0]=%d",
511 peercred
.cr_groups
[0]);
512 # endif /* HAVE_SYS_UCRED_H */
516 strlcpy(username
, authorization
+ 9, sizeof(username
));
518 cupsdLogMessage(CUPSD_LOG_DEBUG
,
519 "cupsdAuthorize: Authorized as %s using PeerCred",
522 #endif /* SO_PEERCRED && AF_LOCAL */
523 else if (!strncmp(authorization
, "Local", 5) &&
524 !strcasecmp(con
->http
.hostname
, "localhost"))
527 * Get Local certificate authentication data...
531 while (isspace(*authorization
& 255))
534 if ((localuser
= cupsdFindCert(authorization
)) != NULL
)
536 strlcpy(username
, localuser
, sizeof(username
));
538 cupsdLogMessage(CUPSD_LOG_DEBUG
,
539 "cupsdAuthorize: Authorized as %s using Local",
544 cupsdLogMessage(CUPSD_LOG_ERROR
,
545 "cupsdAuthorize: Local authentication certificate not "
550 else if (!strncmp(authorization
, "Basic", 5) &&
551 (type
== AUTH_BASIC
|| type
== AUTH_BASICDIGEST
))
554 * Get the Basic authentication data...
557 int userlen
; /* Username:password length */
561 while (isspace(*authorization
& 255))
564 userlen
= sizeof(username
);
565 httpDecode64_2(username
, &userlen
, authorization
);
568 * Pull the username and password out...
571 if ((ptr
= strchr(username
, ':')) == NULL
)
573 cupsdLogMessage(CUPSD_LOG_ERROR
,
574 "cupsdAuthorize: Missing Basic password!");
583 * Username must not be empty...
586 cupsdLogMessage(CUPSD_LOG_ERROR
,
587 "cupsdAuthorize: Empty Basic username!");
594 * Password must not be empty...
597 cupsdLogMessage(CUPSD_LOG_ERROR
,
598 "cupsdAuthorize: Empty Basic password!");
602 strlcpy(password
, ptr
, sizeof(password
));
605 * Validate the username and password...
614 * Only use PAM to do authentication. This supports MD5
615 * passwords, among other things...
618 pam_handle_t
*pamh
; /* PAM authentication handle */
619 int pamerr
; /* PAM error code */
620 struct pam_conv pamdata
;/* PAM conversation data */
621 cupsd_authdata_t data
; /* Authentication data */
624 strlcpy(data
.username
, username
, sizeof(data
.username
));
625 strlcpy(data
.password
, password
, sizeof(data
.password
));
627 # if defined(__sun) || defined(__hpux)
628 pamdata
.conv
= (int (*)(int, struct pam_message
**,
629 struct pam_response
**,
632 pamdata
.conv
= pam_func
;
633 # endif /* __sun || __hpux */
634 pamdata
.appdata_ptr
= &data
;
638 * Workaround for HP-UX bug in pam_unix; see pam_func() below for
645 pamerr
= pam_start("cups", username
, &pamdata
, &pamh
);
646 if (pamerr
!= PAM_SUCCESS
)
648 cupsdLogMessage(CUPSD_LOG_ERROR
,
649 "cupsdAuthorize: pam_start() returned %d (%s)!\n",
650 pamerr
, pam_strerror(pamh
, pamerr
));
655 pamerr
= pam_authenticate(pamh
, PAM_SILENT
);
656 if (pamerr
!= PAM_SUCCESS
)
658 cupsdLogMessage(CUPSD_LOG_ERROR
,
659 "cupsdAuthorize: pam_authenticate() returned %d "
661 pamerr
, pam_strerror(pamh
, pamerr
));
666 pamerr
= pam_acct_mgmt(pamh
, PAM_SILENT
);
667 if (pamerr
!= PAM_SUCCESS
)
669 cupsdLogMessage(CUPSD_LOG_ERROR
,
670 "cupsdAuthorize: pam_acct_mgmt() returned %d "
672 pamerr
, pam_strerror(pamh
, pamerr
));
677 pam_end(pamh
, PAM_SUCCESS
);
679 #elif defined(HAVE_USERSEC_H)
681 * Use AIX authentication interface...
684 char *authmsg
; /* Authentication message */
685 int reenter
; /* ??? */
688 cupsdLogMessage(CUPSD_LOG_DEBUG
,
689 "cupsdAuthorize: AIX authenticate of username \"%s\"",
693 if (authenticate(username
, password
, &reenter
, &authmsg
) != 0)
695 cupsdLogMessage(CUPSD_LOG_DEBUG
,
696 "cupsdAuthorize: Unable to authenticate username "
698 username
, strerror(errno
));
704 * Use normal UNIX password file-based authentication...
707 char *pass
; /* Encrypted password */
708 struct passwd
*pw
; /* User password data */
709 # ifdef HAVE_SHADOW_H
710 struct spwd
*spw
; /* Shadow password data */
711 # endif /* HAVE_SHADOW_H */
714 pw
= getpwnam(username
); /* Get the current password */
715 endpwent(); /* Close the password file */
723 cupsdLogMessage(CUPSD_LOG_ERROR
,
724 "cupsdAuthorize: Unknown username \"%s\"!",
729 # ifdef HAVE_SHADOW_H
730 spw
= getspnam(username
);
733 if (!spw
&& !strcmp(pw
->pw_passwd
, "x"))
736 * Don't allow blank passwords!
739 cupsdLogMessage(CUPSD_LOG_ERROR
,
740 "cupsdAuthorize: Username \"%s\" has no shadow "
741 "password!", username
);
745 if (spw
&& !spw
->sp_pwdp
[0] && !pw
->pw_passwd
[0])
747 if (!pw
->pw_passwd
[0])
748 # endif /* HAVE_SHADOW_H */
751 * Don't allow blank passwords!
754 cupsdLogMessage(CUPSD_LOG_ERROR
,
755 "cupsdAuthorize: Username \"%s\" has no password!",
761 * OK, the password isn't blank, so compare with what came from the
765 pass
= cups_crypt(password
, pw
->pw_passwd
);
767 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
768 "cupsdAuthorize: pw_passwd=\"%s\", crypt=\"%s\"",
769 pw
->pw_passwd
, pass
);
771 if (!pass
|| strcmp(pw
->pw_passwd
, pass
))
773 # ifdef HAVE_SHADOW_H
776 pass
= cups_crypt(password
, spw
->sp_pwdp
);
778 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
779 "cupsdAuthorize: sp_pwdp=\"%s\", crypt=\"%s\"",
782 if (pass
== NULL
|| strcmp(spw
->sp_pwdp
, pass
))
784 cupsdLogMessage(CUPSD_LOG_ERROR
,
785 "cupsdAuthorize: Authentication failed for "
792 # endif /* HAVE_SHADOW_H */
794 cupsdLogMessage(CUPSD_LOG_ERROR
,
795 "cupsdAuthorize: Authentication failed for "
801 #endif /* HAVE_LIBPAM */
804 cupsdLogMessage(CUPSD_LOG_DEBUG
,
805 "cupsdAuthorize: Authorized as %s using Basic",
809 case AUTH_BASICDIGEST
:
811 * Do Basic authentication with the Digest password file...
814 if (!get_md5_password(username
, NULL
, md5
))
816 cupsdLogMessage(CUPSD_LOG_ERROR
,
817 "cupsdAuthorize: Unknown MD5 username \"%s\"!",
822 httpMD5(username
, "CUPS", password
, basicmd5
);
824 if (strcmp(md5
, basicmd5
))
826 cupsdLogMessage(CUPSD_LOG_ERROR
,
827 "cupsdAuthorize: Authentication failed for \"%s\"!",
832 cupsdLogMessage(CUPSD_LOG_DEBUG
,
833 "cupsdAuthorize: Authorized as %s using BasicDigest",
838 else if (!strncmp(authorization
, "Digest", 6) && type
== AUTH_DIGEST
)
841 * Get the username, password, and nonce from the Digest attributes...
844 if (!httpGetSubField2(&(con
->http
), HTTP_FIELD_AUTHORIZATION
, "username",
845 username
, sizeof(username
)) || !username
[0])
848 * Username must not be empty...
851 cupsdLogMessage(CUPSD_LOG_ERROR
,
852 "cupsdAuthorize: Empty or missing Digest username!");
856 if (!httpGetSubField2(&(con
->http
), HTTP_FIELD_AUTHORIZATION
, "response",
857 password
, sizeof(password
)) || !password
[0])
860 * Password must not be empty...
863 cupsdLogMessage(CUPSD_LOG_ERROR
,
864 "cupsdAuthorize: Empty or missing Digest password!");
868 if (!httpGetSubField(&(con
->http
), HTTP_FIELD_AUTHORIZATION
, "nonce",
871 cupsdLogMessage(CUPSD_LOG_ERROR
,
872 "cupsdAuthorize: No nonce value for Digest "
877 if (strcmp(con
->http
.hostname
, nonce
))
879 cupsdLogMessage(CUPSD_LOG_ERROR
,
880 "cupsdAuthorize: Bad nonce value, expected \"%s\", "
881 "got \"%s\"!", con
->http
.hostname
, nonce
);
886 * Validate the username and password...
889 if (!get_md5_password(username
, NULL
, md5
))
891 cupsdLogMessage(CUPSD_LOG_ERROR
,
892 "cupsdAuthorize: Unknown MD5 username \"%s\"!",
897 httpMD5Final(nonce
, states
[con
->http
.state
], con
->uri
, md5
);
899 if (strcmp(md5
, password
))
901 cupsdLogMessage(CUPSD_LOG_ERROR
,
902 "cupsdAuthorize: Authentication failed for \"%s\"!",
907 cupsdLogMessage(CUPSD_LOG_DEBUG
,
908 "cupsdAuthorize: Authorized as %s using Digest",
912 else if (!strncmp(authorization
, "Negotiate", 9) && type
== AUTH_NEGOTIATE
)
914 int len
; /* Length of authorization string */
915 gss_cred_id_t server_creds
; /* Server credentials */
916 gss_ctx_id_t context
; /* Authorization context */
917 OM_uint32 major_status
, /* Major status code */
918 minor_status
; /* Minor status code */
919 gss_buffer_desc input_token
= GSS_C_EMPTY_BUFFER
,
920 /* Input token from string */
921 output_token
= GSS_C_EMPTY_BUFFER
;
922 /* Output token for username */
923 gss_name_t client_name
; /* Client name */
924 unsigned int ret_flags
; /* Credential flags */
929 * If the weak-linked GSSAPI/Kerberos library is not present, don't try
933 if (gss_init_sec_context
== NULL
)
935 cupsdLogMessage(CUPSD_LOG_WARN
,
936 "GSSAPI/Kerberos authentication failed because the "
937 "Kerberos framework is not present.");
940 # endif /* __APPLE__ */
942 con
->gss_output_token
.length
= 0;
945 * Find the start of the Kerberos input token...
949 while (isspace(*authorization
& 255))
954 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
955 "cupsdAuthorize: No authentication data specified.");
960 * Get the server credentials...
963 if ((server_creds
= get_gss_creds(GSSServiceName
)) == NULL
)
967 * Decode the authorization string to get the input token...
970 len
= strlen(authorization
);
971 input_token
.value
= malloc(len
);
972 input_token
.value
= httpDecode64_2(input_token
.value
, &len
,
974 input_token
.length
= len
;
977 * Accept the input token to get the authorization info...
980 context
= GSS_C_NO_CONTEXT
;
981 client_name
= GSS_C_NO_NAME
;
982 major_status
= gss_accept_sec_context(&minor_status
,
986 GSS_C_NO_CHANNEL_BINDINGS
,
989 &con
->gss_output_token
,
992 &con
->gss_delegated_cred
);
994 if (GSS_ERROR(major_status
))
996 cupsdLogGSSMessage(CUPSD_LOG_DEBUG
, major_status
, minor_status
,
997 "cupsdAuthorize: Error accepting GSSAPI security "
1000 if (context
!= GSS_C_NO_CONTEXT
)
1001 gss_delete_sec_context(&minor_status
, &context
, GSS_C_NO_BUFFER
);
1006 * Get the username associated with the credentials...
1009 if (!con
->gss_delegated_cred
)
1010 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1011 "cupsdAuthorize: No delegated credentials!");
1013 if (major_status
== GSS_S_CONTINUE_NEEDED
)
1014 cupsdLogGSSMessage(CUPSD_LOG_DEBUG
, major_status
, minor_status
,
1015 "cupsdAuthorize: Credentials not complete");
1016 else if (major_status
== GSS_S_COMPLETE
)
1018 major_status
= gss_display_name(&minor_status
, client_name
,
1019 &output_token
, NULL
);
1021 if (GSS_ERROR(major_status
))
1023 cupsdLogGSSMessage(CUPSD_LOG_DEBUG
, major_status
, minor_status
,
1024 "cupsdAuthorize: Error getting username");
1025 gss_release_name(&minor_status
, &client_name
);
1026 gss_delete_sec_context(&minor_status
, &context
, GSS_C_NO_BUFFER
);
1030 gss_release_name(&minor_status
, &client_name
);
1031 strlcpy(username
, output_token
.value
, sizeof(username
));
1032 if ((ptr
= strchr(username
, '@')) != NULL
)
1033 *ptr
= '\0'; /* Strip @KDC from the username */
1035 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1036 "cupsdAuthorize: Authorized as %s using Negotiate",
1039 gss_release_buffer(&minor_status
, &output_token
);
1040 gss_delete_sec_context(&minor_status
, &context
, GSS_C_NO_BUFFER
);
1042 con
->gss_have_creds
= 1;
1045 gss_release_name(&minor_status
, &client_name
);
1047 #endif /* HAVE_GSSAPI */
1048 else if (type
!= AUTH_NONE
)
1050 char scheme
[256]; /* Auth scheme... */
1051 static const char * const types
[] = /* Auth types */
1061 if (sscanf(authorization
, "%255s", scheme
) != 1)
1062 strcpy(scheme
, "UNKNOWN");
1064 cupsdLogMessage(CUPSD_LOG_ERROR
,
1065 "Bad authentication data \"%s ...\", expected \"%s ...\"",
1066 scheme
, types
[type
]);
1071 * If we get here, then we were able to validate the username and
1072 * password - copy the validated username and password to the client
1073 * data and return...
1076 strlcpy(con
->username
, username
, sizeof(con
->username
));
1077 strlcpy(con
->password
, password
, sizeof(con
->password
));
1082 * 'cupsdCheckAuth()' - Check authorization masks.
1085 int /* O - 1 if mask matches, 0 otherwise */
1087 unsigned ip
[4], /* I - Client address */
1088 char *name
, /* I - Client hostname */
1089 int name_len
, /* I - Length of hostname */
1090 int num_masks
, /* I - Number of masks */
1091 cupsd_authmask_t
*masks
) /* I - Masks */
1093 int i
; /* Looping var */
1094 cupsd_netif_t
*iface
; /* Network interface */
1095 unsigned netip4
; /* IPv4 network address */
1097 unsigned netip6
[4]; /* IPv6 network address */
1098 #endif /* AF_INET6 */
1100 while (num_masks
> 0)
1102 switch (masks
->type
)
1104 case AUTH_INTERFACE
:
1106 * Check for a match with a network interface...
1109 netip4
= htonl(ip
[3]);
1112 netip6
[0] = htonl(ip
[0]);
1113 netip6
[1] = htonl(ip
[1]);
1114 netip6
[2] = htonl(ip
[2]);
1115 netip6
[3] = htonl(ip
[3]);
1116 #endif /* AF_INET6 */
1118 if (!strcmp(masks
->mask
.name
.name
, "*"))
1121 * Check against all local interfaces...
1126 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
1128 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
1131 * Only check local interfaces...
1134 if (!iface
->is_local
)
1137 if (iface
->address
.addr
.sa_family
== AF_INET
)
1140 * Check IPv4 address...
1143 if ((netip4
& iface
->mask
.ipv4
.sin_addr
.s_addr
) ==
1144 (iface
->address
.ipv4
.sin_addr
.s_addr
&
1145 iface
->mask
.ipv4
.sin_addr
.s_addr
))
1152 * Check IPv6 address...
1155 for (i
= 0; i
< 4; i
++)
1156 if ((netip6
[i
] & iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]) !=
1157 (iface
->address
.ipv6
.sin6_addr
.s6_addr32
[i
] &
1158 iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]))
1164 #endif /* AF_INET6 */
1170 * Check the named interface...
1173 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
1175 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
1177 if (strcmp(masks
->mask
.name
.name
, iface
->name
))
1180 if (iface
->address
.addr
.sa_family
== AF_INET
)
1183 * Check IPv4 address...
1186 if ((netip4
& iface
->mask
.ipv4
.sin_addr
.s_addr
) ==
1187 (iface
->address
.ipv4
.sin_addr
.s_addr
&
1188 iface
->mask
.ipv4
.sin_addr
.s_addr
))
1195 * Check IPv6 address...
1198 for (i
= 0; i
< 4; i
++)
1199 if ((netip6
[i
] & iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]) !=
1200 (iface
->address
.ipv6
.sin6_addr
.s6_addr32
[i
] &
1201 iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]))
1207 #endif /* AF_INET6 */
1214 * Check for exact name match...
1217 if (!strcasecmp(name
, masks
->mask
.name
.name
))
1221 * Check for domain match...
1224 if (name_len
>= masks
->mask
.name
.length
&&
1225 masks
->mask
.name
.name
[0] == '.' &&
1226 !strcasecmp(name
+ name_len
- masks
->mask
.name
.length
,
1227 masks
->mask
.name
.name
))
1233 * Check for IP/network address match...
1236 for (i
= 0; i
< 4; i
++)
1237 if ((ip
[i
] & masks
->mask
.ip
.netmask
[i
]) !=
1238 masks
->mask
.ip
.address
[i
])
1255 * 'cupsdCheckGroup()' - Check for a user's group membership.
1258 int /* O - 1 if user is a member, 0 otherwise */
1260 const char *username
, /* I - User name */
1261 struct passwd
*user
, /* I - System user info */
1262 const char *groupname
) /* I - Group name */
1264 int i
; /* Looping var */
1265 struct group
*group
; /* System group info */
1266 char junk
[33]; /* MD5 password (not used) */
1267 #ifdef HAVE_MBR_UID_TO_UUID
1268 uuid_t useruuid
, /* UUID for username */
1269 groupuuid
; /* UUID for groupname */
1270 int is_member
; /* True if user is a member of group */
1271 #endif /* HAVE_MBR_UID_TO_UUID */
1274 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1275 "cupsdCheckGroup(username=\"%s\", user=%p, groupname=\"%s\")",
1276 username
, user
, groupname
);
1282 if (!username
|| !groupname
)
1286 * Check to see if the user is a member of the named group...
1289 group
= getgrnam(groupname
);
1295 * Group exists, check it...
1298 for (i
= 0; group
->gr_mem
[i
]; i
++)
1299 if (!strcasecmp(username
, group
->gr_mem
[i
]))
1304 * Group doesn't exist or user not in group list, check the group ID
1305 * against the user's group ID...
1308 if (user
&& group
&& group
->gr_gid
== user
->pw_gid
)
1311 #ifdef HAVE_MBR_UID_TO_UUID
1313 * Check group membership through MacOS X membership API...
1317 if (!mbr_uid_to_uuid(user
->pw_uid
, useruuid
))
1318 if (!mbr_gid_to_uuid(group
->gr_gid
, groupuuid
))
1319 if (!mbr_check_membership(useruuid
, groupuuid
, &is_member
))
1322 #endif /* HAVE_MBR_UID_TO_UUID */
1325 * Username not found, group not found, or user is not part of the
1326 * system group... Check for a user and group in the MD5 password
1330 if (get_md5_password(username
, groupname
, junk
) != NULL
)
1334 * If we get this far, then the user isn't part of the named group...
1342 * 'cupsdCopyLocation()' - Make a copy of a location...
1345 cupsd_location_t
* /* O - New location */
1347 cupsd_location_t
**loc
) /* IO - Original location */
1349 int i
; /* Looping var */
1350 cupsd_location_t
*temp
; /* New location */
1351 char location
[HTTP_MAX_URI
];
1352 /* Location of resource */
1356 * Use a local copy of location because cupsdAddLocation may cause
1357 * this memory to be moved...
1360 strlcpy(location
, (*loc
)->location
, sizeof(location
));
1362 if ((temp
= cupsdAddLocation(location
)) == NULL
)
1366 * Copy the information from the original location to the new one.
1369 temp
->limit
= (*loc
)->limit
;
1370 temp
->order_type
= (*loc
)->order_type
;
1371 temp
->type
= (*loc
)->type
;
1372 temp
->level
= (*loc
)->level
;
1373 temp
->satisfy
= (*loc
)->satisfy
;
1374 temp
->encryption
= (*loc
)->encryption
;
1376 if ((temp
->num_names
= (*loc
)->num_names
) > 0)
1379 * Copy the names array...
1382 if ((temp
->names
= calloc(temp
->num_names
, sizeof(char *))) == NULL
)
1384 cupsdLogMessage(CUPSD_LOG_ERROR
,
1385 "cupsdCopyLocation: Unable to allocate memory for %d names: %s",
1386 temp
->num_names
, strerror(errno
));
1388 cupsdDeleteLocation(temp
);
1392 for (i
= 0; i
< temp
->num_names
; i
++)
1393 if ((temp
->names
[i
] = strdup((*loc
)->names
[i
])) == NULL
)
1395 cupsdLogMessage(CUPSD_LOG_ERROR
,
1396 "cupsdCopyLocation: Unable to copy name \"%s\": %s",
1397 (*loc
)->names
[i
], strerror(errno
));
1399 cupsdDeleteLocation(temp
);
1404 if ((temp
->num_allow
= (*loc
)->num_allow
) > 0)
1407 * Copy allow rules...
1410 if ((temp
->allow
= calloc(temp
->num_allow
, sizeof(cupsd_authmask_t
))) == NULL
)
1412 cupsdLogMessage(CUPSD_LOG_ERROR
,
1413 "cupsdCopyLocation: Unable to allocate memory for %d allow rules: %s",
1414 temp
->num_allow
, strerror(errno
));
1415 cupsdDeleteLocation(temp
);
1419 for (i
= 0; i
< temp
->num_allow
; i
++)
1420 switch (temp
->allow
[i
].type
= (*loc
)->allow
[i
].type
)
1423 temp
->allow
[i
].mask
.name
.length
= (*loc
)->allow
[i
].mask
.name
.length
;
1424 temp
->allow
[i
].mask
.name
.name
= strdup((*loc
)->allow
[i
].mask
.name
.name
);
1426 if (temp
->allow
[i
].mask
.name
.name
== NULL
)
1428 cupsdLogMessage(CUPSD_LOG_ERROR
,
1429 "cupsdCopyLocation: Unable to copy allow name \"%s\": %s",
1430 (*loc
)->allow
[i
].mask
.name
.name
, strerror(errno
));
1431 cupsdDeleteLocation(temp
);
1436 memcpy(&(temp
->allow
[i
].mask
.ip
), &((*loc
)->allow
[i
].mask
.ip
),
1437 sizeof(cupsd_ipmask_t
));
1442 if ((temp
->num_deny
= (*loc
)->num_deny
) > 0)
1445 * Copy deny rules...
1448 if ((temp
->deny
= calloc(temp
->num_deny
, sizeof(cupsd_authmask_t
))) == NULL
)
1450 cupsdLogMessage(CUPSD_LOG_ERROR
,
1451 "cupsdCopyLocation: Unable to allocate memory for %d deny rules: %s",
1452 temp
->num_deny
, strerror(errno
));
1453 cupsdDeleteLocation(temp
);
1457 for (i
= 0; i
< temp
->num_deny
; i
++)
1458 switch (temp
->deny
[i
].type
= (*loc
)->deny
[i
].type
)
1461 temp
->deny
[i
].mask
.name
.length
= (*loc
)->deny
[i
].mask
.name
.length
;
1462 temp
->deny
[i
].mask
.name
.name
= strdup((*loc
)->deny
[i
].mask
.name
.name
);
1464 if (temp
->deny
[i
].mask
.name
.name
== NULL
)
1466 cupsdLogMessage(CUPSD_LOG_ERROR
,
1467 "cupsdCopyLocation: Unable to copy deny name \"%s\": %s",
1468 (*loc
)->deny
[i
].mask
.name
.name
, strerror(errno
));
1469 cupsdDeleteLocation(temp
);
1474 memcpy(&(temp
->deny
[i
].mask
.ip
), &((*loc
)->deny
[i
].mask
.ip
),
1475 sizeof(cupsd_ipmask_t
));
1485 * 'cupsdDeleteAllLocations()' - Free all memory used for location authorization.
1489 cupsdDeleteAllLocations(void)
1491 cupsd_location_t
*loc
; /* Current location */
1495 * Free all of the allow/deny records first...
1498 for (loc
= (cupsd_location_t
*)cupsArrayFirst(Locations
);
1500 loc
= (cupsd_location_t
*)cupsArrayNext(Locations
))
1501 cupsdDeleteLocation(loc
);
1504 * Then free the location array...
1507 cupsArrayDelete(Locations
);
1513 * 'cupsdDeleteLocation()' - Free all memory used by a location.
1517 cupsdDeleteLocation(
1518 cupsd_location_t
*loc
) /* I - Location to delete */
1520 int i
; /* Looping var */
1521 cupsd_authmask_t
*mask
; /* Current mask */
1524 cupsArrayRemove(Locations
, loc
);
1526 for (i
= loc
->num_names
- 1; i
>= 0; i
--)
1527 free(loc
->names
[i
]);
1529 if (loc
->num_names
> 0)
1532 for (i
= loc
->num_allow
, mask
= loc
->allow
; i
> 0; i
--, mask
++)
1533 if (mask
->type
== AUTH_NAME
|| mask
->type
== AUTH_INTERFACE
)
1534 free(mask
->mask
.name
.name
);
1536 if (loc
->num_allow
> 0)
1539 for (i
= loc
->num_deny
, mask
= loc
->deny
; i
> 0; i
--, mask
++)
1540 if (mask
->type
== AUTH_NAME
|| mask
->type
== AUTH_INTERFACE
)
1541 free(mask
->mask
.name
.name
);
1543 if (loc
->num_deny
> 0)
1546 free(loc
->location
);
1552 * 'cupsdDenyHost()' - Add a host name that is not allowed to access the
1557 cupsdDenyHost(cupsd_location_t
*loc
, /* I - Location to add to */
1558 char *name
) /* I - Name of host or domain to add */
1560 cupsd_authmask_t
*temp
; /* New host/domain mask */
1561 char ifname
[32], /* Interface name */
1562 *ifptr
; /* Pointer to end of name */
1565 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdDenyHost(loc=%p(%s), name=\"%s\")",
1566 loc
, loc
->location
? loc
->location
: "nil", name
);
1568 if ((temp
= add_deny(loc
)) == NULL
)
1571 if (!strcasecmp(name
, "@LOCAL"))
1574 * Deny *interface*...
1577 temp
->type
= AUTH_INTERFACE
;
1578 temp
->mask
.name
.name
= strdup("*");
1579 temp
->mask
.name
.length
= 1;
1581 else if (!strncasecmp(name
, "@IF(", 4))
1584 * Deny *interface*...
1587 strlcpy(ifname
, name
+ 4, sizeof(ifname
));
1589 ifptr
= ifname
+ strlen(ifname
);
1591 if (ifptr
[-1] == ')')
1597 temp
->type
= AUTH_INTERFACE
;
1598 temp
->mask
.name
.name
= strdup(ifname
);
1599 temp
->mask
.name
.length
= ifptr
- ifname
;
1607 temp
->type
= AUTH_NAME
;
1608 temp
->mask
.name
.name
= strdup(name
);
1609 temp
->mask
.name
.length
= strlen(name
);
1615 * 'cupsdDenyIP()' - Add an IP address or network that is not allowed to
1616 * access the location.
1620 cupsdDenyIP(cupsd_location_t
*loc
, /* I - Location to add to */
1621 unsigned address
[4],/* I - IP address to add */
1622 unsigned netmask
[4])/* I - Netmask of address */
1624 cupsd_authmask_t
*temp
; /* New host/domain mask */
1627 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1628 "cupsdDenyIP(loc=%p(%s), address=%x:%x:%x:%x, netmask=%x:%x:%x:%x)",
1629 loc
, loc
->location
? loc
->location
: "nil",
1630 address
[0], address
[1], address
[2], address
[3],
1631 netmask
[0], netmask
[1], netmask
[2], netmask
[3]);
1633 if ((temp
= add_deny(loc
)) == NULL
)
1636 temp
->type
= AUTH_IP
;
1637 memcpy(temp
->mask
.ip
.address
, address
, sizeof(temp
->mask
.ip
.address
));
1638 memcpy(temp
->mask
.ip
.netmask
, netmask
, sizeof(temp
->mask
.ip
.netmask
));
1643 * 'cupsdFindBest()' - Find the location entry that best matches the resource.
1646 cupsd_location_t
* /* O - Location that matches */
1647 cupsdFindBest(const char *path
, /* I - Resource path */
1648 http_state_t state
) /* I - HTTP state/request */
1650 char uri
[HTTP_MAX_URI
],
1651 /* URI in request... */
1652 *uriptr
; /* Pointer into URI */
1653 cupsd_location_t
*loc
, /* Current location */
1654 *best
; /* Best match for location so far */
1655 int bestlen
; /* Length of best match */
1656 int limit
; /* Limit field */
1657 static const int limits
[] = /* Map http_status_t to AUTH_LIMIT_xyz */
1677 * First copy the connection URI to a local string so we have drop
1678 * any .ppd extension from the pathname in /printers or /classes
1682 strlcpy(uri
, path
, sizeof(uri
));
1684 if (!strncmp(uri
, "/printers/", 10) ||
1685 !strncmp(uri
, "/classes/", 9))
1688 * Check if the URI has .ppd on the end...
1691 uriptr
= uri
+ strlen(uri
) - 4; /* len > 4 if we get here... */
1693 if (!strcmp(uriptr
, ".ppd"))
1697 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: uri = \"%s\"...", uri
);
1700 * Loop through the list of locations to find a match...
1703 limit
= limits
[state
];
1707 for (loc
= (cupsd_location_t
*)cupsArrayFirst(Locations
);
1709 loc
= (cupsd_location_t
*)cupsArrayNext(Locations
))
1711 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: Location %s Limit %x",
1712 loc
->location
? loc
->location
: "nil", loc
->limit
);
1714 if (!strncmp(uri
, "/printers/", 10) || !strncmp(uri
, "/classes/", 9))
1717 * Use case-insensitive comparison for queue names...
1720 if (loc
->length
> bestlen
&& loc
->location
&&
1721 !strncasecmp(uri
, loc
->location
, loc
->length
) &&
1722 loc
->location
[0] == '/' &&
1723 (limit
& loc
->limit
) != 0)
1726 bestlen
= loc
->length
;
1732 * Use case-sensitive comparison for other URIs...
1735 if (loc
->length
> bestlen
&& loc
->location
&&
1736 !strncmp(uri
, loc
->location
, loc
->length
) &&
1737 loc
->location
[0] == '/' &&
1738 (limit
& loc
->limit
) != 0)
1741 bestlen
= loc
->length
;
1747 * Return the match, if any...
1750 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: best = %s",
1751 best
? best
->location
: "NONE");
1758 * 'cupsdFindLocation()' - Find the named location.
1761 cupsd_location_t
* /* O - Location that matches */
1762 cupsdFindLocation(const char *location
) /* I - Connection */
1764 cupsd_location_t key
; /* Search key */
1767 key
.location
= (char *)location
;
1769 return ((cupsd_location_t
*)cupsArrayFind(Locations
, &key
));
1774 * 'cupsdIsAuthorized()' - Check to see if the user is authorized...
1777 http_status_t
/* O - HTTP_OK if authorized or error code */
1778 cupsdIsAuthorized(cupsd_client_t
*con
, /* I - Connection */
1779 const char *owner
)/* I - Owner of object */
1781 int i
, j
, /* Looping vars */
1782 auth
; /* Authorization status */
1783 unsigned address
[4]; /* Authorization address */
1784 cupsd_location_t
*best
; /* Best match for location so far */
1785 int hostlen
; /* Length of hostname */
1786 const char *username
; /* Username to authorize */
1787 struct passwd
*pw
; /* User password data */
1788 static const char * const levels
[] = /* Auth levels */
1794 static const char * const types
[] = /* Auth types */
1804 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1805 "cupsdIsAuthorized: con->uri=\"%s\", con->best=%p(%s)",
1806 con
->uri
, con
->best
, con
->best
? con
->best
->location
?
1807 con
->best
->location
: "(null)" : "");
1809 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1810 "cupsdIsAuthorized: owner=\"%s\"", owner
);
1813 * If there is no "best" authentication rule for this request, then
1814 * access is allowed from the local system and denied from other
1820 if (!strcmp(con
->http
.hostname
, "localhost") ||
1821 !strcmp(con
->http
.hostname
, ServerName
))
1824 return (HTTP_FORBIDDEN
);
1829 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1830 "cupsdIsAuthorized: level=AUTH_%s, type=AUTH_%s, "
1831 "satisfy=AUTH_SATISFY_%s, num_names=%d",
1832 levels
[best
->level
], types
[best
->type
],
1833 best
->satisfy
? "ANY" : "ALL", best
->num_names
);
1835 if (best
->limit
== AUTH_LIMIT_IPP
)
1836 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: op=%x(%s)",
1837 best
->op
, ippOpString(best
->op
));
1840 * Check host/ip-based accesses...
1844 if (con
->http
.hostaddr
->addr
.sa_family
== AF_INET6
)
1847 * Copy IPv6 address...
1850 address
[0] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[0]);
1851 address
[1] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[1]);
1852 address
[2] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[2]);
1853 address
[3] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[3]);
1856 #endif /* AF_INET6 */
1857 if (con
->http
.hostaddr
->addr
.sa_family
== AF_INET
)
1860 * Copy IPv4 address...
1866 address
[3] = ntohl(con
->http
.hostaddr
->ipv4
.sin_addr
.s_addr
);
1869 memset(address
, 0, sizeof(address
));
1871 hostlen
= strlen(con
->http
.hostname
);
1873 if (!strcasecmp(con
->http
.hostname
, "localhost"))
1876 * Access from localhost (127.0.0.1 or ::1) is always allowed...
1884 * Do authorization checks on the domain/address...
1887 switch (best
->order_type
)
1890 auth
= AUTH_DENY
; /* anti-compiler-warning-code */
1893 case AUTH_ALLOW
: /* Order Deny,Allow */
1896 if (cupsdCheckAuth(address
, con
->http
.hostname
, hostlen
,
1897 best
->num_deny
, best
->deny
))
1900 if (cupsdCheckAuth(address
, con
->http
.hostname
, hostlen
,
1901 best
->num_allow
, best
->allow
))
1905 case AUTH_DENY
: /* Order Allow,Deny */
1908 if (cupsdCheckAuth(address
, con
->http
.hostname
, hostlen
,
1909 best
->num_allow
, best
->allow
))
1912 if (cupsdCheckAuth(address
, con
->http
.hostname
, hostlen
,
1913 best
->num_deny
, best
->deny
))
1919 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: auth=AUTH_%s...",
1920 auth
? "DENY" : "ALLOW");
1922 if (auth
== AUTH_DENY
&& best
->satisfy
== AUTH_SATISFY_ALL
)
1923 return (HTTP_FORBIDDEN
);
1927 * See if encryption is required...
1930 if ((best
->encryption
>= HTTP_ENCRYPT_REQUIRED
&& !con
->http
.tls
&&
1931 strcasecmp(con
->http
.hostname
, "localhost") &&
1932 best
->satisfy
== AUTH_SATISFY_ALL
) &&
1933 !(best
->type
== AUTH_NEGOTIATE
||
1934 (best
->type
== AUTH_NONE
&& DefaultAuthType
== AUTH_NEGOTIATE
)))
1936 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1937 "cupsdIsAuthorized: Need upgrade to TLS...");
1938 return (HTTP_UPGRADE_REQUIRED
);
1940 #endif /* HAVE_SSL */
1943 * Now see what access level is required...
1946 if (best
->level
== AUTH_ANON
|| /* Anonymous access - allow it */
1947 (best
->type
== AUTH_NONE
&& best
->num_names
== 0))
1950 if (!con
->username
[0] && best
->type
== AUTH_NONE
&&
1951 best
->limit
== AUTH_LIMIT_IPP
)
1954 * Check for unauthenticated username...
1957 ipp_attribute_t
*attr
; /* requesting-user-name attribute */
1960 attr
= ippFindAttribute(con
->request
, "requesting-user-name", IPP_TAG_NAME
);
1963 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1964 "cupsdIsAuthorized: requesting-user-name=\"%s\"",
1965 attr
->values
[0].string
.text
);
1966 username
= attr
->values
[0].string
.text
;
1968 else if (best
->satisfy
== AUTH_SATISFY_ALL
|| auth
== AUTH_DENY
)
1969 return (HTTP_UNAUTHORIZED
); /* Non-anonymous needs user/pass */
1971 return (HTTP_OK
); /* unless overridden with Satisfy */
1975 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdIsAuthorized: username=\"%s\"",
1978 #ifdef HAVE_AUTHORIZATION_H
1979 if (!con
->username
[0] && !con
->authref
)
1981 if (!con
->username
[0])
1982 #endif /* HAVE_AUTHORIZATION_H */
1984 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 */
1990 username
= con
->username
;
1994 * OK, got a username. See if we need normal user access, or group
1995 * access... (root always matches)
1998 if (!strcmp(username
, "root"))
2002 * Get the user info...
2007 pw
= getpwnam(username
);
2013 if (best
->level
== AUTH_USER
)
2016 * If there are no names associated with this location, then
2017 * any valid user is OK...
2020 if (best
->num_names
== 0)
2024 * Otherwise check the user list and return OK if this user is
2028 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2029 "cupsdIsAuthorized: Checking user membership...");
2031 #ifdef HAVE_AUTHORIZATION_H
2033 * If an authorization reference was supplied it must match a right name...
2038 for (i
= 0; i
< best
->num_names
; i
++)
2040 if (!strncasecmp(best
->names
[i
], "@AUTHKEY(", 9) &&
2041 check_authref(con
, best
->names
[i
] + 9))
2043 else if (!strcasecmp(best
->names
[i
], "@SYSTEM") &&
2044 SystemGroupAuthKey
&&
2045 check_authref(con
, SystemGroupAuthKey
))
2049 return (HTTP_UNAUTHORIZED
);
2051 #endif /* HAVE_AUTHORIZATION_H */
2053 for (i
= 0; i
< best
->num_names
; i
++)
2055 if (!strcasecmp(best
->names
[i
], "@OWNER") && owner
&&
2056 !strcasecmp(username
, owner
))
2058 else if (!strcasecmp(best
->names
[i
], "@SYSTEM"))
2060 for (j
= 0; j
< NumSystemGroups
; j
++)
2061 if (cupsdCheckGroup(username
, pw
, SystemGroups
[j
]))
2064 else if (best
->names
[i
][0] == '@')
2066 if (cupsdCheckGroup(username
, pw
, best
->names
[i
] + 1))
2069 else if (!strcasecmp(username
, best
->names
[i
]))
2073 return (HTTP_UNAUTHORIZED
);
2077 * Check to see if this user is in any of the named groups...
2080 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2081 "cupsdIsAuthorized: Checking group membership...");
2084 * Check to see if this user is in any of the named groups...
2087 for (i
= 0; i
< best
->num_names
; i
++)
2089 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2090 "cupsdIsAuthorized: Checking group \"%s\" membership...",
2093 if (!strcasecmp(best
->names
[i
], "@SYSTEM"))
2095 for (j
= 0; j
< NumSystemGroups
; j
++)
2096 if (cupsdCheckGroup(username
, pw
, SystemGroups
[j
]))
2099 else if (cupsdCheckGroup(username
, pw
, best
->names
[i
]))
2104 * The user isn't part of the specified group, so deny access...
2107 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2108 "cupsdIsAuthorized: User not in group(s)!");
2110 return (HTTP_UNAUTHORIZED
);
2115 * 'add_allow()' - Add an allow mask to the location.
2118 static cupsd_authmask_t
* /* O - New mask record */
2119 add_allow(cupsd_location_t
*loc
) /* I - Location to add to */
2121 cupsd_authmask_t
*temp
; /* New mask record */
2132 * Try to allocate memory for the record...
2135 if (loc
->num_allow
== 0)
2136 temp
= malloc(sizeof(cupsd_authmask_t
));
2138 temp
= realloc(loc
->allow
, sizeof(cupsd_authmask_t
) * (loc
->num_allow
+ 1));
2144 temp
+= loc
->num_allow
;
2148 * Clear the mask record and return...
2151 memset(temp
, 0, sizeof(cupsd_authmask_t
));
2157 * 'add_deny()' - Add a deny mask to the location.
2160 static cupsd_authmask_t
* /* O - New mask record */
2161 add_deny(cupsd_location_t
*loc
) /* I - Location to add to */
2163 cupsd_authmask_t
*temp
; /* New mask record */
2174 * Try to allocate memory for the record...
2177 if (loc
->num_deny
== 0)
2178 temp
= malloc(sizeof(cupsd_authmask_t
));
2180 temp
= realloc(loc
->deny
, sizeof(cupsd_authmask_t
) * (loc
->num_deny
+ 1));
2186 temp
+= loc
->num_deny
;
2190 * Clear the mask record and return...
2193 memset(temp
, 0, sizeof(cupsd_authmask_t
));
2198 #ifdef HAVE_AUTHORIZATION_H
2200 * 'check_authref()' - Check if an authorization services reference has the
2204 static int /* O - 1 if right is valid, 0 otherwise */
2205 check_authref(cupsd_client_t
*con
, /* I - Connection */
2206 const char *right
) /* I - Right name */
2208 OSStatus status
; /* OS Status */
2209 AuthorizationItem authright
; /* Authorization right */
2210 AuthorizationRights authrights
; /* Authorization rights */
2211 AuthorizationFlags authflags
; /* Authorization flags */
2215 * Check to see if the user is allowed to perform the task...
2221 authright
.name
= right
;
2222 authright
.valueLength
= 0;
2223 authright
.value
= NULL
;
2224 authright
.flags
= 0;
2226 authrights
.count
= 1;
2227 authrights
.items
= &authright
;
2229 authflags
= kAuthorizationFlagDefaults
|
2230 kAuthorizationFlagExtendRights
;
2232 if ((status
= AuthorizationCopyRights(con
->authref
, &authrights
,
2233 kAuthorizationEmptyEnvironment
,
2234 authflags
, NULL
)) != 0)
2236 cupsdLogMessage(CUPSD_LOG_ERROR
,
2237 "AuthorizationCopyRights(\"%s\") returned %d (%s)",
2238 authright
.name
, (int)status
, cssmErrorString(status
));
2242 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2243 "AuthorizationCopyRights(\"%s\") succeeded!",
2248 #endif /* HAVE_AUTHORIZATION_H */
2252 * 'compare_locations()' - Compare two locations.
2255 static int /* O - Result of comparison */
2256 compare_locations(cupsd_location_t
*a
, /* I - First location */
2257 cupsd_location_t
*b
) /* I - Second location */
2259 return (strcmp(b
->location
, a
->location
));
2263 #if !HAVE_LIBPAM && !defined(HAVE_USERSEC_H)
2265 * 'cups_crypt()' - Encrypt the password using the DES or MD5 algorithms,
2269 static char * /* O - Encrypted password */
2270 cups_crypt(const char *pw
, /* I - Password string */
2271 const char *salt
) /* I - Salt (key) string */
2273 if (!strncmp(salt
, "$1$", 3))
2276 * Use MD5 passwords without the benefit of PAM; this is for
2277 * Slackware Linux, and the algorithm was taken from the
2278 * old shadow-19990827/lib/md5crypt.c source code... :(
2281 int i
; /* Looping var */
2282 unsigned long n
; /* Output number */
2283 int pwlen
; /* Length of password string */
2284 const char *salt_end
; /* End of "salt" data for MD5 */
2285 char *ptr
; /* Pointer into result string */
2286 _cups_md5_state_t state
; /* Primary MD5 state info */
2287 _cups_md5_state_t state2
; /* Secondary MD5 state info */
2288 unsigned char digest
[16]; /* MD5 digest result */
2289 static char result
[120]; /* Final password string */
2293 * Get the salt data between dollar signs, e.g. $1$saltdata$md5.
2294 * Get a maximum of 8 characters of salt data after $1$...
2297 for (salt_end
= salt
+ 3; *salt_end
&& (salt_end
- salt
) < 11; salt_end
++)
2298 if (*salt_end
== '$')
2302 * Compute the MD5 sum we need...
2307 _cupsMD5Init(&state
);
2308 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
2309 _cupsMD5Append(&state
, (unsigned char *)salt
, salt_end
- salt
);
2311 _cupsMD5Init(&state2
);
2312 _cupsMD5Append(&state2
, (unsigned char *)pw
, pwlen
);
2313 _cupsMD5Append(&state2
, (unsigned char *)salt
+ 3, salt_end
- salt
- 3);
2314 _cupsMD5Append(&state2
, (unsigned char *)pw
, pwlen
);
2315 _cupsMD5Finish(&state2
, digest
);
2317 for (i
= pwlen
; i
> 0; i
-= 16)
2318 _cupsMD5Append(&state
, digest
, i
> 16 ? 16 : i
);
2320 for (i
= pwlen
; i
> 0; i
>>= 1)
2321 _cupsMD5Append(&state
, (unsigned char *)((i
& 1) ? "" : pw
), 1);
2323 _cupsMD5Finish(&state
, digest
);
2325 for (i
= 0; i
< 1000; i
++)
2327 _cupsMD5Init(&state
);
2330 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
2332 _cupsMD5Append(&state
, digest
, 16);
2335 _cupsMD5Append(&state
, (unsigned char *)salt
+ 3, salt_end
- salt
- 3);
2338 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
2341 _cupsMD5Append(&state
, digest
, 16);
2343 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
2345 _cupsMD5Finish(&state
, digest
);
2349 * Copy the final sum to the result string and return...
2352 memcpy(result
, salt
, salt_end
- salt
);
2353 ptr
= result
+ (salt_end
- salt
);
2356 for (i
= 0; i
< 5; i
++, ptr
+= 4)
2358 n
= (((digest
[i
] << 8) | digest
[i
+ 6]) << 8);
2361 n
|= digest
[i
+ 12];
2368 to64(ptr
, digest
[11], 2);
2377 * Use the standard crypt() function...
2380 return (crypt(pw
, salt
));
2383 #endif /* !HAVE_LIBPAM && !HAVE_USERSEC_H */
2388 * 'get_gss_creds()' - Obtain GSS credentials.
2391 static gss_cred_id_t
/* O - Server credentials */
2392 get_gss_creds(const char *service_name
) /* I - Service name */
2394 OM_uint32 major_status
, /* Major status code */
2395 minor_status
; /* Minor status code */
2396 gss_name_t server_name
; /* Server name */
2397 gss_cred_id_t server_creds
; /* Server credentials */
2398 gss_buffer_desc token
= GSS_C_EMPTY_BUFFER
;
2399 /* Service name token */
2400 char buf
[1024], /* Service name buffer */
2401 fqdn
[HTTP_MAX_URI
]; /* Hostname of server */
2404 snprintf(buf
, sizeof(buf
), "%s@%s", service_name
,
2405 httpGetHostname(NULL
, fqdn
, sizeof(fqdn
)));
2408 token
.length
= strlen(buf
);
2409 server_name
= GSS_C_NO_NAME
;
2410 major_status
= gss_import_name(&minor_status
, &token
,
2411 GSS_C_NT_HOSTBASED_SERVICE
,
2414 memset(&token
, 0, sizeof(token
));
2416 if (GSS_ERROR(major_status
))
2418 cupsdLogGSSMessage(CUPSD_LOG_WARN
, major_status
, minor_status
,
2419 "gss_import_name() failed");
2423 major_status
= gss_display_name(&minor_status
, server_name
, &token
, NULL
);
2425 if (GSS_ERROR(major_status
))
2427 cupsdLogGSSMessage(CUPSD_LOG_WARN
, major_status
, minor_status
,
2428 "gss_display_name() failed");
2432 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2433 "get_gss_creds: Attempting to acquire credentials for %s...",
2434 (char *)token
.value
);
2436 server_creds
= GSS_C_NO_CREDENTIAL
;
2437 major_status
= gss_acquire_cred(&minor_status
, server_name
, GSS_C_INDEFINITE
,
2438 GSS_C_NO_OID_SET
, GSS_C_ACCEPT
,
2439 &server_creds
, NULL
, NULL
);
2440 if (GSS_ERROR(major_status
))
2442 cupsdLogGSSMessage(CUPSD_LOG_WARN
, major_status
, minor_status
,
2443 "gss_acquire_cred() failed");
2444 gss_release_name(&minor_status
, &server_name
);
2445 gss_release_buffer(&minor_status
, &token
);
2449 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2450 "get_gss_creds: Credentials acquired successfully for %s.",
2451 (char *)token
.value
);
2453 gss_release_name(&minor_status
, &server_name
);
2454 gss_release_buffer(&minor_status
, &token
);
2456 return (server_creds
);
2458 #endif /* HAVE_GSSAPI */
2462 * 'get_md5_password()' - Get an MD5 password.
2465 static char * /* O - MD5 password string */
2466 get_md5_password(const char *username
, /* I - Username */
2467 const char *group
, /* I - Group */
2468 char passwd
[33]) /* O - MD5 password string */
2470 cups_file_t
*fp
; /* passwd.md5 file */
2471 char filename
[1024], /* passwd.md5 filename */
2472 line
[256], /* Line from file */
2473 tempuser
[33], /* User from file */
2474 tempgroup
[33]; /* Group from file */
2477 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2478 "get_md5_password(username=\"%s\", group=\"%s\", passwd=%p)",
2479 username
, group
? group
: "(null)", passwd
);
2481 snprintf(filename
, sizeof(filename
), "%s/passwd.md5", ServerRoot
);
2482 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
2484 if (errno
!= ENOENT
)
2485 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open %s - %s", filename
,
2491 while (cupsFileGets(fp
, line
, sizeof(line
)) != NULL
)
2493 if (sscanf(line
, "%32[^:]:%32[^:]:%32s", tempuser
, tempgroup
, passwd
) != 3)
2495 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad MD5 password line: %s", line
);
2499 if (!strcmp(username
, tempuser
) &&
2500 (group
== NULL
|| !strcmp(group
, tempgroup
)))
2503 * Found the password entry!
2506 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Found MD5 user %s, group %s...",
2507 username
, tempgroup
);
2515 * Didn't find a password entry - return NULL!
2525 * 'pam_func()' - PAM conversation function.
2528 static int /* O - Success or failure */
2530 int num_msg
, /* I - Number of messages */
2531 const struct pam_message
**msg
, /* I - Messages */
2532 struct pam_response
**resp
, /* O - Responses */
2534 /* I - Pointer to connection */
2536 int i
; /* Looping var */
2537 struct pam_response
*replies
; /* Replies */
2538 cupsd_authdata_t
*data
; /* Pointer to auth data */
2542 * Allocate memory for the responses...
2545 if ((replies
= malloc(sizeof(struct pam_response
) * num_msg
)) == NULL
)
2546 return (PAM_CONV_ERR
);
2549 * Answer all of the messages...
2552 DEBUG_printf(("pam_func: appdata_ptr = %p\n", appdata_ptr
));
2556 * Apparently some versions of HP-UX 11 have a broken pam_unix security
2557 * module. This is a workaround...
2563 data
= (cupsd_authdata_t
*)appdata_ptr
;
2566 for (i
= 0; i
< num_msg
; i
++)
2568 DEBUG_printf(("pam_func: Message = \"%s\"\n", msg
[i
]->msg
));
2570 switch (msg
[i
]->msg_style
)
2572 case PAM_PROMPT_ECHO_ON
:
2573 DEBUG_printf(("pam_func: PAM_PROMPT_ECHO_ON, returning \"%s\"...\n",
2575 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2576 replies
[i
].resp
= strdup(data
->username
);
2579 case PAM_PROMPT_ECHO_OFF
:
2580 DEBUG_printf(("pam_func: PAM_PROMPT_ECHO_OFF, returning \"%s\"...\n",
2582 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2583 replies
[i
].resp
= strdup(data
->password
);
2587 DEBUG_puts("pam_func: PAM_TEXT_INFO...");
2588 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2589 replies
[i
].resp
= NULL
;
2593 DEBUG_puts("pam_func: PAM_ERROR_MSG...");
2594 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2595 replies
[i
].resp
= NULL
;
2599 DEBUG_printf(("pam_func: Unknown PAM message %d...\n",
2600 msg
[i
]->msg_style
));
2602 return (PAM_CONV_ERR
);
2607 * Return the responses back to PAM...
2612 return (PAM_SUCCESS
);
2614 #elif !defined(HAVE_USERSEC_H)
2618 * 'to64()' - Base64-encode an integer value...
2622 to64(char *s
, /* O - Output string */
2623 unsigned long v
, /* I - Value to encode */
2624 int n
) /* I - Number of digits */
2626 const char *itoa64
= "./0123456789"
2627 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2628 "abcdefghijklmnopqrstuvwxyz";
2631 for (; n
> 0; n
--, v
>>= 6)
2632 *s
++ = itoa64
[v
& 0x3f];
2634 #endif /* HAVE_LIBPAM */
2638 * End of "$Id: auth.c 6758 2007-08-02 00:13:44Z mike $".