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 "http-private.h"
56 static void cups_read_client_conf(cups_file_t
*fp
,
58 const char *cups_encryption
,
59 const char *cups_server
);
63 * 'cupsEncryption()' - Get the current encryption settings.
65 * The default encryption setting comes from the CUPS_ENCRYPTION
66 * environment variable, then the ~/.cups/client.conf file, and finally the
67 * /etc/cups/client.conf file. If not set, the default is
68 * @code HTTP_ENCRYPT_IF_REQUESTED@.
70 * Note: The current encryption setting is tracked separately for each thread
71 * in a program. Multi-threaded programs that override the setting via the
72 * @link cupsSetEncryption@ function need to do so in each thread for the same
76 http_encryption_t
/* O - Encryption settings */
79 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
82 if (cg
->encryption
== (http_encryption_t
)-1)
85 return (cg
->encryption
);
90 * 'cupsGetPassword()' - Get a password from the user.
92 * Uses the current password callback function. Returns @code NULL@ if the
93 * user does not provide a password.
95 * Note: The current password callback function is tracked separately for each
96 * thread in a program. Multi-threaded programs that override the setting via
97 * the @link cupsSetPasswordCB@ or @link cupsSetPasswordCB2@ functions need to
98 * do so in each thread for the same function to be used.
101 const char * /* O - Password */
102 cupsGetPassword(const char *prompt
) /* I - Prompt string */
104 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
107 return ((cg
->password_cb
)(prompt
, NULL
, NULL
, NULL
, cg
->password_data
));
112 * 'cupsGetPassword2()' - Get a password from the user using the advanced
115 * Uses the current password callback function. Returns @code NULL@ if the
116 * user does not provide a password.
118 * Note: The current password callback function is tracked separately for each
119 * thread in a program. Multi-threaded programs that override the setting via
120 * the @link cupsSetPasswordCB@ or @link cupsSetPasswordCB2@ functions need to
121 * do so in each thread for the same function to be used.
123 * @since CUPS 1.4/Mac OS X 10.6@
126 const char * /* O - Password */
127 cupsGetPassword2(const char *prompt
, /* I - Prompt string */
128 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
129 const char *method
, /* I - Request method ("GET", "POST", "PUT") */
130 const char *resource
) /* I - Resource path */
132 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
136 http
= _cupsConnect();
138 return ((cg
->password_cb
)(prompt
, http
, method
, resource
, cg
->password_data
));
143 * 'cupsServer()' - Return the hostname/address of the current server.
145 * The default server comes from the CUPS_SERVER environment variable, then the
146 * ~/.cups/client.conf file, and finally the /etc/cups/client.conf file. If not
147 * set, the default is the local system - either "localhost" or a domain socket
150 * The returned value can be a fully-qualified hostname, a numeric IPv4 or IPv6
151 * address, or a domain socket pathname.
153 * Note: The current server is tracked separately for each thread in a program.
154 * Multi-threaded programs that override the server via the
155 * @link cupsSetServer@ function need to do so in each thread for the same
159 const char * /* O - Server name */
162 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
173 * 'cupsSetEncryption()' - Set the encryption preference.
175 * The default encryption setting comes from the CUPS_ENCRYPTION
176 * environment variable, then the ~/.cups/client.conf file, and finally the
177 * /etc/cups/client.conf file. If not set, the default is
178 * @code HTTP_ENCRYPT_IF_REQUESTED@.
180 * Note: The current encryption setting is tracked separately for each thread
181 * in a program. Multi-threaded programs that override the setting need to do
182 * so in each thread for the same setting to be used.
186 cupsSetEncryption(http_encryption_t e
) /* I - New encryption preference */
188 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
194 httpEncryption(cg
->http
, e
);
199 * 'cupsSetPasswordCB()' - Set the password callback for CUPS.
201 * Pass @code NULL@ to restore the default (console) password callback, which
202 * reads the password from the console. Programs should call either this
203 * function or @link cupsSetPasswordCB2@, as only one callback can be registered
204 * by a program per thread.
206 * Note: The current password callback is tracked separately for each thread
207 * in a program. Multi-threaded programs that override the callback need to do
208 * so in each thread for the same callback to be used.
212 cupsSetPasswordCB(cups_password_cb_t cb
)/* I - Callback function */
214 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
217 if (cb
== (cups_password_cb_t
)0)
218 cg
->password_cb
= (cups_password_cb2_t
)_cupsGetPassword
;
220 cg
->password_cb
= (cups_password_cb2_t
)cb
;
222 cg
->password_data
= NULL
;
227 * 'cupsSetPasswordCB2()' - Set the advanced password callback for CUPS.
229 * Pass @code NULL@ to restore the default (console) password callback, which
230 * reads the password from the console. Programs should call either this
231 * function or @link cupsSetPasswordCB2@, as only one callback can be registered
232 * by a program per thread.
234 * Note: The current password callback is tracked separately for each thread
235 * in a program. Multi-threaded programs that override the callback need to do
236 * so in each thread for the same callback to be used.
238 * @since CUPS 1.4/Mac OS X 10.6@
243 cups_password_cb2_t cb
, /* I - Callback function */
244 void *user_data
) /* I - User data pointer */
246 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
249 if (cb
== (cups_password_cb2_t
)0)
250 cg
->password_cb
= (cups_password_cb2_t
)_cupsGetPassword
;
252 cg
->password_cb
= cb
;
254 cg
->password_data
= user_data
;
259 * 'cupsSetServer()' - Set the default server name and port.
261 * The "server" string can be a fully-qualified hostname, a numeric
262 * IPv4 or IPv6 address, or a domain socket pathname. Hostnames and numeric IP
263 * addresses can be optionally followed by a colon and port number to override
264 * the default port 631, e.g. "hostname:8631". Pass @code NULL@ to restore the
265 * default server name and port.
267 * Note: The current server is tracked separately for each thread in a program.
268 * Multi-threaded programs that override the server need to do so in each
269 * thread for the same server to be used.
273 cupsSetServer(const char *server
) /* I - Server name */
275 char *port
; /* Pointer to port */
276 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
281 strlcpy(cg
->server
, server
, sizeof(cg
->server
));
283 if (cg
->server
[0] != '/' && (port
= strrchr(cg
->server
, ':')) != NULL
&&
284 !strchr(port
, ']') && isdigit(port
[1] & 255))
288 cg
->ipp_port
= atoi(port
);
291 if (cg
->server
[0] == '/')
292 strcpy(cg
->servername
, "localhost");
294 strlcpy(cg
->servername
, cg
->server
, sizeof(cg
->servername
));
298 cg
->server
[0] = '\0';
299 cg
->servername
[0] = '\0';
311 * 'cupsSetUser()' - Set the default user name.
313 * Pass @code NULL@ to restore the default user name.
315 * Note: The current user name is tracked separately for each thread in a
316 * program. Multi-threaded programs that override the user name need to do so
317 * in each thread for the same user name to be used.
321 cupsSetUser(const char *user
) /* I - User name */
323 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
327 strlcpy(cg
->user
, user
, sizeof(cg
->user
));
334 * 'cupsUser()' - Return the current user's name.
336 * Note: The current user name is tracked separately for each thread in a
337 * program. Multi-threaded programs that override the user name with the
338 * @link cupsSetUser@ function need to do so in each thread for the same user
342 const char * /* O - User name */
345 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
352 * Get the current user name from the OS...
355 DWORD size
; /* Size of string */
357 size
= sizeof(cg
->user
);
358 if (!GetUserName(cg
->user
, &size
))
361 * Get the user name corresponding to the current UID...
364 struct passwd
*pwd
; /* User/password entry */
367 if ((pwd
= getpwuid(getuid())) != NULL
)
373 strlcpy(cg
->user
, pwd
->pw_name
, sizeof(cg
->user
));
379 * Use the default "unknown" user name...
382 strcpy(cg
->user
, "unknown");
391 * '_cupsGetPassword()' - Get a password from the user.
394 const char * /* O - Password */
395 _cupsGetPassword(const char *prompt
) /* I - Prompt string */
399 * Currently no console password support is provided on Windows.
406 * Use the standard getpass function to get a password from the console.
409 return (getpass(prompt
));
415 * '_cupsSetDefaults()' - Set the default server, port, and encryption.
419 _cupsSetDefaults(void)
421 cups_file_t
*fp
; /* File */
422 const char *home
, /* Home directory of user */
423 *cups_encryption
, /* CUPS_ENCRYPTION env var */
424 *cups_server
; /* CUPS_SERVER env var */
425 char filename
[1024]; /* Filename */
426 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
429 DEBUG_puts("_cupsSetDefaults()");
432 * First collect environment variables...
435 cups_encryption
= getenv("CUPS_ENCRYPTION");
436 cups_server
= getenv("CUPS_SERVER");
439 * Then, if needed, the .cups/client.conf or .cupsrc file in the home
443 if ((cg
->encryption
== (http_encryption_t
)-1 || !cg
->server
[0] ||
444 !cg
->ipp_port
) && (home
= getenv("HOME")) != NULL
)
447 * Look for ~/.cups/client.conf...
450 snprintf(filename
, sizeof(filename
), "%s/.cups/client.conf", home
);
451 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
453 cups_read_client_conf(fp
, cg
, cups_encryption
, cups_server
);
458 if (cg
->encryption
== (http_encryption_t
)-1 || !cg
->server
[0] ||
462 * Look for CUPS_SERVERROOT/client.conf...
465 snprintf(filename
, sizeof(filename
), "%s/client.conf", cg
->cups_serverroot
);
466 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
468 cups_read_client_conf(fp
, cg
, cups_encryption
, cups_server
);
474 * If we still have things that aren't set, use the compiled in defaults...
477 if (cg
->encryption
== (http_encryption_t
)-1)
478 cg
->encryption
= HTTP_ENCRYPT_IF_REQUESTED
;
484 #ifdef CUPS_DEFAULT_DOMAINSOCKET
486 * If we are compiled with domain socket support, only use the
487 * domain socket if it exists and has the right permissions...
490 struct stat sockinfo
; /* Domain socket information */
492 if (!stat(CUPS_DEFAULT_DOMAINSOCKET
, &sockinfo
) &&
493 (sockinfo
.st_mode
& S_IRWXO
) == S_IRWXO
)
494 cups_server
= CUPS_DEFAULT_DOMAINSOCKET
;
496 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
497 cups_server
= "localhost";
500 cupsSetServer(cups_server
);
505 const char *ipp_port
; /* IPP_PORT environment variable */
506 struct servent
*service
; /* Port number info */
509 if ((ipp_port
= getenv("IPP_PORT")) != NULL
)
511 if ((cg
->ipp_port
= atoi(ipp_port
)) <= 0)
512 cg
->ipp_port
= CUPS_DEFAULT_IPP_PORT
;
514 else if ((service
= getservbyname("ipp", NULL
)) == NULL
||
515 service
->s_port
<= 0)
516 cg
->ipp_port
= CUPS_DEFAULT_IPP_PORT
;
518 cg
->ipp_port
= ntohs(service
->s_port
);
524 * 'cups_read_client_conf()' - Read a client.conf file.
528 cups_read_client_conf(
529 cups_file_t
*fp
, /* I - File to read */
530 _cups_globals_t
*cg
, /* I - Global data */
531 const char *cups_encryption
, /* I - CUPS_ENCRYPTION env var */
532 const char *cups_server
) /* I - CUPS_SERVER env var */
534 int linenum
; /* Current line number */
535 char line
[1024], /* Line from file */
536 *value
, /* Pointer into line */
537 encryption
[1024], /* Encryption value */
538 server_name
[1024]; /* ServerName value */
542 * Read from the file...
546 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
548 if (!cups_encryption
&& cg
->encryption
== (http_encryption_t
)-1 &&
549 !strcasecmp(line
, "Encryption") && value
)
551 strlcpy(encryption
, value
, sizeof(encryption
));
552 cups_encryption
= encryption
;
554 else if (!cups_server
&& (!cg
->server
[0] || !cg
->ipp_port
) &&
555 !strcasecmp(line
, "ServerName") && value
)
557 strlcpy(server_name
, value
, sizeof(server_name
));
558 cups_server
= server_name
;
566 if (cg
->encryption
== (http_encryption_t
)-1 && cups_encryption
)
568 if (!strcasecmp(cups_encryption
, "never"))
569 cg
->encryption
= HTTP_ENCRYPT_NEVER
;
570 else if (!strcasecmp(cups_encryption
, "always"))
571 cg
->encryption
= HTTP_ENCRYPT_ALWAYS
;
572 else if (!strcasecmp(cups_encryption
, "required"))
573 cg
->encryption
= HTTP_ENCRYPT_REQUIRED
;
575 cg
->encryption
= HTTP_ENCRYPT_IF_REQUESTED
;
578 if ((!cg
->server
[0] || !cg
->ipp_port
) && cups_server
)
583 * Copy server name...
586 strlcpy(cg
->server
, cups_server
, sizeof(cg
->server
));
588 if (cg
->server
[0] != '/' && (value
= strrchr(cg
->server
, ':')) != NULL
&&
589 !strchr(value
, ']') && isdigit(value
[1] & 255))
594 if (cg
->server
[0] == '/')
595 strcpy(cg
->servername
, "localhost");
597 strlcpy(cg
->servername
, cg
->server
, sizeof(cg
->servername
));
599 else if (cups_server
[0] != '/' &&
600 (value
= strrchr(cups_server
, ':')) != NULL
&&
601 !strchr(value
, ']') && isdigit(value
[1] & 255))
606 if (!cg
->ipp_port
&& value
)
607 cg
->ipp_port
= atoi(value
);
613 * End of "$Id: usersys.c 8498 2009-04-13 17:03:15Z mike $".