]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
e07d4801 | 2 | * "$Id: usersys.c 8498 2009-04-13 17:03:15Z mike $" |
ef416fc2 | 3 | * |
5a6b583a | 4 | * User, system, and password routines for CUPS. |
ef416fc2 | 5 | * |
07ed0e9a | 6 | * Copyright 2007-2011 by Apple Inc. |
ef416fc2 | 7 | * Copyright 1997-2006 by Easy Software Products. |
8 | * | |
9 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 10 | * property of Apple Inc. and are protected by Federal copyright |
11 | * law. Distribution and use rights are outlined in the file "LICENSE.txt" | |
12 | * which should have been included with this file. If this file is | |
13 | * file is missing or damaged, see the license at "http://www.cups.org/". | |
ef416fc2 | 14 | * |
15 | * This file is subject to the Apple OS-Developed Software exception. | |
16 | * | |
17 | * Contents: | |
18 | * | |
5a6b583a | 19 | * cupsEncryption() - Get the current encryption settings. |
b423cd4c | 20 | * cupsGetPassword() - Get a password from the user. |
5a6b583a MS |
21 | * cupsGetPassword2() - Get a password from the user using the advanced |
22 | * password callback. | |
23 | * cupsServer() - Return the hostname/address of the current | |
e07d4801 | 24 | * server. |
7cf5915e | 25 | * cupsSetClientCertCB() - Set the client certificate callback. |
b423cd4c | 26 | * cupsSetEncryption() - Set the encryption preference. |
27 | * cupsSetPasswordCB() - Set the password callback for CUPS. | |
f11a948a | 28 | * cupsSetPasswordCB2() - Set the advanced password callback for CUPS. |
5a6b583a | 29 | * cupsSetServer() - Set the default server name and port. |
7cf5915e | 30 | * cupsSetServerCertCB() - Set the server certificate callback. |
b423cd4c | 31 | * cupsSetUser() - Set the default user name. |
e07d4801 | 32 | * cupsUser() - Return the current user's name. |
b423cd4c | 33 | * _cupsGetPassword() - Get a password from the user. |
e07d4801 MS |
34 | * _cupsSetDefaults() - Set the default server, port, and encryption. |
35 | * cups_read_client_conf() - Read a client.conf file. | |
ef416fc2 | 36 | */ |
37 | ||
38 | /* | |
39 | * Include necessary headers... | |
40 | */ | |
41 | ||
71e16022 | 42 | #include "cups-private.h" |
ef416fc2 | 43 | #include <stdlib.h> |
e00b005a | 44 | #include <sys/stat.h> |
ef416fc2 | 45 | #ifdef WIN32 |
46 | # include <windows.h> | |
5a6b583a MS |
47 | #else |
48 | # include <pwd.h> | |
ef416fc2 | 49 | #endif /* WIN32 */ |
50 | ||
51 | ||
b423cd4c | 52 | /* |
53 | * Local functions... | |
54 | */ | |
55 | ||
e07d4801 MS |
56 | static void cups_read_client_conf(cups_file_t *fp, |
57 | _cups_globals_t *cg, | |
58 | const char *cups_encryption, | |
7cf5915e | 59 | const char *cups_server, |
07ed0e9a MS |
60 | #ifdef HAVE_GSSAPI |
61 | const char *cups_gssservicename, | |
62 | #endif /* HAVE_GSSAPI */ | |
7cf5915e MS |
63 | const char *cups_anyroot, |
64 | const char *cups_expiredroot, | |
65 | const char *cups_expiredcerts); | |
b423cd4c | 66 | |
67 | ||
ef416fc2 | 68 | /* |
5a6b583a | 69 | * 'cupsEncryption()' - Get the current encryption settings. |
ef416fc2 | 70 | * |
71 | * The default encryption setting comes from the CUPS_ENCRYPTION | |
568fa3fa | 72 | * environment variable, then the ~/.cups/client.conf file, and finally the |
ef416fc2 | 73 | * /etc/cups/client.conf file. If not set, the default is |
5a738aea | 74 | * @code HTTP_ENCRYPT_IF_REQUESTED@. |
5a6b583a MS |
75 | * |
76 | * Note: The current encryption setting is tracked separately for each thread | |
77 | * in a program. Multi-threaded programs that override the setting via the | |
78 | * @link cupsSetEncryption@ function need to do so in each thread for the same | |
79 | * setting to be used. | |
ef416fc2 | 80 | */ |
81 | ||
82 | http_encryption_t /* O - Encryption settings */ | |
83 | cupsEncryption(void) | |
84 | { | |
ef416fc2 | 85 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ |
86 | ||
87 | ||
ef416fc2 | 88 | if (cg->encryption == (http_encryption_t)-1) |
e07d4801 | 89 | _cupsSetDefaults(); |
ef416fc2 | 90 | |
91 | return (cg->encryption); | |
92 | } | |
93 | ||
94 | ||
95 | /* | |
96 | * 'cupsGetPassword()' - Get a password from the user. | |
97 | * | |
5a738aea | 98 | * Uses the current password callback function. Returns @code NULL@ if the |
ecdc0628 | 99 | * user does not provide a password. |
5a6b583a MS |
100 | * |
101 | * Note: The current password callback function is tracked separately for each | |
102 | * thread in a program. Multi-threaded programs that override the setting via | |
103 | * the @link cupsSetPasswordCB@ or @link cupsSetPasswordCB2@ functions need to | |
104 | * do so in each thread for the same function to be used. | |
ef416fc2 | 105 | */ |
106 | ||
107 | const char * /* O - Password */ | |
108 | cupsGetPassword(const char *prompt) /* I - Prompt string */ | |
109 | { | |
f11a948a MS |
110 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ |
111 | ||
112 | ||
113 | return ((cg->password_cb)(prompt, NULL, NULL, NULL, cg->password_data)); | |
114 | } | |
115 | ||
116 | ||
117 | /* | |
118 | * 'cupsGetPassword2()' - Get a password from the user using the advanced | |
5a6b583a | 119 | * password callback. |
f11a948a MS |
120 | * |
121 | * Uses the current password callback function. Returns @code NULL@ if the | |
122 | * user does not provide a password. | |
123 | * | |
5a6b583a MS |
124 | * Note: The current password callback function is tracked separately for each |
125 | * thread in a program. Multi-threaded programs that override the setting via | |
126 | * the @link cupsSetPasswordCB@ or @link cupsSetPasswordCB2@ functions need to | |
127 | * do so in each thread for the same function to be used. | |
128 | * | |
178cb736 | 129 | * @since CUPS 1.4/Mac OS X 10.6@ |
f11a948a MS |
130 | */ |
131 | ||
132 | const char * /* O - Password */ | |
133 | cupsGetPassword2(const char *prompt, /* I - Prompt string */ | |
134 | http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ | |
135 | const char *method, /* I - Request method ("GET", "POST", "PUT") */ | |
136 | const char *resource) /* I - Resource path */ | |
137 | { | |
138 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ | |
139 | ||
140 | ||
141 | if (!http) | |
142 | http = _cupsConnect(); | |
143 | ||
144 | return ((cg->password_cb)(prompt, http, method, resource, cg->password_data)); | |
ef416fc2 | 145 | } |
146 | ||
147 | ||
ef416fc2 | 148 | /* |
5a6b583a MS |
149 | * 'cupsServer()' - Return the hostname/address of the current server. |
150 | * | |
151 | * The default server comes from the CUPS_SERVER environment variable, then the | |
152 | * ~/.cups/client.conf file, and finally the /etc/cups/client.conf file. If not | |
153 | * set, the default is the local system - either "localhost" or a domain socket | |
154 | * path. | |
ef416fc2 | 155 | * |
5a6b583a MS |
156 | * The returned value can be a fully-qualified hostname, a numeric IPv4 or IPv6 |
157 | * address, or a domain socket pathname. | |
158 | * | |
159 | * Note: The current server is tracked separately for each thread in a program. | |
160 | * Multi-threaded programs that override the server via the | |
161 | * @link cupsSetServer@ function need to do so in each thread for the same | |
162 | * server to be used. | |
ef416fc2 | 163 | */ |
164 | ||
165 | const char * /* O - Server name */ | |
166 | cupsServer(void) | |
167 | { | |
ef416fc2 | 168 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ |
169 | ||
170 | ||
ef416fc2 | 171 | if (!cg->server[0]) |
e07d4801 | 172 | _cupsSetDefaults(); |
ef416fc2 | 173 | |
e07d4801 MS |
174 | return (cg->server); |
175 | } | |
ef416fc2 | 176 | |
d09495fa | 177 | |
7cf5915e MS |
178 | /* |
179 | * 'cupsSetClientCertCB()' - Set the client certificate callback. | |
180 | * | |
181 | * Pass @code NULL@ to restore the default callback. | |
182 | * | |
183 | * Note: The current certificate callback is tracked separately for each thread | |
184 | * in a program. Multi-threaded programs that override the callback need to do | |
185 | * so in each thread for the same callback to be used. | |
186 | * | |
187 | * @since CUPS 1.5@ | |
188 | */ | |
189 | ||
190 | void | |
191 | cupsSetClientCertCB( | |
192 | cups_client_cert_cb_t cb, /* I - Callback function */ | |
193 | void *user_data) /* I - User data pointer */ | |
194 | { | |
195 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ | |
196 | ||
197 | ||
198 | cg->client_cert_cb = cb; | |
199 | cg->client_cert_data = user_data; | |
200 | } | |
201 | ||
202 | ||
203 | /* | |
204 | * 'cupsSetCredentials()' - Set the default credentials to be used for SSL/TLS | |
205 | * connections. | |
206 | * | |
207 | * Note: The default credentials are tracked separately for each thread in a | |
208 | * program. Multi-threaded programs that override the setting need to do so in | |
209 | * each thread for the same setting to be used. | |
210 | * | |
211 | * @since CUPS 1.5@ | |
212 | */ | |
213 | ||
214 | int /* O - Status of call (0 = success) */ | |
215 | cupsSetCredentials( | |
216 | cups_array_t *credentials) /* I - Array of credentials */ | |
217 | { | |
218 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ | |
219 | ||
220 | ||
221 | if (cupsArrayCount(credentials) < 1) | |
222 | return (-1); | |
223 | ||
224 | _httpFreeCredentials(cg->tls_credentials); | |
225 | cg->tls_credentials = _httpConvertCredentials(credentials); | |
226 | ||
227 | return (cg->tls_credentials ? 0 : -1); | |
228 | } | |
229 | ||
230 | ||
e07d4801 MS |
231 | /* |
232 | * 'cupsSetEncryption()' - Set the encryption preference. | |
5a6b583a MS |
233 | * |
234 | * The default encryption setting comes from the CUPS_ENCRYPTION | |
235 | * environment variable, then the ~/.cups/client.conf file, and finally the | |
236 | * /etc/cups/client.conf file. If not set, the default is | |
237 | * @code HTTP_ENCRYPT_IF_REQUESTED@. | |
238 | * | |
239 | * Note: The current encryption setting is tracked separately for each thread | |
240 | * in a program. Multi-threaded programs that override the setting need to do | |
241 | * so in each thread for the same setting to be used. | |
e07d4801 | 242 | */ |
ef416fc2 | 243 | |
e07d4801 MS |
244 | void |
245 | cupsSetEncryption(http_encryption_t e) /* I - New encryption preference */ | |
246 | { | |
247 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ | |
ef416fc2 | 248 | |
ef416fc2 | 249 | |
e07d4801 | 250 | cg->encryption = e; |
ef416fc2 | 251 | |
e07d4801 MS |
252 | if (cg->http) |
253 | httpEncryption(cg->http, e); | |
ef416fc2 | 254 | } |
255 | ||
256 | ||
257 | /* | |
258 | * 'cupsSetPasswordCB()' - Set the password callback for CUPS. | |
259 | * | |
5a6b583a MS |
260 | * Pass @code NULL@ to restore the default (console) password callback, which |
261 | * reads the password from the console. Programs should call either this | |
262 | * function or @link cupsSetPasswordCB2@, as only one callback can be registered | |
263 | * by a program per thread. | |
264 | * | |
265 | * Note: The current password callback is tracked separately for each thread | |
266 | * in a program. Multi-threaded programs that override the callback need to do | |
267 | * so in each thread for the same callback to be used. | |
ef416fc2 | 268 | */ |
269 | ||
270 | void | |
271 | cupsSetPasswordCB(cups_password_cb_t cb)/* I - Callback function */ | |
272 | { | |
273 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ | |
274 | ||
275 | ||
f11a948a MS |
276 | if (cb == (cups_password_cb_t)0) |
277 | cg->password_cb = (cups_password_cb2_t)_cupsGetPassword; | |
278 | else | |
279 | cg->password_cb = (cups_password_cb2_t)cb; | |
280 | ||
281 | cg->password_data = NULL; | |
282 | } | |
283 | ||
284 | ||
285 | /* | |
286 | * 'cupsSetPasswordCB2()' - Set the advanced password callback for CUPS. | |
287 | * | |
5a6b583a MS |
288 | * Pass @code NULL@ to restore the default (console) password callback, which |
289 | * reads the password from the console. Programs should call either this | |
290 | * function or @link cupsSetPasswordCB2@, as only one callback can be registered | |
291 | * by a program per thread. | |
292 | * | |
293 | * Note: The current password callback is tracked separately for each thread | |
294 | * in a program. Multi-threaded programs that override the callback need to do | |
295 | * so in each thread for the same callback to be used. | |
f11a948a | 296 | * |
178cb736 | 297 | * @since CUPS 1.4/Mac OS X 10.6@ |
f11a948a MS |
298 | */ |
299 | ||
300 | void | |
301 | cupsSetPasswordCB2( | |
302 | cups_password_cb2_t cb, /* I - Callback function */ | |
303 | void *user_data) /* I - User data pointer */ | |
304 | { | |
305 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ | |
306 | ||
307 | ||
308 | if (cb == (cups_password_cb2_t)0) | |
309 | cg->password_cb = (cups_password_cb2_t)_cupsGetPassword; | |
ef416fc2 | 310 | else |
311 | cg->password_cb = cb; | |
f11a948a MS |
312 | |
313 | cg->password_data = user_data; | |
ef416fc2 | 314 | } |
315 | ||
316 | ||
317 | /* | |
5a6b583a | 318 | * 'cupsSetServer()' - Set the default server name and port. |
ef416fc2 | 319 | * |
320 | * The "server" string can be a fully-qualified hostname, a numeric | |
5a6b583a MS |
321 | * IPv4 or IPv6 address, or a domain socket pathname. Hostnames and numeric IP |
322 | * addresses can be optionally followed by a colon and port number to override | |
323 | * the default port 631, e.g. "hostname:8631". Pass @code NULL@ to restore the | |
324 | * default server name and port. | |
325 | * | |
326 | * Note: The current server is tracked separately for each thread in a program. | |
327 | * Multi-threaded programs that override the server need to do so in each | |
328 | * thread for the same server to be used. | |
ef416fc2 | 329 | */ |
330 | ||
331 | void | |
332 | cupsSetServer(const char *server) /* I - Server name */ | |
333 | { | |
334 | char *port; /* Pointer to port */ | |
335 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ | |
336 | ||
337 | ||
338 | if (server) | |
339 | { | |
340 | strlcpy(cg->server, server, sizeof(cg->server)); | |
341 | ||
342 | if (cg->server[0] != '/' && (port = strrchr(cg->server, ':')) != NULL && | |
343 | !strchr(port, ']') && isdigit(port[1] & 255)) | |
344 | { | |
345 | *port++ = '\0'; | |
346 | ||
e07d4801 | 347 | cg->ipp_port = atoi(port); |
ef416fc2 | 348 | } |
349 | ||
350 | if (cg->server[0] == '/') | |
351 | strcpy(cg->servername, "localhost"); | |
352 | else | |
353 | strlcpy(cg->servername, cg->server, sizeof(cg->servername)); | |
354 | } | |
355 | else | |
356 | { | |
357 | cg->server[0] = '\0'; | |
358 | cg->servername[0] = '\0'; | |
359 | } | |
5a738aea MS |
360 | |
361 | if (cg->http) | |
362 | { | |
363 | httpClose(cg->http); | |
364 | cg->http = NULL; | |
365 | } | |
ef416fc2 | 366 | } |
367 | ||
368 | ||
7cf5915e MS |
369 | /* |
370 | * 'cupsSetServerCertCB()' - Set the server certificate callback. | |
371 | * | |
372 | * Pass @code NULL@ to restore the default callback. | |
373 | * | |
374 | * Note: The current credentials callback is tracked separately for each thread | |
375 | * in a program. Multi-threaded programs that override the callback need to do | |
376 | * so in each thread for the same callback to be used. | |
377 | * | |
378 | * @since CUPS 1.5@ | |
379 | */ | |
380 | ||
381 | void | |
382 | cupsSetServerCertCB( | |
383 | cups_server_cert_cb_t cb, /* I - Callback function */ | |
384 | void *user_data) /* I - User data pointer */ | |
385 | { | |
386 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ | |
387 | ||
388 | ||
389 | cg->server_cert_cb = cb; | |
390 | cg->server_cert_data = user_data; | |
391 | } | |
392 | ||
393 | ||
ef416fc2 | 394 | /* |
395 | * 'cupsSetUser()' - Set the default user name. | |
396 | * | |
5a738aea | 397 | * Pass @code NULL@ to restore the default user name. |
5a6b583a MS |
398 | * |
399 | * Note: The current user name is tracked separately for each thread in a | |
400 | * program. Multi-threaded programs that override the user name need to do so | |
401 | * in each thread for the same user name to be used. | |
ef416fc2 | 402 | */ |
403 | ||
404 | void | |
405 | cupsSetUser(const char *user) /* I - User name */ | |
406 | { | |
407 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ | |
408 | ||
409 | ||
410 | if (user) | |
411 | strlcpy(cg->user, user, sizeof(cg->user)); | |
412 | else | |
413 | cg->user[0] = '\0'; | |
414 | } | |
415 | ||
416 | ||
ef416fc2 | 417 | /* |
418 | * 'cupsUser()' - Return the current user's name. | |
5a6b583a MS |
419 | * |
420 | * Note: The current user name is tracked separately for each thread in a | |
421 | * program. Multi-threaded programs that override the user name with the | |
422 | * @link cupsSetUser@ function need to do so in each thread for the same user | |
423 | * name to be used. | |
ef416fc2 | 424 | */ |
425 | ||
426 | const char * /* O - User name */ | |
427 | cupsUser(void) | |
428 | { | |
6d2f911b | 429 | const char *user; /* USER environment variable */ |
ef416fc2 | 430 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ |
431 | ||
432 | ||
433 | if (!cg->user[0]) | |
434 | { | |
5a6b583a MS |
435 | #ifdef WIN32 |
436 | /* | |
437 | * Get the current user name from the OS... | |
438 | */ | |
ef416fc2 | 439 | |
5a6b583a | 440 | DWORD size; /* Size of string */ |
ef416fc2 | 441 | |
442 | size = sizeof(cg->user); | |
443 | if (!GetUserName(cg->user, &size)) | |
ef416fc2 | 444 | #else |
ef416fc2 | 445 | /* |
5a6b583a | 446 | * Get the user name corresponding to the current UID... |
ef416fc2 | 447 | */ |
448 | ||
5a6b583a | 449 | struct passwd *pwd; /* User/password entry */ |
ef416fc2 | 450 | |
5a6b583a MS |
451 | setpwent(); |
452 | if ((pwd = getpwuid(getuid())) != NULL) | |
ef416fc2 | 453 | { |
454 | /* | |
5a6b583a | 455 | * Found a match! |
ef416fc2 | 456 | */ |
457 | ||
ef416fc2 | 458 | strlcpy(cg->user, pwd->pw_name, sizeof(cg->user)); |
459 | } | |
5a6b583a MS |
460 | else |
461 | #endif /* WIN32 */ | |
6d2f911b MS |
462 | if ((user = getenv("USER")) != NULL) |
463 | { | |
464 | /* | |
465 | * Use the username from the "USER" environment variable... | |
466 | */ | |
467 | strlcpy(cg->user, user, sizeof(cg->user)); | |
468 | } | |
469 | else | |
5a6b583a MS |
470 | { |
471 | /* | |
472 | * Use the default "unknown" user name... | |
473 | */ | |
6d2f911b | 474 | |
5a6b583a MS |
475 | strcpy(cg->user, "unknown"); |
476 | } | |
ef416fc2 | 477 | } |
478 | ||
479 | return (cg->user); | |
480 | } | |
481 | ||
482 | ||
483 | /* | |
484 | * '_cupsGetPassword()' - Get a password from the user. | |
485 | */ | |
486 | ||
487 | const char * /* O - Password */ | |
488 | _cupsGetPassword(const char *prompt) /* I - Prompt string */ | |
489 | { | |
5a6b583a MS |
490 | #ifdef WIN32 |
491 | /* | |
492 | * Currently no console password support is provided on Windows. | |
493 | */ | |
494 | ||
495 | return (NULL); | |
496 | ||
497 | #else | |
498 | /* | |
7cf5915e MS |
499 | * Use the standard getpass function to get a password from the console. An |
500 | * empty password is treated as canceling the authentication request. | |
5a6b583a MS |
501 | */ |
502 | ||
7cf5915e MS |
503 | const char *password = getpass(prompt); |
504 | /* Password string */ | |
505 | ||
506 | if (!password || !password[0]) | |
507 | return (NULL); | |
508 | else | |
509 | return (password); | |
ef416fc2 | 510 | #endif /* WIN32 */ |
5a6b583a | 511 | } |
ef416fc2 | 512 | |
513 | ||
514 | /* | |
e07d4801 | 515 | * '_cupsSetDefaults()' - Set the default server, port, and encryption. |
b423cd4c | 516 | */ |
517 | ||
e07d4801 MS |
518 | void |
519 | _cupsSetDefaults(void) | |
b423cd4c | 520 | { |
521 | cups_file_t *fp; /* File */ | |
e07d4801 MS |
522 | const char *home, /* Home directory of user */ |
523 | *cups_encryption, /* CUPS_ENCRYPTION env var */ | |
7cf5915e | 524 | *cups_server, /* CUPS_SERVER env var */ |
07ed0e9a MS |
525 | #ifdef HAVE_GSSAPI |
526 | *cups_gssservicename, /* CUPS_GSSSERVICENAME env var */ | |
527 | #endif /* HAVE_GSSAPI */ | |
7cf5915e MS |
528 | *cups_anyroot, /* CUPS_ANYROOT env var */ |
529 | *cups_expiredroot, /* CUPS_EXPIREDROOT env var */ | |
530 | *cups_expiredcerts; /* CUPS_EXPIREDCERTS env var */ | |
b423cd4c | 531 | char filename[1024]; /* Filename */ |
532 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ | |
533 | ||
534 | ||
e07d4801 MS |
535 | DEBUG_puts("_cupsSetDefaults()"); |
536 | ||
537 | /* | |
538 | * First collect environment variables... | |
539 | */ | |
540 | ||
07ed0e9a MS |
541 | cups_encryption = getenv("CUPS_ENCRYPTION"); |
542 | cups_server = getenv("CUPS_SERVER"); | |
543 | #ifdef HAVE_GSSAPI | |
544 | cups_gssservicename = getenv("CUPS_GSSSERVICENAME"); | |
545 | #endif /* HAVE_GSSAPI */ | |
546 | cups_anyroot = getenv("CUPS_ANYROOT"); | |
547 | cups_expiredroot = getenv("CUPS_EXPIREDROOT"); | |
548 | cups_expiredcerts = getenv("CUPS_EXPIREDCERTS"); | |
e07d4801 MS |
549 | |
550 | /* | |
10d09e33 MS |
551 | * Then, if needed, read the ~/.cups/client.conf or /etc/cups/client.conf |
552 | * files to get the default values... | |
e07d4801 MS |
553 | */ |
554 | ||
e07d4801 MS |
555 | if (cg->encryption == (http_encryption_t)-1 || !cg->server[0] || |
556 | !cg->ipp_port) | |
557 | { | |
10d09e33 | 558 | if ((home = getenv("HOME")) != NULL) |
d09495fa | 559 | { |
10d09e33 MS |
560 | /* |
561 | * Look for ~/.cups/client.conf... | |
562 | */ | |
e07d4801 | 563 | |
10d09e33 MS |
564 | snprintf(filename, sizeof(filename), "%s/.cups/client.conf", home); |
565 | fp = cupsFileOpen(filename, "r"); | |
566 | } | |
567 | else | |
568 | fp = NULL; | |
e07d4801 | 569 | |
10d09e33 | 570 | if (!fp) |
e07d4801 | 571 | { |
e07d4801 | 572 | /* |
10d09e33 | 573 | * Look for CUPS_SERVERROOT/client.conf... |
e07d4801 MS |
574 | */ |
575 | ||
10d09e33 MS |
576 | snprintf(filename, sizeof(filename), "%s/client.conf", |
577 | cg->cups_serverroot); | |
578 | fp = cupsFileOpen(filename, "r"); | |
e07d4801 MS |
579 | } |
580 | ||
10d09e33 MS |
581 | /* |
582 | * Read the configuration file and apply any environment variables; both | |
583 | * functions handle NULL cups_file_t pointers... | |
584 | */ | |
e07d4801 | 585 | |
10d09e33 | 586 | cups_read_client_conf(fp, cg, cups_encryption, cups_server, |
07ed0e9a MS |
587 | #ifdef HAVE_GSSAPI |
588 | cups_gssservicename, | |
589 | #endif /* HAVE_GSSAPI */ | |
10d09e33 MS |
590 | cups_anyroot, cups_expiredroot, |
591 | cups_expiredcerts); | |
592 | cupsFileClose(fp); | |
e07d4801 MS |
593 | } |
594 | } | |
595 | ||
596 | ||
597 | /* | |
598 | * 'cups_read_client_conf()' - Read a client.conf file. | |
599 | */ | |
600 | ||
601 | static void | |
602 | cups_read_client_conf( | |
603 | cups_file_t *fp, /* I - File to read */ | |
604 | _cups_globals_t *cg, /* I - Global data */ | |
605 | const char *cups_encryption, /* I - CUPS_ENCRYPTION env var */ | |
7cf5915e | 606 | const char *cups_server, /* I - CUPS_SERVER env var */ |
07ed0e9a MS |
607 | #ifdef HAVE_GSSAPI |
608 | const char *cups_gssservicename, | |
609 | /* I - CUPS_GSSSERVICENAME env var */ | |
610 | #endif /* HAVE_GSSAPI */ | |
7cf5915e MS |
611 | const char *cups_anyroot, /* I - CUPS_ANYROOT env var */ |
612 | const char *cups_expiredroot, /* I - CUPS_EXPIREDROOT env var */ | |
613 | const char *cups_expiredcerts) /* I - CUPS_EXPIREDCERTS env var */ | |
e07d4801 MS |
614 | { |
615 | int linenum; /* Current line number */ | |
616 | char line[1024], /* Line from file */ | |
617 | *value, /* Pointer into line */ | |
618 | encryption[1024], /* Encryption value */ | |
7cf5915e MS |
619 | server_name[1024], /* ServerName value */ |
620 | any_root[1024], /* AllowAnyRoot value */ | |
621 | expired_root[1024], /* AllowExpiredRoot value */ | |
622 | expired_certs[1024]; /* AllowExpiredCerts value */ | |
07ed0e9a MS |
623 | #ifdef HAVE_GSSAPI |
624 | char gss_service_name[32]; /* GSSServiceName value */ | |
625 | #endif /* HAVE_GSSAPI */ | |
e07d4801 MS |
626 | |
627 | ||
628 | /* | |
629 | * Read from the file... | |
630 | */ | |
631 | ||
632 | linenum = 0; | |
633 | while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum)) | |
634 | { | |
635 | if (!cups_encryption && cg->encryption == (http_encryption_t)-1 && | |
636 | !strcasecmp(line, "Encryption") && value) | |
637 | { | |
638 | strlcpy(encryption, value, sizeof(encryption)); | |
639 | cups_encryption = encryption; | |
640 | } | |
641 | else if (!cups_server && (!cg->server[0] || !cg->ipp_port) && | |
642 | !strcasecmp(line, "ServerName") && value) | |
643 | { | |
644 | strlcpy(server_name, value, sizeof(server_name)); | |
645 | cups_server = server_name; | |
646 | } | |
7cf5915e MS |
647 | else if (!cups_anyroot && !strcasecmp(line, "AllowAnyRoot") && value) |
648 | { | |
649 | strlcpy(any_root, value, sizeof(any_root)); | |
650 | cups_anyroot = any_root; | |
651 | } | |
652 | else if (!cups_expiredroot && !strcasecmp(line, "AllowExpiredRoot") && | |
653 | value) | |
654 | { | |
655 | strlcpy(expired_root, value, sizeof(expired_root)); | |
656 | cups_expiredroot = expired_root; | |
657 | } | |
658 | else if (!cups_expiredcerts && !strcasecmp(line, "AllowExpiredCerts") && | |
659 | value) | |
660 | { | |
661 | strlcpy(expired_certs, value, sizeof(expired_certs)); | |
662 | cups_expiredcerts = expired_certs; | |
663 | } | |
07ed0e9a MS |
664 | #ifdef HAVE_GSSAPI |
665 | else if (!cups_gssservicename && !strcasecmp(line, "GSSServiceName") && | |
666 | value) | |
667 | { | |
668 | strlcpy(gss_service_name, value, sizeof(gss_service_name)); | |
669 | cups_gssservicename = gss_service_name; | |
670 | } | |
671 | #endif /* HAVE_GSSAPI */ | |
e07d4801 MS |
672 | } |
673 | ||
674 | /* | |
675 | * Set values... | |
676 | */ | |
677 | ||
678 | if (cg->encryption == (http_encryption_t)-1 && cups_encryption) | |
679 | { | |
680 | if (!strcasecmp(cups_encryption, "never")) | |
681 | cg->encryption = HTTP_ENCRYPT_NEVER; | |
682 | else if (!strcasecmp(cups_encryption, "always")) | |
683 | cg->encryption = HTTP_ENCRYPT_ALWAYS; | |
684 | else if (!strcasecmp(cups_encryption, "required")) | |
685 | cg->encryption = HTTP_ENCRYPT_REQUIRED; | |
686 | else | |
687 | cg->encryption = HTTP_ENCRYPT_IF_REQUESTED; | |
b423cd4c | 688 | } |
689 | ||
e07d4801 MS |
690 | if ((!cg->server[0] || !cg->ipp_port) && cups_server) |
691 | { | |
692 | if (!cg->server[0]) | |
693 | { | |
694 | /* | |
695 | * Copy server name... | |
696 | */ | |
697 | ||
698 | strlcpy(cg->server, cups_server, sizeof(cg->server)); | |
699 | ||
700 | if (cg->server[0] != '/' && (value = strrchr(cg->server, ':')) != NULL && | |
701 | !strchr(value, ']') && isdigit(value[1] & 255)) | |
702 | *value++ = '\0'; | |
703 | else | |
704 | value = NULL; | |
705 | ||
706 | if (cg->server[0] == '/') | |
707 | strcpy(cg->servername, "localhost"); | |
708 | else | |
709 | strlcpy(cg->servername, cg->server, sizeof(cg->servername)); | |
710 | } | |
711 | else if (cups_server[0] != '/' && | |
712 | (value = strrchr(cups_server, ':')) != NULL && | |
713 | !strchr(value, ']') && isdigit(value[1] & 255)) | |
714 | value ++; | |
715 | else | |
716 | value = NULL; | |
717 | ||
718 | if (!cg->ipp_port && value) | |
719 | cg->ipp_port = atoi(value); | |
720 | } | |
7cf5915e | 721 | |
10d09e33 MS |
722 | if (!cg->server[0]) |
723 | { | |
724 | #ifdef CUPS_DEFAULT_DOMAINSOCKET | |
725 | /* | |
726 | * If we are compiled with domain socket support, only use the | |
727 | * domain socket if it exists and has the right permissions... | |
728 | */ | |
729 | ||
730 | struct stat sockinfo; /* Domain socket information */ | |
731 | ||
732 | if (!stat(CUPS_DEFAULT_DOMAINSOCKET, &sockinfo) && | |
733 | (sockinfo.st_mode & S_IRWXO) == S_IRWXO) | |
734 | cups_server = CUPS_DEFAULT_DOMAINSOCKET; | |
735 | else | |
736 | #endif /* CUPS_DEFAULT_DOMAINSOCKET */ | |
737 | cups_server = "localhost"; | |
738 | ||
739 | cupsSetServer(cups_server); | |
740 | } | |
741 | ||
742 | if (!cg->ipp_port) | |
743 | { | |
744 | const char *ipp_port; /* IPP_PORT environment variable */ | |
745 | struct servent *service; /* Port number info */ | |
746 | ||
747 | ||
748 | if ((ipp_port = getenv("IPP_PORT")) != NULL) | |
749 | { | |
750 | if ((cg->ipp_port = atoi(ipp_port)) <= 0) | |
751 | cg->ipp_port = CUPS_DEFAULT_IPP_PORT; | |
752 | } | |
753 | else if ((service = getservbyname("ipp", NULL)) == NULL || | |
754 | service->s_port <= 0) | |
755 | cg->ipp_port = CUPS_DEFAULT_IPP_PORT; | |
756 | else | |
757 | cg->ipp_port = ntohs(service->s_port); | |
758 | } | |
759 | ||
07ed0e9a MS |
760 | #ifdef HAVE_GSSAPI |
761 | if (!cups_gssservicename) | |
762 | cups_gssservicename = CUPS_DEFAULT_GSSSERVICENAME; | |
763 | ||
764 | strlcpy(cg->gss_service_name, cups_gssservicename, | |
765 | sizeof(cg->gss_service_name)); | |
766 | #endif /* HAVE_GSSAPI */ | |
767 | ||
7cf5915e MS |
768 | if (cups_anyroot) |
769 | cg->any_root = !strcasecmp(cups_anyroot, "yes") || | |
770 | !strcasecmp(cups_anyroot, "on") || | |
771 | !strcasecmp(cups_anyroot, "true"); | |
772 | ||
773 | if (cups_expiredroot) | |
774 | cg->expired_root = !strcasecmp(cups_expiredroot, "yes") || | |
775 | !strcasecmp(cups_expiredroot, "on") || | |
776 | !strcasecmp(cups_expiredroot, "true"); | |
777 | ||
778 | if (cups_expiredcerts) | |
779 | cg->expired_certs = !strcasecmp(cups_expiredcerts, "yes") || | |
780 | !strcasecmp(cups_expiredcerts, "on") || | |
781 | !strcasecmp(cups_expiredcerts, "true"); | |
b423cd4c | 782 | } |
783 | ||
784 | ||
785 | /* | |
e07d4801 | 786 | * End of "$Id: usersys.c 8498 2009-04-13 17:03:15Z mike $". |
ef416fc2 | 787 | */ |