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.
38 * cups_open_client_conf() - Open the client.conf file.
42 * Include necessary headers...
45 #include "http-private.h"
58 static cups_file_t
*cups_open_client_conf(void);
62 * 'cupsEncryption()' - Get the default encryption settings.
64 * The default encryption setting comes from the CUPS_ENCRYPTION
65 * environment variable, then the ~/.cupsrc file, and finally the
66 * /etc/cups/client.conf file. If not set, the default is
67 * HTTP_ENCRYPT_IF_REQUESTED.
70 http_encryption_t
/* O - Encryption settings */
73 cups_file_t
*fp
; /* client.conf file */
74 char *encryption
; /* CUPS_ENCRYPTION variable */
75 char line
[1024], /* Line from file */
76 *value
; /* Value on line */
77 int linenum
; /* Line number */
78 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
82 * First see if we have already set the encryption stuff...
85 if (cg
->encryption
== (http_encryption_t
)-1)
88 * Then see if the CUPS_ENCRYPTION environment variable is set...
91 if ((encryption
= getenv("CUPS_ENCRYPTION")) == NULL
)
94 * No, open the client.conf file...
97 fp
= cups_open_client_conf();
98 encryption
= "IfRequested";
103 * Read the config file and look for an Encryption line...
108 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
) != NULL
)
109 if (!strcasecmp(line
, "Encryption") && value
)
124 * Set the encryption preference...
127 if (!strcasecmp(encryption
, "never"))
128 cg
->encryption
= HTTP_ENCRYPT_NEVER
;
129 else if (!strcasecmp(encryption
, "always"))
130 cg
->encryption
= HTTP_ENCRYPT_ALWAYS
;
131 else if (!strcasecmp(encryption
, "required"))
132 cg
->encryption
= HTTP_ENCRYPT_REQUIRED
;
134 cg
->encryption
= HTTP_ENCRYPT_IF_REQUESTED
;
137 return (cg
->encryption
);
142 * 'cupsGetPassword()' - Get a password from the user.
144 * Uses the current password callback function. Returns NULL if the
145 * user does not provide a password.
148 const char * /* O - Password */
149 cupsGetPassword(const char *prompt
) /* I - Prompt string */
151 return ((*_cupsGlobals()->password_cb
)(prompt
));
156 * 'cupsSetEncryption()' - Set the encryption preference.
160 cupsSetEncryption(http_encryption_t e
) /* I - New encryption preference */
162 _cupsGlobals()->encryption
= e
;
167 * 'cupsServer()' - Return the hostname/address of the default server.
169 * The returned value can be a fully-qualified hostname, a numeric
170 * IPv4 or IPv6 address, or a domain socket pathname.
173 const char * /* O - Server name */
176 cups_file_t
*fp
; /* client.conf file */
177 char *server
; /* Pointer to server name */
178 char *port
; /* Port number */
179 char line
[1024], /* Line from file */
180 *value
; /* Value on line */
181 int linenum
; /* Line number in file */
182 #ifdef CUPS_DEFAULT_DOMAINSOCKET
183 struct stat sockinfo
; /* Domain socket information */
184 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
185 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
189 * First see if we have already set the server name...
195 * Then see if the CUPS_SERVER environment variable is set...
198 if ((server
= getenv("CUPS_SERVER")) == NULL
)
201 * No environment variable, try the client.conf file...
204 fp
= cups_open_client_conf();
206 #ifdef CUPS_DEFAULT_DOMAINSOCKET
208 * If we are compiled with domain socket support, only use the
209 * domain socket if it exists and has the right permissions...
212 if (!stat(CUPS_DEFAULT_DOMAINSOCKET
, &sockinfo
) &&
213 (sockinfo
.st_mode
& S_IRWXO
) == S_IRWXO
)
214 server
= CUPS_DEFAULT_DOMAINSOCKET
;
216 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
217 server
= "localhost";
222 * Read the config file and look for a ServerName line...
226 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
) != NULL
)
227 if (!strcasecmp(line
, "ServerName") && value
)
242 * Copy the server name over and set the port number, if any...
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 ippSetPort(atoi(port
));
255 if (cg
->server
[0] == '/')
256 strcpy(cg
->servername
, "localhost");
258 strlcpy(cg
->servername
, cg
->server
, sizeof(cg
->servername
));
266 * 'cupsSetPasswordCB()' - Set the password callback for CUPS.
268 * Pass NULL to restore the default (console) password callback.
272 cupsSetPasswordCB(cups_password_cb_t cb
)/* I - Callback function */
274 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
277 if (cb
== (const char *(*)(const char *))0)
278 cg
->password_cb
= _cupsGetPassword
;
280 cg
->password_cb
= cb
;
285 * 'cupsSetServer()' - Set the default server name.
287 * The "server" string can be a fully-qualified hostname, a numeric
288 * IPv4 or IPv6 address, or a domain socket pathname. Pass NULL to
289 * restore the default server name.
293 cupsSetServer(const char *server
) /* I - Server name */
295 char *port
; /* Pointer to port */
296 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
301 strlcpy(cg
->server
, server
, sizeof(cg
->server
));
303 if (cg
->server
[0] != '/' && (port
= strrchr(cg
->server
, ':')) != NULL
&&
304 !strchr(port
, ']') && isdigit(port
[1] & 255))
308 ippSetPort(atoi(port
));
311 if (cg
->server
[0] == '/')
312 strcpy(cg
->servername
, "localhost");
314 strlcpy(cg
->servername
, cg
->server
, sizeof(cg
->servername
));
318 cg
->server
[0] = '\0';
319 cg
->servername
[0] = '\0';
325 * 'cupsSetUser()' - Set the default user name.
327 * Pass NULL to restore the default user name.
331 cupsSetUser(const char *user
) /* I - User name */
333 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
337 strlcpy(cg
->user
, user
, sizeof(cg
->user
));
345 * WIN32 username and password stuff.
349 * 'cupsUser()' - Return the current user's name.
352 const char * /* O - User name */
355 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
360 DWORD size
; /* Size of string */
363 size
= sizeof(cg
->user
);
364 if (!GetUserName(cg
->user
, &size
))
367 * Use the default username...
370 strcpy(cg
->user
, "unknown");
379 * '_cupsGetPassword()' - Get a password from the user.
382 const char * /* O - Password */
383 _cupsGetPassword(const char *prompt
) /* I - Prompt string */
389 * UNIX username and password stuff...
395 * 'cupsUser()' - Return the current user's name.
398 const char * /* O - User name */
401 struct passwd
*pwd
; /* User/password entry */
402 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
408 * Rewind the password file...
414 * Lookup the password entry for the current user.
417 if ((pwd
= getpwuid(getuid())) == NULL
)
418 strcpy(cg
->user
, "unknown"); /* Unknown user! */
422 * Copy the username...
427 strlcpy(cg
->user
, pwd
->pw_name
, sizeof(cg
->user
));
431 * Rewind the password file again...
442 * '_cupsGetPassword()' - Get a password from the user.
445 const char * /* O - Password */
446 _cupsGetPassword(const char *prompt
) /* I - Prompt string */
448 return (getpass(prompt
));
454 * 'cups_open_client_conf()' - Open the client.conf file.
457 static cups_file_t
* /* O - File or NULL */
458 cups_open_client_conf(void)
460 cups_file_t
*fp
; /* File */
461 const char *home
; /* Home directory of user */
462 char filename
[1024]; /* Filename */
463 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
466 if ((home
= getenv("HOME")) != NULL
)
469 * Look for ~/.cups/client.conf or ~/.cupsrc...
472 snprintf(filename
, sizeof(filename
), "%s/.cups/client.conf", home
);
473 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
476 snprintf(filename
, sizeof(filename
), "%s/.cupsrc", home
);
477 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
481 snprintf(filename
, sizeof(filename
), "%s/client.conf", cg
->cups_serverroot
);
482 return (cupsFileOpen(filename
, "r"));