]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
83bc2aac MS |
2 | * User, system, and password routines for CUPS. |
3 | * | |
53af7f21 | 4 | * Copyright 2007-2017 by Apple Inc. |
83bc2aac MS |
5 | * Copyright 1997-2006 by Easy Software Products. |
6 | * | |
7 | * These coded instructions, statements, and computer programs are the | |
8 | * property of Apple Inc. and are protected by Federal copyright | |
9 | * law. Distribution and use rights are outlined in the file "LICENSE.txt" | |
10 | * which should have been included with this file. If this file is | |
57b7b66b | 11 | * missing or damaged, see the license at "http://www.cups.org/". |
83bc2aac MS |
12 | * |
13 | * This file is subject to the Apple OS-Developed Software exception. | |
ef416fc2 | 14 | */ |
15 | ||
16 | /* | |
17 | * Include necessary headers... | |
18 | */ | |
19 | ||
71e16022 | 20 | #include "cups-private.h" |
ef416fc2 | 21 | #include <stdlib.h> |
e00b005a | 22 | #include <sys/stat.h> |
ef416fc2 | 23 | #ifdef WIN32 |
24 | # include <windows.h> | |
5a6b583a MS |
25 | #else |
26 | # include <pwd.h> | |
dcb445bc | 27 | # include <termios.h> |
db8b865d | 28 | # include <sys/utsname.h> |
ef416fc2 | 29 | #endif /* WIN32 */ |
30 | ||
31 | ||
dcb445bc MS |
32 | /* |
33 | * Local constants... | |
34 | */ | |
35 | ||
08d56b1f MS |
36 | #ifdef __APPLE__ |
37 | # define kCUPSPrintingPrefs CFSTR("org.cups.PrintingPrefs") | |
38 | # define kAllowAnyRootKey CFSTR("AllowAnyRoot") | |
39 | # define kAllowExpiredCertsKey CFSTR("AllowExpiredCerts") | |
40 | # define kEncryptionKey CFSTR("Encryption") | |
41 | # define kGSSServiceNameKey CFSTR("GSSServiceName") | |
42 | # define kSSLOptionsKey CFSTR("SSLOptions") | |
43 | # define kTrustOnFirstUseKey CFSTR("TrustOnFirstUse") | |
44 | # define kValidateCertsKey CFSTR("ValidateCerts") | |
45 | #endif /* __APPLE__ */ | |
46 | ||
dcb445bc MS |
47 | #define _CUPS_PASSCHAR '*' /* Character that is echoed for password */ |
48 | ||
49 | ||
3abb875b MS |
50 | /* |
51 | * Local types... | |
52 | */ | |
53 | ||
54 | typedef struct _cups_client_conf_s /**** client.conf config data ****/ | |
55 | { | |
56 | #ifdef HAVE_SSL | |
57 | int ssl_options; /* SSLOptions values */ | |
58 | #endif /* HAVE_SSL */ | |
08d56b1f MS |
59 | int trust_first, /* Trust on first use? */ |
60 | any_root, /* Allow any (e.g., self-signed) root */ | |
3abb875b MS |
61 | expired_certs, /* Allow expired certs */ |
62 | validate_certs; /* Validate certificates */ | |
63 | http_encryption_t encryption; /* Encryption setting */ | |
64 | char user[65], /* User name */ | |
65 | server_name[256]; | |
66 | /* Server hostname */ | |
67 | #ifdef HAVE_GSSAPI | |
68 | char gss_service_name[32]; | |
69 | /* Kerberos service name */ | |
70 | #endif /* HAVE_GSSAPI */ | |
71 | } _cups_client_conf_t; | |
72 | ||
73 | ||
b423cd4c | 74 | /* |
75 | * Local functions... | |
76 | */ | |
77 | ||
08d56b1f MS |
78 | #ifdef __APPLE__ |
79 | static int cups_apple_get_boolean(CFStringRef key, int *value); | |
80 | static int cups_apple_get_string(CFStringRef key, char *value, size_t valsize); | |
81 | #endif /* __APPLE__ */ | |
82 | static int cups_boolean_value(const char *value); | |
3abb875b MS |
83 | static void cups_finalize_client_conf(_cups_client_conf_t *cc); |
84 | static void cups_init_client_conf(_cups_client_conf_t *cc); | |
85 | static void cups_read_client_conf(cups_file_t *fp, _cups_client_conf_t *cc); | |
4b9daaf4 | 86 | static void cups_set_default_ipp_port(_cups_globals_t *cg); |
3abb875b | 87 | static void cups_set_encryption(_cups_client_conf_t *cc, const char *value); |
07ed0e9a | 88 | #ifdef HAVE_GSSAPI |
3abb875b | 89 | static void cups_set_gss_service_name(_cups_client_conf_t *cc, const char *value); |
07ed0e9a | 90 | #endif /* HAVE_GSSAPI */ |
3abb875b MS |
91 | static void cups_set_server_name(_cups_client_conf_t *cc, const char *value); |
92 | #ifdef HAVE_SSL | |
93 | static void cups_set_ssl_options(_cups_client_conf_t *cc, const char *value); | |
94 | #endif /* HAVE_SSL */ | |
95 | static void cups_set_user(_cups_client_conf_t *cc, const char *value); | |
b423cd4c | 96 | |
97 | ||
ef416fc2 | 98 | /* |
5a6b583a | 99 | * 'cupsEncryption()' - Get the current encryption settings. |
ef416fc2 | 100 | * |
101 | * The default encryption setting comes from the CUPS_ENCRYPTION | |
568fa3fa | 102 | * environment variable, then the ~/.cups/client.conf file, and finally the |
ef416fc2 | 103 | * /etc/cups/client.conf file. If not set, the default is |
cb7f98ee | 104 | * @code HTTP_ENCRYPTION_IF_REQUESTED@. |
5a6b583a MS |
105 | * |
106 | * Note: The current encryption setting is tracked separately for each thread | |
107 | * in a program. Multi-threaded programs that override the setting via the | |
108 | * @link cupsSetEncryption@ function need to do so in each thread for the same | |
109 | * setting to be used. | |
ef416fc2 | 110 | */ |
111 | ||
112 | http_encryption_t /* O - Encryption settings */ | |
113 | cupsEncryption(void) | |
114 | { | |
ef416fc2 | 115 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ |
116 | ||
117 | ||
ef416fc2 | 118 | if (cg->encryption == (http_encryption_t)-1) |
e07d4801 | 119 | _cupsSetDefaults(); |
ef416fc2 | 120 | |
121 | return (cg->encryption); | |
122 | } | |
123 | ||
124 | ||
125 | /* | |
126 | * 'cupsGetPassword()' - Get a password from the user. | |
127 | * | |
5a738aea | 128 | * Uses the current password callback function. Returns @code NULL@ if the |
ecdc0628 | 129 | * user does not provide a password. |
5a6b583a MS |
130 | * |
131 | * Note: The current password callback function is tracked separately for each | |
132 | * thread in a program. Multi-threaded programs that override the setting via | |
133 | * the @link cupsSetPasswordCB@ or @link cupsSetPasswordCB2@ functions need to | |
134 | * do so in each thread for the same function to be used. | |
53af7f21 MS |
135 | * |
136 | * @exclude all@ | |
ef416fc2 | 137 | */ |
138 | ||
139 | const char * /* O - Password */ | |
140 | cupsGetPassword(const char *prompt) /* I - Prompt string */ | |
141 | { | |
f11a948a MS |
142 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ |
143 | ||
144 | ||
145 | return ((cg->password_cb)(prompt, NULL, NULL, NULL, cg->password_data)); | |
146 | } | |
147 | ||
148 | ||
149 | /* | |
98d88c8d | 150 | * 'cupsGetPassword2()' - Get a password from the user using the current |
5a6b583a | 151 | * password callback. |
f11a948a MS |
152 | * |
153 | * Uses the current password callback function. Returns @code NULL@ if the | |
154 | * user does not provide a password. | |
155 | * | |
5a6b583a MS |
156 | * Note: The current password callback function is tracked separately for each |
157 | * thread in a program. Multi-threaded programs that override the setting via | |
98d88c8d MS |
158 | * the @link cupsSetPasswordCB2@ function need to do so in each thread for the |
159 | * same function to be used. | |
5a6b583a | 160 | * |
8072030b | 161 | * @since CUPS 1.4/macOS 10.6@ |
f11a948a MS |
162 | */ |
163 | ||
164 | const char * /* O - Password */ | |
165 | cupsGetPassword2(const char *prompt, /* I - Prompt string */ | |
166 | http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ | |
167 | const char *method, /* I - Request method ("GET", "POST", "PUT") */ | |
168 | const char *resource) /* I - Resource path */ | |
169 | { | |
170 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ | |
171 | ||
172 | ||
173 | if (!http) | |
174 | http = _cupsConnect(); | |
175 | ||
176 | return ((cg->password_cb)(prompt, http, method, resource, cg->password_data)); | |
ef416fc2 | 177 | } |
178 | ||
179 | ||
ef416fc2 | 180 | /* |
5a6b583a MS |
181 | * 'cupsServer()' - Return the hostname/address of the current server. |
182 | * | |
183 | * The default server comes from the CUPS_SERVER environment variable, then the | |
184 | * ~/.cups/client.conf file, and finally the /etc/cups/client.conf file. If not | |
185 | * set, the default is the local system - either "localhost" or a domain socket | |
186 | * path. | |
ef416fc2 | 187 | * |
5a6b583a MS |
188 | * The returned value can be a fully-qualified hostname, a numeric IPv4 or IPv6 |
189 | * address, or a domain socket pathname. | |
190 | * | |
191 | * Note: The current server is tracked separately for each thread in a program. | |
192 | * Multi-threaded programs that override the server via the | |
193 | * @link cupsSetServer@ function need to do so in each thread for the same | |
194 | * server to be used. | |
ef416fc2 | 195 | */ |
196 | ||
197 | const char * /* O - Server name */ | |
198 | cupsServer(void) | |
199 | { | |
ef416fc2 | 200 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ |
201 | ||
202 | ||
ef416fc2 | 203 | if (!cg->server[0]) |
e07d4801 | 204 | _cupsSetDefaults(); |
ef416fc2 | 205 | |
e07d4801 MS |
206 | return (cg->server); |
207 | } | |
ef416fc2 | 208 | |
d09495fa | 209 | |
7cf5915e MS |
210 | /* |
211 | * 'cupsSetClientCertCB()' - Set the client certificate callback. | |
212 | * | |
213 | * Pass @code NULL@ to restore the default callback. | |
214 | * | |
215 | * Note: The current certificate callback is tracked separately for each thread | |
216 | * in a program. Multi-threaded programs that override the callback need to do | |
217 | * so in each thread for the same callback to be used. | |
218 | * | |
8072030b | 219 | * @since CUPS 1.5/macOS 10.7@ |
7cf5915e MS |
220 | */ |
221 | ||
222 | void | |
223 | cupsSetClientCertCB( | |
224 | cups_client_cert_cb_t cb, /* I - Callback function */ | |
225 | void *user_data) /* I - User data pointer */ | |
226 | { | |
227 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ | |
228 | ||
229 | ||
230 | cg->client_cert_cb = cb; | |
231 | cg->client_cert_data = user_data; | |
232 | } | |
233 | ||
234 | ||
235 | /* | |
236 | * 'cupsSetCredentials()' - Set the default credentials to be used for SSL/TLS | |
237 | * connections. | |
238 | * | |
239 | * Note: The default credentials are tracked separately for each thread in a | |
240 | * program. Multi-threaded programs that override the setting need to do so in | |
241 | * each thread for the same setting to be used. | |
242 | * | |
8072030b | 243 | * @since CUPS 1.5/macOS 10.7@ |
7cf5915e MS |
244 | */ |
245 | ||
246 | int /* O - Status of call (0 = success) */ | |
247 | cupsSetCredentials( | |
248 | cups_array_t *credentials) /* I - Array of credentials */ | |
249 | { | |
250 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ | |
251 | ||
252 | ||
253 | if (cupsArrayCount(credentials) < 1) | |
254 | return (-1); | |
255 | ||
7d5824d6 | 256 | #ifdef HAVE_SSL |
7cf5915e | 257 | _httpFreeCredentials(cg->tls_credentials); |
85dda01c | 258 | cg->tls_credentials = _httpCreateCredentials(credentials); |
7d5824d6 | 259 | #endif /* HAVE_SSL */ |
7cf5915e MS |
260 | |
261 | return (cg->tls_credentials ? 0 : -1); | |
262 | } | |
263 | ||
264 | ||
e07d4801 MS |
265 | /* |
266 | * 'cupsSetEncryption()' - Set the encryption preference. | |
5a6b583a MS |
267 | * |
268 | * The default encryption setting comes from the CUPS_ENCRYPTION | |
269 | * environment variable, then the ~/.cups/client.conf file, and finally the | |
270 | * /etc/cups/client.conf file. If not set, the default is | |
cb7f98ee | 271 | * @code HTTP_ENCRYPTION_IF_REQUESTED@. |
5a6b583a MS |
272 | * |
273 | * Note: The current encryption setting is tracked separately for each thread | |
274 | * in a program. Multi-threaded programs that override the setting need to do | |
275 | * so in each thread for the same setting to be used. | |
e07d4801 | 276 | */ |
ef416fc2 | 277 | |
e07d4801 MS |
278 | void |
279 | cupsSetEncryption(http_encryption_t e) /* I - New encryption preference */ | |
280 | { | |
281 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ | |
ef416fc2 | 282 | |
ef416fc2 | 283 | |
e07d4801 | 284 | cg->encryption = e; |
ef416fc2 | 285 | |
e07d4801 MS |
286 | if (cg->http) |
287 | httpEncryption(cg->http, e); | |
ef416fc2 | 288 | } |
289 | ||
290 | ||
291 | /* | |
292 | * 'cupsSetPasswordCB()' - Set the password callback for CUPS. | |
293 | * | |
5a6b583a MS |
294 | * Pass @code NULL@ to restore the default (console) password callback, which |
295 | * reads the password from the console. Programs should call either this | |
296 | * function or @link cupsSetPasswordCB2@, as only one callback can be registered | |
297 | * by a program per thread. | |
298 | * | |
299 | * Note: The current password callback is tracked separately for each thread | |
300 | * in a program. Multi-threaded programs that override the callback need to do | |
301 | * so in each thread for the same callback to be used. | |
53af7f21 MS |
302 | * |
303 | * @exclude all@ | |
ef416fc2 | 304 | */ |
305 | ||
306 | void | |
307 | cupsSetPasswordCB(cups_password_cb_t cb)/* I - Callback function */ | |
308 | { | |
309 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ | |
310 | ||
311 | ||
f11a948a MS |
312 | if (cb == (cups_password_cb_t)0) |
313 | cg->password_cb = (cups_password_cb2_t)_cupsGetPassword; | |
314 | else | |
315 | cg->password_cb = (cups_password_cb2_t)cb; | |
316 | ||
317 | cg->password_data = NULL; | |
318 | } | |
319 | ||
320 | ||
321 | /* | |
322 | * 'cupsSetPasswordCB2()' - Set the advanced password callback for CUPS. | |
323 | * | |
5a6b583a MS |
324 | * Pass @code NULL@ to restore the default (console) password callback, which |
325 | * reads the password from the console. Programs should call either this | |
326 | * function or @link cupsSetPasswordCB2@, as only one callback can be registered | |
327 | * by a program per thread. | |
328 | * | |
329 | * Note: The current password callback is tracked separately for each thread | |
330 | * in a program. Multi-threaded programs that override the callback need to do | |
331 | * so in each thread for the same callback to be used. | |
f11a948a | 332 | * |
8072030b | 333 | * @since CUPS 1.4/macOS 10.6@ |
f11a948a MS |
334 | */ |
335 | ||
336 | void | |
337 | cupsSetPasswordCB2( | |
338 | cups_password_cb2_t cb, /* I - Callback function */ | |
339 | void *user_data) /* I - User data pointer */ | |
340 | { | |
341 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ | |
342 | ||
343 | ||
344 | if (cb == (cups_password_cb2_t)0) | |
345 | cg->password_cb = (cups_password_cb2_t)_cupsGetPassword; | |
ef416fc2 | 346 | else |
347 | cg->password_cb = cb; | |
f11a948a MS |
348 | |
349 | cg->password_data = user_data; | |
ef416fc2 | 350 | } |
351 | ||
352 | ||
353 | /* | |
5a6b583a | 354 | * 'cupsSetServer()' - Set the default server name and port. |
ef416fc2 | 355 | * |
356 | * The "server" string can be a fully-qualified hostname, a numeric | |
5a6b583a MS |
357 | * IPv4 or IPv6 address, or a domain socket pathname. Hostnames and numeric IP |
358 | * addresses can be optionally followed by a colon and port number to override | |
359 | * the default port 631, e.g. "hostname:8631". Pass @code NULL@ to restore the | |
360 | * default server name and port. | |
361 | * | |
362 | * Note: The current server is tracked separately for each thread in a program. | |
363 | * Multi-threaded programs that override the server need to do so in each | |
364 | * thread for the same server to be used. | |
ef416fc2 | 365 | */ |
366 | ||
367 | void | |
368 | cupsSetServer(const char *server) /* I - Server name */ | |
369 | { | |
0cb67df3 MS |
370 | char *options, /* Options */ |
371 | *port; /* Pointer to port */ | |
ef416fc2 | 372 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ |
373 | ||
374 | ||
375 | if (server) | |
376 | { | |
377 | strlcpy(cg->server, server, sizeof(cg->server)); | |
378 | ||
0cb67df3 MS |
379 | if (cg->server[0] != '/' && (options = strrchr(cg->server, '/')) != NULL) |
380 | { | |
381 | *options++ = '\0'; | |
382 | ||
383 | if (!strcmp(options, "version=1.0")) | |
384 | cg->server_version = 10; | |
385 | else if (!strcmp(options, "version=1.1")) | |
386 | cg->server_version = 11; | |
387 | else if (!strcmp(options, "version=2.0")) | |
388 | cg->server_version = 20; | |
389 | else if (!strcmp(options, "version=2.1")) | |
390 | cg->server_version = 21; | |
391 | else if (!strcmp(options, "version=2.2")) | |
392 | cg->server_version = 22; | |
393 | } | |
567f49cb MS |
394 | else |
395 | cg->server_version = 20; | |
0cb67df3 | 396 | |
ef416fc2 | 397 | if (cg->server[0] != '/' && (port = strrchr(cg->server, ':')) != NULL && |
398 | !strchr(port, ']') && isdigit(port[1] & 255)) | |
399 | { | |
400 | *port++ = '\0'; | |
401 | ||
e07d4801 | 402 | cg->ipp_port = atoi(port); |
ef416fc2 | 403 | } |
404 | ||
4b9daaf4 MS |
405 | if (!cg->ipp_port) |
406 | cups_set_default_ipp_port(cg); | |
407 | ||
ef416fc2 | 408 | if (cg->server[0] == '/') |
5a9febac | 409 | strlcpy(cg->servername, "localhost", sizeof(cg->servername)); |
ef416fc2 | 410 | else |
411 | strlcpy(cg->servername, cg->server, sizeof(cg->servername)); | |
412 | } | |
413 | else | |
414 | { | |
0cb67df3 MS |
415 | cg->server[0] = '\0'; |
416 | cg->servername[0] = '\0'; | |
417 | cg->server_version = 20; | |
4b9daaf4 | 418 | cg->ipp_port = 0; |
ef416fc2 | 419 | } |
5a738aea MS |
420 | |
421 | if (cg->http) | |
422 | { | |
423 | httpClose(cg->http); | |
424 | cg->http = NULL; | |
425 | } | |
ef416fc2 | 426 | } |
427 | ||
428 | ||
7cf5915e MS |
429 | /* |
430 | * 'cupsSetServerCertCB()' - Set the server certificate callback. | |
431 | * | |
432 | * Pass @code NULL@ to restore the default callback. | |
433 | * | |
434 | * Note: The current credentials callback is tracked separately for each thread | |
435 | * in a program. Multi-threaded programs that override the callback need to do | |
436 | * so in each thread for the same callback to be used. | |
437 | * | |
8072030b | 438 | * @since CUPS 1.5/macOS 10.7@ |
7cf5915e MS |
439 | */ |
440 | ||
441 | void | |
442 | cupsSetServerCertCB( | |
443 | cups_server_cert_cb_t cb, /* I - Callback function */ | |
444 | void *user_data) /* I - User data pointer */ | |
445 | { | |
446 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ | |
447 | ||
448 | ||
449 | cg->server_cert_cb = cb; | |
450 | cg->server_cert_data = user_data; | |
451 | } | |
452 | ||
453 | ||
ef416fc2 | 454 | /* |
455 | * 'cupsSetUser()' - Set the default user name. | |
456 | * | |
5a738aea | 457 | * Pass @code NULL@ to restore the default user name. |
5a6b583a MS |
458 | * |
459 | * Note: The current user name is tracked separately for each thread in a | |
460 | * program. Multi-threaded programs that override the user name need to do so | |
461 | * in each thread for the same user name to be used. | |
ef416fc2 | 462 | */ |
463 | ||
464 | void | |
465 | cupsSetUser(const char *user) /* I - User name */ | |
466 | { | |
467 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ | |
468 | ||
469 | ||
470 | if (user) | |
471 | strlcpy(cg->user, user, sizeof(cg->user)); | |
472 | else | |
473 | cg->user[0] = '\0'; | |
474 | } | |
475 | ||
476 | ||
db8b865d MS |
477 | /* |
478 | * 'cupsSetUserAgent()' - Set the default HTTP User-Agent string. | |
479 | * | |
480 | * Setting the string to NULL forces the default value containing the CUPS | |
481 | * version, IPP version, and operating system version and architecture. | |
482 | * | |
8072030b | 483 | * @since CUPS 1.7/macOS 10.9@ |
db8b865d MS |
484 | */ |
485 | ||
486 | void | |
487 | cupsSetUserAgent(const char *user_agent)/* I - User-Agent string or @code NULL@ */ | |
488 | { | |
489 | _cups_globals_t *cg = _cupsGlobals(); | |
490 | /* Thread globals */ | |
491 | #ifdef WIN32 | |
492 | SYSTEM_INFO sysinfo; /* System information */ | |
493 | OSVERSIONINFO version; /* OS version info */ | |
494 | #else | |
495 | struct utsname name; /* uname info */ | |
496 | #endif /* WIN32 */ | |
497 | ||
498 | ||
499 | if (user_agent) | |
500 | { | |
501 | strlcpy(cg->user_agent, user_agent, sizeof(cg->user_agent)); | |
502 | return; | |
503 | } | |
504 | ||
505 | #ifdef WIN32 | |
506 | version.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); | |
507 | GetVersionEx(&version); | |
508 | GetNativeSystemInfo(&sysinfo); | |
509 | ||
510 | snprintf(cg->user_agent, sizeof(cg->user_agent), | |
511 | CUPS_MINIMAL " (Windows %d.%d; %s) IPP/2.0", | |
512 | version.dwMajorVersion, version.dwMinorVersion, | |
513 | sysinfo.wProcessorArchitecture | |
514 | == PROCESSOR_ARCHITECTURE_AMD64 ? "amd64" : | |
515 | sysinfo.wProcessorArchitecture | |
516 | == PROCESSOR_ARCHITECTURE_ARM ? "arm" : | |
517 | sysinfo.wProcessorArchitecture | |
518 | == PROCESSOR_ARCHITECTURE_IA64 ? "ia64" : | |
519 | sysinfo.wProcessorArchitecture | |
520 | == PROCESSOR_ARCHITECTURE_INTEL ? "intel" : | |
521 | "unknown"); | |
522 | ||
523 | #else | |
524 | uname(&name); | |
525 | ||
526 | snprintf(cg->user_agent, sizeof(cg->user_agent), | |
527 | CUPS_MINIMAL " (%s %s; %s) IPP/2.0", | |
528 | name.sysname, name.release, name.machine); | |
529 | #endif /* WIN32 */ | |
530 | } | |
531 | ||
532 | ||
ef416fc2 | 533 | /* |
534 | * 'cupsUser()' - Return the current user's name. | |
5a6b583a MS |
535 | * |
536 | * Note: The current user name is tracked separately for each thread in a | |
537 | * program. Multi-threaded programs that override the user name with the | |
538 | * @link cupsSetUser@ function need to do so in each thread for the same user | |
539 | * name to be used. | |
ef416fc2 | 540 | */ |
541 | ||
542 | const char * /* O - User name */ | |
543 | cupsUser(void) | |
544 | { | |
545 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ | |
546 | ||
547 | ||
548 | if (!cg->user[0]) | |
3e7fe0ca | 549 | _cupsSetDefaults(); |
ef416fc2 | 550 | |
551 | return (cg->user); | |
552 | } | |
553 | ||
554 | ||
db8b865d MS |
555 | /* |
556 | * 'cupsUserAgent()' - Return the default HTTP User-Agent string. | |
557 | * | |
8072030b | 558 | * @since CUPS 1.7/macOS 10.9@ |
db8b865d MS |
559 | */ |
560 | ||
561 | const char * /* O - User-Agent string */ | |
562 | cupsUserAgent(void) | |
563 | { | |
564 | _cups_globals_t *cg = _cupsGlobals(); /* Thread globals */ | |
565 | ||
566 | ||
567 | if (!cg->user_agent[0]) | |
568 | cupsSetUserAgent(NULL); | |
569 | ||
570 | return (cg->user_agent); | |
571 | } | |
572 | ||
573 | ||
ef416fc2 | 574 | /* |
575 | * '_cupsGetPassword()' - Get a password from the user. | |
576 | */ | |
577 | ||
dcb445bc | 578 | const char * /* O - Password or @code NULL@ if none */ |
ef416fc2 | 579 | _cupsGetPassword(const char *prompt) /* I - Prompt string */ |
580 | { | |
5a6b583a | 581 | #ifdef WIN32 |
dcb445bc MS |
582 | HANDLE tty; /* Console handle */ |
583 | DWORD mode; /* Console mode */ | |
584 | char passch, /* Current key press */ | |
585 | *passptr, /* Pointer into password string */ | |
586 | *passend; /* End of password string */ | |
12f89d24 | 587 | DWORD passbytes; /* Bytes read */ |
dcb445bc MS |
588 | _cups_globals_t *cg = _cupsGlobals(); |
589 | /* Thread globals */ | |
590 | ||
591 | ||
5a6b583a | 592 | /* |
dcb445bc | 593 | * Disable input echo and set raw input... |
5a6b583a MS |
594 | */ |
595 | ||
dcb445bc MS |
596 | if ((tty = GetStdHandle(STD_INPUT_HANDLE)) == INVALID_HANDLE_VALUE) |
597 | return (NULL); | |
598 | ||
599 | if (!GetConsoleMode(tty, &mode)) | |
600 | return (NULL); | |
601 | ||
602 | if (!SetConsoleMode(tty, 0)) | |
603 | return (NULL); | |
604 | ||
605 | /* | |
606 | * Display the prompt... | |
607 | */ | |
608 | ||
609 | printf("%s ", prompt); | |
610 | fflush(stdout); | |
611 | ||
612 | /* | |
613 | * Read the password string from /dev/tty until we get interrupted or get a | |
614 | * carriage return or newline... | |
615 | */ | |
616 | ||
617 | passptr = cg->password; | |
618 | passend = cg->password + sizeof(cg->password) - 1; | |
619 | ||
12f89d24 | 620 | while (ReadFile(tty, &passch, 1, &passbytes, NULL)) |
dcb445bc MS |
621 | { |
622 | if (passch == 0x0A || passch == 0x0D) | |
623 | { | |
624 | /* | |
625 | * Enter/return... | |
626 | */ | |
627 | ||
628 | break; | |
629 | } | |
630 | else if (passch == 0x08 || passch == 0x7F) | |
631 | { | |
632 | /* | |
633 | * Backspace/delete (erase character)... | |
634 | */ | |
635 | ||
636 | if (passptr > cg->password) | |
637 | { | |
638 | passptr --; | |
639 | fputs("\010 \010", stdout); | |
640 | } | |
641 | else | |
642 | putchar(0x07); | |
643 | } | |
644 | else if (passch == 0x15) | |
645 | { | |
646 | /* | |
647 | * CTRL+U (erase line) | |
648 | */ | |
649 | ||
650 | if (passptr > cg->password) | |
651 | { | |
652 | while (passptr > cg->password) | |
653 | { | |
654 | passptr --; | |
655 | fputs("\010 \010", stdout); | |
656 | } | |
657 | } | |
658 | else | |
659 | putchar(0x07); | |
660 | } | |
661 | else if (passch == 0x03) | |
662 | { | |
663 | /* | |
664 | * CTRL+C... | |
665 | */ | |
666 | ||
667 | passptr = cg->password; | |
668 | break; | |
669 | } | |
670 | else if ((passch & 255) < 0x20 || passptr >= passend) | |
671 | putchar(0x07); | |
672 | else | |
673 | { | |
674 | *passptr++ = passch; | |
675 | putchar(_CUPS_PASSCHAR); | |
676 | } | |
677 | ||
678 | fflush(stdout); | |
679 | } | |
680 | ||
681 | putchar('\n'); | |
682 | fflush(stdout); | |
683 | ||
684 | /* | |
685 | * Cleanup... | |
686 | */ | |
687 | ||
688 | SetConsoleMode(tty, mode); | |
689 | ||
690 | /* | |
691 | * Return the proper value... | |
692 | */ | |
693 | ||
694 | if (passbytes == 1 && passptr > cg->password) | |
695 | { | |
696 | *passptr = '\0'; | |
697 | return (cg->password); | |
698 | } | |
699 | else | |
700 | { | |
701 | memset(cg->password, 0, sizeof(cg->password)); | |
702 | return (NULL); | |
703 | } | |
5a6b583a MS |
704 | |
705 | #else | |
dcb445bc MS |
706 | int tty; /* /dev/tty - never read from stdin */ |
707 | struct termios original, /* Original input mode */ | |
708 | noecho; /* No echo input mode */ | |
709 | char passch, /* Current key press */ | |
710 | *passptr, /* Pointer into password string */ | |
711 | *passend; /* End of password string */ | |
712 | ssize_t passbytes; /* Bytes read */ | |
713 | _cups_globals_t *cg = _cupsGlobals(); | |
714 | /* Thread globals */ | |
715 | ||
716 | ||
5a6b583a | 717 | /* |
dcb445bc | 718 | * Disable input echo and set raw input... |
5a6b583a MS |
719 | */ |
720 | ||
dcb445bc MS |
721 | if ((tty = open("/dev/tty", O_RDONLY)) < 0) |
722 | return (NULL); | |
723 | ||
724 | if (tcgetattr(tty, &original)) | |
725 | { | |
726 | close(tty); | |
727 | return (NULL); | |
728 | } | |
729 | ||
730 | noecho = original; | |
7e86f2f6 | 731 | noecho.c_lflag &= (tcflag_t)~(ICANON | ECHO | ECHOE | ISIG); |
a8db9df8 MS |
732 | noecho.c_cc[VMIN] = 1; |
733 | noecho.c_cc[VTIME] = 0; | |
7cf5915e | 734 | |
dcb445bc MS |
735 | if (tcsetattr(tty, TCSAFLUSH, &noecho)) |
736 | { | |
737 | close(tty); | |
7cf5915e | 738 | return (NULL); |
dcb445bc MS |
739 | } |
740 | ||
741 | /* | |
742 | * Display the prompt... | |
743 | */ | |
744 | ||
745 | printf("%s ", prompt); | |
746 | fflush(stdout); | |
747 | ||
748 | /* | |
749 | * Read the password string from /dev/tty until we get interrupted or get a | |
750 | * carriage return or newline... | |
751 | */ | |
752 | ||
753 | passptr = cg->password; | |
754 | passend = cg->password + sizeof(cg->password) - 1; | |
755 | ||
756 | while ((passbytes = read(tty, &passch, 1)) == 1) | |
757 | { | |
8dd318e5 MS |
758 | if (passch == noecho.c_cc[VEOL] || |
759 | # ifdef VEOL2 | |
760 | passch == noecho.c_cc[VEOL2] || | |
761 | # endif /* VEOL2 */ | |
dcb445bc MS |
762 | passch == 0x0A || passch == 0x0D) |
763 | { | |
764 | /* | |
765 | * Enter/return... | |
766 | */ | |
767 | ||
768 | break; | |
769 | } | |
770 | else if (passch == noecho.c_cc[VERASE] || | |
771 | passch == 0x08 || passch == 0x7F) | |
772 | { | |
773 | /* | |
774 | * Backspace/delete (erase character)... | |
775 | */ | |
776 | ||
777 | if (passptr > cg->password) | |
778 | { | |
779 | passptr --; | |
780 | fputs("\010 \010", stdout); | |
781 | } | |
782 | else | |
783 | putchar(0x07); | |
784 | } | |
785 | else if (passch == noecho.c_cc[VKILL]) | |
786 | { | |
787 | /* | |
788 | * CTRL+U (erase line) | |
789 | */ | |
790 | ||
791 | if (passptr > cg->password) | |
792 | { | |
793 | while (passptr > cg->password) | |
794 | { | |
795 | passptr --; | |
796 | fputs("\010 \010", stdout); | |
797 | } | |
798 | } | |
799 | else | |
800 | putchar(0x07); | |
801 | } | |
802 | else if (passch == noecho.c_cc[VINTR] || passch == noecho.c_cc[VQUIT] || | |
803 | passch == noecho.c_cc[VEOF]) | |
804 | { | |
805 | /* | |
806 | * CTRL+C, CTRL+D, or CTRL+Z... | |
807 | */ | |
808 | ||
809 | passptr = cg->password; | |
810 | break; | |
811 | } | |
812 | else if ((passch & 255) < 0x20 || passptr >= passend) | |
813 | putchar(0x07); | |
814 | else | |
815 | { | |
816 | *passptr++ = passch; | |
817 | putchar(_CUPS_PASSCHAR); | |
818 | } | |
819 | ||
820 | fflush(stdout); | |
821 | } | |
822 | ||
823 | putchar('\n'); | |
824 | fflush(stdout); | |
825 | ||
826 | /* | |
827 | * Cleanup... | |
828 | */ | |
829 | ||
830 | tcsetattr(tty, TCSAFLUSH, &original); | |
831 | close(tty); | |
832 | ||
833 | /* | |
834 | * Return the proper value... | |
835 | */ | |
836 | ||
837 | if (passbytes == 1 && passptr > cg->password) | |
838 | { | |
839 | *passptr = '\0'; | |
840 | return (cg->password); | |
841 | } | |
7cf5915e | 842 | else |
dcb445bc MS |
843 | { |
844 | memset(cg->password, 0, sizeof(cg->password)); | |
845 | return (NULL); | |
846 | } | |
ef416fc2 | 847 | #endif /* WIN32 */ |
5a6b583a | 848 | } |
ef416fc2 | 849 | |
850 | ||
eac3a0a0 MS |
851 | #ifdef HAVE_GSSAPI |
852 | /* | |
853 | * '_cupsGSSServiceName()' - Get the GSS (Kerberos) service name. | |
854 | */ | |
855 | ||
856 | const char * | |
857 | _cupsGSSServiceName(void) | |
858 | { | |
859 | _cups_globals_t *cg = _cupsGlobals(); /* Thread globals */ | |
860 | ||
861 | ||
862 | if (!cg->gss_service_name[0]) | |
863 | _cupsSetDefaults(); | |
864 | ||
865 | return (cg->gss_service_name); | |
866 | } | |
867 | #endif /* HAVE_GSSAPI */ | |
868 | ||
869 | ||
ef416fc2 | 870 | /* |
e07d4801 | 871 | * '_cupsSetDefaults()' - Set the default server, port, and encryption. |
b423cd4c | 872 | */ |
873 | ||
e07d4801 MS |
874 | void |
875 | _cupsSetDefaults(void) | |
b423cd4c | 876 | { |
877 | cups_file_t *fp; /* File */ | |
3abb875b | 878 | const char *home; /* Home directory of user */ |
b423cd4c | 879 | char filename[1024]; /* Filename */ |
3abb875b | 880 | _cups_client_conf_t cc; /* client.conf values */ |
b423cd4c | 881 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ |
882 | ||
883 | ||
e07d4801 MS |
884 | DEBUG_puts("_cupsSetDefaults()"); |
885 | ||
886 | /* | |
3abb875b MS |
887 | * Load initial client.conf values... |
888 | */ | |
889 | ||
890 | cups_init_client_conf(&cc); | |
891 | ||
892 | /* | |
893 | * Read the /etc/cups/client.conf and ~/.cups/client.conf files, if | |
894 | * present. | |
895 | */ | |
896 | ||
897 | snprintf(filename, sizeof(filename), "%s/client.conf", cg->cups_serverroot); | |
898 | if ((fp = cupsFileOpen(filename, "r")) != NULL) | |
899 | { | |
900 | cups_read_client_conf(fp, &cc); | |
901 | cupsFileClose(fp); | |
902 | } | |
903 | ||
904 | # ifdef HAVE_GETEUID | |
905 | if ((geteuid() == getuid() || !getuid()) && getegid() == getgid() && (home = getenv("HOME")) != NULL) | |
906 | # elif !defined(WIN32) | |
907 | if (getuid() && (home = getenv("HOME")) != NULL) | |
908 | # else | |
909 | if ((home = getenv("HOME")) != NULL) | |
910 | # endif /* HAVE_GETEUID */ | |
911 | { | |
912 | /* | |
913 | * Look for ~/.cups/client.conf... | |
914 | */ | |
915 | ||
916 | snprintf(filename, sizeof(filename), "%s/.cups/client.conf", home); | |
917 | if ((fp = cupsFileOpen(filename, "r")) != NULL) | |
918 | { | |
919 | cups_read_client_conf(fp, &cc); | |
920 | cupsFileClose(fp); | |
921 | } | |
922 | } | |
923 | ||
924 | /* | |
925 | * Finalize things so every client.conf value is set... | |
e07d4801 MS |
926 | */ |
927 | ||
3abb875b MS |
928 | cups_finalize_client_conf(&cc); |
929 | ||
930 | if (cg->encryption == (http_encryption_t)-1) | |
931 | cg->encryption = cc.encryption; | |
932 | ||
933 | if (!cg->server[0] || !cg->ipp_port) | |
934 | cupsSetServer(cc.server_name); | |
935 | ||
936 | if (!cg->ipp_port) | |
4b9daaf4 | 937 | cups_set_default_ipp_port(cg); |
3abb875b MS |
938 | |
939 | if (!cg->user[0]) | |
940 | strlcpy(cg->user, cc.user, sizeof(cg->user)); | |
941 | ||
942 | #ifdef HAVE_GSSAPI | |
943 | if (!cg->gss_service_name[0]) | |
944 | strlcpy(cg->gss_service_name, cc.gss_service_name, sizeof(cg->gss_service_name)); | |
945 | #endif /* HAVE_GSSAPI */ | |
946 | ||
08d56b1f MS |
947 | if (cg->trust_first < 0) |
948 | cg->trust_first = cc.trust_first; | |
949 | ||
3abb875b MS |
950 | if (cg->any_root < 0) |
951 | cg->any_root = cc.any_root; | |
952 | ||
953 | if (cg->expired_certs < 0) | |
954 | cg->expired_certs = cc.expired_certs; | |
955 | ||
956 | if (cg->validate_certs < 0) | |
957 | cg->validate_certs = cc.validate_certs; | |
958 | ||
959 | #ifdef HAVE_SSL | |
960 | _httpTLSSetOptions(cc.ssl_options); | |
961 | #endif /* HAVE_SSL */ | |
962 | } | |
963 | ||
964 | ||
08d56b1f MS |
965 | #ifdef __APPLE__ |
966 | /* | |
967 | * 'cups_apple_get_boolean()' - Get a boolean setting from the CUPS preferences. | |
968 | */ | |
969 | ||
970 | static int /* O - 1 if set, 0 otherwise */ | |
971 | cups_apple_get_boolean( | |
972 | CFStringRef key, /* I - Key (name) */ | |
973 | int *value) /* O - Boolean value */ | |
974 | { | |
975 | Boolean bval, /* Preference value */ | |
976 | bval_set; /* Value is set? */ | |
977 | ||
978 | ||
979 | bval = CFPreferencesGetAppBooleanValue(key, kCUPSPrintingPrefs, &bval_set); | |
980 | ||
981 | if (bval_set) | |
982 | *value = (int)bval; | |
983 | ||
984 | return ((int)bval_set); | |
985 | } | |
986 | ||
987 | ||
988 | /* | |
989 | * 'cups_apple_get_string()' - Get a string setting from the CUPS preferences. | |
990 | */ | |
991 | ||
992 | static int /* O - 1 if set, 0 otherwise */ | |
993 | cups_apple_get_string( | |
994 | CFStringRef key, /* I - Key (name) */ | |
995 | char *value, /* O - String value */ | |
996 | size_t valsize) /* I - Size of value buffer */ | |
997 | { | |
998 | CFStringRef sval; /* String value */ | |
999 | ||
1000 | ||
1001 | if ((sval = CFPreferencesCopyAppValue(key, kCUPSPrintingPrefs)) != NULL) | |
1002 | { | |
1003 | Boolean result = CFStringGetCString(sval, value, (CFIndex)valsize, kCFStringEncodingUTF8); | |
1004 | ||
1005 | CFRelease(sval); | |
1006 | ||
1007 | if (result) | |
1008 | return (1); | |
1009 | } | |
1010 | ||
1011 | return (0); | |
1012 | } | |
1013 | #endif /* __APPLE__ */ | |
1014 | ||
1015 | ||
3abb875b MS |
1016 | /* |
1017 | * 'cups_boolean_value()' - Convert a string to a boolean value. | |
1018 | */ | |
1019 | ||
1020 | static int /* O - Boolean value */ | |
1021 | cups_boolean_value(const char *value) /* I - String value */ | |
1022 | { | |
1023 | return (!_cups_strcasecmp(value, "yes") || !_cups_strcasecmp(value, "on") || !_cups_strcasecmp(value, "true")); | |
1024 | } | |
1025 | ||
1026 | ||
1027 | /* | |
1028 | * 'cups_finalize_client_conf()' - Finalize client.conf values. | |
1029 | */ | |
1030 | ||
1031 | static void | |
1032 | cups_finalize_client_conf( | |
1033 | _cups_client_conf_t *cc) /* I - client.conf values */ | |
1034 | { | |
1035 | const char *value; /* Environment variable */ | |
1036 | ||
1037 | ||
08d56b1f MS |
1038 | if ((value = getenv("CUPS_TRUSTFIRST")) != NULL) |
1039 | cc->trust_first = cups_boolean_value(value); | |
1040 | ||
3abb875b MS |
1041 | if ((value = getenv("CUPS_ANYROOT")) != NULL) |
1042 | cc->any_root = cups_boolean_value(value); | |
1043 | ||
1044 | if ((value = getenv("CUPS_ENCRYPTION")) != NULL) | |
1045 | cups_set_encryption(cc, value); | |
1046 | ||
1047 | if ((value = getenv("CUPS_EXPIREDCERTS")) != NULL) | |
1048 | cc->expired_certs = cups_boolean_value(value); | |
1049 | ||
07ed0e9a | 1050 | #ifdef HAVE_GSSAPI |
3abb875b MS |
1051 | if ((value = getenv("CUPS_GSSSERVICENAME")) != NULL) |
1052 | cups_set_gss_service_name(cc, value); | |
07ed0e9a | 1053 | #endif /* HAVE_GSSAPI */ |
3abb875b MS |
1054 | |
1055 | if ((value = getenv("CUPS_SERVER")) != NULL) | |
1056 | cups_set_server_name(cc, value); | |
1057 | ||
1058 | if ((value = getenv("CUPS_USER")) != NULL) | |
1059 | cups_set_user(cc, value); | |
1060 | ||
1061 | if ((value = getenv("CUPS_VALIDATECERTS")) != NULL) | |
1062 | cc->validate_certs = cups_boolean_value(value); | |
e07d4801 MS |
1063 | |
1064 | /* | |
3abb875b | 1065 | * Then apply defaults for those values that haven't been set... |
e07d4801 MS |
1066 | */ |
1067 | ||
08d56b1f MS |
1068 | if (cc->trust_first < 0) |
1069 | cc->trust_first = 1; | |
1070 | ||
3abb875b MS |
1071 | if (cc->any_root < 0) |
1072 | cc->any_root = 1; | |
1073 | ||
1074 | if (cc->encryption == (http_encryption_t)-1) | |
1075 | cc->encryption = HTTP_ENCRYPTION_IF_REQUESTED; | |
1076 | ||
1077 | if (cc->expired_certs < 0) | |
08d56b1f | 1078 | cc->expired_certs = 0; |
3abb875b MS |
1079 | |
1080 | #ifdef HAVE_GSSAPI | |
1081 | if (!cc->gss_service_name[0]) | |
1082 | cups_set_gss_service_name(cc, CUPS_DEFAULT_GSSSERVICENAME); | |
1083 | #endif /* HAVE_GSSAPI */ | |
1084 | ||
1085 | if (!cc->server_name[0]) | |
e07d4801 | 1086 | { |
3abb875b | 1087 | #ifdef CUPS_DEFAULT_DOMAINSOCKET |
63aefcd5 | 1088 | /* |
3abb875b MS |
1089 | * If we are compiled with domain socket support, only use the |
1090 | * domain socket if it exists and has the right permissions... | |
63aefcd5 MS |
1091 | */ |
1092 | ||
89b7fd55 | 1093 | if (!access(CUPS_DEFAULT_DOMAINSOCKET, R_OK)) |
3abb875b MS |
1094 | cups_set_server_name(cc, CUPS_DEFAULT_DOMAINSOCKET); |
1095 | else | |
1096 | #endif /* CUPS_DEFAULT_DOMAINSOCKET */ | |
1097 | cups_set_server_name(cc, "localhost"); | |
1098 | } | |
63aefcd5 | 1099 | |
3abb875b MS |
1100 | if (!cc->user[0]) |
1101 | { | |
1102 | #ifdef WIN32 | |
63aefcd5 | 1103 | /* |
3abb875b | 1104 | * Get the current user name from the OS... |
63aefcd5 MS |
1105 | */ |
1106 | ||
3abb875b | 1107 | DWORD size; /* Size of string */ |
63aefcd5 | 1108 | |
3abb875b MS |
1109 | size = sizeof(cc->user); |
1110 | if (!GetUserName(cc->user, &size)) | |
1111 | #else | |
63aefcd5 | 1112 | /* |
3abb875b | 1113 | * Try the USER environment variable as the default username... |
63aefcd5 MS |
1114 | */ |
1115 | ||
3abb875b MS |
1116 | const char *envuser = getenv("USER"); |
1117 | /* Default username */ | |
1118 | struct passwd *pw = NULL; /* Account information */ | |
1119 | ||
1120 | if (envuser) | |
d09495fa | 1121 | { |
10d09e33 | 1122 | /* |
3abb875b MS |
1123 | * Validate USER matches the current UID, otherwise don't allow it to |
1124 | * override things... This makes sure that printing after doing su | |
1125 | * or sudo records the correct username. | |
10d09e33 | 1126 | */ |
e07d4801 | 1127 | |
3abb875b MS |
1128 | if ((pw = getpwnam(envuser)) != NULL && pw->pw_uid != getuid()) |
1129 | pw = NULL; | |
1130 | } | |
1131 | ||
1132 | if (!pw) | |
1133 | pw = getpwuid(getuid()); | |
e07d4801 | 1134 | |
3abb875b MS |
1135 | if (pw) |
1136 | strlcpy(cc->user, pw->pw_name, sizeof(cc->user)); | |
1137 | else | |
1138 | #endif /* WIN32 */ | |
1139 | { | |
e07d4801 | 1140 | /* |
3abb875b | 1141 | * Use the default "unknown" user name... |
e07d4801 MS |
1142 | */ |
1143 | ||
3abb875b | 1144 | strlcpy(cc->user, "unknown", sizeof(cc->user)); |
63aefcd5 | 1145 | } |
e07d4801 | 1146 | } |
3abb875b MS |
1147 | |
1148 | if (cc->validate_certs < 0) | |
1149 | cc->validate_certs = 0; | |
1150 | } | |
1151 | ||
1152 | ||
1153 | /* | |
1154 | * 'cups_init_client_conf()' - Initialize client.conf values. | |
1155 | */ | |
1156 | ||
1157 | static void | |
1158 | cups_init_client_conf( | |
1159 | _cups_client_conf_t *cc) /* I - client.conf values */ | |
1160 | { | |
1161 | /* | |
1162 | * Clear all values to "not set"... | |
1163 | */ | |
1164 | ||
1165 | memset(cc, 0, sizeof(_cups_client_conf_t)); | |
1166 | ||
1167 | cc->encryption = (http_encryption_t)-1; | |
08d56b1f | 1168 | cc->trust_first = -1; |
3abb875b MS |
1169 | cc->any_root = -1; |
1170 | cc->expired_certs = -1; | |
1171 | cc->validate_certs = -1; | |
08d56b1f MS |
1172 | |
1173 | /* | |
1174 | * Load settings from the org.cups.PrintingPrefs plist (which trump | |
1175 | * everything...) | |
1176 | */ | |
1177 | ||
1178 | #ifdef __APPLE__ | |
1179 | char sval[1024]; /* String value */ | |
1180 | int bval; /* Boolean value */ | |
1181 | ||
1182 | if (cups_apple_get_boolean(kAllowAnyRootKey, &bval)) | |
1183 | cc->any_root = bval; | |
1184 | ||
1185 | if (cups_apple_get_boolean(kAllowExpiredCertsKey, &bval)) | |
1186 | cc->expired_certs = bval; | |
1187 | ||
1188 | if (cups_apple_get_string(kEncryptionKey, sval, sizeof(sval))) | |
1189 | cups_set_encryption(cc, sval); | |
1190 | ||
1191 | if (cups_apple_get_string(kSSLOptionsKey, sval, sizeof(sval))) | |
1192 | cups_set_ssl_options(cc, sval); | |
1193 | ||
1194 | if (cups_apple_get_boolean(kTrustOnFirstUseKey, &bval)) | |
1195 | cc->trust_first = bval; | |
1196 | ||
1197 | if (cups_apple_get_boolean(kValidateCertsKey, &bval)) | |
1198 | cc->validate_certs = bval; | |
1199 | #endif /* __APPLE__ */ | |
e07d4801 MS |
1200 | } |
1201 | ||
1202 | ||
1203 | /* | |
1204 | * 'cups_read_client_conf()' - Read a client.conf file. | |
1205 | */ | |
1206 | ||
1207 | static void | |
1208 | cups_read_client_conf( | |
3abb875b MS |
1209 | cups_file_t *fp, /* I - File to read */ |
1210 | _cups_client_conf_t *cc) /* I - client.conf values */ | |
e07d4801 MS |
1211 | { |
1212 | int linenum; /* Current line number */ | |
1213 | char line[1024], /* Line from file */ | |
3abb875b | 1214 | *value; /* Pointer into line */ |
e07d4801 MS |
1215 | |
1216 | ||
1217 | /* | |
1218 | * Read from the file... | |
1219 | */ | |
1220 | ||
1221 | linenum = 0; | |
1222 | while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum)) | |
1223 | { | |
3abb875b MS |
1224 | if (!_cups_strcasecmp(line, "Encryption") && value) |
1225 | cups_set_encryption(cc, value); | |
85dda01c MS |
1226 | #ifndef __APPLE__ |
1227 | /* | |
8072030b | 1228 | * The ServerName directive is not supported on macOS due to app |
3abb875b | 1229 | * sandboxing restrictions, i.e. not all apps request network access. |
85dda01c | 1230 | */ |
3abb875b MS |
1231 | else if (!_cups_strcasecmp(line, "ServerName") && value) |
1232 | cups_set_server_name(cc, value); | |
85dda01c | 1233 | #endif /* !__APPLE__ */ |
3abb875b MS |
1234 | else if (!_cups_strcasecmp(line, "User") && value) |
1235 | cups_set_user(cc, value); | |
08d56b1f MS |
1236 | else if (!_cups_strcasecmp(line, "TrustOnFirstUse") && value) |
1237 | cc->trust_first = cups_boolean_value(value); | |
3abb875b MS |
1238 | else if (!_cups_strcasecmp(line, "AllowAnyRoot") && value) |
1239 | cc->any_root = cups_boolean_value(value); | |
1240 | else if (!_cups_strcasecmp(line, "AllowExpiredCerts") && | |
7cf5915e | 1241 | value) |
3abb875b MS |
1242 | cc->expired_certs = cups_boolean_value(value); |
1243 | else if (!_cups_strcasecmp(line, "ValidateCerts") && value) | |
1244 | cc->validate_certs = cups_boolean_value(value); | |
07ed0e9a | 1245 | #ifdef HAVE_GSSAPI |
3abb875b MS |
1246 | else if (!_cups_strcasecmp(line, "GSSServiceName") && value) |
1247 | cups_set_gss_service_name(cc, value); | |
07ed0e9a | 1248 | #endif /* HAVE_GSSAPI */ |
22ebb7d0 | 1249 | #ifdef HAVE_SSL |
3abb875b MS |
1250 | else if (!_cups_strcasecmp(line, "SSLOptions") && value) |
1251 | cups_set_ssl_options(cc, value); | |
1252 | #endif /* HAVE_SSL */ | |
1253 | } | |
1254 | } | |
63aefcd5 | 1255 | |
63aefcd5 | 1256 | |
4b9daaf4 MS |
1257 | /* |
1258 | * 'cups_set_default_ipp_port()' - Set the default IPP port value. | |
1259 | */ | |
1260 | ||
1261 | static void | |
1262 | cups_set_default_ipp_port( | |
1263 | _cups_globals_t *cg) /* I - Global data */ | |
1264 | { | |
1265 | const char *ipp_port; /* IPP_PORT environment variable */ | |
1266 | ||
1267 | ||
1268 | if ((ipp_port = getenv("IPP_PORT")) != NULL) | |
1269 | { | |
1270 | if ((cg->ipp_port = atoi(ipp_port)) <= 0) | |
1271 | cg->ipp_port = CUPS_DEFAULT_IPP_PORT; | |
1272 | } | |
1273 | else | |
1274 | cg->ipp_port = CUPS_DEFAULT_IPP_PORT; | |
1275 | } | |
1276 | ||
3abb875b MS |
1277 | /* |
1278 | * 'cups_set_encryption()' - Set the Encryption value. | |
1279 | */ | |
63aefcd5 | 1280 | |
3abb875b MS |
1281 | static void |
1282 | cups_set_encryption( | |
1283 | _cups_client_conf_t *cc, /* I - client.conf values */ | |
1284 | const char *value) /* I - Value */ | |
1285 | { | |
1286 | if (!_cups_strcasecmp(value, "never")) | |
1287 | cc->encryption = HTTP_ENCRYPTION_NEVER; | |
1288 | else if (!_cups_strcasecmp(value, "always")) | |
1289 | cc->encryption = HTTP_ENCRYPTION_ALWAYS; | |
1290 | else if (!_cups_strcasecmp(value, "required")) | |
1291 | cc->encryption = HTTP_ENCRYPTION_REQUIRED; | |
1292 | else | |
1293 | cc->encryption = HTTP_ENCRYPTION_IF_REQUESTED; | |
1294 | } | |
e07d4801 | 1295 | |
e07d4801 | 1296 | |
3abb875b MS |
1297 | /* |
1298 | * 'cups_set_gss_service_name()' - Set the GSSServiceName value. | |
1299 | */ | |
b423cd4c | 1300 | |
3abb875b MS |
1301 | #ifdef HAVE_GSSAPI |
1302 | static void | |
1303 | cups_set_gss_service_name( | |
1304 | _cups_client_conf_t *cc, /* I - client.conf values */ | |
1305 | const char *value) /* I - Value */ | |
1306 | { | |
1307 | strlcpy(cc->gss_service_name, value, sizeof(cc->gss_service_name)); | |
1308 | } | |
1309 | #endif /* HAVE_GSSAPI */ | |
7cf5915e | 1310 | |
10d09e33 | 1311 | |
3abb875b MS |
1312 | /* |
1313 | * 'cups_set_server_name()' - Set the ServerName value. | |
1314 | */ | |
10d09e33 | 1315 | |
3abb875b MS |
1316 | static void |
1317 | cups_set_server_name( | |
1318 | _cups_client_conf_t *cc, /* I - client.conf values */ | |
1319 | const char *value) /* I - Value */ | |
1320 | { | |
1321 | strlcpy(cc->server_name, value, sizeof(cc->server_name)); | |
1322 | } | |
10d09e33 | 1323 | |
10d09e33 | 1324 | |
3abb875b MS |
1325 | /* |
1326 | * 'cups_set_ssl_options()' - Set the SSLOptions value. | |
1327 | */ | |
10d09e33 | 1328 | |
3abb875b MS |
1329 | #ifdef HAVE_SSL |
1330 | static void | |
1331 | cups_set_ssl_options( | |
1332 | _cups_client_conf_t *cc, /* I - client.conf values */ | |
1333 | const char *value) /* I - Value */ | |
1334 | { | |
1335 | /* | |
ee6226a5 | 1336 | * SSLOptions [AllowRC4] [AllowSSL3] [AllowDH] [DenyTLS1.0] [None] |
3abb875b | 1337 | */ |
10d09e33 | 1338 | |
ee6226a5 | 1339 | int options = _HTTP_TLS_NONE; /* SSL/TLS options */ |
3abb875b MS |
1340 | char temp[256], /* Copy of value */ |
1341 | *start, /* Start of option */ | |
1342 | *end; /* End of option */ | |
3e7fe0ca | 1343 | |
3e7fe0ca | 1344 | |
3abb875b | 1345 | strlcpy(temp, value, sizeof(temp)); |
3e7fe0ca | 1346 | |
3abb875b MS |
1347 | for (start = temp; *start; start = end) |
1348 | { | |
a8db9df8 | 1349 | /* |
3abb875b MS |
1350 | * Find end of keyword... |
1351 | */ | |
3e7fe0ca | 1352 | |
3abb875b MS |
1353 | end = start; |
1354 | while (*end && !_cups_isspace(*end)) | |
1355 | end ++; | |
93e3d3f5 | 1356 | |
3abb875b MS |
1357 | if (*end) |
1358 | *end++ = '\0'; | |
93e3d3f5 | 1359 | |
3abb875b MS |
1360 | /* |
1361 | * Compare... | |
1362 | */ | |
3e7fe0ca | 1363 | |
3abb875b MS |
1364 | if (!_cups_strcasecmp(start, "AllowRC4")) |
1365 | options |= _HTTP_TLS_ALLOW_RC4; | |
1366 | else if (!_cups_strcasecmp(start, "AllowSSL3")) | |
1367 | options |= _HTTP_TLS_ALLOW_SSL3; | |
ee6226a5 MS |
1368 | else if (!_cups_strcasecmp(start, "AllowDH")) |
1369 | options |= _HTTP_TLS_ALLOW_DH; | |
1370 | else if (!_cups_strcasecmp(start, "DenyTLS1.0")) | |
1371 | options |= _HTTP_TLS_DENY_TLS10; | |
3abb875b | 1372 | else if (!_cups_strcasecmp(start, "None")) |
ee6226a5 | 1373 | options = _HTTP_TLS_NONE; |
3e7fe0ca MS |
1374 | } |
1375 | ||
3abb875b | 1376 | cc->ssl_options = options; |
b37d45d9 | 1377 | |
807315e6 | 1378 | DEBUG_printf(("4cups_set_ssl_options(cc=%p, value=\"%s\") options=%x", (void *)cc, value, options)); |
3abb875b MS |
1379 | } |
1380 | #endif /* HAVE_SSL */ | |
07ed0e9a | 1381 | |
7cf5915e | 1382 | |
3abb875b MS |
1383 | /* |
1384 | * 'cups_set_user()' - Set the User value. | |
1385 | */ | |
f51f3773 | 1386 | |
3abb875b MS |
1387 | static void |
1388 | cups_set_user( | |
1389 | _cups_client_conf_t *cc, /* I - client.conf values */ | |
1390 | const char *value) /* I - Value */ | |
1391 | { | |
1392 | strlcpy(cc->user, value, sizeof(cc->user)); | |
b423cd4c | 1393 | } |