2 * User, system, and password routines for CUPS.
4 * Copyright 2007-2019 by Apple Inc.
5 * Copyright 1997-2006 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more
12 * Include necessary headers...
15 #include "cups-private.h"
16 #include "debug-internal.h"
24 # include <sys/utsname.h>
27 # include <sys/sysctl.h>
28 #endif /* __APPLE__ */
37 # define kCUPSPrintingPrefs CFSTR("org.cups.PrintingPrefs")
40 # define kCUPSPrintingPrefs CFSTR(".GlobalPreferences")
41 # define kPREFIX "AirPrint"
42 # endif /* TARGET_OS_OSX */
43 # define kAllowAnyRootKey CFSTR(kPREFIX "AllowAnyRoot")
44 # define kAllowExpiredCertsKey CFSTR(kPREFIX "AllowExpiredCerts")
45 # define kEncryptionKey CFSTR(kPREFIX "Encryption")
46 # define kGSSServiceNameKey CFSTR(kPREFIX "GSSServiceName")
47 # define kSSLOptionsKey CFSTR(kPREFIX "SSLOptions")
48 # define kTrustOnFirstUseKey CFSTR(kPREFIX "TrustOnFirstUse")
49 # define kValidateCertsKey CFSTR(kPREFIX "ValidateCerts")
51 # define kAllowRC4 CFSTR(kPREFIX "AllowRC4")
52 # define kAllowSSL3 CFSTR(kPREFIX "AllowSSL3")
53 # define kAllowDH CFSTR(kPREFIX "AllowDH")
54 #endif /* __APPLE__ */
56 #define _CUPS_PASSCHAR '*' /* Character that is echoed for password */
63 typedef struct _cups_client_conf_s
/**** client.conf config data ****/
66 int ssl_options
, /* SSLOptions values */
67 ssl_min_version
,/* Minimum SSL/TLS version */
68 ssl_max_version
;/* Maximum SSL/TLS version */
70 int trust_first
, /* Trust on first use? */
71 any_root
, /* Allow any (e.g., self-signed) root */
72 expired_certs
, /* Allow expired certs */
73 validate_certs
; /* Validate certificates */
74 http_encryption_t encryption
; /* Encryption setting */
75 char user
[65], /* User name */
79 char gss_service_name
[32];
80 /* Kerberos service name */
81 #endif /* HAVE_GSSAPI */
82 } _cups_client_conf_t
;
90 static int cups_apple_get_boolean(CFStringRef key
, int *value
);
91 static int cups_apple_get_string(CFStringRef key
, char *value
, size_t valsize
);
92 #endif /* __APPLE__ */
93 static int cups_boolean_value(const char *value
);
94 static void cups_finalize_client_conf(_cups_client_conf_t
*cc
);
95 static void cups_init_client_conf(_cups_client_conf_t
*cc
);
96 static void cups_read_client_conf(cups_file_t
*fp
, _cups_client_conf_t
*cc
);
97 static void cups_set_default_ipp_port(_cups_globals_t
*cg
);
98 static void cups_set_encryption(_cups_client_conf_t
*cc
, const char *value
);
100 static void cups_set_gss_service_name(_cups_client_conf_t
*cc
, const char *value
);
101 #endif /* HAVE_GSSAPI */
102 static void cups_set_server_name(_cups_client_conf_t
*cc
, const char *value
);
104 static void cups_set_ssl_options(_cups_client_conf_t
*cc
, const char *value
);
105 #endif /* HAVE_SSL */
106 static void cups_set_user(_cups_client_conf_t
*cc
, const char *value
);
110 * 'cupsEncryption()' - Get the current encryption settings.
112 * The default encryption setting comes from the CUPS_ENCRYPTION
113 * environment variable, then the ~/.cups/client.conf file, and finally the
114 * /etc/cups/client.conf file. If not set, the default is
115 * @code HTTP_ENCRYPTION_IF_REQUESTED@.
117 * Note: The current encryption setting is tracked separately for each thread
118 * in a program. Multi-threaded programs that override the setting via the
119 * @link cupsSetEncryption@ function need to do so in each thread for the same
120 * setting to be used.
123 http_encryption_t
/* O - Encryption settings */
126 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
129 if (cg
->encryption
== (http_encryption_t
)-1)
132 return (cg
->encryption
);
137 * 'cupsGetPassword()' - Get a password from the user.
139 * Uses the current password callback function. Returns @code NULL@ if the
140 * user does not provide a password.
142 * Note: The current password callback function is tracked separately for each
143 * thread in a program. Multi-threaded programs that override the setting via
144 * the @link cupsSetPasswordCB@ or @link cupsSetPasswordCB2@ functions need to
145 * do so in each thread for the same function to be used.
150 const char * /* O - Password */
151 cupsGetPassword(const char *prompt
) /* I - Prompt string */
153 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
156 return ((cg
->password_cb
)(prompt
, NULL
, NULL
, NULL
, cg
->password_data
));
161 * 'cupsGetPassword2()' - Get a password from the user using the current
164 * Uses the current password callback function. Returns @code NULL@ if the
165 * user does not provide a password.
167 * Note: The current password callback function is tracked separately for each
168 * thread in a program. Multi-threaded programs that override the setting via
169 * the @link cupsSetPasswordCB2@ function need to do so in each thread for the
170 * same function to be used.
172 * @since CUPS 1.4/macOS 10.6@
175 const char * /* O - Password */
176 cupsGetPassword2(const char *prompt
, /* I - Prompt string */
177 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
178 const char *method
, /* I - Request method ("GET", "POST", "PUT") */
179 const char *resource
) /* I - Resource path */
181 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
185 http
= _cupsConnect();
187 return ((cg
->password_cb
)(prompt
, http
, method
, resource
, cg
->password_data
));
192 * 'cupsServer()' - Return the hostname/address of the current server.
194 * The default server comes from the CUPS_SERVER environment variable, then the
195 * ~/.cups/client.conf file, and finally the /etc/cups/client.conf file. If not
196 * set, the default is the local system - either "localhost" or a domain socket
199 * The returned value can be a fully-qualified hostname, a numeric IPv4 or IPv6
200 * address, or a domain socket pathname.
202 * Note: The current server is tracked separately for each thread in a program.
203 * Multi-threaded programs that override the server via the
204 * @link cupsSetServer@ function need to do so in each thread for the same
208 const char * /* O - Server name */
211 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
222 * 'cupsSetClientCertCB()' - Set the client certificate callback.
224 * Pass @code NULL@ to restore the default callback.
226 * Note: The current certificate callback is tracked separately for each thread
227 * in a program. Multi-threaded programs that override the callback need to do
228 * so in each thread for the same callback to be used.
230 * @since CUPS 1.5/macOS 10.7@
235 cups_client_cert_cb_t cb
, /* I - Callback function */
236 void *user_data
) /* I - User data pointer */
238 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
241 cg
->client_cert_cb
= cb
;
242 cg
->client_cert_data
= user_data
;
247 * 'cupsSetCredentials()' - Set the default credentials to be used for SSL/TLS
250 * Note: The default credentials are tracked separately for each thread in a
251 * program. Multi-threaded programs that override the setting need to do so in
252 * each thread for the same setting to be used.
254 * @since CUPS 1.5/macOS 10.7@
257 int /* O - Status of call (0 = success) */
259 cups_array_t
*credentials
) /* I - Array of credentials */
261 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
264 if (cupsArrayCount(credentials
) < 1)
268 _httpFreeCredentials(cg
->tls_credentials
);
269 cg
->tls_credentials
= _httpCreateCredentials(credentials
);
270 #endif /* HAVE_SSL */
272 return (cg
->tls_credentials
? 0 : -1);
277 * 'cupsSetEncryption()' - Set the encryption preference.
279 * The default encryption setting comes from the CUPS_ENCRYPTION
280 * environment variable, then the ~/.cups/client.conf file, and finally the
281 * /etc/cups/client.conf file. If not set, the default is
282 * @code HTTP_ENCRYPTION_IF_REQUESTED@.
284 * Note: The current encryption setting is tracked separately for each thread
285 * in a program. Multi-threaded programs that override the setting need to do
286 * so in each thread for the same setting to be used.
290 cupsSetEncryption(http_encryption_t e
) /* I - New encryption preference */
292 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
298 httpEncryption(cg
->http
, e
);
303 * 'cupsSetPasswordCB()' - Set the 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.
318 cupsSetPasswordCB(cups_password_cb_t cb
)/* I - Callback function */
320 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
323 if (cb
== (cups_password_cb_t
)0)
324 cg
->password_cb
= (cups_password_cb2_t
)_cupsGetPassword
;
326 cg
->password_cb
= (cups_password_cb2_t
)cb
;
328 cg
->password_data
= NULL
;
333 * 'cupsSetPasswordCB2()' - Set the advanced password callback for CUPS.
335 * Pass @code NULL@ to restore the default (console) password callback, which
336 * reads the password from the console. Programs should call either this
337 * function or @link cupsSetPasswordCB2@, as only one callback can be registered
338 * by a program per thread.
340 * Note: The current password callback is tracked separately for each thread
341 * in a program. Multi-threaded programs that override the callback need to do
342 * so in each thread for the same callback to be used.
344 * @since CUPS 1.4/macOS 10.6@
349 cups_password_cb2_t cb
, /* I - Callback function */
350 void *user_data
) /* I - User data pointer */
352 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
355 if (cb
== (cups_password_cb2_t
)0)
356 cg
->password_cb
= (cups_password_cb2_t
)_cupsGetPassword
;
358 cg
->password_cb
= cb
;
360 cg
->password_data
= user_data
;
365 * 'cupsSetServer()' - Set the default server name and port.
367 * The "server" string can be a fully-qualified hostname, a numeric
368 * IPv4 or IPv6 address, or a domain socket pathname. Hostnames and numeric IP
369 * addresses can be optionally followed by a colon and port number to override
370 * the default port 631, e.g. "hostname:8631". Pass @code NULL@ to restore the
371 * default server name and port.
373 * Note: The current server is tracked separately for each thread in a program.
374 * Multi-threaded programs that override the server need to do so in each
375 * thread for the same server to be used.
379 cupsSetServer(const char *server
) /* I - Server name */
381 char *options
, /* Options */
382 *port
; /* Pointer to port */
383 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
388 strlcpy(cg
->server
, server
, sizeof(cg
->server
));
390 if (cg
->server
[0] != '/' && (options
= strrchr(cg
->server
, '/')) != NULL
)
394 if (!strcmp(options
, "version=1.0"))
395 cg
->server_version
= 10;
396 else if (!strcmp(options
, "version=1.1"))
397 cg
->server_version
= 11;
398 else if (!strcmp(options
, "version=2.0"))
399 cg
->server_version
= 20;
400 else if (!strcmp(options
, "version=2.1"))
401 cg
->server_version
= 21;
402 else if (!strcmp(options
, "version=2.2"))
403 cg
->server_version
= 22;
406 cg
->server_version
= 20;
408 if (cg
->server
[0] != '/' && (port
= strrchr(cg
->server
, ':')) != NULL
&&
409 !strchr(port
, ']') && isdigit(port
[1] & 255))
413 cg
->ipp_port
= atoi(port
);
417 cups_set_default_ipp_port(cg
);
419 if (cg
->server
[0] == '/')
420 strlcpy(cg
->servername
, "localhost", sizeof(cg
->servername
));
422 strlcpy(cg
->servername
, cg
->server
, sizeof(cg
->servername
));
426 cg
->server
[0] = '\0';
427 cg
->servername
[0] = '\0';
428 cg
->server_version
= 20;
441 * 'cupsSetServerCertCB()' - Set the server certificate callback.
443 * Pass @code NULL@ to restore the default callback.
445 * Note: The current credentials callback is tracked separately for each thread
446 * in a program. Multi-threaded programs that override the callback need to do
447 * so in each thread for the same callback to be used.
449 * @since CUPS 1.5/macOS 10.7@
454 cups_server_cert_cb_t cb
, /* I - Callback function */
455 void *user_data
) /* I - User data pointer */
457 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
460 cg
->server_cert_cb
= cb
;
461 cg
->server_cert_data
= user_data
;
466 * 'cupsSetUser()' - Set the default user name.
468 * Pass @code NULL@ to restore the default user name.
470 * Note: The current user name is tracked separately for each thread in a
471 * program. Multi-threaded programs that override the user name need to do so
472 * in each thread for the same user name to be used.
476 cupsSetUser(const char *user
) /* I - User name */
478 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
482 strlcpy(cg
->user
, user
, sizeof(cg
->user
));
489 * 'cupsSetUserAgent()' - Set the default HTTP User-Agent string.
491 * Setting the string to NULL forces the default value containing the CUPS
492 * version, IPP version, and operating system version and architecture.
494 * @since CUPS 1.7/macOS 10.9@
498 cupsSetUserAgent(const char *user_agent
)/* I - User-Agent string or @code NULL@ */
500 _cups_globals_t
*cg
= _cupsGlobals();
503 SYSTEM_INFO sysinfo
; /* System information */
504 OSVERSIONINFOA version
; /* OS version info */
505 const char *machine
; /* Hardware/machine name */
506 #elif defined(__APPLE__)
507 struct utsname name
; /* uname info */
508 char version
[256]; /* macOS/iOS version */
509 size_t len
; /* Length of value */
511 struct utsname name
; /* uname info */
517 strlcpy(cg
->user_agent
, user_agent
, sizeof(cg
->user_agent
));
523 * Gather Windows version information for the User-Agent string...
526 version
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
527 GetVersionExA(&version
);
528 GetNativeSystemInfo(&sysinfo
);
530 switch (sysinfo
.wProcessorArchitecture
)
532 case PROCESSOR_ARCHITECTURE_AMD64
:
536 case PROCESSOR_ARCHITECTURE_ARM
:
540 case PROCESSOR_ARCHITECTURE_IA64
:
544 case PROCESSOR_ARCHITECTURE_INTEL
:
553 snprintf(cg
->user_agent
, sizeof(cg
->user_agent
), CUPS_MINIMAL
" (Windows %d.%d; %s) IPP/2.0", version
.dwMajorVersion
, version
.dwMinorVersion
, machine
);
555 #elif defined(__APPLE__)
557 * Gather macOS/iOS version information for the User-Agent string...
562 len
= sizeof(version
) - 1;
563 if (!sysctlbyname("kern.osproductversion", version
, &len
, NULL
, 0))
566 strlcpy(version
, "unknown", sizeof(version
));
569 snprintf(cg
->user_agent
, sizeof(cg
->user_agent
), CUPS_MINIMAL
" (macOS %s; %s) IPP/2.0", version
, name
.machine
);
571 snprintf(cg
->user_agent
, sizeof(cg
->user_agent
), CUPS_MINIMAL
" (iOS %s; %s) IPP/2.0", version
, name
.machine
);
572 # endif /* TARGET_OS_OSX */
576 * Gather generic UNIX version information for the User-Agent string...
581 snprintf(cg
->user_agent
, sizeof(cg
->user_agent
), CUPS_MINIMAL
" (%s %s; %s) IPP/2.0", name
.sysname
, name
.release
, name
.machine
);
587 * 'cupsUser()' - Return the current user's name.
589 * Note: The current user name is tracked separately for each thread in a
590 * program. Multi-threaded programs that override the user name with the
591 * @link cupsSetUser@ function need to do so in each thread for the same user
595 const char * /* O - User name */
598 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
609 * 'cupsUserAgent()' - Return the default HTTP User-Agent string.
611 * @since CUPS 1.7/macOS 10.9@
614 const char * /* O - User-Agent string */
617 _cups_globals_t
*cg
= _cupsGlobals(); /* Thread globals */
620 if (!cg
->user_agent
[0])
621 cupsSetUserAgent(NULL
);
623 return (cg
->user_agent
);
628 * '_cupsGetPassword()' - Get a password from the user.
631 const char * /* O - Password or @code NULL@ if none */
632 _cupsGetPassword(const char *prompt
) /* I - Prompt string */
635 HANDLE tty
; /* Console handle */
636 DWORD mode
; /* Console mode */
637 char passch
, /* Current key press */
638 *passptr
, /* Pointer into password string */
639 *passend
; /* End of password string */
640 DWORD passbytes
; /* Bytes read */
641 _cups_globals_t
*cg
= _cupsGlobals();
646 * Disable input echo and set raw input...
649 if ((tty
= GetStdHandle(STD_INPUT_HANDLE
)) == INVALID_HANDLE_VALUE
)
652 if (!GetConsoleMode(tty
, &mode
))
655 if (!SetConsoleMode(tty
, 0))
659 * Display the prompt...
662 printf("%s ", prompt
);
666 * Read the password string from /dev/tty until we get interrupted or get a
667 * carriage return or newline...
670 passptr
= cg
->password
;
671 passend
= cg
->password
+ sizeof(cg
->password
) - 1;
673 while (ReadFile(tty
, &passch
, 1, &passbytes
, NULL
))
675 if (passch
== 0x0A || passch
== 0x0D)
683 else if (passch
== 0x08 || passch
== 0x7F)
686 * Backspace/delete (erase character)...
689 if (passptr
> cg
->password
)
692 fputs("\010 \010", stdout
);
697 else if (passch
== 0x15)
700 * CTRL+U (erase line)
703 if (passptr
> cg
->password
)
705 while (passptr
> cg
->password
)
708 fputs("\010 \010", stdout
);
714 else if (passch
== 0x03)
720 passptr
= cg
->password
;
723 else if ((passch
& 255) < 0x20 || passptr
>= passend
)
728 putchar(_CUPS_PASSCHAR
);
741 SetConsoleMode(tty
, mode
);
744 * Return the proper value...
747 if (passbytes
== 1 && passptr
> cg
->password
)
750 return (cg
->password
);
754 memset(cg
->password
, 0, sizeof(cg
->password
));
759 int tty
; /* /dev/tty - never read from stdin */
760 struct termios original
, /* Original input mode */
761 noecho
; /* No echo input mode */
762 char passch
, /* Current key press */
763 *passptr
, /* Pointer into password string */
764 *passend
; /* End of password string */
765 ssize_t passbytes
; /* Bytes read */
766 _cups_globals_t
*cg
= _cupsGlobals();
771 * Disable input echo and set raw input...
774 if ((tty
= open("/dev/tty", O_RDONLY
)) < 0)
777 if (tcgetattr(tty
, &original
))
784 noecho
.c_lflag
&= (tcflag_t
)~(ICANON
| ECHO
| ECHOE
| ISIG
);
785 noecho
.c_cc
[VMIN
] = 1;
786 noecho
.c_cc
[VTIME
] = 0;
788 if (tcsetattr(tty
, TCSAFLUSH
, &noecho
))
795 * Display the prompt...
798 printf("%s ", prompt
);
802 * Read the password string from /dev/tty until we get interrupted or get a
803 * carriage return or newline...
806 passptr
= cg
->password
;
807 passend
= cg
->password
+ sizeof(cg
->password
) - 1;
809 while ((passbytes
= read(tty
, &passch
, 1)) == 1)
811 if (passch
== noecho
.c_cc
[VEOL
] ||
813 passch
== noecho
.c_cc
[VEOL2
] ||
815 passch
== 0x0A || passch
== 0x0D)
823 else if (passch
== noecho
.c_cc
[VERASE
] ||
824 passch
== 0x08 || passch
== 0x7F)
827 * Backspace/delete (erase character)...
830 if (passptr
> cg
->password
)
833 fputs("\010 \010", stdout
);
838 else if (passch
== noecho
.c_cc
[VKILL
])
841 * CTRL+U (erase line)
844 if (passptr
> cg
->password
)
846 while (passptr
> cg
->password
)
849 fputs("\010 \010", stdout
);
855 else if (passch
== noecho
.c_cc
[VINTR
] || passch
== noecho
.c_cc
[VQUIT
] ||
856 passch
== noecho
.c_cc
[VEOF
])
859 * CTRL+C, CTRL+D, or CTRL+Z...
862 passptr
= cg
->password
;
865 else if ((passch
& 255) < 0x20 || passptr
>= passend
)
870 putchar(_CUPS_PASSCHAR
);
883 tcsetattr(tty
, TCSAFLUSH
, &original
);
887 * Return the proper value...
890 if (passbytes
== 1 && passptr
> cg
->password
)
893 return (cg
->password
);
897 memset(cg
->password
, 0, sizeof(cg
->password
));
906 * '_cupsGSSServiceName()' - Get the GSS (Kerberos) service name.
910 _cupsGSSServiceName(void)
912 _cups_globals_t
*cg
= _cupsGlobals(); /* Thread globals */
915 if (!cg
->gss_service_name
[0])
918 return (cg
->gss_service_name
);
920 #endif /* HAVE_GSSAPI */
924 * '_cupsSetDefaults()' - Set the default server, port, and encryption.
928 _cupsSetDefaults(void)
930 cups_file_t
*fp
; /* File */
931 const char *home
; /* Home directory of user */
932 char filename
[1024]; /* Filename */
933 _cups_client_conf_t cc
; /* client.conf values */
934 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
937 DEBUG_puts("_cupsSetDefaults()");
940 * Load initial client.conf values...
943 cups_init_client_conf(&cc
);
946 * Read the /etc/cups/client.conf and ~/.cups/client.conf files, if
950 snprintf(filename
, sizeof(filename
), "%s/client.conf", cg
->cups_serverroot
);
951 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
953 cups_read_client_conf(fp
, &cc
);
958 if ((geteuid() == getuid() || !getuid()) && getegid() == getgid() && (home
= getenv("HOME")) != NULL
)
959 # elif !defined(_WIN32)
960 if (getuid() && (home
= getenv("HOME")) != NULL
)
962 if ((home
= getenv("HOME")) != NULL
)
963 # endif /* HAVE_GETEUID */
966 * Look for ~/.cups/client.conf...
969 snprintf(filename
, sizeof(filename
), "%s/.cups/client.conf", home
);
970 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
972 cups_read_client_conf(fp
, &cc
);
978 * Finalize things so every client.conf value is set...
981 cups_finalize_client_conf(&cc
);
983 if (cg
->encryption
== (http_encryption_t
)-1)
984 cg
->encryption
= cc
.encryption
;
986 if (!cg
->server
[0] || !cg
->ipp_port
)
987 cupsSetServer(cc
.server_name
);
990 cups_set_default_ipp_port(cg
);
993 strlcpy(cg
->user
, cc
.user
, sizeof(cg
->user
));
996 if (!cg
->gss_service_name
[0])
997 strlcpy(cg
->gss_service_name
, cc
.gss_service_name
, sizeof(cg
->gss_service_name
));
998 #endif /* HAVE_GSSAPI */
1000 if (cg
->trust_first
< 0)
1001 cg
->trust_first
= cc
.trust_first
;
1003 if (cg
->any_root
< 0)
1004 cg
->any_root
= cc
.any_root
;
1006 if (cg
->expired_certs
< 0)
1007 cg
->expired_certs
= cc
.expired_certs
;
1009 if (cg
->validate_certs
< 0)
1010 cg
->validate_certs
= cc
.validate_certs
;
1013 _httpTLSSetOptions(cc
.ssl_options
| _HTTP_TLS_SET_DEFAULT
, cc
.ssl_min_version
, cc
.ssl_max_version
);
1014 #endif /* HAVE_SSL */
1020 * 'cups_apple_get_boolean()' - Get a boolean setting from the CUPS preferences.
1023 static int /* O - 1 if set, 0 otherwise */
1024 cups_apple_get_boolean(
1025 CFStringRef key
, /* I - Key (name) */
1026 int *value
) /* O - Boolean value */
1028 Boolean bval
, /* Preference value */
1029 bval_set
; /* Value is set? */
1032 bval
= CFPreferencesGetAppBooleanValue(key
, kCUPSPrintingPrefs
, &bval_set
);
1037 return ((int)bval_set
);
1042 * 'cups_apple_get_string()' - Get a string setting from the CUPS preferences.
1045 static int /* O - 1 if set, 0 otherwise */
1046 cups_apple_get_string(
1047 CFStringRef key
, /* I - Key (name) */
1048 char *value
, /* O - String value */
1049 size_t valsize
) /* I - Size of value buffer */
1051 CFStringRef sval
; /* String value */
1054 if ((sval
= CFPreferencesCopyAppValue(key
, kCUPSPrintingPrefs
)) != NULL
)
1056 Boolean result
= CFStringGetCString(sval
, value
, (CFIndex
)valsize
, kCFStringEncodingUTF8
);
1066 #endif /* __APPLE__ */
1070 * 'cups_boolean_value()' - Convert a string to a boolean value.
1073 static int /* O - Boolean value */
1074 cups_boolean_value(const char *value
) /* I - String value */
1076 return (!_cups_strcasecmp(value
, "yes") || !_cups_strcasecmp(value
, "on") || !_cups_strcasecmp(value
, "true"));
1081 * 'cups_finalize_client_conf()' - Finalize client.conf values.
1085 cups_finalize_client_conf(
1086 _cups_client_conf_t
*cc
) /* I - client.conf values */
1088 const char *value
; /* Environment variable */
1091 if ((value
= getenv("CUPS_TRUSTFIRST")) != NULL
)
1092 cc
->trust_first
= cups_boolean_value(value
);
1094 if ((value
= getenv("CUPS_ANYROOT")) != NULL
)
1095 cc
->any_root
= cups_boolean_value(value
);
1097 if ((value
= getenv("CUPS_ENCRYPTION")) != NULL
)
1098 cups_set_encryption(cc
, value
);
1100 if ((value
= getenv("CUPS_EXPIREDCERTS")) != NULL
)
1101 cc
->expired_certs
= cups_boolean_value(value
);
1104 if ((value
= getenv("CUPS_GSSSERVICENAME")) != NULL
)
1105 cups_set_gss_service_name(cc
, value
);
1106 #endif /* HAVE_GSSAPI */
1108 if ((value
= getenv("CUPS_SERVER")) != NULL
)
1109 cups_set_server_name(cc
, value
);
1111 if ((value
= getenv("CUPS_USER")) != NULL
)
1112 cups_set_user(cc
, value
);
1114 if ((value
= getenv("CUPS_VALIDATECERTS")) != NULL
)
1115 cc
->validate_certs
= cups_boolean_value(value
);
1118 * Then apply defaults for those values that haven't been set...
1121 if (cc
->trust_first
< 0)
1122 cc
->trust_first
= 1;
1124 if (cc
->any_root
< 0)
1127 if (cc
->encryption
== (http_encryption_t
)-1)
1128 cc
->encryption
= HTTP_ENCRYPTION_IF_REQUESTED
;
1130 if (cc
->expired_certs
< 0)
1131 cc
->expired_certs
= 0;
1134 if (!cc
->gss_service_name
[0])
1135 cups_set_gss_service_name(cc
, CUPS_DEFAULT_GSSSERVICENAME
);
1136 #endif /* HAVE_GSSAPI */
1138 if (!cc
->server_name
[0])
1141 * If we are compiled with domain socket support, only use the
1142 * domain socket if it exists and has the right permissions...
1145 #if defined(__APPLE__) && !TARGET_OS_OSX
1146 cups_set_server_name(cc
, "/private/var/run/printd");
1149 # ifdef CUPS_DEFAULT_DOMAINSOCKET
1150 if (!access(CUPS_DEFAULT_DOMAINSOCKET
, R_OK
))
1151 cups_set_server_name(cc
, CUPS_DEFAULT_DOMAINSOCKET
);
1153 # endif /* CUPS_DEFAULT_DOMAINSOCKET */
1154 cups_set_server_name(cc
, "localhost");
1155 #endif /* __APPLE__ && !TARGET_OS_OSX */
1162 * Get the current user name from the OS...
1165 DWORD size
; /* Size of string */
1167 size
= sizeof(cc
->user
);
1168 if (!GetUserNameA(cc
->user
, &size
))
1171 * Try the USER environment variable as the default username...
1174 const char *envuser
= getenv("USER");
1175 /* Default username */
1176 struct passwd
*pw
= NULL
; /* Account information */
1181 * Validate USER matches the current UID, otherwise don't allow it to
1182 * override things... This makes sure that printing after doing su
1183 * or sudo records the correct username.
1186 if ((pw
= getpwnam(envuser
)) != NULL
&& pw
->pw_uid
!= getuid())
1191 pw
= getpwuid(getuid());
1194 strlcpy(cc
->user
, pw
->pw_name
, sizeof(cc
->user
));
1199 * Use the default "unknown" user name...
1202 strlcpy(cc
->user
, "unknown", sizeof(cc
->user
));
1206 if (cc
->validate_certs
< 0)
1207 cc
->validate_certs
= 0;
1212 * 'cups_init_client_conf()' - Initialize client.conf values.
1216 cups_init_client_conf(
1217 _cups_client_conf_t
*cc
) /* I - client.conf values */
1220 * Clear all values to "not set"...
1223 memset(cc
, 0, sizeof(_cups_client_conf_t
));
1225 #if defined(__APPLE__) && !TARGET_OS_OSX
1226 cups_set_user(cc
, "mobile");
1227 #endif /* __APPLE__ && !TARGET_OS_OSX */
1230 cc
->ssl_min_version
= _HTTP_TLS_1_0
;
1231 cc
->ssl_max_version
= _HTTP_TLS_MAX
;
1232 #endif /* HAVE_SSL */
1233 cc
->encryption
= (http_encryption_t
)-1;
1234 cc
->trust_first
= -1;
1236 cc
->expired_certs
= -1;
1237 cc
->validate_certs
= -1;
1240 * Load settings from the org.cups.PrintingPrefs plist (which trump
1244 #if defined(__APPLE__) && defined(HAVE_SSL)
1245 char sval
[1024]; /* String value */
1246 int bval
; /* Boolean value */
1248 if (cups_apple_get_boolean(kAllowAnyRootKey
, &bval
))
1249 cc
->any_root
= bval
;
1251 if (cups_apple_get_boolean(kAllowExpiredCertsKey
, &bval
))
1252 cc
->expired_certs
= bval
;
1254 if (cups_apple_get_string(kEncryptionKey
, sval
, sizeof(sval
)))
1255 cups_set_encryption(cc
, sval
);
1257 if (cups_apple_get_string(kSSLOptionsKey
, sval
, sizeof(sval
)))
1259 cups_set_ssl_options(cc
, sval
);
1265 if (cups_apple_get_boolean(kAllowRC4
, &bval
) && bval
)
1266 strlcat(sval
, " AllowRC4", sizeof(sval
));
1267 if (cups_apple_get_boolean(kAllowSSL3
, &bval
) && bval
)
1268 strlcat(sval
, " AllowSSL3", sizeof(sval
));
1269 if (cups_apple_get_boolean(kAllowDH
, &bval
) && bval
)
1270 strlcat(sval
, " AllowDH", sizeof(sval
));
1273 cups_set_ssl_options(cc
, sval
);
1276 if (cups_apple_get_boolean(kTrustOnFirstUseKey
, &bval
))
1277 cc
->trust_first
= bval
;
1279 if (cups_apple_get_boolean(kValidateCertsKey
, &bval
))
1280 cc
->validate_certs
= bval
;
1281 #endif /* __APPLE__ && HAVE_SSL */
1286 * 'cups_read_client_conf()' - Read a client.conf file.
1290 cups_read_client_conf(
1291 cups_file_t
*fp
, /* I - File to read */
1292 _cups_client_conf_t
*cc
) /* I - client.conf values */
1294 int linenum
; /* Current line number */
1295 char line
[1024], /* Line from file */
1296 *value
; /* Pointer into line */
1300 * Read from the file...
1304 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
1306 if (!_cups_strcasecmp(line
, "Encryption") && value
)
1307 cups_set_encryption(cc
, value
);
1310 * The ServerName directive is not supported on macOS due to app
1311 * sandboxing restrictions, i.e. not all apps request network access.
1313 else if (!_cups_strcasecmp(line
, "ServerName") && value
)
1314 cups_set_server_name(cc
, value
);
1315 #endif /* !__APPLE__ */
1316 else if (!_cups_strcasecmp(line
, "User") && value
)
1317 cups_set_user(cc
, value
);
1318 else if (!_cups_strcasecmp(line
, "TrustOnFirstUse") && value
)
1319 cc
->trust_first
= cups_boolean_value(value
);
1320 else if (!_cups_strcasecmp(line
, "AllowAnyRoot") && value
)
1321 cc
->any_root
= cups_boolean_value(value
);
1322 else if (!_cups_strcasecmp(line
, "AllowExpiredCerts") &&
1324 cc
->expired_certs
= cups_boolean_value(value
);
1325 else if (!_cups_strcasecmp(line
, "ValidateCerts") && value
)
1326 cc
->validate_certs
= cups_boolean_value(value
);
1328 else if (!_cups_strcasecmp(line
, "GSSServiceName") && value
)
1329 cups_set_gss_service_name(cc
, value
);
1330 #endif /* HAVE_GSSAPI */
1332 else if (!_cups_strcasecmp(line
, "SSLOptions") && value
)
1333 cups_set_ssl_options(cc
, value
);
1334 #endif /* HAVE_SSL */
1340 * 'cups_set_default_ipp_port()' - Set the default IPP port value.
1344 cups_set_default_ipp_port(
1345 _cups_globals_t
*cg
) /* I - Global data */
1347 const char *ipp_port
; /* IPP_PORT environment variable */
1350 if ((ipp_port
= getenv("IPP_PORT")) != NULL
)
1352 if ((cg
->ipp_port
= atoi(ipp_port
)) <= 0)
1353 cg
->ipp_port
= CUPS_DEFAULT_IPP_PORT
;
1356 cg
->ipp_port
= CUPS_DEFAULT_IPP_PORT
;
1360 * 'cups_set_encryption()' - Set the Encryption value.
1364 cups_set_encryption(
1365 _cups_client_conf_t
*cc
, /* I - client.conf values */
1366 const char *value
) /* I - Value */
1368 if (!_cups_strcasecmp(value
, "never"))
1369 cc
->encryption
= HTTP_ENCRYPTION_NEVER
;
1370 else if (!_cups_strcasecmp(value
, "always"))
1371 cc
->encryption
= HTTP_ENCRYPTION_ALWAYS
;
1372 else if (!_cups_strcasecmp(value
, "required"))
1373 cc
->encryption
= HTTP_ENCRYPTION_REQUIRED
;
1375 cc
->encryption
= HTTP_ENCRYPTION_IF_REQUESTED
;
1380 * 'cups_set_gss_service_name()' - Set the GSSServiceName value.
1385 cups_set_gss_service_name(
1386 _cups_client_conf_t
*cc
, /* I - client.conf values */
1387 const char *value
) /* I - Value */
1389 strlcpy(cc
->gss_service_name
, value
, sizeof(cc
->gss_service_name
));
1391 #endif /* HAVE_GSSAPI */
1395 * 'cups_set_server_name()' - Set the ServerName value.
1399 cups_set_server_name(
1400 _cups_client_conf_t
*cc
, /* I - client.conf values */
1401 const char *value
) /* I - Value */
1403 strlcpy(cc
->server_name
, value
, sizeof(cc
->server_name
));
1408 * 'cups_set_ssl_options()' - Set the SSLOptions value.
1413 cups_set_ssl_options(
1414 _cups_client_conf_t
*cc
, /* I - client.conf values */
1415 const char *value
) /* I - Value */
1418 * SSLOptions [AllowRC4] [AllowSSL3] [AllowDH] [DenyTLS1.0] [None]
1421 int options
= _HTTP_TLS_NONE
, /* SSL/TLS options */
1422 min_version
= _HTTP_TLS_1_0
, /* Minimum SSL/TLS version */
1423 max_version
= _HTTP_TLS_MAX
; /* Maximum SSL/TLS version */
1424 char temp
[256], /* Copy of value */
1425 *start
, /* Start of option */
1426 *end
; /* End of option */
1429 strlcpy(temp
, value
, sizeof(temp
));
1431 for (start
= temp
; *start
; start
= end
)
1434 * Find end of keyword...
1438 while (*end
&& !_cups_isspace(*end
))
1448 if (!_cups_strcasecmp(start
, "AllowRC4"))
1449 options
|= _HTTP_TLS_ALLOW_RC4
;
1450 else if (!_cups_strcasecmp(start
, "AllowSSL3"))
1451 min_version
= _HTTP_TLS_SSL3
;
1452 else if (!_cups_strcasecmp(start
, "AllowDH"))
1453 options
|= _HTTP_TLS_ALLOW_DH
;
1454 else if (!_cups_strcasecmp(start
, "DenyCBC"))
1455 options
|= _HTTP_TLS_DENY_CBC
;
1456 else if (!_cups_strcasecmp(start
, "DenyTLS1.0"))
1457 min_version
= _HTTP_TLS_1_1
;
1458 else if (!_cups_strcasecmp(start
, "MaxTLS1.0"))
1459 max_version
= _HTTP_TLS_1_0
;
1460 else if (!_cups_strcasecmp(start
, "MaxTLS1.1"))
1461 max_version
= _HTTP_TLS_1_1
;
1462 else if (!_cups_strcasecmp(start
, "MaxTLS1.2"))
1463 max_version
= _HTTP_TLS_1_2
;
1464 else if (!_cups_strcasecmp(start
, "MaxTLS1.3"))
1465 max_version
= _HTTP_TLS_1_3
;
1466 else if (!_cups_strcasecmp(start
, "MinTLS1.0"))
1467 min_version
= _HTTP_TLS_1_0
;
1468 else if (!_cups_strcasecmp(start
, "MinTLS1.1"))
1469 min_version
= _HTTP_TLS_1_1
;
1470 else if (!_cups_strcasecmp(start
, "MinTLS1.2"))
1471 min_version
= _HTTP_TLS_1_2
;
1472 else if (!_cups_strcasecmp(start
, "MinTLS1.3"))
1473 min_version
= _HTTP_TLS_1_3
;
1474 else if (!_cups_strcasecmp(start
, "None"))
1475 options
= _HTTP_TLS_NONE
;
1478 cc
->ssl_options
= options
;
1479 cc
->ssl_max_version
= max_version
;
1480 cc
->ssl_min_version
= min_version
;
1482 DEBUG_printf(("4cups_set_ssl_options(cc=%p, value=\"%s\") options=%x, min_version=%d, max_version=%d", (void *)cc
, value
, options
, min_version
, max_version
));
1484 #endif /* HAVE_SSL */
1488 * 'cups_set_user()' - Set the User value.
1493 _cups_client_conf_t
*cc
, /* I - client.conf values */
1494 const char *value
) /* I - Value */
1496 strlcpy(cc
->user
, value
, sizeof(cc
->user
));