Home | History | Annotate | Line # | Download | only in ftpd
conf.c revision 1.29
      1 /*	$NetBSD: conf.c,v 1.29 2000/01/13 00:04:31 lukem Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997-2000 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.29 2000/01/13 00:04:31 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 <ctype.h>
     49 #include <errno.h>
     50 #include <fcntl.h>
     51 #include <glob.h>
     52 #include <setjmp.h>
     53 #include <signal.h>
     54 #include <stdio.h>
     55 #include <stdlib.h>
     56 #include <string.h>
     57 #include <stringlist.h>
     58 #include <syslog.h>
     59 #include <time.h>
     60 #include <unistd.h>
     61 #include <util.h>
     62 
     63 #ifdef KERBEROS5
     64 #include <krb5/krb5.h>
     65 #endif
     66 
     67 #include "extern.h"
     68 #include "pathnames.h"
     69 
     70 static char *strend __P((const char *, char *));
     71 static int filetypematch __P((char *, int));
     72 
     73 
     74 /*
     75  * Initialise curclass to an `empty' state
     76  */
     77 void
     78 init_curclass()
     79 {
     80 	struct ftpconv	*conv, *cnext;
     81 
     82 	for (conv = curclass.conversions; conv != NULL; conv = cnext) {
     83 		REASSIGN(conv->suffix, NULL);
     84 		REASSIGN(conv->types, NULL);
     85 		REASSIGN(conv->disable, NULL);
     86 		REASSIGN(conv->command, NULL);
     87 		cnext = conv->next;
     88 		free(conv);
     89 	}
     90 
     91 	curclass.checkportcmd = 0;
     92 	REASSIGN(curclass.classname, NULL);
     93 	curclass.conversions =	NULL;
     94 	REASSIGN(curclass.display, NULL);
     95 	curclass.limit =	-1;		/* unlimited connections */
     96 	REASSIGN(curclass.limitfile, NULL);
     97 	curclass.maxrateget =	0;
     98 	curclass.maxrateput =	0;
     99 	curclass.maxtimeout =	7200;		/* 2 hours */
    100 	curclass.modify =	1;
    101 	REASSIGN(curclass.motd, xstrdup(_PATH_FTPLOGINMESG));
    102 	REASSIGN(curclass.notify, NULL);
    103 	curclass.passive =	1;
    104 	curclass.portmin =	0;
    105 	curclass.portmax =	0;
    106 	curclass.rateget =	0;
    107 	curclass.rateput =	0;
    108 	curclass.timeout =	900;		/* 15 minutes */
    109 	curclass.umask =	027;
    110 	curclass.upload =	1;
    111 }
    112 
    113 /*
    114  * Parse the configuration file, looking for the named class, and
    115  * define curclass to contain the appropriate settings.
    116  */
    117 void
    118 parse_conf(findclass)
    119 	char *findclass;
    120 {
    121 	FILE		*f;
    122 	char		*buf, *p;
    123 	size_t		 len;
    124 	int		 none, match, rate;
    125 	char		*endp;
    126 	char		*class, *word, *arg, *template;
    127 	const char	*infile;
    128 	size_t		 line;
    129 	unsigned int	 timeout;
    130 	struct ftpconv	*conv, *cnext;
    131 
    132 	init_curclass();
    133 	REASSIGN(curclass.classname, xstrdup(findclass));
    134 	if (strcasecmp(findclass, "guest") == 0) {
    135 		curclass.modify = 0;
    136 		curclass.umask = 0707;
    137 	}
    138 
    139 	infile = conffilename(_PATH_FTPDCONF);
    140 	if ((f = fopen(infile, "r")) == NULL)
    141 		return;
    142 
    143 	line = 0;
    144 	template = NULL;
    145 	for (;
    146 	    (buf = fparseln(f, &len, &line, NULL, FPARSELN_UNESCCOMM |
    147 	    		FPARSELN_UNESCCONT | FPARSELN_UNESCESC)) != NULL;
    148 	    free(buf)) {
    149 		none = match = 0;
    150 		p = buf;
    151 		if (len < 1)
    152 			continue;
    153 		if (p[len - 1] == '\n')
    154 			p[--len] = '\0';
    155 		if (EMPTYSTR(p))
    156 			continue;
    157 
    158 		NEXTWORD(p, word);
    159 		NEXTWORD(p, class);
    160 		NEXTWORD(p, arg);
    161 		if (EMPTYSTR(word) || EMPTYSTR(class))
    162 			continue;
    163 		if (strcasecmp(class, "none") == 0)
    164 			none = 1;
    165 		if (! (strcasecmp(class, findclass) == 0 ||
    166 		       (template != NULL && strcasecmp(class, template) == 0) ||
    167 		       none ||
    168 		       strcasecmp(class, "all") == 0) )
    169 			continue;
    170 
    171 		if (strcasecmp(word, "checkportcmd") == 0) {
    172 			if (none ||
    173 			    (!EMPTYSTR(arg) && strcasecmp(arg, "off") == 0))
    174 				curclass.checkportcmd = 0;
    175 			else
    176 				curclass.checkportcmd = 1;
    177 
    178 		} else if (strcasecmp(word, "classtype") == 0) {
    179 			if (!none && !EMPTYSTR(arg)) {
    180 				if (strcasecmp(arg, "GUEST") == 0)
    181 					curclass.type = CLASS_GUEST;
    182 				else if (strcasecmp(arg, "CHROOT") == 0)
    183 					curclass.type = CLASS_CHROOT;
    184 				else if (strcasecmp(arg, "REAL") == 0)
    185 					curclass.type = CLASS_REAL;
    186 				else {
    187 					syslog(LOG_WARNING,
    188 				    "%s line %d: unknown class type `%s'",
    189 					    infile, (int)line, arg);
    190 					continue;
    191 				}
    192 			}
    193 
    194 		} else if (strcasecmp(word, "conversion") == 0) {
    195 			char *suffix, *types, *disable, *convcmd;
    196 
    197 			if (EMPTYSTR(arg)) {
    198 				syslog(LOG_WARNING,
    199 				    "%s line %d: %s requires a suffix",
    200 				    infile, (int)line, word);
    201 				continue;	/* need a suffix */
    202 			}
    203 			NEXTWORD(p, types);
    204 			NEXTWORD(p, disable);
    205 			convcmd = p;
    206 			if (convcmd)
    207 				convcmd += strspn(convcmd, " \t");
    208 			suffix = xstrdup(arg);
    209 			if (none || EMPTYSTR(types) ||
    210 			    EMPTYSTR(disable) || EMPTYSTR(convcmd)) {
    211 				types = NULL;
    212 				disable = NULL;
    213 				convcmd = NULL;
    214 			} else {
    215 				types = xstrdup(types);
    216 				disable = xstrdup(disable);
    217 				convcmd = xstrdup(convcmd);
    218 			}
    219 			for (conv = curclass.conversions; conv != NULL;
    220 			    conv = conv->next) {
    221 				if (strcmp(conv->suffix, suffix) == 0)
    222 					break;
    223 			}
    224 			if (conv == NULL) {
    225 				conv = (struct ftpconv *)
    226 				    calloc(1, sizeof(struct ftpconv));
    227 				if (conv == NULL) {
    228 					syslog(LOG_WARNING, "can't malloc");
    229 					continue;
    230 				}
    231 				conv->next = NULL;
    232 				for (cnext = curclass.conversions;
    233 				    cnext != NULL; cnext = cnext->next)
    234 					if (cnext->next == NULL)
    235 						break;
    236 				if (cnext != NULL)
    237 					cnext->next = conv;
    238 				else
    239 					curclass.conversions = conv;
    240 			}
    241 			REASSIGN(conv->suffix, suffix);
    242 			REASSIGN(conv->types, types);
    243 			REASSIGN(conv->disable, disable);
    244 			REASSIGN(conv->command, convcmd);
    245 
    246 		} else if (strcasecmp(word, "display") == 0) {
    247 			if (none || EMPTYSTR(arg))
    248 				arg = NULL;
    249 			else
    250 				arg = xstrdup(arg);
    251 			REASSIGN(curclass.display, arg);
    252 
    253 		} else if (strcasecmp(word, "limit") == 0) {
    254 			int limit;
    255 
    256 			if (none || EMPTYSTR(arg))
    257 				continue;
    258 			limit = (int)strtol(arg, &endp, 10);
    259 			if (*endp != 0) {
    260 				syslog(LOG_WARNING,
    261 				    "%s line %d: invalid limit %s",
    262 				    infile, (int)line, arg);
    263 				continue;
    264 			}
    265 			curclass.limit = limit;
    266 			REASSIGN(curclass.limitfile,
    267 			    EMPTYSTR(p) ? NULL : xstrdup(p));
    268 
    269 		} else if (strcasecmp(word, "maxtimeout") == 0) {
    270 			if (none || EMPTYSTR(arg))
    271 				continue;
    272 			timeout = (unsigned int)strtoul(arg, &endp, 10);
    273 			if (*endp != 0) {
    274 				syslog(LOG_WARNING,
    275 				    "%s line %d: invalid maxtimeout %s",
    276 				    infile, (int)line, arg);
    277 				continue;
    278 			}
    279 			if (timeout < 30) {
    280 				syslog(LOG_WARNING,
    281 				    "%s line %d: maxtimeout %d < 30 seconds",
    282 				    infile, (int)line, timeout);
    283 				continue;
    284 			}
    285 			if (timeout < curclass.timeout) {
    286 				syslog(LOG_WARNING,
    287 				    "%s line %d: maxtimeout %d < timeout (%d)",
    288 				    infile, (int)line, timeout,
    289 				    curclass.timeout);
    290 				continue;
    291 			}
    292 			curclass.maxtimeout = timeout;
    293 
    294 		} else if (strcasecmp(word, "modify") == 0) {
    295 			if (none ||
    296 			    (!EMPTYSTR(arg) && strcasecmp(arg, "off") == 0))
    297 				curclass.modify = 0;
    298 			else
    299 				curclass.modify = 1;
    300 
    301 		} else if (strcasecmp(word, "motd") == 0) {
    302 			if (none || EMPTYSTR(arg))
    303 				arg = NULL;
    304 			else
    305 				arg = xstrdup(arg);
    306 			REASSIGN(curclass.motd, arg);
    307 
    308 
    309 		} else if (strcasecmp(word, "notify") == 0) {
    310 			if (none || EMPTYSTR(arg))
    311 				arg = NULL;
    312 			else
    313 				arg = xstrdup(arg);
    314 			REASSIGN(curclass.notify, arg);
    315 
    316 		} else if (strcasecmp(word, "passive") == 0) {
    317 			if (none ||
    318 			    (!EMPTYSTR(arg) && strcasecmp(arg, "off") == 0))
    319 				curclass.passive = 0;
    320 			else
    321 				curclass.passive = 1;
    322 
    323 		} else if (strcasecmp(word, "portrange") == 0) {
    324 			int minport, maxport;
    325 			char *min, *max;
    326 
    327 			if (none) {
    328 				curclass.portmin = 0;
    329 				curclass.portmax = 0;
    330 				continue;
    331 			}
    332 			if (EMPTYSTR(arg))
    333 				continue;
    334 			min = arg;
    335 			NEXTWORD(p, max);
    336 			if (EMPTYSTR(max)) {
    337 				syslog(LOG_WARNING,
    338 				   "%s line %d: missing maxport argument",
    339 				   infile, (int)line);
    340 				continue;
    341 			}
    342 			minport = (int)strtol(min, &endp, 10);
    343 			if (*endp != 0 || minport < IPPORT_RESERVED ||
    344 			    minport > IPPORT_ANONMAX) {
    345 				syslog(LOG_WARNING,
    346 				    "%s line %d: invalid minport %s",
    347 				    infile, (int)line, min);
    348 				continue;
    349 			}
    350 			maxport = (int)strtol(max, &endp, 10);
    351 			if (*endp != 0 || maxport < IPPORT_RESERVED ||
    352 			    maxport > IPPORT_ANONMAX) {
    353 				syslog(LOG_WARNING,
    354 				    "%s line %d: invalid maxport %s",
    355 				    infile, (int)line, max);
    356 				continue;
    357 			}
    358 			if (minport >= maxport) {
    359 				syslog(LOG_WARNING,
    360 				    "%s line %d: minport %d >= maxport %d",
    361 				    infile, (int)line, minport, maxport);
    362 				continue;
    363 			}
    364 			curclass.portmin = minport;
    365 			curclass.portmax = maxport;
    366 
    367 		} else if (strcasecmp(word, "rateget") == 0) {
    368 			if (none || EMPTYSTR(arg))
    369 				continue;
    370 			rate = strsuftoi(arg);
    371 			if (rate == -1) {
    372 				syslog(LOG_WARNING,
    373 				    "%s line %d: invalid rateget %s",
    374 				    infile, (int)line, arg);
    375 				continue;
    376 			}
    377 			curclass.maxrateget = rate;
    378 			curclass.rateget = rate;
    379 
    380 		} else if (strcasecmp(word, "rateput") == 0) {
    381 			if (none || EMPTYSTR(arg))
    382 				continue;
    383 			rate = strsuftoi(arg);
    384 			if (rate == -1) {
    385 				syslog(LOG_WARNING,
    386 				    "%s line %d: invalid rateput %s",
    387 				    infile, (int)line, arg);
    388 				continue;
    389 			}
    390 			curclass.maxrateput = rate;
    391 			curclass.rateput = rate;
    392 
    393 		} else if (strcasecmp(word, "timeout") == 0) {
    394 			if (none || EMPTYSTR(arg))
    395 				continue;
    396 			timeout = (unsigned int)strtoul(arg, &endp, 10);
    397 			if (*endp != 0) {
    398 				syslog(LOG_WARNING,
    399 				    "%s line %d: invalid timeout %s",
    400 				    infile, (int)line, arg);
    401 				continue;
    402 			}
    403 			if (timeout < 30) {
    404 				syslog(LOG_WARNING,
    405 				    "%s line %d: timeout %d < 30 seconds",
    406 				    infile, (int)line, timeout);
    407 				continue;
    408 			}
    409 			if (timeout > curclass.maxtimeout) {
    410 				syslog(LOG_WARNING,
    411 				    "%s line %d: timeout %d > maxtimeout (%d)",
    412 				    infile, (int)line, timeout,
    413 				    curclass.maxtimeout);
    414 				continue;
    415 			}
    416 			curclass.timeout = timeout;
    417 
    418 		} else if (strcasecmp(word, "template") == 0) {
    419 			if (none)
    420 				continue;
    421 			REASSIGN(template, EMPTYSTR(arg) ? NULL : xstrdup(arg));
    422 
    423 		} else if (strcasecmp(word, "umask") == 0) {
    424 			mode_t umask;
    425 
    426 			if (none || EMPTYSTR(arg))
    427 				continue;
    428 			umask = (mode_t)strtoul(arg, &endp, 8);
    429 			if (*endp != 0 || umask > 0777) {
    430 				syslog(LOG_WARNING,
    431 				    "%s line %d: invalid umask %s",
    432 				    infile, (int)line, arg);
    433 				continue;
    434 			}
    435 			curclass.umask = umask;
    436 
    437 		} else if (strcasecmp(word, "upload") == 0) {
    438 			if (none ||
    439 			    (!EMPTYSTR(arg) && strcasecmp(arg, "off") == 0)) {
    440 				curclass.modify = 0;
    441 				curclass.upload = 0;
    442 			} else
    443 				curclass.upload = 1;
    444 
    445 		} else {
    446 			syslog(LOG_WARNING,
    447 			    "%s line %d: unknown directive '%s'",
    448 			    infile, (int)line, word);
    449 			continue;
    450 		}
    451 	}
    452 	REASSIGN(template, NULL);
    453 	fclose(f);
    454 }
    455 
    456 /*
    457  * Show file listed in curclass.display first time in, and list all the
    458  * files named in curclass.notify in the current directory.  Send back
    459  * responses with the prefix `code' + "-".
    460  */
    461 void
    462 show_chdir_messages(code)
    463 	int	code;
    464 {
    465 	static StringList *slist = NULL;
    466 
    467 	struct stat st;
    468 	struct tm *t;
    469 	glob_t	 gl;
    470 	time_t	 now, then;
    471 	int	 age;
    472 	char	 cwd[MAXPATHLEN];
    473 	char	*cp, **rlist;
    474 
    475 	if (quietmessages)
    476 		return;
    477 
    478 		/* Setup list for directory cache */
    479 	if (slist == NULL)
    480 		slist = sl_init();
    481 	if (slist == NULL) {
    482 		syslog(LOG_WARNING, "can't allocate memory for stringlist");
    483 		return;
    484 	}
    485 
    486 		/* Check if this directory has already been visited */
    487 	if (getcwd(cwd, sizeof(cwd) - 1) == NULL) {
    488 		syslog(LOG_WARNING, "can't getcwd: %s", strerror(errno));
    489 		return;
    490 	}
    491 	if (sl_find(slist, cwd) != NULL)
    492 		return;
    493 
    494 	cp = xstrdup(cwd);
    495 	if (sl_add(slist, cp) == -1)
    496 		syslog(LOG_WARNING, "can't add `%s' to stringlist", cp);
    497 
    498 		/* First check for a display file */
    499 	(void)format_file(curclass.display, code);
    500 
    501 		/* Now see if there are any notify files */
    502 	if (EMPTYSTR(curclass.notify))
    503 		return;
    504 
    505 	if (glob(curclass.notify, 0, NULL, &gl) != 0 || gl.gl_matchc == 0)
    506 		return;
    507 	time(&now);
    508 	for (rlist = gl.gl_pathv; *rlist != NULL; rlist++) {
    509 		if (stat(*rlist, &st) != 0)
    510 			continue;
    511 		if (!S_ISREG(st.st_mode))
    512 			continue;
    513 		then = st.st_mtime;
    514 		if (code != 0) {
    515 			lreply(code, "");
    516 			code = 0;
    517 		}
    518 		lreply(code, "Please read the file %s", *rlist);
    519 		t = localtime(&now);
    520 		age = 365 * t->tm_year + t->tm_yday;
    521 		t = localtime(&then);
    522 		age -= 365 * t->tm_year + t->tm_yday;
    523 		lreply(code, "  it was last modified on %.24s - %d day%s ago",
    524 		    ctime(&then), age, PLURAL(age));
    525 	}
    526 	globfree(&gl);
    527 }
    528 
    529 int
    530 format_file(file, code)
    531 	const char *file;
    532 	int code;
    533 {
    534 	FILE   *f;
    535 	char   *buf, *p, *cwd;
    536 	size_t	len;
    537 	off_t	b;
    538 	time_t	now;
    539 
    540 	if (quietmessages)
    541 		return (0);
    542 
    543 #define PUTC(x)	putchar(x), b++
    544 
    545 	if (EMPTYSTR(file))
    546 		return(0);
    547 	if ((f = fopen(file, "r")) == NULL)
    548 		return (0);
    549 	lreply(code, "");
    550 
    551 	b = 0;
    552 	for (;
    553 	    (buf = fparseln(f, &len, NULL, "\0\0\0", 0)) != NULL; free(buf)) {
    554 		if (len > 0)
    555 			if (buf[len - 1] == '\n')
    556 				buf[--len] = '\0';
    557 		b += printf("    ");
    558 
    559 		for (p = buf; *p; p++) {
    560 			if (*p == '%') {
    561 				p++;
    562 				switch (*p) {
    563 
    564 				case 'c':
    565 					b += printf("%s",
    566 					    curclass.classname ?
    567 					    curclass.classname : "<unknown>");
    568 					break;
    569 
    570 				case 'C':
    571 					if (getcwd(cwd, sizeof(cwd)-1) == NULL){
    572 						syslog(LOG_WARNING,
    573 						    "can't getcwd: %s",
    574 						    strerror(errno));
    575 						continue;
    576 					}
    577 					b += printf("%s", cwd);
    578 					break;
    579 
    580 				case 'E':
    581 						/* XXXX email address */
    582 					break;
    583 
    584 				case 'L':
    585 					b += printf("%s", hostname);
    586 					break;
    587 
    588 				case 'M':
    589 					if (curclass.limit == -1)
    590 						b += printf("unlimited");
    591 					else
    592 						b += printf("%d",
    593 						    curclass.limit);
    594 					break;
    595 
    596 				case 'N':
    597 					if (connections > 0)
    598 						b += printf("%d", connections);
    599 					break;
    600 
    601 				case 'R':
    602 					b += printf("%s", remotehost);
    603 					break;
    604 
    605 				case 'T':
    606 					now = time(NULL);
    607 					b += printf("%.24s", ctime(&now));
    608 					break;
    609 
    610 				case 'U':
    611 					b += printf("%s",
    612 					    pw ? pw->pw_name : "<unknown>");
    613 					break;
    614 
    615 				case '%':
    616 					PUTC('%');
    617 					break;
    618 
    619 				}
    620 			} else {
    621 				PUTC(*p);
    622 			}
    623 		}
    624 		PUTC('\r');
    625 		PUTC('\n');
    626 	}
    627 
    628 	total_bytes += b;
    629 	total_bytes_out += b;
    630 	(void)fflush(stdout);
    631 	(void)fclose(f);
    632 	return (1);
    633 }
    634 
    635 /*
    636  * Find s2 at the end of s1.  If found, return a string up to (but
    637  * not including) s2, otherwise returns NULL.
    638  */
    639 static char *
    640 strend(s1, s2)
    641 	const char *s1;
    642 	char *s2;
    643 {
    644 	static	char buf[MAXPATHLEN];
    645 
    646 	char	*start;
    647 	size_t	l1, l2;
    648 
    649 	l1 = strlen(s1);
    650 	l2 = strlen(s2);
    651 
    652 	if (l2 >= l1)
    653 		return(NULL);
    654 
    655 	strlcpy(buf, s1, sizeof(buf));
    656 	start = buf + (l1 - l2);
    657 
    658 	if (strcmp(start, s2) == 0) {
    659 		*start = '\0';
    660 		return(buf);
    661 	} else
    662 		return(NULL);
    663 }
    664 
    665 static int
    666 filetypematch(types, mode)
    667 	char	*types;
    668 	int	mode;
    669 {
    670 	for ( ; types[0] != '\0'; types++)
    671 		switch (*types) {
    672 		  case 'd':
    673 			if (S_ISDIR(mode))
    674 				return(1);
    675 			break;
    676 		  case 'f':
    677 			if (S_ISREG(mode))
    678 				return(1);
    679 			break;
    680 		}
    681 	return(0);
    682 }
    683 
    684 /*
    685  * Look for a conversion.  If we succeed, return a pointer to the
    686  * command to execute for the conversion.
    687  *
    688  * The command is stored in a static array so there's no memory
    689  * leak problems, and not too much to change in ftpd.c.  This
    690  * routine doesn't need to be re-entrant unless we start using a
    691  * multi-threaded ftpd, and that's not likely for a while...
    692  */
    693 char **
    694 do_conversion(fname)
    695 	const char *fname;
    696 {
    697 	struct ftpconv	*cp;
    698 	struct stat	 st;
    699 	int		 o_errno;
    700 	char		*base = NULL;
    701 	char		*cmd, *p, *lp, **argv;
    702 	StringList	*sl;
    703 
    704 	o_errno = errno;
    705 	sl = NULL;
    706 	cmd = NULL;
    707 	for (cp = curclass.conversions; cp != NULL; cp = cp->next) {
    708 		if (cp->suffix == NULL) {
    709 			syslog(LOG_WARNING,
    710 			    "cp->suffix==NULL in conv list; SHOULDN'T HAPPEN!");
    711 			continue;
    712 		}
    713 		if ((base = strend(fname, cp->suffix)) == NULL)
    714 			continue;
    715 		if (cp->types == NULL || cp->disable == NULL ||
    716 		    cp->command == NULL)
    717 			continue;
    718 					/* Is it enabled? */
    719 		if (strcmp(cp->disable, ".") != 0 &&
    720 		    stat(cp->disable, &st) == 0)
    721 				continue;
    722 					/* Does the base exist? */
    723 		if (stat(base, &st) < 0)
    724 			continue;
    725 					/* Is the file type ok */
    726 		if (!filetypematch(cp->types, st.st_mode))
    727 			continue;
    728 		break;			/* "We have a winner!" */
    729 	}
    730 
    731 	/* If we got through the list, no conversion */
    732 	if (cp == NULL)
    733 		goto cleanup_do_conv;
    734 
    735 	/* Split up command into an argv */
    736 	if ((sl = sl_init()) == NULL)
    737 		goto cleanup_do_conv;
    738 	cmd = xstrdup(cp->command);
    739 	p = cmd;
    740 	while (p) {
    741 		NEXTWORD(p, lp);
    742 		if (strcmp(lp, "%s") == 0)
    743 			lp = base;
    744 		if (sl_add(sl, xstrdup(lp)) == -1)
    745 			goto cleanup_do_conv;
    746 	}
    747 
    748 	if (sl_add(sl, NULL) == -1)
    749 		goto cleanup_do_conv;
    750 	argv = sl->sl_str;
    751 	free(cmd);
    752 	free(sl);
    753 	return(argv);
    754 
    755  cleanup_do_conv:
    756 	if (sl)
    757 		sl_free(sl, 1);
    758 	free(cmd);
    759 	errno = o_errno;
    760 	return(NULL);
    761 }
    762 
    763 /*
    764  * Convert the string `arg' to an int, which may have an optional SI suffix
    765  * (`b', `k', `m', `g'). Returns the number for success, -1 otherwise.
    766  */
    767 int
    768 strsuftoi(arg)
    769 	const char *arg;
    770 {
    771 	char *cp;
    772 	long val;
    773 
    774 	if (!isdigit((unsigned char)arg[0]))
    775 		return (-1);
    776 
    777 	val = strtol(arg, &cp, 10);
    778 	if (cp != NULL) {
    779 		if (cp[0] != '\0' && cp[1] != '\0')
    780 			 return (-1);
    781 		switch (tolower((unsigned char)cp[0])) {
    782 		case '\0':
    783 		case 'b':
    784 			break;
    785 		case 'k':
    786 			val <<= 10;
    787 			break;
    788 		case 'm':
    789 			val <<= 20;
    790 			break;
    791 		case 'g':
    792 			val <<= 30;
    793 			break;
    794 		default:
    795 			return (-1);
    796 		}
    797 	}
    798 	if (val < 0 || val > INT_MAX)
    799 		return (-1);
    800 
    801 	return (val);
    802 }
    803 
    804 /*
    805  * Count the number of current connections, reading from
    806  *	/var/run/ftpd.pids-<class>
    807  * Does a kill -0 on each pid in that file, and only counts
    808  * processes that exist (or frees the slot if it doesn't).
    809  * Adds getpid() to the first free slot. Truncates the file
    810  * if possible.
    811  */
    812 void
    813 count_users()
    814 {
    815 	char	fn[MAXPATHLEN];
    816 	int	fd, i, last;
    817 	size_t	count;
    818 	pid_t  *pids, mypid;
    819 	struct stat sb;
    820 
    821 	(void)strlcpy(fn, _PATH_CLASSPIDS, sizeof(fn));
    822 	(void)strlcat(fn, curclass.classname, sizeof(fn));
    823 	pids = NULL;
    824 	connections = 1;
    825 
    826 	if ((fd = open(fn, O_RDWR | O_CREAT | O_EXLOCK, 0600)) == -1)
    827 		return;
    828 	if (fstat(fd, &sb) == -1)
    829 		goto cleanup_count;
    830 	if ((pids = malloc(sb.st_size + sizeof(pid_t))) == NULL)
    831 		goto cleanup_count;
    832 	count = read(fd, pids, sb.st_size);
    833 	if (count < 0 || count != sb.st_size)
    834 		goto cleanup_count;
    835 	count /= sizeof(pid_t);
    836 	mypid = getpid();
    837 	last = 0;
    838 	for (i = 0; i < count; i++) {
    839 		if (pids[i] == 0)
    840 			continue;
    841 		if (kill(pids[i], 0) == -1 && errno != EPERM) {
    842 			if (mypid != 0) {
    843 				pids[i] = mypid;
    844 				mypid = 0;
    845 				last = i;
    846 			}
    847 		} else {
    848 			connections++;
    849 			last = i;
    850 		}
    851 	}
    852 	if (mypid != 0) {
    853 		if (pids[last] != 0)
    854 			last++;
    855 		pids[last] = mypid;
    856 	}
    857 	count = (last + 1) * sizeof(pid_t);
    858 	if (lseek(fd, 0, SEEK_SET) == -1)
    859 		goto cleanup_count;
    860 	if (write(fd, pids, count) == -1)
    861 		goto cleanup_count;
    862 	(void)ftruncate(fd, count);
    863 
    864  cleanup_count:
    865 	(void)flock(fd, LOCK_UN);
    866 	close(fd);
    867 	REASSIGN(pids, NULL);
    868 }
    869