Home | History | Annotate | Line # | Download | only in ftpd
conf.c revision 1.23
      1  1.23     lukem /*	$NetBSD: conf.c,v 1.23 1999/12/07 05:30:54 lukem Exp $	*/
      2   1.1     lukem 
      3   1.1     lukem /*-
      4  1.19     lukem  * Copyright (c) 1997, 1999 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.23     lukem __RCSID("$NetBSD: conf.c,v 1.23 1999/12/07 05:30:54 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.1     lukem #include <errno.h>
     49   1.1     lukem #include <glob.h>
     50   1.1     lukem #include <stdio.h>
     51   1.2  christos #include <stdlib.h>
     52   1.1     lukem #include <string.h>
     53   1.1     lukem #include <stringlist.h>
     54   1.1     lukem #include <syslog.h>
     55  1.23     lukem #include <time.h>
     56  1.23     lukem #include <unistd.h>
     57  1.23     lukem #include <util.h>
     58  1.18  explorer 
     59  1.18  explorer #ifdef KERBEROS5
     60  1.21  christos #include <krb5/krb5.h>
     61  1.18  explorer #endif
     62   1.1     lukem 
     63   1.1     lukem #include "extern.h"
     64   1.1     lukem #include "pathnames.h"
     65   1.1     lukem 
     66   1.2  christos static char *strend __P((const char *, char *));
     67   1.2  christos static int filetypematch __P((char *, int));
     68  1.15     lukem 
     69  1.15     lukem struct ftpclass curclass;
     70  1.15     lukem 
     71   1.1     lukem 
     72   1.1     lukem /*
     73   1.1     lukem  * Parse the configuration file, looking for the named class, and
     74   1.1     lukem  * define curclass to contain the appropriate settings.
     75   1.1     lukem  */
     76   1.1     lukem void
     77   1.1     lukem parse_conf(findclass)
     78   1.2  christos 	char *findclass;
     79   1.1     lukem {
     80   1.1     lukem 	FILE		*f;
     81   1.1     lukem 	char		*buf, *p;
     82   1.1     lukem 	size_t		 len;
     83   1.2  christos 	int		 none, match;
     84   1.1     lukem 	char		*endp;
     85   1.1     lukem 	char		*class, *word, *arg;
     86   1.1     lukem 	const char	*infile;
     87  1.23     lukem 	size_t		 line;
     88   1.1     lukem 	unsigned int	 timeout;
     89   1.1     lukem 	struct ftpconv	*conv, *cnext;
     90   1.1     lukem 
     91   1.1     lukem #define REASSIGN(X,Y)	if (X) free(X); (X)=(Y)
     92  1.23     lukem #define NEXTWORD(P, W)	while ((W = strsep(&P, " \t")) != NULL && *W == '\0')
     93   1.1     lukem #define EMPTYSTR(W)	(W == NULL || *W == '\0')
     94   1.1     lukem 
     95   1.1     lukem 	REASSIGN(curclass.classname, findclass);
     96  1.23     lukem 	for (conv = curclass.conversions; conv != NULL; conv = cnext) {
     97   1.1     lukem 		REASSIGN(conv->suffix, NULL);
     98   1.1     lukem 		REASSIGN(conv->types, NULL);
     99   1.1     lukem 		REASSIGN(conv->disable, NULL);
    100   1.1     lukem 		REASSIGN(conv->command, NULL);
    101   1.1     lukem 		cnext = conv->next;
    102   1.1     lukem 		free(conv);
    103   1.1     lukem 	}
    104   1.9     lukem 	curclass.checkportcmd = 0;
    105   1.1     lukem 	curclass.conversions =	NULL;
    106   1.1     lukem 	REASSIGN(curclass.display, NULL);
    107   1.9     lukem 	curclass.maxtimeout =	7200;		/* 2 hours */
    108   1.1     lukem 	curclass.modify =	1;
    109   1.1     lukem 	REASSIGN(curclass.notify, NULL);
    110  1.14        tv 	curclass.passive =	1;
    111   1.1     lukem 	curclass.timeout =	900;		/* 15 minutes */
    112   1.1     lukem 	curclass.umask =	027;
    113   1.1     lukem 
    114   1.1     lukem 	if (strcasecmp(findclass, "guest") == 0) {
    115   1.9     lukem 		curclass.modify = 0;
    116   1.1     lukem 		curclass.umask = 0707;
    117   1.1     lukem 	}
    118   1.1     lukem 
    119   1.6     lukem 	infile = conffilename(_PATH_FTPDCONF);
    120   1.1     lukem 	if ((f = fopen(infile, "r")) == NULL)
    121   1.1     lukem 		return;
    122   1.1     lukem 
    123   1.1     lukem 	line = 0;
    124  1.23     lukem 	for (;
    125  1.23     lukem 	    (buf = fparseln(f, &len, &line, NULL, FPARSELN_UNESCCOMM |
    126  1.23     lukem 	    		FPARSELN_UNESCCONT | FPARSELN_UNESCESC)) != NULL;
    127  1.23     lukem 	    free(buf)) {
    128   1.1     lukem 		none = match = 0;
    129  1.23     lukem 		p = buf;
    130   1.5     lukem 		if (len < 1)
    131   1.5     lukem 			continue;
    132  1.23     lukem 		if (p[len - 1] == '\n')
    133  1.23     lukem 			p[--len] = '\0';
    134  1.23     lukem 		if (EMPTYSTR(p))
    135   1.1     lukem 			continue;
    136   1.1     lukem 
    137  1.23     lukem 		NEXTWORD(p, word);
    138  1.23     lukem 		NEXTWORD(p, class);
    139  1.23     lukem 		NEXTWORD(p, arg);
    140   1.1     lukem 		if (EMPTYSTR(word) || EMPTYSTR(class))
    141   1.1     lukem 			continue;
    142   1.1     lukem 		if (strcasecmp(class, "none") == 0)
    143   1.1     lukem 			none = 1;
    144   1.1     lukem 		if (strcasecmp(class, findclass) != 0 &&
    145   1.1     lukem 		    !none && strcasecmp(class, "all") != 0)
    146   1.1     lukem 			continue;
    147   1.1     lukem 
    148   1.9     lukem 		if (strcasecmp(word, "checkportcmd") == 0) {
    149   1.9     lukem 			if (none ||
    150   1.9     lukem 			    (!EMPTYSTR(arg) && strcasecmp(arg, "off") == 0))
    151   1.9     lukem 				curclass.checkportcmd = 0;
    152   1.9     lukem 			else
    153   1.9     lukem 				curclass.checkportcmd = 1;
    154  1.23     lukem 
    155   1.9     lukem 		} else if (strcasecmp(word, "conversion") == 0) {
    156   1.5     lukem 			char *suffix, *types, *disable, *convcmd;
    157   1.5     lukem 
    158   1.1     lukem 			if (EMPTYSTR(arg)) {
    159   1.1     lukem 				syslog(LOG_WARNING,
    160   1.1     lukem 				    "%s line %d: %s requires a suffix",
    161  1.23     lukem 				    infile, (int)line, word);
    162   1.1     lukem 				continue;	/* need a suffix */
    163   1.1     lukem 			}
    164  1.23     lukem 			NEXTWORD(p, types);
    165  1.23     lukem 			NEXTWORD(p, disable);
    166  1.23     lukem 			convcmd = p;
    167   1.1     lukem 			if (convcmd)
    168   1.1     lukem 				convcmd += strspn(convcmd, " \t");
    169  1.23     lukem 			suffix = xstrdup(arg);
    170   1.1     lukem 			if (none || EMPTYSTR(types) ||
    171   1.1     lukem 			    EMPTYSTR(disable) || EMPTYSTR(convcmd)) {
    172   1.1     lukem 				types = NULL;
    173   1.1     lukem 				disable = NULL;
    174   1.1     lukem 				convcmd = NULL;
    175   1.1     lukem 			} else {
    176  1.23     lukem 				types = xstrdup(types);
    177  1.23     lukem 				disable = xstrdup(disable);
    178  1.23     lukem 				convcmd = xstrdup(convcmd);
    179   1.1     lukem 			}
    180   1.1     lukem 			for (conv = curclass.conversions; conv != NULL;
    181   1.1     lukem 			    conv = conv->next) {
    182   1.5     lukem 				if (strcmp(conv->suffix, suffix) == 0)
    183   1.1     lukem 					break;
    184   1.1     lukem 			}
    185   1.1     lukem 			if (conv == NULL) {
    186   1.1     lukem 				conv = (struct ftpconv *)
    187   1.1     lukem 				    calloc(1, sizeof(struct ftpconv));
    188   1.1     lukem 				if (conv == NULL) {
    189   1.1     lukem 					syslog(LOG_WARNING, "can't malloc");
    190   1.1     lukem 					continue;
    191   1.1     lukem 				}
    192  1.23     lukem 				conv->next = NULL;
    193  1.23     lukem 				for (cnext = curclass.conversions;
    194  1.23     lukem 				    cnext != NULL; cnext = cnext->next)
    195  1.23     lukem 					if (cnext->next == NULL)
    196  1.23     lukem 						break;
    197  1.23     lukem 				if (cnext != NULL)
    198  1.23     lukem 					cnext->next = conv;
    199  1.23     lukem 				else
    200  1.23     lukem 					curclass.conversions = conv;
    201   1.1     lukem 			}
    202   1.5     lukem 			REASSIGN(conv->suffix, suffix);
    203   1.1     lukem 			REASSIGN(conv->types, types);
    204   1.1     lukem 			REASSIGN(conv->disable, disable);
    205   1.1     lukem 			REASSIGN(conv->command, convcmd);
    206  1.23     lukem 
    207   1.1     lukem 		} else if (strcasecmp(word, "display") == 0) {
    208   1.1     lukem 			if (none || EMPTYSTR(arg))
    209   1.1     lukem 				arg = NULL;
    210   1.1     lukem 			else
    211  1.23     lukem 				arg = xstrdup(arg);
    212   1.1     lukem 			REASSIGN(curclass.display, arg);
    213  1.23     lukem 
    214   1.1     lukem 		} else if (strcasecmp(word, "maxtimeout") == 0) {
    215   1.1     lukem 			if (none || EMPTYSTR(arg))
    216   1.1     lukem 				continue;
    217   1.1     lukem 			timeout = (unsigned int)strtoul(arg, &endp, 10);
    218   1.1     lukem 			if (*endp != 0) {
    219   1.1     lukem 				syslog(LOG_WARNING,
    220   1.1     lukem 				    "%s line %d: invalid maxtimeout %s",
    221  1.23     lukem 				    infile, (int)line, arg);
    222   1.1     lukem 				continue;
    223   1.1     lukem 			}
    224   1.1     lukem 			if (timeout < 30) {
    225   1.1     lukem 				syslog(LOG_WARNING,
    226   1.1     lukem 				    "%s line %d: maxtimeout %d < 30 seconds",
    227  1.23     lukem 				    infile, (int)line, timeout);
    228   1.1     lukem 				continue;
    229   1.1     lukem 			}
    230   1.1     lukem 			if (timeout < curclass.timeout) {
    231   1.1     lukem 				syslog(LOG_WARNING,
    232   1.1     lukem 				    "%s line %d: maxtimeout %d < timeout (%d)",
    233  1.23     lukem 				    infile, (int)line, timeout,
    234  1.23     lukem 				    curclass.timeout);
    235   1.1     lukem 				continue;
    236   1.1     lukem 			}
    237   1.1     lukem 			curclass.maxtimeout = timeout;
    238  1.23     lukem 
    239   1.1     lukem 		} else if (strcasecmp(word, "modify") == 0) {
    240   1.1     lukem 			if (none ||
    241   1.2  christos 			    (!EMPTYSTR(arg) && strcasecmp(arg, "off") == 0))
    242   1.1     lukem 				curclass.modify = 0;
    243   1.1     lukem 			else
    244   1.1     lukem 				curclass.modify = 1;
    245  1.23     lukem 
    246   1.1     lukem 		} else if (strcasecmp(word, "notify") == 0) {
    247   1.1     lukem 			if (none || EMPTYSTR(arg))
    248   1.1     lukem 				arg = NULL;
    249   1.1     lukem 			else
    250  1.23     lukem 				arg = xstrdup(arg);
    251   1.1     lukem 			REASSIGN(curclass.notify, arg);
    252  1.23     lukem 
    253  1.14        tv 		} else if (strcasecmp(word, "passive") == 0) {
    254  1.14        tv 			if (none ||
    255  1.14        tv 			    (!EMPTYSTR(arg) && strcasecmp(arg, "off") == 0))
    256  1.14        tv 				curclass.passive = 0;
    257  1.14        tv 			else
    258  1.14        tv 				curclass.passive = 1;
    259  1.23     lukem 
    260   1.1     lukem 		} else if (strcasecmp(word, "timeout") == 0) {
    261   1.1     lukem 			if (none || EMPTYSTR(arg))
    262   1.1     lukem 				continue;
    263   1.1     lukem 			timeout = (unsigned int)strtoul(arg, &endp, 10);
    264   1.1     lukem 			if (*endp != 0) {
    265   1.1     lukem 				syslog(LOG_WARNING,
    266   1.1     lukem 				    "%s line %d: invalid timeout %s",
    267  1.23     lukem 				    infile, (int)line, arg);
    268   1.1     lukem 				continue;
    269   1.1     lukem 			}
    270   1.1     lukem 			if (timeout < 30) {
    271   1.1     lukem 				syslog(LOG_WARNING,
    272   1.1     lukem 				    "%s line %d: timeout %d < 30 seconds",
    273  1.23     lukem 				    infile, (int)line, timeout);
    274   1.1     lukem 				continue;
    275   1.1     lukem 			}
    276   1.1     lukem 			if (timeout > curclass.maxtimeout) {
    277   1.1     lukem 				syslog(LOG_WARNING,
    278   1.1     lukem 				    "%s line %d: timeout %d > maxtimeout (%d)",
    279  1.23     lukem 				    infile, (int)line, timeout,
    280  1.23     lukem 				    curclass.maxtimeout);
    281   1.1     lukem 				continue;
    282   1.1     lukem 			}
    283   1.1     lukem 			curclass.timeout = timeout;
    284  1.23     lukem 
    285   1.1     lukem 		} else if (strcasecmp(word, "umask") == 0) {
    286   1.1     lukem 			mode_t umask;
    287   1.1     lukem 
    288   1.1     lukem 			if (none || EMPTYSTR(arg))
    289   1.1     lukem 				continue;
    290   1.1     lukem 			umask = (mode_t)strtoul(arg, &endp, 8);
    291   1.1     lukem 			if (*endp != 0 || umask > 0777) {
    292   1.1     lukem 				syslog(LOG_WARNING,
    293   1.1     lukem 				    "%s line %d: invalid umask %s",
    294  1.23     lukem 				    infile, (int)line, arg);
    295   1.1     lukem 				continue;
    296   1.1     lukem 			}
    297   1.1     lukem 			curclass.umask = umask;
    298  1.23     lukem 
    299   1.1     lukem 		} else {
    300   1.1     lukem 			syslog(LOG_WARNING,
    301   1.1     lukem 			    "%s line %d: unknown directive '%s'",
    302  1.23     lukem 			    infile, (int)line, word);
    303   1.1     lukem 			continue;
    304   1.1     lukem 		}
    305   1.1     lukem 	}
    306   1.1     lukem 	fclose(f);
    307   1.1     lukem }
    308   1.1     lukem 
    309   1.1     lukem /*
    310   1.1     lukem  * Show file listed in curclass.display first time in, and list all the
    311   1.1     lukem  * files named in curclass.notify in the current directory.  Send back
    312  1.17     lukem  * responses with the prefix `code' + "-".
    313   1.1     lukem  */
    314   1.1     lukem void
    315   1.1     lukem show_chdir_messages(code)
    316   1.1     lukem 	int	code;
    317   1.1     lukem {
    318   1.1     lukem 	static StringList *slist = NULL;
    319   1.1     lukem 
    320   1.1     lukem 	struct stat st;
    321   1.1     lukem 	struct tm *t;
    322   1.1     lukem 	glob_t	 gl;
    323   1.1     lukem 	time_t	 now, then;
    324   1.1     lukem 	int	 age;
    325   1.1     lukem 	char	 cwd[MAXPATHLEN + 1];
    326   1.1     lukem 	char	 line[BUFSIZ];
    327   1.1     lukem 	char	*cp, **rlist;
    328   1.1     lukem 	FILE	*f;
    329   1.1     lukem 
    330   1.1     lukem 		/* Setup list for directory cache */
    331   1.1     lukem 	if (slist == NULL)
    332   1.1     lukem 		slist = sl_init();
    333  1.22     lukem 	if (slist == NULL) {
    334  1.22     lukem 		syslog(LOG_WARNING, "can't allocate memory for stringlist");
    335  1.22     lukem 		return;
    336  1.22     lukem 	}
    337   1.1     lukem 
    338   1.1     lukem 		/* Check if this directory has already been visited */
    339   1.1     lukem 	if (getcwd(cwd, sizeof(cwd) - 1) == NULL) {
    340  1.13     mouse 		syslog(LOG_WARNING, "can't getcwd: %s", strerror(errno));
    341   1.1     lukem 		return;
    342   1.1     lukem 	}
    343   1.1     lukem 	if (sl_find(slist, cwd) != NULL)
    344   1.1     lukem 		return;
    345   1.1     lukem 
    346  1.23     lukem 	cp = xstrdup(cwd);
    347  1.22     lukem 	if (sl_add(slist, cp) == -1)
    348  1.22     lukem 		syslog(LOG_WARNING, "can't add `%s' to stringlist", cp);
    349   1.1     lukem 
    350   1.1     lukem 		/* First check for a display file */
    351   1.1     lukem 	if (curclass.display != NULL && curclass.display[0] &&
    352   1.1     lukem 	    (f = fopen(curclass.display, "r")) != NULL) {
    353  1.20     lukem 		lreply(code, "");
    354   1.1     lukem 		while (fgets(line, BUFSIZ, f)) {
    355   1.1     lukem 			if ((cp = strchr(line, '\n')) != NULL)
    356   1.1     lukem 				*cp = '\0';
    357  1.20     lukem 			lreply(0, "%s", line);
    358   1.1     lukem 		}
    359   1.1     lukem 		fclose(f);
    360   1.1     lukem 	}
    361   1.1     lukem 
    362   1.1     lukem 		/* Now see if there are any notify files */
    363   1.1     lukem 	if (curclass.notify == NULL || curclass.notify[0] == '\0')
    364   1.1     lukem 		return;
    365   1.1     lukem 
    366   1.1     lukem 	if (glob(curclass.notify, 0, NULL, &gl) != 0 || gl.gl_matchc == 0)
    367   1.1     lukem 		return;
    368   1.1     lukem 	time(&now);
    369   1.1     lukem 	for (rlist = gl.gl_pathv; *rlist != NULL; rlist++) {
    370   1.1     lukem 		if (stat(*rlist, &st) != 0)
    371   1.1     lukem 			continue;
    372   1.7   mycroft 		if (!S_ISREG(st.st_mode))
    373   1.1     lukem 			continue;
    374   1.1     lukem 		then = st.st_mtime;
    375  1.20     lukem 		if (code != 0) {
    376  1.20     lukem 			lreply(code, "");
    377  1.20     lukem 			code = 0;
    378  1.20     lukem 		}
    379   1.1     lukem 		lreply(code, "Please read the file %s", *rlist);
    380   1.1     lukem 		t = localtime(&now);
    381   1.1     lukem 		age = 365 * t->tm_year + t->tm_yday;
    382   1.1     lukem 		t = localtime(&then);
    383   1.1     lukem 		age -= 365 * t->tm_year + t->tm_yday;
    384   1.1     lukem 		lreply(code, "  it was last modified on %.24s - %d day%s ago",
    385  1.19     lukem 		    ctime(&then), age, PLURAL(age));
    386   1.1     lukem 	}
    387   1.1     lukem 	globfree(&gl);
    388   1.1     lukem }
    389   1.1     lukem 
    390   1.1     lukem /*
    391  1.23     lukem  * Find s2 at the end of s1.  If found, return a string up to (but
    392   1.1     lukem  * not including) s2, otherwise returns NULL.
    393   1.1     lukem  */
    394   1.1     lukem static char *
    395   1.1     lukem strend(s1, s2)
    396   1.2  christos 	const char *s1;
    397   1.2  christos 	char *s2;
    398   1.1     lukem {
    399   1.1     lukem 	static	char buf[MAXPATHLEN + 1];
    400   1.1     lukem 
    401   1.1     lukem 	char	*start;
    402   1.1     lukem 	size_t	l1, l2;
    403   1.1     lukem 
    404   1.1     lukem 	l1 = strlen(s1);
    405   1.1     lukem 	l2 = strlen(s2);
    406   1.1     lukem 
    407   1.1     lukem 	if (l2 >= l1)
    408   1.1     lukem 		return(NULL);
    409   1.1     lukem 
    410   1.1     lukem 	strncpy(buf, s1, MAXPATHLEN);
    411   1.1     lukem 	start = buf + (l1 - l2);
    412   1.1     lukem 
    413   1.1     lukem 	if (strcmp(start, s2) == 0) {
    414   1.1     lukem 		*start = '\0';
    415   1.1     lukem 		return(buf);
    416   1.1     lukem 	} else
    417   1.1     lukem 		return(NULL);
    418   1.1     lukem }
    419   1.1     lukem 
    420   1.1     lukem static int
    421   1.1     lukem filetypematch(types, mode)
    422   1.1     lukem 	char	*types;
    423   1.1     lukem 	int	mode;
    424   1.1     lukem {
    425   1.1     lukem 	for ( ; types[0] != '\0'; types++)
    426   1.1     lukem 		switch (*types) {
    427   1.1     lukem 		  case 'd':
    428   1.1     lukem 			if (S_ISDIR(mode))
    429   1.1     lukem 				return(1);
    430   1.1     lukem 			break;
    431   1.1     lukem 		  case 'f':
    432   1.1     lukem 			if (S_ISREG(mode))
    433   1.1     lukem 				return(1);
    434   1.1     lukem 			break;
    435   1.1     lukem 		}
    436   1.1     lukem 	return(0);
    437   1.1     lukem }
    438   1.1     lukem 
    439   1.1     lukem /*
    440   1.1     lukem  * Look for a conversion.  If we succeed, return a pointer to the
    441   1.1     lukem  * command to execute for the conversion.
    442   1.1     lukem  *
    443   1.1     lukem  * The command is stored in a static array so there's no memory
    444   1.1     lukem  * leak problems, and not too much to change in ftpd.c.  This
    445   1.1     lukem  * routine doesn't need to be re-entrant unless we start using a
    446   1.1     lukem  * multi-threaded ftpd, and that's not likely for a while...
    447   1.1     lukem  */
    448  1.23     lukem char **
    449   1.1     lukem do_conversion(fname)
    450   1.1     lukem 	const char *fname;
    451   1.1     lukem {
    452   1.1     lukem 	struct ftpconv	*cp;
    453   1.1     lukem 	struct stat	 st;
    454   1.1     lukem 	int		 o_errno;
    455   1.3  christos 	char		*base = NULL;
    456  1.23     lukem 	char		*cmd, *p, *lp, **argv;
    457  1.23     lukem 	StringList	*sl;
    458   1.1     lukem 
    459   1.1     lukem 	o_errno = errno;
    460  1.23     lukem 	sl = NULL;
    461  1.23     lukem 	cmd = NULL;
    462   1.1     lukem 	for (cp = curclass.conversions; cp != NULL; cp = cp->next) {
    463   1.5     lukem 		if (cp->suffix == NULL) {
    464   1.5     lukem 			syslog(LOG_WARNING,
    465   1.5     lukem 			    "cp->suffix==NULL in conv list; SHOULDN'T HAPPEN!");
    466   1.5     lukem 			continue;
    467   1.5     lukem 		}
    468   1.1     lukem 		if ((base = strend(fname, cp->suffix)) == NULL)
    469   1.1     lukem 			continue;
    470   1.5     lukem 		if (cp->types == NULL || cp->disable == NULL ||
    471   1.1     lukem 		    cp->command == NULL)
    472   1.1     lukem 			continue;
    473   1.1     lukem 					/* Is it enabled? */
    474   1.1     lukem 		if (strcmp(cp->disable, ".") != 0 &&
    475   1.1     lukem 		    stat(cp->disable, &st) == 0)
    476   1.1     lukem 				continue;
    477   1.1     lukem 					/* Does the base exist? */
    478   1.1     lukem 		if (stat(base, &st) < 0)
    479   1.1     lukem 			continue;
    480   1.1     lukem 					/* Is the file type ok */
    481   1.1     lukem 		if (!filetypematch(cp->types, st.st_mode))
    482   1.1     lukem 			continue;
    483   1.1     lukem 		break;			/* "We have a winner!" */
    484   1.1     lukem 	}
    485   1.1     lukem 
    486   1.1     lukem 	/* If we got through the list, no conversion */
    487  1.23     lukem 	if (cp == NULL)
    488  1.23     lukem 		goto cleanup_do_conv;
    489  1.23     lukem 
    490  1.23     lukem 	/* Split up command into an argv */
    491  1.23     lukem 	if ((sl = sl_init()) == NULL)
    492  1.23     lukem 		goto cleanup_do_conv;
    493  1.23     lukem 	cmd = xstrdup(cp->command);
    494  1.23     lukem 	p = cmd;
    495  1.23     lukem 	while (p) {
    496  1.23     lukem 		NEXTWORD(p, lp);
    497  1.23     lukem 		if (strcmp(lp, "%s") == 0)
    498  1.23     lukem 			lp = base;
    499  1.23     lukem 		if (sl_add(sl, xstrdup(lp)) == -1)
    500  1.23     lukem 			goto cleanup_do_conv;
    501   1.1     lukem 	}
    502   1.1     lukem 
    503  1.23     lukem 	if (sl_add(sl, NULL) == -1)
    504  1.23     lukem 		goto cleanup_do_conv;
    505  1.23     lukem 	argv = sl->sl_str;
    506  1.23     lukem 	free(cmd);
    507  1.23     lukem 	free(sl);
    508  1.23     lukem 	return(argv);
    509  1.23     lukem 
    510  1.23     lukem  cleanup_do_conv:
    511  1.23     lukem 	if (sl)
    512  1.23     lukem 		sl_free(sl, 1);
    513  1.23     lukem 	free(cmd);
    514  1.23     lukem 	errno = o_errno;
    515  1.23     lukem 	return(NULL);
    516   1.1     lukem }
    517