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