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