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 * cupsSetEncryption() - Set the encryption preference.
26 * cupsSetPasswordCB() - Set the password callback for CUPS.
27 * cupsSetPasswordCB2() - Set the advanced password callback for CUPS.
28 * cupsSetServer() - Set the default server name and port.
29 * cupsSetUser() - Set the default user name.
30 * cupsUser() - Return the current user's name.
31 * _cupsGetPassword() - Get a password from the user.
32 * _cupsSetDefaults() - Set the default server, port, and encryption.
33 * cups_read_client_conf() - Read a client.conf file.
37 * Include necessary headers...
40 #include "cups-private.h"
54 static void cups_read_client_conf(cups_file_t
*fp
,
56 const char *cups_encryption
,
57 const char *cups_server
);
61 * 'cupsEncryption()' - Get the current encryption settings.
63 * The default encryption setting comes from the CUPS_ENCRYPTION
64 * environment variable, then the ~/.cups/client.conf file, and finally the
65 * /etc/cups/client.conf file. If not set, the default is
66 * @code HTTP_ENCRYPT_IF_REQUESTED@.
68 * Note: The current encryption setting is tracked separately for each thread
69 * in a program. Multi-threaded programs that override the setting via the
70 * @link cupsSetEncryption@ function need to do so in each thread for the same
74 http_encryption_t
/* O - Encryption settings */
77 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
80 if (cg
->encryption
== (http_encryption_t
)-1)
83 return (cg
->encryption
);
88 * 'cupsGetPassword()' - Get a password from the user.
90 * Uses the current password callback function. Returns @code NULL@ if the
91 * user does not provide a password.
93 * Note: The current password callback function is tracked separately for each
94 * thread in a program. Multi-threaded programs that override the setting via
95 * the @link cupsSetPasswordCB@ or @link cupsSetPasswordCB2@ functions need to
96 * do so in each thread for the same function to be used.
99 const char * /* O - Password */
100 cupsGetPassword(const char *prompt
) /* I - Prompt string */
102 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
105 return ((cg
->password_cb
)(prompt
, NULL
, NULL
, NULL
, cg
->password_data
));
110 * 'cupsGetPassword2()' - Get a password from the user using the advanced
113 * Uses the current password callback function. Returns @code NULL@ if the
114 * user does not provide a password.
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.
121 * @since CUPS 1.4/Mac OS X 10.6@
124 const char * /* O - Password */
125 cupsGetPassword2(const char *prompt
, /* I - Prompt string */
126 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
127 const char *method
, /* I - Request method ("GET", "POST", "PUT") */
128 const char *resource
) /* I - Resource path */
130 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
134 http
= _cupsConnect();
136 return ((cg
->password_cb
)(prompt
, http
, method
, resource
, cg
->password_data
));
141 * 'cupsServer()' - Return the hostname/address of the current server.
143 * The default server comes from the CUPS_SERVER environment variable, then the
144 * ~/.cups/client.conf file, and finally the /etc/cups/client.conf file. If not
145 * set, the default is the local system - either "localhost" or a domain socket
148 * The returned value can be a fully-qualified hostname, a numeric IPv4 or IPv6
149 * address, or a domain socket pathname.
151 * Note: The current server is tracked separately for each thread in a program.
152 * Multi-threaded programs that override the server via the
153 * @link cupsSetServer@ function need to do so in each thread for the same
157 const char * /* O - Server name */
160 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
171 * 'cupsSetEncryption()' - Set the encryption preference.
173 * The default encryption setting comes from the CUPS_ENCRYPTION
174 * environment variable, then the ~/.cups/client.conf file, and finally the
175 * /etc/cups/client.conf file. If not set, the default is
176 * @code HTTP_ENCRYPT_IF_REQUESTED@.
178 * Note: The current encryption setting is tracked separately for each thread
179 * in a program. Multi-threaded programs that override the setting need to do
180 * so in each thread for the same setting to be used.
184 cupsSetEncryption(http_encryption_t e
) /* I - New encryption preference */
186 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
192 httpEncryption(cg
->http
, e
);
197 * 'cupsSetPasswordCB()' - Set the password callback for CUPS.
199 * Pass @code NULL@ to restore the default (console) password callback, which
200 * reads the password from the console. Programs should call either this
201 * function or @link cupsSetPasswordCB2@, as only one callback can be registered
202 * by a program per thread.
204 * Note: The current password callback is tracked separately for each thread
205 * in a program. Multi-threaded programs that override the callback need to do
206 * so in each thread for the same callback to be used.
210 cupsSetPasswordCB(cups_password_cb_t cb
)/* I - Callback function */
212 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
215 if (cb
== (cups_password_cb_t
)0)
216 cg
->password_cb
= (cups_password_cb2_t
)_cupsGetPassword
;
218 cg
->password_cb
= (cups_password_cb2_t
)cb
;
220 cg
->password_data
= NULL
;
225 * 'cupsSetPasswordCB2()' - Set the advanced password callback for CUPS.
227 * Pass @code NULL@ to restore the default (console) password callback, which
228 * reads the password from the console. Programs should call either this
229 * function or @link cupsSetPasswordCB2@, as only one callback can be registered
230 * by a program per thread.
232 * Note: The current password callback is tracked separately for each thread
233 * in a program. Multi-threaded programs that override the callback need to do
234 * so in each thread for the same callback to be used.
236 * @since CUPS 1.4/Mac OS X 10.6@
241 cups_password_cb2_t cb
, /* I - Callback function */
242 void *user_data
) /* I - User data pointer */
244 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
247 if (cb
== (cups_password_cb2_t
)0)
248 cg
->password_cb
= (cups_password_cb2_t
)_cupsGetPassword
;
250 cg
->password_cb
= cb
;
252 cg
->password_data
= user_data
;
257 * 'cupsSetServer()' - Set the default server name and port.
259 * The "server" string can be a fully-qualified hostname, a numeric
260 * IPv4 or IPv6 address, or a domain socket pathname. Hostnames and numeric IP
261 * addresses can be optionally followed by a colon and port number to override
262 * the default port 631, e.g. "hostname:8631". Pass @code NULL@ to restore the
263 * default server name and port.
265 * Note: The current server is tracked separately for each thread in a program.
266 * Multi-threaded programs that override the server need to do so in each
267 * thread for the same server to be used.
271 cupsSetServer(const char *server
) /* I - Server name */
273 char *port
; /* Pointer to port */
274 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
279 strlcpy(cg
->server
, server
, sizeof(cg
->server
));
281 if (cg
->server
[0] != '/' && (port
= strrchr(cg
->server
, ':')) != NULL
&&
282 !strchr(port
, ']') && isdigit(port
[1] & 255))
286 cg
->ipp_port
= atoi(port
);
289 if (cg
->server
[0] == '/')
290 strcpy(cg
->servername
, "localhost");
292 strlcpy(cg
->servername
, cg
->server
, sizeof(cg
->servername
));
296 cg
->server
[0] = '\0';
297 cg
->servername
[0] = '\0';
309 * 'cupsSetUser()' - Set the default user name.
311 * Pass @code NULL@ to restore the default user name.
313 * Note: The current user name is tracked separately for each thread in a
314 * program. Multi-threaded programs that override the user name need to do so
315 * in each thread for the same user name to be used.
319 cupsSetUser(const char *user
) /* I - User name */
321 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
325 strlcpy(cg
->user
, user
, sizeof(cg
->user
));
332 * 'cupsUser()' - Return the current user's name.
334 * Note: The current user name is tracked separately for each thread in a
335 * program. Multi-threaded programs that override the user name with the
336 * @link cupsSetUser@ function need to do so in each thread for the same user
340 const char * /* O - User name */
343 const char *user
; /* USER environment variable */
344 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
351 * Get the current user name from the OS...
354 DWORD size
; /* Size of string */
356 size
= sizeof(cg
->user
);
357 if (!GetUserName(cg
->user
, &size
))
360 * Get the user name corresponding to the current UID...
363 struct passwd
*pwd
; /* User/password entry */
366 if ((pwd
= getpwuid(getuid())) != NULL
)
372 strlcpy(cg
->user
, pwd
->pw_name
, sizeof(cg
->user
));
376 if ((user
= getenv("USER")) != NULL
)
379 * Use the username from the "USER" environment variable...
381 strlcpy(cg
->user
, user
, sizeof(cg
->user
));
386 * Use the default "unknown" user name...
389 strcpy(cg
->user
, "unknown");
398 * '_cupsGetPassword()' - Get a password from the user.
401 const char * /* O - Password */
402 _cupsGetPassword(const char *prompt
) /* I - Prompt string */
406 * Currently no console password support is provided on Windows.
413 * Use the standard getpass function to get a password from the console.
416 return (getpass(prompt
));
422 * '_cupsSetDefaults()' - Set the default server, port, and encryption.
426 _cupsSetDefaults(void)
428 cups_file_t
*fp
; /* File */
429 const char *home
, /* Home directory of user */
430 *cups_encryption
, /* CUPS_ENCRYPTION env var */
431 *cups_server
; /* CUPS_SERVER env var */
432 char filename
[1024]; /* Filename */
433 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
436 DEBUG_puts("_cupsSetDefaults()");
439 * First collect environment variables...
442 cups_encryption
= getenv("CUPS_ENCRYPTION");
443 cups_server
= getenv("CUPS_SERVER");
446 * Then, if needed, the .cups/client.conf or .cupsrc file in the home
450 if ((cg
->encryption
== (http_encryption_t
)-1 || !cg
->server
[0] ||
451 !cg
->ipp_port
) && (home
= getenv("HOME")) != NULL
)
454 * Look for ~/.cups/client.conf...
457 snprintf(filename
, sizeof(filename
), "%s/.cups/client.conf", home
);
458 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
460 cups_read_client_conf(fp
, cg
, cups_encryption
, cups_server
);
465 if (cg
->encryption
== (http_encryption_t
)-1 || !cg
->server
[0] ||
469 * Look for CUPS_SERVERROOT/client.conf...
472 snprintf(filename
, sizeof(filename
), "%s/client.conf", cg
->cups_serverroot
);
473 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
475 cups_read_client_conf(fp
, cg
, cups_encryption
, cups_server
);
481 * If we still have things that aren't set, use the compiled in defaults...
484 if (cg
->encryption
== (http_encryption_t
)-1)
485 cg
->encryption
= HTTP_ENCRYPT_IF_REQUESTED
;
491 #ifdef CUPS_DEFAULT_DOMAINSOCKET
493 * If we are compiled with domain socket support, only use the
494 * domain socket if it exists and has the right permissions...
497 struct stat sockinfo
; /* Domain socket information */
499 if (!stat(CUPS_DEFAULT_DOMAINSOCKET
, &sockinfo
) &&
500 (sockinfo
.st_mode
& S_IRWXO
) == S_IRWXO
)
501 cups_server
= CUPS_DEFAULT_DOMAINSOCKET
;
503 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
504 cups_server
= "localhost";
507 cupsSetServer(cups_server
);
512 const char *ipp_port
; /* IPP_PORT environment variable */
513 struct servent
*service
; /* Port number info */
516 if ((ipp_port
= getenv("IPP_PORT")) != NULL
)
518 if ((cg
->ipp_port
= atoi(ipp_port
)) <= 0)
519 cg
->ipp_port
= CUPS_DEFAULT_IPP_PORT
;
521 else if ((service
= getservbyname("ipp", NULL
)) == NULL
||
522 service
->s_port
<= 0)
523 cg
->ipp_port
= CUPS_DEFAULT_IPP_PORT
;
525 cg
->ipp_port
= ntohs(service
->s_port
);
531 * 'cups_read_client_conf()' - Read a client.conf file.
535 cups_read_client_conf(
536 cups_file_t
*fp
, /* I - File to read */
537 _cups_globals_t
*cg
, /* I - Global data */
538 const char *cups_encryption
, /* I - CUPS_ENCRYPTION env var */
539 const char *cups_server
) /* I - CUPS_SERVER env var */
541 int linenum
; /* Current line number */
542 char line
[1024], /* Line from file */
543 *value
, /* Pointer into line */
544 encryption
[1024], /* Encryption value */
545 server_name
[1024]; /* ServerName value */
549 * Read from the file...
553 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
555 if (!cups_encryption
&& cg
->encryption
== (http_encryption_t
)-1 &&
556 !strcasecmp(line
, "Encryption") && value
)
558 strlcpy(encryption
, value
, sizeof(encryption
));
559 cups_encryption
= encryption
;
561 else if (!cups_server
&& (!cg
->server
[0] || !cg
->ipp_port
) &&
562 !strcasecmp(line
, "ServerName") && value
)
564 strlcpy(server_name
, value
, sizeof(server_name
));
565 cups_server
= server_name
;
573 if (cg
->encryption
== (http_encryption_t
)-1 && cups_encryption
)
575 if (!strcasecmp(cups_encryption
, "never"))
576 cg
->encryption
= HTTP_ENCRYPT_NEVER
;
577 else if (!strcasecmp(cups_encryption
, "always"))
578 cg
->encryption
= HTTP_ENCRYPT_ALWAYS
;
579 else if (!strcasecmp(cups_encryption
, "required"))
580 cg
->encryption
= HTTP_ENCRYPT_REQUIRED
;
582 cg
->encryption
= HTTP_ENCRYPT_IF_REQUESTED
;
585 if ((!cg
->server
[0] || !cg
->ipp_port
) && cups_server
)
590 * Copy server name...
593 strlcpy(cg
->server
, cups_server
, sizeof(cg
->server
));
595 if (cg
->server
[0] != '/' && (value
= strrchr(cg
->server
, ':')) != NULL
&&
596 !strchr(value
, ']') && isdigit(value
[1] & 255))
601 if (cg
->server
[0] == '/')
602 strcpy(cg
->servername
, "localhost");
604 strlcpy(cg
->servername
, cg
->server
, sizeof(cg
->servername
));
606 else if (cups_server
[0] != '/' &&
607 (value
= strrchr(cups_server
, ':')) != NULL
&&
608 !strchr(value
, ']') && isdigit(value
[1] & 255))
613 if (!cg
->ipp_port
&& value
)
614 cg
->ipp_port
= atoi(value
);
620 * End of "$Id: usersys.c 8498 2009-04-13 17:03:15Z mike $".