2 * "$Id: usersys.c 5041 2006-02-01 16:54:50Z mike $"
4 * User, system, and password routines for the Common UNIX Printing
7 * Copyright 1997-2006 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Easy Software Products and are protected by Federal
11 * copyright law. Distribution and use rights are outlined in the file
12 * "LICENSE.txt" which should have been included with this file. If this
13 * file is missing or damaged please contact Easy Software Products
16 * Attn: CUPS Licensing Information
17 * Easy Software Products
18 * 44141 Airport View Drive, Suite 204
19 * Hollywood, Maryland 20636 USA
21 * Voice: (301) 373-9600
22 * EMail: cups-info@cups.org
23 * WWW: http://www.cups.org
25 * This file is subject to the Apple OS-Developed Software exception.
29 * cupsEncryption() - Get the default encryption settings.
30 * cupsGetPassword() - Get a password from the user.
31 * cupsServer() - Return the hostname of the default server.
32 * cupsSetEncryption() - Set the encryption preference.
33 * cupsSetPasswordCB() - Set the password callback for CUPS.
34 * cupsSetServer() - Set the default server name.
35 * cupsSetUser() - Set the default user name.
36 * cupsUser() - Return the current users name.
37 * _cupsGetPassword() - Get a password from the user.
41 * Include necessary headers...
44 #include "http-private.h"
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 const char *home
; /* Home directory of user */
68 char line
[1024], /* Line from file */
69 *value
; /* Value on line */
70 int linenum
; /* Line number */
71 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
75 * First see if we have already set the encryption stuff...
78 if (cg
->encryption
== (http_encryption_t
)-1)
81 * Then see if the CUPS_ENCRYPTION environment variable is set...
84 if ((encryption
= getenv("CUPS_ENCRYPTION")) == NULL
)
87 * Next check to see if we have a $HOME/.cupsrc or client.conf file...
90 if ((home
= getenv("HOME")) != NULL
)
92 snprintf(line
, sizeof(line
), "%s/.cupsrc", home
);
93 fp
= cupsFileOpen(line
, "r");
100 snprintf(line
, sizeof(line
), "%s/client.conf", cg
->cups_serverroot
);
101 fp
= cupsFileOpen(line
, "r");
104 encryption
= "IfRequested";
109 * Read the config file and look for an Encryption line...
114 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
) != NULL
)
115 if (!strcasecmp(line
, "Encryption") && value
)
130 * Set the encryption preference...
133 if (!strcasecmp(encryption
, "never"))
134 cg
->encryption
= HTTP_ENCRYPT_NEVER
;
135 else if (!strcasecmp(encryption
, "always"))
136 cg
->encryption
= HTTP_ENCRYPT_ALWAYS
;
137 else if (!strcasecmp(encryption
, "required"))
138 cg
->encryption
= HTTP_ENCRYPT_REQUIRED
;
140 cg
->encryption
= HTTP_ENCRYPT_IF_REQUESTED
;
143 return (cg
->encryption
);
148 * 'cupsGetPassword()' - Get a password from the user.
150 * Returns NULL if the user does not provide a password.
153 const char * /* O - Password */
154 cupsGetPassword(const char *prompt
) /* I - Prompt string */
156 return ((*_cupsGlobals()->password_cb
)(prompt
));
161 * 'cupsSetEncryption()' - Set the encryption preference.
165 cupsSetEncryption(http_encryption_t e
) /* I - New encryption preference */
167 _cupsGlobals()->encryption
= e
;
172 * 'cupsServer()' - Return the hostname/address of the default server.
174 * The returned value can be a fully-qualified hostname, a numeric
175 * IPv4 or IPv6 address, or a domain socket pathname.
178 const char * /* O - Server name */
181 cups_file_t
*fp
; /* client.conf file */
182 char *server
; /* Pointer to server name */
183 const char *home
; /* Home directory of user */
184 char *port
; /* Port number */
185 char line
[1024], /* Line from file */
186 *value
; /* Value on line */
187 int linenum
; /* Line number in file */
188 #ifdef CUPS_DEFAULT_DOMAINSOCKET
189 struct stat sockinfo
; /* Domain socket information */
190 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
191 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
195 * First see if we have already set the server name...
201 * Then see if the CUPS_SERVER environment variable is set...
204 if ((server
= getenv("CUPS_SERVER")) == NULL
)
207 * Next check to see if we have a $HOME/.cupsrc or client.conf file...
210 if ((home
= getenv("HOME")) != NULL
)
212 snprintf(line
, sizeof(line
), "%s/.cupsrc", home
);
213 fp
= cupsFileOpen(line
, "r");
220 snprintf(line
, sizeof(line
), "%s/client.conf", cg
->cups_serverroot
);
221 fp
= cupsFileOpen(line
, "r");
224 #ifdef CUPS_DEFAULT_DOMAINSOCKET
226 * If we are compiled with domain socket support, only use the
227 * domain socket if it exists and has the right permissions...
230 if (!stat(CUPS_DEFAULT_DOMAINSOCKET
, &sockinfo
) &&
231 (sockinfo
.st_mode
& S_IRWXO
) == S_IRWXO
)
232 server
= CUPS_DEFAULT_DOMAINSOCKET
;
234 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
235 server
= "localhost";
240 * Read the config file and look for a ServerName line...
244 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
) != NULL
)
245 if (!strcasecmp(line
, "ServerName") && value
)
260 * Copy the server name over and set the port number, if any...
263 strlcpy(cg
->server
, server
, sizeof(cg
->server
));
265 if (cg
->server
[0] != '/' && (port
= strrchr(cg
->server
, ':')) != NULL
&&
266 !strchr(port
, ']') && isdigit(port
[1] & 255))
270 ippSetPort(atoi(port
));
273 if (cg
->server
[0] == '/')
274 strcpy(cg
->servername
, "localhost");
276 strlcpy(cg
->servername
, cg
->server
, sizeof(cg
->servername
));
284 * 'cupsSetPasswordCB()' - Set the password callback for CUPS.
286 * Pass NULL to restore the default (console) password callback.
290 cupsSetPasswordCB(cups_password_cb_t cb
)/* I - Callback function */
292 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
295 if (cb
== (const char *(*)(const char *))0)
296 cg
->password_cb
= _cupsGetPassword
;
298 cg
->password_cb
= cb
;
303 * 'cupsSetServer()' - Set the default server name.
305 * The "server" string can be a fully-qualified hostname, a numeric
306 * IPv4 or IPv6 address, or a domain socket pathname. Pass NULL to
307 * restore the default server name.
311 cupsSetServer(const char *server
) /* I - Server name */
313 char *port
; /* Pointer to port */
314 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
319 strlcpy(cg
->server
, server
, sizeof(cg
->server
));
321 if (cg
->server
[0] != '/' && (port
= strrchr(cg
->server
, ':')) != NULL
&&
322 !strchr(port
, ']') && isdigit(port
[1] & 255))
326 ippSetPort(atoi(port
));
329 if (cg
->server
[0] == '/')
330 strcpy(cg
->servername
, "localhost");
332 strlcpy(cg
->servername
, cg
->server
, sizeof(cg
->servername
));
336 cg
->server
[0] = '\0';
337 cg
->servername
[0] = '\0';
343 * 'cupsSetUser()' - Set the default user name.
345 * Pass NULL to restore the default user name.
349 cupsSetUser(const char *user
) /* I - User name */
351 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
355 strlcpy(cg
->user
, user
, sizeof(cg
->user
));
363 * WIN32 username and password stuff.
367 * 'cupsUser()' - Return the current user's name.
370 const char * /* O - User name */
373 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
378 DWORD size
; /* Size of string */
381 size
= sizeof(cg
->user
);
382 if (!GetUserName(cg
->user
, &size
))
385 * Use the default username...
388 strcpy(cg
->user
, "unknown");
397 * '_cupsGetPassword()' - Get a password from the user.
400 const char * /* O - Password */
401 _cupsGetPassword(const char *prompt
) /* I - Prompt string */
407 * UNIX username and password stuff...
413 * 'cupsUser()' - Return the current user's name.
416 const char * /* O - User name */
419 struct passwd
*pwd
; /* User/password entry */
420 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
426 * Rewind the password file...
432 * Lookup the password entry for the current user.
435 if ((pwd
= getpwuid(getuid())) == NULL
)
436 strcpy(cg
->user
, "unknown"); /* Unknown user! */
440 * Copy the username...
445 strlcpy(cg
->user
, pwd
->pw_name
, sizeof(cg
->user
));
449 * Rewind the password file again...
460 * '_cupsGetPassword()' - Get a password from the user.
463 const char * /* O - Password */
464 _cupsGetPassword(const char *prompt
) /* I - Prompt string */
466 return (getpass(prompt
));
472 * End of "$Id: usersys.c 5041 2006-02-01 16:54:50Z mike $".