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