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