1 /* $NetBSD: conf.c,v 1.46 2001/12/04 13:54:12 lukem Exp $ */
4 * Copyright (c) 1997-2001 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Simon Burge and Luke Mewburn.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
39 #include "lukemftpd.h"
42 #include "pathnames.h"
44 static char *strend(const char *, char *);
45 static int filetypematch(char *, int);
49 #define DEFAULT_LIMIT -1 /* unlimited connections */
50 #define DEFAULT_MAXFILESIZE -1 /* unlimited file size */
51 #define DEFAULT_MAXTIMEOUT 7200 /* 2 hours */
52 #define DEFAULT_TIMEOUT 900 /* 15 minutes */
53 #define DEFAULT_UMASK 027 /* 15 minutes */
56 * Initialise curclass to an `empty' state
61 struct ftpconv *conv, *cnext;
63 for (conv = curclass.conversions; conv != NULL; conv = cnext) {
64 REASSIGN(conv->suffix, NULL);
65 REASSIGN(conv->types, NULL);
66 REASSIGN(conv->disable, NULL);
67 REASSIGN(conv->command, NULL);
72 memset((char *)&curclass.advertise, 0, sizeof(curclass.advertise));
73 curclass.advertise.su_len = 0; /* `not used' */
74 REASSIGN(curclass.chroot, NULL);
75 REASSIGN(curclass.classname, NULL);
76 curclass.conversions = NULL;
77 REASSIGN(curclass.display, NULL);
78 REASSIGN(curclass.homedir, NULL);
79 curclass.limit = DEFAULT_LIMIT;
80 REASSIGN(curclass.limitfile, NULL);
81 curclass.maxfilesize = DEFAULT_MAXFILESIZE;
82 curclass.maxrateget = 0;
83 curclass.maxrateput = 0;
84 curclass.maxtimeout = DEFAULT_MAXTIMEOUT;
85 REASSIGN(curclass.motd, xstrdup(_PATH_FTPLOGINMESG));
86 REASSIGN(curclass.notify, NULL);
91 curclass.timeout = DEFAULT_TIMEOUT;
92 /* curclass.type is set elsewhere */
93 curclass.umask = DEFAULT_UMASK;
95 CURCLASS_FLAGS_SET(checkportcmd);
96 CURCLASS_FLAGS_CLR(denyquick);
97 CURCLASS_FLAGS_SET(modify);
98 CURCLASS_FLAGS_SET(passive);
99 CURCLASS_FLAGS_CLR(private);
100 CURCLASS_FLAGS_CLR(sanenames);
101 CURCLASS_FLAGS_SET(upload);
105 * Parse the configuration file, looking for the named class, and
106 * define curclass to contain the appropriate settings.
109 parse_conf(const char *findclass)
117 char *class, *word, *arg, *template;
120 unsigned int timeout;
121 struct ftpconv *conv, *cnext;
124 REASSIGN(curclass.classname, xstrdup(findclass));
125 /* set more guest defaults */
126 if (strcasecmp(findclass, "guest") == 0) {
127 CURCLASS_FLAGS_CLR(modify);
128 curclass.umask = 0707;
131 infile = conffilename(_PATH_FTPDCONF);
132 if ((f = fopen(infile, "r")) == NULL)
138 (buf = fparseln(f, &len, &line, NULL, FPARSELN_UNESCCOMM |
139 FPARSELN_UNESCCONT | FPARSELN_UNESCESC)) != NULL;
145 if (p[len - 1] == '\n')
153 if (EMPTYSTR(word) || EMPTYSTR(class))
155 if (strcasecmp(class, "none") == 0)
157 if (! (strcasecmp(class, findclass) == 0 ||
158 (template != NULL && strcasecmp(class, template) == 0) ||
160 strcasecmp(class, "all") == 0) )
163 #define CONF_FLAG(x) \
166 (!EMPTYSTR(arg) && strcasecmp(arg, "off") == 0)) \
167 CURCLASS_FLAGS_CLR(x); \
169 CURCLASS_FLAGS_SET(x); \
172 #define CONF_STRING(x) \
174 if (none || EMPTYSTR(arg)) \
177 arg = xstrdup(arg); \
178 REASSIGN(curclass.x, arg); \
185 } else if ((strcasecmp(word, "advertise") == 0)
186 || (strcasecmp(word, "advertize") == 0)) {
187 struct addrinfo hints, *res;
190 memset((char *)&curclass.advertise, 0,
191 sizeof(curclass.advertise));
192 curclass.advertise.su_len = 0;
193 if (none || EMPTYSTR(arg))
196 memset(&hints, 0, sizeof(hints));
198 * only get addresses of the family
199 * that we're listening on
201 hints.ai_family = ctrl_addr.su_family;
202 hints.ai_socktype = SOCK_STREAM;
203 error = getaddrinfo(arg, "0", &hints, &res);
205 syslog(LOG_WARNING, "%s line %d: %s",
206 infile, (int)line, gai_strerror(error));
214 "%s line %d: multiple addresses returned for `%s'; please be more specific",
215 infile, (int)line, arg);
216 goto advertiseparsefail;
218 if (sizeof(curclass.advertise) < res->ai_addrlen || (
220 res->ai_family != AF_INET6 &&
222 res->ai_family != AF_INET)) {
224 "%s line %d: unsupported protocol %d for `%s'",
225 infile, (int)line, res->ai_family, arg);
226 goto advertiseparsefail;
228 memcpy(&curclass.advertise, res->ai_addr,
230 curclass.advertise.su_len = res->ai_addrlen;
233 } else if (strcasecmp(word, "checkportcmd") == 0) {
234 CONF_FLAG(checkportcmd);
236 } else if (strcasecmp(word, "chroot") == 0) {
239 } else if (strcasecmp(word, "classtype") == 0) {
240 if (!none && !EMPTYSTR(arg)) {
241 if (strcasecmp(arg, "GUEST") == 0)
242 curclass.type = CLASS_GUEST;
243 else if (strcasecmp(arg, "CHROOT") == 0)
244 curclass.type = CLASS_CHROOT;
245 else if (strcasecmp(arg, "REAL") == 0)
246 curclass.type = CLASS_REAL;
249 "%s line %d: unknown class type `%s'",
250 infile, (int)line, arg);
255 } else if (strcasecmp(word, "conversion") == 0) {
256 char *suffix, *types, *disable, *convcmd;
260 "%s line %d: %s requires a suffix",
261 infile, (int)line, word);
262 continue; /* need a suffix */
265 NEXTWORD(p, disable);
268 convcmd += strspn(convcmd, " \t");
269 suffix = xstrdup(arg);
270 if (none || EMPTYSTR(types) ||
271 EMPTYSTR(disable) || EMPTYSTR(convcmd)) {
276 types = xstrdup(types);
277 disable = xstrdup(disable);
278 convcmd = xstrdup(convcmd);
280 for (conv = curclass.conversions; conv != NULL;
282 if (strcmp(conv->suffix, suffix) == 0)
286 conv = (struct ftpconv *)
287 calloc(1, sizeof(struct ftpconv));
289 syslog(LOG_WARNING, "can't malloc");
293 for (cnext = curclass.conversions;
294 cnext != NULL; cnext = cnext->next)
295 if (cnext->next == NULL)
300 curclass.conversions = conv;
302 REASSIGN(conv->suffix, suffix);
303 REASSIGN(conv->types, types);
304 REASSIGN(conv->disable, disable);
305 REASSIGN(conv->command, convcmd);
307 } else if (strcasecmp(word, "denyquick") == 0) {
308 CONF_FLAG(denyquick);
310 } else if (strcasecmp(word, "display") == 0) {
311 CONF_STRING(display);
313 } else if (strcasecmp(word, "homedir") == 0) {
314 CONF_STRING(homedir);
316 } else if (strcasecmp(word, "limit") == 0) {
319 curclass.limit = DEFAULT_LIMIT;
320 REASSIGN(curclass.limitfile, NULL);
321 if (none || EMPTYSTR(arg))
323 limit = (int)strtol(arg, &endp, 10);
326 "%s line %d: invalid limit %s",
327 infile, (int)line, arg);
330 curclass.limit = limit;
331 REASSIGN(curclass.limitfile,
332 EMPTYSTR(p) ? NULL : xstrdup(p));
334 } else if (strcasecmp(word, "maxfilesize") == 0) {
335 curclass.maxfilesize = DEFAULT_MAXFILESIZE;
336 if (none || EMPTYSTR(arg))
338 llval = strsuftoll(arg);
341 "%s line %d: invalid maxfilesize %s",
342 infile, (int)line, arg);
345 curclass.maxfilesize = llval;
347 } else if (strcasecmp(word, "maxtimeout") == 0) {
348 curclass.maxtimeout = DEFAULT_MAXTIMEOUT;
349 if (none || EMPTYSTR(arg))
351 timeout = (unsigned int)strtoul(arg, &endp, 10);
354 "%s line %d: invalid maxtimeout %s",
355 infile, (int)line, arg);
360 "%s line %d: maxtimeout %d < 30 seconds",
361 infile, (int)line, timeout);
364 if (timeout < curclass.timeout) {
366 "%s line %d: maxtimeout %d < timeout (%d)",
367 infile, (int)line, timeout,
371 curclass.maxtimeout = timeout;
373 } else if (strcasecmp(word, "modify") == 0) {
376 } else if (strcasecmp(word, "motd") == 0) {
379 } else if (strcasecmp(word, "notify") == 0) {
382 } else if (strcasecmp(word, "passive") == 0) {
385 } else if (strcasecmp(word, "portrange") == 0) {
386 int minport, maxport;
389 curclass.portmin = 0;
390 curclass.portmax = 0;
391 if (none || EMPTYSTR(arg))
397 "%s line %d: missing maxport argument",
401 minport = (int)strtol(min, &endp, 10);
402 if (*endp != 0 || minport < IPPORT_RESERVED ||
403 minport > IPPORT_ANONMAX) {
405 "%s line %d: invalid minport %s",
406 infile, (int)line, min);
409 maxport = (int)strtol(max, &endp, 10);
410 if (*endp != 0 || maxport < IPPORT_RESERVED ||
411 maxport > IPPORT_ANONMAX) {
413 "%s line %d: invalid maxport %s",
414 infile, (int)line, max);
417 if (minport >= maxport) {
419 "%s line %d: minport %d >= maxport %d",
420 infile, (int)line, minport, maxport);
423 curclass.portmin = minport;
424 curclass.portmax = maxport;
426 } else if (strcasecmp(word, "private") == 0) {
429 } else if (strcasecmp(word, "rateget") == 0) {
430 curclass.maxrateget = 0;
431 curclass.rateget = 0;
432 if (none || EMPTYSTR(arg))
434 llval = strsuftoll(arg);
437 "%s line %d: invalid rateget %s",
438 infile, (int)line, arg);
441 curclass.maxrateget = llval;
442 curclass.rateget = llval;
444 } else if (strcasecmp(word, "rateput") == 0) {
445 curclass.maxrateput = 0;
446 curclass.rateput = 0;
447 if (none || EMPTYSTR(arg))
449 llval = strsuftoll(arg);
452 "%s line %d: invalid rateput %s",
453 infile, (int)line, arg);
456 curclass.maxrateput = llval;
457 curclass.rateput = llval;
459 } else if (strcasecmp(word, "sanenames") == 0) {
460 CONF_FLAG(sanenames);
462 } else if (strcasecmp(word, "timeout") == 0) {
463 curclass.timeout = DEFAULT_TIMEOUT;
464 if (none || EMPTYSTR(arg))
466 timeout = (unsigned int)strtoul(arg, &endp, 10);
469 "%s line %d: invalid timeout %s",
470 infile, (int)line, arg);
475 "%s line %d: timeout %d < 30 seconds",
476 infile, (int)line, timeout);
479 if (timeout > curclass.maxtimeout) {
481 "%s line %d: timeout %d > maxtimeout (%d)",
482 infile, (int)line, timeout,
483 curclass.maxtimeout);
486 curclass.timeout = timeout;
488 } else if (strcasecmp(word, "template") == 0) {
491 REASSIGN(template, EMPTYSTR(arg) ? NULL : xstrdup(arg));
493 } else if (strcasecmp(word, "umask") == 0) {
496 curclass.umask = DEFAULT_UMASK;
497 if (none || EMPTYSTR(arg))
499 fumask = (mode_t)strtoul(arg, &endp, 8);
500 if (*endp != 0 || fumask > 0777) {
502 "%s line %d: invalid umask %s",
503 infile, (int)line, arg);
506 curclass.umask = fumask;
508 } else if (strcasecmp(word, "upload") == 0) {
510 if (! CURCLASS_FLAGS_ISSET(upload))
511 CURCLASS_FLAGS_CLR(modify);
515 "%s line %d: unknown directive '%s'",
516 infile, (int)line, word);
520 REASSIGN(template, NULL);
525 * Show file listed in curclass.display first time in, and list all the
526 * files named in curclass.notify in the current directory.
527 * Send back responses with the prefix `code' + "-".
528 * If code == -1, flush the internal cache of directory names and return.
531 show_chdir_messages(int code)
533 static StringList *slist = NULL;
540 char curwd[MAXPATHLEN];
553 /* Setup list for directory cache */
557 syslog(LOG_WARNING, "can't allocate memory for stringlist");
561 /* Check if this directory has already been visited */
562 if (getcwd(curwd, sizeof(curwd) - 1) == NULL) {
563 syslog(LOG_WARNING, "can't getcwd: %s", strerror(errno));
566 if (sl_find(slist, curwd) != NULL)
570 if (sl_add(slist, cp) == -1)
571 syslog(LOG_WARNING, "can't add `%s' to stringlist", cp);
573 /* First check for a display file */
574 (void)display_file(curclass.display, code);
576 /* Now see if there are any notify files */
577 if (EMPTYSTR(curclass.notify))
580 memset(&gl, 0, sizeof(gl));
581 if (glob(curclass.notify, GLOB_LIMIT, NULL, &gl) != 0
582 || gl.gl_matchc == 0) {
587 for (rlist = gl.gl_pathv; *rlist != NULL; rlist++) {
588 if (stat(*rlist, &st) != 0)
590 if (!S_ISREG(st.st_mode))
594 reply(-code, "%s", "");
597 reply(-code, "Please read the file %s", *rlist);
599 age = 365 * t->tm_year + t->tm_yday;
600 t = localtime(&then);
601 age -= 365 * t->tm_year + t->tm_yday;
602 reply(-code, " it was last modified on %.24s - %d day%s ago",
603 ctime(&then), age, PLURAL(age));
609 display_file(const char *file, int code)
613 char curwd[MAXPATHLEN];
624 if ((f = fopen(file, "r")) == NULL)
626 reply(-code, "%s", "");
629 (buf = fparseln(f, &len, NULL, "\0\0\0", 0)) != NULL; free(buf)) {
631 if (buf[len - 1] == '\n')
633 cprintf(stdout, " ");
635 for (p = buf; *p; p++) {
641 cprintf(stdout, "%s",
643 curclass.classname : "<unknown>");
647 if (getcwd(curwd, sizeof(curwd)-1)
654 cprintf(stdout, "%s", curwd);
658 if (! EMPTYSTR(emailaddr))
659 cprintf(stdout, "%s",
664 cprintf(stdout, "%s", hostname);
668 if (curclass.limit == -1) {
669 cprintf(stdout, "unlimited");
672 cprintf(stdout, "%d",
674 lastnum = curclass.limit;
679 cprintf(stdout, "%d", connections);
680 lastnum = connections;
684 cprintf(stdout, "%s", remotehost);
689 cprintf(stdout, "s");
694 cprintf(stdout, "S");
699 cprintf(stdout, "%.24s", ctime(&now));
703 cprintf(stdout, "%s",
704 pw ? pw->pw_name : "<unknown>");
715 cprintf(stdout, "\r\n");
718 (void)fflush(stdout);
724 * Parse src, expanding '%' escapes, into dst (which must be at least
728 format_path(char *dst, const char *src)
737 for (p = src; *p && len < MAXPATHLEN; p++) {
743 len += strlcpy(dst + len, curclass.classname,
748 len += strlcpy(dst + len, pw->pw_dir,
753 len += strlcpy(dst + len, pw->pw_name,
765 if (len < MAXPATHLEN)
767 dst[MAXPATHLEN - 1] = '\0';
771 * Find s2 at the end of s1. If found, return a string up to (but
772 * not including) s2, otherwise returns NULL.
775 strend(const char *s1, char *s2)
777 static char buf[MAXPATHLEN];
785 if (l2 >= l1 || l1 >= sizeof(buf))
788 strlcpy(buf, s1, sizeof(buf));
789 start = buf + (l1 - l2);
791 if (strcmp(start, s2) == 0) {
799 filetypematch(char *types, int mode)
801 for ( ; types[0] != '\0'; types++)
816 * Look for a conversion. If we succeed, return a pointer to the
817 * command to execute for the conversion.
819 * The command is stored in a static array so there's no memory
820 * leak problems, and not too much to change in ftpd.c. This
821 * routine doesn't need to be re-entrant unless we start using a
822 * multi-threaded ftpd, and that's not likely for a while...
825 do_conversion(const char *fname)
831 char *cmd, *p, *lp, **argv;
837 for (cp = curclass.conversions; cp != NULL; cp = cp->next) {
838 if (cp->suffix == NULL) {
840 "cp->suffix==NULL in conv list; SHOULDN'T HAPPEN!");
843 if ((base = strend(fname, cp->suffix)) == NULL)
845 if (cp->types == NULL || cp->disable == NULL ||
849 if (strcmp(cp->disable, ".") != 0 &&
850 stat(cp->disable, &st) == 0)
852 /* Does the base exist? */
853 if (stat(base, &st) < 0)
855 /* Is the file type ok */
856 if (!filetypematch(cp->types, st.st_mode))
858 break; /* "We have a winner!" */
861 /* If we got through the list, no conversion */
863 goto cleanup_do_conv;
865 /* Split up command into an argv */
866 if ((sl = sl_init()) == NULL)
867 goto cleanup_do_conv;
868 cmd = xstrdup(cp->command);
872 if (strcmp(lp, "%s") == 0)
874 if (sl_add(sl, xstrdup(lp)) == -1)
875 goto cleanup_do_conv;
878 if (sl_add(sl, NULL) == -1)
879 goto cleanup_do_conv;
894 * Convert the string `arg' to a long long, which may have an optional SI suffix
895 * (`b', `k', `m', `g', `t'). Returns the number for success, -1 otherwise.
898 strsuftoll(const char *arg)
903 if (!isdigit((unsigned char)arg[0]))
906 val = STRTOLL(arg, &cp, 10);
908 if (cp[0] != '\0' && cp[1] != '\0')
910 switch (tolower((unsigned char)cp[0])) {
939 * Count the number of current connections, reading from
940 * /var/run/ftpd.pids-<class>
941 * Does a kill -0 on each pid in that file, and only counts
942 * processes that exist (or frees the slot if it doesn't).
943 * Adds getpid() to the first free slot. Truncates the file
955 (void)strlcpy(fn, _PATH_CLASSPIDS, sizeof(fn));
956 (void)strlcat(fn, curclass.classname, sizeof(fn));
960 if ((fd = open(fn, O_RDWR | O_CREAT, 0600)) == -1)
963 if (lockf(fd, F_TLOCK, 0) == -1)
966 if (flock(fd, LOCK_EX | LOCK_NB) != 0)
969 /* XXX: use fcntl ? */
971 if (fstat(fd, &sb) == -1)
973 if ((pids = malloc(sb.st_size + sizeof(pid_t))) == NULL)
975 count = read(fd, pids, sb.st_size);
976 if (count < 0 || count != sb.st_size)
978 count /= sizeof(pid_t);
981 for (i = 0; i < count; i++) {
984 if (kill(pids[i], 0) == -1 && errno != EPERM) {
1000 count = (last + 1) * sizeof(pid_t);
1001 if (lseek(fd, 0, SEEK_SET) == -1)
1003 if (write(fd, pids, count) == -1)
1005 (void)ftruncate(fd, count);
1009 if (lseek(fd, 0, SEEK_SET) != -1)
1010 (void)lockf(fd, F_ULOCK, 0);
1012 (void)flock(fd, LOCK_UN);
1014 /* XXX: use fcntl ? */
1017 REASSIGN(pids, NULL);