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