]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/usersys.c
Merge changes from CUPS 1.5svn-r9641
[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.
7cf5915e 25 * cupsSetClientCertCB() - Set the client certificate callback.
b423cd4c 26 * cupsSetEncryption() - Set the encryption preference.
27 * cupsSetPasswordCB() - Set the password callback for CUPS.
f11a948a 28 * cupsSetPasswordCB2() - Set the advanced password callback for CUPS.
5a6b583a 29 * cupsSetServer() - Set the default server name and port.
7cf5915e 30 * cupsSetServerCertCB() - Set the server certificate callback.
b423cd4c 31 * cupsSetUser() - Set the default user name.
e07d4801 32 * cupsUser() - Return the current user's name.
b423cd4c 33 * _cupsGetPassword() - Get a password from the user.
e07d4801
MS
34 * _cupsSetDefaults() - Set the default server, port, and encryption.
35 * cups_read_client_conf() - Read a client.conf file.
ef416fc2 36 */
37
38/*
39 * Include necessary headers...
40 */
41
71e16022 42#include "cups-private.h"
ef416fc2 43#include <stdlib.h>
e00b005a 44#include <sys/stat.h>
ef416fc2 45#ifdef WIN32
46# include <windows.h>
5a6b583a
MS
47#else
48# include <pwd.h>
ef416fc2 49#endif /* WIN32 */
50
51
b423cd4c 52/*
53 * Local functions...
54 */
55
e07d4801
MS
56static void cups_read_client_conf(cups_file_t *fp,
57 _cups_globals_t *cg,
58 const char *cups_encryption,
7cf5915e
MS
59 const char *cups_server,
60 const char *cups_anyroot,
61 const char *cups_expiredroot,
62 const char *cups_expiredcerts);
b423cd4c 63
64
ef416fc2 65/*
5a6b583a 66 * 'cupsEncryption()' - Get the current encryption settings.
ef416fc2 67 *
68 * The default encryption setting comes from the CUPS_ENCRYPTION
568fa3fa 69 * environment variable, then the ~/.cups/client.conf file, and finally the
ef416fc2 70 * /etc/cups/client.conf file. If not set, the default is
5a738aea 71 * @code HTTP_ENCRYPT_IF_REQUESTED@.
5a6b583a
MS
72 *
73 * Note: The current encryption setting is tracked separately for each thread
74 * in a program. Multi-threaded programs that override the setting via the
75 * @link cupsSetEncryption@ function need to do so in each thread for the same
76 * setting to be used.
ef416fc2 77 */
78
79http_encryption_t /* O - Encryption settings */
80cupsEncryption(void)
81{
ef416fc2 82 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
83
84
ef416fc2 85 if (cg->encryption == (http_encryption_t)-1)
e07d4801 86 _cupsSetDefaults();
ef416fc2 87
88 return (cg->encryption);
89}
90
91
92/*
93 * 'cupsGetPassword()' - Get a password from the user.
94 *
5a738aea 95 * Uses the current password callback function. Returns @code NULL@ if the
ecdc0628 96 * user does not provide a password.
5a6b583a
MS
97 *
98 * Note: The current password callback function is tracked separately for each
99 * thread in a program. Multi-threaded programs that override the setting via
100 * the @link cupsSetPasswordCB@ or @link cupsSetPasswordCB2@ functions need to
101 * do so in each thread for the same function to be used.
ef416fc2 102 */
103
104const char * /* O - Password */
105cupsGetPassword(const char *prompt) /* I - Prompt string */
106{
f11a948a
MS
107 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
108
109
110 return ((cg->password_cb)(prompt, NULL, NULL, NULL, cg->password_data));
111}
112
113
114/*
115 * 'cupsGetPassword2()' - Get a password from the user using the advanced
5a6b583a 116 * password callback.
f11a948a
MS
117 *
118 * Uses the current password callback function. Returns @code NULL@ if the
119 * user does not provide a password.
120 *
5a6b583a
MS
121 * Note: The current password callback function is tracked separately for each
122 * thread in a program. Multi-threaded programs that override the setting via
123 * the @link cupsSetPasswordCB@ or @link cupsSetPasswordCB2@ functions need to
124 * do so in each thread for the same function to be used.
125 *
178cb736 126 * @since CUPS 1.4/Mac OS X 10.6@
f11a948a
MS
127 */
128
129const char * /* O - Password */
130cupsGetPassword2(const char *prompt, /* I - Prompt string */
131 http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
132 const char *method, /* I - Request method ("GET", "POST", "PUT") */
133 const char *resource) /* I - Resource path */
134{
135 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
136
137
138 if (!http)
139 http = _cupsConnect();
140
141 return ((cg->password_cb)(prompt, http, method, resource, cg->password_data));
ef416fc2 142}
143
144
ef416fc2 145/*
5a6b583a
MS
146 * 'cupsServer()' - Return the hostname/address of the current server.
147 *
148 * The default server comes from the CUPS_SERVER environment variable, then the
149 * ~/.cups/client.conf file, and finally the /etc/cups/client.conf file. If not
150 * set, the default is the local system - either "localhost" or a domain socket
151 * path.
ef416fc2 152 *
5a6b583a
MS
153 * The returned value can be a fully-qualified hostname, a numeric IPv4 or IPv6
154 * address, or a domain socket pathname.
155 *
156 * Note: The current server is tracked separately for each thread in a program.
157 * Multi-threaded programs that override the server via the
158 * @link cupsSetServer@ function need to do so in each thread for the same
159 * server to be used.
ef416fc2 160 */
161
162const char * /* O - Server name */
163cupsServer(void)
164{
ef416fc2 165 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
166
167
ef416fc2 168 if (!cg->server[0])
e07d4801 169 _cupsSetDefaults();
ef416fc2 170
e07d4801
MS
171 return (cg->server);
172}
ef416fc2 173
d09495fa 174
7cf5915e
MS
175/*
176 * 'cupsSetClientCertCB()' - Set the client certificate callback.
177 *
178 * Pass @code NULL@ to restore the default callback.
179 *
180 * Note: The current certificate callback is tracked separately for each thread
181 * in a program. Multi-threaded programs that override the callback need to do
182 * so in each thread for the same callback to be used.
183 *
184 * @since CUPS 1.5@
185 */
186
187void
188cupsSetClientCertCB(
189 cups_client_cert_cb_t cb, /* I - Callback function */
190 void *user_data) /* I - User data pointer */
191{
192 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
193
194
195 cg->client_cert_cb = cb;
196 cg->client_cert_data = user_data;
197}
198
199
200/*
201 * 'cupsSetCredentials()' - Set the default credentials to be used for SSL/TLS
202 * connections.
203 *
204 * Note: The default credentials are tracked separately for each thread in a
205 * program. Multi-threaded programs that override the setting need to do so in
206 * each thread for the same setting to be used.
207 *
208 * @since CUPS 1.5@
209 */
210
211int /* O - Status of call (0 = success) */
212cupsSetCredentials(
213 cups_array_t *credentials) /* I - Array of credentials */
214{
215 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
216
217
218 if (cupsArrayCount(credentials) < 1)
219 return (-1);
220
221 _httpFreeCredentials(cg->tls_credentials);
222 cg->tls_credentials = _httpConvertCredentials(credentials);
223
224 return (cg->tls_credentials ? 0 : -1);
225}
226
227
e07d4801
MS
228/*
229 * 'cupsSetEncryption()' - Set the encryption preference.
5a6b583a
MS
230 *
231 * The default encryption setting comes from the CUPS_ENCRYPTION
232 * environment variable, then the ~/.cups/client.conf file, and finally the
233 * /etc/cups/client.conf file. If not set, the default is
234 * @code HTTP_ENCRYPT_IF_REQUESTED@.
235 *
236 * Note: The current encryption setting is tracked separately for each thread
237 * in a program. Multi-threaded programs that override the setting need to do
238 * so in each thread for the same setting to be used.
e07d4801 239 */
ef416fc2 240
e07d4801
MS
241void
242cupsSetEncryption(http_encryption_t e) /* I - New encryption preference */
243{
244 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
ef416fc2 245
ef416fc2 246
e07d4801 247 cg->encryption = e;
ef416fc2 248
e07d4801
MS
249 if (cg->http)
250 httpEncryption(cg->http, e);
ef416fc2 251}
252
253
254/*
255 * 'cupsSetPasswordCB()' - Set the password callback for CUPS.
256 *
5a6b583a
MS
257 * Pass @code NULL@ to restore the default (console) password callback, which
258 * reads the password from the console. Programs should call either this
259 * function or @link cupsSetPasswordCB2@, as only one callback can be registered
260 * by a program per thread.
261 *
262 * Note: The current password callback is tracked separately for each thread
263 * in a program. Multi-threaded programs that override the callback need to do
264 * so in each thread for the same callback to be used.
ef416fc2 265 */
266
267void
268cupsSetPasswordCB(cups_password_cb_t cb)/* I - Callback function */
269{
270 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
271
272
f11a948a
MS
273 if (cb == (cups_password_cb_t)0)
274 cg->password_cb = (cups_password_cb2_t)_cupsGetPassword;
275 else
276 cg->password_cb = (cups_password_cb2_t)cb;
277
278 cg->password_data = NULL;
279}
280
281
282/*
283 * 'cupsSetPasswordCB2()' - Set the advanced password callback for CUPS.
284 *
5a6b583a
MS
285 * Pass @code NULL@ to restore the default (console) password callback, which
286 * reads the password from the console. Programs should call either this
287 * function or @link cupsSetPasswordCB2@, as only one callback can be registered
288 * by a program per thread.
289 *
290 * Note: The current password callback is tracked separately for each thread
291 * in a program. Multi-threaded programs that override the callback need to do
292 * so in each thread for the same callback to be used.
f11a948a 293 *
178cb736 294 * @since CUPS 1.4/Mac OS X 10.6@
f11a948a
MS
295 */
296
297void
298cupsSetPasswordCB2(
299 cups_password_cb2_t cb, /* I - Callback function */
300 void *user_data) /* I - User data pointer */
301{
302 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
303
304
305 if (cb == (cups_password_cb2_t)0)
306 cg->password_cb = (cups_password_cb2_t)_cupsGetPassword;
ef416fc2 307 else
308 cg->password_cb = cb;
f11a948a
MS
309
310 cg->password_data = user_data;
ef416fc2 311}
312
313
314/*
5a6b583a 315 * 'cupsSetServer()' - Set the default server name and port.
ef416fc2 316 *
317 * The "server" string can be a fully-qualified hostname, a numeric
5a6b583a
MS
318 * IPv4 or IPv6 address, or a domain socket pathname. Hostnames and numeric IP
319 * addresses can be optionally followed by a colon and port number to override
320 * the default port 631, e.g. "hostname:8631". Pass @code NULL@ to restore the
321 * default server name and port.
322 *
323 * Note: The current server is tracked separately for each thread in a program.
324 * Multi-threaded programs that override the server need to do so in each
325 * thread for the same server to be used.
ef416fc2 326 */
327
328void
329cupsSetServer(const char *server) /* I - Server name */
330{
331 char *port; /* Pointer to port */
332 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
333
334
335 if (server)
336 {
337 strlcpy(cg->server, server, sizeof(cg->server));
338
339 if (cg->server[0] != '/' && (port = strrchr(cg->server, ':')) != NULL &&
340 !strchr(port, ']') && isdigit(port[1] & 255))
341 {
342 *port++ = '\0';
343
e07d4801 344 cg->ipp_port = atoi(port);
ef416fc2 345 }
346
347 if (cg->server[0] == '/')
348 strcpy(cg->servername, "localhost");
349 else
350 strlcpy(cg->servername, cg->server, sizeof(cg->servername));
351 }
352 else
353 {
354 cg->server[0] = '\0';
355 cg->servername[0] = '\0';
356 }
5a738aea
MS
357
358 if (cg->http)
359 {
360 httpClose(cg->http);
361 cg->http = NULL;
362 }
ef416fc2 363}
364
365
7cf5915e
MS
366/*
367 * 'cupsSetServerCertCB()' - Set the server certificate callback.
368 *
369 * Pass @code NULL@ to restore the default callback.
370 *
371 * Note: The current credentials callback is tracked separately for each thread
372 * in a program. Multi-threaded programs that override the callback need to do
373 * so in each thread for the same callback to be used.
374 *
375 * @since CUPS 1.5@
376 */
377
378void
379cupsSetServerCertCB(
380 cups_server_cert_cb_t cb, /* I - Callback function */
381 void *user_data) /* I - User data pointer */
382{
383 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
384
385
386 cg->server_cert_cb = cb;
387 cg->server_cert_data = user_data;
388}
389
390
ef416fc2 391/*
392 * 'cupsSetUser()' - Set the default user name.
393 *
5a738aea 394 * Pass @code NULL@ to restore the default user name.
5a6b583a
MS
395 *
396 * Note: The current user name is tracked separately for each thread in a
397 * program. Multi-threaded programs that override the user name need to do so
398 * in each thread for the same user name to be used.
ef416fc2 399 */
400
401void
402cupsSetUser(const char *user) /* I - User name */
403{
404 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
405
406
407 if (user)
408 strlcpy(cg->user, user, sizeof(cg->user));
409 else
410 cg->user[0] = '\0';
411}
412
413
ef416fc2 414/*
415 * 'cupsUser()' - Return the current user's name.
5a6b583a
MS
416 *
417 * Note: The current user name is tracked separately for each thread in a
418 * program. Multi-threaded programs that override the user name with the
419 * @link cupsSetUser@ function need to do so in each thread for the same user
420 * name to be used.
ef416fc2 421 */
422
423const char * /* O - User name */
424cupsUser(void)
425{
6d2f911b 426 const char *user; /* USER environment variable */
ef416fc2 427 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
428
429
430 if (!cg->user[0])
431 {
5a6b583a
MS
432#ifdef WIN32
433 /*
434 * Get the current user name from the OS...
435 */
ef416fc2 436
5a6b583a 437 DWORD size; /* Size of string */
ef416fc2 438
439 size = sizeof(cg->user);
440 if (!GetUserName(cg->user, &size))
ef416fc2 441#else
ef416fc2 442 /*
5a6b583a 443 * Get the user name corresponding to the current UID...
ef416fc2 444 */
445
5a6b583a 446 struct passwd *pwd; /* User/password entry */
ef416fc2 447
5a6b583a
MS
448 setpwent();
449 if ((pwd = getpwuid(getuid())) != NULL)
ef416fc2 450 {
451 /*
5a6b583a 452 * Found a match!
ef416fc2 453 */
454
ef416fc2 455 strlcpy(cg->user, pwd->pw_name, sizeof(cg->user));
456 }
5a6b583a
MS
457 else
458#endif /* WIN32 */
6d2f911b
MS
459 if ((user = getenv("USER")) != NULL)
460 {
461 /*
462 * Use the username from the "USER" environment variable...
463 */
464 strlcpy(cg->user, user, sizeof(cg->user));
465 }
466 else
5a6b583a
MS
467 {
468 /*
469 * Use the default "unknown" user name...
470 */
6d2f911b 471
5a6b583a
MS
472 strcpy(cg->user, "unknown");
473 }
ef416fc2 474 }
475
476 return (cg->user);
477}
478
479
480/*
481 * '_cupsGetPassword()' - Get a password from the user.
482 */
483
484const char * /* O - Password */
485_cupsGetPassword(const char *prompt) /* I - Prompt string */
486{
5a6b583a
MS
487#ifdef WIN32
488 /*
489 * Currently no console password support is provided on Windows.
490 */
491
492 return (NULL);
493
494#else
495 /*
7cf5915e
MS
496 * Use the standard getpass function to get a password from the console. An
497 * empty password is treated as canceling the authentication request.
5a6b583a
MS
498 */
499
7cf5915e
MS
500 const char *password = getpass(prompt);
501 /* Password string */
502
503 if (!password || !password[0])
504 return (NULL);
505 else
506 return (password);
ef416fc2 507#endif /* WIN32 */
5a6b583a 508}
ef416fc2 509
510
511/*
e07d4801 512 * '_cupsSetDefaults()' - Set the default server, port, and encryption.
b423cd4c 513 */
514
e07d4801
MS
515void
516_cupsSetDefaults(void)
b423cd4c 517{
518 cups_file_t *fp; /* File */
e07d4801
MS
519 const char *home, /* Home directory of user */
520 *cups_encryption, /* CUPS_ENCRYPTION env var */
7cf5915e
MS
521 *cups_server, /* CUPS_SERVER env var */
522 *cups_anyroot, /* CUPS_ANYROOT env var */
523 *cups_expiredroot, /* CUPS_EXPIREDROOT env var */
524 *cups_expiredcerts; /* CUPS_EXPIREDCERTS env var */
b423cd4c 525 char filename[1024]; /* Filename */
526 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
527
528
e07d4801
MS
529 DEBUG_puts("_cupsSetDefaults()");
530
531 /*
532 * First collect environment variables...
533 */
534
7cf5915e
MS
535 cups_encryption = getenv("CUPS_ENCRYPTION");
536 cups_server = getenv("CUPS_SERVER");
537 cups_anyroot = getenv("CUPS_ANYROOT");
538 cups_expiredroot = getenv("CUPS_EXPIREDROOT");
539 cups_expiredcerts = getenv("CUPS_EXPIREDCERTS");
e07d4801
MS
540
541 /*
10d09e33
MS
542 * Then, if needed, read the ~/.cups/client.conf or /etc/cups/client.conf
543 * files to get the default values...
e07d4801
MS
544 */
545
e07d4801
MS
546 if (cg->encryption == (http_encryption_t)-1 || !cg->server[0] ||
547 !cg->ipp_port)
548 {
10d09e33 549 if ((home = getenv("HOME")) != NULL)
d09495fa 550 {
10d09e33
MS
551 /*
552 * Look for ~/.cups/client.conf...
553 */
e07d4801 554
10d09e33
MS
555 snprintf(filename, sizeof(filename), "%s/.cups/client.conf", home);
556 fp = cupsFileOpen(filename, "r");
557 }
558 else
559 fp = NULL;
e07d4801 560
10d09e33 561 if (!fp)
e07d4801 562 {
e07d4801 563 /*
10d09e33 564 * Look for CUPS_SERVERROOT/client.conf...
e07d4801
MS
565 */
566
10d09e33
MS
567 snprintf(filename, sizeof(filename), "%s/client.conf",
568 cg->cups_serverroot);
569 fp = cupsFileOpen(filename, "r");
e07d4801
MS
570 }
571
10d09e33
MS
572 /*
573 * Read the configuration file and apply any environment variables; both
574 * functions handle NULL cups_file_t pointers...
575 */
e07d4801 576
10d09e33
MS
577 cups_read_client_conf(fp, cg, cups_encryption, cups_server,
578 cups_anyroot, cups_expiredroot,
579 cups_expiredcerts);
580 cupsFileClose(fp);
e07d4801
MS
581 }
582}
583
584
585/*
586 * 'cups_read_client_conf()' - Read a client.conf file.
587 */
588
589static void
590cups_read_client_conf(
591 cups_file_t *fp, /* I - File to read */
592 _cups_globals_t *cg, /* I - Global data */
593 const char *cups_encryption, /* I - CUPS_ENCRYPTION env var */
7cf5915e
MS
594 const char *cups_server, /* I - CUPS_SERVER env var */
595 const char *cups_anyroot, /* I - CUPS_ANYROOT env var */
596 const char *cups_expiredroot, /* I - CUPS_EXPIREDROOT env var */
597 const char *cups_expiredcerts) /* I - CUPS_EXPIREDCERTS env var */
e07d4801
MS
598{
599 int linenum; /* Current line number */
600 char line[1024], /* Line from file */
601 *value, /* Pointer into line */
602 encryption[1024], /* Encryption value */
7cf5915e
MS
603 server_name[1024], /* ServerName value */
604 any_root[1024], /* AllowAnyRoot value */
605 expired_root[1024], /* AllowExpiredRoot value */
606 expired_certs[1024]; /* AllowExpiredCerts value */
e07d4801
MS
607
608
609 /*
610 * Read from the file...
611 */
612
613 linenum = 0;
614 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
615 {
616 if (!cups_encryption && cg->encryption == (http_encryption_t)-1 &&
617 !strcasecmp(line, "Encryption") && value)
618 {
619 strlcpy(encryption, value, sizeof(encryption));
620 cups_encryption = encryption;
621 }
622 else if (!cups_server && (!cg->server[0] || !cg->ipp_port) &&
623 !strcasecmp(line, "ServerName") && value)
624 {
625 strlcpy(server_name, value, sizeof(server_name));
626 cups_server = server_name;
627 }
7cf5915e
MS
628 else if (!cups_anyroot && !strcasecmp(line, "AllowAnyRoot") && value)
629 {
630 strlcpy(any_root, value, sizeof(any_root));
631 cups_anyroot = any_root;
632 }
633 else if (!cups_expiredroot && !strcasecmp(line, "AllowExpiredRoot") &&
634 value)
635 {
636 strlcpy(expired_root, value, sizeof(expired_root));
637 cups_expiredroot = expired_root;
638 }
639 else if (!cups_expiredcerts && !strcasecmp(line, "AllowExpiredCerts") &&
640 value)
641 {
642 strlcpy(expired_certs, value, sizeof(expired_certs));
643 cups_expiredcerts = expired_certs;
644 }
e07d4801
MS
645 }
646
647 /*
648 * Set values...
649 */
650
651 if (cg->encryption == (http_encryption_t)-1 && cups_encryption)
652 {
653 if (!strcasecmp(cups_encryption, "never"))
654 cg->encryption = HTTP_ENCRYPT_NEVER;
655 else if (!strcasecmp(cups_encryption, "always"))
656 cg->encryption = HTTP_ENCRYPT_ALWAYS;
657 else if (!strcasecmp(cups_encryption, "required"))
658 cg->encryption = HTTP_ENCRYPT_REQUIRED;
659 else
660 cg->encryption = HTTP_ENCRYPT_IF_REQUESTED;
b423cd4c 661 }
662
e07d4801
MS
663 if ((!cg->server[0] || !cg->ipp_port) && cups_server)
664 {
665 if (!cg->server[0])
666 {
667 /*
668 * Copy server name...
669 */
670
671 strlcpy(cg->server, cups_server, sizeof(cg->server));
672
673 if (cg->server[0] != '/' && (value = strrchr(cg->server, ':')) != NULL &&
674 !strchr(value, ']') && isdigit(value[1] & 255))
675 *value++ = '\0';
676 else
677 value = NULL;
678
679 if (cg->server[0] == '/')
680 strcpy(cg->servername, "localhost");
681 else
682 strlcpy(cg->servername, cg->server, sizeof(cg->servername));
683 }
684 else if (cups_server[0] != '/' &&
685 (value = strrchr(cups_server, ':')) != NULL &&
686 !strchr(value, ']') && isdigit(value[1] & 255))
687 value ++;
688 else
689 value = NULL;
690
691 if (!cg->ipp_port && value)
692 cg->ipp_port = atoi(value);
693 }
7cf5915e 694
10d09e33
MS
695 if (!cg->server[0])
696 {
697#ifdef CUPS_DEFAULT_DOMAINSOCKET
698 /*
699 * If we are compiled with domain socket support, only use the
700 * domain socket if it exists and has the right permissions...
701 */
702
703 struct stat sockinfo; /* Domain socket information */
704
705 if (!stat(CUPS_DEFAULT_DOMAINSOCKET, &sockinfo) &&
706 (sockinfo.st_mode & S_IRWXO) == S_IRWXO)
707 cups_server = CUPS_DEFAULT_DOMAINSOCKET;
708 else
709#endif /* CUPS_DEFAULT_DOMAINSOCKET */
710 cups_server = "localhost";
711
712 cupsSetServer(cups_server);
713 }
714
715 if (!cg->ipp_port)
716 {
717 const char *ipp_port; /* IPP_PORT environment variable */
718 struct servent *service; /* Port number info */
719
720
721 if ((ipp_port = getenv("IPP_PORT")) != NULL)
722 {
723 if ((cg->ipp_port = atoi(ipp_port)) <= 0)
724 cg->ipp_port = CUPS_DEFAULT_IPP_PORT;
725 }
726 else if ((service = getservbyname("ipp", NULL)) == NULL ||
727 service->s_port <= 0)
728 cg->ipp_port = CUPS_DEFAULT_IPP_PORT;
729 else
730 cg->ipp_port = ntohs(service->s_port);
731 }
732
7cf5915e
MS
733 if (cups_anyroot)
734 cg->any_root = !strcasecmp(cups_anyroot, "yes") ||
735 !strcasecmp(cups_anyroot, "on") ||
736 !strcasecmp(cups_anyroot, "true");
737
738 if (cups_expiredroot)
739 cg->expired_root = !strcasecmp(cups_expiredroot, "yes") ||
740 !strcasecmp(cups_expiredroot, "on") ||
741 !strcasecmp(cups_expiredroot, "true");
742
743 if (cups_expiredcerts)
744 cg->expired_certs = !strcasecmp(cups_expiredcerts, "yes") ||
745 !strcasecmp(cups_expiredcerts, "on") ||
746 !strcasecmp(cups_expiredcerts, "true");
b423cd4c 747}
748
749
750/*
e07d4801 751 * End of "$Id: usersys.c 8498 2009-04-13 17:03:15Z mike $".
ef416fc2 752 */