4 * Authentication functions for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2003 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-3111 USA
20 * Voice: (301) 373-9603
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...
47 #if defined(WIN32) || defined(__EMX__)
51 #endif /* WIN32 || __EMX__ */
58 static int cups_local_auth(http_t
*http
);
62 * 'cupsDoAuthentication()' - Authenticate a request...
65 int /* O - 0 on success, -1 on error */
66 cupsDoAuthentication(http_t
*http
, /* I - HTTP connection to server */
67 const char *method
,/* I - Request method (GET, POST, PUT) */
69 /* I - Resource path */
71 const char *password
; /* Password string */
72 char prompt
[1024], /* Prompt for user */
73 realm
[HTTP_MAX_VALUE
], /* realm="xyz" string */
74 nonce
[HTTP_MAX_VALUE
], /* nonce="xyz" string */
75 encode
[512]; /* Encoded username:password */
79 * Clear the current authentication string...
82 http
->authstring
[0] = '\0';
85 * See if we can do local authentication...
88 if (!cups_local_auth(http
))
92 * Nope, see if we should retry the current digest password...
95 if (strncmp(http
->fields
[HTTP_FIELD_WWW_AUTHENTICATE
], "Basic", 5) == 0 ||
96 http
->digest_tries
> 1 || !http
->userpass
[0])
99 * Nope - get a new password from the user...
102 snprintf(prompt
, sizeof(prompt
), "Password for %s on %s? ", cupsUser(),
105 http
->digest_tries
= 0;
106 http
->userpass
[0] = '\0';
108 if ((password
= cupsGetPassword(prompt
)) == NULL
)
114 snprintf(http
->userpass
, sizeof(http
->userpass
), "%s:%s", cupsUser(),
118 http
->digest_tries
++;
121 * Got a password; encode it for the server...
124 if (strncmp(http
->fields
[HTTP_FIELD_WWW_AUTHENTICATE
], "Basic", 5) == 0)
127 * Basic authentication...
130 httpEncode64(encode
, http
->userpass
);
131 snprintf(http
->authstring
, sizeof(http
->authstring
), "Basic %s", encode
);
136 * Digest authentication...
139 httpGetSubField(http
, HTTP_FIELD_WWW_AUTHENTICATE
, "realm", realm
);
140 httpGetSubField(http
, HTTP_FIELD_WWW_AUTHENTICATE
, "nonce", nonce
);
142 httpMD5(cupsUser(), realm
, strchr(http
->userpass
, ':') + 1, encode
);
143 httpMD5Final(nonce
, "POST", resource
, encode
);
144 snprintf(http
->authstring
, sizeof(http
->authstring
),
145 "Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", "
146 "response=\"%s\"", cupsUser(), realm
, nonce
, encode
);
154 * 'cups_local_auth()' - Get the local authorization certificate if
155 * available/applicable...
158 static int /* O - 0 if available, -1 if not */
159 cups_local_auth(http_t
*http
) /* I - HTTP connection to server */
161 #if defined(WIN32) || defined(__EMX__)
163 * Currently WIN32 and OS-2 do not support the CUPS server...
168 int pid
; /* Current process ID */
169 FILE *fp
; /* Certificate file */
170 char filename
[1024], /* Certificate filename */
171 certificate
[33]; /* Certificate string */
172 const char *root
; /* Server root directory */
175 DEBUG_printf(("cups_local_auth(http=%p) hostaddr=%08x, hostname=\"%s\"\n",
176 http
, ntohl(http
->hostaddr
.sin_addr
.s_addr
), http
->hostname
));
179 * See if we are accessing localhost...
182 if (ntohl(http
->hostaddr
.sin_addr
.s_addr
) != 0x7f000001 &&
183 strcasecmp(http
->hostname
, "localhost") != 0)
185 DEBUG_puts("cups_local_auth: Not a local connection!");
190 * Try opening a certificate file for this PID. If that fails,
191 * try the root certificate...
194 if ((root
= getenv("CUPS_SERVERROOT")) == NULL
)
195 root
= CUPS_SERVERROOT
;
198 snprintf(filename
, sizeof(filename
), "%s/certs/%d", root
, pid
);
199 if ((fp
= fopen(filename
, "r")) == NULL
&& pid
> 0)
201 DEBUG_printf(("cups_local_auth: Unable to open file %s: %s\n",
202 filename
, strerror(errno
)));
204 snprintf(filename
, sizeof(filename
), "%s/certs/0", root
);
205 fp
= fopen(filename
, "r");
210 DEBUG_printf(("cups_local_auth: Unable to open file %s: %s\n",
211 filename
, strerror(errno
)));
216 * Read the certificate from the file...
219 fgets(certificate
, sizeof(certificate
), fp
);
223 * Set the authorization string and return...
226 snprintf(http
->authstring
, sizeof(http
->authstring
), "Local %s", certificate
);
228 DEBUG_printf(("cups_local_auth: Returning authstring = \"%s\"\n",
232 #endif /* WIN32 || __EMX__ */