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 kUserAgentTokensKey CFSTR(kPREFIX "UserAgentTokens")
44 # define kAllowAnyRootKey CFSTR(kPREFIX "AllowAnyRoot")
45 # define kAllowExpiredCertsKey CFSTR(kPREFIX "AllowExpiredCerts")
46 # define kEncryptionKey CFSTR(kPREFIX "Encryption")
47 # define kGSSServiceNameKey CFSTR(kPREFIX "GSSServiceName")
48 # define kSSLOptionsKey CFSTR(kPREFIX "SSLOptions")
49 # define kTrustOnFirstUseKey CFSTR(kPREFIX "TrustOnFirstUse")
50 # define kValidateCertsKey CFSTR(kPREFIX "ValidateCerts")
52 # define kAllowRC4 CFSTR(kPREFIX "AllowRC4")
53 # define kAllowSSL3 CFSTR(kPREFIX "AllowSSL3")
54 # define kAllowDH CFSTR(kPREFIX "AllowDH")
55 #endif /* __APPLE__ */
57 #define _CUPS_PASSCHAR '*' /* Character that is echoed for password */
64 typedef struct _cups_client_conf_s
/**** client.conf config data ****/
66 _cups_uatokens_t uatokens
; /* UserAgentTokens values */
68 int ssl_options
, /* SSLOptions values */
69 ssl_min_version
,/* Minimum SSL/TLS version */
70 ssl_max_version
;/* Maximum SSL/TLS version */
72 int trust_first
, /* Trust on first use? */
73 any_root
, /* Allow any (e.g., self-signed) root */
74 expired_certs
, /* Allow expired certs */
75 validate_certs
; /* Validate certificates */
76 http_encryption_t encryption
; /* Encryption setting */
77 char user
[65], /* User name */
81 char gss_service_name
[32];
82 /* Kerberos service name */
83 #endif /* HAVE_GSSAPI */
84 } _cups_client_conf_t
;
92 static int cups_apple_get_boolean(CFStringRef key
, int *value
);
93 static int cups_apple_get_string(CFStringRef key
, char *value
, size_t valsize
);
94 #endif /* __APPLE__ */
95 static int cups_boolean_value(const char *value
);
96 static void cups_finalize_client_conf(_cups_client_conf_t
*cc
);
97 static void cups_init_client_conf(_cups_client_conf_t
*cc
);
98 static void cups_read_client_conf(cups_file_t
*fp
, _cups_client_conf_t
*cc
);
99 static void cups_set_default_ipp_port(_cups_globals_t
*cg
);
100 static void cups_set_encryption(_cups_client_conf_t
*cc
, const char *value
);
102 static void cups_set_gss_service_name(_cups_client_conf_t
*cc
, const char *value
);
103 #endif /* HAVE_GSSAPI */
104 static void cups_set_server_name(_cups_client_conf_t
*cc
, const char *value
);
106 static void cups_set_ssl_options(_cups_client_conf_t
*cc
, const char *value
);
107 #endif /* HAVE_SSL */
108 static void cups_set_uatokens(_cups_client_conf_t
*cc
, const char *value
);
109 static void cups_set_user(_cups_client_conf_t
*cc
, const char *value
);
113 * 'cupsEncryption()' - Get the current encryption settings.
115 * The default encryption setting comes from the CUPS_ENCRYPTION
116 * environment variable, then the ~/.cups/client.conf file, and finally the
117 * /etc/cups/client.conf file. If not set, the default is
118 * @code HTTP_ENCRYPTION_IF_REQUESTED@.
120 * Note: The current encryption setting is tracked separately for each thread
121 * in a program. Multi-threaded programs that override the setting via the
122 * @link cupsSetEncryption@ function need to do so in each thread for the same
123 * setting to be used.
126 http_encryption_t
/* O - Encryption settings */
129 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
132 if (cg
->encryption
== (http_encryption_t
)-1)
135 return (cg
->encryption
);
140 * 'cupsGetPassword()' - Get a password from the user.
142 * Uses the current password callback function. Returns @code NULL@ if the
143 * user does not provide a password.
145 * Note: The current password callback function is tracked separately for each
146 * thread in a program. Multi-threaded programs that override the setting via
147 * the @link cupsSetPasswordCB@ or @link cupsSetPasswordCB2@ functions need to
148 * do so in each thread for the same function to be used.
153 const char * /* O - Password */
154 cupsGetPassword(const char *prompt
) /* I - Prompt string */
156 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
159 return ((cg
->password_cb
)(prompt
, NULL
, NULL
, NULL
, cg
->password_data
));
164 * 'cupsGetPassword2()' - Get a password from the user using the current
167 * Uses the current password callback function. Returns @code NULL@ if the
168 * user does not provide a password.
170 * Note: The current password callback function is tracked separately for each
171 * thread in a program. Multi-threaded programs that override the setting via
172 * the @link cupsSetPasswordCB2@ function need to do so in each thread for the
173 * same function to be used.
175 * @since CUPS 1.4/macOS 10.6@
178 const char * /* O - Password */
179 cupsGetPassword2(const char *prompt
, /* I - Prompt string */
180 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
181 const char *method
, /* I - Request method ("GET", "POST", "PUT") */
182 const char *resource
) /* I - Resource path */
184 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
188 http
= _cupsConnect();
190 return ((cg
->password_cb
)(prompt
, http
, method
, resource
, cg
->password_data
));
195 * 'cupsServer()' - Return the hostname/address of the current server.
197 * The default server comes from the CUPS_SERVER environment variable, then the
198 * ~/.cups/client.conf file, and finally the /etc/cups/client.conf file. If not
199 * set, the default is the local system - either "localhost" or a domain socket
202 * The returned value can be a fully-qualified hostname, a numeric IPv4 or IPv6
203 * address, or a domain socket pathname.
205 * Note: The current server is tracked separately for each thread in a program.
206 * Multi-threaded programs that override the server via the
207 * @link cupsSetServer@ function need to do so in each thread for the same
211 const char * /* O - Server name */
214 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
225 * 'cupsSetClientCertCB()' - Set the client certificate callback.
227 * Pass @code NULL@ to restore the default callback.
229 * Note: The current certificate callback is tracked separately for each thread
230 * in a program. Multi-threaded programs that override the callback need to do
231 * so in each thread for the same callback to be used.
233 * @since CUPS 1.5/macOS 10.7@
238 cups_client_cert_cb_t cb
, /* I - Callback function */
239 void *user_data
) /* I - User data pointer */
241 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
244 cg
->client_cert_cb
= cb
;
245 cg
->client_cert_data
= user_data
;
250 * 'cupsSetCredentials()' - Set the default credentials to be used for SSL/TLS
253 * Note: The default credentials are tracked separately for each thread in a
254 * program. Multi-threaded programs that override the setting need to do so in
255 * each thread for the same setting to be used.
257 * @since CUPS 1.5/macOS 10.7@
260 int /* O - Status of call (0 = success) */
262 cups_array_t
*credentials
) /* I - Array of credentials */
264 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
267 if (cupsArrayCount(credentials
) < 1)
271 _httpFreeCredentials(cg
->tls_credentials
);
272 cg
->tls_credentials
= _httpCreateCredentials(credentials
);
273 #endif /* HAVE_SSL */
275 return (cg
->tls_credentials
? 0 : -1);
280 * 'cupsSetEncryption()' - Set the encryption preference.
282 * The default encryption setting comes from the CUPS_ENCRYPTION
283 * environment variable, then the ~/.cups/client.conf file, and finally the
284 * /etc/cups/client.conf file. If not set, the default is
285 * @code HTTP_ENCRYPTION_IF_REQUESTED@.
287 * Note: The current encryption setting is tracked separately for each thread
288 * in a program. Multi-threaded programs that override the setting need to do
289 * so in each thread for the same setting to be used.
293 cupsSetEncryption(http_encryption_t e
) /* I - New encryption preference */
295 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
301 httpEncryption(cg
->http
, e
);
306 * 'cupsSetPasswordCB()' - Set the password callback for CUPS.
308 * Pass @code NULL@ to restore the default (console) password callback, which
309 * reads the password from the console. Programs should call either this
310 * function or @link cupsSetPasswordCB2@, as only one callback can be registered
311 * by a program per thread.
313 * Note: The current password callback is tracked separately for each thread
314 * in a program. Multi-threaded programs that override the callback need to do
315 * so in each thread for the same callback to be used.
321 cupsSetPasswordCB(cups_password_cb_t cb
)/* I - Callback function */
323 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
326 if (cb
== (cups_password_cb_t
)0)
327 cg
->password_cb
= (cups_password_cb2_t
)_cupsGetPassword
;
329 cg
->password_cb
= (cups_password_cb2_t
)cb
;
331 cg
->password_data
= NULL
;
336 * 'cupsSetPasswordCB2()' - Set the advanced password callback for CUPS.
338 * Pass @code NULL@ to restore the default (console) password callback, which
339 * reads the password from the console. Programs should call either this
340 * function or @link cupsSetPasswordCB2@, as only one callback can be registered
341 * by a program per thread.
343 * Note: The current password callback is tracked separately for each thread
344 * in a program. Multi-threaded programs that override the callback need to do
345 * so in each thread for the same callback to be used.
347 * @since CUPS 1.4/macOS 10.6@
352 cups_password_cb2_t cb
, /* I - Callback function */
353 void *user_data
) /* I - User data pointer */
355 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
358 if (cb
== (cups_password_cb2_t
)0)
359 cg
->password_cb
= (cups_password_cb2_t
)_cupsGetPassword
;
361 cg
->password_cb
= cb
;
363 cg
->password_data
= user_data
;
368 * 'cupsSetServer()' - Set the default server name and port.
370 * The "server" string can be a fully-qualified hostname, a numeric
371 * IPv4 or IPv6 address, or a domain socket pathname. Hostnames and numeric IP
372 * addresses can be optionally followed by a colon and port number to override
373 * the default port 631, e.g. "hostname:8631". Pass @code NULL@ to restore the
374 * default server name and port.
376 * Note: The current server is tracked separately for each thread in a program.
377 * Multi-threaded programs that override the server need to do so in each
378 * thread for the same server to be used.
382 cupsSetServer(const char *server
) /* I - Server name */
384 char *options
, /* Options */
385 *port
; /* Pointer to port */
386 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
391 strlcpy(cg
->server
, server
, sizeof(cg
->server
));
393 if (cg
->server
[0] != '/' && (options
= strrchr(cg
->server
, '/')) != NULL
)
397 if (!strcmp(options
, "version=1.0"))
398 cg
->server_version
= 10;
399 else if (!strcmp(options
, "version=1.1"))
400 cg
->server_version
= 11;
401 else if (!strcmp(options
, "version=2.0"))
402 cg
->server_version
= 20;
403 else if (!strcmp(options
, "version=2.1"))
404 cg
->server_version
= 21;
405 else if (!strcmp(options
, "version=2.2"))
406 cg
->server_version
= 22;
409 cg
->server_version
= 20;
411 if (cg
->server
[0] != '/' && (port
= strrchr(cg
->server
, ':')) != NULL
&&
412 !strchr(port
, ']') && isdigit(port
[1] & 255))
416 cg
->ipp_port
= atoi(port
);
420 cups_set_default_ipp_port(cg
);
422 if (cg
->server
[0] == '/')
423 strlcpy(cg
->servername
, "localhost", sizeof(cg
->servername
));
425 strlcpy(cg
->servername
, cg
->server
, sizeof(cg
->servername
));
429 cg
->server
[0] = '\0';
430 cg
->servername
[0] = '\0';
431 cg
->server_version
= 20;
444 * 'cupsSetServerCertCB()' - Set the server certificate callback.
446 * Pass @code NULL@ to restore the default callback.
448 * Note: The current credentials callback is tracked separately for each thread
449 * in a program. Multi-threaded programs that override the callback need to do
450 * so in each thread for the same callback to be used.
452 * @since CUPS 1.5/macOS 10.7@
457 cups_server_cert_cb_t cb
, /* I - Callback function */
458 void *user_data
) /* I - User data pointer */
460 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
463 cg
->server_cert_cb
= cb
;
464 cg
->server_cert_data
= user_data
;
469 * 'cupsSetUser()' - Set the default user name.
471 * Pass @code NULL@ to restore the default user name.
473 * Note: The current user name is tracked separately for each thread in a
474 * program. Multi-threaded programs that override the user name need to do so
475 * in each thread for the same user name to be used.
479 cupsSetUser(const char *user
) /* I - User name */
481 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
485 strlcpy(cg
->user
, user
, sizeof(cg
->user
));
492 * 'cupsSetUserAgent()' - Set the default HTTP User-Agent string.
494 * Setting the string to NULL forces the default value containing the CUPS
495 * version, IPP version, and operating system version and architecture.
497 * @since CUPS 1.7/macOS 10.9@
501 cupsSetUserAgent(const char *user_agent
)/* I - User-Agent string or @code NULL@ */
503 _cups_globals_t
*cg
= _cupsGlobals();
506 SYSTEM_INFO sysinfo
; /* System information */
507 OSVERSIONINFOA version
; /* OS version info */
508 const char *machine
; /* Hardware/machine name */
509 #elif defined(__APPLE__)
510 struct utsname name
; /* uname info */
511 char version
[256]; /* macOS/iOS version */
512 size_t len
; /* Length of value */
514 struct utsname name
; /* uname info */
520 strlcpy(cg
->user_agent
, user_agent
, sizeof(cg
->user_agent
));
524 if (cg
->uatokens
< _CUPS_UATOKENS_OS
)
526 switch (cg
->uatokens
)
529 case _CUPS_UATOKENS_NONE
:
530 cg
->user_agent
[0] = '\0';
532 case _CUPS_UATOKENS_PRODUCT_ONLY
:
533 strlcpy(cg
->user_agent
, "CUPS IPP", sizeof(cg
->user_agent
));
535 case _CUPS_UATOKENS_MAJOR
:
536 snprintf(cg
->user_agent
, sizeof(cg
->user_agent
), "CUPS/%d IPP/2", CUPS_VERSION_MAJOR
);
538 case _CUPS_UATOKENS_MINOR
:
539 snprintf(cg
->user_agent
, sizeof(cg
->user_agent
), "CUPS/%d.%d IPP/2.1", CUPS_VERSION_MAJOR
, CUPS_VERSION_MINOR
);
541 case _CUPS_UATOKENS_MINIMAL
:
542 strlcpy(cg
->user_agent
, CUPS_MINIMAL
" IPP/2.1", sizeof(cg
->user_agent
));
549 * Gather Windows version information for the User-Agent string...
552 version
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
553 GetVersionExA(&version
);
554 GetNativeSystemInfo(&sysinfo
);
556 switch (sysinfo
.wProcessorArchitecture
)
558 case PROCESSOR_ARCHITECTURE_AMD64
:
562 case PROCESSOR_ARCHITECTURE_ARM
:
566 case PROCESSOR_ARCHITECTURE_IA64
:
570 case PROCESSOR_ARCHITECTURE_INTEL
:
579 if (cg
->uatokens
== _CUPS_UATOKENS_OS
)
580 snprintf(cg
->user_agent
, sizeof(cg
->user_agent
), CUPS_MINIMAL
" (Windows %d.%d) IPP/2.0", version
.dwMajorVersion
, version
.dwMinorVersion
);
582 snprintf(cg
->user_agent
, sizeof(cg
->user_agent
), CUPS_MINIMAL
" (Windows %d.%d; %s) IPP/2.0", version
.dwMajorVersion
, version
.dwMinorVersion
, machine
);
584 #elif defined(__APPLE__)
586 * Gather macOS/iOS version information for the User-Agent string...
591 len
= sizeof(version
) - 1;
592 if (!sysctlbyname("kern.osproductversion", version
, &len
, NULL
, 0))
595 strlcpy(version
, "unknown", sizeof(version
));
598 if (cg
->uatokens
== _CUPS_UATOKENS_OS
)
599 snprintf(cg
->user_agent
, sizeof(cg
->user_agent
), CUPS_MINIMAL
" (macOS %s) IPP/2.0", version
);
601 snprintf(cg
->user_agent
, sizeof(cg
->user_agent
), CUPS_MINIMAL
" (macOS %s; %s) IPP/2.0", version
, name
.machine
);
604 if (cg
->uatokens
== _CUPS_UATOKENS_OS
)
605 snprintf(cg
->user_agent
, sizeof(cg
->user_agent
), CUPS_MINIMAL
" (iOS %s) IPP/2.0", version
);
607 snprintf(cg
->user_agent
, sizeof(cg
->user_agent
), CUPS_MINIMAL
" (iOS %s; %s) IPP/2.0", version
, name
.machine
);
608 # endif /* TARGET_OS_OSX */
612 * Gather generic UNIX version information for the User-Agent string...
617 if (cg
->uatokens
== _CUPS_UATOKENS_OS
)
618 snprintf(cg
->user_agent
, sizeof(cg
->user_agent
), CUPS_MINIMAL
" (%s %s) IPP/2.0", name
.sysname
, name
.release
);
620 snprintf(cg
->user_agent
, sizeof(cg
->user_agent
), CUPS_MINIMAL
" (%s %s; %s) IPP/2.0", name
.sysname
, name
.release
, name
.machine
);
626 * 'cupsUser()' - Return the current user's name.
628 * Note: The current user name is tracked separately for each thread in a
629 * program. Multi-threaded programs that override the user name with the
630 * @link cupsSetUser@ function need to do so in each thread for the same user
634 const char * /* O - User name */
637 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
648 * 'cupsUserAgent()' - Return the default HTTP User-Agent string.
650 * @since CUPS 1.7/macOS 10.9@
653 const char * /* O - User-Agent string */
656 _cups_globals_t
*cg
= _cupsGlobals(); /* Thread globals */
659 if (!cg
->user_agent
[0])
660 cupsSetUserAgent(NULL
);
662 return (cg
->user_agent
);
667 * '_cupsGetPassword()' - Get a password from the user.
670 const char * /* O - Password or @code NULL@ if none */
671 _cupsGetPassword(const char *prompt
) /* I - Prompt string */
674 HANDLE tty
; /* Console handle */
675 DWORD mode
; /* Console mode */
676 char passch
, /* Current key press */
677 *passptr
, /* Pointer into password string */
678 *passend
; /* End of password string */
679 DWORD passbytes
; /* Bytes read */
680 _cups_globals_t
*cg
= _cupsGlobals();
685 * Disable input echo and set raw input...
688 if ((tty
= GetStdHandle(STD_INPUT_HANDLE
)) == INVALID_HANDLE_VALUE
)
691 if (!GetConsoleMode(tty
, &mode
))
694 if (!SetConsoleMode(tty
, 0))
698 * Display the prompt...
701 printf("%s ", prompt
);
705 * Read the password string from /dev/tty until we get interrupted or get a
706 * carriage return or newline...
709 passptr
= cg
->password
;
710 passend
= cg
->password
+ sizeof(cg
->password
) - 1;
712 while (ReadFile(tty
, &passch
, 1, &passbytes
, NULL
))
714 if (passch
== 0x0A || passch
== 0x0D)
722 else if (passch
== 0x08 || passch
== 0x7F)
725 * Backspace/delete (erase character)...
728 if (passptr
> cg
->password
)
731 fputs("\010 \010", stdout
);
736 else if (passch
== 0x15)
739 * CTRL+U (erase line)
742 if (passptr
> cg
->password
)
744 while (passptr
> cg
->password
)
747 fputs("\010 \010", stdout
);
753 else if (passch
== 0x03)
759 passptr
= cg
->password
;
762 else if ((passch
& 255) < 0x20 || passptr
>= passend
)
767 putchar(_CUPS_PASSCHAR
);
780 SetConsoleMode(tty
, mode
);
783 * Return the proper value...
786 if (passbytes
== 1 && passptr
> cg
->password
)
789 return (cg
->password
);
793 memset(cg
->password
, 0, sizeof(cg
->password
));
798 int tty
; /* /dev/tty - never read from stdin */
799 struct termios original
, /* Original input mode */
800 noecho
; /* No echo input mode */
801 char passch
, /* Current key press */
802 *passptr
, /* Pointer into password string */
803 *passend
; /* End of password string */
804 ssize_t passbytes
; /* Bytes read */
805 _cups_globals_t
*cg
= _cupsGlobals();
810 * Disable input echo and set raw input...
813 if ((tty
= open("/dev/tty", O_RDONLY
)) < 0)
816 if (tcgetattr(tty
, &original
))
823 noecho
.c_lflag
&= (tcflag_t
)~(ICANON
| ECHO
| ECHOE
| ISIG
);
824 noecho
.c_cc
[VMIN
] = 1;
825 noecho
.c_cc
[VTIME
] = 0;
827 if (tcsetattr(tty
, TCSAFLUSH
, &noecho
))
834 * Display the prompt...
837 printf("%s ", prompt
);
841 * Read the password string from /dev/tty until we get interrupted or get a
842 * carriage return or newline...
845 passptr
= cg
->password
;
846 passend
= cg
->password
+ sizeof(cg
->password
) - 1;
848 while ((passbytes
= read(tty
, &passch
, 1)) == 1)
850 if (passch
== noecho
.c_cc
[VEOL
] ||
852 passch
== noecho
.c_cc
[VEOL2
] ||
854 passch
== 0x0A || passch
== 0x0D)
862 else if (passch
== noecho
.c_cc
[VERASE
] ||
863 passch
== 0x08 || passch
== 0x7F)
866 * Backspace/delete (erase character)...
869 if (passptr
> cg
->password
)
872 fputs("\010 \010", stdout
);
877 else if (passch
== noecho
.c_cc
[VKILL
])
880 * CTRL+U (erase line)
883 if (passptr
> cg
->password
)
885 while (passptr
> cg
->password
)
888 fputs("\010 \010", stdout
);
894 else if (passch
== noecho
.c_cc
[VINTR
] || passch
== noecho
.c_cc
[VQUIT
] ||
895 passch
== noecho
.c_cc
[VEOF
])
898 * CTRL+C, CTRL+D, or CTRL+Z...
901 passptr
= cg
->password
;
904 else if ((passch
& 255) < 0x20 || passptr
>= passend
)
909 putchar(_CUPS_PASSCHAR
);
922 tcsetattr(tty
, TCSAFLUSH
, &original
);
926 * Return the proper value...
929 if (passbytes
== 1 && passptr
> cg
->password
)
932 return (cg
->password
);
936 memset(cg
->password
, 0, sizeof(cg
->password
));
945 * '_cupsGSSServiceName()' - Get the GSS (Kerberos) service name.
949 _cupsGSSServiceName(void)
951 _cups_globals_t
*cg
= _cupsGlobals(); /* Thread globals */
954 if (!cg
->gss_service_name
[0])
957 return (cg
->gss_service_name
);
959 #endif /* HAVE_GSSAPI */
963 * '_cupsSetDefaults()' - Set the default server, port, and encryption.
967 _cupsSetDefaults(void)
969 cups_file_t
*fp
; /* File */
970 const char *home
; /* Home directory of user */
971 char filename
[1024]; /* Filename */
972 _cups_client_conf_t cc
; /* client.conf values */
973 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
976 DEBUG_puts("_cupsSetDefaults()");
979 * Load initial client.conf values...
982 cups_init_client_conf(&cc
);
985 * Read the /etc/cups/client.conf and ~/.cups/client.conf files, if
989 snprintf(filename
, sizeof(filename
), "%s/client.conf", cg
->cups_serverroot
);
990 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
992 cups_read_client_conf(fp
, &cc
);
997 if ((geteuid() == getuid() || !getuid()) && getegid() == getgid() && (home
= getenv("HOME")) != NULL
)
998 # elif !defined(_WIN32)
999 if (getuid() && (home
= getenv("HOME")) != NULL
)
1001 if ((home
= getenv("HOME")) != NULL
)
1002 # endif /* HAVE_GETEUID */
1005 * Look for ~/.cups/client.conf...
1008 snprintf(filename
, sizeof(filename
), "%s/.cups/client.conf", home
);
1009 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
1011 cups_read_client_conf(fp
, &cc
);
1017 * Finalize things so every client.conf value is set...
1020 cups_finalize_client_conf(&cc
);
1022 cg
->uatokens
= cc
.uatokens
;
1024 if (cg
->encryption
== (http_encryption_t
)-1)
1025 cg
->encryption
= cc
.encryption
;
1027 if (!cg
->server
[0] || !cg
->ipp_port
)
1028 cupsSetServer(cc
.server_name
);
1031 cups_set_default_ipp_port(cg
);
1034 strlcpy(cg
->user
, cc
.user
, sizeof(cg
->user
));
1037 if (!cg
->gss_service_name
[0])
1038 strlcpy(cg
->gss_service_name
, cc
.gss_service_name
, sizeof(cg
->gss_service_name
));
1039 #endif /* HAVE_GSSAPI */
1041 if (cg
->trust_first
< 0)
1042 cg
->trust_first
= cc
.trust_first
;
1044 if (cg
->any_root
< 0)
1045 cg
->any_root
= cc
.any_root
;
1047 if (cg
->expired_certs
< 0)
1048 cg
->expired_certs
= cc
.expired_certs
;
1050 if (cg
->validate_certs
< 0)
1051 cg
->validate_certs
= cc
.validate_certs
;
1054 _httpTLSSetOptions(cc
.ssl_options
| _HTTP_TLS_SET_DEFAULT
, cc
.ssl_min_version
, cc
.ssl_max_version
);
1055 #endif /* HAVE_SSL */
1061 * 'cups_apple_get_boolean()' - Get a boolean setting from the CUPS preferences.
1064 static int /* O - 1 if set, 0 otherwise */
1065 cups_apple_get_boolean(
1066 CFStringRef key
, /* I - Key (name) */
1067 int *value
) /* O - Boolean value */
1069 Boolean bval
, /* Preference value */
1070 bval_set
; /* Value is set? */
1073 bval
= CFPreferencesGetAppBooleanValue(key
, kCUPSPrintingPrefs
, &bval_set
);
1078 return ((int)bval_set
);
1083 * 'cups_apple_get_string()' - Get a string setting from the CUPS preferences.
1086 static int /* O - 1 if set, 0 otherwise */
1087 cups_apple_get_string(
1088 CFStringRef key
, /* I - Key (name) */
1089 char *value
, /* O - String value */
1090 size_t valsize
) /* I - Size of value buffer */
1092 CFStringRef sval
; /* String value */
1095 if ((sval
= CFPreferencesCopyAppValue(key
, kCUPSPrintingPrefs
)) != NULL
)
1097 Boolean result
= CFStringGetCString(sval
, value
, (CFIndex
)valsize
, kCFStringEncodingUTF8
);
1107 #endif /* __APPLE__ */
1111 * 'cups_boolean_value()' - Convert a string to a boolean value.
1114 static int /* O - Boolean value */
1115 cups_boolean_value(const char *value
) /* I - String value */
1117 return (!_cups_strcasecmp(value
, "yes") || !_cups_strcasecmp(value
, "on") || !_cups_strcasecmp(value
, "true"));
1122 * 'cups_finalize_client_conf()' - Finalize client.conf values.
1126 cups_finalize_client_conf(
1127 _cups_client_conf_t
*cc
) /* I - client.conf values */
1129 const char *value
; /* Environment variable */
1132 if ((value
= getenv("CUPS_TRUSTFIRST")) != NULL
)
1133 cc
->trust_first
= cups_boolean_value(value
);
1135 if ((value
= getenv("CUPS_ANYROOT")) != NULL
)
1136 cc
->any_root
= cups_boolean_value(value
);
1138 if ((value
= getenv("CUPS_ENCRYPTION")) != NULL
)
1139 cups_set_encryption(cc
, value
);
1141 if ((value
= getenv("CUPS_EXPIREDCERTS")) != NULL
)
1142 cc
->expired_certs
= cups_boolean_value(value
);
1145 if ((value
= getenv("CUPS_GSSSERVICENAME")) != NULL
)
1146 cups_set_gss_service_name(cc
, value
);
1147 #endif /* HAVE_GSSAPI */
1149 if ((value
= getenv("CUPS_SERVER")) != NULL
)
1150 cups_set_server_name(cc
, value
);
1152 if ((value
= getenv("CUPS_USER")) != NULL
)
1153 cups_set_user(cc
, value
);
1155 if ((value
= getenv("CUPS_VALIDATECERTS")) != NULL
)
1156 cc
->validate_certs
= cups_boolean_value(value
);
1159 * Then apply defaults for those values that haven't been set...
1162 if (cc
->trust_first
< 0)
1163 cc
->trust_first
= 1;
1165 if (cc
->any_root
< 0)
1168 if (cc
->encryption
== (http_encryption_t
)-1)
1169 cc
->encryption
= HTTP_ENCRYPTION_IF_REQUESTED
;
1171 if (cc
->expired_certs
< 0)
1172 cc
->expired_certs
= 0;
1175 if (!cc
->gss_service_name
[0])
1176 cups_set_gss_service_name(cc
, CUPS_DEFAULT_GSSSERVICENAME
);
1177 #endif /* HAVE_GSSAPI */
1179 if (!cc
->server_name
[0])
1182 * If we are compiled with domain socket support, only use the
1183 * domain socket if it exists and has the right permissions...
1186 #if defined(__APPLE__) && !TARGET_OS_OSX
1187 cups_set_server_name(cc
, "/private/var/run/printd");
1190 # ifdef CUPS_DEFAULT_DOMAINSOCKET
1191 if (!access(CUPS_DEFAULT_DOMAINSOCKET
, R_OK
))
1192 cups_set_server_name(cc
, CUPS_DEFAULT_DOMAINSOCKET
);
1194 # endif /* CUPS_DEFAULT_DOMAINSOCKET */
1195 cups_set_server_name(cc
, "localhost");
1196 #endif /* __APPLE__ && !TARGET_OS_OSX */
1203 * Get the current user name from the OS...
1206 DWORD size
; /* Size of string */
1208 size
= sizeof(cc
->user
);
1209 if (!GetUserNameA(cc
->user
, &size
))
1212 * Try the USER environment variable as the default username...
1215 const char *envuser
= getenv("USER");
1216 /* Default username */
1217 struct passwd
*pw
= NULL
; /* Account information */
1222 * Validate USER matches the current UID, otherwise don't allow it to
1223 * override things... This makes sure that printing after doing su
1224 * or sudo records the correct username.
1227 if ((pw
= getpwnam(envuser
)) != NULL
&& pw
->pw_uid
!= getuid())
1232 pw
= getpwuid(getuid());
1235 strlcpy(cc
->user
, pw
->pw_name
, sizeof(cc
->user
));
1240 * Use the default "unknown" user name...
1243 strlcpy(cc
->user
, "unknown", sizeof(cc
->user
));
1247 if (cc
->validate_certs
< 0)
1248 cc
->validate_certs
= 0;
1253 * 'cups_init_client_conf()' - Initialize client.conf values.
1257 cups_init_client_conf(
1258 _cups_client_conf_t
*cc
) /* I - client.conf values */
1261 * Clear all values to "not set"...
1264 memset(cc
, 0, sizeof(_cups_client_conf_t
));
1266 cc
->uatokens
= _CUPS_UATOKENS_MINIMAL
;
1268 #if defined(__APPLE__) && !TARGET_OS_OSX
1269 cups_set_user(cc
, "mobile");
1270 #endif /* __APPLE__ && !TARGET_OS_OSX */
1273 cc
->ssl_min_version
= _HTTP_TLS_1_0
;
1274 cc
->ssl_max_version
= _HTTP_TLS_MAX
;
1275 #endif /* HAVE_SSL */
1276 cc
->encryption
= (http_encryption_t
)-1;
1277 cc
->trust_first
= -1;
1279 cc
->expired_certs
= -1;
1280 cc
->validate_certs
= -1;
1283 * Load settings from the org.cups.PrintingPrefs plist (which trump
1287 #if defined(__APPLE__)
1288 char sval
[1024]; /* String value */
1290 int bval
; /* Boolean value */
1292 if (cups_apple_get_boolean(kAllowAnyRootKey
, &bval
))
1293 cc
->any_root
= bval
;
1295 if (cups_apple_get_boolean(kAllowExpiredCertsKey
, &bval
))
1296 cc
->expired_certs
= bval
;
1298 if (cups_apple_get_string(kEncryptionKey
, sval
, sizeof(sval
)))
1299 cups_set_encryption(cc
, sval
);
1301 if (cups_apple_get_string(kSSLOptionsKey
, sval
, sizeof(sval
)))
1303 cups_set_ssl_options(cc
, sval
);
1309 if (cups_apple_get_boolean(kAllowRC4
, &bval
) && bval
)
1310 strlcat(sval
, " AllowRC4", sizeof(sval
));
1311 if (cups_apple_get_boolean(kAllowSSL3
, &bval
) && bval
)
1312 strlcat(sval
, " AllowSSL3", sizeof(sval
));
1313 if (cups_apple_get_boolean(kAllowDH
, &bval
) && bval
)
1314 strlcat(sval
, " AllowDH", sizeof(sval
));
1317 cups_set_ssl_options(cc
, sval
);
1320 if (cups_apple_get_boolean(kTrustOnFirstUseKey
, &bval
))
1321 cc
->trust_first
= bval
;
1323 if (cups_apple_get_boolean(kValidateCertsKey
, &bval
))
1324 cc
->validate_certs
= bval
;
1325 # endif /* HAVE_SSL */
1327 if (cups_apple_get_string(kUserAgentTokensKey
, sval
, sizeof(sval
)))
1329 cups_set_uatokens(cc
, sval
);
1331 #endif /* __APPLE__ */
1336 * 'cups_read_client_conf()' - Read a client.conf file.
1340 cups_read_client_conf(
1341 cups_file_t
*fp
, /* I - File to read */
1342 _cups_client_conf_t
*cc
) /* I - client.conf values */
1344 int linenum
; /* Current line number */
1345 char line
[1024], /* Line from file */
1346 *value
; /* Pointer into line */
1350 * Read from the file...
1354 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
1356 if (!_cups_strcasecmp(line
, "Encryption") && value
)
1357 cups_set_encryption(cc
, value
);
1360 * The ServerName directive is not supported on macOS due to app
1361 * sandboxing restrictions, i.e. not all apps request network access.
1363 else if (!_cups_strcasecmp(line
, "ServerName") && value
)
1364 cups_set_server_name(cc
, value
);
1365 #endif /* !__APPLE__ */
1366 else if (!_cups_strcasecmp(line
, "User") && value
)
1367 cups_set_user(cc
, value
);
1368 else if (!_cups_strcasecmp(line
, "UserAgentTokens") && value
)
1369 cups_set_uatokens(cc
, value
);
1370 else if (!_cups_strcasecmp(line
, "TrustOnFirstUse") && value
)
1371 cc
->trust_first
= cups_boolean_value(value
);
1372 else if (!_cups_strcasecmp(line
, "AllowAnyRoot") && value
)
1373 cc
->any_root
= cups_boolean_value(value
);
1374 else if (!_cups_strcasecmp(line
, "AllowExpiredCerts") &&
1376 cc
->expired_certs
= cups_boolean_value(value
);
1377 else if (!_cups_strcasecmp(line
, "ValidateCerts") && value
)
1378 cc
->validate_certs
= cups_boolean_value(value
);
1380 else if (!_cups_strcasecmp(line
, "GSSServiceName") && value
)
1381 cups_set_gss_service_name(cc
, value
);
1382 #endif /* HAVE_GSSAPI */
1384 else if (!_cups_strcasecmp(line
, "SSLOptions") && value
)
1385 cups_set_ssl_options(cc
, value
);
1386 #endif /* HAVE_SSL */
1392 * 'cups_set_default_ipp_port()' - Set the default IPP port value.
1396 cups_set_default_ipp_port(
1397 _cups_globals_t
*cg
) /* I - Global data */
1399 const char *ipp_port
; /* IPP_PORT environment variable */
1402 if ((ipp_port
= getenv("IPP_PORT")) != NULL
)
1404 if ((cg
->ipp_port
= atoi(ipp_port
)) <= 0)
1405 cg
->ipp_port
= CUPS_DEFAULT_IPP_PORT
;
1408 cg
->ipp_port
= CUPS_DEFAULT_IPP_PORT
;
1412 * 'cups_set_encryption()' - Set the Encryption value.
1416 cups_set_encryption(
1417 _cups_client_conf_t
*cc
, /* I - client.conf values */
1418 const char *value
) /* I - Value */
1420 if (!_cups_strcasecmp(value
, "never"))
1421 cc
->encryption
= HTTP_ENCRYPTION_NEVER
;
1422 else if (!_cups_strcasecmp(value
, "always"))
1423 cc
->encryption
= HTTP_ENCRYPTION_ALWAYS
;
1424 else if (!_cups_strcasecmp(value
, "required"))
1425 cc
->encryption
= HTTP_ENCRYPTION_REQUIRED
;
1427 cc
->encryption
= HTTP_ENCRYPTION_IF_REQUESTED
;
1432 * 'cups_set_gss_service_name()' - Set the GSSServiceName value.
1437 cups_set_gss_service_name(
1438 _cups_client_conf_t
*cc
, /* I - client.conf values */
1439 const char *value
) /* I - Value */
1441 strlcpy(cc
->gss_service_name
, value
, sizeof(cc
->gss_service_name
));
1443 #endif /* HAVE_GSSAPI */
1447 * 'cups_set_server_name()' - Set the ServerName value.
1451 cups_set_server_name(
1452 _cups_client_conf_t
*cc
, /* I - client.conf values */
1453 const char *value
) /* I - Value */
1455 strlcpy(cc
->server_name
, value
, sizeof(cc
->server_name
));
1460 * 'cups_set_ssl_options()' - Set the SSLOptions value.
1465 cups_set_ssl_options(
1466 _cups_client_conf_t
*cc
, /* I - client.conf values */
1467 const char *value
) /* I - Value */
1470 * SSLOptions [AllowRC4] [AllowSSL3] [AllowDH] [DenyTLS1.0] [None]
1473 int options
= _HTTP_TLS_NONE
, /* SSL/TLS options */
1474 min_version
= _HTTP_TLS_1_0
, /* Minimum SSL/TLS version */
1475 max_version
= _HTTP_TLS_MAX
; /* Maximum SSL/TLS version */
1476 char temp
[256], /* Copy of value */
1477 *start
, /* Start of option */
1478 *end
; /* End of option */
1481 strlcpy(temp
, value
, sizeof(temp
));
1483 for (start
= temp
; *start
; start
= end
)
1486 * Find end of keyword...
1490 while (*end
&& !_cups_isspace(*end
))
1500 if (!_cups_strcasecmp(start
, "AllowRC4"))
1501 options
|= _HTTP_TLS_ALLOW_RC4
;
1502 else if (!_cups_strcasecmp(start
, "AllowSSL3"))
1503 min_version
= _HTTP_TLS_SSL3
;
1504 else if (!_cups_strcasecmp(start
, "AllowDH"))
1505 options
|= _HTTP_TLS_ALLOW_DH
;
1506 else if (!_cups_strcasecmp(start
, "DenyCBC"))
1507 options
|= _HTTP_TLS_DENY_CBC
;
1508 else if (!_cups_strcasecmp(start
, "DenyTLS1.0"))
1509 min_version
= _HTTP_TLS_1_1
;
1510 else if (!_cups_strcasecmp(start
, "MaxTLS1.0"))
1511 max_version
= _HTTP_TLS_1_0
;
1512 else if (!_cups_strcasecmp(start
, "MaxTLS1.1"))
1513 max_version
= _HTTP_TLS_1_1
;
1514 else if (!_cups_strcasecmp(start
, "MaxTLS1.2"))
1515 max_version
= _HTTP_TLS_1_2
;
1516 else if (!_cups_strcasecmp(start
, "MaxTLS1.3"))
1517 max_version
= _HTTP_TLS_1_3
;
1518 else if (!_cups_strcasecmp(start
, "MinTLS1.0"))
1519 min_version
= _HTTP_TLS_1_0
;
1520 else if (!_cups_strcasecmp(start
, "MinTLS1.1"))
1521 min_version
= _HTTP_TLS_1_1
;
1522 else if (!_cups_strcasecmp(start
, "MinTLS1.2"))
1523 min_version
= _HTTP_TLS_1_2
;
1524 else if (!_cups_strcasecmp(start
, "MinTLS1.3"))
1525 min_version
= _HTTP_TLS_1_3
;
1526 else if (!_cups_strcasecmp(start
, "None"))
1527 options
= _HTTP_TLS_NONE
;
1530 cc
->ssl_options
= options
;
1531 cc
->ssl_max_version
= max_version
;
1532 cc
->ssl_min_version
= min_version
;
1534 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
));
1536 #endif /* HAVE_SSL */
1540 * 'cups_set_uatokens()' - Set the UserAgentTokens value.
1545 _cups_client_conf_t
*cc
, /* I - client.conf values */
1546 const char *value
) /* I - Value */
1548 int i
; /* Looping var */
1549 static const char * const uatokens
[] =/* UserAgentTokens values */
1560 for (i
= 0; i
< (int)(sizeof(uatokens
) / sizeof(uatokens
[0])); i
++)
1562 if (!_cups_strcasecmp(value
, uatokens
[i
]))
1564 cc
->uatokens
= (_cups_uatokens_t
)i
;
1572 * 'cups_set_user()' - Set the User value.
1577 _cups_client_conf_t
*cc
, /* I - client.conf values */
1578 const char *value
) /* I - Value */
1580 strlcpy(cc
->user
, value
, sizeof(cc
->user
));