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