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