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