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