]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
db1f069b | 2 | * "$Id: auth.c 6779 2007-08-08 19:49:48Z mike $" |
ef416fc2 | 3 | * |
4 | * Authentication functions for the Common UNIX Printing System (CUPS). | |
5 | * | |
91c84a35 | 6 | * Copyright 2007-2008 by Apple Inc. |
b86bc4cf | 7 | * Copyright 1997-2007 by Easy Software Products. |
ef416fc2 | 8 | * |
f7deaa1a | 9 | * This file contains Kerberos support code, copyright 2006 by |
10 | * Jelmer Vernooij. | |
11 | * | |
ef416fc2 | 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 | * This file is subject to the Apple OS-Developed Software exception. | |
19 | * | |
20 | * Contents: | |
21 | * | |
22 | * cupsDoAuthentication() - Authenticate a request. | |
f7deaa1a | 23 | * DEBUG_gss_printf() - Show debug error messages from GSSAPI... |
24 | * cups_get_gss_creds() - Get CUPS service credentials for authentication. | |
ef416fc2 | 25 | * cups_local_auth() - Get the local authorization certificate if |
26 | * available/applicable... | |
27 | */ | |
28 | ||
29 | /* | |
30 | * Include necessary headers... | |
31 | */ | |
32 | ||
33 | #include "globals.h" | |
34 | #include "debug.h" | |
35 | #include <stdlib.h> | |
36 | #include <ctype.h> | |
37 | #include <errno.h> | |
38 | #include <fcntl.h> | |
39 | #include <sys/stat.h> | |
40 | #if defined(WIN32) || defined(__EMX__) | |
41 | # include <io.h> | |
42 | #else | |
43 | # include <unistd.h> | |
44 | #endif /* WIN32 || __EMX__ */ | |
45 | ||
f7deaa1a | 46 | #if HAVE_AUTHORIZATION_H |
47 | # include <Security/Authorization.h> | |
48 | # ifdef HAVE_SECBASEPRIV_H | |
49 | # include <Security/SecBasePriv.h> | |
50 | # else | |
51 | extern const char *cssmErrorString(int error); | |
52 | # endif /* HAVE_SECBASEPRIV_H */ | |
53 | #endif /* HAVE_AUTHORIZATION_H */ | |
54 | ||
bc44d920 | 55 | #if defined(SO_PEERCRED) && defined(AF_LOCAL) |
56 | # include <pwd.h> | |
57 | #endif /* SO_PEERCRED && AF_LOCAL */ | |
58 | ||
ef416fc2 | 59 | |
60 | /* | |
61 | * Local functions... | |
62 | */ | |
63 | ||
f7deaa1a | 64 | #ifdef HAVE_GSSAPI |
65 | # ifdef DEBUG | |
66 | static void DEBUG_gss_printf(OM_uint32 major_status, OM_uint32 minor_status, | |
67 | const char *message); | |
355e94dc MS |
68 | # else |
69 | # define DEBUG_gss_printf(major, minor, message) | |
f7deaa1a | 70 | # endif /* DEBUG */ |
71 | static gss_name_t cups_get_gss_creds(http_t *http, const char *service_name); | |
72 | #endif /* HAVE_GSSAPI */ | |
ef416fc2 | 73 | static int cups_local_auth(http_t *http); |
74 | ||
75 | ||
76 | /* | |
77 | * 'cupsDoAuthentication()' - Authenticate a request. | |
78 | * | |
5a738aea | 79 | * This function should be called in response to a @code HTTP_UNAUTHORIZED@ |
ef416fc2 | 80 | * status, prior to resubmitting your request. |
81 | * | |
82 | * @since CUPS 1.1.20@ | |
83 | */ | |
84 | ||
85 | int /* O - 0 on success, -1 on error */ | |
568fa3fa | 86 | cupsDoAuthentication(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
5a738aea | 87 | const char *method,/* I - Request method ("GET", "POST", "PUT") */ |
ef416fc2 | 88 | const char *resource) |
89 | /* I - Resource path */ | |
90 | { | |
91 | const char *password; /* Password string */ | |
92 | char prompt[1024], /* Prompt for user */ | |
93 | realm[HTTP_MAX_VALUE], /* realm="xyz" string */ | |
5a738aea | 94 | nonce[HTTP_MAX_VALUE]; /* nonce="xyz" string */ |
f7deaa1a | 95 | int localauth; /* Local authentication result */ |
b86bc4cf | 96 | _cups_globals_t *cg; /* Global data */ |
ef416fc2 | 97 | |
98 | ||
99 | DEBUG_printf(("cupsDoAuthentication(http=%p, method=\"%s\", resource=\"%s\")\n", | |
100 | http, method, resource)); | |
101 | DEBUG_printf(("cupsDoAuthentication: digest_tries=%d, userpass=\"%s\"\n", | |
102 | http->digest_tries, http->userpass)); | |
07725fee | 103 | DEBUG_printf(("cupsDoAuthentication: WWW-Authenticate=\"%s\"\n", |
104 | httpGetField(http, HTTP_FIELD_WWW_AUTHENTICATE))); | |
ef416fc2 | 105 | |
106 | /* | |
107 | * Clear the current authentication string... | |
108 | */ | |
109 | ||
355e94dc | 110 | httpSetAuthString(http, NULL, NULL); |
ef416fc2 | 111 | |
112 | /* | |
113 | * See if we can do local authentication... | |
114 | */ | |
115 | ||
f7deaa1a | 116 | if (http->digest_tries < 3) |
ef416fc2 | 117 | { |
f7deaa1a | 118 | if ((localauth = cups_local_auth(http)) == 0) |
119 | { | |
120 | DEBUG_printf(("cupsDoAuthentication: authstring=\"%s\"\n", | |
121 | http->authstring)); | |
122 | ||
123 | if (http->status == HTTP_UNAUTHORIZED) | |
124 | http->digest_tries ++; | |
125 | ||
126 | return (0); | |
127 | } | |
128 | else if (localauth == -1) | |
129 | return (-1); /* Error or canceled */ | |
ef416fc2 | 130 | } |
131 | ||
132 | /* | |
133 | * Nope, see if we should retry the current username:password... | |
134 | */ | |
135 | ||
f7deaa1a | 136 | if ((http->digest_tries > 1 || !http->userpass[0]) && |
137 | strncmp(http->fields[HTTP_FIELD_WWW_AUTHENTICATE], "Negotiate", 9)) | |
ef416fc2 | 138 | { |
139 | /* | |
140 | * Nope - get a new password from the user... | |
141 | */ | |
142 | ||
b86bc4cf | 143 | cg = _cupsGlobals(); |
144 | ||
145 | if (!cg->lang_default) | |
146 | cg->lang_default = cupsLangDefault(); | |
147 | ||
148 | snprintf(prompt, sizeof(prompt), | |
149 | _cupsLangString(cg->lang_default, _("Password for %s on %s? ")), | |
150 | cupsUser(), | |
151 | http->hostname[0] == '/' ? "localhost" : http->hostname); | |
ef416fc2 | 152 | |
153 | http->digest_tries = strncasecmp(http->fields[HTTP_FIELD_WWW_AUTHENTICATE], | |
154 | "Digest", 5) != 0; | |
155 | http->userpass[0] = '\0'; | |
156 | ||
157 | if ((password = cupsGetPassword(prompt)) == NULL) | |
158 | return (-1); | |
159 | ||
160 | if (!password[0]) | |
161 | return (-1); | |
162 | ||
163 | snprintf(http->userpass, sizeof(http->userpass), "%s:%s", cupsUser(), | |
164 | password); | |
165 | } | |
166 | else if (http->status == HTTP_UNAUTHORIZED) | |
167 | http->digest_tries ++; | |
168 | ||
169 | /* | |
170 | * Got a password; encode it for the server... | |
171 | */ | |
172 | ||
f7deaa1a | 173 | if (!strncmp(http->fields[HTTP_FIELD_WWW_AUTHENTICATE], "Negotiate", 9)) |
174 | { | |
175 | #ifdef HAVE_GSSAPI | |
176 | /* | |
177 | * Kerberos authentication... | |
178 | */ | |
179 | ||
180 | OM_uint32 minor_status, /* Minor status code */ | |
181 | major_status; /* Major status code */ | |
182 | gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER, | |
183 | /* Output token */ | |
184 | input_token = GSS_C_EMPTY_BUFFER; | |
185 | /* Input token */ | |
186 | char *gss_service_name; | |
187 | /* GSS service name */ | |
355e94dc | 188 | # ifdef USE_SPNEGO |
f7deaa1a | 189 | const char *authorization; |
190 | /* Pointer into Authorization string */ | |
355e94dc | 191 | # endif /* USE_SPNEGO */ |
f7deaa1a | 192 | |
193 | ||
f42414bf | 194 | # ifdef __APPLE__ |
195 | /* | |
196 | * If the weak-linked GSSAPI/Kerberos library is not present, don't try | |
197 | * to use it... | |
198 | */ | |
199 | ||
200 | if (gss_init_sec_context == NULL) | |
201 | { | |
202 | DEBUG_puts("cupsDoAuthentication: Weak-linked GSSAPI/Kerberos framework " | |
203 | "is not present"); | |
204 | return (-1); | |
205 | } | |
206 | # endif /* __APPLE__ */ | |
207 | ||
355e94dc MS |
208 | if (http->status == HTTP_UNAUTHORIZED && http->digest_tries >= 3) |
209 | { | |
210 | DEBUG_printf(("cupsDoAuthentication: too many Negotiate tries (%d)\n", | |
211 | http->digest_tries)); | |
212 | ||
213 | return (-1); | |
214 | } | |
215 | ||
f7deaa1a | 216 | if (http->gssname == GSS_C_NO_NAME) |
217 | { | |
218 | if ((gss_service_name = getenv("CUPS_GSSSERVICENAME")) == NULL) | |
219 | gss_service_name = CUPS_DEFAULT_GSSSERVICENAME; | |
220 | else | |
221 | DEBUG_puts("cupsDoAuthentication: GSS service name set via environment"); | |
222 | ||
223 | http->gssname = cups_get_gss_creds(http, gss_service_name); | |
224 | } | |
225 | ||
355e94dc | 226 | # ifdef USE_SPNEGO /* We don't implement SPNEGO just yet... */ |
f7deaa1a | 227 | /* |
228 | * Find the start of the Kerberos input token... | |
229 | */ | |
230 | ||
231 | authorization = httpGetField(http, HTTP_FIELD_WWW_AUTHENTICATE); | |
232 | ||
233 | authorization += 9; | |
234 | while (*authorization && isspace(*authorization & 255)) | |
235 | authorization ++; | |
236 | ||
237 | if (*authorization) | |
238 | { | |
239 | /* | |
355e94dc | 240 | * Decode the authorization string to get the input token... |
f7deaa1a | 241 | */ |
355e94dc MS |
242 | |
243 | int len = strlen(authorization); | |
244 | ||
245 | input_token.value = malloc(len); | |
246 | input_token.value = httpDecode64_2(input_token.value, &len, | |
247 | authorization); | |
248 | input_token.length = len; | |
249 | ||
250 | # ifdef DEBUG | |
251 | { | |
252 | char *ptr = (char *)input_token.value; | |
253 | int left = len; | |
254 | ||
255 | fputs("input_token=", stdout); | |
256 | while (left > 0) | |
257 | { | |
258 | if (*ptr < ' ') | |
259 | printf("\\%03o", *ptr & 255); | |
260 | else | |
261 | putchar(*ptr); | |
262 | ptr ++; | |
263 | left --; | |
264 | } | |
265 | putchar('\n'); | |
266 | } | |
267 | # endif /* DEBUG */ | |
f7deaa1a | 268 | } |
355e94dc | 269 | # endif /* USE_SPNEGO */ |
bc44d920 | 270 | |
271 | if (http->gssctx != GSS_C_NO_CONTEXT) | |
f7deaa1a | 272 | { |
bc44d920 | 273 | major_status = gss_delete_sec_context(&minor_status, &http->gssctx, |
274 | GSS_C_NO_BUFFER); | |
275 | http->gssctx = GSS_C_NO_CONTEXT; | |
f7deaa1a | 276 | } |
277 | ||
278 | major_status = gss_init_sec_context(&minor_status, GSS_C_NO_CREDENTIAL, | |
279 | &http->gssctx, | |
280 | http->gssname, http->gssmech, | |
355e94dc MS |
281 | GSS_C_DELEG_FLAG | GSS_C_MUTUAL_FLAG | |
282 | GSS_C_INTEG_FLAG, | |
bc44d920 | 283 | GSS_C_INDEFINITE, |
f7deaa1a | 284 | GSS_C_NO_CHANNEL_BINDINGS, |
285 | &input_token, &http->gssmech, | |
286 | &output_token, NULL, NULL); | |
287 | ||
288 | if (input_token.value) | |
289 | free(input_token.value); | |
290 | ||
291 | if (GSS_ERROR(major_status)) | |
292 | { | |
293 | # ifdef DEBUG | |
294 | DEBUG_gss_printf(major_status, minor_status, | |
7ff4fea9 | 295 | "Unable to initialize security context"); |
f7deaa1a | 296 | # endif /* DEBUG */ |
297 | return (-1); | |
298 | } | |
299 | ||
f7deaa1a | 300 | if (major_status == GSS_S_CONTINUE_NEEDED) |
301 | DEBUG_gss_printf(major_status, minor_status, "Continuation needed!"); | |
f7deaa1a | 302 | |
5a738aea | 303 | if (output_token.length > 0 && output_token.length <= 65536) |
f7deaa1a | 304 | { |
5a738aea MS |
305 | /* |
306 | * Allocate the authorization string since Windows KDCs can have | |
307 | * arbitrarily large credentials... | |
308 | */ | |
309 | ||
310 | int authsize = 10 + /* "Negotiate " */ | |
311 | output_token.length * 4 / 3 + 1 + /* Base64 */ | |
312 | 1; /* nul */ | |
313 | ||
314 | httpSetAuthString(http, NULL, NULL); | |
315 | ||
316 | if ((http->authstring = malloc(authsize)) == NULL) | |
317 | { | |
318 | http->authstring = http->_authstring; | |
319 | authsize = sizeof(http->_authstring); | |
320 | } | |
321 | ||
322 | strcpy(http->authstring, "Negotiate "); | |
323 | httpEncode64_2(http->authstring + 10, authsize - 10, output_token.value, | |
f7deaa1a | 324 | output_token.length); |
f7deaa1a | 325 | |
326 | major_status = gss_release_buffer(&minor_status, &output_token); | |
327 | } | |
5a738aea MS |
328 | else |
329 | { | |
330 | DEBUG_printf(("cupsDoAuthentication: Kerberos credentials too large - " | |
331 | "%d bytes!\n", output_token.length)); | |
332 | ||
333 | major_status = gss_release_buffer(&minor_status, &output_token); | |
334 | ||
335 | return (-1); | |
336 | } | |
f7deaa1a | 337 | #endif /* HAVE_GSSAPI */ |
338 | } | |
339 | else if (strncmp(http->fields[HTTP_FIELD_WWW_AUTHENTICATE], "Digest", 6)) | |
ef416fc2 | 340 | { |
341 | /* | |
342 | * Basic authentication... | |
343 | */ | |
344 | ||
5a738aea MS |
345 | char encode[256]; /* Base64 buffer */ |
346 | ||
347 | ||
ef416fc2 | 348 | httpEncode64_2(encode, sizeof(encode), http->userpass, |
b86bc4cf | 349 | (int)strlen(http->userpass)); |
355e94dc | 350 | httpSetAuthString(http, "Basic", encode); |
ef416fc2 | 351 | } |
352 | else | |
353 | { | |
354 | /* | |
355 | * Digest authentication... | |
356 | */ | |
357 | ||
5a738aea MS |
358 | char encode[33], /* MD5 buffer */ |
359 | digest[1024]; /* Digest auth data */ | |
355e94dc MS |
360 | |
361 | ||
ef416fc2 | 362 | httpGetSubField(http, HTTP_FIELD_WWW_AUTHENTICATE, "realm", realm); |
363 | httpGetSubField(http, HTTP_FIELD_WWW_AUTHENTICATE, "nonce", nonce); | |
364 | ||
365 | httpMD5(cupsUser(), realm, strchr(http->userpass, ':') + 1, encode); | |
366 | httpMD5Final(nonce, method, resource, encode); | |
355e94dc MS |
367 | snprintf(digest, sizeof(digest), |
368 | "username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", " | |
369 | "response=\"%s\"", cupsUser(), realm, nonce, resource, encode); | |
370 | httpSetAuthString(http, "Digest", digest); | |
ef416fc2 | 371 | } |
372 | ||
373 | DEBUG_printf(("cupsDoAuthentication: authstring=\"%s\"\n", http->authstring)); | |
374 | ||
375 | return (0); | |
376 | } | |
377 | ||
378 | ||
f7deaa1a | 379 | #ifdef HAVE_GSSAPI |
380 | # ifdef DEBUG | |
381 | /* | |
382 | * 'DEBUG_gss_printf()' - Show debug error messages from GSSAPI... | |
383 | */ | |
384 | ||
385 | static void | |
386 | DEBUG_gss_printf(OM_uint32 major_status,/* I - Major status code */ | |
387 | OM_uint32 minor_status,/* I - Minor status code */ | |
388 | const char *message) /* I - Prefix for error message */ | |
389 | { | |
390 | OM_uint32 err_major_status, /* Major status code for display */ | |
391 | err_minor_status; /* Minor status code for display */ | |
392 | OM_uint32 msg_ctx; /* Message context */ | |
393 | gss_buffer_desc major_status_string = GSS_C_EMPTY_BUFFER, | |
394 | /* Major status message */ | |
395 | minor_status_string = GSS_C_EMPTY_BUFFER; | |
396 | /* Minor status message */ | |
397 | ||
398 | ||
399 | msg_ctx = 0; | |
400 | err_major_status = gss_display_status(&err_minor_status, | |
401 | major_status, | |
402 | GSS_C_GSS_CODE, | |
403 | GSS_C_NO_OID, | |
404 | &msg_ctx, | |
405 | &major_status_string); | |
406 | ||
407 | if (!GSS_ERROR(err_major_status)) | |
408 | err_major_status = gss_display_status(&err_minor_status, | |
409 | minor_status, | |
410 | GSS_C_MECH_CODE, | |
411 | GSS_C_NULL_OID, | |
412 | &msg_ctx, | |
413 | &minor_status_string); | |
414 | ||
415 | printf("%s: %s, %s\n", message, (char *)major_status_string.value, | |
416 | (char *)minor_status_string.value); | |
417 | ||
418 | gss_release_buffer(&err_minor_status, &major_status_string); | |
419 | gss_release_buffer(&err_minor_status, &minor_status_string); | |
420 | } | |
421 | # endif /* DEBUG */ | |
422 | ||
423 | ||
424 | /* | |
425 | * 'cups_get_gss_creds()' - Get CUPS service credentials for authentication. | |
426 | */ | |
427 | ||
428 | static gss_name_t /* O - Server name */ | |
429 | cups_get_gss_creds( | |
430 | http_t *http, /* I - Connection to server */ | |
431 | const char *service_name) /* I - Service name */ | |
432 | { | |
433 | gss_buffer_desc token = GSS_C_EMPTY_BUFFER; | |
434 | /* Service token */ | |
bc44d920 | 435 | OM_uint32 major_status, /* Major status code */ |
436 | minor_status; /* Minor status code */ | |
437 | gss_name_t server_name; /* Server name */ | |
438 | char buf[1024], /* Name buffer */ | |
439 | fqdn[HTTP_MAX_URI]; /* Server name buffer */ | |
440 | ||
441 | ||
442 | /* | |
443 | * Get the hostname... | |
444 | */ | |
f7deaa1a | 445 | |
bc44d920 | 446 | httpGetHostname(http, fqdn, sizeof(fqdn)); |
447 | ||
448 | if (!strcmp(fqdn, "localhost")) | |
449 | httpGetHostname(NULL, fqdn, sizeof(fqdn)); | |
f7deaa1a | 450 | |
451 | /* | |
452 | * Get a server name we can use for authentication purposes... | |
453 | */ | |
454 | ||
bc44d920 | 455 | snprintf(buf, sizeof(buf), "%s@%s", service_name, fqdn); |
f7deaa1a | 456 | |
7ff4fea9 MS |
457 | DEBUG_printf(("cups_get_gss_creds: Looking up %s...\n", buf)); |
458 | ||
f7deaa1a | 459 | token.value = buf; |
460 | token.length = strlen(buf); | |
461 | server_name = GSS_C_NO_NAME; | |
462 | major_status = gss_import_name(&minor_status, &token, | |
463 | GSS_C_NT_HOSTBASED_SERVICE, | |
464 | &server_name); | |
465 | ||
466 | if (GSS_ERROR(major_status)) | |
467 | { | |
468 | # ifdef DEBUG | |
469 | DEBUG_gss_printf(major_status, minor_status, "gss_import_name() failed"); | |
470 | # endif /* DEBUG */ | |
471 | ||
472 | return (NULL); | |
473 | } | |
474 | ||
475 | return (server_name); | |
476 | } | |
477 | #endif /* HAVE_GSSAPI */ | |
478 | ||
479 | ||
ef416fc2 | 480 | /* |
481 | * 'cups_local_auth()' - Get the local authorization certificate if | |
482 | * available/applicable... | |
483 | */ | |
484 | ||
f7deaa1a | 485 | static int /* O - 0 if available */ |
bc44d920 | 486 | /* 1 if not available */ |
f7deaa1a | 487 | /* -1 error */ |
ef416fc2 | 488 | cups_local_auth(http_t *http) /* I - HTTP connection to server */ |
489 | { | |
490 | #if defined(WIN32) || defined(__EMX__) | |
491 | /* | |
492 | * Currently WIN32 and OS-2 do not support the CUPS server... | |
493 | */ | |
494 | ||
bc44d920 | 495 | return (1); |
ef416fc2 | 496 | #else |
f7deaa1a | 497 | int pid; /* Current process ID */ |
498 | FILE *fp; /* Certificate file */ | |
499 | char filename[1024], /* Certificate filename */ | |
500 | certificate[33];/* Certificate string */ | |
ef416fc2 | 501 | _cups_globals_t *cg = _cupsGlobals(); /* Global data */ |
bc44d920 | 502 | # if defined(HAVE_AUTHORIZATION_H) |
f7deaa1a | 503 | OSStatus status; /* Status */ |
504 | AuthorizationItem auth_right; /* Authorization right */ | |
505 | AuthorizationRights auth_rights; /* Authorization rights */ | |
506 | AuthorizationFlags auth_flags; /* Authorization flags */ | |
507 | AuthorizationExternalForm auth_extrn; /* Authorization ref external */ | |
508 | char auth_key[1024]; /* Buffer */ | |
509 | char buffer[1024]; /* Buffer */ | |
bc44d920 | 510 | # endif /* HAVE_AUTHORIZATION_H */ |
ef416fc2 | 511 | |
512 | ||
513 | DEBUG_printf(("cups_local_auth(http=%p) hostaddr=%s, hostname=\"%s\"\n", | |
514 | http, httpAddrString(http->hostaddr, filename, sizeof(filename)), http->hostname)); | |
515 | ||
516 | /* | |
517 | * See if we are accessing localhost... | |
518 | */ | |
519 | ||
520 | if (!httpAddrLocalhost(http->hostaddr) && | |
521 | strcasecmp(http->hostname, "localhost") != 0) | |
522 | { | |
523 | DEBUG_puts("cups_local_auth: Not a local connection!"); | |
bc44d920 | 524 | return (1); |
ef416fc2 | 525 | } |
526 | ||
bc44d920 | 527 | # if defined(HAVE_AUTHORIZATION_H) |
f7deaa1a | 528 | /* |
529 | * Delete any previous authorization reference... | |
530 | */ | |
531 | ||
b94498cf | 532 | if (http->auth_ref) |
f7deaa1a | 533 | { |
b94498cf | 534 | AuthorizationFree(http->auth_ref, kAuthorizationFlagDefaults); |
535 | http->auth_ref = NULL; | |
f7deaa1a | 536 | } |
537 | ||
538 | if (httpGetSubField2(http, HTTP_FIELD_WWW_AUTHENTICATE, "authkey", | |
539 | auth_key, sizeof(auth_key))) | |
540 | { | |
541 | status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, | |
b94498cf | 542 | kAuthorizationFlagDefaults, &http->auth_ref); |
f7deaa1a | 543 | if (status != errAuthorizationSuccess) |
544 | { | |
545 | DEBUG_printf(("cups_local_auth: AuthorizationCreate() returned %d (%s)\n", | |
546 | (int)status, cssmErrorString(status))); | |
547 | return (-1); | |
548 | } | |
549 | ||
550 | auth_right.name = auth_key; | |
551 | auth_right.valueLength = 0; | |
552 | auth_right.value = NULL; | |
553 | auth_right.flags = 0; | |
554 | ||
555 | auth_rights.count = 1; | |
556 | auth_rights.items = &auth_right; | |
557 | ||
558 | auth_flags = kAuthorizationFlagDefaults | | |
559 | kAuthorizationFlagPreAuthorize | | |
560 | kAuthorizationFlagInteractionAllowed | | |
561 | kAuthorizationFlagExtendRights; | |
562 | ||
b94498cf | 563 | status = AuthorizationCopyRights(http->auth_ref, &auth_rights, |
f7deaa1a | 564 | kAuthorizationEmptyEnvironment, |
565 | auth_flags, NULL); | |
566 | if (status == errAuthorizationSuccess) | |
b94498cf | 567 | status = AuthorizationMakeExternalForm(http->auth_ref, &auth_extrn); |
f7deaa1a | 568 | |
569 | if (status == errAuthorizationSuccess) | |
570 | { | |
571 | /* | |
572 | * Set the authorization string and return... | |
573 | */ | |
574 | ||
575 | httpEncode64_2(buffer, sizeof(buffer), (void *)&auth_extrn, | |
576 | sizeof(auth_extrn)); | |
577 | ||
355e94dc | 578 | httpSetAuthString(http, "AuthRef", buffer); |
f7deaa1a | 579 | |
580 | DEBUG_printf(("cups_local_auth: Returning authstring = \"%s\"\n", | |
581 | http->authstring)); | |
582 | return (0); | |
583 | } | |
584 | else if (status == errAuthorizationCanceled) | |
585 | return (-1); | |
586 | ||
587 | DEBUG_printf(("cups_local_auth: AuthorizationCopyRights() returned %d (%s)\n", | |
588 | (int)status, cssmErrorString(status))); | |
589 | ||
590 | /* | |
591 | * Fall through to try certificates... | |
592 | */ | |
593 | } | |
bc44d920 | 594 | # endif /* HAVE_AUTHORIZATION_H */ |
f7deaa1a | 595 | |
ef416fc2 | 596 | /* |
597 | * Try opening a certificate file for this PID. If that fails, | |
598 | * try the root certificate... | |
599 | */ | |
600 | ||
601 | pid = getpid(); | |
602 | snprintf(filename, sizeof(filename), "%s/certs/%d", cg->cups_statedir, pid); | |
603 | if ((fp = fopen(filename, "r")) == NULL && pid > 0) | |
604 | { | |
605 | DEBUG_printf(("cups_local_auth: Unable to open file %s: %s\n", | |
606 | filename, strerror(errno))); | |
607 | ||
bc44d920 | 608 | #ifdef HAVE_GSSAPI |
609 | /* | |
610 | * If local certificate authentication isn't available for this PID, | |
611 | * check if we need Kerberos authentication... | |
612 | */ | |
613 | ||
355e94dc | 614 | if (!strncmp(http->fields[HTTP_FIELD_WWW_AUTHENTICATE], "Negotiate", 9)) |
bc44d920 | 615 | { |
616 | /* | |
617 | * Yes, don't try the root certificate... | |
618 | */ | |
619 | ||
620 | return (1); | |
621 | } | |
622 | #endif /* HAVE_GSSAPI */ | |
623 | ||
ef416fc2 | 624 | snprintf(filename, sizeof(filename), "%s/certs/0", cg->cups_statedir); |
625 | fp = fopen(filename, "r"); | |
626 | } | |
627 | ||
f7deaa1a | 628 | if (fp) |
ef416fc2 | 629 | { |
f7deaa1a | 630 | /* |
631 | * Read the certificate from the file... | |
632 | */ | |
ef416fc2 | 633 | |
f7deaa1a | 634 | fgets(certificate, sizeof(certificate), fp); |
635 | fclose(fp); | |
ef416fc2 | 636 | |
f7deaa1a | 637 | /* |
638 | * Set the authorization string and return... | |
639 | */ | |
ef416fc2 | 640 | |
355e94dc | 641 | httpSetAuthString(http, "Local", certificate); |
ef416fc2 | 642 | |
f7deaa1a | 643 | DEBUG_printf(("cups_local_auth: Returning authstring = \"%s\"\n", |
644 | http->authstring)); | |
ef416fc2 | 645 | |
f7deaa1a | 646 | return (0); |
647 | } | |
648 | ||
bc44d920 | 649 | # if defined(SO_PEERCRED) && defined(AF_LOCAL) |
650 | /* | |
651 | * See if we can authenticate using the peer credentials provided over a | |
652 | * domain socket; if so, specify "PeerCred username" as the authentication | |
653 | * information... | |
654 | */ | |
655 | ||
656 | if (http->hostaddr->addr.sa_family == AF_LOCAL && | |
657 | !getenv("GATEWAY_INTERFACE")) /* Not via CGI programs... */ | |
658 | { | |
659 | /* | |
660 | * Verify that the current cupsUser() matches the current UID... | |
661 | */ | |
662 | ||
663 | struct passwd *pwd; /* Password information */ | |
664 | const char *username; /* Current username */ | |
665 | ||
666 | username = cupsUser(); | |
667 | ||
668 | if ((pwd = getpwnam(username)) != NULL && pwd->pw_uid == getuid()) | |
669 | { | |
355e94dc | 670 | httpSetAuthString(http, "PeerCred", username); |
bc44d920 | 671 | |
672 | DEBUG_printf(("cups_local_auth: Returning authstring = \"%s\"\n", | |
673 | http->authstring)); | |
674 | ||
675 | return (0); | |
676 | } | |
677 | } | |
678 | # endif /* SO_PEERCRED && AF_LOCAL */ | |
679 | ||
f7deaa1a | 680 | return (1); |
ef416fc2 | 681 | #endif /* WIN32 || __EMX__ */ |
682 | } | |
683 | ||
684 | ||
685 | /* | |
db1f069b | 686 | * End of "$Id: auth.c 6779 2007-08-08 19:49:48Z mike $". |
ef416fc2 | 687 | */ |