]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/usersys.c
Add strlcat() and strlcpy() checks and emulation functions.
[thirdparty/cups.git] / cups / usersys.c
1 /*
2 * "$Id: usersys.c,v 1.17 2002/05/16 13:44:54 mike Exp $"
3 *
4 * User, system, and password routines for the Common UNIX Printing
5 * System (CUPS).
6 *
7 * Copyright 1997-2002 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-3111 USA
20 *
21 * Voice: (301) 373-9603
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 * cups_get_password() - Get a password from the user...
38 * cups_get_line() - Get a line from a file...
39 */
40
41 /*
42 * Include necessary headers...
43 */
44
45 #include "cups.h"
46 #include "string.h"
47 #include <stdlib.h>
48 #include <ctype.h>
49
50
51 /*
52 * Local functions...
53 */
54
55 static const char *cups_get_password(const char *prompt);
56 static char *cups_get_line(char *buf, int buflen, FILE *fp);
57
58
59 /*
60 * Local globals...
61 */
62
63 static http_encryption_t cups_encryption = (http_encryption_t)-1;
64 static char cups_user[65] = "",
65 cups_server[256] = "";
66 static const char *(*cups_pwdcb)(const char *) = cups_get_password;
67
68
69 /*
70 * 'cupsEncryption()' - Get the default encryption settings...
71 */
72
73 http_encryption_t
74 cupsEncryption(void)
75 {
76 FILE *fp; /* client.conf file */
77 char *encryption; /* CUPS_ENCRYPTION variable */
78 const char *home; /* Home directory of user */
79 static char line[1024]; /* Line from file */
80
81
82 /*
83 * First see if we have already set the encryption stuff...
84 */
85
86 if (cups_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 * Next check to see if we have a $HOME/.cupsrc or client.conf file...
96 */
97
98 if ((home = getenv("HOME")) != NULL)
99 {
100 snprintf(line, sizeof(line), "%s/.cupsrc", home);
101 fp = fopen(line, "r");
102 }
103 else
104 fp = NULL;
105
106 if (fp == NULL)
107 {
108 if ((home = getenv("CUPS_SERVERROOT")) != NULL)
109 {
110 snprintf(line, sizeof(line), "%s/client.conf", home);
111 fp = fopen(line, "r");
112 }
113 else
114 fp = fopen(CUPS_SERVERROOT "/client.conf", "r");
115 }
116
117 encryption = "IfRequested";
118
119 if (fp != NULL)
120 {
121 /*
122 * Read the config file and look for a ServerName line...
123 */
124
125 while (cups_get_line(line, sizeof(line), fp) != NULL)
126 if (strncmp(line, "Encryption ", 11) == 0)
127 {
128 /*
129 * Got it! Drop any trailing newline and find the name...
130 */
131
132 encryption = line + strlen(line) - 1;
133 if (*encryption == '\n')
134 *encryption = '\0';
135
136 for (encryption = line + 11; isspace(*encryption); encryption ++);
137 break;
138 }
139
140 fclose(fp);
141 }
142 }
143
144 /*
145 * Set the encryption preference...
146 */
147
148 if (strcasecmp(encryption, "never") == 0)
149 cups_encryption = HTTP_ENCRYPT_NEVER;
150 else if (strcasecmp(encryption, "always") == 0)
151 cups_encryption = HTTP_ENCRYPT_ALWAYS;
152 else if (strcasecmp(encryption, "required") == 0)
153 cups_encryption = HTTP_ENCRYPT_REQUIRED;
154 else
155 cups_encryption = HTTP_ENCRYPT_IF_REQUESTED;
156 }
157
158 return (cups_encryption);
159 }
160
161
162 /*
163 * 'cupsGetPassword()' - Get a password from the user...
164 */
165
166 const char * /* O - Password */
167 cupsGetPassword(const char *prompt) /* I - Prompt string */
168 {
169 return ((*cups_pwdcb)(prompt));
170 }
171
172
173 /*
174 * 'cupsSetEncryption()' - Set the encryption preference.
175 */
176
177 void
178 cupsSetEncryption(http_encryption_t e) /* I - New encryption preference */
179 {
180 cups_encryption = e;
181 }
182
183
184 /*
185 * 'cupsServer()' - Return the hostname of the default server...
186 */
187
188 const char * /* O - Server name */
189 cupsServer(void)
190 {
191 FILE *fp; /* client.conf file */
192 char *server; /* Pointer to server name */
193 const char *home; /* Home directory of user */
194 static char line[1024]; /* Line from file */
195
196
197 /*
198 * First see if we have already set the server name...
199 */
200
201 if (!cups_server[0])
202 {
203 /*
204 * Then see if the CUPS_SERVER environment variable is set...
205 */
206
207 if ((server = getenv("CUPS_SERVER")) == NULL)
208 {
209 /*
210 * Next check to see if we have a $HOME/.cupsrc or client.conf file...
211 */
212
213 if ((home = getenv("HOME")) != NULL)
214 {
215 snprintf(line, sizeof(line), "%s/.cupsrc", home);
216 fp = fopen(line, "r");
217 }
218 else
219 fp = NULL;
220
221 if (fp == NULL)
222 {
223 if ((home = getenv("CUPS_SERVERROOT")) != NULL)
224 {
225 snprintf(line, sizeof(line), "%s/client.conf", home);
226 fp = fopen(line, "r");
227 }
228 else
229 fp = fopen(CUPS_SERVERROOT "/client.conf", "r");
230 }
231
232 server = "localhost";
233
234 if (fp != NULL)
235 {
236 /*
237 * Read the config file and look for a ServerName line...
238 */
239
240 while (cups_get_line(line, sizeof(line), fp) != NULL)
241 if (strncmp(line, "ServerName ", 11) == 0)
242 {
243 /*
244 * Got it! Drop any trailing newline and find the name...
245 */
246
247 server = line + strlen(line) - 1;
248 if (*server == '\n')
249 *server = '\0';
250
251 for (server = line + 11; isspace(*server); server ++);
252 break;
253 }
254
255 fclose(fp);
256 }
257 }
258
259 /*
260 * Copy the server name over...
261 */
262
263 strlcpy(cups_server, server, sizeof(cups_server));
264 }
265
266 return (cups_server);
267 }
268
269
270 /*
271 * 'cupsSetPasswordCB()' - Set the password callback for CUPS.
272 */
273
274 void
275 cupsSetPasswordCB(const char *(*cb)(const char *)) /* I - Callback function */
276 {
277 if (cb == (const char *(*)(const char *))0)
278 cups_pwdcb = cups_get_password;
279 else
280 cups_pwdcb = cb;
281 }
282
283
284 /*
285 * 'cupsSetServer()' - Set the default server name...
286 */
287
288 void
289 cupsSetServer(const char *server) /* I - Server name */
290 {
291 if (server)
292 strlcpy(cups_server, server, sizeof(cups_server));
293 else
294 cups_server[0] = '\0';
295 }
296
297
298 /*
299 * 'cupsSetUser()' - Set the default user name...
300 */
301
302 void
303 cupsSetUser(const char *user) /* I - User name */
304 {
305 if (user)
306 strlcpy(cups_user, user, sizeof(cups_user));
307 else
308 cups_user[0] = '\0';
309 }
310
311
312 #if defined(WIN32) || defined(__EMX__)
313 /*
314 * WIN32 and OS/2 username and password stuff...
315 */
316
317 /*
318 * 'cupsUser()' - Return the current user's name.
319 */
320
321 const char * /* O - User name */
322 cupsUser(void)
323 {
324 if (!cups_user[0])
325 strcpy(cups_user, "WindowsUser");
326
327 return (cups_user);
328 }
329
330
331 /*
332 * 'cups_get_password()' - Get a password from the user...
333 */
334
335 static const char * /* O - Password */
336 cups_get_password(const char *prompt) /* I - Prompt string */
337 {
338 return (NULL);
339 }
340 #else
341 /*
342 * UNIX username and password stuff...
343 */
344
345 # include <pwd.h>
346
347 /*
348 * 'cupsUser()' - Return the current user's name.
349 */
350
351 const char * /* O - User name */
352 cupsUser(void)
353 {
354 struct passwd *pwd; /* User/password entry */
355
356
357 if (!cups_user[0])
358 {
359 /*
360 * Rewind the password file...
361 */
362
363 setpwent();
364
365 /*
366 * Lookup the password entry for the current user.
367 */
368
369 if ((pwd = getpwuid(getuid())) == NULL)
370 strcpy(cups_user, "unknown"); /* Unknown user! */
371 else
372 {
373 /*
374 * Copy the username...
375 */
376
377 setpwent();
378
379 strlcpy(cups_user, pwd->pw_name, sizeof(cups_user));
380 }
381
382 /*
383 * Rewind the password file again...
384 */
385
386 setpwent();
387 }
388
389 return (cups_user);
390 }
391
392
393 /*
394 * 'cups_get_password()' - Get a password from the user...
395 */
396
397 static const char * /* O - Password */
398 cups_get_password(const char *prompt) /* I - Prompt string */
399 {
400 return (getpass(prompt));
401 }
402 #endif /* WIN32 || __EMX__ */
403
404
405 /*
406 * 'cups_get_line()' - Get a line from a file.
407 */
408
409 static char * /* O - Line from file */
410 cups_get_line(char *buf, /* I - Line buffer */
411 int buflen, /* I - Size of line buffer */
412 FILE *fp) /* I - File to read from */
413 {
414 char *bufptr; /* Pointer to end of buffer */
415
416
417 /*
418 * Get the line from a file...
419 */
420
421 if (fgets(buf, buflen, fp) == NULL)
422 return (NULL);
423
424 /*
425 * Remove all trailing whitespace...
426 */
427
428 bufptr = buf + strlen(buf) - 1;
429 if (bufptr < buf)
430 return (NULL);
431
432 while (isspace(*bufptr) && bufptr >= buf)
433 *bufptr-- = '\0';
434
435 return (buf);
436 }
437
438
439 /*
440 * End of "$Id: usersys.c,v 1.17 2002/05/16 13:44:54 mike Exp $".
441 */