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