2 * "$Id: auth.c 5559 2006-05-21 13:59:42Z mike $"
4 * Authentication functions for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 by Easy Software Products.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
24 * This file is subject to the Apple OS-Developed Software exception.
28 * cupsDoAuthentication() - Authenticate a request.
29 * cups_local_auth() - Get the local authorization certificate if
30 * available/applicable...
34 * Include necessary headers...
44 #if defined(WIN32) || defined(__EMX__)
48 #endif /* WIN32 || __EMX__ */
55 static int cups_local_auth(http_t
*http
);
59 * 'cupsDoAuthentication()' - Authenticate a request.
61 * This function should be called in response to a HTTP_UNAUTHORIZED
62 * status, prior to resubmitting your request.
67 int /* O - 0 on success, -1 on error */
68 cupsDoAuthentication(http_t
*http
, /* I - HTTP connection to server */
69 const char *method
,/* I - Request method (GET, POST, PUT) */
71 /* I - Resource path */
73 const char *password
; /* Password string */
74 char prompt
[1024], /* Prompt for user */
75 realm
[HTTP_MAX_VALUE
], /* realm="xyz" string */
76 nonce
[HTTP_MAX_VALUE
], /* nonce="xyz" string */
77 encode
[512]; /* Encoded username:password */
80 DEBUG_printf(("cupsDoAuthentication(http=%p, method=\"%s\", resource=\"%s\")\n",
81 http
, method
, resource
));
82 DEBUG_printf(("cupsDoAuthentication: digest_tries=%d, userpass=\"%s\"\n",
83 http
->digest_tries
, http
->userpass
));
86 * Clear the current authentication string...
89 http
->authstring
[0] = '\0';
92 * See if we can do local authentication...
95 if (http
->digest_tries
< 3 && !cups_local_auth(http
))
97 DEBUG_printf(("cupsDoAuthentication: authstring=\"%s\"\n", http
->authstring
));
99 if (http
->status
== HTTP_UNAUTHORIZED
)
100 http
->digest_tries
++;
106 * Nope, see if we should retry the current username:password...
109 if (http
->digest_tries
> 1 || !http
->userpass
[0])
112 * Nope - get a new password from the user...
115 snprintf(prompt
, sizeof(prompt
), _("Password for %s on %s? "), cupsUser(),
116 http
->hostname
[0] == '/' ? "localhost" : http
->hostname
);
118 http
->digest_tries
= strncasecmp(http
->fields
[HTTP_FIELD_WWW_AUTHENTICATE
],
120 http
->userpass
[0] = '\0';
122 if ((password
= cupsGetPassword(prompt
)) == NULL
)
128 snprintf(http
->userpass
, sizeof(http
->userpass
), "%s:%s", cupsUser(),
131 else if (http
->status
== HTTP_UNAUTHORIZED
)
132 http
->digest_tries
++;
135 * Got a password; encode it for the server...
138 if (strncmp(http
->fields
[HTTP_FIELD_WWW_AUTHENTICATE
], "Digest", 6))
141 * Basic authentication...
144 httpEncode64_2(encode
, sizeof(encode
), http
->userpass
,
145 strlen(http
->userpass
));
146 snprintf(http
->authstring
, sizeof(http
->authstring
), "Basic %s", encode
);
151 * Digest authentication...
154 httpGetSubField(http
, HTTP_FIELD_WWW_AUTHENTICATE
, "realm", realm
);
155 httpGetSubField(http
, HTTP_FIELD_WWW_AUTHENTICATE
, "nonce", nonce
);
157 httpMD5(cupsUser(), realm
, strchr(http
->userpass
, ':') + 1, encode
);
158 httpMD5Final(nonce
, method
, resource
, encode
);
159 snprintf(http
->authstring
, sizeof(http
->authstring
),
160 "Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", "
161 "uri=\"%s\", response=\"%s\"", cupsUser(), realm
, nonce
,
165 DEBUG_printf(("cupsDoAuthentication: authstring=\"%s\"\n", http
->authstring
));
172 * 'cups_local_auth()' - Get the local authorization certificate if
173 * available/applicable...
176 static int /* O - 0 if available, -1 if not */
177 cups_local_auth(http_t
*http
) /* I - HTTP connection to server */
179 #if defined(WIN32) || defined(__EMX__)
181 * Currently WIN32 and OS-2 do not support the CUPS server...
186 int pid
; /* Current process ID */
187 FILE *fp
; /* Certificate file */
188 char filename
[1024], /* Certificate filename */
189 certificate
[33]; /* Certificate string */
190 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
193 DEBUG_printf(("cups_local_auth(http=%p) hostaddr=%s, hostname=\"%s\"\n",
194 http
, httpAddrString(http
->hostaddr
, filename
, sizeof(filename
)), http
->hostname
));
197 * See if we are accessing localhost...
200 if (!httpAddrLocalhost(http
->hostaddr
) &&
201 strcasecmp(http
->hostname
, "localhost") != 0)
203 DEBUG_puts("cups_local_auth: Not a local connection!");
208 * Try opening a certificate file for this PID. If that fails,
209 * try the root certificate...
213 snprintf(filename
, sizeof(filename
), "%s/certs/%d", cg
->cups_statedir
, pid
);
214 if ((fp
= fopen(filename
, "r")) == NULL
&& pid
> 0)
216 DEBUG_printf(("cups_local_auth: Unable to open file %s: %s\n",
217 filename
, strerror(errno
)));
219 snprintf(filename
, sizeof(filename
), "%s/certs/0", cg
->cups_statedir
);
220 fp
= fopen(filename
, "r");
225 DEBUG_printf(("cups_local_auth: Unable to open file %s: %s\n",
226 filename
, strerror(errno
)));
231 * Read the certificate from the file...
234 fgets(certificate
, sizeof(certificate
), fp
);
238 * Set the authorization string and return...
241 snprintf(http
->authstring
, sizeof(http
->authstring
), "Local %s", certificate
);
243 DEBUG_printf(("cups_local_auth: Returning authstring = \"%s\"\n",
247 #endif /* WIN32 || __EMX__ */
252 * End of "$Id: auth.c 5559 2006-05-21 13:59:42Z mike $".