4 * Authentication functions for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2007 by Easy Software Products.
8 * This file contains Kerberos support code, copyright 2006 by
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
18 * Attn: CUPS Licensing Information
19 * Easy Software Products
20 * 44141 Airport View Drive, Suite 204
21 * Hollywood, Maryland 20636 USA
23 * Voice: (301) 373-9600
24 * EMail: cups-info@cups.org
25 * WWW: http://www.cups.org
27 * This file is subject to the Apple OS-Developed Software exception.
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...
39 * Include necessary headers...
49 #if defined(WIN32) || defined(__EMX__)
53 #endif /* WIN32 || __EMX__ */
62 static void DEBUG_gss_printf(OM_uint32 major_status
, OM_uint32 minor_status
,
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
);
71 * 'cupsDoAuthentication()' - Authenticate a request.
73 * This function should be called in response to a HTTP_UNAUTHORIZED
74 * status, prior to resubmitting your request.
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) */
83 /* I - Resource path */
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 */
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
)));
101 * Clear the current authentication string...
104 http
->_authstring
[0] = '\0';
106 if (http
->authstring
&& http
->authstring
!= http
->_authstring
)
107 free(http
->authstring
);
109 http
->authstring
= http
->_authstring
;
112 * See if we can do local authentication...
115 if (http
->digest_tries
< 3 && !cups_local_auth(http
))
117 DEBUG_printf(("cupsDoAuthentication: authstring=\"%s\"\n", http
->authstring
));
119 if (http
->status
== HTTP_UNAUTHORIZED
)
120 http
->digest_tries
++;
126 * Nope, see if we should retry the current username:password...
129 if ((http
->digest_tries
> 1 || !http
->userpass
[0]) &&
130 strncmp(http
->fields
[HTTP_FIELD_WWW_AUTHENTICATE
], "Negotiate", 9))
133 * Nope - get a new password from the user...
138 if (!cg
->lang_default
)
139 cg
->lang_default
= cupsLangDefault();
141 snprintf(prompt
, sizeof(prompt
),
142 _cupsLangString(cg
->lang_default
, _("Password for %s on %s? ")),
144 http
->hostname
[0] == '/' ? "localhost" : http
->hostname
);
146 http
->digest_tries
= strncasecmp(http
->fields
[HTTP_FIELD_WWW_AUTHENTICATE
],
148 http
->userpass
[0] = '\0';
150 if ((password
= cupsGetPassword(prompt
)) == NULL
)
156 snprintf(http
->userpass
, sizeof(http
->userpass
), "%s:%s", cupsUser(),
159 else if (http
->status
== HTTP_UNAUTHORIZED
)
160 http
->digest_tries
++;
163 * Got a password; encode it for the server...
166 if (!strncmp(http
->fields
[HTTP_FIELD_WWW_AUTHENTICATE
], "Negotiate", 9))
170 * Kerberos authentication...
173 OM_uint32 minor_status
, /* Minor status code */
174 major_status
; /* Major status code */
175 gss_buffer_desc output_token
= GSS_C_EMPTY_BUFFER
,
177 input_token
= GSS_C_EMPTY_BUFFER
;
179 char *gss_service_name
;
180 /* GSS service name */
181 const char *authorization
;
182 /* Pointer into Authorization string */
185 if (http
->gssname
== GSS_C_NO_NAME
)
187 if ((gss_service_name
= getenv("CUPS_GSSSERVICENAME")) == NULL
)
188 gss_service_name
= CUPS_DEFAULT_GSSSERVICENAME
;
190 DEBUG_puts("cupsDoAuthentication: GSS service name set via environment");
192 http
->gssname
= cups_get_gss_creds(http
, gss_service_name
);
196 * Find the start of the Kerberos input token...
199 authorization
= httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
);
202 while (*authorization
&& isspace(*authorization
& 255))
208 * For SPNEGO, this is where we'll feed the server's authorization data
209 * back into gss via input_token...
214 if (http
->gssctx
!= GSS_C_NO_CONTEXT
)
216 major_status
= gss_delete_sec_context(&minor_status
, &http
->gssctx
,
218 http
->gssctx
= GSS_C_NO_CONTEXT
;
222 major_status
= gss_init_sec_context(&minor_status
, GSS_C_NO_CREDENTIAL
,
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
);
230 if (input_token
.value
)
231 free(input_token
.value
);
233 if (GSS_ERROR(major_status
))
236 DEBUG_gss_printf(major_status
, minor_status
,
237 "Unable to initialise security context");
243 if (major_status
== GSS_S_CONTINUE_NEEDED
)
244 DEBUG_gss_printf(major_status
, minor_status
, "Continuation needed!");
247 if (output_token
.length
)
249 httpEncode64_2(encode
, sizeof(encode
), output_token
.value
,
250 output_token
.length
);
252 http
->authstring
= malloc(strlen(encode
) + 11);
253 sprintf(http
->authstring
, "Negotiate %s", encode
); /* Safe because allocated */
255 major_status
= gss_release_buffer(&minor_status
, &output_token
);
259 * Copy back what we can to _authstring for backwards compatibility...
262 strlcpy(http
->_authstring
, http
->authstring
, sizeof(http
->_authstring
));
263 #endif /* HAVE_GSSAPI */
265 else if (strncmp(http
->fields
[HTTP_FIELD_WWW_AUTHENTICATE
], "Digest", 6))
268 * Basic authentication...
271 httpEncode64_2(encode
, sizeof(encode
), http
->userpass
,
272 (int)strlen(http
->userpass
));
273 snprintf(http
->_authstring
, sizeof(http
->_authstring
), "Basic %s", encode
);
278 * Digest authentication...
281 httpGetSubField(http
, HTTP_FIELD_WWW_AUTHENTICATE
, "realm", realm
);
282 httpGetSubField(http
, HTTP_FIELD_WWW_AUTHENTICATE
, "nonce", nonce
);
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
,
292 DEBUG_printf(("cupsDoAuthentication: authstring=\"%s\"\n", http
->authstring
));
301 * 'DEBUG_gss_printf()' - Show debug error messages from GSSAPI...
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 */
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 */
319 err_major_status
= gss_display_status(&err_minor_status
,
324 &major_status_string
);
326 if (!GSS_ERROR(err_major_status
))
327 err_major_status
= gss_display_status(&err_minor_status
,
332 &minor_status_string
);
334 printf("%s: %s, %s\n", message
, (char *)major_status_string
.value
,
335 (char *)minor_status_string
.value
);
337 gss_release_buffer(&err_minor_status
, &major_status_string
);
338 gss_release_buffer(&err_minor_status
, &minor_status_string
);
344 * 'cups_get_gss_creds()' - Get CUPS service credentials for authentication.
347 static gss_name_t
/* O - Server name */
349 http_t
*http
, /* I - Connection to server */
350 const char *service_name
) /* I - Service name */
352 gss_buffer_desc token
= GSS_C_EMPTY_BUFFER
;
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 */
362 * Get a server name we can use for authentication purposes...
365 snprintf(buf
, sizeof(buf
), "%s@%s", service_name
,
366 httpGetHostname(http
, fqdn
, sizeof(fqdn
)));
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
,
375 if (GSS_ERROR(major_status
))
378 DEBUG_gss_printf(major_status
, minor_status
, "gss_import_name() failed");
384 return (server_name
);
386 #endif /* HAVE_GSSAPI */
390 * 'cups_local_auth()' - Get the local authorization certificate if
391 * available/applicable...
394 static int /* O - 0 if available, -1 if not */
395 cups_local_auth(http_t
*http
) /* I - HTTP connection to server */
397 #if defined(WIN32) || defined(__EMX__)
399 * Currently WIN32 and OS-2 do not support the CUPS server...
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 */
411 DEBUG_printf(("cups_local_auth(http=%p) hostaddr=%s, hostname=\"%s\"\n",
412 http
, httpAddrString(http
->hostaddr
, filename
, sizeof(filename
)), http
->hostname
));
415 * See if we are accessing localhost...
418 if (!httpAddrLocalhost(http
->hostaddr
) &&
419 strcasecmp(http
->hostname
, "localhost") != 0)
421 DEBUG_puts("cups_local_auth: Not a local connection!");
426 * Try opening a certificate file for this PID. If that fails,
427 * try the root certificate...
431 snprintf(filename
, sizeof(filename
), "%s/certs/%d", cg
->cups_statedir
, pid
);
432 if ((fp
= fopen(filename
, "r")) == NULL
&& pid
> 0)
434 DEBUG_printf(("cups_local_auth: Unable to open file %s: %s\n",
435 filename
, strerror(errno
)));
437 snprintf(filename
, sizeof(filename
), "%s/certs/0", cg
->cups_statedir
);
438 fp
= fopen(filename
, "r");
443 DEBUG_printf(("cups_local_auth: Unable to open file %s: %s\n",
444 filename
, strerror(errno
)));
449 * Read the certificate from the file...
452 fgets(certificate
, sizeof(certificate
), fp
);
456 * Set the authorization string and return...
459 http
->authstring
= malloc(strlen(certificate
) + 10);
460 sprintf(http
->authstring
, "Local %s", certificate
);
462 /* Copy back to _authstring for backwards compatibility */
463 strlcpy(http
->_authstring
, http
->authstring
, sizeof(http
->_authstring
));
465 DEBUG_printf(("cups_local_auth: Returning authstring = \"%s\"\n",
469 #endif /* WIN32 || __EMX__ */