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