2 * "$Id: usersys.c 5901 2006-08-29 15:49:34Z 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.
38 * cups_open_client_conf() - Open the client.conf file.
42 * Include necessary headers...
45 #include "http-private.h"
59 static cups_file_t
*cups_open_client_conf(void);
63 * 'cupsEncryption()' - Get the default encryption settings.
65 * The default encryption setting comes from the CUPS_ENCRYPTION
66 * environment variable, then the ~/.cupsrc file, and finally the
67 * /etc/cups/client.conf file. If not set, the default is
68 * HTTP_ENCRYPT_IF_REQUESTED.
71 http_encryption_t
/* O - Encryption settings */
74 cups_file_t
*fp
; /* client.conf file */
75 char *encryption
; /* CUPS_ENCRYPTION variable */
76 char line
[1024], /* Line from file */
77 *value
; /* Value on line */
78 int linenum
; /* Line number */
79 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
83 * First see if we have already set the encryption stuff...
86 if (cg
->encryption
== (http_encryption_t
)-1)
89 * Then see if the CUPS_ENCRYPTION environment variable is set...
92 if ((encryption
= getenv("CUPS_ENCRYPTION")) == NULL
)
95 * No, open the client.conf file...
98 fp
= cups_open_client_conf();
99 encryption
= "IfRequested";
104 * Read the config file and look for an Encryption line...
109 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
) != NULL
)
110 if (!strcasecmp(line
, "Encryption") && value
)
125 * Set the encryption preference...
128 if (!strcasecmp(encryption
, "never"))
129 cg
->encryption
= HTTP_ENCRYPT_NEVER
;
130 else if (!strcasecmp(encryption
, "always"))
131 cg
->encryption
= HTTP_ENCRYPT_ALWAYS
;
132 else if (!strcasecmp(encryption
, "required"))
133 cg
->encryption
= HTTP_ENCRYPT_REQUIRED
;
135 cg
->encryption
= HTTP_ENCRYPT_IF_REQUESTED
;
138 return (cg
->encryption
);
143 * 'cupsGetPassword()' - Get a password from the user.
145 * Uses the current password callback function. Returns NULL if the
146 * user does not provide a password.
149 const char * /* O - Password */
150 cupsGetPassword(const char *prompt
) /* I - Prompt string */
152 return ((*_cupsGlobals()->password_cb
)(prompt
));
157 * 'cupsSetEncryption()' - Set the encryption preference.
161 cupsSetEncryption(http_encryption_t e
) /* I - New encryption preference */
163 _cupsGlobals()->encryption
= e
;
168 * 'cupsServer()' - Return the hostname/address of the default server.
170 * The returned value can be a fully-qualified hostname, a numeric
171 * IPv4 or IPv6 address, or a domain socket pathname.
174 const char * /* O - Server name */
177 cups_file_t
*fp
; /* client.conf file */
178 char *server
; /* Pointer to server name */
179 char *port
; /* Port number */
180 char line
[1024], /* Line from file */
181 *value
; /* Value on line */
182 int linenum
; /* Line number in file */
183 #ifdef CUPS_DEFAULT_DOMAINSOCKET
184 struct stat sockinfo
; /* Domain socket information */
185 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
186 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
190 * First see if we have already set the server name...
196 * Then see if the CUPS_SERVER environment variable is set...
199 if ((server
= getenv("CUPS_SERVER")) == NULL
)
202 * No environment variable, try the client.conf file...
205 fp
= cups_open_client_conf();
207 #ifdef CUPS_DEFAULT_DOMAINSOCKET
209 * If we are compiled with domain socket support, only use the
210 * domain socket if it exists and has the right permissions...
213 if (!stat(CUPS_DEFAULT_DOMAINSOCKET
, &sockinfo
) &&
214 (sockinfo
.st_mode
& S_IRWXO
) == S_IRWXO
)
215 server
= CUPS_DEFAULT_DOMAINSOCKET
;
217 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
218 server
= "localhost";
223 * Read the config file and look for a ServerName line...
227 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
) != NULL
)
229 DEBUG_printf(("cupsServer: %d: %s %s\n", linenum
, line
,
230 value
? value
: "(null)"));
232 if (!strcasecmp(line
, "ServerName") && value
)
238 DEBUG_puts("cupsServer: Got a ServerName line!");
249 * Copy the server name over and set the port number, if any...
252 DEBUG_printf(("cupsServer: Using server \"%s\"...\n", server
));
254 strlcpy(cg
->server
, server
, sizeof(cg
->server
));
256 if (cg
->server
[0] != '/' && (port
= strrchr(cg
->server
, ':')) != NULL
&&
257 !strchr(port
, ']') && isdigit(port
[1] & 255))
261 DEBUG_printf(("cupsServer: Using port %d...\n", atoi(port
)));
262 ippSetPort(atoi(port
));
265 if (cg
->server
[0] == '/')
266 strcpy(cg
->servername
, "localhost");
268 strlcpy(cg
->servername
, cg
->server
, sizeof(cg
->servername
));
276 * 'cupsSetPasswordCB()' - Set the password callback for CUPS.
278 * Pass NULL to restore the default (console) password callback.
282 cupsSetPasswordCB(cups_password_cb_t cb
)/* I - Callback function */
284 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
287 if (cb
== (const char *(*)(const char *))0)
288 cg
->password_cb
= _cupsGetPassword
;
290 cg
->password_cb
= cb
;
295 * 'cupsSetServer()' - Set the default server name.
297 * The "server" string can be a fully-qualified hostname, a numeric
298 * IPv4 or IPv6 address, or a domain socket pathname. Pass NULL to
299 * restore the default server name.
303 cupsSetServer(const char *server
) /* I - Server name */
305 char *port
; /* Pointer to port */
306 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
311 strlcpy(cg
->server
, server
, sizeof(cg
->server
));
313 if (cg
->server
[0] != '/' && (port
= strrchr(cg
->server
, ':')) != NULL
&&
314 !strchr(port
, ']') && isdigit(port
[1] & 255))
318 ippSetPort(atoi(port
));
321 if (cg
->server
[0] == '/')
322 strcpy(cg
->servername
, "localhost");
324 strlcpy(cg
->servername
, cg
->server
, sizeof(cg
->servername
));
328 cg
->server
[0] = '\0';
329 cg
->servername
[0] = '\0';
335 * 'cupsSetUser()' - Set the default user name.
337 * Pass NULL to restore the default user name.
341 cupsSetUser(const char *user
) /* I - User name */
343 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
347 strlcpy(cg
->user
, user
, sizeof(cg
->user
));
355 * WIN32 username and password stuff.
359 * 'cupsUser()' - Return the current user's name.
362 const char * /* O - User name */
365 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
370 DWORD size
; /* Size of string */
373 size
= sizeof(cg
->user
);
374 if (!GetUserName(cg
->user
, &size
))
377 * Use the default username...
380 strcpy(cg
->user
, "unknown");
389 * '_cupsGetPassword()' - Get a password from the user.
392 const char * /* O - Password */
393 _cupsGetPassword(const char *prompt
) /* I - Prompt string */
399 * UNIX username and password stuff...
405 * 'cupsUser()' - Return the current user's name.
408 const char * /* O - User name */
411 struct passwd
*pwd
; /* User/password entry */
412 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
418 * Rewind the password file...
424 * Lookup the password entry for the current user.
427 if ((pwd
= getpwuid(getuid())) == NULL
)
428 strcpy(cg
->user
, "unknown"); /* Unknown user! */
432 * Copy the username...
437 strlcpy(cg
->user
, pwd
->pw_name
, sizeof(cg
->user
));
441 * Rewind the password file again...
452 * '_cupsGetPassword()' - Get a password from the user.
455 const char * /* O - Password */
456 _cupsGetPassword(const char *prompt
) /* I - Prompt string */
458 return (getpass(prompt
));
464 * 'cups_open_client_conf()' - Open the client.conf file.
467 static cups_file_t
* /* O - File or NULL */
468 cups_open_client_conf(void)
470 cups_file_t
*fp
; /* File */
471 const char *home
; /* Home directory of user */
472 char filename
[1024]; /* Filename */
473 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
476 if ((home
= getenv("HOME")) != NULL
)
479 * Look for ~/.cups/client.conf or ~/.cupsrc...
482 snprintf(filename
, sizeof(filename
), "%s/.cups/client.conf", home
);
483 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
485 DEBUG_printf(("cups_open_client_conf: Using \"%s\"...\n", filename
));
489 snprintf(filename
, sizeof(filename
), "%s/.cupsrc", home
);
490 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
492 DEBUG_printf(("cups_open_client_conf: Using \"%s\"...\n", filename
));
497 snprintf(filename
, sizeof(filename
), "%s/client.conf", cg
->cups_serverroot
);
498 return (cupsFileOpen(filename
, "r"));
503 * End of "$Id: usersys.c 5901 2006-08-29 15:49:34Z mike $".