2 * "$Id: usersys.c 8498 2009-04-13 17:03:15Z mike $"
4 * User, system, and password routines for the Common UNIX Printing
7 * Copyright 2007-2009 by Apple Inc.
8 * Copyright 1997-2006 by Easy Software Products.
10 * These coded instructions, statements, and computer programs are the
11 * property of Apple Inc. and are protected by Federal copyright
12 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
13 * which should have been included with this file. If this file is
14 * file is missing or damaged, see the license at "http://www.cups.org/".
16 * This file is subject to the Apple OS-Developed Software exception.
20 * cupsEncryption() - Get the default encryption settings.
21 * cupsGetPassword() - Get a password from the user.
22 * cupsServer() - Return the hostname/address of the default
24 * cupsSetEncryption() - Set the encryption preference.
25 * cupsSetPasswordCB() - Set the password callback for CUPS.
26 * cupsSetPasswordCB2() - Set the advanced password callback for CUPS.
27 * cupsSetServer() - Set the default server name.
28 * cupsSetUser() - Set the default user name.
29 * cupsUser() - Return the current user's name.
30 * _cupsGetPassword() - Get a password from the user.
31 * _cupsSetDefaults() - Set the default server, port, and encryption.
32 * cups_read_client_conf() - Read a client.conf file.
36 * Include necessary headers...
39 #include "http-private.h"
53 static void cups_read_client_conf(cups_file_t
*fp
,
55 const char *cups_encryption
,
56 const char *cups_server
);
60 * 'cupsEncryption()' - Get the default encryption settings.
62 * The default encryption setting comes from the CUPS_ENCRYPTION
63 * environment variable, then the ~/.cups/client.conf file, and finally the
64 * /etc/cups/client.conf file. If not set, the default is
65 * @code HTTP_ENCRYPT_IF_REQUESTED@.
68 http_encryption_t
/* O - Encryption settings */
71 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
74 if (cg
->encryption
== (http_encryption_t
)-1)
77 return (cg
->encryption
);
82 * 'cupsGetPassword()' - Get a password from the user.
84 * Uses the current password callback function. Returns @code NULL@ if the
85 * user does not provide a password.
88 const char * /* O - Password */
89 cupsGetPassword(const char *prompt
) /* I - Prompt string */
91 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
94 return ((cg
->password_cb
)(prompt
, NULL
, NULL
, NULL
, cg
->password_data
));
99 * 'cupsGetPassword2()' - Get a password from the user using the advanced
102 * Uses the current password callback function. Returns @code NULL@ if the
103 * user does not provide a password.
108 const char * /* O - Password */
109 cupsGetPassword2(const char *prompt
, /* I - Prompt string */
110 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
111 const char *method
, /* I - Request method ("GET", "POST", "PUT") */
112 const char *resource
) /* I - Resource path */
114 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
118 http
= _cupsConnect();
120 return ((cg
->password_cb
)(prompt
, http
, method
, resource
, cg
->password_data
));
125 * 'cupsServer()' - Return the hostname/address of the default server.
127 * The returned value can be a fully-qualified hostname, a numeric
128 * IPv4 or IPv6 address, or a domain socket pathname.
131 const char * /* O - Server name */
134 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
145 * 'cupsSetEncryption()' - Set the encryption preference.
149 cupsSetEncryption(http_encryption_t e
) /* I - New encryption preference */
151 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
157 httpEncryption(cg
->http
, e
);
162 * 'cupsSetPasswordCB()' - Set the password callback for CUPS.
164 * Pass @code NULL@ to restore the default (console) password callback.
168 cupsSetPasswordCB(cups_password_cb_t cb
)/* I - Callback function */
170 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
173 if (cb
== (cups_password_cb_t
)0)
174 cg
->password_cb
= (cups_password_cb2_t
)_cupsGetPassword
;
176 cg
->password_cb
= (cups_password_cb2_t
)cb
;
178 cg
->password_data
= NULL
;
183 * 'cupsSetPasswordCB2()' - Set the advanced password callback for CUPS.
185 * Pass @code NULL@ to restore the default (console) password callback.
192 cups_password_cb2_t cb
, /* I - Callback function */
193 void *user_data
) /* I - User data pointer */
195 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
198 if (cb
== (cups_password_cb2_t
)0)
199 cg
->password_cb
= (cups_password_cb2_t
)_cupsGetPassword
;
201 cg
->password_cb
= cb
;
203 cg
->password_data
= user_data
;
208 * 'cupsSetServer()' - Set the default server name.
210 * The "server" string can be a fully-qualified hostname, a numeric
211 * IPv4 or IPv6 address, or a domain socket pathname. Pass @code NULL@ to
212 * restore the default server name.
216 cupsSetServer(const char *server
) /* I - Server name */
218 char *port
; /* Pointer to port */
219 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
224 strlcpy(cg
->server
, server
, sizeof(cg
->server
));
226 if (cg
->server
[0] != '/' && (port
= strrchr(cg
->server
, ':')) != NULL
&&
227 !strchr(port
, ']') && isdigit(port
[1] & 255))
231 cg
->ipp_port
= atoi(port
);
234 if (cg
->server
[0] == '/')
235 strcpy(cg
->servername
, "localhost");
237 strlcpy(cg
->servername
, cg
->server
, sizeof(cg
->servername
));
241 cg
->server
[0] = '\0';
242 cg
->servername
[0] = '\0';
254 * 'cupsSetUser()' - Set the default user name.
256 * Pass @code NULL@ to restore the default user name.
260 cupsSetUser(const char *user
) /* I - User name */
262 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
266 strlcpy(cg
->user
, user
, sizeof(cg
->user
));
274 * WIN32 username and password stuff.
278 * 'cupsUser()' - Return the current user's name.
281 const char * /* O - User name */
284 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
289 DWORD size
; /* Size of string */
292 size
= sizeof(cg
->user
);
293 if (!GetUserName(cg
->user
, &size
))
296 * Use the default username...
299 strcpy(cg
->user
, "unknown");
308 * '_cupsGetPassword()' - Get a password from the user.
311 const char * /* O - Password */
312 _cupsGetPassword(const char *prompt
) /* I - Prompt string */
318 * UNIX username and password stuff...
324 * 'cupsUser()' - Return the current user's name.
327 const char * /* O - User name */
330 struct passwd
*pwd
; /* User/password entry */
331 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
337 * Rewind the password file...
343 * Lookup the password entry for the current user.
346 if ((pwd
= getpwuid(getuid())) == NULL
)
347 strcpy(cg
->user
, "unknown"); /* Unknown user! */
351 * Copy the username...
356 strlcpy(cg
->user
, pwd
->pw_name
, sizeof(cg
->user
));
360 * Rewind the password file again...
371 * '_cupsGetPassword()' - Get a password from the user.
374 const char * /* O - Password */
375 _cupsGetPassword(const char *prompt
) /* I - Prompt string */
377 return (getpass(prompt
));
383 * '_cupsSetDefaults()' - Set the default server, port, and encryption.
387 _cupsSetDefaults(void)
389 cups_file_t
*fp
; /* File */
390 const char *home
, /* Home directory of user */
391 *cups_encryption
, /* CUPS_ENCRYPTION env var */
392 *cups_server
; /* CUPS_SERVER env var */
393 char filename
[1024]; /* Filename */
394 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
397 DEBUG_puts("_cupsSetDefaults()");
400 * First collect environment variables...
403 cups_encryption
= getenv("CUPS_ENCRYPTION");
404 cups_server
= getenv("CUPS_SERVER");
407 * Then, if needed, the .cups/client.conf or .cupsrc file in the home
411 if ((cg
->encryption
== (http_encryption_t
)-1 || !cg
->server
[0] ||
412 !cg
->ipp_port
) && (home
= getenv("HOME")) != NULL
)
415 * Look for ~/.cups/client.conf or ~/.cupsrc...
418 snprintf(filename
, sizeof(filename
), "%s/.cups/client.conf", home
);
419 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
421 snprintf(filename
, sizeof(filename
), "%s/.cupsrc", home
);
422 fp
= cupsFileOpen(filename
, "r");
427 cups_read_client_conf(fp
, cg
, cups_encryption
, cups_server
);
432 if (cg
->encryption
== (http_encryption_t
)-1 || !cg
->server
[0] ||
436 * Look for CUPS_SERVERROOT/client.conf...
439 snprintf(filename
, sizeof(filename
), "%s/client.conf", cg
->cups_serverroot
);
440 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
442 cups_read_client_conf(fp
, cg
, cups_encryption
, cups_server
);
448 * If we still have things that aren't set, use the compiled in defaults...
451 if (cg
->encryption
== (http_encryption_t
)-1)
452 cg
->encryption
= HTTP_ENCRYPT_IF_REQUESTED
;
458 #ifdef CUPS_DEFAULT_DOMAINSOCKET
460 * If we are compiled with domain socket support, only use the
461 * domain socket if it exists and has the right permissions...
464 struct stat sockinfo
; /* Domain socket information */
466 if (!stat(CUPS_DEFAULT_DOMAINSOCKET
, &sockinfo
) &&
467 (sockinfo
.st_mode
& S_IRWXO
) == S_IRWXO
)
468 cups_server
= CUPS_DEFAULT_DOMAINSOCKET
;
470 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
471 cups_server
= "localhost";
474 cupsSetServer(cups_server
);
479 const char *ipp_port
; /* IPP_PORT environment variable */
480 struct servent
*service
; /* Port number info */
483 if ((ipp_port
= getenv("IPP_PORT")) != NULL
)
485 if ((cg
->ipp_port
= atoi(ipp_port
)) <= 0)
486 cg
->ipp_port
= CUPS_DEFAULT_IPP_PORT
;
488 else if ((service
= getservbyname("ipp", NULL
)) == NULL
||
489 service
->s_port
<= 0)
490 cg
->ipp_port
= CUPS_DEFAULT_IPP_PORT
;
492 cg
->ipp_port
= ntohs(service
->s_port
);
498 * 'cups_read_client_conf()' - Read a client.conf file.
502 cups_read_client_conf(
503 cups_file_t
*fp
, /* I - File to read */
504 _cups_globals_t
*cg
, /* I - Global data */
505 const char *cups_encryption
, /* I - CUPS_ENCRYPTION env var */
506 const char *cups_server
) /* I - CUPS_SERVER env var */
508 int linenum
; /* Current line number */
509 char line
[1024], /* Line from file */
510 *value
, /* Pointer into line */
511 encryption
[1024], /* Encryption value */
512 server_name
[1024]; /* ServerName value */
516 * Read from the file...
520 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
522 if (!cups_encryption
&& cg
->encryption
== (http_encryption_t
)-1 &&
523 !strcasecmp(line
, "Encryption") && value
)
525 strlcpy(encryption
, value
, sizeof(encryption
));
526 cups_encryption
= encryption
;
528 else if (!cups_server
&& (!cg
->server
[0] || !cg
->ipp_port
) &&
529 !strcasecmp(line
, "ServerName") && value
)
531 strlcpy(server_name
, value
, sizeof(server_name
));
532 cups_server
= server_name
;
540 if (cg
->encryption
== (http_encryption_t
)-1 && cups_encryption
)
542 if (!strcasecmp(cups_encryption
, "never"))
543 cg
->encryption
= HTTP_ENCRYPT_NEVER
;
544 else if (!strcasecmp(cups_encryption
, "always"))
545 cg
->encryption
= HTTP_ENCRYPT_ALWAYS
;
546 else if (!strcasecmp(cups_encryption
, "required"))
547 cg
->encryption
= HTTP_ENCRYPT_REQUIRED
;
549 cg
->encryption
= HTTP_ENCRYPT_IF_REQUESTED
;
552 if ((!cg
->server
[0] || !cg
->ipp_port
) && cups_server
)
557 * Copy server name...
560 strlcpy(cg
->server
, cups_server
, sizeof(cg
->server
));
562 if (cg
->server
[0] != '/' && (value
= strrchr(cg
->server
, ':')) != NULL
&&
563 !strchr(value
, ']') && isdigit(value
[1] & 255))
568 if (cg
->server
[0] == '/')
569 strcpy(cg
->servername
, "localhost");
571 strlcpy(cg
->servername
, cg
->server
, sizeof(cg
->servername
));
573 else if (cups_server
[0] != '/' &&
574 (value
= strrchr(cups_server
, ':')) != NULL
&&
575 !strchr(value
, ']') && isdigit(value
[1] & 255))
580 if (!cg
->ipp_port
&& value
)
581 cg
->ipp_port
= atoi(value
);
587 * End of "$Id: usersys.c 8498 2009-04-13 17:03:15Z mike $".