]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * "$Id: auth.c 7720 2008-07-11 22:46:21Z mike $" | |
3 | * | |
4 | * Authentication functions for the Common UNIX Printing System (CUPS). | |
5 | * | |
6 | * Copyright 2007-2008 by Apple Inc. | |
7 | * Copyright 1997-2007 by Easy Software Products. | |
8 | * | |
9 | * This file contains Kerberos support code, copyright 2006 by | |
10 | * Jelmer Vernooij. | |
11 | * | |
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/". | |
17 | * | |
18 | * This file is subject to the Apple OS-Developed Software exception. | |
19 | * | |
20 | * Contents: | |
21 | * | |
22 | * cupsDoAuthentication() - Authenticate a request. | |
23 | * DEBUG_gss_printf() - Show debug error messages from GSSAPI... | |
24 | * cups_get_gss_creds() - Get CUPS service credentials for authentication. | |
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 | ||
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 | ||
55 | #if defined(SO_PEERCRED) && defined(AF_LOCAL) | |
56 | # include <pwd.h> | |
57 | #endif /* SO_PEERCRED && AF_LOCAL */ | |
58 | ||
59 | ||
60 | /* | |
61 | * Local functions... | |
62 | */ | |
63 | ||
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); | |
68 | # else | |
69 | # define DEBUG_gss_printf(major, minor, message) | |
70 | # endif /* DEBUG */ | |
71 | static gss_name_t cups_get_gss_creds(http_t *http, const char *service_name); | |
72 | #endif /* HAVE_GSSAPI */ | |
73 | static int cups_local_auth(http_t *http); | |
74 | ||
75 | ||
76 | /* | |
77 | * 'cupsDoAuthentication()' - Authenticate a request. | |
78 | * | |
79 | * This function should be called in response to a @code HTTP_UNAUTHORIZED@ | |
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 */ | |
86 | cupsDoAuthentication(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ | |
87 | const char *method,/* I - Request method ("GET", "POST", "PUT") */ | |
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 */ | |
94 | nonce[HTTP_MAX_VALUE]; /* nonce="xyz" string */ | |
95 | int localauth; /* Local authentication result */ | |
96 | _cups_globals_t *cg; /* Global data */ | |
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)); | |
103 | DEBUG_printf(("cupsDoAuthentication: WWW-Authenticate=\"%s\"\n", | |
104 | httpGetField(http, HTTP_FIELD_WWW_AUTHENTICATE))); | |
105 | ||
106 | /* | |
107 | * Clear the current authentication string... | |
108 | */ | |
109 | ||
110 | httpSetAuthString(http, NULL, NULL); | |
111 | ||
112 | /* | |
113 | * See if we can do local authentication... | |
114 | */ | |
115 | ||
116 | if (http->digest_tries < 3) | |
117 | { | |
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 */ | |
130 | } | |
131 | ||
132 | /* | |
133 | * Nope, see if we should retry the current username:password... | |
134 | */ | |
135 | ||
136 | if ((http->digest_tries > 1 || !http->userpass[0]) && | |
137 | strncmp(http->fields[HTTP_FIELD_WWW_AUTHENTICATE], "Negotiate", 9)) | |
138 | { | |
139 | /* | |
140 | * Nope - get a new password from the user... | |
141 | */ | |
142 | ||
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); | |
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 | ||
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 */ | |
188 | # ifdef USE_SPNEGO | |
189 | const char *authorization; | |
190 | /* Pointer into Authorization string */ | |
191 | # endif /* USE_SPNEGO */ | |
192 | ||
193 | ||
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 | ||
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 | ||
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 | ||
226 | # ifdef USE_SPNEGO /* We don't implement SPNEGO just yet... */ | |
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 | /* | |
240 | * Decode the authorization string to get the input token... | |
241 | */ | |
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 */ | |
268 | } | |
269 | # endif /* USE_SPNEGO */ | |
270 | ||
271 | if (http->gssctx != GSS_C_NO_CONTEXT) | |
272 | { | |
273 | gss_delete_sec_context(&minor_status, &http->gssctx, GSS_C_NO_BUFFER); | |
274 | http->gssctx = GSS_C_NO_CONTEXT; | |
275 | } | |
276 | ||
277 | major_status = gss_init_sec_context(&minor_status, GSS_C_NO_CREDENTIAL, | |
278 | &http->gssctx, | |
279 | http->gssname, http->gssmech, | |
280 | GSS_C_DELEG_FLAG | GSS_C_MUTUAL_FLAG | | |
281 | GSS_C_INTEG_FLAG, | |
282 | GSS_C_INDEFINITE, | |
283 | GSS_C_NO_CHANNEL_BINDINGS, | |
284 | &input_token, &http->gssmech, | |
285 | &output_token, NULL, NULL); | |
286 | ||
287 | if (input_token.value) | |
288 | free(input_token.value); | |
289 | ||
290 | if (GSS_ERROR(major_status)) | |
291 | { | |
292 | # ifdef DEBUG | |
293 | DEBUG_gss_printf(major_status, minor_status, | |
294 | "Unable to initialize security context"); | |
295 | # endif /* DEBUG */ | |
296 | return (-1); | |
297 | } | |
298 | ||
299 | if (major_status == GSS_S_CONTINUE_NEEDED) | |
300 | DEBUG_gss_printf(major_status, minor_status, "Continuation needed!"); | |
301 | ||
302 | if (output_token.length > 0 && output_token.length <= 65536) | |
303 | { | |
304 | /* | |
305 | * Allocate the authorization string since Windows KDCs can have | |
306 | * arbitrarily large credentials... | |
307 | */ | |
308 | ||
309 | int authsize = 10 + /* "Negotiate " */ | |
310 | output_token.length * 4 / 3 + 1 + /* Base64 */ | |
311 | 1; /* nul */ | |
312 | ||
313 | httpSetAuthString(http, NULL, NULL); | |
314 | ||
315 | if ((http->authstring = malloc(authsize)) == NULL) | |
316 | { | |
317 | http->authstring = http->_authstring; | |
318 | authsize = sizeof(http->_authstring); | |
319 | } | |
320 | ||
321 | strcpy(http->authstring, "Negotiate "); | |
322 | httpEncode64_2(http->authstring + 10, authsize - 10, output_token.value, | |
323 | output_token.length); | |
324 | ||
325 | gss_release_buffer(&minor_status, &output_token); | |
326 | } | |
327 | else | |
328 | { | |
329 | DEBUG_printf(("cupsDoAuthentication: Kerberos credentials too large - " | |
330 | "%d bytes!\n", (int)output_token.length)); | |
331 | ||
332 | gss_release_buffer(&minor_status, &output_token); | |
333 | ||
334 | return (-1); | |
335 | } | |
336 | #endif /* HAVE_GSSAPI */ | |
337 | } | |
338 | else if (strncmp(http->fields[HTTP_FIELD_WWW_AUTHENTICATE], "Digest", 6)) | |
339 | { | |
340 | /* | |
341 | * Basic authentication... | |
342 | */ | |
343 | ||
344 | char encode[256]; /* Base64 buffer */ | |
345 | ||
346 | ||
347 | httpEncode64_2(encode, sizeof(encode), http->userpass, | |
348 | (int)strlen(http->userpass)); | |
349 | httpSetAuthString(http, "Basic", encode); | |
350 | } | |
351 | else | |
352 | { | |
353 | /* | |
354 | * Digest authentication... | |
355 | */ | |
356 | ||
357 | char encode[33], /* MD5 buffer */ | |
358 | digest[1024]; /* Digest auth data */ | |
359 | ||
360 | ||
361 | httpGetSubField(http, HTTP_FIELD_WWW_AUTHENTICATE, "realm", realm); | |
362 | httpGetSubField(http, HTTP_FIELD_WWW_AUTHENTICATE, "nonce", nonce); | |
363 | ||
364 | httpMD5(cupsUser(), realm, strchr(http->userpass, ':') + 1, encode); | |
365 | httpMD5Final(nonce, method, resource, encode); | |
366 | snprintf(digest, sizeof(digest), | |
367 | "username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", " | |
368 | "response=\"%s\"", cupsUser(), realm, nonce, resource, encode); | |
369 | httpSetAuthString(http, "Digest", digest); | |
370 | } | |
371 | ||
372 | DEBUG_printf(("cupsDoAuthentication: authstring=\"%s\"\n", http->authstring)); | |
373 | ||
374 | return (0); | |
375 | } | |
376 | ||
377 | ||
378 | #ifdef HAVE_GSSAPI | |
379 | # ifdef DEBUG | |
380 | /* | |
381 | * 'DEBUG_gss_printf()' - Show debug error messages from GSSAPI... | |
382 | */ | |
383 | ||
384 | static void | |
385 | DEBUG_gss_printf(OM_uint32 major_status,/* I - Major status code */ | |
386 | OM_uint32 minor_status,/* I - Minor status code */ | |
387 | const char *message) /* I - Prefix for error message */ | |
388 | { | |
389 | OM_uint32 err_major_status, /* Major status code for display */ | |
390 | err_minor_status; /* Minor status code for display */ | |
391 | OM_uint32 msg_ctx; /* Message context */ | |
392 | gss_buffer_desc major_status_string = GSS_C_EMPTY_BUFFER, | |
393 | /* Major status message */ | |
394 | minor_status_string = GSS_C_EMPTY_BUFFER; | |
395 | /* Minor status message */ | |
396 | ||
397 | ||
398 | msg_ctx = 0; | |
399 | err_major_status = gss_display_status(&err_minor_status, | |
400 | major_status, | |
401 | GSS_C_GSS_CODE, | |
402 | GSS_C_NO_OID, | |
403 | &msg_ctx, | |
404 | &major_status_string); | |
405 | ||
406 | if (!GSS_ERROR(err_major_status)) | |
407 | gss_display_status(&err_minor_status, minor_status, GSS_C_MECH_CODE, | |
408 | GSS_C_NULL_OID, &msg_ctx, &minor_status_string); | |
409 | ||
410 | printf("%s: %s, %s\n", message, (char *)major_status_string.value, | |
411 | (char *)minor_status_string.value); | |
412 | ||
413 | gss_release_buffer(&err_minor_status, &major_status_string); | |
414 | gss_release_buffer(&err_minor_status, &minor_status_string); | |
415 | } | |
416 | # endif /* DEBUG */ | |
417 | ||
418 | ||
419 | /* | |
420 | * 'cups_get_gss_creds()' - Get CUPS service credentials for authentication. | |
421 | */ | |
422 | ||
423 | static gss_name_t /* O - Server name */ | |
424 | cups_get_gss_creds( | |
425 | http_t *http, /* I - Connection to server */ | |
426 | const char *service_name) /* I - Service name */ | |
427 | { | |
428 | gss_buffer_desc token = GSS_C_EMPTY_BUFFER; | |
429 | /* Service token */ | |
430 | OM_uint32 major_status, /* Major status code */ | |
431 | minor_status; /* Minor status code */ | |
432 | gss_name_t server_name; /* Server name */ | |
433 | char buf[1024], /* Name buffer */ | |
434 | fqdn[HTTP_MAX_URI]; /* Server name buffer */ | |
435 | ||
436 | ||
437 | /* | |
438 | * Get the hostname... | |
439 | */ | |
440 | ||
441 | httpGetHostname(http, fqdn, sizeof(fqdn)); | |
442 | ||
443 | if (!strcmp(fqdn, "localhost")) | |
444 | httpGetHostname(NULL, fqdn, sizeof(fqdn)); | |
445 | ||
446 | /* | |
447 | * Get a server name we can use for authentication purposes... | |
448 | */ | |
449 | ||
450 | snprintf(buf, sizeof(buf), "%s@%s", service_name, fqdn); | |
451 | ||
452 | DEBUG_printf(("cups_get_gss_creds: Looking up %s...\n", buf)); | |
453 | ||
454 | token.value = buf; | |
455 | token.length = strlen(buf); | |
456 | server_name = GSS_C_NO_NAME; | |
457 | major_status = gss_import_name(&minor_status, &token, | |
458 | GSS_C_NT_HOSTBASED_SERVICE, | |
459 | &server_name); | |
460 | ||
461 | if (GSS_ERROR(major_status)) | |
462 | { | |
463 | # ifdef DEBUG | |
464 | DEBUG_gss_printf(major_status, minor_status, "gss_import_name() failed"); | |
465 | # endif /* DEBUG */ | |
466 | ||
467 | return (NULL); | |
468 | } | |
469 | ||
470 | return (server_name); | |
471 | } | |
472 | #endif /* HAVE_GSSAPI */ | |
473 | ||
474 | ||
475 | /* | |
476 | * 'cups_local_auth()' - Get the local authorization certificate if | |
477 | * available/applicable... | |
478 | */ | |
479 | ||
480 | static int /* O - 0 if available */ | |
481 | /* 1 if not available */ | |
482 | /* -1 error */ | |
483 | cups_local_auth(http_t *http) /* I - HTTP connection to server */ | |
484 | { | |
485 | #if defined(WIN32) || defined(__EMX__) | |
486 | /* | |
487 | * Currently WIN32 and OS-2 do not support the CUPS server... | |
488 | */ | |
489 | ||
490 | return (1); | |
491 | #else | |
492 | int pid; /* Current process ID */ | |
493 | FILE *fp; /* Certificate file */ | |
494 | char filename[1024], /* Certificate filename */ | |
495 | certificate[33];/* Certificate string */ | |
496 | _cups_globals_t *cg = _cupsGlobals(); /* Global data */ | |
497 | # if defined(HAVE_AUTHORIZATION_H) | |
498 | OSStatus status; /* Status */ | |
499 | AuthorizationItem auth_right; /* Authorization right */ | |
500 | AuthorizationRights auth_rights; /* Authorization rights */ | |
501 | AuthorizationFlags auth_flags; /* Authorization flags */ | |
502 | AuthorizationExternalForm auth_extrn; /* Authorization ref external */ | |
503 | char auth_key[1024]; /* Buffer */ | |
504 | char buffer[1024]; /* Buffer */ | |
505 | # endif /* HAVE_AUTHORIZATION_H */ | |
506 | ||
507 | ||
508 | DEBUG_printf(("cups_local_auth(http=%p) hostaddr=%s, hostname=\"%s\"\n", | |
509 | http, httpAddrString(http->hostaddr, filename, sizeof(filename)), http->hostname)); | |
510 | ||
511 | /* | |
512 | * See if we are accessing localhost... | |
513 | */ | |
514 | ||
515 | if (!httpAddrLocalhost(http->hostaddr) && | |
516 | strcasecmp(http->hostname, "localhost") != 0) | |
517 | { | |
518 | DEBUG_puts("cups_local_auth: Not a local connection!"); | |
519 | return (1); | |
520 | } | |
521 | ||
522 | # if defined(HAVE_AUTHORIZATION_H) | |
523 | /* | |
524 | * Delete any previous authorization reference... | |
525 | */ | |
526 | ||
527 | if (http->auth_ref) | |
528 | { | |
529 | AuthorizationFree(http->auth_ref, kAuthorizationFlagDefaults); | |
530 | http->auth_ref = NULL; | |
531 | } | |
532 | ||
533 | if (httpGetSubField2(http, HTTP_FIELD_WWW_AUTHENTICATE, "authkey", | |
534 | auth_key, sizeof(auth_key))) | |
535 | { | |
536 | status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, | |
537 | kAuthorizationFlagDefaults, &http->auth_ref); | |
538 | if (status != errAuthorizationSuccess) | |
539 | { | |
540 | DEBUG_printf(("cups_local_auth: AuthorizationCreate() returned %d (%s)\n", | |
541 | (int)status, cssmErrorString(status))); | |
542 | return (-1); | |
543 | } | |
544 | ||
545 | auth_right.name = auth_key; | |
546 | auth_right.valueLength = 0; | |
547 | auth_right.value = NULL; | |
548 | auth_right.flags = 0; | |
549 | ||
550 | auth_rights.count = 1; | |
551 | auth_rights.items = &auth_right; | |
552 | ||
553 | auth_flags = kAuthorizationFlagDefaults | | |
554 | kAuthorizationFlagPreAuthorize | | |
555 | kAuthorizationFlagInteractionAllowed | | |
556 | kAuthorizationFlagExtendRights; | |
557 | ||
558 | status = AuthorizationCopyRights(http->auth_ref, &auth_rights, | |
559 | kAuthorizationEmptyEnvironment, | |
560 | auth_flags, NULL); | |
561 | if (status == errAuthorizationSuccess) | |
562 | status = AuthorizationMakeExternalForm(http->auth_ref, &auth_extrn); | |
563 | ||
564 | if (status == errAuthorizationSuccess) | |
565 | { | |
566 | /* | |
567 | * Set the authorization string and return... | |
568 | */ | |
569 | ||
570 | httpEncode64_2(buffer, sizeof(buffer), (void *)&auth_extrn, | |
571 | sizeof(auth_extrn)); | |
572 | ||
573 | httpSetAuthString(http, "AuthRef", buffer); | |
574 | ||
575 | DEBUG_printf(("cups_local_auth: Returning authstring = \"%s\"\n", | |
576 | http->authstring)); | |
577 | return (0); | |
578 | } | |
579 | else if (status == errAuthorizationCanceled) | |
580 | return (-1); | |
581 | ||
582 | DEBUG_printf(("cups_local_auth: AuthorizationCopyRights() returned %d (%s)\n", | |
583 | (int)status, cssmErrorString(status))); | |
584 | ||
585 | /* | |
586 | * Fall through to try certificates... | |
587 | */ | |
588 | } | |
589 | # endif /* HAVE_AUTHORIZATION_H */ | |
590 | ||
591 | /* | |
592 | * Try opening a certificate file for this PID. If that fails, | |
593 | * try the root certificate... | |
594 | */ | |
595 | ||
596 | pid = getpid(); | |
597 | snprintf(filename, sizeof(filename), "%s/certs/%d", cg->cups_statedir, pid); | |
598 | if ((fp = fopen(filename, "r")) == NULL && pid > 0) | |
599 | { | |
600 | DEBUG_printf(("cups_local_auth: Unable to open file %s: %s\n", | |
601 | filename, strerror(errno))); | |
602 | ||
603 | #ifdef HAVE_GSSAPI | |
604 | /* | |
605 | * If local certificate authentication isn't available for this PID, | |
606 | * check if we need Kerberos authentication... | |
607 | */ | |
608 | ||
609 | if (!strncmp(http->fields[HTTP_FIELD_WWW_AUTHENTICATE], "Negotiate", 9)) | |
610 | { | |
611 | /* | |
612 | * Yes, don't try the root certificate... | |
613 | */ | |
614 | ||
615 | return (1); | |
616 | } | |
617 | #endif /* HAVE_GSSAPI */ | |
618 | ||
619 | snprintf(filename, sizeof(filename), "%s/certs/0", cg->cups_statedir); | |
620 | fp = fopen(filename, "r"); | |
621 | } | |
622 | ||
623 | if (fp) | |
624 | { | |
625 | /* | |
626 | * Read the certificate from the file... | |
627 | */ | |
628 | ||
629 | fgets(certificate, sizeof(certificate), fp); | |
630 | fclose(fp); | |
631 | ||
632 | /* | |
633 | * Set the authorization string and return... | |
634 | */ | |
635 | ||
636 | httpSetAuthString(http, "Local", certificate); | |
637 | ||
638 | DEBUG_printf(("cups_local_auth: Returning authstring = \"%s\"\n", | |
639 | http->authstring)); | |
640 | ||
641 | return (0); | |
642 | } | |
643 | ||
644 | # if defined(SO_PEERCRED) && defined(AF_LOCAL) | |
645 | /* | |
646 | * See if we can authenticate using the peer credentials provided over a | |
647 | * domain socket; if so, specify "PeerCred username" as the authentication | |
648 | * information... | |
649 | */ | |
650 | ||
651 | if (http->hostaddr->addr.sa_family == AF_LOCAL && | |
652 | !getenv("GATEWAY_INTERFACE")) /* Not via CGI programs... */ | |
653 | { | |
654 | /* | |
655 | * Verify that the current cupsUser() matches the current UID... | |
656 | */ | |
657 | ||
658 | struct passwd *pwd; /* Password information */ | |
659 | const char *username; /* Current username */ | |
660 | ||
661 | username = cupsUser(); | |
662 | ||
663 | if ((pwd = getpwnam(username)) != NULL && pwd->pw_uid == getuid()) | |
664 | { | |
665 | httpSetAuthString(http, "PeerCred", username); | |
666 | ||
667 | DEBUG_printf(("cups_local_auth: Returning authstring = \"%s\"\n", | |
668 | http->authstring)); | |
669 | ||
670 | return (0); | |
671 | } | |
672 | } | |
673 | # endif /* SO_PEERCRED && AF_LOCAL */ | |
674 | ||
675 | return (1); | |
676 | #endif /* WIN32 || __EMX__ */ | |
677 | } | |
678 | ||
679 | ||
680 | /* | |
681 | * End of "$Id: auth.c 7720 2008-07-11 22:46:21Z mike $". | |
682 | */ |