]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/usersys.c
Merge changes from CUPS 1.5svn-r9229.
[thirdparty/cups.git] / cups / usersys.c
CommitLineData
ef416fc2 1/*
e07d4801 2 * "$Id: usersys.c 8498 2009-04-13 17:03:15Z mike $"
ef416fc2 3 *
5a6b583a 4 * User, system, and password routines for CUPS.
ef416fc2 5 *
5a6b583a 6 * Copyright 2007-2010 by Apple Inc.
ef416fc2 7 * Copyright 1997-2006 by Easy Software Products.
8 *
9 * These coded instructions, statements, and computer programs are the
bc44d920 10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
ef416fc2 14 *
15 * This file is subject to the Apple OS-Developed Software exception.
16 *
17 * Contents:
18 *
5a6b583a 19 * cupsEncryption() - Get the current encryption settings.
b423cd4c 20 * cupsGetPassword() - Get a password from the user.
5a6b583a
MS
21 * cupsGetPassword2() - Get a password from the user using the advanced
22 * password callback.
23 * cupsServer() - Return the hostname/address of the current
e07d4801 24 * server.
b423cd4c 25 * cupsSetEncryption() - Set the encryption preference.
26 * cupsSetPasswordCB() - Set the password callback for CUPS.
f11a948a 27 * cupsSetPasswordCB2() - Set the advanced password callback for CUPS.
5a6b583a 28 * cupsSetServer() - Set the default server name and port.
b423cd4c 29 * cupsSetUser() - Set the default user name.
e07d4801 30 * cupsUser() - Return the current user's name.
b423cd4c 31 * _cupsGetPassword() - Get a password from the user.
e07d4801
MS
32 * _cupsSetDefaults() - Set the default server, port, and encryption.
33 * cups_read_client_conf() - Read a client.conf file.
ef416fc2 34 */
35
36/*
37 * Include necessary headers...
38 */
39
71e16022 40#include "cups-private.h"
ef416fc2 41#include <stdlib.h>
e00b005a 42#include <sys/stat.h>
ef416fc2 43#ifdef WIN32
44# include <windows.h>
5a6b583a
MS
45#else
46# include <pwd.h>
ef416fc2 47#endif /* WIN32 */
48
49
b423cd4c 50/*
51 * Local functions...
52 */
53
e07d4801
MS
54static void cups_read_client_conf(cups_file_t *fp,
55 _cups_globals_t *cg,
56 const char *cups_encryption,
57 const char *cups_server);
b423cd4c 58
59
ef416fc2 60/*
5a6b583a 61 * 'cupsEncryption()' - Get the current encryption settings.
ef416fc2 62 *
63 * The default encryption setting comes from the CUPS_ENCRYPTION
568fa3fa 64 * environment variable, then the ~/.cups/client.conf file, and finally the
ef416fc2 65 * /etc/cups/client.conf file. If not set, the default is
5a738aea 66 * @code HTTP_ENCRYPT_IF_REQUESTED@.
5a6b583a
MS
67 *
68 * Note: The current encryption setting is tracked separately for each thread
69 * in a program. Multi-threaded programs that override the setting via the
70 * @link cupsSetEncryption@ function need to do so in each thread for the same
71 * setting to be used.
ef416fc2 72 */
73
74http_encryption_t /* O - Encryption settings */
75cupsEncryption(void)
76{
ef416fc2 77 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
78
79
ef416fc2 80 if (cg->encryption == (http_encryption_t)-1)
e07d4801 81 _cupsSetDefaults();
ef416fc2 82
83 return (cg->encryption);
84}
85
86
87/*
88 * 'cupsGetPassword()' - Get a password from the user.
89 *
5a738aea 90 * Uses the current password callback function. Returns @code NULL@ if the
ecdc0628 91 * user does not provide a password.
5a6b583a
MS
92 *
93 * Note: The current password callback function is tracked separately for each
94 * thread in a program. Multi-threaded programs that override the setting via
95 * the @link cupsSetPasswordCB@ or @link cupsSetPasswordCB2@ functions need to
96 * do so in each thread for the same function to be used.
ef416fc2 97 */
98
99const char * /* O - Password */
100cupsGetPassword(const char *prompt) /* I - Prompt string */
101{
f11a948a
MS
102 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
103
104
105 return ((cg->password_cb)(prompt, NULL, NULL, NULL, cg->password_data));
106}
107
108
109/*
110 * 'cupsGetPassword2()' - Get a password from the user using the advanced
5a6b583a 111 * password callback.
f11a948a
MS
112 *
113 * Uses the current password callback function. Returns @code NULL@ if the
114 * user does not provide a password.
115 *
5a6b583a
MS
116 * Note: The current password callback function is tracked separately for each
117 * thread in a program. Multi-threaded programs that override the setting via
118 * the @link cupsSetPasswordCB@ or @link cupsSetPasswordCB2@ functions need to
119 * do so in each thread for the same function to be used.
120 *
178cb736 121 * @since CUPS 1.4/Mac OS X 10.6@
f11a948a
MS
122 */
123
124const char * /* O - Password */
125cupsGetPassword2(const char *prompt, /* I - Prompt string */
126 http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
127 const char *method, /* I - Request method ("GET", "POST", "PUT") */
128 const char *resource) /* I - Resource path */
129{
130 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
131
132
133 if (!http)
134 http = _cupsConnect();
135
136 return ((cg->password_cb)(prompt, http, method, resource, cg->password_data));
ef416fc2 137}
138
139
ef416fc2 140/*
5a6b583a
MS
141 * 'cupsServer()' - Return the hostname/address of the current server.
142 *
143 * The default server comes from the CUPS_SERVER environment variable, then the
144 * ~/.cups/client.conf file, and finally the /etc/cups/client.conf file. If not
145 * set, the default is the local system - either "localhost" or a domain socket
146 * path.
ef416fc2 147 *
5a6b583a
MS
148 * The returned value can be a fully-qualified hostname, a numeric IPv4 or IPv6
149 * address, or a domain socket pathname.
150 *
151 * Note: The current server is tracked separately for each thread in a program.
152 * Multi-threaded programs that override the server via the
153 * @link cupsSetServer@ function need to do so in each thread for the same
154 * server to be used.
ef416fc2 155 */
156
157const char * /* O - Server name */
158cupsServer(void)
159{
ef416fc2 160 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
161
162
ef416fc2 163 if (!cg->server[0])
e07d4801 164 _cupsSetDefaults();
ef416fc2 165
e07d4801
MS
166 return (cg->server);
167}
ef416fc2 168
d09495fa 169
e07d4801
MS
170/*
171 * 'cupsSetEncryption()' - Set the encryption preference.
5a6b583a
MS
172 *
173 * The default encryption setting comes from the CUPS_ENCRYPTION
174 * environment variable, then the ~/.cups/client.conf file, and finally the
175 * /etc/cups/client.conf file. If not set, the default is
176 * @code HTTP_ENCRYPT_IF_REQUESTED@.
177 *
178 * Note: The current encryption setting is tracked separately for each thread
179 * in a program. Multi-threaded programs that override the setting need to do
180 * so in each thread for the same setting to be used.
e07d4801 181 */
ef416fc2 182
e07d4801
MS
183void
184cupsSetEncryption(http_encryption_t e) /* I - New encryption preference */
185{
186 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
ef416fc2 187
ef416fc2 188
e07d4801 189 cg->encryption = e;
ef416fc2 190
e07d4801
MS
191 if (cg->http)
192 httpEncryption(cg->http, e);
ef416fc2 193}
194
195
196/*
197 * 'cupsSetPasswordCB()' - Set the password callback for CUPS.
198 *
5a6b583a
MS
199 * Pass @code NULL@ to restore the default (console) password callback, which
200 * reads the password from the console. Programs should call either this
201 * function or @link cupsSetPasswordCB2@, as only one callback can be registered
202 * by a program per thread.
203 *
204 * Note: The current password callback is tracked separately for each thread
205 * in a program. Multi-threaded programs that override the callback need to do
206 * so in each thread for the same callback to be used.
ef416fc2 207 */
208
209void
210cupsSetPasswordCB(cups_password_cb_t cb)/* I - Callback function */
211{
212 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
213
214
f11a948a
MS
215 if (cb == (cups_password_cb_t)0)
216 cg->password_cb = (cups_password_cb2_t)_cupsGetPassword;
217 else
218 cg->password_cb = (cups_password_cb2_t)cb;
219
220 cg->password_data = NULL;
221}
222
223
224/*
225 * 'cupsSetPasswordCB2()' - Set the advanced password callback for CUPS.
226 *
5a6b583a
MS
227 * Pass @code NULL@ to restore the default (console) password callback, which
228 * reads the password from the console. Programs should call either this
229 * function or @link cupsSetPasswordCB2@, as only one callback can be registered
230 * by a program per thread.
231 *
232 * Note: The current password callback is tracked separately for each thread
233 * in a program. Multi-threaded programs that override the callback need to do
234 * so in each thread for the same callback to be used.
f11a948a 235 *
178cb736 236 * @since CUPS 1.4/Mac OS X 10.6@
f11a948a
MS
237 */
238
239void
240cupsSetPasswordCB2(
241 cups_password_cb2_t cb, /* I - Callback function */
242 void *user_data) /* I - User data pointer */
243{
244 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
245
246
247 if (cb == (cups_password_cb2_t)0)
248 cg->password_cb = (cups_password_cb2_t)_cupsGetPassword;
ef416fc2 249 else
250 cg->password_cb = cb;
f11a948a
MS
251
252 cg->password_data = user_data;
ef416fc2 253}
254
255
256/*
5a6b583a 257 * 'cupsSetServer()' - Set the default server name and port.
ef416fc2 258 *
259 * The "server" string can be a fully-qualified hostname, a numeric
5a6b583a
MS
260 * IPv4 or IPv6 address, or a domain socket pathname. Hostnames and numeric IP
261 * addresses can be optionally followed by a colon and port number to override
262 * the default port 631, e.g. "hostname:8631". Pass @code NULL@ to restore the
263 * default server name and port.
264 *
265 * Note: The current server is tracked separately for each thread in a program.
266 * Multi-threaded programs that override the server need to do so in each
267 * thread for the same server to be used.
ef416fc2 268 */
269
270void
271cupsSetServer(const char *server) /* I - Server name */
272{
273 char *port; /* Pointer to port */
274 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
275
276
277 if (server)
278 {
279 strlcpy(cg->server, server, sizeof(cg->server));
280
281 if (cg->server[0] != '/' && (port = strrchr(cg->server, ':')) != NULL &&
282 !strchr(port, ']') && isdigit(port[1] & 255))
283 {
284 *port++ = '\0';
285
e07d4801 286 cg->ipp_port = atoi(port);
ef416fc2 287 }
288
289 if (cg->server[0] == '/')
290 strcpy(cg->servername, "localhost");
291 else
292 strlcpy(cg->servername, cg->server, sizeof(cg->servername));
293 }
294 else
295 {
296 cg->server[0] = '\0';
297 cg->servername[0] = '\0';
298 }
5a738aea
MS
299
300 if (cg->http)
301 {
302 httpClose(cg->http);
303 cg->http = NULL;
304 }
ef416fc2 305}
306
307
308/*
309 * 'cupsSetUser()' - Set the default user name.
310 *
5a738aea 311 * Pass @code NULL@ to restore the default user name.
5a6b583a
MS
312 *
313 * Note: The current user name is tracked separately for each thread in a
314 * program. Multi-threaded programs that override the user name need to do so
315 * in each thread for the same user name to be used.
ef416fc2 316 */
317
318void
319cupsSetUser(const char *user) /* I - User name */
320{
321 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
322
323
324 if (user)
325 strlcpy(cg->user, user, sizeof(cg->user));
326 else
327 cg->user[0] = '\0';
328}
329
330
ef416fc2 331/*
332 * 'cupsUser()' - Return the current user's name.
5a6b583a
MS
333 *
334 * Note: The current user name is tracked separately for each thread in a
335 * program. Multi-threaded programs that override the user name with the
336 * @link cupsSetUser@ function need to do so in each thread for the same user
337 * name to be used.
ef416fc2 338 */
339
340const char * /* O - User name */
341cupsUser(void)
342{
6d2f911b 343 const char *user; /* USER environment variable */
ef416fc2 344 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
345
346
347 if (!cg->user[0])
348 {
5a6b583a
MS
349#ifdef WIN32
350 /*
351 * Get the current user name from the OS...
352 */
ef416fc2 353
5a6b583a 354 DWORD size; /* Size of string */
ef416fc2 355
356 size = sizeof(cg->user);
357 if (!GetUserName(cg->user, &size))
ef416fc2 358#else
ef416fc2 359 /*
5a6b583a 360 * Get the user name corresponding to the current UID...
ef416fc2 361 */
362
5a6b583a 363 struct passwd *pwd; /* User/password entry */
ef416fc2 364
5a6b583a
MS
365 setpwent();
366 if ((pwd = getpwuid(getuid())) != NULL)
ef416fc2 367 {
368 /*
5a6b583a 369 * Found a match!
ef416fc2 370 */
371
ef416fc2 372 strlcpy(cg->user, pwd->pw_name, sizeof(cg->user));
373 }
5a6b583a
MS
374 else
375#endif /* WIN32 */
6d2f911b
MS
376 if ((user = getenv("USER")) != NULL)
377 {
378 /*
379 * Use the username from the "USER" environment variable...
380 */
381 strlcpy(cg->user, user, sizeof(cg->user));
382 }
383 else
5a6b583a
MS
384 {
385 /*
386 * Use the default "unknown" user name...
387 */
6d2f911b 388
5a6b583a
MS
389 strcpy(cg->user, "unknown");
390 }
ef416fc2 391 }
392
393 return (cg->user);
394}
395
396
397/*
398 * '_cupsGetPassword()' - Get a password from the user.
399 */
400
401const char * /* O - Password */
402_cupsGetPassword(const char *prompt) /* I - Prompt string */
403{
5a6b583a
MS
404#ifdef WIN32
405 /*
406 * Currently no console password support is provided on Windows.
407 */
408
409 return (NULL);
410
411#else
412 /*
413 * Use the standard getpass function to get a password from the console.
414 */
415
ef416fc2 416 return (getpass(prompt));
ef416fc2 417#endif /* WIN32 */
5a6b583a 418}
ef416fc2 419
420
421/*
e07d4801 422 * '_cupsSetDefaults()' - Set the default server, port, and encryption.
b423cd4c 423 */
424
e07d4801
MS
425void
426_cupsSetDefaults(void)
b423cd4c 427{
428 cups_file_t *fp; /* File */
e07d4801
MS
429 const char *home, /* Home directory of user */
430 *cups_encryption, /* CUPS_ENCRYPTION env var */
431 *cups_server; /* CUPS_SERVER env var */
b423cd4c 432 char filename[1024]; /* Filename */
433 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
434
435
e07d4801
MS
436 DEBUG_puts("_cupsSetDefaults()");
437
438 /*
439 * First collect environment variables...
440 */
441
442 cups_encryption = getenv("CUPS_ENCRYPTION");
443 cups_server = getenv("CUPS_SERVER");
444
445 /*
446 * Then, if needed, the .cups/client.conf or .cupsrc file in the home
447 * directory...
448 */
449
450 if ((cg->encryption == (http_encryption_t)-1 || !cg->server[0] ||
451 !cg->ipp_port) && (home = getenv("HOME")) != NULL)
b423cd4c 452 {
453 /*
5a6b583a 454 * Look for ~/.cups/client.conf...
b423cd4c 455 */
456
457 snprintf(filename, sizeof(filename), "%s/.cups/client.conf", home);
5a6b583a 458 if ((fp = cupsFileOpen(filename, "r")) != NULL)
e07d4801
MS
459 {
460 cups_read_client_conf(fp, cg, cups_encryption, cups_server);
461 cupsFileClose(fp);
462 }
463 }
464
465 if (cg->encryption == (http_encryption_t)-1 || !cg->server[0] ||
466 !cg->ipp_port)
467 {
468 /*
469 * Look for CUPS_SERVERROOT/client.conf...
470 */
471
472 snprintf(filename, sizeof(filename), "%s/client.conf", cg->cups_serverroot);
b423cd4c 473 if ((fp = cupsFileOpen(filename, "r")) != NULL)
d09495fa 474 {
e07d4801
MS
475 cups_read_client_conf(fp, cg, cups_encryption, cups_server);
476 cupsFileClose(fp);
477 }
478 }
479
480 /*
481 * If we still have things that aren't set, use the compiled in defaults...
482 */
483
484 if (cg->encryption == (http_encryption_t)-1)
485 cg->encryption = HTTP_ENCRYPT_IF_REQUESTED;
486
487 if (!cg->server[0])
488 {
489 if (!cups_server)
490 {
491#ifdef CUPS_DEFAULT_DOMAINSOCKET
492 /*
493 * If we are compiled with domain socket support, only use the
494 * domain socket if it exists and has the right permissions...
495 */
496
497 struct stat sockinfo; /* Domain socket information */
498
499 if (!stat(CUPS_DEFAULT_DOMAINSOCKET, &sockinfo) &&
500 (sockinfo.st_mode & S_IRWXO) == S_IRWXO)
501 cups_server = CUPS_DEFAULT_DOMAINSOCKET;
502 else
503#endif /* CUPS_DEFAULT_DOMAINSOCKET */
504 cups_server = "localhost";
505 }
506
507 cupsSetServer(cups_server);
508 }
509
510 if (!cg->ipp_port)
511 {
512 const char *ipp_port; /* IPP_PORT environment variable */
6d2f911b 513 struct servent *service; /* Port number info */
e07d4801
MS
514
515
516 if ((ipp_port = getenv("IPP_PORT")) != NULL)
517 {
518 if ((cg->ipp_port = atoi(ipp_port)) <= 0)
519 cg->ipp_port = CUPS_DEFAULT_IPP_PORT;
d09495fa 520 }
e07d4801
MS
521 else if ((service = getservbyname("ipp", NULL)) == NULL ||
522 service->s_port <= 0)
523 cg->ipp_port = CUPS_DEFAULT_IPP_PORT;
524 else
525 cg->ipp_port = ntohs(service->s_port);
526 }
527}
528
529
530/*
531 * 'cups_read_client_conf()' - Read a client.conf file.
532 */
533
534static void
535cups_read_client_conf(
536 cups_file_t *fp, /* I - File to read */
537 _cups_globals_t *cg, /* I - Global data */
538 const char *cups_encryption, /* I - CUPS_ENCRYPTION env var */
539 const char *cups_server) /* I - CUPS_SERVER env var */
540{
541 int linenum; /* Current line number */
542 char line[1024], /* Line from file */
543 *value, /* Pointer into line */
544 encryption[1024], /* Encryption value */
545 server_name[1024]; /* ServerName value */
546
547
548 /*
549 * Read from the file...
550 */
551
552 linenum = 0;
553 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
554 {
555 if (!cups_encryption && cg->encryption == (http_encryption_t)-1 &&
556 !strcasecmp(line, "Encryption") && value)
557 {
558 strlcpy(encryption, value, sizeof(encryption));
559 cups_encryption = encryption;
560 }
561 else if (!cups_server && (!cg->server[0] || !cg->ipp_port) &&
562 !strcasecmp(line, "ServerName") && value)
563 {
564 strlcpy(server_name, value, sizeof(server_name));
565 cups_server = server_name;
566 }
567 }
568
569 /*
570 * Set values...
571 */
572
573 if (cg->encryption == (http_encryption_t)-1 && cups_encryption)
574 {
575 if (!strcasecmp(cups_encryption, "never"))
576 cg->encryption = HTTP_ENCRYPT_NEVER;
577 else if (!strcasecmp(cups_encryption, "always"))
578 cg->encryption = HTTP_ENCRYPT_ALWAYS;
579 else if (!strcasecmp(cups_encryption, "required"))
580 cg->encryption = HTTP_ENCRYPT_REQUIRED;
581 else
582 cg->encryption = HTTP_ENCRYPT_IF_REQUESTED;
b423cd4c 583 }
584
e07d4801
MS
585 if ((!cg->server[0] || !cg->ipp_port) && cups_server)
586 {
587 if (!cg->server[0])
588 {
589 /*
590 * Copy server name...
591 */
592
593 strlcpy(cg->server, cups_server, sizeof(cg->server));
594
595 if (cg->server[0] != '/' && (value = strrchr(cg->server, ':')) != NULL &&
596 !strchr(value, ']') && isdigit(value[1] & 255))
597 *value++ = '\0';
598 else
599 value = NULL;
600
601 if (cg->server[0] == '/')
602 strcpy(cg->servername, "localhost");
603 else
604 strlcpy(cg->servername, cg->server, sizeof(cg->servername));
605 }
606 else if (cups_server[0] != '/' &&
607 (value = strrchr(cups_server, ':')) != NULL &&
608 !strchr(value, ']') && isdigit(value[1] & 255))
609 value ++;
610 else
611 value = NULL;
612
613 if (!cg->ipp_port && value)
614 cg->ipp_port = atoi(value);
615 }
b423cd4c 616}
617
618
619/*
e07d4801 620 * End of "$Id: usersys.c 8498 2009-04-13 17:03:15Z mike $".
ef416fc2 621 */