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