]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/usersys.c
Load cups into easysw/current.
[thirdparty/cups.git] / cups / usersys.c
CommitLineData
ef416fc2 1/*
bc44d920 2 * "$Id: usersys.c 6649 2007-07-11 21:46:42Z mike $"
ef416fc2 3 *
4 * User, system, and password routines for the Common UNIX Printing
5 * System (CUPS).
6 *
bc44d920 7 * Copyright 2007 by Apple Inc.
ef416fc2 8 * Copyright 1997-2006 by Easy Software Products.
9 *
10 * These coded instructions, statements, and computer programs are the
bc44d920 11 * property of Apple Inc. and are protected by Federal copyright
12 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
13 * which should have been included with this file. If this file is
14 * file is missing or damaged, see the license at "http://www.cups.org/".
ef416fc2 15 *
16 * This file is subject to the Apple OS-Developed Software exception.
17 *
18 * Contents:
19 *
b423cd4c 20 * cupsEncryption() - Get the default encryption settings.
21 * cupsGetPassword() - Get a password from the user.
22 * cupsServer() - Return the hostname of the default server.
23 * cupsSetEncryption() - Set the encryption preference.
24 * cupsSetPasswordCB() - Set the password callback for CUPS.
25 * cupsSetServer() - Set the default server name.
26 * cupsSetUser() - Set the default user name.
27 * cupsUser() - Return the current users name.
28 * _cupsGetPassword() - Get a password from the user.
29 * cups_open_client_conf() - Open the client.conf file.
ef416fc2 30 */
31
32/*
33 * Include necessary headers...
34 */
35
36#include "http-private.h"
37#include "globals.h"
38#include <stdlib.h>
e00b005a 39#include <sys/stat.h>
ef416fc2 40#ifdef WIN32
41# include <windows.h>
42#endif /* WIN32 */
d09495fa 43#include "debug.h"
ef416fc2 44
45
b423cd4c 46/*
47 * Local functions...
48 */
49
50static cups_file_t *cups_open_client_conf(void);
51
52
ef416fc2 53/*
54 * 'cupsEncryption()' - Get the default encryption settings.
55 *
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.
60 */
61
62http_encryption_t /* O - Encryption settings */
63cupsEncryption(void)
64{
65 cups_file_t *fp; /* client.conf file */
66 char *encryption; /* CUPS_ENCRYPTION variable */
ef416fc2 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 */
71
72
73 /*
74 * First see if we have already set the encryption stuff...
75 */
76
77 if (cg->encryption == (http_encryption_t)-1)
78 {
79 /*
80 * Then see if the CUPS_ENCRYPTION environment variable is set...
81 */
82
83 if ((encryption = getenv("CUPS_ENCRYPTION")) == NULL)
84 {
85 /*
b423cd4c 86 * No, open the client.conf file...
ef416fc2 87 */
88
b423cd4c 89 fp = cups_open_client_conf();
ef416fc2 90 encryption = "IfRequested";
91
b423cd4c 92 if (fp)
ef416fc2 93 {
94 /*
95 * Read the config file and look for an Encryption line...
96 */
97
98 linenum = 0;
99
100 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum) != NULL)
101 if (!strcasecmp(line, "Encryption") && value)
102 {
103 /*
104 * Got it!
105 */
106
107 encryption = value;
108 break;
109 }
110
111 cupsFileClose(fp);
112 }
113 }
114
115 /*
116 * Set the encryption preference...
117 */
118
119 if (!strcasecmp(encryption, "never"))
120 cg->encryption = HTTP_ENCRYPT_NEVER;
121 else if (!strcasecmp(encryption, "always"))
122 cg->encryption = HTTP_ENCRYPT_ALWAYS;
123 else if (!strcasecmp(encryption, "required"))
124 cg->encryption = HTTP_ENCRYPT_REQUIRED;
125 else
126 cg->encryption = HTTP_ENCRYPT_IF_REQUESTED;
127 }
128
129 return (cg->encryption);
130}
131
132
133/*
134 * 'cupsGetPassword()' - Get a password from the user.
135 *
ecdc0628 136 * Uses the current password callback function. Returns NULL if the
137 * user does not provide a password.
ef416fc2 138 */
139
140const char * /* O - Password */
141cupsGetPassword(const char *prompt) /* I - Prompt string */
142{
143 return ((*_cupsGlobals()->password_cb)(prompt));
144}
145
146
147/*
148 * 'cupsSetEncryption()' - Set the encryption preference.
149 */
150
151void
152cupsSetEncryption(http_encryption_t e) /* I - New encryption preference */
153{
154 _cupsGlobals()->encryption = e;
155}
156
157
158/*
159 * 'cupsServer()' - Return the hostname/address of the default server.
160 *
161 * The returned value can be a fully-qualified hostname, a numeric
162 * IPv4 or IPv6 address, or a domain socket pathname.
163 */
164
165const char * /* O - Server name */
166cupsServer(void)
167{
168 cups_file_t *fp; /* client.conf file */
169 char *server; /* Pointer to server name */
ef416fc2 170 char *port; /* Port number */
171 char line[1024], /* Line from file */
172 *value; /* Value on line */
173 int linenum; /* Line number in file */
e00b005a 174#ifdef CUPS_DEFAULT_DOMAINSOCKET
175 struct stat sockinfo; /* Domain socket information */
176#endif /* CUPS_DEFAULT_DOMAINSOCKET */
ef416fc2 177 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
178
179
180 /*
181 * First see if we have already set the server name...
182 */
183
184 if (!cg->server[0])
185 {
186 /*
187 * Then see if the CUPS_SERVER environment variable is set...
188 */
189
190 if ((server = getenv("CUPS_SERVER")) == NULL)
191 {
192 /*
b423cd4c 193 * No environment variable, try the client.conf file...
ef416fc2 194 */
195
b423cd4c 196 fp = cups_open_client_conf();
ef416fc2 197
198#ifdef CUPS_DEFAULT_DOMAINSOCKET
e00b005a 199 /*
200 * If we are compiled with domain socket support, only use the
201 * domain socket if it exists and has the right permissions...
202 */
203
204 if (!stat(CUPS_DEFAULT_DOMAINSOCKET, &sockinfo) &&
205 (sockinfo.st_mode & S_IRWXO) == S_IRWXO)
ef416fc2 206 server = CUPS_DEFAULT_DOMAINSOCKET;
207 else
208#endif /* CUPS_DEFAULT_DOMAINSOCKET */
209 server = "localhost";
210
b423cd4c 211 if (fp)
ef416fc2 212 {
213 /*
214 * Read the config file and look for a ServerName line...
215 */
216
217 linenum = 0;
218 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum) != NULL)
d09495fa 219 {
220 DEBUG_printf(("cupsServer: %d: %s %s\n", linenum, line,
221 value ? value : "(null)"));
222
ef416fc2 223 if (!strcasecmp(line, "ServerName") && value)
224 {
225 /*
226 * Got it!
227 */
228
d09495fa 229 DEBUG_puts("cupsServer: Got a ServerName line!");
ef416fc2 230 server = value;
231 break;
232 }
d09495fa 233 }
ef416fc2 234
235 cupsFileClose(fp);
236 }
237 }
238
239 /*
240 * Copy the server name over and set the port number, if any...
241 */
242
d09495fa 243 DEBUG_printf(("cupsServer: Using server \"%s\"...\n", server));
244
ef416fc2 245 strlcpy(cg->server, server, sizeof(cg->server));
246
247 if (cg->server[0] != '/' && (port = strrchr(cg->server, ':')) != NULL &&
248 !strchr(port, ']') && isdigit(port[1] & 255))
249 {
250 *port++ = '\0';
251
d09495fa 252 DEBUG_printf(("cupsServer: Using port %d...\n", atoi(port)));
ef416fc2 253 ippSetPort(atoi(port));
254 }
255
256 if (cg->server[0] == '/')
257 strcpy(cg->servername, "localhost");
258 else
259 strlcpy(cg->servername, cg->server, sizeof(cg->servername));
260 }
261
262 return (cg->server);
263}
264
265
266/*
267 * 'cupsSetPasswordCB()' - Set the password callback for CUPS.
268 *
269 * Pass NULL to restore the default (console) password callback.
270 */
271
272void
273cupsSetPasswordCB(cups_password_cb_t cb)/* I - Callback function */
274{
275 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
276
277
278 if (cb == (const char *(*)(const char *))0)
279 cg->password_cb = _cupsGetPassword;
280 else
281 cg->password_cb = cb;
282}
283
284
285/*
286 * 'cupsSetServer()' - Set the default server name.
287 *
288 * The "server" string can be a fully-qualified hostname, a numeric
289 * IPv4 or IPv6 address, or a domain socket pathname. Pass NULL to
290 * restore the default server name.
291 */
292
293void
294cupsSetServer(const char *server) /* I - Server name */
295{
296 char *port; /* Pointer to port */
297 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
298
299
300 if (server)
301 {
302 strlcpy(cg->server, server, sizeof(cg->server));
303
304 if (cg->server[0] != '/' && (port = strrchr(cg->server, ':')) != NULL &&
305 !strchr(port, ']') && isdigit(port[1] & 255))
306 {
307 *port++ = '\0';
308
309 ippSetPort(atoi(port));
310 }
311
312 if (cg->server[0] == '/')
313 strcpy(cg->servername, "localhost");
314 else
315 strlcpy(cg->servername, cg->server, sizeof(cg->servername));
316 }
317 else
318 {
319 cg->server[0] = '\0';
320 cg->servername[0] = '\0';
321 }
322}
323
324
325/*
326 * 'cupsSetUser()' - Set the default user name.
327 *
328 * Pass NULL to restore the default user name.
329 */
330
331void
332cupsSetUser(const char *user) /* I - User name */
333{
334 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
335
336
337 if (user)
338 strlcpy(cg->user, user, sizeof(cg->user));
339 else
340 cg->user[0] = '\0';
341}
342
343
344#if defined(WIN32)
345/*
346 * WIN32 username and password stuff.
347 */
348
349/*
350 * 'cupsUser()' - Return the current user's name.
351 */
352
353const char * /* O - User name */
354cupsUser(void)
355{
356 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
357
358
359 if (!cg->user[0])
360 {
361 DWORD size; /* Size of string */
362
363
364 size = sizeof(cg->user);
365 if (!GetUserName(cg->user, &size))
366 {
367 /*
368 * Use the default username...
369 */
370
371 strcpy(cg->user, "unknown");
372 }
373 }
374
375 return (cg->user);
376}
377
378
379/*
380 * '_cupsGetPassword()' - Get a password from the user.
381 */
382
383const char * /* O - Password */
384_cupsGetPassword(const char *prompt) /* I - Prompt string */
385{
386 return (NULL);
387}
388#else
389/*
390 * UNIX username and password stuff...
391 */
392
393# include <pwd.h>
394
395/*
396 * 'cupsUser()' - Return the current user's name.
397 */
398
399const char * /* O - User name */
400cupsUser(void)
401{
402 struct passwd *pwd; /* User/password entry */
403 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
404
405
406 if (!cg->user[0])
407 {
408 /*
409 * Rewind the password file...
410 */
411
412 setpwent();
413
414 /*
415 * Lookup the password entry for the current user.
416 */
417
418 if ((pwd = getpwuid(getuid())) == NULL)
419 strcpy(cg->user, "unknown"); /* Unknown user! */
420 else
421 {
422 /*
423 * Copy the username...
424 */
425
426 setpwent();
427
428 strlcpy(cg->user, pwd->pw_name, sizeof(cg->user));
429 }
430
431 /*
432 * Rewind the password file again...
433 */
434
435 setpwent();
436 }
437
438 return (cg->user);
439}
440
441
442/*
443 * '_cupsGetPassword()' - Get a password from the user.
444 */
445
446const char * /* O - Password */
447_cupsGetPassword(const char *prompt) /* I - Prompt string */
448{
449 return (getpass(prompt));
450}
451#endif /* WIN32 */
452
453
454/*
b423cd4c 455 * 'cups_open_client_conf()' - Open the client.conf file.
456 */
457
458static cups_file_t * /* O - File or NULL */
459cups_open_client_conf(void)
460{
461 cups_file_t *fp; /* File */
462 const char *home; /* Home directory of user */
463 char filename[1024]; /* Filename */
464 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
465
466
467 if ((home = getenv("HOME")) != NULL)
468 {
469 /*
470 * Look for ~/.cups/client.conf or ~/.cupsrc...
471 */
472
473 snprintf(filename, sizeof(filename), "%s/.cups/client.conf", home);
474 if ((fp = cupsFileOpen(filename, "r")) != NULL)
d09495fa 475 {
476 DEBUG_printf(("cups_open_client_conf: Using \"%s\"...\n", filename));
b423cd4c 477 return (fp);
d09495fa 478 }
b423cd4c 479
480 snprintf(filename, sizeof(filename), "%s/.cupsrc", home);
481 if ((fp = cupsFileOpen(filename, "r")) != NULL)
d09495fa 482 {
483 DEBUG_printf(("cups_open_client_conf: Using \"%s\"...\n", filename));
b423cd4c 484 return (fp);
d09495fa 485 }
b423cd4c 486 }
487
488 snprintf(filename, sizeof(filename), "%s/client.conf", cg->cups_serverroot);
489 return (cupsFileOpen(filename, "r"));
490}
491
492
493/*
bc44d920 494 * End of "$Id: usersys.c 6649 2007-07-11 21:46:42Z mike $".
ef416fc2 495 */