conf.c revision 1.21 1 /* $NetBSD: conf.c,v 1.21 1999/08/25 20:07:33 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Simon Burge and Luke Mewburn.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #ifndef lint
41 __RCSID("$NetBSD: conf.c,v 1.21 1999/08/25 20:07:33 christos Exp $");
42 #endif /* not lint */
43
44 #include <sys/types.h>
45 #include <sys/param.h>
46 #include <sys/stat.h>
47
48 #include <errno.h>
49 #include <glob.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <time.h>
53 #include <unistd.h>
54 #include <string.h>
55 #include <stringlist.h>
56 #include <syslog.h>
57
58 #ifdef KERBEROS5
59 #include <krb5/krb5.h>
60 #endif
61
62 #include "extern.h"
63 #include "pathnames.h"
64
65 static char *strend __P((const char *, char *));
66 static int filetypematch __P((char *, int));
67
68 struct ftpclass curclass;
69
70
71 /*
72 * Parse the configuration file, looking for the named class, and
73 * define curclass to contain the appropriate settings.
74 */
75 void
76 parse_conf(findclass)
77 char *findclass;
78 {
79 FILE *f;
80 char *buf, *p;
81 size_t len;
82 int none, match;
83 char *endp;
84 char *class, *word, *arg;
85 const char *infile;
86 int line;
87 unsigned int timeout;
88 struct ftpconv *conv, *cnext;
89
90 #define REASSIGN(X,Y) if (X) free(X); (X)=(Y)
91 #define NEXTWORD(W) while ((W = strsep(&buf, " \t")) != NULL && *W == '\0')
92 #define EMPTYSTR(W) (W == NULL || *W == '\0')
93
94 REASSIGN(curclass.classname, findclass);
95 for (conv = curclass.conversions; conv != NULL; conv=cnext) {
96 REASSIGN(conv->suffix, NULL);
97 REASSIGN(conv->types, NULL);
98 REASSIGN(conv->disable, NULL);
99 REASSIGN(conv->command, NULL);
100 cnext = conv->next;
101 free(conv);
102 }
103 curclass.checkportcmd = 0;
104 curclass.conversions = NULL;
105 REASSIGN(curclass.display, NULL);
106 curclass.maxtimeout = 7200; /* 2 hours */
107 curclass.modify = 1;
108 REASSIGN(curclass.notify, NULL);
109 curclass.passive = 1;
110 curclass.timeout = 900; /* 15 minutes */
111 curclass.umask = 027;
112
113 if (strcasecmp(findclass, "guest") == 0) {
114 curclass.modify = 0;
115 curclass.umask = 0707;
116 }
117
118 infile = conffilename(_PATH_FTPDCONF);
119 if ((f = fopen(infile, "r")) == NULL)
120 return;
121
122 line = 0;
123 while ((buf = fgetln(f, &len)) != NULL) {
124 none = match = 0;
125 line++;
126 if (len < 1)
127 continue;
128 if (buf[len - 1] != '\n') {
129 syslog(LOG_WARNING,
130 "%s line %d is partially truncated?", infile, line);
131 continue;
132 }
133 buf[--len] = '\0';
134 if ((p = strchr(buf, '#')) != NULL)
135 *p = '\0';
136 if (EMPTYSTR(buf))
137 continue;
138
139 NEXTWORD(word);
140 NEXTWORD(class);
141 NEXTWORD(arg);
142 if (EMPTYSTR(word) || EMPTYSTR(class))
143 continue;
144 if (strcasecmp(class, "none") == 0)
145 none = 1;
146 if (strcasecmp(class, findclass) != 0 &&
147 !none && strcasecmp(class, "all") != 0)
148 continue;
149
150 if (strcasecmp(word, "checkportcmd") == 0) {
151 if (none ||
152 (!EMPTYSTR(arg) && strcasecmp(arg, "off") == 0))
153 curclass.checkportcmd = 0;
154 else
155 curclass.checkportcmd = 1;
156 } else if (strcasecmp(word, "conversion") == 0) {
157 char *suffix, *types, *disable, *convcmd;
158
159 if (EMPTYSTR(arg)) {
160 syslog(LOG_WARNING,
161 "%s line %d: %s requires a suffix",
162 infile, line, word);
163 continue; /* need a suffix */
164 }
165 NEXTWORD(types);
166 NEXTWORD(disable);
167 convcmd = buf;
168 if (convcmd)
169 convcmd += strspn(convcmd, " \t");
170 suffix = strdup(arg);
171 if (suffix == NULL) {
172 syslog(LOG_WARNING, "can't strdup");
173 continue;
174 }
175 if (none || EMPTYSTR(types) ||
176 EMPTYSTR(disable) || EMPTYSTR(convcmd)) {
177 types = NULL;
178 disable = NULL;
179 convcmd = NULL;
180 } else {
181 types = strdup(types);
182 disable = strdup(disable);
183 convcmd = strdup(convcmd);
184 if (types == NULL || disable == NULL ||
185 convcmd == NULL) {
186 syslog(LOG_WARNING, "can't strdup");
187 if (types)
188 free(types);
189 if (disable)
190 free(disable);
191 if (convcmd)
192 free(convcmd);
193 continue;
194 }
195 }
196 for (conv = curclass.conversions; conv != NULL;
197 conv = conv->next) {
198 if (strcmp(conv->suffix, suffix) == 0)
199 break;
200 }
201 if (conv == NULL) {
202 conv = (struct ftpconv *)
203 calloc(1, sizeof(struct ftpconv));
204 if (conv == NULL) {
205 syslog(LOG_WARNING, "can't malloc");
206 continue;
207 }
208 conv->next = curclass.conversions;
209 curclass.conversions = conv;
210 }
211 REASSIGN(conv->suffix, suffix);
212 REASSIGN(conv->types, types);
213 REASSIGN(conv->disable, disable);
214 REASSIGN(conv->command, convcmd);
215 } else if (strcasecmp(word, "display") == 0) {
216 if (none || EMPTYSTR(arg))
217 arg = NULL;
218 else
219 arg = strdup(arg);
220 REASSIGN(curclass.display, arg);
221 } else if (strcasecmp(word, "maxtimeout") == 0) {
222 if (none || EMPTYSTR(arg))
223 continue;
224 timeout = (unsigned int)strtoul(arg, &endp, 10);
225 if (*endp != 0) {
226 syslog(LOG_WARNING,
227 "%s line %d: invalid maxtimeout %s",
228 infile, line, arg);
229 continue;
230 }
231 if (timeout < 30) {
232 syslog(LOG_WARNING,
233 "%s line %d: maxtimeout %d < 30 seconds",
234 infile, line, timeout);
235 continue;
236 }
237 if (timeout < curclass.timeout) {
238 syslog(LOG_WARNING,
239 "%s line %d: maxtimeout %d < timeout (%d)",
240 infile, line, timeout, curclass.timeout);
241 continue;
242 }
243 curclass.maxtimeout = timeout;
244 } else if (strcasecmp(word, "modify") == 0) {
245 if (none ||
246 (!EMPTYSTR(arg) && strcasecmp(arg, "off") == 0))
247 curclass.modify = 0;
248 else
249 curclass.modify = 1;
250 } else if (strcasecmp(word, "notify") == 0) {
251 if (none || EMPTYSTR(arg))
252 arg = NULL;
253 else
254 arg = strdup(arg);
255 REASSIGN(curclass.notify, arg);
256 } else if (strcasecmp(word, "passive") == 0) {
257 if (none ||
258 (!EMPTYSTR(arg) && strcasecmp(arg, "off") == 0))
259 curclass.passive = 0;
260 else
261 curclass.passive = 1;
262 } else if (strcasecmp(word, "timeout") == 0) {
263 if (none || EMPTYSTR(arg))
264 continue;
265 timeout = (unsigned int)strtoul(arg, &endp, 10);
266 if (*endp != 0) {
267 syslog(LOG_WARNING,
268 "%s line %d: invalid timeout %s",
269 infile, line, arg);
270 continue;
271 }
272 if (timeout < 30) {
273 syslog(LOG_WARNING,
274 "%s line %d: timeout %d < 30 seconds",
275 infile, line, timeout);
276 continue;
277 }
278 if (timeout > curclass.maxtimeout) {
279 syslog(LOG_WARNING,
280 "%s line %d: timeout %d > maxtimeout (%d)",
281 infile, line, timeout, curclass.maxtimeout);
282 continue;
283 }
284 curclass.timeout = timeout;
285 } else if (strcasecmp(word, "umask") == 0) {
286 mode_t umask;
287
288 if (none || EMPTYSTR(arg))
289 continue;
290 umask = (mode_t)strtoul(arg, &endp, 8);
291 if (*endp != 0 || umask > 0777) {
292 syslog(LOG_WARNING,
293 "%s line %d: invalid umask %s",
294 infile, line, arg);
295 continue;
296 }
297 curclass.umask = umask;
298 } else {
299 syslog(LOG_WARNING,
300 "%s line %d: unknown directive '%s'",
301 infile, line, word);
302 continue;
303 }
304 }
305 #undef REASSIGN
306 #undef NEXTWORD
307 #undef EMPTYSTR
308 fclose(f);
309 }
310
311 /*
312 * Show file listed in curclass.display first time in, and list all the
313 * files named in curclass.notify in the current directory. Send back
314 * responses with the prefix `code' + "-".
315 */
316 void
317 show_chdir_messages(code)
318 int code;
319 {
320 static StringList *slist = NULL;
321
322 struct stat st;
323 struct tm *t;
324 glob_t gl;
325 time_t now, then;
326 int age;
327 char cwd[MAXPATHLEN + 1];
328 char line[BUFSIZ];
329 char *cp, **rlist;
330 FILE *f;
331
332 /* Setup list for directory cache */
333 if (slist == NULL)
334 slist = sl_init();
335
336 /* Check if this directory has already been visited */
337 if (getcwd(cwd, sizeof(cwd) - 1) == NULL) {
338 syslog(LOG_WARNING, "can't getcwd: %s", strerror(errno));
339 return;
340 }
341 if (sl_find(slist, cwd) != NULL)
342 return;
343
344 cp = strdup(cwd);
345 if (cp == NULL) {
346 syslog(LOG_WARNING, "can't strdup");
347 return;
348 }
349 sl_add(slist, cp);
350
351 /* First check for a display file */
352 if (curclass.display != NULL && curclass.display[0] &&
353 (f = fopen(curclass.display, "r")) != NULL) {
354 lreply(code, "");
355 while (fgets(line, BUFSIZ, f)) {
356 if ((cp = strchr(line, '\n')) != NULL)
357 *cp = '\0';
358 lreply(0, "%s", line);
359 }
360 fclose(f);
361 }
362
363 /* Now see if there are any notify files */
364 if (curclass.notify == NULL || curclass.notify[0] == '\0')
365 return;
366
367 if (glob(curclass.notify, 0, NULL, &gl) != 0 || gl.gl_matchc == 0)
368 return;
369 time(&now);
370 for (rlist = gl.gl_pathv; *rlist != NULL; rlist++) {
371 if (stat(*rlist, &st) != 0)
372 continue;
373 if (!S_ISREG(st.st_mode))
374 continue;
375 then = st.st_mtime;
376 if (code != 0) {
377 lreply(code, "");
378 code = 0;
379 }
380 lreply(code, "Please read the file %s", *rlist);
381 t = localtime(&now);
382 age = 365 * t->tm_year + t->tm_yday;
383 t = localtime(&then);
384 age -= 365 * t->tm_year + t->tm_yday;
385 lreply(code, " it was last modified on %.24s - %d day%s ago",
386 ctime(&then), age, PLURAL(age));
387 }
388 globfree(&gl);
389 }
390
391 /*
392 * Find s2 at the end of s1. If found, return a string up and up (but
393 * not including) s2, otherwise returns NULL.
394 */
395 static char *
396 strend(s1, s2)
397 const char *s1;
398 char *s2;
399 {
400 static char buf[MAXPATHLEN + 1];
401
402 char *start;
403 size_t l1, l2;
404
405 l1 = strlen(s1);
406 l2 = strlen(s2);
407
408 if (l2 >= l1)
409 return(NULL);
410
411 strncpy(buf, s1, MAXPATHLEN);
412 start = buf + (l1 - l2);
413
414 if (strcmp(start, s2) == 0) {
415 *start = '\0';
416 return(buf);
417 } else
418 return(NULL);
419 }
420
421 static int
422 filetypematch(types, mode)
423 char *types;
424 int mode;
425 {
426 for ( ; types[0] != '\0'; types++)
427 switch (*types) {
428 case 'd':
429 if (S_ISDIR(mode))
430 return(1);
431 break;
432 case 'f':
433 if (S_ISREG(mode))
434 return(1);
435 break;
436 }
437 return(0);
438 }
439
440 /*
441 * Look for a conversion. If we succeed, return a pointer to the
442 * command to execute for the conversion.
443 *
444 * The command is stored in a static array so there's no memory
445 * leak problems, and not too much to change in ftpd.c. This
446 * routine doesn't need to be re-entrant unless we start using a
447 * multi-threaded ftpd, and that's not likely for a while...
448 */
449 char *
450 do_conversion(fname)
451 const char *fname;
452 {
453 static char cmd[LINE_MAX];
454
455 struct ftpconv *cp;
456 struct stat st;
457 int o_errno;
458 char *base = NULL;
459
460 o_errno = errno;
461 for (cp = curclass.conversions; cp != NULL; cp = cp->next) {
462 if (cp->suffix == NULL) {
463 syslog(LOG_WARNING,
464 "cp->suffix==NULL in conv list; SHOULDN'T HAPPEN!");
465 continue;
466 }
467 if ((base = strend(fname, cp->suffix)) == NULL)
468 continue;
469 if (cp->types == NULL || cp->disable == NULL ||
470 cp->command == NULL)
471 continue;
472 /* Is it enabled? */
473 if (strcmp(cp->disable, ".") != 0 &&
474 stat(cp->disable, &st) == 0)
475 continue;
476 /* Does the base exist? */
477 if (stat(base, &st) < 0)
478 continue;
479 /* Is the file type ok */
480 if (!filetypematch(cp->types, st.st_mode))
481 continue;
482 break; /* "We have a winner!" */
483 }
484
485 /* If we got through the list, no conversion */
486 if (cp == NULL) {
487 errno = o_errno;
488 return(NULL);
489 }
490
491 snprintf(cmd, LINE_MAX, cp->command, base);
492 return(cmd);
493 }
494