conf.c revision 1.29 1 1.29 lukem /* $NetBSD: conf.c,v 1.29 2000/01/13 00:04:31 lukem Exp $ */
2 1.1 lukem
3 1.1 lukem /*-
4 1.25 lukem * Copyright (c) 1997-2000 The NetBSD Foundation, Inc.
5 1.1 lukem * All rights reserved.
6 1.1 lukem *
7 1.1 lukem * This code is derived from software contributed to The NetBSD Foundation
8 1.1 lukem * by Simon Burge and Luke Mewburn.
9 1.1 lukem *
10 1.1 lukem * Redistribution and use in source and binary forms, with or without
11 1.1 lukem * modification, are permitted provided that the following conditions
12 1.1 lukem * are met:
13 1.1 lukem * 1. Redistributions of source code must retain the above copyright
14 1.1 lukem * notice, this list of conditions and the following disclaimer.
15 1.1 lukem * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 lukem * notice, this list of conditions and the following disclaimer in the
17 1.1 lukem * documentation and/or other materials provided with the distribution.
18 1.1 lukem * 3. All advertising materials mentioning features or use of this software
19 1.1 lukem * must display the following acknowledgement:
20 1.1 lukem * This product includes software developed by the NetBSD
21 1.1 lukem * Foundation, Inc. and its contributors.
22 1.1 lukem * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 lukem * contributors may be used to endorse or promote products derived
24 1.1 lukem * from this software without specific prior written permission.
25 1.1 lukem *
26 1.1 lukem * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 lukem * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 lukem * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.4 jtc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.4 jtc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 lukem * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 lukem * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 lukem * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 lukem * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 lukem * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 lukem * POSSIBILITY OF SUCH DAMAGE.
37 1.1 lukem */
38 1.1 lukem
39 1.2 christos #include <sys/cdefs.h>
40 1.1 lukem #ifndef lint
41 1.29 lukem __RCSID("$NetBSD: conf.c,v 1.29 2000/01/13 00:04:31 lukem Exp $");
42 1.1 lukem #endif /* not lint */
43 1.1 lukem
44 1.1 lukem #include <sys/types.h>
45 1.1 lukem #include <sys/param.h>
46 1.1 lukem #include <sys/stat.h>
47 1.1 lukem
48 1.24 lukem #include <ctype.h>
49 1.1 lukem #include <errno.h>
50 1.25 lukem #include <fcntl.h>
51 1.1 lukem #include <glob.h>
52 1.28 lukem #include <setjmp.h>
53 1.25 lukem #include <signal.h>
54 1.1 lukem #include <stdio.h>
55 1.2 christos #include <stdlib.h>
56 1.1 lukem #include <string.h>
57 1.1 lukem #include <stringlist.h>
58 1.1 lukem #include <syslog.h>
59 1.23 lukem #include <time.h>
60 1.23 lukem #include <unistd.h>
61 1.23 lukem #include <util.h>
62 1.18 explorer
63 1.18 explorer #ifdef KERBEROS5
64 1.21 christos #include <krb5/krb5.h>
65 1.18 explorer #endif
66 1.1 lukem
67 1.1 lukem #include "extern.h"
68 1.1 lukem #include "pathnames.h"
69 1.1 lukem
70 1.2 christos static char *strend __P((const char *, char *));
71 1.2 christos static int filetypematch __P((char *, int));
72 1.15 lukem
73 1.1 lukem
74 1.1 lukem /*
75 1.25 lukem * Initialise curclass to an `empty' state
76 1.1 lukem */
77 1.1 lukem void
78 1.25 lukem init_curclass()
79 1.1 lukem {
80 1.1 lukem struct ftpconv *conv, *cnext;
81 1.1 lukem
82 1.23 lukem for (conv = curclass.conversions; conv != NULL; conv = cnext) {
83 1.1 lukem REASSIGN(conv->suffix, NULL);
84 1.1 lukem REASSIGN(conv->types, NULL);
85 1.1 lukem REASSIGN(conv->disable, NULL);
86 1.1 lukem REASSIGN(conv->command, NULL);
87 1.1 lukem cnext = conv->next;
88 1.1 lukem free(conv);
89 1.1 lukem }
90 1.28 lukem
91 1.9 lukem curclass.checkportcmd = 0;
92 1.25 lukem REASSIGN(curclass.classname, NULL);
93 1.1 lukem curclass.conversions = NULL;
94 1.1 lukem REASSIGN(curclass.display, NULL);
95 1.25 lukem curclass.limit = -1; /* unlimited connections */
96 1.25 lukem REASSIGN(curclass.limitfile, NULL);
97 1.24 lukem curclass.maxrateget = 0;
98 1.24 lukem curclass.maxrateput = 0;
99 1.9 lukem curclass.maxtimeout = 7200; /* 2 hours */
100 1.1 lukem curclass.modify = 1;
101 1.24 lukem REASSIGN(curclass.motd, xstrdup(_PATH_FTPLOGINMESG));
102 1.1 lukem REASSIGN(curclass.notify, NULL);
103 1.14 tv curclass.passive = 1;
104 1.28 lukem curclass.portmin = 0;
105 1.28 lukem curclass.portmax = 0;
106 1.24 lukem curclass.rateget = 0;
107 1.24 lukem curclass.rateput = 0;
108 1.1 lukem curclass.timeout = 900; /* 15 minutes */
109 1.1 lukem curclass.umask = 027;
110 1.24 lukem curclass.upload = 1;
111 1.25 lukem }
112 1.25 lukem
113 1.25 lukem /*
114 1.25 lukem * Parse the configuration file, looking for the named class, and
115 1.25 lukem * define curclass to contain the appropriate settings.
116 1.25 lukem */
117 1.25 lukem void
118 1.25 lukem parse_conf(findclass)
119 1.25 lukem char *findclass;
120 1.25 lukem {
121 1.25 lukem FILE *f;
122 1.25 lukem char *buf, *p;
123 1.25 lukem size_t len;
124 1.25 lukem int none, match, rate;
125 1.25 lukem char *endp;
126 1.26 lukem char *class, *word, *arg, *template;
127 1.25 lukem const char *infile;
128 1.25 lukem size_t line;
129 1.25 lukem unsigned int timeout;
130 1.25 lukem struct ftpconv *conv, *cnext;
131 1.25 lukem
132 1.25 lukem init_curclass();
133 1.25 lukem REASSIGN(curclass.classname, xstrdup(findclass));
134 1.1 lukem if (strcasecmp(findclass, "guest") == 0) {
135 1.9 lukem curclass.modify = 0;
136 1.1 lukem curclass.umask = 0707;
137 1.1 lukem }
138 1.1 lukem
139 1.6 lukem infile = conffilename(_PATH_FTPDCONF);
140 1.1 lukem if ((f = fopen(infile, "r")) == NULL)
141 1.1 lukem return;
142 1.1 lukem
143 1.1 lukem line = 0;
144 1.26 lukem template = NULL;
145 1.23 lukem for (;
146 1.23 lukem (buf = fparseln(f, &len, &line, NULL, FPARSELN_UNESCCOMM |
147 1.23 lukem FPARSELN_UNESCCONT | FPARSELN_UNESCESC)) != NULL;
148 1.23 lukem free(buf)) {
149 1.1 lukem none = match = 0;
150 1.23 lukem p = buf;
151 1.5 lukem if (len < 1)
152 1.5 lukem continue;
153 1.23 lukem if (p[len - 1] == '\n')
154 1.23 lukem p[--len] = '\0';
155 1.23 lukem if (EMPTYSTR(p))
156 1.1 lukem continue;
157 1.1 lukem
158 1.23 lukem NEXTWORD(p, word);
159 1.23 lukem NEXTWORD(p, class);
160 1.23 lukem NEXTWORD(p, arg);
161 1.1 lukem if (EMPTYSTR(word) || EMPTYSTR(class))
162 1.1 lukem continue;
163 1.1 lukem if (strcasecmp(class, "none") == 0)
164 1.1 lukem none = 1;
165 1.27 lukem if (! (strcasecmp(class, findclass) == 0 ||
166 1.27 lukem (template != NULL && strcasecmp(class, template) == 0) ||
167 1.27 lukem none ||
168 1.27 lukem strcasecmp(class, "all") == 0) )
169 1.1 lukem continue;
170 1.1 lukem
171 1.9 lukem if (strcasecmp(word, "checkportcmd") == 0) {
172 1.9 lukem if (none ||
173 1.9 lukem (!EMPTYSTR(arg) && strcasecmp(arg, "off") == 0))
174 1.9 lukem curclass.checkportcmd = 0;
175 1.9 lukem else
176 1.9 lukem curclass.checkportcmd = 1;
177 1.23 lukem
178 1.24 lukem } else if (strcasecmp(word, "classtype") == 0) {
179 1.24 lukem if (!none && !EMPTYSTR(arg)) {
180 1.24 lukem if (strcasecmp(arg, "GUEST") == 0)
181 1.24 lukem curclass.type = CLASS_GUEST;
182 1.24 lukem else if (strcasecmp(arg, "CHROOT") == 0)
183 1.24 lukem curclass.type = CLASS_CHROOT;
184 1.24 lukem else if (strcasecmp(arg, "REAL") == 0)
185 1.24 lukem curclass.type = CLASS_REAL;
186 1.24 lukem else {
187 1.24 lukem syslog(LOG_WARNING,
188 1.24 lukem "%s line %d: unknown class type `%s'",
189 1.24 lukem infile, (int)line, arg);
190 1.24 lukem continue;
191 1.24 lukem }
192 1.24 lukem }
193 1.24 lukem
194 1.9 lukem } else if (strcasecmp(word, "conversion") == 0) {
195 1.5 lukem char *suffix, *types, *disable, *convcmd;
196 1.5 lukem
197 1.1 lukem if (EMPTYSTR(arg)) {
198 1.1 lukem syslog(LOG_WARNING,
199 1.1 lukem "%s line %d: %s requires a suffix",
200 1.23 lukem infile, (int)line, word);
201 1.1 lukem continue; /* need a suffix */
202 1.1 lukem }
203 1.23 lukem NEXTWORD(p, types);
204 1.23 lukem NEXTWORD(p, disable);
205 1.23 lukem convcmd = p;
206 1.1 lukem if (convcmd)
207 1.1 lukem convcmd += strspn(convcmd, " \t");
208 1.23 lukem suffix = xstrdup(arg);
209 1.1 lukem if (none || EMPTYSTR(types) ||
210 1.1 lukem EMPTYSTR(disable) || EMPTYSTR(convcmd)) {
211 1.1 lukem types = NULL;
212 1.1 lukem disable = NULL;
213 1.1 lukem convcmd = NULL;
214 1.1 lukem } else {
215 1.23 lukem types = xstrdup(types);
216 1.23 lukem disable = xstrdup(disable);
217 1.23 lukem convcmd = xstrdup(convcmd);
218 1.1 lukem }
219 1.1 lukem for (conv = curclass.conversions; conv != NULL;
220 1.1 lukem conv = conv->next) {
221 1.5 lukem if (strcmp(conv->suffix, suffix) == 0)
222 1.1 lukem break;
223 1.1 lukem }
224 1.1 lukem if (conv == NULL) {
225 1.1 lukem conv = (struct ftpconv *)
226 1.1 lukem calloc(1, sizeof(struct ftpconv));
227 1.1 lukem if (conv == NULL) {
228 1.1 lukem syslog(LOG_WARNING, "can't malloc");
229 1.1 lukem continue;
230 1.1 lukem }
231 1.23 lukem conv->next = NULL;
232 1.23 lukem for (cnext = curclass.conversions;
233 1.23 lukem cnext != NULL; cnext = cnext->next)
234 1.23 lukem if (cnext->next == NULL)
235 1.23 lukem break;
236 1.23 lukem if (cnext != NULL)
237 1.23 lukem cnext->next = conv;
238 1.23 lukem else
239 1.23 lukem curclass.conversions = conv;
240 1.1 lukem }
241 1.5 lukem REASSIGN(conv->suffix, suffix);
242 1.1 lukem REASSIGN(conv->types, types);
243 1.1 lukem REASSIGN(conv->disable, disable);
244 1.1 lukem REASSIGN(conv->command, convcmd);
245 1.23 lukem
246 1.1 lukem } else if (strcasecmp(word, "display") == 0) {
247 1.1 lukem if (none || EMPTYSTR(arg))
248 1.1 lukem arg = NULL;
249 1.1 lukem else
250 1.23 lukem arg = xstrdup(arg);
251 1.1 lukem REASSIGN(curclass.display, arg);
252 1.23 lukem
253 1.25 lukem } else if (strcasecmp(word, "limit") == 0) {
254 1.25 lukem int limit;
255 1.25 lukem
256 1.25 lukem if (none || EMPTYSTR(arg))
257 1.25 lukem continue;
258 1.25 lukem limit = (int)strtol(arg, &endp, 10);
259 1.25 lukem if (*endp != 0) {
260 1.25 lukem syslog(LOG_WARNING,
261 1.25 lukem "%s line %d: invalid limit %s",
262 1.25 lukem infile, (int)line, arg);
263 1.25 lukem continue;
264 1.25 lukem }
265 1.25 lukem curclass.limit = limit;
266 1.26 lukem REASSIGN(curclass.limitfile,
267 1.26 lukem EMPTYSTR(p) ? NULL : xstrdup(p));
268 1.25 lukem
269 1.1 lukem } else if (strcasecmp(word, "maxtimeout") == 0) {
270 1.1 lukem if (none || EMPTYSTR(arg))
271 1.1 lukem continue;
272 1.1 lukem timeout = (unsigned int)strtoul(arg, &endp, 10);
273 1.1 lukem if (*endp != 0) {
274 1.1 lukem syslog(LOG_WARNING,
275 1.1 lukem "%s line %d: invalid maxtimeout %s",
276 1.23 lukem infile, (int)line, arg);
277 1.1 lukem continue;
278 1.1 lukem }
279 1.1 lukem if (timeout < 30) {
280 1.1 lukem syslog(LOG_WARNING,
281 1.1 lukem "%s line %d: maxtimeout %d < 30 seconds",
282 1.23 lukem infile, (int)line, timeout);
283 1.1 lukem continue;
284 1.1 lukem }
285 1.1 lukem if (timeout < curclass.timeout) {
286 1.1 lukem syslog(LOG_WARNING,
287 1.1 lukem "%s line %d: maxtimeout %d < timeout (%d)",
288 1.23 lukem infile, (int)line, timeout,
289 1.23 lukem curclass.timeout);
290 1.1 lukem continue;
291 1.1 lukem }
292 1.1 lukem curclass.maxtimeout = timeout;
293 1.23 lukem
294 1.1 lukem } else if (strcasecmp(word, "modify") == 0) {
295 1.1 lukem if (none ||
296 1.2 christos (!EMPTYSTR(arg) && strcasecmp(arg, "off") == 0))
297 1.1 lukem curclass.modify = 0;
298 1.1 lukem else
299 1.1 lukem curclass.modify = 1;
300 1.23 lukem
301 1.24 lukem } else if (strcasecmp(word, "motd") == 0) {
302 1.24 lukem if (none || EMPTYSTR(arg))
303 1.24 lukem arg = NULL;
304 1.24 lukem else
305 1.24 lukem arg = xstrdup(arg);
306 1.24 lukem REASSIGN(curclass.motd, arg);
307 1.24 lukem
308 1.24 lukem
309 1.1 lukem } else if (strcasecmp(word, "notify") == 0) {
310 1.1 lukem if (none || EMPTYSTR(arg))
311 1.1 lukem arg = NULL;
312 1.1 lukem else
313 1.23 lukem arg = xstrdup(arg);
314 1.1 lukem REASSIGN(curclass.notify, arg);
315 1.23 lukem
316 1.14 tv } else if (strcasecmp(word, "passive") == 0) {
317 1.14 tv if (none ||
318 1.14 tv (!EMPTYSTR(arg) && strcasecmp(arg, "off") == 0))
319 1.14 tv curclass.passive = 0;
320 1.14 tv else
321 1.14 tv curclass.passive = 1;
322 1.28 lukem
323 1.28 lukem } else if (strcasecmp(word, "portrange") == 0) {
324 1.28 lukem int minport, maxport;
325 1.28 lukem char *min, *max;
326 1.28 lukem
327 1.28 lukem if (none) {
328 1.28 lukem curclass.portmin = 0;
329 1.28 lukem curclass.portmax = 0;
330 1.28 lukem continue;
331 1.28 lukem }
332 1.28 lukem if (EMPTYSTR(arg))
333 1.28 lukem continue;
334 1.28 lukem min = arg;
335 1.28 lukem NEXTWORD(p, max);
336 1.28 lukem if (EMPTYSTR(max)) {
337 1.28 lukem syslog(LOG_WARNING,
338 1.28 lukem "%s line %d: missing maxport argument",
339 1.28 lukem infile, (int)line);
340 1.28 lukem continue;
341 1.28 lukem }
342 1.28 lukem minport = (int)strtol(min, &endp, 10);
343 1.28 lukem if (*endp != 0 || minport < IPPORT_RESERVED ||
344 1.28 lukem minport > IPPORT_ANONMAX) {
345 1.28 lukem syslog(LOG_WARNING,
346 1.28 lukem "%s line %d: invalid minport %s",
347 1.28 lukem infile, (int)line, min);
348 1.28 lukem continue;
349 1.28 lukem }
350 1.28 lukem maxport = (int)strtol(max, &endp, 10);
351 1.28 lukem if (*endp != 0 || maxport < IPPORT_RESERVED ||
352 1.28 lukem maxport > IPPORT_ANONMAX) {
353 1.28 lukem syslog(LOG_WARNING,
354 1.28 lukem "%s line %d: invalid maxport %s",
355 1.28 lukem infile, (int)line, max);
356 1.28 lukem continue;
357 1.28 lukem }
358 1.28 lukem if (minport >= maxport) {
359 1.28 lukem syslog(LOG_WARNING,
360 1.28 lukem "%s line %d: minport %d >= maxport %d",
361 1.28 lukem infile, (int)line, minport, maxport);
362 1.28 lukem continue;
363 1.28 lukem }
364 1.28 lukem curclass.portmin = minport;
365 1.28 lukem curclass.portmax = maxport;
366 1.23 lukem
367 1.24 lukem } else if (strcasecmp(word, "rateget") == 0) {
368 1.24 lukem if (none || EMPTYSTR(arg))
369 1.24 lukem continue;
370 1.24 lukem rate = strsuftoi(arg);
371 1.24 lukem if (rate == -1) {
372 1.24 lukem syslog(LOG_WARNING,
373 1.24 lukem "%s line %d: invalid rateget %s",
374 1.24 lukem infile, (int)line, arg);
375 1.24 lukem continue;
376 1.24 lukem }
377 1.24 lukem curclass.maxrateget = rate;
378 1.24 lukem curclass.rateget = rate;
379 1.24 lukem
380 1.24 lukem } else if (strcasecmp(word, "rateput") == 0) {
381 1.24 lukem if (none || EMPTYSTR(arg))
382 1.24 lukem continue;
383 1.24 lukem rate = strsuftoi(arg);
384 1.24 lukem if (rate == -1) {
385 1.24 lukem syslog(LOG_WARNING,
386 1.24 lukem "%s line %d: invalid rateput %s",
387 1.24 lukem infile, (int)line, arg);
388 1.24 lukem continue;
389 1.24 lukem }
390 1.24 lukem curclass.maxrateput = rate;
391 1.24 lukem curclass.rateput = rate;
392 1.24 lukem
393 1.1 lukem } else if (strcasecmp(word, "timeout") == 0) {
394 1.1 lukem if (none || EMPTYSTR(arg))
395 1.1 lukem continue;
396 1.1 lukem timeout = (unsigned int)strtoul(arg, &endp, 10);
397 1.1 lukem if (*endp != 0) {
398 1.1 lukem syslog(LOG_WARNING,
399 1.1 lukem "%s line %d: invalid timeout %s",
400 1.23 lukem infile, (int)line, arg);
401 1.1 lukem continue;
402 1.1 lukem }
403 1.1 lukem if (timeout < 30) {
404 1.1 lukem syslog(LOG_WARNING,
405 1.1 lukem "%s line %d: timeout %d < 30 seconds",
406 1.23 lukem infile, (int)line, timeout);
407 1.1 lukem continue;
408 1.1 lukem }
409 1.1 lukem if (timeout > curclass.maxtimeout) {
410 1.1 lukem syslog(LOG_WARNING,
411 1.1 lukem "%s line %d: timeout %d > maxtimeout (%d)",
412 1.23 lukem infile, (int)line, timeout,
413 1.23 lukem curclass.maxtimeout);
414 1.1 lukem continue;
415 1.1 lukem }
416 1.1 lukem curclass.timeout = timeout;
417 1.23 lukem
418 1.26 lukem } else if (strcasecmp(word, "template") == 0) {
419 1.26 lukem if (none)
420 1.26 lukem continue;
421 1.26 lukem REASSIGN(template, EMPTYSTR(arg) ? NULL : xstrdup(arg));
422 1.26 lukem
423 1.1 lukem } else if (strcasecmp(word, "umask") == 0) {
424 1.1 lukem mode_t umask;
425 1.1 lukem
426 1.1 lukem if (none || EMPTYSTR(arg))
427 1.1 lukem continue;
428 1.1 lukem umask = (mode_t)strtoul(arg, &endp, 8);
429 1.1 lukem if (*endp != 0 || umask > 0777) {
430 1.1 lukem syslog(LOG_WARNING,
431 1.1 lukem "%s line %d: invalid umask %s",
432 1.23 lukem infile, (int)line, arg);
433 1.1 lukem continue;
434 1.1 lukem }
435 1.1 lukem curclass.umask = umask;
436 1.23 lukem
437 1.24 lukem } else if (strcasecmp(word, "upload") == 0) {
438 1.24 lukem if (none ||
439 1.24 lukem (!EMPTYSTR(arg) && strcasecmp(arg, "off") == 0)) {
440 1.24 lukem curclass.modify = 0;
441 1.24 lukem curclass.upload = 0;
442 1.24 lukem } else
443 1.24 lukem curclass.upload = 1;
444 1.24 lukem
445 1.1 lukem } else {
446 1.1 lukem syslog(LOG_WARNING,
447 1.1 lukem "%s line %d: unknown directive '%s'",
448 1.23 lukem infile, (int)line, word);
449 1.1 lukem continue;
450 1.1 lukem }
451 1.1 lukem }
452 1.26 lukem REASSIGN(template, NULL);
453 1.1 lukem fclose(f);
454 1.1 lukem }
455 1.1 lukem
456 1.1 lukem /*
457 1.1 lukem * Show file listed in curclass.display first time in, and list all the
458 1.1 lukem * files named in curclass.notify in the current directory. Send back
459 1.17 lukem * responses with the prefix `code' + "-".
460 1.1 lukem */
461 1.1 lukem void
462 1.1 lukem show_chdir_messages(code)
463 1.1 lukem int code;
464 1.1 lukem {
465 1.1 lukem static StringList *slist = NULL;
466 1.1 lukem
467 1.1 lukem struct stat st;
468 1.1 lukem struct tm *t;
469 1.1 lukem glob_t gl;
470 1.1 lukem time_t now, then;
471 1.1 lukem int age;
472 1.25 lukem char cwd[MAXPATHLEN];
473 1.1 lukem char *cp, **rlist;
474 1.1 lukem
475 1.29 lukem if (quietmessages)
476 1.29 lukem return;
477 1.29 lukem
478 1.1 lukem /* Setup list for directory cache */
479 1.1 lukem if (slist == NULL)
480 1.1 lukem slist = sl_init();
481 1.22 lukem if (slist == NULL) {
482 1.22 lukem syslog(LOG_WARNING, "can't allocate memory for stringlist");
483 1.22 lukem return;
484 1.22 lukem }
485 1.1 lukem
486 1.1 lukem /* Check if this directory has already been visited */
487 1.1 lukem if (getcwd(cwd, sizeof(cwd) - 1) == NULL) {
488 1.13 mouse syslog(LOG_WARNING, "can't getcwd: %s", strerror(errno));
489 1.1 lukem return;
490 1.1 lukem }
491 1.1 lukem if (sl_find(slist, cwd) != NULL)
492 1.1 lukem return;
493 1.1 lukem
494 1.23 lukem cp = xstrdup(cwd);
495 1.22 lukem if (sl_add(slist, cp) == -1)
496 1.22 lukem syslog(LOG_WARNING, "can't add `%s' to stringlist", cp);
497 1.1 lukem
498 1.1 lukem /* First check for a display file */
499 1.24 lukem (void)format_file(curclass.display, code);
500 1.1 lukem
501 1.1 lukem /* Now see if there are any notify files */
502 1.24 lukem if (EMPTYSTR(curclass.notify))
503 1.1 lukem return;
504 1.1 lukem
505 1.1 lukem if (glob(curclass.notify, 0, NULL, &gl) != 0 || gl.gl_matchc == 0)
506 1.1 lukem return;
507 1.1 lukem time(&now);
508 1.1 lukem for (rlist = gl.gl_pathv; *rlist != NULL; rlist++) {
509 1.1 lukem if (stat(*rlist, &st) != 0)
510 1.1 lukem continue;
511 1.7 mycroft if (!S_ISREG(st.st_mode))
512 1.1 lukem continue;
513 1.1 lukem then = st.st_mtime;
514 1.20 lukem if (code != 0) {
515 1.20 lukem lreply(code, "");
516 1.20 lukem code = 0;
517 1.20 lukem }
518 1.1 lukem lreply(code, "Please read the file %s", *rlist);
519 1.1 lukem t = localtime(&now);
520 1.1 lukem age = 365 * t->tm_year + t->tm_yday;
521 1.1 lukem t = localtime(&then);
522 1.1 lukem age -= 365 * t->tm_year + t->tm_yday;
523 1.1 lukem lreply(code, " it was last modified on %.24s - %d day%s ago",
524 1.19 lukem ctime(&then), age, PLURAL(age));
525 1.1 lukem }
526 1.1 lukem globfree(&gl);
527 1.1 lukem }
528 1.1 lukem
529 1.24 lukem int
530 1.24 lukem format_file(file, code)
531 1.24 lukem const char *file;
532 1.24 lukem int code;
533 1.24 lukem {
534 1.24 lukem FILE *f;
535 1.24 lukem char *buf, *p, *cwd;
536 1.24 lukem size_t len;
537 1.24 lukem off_t b;
538 1.24 lukem time_t now;
539 1.29 lukem
540 1.29 lukem if (quietmessages)
541 1.29 lukem return (0);
542 1.24 lukem
543 1.24 lukem #define PUTC(x) putchar(x), b++
544 1.24 lukem
545 1.24 lukem if (EMPTYSTR(file))
546 1.24 lukem return(0);
547 1.24 lukem if ((f = fopen(file, "r")) == NULL)
548 1.24 lukem return (0);
549 1.24 lukem lreply(code, "");
550 1.24 lukem
551 1.24 lukem b = 0;
552 1.24 lukem for (;
553 1.24 lukem (buf = fparseln(f, &len, NULL, "\0\0\0", 0)) != NULL; free(buf)) {
554 1.24 lukem if (len > 0)
555 1.24 lukem if (buf[len - 1] == '\n')
556 1.24 lukem buf[--len] = '\0';
557 1.24 lukem b += printf(" ");
558 1.24 lukem
559 1.24 lukem for (p = buf; *p; p++) {
560 1.24 lukem if (*p == '%') {
561 1.24 lukem p++;
562 1.24 lukem switch (*p) {
563 1.25 lukem
564 1.25 lukem case 'c':
565 1.25 lukem b += printf("%s",
566 1.25 lukem curclass.classname ?
567 1.25 lukem curclass.classname : "<unknown>");
568 1.25 lukem break;
569 1.25 lukem
570 1.24 lukem case 'C':
571 1.24 lukem if (getcwd(cwd, sizeof(cwd)-1) == NULL){
572 1.24 lukem syslog(LOG_WARNING,
573 1.24 lukem "can't getcwd: %s",
574 1.24 lukem strerror(errno));
575 1.24 lukem continue;
576 1.24 lukem }
577 1.24 lukem b += printf("%s", cwd);
578 1.24 lukem break;
579 1.25 lukem
580 1.24 lukem case 'E':
581 1.24 lukem /* XXXX email address */
582 1.24 lukem break;
583 1.25 lukem
584 1.24 lukem case 'L':
585 1.24 lukem b += printf("%s", hostname);
586 1.24 lukem break;
587 1.25 lukem
588 1.25 lukem case 'M':
589 1.25 lukem if (curclass.limit == -1)
590 1.25 lukem b += printf("unlimited");
591 1.25 lukem else
592 1.25 lukem b += printf("%d",
593 1.25 lukem curclass.limit);
594 1.25 lukem break;
595 1.25 lukem
596 1.25 lukem case 'N':
597 1.25 lukem if (connections > 0)
598 1.25 lukem b += printf("%d", connections);
599 1.25 lukem break;
600 1.25 lukem
601 1.24 lukem case 'R':
602 1.24 lukem b += printf("%s", remotehost);
603 1.24 lukem break;
604 1.25 lukem
605 1.24 lukem case 'T':
606 1.24 lukem now = time(NULL);
607 1.25 lukem b += printf("%.24s", ctime(&now));
608 1.24 lukem break;
609 1.25 lukem
610 1.24 lukem case 'U':
611 1.24 lukem b += printf("%s",
612 1.24 lukem pw ? pw->pw_name : "<unknown>");
613 1.24 lukem break;
614 1.25 lukem
615 1.24 lukem case '%':
616 1.24 lukem PUTC('%');
617 1.24 lukem break;
618 1.25 lukem
619 1.24 lukem }
620 1.24 lukem } else {
621 1.24 lukem PUTC(*p);
622 1.24 lukem }
623 1.24 lukem }
624 1.24 lukem PUTC('\r');
625 1.24 lukem PUTC('\n');
626 1.24 lukem }
627 1.24 lukem
628 1.24 lukem total_bytes += b;
629 1.24 lukem total_bytes_out += b;
630 1.24 lukem (void)fflush(stdout);
631 1.24 lukem (void)fclose(f);
632 1.24 lukem return (1);
633 1.24 lukem }
634 1.24 lukem
635 1.1 lukem /*
636 1.23 lukem * Find s2 at the end of s1. If found, return a string up to (but
637 1.1 lukem * not including) s2, otherwise returns NULL.
638 1.1 lukem */
639 1.1 lukem static char *
640 1.1 lukem strend(s1, s2)
641 1.2 christos const char *s1;
642 1.2 christos char *s2;
643 1.1 lukem {
644 1.25 lukem static char buf[MAXPATHLEN];
645 1.1 lukem
646 1.1 lukem char *start;
647 1.1 lukem size_t l1, l2;
648 1.1 lukem
649 1.1 lukem l1 = strlen(s1);
650 1.1 lukem l2 = strlen(s2);
651 1.1 lukem
652 1.1 lukem if (l2 >= l1)
653 1.1 lukem return(NULL);
654 1.1 lukem
655 1.24 lukem strlcpy(buf, s1, sizeof(buf));
656 1.1 lukem start = buf + (l1 - l2);
657 1.1 lukem
658 1.1 lukem if (strcmp(start, s2) == 0) {
659 1.1 lukem *start = '\0';
660 1.1 lukem return(buf);
661 1.1 lukem } else
662 1.1 lukem return(NULL);
663 1.1 lukem }
664 1.1 lukem
665 1.1 lukem static int
666 1.1 lukem filetypematch(types, mode)
667 1.1 lukem char *types;
668 1.1 lukem int mode;
669 1.1 lukem {
670 1.1 lukem for ( ; types[0] != '\0'; types++)
671 1.1 lukem switch (*types) {
672 1.1 lukem case 'd':
673 1.1 lukem if (S_ISDIR(mode))
674 1.1 lukem return(1);
675 1.1 lukem break;
676 1.1 lukem case 'f':
677 1.1 lukem if (S_ISREG(mode))
678 1.1 lukem return(1);
679 1.1 lukem break;
680 1.1 lukem }
681 1.1 lukem return(0);
682 1.1 lukem }
683 1.1 lukem
684 1.1 lukem /*
685 1.1 lukem * Look for a conversion. If we succeed, return a pointer to the
686 1.1 lukem * command to execute for the conversion.
687 1.1 lukem *
688 1.1 lukem * The command is stored in a static array so there's no memory
689 1.1 lukem * leak problems, and not too much to change in ftpd.c. This
690 1.1 lukem * routine doesn't need to be re-entrant unless we start using a
691 1.1 lukem * multi-threaded ftpd, and that's not likely for a while...
692 1.1 lukem */
693 1.23 lukem char **
694 1.1 lukem do_conversion(fname)
695 1.1 lukem const char *fname;
696 1.1 lukem {
697 1.1 lukem struct ftpconv *cp;
698 1.1 lukem struct stat st;
699 1.1 lukem int o_errno;
700 1.3 christos char *base = NULL;
701 1.23 lukem char *cmd, *p, *lp, **argv;
702 1.23 lukem StringList *sl;
703 1.1 lukem
704 1.1 lukem o_errno = errno;
705 1.23 lukem sl = NULL;
706 1.23 lukem cmd = NULL;
707 1.1 lukem for (cp = curclass.conversions; cp != NULL; cp = cp->next) {
708 1.5 lukem if (cp->suffix == NULL) {
709 1.5 lukem syslog(LOG_WARNING,
710 1.5 lukem "cp->suffix==NULL in conv list; SHOULDN'T HAPPEN!");
711 1.5 lukem continue;
712 1.5 lukem }
713 1.1 lukem if ((base = strend(fname, cp->suffix)) == NULL)
714 1.1 lukem continue;
715 1.5 lukem if (cp->types == NULL || cp->disable == NULL ||
716 1.1 lukem cp->command == NULL)
717 1.1 lukem continue;
718 1.1 lukem /* Is it enabled? */
719 1.1 lukem if (strcmp(cp->disable, ".") != 0 &&
720 1.1 lukem stat(cp->disable, &st) == 0)
721 1.1 lukem continue;
722 1.1 lukem /* Does the base exist? */
723 1.1 lukem if (stat(base, &st) < 0)
724 1.1 lukem continue;
725 1.1 lukem /* Is the file type ok */
726 1.1 lukem if (!filetypematch(cp->types, st.st_mode))
727 1.1 lukem continue;
728 1.1 lukem break; /* "We have a winner!" */
729 1.1 lukem }
730 1.1 lukem
731 1.1 lukem /* If we got through the list, no conversion */
732 1.23 lukem if (cp == NULL)
733 1.23 lukem goto cleanup_do_conv;
734 1.23 lukem
735 1.23 lukem /* Split up command into an argv */
736 1.23 lukem if ((sl = sl_init()) == NULL)
737 1.23 lukem goto cleanup_do_conv;
738 1.23 lukem cmd = xstrdup(cp->command);
739 1.23 lukem p = cmd;
740 1.23 lukem while (p) {
741 1.23 lukem NEXTWORD(p, lp);
742 1.23 lukem if (strcmp(lp, "%s") == 0)
743 1.23 lukem lp = base;
744 1.23 lukem if (sl_add(sl, xstrdup(lp)) == -1)
745 1.23 lukem goto cleanup_do_conv;
746 1.1 lukem }
747 1.1 lukem
748 1.23 lukem if (sl_add(sl, NULL) == -1)
749 1.23 lukem goto cleanup_do_conv;
750 1.23 lukem argv = sl->sl_str;
751 1.23 lukem free(cmd);
752 1.23 lukem free(sl);
753 1.23 lukem return(argv);
754 1.23 lukem
755 1.23 lukem cleanup_do_conv:
756 1.23 lukem if (sl)
757 1.23 lukem sl_free(sl, 1);
758 1.23 lukem free(cmd);
759 1.23 lukem errno = o_errno;
760 1.23 lukem return(NULL);
761 1.24 lukem }
762 1.24 lukem
763 1.24 lukem /*
764 1.24 lukem * Convert the string `arg' to an int, which may have an optional SI suffix
765 1.24 lukem * (`b', `k', `m', `g'). Returns the number for success, -1 otherwise.
766 1.24 lukem */
767 1.24 lukem int
768 1.24 lukem strsuftoi(arg)
769 1.24 lukem const char *arg;
770 1.24 lukem {
771 1.24 lukem char *cp;
772 1.24 lukem long val;
773 1.24 lukem
774 1.24 lukem if (!isdigit((unsigned char)arg[0]))
775 1.24 lukem return (-1);
776 1.24 lukem
777 1.24 lukem val = strtol(arg, &cp, 10);
778 1.24 lukem if (cp != NULL) {
779 1.24 lukem if (cp[0] != '\0' && cp[1] != '\0')
780 1.24 lukem return (-1);
781 1.24 lukem switch (tolower((unsigned char)cp[0])) {
782 1.24 lukem case '\0':
783 1.24 lukem case 'b':
784 1.24 lukem break;
785 1.24 lukem case 'k':
786 1.24 lukem val <<= 10;
787 1.24 lukem break;
788 1.24 lukem case 'm':
789 1.24 lukem val <<= 20;
790 1.24 lukem break;
791 1.24 lukem case 'g':
792 1.24 lukem val <<= 30;
793 1.24 lukem break;
794 1.24 lukem default:
795 1.24 lukem return (-1);
796 1.24 lukem }
797 1.24 lukem }
798 1.24 lukem if (val < 0 || val > INT_MAX)
799 1.24 lukem return (-1);
800 1.24 lukem
801 1.24 lukem return (val);
802 1.25 lukem }
803 1.25 lukem
804 1.26 lukem /*
805 1.26 lukem * Count the number of current connections, reading from
806 1.26 lukem * /var/run/ftpd.pids-<class>
807 1.26 lukem * Does a kill -0 on each pid in that file, and only counts
808 1.26 lukem * processes that exist (or frees the slot if it doesn't).
809 1.26 lukem * Adds getpid() to the first free slot. Truncates the file
810 1.26 lukem * if possible.
811 1.26 lukem */
812 1.25 lukem void
813 1.25 lukem count_users()
814 1.25 lukem {
815 1.25 lukem char fn[MAXPATHLEN];
816 1.25 lukem int fd, i, last;
817 1.25 lukem size_t count;
818 1.25 lukem pid_t *pids, mypid;
819 1.25 lukem struct stat sb;
820 1.25 lukem
821 1.25 lukem (void)strlcpy(fn, _PATH_CLASSPIDS, sizeof(fn));
822 1.25 lukem (void)strlcat(fn, curclass.classname, sizeof(fn));
823 1.25 lukem pids = NULL;
824 1.25 lukem connections = 1;
825 1.25 lukem
826 1.25 lukem if ((fd = open(fn, O_RDWR | O_CREAT | O_EXLOCK, 0600)) == -1)
827 1.25 lukem return;
828 1.25 lukem if (fstat(fd, &sb) == -1)
829 1.25 lukem goto cleanup_count;
830 1.25 lukem if ((pids = malloc(sb.st_size + sizeof(pid_t))) == NULL)
831 1.25 lukem goto cleanup_count;
832 1.25 lukem count = read(fd, pids, sb.st_size);
833 1.25 lukem if (count < 0 || count != sb.st_size)
834 1.25 lukem goto cleanup_count;
835 1.25 lukem count /= sizeof(pid_t);
836 1.25 lukem mypid = getpid();
837 1.25 lukem last = 0;
838 1.25 lukem for (i = 0; i < count; i++) {
839 1.25 lukem if (pids[i] == 0)
840 1.25 lukem continue;
841 1.25 lukem if (kill(pids[i], 0) == -1 && errno != EPERM) {
842 1.25 lukem if (mypid != 0) {
843 1.25 lukem pids[i] = mypid;
844 1.25 lukem mypid = 0;
845 1.25 lukem last = i;
846 1.25 lukem }
847 1.25 lukem } else {
848 1.25 lukem connections++;
849 1.25 lukem last = i;
850 1.25 lukem }
851 1.25 lukem }
852 1.25 lukem if (mypid != 0) {
853 1.25 lukem if (pids[last] != 0)
854 1.25 lukem last++;
855 1.25 lukem pids[last] = mypid;
856 1.25 lukem }
857 1.25 lukem count = (last + 1) * sizeof(pid_t);
858 1.25 lukem if (lseek(fd, 0, SEEK_SET) == -1)
859 1.25 lukem goto cleanup_count;
860 1.25 lukem if (write(fd, pids, count) == -1)
861 1.25 lukem goto cleanup_count;
862 1.25 lukem (void)ftruncate(fd, count);
863 1.25 lukem
864 1.25 lukem cleanup_count:
865 1.25 lukem (void)flock(fd, LOCK_UN);
866 1.25 lukem close(fd);
867 1.25 lukem REASSIGN(pids, NULL);
868 1.1 lukem }
869