2 * Authorization routines for the CUPS scheduler.
4 * Copyright © 2007-2019 by Apple Inc.
5 * Copyright © 1997-2007 by Easy Software Products, all rights reserved.
7 * This file contains Kerberos support code, copyright 2006 by
10 * Licensed under Apache License v2.0. See the file "LICENSE" for more
15 * Include necessary headers...
22 #endif /* HAVE_SHADOW_H */
25 #endif /* HAVE_CRYPT_H */
27 # ifdef HAVE_PAM_PAM_APPL_H
28 # include <pam/pam_appl.h>
30 # include <security/pam_appl.h>
31 # endif /* HAVE_PAM_PAM_APPL_H */
32 #endif /* HAVE_LIBPAM */
33 #ifdef HAVE_MEMBERSHIP_H
34 # include <membership.h>
35 #endif /* HAVE_MEMBERSHIP_H */
36 #ifdef HAVE_AUTHORIZATION_H
37 # include <Security/AuthorizationTags.h>
38 #endif /* HAVE_AUTHORIZATION_H */
39 #ifdef HAVE_SYS_PARAM_H
40 # include <sys/param.h>
41 #endif /* HAVE_SYS_PARAM_H */
42 #ifdef HAVE_SYS_UCRED_H
43 # include <sys/ucred.h>
44 typedef struct xucred cupsd_ucred_t
;
45 # define CUPSD_UCRED_UID(c) (c).cr_uid
48 typedef struct ucred cupsd_ucred_t
;
50 typedef struct sockpeercred cupsd_ucred_t
;
52 # define CUPSD_UCRED_UID(c) (c).uid
53 #endif /* HAVE_SYS_UCRED_H */
60 #ifdef HAVE_AUTHORIZATION_H
61 static int check_authref(cupsd_client_t
*con
, const char *right
);
62 #endif /* HAVE_AUTHORIZATION_H */
63 static int compare_locations(cupsd_location_t
*a
,
65 static cupsd_authmask_t
*copy_authmask(cupsd_authmask_t
*am
, void *data
);
66 static void free_authmask(cupsd_authmask_t
*am
, void *data
);
68 static int pam_func(int, const struct pam_message
**,
69 struct pam_response
**, void *);
71 static void to64(char *s
, unsigned long v
, int n
);
72 #endif /* HAVE_LIBPAM */
80 typedef struct cupsd_authdata_s
/**** Authentication data ****/
82 char username
[HTTP_MAX_VALUE
], /* Username string */
83 password
[HTTP_MAX_VALUE
]; /* Password string */
85 #endif /* HAVE_LIBPAM */
89 * 'cupsdAddIPMask()' - Add an IP address authorization mask.
92 int /* O - 1 on success, 0 on failure */
94 cups_array_t
**masks
, /* IO - Masks array (created as needed) */
95 const unsigned address
[4], /* I - IP address */
96 const unsigned netmask
[4]) /* I - IP netmask */
98 cupsd_authmask_t temp
; /* New host/domain mask */
101 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]);
103 temp
.type
= CUPSD_AUTH_IP
;
104 memcpy(temp
.mask
.ip
.address
, address
, sizeof(temp
.mask
.ip
.address
));
105 memcpy(temp
.mask
.ip
.netmask
, netmask
, sizeof(temp
.mask
.ip
.netmask
));
108 * Create the masks array as needed and add...
112 *masks
= cupsArrayNew3(NULL
, NULL
, NULL
, 0,
113 (cups_acopy_func_t
)copy_authmask
,
114 (cups_afree_func_t
)free_authmask
);
116 return (cupsArrayAdd(*masks
, &temp
));
121 * 'cupsdAddLocation()' - Add a location for authorization.
125 cupsdAddLocation(cupsd_location_t
*loc
) /* I - Location to add */
128 * Make sure the locations array is created...
132 Locations
= cupsArrayNew3((cups_array_func_t
)compare_locations
, NULL
,
133 (cups_ahash_func_t
)NULL
, 0,
134 (cups_acopy_func_t
)NULL
,
135 (cups_afree_func_t
)cupsdFreeLocation
);
139 cupsArrayAdd(Locations
, loc
);
141 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAddLocation: Added location \"%s\"", loc
->location
? loc
->location
: "(null)");
147 * 'cupsdAddName()' - Add a name to a location...
151 cupsdAddName(cupsd_location_t
*loc
, /* I - Location to add to */
152 char *name
) /* I - Name to add */
154 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAddName(loc=%p, name=\"%s\")", loc
, name
);
157 loc
->names
= cupsArrayNew3(NULL
, NULL
, NULL
, 0,
158 (cups_acopy_func_t
)_cupsStrAlloc
,
159 (cups_afree_func_t
)_cupsStrFree
);
161 if (!cupsArrayAdd(loc
->names
, name
))
163 cupsdLogMessage(CUPSD_LOG_ERROR
,
164 "Unable to duplicate name for location %s: %s",
165 loc
->location
? loc
->location
: "nil", strerror(errno
));
172 * 'cupsdAddNameMask()' - Add a host or interface name authorization mask.
175 int /* O - 1 on success, 0 on failure */
176 cupsdAddNameMask(cups_array_t
**masks
, /* IO - Masks array (created as needed) */
177 char *name
) /* I - Host or interface name */
179 cupsd_authmask_t temp
; /* New host/domain mask */
180 char ifname
[32], /* Interface name */
181 *ifptr
; /* Pointer to end of name */
184 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAddNameMask(masks=%p(%p), name=\"%s\")", masks
, *masks
, name
);
186 if (!_cups_strcasecmp(name
, "@LOCAL"))
189 * Deny *interface*...
192 temp
.type
= CUPSD_AUTH_INTERFACE
;
193 temp
.mask
.name
.name
= (char *)"*";
195 else if (!_cups_strncasecmp(name
, "@IF(", 4))
198 * Deny *interface*...
201 strlcpy(ifname
, name
+ 4, sizeof(ifname
));
203 ifptr
= ifname
+ strlen(ifname
) - 1;
205 if (ifptr
>= ifname
&& *ifptr
== ')')
211 temp
.type
= CUPSD_AUTH_INTERFACE
;
212 temp
.mask
.name
.name
= ifname
;
223 temp
.type
= CUPSD_AUTH_NAME
;
224 temp
.mask
.name
.name
= (char *)name
;
228 * Set the name length...
231 temp
.mask
.name
.length
= strlen(temp
.mask
.name
.name
);
234 * Create the masks array as needed and add...
238 *masks
= cupsArrayNew3(NULL
, NULL
, NULL
, 0,
239 (cups_acopy_func_t
)copy_authmask
,
240 (cups_afree_func_t
)free_authmask
);
242 return (cupsArrayAdd(*masks
, &temp
));
247 * 'cupsdAuthorize()' - Validate any authorization credentials.
251 cupsdAuthorize(cupsd_client_t
*con
) /* I - Client connection */
253 int type
; /* Authentication type */
254 const char *authorization
; /* Pointer into Authorization string */
255 char *ptr
, /* Pointer into string */
256 username
[HTTP_MAX_VALUE
],
257 /* Username string */
258 password
[HTTP_MAX_VALUE
];
259 /* Password string */
260 cupsd_cert_t
*localuser
; /* Certificate username */
264 * Locate the best matching location so we know what kind of
265 * authentication to expect...
268 con
->best
= cupsdFindBest(con
->uri
, httpGetState(con
->http
));
269 con
->type
= CUPSD_AUTH_NONE
;
271 cupsdLogClient(con
, CUPSD_LOG_DEBUG2
, "con->uri=\"%s\", con->best=%p(%s)", con
->uri
, con
->best
, con
->best
? con
->best
->location
: "");
273 if (con
->best
&& con
->best
->type
!= CUPSD_AUTH_NONE
)
275 if (con
->best
->type
== CUPSD_AUTH_DEFAULT
)
276 type
= cupsdDefaultAuthType();
278 type
= con
->best
->type
;
281 type
= cupsdDefaultAuthType();
284 * Decode the Authorization string...
287 authorization
= httpGetField(con
->http
, HTTP_FIELD_AUTHORIZATION
);
294 #endif /* HAVE_GSSAPI */
296 #ifdef HAVE_AUTHORIZATION_H
299 AuthorizationFree(con
->authref
, kAuthorizationFlagDefaults
);
302 #endif /* HAVE_AUTHORIZATION_H */
307 * No authorization data provided, return early...
310 cupsdLogClient(con
, CUPSD_LOG_DEBUG
, "No authentication data provided.");
313 #ifdef HAVE_AUTHORIZATION_H
314 else if (!strncmp(authorization
, "AuthRef ", 8) &&
315 httpAddrLocalhost(httpGetAddress(con
->http
)))
317 OSStatus status
; /* Status */
318 char authdata
[HTTP_MAX_VALUE
];
319 /* Nonce value from client */
320 int authlen
; /* Auth string length */
321 AuthorizationItemSet
*authinfo
; /* Authorization item set */
324 * Get the Authorization Services data...
328 while (isspace(*authorization
& 255))
331 authlen
= sizeof(authdata
);
332 httpDecode64_2(authdata
, &authlen
, authorization
);
334 if (authlen
!= kAuthorizationExternalFormLength
)
336 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "External Authorization reference size is incorrect.");
340 if ((status
= AuthorizationCreateFromExternalForm((AuthorizationExternalForm
*)authdata
, &con
->authref
)) != 0)
342 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "AuthorizationCreateFromExternalForm returned %d", (int)status
);
348 if (!AuthorizationCopyInfo(con
->authref
, kAuthorizationEnvironmentUsername
,
351 if (authinfo
->count
== 1 && authinfo
->items
[0].value
&&
352 authinfo
->items
[0].valueLength
>= 2)
354 strlcpy(username
, authinfo
->items
[0].value
, sizeof(username
));
356 cupsdLogClient(con
, CUPSD_LOG_DEBUG
, "Authorized as \"%s\" using AuthRef.", username
);
359 AuthorizationFreeItemSet(authinfo
);
365 * No username in AuthRef, grab username using peer credentials...
368 struct passwd
*pwd
; /* Password entry for this user */
369 cupsd_ucred_t peercred
; /* Peer credentials */
370 socklen_t peersize
; /* Size of peer credentials */
372 peersize
= sizeof(peercred
);
374 if (getsockopt(httpGetFd(con
->http
), 0, LOCAL_PEERCRED
, &peercred
, &peersize
))
376 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "Unable to get peer credentials - %s", strerror(errno
));
380 if ((pwd
= getpwuid(CUPSD_UCRED_UID(peercred
))) == NULL
)
382 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "Unable to find UID %d for peer credentials.", (int)CUPSD_UCRED_UID(peercred
));
386 strlcpy(username
, pwd
->pw_name
, sizeof(username
));
388 cupsdLogClient(con
, CUPSD_LOG_DEBUG
, "Authorized as \"%s\" using AuthRef + PeerCred.", username
);
391 con
->type
= CUPSD_AUTH_BASIC
;
393 #endif /* HAVE_AUTHORIZATION_H */
394 #if defined(SO_PEERCRED) && defined(AF_LOCAL)
395 else if (!strncmp(authorization
, "PeerCred ", 9) &&
396 con
->http
->hostaddr
->addr
.sa_family
== AF_LOCAL
&& con
->best
)
399 * Use peer credentials from domain socket connection...
402 struct passwd
*pwd
; /* Password entry for this user */
403 cupsd_ucred_t peercred
; /* Peer credentials */
404 socklen_t peersize
; /* Size of peer credentials */
405 #ifdef HAVE_AUTHORIZATION_H
406 const char *name
; /* Authorizing name */
407 int no_peer
= 0; /* Don't allow peer credentials? */
410 * See if we should allow peer credentials...
413 for (name
= (char *)cupsArrayFirst(con
->best
->names
);
415 name
= (char *)cupsArrayNext(con
->best
->names
))
417 if (!_cups_strncasecmp(name
, "@AUTHKEY(", 9) ||
418 !_cups_strcasecmp(name
, "@SYSTEM"))
420 /* Normally don't want peer credentials if we need an auth key... */
423 else if (!_cups_strcasecmp(name
, "@OWNER"))
425 /* but if @OWNER is present then we allow it... */
433 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "PeerCred authentication not allowed for resource per AUTHKEY policy.");
436 #endif /* HAVE_AUTHORIZATION_H */
438 if ((pwd
= getpwnam(authorization
+ 9)) == NULL
)
440 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "User \"%s\" does not exist.", authorization
+ 9);
444 peersize
= sizeof(peercred
);
447 if (getsockopt(httpGetFd(con
->http
), 0, LOCAL_PEERCRED
, &peercred
, &peersize
))
449 if (getsockopt(httpGetFd(con
->http
), SOL_SOCKET
, SO_PEERCRED
, &peercred
, &peersize
))
450 # endif /* __APPLE__ */
452 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "Unable to get peer credentials - %s", strerror(errno
));
456 if (pwd
->pw_uid
!= CUPSD_UCRED_UID(peercred
))
458 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "Invalid peer credentials for \"%s\" - got %d, expected %d.", authorization
+ 9, CUPSD_UCRED_UID(peercred
), pwd
->pw_uid
);
459 # ifdef HAVE_SYS_UCRED_H
460 cupsdLogClient(con
, CUPSD_LOG_DEBUG2
, "cr_version=%d", peercred
.cr_version
);
461 cupsdLogClient(con
, CUPSD_LOG_DEBUG2
, "cr_uid=%d", peercred
.cr_uid
);
462 cupsdLogClient(con
, CUPSD_LOG_DEBUG2
, "cr_ngroups=%d", peercred
.cr_ngroups
);
463 cupsdLogClient(con
, CUPSD_LOG_DEBUG2
, "cr_groups[0]=%d", peercred
.cr_groups
[0]);
464 # endif /* HAVE_SYS_UCRED_H */
468 strlcpy(username
, authorization
+ 9, sizeof(username
));
471 con
->gss_uid
= CUPSD_UCRED_UID(peercred
);
472 # endif /* HAVE_GSSAPI */
474 cupsdLogClient(con
, CUPSD_LOG_DEBUG
, "Authorized as %s using PeerCred.", username
);
476 con
->type
= CUPSD_AUTH_BASIC
;
478 #endif /* SO_PEERCRED && AF_LOCAL */
479 else if (!strncmp(authorization
, "Local", 5) &&
480 httpAddrLocalhost(httpGetAddress(con
->http
)))
483 * Get Local certificate authentication data...
487 while (isspace(*authorization
& 255))
490 if ((localuser
= cupsdFindCert(authorization
)) == NULL
)
492 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "Local authentication certificate not found.");
496 strlcpy(username
, localuser
->username
, sizeof(username
));
497 con
->type
= localuser
->type
;
499 cupsdLogClient(con
, CUPSD_LOG_DEBUG
, "Authorized as %s using Local.", username
);
501 else if (!strncmp(authorization
, "Basic", 5))
504 * Get the Basic authentication data...
507 int userlen
; /* Username:password length */
511 while (isspace(*authorization
& 255))
514 userlen
= sizeof(username
);
515 httpDecode64_2(username
, &userlen
, authorization
);
518 * Pull the username and password out...
521 if ((ptr
= strchr(username
, ':')) == NULL
)
523 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "Missing Basic password.");
532 * Username must not be empty...
535 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "Empty Basic username.");
542 * Password must not be empty...
545 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "Empty Basic password.");
549 strlcpy(password
, ptr
, sizeof(password
));
552 * Validate the username and password...
558 case CUPSD_AUTH_BASIC
:
562 * Only use PAM to do authentication. This supports MD5
563 * passwords, among other things...
566 pam_handle_t
*pamh
; /* PAM authentication handle */
567 int pamerr
; /* PAM error code */
568 struct pam_conv pamdata
;/* PAM conversation data */
569 cupsd_authdata_t data
; /* Authentication data */
572 strlcpy(data
.username
, username
, sizeof(data
.username
));
573 strlcpy(data
.password
, password
, sizeof(data
.password
));
576 pamdata
.conv
= (int (*)(int, struct pam_message
**,
577 struct pam_response
**,
580 pamdata
.conv
= pam_func
;
582 pamdata
.appdata_ptr
= &data
;
584 pamerr
= pam_start("cups", username
, &pamdata
, &pamh
);
585 if (pamerr
!= PAM_SUCCESS
)
587 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "pam_start() returned %d (%s)", pamerr
, pam_strerror(pamh
, pamerr
));
591 # ifdef HAVE_PAM_SET_ITEM
593 pamerr
= pam_set_item(pamh
, PAM_RHOST
, con
->http
->hostname
);
594 if (pamerr
!= PAM_SUCCESS
)
595 cupsdLogClient(con
, CUPSD_LOG_WARN
, "pam_set_item(PAM_RHOST) returned %d (%s)", pamerr
, pam_strerror(pamh
, pamerr
));
596 # endif /* PAM_RHOST */
599 pamerr
= pam_set_item(pamh
, PAM_TTY
, "cups");
600 if (pamerr
!= PAM_SUCCESS
)
601 cupsdLogClient(con
, CUPSD_LOG_WARN
, "pam_set_item(PAM_TTY) returned %d (%s)", pamerr
, pam_strerror(pamh
, pamerr
));
602 # endif /* PAM_TTY */
603 # endif /* HAVE_PAM_SET_ITEM */
605 pamerr
= pam_authenticate(pamh
, PAM_SILENT
);
606 if (pamerr
!= PAM_SUCCESS
)
608 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "pam_authenticate() returned %d (%s)", pamerr
, pam_strerror(pamh
, pamerr
));
613 # ifdef HAVE_PAM_SETCRED
614 pamerr
= pam_setcred(pamh
, PAM_ESTABLISH_CRED
| PAM_SILENT
);
615 if (pamerr
!= PAM_SUCCESS
)
616 cupsdLogClient(con
, CUPSD_LOG_WARN
, "pam_setcred() returned %d (%s)", pamerr
, pam_strerror(pamh
, pamerr
));
617 # endif /* HAVE_PAM_SETCRED */
619 pamerr
= pam_acct_mgmt(pamh
, PAM_SILENT
);
620 if (pamerr
!= PAM_SUCCESS
)
622 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "pam_acct_mgmt() returned %d (%s)", pamerr
, pam_strerror(pamh
, pamerr
));
627 pam_end(pamh
, PAM_SUCCESS
);
631 * Use normal UNIX password file-based authentication...
634 char *pass
; /* Encrypted password */
635 struct passwd
*pw
; /* User password data */
636 # ifdef HAVE_SHADOW_H
637 struct spwd
*spw
; /* Shadow password data */
638 # endif /* HAVE_SHADOW_H */
641 pw
= getpwnam(username
); /* Get the current password */
642 endpwent(); /* Close the password file */
650 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "Unknown username \"%s\".", username
);
654 # ifdef HAVE_SHADOW_H
655 spw
= getspnam(username
);
658 if (!spw
&& !strcmp(pw
->pw_passwd
, "x"))
661 * Don't allow blank passwords!
664 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "Username \"%s\" has no shadow password.", username
);
668 if (spw
&& !spw
->sp_pwdp
[0] && !pw
->pw_passwd
[0])
670 if (!pw
->pw_passwd
[0])
671 # endif /* HAVE_SHADOW_H */
674 * Don't allow blank passwords!
677 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "Username \"%s\" has no password.", username
);
682 * OK, the password isn't blank, so compare with what came from the
686 pass
= crypt(password
, pw
->pw_passwd
);
688 if (!pass
|| strcmp(pw
->pw_passwd
, pass
))
690 # ifdef HAVE_SHADOW_H
693 pass
= crypt(password
, spw
->sp_pwdp
);
695 if (pass
== NULL
|| strcmp(spw
->sp_pwdp
, pass
))
697 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "Authentication failed for user \"%s\".", username
);
702 # endif /* HAVE_SHADOW_H */
704 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "Authentication failed for user \"%s\".", username
);
708 #endif /* HAVE_LIBPAM */
711 cupsdLogClient(con
, CUPSD_LOG_DEBUG
, "Authorized as \"%s\" using Basic.", username
);
718 else if (!strncmp(authorization
, "Negotiate", 9))
720 int len
; /* Length of authorization string */
721 gss_ctx_id_t context
; /* Authorization context */
722 OM_uint32 major_status
, /* Major status code */
723 minor_status
; /* Minor status code */
724 gss_buffer_desc input_token
= GSS_C_EMPTY_BUFFER
,
725 /* Input token from string */
726 output_token
= GSS_C_EMPTY_BUFFER
;
727 /* Output token for username */
728 gss_name_t client_name
; /* Client name */
733 * If the weak-linked GSSAPI/Kerberos library is not present, don't try
737 if (&gss_init_sec_context
== NULL
)
739 cupsdLogClient(con
, CUPSD_LOG_WARN
, "GSSAPI/Kerberos authentication failed because the Kerberos framework is not present.");
742 # endif /* __APPLE__ */
745 * Find the start of the Kerberos input token...
749 while (isspace(*authorization
& 255))
754 cupsdLogClient(con
, CUPSD_LOG_DEBUG2
, "No authentication data specified.");
759 * Decode the authorization string to get the input token...
762 len
= (int)strlen(authorization
);
763 input_token
.value
= malloc((size_t)len
);
764 input_token
.value
= httpDecode64_2(input_token
.value
, &len
,
766 input_token
.length
= (size_t)len
;
769 * Accept the input token to get the authorization info...
772 context
= GSS_C_NO_CONTEXT
;
773 client_name
= GSS_C_NO_NAME
;
774 major_status
= gss_accept_sec_context(&minor_status
,
778 GSS_C_NO_CHANNEL_BINDINGS
,
786 if (output_token
.length
> 0)
787 gss_release_buffer(&minor_status
, &output_token
);
789 if (GSS_ERROR(major_status
))
791 cupsdLogGSSMessage(CUPSD_LOG_DEBUG
, major_status
, minor_status
, "[Client %d] Error accepting GSSAPI security context.", con
->number
);
793 if (context
!= GSS_C_NO_CONTEXT
)
794 gss_delete_sec_context(&minor_status
, &context
, GSS_C_NO_BUFFER
);
801 * Get the username associated with the client's credentials...
804 if (major_status
== GSS_S_CONTINUE_NEEDED
)
805 cupsdLogGSSMessage(CUPSD_LOG_DEBUG
, major_status
, minor_status
, "[Client %d] Credentials not complete.", con
->number
);
806 else if (major_status
== GSS_S_COMPLETE
)
808 major_status
= gss_display_name(&minor_status
, client_name
,
809 &output_token
, NULL
);
811 if (GSS_ERROR(major_status
))
813 cupsdLogGSSMessage(CUPSD_LOG_DEBUG
, major_status
, minor_status
, "[Client %d] Error getting username.", con
->number
);
814 gss_release_name(&minor_status
, &client_name
);
815 gss_delete_sec_context(&minor_status
, &context
, GSS_C_NO_BUFFER
);
819 strlcpy(username
, output_token
.value
, sizeof(username
));
821 cupsdLogClient(con
, CUPSD_LOG_DEBUG
, "Authorized as \"%s\" using Negotiate.", username
);
823 gss_release_name(&minor_status
, &client_name
);
824 gss_release_buffer(&minor_status
, &output_token
);
826 con
->type
= CUPSD_AUTH_NEGOTIATE
;
829 gss_delete_sec_context(&minor_status
, &context
, GSS_C_NO_BUFFER
);
831 # if defined(SO_PEERCRED) && defined(AF_LOCAL)
833 * Get the client's UID if we are printing locally - that allows a backend
834 * to run as the correct user to get Kerberos credentials of its own.
837 if (httpAddrFamily(con
->http
->hostaddr
) == AF_LOCAL
)
839 cupsd_ucred_t peercred
; /* Peer credentials */
840 socklen_t peersize
; /* Size of peer credentials */
842 peersize
= sizeof(peercred
);
845 if (getsockopt(httpGetFd(con
->http
), 0, LOCAL_PEERCRED
, &peercred
, &peersize
))
847 if (getsockopt(httpGetFd(con
->http
), SOL_SOCKET
, SO_PEERCRED
, &peercred
,
849 # endif /* __APPLE__ */
851 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "Unable to get peer credentials - %s", strerror(errno
));
855 cupsdLogClient(con
, CUPSD_LOG_DEBUG
, "Using credentials for UID %d.", CUPSD_UCRED_UID(peercred
));
856 con
->gss_uid
= CUPSD_UCRED_UID(peercred
);
859 # endif /* SO_PEERCRED && AF_LOCAL */
861 #endif /* HAVE_GSSAPI */
864 char scheme
[256]; /* Auth scheme... */
867 if (sscanf(authorization
, "%255s", scheme
) != 1)
868 strlcpy(scheme
, "UNKNOWN", sizeof(scheme
));
870 cupsdLogClient(con
, CUPSD_LOG_ERROR
, "Bad authentication data \"%s ...\".", scheme
);
875 * If we get here, then we were able to validate the username and
876 * password - copy the validated username and password to the client
880 strlcpy(con
->username
, username
, sizeof(con
->username
));
881 strlcpy(con
->password
, password
, sizeof(con
->password
));
886 * 'cupsdCheckAccess()' - Check whether the given address is allowed to
890 int /* O - 1 if allowed, 0 otherwise */
892 unsigned ip
[4], /* I - Client address */
893 const char *name
, /* I - Client hostname */
894 size_t namelen
, /* I - Length of hostname */
895 cupsd_location_t
*loc
) /* I - Location to check */
897 int allow
; /* 1 if allowed, 0 otherwise */
900 if (!_cups_strcasecmp(name
, "localhost"))
903 * Access from localhost (127.0.0.1 or ::1) is always allowed...
911 * Do authorization checks on the domain/address...
914 switch (loc
->order_type
)
917 allow
= 0; /* anti-compiler-warning-code */
920 case CUPSD_AUTH_ALLOW
: /* Order Deny,Allow */
923 if (cupsdCheckAuth(ip
, name
, namelen
, loc
->deny
))
926 if (cupsdCheckAuth(ip
, name
, namelen
, loc
->allow
))
930 case CUPSD_AUTH_DENY
: /* Order Allow,Deny */
933 if (cupsdCheckAuth(ip
, name
, namelen
, loc
->allow
))
936 if (cupsdCheckAuth(ip
, name
, namelen
, loc
->deny
))
947 * 'cupsdCheckAuth()' - Check authorization masks.
950 int /* O - 1 if mask matches, 0 otherwise */
951 cupsdCheckAuth(unsigned ip
[4], /* I - Client address */
952 const char *name
, /* I - Client hostname */
953 size_t name_len
, /* I - Length of hostname */
954 cups_array_t
*masks
) /* I - Masks */
956 int i
; /* Looping var */
957 cupsd_authmask_t
*mask
; /* Current mask */
958 cupsd_netif_t
*iface
; /* Network interface */
959 unsigned netip4
; /* IPv4 network address */
961 unsigned netip6
[4]; /* IPv6 network address */
962 #endif /* AF_INET6 */
965 for (mask
= (cupsd_authmask_t
*)cupsArrayFirst(masks
);
967 mask
= (cupsd_authmask_t
*)cupsArrayNext(masks
))
971 case CUPSD_AUTH_INTERFACE
:
973 * Check for a match with a network interface...
976 netip4
= htonl(ip
[3]);
979 netip6
[0] = htonl(ip
[0]);
980 netip6
[1] = htonl(ip
[1]);
981 netip6
[2] = htonl(ip
[2]);
982 netip6
[3] = htonl(ip
[3]);
983 #endif /* AF_INET6 */
987 if (!strcmp(mask
->mask
.name
.name
, "*"))
991 * Allow Back-to-My-Mac addresses...
994 if ((ip
[0] & 0xff000000) == 0xfd000000)
996 #endif /* __APPLE__ */
999 * Check against all local interfaces...
1002 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
1004 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
1007 * Only check local interfaces...
1010 if (!iface
->is_local
)
1013 if (iface
->address
.addr
.sa_family
== AF_INET
)
1016 * Check IPv4 address...
1019 if ((netip4
& iface
->mask
.ipv4
.sin_addr
.s_addr
) ==
1020 (iface
->address
.ipv4
.sin_addr
.s_addr
&
1021 iface
->mask
.ipv4
.sin_addr
.s_addr
))
1028 * Check IPv6 address...
1031 for (i
= 0; i
< 4; i
++)
1032 if ((netip6
[i
] & iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]) !=
1033 (iface
->address
.ipv6
.sin6_addr
.s6_addr32
[i
] &
1034 iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]))
1040 #endif /* AF_INET6 */
1046 * Check the named interface...
1049 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
1051 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
1053 if (strcmp(mask
->mask
.name
.name
, iface
->name
))
1056 if (iface
->address
.addr
.sa_family
== AF_INET
)
1059 * Check IPv4 address...
1062 if ((netip4
& iface
->mask
.ipv4
.sin_addr
.s_addr
) ==
1063 (iface
->address
.ipv4
.sin_addr
.s_addr
&
1064 iface
->mask
.ipv4
.sin_addr
.s_addr
))
1071 * Check IPv6 address...
1074 for (i
= 0; i
< 4; i
++)
1075 if ((netip6
[i
] & iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]) !=
1076 (iface
->address
.ipv6
.sin6_addr
.s6_addr32
[i
] &
1077 iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]))
1083 #endif /* AF_INET6 */
1088 case CUPSD_AUTH_NAME
:
1090 * Check for exact name match...
1093 if (!_cups_strcasecmp(name
, mask
->mask
.name
.name
))
1097 * Check for domain match...
1100 if (name_len
>= mask
->mask
.name
.length
&&
1101 mask
->mask
.name
.name
[0] == '.' &&
1102 !_cups_strcasecmp(name
+ name_len
- mask
->mask
.name
.length
,
1103 mask
->mask
.name
.name
))
1107 case CUPSD_AUTH_IP
:
1109 * Check for IP/network address match...
1112 for (i
= 0; i
< 4; i
++)
1113 if ((ip
[i
] & mask
->mask
.ip
.netmask
[i
]) !=
1114 mask
->mask
.ip
.address
[i
])
1128 * 'cupsdCheckGroup()' - Check for a user's group membership.
1131 int /* O - 1 if user is a member, 0 otherwise */
1133 const char *username
, /* I - User name */
1134 struct passwd
*user
, /* I - System user info */
1135 const char *groupname
) /* I - Group name */
1137 int i
; /* Looping var */
1138 struct group
*group
; /* Group info */
1139 gid_t groupid
; /* ID of named group */
1140 #ifdef HAVE_MBR_UID_TO_UUID
1141 uuid_t useruuid
, /* UUID for username */
1142 groupuuid
; /* UUID for groupname */
1143 int is_member
; /* True if user is a member of group */
1144 #endif /* HAVE_MBR_UID_TO_UUID */
1147 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdCheckGroup(username=\"%s\", user=%p, groupname=\"%s\")", username
, user
, groupname
);
1153 if (!username
|| !groupname
)
1157 * Check to see if the user is a member of the named group...
1160 group
= getgrnam(groupname
);
1166 * Group exists, check it...
1169 groupid
= group
->gr_gid
;
1171 #ifdef HAVE_GETGROUPLIST
1174 int ngroups
; /* Number of groups */
1176 int groups
[2048]; /* Groups that user belongs to */
1178 gid_t groups
[2048]; /* Groups that user belongs to */
1179 # endif /* __APPLE__ */
1181 ngroups
= (int)(sizeof(groups
) / sizeof(groups
[0]));
1183 getgrouplist(username
, (int)user
->pw_gid
, groups
, &ngroups
);
1185 getgrouplist(username
, user
->pw_gid
, groups
, &ngroups
);
1186 #endif /* __APPLE__ */
1188 for (i
= 0; i
< ngroups
; i
++)
1189 if ((int)groupid
== (int)groups
[i
])
1194 for (i
= 0; group
->gr_mem
[i
]; i
++)
1196 if (!_cups_strcasecmp(username
, group
->gr_mem
[i
]))
1199 #endif /* HAVE_GETGROUPLIST */
1202 groupid
= (gid_t
)-1;
1205 * Group doesn't exist or user not in group list, check the group ID
1206 * against the user's group ID...
1209 if (user
&& groupid
== user
->pw_gid
)
1212 #ifdef HAVE_MBR_UID_TO_UUID
1214 * Check group membership through macOS membership API...
1217 if (user
&& !mbr_uid_to_uuid(user
->pw_uid
, useruuid
))
1219 if (groupid
!= (gid_t
)-1)
1222 * Map group name to UUID and check membership...
1225 if (!mbr_gid_to_uuid(groupid
, groupuuid
))
1226 if (!mbr_check_membership(useruuid
, groupuuid
, &is_member
))
1230 else if (groupname
[0] == '#')
1233 * Use UUID directly and check for equality (user UUID) and
1234 * membership (group UUID)...
1237 if (!uuid_parse((char *)groupname
+ 1, groupuuid
))
1239 if (!uuid_compare(useruuid
, groupuuid
))
1241 else if (!mbr_check_membership(useruuid
, groupuuid
, &is_member
))
1249 else if (groupname
[0] == '#')
1251 #endif /* HAVE_MBR_UID_TO_UUID */
1254 * If we get this far, then the user isn't part of the named group...
1262 * 'cupsdCopyLocation()' - Make a copy of a location...
1265 cupsd_location_t
* /* O - New location */
1267 cupsd_location_t
*loc
) /* I - Original location */
1269 cupsd_location_t
*temp
; /* New location */
1273 * Make a copy of the original location...
1276 if ((temp
= calloc(1, sizeof(cupsd_location_t
))) == NULL
)
1280 * Copy the information from the original location to the new one.
1287 temp
->location
= _cupsStrAlloc(loc
->location
);
1289 temp
->length
= loc
->length
;
1290 temp
->limit
= loc
->limit
;
1291 temp
->order_type
= loc
->order_type
;
1292 temp
->type
= loc
->type
;
1293 temp
->level
= loc
->level
;
1294 temp
->satisfy
= loc
->satisfy
;
1295 temp
->encryption
= loc
->encryption
;
1299 if ((temp
->names
= cupsArrayDup(loc
->names
)) == NULL
)
1301 cupsdLogMessage(CUPSD_LOG_ERROR
,
1302 "Unable to allocate memory for %d names: %s",
1303 cupsArrayCount(loc
->names
), strerror(errno
));
1305 cupsdFreeLocation(temp
);
1313 * Copy allow rules...
1316 if ((temp
->allow
= cupsArrayDup(loc
->allow
)) == NULL
)
1318 cupsdLogMessage(CUPSD_LOG_ERROR
,
1319 "Unable to allocate memory for %d allow rules: %s",
1320 cupsArrayCount(loc
->allow
), strerror(errno
));
1321 cupsdFreeLocation(temp
);
1329 * Copy deny rules...
1332 if ((temp
->deny
= cupsArrayDup(loc
->deny
)) == NULL
)
1334 cupsdLogMessage(CUPSD_LOG_ERROR
,
1335 "Unable to allocate memory for %d deny rules: %s",
1336 cupsArrayCount(loc
->deny
), strerror(errno
));
1337 cupsdFreeLocation(temp
);
1347 * 'cupsdDeleteAllLocations()' - Free all memory used for location authorization.
1351 cupsdDeleteAllLocations(void)
1354 * Free the location array, which will free all of the locations...
1357 cupsArrayDelete(Locations
);
1363 * 'cupsdFindBest()' - Find the location entry that best matches the resource.
1366 cupsd_location_t
* /* O - Location that matches */
1367 cupsdFindBest(const char *path
, /* I - Resource path */
1368 http_state_t state
) /* I - HTTP state/request */
1370 char uri
[HTTP_MAX_URI
],
1371 /* URI in request... */
1372 *uriptr
; /* Pointer into URI */
1373 cupsd_location_t
*loc
, /* Current location */
1374 *best
; /* Best match for location so far */
1375 size_t bestlen
; /* Length of best match */
1376 int limit
; /* Limit field */
1377 static const int limits
[] = /* Map http_status_t to CUPSD_AUTH_LIMIT_xyz */
1379 CUPSD_AUTH_LIMIT_ALL
,
1380 CUPSD_AUTH_LIMIT_OPTIONS
,
1381 CUPSD_AUTH_LIMIT_GET
,
1382 CUPSD_AUTH_LIMIT_GET
,
1383 CUPSD_AUTH_LIMIT_HEAD
,
1384 CUPSD_AUTH_LIMIT_POST
,
1385 CUPSD_AUTH_LIMIT_POST
,
1386 CUPSD_AUTH_LIMIT_POST
,
1387 CUPSD_AUTH_LIMIT_PUT
,
1388 CUPSD_AUTH_LIMIT_PUT
,
1389 CUPSD_AUTH_LIMIT_DELETE
,
1390 CUPSD_AUTH_LIMIT_TRACE
,
1391 CUPSD_AUTH_LIMIT_ALL
,
1392 CUPSD_AUTH_LIMIT_ALL
,
1393 CUPSD_AUTH_LIMIT_ALL
,
1394 CUPSD_AUTH_LIMIT_ALL
1399 * First copy the connection URI to a local string so we have drop
1400 * any .ppd extension from the pathname in /printers or /classes
1404 strlcpy(uri
, path
, sizeof(uri
));
1406 if ((uriptr
= strchr(uri
, '?')) != NULL
)
1407 *uriptr
= '\0'; /* Drop trailing query string */
1409 if ((uriptr
= uri
+ strlen(uri
) - 1) > uri
&& *uriptr
== '/')
1410 *uriptr
= '\0'; /* Remove trailing '/' */
1412 if (!strncmp(uri
, "/printers/", 10) ||
1413 !strncmp(uri
, "/classes/", 9))
1416 * Check if the URI has .ppd on the end...
1419 uriptr
= uri
+ strlen(uri
) - 4; /* len > 4 if we get here... */
1421 if (!strcmp(uriptr
, ".ppd"))
1426 * Loop through the list of locations to find a match...
1429 limit
= limits
[state
];
1433 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: uri=\"%s\", limit=%x...", uri
, limit
);
1436 for (loc
= (cupsd_location_t
*)cupsArrayFirst(Locations
);
1438 loc
= (cupsd_location_t
*)cupsArrayNext(Locations
))
1440 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: Location %s(%d) Limit %x", loc
->location
? loc
->location
: "(null)", (int)loc
->length
, loc
->limit
);
1442 if (!strncmp(uri
, "/printers/", 10) || !strncmp(uri
, "/classes/", 9))
1445 * Use case-insensitive comparison for queue names...
1448 if (loc
->length
> bestlen
&& loc
->location
&&
1449 !_cups_strncasecmp(uri
, loc
->location
, loc
->length
) &&
1450 loc
->location
[0] == '/' &&
1451 (limit
& loc
->limit
) != 0)
1454 bestlen
= loc
->length
;
1460 * Use case-sensitive comparison for other URIs...
1463 if (loc
->length
> bestlen
&& loc
->location
&&
1464 !strncmp(uri
, loc
->location
, loc
->length
) &&
1465 loc
->location
[0] == '/' &&
1466 (limit
& loc
->limit
) != 0)
1469 bestlen
= loc
->length
;
1475 * Return the match, if any...
1478 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: best=%s", best
? best
->location
: "NONE");
1485 * 'cupsdFindLocation()' - Find the named location.
1488 cupsd_location_t
* /* O - Location that matches */
1489 cupsdFindLocation(const char *location
) /* I - Connection */
1491 cupsd_location_t key
; /* Search key */
1494 key
.location
= (char *)location
;
1496 return ((cupsd_location_t
*)cupsArrayFind(Locations
, &key
));
1501 * 'cupsdFreeLocation()' - Free all memory used by a location.
1505 cupsdFreeLocation(cupsd_location_t
*loc
)/* I - Location to free */
1507 cupsArrayDelete(loc
->names
);
1508 cupsArrayDelete(loc
->allow
);
1509 cupsArrayDelete(loc
->deny
);
1511 _cupsStrFree(loc
->location
);
1517 * 'cupsdIsAuthorized()' - Check to see if the user is authorized...
1520 http_status_t
/* O - HTTP_OK if authorized or error code */
1521 cupsdIsAuthorized(cupsd_client_t
*con
, /* I - Connection */
1522 const char *owner
)/* I - Owner of object */
1524 int i
, /* Looping vars */
1525 auth
, /* Authorization status */
1526 type
; /* Type of authentication */
1527 http_addr_t
*hostaddr
= httpGetAddress(con
->http
);
1528 /* Client address */
1529 const char *hostname
= httpGetHostname(con
->http
, NULL
, 0);
1530 /* Client hostname */
1531 unsigned address
[4]; /* Authorization address */
1532 cupsd_location_t
*best
; /* Best match for location so far */
1533 size_t hostlen
; /* Length of hostname */
1534 char *name
, /* Current username */
1535 username
[256], /* Username to authorize */
1536 ownername
[256], /* Owner name to authorize */
1537 *ptr
; /* Pointer into username */
1538 struct passwd
*pw
; /* User password data */
1539 static const char * const levels
[] = /* Auth levels */
1545 static const char * const types
[] = /* Auth types */
1553 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)" : "");
1555 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: owner=\"%s\"", owner
);
1558 * If there is no "best" authentication rule for this request, then
1559 * access is allowed from the local system and denied from other
1565 if (httpAddrLocalhost(httpGetAddress(con
->http
)) ||
1566 !strcmp(hostname
, ServerName
) ||
1567 cupsArrayFind(ServerAlias
, (void *)hostname
))
1570 return (HTTP_FORBIDDEN
);
1575 if ((type
= best
->type
) == CUPSD_AUTH_DEFAULT
)
1576 type
= cupsdDefaultAuthType();
1578 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
));
1580 if (best
->limit
== CUPSD_AUTH_LIMIT_IPP
)
1581 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: op=%x(%s)", best
->op
, ippOpString(best
->op
));
1584 * Check host/ip-based accesses...
1588 if (httpAddrFamily(hostaddr
) == AF_INET6
)
1591 * Copy IPv6 address...
1594 address
[0] = ntohl(hostaddr
->ipv6
.sin6_addr
.s6_addr32
[0]);
1595 address
[1] = ntohl(hostaddr
->ipv6
.sin6_addr
.s6_addr32
[1]);
1596 address
[2] = ntohl(hostaddr
->ipv6
.sin6_addr
.s6_addr32
[2]);
1597 address
[3] = ntohl(hostaddr
->ipv6
.sin6_addr
.s6_addr32
[3]);
1600 #endif /* AF_INET6 */
1601 if (con
->http
->hostaddr
->addr
.sa_family
== AF_INET
)
1604 * Copy IPv4 address...
1610 address
[3] = ntohl(hostaddr
->ipv4
.sin_addr
.s_addr
);
1613 memset(address
, 0, sizeof(address
));
1615 hostlen
= strlen(hostname
);
1617 auth
= cupsdCheckAccess(address
, hostname
, hostlen
, best
)
1618 ? CUPSD_AUTH_ALLOW
: CUPSD_AUTH_DENY
;
1620 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: auth=CUPSD_AUTH_%s...", auth
? "DENY" : "ALLOW");
1622 if (auth
== CUPSD_AUTH_DENY
&& best
->satisfy
== CUPSD_AUTH_SATISFY_ALL
)
1623 return (HTTP_FORBIDDEN
);
1627 * See if encryption is required...
1630 if ((best
->encryption
>= HTTP_ENCRYPT_REQUIRED
&& !con
->http
->tls
&&
1631 _cups_strcasecmp(hostname
, "localhost") &&
1632 !httpAddrLocalhost(hostaddr
) &&
1633 best
->satisfy
== CUPSD_AUTH_SATISFY_ALL
) &&
1634 !(type
== CUPSD_AUTH_NEGOTIATE
||
1635 (type
== CUPSD_AUTH_NONE
&&
1636 cupsdDefaultAuthType() == CUPSD_AUTH_NEGOTIATE
)))
1638 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1639 "cupsdIsAuthorized: Need upgrade to TLS...");
1640 return (HTTP_UPGRADE_REQUIRED
);
1642 #endif /* HAVE_SSL */
1645 * Now see what access level is required...
1648 if (best
->level
== CUPSD_AUTH_ANON
|| /* Anonymous access - allow it */
1649 (type
== CUPSD_AUTH_NONE
&& cupsArrayCount(best
->names
) == 0))
1652 if (!con
->username
[0] && type
== CUPSD_AUTH_NONE
&&
1653 best
->limit
== CUPSD_AUTH_LIMIT_IPP
)
1656 * Check for unauthenticated username...
1659 ipp_attribute_t
*attr
; /* requesting-user-name attribute */
1662 attr
= ippFindAttribute(con
->request
, "requesting-user-name", IPP_TAG_NAME
);
1665 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1666 "cupsdIsAuthorized: requesting-user-name=\"%s\"",
1667 attr
->values
[0].string
.text
);
1668 strlcpy(username
, attr
->values
[0].string
.text
, sizeof(username
));
1670 else if (best
->satisfy
== CUPSD_AUTH_SATISFY_ALL
|| auth
== CUPSD_AUTH_DENY
)
1671 return (HTTP_UNAUTHORIZED
); /* Non-anonymous needs user/pass */
1673 return (HTTP_OK
); /* unless overridden with Satisfy */
1677 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdIsAuthorized: username=\"%s\"",
1680 #ifdef HAVE_AUTHORIZATION_H
1681 if (!con
->username
[0] && !con
->authref
)
1683 if (!con
->username
[0])
1684 #endif /* HAVE_AUTHORIZATION_H */
1686 if (best
->satisfy
== CUPSD_AUTH_SATISFY_ALL
|| auth
== CUPSD_AUTH_DENY
)
1687 return (HTTP_UNAUTHORIZED
); /* Non-anonymous needs user/pass */
1689 return (HTTP_OK
); /* unless overridden with Satisfy */
1693 if (con
->type
!= type
&& type
!= CUPSD_AUTH_NONE
&&
1695 (type
!= CUPSD_AUTH_NEGOTIATE
|| con
->gss_uid
<= 0) &&
1696 #endif /* HAVE_GSSAPI */
1697 con
->type
!= CUPSD_AUTH_BASIC
)
1699 cupsdLogMessage(CUPSD_LOG_ERROR
, "Authorized using %s, expected %s.",
1700 types
[con
->type
], types
[type
]);
1702 return (HTTP_UNAUTHORIZED
);
1705 strlcpy(username
, con
->username
, sizeof(username
));
1709 * OK, got a username. See if we need normal user access, or group
1710 * access... (root always matches)
1713 if (!strcmp(username
, "root"))
1717 * Strip any @domain or @KDC from the username and owner...
1720 if ((ptr
= strchr(username
, '@')) != NULL
)
1725 strlcpy(ownername
, owner
, sizeof(ownername
));
1727 if ((ptr
= strchr(ownername
, '@')) != NULL
)
1731 ownername
[0] = '\0';
1734 * Get the user info...
1739 pw
= getpwnam(username
);
1745 if (best
->level
== CUPSD_AUTH_USER
)
1748 * If there are no names associated with this location, then
1749 * any valid user is OK...
1752 if (cupsArrayCount(best
->names
) == 0)
1756 * Otherwise check the user list and return OK if this user is
1760 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: Checking user membership...");
1762 #ifdef HAVE_AUTHORIZATION_H
1764 * If an authorization reference was supplied it must match a right name...
1769 for (name
= (char *)cupsArrayFirst(best
->names
);
1771 name
= (char *)cupsArrayNext(best
->names
))
1773 if (!_cups_strncasecmp(name
, "@AUTHKEY(", 9) && check_authref(con
, name
+ 9))
1775 else if (!_cups_strcasecmp(name
, "@SYSTEM") && SystemGroupAuthKey
&&
1776 check_authref(con
, SystemGroupAuthKey
))
1780 return (HTTP_FORBIDDEN
);
1782 #endif /* HAVE_AUTHORIZATION_H */
1784 for (name
= (char *)cupsArrayFirst(best
->names
);
1786 name
= (char *)cupsArrayNext(best
->names
))
1788 if (!_cups_strcasecmp(name
, "@OWNER") && owner
&&
1789 !_cups_strcasecmp(username
, ownername
))
1791 else if (!_cups_strcasecmp(name
, "@SYSTEM"))
1793 for (i
= 0; i
< NumSystemGroups
; i
++)
1794 if (cupsdCheckGroup(username
, pw
, SystemGroups
[i
]))
1797 else if (name
[0] == '@')
1799 if (cupsdCheckGroup(username
, pw
, name
+ 1))
1802 else if (!_cups_strcasecmp(username
, name
))
1806 return (con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
);
1810 * Check to see if this user is in any of the named groups...
1813 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: Checking group membership...");
1816 * Check to see if this user is in any of the named groups...
1819 for (name
= (char *)cupsArrayFirst(best
->names
);
1821 name
= (char *)cupsArrayNext(best
->names
))
1823 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: Checking group \"%s\" membership...", name
);
1825 if (!_cups_strcasecmp(name
, "@SYSTEM"))
1827 for (i
= 0; i
< NumSystemGroups
; i
++)
1828 if (cupsdCheckGroup(username
, pw
, SystemGroups
[i
]))
1831 else if (cupsdCheckGroup(username
, pw
, name
))
1836 * The user isn't part of the specified group, so deny access...
1839 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdIsAuthorized: User not in group(s).");
1841 return (con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
);
1846 * 'cupsdNewLocation()' - Create a new location for authorization.
1848 * Note: Still need to call cupsdAddLocation() to add it to the list of global
1852 cupsd_location_t
* /* O - Pointer to new location record */
1853 cupsdNewLocation(const char *location
) /* I - Location path */
1855 cupsd_location_t
*temp
; /* New location */
1859 * Try to allocate memory for the new location.
1862 if ((temp
= calloc(1, sizeof(cupsd_location_t
))) == NULL
)
1866 * Initialize the record and copy the name over...
1869 if ((temp
->location
= _cupsStrAlloc(location
)) == NULL
)
1875 temp
->length
= strlen(temp
->location
);
1878 * Return the new record...
1885 #ifdef HAVE_AUTHORIZATION_H
1887 * 'check_authref()' - Check if an authorization services reference has the
1891 static int /* O - 1 if right is valid, 0 otherwise */
1892 check_authref(cupsd_client_t
*con
, /* I - Connection */
1893 const char *right
) /* I - Right name */
1895 OSStatus status
; /* OS Status */
1896 AuthorizationItem authright
; /* Authorization right */
1897 AuthorizationRights authrights
; /* Authorization rights */
1898 AuthorizationFlags authflags
; /* Authorization flags */
1902 * Check to see if the user is allowed to perform the task...
1908 authright
.name
= right
;
1909 authright
.valueLength
= 0;
1910 authright
.value
= NULL
;
1911 authright
.flags
= 0;
1913 authrights
.count
= 1;
1914 authrights
.items
= &authright
;
1916 authflags
= kAuthorizationFlagDefaults
|
1917 kAuthorizationFlagExtendRights
;
1919 if ((status
= AuthorizationCopyRights(con
->authref
, &authrights
,
1920 kAuthorizationEmptyEnvironment
,
1921 authflags
, NULL
)) != 0)
1923 cupsdLogMessage(CUPSD_LOG_ERROR
, "AuthorizationCopyRights(\"%s\") returned %d", authright
.name
, (int)status
);
1927 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "AuthorizationCopyRights(\"%s\") succeeded.", authright
.name
);
1931 #endif /* HAVE_AUTHORIZATION_H */
1935 * 'compare_locations()' - Compare two locations.
1938 static int /* O - Result of comparison */
1939 compare_locations(cupsd_location_t
*a
, /* I - First location */
1940 cupsd_location_t
*b
) /* I - Second location */
1942 return (strcmp(b
->location
, a
->location
));
1947 * 'copy_authmask()' - Copy function for auth masks.
1950 static cupsd_authmask_t
* /* O - New auth mask */
1951 copy_authmask(cupsd_authmask_t
*mask
, /* I - Existing auth mask */
1952 void *data
) /* I - User data (unused) */
1954 cupsd_authmask_t
*temp
; /* New auth mask */
1959 if ((temp
= malloc(sizeof(cupsd_authmask_t
))) != NULL
)
1961 memcpy(temp
, mask
, sizeof(cupsd_authmask_t
));
1963 if (temp
->type
== CUPSD_AUTH_NAME
|| temp
->type
== CUPSD_AUTH_INTERFACE
)
1966 * Make a copy of the name...
1969 if ((temp
->mask
.name
.name
= _cupsStrAlloc(temp
->mask
.name
.name
)) == NULL
)
1972 * Failed to make copy...
1986 * 'free_authmask()' - Free function for auth masks.
1990 free_authmask(cupsd_authmask_t
*mask
, /* I - Auth mask to free */
1991 void *data
) /* I - User data (unused) */
1995 if (mask
->type
== CUPSD_AUTH_NAME
|| mask
->type
== CUPSD_AUTH_INTERFACE
)
1996 _cupsStrFree(mask
->mask
.name
.name
);
2004 * 'pam_func()' - PAM conversation function.
2007 static int /* O - Success or failure */
2009 int num_msg
, /* I - Number of messages */
2010 const struct pam_message
**msg
, /* I - Messages */
2011 struct pam_response
**resp
, /* O - Responses */
2013 /* I - Pointer to connection */
2015 int i
; /* Looping var */
2016 struct pam_response
*replies
; /* Replies */
2017 cupsd_authdata_t
*data
; /* Pointer to auth data */
2021 * Allocate memory for the responses...
2024 if ((replies
= malloc(sizeof(struct pam_response
) * (size_t)num_msg
)) == NULL
)
2025 return (PAM_CONV_ERR
);
2028 * Answer all of the messages...
2031 data
= (cupsd_authdata_t
*)appdata_ptr
;
2033 for (i
= 0; i
< num_msg
; i
++)
2035 switch (msg
[i
]->msg_style
)
2037 case PAM_PROMPT_ECHO_ON
:
2038 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2039 replies
[i
].resp
= strdup(data
->username
);
2042 case PAM_PROMPT_ECHO_OFF
:
2043 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2044 replies
[i
].resp
= strdup(data
->password
);
2048 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2049 replies
[i
].resp
= NULL
;
2053 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2054 replies
[i
].resp
= NULL
;
2059 return (PAM_CONV_ERR
);
2064 * Return the responses back to PAM...
2069 return (PAM_SUCCESS
);
2075 * 'to64()' - Base64-encode an integer value...
2079 to64(char *s
, /* O - Output string */
2080 unsigned long v
, /* I - Value to encode */
2081 int n
) /* I - Number of digits */
2083 const char *itoa64
= "./0123456789"
2084 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2085 "abcdefghijklmnopqrstuvwxyz";
2088 for (; n
> 0; n
--, v
>>= 6)
2089 *s
++ = itoa64
[v
& 0x3f];
2091 #endif /* HAVE_LIBPAM */