xref: /aosp_15_r20/external/libcups/cups/usersys.c (revision 5e7646d21f1134fb0638875d812ef646c12ab91e)
1*5e7646d2SAndroid Build Coastguard Worker /*
2*5e7646d2SAndroid Build Coastguard Worker  * User, system, and password routines for CUPS.
3*5e7646d2SAndroid Build Coastguard Worker  *
4*5e7646d2SAndroid Build Coastguard Worker  * Copyright 2007-2019 by Apple Inc.
5*5e7646d2SAndroid Build Coastguard Worker  * Copyright 1997-2006 by Easy Software Products.
6*5e7646d2SAndroid Build Coastguard Worker  *
7*5e7646d2SAndroid Build Coastguard Worker  * Licensed under Apache License v2.0.  See the file "LICENSE" for more
8*5e7646d2SAndroid Build Coastguard Worker  * information.
9*5e7646d2SAndroid Build Coastguard Worker  */
10*5e7646d2SAndroid Build Coastguard Worker 
11*5e7646d2SAndroid Build Coastguard Worker /*
12*5e7646d2SAndroid Build Coastguard Worker  * Include necessary headers...
13*5e7646d2SAndroid Build Coastguard Worker  */
14*5e7646d2SAndroid Build Coastguard Worker 
15*5e7646d2SAndroid Build Coastguard Worker #include "cups-private.h"
16*5e7646d2SAndroid Build Coastguard Worker #include "debug-internal.h"
17*5e7646d2SAndroid Build Coastguard Worker #include <stdlib.h>
18*5e7646d2SAndroid Build Coastguard Worker #include <sys/stat.h>
19*5e7646d2SAndroid Build Coastguard Worker #ifdef _WIN32
20*5e7646d2SAndroid Build Coastguard Worker #  include <windows.h>
21*5e7646d2SAndroid Build Coastguard Worker #else
22*5e7646d2SAndroid Build Coastguard Worker #  include <pwd.h>
23*5e7646d2SAndroid Build Coastguard Worker #  include <termios.h>
24*5e7646d2SAndroid Build Coastguard Worker #  include <sys/utsname.h>
25*5e7646d2SAndroid Build Coastguard Worker #endif /* _WIN32 */
26*5e7646d2SAndroid Build Coastguard Worker #ifdef __APPLE__
27*5e7646d2SAndroid Build Coastguard Worker #  include <sys/sysctl.h>
28*5e7646d2SAndroid Build Coastguard Worker #endif /* __APPLE__ */
29*5e7646d2SAndroid Build Coastguard Worker 
30*5e7646d2SAndroid Build Coastguard Worker 
31*5e7646d2SAndroid Build Coastguard Worker /*
32*5e7646d2SAndroid Build Coastguard Worker  * Local constants...
33*5e7646d2SAndroid Build Coastguard Worker  */
34*5e7646d2SAndroid Build Coastguard Worker 
35*5e7646d2SAndroid Build Coastguard Worker #ifdef __APPLE__
36*5e7646d2SAndroid Build Coastguard Worker #  if TARGET_OS_OSX
37*5e7646d2SAndroid Build Coastguard Worker #    define kCUPSPrintingPrefs	CFSTR("org.cups.PrintingPrefs")
38*5e7646d2SAndroid Build Coastguard Worker #    define kPREFIX		""
39*5e7646d2SAndroid Build Coastguard Worker #  else
40*5e7646d2SAndroid Build Coastguard Worker #    define kCUPSPrintingPrefs	CFSTR(".GlobalPreferences")
41*5e7646d2SAndroid Build Coastguard Worker #    define kPREFIX		"AirPrint"
42*5e7646d2SAndroid Build Coastguard Worker #  endif /* TARGET_OS_OSX */
43*5e7646d2SAndroid Build Coastguard Worker #  define kDigestOptionsKey	CFSTR(kPREFIX "DigestOptions")
44*5e7646d2SAndroid Build Coastguard Worker #  define kUserKey		CFSTR(kPREFIX "User")
45*5e7646d2SAndroid Build Coastguard Worker #  define kUserAgentTokensKey	CFSTR(kPREFIX "UserAgentTokens")
46*5e7646d2SAndroid Build Coastguard Worker #  define kAllowAnyRootKey	CFSTR(kPREFIX "AllowAnyRoot")
47*5e7646d2SAndroid Build Coastguard Worker #  define kAllowExpiredCertsKey	CFSTR(kPREFIX "AllowExpiredCerts")
48*5e7646d2SAndroid Build Coastguard Worker #  define kEncryptionKey	CFSTR(kPREFIX "Encryption")
49*5e7646d2SAndroid Build Coastguard Worker #  define kGSSServiceNameKey	CFSTR(kPREFIX "GSSServiceName")
50*5e7646d2SAndroid Build Coastguard Worker #  define kSSLOptionsKey	CFSTR(kPREFIX "SSLOptions")
51*5e7646d2SAndroid Build Coastguard Worker #  define kTrustOnFirstUseKey	CFSTR(kPREFIX "TrustOnFirstUse")
52*5e7646d2SAndroid Build Coastguard Worker #  define kValidateCertsKey	CFSTR(kPREFIX "ValidateCerts")
53*5e7646d2SAndroid Build Coastguard Worker /* Deprecated */
54*5e7646d2SAndroid Build Coastguard Worker #  define kAllowRC4		CFSTR(kPREFIX "AllowRC4")
55*5e7646d2SAndroid Build Coastguard Worker #  define kAllowSSL3		CFSTR(kPREFIX "AllowSSL3")
56*5e7646d2SAndroid Build Coastguard Worker #  define kAllowDH		CFSTR(kPREFIX "AllowDH")
57*5e7646d2SAndroid Build Coastguard Worker #endif /* __APPLE__ */
58*5e7646d2SAndroid Build Coastguard Worker 
59*5e7646d2SAndroid Build Coastguard Worker #define _CUPS_PASSCHAR	'*'		/* Character that is echoed for password */
60*5e7646d2SAndroid Build Coastguard Worker 
61*5e7646d2SAndroid Build Coastguard Worker 
62*5e7646d2SAndroid Build Coastguard Worker /*
63*5e7646d2SAndroid Build Coastguard Worker  * Local types...
64*5e7646d2SAndroid Build Coastguard Worker  */
65*5e7646d2SAndroid Build Coastguard Worker 
66*5e7646d2SAndroid Build Coastguard Worker typedef struct _cups_client_conf_s	/**** client.conf config data ****/
67*5e7646d2SAndroid Build Coastguard Worker {
68*5e7646d2SAndroid Build Coastguard Worker   _cups_digestoptions_t	digestoptions;	/* DigestOptions values */
69*5e7646d2SAndroid Build Coastguard Worker   _cups_uatokens_t	uatokens;	/* UserAgentTokens values */
70*5e7646d2SAndroid Build Coastguard Worker #ifdef HAVE_SSL
71*5e7646d2SAndroid Build Coastguard Worker   int			ssl_options,	/* SSLOptions values */
72*5e7646d2SAndroid Build Coastguard Worker 			ssl_min_version,/* Minimum SSL/TLS version */
73*5e7646d2SAndroid Build Coastguard Worker 			ssl_max_version;/* Maximum SSL/TLS version */
74*5e7646d2SAndroid Build Coastguard Worker #endif /* HAVE_SSL */
75*5e7646d2SAndroid Build Coastguard Worker   int			trust_first,	/* Trust on first use? */
76*5e7646d2SAndroid Build Coastguard Worker 			any_root,	/* Allow any (e.g., self-signed) root */
77*5e7646d2SAndroid Build Coastguard Worker 			expired_certs,	/* Allow expired certs */
78*5e7646d2SAndroid Build Coastguard Worker 			validate_certs;	/* Validate certificates */
79*5e7646d2SAndroid Build Coastguard Worker   http_encryption_t	encryption;	/* Encryption setting */
80*5e7646d2SAndroid Build Coastguard Worker   char			user[65],	/* User name */
81*5e7646d2SAndroid Build Coastguard Worker 			server_name[256];
82*5e7646d2SAndroid Build Coastguard Worker 					/* Server hostname */
83*5e7646d2SAndroid Build Coastguard Worker #ifdef HAVE_GSSAPI
84*5e7646d2SAndroid Build Coastguard Worker   char			gss_service_name[32];
85*5e7646d2SAndroid Build Coastguard Worker   					/* Kerberos service name */
86*5e7646d2SAndroid Build Coastguard Worker #endif /* HAVE_GSSAPI */
87*5e7646d2SAndroid Build Coastguard Worker } _cups_client_conf_t;
88*5e7646d2SAndroid Build Coastguard Worker 
89*5e7646d2SAndroid Build Coastguard Worker 
90*5e7646d2SAndroid Build Coastguard Worker /*
91*5e7646d2SAndroid Build Coastguard Worker  * Local functions...
92*5e7646d2SAndroid Build Coastguard Worker  */
93*5e7646d2SAndroid Build Coastguard Worker 
94*5e7646d2SAndroid Build Coastguard Worker #ifdef __APPLE__
95*5e7646d2SAndroid Build Coastguard Worker static int	cups_apple_get_boolean(CFStringRef key, int *value);
96*5e7646d2SAndroid Build Coastguard Worker static int	cups_apple_get_string(CFStringRef key, char *value, size_t valsize);
97*5e7646d2SAndroid Build Coastguard Worker #endif /* __APPLE__ */
98*5e7646d2SAndroid Build Coastguard Worker static int	cups_boolean_value(const char *value);
99*5e7646d2SAndroid Build Coastguard Worker static void	cups_finalize_client_conf(_cups_client_conf_t *cc);
100*5e7646d2SAndroid Build Coastguard Worker static void	cups_init_client_conf(_cups_client_conf_t *cc);
101*5e7646d2SAndroid Build Coastguard Worker static void	cups_read_client_conf(cups_file_t *fp, _cups_client_conf_t *cc);
102*5e7646d2SAndroid Build Coastguard Worker static void	cups_set_default_ipp_port(_cups_globals_t *cg);
103*5e7646d2SAndroid Build Coastguard Worker static void	cups_set_digestoptions(_cups_client_conf_t *cc, const char *value);
104*5e7646d2SAndroid Build Coastguard Worker static void	cups_set_encryption(_cups_client_conf_t *cc, const char *value);
105*5e7646d2SAndroid Build Coastguard Worker #ifdef HAVE_GSSAPI
106*5e7646d2SAndroid Build Coastguard Worker static void	cups_set_gss_service_name(_cups_client_conf_t *cc, const char *value);
107*5e7646d2SAndroid Build Coastguard Worker #endif /* HAVE_GSSAPI */
108*5e7646d2SAndroid Build Coastguard Worker static void	cups_set_server_name(_cups_client_conf_t *cc, const char *value);
109*5e7646d2SAndroid Build Coastguard Worker #ifdef HAVE_SSL
110*5e7646d2SAndroid Build Coastguard Worker static void	cups_set_ssl_options(_cups_client_conf_t *cc, const char *value);
111*5e7646d2SAndroid Build Coastguard Worker #endif /* HAVE_SSL */
112*5e7646d2SAndroid Build Coastguard Worker static void	cups_set_uatokens(_cups_client_conf_t *cc, const char *value);
113*5e7646d2SAndroid Build Coastguard Worker static void	cups_set_user(_cups_client_conf_t *cc, const char *value);
114*5e7646d2SAndroid Build Coastguard Worker 
115*5e7646d2SAndroid Build Coastguard Worker 
116*5e7646d2SAndroid Build Coastguard Worker /*
117*5e7646d2SAndroid Build Coastguard Worker  * 'cupsEncryption()' - Get the current encryption settings.
118*5e7646d2SAndroid Build Coastguard Worker  *
119*5e7646d2SAndroid Build Coastguard Worker  * The default encryption setting comes from the CUPS_ENCRYPTION
120*5e7646d2SAndroid Build Coastguard Worker  * environment variable, then the ~/.cups/client.conf file, and finally the
121*5e7646d2SAndroid Build Coastguard Worker  * /etc/cups/client.conf file. If not set, the default is
122*5e7646d2SAndroid Build Coastguard Worker  * @code HTTP_ENCRYPTION_IF_REQUESTED@.
123*5e7646d2SAndroid Build Coastguard Worker  *
124*5e7646d2SAndroid Build Coastguard Worker  * Note: The current encryption setting is tracked separately for each thread
125*5e7646d2SAndroid Build Coastguard Worker  * in a program. Multi-threaded programs that override the setting via the
126*5e7646d2SAndroid Build Coastguard Worker  * @link cupsSetEncryption@ function need to do so in each thread for the same
127*5e7646d2SAndroid Build Coastguard Worker  * setting to be used.
128*5e7646d2SAndroid Build Coastguard Worker  */
129*5e7646d2SAndroid Build Coastguard Worker 
130*5e7646d2SAndroid Build Coastguard Worker http_encryption_t			/* O - Encryption settings */
cupsEncryption(void)131*5e7646d2SAndroid Build Coastguard Worker cupsEncryption(void)
132*5e7646d2SAndroid Build Coastguard Worker {
133*5e7646d2SAndroid Build Coastguard Worker   _cups_globals_t *cg = _cupsGlobals();	/* Pointer to library globals */
134*5e7646d2SAndroid Build Coastguard Worker 
135*5e7646d2SAndroid Build Coastguard Worker 
136*5e7646d2SAndroid Build Coastguard Worker   if (cg->encryption == (http_encryption_t)-1)
137*5e7646d2SAndroid Build Coastguard Worker     _cupsSetDefaults();
138*5e7646d2SAndroid Build Coastguard Worker 
139*5e7646d2SAndroid Build Coastguard Worker   return (cg->encryption);
140*5e7646d2SAndroid Build Coastguard Worker }
141*5e7646d2SAndroid Build Coastguard Worker 
142*5e7646d2SAndroid Build Coastguard Worker 
143*5e7646d2SAndroid Build Coastguard Worker /*
144*5e7646d2SAndroid Build Coastguard Worker  * 'cupsGetPassword()' - Get a password from the user.
145*5e7646d2SAndroid Build Coastguard Worker  *
146*5e7646d2SAndroid Build Coastguard Worker  * Uses the current password callback function. Returns @code NULL@ if the
147*5e7646d2SAndroid Build Coastguard Worker  * user does not provide a password.
148*5e7646d2SAndroid Build Coastguard Worker  *
149*5e7646d2SAndroid Build Coastguard Worker  * Note: The current password callback function is tracked separately for each
150*5e7646d2SAndroid Build Coastguard Worker  * thread in a program. Multi-threaded programs that override the setting via
151*5e7646d2SAndroid Build Coastguard Worker  * the @link cupsSetPasswordCB@ or @link cupsSetPasswordCB2@ functions need to
152*5e7646d2SAndroid Build Coastguard Worker  * do so in each thread for the same function to be used.
153*5e7646d2SAndroid Build Coastguard Worker  *
154*5e7646d2SAndroid Build Coastguard Worker  * @exclude all@
155*5e7646d2SAndroid Build Coastguard Worker  */
156*5e7646d2SAndroid Build Coastguard Worker 
157*5e7646d2SAndroid Build Coastguard Worker const char *				/* O - Password */
cupsGetPassword(const char * prompt)158*5e7646d2SAndroid Build Coastguard Worker cupsGetPassword(const char *prompt)	/* I - Prompt string */
159*5e7646d2SAndroid Build Coastguard Worker {
160*5e7646d2SAndroid Build Coastguard Worker   _cups_globals_t *cg = _cupsGlobals();	/* Pointer to library globals */
161*5e7646d2SAndroid Build Coastguard Worker 
162*5e7646d2SAndroid Build Coastguard Worker 
163*5e7646d2SAndroid Build Coastguard Worker   return ((cg->password_cb)(prompt, NULL, NULL, NULL, cg->password_data));
164*5e7646d2SAndroid Build Coastguard Worker }
165*5e7646d2SAndroid Build Coastguard Worker 
166*5e7646d2SAndroid Build Coastguard Worker 
167*5e7646d2SAndroid Build Coastguard Worker /*
168*5e7646d2SAndroid Build Coastguard Worker  * 'cupsGetPassword2()' - Get a password from the user using the current
169*5e7646d2SAndroid Build Coastguard Worker  *                        password callback.
170*5e7646d2SAndroid Build Coastguard Worker  *
171*5e7646d2SAndroid Build Coastguard Worker  * Uses the current password callback function. Returns @code NULL@ if the
172*5e7646d2SAndroid Build Coastguard Worker  * user does not provide a password.
173*5e7646d2SAndroid Build Coastguard Worker  *
174*5e7646d2SAndroid Build Coastguard Worker  * Note: The current password callback function is tracked separately for each
175*5e7646d2SAndroid Build Coastguard Worker  * thread in a program. Multi-threaded programs that override the setting via
176*5e7646d2SAndroid Build Coastguard Worker  * the @link cupsSetPasswordCB2@ function need to do so in each thread for the
177*5e7646d2SAndroid Build Coastguard Worker  * same function to be used.
178*5e7646d2SAndroid Build Coastguard Worker  *
179*5e7646d2SAndroid Build Coastguard Worker  * @since CUPS 1.4/macOS 10.6@
180*5e7646d2SAndroid Build Coastguard Worker  */
181*5e7646d2SAndroid Build Coastguard Worker 
182*5e7646d2SAndroid Build Coastguard Worker const char *				/* O - Password */
cupsGetPassword2(const char * prompt,http_t * http,const char * method,const char * resource)183*5e7646d2SAndroid Build Coastguard Worker cupsGetPassword2(const char *prompt,	/* I - Prompt string */
184*5e7646d2SAndroid Build Coastguard Worker 		 http_t     *http,	/* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
185*5e7646d2SAndroid Build Coastguard Worker 		 const char *method,	/* I - Request method ("GET", "POST", "PUT") */
186*5e7646d2SAndroid Build Coastguard Worker 		 const char *resource)	/* I - Resource path */
187*5e7646d2SAndroid Build Coastguard Worker {
188*5e7646d2SAndroid Build Coastguard Worker   _cups_globals_t *cg = _cupsGlobals();	/* Pointer to library globals */
189*5e7646d2SAndroid Build Coastguard Worker 
190*5e7646d2SAndroid Build Coastguard Worker 
191*5e7646d2SAndroid Build Coastguard Worker   if (!http)
192*5e7646d2SAndroid Build Coastguard Worker     http = _cupsConnect();
193*5e7646d2SAndroid Build Coastguard Worker 
194*5e7646d2SAndroid Build Coastguard Worker   return ((cg->password_cb)(prompt, http, method, resource, cg->password_data));
195*5e7646d2SAndroid Build Coastguard Worker }
196*5e7646d2SAndroid Build Coastguard Worker 
197*5e7646d2SAndroid Build Coastguard Worker 
198*5e7646d2SAndroid Build Coastguard Worker /*
199*5e7646d2SAndroid Build Coastguard Worker  * 'cupsServer()' - Return the hostname/address of the current server.
200*5e7646d2SAndroid Build Coastguard Worker  *
201*5e7646d2SAndroid Build Coastguard Worker  * The default server comes from the CUPS_SERVER environment variable, then the
202*5e7646d2SAndroid Build Coastguard Worker  * ~/.cups/client.conf file, and finally the /etc/cups/client.conf file. If not
203*5e7646d2SAndroid Build Coastguard Worker  * set, the default is the local system - either "localhost" or a domain socket
204*5e7646d2SAndroid Build Coastguard Worker  * path.
205*5e7646d2SAndroid Build Coastguard Worker  *
206*5e7646d2SAndroid Build Coastguard Worker  * The returned value can be a fully-qualified hostname, a numeric IPv4 or IPv6
207*5e7646d2SAndroid Build Coastguard Worker  * address, or a domain socket pathname.
208*5e7646d2SAndroid Build Coastguard Worker  *
209*5e7646d2SAndroid Build Coastguard Worker  * Note: The current server is tracked separately for each thread in a program.
210*5e7646d2SAndroid Build Coastguard Worker  * Multi-threaded programs that override the server via the
211*5e7646d2SAndroid Build Coastguard Worker  * @link cupsSetServer@ function need to do so in each thread for the same
212*5e7646d2SAndroid Build Coastguard Worker  * server to be used.
213*5e7646d2SAndroid Build Coastguard Worker  */
214*5e7646d2SAndroid Build Coastguard Worker 
215*5e7646d2SAndroid Build Coastguard Worker const char *				/* O - Server name */
cupsServer(void)216*5e7646d2SAndroid Build Coastguard Worker cupsServer(void)
217*5e7646d2SAndroid Build Coastguard Worker {
218*5e7646d2SAndroid Build Coastguard Worker   _cups_globals_t *cg = _cupsGlobals();	/* Pointer to library globals */
219*5e7646d2SAndroid Build Coastguard Worker 
220*5e7646d2SAndroid Build Coastguard Worker 
221*5e7646d2SAndroid Build Coastguard Worker   if (!cg->server[0])
222*5e7646d2SAndroid Build Coastguard Worker     _cupsSetDefaults();
223*5e7646d2SAndroid Build Coastguard Worker 
224*5e7646d2SAndroid Build Coastguard Worker   return (cg->server);
225*5e7646d2SAndroid Build Coastguard Worker }
226*5e7646d2SAndroid Build Coastguard Worker 
227*5e7646d2SAndroid Build Coastguard Worker 
228*5e7646d2SAndroid Build Coastguard Worker /*
229*5e7646d2SAndroid Build Coastguard Worker  * 'cupsSetClientCertCB()' - Set the client certificate callback.
230*5e7646d2SAndroid Build Coastguard Worker  *
231*5e7646d2SAndroid Build Coastguard Worker  * Pass @code NULL@ to restore the default callback.
232*5e7646d2SAndroid Build Coastguard Worker  *
233*5e7646d2SAndroid Build Coastguard Worker  * Note: The current certificate callback is tracked separately for each thread
234*5e7646d2SAndroid Build Coastguard Worker  * in a program. Multi-threaded programs that override the callback need to do
235*5e7646d2SAndroid Build Coastguard Worker  * so in each thread for the same callback to be used.
236*5e7646d2SAndroid Build Coastguard Worker  *
237*5e7646d2SAndroid Build Coastguard Worker  * @since CUPS 1.5/macOS 10.7@
238*5e7646d2SAndroid Build Coastguard Worker  */
239*5e7646d2SAndroid Build Coastguard Worker 
240*5e7646d2SAndroid Build Coastguard Worker void
cupsSetClientCertCB(cups_client_cert_cb_t cb,void * user_data)241*5e7646d2SAndroid Build Coastguard Worker cupsSetClientCertCB(
242*5e7646d2SAndroid Build Coastguard Worker     cups_client_cert_cb_t cb,		/* I - Callback function */
243*5e7646d2SAndroid Build Coastguard Worker     void                  *user_data)	/* I - User data pointer */
244*5e7646d2SAndroid Build Coastguard Worker {
245*5e7646d2SAndroid Build Coastguard Worker   _cups_globals_t *cg = _cupsGlobals();	/* Pointer to library globals */
246*5e7646d2SAndroid Build Coastguard Worker 
247*5e7646d2SAndroid Build Coastguard Worker 
248*5e7646d2SAndroid Build Coastguard Worker   cg->client_cert_cb	= cb;
249*5e7646d2SAndroid Build Coastguard Worker   cg->client_cert_data	= user_data;
250*5e7646d2SAndroid Build Coastguard Worker }
251*5e7646d2SAndroid Build Coastguard Worker 
252*5e7646d2SAndroid Build Coastguard Worker 
253*5e7646d2SAndroid Build Coastguard Worker /*
254*5e7646d2SAndroid Build Coastguard Worker  * 'cupsSetCredentials()' - Set the default credentials to be used for SSL/TLS
255*5e7646d2SAndroid Build Coastguard Worker  *			    connections.
256*5e7646d2SAndroid Build Coastguard Worker  *
257*5e7646d2SAndroid Build Coastguard Worker  * Note: The default credentials are tracked separately for each thread in a
258*5e7646d2SAndroid Build Coastguard Worker  * program. Multi-threaded programs that override the setting need to do so in
259*5e7646d2SAndroid Build Coastguard Worker  * each thread for the same setting to be used.
260*5e7646d2SAndroid Build Coastguard Worker  *
261*5e7646d2SAndroid Build Coastguard Worker  * @since CUPS 1.5/macOS 10.7@
262*5e7646d2SAndroid Build Coastguard Worker  */
263*5e7646d2SAndroid Build Coastguard Worker 
264*5e7646d2SAndroid Build Coastguard Worker int					/* O - Status of call (0 = success) */
cupsSetCredentials(cups_array_t * credentials)265*5e7646d2SAndroid Build Coastguard Worker cupsSetCredentials(
266*5e7646d2SAndroid Build Coastguard Worker     cups_array_t *credentials)		/* I - Array of credentials */
267*5e7646d2SAndroid Build Coastguard Worker {
268*5e7646d2SAndroid Build Coastguard Worker   _cups_globals_t *cg = _cupsGlobals();	/* Pointer to library globals */
269*5e7646d2SAndroid Build Coastguard Worker 
270*5e7646d2SAndroid Build Coastguard Worker 
271*5e7646d2SAndroid Build Coastguard Worker   if (cupsArrayCount(credentials) < 1)
272*5e7646d2SAndroid Build Coastguard Worker     return (-1);
273*5e7646d2SAndroid Build Coastguard Worker 
274*5e7646d2SAndroid Build Coastguard Worker #ifdef HAVE_SSL
275*5e7646d2SAndroid Build Coastguard Worker   _httpFreeCredentials(cg->tls_credentials);
276*5e7646d2SAndroid Build Coastguard Worker   cg->tls_credentials = _httpCreateCredentials(credentials);
277*5e7646d2SAndroid Build Coastguard Worker #endif /* HAVE_SSL */
278*5e7646d2SAndroid Build Coastguard Worker 
279*5e7646d2SAndroid Build Coastguard Worker   return (cg->tls_credentials ? 0 : -1);
280*5e7646d2SAndroid Build Coastguard Worker }
281*5e7646d2SAndroid Build Coastguard Worker 
282*5e7646d2SAndroid Build Coastguard Worker 
283*5e7646d2SAndroid Build Coastguard Worker /*
284*5e7646d2SAndroid Build Coastguard Worker  * 'cupsSetEncryption()' - Set the encryption preference.
285*5e7646d2SAndroid Build Coastguard Worker  *
286*5e7646d2SAndroid Build Coastguard Worker  * The default encryption setting comes from the CUPS_ENCRYPTION
287*5e7646d2SAndroid Build Coastguard Worker  * environment variable, then the ~/.cups/client.conf file, and finally the
288*5e7646d2SAndroid Build Coastguard Worker  * /etc/cups/client.conf file. If not set, the default is
289*5e7646d2SAndroid Build Coastguard Worker  * @code HTTP_ENCRYPTION_IF_REQUESTED@.
290*5e7646d2SAndroid Build Coastguard Worker  *
291*5e7646d2SAndroid Build Coastguard Worker  * Note: The current encryption setting is tracked separately for each thread
292*5e7646d2SAndroid Build Coastguard Worker  * in a program. Multi-threaded programs that override the setting need to do
293*5e7646d2SAndroid Build Coastguard Worker  * so in each thread for the same setting to be used.
294*5e7646d2SAndroid Build Coastguard Worker  */
295*5e7646d2SAndroid Build Coastguard Worker 
296*5e7646d2SAndroid Build Coastguard Worker void
cupsSetEncryption(http_encryption_t e)297*5e7646d2SAndroid Build Coastguard Worker cupsSetEncryption(http_encryption_t e)	/* I - New encryption preference */
298*5e7646d2SAndroid Build Coastguard Worker {
299*5e7646d2SAndroid Build Coastguard Worker   _cups_globals_t *cg = _cupsGlobals();	/* Pointer to library globals */
300*5e7646d2SAndroid Build Coastguard Worker 
301*5e7646d2SAndroid Build Coastguard Worker 
302*5e7646d2SAndroid Build Coastguard Worker   cg->encryption = e;
303*5e7646d2SAndroid Build Coastguard Worker 
304*5e7646d2SAndroid Build Coastguard Worker   if (cg->http)
305*5e7646d2SAndroid Build Coastguard Worker     httpEncryption(cg->http, e);
306*5e7646d2SAndroid Build Coastguard Worker }
307*5e7646d2SAndroid Build Coastguard Worker 
308*5e7646d2SAndroid Build Coastguard Worker 
309*5e7646d2SAndroid Build Coastguard Worker /*
310*5e7646d2SAndroid Build Coastguard Worker  * 'cupsSetPasswordCB()' - Set the password callback for CUPS.
311*5e7646d2SAndroid Build Coastguard Worker  *
312*5e7646d2SAndroid Build Coastguard Worker  * Pass @code NULL@ to restore the default (console) password callback, which
313*5e7646d2SAndroid Build Coastguard Worker  * reads the password from the console. Programs should call either this
314*5e7646d2SAndroid Build Coastguard Worker  * function or @link cupsSetPasswordCB2@, as only one callback can be registered
315*5e7646d2SAndroid Build Coastguard Worker  * by a program per thread.
316*5e7646d2SAndroid Build Coastguard Worker  *
317*5e7646d2SAndroid Build Coastguard Worker  * Note: The current password callback is tracked separately for each thread
318*5e7646d2SAndroid Build Coastguard Worker  * in a program. Multi-threaded programs that override the callback need to do
319*5e7646d2SAndroid Build Coastguard Worker  * so in each thread for the same callback to be used.
320*5e7646d2SAndroid Build Coastguard Worker  *
321*5e7646d2SAndroid Build Coastguard Worker  * @exclude all@
322*5e7646d2SAndroid Build Coastguard Worker  */
323*5e7646d2SAndroid Build Coastguard Worker 
324*5e7646d2SAndroid Build Coastguard Worker void
cupsSetPasswordCB(cups_password_cb_t cb)325*5e7646d2SAndroid Build Coastguard Worker cupsSetPasswordCB(cups_password_cb_t cb)/* I - Callback function */
326*5e7646d2SAndroid Build Coastguard Worker {
327*5e7646d2SAndroid Build Coastguard Worker   _cups_globals_t *cg = _cupsGlobals();	/* Pointer to library globals */
328*5e7646d2SAndroid Build Coastguard Worker 
329*5e7646d2SAndroid Build Coastguard Worker 
330*5e7646d2SAndroid Build Coastguard Worker   if (cb == (cups_password_cb_t)0)
331*5e7646d2SAndroid Build Coastguard Worker     cg->password_cb = (cups_password_cb2_t)_cupsGetPassword;
332*5e7646d2SAndroid Build Coastguard Worker   else
333*5e7646d2SAndroid Build Coastguard Worker     cg->password_cb = (cups_password_cb2_t)cb;
334*5e7646d2SAndroid Build Coastguard Worker 
335*5e7646d2SAndroid Build Coastguard Worker   cg->password_data = NULL;
336*5e7646d2SAndroid Build Coastguard Worker }
337*5e7646d2SAndroid Build Coastguard Worker 
338*5e7646d2SAndroid Build Coastguard Worker 
339*5e7646d2SAndroid Build Coastguard Worker /*
340*5e7646d2SAndroid Build Coastguard Worker  * 'cupsSetPasswordCB2()' - Set the advanced password callback for CUPS.
341*5e7646d2SAndroid Build Coastguard Worker  *
342*5e7646d2SAndroid Build Coastguard Worker  * Pass @code NULL@ to restore the default (console) password callback, which
343*5e7646d2SAndroid Build Coastguard Worker  * reads the password from the console. Programs should call either this
344*5e7646d2SAndroid Build Coastguard Worker  * function or @link cupsSetPasswordCB2@, as only one callback can be registered
345*5e7646d2SAndroid Build Coastguard Worker  * by a program per thread.
346*5e7646d2SAndroid Build Coastguard Worker  *
347*5e7646d2SAndroid Build Coastguard Worker  * Note: The current password callback is tracked separately for each thread
348*5e7646d2SAndroid Build Coastguard Worker  * in a program. Multi-threaded programs that override the callback need to do
349*5e7646d2SAndroid Build Coastguard Worker  * so in each thread for the same callback to be used.
350*5e7646d2SAndroid Build Coastguard Worker  *
351*5e7646d2SAndroid Build Coastguard Worker  * @since CUPS 1.4/macOS 10.6@
352*5e7646d2SAndroid Build Coastguard Worker  */
353*5e7646d2SAndroid Build Coastguard Worker 
354*5e7646d2SAndroid Build Coastguard Worker void
cupsSetPasswordCB2(cups_password_cb2_t cb,void * user_data)355*5e7646d2SAndroid Build Coastguard Worker cupsSetPasswordCB2(
356*5e7646d2SAndroid Build Coastguard Worker     cups_password_cb2_t cb,		/* I - Callback function */
357*5e7646d2SAndroid Build Coastguard Worker     void                *user_data)	/* I - User data pointer */
358*5e7646d2SAndroid Build Coastguard Worker {
359*5e7646d2SAndroid Build Coastguard Worker   _cups_globals_t *cg = _cupsGlobals();	/* Pointer to library globals */
360*5e7646d2SAndroid Build Coastguard Worker 
361*5e7646d2SAndroid Build Coastguard Worker 
362*5e7646d2SAndroid Build Coastguard Worker   if (cb == (cups_password_cb2_t)0)
363*5e7646d2SAndroid Build Coastguard Worker     cg->password_cb = (cups_password_cb2_t)_cupsGetPassword;
364*5e7646d2SAndroid Build Coastguard Worker   else
365*5e7646d2SAndroid Build Coastguard Worker     cg->password_cb = cb;
366*5e7646d2SAndroid Build Coastguard Worker 
367*5e7646d2SAndroid Build Coastguard Worker   cg->password_data = user_data;
368*5e7646d2SAndroid Build Coastguard Worker }
369*5e7646d2SAndroid Build Coastguard Worker 
370*5e7646d2SAndroid Build Coastguard Worker 
371*5e7646d2SAndroid Build Coastguard Worker /*
372*5e7646d2SAndroid Build Coastguard Worker  * 'cupsSetServer()' - Set the default server name and port.
373*5e7646d2SAndroid Build Coastguard Worker  *
374*5e7646d2SAndroid Build Coastguard Worker  * The "server" string can be a fully-qualified hostname, a numeric
375*5e7646d2SAndroid Build Coastguard Worker  * IPv4 or IPv6 address, or a domain socket pathname. Hostnames and numeric IP
376*5e7646d2SAndroid Build Coastguard Worker  * addresses can be optionally followed by a colon and port number to override
377*5e7646d2SAndroid Build Coastguard Worker  * the default port 631, e.g. "hostname:8631". Pass @code NULL@ to restore the
378*5e7646d2SAndroid Build Coastguard Worker  * default server name and port.
379*5e7646d2SAndroid Build Coastguard Worker  *
380*5e7646d2SAndroid Build Coastguard Worker  * Note: The current server is tracked separately for each thread in a program.
381*5e7646d2SAndroid Build Coastguard Worker  * Multi-threaded programs that override the server need to do so in each
382*5e7646d2SAndroid Build Coastguard Worker  * thread for the same server to be used.
383*5e7646d2SAndroid Build Coastguard Worker  */
384*5e7646d2SAndroid Build Coastguard Worker 
385*5e7646d2SAndroid Build Coastguard Worker void
cupsSetServer(const char * server)386*5e7646d2SAndroid Build Coastguard Worker cupsSetServer(const char *server)	/* I - Server name */
387*5e7646d2SAndroid Build Coastguard Worker {
388*5e7646d2SAndroid Build Coastguard Worker   char		*options,		/* Options */
389*5e7646d2SAndroid Build Coastguard Worker 		*port;			/* Pointer to port */
390*5e7646d2SAndroid Build Coastguard Worker   _cups_globals_t *cg = _cupsGlobals();	/* Pointer to library globals */
391*5e7646d2SAndroid Build Coastguard Worker 
392*5e7646d2SAndroid Build Coastguard Worker 
393*5e7646d2SAndroid Build Coastguard Worker   if (server)
394*5e7646d2SAndroid Build Coastguard Worker   {
395*5e7646d2SAndroid Build Coastguard Worker     strlcpy(cg->server, server, sizeof(cg->server));
396*5e7646d2SAndroid Build Coastguard Worker 
397*5e7646d2SAndroid Build Coastguard Worker     if (cg->server[0] != '/' && (options = strrchr(cg->server, '/')) != NULL)
398*5e7646d2SAndroid Build Coastguard Worker     {
399*5e7646d2SAndroid Build Coastguard Worker       *options++ = '\0';
400*5e7646d2SAndroid Build Coastguard Worker 
401*5e7646d2SAndroid Build Coastguard Worker       if (!strcmp(options, "version=1.0"))
402*5e7646d2SAndroid Build Coastguard Worker         cg->server_version = 10;
403*5e7646d2SAndroid Build Coastguard Worker       else if (!strcmp(options, "version=1.1"))
404*5e7646d2SAndroid Build Coastguard Worker         cg->server_version = 11;
405*5e7646d2SAndroid Build Coastguard Worker       else if (!strcmp(options, "version=2.0"))
406*5e7646d2SAndroid Build Coastguard Worker         cg->server_version = 20;
407*5e7646d2SAndroid Build Coastguard Worker       else if (!strcmp(options, "version=2.1"))
408*5e7646d2SAndroid Build Coastguard Worker         cg->server_version = 21;
409*5e7646d2SAndroid Build Coastguard Worker       else if (!strcmp(options, "version=2.2"))
410*5e7646d2SAndroid Build Coastguard Worker         cg->server_version = 22;
411*5e7646d2SAndroid Build Coastguard Worker     }
412*5e7646d2SAndroid Build Coastguard Worker     else
413*5e7646d2SAndroid Build Coastguard Worker       cg->server_version = 20;
414*5e7646d2SAndroid Build Coastguard Worker 
415*5e7646d2SAndroid Build Coastguard Worker     if (cg->server[0] != '/' && (port = strrchr(cg->server, ':')) != NULL &&
416*5e7646d2SAndroid Build Coastguard Worker         !strchr(port, ']') && isdigit(port[1] & 255))
417*5e7646d2SAndroid Build Coastguard Worker     {
418*5e7646d2SAndroid Build Coastguard Worker       *port++ = '\0';
419*5e7646d2SAndroid Build Coastguard Worker 
420*5e7646d2SAndroid Build Coastguard Worker       cg->ipp_port = atoi(port);
421*5e7646d2SAndroid Build Coastguard Worker     }
422*5e7646d2SAndroid Build Coastguard Worker 
423*5e7646d2SAndroid Build Coastguard Worker     if (!cg->ipp_port)
424*5e7646d2SAndroid Build Coastguard Worker       cups_set_default_ipp_port(cg);
425*5e7646d2SAndroid Build Coastguard Worker 
426*5e7646d2SAndroid Build Coastguard Worker     if (cg->server[0] == '/')
427*5e7646d2SAndroid Build Coastguard Worker       strlcpy(cg->servername, "localhost", sizeof(cg->servername));
428*5e7646d2SAndroid Build Coastguard Worker     else
429*5e7646d2SAndroid Build Coastguard Worker       strlcpy(cg->servername, cg->server, sizeof(cg->servername));
430*5e7646d2SAndroid Build Coastguard Worker   }
431*5e7646d2SAndroid Build Coastguard Worker   else
432*5e7646d2SAndroid Build Coastguard Worker   {
433*5e7646d2SAndroid Build Coastguard Worker     cg->server[0]      = '\0';
434*5e7646d2SAndroid Build Coastguard Worker     cg->servername[0]  = '\0';
435*5e7646d2SAndroid Build Coastguard Worker     cg->server_version = 20;
436*5e7646d2SAndroid Build Coastguard Worker     cg->ipp_port       = 0;
437*5e7646d2SAndroid Build Coastguard Worker   }
438*5e7646d2SAndroid Build Coastguard Worker 
439*5e7646d2SAndroid Build Coastguard Worker   if (cg->http)
440*5e7646d2SAndroid Build Coastguard Worker   {
441*5e7646d2SAndroid Build Coastguard Worker     httpClose(cg->http);
442*5e7646d2SAndroid Build Coastguard Worker     cg->http = NULL;
443*5e7646d2SAndroid Build Coastguard Worker   }
444*5e7646d2SAndroid Build Coastguard Worker }
445*5e7646d2SAndroid Build Coastguard Worker 
446*5e7646d2SAndroid Build Coastguard Worker 
447*5e7646d2SAndroid Build Coastguard Worker /*
448*5e7646d2SAndroid Build Coastguard Worker  * 'cupsSetServerCertCB()' - Set the server certificate callback.
449*5e7646d2SAndroid Build Coastguard Worker  *
450*5e7646d2SAndroid Build Coastguard Worker  * Pass @code NULL@ to restore the default callback.
451*5e7646d2SAndroid Build Coastguard Worker  *
452*5e7646d2SAndroid Build Coastguard Worker  * Note: The current credentials callback is tracked separately for each thread
453*5e7646d2SAndroid Build Coastguard Worker  * in a program. Multi-threaded programs that override the callback need to do
454*5e7646d2SAndroid Build Coastguard Worker  * so in each thread for the same callback to be used.
455*5e7646d2SAndroid Build Coastguard Worker  *
456*5e7646d2SAndroid Build Coastguard Worker  * @since CUPS 1.5/macOS 10.7@
457*5e7646d2SAndroid Build Coastguard Worker  */
458*5e7646d2SAndroid Build Coastguard Worker 
459*5e7646d2SAndroid Build Coastguard Worker void
cupsSetServerCertCB(cups_server_cert_cb_t cb,void * user_data)460*5e7646d2SAndroid Build Coastguard Worker cupsSetServerCertCB(
461*5e7646d2SAndroid Build Coastguard Worker     cups_server_cert_cb_t cb,		/* I - Callback function */
462*5e7646d2SAndroid Build Coastguard Worker     void		  *user_data)	/* I - User data pointer */
463*5e7646d2SAndroid Build Coastguard Worker {
464*5e7646d2SAndroid Build Coastguard Worker   _cups_globals_t *cg = _cupsGlobals();	/* Pointer to library globals */
465*5e7646d2SAndroid Build Coastguard Worker 
466*5e7646d2SAndroid Build Coastguard Worker 
467*5e7646d2SAndroid Build Coastguard Worker   cg->server_cert_cb	= cb;
468*5e7646d2SAndroid Build Coastguard Worker   cg->server_cert_data	= user_data;
469*5e7646d2SAndroid Build Coastguard Worker }
470*5e7646d2SAndroid Build Coastguard Worker 
471*5e7646d2SAndroid Build Coastguard Worker 
472*5e7646d2SAndroid Build Coastguard Worker /*
473*5e7646d2SAndroid Build Coastguard Worker  * 'cupsSetUser()' - Set the default user name.
474*5e7646d2SAndroid Build Coastguard Worker  *
475*5e7646d2SAndroid Build Coastguard Worker  * Pass @code NULL@ to restore the default user name.
476*5e7646d2SAndroid Build Coastguard Worker  *
477*5e7646d2SAndroid Build Coastguard Worker  * Note: The current user name is tracked separately for each thread in a
478*5e7646d2SAndroid Build Coastguard Worker  * program. Multi-threaded programs that override the user name need to do so
479*5e7646d2SAndroid Build Coastguard Worker  * in each thread for the same user name to be used.
480*5e7646d2SAndroid Build Coastguard Worker  */
481*5e7646d2SAndroid Build Coastguard Worker 
482*5e7646d2SAndroid Build Coastguard Worker void
cupsSetUser(const char * user)483*5e7646d2SAndroid Build Coastguard Worker cupsSetUser(const char *user)		/* I - User name */
484*5e7646d2SAndroid Build Coastguard Worker {
485*5e7646d2SAndroid Build Coastguard Worker   _cups_globals_t *cg = _cupsGlobals();	/* Pointer to library globals */
486*5e7646d2SAndroid Build Coastguard Worker 
487*5e7646d2SAndroid Build Coastguard Worker 
488*5e7646d2SAndroid Build Coastguard Worker   if (user)
489*5e7646d2SAndroid Build Coastguard Worker     strlcpy(cg->user, user, sizeof(cg->user));
490*5e7646d2SAndroid Build Coastguard Worker   else
491*5e7646d2SAndroid Build Coastguard Worker     cg->user[0] = '\0';
492*5e7646d2SAndroid Build Coastguard Worker }
493*5e7646d2SAndroid Build Coastguard Worker 
494*5e7646d2SAndroid Build Coastguard Worker 
495*5e7646d2SAndroid Build Coastguard Worker /*
496*5e7646d2SAndroid Build Coastguard Worker  * 'cupsSetUserAgent()' - Set the default HTTP User-Agent string.
497*5e7646d2SAndroid Build Coastguard Worker  *
498*5e7646d2SAndroid Build Coastguard Worker  * Setting the string to NULL forces the default value containing the CUPS
499*5e7646d2SAndroid Build Coastguard Worker  * version, IPP version, and operating system version and architecture.
500*5e7646d2SAndroid Build Coastguard Worker  *
501*5e7646d2SAndroid Build Coastguard Worker  * @since CUPS 1.7/macOS 10.9@
502*5e7646d2SAndroid Build Coastguard Worker  */
503*5e7646d2SAndroid Build Coastguard Worker 
504*5e7646d2SAndroid Build Coastguard Worker void
cupsSetUserAgent(const char * user_agent)505*5e7646d2SAndroid Build Coastguard Worker cupsSetUserAgent(const char *user_agent)/* I - User-Agent string or @code NULL@ */
506*5e7646d2SAndroid Build Coastguard Worker {
507*5e7646d2SAndroid Build Coastguard Worker   _cups_globals_t	*cg = _cupsGlobals();
508*5e7646d2SAndroid Build Coastguard Worker 					/* Thread globals */
509*5e7646d2SAndroid Build Coastguard Worker #ifdef _WIN32
510*5e7646d2SAndroid Build Coastguard Worker   SYSTEM_INFO		sysinfo;	/* System information */
511*5e7646d2SAndroid Build Coastguard Worker   OSVERSIONINFOA	version;	/* OS version info */
512*5e7646d2SAndroid Build Coastguard Worker   const char		*machine;	/* Hardware/machine name */
513*5e7646d2SAndroid Build Coastguard Worker #elif defined(__APPLE__)
514*5e7646d2SAndroid Build Coastguard Worker   struct utsname	name;		/* uname info */
515*5e7646d2SAndroid Build Coastguard Worker   char			version[256];	/* macOS/iOS version */
516*5e7646d2SAndroid Build Coastguard Worker   size_t		len;		/* Length of value */
517*5e7646d2SAndroid Build Coastguard Worker #else
518*5e7646d2SAndroid Build Coastguard Worker   struct utsname	name;		/* uname info */
519*5e7646d2SAndroid Build Coastguard Worker #endif /* _WIN32 */
520*5e7646d2SAndroid Build Coastguard Worker 
521*5e7646d2SAndroid Build Coastguard Worker 
522*5e7646d2SAndroid Build Coastguard Worker   if (user_agent)
523*5e7646d2SAndroid Build Coastguard Worker   {
524*5e7646d2SAndroid Build Coastguard Worker     strlcpy(cg->user_agent, user_agent, sizeof(cg->user_agent));
525*5e7646d2SAndroid Build Coastguard Worker     return;
526*5e7646d2SAndroid Build Coastguard Worker   }
527*5e7646d2SAndroid Build Coastguard Worker 
528*5e7646d2SAndroid Build Coastguard Worker   if (cg->uatokens < _CUPS_UATOKENS_OS)
529*5e7646d2SAndroid Build Coastguard Worker   {
530*5e7646d2SAndroid Build Coastguard Worker     switch (cg->uatokens)
531*5e7646d2SAndroid Build Coastguard Worker     {
532*5e7646d2SAndroid Build Coastguard Worker       default :
533*5e7646d2SAndroid Build Coastguard Worker       case _CUPS_UATOKENS_NONE :
534*5e7646d2SAndroid Build Coastguard Worker 	  cg->user_agent[0] = '\0';
535*5e7646d2SAndroid Build Coastguard Worker 	  break;
536*5e7646d2SAndroid Build Coastguard Worker       case _CUPS_UATOKENS_PRODUCT_ONLY :
537*5e7646d2SAndroid Build Coastguard Worker 	  strlcpy(cg->user_agent, "CUPS IPP", sizeof(cg->user_agent));
538*5e7646d2SAndroid Build Coastguard Worker 	  break;
539*5e7646d2SAndroid Build Coastguard Worker       case _CUPS_UATOKENS_MAJOR :
540*5e7646d2SAndroid Build Coastguard Worker 	  snprintf(cg->user_agent, sizeof(cg->user_agent), "CUPS/%d IPP/2", CUPS_VERSION_MAJOR);
541*5e7646d2SAndroid Build Coastguard Worker 	  break;
542*5e7646d2SAndroid Build Coastguard Worker       case _CUPS_UATOKENS_MINOR :
543*5e7646d2SAndroid Build Coastguard Worker 	  snprintf(cg->user_agent, sizeof(cg->user_agent), "CUPS/%d.%d IPP/2.1", CUPS_VERSION_MAJOR, CUPS_VERSION_MINOR);
544*5e7646d2SAndroid Build Coastguard Worker 	  break;
545*5e7646d2SAndroid Build Coastguard Worker       case _CUPS_UATOKENS_MINIMAL :
546*5e7646d2SAndroid Build Coastguard Worker 	  strlcpy(cg->user_agent, CUPS_MINIMAL " IPP/2.1", sizeof(cg->user_agent));
547*5e7646d2SAndroid Build Coastguard Worker 	  break;
548*5e7646d2SAndroid Build Coastguard Worker     }
549*5e7646d2SAndroid Build Coastguard Worker   }
550*5e7646d2SAndroid Build Coastguard Worker 
551*5e7646d2SAndroid Build Coastguard Worker #ifdef _WIN32
552*5e7646d2SAndroid Build Coastguard Worker  /*
553*5e7646d2SAndroid Build Coastguard Worker   * Gather Windows version information for the User-Agent string...
554*5e7646d2SAndroid Build Coastguard Worker   */
555*5e7646d2SAndroid Build Coastguard Worker 
556*5e7646d2SAndroid Build Coastguard Worker   version.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
557*5e7646d2SAndroid Build Coastguard Worker   GetVersionExA(&version);
558*5e7646d2SAndroid Build Coastguard Worker   GetNativeSystemInfo(&sysinfo);
559*5e7646d2SAndroid Build Coastguard Worker 
560*5e7646d2SAndroid Build Coastguard Worker   switch (sysinfo.wProcessorArchitecture)
561*5e7646d2SAndroid Build Coastguard Worker   {
562*5e7646d2SAndroid Build Coastguard Worker     case PROCESSOR_ARCHITECTURE_AMD64 :
563*5e7646d2SAndroid Build Coastguard Worker         machine = "amd64";
564*5e7646d2SAndroid Build Coastguard Worker         break;
565*5e7646d2SAndroid Build Coastguard Worker 
566*5e7646d2SAndroid Build Coastguard Worker     case PROCESSOR_ARCHITECTURE_ARM :
567*5e7646d2SAndroid Build Coastguard Worker         machine = "arm";
568*5e7646d2SAndroid Build Coastguard Worker         break;
569*5e7646d2SAndroid Build Coastguard Worker 
570*5e7646d2SAndroid Build Coastguard Worker     case PROCESSOR_ARCHITECTURE_IA64 :
571*5e7646d2SAndroid Build Coastguard Worker         machine = "ia64";
572*5e7646d2SAndroid Build Coastguard Worker         break;
573*5e7646d2SAndroid Build Coastguard Worker 
574*5e7646d2SAndroid Build Coastguard Worker     case PROCESSOR_ARCHITECTURE_INTEL :
575*5e7646d2SAndroid Build Coastguard Worker         machine = "intel";
576*5e7646d2SAndroid Build Coastguard Worker         break;
577*5e7646d2SAndroid Build Coastguard Worker 
578*5e7646d2SAndroid Build Coastguard Worker     default :
579*5e7646d2SAndroid Build Coastguard Worker         machine = "unknown";
580*5e7646d2SAndroid Build Coastguard Worker         break;
581*5e7646d2SAndroid Build Coastguard Worker   }
582*5e7646d2SAndroid Build Coastguard Worker 
583*5e7646d2SAndroid Build Coastguard Worker   if (cg->uatokens == _CUPS_UATOKENS_OS)
584*5e7646d2SAndroid Build Coastguard Worker     snprintf(cg->user_agent, sizeof(cg->user_agent), CUPS_MINIMAL " (Windows %d.%d) IPP/2.0", version.dwMajorVersion, version.dwMinorVersion);
585*5e7646d2SAndroid Build Coastguard Worker   else
586*5e7646d2SAndroid Build Coastguard Worker     snprintf(cg->user_agent, sizeof(cg->user_agent), CUPS_MINIMAL " (Windows %d.%d; %s) IPP/2.0", version.dwMajorVersion, version.dwMinorVersion, machine);
587*5e7646d2SAndroid Build Coastguard Worker 
588*5e7646d2SAndroid Build Coastguard Worker #elif defined(__APPLE__)
589*5e7646d2SAndroid Build Coastguard Worker  /*
590*5e7646d2SAndroid Build Coastguard Worker   * Gather macOS/iOS version information for the User-Agent string...
591*5e7646d2SAndroid Build Coastguard Worker   */
592*5e7646d2SAndroid Build Coastguard Worker 
593*5e7646d2SAndroid Build Coastguard Worker   uname(&name);
594*5e7646d2SAndroid Build Coastguard Worker 
595*5e7646d2SAndroid Build Coastguard Worker   len = sizeof(version) - 1;
596*5e7646d2SAndroid Build Coastguard Worker   if (!sysctlbyname("kern.osproductversion", version, &len, NULL, 0))
597*5e7646d2SAndroid Build Coastguard Worker     version[len] = '\0';
598*5e7646d2SAndroid Build Coastguard Worker   else
599*5e7646d2SAndroid Build Coastguard Worker     strlcpy(version, "unknown", sizeof(version));
600*5e7646d2SAndroid Build Coastguard Worker 
601*5e7646d2SAndroid Build Coastguard Worker #  if TARGET_OS_OSX
602*5e7646d2SAndroid Build Coastguard Worker   if (cg->uatokens == _CUPS_UATOKENS_OS)
603*5e7646d2SAndroid Build Coastguard Worker     snprintf(cg->user_agent, sizeof(cg->user_agent), CUPS_MINIMAL " (macOS %s) IPP/2.0", version);
604*5e7646d2SAndroid Build Coastguard Worker   else
605*5e7646d2SAndroid Build Coastguard Worker     snprintf(cg->user_agent, sizeof(cg->user_agent), CUPS_MINIMAL " (macOS %s; %s) IPP/2.0", version, name.machine);
606*5e7646d2SAndroid Build Coastguard Worker 
607*5e7646d2SAndroid Build Coastguard Worker #  else
608*5e7646d2SAndroid Build Coastguard Worker   if (cg->uatokens == _CUPS_UATOKENS_OS)
609*5e7646d2SAndroid Build Coastguard Worker     snprintf(cg->user_agent, sizeof(cg->user_agent), CUPS_MINIMAL " (iOS %s) IPP/2.0", version);
610*5e7646d2SAndroid Build Coastguard Worker   else
611*5e7646d2SAndroid Build Coastguard Worker     snprintf(cg->user_agent, sizeof(cg->user_agent), CUPS_MINIMAL " (iOS %s; %s) IPP/2.0", version, name.machine);
612*5e7646d2SAndroid Build Coastguard Worker #  endif /* TARGET_OS_OSX */
613*5e7646d2SAndroid Build Coastguard Worker 
614*5e7646d2SAndroid Build Coastguard Worker #else
615*5e7646d2SAndroid Build Coastguard Worker  /*
616*5e7646d2SAndroid Build Coastguard Worker   * Gather generic UNIX version information for the User-Agent string...
617*5e7646d2SAndroid Build Coastguard Worker   */
618*5e7646d2SAndroid Build Coastguard Worker 
619*5e7646d2SAndroid Build Coastguard Worker   uname(&name);
620*5e7646d2SAndroid Build Coastguard Worker 
621*5e7646d2SAndroid Build Coastguard Worker   if (cg->uatokens == _CUPS_UATOKENS_OS)
622*5e7646d2SAndroid Build Coastguard Worker     snprintf(cg->user_agent, sizeof(cg->user_agent), CUPS_MINIMAL " (%s %s) IPP/2.0", name.sysname, name.release);
623*5e7646d2SAndroid Build Coastguard Worker   else
624*5e7646d2SAndroid Build Coastguard Worker     snprintf(cg->user_agent, sizeof(cg->user_agent), CUPS_MINIMAL " (%s %s; %s) IPP/2.0", name.sysname, name.release, name.machine);
625*5e7646d2SAndroid Build Coastguard Worker #endif /* _WIN32 */
626*5e7646d2SAndroid Build Coastguard Worker }
627*5e7646d2SAndroid Build Coastguard Worker 
628*5e7646d2SAndroid Build Coastguard Worker 
629*5e7646d2SAndroid Build Coastguard Worker /*
630*5e7646d2SAndroid Build Coastguard Worker  * 'cupsUser()' - Return the current user's name.
631*5e7646d2SAndroid Build Coastguard Worker  *
632*5e7646d2SAndroid Build Coastguard Worker  * Note: The current user name is tracked separately for each thread in a
633*5e7646d2SAndroid Build Coastguard Worker  * program. Multi-threaded programs that override the user name with the
634*5e7646d2SAndroid Build Coastguard Worker  * @link cupsSetUser@ function need to do so in each thread for the same user
635*5e7646d2SAndroid Build Coastguard Worker  * name to be used.
636*5e7646d2SAndroid Build Coastguard Worker  */
637*5e7646d2SAndroid Build Coastguard Worker 
638*5e7646d2SAndroid Build Coastguard Worker const char *				/* O - User name */
cupsUser(void)639*5e7646d2SAndroid Build Coastguard Worker cupsUser(void)
640*5e7646d2SAndroid Build Coastguard Worker {
641*5e7646d2SAndroid Build Coastguard Worker   _cups_globals_t *cg = _cupsGlobals();	/* Pointer to library globals */
642*5e7646d2SAndroid Build Coastguard Worker 
643*5e7646d2SAndroid Build Coastguard Worker 
644*5e7646d2SAndroid Build Coastguard Worker   if (!cg->user[0])
645*5e7646d2SAndroid Build Coastguard Worker     _cupsSetDefaults();
646*5e7646d2SAndroid Build Coastguard Worker 
647*5e7646d2SAndroid Build Coastguard Worker   return (cg->user);
648*5e7646d2SAndroid Build Coastguard Worker }
649*5e7646d2SAndroid Build Coastguard Worker 
650*5e7646d2SAndroid Build Coastguard Worker 
651*5e7646d2SAndroid Build Coastguard Worker /*
652*5e7646d2SAndroid Build Coastguard Worker  * 'cupsUserAgent()' - Return the default HTTP User-Agent string.
653*5e7646d2SAndroid Build Coastguard Worker  *
654*5e7646d2SAndroid Build Coastguard Worker  * @since CUPS 1.7/macOS 10.9@
655*5e7646d2SAndroid Build Coastguard Worker  */
656*5e7646d2SAndroid Build Coastguard Worker 
657*5e7646d2SAndroid Build Coastguard Worker const char *				/* O - User-Agent string */
cupsUserAgent(void)658*5e7646d2SAndroid Build Coastguard Worker cupsUserAgent(void)
659*5e7646d2SAndroid Build Coastguard Worker {
660*5e7646d2SAndroid Build Coastguard Worker   _cups_globals_t *cg = _cupsGlobals();	/* Thread globals */
661*5e7646d2SAndroid Build Coastguard Worker 
662*5e7646d2SAndroid Build Coastguard Worker 
663*5e7646d2SAndroid Build Coastguard Worker   if (!cg->user_agent[0])
664*5e7646d2SAndroid Build Coastguard Worker     cupsSetUserAgent(NULL);
665*5e7646d2SAndroid Build Coastguard Worker 
666*5e7646d2SAndroid Build Coastguard Worker   return (cg->user_agent);
667*5e7646d2SAndroid Build Coastguard Worker }
668*5e7646d2SAndroid Build Coastguard Worker 
669*5e7646d2SAndroid Build Coastguard Worker 
670*5e7646d2SAndroid Build Coastguard Worker /*
671*5e7646d2SAndroid Build Coastguard Worker  * '_cupsGetPassword()' - Get a password from the user.
672*5e7646d2SAndroid Build Coastguard Worker  */
673*5e7646d2SAndroid Build Coastguard Worker 
674*5e7646d2SAndroid Build Coastguard Worker const char *				/* O - Password or @code NULL@ if none */
_cupsGetPassword(const char * prompt)675*5e7646d2SAndroid Build Coastguard Worker _cupsGetPassword(const char *prompt)	/* I - Prompt string */
676*5e7646d2SAndroid Build Coastguard Worker {
677*5e7646d2SAndroid Build Coastguard Worker #ifdef _WIN32
678*5e7646d2SAndroid Build Coastguard Worker   HANDLE		tty;		/* Console handle */
679*5e7646d2SAndroid Build Coastguard Worker   DWORD			mode;		/* Console mode */
680*5e7646d2SAndroid Build Coastguard Worker   char			passch,		/* Current key press */
681*5e7646d2SAndroid Build Coastguard Worker 			*passptr,	/* Pointer into password string */
682*5e7646d2SAndroid Build Coastguard Worker 			*passend;	/* End of password string */
683*5e7646d2SAndroid Build Coastguard Worker   DWORD			passbytes;	/* Bytes read */
684*5e7646d2SAndroid Build Coastguard Worker   _cups_globals_t	*cg = _cupsGlobals();
685*5e7646d2SAndroid Build Coastguard Worker 					/* Thread globals */
686*5e7646d2SAndroid Build Coastguard Worker 
687*5e7646d2SAndroid Build Coastguard Worker 
688*5e7646d2SAndroid Build Coastguard Worker  /*
689*5e7646d2SAndroid Build Coastguard Worker   * Disable input echo and set raw input...
690*5e7646d2SAndroid Build Coastguard Worker   */
691*5e7646d2SAndroid Build Coastguard Worker 
692*5e7646d2SAndroid Build Coastguard Worker   if ((tty = GetStdHandle(STD_INPUT_HANDLE)) == INVALID_HANDLE_VALUE)
693*5e7646d2SAndroid Build Coastguard Worker     return (NULL);
694*5e7646d2SAndroid Build Coastguard Worker 
695*5e7646d2SAndroid Build Coastguard Worker   if (!GetConsoleMode(tty, &mode))
696*5e7646d2SAndroid Build Coastguard Worker     return (NULL);
697*5e7646d2SAndroid Build Coastguard Worker 
698*5e7646d2SAndroid Build Coastguard Worker   if (!SetConsoleMode(tty, 0))
699*5e7646d2SAndroid Build Coastguard Worker     return (NULL);
700*5e7646d2SAndroid Build Coastguard Worker 
701*5e7646d2SAndroid Build Coastguard Worker  /*
702*5e7646d2SAndroid Build Coastguard Worker   * Display the prompt...
703*5e7646d2SAndroid Build Coastguard Worker   */
704*5e7646d2SAndroid Build Coastguard Worker 
705*5e7646d2SAndroid Build Coastguard Worker   printf("%s ", prompt);
706*5e7646d2SAndroid Build Coastguard Worker   fflush(stdout);
707*5e7646d2SAndroid Build Coastguard Worker 
708*5e7646d2SAndroid Build Coastguard Worker  /*
709*5e7646d2SAndroid Build Coastguard Worker   * Read the password string from /dev/tty until we get interrupted or get a
710*5e7646d2SAndroid Build Coastguard Worker   * carriage return or newline...
711*5e7646d2SAndroid Build Coastguard Worker   */
712*5e7646d2SAndroid Build Coastguard Worker 
713*5e7646d2SAndroid Build Coastguard Worker   passptr = cg->password;
714*5e7646d2SAndroid Build Coastguard Worker   passend = cg->password + sizeof(cg->password) - 1;
715*5e7646d2SAndroid Build Coastguard Worker 
716*5e7646d2SAndroid Build Coastguard Worker   while (ReadFile(tty, &passch, 1, &passbytes, NULL))
717*5e7646d2SAndroid Build Coastguard Worker   {
718*5e7646d2SAndroid Build Coastguard Worker     if (passch == 0x0A || passch == 0x0D)
719*5e7646d2SAndroid Build Coastguard Worker     {
720*5e7646d2SAndroid Build Coastguard Worker      /*
721*5e7646d2SAndroid Build Coastguard Worker       * Enter/return...
722*5e7646d2SAndroid Build Coastguard Worker       */
723*5e7646d2SAndroid Build Coastguard Worker 
724*5e7646d2SAndroid Build Coastguard Worker       break;
725*5e7646d2SAndroid Build Coastguard Worker     }
726*5e7646d2SAndroid Build Coastguard Worker     else if (passch == 0x08 || passch == 0x7F)
727*5e7646d2SAndroid Build Coastguard Worker     {
728*5e7646d2SAndroid Build Coastguard Worker      /*
729*5e7646d2SAndroid Build Coastguard Worker       * Backspace/delete (erase character)...
730*5e7646d2SAndroid Build Coastguard Worker       */
731*5e7646d2SAndroid Build Coastguard Worker 
732*5e7646d2SAndroid Build Coastguard Worker       if (passptr > cg->password)
733*5e7646d2SAndroid Build Coastguard Worker       {
734*5e7646d2SAndroid Build Coastguard Worker         passptr --;
735*5e7646d2SAndroid Build Coastguard Worker         fputs("\010 \010", stdout);
736*5e7646d2SAndroid Build Coastguard Worker       }
737*5e7646d2SAndroid Build Coastguard Worker       else
738*5e7646d2SAndroid Build Coastguard Worker         putchar(0x07);
739*5e7646d2SAndroid Build Coastguard Worker     }
740*5e7646d2SAndroid Build Coastguard Worker     else if (passch == 0x15)
741*5e7646d2SAndroid Build Coastguard Worker     {
742*5e7646d2SAndroid Build Coastguard Worker      /*
743*5e7646d2SAndroid Build Coastguard Worker       * CTRL+U (erase line)
744*5e7646d2SAndroid Build Coastguard Worker       */
745*5e7646d2SAndroid Build Coastguard Worker 
746*5e7646d2SAndroid Build Coastguard Worker       if (passptr > cg->password)
747*5e7646d2SAndroid Build Coastguard Worker       {
748*5e7646d2SAndroid Build Coastguard Worker 	while (passptr > cg->password)
749*5e7646d2SAndroid Build Coastguard Worker 	{
750*5e7646d2SAndroid Build Coastguard Worker           passptr --;
751*5e7646d2SAndroid Build Coastguard Worker           fputs("\010 \010", stdout);
752*5e7646d2SAndroid Build Coastguard Worker         }
753*5e7646d2SAndroid Build Coastguard Worker       }
754*5e7646d2SAndroid Build Coastguard Worker       else
755*5e7646d2SAndroid Build Coastguard Worker         putchar(0x07);
756*5e7646d2SAndroid Build Coastguard Worker     }
757*5e7646d2SAndroid Build Coastguard Worker     else if (passch == 0x03)
758*5e7646d2SAndroid Build Coastguard Worker     {
759*5e7646d2SAndroid Build Coastguard Worker      /*
760*5e7646d2SAndroid Build Coastguard Worker       * CTRL+C...
761*5e7646d2SAndroid Build Coastguard Worker       */
762*5e7646d2SAndroid Build Coastguard Worker 
763*5e7646d2SAndroid Build Coastguard Worker       passptr = cg->password;
764*5e7646d2SAndroid Build Coastguard Worker       break;
765*5e7646d2SAndroid Build Coastguard Worker     }
766*5e7646d2SAndroid Build Coastguard Worker     else if ((passch & 255) < 0x20 || passptr >= passend)
767*5e7646d2SAndroid Build Coastguard Worker       putchar(0x07);
768*5e7646d2SAndroid Build Coastguard Worker     else
769*5e7646d2SAndroid Build Coastguard Worker     {
770*5e7646d2SAndroid Build Coastguard Worker       *passptr++ = passch;
771*5e7646d2SAndroid Build Coastguard Worker       putchar(_CUPS_PASSCHAR);
772*5e7646d2SAndroid Build Coastguard Worker     }
773*5e7646d2SAndroid Build Coastguard Worker 
774*5e7646d2SAndroid Build Coastguard Worker     fflush(stdout);
775*5e7646d2SAndroid Build Coastguard Worker   }
776*5e7646d2SAndroid Build Coastguard Worker 
777*5e7646d2SAndroid Build Coastguard Worker   putchar('\n');
778*5e7646d2SAndroid Build Coastguard Worker   fflush(stdout);
779*5e7646d2SAndroid Build Coastguard Worker 
780*5e7646d2SAndroid Build Coastguard Worker  /*
781*5e7646d2SAndroid Build Coastguard Worker   * Cleanup...
782*5e7646d2SAndroid Build Coastguard Worker   */
783*5e7646d2SAndroid Build Coastguard Worker 
784*5e7646d2SAndroid Build Coastguard Worker   SetConsoleMode(tty, mode);
785*5e7646d2SAndroid Build Coastguard Worker 
786*5e7646d2SAndroid Build Coastguard Worker  /*
787*5e7646d2SAndroid Build Coastguard Worker   * Return the proper value...
788*5e7646d2SAndroid Build Coastguard Worker   */
789*5e7646d2SAndroid Build Coastguard Worker 
790*5e7646d2SAndroid Build Coastguard Worker   if (passbytes == 1 && passptr > cg->password)
791*5e7646d2SAndroid Build Coastguard Worker   {
792*5e7646d2SAndroid Build Coastguard Worker     *passptr = '\0';
793*5e7646d2SAndroid Build Coastguard Worker     return (cg->password);
794*5e7646d2SAndroid Build Coastguard Worker   }
795*5e7646d2SAndroid Build Coastguard Worker   else
796*5e7646d2SAndroid Build Coastguard Worker   {
797*5e7646d2SAndroid Build Coastguard Worker     memset(cg->password, 0, sizeof(cg->password));
798*5e7646d2SAndroid Build Coastguard Worker     return (NULL);
799*5e7646d2SAndroid Build Coastguard Worker   }
800*5e7646d2SAndroid Build Coastguard Worker 
801*5e7646d2SAndroid Build Coastguard Worker #else
802*5e7646d2SAndroid Build Coastguard Worker   int			tty;		/* /dev/tty - never read from stdin */
803*5e7646d2SAndroid Build Coastguard Worker   struct termios	original,	/* Original input mode */
804*5e7646d2SAndroid Build Coastguard Worker 			noecho;		/* No echo input mode */
805*5e7646d2SAndroid Build Coastguard Worker   char			passch,		/* Current key press */
806*5e7646d2SAndroid Build Coastguard Worker 			*passptr,	/* Pointer into password string */
807*5e7646d2SAndroid Build Coastguard Worker 			*passend;	/* End of password string */
808*5e7646d2SAndroid Build Coastguard Worker   ssize_t		passbytes;	/* Bytes read */
809*5e7646d2SAndroid Build Coastguard Worker   _cups_globals_t	*cg = _cupsGlobals();
810*5e7646d2SAndroid Build Coastguard Worker 					/* Thread globals */
811*5e7646d2SAndroid Build Coastguard Worker 
812*5e7646d2SAndroid Build Coastguard Worker 
813*5e7646d2SAndroid Build Coastguard Worker  /*
814*5e7646d2SAndroid Build Coastguard Worker   * Disable input echo and set raw input...
815*5e7646d2SAndroid Build Coastguard Worker   */
816*5e7646d2SAndroid Build Coastguard Worker 
817*5e7646d2SAndroid Build Coastguard Worker   if ((tty = open("/dev/tty", O_RDONLY)) < 0)
818*5e7646d2SAndroid Build Coastguard Worker     return (NULL);
819*5e7646d2SAndroid Build Coastguard Worker 
820*5e7646d2SAndroid Build Coastguard Worker   if (tcgetattr(tty, &original))
821*5e7646d2SAndroid Build Coastguard Worker   {
822*5e7646d2SAndroid Build Coastguard Worker     close(tty);
823*5e7646d2SAndroid Build Coastguard Worker     return (NULL);
824*5e7646d2SAndroid Build Coastguard Worker   }
825*5e7646d2SAndroid Build Coastguard Worker 
826*5e7646d2SAndroid Build Coastguard Worker   noecho = original;
827*5e7646d2SAndroid Build Coastguard Worker   noecho.c_lflag &= (tcflag_t)~(ICANON | ECHO | ECHOE | ISIG);
828*5e7646d2SAndroid Build Coastguard Worker   noecho.c_cc[VMIN]  = 1;
829*5e7646d2SAndroid Build Coastguard Worker   noecho.c_cc[VTIME] = 0;
830*5e7646d2SAndroid Build Coastguard Worker 
831*5e7646d2SAndroid Build Coastguard Worker   if (tcsetattr(tty, TCSAFLUSH, &noecho))
832*5e7646d2SAndroid Build Coastguard Worker   {
833*5e7646d2SAndroid Build Coastguard Worker     close(tty);
834*5e7646d2SAndroid Build Coastguard Worker     return (NULL);
835*5e7646d2SAndroid Build Coastguard Worker   }
836*5e7646d2SAndroid Build Coastguard Worker 
837*5e7646d2SAndroid Build Coastguard Worker  /*
838*5e7646d2SAndroid Build Coastguard Worker   * Display the prompt...
839*5e7646d2SAndroid Build Coastguard Worker   */
840*5e7646d2SAndroid Build Coastguard Worker 
841*5e7646d2SAndroid Build Coastguard Worker   printf("%s ", prompt);
842*5e7646d2SAndroid Build Coastguard Worker   fflush(stdout);
843*5e7646d2SAndroid Build Coastguard Worker 
844*5e7646d2SAndroid Build Coastguard Worker  /*
845*5e7646d2SAndroid Build Coastguard Worker   * Read the password string from /dev/tty until we get interrupted or get a
846*5e7646d2SAndroid Build Coastguard Worker   * carriage return or newline...
847*5e7646d2SAndroid Build Coastguard Worker   */
848*5e7646d2SAndroid Build Coastguard Worker 
849*5e7646d2SAndroid Build Coastguard Worker   passptr = cg->password;
850*5e7646d2SAndroid Build Coastguard Worker   passend = cg->password + sizeof(cg->password) - 1;
851*5e7646d2SAndroid Build Coastguard Worker 
852*5e7646d2SAndroid Build Coastguard Worker   while ((passbytes = read(tty, &passch, 1)) == 1)
853*5e7646d2SAndroid Build Coastguard Worker   {
854*5e7646d2SAndroid Build Coastguard Worker     if (passch == noecho.c_cc[VEOL] ||
855*5e7646d2SAndroid Build Coastguard Worker #  ifdef VEOL2
856*5e7646d2SAndroid Build Coastguard Worker         passch == noecho.c_cc[VEOL2] ||
857*5e7646d2SAndroid Build Coastguard Worker #  endif /* VEOL2 */
858*5e7646d2SAndroid Build Coastguard Worker         passch == 0x0A || passch == 0x0D)
859*5e7646d2SAndroid Build Coastguard Worker     {
860*5e7646d2SAndroid Build Coastguard Worker      /*
861*5e7646d2SAndroid Build Coastguard Worker       * Enter/return...
862*5e7646d2SAndroid Build Coastguard Worker       */
863*5e7646d2SAndroid Build Coastguard Worker 
864*5e7646d2SAndroid Build Coastguard Worker       break;
865*5e7646d2SAndroid Build Coastguard Worker     }
866*5e7646d2SAndroid Build Coastguard Worker     else if (passch == noecho.c_cc[VERASE] ||
867*5e7646d2SAndroid Build Coastguard Worker              passch == 0x08 || passch == 0x7F)
868*5e7646d2SAndroid Build Coastguard Worker     {
869*5e7646d2SAndroid Build Coastguard Worker      /*
870*5e7646d2SAndroid Build Coastguard Worker       * Backspace/delete (erase character)...
871*5e7646d2SAndroid Build Coastguard Worker       */
872*5e7646d2SAndroid Build Coastguard Worker 
873*5e7646d2SAndroid Build Coastguard Worker       if (passptr > cg->password)
874*5e7646d2SAndroid Build Coastguard Worker       {
875*5e7646d2SAndroid Build Coastguard Worker         passptr --;
876*5e7646d2SAndroid Build Coastguard Worker         fputs("\010 \010", stdout);
877*5e7646d2SAndroid Build Coastguard Worker       }
878*5e7646d2SAndroid Build Coastguard Worker       else
879*5e7646d2SAndroid Build Coastguard Worker         putchar(0x07);
880*5e7646d2SAndroid Build Coastguard Worker     }
881*5e7646d2SAndroid Build Coastguard Worker     else if (passch == noecho.c_cc[VKILL])
882*5e7646d2SAndroid Build Coastguard Worker     {
883*5e7646d2SAndroid Build Coastguard Worker      /*
884*5e7646d2SAndroid Build Coastguard Worker       * CTRL+U (erase line)
885*5e7646d2SAndroid Build Coastguard Worker       */
886*5e7646d2SAndroid Build Coastguard Worker 
887*5e7646d2SAndroid Build Coastguard Worker       if (passptr > cg->password)
888*5e7646d2SAndroid Build Coastguard Worker       {
889*5e7646d2SAndroid Build Coastguard Worker 	while (passptr > cg->password)
890*5e7646d2SAndroid Build Coastguard Worker 	{
891*5e7646d2SAndroid Build Coastguard Worker           passptr --;
892*5e7646d2SAndroid Build Coastguard Worker           fputs("\010 \010", stdout);
893*5e7646d2SAndroid Build Coastguard Worker         }
894*5e7646d2SAndroid Build Coastguard Worker       }
895*5e7646d2SAndroid Build Coastguard Worker       else
896*5e7646d2SAndroid Build Coastguard Worker         putchar(0x07);
897*5e7646d2SAndroid Build Coastguard Worker     }
898*5e7646d2SAndroid Build Coastguard Worker     else if (passch == noecho.c_cc[VINTR] || passch == noecho.c_cc[VQUIT] ||
899*5e7646d2SAndroid Build Coastguard Worker              passch == noecho.c_cc[VEOF])
900*5e7646d2SAndroid Build Coastguard Worker     {
901*5e7646d2SAndroid Build Coastguard Worker      /*
902*5e7646d2SAndroid Build Coastguard Worker       * CTRL+C, CTRL+D, or CTRL+Z...
903*5e7646d2SAndroid Build Coastguard Worker       */
904*5e7646d2SAndroid Build Coastguard Worker 
905*5e7646d2SAndroid Build Coastguard Worker       passptr = cg->password;
906*5e7646d2SAndroid Build Coastguard Worker       break;
907*5e7646d2SAndroid Build Coastguard Worker     }
908*5e7646d2SAndroid Build Coastguard Worker     else if ((passch & 255) < 0x20 || passptr >= passend)
909*5e7646d2SAndroid Build Coastguard Worker       putchar(0x07);
910*5e7646d2SAndroid Build Coastguard Worker     else
911*5e7646d2SAndroid Build Coastguard Worker     {
912*5e7646d2SAndroid Build Coastguard Worker       *passptr++ = passch;
913*5e7646d2SAndroid Build Coastguard Worker       putchar(_CUPS_PASSCHAR);
914*5e7646d2SAndroid Build Coastguard Worker     }
915*5e7646d2SAndroid Build Coastguard Worker 
916*5e7646d2SAndroid Build Coastguard Worker     fflush(stdout);
917*5e7646d2SAndroid Build Coastguard Worker   }
918*5e7646d2SAndroid Build Coastguard Worker 
919*5e7646d2SAndroid Build Coastguard Worker   putchar('\n');
920*5e7646d2SAndroid Build Coastguard Worker   fflush(stdout);
921*5e7646d2SAndroid Build Coastguard Worker 
922*5e7646d2SAndroid Build Coastguard Worker  /*
923*5e7646d2SAndroid Build Coastguard Worker   * Cleanup...
924*5e7646d2SAndroid Build Coastguard Worker   */
925*5e7646d2SAndroid Build Coastguard Worker 
926*5e7646d2SAndroid Build Coastguard Worker   tcsetattr(tty, TCSAFLUSH, &original);
927*5e7646d2SAndroid Build Coastguard Worker   close(tty);
928*5e7646d2SAndroid Build Coastguard Worker 
929*5e7646d2SAndroid Build Coastguard Worker  /*
930*5e7646d2SAndroid Build Coastguard Worker   * Return the proper value...
931*5e7646d2SAndroid Build Coastguard Worker   */
932*5e7646d2SAndroid Build Coastguard Worker 
933*5e7646d2SAndroid Build Coastguard Worker   if (passbytes == 1 && passptr > cg->password)
934*5e7646d2SAndroid Build Coastguard Worker   {
935*5e7646d2SAndroid Build Coastguard Worker     *passptr = '\0';
936*5e7646d2SAndroid Build Coastguard Worker     return (cg->password);
937*5e7646d2SAndroid Build Coastguard Worker   }
938*5e7646d2SAndroid Build Coastguard Worker   else
939*5e7646d2SAndroid Build Coastguard Worker   {
940*5e7646d2SAndroid Build Coastguard Worker     memset(cg->password, 0, sizeof(cg->password));
941*5e7646d2SAndroid Build Coastguard Worker     return (NULL);
942*5e7646d2SAndroid Build Coastguard Worker   }
943*5e7646d2SAndroid Build Coastguard Worker #endif /* _WIN32 */
944*5e7646d2SAndroid Build Coastguard Worker }
945*5e7646d2SAndroid Build Coastguard Worker 
946*5e7646d2SAndroid Build Coastguard Worker 
947*5e7646d2SAndroid Build Coastguard Worker #ifdef HAVE_GSSAPI
948*5e7646d2SAndroid Build Coastguard Worker /*
949*5e7646d2SAndroid Build Coastguard Worker  * '_cupsGSSServiceName()' - Get the GSS (Kerberos) service name.
950*5e7646d2SAndroid Build Coastguard Worker  */
951*5e7646d2SAndroid Build Coastguard Worker 
952*5e7646d2SAndroid Build Coastguard Worker const char *
_cupsGSSServiceName(void)953*5e7646d2SAndroid Build Coastguard Worker _cupsGSSServiceName(void)
954*5e7646d2SAndroid Build Coastguard Worker {
955*5e7646d2SAndroid Build Coastguard Worker   _cups_globals_t *cg = _cupsGlobals();	/* Thread globals */
956*5e7646d2SAndroid Build Coastguard Worker 
957*5e7646d2SAndroid Build Coastguard Worker 
958*5e7646d2SAndroid Build Coastguard Worker   if (!cg->gss_service_name[0])
959*5e7646d2SAndroid Build Coastguard Worker     _cupsSetDefaults();
960*5e7646d2SAndroid Build Coastguard Worker 
961*5e7646d2SAndroid Build Coastguard Worker   return (cg->gss_service_name);
962*5e7646d2SAndroid Build Coastguard Worker }
963*5e7646d2SAndroid Build Coastguard Worker #endif /* HAVE_GSSAPI */
964*5e7646d2SAndroid Build Coastguard Worker 
965*5e7646d2SAndroid Build Coastguard Worker 
966*5e7646d2SAndroid Build Coastguard Worker /*
967*5e7646d2SAndroid Build Coastguard Worker  * '_cupsSetDefaults()' - Set the default server, port, and encryption.
968*5e7646d2SAndroid Build Coastguard Worker  */
969*5e7646d2SAndroid Build Coastguard Worker 
970*5e7646d2SAndroid Build Coastguard Worker void
_cupsSetDefaults(void)971*5e7646d2SAndroid Build Coastguard Worker _cupsSetDefaults(void)
972*5e7646d2SAndroid Build Coastguard Worker {
973*5e7646d2SAndroid Build Coastguard Worker   cups_file_t	*fp;			/* File */
974*5e7646d2SAndroid Build Coastguard Worker   char		filename[1024];		/* Filename */
975*5e7646d2SAndroid Build Coastguard Worker   _cups_client_conf_t cc;		/* client.conf values */
976*5e7646d2SAndroid Build Coastguard Worker   _cups_globals_t *cg = _cupsGlobals();	/* Pointer to library globals */
977*5e7646d2SAndroid Build Coastguard Worker 
978*5e7646d2SAndroid Build Coastguard Worker 
979*5e7646d2SAndroid Build Coastguard Worker   DEBUG_puts("_cupsSetDefaults()");
980*5e7646d2SAndroid Build Coastguard Worker 
981*5e7646d2SAndroid Build Coastguard Worker  /*
982*5e7646d2SAndroid Build Coastguard Worker   * Load initial client.conf values...
983*5e7646d2SAndroid Build Coastguard Worker   */
984*5e7646d2SAndroid Build Coastguard Worker 
985*5e7646d2SAndroid Build Coastguard Worker   cups_init_client_conf(&cc);
986*5e7646d2SAndroid Build Coastguard Worker 
987*5e7646d2SAndroid Build Coastguard Worker  /*
988*5e7646d2SAndroid Build Coastguard Worker   * Read the /etc/cups/client.conf and ~/.cups/client.conf files, if
989*5e7646d2SAndroid Build Coastguard Worker   * present.
990*5e7646d2SAndroid Build Coastguard Worker   */
991*5e7646d2SAndroid Build Coastguard Worker 
992*5e7646d2SAndroid Build Coastguard Worker   snprintf(filename, sizeof(filename), "%s/client.conf", cg->cups_serverroot);
993*5e7646d2SAndroid Build Coastguard Worker   if ((fp = cupsFileOpen(filename, "r")) != NULL)
994*5e7646d2SAndroid Build Coastguard Worker   {
995*5e7646d2SAndroid Build Coastguard Worker     cups_read_client_conf(fp, &cc);
996*5e7646d2SAndroid Build Coastguard Worker     cupsFileClose(fp);
997*5e7646d2SAndroid Build Coastguard Worker   }
998*5e7646d2SAndroid Build Coastguard Worker 
999*5e7646d2SAndroid Build Coastguard Worker   if (cg->home)
1000*5e7646d2SAndroid Build Coastguard Worker   {
1001*5e7646d2SAndroid Build Coastguard Worker    /*
1002*5e7646d2SAndroid Build Coastguard Worker     * Look for ~/.cups/client.conf...
1003*5e7646d2SAndroid Build Coastguard Worker     */
1004*5e7646d2SAndroid Build Coastguard Worker 
1005*5e7646d2SAndroid Build Coastguard Worker     snprintf(filename, sizeof(filename), "%s/.cups/client.conf", cg->home);
1006*5e7646d2SAndroid Build Coastguard Worker     if ((fp = cupsFileOpen(filename, "r")) != NULL)
1007*5e7646d2SAndroid Build Coastguard Worker     {
1008*5e7646d2SAndroid Build Coastguard Worker       cups_read_client_conf(fp, &cc);
1009*5e7646d2SAndroid Build Coastguard Worker       cupsFileClose(fp);
1010*5e7646d2SAndroid Build Coastguard Worker     }
1011*5e7646d2SAndroid Build Coastguard Worker   }
1012*5e7646d2SAndroid Build Coastguard Worker 
1013*5e7646d2SAndroid Build Coastguard Worker  /*
1014*5e7646d2SAndroid Build Coastguard Worker   * Finalize things so every client.conf value is set...
1015*5e7646d2SAndroid Build Coastguard Worker   */
1016*5e7646d2SAndroid Build Coastguard Worker 
1017*5e7646d2SAndroid Build Coastguard Worker   cups_finalize_client_conf(&cc);
1018*5e7646d2SAndroid Build Coastguard Worker 
1019*5e7646d2SAndroid Build Coastguard Worker   cg->uatokens = cc.uatokens;
1020*5e7646d2SAndroid Build Coastguard Worker 
1021*5e7646d2SAndroid Build Coastguard Worker   if (cg->encryption == (http_encryption_t)-1)
1022*5e7646d2SAndroid Build Coastguard Worker     cg->encryption = cc.encryption;
1023*5e7646d2SAndroid Build Coastguard Worker 
1024*5e7646d2SAndroid Build Coastguard Worker   if (!cg->server[0] || !cg->ipp_port)
1025*5e7646d2SAndroid Build Coastguard Worker     cupsSetServer(cc.server_name);
1026*5e7646d2SAndroid Build Coastguard Worker 
1027*5e7646d2SAndroid Build Coastguard Worker   if (!cg->ipp_port)
1028*5e7646d2SAndroid Build Coastguard Worker     cups_set_default_ipp_port(cg);
1029*5e7646d2SAndroid Build Coastguard Worker 
1030*5e7646d2SAndroid Build Coastguard Worker   if (!cg->user[0])
1031*5e7646d2SAndroid Build Coastguard Worker     strlcpy(cg->user, cc.user, sizeof(cg->user));
1032*5e7646d2SAndroid Build Coastguard Worker 
1033*5e7646d2SAndroid Build Coastguard Worker #ifdef HAVE_GSSAPI
1034*5e7646d2SAndroid Build Coastguard Worker   if (!cg->gss_service_name[0])
1035*5e7646d2SAndroid Build Coastguard Worker     strlcpy(cg->gss_service_name, cc.gss_service_name, sizeof(cg->gss_service_name));
1036*5e7646d2SAndroid Build Coastguard Worker #endif /* HAVE_GSSAPI */
1037*5e7646d2SAndroid Build Coastguard Worker 
1038*5e7646d2SAndroid Build Coastguard Worker   if (cg->trust_first < 0)
1039*5e7646d2SAndroid Build Coastguard Worker     cg->trust_first = cc.trust_first;
1040*5e7646d2SAndroid Build Coastguard Worker 
1041*5e7646d2SAndroid Build Coastguard Worker   if (cg->any_root < 0)
1042*5e7646d2SAndroid Build Coastguard Worker     cg->any_root = cc.any_root;
1043*5e7646d2SAndroid Build Coastguard Worker 
1044*5e7646d2SAndroid Build Coastguard Worker   if (cg->expired_certs < 0)
1045*5e7646d2SAndroid Build Coastguard Worker     cg->expired_certs = cc.expired_certs;
1046*5e7646d2SAndroid Build Coastguard Worker 
1047*5e7646d2SAndroid Build Coastguard Worker   if (cg->validate_certs < 0)
1048*5e7646d2SAndroid Build Coastguard Worker     cg->validate_certs = cc.validate_certs;
1049*5e7646d2SAndroid Build Coastguard Worker 
1050*5e7646d2SAndroid Build Coastguard Worker #ifdef HAVE_SSL
1051*5e7646d2SAndroid Build Coastguard Worker   _httpTLSSetOptions(cc.ssl_options | _HTTP_TLS_SET_DEFAULT, cc.ssl_min_version, cc.ssl_max_version);
1052*5e7646d2SAndroid Build Coastguard Worker #endif /* HAVE_SSL */
1053*5e7646d2SAndroid Build Coastguard Worker }
1054*5e7646d2SAndroid Build Coastguard Worker 
1055*5e7646d2SAndroid Build Coastguard Worker 
1056*5e7646d2SAndroid Build Coastguard Worker #ifdef __APPLE__
1057*5e7646d2SAndroid Build Coastguard Worker /*
1058*5e7646d2SAndroid Build Coastguard Worker  * 'cups_apple_get_boolean()' - Get a boolean setting from the CUPS preferences.
1059*5e7646d2SAndroid Build Coastguard Worker  */
1060*5e7646d2SAndroid Build Coastguard Worker 
1061*5e7646d2SAndroid Build Coastguard Worker static int				/* O - 1 if set, 0 otherwise */
cups_apple_get_boolean(CFStringRef key,int * value)1062*5e7646d2SAndroid Build Coastguard Worker cups_apple_get_boolean(
1063*5e7646d2SAndroid Build Coastguard Worker     CFStringRef key,			/* I - Key (name) */
1064*5e7646d2SAndroid Build Coastguard Worker     int         *value)			/* O - Boolean value */
1065*5e7646d2SAndroid Build Coastguard Worker {
1066*5e7646d2SAndroid Build Coastguard Worker   Boolean	bval,			/* Preference value */
1067*5e7646d2SAndroid Build Coastguard Worker 		bval_set;		/* Value is set? */
1068*5e7646d2SAndroid Build Coastguard Worker 
1069*5e7646d2SAndroid Build Coastguard Worker 
1070*5e7646d2SAndroid Build Coastguard Worker   bval = CFPreferencesGetAppBooleanValue(key, kCUPSPrintingPrefs, &bval_set);
1071*5e7646d2SAndroid Build Coastguard Worker 
1072*5e7646d2SAndroid Build Coastguard Worker   if (bval_set)
1073*5e7646d2SAndroid Build Coastguard Worker     *value = (int)bval;
1074*5e7646d2SAndroid Build Coastguard Worker 
1075*5e7646d2SAndroid Build Coastguard Worker   return ((int)bval_set);
1076*5e7646d2SAndroid Build Coastguard Worker }
1077*5e7646d2SAndroid Build Coastguard Worker 
1078*5e7646d2SAndroid Build Coastguard Worker 
1079*5e7646d2SAndroid Build Coastguard Worker /*
1080*5e7646d2SAndroid Build Coastguard Worker  * 'cups_apple_get_string()' - Get a string setting from the CUPS preferences.
1081*5e7646d2SAndroid Build Coastguard Worker  */
1082*5e7646d2SAndroid Build Coastguard Worker 
1083*5e7646d2SAndroid Build Coastguard Worker static int				/* O - 1 if set, 0 otherwise */
cups_apple_get_string(CFStringRef key,char * value,size_t valsize)1084*5e7646d2SAndroid Build Coastguard Worker cups_apple_get_string(
1085*5e7646d2SAndroid Build Coastguard Worker     CFStringRef key,			/* I - Key (name) */
1086*5e7646d2SAndroid Build Coastguard Worker     char        *value,			/* O - String value */
1087*5e7646d2SAndroid Build Coastguard Worker     size_t      valsize)		/* I - Size of value buffer */
1088*5e7646d2SAndroid Build Coastguard Worker {
1089*5e7646d2SAndroid Build Coastguard Worker   CFStringRef	sval;			/* String value */
1090*5e7646d2SAndroid Build Coastguard Worker 
1091*5e7646d2SAndroid Build Coastguard Worker 
1092*5e7646d2SAndroid Build Coastguard Worker   if ((sval = CFPreferencesCopyAppValue(key, kCUPSPrintingPrefs)) != NULL)
1093*5e7646d2SAndroid Build Coastguard Worker   {
1094*5e7646d2SAndroid Build Coastguard Worker     Boolean result = CFStringGetCString(sval, value, (CFIndex)valsize, kCFStringEncodingUTF8);
1095*5e7646d2SAndroid Build Coastguard Worker 
1096*5e7646d2SAndroid Build Coastguard Worker     CFRelease(sval);
1097*5e7646d2SAndroid Build Coastguard Worker 
1098*5e7646d2SAndroid Build Coastguard Worker     if (result)
1099*5e7646d2SAndroid Build Coastguard Worker       return (1);
1100*5e7646d2SAndroid Build Coastguard Worker   }
1101*5e7646d2SAndroid Build Coastguard Worker 
1102*5e7646d2SAndroid Build Coastguard Worker   return (0);
1103*5e7646d2SAndroid Build Coastguard Worker }
1104*5e7646d2SAndroid Build Coastguard Worker #endif /* __APPLE__ */
1105*5e7646d2SAndroid Build Coastguard Worker 
1106*5e7646d2SAndroid Build Coastguard Worker 
1107*5e7646d2SAndroid Build Coastguard Worker /*
1108*5e7646d2SAndroid Build Coastguard Worker  * 'cups_boolean_value()' - Convert a string to a boolean value.
1109*5e7646d2SAndroid Build Coastguard Worker  */
1110*5e7646d2SAndroid Build Coastguard Worker 
1111*5e7646d2SAndroid Build Coastguard Worker static int				/* O - Boolean value */
cups_boolean_value(const char * value)1112*5e7646d2SAndroid Build Coastguard Worker cups_boolean_value(const char *value)	/* I - String value */
1113*5e7646d2SAndroid Build Coastguard Worker {
1114*5e7646d2SAndroid Build Coastguard Worker   return (!_cups_strcasecmp(value, "yes") || !_cups_strcasecmp(value, "on") || !_cups_strcasecmp(value, "true"));
1115*5e7646d2SAndroid Build Coastguard Worker }
1116*5e7646d2SAndroid Build Coastguard Worker 
1117*5e7646d2SAndroid Build Coastguard Worker 
1118*5e7646d2SAndroid Build Coastguard Worker /*
1119*5e7646d2SAndroid Build Coastguard Worker  * 'cups_finalize_client_conf()' - Finalize client.conf values.
1120*5e7646d2SAndroid Build Coastguard Worker  */
1121*5e7646d2SAndroid Build Coastguard Worker 
1122*5e7646d2SAndroid Build Coastguard Worker static void
cups_finalize_client_conf(_cups_client_conf_t * cc)1123*5e7646d2SAndroid Build Coastguard Worker cups_finalize_client_conf(
1124*5e7646d2SAndroid Build Coastguard Worker     _cups_client_conf_t *cc)		/* I - client.conf values */
1125*5e7646d2SAndroid Build Coastguard Worker {
1126*5e7646d2SAndroid Build Coastguard Worker   const char	*value;			/* Environment variable */
1127*5e7646d2SAndroid Build Coastguard Worker 
1128*5e7646d2SAndroid Build Coastguard Worker 
1129*5e7646d2SAndroid Build Coastguard Worker   if ((value = getenv("CUPS_TRUSTFIRST")) != NULL)
1130*5e7646d2SAndroid Build Coastguard Worker     cc->trust_first = cups_boolean_value(value);
1131*5e7646d2SAndroid Build Coastguard Worker 
1132*5e7646d2SAndroid Build Coastguard Worker   if ((value = getenv("CUPS_ANYROOT")) != NULL)
1133*5e7646d2SAndroid Build Coastguard Worker     cc->any_root = cups_boolean_value(value);
1134*5e7646d2SAndroid Build Coastguard Worker 
1135*5e7646d2SAndroid Build Coastguard Worker   if ((value = getenv("CUPS_ENCRYPTION")) != NULL)
1136*5e7646d2SAndroid Build Coastguard Worker     cups_set_encryption(cc, value);
1137*5e7646d2SAndroid Build Coastguard Worker 
1138*5e7646d2SAndroid Build Coastguard Worker   if ((value = getenv("CUPS_EXPIREDCERTS")) != NULL)
1139*5e7646d2SAndroid Build Coastguard Worker     cc->expired_certs = cups_boolean_value(value);
1140*5e7646d2SAndroid Build Coastguard Worker 
1141*5e7646d2SAndroid Build Coastguard Worker #ifdef HAVE_GSSAPI
1142*5e7646d2SAndroid Build Coastguard Worker   if ((value = getenv("CUPS_GSSSERVICENAME")) != NULL)
1143*5e7646d2SAndroid Build Coastguard Worker     cups_set_gss_service_name(cc, value);
1144*5e7646d2SAndroid Build Coastguard Worker #endif /* HAVE_GSSAPI */
1145*5e7646d2SAndroid Build Coastguard Worker 
1146*5e7646d2SAndroid Build Coastguard Worker   if ((value = getenv("CUPS_SERVER")) != NULL)
1147*5e7646d2SAndroid Build Coastguard Worker     cups_set_server_name(cc, value);
1148*5e7646d2SAndroid Build Coastguard Worker 
1149*5e7646d2SAndroid Build Coastguard Worker   if ((value = getenv("CUPS_USER")) != NULL)
1150*5e7646d2SAndroid Build Coastguard Worker     cups_set_user(cc, value);
1151*5e7646d2SAndroid Build Coastguard Worker 
1152*5e7646d2SAndroid Build Coastguard Worker   if ((value = getenv("CUPS_VALIDATECERTS")) != NULL)
1153*5e7646d2SAndroid Build Coastguard Worker     cc->validate_certs = cups_boolean_value(value);
1154*5e7646d2SAndroid Build Coastguard Worker 
1155*5e7646d2SAndroid Build Coastguard Worker  /*
1156*5e7646d2SAndroid Build Coastguard Worker   * Then apply defaults for those values that haven't been set...
1157*5e7646d2SAndroid Build Coastguard Worker   */
1158*5e7646d2SAndroid Build Coastguard Worker 
1159*5e7646d2SAndroid Build Coastguard Worker   if (cc->trust_first < 0)
1160*5e7646d2SAndroid Build Coastguard Worker     cc->trust_first = 1;
1161*5e7646d2SAndroid Build Coastguard Worker 
1162*5e7646d2SAndroid Build Coastguard Worker   if (cc->any_root < 0)
1163*5e7646d2SAndroid Build Coastguard Worker     cc->any_root = 1;
1164*5e7646d2SAndroid Build Coastguard Worker 
1165*5e7646d2SAndroid Build Coastguard Worker   if (cc->encryption == (http_encryption_t)-1)
1166*5e7646d2SAndroid Build Coastguard Worker     cc->encryption = HTTP_ENCRYPTION_IF_REQUESTED;
1167*5e7646d2SAndroid Build Coastguard Worker 
1168*5e7646d2SAndroid Build Coastguard Worker   if (cc->expired_certs < 0)
1169*5e7646d2SAndroid Build Coastguard Worker     cc->expired_certs = 0;
1170*5e7646d2SAndroid Build Coastguard Worker 
1171*5e7646d2SAndroid Build Coastguard Worker #ifdef HAVE_GSSAPI
1172*5e7646d2SAndroid Build Coastguard Worker   if (!cc->gss_service_name[0])
1173*5e7646d2SAndroid Build Coastguard Worker     cups_set_gss_service_name(cc, CUPS_DEFAULT_GSSSERVICENAME);
1174*5e7646d2SAndroid Build Coastguard Worker #endif /* HAVE_GSSAPI */
1175*5e7646d2SAndroid Build Coastguard Worker 
1176*5e7646d2SAndroid Build Coastguard Worker   if (!cc->server_name[0])
1177*5e7646d2SAndroid Build Coastguard Worker   {
1178*5e7646d2SAndroid Build Coastguard Worker    /*
1179*5e7646d2SAndroid Build Coastguard Worker     * If we are compiled with domain socket support, only use the
1180*5e7646d2SAndroid Build Coastguard Worker     * domain socket if it exists and has the right permissions...
1181*5e7646d2SAndroid Build Coastguard Worker     */
1182*5e7646d2SAndroid Build Coastguard Worker 
1183*5e7646d2SAndroid Build Coastguard Worker #if defined(__APPLE__) && !TARGET_OS_OSX
1184*5e7646d2SAndroid Build Coastguard Worker     cups_set_server_name(cc, "/private/var/run/printd");
1185*5e7646d2SAndroid Build Coastguard Worker 
1186*5e7646d2SAndroid Build Coastguard Worker #else
1187*5e7646d2SAndroid Build Coastguard Worker #  ifdef CUPS_DEFAULT_DOMAINSOCKET
1188*5e7646d2SAndroid Build Coastguard Worker     if (!access(CUPS_DEFAULT_DOMAINSOCKET, R_OK))
1189*5e7646d2SAndroid Build Coastguard Worker       cups_set_server_name(cc, CUPS_DEFAULT_DOMAINSOCKET);
1190*5e7646d2SAndroid Build Coastguard Worker     else
1191*5e7646d2SAndroid Build Coastguard Worker #  endif /* CUPS_DEFAULT_DOMAINSOCKET */
1192*5e7646d2SAndroid Build Coastguard Worker     cups_set_server_name(cc, "localhost");
1193*5e7646d2SAndroid Build Coastguard Worker #endif /* __APPLE__ && !TARGET_OS_OSX */
1194*5e7646d2SAndroid Build Coastguard Worker   }
1195*5e7646d2SAndroid Build Coastguard Worker 
1196*5e7646d2SAndroid Build Coastguard Worker   if (!cc->user[0])
1197*5e7646d2SAndroid Build Coastguard Worker   {
1198*5e7646d2SAndroid Build Coastguard Worker #ifdef _WIN32
1199*5e7646d2SAndroid Build Coastguard Worker    /*
1200*5e7646d2SAndroid Build Coastguard Worker     * Get the current user name from the OS...
1201*5e7646d2SAndroid Build Coastguard Worker     */
1202*5e7646d2SAndroid Build Coastguard Worker 
1203*5e7646d2SAndroid Build Coastguard Worker     DWORD	size;			/* Size of string */
1204*5e7646d2SAndroid Build Coastguard Worker 
1205*5e7646d2SAndroid Build Coastguard Worker     size = sizeof(cc->user);
1206*5e7646d2SAndroid Build Coastguard Worker     if (!GetUserNameA(cc->user, &size))
1207*5e7646d2SAndroid Build Coastguard Worker #else
1208*5e7646d2SAndroid Build Coastguard Worker    /*
1209*5e7646d2SAndroid Build Coastguard Worker     * Try the USER environment variable as the default username...
1210*5e7646d2SAndroid Build Coastguard Worker     */
1211*5e7646d2SAndroid Build Coastguard Worker 
1212*5e7646d2SAndroid Build Coastguard Worker     const char *envuser = getenv("USER");
1213*5e7646d2SAndroid Build Coastguard Worker 					/* Default username */
1214*5e7646d2SAndroid Build Coastguard Worker     struct passwd *pw = NULL;		/* Account information */
1215*5e7646d2SAndroid Build Coastguard Worker 
1216*5e7646d2SAndroid Build Coastguard Worker     if (envuser)
1217*5e7646d2SAndroid Build Coastguard Worker     {
1218*5e7646d2SAndroid Build Coastguard Worker      /*
1219*5e7646d2SAndroid Build Coastguard Worker       * Validate USER matches the current UID, otherwise don't allow it to
1220*5e7646d2SAndroid Build Coastguard Worker       * override things...  This makes sure that printing after doing su
1221*5e7646d2SAndroid Build Coastguard Worker       * or sudo records the correct username.
1222*5e7646d2SAndroid Build Coastguard Worker       */
1223*5e7646d2SAndroid Build Coastguard Worker 
1224*5e7646d2SAndroid Build Coastguard Worker       if ((pw = getpwnam(envuser)) != NULL && pw->pw_uid != getuid())
1225*5e7646d2SAndroid Build Coastguard Worker 	pw = NULL;
1226*5e7646d2SAndroid Build Coastguard Worker     }
1227*5e7646d2SAndroid Build Coastguard Worker 
1228*5e7646d2SAndroid Build Coastguard Worker     if (!pw)
1229*5e7646d2SAndroid Build Coastguard Worker       pw = getpwuid(getuid());
1230*5e7646d2SAndroid Build Coastguard Worker 
1231*5e7646d2SAndroid Build Coastguard Worker     if (pw)
1232*5e7646d2SAndroid Build Coastguard Worker       strlcpy(cc->user, pw->pw_name, sizeof(cc->user));
1233*5e7646d2SAndroid Build Coastguard Worker     else
1234*5e7646d2SAndroid Build Coastguard Worker #endif /* _WIN32 */
1235*5e7646d2SAndroid Build Coastguard Worker     {
1236*5e7646d2SAndroid Build Coastguard Worker      /*
1237*5e7646d2SAndroid Build Coastguard Worker       * Use the default "unknown" user name...
1238*5e7646d2SAndroid Build Coastguard Worker       */
1239*5e7646d2SAndroid Build Coastguard Worker 
1240*5e7646d2SAndroid Build Coastguard Worker       strlcpy(cc->user, "unknown", sizeof(cc->user));
1241*5e7646d2SAndroid Build Coastguard Worker     }
1242*5e7646d2SAndroid Build Coastguard Worker   }
1243*5e7646d2SAndroid Build Coastguard Worker 
1244*5e7646d2SAndroid Build Coastguard Worker   if (cc->validate_certs < 0)
1245*5e7646d2SAndroid Build Coastguard Worker     cc->validate_certs = 0;
1246*5e7646d2SAndroid Build Coastguard Worker }
1247*5e7646d2SAndroid Build Coastguard Worker 
1248*5e7646d2SAndroid Build Coastguard Worker 
1249*5e7646d2SAndroid Build Coastguard Worker /*
1250*5e7646d2SAndroid Build Coastguard Worker  * 'cups_init_client_conf()' - Initialize client.conf values.
1251*5e7646d2SAndroid Build Coastguard Worker  */
1252*5e7646d2SAndroid Build Coastguard Worker 
1253*5e7646d2SAndroid Build Coastguard Worker static void
cups_init_client_conf(_cups_client_conf_t * cc)1254*5e7646d2SAndroid Build Coastguard Worker cups_init_client_conf(
1255*5e7646d2SAndroid Build Coastguard Worker     _cups_client_conf_t *cc)		/* I - client.conf values */
1256*5e7646d2SAndroid Build Coastguard Worker {
1257*5e7646d2SAndroid Build Coastguard Worker  /*
1258*5e7646d2SAndroid Build Coastguard Worker   * Clear all values to "not set"...
1259*5e7646d2SAndroid Build Coastguard Worker   */
1260*5e7646d2SAndroid Build Coastguard Worker 
1261*5e7646d2SAndroid Build Coastguard Worker   memset(cc, 0, sizeof(_cups_client_conf_t));
1262*5e7646d2SAndroid Build Coastguard Worker 
1263*5e7646d2SAndroid Build Coastguard Worker   cc->uatokens = _CUPS_UATOKENS_MINIMAL;
1264*5e7646d2SAndroid Build Coastguard Worker 
1265*5e7646d2SAndroid Build Coastguard Worker #if defined(__APPLE__) && !TARGET_OS_OSX
1266*5e7646d2SAndroid Build Coastguard Worker   cups_set_user(cc, "mobile");
1267*5e7646d2SAndroid Build Coastguard Worker #endif /* __APPLE__ && !TARGET_OS_OSX */
1268*5e7646d2SAndroid Build Coastguard Worker 
1269*5e7646d2SAndroid Build Coastguard Worker #ifdef HAVE_SSL
1270*5e7646d2SAndroid Build Coastguard Worker   cc->ssl_min_version = _HTTP_TLS_1_0;
1271*5e7646d2SAndroid Build Coastguard Worker   cc->ssl_max_version = _HTTP_TLS_MAX;
1272*5e7646d2SAndroid Build Coastguard Worker #endif /* HAVE_SSL */
1273*5e7646d2SAndroid Build Coastguard Worker   cc->encryption      = (http_encryption_t)-1;
1274*5e7646d2SAndroid Build Coastguard Worker   cc->trust_first     = -1;
1275*5e7646d2SAndroid Build Coastguard Worker   cc->any_root        = -1;
1276*5e7646d2SAndroid Build Coastguard Worker   cc->expired_certs   = -1;
1277*5e7646d2SAndroid Build Coastguard Worker   cc->validate_certs  = -1;
1278*5e7646d2SAndroid Build Coastguard Worker 
1279*5e7646d2SAndroid Build Coastguard Worker  /*
1280*5e7646d2SAndroid Build Coastguard Worker   * Load settings from the org.cups.PrintingPrefs plist (which trump
1281*5e7646d2SAndroid Build Coastguard Worker   * everything...)
1282*5e7646d2SAndroid Build Coastguard Worker   */
1283*5e7646d2SAndroid Build Coastguard Worker 
1284*5e7646d2SAndroid Build Coastguard Worker #if defined(__APPLE__)
1285*5e7646d2SAndroid Build Coastguard Worker   char	sval[1024];			/* String value */
1286*5e7646d2SAndroid Build Coastguard Worker #  ifdef HAVE_SSL
1287*5e7646d2SAndroid Build Coastguard Worker   int	bval;				/* Boolean value */
1288*5e7646d2SAndroid Build Coastguard Worker 
1289*5e7646d2SAndroid Build Coastguard Worker   if (cups_apple_get_boolean(kAllowAnyRootKey, &bval))
1290*5e7646d2SAndroid Build Coastguard Worker     cc->any_root = bval;
1291*5e7646d2SAndroid Build Coastguard Worker 
1292*5e7646d2SAndroid Build Coastguard Worker   if (cups_apple_get_boolean(kAllowExpiredCertsKey, &bval))
1293*5e7646d2SAndroid Build Coastguard Worker     cc->expired_certs = bval;
1294*5e7646d2SAndroid Build Coastguard Worker 
1295*5e7646d2SAndroid Build Coastguard Worker   if (cups_apple_get_string(kEncryptionKey, sval, sizeof(sval)))
1296*5e7646d2SAndroid Build Coastguard Worker     cups_set_encryption(cc, sval);
1297*5e7646d2SAndroid Build Coastguard Worker 
1298*5e7646d2SAndroid Build Coastguard Worker   if (cups_apple_get_string(kSSLOptionsKey, sval, sizeof(sval)))
1299*5e7646d2SAndroid Build Coastguard Worker   {
1300*5e7646d2SAndroid Build Coastguard Worker     cups_set_ssl_options(cc, sval);
1301*5e7646d2SAndroid Build Coastguard Worker   }
1302*5e7646d2SAndroid Build Coastguard Worker   else
1303*5e7646d2SAndroid Build Coastguard Worker   {
1304*5e7646d2SAndroid Build Coastguard Worker     sval[0] = '\0';
1305*5e7646d2SAndroid Build Coastguard Worker 
1306*5e7646d2SAndroid Build Coastguard Worker     if (cups_apple_get_boolean(kAllowRC4, &bval) && bval)
1307*5e7646d2SAndroid Build Coastguard Worker       strlcat(sval, " AllowRC4", sizeof(sval));
1308*5e7646d2SAndroid Build Coastguard Worker     if (cups_apple_get_boolean(kAllowSSL3, &bval) && bval)
1309*5e7646d2SAndroid Build Coastguard Worker       strlcat(sval, " AllowSSL3", sizeof(sval));
1310*5e7646d2SAndroid Build Coastguard Worker     if (cups_apple_get_boolean(kAllowDH, &bval) && bval)
1311*5e7646d2SAndroid Build Coastguard Worker       strlcat(sval, " AllowDH", sizeof(sval));
1312*5e7646d2SAndroid Build Coastguard Worker 
1313*5e7646d2SAndroid Build Coastguard Worker     if (sval[0])
1314*5e7646d2SAndroid Build Coastguard Worker       cups_set_ssl_options(cc, sval);
1315*5e7646d2SAndroid Build Coastguard Worker   }
1316*5e7646d2SAndroid Build Coastguard Worker 
1317*5e7646d2SAndroid Build Coastguard Worker   if (cups_apple_get_boolean(kTrustOnFirstUseKey, &bval))
1318*5e7646d2SAndroid Build Coastguard Worker     cc->trust_first = bval;
1319*5e7646d2SAndroid Build Coastguard Worker 
1320*5e7646d2SAndroid Build Coastguard Worker   if (cups_apple_get_boolean(kValidateCertsKey, &bval))
1321*5e7646d2SAndroid Build Coastguard Worker     cc->validate_certs = bval;
1322*5e7646d2SAndroid Build Coastguard Worker #  endif /* HAVE_SSL */
1323*5e7646d2SAndroid Build Coastguard Worker 
1324*5e7646d2SAndroid Build Coastguard Worker   if (cups_apple_get_string(kDigestOptionsKey, sval, sizeof(sval)))
1325*5e7646d2SAndroid Build Coastguard Worker     cups_set_digestoptions(cc, sval);
1326*5e7646d2SAndroid Build Coastguard Worker 
1327*5e7646d2SAndroid Build Coastguard Worker   if (cups_apple_get_string(kUserKey, sval, sizeof(sval)))
1328*5e7646d2SAndroid Build Coastguard Worker     strlcpy(cc->user, sval, sizeof(cc->user));
1329*5e7646d2SAndroid Build Coastguard Worker 
1330*5e7646d2SAndroid Build Coastguard Worker   if (cups_apple_get_string(kUserAgentTokensKey, sval, sizeof(sval)))
1331*5e7646d2SAndroid Build Coastguard Worker     cups_set_uatokens(cc, sval);
1332*5e7646d2SAndroid Build Coastguard Worker #endif /* __APPLE__ */
1333*5e7646d2SAndroid Build Coastguard Worker }
1334*5e7646d2SAndroid Build Coastguard Worker 
1335*5e7646d2SAndroid Build Coastguard Worker 
1336*5e7646d2SAndroid Build Coastguard Worker /*
1337*5e7646d2SAndroid Build Coastguard Worker  * 'cups_read_client_conf()' - Read a client.conf file.
1338*5e7646d2SAndroid Build Coastguard Worker  */
1339*5e7646d2SAndroid Build Coastguard Worker 
1340*5e7646d2SAndroid Build Coastguard Worker static void
cups_read_client_conf(cups_file_t * fp,_cups_client_conf_t * cc)1341*5e7646d2SAndroid Build Coastguard Worker cups_read_client_conf(
1342*5e7646d2SAndroid Build Coastguard Worker     cups_file_t         *fp,		/* I - File to read */
1343*5e7646d2SAndroid Build Coastguard Worker     _cups_client_conf_t *cc)		/* I - client.conf values */
1344*5e7646d2SAndroid Build Coastguard Worker {
1345*5e7646d2SAndroid Build Coastguard Worker   int	linenum;			/* Current line number */
1346*5e7646d2SAndroid Build Coastguard Worker   char	line[1024],			/* Line from file */
1347*5e7646d2SAndroid Build Coastguard Worker         *value;				/* Pointer into line */
1348*5e7646d2SAndroid Build Coastguard Worker 
1349*5e7646d2SAndroid Build Coastguard Worker 
1350*5e7646d2SAndroid Build Coastguard Worker  /*
1351*5e7646d2SAndroid Build Coastguard Worker   * Read from the file...
1352*5e7646d2SAndroid Build Coastguard Worker   */
1353*5e7646d2SAndroid Build Coastguard Worker 
1354*5e7646d2SAndroid Build Coastguard Worker   linenum = 0;
1355*5e7646d2SAndroid Build Coastguard Worker   while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
1356*5e7646d2SAndroid Build Coastguard Worker   {
1357*5e7646d2SAndroid Build Coastguard Worker     if (!_cups_strcasecmp(line, "DigestOptions") && value)
1358*5e7646d2SAndroid Build Coastguard Worker       cups_set_digestoptions(cc, value);
1359*5e7646d2SAndroid Build Coastguard Worker     else if (!_cups_strcasecmp(line, "Encryption") && value)
1360*5e7646d2SAndroid Build Coastguard Worker       cups_set_encryption(cc, value);
1361*5e7646d2SAndroid Build Coastguard Worker #ifndef __APPLE__
1362*5e7646d2SAndroid Build Coastguard Worker    /*
1363*5e7646d2SAndroid Build Coastguard Worker     * The ServerName directive is not supported on macOS due to app
1364*5e7646d2SAndroid Build Coastguard Worker     * sandboxing restrictions, i.e. not all apps request network access.
1365*5e7646d2SAndroid Build Coastguard Worker     */
1366*5e7646d2SAndroid Build Coastguard Worker     else if (!_cups_strcasecmp(line, "ServerName") && value)
1367*5e7646d2SAndroid Build Coastguard Worker       cups_set_server_name(cc, value);
1368*5e7646d2SAndroid Build Coastguard Worker #endif /* !__APPLE__ */
1369*5e7646d2SAndroid Build Coastguard Worker     else if (!_cups_strcasecmp(line, "User") && value)
1370*5e7646d2SAndroid Build Coastguard Worker       cups_set_user(cc, value);
1371*5e7646d2SAndroid Build Coastguard Worker     else if (!_cups_strcasecmp(line, "UserAgentTokens") && value)
1372*5e7646d2SAndroid Build Coastguard Worker       cups_set_uatokens(cc, value);
1373*5e7646d2SAndroid Build Coastguard Worker     else if (!_cups_strcasecmp(line, "TrustOnFirstUse") && value)
1374*5e7646d2SAndroid Build Coastguard Worker       cc->trust_first = cups_boolean_value(value);
1375*5e7646d2SAndroid Build Coastguard Worker     else if (!_cups_strcasecmp(line, "AllowAnyRoot") && value)
1376*5e7646d2SAndroid Build Coastguard Worker       cc->any_root = cups_boolean_value(value);
1377*5e7646d2SAndroid Build Coastguard Worker     else if (!_cups_strcasecmp(line, "AllowExpiredCerts") &&
1378*5e7646d2SAndroid Build Coastguard Worker              value)
1379*5e7646d2SAndroid Build Coastguard Worker       cc->expired_certs = cups_boolean_value(value);
1380*5e7646d2SAndroid Build Coastguard Worker     else if (!_cups_strcasecmp(line, "ValidateCerts") && value)
1381*5e7646d2SAndroid Build Coastguard Worker       cc->validate_certs = cups_boolean_value(value);
1382*5e7646d2SAndroid Build Coastguard Worker #ifdef HAVE_GSSAPI
1383*5e7646d2SAndroid Build Coastguard Worker     else if (!_cups_strcasecmp(line, "GSSServiceName") && value)
1384*5e7646d2SAndroid Build Coastguard Worker       cups_set_gss_service_name(cc, value);
1385*5e7646d2SAndroid Build Coastguard Worker #endif /* HAVE_GSSAPI */
1386*5e7646d2SAndroid Build Coastguard Worker #ifdef HAVE_SSL
1387*5e7646d2SAndroid Build Coastguard Worker     else if (!_cups_strcasecmp(line, "SSLOptions") && value)
1388*5e7646d2SAndroid Build Coastguard Worker       cups_set_ssl_options(cc, value);
1389*5e7646d2SAndroid Build Coastguard Worker #endif /* HAVE_SSL */
1390*5e7646d2SAndroid Build Coastguard Worker   }
1391*5e7646d2SAndroid Build Coastguard Worker }
1392*5e7646d2SAndroid Build Coastguard Worker 
1393*5e7646d2SAndroid Build Coastguard Worker 
1394*5e7646d2SAndroid Build Coastguard Worker /*
1395*5e7646d2SAndroid Build Coastguard Worker  * 'cups_set_default_ipp_port()' - Set the default IPP port value.
1396*5e7646d2SAndroid Build Coastguard Worker  */
1397*5e7646d2SAndroid Build Coastguard Worker 
1398*5e7646d2SAndroid Build Coastguard Worker static void
cups_set_default_ipp_port(_cups_globals_t * cg)1399*5e7646d2SAndroid Build Coastguard Worker cups_set_default_ipp_port(
1400*5e7646d2SAndroid Build Coastguard Worker     _cups_globals_t *cg)		/* I - Global data */
1401*5e7646d2SAndroid Build Coastguard Worker {
1402*5e7646d2SAndroid Build Coastguard Worker   const char	*ipp_port;		/* IPP_PORT environment variable */
1403*5e7646d2SAndroid Build Coastguard Worker 
1404*5e7646d2SAndroid Build Coastguard Worker 
1405*5e7646d2SAndroid Build Coastguard Worker   if ((ipp_port = getenv("IPP_PORT")) != NULL)
1406*5e7646d2SAndroid Build Coastguard Worker   {
1407*5e7646d2SAndroid Build Coastguard Worker     if ((cg->ipp_port = atoi(ipp_port)) <= 0)
1408*5e7646d2SAndroid Build Coastguard Worker       cg->ipp_port = CUPS_DEFAULT_IPP_PORT;
1409*5e7646d2SAndroid Build Coastguard Worker   }
1410*5e7646d2SAndroid Build Coastguard Worker   else
1411*5e7646d2SAndroid Build Coastguard Worker     cg->ipp_port = CUPS_DEFAULT_IPP_PORT;
1412*5e7646d2SAndroid Build Coastguard Worker }
1413*5e7646d2SAndroid Build Coastguard Worker 
1414*5e7646d2SAndroid Build Coastguard Worker 
1415*5e7646d2SAndroid Build Coastguard Worker /*
1416*5e7646d2SAndroid Build Coastguard Worker  * 'cups_set_digestoptions()' - Set the DigestOptions value.
1417*5e7646d2SAndroid Build Coastguard Worker  */
1418*5e7646d2SAndroid Build Coastguard Worker 
1419*5e7646d2SAndroid Build Coastguard Worker static void
cups_set_digestoptions(_cups_client_conf_t * cc,const char * value)1420*5e7646d2SAndroid Build Coastguard Worker cups_set_digestoptions(
1421*5e7646d2SAndroid Build Coastguard Worker     _cups_client_conf_t *cc,		/* I - client.conf values */
1422*5e7646d2SAndroid Build Coastguard Worker     const char          *value)		/* I - Value */
1423*5e7646d2SAndroid Build Coastguard Worker {
1424*5e7646d2SAndroid Build Coastguard Worker   if (!_cups_strcasecmp(value, "DenyMD5"))
1425*5e7646d2SAndroid Build Coastguard Worker     cc->digestoptions = _CUPS_DIGESTOPTIONS_DENYMD5;
1426*5e7646d2SAndroid Build Coastguard Worker   else if (!_cups_strcasecmp(value, "None"))
1427*5e7646d2SAndroid Build Coastguard Worker     cc->digestoptions = _CUPS_DIGESTOPTIONS_NONE;
1428*5e7646d2SAndroid Build Coastguard Worker }
1429*5e7646d2SAndroid Build Coastguard Worker 
1430*5e7646d2SAndroid Build Coastguard Worker 
1431*5e7646d2SAndroid Build Coastguard Worker /*
1432*5e7646d2SAndroid Build Coastguard Worker  * 'cups_set_encryption()' - Set the Encryption value.
1433*5e7646d2SAndroid Build Coastguard Worker  */
1434*5e7646d2SAndroid Build Coastguard Worker 
1435*5e7646d2SAndroid Build Coastguard Worker static void
cups_set_encryption(_cups_client_conf_t * cc,const char * value)1436*5e7646d2SAndroid Build Coastguard Worker cups_set_encryption(
1437*5e7646d2SAndroid Build Coastguard Worker     _cups_client_conf_t *cc,		/* I - client.conf values */
1438*5e7646d2SAndroid Build Coastguard Worker     const char          *value)		/* I - Value */
1439*5e7646d2SAndroid Build Coastguard Worker {
1440*5e7646d2SAndroid Build Coastguard Worker   if (!_cups_strcasecmp(value, "never"))
1441*5e7646d2SAndroid Build Coastguard Worker     cc->encryption = HTTP_ENCRYPTION_NEVER;
1442*5e7646d2SAndroid Build Coastguard Worker   else if (!_cups_strcasecmp(value, "always"))
1443*5e7646d2SAndroid Build Coastguard Worker     cc->encryption = HTTP_ENCRYPTION_ALWAYS;
1444*5e7646d2SAndroid Build Coastguard Worker   else if (!_cups_strcasecmp(value, "required"))
1445*5e7646d2SAndroid Build Coastguard Worker     cc->encryption = HTTP_ENCRYPTION_REQUIRED;
1446*5e7646d2SAndroid Build Coastguard Worker   else
1447*5e7646d2SAndroid Build Coastguard Worker     cc->encryption = HTTP_ENCRYPTION_IF_REQUESTED;
1448*5e7646d2SAndroid Build Coastguard Worker }
1449*5e7646d2SAndroid Build Coastguard Worker 
1450*5e7646d2SAndroid Build Coastguard Worker 
1451*5e7646d2SAndroid Build Coastguard Worker /*
1452*5e7646d2SAndroid Build Coastguard Worker  * 'cups_set_gss_service_name()' - Set the GSSServiceName value.
1453*5e7646d2SAndroid Build Coastguard Worker  */
1454*5e7646d2SAndroid Build Coastguard Worker 
1455*5e7646d2SAndroid Build Coastguard Worker #ifdef HAVE_GSSAPI
1456*5e7646d2SAndroid Build Coastguard Worker static void
cups_set_gss_service_name(_cups_client_conf_t * cc,const char * value)1457*5e7646d2SAndroid Build Coastguard Worker cups_set_gss_service_name(
1458*5e7646d2SAndroid Build Coastguard Worker     _cups_client_conf_t *cc,		/* I - client.conf values */
1459*5e7646d2SAndroid Build Coastguard Worker     const char          *value)		/* I - Value */
1460*5e7646d2SAndroid Build Coastguard Worker {
1461*5e7646d2SAndroid Build Coastguard Worker   strlcpy(cc->gss_service_name, value, sizeof(cc->gss_service_name));
1462*5e7646d2SAndroid Build Coastguard Worker }
1463*5e7646d2SAndroid Build Coastguard Worker #endif /* HAVE_GSSAPI */
1464*5e7646d2SAndroid Build Coastguard Worker 
1465*5e7646d2SAndroid Build Coastguard Worker 
1466*5e7646d2SAndroid Build Coastguard Worker /*
1467*5e7646d2SAndroid Build Coastguard Worker  * 'cups_set_server_name()' - Set the ServerName value.
1468*5e7646d2SAndroid Build Coastguard Worker  */
1469*5e7646d2SAndroid Build Coastguard Worker 
1470*5e7646d2SAndroid Build Coastguard Worker static void
cups_set_server_name(_cups_client_conf_t * cc,const char * value)1471*5e7646d2SAndroid Build Coastguard Worker cups_set_server_name(
1472*5e7646d2SAndroid Build Coastguard Worker     _cups_client_conf_t *cc,		/* I - client.conf values */
1473*5e7646d2SAndroid Build Coastguard Worker     const char          *value)		/* I - Value */
1474*5e7646d2SAndroid Build Coastguard Worker {
1475*5e7646d2SAndroid Build Coastguard Worker   strlcpy(cc->server_name, value, sizeof(cc->server_name));
1476*5e7646d2SAndroid Build Coastguard Worker }
1477*5e7646d2SAndroid Build Coastguard Worker 
1478*5e7646d2SAndroid Build Coastguard Worker 
1479*5e7646d2SAndroid Build Coastguard Worker /*
1480*5e7646d2SAndroid Build Coastguard Worker  * 'cups_set_ssl_options()' - Set the SSLOptions value.
1481*5e7646d2SAndroid Build Coastguard Worker  */
1482*5e7646d2SAndroid Build Coastguard Worker 
1483*5e7646d2SAndroid Build Coastguard Worker #ifdef HAVE_SSL
1484*5e7646d2SAndroid Build Coastguard Worker static void
cups_set_ssl_options(_cups_client_conf_t * cc,const char * value)1485*5e7646d2SAndroid Build Coastguard Worker cups_set_ssl_options(
1486*5e7646d2SAndroid Build Coastguard Worker     _cups_client_conf_t *cc,		/* I - client.conf values */
1487*5e7646d2SAndroid Build Coastguard Worker     const char          *value)		/* I - Value */
1488*5e7646d2SAndroid Build Coastguard Worker {
1489*5e7646d2SAndroid Build Coastguard Worker  /*
1490*5e7646d2SAndroid Build Coastguard Worker   * SSLOptions [AllowRC4] [AllowSSL3] [AllowDH] [DenyTLS1.0] [None]
1491*5e7646d2SAndroid Build Coastguard Worker   */
1492*5e7646d2SAndroid Build Coastguard Worker 
1493*5e7646d2SAndroid Build Coastguard Worker   int	options = _HTTP_TLS_NONE,	/* SSL/TLS options */
1494*5e7646d2SAndroid Build Coastguard Worker 	min_version = _HTTP_TLS_1_0,	/* Minimum SSL/TLS version */
1495*5e7646d2SAndroid Build Coastguard Worker 	max_version = _HTTP_TLS_MAX;	/* Maximum SSL/TLS version */
1496*5e7646d2SAndroid Build Coastguard Worker   char	temp[256],			/* Copy of value */
1497*5e7646d2SAndroid Build Coastguard Worker 	*start,				/* Start of option */
1498*5e7646d2SAndroid Build Coastguard Worker 	*end;				/* End of option */
1499*5e7646d2SAndroid Build Coastguard Worker 
1500*5e7646d2SAndroid Build Coastguard Worker 
1501*5e7646d2SAndroid Build Coastguard Worker   strlcpy(temp, value, sizeof(temp));
1502*5e7646d2SAndroid Build Coastguard Worker 
1503*5e7646d2SAndroid Build Coastguard Worker   for (start = temp; *start; start = end)
1504*5e7646d2SAndroid Build Coastguard Worker   {
1505*5e7646d2SAndroid Build Coastguard Worker    /*
1506*5e7646d2SAndroid Build Coastguard Worker     * Find end of keyword...
1507*5e7646d2SAndroid Build Coastguard Worker     */
1508*5e7646d2SAndroid Build Coastguard Worker 
1509*5e7646d2SAndroid Build Coastguard Worker     end = start;
1510*5e7646d2SAndroid Build Coastguard Worker     while (*end && !_cups_isspace(*end))
1511*5e7646d2SAndroid Build Coastguard Worker       end ++;
1512*5e7646d2SAndroid Build Coastguard Worker 
1513*5e7646d2SAndroid Build Coastguard Worker     if (*end)
1514*5e7646d2SAndroid Build Coastguard Worker       *end++ = '\0';
1515*5e7646d2SAndroid Build Coastguard Worker 
1516*5e7646d2SAndroid Build Coastguard Worker    /*
1517*5e7646d2SAndroid Build Coastguard Worker     * Compare...
1518*5e7646d2SAndroid Build Coastguard Worker     */
1519*5e7646d2SAndroid Build Coastguard Worker 
1520*5e7646d2SAndroid Build Coastguard Worker     if (!_cups_strcasecmp(start, "AllowRC4"))
1521*5e7646d2SAndroid Build Coastguard Worker       options |= _HTTP_TLS_ALLOW_RC4;
1522*5e7646d2SAndroid Build Coastguard Worker     else if (!_cups_strcasecmp(start, "AllowSSL3"))
1523*5e7646d2SAndroid Build Coastguard Worker       min_version = _HTTP_TLS_SSL3;
1524*5e7646d2SAndroid Build Coastguard Worker     else if (!_cups_strcasecmp(start, "AllowDH"))
1525*5e7646d2SAndroid Build Coastguard Worker       options |= _HTTP_TLS_ALLOW_DH;
1526*5e7646d2SAndroid Build Coastguard Worker     else if (!_cups_strcasecmp(start, "DenyCBC"))
1527*5e7646d2SAndroid Build Coastguard Worker       options |= _HTTP_TLS_DENY_CBC;
1528*5e7646d2SAndroid Build Coastguard Worker     else if (!_cups_strcasecmp(start, "DenyTLS1.0"))
1529*5e7646d2SAndroid Build Coastguard Worker       min_version = _HTTP_TLS_1_1;
1530*5e7646d2SAndroid Build Coastguard Worker     else if (!_cups_strcasecmp(start, "MaxTLS1.0"))
1531*5e7646d2SAndroid Build Coastguard Worker       max_version = _HTTP_TLS_1_0;
1532*5e7646d2SAndroid Build Coastguard Worker     else if (!_cups_strcasecmp(start, "MaxTLS1.1"))
1533*5e7646d2SAndroid Build Coastguard Worker       max_version = _HTTP_TLS_1_1;
1534*5e7646d2SAndroid Build Coastguard Worker     else if (!_cups_strcasecmp(start, "MaxTLS1.2"))
1535*5e7646d2SAndroid Build Coastguard Worker       max_version = _HTTP_TLS_1_2;
1536*5e7646d2SAndroid Build Coastguard Worker     else if (!_cups_strcasecmp(start, "MaxTLS1.3"))
1537*5e7646d2SAndroid Build Coastguard Worker       max_version = _HTTP_TLS_1_3;
1538*5e7646d2SAndroid Build Coastguard Worker     else if (!_cups_strcasecmp(start, "MinTLS1.0"))
1539*5e7646d2SAndroid Build Coastguard Worker       min_version = _HTTP_TLS_1_0;
1540*5e7646d2SAndroid Build Coastguard Worker     else if (!_cups_strcasecmp(start, "MinTLS1.1"))
1541*5e7646d2SAndroid Build Coastguard Worker       min_version = _HTTP_TLS_1_1;
1542*5e7646d2SAndroid Build Coastguard Worker     else if (!_cups_strcasecmp(start, "MinTLS1.2"))
1543*5e7646d2SAndroid Build Coastguard Worker       min_version = _HTTP_TLS_1_2;
1544*5e7646d2SAndroid Build Coastguard Worker     else if (!_cups_strcasecmp(start, "MinTLS1.3"))
1545*5e7646d2SAndroid Build Coastguard Worker       min_version = _HTTP_TLS_1_3;
1546*5e7646d2SAndroid Build Coastguard Worker     else if (!_cups_strcasecmp(start, "None"))
1547*5e7646d2SAndroid Build Coastguard Worker       options = _HTTP_TLS_NONE;
1548*5e7646d2SAndroid Build Coastguard Worker   }
1549*5e7646d2SAndroid Build Coastguard Worker 
1550*5e7646d2SAndroid Build Coastguard Worker   cc->ssl_options     = options;
1551*5e7646d2SAndroid Build Coastguard Worker   cc->ssl_max_version = max_version;
1552*5e7646d2SAndroid Build Coastguard Worker   cc->ssl_min_version = min_version;
1553*5e7646d2SAndroid Build Coastguard Worker 
1554*5e7646d2SAndroid Build Coastguard Worker   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));
1555*5e7646d2SAndroid Build Coastguard Worker }
1556*5e7646d2SAndroid Build Coastguard Worker #endif /* HAVE_SSL */
1557*5e7646d2SAndroid Build Coastguard Worker 
1558*5e7646d2SAndroid Build Coastguard Worker 
1559*5e7646d2SAndroid Build Coastguard Worker /*
1560*5e7646d2SAndroid Build Coastguard Worker  * 'cups_set_uatokens()' - Set the UserAgentTokens value.
1561*5e7646d2SAndroid Build Coastguard Worker  */
1562*5e7646d2SAndroid Build Coastguard Worker 
1563*5e7646d2SAndroid Build Coastguard Worker static void
cups_set_uatokens(_cups_client_conf_t * cc,const char * value)1564*5e7646d2SAndroid Build Coastguard Worker cups_set_uatokens(
1565*5e7646d2SAndroid Build Coastguard Worker     _cups_client_conf_t *cc,		/* I - client.conf values */
1566*5e7646d2SAndroid Build Coastguard Worker     const char          *value)		/* I - Value */
1567*5e7646d2SAndroid Build Coastguard Worker {
1568*5e7646d2SAndroid Build Coastguard Worker   int	i;				/* Looping var */
1569*5e7646d2SAndroid Build Coastguard Worker   static const char * const uatokens[] =/* UserAgentTokens values */
1570*5e7646d2SAndroid Build Coastguard Worker   {
1571*5e7646d2SAndroid Build Coastguard Worker     "NONE",
1572*5e7646d2SAndroid Build Coastguard Worker     "PRODUCTONLY",
1573*5e7646d2SAndroid Build Coastguard Worker     "MAJOR",
1574*5e7646d2SAndroid Build Coastguard Worker     "MINOR",
1575*5e7646d2SAndroid Build Coastguard Worker     "MINIMAL",
1576*5e7646d2SAndroid Build Coastguard Worker     "OS",
1577*5e7646d2SAndroid Build Coastguard Worker     "FULL"
1578*5e7646d2SAndroid Build Coastguard Worker   };
1579*5e7646d2SAndroid Build Coastguard Worker 
1580*5e7646d2SAndroid Build Coastguard Worker   for (i = 0; i < (int)(sizeof(uatokens) / sizeof(uatokens[0])); i ++)
1581*5e7646d2SAndroid Build Coastguard Worker   {
1582*5e7646d2SAndroid Build Coastguard Worker     if (!_cups_strcasecmp(value, uatokens[i]))
1583*5e7646d2SAndroid Build Coastguard Worker     {
1584*5e7646d2SAndroid Build Coastguard Worker       cc->uatokens = (_cups_uatokens_t)i;
1585*5e7646d2SAndroid Build Coastguard Worker       return;
1586*5e7646d2SAndroid Build Coastguard Worker     }
1587*5e7646d2SAndroid Build Coastguard Worker   }
1588*5e7646d2SAndroid Build Coastguard Worker }
1589*5e7646d2SAndroid Build Coastguard Worker 
1590*5e7646d2SAndroid Build Coastguard Worker 
1591*5e7646d2SAndroid Build Coastguard Worker /*
1592*5e7646d2SAndroid Build Coastguard Worker  * 'cups_set_user()' - Set the User value.
1593*5e7646d2SAndroid Build Coastguard Worker  */
1594*5e7646d2SAndroid Build Coastguard Worker 
1595*5e7646d2SAndroid Build Coastguard Worker static void
cups_set_user(_cups_client_conf_t * cc,const char * value)1596*5e7646d2SAndroid Build Coastguard Worker cups_set_user(
1597*5e7646d2SAndroid Build Coastguard Worker     _cups_client_conf_t *cc,		/* I - client.conf values */
1598*5e7646d2SAndroid Build Coastguard Worker     const char          *value)		/* I - Value */
1599*5e7646d2SAndroid Build Coastguard Worker {
1600*5e7646d2SAndroid Build Coastguard Worker   strlcpy(cc->user, value, sizeof(cc->user));
1601*5e7646d2SAndroid Build Coastguard Worker }
1602