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