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