cmds.c revision 1.4 1 /* $NetBSD: cmds.c,v 1.4 2000/06/19 17:08:05 lukem Exp $ */
2
3 /*
4 * Copyright (c) 1999-2000 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.4 2000/06/19 17:08:05 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 logcmd("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 logcmd("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 dout = dataconn("MLSD", (off_t)-1, "w");
250 if (dout == NULL)
251 return;
252
253 if ((dirp = opendir(path)) == NULL)
254 goto mlsdperror;
255 f.stat = &sb;
256 while ((dp = readdir(dirp)) != NULL) {
257 snprintf(name, sizeof(name), "%s/%s", path, dp->d_name);
258 if (ISDOTDIR(dp->d_name)) { /* special case curdir: */
259 if (! hastypefact)
260 continue;
261 f.pdirstat = NULL; /* require stat of parent */
262 f.display = path; /* set name to real name */
263 f.iscurdir = 1; /* flag name is curdir */
264 } else {
265 if (ISDOTDOTDIR(dp->d_name)) {
266 if (! hastypefact)
267 continue;
268 f.pdirstat = NULL;
269 } else
270 f.pdirstat = &pdirstat; /* cache parent stat */
271 f.display = dp->d_name;
272 f.iscurdir = 0;
273 }
274 if (stat(name, &sb) == -1)
275 continue;
276 f.path = name;
277 mlsname(dout, &f);
278 }
279 (void)closedir(dirp);
280
281 if (ferror(dout) != 0)
282 perror_reply(550, "Data connection");
283 else
284 reply(226, "MLSD complete.");
285 closedataconn(dout);
286 total_xfers_out++;
287 total_xfers++;
288 }
289
290 void
291 mlst(const char *path)
292 {
293 struct stat sb;
294 factelem f;
295
296 if (path == NULL)
297 path = ".";
298 if (stat(path, &sb) == -1) {
299 perror_reply(550, path);
300 return;
301 }
302 reply(-250, "MLST %s", path);
303 f.path = path;
304 f.display = path;
305 f.stat = &sb;
306 f.pdirstat = NULL;
307 f.iscurdir = 0;
308 CPUTC(' ', stdout);
309 mlsname(stdout, &f);
310 reply(250, "End");
311 }
312
313
314 void
315 opts(const char *command)
316 {
317 struct tab *c;
318 char *ep;
319
320 if ((ep = strchr(command, ' ')) != NULL)
321 *ep++ = '\0';
322 c = lookup(cmdtab, command);
323 if (c == NULL) {
324 reply(502, "Unknown command %s.", command);
325 return;
326 }
327 if (! CMD_IMPLEMENTED(c)) {
328 reply(501, "%s command not implemented.", c->name);
329 return;
330 }
331 if (! CMD_HAS_OPTIONS(c)) {
332 reply(501, "%s command does not support persistent options.",
333 c->name);
334 return;
335 }
336
337 /* special case: MLST */
338 if (strcasecmp(command, "MLST") == 0) {
339 int enabled[FACTTABSIZE];
340 int i, onedone;
341 size_t len;
342 char *p;
343
344 for (i = 0; i < sizeof(enabled) / sizeof(int); i++)
345 enabled[i] = 0;
346 if (ep == NULL || *ep == '\0')
347 goto displaymlstopts;
348
349 /* don't like spaces, and need trailing ; */
350 len = strlen(ep);
351 if (strchr(ep, ' ') != NULL || ep[len - 1] != ';') {
352 badmlstopt:
353 reply(501, "Invalid MLST options");
354 return;
355 }
356 ep[len - 1] = '\0';
357 while ((p = strsep(&ep, ";")) != NULL) {
358 if (*p == '\0')
359 goto badmlstopt;
360 for (i = 0; i < FACTTABSIZE; i++)
361 if (strcasecmp(p, facttab[i].name) == 0) {
362 enabled[i] = 1;
363 break;
364 }
365 }
366
367 displaymlstopts:
368 for (i = 0; i < FACTTABSIZE; i++)
369 facttab[i].enabled = enabled[i];
370 cprintf(stdout, "200 MLST OPTS");
371 for (i = onedone = 0; i < FACTTABSIZE; i++) {
372 if (facttab[i].enabled) {
373 cprintf(stdout, "%s%s;", onedone ? "" : " ",
374 facttab[i].name);
375 onedone++;
376 }
377 }
378 cprintf(stdout, "\r\n");
379 fflush(stdout);
380 return;
381 }
382
383 /* default cases */
384 if (ep != NULL && *ep != '\0')
385 REASSIGN(c->options, xstrdup(ep));
386 if (c->options != NULL)
387 reply(200, "Options for %s are '%s'.", c->name,
388 c->options);
389 else
390 reply(200, "No options defined for %s.", c->name);
391 }
392
393 void
394 pwd(void)
395 {
396 char path[MAXPATHLEN];
397
398 if (getcwd(path, sizeof(path) - 1) == NULL)
399 reply(550, "Can't get the current directory: %s.",
400 strerror(errno));
401 else
402 replydirname(path, "is the current directory.");
403 }
404
405 void
406 removedir(const char *name)
407 {
408 char *p = NULL;
409
410 if (rmdir(name) < 0) {
411 p = strerror(errno);
412 perror_reply(550, name);
413 } else
414 ack("RMD");
415 logcmd("rmdir", -1, name, NULL, NULL, p);
416 }
417
418 char *
419 renamefrom(const char *name)
420 {
421 struct stat st;
422
423 if (stat(name, &st) < 0) {
424 perror_reply(550, name);
425 return (NULL);
426 }
427 reply(350, "File exists, ready for destination name");
428 return (xstrdup(name));
429 }
430
431 void
432 renamecmd(const char *from, const char *to)
433 {
434 char *p = NULL;
435
436 if (rename(from, to) < 0) {
437 p = strerror(errno);
438 perror_reply(550, "rename");
439 } else
440 ack("RNTO");
441 logcmd("rename", -1, from, to, NULL, p);
442 }
443
444 void
445 sizecmd(const char *filename)
446 {
447 switch (type) {
448 case TYPE_L:
449 case TYPE_I: {
450 struct stat stbuf;
451 if (stat(filename, &stbuf) < 0 || !S_ISREG(stbuf.st_mode))
452 reply(550, "%s: not a plain file.", filename);
453 else
454 reply(213, "%qu", (qufmt_t)stbuf.st_size);
455 break; }
456 case TYPE_A: {
457 FILE *fin;
458 int c;
459 off_t count;
460 struct stat stbuf;
461 fin = fopen(filename, "r");
462 if (fin == NULL) {
463 perror_reply(550, filename);
464 return;
465 }
466 if (fstat(fileno(fin), &stbuf) < 0 || !S_ISREG(stbuf.st_mode)) {
467 reply(550, "%s: not a plain file.", filename);
468 (void) fclose(fin);
469 return;
470 }
471
472 count = 0;
473 while((c=getc(fin)) != EOF) {
474 if (c == '\n') /* will get expanded to \r\n */
475 count++;
476 count++;
477 }
478 (void) fclose(fin);
479
480 reply(213, "%qd", (qdfmt_t)count);
481 break; }
482 default:
483 reply(504, "SIZE not implemented for Type %c.", "?AEIL"[type]);
484 }
485 }
486
487 void
488 statfilecmd(const char *filename)
489 {
490 FILE *fin;
491 int c;
492 char *argv[] = { INTERNAL_LS, "-lgA", "", NULL };
493
494 argv[2] = (char *)filename;
495 fin = ftpd_popen(argv, "r", STDOUT_FILENO);
496 reply(-211, "status of %s:", filename);
497 /* XXX: use fgetln() or fparseln() here? */
498 while ((c = getc(fin)) != EOF) {
499 if (c == '\n') {
500 if (ferror(stdout)){
501 perror_reply(421, "control connection");
502 (void) ftpd_pclose(fin);
503 dologout(1);
504 /* NOTREACHED */
505 }
506 if (ferror(fin)) {
507 perror_reply(551, filename);
508 (void) ftpd_pclose(fin);
509 return;
510 }
511 CPUTC('\r', stdout);
512 }
513 CPUTC(c, stdout);
514 }
515 (void) ftpd_pclose(fin);
516 reply(211, "End of Status");
517 }
518
519 /* -- */
520
521 static void
522 ack(const char *s)
523 {
524
525 reply(250, "%s command successful.", s);
526 }
527
528 /*
529 * Encode len bytes starting at clear using base64 encoding into encoded,
530 * which should be at least ((len + 2) * 4 / 3 + 1) in size.
531 * If nulterm is non-zero, terminate with \0 else pad to len with `='.
532 */
533 static void
534 base64_encode(const char *clear, size_t len, char *encoded, int nulterm)
535 {
536 static const char enc[] =
537 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
538 char *cp;
539 int i;
540
541 cp = encoded;
542 for (i = 0; i < len; i += 3) {
543 *(cp++) = enc[((clear[i + 0] >> 2))];
544 *(cp++) = enc[((clear[i + 0] << 4) & 0x30)
545 | ((clear[i + 1] >> 4) & 0x0f)];
546 *(cp++) = enc[((clear[i + 1] << 2) & 0x3c)
547 | ((clear[i + 2] >> 6) & 0x03)];
548 *(cp++) = enc[((clear[i + 2] ) & 0x3f)];
549 }
550 *cp = '\0';
551 while (i-- > len)
552 *(--cp) = nulterm ? '\0' : '=';
553 }
554
555 static void
556 fact_modify(const char *fact, FILE *fd, factelem *fe)
557 {
558 struct tm *t;
559
560 t = gmtime(&(fe->stat->st_mtime));
561 cprintf(fd, "%s=%04d%02d%02d%02d%02d%02d;", fact,
562 TM_YEAR_BASE + t->tm_year,
563 t->tm_mon+1, t->tm_mday,
564 t->tm_hour, t->tm_min, t->tm_sec);
565 }
566
567 static void
568 fact_perm(const char *fact, FILE *fd, factelem *fe)
569 {
570 int rok, wok, xok, pdirwok;
571 struct stat *pdir;
572
573 if (fe->stat->st_uid == geteuid()) {
574 rok = ((fe->stat->st_mode & S_IRUSR) != 0);
575 wok = ((fe->stat->st_mode & S_IWUSR) != 0);
576 xok = ((fe->stat->st_mode & S_IXUSR) != 0);
577 } else if (matchgroup(fe->stat->st_gid)) {
578 rok = ((fe->stat->st_mode & S_IRGRP) != 0);
579 wok = ((fe->stat->st_mode & S_IWGRP) != 0);
580 xok = ((fe->stat->st_mode & S_IXGRP) != 0);
581 } else {
582 rok = ((fe->stat->st_mode & S_IROTH) != 0);
583 wok = ((fe->stat->st_mode & S_IWOTH) != 0);
584 xok = ((fe->stat->st_mode & S_IXOTH) != 0);
585 }
586
587 cprintf(fd, "%s=", fact);
588
589 /*
590 * if parent info not provided, look it up, but
591 * only if the current class has modify rights,
592 * since we only need this info in such a case.
593 */
594 pdir = fe->pdirstat;
595 if (pdir == NULL && curclass.modify) {
596 size_t len;
597 char realdir[MAXPATHLEN], *p;
598 struct stat dir;
599
600 len = strlcpy(realdir, fe->path, sizeof(realdir));
601 if (len < sizeof(realdir) - 4) {
602 if (S_ISDIR(fe->stat->st_mode))
603 strlcat(realdir, "/..", sizeof(realdir));
604 else {
605 /* if has a /, move back to it */
606 /* otherwise use '..' */
607 if ((p = strrchr(realdir, '/')) != NULL) {
608 if (p == realdir)
609 p++;
610 *p = '\0';
611 } else
612 strlcpy(realdir, "..", sizeof(realdir));
613 }
614 if (stat(realdir, &dir) == 0)
615 pdir = &dir;
616 }
617 }
618 pdirwok = 0;
619 if (pdir != NULL) {
620 if (pdir->st_uid == geteuid())
621 pdirwok = ((pdir->st_mode & S_IWUSR) != 0);
622 else if (matchgroup(pdir->st_gid))
623 pdirwok = ((pdir->st_mode & S_IWGRP) != 0);
624 else
625 pdirwok = ((pdir->st_mode & S_IWOTH) != 0);
626 }
627
628 /* 'a': can APPE to file */
629 if (wok && curclass.upload && S_ISREG(fe->stat->st_mode))
630 CPUTC('a', fd);
631
632 /* 'c': can create or append to files in directory */
633 if (wok && curclass.modify && S_ISDIR(fe->stat->st_mode))
634 CPUTC('c', fd);
635
636 /* 'd': can delete file or directory */
637 if (pdirwok && curclass.modify) {
638 int candel;
639
640 candel = 1;
641 if (S_ISDIR(fe->stat->st_mode)) {
642 DIR *dirp;
643 struct dirent *dp;
644
645 if ((dirp = opendir(fe->display)) == NULL)
646 candel = 0;
647 else {
648 while ((dp = readdir(dirp)) != NULL) {
649 if (ISDOTDIR(dp->d_name) ||
650 ISDOTDOTDIR(dp->d_name))
651 continue;
652 candel = 0;
653 break;
654 }
655 closedir(dirp);
656 }
657 }
658 if (candel)
659 CPUTC('d', fd);
660 }
661
662 /* 'e': can enter directory */
663 if (xok && S_ISDIR(fe->stat->st_mode))
664 CPUTC('e', fd);
665
666 /* 'f': can rename file or directory */
667 if (pdirwok && curclass.modify)
668 CPUTC('f', fd);
669
670 /* 'l': can list directory */
671 if (rok && xok && S_ISDIR(fe->stat->st_mode))
672 CPUTC('l', fd);
673
674 /* 'm': can create directory */
675 if (wok && curclass.modify && S_ISDIR(fe->stat->st_mode))
676 CPUTC('m', fd);
677
678 /* 'p': can remove files in directory */
679 if (wok && curclass.modify && S_ISDIR(fe->stat->st_mode))
680 CPUTC('p', fd);
681
682 /* 'r': can RETR file */
683 if (rok && S_ISREG(fe->stat->st_mode))
684 CPUTC('r', fd);
685
686 /* 'w': can STOR file */
687 if (wok && curclass.upload && S_ISREG(fe->stat->st_mode))
688 CPUTC('w', fd);
689
690 CPUTC(';', fd);
691 }
692
693 static void
694 fact_size(const char *fact, FILE *fd, factelem *fe)
695 {
696
697 if (S_ISREG(fe->stat->st_mode))
698 cprintf(fd, "%s=%lld;", fact, (long long)fe->stat->st_size);
699 }
700
701 static void
702 fact_type(const char *fact, FILE *fd, factelem *fe)
703 {
704
705 cprintf(fd, "%s=", fact);
706 switch (fe->stat->st_mode & S_IFMT) {
707 case S_IFDIR:
708 if (fe->iscurdir || ISDOTDIR(fe->display))
709 cprintf(fd, "cdir");
710 else if (ISDOTDOTDIR(fe->display))
711 cprintf(fd, "pdir");
712 else
713 cprintf(fd, "dir");
714 break;
715 case S_IFREG:
716 cprintf(fd, "file");
717 break;
718 case S_IFIFO:
719 cprintf(fd, "OS.unix=fifo");
720 break;
721 case S_IFLNK: /* XXX: probably a NO-OP with stat() */
722 cprintf(fd, "OS.unix=slink");
723 break;
724 case S_IFSOCK:
725 cprintf(fd, "OS.unix=socket");
726 break;
727 case S_IFBLK:
728 case S_IFCHR:
729 cprintf(fd, "OS.unix=%s-%d/%d",
730 S_ISBLK(fe->stat->st_mode) ? "blk" : "chr",
731 major(fe->stat->st_rdev), minor(fe->stat->st_rdev));
732 break;
733 default:
734 cprintf(fd, "OS.unix=UNKNOWN(0%o)", fe->stat->st_mode & S_IFMT);
735 break;
736 }
737 CPUTC(';', fd);
738 }
739
740 static void
741 fact_unique(const char *fact, FILE *fd, factelem *fe)
742 {
743 char obuf[(MAX(sizeof(dev_t),sizeof(ino_t)) + 2) * 4 / 3 + 2];
744
745 base64_encode((char *)&(fe->stat->st_dev), sizeof(dev_t), obuf, 1);
746 cprintf(fd, "%s=%s", fact, obuf);
747 base64_encode((char *)&(fe->stat->st_ino), sizeof(ino_t), obuf, 1);
748 cprintf(fd, "%s;", obuf);
749 }
750
751 static int
752 matchgroup(gid_t gid)
753 {
754 int i;
755
756 for (i = 0; i < gidcount; i++)
757 if (gid == gidlist[i])
758 return(1);
759 return (0);
760 }
761
762 static void
763 mlsname(FILE *fp, factelem *fe)
764 {
765 int i;
766
767 for (i = 0; i < FACTTABSIZE; i++) {
768 if (facttab[i].enabled)
769 (facttab[i].display)(facttab[i].name, fp, fe);
770 }
771 cprintf(fp, " %s\r\n", fe->display);
772 }
773
774 static void
775 replydirname(const char *name, const char *message)
776 {
777 char npath[MAXPATHLEN];
778 int i;
779
780 for (i = 0; *name != '\0' && i < sizeof(npath) - 1; i++, name++) {
781 npath[i] = *name;
782 if (*name == '"')
783 npath[++i] = '"';
784 }
785 npath[i] = '\0';
786 reply(257, "\"%s\" %s", npath, message);
787 }
788