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