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