2 * "$Id: auth.c 6361 2007-03-19 16:01:28Z mike $"
4 * Authorization routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
8 * This file contains Kerberos support code, copyright 2006 by
11 * These coded instructions, statements, and computer programs are the
12 * property of Easy Software Products and are protected by Federal
13 * copyright law. Distribution and use rights are outlined in the file
14 * "LICENSE.txt" which should have been included with this file. If this
15 * file is missing or damaged please contact Easy Software Products
18 * Attn: CUPS Licensing Information
19 * Easy Software Products
20 * 44141 Airport View Drive, Suite 204
21 * Hollywood, Maryland 20636 USA
23 * Voice: (301) 373-9600
24 * EMail: cups-info@cups.org
25 * WWW: http://www.cups.org
29 * cupsdAddLocation() - Add a location for authorization.
30 * cupsdAddName() - Add a name to a location...
31 * cupsdAllowHost() - Add a host name that is allowed to access the
33 * cupsdAllowIP() - Add an IP address or network that is allowed
34 * to access the location.
35 * cupsdAuthorize() - Validate any authorization credentials.
36 * cupsdCheckAuth() - Check authorization masks.
37 * cupsdCheckGroup() - Check for a user's group membership.
38 * cupsdCopyLocation() - Make a copy of a location...
39 * cupsdDeleteAllLocations() - Free all memory used for location
41 * cupsdDeleteLocation() - Free all memory used by a location.
42 * cupsdDenyHost() - Add a host name that is not allowed to access
44 * cupsdDenyIP() - Add an IP address or network that is not
45 * allowed to access the location.
46 * cupsdFindBest() - Find the location entry that best matches the
48 * cupsdFindLocation() - Find the named location.
49 * cupsdIsAuthorized() - Check to see if the user is authorized...
50 * add_allow() - Add an allow mask to the location.
51 * add_deny() - Add a deny mask to the location.
52 * compare_locations() - Compare two locations.
53 * cups_crypt() - Encrypt the password using the DES or MD5
54 * algorithms, as needed.
55 * get_gss_creds() - Obtain GSS credentials.
56 * get_md5_password() - Get an MD5 password.
57 * pam_func() - PAM conversation function.
58 * to64() - Base64-encode an integer value...
59 * check_authref() - Check an authorization services reference.
63 * Include necessary headers...
70 #endif /* HAVE_SHADOW_H */
73 #endif /* HAVE_CRYPT_H */
75 # ifdef HAVE_PAM_PAM_APPL_H
76 # include <pam/pam_appl.h>
78 # include <security/pam_appl.h>
79 # endif /* HAVE_PAM_PAM_APPL_H */
80 #endif /* HAVE_LIBPAM */
83 #endif /* HAVE_USERSEC_H */
84 #ifdef HAVE_MEMBERSHIP_H
85 # include <membership.h>
86 #endif /* HAVE_MEMBERSHIP_H */
87 #ifdef HAVE_AUTHORIZATION_H
88 # include <Security/AuthorizationTags.h>
89 # ifdef HAVE_SECBASEPRIV_H
90 # include <Security/SecBasePriv.h>
92 extern const char *cssmErrorString(int error
);
93 # endif /* HAVE_SECBASEPRIV_H */
94 #endif /* HAVE_AUTHORIZATION_H */
101 static cupsd_authmask_t
*add_allow(cupsd_location_t
*loc
);
102 static cupsd_authmask_t
*add_deny(cupsd_location_t
*loc
);
103 #ifdef HAVE_AUTHORIZATION_H
104 static int check_authref(cupsd_client_t
*con
, const char *right
);
105 #endif /* HAVE_AUTHORIZATION_H */
106 static int compare_locations(cupsd_location_t
*a
,
107 cupsd_location_t
*b
);
108 #if !HAVE_LIBPAM && !defined(HAVE_USERSEC_H)
109 static char *cups_crypt(const char *pw
, const char *salt
);
110 #endif /* !HAVE_LIBPAM && !HAVE_USERSEC_H */
112 static gss_cred_id_t
get_gss_creds(const char *service_name
);
113 #endif /* HAVE_GSSAPI */
114 static char *get_md5_password(const char *username
,
115 const char *group
, char passwd
[33]);
117 static int pam_func(int, const struct pam_message
**,
118 struct pam_response
**, void *);
119 #elif !defined(HAVE_USERSEC_H)
120 static void to64(char *s
, unsigned long v
, int n
);
121 #endif /* HAVE_LIBPAM */
125 * Local structures...
129 typedef struct cupsd_authdata_s
/**** Authentication data ****/
131 char username
[33], /* Username string */
132 password
[33]; /* Password string */
134 #endif /* HAVE_LIBPAM */
141 #if defined(__hpux) && HAVE_LIBPAM
142 static cupsd_authdata_t
*auth_data
; /* Current client being authenticated */
143 #endif /* __hpux && HAVE_LIBPAM */
147 * 'cupsdAddLocation()' - Add a location for authorization.
150 cupsd_location_t
* /* O - Pointer to new location record */
151 cupsdAddLocation(const char *location
) /* I - Location path */
153 cupsd_location_t
*temp
; /* New location */
157 * Make sure the locations array is created...
161 Locations
= cupsArrayNew((cups_array_func_t
)compare_locations
, NULL
);
167 * Try to allocate memory for the new location.
170 if ((temp
= calloc(1, sizeof(cupsd_location_t
))) == NULL
)
174 * Initialize the record and copy the name over...
177 temp
->location
= strdup(location
);
178 temp
->length
= strlen(temp
->location
);
180 cupsArrayAdd(Locations
, temp
);
182 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAddLocation: added location \'%s\'",
186 * Return the new record...
194 * 'cupsdAddName()' - Add a name to a location...
198 cupsdAddName(cupsd_location_t
*loc
, /* I - Location to add to */
199 char *name
) /* I - Name to add */
201 char **temp
; /* Pointer to names array */
204 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAddName(loc=%p, name=\"%s\")",
207 if (loc
->num_names
== 0)
208 temp
= malloc(sizeof(char *));
210 temp
= realloc(loc
->names
, (loc
->num_names
+ 1) * sizeof(char *));
214 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to add name to location %s: %s",
215 loc
->location
? loc
->location
: "nil", strerror(errno
));
221 if ((temp
[loc
->num_names
] = strdup(name
)) == NULL
)
223 cupsdLogMessage(CUPSD_LOG_ERROR
,
224 "Unable to duplicate name for location %s: %s",
225 loc
->location
? loc
->location
: "nil", strerror(errno
));
234 * 'cupsdAllowHost()' - Add a host name that is allowed to access the location.
238 cupsdAllowHost(cupsd_location_t
*loc
, /* I - Location to add to */
239 char *name
) /* I - Name of host or domain to add */
241 cupsd_authmask_t
*temp
; /* New host/domain mask */
242 char ifname
[32], /* Interface name */
243 *ifptr
; /* Pointer to end of name */
246 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAllowHost(loc=%p(%s), name=\"%s\")",
247 loc
, loc
->location
? loc
->location
: "nil", name
);
249 if ((temp
= add_allow(loc
)) == NULL
)
252 if (!strcasecmp(name
, "@LOCAL"))
255 * Allow *interface*...
258 temp
->type
= AUTH_INTERFACE
;
259 temp
->mask
.name
.name
= strdup("*");
260 temp
->mask
.name
.length
= 1;
262 else if (!strncasecmp(name
, "@IF(", 4))
265 * Allow *interface*...
268 strlcpy(ifname
, name
+ 4, sizeof(ifname
));
270 ifptr
= ifname
+ strlen(ifname
);
272 if (ifptr
[-1] == ')')
278 temp
->type
= AUTH_INTERFACE
;
279 temp
->mask
.name
.name
= strdup(ifname
);
280 temp
->mask
.name
.length
= ifptr
- ifname
;
288 temp
->type
= AUTH_NAME
;
289 temp
->mask
.name
.name
= strdup(name
);
290 temp
->mask
.name
.length
= strlen(name
);
296 * 'cupsdAllowIP()' - Add an IP address or network that is allowed to access
301 cupsdAllowIP(cupsd_location_t
*loc
, /* I - Location to add to */
302 unsigned address
[4], /* I - IP address to add */
303 unsigned netmask
[4]) /* I - Netmask of address */
305 cupsd_authmask_t
*temp
; /* New host/domain mask */
308 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
309 "cupsdAllowIP(loc=%p(%s), address=%x:%x:%x:%x, netmask=%x:%x:%x:%x)",
310 loc
, loc
->location
? loc
->location
: "nil",
311 address
[0], address
[1], address
[2], address
[3],
312 netmask
[0], netmask
[1], netmask
[2], netmask
[3]);
314 if ((temp
= add_allow(loc
)) == NULL
)
317 temp
->type
= AUTH_IP
;
318 memcpy(temp
->mask
.ip
.address
, address
, sizeof(temp
->mask
.ip
.address
));
319 memcpy(temp
->mask
.ip
.netmask
, netmask
, sizeof(temp
->mask
.ip
.netmask
));
324 * 'cupsdAuthorize()' - Validate any authorization credentials.
328 cupsdAuthorize(cupsd_client_t
*con
) /* I - Client connection */
330 int type
; /* Authentication type */
331 const char *authorization
; /* Pointer into Authorization string */
332 char *ptr
, /* Pointer into string */
333 username
[65], /* Username string */
334 password
[33]; /* Password string */
335 const char *localuser
; /* Certificate username */
336 char nonce
[HTTP_MAX_VALUE
], /* Nonce value from client */
337 md5
[33], /* MD5 password */
338 basicmd5
[33]; /* MD5 of Basic password */
339 static const char * const states
[] = /* HTTP client states... */
359 * Locate the best matching location so we know what kind of
360 * authentication to expect...
363 con
->best
= cupsdFindBest(con
->uri
, con
->http
.state
);
365 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
366 "cupsdAuthorize: con->uri=\"%s\", con->best=%p(%s)",
367 con
->uri
, con
->best
, con
->best
? con
->best
->location
: "");
369 if (con
->best
&& con
->best
->type
!= AUTH_NONE
)
370 type
= con
->best
->type
;
372 type
= DefaultAuthType
;
375 * Decode the Authorization string...
378 authorization
= httpGetField(&con
->http
, HTTP_FIELD_AUTHORIZATION
);
380 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAuthorize: Authorization=\"%s\"",
386 #ifdef HAVE_AUTHORIZATION_H
389 AuthorizationFree(con
->authref
, kAuthorizationFlagDefaults
);
392 #endif /* HAVE_AUTHORIZATION_H */
394 if (type
== AUTH_NONE
)
397 * No authorization required, return early...
400 cupsdLogMessage(CUPSD_LOG_DEBUG
,
401 "cupsdAuthorize: No authentication required.");
404 else if (!*authorization
)
407 * No authorization data provided, return early...
410 cupsdLogMessage(CUPSD_LOG_DEBUG
,
411 "cupsdAuthorize: No authentication data provided.");
414 #ifdef HAVE_AUTHORIZATION_H
415 else if (!strncmp(authorization
, "AuthRef", 6) &&
416 !strcasecmp(con
->http
.hostname
, "localhost"))
418 OSStatus status
; /* Status */
419 int authlen
; /* Auth string length */
420 AuthorizationItemSet
*authinfo
; /* Authorization item set */
423 * Get the Authorization Services data...
427 while (isspace(*authorization
& 255))
430 authlen
= sizeof(nonce
);
431 httpDecode64_2(nonce
, &authlen
, authorization
);
433 if (authlen
!= kAuthorizationExternalFormLength
)
435 cupsdLogMessage(CUPSD_LOG_ERROR
,
436 "cupsdAuthorize: External Authorization reference size"
441 if ((status
= AuthorizationCreateFromExternalForm(
442 (AuthorizationExternalForm
*)nonce
, &con
->authref
)) != 0)
444 cupsdLogMessage(CUPSD_LOG_ERROR
,
445 "cupsdAuthorize: AuthorizationCreateFromExternalForm "
447 (int)status
, cssmErrorString(status
));
451 if ((status
= AuthorizationCopyInfo(con
->authref
,
452 kAuthorizationEnvironmentUsername
,
455 cupsdLogMessage(CUPSD_LOG_ERROR
,
456 "cupsdAuthorize: AuthorizationCopyInfo returned %d (%s)",
457 (int)status
, cssmErrorString(status
));
461 if (authinfo
->count
== 1)
462 strlcpy(username
, authinfo
->items
[0].value
, sizeof(username
));
464 AuthorizationFreeItemSet(authinfo
);
466 #endif /* HAVE_AUTHORIZATION_H */
467 else if (!strncmp(authorization
, "Local", 5) &&
468 !strcasecmp(con
->http
.hostname
, "localhost"))
471 * Get Local certificate authentication data...
475 while (isspace(*authorization
& 255))
478 if ((localuser
= cupsdFindCert(authorization
)) != NULL
)
479 strlcpy(username
, localuser
, sizeof(username
));
482 cupsdLogMessage(CUPSD_LOG_ERROR
,
483 "cupsdAuthorize: Local authentication certificate not "
488 else if (!strncmp(authorization
, "Basic", 5) &&
489 (type
== AUTH_BASIC
|| type
== AUTH_BASICDIGEST
))
492 * Get the Basic authentication data...
495 int userlen
; /* Username:password length */
499 while (isspace(*authorization
& 255))
502 userlen
= sizeof(username
);
503 httpDecode64_2(username
, &userlen
, authorization
);
506 * Pull the username and password out...
509 if ((ptr
= strchr(username
, ':')) == NULL
)
511 cupsdLogMessage(CUPSD_LOG_ERROR
,
512 "cupsdAuthorize: Missing Basic password!");
521 * Username must not be empty...
524 cupsdLogMessage(CUPSD_LOG_ERROR
,
525 "cupsdAuthorize: Empty Basic username!");
532 * Password must not be empty...
535 cupsdLogMessage(CUPSD_LOG_ERROR
,
536 "cupsdAuthorize: Empty Basic password!");
540 strlcpy(password
, ptr
, sizeof(password
));
543 * Validate the username and password...
552 * Only use PAM to do authentication. This supports MD5
553 * passwords, among other things...
556 pam_handle_t
*pamh
; /* PAM authentication handle */
557 int pamerr
; /* PAM error code */
558 struct pam_conv pamdata
;/* PAM conversation data */
559 cupsd_authdata_t data
; /* Authentication data */
562 strlcpy(data
.username
, username
, sizeof(data
.username
));
563 strlcpy(data
.password
, password
, sizeof(data
.password
));
565 # if defined(__sun) || defined(__hpux)
566 pamdata
.conv
= (int (*)(int, struct pam_message
**,
567 struct pam_response
**,
570 pamdata
.conv
= pam_func
;
571 # endif /* __sun || __hpux */
572 pamdata
.appdata_ptr
= &data
;
576 * Workaround for HP-UX bug in pam_unix; see pam_func() below for
583 pamerr
= pam_start("cups", username
, &pamdata
, &pamh
);
584 if (pamerr
!= PAM_SUCCESS
)
586 cupsdLogMessage(CUPSD_LOG_ERROR
,
587 "cupsdAuthorize: pam_start() returned %d (%s)!\n",
588 pamerr
, pam_strerror(pamh
, pamerr
));
593 pamerr
= pam_authenticate(pamh
, PAM_SILENT
);
594 if (pamerr
!= PAM_SUCCESS
)
596 cupsdLogMessage(CUPSD_LOG_ERROR
,
597 "cupsdAuthorize: pam_authenticate() returned %d "
599 pamerr
, pam_strerror(pamh
, pamerr
));
604 pamerr
= pam_acct_mgmt(pamh
, PAM_SILENT
);
605 if (pamerr
!= PAM_SUCCESS
)
607 cupsdLogMessage(CUPSD_LOG_ERROR
,
608 "cupsdAuthorize: pam_acct_mgmt() returned %d "
610 pamerr
, pam_strerror(pamh
, pamerr
));
615 pam_end(pamh
, PAM_SUCCESS
);
617 #elif defined(HAVE_USERSEC_H)
619 * Use AIX authentication interface...
622 char *authmsg
; /* Authentication message */
623 int reenter
; /* ??? */
626 cupsdLogMessage(CUPSD_LOG_DEBUG
,
627 "cupsdAuthorize: AIX authenticate of username \"%s\"",
631 if (authenticate(username
, password
, &reenter
, &authmsg
) != 0)
633 cupsdLogMessage(CUPSD_LOG_DEBUG
,
634 "cupsdAuthorize: Unable to authenticate username "
636 username
, strerror(errno
));
642 * Use normal UNIX password file-based authentication...
645 char *pass
; /* Encrypted password */
646 struct passwd
*pw
; /* User password data */
647 # ifdef HAVE_SHADOW_H
648 struct spwd
*spw
; /* Shadow password data */
649 # endif /* HAVE_SHADOW_H */
652 pw
= getpwnam(username
); /* Get the current password */
653 endpwent(); /* Close the password file */
661 cupsdLogMessage(CUPSD_LOG_ERROR
,
662 "cupsdAuthorize: Unknown username \"%s\"!",
667 # ifdef HAVE_SHADOW_H
668 spw
= getspnam(username
);
671 if (!spw
&& !strcmp(pw
->pw_passwd
, "x"))
674 * Don't allow blank passwords!
677 cupsdLogMessage(CUPSD_LOG_ERROR
,
678 "cupsdAuthorize: Username \"%s\" has no shadow "
679 "password!", username
);
683 if (spw
&& !spw
->sp_pwdp
[0] && !pw
->pw_passwd
[0])
685 if (!pw
->pw_passwd
[0])
686 # endif /* HAVE_SHADOW_H */
689 * Don't allow blank passwords!
692 cupsdLogMessage(CUPSD_LOG_ERROR
,
693 "cupsdAuthorize: Username \"%s\" has no password!",
699 * OK, the password isn't blank, so compare with what came from the
703 pass
= cups_crypt(password
, pw
->pw_passwd
);
705 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
706 "cupsdAuthorize: pw_passwd=\"%s\", crypt=\"%s\"",
707 pw
->pw_passwd
, pass
);
709 if (!pass
|| strcmp(pw
->pw_passwd
, pass
))
711 # ifdef HAVE_SHADOW_H
714 pass
= cups_crypt(password
, spw
->sp_pwdp
);
716 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
717 "cupsdAuthorize: sp_pwdp=\"%s\", crypt=\"%s\"",
720 if (pass
== NULL
|| strcmp(spw
->sp_pwdp
, pass
))
722 cupsdLogMessage(CUPSD_LOG_ERROR
,
723 "cupsdAuthorize: Authentication failed for "
730 # endif /* HAVE_SHADOW_H */
732 cupsdLogMessage(CUPSD_LOG_ERROR
,
733 "cupsdAuthorize: Authentication failed for "
739 #endif /* HAVE_LIBPAM */
743 case AUTH_BASICDIGEST
:
745 * Do Basic authentication with the Digest password file...
748 if (!get_md5_password(username
, NULL
, md5
))
750 cupsdLogMessage(CUPSD_LOG_ERROR
,
751 "cupsdAuthorize: Unknown MD5 username \"%s\"!",
756 httpMD5(username
, "CUPS", password
, basicmd5
);
758 if (strcmp(md5
, basicmd5
))
760 cupsdLogMessage(CUPSD_LOG_ERROR
,
761 "cupsdAuthorize: Authentication failed for \"%s\"!",
768 else if (!strncmp(authorization
, "Digest", 6) && type
== AUTH_DIGEST
)
771 * Get the username, password, and nonce from the Digest attributes...
774 if (!httpGetSubField2(&(con
->http
), HTTP_FIELD_AUTHORIZATION
, "username",
775 username
, sizeof(username
)) || !username
[0])
778 * Username must not be empty...
781 cupsdLogMessage(CUPSD_LOG_ERROR
,
782 "cupsdAuthorize: Empty or missing Digest username!");
786 if (!httpGetSubField2(&(con
->http
), HTTP_FIELD_AUTHORIZATION
, "response",
787 password
, sizeof(password
)) || !password
[0])
790 * Password must not be empty...
793 cupsdLogMessage(CUPSD_LOG_ERROR
,
794 "cupsdAuthorize: Empty or missing Digest password!");
798 if (!httpGetSubField(&(con
->http
), HTTP_FIELD_AUTHORIZATION
, "nonce",
801 cupsdLogMessage(CUPSD_LOG_ERROR
,
802 "cupsdAuthorize: No nonce value for Digest "
807 if (strcmp(con
->http
.hostname
, nonce
))
809 cupsdLogMessage(CUPSD_LOG_ERROR
,
810 "cupsdAuthorize: Bad nonce value, expected \"%s\", "
811 "got \"%s\"!", con
->http
.hostname
, nonce
);
816 * Validate the username and password...
819 if (!get_md5_password(username
, NULL
, md5
))
821 cupsdLogMessage(CUPSD_LOG_ERROR
,
822 "cupsdAuthorize: Unknown MD5 username \"%s\"!",
827 httpMD5Final(nonce
, states
[con
->http
.state
], con
->uri
, md5
);
829 if (strcmp(md5
, password
))
831 cupsdLogMessage(CUPSD_LOG_ERROR
,
832 "cupsdAuthorize: Authentication failed for \"%s\"!",
838 else if (!strncmp(authorization
, "Negotiate", 9) && type
== AUTH_NEGOTIATE
)
840 int len
; /* Length of authorization string */
841 gss_cred_id_t server_creds
; /* Server credentials */
842 gss_ctx_id_t context
; /* Authorization context */
843 OM_uint32 major_status
, /* Major status code */
844 minor_status
; /* Minor status code */
845 gss_buffer_desc input_token
= GSS_C_EMPTY_BUFFER
,
846 /* Input token from string */
847 output_token
= GSS_C_EMPTY_BUFFER
;
848 /* Output token for username */
849 gss_name_t client_name
; /* Client name */
852 con
->gss_output_token
.length
= 0;
855 * Find the start of the Kerberos input token...
859 while (isspace(*authorization
& 255))
864 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
865 "cupsdAuthorize: No authentication data specified.");
870 * Get the server credentials...
873 if ((server_creds
= get_gss_creds(GSSServiceName
)) == NULL
)
875 con
->no_negotiate
= 1;
880 * Decode the authorization string to get the input token...
883 len
= strlen(authorization
);
884 input_token
.value
= malloc(len
);
885 input_token
.value
= httpDecode64_2(input_token
.value
, &len
,
887 input_token
.length
= len
;
890 * Accept the input token to get the authorization info...
893 context
= GSS_C_NO_CONTEXT
;
894 client_name
= GSS_C_NO_NAME
;
895 major_status
= gss_accept_sec_context(&minor_status
,
899 GSS_C_NO_CHANNEL_BINDINGS
,
902 &con
->gss_output_token
,
905 &con
->gss_delegated_cred
);
907 if (GSS_ERROR(major_status
))
909 cupsdLogGSSMessage(CUPSD_LOG_DEBUG
, major_status
, minor_status
,
910 "cupsdAuthorize: Error accepting GSSAPI security "
913 if (context
!= GSS_C_NO_CONTEXT
)
914 gss_delete_sec_context(&minor_status
, &context
, GSS_C_NO_BUFFER
);
916 con
->no_negotiate
= 1;
921 * Get the username associated with the credentials...
924 if (major_status
== GSS_S_COMPLETE
)
926 major_status
= gss_display_name(&minor_status
, client_name
,
927 &output_token
, NULL
);
929 if (GSS_ERROR(major_status
))
931 cupsdLogGSSMessage(CUPSD_LOG_DEBUG
, major_status
, minor_status
,
932 "cupsdAuthorize: Error getting username");
933 gss_release_name(&minor_status
, &client_name
);
934 gss_delete_sec_context(&minor_status
, &context
, GSS_C_NO_BUFFER
);
935 con
->no_negotiate
= 1;
939 gss_release_name(&minor_status
, &client_name
);
940 strlcpy(username
, output_token
.value
, sizeof(username
));
942 gss_release_buffer(&minor_status
, &output_token
);
943 gss_delete_sec_context(&minor_status
, &context
, GSS_C_NO_BUFFER
);
946 gss_release_name(&minor_status
, &client_name
);
948 #endif /* HAVE_GSSAPI */
951 cupsdLogMessage(CUPSD_LOG_DEBUG
,
952 "cupsdAuthorize: Bad authentication data.");
957 * If we get here, then we were able to validate the username and
958 * password - copy the validated username and password to the client
962 strlcpy(con
->username
, username
, sizeof(con
->username
));
963 strlcpy(con
->password
, password
, sizeof(con
->password
));
965 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAuthorize: username=\"%s\"",
971 * 'cupsdCheckAuth()' - Check authorization masks.
974 int /* O - 1 if mask matches, 0 otherwise */
976 unsigned ip
[4], /* I - Client address */
977 char *name
, /* I - Client hostname */
978 int name_len
, /* I - Length of hostname */
979 int num_masks
, /* I - Number of masks */
980 cupsd_authmask_t
*masks
) /* I - Masks */
982 int i
; /* Looping var */
983 cupsd_netif_t
*iface
; /* Network interface */
984 unsigned netip4
; /* IPv4 network address */
986 unsigned netip6
[4]; /* IPv6 network address */
987 #endif /* AF_INET6 */
989 while (num_masks
> 0)
993 case AUTH_INTERFACE
:
995 * Check for a match with a network interface...
998 netip4
= htonl(ip
[3]);
1001 netip6
[0] = htonl(ip
[0]);
1002 netip6
[1] = htonl(ip
[1]);
1003 netip6
[2] = htonl(ip
[2]);
1004 netip6
[3] = htonl(ip
[3]);
1005 #endif /* AF_INET6 */
1007 if (!strcmp(masks
->mask
.name
.name
, "*"))
1010 * Check against all local interfaces...
1015 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
1017 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
1020 * Only check local interfaces...
1023 if (!iface
->is_local
)
1026 if (iface
->address
.addr
.sa_family
== AF_INET
)
1029 * Check IPv4 address...
1032 if ((netip4
& iface
->mask
.ipv4
.sin_addr
.s_addr
) ==
1033 (iface
->address
.ipv4
.sin_addr
.s_addr
&
1034 iface
->mask
.ipv4
.sin_addr
.s_addr
))
1041 * Check IPv6 address...
1044 for (i
= 0; i
< 4; i
++)
1045 if ((netip6
[i
] & iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]) !=
1046 (iface
->address
.ipv6
.sin6_addr
.s6_addr32
[i
] &
1047 iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]))
1053 #endif /* AF_INET6 */
1059 * Check the named interface...
1062 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
1064 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
1066 if (strcmp(masks
->mask
.name
.name
, iface
->name
))
1069 if (iface
->address
.addr
.sa_family
== AF_INET
)
1072 * Check IPv4 address...
1075 if ((netip4
& iface
->mask
.ipv4
.sin_addr
.s_addr
) ==
1076 (iface
->address
.ipv4
.sin_addr
.s_addr
&
1077 iface
->mask
.ipv4
.sin_addr
.s_addr
))
1084 * Check IPv6 address...
1087 for (i
= 0; i
< 4; i
++)
1088 if ((netip6
[i
] & iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]) !=
1089 (iface
->address
.ipv6
.sin6_addr
.s6_addr32
[i
] &
1090 iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]))
1096 #endif /* AF_INET6 */
1103 * Check for exact name match...
1106 if (!strcasecmp(name
, masks
->mask
.name
.name
))
1110 * Check for domain match...
1113 if (name_len
>= masks
->mask
.name
.length
&&
1114 masks
->mask
.name
.name
[0] == '.' &&
1115 !strcasecmp(name
+ name_len
- masks
->mask
.name
.length
,
1116 masks
->mask
.name
.name
))
1122 * Check for IP/network address match...
1125 for (i
= 0; i
< 4; i
++)
1126 if ((ip
[i
] & masks
->mask
.ip
.netmask
[i
]) !=
1127 masks
->mask
.ip
.address
[i
])
1144 * 'cupsdCheckGroup()' - Check for a user's group membership.
1147 int /* O - 1 if user is a member, 0 otherwise */
1149 const char *username
, /* I - User name */
1150 struct passwd
*user
, /* I - System user info */
1151 const char *groupname
) /* I - Group name */
1153 int i
; /* Looping var */
1154 struct group
*group
; /* System group info */
1155 char junk
[33]; /* MD5 password (not used) */
1156 #ifdef HAVE_MBR_UID_TO_UUID
1157 uuid_t useruuid
, /* UUID for username */
1158 groupuuid
; /* UUID for groupname */
1159 int is_member
; /* True if user is a member of group */
1160 #endif /* HAVE_MBR_UID_TO_UUID */
1163 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1164 "cupsdCheckGroup(username=\"%s\", user=%p, groupname=\"%s\")",
1165 username
, user
, groupname
);
1171 if (!username
|| !groupname
)
1175 * Check to see if the user is a member of the named group...
1178 group
= getgrnam(groupname
);
1184 * Group exists, check it...
1187 for (i
= 0; group
->gr_mem
[i
]; i
++)
1188 if (!strcasecmp(username
, group
->gr_mem
[i
]))
1193 * Group doesn't exist or user not in group list, check the group ID
1194 * against the user's group ID...
1197 if (user
&& group
&& group
->gr_gid
== user
->pw_gid
)
1200 #ifdef HAVE_MBR_UID_TO_UUID
1202 * Check group membership through MacOS X membership API...
1206 if (!mbr_uid_to_uuid(user
->pw_uid
, useruuid
))
1207 if (!mbr_gid_to_uuid(group
->gr_gid
, groupuuid
))
1208 if (!mbr_check_membership(useruuid
, groupuuid
, &is_member
))
1211 #endif /* HAVE_MBR_UID_TO_UUID */
1214 * Username not found, group not found, or user is not part of the
1215 * system group... Check for a user and group in the MD5 password
1219 if (get_md5_password(username
, groupname
, junk
) != NULL
)
1223 * If we get this far, then the user isn't part of the named group...
1231 * 'cupsdCopyLocation()' - Make a copy of a location...
1234 cupsd_location_t
* /* O - New location */
1236 cupsd_location_t
**loc
) /* IO - Original location */
1238 int i
; /* Looping var */
1239 cupsd_location_t
*temp
; /* New location */
1240 char location
[HTTP_MAX_URI
];
1241 /* Location of resource */
1245 * Use a local copy of location because cupsdAddLocation may cause
1246 * this memory to be moved...
1249 strlcpy(location
, (*loc
)->location
, sizeof(location
));
1251 if ((temp
= cupsdAddLocation(location
)) == NULL
)
1255 * Copy the information from the original location to the new one.
1258 temp
->limit
= (*loc
)->limit
;
1259 temp
->order_type
= (*loc
)->order_type
;
1260 temp
->type
= (*loc
)->type
;
1261 temp
->level
= (*loc
)->level
;
1262 temp
->satisfy
= (*loc
)->satisfy
;
1263 temp
->encryption
= (*loc
)->encryption
;
1265 if ((temp
->num_names
= (*loc
)->num_names
) > 0)
1268 * Copy the names array...
1271 if ((temp
->names
= calloc(temp
->num_names
, sizeof(char *))) == NULL
)
1273 cupsdLogMessage(CUPSD_LOG_ERROR
,
1274 "cupsdCopyLocation: Unable to allocate memory for %d names: %s",
1275 temp
->num_names
, strerror(errno
));
1277 cupsdDeleteLocation(temp
);
1281 for (i
= 0; i
< temp
->num_names
; i
++)
1282 if ((temp
->names
[i
] = strdup((*loc
)->names
[i
])) == NULL
)
1284 cupsdLogMessage(CUPSD_LOG_ERROR
,
1285 "cupsdCopyLocation: Unable to copy name \"%s\": %s",
1286 (*loc
)->names
[i
], strerror(errno
));
1288 cupsdDeleteLocation(temp
);
1293 if ((temp
->num_allow
= (*loc
)->num_allow
) > 0)
1296 * Copy allow rules...
1299 if ((temp
->allow
= calloc(temp
->num_allow
, sizeof(cupsd_authmask_t
))) == NULL
)
1301 cupsdLogMessage(CUPSD_LOG_ERROR
,
1302 "cupsdCopyLocation: Unable to allocate memory for %d allow rules: %s",
1303 temp
->num_allow
, strerror(errno
));
1304 cupsdDeleteLocation(temp
);
1308 for (i
= 0; i
< temp
->num_allow
; i
++)
1309 switch (temp
->allow
[i
].type
= (*loc
)->allow
[i
].type
)
1312 temp
->allow
[i
].mask
.name
.length
= (*loc
)->allow
[i
].mask
.name
.length
;
1313 temp
->allow
[i
].mask
.name
.name
= strdup((*loc
)->allow
[i
].mask
.name
.name
);
1315 if (temp
->allow
[i
].mask
.name
.name
== NULL
)
1317 cupsdLogMessage(CUPSD_LOG_ERROR
,
1318 "cupsdCopyLocation: Unable to copy allow name \"%s\": %s",
1319 (*loc
)->allow
[i
].mask
.name
.name
, strerror(errno
));
1320 cupsdDeleteLocation(temp
);
1325 memcpy(&(temp
->allow
[i
].mask
.ip
), &((*loc
)->allow
[i
].mask
.ip
),
1326 sizeof(cupsd_ipmask_t
));
1331 if ((temp
->num_deny
= (*loc
)->num_deny
) > 0)
1334 * Copy deny rules...
1337 if ((temp
->deny
= calloc(temp
->num_deny
, sizeof(cupsd_authmask_t
))) == NULL
)
1339 cupsdLogMessage(CUPSD_LOG_ERROR
,
1340 "cupsdCopyLocation: Unable to allocate memory for %d deny rules: %s",
1341 temp
->num_deny
, strerror(errno
));
1342 cupsdDeleteLocation(temp
);
1346 for (i
= 0; i
< temp
->num_deny
; i
++)
1347 switch (temp
->deny
[i
].type
= (*loc
)->deny
[i
].type
)
1350 temp
->deny
[i
].mask
.name
.length
= (*loc
)->deny
[i
].mask
.name
.length
;
1351 temp
->deny
[i
].mask
.name
.name
= strdup((*loc
)->deny
[i
].mask
.name
.name
);
1353 if (temp
->deny
[i
].mask
.name
.name
== NULL
)
1355 cupsdLogMessage(CUPSD_LOG_ERROR
,
1356 "cupsdCopyLocation: Unable to copy deny name \"%s\": %s",
1357 (*loc
)->deny
[i
].mask
.name
.name
, strerror(errno
));
1358 cupsdDeleteLocation(temp
);
1363 memcpy(&(temp
->deny
[i
].mask
.ip
), &((*loc
)->deny
[i
].mask
.ip
),
1364 sizeof(cupsd_ipmask_t
));
1374 * 'cupsdDeleteAllLocations()' - Free all memory used for location authorization.
1378 cupsdDeleteAllLocations(void)
1380 cupsd_location_t
*loc
; /* Current location */
1384 * Free all of the allow/deny records first...
1387 for (loc
= (cupsd_location_t
*)cupsArrayFirst(Locations
);
1389 loc
= (cupsd_location_t
*)cupsArrayNext(Locations
))
1390 cupsdDeleteLocation(loc
);
1393 * Then free the location array...
1396 cupsArrayDelete(Locations
);
1402 * 'cupsdDeleteLocation()' - Free all memory used by a location.
1406 cupsdDeleteLocation(
1407 cupsd_location_t
*loc
) /* I - Location to delete */
1409 int i
; /* Looping var */
1410 cupsd_authmask_t
*mask
; /* Current mask */
1413 cupsArrayRemove(Locations
, loc
);
1415 for (i
= loc
->num_names
- 1; i
>= 0; i
--)
1416 free(loc
->names
[i
]);
1418 if (loc
->num_names
> 0)
1421 for (i
= loc
->num_allow
, mask
= loc
->allow
; i
> 0; i
--, mask
++)
1422 if (mask
->type
== AUTH_NAME
|| mask
->type
== AUTH_INTERFACE
)
1423 free(mask
->mask
.name
.name
);
1425 if (loc
->num_allow
> 0)
1428 for (i
= loc
->num_deny
, mask
= loc
->deny
; i
> 0; i
--, mask
++)
1429 if (mask
->type
== AUTH_NAME
|| mask
->type
== AUTH_INTERFACE
)
1430 free(mask
->mask
.name
.name
);
1432 if (loc
->num_deny
> 0)
1435 free(loc
->location
);
1441 * 'cupsdDenyHost()' - Add a host name that is not allowed to access the
1446 cupsdDenyHost(cupsd_location_t
*loc
, /* I - Location to add to */
1447 char *name
) /* I - Name of host or domain to add */
1449 cupsd_authmask_t
*temp
; /* New host/domain mask */
1450 char ifname
[32], /* Interface name */
1451 *ifptr
; /* Pointer to end of name */
1454 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdDenyHost(loc=%p(%s), name=\"%s\")",
1455 loc
, loc
->location
? loc
->location
: "nil", name
);
1457 if ((temp
= add_deny(loc
)) == NULL
)
1460 if (!strcasecmp(name
, "@LOCAL"))
1463 * Deny *interface*...
1466 temp
->type
= AUTH_INTERFACE
;
1467 temp
->mask
.name
.name
= strdup("*");
1468 temp
->mask
.name
.length
= 1;
1470 else if (!strncasecmp(name
, "@IF(", 4))
1473 * Deny *interface*...
1476 strlcpy(ifname
, name
+ 4, sizeof(ifname
));
1478 ifptr
= ifname
+ strlen(ifname
);
1480 if (ifptr
[-1] == ')')
1486 temp
->type
= AUTH_INTERFACE
;
1487 temp
->mask
.name
.name
= strdup(ifname
);
1488 temp
->mask
.name
.length
= ifptr
- ifname
;
1496 temp
->type
= AUTH_NAME
;
1497 temp
->mask
.name
.name
= strdup(name
);
1498 temp
->mask
.name
.length
= strlen(name
);
1504 * 'cupsdDenyIP()' - Add an IP address or network that is not allowed to
1505 * access the location.
1509 cupsdDenyIP(cupsd_location_t
*loc
, /* I - Location to add to */
1510 unsigned address
[4],/* I - IP address to add */
1511 unsigned netmask
[4])/* I - Netmask of address */
1513 cupsd_authmask_t
*temp
; /* New host/domain mask */
1516 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1517 "cupsdDenyIP(loc=%p(%s), address=%x:%x:%x:%x, netmask=%x:%x:%x:%x)",
1518 loc
, loc
->location
? loc
->location
: "nil",
1519 address
[0], address
[1], address
[2], address
[3],
1520 netmask
[0], netmask
[1], netmask
[2], netmask
[3]);
1522 if ((temp
= add_deny(loc
)) == NULL
)
1525 temp
->type
= AUTH_IP
;
1526 memcpy(temp
->mask
.ip
.address
, address
, sizeof(temp
->mask
.ip
.address
));
1527 memcpy(temp
->mask
.ip
.netmask
, netmask
, sizeof(temp
->mask
.ip
.netmask
));
1532 * 'cupsdFindBest()' - Find the location entry that best matches the resource.
1535 cupsd_location_t
* /* O - Location that matches */
1536 cupsdFindBest(const char *path
, /* I - Resource path */
1537 http_state_t state
) /* I - HTTP state/request */
1539 char uri
[HTTP_MAX_URI
],
1540 /* URI in request... */
1541 *uriptr
; /* Pointer into URI */
1542 cupsd_location_t
*loc
, /* Current location */
1543 *best
; /* Best match for location so far */
1544 int bestlen
; /* Length of best match */
1545 int limit
; /* Limit field */
1546 static const int limits
[] = /* Map http_status_t to AUTH_LIMIT_xyz */
1566 * First copy the connection URI to a local string so we have drop
1567 * any .ppd extension from the pathname in /printers or /classes
1571 strlcpy(uri
, path
, sizeof(uri
));
1573 if (!strncmp(uri
, "/printers/", 10) ||
1574 !strncmp(uri
, "/classes/", 9))
1577 * Check if the URI has .ppd on the end...
1580 uriptr
= uri
+ strlen(uri
) - 4; /* len > 4 if we get here... */
1582 if (!strcmp(uriptr
, ".ppd"))
1586 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: uri = \"%s\"...", uri
);
1589 * Loop through the list of locations to find a match...
1592 limit
= limits
[state
];
1596 for (loc
= (cupsd_location_t
*)cupsArrayFirst(Locations
);
1598 loc
= (cupsd_location_t
*)cupsArrayNext(Locations
))
1600 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: Location %s Limit %x",
1601 loc
->location
? loc
->location
: "nil", loc
->limit
);
1603 if (!strncmp(uri
, "/printers/", 10) || !strncmp(uri
, "/classes/", 9))
1606 * Use case-insensitive comparison for queue names...
1609 if (loc
->length
> bestlen
&& loc
->location
&&
1610 !strncasecmp(uri
, loc
->location
, loc
->length
) &&
1611 loc
->location
[0] == '/' &&
1612 (limit
& loc
->limit
) != 0)
1615 bestlen
= loc
->length
;
1621 * Use case-sensitive comparison for other URIs...
1624 if (loc
->length
> bestlen
&& loc
->location
&&
1625 !strncmp(uri
, loc
->location
, loc
->length
) &&
1626 loc
->location
[0] == '/' &&
1627 (limit
& loc
->limit
) != 0)
1630 bestlen
= loc
->length
;
1636 * Return the match, if any...
1639 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: best = %s",
1640 best
? best
->location
: "NONE");
1647 * 'cupsdFindLocation()' - Find the named location.
1650 cupsd_location_t
* /* O - Location that matches */
1651 cupsdFindLocation(const char *location
) /* I - Connection */
1653 cupsd_location_t key
; /* Search key */
1656 key
.location
= (char *)location
;
1658 return ((cupsd_location_t
*)cupsArrayFind(Locations
, &key
));
1663 * 'cupsdIsAuthorized()' - Check to see if the user is authorized...
1666 http_status_t
/* O - HTTP_OK if authorized or error code */
1667 cupsdIsAuthorized(cupsd_client_t
*con
, /* I - Connection */
1668 const char *owner
)/* I - Owner of object */
1670 int i
, j
, /* Looping vars */
1671 auth
; /* Authorization status */
1672 unsigned address
[4]; /* Authorization address */
1673 cupsd_location_t
*best
; /* Best match for location so far */
1674 int hostlen
; /* Length of hostname */
1675 const char *username
; /* Username to authorize */
1676 struct passwd
*pw
; /* User password data */
1677 static const char * const levels
[] = /* Auth levels */
1683 static const char * const types
[] = /* Auth types */
1693 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1694 "cupsdIsAuthorized: con->uri=\"%s\", con->best=%p(%s)",
1695 con
->uri
, con
->best
, con
->best
? con
->best
->location
?
1696 con
->best
->location
: "(null)" : "");
1698 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1699 "cupsdIsAuthorized: owner=\"%s\"", owner
);
1702 * If there is no "best" authentication rule for this request, then
1703 * access is allowed from the local system and denied from other
1709 if (!strcmp(con
->http
.hostname
, "localhost") ||
1710 !strcmp(con
->http
.hostname
, ServerName
))
1713 return (HTTP_FORBIDDEN
);
1718 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1719 "cupsdIsAuthorized: level=AUTH_%s, type=AUTH_%s, "
1720 "satisfy=AUTH_SATISFY_%s, num_names=%d",
1721 levels
[best
->level
], types
[best
->type
],
1722 best
->satisfy
? "ANY" : "ALL", best
->num_names
);
1724 if (best
->limit
== AUTH_LIMIT_IPP
)
1725 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: op=%x(%s)",
1726 best
->op
, ippOpString(best
->op
));
1729 * Check host/ip-based accesses...
1733 if (con
->http
.hostaddr
->addr
.sa_family
== AF_INET6
)
1736 * Copy IPv6 address...
1739 address
[0] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[0]);
1740 address
[1] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[1]);
1741 address
[2] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[2]);
1742 address
[3] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[3]);
1745 #endif /* AF_INET6 */
1746 if (con
->http
.hostaddr
->addr
.sa_family
== AF_INET
)
1749 * Copy IPv4 address...
1755 address
[3] = ntohl(con
->http
.hostaddr
->ipv4
.sin_addr
.s_addr
);
1758 memset(address
, 0, sizeof(address
));
1760 hostlen
= strlen(con
->http
.hostname
);
1762 if (!strcasecmp(con
->http
.hostname
, "localhost"))
1765 * Access from localhost (127.0.0.1 or ::1) is always allowed...
1773 * Do authorization checks on the domain/address...
1776 switch (best
->order_type
)
1779 auth
= AUTH_DENY
; /* anti-compiler-warning-code */
1782 case AUTH_ALLOW
: /* Order Deny,Allow */
1785 if (cupsdCheckAuth(address
, con
->http
.hostname
, hostlen
,
1786 best
->num_deny
, best
->deny
))
1789 if (cupsdCheckAuth(address
, con
->http
.hostname
, hostlen
,
1790 best
->num_allow
, best
->allow
))
1794 case AUTH_DENY
: /* Order Allow,Deny */
1797 if (cupsdCheckAuth(address
, con
->http
.hostname
, hostlen
,
1798 best
->num_allow
, best
->allow
))
1801 if (cupsdCheckAuth(address
, con
->http
.hostname
, hostlen
,
1802 best
->num_deny
, best
->deny
))
1808 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: auth=AUTH_%s...",
1809 auth
? "DENY" : "ALLOW");
1811 if (auth
== AUTH_DENY
&& best
->satisfy
== AUTH_SATISFY_ALL
)
1812 return (HTTP_FORBIDDEN
);
1816 * See if encryption is required...
1819 if ((best
->encryption
>= HTTP_ENCRYPT_REQUIRED
&& !con
->http
.tls
&&
1820 strcasecmp(con
->http
.hostname
, "localhost") &&
1821 best
->satisfy
== AUTH_SATISFY_ALL
) &&
1822 !(best
->type
== AUTH_NEGOTIATE
||
1823 (best
->type
== AUTH_NONE
&& DefaultAuthType
== AUTH_NEGOTIATE
)))
1825 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1826 "cupsdIsAuthorized: Need upgrade to TLS...");
1827 return (HTTP_UPGRADE_REQUIRED
);
1829 #endif /* HAVE_SSL */
1832 * Now see what access level is required...
1835 if (best
->level
== AUTH_ANON
|| /* Anonymous access - allow it */
1836 (best
->type
== AUTH_NONE
&& best
->num_names
== 0))
1839 if (!con
->username
[0] && best
->type
== AUTH_NONE
&&
1840 best
->limit
== AUTH_LIMIT_IPP
)
1843 * Check for unauthenticated username...
1846 ipp_attribute_t
*attr
; /* requesting-user-name attribute */
1849 attr
= ippFindAttribute(con
->request
, "requesting-user-name", IPP_TAG_NAME
);
1852 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1853 "cupsdIsAuthorized: requesting-user-name=\"%s\"",
1854 attr
->values
[0].string
.text
);
1855 username
= attr
->values
[0].string
.text
;
1857 else if (best
->satisfy
== AUTH_SATISFY_ALL
|| auth
== AUTH_DENY
)
1858 return (HTTP_UNAUTHORIZED
); /* Non-anonymous needs user/pass */
1860 return (HTTP_OK
); /* unless overridden with Satisfy */
1864 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: username=\"%s\"",
1867 #ifdef HAVE_AUTHORIZATION_H
1868 if (!con
->username
[0] && !con
->authref
)
1870 if (!con
->username
[0])
1871 #endif /* HAVE_AUTHORIZATION_H */
1873 if (best
->satisfy
== AUTH_SATISFY_ALL
|| auth
== AUTH_DENY
)
1874 return (HTTP_UNAUTHORIZED
); /* Non-anonymous needs user/pass */
1876 return (HTTP_OK
); /* unless overridden with Satisfy */
1879 username
= con
->username
;
1883 * OK, got a username. See if we need normal user access, or group
1884 * access... (root always matches)
1887 if (!strcmp(username
, "root"))
1891 * Get the user info...
1896 pw
= getpwnam(username
);
1902 if (best
->level
== AUTH_USER
)
1905 * If there are no names associated with this location, then
1906 * any valid user is OK...
1909 if (best
->num_names
== 0)
1913 * Otherwise check the user list and return OK if this user is
1917 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1918 "cupsdIsAuthorized: Checking user membership...");
1920 #ifdef HAVE_AUTHORIZATION_H
1922 * If an authorization reference was supplied it must match a right name...
1927 for (i
= 0; i
< best
->num_names
; i
++)
1929 if (!strncasecmp(best
->names
[i
], "@AUTHKEY(", 9) &&
1930 check_authref(con
, best
->names
[i
] + 9))
1932 else if (!strcasecmp(best
->names
[i
], "@SYSTEM") &&
1933 SystemGroupAuthKey
&&
1934 check_authref(con
, SystemGroupAuthKey
))
1938 return (HTTP_UNAUTHORIZED
);
1940 #endif /* HAVE_AUTHORIZATION_H */
1942 for (i
= 0; i
< best
->num_names
; i
++)
1944 if (!strcasecmp(best
->names
[i
], "@OWNER") && owner
&&
1945 !strcasecmp(username
, owner
))
1947 else if (!strcasecmp(best
->names
[i
], "@SYSTEM"))
1949 for (j
= 0; j
< NumSystemGroups
; j
++)
1950 if (cupsdCheckGroup(username
, pw
, SystemGroups
[j
]))
1953 else if (best
->names
[i
][0] == '@')
1955 if (cupsdCheckGroup(username
, pw
, best
->names
[i
] + 1))
1958 else if (!strcasecmp(username
, best
->names
[i
]))
1962 return (HTTP_UNAUTHORIZED
);
1966 * Check to see if this user is in any of the named groups...
1969 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1970 "cupsdIsAuthorized: Checking group membership...");
1973 * Check to see if this user is in any of the named groups...
1976 for (i
= 0; i
< best
->num_names
; i
++)
1978 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1979 "cupsdIsAuthorized: Checking group \"%s\" membership...",
1982 if (!strcasecmp(best
->names
[i
], "@SYSTEM"))
1984 for (j
= 0; j
< NumSystemGroups
; j
++)
1985 if (cupsdCheckGroup(username
, pw
, SystemGroups
[j
]))
1988 else if (cupsdCheckGroup(username
, pw
, best
->names
[i
]))
1993 * The user isn't part of the specified group, so deny access...
1996 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1997 "cupsdIsAuthorized: User not in group(s)!");
1999 return (HTTP_UNAUTHORIZED
);
2004 * 'add_allow()' - Add an allow mask to the location.
2007 static cupsd_authmask_t
* /* O - New mask record */
2008 add_allow(cupsd_location_t
*loc
) /* I - Location to add to */
2010 cupsd_authmask_t
*temp
; /* New mask record */
2021 * Try to allocate memory for the record...
2024 if (loc
->num_allow
== 0)
2025 temp
= malloc(sizeof(cupsd_authmask_t
));
2027 temp
= realloc(loc
->allow
, sizeof(cupsd_authmask_t
) * (loc
->num_allow
+ 1));
2033 temp
+= loc
->num_allow
;
2037 * Clear the mask record and return...
2040 memset(temp
, 0, sizeof(cupsd_authmask_t
));
2046 * 'add_deny()' - Add a deny mask to the location.
2049 static cupsd_authmask_t
* /* O - New mask record */
2050 add_deny(cupsd_location_t
*loc
) /* I - Location to add to */
2052 cupsd_authmask_t
*temp
; /* New mask record */
2063 * Try to allocate memory for the record...
2066 if (loc
->num_deny
== 0)
2067 temp
= malloc(sizeof(cupsd_authmask_t
));
2069 temp
= realloc(loc
->deny
, sizeof(cupsd_authmask_t
) * (loc
->num_deny
+ 1));
2075 temp
+= loc
->num_deny
;
2079 * Clear the mask record and return...
2082 memset(temp
, 0, sizeof(cupsd_authmask_t
));
2087 #ifdef HAVE_AUTHORIZATION_H
2089 * 'check_authref()' - Check if an authorization services reference has the
2093 static int /* O - 1 if right is valid, 0 otherwise */
2094 check_authref(cupsd_client_t
*con
, /* I - Connection */
2095 const char *right
) /* I - Right name */
2097 OSStatus status
; /* OS Status */
2098 AuthorizationItem authright
; /* Authorization right */
2099 AuthorizationRights authrights
; /* Authorization rights */
2100 AuthorizationFlags authflags
; /* Authorization flags */
2104 * Check to see if the user is allowed to perform the task...
2110 authright
.name
= right
;
2111 authright
.valueLength
= 0;
2112 authright
.value
= NULL
;
2113 authright
.flags
= 0;
2115 authrights
.count
= 1;
2116 authrights
.items
= &authright
;
2118 authflags
= kAuthorizationFlagDefaults
|
2119 kAuthorizationFlagExtendRights
;
2121 if ((status
= AuthorizationCopyRights(con
->authref
, &authrights
,
2122 kAuthorizationEmptyEnvironment
,
2123 authflags
, NULL
)) != 0)
2125 cupsdLogMessage(CUPSD_LOG_ERROR
,
2126 "AuthorizationCopyRights(\"%s\") returned %d (%s)",
2127 authright
.name
, (int)status
, cssmErrorString(status
));
2131 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2132 "AuthorizationCopyRights(\"%s\") succeeded!",
2137 #endif /* HAVE_AUTHORIZATION_H */
2141 * 'compare_locations()' - Compare two locations.
2144 static int /* O - Result of comparison */
2145 compare_locations(cupsd_location_t
*a
, /* I - First location */
2146 cupsd_location_t
*b
) /* I - Second location */
2148 return (strcmp(b
->location
, a
->location
));
2152 #if !HAVE_LIBPAM && !defined(HAVE_USERSEC_H)
2154 * 'cups_crypt()' - Encrypt the password using the DES or MD5 algorithms,
2158 static char * /* O - Encrypted password */
2159 cups_crypt(const char *pw
, /* I - Password string */
2160 const char *salt
) /* I - Salt (key) string */
2162 if (!strncmp(salt
, "$1$", 3))
2165 * Use MD5 passwords without the benefit of PAM; this is for
2166 * Slackware Linux, and the algorithm was taken from the
2167 * old shadow-19990827/lib/md5crypt.c source code... :(
2170 int i
; /* Looping var */
2171 unsigned long n
; /* Output number */
2172 int pwlen
; /* Length of password string */
2173 const char *salt_end
; /* End of "salt" data for MD5 */
2174 char *ptr
; /* Pointer into result string */
2175 _cups_md5_state_t state
; /* Primary MD5 state info */
2176 _cups_md5_state_t state2
; /* Secondary MD5 state info */
2177 unsigned char digest
[16]; /* MD5 digest result */
2178 static char result
[120]; /* Final password string */
2182 * Get the salt data between dollar signs, e.g. $1$saltdata$md5.
2183 * Get a maximum of 8 characters of salt data after $1$...
2186 for (salt_end
= salt
+ 3; *salt_end
&& (salt_end
- salt
) < 11; salt_end
++)
2187 if (*salt_end
== '$')
2191 * Compute the MD5 sum we need...
2196 _cupsMD5Init(&state
);
2197 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
2198 _cupsMD5Append(&state
, (unsigned char *)salt
, salt_end
- salt
);
2200 _cupsMD5Init(&state2
);
2201 _cupsMD5Append(&state2
, (unsigned char *)pw
, pwlen
);
2202 _cupsMD5Append(&state2
, (unsigned char *)salt
+ 3, salt_end
- salt
- 3);
2203 _cupsMD5Append(&state2
, (unsigned char *)pw
, pwlen
);
2204 _cupsMD5Finish(&state2
, digest
);
2206 for (i
= pwlen
; i
> 0; i
-= 16)
2207 _cupsMD5Append(&state
, digest
, i
> 16 ? 16 : i
);
2209 for (i
= pwlen
; i
> 0; i
>>= 1)
2210 _cupsMD5Append(&state
, (unsigned char *)((i
& 1) ? "" : pw
), 1);
2212 _cupsMD5Finish(&state
, digest
);
2214 for (i
= 0; i
< 1000; i
++)
2216 _cupsMD5Init(&state
);
2219 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
2221 _cupsMD5Append(&state
, digest
, 16);
2224 _cupsMD5Append(&state
, (unsigned char *)salt
+ 3, salt_end
- salt
- 3);
2227 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
2230 _cupsMD5Append(&state
, digest
, 16);
2232 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
2234 _cupsMD5Finish(&state
, digest
);
2238 * Copy the final sum to the result string and return...
2241 memcpy(result
, salt
, salt_end
- salt
);
2242 ptr
= result
+ (salt_end
- salt
);
2245 for (i
= 0; i
< 5; i
++, ptr
+= 4)
2247 n
= (((digest
[i
] << 8) | digest
[i
+ 6]) << 8);
2250 n
|= digest
[i
+ 12];
2257 to64(ptr
, digest
[11], 2);
2266 * Use the standard crypt() function...
2269 return (crypt(pw
, salt
));
2272 #endif /* !HAVE_LIBPAM && !HAVE_USERSEC_H */
2277 * 'get_gss_creds()' - Obtain GSS credentials.
2280 static gss_cred_id_t
/* O - Server credentials */
2281 get_gss_creds(const char *service_name
) /* I - Service name */
2283 OM_uint32 major_status
, /* Major status code */
2284 minor_status
; /* Minor status code */
2285 gss_name_t server_name
; /* Server name */
2286 gss_cred_id_t server_creds
; /* Server credentials */
2287 gss_buffer_desc token
= GSS_C_EMPTY_BUFFER
;
2288 /* Service name token */
2289 char buf
[1024], /* Service name buffer */
2290 fqdn
[HTTP_MAX_URI
]; /* Hostname of server */
2293 snprintf(buf
, sizeof(buf
), "%s@%s", service_name
,
2294 httpGetHostname(NULL
, fqdn
, sizeof(fqdn
)));
2297 token
.length
= strlen(buf
);
2298 server_name
= GSS_C_NO_NAME
;
2299 major_status
= gss_import_name(&minor_status
, &token
,
2300 GSS_C_NT_HOSTBASED_SERVICE
,
2303 memset(&token
, 0, sizeof(token
));
2305 if (GSS_ERROR(major_status
))
2307 cupsdLogGSSMessage(CUPSD_LOG_WARN
, major_status
, minor_status
,
2308 "gss_import_name() failed");
2312 major_status
= gss_display_name(&minor_status
, server_name
, &token
, NULL
);
2314 if (GSS_ERROR(major_status
))
2316 cupsdLogGSSMessage(CUPSD_LOG_WARN
, major_status
, minor_status
,
2317 "gss_display_name() failed");
2321 cupsdLogMessage(CUPSD_LOG_INFO
, "Attempting to acquire credentials for %s...",
2322 (char *)token
.value
);
2324 server_creds
= GSS_C_NO_CREDENTIAL
;
2325 major_status
= gss_acquire_cred(&minor_status
, server_name
, GSS_C_INDEFINITE
,
2326 GSS_C_NO_OID_SET
, GSS_C_ACCEPT
,
2327 &server_creds
, NULL
, NULL
);
2328 if (GSS_ERROR(major_status
))
2330 cupsdLogGSSMessage(CUPSD_LOG_WARN
, major_status
, minor_status
,
2331 "gss_acquire_cred() failed");
2332 gss_release_name(&minor_status
, &server_name
);
2333 gss_release_buffer(&minor_status
, &token
);
2337 cupsdLogMessage(CUPSD_LOG_INFO
, "Credentials acquired successfully for %s.",
2338 (char *)token
.value
);
2340 gss_release_name(&minor_status
, &server_name
);
2341 gss_release_buffer(&minor_status
, &token
);
2343 return (server_creds
);
2345 #endif /* HAVE_GSSAPI */
2349 * 'get_md5_password()' - Get an MD5 password.
2352 static char * /* O - MD5 password string */
2353 get_md5_password(const char *username
, /* I - Username */
2354 const char *group
, /* I - Group */
2355 char passwd
[33]) /* O - MD5 password string */
2357 cups_file_t
*fp
; /* passwd.md5 file */
2358 char filename
[1024], /* passwd.md5 filename */
2359 line
[256], /* Line from file */
2360 tempuser
[33], /* User from file */
2361 tempgroup
[33]; /* Group from file */
2364 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2365 "get_md5_password(username=\"%s\", group=\"%s\", passwd=%p)",
2366 username
, group
? group
: "(null)", passwd
);
2368 snprintf(filename
, sizeof(filename
), "%s/passwd.md5", ServerRoot
);
2369 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
2371 if (errno
!= ENOENT
)
2372 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open %s - %s", filename
,
2378 while (cupsFileGets(fp
, line
, sizeof(line
)) != NULL
)
2380 if (sscanf(line
, "%32[^:]:%32[^:]:%32s", tempuser
, tempgroup
, passwd
) != 3)
2382 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad MD5 password line: %s", line
);
2386 if (!strcmp(username
, tempuser
) &&
2387 (group
== NULL
|| !strcmp(group
, tempgroup
)))
2390 * Found the password entry!
2393 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Found MD5 user %s, group %s...",
2394 username
, tempgroup
);
2402 * Didn't find a password entry - return NULL!
2412 * 'pam_func()' - PAM conversation function.
2415 static int /* O - Success or failure */
2417 int num_msg
, /* I - Number of messages */
2418 const struct pam_message
**msg
, /* I - Messages */
2419 struct pam_response
**resp
, /* O - Responses */
2421 /* I - Pointer to connection */
2423 int i
; /* Looping var */
2424 struct pam_response
*replies
; /* Replies */
2425 cupsd_authdata_t
*data
; /* Pointer to auth data */
2429 * Allocate memory for the responses...
2432 if ((replies
= malloc(sizeof(struct pam_response
) * num_msg
)) == NULL
)
2433 return (PAM_CONV_ERR
);
2436 * Answer all of the messages...
2439 DEBUG_printf(("pam_func: appdata_ptr = %p\n", appdata_ptr
));
2443 * Apparently some versions of HP-UX 11 have a broken pam_unix security
2444 * module. This is a workaround...
2450 data
= (cupsd_authdata_t
*)appdata_ptr
;
2453 for (i
= 0; i
< num_msg
; i
++)
2455 DEBUG_printf(("pam_func: Message = \"%s\"\n", msg
[i
]->msg
));
2457 switch (msg
[i
]->msg_style
)
2459 case PAM_PROMPT_ECHO_ON
:
2460 DEBUG_printf(("pam_func: PAM_PROMPT_ECHO_ON, returning \"%s\"...\n",
2462 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2463 replies
[i
].resp
= strdup(data
->username
);
2466 case PAM_PROMPT_ECHO_OFF
:
2467 DEBUG_printf(("pam_func: PAM_PROMPT_ECHO_OFF, returning \"%s\"...\n",
2469 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2470 replies
[i
].resp
= strdup(data
->password
);
2474 DEBUG_puts("pam_func: PAM_TEXT_INFO...");
2475 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2476 replies
[i
].resp
= NULL
;
2480 DEBUG_puts("pam_func: PAM_ERROR_MSG...");
2481 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2482 replies
[i
].resp
= NULL
;
2486 DEBUG_printf(("pam_func: Unknown PAM message %d...\n",
2487 msg
[i
]->msg_style
));
2489 return (PAM_CONV_ERR
);
2494 * Return the responses back to PAM...
2499 return (PAM_SUCCESS
);
2501 #elif !defined(HAVE_USERSEC_H)
2505 * 'to64()' - Base64-encode an integer value...
2509 to64(char *s
, /* O - Output string */
2510 unsigned long v
, /* I - Value to encode */
2511 int n
) /* I - Number of digits */
2513 const char *itoa64
= "./0123456789"
2514 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2515 "abcdefghijklmnopqrstuvwxyz";
2518 for (; n
> 0; n
--, v
>>= 6)
2519 *s
++ = itoa64
[v
& 0x3f];
2521 #endif /* HAVE_LIBPAM */
2525 * End of "$Id: auth.c 6361 2007-03-19 16:01:28Z mike $".