]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/usersys.c
Fix servername/version=1.1 in client.conf (STR #4291)
[thirdparty/cups.git] / cups / usersys.c
CommitLineData
6b67a15e 1/*
c9d3f842 2 * "$Id$"
6b67a15e 3 *
74a64efc 4 * User, system, and password routines for CUPS.
6b67a15e 5 *
c35095b6 6 * Copyright 2007-2013 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 *
c35095b6 19 * cupsEncryption() - Get the current encryption settings.
20 * cupsGetPassword() - Get a password from the user.
74a64efc 21 * cupsGetPassword2() - Get a password from the user using the advanced
c35095b6 22 * password callback.
23 * cupsServer() - Return the hostname/address of the current
24 * server.
b9738d7c 25 * cupsSetClientCertCB() - Set the client certificate callback.
c35095b6 26 * cupsSetCredentials() - Set the default credentials to be used for
27 * SSL/TLS connections.
feb27103 28 * cupsSetEncryption() - Set the encryption preference.
29 * cupsSetPasswordCB() - Set the password callback for CUPS.
c890e5ea 30 * cupsSetPasswordCB2() - Set the advanced password callback for CUPS.
c35095b6 31 * cupsSetServer() - Set the default server name and port.
b9738d7c 32 * cupsSetServerCertCB() - Set the server certificate callback.
c35095b6 33 * cupsSetUser() - Set the default user name.
34 * cupsSetUserAgent() - Set the default HTTP User-Agent string.
35 * cupsUser() - Return the current user's name.
36 * cupsUserAgent() - Return the default HTTP User-Agent string.
feb27103 37 * _cupsGetPassword() - Get a password from the user.
1b26e418 38 * _cupsGSSServiceName() - Get the GSS (Kerberos) service name.
d4c4cd54 39 * _cupsSetDefaults() - Set the default server, port, and encryption.
40 * cups_read_client_conf() - Read a client.conf file.
6b67a15e 41 */
42
43/*
44 * Include necessary headers...
45 */
46
3d94661a 47#include "cups-private.h"
6b67a15e 48#include <stdlib.h>
9c9584a8 49#include <sys/stat.h>
e335a094 50#ifdef WIN32
51# include <windows.h>
74a64efc 52#else
53# include <pwd.h>
24e92ab1 54# include <termios.h>
c35095b6 55# include <sys/utsname.h>
e335a094 56#endif /* WIN32 */
57
6b67a15e 58
24e92ab1 59/*
60 * Local constants...
61 */
62
63#define _CUPS_PASSCHAR '*' /* Character that is echoed for password */
64
65
feb27103 66/*
67 * Local functions...
68 */
69
d4c4cd54 70static void cups_read_client_conf(cups_file_t *fp,
71 _cups_globals_t *cg,
72 const char *cups_encryption,
b9738d7c 73 const char *cups_server,
1d81a2a2 74 const char *cups_user,
9200f229 75#ifdef HAVE_GSSAPI
76 const char *cups_gssservicename,
77#endif /* HAVE_GSSAPI */
b9738d7c 78 const char *cups_anyroot,
79 const char *cups_expiredroot,
80 const char *cups_expiredcerts);
feb27103 81
82
2a0ef17a 83/*
74a64efc 84 * 'cupsEncryption()' - Get the current encryption settings.
f82d9dea 85 *
86 * The default encryption setting comes from the CUPS_ENCRYPTION
c73f649f 87 * environment variable, then the ~/.cups/client.conf file, and finally the
f82d9dea 88 * /etc/cups/client.conf file. If not set, the default is
d6ff282a 89 * @code HTTP_ENCRYPTION_IF_REQUESTED@.
74a64efc 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.
2a0ef17a 95 */
96
0341722a 97http_encryption_t /* O - Encryption settings */
2a0ef17a 98cupsEncryption(void)
99{
2625096f 100 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
2a0ef17a 101
102
03f61bf3 103 if (cg->encryption == (http_encryption_t)-1)
d4c4cd54 104 _cupsSetDefaults();
2a0ef17a 105
03f61bf3 106 return (cg->encryption);
2a0ef17a 107}
108
109
6b67a15e 110/*
f82d9dea 111 * 'cupsGetPassword()' - Get a password from the user.
112 *
8168bc47 113 * Uses the current password callback function. Returns @code NULL@ if the
7fbf7fc4 114 * user does not provide a password.
74a64efc 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.
6b67a15e 120 */
121
063e1ac7 122const char * /* O - Password */
6b67a15e 123cupsGetPassword(const char *prompt) /* I - Prompt string */
124{
c890e5ea 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
74a64efc 134 * password callback.
c890e5ea 135 *
136 * Uses the current password callback function. Returns @code NULL@ if the
137 * user does not provide a password.
138 *
74a64efc 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 *
c45401bb 144 * @since CUPS 1.4/OS X 10.6@
c890e5ea 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));
6b67a15e 160}
6b67a15e 161
6b67a15e 162
163/*
74a64efc 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.
f82d9dea 170 *
74a64efc 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.
6b67a15e 178 */
179
c68b6ae8 180const char * /* O - Server name */
181cupsServer(void)
6b67a15e 182{
2625096f 183 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
6b67a15e 184
185
03f61bf3 186 if (!cg->server[0])
d4c4cd54 187 _cupsSetDefaults();
6b67a15e 188
d4c4cd54 189 return (cg->server);
190}
c68b6ae8 191
80294044 192
b9738d7c 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 *
c45401bb 202 * @since CUPS 1.5/OS X 10.7@
b9738d7c 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 *
c45401bb 226 * @since CUPS 1.5/OS X 10.7@
b9738d7c 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
239 _httpFreeCredentials(cg->tls_credentials);
4fee0aad 240 cg->tls_credentials = _httpCreateCredentials(credentials);
b9738d7c 241
242 return (cg->tls_credentials ? 0 : -1);
243}
244
245
d4c4cd54 246/*
247 * 'cupsSetEncryption()' - Set the encryption preference.
74a64efc 248 *
249 * The default encryption setting comes from the CUPS_ENCRYPTION
250 * environment variable, then the ~/.cups/client.conf file, and finally the
251 * /etc/cups/client.conf file. If not set, the default is
d6ff282a 252 * @code HTTP_ENCRYPTION_IF_REQUESTED@.
74a64efc 253 *
254 * Note: The current encryption setting is tracked separately for each thread
255 * in a program. Multi-threaded programs that override the setting need to do
256 * so in each thread for the same setting to be used.
d4c4cd54 257 */
c6075312 258
d4c4cd54 259void
260cupsSetEncryption(http_encryption_t e) /* I - New encryption preference */
261{
262 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
1fcc120e 263
c94f4aa9 264
d4c4cd54 265 cg->encryption = e;
c68b6ae8 266
d4c4cd54 267 if (cg->http)
268 httpEncryption(cg->http, e);
6b67a15e 269}
270
271
272/*
c68b6ae8 273 * 'cupsSetPasswordCB()' - Set the password callback for CUPS.
f82d9dea 274 *
74a64efc 275 * Pass @code NULL@ to restore the default (console) password callback, which
276 * reads the password from the console. Programs should call either this
277 * function or @link cupsSetPasswordCB2@, as only one callback can be registered
278 * by a program per thread.
279 *
280 * Note: The current password callback is tracked separately for each thread
281 * in a program. Multi-threaded programs that override the callback need to do
282 * so in each thread for the same callback to be used.
6b67a15e 283 */
284
c68b6ae8 285void
f82d9dea 286cupsSetPasswordCB(cups_password_cb_t cb)/* I - Callback function */
6b67a15e 287{
2625096f 288 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
03f61bf3 289
290
c890e5ea 291 if (cb == (cups_password_cb_t)0)
292 cg->password_cb = (cups_password_cb2_t)_cupsGetPassword;
293 else
294 cg->password_cb = (cups_password_cb2_t)cb;
295
296 cg->password_data = NULL;
297}
298
299
300/*
301 * 'cupsSetPasswordCB2()' - Set the advanced password callback for CUPS.
302 *
74a64efc 303 * Pass @code NULL@ to restore the default (console) password callback, which
304 * reads the password from the console. Programs should call either this
305 * function or @link cupsSetPasswordCB2@, as only one callback can be registered
306 * by a program per thread.
307 *
308 * Note: The current password callback is tracked separately for each thread
309 * in a program. Multi-threaded programs that override the callback need to do
310 * so in each thread for the same callback to be used.
c890e5ea 311 *
c45401bb 312 * @since CUPS 1.4/OS X 10.6@
c890e5ea 313 */
314
315void
316cupsSetPasswordCB2(
317 cups_password_cb2_t cb, /* I - Callback function */
318 void *user_data) /* I - User data pointer */
319{
320 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
321
322
323 if (cb == (cups_password_cb2_t)0)
324 cg->password_cb = (cups_password_cb2_t)_cupsGetPassword;
c68b6ae8 325 else
03f61bf3 326 cg->password_cb = cb;
c890e5ea 327
328 cg->password_data = user_data;
6b67a15e 329}
6b67a15e 330
331
332/*
74a64efc 333 * 'cupsSetServer()' - Set the default server name and port.
f82d9dea 334 *
335 * The "server" string can be a fully-qualified hostname, a numeric
74a64efc 336 * IPv4 or IPv6 address, or a domain socket pathname. Hostnames and numeric IP
337 * addresses can be optionally followed by a colon and port number to override
338 * the default port 631, e.g. "hostname:8631". Pass @code NULL@ to restore the
339 * default server name and port.
340 *
341 * Note: The current server is tracked separately for each thread in a program.
342 * Multi-threaded programs that override the server need to do so in each
343 * thread for the same server to be used.
6b67a15e 344 */
345
c68b6ae8 346void
347cupsSetServer(const char *server) /* I - Server name */
6b67a15e 348{
821fdb51 349 char *options, /* Options */
350 *port; /* Pointer to port */
2625096f 351 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
03f61bf3 352
353
c68b6ae8 354 if (server)
c94f4aa9 355 {
03f61bf3 356 strlcpy(cg->server, server, sizeof(cg->server));
c94f4aa9 357
821fdb51 358 if (cg->server[0] != '/' && (options = strrchr(cg->server, '/')) != NULL)
359 {
360 *options++ = '\0';
361
362 if (!strcmp(options, "version=1.0"))
363 cg->server_version = 10;
364 else if (!strcmp(options, "version=1.1"))
365 cg->server_version = 11;
366 else if (!strcmp(options, "version=2.0"))
367 cg->server_version = 20;
368 else if (!strcmp(options, "version=2.1"))
369 cg->server_version = 21;
370 else if (!strcmp(options, "version=2.2"))
371 cg->server_version = 22;
372 }
373
c94f4aa9 374 if (cg->server[0] != '/' && (port = strrchr(cg->server, ':')) != NULL &&
375 !strchr(port, ']') && isdigit(port[1] & 255))
376 {
377 *port++ = '\0';
378
d4c4cd54 379 cg->ipp_port = atoi(port);
c94f4aa9 380 }
381
382 if (cg->server[0] == '/')
af882006 383 strlcpy(cg->servername, "localhost", sizeof(cg->servername));
c94f4aa9 384 else
385 strlcpy(cg->servername, cg->server, sizeof(cg->servername));
386 }
c68b6ae8 387 else
c94f4aa9 388 {
821fdb51 389 cg->server[0] = '\0';
390 cg->servername[0] = '\0';
391 cg->server_version = 20;
c94f4aa9 392 }
7f826886 393
394 if (cg->http)
395 {
396 httpClose(cg->http);
397 cg->http = NULL;
398 }
c68b6ae8 399}
6b67a15e 400
6b67a15e 401
b9738d7c 402/*
403 * 'cupsSetServerCertCB()' - Set the server certificate callback.
404 *
405 * Pass @code NULL@ to restore the default callback.
406 *
407 * Note: The current credentials callback is tracked separately for each thread
408 * in a program. Multi-threaded programs that override the callback need to do
409 * so in each thread for the same callback to be used.
410 *
c45401bb 411 * @since CUPS 1.5/OS X 10.7@
b9738d7c 412 */
413
414void
415cupsSetServerCertCB(
416 cups_server_cert_cb_t cb, /* I - Callback function */
417 void *user_data) /* I - User data pointer */
418{
419 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
420
421
422 cg->server_cert_cb = cb;
423 cg->server_cert_data = user_data;
424}
425
426
c68b6ae8 427/*
f82d9dea 428 * 'cupsSetUser()' - Set the default user name.
429 *
8168bc47 430 * Pass @code NULL@ to restore the default user name.
74a64efc 431 *
432 * Note: The current user name is tracked separately for each thread in a
433 * program. Multi-threaded programs that override the user name need to do so
434 * in each thread for the same user name to be used.
c68b6ae8 435 */
6b67a15e 436
c68b6ae8 437void
438cupsSetUser(const char *user) /* I - User name */
439{
2625096f 440 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
03f61bf3 441
442
c68b6ae8 443 if (user)
03f61bf3 444 strlcpy(cg->user, user, sizeof(cg->user));
caa58f49 445 else
03f61bf3 446 cg->user[0] = '\0';
c68b6ae8 447}
448
449
c35095b6 450/*
451 * 'cupsSetUserAgent()' - Set the default HTTP User-Agent string.
452 *
453 * Setting the string to NULL forces the default value containing the CUPS
454 * version, IPP version, and operating system version and architecture.
455 *
456 * @since CUPS 1.7@
457 */
458
459void
460cupsSetUserAgent(const char *user_agent)/* I - User-Agent string or @code NULL@ */
461{
462 _cups_globals_t *cg = _cupsGlobals();
463 /* Thread globals */
464#ifdef WIN32
353faf12 465 SYSTEM_INFO sysinfo; /* System information */
466 OSVERSIONINFO version; /* OS version info */
c35095b6 467#else
468 struct utsname name; /* uname info */
469#endif /* WIN32 */
470
471
472 if (user_agent)
473 {
474 strlcpy(cg->user_agent, user_agent, sizeof(cg->user_agent));
475 return;
476 }
477
478#ifdef WIN32
353faf12 479 version.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
480 GetVersionEx(&version);
c35095b6 481 GetNativeSystemInfo(&sysinfo);
482
483 snprintf(cg->user_agent, sizeof(cg->user_agent),
484 CUPS_MINIMAL " (Windows %d.%d; %s) IPP/2.0",
353faf12 485 version.dwMajorVersion, version.dwMinorVersion,
c35095b6 486 sysinfo.wProcessorArchitecture
487 == PROCESSOR_ARCHITECTURE_AMD64 ? "amd64" :
488 sysinfo.wProcessorArchitecture
489 == PROCESSOR_ARCHITECTURE_ARM ? "arm" :
490 sysinfo.wProcessorArchitecture
491 == PROCESSOR_ARCHITECTURE_IA64 ? "ia64" :
492 sysinfo.wProcessorArchitecture
493 == PROCESSOR_ARCHITECTURE_INTEL ? "intel" :
494 "unknown");
495
496#else
497 uname(&name);
498
499 snprintf(cg->user_agent, sizeof(cg->user_agent),
500 CUPS_MINIMAL " (%s %s; %s) IPP/2.0",
501 name.sysname, name.release, name.machine);
502#endif /* WIN32 */
503}
504
505
c68b6ae8 506/*
507 * 'cupsUser()' - Return the current user's name.
74a64efc 508 *
509 * Note: The current user name is tracked separately for each thread in a
510 * program. Multi-threaded programs that override the user name with the
511 * @link cupsSetUser@ function need to do so in each thread for the same user
512 * name to be used.
c68b6ae8 513 */
514
515const char * /* O - User name */
516cupsUser(void)
517{
2625096f 518 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
03f61bf3 519
520
521 if (!cg->user[0])
1d81a2a2 522 _cupsSetDefaults();
6b67a15e 523
03f61bf3 524 return (cg->user);
c68b6ae8 525}
6b67a15e 526
6b67a15e 527
c35095b6 528/*
529 * 'cupsUserAgent()' - Return the default HTTP User-Agent string.
530 *
531 * @since CUPS 1.7@
532 */
533
534const char * /* O - User-Agent string */
535cupsUserAgent(void)
536{
537 _cups_globals_t *cg = _cupsGlobals(); /* Thread globals */
538
539
540 if (!cg->user_agent[0])
541 cupsSetUserAgent(NULL);
542
543 return (cg->user_agent);
544}
545
546
c68b6ae8 547/*
f82d9dea 548 * '_cupsGetPassword()' - Get a password from the user.
c68b6ae8 549 */
550
24e92ab1 551const char * /* O - Password or @code NULL@ if none */
03f61bf3 552_cupsGetPassword(const char *prompt) /* I - Prompt string */
c68b6ae8 553{
74a64efc 554#ifdef WIN32
24e92ab1 555 HANDLE tty; /* Console handle */
556 DWORD mode; /* Console mode */
557 char passch, /* Current key press */
558 *passptr, /* Pointer into password string */
559 *passend; /* End of password string */
756c3d94 560 DWORD passbytes; /* Bytes read */
24e92ab1 561 _cups_globals_t *cg = _cupsGlobals();
562 /* Thread globals */
563
564
74a64efc 565 /*
24e92ab1 566 * Disable input echo and set raw input...
74a64efc 567 */
568
24e92ab1 569 if ((tty = GetStdHandle(STD_INPUT_HANDLE)) == INVALID_HANDLE_VALUE)
570 return (NULL);
571
572 if (!GetConsoleMode(tty, &mode))
573 return (NULL);
574
575 if (!SetConsoleMode(tty, 0))
576 return (NULL);
577
578 /*
579 * Display the prompt...
580 */
581
582 printf("%s ", prompt);
583 fflush(stdout);
584
585 /*
586 * Read the password string from /dev/tty until we get interrupted or get a
587 * carriage return or newline...
588 */
589
590 passptr = cg->password;
591 passend = cg->password + sizeof(cg->password) - 1;
592
756c3d94 593 while (ReadFile(tty, &passch, 1, &passbytes, NULL))
24e92ab1 594 {
595 if (passch == 0x0A || passch == 0x0D)
596 {
597 /*
598 * Enter/return...
599 */
600
601 break;
602 }
603 else if (passch == 0x08 || passch == 0x7F)
604 {
605 /*
606 * Backspace/delete (erase character)...
607 */
608
609 if (passptr > cg->password)
610 {
611 passptr --;
612 fputs("\010 \010", stdout);
613 }
614 else
615 putchar(0x07);
616 }
617 else if (passch == 0x15)
618 {
619 /*
620 * CTRL+U (erase line)
621 */
622
623 if (passptr > cg->password)
624 {
625 while (passptr > cg->password)
626 {
627 passptr --;
628 fputs("\010 \010", stdout);
629 }
630 }
631 else
632 putchar(0x07);
633 }
634 else if (passch == 0x03)
635 {
636 /*
637 * CTRL+C...
638 */
639
640 passptr = cg->password;
641 break;
642 }
643 else if ((passch & 255) < 0x20 || passptr >= passend)
644 putchar(0x07);
645 else
646 {
647 *passptr++ = passch;
648 putchar(_CUPS_PASSCHAR);
649 }
650
651 fflush(stdout);
652 }
653
654 putchar('\n');
655 fflush(stdout);
656
657 /*
658 * Cleanup...
659 */
660
661 SetConsoleMode(tty, mode);
662
663 /*
664 * Return the proper value...
665 */
666
667 if (passbytes == 1 && passptr > cg->password)
668 {
669 *passptr = '\0';
670 return (cg->password);
671 }
672 else
673 {
674 memset(cg->password, 0, sizeof(cg->password));
675 return (NULL);
676 }
74a64efc 677
678#else
24e92ab1 679 int tty; /* /dev/tty - never read from stdin */
680 struct termios original, /* Original input mode */
681 noecho; /* No echo input mode */
682 char passch, /* Current key press */
683 *passptr, /* Pointer into password string */
684 *passend; /* End of password string */
685 ssize_t passbytes; /* Bytes read */
686 _cups_globals_t *cg = _cupsGlobals();
687 /* Thread globals */
688
689
74a64efc 690 /*
24e92ab1 691 * Disable input echo and set raw input...
74a64efc 692 */
693
24e92ab1 694 if ((tty = open("/dev/tty", O_RDONLY)) < 0)
695 return (NULL);
696
697 if (tcgetattr(tty, &original))
698 {
699 close(tty);
700 return (NULL);
701 }
702
703 noecho = original;
704 noecho.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
179c800c 705
24e92ab1 706 if (tcsetattr(tty, TCSAFLUSH, &noecho))
707 {
708 close(tty);
179c800c 709 return (NULL);
24e92ab1 710 }
711
712 /*
713 * Display the prompt...
714 */
715
716 printf("%s ", prompt);
717 fflush(stdout);
718
719 /*
720 * Read the password string from /dev/tty until we get interrupted or get a
721 * carriage return or newline...
722 */
723
724 passptr = cg->password;
725 passend = cg->password + sizeof(cg->password) - 1;
726
727 while ((passbytes = read(tty, &passch, 1)) == 1)
728 {
729 if (passch == noecho.c_cc[VEOL] || passch == noecho.c_cc[VEOL2] ||
730 passch == 0x0A || passch == 0x0D)
731 {
732 /*
733 * Enter/return...
734 */
735
736 break;
737 }
738 else if (passch == noecho.c_cc[VERASE] ||
739 passch == 0x08 || passch == 0x7F)
740 {
741 /*
742 * Backspace/delete (erase character)...
743 */
744
745 if (passptr > cg->password)
746 {
747 passptr --;
748 fputs("\010 \010", stdout);
749 }
750 else
751 putchar(0x07);
752 }
753 else if (passch == noecho.c_cc[VKILL])
754 {
755 /*
756 * CTRL+U (erase line)
757 */
758
759 if (passptr > cg->password)
760 {
761 while (passptr > cg->password)
762 {
763 passptr --;
764 fputs("\010 \010", stdout);
765 }
766 }
767 else
768 putchar(0x07);
769 }
770 else if (passch == noecho.c_cc[VINTR] || passch == noecho.c_cc[VQUIT] ||
771 passch == noecho.c_cc[VEOF])
772 {
773 /*
774 * CTRL+C, CTRL+D, or CTRL+Z...
775 */
776
777 passptr = cg->password;
778 break;
779 }
780 else if ((passch & 255) < 0x20 || passptr >= passend)
781 putchar(0x07);
782 else
783 {
784 *passptr++ = passch;
785 putchar(_CUPS_PASSCHAR);
786 }
787
788 fflush(stdout);
789 }
790
791 putchar('\n');
792 fflush(stdout);
793
794 /*
795 * Cleanup...
796 */
797
798 tcsetattr(tty, TCSAFLUSH, &original);
799 close(tty);
800
801 /*
802 * Return the proper value...
803 */
804
805 if (passbytes == 1 && passptr > cg->password)
806 {
807 *passptr = '\0';
808 return (cg->password);
809 }
179c800c 810 else
24e92ab1 811 {
812 memset(cg->password, 0, sizeof(cg->password));
813 return (NULL);
814 }
e335a094 815#endif /* WIN32 */
74a64efc 816}
6b67a15e 817
818
7ca891cd 819#ifdef HAVE_GSSAPI
1b26e418 820/*
821 * '_cupsGSSServiceName()' - Get the GSS (Kerberos) service name.
822 */
823
824const char *
825_cupsGSSServiceName(void)
826{
827 _cups_globals_t *cg = _cupsGlobals(); /* Thread globals */
828
829
830 if (!cg->gss_service_name[0])
831 _cupsSetDefaults();
832
833 return (cg->gss_service_name);
834}
7ca891cd 835#endif /* HAVE_GSSAPI */
1b26e418 836
837
feb27103 838/*
d4c4cd54 839 * '_cupsSetDefaults()' - Set the default server, port, and encryption.
feb27103 840 */
841
d4c4cd54 842void
843_cupsSetDefaults(void)
feb27103 844{
845 cups_file_t *fp; /* File */
d4c4cd54 846 const char *home, /* Home directory of user */
847 *cups_encryption, /* CUPS_ENCRYPTION env var */
b9738d7c 848 *cups_server, /* CUPS_SERVER env var */
1d81a2a2 849 *cups_user, /* CUPS_USER/USER env var */
9200f229 850#ifdef HAVE_GSSAPI
851 *cups_gssservicename, /* CUPS_GSSSERVICENAME env var */
852#endif /* HAVE_GSSAPI */
b9738d7c 853 *cups_anyroot, /* CUPS_ANYROOT env var */
854 *cups_expiredroot, /* CUPS_EXPIREDROOT env var */
855 *cups_expiredcerts; /* CUPS_EXPIREDCERTS env var */
feb27103 856 char filename[1024]; /* Filename */
857 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
858
859
d4c4cd54 860 DEBUG_puts("_cupsSetDefaults()");
861
862 /*
863 * First collect environment variables...
864 */
865
9200f229 866 cups_encryption = getenv("CUPS_ENCRYPTION");
867 cups_server = getenv("CUPS_SERVER");
868#ifdef HAVE_GSSAPI
869 cups_gssservicename = getenv("CUPS_GSSSERVICENAME");
870#endif /* HAVE_GSSAPI */
871 cups_anyroot = getenv("CUPS_ANYROOT");
872 cups_expiredroot = getenv("CUPS_EXPIREDROOT");
873 cups_expiredcerts = getenv("CUPS_EXPIREDCERTS");
d4c4cd54 874
1d81a2a2 875 if ((cups_user = getenv("CUPS_USER")) == NULL)
876 cups_user = getenv("USER");
877
d4c4cd54 878 /*
86b1d49f 879 * Then, if needed, read the ~/.cups/client.conf or /etc/cups/client.conf
880 * files to get the default values...
d4c4cd54 881 */
882
d4c4cd54 883 if (cg->encryption == (http_encryption_t)-1 || !cg->server[0] ||
1d81a2a2 884 !cg->user[0] || !cg->ipp_port)
d4c4cd54 885 {
86b1d49f 886 if ((home = getenv("HOME")) != NULL)
80294044 887 {
86b1d49f 888 /*
889 * Look for ~/.cups/client.conf...
890 */
d4c4cd54 891
86b1d49f 892 snprintf(filename, sizeof(filename), "%s/.cups/client.conf", home);
893 fp = cupsFileOpen(filename, "r");
894 }
895 else
896 fp = NULL;
d4c4cd54 897
86b1d49f 898 if (!fp)
d4c4cd54 899 {
d4c4cd54 900 /*
86b1d49f 901 * Look for CUPS_SERVERROOT/client.conf...
d4c4cd54 902 */
903
86b1d49f 904 snprintf(filename, sizeof(filename), "%s/client.conf",
905 cg->cups_serverroot);
906 fp = cupsFileOpen(filename, "r");
d4c4cd54 907 }
908
86b1d49f 909 /*
910 * Read the configuration file and apply any environment variables; both
911 * functions handle NULL cups_file_t pointers...
912 */
d4c4cd54 913
1d81a2a2 914 cups_read_client_conf(fp, cg, cups_encryption, cups_server, cups_user,
9200f229 915#ifdef HAVE_GSSAPI
916 cups_gssservicename,
917#endif /* HAVE_GSSAPI */
86b1d49f 918 cups_anyroot, cups_expiredroot,
919 cups_expiredcerts);
920 cupsFileClose(fp);
d4c4cd54 921 }
922}
923
924
925/*
926 * 'cups_read_client_conf()' - Read a client.conf file.
927 */
928
929static void
930cups_read_client_conf(
931 cups_file_t *fp, /* I - File to read */
932 _cups_globals_t *cg, /* I - Global data */
933 const char *cups_encryption, /* I - CUPS_ENCRYPTION env var */
b9738d7c 934 const char *cups_server, /* I - CUPS_SERVER env var */
1d81a2a2 935 const char *cups_user, /* I - CUPS_USER env var */
9200f229 936#ifdef HAVE_GSSAPI
937 const char *cups_gssservicename,
938 /* I - CUPS_GSSSERVICENAME env var */
939#endif /* HAVE_GSSAPI */
b9738d7c 940 const char *cups_anyroot, /* I - CUPS_ANYROOT env var */
941 const char *cups_expiredroot, /* I - CUPS_EXPIREDROOT env var */
942 const char *cups_expiredcerts) /* I - CUPS_EXPIREDCERTS env var */
d4c4cd54 943{
944 int linenum; /* Current line number */
945 char line[1024], /* Line from file */
946 *value, /* Pointer into line */
947 encryption[1024], /* Encryption value */
dd319ef0 948#ifndef __APPLE__
b9738d7c 949 server_name[1024], /* ServerName value */
dd319ef0 950#endif /* !__APPLE__ */
1d81a2a2 951 user[256], /* User value */
b9738d7c 952 any_root[1024], /* AllowAnyRoot value */
953 expired_root[1024], /* AllowExpiredRoot value */
954 expired_certs[1024]; /* AllowExpiredCerts value */
9200f229 955#ifdef HAVE_GSSAPI
956 char gss_service_name[32]; /* GSSServiceName value */
957#endif /* HAVE_GSSAPI */
d4c4cd54 958
959
960 /*
961 * Read from the file...
962 */
963
964 linenum = 0;
965 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
966 {
967 if (!cups_encryption && cg->encryption == (http_encryption_t)-1 &&
c6fab96f 968 !_cups_strcasecmp(line, "Encryption") && value)
d4c4cd54 969 {
970 strlcpy(encryption, value, sizeof(encryption));
971 cups_encryption = encryption;
972 }
3003b9c7 973#ifndef __APPLE__
974 /*
c45401bb 975 * The Server directive is not supported on OS X due to app sandboxing
3003b9c7 976 * restrictions, i.e. not all apps request network access.
977 */
d4c4cd54 978 else if (!cups_server && (!cg->server[0] || !cg->ipp_port) &&
c6fab96f 979 !_cups_strcasecmp(line, "ServerName") && value)
d4c4cd54 980 {
981 strlcpy(server_name, value, sizeof(server_name));
982 cups_server = server_name;
983 }
3003b9c7 984#endif /* !__APPLE__ */
1d81a2a2 985 else if (!cups_user && !_cups_strcasecmp(line, "User") && value)
986 {
987 strlcpy(user, value, sizeof(user));
988 cups_user = user;
989 }
c6fab96f 990 else if (!cups_anyroot && !_cups_strcasecmp(line, "AllowAnyRoot") && value)
b9738d7c 991 {
992 strlcpy(any_root, value, sizeof(any_root));
993 cups_anyroot = any_root;
994 }
c6fab96f 995 else if (!cups_expiredroot && !_cups_strcasecmp(line, "AllowExpiredRoot") &&
b9738d7c 996 value)
997 {
998 strlcpy(expired_root, value, sizeof(expired_root));
999 cups_expiredroot = expired_root;
1000 }
c6fab96f 1001 else if (!cups_expiredcerts && !_cups_strcasecmp(line, "AllowExpiredCerts") &&
b9738d7c 1002 value)
1003 {
1004 strlcpy(expired_certs, value, sizeof(expired_certs));
1005 cups_expiredcerts = expired_certs;
1006 }
9200f229 1007#ifdef HAVE_GSSAPI
c6fab96f 1008 else if (!cups_gssservicename && !_cups_strcasecmp(line, "GSSServiceName") &&
9200f229 1009 value)
1010 {
1011 strlcpy(gss_service_name, value, sizeof(gss_service_name));
1012 cups_gssservicename = gss_service_name;
1013 }
1014#endif /* HAVE_GSSAPI */
d4c4cd54 1015 }
1016
1017 /*
1018 * Set values...
1019 */
1020
1021 if (cg->encryption == (http_encryption_t)-1 && cups_encryption)
1022 {
c6fab96f 1023 if (!_cups_strcasecmp(cups_encryption, "never"))
d6ff282a 1024 cg->encryption = HTTP_ENCRYPTION_NEVER;
c6fab96f 1025 else if (!_cups_strcasecmp(cups_encryption, "always"))
d6ff282a 1026 cg->encryption = HTTP_ENCRYPTION_ALWAYS;
c6fab96f 1027 else if (!_cups_strcasecmp(cups_encryption, "required"))
d6ff282a 1028 cg->encryption = HTTP_ENCRYPTION_REQUIRED;
d4c4cd54 1029 else
d6ff282a 1030 cg->encryption = HTTP_ENCRYPTION_IF_REQUESTED;
feb27103 1031 }
1032
d4c4cd54 1033 if ((!cg->server[0] || !cg->ipp_port) && cups_server)
969d6a6d 1034 cupsSetServer(cups_server);
b9738d7c 1035
86b1d49f 1036 if (!cg->server[0])
1037 {
1038#ifdef CUPS_DEFAULT_DOMAINSOCKET
1039 /*
1040 * If we are compiled with domain socket support, only use the
1041 * domain socket if it exists and has the right permissions...
1042 */
1043
1044 struct stat sockinfo; /* Domain socket information */
1045
1046 if (!stat(CUPS_DEFAULT_DOMAINSOCKET, &sockinfo) &&
1047 (sockinfo.st_mode & S_IRWXO) == S_IRWXO)
1048 cups_server = CUPS_DEFAULT_DOMAINSOCKET;
1049 else
1050#endif /* CUPS_DEFAULT_DOMAINSOCKET */
1051 cups_server = "localhost";
1052
1053 cupsSetServer(cups_server);
1054 }
1055
1056 if (!cg->ipp_port)
1057 {
d69ab0a8 1058 const char *ipp_port; /* IPP_PORT environment variable */
86b1d49f 1059
1060 if ((ipp_port = getenv("IPP_PORT")) != NULL)
1061 {
1062 if ((cg->ipp_port = atoi(ipp_port)) <= 0)
1063 cg->ipp_port = CUPS_DEFAULT_IPP_PORT;
1064 }
86b1d49f 1065 else
7bd21f5d 1066 cg->ipp_port = CUPS_DEFAULT_IPP_PORT;
86b1d49f 1067 }
1068
1d81a2a2 1069 if (!cg->user[0])
1070 {
1071 if (cups_user)
1072 strlcpy(cg->user, cups_user, sizeof(cg->user));
1073 else
1074 {
1075#ifdef WIN32
1076 /*
1077 * Get the current user name from the OS...
1078 */
1079
1080 DWORD size; /* Size of string */
1081
1082 size = sizeof(cg->user);
1083 if (!GetUserName(cg->user, &size))
1084#else
1085 /*
1086 * Get the user name corresponding to the current UID...
1087 */
1088
1089 struct passwd *pwd; /* User/password entry */
1090
1091 setpwent();
1092 if ((pwd = getpwuid(getuid())) != NULL)
1093 {
1094 /*
1095 * Found a match!
1096 */
1097
1098 strlcpy(cg->user, pwd->pw_name, sizeof(cg->user));
1099 }
1100 else
1101#endif /* WIN32 */
1102 {
1103 /*
1104 * Use the default "unknown" user name...
1105 */
1106
af882006 1107 strlcpy(cg->user, "unknown", sizeof(cg->user));
1d81a2a2 1108 }
1109 }
1110 }
1111
9200f229 1112#ifdef HAVE_GSSAPI
1113 if (!cups_gssservicename)
1114 cups_gssservicename = CUPS_DEFAULT_GSSSERVICENAME;
1115
1116 strlcpy(cg->gss_service_name, cups_gssservicename,
1117 sizeof(cg->gss_service_name));
1118#endif /* HAVE_GSSAPI */
1119
b9738d7c 1120 if (cups_anyroot)
c6fab96f 1121 cg->any_root = !_cups_strcasecmp(cups_anyroot, "yes") ||
1122 !_cups_strcasecmp(cups_anyroot, "on") ||
1123 !_cups_strcasecmp(cups_anyroot, "true");
b9738d7c 1124
1125 if (cups_expiredroot)
c6fab96f 1126 cg->expired_root = !_cups_strcasecmp(cups_expiredroot, "yes") ||
1127 !_cups_strcasecmp(cups_expiredroot, "on") ||
1128 !_cups_strcasecmp(cups_expiredroot, "true");
b9738d7c 1129
1130 if (cups_expiredcerts)
c6fab96f 1131 cg->expired_certs = !_cups_strcasecmp(cups_expiredcerts, "yes") ||
1132 !_cups_strcasecmp(cups_expiredcerts, "on") ||
1133 !_cups_strcasecmp(cups_expiredcerts, "true");
feb27103 1134}
1135
1136
e93faa10 1137/*
c9d3f842 1138 * End of "$Id$".
6b67a15e 1139 */