]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/auth.c
More VC++.NET fun...
[thirdparty/cups.git] / cups / auth.c
1 /*
2 * "$Id$"
3 *
4 * Authentication functions for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2007 by Easy Software Products.
7 *
8 * This file contains Kerberos support code, copyright 2006 by
9 * Jelmer Vernooij.
10 *
11 * These coded instructions, statements, and computer programs are the
12 * property of Easy Software Products and are protected by Federal
13 * copyright law. Distribution and use rights are outlined in the file
14 * "LICENSE.txt" which should have been included with this file. If this
15 * file is missing or damaged please contact Easy Software Products
16 * at:
17 *
18 * Attn: CUPS Licensing Information
19 * Easy Software Products
20 * 44141 Airport View Drive, Suite 204
21 * Hollywood, Maryland 20636 USA
22 *
23 * Voice: (301) 373-9600
24 * EMail: cups-info@cups.org
25 * WWW: http://www.cups.org
26 *
27 * This file is subject to the Apple OS-Developed Software exception.
28 *
29 * Contents:
30 *
31 * cupsDoAuthentication() - Authenticate a request.
32 * DEBUG_gss_printf() - Show debug error messages from GSSAPI...
33 * cups_get_gss_creds() - Get CUPS service credentials for authentication.
34 * cups_local_auth() - Get the local authorization certificate if
35 * available/applicable...
36 */
37
38 /*
39 * Include necessary headers...
40 */
41
42 #include "globals.h"
43 #include "debug.h"
44 #include <stdlib.h>
45 #include <ctype.h>
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <sys/stat.h>
49 #if defined(WIN32) || defined(__EMX__)
50 # include <io.h>
51 #else
52 # include <unistd.h>
53 #endif /* WIN32 || __EMX__ */
54
55
56 /*
57 * Local functions...
58 */
59
60 #ifdef HAVE_GSSAPI
61 # ifdef DEBUG
62 static void DEBUG_gss_printf(OM_uint32 major_status, OM_uint32 minor_status,
63 const char *message);
64 # endif /* DEBUG */
65 static gss_name_t cups_get_gss_creds(http_t *http, const char *service_name);
66 #endif /* HAVE_GSSAPI */
67 static int cups_local_auth(http_t *http);
68
69
70 /*
71 * 'cupsDoAuthentication()' - Authenticate a request.
72 *
73 * This function should be called in response to a HTTP_UNAUTHORIZED
74 * status, prior to resubmitting your request.
75 *
76 * @since CUPS 1.1.20@
77 */
78
79 int /* O - 0 on success, -1 on error */
80 cupsDoAuthentication(http_t *http, /* I - HTTP connection to server */
81 const char *method,/* I - Request method (GET, POST, PUT) */
82 const char *resource)
83 /* I - Resource path */
84 {
85 const char *password; /* Password string */
86 char prompt[1024], /* Prompt for user */
87 realm[HTTP_MAX_VALUE], /* realm="xyz" string */
88 nonce[HTTP_MAX_VALUE], /* nonce="xyz" string */
89 encode[2048]; /* Encoded username:password */
90 _cups_globals_t *cg; /* Global data */
91
92
93 DEBUG_printf(("cupsDoAuthentication(http=%p, method=\"%s\", resource=\"%s\")\n",
94 http, method, resource));
95 DEBUG_printf(("cupsDoAuthentication: digest_tries=%d, userpass=\"%s\"\n",
96 http->digest_tries, http->userpass));
97 DEBUG_printf(("cupsDoAuthentication: WWW-Authenticate=\"%s\"\n",
98 httpGetField(http, HTTP_FIELD_WWW_AUTHENTICATE)));
99
100 /*
101 * Clear the current authentication string...
102 */
103
104 http->_authstring[0] = '\0';
105
106 if (http->authstring && http->authstring != http->_authstring)
107 free(http->authstring);
108
109 http->authstring = http->_authstring;
110
111 /*
112 * See if we can do local authentication...
113 */
114
115 if (http->digest_tries < 3 && !cups_local_auth(http))
116 {
117 DEBUG_printf(("cupsDoAuthentication: authstring=\"%s\"\n", http->authstring));
118
119 if (http->status == HTTP_UNAUTHORIZED)
120 http->digest_tries ++;
121
122 return (0);
123 }
124
125 /*
126 * Nope, see if we should retry the current username:password...
127 */
128
129 if ((http->digest_tries > 1 || !http->userpass[0]) &&
130 strncmp(http->fields[HTTP_FIELD_WWW_AUTHENTICATE], "Negotiate", 9))
131 {
132 /*
133 * Nope - get a new password from the user...
134 */
135
136 cg = _cupsGlobals();
137
138 if (!cg->lang_default)
139 cg->lang_default = cupsLangDefault();
140
141 snprintf(prompt, sizeof(prompt),
142 _cupsLangString(cg->lang_default, _("Password for %s on %s? ")),
143 cupsUser(),
144 http->hostname[0] == '/' ? "localhost" : http->hostname);
145
146 http->digest_tries = strncasecmp(http->fields[HTTP_FIELD_WWW_AUTHENTICATE],
147 "Digest", 5) != 0;
148 http->userpass[0] = '\0';
149
150 if ((password = cupsGetPassword(prompt)) == NULL)
151 return (-1);
152
153 if (!password[0])
154 return (-1);
155
156 snprintf(http->userpass, sizeof(http->userpass), "%s:%s", cupsUser(),
157 password);
158 }
159 else if (http->status == HTTP_UNAUTHORIZED)
160 http->digest_tries ++;
161
162 /*
163 * Got a password; encode it for the server...
164 */
165
166 if (!strncmp(http->fields[HTTP_FIELD_WWW_AUTHENTICATE], "Negotiate", 9))
167 {
168 #ifdef HAVE_GSSAPI
169 /*
170 * Kerberos authentication...
171 */
172
173 OM_uint32 minor_status, /* Minor status code */
174 major_status; /* Major status code */
175 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER,
176 /* Output token */
177 input_token = GSS_C_EMPTY_BUFFER;
178 /* Input token */
179 char *gss_service_name;
180 /* GSS service name */
181 const char *authorization;
182 /* Pointer into Authorization string */
183
184
185 if (http->gssname == GSS_C_NO_NAME)
186 {
187 if ((gss_service_name = getenv("CUPS_GSSSERVICENAME")) == NULL)
188 gss_service_name = CUPS_DEFAULT_GSSSERVICENAME;
189 else
190 DEBUG_puts("cupsDoAuthentication: GSS service name set via environment");
191
192 http->gssname = cups_get_gss_creds(http, gss_service_name);
193 }
194
195 /*
196 * Find the start of the Kerberos input token...
197 */
198
199 authorization = httpGetField(http, HTTP_FIELD_WWW_AUTHENTICATE);
200
201 authorization += 9;
202 while (*authorization && isspace(*authorization & 255))
203 authorization ++;
204
205 if (*authorization)
206 {
207 /*
208 * For SPNEGO, this is where we'll feed the server's authorization data
209 * back into gss via input_token...
210 */
211 }
212 else
213 {
214 if (http->gssctx != GSS_C_NO_CONTEXT)
215 {
216 major_status = gss_delete_sec_context(&minor_status, &http->gssctx,
217 GSS_C_NO_BUFFER);
218 http->gssctx = GSS_C_NO_CONTEXT;
219 }
220 }
221
222 major_status = gss_init_sec_context(&minor_status, GSS_C_NO_CREDENTIAL,
223 &http->gssctx,
224 http->gssname, http->gssmech,
225 GSS_C_MUTUAL_FLAG, GSS_C_INDEFINITE,
226 GSS_C_NO_CHANNEL_BINDINGS,
227 &input_token, &http->gssmech,
228 &output_token, NULL, NULL);
229
230 if (input_token.value)
231 free(input_token.value);
232
233 if (GSS_ERROR(major_status))
234 {
235 # ifdef DEBUG
236 DEBUG_gss_printf(major_status, minor_status,
237 "Unable to initialise security context");
238 # endif /* DEBUG */
239 return (-1);
240 }
241
242 # ifdef DEBUG
243 if (major_status == GSS_S_CONTINUE_NEEDED)
244 DEBUG_gss_printf(major_status, minor_status, "Continuation needed!");
245 # endif /* DEBUG */
246
247 if (output_token.length)
248 {
249 httpEncode64_2(encode, sizeof(encode), output_token.value,
250 output_token.length);
251
252 http->authstring = malloc(strlen(encode) + 11);
253 sprintf(http->authstring, "Negotiate %s", encode); /* Safe because allocated */
254
255 major_status = gss_release_buffer(&minor_status, &output_token);
256 }
257
258 /*
259 * Copy back what we can to _authstring for backwards compatibility...
260 */
261
262 strlcpy(http->_authstring, http->authstring, sizeof(http->_authstring));
263 #endif /* HAVE_GSSAPI */
264 }
265 else if (strncmp(http->fields[HTTP_FIELD_WWW_AUTHENTICATE], "Digest", 6))
266 {
267 /*
268 * Basic authentication...
269 */
270
271 httpEncode64_2(encode, sizeof(encode), http->userpass,
272 (int)strlen(http->userpass));
273 snprintf(http->_authstring, sizeof(http->_authstring), "Basic %s", encode);
274 }
275 else
276 {
277 /*
278 * Digest authentication...
279 */
280
281 httpGetSubField(http, HTTP_FIELD_WWW_AUTHENTICATE, "realm", realm);
282 httpGetSubField(http, HTTP_FIELD_WWW_AUTHENTICATE, "nonce", nonce);
283
284 httpMD5(cupsUser(), realm, strchr(http->userpass, ':') + 1, encode);
285 httpMD5Final(nonce, method, resource, encode);
286 snprintf(http->_authstring, sizeof(http->_authstring),
287 "Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", "
288 "uri=\"%s\", response=\"%s\"", cupsUser(), realm, nonce,
289 resource, encode);
290 }
291
292 DEBUG_printf(("cupsDoAuthentication: authstring=\"%s\"\n", http->authstring));
293
294 return (0);
295 }
296
297
298 #ifdef HAVE_GSSAPI
299 # ifdef DEBUG
300 /*
301 * 'DEBUG_gss_printf()' - Show debug error messages from GSSAPI...
302 */
303
304 static void
305 DEBUG_gss_printf(OM_uint32 major_status,/* I - Major status code */
306 OM_uint32 minor_status,/* I - Minor status code */
307 const char *message) /* I - Prefix for error message */
308 {
309 OM_uint32 err_major_status, /* Major status code for display */
310 err_minor_status; /* Minor status code for display */
311 OM_uint32 msg_ctx; /* Message context */
312 gss_buffer_desc major_status_string = GSS_C_EMPTY_BUFFER,
313 /* Major status message */
314 minor_status_string = GSS_C_EMPTY_BUFFER;
315 /* Minor status message */
316
317
318 msg_ctx = 0;
319 err_major_status = gss_display_status(&err_minor_status,
320 major_status,
321 GSS_C_GSS_CODE,
322 GSS_C_NO_OID,
323 &msg_ctx,
324 &major_status_string);
325
326 if (!GSS_ERROR(err_major_status))
327 err_major_status = gss_display_status(&err_minor_status,
328 minor_status,
329 GSS_C_MECH_CODE,
330 GSS_C_NULL_OID,
331 &msg_ctx,
332 &minor_status_string);
333
334 printf("%s: %s, %s\n", message, (char *)major_status_string.value,
335 (char *)minor_status_string.value);
336
337 gss_release_buffer(&err_minor_status, &major_status_string);
338 gss_release_buffer(&err_minor_status, &minor_status_string);
339 }
340 # endif /* DEBUG */
341
342
343 /*
344 * 'cups_get_gss_creds()' - Get CUPS service credentials for authentication.
345 */
346
347 static gss_name_t /* O - Server name */
348 cups_get_gss_creds(
349 http_t *http, /* I - Connection to server */
350 const char *service_name) /* I - Service name */
351 {
352 gss_buffer_desc token = GSS_C_EMPTY_BUFFER;
353 /* Service token */
354 OM_uint32 major_status, /* Major status code */
355 minor_status; /* Minor status code */
356 gss_name_t server_name; /* Server name */
357 char buf[1024], /* Name buffer */
358 fqdn[HTTP_MAX_URI]; /* Server name buffer */
359
360
361 /*
362 * Get a server name we can use for authentication purposes...
363 */
364
365 snprintf(buf, sizeof(buf), "%s@%s", service_name,
366 httpGetHostname(http, fqdn, sizeof(fqdn)));
367
368 token.value = buf;
369 token.length = strlen(buf);
370 server_name = GSS_C_NO_NAME;
371 major_status = gss_import_name(&minor_status, &token,
372 GSS_C_NT_HOSTBASED_SERVICE,
373 &server_name);
374
375 if (GSS_ERROR(major_status))
376 {
377 # ifdef DEBUG
378 DEBUG_gss_printf(major_status, minor_status, "gss_import_name() failed");
379 # endif /* DEBUG */
380
381 return (NULL);
382 }
383
384 return (server_name);
385 }
386 #endif /* HAVE_GSSAPI */
387
388
389 /*
390 * 'cups_local_auth()' - Get the local authorization certificate if
391 * available/applicable...
392 */
393
394 static int /* O - 0 if available, -1 if not */
395 cups_local_auth(http_t *http) /* I - HTTP connection to server */
396 {
397 #if defined(WIN32) || defined(__EMX__)
398 /*
399 * Currently WIN32 and OS-2 do not support the CUPS server...
400 */
401
402 return (-1);
403 #else
404 int pid; /* Current process ID */
405 FILE *fp; /* Certificate file */
406 char filename[1024], /* Certificate filename */
407 certificate[33]; /* Certificate string */
408 _cups_globals_t *cg = _cupsGlobals(); /* Global data */
409
410
411 DEBUG_printf(("cups_local_auth(http=%p) hostaddr=%s, hostname=\"%s\"\n",
412 http, httpAddrString(http->hostaddr, filename, sizeof(filename)), http->hostname));
413
414 /*
415 * See if we are accessing localhost...
416 */
417
418 if (!httpAddrLocalhost(http->hostaddr) &&
419 strcasecmp(http->hostname, "localhost") != 0)
420 {
421 DEBUG_puts("cups_local_auth: Not a local connection!");
422 return (-1);
423 }
424
425 /*
426 * Try opening a certificate file for this PID. If that fails,
427 * try the root certificate...
428 */
429
430 pid = getpid();
431 snprintf(filename, sizeof(filename), "%s/certs/%d", cg->cups_statedir, pid);
432 if ((fp = fopen(filename, "r")) == NULL && pid > 0)
433 {
434 DEBUG_printf(("cups_local_auth: Unable to open file %s: %s\n",
435 filename, strerror(errno)));
436
437 snprintf(filename, sizeof(filename), "%s/certs/0", cg->cups_statedir);
438 fp = fopen(filename, "r");
439 }
440
441 if (fp == NULL)
442 {
443 DEBUG_printf(("cups_local_auth: Unable to open file %s: %s\n",
444 filename, strerror(errno)));
445 return (-1);
446 }
447
448 /*
449 * Read the certificate from the file...
450 */
451
452 fgets(certificate, sizeof(certificate), fp);
453 fclose(fp);
454
455 /*
456 * Set the authorization string and return...
457 */
458
459 http->authstring = malloc(strlen(certificate) + 10);
460 sprintf(http->authstring, "Local %s", certificate);
461
462 /* Copy back to _authstring for backwards compatibility */
463 strlcpy(http->_authstring, http->authstring, sizeof(http->_authstring));
464
465 DEBUG_printf(("cups_local_auth: Returning authstring = \"%s\"\n",
466 http->authstring));
467
468 return (0);
469 #endif /* WIN32 || __EMX__ */
470 }
471
472
473 /*
474 * End of "$Id$".
475 */