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