2 * "$Id: usersys.c 4918 2006-01-12 05:14:40Z 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"
53 * 'cupsEncryption()' - Get the default encryption settings.
55 * The default encryption setting comes from the CUPS_ENCRYPTION
56 * environment variable, then the ~/.cupsrc file, and finally the
57 * /etc/cups/client.conf file. If not set, the default is
58 * HTTP_ENCRYPT_IF_REQUESTED.
61 http_encryption_t
/* O - Encryption settings */
64 cups_file_t
*fp
; /* client.conf file */
65 char *encryption
; /* CUPS_ENCRYPTION variable */
66 const char *home
; /* Home directory of user */
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 * Next check to see if we have a $HOME/.cupsrc or client.conf file...
89 if ((home
= getenv("HOME")) != NULL
)
91 snprintf(line
, sizeof(line
), "%s/.cupsrc", home
);
92 fp
= cupsFileOpen(line
, "r");
99 snprintf(line
, sizeof(line
), "%s/client.conf", cg
->cups_serverroot
);
100 fp
= cupsFileOpen(line
, "r");
103 encryption
= "IfRequested";
108 * Read the config file and look for an Encryption line...
113 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
) != NULL
)
114 if (!strcasecmp(line
, "Encryption") && value
)
129 * Set the encryption preference...
132 if (!strcasecmp(encryption
, "never"))
133 cg
->encryption
= HTTP_ENCRYPT_NEVER
;
134 else if (!strcasecmp(encryption
, "always"))
135 cg
->encryption
= HTTP_ENCRYPT_ALWAYS
;
136 else if (!strcasecmp(encryption
, "required"))
137 cg
->encryption
= HTTP_ENCRYPT_REQUIRED
;
139 cg
->encryption
= HTTP_ENCRYPT_IF_REQUESTED
;
142 return (cg
->encryption
);
147 * 'cupsGetPassword()' - Get a password from the user.
149 * Returns NULL if the user does not provide a password.
152 const char * /* O - Password */
153 cupsGetPassword(const char *prompt
) /* I - Prompt string */
155 return ((*_cupsGlobals()->password_cb
)(prompt
));
160 * 'cupsSetEncryption()' - Set the encryption preference.
164 cupsSetEncryption(http_encryption_t e
) /* I - New encryption preference */
166 _cupsGlobals()->encryption
= e
;
171 * 'cupsServer()' - Return the hostname/address of the default server.
173 * The returned value can be a fully-qualified hostname, a numeric
174 * IPv4 or IPv6 address, or a domain socket pathname.
177 const char * /* O - Server name */
180 cups_file_t
*fp
; /* client.conf file */
181 char *server
; /* Pointer to server name */
182 const char *home
; /* Home directory of user */
183 char *port
; /* Port number */
184 char line
[1024], /* Line from file */
185 *value
; /* Value on line */
186 int linenum
; /* Line number in file */
187 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
191 * First see if we have already set the server name...
197 * Then see if the CUPS_SERVER environment variable is set...
200 if ((server
= getenv("CUPS_SERVER")) == NULL
)
203 * Next check to see if we have a $HOME/.cupsrc or client.conf file...
206 if ((home
= getenv("HOME")) != NULL
)
208 snprintf(line
, sizeof(line
), "%s/.cupsrc", home
);
209 fp
= cupsFileOpen(line
, "r");
216 snprintf(line
, sizeof(line
), "%s/client.conf", cg
->cups_serverroot
);
217 fp
= cupsFileOpen(line
, "r");
220 #ifdef CUPS_DEFAULT_DOMAINSOCKET
221 if (!access(CUPS_DEFAULT_DOMAINSOCKET
, 0))
222 server
= CUPS_DEFAULT_DOMAINSOCKET
;
224 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
225 server
= "localhost";
230 * Read the config file and look for a ServerName line...
234 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
) != NULL
)
235 if (!strcasecmp(line
, "ServerName") && value
)
250 * Copy the server name over and set the port number, if any...
253 strlcpy(cg
->server
, server
, sizeof(cg
->server
));
255 if (cg
->server
[0] != '/' && (port
= strrchr(cg
->server
, ':')) != NULL
&&
256 !strchr(port
, ']') && isdigit(port
[1] & 255))
260 ippSetPort(atoi(port
));
263 if (cg
->server
[0] == '/')
264 strcpy(cg
->servername
, "localhost");
266 strlcpy(cg
->servername
, cg
->server
, sizeof(cg
->servername
));
274 * 'cupsSetPasswordCB()' - Set the password callback for CUPS.
276 * Pass NULL to restore the default (console) password callback.
280 cupsSetPasswordCB(cups_password_cb_t cb
)/* I - Callback function */
282 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
285 if (cb
== (const char *(*)(const char *))0)
286 cg
->password_cb
= _cupsGetPassword
;
288 cg
->password_cb
= cb
;
293 * 'cupsSetServer()' - Set the default server name.
295 * The "server" string can be a fully-qualified hostname, a numeric
296 * IPv4 or IPv6 address, or a domain socket pathname. Pass NULL to
297 * restore the default server name.
301 cupsSetServer(const char *server
) /* I - Server name */
303 char *port
; /* Pointer to port */
304 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
309 strlcpy(cg
->server
, server
, sizeof(cg
->server
));
311 if (cg
->server
[0] != '/' && (port
= strrchr(cg
->server
, ':')) != NULL
&&
312 !strchr(port
, ']') && isdigit(port
[1] & 255))
316 ippSetPort(atoi(port
));
319 if (cg
->server
[0] == '/')
320 strcpy(cg
->servername
, "localhost");
322 strlcpy(cg
->servername
, cg
->server
, sizeof(cg
->servername
));
326 cg
->server
[0] = '\0';
327 cg
->servername
[0] = '\0';
333 * 'cupsSetUser()' - Set the default user name.
335 * Pass NULL to restore the default user name.
339 cupsSetUser(const char *user
) /* I - User name */
341 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
345 strlcpy(cg
->user
, user
, sizeof(cg
->user
));
353 * WIN32 username and password stuff.
357 * 'cupsUser()' - Return the current user's name.
360 const char * /* O - User name */
363 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
368 DWORD size
; /* Size of string */
371 size
= sizeof(cg
->user
);
372 if (!GetUserName(cg
->user
, &size
))
375 * Use the default username...
378 strcpy(cg
->user
, "unknown");
387 * '_cupsGetPassword()' - Get a password from the user.
390 const char * /* O - Password */
391 _cupsGetPassword(const char *prompt
) /* I - Prompt string */
397 * UNIX username and password stuff...
403 * 'cupsUser()' - Return the current user's name.
406 const char * /* O - User name */
409 struct passwd
*pwd
; /* User/password entry */
410 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
416 * Rewind the password file...
422 * Lookup the password entry for the current user.
425 if ((pwd
= getpwuid(getuid())) == NULL
)
426 strcpy(cg
->user
, "unknown"); /* Unknown user! */
430 * Copy the username...
435 strlcpy(cg
->user
, pwd
->pw_name
, sizeof(cg
->user
));
439 * Rewind the password file again...
450 * '_cupsGetPassword()' - Get a password from the user.
453 const char * /* O - Password */
454 _cupsGetPassword(const char *prompt
) /* I - Prompt string */
456 return (getpass(prompt
));
462 * End of "$Id: usersys.c 4918 2006-01-12 05:14:40Z mike $".