2 * "$Id: auth.c 7830 2008-08-04 20:38:50Z mike $"
4 * Authorization routines for the CUPS scheduler.
6 * Copyright 2007-2011 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * This file contains Kerberos support code, copyright 2006 by
12 * These coded instructions, statements, and computer programs are the
13 * property of Apple Inc. and are protected by Federal copyright
14 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
15 * which should have been included with this file. If this file is
16 * file is missing or damaged, see the license at "http://www.cups.org/".
20 * cupsdAddIPMask() - Add an IP address authorization mask.
21 * cupsdAddLocation() - Add a location for authorization.
22 * cupsdAddName() - Add a name to a location...
23 * cupsdAddNameMask() - Add a host or interface name authorization
25 * cupsdAuthorize() - Validate any authorization credentials.
26 * cupsdCheckAccess() - Check whether the given address is allowed to
28 * cupsdCheckAuth() - Check authorization masks.
29 * cupsdCheckGroup() - Check for a user's group membership.
30 * cupsdCopyKrb5Creds() - Get a copy of the Kerberos credentials.
31 * cupsdCopyLocation() - Make a copy of a location...
32 * cupsdDeleteAllLocations() - Free all memory used for location
34 * cupsdFindBest() - Find the location entry that best matches the
36 * cupsdFindLocation() - Find the named location.
37 * cupsdFreeLocation() - Free all memory used by a location.
38 * cupsdIsAuthorized() - Check to see if the user is authorized...
39 * cupsdNewLocation() - Create a new location for authorization.
40 * check_authref() - Check if an authorization services reference
41 * has the supplied right.
42 * compare_locations() - Compare two locations.
43 * copy_authmask() - Copy function for auth masks.
44 * cups_crypt() - Encrypt the password using the DES or MD5
45 * algorithms, as needed.
46 * free_authmask() - Free function for auth masks.
47 * get_md5_password() - Get an MD5 password.
48 * pam_func() - PAM conversation function.
49 * to64() - Base64-encode an integer value...
53 * Include necessary headers...
60 #endif /* HAVE_SHADOW_H */
63 #endif /* HAVE_CRYPT_H */
65 # ifdef HAVE_PAM_PAM_APPL_H
66 # include <pam/pam_appl.h>
68 # include <security/pam_appl.h>
69 # endif /* HAVE_PAM_PAM_APPL_H */
70 #endif /* HAVE_LIBPAM */
73 #endif /* HAVE_USERSEC_H */
74 #ifdef HAVE_MEMBERSHIP_H
75 # include <membership.h>
76 #endif /* HAVE_MEMBERSHIP_H */
77 #ifdef HAVE_AUTHORIZATION_H
78 # include <Security/AuthorizationTags.h>
79 # ifdef HAVE_SECBASEPRIV_H
80 # include <Security/SecBasePriv.h>
82 extern const char *cssmErrorString(int error
);
83 # endif /* HAVE_SECBASEPRIV_H */
84 #endif /* HAVE_AUTHORIZATION_H */
85 #ifdef HAVE_SYS_PARAM_H
86 # include <sys/param.h>
87 #endif /* HAVE_SYS_PARAM_H */
88 #ifdef HAVE_SYS_UCRED_H
89 # include <sys/ucred.h>
90 typedef struct xucred cupsd_ucred_t
;
91 # define CUPSD_UCRED_UID(c) (c).cr_uid
93 typedef struct ucred cupsd_ucred_t
;
94 # define CUPSD_UCRED_UID(c) (c).uid
95 #endif /* HAVE_SYS_UCRED_H */
96 #ifdef HAVE_KRB5_IPC_CLIENT_SET_TARGET_UID
97 /* Not in public headers... */
98 extern void krb5_ipc_client_set_target_uid(uid_t
);
99 extern void krb5_ipc_client_clear_target(void);
100 #endif /* HAVE_KRB5_IPC_CLIENT_SET_TARGET_UID */
107 #ifdef HAVE_AUTHORIZATION_H
108 static int check_authref(cupsd_client_t
*con
, const char *right
);
109 #endif /* HAVE_AUTHORIZATION_H */
110 static int compare_locations(cupsd_location_t
*a
,
111 cupsd_location_t
*b
);
112 static cupsd_authmask_t
*copy_authmask(cupsd_authmask_t
*am
, void *data
);
113 #if !HAVE_LIBPAM && !defined(HAVE_USERSEC_H)
114 static char *cups_crypt(const char *pw
, const char *salt
);
115 #endif /* !HAVE_LIBPAM && !HAVE_USERSEC_H */
116 static void free_authmask(cupsd_authmask_t
*am
, void *data
);
117 static char *get_md5_password(const char *username
,
118 const char *group
, char passwd
[33]);
120 static int pam_func(int, const struct pam_message
**,
121 struct pam_response
**, void *);
122 #elif !defined(HAVE_USERSEC_H)
123 static void to64(char *s
, unsigned long v
, int n
);
124 #endif /* HAVE_LIBPAM */
128 * Local structures...
132 typedef struct cupsd_authdata_s
/**** Authentication data ****/
134 char username
[33], /* Username string */
135 password
[33]; /* Password string */
137 #endif /* HAVE_LIBPAM */
144 #if defined(__hpux) && HAVE_LIBPAM
145 static cupsd_authdata_t
*auth_data
; /* Current client being authenticated */
146 #endif /* __hpux && HAVE_LIBPAM */
150 * 'cupsdAddIPMask()' - Add an IP address authorization mask.
153 int /* O - 1 on success, 0 on failure */
155 cups_array_t
**masks
, /* IO - Masks array (created as needed) */
156 const unsigned address
[4], /* I - IP address */
157 const unsigned netmask
[4]) /* I - IP netmask */
159 cupsd_authmask_t temp
; /* New host/domain mask */
162 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
163 "cupsdAddIPMask(masks=%p(%p), address=%x:%x:%x:%x, "
164 "netmask=%x:%x:%x:%x)",
166 address
[0], address
[1], address
[2], address
[3],
167 netmask
[0], netmask
[1], netmask
[2], netmask
[3]);
169 temp
.type
= CUPSD_AUTH_IP
;
170 memcpy(temp
.mask
.ip
.address
, address
, sizeof(temp
.mask
.ip
.address
));
171 memcpy(temp
.mask
.ip
.netmask
, netmask
, sizeof(temp
.mask
.ip
.netmask
));
174 * Create the masks array as needed and add...
178 *masks
= cupsArrayNew3(NULL
, NULL
, NULL
, 0,
179 (cups_acopy_func_t
)copy_authmask
,
180 (cups_afree_func_t
)free_authmask
);
182 return (cupsArrayAdd(*masks
, &temp
));
187 * 'cupsdAddLocation()' - Add a location for authorization.
191 cupsdAddLocation(cupsd_location_t
*loc
) /* I - Location to add */
194 * Make sure the locations array is created...
198 Locations
= cupsArrayNew3((cups_array_func_t
)compare_locations
, NULL
,
199 (cups_ahash_func_t
)NULL
, 0,
200 (cups_acopy_func_t
)NULL
,
201 (cups_afree_func_t
)cupsdFreeLocation
);
205 cupsArrayAdd(Locations
, loc
);
207 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAddLocation: Added location \"%s\"",
208 loc
->location
? loc
->location
: "(null)");
214 * 'cupsdAddName()' - Add a name to a location...
218 cupsdAddName(cupsd_location_t
*loc
, /* I - Location to add to */
219 char *name
) /* I - Name to add */
221 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAddName(loc=%p, name=\"%s\")",
225 loc
->names
= cupsArrayNew3(NULL
, NULL
, NULL
, 0,
226 (cups_acopy_func_t
)_cupsStrAlloc
,
227 (cups_afree_func_t
)_cupsStrFree
);
229 if (!cupsArrayAdd(loc
->names
, name
))
231 cupsdLogMessage(CUPSD_LOG_ERROR
,
232 "Unable to duplicate name for location %s: %s",
233 loc
->location
? loc
->location
: "nil", strerror(errno
));
240 * 'cupsdAddNameMask()' - Add a host or interface name authorization mask.
243 int /* O - 1 on success, 0 on failure */
244 cupsdAddNameMask(cups_array_t
**masks
, /* IO - Masks array (created as needed) */
245 char *name
) /* I - Host or interface name */
247 cupsd_authmask_t temp
; /* New host/domain mask */
248 char ifname
[32], /* Interface name */
249 *ifptr
; /* Pointer to end of name */
252 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
253 "cupsdAddNameMask(masks=%p(%p), name=\"%s\")",
254 masks
, *masks
, name
);
256 if (!strcasecmp(name
, "@LOCAL"))
259 * Deny *interface*...
262 temp
.type
= CUPSD_AUTH_INTERFACE
;
263 temp
.mask
.name
.name
= (char *)"*";
265 else if (!strncasecmp(name
, "@IF(", 4))
268 * Deny *interface*...
271 strlcpy(ifname
, name
+ 4, sizeof(ifname
));
273 ifptr
= ifname
+ strlen(ifname
) - 1;
275 if (ifptr
>= ifname
&& *ifptr
== ')')
281 temp
.type
= CUPSD_AUTH_INTERFACE
;
282 temp
.mask
.name
.name
= ifname
;
293 temp
.type
= CUPSD_AUTH_NAME
;
294 temp
.mask
.name
.name
= (char *)name
;
298 * Set the name length...
301 temp
.mask
.name
.length
= strlen(temp
.mask
.name
.name
);
304 * Create the masks array as needed and add...
308 *masks
= cupsArrayNew3(NULL
, NULL
, NULL
, 0,
309 (cups_acopy_func_t
)copy_authmask
,
310 (cups_afree_func_t
)free_authmask
);
312 return (cupsArrayAdd(*masks
, &temp
));
317 * 'cupsdAuthorize()' - Validate any authorization credentials.
321 cupsdAuthorize(cupsd_client_t
*con
) /* I - Client connection */
323 int type
; /* Authentication type */
324 const char *authorization
; /* Pointer into Authorization string */
325 char *ptr
, /* Pointer into string */
326 username
[256], /* Username string */
327 password
[33]; /* Password string */
328 cupsd_cert_t
*localuser
; /* Certificate username */
329 char nonce
[HTTP_MAX_VALUE
], /* Nonce value from client */
330 md5
[33], /* MD5 password */
331 basicmd5
[33]; /* MD5 of Basic password */
332 static const char * const states
[] = /* HTTP client states... */
352 * Locate the best matching location so we know what kind of
353 * authentication to expect...
356 con
->best
= cupsdFindBest(con
->uri
, con
->http
.state
);
357 con
->type
= CUPSD_AUTH_NONE
;
359 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
360 "cupsdAuthorize: con->uri=\"%s\", con->best=%p(%s)",
361 con
->uri
, con
->best
, con
->best
? con
->best
->location
: "");
363 if (con
->best
&& con
->best
->type
!= CUPSD_AUTH_NONE
)
365 if (con
->best
->type
== CUPSD_AUTH_DEFAULT
)
366 type
= DefaultAuthType
;
368 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 */
396 * No authorization data provided, return early...
399 cupsdLogMessage(CUPSD_LOG_DEBUG
,
400 "cupsdAuthorize: No authentication data provided.");
403 #ifdef HAVE_AUTHORIZATION_H
404 else if (!strncmp(authorization
, "AuthRef ", 8) &&
405 !strcasecmp(con
->http
.hostname
, "localhost"))
407 OSStatus status
; /* Status */
408 int authlen
; /* Auth string length */
409 AuthorizationItemSet
*authinfo
; /* Authorization item set */
412 * Get the Authorization Services data...
416 while (isspace(*authorization
& 255))
419 authlen
= sizeof(nonce
);
420 httpDecode64_2(nonce
, &authlen
, authorization
);
422 if (authlen
!= kAuthorizationExternalFormLength
)
424 cupsdLogMessage(CUPSD_LOG_ERROR
,
425 "External Authorization reference size is incorrect!");
429 if ((status
= AuthorizationCreateFromExternalForm(
430 (AuthorizationExternalForm
*)nonce
, &con
->authref
)) != 0)
432 cupsdLogMessage(CUPSD_LOG_ERROR
,
433 "AuthorizationCreateFromExternalForm returned %d (%s)",
434 (int)status
, cssmErrorString(status
));
440 if (!AuthorizationCopyInfo(con
->authref
, kAuthorizationEnvironmentUsername
,
443 if (authinfo
->count
== 1 && authinfo
->items
[0].value
&&
444 authinfo
->items
[0].valueLength
>= 2)
446 strlcpy(username
, authinfo
->items
[0].value
, sizeof(username
));
448 cupsdLogMessage(CUPSD_LOG_DEBUG
,
449 "cupsdAuthorize: Authorized as \"%s\" using AuthRef",
453 AuthorizationFreeItemSet(authinfo
);
459 * No username in AuthRef, grab username using peer credentials...
462 struct passwd
*pwd
; /* Password entry for this user */
463 cupsd_ucred_t peercred
; /* Peer credentials */
464 socklen_t peersize
; /* Size of peer credentials */
466 peersize
= sizeof(peercred
);
468 if (getsockopt(con
->http
.fd
, 0, LOCAL_PEERCRED
, &peercred
, &peersize
))
470 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to get peer credentials - %s",
475 if ((pwd
= getpwuid(CUPSD_UCRED_UID(peercred
))) == NULL
)
477 cupsdLogMessage(CUPSD_LOG_ERROR
,
478 "Unable to find UID %d for peer credentials.",
479 (int)CUPSD_UCRED_UID(peercred
));
483 strlcpy(username
, pwd
->pw_name
, sizeof(username
));
485 cupsdLogMessage(CUPSD_LOG_DEBUG
,
486 "cupsdAuthorize: Authorized as \"%s\" using "
487 "AuthRef + PeerCred", username
);
490 con
->type
= CUPSD_AUTH_BASIC
;
492 #endif /* HAVE_AUTHORIZATION_H */
493 #if defined(SO_PEERCRED) && defined(AF_LOCAL)
494 else if (!strncmp(authorization
, "PeerCred ", 9) &&
495 con
->http
.hostaddr
->addr
.sa_family
== AF_LOCAL
)
498 * Use peer credentials from domain socket connection...
501 struct passwd
*pwd
; /* Password entry for this user */
502 cupsd_ucred_t peercred
; /* Peer credentials */
503 socklen_t peersize
; /* Size of peer credentials */
506 if ((pwd
= getpwnam(authorization
+ 9)) == NULL
)
508 cupsdLogMessage(CUPSD_LOG_ERROR
, "User \"%s\" does not exist!",
513 peersize
= sizeof(peercred
);
516 if (getsockopt(con
->http
.fd
, 0, LOCAL_PEERCRED
, &peercred
, &peersize
))
518 if (getsockopt(con
->http
.fd
, SOL_SOCKET
, SO_PEERCRED
, &peercred
, &peersize
))
519 # endif /* __APPLE__ */
521 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to get peer credentials - %s",
526 if (pwd
->pw_uid
!= CUPSD_UCRED_UID(peercred
))
528 cupsdLogMessage(CUPSD_LOG_ERROR
,
529 "Invalid peer credentials for \"%s\" - got %d, "
530 "expected %d!", authorization
+ 9,
531 CUPSD_UCRED_UID(peercred
), pwd
->pw_uid
);
532 # ifdef HAVE_SYS_UCRED_H
533 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAuthorize: cr_version=%d",
534 peercred
.cr_version
);
535 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAuthorize: cr_uid=%d",
537 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAuthorize: cr_ngroups=%d",
538 peercred
.cr_ngroups
);
539 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAuthorize: cr_groups[0]=%d",
540 peercred
.cr_groups
[0]);
541 # endif /* HAVE_SYS_UCRED_H */
545 strlcpy(username
, authorization
+ 9, sizeof(username
));
547 cupsdLogMessage(CUPSD_LOG_DEBUG
,
548 "cupsdAuthorize: Authorized as %s using PeerCred",
551 con
->type
= CUPSD_AUTH_BASIC
;
553 #endif /* SO_PEERCRED && AF_LOCAL */
554 else if (!strncmp(authorization
, "Local", 5) &&
555 !strcasecmp(con
->http
.hostname
, "localhost"))
558 * Get Local certificate authentication data...
562 while (isspace(*authorization
& 255))
565 if ((localuser
= cupsdFindCert(authorization
)) != NULL
)
567 strlcpy(username
, localuser
->username
, sizeof(username
));
569 cupsdLogMessage(CUPSD_LOG_DEBUG
,
570 "cupsdAuthorize: Authorized as %s using Local",
575 cupsdLogMessage(CUPSD_LOG_ERROR
,
576 "cupsdAuthorize: Local authentication certificate not "
582 if (localuser
->ccache
)
583 con
->type
= CUPSD_AUTH_NEGOTIATE
;
585 #endif /* HAVE_GSSAPI */
586 con
->type
= CUPSD_AUTH_BASIC
;
588 else if (!strncmp(authorization
, "Basic", 5))
591 * Get the Basic authentication data...
594 int userlen
; /* Username:password length */
598 while (isspace(*authorization
& 255))
601 userlen
= sizeof(username
);
602 httpDecode64_2(username
, &userlen
, authorization
);
605 * Pull the username and password out...
608 if ((ptr
= strchr(username
, ':')) == NULL
)
610 cupsdLogMessage(CUPSD_LOG_ERROR
,
611 "cupsdAuthorize: Missing Basic password!");
620 * Username must not be empty...
623 cupsdLogMessage(CUPSD_LOG_ERROR
,
624 "cupsdAuthorize: Empty Basic username!");
631 * Password must not be empty...
634 cupsdLogMessage(CUPSD_LOG_ERROR
,
635 "cupsdAuthorize: Empty Basic password!");
639 strlcpy(password
, ptr
, sizeof(password
));
642 * Validate the username and password...
648 case CUPSD_AUTH_BASIC
:
652 * Only use PAM to do authentication. This supports MD5
653 * passwords, among other things...
656 pam_handle_t
*pamh
; /* PAM authentication handle */
657 int pamerr
; /* PAM error code */
658 struct pam_conv pamdata
;/* PAM conversation data */
659 cupsd_authdata_t data
; /* Authentication data */
662 strlcpy(data
.username
, username
, sizeof(data
.username
));
663 strlcpy(data
.password
, password
, sizeof(data
.password
));
665 # if defined(__sun) || defined(__hpux)
666 pamdata
.conv
= (int (*)(int, struct pam_message
**,
667 struct pam_response
**,
670 pamdata
.conv
= pam_func
;
671 # endif /* __sun || __hpux */
672 pamdata
.appdata_ptr
= &data
;
676 * Workaround for HP-UX bug in pam_unix; see pam_func() below for
683 pamerr
= pam_start("cups", username
, &pamdata
, &pamh
);
684 if (pamerr
!= PAM_SUCCESS
)
686 cupsdLogMessage(CUPSD_LOG_ERROR
,
687 "cupsdAuthorize: pam_start() returned %d (%s)!",
688 pamerr
, pam_strerror(pamh
, pamerr
));
692 # ifdef HAVE_PAM_SET_ITEM
694 pamerr
= pam_set_item(pamh
, PAM_RHOST
, con
->http
.hostname
);
695 if (pamerr
!= PAM_SUCCESS
)
696 cupsdLogMessage(CUPSD_LOG_WARN
,
697 "cupsdAuthorize: pam_set_item(PAM_RHOST) "
698 "returned %d (%s)!", pamerr
,
699 pam_strerror(pamh
, pamerr
));
700 # endif /* PAM_RHOST */
703 pamerr
= pam_set_item(pamh
, PAM_TTY
, "cups");
704 if (pamerr
!= PAM_SUCCESS
)
705 cupsdLogMessage(CUPSD_LOG_WARN
,
706 "cupsdAuthorize: pam_set_item(PAM_TTY) "
707 "returned %d (%s)!", pamerr
,
708 pam_strerror(pamh
, pamerr
));
709 # endif /* PAM_TTY */
710 # endif /* HAVE_PAM_SET_ITEM */
712 pamerr
= pam_authenticate(pamh
, PAM_SILENT
);
713 if (pamerr
!= PAM_SUCCESS
)
715 cupsdLogMessage(CUPSD_LOG_ERROR
,
716 "cupsdAuthorize: pam_authenticate() returned %d "
718 pamerr
, pam_strerror(pamh
, pamerr
));
723 # ifdef HAVE_PAM_SETCRED
724 pamerr
= pam_setcred(pamh
, PAM_ESTABLISH_CRED
| PAM_SILENT
);
725 if (pamerr
!= PAM_SUCCESS
)
726 cupsdLogMessage(CUPSD_LOG_WARN
,
727 "cupsdAuthorize: pam_setcred() "
728 "returned %d (%s)!", pamerr
,
729 pam_strerror(pamh
, pamerr
));
730 # endif /* HAVE_PAM_SETCRED */
732 pamerr
= pam_acct_mgmt(pamh
, PAM_SILENT
);
733 if (pamerr
!= PAM_SUCCESS
)
735 cupsdLogMessage(CUPSD_LOG_ERROR
,
736 "cupsdAuthorize: pam_acct_mgmt() returned %d "
738 pamerr
, pam_strerror(pamh
, pamerr
));
743 pam_end(pamh
, PAM_SUCCESS
);
745 #elif defined(HAVE_USERSEC_H)
747 * Use AIX authentication interface...
750 char *authmsg
; /* Authentication message */
751 int reenter
; /* ??? */
754 cupsdLogMessage(CUPSD_LOG_DEBUG
,
755 "cupsdAuthorize: AIX authenticate of username "
759 if (authenticate(username
, password
, &reenter
, &authmsg
) != 0)
761 cupsdLogMessage(CUPSD_LOG_DEBUG
,
762 "cupsdAuthorize: Unable to authenticate username "
764 username
, strerror(errno
));
770 * Use normal UNIX password file-based authentication...
773 char *pass
; /* Encrypted password */
774 struct passwd
*pw
; /* User password data */
775 # ifdef HAVE_SHADOW_H
776 struct spwd
*spw
; /* Shadow password data */
777 # endif /* HAVE_SHADOW_H */
780 pw
= getpwnam(username
); /* Get the current password */
781 endpwent(); /* Close the password file */
789 cupsdLogMessage(CUPSD_LOG_ERROR
,
790 "cupsdAuthorize: Unknown username \"%s\"!",
795 # ifdef HAVE_SHADOW_H
796 spw
= getspnam(username
);
799 if (!spw
&& !strcmp(pw
->pw_passwd
, "x"))
802 * Don't allow blank passwords!
805 cupsdLogMessage(CUPSD_LOG_ERROR
,
806 "cupsdAuthorize: Username \"%s\" has no shadow "
807 "password!", username
);
811 if (spw
&& !spw
->sp_pwdp
[0] && !pw
->pw_passwd
[0])
813 if (!pw
->pw_passwd
[0])
814 # endif /* HAVE_SHADOW_H */
817 * Don't allow blank passwords!
820 cupsdLogMessage(CUPSD_LOG_ERROR
,
821 "cupsdAuthorize: Username \"%s\" has no password!",
827 * OK, the password isn't blank, so compare with what came from the
831 pass
= cups_crypt(password
, pw
->pw_passwd
);
833 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
834 "cupsdAuthorize: pw_passwd=\"%s\", crypt=\"%s\"",
835 pw
->pw_passwd
, pass
);
837 if (!pass
|| strcmp(pw
->pw_passwd
, pass
))
839 # ifdef HAVE_SHADOW_H
842 pass
= cups_crypt(password
, spw
->sp_pwdp
);
844 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
845 "cupsdAuthorize: sp_pwdp=\"%s\", crypt=\"%s\"",
848 if (pass
== NULL
|| strcmp(spw
->sp_pwdp
, pass
))
850 cupsdLogMessage(CUPSD_LOG_ERROR
,
851 "cupsdAuthorize: Authentication failed for "
858 # endif /* HAVE_SHADOW_H */
860 cupsdLogMessage(CUPSD_LOG_ERROR
,
861 "cupsdAuthorize: Authentication failed for "
867 #endif /* HAVE_LIBPAM */
870 cupsdLogMessage(CUPSD_LOG_DEBUG
,
871 "cupsdAuthorize: Authorized as %s using Basic",
875 case CUPSD_AUTH_BASICDIGEST
:
877 * Do Basic authentication with the Digest password file...
880 if (!get_md5_password(username
, NULL
, md5
))
882 cupsdLogMessage(CUPSD_LOG_ERROR
,
883 "cupsdAuthorize: Unknown MD5 username \"%s\"!",
888 httpMD5(username
, "CUPS", password
, basicmd5
);
890 if (strcmp(md5
, basicmd5
))
892 cupsdLogMessage(CUPSD_LOG_ERROR
,
893 "cupsdAuthorize: Authentication failed for \"%s\"!",
898 cupsdLogMessage(CUPSD_LOG_DEBUG
,
899 "cupsdAuthorize: Authorized as %s using BasicDigest",
906 else if (!strncmp(authorization
, "Digest", 6))
909 * Get the username, password, and nonce from the Digest attributes...
912 if (!httpGetSubField2(&(con
->http
), HTTP_FIELD_AUTHORIZATION
, "username",
913 username
, sizeof(username
)) || !username
[0])
916 * Username must not be empty...
919 cupsdLogMessage(CUPSD_LOG_ERROR
,
920 "cupsdAuthorize: Empty or missing Digest username!");
924 if (!httpGetSubField2(&(con
->http
), HTTP_FIELD_AUTHORIZATION
, "response",
925 password
, sizeof(password
)) || !password
[0])
928 * Password must not be empty...
931 cupsdLogMessage(CUPSD_LOG_ERROR
,
932 "cupsdAuthorize: Empty or missing Digest password!");
936 if (!httpGetSubField(&(con
->http
), HTTP_FIELD_AUTHORIZATION
, "nonce",
939 cupsdLogMessage(CUPSD_LOG_ERROR
,
940 "cupsdAuthorize: No nonce value for Digest "
945 if (strcmp(con
->http
.hostname
, nonce
))
947 cupsdLogMessage(CUPSD_LOG_ERROR
,
948 "cupsdAuthorize: Bad nonce value, expected \"%s\", "
949 "got \"%s\"!", con
->http
.hostname
, nonce
);
954 * Validate the username and password...
957 if (!get_md5_password(username
, NULL
, md5
))
959 cupsdLogMessage(CUPSD_LOG_ERROR
,
960 "cupsdAuthorize: Unknown MD5 username \"%s\"!",
965 httpMD5Final(nonce
, states
[con
->http
.state
], con
->uri
, md5
);
967 if (strcmp(md5
, password
))
969 cupsdLogMessage(CUPSD_LOG_ERROR
,
970 "cupsdAuthorize: Authentication failed for \"%s\"!",
975 cupsdLogMessage(CUPSD_LOG_DEBUG
,
976 "cupsdAuthorize: Authorized as %s using Digest",
979 con
->type
= CUPSD_AUTH_DIGEST
;
982 # ifdef HAVE_KRB5_IPC_CLIENT_SET_TARGET_UID
983 else if (con
->http
.hostaddr
->addr
.sa_family
== AF_LOCAL
&&
984 !strncmp(authorization
, "Negotiate", 9))
987 * Pull the credentials directly from the user...
990 krb5_error_code error
; /* Kerberos error code */
991 cupsd_ucred_t peercred
; /* Peer credentials */
992 socklen_t peersize
; /* Size of peer credentials */
993 krb5_ccache peerccache
; /* Peer Kerberos credentials */
994 krb5_principal peerprncpl
; /* Peer's default principal */
995 char *peername
; /* Peer username */
997 peersize
= sizeof(peercred
);
1000 if (getsockopt(con
->http
.fd
, 0, LOCAL_PEERCRED
, &peercred
, &peersize
))
1002 if (getsockopt(con
->http
.fd
, SOL_SOCKET
, SO_PEERCRED
, &peercred
, &peersize
))
1003 # endif /* __APPLE__ */
1005 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to get peer credentials - %s",
1010 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1011 "cupsdAuthorize: Copying credentials for UID %d...",
1012 CUPSD_UCRED_UID(peercred
));
1014 if (!KerberosInitialized
)
1017 * Setup a Kerberos context for the scheduler to use...
1020 KerberosInitialized
= 1;
1022 if (krb5_init_context(&KerberosContext
))
1024 KerberosContext
= NULL
;
1026 cupsdLogMessage(CUPSD_LOG_ERROR
,
1027 "Unable to initialize Kerberos context");
1032 krb5_ipc_client_set_target_uid(CUPSD_UCRED_UID(peercred
));
1034 if ((error
= krb5_cc_default(KerberosContext
, &peerccache
)) != 0)
1036 cupsdLogMessage(CUPSD_LOG_ERROR
,
1037 "Unable to get credentials cache for UID %d (%d/%s)",
1038 (int)CUPSD_UCRED_UID(peercred
), error
, strerror(errno
));
1039 krb5_ipc_client_clear_target();
1043 if ((error
= krb5_cc_get_principal(KerberosContext
, peerccache
,
1046 cupsdLogMessage(CUPSD_LOG_ERROR
,
1047 "Unable to get Kerberos principal for UID %d",
1048 (int)CUPSD_UCRED_UID(peercred
));
1049 krb5_cc_close(KerberosContext
, peerccache
);
1050 krb5_ipc_client_clear_target();
1054 if ((error
= krb5_unparse_name(KerberosContext
, peerprncpl
,
1057 cupsdLogMessage(CUPSD_LOG_ERROR
,
1058 "Unable to get Kerberos name for UID %d",
1059 (int)CUPSD_UCRED_UID(peercred
));
1060 krb5_cc_close(KerberosContext
, peerccache
);
1061 krb5_ipc_client_clear_target();
1065 strlcpy(username
, peername
, sizeof(username
));
1068 con
->type
= CUPSD_AUTH_NEGOTIATE
;
1072 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1073 "cupsdAuthorize: Authorized as %s using Negotiate",
1076 krb5_cc_close(KerberosContext
, peerccache
);
1077 krb5_ipc_client_clear_target();
1079 # endif /* HAVE_KRB5_IPC_CLIENT_SET_TARGET_UID */
1080 else if (!strncmp(authorization
, "Negotiate", 9))
1082 int len
; /* Length of authorization string */
1083 gss_ctx_id_t context
; /* Authorization context */
1084 OM_uint32 major_status
, /* Major status code */
1085 minor_status
; /* Minor status code */
1086 gss_buffer_desc input_token
= GSS_C_EMPTY_BUFFER
,
1087 /* Input token from string */
1088 output_token
= GSS_C_EMPTY_BUFFER
;
1089 /* Output token for username */
1090 gss_name_t client_name
; /* Client name */
1095 * If the weak-linked GSSAPI/Kerberos library is not present, don't try
1099 if (gss_init_sec_context
== NULL
)
1101 cupsdLogMessage(CUPSD_LOG_WARN
,
1102 "GSSAPI/Kerberos authentication failed because the "
1103 "Kerberos framework is not present.");
1106 # endif /* __APPLE__ */
1108 con
->gss_output_token
.length
= 0;
1111 * Find the start of the Kerberos input token...
1115 while (isspace(*authorization
& 255))
1118 if (!*authorization
)
1120 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1121 "cupsdAuthorize: No authentication data specified.");
1126 * Decode the authorization string to get the input token...
1129 len
= strlen(authorization
);
1130 input_token
.value
= malloc(len
);
1131 input_token
.value
= httpDecode64_2(input_token
.value
, &len
,
1133 input_token
.length
= len
;
1136 * Accept the input token to get the authorization info...
1139 context
= GSS_C_NO_CONTEXT
;
1140 client_name
= GSS_C_NO_NAME
;
1141 major_status
= gss_accept_sec_context(&minor_status
,
1143 GSS_C_NO_CREDENTIAL
,
1145 GSS_C_NO_CHANNEL_BINDINGS
,
1148 &con
->gss_output_token
,
1153 if (GSS_ERROR(major_status
))
1155 cupsdLogGSSMessage(CUPSD_LOG_DEBUG
, major_status
, minor_status
,
1156 "cupsdAuthorize: Error accepting GSSAPI security "
1159 if (context
!= GSS_C_NO_CONTEXT
)
1160 gss_delete_sec_context(&minor_status
, &context
, GSS_C_NO_BUFFER
);
1167 * Get the username associated with the client's credentials...
1170 if (!con
->gss_creds
)
1171 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1172 "cupsdAuthorize: No delegated credentials!");
1174 if (major_status
== GSS_S_CONTINUE_NEEDED
)
1175 cupsdLogGSSMessage(CUPSD_LOG_DEBUG
, major_status
, minor_status
,
1176 "cupsdAuthorize: Credentials not complete");
1177 else if (major_status
== GSS_S_COMPLETE
)
1179 major_status
= gss_display_name(&minor_status
, client_name
,
1180 &output_token
, NULL
);
1182 if (GSS_ERROR(major_status
))
1184 cupsdLogGSSMessage(CUPSD_LOG_DEBUG
, major_status
, minor_status
,
1185 "cupsdAuthorize: Error getting username");
1186 gss_release_cred(&minor_status
, &con
->gss_creds
);
1187 gss_release_name(&minor_status
, &client_name
);
1188 gss_delete_sec_context(&minor_status
, &context
, GSS_C_NO_BUFFER
);
1192 strlcpy(username
, output_token
.value
, sizeof(username
));
1194 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1195 "cupsdAuthorize: Authorized as %s using Negotiate",
1198 gss_release_name(&minor_status
, &client_name
);
1199 gss_release_buffer(&minor_status
, &output_token
);
1201 con
->type
= CUPSD_AUTH_NEGOTIATE
;
1204 gss_release_cred(&minor_status
, &con
->gss_creds
);
1206 gss_delete_sec_context(&minor_status
, &context
, GSS_C_NO_BUFFER
);
1208 #endif /* HAVE_GSSAPI */
1211 char scheme
[256]; /* Auth scheme... */
1214 if (sscanf(authorization
, "%255s", scheme
) != 1)
1215 strcpy(scheme
, "UNKNOWN");
1217 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad authentication data \"%s ...\"",
1223 * If we get here, then we were able to validate the username and
1224 * password - copy the validated username and password to the client
1225 * data and return...
1228 strlcpy(con
->username
, username
, sizeof(con
->username
));
1229 strlcpy(con
->password
, password
, sizeof(con
->password
));
1234 * 'cupsdCheckAccess()' - Check whether the given address is allowed to
1235 * access a location.
1238 int /* O - 1 if allowed, 0 otherwise */
1240 unsigned ip
[4], /* I - Client address */
1241 char *name
, /* I - Client hostname */
1242 int namelen
, /* I - Length of hostname */
1243 cupsd_location_t
*loc
) /* I - Location to check */
1245 int allow
; /* 1 if allowed, 0 otherwise */
1248 if (!strcasecmp(name
, "localhost"))
1251 * Access from localhost (127.0.0.1 or ::1) is always allowed...
1259 * Do authorization checks on the domain/address...
1262 switch (loc
->order_type
)
1265 allow
= 0; /* anti-compiler-warning-code */
1268 case CUPSD_AUTH_ALLOW
: /* Order Deny,Allow */
1271 if (cupsdCheckAuth(ip
, name
, namelen
, loc
->deny
))
1274 if (cupsdCheckAuth(ip
, name
, namelen
, loc
->allow
))
1278 case CUPSD_AUTH_DENY
: /* Order Allow,Deny */
1281 if (cupsdCheckAuth(ip
, name
, namelen
, loc
->allow
))
1284 if (cupsdCheckAuth(ip
, name
, namelen
, loc
->deny
))
1295 * 'cupsdCheckAuth()' - Check authorization masks.
1298 int /* O - 1 if mask matches, 0 otherwise */
1299 cupsdCheckAuth(unsigned ip
[4], /* I - Client address */
1300 char *name
, /* I - Client hostname */
1301 int name_len
, /* I - Length of hostname */
1302 cups_array_t
*masks
) /* I - Masks */
1304 int i
; /* Looping var */
1305 cupsd_authmask_t
*mask
; /* Current mask */
1306 cupsd_netif_t
*iface
; /* Network interface */
1307 unsigned netip4
; /* IPv4 network address */
1309 unsigned netip6
[4]; /* IPv6 network address */
1310 #endif /* AF_INET6 */
1313 for (mask
= (cupsd_authmask_t
*)cupsArrayFirst(masks
);
1315 mask
= (cupsd_authmask_t
*)cupsArrayNext(masks
))
1319 case CUPSD_AUTH_INTERFACE
:
1321 * Check for a match with a network interface...
1324 netip4
= htonl(ip
[3]);
1327 netip6
[0] = htonl(ip
[0]);
1328 netip6
[1] = htonl(ip
[1]);
1329 netip6
[2] = htonl(ip
[2]);
1330 netip6
[3] = htonl(ip
[3]);
1331 #endif /* AF_INET6 */
1333 if (!strcmp(mask
->mask
.name
.name
, "*"))
1337 * Allow Back-to-My-Mac addresses...
1340 if ((ip
[0] & 0xff000000) == 0xfd000000)
1342 #endif /* __APPLE__ */
1345 * Check against all local interfaces...
1350 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
1352 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
1355 * Only check local interfaces...
1358 if (!iface
->is_local
)
1361 if (iface
->address
.addr
.sa_family
== AF_INET
)
1364 * Check IPv4 address...
1367 if ((netip4
& iface
->mask
.ipv4
.sin_addr
.s_addr
) ==
1368 (iface
->address
.ipv4
.sin_addr
.s_addr
&
1369 iface
->mask
.ipv4
.sin_addr
.s_addr
))
1376 * Check IPv6 address...
1379 for (i
= 0; i
< 4; i
++)
1380 if ((netip6
[i
] & iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]) !=
1381 (iface
->address
.ipv6
.sin6_addr
.s6_addr32
[i
] &
1382 iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]))
1388 #endif /* AF_INET6 */
1394 * Check the named interface...
1397 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
1399 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
1401 if (strcmp(mask
->mask
.name
.name
, iface
->name
))
1404 if (iface
->address
.addr
.sa_family
== AF_INET
)
1407 * Check IPv4 address...
1410 if ((netip4
& iface
->mask
.ipv4
.sin_addr
.s_addr
) ==
1411 (iface
->address
.ipv4
.sin_addr
.s_addr
&
1412 iface
->mask
.ipv4
.sin_addr
.s_addr
))
1419 * Check IPv6 address...
1422 for (i
= 0; i
< 4; i
++)
1423 if ((netip6
[i
] & iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]) !=
1424 (iface
->address
.ipv6
.sin6_addr
.s6_addr32
[i
] &
1425 iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]))
1431 #endif /* AF_INET6 */
1436 case CUPSD_AUTH_NAME
:
1438 * Check for exact name match...
1441 if (!strcasecmp(name
, mask
->mask
.name
.name
))
1445 * Check for domain match...
1448 if (name_len
>= mask
->mask
.name
.length
&&
1449 mask
->mask
.name
.name
[0] == '.' &&
1450 !strcasecmp(name
+ name_len
- mask
->mask
.name
.length
,
1451 mask
->mask
.name
.name
))
1455 case CUPSD_AUTH_IP
:
1457 * Check for IP/network address match...
1460 for (i
= 0; i
< 4; i
++)
1461 if ((ip
[i
] & mask
->mask
.ip
.netmask
[i
]) !=
1462 mask
->mask
.ip
.address
[i
])
1476 * 'cupsdCheckGroup()' - Check for a user's group membership.
1479 int /* O - 1 if user is a member, 0 otherwise */
1481 const char *username
, /* I - User name */
1482 struct passwd
*user
, /* I - System user info */
1483 const char *groupname
) /* I - Group name */
1485 int i
; /* Looping var */
1486 struct group
*group
; /* System group info */
1487 char junk
[33]; /* MD5 password (not used) */
1488 #ifdef HAVE_MBR_UID_TO_UUID
1489 uuid_t useruuid
, /* UUID for username */
1490 groupuuid
; /* UUID for groupname */
1491 int is_member
; /* True if user is a member of group */
1492 #endif /* HAVE_MBR_UID_TO_UUID */
1495 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1496 "cupsdCheckGroup(username=\"%s\", user=%p, groupname=\"%s\")",
1497 username
, user
, groupname
);
1503 if (!username
|| !groupname
)
1507 * Check to see if the user is a member of the named group...
1510 group
= getgrnam(groupname
);
1516 * Group exists, check it...
1519 for (i
= 0; group
->gr_mem
[i
]; i
++)
1520 if (!strcasecmp(username
, group
->gr_mem
[i
]))
1525 * Group doesn't exist or user not in group list, check the group ID
1526 * against the user's group ID...
1529 if (user
&& group
&& group
->gr_gid
== user
->pw_gid
)
1532 #ifdef HAVE_MBR_UID_TO_UUID
1534 * Check group membership through MacOS X membership API...
1537 if (user
&& !mbr_uid_to_uuid(user
->pw_uid
, useruuid
))
1542 * Map group name to UUID and check membership...
1545 if (!mbr_gid_to_uuid(group
->gr_gid
, groupuuid
))
1546 if (!mbr_check_membership(useruuid
, groupuuid
, &is_member
))
1550 else if (groupname
[0] == '#')
1553 * Use UUID directly and check for equality (user UUID) and
1554 * membership (group UUID)...
1557 if (!uuid_parse((char *)groupname
+ 1, groupuuid
))
1559 if (!uuid_compare(useruuid
, groupuuid
))
1561 else if (!mbr_check_membership(useruuid
, groupuuid
, &is_member
))
1569 else if (groupname
[0] == '#')
1571 #endif /* HAVE_MBR_UID_TO_UUID */
1574 * Username not found, group not found, or user is not part of the
1575 * system group... Check for a user and group in the MD5 password
1579 if (get_md5_password(username
, groupname
, junk
) != NULL
)
1583 * If we get this far, then the user isn't part of the named group...
1592 * 'cupsdCopyKrb5Creds()' - Get a copy of the Kerberos credentials.
1595 krb5_ccache
/* O - Credentials or NULL */
1596 cupsdCopyKrb5Creds(cupsd_client_t
*con
) /* I - Client connection */
1598 # if !defined(HAVE_KRB5_CC_NEW_UNIQUE) && !defined(HAVE_HEIMDAL)
1599 cupsdLogMessage(CUPSD_LOG_INFO
,
1600 "Sorry, your version of Kerberos does not support delegated "
1605 krb5_ccache ccache
= NULL
; /* Credentials */
1606 krb5_error_code error
; /* Kerberos error code */
1607 OM_uint32 major_status
, /* Major status code */
1608 minor_status
; /* Minor status code */
1609 krb5_principal principal
; /* Kerberos principal */
1614 * If the weak-linked GSSAPI/Kerberos library is not present, don't try
1618 if (krb5_init_context
== NULL
)
1620 # endif /* __APPLE__ */
1622 if (!KerberosInitialized
)
1625 * Setup a Kerberos context for the scheduler to use...
1628 KerberosInitialized
= 1;
1630 if (krb5_init_context(&KerberosContext
))
1632 KerberosContext
= NULL
;
1634 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to initialize Kerberos context");
1640 * We MUST create a file-based cache because memory-based caches are
1641 * only valid for the current process/address space.
1643 * Due to various bugs/features in different versions of Kerberos, we
1644 * need either the krb5_cc_new_unique() function or Heimdal's version
1645 * of krb5_cc_gen_new() to create a new FILE: credential cache that
1646 * can be passed to the backend. These functions create a temporary
1647 * file (typically in /tmp) containing the cached credentials, which
1648 * are removed when we have successfully printed a job.
1651 # ifdef HAVE_KRB5_CC_NEW_UNIQUE
1652 if ((error
= krb5_cc_new_unique(KerberosContext
, "FILE", NULL
, &ccache
)) != 0)
1653 # else /* HAVE_HEIMDAL */
1654 if ((error
= krb5_cc_gen_new(KerberosContext
, &krb5_fcc_ops
, &ccache
)) != 0)
1655 # endif /* HAVE_KRB5_CC_NEW_UNIQUE */
1657 cupsdLogMessage(CUPSD_LOG_ERROR
,
1658 "Unable to create new credentials cache (%d/%s)",
1659 error
, strerror(errno
));
1663 if ((error
= krb5_parse_name(KerberosContext
, con
->username
, &principal
)) != 0)
1665 cupsdLogMessage(CUPSD_LOG_ERROR
,
1666 "Unable to parse kerberos username (%d/%s)", error
,
1668 krb5_cc_destroy(KerberosContext
, ccache
);
1672 if ((error
= krb5_cc_initialize(KerberosContext
, ccache
, principal
)))
1674 cupsdLogMessage(CUPSD_LOG_ERROR
,
1675 "Unable to initialize credentials cache (%d/%s)", error
,
1677 krb5_cc_destroy(KerberosContext
, ccache
);
1678 krb5_free_principal(KerberosContext
, principal
);
1682 krb5_free_principal(KerberosContext
, principal
);
1685 * Copy the user's credentials to the new cache file...
1688 # ifdef HAVE_KRB5_IPC_CLIENT_SET_TARGET_UID
1689 if (con
->http
.hostaddr
->addr
.sa_family
== AF_LOCAL
&&
1690 !(con
->gss_flags
& GSS_C_DELEG_FLAG
))
1693 * Pull the credentials directly from the user...
1696 cupsd_ucred_t peercred
; /* Peer credentials */
1697 socklen_t peersize
; /* Size of peer credentials */
1698 krb5_ccache peerccache
; /* Peer Kerberos credentials */
1700 peersize
= sizeof(peercred
);
1703 if (getsockopt(con
->http
.fd
, 0, LOCAL_PEERCRED
, &peercred
, &peersize
))
1705 if (getsockopt(con
->http
.fd
, SOL_SOCKET
, SO_PEERCRED
, &peercred
, &peersize
))
1706 # endif /* __APPLE__ */
1708 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to get peer credentials - %s",
1710 krb5_cc_destroy(KerberosContext
, ccache
);
1714 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1715 "cupsdCopyKrb5Creds: Copying credentials for UID %d...",
1716 CUPSD_UCRED_UID(peercred
));
1718 krb5_ipc_client_set_target_uid(CUPSD_UCRED_UID(peercred
));
1720 if ((error
= krb5_cc_default(KerberosContext
, &peerccache
)) != 0)
1722 cupsdLogMessage(CUPSD_LOG_ERROR
,
1723 "Unable to get credentials cache for UID %d (%d/%s)",
1724 (int)CUPSD_UCRED_UID(peercred
), error
, strerror(errno
));
1725 krb5_cc_destroy(KerberosContext
, ccache
);
1729 error
= krb5_cc_copy_creds(KerberosContext
, peerccache
, ccache
);
1730 krb5_cc_close(KerberosContext
, peerccache
);
1731 krb5_ipc_client_clear_target();
1735 cupsdLogMessage(CUPSD_LOG_ERROR
,
1736 "Unable to copy credentials cache for UID %d (%d/%s)",
1737 (int)CUPSD_UCRED_UID(peercred
), error
, strerror(errno
));
1738 krb5_cc_destroy(KerberosContext
, ccache
);
1743 # endif /* HAVE_KRB5_IPC_CLIENT_SET_TARGET_UID */
1745 major_status
= gss_krb5_copy_ccache(&minor_status
, con
->gss_creds
, ccache
);
1747 if (GSS_ERROR(major_status
))
1749 cupsdLogGSSMessage(CUPSD_LOG_ERROR
, major_status
, minor_status
,
1750 "Unable to copy client credentials cache");
1751 krb5_cc_destroy(KerberosContext
, ccache
);
1757 # endif /* !HAVE_KRB5_CC_NEW_UNIQUE && !HAVE_HEIMDAL */
1759 #endif /* HAVE_GSSAPI */
1763 * 'cupsdCopyLocation()' - Make a copy of a location...
1766 cupsd_location_t
* /* O - New location */
1768 cupsd_location_t
*loc
) /* I - Original location */
1770 cupsd_location_t
*temp
; /* New location */
1774 * Make a copy of the original location...
1777 if ((temp
= calloc(1, sizeof(cupsd_location_t
))) == NULL
)
1781 * Copy the information from the original location to the new one.
1788 temp
->location
= _cupsStrAlloc(loc
->location
);
1790 temp
->limit
= loc
->limit
;
1791 temp
->order_type
= loc
->order_type
;
1792 temp
->type
= loc
->type
;
1793 temp
->level
= loc
->level
;
1794 temp
->satisfy
= loc
->satisfy
;
1795 temp
->encryption
= loc
->encryption
;
1799 if ((temp
->names
= cupsArrayDup(loc
->names
)) == NULL
)
1801 cupsdLogMessage(CUPSD_LOG_ERROR
,
1802 "Unable to allocate memory for %d names: %s",
1803 cupsArrayCount(loc
->names
), strerror(errno
));
1805 cupsdFreeLocation(temp
);
1813 * Copy allow rules...
1816 if ((temp
->allow
= cupsArrayDup(loc
->allow
)) == NULL
)
1818 cupsdLogMessage(CUPSD_LOG_ERROR
,
1819 "Unable to allocate memory for %d allow rules: %s",
1820 cupsArrayCount(loc
->allow
), strerror(errno
));
1821 cupsdFreeLocation(temp
);
1829 * Copy deny rules...
1832 if ((temp
->deny
= cupsArrayDup(loc
->deny
)) == NULL
)
1834 cupsdLogMessage(CUPSD_LOG_ERROR
,
1835 "Unable to allocate memory for %d deny rules: %s",
1836 cupsArrayCount(loc
->deny
), strerror(errno
));
1837 cupsdFreeLocation(temp
);
1847 * 'cupsdDeleteAllLocations()' - Free all memory used for location authorization.
1851 cupsdDeleteAllLocations(void)
1854 * Free the location array, which will free all of the locations...
1857 cupsArrayDelete(Locations
);
1863 * 'cupsdFindBest()' - Find the location entry that best matches the resource.
1866 cupsd_location_t
* /* O - Location that matches */
1867 cupsdFindBest(const char *path
, /* I - Resource path */
1868 http_state_t state
) /* I - HTTP state/request */
1870 char uri
[HTTP_MAX_URI
],
1871 /* URI in request... */
1872 *uriptr
; /* Pointer into URI */
1873 cupsd_location_t
*loc
, /* Current location */
1874 *best
; /* Best match for location so far */
1875 int bestlen
; /* Length of best match */
1876 int limit
; /* Limit field */
1877 static const int limits
[] = /* Map http_status_t to CUPSD_AUTH_LIMIT_xyz */
1879 CUPSD_AUTH_LIMIT_ALL
,
1880 CUPSD_AUTH_LIMIT_OPTIONS
,
1881 CUPSD_AUTH_LIMIT_GET
,
1882 CUPSD_AUTH_LIMIT_GET
,
1883 CUPSD_AUTH_LIMIT_HEAD
,
1884 CUPSD_AUTH_LIMIT_POST
,
1885 CUPSD_AUTH_LIMIT_POST
,
1886 CUPSD_AUTH_LIMIT_POST
,
1887 CUPSD_AUTH_LIMIT_PUT
,
1888 CUPSD_AUTH_LIMIT_PUT
,
1889 CUPSD_AUTH_LIMIT_DELETE
,
1890 CUPSD_AUTH_LIMIT_TRACE
,
1891 CUPSD_AUTH_LIMIT_ALL
,
1892 CUPSD_AUTH_LIMIT_ALL
1897 * First copy the connection URI to a local string so we have drop
1898 * any .ppd extension from the pathname in /printers or /classes
1902 strlcpy(uri
, path
, sizeof(uri
));
1904 if (!strncmp(uri
, "/printers/", 10) ||
1905 !strncmp(uri
, "/classes/", 9))
1908 * Check if the URI has .ppd on the end...
1911 uriptr
= uri
+ strlen(uri
) - 4; /* len > 4 if we get here... */
1913 if (!strcmp(uriptr
, ".ppd"))
1917 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: uri = \"%s\"...", uri
);
1920 * Loop through the list of locations to find a match...
1923 limit
= limits
[state
];
1927 for (loc
= (cupsd_location_t
*)cupsArrayFirst(Locations
);
1929 loc
= (cupsd_location_t
*)cupsArrayNext(Locations
))
1931 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: Location %s Limit %x",
1932 loc
->location
? loc
->location
: "nil", loc
->limit
);
1934 if (!strncmp(uri
, "/printers/", 10) || !strncmp(uri
, "/classes/", 9))
1937 * Use case-insensitive comparison for queue names...
1940 if (loc
->length
> bestlen
&& loc
->location
&&
1941 !strncasecmp(uri
, loc
->location
, loc
->length
) &&
1942 loc
->location
[0] == '/' &&
1943 (limit
& loc
->limit
) != 0)
1946 bestlen
= loc
->length
;
1952 * Use case-sensitive comparison for other URIs...
1955 if (loc
->length
> bestlen
&& loc
->location
&&
1956 !strncmp(uri
, loc
->location
, loc
->length
) &&
1957 loc
->location
[0] == '/' &&
1958 (limit
& loc
->limit
) != 0)
1961 bestlen
= loc
->length
;
1967 * Return the match, if any...
1970 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: best = %s",
1971 best
? best
->location
: "NONE");
1978 * 'cupsdFindLocation()' - Find the named location.
1981 cupsd_location_t
* /* O - Location that matches */
1982 cupsdFindLocation(const char *location
) /* I - Connection */
1984 cupsd_location_t key
; /* Search key */
1987 key
.location
= (char *)location
;
1989 return ((cupsd_location_t
*)cupsArrayFind(Locations
, &key
));
1994 * 'cupsdFreeLocation()' - Free all memory used by a location.
1998 cupsdFreeLocation(cupsd_location_t
*loc
)/* I - Location to free */
2000 cupsArrayDelete(loc
->names
);
2001 cupsArrayDelete(loc
->allow
);
2002 cupsArrayDelete(loc
->deny
);
2004 _cupsStrFree(loc
->location
);
2010 * 'cupsdIsAuthorized()' - Check to see if the user is authorized...
2013 http_status_t
/* O - HTTP_OK if authorized or error code */
2014 cupsdIsAuthorized(cupsd_client_t
*con
, /* I - Connection */
2015 const char *owner
)/* I - Owner of object */
2017 int i
, /* Looping vars */
2018 auth
, /* Authorization status */
2019 type
; /* Type of authentication */
2020 unsigned address
[4]; /* Authorization address */
2021 cupsd_location_t
*best
; /* Best match for location so far */
2022 int hostlen
; /* Length of hostname */
2023 char *name
, /* Current username */
2024 username
[256], /* Username to authorize */
2025 ownername
[256], /* Owner name to authorize */
2026 *ptr
; /* Pointer into username */
2027 struct passwd
*pw
; /* User password data */
2028 static const char * const levels
[] = /* Auth levels */
2034 static const char * const types
[] = /* Auth types */
2044 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2045 "cupsdIsAuthorized: con->uri=\"%s\", con->best=%p(%s)",
2046 con
->uri
, con
->best
, con
->best
? con
->best
->location
?
2047 con
->best
->location
: "(null)" : "");
2049 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2050 "cupsdIsAuthorized: owner=\"%s\"", owner
);
2053 * If there is no "best" authentication rule for this request, then
2054 * access is allowed from the local system and denied from other
2060 if (!strcmp(con
->http
.hostname
, "localhost") ||
2061 !strcmp(con
->http
.hostname
, ServerName
))
2064 return (HTTP_FORBIDDEN
);
2069 if ((type
= best
->type
) == CUPSD_AUTH_DEFAULT
)
2070 type
= DefaultAuthType
;
2072 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2073 "cupsdIsAuthorized: level=CUPSD_AUTH_%s, type=%s, "
2074 "satisfy=CUPSD_AUTH_SATISFY_%s, num_names=%d",
2075 levels
[best
->level
], types
[type
],
2076 best
->satisfy
? "ANY" : "ALL", cupsArrayCount(best
->names
));
2078 if (best
->limit
== CUPSD_AUTH_LIMIT_IPP
)
2079 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: op=%x(%s)",
2080 best
->op
, ippOpString(best
->op
));
2083 * Check host/ip-based accesses...
2087 if (con
->http
.hostaddr
->addr
.sa_family
== AF_INET6
)
2090 * Copy IPv6 address...
2093 address
[0] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[0]);
2094 address
[1] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[1]);
2095 address
[2] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[2]);
2096 address
[3] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[3]);
2099 #endif /* AF_INET6 */
2100 if (con
->http
.hostaddr
->addr
.sa_family
== AF_INET
)
2103 * Copy IPv4 address...
2109 address
[3] = ntohl(con
->http
.hostaddr
->ipv4
.sin_addr
.s_addr
);
2112 memset(address
, 0, sizeof(address
));
2114 hostlen
= strlen(con
->http
.hostname
);
2116 auth
= cupsdCheckAccess(address
, con
->http
.hostname
, hostlen
, best
)
2117 ? CUPSD_AUTH_ALLOW
: CUPSD_AUTH_DENY
;
2119 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: auth=CUPSD_AUTH_%s...",
2120 auth
? "DENY" : "ALLOW");
2122 if (auth
== CUPSD_AUTH_DENY
&& best
->satisfy
== CUPSD_AUTH_SATISFY_ALL
)
2123 return (HTTP_FORBIDDEN
);
2127 * See if encryption is required...
2130 if ((best
->encryption
>= HTTP_ENCRYPT_REQUIRED
&& !con
->http
.tls
&&
2131 strcasecmp(con
->http
.hostname
, "localhost") &&
2132 best
->satisfy
== CUPSD_AUTH_SATISFY_ALL
) &&
2133 !(type
== CUPSD_AUTH_NEGOTIATE
||
2134 (type
== CUPSD_AUTH_NONE
&& DefaultAuthType
== CUPSD_AUTH_NEGOTIATE
)))
2136 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2137 "cupsdIsAuthorized: Need upgrade to TLS...");
2138 return (HTTP_UPGRADE_REQUIRED
);
2140 #endif /* HAVE_SSL */
2143 * Now see what access level is required...
2146 if (best
->level
== CUPSD_AUTH_ANON
|| /* Anonymous access - allow it */
2147 (type
== CUPSD_AUTH_NONE
&& cupsArrayCount(best
->names
) == 0))
2150 if (!con
->username
[0] && type
== CUPSD_AUTH_NONE
&&
2151 best
->limit
== CUPSD_AUTH_LIMIT_IPP
)
2154 * Check for unauthenticated username...
2157 ipp_attribute_t
*attr
; /* requesting-user-name attribute */
2160 attr
= ippFindAttribute(con
->request
, "requesting-user-name", IPP_TAG_NAME
);
2163 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2164 "cupsdIsAuthorized: requesting-user-name=\"%s\"",
2165 attr
->values
[0].string
.text
);
2166 strlcpy(username
, attr
->values
[0].string
.text
, sizeof(username
));
2168 else if (best
->satisfy
== CUPSD_AUTH_SATISFY_ALL
|| auth
== CUPSD_AUTH_DENY
)
2169 return (HTTP_UNAUTHORIZED
); /* Non-anonymous needs user/pass */
2171 return (HTTP_OK
); /* unless overridden with Satisfy */
2175 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdIsAuthorized: username=\"%s\"",
2178 #ifdef HAVE_AUTHORIZATION_H
2179 if (!con
->username
[0] && !con
->authref
)
2181 if (!con
->username
[0])
2182 #endif /* HAVE_AUTHORIZATION_H */
2184 if (best
->satisfy
== CUPSD_AUTH_SATISFY_ALL
|| auth
== CUPSD_AUTH_DENY
)
2185 return (HTTP_UNAUTHORIZED
); /* Non-anonymous needs user/pass */
2187 return (HTTP_OK
); /* unless overridden with Satisfy */
2190 if (con
->type
!= type
&& type
!= CUPSD_AUTH_NONE
&&
2191 (con
->type
!= CUPSD_AUTH_BASIC
|| type
!= CUPSD_AUTH_BASICDIGEST
))
2193 cupsdLogMessage(CUPSD_LOG_ERROR
, "Authorized using %s, expected %s!",
2194 types
[con
->type
], types
[type
]);
2196 return (HTTP_UNAUTHORIZED
);
2199 strlcpy(username
, con
->username
, sizeof(username
));
2203 * OK, got a username. See if we need normal user access, or group
2204 * access... (root always matches)
2207 if (!strcmp(username
, "root"))
2211 * Strip any @domain or @KDC from the username and owner...
2214 if ((ptr
= strchr(username
, '@')) != NULL
)
2219 strlcpy(ownername
, owner
, sizeof(ownername
));
2221 if ((ptr
= strchr(ownername
, '@')) != NULL
)
2225 ownername
[0] = '\0';
2228 * Get the user info...
2233 pw
= getpwnam(username
);
2239 if (best
->level
== CUPSD_AUTH_USER
)
2242 * If there are no names associated with this location, then
2243 * any valid user is OK...
2246 if (cupsArrayCount(best
->names
) == 0)
2250 * Otherwise check the user list and return OK if this user is
2254 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2255 "cupsdIsAuthorized: Checking user membership...");
2257 #ifdef HAVE_AUTHORIZATION_H
2259 * If an authorization reference was supplied it must match a right name...
2264 for (name
= (char *)cupsArrayFirst(best
->names
);
2266 name
= (char *)cupsArrayNext(best
->names
))
2268 if (!strncasecmp(name
, "@AUTHKEY(", 9) && check_authref(con
, name
+ 9))
2270 else if (!strcasecmp(name
, "@SYSTEM") && SystemGroupAuthKey
&&
2271 check_authref(con
, SystemGroupAuthKey
))
2275 return (HTTP_FORBIDDEN
);
2277 #endif /* HAVE_AUTHORIZATION_H */
2279 for (name
= (char *)cupsArrayFirst(best
->names
);
2281 name
= (char *)cupsArrayNext(best
->names
))
2283 if (!strcasecmp(name
, "@OWNER") && owner
&&
2284 !strcasecmp(username
, ownername
))
2286 else if (!strcasecmp(name
, "@SYSTEM"))
2288 for (i
= 0; i
< NumSystemGroups
; i
++)
2289 if (cupsdCheckGroup(username
, pw
, SystemGroups
[i
]))
2292 else if (name
[0] == '@')
2294 if (cupsdCheckGroup(username
, pw
, name
+ 1))
2297 else if (!strcasecmp(username
, name
))
2301 return (HTTP_FORBIDDEN
);
2305 * Check to see if this user is in any of the named groups...
2308 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2309 "cupsdIsAuthorized: Checking group membership...");
2312 * Check to see if this user is in any of the named groups...
2315 for (name
= (char *)cupsArrayFirst(best
->names
);
2317 name
= (char *)cupsArrayNext(best
->names
))
2319 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2320 "cupsdIsAuthorized: Checking group \"%s\" membership...",
2323 if (!strcasecmp(name
, "@SYSTEM"))
2325 for (i
= 0; i
< NumSystemGroups
; i
++)
2326 if (cupsdCheckGroup(username
, pw
, SystemGroups
[i
]))
2329 else if (cupsdCheckGroup(username
, pw
, name
))
2334 * The user isn't part of the specified group, so deny access...
2337 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2338 "cupsdIsAuthorized: User not in group(s)!");
2340 return (HTTP_FORBIDDEN
);
2345 * 'cupsdNewLocation()' - Create a new location for authorization.
2347 * Note: Still need to call cupsdAddLocation() to add it to the list of global
2351 cupsd_location_t
* /* O - Pointer to new location record */
2352 cupsdNewLocation(const char *location
) /* I - Location path */
2354 cupsd_location_t
*temp
; /* New location */
2358 * Try to allocate memory for the new location.
2361 if ((temp
= calloc(1, sizeof(cupsd_location_t
))) == NULL
)
2365 * Initialize the record and copy the name over...
2368 if ((temp
->location
= _cupsStrAlloc(location
)) == NULL
)
2374 temp
->length
= strlen(temp
->location
);
2377 * Return the new record...
2384 #ifdef HAVE_AUTHORIZATION_H
2386 * 'check_authref()' - Check if an authorization services reference has the
2390 static int /* O - 1 if right is valid, 0 otherwise */
2391 check_authref(cupsd_client_t
*con
, /* I - Connection */
2392 const char *right
) /* I - Right name */
2394 OSStatus status
; /* OS Status */
2395 AuthorizationItem authright
; /* Authorization right */
2396 AuthorizationRights authrights
; /* Authorization rights */
2397 AuthorizationFlags authflags
; /* Authorization flags */
2401 * Check to see if the user is allowed to perform the task...
2407 authright
.name
= right
;
2408 authright
.valueLength
= 0;
2409 authright
.value
= NULL
;
2410 authright
.flags
= 0;
2412 authrights
.count
= 1;
2413 authrights
.items
= &authright
;
2415 authflags
= kAuthorizationFlagDefaults
|
2416 kAuthorizationFlagExtendRights
;
2418 if ((status
= AuthorizationCopyRights(con
->authref
, &authrights
,
2419 kAuthorizationEmptyEnvironment
,
2420 authflags
, NULL
)) != 0)
2422 cupsdLogMessage(CUPSD_LOG_ERROR
,
2423 "AuthorizationCopyRights(\"%s\") returned %d (%s)",
2424 authright
.name
, (int)status
, cssmErrorString(status
));
2428 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2429 "AuthorizationCopyRights(\"%s\") succeeded!",
2434 #endif /* HAVE_AUTHORIZATION_H */
2438 * 'compare_locations()' - Compare two locations.
2441 static int /* O - Result of comparison */
2442 compare_locations(cupsd_location_t
*a
, /* I - First location */
2443 cupsd_location_t
*b
) /* I - Second location */
2445 return (strcmp(b
->location
, a
->location
));
2450 * 'copy_authmask()' - Copy function for auth masks.
2453 static cupsd_authmask_t
* /* O - New auth mask */
2454 copy_authmask(cupsd_authmask_t
*mask
, /* I - Existing auth mask */
2455 void *data
) /* I - User data (unused) */
2457 cupsd_authmask_t
*temp
; /* New auth mask */
2462 if ((temp
= malloc(sizeof(cupsd_authmask_t
))) != NULL
)
2464 memcpy(temp
, mask
, sizeof(cupsd_authmask_t
));
2466 if (temp
->type
== CUPSD_AUTH_NAME
|| temp
->type
== CUPSD_AUTH_INTERFACE
)
2469 * Make a copy of the name...
2472 if ((temp
->mask
.name
.name
= _cupsStrAlloc(temp
->mask
.name
.name
)) == NULL
)
2475 * Failed to make copy...
2488 #if !HAVE_LIBPAM && !defined(HAVE_USERSEC_H)
2490 * 'cups_crypt()' - Encrypt the password using the DES or MD5 algorithms,
2494 static char * /* O - Encrypted password */
2495 cups_crypt(const char *pw
, /* I - Password string */
2496 const char *salt
) /* I - Salt (key) string */
2498 if (!strncmp(salt
, "$1$", 3))
2501 * Use MD5 passwords without the benefit of PAM; this is for
2502 * Slackware Linux, and the algorithm was taken from the
2503 * old shadow-19990827/lib/md5crypt.c source code... :(
2506 int i
; /* Looping var */
2507 unsigned long n
; /* Output number */
2508 int pwlen
; /* Length of password string */
2509 const char *salt_end
; /* End of "salt" data for MD5 */
2510 char *ptr
; /* Pointer into result string */
2511 _cups_md5_state_t state
; /* Primary MD5 state info */
2512 _cups_md5_state_t state2
; /* Secondary MD5 state info */
2513 unsigned char digest
[16]; /* MD5 digest result */
2514 static char result
[120]; /* Final password string */
2518 * Get the salt data between dollar signs, e.g. $1$saltdata$md5.
2519 * Get a maximum of 8 characters of salt data after $1$...
2522 for (salt_end
= salt
+ 3; *salt_end
&& (salt_end
- salt
) < 11; salt_end
++)
2523 if (*salt_end
== '$')
2527 * Compute the MD5 sum we need...
2532 _cupsMD5Init(&state
);
2533 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
2534 _cupsMD5Append(&state
, (unsigned char *)salt
, salt_end
- salt
);
2536 _cupsMD5Init(&state2
);
2537 _cupsMD5Append(&state2
, (unsigned char *)pw
, pwlen
);
2538 _cupsMD5Append(&state2
, (unsigned char *)salt
+ 3, salt_end
- salt
- 3);
2539 _cupsMD5Append(&state2
, (unsigned char *)pw
, pwlen
);
2540 _cupsMD5Finish(&state2
, digest
);
2542 for (i
= pwlen
; i
> 0; i
-= 16)
2543 _cupsMD5Append(&state
, digest
, i
> 16 ? 16 : i
);
2545 for (i
= pwlen
; i
> 0; i
>>= 1)
2546 _cupsMD5Append(&state
, (unsigned char *)((i
& 1) ? "" : pw
), 1);
2548 _cupsMD5Finish(&state
, digest
);
2550 for (i
= 0; i
< 1000; i
++)
2552 _cupsMD5Init(&state
);
2555 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
2557 _cupsMD5Append(&state
, digest
, 16);
2560 _cupsMD5Append(&state
, (unsigned char *)salt
+ 3, salt_end
- salt
- 3);
2563 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
2566 _cupsMD5Append(&state
, digest
, 16);
2568 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
2570 _cupsMD5Finish(&state
, digest
);
2574 * Copy the final sum to the result string and return...
2577 memcpy(result
, salt
, salt_end
- salt
);
2578 ptr
= result
+ (salt_end
- salt
);
2581 for (i
= 0; i
< 5; i
++, ptr
+= 4)
2583 n
= (((digest
[i
] << 8) | digest
[i
+ 6]) << 8);
2586 n
|= digest
[i
+ 12];
2593 to64(ptr
, digest
[11], 2);
2602 * Use the standard crypt() function...
2605 return (crypt(pw
, salt
));
2608 #endif /* !HAVE_LIBPAM && !HAVE_USERSEC_H */
2612 * 'free_authmask()' - Free function for auth masks.
2616 free_authmask(cupsd_authmask_t
*mask
, /* I - Auth mask to free */
2617 void *data
) /* I - User data (unused) */
2621 if (mask
->type
== CUPSD_AUTH_NAME
|| mask
->type
== CUPSD_AUTH_INTERFACE
)
2622 _cupsStrFree(mask
->mask
.name
.name
);
2629 * 'get_md5_password()' - Get an MD5 password.
2632 static char * /* O - MD5 password string */
2633 get_md5_password(const char *username
, /* I - Username */
2634 const char *group
, /* I - Group */
2635 char passwd
[33]) /* O - MD5 password string */
2637 cups_file_t
*fp
; /* passwd.md5 file */
2638 char filename
[1024], /* passwd.md5 filename */
2639 line
[256], /* Line from file */
2640 tempuser
[33], /* User from file */
2641 tempgroup
[33]; /* Group from file */
2644 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2645 "get_md5_password(username=\"%s\", group=\"%s\", passwd=%p)",
2646 username
, group
? group
: "(null)", passwd
);
2648 snprintf(filename
, sizeof(filename
), "%s/passwd.md5", ServerRoot
);
2649 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
2651 if (errno
!= ENOENT
)
2652 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open %s - %s", filename
,
2658 while (cupsFileGets(fp
, line
, sizeof(line
)) != NULL
)
2660 if (sscanf(line
, "%32[^:]:%32[^:]:%32s", tempuser
, tempgroup
, passwd
) != 3)
2662 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad MD5 password line: %s", line
);
2666 if (!strcmp(username
, tempuser
) &&
2667 (group
== NULL
|| !strcmp(group
, tempgroup
)))
2670 * Found the password entry!
2673 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Found MD5 user %s, group %s...",
2674 username
, tempgroup
);
2682 * Didn't find a password entry - return NULL!
2692 * 'pam_func()' - PAM conversation function.
2695 static int /* O - Success or failure */
2697 int num_msg
, /* I - Number of messages */
2698 const struct pam_message
**msg
, /* I - Messages */
2699 struct pam_response
**resp
, /* O - Responses */
2701 /* I - Pointer to connection */
2703 int i
; /* Looping var */
2704 struct pam_response
*replies
; /* Replies */
2705 cupsd_authdata_t
*data
; /* Pointer to auth data */
2709 * Allocate memory for the responses...
2712 if ((replies
= malloc(sizeof(struct pam_response
) * num_msg
)) == NULL
)
2713 return (PAM_CONV_ERR
);
2716 * Answer all of the messages...
2719 DEBUG_printf(("pam_func: appdata_ptr = %p\n", appdata_ptr
));
2723 * Apparently some versions of HP-UX 11 have a broken pam_unix security
2724 * module. This is a workaround...
2730 data
= (cupsd_authdata_t
*)appdata_ptr
;
2733 for (i
= 0; i
< num_msg
; i
++)
2735 DEBUG_printf(("pam_func: Message = \"%s\"\n", msg
[i
]->msg
));
2737 switch (msg
[i
]->msg_style
)
2739 case PAM_PROMPT_ECHO_ON
:
2740 DEBUG_printf(("pam_func: PAM_PROMPT_ECHO_ON, returning \"%s\"...\n",
2742 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2743 replies
[i
].resp
= strdup(data
->username
);
2746 case PAM_PROMPT_ECHO_OFF
:
2747 DEBUG_printf(("pam_func: PAM_PROMPT_ECHO_OFF, returning \"%s\"...\n",
2749 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2750 replies
[i
].resp
= strdup(data
->password
);
2754 DEBUG_puts("pam_func: PAM_TEXT_INFO...");
2755 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2756 replies
[i
].resp
= NULL
;
2760 DEBUG_puts("pam_func: PAM_ERROR_MSG...");
2761 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2762 replies
[i
].resp
= NULL
;
2766 DEBUG_printf(("pam_func: Unknown PAM message %d...\n",
2767 msg
[i
]->msg_style
));
2769 return (PAM_CONV_ERR
);
2774 * Return the responses back to PAM...
2779 return (PAM_SUCCESS
);
2781 #elif !defined(HAVE_USERSEC_H)
2785 * 'to64()' - Base64-encode an integer value...
2789 to64(char *s
, /* O - Output string */
2790 unsigned long v
, /* I - Value to encode */
2791 int n
) /* I - Number of digits */
2793 const char *itoa64
= "./0123456789"
2794 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2795 "abcdefghijklmnopqrstuvwxyz";
2798 for (; n
> 0; n
--, v
>>= 6)
2799 *s
++ = itoa64
[v
& 0x3f];
2801 #endif /* HAVE_LIBPAM */
2805 * End of "$Id: auth.c 7830 2008-08-04 20:38:50Z mike $".