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