]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/usersys.c
Merge changes from CUPS 1.5svn-r9049 (private header support)
[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{
343 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
344
345
346 if (!cg->user[0])
347 {
5a6b583a
MS
348#ifdef WIN32
349 /*
350 * Get the current user name from the OS...
351 */
ef416fc2 352
5a6b583a 353 DWORD size; /* Size of string */
ef416fc2 354
355 size = sizeof(cg->user);
356 if (!GetUserName(cg->user, &size))
ef416fc2 357#else
ef416fc2 358 /*
5a6b583a 359 * Get the user name corresponding to the current UID...
ef416fc2 360 */
361
5a6b583a 362 struct passwd *pwd; /* User/password entry */
ef416fc2 363
5a6b583a
MS
364 setpwent();
365 if ((pwd = getpwuid(getuid())) != NULL)
ef416fc2 366 {
367 /*
5a6b583a 368 * Found a match!
ef416fc2 369 */
370
ef416fc2 371 strlcpy(cg->user, pwd->pw_name, sizeof(cg->user));
372 }
5a6b583a
MS
373 else
374#endif /* WIN32 */
375 {
376 /*
377 * Use the default "unknown" user name...
378 */
379
380 strcpy(cg->user, "unknown");
381 }
ef416fc2 382 }
383
384 return (cg->user);
385}
386
387
388/*
389 * '_cupsGetPassword()' - Get a password from the user.
390 */
391
392const char * /* O - Password */
393_cupsGetPassword(const char *prompt) /* I - Prompt string */
394{
5a6b583a
MS
395#ifdef WIN32
396 /*
397 * Currently no console password support is provided on Windows.
398 */
399
400 return (NULL);
401
402#else
403 /*
404 * Use the standard getpass function to get a password from the console.
405 */
406
ef416fc2 407 return (getpass(prompt));
ef416fc2 408#endif /* WIN32 */
5a6b583a 409}
ef416fc2 410
411
412/*
e07d4801 413 * '_cupsSetDefaults()' - Set the default server, port, and encryption.
b423cd4c 414 */
415
e07d4801
MS
416void
417_cupsSetDefaults(void)
b423cd4c 418{
419 cups_file_t *fp; /* File */
e07d4801
MS
420 const char *home, /* Home directory of user */
421 *cups_encryption, /* CUPS_ENCRYPTION env var */
422 *cups_server; /* CUPS_SERVER env var */
b423cd4c 423 char filename[1024]; /* Filename */
424 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
425
426
e07d4801
MS
427 DEBUG_puts("_cupsSetDefaults()");
428
429 /*
430 * First collect environment variables...
431 */
432
433 cups_encryption = getenv("CUPS_ENCRYPTION");
434 cups_server = getenv("CUPS_SERVER");
435
436 /*
437 * Then, if needed, the .cups/client.conf or .cupsrc file in the home
438 * directory...
439 */
440
441 if ((cg->encryption == (http_encryption_t)-1 || !cg->server[0] ||
442 !cg->ipp_port) && (home = getenv("HOME")) != NULL)
b423cd4c 443 {
444 /*
5a6b583a 445 * Look for ~/.cups/client.conf...
b423cd4c 446 */
447
448 snprintf(filename, sizeof(filename), "%s/.cups/client.conf", home);
5a6b583a 449 if ((fp = cupsFileOpen(filename, "r")) != NULL)
e07d4801
MS
450 {
451 cups_read_client_conf(fp, cg, cups_encryption, cups_server);
452 cupsFileClose(fp);
453 }
454 }
455
456 if (cg->encryption == (http_encryption_t)-1 || !cg->server[0] ||
457 !cg->ipp_port)
458 {
459 /*
460 * Look for CUPS_SERVERROOT/client.conf...
461 */
462
463 snprintf(filename, sizeof(filename), "%s/client.conf", cg->cups_serverroot);
b423cd4c 464 if ((fp = cupsFileOpen(filename, "r")) != NULL)
d09495fa 465 {
e07d4801
MS
466 cups_read_client_conf(fp, cg, cups_encryption, cups_server);
467 cupsFileClose(fp);
468 }
469 }
470
471 /*
472 * If we still have things that aren't set, use the compiled in defaults...
473 */
474
475 if (cg->encryption == (http_encryption_t)-1)
476 cg->encryption = HTTP_ENCRYPT_IF_REQUESTED;
477
478 if (!cg->server[0])
479 {
480 if (!cups_server)
481 {
482#ifdef CUPS_DEFAULT_DOMAINSOCKET
483 /*
484 * If we are compiled with domain socket support, only use the
485 * domain socket if it exists and has the right permissions...
486 */
487
488 struct stat sockinfo; /* Domain socket information */
489
490 if (!stat(CUPS_DEFAULT_DOMAINSOCKET, &sockinfo) &&
491 (sockinfo.st_mode & S_IRWXO) == S_IRWXO)
492 cups_server = CUPS_DEFAULT_DOMAINSOCKET;
493 else
494#endif /* CUPS_DEFAULT_DOMAINSOCKET */
495 cups_server = "localhost";
496 }
497
498 cupsSetServer(cups_server);
499 }
500
501 if (!cg->ipp_port)
502 {
503 const char *ipp_port; /* IPP_PORT environment variable */
504 struct servent *service; /* Port number info */
505
506
507 if ((ipp_port = getenv("IPP_PORT")) != NULL)
508 {
509 if ((cg->ipp_port = atoi(ipp_port)) <= 0)
510 cg->ipp_port = CUPS_DEFAULT_IPP_PORT;
d09495fa 511 }
e07d4801
MS
512 else if ((service = getservbyname("ipp", NULL)) == NULL ||
513 service->s_port <= 0)
514 cg->ipp_port = CUPS_DEFAULT_IPP_PORT;
515 else
516 cg->ipp_port = ntohs(service->s_port);
517 }
518}
519
520
521/*
522 * 'cups_read_client_conf()' - Read a client.conf file.
523 */
524
525static void
526cups_read_client_conf(
527 cups_file_t *fp, /* I - File to read */
528 _cups_globals_t *cg, /* I - Global data */
529 const char *cups_encryption, /* I - CUPS_ENCRYPTION env var */
530 const char *cups_server) /* I - CUPS_SERVER env var */
531{
532 int linenum; /* Current line number */
533 char line[1024], /* Line from file */
534 *value, /* Pointer into line */
535 encryption[1024], /* Encryption value */
536 server_name[1024]; /* ServerName value */
537
538
539 /*
540 * Read from the file...
541 */
542
543 linenum = 0;
544 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
545 {
546 if (!cups_encryption && cg->encryption == (http_encryption_t)-1 &&
547 !strcasecmp(line, "Encryption") && value)
548 {
549 strlcpy(encryption, value, sizeof(encryption));
550 cups_encryption = encryption;
551 }
552 else if (!cups_server && (!cg->server[0] || !cg->ipp_port) &&
553 !strcasecmp(line, "ServerName") && value)
554 {
555 strlcpy(server_name, value, sizeof(server_name));
556 cups_server = server_name;
557 }
558 }
559
560 /*
561 * Set values...
562 */
563
564 if (cg->encryption == (http_encryption_t)-1 && cups_encryption)
565 {
566 if (!strcasecmp(cups_encryption, "never"))
567 cg->encryption = HTTP_ENCRYPT_NEVER;
568 else if (!strcasecmp(cups_encryption, "always"))
569 cg->encryption = HTTP_ENCRYPT_ALWAYS;
570 else if (!strcasecmp(cups_encryption, "required"))
571 cg->encryption = HTTP_ENCRYPT_REQUIRED;
572 else
573 cg->encryption = HTTP_ENCRYPT_IF_REQUESTED;
b423cd4c 574 }
575
e07d4801
MS
576 if ((!cg->server[0] || !cg->ipp_port) && cups_server)
577 {
578 if (!cg->server[0])
579 {
580 /*
581 * Copy server name...
582 */
583
584 strlcpy(cg->server, cups_server, sizeof(cg->server));
585
586 if (cg->server[0] != '/' && (value = strrchr(cg->server, ':')) != NULL &&
587 !strchr(value, ']') && isdigit(value[1] & 255))
588 *value++ = '\0';
589 else
590 value = NULL;
591
592 if (cg->server[0] == '/')
593 strcpy(cg->servername, "localhost");
594 else
595 strlcpy(cg->servername, cg->server, sizeof(cg->servername));
596 }
597 else if (cups_server[0] != '/' &&
598 (value = strrchr(cups_server, ':')) != NULL &&
599 !strchr(value, ']') && isdigit(value[1] & 255))
600 value ++;
601 else
602 value = NULL;
603
604 if (!cg->ipp_port && value)
605 cg->ipp_port = atoi(value);
606 }
b423cd4c 607}
608
609
610/*
e07d4801 611 * End of "$Id: usersys.c 8498 2009-04-13 17:03:15Z mike $".
ef416fc2 612 */