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