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