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