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