Home | History | Annotate | Line # | Download | only in ftpd
cmds.c revision 1.22
      1  1.22       agc /*	$NetBSD: cmds.c,v 1.22 2003/08/07 09:46:38 agc Exp $	*/
      2   1.1     lukem 
      3   1.1     lukem /*
      4  1.13     lukem  * Copyright (c) 1999-2001 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 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.1     lukem  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.1     lukem  * 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.1     lukem /*
     40   1.1     lukem  * Copyright (c) 1985, 1988, 1990, 1992, 1993, 1994
     41   1.1     lukem  *	The Regents of the University of California.  All rights reserved.
     42   1.1     lukem  *
     43   1.1     lukem  * Redistribution and use in source and binary forms, with or without
     44   1.1     lukem  * modification, are permitted provided that the following conditions
     45   1.1     lukem  * are met:
     46   1.1     lukem  * 1. Redistributions of source code must retain the above copyright
     47   1.1     lukem  *    notice, this list of conditions and the following disclaimer.
     48   1.1     lukem  * 2. Redistributions in binary form must reproduce the above copyright
     49   1.1     lukem  *    notice, this list of conditions and the following disclaimer in the
     50   1.1     lukem  *    documentation and/or other materials provided with the distribution.
     51  1.22       agc  * 3. Neither the name of the University nor the names of its contributors
     52   1.1     lukem  *    may be used to endorse or promote products derived from this software
     53   1.1     lukem  *    without specific prior written permission.
     54   1.1     lukem  *
     55   1.1     lukem  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     56   1.1     lukem  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     57   1.1     lukem  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     58   1.1     lukem  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     59   1.1     lukem  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     60   1.1     lukem  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     61   1.1     lukem  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     62   1.1     lukem  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     63   1.1     lukem  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     64   1.1     lukem  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     65   1.1     lukem  * SUCH DAMAGE.
     66   1.1     lukem  */
     67   1.1     lukem 
     68   1.1     lukem /*
     69   1.1     lukem  * Copyright (C) 1997 and 1998 WIDE Project.
     70   1.1     lukem  * All rights reserved.
     71   1.1     lukem  *
     72   1.1     lukem  * Redistribution and use in source and binary forms, with or without
     73   1.1     lukem  * modification, are permitted provided that the following conditions
     74   1.1     lukem  * are met:
     75   1.1     lukem  * 1. Redistributions of source code must retain the above copyright
     76   1.1     lukem  *    notice, this list of conditions and the following disclaimer.
     77   1.1     lukem  * 2. Redistributions in binary form must reproduce the above copyright
     78   1.1     lukem  *    notice, this list of conditions and the following disclaimer in the
     79   1.1     lukem  *    documentation and/or other materials provided with the distribution.
     80   1.1     lukem  * 3. Neither the name of the project nor the names of its contributors
     81   1.1     lukem  *    may be used to endorse or promote products derived from this software
     82   1.1     lukem  *    without specific prior written permission.
     83   1.1     lukem  *
     84   1.1     lukem  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     85   1.1     lukem  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     86   1.1     lukem  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     87   1.1     lukem  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     88   1.1     lukem  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     89   1.1     lukem  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     90   1.1     lukem  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     91   1.1     lukem  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     92   1.1     lukem  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     93   1.1     lukem  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     94   1.1     lukem  * SUCH DAMAGE.
     95   1.1     lukem  */
     96   1.1     lukem 
     97   1.1     lukem 
     98   1.1     lukem #include <sys/cdefs.h>
     99   1.1     lukem #ifndef lint
    100  1.22       agc __RCSID("$NetBSD: cmds.c,v 1.22 2003/08/07 09:46:38 agc Exp $");
    101   1.1     lukem #endif /* not lint */
    102   1.1     lukem 
    103   1.1     lukem #include <sys/param.h>
    104   1.1     lukem #include <sys/stat.h>
    105   1.1     lukem 
    106   1.1     lukem #include <arpa/ftp.h>
    107   1.1     lukem 
    108   1.1     lukem #include <dirent.h>
    109   1.1     lukem #include <errno.h>
    110   1.1     lukem #include <setjmp.h>
    111   1.1     lukem #include <stdio.h>
    112   1.1     lukem #include <stdlib.h>
    113   1.1     lukem #include <string.h>
    114   1.1     lukem #include <tzfile.h>
    115   1.1     lukem #include <unistd.h>
    116  1.19    itojun #include <ctype.h>
    117   1.2  explorer 
    118   1.2  explorer #ifdef KERBEROS5
    119   1.2  explorer #include <krb5/krb5.h>
    120   1.2  explorer #endif
    121   1.1     lukem 
    122   1.1     lukem #include "extern.h"
    123   1.1     lukem 
    124  1.16     lukem typedef enum {
    125  1.16     lukem 	FE_MLSD		= 1<<0,		/* if op is MLSD (MLST otherwise ) */
    126  1.16     lukem 	FE_ISCURDIR	= 1<<1,		/* if name is the current directory */
    127  1.16     lukem } factflag_t;
    128  1.16     lukem 
    129   1.1     lukem typedef struct {
    130   1.1     lukem 	const char	*path;		/* full pathname */
    131   1.1     lukem 	const char	*display;	/* name to display */
    132   1.1     lukem 	struct stat	*stat;		/* stat of path */
    133   1.1     lukem 	struct stat	*pdirstat;	/* stat of path's parent dir */
    134  1.16     lukem 	factflag_t	 flags;		/* flags */
    135   1.1     lukem } factelem;
    136   1.1     lukem 
    137   1.1     lukem static void	ack(const char *);
    138   1.3     lukem static void	base64_encode(const char *, size_t, char *, int);
    139   1.1     lukem static void	fact_type(const char *, FILE *, factelem *);
    140   1.1     lukem static void	fact_size(const char *, FILE *, factelem *);
    141   1.1     lukem static void	fact_modify(const char *, FILE *, factelem *);
    142   1.1     lukem static void	fact_perm(const char *, FILE *, factelem *);
    143   1.1     lukem static void	fact_unique(const char *, FILE *, factelem *);
    144   1.3     lukem static int	matchgroup(gid_t);
    145   1.1     lukem static void	mlsname(FILE *, factelem *);
    146   1.1     lukem static void	replydirname(const char *, const char *);
    147   1.1     lukem 
    148   1.1     lukem struct ftpfact {
    149   1.1     lukem 	const char	 *name;		/* name of fact */
    150   1.1     lukem 	int		  enabled;	/* if fact is enabled */
    151   1.1     lukem 	void		(*display)(const char *, FILE *, factelem *);
    152   1.1     lukem 					/* function to display fact */
    153   1.1     lukem };
    154   1.1     lukem 
    155   1.1     lukem struct ftpfact facttab[] = {
    156   1.1     lukem 	{ "Type",	1, fact_type },
    157   1.3     lukem #define	FACT_TYPE 0
    158   1.1     lukem 	{ "Size",	1, fact_size },
    159   1.1     lukem 	{ "Modify",	1, fact_modify },
    160   1.1     lukem 	{ "Perm",	1, fact_perm },
    161   1.1     lukem 	{ "Unique",	1, fact_unique },
    162   1.1     lukem 	/* "Create" */
    163   1.1     lukem 	/* "Lang" */
    164   1.1     lukem 	/* "Media-Type" */
    165   1.1     lukem 	/* "CharSet" */
    166   1.1     lukem };
    167   1.1     lukem 
    168   1.4     lukem #define FACTTABSIZE	(sizeof(facttab) / sizeof(struct ftpfact))
    169   1.4     lukem 
    170  1.20      manu static char cached_path[MAXPATHLEN + 1] = "/";
    171  1.20      manu static void discover_path(char *, const char *);
    172   1.1     lukem 
    173   1.1     lukem void
    174   1.1     lukem cwd(const char *path)
    175   1.1     lukem {
    176   1.1     lukem 
    177   1.1     lukem 	if (chdir(path) < 0)
    178   1.1     lukem 		perror_reply(550, path);
    179   1.1     lukem 	else {
    180   1.1     lukem 		show_chdir_messages(250);
    181   1.1     lukem 		ack("CWD");
    182  1.20      manu 		if (getcwd(cached_path, MAXPATHLEN) == NULL) {
    183  1.20      manu 			discover_path(cached_path, path);
    184  1.20      manu 		}
    185   1.1     lukem 	}
    186   1.1     lukem }
    187   1.1     lukem 
    188   1.1     lukem void
    189   1.1     lukem delete(const char *name)
    190   1.1     lukem {
    191   1.1     lukem 	char *p = NULL;
    192   1.1     lukem 
    193   1.1     lukem 	if (remove(name) < 0) {
    194   1.1     lukem 		p = strerror(errno);
    195   1.1     lukem 		perror_reply(550, name);
    196   1.1     lukem 	} else
    197   1.1     lukem 		ack("DELE");
    198  1.10     lukem 	logxfer("delete", -1, name, NULL, NULL, p);
    199   1.1     lukem }
    200   1.1     lukem 
    201   1.1     lukem void
    202   1.1     lukem feat(void)
    203   1.1     lukem {
    204   1.1     lukem 	int i;
    205   1.1     lukem 
    206   1.3     lukem 	reply(-211, "Features supported");
    207   1.3     lukem 	cprintf(stdout, " MDTM\r\n");
    208   1.3     lukem 	cprintf(stdout, " MLST ");
    209   1.4     lukem 	for (i = 0; i < FACTTABSIZE; i++)
    210   1.3     lukem 		cprintf(stdout, "%s%s;", facttab[i].name,
    211   1.1     lukem 		    facttab[i].enabled ? "*" : "");
    212   1.3     lukem 	cprintf(stdout, "\r\n");
    213   1.3     lukem 	cprintf(stdout, " REST STREAM\r\n");
    214   1.3     lukem 	cprintf(stdout, " SIZE\r\n");
    215   1.3     lukem 	cprintf(stdout, " TVFS\r\n");
    216   1.1     lukem 	reply(211,  "End");
    217   1.1     lukem }
    218   1.1     lukem 
    219   1.1     lukem void
    220   1.1     lukem makedir(const char *name)
    221   1.1     lukem {
    222   1.1     lukem 	char *p = NULL;
    223   1.1     lukem 
    224   1.1     lukem 	if (mkdir(name, 0777) < 0) {
    225   1.1     lukem 		p = strerror(errno);
    226   1.1     lukem 		perror_reply(550, name);
    227   1.1     lukem 	} else
    228   1.1     lukem 		replydirname(name, "directory created.");
    229  1.10     lukem 	logxfer("mkdir", -1, name, NULL, NULL, p);
    230   1.1     lukem }
    231   1.1     lukem 
    232   1.1     lukem void
    233   1.1     lukem mlsd(const char *path)
    234   1.1     lukem {
    235   1.3     lukem 	struct dirent	*dp;
    236   1.3     lukem 	struct stat	 sb, pdirstat;
    237   1.1     lukem 	factelem f;
    238   1.3     lukem 	FILE	*dout;
    239   1.3     lukem 	DIR	*dirp;
    240   1.3     lukem 	char	name[MAXPATHLEN];
    241   1.3     lukem 	int	hastypefact;
    242   1.1     lukem 
    243   1.3     lukem 	hastypefact = facttab[FACT_TYPE].enabled;
    244   1.1     lukem 	if (path == NULL)
    245   1.1     lukem 		path = ".";
    246   1.1     lukem 	if (stat(path, &pdirstat) == -1) {
    247   1.1     lukem  mlsdperror:
    248   1.1     lukem 		perror_reply(550, path);
    249   1.1     lukem 		return;
    250   1.1     lukem 	}
    251   1.1     lukem 	if (! S_ISDIR(pdirstat.st_mode)) {
    252   1.1     lukem 		errno = ENOTDIR;
    253   1.3     lukem 		perror_reply(501, path);
    254   1.3     lukem 		return;
    255   1.1     lukem 	}
    256  1.15     lukem 	if ((dirp = opendir(path)) == NULL)
    257  1.15     lukem 		goto mlsdperror;
    258  1.15     lukem 
    259   1.1     lukem 	dout = dataconn("MLSD", (off_t)-1, "w");
    260   1.1     lukem 	if (dout == NULL)
    261   1.1     lukem 		return;
    262   1.1     lukem 
    263  1.16     lukem 	memset(&f, 0, sizeof(f));
    264   1.1     lukem 	f.stat = &sb;
    265  1.16     lukem 	f.flags |= FE_MLSD;
    266   1.1     lukem 	while ((dp = readdir(dirp)) != NULL) {
    267   1.1     lukem 		snprintf(name, sizeof(name), "%s/%s", path, dp->d_name);
    268   1.1     lukem 		if (ISDOTDIR(dp->d_name)) {	/* special case curdir: */
    269   1.3     lukem 			if (! hastypefact)
    270   1.3     lukem 				continue;
    271   1.3     lukem 			f.pdirstat = NULL;	/*   require stat of parent */
    272   1.1     lukem 			f.display = path;	/*   set name to real name */
    273  1.16     lukem 			f.flags |= FE_ISCURDIR; /*   flag name is curdir */
    274   1.1     lukem 		} else {
    275   1.3     lukem 			if (ISDOTDOTDIR(dp->d_name)) {
    276   1.3     lukem 				if (! hastypefact)
    277   1.3     lukem 					continue;
    278   1.3     lukem 				f.pdirstat = NULL;
    279   1.3     lukem 			} else
    280   1.3     lukem 				f.pdirstat = &pdirstat;	/* cache parent stat */
    281   1.1     lukem 			f.display = dp->d_name;
    282  1.16     lukem 			f.flags &= ~FE_ISCURDIR;
    283   1.1     lukem 		}
    284   1.3     lukem 		if (stat(name, &sb) == -1)
    285   1.3     lukem 			continue;
    286   1.3     lukem 		f.path = name;
    287   1.1     lukem 		mlsname(dout, &f);
    288   1.1     lukem 	}
    289   1.1     lukem 	(void)closedir(dirp);
    290   1.1     lukem 
    291   1.1     lukem 	if (ferror(dout) != 0)
    292   1.1     lukem 		perror_reply(550, "Data connection");
    293   1.1     lukem 	else
    294   1.1     lukem 		reply(226, "MLSD complete.");
    295   1.3     lukem 	closedataconn(dout);
    296   1.1     lukem 	total_xfers_out++;
    297   1.1     lukem 	total_xfers++;
    298   1.1     lukem }
    299   1.1     lukem 
    300   1.1     lukem void
    301   1.1     lukem mlst(const char *path)
    302   1.1     lukem {
    303   1.1     lukem 	struct stat sb;
    304   1.1     lukem 	factelem f;
    305   1.1     lukem 
    306   1.1     lukem 	if (path == NULL)
    307   1.1     lukem 		path = ".";
    308   1.1     lukem 	if (stat(path, &sb) == -1) {
    309   1.1     lukem 		perror_reply(550, path);
    310   1.1     lukem 		return;
    311   1.1     lukem 	}
    312   1.3     lukem 	reply(-250, "MLST %s", path);
    313  1.16     lukem 	memset(&f, 0, sizeof(f));
    314   1.1     lukem 	f.path = path;
    315   1.1     lukem 	f.display = path;
    316   1.1     lukem 	f.stat = &sb;
    317   1.1     lukem 	f.pdirstat = NULL;
    318   1.3     lukem 	CPUTC(' ', stdout);
    319   1.1     lukem 	mlsname(stdout, &f);
    320   1.1     lukem 	reply(250, "End");
    321   1.1     lukem }
    322   1.1     lukem 
    323   1.1     lukem 
    324   1.1     lukem void
    325   1.1     lukem opts(const char *command)
    326   1.1     lukem {
    327   1.1     lukem 	struct tab *c;
    328   1.1     lukem 	char *ep;
    329   1.1     lukem 
    330   1.1     lukem 	if ((ep = strchr(command, ' ')) != NULL)
    331   1.1     lukem 		*ep++ = '\0';
    332   1.1     lukem 	c = lookup(cmdtab, command);
    333   1.1     lukem 	if (c == NULL) {
    334  1.18   darrenr 		reply(502, "Unknown command '%s'.", command);
    335   1.1     lukem 		return;
    336   1.1     lukem 	}
    337   1.1     lukem 	if (! CMD_IMPLEMENTED(c)) {
    338  1.17   darrenr 		reply(502, "%s command not implemented.", c->name);
    339   1.1     lukem 		return;
    340   1.1     lukem 	}
    341   1.1     lukem 	if (! CMD_HAS_OPTIONS(c)) {
    342   1.1     lukem 		reply(501, "%s command does not support persistent options.",
    343   1.1     lukem 		    c->name);
    344   1.1     lukem 		return;
    345   1.1     lukem 	}
    346   1.1     lukem 
    347   1.1     lukem 			/* special case: MLST */
    348   1.1     lukem 	if (strcasecmp(command, "MLST") == 0) {
    349   1.4     lukem 		int	 enabled[FACTTABSIZE];
    350   1.1     lukem 		int	 i, onedone;
    351   1.3     lukem 		size_t	 len;
    352   1.1     lukem 		char	*p;
    353   1.1     lukem 
    354   1.3     lukem 		for (i = 0; i < sizeof(enabled) / sizeof(int); i++)
    355   1.3     lukem 			enabled[i] = 0;
    356   1.1     lukem 		if (ep == NULL || *ep == '\0')
    357   1.1     lukem 			goto displaymlstopts;
    358   1.1     lukem 
    359   1.1     lukem 				/* don't like spaces, and need trailing ; */
    360   1.3     lukem 		len = strlen(ep);
    361   1.3     lukem 		if (strchr(ep, ' ') != NULL || ep[len - 1] != ';') {
    362   1.3     lukem  badmlstopt:
    363   1.1     lukem 			reply(501, "Invalid MLST options");
    364   1.1     lukem 			return;
    365   1.1     lukem 		}
    366   1.3     lukem 		ep[len - 1] = '\0';
    367   1.1     lukem 		while ((p = strsep(&ep, ";")) != NULL) {
    368   1.3     lukem 			if (*p == '\0')
    369   1.3     lukem 				goto badmlstopt;
    370   1.4     lukem 			for (i = 0; i < FACTTABSIZE; i++)
    371   1.1     lukem 				if (strcasecmp(p, facttab[i].name) == 0) {
    372   1.3     lukem 					enabled[i] = 1;
    373   1.1     lukem 					break;
    374   1.1     lukem 				}
    375   1.1     lukem 		}
    376   1.1     lukem 
    377   1.1     lukem  displaymlstopts:
    378   1.4     lukem 		for (i = 0; i < FACTTABSIZE; i++)
    379   1.3     lukem 			facttab[i].enabled = enabled[i];
    380   1.3     lukem 		cprintf(stdout, "200 MLST OPTS");
    381   1.4     lukem 		for (i = onedone = 0; i < FACTTABSIZE; i++) {
    382   1.1     lukem 			if (facttab[i].enabled) {
    383   1.3     lukem 				cprintf(stdout, "%s%s;", onedone ? "" : " ",
    384   1.1     lukem 				    facttab[i].name);
    385   1.1     lukem 				onedone++;
    386   1.1     lukem 			}
    387   1.1     lukem 		}
    388   1.3     lukem 		cprintf(stdout, "\r\n");
    389   1.3     lukem 		fflush(stdout);
    390   1.1     lukem 		return;
    391   1.1     lukem 	}
    392   1.1     lukem 
    393   1.1     lukem 			/* default cases */
    394   1.1     lukem 	if (ep != NULL && *ep != '\0')
    395   1.1     lukem 		REASSIGN(c->options, xstrdup(ep));
    396   1.1     lukem 	if (c->options != NULL)
    397   1.1     lukem 		reply(200, "Options for %s are '%s'.", c->name,
    398   1.1     lukem 		    c->options);
    399   1.1     lukem 	else
    400   1.1     lukem 		reply(200, "No options defined for %s.", c->name);
    401   1.1     lukem }
    402   1.1     lukem 
    403   1.1     lukem void
    404   1.1     lukem pwd(void)
    405   1.1     lukem {
    406   1.1     lukem 	char path[MAXPATHLEN];
    407   1.1     lukem 
    408  1.20      manu 	if (getcwd(path, sizeof(path) - 1) == NULL) {
    409  1.20      manu 		if (chdir(cached_path) < 0) {
    410  1.20      manu 			reply(550, "Can't get the current directory: %s.",
    411  1.20      manu 			    strerror(errno));
    412  1.20      manu 			return;
    413  1.20      manu 		}
    414  1.20      manu 		(void)strlcpy(path, cached_path, MAXPATHLEN);
    415  1.20      manu 	}
    416  1.20      manu 	replydirname(path, "is the current directory.");
    417   1.1     lukem }
    418   1.1     lukem 
    419   1.1     lukem void
    420   1.1     lukem removedir(const char *name)
    421   1.1     lukem {
    422   1.1     lukem 	char *p = NULL;
    423   1.1     lukem 
    424   1.1     lukem 	if (rmdir(name) < 0) {
    425   1.1     lukem 		p = strerror(errno);
    426   1.1     lukem 		perror_reply(550, name);
    427   1.1     lukem 	} else
    428   1.1     lukem 		ack("RMD");
    429  1.10     lukem 	logxfer("rmdir", -1, name, NULL, NULL, p);
    430   1.1     lukem }
    431   1.1     lukem 
    432   1.1     lukem char *
    433   1.1     lukem renamefrom(const char *name)
    434   1.1     lukem {
    435   1.1     lukem 	struct stat st;
    436   1.1     lukem 
    437   1.1     lukem 	if (stat(name, &st) < 0) {
    438   1.1     lukem 		perror_reply(550, name);
    439   1.1     lukem 		return (NULL);
    440   1.1     lukem 	}
    441   1.1     lukem 	reply(350, "File exists, ready for destination name");
    442   1.1     lukem 	return (xstrdup(name));
    443   1.1     lukem }
    444   1.1     lukem 
    445   1.1     lukem void
    446   1.1     lukem renamecmd(const char *from, const char *to)
    447   1.1     lukem {
    448   1.1     lukem 	char *p = NULL;
    449   1.1     lukem 
    450   1.1     lukem 	if (rename(from, to) < 0) {
    451   1.1     lukem 		p = strerror(errno);
    452   1.1     lukem 		perror_reply(550, "rename");
    453   1.1     lukem 	} else
    454   1.1     lukem 		ack("RNTO");
    455  1.10     lukem 	logxfer("rename", -1, from, to, NULL, p);
    456   1.1     lukem }
    457   1.1     lukem 
    458   1.1     lukem void
    459   1.1     lukem sizecmd(const char *filename)
    460   1.1     lukem {
    461   1.1     lukem 	switch (type) {
    462   1.1     lukem 	case TYPE_L:
    463  1.10     lukem 	case TYPE_I:
    464  1.10     lukem 	    {
    465   1.1     lukem 		struct stat stbuf;
    466   1.1     lukem 		if (stat(filename, &stbuf) < 0 || !S_ISREG(stbuf.st_mode))
    467   1.1     lukem 			reply(550, "%s: not a plain file.", filename);
    468   1.1     lukem 		else
    469   1.7     lukem 			reply(213, ULLF, (ULLT)stbuf.st_size);
    470  1.10     lukem 		break;
    471  1.10     lukem 	    }
    472  1.10     lukem 	case TYPE_A:
    473  1.10     lukem 	    {
    474   1.1     lukem 		FILE *fin;
    475   1.1     lukem 		int c;
    476   1.1     lukem 		off_t count;
    477   1.1     lukem 		struct stat stbuf;
    478   1.1     lukem 		fin = fopen(filename, "r");
    479   1.1     lukem 		if (fin == NULL) {
    480   1.1     lukem 			perror_reply(550, filename);
    481   1.1     lukem 			return;
    482   1.1     lukem 		}
    483   1.1     lukem 		if (fstat(fileno(fin), &stbuf) < 0 || !S_ISREG(stbuf.st_mode)) {
    484   1.1     lukem 			reply(550, "%s: not a plain file.", filename);
    485   1.1     lukem 			(void) fclose(fin);
    486   1.1     lukem 			return;
    487   1.1     lukem 		}
    488  1.14    itojun 		if (stbuf.st_size > 10240) {
    489  1.14    itojun 			reply(550, "%s: file too large for SIZE.", filename);
    490  1.14    itojun 			(void) fclose(fin);
    491  1.14    itojun 			return;
    492  1.14    itojun 		}
    493   1.1     lukem 
    494   1.1     lukem 		count = 0;
    495  1.14    itojun 		while((c = getc(fin)) != EOF) {
    496   1.1     lukem 			if (c == '\n')	/* will get expanded to \r\n */
    497   1.1     lukem 				count++;
    498   1.1     lukem 			count++;
    499   1.1     lukem 		}
    500   1.1     lukem 		(void) fclose(fin);
    501   1.1     lukem 
    502   1.7     lukem 		reply(213, LLF, (LLT)count);
    503  1.10     lukem 		break;
    504  1.10     lukem 	    }
    505   1.1     lukem 	default:
    506   1.1     lukem 		reply(504, "SIZE not implemented for Type %c.", "?AEIL"[type]);
    507   1.1     lukem 	}
    508   1.1     lukem }
    509   1.1     lukem 
    510   1.1     lukem void
    511   1.1     lukem statfilecmd(const char *filename)
    512   1.1     lukem {
    513   1.1     lukem 	FILE *fin;
    514   1.1     lukem 	int c;
    515  1.19    itojun 	int atstart;
    516   1.1     lukem 	char *argv[] = { INTERNAL_LS, "-lgA", "", NULL };
    517   1.1     lukem 
    518   1.1     lukem 	argv[2] = (char *)filename;
    519   1.1     lukem 	fin = ftpd_popen(argv, "r", STDOUT_FILENO);
    520   1.3     lukem 	reply(-211, "status of %s:", filename);
    521   1.3     lukem /* XXX: use fgetln() or fparseln() here? */
    522  1.19    itojun 	atstart = 1;
    523   1.1     lukem 	while ((c = getc(fin)) != EOF) {
    524   1.1     lukem 		if (c == '\n') {
    525   1.1     lukem 			if (ferror(stdout)){
    526   1.1     lukem 				perror_reply(421, "control connection");
    527   1.1     lukem 				(void) ftpd_pclose(fin);
    528   1.1     lukem 				dologout(1);
    529   1.1     lukem 				/* NOTREACHED */
    530   1.1     lukem 			}
    531   1.1     lukem 			if (ferror(fin)) {
    532   1.1     lukem 				perror_reply(551, filename);
    533   1.1     lukem 				(void) ftpd_pclose(fin);
    534   1.1     lukem 				return;
    535   1.1     lukem 			}
    536   1.3     lukem 			CPUTC('\r', stdout);
    537   1.1     lukem 		}
    538  1.19    itojun 		if (atstart && isdigit(c))
    539  1.19    itojun 			CPUTC(' ', stdout);
    540   1.3     lukem 		CPUTC(c, stdout);
    541  1.19    itojun 		atstart = (c == '\n');
    542   1.1     lukem 	}
    543   1.1     lukem 	(void) ftpd_pclose(fin);
    544   1.1     lukem 	reply(211, "End of Status");
    545   1.1     lukem }
    546   1.1     lukem 
    547   1.1     lukem /* -- */
    548   1.1     lukem 
    549   1.1     lukem static void
    550   1.1     lukem ack(const char *s)
    551   1.1     lukem {
    552   1.1     lukem 
    553   1.1     lukem 	reply(250, "%s command successful.", s);
    554   1.1     lukem }
    555   1.1     lukem 
    556   1.3     lukem /*
    557   1.3     lukem  * Encode len bytes starting at clear using base64 encoding into encoded,
    558   1.3     lukem  * which should be at least ((len + 2) * 4 / 3 + 1) in size.
    559   1.6     lukem  * If nulterm is non-zero, terminate with \0 otherwise pad to 3 byte boundary
    560   1.6     lukem  * with `='.
    561   1.3     lukem  */
    562   1.3     lukem static void
    563   1.3     lukem base64_encode(const char *clear, size_t len, char *encoded, int nulterm)
    564   1.3     lukem {
    565   1.5     lukem 	static const char base64[] =
    566   1.3     lukem 	    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    567   1.5     lukem 	const char *c;
    568   1.5     lukem 	char	*e, termchar;
    569   1.3     lukem 	int	 i;
    570   1.3     lukem 
    571   1.6     lukem 			/* determine whether to pad with '=' or NUL terminate */
    572   1.5     lukem 	termchar = nulterm ? '\0' : '=';
    573   1.5     lukem 	c = clear;
    574   1.5     lukem 	e = encoded;
    575   1.6     lukem 			/* convert all but last 2 bytes */
    576   1.6     lukem 	for (i = len; i > 2; i -= 3, c += 3) {
    577   1.6     lukem 		*e++ = base64[(c[0] >> 2) & 0x3f];
    578   1.6     lukem 		*e++ = base64[((c[0] << 4) & 0x30) | ((c[1] >> 4) & 0x0f)];
    579   1.6     lukem 		*e++ = base64[((c[1] << 2) & 0x3c) | ((c[2] >> 6) & 0x03)];
    580   1.6     lukem 		*e++ = base64[(c[2]) & 0x3f];
    581   1.6     lukem 	}
    582   1.6     lukem 			/* handle slop at end */
    583   1.6     lukem 	if (i > 0) {
    584   1.6     lukem 		*e++ = base64[(c[0] >> 2) & 0x3f];
    585   1.5     lukem 		*e++ = base64[((c[0] << 4) & 0x30) |
    586   1.6     lukem 		     (i > 1 ? ((c[1] >> 4) & 0x0f) : 0)];
    587   1.6     lukem 		*e++ = (i > 1) ? base64[(c[1] << 2) & 0x3c] : termchar;
    588   1.6     lukem 		*e++ = termchar;
    589   1.5     lukem 	}
    590   1.5     lukem 	*e = '\0';
    591   1.3     lukem }
    592   1.3     lukem 
    593   1.1     lukem static void
    594   1.1     lukem fact_modify(const char *fact, FILE *fd, factelem *fe)
    595   1.1     lukem {
    596   1.1     lukem 	struct tm *t;
    597   1.1     lukem 
    598   1.1     lukem 	t = gmtime(&(fe->stat->st_mtime));
    599   1.3     lukem 	cprintf(fd, "%s=%04d%02d%02d%02d%02d%02d;", fact,
    600   1.1     lukem 	    TM_YEAR_BASE + t->tm_year,
    601   1.1     lukem 	    t->tm_mon+1, t->tm_mday,
    602   1.1     lukem 	    t->tm_hour, t->tm_min, t->tm_sec);
    603   1.1     lukem }
    604   1.1     lukem 
    605   1.1     lukem static void
    606   1.1     lukem fact_perm(const char *fact, FILE *fd, factelem *fe)
    607   1.1     lukem {
    608   1.3     lukem 	int		rok, wok, xok, pdirwok;
    609   1.1     lukem 	struct stat	*pdir;
    610   1.1     lukem 
    611   1.1     lukem 	if (fe->stat->st_uid == geteuid()) {
    612   1.1     lukem 		rok = ((fe->stat->st_mode & S_IRUSR) != 0);
    613   1.1     lukem 		wok = ((fe->stat->st_mode & S_IWUSR) != 0);
    614   1.1     lukem 		xok = ((fe->stat->st_mode & S_IXUSR) != 0);
    615   1.3     lukem 	} else if (matchgroup(fe->stat->st_gid)) {
    616   1.1     lukem 		rok = ((fe->stat->st_mode & S_IRGRP) != 0);
    617   1.1     lukem 		wok = ((fe->stat->st_mode & S_IWGRP) != 0);
    618   1.1     lukem 		xok = ((fe->stat->st_mode & S_IXGRP) != 0);
    619   1.1     lukem 	} else {
    620   1.1     lukem 		rok = ((fe->stat->st_mode & S_IROTH) != 0);
    621   1.1     lukem 		wok = ((fe->stat->st_mode & S_IWOTH) != 0);
    622   1.1     lukem 		xok = ((fe->stat->st_mode & S_IXOTH) != 0);
    623   1.1     lukem 	}
    624   1.1     lukem 
    625   1.3     lukem 	cprintf(fd, "%s=", fact);
    626   1.1     lukem 
    627   1.1     lukem 			/*
    628   1.1     lukem 			 * if parent info not provided, look it up, but
    629   1.1     lukem 			 * only if the current class has modify rights,
    630   1.1     lukem 			 * since we only need this info in such a case.
    631   1.1     lukem 			 */
    632   1.1     lukem 	pdir = fe->pdirstat;
    633   1.8     lukem 	if (pdir == NULL && CURCLASS_FLAGS_ISSET(modify)) {
    634   1.1     lukem 		size_t		len;
    635   1.1     lukem 		char		realdir[MAXPATHLEN], *p;
    636   1.1     lukem 		struct stat	dir;
    637   1.1     lukem 
    638   1.1     lukem 		len = strlcpy(realdir, fe->path, sizeof(realdir));
    639   1.1     lukem 		if (len < sizeof(realdir) - 4) {
    640   1.1     lukem 			if (S_ISDIR(fe->stat->st_mode))
    641   1.1     lukem 				strlcat(realdir, "/..", sizeof(realdir));
    642   1.1     lukem 			else {
    643   1.1     lukem 					/* if has a /, move back to it */
    644   1.1     lukem 					/* otherwise use '..' */
    645   1.1     lukem 				if ((p = strrchr(realdir, '/')) != NULL) {
    646   1.1     lukem 					if (p == realdir)
    647   1.1     lukem 						p++;
    648   1.1     lukem 					*p = '\0';
    649   1.1     lukem 				} else
    650   1.1     lukem 					strlcpy(realdir, "..", sizeof(realdir));
    651   1.1     lukem 			}
    652   1.1     lukem 			if (stat(realdir, &dir) == 0)
    653   1.1     lukem 				pdir = &dir;
    654   1.1     lukem 		}
    655   1.1     lukem 	}
    656   1.1     lukem 	pdirwok = 0;
    657   1.1     lukem 	if (pdir != NULL) {
    658   1.1     lukem 		if (pdir->st_uid == geteuid())
    659   1.1     lukem 			pdirwok = ((pdir->st_mode & S_IWUSR) != 0);
    660   1.3     lukem 		else if (matchgroup(pdir->st_gid))
    661   1.1     lukem 			pdirwok = ((pdir->st_mode & S_IWGRP) != 0);
    662   1.1     lukem 		else
    663   1.1     lukem 			pdirwok = ((pdir->st_mode & S_IWOTH) != 0);
    664   1.1     lukem 	}
    665   1.1     lukem 
    666   1.1     lukem 			/* 'a': can APPE to file */
    667   1.8     lukem 	if (wok && CURCLASS_FLAGS_ISSET(upload) && S_ISREG(fe->stat->st_mode))
    668   1.3     lukem 		CPUTC('a', fd);
    669   1.1     lukem 
    670   1.1     lukem 			/* 'c': can create or append to files in directory */
    671   1.8     lukem 	if (wok && CURCLASS_FLAGS_ISSET(modify) && S_ISDIR(fe->stat->st_mode))
    672   1.3     lukem 		CPUTC('c', fd);
    673   1.1     lukem 
    674   1.1     lukem 			/* 'd': can delete file or directory */
    675   1.8     lukem 	if (pdirwok && CURCLASS_FLAGS_ISSET(modify)) {
    676   1.1     lukem 		int candel;
    677   1.1     lukem 
    678   1.1     lukem 		candel = 1;
    679   1.1     lukem 		if (S_ISDIR(fe->stat->st_mode)) {
    680   1.1     lukem 			DIR *dirp;
    681   1.1     lukem 			struct dirent *dp;
    682   1.1     lukem 
    683   1.1     lukem 			if ((dirp = opendir(fe->display)) == NULL)
    684   1.1     lukem 				candel = 0;
    685   1.1     lukem 			else {
    686   1.1     lukem 				while ((dp = readdir(dirp)) != NULL) {
    687   1.1     lukem 					if (ISDOTDIR(dp->d_name) ||
    688   1.1     lukem 					    ISDOTDOTDIR(dp->d_name))
    689   1.1     lukem 						continue;
    690   1.1     lukem 					candel = 0;
    691   1.1     lukem 					break;
    692   1.1     lukem 				}
    693   1.1     lukem 				closedir(dirp);
    694   1.1     lukem 			}
    695   1.1     lukem 		}
    696   1.1     lukem 		if (candel)
    697   1.3     lukem 			CPUTC('d', fd);
    698   1.1     lukem 	}
    699   1.1     lukem 
    700   1.1     lukem 			/* 'e': can enter directory */
    701   1.1     lukem 	if (xok && S_ISDIR(fe->stat->st_mode))
    702   1.3     lukem 		CPUTC('e', fd);
    703   1.1     lukem 
    704   1.1     lukem 			/* 'f': can rename file or directory */
    705   1.8     lukem 	if (pdirwok && CURCLASS_FLAGS_ISSET(modify))
    706   1.3     lukem 		CPUTC('f', fd);
    707   1.1     lukem 
    708   1.1     lukem 			/* 'l': can list directory */
    709   1.1     lukem 	if (rok && xok && S_ISDIR(fe->stat->st_mode))
    710   1.3     lukem 		CPUTC('l', fd);
    711   1.1     lukem 
    712   1.1     lukem 			/* 'm': can create directory */
    713   1.8     lukem 	if (wok && CURCLASS_FLAGS_ISSET(modify) && S_ISDIR(fe->stat->st_mode))
    714   1.3     lukem 		CPUTC('m', fd);
    715   1.1     lukem 
    716   1.1     lukem 			/* 'p': can remove files in directory */
    717   1.8     lukem 	if (wok && CURCLASS_FLAGS_ISSET(modify) && S_ISDIR(fe->stat->st_mode))
    718   1.3     lukem 		CPUTC('p', fd);
    719   1.1     lukem 
    720   1.1     lukem 			/* 'r': can RETR file */
    721   1.1     lukem 	if (rok && S_ISREG(fe->stat->st_mode))
    722   1.3     lukem 		CPUTC('r', fd);
    723   1.1     lukem 
    724   1.1     lukem 			/* 'w': can STOR file */
    725   1.8     lukem 	if (wok && CURCLASS_FLAGS_ISSET(upload) && S_ISREG(fe->stat->st_mode))
    726   1.3     lukem 		CPUTC('w', fd);
    727   1.1     lukem 
    728   1.3     lukem 	CPUTC(';', fd);
    729   1.1     lukem }
    730   1.1     lukem 
    731   1.1     lukem static void
    732   1.1     lukem fact_size(const char *fact, FILE *fd, factelem *fe)
    733   1.1     lukem {
    734   1.1     lukem 
    735   1.1     lukem 	if (S_ISREG(fe->stat->st_mode))
    736   1.7     lukem 		cprintf(fd, "%s=" LLF ";", fact, (LLT)fe->stat->st_size);
    737   1.1     lukem }
    738   1.1     lukem 
    739   1.1     lukem static void
    740   1.1     lukem fact_type(const char *fact, FILE *fd, factelem *fe)
    741   1.1     lukem {
    742   1.1     lukem 
    743   1.3     lukem 	cprintf(fd, "%s=", fact);
    744   1.1     lukem 	switch (fe->stat->st_mode & S_IFMT) {
    745   1.1     lukem 	case S_IFDIR:
    746  1.16     lukem 		if (fe->flags & FE_MLSD) {
    747  1.16     lukem 			if ((fe->flags & FE_ISCURDIR) || ISDOTDIR(fe->display))
    748  1.16     lukem 				cprintf(fd, "cdir");
    749  1.16     lukem 			else if (ISDOTDOTDIR(fe->display))
    750  1.16     lukem 				cprintf(fd, "pdir");
    751  1.16     lukem 			else
    752  1.16     lukem 				cprintf(fd, "dir");
    753  1.16     lukem 		} else {
    754   1.3     lukem 			cprintf(fd, "dir");
    755  1.16     lukem 		}
    756   1.1     lukem 		break;
    757   1.1     lukem 	case S_IFREG:
    758   1.3     lukem 		cprintf(fd, "file");
    759   1.1     lukem 		break;
    760   1.1     lukem 	case S_IFIFO:
    761   1.3     lukem 		cprintf(fd, "OS.unix=fifo");
    762   1.1     lukem 		break;
    763   1.3     lukem 	case S_IFLNK:		/* XXX: probably a NO-OP with stat() */
    764   1.3     lukem 		cprintf(fd, "OS.unix=slink");
    765   1.1     lukem 		break;
    766   1.1     lukem 	case S_IFSOCK:
    767   1.3     lukem 		cprintf(fd, "OS.unix=socket");
    768   1.1     lukem 		break;
    769   1.1     lukem 	case S_IFBLK:
    770   1.1     lukem 	case S_IFCHR:
    771   1.3     lukem 		cprintf(fd, "OS.unix=%s-%d/%d",
    772   1.1     lukem 		    S_ISBLK(fe->stat->st_mode) ? "blk" : "chr",
    773   1.1     lukem 		    major(fe->stat->st_rdev), minor(fe->stat->st_rdev));
    774   1.1     lukem 		break;
    775   1.1     lukem 	default:
    776   1.3     lukem 		cprintf(fd, "OS.unix=UNKNOWN(0%o)", fe->stat->st_mode & S_IFMT);
    777   1.1     lukem 		break;
    778   1.1     lukem 	}
    779   1.3     lukem 	CPUTC(';', fd);
    780   1.1     lukem }
    781   1.1     lukem 
    782   1.1     lukem static void
    783   1.1     lukem fact_unique(const char *fact, FILE *fd, factelem *fe)
    784   1.1     lukem {
    785   1.5     lukem 	char obuf[(sizeof(dev_t) + sizeof(ino_t) + 2) * 4 / 3 + 2];
    786   1.5     lukem 	char tbuf[sizeof(dev_t) + sizeof(ino_t)];
    787   1.1     lukem 
    788   1.5     lukem 	memcpy(tbuf,
    789   1.5     lukem 	    (char *)&(fe->stat->st_dev), sizeof(dev_t));
    790   1.5     lukem 	memcpy(tbuf + sizeof(dev_t),
    791   1.5     lukem 	    (char *)&(fe->stat->st_ino), sizeof(ino_t));
    792   1.5     lukem 	base64_encode(tbuf, sizeof(dev_t) + sizeof(ino_t), obuf, 1);
    793   1.5     lukem 	cprintf(fd, "%s=%s;", fact, obuf);
    794   1.1     lukem }
    795   1.1     lukem 
    796   1.1     lukem static int
    797   1.3     lukem matchgroup(gid_t gid)
    798   1.1     lukem {
    799   1.1     lukem 	int	i;
    800   1.1     lukem 
    801   1.3     lukem 	for (i = 0; i < gidcount; i++)
    802   1.1     lukem 		if (gid == gidlist[i])
    803   1.1     lukem 			return(1);
    804   1.1     lukem 	return (0);
    805   1.1     lukem }
    806   1.1     lukem 
    807   1.1     lukem static void
    808   1.1     lukem mlsname(FILE *fp, factelem *fe)
    809   1.1     lukem {
    810  1.16     lukem 	char realfile[MAXPATHLEN];
    811  1.21       erh 	int i, userf = 0;
    812   1.1     lukem 
    813   1.4     lukem 	for (i = 0; i < FACTTABSIZE; i++) {
    814   1.1     lukem 		if (facttab[i].enabled)
    815   1.1     lukem 			(facttab[i].display)(facttab[i].name, fp, fe);
    816   1.1     lukem 	}
    817  1.16     lukem 	if ((fe->flags & FE_MLSD) &&
    818  1.16     lukem 	    !(fe->flags & FE_ISCURDIR) && !ISDOTDIR(fe->display)) {
    819  1.16     lukem 			/* if MLSD and not "." entry, display as-is */
    820  1.16     lukem 		userf = 0;
    821  1.16     lukem 	} else {
    822  1.16     lukem 			/* if MLST, or MLSD and "." entry, realpath(3) it */
    823  1.16     lukem 		if (realpath(fe->display, realfile) != NULL)
    824  1.16     lukem 			userf = 1;
    825  1.16     lukem 	}
    826  1.16     lukem 	cprintf(fp, " %s\r\n", userf ? realfile : fe->display);
    827   1.1     lukem }
    828   1.1     lukem 
    829   1.1     lukem static void
    830   1.1     lukem replydirname(const char *name, const char *message)
    831   1.1     lukem {
    832   1.9    itojun 	char *p, *ep;
    833  1.12    itojun 	char npath[MAXPATHLEN * 2];
    834   1.1     lukem 
    835   1.9    itojun 	p = npath;
    836   1.9    itojun 	ep = &npath[sizeof(npath) - 1];
    837   1.9    itojun 	while (*name) {
    838  1.11    itojun 		if (*name == '"') {
    839  1.11    itojun 			if (ep - p < 2)
    840  1.11    itojun 				break;
    841   1.9    itojun 			*p++ = *name++;
    842   1.9    itojun 			*p++ = '"';
    843  1.11    itojun 		} else {
    844  1.11    itojun 			if (ep - p < 1)
    845  1.11    itojun 				break;
    846   1.9    itojun 			*p++ = *name++;
    847  1.11    itojun 		}
    848   1.1     lukem 	}
    849   1.9    itojun 	*p = '\0';
    850   1.1     lukem 	reply(257, "\"%s\" %s", npath, message);
    851   1.1     lukem }
    852  1.20      manu 
    853  1.20      manu static void
    854  1.20      manu discover_path(last_path, new_path)
    855  1.20      manu 	char *last_path;
    856  1.20      manu 	const char *new_path;
    857  1.20      manu {
    858  1.20      manu 	char tp[MAXPATHLEN + 1] = "";
    859  1.20      manu 	char tq[MAXPATHLEN + 1] = "";
    860  1.20      manu 	char *cp;
    861  1.20      manu 	char *cq;
    862  1.20      manu 	int sz1, sz2;
    863  1.20      manu 	int nomorelink;
    864  1.20      manu 	struct stat st1, st2;
    865  1.20      manu 
    866  1.20      manu 	if (new_path[0] != '/') {
    867  1.20      manu 		(void)strlcpy(tp, last_path, MAXPATHLEN);
    868  1.20      manu 		(void)strlcat(tp, "/", MAXPATHLEN);
    869  1.20      manu 	}
    870  1.20      manu 	(void)strlcat(tp, new_path, MAXPATHLEN);
    871  1.20      manu 	(void)strlcat(tp, "/", MAXPATHLEN);
    872  1.20      manu 
    873  1.20      manu 	/*
    874  1.20      manu 	 * resolve symlinks. A symlink may introduce another symlink, so we
    875  1.20      manu 	 * loop trying to resolve symlinks until we don't find any of them.
    876  1.20      manu 	 */
    877  1.20      manu 	do {
    878  1.20      manu 		/* Collapse any // into / */
    879  1.20      manu 		while ((cp = strstr(tp, "//")) != NULL)
    880  1.20      manu 			(void)memmove(cp, cp + 1, strlen(cp) - 1 + 1);
    881  1.20      manu 
    882  1.20      manu 		/* Collapse any /./ into / */
    883  1.20      manu 		while ((cp = strstr(tp, "/./")) != NULL)
    884  1.20      manu 			(void)memmove(cp, cp + 2, strlen(cp) - 2 + 1);
    885  1.20      manu 
    886  1.20      manu 		cp = tp;
    887  1.20      manu 		nomorelink = 1;
    888  1.20      manu 
    889  1.20      manu 		while ((cp = strstr(++cp, "/")) != NULL) {
    890  1.20      manu 			sz1 = (u_long)cp - (u_long)tp;
    891  1.20      manu 			if (sz1 > MAXPATHLEN)
    892  1.20      manu 				goto bad;
    893  1.20      manu 			*cp = 0;
    894  1.20      manu 			sz2 = readlink(tp, tq, MAXPATHLEN);
    895  1.20      manu 			*cp = '/';
    896  1.20      manu 
    897  1.20      manu 			/* If this is not a symlink, move to next / */
    898  1.20      manu 			if (sz2 <= 0)
    899  1.20      manu 				continue;
    900  1.20      manu 
    901  1.20      manu 			/*
    902  1.20      manu 			 * We found a symlink, so we will have to
    903  1.20      manu 			 * do one more pass to check there is no
    904  1.20      manu 			 * more symlink in the path
    905  1.20      manu 			 */
    906  1.20      manu 			nomorelink = 0;
    907  1.20      manu 
    908  1.20      manu 			/*
    909  1.20      manu 			 * Null terminate the string and remove trailing /
    910  1.20      manu 			 */
    911  1.20      manu 			tq[sz2] = 0;
    912  1.20      manu 			sz2 = strlen(tq);
    913  1.20      manu 			if (tq[sz2 - 1] == '/')
    914  1.20      manu 				tq[--sz2] = 0;
    915  1.20      manu 
    916  1.20      manu 			/*
    917  1.20      manu 			 * Is this an absolute link or a relative link?
    918  1.20      manu 			 */
    919  1.20      manu 			if (tq[0] == '/') {
    920  1.20      manu 				/* absolute link */
    921  1.20      manu 				if (strlen(cp) + sz2 > MAXPATHLEN)
    922  1.20      manu 					goto bad;
    923  1.20      manu 				memmove(tp + sz2, cp, strlen(cp) + 1);
    924  1.20      manu 				memcpy(tp, tq, sz2);
    925  1.20      manu 			} else {
    926  1.20      manu 				/* relative link */
    927  1.20      manu 				for (cq = cp - 1; *cq != '/'; cq--);
    928  1.20      manu 				if (strlen(tp) - ((u_long)cq - (u_long)cp)
    929  1.20      manu 				    + 1 + sz2 > MAXPATHLEN)
    930  1.20      manu 					goto bad;
    931  1.20      manu 				(void)memmove(cq + 1 + sz2,
    932  1.20      manu 				    cp, strlen(cp) + 1);
    933  1.20      manu 				(void)memcpy(cq + 1, tq, sz2);
    934  1.20      manu 			}
    935  1.20      manu 
    936  1.20      manu 			/*
    937  1.20      manu 			 * start over, looking for new symlinks
    938  1.20      manu 			 */
    939  1.20      manu 			break;
    940  1.20      manu 		}
    941  1.20      manu 	} while (nomorelink == 0);
    942  1.20      manu 
    943  1.20      manu 	/* Collapse any /foo/../ into /foo/ */
    944  1.20      manu 	while ((cp = strstr(tp, "/../")) != NULL) {
    945  1.20      manu 		/* ^/../foo/ becomes ^/foo/ */
    946  1.20      manu 		if (cp == tp) {
    947  1.20      manu 			(void)memmove(cp, cp + 3,
    948  1.20      manu 			    strlen(cp) - 3 + 1);
    949  1.20      manu 		} else {
    950  1.20      manu 			for (cq = cp - 1; *cq != '/'; cq--);
    951  1.20      manu 			(void)memmove(cq, cp + 3,
    952  1.20      manu 			    strlen(cp) - 3 + 1);
    953  1.20      manu 		}
    954  1.20      manu 	}
    955  1.20      manu 
    956  1.20      manu 	/* strip strailing / */
    957  1.20      manu 	if (strlen(tp) != 1)
    958  1.20      manu 		tp[strlen(tp) - 1] = '\0';
    959  1.20      manu 
    960  1.20      manu 	/* check that the path is correct */
    961  1.20      manu 	stat(tp, &st1);
    962  1.20      manu 	stat(".", &st2);
    963  1.20      manu 	if ((st1.st_dev != st2.st_dev) || (st1.st_ino != st2.st_ino))
    964  1.20      manu 		goto bad;
    965  1.20      manu 
    966  1.20      manu 	(void)strlcpy(last_path, tp, MAXPATHLEN);
    967  1.20      manu 	return;
    968  1.20      manu 
    969  1.20      manu bad:
    970  1.20      manu 	(void)strlcat(last_path, "/", MAXPATHLEN);
    971  1.20      manu 	(void)strlcat(last_path, new_path, MAXPATHLEN);
    972  1.20      manu 	return;
    973  1.20      manu }
    974  1.20      manu 
    975