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