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