]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/auth.c
Make sure we escape the subject and text in the RSS feed
[thirdparty/cups.git] / scheduler / auth.c
CommitLineData
b423cd4c 1/*
f2d18633 2 * "$Id$"
ef416fc2 3 *
5ec1fd3d 4 * Authorization routines for the CUPS scheduler.
ef416fc2 5 *
d201ec2b 6 * Copyright 2007-2015 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
d2ff4622 838 if (&gss_init_sec_context == NULL)
f42414bf 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
d2ff4622 1378 temp->length = loc->length;
10d09e33
MS
1379 temp->limit = loc->limit;
1380 temp->order_type = loc->order_type;
1381 temp->type = loc->type;
1382 temp->level = loc->level;
1383 temp->satisfy = loc->satisfy;
1384 temp->encryption = loc->encryption;
1385
1386 if (loc->names)
1387 {
1388 if ((temp->names = cupsArrayDup(loc->names)) == NULL)
ef416fc2 1389 {
1390 cupsdLogMessage(CUPSD_LOG_ERROR,
10d09e33
MS
1391 "Unable to allocate memory for %d names: %s",
1392 cupsArrayCount(loc->names), strerror(errno));
bd7854cb 1393
10d09e33 1394 cupsdFreeLocation(temp);
ef416fc2 1395 return (NULL);
1396 }
ef416fc2 1397 }
1398
10d09e33 1399 if (loc->allow)
ef416fc2 1400 {
1401 /*
1402 * Copy allow rules...
1403 */
1404
10d09e33 1405 if ((temp->allow = cupsArrayDup(loc->allow)) == NULL)
ef416fc2 1406 {
1407 cupsdLogMessage(CUPSD_LOG_ERROR,
10d09e33
MS
1408 "Unable to allocate memory for %d allow rules: %s",
1409 cupsArrayCount(loc->allow), strerror(errno));
1410 cupsdFreeLocation(temp);
ef416fc2 1411 return (NULL);
1412 }
ef416fc2 1413 }
1414
10d09e33 1415 if (loc->deny)
ef416fc2 1416 {
1417 /*
1418 * Copy deny rules...
1419 */
1420
10d09e33 1421 if ((temp->deny = cupsArrayDup(loc->deny)) == NULL)
ef416fc2 1422 {
1423 cupsdLogMessage(CUPSD_LOG_ERROR,
10d09e33
MS
1424 "Unable to allocate memory for %d deny rules: %s",
1425 cupsArrayCount(loc->deny), strerror(errno));
1426 cupsdFreeLocation(temp);
ef416fc2 1427 return (NULL);
1428 }
ef416fc2 1429 }
1430
1431 return (temp);
1432}
1433
1434
1435/*
1436 * 'cupsdDeleteAllLocations()' - Free all memory used for location authorization.
1437 */
1438
1439void
1440cupsdDeleteAllLocations(void)
1441{
ef416fc2 1442 /*
10d09e33 1443 * Free the location array, which will free all of the locations...
ef416fc2 1444 */
1445
bd7854cb 1446 cupsArrayDelete(Locations);
1447 Locations = NULL;
ef416fc2 1448}
1449
1450
ef416fc2 1451/*
1452 * 'cupsdFindBest()' - Find the location entry that best matches the resource.
1453 */
1454
1455cupsd_location_t * /* O - Location that matches */
1456cupsdFindBest(const char *path, /* I - Resource path */
1457 http_state_t state) /* I - HTTP state/request */
1458{
ef416fc2 1459 char uri[HTTP_MAX_URI],
1460 /* URI in request... */
1461 *uriptr; /* Pointer into URI */
1462 cupsd_location_t *loc, /* Current location */
1463 *best; /* Best match for location so far */
7e86f2f6 1464 size_t bestlen; /* Length of best match */
ef416fc2 1465 int limit; /* Limit field */
5bd77a73 1466 static const int limits[] = /* Map http_status_t to CUPSD_AUTH_LIMIT_xyz */
ef416fc2 1467 {
5bd77a73
MS
1468 CUPSD_AUTH_LIMIT_ALL,
1469 CUPSD_AUTH_LIMIT_OPTIONS,
1470 CUPSD_AUTH_LIMIT_GET,
1471 CUPSD_AUTH_LIMIT_GET,
1472 CUPSD_AUTH_LIMIT_HEAD,
1473 CUPSD_AUTH_LIMIT_POST,
1474 CUPSD_AUTH_LIMIT_POST,
1475 CUPSD_AUTH_LIMIT_POST,
1476 CUPSD_AUTH_LIMIT_PUT,
1477 CUPSD_AUTH_LIMIT_PUT,
1478 CUPSD_AUTH_LIMIT_DELETE,
1479 CUPSD_AUTH_LIMIT_TRACE,
1480 CUPSD_AUTH_LIMIT_ALL,
d2ff4622
MS
1481 CUPSD_AUTH_LIMIT_ALL,
1482 CUPSD_AUTH_LIMIT_ALL,
5bd77a73 1483 CUPSD_AUTH_LIMIT_ALL
ef416fc2 1484 };
1485
1486
1487 /*
1488 * First copy the connection URI to a local string so we have drop
1489 * any .ppd extension from the pathname in /printers or /classes
1490 * URIs...
1491 */
1492
1493 strlcpy(uri, path, sizeof(uri));
1494
c26ceab0
MS
1495 if ((uriptr = strchr(uri, '?')) != NULL)
1496 *uriptr = '\0'; /* Drop trailing query string */
1497
1498 if ((uriptr = uri + strlen(uri) - 1) > uri && *uriptr == '/')
1499 *uriptr = '\0'; /* Remove trailing '/' */
1500
ef416fc2 1501 if (!strncmp(uri, "/printers/", 10) ||
1502 !strncmp(uri, "/classes/", 9))
1503 {
1504 /*
1505 * Check if the URI has .ppd on the end...
1506 */
1507
1508 uriptr = uri + strlen(uri) - 4; /* len > 4 if we get here... */
1509
1510 if (!strcmp(uriptr, ".ppd"))
1511 *uriptr = '\0';
1512 }
1513
ef416fc2 1514 /*
1515 * Loop through the list of locations to find a match...
1516 */
1517
1518 limit = limits[state];
1519 best = NULL;
1520 bestlen = 0;
1521
d2ff4622
MS
1522 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdFindBest: uri = \"%s\", limit=%x...", uri, limit);
1523
1524
bd7854cb 1525 for (loc = (cupsd_location_t *)cupsArrayFirst(Locations);
1526 loc;
1527 loc = (cupsd_location_t *)cupsArrayNext(Locations))
ef416fc2 1528 {
d2ff4622 1529 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdFindBest: Location %s(%d) Limit %x", loc->location ? loc->location : "(null)", (int)loc->length, loc->limit);
ef416fc2 1530
1531 if (!strncmp(uri, "/printers/", 10) || !strncmp(uri, "/classes/", 9))
1532 {
1533 /*
1534 * Use case-insensitive comparison for queue names...
1535 */
1536
e1d6a774 1537 if (loc->length > bestlen && loc->location &&
88f9aafc 1538 !_cups_strncasecmp(uri, loc->location, loc->length) &&
ef416fc2 1539 loc->location[0] == '/' &&
1540 (limit & loc->limit) != 0)
1541 {
1542 best = loc;
1543 bestlen = loc->length;
1544 }
1545 }
1546 else
1547 {
1548 /*
1549 * Use case-sensitive comparison for other URIs...
1550 */
1551
e1d6a774 1552 if (loc->length > bestlen && loc->location &&
ef416fc2 1553 !strncmp(uri, loc->location, loc->length) &&
1554 loc->location[0] == '/' &&
1555 (limit & loc->limit) != 0)
1556 {
1557 best = loc;
1558 bestlen = loc->length;
1559 }
1560 }
1561 }
1562
1563 /*
1564 * Return the match, if any...
1565 */
1566
1567 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdFindBest: best = %s",
1568 best ? best->location : "NONE");
1569
1570 return (best);
1571}
1572
1573
1574/*
1575 * 'cupsdFindLocation()' - Find the named location.
1576 */
1577
1578cupsd_location_t * /* O - Location that matches */
1579cupsdFindLocation(const char *location) /* I - Connection */
1580{
bd7854cb 1581 cupsd_location_t key; /* Search key */
ef416fc2 1582
1583
bd7854cb 1584 key.location = (char *)location;
ef416fc2 1585
bd7854cb 1586 return ((cupsd_location_t *)cupsArrayFind(Locations, &key));
ef416fc2 1587}
1588
1589
10d09e33
MS
1590/*
1591 * 'cupsdFreeLocation()' - Free all memory used by a location.
1592 */
1593
1594void
1595cupsdFreeLocation(cupsd_location_t *loc)/* I - Location to free */
1596{
1597 cupsArrayDelete(loc->names);
1598 cupsArrayDelete(loc->allow);
1599 cupsArrayDelete(loc->deny);
1600
1601 _cupsStrFree(loc->location);
1602 free(loc);
1603}
1604
1605
ef416fc2 1606/*
1607 * 'cupsdIsAuthorized()' - Check to see if the user is authorized...
1608 */
1609
1610http_status_t /* O - HTTP_OK if authorized or error code */
1611cupsdIsAuthorized(cupsd_client_t *con, /* I - Connection */
1612 const char *owner)/* I - Owner of object */
1613{
10d09e33 1614 int i, /* Looping vars */
2fb76298
MS
1615 auth, /* Authorization status */
1616 type; /* Type of authentication */
5ec1fd3d
MS
1617 http_addr_t *hostaddr = httpGetAddress(con->http);
1618 /* Client address */
1619 const char *hostname = httpGetHostname(con->http, NULL, 0);
1620 /* Client hostname */
ef416fc2 1621 unsigned address[4]; /* Authorization address */
1622 cupsd_location_t *best; /* Best match for location so far */
7e86f2f6 1623 size_t hostlen; /* Length of hostname */
10d09e33
MS
1624 char *name, /* Current username */
1625 username[256], /* Username to authorize */
db1f069b
MS
1626 ownername[256], /* Owner name to authorize */
1627 *ptr; /* Pointer into username */
ef416fc2 1628 struct passwd *pw; /* User password data */
1629 static const char * const levels[] = /* Auth levels */
1630 {
1631 "ANON",
1632 "USER",
1633 "GROUP"
1634 };
1635 static const char * const types[] = /* Auth types */
1636 {
2fb76298
MS
1637 "None",
1638 "Basic",
2fb76298 1639 "Negotiate"
ef416fc2 1640 };
1641
1642
1643 cupsdLogMessage(CUPSD_LOG_DEBUG2,
1644 "cupsdIsAuthorized: con->uri=\"%s\", con->best=%p(%s)",
f301802f 1645 con->uri, con->best, con->best ? con->best->location ?
1646 con->best->location : "(null)" : "");
fa73b229 1647 if (owner)
1648 cupsdLogMessage(CUPSD_LOG_DEBUG2,
1649 "cupsdIsAuthorized: owner=\"%s\"", owner);
ef416fc2 1650
1651 /*
1652 * If there is no "best" authentication rule for this request, then
1653 * access is allowed from the local system and denied from other
1654 * addresses...
1655 */
1656
1657 if (!con->best)
1658 {
5ec1fd3d
MS
1659 if (httpAddrLocalhost(httpGetAddress(con->http)) ||
1660 !strcmp(hostname, ServerName) ||
1661 cupsArrayFind(ServerAlias, (void *)hostname))
ef416fc2 1662 return (HTTP_OK);
1663 else
1664 return (HTTP_FORBIDDEN);
1665 }
1666
1667 best = con->best;
1668
5bd77a73 1669 if ((type = best->type) == CUPSD_AUTH_DEFAULT)
dcb445bc 1670 type = cupsdDefaultAuthType();
2fb76298 1671
ef416fc2 1672 cupsdLogMessage(CUPSD_LOG_DEBUG2,
5bd77a73
MS
1673 "cupsdIsAuthorized: level=CUPSD_AUTH_%s, type=%s, "
1674 "satisfy=CUPSD_AUTH_SATISFY_%s, num_names=%d",
2fb76298 1675 levels[best->level], types[type],
10d09e33 1676 best->satisfy ? "ANY" : "ALL", cupsArrayCount(best->names));
ef416fc2 1677
5bd77a73 1678 if (best->limit == CUPSD_AUTH_LIMIT_IPP)
ef416fc2 1679 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdIsAuthorized: op=%x(%s)",
1680 best->op, ippOpString(best->op));
1681
1682 /*
1683 * Check host/ip-based accesses...
1684 */
1685
1686#ifdef AF_INET6
5ec1fd3d 1687 if (httpAddrFamily(hostaddr) == AF_INET6)
ef416fc2 1688 {
1689 /*
1690 * Copy IPv6 address...
1691 */
1692
5ec1fd3d
MS
1693 address[0] = ntohl(hostaddr->ipv6.sin6_addr.s6_addr32[0]);
1694 address[1] = ntohl(hostaddr->ipv6.sin6_addr.s6_addr32[1]);
1695 address[2] = ntohl(hostaddr->ipv6.sin6_addr.s6_addr32[2]);
1696 address[3] = ntohl(hostaddr->ipv6.sin6_addr.s6_addr32[3]);
ef416fc2 1697 }
1698 else
1699#endif /* AF_INET6 */
996acce8 1700 if (con->http->hostaddr->addr.sa_family == AF_INET)
ef416fc2 1701 {
1702 /*
1703 * Copy IPv4 address...
1704 */
1705
1706 address[0] = 0;
1707 address[1] = 0;
1708 address[2] = 0;
5ec1fd3d 1709 address[3] = ntohl(hostaddr->ipv4.sin_addr.s_addr);
ef416fc2 1710 }
1711 else
1712 memset(address, 0, sizeof(address));
1713
5ec1fd3d 1714 hostlen = strlen(hostname);
ef416fc2 1715
5ec1fd3d 1716 auth = cupsdCheckAccess(address, hostname, hostlen, best)
5bd77a73 1717 ? CUPSD_AUTH_ALLOW : CUPSD_AUTH_DENY;
ef416fc2 1718
5bd77a73 1719 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdIsAuthorized: auth=CUPSD_AUTH_%s...",
ef416fc2 1720 auth ? "DENY" : "ALLOW");
1721
5bd77a73 1722 if (auth == CUPSD_AUTH_DENY && best->satisfy == CUPSD_AUTH_SATISFY_ALL)
ef416fc2 1723 return (HTTP_FORBIDDEN);
1724
1725#ifdef HAVE_SSL
1726 /*
1727 * See if encryption is required...
1728 */
1729
996acce8 1730 if ((best->encryption >= HTTP_ENCRYPT_REQUIRED && !con->http->tls &&
5ec1fd3d
MS
1731 _cups_strcasecmp(hostname, "localhost") &&
1732 !httpAddrLocalhost(hostaddr) &&
5bd77a73 1733 best->satisfy == CUPSD_AUTH_SATISFY_ALL) &&
c8fef167 1734 !(type == CUPSD_AUTH_NEGOTIATE ||
dcb445bc
MS
1735 (type == CUPSD_AUTH_NONE &&
1736 cupsdDefaultAuthType() == CUPSD_AUTH_NEGOTIATE)))
ef416fc2 1737 {
355e94dc 1738 cupsdLogMessage(CUPSD_LOG_DEBUG,
ef416fc2 1739 "cupsdIsAuthorized: Need upgrade to TLS...");
1740 return (HTTP_UPGRADE_REQUIRED);
1741 }
1742#endif /* HAVE_SSL */
1743
1744 /*
1745 * Now see what access level is required...
1746 */
1747
5bd77a73 1748 if (best->level == CUPSD_AUTH_ANON || /* Anonymous access - allow it */
10d09e33 1749 (type == CUPSD_AUTH_NONE && cupsArrayCount(best->names) == 0))
ef416fc2 1750 return (HTTP_OK);
1751
5bd77a73
MS
1752 if (!con->username[0] && type == CUPSD_AUTH_NONE &&
1753 best->limit == CUPSD_AUTH_LIMIT_IPP)
ef416fc2 1754 {
1755 /*
1756 * Check for unauthenticated username...
1757 */
1758
1759 ipp_attribute_t *attr; /* requesting-user-name attribute */
1760
1761
1762 attr = ippFindAttribute(con->request, "requesting-user-name", IPP_TAG_NAME);
1763 if (attr)
1764 {
355e94dc 1765 cupsdLogMessage(CUPSD_LOG_DEBUG,
ef416fc2 1766 "cupsdIsAuthorized: requesting-user-name=\"%s\"",
1767 attr->values[0].string.text);
db1f069b 1768 strlcpy(username, attr->values[0].string.text, sizeof(username));
ef416fc2 1769 }
5bd77a73 1770 else if (best->satisfy == CUPSD_AUTH_SATISFY_ALL || auth == CUPSD_AUTH_DENY)
ef416fc2 1771 return (HTTP_UNAUTHORIZED); /* Non-anonymous needs user/pass */
1772 else
1773 return (HTTP_OK); /* unless overridden with Satisfy */
1774 }
fa73b229 1775 else
1776 {
355e94dc 1777 cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdIsAuthorized: username=\"%s\"",
fa73b229 1778 con->username);
1779
f7deaa1a 1780#ifdef HAVE_AUTHORIZATION_H
1781 if (!con->username[0] && !con->authref)
1782#else
fa73b229 1783 if (!con->username[0])
f7deaa1a 1784#endif /* HAVE_AUTHORIZATION_H */
fa73b229 1785 {
5bd77a73 1786 if (best->satisfy == CUPSD_AUTH_SATISFY_ALL || auth == CUPSD_AUTH_DENY)
fa73b229 1787 return (HTTP_UNAUTHORIZED); /* Non-anonymous needs user/pass */
1788 else
1789 return (HTTP_OK); /* unless overridden with Satisfy */
1790 }
1791
eac3a0a0 1792
5bd77a73 1793 if (con->type != type && type != CUPSD_AUTH_NONE &&
eac3a0a0
MS
1794#ifdef HAVE_GSSAPI
1795 (type != CUPSD_AUTH_NEGOTIATE || con->gss_uid <= 0) &&
1796#endif /* HAVE_GSSAPI */
e0660879 1797 con->type != CUPSD_AUTH_BASIC)
2fb76298 1798 {
e0660879 1799 cupsdLogMessage(CUPSD_LOG_ERROR, "Authorized using %s, expected %s.",
2fb76298
MS
1800 types[con->type], types[type]);
1801
1802 return (HTTP_UNAUTHORIZED);
1803 }
1804
db1f069b 1805 strlcpy(username, con->username, sizeof(username));
fa73b229 1806 }
ef416fc2 1807
1808 /*
fa73b229 1809 * OK, got a username. See if we need normal user access, or group
ef416fc2 1810 * access... (root always matches)
1811 */
1812
fa73b229 1813 if (!strcmp(username, "root"))
ef416fc2 1814 return (HTTP_OK);
1815
db1f069b
MS
1816 /*
1817 * Strip any @domain or @KDC from the username and owner...
1818 */
1819
1820 if ((ptr = strchr(username, '@')) != NULL)
1821 *ptr = '\0';
1822
1823 if (owner)
1824 {
1825 strlcpy(ownername, owner, sizeof(ownername));
1826
1827 if ((ptr = strchr(ownername, '@')) != NULL)
1828 *ptr = '\0';
1829 }
1830 else
1831 ownername[0] = '\0';
1832
ef416fc2 1833 /*
1834 * Get the user info...
1835 */
1836
f7deaa1a 1837 if (username[0])
1838 {
1839 pw = getpwnam(username);
1840 endpwent();
1841 }
1842 else
1843 pw = NULL;
ef416fc2 1844
5bd77a73 1845 if (best->level == CUPSD_AUTH_USER)
ef416fc2 1846 {
1847 /*
1848 * If there are no names associated with this location, then
1849 * any valid user is OK...
1850 */
1851
10d09e33 1852 if (cupsArrayCount(best->names) == 0)
ef416fc2 1853 return (HTTP_OK);
1854
1855 /*
1856 * Otherwise check the user list and return OK if this user is
1857 * allowed...
1858 */
1859
1860 cupsdLogMessage(CUPSD_LOG_DEBUG2,
1861 "cupsdIsAuthorized: Checking user membership...");
1862
f7deaa1a 1863#ifdef HAVE_AUTHORIZATION_H
1864 /*
1865 * If an authorization reference was supplied it must match a right name...
1866 */
1867
1868 if (con->authref)
1869 {
10d09e33
MS
1870 for (name = (char *)cupsArrayFirst(best->names);
1871 name;
1872 name = (char *)cupsArrayNext(best->names))
f7deaa1a 1873 {
88f9aafc 1874 if (!_cups_strncasecmp(name, "@AUTHKEY(", 9) && check_authref(con, name + 9))
f7deaa1a 1875 return (HTTP_OK);
88f9aafc 1876 else if (!_cups_strcasecmp(name, "@SYSTEM") && SystemGroupAuthKey &&
f7deaa1a 1877 check_authref(con, SystemGroupAuthKey))
1878 return (HTTP_OK);
1879 }
1880
ee571f26 1881 return (HTTP_FORBIDDEN);
f7deaa1a 1882 }
1883#endif /* HAVE_AUTHORIZATION_H */
1884
10d09e33
MS
1885 for (name = (char *)cupsArrayFirst(best->names);
1886 name;
1887 name = (char *)cupsArrayNext(best->names))
ef416fc2 1888 {
88f9aafc
MS
1889 if (!_cups_strcasecmp(name, "@OWNER") && owner &&
1890 !_cups_strcasecmp(username, ownername))
ef416fc2 1891 return (HTTP_OK);
88f9aafc 1892 else if (!_cups_strcasecmp(name, "@SYSTEM"))
ef416fc2 1893 {
10d09e33
MS
1894 for (i = 0; i < NumSystemGroups; i ++)
1895 if (cupsdCheckGroup(username, pw, SystemGroups[i]))
ef416fc2 1896 return (HTTP_OK);
1897 }
10d09e33 1898 else if (name[0] == '@')
ef416fc2 1899 {
10d09e33 1900 if (cupsdCheckGroup(username, pw, name + 1))
ef416fc2 1901 return (HTTP_OK);
1902 }
88f9aafc 1903 else if (!_cups_strcasecmp(username, name))
ef416fc2 1904 return (HTTP_OK);
1905 }
1906
85dda01c 1907 return (con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED);
ef416fc2 1908 }
1909
1910 /*
1911 * Check to see if this user is in any of the named groups...
1912 */
1913
1914 cupsdLogMessage(CUPSD_LOG_DEBUG2,
1915 "cupsdIsAuthorized: Checking group membership...");
1916
1917 /*
1918 * Check to see if this user is in any of the named groups...
1919 */
1920
10d09e33
MS
1921 for (name = (char *)cupsArrayFirst(best->names);
1922 name;
1923 name = (char *)cupsArrayNext(best->names))
ef416fc2 1924 {
1925 cupsdLogMessage(CUPSD_LOG_DEBUG2,
1926 "cupsdIsAuthorized: Checking group \"%s\" membership...",
10d09e33 1927 name);
ef416fc2 1928
88f9aafc 1929 if (!_cups_strcasecmp(name, "@SYSTEM"))
ef416fc2 1930 {
10d09e33
MS
1931 for (i = 0; i < NumSystemGroups; i ++)
1932 if (cupsdCheckGroup(username, pw, SystemGroups[i]))
ef416fc2 1933 return (HTTP_OK);
1934 }
10d09e33 1935 else if (cupsdCheckGroup(username, pw, name))
ef416fc2 1936 return (HTTP_OK);
1937 }
1938
1939 /*
1940 * The user isn't part of the specified group, so deny access...
1941 */
1942
355e94dc 1943 cupsdLogMessage(CUPSD_LOG_DEBUG,
ef416fc2 1944 "cupsdIsAuthorized: User not in group(s)!");
1945
85dda01c 1946 return (con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED);
ef416fc2 1947}
1948
1949
1950/*
10d09e33
MS
1951 * 'cupsdNewLocation()' - Create a new location for authorization.
1952 *
1953 * Note: Still need to call cupsdAddLocation() to add it to the list of global
1954 * locations.
ef416fc2 1955 */
1956
10d09e33
MS
1957cupsd_location_t * /* O - Pointer to new location record */
1958cupsdNewLocation(const char *location) /* I - Location path */
ef416fc2 1959{
10d09e33 1960 cupsd_location_t *temp; /* New location */
ef416fc2 1961
1962
1963 /*
10d09e33 1964 * Try to allocate memory for the new location.
ef416fc2 1965 */
1966
10d09e33 1967 if ((temp = calloc(1, sizeof(cupsd_location_t))) == NULL)
ef416fc2 1968 return (NULL);
1969
1970 /*
10d09e33 1971 * Initialize the record and copy the name over...
ef416fc2 1972 */
1973
10d09e33
MS
1974 if ((temp->location = _cupsStrAlloc(location)) == NULL)
1975 {
1976 free(temp);
ef416fc2 1977 return (NULL);
10d09e33 1978 }
ef416fc2 1979
10d09e33 1980 temp->length = strlen(temp->location);
ef416fc2 1981
1982 /*
10d09e33 1983 * Return the new record...
ef416fc2 1984 */
1985
ef416fc2 1986 return (temp);
1987}
1988
1989
f7deaa1a 1990#ifdef HAVE_AUTHORIZATION_H
1991/*
1992 * 'check_authref()' - Check if an authorization services reference has the
1993 * supplied right.
1994 */
1995
1996static int /* O - 1 if right is valid, 0 otherwise */
1997check_authref(cupsd_client_t *con, /* I - Connection */
1998 const char *right) /* I - Right name */
1999{
2000 OSStatus status; /* OS Status */
2001 AuthorizationItem authright; /* Authorization right */
2002 AuthorizationRights authrights; /* Authorization rights */
2003 AuthorizationFlags authflags; /* Authorization flags */
2004
2005
2006 /*
2007 * Check to see if the user is allowed to perform the task...
2008 */
2009
2010 if (!con->authref)
2011 return (0);
2012
2013 authright.name = right;
2014 authright.valueLength = 0;
2015 authright.value = NULL;
2016 authright.flags = 0;
2017
2018 authrights.count = 1;
2019 authrights.items = &authright;
2020
c8fef167 2021 authflags = kAuthorizationFlagDefaults |
f7deaa1a 2022 kAuthorizationFlagExtendRights;
2023
c8fef167
MS
2024 if ((status = AuthorizationCopyRights(con->authref, &authrights,
2025 kAuthorizationEmptyEnvironment,
f7deaa1a 2026 authflags, NULL)) != 0)
2027 {
2028 cupsdLogMessage(CUPSD_LOG_ERROR,
2029 "AuthorizationCopyRights(\"%s\") returned %d (%s)",
2030 authright.name, (int)status, cssmErrorString(status));
2031 return (0);
2032 }
2033
2034 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2035 "AuthorizationCopyRights(\"%s\") succeeded!",
2036 authright.name);
2037
2038 return (1);
2039}
2040#endif /* HAVE_AUTHORIZATION_H */
2041
2042
bd7854cb 2043/*
2044 * 'compare_locations()' - Compare two locations.
2045 */
2046
2047static int /* O - Result of comparison */
2048compare_locations(cupsd_location_t *a, /* I - First location */
2049 cupsd_location_t *b) /* I - Second location */
2050{
2051 return (strcmp(b->location, a->location));
2052}
2053
2054
10d09e33
MS
2055/*
2056 * 'copy_authmask()' - Copy function for auth masks.
2057 */
2058
2059static cupsd_authmask_t * /* O - New auth mask */
2060copy_authmask(cupsd_authmask_t *mask, /* I - Existing auth mask */
2061 void *data) /* I - User data (unused) */
2062{
2063 cupsd_authmask_t *temp; /* New auth mask */
2064
2065
2066 (void)data;
2067
2068 if ((temp = malloc(sizeof(cupsd_authmask_t))) != NULL)
2069 {
2070 memcpy(temp, mask, sizeof(cupsd_authmask_t));
2071
2072 if (temp->type == CUPSD_AUTH_NAME || temp->type == CUPSD_AUTH_INTERFACE)
2073 {
2074 /*
2075 * Make a copy of the name...
2076 */
2077
2078 if ((temp->mask.name.name = _cupsStrAlloc(temp->mask.name.name)) == NULL)
2079 {
2080 /*
2081 * Failed to make copy...
2082 */
2083
2084 free(temp);
2085 temp = NULL;
2086 }
2087 }
2088 }
2089
2090 return (temp);
2091}
2092
2093
5a1d7a17 2094#if !HAVE_LIBPAM
ef416fc2 2095/*
2096 * 'cups_crypt()' - Encrypt the password using the DES or MD5 algorithms,
2097 * as needed.
2098 */
2099
2100static char * /* O - Encrypted password */
2101cups_crypt(const char *pw, /* I - Password string */
2102 const char *salt) /* I - Salt (key) string */
2103{
bd7854cb 2104 if (!strncmp(salt, "$1$", 3))
ef416fc2 2105 {
2106 /*
2107 * Use MD5 passwords without the benefit of PAM; this is for
2108 * Slackware Linux, and the algorithm was taken from the
2109 * old shadow-19990827/lib/md5crypt.c source code... :(
2110 */
2111
2112 int i; /* Looping var */
2113 unsigned long n; /* Output number */
2114 int pwlen; /* Length of password string */
2115 const char *salt_end; /* End of "salt" data for MD5 */
2116 char *ptr; /* Pointer into result string */
2117 _cups_md5_state_t state; /* Primary MD5 state info */
2118 _cups_md5_state_t state2; /* Secondary MD5 state info */
2119 unsigned char digest[16]; /* MD5 digest result */
2120 static char result[120]; /* Final password string */
2121
2122
2123 /*
2124 * Get the salt data between dollar signs, e.g. $1$saltdata$md5.
2125 * Get a maximum of 8 characters of salt data after $1$...
2126 */
2127
2128 for (salt_end = salt + 3; *salt_end && (salt_end - salt) < 11; salt_end ++)
2129 if (*salt_end == '$')
2130 break;
2131
2132 /*
2133 * Compute the MD5 sum we need...
2134 */
2135
2136 pwlen = strlen(pw);
2137
757d2cad 2138 _cupsMD5Init(&state);
2139 _cupsMD5Append(&state, (unsigned char *)pw, pwlen);
2140 _cupsMD5Append(&state, (unsigned char *)salt, salt_end - salt);
ef416fc2 2141
757d2cad 2142 _cupsMD5Init(&state2);
2143 _cupsMD5Append(&state2, (unsigned char *)pw, pwlen);
2144 _cupsMD5Append(&state2, (unsigned char *)salt + 3, salt_end - salt - 3);
2145 _cupsMD5Append(&state2, (unsigned char *)pw, pwlen);
2146 _cupsMD5Finish(&state2, digest);
ef416fc2 2147
2148 for (i = pwlen; i > 0; i -= 16)
757d2cad 2149 _cupsMD5Append(&state, digest, i > 16 ? 16 : i);
ef416fc2 2150
2151 for (i = pwlen; i > 0; i >>= 1)
757d2cad 2152 _cupsMD5Append(&state, (unsigned char *)((i & 1) ? "" : pw), 1);
ef416fc2 2153
757d2cad 2154 _cupsMD5Finish(&state, digest);
ef416fc2 2155
2156 for (i = 0; i < 1000; i ++)
2157 {
757d2cad 2158 _cupsMD5Init(&state);
ef416fc2 2159
2160 if (i & 1)
757d2cad 2161 _cupsMD5Append(&state, (unsigned char *)pw, pwlen);
ef416fc2 2162 else
757d2cad 2163 _cupsMD5Append(&state, digest, 16);
ef416fc2 2164
2165 if (i % 3)
757d2cad 2166 _cupsMD5Append(&state, (unsigned char *)salt + 3, salt_end - salt - 3);
ef416fc2 2167
2168 if (i % 7)
757d2cad 2169 _cupsMD5Append(&state, (unsigned char *)pw, pwlen);
ef416fc2 2170
2171 if (i & 1)
757d2cad 2172 _cupsMD5Append(&state, digest, 16);
ef416fc2 2173 else
757d2cad 2174 _cupsMD5Append(&state, (unsigned char *)pw, pwlen);
ef416fc2 2175
757d2cad 2176 _cupsMD5Finish(&state, digest);
ef416fc2 2177 }
2178
2179 /*
2180 * Copy the final sum to the result string and return...
2181 */
2182
7d5824d6 2183 memcpy(result, salt, (size_t)(salt_end - salt));
ef416fc2 2184 ptr = result + (salt_end - salt);
2185 *ptr++ = '$';
2186
2187 for (i = 0; i < 5; i ++, ptr += 4)
2188 {
7d5824d6 2189 n = ((((unsigned)digest[i] << 8) | (unsigned)digest[i + 6]) << 8);
ef416fc2 2190
2191 if (i < 4)
7d5824d6 2192 n |= (unsigned)digest[i + 12];
ef416fc2 2193 else
7d5824d6 2194 n |= (unsigned)digest[5];
ef416fc2 2195
2196 to64(ptr, n, 4);
2197 }
2198
7d5824d6 2199 to64(ptr, (unsigned)digest[11], 2);
ef416fc2 2200 ptr += 2;
2201 *ptr = '\0';
2202
2203 return (result);
2204 }
2205 else
2206 {
2207 /*
2208 * Use the standard crypt() function...
2209 */
2210
2211 return (crypt(pw, salt));
2212 }
2213}
5a1d7a17 2214#endif /* !HAVE_LIBPAM */
ef416fc2 2215
2216
10d09e33
MS
2217/*
2218 * 'free_authmask()' - Free function for auth masks.
2219 */
2220
2221static void
2222free_authmask(cupsd_authmask_t *mask, /* I - Auth mask to free */
2223 void *data) /* I - User data (unused) */
2224{
2225 (void)data;
2226
2227 if (mask->type == CUPSD_AUTH_NAME || mask->type == CUPSD_AUTH_INTERFACE)
2228 _cupsStrFree(mask->mask.name.name);
2229
2230 free(mask);
2231}
2232
2233
ef416fc2 2234#if HAVE_LIBPAM
2235/*
2236 * 'pam_func()' - PAM conversation function.
2237 */
2238
2239static int /* O - Success or failure */
2240pam_func(
2241 int num_msg, /* I - Number of messages */
2242 const struct pam_message **msg, /* I - Messages */
2243 struct pam_response **resp, /* O - Responses */
2244 void *appdata_ptr)
2245 /* I - Pointer to connection */
2246{
2247 int i; /* Looping var */
2248 struct pam_response *replies; /* Replies */
2249 cupsd_authdata_t *data; /* Pointer to auth data */
2250
2251
2252 /*
2253 * Allocate memory for the responses...
2254 */
2255
7e86f2f6 2256 if ((replies = malloc(sizeof(struct pam_response) * (size_t)num_msg)) == NULL)
ef416fc2 2257 return (PAM_CONV_ERR);
2258
2259 /*
2260 * Answer all of the messages...
2261 */
2262
2263 DEBUG_printf(("pam_func: appdata_ptr = %p\n", appdata_ptr));
2264
ef416fc2 2265 data = (cupsd_authdata_t *)appdata_ptr;
ef416fc2 2266
2267 for (i = 0; i < num_msg; i ++)
2268 {
2269 DEBUG_printf(("pam_func: Message = \"%s\"\n", msg[i]->msg));
2270
2271 switch (msg[i]->msg_style)
2272 {
2273 case PAM_PROMPT_ECHO_ON:
2274 DEBUG_printf(("pam_func: PAM_PROMPT_ECHO_ON, returning \"%s\"...\n",
2275 data->username));
2276 replies[i].resp_retcode = PAM_SUCCESS;
2277 replies[i].resp = strdup(data->username);
2278 break;
2279
2280 case PAM_PROMPT_ECHO_OFF:
2281 DEBUG_printf(("pam_func: PAM_PROMPT_ECHO_OFF, returning \"%s\"...\n",
2282 data->password));
2283 replies[i].resp_retcode = PAM_SUCCESS;
2284 replies[i].resp = strdup(data->password);
2285 break;
2286
2287 case PAM_TEXT_INFO:
2288 DEBUG_puts("pam_func: PAM_TEXT_INFO...");
2289 replies[i].resp_retcode = PAM_SUCCESS;
2290 replies[i].resp = NULL;
2291 break;
2292
2293 case PAM_ERROR_MSG:
2294 DEBUG_puts("pam_func: PAM_ERROR_MSG...");
2295 replies[i].resp_retcode = PAM_SUCCESS;
2296 replies[i].resp = NULL;
2297 break;
2298
2299 default:
2300 DEBUG_printf(("pam_func: Unknown PAM message %d...\n",
2301 msg[i]->msg_style));
2302 free(replies);
2303 return (PAM_CONV_ERR);
2304 }
2305 }
2306
2307 /*
2308 * Return the responses back to PAM...
2309 */
2310
2311 *resp = replies;
2312
2313 return (PAM_SUCCESS);
2314}
5a1d7a17 2315#else
ef416fc2 2316
2317
2318/*
2319 * 'to64()' - Base64-encode an integer value...
2320 */
2321
2322static void
2323to64(char *s, /* O - Output string */
2324 unsigned long v, /* I - Value to encode */
2325 int n) /* I - Number of digits */
2326{
2327 const char *itoa64 = "./0123456789"
2328 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2329 "abcdefghijklmnopqrstuvwxyz";
2330
2331
2332 for (; n > 0; n --, v >>= 6)
2333 *s++ = itoa64[v & 0x3f];
2334}
2335#endif /* HAVE_LIBPAM */
2336
2337
2338/*
f2d18633 2339 * End of "$Id$".
ef416fc2 2340 */