conf.c revision 1.14 1 /* $NetBSD: conf.c,v 1.14 1998/06/30 20:18:52 tv Exp $ */
2
3 /*-
4 * Copyright (c) 1997 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.14 1998/06/30 20:18:52 tv 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 #include "extern.h"
59 #include "pathnames.h"
60
61 struct ftpclass curclass;
62 static char *strend __P((const char *, char *));
63 static int filetypematch __P((char *, int));
64
65 /*
66 * Parse the configuration file, looking for the named class, and
67 * define curclass to contain the appropriate settings.
68 */
69 void
70 parse_conf(findclass)
71 char *findclass;
72 {
73 FILE *f;
74 char *buf, *p;
75 size_t len;
76 int none, match;
77 char *endp;
78 char *class, *word, *arg;
79 const char *infile;
80 int line;
81 unsigned int timeout;
82 struct ftpconv *conv, *cnext;
83
84 #define REASSIGN(X,Y) if (X) free(X); (X)=(Y)
85 #define NEXTWORD(W) while ((W = strsep(&buf, " \t")) != NULL && *W == '\0')
86 #define EMPTYSTR(W) (W == NULL || *W == '\0')
87
88 REASSIGN(curclass.classname, findclass);
89 for (conv = curclass.conversions; conv != NULL; conv=cnext) {
90 REASSIGN(conv->suffix, NULL);
91 REASSIGN(conv->types, NULL);
92 REASSIGN(conv->disable, NULL);
93 REASSIGN(conv->command, NULL);
94 cnext = conv->next;
95 free(conv);
96 }
97 curclass.checkportcmd = 0;
98 curclass.conversions = NULL;
99 REASSIGN(curclass.display, NULL);
100 curclass.maxtimeout = 7200; /* 2 hours */
101 curclass.modify = 1;
102 REASSIGN(curclass.notify, NULL);
103 curclass.passive = 1;
104 curclass.timeout = 900; /* 15 minutes */
105 curclass.umask = 027;
106
107 if (strcasecmp(findclass, "guest") == 0) {
108 curclass.modify = 0;
109 curclass.umask = 0707;
110 }
111
112 infile = conffilename(_PATH_FTPDCONF);
113 if ((f = fopen(infile, "r")) == NULL)
114 return;
115
116 line = 0;
117 while ((buf = fgetln(f, &len)) != NULL) {
118 none = match = 0;
119 line++;
120 if (len < 1)
121 continue;
122 if (buf[len - 1] != '\n') {
123 syslog(LOG_WARNING,
124 "%s line %d is partially truncated?", infile, line);
125 continue;
126 }
127 buf[--len] = '\0';
128 if ((p = strchr(buf, '#')) != NULL)
129 *p = '\0';
130 if (EMPTYSTR(buf))
131 continue;
132
133 NEXTWORD(word);
134 NEXTWORD(class);
135 NEXTWORD(arg);
136 if (EMPTYSTR(word) || EMPTYSTR(class))
137 continue;
138 if (strcasecmp(class, "none") == 0)
139 none = 1;
140 if (strcasecmp(class, findclass) != 0 &&
141 !none && strcasecmp(class, "all") != 0)
142 continue;
143
144 if (strcasecmp(word, "checkportcmd") == 0) {
145 if (none ||
146 (!EMPTYSTR(arg) && strcasecmp(arg, "off") == 0))
147 curclass.checkportcmd = 0;
148 else
149 curclass.checkportcmd = 1;
150 } else if (strcasecmp(word, "conversion") == 0) {
151 char *suffix, *types, *disable, *convcmd;
152
153 if (EMPTYSTR(arg)) {
154 syslog(LOG_WARNING,
155 "%s line %d: %s requires a suffix",
156 infile, line, word);
157 continue; /* need a suffix */
158 }
159 NEXTWORD(types);
160 NEXTWORD(disable);
161 convcmd = buf;
162 if (convcmd)
163 convcmd += strspn(convcmd, " \t");
164 suffix = strdup(arg);
165 if (suffix == NULL) {
166 syslog(LOG_WARNING, "can't strdup");
167 continue;
168 }
169 if (none || EMPTYSTR(types) ||
170 EMPTYSTR(disable) || EMPTYSTR(convcmd)) {
171 types = NULL;
172 disable = NULL;
173 convcmd = NULL;
174 } else {
175 types = strdup(types);
176 disable = strdup(disable);
177 convcmd = strdup(convcmd);
178 if (types == NULL || disable == NULL ||
179 convcmd == NULL) {
180 syslog(LOG_WARNING, "can't strdup");
181 if (types)
182 free(types);
183 if (disable)
184 free(disable);
185 if (convcmd)
186 free(convcmd);
187 continue;
188 }
189 }
190 for (conv = curclass.conversions; conv != NULL;
191 conv = conv->next) {
192 if (strcmp(conv->suffix, suffix) == 0)
193 break;
194 }
195 if (conv == NULL) {
196 conv = (struct ftpconv *)
197 calloc(1, sizeof(struct ftpconv));
198 if (conv == NULL) {
199 syslog(LOG_WARNING, "can't malloc");
200 continue;
201 }
202 conv->next = curclass.conversions;
203 curclass.conversions = conv;
204 }
205 REASSIGN(conv->suffix, suffix);
206 REASSIGN(conv->types, types);
207 REASSIGN(conv->disable, disable);
208 REASSIGN(conv->command, convcmd);
209 } else if (strcasecmp(word, "display") == 0) {
210 if (none || EMPTYSTR(arg))
211 arg = NULL;
212 else
213 arg = strdup(arg);
214 REASSIGN(curclass.display, arg);
215 } else if (strcasecmp(word, "maxtimeout") == 0) {
216 if (none || EMPTYSTR(arg))
217 continue;
218 timeout = (unsigned int)strtoul(arg, &endp, 10);
219 if (*endp != 0) {
220 syslog(LOG_WARNING,
221 "%s line %d: invalid maxtimeout %s",
222 infile, line, arg);
223 continue;
224 }
225 if (timeout < 30) {
226 syslog(LOG_WARNING,
227 "%s line %d: maxtimeout %d < 30 seconds",
228 infile, line, timeout);
229 continue;
230 }
231 if (timeout < curclass.timeout) {
232 syslog(LOG_WARNING,
233 "%s line %d: maxtimeout %d < timeout (%d)",
234 infile, line, timeout, curclass.timeout);
235 continue;
236 }
237 curclass.maxtimeout = timeout;
238 } else if (strcasecmp(word, "modify") == 0) {
239 if (none ||
240 (!EMPTYSTR(arg) && strcasecmp(arg, "off") == 0))
241 curclass.modify = 0;
242 else
243 curclass.modify = 1;
244 } else if (strcasecmp(word, "notify") == 0) {
245 if (none || EMPTYSTR(arg))
246 arg = NULL;
247 else
248 arg = strdup(arg);
249 REASSIGN(curclass.notify, arg);
250 } else if (strcasecmp(word, "passive") == 0) {
251 if (none ||
252 (!EMPTYSTR(arg) && strcasecmp(arg, "off") == 0))
253 curclass.passive = 0;
254 else
255 curclass.passive = 1;
256 } else if (strcasecmp(word, "timeout") == 0) {
257 if (none || EMPTYSTR(arg))
258 continue;
259 timeout = (unsigned int)strtoul(arg, &endp, 10);
260 if (*endp != 0) {
261 syslog(LOG_WARNING,
262 "%s line %d: invalid timeout %s",
263 infile, line, arg);
264 continue;
265 }
266 if (timeout < 30) {
267 syslog(LOG_WARNING,
268 "%s line %d: timeout %d < 30 seconds",
269 infile, line, timeout);
270 continue;
271 }
272 if (timeout > curclass.maxtimeout) {
273 syslog(LOG_WARNING,
274 "%s line %d: timeout %d > maxtimeout (%d)",
275 infile, line, timeout, curclass.maxtimeout);
276 continue;
277 }
278 curclass.timeout = timeout;
279 } else if (strcasecmp(word, "umask") == 0) {
280 mode_t umask;
281
282 if (none || EMPTYSTR(arg))
283 continue;
284 umask = (mode_t)strtoul(arg, &endp, 8);
285 if (*endp != 0 || umask > 0777) {
286 syslog(LOG_WARNING,
287 "%s line %d: invalid umask %s",
288 infile, line, arg);
289 continue;
290 }
291 curclass.umask = umask;
292 } else {
293 syslog(LOG_WARNING,
294 "%s line %d: unknown directive '%s'",
295 infile, line, word);
296 continue;
297 }
298 }
299 #undef REASSIGN
300 #undef NEXTWORD
301 #undef EMPTYSTR
302 fclose(f);
303 }
304
305 /*
306 * Show file listed in curclass.display first time in, and list all the
307 * files named in curclass.notify in the current directory. Send back
308 * responses with the "reply" prefix.
309 */
310 void
311 show_chdir_messages(code)
312 int code;
313 {
314 static StringList *slist = NULL;
315
316 struct stat st;
317 struct tm *t;
318 glob_t gl;
319 time_t now, then;
320 int age;
321 char cwd[MAXPATHLEN + 1];
322 char line[BUFSIZ];
323 char *cp, **rlist;
324 FILE *f;
325
326 /* Setup list for directory cache */
327 if (slist == NULL)
328 slist = sl_init();
329
330 /* Check if this directory has already been visited */
331 if (getcwd(cwd, sizeof(cwd) - 1) == NULL) {
332 syslog(LOG_WARNING, "can't getcwd: %s", strerror(errno));
333 return;
334 }
335 if (sl_find(slist, cwd) != NULL)
336 return;
337
338 cp = strdup(cwd);
339 if (cp == NULL) {
340 syslog(LOG_WARNING, "can't strdup");
341 return;
342 }
343 sl_add(slist, cp);
344
345 /* First check for a display file */
346 if (curclass.display != NULL && curclass.display[0] &&
347 (f = fopen(curclass.display, "r")) != NULL) {
348 while (fgets(line, BUFSIZ, f)) {
349 if ((cp = strchr(line, '\n')) != NULL)
350 *cp = '\0';
351 lreply(code, "%s", line);
352 }
353 fclose(f);
354 lreply(code, "");
355 }
356
357 /* Now see if there are any notify files */
358 if (curclass.notify == NULL || curclass.notify[0] == '\0')
359 return;
360
361 if (glob(curclass.notify, 0, NULL, &gl) != 0 || gl.gl_matchc == 0)
362 return;
363 time(&now);
364 for (rlist = gl.gl_pathv; *rlist != NULL; rlist++) {
365 if (stat(*rlist, &st) != 0)
366 continue;
367 if (!S_ISREG(st.st_mode))
368 continue;
369 then = st.st_mtime;
370 lreply(code, "Please read the file %s", *rlist);
371 t = localtime(&now);
372 age = 365 * t->tm_year + t->tm_yday;
373 t = localtime(&then);
374 age -= 365 * t->tm_year + t->tm_yday;
375 lreply(code, " it was last modified on %.24s - %d day%s ago",
376 ctime(&then), age, age == 1 ? "" : "s");
377 }
378 globfree(&gl);
379 }
380
381 /*
382 * Find s2 at the end of s1. If found, return a string up and up (but
383 * not including) s2, otherwise returns NULL.
384 */
385 static char *
386 strend(s1, s2)
387 const char *s1;
388 char *s2;
389 {
390 static char buf[MAXPATHLEN + 1];
391
392 char *start;
393 size_t l1, l2;
394
395 l1 = strlen(s1);
396 l2 = strlen(s2);
397
398 if (l2 >= l1)
399 return(NULL);
400
401 strncpy(buf, s1, MAXPATHLEN);
402 start = buf + (l1 - l2);
403
404 if (strcmp(start, s2) == 0) {
405 *start = '\0';
406 return(buf);
407 } else
408 return(NULL);
409 }
410
411 static int
412 filetypematch(types, mode)
413 char *types;
414 int mode;
415 {
416 for ( ; types[0] != '\0'; types++)
417 switch (*types) {
418 case 'd':
419 if (S_ISDIR(mode))
420 return(1);
421 break;
422 case 'f':
423 if (S_ISREG(mode))
424 return(1);
425 break;
426 }
427 return(0);
428 }
429
430 /*
431 * Look for a conversion. If we succeed, return a pointer to the
432 * command to execute for the conversion.
433 *
434 * The command is stored in a static array so there's no memory
435 * leak problems, and not too much to change in ftpd.c. This
436 * routine doesn't need to be re-entrant unless we start using a
437 * multi-threaded ftpd, and that's not likely for a while...
438 */
439 char *
440 do_conversion(fname)
441 const char *fname;
442 {
443 static char cmd[LINE_MAX];
444
445 struct ftpconv *cp;
446 struct stat st;
447 int o_errno;
448 char *base = NULL;
449
450 o_errno = errno;
451 for (cp = curclass.conversions; cp != NULL; cp = cp->next) {
452 if (cp->suffix == NULL) {
453 syslog(LOG_WARNING,
454 "cp->suffix==NULL in conv list; SHOULDN'T HAPPEN!");
455 continue;
456 }
457 if ((base = strend(fname, cp->suffix)) == NULL)
458 continue;
459 if (cp->types == NULL || cp->disable == NULL ||
460 cp->command == NULL)
461 continue;
462 /* Is it enabled? */
463 if (strcmp(cp->disable, ".") != 0 &&
464 stat(cp->disable, &st) == 0)
465 continue;
466 /* Does the base exist? */
467 if (stat(base, &st) < 0)
468 continue;
469 /* Is the file type ok */
470 if (!filetypematch(cp->types, st.st_mode))
471 continue;
472 break; /* "We have a winner!" */
473 }
474
475 /* If we got through the list, no conversion */
476 if (cp == NULL) {
477 errno = o_errno;
478 return(NULL);
479 }
480
481 snprintf(cmd, LINE_MAX, cp->command, base);
482 syslog(LOG_DEBUG, "get command: %s", cmd);
483 return(cmd);
484 }
485