conf.c revision 1.32 1 1.32 sommerfe /* $NetBSD: conf.c,v 1.32 2000/07/09 02:24:30 sommerfeld 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.32 sommerfe __RCSID("$NetBSD: conf.c,v 1.32 2000/07/09 02:24:30 sommerfeld 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.30 lukem static char *strend(const char *, char *);
71 1.30 lukem static int filetypematch(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.30 lukem init_curclass(void)
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.30 lukem parse_conf(const char *findclass)
119 1.25 lukem {
120 1.25 lukem FILE *f;
121 1.25 lukem char *buf, *p;
122 1.25 lukem size_t len;
123 1.25 lukem int none, match, rate;
124 1.25 lukem char *endp;
125 1.26 lukem char *class, *word, *arg, *template;
126 1.25 lukem const char *infile;
127 1.25 lukem size_t line;
128 1.25 lukem unsigned int timeout;
129 1.25 lukem struct ftpconv *conv, *cnext;
130 1.25 lukem
131 1.25 lukem init_curclass();
132 1.25 lukem REASSIGN(curclass.classname, xstrdup(findclass));
133 1.1 lukem if (strcasecmp(findclass, "guest") == 0) {
134 1.9 lukem curclass.modify = 0;
135 1.1 lukem curclass.umask = 0707;
136 1.1 lukem }
137 1.1 lukem
138 1.6 lukem infile = conffilename(_PATH_FTPDCONF);
139 1.1 lukem if ((f = fopen(infile, "r")) == NULL)
140 1.1 lukem return;
141 1.1 lukem
142 1.1 lukem line = 0;
143 1.26 lukem template = NULL;
144 1.23 lukem for (;
145 1.23 lukem (buf = fparseln(f, &len, &line, NULL, FPARSELN_UNESCCOMM |
146 1.23 lukem FPARSELN_UNESCCONT | FPARSELN_UNESCESC)) != NULL;
147 1.23 lukem free(buf)) {
148 1.1 lukem none = match = 0;
149 1.23 lukem p = buf;
150 1.5 lukem if (len < 1)
151 1.5 lukem continue;
152 1.23 lukem if (p[len - 1] == '\n')
153 1.23 lukem p[--len] = '\0';
154 1.23 lukem if (EMPTYSTR(p))
155 1.1 lukem continue;
156 1.1 lukem
157 1.23 lukem NEXTWORD(p, word);
158 1.23 lukem NEXTWORD(p, class);
159 1.23 lukem NEXTWORD(p, arg);
160 1.1 lukem if (EMPTYSTR(word) || EMPTYSTR(class))
161 1.1 lukem continue;
162 1.1 lukem if (strcasecmp(class, "none") == 0)
163 1.1 lukem none = 1;
164 1.27 lukem if (! (strcasecmp(class, findclass) == 0 ||
165 1.27 lukem (template != NULL && strcasecmp(class, template) == 0) ||
166 1.27 lukem none ||
167 1.27 lukem strcasecmp(class, "all") == 0) )
168 1.1 lukem continue;
169 1.1 lukem
170 1.9 lukem if (strcasecmp(word, "checkportcmd") == 0) {
171 1.9 lukem if (none ||
172 1.9 lukem (!EMPTYSTR(arg) && strcasecmp(arg, "off") == 0))
173 1.9 lukem curclass.checkportcmd = 0;
174 1.9 lukem else
175 1.9 lukem curclass.checkportcmd = 1;
176 1.23 lukem
177 1.24 lukem } else if (strcasecmp(word, "classtype") == 0) {
178 1.24 lukem if (!none && !EMPTYSTR(arg)) {
179 1.24 lukem if (strcasecmp(arg, "GUEST") == 0)
180 1.24 lukem curclass.type = CLASS_GUEST;
181 1.24 lukem else if (strcasecmp(arg, "CHROOT") == 0)
182 1.24 lukem curclass.type = CLASS_CHROOT;
183 1.24 lukem else if (strcasecmp(arg, "REAL") == 0)
184 1.24 lukem curclass.type = CLASS_REAL;
185 1.24 lukem else {
186 1.24 lukem syslog(LOG_WARNING,
187 1.24 lukem "%s line %d: unknown class type `%s'",
188 1.24 lukem infile, (int)line, arg);
189 1.24 lukem continue;
190 1.24 lukem }
191 1.24 lukem }
192 1.24 lukem
193 1.9 lukem } else if (strcasecmp(word, "conversion") == 0) {
194 1.5 lukem char *suffix, *types, *disable, *convcmd;
195 1.5 lukem
196 1.1 lukem if (EMPTYSTR(arg)) {
197 1.1 lukem syslog(LOG_WARNING,
198 1.1 lukem "%s line %d: %s requires a suffix",
199 1.23 lukem infile, (int)line, word);
200 1.1 lukem continue; /* need a suffix */
201 1.1 lukem }
202 1.23 lukem NEXTWORD(p, types);
203 1.23 lukem NEXTWORD(p, disable);
204 1.23 lukem convcmd = p;
205 1.1 lukem if (convcmd)
206 1.1 lukem convcmd += strspn(convcmd, " \t");
207 1.23 lukem suffix = xstrdup(arg);
208 1.1 lukem if (none || EMPTYSTR(types) ||
209 1.1 lukem EMPTYSTR(disable) || EMPTYSTR(convcmd)) {
210 1.1 lukem types = NULL;
211 1.1 lukem disable = NULL;
212 1.1 lukem convcmd = NULL;
213 1.1 lukem } else {
214 1.23 lukem types = xstrdup(types);
215 1.23 lukem disable = xstrdup(disable);
216 1.23 lukem convcmd = xstrdup(convcmd);
217 1.1 lukem }
218 1.1 lukem for (conv = curclass.conversions; conv != NULL;
219 1.1 lukem conv = conv->next) {
220 1.5 lukem if (strcmp(conv->suffix, suffix) == 0)
221 1.1 lukem break;
222 1.1 lukem }
223 1.1 lukem if (conv == NULL) {
224 1.1 lukem conv = (struct ftpconv *)
225 1.1 lukem calloc(1, sizeof(struct ftpconv));
226 1.1 lukem if (conv == NULL) {
227 1.1 lukem syslog(LOG_WARNING, "can't malloc");
228 1.1 lukem continue;
229 1.1 lukem }
230 1.23 lukem conv->next = NULL;
231 1.23 lukem for (cnext = curclass.conversions;
232 1.23 lukem cnext != NULL; cnext = cnext->next)
233 1.23 lukem if (cnext->next == NULL)
234 1.23 lukem break;
235 1.23 lukem if (cnext != NULL)
236 1.23 lukem cnext->next = conv;
237 1.23 lukem else
238 1.23 lukem curclass.conversions = conv;
239 1.1 lukem }
240 1.5 lukem REASSIGN(conv->suffix, suffix);
241 1.1 lukem REASSIGN(conv->types, types);
242 1.1 lukem REASSIGN(conv->disable, disable);
243 1.1 lukem REASSIGN(conv->command, convcmd);
244 1.23 lukem
245 1.1 lukem } else if (strcasecmp(word, "display") == 0) {
246 1.1 lukem if (none || EMPTYSTR(arg))
247 1.1 lukem arg = NULL;
248 1.1 lukem else
249 1.23 lukem arg = xstrdup(arg);
250 1.1 lukem REASSIGN(curclass.display, arg);
251 1.23 lukem
252 1.25 lukem } else if (strcasecmp(word, "limit") == 0) {
253 1.25 lukem int limit;
254 1.25 lukem
255 1.25 lukem if (none || EMPTYSTR(arg))
256 1.25 lukem continue;
257 1.25 lukem limit = (int)strtol(arg, &endp, 10);
258 1.25 lukem if (*endp != 0) {
259 1.25 lukem syslog(LOG_WARNING,
260 1.25 lukem "%s line %d: invalid limit %s",
261 1.25 lukem infile, (int)line, arg);
262 1.25 lukem continue;
263 1.25 lukem }
264 1.25 lukem curclass.limit = limit;
265 1.26 lukem REASSIGN(curclass.limitfile,
266 1.26 lukem EMPTYSTR(p) ? NULL : xstrdup(p));
267 1.25 lukem
268 1.1 lukem } else if (strcasecmp(word, "maxtimeout") == 0) {
269 1.1 lukem if (none || EMPTYSTR(arg))
270 1.1 lukem continue;
271 1.1 lukem timeout = (unsigned int)strtoul(arg, &endp, 10);
272 1.1 lukem if (*endp != 0) {
273 1.1 lukem syslog(LOG_WARNING,
274 1.1 lukem "%s line %d: invalid maxtimeout %s",
275 1.23 lukem infile, (int)line, arg);
276 1.1 lukem continue;
277 1.1 lukem }
278 1.1 lukem if (timeout < 30) {
279 1.1 lukem syslog(LOG_WARNING,
280 1.1 lukem "%s line %d: maxtimeout %d < 30 seconds",
281 1.23 lukem infile, (int)line, timeout);
282 1.1 lukem continue;
283 1.1 lukem }
284 1.1 lukem if (timeout < curclass.timeout) {
285 1.1 lukem syslog(LOG_WARNING,
286 1.1 lukem "%s line %d: maxtimeout %d < timeout (%d)",
287 1.23 lukem infile, (int)line, timeout,
288 1.23 lukem curclass.timeout);
289 1.1 lukem continue;
290 1.1 lukem }
291 1.1 lukem curclass.maxtimeout = timeout;
292 1.23 lukem
293 1.1 lukem } else if (strcasecmp(word, "modify") == 0) {
294 1.1 lukem if (none ||
295 1.2 christos (!EMPTYSTR(arg) && strcasecmp(arg, "off") == 0))
296 1.1 lukem curclass.modify = 0;
297 1.1 lukem else
298 1.1 lukem curclass.modify = 1;
299 1.23 lukem
300 1.24 lukem } else if (strcasecmp(word, "motd") == 0) {
301 1.24 lukem if (none || EMPTYSTR(arg))
302 1.24 lukem arg = NULL;
303 1.24 lukem else
304 1.24 lukem arg = xstrdup(arg);
305 1.24 lukem REASSIGN(curclass.motd, arg);
306 1.24 lukem
307 1.24 lukem
308 1.1 lukem } else if (strcasecmp(word, "notify") == 0) {
309 1.1 lukem if (none || EMPTYSTR(arg))
310 1.1 lukem arg = NULL;
311 1.1 lukem else
312 1.23 lukem arg = xstrdup(arg);
313 1.1 lukem REASSIGN(curclass.notify, arg);
314 1.23 lukem
315 1.14 tv } else if (strcasecmp(word, "passive") == 0) {
316 1.14 tv if (none ||
317 1.14 tv (!EMPTYSTR(arg) && strcasecmp(arg, "off") == 0))
318 1.14 tv curclass.passive = 0;
319 1.14 tv else
320 1.14 tv curclass.passive = 1;
321 1.28 lukem
322 1.28 lukem } else if (strcasecmp(word, "portrange") == 0) {
323 1.28 lukem int minport, maxport;
324 1.28 lukem char *min, *max;
325 1.28 lukem
326 1.28 lukem if (none) {
327 1.28 lukem curclass.portmin = 0;
328 1.28 lukem curclass.portmax = 0;
329 1.28 lukem continue;
330 1.28 lukem }
331 1.28 lukem if (EMPTYSTR(arg))
332 1.28 lukem continue;
333 1.28 lukem min = arg;
334 1.28 lukem NEXTWORD(p, max);
335 1.28 lukem if (EMPTYSTR(max)) {
336 1.28 lukem syslog(LOG_WARNING,
337 1.28 lukem "%s line %d: missing maxport argument",
338 1.28 lukem infile, (int)line);
339 1.28 lukem continue;
340 1.28 lukem }
341 1.28 lukem minport = (int)strtol(min, &endp, 10);
342 1.28 lukem if (*endp != 0 || minport < IPPORT_RESERVED ||
343 1.28 lukem minport > IPPORT_ANONMAX) {
344 1.28 lukem syslog(LOG_WARNING,
345 1.28 lukem "%s line %d: invalid minport %s",
346 1.28 lukem infile, (int)line, min);
347 1.28 lukem continue;
348 1.28 lukem }
349 1.28 lukem maxport = (int)strtol(max, &endp, 10);
350 1.28 lukem if (*endp != 0 || maxport < IPPORT_RESERVED ||
351 1.28 lukem maxport > IPPORT_ANONMAX) {
352 1.28 lukem syslog(LOG_WARNING,
353 1.28 lukem "%s line %d: invalid maxport %s",
354 1.28 lukem infile, (int)line, max);
355 1.28 lukem continue;
356 1.28 lukem }
357 1.28 lukem if (minport >= maxport) {
358 1.28 lukem syslog(LOG_WARNING,
359 1.28 lukem "%s line %d: minport %d >= maxport %d",
360 1.28 lukem infile, (int)line, minport, maxport);
361 1.28 lukem continue;
362 1.28 lukem }
363 1.28 lukem curclass.portmin = minport;
364 1.28 lukem curclass.portmax = maxport;
365 1.23 lukem
366 1.24 lukem } else if (strcasecmp(word, "rateget") == 0) {
367 1.24 lukem if (none || EMPTYSTR(arg))
368 1.24 lukem continue;
369 1.24 lukem rate = strsuftoi(arg);
370 1.24 lukem if (rate == -1) {
371 1.24 lukem syslog(LOG_WARNING,
372 1.24 lukem "%s line %d: invalid rateget %s",
373 1.24 lukem infile, (int)line, arg);
374 1.24 lukem continue;
375 1.24 lukem }
376 1.24 lukem curclass.maxrateget = rate;
377 1.24 lukem curclass.rateget = rate;
378 1.24 lukem
379 1.24 lukem } else if (strcasecmp(word, "rateput") == 0) {
380 1.24 lukem if (none || EMPTYSTR(arg))
381 1.24 lukem continue;
382 1.24 lukem rate = strsuftoi(arg);
383 1.24 lukem if (rate == -1) {
384 1.24 lukem syslog(LOG_WARNING,
385 1.24 lukem "%s line %d: invalid rateput %s",
386 1.24 lukem infile, (int)line, arg);
387 1.24 lukem continue;
388 1.24 lukem }
389 1.24 lukem curclass.maxrateput = rate;
390 1.24 lukem curclass.rateput = rate;
391 1.24 lukem
392 1.1 lukem } else if (strcasecmp(word, "timeout") == 0) {
393 1.1 lukem if (none || EMPTYSTR(arg))
394 1.1 lukem continue;
395 1.1 lukem timeout = (unsigned int)strtoul(arg, &endp, 10);
396 1.1 lukem if (*endp != 0) {
397 1.1 lukem syslog(LOG_WARNING,
398 1.1 lukem "%s line %d: invalid timeout %s",
399 1.23 lukem infile, (int)line, arg);
400 1.1 lukem continue;
401 1.1 lukem }
402 1.1 lukem if (timeout < 30) {
403 1.1 lukem syslog(LOG_WARNING,
404 1.1 lukem "%s line %d: timeout %d < 30 seconds",
405 1.23 lukem infile, (int)line, timeout);
406 1.1 lukem continue;
407 1.1 lukem }
408 1.1 lukem if (timeout > curclass.maxtimeout) {
409 1.1 lukem syslog(LOG_WARNING,
410 1.1 lukem "%s line %d: timeout %d > maxtimeout (%d)",
411 1.23 lukem infile, (int)line, timeout,
412 1.23 lukem curclass.maxtimeout);
413 1.1 lukem continue;
414 1.1 lukem }
415 1.1 lukem curclass.timeout = timeout;
416 1.23 lukem
417 1.26 lukem } else if (strcasecmp(word, "template") == 0) {
418 1.26 lukem if (none)
419 1.26 lukem continue;
420 1.26 lukem REASSIGN(template, EMPTYSTR(arg) ? NULL : xstrdup(arg));
421 1.26 lukem
422 1.1 lukem } else if (strcasecmp(word, "umask") == 0) {
423 1.1 lukem mode_t umask;
424 1.1 lukem
425 1.1 lukem if (none || EMPTYSTR(arg))
426 1.1 lukem continue;
427 1.1 lukem umask = (mode_t)strtoul(arg, &endp, 8);
428 1.1 lukem if (*endp != 0 || umask > 0777) {
429 1.1 lukem syslog(LOG_WARNING,
430 1.1 lukem "%s line %d: invalid umask %s",
431 1.23 lukem infile, (int)line, arg);
432 1.1 lukem continue;
433 1.1 lukem }
434 1.1 lukem curclass.umask = umask;
435 1.23 lukem
436 1.24 lukem } else if (strcasecmp(word, "upload") == 0) {
437 1.24 lukem if (none ||
438 1.24 lukem (!EMPTYSTR(arg) && strcasecmp(arg, "off") == 0)) {
439 1.24 lukem curclass.modify = 0;
440 1.24 lukem curclass.upload = 0;
441 1.24 lukem } else
442 1.24 lukem curclass.upload = 1;
443 1.24 lukem
444 1.1 lukem } else {
445 1.1 lukem syslog(LOG_WARNING,
446 1.1 lukem "%s line %d: unknown directive '%s'",
447 1.23 lukem infile, (int)line, word);
448 1.1 lukem continue;
449 1.1 lukem }
450 1.1 lukem }
451 1.26 lukem REASSIGN(template, NULL);
452 1.1 lukem fclose(f);
453 1.1 lukem }
454 1.1 lukem
455 1.1 lukem /*
456 1.1 lukem * Show file listed in curclass.display first time in, and list all the
457 1.1 lukem * files named in curclass.notify in the current directory. Send back
458 1.17 lukem * responses with the prefix `code' + "-".
459 1.1 lukem */
460 1.1 lukem void
461 1.30 lukem show_chdir_messages(int code)
462 1.1 lukem {
463 1.1 lukem static StringList *slist = NULL;
464 1.1 lukem
465 1.1 lukem struct stat st;
466 1.1 lukem struct tm *t;
467 1.1 lukem glob_t gl;
468 1.1 lukem time_t now, then;
469 1.1 lukem int age;
470 1.25 lukem char cwd[MAXPATHLEN];
471 1.1 lukem char *cp, **rlist;
472 1.1 lukem
473 1.29 lukem if (quietmessages)
474 1.29 lukem return;
475 1.29 lukem
476 1.1 lukem /* Setup list for directory cache */
477 1.1 lukem if (slist == NULL)
478 1.1 lukem slist = sl_init();
479 1.22 lukem if (slist == NULL) {
480 1.22 lukem syslog(LOG_WARNING, "can't allocate memory for stringlist");
481 1.22 lukem return;
482 1.22 lukem }
483 1.1 lukem
484 1.1 lukem /* Check if this directory has already been visited */
485 1.1 lukem if (getcwd(cwd, sizeof(cwd) - 1) == NULL) {
486 1.13 mouse syslog(LOG_WARNING, "can't getcwd: %s", strerror(errno));
487 1.1 lukem return;
488 1.1 lukem }
489 1.1 lukem if (sl_find(slist, cwd) != NULL)
490 1.1 lukem return;
491 1.1 lukem
492 1.23 lukem cp = xstrdup(cwd);
493 1.22 lukem if (sl_add(slist, cp) == -1)
494 1.22 lukem syslog(LOG_WARNING, "can't add `%s' to stringlist", cp);
495 1.1 lukem
496 1.1 lukem /* First check for a display file */
497 1.24 lukem (void)format_file(curclass.display, code);
498 1.1 lukem
499 1.1 lukem /* Now see if there are any notify files */
500 1.24 lukem if (EMPTYSTR(curclass.notify))
501 1.1 lukem return;
502 1.1 lukem
503 1.1 lukem if (glob(curclass.notify, 0, NULL, &gl) != 0 || gl.gl_matchc == 0)
504 1.1 lukem return;
505 1.1 lukem time(&now);
506 1.1 lukem for (rlist = gl.gl_pathv; *rlist != NULL; rlist++) {
507 1.1 lukem if (stat(*rlist, &st) != 0)
508 1.1 lukem continue;
509 1.7 mycroft if (!S_ISREG(st.st_mode))
510 1.1 lukem continue;
511 1.1 lukem then = st.st_mtime;
512 1.20 lukem if (code != 0) {
513 1.32 sommerfe reply(-code, "%s", "");
514 1.20 lukem code = 0;
515 1.20 lukem }
516 1.31 lukem reply(-code, "Please read the file %s", *rlist);
517 1.1 lukem t = localtime(&now);
518 1.1 lukem age = 365 * t->tm_year + t->tm_yday;
519 1.1 lukem t = localtime(&then);
520 1.1 lukem age -= 365 * t->tm_year + t->tm_yday;
521 1.31 lukem reply(-code, " it was last modified on %.24s - %d day%s ago",
522 1.19 lukem ctime(&then), age, PLURAL(age));
523 1.1 lukem }
524 1.1 lukem globfree(&gl);
525 1.1 lukem }
526 1.1 lukem
527 1.24 lukem int
528 1.30 lukem format_file(const char *file, int code)
529 1.24 lukem {
530 1.24 lukem FILE *f;
531 1.24 lukem char *buf, *p, *cwd;
532 1.24 lukem size_t len;
533 1.24 lukem time_t now;
534 1.29 lukem
535 1.29 lukem if (quietmessages)
536 1.29 lukem return (0);
537 1.24 lukem
538 1.24 lukem if (EMPTYSTR(file))
539 1.24 lukem return(0);
540 1.24 lukem if ((f = fopen(file, "r")) == NULL)
541 1.24 lukem return (0);
542 1.32 sommerfe reply(-code, "%s", "");
543 1.24 lukem
544 1.24 lukem for (;
545 1.24 lukem (buf = fparseln(f, &len, NULL, "\0\0\0", 0)) != NULL; free(buf)) {
546 1.24 lukem if (len > 0)
547 1.24 lukem if (buf[len - 1] == '\n')
548 1.24 lukem buf[--len] = '\0';
549 1.31 lukem cprintf(stdout, " ");
550 1.24 lukem
551 1.24 lukem for (p = buf; *p; p++) {
552 1.24 lukem if (*p == '%') {
553 1.24 lukem p++;
554 1.24 lukem switch (*p) {
555 1.25 lukem
556 1.25 lukem case 'c':
557 1.31 lukem cprintf(stdout, "%s",
558 1.25 lukem curclass.classname ?
559 1.25 lukem curclass.classname : "<unknown>");
560 1.25 lukem break;
561 1.25 lukem
562 1.24 lukem case 'C':
563 1.24 lukem if (getcwd(cwd, sizeof(cwd)-1) == NULL){
564 1.24 lukem syslog(LOG_WARNING,
565 1.24 lukem "can't getcwd: %s",
566 1.24 lukem strerror(errno));
567 1.24 lukem continue;
568 1.24 lukem }
569 1.31 lukem cprintf(stdout, "%s", cwd);
570 1.24 lukem break;
571 1.25 lukem
572 1.24 lukem case 'E':
573 1.24 lukem /* XXXX email address */
574 1.24 lukem break;
575 1.25 lukem
576 1.24 lukem case 'L':
577 1.31 lukem cprintf(stdout, "%s", hostname);
578 1.24 lukem break;
579 1.25 lukem
580 1.25 lukem case 'M':
581 1.25 lukem if (curclass.limit == -1)
582 1.31 lukem cprintf(stdout, "unlimited");
583 1.25 lukem else
584 1.31 lukem cprintf(stdout, "%d",
585 1.25 lukem curclass.limit);
586 1.25 lukem break;
587 1.25 lukem
588 1.25 lukem case 'N':
589 1.25 lukem if (connections > 0)
590 1.31 lukem cprintf(stdout, "%d",
591 1.31 lukem connections);
592 1.25 lukem break;
593 1.25 lukem
594 1.24 lukem case 'R':
595 1.31 lukem cprintf(stdout, "%s", remotehost);
596 1.24 lukem break;
597 1.25 lukem
598 1.24 lukem case 'T':
599 1.24 lukem now = time(NULL);
600 1.31 lukem cprintf(stdout, "%.24s", ctime(&now));
601 1.24 lukem break;
602 1.25 lukem
603 1.24 lukem case 'U':
604 1.31 lukem cprintf(stdout, "%s",
605 1.24 lukem pw ? pw->pw_name : "<unknown>");
606 1.24 lukem break;
607 1.25 lukem
608 1.24 lukem case '%':
609 1.31 lukem CPUTC('%', stdout);
610 1.24 lukem break;
611 1.25 lukem
612 1.24 lukem }
613 1.31 lukem } else
614 1.31 lukem CPUTC(*p, stdout);
615 1.24 lukem }
616 1.31 lukem cprintf(stdout, "\r\n");
617 1.24 lukem }
618 1.24 lukem
619 1.24 lukem (void)fflush(stdout);
620 1.24 lukem (void)fclose(f);
621 1.24 lukem return (1);
622 1.24 lukem }
623 1.24 lukem
624 1.1 lukem /*
625 1.23 lukem * Find s2 at the end of s1. If found, return a string up to (but
626 1.1 lukem * not including) s2, otherwise returns NULL.
627 1.1 lukem */
628 1.1 lukem static char *
629 1.30 lukem strend(const char *s1, char *s2)
630 1.1 lukem {
631 1.25 lukem static char buf[MAXPATHLEN];
632 1.1 lukem
633 1.1 lukem char *start;
634 1.1 lukem size_t l1, l2;
635 1.1 lukem
636 1.1 lukem l1 = strlen(s1);
637 1.1 lukem l2 = strlen(s2);
638 1.1 lukem
639 1.1 lukem if (l2 >= l1)
640 1.1 lukem return(NULL);
641 1.1 lukem
642 1.24 lukem strlcpy(buf, s1, sizeof(buf));
643 1.1 lukem start = buf + (l1 - l2);
644 1.1 lukem
645 1.1 lukem if (strcmp(start, s2) == 0) {
646 1.1 lukem *start = '\0';
647 1.1 lukem return(buf);
648 1.1 lukem } else
649 1.1 lukem return(NULL);
650 1.1 lukem }
651 1.1 lukem
652 1.1 lukem static int
653 1.30 lukem filetypematch(char *types, int mode)
654 1.1 lukem {
655 1.1 lukem for ( ; types[0] != '\0'; types++)
656 1.1 lukem switch (*types) {
657 1.1 lukem case 'd':
658 1.1 lukem if (S_ISDIR(mode))
659 1.1 lukem return(1);
660 1.1 lukem break;
661 1.1 lukem case 'f':
662 1.1 lukem if (S_ISREG(mode))
663 1.1 lukem return(1);
664 1.1 lukem break;
665 1.1 lukem }
666 1.1 lukem return(0);
667 1.1 lukem }
668 1.1 lukem
669 1.1 lukem /*
670 1.1 lukem * Look for a conversion. If we succeed, return a pointer to the
671 1.1 lukem * command to execute for the conversion.
672 1.1 lukem *
673 1.1 lukem * The command is stored in a static array so there's no memory
674 1.1 lukem * leak problems, and not too much to change in ftpd.c. This
675 1.1 lukem * routine doesn't need to be re-entrant unless we start using a
676 1.1 lukem * multi-threaded ftpd, and that's not likely for a while...
677 1.1 lukem */
678 1.23 lukem char **
679 1.30 lukem do_conversion(const char *fname)
680 1.1 lukem {
681 1.1 lukem struct ftpconv *cp;
682 1.1 lukem struct stat st;
683 1.1 lukem int o_errno;
684 1.3 christos char *base = NULL;
685 1.23 lukem char *cmd, *p, *lp, **argv;
686 1.23 lukem StringList *sl;
687 1.1 lukem
688 1.1 lukem o_errno = errno;
689 1.23 lukem sl = NULL;
690 1.23 lukem cmd = NULL;
691 1.1 lukem for (cp = curclass.conversions; cp != NULL; cp = cp->next) {
692 1.5 lukem if (cp->suffix == NULL) {
693 1.5 lukem syslog(LOG_WARNING,
694 1.5 lukem "cp->suffix==NULL in conv list; SHOULDN'T HAPPEN!");
695 1.5 lukem continue;
696 1.5 lukem }
697 1.1 lukem if ((base = strend(fname, cp->suffix)) == NULL)
698 1.1 lukem continue;
699 1.5 lukem if (cp->types == NULL || cp->disable == NULL ||
700 1.1 lukem cp->command == NULL)
701 1.1 lukem continue;
702 1.1 lukem /* Is it enabled? */
703 1.1 lukem if (strcmp(cp->disable, ".") != 0 &&
704 1.1 lukem stat(cp->disable, &st) == 0)
705 1.1 lukem continue;
706 1.1 lukem /* Does the base exist? */
707 1.1 lukem if (stat(base, &st) < 0)
708 1.1 lukem continue;
709 1.1 lukem /* Is the file type ok */
710 1.1 lukem if (!filetypematch(cp->types, st.st_mode))
711 1.1 lukem continue;
712 1.1 lukem break; /* "We have a winner!" */
713 1.1 lukem }
714 1.1 lukem
715 1.1 lukem /* If we got through the list, no conversion */
716 1.23 lukem if (cp == NULL)
717 1.23 lukem goto cleanup_do_conv;
718 1.23 lukem
719 1.23 lukem /* Split up command into an argv */
720 1.23 lukem if ((sl = sl_init()) == NULL)
721 1.23 lukem goto cleanup_do_conv;
722 1.23 lukem cmd = xstrdup(cp->command);
723 1.23 lukem p = cmd;
724 1.23 lukem while (p) {
725 1.23 lukem NEXTWORD(p, lp);
726 1.23 lukem if (strcmp(lp, "%s") == 0)
727 1.23 lukem lp = base;
728 1.23 lukem if (sl_add(sl, xstrdup(lp)) == -1)
729 1.23 lukem goto cleanup_do_conv;
730 1.1 lukem }
731 1.1 lukem
732 1.23 lukem if (sl_add(sl, NULL) == -1)
733 1.23 lukem goto cleanup_do_conv;
734 1.23 lukem argv = sl->sl_str;
735 1.23 lukem free(cmd);
736 1.23 lukem free(sl);
737 1.23 lukem return(argv);
738 1.23 lukem
739 1.23 lukem cleanup_do_conv:
740 1.23 lukem if (sl)
741 1.23 lukem sl_free(sl, 1);
742 1.23 lukem free(cmd);
743 1.23 lukem errno = o_errno;
744 1.23 lukem return(NULL);
745 1.24 lukem }
746 1.24 lukem
747 1.24 lukem /*
748 1.24 lukem * Convert the string `arg' to an int, which may have an optional SI suffix
749 1.24 lukem * (`b', `k', `m', `g'). Returns the number for success, -1 otherwise.
750 1.24 lukem */
751 1.24 lukem int
752 1.30 lukem strsuftoi(const char *arg)
753 1.24 lukem {
754 1.24 lukem char *cp;
755 1.24 lukem long val;
756 1.24 lukem
757 1.24 lukem if (!isdigit((unsigned char)arg[0]))
758 1.24 lukem return (-1);
759 1.24 lukem
760 1.24 lukem val = strtol(arg, &cp, 10);
761 1.24 lukem if (cp != NULL) {
762 1.24 lukem if (cp[0] != '\0' && cp[1] != '\0')
763 1.24 lukem return (-1);
764 1.24 lukem switch (tolower((unsigned char)cp[0])) {
765 1.24 lukem case '\0':
766 1.24 lukem case 'b':
767 1.24 lukem break;
768 1.24 lukem case 'k':
769 1.24 lukem val <<= 10;
770 1.24 lukem break;
771 1.24 lukem case 'm':
772 1.24 lukem val <<= 20;
773 1.24 lukem break;
774 1.24 lukem case 'g':
775 1.24 lukem val <<= 30;
776 1.24 lukem break;
777 1.24 lukem default:
778 1.24 lukem return (-1);
779 1.24 lukem }
780 1.24 lukem }
781 1.24 lukem if (val < 0 || val > INT_MAX)
782 1.24 lukem return (-1);
783 1.24 lukem
784 1.24 lukem return (val);
785 1.25 lukem }
786 1.25 lukem
787 1.26 lukem /*
788 1.26 lukem * Count the number of current connections, reading from
789 1.26 lukem * /var/run/ftpd.pids-<class>
790 1.26 lukem * Does a kill -0 on each pid in that file, and only counts
791 1.26 lukem * processes that exist (or frees the slot if it doesn't).
792 1.26 lukem * Adds getpid() to the first free slot. Truncates the file
793 1.26 lukem * if possible.
794 1.26 lukem */
795 1.25 lukem void
796 1.30 lukem count_users(void)
797 1.25 lukem {
798 1.25 lukem char fn[MAXPATHLEN];
799 1.25 lukem int fd, i, last;
800 1.25 lukem size_t count;
801 1.25 lukem pid_t *pids, mypid;
802 1.25 lukem struct stat sb;
803 1.25 lukem
804 1.25 lukem (void)strlcpy(fn, _PATH_CLASSPIDS, sizeof(fn));
805 1.25 lukem (void)strlcat(fn, curclass.classname, sizeof(fn));
806 1.25 lukem pids = NULL;
807 1.25 lukem connections = 1;
808 1.25 lukem
809 1.25 lukem if ((fd = open(fn, O_RDWR | O_CREAT | O_EXLOCK, 0600)) == -1)
810 1.25 lukem return;
811 1.25 lukem if (fstat(fd, &sb) == -1)
812 1.25 lukem goto cleanup_count;
813 1.25 lukem if ((pids = malloc(sb.st_size + sizeof(pid_t))) == NULL)
814 1.25 lukem goto cleanup_count;
815 1.25 lukem count = read(fd, pids, sb.st_size);
816 1.25 lukem if (count < 0 || count != sb.st_size)
817 1.25 lukem goto cleanup_count;
818 1.25 lukem count /= sizeof(pid_t);
819 1.25 lukem mypid = getpid();
820 1.25 lukem last = 0;
821 1.25 lukem for (i = 0; i < count; i++) {
822 1.25 lukem if (pids[i] == 0)
823 1.25 lukem continue;
824 1.25 lukem if (kill(pids[i], 0) == -1 && errno != EPERM) {
825 1.25 lukem if (mypid != 0) {
826 1.25 lukem pids[i] = mypid;
827 1.25 lukem mypid = 0;
828 1.25 lukem last = i;
829 1.25 lukem }
830 1.25 lukem } else {
831 1.25 lukem connections++;
832 1.25 lukem last = i;
833 1.25 lukem }
834 1.25 lukem }
835 1.25 lukem if (mypid != 0) {
836 1.25 lukem if (pids[last] != 0)
837 1.25 lukem last++;
838 1.25 lukem pids[last] = mypid;
839 1.25 lukem }
840 1.25 lukem count = (last + 1) * sizeof(pid_t);
841 1.25 lukem if (lseek(fd, 0, SEEK_SET) == -1)
842 1.25 lukem goto cleanup_count;
843 1.25 lukem if (write(fd, pids, count) == -1)
844 1.25 lukem goto cleanup_count;
845 1.25 lukem (void)ftruncate(fd, count);
846 1.25 lukem
847 1.25 lukem cleanup_count:
848 1.25 lukem (void)flock(fd, LOCK_UN);
849 1.25 lukem close(fd);
850 1.25 lukem REASSIGN(pids, NULL);
851 1.1 lukem }
852