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