2 * "$Id: usersys.c 7337 2008-02-22 04:44:04Z mike $"
4 * User, system, and password routines for the Common UNIX Printing
7 * Copyright 2007-2008 by Apple Inc.
8 * Copyright 1997-2006 by Easy Software Products.
10 * These coded instructions, statements, and computer programs are the
11 * property of Apple Inc. and are protected by Federal copyright
12 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
13 * which should have been included with this file. If this file is
14 * file is missing or damaged, see the license at "http://www.cups.org/".
16 * This file is subject to the Apple OS-Developed Software exception.
20 * cupsEncryption() - Get the default encryption settings.
21 * cupsGetPassword() - Get a password from the user.
22 * cupsServer() - Return the hostname of the default server.
23 * cupsSetEncryption() - Set the encryption preference.
24 * cupsSetPasswordCB() - Set the password callback for CUPS.
25 * cupsSetServer() - Set the default server name.
26 * cupsSetUser() - Set the default user name.
27 * cupsUser() - Return the current users name.
28 * _cupsGetPassword() - Get a password from the user.
29 * cups_open_client_conf() - Open the client.conf file.
33 * Include necessary headers...
36 #include "http-private.h"
50 static cups_file_t
*cups_open_client_conf(void);
54 * 'cupsEncryption()' - Get the default encryption settings.
56 * The default encryption setting comes from the CUPS_ENCRYPTION
57 * environment variable, then the ~/.cups/client.conf file, and finally the
58 * /etc/cups/client.conf file. If not set, the default is
59 * @code HTTP_ENCRYPT_IF_REQUESTED@.
62 http_encryption_t
/* O - Encryption settings */
65 cups_file_t
*fp
; /* client.conf file */
66 char *encryption
; /* CUPS_ENCRYPTION variable */
67 char line
[1024], /* Line from file */
68 *value
; /* Value on line */
69 int linenum
; /* Line number */
70 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
74 * First see if we have already set the encryption stuff...
77 if (cg
->encryption
== (http_encryption_t
)-1)
80 * Then see if the CUPS_ENCRYPTION environment variable is set...
83 if ((encryption
= getenv("CUPS_ENCRYPTION")) == NULL
)
86 * No, open the client.conf file...
89 fp
= cups_open_client_conf();
90 encryption
= "IfRequested";
95 * Read the config file and look for an Encryption line...
100 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
) != NULL
)
101 if (!strcasecmp(line
, "Encryption") && value
)
116 * Set the encryption preference...
119 if (!strcasecmp(encryption
, "never"))
120 cg
->encryption
= HTTP_ENCRYPT_NEVER
;
121 else if (!strcasecmp(encryption
, "always"))
122 cg
->encryption
= HTTP_ENCRYPT_ALWAYS
;
123 else if (!strcasecmp(encryption
, "required"))
124 cg
->encryption
= HTTP_ENCRYPT_REQUIRED
;
126 cg
->encryption
= HTTP_ENCRYPT_IF_REQUESTED
;
129 return (cg
->encryption
);
134 * 'cupsGetPassword()' - Get a password from the user.
136 * Uses the current password callback function. Returns @code NULL@ if the
137 * user does not provide a password.
140 const char * /* O - Password */
141 cupsGetPassword(const char *prompt
) /* I - Prompt string */
143 return ((*_cupsGlobals()->password_cb
)(prompt
));
148 * 'cupsSetEncryption()' - Set the encryption preference.
152 cupsSetEncryption(http_encryption_t e
) /* I - New encryption preference */
154 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
160 httpEncryption(cg
->http
, e
);
165 * 'cupsServer()' - Return the hostname/address of the default server.
167 * The returned value can be a fully-qualified hostname, a numeric
168 * IPv4 or IPv6 address, or a domain socket pathname.
171 const char * /* O - Server name */
174 cups_file_t
*fp
; /* client.conf file */
175 char *server
; /* Pointer to server name */
176 char *port
; /* Port number */
177 char line
[1024], /* Line from file */
178 *value
; /* Value on line */
179 int linenum
; /* Line number in file */
180 #ifdef CUPS_DEFAULT_DOMAINSOCKET
181 struct stat sockinfo
; /* Domain socket information */
182 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
183 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
187 * First see if we have already set the server name...
193 * Then see if the CUPS_SERVER environment variable is set...
196 if ((server
= getenv("CUPS_SERVER")) == NULL
)
199 * No environment variable, try the client.conf file...
202 fp
= cups_open_client_conf();
204 #ifdef CUPS_DEFAULT_DOMAINSOCKET
206 * If we are compiled with domain socket support, only use the
207 * domain socket if it exists and has the right permissions...
210 if (!stat(CUPS_DEFAULT_DOMAINSOCKET
, &sockinfo
) &&
211 (sockinfo
.st_mode
& S_IRWXO
) == S_IRWXO
)
212 server
= CUPS_DEFAULT_DOMAINSOCKET
;
214 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
215 server
= "localhost";
220 * Read the config file and look for a ServerName line...
224 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
) != NULL
)
226 DEBUG_printf(("cupsServer: %d: %s %s\n", linenum
, line
,
227 value
? value
: "(null)"));
229 if (!strcasecmp(line
, "ServerName") && value
)
235 DEBUG_puts("cupsServer: Got a ServerName line!");
246 * Copy the server name over and set the port number, if any...
249 DEBUG_printf(("cupsServer: Using server \"%s\"...\n", server
));
251 strlcpy(cg
->server
, server
, sizeof(cg
->server
));
253 if (cg
->server
[0] != '/' && (port
= strrchr(cg
->server
, ':')) != NULL
&&
254 !strchr(port
, ']') && isdigit(port
[1] & 255))
258 DEBUG_printf(("cupsServer: Using port %d...\n", atoi(port
)));
259 ippSetPort(atoi(port
));
262 if (cg
->server
[0] == '/')
263 strcpy(cg
->servername
, "localhost");
265 strlcpy(cg
->servername
, cg
->server
, sizeof(cg
->servername
));
273 * 'cupsSetPasswordCB()' - Set the password callback for CUPS.
275 * Pass @code NULL@ to restore the default (console) password callback.
279 cupsSetPasswordCB(cups_password_cb_t cb
)/* I - Callback function */
281 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
284 if (cb
== (const char *(*)(const char *))0)
285 cg
->password_cb
= _cupsGetPassword
;
287 cg
->password_cb
= cb
;
292 * 'cupsSetServer()' - Set the default server name.
294 * The "server" string can be a fully-qualified hostname, a numeric
295 * IPv4 or IPv6 address, or a domain socket pathname. Pass @code NULL@ to
296 * restore the default server name.
300 cupsSetServer(const char *server
) /* I - Server name */
302 char *port
; /* Pointer to port */
303 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
308 strlcpy(cg
->server
, server
, sizeof(cg
->server
));
310 if (cg
->server
[0] != '/' && (port
= strrchr(cg
->server
, ':')) != NULL
&&
311 !strchr(port
, ']') && isdigit(port
[1] & 255))
315 ippSetPort(atoi(port
));
318 if (cg
->server
[0] == '/')
319 strcpy(cg
->servername
, "localhost");
321 strlcpy(cg
->servername
, cg
->server
, sizeof(cg
->servername
));
325 cg
->server
[0] = '\0';
326 cg
->servername
[0] = '\0';
338 * 'cupsSetUser()' - Set the default user name.
340 * Pass @code NULL@ to restore the default user name.
344 cupsSetUser(const char *user
) /* I - User name */
346 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
350 strlcpy(cg
->user
, user
, sizeof(cg
->user
));
358 * WIN32 username and password stuff.
362 * 'cupsUser()' - Return the current user's name.
365 const char * /* O - User name */
368 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
373 DWORD size
; /* Size of string */
376 size
= sizeof(cg
->user
);
377 if (!GetUserName(cg
->user
, &size
))
380 * Use the default username...
383 strcpy(cg
->user
, "unknown");
392 * '_cupsGetPassword()' - Get a password from the user.
395 const char * /* O - Password */
396 _cupsGetPassword(const char *prompt
) /* I - Prompt string */
402 * UNIX username and password stuff...
408 * 'cupsUser()' - Return the current user's name.
411 const char * /* O - User name */
414 struct passwd
*pwd
; /* User/password entry */
415 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
421 * Rewind the password file...
427 * Lookup the password entry for the current user.
430 if ((pwd
= getpwuid(getuid())) == NULL
)
431 strcpy(cg
->user
, "unknown"); /* Unknown user! */
435 * Copy the username...
440 strlcpy(cg
->user
, pwd
->pw_name
, sizeof(cg
->user
));
444 * Rewind the password file again...
455 * '_cupsGetPassword()' - Get a password from the user.
458 const char * /* O - Password */
459 _cupsGetPassword(const char *prompt
) /* I - Prompt string */
461 return (getpass(prompt
));
467 * 'cups_open_client_conf()' - Open the client.conf file.
470 static cups_file_t
* /* O - File or NULL */
471 cups_open_client_conf(void)
473 cups_file_t
*fp
; /* File */
474 const char *home
; /* Home directory of user */
475 char filename
[1024]; /* Filename */
476 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
479 if ((home
= getenv("HOME")) != NULL
)
482 * Look for ~/.cups/client.conf or ~/.cupsrc...
485 snprintf(filename
, sizeof(filename
), "%s/.cups/client.conf", home
);
486 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
488 DEBUG_printf(("cups_open_client_conf: Using \"%s\"...\n", filename
));
492 snprintf(filename
, sizeof(filename
), "%s/.cupsrc", home
);
493 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
495 DEBUG_printf(("cups_open_client_conf: Using \"%s\"...\n", filename
));
500 snprintf(filename
, sizeof(filename
), "%s/client.conf", cg
->cups_serverroot
);
501 return (cupsFileOpen(filename
, "r"));
506 * End of "$Id: usersys.c 7337 2008-02-22 04:44:04Z mike $".