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