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