4 * User, system, and password routines for CUPS.
6 * Copyright 2007-2015 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products.
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/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * Include necessary headers...
22 #include "cups-private.h"
30 # include <sys/utsname.h>
38 #define _CUPS_PASSCHAR '*' /* Character that is echoed for password */
45 typedef struct _cups_client_conf_s
/**** client.conf config data ****/
48 int ssl_options
; /* SSLOptions values */
50 int any_root
, /* Allow any (e.g., self-signed) root */
51 expired_certs
, /* Allow expired certs */
52 validate_certs
; /* Validate certificates */
53 http_encryption_t encryption
; /* Encryption setting */
54 char user
[65], /* User name */
58 char gss_service_name
[32];
59 /* Kerberos service name */
60 #endif /* HAVE_GSSAPI */
61 } _cups_client_conf_t
;
68 static void cups_finalize_client_conf(_cups_client_conf_t
*cc
);
69 static void cups_init_client_conf(_cups_client_conf_t
*cc
);
70 static void cups_read_client_conf(cups_file_t
*fp
, _cups_client_conf_t
*cc
);
71 static void cups_set_default_ipp_port(_cups_globals_t
*cg
);
72 static void cups_set_encryption(_cups_client_conf_t
*cc
, const char *value
);
74 static void cups_set_gss_service_name(_cups_client_conf_t
*cc
, const char *value
);
75 #endif /* HAVE_GSSAPI */
76 static void cups_set_server_name(_cups_client_conf_t
*cc
, const char *value
);
78 static void cups_set_ssl_options(_cups_client_conf_t
*cc
, const char *value
);
80 static void cups_set_user(_cups_client_conf_t
*cc
, const char *value
);
84 * 'cupsEncryption()' - Get the current encryption settings.
86 * The default encryption setting comes from the CUPS_ENCRYPTION
87 * environment variable, then the ~/.cups/client.conf file, and finally the
88 * /etc/cups/client.conf file. If not set, the default is
89 * @code HTTP_ENCRYPTION_IF_REQUESTED@.
91 * Note: The current encryption setting is tracked separately for each thread
92 * in a program. Multi-threaded programs that override the setting via the
93 * @link cupsSetEncryption@ function need to do so in each thread for the same
97 http_encryption_t
/* O - Encryption settings */
100 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
103 if (cg
->encryption
== (http_encryption_t
)-1)
106 return (cg
->encryption
);
111 * 'cupsGetPassword()' - Get a password from the user.
113 * Uses the current password callback function. Returns @code NULL@ if the
114 * user does not provide a password.
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.
122 const char * /* O - Password */
123 cupsGetPassword(const char *prompt
) /* I - Prompt string */
125 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
128 return ((cg
->password_cb
)(prompt
, NULL
, NULL
, NULL
, cg
->password_data
));
133 * 'cupsGetPassword2()' - Get a password from the user using the advanced
136 * Uses the current password callback function. Returns @code NULL@ if the
137 * user does not provide a password.
139 * Note: The current password callback function is tracked separately for each
140 * thread in a program. Multi-threaded programs that override the setting via
141 * the @link cupsSetPasswordCB@ or @link cupsSetPasswordCB2@ functions need to
142 * do so in each thread for the same function to be used.
144 * @since CUPS 1.4/OS X 10.6@
147 const char * /* O - Password */
148 cupsGetPassword2(const char *prompt
, /* I - Prompt string */
149 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
150 const char *method
, /* I - Request method ("GET", "POST", "PUT") */
151 const char *resource
) /* I - Resource path */
153 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
157 http
= _cupsConnect();
159 return ((cg
->password_cb
)(prompt
, http
, method
, resource
, cg
->password_data
));
164 * 'cupsServer()' - Return the hostname/address of the current server.
166 * The default server comes from the CUPS_SERVER environment variable, then the
167 * ~/.cups/client.conf file, and finally the /etc/cups/client.conf file. If not
168 * set, the default is the local system - either "localhost" or a domain socket
171 * The returned value can be a fully-qualified hostname, a numeric IPv4 or IPv6
172 * address, or a domain socket pathname.
174 * Note: The current server is tracked separately for each thread in a program.
175 * Multi-threaded programs that override the server via the
176 * @link cupsSetServer@ function need to do so in each thread for the same
180 const char * /* O - Server name */
183 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
194 * 'cupsSetClientCertCB()' - Set the client certificate callback.
196 * Pass @code NULL@ to restore the default callback.
198 * Note: The current certificate callback is tracked separately for each thread
199 * in a program. Multi-threaded programs that override the callback need to do
200 * so in each thread for the same callback to be used.
202 * @since CUPS 1.5/OS X 10.7@
207 cups_client_cert_cb_t cb
, /* I - Callback function */
208 void *user_data
) /* I - User data pointer */
210 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
213 cg
->client_cert_cb
= cb
;
214 cg
->client_cert_data
= user_data
;
219 * 'cupsSetCredentials()' - Set the default credentials to be used for SSL/TLS
222 * Note: The default credentials are tracked separately for each thread in a
223 * program. Multi-threaded programs that override the setting need to do so in
224 * each thread for the same setting to be used.
226 * @since CUPS 1.5/OS X 10.7@
229 int /* O - Status of call (0 = success) */
231 cups_array_t
*credentials
) /* I - Array of credentials */
233 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
236 if (cupsArrayCount(credentials
) < 1)
240 _httpFreeCredentials(cg
->tls_credentials
);
241 cg
->tls_credentials
= _httpCreateCredentials(credentials
);
242 #endif /* HAVE_SSL */
244 return (cg
->tls_credentials
? 0 : -1);
249 * 'cupsSetEncryption()' - Set the encryption preference.
251 * The default encryption setting comes from the CUPS_ENCRYPTION
252 * environment variable, then the ~/.cups/client.conf file, and finally the
253 * /etc/cups/client.conf file. If not set, the default is
254 * @code HTTP_ENCRYPTION_IF_REQUESTED@.
256 * Note: The current encryption setting is tracked separately for each thread
257 * in a program. Multi-threaded programs that override the setting need to do
258 * so in each thread for the same setting to be used.
262 cupsSetEncryption(http_encryption_t e
) /* I - New encryption preference */
264 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
270 httpEncryption(cg
->http
, e
);
275 * 'cupsSetPasswordCB()' - Set the password callback for CUPS.
277 * Pass @code NULL@ to restore the default (console) password callback, which
278 * reads the password from the console. Programs should call either this
279 * function or @link cupsSetPasswordCB2@, as only one callback can be registered
280 * by a program per thread.
282 * Note: The current password callback is tracked separately for each thread
283 * in a program. Multi-threaded programs that override the callback need to do
284 * so in each thread for the same callback to be used.
288 cupsSetPasswordCB(cups_password_cb_t cb
)/* I - Callback function */
290 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
293 if (cb
== (cups_password_cb_t
)0)
294 cg
->password_cb
= (cups_password_cb2_t
)_cupsGetPassword
;
296 cg
->password_cb
= (cups_password_cb2_t
)cb
;
298 cg
->password_data
= NULL
;
303 * 'cupsSetPasswordCB2()' - Set the advanced password callback for CUPS.
305 * Pass @code NULL@ to restore the default (console) password callback, which
306 * reads the password from the console. Programs should call either this
307 * function or @link cupsSetPasswordCB2@, as only one callback can be registered
308 * by a program per thread.
310 * Note: The current password callback is tracked separately for each thread
311 * in a program. Multi-threaded programs that override the callback need to do
312 * so in each thread for the same callback to be used.
314 * @since CUPS 1.4/OS X 10.6@
319 cups_password_cb2_t cb
, /* I - Callback function */
320 void *user_data
) /* I - User data pointer */
322 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
325 if (cb
== (cups_password_cb2_t
)0)
326 cg
->password_cb
= (cups_password_cb2_t
)_cupsGetPassword
;
328 cg
->password_cb
= cb
;
330 cg
->password_data
= user_data
;
335 * 'cupsSetServer()' - Set the default server name and port.
337 * The "server" string can be a fully-qualified hostname, a numeric
338 * IPv4 or IPv6 address, or a domain socket pathname. Hostnames and numeric IP
339 * addresses can be optionally followed by a colon and port number to override
340 * the default port 631, e.g. "hostname:8631". Pass @code NULL@ to restore the
341 * default server name and port.
343 * Note: The current server is tracked separately for each thread in a program.
344 * Multi-threaded programs that override the server need to do so in each
345 * thread for the same server to be used.
349 cupsSetServer(const char *server
) /* I - Server name */
351 char *options
, /* Options */
352 *port
; /* Pointer to port */
353 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
358 strlcpy(cg
->server
, server
, sizeof(cg
->server
));
360 if (cg
->server
[0] != '/' && (options
= strrchr(cg
->server
, '/')) != NULL
)
364 if (!strcmp(options
, "version=1.0"))
365 cg
->server_version
= 10;
366 else if (!strcmp(options
, "version=1.1"))
367 cg
->server_version
= 11;
368 else if (!strcmp(options
, "version=2.0"))
369 cg
->server_version
= 20;
370 else if (!strcmp(options
, "version=2.1"))
371 cg
->server_version
= 21;
372 else if (!strcmp(options
, "version=2.2"))
373 cg
->server_version
= 22;
376 cg
->server_version
= 20;
378 if (cg
->server
[0] != '/' && (port
= strrchr(cg
->server
, ':')) != NULL
&&
379 !strchr(port
, ']') && isdigit(port
[1] & 255))
383 cg
->ipp_port
= atoi(port
);
387 cups_set_default_ipp_port(cg
);
389 if (cg
->server
[0] == '/')
390 strlcpy(cg
->servername
, "localhost", sizeof(cg
->servername
));
392 strlcpy(cg
->servername
, cg
->server
, sizeof(cg
->servername
));
396 cg
->server
[0] = '\0';
397 cg
->servername
[0] = '\0';
398 cg
->server_version
= 20;
411 * 'cupsSetServerCertCB()' - Set the server certificate callback.
413 * Pass @code NULL@ to restore the default callback.
415 * Note: The current credentials callback is tracked separately for each thread
416 * in a program. Multi-threaded programs that override the callback need to do
417 * so in each thread for the same callback to be used.
419 * @since CUPS 1.5/OS X 10.7@
424 cups_server_cert_cb_t cb
, /* I - Callback function */
425 void *user_data
) /* I - User data pointer */
427 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
430 cg
->server_cert_cb
= cb
;
431 cg
->server_cert_data
= user_data
;
436 * 'cupsSetUser()' - Set the default user name.
438 * Pass @code NULL@ to restore the default user name.
440 * Note: The current user name is tracked separately for each thread in a
441 * program. Multi-threaded programs that override the user name need to do so
442 * in each thread for the same user name to be used.
446 cupsSetUser(const char *user
) /* I - User name */
448 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
452 strlcpy(cg
->user
, user
, sizeof(cg
->user
));
459 * 'cupsSetUserAgent()' - Set the default HTTP User-Agent string.
461 * Setting the string to NULL forces the default value containing the CUPS
462 * version, IPP version, and operating system version and architecture.
464 * @since CUPS 1.7/OS X 10.9@
468 cupsSetUserAgent(const char *user_agent
)/* I - User-Agent string or @code NULL@ */
470 _cups_globals_t
*cg
= _cupsGlobals();
473 SYSTEM_INFO sysinfo
; /* System information */
474 OSVERSIONINFO version
; /* OS version info */
476 struct utsname name
; /* uname info */
482 strlcpy(cg
->user_agent
, user_agent
, sizeof(cg
->user_agent
));
487 version
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
488 GetVersionEx(&version
);
489 GetNativeSystemInfo(&sysinfo
);
491 snprintf(cg
->user_agent
, sizeof(cg
->user_agent
),
492 CUPS_MINIMAL
" (Windows %d.%d; %s) IPP/2.0",
493 version
.dwMajorVersion
, version
.dwMinorVersion
,
494 sysinfo
.wProcessorArchitecture
495 == PROCESSOR_ARCHITECTURE_AMD64
? "amd64" :
496 sysinfo
.wProcessorArchitecture
497 == PROCESSOR_ARCHITECTURE_ARM
? "arm" :
498 sysinfo
.wProcessorArchitecture
499 == PROCESSOR_ARCHITECTURE_IA64
? "ia64" :
500 sysinfo
.wProcessorArchitecture
501 == PROCESSOR_ARCHITECTURE_INTEL
? "intel" :
507 snprintf(cg
->user_agent
, sizeof(cg
->user_agent
),
508 CUPS_MINIMAL
" (%s %s; %s) IPP/2.0",
509 name
.sysname
, name
.release
, name
.machine
);
515 * 'cupsUser()' - Return the current user's name.
517 * Note: The current user name is tracked separately for each thread in a
518 * program. Multi-threaded programs that override the user name with the
519 * @link cupsSetUser@ function need to do so in each thread for the same user
523 const char * /* O - User name */
526 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
537 * 'cupsUserAgent()' - Return the default HTTP User-Agent string.
539 * @since CUPS 1.7/OS X 10.9@
542 const char * /* O - User-Agent string */
545 _cups_globals_t
*cg
= _cupsGlobals(); /* Thread globals */
548 if (!cg
->user_agent
[0])
549 cupsSetUserAgent(NULL
);
551 return (cg
->user_agent
);
556 * '_cupsGetPassword()' - Get a password from the user.
559 const char * /* O - Password or @code NULL@ if none */
560 _cupsGetPassword(const char *prompt
) /* I - Prompt string */
563 HANDLE tty
; /* Console handle */
564 DWORD mode
; /* Console mode */
565 char passch
, /* Current key press */
566 *passptr
, /* Pointer into password string */
567 *passend
; /* End of password string */
568 DWORD passbytes
; /* Bytes read */
569 _cups_globals_t
*cg
= _cupsGlobals();
574 * Disable input echo and set raw input...
577 if ((tty
= GetStdHandle(STD_INPUT_HANDLE
)) == INVALID_HANDLE_VALUE
)
580 if (!GetConsoleMode(tty
, &mode
))
583 if (!SetConsoleMode(tty
, 0))
587 * Display the prompt...
590 printf("%s ", prompt
);
594 * Read the password string from /dev/tty until we get interrupted or get a
595 * carriage return or newline...
598 passptr
= cg
->password
;
599 passend
= cg
->password
+ sizeof(cg
->password
) - 1;
601 while (ReadFile(tty
, &passch
, 1, &passbytes
, NULL
))
603 if (passch
== 0x0A || passch
== 0x0D)
611 else if (passch
== 0x08 || passch
== 0x7F)
614 * Backspace/delete (erase character)...
617 if (passptr
> cg
->password
)
620 fputs("\010 \010", stdout
);
625 else if (passch
== 0x15)
628 * CTRL+U (erase line)
631 if (passptr
> cg
->password
)
633 while (passptr
> cg
->password
)
636 fputs("\010 \010", stdout
);
642 else if (passch
== 0x03)
648 passptr
= cg
->password
;
651 else if ((passch
& 255) < 0x20 || passptr
>= passend
)
656 putchar(_CUPS_PASSCHAR
);
669 SetConsoleMode(tty
, mode
);
672 * Return the proper value...
675 if (passbytes
== 1 && passptr
> cg
->password
)
678 return (cg
->password
);
682 memset(cg
->password
, 0, sizeof(cg
->password
));
687 int tty
; /* /dev/tty - never read from stdin */
688 struct termios original
, /* Original input mode */
689 noecho
; /* No echo input mode */
690 char passch
, /* Current key press */
691 *passptr
, /* Pointer into password string */
692 *passend
; /* End of password string */
693 ssize_t passbytes
; /* Bytes read */
694 _cups_globals_t
*cg
= _cupsGlobals();
699 * Disable input echo and set raw input...
702 if ((tty
= open("/dev/tty", O_RDONLY
)) < 0)
705 if (tcgetattr(tty
, &original
))
712 noecho
.c_lflag
&= (tcflag_t
)~(ICANON
| ECHO
| ECHOE
| ISIG
);
713 noecho
.c_cc
[VMIN
] = 1;
714 noecho
.c_cc
[VTIME
] = 0;
716 if (tcsetattr(tty
, TCSAFLUSH
, &noecho
))
723 * Display the prompt...
726 printf("%s ", prompt
);
730 * Read the password string from /dev/tty until we get interrupted or get a
731 * carriage return or newline...
734 passptr
= cg
->password
;
735 passend
= cg
->password
+ sizeof(cg
->password
) - 1;
737 while ((passbytes
= read(tty
, &passch
, 1)) == 1)
739 if (passch
== noecho
.c_cc
[VEOL
] ||
741 passch
== noecho
.c_cc
[VEOL2
] ||
743 passch
== 0x0A || passch
== 0x0D)
751 else if (passch
== noecho
.c_cc
[VERASE
] ||
752 passch
== 0x08 || passch
== 0x7F)
755 * Backspace/delete (erase character)...
758 if (passptr
> cg
->password
)
761 fputs("\010 \010", stdout
);
766 else if (passch
== noecho
.c_cc
[VKILL
])
769 * CTRL+U (erase line)
772 if (passptr
> cg
->password
)
774 while (passptr
> cg
->password
)
777 fputs("\010 \010", stdout
);
783 else if (passch
== noecho
.c_cc
[VINTR
] || passch
== noecho
.c_cc
[VQUIT
] ||
784 passch
== noecho
.c_cc
[VEOF
])
787 * CTRL+C, CTRL+D, or CTRL+Z...
790 passptr
= cg
->password
;
793 else if ((passch
& 255) < 0x20 || passptr
>= passend
)
798 putchar(_CUPS_PASSCHAR
);
811 tcsetattr(tty
, TCSAFLUSH
, &original
);
815 * Return the proper value...
818 if (passbytes
== 1 && passptr
> cg
->password
)
821 return (cg
->password
);
825 memset(cg
->password
, 0, sizeof(cg
->password
));
834 * '_cupsGSSServiceName()' - Get the GSS (Kerberos) service name.
838 _cupsGSSServiceName(void)
840 _cups_globals_t
*cg
= _cupsGlobals(); /* Thread globals */
843 if (!cg
->gss_service_name
[0])
846 return (cg
->gss_service_name
);
848 #endif /* HAVE_GSSAPI */
852 * '_cupsSetDefaults()' - Set the default server, port, and encryption.
856 _cupsSetDefaults(void)
858 cups_file_t
*fp
; /* File */
859 const char *home
; /* Home directory of user */
860 char filename
[1024]; /* Filename */
861 _cups_client_conf_t cc
; /* client.conf values */
862 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
865 DEBUG_puts("_cupsSetDefaults()");
868 * Load initial client.conf values...
871 cups_init_client_conf(&cc
);
874 * Read the /etc/cups/client.conf and ~/.cups/client.conf files, if
878 snprintf(filename
, sizeof(filename
), "%s/client.conf", cg
->cups_serverroot
);
879 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
881 cups_read_client_conf(fp
, &cc
);
886 if ((geteuid() == getuid() || !getuid()) && getegid() == getgid() && (home
= getenv("HOME")) != NULL
)
887 # elif !defined(WIN32)
888 if (getuid() && (home
= getenv("HOME")) != NULL
)
890 if ((home
= getenv("HOME")) != NULL
)
891 # endif /* HAVE_GETEUID */
894 * Look for ~/.cups/client.conf...
897 snprintf(filename
, sizeof(filename
), "%s/.cups/client.conf", home
);
898 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
900 cups_read_client_conf(fp
, &cc
);
906 * Finalize things so every client.conf value is set...
909 cups_finalize_client_conf(&cc
);
911 if (cg
->encryption
== (http_encryption_t
)-1)
912 cg
->encryption
= cc
.encryption
;
914 if (!cg
->server
[0] || !cg
->ipp_port
)
915 cupsSetServer(cc
.server_name
);
918 cups_set_default_ipp_port(cg
);
921 strlcpy(cg
->user
, cc
.user
, sizeof(cg
->user
));
924 if (!cg
->gss_service_name
[0])
925 strlcpy(cg
->gss_service_name
, cc
.gss_service_name
, sizeof(cg
->gss_service_name
));
926 #endif /* HAVE_GSSAPI */
928 if (cg
->any_root
< 0)
929 cg
->any_root
= cc
.any_root
;
931 if (cg
->expired_certs
< 0)
932 cg
->expired_certs
= cc
.expired_certs
;
934 if (cg
->validate_certs
< 0)
935 cg
->validate_certs
= cc
.validate_certs
;
938 _httpTLSSetOptions(cc
.ssl_options
);
939 #endif /* HAVE_SSL */
944 * 'cups_boolean_value()' - Convert a string to a boolean value.
947 static int /* O - Boolean value */
948 cups_boolean_value(const char *value
) /* I - String value */
950 return (!_cups_strcasecmp(value
, "yes") || !_cups_strcasecmp(value
, "on") || !_cups_strcasecmp(value
, "true"));
955 * 'cups_finalize_client_conf()' - Finalize client.conf values.
959 cups_finalize_client_conf(
960 _cups_client_conf_t
*cc
) /* I - client.conf values */
962 const char *value
; /* Environment variable */
965 if ((value
= getenv("CUPS_ANYROOT")) != NULL
)
966 cc
->any_root
= cups_boolean_value(value
);
968 if ((value
= getenv("CUPS_ENCRYPTION")) != NULL
)
969 cups_set_encryption(cc
, value
);
971 if ((value
= getenv("CUPS_EXPIREDCERTS")) != NULL
)
972 cc
->expired_certs
= cups_boolean_value(value
);
975 if ((value
= getenv("CUPS_GSSSERVICENAME")) != NULL
)
976 cups_set_gss_service_name(cc
, value
);
977 #endif /* HAVE_GSSAPI */
979 if ((value
= getenv("CUPS_SERVER")) != NULL
)
980 cups_set_server_name(cc
, value
);
982 if ((value
= getenv("CUPS_USER")) != NULL
)
983 cups_set_user(cc
, value
);
985 if ((value
= getenv("CUPS_VALIDATECERTS")) != NULL
)
986 cc
->validate_certs
= cups_boolean_value(value
);
989 * Then apply defaults for those values that haven't been set...
992 if (cc
->any_root
< 0)
995 if (cc
->encryption
== (http_encryption_t
)-1)
996 cc
->encryption
= HTTP_ENCRYPTION_IF_REQUESTED
;
998 if (cc
->expired_certs
< 0)
999 cc
->expired_certs
= 1;
1002 if (!cc
->gss_service_name
[0])
1003 cups_set_gss_service_name(cc
, CUPS_DEFAULT_GSSSERVICENAME
);
1004 #endif /* HAVE_GSSAPI */
1006 if (!cc
->server_name
[0])
1008 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1010 * If we are compiled with domain socket support, only use the
1011 * domain socket if it exists and has the right permissions...
1014 if (!access(CUPS_DEFAULT_DOMAINSOCKET
, R_OK
))
1015 cups_set_server_name(cc
, CUPS_DEFAULT_DOMAINSOCKET
);
1017 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1018 cups_set_server_name(cc
, "localhost");
1025 * Get the current user name from the OS...
1028 DWORD size
; /* Size of string */
1030 size
= sizeof(cc
->user
);
1031 if (!GetUserName(cc
->user
, &size
))
1034 * Try the USER environment variable as the default username...
1037 const char *envuser
= getenv("USER");
1038 /* Default username */
1039 struct passwd
*pw
= NULL
; /* Account information */
1044 * Validate USER matches the current UID, otherwise don't allow it to
1045 * override things... This makes sure that printing after doing su
1046 * or sudo records the correct username.
1049 if ((pw
= getpwnam(envuser
)) != NULL
&& pw
->pw_uid
!= getuid())
1054 pw
= getpwuid(getuid());
1057 strlcpy(cc
->user
, pw
->pw_name
, sizeof(cc
->user
));
1062 * Use the default "unknown" user name...
1065 strlcpy(cc
->user
, "unknown", sizeof(cc
->user
));
1069 if (cc
->validate_certs
< 0)
1070 cc
->validate_certs
= 0;
1075 * 'cups_init_client_conf()' - Initialize client.conf values.
1079 cups_init_client_conf(
1080 _cups_client_conf_t
*cc
) /* I - client.conf values */
1083 * Clear all values to "not set"...
1086 memset(cc
, 0, sizeof(_cups_client_conf_t
));
1088 cc
->encryption
= (http_encryption_t
)-1;
1090 cc
->expired_certs
= -1;
1091 cc
->validate_certs
= -1;
1096 * 'cups_read_client_conf()' - Read a client.conf file.
1100 cups_read_client_conf(
1101 cups_file_t
*fp
, /* I - File to read */
1102 _cups_client_conf_t
*cc
) /* I - client.conf values */
1104 int linenum
; /* Current line number */
1105 char line
[1024], /* Line from file */
1106 *value
; /* Pointer into line */
1110 * Read from the file...
1114 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
1116 if (!_cups_strcasecmp(line
, "Encryption") && value
)
1117 cups_set_encryption(cc
, value
);
1120 * The ServerName directive is not supported on OS X due to app
1121 * sandboxing restrictions, i.e. not all apps request network access.
1123 else if (!_cups_strcasecmp(line
, "ServerName") && value
)
1124 cups_set_server_name(cc
, value
);
1125 #endif /* !__APPLE__ */
1126 else if (!_cups_strcasecmp(line
, "User") && value
)
1127 cups_set_user(cc
, value
);
1128 else if (!_cups_strcasecmp(line
, "AllowAnyRoot") && value
)
1129 cc
->any_root
= cups_boolean_value(value
);
1130 else if (!_cups_strcasecmp(line
, "AllowExpiredCerts") &&
1132 cc
->expired_certs
= cups_boolean_value(value
);
1133 else if (!_cups_strcasecmp(line
, "ValidateCerts") && value
)
1134 cc
->validate_certs
= cups_boolean_value(value
);
1136 else if (!_cups_strcasecmp(line
, "GSSServiceName") && value
)
1137 cups_set_gss_service_name(cc
, value
);
1138 #endif /* HAVE_GSSAPI */
1140 else if (!_cups_strcasecmp(line
, "SSLOptions") && value
)
1141 cups_set_ssl_options(cc
, value
);
1142 #endif /* HAVE_SSL */
1148 * 'cups_set_default_ipp_port()' - Set the default IPP port value.
1152 cups_set_default_ipp_port(
1153 _cups_globals_t
*cg
) /* I - Global data */
1155 const char *ipp_port
; /* IPP_PORT environment variable */
1158 if ((ipp_port
= getenv("IPP_PORT")) != NULL
)
1160 if ((cg
->ipp_port
= atoi(ipp_port
)) <= 0)
1161 cg
->ipp_port
= CUPS_DEFAULT_IPP_PORT
;
1164 cg
->ipp_port
= CUPS_DEFAULT_IPP_PORT
;
1168 * 'cups_set_encryption()' - Set the Encryption value.
1172 cups_set_encryption(
1173 _cups_client_conf_t
*cc
, /* I - client.conf values */
1174 const char *value
) /* I - Value */
1176 if (!_cups_strcasecmp(value
, "never"))
1177 cc
->encryption
= HTTP_ENCRYPTION_NEVER
;
1178 else if (!_cups_strcasecmp(value
, "always"))
1179 cc
->encryption
= HTTP_ENCRYPTION_ALWAYS
;
1180 else if (!_cups_strcasecmp(value
, "required"))
1181 cc
->encryption
= HTTP_ENCRYPTION_REQUIRED
;
1183 cc
->encryption
= HTTP_ENCRYPTION_IF_REQUESTED
;
1188 * 'cups_set_gss_service_name()' - Set the GSSServiceName value.
1193 cups_set_gss_service_name(
1194 _cups_client_conf_t
*cc
, /* I - client.conf values */
1195 const char *value
) /* I - Value */
1197 strlcpy(cc
->gss_service_name
, value
, sizeof(cc
->gss_service_name
));
1199 #endif /* HAVE_GSSAPI */
1203 * 'cups_set_server_name()' - Set the ServerName value.
1207 cups_set_server_name(
1208 _cups_client_conf_t
*cc
, /* I - client.conf values */
1209 const char *value
) /* I - Value */
1211 strlcpy(cc
->server_name
, value
, sizeof(cc
->server_name
));
1216 * 'cups_set_ssl_options()' - Set the SSLOptions value.
1221 cups_set_ssl_options(
1222 _cups_client_conf_t
*cc
, /* I - client.conf values */
1223 const char *value
) /* I - Value */
1226 * SSLOptions [AllowRC4] [AllowSSL3] [AllowDH] [DenyTLS1.0] [None]
1229 int options
= _HTTP_TLS_NONE
; /* SSL/TLS options */
1230 char temp
[256], /* Copy of value */
1231 *start
, /* Start of option */
1232 *end
; /* End of option */
1235 strlcpy(temp
, value
, sizeof(temp
));
1237 for (start
= temp
; *start
; start
= end
)
1240 * Find end of keyword...
1244 while (*end
&& !_cups_isspace(*end
))
1254 if (!_cups_strcasecmp(start
, "AllowRC4"))
1255 options
|= _HTTP_TLS_ALLOW_RC4
;
1256 else if (!_cups_strcasecmp(start
, "AllowSSL3"))
1257 options
|= _HTTP_TLS_ALLOW_SSL3
;
1258 else if (!_cups_strcasecmp(start
, "AllowDH"))
1259 options
|= _HTTP_TLS_ALLOW_DH
;
1260 else if (!_cups_strcasecmp(start
, "DenyTLS1.0"))
1261 options
|= _HTTP_TLS_DENY_TLS10
;
1262 else if (!_cups_strcasecmp(start
, "None"))
1263 options
= _HTTP_TLS_NONE
;
1266 cc
->ssl_options
= options
;
1268 DEBUG_printf(("4cups_set_ssl_options(cc=%p, value=\"%s\") options=%x", (void *)cc
, value
, options
));
1270 #endif /* HAVE_SSL */
1274 * 'cups_set_user()' - Set the User value.
1279 _cups_client_conf_t
*cc
, /* I - client.conf values */
1280 const char *value
) /* I - Value */
1282 strlcpy(cc
->user
, value
, sizeof(cc
->user
));