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