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