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