Home | History | Annotate | Line # | Download | only in ftpd
conf.c revision 1.16
      1 /*	$NetBSD: conf.c,v 1.16 1998/12/28 04:54:00 lukem Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997 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.16 1998/12/28 04:54:00 lukem Exp $");
     42 #endif /* not lint */
     43 
     44 #include <sys/types.h>
     45 #include <sys/param.h>
     46 #include <sys/stat.h>
     47 
     48 #include <errno.h>
     49 #include <glob.h>
     50 #include <stdio.h>
     51 #include <stdlib.h>
     52 #include <time.h>
     53 #include <unistd.h>
     54 #include <string.h>
     55 #include <stringlist.h>
     56 #include <syslog.h>
     57 
     58 #include "extern.h"
     59 #include "pathnames.h"
     60 
     61 static char *strend __P((const char *, char *));
     62 static int filetypematch __P((char *, int));
     63 
     64 struct ftpclass curclass;
     65 
     66 
     67 /*
     68  * Parse the configuration file, looking for the named class, and
     69  * define curclass to contain the appropriate settings.
     70  */
     71 void
     72 parse_conf(findclass)
     73 	char *findclass;
     74 {
     75 	FILE		*f;
     76 	char		*buf, *p;
     77 	size_t		 len;
     78 	int		 none, match;
     79 	char		*endp;
     80 	char		*class, *word, *arg;
     81 	const char	*infile;
     82 	int		 line;
     83 	unsigned int	 timeout;
     84 	struct ftpconv	*conv, *cnext;
     85 
     86 #define REASSIGN(X,Y)	if (X) free(X); (X)=(Y)
     87 #define NEXTWORD(W)	while ((W = strsep(&buf, " \t")) != NULL && *W == '\0')
     88 #define EMPTYSTR(W)	(W == NULL || *W == '\0')
     89 
     90 	REASSIGN(curclass.classname, findclass);
     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 	curclass.checkportcmd = 0;
    100 	curclass.conversions =	NULL;
    101 	REASSIGN(curclass.display, NULL);
    102 	curclass.maxtimeout =	7200;		/* 2 hours */
    103 	curclass.modify =	1;
    104 	REASSIGN(curclass.notify, NULL);
    105 	curclass.passive =	1;
    106 	curclass.timeout =	900;		/* 15 minutes */
    107 	curclass.umask =	027;
    108 
    109 	if (strcasecmp(findclass, "guest") == 0) {
    110 		curclass.modify = 0;
    111 		curclass.umask = 0707;
    112 	}
    113 
    114 	infile = conffilename(_PATH_FTPDCONF);
    115 	if ((f = fopen(infile, "r")) == NULL)
    116 		return;
    117 
    118 	line = 0;
    119 	while ((buf = fgetln(f, &len)) != NULL) {
    120 		none = match = 0;
    121 		line++;
    122 		if (len < 1)
    123 			continue;
    124 		if (buf[len - 1] != '\n') {
    125 			syslog(LOG_WARNING,
    126 			    "%s line %d is partially truncated?", infile, line);
    127 			continue;
    128 		}
    129 		buf[--len] = '\0';
    130 		if ((p = strchr(buf, '#')) != NULL)
    131 			*p = '\0';
    132 		if (EMPTYSTR(buf))
    133 			continue;
    134 
    135 		NEXTWORD(word);
    136 		NEXTWORD(class);
    137 		NEXTWORD(arg);
    138 		if (EMPTYSTR(word) || EMPTYSTR(class))
    139 			continue;
    140 		if (strcasecmp(class, "none") == 0)
    141 			none = 1;
    142 		if (strcasecmp(class, findclass) != 0 &&
    143 		    !none && strcasecmp(class, "all") != 0)
    144 			continue;
    145 
    146 		if (strcasecmp(word, "checkportcmd") == 0) {
    147 			if (none ||
    148 			    (!EMPTYSTR(arg) && strcasecmp(arg, "off") == 0))
    149 				curclass.checkportcmd = 0;
    150 			else
    151 				curclass.checkportcmd = 1;
    152 		} else if (strcasecmp(word, "conversion") == 0) {
    153 			char *suffix, *types, *disable, *convcmd;
    154 
    155 			if (EMPTYSTR(arg)) {
    156 				syslog(LOG_WARNING,
    157 				    "%s line %d: %s requires a suffix",
    158 				    infile, line, word);
    159 				continue;	/* need a suffix */
    160 			}
    161 			NEXTWORD(types);
    162 			NEXTWORD(disable);
    163 			convcmd = buf;
    164 			if (convcmd)
    165 				convcmd += strspn(convcmd, " \t");
    166 			suffix = strdup(arg);
    167 			if (suffix == NULL) {
    168 				syslog(LOG_WARNING, "can't strdup");
    169 				continue;
    170 			}
    171 			if (none || EMPTYSTR(types) ||
    172 			    EMPTYSTR(disable) || EMPTYSTR(convcmd)) {
    173 				types = NULL;
    174 				disable = NULL;
    175 				convcmd = NULL;
    176 			} else {
    177 				types = strdup(types);
    178 				disable = strdup(disable);
    179 				convcmd = strdup(convcmd);
    180 				if (types == NULL || disable == NULL ||
    181 				    convcmd == NULL) {
    182 					syslog(LOG_WARNING, "can't strdup");
    183 					if (types)
    184 						free(types);
    185 					if (disable)
    186 						free(disable);
    187 					if (convcmd)
    188 						free(convcmd);
    189 					continue;
    190 				}
    191 			}
    192 			for (conv = curclass.conversions; conv != NULL;
    193 			    conv = conv->next) {
    194 				if (strcmp(conv->suffix, suffix) == 0)
    195 					break;
    196 			}
    197 			if (conv == NULL) {
    198 				conv = (struct ftpconv *)
    199 				    calloc(1, sizeof(struct ftpconv));
    200 				if (conv == NULL) {
    201 					syslog(LOG_WARNING, "can't malloc");
    202 					continue;
    203 				}
    204 				conv->next = curclass.conversions;
    205 				curclass.conversions = conv;
    206 			}
    207 			REASSIGN(conv->suffix, suffix);
    208 			REASSIGN(conv->types, types);
    209 			REASSIGN(conv->disable, disable);
    210 			REASSIGN(conv->command, convcmd);
    211 		} else if (strcasecmp(word, "display") == 0) {
    212 			if (none || EMPTYSTR(arg))
    213 				arg = NULL;
    214 			else
    215 				arg = strdup(arg);
    216 			REASSIGN(curclass.display, arg);
    217 		} else if (strcasecmp(word, "maxtimeout") == 0) {
    218 			if (none || EMPTYSTR(arg))
    219 				continue;
    220 			timeout = (unsigned int)strtoul(arg, &endp, 10);
    221 			if (*endp != 0) {
    222 				syslog(LOG_WARNING,
    223 				    "%s line %d: invalid maxtimeout %s",
    224 				    infile, line, arg);
    225 				continue;
    226 			}
    227 			if (timeout < 30) {
    228 				syslog(LOG_WARNING,
    229 				    "%s line %d: maxtimeout %d < 30 seconds",
    230 				    infile, line, timeout);
    231 				continue;
    232 			}
    233 			if (timeout < curclass.timeout) {
    234 				syslog(LOG_WARNING,
    235 				    "%s line %d: maxtimeout %d < timeout (%d)",
    236 				    infile, line, timeout, curclass.timeout);
    237 				continue;
    238 			}
    239 			curclass.maxtimeout = timeout;
    240 		} else if (strcasecmp(word, "modify") == 0) {
    241 			if (none ||
    242 			    (!EMPTYSTR(arg) && strcasecmp(arg, "off") == 0))
    243 				curclass.modify = 0;
    244 			else
    245 				curclass.modify = 1;
    246 		} else if (strcasecmp(word, "notify") == 0) {
    247 			if (none || EMPTYSTR(arg))
    248 				arg = NULL;
    249 			else
    250 				arg = strdup(arg);
    251 			REASSIGN(curclass.notify, arg);
    252 		} else if (strcasecmp(word, "passive") == 0) {
    253 			if (none ||
    254 			    (!EMPTYSTR(arg) && strcasecmp(arg, "off") == 0))
    255 				curclass.passive = 0;
    256 			else
    257 				curclass.passive = 1;
    258 		} else if (strcasecmp(word, "timeout") == 0) {
    259 			if (none || EMPTYSTR(arg))
    260 				continue;
    261 			timeout = (unsigned int)strtoul(arg, &endp, 10);
    262 			if (*endp != 0) {
    263 				syslog(LOG_WARNING,
    264 				    "%s line %d: invalid timeout %s",
    265 				    infile, line, arg);
    266 				continue;
    267 			}
    268 			if (timeout < 30) {
    269 				syslog(LOG_WARNING,
    270 				    "%s line %d: timeout %d < 30 seconds",
    271 				    infile, line, timeout);
    272 				continue;
    273 			}
    274 			if (timeout > curclass.maxtimeout) {
    275 				syslog(LOG_WARNING,
    276 				    "%s line %d: timeout %d > maxtimeout (%d)",
    277 				    infile, line, timeout, curclass.maxtimeout);
    278 				continue;
    279 			}
    280 			curclass.timeout = timeout;
    281 		} else if (strcasecmp(word, "umask") == 0) {
    282 			mode_t umask;
    283 
    284 			if (none || EMPTYSTR(arg))
    285 				continue;
    286 			umask = (mode_t)strtoul(arg, &endp, 8);
    287 			if (*endp != 0 || umask > 0777) {
    288 				syslog(LOG_WARNING,
    289 				    "%s line %d: invalid umask %s",
    290 				    infile, line, arg);
    291 				continue;
    292 			}
    293 			curclass.umask = umask;
    294 		} else {
    295 			syslog(LOG_WARNING,
    296 			    "%s line %d: unknown directive '%s'",
    297 			    infile, line, word);
    298 			continue;
    299 		}
    300 	}
    301 #undef REASSIGN
    302 #undef NEXTWORD
    303 #undef EMPTYSTR
    304 	fclose(f);
    305 }
    306 
    307 /*
    308  * Show file listed in curclass.display first time in, and list all the
    309  * files named in curclass.notify in the current directory.  Send back
    310  * responses with the "reply" prefix.
    311  */
    312 void
    313 show_chdir_messages(code)
    314 	int	code;
    315 {
    316 	static StringList *slist = NULL;
    317 
    318 	struct stat st;
    319 	struct tm *t;
    320 	glob_t	 gl;
    321 	time_t	 now, then;
    322 	int	 age;
    323 	char	 cwd[MAXPATHLEN + 1];
    324 	char	 line[BUFSIZ];
    325 	char	*cp, **rlist;
    326 	FILE	*f;
    327 
    328 		/* Setup list for directory cache */
    329 	if (slist == NULL)
    330 		slist = sl_init();
    331 
    332 		/* Check if this directory has already been visited */
    333 	if (getcwd(cwd, sizeof(cwd) - 1) == NULL) {
    334 		syslog(LOG_WARNING, "can't getcwd: %s", strerror(errno));
    335 		return;
    336 	}
    337 	if (sl_find(slist, cwd) != NULL)
    338 		return;
    339 
    340 	cp = strdup(cwd);
    341 	if (cp == NULL) {
    342 		syslog(LOG_WARNING, "can't strdup");
    343 		return;
    344 	}
    345 	sl_add(slist, cp);
    346 
    347 		/* First check for a display file */
    348 	if (curclass.display != NULL && curclass.display[0] &&
    349 	    (f = fopen(curclass.display, "r")) != NULL) {
    350 		while (fgets(line, BUFSIZ, f)) {
    351 			if ((cp = strchr(line, '\n')) != NULL)
    352 				*cp = '\0';
    353 			lreply(code, "%s", line);
    354 		}
    355 		fclose(f);
    356 		lreply(code, "");
    357 	}
    358 
    359 		/* Now see if there are any notify files */
    360 	if (curclass.notify == NULL || curclass.notify[0] == '\0')
    361 		return;
    362 
    363 	if (glob(curclass.notify, 0, NULL, &gl) != 0 || gl.gl_matchc == 0)
    364 		return;
    365 	time(&now);
    366 	for (rlist = gl.gl_pathv; *rlist != NULL; rlist++) {
    367 		if (stat(*rlist, &st) != 0)
    368 			continue;
    369 		if (!S_ISREG(st.st_mode))
    370 			continue;
    371 		then = st.st_mtime;
    372 		lreply(code, "Please read the file %s", *rlist);
    373 		t = localtime(&now);
    374 		age = 365 * t->tm_year + t->tm_yday;
    375 		t = localtime(&then);
    376 		age -= 365 * t->tm_year + t->tm_yday;
    377 		lreply(code, "  it was last modified on %.24s - %d day%s ago",
    378 		    ctime(&then), age, age == 1 ? "" : "s");
    379 	}
    380 	globfree(&gl);
    381 }
    382 
    383 /*
    384  * Find s2 at the end of s1.  If found, return a string up and up (but
    385  * not including) s2, otherwise returns NULL.
    386  */
    387 static char *
    388 strend(s1, s2)
    389 	const char *s1;
    390 	char *s2;
    391 {
    392 	static	char buf[MAXPATHLEN + 1];
    393 
    394 	char	*start;
    395 	size_t	l1, l2;
    396 
    397 	l1 = strlen(s1);
    398 	l2 = strlen(s2);
    399 
    400 	if (l2 >= l1)
    401 		return(NULL);
    402 
    403 	strncpy(buf, s1, MAXPATHLEN);
    404 	start = buf + (l1 - l2);
    405 
    406 	if (strcmp(start, s2) == 0) {
    407 		*start = '\0';
    408 		return(buf);
    409 	} else
    410 		return(NULL);
    411 }
    412 
    413 static int
    414 filetypematch(types, mode)
    415 	char	*types;
    416 	int	mode;
    417 {
    418 	for ( ; types[0] != '\0'; types++)
    419 		switch (*types) {
    420 		  case 'd':
    421 			if (S_ISDIR(mode))
    422 				return(1);
    423 			break;
    424 		  case 'f':
    425 			if (S_ISREG(mode))
    426 				return(1);
    427 			break;
    428 		}
    429 	return(0);
    430 }
    431 
    432 /*
    433  * Look for a conversion.  If we succeed, return a pointer to the
    434  * command to execute for the conversion.
    435  *
    436  * The command is stored in a static array so there's no memory
    437  * leak problems, and not too much to change in ftpd.c.  This
    438  * routine doesn't need to be re-entrant unless we start using a
    439  * multi-threaded ftpd, and that's not likely for a while...
    440  */
    441 char *
    442 do_conversion(fname)
    443 	const char *fname;
    444 {
    445 	static char	 cmd[LINE_MAX];
    446 
    447 	struct ftpconv	*cp;
    448 	struct stat	 st;
    449 	int		 o_errno;
    450 	char		*base = NULL;
    451 
    452 	o_errno = errno;
    453 	for (cp = curclass.conversions; cp != NULL; cp = cp->next) {
    454 		if (cp->suffix == NULL) {
    455 			syslog(LOG_WARNING,
    456 			    "cp->suffix==NULL in conv list; SHOULDN'T HAPPEN!");
    457 			continue;
    458 		}
    459 		if ((base = strend(fname, cp->suffix)) == NULL)
    460 			continue;
    461 		if (cp->types == NULL || cp->disable == NULL ||
    462 		    cp->command == NULL)
    463 			continue;
    464 					/* Is it enabled? */
    465 		if (strcmp(cp->disable, ".") != 0 &&
    466 		    stat(cp->disable, &st) == 0)
    467 				continue;
    468 					/* Does the base exist? */
    469 		if (stat(base, &st) < 0)
    470 			continue;
    471 					/* Is the file type ok */
    472 		if (!filetypematch(cp->types, st.st_mode))
    473 			continue;
    474 		break;			/* "We have a winner!" */
    475 	}
    476 
    477 	/* If we got through the list, no conversion */
    478 	if (cp == NULL) {
    479 		errno = o_errno;
    480 		return(NULL);
    481 	}
    482 
    483 	snprintf(cmd, LINE_MAX, cp->command, base);
    484 	syslog(LOG_DEBUG, "get command: %s", cmd);
    485 	return(cmd);
    486 }
    487