2 * "$Id: usersys.c 8498 2009-04-13 17:03:15Z mike $"
4 * User, system, and password routines for CUPS.
6 * Copyright 2007-2010 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products.
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/".
15 * This file is subject to the Apple OS-Developed Software exception.
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
23 * cupsServer() - Return the hostname/address of the current
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.
39 * Include necessary headers...
42 #include "cups-private.h"
56 static void cups_read_client_conf(cups_file_t
*fp
,
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
);
66 * 'cupsEncryption()' - Get the current encryption settings.
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@.
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
79 http_encryption_t
/* O - Encryption settings */
82 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
85 if (cg
->encryption
== (http_encryption_t
)-1)
88 return (cg
->encryption
);
93 * 'cupsGetPassword()' - Get a password from the user.
95 * Uses the current password callback function. Returns @code NULL@ if the
96 * user does not provide a password.
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.
104 const char * /* O - Password */
105 cupsGetPassword(const char *prompt
) /* I - Prompt string */
107 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
110 return ((cg
->password_cb
)(prompt
, NULL
, NULL
, NULL
, cg
->password_data
));
115 * 'cupsGetPassword2()' - Get a password from the user using the advanced
118 * Uses the current password callback function. Returns @code NULL@ if the
119 * user does not provide a password.
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.
126 * @since CUPS 1.4/Mac OS X 10.6@
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 */
135 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
139 http
= _cupsConnect();
141 return ((cg
->password_cb
)(prompt
, http
, method
, resource
, cg
->password_data
));
146 * 'cupsServer()' - Return the hostname/address of the current server.
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
153 * The returned value can be a fully-qualified hostname, a numeric IPv4 or IPv6
154 * address, or a domain socket pathname.
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
162 const char * /* O - Server name */
165 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
176 * 'cupsSetClientCertCB()' - Set the client certificate callback.
178 * Pass @code NULL@ to restore the default callback.
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.
189 cups_client_cert_cb_t cb
, /* I - Callback function */
190 void *user_data
) /* I - User data pointer */
192 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
195 cg
->client_cert_cb
= cb
;
196 cg
->client_cert_data
= user_data
;
201 * 'cupsSetCredentials()' - Set the default credentials to be used for SSL/TLS
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.
211 int /* O - Status of call (0 = success) */
213 cups_array_t
*credentials
) /* I - Array of credentials */
215 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
218 if (cupsArrayCount(credentials
) < 1)
221 _httpFreeCredentials(cg
->tls_credentials
);
222 cg
->tls_credentials
= _httpConvertCredentials(credentials
);
224 return (cg
->tls_credentials
? 0 : -1);
229 * 'cupsSetEncryption()' - Set the encryption preference.
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@.
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.
242 cupsSetEncryption(http_encryption_t e
) /* I - New encryption preference */
244 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
250 httpEncryption(cg
->http
, e
);
255 * 'cupsSetPasswordCB()' - Set the password callback for CUPS.
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.
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.
268 cupsSetPasswordCB(cups_password_cb_t cb
)/* I - Callback function */
270 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
273 if (cb
== (cups_password_cb_t
)0)
274 cg
->password_cb
= (cups_password_cb2_t
)_cupsGetPassword
;
276 cg
->password_cb
= (cups_password_cb2_t
)cb
;
278 cg
->password_data
= NULL
;
283 * 'cupsSetPasswordCB2()' - Set the advanced password callback for CUPS.
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.
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.
294 * @since CUPS 1.4/Mac OS X 10.6@
299 cups_password_cb2_t cb
, /* I - Callback function */
300 void *user_data
) /* I - User data pointer */
302 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
305 if (cb
== (cups_password_cb2_t
)0)
306 cg
->password_cb
= (cups_password_cb2_t
)_cupsGetPassword
;
308 cg
->password_cb
= cb
;
310 cg
->password_data
= user_data
;
315 * 'cupsSetServer()' - Set the default server name and port.
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.
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.
329 cupsSetServer(const char *server
) /* I - Server name */
331 char *port
; /* Pointer to port */
332 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
337 strlcpy(cg
->server
, server
, sizeof(cg
->server
));
339 if (cg
->server
[0] != '/' && (port
= strrchr(cg
->server
, ':')) != NULL
&&
340 !strchr(port
, ']') && isdigit(port
[1] & 255))
344 cg
->ipp_port
= atoi(port
);
347 if (cg
->server
[0] == '/')
348 strcpy(cg
->servername
, "localhost");
350 strlcpy(cg
->servername
, cg
->server
, sizeof(cg
->servername
));
354 cg
->server
[0] = '\0';
355 cg
->servername
[0] = '\0';
367 * 'cupsSetServerCertCB()' - Set the server certificate callback.
369 * Pass @code NULL@ to restore the default callback.
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.
380 cups_server_cert_cb_t cb
, /* I - Callback function */
381 void *user_data
) /* I - User data pointer */
383 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
386 cg
->server_cert_cb
= cb
;
387 cg
->server_cert_data
= user_data
;
392 * 'cupsSetUser()' - Set the default user name.
394 * Pass @code NULL@ to restore the default user name.
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.
402 cupsSetUser(const char *user
) /* I - User name */
404 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
408 strlcpy(cg
->user
, user
, sizeof(cg
->user
));
415 * 'cupsUser()' - Return the current user's name.
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
423 const char * /* O - User name */
426 const char *user
; /* USER environment variable */
427 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
434 * Get the current user name from the OS...
437 DWORD size
; /* Size of string */
439 size
= sizeof(cg
->user
);
440 if (!GetUserName(cg
->user
, &size
))
443 * Get the user name corresponding to the current UID...
446 struct passwd
*pwd
; /* User/password entry */
449 if ((pwd
= getpwuid(getuid())) != NULL
)
455 strlcpy(cg
->user
, pwd
->pw_name
, sizeof(cg
->user
));
459 if ((user
= getenv("USER")) != NULL
)
462 * Use the username from the "USER" environment variable...
464 strlcpy(cg
->user
, user
, sizeof(cg
->user
));
469 * Use the default "unknown" user name...
472 strcpy(cg
->user
, "unknown");
481 * '_cupsGetPassword()' - Get a password from the user.
484 const char * /* O - Password */
485 _cupsGetPassword(const char *prompt
) /* I - Prompt string */
489 * Currently no console password support is provided on Windows.
496 * Use the standard getpass function to get a password from the console. An
497 * empty password is treated as canceling the authentication request.
500 const char *password
= getpass(prompt
);
501 /* Password string */
503 if (!password
|| !password
[0])
512 * '_cupsSetDefaults()' - Set the default server, port, and encryption.
516 _cupsSetDefaults(void)
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 */
529 DEBUG_puts("_cupsSetDefaults()");
532 * First collect environment variables...
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");
542 * Then, if needed, read the ~/.cups/client.conf or /etc/cups/client.conf
543 * files to get the default values...
546 if (cg
->encryption
== (http_encryption_t
)-1 || !cg
->server
[0] ||
549 if ((home
= getenv("HOME")) != NULL
)
552 * Look for ~/.cups/client.conf...
555 snprintf(filename
, sizeof(filename
), "%s/.cups/client.conf", home
);
556 fp
= cupsFileOpen(filename
, "r");
564 * Look for CUPS_SERVERROOT/client.conf...
567 snprintf(filename
, sizeof(filename
), "%s/client.conf",
568 cg
->cups_serverroot
);
569 fp
= cupsFileOpen(filename
, "r");
573 * Read the configuration file and apply any environment variables; both
574 * functions handle NULL cups_file_t pointers...
577 cups_read_client_conf(fp
, cg
, cups_encryption
, cups_server
,
578 cups_anyroot
, cups_expiredroot
,
586 * 'cups_read_client_conf()' - Read a client.conf file.
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 */
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 */
610 * Read from the file...
614 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
616 if (!cups_encryption
&& cg
->encryption
== (http_encryption_t
)-1 &&
617 !strcasecmp(line
, "Encryption") && value
)
619 strlcpy(encryption
, value
, sizeof(encryption
));
620 cups_encryption
= encryption
;
622 else if (!cups_server
&& (!cg
->server
[0] || !cg
->ipp_port
) &&
623 !strcasecmp(line
, "ServerName") && value
)
625 strlcpy(server_name
, value
, sizeof(server_name
));
626 cups_server
= server_name
;
628 else if (!cups_anyroot
&& !strcasecmp(line
, "AllowAnyRoot") && value
)
630 strlcpy(any_root
, value
, sizeof(any_root
));
631 cups_anyroot
= any_root
;
633 else if (!cups_expiredroot
&& !strcasecmp(line
, "AllowExpiredRoot") &&
636 strlcpy(expired_root
, value
, sizeof(expired_root
));
637 cups_expiredroot
= expired_root
;
639 else if (!cups_expiredcerts
&& !strcasecmp(line
, "AllowExpiredCerts") &&
642 strlcpy(expired_certs
, value
, sizeof(expired_certs
));
643 cups_expiredcerts
= expired_certs
;
651 if (cg
->encryption
== (http_encryption_t
)-1 && cups_encryption
)
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
;
660 cg
->encryption
= HTTP_ENCRYPT_IF_REQUESTED
;
663 if ((!cg
->server
[0] || !cg
->ipp_port
) && cups_server
)
668 * Copy server name...
671 strlcpy(cg
->server
, cups_server
, sizeof(cg
->server
));
673 if (cg
->server
[0] != '/' && (value
= strrchr(cg
->server
, ':')) != NULL
&&
674 !strchr(value
, ']') && isdigit(value
[1] & 255))
679 if (cg
->server
[0] == '/')
680 strcpy(cg
->servername
, "localhost");
682 strlcpy(cg
->servername
, cg
->server
, sizeof(cg
->servername
));
684 else if (cups_server
[0] != '/' &&
685 (value
= strrchr(cups_server
, ':')) != NULL
&&
686 !strchr(value
, ']') && isdigit(value
[1] & 255))
691 if (!cg
->ipp_port
&& value
)
692 cg
->ipp_port
= atoi(value
);
697 #ifdef CUPS_DEFAULT_DOMAINSOCKET
699 * If we are compiled with domain socket support, only use the
700 * domain socket if it exists and has the right permissions...
703 struct stat sockinfo
; /* Domain socket information */
705 if (!stat(CUPS_DEFAULT_DOMAINSOCKET
, &sockinfo
) &&
706 (sockinfo
.st_mode
& S_IRWXO
) == S_IRWXO
)
707 cups_server
= CUPS_DEFAULT_DOMAINSOCKET
;
709 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
710 cups_server
= "localhost";
712 cupsSetServer(cups_server
);
717 const char *ipp_port
; /* IPP_PORT environment variable */
718 struct servent
*service
; /* Port number info */
721 if ((ipp_port
= getenv("IPP_PORT")) != NULL
)
723 if ((cg
->ipp_port
= atoi(ipp_port
)) <= 0)
724 cg
->ipp_port
= CUPS_DEFAULT_IPP_PORT
;
726 else if ((service
= getservbyname("ipp", NULL
)) == NULL
||
727 service
->s_port
<= 0)
728 cg
->ipp_port
= CUPS_DEFAULT_IPP_PORT
;
730 cg
->ipp_port
= ntohs(service
->s_port
);
734 cg
->any_root
= !strcasecmp(cups_anyroot
, "yes") ||
735 !strcasecmp(cups_anyroot
, "on") ||
736 !strcasecmp(cups_anyroot
, "true");
738 if (cups_expiredroot
)
739 cg
->expired_root
= !strcasecmp(cups_expiredroot
, "yes") ||
740 !strcasecmp(cups_expiredroot
, "on") ||
741 !strcasecmp(cups_expiredroot
, "true");
743 if (cups_expiredcerts
)
744 cg
->expired_certs
= !strcasecmp(cups_expiredcerts
, "yes") ||
745 !strcasecmp(cups_expiredcerts
, "on") ||
746 !strcasecmp(cups_expiredcerts
, "true");
751 * End of "$Id: usersys.c 8498 2009-04-13 17:03:15Z mike $".