]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/usersys.c
Only use domain socket if we can access it, and clean up conditional code.
[thirdparty/cups.git] / cups / usersys.c
CommitLineData
6b67a15e 1/*
c9d3f842 2 * "$Id$"
6b67a15e 3 *
4 * User, system, and password routines for the Common UNIX Printing
5 * System (CUPS).
6 *
24c1b5ce 7 * Copyright 1997-2006 by Easy Software Products.
6b67a15e 8 *
9 * These coded instructions, statements, and computer programs are the
10 * property of Easy Software Products and are protected by Federal
11 * copyright law. Distribution and use rights are outlined in the file
12 * "LICENSE.txt" which should have been included with this file. If this
13 * file is missing or damaged please contact Easy Software Products
14 * at:
15 *
16 * Attn: CUPS Licensing Information
17 * Easy Software Products
18 * 44141 Airport View Drive, Suite 204
c9d3f842 19 * Hollywood, Maryland 20636 USA
6b67a15e 20 *
c4dcf3cc 21 * Voice: (301) 373-9600
6b67a15e 22 * EMail: cups-info@cups.org
23 * WWW: http://www.cups.org
24 *
dab1a4d8 25 * This file is subject to the Apple OS-Developed Software exception.
26 *
6b67a15e 27 * Contents:
28 *
f82d9dea 29 * cupsEncryption() - Get the default encryption settings.
30 * cupsGetPassword() - Get a password from the user.
31 * cupsServer() - Return the hostname of the default server.
2a0ef17a 32 * cupsSetEncryption() - Set the encryption preference.
c68b6ae8 33 * cupsSetPasswordCB() - Set the password callback for CUPS.
f82d9dea 34 * cupsSetServer() - Set the default server name.
35 * cupsSetUser() - Set the default user name.
c68b6ae8 36 * cupsUser() - Return the current users name.
f82d9dea 37 * _cupsGetPassword() - Get a password from the user.
6b67a15e 38 */
39
40/*
41 * Include necessary headers...
42 */
43
c6075312 44#include "http-private.h"
03f61bf3 45#include "globals.h"
6b67a15e 46#include <stdlib.h>
9c9584a8 47#include <sys/stat.h>
e335a094 48#ifdef WIN32
49# include <windows.h>
50#endif /* WIN32 */
51
6b67a15e 52
2a0ef17a 53/*
f82d9dea 54 * 'cupsEncryption()' - Get the default encryption settings.
55 *
56 * The default encryption setting comes from the CUPS_ENCRYPTION
57 * environment variable, then the ~/.cupsrc file, and finally the
58 * /etc/cups/client.conf file. If not set, the default is
59 * HTTP_ENCRYPT_IF_REQUESTED.
2a0ef17a 60 */
61
0341722a 62http_encryption_t /* O - Encryption settings */
2a0ef17a 63cupsEncryption(void)
64{
03f61bf3 65 cups_file_t *fp; /* client.conf file */
2a0ef17a 66 char *encryption; /* CUPS_ENCRYPTION variable */
67 const char *home; /* Home directory of user */
30c80414 68 char line[1024], /* Line from file */
69 *value; /* Value on line */
70 int linenum; /* Line number */
2625096f 71 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
2a0ef17a 72
73
74 /*
75 * First see if we have already set the encryption stuff...
76 */
77
03f61bf3 78 if (cg->encryption == (http_encryption_t)-1)
2a0ef17a 79 {
80 /*
81 * Then see if the CUPS_ENCRYPTION environment variable is set...
82 */
83
84 if ((encryption = getenv("CUPS_ENCRYPTION")) == NULL)
85 {
86 /*
87 * Next check to see if we have a $HOME/.cupsrc or client.conf file...
88 */
89
90 if ((home = getenv("HOME")) != NULL)
91 {
92 snprintf(line, sizeof(line), "%s/.cupsrc", home);
03f61bf3 93 fp = cupsFileOpen(line, "r");
2a0ef17a 94 }
95 else
96 fp = NULL;
97
98 if (fp == NULL)
99 {
a501ad17 100 snprintf(line, sizeof(line), "%s/client.conf", cg->cups_serverroot);
101 fp = cupsFileOpen(line, "r");
2a0ef17a 102 }
103
104 encryption = "IfRequested";
105
106 if (fp != NULL)
107 {
108 /*
6cf4fe45 109 * Read the config file and look for an Encryption line...
2a0ef17a 110 */
111
30c80414 112 linenum = 0;
113
114 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum) != NULL)
115 if (!strcasecmp(line, "Encryption") && value)
2a0ef17a 116 {
117 /*
30c80414 118 * Got it!
2a0ef17a 119 */
120
30c80414 121 encryption = value;
2a0ef17a 122 break;
123 }
124
03f61bf3 125 cupsFileClose(fp);
2a0ef17a 126 }
127 }
128
129 /*
130 * Set the encryption preference...
131 */
132
30c80414 133 if (!strcasecmp(encryption, "never"))
03f61bf3 134 cg->encryption = HTTP_ENCRYPT_NEVER;
30c80414 135 else if (!strcasecmp(encryption, "always"))
03f61bf3 136 cg->encryption = HTTP_ENCRYPT_ALWAYS;
30c80414 137 else if (!strcasecmp(encryption, "required"))
03f61bf3 138 cg->encryption = HTTP_ENCRYPT_REQUIRED;
2a0ef17a 139 else
03f61bf3 140 cg->encryption = HTTP_ENCRYPT_IF_REQUESTED;
2a0ef17a 141 }
142
03f61bf3 143 return (cg->encryption);
2a0ef17a 144}
145
146
6b67a15e 147/*
f82d9dea 148 * 'cupsGetPassword()' - Get a password from the user.
149 *
150 * Returns NULL if the user does not provide a password.
6b67a15e 151 */
152
063e1ac7 153const char * /* O - Password */
6b67a15e 154cupsGetPassword(const char *prompt) /* I - Prompt string */
155{
03f61bf3 156 return ((*_cupsGlobals()->password_cb)(prompt));
6b67a15e 157}
6b67a15e 158
6b67a15e 159
2a0ef17a 160/*
161 * 'cupsSetEncryption()' - Set the encryption preference.
162 */
163
164void
165cupsSetEncryption(http_encryption_t e) /* I - New encryption preference */
166{
03f61bf3 167 _cupsGlobals()->encryption = e;
2a0ef17a 168}
169
170
6b67a15e 171/*
f82d9dea 172 * 'cupsServer()' - Return the hostname/address of the default server.
173 *
174 * The returned value can be a fully-qualified hostname, a numeric
175 * IPv4 or IPv6 address, or a domain socket pathname.
6b67a15e 176 */
177
c68b6ae8 178const char * /* O - Server name */
179cupsServer(void)
6b67a15e 180{
03f61bf3 181 cups_file_t *fp; /* client.conf file */
c68b6ae8 182 char *server; /* Pointer to server name */
183 const char *home; /* Home directory of user */
1fcc120e 184 char *port; /* Port number */
03f61bf3 185 char line[1024], /* Line from file */
186 *value; /* Value on line */
187 int linenum; /* Line number in file */
9c9584a8 188#ifdef CUPS_DEFAULT_DOMAINSOCKET
189 struct stat sockinfo; /* Domain socket information */
190#endif /* CUPS_DEFAULT_DOMAINSOCKET */
2625096f 191 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
6b67a15e 192
193
194 /*
c68b6ae8 195 * First see if we have already set the server name...
6b67a15e 196 */
197
03f61bf3 198 if (!cg->server[0])
c68b6ae8 199 {
200 /*
201 * Then see if the CUPS_SERVER environment variable is set...
202 */
6b67a15e 203
c68b6ae8 204 if ((server = getenv("CUPS_SERVER")) == NULL)
205 {
206 /*
207 * Next check to see if we have a $HOME/.cupsrc or client.conf file...
208 */
209
210 if ((home = getenv("HOME")) != NULL)
211 {
04de52f8 212 snprintf(line, sizeof(line), "%s/.cupsrc", home);
03f61bf3 213 fp = cupsFileOpen(line, "r");
c68b6ae8 214 }
215 else
216 fp = NULL;
6b67a15e 217
c68b6ae8 218 if (fp == NULL)
219 {
a501ad17 220 snprintf(line, sizeof(line), "%s/client.conf", cg->cups_serverroot);
221 fp = cupsFileOpen(line, "r");
c68b6ae8 222 }
6b67a15e 223
c94f4aa9 224#ifdef CUPS_DEFAULT_DOMAINSOCKET
9c9584a8 225 /*
226 * If we are compiled with domain socket support, only use the
227 * domain socket if it exists and has the right permissions...
228 */
229
230 if (!stat(CUPS_DEFAULT_DOMAINSOCKET, &sockinfo) &&
231 (sockinfo.st_mode & S_IRWXO) == S_IRWXO)
87650f55 232 server = CUPS_DEFAULT_DOMAINSOCKET;
233 else
c94f4aa9 234#endif /* CUPS_DEFAULT_DOMAINSOCKET */
87650f55 235 server = "localhost";
6b67a15e 236
c68b6ae8 237 if (fp != NULL)
238 {
239 /*
240 * Read the config file and look for a ServerName line...
241 */
242
03f61bf3 243 linenum = 0;
244 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum) != NULL)
30c80414 245 if (!strcasecmp(line, "ServerName") && value)
c68b6ae8 246 {
247 /*
03f61bf3 248 * Got it!
c68b6ae8 249 */
250
03f61bf3 251 server = value;
c68b6ae8 252 break;
253 }
254
03f61bf3 255 cupsFileClose(fp);
c68b6ae8 256 }
257 }
6b67a15e 258
c68b6ae8 259 /*
1fcc120e 260 * Copy the server name over and set the port number, if any...
c68b6ae8 261 */
262
03f61bf3 263 strlcpy(cg->server, server, sizeof(cg->server));
c6075312 264
03f61bf3 265 if (cg->server[0] != '/' && (port = strrchr(cg->server, ':')) != NULL &&
c94f4aa9 266 !strchr(port, ']') && isdigit(port[1] & 255))
c6075312 267 {
1fcc120e 268 *port++ = '\0';
269
270 ippSetPort(atoi(port));
271 }
c94f4aa9 272
273 if (cg->server[0] == '/')
274 strcpy(cg->servername, "localhost");
275 else
276 strlcpy(cg->servername, cg->server, sizeof(cg->servername));
c68b6ae8 277 }
278
03f61bf3 279 return (cg->server);
6b67a15e 280}
281
282
283/*
c68b6ae8 284 * 'cupsSetPasswordCB()' - Set the password callback for CUPS.
f82d9dea 285 *
286 * Pass NULL to restore the default (console) password callback.
6b67a15e 287 */
288
c68b6ae8 289void
f82d9dea 290cupsSetPasswordCB(cups_password_cb_t cb)/* I - Callback function */
6b67a15e 291{
2625096f 292 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
03f61bf3 293
294
c68b6ae8 295 if (cb == (const char *(*)(const char *))0)
03f61bf3 296 cg->password_cb = _cupsGetPassword;
c68b6ae8 297 else
03f61bf3 298 cg->password_cb = cb;
6b67a15e 299}
6b67a15e 300
301
302/*
f82d9dea 303 * 'cupsSetServer()' - Set the default server name.
304 *
305 * The "server" string can be a fully-qualified hostname, a numeric
306 * IPv4 or IPv6 address, or a domain socket pathname. Pass NULL to
307 * restore the default server name.
6b67a15e 308 */
309
c68b6ae8 310void
311cupsSetServer(const char *server) /* I - Server name */
6b67a15e 312{
c94f4aa9 313 char *port; /* Pointer to port */
2625096f 314 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
03f61bf3 315
316
c68b6ae8 317 if (server)
c94f4aa9 318 {
03f61bf3 319 strlcpy(cg->server, server, sizeof(cg->server));
c94f4aa9 320
321 if (cg->server[0] != '/' && (port = strrchr(cg->server, ':')) != NULL &&
322 !strchr(port, ']') && isdigit(port[1] & 255))
323 {
324 *port++ = '\0';
325
326 ippSetPort(atoi(port));
327 }
328
329 if (cg->server[0] == '/')
330 strcpy(cg->servername, "localhost");
331 else
332 strlcpy(cg->servername, cg->server, sizeof(cg->servername));
333 }
c68b6ae8 334 else
c94f4aa9 335 {
336 cg->server[0] = '\0';
337 cg->servername[0] = '\0';
338 }
c68b6ae8 339}
6b67a15e 340
6b67a15e 341
c68b6ae8 342/*
f82d9dea 343 * 'cupsSetUser()' - Set the default user name.
344 *
345 * Pass NULL to restore the default user name.
c68b6ae8 346 */
6b67a15e 347
c68b6ae8 348void
349cupsSetUser(const char *user) /* I - User name */
350{
2625096f 351 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
03f61bf3 352
353
c68b6ae8 354 if (user)
03f61bf3 355 strlcpy(cg->user, user, sizeof(cg->user));
caa58f49 356 else
03f61bf3 357 cg->user[0] = '\0';
c68b6ae8 358}
359
360
e335a094 361#if defined(WIN32)
c68b6ae8 362/*
f82d9dea 363 * WIN32 username and password stuff.
c68b6ae8 364 */
365
366/*
367 * 'cupsUser()' - Return the current user's name.
368 */
369
370const char * /* O - User name */
371cupsUser(void)
372{
2625096f 373 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
03f61bf3 374
375
376 if (!cg->user[0])
e335a094 377 {
378 DWORD size; /* Size of string */
379
380
03f61bf3 381 size = sizeof(cg->user);
382 if (!GetUserName(cg->user, &size))
e335a094 383 {
384 /*
385 * Use the default username...
386 */
387
03f61bf3 388 strcpy(cg->user, "unknown");
e335a094 389 }
390 }
c68b6ae8 391
03f61bf3 392 return (cg->user);
c68b6ae8 393}
394
395
396/*
f82d9dea 397 * '_cupsGetPassword()' - Get a password from the user.
c68b6ae8 398 */
399
03f61bf3 400const char * /* O - Password */
401_cupsGetPassword(const char *prompt) /* I - Prompt string */
c68b6ae8 402{
403 return (NULL);
404}
405#else
406/*
407 * UNIX username and password stuff...
408 */
409
410# include <pwd.h>
caa58f49 411
c68b6ae8 412/*
413 * 'cupsUser()' - Return the current user's name.
414 */
415
416const char * /* O - User name */
417cupsUser(void)
418{
419 struct passwd *pwd; /* User/password entry */
2625096f 420 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
c68b6ae8 421
422
03f61bf3 423 if (!cg->user[0])
3e3712a4 424 {
c68b6ae8 425 /*
426 * Rewind the password file...
427 */
caa58f49 428
c68b6ae8 429 setpwent();
6b67a15e 430
c68b6ae8 431 /*
432 * Lookup the password entry for the current user.
433 */
6b67a15e 434
c68b6ae8 435 if ((pwd = getpwuid(getuid())) == NULL)
03f61bf3 436 strcpy(cg->user, "unknown"); /* Unknown user! */
c68b6ae8 437 else
6b67a15e 438 {
439 /*
06d5ef42 440 * Copy the username...
6b67a15e 441 */
442
c68b6ae8 443 setpwent();
6b67a15e 444
03f61bf3 445 strlcpy(cg->user, pwd->pw_name, sizeof(cg->user));
6b67a15e 446 }
06d5ef42 447
448 /*
449 * Rewind the password file again...
450 */
451
452 setpwent();
c68b6ae8 453 }
6b67a15e 454
03f61bf3 455 return (cg->user);
c68b6ae8 456}
6b67a15e 457
6b67a15e 458
c68b6ae8 459/*
f82d9dea 460 * '_cupsGetPassword()' - Get a password from the user.
c68b6ae8 461 */
462
03f61bf3 463const char * /* O - Password */
464_cupsGetPassword(const char *prompt) /* I - Prompt string */
c68b6ae8 465{
466 return (getpass(prompt));
6b67a15e 467}
e335a094 468#endif /* WIN32 */
6b67a15e 469
470
e93faa10 471/*
c9d3f842 472 * End of "$Id$".
6b67a15e 473 */