conf.c revision 1.27 1 1.27 lukem /* $NetBSD: conf.c,v 1.27 2000/01/10 08:03:50 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.27 lukem __RCSID("$NetBSD: conf.c,v 1.27 2000/01/10 08:03:50 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.25 lukem #include <signal.h>
53 1.1 lukem #include <stdio.h>
54 1.2 christos #include <stdlib.h>
55 1.1 lukem #include <string.h>
56 1.1 lukem #include <stringlist.h>
57 1.1 lukem #include <syslog.h>
58 1.23 lukem #include <time.h>
59 1.23 lukem #include <unistd.h>
60 1.23 lukem #include <util.h>
61 1.18 explorer
62 1.18 explorer #ifdef KERBEROS5
63 1.21 christos #include <krb5/krb5.h>
64 1.18 explorer #endif
65 1.1 lukem
66 1.1 lukem #include "extern.h"
67 1.1 lukem #include "pathnames.h"
68 1.1 lukem
69 1.2 christos static char *strend __P((const char *, char *));
70 1.2 christos static int filetypematch __P((char *, int));
71 1.15 lukem
72 1.15 lukem struct ftpclass curclass;
73 1.15 lukem
74 1.1 lukem
75 1.1 lukem /*
76 1.25 lukem * Initialise curclass to an `empty' state
77 1.1 lukem */
78 1.1 lukem void
79 1.25 lukem init_curclass()
80 1.1 lukem {
81 1.1 lukem struct ftpconv *conv, *cnext;
82 1.1 lukem
83 1.23 lukem for (conv = curclass.conversions; conv != NULL; conv = cnext) {
84 1.1 lukem REASSIGN(conv->suffix, NULL);
85 1.1 lukem REASSIGN(conv->types, NULL);
86 1.1 lukem REASSIGN(conv->disable, NULL);
87 1.1 lukem REASSIGN(conv->command, NULL);
88 1.1 lukem cnext = conv->next;
89 1.1 lukem free(conv);
90 1.1 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.24 lukem curclass.rateget = 0;
105 1.24 lukem curclass.rateput = 0;
106 1.1 lukem curclass.timeout = 900; /* 15 minutes */
107 1.1 lukem curclass.umask = 027;
108 1.24 lukem curclass.upload = 1;
109 1.1 lukem
110 1.25 lukem }
111 1.25 lukem
112 1.25 lukem /*
113 1.25 lukem * Parse the configuration file, looking for the named class, and
114 1.25 lukem * define curclass to contain the appropriate settings.
115 1.25 lukem */
116 1.25 lukem void
117 1.25 lukem parse_conf(findclass)
118 1.25 lukem 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.23 lukem
322 1.24 lukem } else if (strcasecmp(word, "rateget") == 0) {
323 1.24 lukem if (none || EMPTYSTR(arg))
324 1.24 lukem continue;
325 1.24 lukem rate = strsuftoi(arg);
326 1.24 lukem if (rate == -1) {
327 1.24 lukem syslog(LOG_WARNING,
328 1.24 lukem "%s line %d: invalid rateget %s",
329 1.24 lukem infile, (int)line, arg);
330 1.24 lukem continue;
331 1.24 lukem }
332 1.24 lukem curclass.maxrateget = rate;
333 1.24 lukem curclass.rateget = rate;
334 1.24 lukem
335 1.24 lukem } else if (strcasecmp(word, "rateput") == 0) {
336 1.24 lukem if (none || EMPTYSTR(arg))
337 1.24 lukem continue;
338 1.24 lukem rate = strsuftoi(arg);
339 1.24 lukem if (rate == -1) {
340 1.24 lukem syslog(LOG_WARNING,
341 1.24 lukem "%s line %d: invalid rateput %s",
342 1.24 lukem infile, (int)line, arg);
343 1.24 lukem continue;
344 1.24 lukem }
345 1.24 lukem curclass.maxrateput = rate;
346 1.24 lukem curclass.rateput = rate;
347 1.24 lukem
348 1.1 lukem } else if (strcasecmp(word, "timeout") == 0) {
349 1.1 lukem if (none || EMPTYSTR(arg))
350 1.1 lukem continue;
351 1.1 lukem timeout = (unsigned int)strtoul(arg, &endp, 10);
352 1.1 lukem if (*endp != 0) {
353 1.1 lukem syslog(LOG_WARNING,
354 1.1 lukem "%s line %d: invalid timeout %s",
355 1.23 lukem infile, (int)line, arg);
356 1.1 lukem continue;
357 1.1 lukem }
358 1.1 lukem if (timeout < 30) {
359 1.1 lukem syslog(LOG_WARNING,
360 1.1 lukem "%s line %d: timeout %d < 30 seconds",
361 1.23 lukem infile, (int)line, timeout);
362 1.1 lukem continue;
363 1.1 lukem }
364 1.1 lukem if (timeout > curclass.maxtimeout) {
365 1.1 lukem syslog(LOG_WARNING,
366 1.1 lukem "%s line %d: timeout %d > maxtimeout (%d)",
367 1.23 lukem infile, (int)line, timeout,
368 1.23 lukem curclass.maxtimeout);
369 1.1 lukem continue;
370 1.1 lukem }
371 1.1 lukem curclass.timeout = timeout;
372 1.23 lukem
373 1.26 lukem } else if (strcasecmp(word, "template") == 0) {
374 1.26 lukem if (none)
375 1.26 lukem continue;
376 1.26 lukem REASSIGN(template, EMPTYSTR(arg) ? NULL : xstrdup(arg));
377 1.26 lukem
378 1.1 lukem } else if (strcasecmp(word, "umask") == 0) {
379 1.1 lukem mode_t umask;
380 1.1 lukem
381 1.1 lukem if (none || EMPTYSTR(arg))
382 1.1 lukem continue;
383 1.1 lukem umask = (mode_t)strtoul(arg, &endp, 8);
384 1.1 lukem if (*endp != 0 || umask > 0777) {
385 1.1 lukem syslog(LOG_WARNING,
386 1.1 lukem "%s line %d: invalid umask %s",
387 1.23 lukem infile, (int)line, arg);
388 1.1 lukem continue;
389 1.1 lukem }
390 1.1 lukem curclass.umask = umask;
391 1.23 lukem
392 1.24 lukem } else if (strcasecmp(word, "upload") == 0) {
393 1.24 lukem if (none ||
394 1.24 lukem (!EMPTYSTR(arg) && strcasecmp(arg, "off") == 0)) {
395 1.24 lukem curclass.modify = 0;
396 1.24 lukem curclass.upload = 0;
397 1.24 lukem } else
398 1.24 lukem curclass.upload = 1;
399 1.24 lukem
400 1.1 lukem } else {
401 1.1 lukem syslog(LOG_WARNING,
402 1.1 lukem "%s line %d: unknown directive '%s'",
403 1.23 lukem infile, (int)line, word);
404 1.1 lukem continue;
405 1.1 lukem }
406 1.1 lukem }
407 1.26 lukem REASSIGN(template, NULL);
408 1.1 lukem fclose(f);
409 1.1 lukem }
410 1.1 lukem
411 1.1 lukem /*
412 1.1 lukem * Show file listed in curclass.display first time in, and list all the
413 1.1 lukem * files named in curclass.notify in the current directory. Send back
414 1.17 lukem * responses with the prefix `code' + "-".
415 1.1 lukem */
416 1.1 lukem void
417 1.1 lukem show_chdir_messages(code)
418 1.1 lukem int code;
419 1.1 lukem {
420 1.1 lukem static StringList *slist = NULL;
421 1.1 lukem
422 1.1 lukem struct stat st;
423 1.1 lukem struct tm *t;
424 1.1 lukem glob_t gl;
425 1.1 lukem time_t now, then;
426 1.1 lukem int age;
427 1.25 lukem char cwd[MAXPATHLEN];
428 1.1 lukem char *cp, **rlist;
429 1.1 lukem
430 1.1 lukem /* Setup list for directory cache */
431 1.1 lukem if (slist == NULL)
432 1.1 lukem slist = sl_init();
433 1.22 lukem if (slist == NULL) {
434 1.22 lukem syslog(LOG_WARNING, "can't allocate memory for stringlist");
435 1.22 lukem return;
436 1.22 lukem }
437 1.1 lukem
438 1.1 lukem /* Check if this directory has already been visited */
439 1.1 lukem if (getcwd(cwd, sizeof(cwd) - 1) == NULL) {
440 1.13 mouse syslog(LOG_WARNING, "can't getcwd: %s", strerror(errno));
441 1.1 lukem return;
442 1.1 lukem }
443 1.1 lukem if (sl_find(slist, cwd) != NULL)
444 1.1 lukem return;
445 1.1 lukem
446 1.23 lukem cp = xstrdup(cwd);
447 1.22 lukem if (sl_add(slist, cp) == -1)
448 1.22 lukem syslog(LOG_WARNING, "can't add `%s' to stringlist", cp);
449 1.1 lukem
450 1.1 lukem /* First check for a display file */
451 1.24 lukem (void)format_file(curclass.display, code);
452 1.1 lukem
453 1.1 lukem /* Now see if there are any notify files */
454 1.24 lukem if (EMPTYSTR(curclass.notify))
455 1.1 lukem return;
456 1.1 lukem
457 1.1 lukem if (glob(curclass.notify, 0, NULL, &gl) != 0 || gl.gl_matchc == 0)
458 1.1 lukem return;
459 1.1 lukem time(&now);
460 1.1 lukem for (rlist = gl.gl_pathv; *rlist != NULL; rlist++) {
461 1.1 lukem if (stat(*rlist, &st) != 0)
462 1.1 lukem continue;
463 1.7 mycroft if (!S_ISREG(st.st_mode))
464 1.1 lukem continue;
465 1.1 lukem then = st.st_mtime;
466 1.20 lukem if (code != 0) {
467 1.20 lukem lreply(code, "");
468 1.20 lukem code = 0;
469 1.20 lukem }
470 1.1 lukem lreply(code, "Please read the file %s", *rlist);
471 1.1 lukem t = localtime(&now);
472 1.1 lukem age = 365 * t->tm_year + t->tm_yday;
473 1.1 lukem t = localtime(&then);
474 1.1 lukem age -= 365 * t->tm_year + t->tm_yday;
475 1.1 lukem lreply(code, " it was last modified on %.24s - %d day%s ago",
476 1.19 lukem ctime(&then), age, PLURAL(age));
477 1.1 lukem }
478 1.1 lukem globfree(&gl);
479 1.1 lukem }
480 1.1 lukem
481 1.24 lukem int
482 1.24 lukem format_file(file, code)
483 1.24 lukem const char *file;
484 1.24 lukem int code;
485 1.24 lukem {
486 1.24 lukem FILE *f;
487 1.24 lukem char *buf, *p, *cwd;
488 1.24 lukem size_t len;
489 1.24 lukem off_t b;
490 1.24 lukem time_t now;
491 1.24 lukem
492 1.24 lukem #define PUTC(x) putchar(x), b++
493 1.24 lukem
494 1.24 lukem if (EMPTYSTR(file))
495 1.24 lukem return(0);
496 1.24 lukem if ((f = fopen(file, "r")) == NULL)
497 1.24 lukem return (0);
498 1.24 lukem lreply(code, "");
499 1.24 lukem
500 1.24 lukem b = 0;
501 1.24 lukem for (;
502 1.24 lukem (buf = fparseln(f, &len, NULL, "\0\0\0", 0)) != NULL; free(buf)) {
503 1.24 lukem if (len > 0)
504 1.24 lukem if (buf[len - 1] == '\n')
505 1.24 lukem buf[--len] = '\0';
506 1.24 lukem b += printf(" ");
507 1.24 lukem
508 1.24 lukem for (p = buf; *p; p++) {
509 1.24 lukem if (*p == '%') {
510 1.24 lukem p++;
511 1.24 lukem switch (*p) {
512 1.25 lukem
513 1.25 lukem case 'c':
514 1.25 lukem b += printf("%s",
515 1.25 lukem curclass.classname ?
516 1.25 lukem curclass.classname : "<unknown>");
517 1.25 lukem break;
518 1.25 lukem
519 1.24 lukem case 'C':
520 1.24 lukem if (getcwd(cwd, sizeof(cwd)-1) == NULL){
521 1.24 lukem syslog(LOG_WARNING,
522 1.24 lukem "can't getcwd: %s",
523 1.24 lukem strerror(errno));
524 1.24 lukem continue;
525 1.24 lukem }
526 1.24 lukem b += printf("%s", cwd);
527 1.24 lukem break;
528 1.25 lukem
529 1.24 lukem case 'E':
530 1.24 lukem /* XXXX email address */
531 1.24 lukem break;
532 1.25 lukem
533 1.24 lukem case 'L':
534 1.24 lukem b += printf("%s", hostname);
535 1.24 lukem break;
536 1.25 lukem
537 1.25 lukem case 'M':
538 1.25 lukem if (curclass.limit == -1)
539 1.25 lukem b += printf("unlimited");
540 1.25 lukem else
541 1.25 lukem b += printf("%d",
542 1.25 lukem curclass.limit);
543 1.25 lukem break;
544 1.25 lukem
545 1.25 lukem case 'N':
546 1.25 lukem if (connections > 0)
547 1.25 lukem b += printf("%d", connections);
548 1.25 lukem break;
549 1.25 lukem
550 1.24 lukem case 'R':
551 1.24 lukem b += printf("%s", remotehost);
552 1.24 lukem break;
553 1.25 lukem
554 1.24 lukem case 'T':
555 1.24 lukem now = time(NULL);
556 1.25 lukem b += printf("%.24s", ctime(&now));
557 1.24 lukem break;
558 1.25 lukem
559 1.24 lukem case 'U':
560 1.24 lukem b += printf("%s",
561 1.24 lukem pw ? pw->pw_name : "<unknown>");
562 1.24 lukem break;
563 1.25 lukem
564 1.24 lukem case '%':
565 1.24 lukem PUTC('%');
566 1.24 lukem break;
567 1.25 lukem
568 1.24 lukem }
569 1.24 lukem } else {
570 1.24 lukem PUTC(*p);
571 1.24 lukem }
572 1.24 lukem }
573 1.24 lukem PUTC('\r');
574 1.24 lukem PUTC('\n');
575 1.24 lukem }
576 1.24 lukem
577 1.24 lukem total_bytes += b;
578 1.24 lukem total_bytes_out += b;
579 1.24 lukem (void)fflush(stdout);
580 1.24 lukem (void)fclose(f);
581 1.24 lukem return (1);
582 1.24 lukem }
583 1.24 lukem
584 1.1 lukem /*
585 1.23 lukem * Find s2 at the end of s1. If found, return a string up to (but
586 1.1 lukem * not including) s2, otherwise returns NULL.
587 1.1 lukem */
588 1.1 lukem static char *
589 1.1 lukem strend(s1, s2)
590 1.2 christos const char *s1;
591 1.2 christos char *s2;
592 1.1 lukem {
593 1.25 lukem static char buf[MAXPATHLEN];
594 1.1 lukem
595 1.1 lukem char *start;
596 1.1 lukem size_t l1, l2;
597 1.1 lukem
598 1.1 lukem l1 = strlen(s1);
599 1.1 lukem l2 = strlen(s2);
600 1.1 lukem
601 1.1 lukem if (l2 >= l1)
602 1.1 lukem return(NULL);
603 1.1 lukem
604 1.24 lukem strlcpy(buf, s1, sizeof(buf));
605 1.1 lukem start = buf + (l1 - l2);
606 1.1 lukem
607 1.1 lukem if (strcmp(start, s2) == 0) {
608 1.1 lukem *start = '\0';
609 1.1 lukem return(buf);
610 1.1 lukem } else
611 1.1 lukem return(NULL);
612 1.1 lukem }
613 1.1 lukem
614 1.1 lukem static int
615 1.1 lukem filetypematch(types, mode)
616 1.1 lukem char *types;
617 1.1 lukem int mode;
618 1.1 lukem {
619 1.1 lukem for ( ; types[0] != '\0'; types++)
620 1.1 lukem switch (*types) {
621 1.1 lukem case 'd':
622 1.1 lukem if (S_ISDIR(mode))
623 1.1 lukem return(1);
624 1.1 lukem break;
625 1.1 lukem case 'f':
626 1.1 lukem if (S_ISREG(mode))
627 1.1 lukem return(1);
628 1.1 lukem break;
629 1.1 lukem }
630 1.1 lukem return(0);
631 1.1 lukem }
632 1.1 lukem
633 1.1 lukem /*
634 1.1 lukem * Look for a conversion. If we succeed, return a pointer to the
635 1.1 lukem * command to execute for the conversion.
636 1.1 lukem *
637 1.1 lukem * The command is stored in a static array so there's no memory
638 1.1 lukem * leak problems, and not too much to change in ftpd.c. This
639 1.1 lukem * routine doesn't need to be re-entrant unless we start using a
640 1.1 lukem * multi-threaded ftpd, and that's not likely for a while...
641 1.1 lukem */
642 1.23 lukem char **
643 1.1 lukem do_conversion(fname)
644 1.1 lukem const char *fname;
645 1.1 lukem {
646 1.1 lukem struct ftpconv *cp;
647 1.1 lukem struct stat st;
648 1.1 lukem int o_errno;
649 1.3 christos char *base = NULL;
650 1.23 lukem char *cmd, *p, *lp, **argv;
651 1.23 lukem StringList *sl;
652 1.1 lukem
653 1.1 lukem o_errno = errno;
654 1.23 lukem sl = NULL;
655 1.23 lukem cmd = NULL;
656 1.1 lukem for (cp = curclass.conversions; cp != NULL; cp = cp->next) {
657 1.5 lukem if (cp->suffix == NULL) {
658 1.5 lukem syslog(LOG_WARNING,
659 1.5 lukem "cp->suffix==NULL in conv list; SHOULDN'T HAPPEN!");
660 1.5 lukem continue;
661 1.5 lukem }
662 1.1 lukem if ((base = strend(fname, cp->suffix)) == NULL)
663 1.1 lukem continue;
664 1.5 lukem if (cp->types == NULL || cp->disable == NULL ||
665 1.1 lukem cp->command == NULL)
666 1.1 lukem continue;
667 1.1 lukem /* Is it enabled? */
668 1.1 lukem if (strcmp(cp->disable, ".") != 0 &&
669 1.1 lukem stat(cp->disable, &st) == 0)
670 1.1 lukem continue;
671 1.1 lukem /* Does the base exist? */
672 1.1 lukem if (stat(base, &st) < 0)
673 1.1 lukem continue;
674 1.1 lukem /* Is the file type ok */
675 1.1 lukem if (!filetypematch(cp->types, st.st_mode))
676 1.1 lukem continue;
677 1.1 lukem break; /* "We have a winner!" */
678 1.1 lukem }
679 1.1 lukem
680 1.1 lukem /* If we got through the list, no conversion */
681 1.23 lukem if (cp == NULL)
682 1.23 lukem goto cleanup_do_conv;
683 1.23 lukem
684 1.23 lukem /* Split up command into an argv */
685 1.23 lukem if ((sl = sl_init()) == NULL)
686 1.23 lukem goto cleanup_do_conv;
687 1.23 lukem cmd = xstrdup(cp->command);
688 1.23 lukem p = cmd;
689 1.23 lukem while (p) {
690 1.23 lukem NEXTWORD(p, lp);
691 1.23 lukem if (strcmp(lp, "%s") == 0)
692 1.23 lukem lp = base;
693 1.23 lukem if (sl_add(sl, xstrdup(lp)) == -1)
694 1.23 lukem goto cleanup_do_conv;
695 1.1 lukem }
696 1.1 lukem
697 1.23 lukem if (sl_add(sl, NULL) == -1)
698 1.23 lukem goto cleanup_do_conv;
699 1.23 lukem argv = sl->sl_str;
700 1.23 lukem free(cmd);
701 1.23 lukem free(sl);
702 1.23 lukem return(argv);
703 1.23 lukem
704 1.23 lukem cleanup_do_conv:
705 1.23 lukem if (sl)
706 1.23 lukem sl_free(sl, 1);
707 1.23 lukem free(cmd);
708 1.23 lukem errno = o_errno;
709 1.23 lukem return(NULL);
710 1.24 lukem }
711 1.24 lukem
712 1.24 lukem /*
713 1.24 lukem * Convert the string `arg' to an int, which may have an optional SI suffix
714 1.24 lukem * (`b', `k', `m', `g'). Returns the number for success, -1 otherwise.
715 1.24 lukem */
716 1.24 lukem int
717 1.24 lukem strsuftoi(arg)
718 1.24 lukem const char *arg;
719 1.24 lukem {
720 1.24 lukem char *cp;
721 1.24 lukem long val;
722 1.24 lukem
723 1.24 lukem if (!isdigit((unsigned char)arg[0]))
724 1.24 lukem return (-1);
725 1.24 lukem
726 1.24 lukem val = strtol(arg, &cp, 10);
727 1.24 lukem if (cp != NULL) {
728 1.24 lukem if (cp[0] != '\0' && cp[1] != '\0')
729 1.24 lukem return (-1);
730 1.24 lukem switch (tolower((unsigned char)cp[0])) {
731 1.24 lukem case '\0':
732 1.24 lukem case 'b':
733 1.24 lukem break;
734 1.24 lukem case 'k':
735 1.24 lukem val <<= 10;
736 1.24 lukem break;
737 1.24 lukem case 'm':
738 1.24 lukem val <<= 20;
739 1.24 lukem break;
740 1.24 lukem case 'g':
741 1.24 lukem val <<= 30;
742 1.24 lukem break;
743 1.24 lukem default:
744 1.24 lukem return (-1);
745 1.24 lukem }
746 1.24 lukem }
747 1.24 lukem if (val < 0 || val > INT_MAX)
748 1.24 lukem return (-1);
749 1.24 lukem
750 1.24 lukem return (val);
751 1.25 lukem }
752 1.25 lukem
753 1.26 lukem /*
754 1.26 lukem * Count the number of current connections, reading from
755 1.26 lukem * /var/run/ftpd.pids-<class>
756 1.26 lukem * Does a kill -0 on each pid in that file, and only counts
757 1.26 lukem * processes that exist (or frees the slot if it doesn't).
758 1.26 lukem * Adds getpid() to the first free slot. Truncates the file
759 1.26 lukem * if possible.
760 1.26 lukem */
761 1.25 lukem void
762 1.25 lukem count_users()
763 1.25 lukem {
764 1.25 lukem char fn[MAXPATHLEN];
765 1.25 lukem int fd, i, last;
766 1.25 lukem size_t count;
767 1.25 lukem pid_t *pids, mypid;
768 1.25 lukem struct stat sb;
769 1.25 lukem
770 1.25 lukem (void)strlcpy(fn, _PATH_CLASSPIDS, sizeof(fn));
771 1.25 lukem (void)strlcat(fn, curclass.classname, sizeof(fn));
772 1.25 lukem pids = NULL;
773 1.25 lukem connections = 1;
774 1.25 lukem
775 1.25 lukem if ((fd = open(fn, O_RDWR | O_CREAT | O_EXLOCK, 0600)) == -1)
776 1.25 lukem return;
777 1.25 lukem if (fstat(fd, &sb) == -1)
778 1.25 lukem goto cleanup_count;
779 1.25 lukem if ((pids = malloc(sb.st_size + sizeof(pid_t))) == NULL)
780 1.25 lukem goto cleanup_count;
781 1.25 lukem count = read(fd, pids, sb.st_size);
782 1.25 lukem if (count < 0 || count != sb.st_size)
783 1.25 lukem goto cleanup_count;
784 1.25 lukem count /= sizeof(pid_t);
785 1.25 lukem mypid = getpid();
786 1.25 lukem last = 0;
787 1.25 lukem for (i = 0; i < count; i++) {
788 1.25 lukem if (pids[i] == 0)
789 1.25 lukem continue;
790 1.25 lukem if (kill(pids[i], 0) == -1 && errno != EPERM) {
791 1.25 lukem if (mypid != 0) {
792 1.25 lukem pids[i] = mypid;
793 1.25 lukem mypid = 0;
794 1.25 lukem last = i;
795 1.25 lukem }
796 1.25 lukem } else {
797 1.25 lukem connections++;
798 1.25 lukem last = i;
799 1.25 lukem }
800 1.25 lukem }
801 1.25 lukem if (mypid != 0) {
802 1.25 lukem if (pids[last] != 0)
803 1.25 lukem last++;
804 1.25 lukem pids[last] = mypid;
805 1.25 lukem }
806 1.25 lukem count = (last + 1) * sizeof(pid_t);
807 1.25 lukem if (lseek(fd, 0, SEEK_SET) == -1)
808 1.25 lukem goto cleanup_count;
809 1.25 lukem if (write(fd, pids, count) == -1)
810 1.25 lukem goto cleanup_count;
811 1.25 lukem (void)ftruncate(fd, count);
812 1.25 lukem
813 1.25 lukem cleanup_count:
814 1.25 lukem (void)flock(fd, LOCK_UN);
815 1.25 lukem close(fd);
816 1.25 lukem REASSIGN(pids, NULL);
817 1.1 lukem }
818