cmds.c revision 1.1 1 /* $NetBSD: cmds.c,v 1.1 2000/06/14 13:44:22 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.1 2000/06/14 13:44:22 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 #include "extern.h"
122
123 typedef struct {
124 const char *path; /* full pathname */
125 const char *display; /* name to display */
126 struct stat *stat; /* stat of path */
127 struct stat *pdirstat; /* stat of path's parent dir */
128 int iscurdir; /* nonzero if name is the current dir */
129 } factelem;
130
131 static void ack(const char *);
132 static void fact_type(const char *, FILE *, factelem *);
133 static void fact_size(const char *, FILE *, factelem *);
134 static void fact_modify(const char *, FILE *, factelem *);
135 static void fact_perm(const char *, FILE *, factelem *);
136 static void fact_unique(const char *, FILE *, factelem *);
137 static void fact_unix_group(const char *, FILE *, factelem *);
138 static void fact_unix_mode(const char *, FILE *, factelem *);
139 static void fact_unix_owner(const char *, FILE *, factelem *);
140 static int matchgroup(gid_t, gid_t *, int);
141 static void mlsname(FILE *, factelem *);
142 static void replydirname(const char *, const char *);
143
144 struct ftpfact {
145 const char *name; /* name of fact */
146 int enabled; /* if fact is enabled */
147 void (*display)(const char *, FILE *, factelem *);
148 /* function to display fact */
149 };
150
151 struct ftpfact facttab[] = {
152 { "Type", 1, fact_type },
153 { "Size", 1, fact_size },
154 { "Modify", 1, fact_modify },
155 { "Perm", 1, fact_perm },
156 { "Unique", 1, fact_unique },
157 { "UNIX.mode", 1, fact_unix_mode },
158 { "UNIX.owner", 0, fact_unix_owner },
159 { "UNIX.group", 0, fact_unix_group },
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 lreply(211, "Features supported");
199 lreply(-1, " MDTM");
200 lreply(-2, " MLST ");
201 for (i = 0; facttab[i].name; i++)
202 lreply(-2, "%s%s;", facttab[i].name,
203 facttab[i].enabled ? "*" : "");
204 lreply(-1, "");
205 lreply(-1, " REST STREAM");
206 lreply(-1, " SIZE");
207 lreply(-1, " TVFS");
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 FILE *dout;
228 DIR *dirp;
229 struct dirent *dp;
230 struct stat sb, pdirstat;
231 char name[MAXPATHLEN];
232 factelem f;
233
234 if (path == NULL)
235 path = ".";
236 if (stat(path, &pdirstat) == -1) {
237 mlsdperror:
238 perror_reply(550, path);
239 return;
240 }
241 if (! S_ISDIR(pdirstat.st_mode)) {
242 errno = ENOTDIR;
243 goto mlsdperror;
244 }
245 #ifdef DEBUG /* send mlsd to ctrl connection not data connection */
246 dout = stdout;
247 lreply(250, "MLSD %s", path);
248 #else
249 dout = dataconn("MLSD", (off_t)-1, "w");
250 if (dout == NULL)
251 return;
252 #endif
253
254 if ((dirp = opendir(path)) == NULL)
255 goto mlsdperror;
256 f.stat = &sb;
257 while ((dp = readdir(dirp)) != NULL) {
258 snprintf(name, sizeof(name), "%s/%s", path, dp->d_name);
259 /* printf("got >%s< >%s<\n", dp->d_name, name); */
260 if (stat(name, &sb) == -1)
261 continue;
262 f.path = name;
263 if (ISDOTDIR(dp->d_name)) { /* special case curdir: */
264 f.display = path; /* set name to real name */
265 f.iscurdir = 1; /* flag name is curdir */
266 f.pdirstat = NULL; /* require stat of parent */
267 } else {
268 f.display = dp->d_name;
269 f.iscurdir = 0;
270 if (ISDOTDOTDIR(dp->d_name))
271 f.pdirstat = NULL;
272 else
273 f.pdirstat = &pdirstat; /* cache parent stat */
274 }
275 mlsname(dout, &f);
276 }
277 (void)closedir(dirp);
278
279 #ifdef DEBUG
280 reply(250, "End");
281 #else
282 if (ferror(dout) != 0)
283 perror_reply(550, "Data connection");
284 else
285 reply(226, "MLSD complete.");
286 (void) fclose(dout);
287 total_xfers_out++;
288 total_xfers++;
289 #endif
290 }
291
292 void
293 mlst(const char *path)
294 {
295 struct stat sb;
296 factelem f;
297
298 if (path == NULL)
299 path = ".";
300 if (stat(path, &sb) == -1) {
301 perror_reply(550, path);
302 return;
303 }
304 lreply(250, "MLST %s", path);
305 f.path = path;
306 f.display = path;
307 f.stat = &sb;
308 f.pdirstat = NULL;
309 f.iscurdir = 0;
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 i, onedone;
341 char *p;
342
343 for (i = 0; facttab[i].name; i++)
344 facttab[i].enabled = 0;
345 if (ep == NULL || *ep == '\0')
346 goto displaymlstopts;
347
348 /* don't like spaces, and need trailing ; */
349 if (strchr(ep, ' ') != NULL || ep[strlen(ep) - 1] != ';') {
350 reply(501, "Invalid MLST options");
351 return;
352 }
353 while ((p = strsep(&ep, ";")) != NULL) {
354 for (i = 0; facttab[i].name; i++)
355 if (strcasecmp(p, facttab[i].name) == 0) {
356 facttab[i].enabled = 1;
357 break;
358 }
359 }
360
361 displaymlstopts:
362 lreply(-2, "200 MLST OPTS");
363 for (i = onedone = 0; facttab[i].name; i++) {
364 if (facttab[i].enabled) {
365 lreply(-2, "%s%s;", onedone ? "" : " ",
366 facttab[i].name);
367 onedone++;
368 }
369 }
370 lreply(-1, "");
371 return;
372 }
373
374 /* default cases */
375 if (ep != NULL && *ep != '\0')
376 REASSIGN(c->options, xstrdup(ep));
377 if (c->options != NULL)
378 reply(200, "Options for %s are '%s'.", c->name,
379 c->options);
380 else
381 reply(200, "No options defined for %s.", c->name);
382 }
383
384 void
385 pwd(void)
386 {
387 char path[MAXPATHLEN];
388
389 if (getcwd(path, sizeof(path) - 1) == NULL)
390 reply(550, "Can't get the current directory: %s.",
391 strerror(errno));
392 else
393 replydirname(path, "is the current directory.");
394 }
395
396 void
397 removedir(const char *name)
398 {
399 char *p = NULL;
400
401 if (rmdir(name) < 0) {
402 p = strerror(errno);
403 perror_reply(550, name);
404 } else
405 ack("RMD");
406 logcmd("rmdir", -1, name, NULL, NULL, p);
407 }
408
409 char *
410 renamefrom(const char *name)
411 {
412 struct stat st;
413
414 if (stat(name, &st) < 0) {
415 perror_reply(550, name);
416 return (NULL);
417 }
418 reply(350, "File exists, ready for destination name");
419 return (xstrdup(name));
420 }
421
422 void
423 renamecmd(const char *from, const char *to)
424 {
425 char *p = NULL;
426
427 if (rename(from, to) < 0) {
428 p = strerror(errno);
429 perror_reply(550, "rename");
430 } else
431 ack("RNTO");
432 logcmd("rename", -1, from, to, NULL, p);
433 }
434
435 void
436 sizecmd(const char *filename)
437 {
438 switch (type) {
439 case TYPE_L:
440 case TYPE_I: {
441 struct stat stbuf;
442 if (stat(filename, &stbuf) < 0 || !S_ISREG(stbuf.st_mode))
443 reply(550, "%s: not a plain file.", filename);
444 else
445 reply(213, "%qu", (qufmt_t)stbuf.st_size);
446 break; }
447 case TYPE_A: {
448 FILE *fin;
449 int c;
450 off_t count;
451 struct stat stbuf;
452 fin = fopen(filename, "r");
453 if (fin == NULL) {
454 perror_reply(550, filename);
455 return;
456 }
457 if (fstat(fileno(fin), &stbuf) < 0 || !S_ISREG(stbuf.st_mode)) {
458 reply(550, "%s: not a plain file.", filename);
459 (void) fclose(fin);
460 return;
461 }
462
463 count = 0;
464 while((c=getc(fin)) != EOF) {
465 if (c == '\n') /* will get expanded to \r\n */
466 count++;
467 count++;
468 }
469 (void) fclose(fin);
470
471 reply(213, "%qd", (qdfmt_t)count);
472 break; }
473 default:
474 reply(504, "SIZE not implemented for Type %c.", "?AEIL"[type]);
475 }
476 }
477
478 void
479 statfilecmd(const char *filename)
480 {
481 FILE *fin;
482 int c;
483 char *argv[] = { INTERNAL_LS, "-lgA", "", NULL };
484
485 argv[2] = (char *)filename;
486 fin = ftpd_popen(argv, "r", STDOUT_FILENO);
487 lreply(211, "status of %s:", filename);
488 while ((c = getc(fin)) != EOF) {
489 if (c == '\n') {
490 if (ferror(stdout)){
491 perror_reply(421, "control connection");
492 (void) ftpd_pclose(fin);
493 dologout(1);
494 /* NOTREACHED */
495 }
496 if (ferror(fin)) {
497 perror_reply(551, filename);
498 (void) ftpd_pclose(fin);
499 return;
500 }
501 (void) putchar('\r');
502 total_bytes++;
503 total_bytes_out++;
504 }
505 (void) putchar(c);
506 total_bytes++;
507 total_bytes_out++;
508 }
509 (void) ftpd_pclose(fin);
510 reply(211, "End of Status");
511 }
512
513 /* -- */
514
515 static void
516 ack(const char *s)
517 {
518
519 reply(250, "%s command successful.", s);
520 }
521
522 static void
523 fact_modify(const char *fact, FILE *fd, factelem *fe)
524 {
525 struct tm *t;
526
527 t = gmtime(&(fe->stat->st_mtime));
528 fprintf(fd, "%s=%04d%02d%02d%02d%02d%02d;", fact,
529 TM_YEAR_BASE + t->tm_year,
530 t->tm_mon+1, t->tm_mday,
531 t->tm_hour, t->tm_min, t->tm_sec);
532 }
533
534 static void
535 fact_perm(const char *fact, FILE *fd, factelem *fe)
536 {
537 int rok, wok, xok, pdirwok, ngid;
538 struct stat *pdir;
539 gid_t gids[NGROUPS_MAX];
540
541 ngid = getgroups(sizeof(gids), gids);
542 if (fe->stat->st_uid == geteuid()) {
543 rok = ((fe->stat->st_mode & S_IRUSR) != 0);
544 wok = ((fe->stat->st_mode & S_IWUSR) != 0);
545 xok = ((fe->stat->st_mode & S_IXUSR) != 0);
546 } else if (matchgroup(fe->stat->st_gid, gids, ngid)) {
547 rok = ((fe->stat->st_mode & S_IRGRP) != 0);
548 wok = ((fe->stat->st_mode & S_IWGRP) != 0);
549 xok = ((fe->stat->st_mode & S_IXGRP) != 0);
550 } else {
551 rok = ((fe->stat->st_mode & S_IROTH) != 0);
552 wok = ((fe->stat->st_mode & S_IWOTH) != 0);
553 xok = ((fe->stat->st_mode & S_IXOTH) != 0);
554 }
555
556 fprintf(fd, "%s=", fact);
557
558 /*
559 * if parent info not provided, look it up, but
560 * only if the current class has modify rights,
561 * since we only need this info in such a case.
562 */
563 pdir = fe->pdirstat;
564 if (pdir == NULL && curclass.modify) {
565 size_t len;
566 char realdir[MAXPATHLEN], *p;
567 struct stat dir;
568
569 len = strlcpy(realdir, fe->path, sizeof(realdir));
570 /* printf("[path=%s]", fe->path); */
571 if (len < sizeof(realdir) - 4) {
572 if (S_ISDIR(fe->stat->st_mode))
573 strlcat(realdir, "/..", sizeof(realdir));
574 else {
575 /* if has a /, move back to it */
576 /* otherwise use '..' */
577 if ((p = strrchr(realdir, '/')) != NULL) {
578 if (p == realdir)
579 p++;
580 *p = '\0';
581 } else
582 strlcpy(realdir, "..", sizeof(realdir));
583 }
584 /* printf("[real=%s]", realdir); */
585 if (stat(realdir, &dir) == 0)
586 pdir = &dir;
587 }
588 }
589 pdirwok = 0;
590 if (pdir != NULL) {
591 if (pdir->st_uid == geteuid())
592 pdirwok = ((pdir->st_mode & S_IWUSR) != 0);
593 else if (matchgroup(pdir->st_gid, gids, ngid))
594 pdirwok = ((pdir->st_mode & S_IWGRP) != 0);
595 else
596 pdirwok = ((pdir->st_mode & S_IWOTH) != 0);
597 }
598
599 /* printf("[euid=%d,r%d,w%d,x%d,pw%d]", geteuid(), rok, wok, xok, pdirwok);
600 */
601
602 /* 'a': can APPE to file */
603 if (wok && curclass.upload && S_ISREG(fe->stat->st_mode))
604 fputs("a", fd);
605
606 /* 'c': can create or append to files in directory */
607 if (wok && curclass.modify && S_ISDIR(fe->stat->st_mode))
608 fputs("c", fd);
609
610 /* 'd': can delete file or directory */
611 if (pdirwok && curclass.modify) {
612 int candel;
613
614 candel = 1;
615 if (S_ISDIR(fe->stat->st_mode)) {
616 DIR *dirp;
617 struct dirent *dp;
618
619 if ((dirp = opendir(fe->display)) == NULL)
620 candel = 0;
621 else {
622 while ((dp = readdir(dirp)) != NULL) {
623 if (ISDOTDIR(dp->d_name) ||
624 ISDOTDOTDIR(dp->d_name))
625 continue;
626 candel = 0;
627 break;
628 }
629 closedir(dirp);
630 }
631 }
632 if (candel)
633 fputs("d", fd);
634 }
635
636 /* 'e': can enter directory */
637 if (xok && S_ISDIR(fe->stat->st_mode))
638 fputs("e", fd);
639
640 /* 'f': can rename file or directory */
641 if (pdirwok && curclass.modify)
642 fputs("f", fd);
643
644 /* 'l': can list directory */
645 if (rok && xok && S_ISDIR(fe->stat->st_mode))
646 fputs("l", fd);
647
648 /* 'm': can create directory */
649 if (wok && curclass.modify && S_ISDIR(fe->stat->st_mode))
650 fputs("m", fd);
651
652 /* 'p': can remove files in directory */
653 if (wok && curclass.modify && S_ISDIR(fe->stat->st_mode))
654 fputs("p", fd);
655
656 /* 'r': can RETR file */
657 if (rok && S_ISREG(fe->stat->st_mode))
658 fputs("r", fd);
659
660 /* 'w': can STOR file */
661 if (wok && curclass.upload && S_ISREG(fe->stat->st_mode))
662 fputs("w", fd);
663
664 fputc(';', fd);
665 }
666
667 static void
668 fact_size(const char *fact, FILE *fd, factelem *fe)
669 {
670
671 if (S_ISREG(fe->stat->st_mode))
672 fprintf(fd, "%s=%lld;", fact, (long long)fe->stat->st_size);
673 }
674
675 static void
676 fact_type(const char *fact, FILE *fd, factelem *fe)
677 {
678
679 fprintf(fd, "%s=", fact);
680 switch (fe->stat->st_mode & S_IFMT) {
681 case S_IFDIR:
682 if (fe->iscurdir || ISDOTDIR(fe->display))
683 fputs("cdir", fd);
684 else if (ISDOTDOTDIR(fe->display))
685 fputs("pdir", fd);
686 else
687 fputs("dir", fd);
688 break;
689 case S_IFREG:
690 fputs("file", fd);
691 break;
692 case S_IFIFO:
693 fputs("OS.unix=fifo", fd);
694 break;
695 case S_IFLNK:
696 fputs("OS.unix=slink", fd);
697 break;
698 case S_IFSOCK:
699 fputs("OS.unix=socket", fd);
700 break;
701 case S_IFBLK:
702 case S_IFCHR:
703 fprintf(fd, "OS.unix=%s-%d/%d",
704 S_ISBLK(fe->stat->st_mode) ? "blk" : "chr",
705 major(fe->stat->st_rdev), minor(fe->stat->st_rdev));
706 break;
707 default:
708 fprintf(fd, "OS.unix=UNKNOWN(0%o)", fe->stat->st_mode & S_IFMT);
709 break;
710 }
711 fputc(';', fd);
712 }
713
714 static void
715 fact_unique(const char *fact, FILE *fd, factelem *fe)
716 {
717
718 fprintf(fd, "%s=%04x%08x;", fact, fe->stat->st_dev, fe->stat->st_ino);
719 }
720
721 static void
722 fact_unix_mode(const char *fact, FILE *fd, factelem *fe)
723 {
724
725 fprintf(fd, "%s=%03o;", fact, fe->stat->st_mode & ACCESSPERMS);
726 }
727
728 static void
729 fact_unix_owner(const char *fact, FILE *fd, factelem *fe)
730 {
731
732 fprintf(fd, "%s=%d;", fact, (int)fe->stat->st_uid);
733 }
734
735 static void
736 fact_unix_group(const char *fact, FILE *fd, factelem *fe)
737 {
738
739 fprintf(fd, "%s=%d;", fact, (int)fe->stat->st_gid);
740 }
741
742 static int
743 matchgroup(gid_t gid, gid_t *gidlist, int ngids)
744 {
745 int i;
746
747 for (i = 0; i < ngids; i++)
748 if (gid == gidlist[i])
749 return(1);
750 return (0);
751 }
752
753 static void
754 mlsname(FILE *fp, factelem *fe)
755 {
756 int i;
757
758 fputs(" ", fp);
759 for (i = 0; facttab[i].name; i++) {
760 if (facttab[i].enabled)
761 (facttab[i].display)(facttab[i].name, fp, fe);
762 }
763 fprintf(fp, " %s\r\n", fe->display);
764 }
765
766 static void
767 replydirname(const char *name, const char *message)
768 {
769 char npath[MAXPATHLEN];
770 int i;
771
772 for (i = 0; *name != '\0' && i < sizeof(npath) - 1; i++, name++) {
773 npath[i] = *name;
774 if (*name == '"')
775 npath[++i] = '"';
776 }
777 npath[i] = '\0';
778 reply(257, "\"%s\" %s", npath, message);
779 }
780