]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/auth.c
Fix builds without PAM (Issue #5283)
[thirdparty/cups.git] / scheduler / auth.c
CommitLineData
b423cd4c 1/*
5ec1fd3d 2 * Authorization routines for the CUPS scheduler.
ef416fc2 3 *
570933a6
MS
4 * Copyright © 2007-2018 by Apple Inc.
5 * Copyright © 1997-2007 by Easy Software Products, all rights reserved.
f7deaa1a 6 *
5ec1fd3d
MS
7 * This file contains Kerberos support code, copyright 2006 by
8 * Jelmer Vernooij.
ef416fc2 9 *
5ec1fd3d
MS
10 * These coded instructions, statements, and computer programs are the
11 * property of Apple Inc. and are protected by Federal copyright
12 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
13 * which should have been included with this file. If this file is
57b7b66b 14 * missing or damaged, see the license at "http://www.cups.org/".
ef416fc2 15 */
16
17/*
18 * Include necessary headers...
19 */
20
21#include "cupsd.h"
22#include <grp.h>
23#ifdef HAVE_SHADOW_H
24# include <shadow.h>
25#endif /* HAVE_SHADOW_H */
26#ifdef HAVE_CRYPT_H
27# include <crypt.h>
28#endif /* HAVE_CRYPT_H */
29#if HAVE_LIBPAM
30# ifdef HAVE_PAM_PAM_APPL_H
31# include <pam/pam_appl.h>
32# else
33# include <security/pam_appl.h>
34# endif /* HAVE_PAM_PAM_APPL_H */
35#endif /* HAVE_LIBPAM */
bd7854cb 36#ifdef HAVE_MEMBERSHIP_H
37# include <membership.h>
38#endif /* HAVE_MEMBERSHIP_H */
f7deaa1a 39#ifdef HAVE_AUTHORIZATION_H
40# include <Security/AuthorizationTags.h>
41# ifdef HAVE_SECBASEPRIV_H
42# include <Security/SecBasePriv.h>
43# else
44extern const char *cssmErrorString(int error);
45# endif /* HAVE_SECBASEPRIV_H */
46#endif /* HAVE_AUTHORIZATION_H */
db1f069b
MS
47#ifdef HAVE_SYS_PARAM_H
48# include <sys/param.h>
49#endif /* HAVE_SYS_PARAM_H */
bc44d920 50#ifdef HAVE_SYS_UCRED_H
51# include <sys/ucred.h>
52typedef struct xucred cupsd_ucred_t;
53# define CUPSD_UCRED_UID(c) (c).cr_uid
54#else
5a9febac 55# ifndef __OpenBSD__
bc44d920 56typedef struct ucred cupsd_ucred_t;
5a9febac
MS
57# else
58typedef struct sockpeercred cupsd_ucred_t;
59# endif
bc44d920 60# define CUPSD_UCRED_UID(c) (c).uid
61#endif /* HAVE_SYS_UCRED_H */
ef416fc2 62
63
64/*
65 * Local functions...
66 */
67
f7deaa1a 68#ifdef HAVE_AUTHORIZATION_H
69static int check_authref(cupsd_client_t *con, const char *right);
70#endif /* HAVE_AUTHORIZATION_H */
bd7854cb 71static int compare_locations(cupsd_location_t *a,
72 cupsd_location_t *b);
10d09e33 73static cupsd_authmask_t *copy_authmask(cupsd_authmask_t *am, void *data);
10d09e33 74static void free_authmask(cupsd_authmask_t *am, void *data);
ef416fc2 75#if HAVE_LIBPAM
76static int pam_func(int, const struct pam_message **,
77 struct pam_response **, void *);
5a1d7a17 78#else
ef416fc2 79static void to64(char *s, unsigned long v, int n);
80#endif /* HAVE_LIBPAM */
81
82
83/*
84 * Local structures...
85 */
86
87#if HAVE_LIBPAM
88typedef struct cupsd_authdata_s /**** Authentication data ****/
89{
3e7fe0ca
MS
90 char username[HTTP_MAX_VALUE], /* Username string */
91 password[HTTP_MAX_VALUE]; /* Password string */
ef416fc2 92} cupsd_authdata_t;
93#endif /* HAVE_LIBPAM */
94
95
ef416fc2 96/*
10d09e33 97 * 'cupsdAddIPMask()' - Add an IP address authorization mask.
ef416fc2 98 */
99
10d09e33
MS
100int /* O - 1 on success, 0 on failure */
101cupsdAddIPMask(
102 cups_array_t **masks, /* IO - Masks array (created as needed) */
103 const unsigned address[4], /* I - IP address */
104 const unsigned netmask[4]) /* I - IP netmask */
ef416fc2 105{
10d09e33 106 cupsd_authmask_t temp; /* New host/domain mask */
ef416fc2 107
108
ffe32673 109 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]);
ef416fc2 110
10d09e33
MS
111 temp.type = CUPSD_AUTH_IP;
112 memcpy(temp.mask.ip.address, address, sizeof(temp.mask.ip.address));
113 memcpy(temp.mask.ip.netmask, netmask, sizeof(temp.mask.ip.netmask));
ef416fc2 114
bd7854cb 115 /*
10d09e33 116 * Create the masks array as needed and add...
bd7854cb 117 */
118
10d09e33
MS
119 if (!*masks)
120 *masks = cupsArrayNew3(NULL, NULL, NULL, 0,
121 (cups_acopy_func_t)copy_authmask,
122 (cups_afree_func_t)free_authmask);
ef416fc2 123
10d09e33
MS
124 return (cupsArrayAdd(*masks, &temp));
125}
ef416fc2 126
bd7854cb 127
10d09e33
MS
128/*
129 * 'cupsdAddLocation()' - Add a location for authorization.
130 */
ef416fc2 131
10d09e33
MS
132void
133cupsdAddLocation(cupsd_location_t *loc) /* I - Location to add */
134{
ef416fc2 135 /*
10d09e33 136 * Make sure the locations array is created...
ef416fc2 137 */
138
10d09e33
MS
139 if (!Locations)
140 Locations = cupsArrayNew3((cups_array_func_t)compare_locations, NULL,
141 (cups_ahash_func_t)NULL, 0,
142 (cups_acopy_func_t)NULL,
143 (cups_afree_func_t)cupsdFreeLocation);
144
145 if (Locations)
146 {
147 cupsArrayAdd(Locations, loc);
148
ffe32673 149 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdAddLocation: Added location \"%s\"", loc->location ? loc->location : "(null)");
10d09e33 150 }
ef416fc2 151}
152
153
154/*
155 * 'cupsdAddName()' - Add a name to a location...
156 */
157
158void
159cupsdAddName(cupsd_location_t *loc, /* I - Location to add to */
160 char *name) /* I - Name to add */
161{
ffe32673 162 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdAddName(loc=%p, name=\"%s\")", loc, name);
ef416fc2 163
10d09e33
MS
164 if (!loc->names)
165 loc->names = cupsArrayNew3(NULL, NULL, NULL, 0,
166 (cups_acopy_func_t)_cupsStrAlloc,
167 (cups_afree_func_t)_cupsStrFree);
ef416fc2 168
10d09e33 169 if (!cupsArrayAdd(loc->names, name))
ef416fc2 170 {
171 cupsdLogMessage(CUPSD_LOG_ERROR,
172 "Unable to duplicate name for location %s: %s",
e1d6a774 173 loc->location ? loc->location : "nil", strerror(errno));
ef416fc2 174 return;
175 }
ef416fc2 176}
177
178
179/*
10d09e33 180 * 'cupsdAddNameMask()' - Add a host or interface name authorization mask.
ef416fc2 181 */
182
10d09e33
MS
183int /* O - 1 on success, 0 on failure */
184cupsdAddNameMask(cups_array_t **masks, /* IO - Masks array (created as needed) */
185 char *name) /* I - Host or interface name */
ef416fc2 186{
10d09e33 187 cupsd_authmask_t temp; /* New host/domain mask */
ef416fc2 188 char ifname[32], /* Interface name */
189 *ifptr; /* Pointer to end of name */
190
191
ffe32673 192 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdAddNameMask(masks=%p(%p), name=\"%s\")", masks, *masks, name);
ef416fc2 193
88f9aafc 194 if (!_cups_strcasecmp(name, "@LOCAL"))
ef416fc2 195 {
196 /*
10d09e33 197 * Deny *interface*...
ef416fc2 198 */
199
10d09e33
MS
200 temp.type = CUPSD_AUTH_INTERFACE;
201 temp.mask.name.name = (char *)"*";
ef416fc2 202 }
88f9aafc 203 else if (!_cups_strncasecmp(name, "@IF(", 4))
ef416fc2 204 {
205 /*
10d09e33 206 * Deny *interface*...
ef416fc2 207 */
208
209 strlcpy(ifname, name + 4, sizeof(ifname));
210
10d09e33 211 ifptr = ifname + strlen(ifname) - 1;
ef416fc2 212
10d09e33 213 if (ifptr >= ifname && *ifptr == ')')
ef416fc2 214 {
215 ifptr --;
216 *ifptr = '\0';
217 }
218
10d09e33
MS
219 temp.type = CUPSD_AUTH_INTERFACE;
220 temp.mask.name.name = ifname;
ef416fc2 221 }
222 else
223 {
224 /*
10d09e33 225 * Deny name...
ef416fc2 226 */
227
10d09e33
MS
228 if (*name == '*')
229 name ++;
ef416fc2 230
10d09e33
MS
231 temp.type = CUPSD_AUTH_NAME;
232 temp.mask.name.name = (char *)name;
233 }
ef416fc2 234
10d09e33
MS
235 /*
236 * Set the name length...
237 */
ef416fc2 238
10d09e33 239 temp.mask.name.length = strlen(temp.mask.name.name);
ef416fc2 240
10d09e33
MS
241 /*
242 * Create the masks array as needed and add...
243 */
ef416fc2 244
10d09e33
MS
245 if (!*masks)
246 *masks = cupsArrayNew3(NULL, NULL, NULL, 0,
247 (cups_acopy_func_t)copy_authmask,
248 (cups_afree_func_t)free_authmask);
ef416fc2 249
10d09e33 250 return (cupsArrayAdd(*masks, &temp));
ef416fc2 251}
252
253
254/*
255 * 'cupsdAuthorize()' - Validate any authorization credentials.
256 */
257
258void
259cupsdAuthorize(cupsd_client_t *con) /* I - Client connection */
260{
261 int type; /* Authentication type */
f7deaa1a 262 const char *authorization; /* Pointer into Authorization string */
263 char *ptr, /* Pointer into string */
3e7fe0ca
MS
264 username[HTTP_MAX_VALUE],
265 /* Username string */
266 password[HTTP_MAX_VALUE];
267 /* Password string */
db0bd74a 268 cupsd_cert_t *localuser; /* Certificate username */
ef416fc2 269
270
271 /*
272 * Locate the best matching location so we know what kind of
273 * authentication to expect...
274 */
275
996acce8 276 con->best = cupsdFindBest(con->uri, httpGetState(con->http));
5bd77a73 277 con->type = CUPSD_AUTH_NONE;
ef416fc2 278
ffe32673 279 cupsdLogClient(con, CUPSD_LOG_DEBUG2, "con->uri=\"%s\", con->best=%p(%s)", con->uri, con->best, con->best ? con->best->location : "");
ef416fc2 280
5bd77a73 281 if (con->best && con->best->type != CUPSD_AUTH_NONE)
7ff4fea9 282 {
5bd77a73 283 if (con->best->type == CUPSD_AUTH_DEFAULT)
dcb445bc 284 type = cupsdDefaultAuthType();
7ff4fea9
MS
285 else
286 type = con->best->type;
287 }
ef416fc2 288 else
dcb445bc 289 type = cupsdDefaultAuthType();
ef416fc2 290
291 /*
292 * Decode the Authorization string...
293 */
294
996acce8 295 authorization = httpGetField(con->http, HTTP_FIELD_AUTHORIZATION);
ef416fc2 296
ef416fc2 297 username[0] = '\0';
298 password[0] = '\0';
299
07ed0e9a
MS
300#ifdef HAVE_GSSAPI
301 con->gss_uid = 0;
302#endif /* HAVE_GSSAPI */
303
f7deaa1a 304#ifdef HAVE_AUTHORIZATION_H
305 if (con->authref)
306 {
307 AuthorizationFree(con->authref, kAuthorizationFlagDefaults);
308 con->authref = NULL;
309 }
310#endif /* HAVE_AUTHORIZATION_H */
311
2fb76298 312 if (!*authorization)
ef416fc2 313 {
314 /*
315 * No authorization data provided, return early...
316 */
317
ffe32673 318 cupsdLogClient(con, CUPSD_LOG_DEBUG, "No authentication data provided.");
ef416fc2 319 return;
320 }
f7deaa1a 321#ifdef HAVE_AUTHORIZATION_H
c8fef167 322 else if (!strncmp(authorization, "AuthRef ", 8) &&
5ec1fd3d 323 httpAddrLocalhost(httpGetAddress(con->http)))
f7deaa1a 324 {
325 OSStatus status; /* Status */
e0660879
MS
326 char authdata[HTTP_MAX_VALUE];
327 /* Nonce value from client */
f7deaa1a 328 int authlen; /* Auth string length */
329 AuthorizationItemSet *authinfo; /* Authorization item set */
330
331 /*
332 * Get the Authorization Services data...
333 */
334
c8fef167 335 authorization += 8;
f7deaa1a 336 while (isspace(*authorization & 255))
337 authorization ++;
338
e0660879
MS
339 authlen = sizeof(authdata);
340 httpDecode64_2(authdata, &authlen, authorization);
f7deaa1a 341
342 if (authlen != kAuthorizationExternalFormLength)
343 {
ffe32673 344 cupsdLogClient(con, CUPSD_LOG_ERROR, "External Authorization reference size is incorrect.");
f7deaa1a 345 return;
346 }
347
e0660879 348 if ((status = AuthorizationCreateFromExternalForm((AuthorizationExternalForm *)authdata, &con->authref)) != 0)
f7deaa1a 349 {
ffe32673 350 cupsdLogClient(con, CUPSD_LOG_ERROR, "AuthorizationCreateFromExternalForm returned %d (%s)", (int)status, cssmErrorString(status));
f7deaa1a 351 return;
352 }
353
c8fef167 354 username[0] = '\0';
ed6e7faf 355
c8fef167 356 if (!AuthorizationCopyInfo(con->authref, kAuthorizationEnvironmentUsername,
bf3816c7 357 &authinfo))
e4572d57 358 {
ed6e7faf
MS
359 if (authinfo->count == 1 && authinfo->items[0].value &&
360 authinfo->items[0].valueLength >= 2)
c8fef167 361 {
ed6e7faf
MS
362 strlcpy(username, authinfo->items[0].value, sizeof(username));
363
ffe32673 364 cupsdLogClient(con, CUPSD_LOG_DEBUG, "Authorized as \"%s\" using AuthRef.", username);
c8fef167
MS
365 }
366
e4572d57 367 AuthorizationFreeItemSet(authinfo);
e4572d57 368 }
355e94dc 369
c8fef167
MS
370 if (!username[0])
371 {
372 /*
373 * No username in AuthRef, grab username using peer credentials...
374 */
375
376 struct passwd *pwd; /* Password entry for this user */
377 cupsd_ucred_t peercred; /* Peer credentials */
378 socklen_t peersize; /* Size of peer credentials */
379
380 peersize = sizeof(peercred);
381
996acce8 382 if (getsockopt(httpGetFd(con->http), 0, LOCAL_PEERCRED, &peercred, &peersize))
c8fef167 383 {
ffe32673 384 cupsdLogClient(con, CUPSD_LOG_ERROR, "Unable to get peer credentials - %s", strerror(errno));
c8fef167
MS
385 return;
386 }
387
388 if ((pwd = getpwuid(CUPSD_UCRED_UID(peercred))) == NULL)
389 {
ffe32673 390 cupsdLogClient(con, CUPSD_LOG_ERROR, "Unable to find UID %d for peer credentials.", (int)CUPSD_UCRED_UID(peercred));
c8fef167
MS
391 return;
392 }
393
394 strlcpy(username, pwd->pw_name, sizeof(username));
395
ffe32673 396 cupsdLogClient(con, CUPSD_LOG_DEBUG, "Authorized as \"%s\" using AuthRef + PeerCred.", username);
c8fef167
MS
397 }
398
5bd77a73 399 con->type = CUPSD_AUTH_BASIC;
f7deaa1a 400 }
401#endif /* HAVE_AUTHORIZATION_H */
bc44d920 402#if defined(SO_PEERCRED) && defined(AF_LOCAL)
403 else if (!strncmp(authorization, "PeerCred ", 9) &&
0d40cb1e 404 con->http->hostaddr->addr.sa_family == AF_LOCAL && con->best)
bc44d920 405 {
406 /*
407 * Use peer credentials from domain socket connection...
408 */
409
410 struct passwd *pwd; /* Password entry for this user */
411 cupsd_ucred_t peercred; /* Peer credentials */
412 socklen_t peersize; /* Size of peer credentials */
f14324a7
MS
413#ifdef HAVE_AUTHORIZATION_H
414 const char *name; /* Authorizing name */
6961465f
MS
415 int no_peer = 0; /* Don't allow peer credentials? */
416
417 /*
418 * See if we should allow peer credentials...
419 */
bc44d920 420
f14324a7
MS
421 for (name = (char *)cupsArrayFirst(con->best->names);
422 name;
423 name = (char *)cupsArrayNext(con->best->names))
6961465f 424 {
dcb445bc
MS
425 if (!_cups_strncasecmp(name, "@AUTHKEY(", 9) ||
426 !_cups_strcasecmp(name, "@SYSTEM"))
f14324a7 427 {
6961465f
MS
428 /* Normally don't want peer credentials if we need an auth key... */
429 no_peer = 1;
430 }
431 else if (!_cups_strcasecmp(name, "@OWNER"))
432 {
433 /* but if @OWNER is present then we allow it... */
434 no_peer = 0;
435 break;
f14324a7 436 }
6961465f
MS
437 }
438
439 if (no_peer)
440 {
ffe32673 441 cupsdLogClient(con, CUPSD_LOG_ERROR, "PeerCred authentication not allowed for resource per AUTHKEY policy.");
6961465f
MS
442 return;
443 }
f14324a7 444#endif /* HAVE_AUTHORIZATION_H */
bc44d920 445
446 if ((pwd = getpwnam(authorization + 9)) == NULL)
447 {
ffe32673 448 cupsdLogClient(con, CUPSD_LOG_ERROR, "User \"%s\" does not exist.", authorization + 9);
bc44d920 449 return;
450 }
451
452 peersize = sizeof(peercred);
453
f11a948a 454# ifdef __APPLE__
996acce8 455 if (getsockopt(httpGetFd(con->http), 0, LOCAL_PEERCRED, &peercred, &peersize))
f11a948a 456# else
996acce8 457 if (getsockopt(httpGetFd(con->http), SOL_SOCKET, SO_PEERCRED, &peercred, &peersize))
f11a948a 458# endif /* __APPLE__ */
bc44d920 459 {
ffe32673 460 cupsdLogClient(con, CUPSD_LOG_ERROR, "Unable to get peer credentials - %s", strerror(errno));
bc44d920 461 return;
462 }
463
464 if (pwd->pw_uid != CUPSD_UCRED_UID(peercred))
465 {
ffe32673 466 cupsdLogClient(con, CUPSD_LOG_ERROR, "Invalid peer credentials for \"%s\" - got %d, expected %d.", authorization + 9, CUPSD_UCRED_UID(peercred), pwd->pw_uid);
bc44d920 467# ifdef HAVE_SYS_UCRED_H
ffe32673
MS
468 cupsdLogClient(con, CUPSD_LOG_DEBUG2, "cr_version=%d", peercred.cr_version);
469 cupsdLogClient(con, CUPSD_LOG_DEBUG2, "cr_uid=%d", peercred.cr_uid);
470 cupsdLogClient(con, CUPSD_LOG_DEBUG2, "cr_ngroups=%d", peercred.cr_ngroups);
471 cupsdLogClient(con, CUPSD_LOG_DEBUG2, "cr_groups[0]=%d", peercred.cr_groups[0]);
bc44d920 472# endif /* HAVE_SYS_UCRED_H */
473 return;
474 }
475
476 strlcpy(username, authorization + 9, sizeof(username));
355e94dc 477
eac3a0a0
MS
478# ifdef HAVE_GSSAPI
479 con->gss_uid = CUPSD_UCRED_UID(peercred);
480# endif /* HAVE_GSSAPI */
481
ffe32673 482 cupsdLogClient(con, CUPSD_LOG_DEBUG, "Authorized as %s using PeerCred.", username);
2fb76298 483
5bd77a73 484 con->type = CUPSD_AUTH_BASIC;
bc44d920 485 }
486#endif /* SO_PEERCRED && AF_LOCAL */
ef416fc2 487 else if (!strncmp(authorization, "Local", 5) &&
5ec1fd3d 488 httpAddrLocalhost(httpGetAddress(con->http)))
ef416fc2 489 {
490 /*
491 * Get Local certificate authentication data...
492 */
493
494 authorization += 5;
80ca4592 495 while (isspace(*authorization & 255))
ef416fc2 496 authorization ++;
497
0fa6c7fa 498 if ((localuser = cupsdFindCert(authorization)) == NULL)
ef416fc2 499 {
ffe32673 500 cupsdLogClient(con, CUPSD_LOG_ERROR, "Local authentication certificate not found.");
ef416fc2 501 return;
502 }
2fb76298 503
0fa6c7fa
MS
504 strlcpy(username, localuser->username, sizeof(username));
505 con->type = localuser->type;
506
ffe32673 507 cupsdLogClient(con, CUPSD_LOG_DEBUG, "Authorized as %s using Local.", username);
ef416fc2 508 }
2fb76298 509 else if (!strncmp(authorization, "Basic", 5))
ef416fc2 510 {
511 /*
512 * Get the Basic authentication data...
513 */
514
515 int userlen; /* Username:password length */
516
517
518 authorization += 5;
80ca4592 519 while (isspace(*authorization & 255))
ef416fc2 520 authorization ++;
521
522 userlen = sizeof(username);
523 httpDecode64_2(username, &userlen, authorization);
524
525 /*
526 * Pull the username and password out...
527 */
528
529 if ((ptr = strchr(username, ':')) == NULL)
530 {
ffe32673 531 cupsdLogClient(con, CUPSD_LOG_ERROR, "Missing Basic password.");
ef416fc2 532 return;
533 }
534
535 *ptr++ = '\0';
536
537 if (!username[0])
538 {
539 /*
540 * Username must not be empty...
541 */
542
ffe32673 543 cupsdLogClient(con, CUPSD_LOG_ERROR, "Empty Basic username.");
ef416fc2 544 return;
545 }
546
547 if (!*ptr)
548 {
549 /*
550 * Password must not be empty...
551 */
552
ffe32673 553 cupsdLogClient(con, CUPSD_LOG_ERROR, "Empty Basic password.");
ef416fc2 554 return;
555 }
556
557 strlcpy(password, ptr, sizeof(password));
558
559 /*
560 * Validate the username and password...
561 */
562
563 switch (type)
564 {
2fb76298 565 default :
5bd77a73 566 case CUPSD_AUTH_BASIC :
ef416fc2 567 {
568#if HAVE_LIBPAM
569 /*
570 * Only use PAM to do authentication. This supports MD5
571 * passwords, among other things...
572 */
573
574 pam_handle_t *pamh; /* PAM authentication handle */
575 int pamerr; /* PAM error code */
576 struct pam_conv pamdata;/* PAM conversation data */
577 cupsd_authdata_t data; /* Authentication data */
578
579
580 strlcpy(data.username, username, sizeof(data.username));
581 strlcpy(data.password, password, sizeof(data.password));
582
5a1d7a17 583# ifdef __sun
e1d6a774 584 pamdata.conv = (int (*)(int, struct pam_message **,
585 struct pam_response **,
586 void *))pam_func;
587# else
ef416fc2 588 pamdata.conv = pam_func;
5a1d7a17 589# endif /* __sun */
ef416fc2 590 pamdata.appdata_ptr = &data;
591
ef416fc2 592 pamerr = pam_start("cups", username, &pamdata, &pamh);
593 if (pamerr != PAM_SUCCESS)
594 {
ffe32673 595 cupsdLogClient(con, CUPSD_LOG_ERROR, "pam_start() returned %d (%s)", pamerr, pam_strerror(pamh, pamerr));
ef416fc2 596 return;
597 }
598
ee571f26
MS
599# ifdef HAVE_PAM_SET_ITEM
600# ifdef PAM_RHOST
996acce8 601 pamerr = pam_set_item(pamh, PAM_RHOST, con->http->hostname);
a4924f6c 602 if (pamerr != PAM_SUCCESS)
ffe32673 603 cupsdLogClient(con, CUPSD_LOG_WARN, "pam_set_item(PAM_RHOST) returned %d (%s)", pamerr, pam_strerror(pamh, pamerr));
ee571f26
MS
604# endif /* PAM_RHOST */
605
606# ifdef PAM_TTY
607 pamerr = pam_set_item(pamh, PAM_TTY, "cups");
608 if (pamerr != PAM_SUCCESS)
ffe32673 609 cupsdLogClient(con, CUPSD_LOG_WARN, "pam_set_item(PAM_TTY) returned %d (%s)", pamerr, pam_strerror(pamh, pamerr));
ee571f26
MS
610# endif /* PAM_TTY */
611# endif /* HAVE_PAM_SET_ITEM */
612
ef416fc2 613 pamerr = pam_authenticate(pamh, PAM_SILENT);
614 if (pamerr != PAM_SUCCESS)
615 {
ffe32673 616 cupsdLogClient(con, CUPSD_LOG_ERROR, "pam_authenticate() returned %d (%s)", pamerr, pam_strerror(pamh, pamerr));
ef416fc2 617 pam_end(pamh, 0);
618 return;
619 }
620
94da7e34
MS
621# ifdef HAVE_PAM_SETCRED
622 pamerr = pam_setcred(pamh, PAM_ESTABLISH_CRED | PAM_SILENT);
623 if (pamerr != PAM_SUCCESS)
ffe32673 624 cupsdLogClient(con, CUPSD_LOG_WARN, "pam_setcred() returned %d (%s)", pamerr, pam_strerror(pamh, pamerr));
94da7e34
MS
625# endif /* HAVE_PAM_SETCRED */
626
ef416fc2 627 pamerr = pam_acct_mgmt(pamh, PAM_SILENT);
628 if (pamerr != PAM_SUCCESS)
629 {
ffe32673 630 cupsdLogClient(con, CUPSD_LOG_ERROR, "pam_acct_mgmt() returned %d (%s)", pamerr, pam_strerror(pamh, pamerr));
ef416fc2 631 pam_end(pamh, 0);
632 return;
633 }
634
635 pam_end(pamh, PAM_SUCCESS);
636
ef416fc2 637#else
638 /*
639 * Use normal UNIX password file-based authentication...
640 */
641
642 char *pass; /* Encrypted password */
643 struct passwd *pw; /* User password data */
644# ifdef HAVE_SHADOW_H
645 struct spwd *spw; /* Shadow password data */
646# endif /* HAVE_SHADOW_H */
647
648
649 pw = getpwnam(username); /* Get the current password */
650 endpwent(); /* Close the password file */
651
652 if (!pw)
653 {
654 /*
655 * No such user...
656 */
657
ffe32673 658 cupsdLogClient(con, CUPSD_LOG_ERROR, "Unknown username \"%s\".", username);
80ca4592 659 return;
ef416fc2 660 }
661
662# ifdef HAVE_SHADOW_H
663 spw = getspnam(username);
664 endspent();
665
666 if (!spw && !strcmp(pw->pw_passwd, "x"))
667 {
668 /*
669 * Don't allow blank passwords!
670 */
671
ffe32673 672 cupsdLogClient(con, CUPSD_LOG_ERROR, "Username \"%s\" has no shadow password.", username);
ef416fc2 673 return;
674 }
675
676 if (spw && !spw->sp_pwdp[0] && !pw->pw_passwd[0])
677# else
678 if (!pw->pw_passwd[0])
679# endif /* HAVE_SHADOW_H */
680 {
681 /*
682 * Don't allow blank passwords!
683 */
684
ffe32673 685 cupsdLogClient(con, CUPSD_LOG_ERROR, "Username \"%s\" has no password.", username);
ef416fc2 686 return;
687 }
688
689 /*
690 * OK, the password isn't blank, so compare with what came from the
691 * client...
692 */
693
570933a6 694 pass = crypt(password, pw->pw_passwd);
ef416fc2 695
ef416fc2 696 if (!pass || strcmp(pw->pw_passwd, pass))
697 {
698# ifdef HAVE_SHADOW_H
699 if (spw)
700 {
570933a6 701 pass = crypt(password, spw->sp_pwdp);
ef416fc2 702
ef416fc2 703 if (pass == NULL || strcmp(spw->sp_pwdp, pass))
704 {
ffe32673 705 cupsdLogClient(con, CUPSD_LOG_ERROR, "Authentication failed for user \"%s\".", username);
ef416fc2 706 return;
707 }
708 }
709 else
710# endif /* HAVE_SHADOW_H */
711 {
ffe32673 712 cupsdLogClient(con, CUPSD_LOG_ERROR, "Authentication failed for user \"%s\".", username);
ef416fc2 713 return;
714 }
715 }
716#endif /* HAVE_LIBPAM */
717 }
355e94dc 718
ffe32673 719 cupsdLogClient(con, CUPSD_LOG_DEBUG, "Authorized as \"%s\" using Basic.", username);
ef416fc2 720 break;
ef416fc2 721 }
2fb76298
MS
722
723 con->type = type;
ef416fc2 724 }
f7deaa1a 725#ifdef HAVE_GSSAPI
c8fef167 726 else if (!strncmp(authorization, "Negotiate", 9))
f7deaa1a 727 {
728 int len; /* Length of authorization string */
f7deaa1a 729 gss_ctx_id_t context; /* Authorization context */
730 OM_uint32 major_status, /* Major status code */
731 minor_status; /* Minor status code */
732 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER,
733 /* Input token from string */
734 output_token = GSS_C_EMPTY_BUFFER;
735 /* Output token for username */
736 gss_name_t client_name; /* Client name */
737
738
f42414bf 739# ifdef __APPLE__
740 /*
741 * If the weak-linked GSSAPI/Kerberos library is not present, don't try
742 * to use it...
743 */
744
d2ff4622 745 if (&gss_init_sec_context == NULL)
f42414bf 746 {
ffe32673 747 cupsdLogClient(con, CUPSD_LOG_WARN, "GSSAPI/Kerberos authentication failed because the Kerberos framework is not present.");
f42414bf 748 return;
749 }
750# endif /* __APPLE__ */
751
f7deaa1a 752 /*
753 * Find the start of the Kerberos input token...
754 */
755
756 authorization += 9;
757 while (isspace(*authorization & 255))
758 authorization ++;
759
760 if (!*authorization)
761 {
ffe32673 762 cupsdLogClient(con, CUPSD_LOG_DEBUG2, "No authentication data specified.");
f7deaa1a 763 return;
764 }
765
f7deaa1a 766 /*
767 * Decode the authorization string to get the input token...
768 */
769
7e86f2f6
MS
770 len = (int)strlen(authorization);
771 input_token.value = malloc((size_t)len);
f7deaa1a 772 input_token.value = httpDecode64_2(input_token.value, &len,
c7017ecc 773 authorization);
7e86f2f6 774 input_token.length = (size_t)len;
f7deaa1a 775
776 /*
777 * Accept the input token to get the authorization info...
778 */
779
780 context = GSS_C_NO_CONTEXT;
781 client_name = GSS_C_NO_NAME;
782 major_status = gss_accept_sec_context(&minor_status,
783 &context,
dcb445bc 784 ServerCreds,
f7deaa1a 785 &input_token,
786 GSS_C_NO_CHANNEL_BINDINGS,
787 &client_name,
788 NULL,
07ed0e9a
MS
789 &output_token,
790 NULL,
f7deaa1a 791 NULL,
07ed0e9a
MS
792 NULL);
793
794 if (output_token.length > 0)
795 gss_release_buffer(&minor_status, &output_token);
f7deaa1a 796
797 if (GSS_ERROR(major_status))
798 {
ffe32673 799 cupsdLogGSSMessage(CUPSD_LOG_DEBUG, major_status, minor_status, "[Client %d] Error accepting GSSAPI security context.", con->number);
f7deaa1a 800
801 if (context != GSS_C_NO_CONTEXT)
802 gss_delete_sec_context(&minor_status, &context, GSS_C_NO_BUFFER);
f7deaa1a 803 return;
804 }
805
97c9a8d7
MS
806 con->have_gss = 1;
807
76cd9e37
MS
808 /*
809 * Get the username associated with the client's credentials...
f7deaa1a 810 */
811
355e94dc 812 if (major_status == GSS_S_CONTINUE_NEEDED)
ffe32673 813 cupsdLogGSSMessage(CUPSD_LOG_DEBUG, major_status, minor_status, "[Client %d] Credentials not complete.", con->number);
355e94dc 814 else if (major_status == GSS_S_COMPLETE)
f7deaa1a 815 {
c8fef167 816 major_status = gss_display_name(&minor_status, client_name,
f7deaa1a 817 &output_token, NULL);
818
819 if (GSS_ERROR(major_status))
820 {
ffe32673 821 cupsdLogGSSMessage(CUPSD_LOG_DEBUG, major_status, minor_status, "[Client %d] Error getting username.", con->number);
f7deaa1a 822 gss_release_name(&minor_status, &client_name);
823 gss_delete_sec_context(&minor_status, &context, GSS_C_NO_BUFFER);
f7deaa1a 824 return;
825 }
826
f7deaa1a 827 strlcpy(username, output_token.value, sizeof(username));
355e94dc 828
ffe32673 829 cupsdLogClient(con, CUPSD_LOG_DEBUG, "Authorized as \"%s\" using Negotiate.", username);
f7deaa1a 830
e07d4801 831 gss_release_name(&minor_status, &client_name);
f7deaa1a 832 gss_release_buffer(&minor_status, &output_token);
2fb76298 833
5bd77a73 834 con->type = CUPSD_AUTH_NEGOTIATE;
f7deaa1a 835 }
e07d4801
MS
836
837 gss_delete_sec_context(&minor_status, &context, GSS_C_NO_BUFFER);
07ed0e9a
MS
838
839# if defined(SO_PEERCRED) && defined(AF_LOCAL)
840 /*
841 * Get the client's UID if we are printing locally - that allows a backend
842 * to run as the correct user to get Kerberos credentials of its own.
843 */
844
5ec1fd3d 845 if (httpAddrFamily(con->http->hostaddr) == AF_LOCAL)
07ed0e9a
MS
846 {
847 cupsd_ucred_t peercred; /* Peer credentials */
848 socklen_t peersize; /* Size of peer credentials */
849
850 peersize = sizeof(peercred);
851
852# ifdef __APPLE__
996acce8 853 if (getsockopt(httpGetFd(con->http), 0, LOCAL_PEERCRED, &peercred, &peersize))
07ed0e9a 854# else
996acce8 855 if (getsockopt(httpGetFd(con->http), SOL_SOCKET, SO_PEERCRED, &peercred,
07ed0e9a
MS
856 &peersize))
857# endif /* __APPLE__ */
858 {
ffe32673 859 cupsdLogClient(con, CUPSD_LOG_ERROR, "Unable to get peer credentials - %s", strerror(errno));
07ed0e9a
MS
860 }
861 else
862 {
ffe32673 863 cupsdLogClient(con, CUPSD_LOG_DEBUG, "Using credentials for UID %d.", CUPSD_UCRED_UID(peercred));
07ed0e9a
MS
864 con->gss_uid = CUPSD_UCRED_UID(peercred);
865 }
866 }
867# endif /* SO_PEERCRED && AF_LOCAL */
f7deaa1a 868 }
869#endif /* HAVE_GSSAPI */
2fb76298 870 else
ef416fc2 871 {
355e94dc 872 char scheme[256]; /* Auth scheme... */
355e94dc
MS
873
874
875 if (sscanf(authorization, "%255s", scheme) != 1)
5a9febac 876 strlcpy(scheme, "UNKNOWN", sizeof(scheme));
355e94dc 877
ffe32673 878 cupsdLogClient(con, CUPSD_LOG_ERROR, "Bad authentication data \"%s ...\".", scheme);
ef416fc2 879 return;
880 }
881
882 /*
883 * If we get here, then we were able to validate the username and
884 * password - copy the validated username and password to the client
885 * data and return...
886 */
887
888 strlcpy(con->username, username, sizeof(con->username));
889 strlcpy(con->password, password, sizeof(con->password));
ef416fc2 890}
891
892
080811b1
MS
893/*
894 * 'cupsdCheckAccess()' - Check whether the given address is allowed to
895 * access a location.
896 */
897
898int /* O - 1 if allowed, 0 otherwise */
899cupsdCheckAccess(
900 unsigned ip[4], /* I - Client address */
5ec1fd3d 901 const char *name, /* I - Client hostname */
7e86f2f6 902 size_t namelen, /* I - Length of hostname */
080811b1
MS
903 cupsd_location_t *loc) /* I - Location to check */
904{
905 int allow; /* 1 if allowed, 0 otherwise */
906
907
88f9aafc 908 if (!_cups_strcasecmp(name, "localhost"))
080811b1
MS
909 {
910 /*
911 * Access from localhost (127.0.0.1 or ::1) is always allowed...
912 */
913
914 return (1);
915 }
916 else
917 {
918 /*
919 * Do authorization checks on the domain/address...
920 */
921
922 switch (loc->order_type)
923 {
924 default :
925 allow = 0; /* anti-compiler-warning-code */
926 break;
927
5bd77a73 928 case CUPSD_AUTH_ALLOW : /* Order Deny,Allow */
080811b1
MS
929 allow = 1;
930
10d09e33 931 if (cupsdCheckAuth(ip, name, namelen, loc->deny))
080811b1
MS
932 allow = 0;
933
10d09e33 934 if (cupsdCheckAuth(ip, name, namelen, loc->allow))
080811b1
MS
935 allow = 1;
936 break;
937
5bd77a73 938 case CUPSD_AUTH_DENY : /* Order Allow,Deny */
080811b1
MS
939 allow = 0;
940
10d09e33 941 if (cupsdCheckAuth(ip, name, namelen, loc->allow))
080811b1
MS
942 allow = 1;
943
10d09e33 944 if (cupsdCheckAuth(ip, name, namelen, loc->deny))
080811b1
MS
945 allow = 0;
946 break;
947 }
948 }
949
950 return (allow);
951}
952
953
ef416fc2 954/*
955 * 'cupsdCheckAuth()' - Check authorization masks.
956 */
957
958int /* O - 1 if mask matches, 0 otherwise */
10d09e33 959cupsdCheckAuth(unsigned ip[4], /* I - Client address */
5ec1fd3d 960 const char *name, /* I - Client hostname */
7e86f2f6 961 size_t name_len, /* I - Length of hostname */
10d09e33 962 cups_array_t *masks) /* I - Masks */
ef416fc2 963{
10d09e33
MS
964 int i; /* Looping var */
965 cupsd_authmask_t *mask; /* Current mask */
966 cupsd_netif_t *iface; /* Network interface */
967 unsigned netip4; /* IPv4 network address */
ef416fc2 968#ifdef AF_INET6
10d09e33 969 unsigned netip6[4]; /* IPv6 network address */
ef416fc2 970#endif /* AF_INET6 */
971
e07d4801 972
10d09e33
MS
973 for (mask = (cupsd_authmask_t *)cupsArrayFirst(masks);
974 mask;
975 mask = (cupsd_authmask_t *)cupsArrayNext(masks))
ef416fc2 976 {
10d09e33 977 switch (mask->type)
ef416fc2 978 {
5bd77a73 979 case CUPSD_AUTH_INTERFACE :
ef416fc2 980 /*
981 * Check for a match with a network interface...
982 */
983
984 netip4 = htonl(ip[3]);
985
986#ifdef AF_INET6
987 netip6[0] = htonl(ip[0]);
988 netip6[1] = htonl(ip[1]);
989 netip6[2] = htonl(ip[2]);
990 netip6[3] = htonl(ip[3]);
991#endif /* AF_INET6 */
992
84ec3a84
MS
993 cupsdNetIFUpdate();
994
10d09e33 995 if (!strcmp(mask->mask.name.name, "*"))
ef416fc2 996 {
e07d4801
MS
997#ifdef __APPLE__
998 /*
999 * Allow Back-to-My-Mac addresses...
1000 */
1001
1002 if ((ip[0] & 0xff000000) == 0xfd000000)
1003 return (1);
1004#endif /* __APPLE__ */
1005
ef416fc2 1006 /*
1007 * Check against all local interfaces...
1008 */
1009
e00b005a 1010 for (iface = (cupsd_netif_t *)cupsArrayFirst(NetIFList);
1011 iface;
1012 iface = (cupsd_netif_t *)cupsArrayNext(NetIFList))
ef416fc2 1013 {
1014 /*
1015 * Only check local interfaces...
1016 */
1017
1018 if (!iface->is_local)
1019 continue;
1020
1021 if (iface->address.addr.sa_family == AF_INET)
1022 {
1023 /*
1024 * Check IPv4 address...
1025 */
1026
1027 if ((netip4 & iface->mask.ipv4.sin_addr.s_addr) ==
1028 (iface->address.ipv4.sin_addr.s_addr &
1029 iface->mask.ipv4.sin_addr.s_addr))
1030 return (1);
1031 }
1032#ifdef AF_INET6
1033 else
1034 {
1035 /*
1036 * Check IPv6 address...
1037 */
1038
1039 for (i = 0; i < 4; i ++)
1040 if ((netip6[i] & iface->mask.ipv6.sin6_addr.s6_addr32[i]) !=
1041 (iface->address.ipv6.sin6_addr.s6_addr32[i] &
1042 iface->mask.ipv6.sin6_addr.s6_addr32[i]))
1043 break;
1044
1045 if (i == 4)
1046 return (1);
1047 }
1048#endif /* AF_INET6 */
1049 }
1050 }
1051 else
1052 {
1053 /*
1054 * Check the named interface...
1055 */
1056
e00b005a 1057 for (iface = (cupsd_netif_t *)cupsArrayFirst(NetIFList);
ed486911 1058 iface;
e00b005a 1059 iface = (cupsd_netif_t *)cupsArrayNext(NetIFList))
ef416fc2 1060 {
10d09e33 1061 if (strcmp(mask->mask.name.name, iface->name))
ed486911 1062 continue;
1063
ef416fc2 1064 if (iface->address.addr.sa_family == AF_INET)
1065 {
1066 /*
1067 * Check IPv4 address...
1068 */
1069
1070 if ((netip4 & iface->mask.ipv4.sin_addr.s_addr) ==
1071 (iface->address.ipv4.sin_addr.s_addr &
1072 iface->mask.ipv4.sin_addr.s_addr))
1073 return (1);
1074 }
1075#ifdef AF_INET6
1076 else
1077 {
1078 /*
1079 * Check IPv6 address...
1080 */
1081
1082 for (i = 0; i < 4; i ++)
1083 if ((netip6[i] & iface->mask.ipv6.sin6_addr.s6_addr32[i]) !=
1084 (iface->address.ipv6.sin6_addr.s6_addr32[i] &
1085 iface->mask.ipv6.sin6_addr.s6_addr32[i]))
1086 break;
1087
1088 if (i == 4)
1089 return (1);
1090 }
1091#endif /* AF_INET6 */
1092 }
1093 }
1094 break;
1095
5bd77a73 1096 case CUPSD_AUTH_NAME :
ef416fc2 1097 /*
1098 * Check for exact name match...
1099 */
1100
88f9aafc 1101 if (!_cups_strcasecmp(name, mask->mask.name.name))
ef416fc2 1102 return (1);
1103
1104 /*
1105 * Check for domain match...
1106 */
1107
10d09e33
MS
1108 if (name_len >= mask->mask.name.length &&
1109 mask->mask.name.name[0] == '.' &&
88f9aafc 1110 !_cups_strcasecmp(name + name_len - mask->mask.name.length,
10d09e33 1111 mask->mask.name.name))
ef416fc2 1112 return (1);
1113 break;
1114
5bd77a73 1115 case CUPSD_AUTH_IP :
ef416fc2 1116 /*
1117 * Check for IP/network address match...
1118 */
1119
1120 for (i = 0; i < 4; i ++)
10d09e33
MS
1121 if ((ip[i] & mask->mask.ip.netmask[i]) !=
1122 mask->mask.ip.address[i])
ef416fc2 1123 break;
1124
1125 if (i == 4)
1126 return (1);
1127 break;
1128 }
ef416fc2 1129 }
1130
1131 return (0);
1132}
1133
1134
1135/*
1136 * 'cupsdCheckGroup()' - Check for a user's group membership.
1137 */
1138
1139int /* O - 1 if user is a member, 0 otherwise */
1140cupsdCheckGroup(
1141 const char *username, /* I - User name */
1142 struct passwd *user, /* I - System user info */
1143 const char *groupname) /* I - Group name */
1144{
8922323b 1145 int i; /* Looping var */
afe94dff
MS
1146 struct group *group; /* Group info */
1147 gid_t groupid; /* ID of named group */
bd7854cb 1148#ifdef HAVE_MBR_UID_TO_UUID
8922323b
MS
1149 uuid_t useruuid, /* UUID for username */
1150 groupuuid; /* UUID for groupname */
1151 int is_member; /* True if user is a member of group */
bd7854cb 1152#endif /* HAVE_MBR_UID_TO_UUID */
ef416fc2 1153
1154
ffe32673 1155 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdCheckGroup(username=\"%s\", user=%p, groupname=\"%s\")", username, user, groupname);
ef416fc2 1156
1157 /*
1158 * Validate input...
1159 */
1160
1161 if (!username || !groupname)
1162 return (0);
1163
1164 /*
1165 * Check to see if the user is a member of the named group...
1166 */
1167
1168 group = getgrnam(groupname);
1169 endgrent();
1170
1171 if (group != NULL)
1172 {
1173 /*
1174 * Group exists, check it...
1175 */
1176
afe94dff
MS
1177 groupid = group->gr_gid;
1178
6eb98aee
MS
1179#ifdef HAVE_GETGROUPLIST
1180 if (user)
1181 {
f093225b 1182 int ngroups; /* Number of groups */
fdc3c81a
MS
1183# ifdef __APPLE__
1184 int groups[2048]; /* Groups that user belongs to */
1185# else
f093225b 1186 gid_t groups[2048]; /* Groups that user belongs to */
fdc3c81a 1187# endif /* __APPLE__ */
6eb98aee
MS
1188
1189 ngroups = (int)(sizeof(groups) / sizeof(groups[0]));
fdc3c81a
MS
1190# ifdef __APPLE__
1191 getgrouplist(username, (int)user->pw_gid, groups, &ngroups);
1192# else
f093225b 1193 getgrouplist(username, user->pw_gid, groups, &ngroups);
fdc3c81a 1194#endif /* __APPLE__ */
6eb98aee
MS
1195
1196 for (i = 0; i < ngroups; i ++)
afe94dff 1197 if ((int)groupid == (int)groups[i])
6eb98aee
MS
1198 return (1);
1199 }
6eb98aee 1200
afe94dff 1201#else
ef416fc2 1202 for (i = 0; group->gr_mem[i]; i ++)
afe94dff 1203 {
88f9aafc 1204 if (!_cups_strcasecmp(username, group->gr_mem[i]))
ef416fc2 1205 return (1);
afe94dff
MS
1206 }
1207#endif /* HAVE_GETGROUPLIST */
ef416fc2 1208 }
afe94dff
MS
1209 else
1210 groupid = (gid_t)-1;
ef416fc2 1211
1212 /*
1213 * Group doesn't exist or user not in group list, check the group ID
1214 * against the user's group ID...
1215 */
1216
afe94dff 1217 if (user && groupid == user->pw_gid)
ef416fc2 1218 return (1);
1219
bd7854cb 1220#ifdef HAVE_MBR_UID_TO_UUID
1221 /*
d4874933 1222 * Check group membership through macOS membership API...
bd7854cb 1223 */
1224
dd1abb6b 1225 if (user && !mbr_uid_to_uuid(user->pw_uid, useruuid))
c9fc04c6 1226 {
afe94dff 1227 if (groupid != (gid_t)-1)
8922323b
MS
1228 {
1229 /*
1230 * Map group name to UUID and check membership...
1231 */
c9fc04c6 1232
afe94dff 1233 if (!mbr_gid_to_uuid(groupid, groupuuid))
8922323b
MS
1234 if (!mbr_check_membership(useruuid, groupuuid, &is_member))
1235 if (is_member)
1236 return (1);
1237 }
1238 else if (groupname[0] == '#')
1239 {
1240 /*
1241 * Use UUID directly and check for equality (user UUID) and
1242 * membership (group UUID)...
1243 */
1244
1245 if (!uuid_parse((char *)groupname + 1, groupuuid))
1246 {
1247 if (!uuid_compare(useruuid, groupuuid))
c9fc04c6 1248 return (1);
8922323b
MS
1249 else if (!mbr_check_membership(useruuid, groupuuid, &is_member))
1250 if (is_member)
1251 return (1);
1252 }
1253
1254 return (0);
1255 }
1256 }
1257 else if (groupname[0] == '#')
1258 return (0);
bd7854cb 1259#endif /* HAVE_MBR_UID_TO_UUID */
1260
ef416fc2 1261 /*
1262 * If we get this far, then the user isn't part of the named group...
1263 */
1264
1265 return (0);
1266}
1267
1268
1269/*
1270 * 'cupsdCopyLocation()' - Make a copy of a location...
1271 */
1272
1273cupsd_location_t * /* O - New location */
1274cupsdCopyLocation(
10d09e33 1275 cupsd_location_t *loc) /* I - Original location */
ef416fc2 1276{
ef416fc2 1277 cupsd_location_t *temp; /* New location */
ef416fc2 1278
1279
ef416fc2 1280 /*
10d09e33 1281 * Make a copy of the original location...
ef416fc2 1282 */
1283
10d09e33 1284 if ((temp = calloc(1, sizeof(cupsd_location_t))) == NULL)
ef416fc2 1285 return (NULL);
1286
ef416fc2 1287 /*
1288 * Copy the information from the original location to the new one.
1289 */
1290
10d09e33
MS
1291 if (!loc)
1292 return (temp);
ef416fc2 1293
10d09e33
MS
1294 if (loc->location)
1295 temp->location = _cupsStrAlloc(loc->location);
ef416fc2 1296
d2ff4622 1297 temp->length = loc->length;
10d09e33
MS
1298 temp->limit = loc->limit;
1299 temp->order_type = loc->order_type;
1300 temp->type = loc->type;
1301 temp->level = loc->level;
1302 temp->satisfy = loc->satisfy;
1303 temp->encryption = loc->encryption;
1304
1305 if (loc->names)
1306 {
1307 if ((temp->names = cupsArrayDup(loc->names)) == NULL)
ef416fc2 1308 {
1309 cupsdLogMessage(CUPSD_LOG_ERROR,
10d09e33
MS
1310 "Unable to allocate memory for %d names: %s",
1311 cupsArrayCount(loc->names), strerror(errno));
bd7854cb 1312
10d09e33 1313 cupsdFreeLocation(temp);
ef416fc2 1314 return (NULL);
1315 }
ef416fc2 1316 }
1317
10d09e33 1318 if (loc->allow)
ef416fc2 1319 {
1320 /*
1321 * Copy allow rules...
1322 */
1323
10d09e33 1324 if ((temp->allow = cupsArrayDup(loc->allow)) == NULL)
ef416fc2 1325 {
1326 cupsdLogMessage(CUPSD_LOG_ERROR,
10d09e33
MS
1327 "Unable to allocate memory for %d allow rules: %s",
1328 cupsArrayCount(loc->allow), strerror(errno));
1329 cupsdFreeLocation(temp);
ef416fc2 1330 return (NULL);
1331 }
ef416fc2 1332 }
1333
10d09e33 1334 if (loc->deny)
ef416fc2 1335 {
1336 /*
1337 * Copy deny rules...
1338 */
1339
10d09e33 1340 if ((temp->deny = cupsArrayDup(loc->deny)) == NULL)
ef416fc2 1341 {
1342 cupsdLogMessage(CUPSD_LOG_ERROR,
10d09e33
MS
1343 "Unable to allocate memory for %d deny rules: %s",
1344 cupsArrayCount(loc->deny), strerror(errno));
1345 cupsdFreeLocation(temp);
ef416fc2 1346 return (NULL);
1347 }
ef416fc2 1348 }
1349
1350 return (temp);
1351}
1352
1353
1354/*
1355 * 'cupsdDeleteAllLocations()' - Free all memory used for location authorization.
1356 */
1357
1358void
1359cupsdDeleteAllLocations(void)
1360{
ef416fc2 1361 /*
10d09e33 1362 * Free the location array, which will free all of the locations...
ef416fc2 1363 */
1364
bd7854cb 1365 cupsArrayDelete(Locations);
1366 Locations = NULL;
ef416fc2 1367}
1368
1369
ef416fc2 1370/*
1371 * 'cupsdFindBest()' - Find the location entry that best matches the resource.
1372 */
1373
1374cupsd_location_t * /* O - Location that matches */
1375cupsdFindBest(const char *path, /* I - Resource path */
1376 http_state_t state) /* I - HTTP state/request */
1377{
ef416fc2 1378 char uri[HTTP_MAX_URI],
1379 /* URI in request... */
1380 *uriptr; /* Pointer into URI */
1381 cupsd_location_t *loc, /* Current location */
1382 *best; /* Best match for location so far */
7e86f2f6 1383 size_t bestlen; /* Length of best match */
ef416fc2 1384 int limit; /* Limit field */
5bd77a73 1385 static const int limits[] = /* Map http_status_t to CUPSD_AUTH_LIMIT_xyz */
ef416fc2 1386 {
5bd77a73
MS
1387 CUPSD_AUTH_LIMIT_ALL,
1388 CUPSD_AUTH_LIMIT_OPTIONS,
1389 CUPSD_AUTH_LIMIT_GET,
1390 CUPSD_AUTH_LIMIT_GET,
1391 CUPSD_AUTH_LIMIT_HEAD,
1392 CUPSD_AUTH_LIMIT_POST,
1393 CUPSD_AUTH_LIMIT_POST,
1394 CUPSD_AUTH_LIMIT_POST,
1395 CUPSD_AUTH_LIMIT_PUT,
1396 CUPSD_AUTH_LIMIT_PUT,
1397 CUPSD_AUTH_LIMIT_DELETE,
1398 CUPSD_AUTH_LIMIT_TRACE,
1399 CUPSD_AUTH_LIMIT_ALL,
d2ff4622
MS
1400 CUPSD_AUTH_LIMIT_ALL,
1401 CUPSD_AUTH_LIMIT_ALL,
5bd77a73 1402 CUPSD_AUTH_LIMIT_ALL
ef416fc2 1403 };
1404
1405
1406 /*
1407 * First copy the connection URI to a local string so we have drop
1408 * any .ppd extension from the pathname in /printers or /classes
1409 * URIs...
1410 */
1411
1412 strlcpy(uri, path, sizeof(uri));
1413
c26ceab0
MS
1414 if ((uriptr = strchr(uri, '?')) != NULL)
1415 *uriptr = '\0'; /* Drop trailing query string */
1416
1417 if ((uriptr = uri + strlen(uri) - 1) > uri && *uriptr == '/')
1418 *uriptr = '\0'; /* Remove trailing '/' */
1419
ef416fc2 1420 if (!strncmp(uri, "/printers/", 10) ||
1421 !strncmp(uri, "/classes/", 9))
1422 {
1423 /*
1424 * Check if the URI has .ppd on the end...
1425 */
1426
1427 uriptr = uri + strlen(uri) - 4; /* len > 4 if we get here... */
1428
1429 if (!strcmp(uriptr, ".ppd"))
1430 *uriptr = '\0';
1431 }
1432
ef416fc2 1433 /*
1434 * Loop through the list of locations to find a match...
1435 */
1436
1437 limit = limits[state];
1438 best = NULL;
1439 bestlen = 0;
1440
ffe32673 1441 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdFindBest: uri=\"%s\", limit=%x...", uri, limit);
d2ff4622
MS
1442
1443
bd7854cb 1444 for (loc = (cupsd_location_t *)cupsArrayFirst(Locations);
1445 loc;
1446 loc = (cupsd_location_t *)cupsArrayNext(Locations))
ef416fc2 1447 {
d2ff4622 1448 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdFindBest: Location %s(%d) Limit %x", loc->location ? loc->location : "(null)", (int)loc->length, loc->limit);
ef416fc2 1449
1450 if (!strncmp(uri, "/printers/", 10) || !strncmp(uri, "/classes/", 9))
1451 {
1452 /*
1453 * Use case-insensitive comparison for queue names...
1454 */
1455
e1d6a774 1456 if (loc->length > bestlen && loc->location &&
88f9aafc 1457 !_cups_strncasecmp(uri, loc->location, loc->length) &&
ef416fc2 1458 loc->location[0] == '/' &&
1459 (limit & loc->limit) != 0)
1460 {
1461 best = loc;
1462 bestlen = loc->length;
1463 }
1464 }
1465 else
1466 {
1467 /*
1468 * Use case-sensitive comparison for other URIs...
1469 */
1470
e1d6a774 1471 if (loc->length > bestlen && loc->location &&
ef416fc2 1472 !strncmp(uri, loc->location, loc->length) &&
1473 loc->location[0] == '/' &&
1474 (limit & loc->limit) != 0)
1475 {
1476 best = loc;
1477 bestlen = loc->length;
1478 }
1479 }
1480 }
1481
1482 /*
1483 * Return the match, if any...
1484 */
1485
ffe32673 1486 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdFindBest: best=%s", best ? best->location : "NONE");
ef416fc2 1487
1488 return (best);
1489}
1490
1491
1492/*
1493 * 'cupsdFindLocation()' - Find the named location.
1494 */
1495
1496cupsd_location_t * /* O - Location that matches */
1497cupsdFindLocation(const char *location) /* I - Connection */
1498{
bd7854cb 1499 cupsd_location_t key; /* Search key */
ef416fc2 1500
1501
bd7854cb 1502 key.location = (char *)location;
ef416fc2 1503
bd7854cb 1504 return ((cupsd_location_t *)cupsArrayFind(Locations, &key));
ef416fc2 1505}
1506
1507
10d09e33
MS
1508/*
1509 * 'cupsdFreeLocation()' - Free all memory used by a location.
1510 */
1511
1512void
1513cupsdFreeLocation(cupsd_location_t *loc)/* I - Location to free */
1514{
1515 cupsArrayDelete(loc->names);
1516 cupsArrayDelete(loc->allow);
1517 cupsArrayDelete(loc->deny);
1518
1519 _cupsStrFree(loc->location);
1520 free(loc);
1521}
1522
1523
ef416fc2 1524/*
1525 * 'cupsdIsAuthorized()' - Check to see if the user is authorized...
1526 */
1527
1528http_status_t /* O - HTTP_OK if authorized or error code */
1529cupsdIsAuthorized(cupsd_client_t *con, /* I - Connection */
1530 const char *owner)/* I - Owner of object */
1531{
10d09e33 1532 int i, /* Looping vars */
2fb76298
MS
1533 auth, /* Authorization status */
1534 type; /* Type of authentication */
5ec1fd3d
MS
1535 http_addr_t *hostaddr = httpGetAddress(con->http);
1536 /* Client address */
1537 const char *hostname = httpGetHostname(con->http, NULL, 0);
1538 /* Client hostname */
ef416fc2 1539 unsigned address[4]; /* Authorization address */
1540 cupsd_location_t *best; /* Best match for location so far */
7e86f2f6 1541 size_t hostlen; /* Length of hostname */
10d09e33
MS
1542 char *name, /* Current username */
1543 username[256], /* Username to authorize */
db1f069b
MS
1544 ownername[256], /* Owner name to authorize */
1545 *ptr; /* Pointer into username */
ef416fc2 1546 struct passwd *pw; /* User password data */
1547 static const char * const levels[] = /* Auth levels */
1548 {
1549 "ANON",
1550 "USER",
1551 "GROUP"
1552 };
1553 static const char * const types[] = /* Auth types */
1554 {
2fb76298
MS
1555 "None",
1556 "Basic",
2fb76298 1557 "Negotiate"
ef416fc2 1558 };
1559
1560
ffe32673 1561 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)" : "");
fa73b229 1562 if (owner)
ffe32673 1563 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdIsAuthorized: owner=\"%s\"", owner);
ef416fc2 1564
1565 /*
1566 * If there is no "best" authentication rule for this request, then
1567 * access is allowed from the local system and denied from other
1568 * addresses...
1569 */
1570
1571 if (!con->best)
1572 {
5ec1fd3d
MS
1573 if (httpAddrLocalhost(httpGetAddress(con->http)) ||
1574 !strcmp(hostname, ServerName) ||
1575 cupsArrayFind(ServerAlias, (void *)hostname))
ef416fc2 1576 return (HTTP_OK);
1577 else
1578 return (HTTP_FORBIDDEN);
1579 }
1580
1581 best = con->best;
1582
5bd77a73 1583 if ((type = best->type) == CUPSD_AUTH_DEFAULT)
dcb445bc 1584 type = cupsdDefaultAuthType();
2fb76298 1585
ffe32673 1586 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));
ef416fc2 1587
5bd77a73 1588 if (best->limit == CUPSD_AUTH_LIMIT_IPP)
ffe32673 1589 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdIsAuthorized: op=%x(%s)", best->op, ippOpString(best->op));
ef416fc2 1590
1591 /*
1592 * Check host/ip-based accesses...
1593 */
1594
1595#ifdef AF_INET6
5ec1fd3d 1596 if (httpAddrFamily(hostaddr) == AF_INET6)
ef416fc2 1597 {
1598 /*
1599 * Copy IPv6 address...
1600 */
1601
5ec1fd3d
MS
1602 address[0] = ntohl(hostaddr->ipv6.sin6_addr.s6_addr32[0]);
1603 address[1] = ntohl(hostaddr->ipv6.sin6_addr.s6_addr32[1]);
1604 address[2] = ntohl(hostaddr->ipv6.sin6_addr.s6_addr32[2]);
1605 address[3] = ntohl(hostaddr->ipv6.sin6_addr.s6_addr32[3]);
ef416fc2 1606 }
1607 else
1608#endif /* AF_INET6 */
996acce8 1609 if (con->http->hostaddr->addr.sa_family == AF_INET)
ef416fc2 1610 {
1611 /*
1612 * Copy IPv4 address...
1613 */
1614
1615 address[0] = 0;
1616 address[1] = 0;
1617 address[2] = 0;
5ec1fd3d 1618 address[3] = ntohl(hostaddr->ipv4.sin_addr.s_addr);
ef416fc2 1619 }
1620 else
1621 memset(address, 0, sizeof(address));
1622
5ec1fd3d 1623 hostlen = strlen(hostname);
ef416fc2 1624
5ec1fd3d 1625 auth = cupsdCheckAccess(address, hostname, hostlen, best)
5bd77a73 1626 ? CUPSD_AUTH_ALLOW : CUPSD_AUTH_DENY;
ef416fc2 1627
ffe32673 1628 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdIsAuthorized: auth=CUPSD_AUTH_%s...", auth ? "DENY" : "ALLOW");
ef416fc2 1629
5bd77a73 1630 if (auth == CUPSD_AUTH_DENY && best->satisfy == CUPSD_AUTH_SATISFY_ALL)
ef416fc2 1631 return (HTTP_FORBIDDEN);
1632
1633#ifdef HAVE_SSL
1634 /*
1635 * See if encryption is required...
1636 */
1637
996acce8 1638 if ((best->encryption >= HTTP_ENCRYPT_REQUIRED && !con->http->tls &&
5ec1fd3d
MS
1639 _cups_strcasecmp(hostname, "localhost") &&
1640 !httpAddrLocalhost(hostaddr) &&
5bd77a73 1641 best->satisfy == CUPSD_AUTH_SATISFY_ALL) &&
c8fef167 1642 !(type == CUPSD_AUTH_NEGOTIATE ||
dcb445bc
MS
1643 (type == CUPSD_AUTH_NONE &&
1644 cupsdDefaultAuthType() == CUPSD_AUTH_NEGOTIATE)))
ef416fc2 1645 {
355e94dc 1646 cupsdLogMessage(CUPSD_LOG_DEBUG,
ef416fc2 1647 "cupsdIsAuthorized: Need upgrade to TLS...");
1648 return (HTTP_UPGRADE_REQUIRED);
1649 }
1650#endif /* HAVE_SSL */
1651
1652 /*
1653 * Now see what access level is required...
1654 */
1655
5bd77a73 1656 if (best->level == CUPSD_AUTH_ANON || /* Anonymous access - allow it */
10d09e33 1657 (type == CUPSD_AUTH_NONE && cupsArrayCount(best->names) == 0))
ef416fc2 1658 return (HTTP_OK);
1659
5bd77a73
MS
1660 if (!con->username[0] && type == CUPSD_AUTH_NONE &&
1661 best->limit == CUPSD_AUTH_LIMIT_IPP)
ef416fc2 1662 {
1663 /*
1664 * Check for unauthenticated username...
1665 */
1666
1667 ipp_attribute_t *attr; /* requesting-user-name attribute */
1668
1669
1670 attr = ippFindAttribute(con->request, "requesting-user-name", IPP_TAG_NAME);
1671 if (attr)
1672 {
355e94dc 1673 cupsdLogMessage(CUPSD_LOG_DEBUG,
ef416fc2 1674 "cupsdIsAuthorized: requesting-user-name=\"%s\"",
1675 attr->values[0].string.text);
db1f069b 1676 strlcpy(username, attr->values[0].string.text, sizeof(username));
ef416fc2 1677 }
5bd77a73 1678 else if (best->satisfy == CUPSD_AUTH_SATISFY_ALL || auth == CUPSD_AUTH_DENY)
ef416fc2 1679 return (HTTP_UNAUTHORIZED); /* Non-anonymous needs user/pass */
1680 else
1681 return (HTTP_OK); /* unless overridden with Satisfy */
1682 }
fa73b229 1683 else
1684 {
355e94dc 1685 cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdIsAuthorized: username=\"%s\"",
fa73b229 1686 con->username);
1687
f7deaa1a 1688#ifdef HAVE_AUTHORIZATION_H
1689 if (!con->username[0] && !con->authref)
1690#else
fa73b229 1691 if (!con->username[0])
f7deaa1a 1692#endif /* HAVE_AUTHORIZATION_H */
fa73b229 1693 {
5bd77a73 1694 if (best->satisfy == CUPSD_AUTH_SATISFY_ALL || auth == CUPSD_AUTH_DENY)
fa73b229 1695 return (HTTP_UNAUTHORIZED); /* Non-anonymous needs user/pass */
1696 else
1697 return (HTTP_OK); /* unless overridden with Satisfy */
1698 }
1699
eac3a0a0 1700
5bd77a73 1701 if (con->type != type && type != CUPSD_AUTH_NONE &&
eac3a0a0
MS
1702#ifdef HAVE_GSSAPI
1703 (type != CUPSD_AUTH_NEGOTIATE || con->gss_uid <= 0) &&
1704#endif /* HAVE_GSSAPI */
e0660879 1705 con->type != CUPSD_AUTH_BASIC)
2fb76298 1706 {
e0660879 1707 cupsdLogMessage(CUPSD_LOG_ERROR, "Authorized using %s, expected %s.",
2fb76298
MS
1708 types[con->type], types[type]);
1709
1710 return (HTTP_UNAUTHORIZED);
1711 }
1712
db1f069b 1713 strlcpy(username, con->username, sizeof(username));
fa73b229 1714 }
ef416fc2 1715
1716 /*
fa73b229 1717 * OK, got a username. See if we need normal user access, or group
ef416fc2 1718 * access... (root always matches)
1719 */
1720
fa73b229 1721 if (!strcmp(username, "root"))
ef416fc2 1722 return (HTTP_OK);
1723
db1f069b
MS
1724 /*
1725 * Strip any @domain or @KDC from the username and owner...
1726 */
1727
1728 if ((ptr = strchr(username, '@')) != NULL)
1729 *ptr = '\0';
1730
1731 if (owner)
1732 {
1733 strlcpy(ownername, owner, sizeof(ownername));
1734
1735 if ((ptr = strchr(ownername, '@')) != NULL)
1736 *ptr = '\0';
1737 }
1738 else
1739 ownername[0] = '\0';
1740
ef416fc2 1741 /*
1742 * Get the user info...
1743 */
1744
f7deaa1a 1745 if (username[0])
1746 {
1747 pw = getpwnam(username);
1748 endpwent();
1749 }
1750 else
1751 pw = NULL;
ef416fc2 1752
5bd77a73 1753 if (best->level == CUPSD_AUTH_USER)
ef416fc2 1754 {
1755 /*
1756 * If there are no names associated with this location, then
1757 * any valid user is OK...
1758 */
1759
10d09e33 1760 if (cupsArrayCount(best->names) == 0)
ef416fc2 1761 return (HTTP_OK);
1762
1763 /*
1764 * Otherwise check the user list and return OK if this user is
1765 * allowed...
1766 */
1767
ffe32673 1768 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdIsAuthorized: Checking user membership...");
ef416fc2 1769
f7deaa1a 1770#ifdef HAVE_AUTHORIZATION_H
1771 /*
1772 * If an authorization reference was supplied it must match a right name...
1773 */
1774
1775 if (con->authref)
1776 {
10d09e33
MS
1777 for (name = (char *)cupsArrayFirst(best->names);
1778 name;
1779 name = (char *)cupsArrayNext(best->names))
f7deaa1a 1780 {
88f9aafc 1781 if (!_cups_strncasecmp(name, "@AUTHKEY(", 9) && check_authref(con, name + 9))
f7deaa1a 1782 return (HTTP_OK);
88f9aafc 1783 else if (!_cups_strcasecmp(name, "@SYSTEM") && SystemGroupAuthKey &&
f7deaa1a 1784 check_authref(con, SystemGroupAuthKey))
1785 return (HTTP_OK);
1786 }
1787
ee571f26 1788 return (HTTP_FORBIDDEN);
f7deaa1a 1789 }
1790#endif /* HAVE_AUTHORIZATION_H */
1791
10d09e33
MS
1792 for (name = (char *)cupsArrayFirst(best->names);
1793 name;
1794 name = (char *)cupsArrayNext(best->names))
ef416fc2 1795 {
88f9aafc
MS
1796 if (!_cups_strcasecmp(name, "@OWNER") && owner &&
1797 !_cups_strcasecmp(username, ownername))
ef416fc2 1798 return (HTTP_OK);
88f9aafc 1799 else if (!_cups_strcasecmp(name, "@SYSTEM"))
ef416fc2 1800 {
10d09e33
MS
1801 for (i = 0; i < NumSystemGroups; i ++)
1802 if (cupsdCheckGroup(username, pw, SystemGroups[i]))
ef416fc2 1803 return (HTTP_OK);
1804 }
10d09e33 1805 else if (name[0] == '@')
ef416fc2 1806 {
10d09e33 1807 if (cupsdCheckGroup(username, pw, name + 1))
ef416fc2 1808 return (HTTP_OK);
1809 }
88f9aafc 1810 else if (!_cups_strcasecmp(username, name))
ef416fc2 1811 return (HTTP_OK);
1812 }
1813
85dda01c 1814 return (con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED);
ef416fc2 1815 }
1816
1817 /*
1818 * Check to see if this user is in any of the named groups...
1819 */
1820
ffe32673 1821 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdIsAuthorized: Checking group membership...");
ef416fc2 1822
1823 /*
1824 * Check to see if this user is in any of the named groups...
1825 */
1826
10d09e33
MS
1827 for (name = (char *)cupsArrayFirst(best->names);
1828 name;
1829 name = (char *)cupsArrayNext(best->names))
ef416fc2 1830 {
ffe32673 1831 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdIsAuthorized: Checking group \"%s\" membership...", name);
ef416fc2 1832
88f9aafc 1833 if (!_cups_strcasecmp(name, "@SYSTEM"))
ef416fc2 1834 {
10d09e33
MS
1835 for (i = 0; i < NumSystemGroups; i ++)
1836 if (cupsdCheckGroup(username, pw, SystemGroups[i]))
ef416fc2 1837 return (HTTP_OK);
1838 }
10d09e33 1839 else if (cupsdCheckGroup(username, pw, name))
ef416fc2 1840 return (HTTP_OK);
1841 }
1842
1843 /*
1844 * The user isn't part of the specified group, so deny access...
1845 */
1846
ffe32673 1847 cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdIsAuthorized: User not in group(s).");
ef416fc2 1848
85dda01c 1849 return (con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED);
ef416fc2 1850}
1851
1852
1853/*
10d09e33
MS
1854 * 'cupsdNewLocation()' - Create a new location for authorization.
1855 *
1856 * Note: Still need to call cupsdAddLocation() to add it to the list of global
1857 * locations.
ef416fc2 1858 */
1859
10d09e33
MS
1860cupsd_location_t * /* O - Pointer to new location record */
1861cupsdNewLocation(const char *location) /* I - Location path */
ef416fc2 1862{
10d09e33 1863 cupsd_location_t *temp; /* New location */
ef416fc2 1864
1865
1866 /*
10d09e33 1867 * Try to allocate memory for the new location.
ef416fc2 1868 */
1869
10d09e33 1870 if ((temp = calloc(1, sizeof(cupsd_location_t))) == NULL)
ef416fc2 1871 return (NULL);
1872
1873 /*
10d09e33 1874 * Initialize the record and copy the name over...
ef416fc2 1875 */
1876
10d09e33
MS
1877 if ((temp->location = _cupsStrAlloc(location)) == NULL)
1878 {
1879 free(temp);
ef416fc2 1880 return (NULL);
10d09e33 1881 }
ef416fc2 1882
10d09e33 1883 temp->length = strlen(temp->location);
ef416fc2 1884
1885 /*
10d09e33 1886 * Return the new record...
ef416fc2 1887 */
1888
ef416fc2 1889 return (temp);
1890}
1891
1892
f7deaa1a 1893#ifdef HAVE_AUTHORIZATION_H
1894/*
1895 * 'check_authref()' - Check if an authorization services reference has the
1896 * supplied right.
1897 */
1898
1899static int /* O - 1 if right is valid, 0 otherwise */
1900check_authref(cupsd_client_t *con, /* I - Connection */
1901 const char *right) /* I - Right name */
1902{
1903 OSStatus status; /* OS Status */
1904 AuthorizationItem authright; /* Authorization right */
1905 AuthorizationRights authrights; /* Authorization rights */
1906 AuthorizationFlags authflags; /* Authorization flags */
1907
1908
1909 /*
1910 * Check to see if the user is allowed to perform the task...
1911 */
1912
1913 if (!con->authref)
1914 return (0);
1915
1916 authright.name = right;
1917 authright.valueLength = 0;
1918 authright.value = NULL;
1919 authright.flags = 0;
1920
1921 authrights.count = 1;
1922 authrights.items = &authright;
1923
c8fef167 1924 authflags = kAuthorizationFlagDefaults |
f7deaa1a 1925 kAuthorizationFlagExtendRights;
1926
c8fef167
MS
1927 if ((status = AuthorizationCopyRights(con->authref, &authrights,
1928 kAuthorizationEmptyEnvironment,
f7deaa1a 1929 authflags, NULL)) != 0)
1930 {
1931 cupsdLogMessage(CUPSD_LOG_ERROR,
1932 "AuthorizationCopyRights(\"%s\") returned %d (%s)",
1933 authright.name, (int)status, cssmErrorString(status));
1934 return (0);
1935 }
1936
ffe32673 1937 cupsdLogMessage(CUPSD_LOG_DEBUG2, "AuthorizationCopyRights(\"%s\") succeeded.", authright.name);
f7deaa1a 1938
1939 return (1);
1940}
1941#endif /* HAVE_AUTHORIZATION_H */
1942
1943
bd7854cb 1944/*
1945 * 'compare_locations()' - Compare two locations.
1946 */
1947
1948static int /* O - Result of comparison */
1949compare_locations(cupsd_location_t *a, /* I - First location */
1950 cupsd_location_t *b) /* I - Second location */
1951{
1952 return (strcmp(b->location, a->location));
1953}
1954
1955
10d09e33
MS
1956/*
1957 * 'copy_authmask()' - Copy function for auth masks.
1958 */
1959
1960static cupsd_authmask_t * /* O - New auth mask */
1961copy_authmask(cupsd_authmask_t *mask, /* I - Existing auth mask */
1962 void *data) /* I - User data (unused) */
1963{
1964 cupsd_authmask_t *temp; /* New auth mask */
1965
1966
1967 (void)data;
1968
1969 if ((temp = malloc(sizeof(cupsd_authmask_t))) != NULL)
1970 {
1971 memcpy(temp, mask, sizeof(cupsd_authmask_t));
1972
1973 if (temp->type == CUPSD_AUTH_NAME || temp->type == CUPSD_AUTH_INTERFACE)
1974 {
1975 /*
1976 * Make a copy of the name...
1977 */
1978
1979 if ((temp->mask.name.name = _cupsStrAlloc(temp->mask.name.name)) == NULL)
1980 {
1981 /*
1982 * Failed to make copy...
1983 */
1984
1985 free(temp);
1986 temp = NULL;
1987 }
1988 }
1989 }
1990
1991 return (temp);
1992}
1993
1994
10d09e33
MS
1995/*
1996 * 'free_authmask()' - Free function for auth masks.
1997 */
1998
1999static void
2000free_authmask(cupsd_authmask_t *mask, /* I - Auth mask to free */
2001 void *data) /* I - User data (unused) */
2002{
2003 (void)data;
2004
2005 if (mask->type == CUPSD_AUTH_NAME || mask->type == CUPSD_AUTH_INTERFACE)
2006 _cupsStrFree(mask->mask.name.name);
2007
2008 free(mask);
2009}
2010
2011
ef416fc2 2012#if HAVE_LIBPAM
2013/*
2014 * 'pam_func()' - PAM conversation function.
2015 */
2016
2017static int /* O - Success or failure */
2018pam_func(
2019 int num_msg, /* I - Number of messages */
2020 const struct pam_message **msg, /* I - Messages */
2021 struct pam_response **resp, /* O - Responses */
2022 void *appdata_ptr)
2023 /* I - Pointer to connection */
2024{
2025 int i; /* Looping var */
2026 struct pam_response *replies; /* Replies */
2027 cupsd_authdata_t *data; /* Pointer to auth data */
2028
2029
2030 /*
2031 * Allocate memory for the responses...
2032 */
2033
7e86f2f6 2034 if ((replies = malloc(sizeof(struct pam_response) * (size_t)num_msg)) == NULL)
ef416fc2 2035 return (PAM_CONV_ERR);
2036
2037 /*
2038 * Answer all of the messages...
2039 */
2040
2041 DEBUG_printf(("pam_func: appdata_ptr = %p\n", appdata_ptr));
2042
ef416fc2 2043 data = (cupsd_authdata_t *)appdata_ptr;
ef416fc2 2044
2045 for (i = 0; i < num_msg; i ++)
2046 {
2047 DEBUG_printf(("pam_func: Message = \"%s\"\n", msg[i]->msg));
2048
2049 switch (msg[i]->msg_style)
2050 {
2051 case PAM_PROMPT_ECHO_ON:
2052 DEBUG_printf(("pam_func: PAM_PROMPT_ECHO_ON, returning \"%s\"...\n",
2053 data->username));
2054 replies[i].resp_retcode = PAM_SUCCESS;
2055 replies[i].resp = strdup(data->username);
2056 break;
2057
2058 case PAM_PROMPT_ECHO_OFF:
2059 DEBUG_printf(("pam_func: PAM_PROMPT_ECHO_OFF, returning \"%s\"...\n",
2060 data->password));
2061 replies[i].resp_retcode = PAM_SUCCESS;
2062 replies[i].resp = strdup(data->password);
2063 break;
2064
2065 case PAM_TEXT_INFO:
2066 DEBUG_puts("pam_func: PAM_TEXT_INFO...");
2067 replies[i].resp_retcode = PAM_SUCCESS;
2068 replies[i].resp = NULL;
2069 break;
2070
2071 case PAM_ERROR_MSG:
2072 DEBUG_puts("pam_func: PAM_ERROR_MSG...");
2073 replies[i].resp_retcode = PAM_SUCCESS;
2074 replies[i].resp = NULL;
2075 break;
2076
2077 default:
2078 DEBUG_printf(("pam_func: Unknown PAM message %d...\n",
2079 msg[i]->msg_style));
2080 free(replies);
2081 return (PAM_CONV_ERR);
2082 }
2083 }
2084
2085 /*
2086 * Return the responses back to PAM...
2087 */
2088
2089 *resp = replies;
2090
2091 return (PAM_SUCCESS);
2092}
5a1d7a17 2093#else
ef416fc2 2094
2095
2096/*
2097 * 'to64()' - Base64-encode an integer value...
2098 */
2099
2100static void
2101to64(char *s, /* O - Output string */
2102 unsigned long v, /* I - Value to encode */
2103 int n) /* I - Number of digits */
2104{
2105 const char *itoa64 = "./0123456789"
2106 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2107 "abcdefghijklmnopqrstuvwxyz";
2108
2109
2110 for (; n > 0; n --, v >>= 6)
2111 *s++ = itoa64[v & 0x3f];
2112}
2113#endif /* HAVE_LIBPAM */