Home | History | Annotate | Line # | Download | only in ftpd
conf.c revision 1.41
      1 /*	$NetBSD: conf.c,v 1.41 2001/04/25 01:46:25 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.41 2001/04/25 01:46:25 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 	gl.gl_offs = 0;
    600 	if (glob(curclass.notify, GLOB_LIMIT, NULL, &gl) != 0
    601 	    || gl.gl_matchc == 0) {
    602 		globfree(&gl);
    603 		return;
    604 	}
    605 	time(&now);
    606 	for (rlist = gl.gl_pathv; *rlist != NULL; rlist++) {
    607 		if (stat(*rlist, &st) != 0)
    608 			continue;
    609 		if (!S_ISREG(st.st_mode))
    610 			continue;
    611 		then = st.st_mtime;
    612 		if (code != 0) {
    613 			reply(-code, "%s", "");
    614 			code = 0;
    615 		}
    616 		reply(-code, "Please read the file %s", *rlist);
    617 		t = localtime(&now);
    618 		age = 365 * t->tm_year + t->tm_yday;
    619 		t = localtime(&then);
    620 		age -= 365 * t->tm_year + t->tm_yday;
    621 		reply(-code, "  it was last modified on %.24s - %d day%s ago",
    622 		    ctime(&then), age, PLURAL(age));
    623 	}
    624 	globfree(&gl);
    625 }
    626 
    627 int
    628 display_file(const char *file, int code)
    629 {
    630 	FILE   *f;
    631 	char   *buf, *p, *cwd;
    632 	size_t	len;
    633 	off_t	lastnum;
    634 	time_t	now;
    635 
    636 	lastnum = 0;
    637 	if (quietmessages)
    638 		return (0);
    639 
    640 	if (EMPTYSTR(file))
    641 		return(0);
    642 	if ((f = fopen(file, "r")) == NULL)
    643 		return (0);
    644 	reply(-code, "%s", "");
    645 
    646 	for (;
    647 	    (buf = fparseln(f, &len, NULL, "\0\0\0", 0)) != NULL; free(buf)) {
    648 		if (len > 0)
    649 			if (buf[len - 1] == '\n')
    650 				buf[--len] = '\0';
    651 		cprintf(stdout, "    ");
    652 
    653 		for (p = buf; *p; p++) {
    654 			if (*p == '%') {
    655 				p++;
    656 				switch (*p) {
    657 
    658 				case 'c':
    659 					cprintf(stdout, "%s",
    660 					    curclass.classname ?
    661 					    curclass.classname : "<unknown>");
    662 					break;
    663 
    664 				case 'C':
    665 					if (getcwd(cwd, sizeof(cwd)-1) == NULL){
    666 						syslog(LOG_WARNING,
    667 						    "can't getcwd: %s",
    668 						    strerror(errno));
    669 						continue;
    670 					}
    671 					cprintf(stdout, "%s", cwd);
    672 					break;
    673 
    674 				case 'E':
    675 					if (! EMPTYSTR(emailaddr))
    676 						cprintf(stdout, "%s",
    677 						    emailaddr);
    678 					break;
    679 
    680 				case 'L':
    681 					cprintf(stdout, "%s", hostname);
    682 					break;
    683 
    684 				case 'M':
    685 					if (curclass.limit == -1) {
    686 						cprintf(stdout, "unlimited");
    687 						lastnum = 0;
    688 					} else {
    689 						cprintf(stdout, "%d",
    690 						    curclass.limit);
    691 						lastnum = curclass.limit;
    692 					}
    693 					break;
    694 
    695 				case 'N':
    696 					cprintf(stdout, "%d", connections);
    697 					lastnum = connections;
    698 					break;
    699 
    700 				case 'R':
    701 					cprintf(stdout, "%s", remotehost);
    702 					break;
    703 
    704 				case 's':
    705 					if (lastnum != 1)
    706 						cprintf(stdout, "s");
    707 					break;
    708 
    709 				case 'S':
    710 					if (lastnum != 1)
    711 						cprintf(stdout, "S");
    712 					break;
    713 
    714 				case 'T':
    715 					now = time(NULL);
    716 					cprintf(stdout, "%.24s", ctime(&now));
    717 					break;
    718 
    719 				case 'U':
    720 					cprintf(stdout, "%s",
    721 					    pw ? pw->pw_name : "<unknown>");
    722 					break;
    723 
    724 				case '%':
    725 					CPUTC('%', stdout);
    726 					break;
    727 
    728 				}
    729 			} else
    730 				CPUTC(*p, stdout);
    731 		}
    732 		cprintf(stdout, "\r\n");
    733 	}
    734 
    735 	(void)fflush(stdout);
    736 	(void)fclose(f);
    737 	return (1);
    738 }
    739 
    740 /*
    741  * Parse src, expanding '%' escapes, into dst (which must be at least
    742  * MAXPATHLEN long).
    743  */
    744 void
    745 format_path(char *dst, const char *src)
    746 {
    747 	size_t len;
    748 	const char *p;
    749 
    750 	dst[0] = '\0';
    751 	len = 0;
    752 	if (src == NULL)
    753 		return;
    754 	for (p = src; *p && len < MAXPATHLEN; p++) {
    755 		if (*p == '%') {
    756 			p++;
    757 			switch (*p) {
    758 
    759 			case 'c':
    760 				len += strlcpy(dst + len, curclass.classname,
    761 				    MAXPATHLEN - len);
    762 				break;
    763 
    764 			case 'd':
    765 				len += strlcpy(dst + len, pw->pw_dir,
    766 				    MAXPATHLEN - len);
    767 				break;
    768 
    769 			case 'u':
    770 				len += strlcpy(dst + len, pw->pw_name,
    771 				    MAXPATHLEN - len);
    772 				break;
    773 
    774 			case '%':
    775 				dst[len++] = '%';
    776 				break;
    777 
    778 			}
    779 		} else
    780 			dst[len++] = *p;
    781 	}
    782 	if (len < MAXPATHLEN)
    783 		dst[len] = '\0';
    784 	dst[MAXPATHLEN - 1] = '\0';
    785 }
    786 
    787 /*
    788  * Find s2 at the end of s1.  If found, return a string up to (but
    789  * not including) s2, otherwise returns NULL.
    790  */
    791 static char *
    792 strend(const char *s1, char *s2)
    793 {
    794 	static	char buf[MAXPATHLEN];
    795 
    796 	char	*start;
    797 	size_t	l1, l2;
    798 
    799 	l1 = strlen(s1);
    800 	l2 = strlen(s2);
    801 
    802 	if (l2 >= l1)
    803 		return(NULL);
    804 
    805 	strlcpy(buf, s1, sizeof(buf));
    806 	start = buf + (l1 - l2);
    807 
    808 	if (strcmp(start, s2) == 0) {
    809 		*start = '\0';
    810 		return(buf);
    811 	} else
    812 		return(NULL);
    813 }
    814 
    815 static int
    816 filetypematch(char *types, int mode)
    817 {
    818 	for ( ; types[0] != '\0'; types++)
    819 		switch (*types) {
    820 		  case 'd':
    821 			if (S_ISDIR(mode))
    822 				return(1);
    823 			break;
    824 		  case 'f':
    825 			if (S_ISREG(mode))
    826 				return(1);
    827 			break;
    828 		}
    829 	return(0);
    830 }
    831 
    832 /*
    833  * Look for a conversion.  If we succeed, return a pointer to the
    834  * command to execute for the conversion.
    835  *
    836  * The command is stored in a static array so there's no memory
    837  * leak problems, and not too much to change in ftpd.c.  This
    838  * routine doesn't need to be re-entrant unless we start using a
    839  * multi-threaded ftpd, and that's not likely for a while...
    840  */
    841 char **
    842 do_conversion(const char *fname)
    843 {
    844 	struct ftpconv	*cp;
    845 	struct stat	 st;
    846 	int		 o_errno;
    847 	char		*base = NULL;
    848 	char		*cmd, *p, *lp, **argv;
    849 	StringList	*sl;
    850 
    851 	o_errno = errno;
    852 	sl = NULL;
    853 	cmd = NULL;
    854 	for (cp = curclass.conversions; cp != NULL; cp = cp->next) {
    855 		if (cp->suffix == NULL) {
    856 			syslog(LOG_WARNING,
    857 			    "cp->suffix==NULL in conv list; SHOULDN'T HAPPEN!");
    858 			continue;
    859 		}
    860 		if ((base = strend(fname, cp->suffix)) == NULL)
    861 			continue;
    862 		if (cp->types == NULL || cp->disable == NULL ||
    863 		    cp->command == NULL)
    864 			continue;
    865 					/* Is it enabled? */
    866 		if (strcmp(cp->disable, ".") != 0 &&
    867 		    stat(cp->disable, &st) == 0)
    868 				continue;
    869 					/* Does the base exist? */
    870 		if (stat(base, &st) < 0)
    871 			continue;
    872 					/* Is the file type ok */
    873 		if (!filetypematch(cp->types, st.st_mode))
    874 			continue;
    875 		break;			/* "We have a winner!" */
    876 	}
    877 
    878 	/* If we got through the list, no conversion */
    879 	if (cp == NULL)
    880 		goto cleanup_do_conv;
    881 
    882 	/* Split up command into an argv */
    883 	if ((sl = sl_init()) == NULL)
    884 		goto cleanup_do_conv;
    885 	cmd = xstrdup(cp->command);
    886 	p = cmd;
    887 	while (p) {
    888 		NEXTWORD(p, lp);
    889 		if (strcmp(lp, "%s") == 0)
    890 			lp = base;
    891 		if (sl_add(sl, xstrdup(lp)) == -1)
    892 			goto cleanup_do_conv;
    893 	}
    894 
    895 	if (sl_add(sl, NULL) == -1)
    896 		goto cleanup_do_conv;
    897 	argv = sl->sl_str;
    898 	free(cmd);
    899 	free(sl);
    900 	return(argv);
    901 
    902  cleanup_do_conv:
    903 	if (sl)
    904 		sl_free(sl, 1);
    905 	free(cmd);
    906 	errno = o_errno;
    907 	return(NULL);
    908 }
    909 
    910 /*
    911  * Convert the string `arg' to a long long, which may have an optional SI suffix
    912  * (`b', `k', `m', `g', `t'). Returns the number for success, -1 otherwise.
    913  */
    914 LLT
    915 strsuftoll(const char *arg)
    916 {
    917 	char *cp;
    918 	LLT val;
    919 
    920 	if (!isdigit((unsigned char)arg[0]))
    921 		return (-1);
    922 
    923 	val = STRTOLL(arg, &cp, 10);
    924 	if (cp != NULL) {
    925 		if (cp[0] != '\0' && cp[1] != '\0')
    926 			 return (-1);
    927 		switch (tolower((unsigned char)cp[0])) {
    928 		case '\0':
    929 		case 'b':
    930 			break;
    931 		case 'k':
    932 			val <<= 10;
    933 			break;
    934 		case 'm':
    935 			val <<= 20;
    936 			break;
    937 		case 'g':
    938 			val <<= 30;
    939 			break;
    940 #ifndef NO_LONG_LONG
    941 		case 't':
    942 			val <<= 40;
    943 			break;
    944 #endif
    945 		default:
    946 			return (-1);
    947 		}
    948 	}
    949 	if (val < 0)
    950 		return (-1);
    951 
    952 	return (val);
    953 }
    954 
    955 /*
    956  * Count the number of current connections, reading from
    957  *	/var/run/ftpd.pids-<class>
    958  * Does a kill -0 on each pid in that file, and only counts
    959  * processes that exist (or frees the slot if it doesn't).
    960  * Adds getpid() to the first free slot. Truncates the file
    961  * if possible.
    962  */
    963 void
    964 count_users(void)
    965 {
    966 	char	fn[MAXPATHLEN];
    967 	int	fd, i, last;
    968 	size_t	count;
    969 	pid_t  *pids, mypid;
    970 	struct stat sb;
    971 
    972 	(void)strlcpy(fn, _PATH_CLASSPIDS, sizeof(fn));
    973 	(void)strlcat(fn, curclass.classname, sizeof(fn));
    974 	pids = NULL;
    975 	connections = 1;
    976 
    977 	if ((fd = open(fn, O_RDWR | O_CREAT, 0600)) == -1)
    978 		return;
    979 	if (lockf(fd, F_TLOCK, 0) == -1)
    980 		goto cleanup_count;
    981 	if (fstat(fd, &sb) == -1)
    982 		goto cleanup_count;
    983 	if ((pids = malloc(sb.st_size + sizeof(pid_t))) == NULL)
    984 		goto cleanup_count;
    985 	count = read(fd, pids, sb.st_size);
    986 	if (count < 0 || count != sb.st_size)
    987 		goto cleanup_count;
    988 	count /= sizeof(pid_t);
    989 	mypid = getpid();
    990 	last = 0;
    991 	for (i = 0; i < count; i++) {
    992 		if (pids[i] == 0)
    993 			continue;
    994 		if (kill(pids[i], 0) == -1 && errno != EPERM) {
    995 			if (mypid != 0) {
    996 				pids[i] = mypid;
    997 				mypid = 0;
    998 				last = i;
    999 			}
   1000 		} else {
   1001 			connections++;
   1002 			last = i;
   1003 		}
   1004 	}
   1005 	if (mypid != 0) {
   1006 		if (pids[last] != 0)
   1007 			last++;
   1008 		pids[last] = mypid;
   1009 	}
   1010 	count = (last + 1) * sizeof(pid_t);
   1011 	if (lseek(fd, 0, SEEK_SET) == -1)
   1012 		goto cleanup_count;
   1013 	if (write(fd, pids, count) == -1)
   1014 		goto cleanup_count;
   1015 	(void)ftruncate(fd, count);
   1016 
   1017  cleanup_count:
   1018 	if (lseek(fd, 0, SEEK_SET) != -1)
   1019 		(void)lockf(fd, F_ULOCK, 0);
   1020 	close(fd);
   1021 	REASSIGN(pids, NULL);
   1022 }
   1023