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