4 * Authorization routines for the CUPS scheduler.
6 * Copyright 2007-2015 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 * Include necessary headers...
27 #endif /* HAVE_SHADOW_H */
30 #endif /* HAVE_CRYPT_H */
32 # ifdef HAVE_PAM_PAM_APPL_H
33 # include <pam/pam_appl.h>
35 # include <security/pam_appl.h>
36 # endif /* HAVE_PAM_PAM_APPL_H */
37 #endif /* HAVE_LIBPAM */
38 #ifdef HAVE_MEMBERSHIP_H
39 # include <membership.h>
40 #endif /* HAVE_MEMBERSHIP_H */
41 #ifdef HAVE_AUTHORIZATION_H
42 # include <Security/AuthorizationTags.h>
43 # ifdef HAVE_SECBASEPRIV_H
44 # include <Security/SecBasePriv.h>
46 extern const char *cssmErrorString(int error
);
47 # endif /* HAVE_SECBASEPRIV_H */
48 #endif /* HAVE_AUTHORIZATION_H */
49 #ifdef HAVE_SYS_PARAM_H
50 # include <sys/param.h>
51 #endif /* HAVE_SYS_PARAM_H */
52 #ifdef HAVE_SYS_UCRED_H
53 # include <sys/ucred.h>
54 typedef struct xucred cupsd_ucred_t
;
55 # define CUPSD_UCRED_UID(c) (c).cr_uid
58 typedef struct ucred cupsd_ucred_t
;
60 typedef struct sockpeercred cupsd_ucred_t
;
62 # define CUPSD_UCRED_UID(c) (c).uid
63 #endif /* HAVE_SYS_UCRED_H */
70 #ifdef HAVE_AUTHORIZATION_H
71 static int check_authref(cupsd_client_t
*con
, const char *right
);
72 #endif /* HAVE_AUTHORIZATION_H */
73 static int compare_locations(cupsd_location_t
*a
,
75 static cupsd_authmask_t
*copy_authmask(cupsd_authmask_t
*am
, void *data
);
77 static char *cups_crypt(const char *pw
, const char *salt
);
78 #endif /* !HAVE_LIBPAM */
79 static void free_authmask(cupsd_authmask_t
*am
, void *data
);
81 static int pam_func(int, const struct pam_message
**,
82 struct pam_response
**, void *);
84 static void to64(char *s
, unsigned long v
, int n
);
85 #endif /* HAVE_LIBPAM */
93 typedef struct cupsd_authdata_s
/**** Authentication data ****/
95 char username
[HTTP_MAX_VALUE
], /* Username string */
96 password
[HTTP_MAX_VALUE
]; /* Password string */
98 #endif /* HAVE_LIBPAM */
102 * 'cupsdAddIPMask()' - Add an IP address authorization mask.
105 int /* O - 1 on success, 0 on failure */
107 cups_array_t
**masks
, /* IO - Masks array (created as needed) */
108 const unsigned address
[4], /* I - IP address */
109 const unsigned netmask
[4]) /* I - IP netmask */
111 cupsd_authmask_t temp
; /* New host/domain mask */
114 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAddIPMask(masks=%p(%p), address=%x:%x:%x:%x, netmask=%x:%x:%x:%x)", masks
, *masks
, address
[0], address
[1], address
[2], address
[3], netmask
[0], netmask
[1], netmask
[2], netmask
[3]);
116 temp
.type
= CUPSD_AUTH_IP
;
117 memcpy(temp
.mask
.ip
.address
, address
, sizeof(temp
.mask
.ip
.address
));
118 memcpy(temp
.mask
.ip
.netmask
, netmask
, sizeof(temp
.mask
.ip
.netmask
));
121 * Create the masks array as needed and add...
125 *masks
= cupsArrayNew3(NULL
, NULL
, NULL
, 0,
126 (cups_acopy_func_t
)copy_authmask
,
127 (cups_afree_func_t
)free_authmask
);
129 return (cupsArrayAdd(*masks
, &temp
));
134 * 'cupsdAddLocation()' - Add a location for authorization.
138 cupsdAddLocation(cupsd_location_t
*loc
) /* I - Location to add */
141 * Make sure the locations array is created...
145 Locations
= cupsArrayNew3((cups_array_func_t
)compare_locations
, NULL
,
146 (cups_ahash_func_t
)NULL
, 0,
147 (cups_acopy_func_t
)NULL
,
148 (cups_afree_func_t
)cupsdFreeLocation
);
152 cupsArrayAdd(Locations
, loc
);
154 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAddLocation: Added location \"%s\"", loc
->location
? loc
->location
: "(null)");
160 * 'cupsdAddName()' - Add a name to a location...
164 cupsdAddName(cupsd_location_t
*loc
, /* I - Location to add to */
165 char *name
) /* I - Name to add */
167 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAddName(loc=%p, name=\"%s\")", loc
, name
);
170 loc
->names
= cupsArrayNew3(NULL
, NULL
, NULL
, 0,
171 (cups_acopy_func_t
)_cupsStrAlloc
,
172 (cups_afree_func_t
)_cupsStrFree
);
174 if (!cupsArrayAdd(loc
->names
, name
))
176 cupsdLogMessage(CUPSD_LOG_ERROR
,
177 "Unable to duplicate name for location %s: %s",
178 loc
->location
? loc
->location
: "nil", strerror(errno
));
185 * 'cupsdAddNameMask()' - Add a host or interface name authorization mask.
188 int /* O - 1 on success, 0 on failure */
189 cupsdAddNameMask(cups_array_t
**masks
, /* IO - Masks array (created as needed) */
190 char *name
) /* I - Host or interface name */
192 cupsd_authmask_t temp
; /* New host/domain mask */
193 char ifname
[32], /* Interface name */
194 *ifptr
; /* Pointer to end of name */
197 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAddNameMask(masks=%p(%p), name=\"%s\")", masks
, *masks
, name
);
199 if (!_cups_strcasecmp(name
, "@LOCAL"))
202 * Deny *interface*...
205 temp
.type
= CUPSD_AUTH_INTERFACE
;
206 temp
.mask
.name
.name
= (char *)"*";
208 else if (!_cups_strncasecmp(name
, "@IF(", 4))
211 * Deny *interface*...
214 strlcpy(ifname
, name
+ 4, sizeof(ifname
));
216 ifptr
= ifname
+ strlen(ifname
) - 1;
218 if (ifptr
>= ifname
&& *ifptr
== ')')
224 temp
.type
= CUPSD_AUTH_INTERFACE
;
225 temp
.mask
.name
.name
= ifname
;
236 temp
.type
= CUPSD_AUTH_NAME
;
237 temp
.mask
.name
.name
= (char *)name
;
241 * Set the name length...
244 temp
.mask
.name
.length
= strlen(temp
.mask
.name
.name
);
247 * Create the masks array as needed and add...
251 *masks
= cupsArrayNew3(NULL
, NULL
, NULL
, 0,
252 (cups_acopy_func_t
)copy_authmask
,
253 (cups_afree_func_t
)free_authmask
);
255 return (cupsArrayAdd(*masks
, &temp
));
260 * 'cupsdAuthorize()' - Validate any authorization credentials.
264 cupsdAuthorize(cupsd_client_t
*con
) /* I - Client connection */
266 int type
; /* Authentication type */
267 const char *authorization
; /* Pointer into Authorization string */
268 char *ptr
, /* Pointer into string */
269 username
[HTTP_MAX_VALUE
],
270 /* Username string */
271 password
[HTTP_MAX_VALUE
];
272 /* Password string */
273 cupsd_cert_t
*localuser
; /* Certificate username */
277 * Locate the best matching location so we know what kind of
278 * authentication to expect...
281 con
->best
= cupsdFindBest(con
->uri
, httpGetState(con
->http
));
282 con
->type
= CUPSD_AUTH_NONE
;
284 cupsdLogClient(con
, CUPSD_LOG_DEBUG2
, "con->uri=\"%s\", con->best=%p(%s)", con
->uri
, con
->best
, con
->best
? con
->best
->location
: "");
286 if (con
->best
&& con
->best
->type
!= CUPSD_AUTH_NONE
)
288 if (con
->best
->type
== CUPSD_AUTH_DEFAULT
)
289 type
= cupsdDefaultAuthType();
291 type
= con
->best
->type
;
294 type
= cupsdDefaultAuthType();
297 * Decode the Authorization string...
300 authorization
= httpGetField(con
->http
, HTTP_FIELD_AUTHORIZATION
);
307 #endif /* HAVE_GSSAPI */
309 #ifdef HAVE_AUTHORIZATION_H
312 AuthorizationFree(con
->authref
, kAuthorizationFlagDefaults
);
315 #endif /* HAVE_AUTHORIZATION_H */
320 * No authorization data provided, return early...
323 cupsdLogClient(con
, CUPSD_LOG_DEBUG
, "No authentication data provided.");
326 #ifdef HAVE_AUTHORIZATION_H
327 else if (!strncmp(authorization
, "AuthRef ", 8) &&
328 httpAddrLocalhost(httpGetAddress(con
->http
)))
330 OSStatus status
; /* Status */
331 char authdata
[HTTP_MAX_VALUE
];
332 /* Nonce value from client */
333 int authlen
; /* Auth string length */
334 AuthorizationItemSet
*authinfo
; /* Authorization item set */
337 * Get the Authorization Services data...
341 while (isspace(*authorization
& 255))
344 authlen
= sizeof(authdata
);
345 httpDecode64_2(authdata
, &authlen
, authorization
);
347 if (authlen
!= kAuthorizationExternalFormLength
)
349 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "External Authorization reference size is incorrect.");
353 if ((status
= AuthorizationCreateFromExternalForm((AuthorizationExternalForm
*)authdata
, &con
->authref
)) != 0)
355 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "AuthorizationCreateFromExternalForm returned %d (%s)", (int)status
, cssmErrorString(status
));
361 if (!AuthorizationCopyInfo(con
->authref
, kAuthorizationEnvironmentUsername
,
364 if (authinfo
->count
== 1 && authinfo
->items
[0].value
&&
365 authinfo
->items
[0].valueLength
>= 2)
367 strlcpy(username
, authinfo
->items
[0].value
, sizeof(username
));
369 cupsdLogClient(con
, CUPSD_LOG_DEBUG
, "Authorized as \"%s\" using AuthRef.", username
);
372 AuthorizationFreeItemSet(authinfo
);
378 * No username in AuthRef, grab username using peer credentials...
381 struct passwd
*pwd
; /* Password entry for this user */
382 cupsd_ucred_t peercred
; /* Peer credentials */
383 socklen_t peersize
; /* Size of peer credentials */
385 peersize
= sizeof(peercred
);
387 if (getsockopt(httpGetFd(con
->http
), 0, LOCAL_PEERCRED
, &peercred
, &peersize
))
389 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "Unable to get peer credentials - %s", strerror(errno
));
393 if ((pwd
= getpwuid(CUPSD_UCRED_UID(peercred
))) == NULL
)
395 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "Unable to find UID %d for peer credentials.", (int)CUPSD_UCRED_UID(peercred
));
399 strlcpy(username
, pwd
->pw_name
, sizeof(username
));
401 cupsdLogClient(con
, CUPSD_LOG_DEBUG
, "Authorized as \"%s\" using AuthRef + PeerCred.", username
);
404 con
->type
= CUPSD_AUTH_BASIC
;
406 #endif /* HAVE_AUTHORIZATION_H */
407 #if defined(SO_PEERCRED) && defined(AF_LOCAL)
408 else if (!strncmp(authorization
, "PeerCred ", 9) &&
409 con
->http
->hostaddr
->addr
.sa_family
== AF_LOCAL
&& con
->best
)
412 * Use peer credentials from domain socket connection...
415 struct passwd
*pwd
; /* Password entry for this user */
416 cupsd_ucred_t peercred
; /* Peer credentials */
417 socklen_t peersize
; /* Size of peer credentials */
418 #ifdef HAVE_AUTHORIZATION_H
419 const char *name
; /* Authorizing name */
420 int no_peer
= 0; /* Don't allow peer credentials? */
423 * See if we should allow peer credentials...
426 for (name
= (char *)cupsArrayFirst(con
->best
->names
);
428 name
= (char *)cupsArrayNext(con
->best
->names
))
430 if (!_cups_strncasecmp(name
, "@AUTHKEY(", 9) ||
431 !_cups_strcasecmp(name
, "@SYSTEM"))
433 /* Normally don't want peer credentials if we need an auth key... */
436 else if (!_cups_strcasecmp(name
, "@OWNER"))
438 /* but if @OWNER is present then we allow it... */
446 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "PeerCred authentication not allowed for resource per AUTHKEY policy.");
449 #endif /* HAVE_AUTHORIZATION_H */
451 if ((pwd
= getpwnam(authorization
+ 9)) == NULL
)
453 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "User \"%s\" does not exist.", authorization
+ 9);
457 peersize
= sizeof(peercred
);
460 if (getsockopt(httpGetFd(con
->http
), 0, LOCAL_PEERCRED
, &peercred
, &peersize
))
462 if (getsockopt(httpGetFd(con
->http
), SOL_SOCKET
, SO_PEERCRED
, &peercred
, &peersize
))
463 # endif /* __APPLE__ */
465 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "Unable to get peer credentials - %s", strerror(errno
));
469 if (pwd
->pw_uid
!= CUPSD_UCRED_UID(peercred
))
471 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "Invalid peer credentials for \"%s\" - got %d, expected %d.", authorization
+ 9, CUPSD_UCRED_UID(peercred
), pwd
->pw_uid
);
472 # ifdef HAVE_SYS_UCRED_H
473 cupsdLogClient(con
, CUPSD_LOG_DEBUG2
, "cr_version=%d", peercred
.cr_version
);
474 cupsdLogClient(con
, CUPSD_LOG_DEBUG2
, "cr_uid=%d", peercred
.cr_uid
);
475 cupsdLogClient(con
, CUPSD_LOG_DEBUG2
, "cr_ngroups=%d", peercred
.cr_ngroups
);
476 cupsdLogClient(con
, CUPSD_LOG_DEBUG2
, "cr_groups[0]=%d", peercred
.cr_groups
[0]);
477 # endif /* HAVE_SYS_UCRED_H */
481 strlcpy(username
, authorization
+ 9, sizeof(username
));
484 con
->gss_uid
= CUPSD_UCRED_UID(peercred
);
485 # endif /* HAVE_GSSAPI */
487 cupsdLogClient(con
, CUPSD_LOG_DEBUG
, "Authorized as %s using PeerCred.", username
);
489 con
->type
= CUPSD_AUTH_BASIC
;
491 #endif /* SO_PEERCRED && AF_LOCAL */
492 else if (!strncmp(authorization
, "Local", 5) &&
493 httpAddrLocalhost(httpGetAddress(con
->http
)))
496 * Get Local certificate authentication data...
500 while (isspace(*authorization
& 255))
503 if ((localuser
= cupsdFindCert(authorization
)) == NULL
)
505 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "Local authentication certificate not found.");
509 strlcpy(username
, localuser
->username
, sizeof(username
));
510 con
->type
= localuser
->type
;
512 cupsdLogClient(con
, CUPSD_LOG_DEBUG
, "Authorized as %s using Local.", username
);
514 else if (!strncmp(authorization
, "Basic", 5))
517 * Get the Basic authentication data...
520 int userlen
; /* Username:password length */
524 while (isspace(*authorization
& 255))
527 userlen
= sizeof(username
);
528 httpDecode64_2(username
, &userlen
, authorization
);
531 * Pull the username and password out...
534 if ((ptr
= strchr(username
, ':')) == NULL
)
536 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "Missing Basic password.");
545 * Username must not be empty...
548 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "Empty Basic username.");
555 * Password must not be empty...
558 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "Empty Basic password.");
562 strlcpy(password
, ptr
, sizeof(password
));
565 * Validate the username and password...
571 case CUPSD_AUTH_BASIC
:
575 * Only use PAM to do authentication. This supports MD5
576 * passwords, among other things...
579 pam_handle_t
*pamh
; /* PAM authentication handle */
580 int pamerr
; /* PAM error code */
581 struct pam_conv pamdata
;/* PAM conversation data */
582 cupsd_authdata_t data
; /* Authentication data */
585 strlcpy(data
.username
, username
, sizeof(data
.username
));
586 strlcpy(data
.password
, password
, sizeof(data
.password
));
589 pamdata
.conv
= (int (*)(int, struct pam_message
**,
590 struct pam_response
**,
593 pamdata
.conv
= pam_func
;
595 pamdata
.appdata_ptr
= &data
;
597 pamerr
= pam_start("cups", username
, &pamdata
, &pamh
);
598 if (pamerr
!= PAM_SUCCESS
)
600 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "pam_start() returned %d (%s)", pamerr
, pam_strerror(pamh
, pamerr
));
604 # ifdef HAVE_PAM_SET_ITEM
606 pamerr
= pam_set_item(pamh
, PAM_RHOST
, con
->http
->hostname
);
607 if (pamerr
!= PAM_SUCCESS
)
608 cupsdLogClient(con
, CUPSD_LOG_WARN
, "pam_set_item(PAM_RHOST) returned %d (%s)", pamerr
, pam_strerror(pamh
, pamerr
));
609 # endif /* PAM_RHOST */
612 pamerr
= pam_set_item(pamh
, PAM_TTY
, "cups");
613 if (pamerr
!= PAM_SUCCESS
)
614 cupsdLogClient(con
, CUPSD_LOG_WARN
, "pam_set_item(PAM_TTY) returned %d (%s)", pamerr
, pam_strerror(pamh
, pamerr
));
615 # endif /* PAM_TTY */
616 # endif /* HAVE_PAM_SET_ITEM */
618 pamerr
= pam_authenticate(pamh
, PAM_SILENT
);
619 if (pamerr
!= PAM_SUCCESS
)
621 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "pam_authenticate() returned %d (%s)", pamerr
, pam_strerror(pamh
, pamerr
));
626 # ifdef HAVE_PAM_SETCRED
627 pamerr
= pam_setcred(pamh
, PAM_ESTABLISH_CRED
| PAM_SILENT
);
628 if (pamerr
!= PAM_SUCCESS
)
629 cupsdLogClient(con
, CUPSD_LOG_WARN
, "pam_setcred() returned %d (%s)", pamerr
, pam_strerror(pamh
, pamerr
));
630 # endif /* HAVE_PAM_SETCRED */
632 pamerr
= pam_acct_mgmt(pamh
, PAM_SILENT
);
633 if (pamerr
!= PAM_SUCCESS
)
635 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "pam_acct_mgmt() returned %d (%s)", pamerr
, pam_strerror(pamh
, pamerr
));
640 pam_end(pamh
, PAM_SUCCESS
);
644 * Use normal UNIX password file-based authentication...
647 char *pass
; /* Encrypted password */
648 struct passwd
*pw
; /* User password data */
649 # ifdef HAVE_SHADOW_H
650 struct spwd
*spw
; /* Shadow password data */
651 # endif /* HAVE_SHADOW_H */
654 pw
= getpwnam(username
); /* Get the current password */
655 endpwent(); /* Close the password file */
663 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "Unknown username \"%s\".", username
);
667 # ifdef HAVE_SHADOW_H
668 spw
= getspnam(username
);
671 if (!spw
&& !strcmp(pw
->pw_passwd
, "x"))
674 * Don't allow blank passwords!
677 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "Username \"%s\" has no shadow password.", username
);
681 if (spw
&& !spw
->sp_pwdp
[0] && !pw
->pw_passwd
[0])
683 if (!pw
->pw_passwd
[0])
684 # endif /* HAVE_SHADOW_H */
687 * Don't allow blank passwords!
690 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "Username \"%s\" has no password.", username
);
695 * OK, the password isn't blank, so compare with what came from the
699 pass
= cups_crypt(password
, pw
->pw_passwd
);
701 if (!pass
|| strcmp(pw
->pw_passwd
, pass
))
703 # ifdef HAVE_SHADOW_H
706 pass
= cups_crypt(password
, spw
->sp_pwdp
);
708 if (pass
== NULL
|| strcmp(spw
->sp_pwdp
, pass
))
710 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "Authentication failed for user \"%s\".", username
);
715 # endif /* HAVE_SHADOW_H */
717 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "Authentication failed for user \"%s\".", username
);
721 #endif /* HAVE_LIBPAM */
724 cupsdLogClient(con
, CUPSD_LOG_DEBUG
, "Authorized as \"%s\" using Basic.", username
);
731 else if (!strncmp(authorization
, "Negotiate", 9))
733 int len
; /* Length of authorization string */
734 gss_ctx_id_t context
; /* Authorization context */
735 OM_uint32 major_status
, /* Major status code */
736 minor_status
; /* Minor status code */
737 gss_buffer_desc input_token
= GSS_C_EMPTY_BUFFER
,
738 /* Input token from string */
739 output_token
= GSS_C_EMPTY_BUFFER
;
740 /* Output token for username */
741 gss_name_t client_name
; /* Client name */
746 * If the weak-linked GSSAPI/Kerberos library is not present, don't try
750 if (&gss_init_sec_context
== NULL
)
752 cupsdLogClient(con
, CUPSD_LOG_WARN
, "GSSAPI/Kerberos authentication failed because the Kerberos framework is not present.");
755 # endif /* __APPLE__ */
758 * Find the start of the Kerberos input token...
762 while (isspace(*authorization
& 255))
767 cupsdLogClient(con
, CUPSD_LOG_DEBUG2
, "No authentication data specified.");
772 * Decode the authorization string to get the input token...
775 len
= (int)strlen(authorization
);
776 input_token
.value
= malloc((size_t)len
);
777 input_token
.value
= httpDecode64_2(input_token
.value
, &len
,
779 input_token
.length
= (size_t)len
;
782 * Accept the input token to get the authorization info...
785 context
= GSS_C_NO_CONTEXT
;
786 client_name
= GSS_C_NO_NAME
;
787 major_status
= gss_accept_sec_context(&minor_status
,
791 GSS_C_NO_CHANNEL_BINDINGS
,
799 if (output_token
.length
> 0)
800 gss_release_buffer(&minor_status
, &output_token
);
802 if (GSS_ERROR(major_status
))
804 cupsdLogGSSMessage(CUPSD_LOG_DEBUG
, major_status
, minor_status
, "[Client %d] Error accepting GSSAPI security context.", con
->number
);
806 if (context
!= GSS_C_NO_CONTEXT
)
807 gss_delete_sec_context(&minor_status
, &context
, GSS_C_NO_BUFFER
);
814 * Get the username associated with the client's credentials...
817 if (major_status
== GSS_S_CONTINUE_NEEDED
)
818 cupsdLogGSSMessage(CUPSD_LOG_DEBUG
, major_status
, minor_status
, "[Client %d] Credentials not complete.", con
->number
);
819 else if (major_status
== GSS_S_COMPLETE
)
821 major_status
= gss_display_name(&minor_status
, client_name
,
822 &output_token
, NULL
);
824 if (GSS_ERROR(major_status
))
826 cupsdLogGSSMessage(CUPSD_LOG_DEBUG
, major_status
, minor_status
, "[Client %d] Error getting username.", con
->number
);
827 gss_release_name(&minor_status
, &client_name
);
828 gss_delete_sec_context(&minor_status
, &context
, GSS_C_NO_BUFFER
);
832 strlcpy(username
, output_token
.value
, sizeof(username
));
834 cupsdLogClient(con
, CUPSD_LOG_DEBUG
, "Authorized as \"%s\" using Negotiate.", username
);
836 gss_release_name(&minor_status
, &client_name
);
837 gss_release_buffer(&minor_status
, &output_token
);
839 con
->type
= CUPSD_AUTH_NEGOTIATE
;
842 gss_delete_sec_context(&minor_status
, &context
, GSS_C_NO_BUFFER
);
844 # if defined(SO_PEERCRED) && defined(AF_LOCAL)
846 * Get the client's UID if we are printing locally - that allows a backend
847 * to run as the correct user to get Kerberos credentials of its own.
850 if (httpAddrFamily(con
->http
->hostaddr
) == AF_LOCAL
)
852 cupsd_ucred_t peercred
; /* Peer credentials */
853 socklen_t peersize
; /* Size of peer credentials */
855 peersize
= sizeof(peercred
);
858 if (getsockopt(httpGetFd(con
->http
), 0, LOCAL_PEERCRED
, &peercred
, &peersize
))
860 if (getsockopt(httpGetFd(con
->http
), SOL_SOCKET
, SO_PEERCRED
, &peercred
,
862 # endif /* __APPLE__ */
864 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "Unable to get peer credentials - %s", strerror(errno
));
868 cupsdLogClient(con
, CUPSD_LOG_DEBUG
, "Using credentials for UID %d.", CUPSD_UCRED_UID(peercred
));
869 con
->gss_uid
= CUPSD_UCRED_UID(peercred
);
872 # endif /* SO_PEERCRED && AF_LOCAL */
874 #endif /* HAVE_GSSAPI */
877 char scheme
[256]; /* Auth scheme... */
880 if (sscanf(authorization
, "%255s", scheme
) != 1)
881 strlcpy(scheme
, "UNKNOWN", sizeof(scheme
));
883 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "Bad authentication data \"%s ...\".", scheme
);
888 * If we get here, then we were able to validate the username and
889 * password - copy the validated username and password to the client
893 strlcpy(con
->username
, username
, sizeof(con
->username
));
894 strlcpy(con
->password
, password
, sizeof(con
->password
));
899 * 'cupsdCheckAccess()' - Check whether the given address is allowed to
903 int /* O - 1 if allowed, 0 otherwise */
905 unsigned ip
[4], /* I - Client address */
906 const char *name
, /* I - Client hostname */
907 size_t namelen
, /* I - Length of hostname */
908 cupsd_location_t
*loc
) /* I - Location to check */
910 int allow
; /* 1 if allowed, 0 otherwise */
913 if (!_cups_strcasecmp(name
, "localhost"))
916 * Access from localhost (127.0.0.1 or ::1) is always allowed...
924 * Do authorization checks on the domain/address...
927 switch (loc
->order_type
)
930 allow
= 0; /* anti-compiler-warning-code */
933 case CUPSD_AUTH_ALLOW
: /* Order Deny,Allow */
936 if (cupsdCheckAuth(ip
, name
, namelen
, loc
->deny
))
939 if (cupsdCheckAuth(ip
, name
, namelen
, loc
->allow
))
943 case CUPSD_AUTH_DENY
: /* Order Allow,Deny */
946 if (cupsdCheckAuth(ip
, name
, namelen
, loc
->allow
))
949 if (cupsdCheckAuth(ip
, name
, namelen
, loc
->deny
))
960 * 'cupsdCheckAuth()' - Check authorization masks.
963 int /* O - 1 if mask matches, 0 otherwise */
964 cupsdCheckAuth(unsigned ip
[4], /* I - Client address */
965 const char *name
, /* I - Client hostname */
966 size_t name_len
, /* I - Length of hostname */
967 cups_array_t
*masks
) /* I - Masks */
969 int i
; /* Looping var */
970 cupsd_authmask_t
*mask
; /* Current mask */
971 cupsd_netif_t
*iface
; /* Network interface */
972 unsigned netip4
; /* IPv4 network address */
974 unsigned netip6
[4]; /* IPv6 network address */
975 #endif /* AF_INET6 */
978 for (mask
= (cupsd_authmask_t
*)cupsArrayFirst(masks
);
980 mask
= (cupsd_authmask_t
*)cupsArrayNext(masks
))
984 case CUPSD_AUTH_INTERFACE
:
986 * Check for a match with a network interface...
989 netip4
= htonl(ip
[3]);
992 netip6
[0] = htonl(ip
[0]);
993 netip6
[1] = htonl(ip
[1]);
994 netip6
[2] = htonl(ip
[2]);
995 netip6
[3] = htonl(ip
[3]);
996 #endif /* AF_INET6 */
1000 if (!strcmp(mask
->mask
.name
.name
, "*"))
1004 * Allow Back-to-My-Mac addresses...
1007 if ((ip
[0] & 0xff000000) == 0xfd000000)
1009 #endif /* __APPLE__ */
1012 * 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(mask
->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 */
1101 case CUPSD_AUTH_NAME
:
1103 * Check for exact name match...
1106 if (!_cups_strcasecmp(name
, mask
->mask
.name
.name
))
1110 * Check for domain match...
1113 if (name_len
>= mask
->mask
.name
.length
&&
1114 mask
->mask
.name
.name
[0] == '.' &&
1115 !_cups_strcasecmp(name
+ name_len
- mask
->mask
.name
.length
,
1116 mask
->mask
.name
.name
))
1120 case CUPSD_AUTH_IP
:
1122 * Check for IP/network address match...
1125 for (i
= 0; i
< 4; i
++)
1126 if ((ip
[i
] & mask
->mask
.ip
.netmask
[i
]) !=
1127 mask
->mask
.ip
.address
[i
])
1141 * 'cupsdCheckGroup()' - Check for a user's group membership.
1144 int /* O - 1 if user is a member, 0 otherwise */
1146 const char *username
, /* I - User name */
1147 struct passwd
*user
, /* I - System user info */
1148 const char *groupname
) /* I - Group name */
1150 int i
; /* Looping var */
1151 struct group
*group
; /* System group info */
1152 #ifdef HAVE_MBR_UID_TO_UUID
1153 uuid_t useruuid
, /* UUID for username */
1154 groupuuid
; /* UUID for groupname */
1155 int is_member
; /* True if user is a member of group */
1156 #endif /* HAVE_MBR_UID_TO_UUID */
1159 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdCheckGroup(username=\"%s\", user=%p, groupname=\"%s\")", username
, user
, groupname
);
1165 if (!username
|| !groupname
)
1169 * Check to see if the user is a member of the named group...
1172 group
= getgrnam(groupname
);
1178 * Group exists, check it...
1181 for (i
= 0; group
->gr_mem
[i
]; i
++)
1182 if (!_cups_strcasecmp(username
, group
->gr_mem
[i
]))
1187 * Group doesn't exist or user not in group list, check the group ID
1188 * against the user's group ID...
1191 if (user
&& group
&& group
->gr_gid
== user
->pw_gid
)
1194 #ifdef HAVE_MBR_UID_TO_UUID
1196 * Check group membership through MacOS X membership API...
1199 if (user
&& !mbr_uid_to_uuid(user
->pw_uid
, useruuid
))
1204 * Map group name to UUID and check membership...
1207 if (!mbr_gid_to_uuid(group
->gr_gid
, groupuuid
))
1208 if (!mbr_check_membership(useruuid
, groupuuid
, &is_member
))
1212 else if (groupname
[0] == '#')
1215 * Use UUID directly and check for equality (user UUID) and
1216 * membership (group UUID)...
1219 if (!uuid_parse((char *)groupname
+ 1, groupuuid
))
1221 if (!uuid_compare(useruuid
, groupuuid
))
1223 else if (!mbr_check_membership(useruuid
, groupuuid
, &is_member
))
1231 else if (groupname
[0] == '#')
1233 #endif /* HAVE_MBR_UID_TO_UUID */
1236 * If we get this far, then the user isn't part of the named group...
1244 * 'cupsdCopyLocation()' - Make a copy of a location...
1247 cupsd_location_t
* /* O - New location */
1249 cupsd_location_t
*loc
) /* I - Original location */
1251 cupsd_location_t
*temp
; /* New location */
1255 * Make a copy of the original location...
1258 if ((temp
= calloc(1, sizeof(cupsd_location_t
))) == NULL
)
1262 * Copy the information from the original location to the new one.
1269 temp
->location
= _cupsStrAlloc(loc
->location
);
1271 temp
->length
= loc
->length
;
1272 temp
->limit
= loc
->limit
;
1273 temp
->order_type
= loc
->order_type
;
1274 temp
->type
= loc
->type
;
1275 temp
->level
= loc
->level
;
1276 temp
->satisfy
= loc
->satisfy
;
1277 temp
->encryption
= loc
->encryption
;
1281 if ((temp
->names
= cupsArrayDup(loc
->names
)) == NULL
)
1283 cupsdLogMessage(CUPSD_LOG_ERROR
,
1284 "Unable to allocate memory for %d names: %s",
1285 cupsArrayCount(loc
->names
), strerror(errno
));
1287 cupsdFreeLocation(temp
);
1295 * Copy allow rules...
1298 if ((temp
->allow
= cupsArrayDup(loc
->allow
)) == NULL
)
1300 cupsdLogMessage(CUPSD_LOG_ERROR
,
1301 "Unable to allocate memory for %d allow rules: %s",
1302 cupsArrayCount(loc
->allow
), strerror(errno
));
1303 cupsdFreeLocation(temp
);
1311 * Copy deny rules...
1314 if ((temp
->deny
= cupsArrayDup(loc
->deny
)) == NULL
)
1316 cupsdLogMessage(CUPSD_LOG_ERROR
,
1317 "Unable to allocate memory for %d deny rules: %s",
1318 cupsArrayCount(loc
->deny
), strerror(errno
));
1319 cupsdFreeLocation(temp
);
1329 * 'cupsdDeleteAllLocations()' - Free all memory used for location authorization.
1333 cupsdDeleteAllLocations(void)
1336 * Free the location array, which will free all of the locations...
1339 cupsArrayDelete(Locations
);
1345 * 'cupsdFindBest()' - Find the location entry that best matches the resource.
1348 cupsd_location_t
* /* O - Location that matches */
1349 cupsdFindBest(const char *path
, /* I - Resource path */
1350 http_state_t state
) /* I - HTTP state/request */
1352 char uri
[HTTP_MAX_URI
],
1353 /* URI in request... */
1354 *uriptr
; /* Pointer into URI */
1355 cupsd_location_t
*loc
, /* Current location */
1356 *best
; /* Best match for location so far */
1357 size_t bestlen
; /* Length of best match */
1358 int limit
; /* Limit field */
1359 static const int limits
[] = /* Map http_status_t to CUPSD_AUTH_LIMIT_xyz */
1361 CUPSD_AUTH_LIMIT_ALL
,
1362 CUPSD_AUTH_LIMIT_OPTIONS
,
1363 CUPSD_AUTH_LIMIT_GET
,
1364 CUPSD_AUTH_LIMIT_GET
,
1365 CUPSD_AUTH_LIMIT_HEAD
,
1366 CUPSD_AUTH_LIMIT_POST
,
1367 CUPSD_AUTH_LIMIT_POST
,
1368 CUPSD_AUTH_LIMIT_POST
,
1369 CUPSD_AUTH_LIMIT_PUT
,
1370 CUPSD_AUTH_LIMIT_PUT
,
1371 CUPSD_AUTH_LIMIT_DELETE
,
1372 CUPSD_AUTH_LIMIT_TRACE
,
1373 CUPSD_AUTH_LIMIT_ALL
,
1374 CUPSD_AUTH_LIMIT_ALL
,
1375 CUPSD_AUTH_LIMIT_ALL
,
1376 CUPSD_AUTH_LIMIT_ALL
1381 * First copy the connection URI to a local string so we have drop
1382 * any .ppd extension from the pathname in /printers or /classes
1386 strlcpy(uri
, path
, sizeof(uri
));
1388 if ((uriptr
= strchr(uri
, '?')) != NULL
)
1389 *uriptr
= '\0'; /* Drop trailing query string */
1391 if ((uriptr
= uri
+ strlen(uri
) - 1) > uri
&& *uriptr
== '/')
1392 *uriptr
= '\0'; /* Remove trailing '/' */
1394 if (!strncmp(uri
, "/printers/", 10) ||
1395 !strncmp(uri
, "/classes/", 9))
1398 * Check if the URI has .ppd on the end...
1401 uriptr
= uri
+ strlen(uri
) - 4; /* len > 4 if we get here... */
1403 if (!strcmp(uriptr
, ".ppd"))
1408 * Loop through the list of locations to find a match...
1411 limit
= limits
[state
];
1415 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: uri=\"%s\", limit=%x...", uri
, limit
);
1418 for (loc
= (cupsd_location_t
*)cupsArrayFirst(Locations
);
1420 loc
= (cupsd_location_t
*)cupsArrayNext(Locations
))
1422 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: Location %s(%d) Limit %x", loc
->location
? loc
->location
: "(null)", (int)loc
->length
, loc
->limit
);
1424 if (!strncmp(uri
, "/printers/", 10) || !strncmp(uri
, "/classes/", 9))
1427 * Use case-insensitive comparison for queue names...
1430 if (loc
->length
> bestlen
&& loc
->location
&&
1431 !_cups_strncasecmp(uri
, loc
->location
, loc
->length
) &&
1432 loc
->location
[0] == '/' &&
1433 (limit
& loc
->limit
) != 0)
1436 bestlen
= loc
->length
;
1442 * Use case-sensitive comparison for other URIs...
1445 if (loc
->length
> bestlen
&& loc
->location
&&
1446 !strncmp(uri
, loc
->location
, loc
->length
) &&
1447 loc
->location
[0] == '/' &&
1448 (limit
& loc
->limit
) != 0)
1451 bestlen
= loc
->length
;
1457 * Return the match, if any...
1460 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: best=%s", best
? best
->location
: "NONE");
1467 * 'cupsdFindLocation()' - Find the named location.
1470 cupsd_location_t
* /* O - Location that matches */
1471 cupsdFindLocation(const char *location
) /* I - Connection */
1473 cupsd_location_t key
; /* Search key */
1476 key
.location
= (char *)location
;
1478 return ((cupsd_location_t
*)cupsArrayFind(Locations
, &key
));
1483 * 'cupsdFreeLocation()' - Free all memory used by a location.
1487 cupsdFreeLocation(cupsd_location_t
*loc
)/* I - Location to free */
1489 cupsArrayDelete(loc
->names
);
1490 cupsArrayDelete(loc
->allow
);
1491 cupsArrayDelete(loc
->deny
);
1493 _cupsStrFree(loc
->location
);
1499 * 'cupsdIsAuthorized()' - Check to see if the user is authorized...
1502 http_status_t
/* O - HTTP_OK if authorized or error code */
1503 cupsdIsAuthorized(cupsd_client_t
*con
, /* I - Connection */
1504 const char *owner
)/* I - Owner of object */
1506 int i
, /* Looping vars */
1507 auth
, /* Authorization status */
1508 type
; /* Type of authentication */
1509 http_addr_t
*hostaddr
= httpGetAddress(con
->http
);
1510 /* Client address */
1511 const char *hostname
= httpGetHostname(con
->http
, NULL
, 0);
1512 /* Client hostname */
1513 unsigned address
[4]; /* Authorization address */
1514 cupsd_location_t
*best
; /* Best match for location so far */
1515 size_t hostlen
; /* Length of hostname */
1516 char *name
, /* Current username */
1517 username
[256], /* Username to authorize */
1518 ownername
[256], /* Owner name to authorize */
1519 *ptr
; /* Pointer into username */
1520 struct passwd
*pw
; /* User password data */
1521 static const char * const levels
[] = /* Auth levels */
1527 static const char * const types
[] = /* Auth types */
1535 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: con->uri=\"%s\", con->best=%p(%s)", con
->uri
, con
->best
, con
->best
? con
->best
->location
? con
->best
->location
: "(null)" : "");
1537 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: owner=\"%s\"", owner
);
1540 * If there is no "best" authentication rule for this request, then
1541 * access is allowed from the local system and denied from other
1547 if (httpAddrLocalhost(httpGetAddress(con
->http
)) ||
1548 !strcmp(hostname
, ServerName
) ||
1549 cupsArrayFind(ServerAlias
, (void *)hostname
))
1552 return (HTTP_FORBIDDEN
);
1557 if ((type
= best
->type
) == CUPSD_AUTH_DEFAULT
)
1558 type
= cupsdDefaultAuthType();
1560 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: level=CUPSD_AUTH_%s, type=%s, satisfy=CUPSD_AUTH_SATISFY_%s, num_names=%d", levels
[best
->level
], types
[type
], best
->satisfy
? "ANY" : "ALL", cupsArrayCount(best
->names
));
1562 if (best
->limit
== CUPSD_AUTH_LIMIT_IPP
)
1563 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: op=%x(%s)", best
->op
, ippOpString(best
->op
));
1566 * Check host/ip-based accesses...
1570 if (httpAddrFamily(hostaddr
) == AF_INET6
)
1573 * Copy IPv6 address...
1576 address
[0] = ntohl(hostaddr
->ipv6
.sin6_addr
.s6_addr32
[0]);
1577 address
[1] = ntohl(hostaddr
->ipv6
.sin6_addr
.s6_addr32
[1]);
1578 address
[2] = ntohl(hostaddr
->ipv6
.sin6_addr
.s6_addr32
[2]);
1579 address
[3] = ntohl(hostaddr
->ipv6
.sin6_addr
.s6_addr32
[3]);
1582 #endif /* AF_INET6 */
1583 if (con
->http
->hostaddr
->addr
.sa_family
== AF_INET
)
1586 * Copy IPv4 address...
1592 address
[3] = ntohl(hostaddr
->ipv4
.sin_addr
.s_addr
);
1595 memset(address
, 0, sizeof(address
));
1597 hostlen
= strlen(hostname
);
1599 auth
= cupsdCheckAccess(address
, hostname
, hostlen
, best
)
1600 ? CUPSD_AUTH_ALLOW
: CUPSD_AUTH_DENY
;
1602 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: auth=CUPSD_AUTH_%s...", auth
? "DENY" : "ALLOW");
1604 if (auth
== CUPSD_AUTH_DENY
&& best
->satisfy
== CUPSD_AUTH_SATISFY_ALL
)
1605 return (HTTP_FORBIDDEN
);
1609 * See if encryption is required...
1612 if ((best
->encryption
>= HTTP_ENCRYPT_REQUIRED
&& !con
->http
->tls
&&
1613 _cups_strcasecmp(hostname
, "localhost") &&
1614 !httpAddrLocalhost(hostaddr
) &&
1615 best
->satisfy
== CUPSD_AUTH_SATISFY_ALL
) &&
1616 !(type
== CUPSD_AUTH_NEGOTIATE
||
1617 (type
== CUPSD_AUTH_NONE
&&
1618 cupsdDefaultAuthType() == CUPSD_AUTH_NEGOTIATE
)))
1620 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1621 "cupsdIsAuthorized: Need upgrade to TLS...");
1622 return (HTTP_UPGRADE_REQUIRED
);
1624 #endif /* HAVE_SSL */
1627 * Now see what access level is required...
1630 if (best
->level
== CUPSD_AUTH_ANON
|| /* Anonymous access - allow it */
1631 (type
== CUPSD_AUTH_NONE
&& cupsArrayCount(best
->names
) == 0))
1634 if (!con
->username
[0] && type
== CUPSD_AUTH_NONE
&&
1635 best
->limit
== CUPSD_AUTH_LIMIT_IPP
)
1638 * Check for unauthenticated username...
1641 ipp_attribute_t
*attr
; /* requesting-user-name attribute */
1644 attr
= ippFindAttribute(con
->request
, "requesting-user-name", IPP_TAG_NAME
);
1647 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1648 "cupsdIsAuthorized: requesting-user-name=\"%s\"",
1649 attr
->values
[0].string
.text
);
1650 strlcpy(username
, attr
->values
[0].string
.text
, sizeof(username
));
1652 else if (best
->satisfy
== CUPSD_AUTH_SATISFY_ALL
|| auth
== CUPSD_AUTH_DENY
)
1653 return (HTTP_UNAUTHORIZED
); /* Non-anonymous needs user/pass */
1655 return (HTTP_OK
); /* unless overridden with Satisfy */
1659 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdIsAuthorized: username=\"%s\"",
1662 #ifdef HAVE_AUTHORIZATION_H
1663 if (!con
->username
[0] && !con
->authref
)
1665 if (!con
->username
[0])
1666 #endif /* HAVE_AUTHORIZATION_H */
1668 if (best
->satisfy
== CUPSD_AUTH_SATISFY_ALL
|| auth
== CUPSD_AUTH_DENY
)
1669 return (HTTP_UNAUTHORIZED
); /* Non-anonymous needs user/pass */
1671 return (HTTP_OK
); /* unless overridden with Satisfy */
1675 if (con
->type
!= type
&& type
!= CUPSD_AUTH_NONE
&&
1677 (type
!= CUPSD_AUTH_NEGOTIATE
|| con
->gss_uid
<= 0) &&
1678 #endif /* HAVE_GSSAPI */
1679 con
->type
!= CUPSD_AUTH_BASIC
)
1681 cupsdLogMessage(CUPSD_LOG_ERROR
, "Authorized using %s, expected %s.",
1682 types
[con
->type
], types
[type
]);
1684 return (HTTP_UNAUTHORIZED
);
1687 strlcpy(username
, con
->username
, sizeof(username
));
1691 * OK, got a username. See if we need normal user access, or group
1692 * access... (root always matches)
1695 if (!strcmp(username
, "root"))
1699 * Strip any @domain or @KDC from the username and owner...
1702 if ((ptr
= strchr(username
, '@')) != NULL
)
1707 strlcpy(ownername
, owner
, sizeof(ownername
));
1709 if ((ptr
= strchr(ownername
, '@')) != NULL
)
1713 ownername
[0] = '\0';
1716 * Get the user info...
1721 pw
= getpwnam(username
);
1727 if (best
->level
== CUPSD_AUTH_USER
)
1730 * If there are no names associated with this location, then
1731 * any valid user is OK...
1734 if (cupsArrayCount(best
->names
) == 0)
1738 * Otherwise check the user list and return OK if this user is
1742 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: Checking user membership...");
1744 #ifdef HAVE_AUTHORIZATION_H
1746 * If an authorization reference was supplied it must match a right name...
1751 for (name
= (char *)cupsArrayFirst(best
->names
);
1753 name
= (char *)cupsArrayNext(best
->names
))
1755 if (!_cups_strncasecmp(name
, "@AUTHKEY(", 9) && check_authref(con
, name
+ 9))
1757 else if (!_cups_strcasecmp(name
, "@SYSTEM") && SystemGroupAuthKey
&&
1758 check_authref(con
, SystemGroupAuthKey
))
1762 return (HTTP_FORBIDDEN
);
1764 #endif /* HAVE_AUTHORIZATION_H */
1766 for (name
= (char *)cupsArrayFirst(best
->names
);
1768 name
= (char *)cupsArrayNext(best
->names
))
1770 if (!_cups_strcasecmp(name
, "@OWNER") && owner
&&
1771 !_cups_strcasecmp(username
, ownername
))
1773 else if (!_cups_strcasecmp(name
, "@SYSTEM"))
1775 for (i
= 0; i
< NumSystemGroups
; i
++)
1776 if (cupsdCheckGroup(username
, pw
, SystemGroups
[i
]))
1779 else if (name
[0] == '@')
1781 if (cupsdCheckGroup(username
, pw
, name
+ 1))
1784 else if (!_cups_strcasecmp(username
, name
))
1788 return (con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
);
1792 * Check to see if this user is in any of the named groups...
1795 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: Checking group membership...");
1798 * Check to see if this user is in any of the named groups...
1801 for (name
= (char *)cupsArrayFirst(best
->names
);
1803 name
= (char *)cupsArrayNext(best
->names
))
1805 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: Checking group \"%s\" membership...", name
);
1807 if (!_cups_strcasecmp(name
, "@SYSTEM"))
1809 for (i
= 0; i
< NumSystemGroups
; i
++)
1810 if (cupsdCheckGroup(username
, pw
, SystemGroups
[i
]))
1813 else if (cupsdCheckGroup(username
, pw
, name
))
1818 * The user isn't part of the specified group, so deny access...
1821 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdIsAuthorized: User not in group(s).");
1823 return (con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
);
1828 * 'cupsdNewLocation()' - Create a new location for authorization.
1830 * Note: Still need to call cupsdAddLocation() to add it to the list of global
1834 cupsd_location_t
* /* O - Pointer to new location record */
1835 cupsdNewLocation(const char *location
) /* I - Location path */
1837 cupsd_location_t
*temp
; /* New location */
1841 * Try to allocate memory for the new location.
1844 if ((temp
= calloc(1, sizeof(cupsd_location_t
))) == NULL
)
1848 * Initialize the record and copy the name over...
1851 if ((temp
->location
= _cupsStrAlloc(location
)) == NULL
)
1857 temp
->length
= strlen(temp
->location
);
1860 * Return the new record...
1867 #ifdef HAVE_AUTHORIZATION_H
1869 * 'check_authref()' - Check if an authorization services reference has the
1873 static int /* O - 1 if right is valid, 0 otherwise */
1874 check_authref(cupsd_client_t
*con
, /* I - Connection */
1875 const char *right
) /* I - Right name */
1877 OSStatus status
; /* OS Status */
1878 AuthorizationItem authright
; /* Authorization right */
1879 AuthorizationRights authrights
; /* Authorization rights */
1880 AuthorizationFlags authflags
; /* Authorization flags */
1884 * Check to see if the user is allowed to perform the task...
1890 authright
.name
= right
;
1891 authright
.valueLength
= 0;
1892 authright
.value
= NULL
;
1893 authright
.flags
= 0;
1895 authrights
.count
= 1;
1896 authrights
.items
= &authright
;
1898 authflags
= kAuthorizationFlagDefaults
|
1899 kAuthorizationFlagExtendRights
;
1901 if ((status
= AuthorizationCopyRights(con
->authref
, &authrights
,
1902 kAuthorizationEmptyEnvironment
,
1903 authflags
, NULL
)) != 0)
1905 cupsdLogMessage(CUPSD_LOG_ERROR
,
1906 "AuthorizationCopyRights(\"%s\") returned %d (%s)",
1907 authright
.name
, (int)status
, cssmErrorString(status
));
1911 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "AuthorizationCopyRights(\"%s\") succeeded.", authright
.name
);
1915 #endif /* HAVE_AUTHORIZATION_H */
1919 * 'compare_locations()' - Compare two locations.
1922 static int /* O - Result of comparison */
1923 compare_locations(cupsd_location_t
*a
, /* I - First location */
1924 cupsd_location_t
*b
) /* I - Second location */
1926 return (strcmp(b
->location
, a
->location
));
1931 * 'copy_authmask()' - Copy function for auth masks.
1934 static cupsd_authmask_t
* /* O - New auth mask */
1935 copy_authmask(cupsd_authmask_t
*mask
, /* I - Existing auth mask */
1936 void *data
) /* I - User data (unused) */
1938 cupsd_authmask_t
*temp
; /* New auth mask */
1943 if ((temp
= malloc(sizeof(cupsd_authmask_t
))) != NULL
)
1945 memcpy(temp
, mask
, sizeof(cupsd_authmask_t
));
1947 if (temp
->type
== CUPSD_AUTH_NAME
|| temp
->type
== CUPSD_AUTH_INTERFACE
)
1950 * Make a copy of the name...
1953 if ((temp
->mask
.name
.name
= _cupsStrAlloc(temp
->mask
.name
.name
)) == NULL
)
1956 * Failed to make copy...
1971 * 'cups_crypt()' - Encrypt the password using the DES or MD5 algorithms,
1975 static char * /* O - Encrypted password */
1976 cups_crypt(const char *pw
, /* I - Password string */
1977 const char *salt
) /* I - Salt (key) string */
1979 if (!strncmp(salt
, "$1$", 3))
1982 * Use MD5 passwords without the benefit of PAM; this is for
1983 * Slackware Linux, and the algorithm was taken from the
1984 * old shadow-19990827/lib/md5crypt.c source code... :(
1987 int i
; /* Looping var */
1988 unsigned long n
; /* Output number */
1989 int pwlen
; /* Length of password string */
1990 const char *salt_end
; /* End of "salt" data for MD5 */
1991 char *ptr
; /* Pointer into result string */
1992 _cups_md5_state_t state
; /* Primary MD5 state info */
1993 _cups_md5_state_t state2
; /* Secondary MD5 state info */
1994 unsigned char digest
[16]; /* MD5 digest result */
1995 static char result
[120]; /* Final password string */
1999 * Get the salt data between dollar signs, e.g. $1$saltdata$md5.
2000 * Get a maximum of 8 characters of salt data after $1$...
2003 for (salt_end
= salt
+ 3; *salt_end
&& (salt_end
- salt
) < 11; salt_end
++)
2004 if (*salt_end
== '$')
2008 * Compute the MD5 sum we need...
2013 _cupsMD5Init(&state
);
2014 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
2015 _cupsMD5Append(&state
, (unsigned char *)salt
, salt_end
- salt
);
2017 _cupsMD5Init(&state2
);
2018 _cupsMD5Append(&state2
, (unsigned char *)pw
, pwlen
);
2019 _cupsMD5Append(&state2
, (unsigned char *)salt
+ 3, salt_end
- salt
- 3);
2020 _cupsMD5Append(&state2
, (unsigned char *)pw
, pwlen
);
2021 _cupsMD5Finish(&state2
, digest
);
2023 for (i
= pwlen
; i
> 0; i
-= 16)
2024 _cupsMD5Append(&state
, digest
, i
> 16 ? 16 : i
);
2026 for (i
= pwlen
; i
> 0; i
>>= 1)
2027 _cupsMD5Append(&state
, (unsigned char *)((i
& 1) ? "" : pw
), 1);
2029 _cupsMD5Finish(&state
, digest
);
2031 for (i
= 0; i
< 1000; i
++)
2033 _cupsMD5Init(&state
);
2036 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
2038 _cupsMD5Append(&state
, digest
, 16);
2041 _cupsMD5Append(&state
, (unsigned char *)salt
+ 3, salt_end
- salt
- 3);
2044 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
2047 _cupsMD5Append(&state
, digest
, 16);
2049 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
2051 _cupsMD5Finish(&state
, digest
);
2055 * Copy the final sum to the result string and return...
2058 memcpy(result
, salt
, (size_t)(salt_end
- salt
));
2059 ptr
= result
+ (salt_end
- salt
);
2062 for (i
= 0; i
< 5; i
++, ptr
+= 4)
2064 n
= ((((unsigned)digest
[i
] << 8) | (unsigned)digest
[i
+ 6]) << 8);
2067 n
|= (unsigned)digest
[i
+ 12];
2069 n
|= (unsigned)digest
[5];
2074 to64(ptr
, (unsigned)digest
[11], 2);
2083 * Use the standard crypt() function...
2086 return (crypt(pw
, salt
));
2089 #endif /* !HAVE_LIBPAM */
2093 * 'free_authmask()' - Free function for auth masks.
2097 free_authmask(cupsd_authmask_t
*mask
, /* I - Auth mask to free */
2098 void *data
) /* I - User data (unused) */
2102 if (mask
->type
== CUPSD_AUTH_NAME
|| mask
->type
== CUPSD_AUTH_INTERFACE
)
2103 _cupsStrFree(mask
->mask
.name
.name
);
2111 * 'pam_func()' - PAM conversation function.
2114 static int /* O - Success or failure */
2116 int num_msg
, /* I - Number of messages */
2117 const struct pam_message
**msg
, /* I - Messages */
2118 struct pam_response
**resp
, /* O - Responses */
2120 /* I - Pointer to connection */
2122 int i
; /* Looping var */
2123 struct pam_response
*replies
; /* Replies */
2124 cupsd_authdata_t
*data
; /* Pointer to auth data */
2128 * Allocate memory for the responses...
2131 if ((replies
= malloc(sizeof(struct pam_response
) * (size_t)num_msg
)) == NULL
)
2132 return (PAM_CONV_ERR
);
2135 * Answer all of the messages...
2138 DEBUG_printf(("pam_func: appdata_ptr = %p\n", appdata_ptr
));
2140 data
= (cupsd_authdata_t
*)appdata_ptr
;
2142 for (i
= 0; i
< num_msg
; i
++)
2144 DEBUG_printf(("pam_func: Message = \"%s\"\n", msg
[i
]->msg
));
2146 switch (msg
[i
]->msg_style
)
2148 case PAM_PROMPT_ECHO_ON
:
2149 DEBUG_printf(("pam_func: PAM_PROMPT_ECHO_ON, returning \"%s\"...\n",
2151 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2152 replies
[i
].resp
= strdup(data
->username
);
2155 case PAM_PROMPT_ECHO_OFF
:
2156 DEBUG_printf(("pam_func: PAM_PROMPT_ECHO_OFF, returning \"%s\"...\n",
2158 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2159 replies
[i
].resp
= strdup(data
->password
);
2163 DEBUG_puts("pam_func: PAM_TEXT_INFO...");
2164 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2165 replies
[i
].resp
= NULL
;
2169 DEBUG_puts("pam_func: PAM_ERROR_MSG...");
2170 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2171 replies
[i
].resp
= NULL
;
2175 DEBUG_printf(("pam_func: Unknown PAM message %d...\n",
2176 msg
[i
]->msg_style
));
2178 return (PAM_CONV_ERR
);
2183 * Return the responses back to PAM...
2188 return (PAM_SUCCESS
);
2194 * 'to64()' - Base64-encode an integer value...
2198 to64(char *s
, /* O - Output string */
2199 unsigned long v
, /* I - Value to encode */
2200 int n
) /* I - Number of digits */
2202 const char *itoa64
= "./0123456789"
2203 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2204 "abcdefghijklmnopqrstuvwxyz";
2207 for (; n
> 0; n
--, v
>>= 6)
2208 *s
++ = itoa64
[v
& 0x3f];
2210 #endif /* HAVE_LIBPAM */