2 * "$Id: auth.c 7673 2008-06-18 22:31:26Z mike $"
4 * Authorization routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 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 to
25 * access the location.
26 * cupsdAuthorize() - Validate any authorization credentials.
27 * cupsdCheckAccess() - Check whether the given address is allowed to
29 * cupsdCheckAuth() - Check authorization masks.
30 * cupsdCheckGroup() - Check for a user's group membership.
31 * cupsdCopyLocation() - Make a copy of a location...
32 * cupsdDeleteAllLocations() - Free all memory used for location
34 * cupsdDeleteLocation() - Free all memory used by a location.
35 * cupsdDenyHost() - Add a host name that is not allowed to access
37 * cupsdDenyIP() - Add an IP address or network that is not
38 * allowed to access the location.
39 * cupsdFindBest() - Find the location entry that best matches the
41 * cupsdFindLocation() - Find the named location.
42 * cupsdIsAuthorized() - Check to see if the user is authorized...
43 * add_allow() - Add an allow mask to the location.
44 * add_deny() - Add a deny mask to the location.
45 * check_authref() - Check if an authorization services reference
46 * has the supplied right.
47 * compare_locations() - Compare two locations.
48 * cups_crypt() - Encrypt the password using the DES or MD5
49 * algorithms, as needed.
50 * get_gss_creds() - Obtain GSS credentials.
51 * get_md5_password() - Get an MD5 password.
52 * pam_func() - PAM conversation function.
53 * to64() - Base64-encode an integer value...
57 * Include necessary headers...
64 #endif /* HAVE_SHADOW_H */
67 #endif /* HAVE_CRYPT_H */
69 # ifdef HAVE_PAM_PAM_APPL_H
70 # include <pam/pam_appl.h>
72 # include <security/pam_appl.h>
73 # endif /* HAVE_PAM_PAM_APPL_H */
74 #endif /* HAVE_LIBPAM */
77 #endif /* HAVE_USERSEC_H */
78 #ifdef HAVE_MEMBERSHIP_H
79 # include <membership.h>
80 #endif /* HAVE_MEMBERSHIP_H */
81 #ifdef HAVE_AUTHORIZATION_H
82 # include <Security/AuthorizationTags.h>
83 # ifdef HAVE_SECBASEPRIV_H
84 # include <Security/SecBasePriv.h>
86 extern const char *cssmErrorString(int error
);
87 # endif /* HAVE_SECBASEPRIV_H */
88 #endif /* HAVE_AUTHORIZATION_H */
89 #ifdef HAVE_SYS_PARAM_H
90 # include <sys/param.h>
91 #endif /* HAVE_SYS_PARAM_H */
92 #ifdef HAVE_SYS_UCRED_H
93 # include <sys/ucred.h>
94 typedef struct xucred cupsd_ucred_t
;
95 # define CUPSD_UCRED_UID(c) (c).cr_uid
97 typedef struct ucred cupsd_ucred_t
;
98 # define CUPSD_UCRED_UID(c) (c).uid
99 #endif /* HAVE_SYS_UCRED_H */
106 static cupsd_authmask_t
*add_allow(cupsd_location_t
*loc
);
107 static cupsd_authmask_t
*add_deny(cupsd_location_t
*loc
);
108 #ifdef HAVE_AUTHORIZATION_H
109 static int check_authref(cupsd_client_t
*con
, const char *right
);
110 #endif /* HAVE_AUTHORIZATION_H */
111 static int compare_locations(cupsd_location_t
*a
,
112 cupsd_location_t
*b
);
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 */
117 static gss_cred_id_t
get_gss_creds(const char *service_name
,
118 const char *con_server_name
);
119 #endif /* HAVE_GSSAPI */
120 static char *get_md5_password(const char *username
,
121 const char *group
, char passwd
[33]);
123 static int pam_func(int, const struct pam_message
**,
124 struct pam_response
**, void *);
125 #elif !defined(HAVE_USERSEC_H)
126 static void to64(char *s
, unsigned long v
, int n
);
127 #endif /* HAVE_LIBPAM */
131 * Local structures...
135 typedef struct cupsd_authdata_s
/**** Authentication data ****/
137 char username
[33], /* Username string */
138 password
[33]; /* Password string */
140 #endif /* HAVE_LIBPAM */
147 #if defined(__hpux) && HAVE_LIBPAM
148 static cupsd_authdata_t
*auth_data
; /* Current client being authenticated */
149 #endif /* __hpux && HAVE_LIBPAM */
153 * 'cupsdAddLocation()' - Add a location for authorization.
156 cupsd_location_t
* /* O - Pointer to new location record */
157 cupsdAddLocation(const char *location
) /* I - Location path */
159 cupsd_location_t
*temp
; /* New location */
163 * Make sure the locations array is created...
167 Locations
= cupsArrayNew((cups_array_func_t
)compare_locations
, NULL
);
173 * Try to allocate memory for the new location.
176 if ((temp
= calloc(1, sizeof(cupsd_location_t
))) == NULL
)
180 * Initialize the record and copy the name over...
183 if ((temp
->location
= strdup(location
)) == NULL
)
189 temp
->length
= strlen(temp
->location
);
191 cupsArrayAdd(Locations
, temp
);
193 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAddLocation: added location \'%s\'",
197 * Return the new record...
205 * 'cupsdAddName()' - Add a name to a location...
209 cupsdAddName(cupsd_location_t
*loc
, /* I - Location to add to */
210 char *name
) /* I - Name to add */
212 char **temp
; /* Pointer to names array */
215 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAddName(loc=%p, name=\"%s\")",
218 if (loc
->num_names
== 0)
219 temp
= malloc(sizeof(char *));
221 temp
= realloc(loc
->names
, (loc
->num_names
+ 1) * sizeof(char *));
225 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to add name to location %s: %s",
226 loc
->location
? loc
->location
: "nil", strerror(errno
));
232 if ((temp
[loc
->num_names
] = strdup(name
)) == NULL
)
234 cupsdLogMessage(CUPSD_LOG_ERROR
,
235 "Unable to duplicate name for location %s: %s",
236 loc
->location
? loc
->location
: "nil", strerror(errno
));
245 * 'cupsdAllowHost()' - Add a host name that is allowed to access the location.
249 cupsdAllowHost(cupsd_location_t
*loc
, /* I - Location to add to */
250 char *name
) /* I - Name of host or domain to add */
252 cupsd_authmask_t
*temp
; /* New host/domain mask */
253 char ifname
[32], /* Interface name */
254 *ifptr
; /* Pointer to end of name */
257 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAllowHost(loc=%p(%s), name=\"%s\")",
258 loc
, loc
->location
? loc
->location
: "nil", name
);
260 if ((temp
= add_allow(loc
)) == NULL
)
263 if (!strcasecmp(name
, "@LOCAL"))
266 * Allow *interface*...
269 temp
->type
= CUPSD_AUTH_INTERFACE
;
270 temp
->mask
.name
.name
= strdup("*");
271 temp
->mask
.name
.length
= 1;
273 else if (!strncasecmp(name
, "@IF(", 4))
276 * Allow *interface*...
279 strlcpy(ifname
, name
+ 4, sizeof(ifname
));
281 ifptr
= ifname
+ strlen(ifname
);
283 if (ifptr
[-1] == ')')
289 temp
->type
= CUPSD_AUTH_INTERFACE
;
290 temp
->mask
.name
.name
= strdup(ifname
);
291 temp
->mask
.name
.length
= ifptr
- ifname
;
299 temp
->type
= CUPSD_AUTH_NAME
;
300 temp
->mask
.name
.name
= strdup(name
);
301 temp
->mask
.name
.length
= strlen(name
);
307 * 'cupsdAllowIP()' - Add an IP address or network that is allowed to access
313 cupsd_location_t
*loc
, /* I - Location to add to */
314 const unsigned address
[4], /* I - IP address to add */
315 const unsigned netmask
[4]) /* I - Netmask of address */
317 cupsd_authmask_t
*temp
; /* New host/domain mask */
320 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
321 "cupsdAllowIP(loc=%p(%s), address=%x:%x:%x:%x, netmask=%x:%x:%x:%x)",
322 loc
, loc
->location
? loc
->location
: "nil",
323 address
[0], address
[1], address
[2], address
[3],
324 netmask
[0], netmask
[1], netmask
[2], netmask
[3]);
326 if ((temp
= add_allow(loc
)) == NULL
)
329 temp
->type
= CUPSD_AUTH_IP
;
330 memcpy(temp
->mask
.ip
.address
, address
, sizeof(temp
->mask
.ip
.address
));
331 memcpy(temp
->mask
.ip
.netmask
, netmask
, sizeof(temp
->mask
.ip
.netmask
));
336 * 'cupsdAuthorize()' - Validate any authorization credentials.
340 cupsdAuthorize(cupsd_client_t
*con
) /* I - Client connection */
342 int type
; /* Authentication type */
343 const char *authorization
; /* Pointer into Authorization string */
344 char *ptr
, /* Pointer into string */
345 username
[256], /* Username string */
346 password
[33]; /* Password string */
347 cupsd_cert_t
*localuser
; /* Certificate username */
348 char nonce
[HTTP_MAX_VALUE
], /* Nonce value from client */
349 md5
[33], /* MD5 password */
350 basicmd5
[33]; /* MD5 of Basic password */
351 static const char * const states
[] = /* HTTP client states... */
371 * Locate the best matching location so we know what kind of
372 * authentication to expect...
375 con
->best
= cupsdFindBest(con
->uri
, con
->http
.state
);
376 con
->type
= CUPSD_AUTH_NONE
;
378 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
379 "cupsdAuthorize: con->uri=\"%s\", con->best=%p(%s)",
380 con
->uri
, con
->best
, con
->best
? con
->best
->location
: "");
382 if (con
->best
&& con
->best
->type
!= CUPSD_AUTH_NONE
)
384 if (con
->best
->type
== CUPSD_AUTH_DEFAULT
)
385 type
= DefaultAuthType
;
387 type
= con
->best
->type
;
390 type
= DefaultAuthType
;
393 * Decode the Authorization string...
396 authorization
= httpGetField(&con
->http
, HTTP_FIELD_AUTHORIZATION
);
398 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAuthorize: Authorization=\"%s\"",
404 #ifdef HAVE_AUTHORIZATION_H
407 AuthorizationFree(con
->authref
, kAuthorizationFlagDefaults
);
410 #endif /* HAVE_AUTHORIZATION_H */
415 * No authorization data provided, return early...
418 cupsdLogMessage(CUPSD_LOG_DEBUG
,
419 "cupsdAuthorize: No authentication data provided.");
422 #ifdef HAVE_AUTHORIZATION_H
423 else if (!strncmp(authorization
, "AuthRef", 6) &&
424 !strcasecmp(con
->http
.hostname
, "localhost"))
426 OSStatus status
; /* Status */
427 int authlen
; /* Auth string length */
428 AuthorizationItemSet
*authinfo
; /* Authorization item set */
431 * Get the Authorization Services data...
435 while (isspace(*authorization
& 255))
438 authlen
= sizeof(nonce
);
439 httpDecode64_2(nonce
, &authlen
, authorization
);
441 if (authlen
!= kAuthorizationExternalFormLength
)
443 cupsdLogMessage(CUPSD_LOG_ERROR
,
444 "External Authorization reference size is incorrect!");
448 if ((status
= AuthorizationCreateFromExternalForm(
449 (AuthorizationExternalForm
*)nonce
, &con
->authref
)) != 0)
451 cupsdLogMessage(CUPSD_LOG_ERROR
,
452 "AuthorizationCreateFromExternalForm returned %d (%s)",
453 (int)status
, cssmErrorString(status
));
457 if ((status
= AuthorizationCopyInfo(con
->authref
,
458 kAuthorizationEnvironmentUsername
,
461 cupsdLogMessage(CUPSD_LOG_ERROR
,
462 "AuthorizationCopyInfo returned %d (%s)",
463 (int)status
, cssmErrorString(status
));
467 if (authinfo
->count
== 1)
468 strlcpy(username
, authinfo
->items
[0].value
, sizeof(username
));
470 cupsdLogMessage(CUPSD_LOG_DEBUG
,
471 "cupsdAuthorize: Authorized as %s using AuthRef",
474 AuthorizationFreeItemSet(authinfo
);
476 con
->type
= CUPSD_AUTH_BASIC
;
478 #endif /* HAVE_AUTHORIZATION_H */
479 #if defined(SO_PEERCRED) && defined(AF_LOCAL)
480 else if (!strncmp(authorization
, "PeerCred ", 9) &&
481 con
->http
.hostaddr
->addr
.sa_family
== AF_LOCAL
)
484 * Use peer credentials from domain socket connection...
487 struct passwd
*pwd
; /* Password entry for this user */
488 cupsd_ucred_t peercred
; /* Peer credentials */
489 socklen_t peersize
; /* Size of peer credentials */
492 if ((pwd
= getpwnam(authorization
+ 9)) == NULL
)
494 cupsdLogMessage(CUPSD_LOG_ERROR
, "User \"%s\" does not exist!",
499 peersize
= sizeof(peercred
);
501 if (getsockopt(con
->http
.fd
, SOL_SOCKET
, SO_PEERCRED
, &peercred
, &peersize
))
503 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to get peer credentials - %s",
508 if (pwd
->pw_uid
!= CUPSD_UCRED_UID(peercred
))
510 cupsdLogMessage(CUPSD_LOG_ERROR
,
511 "Invalid peer credentials for \"%s\" - got %d, "
512 "expected %d!", authorization
+ 9,
513 CUPSD_UCRED_UID(peercred
), pwd
->pw_uid
);
514 # ifdef HAVE_SYS_UCRED_H
515 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAuthorize: cr_version=%d",
516 peercred
.cr_version
);
517 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAuthorize: cr_uid=%d",
519 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAuthorize: cr_ngroups=%d",
520 peercred
.cr_ngroups
);
521 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAuthorize: cr_groups[0]=%d",
522 peercred
.cr_groups
[0]);
523 # endif /* HAVE_SYS_UCRED_H */
527 strlcpy(username
, authorization
+ 9, sizeof(username
));
529 cupsdLogMessage(CUPSD_LOG_DEBUG
,
530 "cupsdAuthorize: Authorized as %s using PeerCred",
533 con
->type
= CUPSD_AUTH_BASIC
;
535 #endif /* SO_PEERCRED && AF_LOCAL */
536 else if (!strncmp(authorization
, "Local", 5) &&
537 !strcasecmp(con
->http
.hostname
, "localhost"))
540 * Get Local certificate authentication data...
544 while (isspace(*authorization
& 255))
547 if ((localuser
= cupsdFindCert(authorization
)) != NULL
)
549 strlcpy(username
, localuser
->username
, sizeof(username
));
551 cupsdLogMessage(CUPSD_LOG_DEBUG
,
552 "cupsdAuthorize: Authorized as %s using Local",
557 cupsdLogMessage(CUPSD_LOG_ERROR
,
558 "cupsdAuthorize: Local authentication certificate not "
564 if (localuser
->ccache
)
565 con
->type
= CUPSD_AUTH_NEGOTIATE
;
567 #endif /* HAVE_GSSAPI */
568 con
->type
= CUPSD_AUTH_BASIC
;
570 else if (!strncmp(authorization
, "Basic", 5))
573 * Get the Basic authentication data...
576 int userlen
; /* Username:password length */
580 while (isspace(*authorization
& 255))
583 userlen
= sizeof(username
);
584 httpDecode64_2(username
, &userlen
, authorization
);
587 * Pull the username and password out...
590 if ((ptr
= strchr(username
, ':')) == NULL
)
592 cupsdLogMessage(CUPSD_LOG_ERROR
,
593 "cupsdAuthorize: Missing Basic password!");
602 * Username must not be empty...
605 cupsdLogMessage(CUPSD_LOG_ERROR
,
606 "cupsdAuthorize: Empty Basic username!");
613 * Password must not be empty...
616 cupsdLogMessage(CUPSD_LOG_ERROR
,
617 "cupsdAuthorize: Empty Basic password!");
621 strlcpy(password
, ptr
, sizeof(password
));
624 * Validate the username and password...
630 case CUPSD_AUTH_BASIC
:
634 * Only use PAM to do authentication. This supports MD5
635 * passwords, among other things...
638 pam_handle_t
*pamh
; /* PAM authentication handle */
639 int pamerr
; /* PAM error code */
640 struct pam_conv pamdata
;/* PAM conversation data */
641 cupsd_authdata_t data
; /* Authentication data */
644 strlcpy(data
.username
, username
, sizeof(data
.username
));
645 strlcpy(data
.password
, password
, sizeof(data
.password
));
647 # if defined(__sun) || defined(__hpux)
648 pamdata
.conv
= (int (*)(int, struct pam_message
**,
649 struct pam_response
**,
652 pamdata
.conv
= pam_func
;
653 # endif /* __sun || __hpux */
654 pamdata
.appdata_ptr
= &data
;
658 * Workaround for HP-UX bug in pam_unix; see pam_func() below for
665 pamerr
= pam_start("cups", username
, &pamdata
, &pamh
);
666 if (pamerr
!= PAM_SUCCESS
)
668 cupsdLogMessage(CUPSD_LOG_ERROR
,
669 "cupsdAuthorize: pam_start() returned %d (%s)!",
670 pamerr
, pam_strerror(pamh
, pamerr
));
674 # if defined(HAVE_PAM_SET_ITEM) && defined(PAM_RHOST)
675 pamerr
= pam_set_item(pamh
, PAM_RHOST
, con
->http
.hostname
);
676 if (pamerr
!= PAM_SUCCESS
)
677 cupsdLogMessage(CUPSD_LOG_WARN
,
678 "cupsdAuthorize: pam_set_item() returned %d "
679 "(%s)!", pamerr
, pam_strerror(pamh
, pamerr
));
680 # endif /* HAVE_PAM_SET_ITEM && PAM_RHOST */
682 pamerr
= pam_authenticate(pamh
, PAM_SILENT
);
683 if (pamerr
!= PAM_SUCCESS
)
685 cupsdLogMessage(CUPSD_LOG_ERROR
,
686 "cupsdAuthorize: pam_authenticate() returned %d "
688 pamerr
, pam_strerror(pamh
, pamerr
));
693 pamerr
= pam_acct_mgmt(pamh
, PAM_SILENT
);
694 if (pamerr
!= PAM_SUCCESS
)
696 cupsdLogMessage(CUPSD_LOG_ERROR
,
697 "cupsdAuthorize: pam_acct_mgmt() returned %d "
699 pamerr
, pam_strerror(pamh
, pamerr
));
704 pam_end(pamh
, PAM_SUCCESS
);
706 #elif defined(HAVE_USERSEC_H)
708 * Use AIX authentication interface...
711 char *authmsg
; /* Authentication message */
712 int reenter
; /* ??? */
715 cupsdLogMessage(CUPSD_LOG_DEBUG
,
716 "cupsdAuthorize: AIX authenticate of username "
720 if (authenticate(username
, password
, &reenter
, &authmsg
) != 0)
722 cupsdLogMessage(CUPSD_LOG_DEBUG
,
723 "cupsdAuthorize: Unable to authenticate username "
725 username
, strerror(errno
));
731 * Use normal UNIX password file-based authentication...
734 char *pass
; /* Encrypted password */
735 struct passwd
*pw
; /* User password data */
736 # ifdef HAVE_SHADOW_H
737 struct spwd
*spw
; /* Shadow password data */
738 # endif /* HAVE_SHADOW_H */
741 pw
= getpwnam(username
); /* Get the current password */
742 endpwent(); /* Close the password file */
750 cupsdLogMessage(CUPSD_LOG_ERROR
,
751 "cupsdAuthorize: Unknown username \"%s\"!",
756 # ifdef HAVE_SHADOW_H
757 spw
= getspnam(username
);
760 if (!spw
&& !strcmp(pw
->pw_passwd
, "x"))
763 * Don't allow blank passwords!
766 cupsdLogMessage(CUPSD_LOG_ERROR
,
767 "cupsdAuthorize: Username \"%s\" has no shadow "
768 "password!", username
);
772 if (spw
&& !spw
->sp_pwdp
[0] && !pw
->pw_passwd
[0])
774 if (!pw
->pw_passwd
[0])
775 # endif /* HAVE_SHADOW_H */
778 * Don't allow blank passwords!
781 cupsdLogMessage(CUPSD_LOG_ERROR
,
782 "cupsdAuthorize: Username \"%s\" has no password!",
788 * OK, the password isn't blank, so compare with what came from the
792 pass
= cups_crypt(password
, pw
->pw_passwd
);
794 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
795 "cupsdAuthorize: pw_passwd=\"%s\", crypt=\"%s\"",
796 pw
->pw_passwd
, pass
);
798 if (!pass
|| strcmp(pw
->pw_passwd
, pass
))
800 # ifdef HAVE_SHADOW_H
803 pass
= cups_crypt(password
, spw
->sp_pwdp
);
805 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
806 "cupsdAuthorize: sp_pwdp=\"%s\", crypt=\"%s\"",
809 if (pass
== NULL
|| strcmp(spw
->sp_pwdp
, pass
))
811 cupsdLogMessage(CUPSD_LOG_ERROR
,
812 "cupsdAuthorize: Authentication failed for "
819 # endif /* HAVE_SHADOW_H */
821 cupsdLogMessage(CUPSD_LOG_ERROR
,
822 "cupsdAuthorize: Authentication failed for "
828 #endif /* HAVE_LIBPAM */
831 cupsdLogMessage(CUPSD_LOG_DEBUG
,
832 "cupsdAuthorize: Authorized as %s using Basic",
836 case CUPSD_AUTH_BASICDIGEST
:
838 * Do Basic authentication with the Digest password file...
841 if (!get_md5_password(username
, NULL
, md5
))
843 cupsdLogMessage(CUPSD_LOG_ERROR
,
844 "cupsdAuthorize: Unknown MD5 username \"%s\"!",
849 httpMD5(username
, "CUPS", password
, basicmd5
);
851 if (strcmp(md5
, basicmd5
))
853 cupsdLogMessage(CUPSD_LOG_ERROR
,
854 "cupsdAuthorize: Authentication failed for \"%s\"!",
859 cupsdLogMessage(CUPSD_LOG_DEBUG
,
860 "cupsdAuthorize: Authorized as %s using BasicDigest",
867 else if (!strncmp(authorization
, "Digest", 6))
870 * Get the username, password, and nonce from the Digest attributes...
873 if (!httpGetSubField2(&(con
->http
), HTTP_FIELD_AUTHORIZATION
, "username",
874 username
, sizeof(username
)) || !username
[0])
877 * Username must not be empty...
880 cupsdLogMessage(CUPSD_LOG_ERROR
,
881 "cupsdAuthorize: Empty or missing Digest username!");
885 if (!httpGetSubField2(&(con
->http
), HTTP_FIELD_AUTHORIZATION
, "response",
886 password
, sizeof(password
)) || !password
[0])
889 * Password must not be empty...
892 cupsdLogMessage(CUPSD_LOG_ERROR
,
893 "cupsdAuthorize: Empty or missing Digest password!");
897 if (!httpGetSubField(&(con
->http
), HTTP_FIELD_AUTHORIZATION
, "nonce",
900 cupsdLogMessage(CUPSD_LOG_ERROR
,
901 "cupsdAuthorize: No nonce value for Digest "
906 if (strcmp(con
->http
.hostname
, nonce
))
908 cupsdLogMessage(CUPSD_LOG_ERROR
,
909 "cupsdAuthorize: Bad nonce value, expected \"%s\", "
910 "got \"%s\"!", con
->http
.hostname
, nonce
);
915 * Validate the username and password...
918 if (!get_md5_password(username
, NULL
, md5
))
920 cupsdLogMessage(CUPSD_LOG_ERROR
,
921 "cupsdAuthorize: Unknown MD5 username \"%s\"!",
926 httpMD5Final(nonce
, states
[con
->http
.state
], con
->uri
, md5
);
928 if (strcmp(md5
, password
))
930 cupsdLogMessage(CUPSD_LOG_ERROR
,
931 "cupsdAuthorize: Authentication failed for \"%s\"!",
936 cupsdLogMessage(CUPSD_LOG_DEBUG
,
937 "cupsdAuthorize: Authorized as %s using Digest",
940 con
->type
= CUPSD_AUTH_DIGEST
;
943 else if (!strncmp(authorization
, "Negotiate", 9))
945 int len
; /* Length of authorization string */
946 gss_cred_id_t server_creds
; /* Server credentials */
947 gss_ctx_id_t context
; /* Authorization context */
948 OM_uint32 major_status
, /* Major status code */
949 minor_status
; /* Minor status code */
950 gss_buffer_desc input_token
= GSS_C_EMPTY_BUFFER
,
951 /* Input token from string */
952 output_token
= GSS_C_EMPTY_BUFFER
;
953 /* Output token for username */
954 gss_name_t client_name
; /* Client name */
955 unsigned int ret_flags
; /* Credential flags */
960 * If the weak-linked GSSAPI/Kerberos library is not present, don't try
964 if (gss_init_sec_context
== NULL
)
966 cupsdLogMessage(CUPSD_LOG_WARN
,
967 "GSSAPI/Kerberos authentication failed because the "
968 "Kerberos framework is not present.");
971 # endif /* __APPLE__ */
973 con
->gss_output_token
.length
= 0;
976 * Find the start of the Kerberos input token...
980 while (isspace(*authorization
& 255))
985 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
986 "cupsdAuthorize: No authentication data specified.");
991 * Get the server credentials...
994 if ((server_creds
= get_gss_creds(GSSServiceName
, con
->servername
)) == NULL
)
998 * Decode the authorization string to get the input token...
1001 len
= strlen(authorization
);
1002 input_token
.value
= malloc(len
);
1003 input_token
.value
= httpDecode64_2(input_token
.value
, &len
,
1005 input_token
.length
= len
;
1008 * Accept the input token to get the authorization info...
1011 context
= GSS_C_NO_CONTEXT
;
1012 client_name
= GSS_C_NO_NAME
;
1013 major_status
= gss_accept_sec_context(&minor_status
,
1017 GSS_C_NO_CHANNEL_BINDINGS
,
1020 &con
->gss_output_token
,
1023 &con
->gss_delegated_cred
);
1025 if (GSS_ERROR(major_status
))
1027 cupsdLogGSSMessage(CUPSD_LOG_DEBUG
, major_status
, minor_status
,
1028 "cupsdAuthorize: Error accepting GSSAPI security "
1031 if (context
!= GSS_C_NO_CONTEXT
)
1032 gss_delete_sec_context(&minor_status
, &context
, GSS_C_NO_BUFFER
);
1034 gss_release_cred(&minor_status
, &server_creds
);
1039 * Release our credentials...
1042 gss_release_cred(&minor_status
, &server_creds
);
1045 * Get the username associated with the client's credentials...
1048 if (!con
->gss_delegated_cred
)
1049 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1050 "cupsdAuthorize: No delegated credentials!");
1052 if (major_status
== GSS_S_CONTINUE_NEEDED
)
1053 cupsdLogGSSMessage(CUPSD_LOG_DEBUG
, major_status
, minor_status
,
1054 "cupsdAuthorize: Credentials not complete");
1055 else if (major_status
== GSS_S_COMPLETE
)
1057 major_status
= gss_display_name(&minor_status
, client_name
,
1058 &output_token
, NULL
);
1060 if (GSS_ERROR(major_status
))
1062 cupsdLogGSSMessage(CUPSD_LOG_DEBUG
, major_status
, minor_status
,
1063 "cupsdAuthorize: Error getting username");
1064 gss_release_name(&minor_status
, &client_name
);
1065 gss_delete_sec_context(&minor_status
, &context
, GSS_C_NO_BUFFER
);
1069 gss_release_name(&minor_status
, &client_name
);
1070 strlcpy(username
, output_token
.value
, sizeof(username
));
1072 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1073 "cupsdAuthorize: Authorized as %s using Negotiate",
1076 gss_release_buffer(&minor_status
, &output_token
);
1077 gss_delete_sec_context(&minor_status
, &context
, GSS_C_NO_BUFFER
);
1079 con
->gss_have_creds
= 1;
1081 con
->type
= CUPSD_AUTH_NEGOTIATE
;
1084 gss_release_name(&minor_status
, &client_name
);
1086 #endif /* HAVE_GSSAPI */
1089 char scheme
[256]; /* Auth scheme... */
1092 if (sscanf(authorization
, "%255s", scheme
) != 1)
1093 strcpy(scheme
, "UNKNOWN");
1095 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad authentication data \"%s ...\"",
1101 * If we get here, then we were able to validate the username and
1102 * password - copy the validated username and password to the client
1103 * data and return...
1106 strlcpy(con
->username
, username
, sizeof(con
->username
));
1107 strlcpy(con
->password
, password
, sizeof(con
->password
));
1112 * 'cupsdCheckAccess()' - Check whether the given address is allowed to
1113 * access a location.
1116 int /* O - 1 if allowed, 0 otherwise */
1118 unsigned ip
[4], /* I - Client address */
1119 char *name
, /* I - Client hostname */
1120 int namelen
, /* I - Length of hostname */
1121 cupsd_location_t
*loc
) /* I - Location to check */
1123 int allow
; /* 1 if allowed, 0 otherwise */
1126 if (!strcasecmp(name
, "localhost"))
1129 * Access from localhost (127.0.0.1 or ::1) is always allowed...
1137 * Do authorization checks on the domain/address...
1140 switch (loc
->order_type
)
1143 allow
= 0; /* anti-compiler-warning-code */
1146 case CUPSD_AUTH_ALLOW
: /* Order Deny,Allow */
1149 if (cupsdCheckAuth(ip
, name
, namelen
, loc
->num_deny
, loc
->deny
))
1152 if (cupsdCheckAuth(ip
, name
, namelen
, loc
->num_allow
, loc
->allow
))
1156 case CUPSD_AUTH_DENY
: /* Order Allow,Deny */
1159 if (cupsdCheckAuth(ip
, name
, namelen
, loc
->num_allow
, loc
->allow
))
1162 if (cupsdCheckAuth(ip
, name
, namelen
, loc
->num_deny
, loc
->deny
))
1173 * 'cupsdCheckAuth()' - Check authorization masks.
1176 int /* O - 1 if mask matches, 0 otherwise */
1178 unsigned ip
[4], /* I - Client address */
1179 char *name
, /* I - Client hostname */
1180 int name_len
, /* I - Length of hostname */
1181 int num_masks
, /* I - Number of masks */
1182 cupsd_authmask_t
*masks
) /* I - Masks */
1184 int i
; /* Looping var */
1185 cupsd_netif_t
*iface
; /* Network interface */
1186 unsigned netip4
; /* IPv4 network address */
1188 unsigned netip6
[4]; /* IPv6 network address */
1189 #endif /* AF_INET6 */
1191 while (num_masks
> 0)
1193 switch (masks
->type
)
1195 case CUPSD_AUTH_INTERFACE
:
1197 * Check for a match with a network interface...
1200 netip4
= htonl(ip
[3]);
1203 netip6
[0] = htonl(ip
[0]);
1204 netip6
[1] = htonl(ip
[1]);
1205 netip6
[2] = htonl(ip
[2]);
1206 netip6
[3] = htonl(ip
[3]);
1207 #endif /* AF_INET6 */
1209 if (!strcmp(masks
->mask
.name
.name
, "*"))
1212 * Check against all local interfaces...
1217 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
1219 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
1222 * Only check local interfaces...
1225 if (!iface
->is_local
)
1228 if (iface
->address
.addr
.sa_family
== AF_INET
)
1231 * Check IPv4 address...
1234 if ((netip4
& iface
->mask
.ipv4
.sin_addr
.s_addr
) ==
1235 (iface
->address
.ipv4
.sin_addr
.s_addr
&
1236 iface
->mask
.ipv4
.sin_addr
.s_addr
))
1243 * Check IPv6 address...
1246 for (i
= 0; i
< 4; i
++)
1247 if ((netip6
[i
] & iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]) !=
1248 (iface
->address
.ipv6
.sin6_addr
.s6_addr32
[i
] &
1249 iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]))
1255 #endif /* AF_INET6 */
1261 * Check the named interface...
1264 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
1266 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
1268 if (strcmp(masks
->mask
.name
.name
, iface
->name
))
1271 if (iface
->address
.addr
.sa_family
== AF_INET
)
1274 * Check IPv4 address...
1277 if ((netip4
& iface
->mask
.ipv4
.sin_addr
.s_addr
) ==
1278 (iface
->address
.ipv4
.sin_addr
.s_addr
&
1279 iface
->mask
.ipv4
.sin_addr
.s_addr
))
1286 * Check IPv6 address...
1289 for (i
= 0; i
< 4; i
++)
1290 if ((netip6
[i
] & iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]) !=
1291 (iface
->address
.ipv6
.sin6_addr
.s6_addr32
[i
] &
1292 iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]))
1298 #endif /* AF_INET6 */
1303 case CUPSD_AUTH_NAME
:
1305 * Check for exact name match...
1308 if (!strcasecmp(name
, masks
->mask
.name
.name
))
1312 * Check for domain match...
1315 if (name_len
>= masks
->mask
.name
.length
&&
1316 masks
->mask
.name
.name
[0] == '.' &&
1317 !strcasecmp(name
+ name_len
- masks
->mask
.name
.length
,
1318 masks
->mask
.name
.name
))
1322 case CUPSD_AUTH_IP
:
1324 * Check for IP/network address match...
1327 for (i
= 0; i
< 4; i
++)
1328 if ((ip
[i
] & masks
->mask
.ip
.netmask
[i
]) !=
1329 masks
->mask
.ip
.address
[i
])
1346 * 'cupsdCheckGroup()' - Check for a user's group membership.
1349 int /* O - 1 if user is a member, 0 otherwise */
1351 const char *username
, /* I - User name */
1352 struct passwd
*user
, /* I - System user info */
1353 const char *groupname
) /* I - Group name */
1355 int i
; /* Looping var */
1356 struct group
*group
; /* System group info */
1357 char junk
[33]; /* MD5 password (not used) */
1358 #ifdef HAVE_MBR_UID_TO_UUID
1359 uuid_t useruuid
, /* UUID for username */
1360 groupuuid
; /* UUID for groupname */
1361 int is_member
; /* True if user is a member of group */
1362 #endif /* HAVE_MBR_UID_TO_UUID */
1365 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1366 "cupsdCheckGroup(username=\"%s\", user=%p, groupname=\"%s\")",
1367 username
, user
, groupname
);
1373 if (!username
|| !groupname
)
1377 * Check to see if the user is a member of the named group...
1380 group
= getgrnam(groupname
);
1386 * Group exists, check it...
1389 for (i
= 0; group
->gr_mem
[i
]; i
++)
1390 if (!strcasecmp(username
, group
->gr_mem
[i
]))
1395 * Group doesn't exist or user not in group list, check the group ID
1396 * against the user's group ID...
1399 if (user
&& group
&& group
->gr_gid
== user
->pw_gid
)
1402 #ifdef HAVE_MBR_UID_TO_UUID
1404 * Check group membership through MacOS X membership API...
1407 if (user
&& !mbr_uid_to_uuid(user
->pw_uid
, useruuid
))
1412 * Map group name to UUID and check membership...
1415 if (!mbr_gid_to_uuid(group
->gr_gid
, groupuuid
))
1416 if (!mbr_check_membership(useruuid
, groupuuid
, &is_member
))
1420 else if (groupname
[0] == '#')
1423 * Use UUID directly and check for equality (user UUID) and
1424 * membership (group UUID)...
1427 if (!uuid_parse((char *)groupname
+ 1, groupuuid
))
1429 if (!uuid_compare(useruuid
, groupuuid
))
1431 else if (!mbr_check_membership(useruuid
, groupuuid
, &is_member
))
1439 else if (groupname
[0] == '#')
1441 #endif /* HAVE_MBR_UID_TO_UUID */
1444 * Username not found, group not found, or user is not part of the
1445 * system group... Check for a user and group in the MD5 password
1449 if (get_md5_password(username
, groupname
, junk
) != NULL
)
1453 * If we get this far, then the user isn't part of the named group...
1461 * 'cupsdCopyLocation()' - Make a copy of a location...
1464 cupsd_location_t
* /* O - New location */
1466 cupsd_location_t
**loc
) /* IO - Original location */
1468 int i
; /* Looping var */
1469 cupsd_location_t
*temp
; /* New location */
1470 char location
[HTTP_MAX_URI
];
1471 /* Location of resource */
1475 * Use a local copy of location because cupsdAddLocation may cause
1476 * this memory to be moved...
1479 strlcpy(location
, (*loc
)->location
, sizeof(location
));
1481 if ((temp
= cupsdAddLocation(location
)) == NULL
)
1485 * Copy the information from the original location to the new one.
1488 temp
->limit
= (*loc
)->limit
;
1489 temp
->order_type
= (*loc
)->order_type
;
1490 temp
->type
= (*loc
)->type
;
1491 temp
->level
= (*loc
)->level
;
1492 temp
->satisfy
= (*loc
)->satisfy
;
1493 temp
->encryption
= (*loc
)->encryption
;
1495 if ((temp
->num_names
= (*loc
)->num_names
) > 0)
1498 * Copy the names array...
1501 if ((temp
->names
= calloc(temp
->num_names
, sizeof(char *))) == NULL
)
1503 cupsdLogMessage(CUPSD_LOG_ERROR
,
1504 "cupsdCopyLocation: Unable to allocate memory for %d names: %s",
1505 temp
->num_names
, strerror(errno
));
1507 cupsdDeleteLocation(temp
);
1511 for (i
= 0; i
< temp
->num_names
; i
++)
1512 if ((temp
->names
[i
] = strdup((*loc
)->names
[i
])) == NULL
)
1514 cupsdLogMessage(CUPSD_LOG_ERROR
,
1515 "cupsdCopyLocation: Unable to copy name \"%s\": %s",
1516 (*loc
)->names
[i
], strerror(errno
));
1518 cupsdDeleteLocation(temp
);
1523 if ((temp
->num_allow
= (*loc
)->num_allow
) > 0)
1526 * Copy allow rules...
1529 if ((temp
->allow
= calloc(temp
->num_allow
, sizeof(cupsd_authmask_t
))) == NULL
)
1531 cupsdLogMessage(CUPSD_LOG_ERROR
,
1532 "cupsdCopyLocation: Unable to allocate memory for %d allow rules: %s",
1533 temp
->num_allow
, strerror(errno
));
1534 cupsdDeleteLocation(temp
);
1538 for (i
= 0; i
< temp
->num_allow
; i
++)
1539 switch (temp
->allow
[i
].type
= (*loc
)->allow
[i
].type
)
1541 case CUPSD_AUTH_NAME
:
1542 temp
->allow
[i
].mask
.name
.length
= (*loc
)->allow
[i
].mask
.name
.length
;
1543 temp
->allow
[i
].mask
.name
.name
= strdup((*loc
)->allow
[i
].mask
.name
.name
);
1545 if (temp
->allow
[i
].mask
.name
.name
== NULL
)
1547 cupsdLogMessage(CUPSD_LOG_ERROR
,
1548 "cupsdCopyLocation: Unable to copy allow name \"%s\": %s",
1549 (*loc
)->allow
[i
].mask
.name
.name
, strerror(errno
));
1550 cupsdDeleteLocation(temp
);
1554 case CUPSD_AUTH_IP
:
1555 memcpy(&(temp
->allow
[i
].mask
.ip
), &((*loc
)->allow
[i
].mask
.ip
),
1556 sizeof(cupsd_ipmask_t
));
1561 if ((temp
->num_deny
= (*loc
)->num_deny
) > 0)
1564 * Copy deny rules...
1567 if ((temp
->deny
= calloc(temp
->num_deny
, sizeof(cupsd_authmask_t
))) == NULL
)
1569 cupsdLogMessage(CUPSD_LOG_ERROR
,
1570 "cupsdCopyLocation: Unable to allocate memory for %d deny rules: %s",
1571 temp
->num_deny
, strerror(errno
));
1572 cupsdDeleteLocation(temp
);
1576 for (i
= 0; i
< temp
->num_deny
; i
++)
1577 switch (temp
->deny
[i
].type
= (*loc
)->deny
[i
].type
)
1579 case CUPSD_AUTH_NAME
:
1580 temp
->deny
[i
].mask
.name
.length
= (*loc
)->deny
[i
].mask
.name
.length
;
1581 temp
->deny
[i
].mask
.name
.name
= strdup((*loc
)->deny
[i
].mask
.name
.name
);
1583 if (temp
->deny
[i
].mask
.name
.name
== NULL
)
1585 cupsdLogMessage(CUPSD_LOG_ERROR
,
1586 "cupsdCopyLocation: Unable to copy deny name \"%s\": %s",
1587 (*loc
)->deny
[i
].mask
.name
.name
, strerror(errno
));
1588 cupsdDeleteLocation(temp
);
1592 case CUPSD_AUTH_IP
:
1593 memcpy(&(temp
->deny
[i
].mask
.ip
), &((*loc
)->deny
[i
].mask
.ip
),
1594 sizeof(cupsd_ipmask_t
));
1604 * 'cupsdDeleteAllLocations()' - Free all memory used for location authorization.
1608 cupsdDeleteAllLocations(void)
1610 cupsd_location_t
*loc
; /* Current location */
1614 * Free all of the allow/deny records first...
1617 for (loc
= (cupsd_location_t
*)cupsArrayFirst(Locations
);
1619 loc
= (cupsd_location_t
*)cupsArrayNext(Locations
))
1620 cupsdDeleteLocation(loc
);
1623 * Then free the location array...
1626 cupsArrayDelete(Locations
);
1632 * 'cupsdDeleteLocation()' - Free all memory used by a location.
1636 cupsdDeleteLocation(
1637 cupsd_location_t
*loc
) /* I - Location to delete */
1639 int i
; /* Looping var */
1640 cupsd_authmask_t
*mask
; /* Current mask */
1643 cupsArrayRemove(Locations
, loc
);
1645 for (i
= loc
->num_names
- 1; i
>= 0; i
--)
1646 free(loc
->names
[i
]);
1648 if (loc
->num_names
> 0)
1651 for (i
= loc
->num_allow
, mask
= loc
->allow
; i
> 0; i
--, mask
++)
1652 if (mask
->type
== CUPSD_AUTH_NAME
|| mask
->type
== CUPSD_AUTH_INTERFACE
)
1653 free(mask
->mask
.name
.name
);
1655 if (loc
->num_allow
> 0)
1658 for (i
= loc
->num_deny
, mask
= loc
->deny
; i
> 0; i
--, mask
++)
1659 if (mask
->type
== CUPSD_AUTH_NAME
|| mask
->type
== CUPSD_AUTH_INTERFACE
)
1660 free(mask
->mask
.name
.name
);
1662 if (loc
->num_deny
> 0)
1665 free(loc
->location
);
1671 * 'cupsdDenyHost()' - Add a host name that is not allowed to access the
1676 cupsdDenyHost(cupsd_location_t
*loc
, /* I - Location to add to */
1677 char *name
) /* I - Name of host or domain to add */
1679 cupsd_authmask_t
*temp
; /* New host/domain mask */
1680 char ifname
[32], /* Interface name */
1681 *ifptr
; /* Pointer to end of name */
1684 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdDenyHost(loc=%p(%s), name=\"%s\")",
1685 loc
, loc
->location
? loc
->location
: "nil", name
);
1687 if ((temp
= add_deny(loc
)) == NULL
)
1690 if (!strcasecmp(name
, "@LOCAL"))
1693 * Deny *interface*...
1696 temp
->type
= CUPSD_AUTH_INTERFACE
;
1697 temp
->mask
.name
.name
= strdup("*");
1698 temp
->mask
.name
.length
= 1;
1700 else if (!strncasecmp(name
, "@IF(", 4))
1703 * Deny *interface*...
1706 strlcpy(ifname
, name
+ 4, sizeof(ifname
));
1708 ifptr
= ifname
+ strlen(ifname
);
1710 if (ifptr
[-1] == ')')
1716 temp
->type
= CUPSD_AUTH_INTERFACE
;
1717 temp
->mask
.name
.name
= strdup(ifname
);
1718 temp
->mask
.name
.length
= ifptr
- ifname
;
1726 temp
->type
= CUPSD_AUTH_NAME
;
1727 temp
->mask
.name
.name
= strdup(name
);
1728 temp
->mask
.name
.length
= strlen(name
);
1734 * 'cupsdDenyIP()' - Add an IP address or network that is not allowed to
1735 * access the location.
1739 cupsdDenyIP(cupsd_location_t
*loc
, /* I - Location to add to */
1740 const unsigned address
[4],/* I - IP address to add */
1741 const unsigned netmask
[4])/* I - Netmask of address */
1743 cupsd_authmask_t
*temp
; /* New host/domain mask */
1746 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1747 "cupsdDenyIP(loc=%p(%s), address=%x:%x:%x:%x, netmask=%x:%x:%x:%x)",
1748 loc
, loc
->location
? loc
->location
: "nil",
1749 address
[0], address
[1], address
[2], address
[3],
1750 netmask
[0], netmask
[1], netmask
[2], netmask
[3]);
1752 if ((temp
= add_deny(loc
)) == NULL
)
1755 temp
->type
= CUPSD_AUTH_IP
;
1756 memcpy(temp
->mask
.ip
.address
, address
, sizeof(temp
->mask
.ip
.address
));
1757 memcpy(temp
->mask
.ip
.netmask
, netmask
, sizeof(temp
->mask
.ip
.netmask
));
1762 * 'cupsdFindBest()' - Find the location entry that best matches the resource.
1765 cupsd_location_t
* /* O - Location that matches */
1766 cupsdFindBest(const char *path
, /* I - Resource path */
1767 http_state_t state
) /* I - HTTP state/request */
1769 char uri
[HTTP_MAX_URI
],
1770 /* URI in request... */
1771 *uriptr
; /* Pointer into URI */
1772 cupsd_location_t
*loc
, /* Current location */
1773 *best
; /* Best match for location so far */
1774 int bestlen
; /* Length of best match */
1775 int limit
; /* Limit field */
1776 static const int limits
[] = /* Map http_status_t to CUPSD_AUTH_LIMIT_xyz */
1778 CUPSD_AUTH_LIMIT_ALL
,
1779 CUPSD_AUTH_LIMIT_OPTIONS
,
1780 CUPSD_AUTH_LIMIT_GET
,
1781 CUPSD_AUTH_LIMIT_GET
,
1782 CUPSD_AUTH_LIMIT_HEAD
,
1783 CUPSD_AUTH_LIMIT_POST
,
1784 CUPSD_AUTH_LIMIT_POST
,
1785 CUPSD_AUTH_LIMIT_POST
,
1786 CUPSD_AUTH_LIMIT_PUT
,
1787 CUPSD_AUTH_LIMIT_PUT
,
1788 CUPSD_AUTH_LIMIT_DELETE
,
1789 CUPSD_AUTH_LIMIT_TRACE
,
1790 CUPSD_AUTH_LIMIT_ALL
,
1791 CUPSD_AUTH_LIMIT_ALL
1796 * First copy the connection URI to a local string so we have drop
1797 * any .ppd extension from the pathname in /printers or /classes
1801 strlcpy(uri
, path
, sizeof(uri
));
1803 if (!strncmp(uri
, "/printers/", 10) ||
1804 !strncmp(uri
, "/classes/", 9))
1807 * Check if the URI has .ppd on the end...
1810 uriptr
= uri
+ strlen(uri
) - 4; /* len > 4 if we get here... */
1812 if (!strcmp(uriptr
, ".ppd"))
1816 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: uri = \"%s\"...", uri
);
1819 * Loop through the list of locations to find a match...
1822 limit
= limits
[state
];
1826 for (loc
= (cupsd_location_t
*)cupsArrayFirst(Locations
);
1828 loc
= (cupsd_location_t
*)cupsArrayNext(Locations
))
1830 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: Location %s Limit %x",
1831 loc
->location
? loc
->location
: "nil", loc
->limit
);
1833 if (!strncmp(uri
, "/printers/", 10) || !strncmp(uri
, "/classes/", 9))
1836 * Use case-insensitive comparison for queue names...
1839 if (loc
->length
> bestlen
&& loc
->location
&&
1840 !strncasecmp(uri
, loc
->location
, loc
->length
) &&
1841 loc
->location
[0] == '/' &&
1842 (limit
& loc
->limit
) != 0)
1845 bestlen
= loc
->length
;
1851 * Use case-sensitive comparison for other URIs...
1854 if (loc
->length
> bestlen
&& loc
->location
&&
1855 !strncmp(uri
, loc
->location
, loc
->length
) &&
1856 loc
->location
[0] == '/' &&
1857 (limit
& loc
->limit
) != 0)
1860 bestlen
= loc
->length
;
1866 * Return the match, if any...
1869 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: best = %s",
1870 best
? best
->location
: "NONE");
1877 * 'cupsdFindLocation()' - Find the named location.
1880 cupsd_location_t
* /* O - Location that matches */
1881 cupsdFindLocation(const char *location
) /* I - Connection */
1883 cupsd_location_t key
; /* Search key */
1886 key
.location
= (char *)location
;
1888 return ((cupsd_location_t
*)cupsArrayFind(Locations
, &key
));
1893 * 'cupsdIsAuthorized()' - Check to see if the user is authorized...
1896 http_status_t
/* O - HTTP_OK if authorized or error code */
1897 cupsdIsAuthorized(cupsd_client_t
*con
, /* I - Connection */
1898 const char *owner
)/* I - Owner of object */
1900 int i
, j
, /* Looping vars */
1901 auth
, /* Authorization status */
1902 type
; /* Type of authentication */
1903 unsigned address
[4]; /* Authorization address */
1904 cupsd_location_t
*best
; /* Best match for location so far */
1905 int hostlen
; /* Length of hostname */
1906 char username
[256], /* Username to authorize */
1907 ownername
[256], /* Owner name to authorize */
1908 *ptr
; /* Pointer into username */
1909 struct passwd
*pw
; /* User password data */
1910 static const char * const levels
[] = /* Auth levels */
1916 static const char * const types
[] = /* Auth types */
1926 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1927 "cupsdIsAuthorized: con->uri=\"%s\", con->best=%p(%s)",
1928 con
->uri
, con
->best
, con
->best
? con
->best
->location
?
1929 con
->best
->location
: "(null)" : "");
1931 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1932 "cupsdIsAuthorized: owner=\"%s\"", owner
);
1935 * If there is no "best" authentication rule for this request, then
1936 * access is allowed from the local system and denied from other
1942 if (!strcmp(con
->http
.hostname
, "localhost") ||
1943 !strcmp(con
->http
.hostname
, ServerName
))
1946 return (HTTP_FORBIDDEN
);
1951 if ((type
= best
->type
) == CUPSD_AUTH_DEFAULT
)
1952 type
= DefaultAuthType
;
1954 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1955 "cupsdIsAuthorized: level=CUPSD_AUTH_%s, type=%s, "
1956 "satisfy=CUPSD_AUTH_SATISFY_%s, num_names=%d",
1957 levels
[best
->level
], types
[type
],
1958 best
->satisfy
? "ANY" : "ALL", best
->num_names
);
1960 if (best
->limit
== CUPSD_AUTH_LIMIT_IPP
)
1961 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: op=%x(%s)",
1962 best
->op
, ippOpString(best
->op
));
1965 * Check host/ip-based accesses...
1969 if (con
->http
.hostaddr
->addr
.sa_family
== AF_INET6
)
1972 * Copy IPv6 address...
1975 address
[0] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[0]);
1976 address
[1] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[1]);
1977 address
[2] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[2]);
1978 address
[3] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[3]);
1981 #endif /* AF_INET6 */
1982 if (con
->http
.hostaddr
->addr
.sa_family
== AF_INET
)
1985 * Copy IPv4 address...
1991 address
[3] = ntohl(con
->http
.hostaddr
->ipv4
.sin_addr
.s_addr
);
1994 memset(address
, 0, sizeof(address
));
1996 hostlen
= strlen(con
->http
.hostname
);
1998 auth
= cupsdCheckAccess(address
, con
->http
.hostname
, hostlen
, best
)
1999 ? CUPSD_AUTH_ALLOW
: CUPSD_AUTH_DENY
;
2001 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: auth=CUPSD_AUTH_%s...",
2002 auth
? "DENY" : "ALLOW");
2004 if (auth
== CUPSD_AUTH_DENY
&& best
->satisfy
== CUPSD_AUTH_SATISFY_ALL
)
2005 return (HTTP_FORBIDDEN
);
2009 * See if encryption is required...
2012 if ((best
->encryption
>= HTTP_ENCRYPT_REQUIRED
&& !con
->http
.tls
&&
2013 strcasecmp(con
->http
.hostname
, "localhost") &&
2014 best
->satisfy
== CUPSD_AUTH_SATISFY_ALL
) &&
2015 !(type
== CUPSD_AUTH_NEGOTIATE
||
2016 (type
== CUPSD_AUTH_NONE
&& DefaultAuthType
== CUPSD_AUTH_NEGOTIATE
)))
2018 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2019 "cupsdIsAuthorized: Need upgrade to TLS...");
2020 return (HTTP_UPGRADE_REQUIRED
);
2022 #endif /* HAVE_SSL */
2025 * Now see what access level is required...
2028 if (best
->level
== CUPSD_AUTH_ANON
|| /* Anonymous access - allow it */
2029 (type
== CUPSD_AUTH_NONE
&& best
->num_names
== 0))
2032 if (!con
->username
[0] && type
== CUPSD_AUTH_NONE
&&
2033 best
->limit
== CUPSD_AUTH_LIMIT_IPP
)
2036 * Check for unauthenticated username...
2039 ipp_attribute_t
*attr
; /* requesting-user-name attribute */
2042 attr
= ippFindAttribute(con
->request
, "requesting-user-name", IPP_TAG_NAME
);
2045 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2046 "cupsdIsAuthorized: requesting-user-name=\"%s\"",
2047 attr
->values
[0].string
.text
);
2048 strlcpy(username
, attr
->values
[0].string
.text
, sizeof(username
));
2050 else if (best
->satisfy
== CUPSD_AUTH_SATISFY_ALL
|| auth
== CUPSD_AUTH_DENY
)
2051 return (HTTP_UNAUTHORIZED
); /* Non-anonymous needs user/pass */
2053 return (HTTP_OK
); /* unless overridden with Satisfy */
2057 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdIsAuthorized: username=\"%s\"",
2060 #ifdef HAVE_AUTHORIZATION_H
2061 if (!con
->username
[0] && !con
->authref
)
2063 if (!con
->username
[0])
2064 #endif /* HAVE_AUTHORIZATION_H */
2066 if (best
->satisfy
== CUPSD_AUTH_SATISFY_ALL
|| auth
== CUPSD_AUTH_DENY
)
2067 return (HTTP_UNAUTHORIZED
); /* Non-anonymous needs user/pass */
2069 return (HTTP_OK
); /* unless overridden with Satisfy */
2072 if (con
->type
!= type
&& type
!= CUPSD_AUTH_NONE
&&
2073 (con
->type
!= CUPSD_AUTH_BASIC
|| type
!= CUPSD_AUTH_BASICDIGEST
))
2075 cupsdLogMessage(CUPSD_LOG_ERROR
, "Authorized using %s, expected %s!",
2076 types
[con
->type
], types
[type
]);
2078 return (HTTP_UNAUTHORIZED
);
2081 strlcpy(username
, con
->username
, sizeof(username
));
2085 * OK, got a username. See if we need normal user access, or group
2086 * access... (root always matches)
2089 if (!strcmp(username
, "root"))
2093 * Strip any @domain or @KDC from the username and owner...
2096 if ((ptr
= strchr(username
, '@')) != NULL
)
2101 strlcpy(ownername
, owner
, sizeof(ownername
));
2103 if ((ptr
= strchr(ownername
, '@')) != NULL
)
2107 ownername
[0] = '\0';
2110 * Get the user info...
2115 pw
= getpwnam(username
);
2121 if (best
->level
== CUPSD_AUTH_USER
)
2124 * If there are no names associated with this location, then
2125 * any valid user is OK...
2128 if (best
->num_names
== 0)
2132 * Otherwise check the user list and return OK if this user is
2136 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2137 "cupsdIsAuthorized: Checking user membership...");
2139 #ifdef HAVE_AUTHORIZATION_H
2141 * If an authorization reference was supplied it must match a right name...
2146 for (i
= 0; i
< best
->num_names
; i
++)
2148 if (!strncasecmp(best
->names
[i
], "@AUTHKEY(", 9) &&
2149 check_authref(con
, best
->names
[i
] + 9))
2151 else if (!strcasecmp(best
->names
[i
], "@SYSTEM") &&
2152 SystemGroupAuthKey
&&
2153 check_authref(con
, SystemGroupAuthKey
))
2157 return (HTTP_UNAUTHORIZED
);
2159 #endif /* HAVE_AUTHORIZATION_H */
2161 for (i
= 0; i
< best
->num_names
; i
++)
2163 if (!strcasecmp(best
->names
[i
], "@OWNER") && owner
&&
2164 !strcasecmp(username
, ownername
))
2166 else if (!strcasecmp(best
->names
[i
], "@SYSTEM"))
2168 for (j
= 0; j
< NumSystemGroups
; j
++)
2169 if (cupsdCheckGroup(username
, pw
, SystemGroups
[j
]))
2172 else if (best
->names
[i
][0] == '@')
2174 if (cupsdCheckGroup(username
, pw
, best
->names
[i
] + 1))
2177 else if (!strcasecmp(username
, best
->names
[i
]))
2181 return (HTTP_UNAUTHORIZED
);
2185 * Check to see if this user is in any of the named groups...
2188 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2189 "cupsdIsAuthorized: Checking group membership...");
2192 * Check to see if this user is in any of the named groups...
2195 for (i
= 0; i
< best
->num_names
; i
++)
2197 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2198 "cupsdIsAuthorized: Checking group \"%s\" membership...",
2201 if (!strcasecmp(best
->names
[i
], "@SYSTEM"))
2203 for (j
= 0; j
< NumSystemGroups
; j
++)
2204 if (cupsdCheckGroup(username
, pw
, SystemGroups
[j
]))
2207 else if (cupsdCheckGroup(username
, pw
, best
->names
[i
]))
2212 * The user isn't part of the specified group, so deny access...
2215 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2216 "cupsdIsAuthorized: User not in group(s)!");
2218 return (HTTP_UNAUTHORIZED
);
2223 * 'add_allow()' - Add an allow mask to the location.
2226 static cupsd_authmask_t
* /* O - New mask record */
2227 add_allow(cupsd_location_t
*loc
) /* I - Location to add to */
2229 cupsd_authmask_t
*temp
; /* New mask record */
2240 * Try to allocate memory for the record...
2243 if (loc
->num_allow
== 0)
2244 temp
= malloc(sizeof(cupsd_authmask_t
));
2246 temp
= realloc(loc
->allow
, sizeof(cupsd_authmask_t
) * (loc
->num_allow
+ 1));
2252 temp
+= loc
->num_allow
;
2256 * Clear the mask record and return...
2259 memset(temp
, 0, sizeof(cupsd_authmask_t
));
2265 * 'add_deny()' - Add a deny mask to the location.
2268 static cupsd_authmask_t
* /* O - New mask record */
2269 add_deny(cupsd_location_t
*loc
) /* I - Location to add to */
2271 cupsd_authmask_t
*temp
; /* New mask record */
2282 * Try to allocate memory for the record...
2285 if (loc
->num_deny
== 0)
2286 temp
= malloc(sizeof(cupsd_authmask_t
));
2288 temp
= realloc(loc
->deny
, sizeof(cupsd_authmask_t
) * (loc
->num_deny
+ 1));
2294 temp
+= loc
->num_deny
;
2298 * Clear the mask record and return...
2301 memset(temp
, 0, sizeof(cupsd_authmask_t
));
2306 #ifdef HAVE_AUTHORIZATION_H
2308 * 'check_authref()' - Check if an authorization services reference has the
2312 static int /* O - 1 if right is valid, 0 otherwise */
2313 check_authref(cupsd_client_t
*con
, /* I - Connection */
2314 const char *right
) /* I - Right name */
2316 OSStatus status
; /* OS Status */
2317 AuthorizationItem authright
; /* Authorization right */
2318 AuthorizationRights authrights
; /* Authorization rights */
2319 AuthorizationFlags authflags
; /* Authorization flags */
2323 * Check to see if the user is allowed to perform the task...
2329 authright
.name
= right
;
2330 authright
.valueLength
= 0;
2331 authright
.value
= NULL
;
2332 authright
.flags
= 0;
2334 authrights
.count
= 1;
2335 authrights
.items
= &authright
;
2337 authflags
= kAuthorizationFlagDefaults
|
2338 kAuthorizationFlagExtendRights
;
2340 if ((status
= AuthorizationCopyRights(con
->authref
, &authrights
,
2341 kAuthorizationEmptyEnvironment
,
2342 authflags
, NULL
)) != 0)
2344 cupsdLogMessage(CUPSD_LOG_ERROR
,
2345 "AuthorizationCopyRights(\"%s\") returned %d (%s)",
2346 authright
.name
, (int)status
, cssmErrorString(status
));
2350 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2351 "AuthorizationCopyRights(\"%s\") succeeded!",
2356 #endif /* HAVE_AUTHORIZATION_H */
2360 * 'compare_locations()' - Compare two locations.
2363 static int /* O - Result of comparison */
2364 compare_locations(cupsd_location_t
*a
, /* I - First location */
2365 cupsd_location_t
*b
) /* I - Second location */
2367 return (strcmp(b
->location
, a
->location
));
2371 #if !HAVE_LIBPAM && !defined(HAVE_USERSEC_H)
2373 * 'cups_crypt()' - Encrypt the password using the DES or MD5 algorithms,
2377 static char * /* O - Encrypted password */
2378 cups_crypt(const char *pw
, /* I - Password string */
2379 const char *salt
) /* I - Salt (key) string */
2381 if (!strncmp(salt
, "$1$", 3))
2384 * Use MD5 passwords without the benefit of PAM; this is for
2385 * Slackware Linux, and the algorithm was taken from the
2386 * old shadow-19990827/lib/md5crypt.c source code... :(
2389 int i
; /* Looping var */
2390 unsigned long n
; /* Output number */
2391 int pwlen
; /* Length of password string */
2392 const char *salt_end
; /* End of "salt" data for MD5 */
2393 char *ptr
; /* Pointer into result string */
2394 _cups_md5_state_t state
; /* Primary MD5 state info */
2395 _cups_md5_state_t state2
; /* Secondary MD5 state info */
2396 unsigned char digest
[16]; /* MD5 digest result */
2397 static char result
[120]; /* Final password string */
2401 * Get the salt data between dollar signs, e.g. $1$saltdata$md5.
2402 * Get a maximum of 8 characters of salt data after $1$...
2405 for (salt_end
= salt
+ 3; *salt_end
&& (salt_end
- salt
) < 11; salt_end
++)
2406 if (*salt_end
== '$')
2410 * Compute the MD5 sum we need...
2415 _cupsMD5Init(&state
);
2416 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
2417 _cupsMD5Append(&state
, (unsigned char *)salt
, salt_end
- salt
);
2419 _cupsMD5Init(&state2
);
2420 _cupsMD5Append(&state2
, (unsigned char *)pw
, pwlen
);
2421 _cupsMD5Append(&state2
, (unsigned char *)salt
+ 3, salt_end
- salt
- 3);
2422 _cupsMD5Append(&state2
, (unsigned char *)pw
, pwlen
);
2423 _cupsMD5Finish(&state2
, digest
);
2425 for (i
= pwlen
; i
> 0; i
-= 16)
2426 _cupsMD5Append(&state
, digest
, i
> 16 ? 16 : i
);
2428 for (i
= pwlen
; i
> 0; i
>>= 1)
2429 _cupsMD5Append(&state
, (unsigned char *)((i
& 1) ? "" : pw
), 1);
2431 _cupsMD5Finish(&state
, digest
);
2433 for (i
= 0; i
< 1000; i
++)
2435 _cupsMD5Init(&state
);
2438 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
2440 _cupsMD5Append(&state
, digest
, 16);
2443 _cupsMD5Append(&state
, (unsigned char *)salt
+ 3, salt_end
- salt
- 3);
2446 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
2449 _cupsMD5Append(&state
, digest
, 16);
2451 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
2453 _cupsMD5Finish(&state
, digest
);
2457 * Copy the final sum to the result string and return...
2460 memcpy(result
, salt
, salt_end
- salt
);
2461 ptr
= result
+ (salt_end
- salt
);
2464 for (i
= 0; i
< 5; i
++, ptr
+= 4)
2466 n
= (((digest
[i
] << 8) | digest
[i
+ 6]) << 8);
2469 n
|= digest
[i
+ 12];
2476 to64(ptr
, digest
[11], 2);
2485 * Use the standard crypt() function...
2488 return (crypt(pw
, salt
));
2491 #endif /* !HAVE_LIBPAM && !HAVE_USERSEC_H */
2496 * 'get_gss_creds()' - Obtain GSS credentials.
2499 static gss_cred_id_t
/* O - Server credentials */
2501 const char *service_name
, /* I - Service name */
2502 const char *con_server_name
) /* I - Hostname of server */
2504 OM_uint32 major_status
, /* Major status code */
2505 minor_status
; /* Minor status code */
2506 gss_name_t server_name
; /* Server name */
2507 gss_cred_id_t server_creds
; /* Server credentials */
2508 gss_buffer_desc token
= GSS_C_EMPTY_BUFFER
;
2509 /* Service name token */
2510 char buf
[1024]; /* Service name buffer */
2513 snprintf(buf
, sizeof(buf
), "%s@%s", service_name
, con_server_name
);
2516 token
.length
= strlen(buf
);
2517 server_name
= GSS_C_NO_NAME
;
2518 major_status
= gss_import_name(&minor_status
, &token
,
2519 GSS_C_NT_HOSTBASED_SERVICE
,
2522 memset(&token
, 0, sizeof(token
));
2524 if (GSS_ERROR(major_status
))
2526 cupsdLogGSSMessage(CUPSD_LOG_WARN
, major_status
, minor_status
,
2527 "gss_import_name() failed");
2531 major_status
= gss_display_name(&minor_status
, server_name
, &token
, NULL
);
2533 if (GSS_ERROR(major_status
))
2535 cupsdLogGSSMessage(CUPSD_LOG_WARN
, major_status
, minor_status
,
2536 "gss_display_name() failed");
2540 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2541 "get_gss_creds: Attempting to acquire credentials for %s...",
2542 (char *)token
.value
);
2544 server_creds
= GSS_C_NO_CREDENTIAL
;
2545 major_status
= gss_acquire_cred(&minor_status
, server_name
, GSS_C_INDEFINITE
,
2546 GSS_C_NO_OID_SET
, GSS_C_ACCEPT
,
2547 &server_creds
, NULL
, NULL
);
2548 if (GSS_ERROR(major_status
))
2550 cupsdLogGSSMessage(CUPSD_LOG_WARN
, major_status
, minor_status
,
2551 "gss_acquire_cred() failed");
2552 gss_release_name(&minor_status
, &server_name
);
2553 gss_release_buffer(&minor_status
, &token
);
2557 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2558 "get_gss_creds: Credentials acquired successfully for %s.",
2559 (char *)token
.value
);
2561 gss_release_name(&minor_status
, &server_name
);
2562 gss_release_buffer(&minor_status
, &token
);
2564 return (server_creds
);
2566 #endif /* HAVE_GSSAPI */
2570 * 'get_md5_password()' - Get an MD5 password.
2573 static char * /* O - MD5 password string */
2574 get_md5_password(const char *username
, /* I - Username */
2575 const char *group
, /* I - Group */
2576 char passwd
[33]) /* O - MD5 password string */
2578 cups_file_t
*fp
; /* passwd.md5 file */
2579 char filename
[1024], /* passwd.md5 filename */
2580 line
[256], /* Line from file */
2581 tempuser
[33], /* User from file */
2582 tempgroup
[33]; /* Group from file */
2585 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2586 "get_md5_password(username=\"%s\", group=\"%s\", passwd=%p)",
2587 username
, group
? group
: "(null)", passwd
);
2589 snprintf(filename
, sizeof(filename
), "%s/passwd.md5", ServerRoot
);
2590 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
2592 if (errno
!= ENOENT
)
2593 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open %s - %s", filename
,
2599 while (cupsFileGets(fp
, line
, sizeof(line
)) != NULL
)
2601 if (sscanf(line
, "%32[^:]:%32[^:]:%32s", tempuser
, tempgroup
, passwd
) != 3)
2603 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad MD5 password line: %s", line
);
2607 if (!strcmp(username
, tempuser
) &&
2608 (group
== NULL
|| !strcmp(group
, tempgroup
)))
2611 * Found the password entry!
2614 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Found MD5 user %s, group %s...",
2615 username
, tempgroup
);
2623 * Didn't find a password entry - return NULL!
2633 * 'pam_func()' - PAM conversation function.
2636 static int /* O - Success or failure */
2638 int num_msg
, /* I - Number of messages */
2639 const struct pam_message
**msg
, /* I - Messages */
2640 struct pam_response
**resp
, /* O - Responses */
2642 /* I - Pointer to connection */
2644 int i
; /* Looping var */
2645 struct pam_response
*replies
; /* Replies */
2646 cupsd_authdata_t
*data
; /* Pointer to auth data */
2650 * Allocate memory for the responses...
2653 if ((replies
= malloc(sizeof(struct pam_response
) * num_msg
)) == NULL
)
2654 return (PAM_CONV_ERR
);
2657 * Answer all of the messages...
2660 DEBUG_printf(("pam_func: appdata_ptr = %p\n", appdata_ptr
));
2664 * Apparently some versions of HP-UX 11 have a broken pam_unix security
2665 * module. This is a workaround...
2671 data
= (cupsd_authdata_t
*)appdata_ptr
;
2674 for (i
= 0; i
< num_msg
; i
++)
2676 DEBUG_printf(("pam_func: Message = \"%s\"\n", msg
[i
]->msg
));
2678 switch (msg
[i
]->msg_style
)
2680 case PAM_PROMPT_ECHO_ON
:
2681 DEBUG_printf(("pam_func: PAM_PROMPT_ECHO_ON, returning \"%s\"...\n",
2683 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2684 replies
[i
].resp
= strdup(data
->username
);
2687 case PAM_PROMPT_ECHO_OFF
:
2688 DEBUG_printf(("pam_func: PAM_PROMPT_ECHO_OFF, returning \"%s\"...\n",
2690 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2691 replies
[i
].resp
= strdup(data
->password
);
2695 DEBUG_puts("pam_func: PAM_TEXT_INFO...");
2696 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2697 replies
[i
].resp
= NULL
;
2701 DEBUG_puts("pam_func: PAM_ERROR_MSG...");
2702 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2703 replies
[i
].resp
= NULL
;
2707 DEBUG_printf(("pam_func: Unknown PAM message %d...\n",
2708 msg
[i
]->msg_style
));
2710 return (PAM_CONV_ERR
);
2715 * Return the responses back to PAM...
2720 return (PAM_SUCCESS
);
2722 #elif !defined(HAVE_USERSEC_H)
2726 * 'to64()' - Base64-encode an integer value...
2730 to64(char *s
, /* O - Output string */
2731 unsigned long v
, /* I - Value to encode */
2732 int n
) /* I - Number of digits */
2734 const char *itoa64
= "./0123456789"
2735 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2736 "abcdefghijklmnopqrstuvwxyz";
2739 for (; n
> 0; n
--, v
>>= 6)
2740 *s
++ = itoa64
[v
& 0x3f];
2742 #endif /* HAVE_LIBPAM */
2746 * End of "$Id: auth.c 7673 2008-06-18 22:31:26Z mike $".