conf.c revision 1.45 1 /* $NetBSD: conf.c,v 1.45 2001/12/01 10:25:29 lukem Exp $ */
2
3 /*-
4 * Copyright (c) 1997-2001 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.45 2001/12/01 10:25:29 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 || (strcasecmp(word, "advertize") == 0)) {
213 struct addrinfo hints, *res;
214 int error;
215
216 memset((char *)&curclass.advertise, 0,
217 sizeof(curclass.advertise));
218 curclass.advertise.su_len = 0;
219 if (none || EMPTYSTR(arg))
220 continue;
221 res = NULL;
222 memset(&hints, 0, sizeof(hints));
223 /*
224 * only get addresses of the family
225 * that we're listening on
226 */
227 hints.ai_family = ctrl_addr.su_family;
228 hints.ai_socktype = SOCK_STREAM;
229 error = getaddrinfo(arg, "0", &hints, &res);
230 if (error) {
231 syslog(LOG_WARNING, "%s line %d: %s",
232 infile, (int)line, gai_strerror(error));
233 advertiseparsefail:
234 if (res)
235 freeaddrinfo(res);
236 continue;
237 }
238 if (res->ai_next) {
239 syslog(LOG_WARNING,
240 "%s line %d: multiple addresses returned for `%s'; please be more specific",
241 infile, (int)line, arg);
242 goto advertiseparsefail;
243 }
244 if (sizeof(curclass.advertise) < res->ai_addrlen || (
245 #ifdef INET6
246 res->ai_family != AF_INET6 &&
247 #endif
248 res->ai_family != AF_INET)) {
249 syslog(LOG_WARNING,
250 "%s line %d: unsupported protocol %d for `%s'",
251 infile, (int)line, res->ai_family, arg);
252 goto advertiseparsefail;
253 }
254 memcpy(&curclass.advertise, res->ai_addr,
255 res->ai_addrlen);
256 curclass.advertise.su_len = res->ai_addrlen;
257 freeaddrinfo(res);
258
259 } else if (strcasecmp(word, "checkportcmd") == 0) {
260 CONF_FLAG(checkportcmd);
261
262 } else if (strcasecmp(word, "chroot") == 0) {
263 CONF_STRING(chroot);
264
265 } else if (strcasecmp(word, "classtype") == 0) {
266 if (!none && !EMPTYSTR(arg)) {
267 if (strcasecmp(arg, "GUEST") == 0)
268 curclass.type = CLASS_GUEST;
269 else if (strcasecmp(arg, "CHROOT") == 0)
270 curclass.type = CLASS_CHROOT;
271 else if (strcasecmp(arg, "REAL") == 0)
272 curclass.type = CLASS_REAL;
273 else {
274 syslog(LOG_WARNING,
275 "%s line %d: unknown class type `%s'",
276 infile, (int)line, arg);
277 continue;
278 }
279 }
280
281 } else if (strcasecmp(word, "conversion") == 0) {
282 char *suffix, *types, *disable, *convcmd;
283
284 if (EMPTYSTR(arg)) {
285 syslog(LOG_WARNING,
286 "%s line %d: %s requires a suffix",
287 infile, (int)line, word);
288 continue; /* need a suffix */
289 }
290 NEXTWORD(p, types);
291 NEXTWORD(p, disable);
292 convcmd = p;
293 if (convcmd)
294 convcmd += strspn(convcmd, " \t");
295 suffix = xstrdup(arg);
296 if (none || EMPTYSTR(types) ||
297 EMPTYSTR(disable) || EMPTYSTR(convcmd)) {
298 types = NULL;
299 disable = NULL;
300 convcmd = NULL;
301 } else {
302 types = xstrdup(types);
303 disable = xstrdup(disable);
304 convcmd = xstrdup(convcmd);
305 }
306 for (conv = curclass.conversions; conv != NULL;
307 conv = conv->next) {
308 if (strcmp(conv->suffix, suffix) == 0)
309 break;
310 }
311 if (conv == NULL) {
312 conv = (struct ftpconv *)
313 calloc(1, sizeof(struct ftpconv));
314 if (conv == NULL) {
315 syslog(LOG_WARNING, "can't malloc");
316 continue;
317 }
318 conv->next = NULL;
319 for (cnext = curclass.conversions;
320 cnext != NULL; cnext = cnext->next)
321 if (cnext->next == NULL)
322 break;
323 if (cnext != NULL)
324 cnext->next = conv;
325 else
326 curclass.conversions = conv;
327 }
328 REASSIGN(conv->suffix, suffix);
329 REASSIGN(conv->types, types);
330 REASSIGN(conv->disable, disable);
331 REASSIGN(conv->command, convcmd);
332
333 } else if (strcasecmp(word, "display") == 0) {
334 CONF_STRING(display);
335
336 } else if (strcasecmp(word, "homedir") == 0) {
337 CONF_STRING(homedir);
338
339 } else if (strcasecmp(word, "limit") == 0) {
340 int limit;
341
342 curclass.limit = DEFAULT_LIMIT;
343 REASSIGN(curclass.limitfile, NULL);
344 if (none || EMPTYSTR(arg))
345 continue;
346 limit = (int)strtol(arg, &endp, 10);
347 if (*endp != 0) {
348 syslog(LOG_WARNING,
349 "%s line %d: invalid limit %s",
350 infile, (int)line, arg);
351 continue;
352 }
353 curclass.limit = limit;
354 REASSIGN(curclass.limitfile,
355 EMPTYSTR(p) ? NULL : xstrdup(p));
356
357 } else if (strcasecmp(word, "maxfilesize") == 0) {
358 curclass.maxfilesize = DEFAULT_MAXFILESIZE;
359 if (none || EMPTYSTR(arg))
360 continue;
361 llval = strsuftoll(arg);
362 if (llval == -1) {
363 syslog(LOG_WARNING,
364 "%s line %d: invalid maxfilesize %s",
365 infile, (int)line, arg);
366 continue;
367 }
368 curclass.maxfilesize = llval;
369
370 } else if (strcasecmp(word, "maxtimeout") == 0) {
371 curclass.maxtimeout = DEFAULT_MAXTIMEOUT;
372 if (none || EMPTYSTR(arg))
373 continue;
374 timeout = (unsigned int)strtoul(arg, &endp, 10);
375 if (*endp != 0) {
376 syslog(LOG_WARNING,
377 "%s line %d: invalid maxtimeout %s",
378 infile, (int)line, arg);
379 continue;
380 }
381 if (timeout < 30) {
382 syslog(LOG_WARNING,
383 "%s line %d: maxtimeout %d < 30 seconds",
384 infile, (int)line, timeout);
385 continue;
386 }
387 if (timeout < curclass.timeout) {
388 syslog(LOG_WARNING,
389 "%s line %d: maxtimeout %d < timeout (%d)",
390 infile, (int)line, timeout,
391 curclass.timeout);
392 continue;
393 }
394 curclass.maxtimeout = timeout;
395
396 } else if (strcasecmp(word, "modify") == 0) {
397 CONF_FLAG(modify);
398
399 } else if (strcasecmp(word, "motd") == 0) {
400 CONF_STRING(motd);
401
402 } else if (strcasecmp(word, "notify") == 0) {
403 CONF_STRING(notify);
404
405 } else if (strcasecmp(word, "passive") == 0) {
406 CONF_FLAG(passive);
407
408 } else if (strcasecmp(word, "portrange") == 0) {
409 int minport, maxport;
410 char *min, *max;
411
412 curclass.portmin = 0;
413 curclass.portmax = 0;
414 if (none || EMPTYSTR(arg))
415 continue;
416 min = arg;
417 NEXTWORD(p, max);
418 if (EMPTYSTR(max)) {
419 syslog(LOG_WARNING,
420 "%s line %d: missing maxport argument",
421 infile, (int)line);
422 continue;
423 }
424 minport = (int)strtol(min, &endp, 10);
425 if (*endp != 0 || minport < IPPORT_RESERVED ||
426 minport > IPPORT_ANONMAX) {
427 syslog(LOG_WARNING,
428 "%s line %d: invalid minport %s",
429 infile, (int)line, min);
430 continue;
431 }
432 maxport = (int)strtol(max, &endp, 10);
433 if (*endp != 0 || maxport < IPPORT_RESERVED ||
434 maxport > IPPORT_ANONMAX) {
435 syslog(LOG_WARNING,
436 "%s line %d: invalid maxport %s",
437 infile, (int)line, max);
438 continue;
439 }
440 if (minport >= maxport) {
441 syslog(LOG_WARNING,
442 "%s line %d: minport %d >= maxport %d",
443 infile, (int)line, minport, maxport);
444 continue;
445 }
446 curclass.portmin = minport;
447 curclass.portmax = maxport;
448
449 } else if (strcasecmp(word, "rateget") == 0) {
450 curclass.maxrateget = 0;
451 curclass.rateget = 0;
452 if (none || EMPTYSTR(arg))
453 continue;
454 llval = strsuftoll(arg);
455 if (llval == -1) {
456 syslog(LOG_WARNING,
457 "%s line %d: invalid rateget %s",
458 infile, (int)line, arg);
459 continue;
460 }
461 curclass.maxrateget = llval;
462 curclass.rateget = llval;
463
464 } else if (strcasecmp(word, "rateput") == 0) {
465 curclass.maxrateput = 0;
466 curclass.rateput = 0;
467 if (none || EMPTYSTR(arg))
468 continue;
469 llval = strsuftoll(arg);
470 if (llval == -1) {
471 syslog(LOG_WARNING,
472 "%s line %d: invalid rateput %s",
473 infile, (int)line, arg);
474 continue;
475 }
476 curclass.maxrateput = llval;
477 curclass.rateput = llval;
478
479 } else if (strcasecmp(word, "sanenames") == 0) {
480 CONF_FLAG(sanenames);
481
482 } else if (strcasecmp(word, "timeout") == 0) {
483 curclass.timeout = DEFAULT_TIMEOUT;
484 if (none || EMPTYSTR(arg))
485 continue;
486 timeout = (unsigned int)strtoul(arg, &endp, 10);
487 if (*endp != 0) {
488 syslog(LOG_WARNING,
489 "%s line %d: invalid timeout %s",
490 infile, (int)line, arg);
491 continue;
492 }
493 if (timeout < 30) {
494 syslog(LOG_WARNING,
495 "%s line %d: timeout %d < 30 seconds",
496 infile, (int)line, timeout);
497 continue;
498 }
499 if (timeout > curclass.maxtimeout) {
500 syslog(LOG_WARNING,
501 "%s line %d: timeout %d > maxtimeout (%d)",
502 infile, (int)line, timeout,
503 curclass.maxtimeout);
504 continue;
505 }
506 curclass.timeout = timeout;
507
508 } else if (strcasecmp(word, "template") == 0) {
509 if (none)
510 continue;
511 REASSIGN(template, EMPTYSTR(arg) ? NULL : xstrdup(arg));
512
513 } else if (strcasecmp(word, "umask") == 0) {
514 mode_t fumask;
515
516 curclass.umask = DEFAULT_UMASK;
517 if (none || EMPTYSTR(arg))
518 continue;
519 fumask = (mode_t)strtoul(arg, &endp, 8);
520 if (*endp != 0 || fumask > 0777) {
521 syslog(LOG_WARNING,
522 "%s line %d: invalid umask %s",
523 infile, (int)line, arg);
524 continue;
525 }
526 curclass.umask = fumask;
527
528 } else if (strcasecmp(word, "upload") == 0) {
529 CONF_FLAG(upload);
530 if (! CURCLASS_FLAGS_ISSET(upload))
531 CURCLASS_FLAGS_CLR(modify);
532
533 } else {
534 syslog(LOG_WARNING,
535 "%s line %d: unknown directive '%s'",
536 infile, (int)line, word);
537 continue;
538 }
539 }
540 REASSIGN(template, NULL);
541 fclose(f);
542 }
543
544 /*
545 * Show file listed in curclass.display first time in, and list all the
546 * files named in curclass.notify in the current directory.
547 * Send back responses with the prefix `code' + "-".
548 * If code == -1, flush the internal cache of directory names and return.
549 */
550 void
551 show_chdir_messages(int code)
552 {
553 static StringList *slist = NULL;
554
555 struct stat st;
556 struct tm *t;
557 glob_t gl;
558 time_t now, then;
559 int age;
560 char curwd[MAXPATHLEN];
561 char *cp, **rlist;
562
563 if (code == -1) {
564 if (slist != NULL)
565 sl_free(slist, 1);
566 slist = NULL;
567 return;
568 }
569
570 if (quietmessages)
571 return;
572
573 /* Setup list for directory cache */
574 if (slist == NULL)
575 slist = sl_init();
576 if (slist == NULL) {
577 syslog(LOG_WARNING, "can't allocate memory for stringlist");
578 return;
579 }
580
581 /* Check if this directory has already been visited */
582 if (getcwd(curwd, sizeof(curwd) - 1) == NULL) {
583 syslog(LOG_WARNING, "can't getcwd: %s", strerror(errno));
584 return;
585 }
586 if (sl_find(slist, curwd) != NULL)
587 return;
588
589 cp = xstrdup(curwd);
590 if (sl_add(slist, cp) == -1)
591 syslog(LOG_WARNING, "can't add `%s' to stringlist", cp);
592
593 /* First check for a display file */
594 (void)display_file(curclass.display, code);
595
596 /* Now see if there are any notify files */
597 if (EMPTYSTR(curclass.notify))
598 return;
599
600 memset(&gl, 0, sizeof(gl));
601 if (glob(curclass.notify, GLOB_LIMIT, NULL, &gl) != 0
602 || gl.gl_matchc == 0) {
603 globfree(&gl);
604 return;
605 }
606 time(&now);
607 for (rlist = gl.gl_pathv; *rlist != NULL; rlist++) {
608 if (stat(*rlist, &st) != 0)
609 continue;
610 if (!S_ISREG(st.st_mode))
611 continue;
612 then = st.st_mtime;
613 if (code != 0) {
614 reply(-code, "%s", "");
615 code = 0;
616 }
617 reply(-code, "Please read the file %s", *rlist);
618 t = localtime(&now);
619 age = 365 * t->tm_year + t->tm_yday;
620 t = localtime(&then);
621 age -= 365 * t->tm_year + t->tm_yday;
622 reply(-code, " it was last modified on %.24s - %d day%s ago",
623 ctime(&then), age, PLURAL(age));
624 }
625 globfree(&gl);
626 }
627
628 int
629 display_file(const char *file, int code)
630 {
631 FILE *f;
632 char *buf, *p;
633 char curwd[MAXPATHLEN];
634 size_t len;
635 off_t lastnum;
636 time_t now;
637
638 lastnum = 0;
639 if (quietmessages)
640 return (0);
641
642 if (EMPTYSTR(file))
643 return(0);
644 if ((f = fopen(file, "r")) == NULL)
645 return (0);
646 reply(-code, "%s", "");
647
648 for (;
649 (buf = fparseln(f, &len, NULL, "\0\0\0", 0)) != NULL; free(buf)) {
650 if (len > 0)
651 if (buf[len - 1] == '\n')
652 buf[--len] = '\0';
653 cprintf(stdout, " ");
654
655 for (p = buf; *p; p++) {
656 if (*p == '%') {
657 p++;
658 switch (*p) {
659
660 case 'c':
661 cprintf(stdout, "%s",
662 curclass.classname ?
663 curclass.classname : "<unknown>");
664 break;
665
666 case 'C':
667 if (getcwd(curwd, sizeof(curwd)-1)
668 == NULL){
669 syslog(LOG_WARNING,
670 "can't getcwd: %s",
671 strerror(errno));
672 continue;
673 }
674 cprintf(stdout, "%s", curwd);
675 break;
676
677 case 'E':
678 if (! EMPTYSTR(emailaddr))
679 cprintf(stdout, "%s",
680 emailaddr);
681 break;
682
683 case 'L':
684 cprintf(stdout, "%s", hostname);
685 break;
686
687 case 'M':
688 if (curclass.limit == -1) {
689 cprintf(stdout, "unlimited");
690 lastnum = 0;
691 } else {
692 cprintf(stdout, "%d",
693 curclass.limit);
694 lastnum = curclass.limit;
695 }
696 break;
697
698 case 'N':
699 cprintf(stdout, "%d", connections);
700 lastnum = connections;
701 break;
702
703 case 'R':
704 cprintf(stdout, "%s", remotehost);
705 break;
706
707 case 's':
708 if (lastnum != 1)
709 cprintf(stdout, "s");
710 break;
711
712 case 'S':
713 if (lastnum != 1)
714 cprintf(stdout, "S");
715 break;
716
717 case 'T':
718 now = time(NULL);
719 cprintf(stdout, "%.24s", ctime(&now));
720 break;
721
722 case 'U':
723 cprintf(stdout, "%s",
724 pw ? pw->pw_name : "<unknown>");
725 break;
726
727 case '%':
728 CPUTC('%', stdout);
729 break;
730
731 }
732 } else
733 CPUTC(*p, stdout);
734 }
735 cprintf(stdout, "\r\n");
736 }
737
738 (void)fflush(stdout);
739 (void)fclose(f);
740 return (1);
741 }
742
743 /*
744 * Parse src, expanding '%' escapes, into dst (which must be at least
745 * MAXPATHLEN long).
746 */
747 void
748 format_path(char *dst, const char *src)
749 {
750 size_t len;
751 const char *p;
752
753 dst[0] = '\0';
754 len = 0;
755 if (src == NULL)
756 return;
757 for (p = src; *p && len < MAXPATHLEN; p++) {
758 if (*p == '%') {
759 p++;
760 switch (*p) {
761
762 case 'c':
763 len += strlcpy(dst + len, curclass.classname,
764 MAXPATHLEN - len);
765 break;
766
767 case 'd':
768 len += strlcpy(dst + len, pw->pw_dir,
769 MAXPATHLEN - len);
770 break;
771
772 case 'u':
773 len += strlcpy(dst + len, pw->pw_name,
774 MAXPATHLEN - len);
775 break;
776
777 case '%':
778 dst[len++] = '%';
779 break;
780
781 }
782 } else
783 dst[len++] = *p;
784 }
785 if (len < MAXPATHLEN)
786 dst[len] = '\0';
787 dst[MAXPATHLEN - 1] = '\0';
788 }
789
790 /*
791 * Find s2 at the end of s1. If found, return a string up to (but
792 * not including) s2, otherwise returns NULL.
793 */
794 static char *
795 strend(const char *s1, char *s2)
796 {
797 static char buf[MAXPATHLEN];
798
799 char *start;
800 size_t l1, l2;
801
802 l1 = strlen(s1);
803 l2 = strlen(s2);
804
805 if (l2 >= l1 || l1 >= sizeof(buf))
806 return(NULL);
807
808 strlcpy(buf, s1, sizeof(buf));
809 start = buf + (l1 - l2);
810
811 if (strcmp(start, s2) == 0) {
812 *start = '\0';
813 return(buf);
814 } else
815 return(NULL);
816 }
817
818 static int
819 filetypematch(char *types, int mode)
820 {
821 for ( ; types[0] != '\0'; types++)
822 switch (*types) {
823 case 'd':
824 if (S_ISDIR(mode))
825 return(1);
826 break;
827 case 'f':
828 if (S_ISREG(mode))
829 return(1);
830 break;
831 }
832 return(0);
833 }
834
835 /*
836 * Look for a conversion. If we succeed, return a pointer to the
837 * command to execute for the conversion.
838 *
839 * The command is stored in a static array so there's no memory
840 * leak problems, and not too much to change in ftpd.c. This
841 * routine doesn't need to be re-entrant unless we start using a
842 * multi-threaded ftpd, and that's not likely for a while...
843 */
844 char **
845 do_conversion(const char *fname)
846 {
847 struct ftpconv *cp;
848 struct stat st;
849 int o_errno;
850 char *base = NULL;
851 char *cmd, *p, *lp, **argv;
852 StringList *sl;
853
854 o_errno = errno;
855 sl = NULL;
856 cmd = NULL;
857 for (cp = curclass.conversions; cp != NULL; cp = cp->next) {
858 if (cp->suffix == NULL) {
859 syslog(LOG_WARNING,
860 "cp->suffix==NULL in conv list; SHOULDN'T HAPPEN!");
861 continue;
862 }
863 if ((base = strend(fname, cp->suffix)) == NULL)
864 continue;
865 if (cp->types == NULL || cp->disable == NULL ||
866 cp->command == NULL)
867 continue;
868 /* Is it enabled? */
869 if (strcmp(cp->disable, ".") != 0 &&
870 stat(cp->disable, &st) == 0)
871 continue;
872 /* Does the base exist? */
873 if (stat(base, &st) < 0)
874 continue;
875 /* Is the file type ok */
876 if (!filetypematch(cp->types, st.st_mode))
877 continue;
878 break; /* "We have a winner!" */
879 }
880
881 /* If we got through the list, no conversion */
882 if (cp == NULL)
883 goto cleanup_do_conv;
884
885 /* Split up command into an argv */
886 if ((sl = sl_init()) == NULL)
887 goto cleanup_do_conv;
888 cmd = xstrdup(cp->command);
889 p = cmd;
890 while (p) {
891 NEXTWORD(p, lp);
892 if (strcmp(lp, "%s") == 0)
893 lp = base;
894 if (sl_add(sl, xstrdup(lp)) == -1)
895 goto cleanup_do_conv;
896 }
897
898 if (sl_add(sl, NULL) == -1)
899 goto cleanup_do_conv;
900 argv = sl->sl_str;
901 free(cmd);
902 free(sl);
903 return(argv);
904
905 cleanup_do_conv:
906 if (sl)
907 sl_free(sl, 1);
908 free(cmd);
909 errno = o_errno;
910 return(NULL);
911 }
912
913 /*
914 * Convert the string `arg' to a long long, which may have an optional SI suffix
915 * (`b', `k', `m', `g', `t'). Returns the number for success, -1 otherwise.
916 */
917 LLT
918 strsuftoll(const char *arg)
919 {
920 char *cp;
921 LLT val;
922
923 if (!isdigit((unsigned char)arg[0]))
924 return (-1);
925
926 val = STRTOLL(arg, &cp, 10);
927 if (cp != NULL) {
928 if (cp[0] != '\0' && cp[1] != '\0')
929 return (-1);
930 switch (tolower((unsigned char)cp[0])) {
931 case '\0':
932 case 'b':
933 break;
934 case 'k':
935 val <<= 10;
936 break;
937 case 'm':
938 val <<= 20;
939 break;
940 case 'g':
941 val <<= 30;
942 break;
943 #ifndef NO_LONG_LONG
944 case 't':
945 val <<= 40;
946 break;
947 #endif
948 default:
949 return (-1);
950 }
951 }
952 if (val < 0)
953 return (-1);
954
955 return (val);
956 }
957
958 /*
959 * Count the number of current connections, reading from
960 * /var/run/ftpd.pids-<class>
961 * Does a kill -0 on each pid in that file, and only counts
962 * processes that exist (or frees the slot if it doesn't).
963 * Adds getpid() to the first free slot. Truncates the file
964 * if possible.
965 */
966 void
967 count_users(void)
968 {
969 char fn[MAXPATHLEN];
970 int fd, i, last;
971 size_t count;
972 pid_t *pids, mypid;
973 struct stat sb;
974
975 (void)strlcpy(fn, _PATH_CLASSPIDS, sizeof(fn));
976 (void)strlcat(fn, curclass.classname, sizeof(fn));
977 pids = NULL;
978 connections = 1;
979
980 if ((fd = open(fn, O_RDWR | O_CREAT, 0600)) == -1)
981 return;
982 if (lockf(fd, F_TLOCK, 0) == -1)
983 goto cleanup_count;
984 if (fstat(fd, &sb) == -1)
985 goto cleanup_count;
986 if ((pids = malloc(sb.st_size + sizeof(pid_t))) == NULL)
987 goto cleanup_count;
988 count = read(fd, pids, sb.st_size);
989 if (count < 0 || count != sb.st_size)
990 goto cleanup_count;
991 count /= sizeof(pid_t);
992 mypid = getpid();
993 last = 0;
994 for (i = 0; i < count; i++) {
995 if (pids[i] == 0)
996 continue;
997 if (kill(pids[i], 0) == -1 && errno != EPERM) {
998 if (mypid != 0) {
999 pids[i] = mypid;
1000 mypid = 0;
1001 last = i;
1002 }
1003 } else {
1004 connections++;
1005 last = i;
1006 }
1007 }
1008 if (mypid != 0) {
1009 if (pids[last] != 0)
1010 last++;
1011 pids[last] = mypid;
1012 }
1013 count = (last + 1) * sizeof(pid_t);
1014 if (lseek(fd, 0, SEEK_SET) == -1)
1015 goto cleanup_count;
1016 if (write(fd, pids, count) == -1)
1017 goto cleanup_count;
1018 (void)ftruncate(fd, count);
1019
1020 cleanup_count:
1021 if (lseek(fd, 0, SEEK_SET) != -1)
1022 (void)lockf(fd, F_ULOCK, 0);
1023 close(fd);
1024 REASSIGN(pids, NULL);
1025 }
1026