extern.h revision 1.43.2.1 1 /* $NetBSD: extern.h,v 1.43.2.1 2004/08/31 01:35:26 jmc Exp $ */
2
3 /*-
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)extern.h 8.2 (Berkeley) 4/4/94
32 */
33
34 /*-
35 * Copyright (c) 1997-2004 The NetBSD Foundation, Inc.
36 * All rights reserved.
37 *
38 * This code is derived from software contributed to The NetBSD Foundation
39 * by Luke Mewburn.
40 *
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
43 * are met:
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. All advertising materials mentioning features or use of this software
50 * must display the following acknowledgement:
51 * This product includes software developed by the NetBSD
52 * Foundation, Inc. and its contributors.
53 * 4. Neither the name of The NetBSD Foundation nor the names of its
54 * contributors may be used to endorse or promote products derived
55 * from this software without specific prior written permission.
56 *
57 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
58 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
59 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
60 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
61 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
62 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
63 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
64 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
65 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
66 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
67 * POSSIBILITY OF SUCH DAMAGE.
68 */
69
70 /*
71 * Copyright (C) 1997 and 1998 WIDE Project.
72 * All rights reserved.
73 *
74 * Redistribution and use in source and binary forms, with or without
75 * modification, are permitted provided that the following conditions
76 * are met:
77 * 1. Redistributions of source code must retain the above copyright
78 * notice, this list of conditions and the following disclaimer.
79 * 2. Redistributions in binary form must reproduce the above copyright
80 * notice, this list of conditions and the following disclaimer in the
81 * documentation and/or other materials provided with the distribution.
82 * 3. Neither the name of the project nor the names of its contributors
83 * may be used to endorse or promote products derived from this software
84 * without specific prior written permission.
85 *
86 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
87 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
88 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
89 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
90 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
91 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
92 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
93 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
94 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
95 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
96 * SUCH DAMAGE.
97 */
98
99 #ifdef NO_LONG_LONG
100 # define LLF "%ld"
101 # define LLFP(x) "%" x "ld"
102 # define LLT long
103 # define ULLF "%lu"
104 # define ULLFP(x) "%" x "lu"
105 # define ULLT unsigned long
106 # define STRTOLL(x,y,z) strtol(x,y,z)
107 # define LLTMIN LONG_MIN
108 # define LLTMAX LONG_MAX
109 #else
110 # define LLF "%lld"
111 # define LLFP(x) "%" x "lld"
112 # define LLT long long
113 # define ULLF "%llu"
114 # define ULLFP(x) "%" x "llu"
115 # define ULLT unsigned long long
116 # define STRTOLL(x,y,z) strtoll(x,y,z)
117 # define LLTMIN LLONG_MIN
118 # define LLTMAX LLONG_MAX
119 #endif
120
121 #define FTP_BUFLEN 512
122
123 void abor(void);
124 void blkfree(char **);
125 void closedataconn(FILE *);
126 char *conffilename(const char *);
127 char **copyblk(char **);
128 void count_users(void);
129 void cprintf(FILE *, const char *, ...)
130 __attribute__((__format__(__printf__, 2, 3)));
131 void cwd(const char *);
132 FILE *dataconn(const char *, off_t, const char *);
133 void delete(const char *);
134 int display_file(const char *, int);
135 char **do_conversion(const char *);
136 void dologout(int);
137 void fatal(const char *);
138 void feat(void);
139 void format_path(char *, const char *);
140 int ftpd_pclose(FILE *);
141 FILE *ftpd_popen(char *[], const char *, int);
142 char *getline(char *, int, FILE *);
143 void init_curclass(void);
144 void logxfer(const char *, off_t, const char *, const char *,
145 const struct timeval *, const char *);
146 struct tab *lookup(struct tab *, const char *);
147 void makedir(const char *);
148 void mlsd(const char *);
149 void mlst(const char *);
150 void opts(const char *);
151 void parse_conf(const char *);
152 void pass(const char *);
153 void passive(void);
154 int lpsvproto2af(int);
155 int af2lpsvproto(int);
156 int epsvproto2af(int);
157 int af2epsvproto(int);
158 void long_passive(char *, int);
159 int extended_port(const char *);
160 void epsv_protounsupp(const char *);
161 void perror_reply(int, const char *);
162 void pwd(void);
163 void removedir(const char *);
164 void renamecmd(const char *, const char *);
165 char *renamefrom(const char *);
166 void reply(int, const char *, ...)
167 __attribute__((__format__(__printf__, 2, 3)));
168 void retrieve(char *[], const char *);
169 void send_file_list(const char *);
170 void show_chdir_messages(int);
171 void sizecmd(const char *);
172 void statcmd(void);
173 void statfilecmd(const char *);
174 void statxfer(void);
175 void store(const char *, const char *, int);
176 void user(const char *);
177 char *xstrdup(const char *);
178 void yyerror(char *);
179
180 long long strsuftoll(const char*, const char*, long long, long long);
181 long long strsuftollx(const char*, const char*, long long, long long, char*, size_t);
182
183 #ifdef SUPPORT_UTMP
184 struct utmp;
185
186 void ftpd_logwtmp(const char *, const char *, const char *);
187 void ftpd_login(const struct utmp *ut);
188 int ftpd_logout(const char *line);
189 #endif
190
191 #ifdef SUPPORT_UTMPX
192 struct utmpx;
193
194 void ftpd_loginx(const struct utmpx *);
195 void ftpd_logwtmpx(const char *, const char *, const char *, int, int);
196 #endif
197
198 #include <netinet/in.h>
199
200 #if defined(__NetBSD__)
201 # define HAVE_SETPROCTITLE 1
202 # define HAVE_SOCKADDR_SA_LEN 1
203 #endif
204
205 struct sockinet {
206 union sockunion {
207 struct sockaddr_in su_sin;
208 #ifdef INET6
209 struct sockaddr_in6 su_sin6;
210 #endif
211 } si_su;
212 #if !HAVE_SOCKADDR_SA_LEN
213 int si_len;
214 #endif
215 };
216
217 #if !HAVE_SOCKADDR_SA_LEN
218 # define su_len si_len
219 #else
220 # define su_len si_su.su_sin.sin_len
221 #endif
222 #define su_addr si_su.su_sin.sin_addr
223 #define su_family si_su.su_sin.sin_family
224 #define su_port si_su.su_sin.sin_port
225 #ifdef INET6
226 # define su_6addr si_su.su_sin6.sin6_addr
227 # define su_scope_id si_su.su_sin6.sin6_scope_id
228 #endif
229
230 struct tab {
231 char *name;
232 short token;
233 short state;
234 short flags; /* 1 if command implemented, 2 if has options,
235 4 if can occur OOB */
236 char *help;
237 char *options;
238 };
239
240 struct ftpconv {
241 struct ftpconv *next;
242 char *suffix; /* Suffix of requested name */
243 char *types; /* Valid file types */
244 char *disable; /* File to disable conversions */
245 char *command; /* Command to do the conversion */
246 };
247
248 typedef enum {
249 CLASS_GUEST,
250 CLASS_CHROOT,
251 CLASS_REAL
252 } class_ft;
253
254 typedef enum {
255 FLAG_checkportcmd = 1<<0, /* Check port commands */
256 FLAG_denyquick = 1<<1, /* Check ftpusers(5) before PASS */
257 FLAG_modify = 1<<2, /* Allow CHMOD, DELE, MKD, RMD, RNFR,
258 UMASK */
259 FLAG_passive = 1<<3, /* Allow PASV mode */
260 FLAG_private = 1<<4, /* Don't publish class info in STAT */
261 FLAG_sanenames = 1<<5, /* Restrict names of uploaded files */
262 FLAG_upload = 1<<6, /* As per modify, but also allow
263 APPE, STOR, STOU */
264 } classflag_t;
265
266 #define CURCLASS_FLAGS_SET(x) (curclass.flags |= (FLAG_ ## x))
267 #define CURCLASS_FLAGS_CLR(x) (curclass.flags &= ~(FLAG_ ## x))
268 #define CURCLASS_FLAGS_ISSET(x) (curclass.flags & (FLAG_ ## x))
269
270 struct ftpclass {
271 struct sockinet advertise; /* PASV address to advertise as */
272 char *chroot; /* Directory to chroot(2) to at login */
273 char *classname; /* Current class */
274 struct ftpconv *conversions; /* List of conversions */
275 char *display; /* File to display upon chdir */
276 char *homedir; /* Directory to chdir(2) to at login */
277 classflag_t flags; /* Flags; see classflag_t above */
278 LLT limit; /* Max connections (-1 = unlimited) */
279 char *limitfile; /* File to display if limit reached */
280 LLT maxfilesize; /* Maximum file size of uploads */
281 LLT maxrateget; /* Maximum get transfer rate throttle */
282 LLT maxrateput; /* Maximum put transfer rate throttle */
283 LLT maxtimeout; /* Maximum permitted timeout */
284 char *motd; /* MotD file to display after login */
285 char *notify; /* Files to notify about upon chdir */
286 LLT portmin; /* Minumum port for passive mode */
287 LLT portmax; /* Maximum port for passive mode */
288 LLT rateget; /* Get (RETR) transfer rate throttle */
289 LLT rateput; /* Put (STOR) transfer rate throttle */
290 LLT timeout; /* Default timeout */
291 class_ft type; /* Class type */
292 mode_t umask; /* Umask to use */
293 LLT mmapsize; /* mmap window size */
294 LLT readsize; /* data read size */
295 LLT writesize; /* data write size */
296 LLT sendbufsize; /* SO_SNDBUF size */
297 LLT sendlowat; /* SO_SNDLOWAT size */
298 };
299
300 extern void ftp_loop(void) __attribute__ ((noreturn));
301 extern void ftp_handle_line(char *);
302
303 #ifndef GLOBAL
304 #define GLOBAL extern
305 #endif
306
307
308 GLOBAL struct sockinet ctrl_addr;
309 GLOBAL struct sockinet data_dest;
310 GLOBAL struct sockinet data_source;
311 GLOBAL struct sockinet his_addr;
312 GLOBAL struct sockinet pasv_addr;
313 GLOBAL int connections;
314 GLOBAL struct ftpclass curclass;
315 GLOBAL int debug;
316 GLOBAL char *emailaddr;
317 GLOBAL int form;
318 GLOBAL int gidcount; /* number of entries in gidlist[] */
319 GLOBAL gid_t *gidlist;
320 GLOBAL int hasyyerrored;
321 GLOBAL char hostname[MAXHOSTNAMELEN+1];
322 GLOBAL char homedir[MAXPATHLEN];
323 #ifdef KERBEROS5
324 GLOBAL krb5_context kcontext;
325 #endif
326 GLOBAL int logged_in;
327 GLOBAL int logging;
328 GLOBAL int pdata; /* for passive mode */
329 #if HAVE_SETPROCTITLE
330 GLOBAL char proctitle[BUFSIZ]; /* initial part of title */
331 #endif
332 GLOBAL struct passwd *pw;
333 GLOBAL int quietmessages;
334 GLOBAL char remotehost[MAXHOSTNAMELEN+1];
335 GLOBAL off_t restart_point;
336 GLOBAL char tmpline[FTP_BUFLEN];
337 GLOBAL int type;
338 GLOBAL int usedefault; /* for data transfers */
339 GLOBAL const char *version;
340 GLOBAL int is_oob;
341
342 /* total file data bytes */
343 GLOBAL off_t total_data_in, total_data_out, total_data;
344 /* total number of data files */
345 GLOBAL off_t total_files_in, total_files_out, total_files;
346 /* total bytes */
347 GLOBAL off_t total_bytes_in, total_bytes_out, total_bytes;
348 /* total number of xfers */
349 GLOBAL off_t total_xfers_in, total_xfers_out, total_xfers;
350
351 extern struct tab cmdtab[];
352
353 #define INTERNAL_LS "/bin/ls"
354
355
356 #define CMD_IMPLEMENTED(x) ((x)->flags != 0)
357 #define CMD_HAS_OPTIONS(x) ((x)->flags & 0x2)
358 #define CMD_OOB(x) ((x)->flags & 0x4)
359
360 #define CPUTC(c, f) do { \
361 putc(c, f); total_bytes++; total_bytes_out++; \
362 } while (0);
363
364 #define CURCLASSTYPE curclass.type == CLASS_GUEST ? "GUEST" : \
365 curclass.type == CLASS_CHROOT ? "CHROOT" : \
366 curclass.type == CLASS_REAL ? "REAL" : \
367 "<unknown>"
368
369 #define ISDOTDIR(x) (x[0] == '.' && x[1] == '\0')
370 #define ISDOTDOTDIR(x) (x[0] == '.' && x[1] == '.' && x[2] == '\0')
371
372 #define EMPTYSTR(p) ((p) == NULL || *(p) == '\0')
373 #define NEXTWORD(P, W) do { \
374 (W) = strsep(&(P), " \t"); \
375 } while ((W) != NULL && *(W) == '\0')
376 #define PLURAL(s) ((s) == 1 ? "" : "s")
377 #define REASSIGN(X,Y) do { if (X) free(X); (X)=(Y); } while (/*CONSTCOND*/0)
378
379 #ifndef IPPORT_ANONMAX
380 # define IPPORT_ANONMAX 65535
381 #endif
382