2 * "$Id: usersys.c 8498 2009-04-13 17:03:15Z mike $"
4 * User, system, and password routines for CUPS.
6 * Copyright 2007-2011 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
,
61 const char *cups_gssservicename
,
62 #endif /* HAVE_GSSAPI */
63 const char *cups_anyroot
,
64 const char *cups_expiredroot
,
65 const char *cups_expiredcerts
);
69 * 'cupsEncryption()' - Get the current encryption settings.
71 * The default encryption setting comes from the CUPS_ENCRYPTION
72 * environment variable, then the ~/.cups/client.conf file, and finally the
73 * /etc/cups/client.conf file. If not set, the default is
74 * @code HTTP_ENCRYPT_IF_REQUESTED@.
76 * Note: The current encryption setting is tracked separately for each thread
77 * in a program. Multi-threaded programs that override the setting via the
78 * @link cupsSetEncryption@ function need to do so in each thread for the same
82 http_encryption_t
/* O - Encryption settings */
85 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
88 if (cg
->encryption
== (http_encryption_t
)-1)
91 return (cg
->encryption
);
96 * 'cupsGetPassword()' - Get a password from the user.
98 * Uses the current password callback function. Returns @code NULL@ if the
99 * user does not provide a password.
101 * Note: The current password callback function is tracked separately for each
102 * thread in a program. Multi-threaded programs that override the setting via
103 * the @link cupsSetPasswordCB@ or @link cupsSetPasswordCB2@ functions need to
104 * do so in each thread for the same function to be used.
107 const char * /* O - Password */
108 cupsGetPassword(const char *prompt
) /* I - Prompt string */
110 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
113 return ((cg
->password_cb
)(prompt
, NULL
, NULL
, NULL
, cg
->password_data
));
118 * 'cupsGetPassword2()' - Get a password from the user using the advanced
121 * Uses the current password callback function. Returns @code NULL@ if the
122 * user does not provide a password.
124 * Note: The current password callback function is tracked separately for each
125 * thread in a program. Multi-threaded programs that override the setting via
126 * the @link cupsSetPasswordCB@ or @link cupsSetPasswordCB2@ functions need to
127 * do so in each thread for the same function to be used.
129 * @since CUPS 1.4/Mac OS X 10.6@
132 const char * /* O - Password */
133 cupsGetPassword2(const char *prompt
, /* I - Prompt string */
134 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
135 const char *method
, /* I - Request method ("GET", "POST", "PUT") */
136 const char *resource
) /* I - Resource path */
138 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
142 http
= _cupsConnect();
144 return ((cg
->password_cb
)(prompt
, http
, method
, resource
, cg
->password_data
));
149 * 'cupsServer()' - Return the hostname/address of the current server.
151 * The default server comes from the CUPS_SERVER environment variable, then the
152 * ~/.cups/client.conf file, and finally the /etc/cups/client.conf file. If not
153 * set, the default is the local system - either "localhost" or a domain socket
156 * The returned value can be a fully-qualified hostname, a numeric IPv4 or IPv6
157 * address, or a domain socket pathname.
159 * Note: The current server is tracked separately for each thread in a program.
160 * Multi-threaded programs that override the server via the
161 * @link cupsSetServer@ function need to do so in each thread for the same
165 const char * /* O - Server name */
168 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
179 * 'cupsSetClientCertCB()' - Set the client certificate callback.
181 * Pass @code NULL@ to restore the default callback.
183 * Note: The current certificate callback is tracked separately for each thread
184 * in a program. Multi-threaded programs that override the callback need to do
185 * so in each thread for the same callback to be used.
192 cups_client_cert_cb_t cb
, /* I - Callback function */
193 void *user_data
) /* I - User data pointer */
195 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
198 cg
->client_cert_cb
= cb
;
199 cg
->client_cert_data
= user_data
;
204 * 'cupsSetCredentials()' - Set the default credentials to be used for SSL/TLS
207 * Note: The default credentials are tracked separately for each thread in a
208 * program. Multi-threaded programs that override the setting need to do so in
209 * each thread for the same setting to be used.
214 int /* O - Status of call (0 = success) */
216 cups_array_t
*credentials
) /* I - Array of credentials */
218 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
221 if (cupsArrayCount(credentials
) < 1)
224 _httpFreeCredentials(cg
->tls_credentials
);
225 cg
->tls_credentials
= _httpConvertCredentials(credentials
);
227 return (cg
->tls_credentials
? 0 : -1);
232 * 'cupsSetEncryption()' - Set the encryption preference.
234 * The default encryption setting comes from the CUPS_ENCRYPTION
235 * environment variable, then the ~/.cups/client.conf file, and finally the
236 * /etc/cups/client.conf file. If not set, the default is
237 * @code HTTP_ENCRYPT_IF_REQUESTED@.
239 * Note: The current encryption setting is tracked separately for each thread
240 * in a program. Multi-threaded programs that override the setting need to do
241 * so in each thread for the same setting to be used.
245 cupsSetEncryption(http_encryption_t e
) /* I - New encryption preference */
247 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
253 httpEncryption(cg
->http
, e
);
258 * 'cupsSetPasswordCB()' - Set the password callback for CUPS.
260 * Pass @code NULL@ to restore the default (console) password callback, which
261 * reads the password from the console. Programs should call either this
262 * function or @link cupsSetPasswordCB2@, as only one callback can be registered
263 * by a program per thread.
265 * Note: The current password callback is tracked separately for each thread
266 * in a program. Multi-threaded programs that override the callback need to do
267 * so in each thread for the same callback to be used.
271 cupsSetPasswordCB(cups_password_cb_t cb
)/* I - Callback function */
273 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
276 if (cb
== (cups_password_cb_t
)0)
277 cg
->password_cb
= (cups_password_cb2_t
)_cupsGetPassword
;
279 cg
->password_cb
= (cups_password_cb2_t
)cb
;
281 cg
->password_data
= NULL
;
286 * 'cupsSetPasswordCB2()' - Set the advanced password callback for CUPS.
288 * Pass @code NULL@ to restore the default (console) password callback, which
289 * reads the password from the console. Programs should call either this
290 * function or @link cupsSetPasswordCB2@, as only one callback can be registered
291 * by a program per thread.
293 * Note: The current password callback is tracked separately for each thread
294 * in a program. Multi-threaded programs that override the callback need to do
295 * so in each thread for the same callback to be used.
297 * @since CUPS 1.4/Mac OS X 10.6@
302 cups_password_cb2_t cb
, /* I - Callback function */
303 void *user_data
) /* I - User data pointer */
305 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
308 if (cb
== (cups_password_cb2_t
)0)
309 cg
->password_cb
= (cups_password_cb2_t
)_cupsGetPassword
;
311 cg
->password_cb
= cb
;
313 cg
->password_data
= user_data
;
318 * 'cupsSetServer()' - Set the default server name and port.
320 * The "server" string can be a fully-qualified hostname, a numeric
321 * IPv4 or IPv6 address, or a domain socket pathname. Hostnames and numeric IP
322 * addresses can be optionally followed by a colon and port number to override
323 * the default port 631, e.g. "hostname:8631". Pass @code NULL@ to restore the
324 * default server name and port.
326 * Note: The current server is tracked separately for each thread in a program.
327 * Multi-threaded programs that override the server need to do so in each
328 * thread for the same server to be used.
332 cupsSetServer(const char *server
) /* I - Server name */
334 char *port
; /* Pointer to port */
335 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
340 strlcpy(cg
->server
, server
, sizeof(cg
->server
));
342 if (cg
->server
[0] != '/' && (port
= strrchr(cg
->server
, ':')) != NULL
&&
343 !strchr(port
, ']') && isdigit(port
[1] & 255))
347 cg
->ipp_port
= atoi(port
);
350 if (cg
->server
[0] == '/')
351 strcpy(cg
->servername
, "localhost");
353 strlcpy(cg
->servername
, cg
->server
, sizeof(cg
->servername
));
357 cg
->server
[0] = '\0';
358 cg
->servername
[0] = '\0';
370 * 'cupsSetServerCertCB()' - Set the server certificate callback.
372 * Pass @code NULL@ to restore the default callback.
374 * Note: The current credentials callback is tracked separately for each thread
375 * in a program. Multi-threaded programs that override the callback need to do
376 * so in each thread for the same callback to be used.
383 cups_server_cert_cb_t cb
, /* I - Callback function */
384 void *user_data
) /* I - User data pointer */
386 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
389 cg
->server_cert_cb
= cb
;
390 cg
->server_cert_data
= user_data
;
395 * 'cupsSetUser()' - Set the default user name.
397 * Pass @code NULL@ to restore the default user name.
399 * Note: The current user name is tracked separately for each thread in a
400 * program. Multi-threaded programs that override the user name need to do so
401 * in each thread for the same user name to be used.
405 cupsSetUser(const char *user
) /* I - User name */
407 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
411 strlcpy(cg
->user
, user
, sizeof(cg
->user
));
418 * 'cupsUser()' - Return the current user's name.
420 * Note: The current user name is tracked separately for each thread in a
421 * program. Multi-threaded programs that override the user name with the
422 * @link cupsSetUser@ function need to do so in each thread for the same user
426 const char * /* O - User name */
429 const char *user
; /* USER environment variable */
430 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
437 * Get the current user name from the OS...
440 DWORD size
; /* Size of string */
442 size
= sizeof(cg
->user
);
443 if (!GetUserName(cg
->user
, &size
))
446 * Get the user name corresponding to the current UID...
449 struct passwd
*pwd
; /* User/password entry */
452 if ((pwd
= getpwuid(getuid())) != NULL
)
458 strlcpy(cg
->user
, pwd
->pw_name
, sizeof(cg
->user
));
462 if ((user
= getenv("USER")) != NULL
)
465 * Use the username from the "USER" environment variable...
467 strlcpy(cg
->user
, user
, sizeof(cg
->user
));
472 * Use the default "unknown" user name...
475 strcpy(cg
->user
, "unknown");
484 * '_cupsGetPassword()' - Get a password from the user.
487 const char * /* O - Password */
488 _cupsGetPassword(const char *prompt
) /* I - Prompt string */
492 * Currently no console password support is provided on Windows.
499 * Use the standard getpass function to get a password from the console. An
500 * empty password is treated as canceling the authentication request.
503 const char *password
= getpass(prompt
);
504 /* Password string */
506 if (!password
|| !password
[0])
515 * '_cupsSetDefaults()' - Set the default server, port, and encryption.
519 _cupsSetDefaults(void)
521 cups_file_t
*fp
; /* File */
522 const char *home
, /* Home directory of user */
523 *cups_encryption
, /* CUPS_ENCRYPTION env var */
524 *cups_server
, /* CUPS_SERVER env var */
526 *cups_gssservicename
, /* CUPS_GSSSERVICENAME env var */
527 #endif /* HAVE_GSSAPI */
528 *cups_anyroot
, /* CUPS_ANYROOT env var */
529 *cups_expiredroot
, /* CUPS_EXPIREDROOT env var */
530 *cups_expiredcerts
; /* CUPS_EXPIREDCERTS env var */
531 char filename
[1024]; /* Filename */
532 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
535 DEBUG_puts("_cupsSetDefaults()");
538 * First collect environment variables...
541 cups_encryption
= getenv("CUPS_ENCRYPTION");
542 cups_server
= getenv("CUPS_SERVER");
544 cups_gssservicename
= getenv("CUPS_GSSSERVICENAME");
545 #endif /* HAVE_GSSAPI */
546 cups_anyroot
= getenv("CUPS_ANYROOT");
547 cups_expiredroot
= getenv("CUPS_EXPIREDROOT");
548 cups_expiredcerts
= getenv("CUPS_EXPIREDCERTS");
551 * Then, if needed, read the ~/.cups/client.conf or /etc/cups/client.conf
552 * files to get the default values...
555 if (cg
->encryption
== (http_encryption_t
)-1 || !cg
->server
[0] ||
558 if ((home
= getenv("HOME")) != NULL
)
561 * Look for ~/.cups/client.conf...
564 snprintf(filename
, sizeof(filename
), "%s/.cups/client.conf", home
);
565 fp
= cupsFileOpen(filename
, "r");
573 * Look for CUPS_SERVERROOT/client.conf...
576 snprintf(filename
, sizeof(filename
), "%s/client.conf",
577 cg
->cups_serverroot
);
578 fp
= cupsFileOpen(filename
, "r");
582 * Read the configuration file and apply any environment variables; both
583 * functions handle NULL cups_file_t pointers...
586 cups_read_client_conf(fp
, cg
, cups_encryption
, cups_server
,
589 #endif /* HAVE_GSSAPI */
590 cups_anyroot
, cups_expiredroot
,
598 * 'cups_read_client_conf()' - Read a client.conf file.
602 cups_read_client_conf(
603 cups_file_t
*fp
, /* I - File to read */
604 _cups_globals_t
*cg
, /* I - Global data */
605 const char *cups_encryption
, /* I - CUPS_ENCRYPTION env var */
606 const char *cups_server
, /* I - CUPS_SERVER env var */
608 const char *cups_gssservicename
,
609 /* I - CUPS_GSSSERVICENAME env var */
610 #endif /* HAVE_GSSAPI */
611 const char *cups_anyroot
, /* I - CUPS_ANYROOT env var */
612 const char *cups_expiredroot
, /* I - CUPS_EXPIREDROOT env var */
613 const char *cups_expiredcerts
) /* I - CUPS_EXPIREDCERTS env var */
615 int linenum
; /* Current line number */
616 char line
[1024], /* Line from file */
617 *value
, /* Pointer into line */
618 encryption
[1024], /* Encryption value */
619 server_name
[1024], /* ServerName value */
620 any_root
[1024], /* AllowAnyRoot value */
621 expired_root
[1024], /* AllowExpiredRoot value */
622 expired_certs
[1024]; /* AllowExpiredCerts value */
624 char gss_service_name
[32]; /* GSSServiceName value */
625 #endif /* HAVE_GSSAPI */
629 * Read from the file...
633 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
635 if (!cups_encryption
&& cg
->encryption
== (http_encryption_t
)-1 &&
636 !strcasecmp(line
, "Encryption") && value
)
638 strlcpy(encryption
, value
, sizeof(encryption
));
639 cups_encryption
= encryption
;
641 else if (!cups_server
&& (!cg
->server
[0] || !cg
->ipp_port
) &&
642 !strcasecmp(line
, "ServerName") && value
)
644 strlcpy(server_name
, value
, sizeof(server_name
));
645 cups_server
= server_name
;
647 else if (!cups_anyroot
&& !strcasecmp(line
, "AllowAnyRoot") && value
)
649 strlcpy(any_root
, value
, sizeof(any_root
));
650 cups_anyroot
= any_root
;
652 else if (!cups_expiredroot
&& !strcasecmp(line
, "AllowExpiredRoot") &&
655 strlcpy(expired_root
, value
, sizeof(expired_root
));
656 cups_expiredroot
= expired_root
;
658 else if (!cups_expiredcerts
&& !strcasecmp(line
, "AllowExpiredCerts") &&
661 strlcpy(expired_certs
, value
, sizeof(expired_certs
));
662 cups_expiredcerts
= expired_certs
;
665 else if (!cups_gssservicename
&& !strcasecmp(line
, "GSSServiceName") &&
668 strlcpy(gss_service_name
, value
, sizeof(gss_service_name
));
669 cups_gssservicename
= gss_service_name
;
671 #endif /* HAVE_GSSAPI */
678 if (cg
->encryption
== (http_encryption_t
)-1 && cups_encryption
)
680 if (!strcasecmp(cups_encryption
, "never"))
681 cg
->encryption
= HTTP_ENCRYPT_NEVER
;
682 else if (!strcasecmp(cups_encryption
, "always"))
683 cg
->encryption
= HTTP_ENCRYPT_ALWAYS
;
684 else if (!strcasecmp(cups_encryption
, "required"))
685 cg
->encryption
= HTTP_ENCRYPT_REQUIRED
;
687 cg
->encryption
= HTTP_ENCRYPT_IF_REQUESTED
;
690 if ((!cg
->server
[0] || !cg
->ipp_port
) && cups_server
)
695 * Copy server name...
698 strlcpy(cg
->server
, cups_server
, sizeof(cg
->server
));
700 if (cg
->server
[0] != '/' && (value
= strrchr(cg
->server
, ':')) != NULL
&&
701 !strchr(value
, ']') && isdigit(value
[1] & 255))
706 if (cg
->server
[0] == '/')
707 strcpy(cg
->servername
, "localhost");
709 strlcpy(cg
->servername
, cg
->server
, sizeof(cg
->servername
));
711 else if (cups_server
[0] != '/' &&
712 (value
= strrchr(cups_server
, ':')) != NULL
&&
713 !strchr(value
, ']') && isdigit(value
[1] & 255))
718 if (!cg
->ipp_port
&& value
)
719 cg
->ipp_port
= atoi(value
);
724 #ifdef CUPS_DEFAULT_DOMAINSOCKET
726 * If we are compiled with domain socket support, only use the
727 * domain socket if it exists and has the right permissions...
730 struct stat sockinfo
; /* Domain socket information */
732 if (!stat(CUPS_DEFAULT_DOMAINSOCKET
, &sockinfo
) &&
733 (sockinfo
.st_mode
& S_IRWXO
) == S_IRWXO
)
734 cups_server
= CUPS_DEFAULT_DOMAINSOCKET
;
736 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
737 cups_server
= "localhost";
739 cupsSetServer(cups_server
);
744 const char *ipp_port
; /* IPP_PORT environment variable */
745 struct servent
*service
; /* Port number info */
748 if ((ipp_port
= getenv("IPP_PORT")) != NULL
)
750 if ((cg
->ipp_port
= atoi(ipp_port
)) <= 0)
751 cg
->ipp_port
= CUPS_DEFAULT_IPP_PORT
;
753 else if ((service
= getservbyname("ipp", NULL
)) == NULL
||
754 service
->s_port
<= 0)
755 cg
->ipp_port
= CUPS_DEFAULT_IPP_PORT
;
757 cg
->ipp_port
= ntohs(service
->s_port
);
761 if (!cups_gssservicename
)
762 cups_gssservicename
= CUPS_DEFAULT_GSSSERVICENAME
;
764 strlcpy(cg
->gss_service_name
, cups_gssservicename
,
765 sizeof(cg
->gss_service_name
));
766 #endif /* HAVE_GSSAPI */
769 cg
->any_root
= !strcasecmp(cups_anyroot
, "yes") ||
770 !strcasecmp(cups_anyroot
, "on") ||
771 !strcasecmp(cups_anyroot
, "true");
773 if (cups_expiredroot
)
774 cg
->expired_root
= !strcasecmp(cups_expiredroot
, "yes") ||
775 !strcasecmp(cups_expiredroot
, "on") ||
776 !strcasecmp(cups_expiredroot
, "true");
778 if (cups_expiredcerts
)
779 cg
->expired_certs
= !strcasecmp(cups_expiredcerts
, "yes") ||
780 !strcasecmp(cups_expiredcerts
, "on") ||
781 !strcasecmp(cups_expiredcerts
, "true");
786 * End of "$Id: usersys.c 8498 2009-04-13 17:03:15Z mike $".