2 * "$Id: usersys.c 6649 2007-07-11 21:46:42Z mike $"
4 * User, system, and password routines for the Common UNIX Printing
7 * Copyright 2007 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 ~/.cupsrc file, and finally the
58 * /etc/cups/client.conf file. If not set, the default is
59 * 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 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 _cupsGlobals()->encryption
= e
;
159 * 'cupsServer()' - Return the hostname/address of the default server.
161 * The returned value can be a fully-qualified hostname, a numeric
162 * IPv4 or IPv6 address, or a domain socket pathname.
165 const char * /* O - Server name */
168 cups_file_t
*fp
; /* client.conf file */
169 char *server
; /* Pointer to server name */
170 char *port
; /* Port number */
171 char line
[1024], /* Line from file */
172 *value
; /* Value on line */
173 int linenum
; /* Line number in file */
174 #ifdef CUPS_DEFAULT_DOMAINSOCKET
175 struct stat sockinfo
; /* Domain socket information */
176 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
177 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
181 * First see if we have already set the server name...
187 * Then see if the CUPS_SERVER environment variable is set...
190 if ((server
= getenv("CUPS_SERVER")) == NULL
)
193 * No environment variable, try the client.conf file...
196 fp
= cups_open_client_conf();
198 #ifdef CUPS_DEFAULT_DOMAINSOCKET
200 * If we are compiled with domain socket support, only use the
201 * domain socket if it exists and has the right permissions...
204 if (!stat(CUPS_DEFAULT_DOMAINSOCKET
, &sockinfo
) &&
205 (sockinfo
.st_mode
& S_IRWXO
) == S_IRWXO
)
206 server
= CUPS_DEFAULT_DOMAINSOCKET
;
208 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
209 server
= "localhost";
214 * Read the config file and look for a ServerName line...
218 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
) != NULL
)
220 DEBUG_printf(("cupsServer: %d: %s %s\n", linenum
, line
,
221 value
? value
: "(null)"));
223 if (!strcasecmp(line
, "ServerName") && value
)
229 DEBUG_puts("cupsServer: Got a ServerName line!");
240 * Copy the server name over and set the port number, if any...
243 DEBUG_printf(("cupsServer: Using server \"%s\"...\n", server
));
245 strlcpy(cg
->server
, server
, sizeof(cg
->server
));
247 if (cg
->server
[0] != '/' && (port
= strrchr(cg
->server
, ':')) != NULL
&&
248 !strchr(port
, ']') && isdigit(port
[1] & 255))
252 DEBUG_printf(("cupsServer: Using port %d...\n", atoi(port
)));
253 ippSetPort(atoi(port
));
256 if (cg
->server
[0] == '/')
257 strcpy(cg
->servername
, "localhost");
259 strlcpy(cg
->servername
, cg
->server
, sizeof(cg
->servername
));
267 * 'cupsSetPasswordCB()' - Set the password callback for CUPS.
269 * Pass NULL to restore the default (console) password callback.
273 cupsSetPasswordCB(cups_password_cb_t cb
)/* I - Callback function */
275 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
278 if (cb
== (const char *(*)(const char *))0)
279 cg
->password_cb
= _cupsGetPassword
;
281 cg
->password_cb
= cb
;
286 * 'cupsSetServer()' - Set the default server name.
288 * The "server" string can be a fully-qualified hostname, a numeric
289 * IPv4 or IPv6 address, or a domain socket pathname. Pass NULL to
290 * restore the default server name.
294 cupsSetServer(const char *server
) /* I - Server name */
296 char *port
; /* Pointer to port */
297 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
302 strlcpy(cg
->server
, server
, sizeof(cg
->server
));
304 if (cg
->server
[0] != '/' && (port
= strrchr(cg
->server
, ':')) != NULL
&&
305 !strchr(port
, ']') && isdigit(port
[1] & 255))
309 ippSetPort(atoi(port
));
312 if (cg
->server
[0] == '/')
313 strcpy(cg
->servername
, "localhost");
315 strlcpy(cg
->servername
, cg
->server
, sizeof(cg
->servername
));
319 cg
->server
[0] = '\0';
320 cg
->servername
[0] = '\0';
326 * 'cupsSetUser()' - Set the default user name.
328 * Pass NULL to restore the default user name.
332 cupsSetUser(const char *user
) /* I - User name */
334 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
338 strlcpy(cg
->user
, user
, sizeof(cg
->user
));
346 * WIN32 username and password stuff.
350 * 'cupsUser()' - Return the current user's name.
353 const char * /* O - User name */
356 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
361 DWORD size
; /* Size of string */
364 size
= sizeof(cg
->user
);
365 if (!GetUserName(cg
->user
, &size
))
368 * Use the default username...
371 strcpy(cg
->user
, "unknown");
380 * '_cupsGetPassword()' - Get a password from the user.
383 const char * /* O - Password */
384 _cupsGetPassword(const char *prompt
) /* I - Prompt string */
390 * UNIX username and password stuff...
396 * 'cupsUser()' - Return the current user's name.
399 const char * /* O - User name */
402 struct passwd
*pwd
; /* User/password entry */
403 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
409 * Rewind the password file...
415 * Lookup the password entry for the current user.
418 if ((pwd
= getpwuid(getuid())) == NULL
)
419 strcpy(cg
->user
, "unknown"); /* Unknown user! */
423 * Copy the username...
428 strlcpy(cg
->user
, pwd
->pw_name
, sizeof(cg
->user
));
432 * Rewind the password file again...
443 * '_cupsGetPassword()' - Get a password from the user.
446 const char * /* O - Password */
447 _cupsGetPassword(const char *prompt
) /* I - Prompt string */
449 return (getpass(prompt
));
455 * 'cups_open_client_conf()' - Open the client.conf file.
458 static cups_file_t
* /* O - File or NULL */
459 cups_open_client_conf(void)
461 cups_file_t
*fp
; /* File */
462 const char *home
; /* Home directory of user */
463 char filename
[1024]; /* Filename */
464 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
467 if ((home
= getenv("HOME")) != NULL
)
470 * Look for ~/.cups/client.conf or ~/.cupsrc...
473 snprintf(filename
, sizeof(filename
), "%s/.cups/client.conf", home
);
474 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
476 DEBUG_printf(("cups_open_client_conf: Using \"%s\"...\n", filename
));
480 snprintf(filename
, sizeof(filename
), "%s/.cupsrc", home
);
481 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
483 DEBUG_printf(("cups_open_client_conf: Using \"%s\"...\n", filename
));
488 snprintf(filename
, sizeof(filename
), "%s/client.conf", cg
->cups_serverroot
);
489 return (cupsFileOpen(filename
, "r"));
494 * End of "$Id: usersys.c 6649 2007-07-11 21:46:42Z mike $".