2 * "$Id: usersys.c 5138 2006-02-21 10:49:06Z 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 * Uses the current password callback function. Returns NULL if the
151 * user does not provide a password.
154 const char * /* O - Password */
155 cupsGetPassword(const char *prompt
) /* I - Prompt string */
157 return ((*_cupsGlobals()->password_cb
)(prompt
));
162 * 'cupsSetEncryption()' - Set the encryption preference.
166 cupsSetEncryption(http_encryption_t e
) /* I - New encryption preference */
168 _cupsGlobals()->encryption
= e
;
173 * 'cupsServer()' - Return the hostname/address of the default server.
175 * The returned value can be a fully-qualified hostname, a numeric
176 * IPv4 or IPv6 address, or a domain socket pathname.
179 const char * /* O - Server name */
182 cups_file_t
*fp
; /* client.conf file */
183 char *server
; /* Pointer to server name */
184 const char *home
; /* Home directory of user */
185 char *port
; /* Port number */
186 char line
[1024], /* Line from file */
187 *value
; /* Value on line */
188 int linenum
; /* Line number in file */
189 #ifdef CUPS_DEFAULT_DOMAINSOCKET
190 struct stat sockinfo
; /* Domain socket information */
191 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
192 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
196 * First see if we have already set the server name...
202 * Then see if the CUPS_SERVER environment variable is set...
205 if ((server
= getenv("CUPS_SERVER")) == NULL
)
208 * Next check to see if we have a $HOME/.cupsrc or client.conf file...
211 if ((home
= getenv("HOME")) != NULL
)
213 snprintf(line
, sizeof(line
), "%s/.cupsrc", home
);
214 fp
= cupsFileOpen(line
, "r");
221 snprintf(line
, sizeof(line
), "%s/client.conf", cg
->cups_serverroot
);
222 fp
= cupsFileOpen(line
, "r");
225 #ifdef CUPS_DEFAULT_DOMAINSOCKET
227 * If we are compiled with domain socket support, only use the
228 * domain socket if it exists and has the right permissions...
231 if (!stat(CUPS_DEFAULT_DOMAINSOCKET
, &sockinfo
) &&
232 (sockinfo
.st_mode
& S_IRWXO
) == S_IRWXO
)
233 server
= CUPS_DEFAULT_DOMAINSOCKET
;
235 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
236 server
= "localhost";
241 * Read the config file and look for a ServerName line...
245 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
) != NULL
)
246 if (!strcasecmp(line
, "ServerName") && value
)
261 * Copy the server name over and set the port number, if any...
264 strlcpy(cg
->server
, server
, sizeof(cg
->server
));
266 if (cg
->server
[0] != '/' && (port
= strrchr(cg
->server
, ':')) != NULL
&&
267 !strchr(port
, ']') && isdigit(port
[1] & 255))
271 ippSetPort(atoi(port
));
274 if (cg
->server
[0] == '/')
275 strcpy(cg
->servername
, "localhost");
277 strlcpy(cg
->servername
, cg
->server
, sizeof(cg
->servername
));
285 * 'cupsSetPasswordCB()' - Set the password callback for CUPS.
287 * Pass NULL to restore the default (console) password callback.
291 cupsSetPasswordCB(cups_password_cb_t cb
)/* I - Callback function */
293 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
296 if (cb
== (const char *(*)(const char *))0)
297 cg
->password_cb
= _cupsGetPassword
;
299 cg
->password_cb
= cb
;
304 * 'cupsSetServer()' - Set the default server name.
306 * The "server" string can be a fully-qualified hostname, a numeric
307 * IPv4 or IPv6 address, or a domain socket pathname. Pass NULL to
308 * restore the default server name.
312 cupsSetServer(const char *server
) /* I - Server name */
314 char *port
; /* Pointer to port */
315 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
320 strlcpy(cg
->server
, server
, sizeof(cg
->server
));
322 if (cg
->server
[0] != '/' && (port
= strrchr(cg
->server
, ':')) != NULL
&&
323 !strchr(port
, ']') && isdigit(port
[1] & 255))
327 ippSetPort(atoi(port
));
330 if (cg
->server
[0] == '/')
331 strcpy(cg
->servername
, "localhost");
333 strlcpy(cg
->servername
, cg
->server
, sizeof(cg
->servername
));
337 cg
->server
[0] = '\0';
338 cg
->servername
[0] = '\0';
344 * 'cupsSetUser()' - Set the default user name.
346 * Pass NULL to restore the default user name.
350 cupsSetUser(const char *user
) /* I - User name */
352 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
356 strlcpy(cg
->user
, user
, sizeof(cg
->user
));
364 * WIN32 username and password stuff.
368 * 'cupsUser()' - Return the current user's name.
371 const char * /* O - User name */
374 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
379 DWORD size
; /* Size of string */
382 size
= sizeof(cg
->user
);
383 if (!GetUserName(cg
->user
, &size
))
386 * Use the default username...
389 strcpy(cg
->user
, "unknown");
398 * '_cupsGetPassword()' - Get a password from the user.
401 const char * /* O - Password */
402 _cupsGetPassword(const char *prompt
) /* I - Prompt string */
408 * UNIX username and password stuff...
414 * 'cupsUser()' - Return the current user's name.
417 const char * /* O - User name */
420 struct passwd
*pwd
; /* User/password entry */
421 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
427 * Rewind the password file...
433 * Lookup the password entry for the current user.
436 if ((pwd
= getpwuid(getuid())) == NULL
)
437 strcpy(cg
->user
, "unknown"); /* Unknown user! */
441 * Copy the username...
446 strlcpy(cg
->user
, pwd
->pw_name
, sizeof(cg
->user
));
450 * Rewind the password file again...
461 * '_cupsGetPassword()' - Get a password from the user.
464 const char * /* O - Password */
465 _cupsGetPassword(const char *prompt
) /* I - Prompt string */
467 return (getpass(prompt
));
473 * End of "$Id: usersys.c 5138 2006-02-21 10:49:06Z mike $".