conf.c revision 1.24 1 1.24 lukem /* $NetBSD: conf.c,v 1.24 1999/12/12 14:05:54 lukem Exp $ */
2 1.1 lukem
3 1.1 lukem /*-
4 1.24 lukem * Copyright (c) 1997-1999 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.24 lukem __RCSID("$NetBSD: conf.c,v 1.24 1999/12/12 14:05:54 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.1 lukem #include <glob.h>
51 1.1 lukem #include <stdio.h>
52 1.2 christos #include <stdlib.h>
53 1.1 lukem #include <string.h>
54 1.1 lukem #include <stringlist.h>
55 1.1 lukem #include <syslog.h>
56 1.23 lukem #include <time.h>
57 1.23 lukem #include <unistd.h>
58 1.23 lukem #include <util.h>
59 1.18 explorer
60 1.18 explorer #ifdef KERBEROS5
61 1.21 christos #include <krb5/krb5.h>
62 1.18 explorer #endif
63 1.1 lukem
64 1.1 lukem #include "extern.h"
65 1.1 lukem #include "pathnames.h"
66 1.1 lukem
67 1.2 christos static char *strend __P((const char *, char *));
68 1.2 christos static int filetypematch __P((char *, int));
69 1.15 lukem
70 1.15 lukem struct ftpclass curclass;
71 1.15 lukem
72 1.1 lukem
73 1.1 lukem /*
74 1.1 lukem * Parse the configuration file, looking for the named class, and
75 1.1 lukem * define curclass to contain the appropriate settings.
76 1.1 lukem */
77 1.1 lukem void
78 1.1 lukem parse_conf(findclass)
79 1.2 christos char *findclass;
80 1.1 lukem {
81 1.1 lukem FILE *f;
82 1.1 lukem char *buf, *p;
83 1.1 lukem size_t len;
84 1.24 lukem int none, match, rate;
85 1.1 lukem char *endp;
86 1.1 lukem char *class, *word, *arg;
87 1.1 lukem const char *infile;
88 1.23 lukem size_t line;
89 1.1 lukem unsigned int timeout;
90 1.1 lukem struct ftpconv *conv, *cnext;
91 1.1 lukem
92 1.24 lukem REASSIGN(curclass.classname, xstrdup(findclass));
93 1.23 lukem for (conv = curclass.conversions; conv != NULL; conv = cnext) {
94 1.1 lukem REASSIGN(conv->suffix, NULL);
95 1.1 lukem REASSIGN(conv->types, NULL);
96 1.1 lukem REASSIGN(conv->disable, NULL);
97 1.1 lukem REASSIGN(conv->command, NULL);
98 1.1 lukem cnext = conv->next;
99 1.1 lukem free(conv);
100 1.1 lukem }
101 1.9 lukem curclass.checkportcmd = 0;
102 1.1 lukem curclass.conversions = NULL;
103 1.1 lukem REASSIGN(curclass.display, NULL);
104 1.24 lukem curclass.maxrateget = 0;
105 1.24 lukem curclass.maxrateput = 0;
106 1.9 lukem curclass.maxtimeout = 7200; /* 2 hours */
107 1.1 lukem curclass.modify = 1;
108 1.24 lukem REASSIGN(curclass.motd, xstrdup(_PATH_FTPLOGINMESG));
109 1.1 lukem REASSIGN(curclass.notify, NULL);
110 1.14 tv curclass.passive = 1;
111 1.24 lukem curclass.maxrateget = 0;
112 1.24 lukem curclass.maxrateput = 0;
113 1.24 lukem curclass.rateget = 0;
114 1.24 lukem curclass.rateput = 0;
115 1.1 lukem curclass.timeout = 900; /* 15 minutes */
116 1.1 lukem curclass.umask = 027;
117 1.24 lukem curclass.upload = 1;
118 1.1 lukem
119 1.1 lukem if (strcasecmp(findclass, "guest") == 0) {
120 1.9 lukem curclass.modify = 0;
121 1.1 lukem curclass.umask = 0707;
122 1.1 lukem }
123 1.1 lukem
124 1.6 lukem infile = conffilename(_PATH_FTPDCONF);
125 1.1 lukem if ((f = fopen(infile, "r")) == NULL)
126 1.1 lukem return;
127 1.1 lukem
128 1.1 lukem line = 0;
129 1.23 lukem for (;
130 1.23 lukem (buf = fparseln(f, &len, &line, NULL, FPARSELN_UNESCCOMM |
131 1.23 lukem FPARSELN_UNESCCONT | FPARSELN_UNESCESC)) != NULL;
132 1.23 lukem free(buf)) {
133 1.1 lukem none = match = 0;
134 1.23 lukem p = buf;
135 1.5 lukem if (len < 1)
136 1.5 lukem continue;
137 1.23 lukem if (p[len - 1] == '\n')
138 1.23 lukem p[--len] = '\0';
139 1.23 lukem if (EMPTYSTR(p))
140 1.1 lukem continue;
141 1.1 lukem
142 1.23 lukem NEXTWORD(p, word);
143 1.23 lukem NEXTWORD(p, class);
144 1.23 lukem NEXTWORD(p, arg);
145 1.1 lukem if (EMPTYSTR(word) || EMPTYSTR(class))
146 1.1 lukem continue;
147 1.1 lukem if (strcasecmp(class, "none") == 0)
148 1.1 lukem none = 1;
149 1.1 lukem if (strcasecmp(class, findclass) != 0 &&
150 1.1 lukem !none && strcasecmp(class, "all") != 0)
151 1.1 lukem continue;
152 1.1 lukem
153 1.9 lukem if (strcasecmp(word, "checkportcmd") == 0) {
154 1.9 lukem if (none ||
155 1.9 lukem (!EMPTYSTR(arg) && strcasecmp(arg, "off") == 0))
156 1.9 lukem curclass.checkportcmd = 0;
157 1.9 lukem else
158 1.9 lukem curclass.checkportcmd = 1;
159 1.23 lukem
160 1.24 lukem } else if (strcasecmp(word, "classtype") == 0) {
161 1.24 lukem if (!none && !EMPTYSTR(arg)) {
162 1.24 lukem if (strcasecmp(arg, "GUEST") == 0)
163 1.24 lukem curclass.type = CLASS_GUEST;
164 1.24 lukem else if (strcasecmp(arg, "CHROOT") == 0)
165 1.24 lukem curclass.type = CLASS_CHROOT;
166 1.24 lukem else if (strcasecmp(arg, "REAL") == 0)
167 1.24 lukem curclass.type = CLASS_REAL;
168 1.24 lukem else {
169 1.24 lukem syslog(LOG_WARNING,
170 1.24 lukem "%s line %d: unknown class type `%s'",
171 1.24 lukem infile, (int)line, arg);
172 1.24 lukem continue;
173 1.24 lukem }
174 1.24 lukem }
175 1.24 lukem
176 1.9 lukem } else if (strcasecmp(word, "conversion") == 0) {
177 1.5 lukem char *suffix, *types, *disable, *convcmd;
178 1.5 lukem
179 1.1 lukem if (EMPTYSTR(arg)) {
180 1.1 lukem syslog(LOG_WARNING,
181 1.1 lukem "%s line %d: %s requires a suffix",
182 1.23 lukem infile, (int)line, word);
183 1.1 lukem continue; /* need a suffix */
184 1.1 lukem }
185 1.23 lukem NEXTWORD(p, types);
186 1.23 lukem NEXTWORD(p, disable);
187 1.23 lukem convcmd = p;
188 1.1 lukem if (convcmd)
189 1.1 lukem convcmd += strspn(convcmd, " \t");
190 1.23 lukem suffix = xstrdup(arg);
191 1.1 lukem if (none || EMPTYSTR(types) ||
192 1.1 lukem EMPTYSTR(disable) || EMPTYSTR(convcmd)) {
193 1.1 lukem types = NULL;
194 1.1 lukem disable = NULL;
195 1.1 lukem convcmd = NULL;
196 1.1 lukem } else {
197 1.23 lukem types = xstrdup(types);
198 1.23 lukem disable = xstrdup(disable);
199 1.23 lukem convcmd = xstrdup(convcmd);
200 1.1 lukem }
201 1.1 lukem for (conv = curclass.conversions; conv != NULL;
202 1.1 lukem conv = conv->next) {
203 1.5 lukem if (strcmp(conv->suffix, suffix) == 0)
204 1.1 lukem break;
205 1.1 lukem }
206 1.1 lukem if (conv == NULL) {
207 1.1 lukem conv = (struct ftpconv *)
208 1.1 lukem calloc(1, sizeof(struct ftpconv));
209 1.1 lukem if (conv == NULL) {
210 1.1 lukem syslog(LOG_WARNING, "can't malloc");
211 1.1 lukem continue;
212 1.1 lukem }
213 1.23 lukem conv->next = NULL;
214 1.23 lukem for (cnext = curclass.conversions;
215 1.23 lukem cnext != NULL; cnext = cnext->next)
216 1.23 lukem if (cnext->next == NULL)
217 1.23 lukem break;
218 1.23 lukem if (cnext != NULL)
219 1.23 lukem cnext->next = conv;
220 1.23 lukem else
221 1.23 lukem curclass.conversions = conv;
222 1.1 lukem }
223 1.5 lukem REASSIGN(conv->suffix, suffix);
224 1.1 lukem REASSIGN(conv->types, types);
225 1.1 lukem REASSIGN(conv->disable, disable);
226 1.1 lukem REASSIGN(conv->command, convcmd);
227 1.23 lukem
228 1.1 lukem } else if (strcasecmp(word, "display") == 0) {
229 1.1 lukem if (none || EMPTYSTR(arg))
230 1.1 lukem arg = NULL;
231 1.1 lukem else
232 1.23 lukem arg = xstrdup(arg);
233 1.1 lukem REASSIGN(curclass.display, arg);
234 1.23 lukem
235 1.1 lukem } else if (strcasecmp(word, "maxtimeout") == 0) {
236 1.1 lukem if (none || EMPTYSTR(arg))
237 1.1 lukem continue;
238 1.1 lukem timeout = (unsigned int)strtoul(arg, &endp, 10);
239 1.1 lukem if (*endp != 0) {
240 1.1 lukem syslog(LOG_WARNING,
241 1.1 lukem "%s line %d: invalid maxtimeout %s",
242 1.23 lukem infile, (int)line, arg);
243 1.1 lukem continue;
244 1.1 lukem }
245 1.1 lukem if (timeout < 30) {
246 1.1 lukem syslog(LOG_WARNING,
247 1.1 lukem "%s line %d: maxtimeout %d < 30 seconds",
248 1.23 lukem infile, (int)line, timeout);
249 1.1 lukem continue;
250 1.1 lukem }
251 1.1 lukem if (timeout < curclass.timeout) {
252 1.1 lukem syslog(LOG_WARNING,
253 1.1 lukem "%s line %d: maxtimeout %d < timeout (%d)",
254 1.23 lukem infile, (int)line, timeout,
255 1.23 lukem curclass.timeout);
256 1.1 lukem continue;
257 1.1 lukem }
258 1.1 lukem curclass.maxtimeout = timeout;
259 1.23 lukem
260 1.1 lukem } else if (strcasecmp(word, "modify") == 0) {
261 1.1 lukem if (none ||
262 1.2 christos (!EMPTYSTR(arg) && strcasecmp(arg, "off") == 0))
263 1.1 lukem curclass.modify = 0;
264 1.1 lukem else
265 1.1 lukem curclass.modify = 1;
266 1.23 lukem
267 1.24 lukem } else if (strcasecmp(word, "motd") == 0) {
268 1.24 lukem if (none || EMPTYSTR(arg))
269 1.24 lukem arg = NULL;
270 1.24 lukem else
271 1.24 lukem arg = xstrdup(arg);
272 1.24 lukem REASSIGN(curclass.motd, arg);
273 1.24 lukem
274 1.24 lukem
275 1.1 lukem } else if (strcasecmp(word, "notify") == 0) {
276 1.1 lukem if (none || EMPTYSTR(arg))
277 1.1 lukem arg = NULL;
278 1.1 lukem else
279 1.23 lukem arg = xstrdup(arg);
280 1.1 lukem REASSIGN(curclass.notify, arg);
281 1.23 lukem
282 1.14 tv } else if (strcasecmp(word, "passive") == 0) {
283 1.14 tv if (none ||
284 1.14 tv (!EMPTYSTR(arg) && strcasecmp(arg, "off") == 0))
285 1.14 tv curclass.passive = 0;
286 1.14 tv else
287 1.14 tv curclass.passive = 1;
288 1.23 lukem
289 1.24 lukem } else if (strcasecmp(word, "rateget") == 0) {
290 1.24 lukem if (none || EMPTYSTR(arg))
291 1.24 lukem continue;
292 1.24 lukem rate = strsuftoi(arg);
293 1.24 lukem if (rate == -1) {
294 1.24 lukem syslog(LOG_WARNING,
295 1.24 lukem "%s line %d: invalid rateget %s",
296 1.24 lukem infile, (int)line, arg);
297 1.24 lukem continue;
298 1.24 lukem }
299 1.24 lukem curclass.maxrateget = rate;
300 1.24 lukem curclass.rateget = rate;
301 1.24 lukem
302 1.24 lukem } else if (strcasecmp(word, "rateput") == 0) {
303 1.24 lukem if (none || EMPTYSTR(arg))
304 1.24 lukem continue;
305 1.24 lukem rate = strsuftoi(arg);
306 1.24 lukem if (rate == -1) {
307 1.24 lukem syslog(LOG_WARNING,
308 1.24 lukem "%s line %d: invalid rateput %s",
309 1.24 lukem infile, (int)line, arg);
310 1.24 lukem continue;
311 1.24 lukem }
312 1.24 lukem curclass.maxrateput = rate;
313 1.24 lukem curclass.rateput = rate;
314 1.24 lukem
315 1.1 lukem } else if (strcasecmp(word, "timeout") == 0) {
316 1.1 lukem if (none || EMPTYSTR(arg))
317 1.1 lukem continue;
318 1.1 lukem timeout = (unsigned int)strtoul(arg, &endp, 10);
319 1.1 lukem if (*endp != 0) {
320 1.1 lukem syslog(LOG_WARNING,
321 1.1 lukem "%s line %d: invalid timeout %s",
322 1.23 lukem infile, (int)line, arg);
323 1.1 lukem continue;
324 1.1 lukem }
325 1.1 lukem if (timeout < 30) {
326 1.1 lukem syslog(LOG_WARNING,
327 1.1 lukem "%s line %d: timeout %d < 30 seconds",
328 1.23 lukem infile, (int)line, timeout);
329 1.1 lukem continue;
330 1.1 lukem }
331 1.1 lukem if (timeout > curclass.maxtimeout) {
332 1.1 lukem syslog(LOG_WARNING,
333 1.1 lukem "%s line %d: timeout %d > maxtimeout (%d)",
334 1.23 lukem infile, (int)line, timeout,
335 1.23 lukem curclass.maxtimeout);
336 1.1 lukem continue;
337 1.1 lukem }
338 1.1 lukem curclass.timeout = timeout;
339 1.23 lukem
340 1.1 lukem } else if (strcasecmp(word, "umask") == 0) {
341 1.1 lukem mode_t umask;
342 1.1 lukem
343 1.1 lukem if (none || EMPTYSTR(arg))
344 1.1 lukem continue;
345 1.1 lukem umask = (mode_t)strtoul(arg, &endp, 8);
346 1.1 lukem if (*endp != 0 || umask > 0777) {
347 1.1 lukem syslog(LOG_WARNING,
348 1.1 lukem "%s line %d: invalid umask %s",
349 1.23 lukem infile, (int)line, arg);
350 1.1 lukem continue;
351 1.1 lukem }
352 1.1 lukem curclass.umask = umask;
353 1.23 lukem
354 1.24 lukem } else if (strcasecmp(word, "upload") == 0) {
355 1.24 lukem if (none ||
356 1.24 lukem (!EMPTYSTR(arg) && strcasecmp(arg, "off") == 0)) {
357 1.24 lukem curclass.modify = 0;
358 1.24 lukem curclass.upload = 0;
359 1.24 lukem } else
360 1.24 lukem curclass.upload = 1;
361 1.24 lukem
362 1.1 lukem } else {
363 1.1 lukem syslog(LOG_WARNING,
364 1.1 lukem "%s line %d: unknown directive '%s'",
365 1.23 lukem infile, (int)line, word);
366 1.1 lukem continue;
367 1.1 lukem }
368 1.1 lukem }
369 1.1 lukem fclose(f);
370 1.1 lukem }
371 1.1 lukem
372 1.1 lukem /*
373 1.1 lukem * Show file listed in curclass.display first time in, and list all the
374 1.1 lukem * files named in curclass.notify in the current directory. Send back
375 1.17 lukem * responses with the prefix `code' + "-".
376 1.1 lukem */
377 1.1 lukem void
378 1.1 lukem show_chdir_messages(code)
379 1.1 lukem int code;
380 1.1 lukem {
381 1.1 lukem static StringList *slist = NULL;
382 1.1 lukem
383 1.1 lukem struct stat st;
384 1.1 lukem struct tm *t;
385 1.1 lukem glob_t gl;
386 1.1 lukem time_t now, then;
387 1.1 lukem int age;
388 1.1 lukem char cwd[MAXPATHLEN + 1];
389 1.1 lukem char *cp, **rlist;
390 1.1 lukem
391 1.1 lukem /* Setup list for directory cache */
392 1.1 lukem if (slist == NULL)
393 1.1 lukem slist = sl_init();
394 1.22 lukem if (slist == NULL) {
395 1.22 lukem syslog(LOG_WARNING, "can't allocate memory for stringlist");
396 1.22 lukem return;
397 1.22 lukem }
398 1.1 lukem
399 1.1 lukem /* Check if this directory has already been visited */
400 1.1 lukem if (getcwd(cwd, sizeof(cwd) - 1) == NULL) {
401 1.13 mouse syslog(LOG_WARNING, "can't getcwd: %s", strerror(errno));
402 1.1 lukem return;
403 1.1 lukem }
404 1.1 lukem if (sl_find(slist, cwd) != NULL)
405 1.1 lukem return;
406 1.1 lukem
407 1.23 lukem cp = xstrdup(cwd);
408 1.22 lukem if (sl_add(slist, cp) == -1)
409 1.22 lukem syslog(LOG_WARNING, "can't add `%s' to stringlist", cp);
410 1.1 lukem
411 1.1 lukem /* First check for a display file */
412 1.24 lukem (void)format_file(curclass.display, code);
413 1.1 lukem
414 1.1 lukem /* Now see if there are any notify files */
415 1.24 lukem if (EMPTYSTR(curclass.notify))
416 1.1 lukem return;
417 1.1 lukem
418 1.1 lukem if (glob(curclass.notify, 0, NULL, &gl) != 0 || gl.gl_matchc == 0)
419 1.1 lukem return;
420 1.1 lukem time(&now);
421 1.1 lukem for (rlist = gl.gl_pathv; *rlist != NULL; rlist++) {
422 1.1 lukem if (stat(*rlist, &st) != 0)
423 1.1 lukem continue;
424 1.7 mycroft if (!S_ISREG(st.st_mode))
425 1.1 lukem continue;
426 1.1 lukem then = st.st_mtime;
427 1.20 lukem if (code != 0) {
428 1.20 lukem lreply(code, "");
429 1.20 lukem code = 0;
430 1.20 lukem }
431 1.1 lukem lreply(code, "Please read the file %s", *rlist);
432 1.1 lukem t = localtime(&now);
433 1.1 lukem age = 365 * t->tm_year + t->tm_yday;
434 1.1 lukem t = localtime(&then);
435 1.1 lukem age -= 365 * t->tm_year + t->tm_yday;
436 1.1 lukem lreply(code, " it was last modified on %.24s - %d day%s ago",
437 1.19 lukem ctime(&then), age, PLURAL(age));
438 1.1 lukem }
439 1.1 lukem globfree(&gl);
440 1.1 lukem }
441 1.1 lukem
442 1.24 lukem int
443 1.24 lukem format_file(file, code)
444 1.24 lukem const char *file;
445 1.24 lukem int code;
446 1.24 lukem {
447 1.24 lukem FILE *f;
448 1.24 lukem char *buf, *p, *cwd;
449 1.24 lukem size_t len;
450 1.24 lukem off_t b;
451 1.24 lukem time_t now;
452 1.24 lukem
453 1.24 lukem #define PUTC(x) putchar(x), b++
454 1.24 lukem
455 1.24 lukem if (EMPTYSTR(file))
456 1.24 lukem return(0);
457 1.24 lukem if ((f = fopen(file, "r")) == NULL)
458 1.24 lukem return (0);
459 1.24 lukem lreply(code, "");
460 1.24 lukem
461 1.24 lukem b = 0;
462 1.24 lukem for (;
463 1.24 lukem (buf = fparseln(f, &len, NULL, "\0\0\0", 0)) != NULL; free(buf)) {
464 1.24 lukem if (len > 0)
465 1.24 lukem if (buf[len - 1] == '\n')
466 1.24 lukem buf[--len] = '\0';
467 1.24 lukem b += printf(" ");
468 1.24 lukem
469 1.24 lukem for (p = buf; *p; p++) {
470 1.24 lukem if (*p == '%') {
471 1.24 lukem p++;
472 1.24 lukem switch (*p) {
473 1.24 lukem case 'C':
474 1.24 lukem if (getcwd(cwd, sizeof(cwd)-1) == NULL){
475 1.24 lukem syslog(LOG_WARNING,
476 1.24 lukem "can't getcwd: %s",
477 1.24 lukem strerror(errno));
478 1.24 lukem continue;
479 1.24 lukem }
480 1.24 lukem b += printf("%s", cwd);
481 1.24 lukem break;
482 1.24 lukem case 'E':
483 1.24 lukem /* XXXX email address */
484 1.24 lukem break;
485 1.24 lukem case 'L':
486 1.24 lukem b += printf("%s", hostname);
487 1.24 lukem break;
488 1.24 lukem case 'R':
489 1.24 lukem b += printf("%s", remotehost);
490 1.24 lukem break;
491 1.24 lukem case 'T':
492 1.24 lukem now = time(NULL);
493 1.24 lukem b += printf("%.25s", ctime(&now));
494 1.24 lukem break;
495 1.24 lukem case 'U':
496 1.24 lukem b += printf("%s",
497 1.24 lukem pw ? pw->pw_name : "<unknown>");
498 1.24 lukem break;
499 1.24 lukem case '%':
500 1.24 lukem PUTC('%');
501 1.24 lukem break;
502 1.24 lukem }
503 1.24 lukem } else {
504 1.24 lukem PUTC(*p);
505 1.24 lukem }
506 1.24 lukem }
507 1.24 lukem PUTC('\r');
508 1.24 lukem PUTC('\n');
509 1.24 lukem }
510 1.24 lukem
511 1.24 lukem total_bytes += b;
512 1.24 lukem total_bytes_out += b;
513 1.24 lukem (void)fflush(stdout);
514 1.24 lukem (void)fclose(f);
515 1.24 lukem return (1);
516 1.24 lukem }
517 1.24 lukem
518 1.1 lukem /*
519 1.23 lukem * Find s2 at the end of s1. If found, return a string up to (but
520 1.1 lukem * not including) s2, otherwise returns NULL.
521 1.1 lukem */
522 1.1 lukem static char *
523 1.1 lukem strend(s1, s2)
524 1.2 christos const char *s1;
525 1.2 christos char *s2;
526 1.1 lukem {
527 1.1 lukem static char buf[MAXPATHLEN + 1];
528 1.1 lukem
529 1.1 lukem char *start;
530 1.1 lukem size_t l1, l2;
531 1.1 lukem
532 1.1 lukem l1 = strlen(s1);
533 1.1 lukem l2 = strlen(s2);
534 1.1 lukem
535 1.1 lukem if (l2 >= l1)
536 1.1 lukem return(NULL);
537 1.1 lukem
538 1.24 lukem strlcpy(buf, s1, sizeof(buf));
539 1.1 lukem start = buf + (l1 - l2);
540 1.1 lukem
541 1.1 lukem if (strcmp(start, s2) == 0) {
542 1.1 lukem *start = '\0';
543 1.1 lukem return(buf);
544 1.1 lukem } else
545 1.1 lukem return(NULL);
546 1.1 lukem }
547 1.1 lukem
548 1.1 lukem static int
549 1.1 lukem filetypematch(types, mode)
550 1.1 lukem char *types;
551 1.1 lukem int mode;
552 1.1 lukem {
553 1.1 lukem for ( ; types[0] != '\0'; types++)
554 1.1 lukem switch (*types) {
555 1.1 lukem case 'd':
556 1.1 lukem if (S_ISDIR(mode))
557 1.1 lukem return(1);
558 1.1 lukem break;
559 1.1 lukem case 'f':
560 1.1 lukem if (S_ISREG(mode))
561 1.1 lukem return(1);
562 1.1 lukem break;
563 1.1 lukem }
564 1.1 lukem return(0);
565 1.1 lukem }
566 1.1 lukem
567 1.1 lukem /*
568 1.1 lukem * Look for a conversion. If we succeed, return a pointer to the
569 1.1 lukem * command to execute for the conversion.
570 1.1 lukem *
571 1.1 lukem * The command is stored in a static array so there's no memory
572 1.1 lukem * leak problems, and not too much to change in ftpd.c. This
573 1.1 lukem * routine doesn't need to be re-entrant unless we start using a
574 1.1 lukem * multi-threaded ftpd, and that's not likely for a while...
575 1.1 lukem */
576 1.23 lukem char **
577 1.1 lukem do_conversion(fname)
578 1.1 lukem const char *fname;
579 1.1 lukem {
580 1.1 lukem struct ftpconv *cp;
581 1.1 lukem struct stat st;
582 1.1 lukem int o_errno;
583 1.3 christos char *base = NULL;
584 1.23 lukem char *cmd, *p, *lp, **argv;
585 1.23 lukem StringList *sl;
586 1.1 lukem
587 1.1 lukem o_errno = errno;
588 1.23 lukem sl = NULL;
589 1.23 lukem cmd = NULL;
590 1.1 lukem for (cp = curclass.conversions; cp != NULL; cp = cp->next) {
591 1.5 lukem if (cp->suffix == NULL) {
592 1.5 lukem syslog(LOG_WARNING,
593 1.5 lukem "cp->suffix==NULL in conv list; SHOULDN'T HAPPEN!");
594 1.5 lukem continue;
595 1.5 lukem }
596 1.1 lukem if ((base = strend(fname, cp->suffix)) == NULL)
597 1.1 lukem continue;
598 1.5 lukem if (cp->types == NULL || cp->disable == NULL ||
599 1.1 lukem cp->command == NULL)
600 1.1 lukem continue;
601 1.1 lukem /* Is it enabled? */
602 1.1 lukem if (strcmp(cp->disable, ".") != 0 &&
603 1.1 lukem stat(cp->disable, &st) == 0)
604 1.1 lukem continue;
605 1.1 lukem /* Does the base exist? */
606 1.1 lukem if (stat(base, &st) < 0)
607 1.1 lukem continue;
608 1.1 lukem /* Is the file type ok */
609 1.1 lukem if (!filetypematch(cp->types, st.st_mode))
610 1.1 lukem continue;
611 1.1 lukem break; /* "We have a winner!" */
612 1.1 lukem }
613 1.1 lukem
614 1.1 lukem /* If we got through the list, no conversion */
615 1.23 lukem if (cp == NULL)
616 1.23 lukem goto cleanup_do_conv;
617 1.23 lukem
618 1.23 lukem /* Split up command into an argv */
619 1.23 lukem if ((sl = sl_init()) == NULL)
620 1.23 lukem goto cleanup_do_conv;
621 1.23 lukem cmd = xstrdup(cp->command);
622 1.23 lukem p = cmd;
623 1.23 lukem while (p) {
624 1.23 lukem NEXTWORD(p, lp);
625 1.23 lukem if (strcmp(lp, "%s") == 0)
626 1.23 lukem lp = base;
627 1.23 lukem if (sl_add(sl, xstrdup(lp)) == -1)
628 1.23 lukem goto cleanup_do_conv;
629 1.1 lukem }
630 1.1 lukem
631 1.23 lukem if (sl_add(sl, NULL) == -1)
632 1.23 lukem goto cleanup_do_conv;
633 1.23 lukem argv = sl->sl_str;
634 1.23 lukem free(cmd);
635 1.23 lukem free(sl);
636 1.23 lukem return(argv);
637 1.23 lukem
638 1.23 lukem cleanup_do_conv:
639 1.23 lukem if (sl)
640 1.23 lukem sl_free(sl, 1);
641 1.23 lukem free(cmd);
642 1.23 lukem errno = o_errno;
643 1.23 lukem return(NULL);
644 1.24 lukem }
645 1.24 lukem
646 1.24 lukem /*
647 1.24 lukem * Convert the string `arg' to an int, which may have an optional SI suffix
648 1.24 lukem * (`b', `k', `m', `g'). Returns the number for success, -1 otherwise.
649 1.24 lukem */
650 1.24 lukem int
651 1.24 lukem strsuftoi(arg)
652 1.24 lukem const char *arg;
653 1.24 lukem {
654 1.24 lukem char *cp;
655 1.24 lukem long val;
656 1.24 lukem
657 1.24 lukem if (!isdigit((unsigned char)arg[0]))
658 1.24 lukem return (-1);
659 1.24 lukem
660 1.24 lukem val = strtol(arg, &cp, 10);
661 1.24 lukem if (cp != NULL) {
662 1.24 lukem if (cp[0] != '\0' && cp[1] != '\0')
663 1.24 lukem return (-1);
664 1.24 lukem switch (tolower((unsigned char)cp[0])) {
665 1.24 lukem case '\0':
666 1.24 lukem case 'b':
667 1.24 lukem break;
668 1.24 lukem case 'k':
669 1.24 lukem val <<= 10;
670 1.24 lukem break;
671 1.24 lukem case 'm':
672 1.24 lukem val <<= 20;
673 1.24 lukem break;
674 1.24 lukem case 'g':
675 1.24 lukem val <<= 30;
676 1.24 lukem break;
677 1.24 lukem default:
678 1.24 lukem return (-1);
679 1.24 lukem }
680 1.24 lukem }
681 1.24 lukem if (val < 0 || val > INT_MAX)
682 1.24 lukem return (-1);
683 1.24 lukem
684 1.24 lukem return (val);
685 1.1 lukem }
686