]> 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/*
e00b005a 2 * "$Id: usersys.c 5041 2006-02-01 16:54:50Z 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 *
150 * Returns NULL if the user does not provide a password.
151 */
152
153const char * /* O - Password */
154cupsGetPassword(const char *prompt) /* I - Prompt string */
155{
156 return ((*_cupsGlobals()->password_cb)(prompt));
157}
158
159
160/*
161 * 'cupsSetEncryption()' - Set the encryption preference.
162 */
163
164void
165cupsSetEncryption(http_encryption_t e) /* I - New encryption preference */
166{
167 _cupsGlobals()->encryption = e;
168}
169
170
171/*
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.
176 */
177
178const char * /* O - Server name */
179cupsServer(void)
180{
181 cups_file_t *fp; /* client.conf file */
182 char *server; /* Pointer to server name */
183 const char *home; /* Home directory of user */
184 char *port; /* Port number */
185 char line[1024], /* Line from file */
186 *value; /* Value on line */
187 int linenum; /* Line number in file */
e00b005a 188#ifdef CUPS_DEFAULT_DOMAINSOCKET
189 struct stat sockinfo; /* Domain socket information */
190#endif /* CUPS_DEFAULT_DOMAINSOCKET */
ef416fc2 191 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
192
193
194 /*
195 * First see if we have already set the server name...
196 */
197
198 if (!cg->server[0])
199 {
200 /*
201 * Then see if the CUPS_SERVER environment variable is set...
202 */
203
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 {
212 snprintf(line, sizeof(line), "%s/.cupsrc", home);
213 fp = cupsFileOpen(line, "r");
214 }
215 else
216 fp = NULL;
217
218 if (fp == NULL)
219 {
220 snprintf(line, sizeof(line), "%s/client.conf", cg->cups_serverroot);
221 fp = cupsFileOpen(line, "r");
222 }
223
224#ifdef CUPS_DEFAULT_DOMAINSOCKET
e00b005a 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)
ef416fc2 232 server = CUPS_DEFAULT_DOMAINSOCKET;
233 else
234#endif /* CUPS_DEFAULT_DOMAINSOCKET */
235 server = "localhost";
236
237 if (fp != NULL)
238 {
239 /*
240 * Read the config file and look for a ServerName line...
241 */
242
243 linenum = 0;
244 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum) != NULL)
245 if (!strcasecmp(line, "ServerName") && value)
246 {
247 /*
248 * Got it!
249 */
250
251 server = value;
252 break;
253 }
254
255 cupsFileClose(fp);
256 }
257 }
258
259 /*
260 * Copy the server name over and set the port number, if any...
261 */
262
263 strlcpy(cg->server, server, sizeof(cg->server));
264
265 if (cg->server[0] != '/' && (port = strrchr(cg->server, ':')) != NULL &&
266 !strchr(port, ']') && isdigit(port[1] & 255))
267 {
268 *port++ = '\0';
269
270 ippSetPort(atoi(port));
271 }
272
273 if (cg->server[0] == '/')
274 strcpy(cg->servername, "localhost");
275 else
276 strlcpy(cg->servername, cg->server, sizeof(cg->servername));
277 }
278
279 return (cg->server);
280}
281
282
283/*
284 * 'cupsSetPasswordCB()' - Set the password callback for CUPS.
285 *
286 * Pass NULL to restore the default (console) password callback.
287 */
288
289void
290cupsSetPasswordCB(cups_password_cb_t cb)/* I - Callback function */
291{
292 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
293
294
295 if (cb == (const char *(*)(const char *))0)
296 cg->password_cb = _cupsGetPassword;
297 else
298 cg->password_cb = cb;
299}
300
301
302/*
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.
308 */
309
310void
311cupsSetServer(const char *server) /* I - Server name */
312{
313 char *port; /* Pointer to port */
314 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
315
316
317 if (server)
318 {
319 strlcpy(cg->server, server, sizeof(cg->server));
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 }
334 else
335 {
336 cg->server[0] = '\0';
337 cg->servername[0] = '\0';
338 }
339}
340
341
342/*
343 * 'cupsSetUser()' - Set the default user name.
344 *
345 * Pass NULL to restore the default user name.
346 */
347
348void
349cupsSetUser(const char *user) /* I - User name */
350{
351 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
352
353
354 if (user)
355 strlcpy(cg->user, user, sizeof(cg->user));
356 else
357 cg->user[0] = '\0';
358}
359
360
361#if defined(WIN32)
362/*
363 * WIN32 username and password stuff.
364 */
365
366/*
367 * 'cupsUser()' - Return the current user's name.
368 */
369
370const char * /* O - User name */
371cupsUser(void)
372{
373 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
374
375
376 if (!cg->user[0])
377 {
378 DWORD size; /* Size of string */
379
380
381 size = sizeof(cg->user);
382 if (!GetUserName(cg->user, &size))
383 {
384 /*
385 * Use the default username...
386 */
387
388 strcpy(cg->user, "unknown");
389 }
390 }
391
392 return (cg->user);
393}
394
395
396/*
397 * '_cupsGetPassword()' - Get a password from the user.
398 */
399
400const char * /* O - Password */
401_cupsGetPassword(const char *prompt) /* I - Prompt string */
402{
403 return (NULL);
404}
405#else
406/*
407 * UNIX username and password stuff...
408 */
409
410# include <pwd.h>
411
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 */
420 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
421
422
423 if (!cg->user[0])
424 {
425 /*
426 * Rewind the password file...
427 */
428
429 setpwent();
430
431 /*
432 * Lookup the password entry for the current user.
433 */
434
435 if ((pwd = getpwuid(getuid())) == NULL)
436 strcpy(cg->user, "unknown"); /* Unknown user! */
437 else
438 {
439 /*
440 * Copy the username...
441 */
442
443 setpwent();
444
445 strlcpy(cg->user, pwd->pw_name, sizeof(cg->user));
446 }
447
448 /*
449 * Rewind the password file again...
450 */
451
452 setpwent();
453 }
454
455 return (cg->user);
456}
457
458
459/*
460 * '_cupsGetPassword()' - Get a password from the user.
461 */
462
463const char * /* O - Password */
464_cupsGetPassword(const char *prompt) /* I - Prompt string */
465{
466 return (getpass(prompt));
467}
468#endif /* WIN32 */
469
470
471/*
e00b005a 472 * End of "$Id: usersys.c 5041 2006-02-01 16:54:50Z mike $".
ef416fc2 473 */