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