1/*	$NetBSD: extern.h,v 1.66 2022/04/17 21:24:53 andvar 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-2009 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
116void	abor(void);
117void	closedataconn(FILE *);
118char   *conffilename(const char *);
119void	count_users(void);
120void	cprintf(FILE *, const char *, ...)
121	    __printflike(2, 3);
122void	cwd(const char *);
123FILE   *dataconn(const char *, off_t, const char *);
124void	delete(const char *);
125int	display_file(const char *, int);
126const char **do_conversion(const char *);
127__dead void	dologout(int);
128__dead void	fatal(const char *);
129void	feat(void);
130void	format_path(char *, const char *);
131int	ftpd_pclose(FILE *);
132FILE   *ftpd_popen(const char *[], const char *, int);
133int	get_line(char *, int, FILE *);
134void	init_curclass(void);
135void	logxfer(const char *, off_t, const char *, const char *,
136	    const struct timeval *, const char *);
137struct tab *lookup(struct tab *, const char *);
138void	makedir(const char *);
139void	mlsd(const char *);
140void	mlst(const char *);
141void	opts(const char *);
142void	parse_conf(const char *);
143void	pass(const char *);
144void	passive(void);
145int	lpsvproto2af(int);
146int	af2lpsvproto(int);
147int	epsvproto2af(int);
148int	af2epsvproto(int);
149void	long_passive(const char *, int);
150int	extended_port(const char *);
151void	epsv_protounsupp(const char *);
152void	perror_reply(int, const char *);
153void	pwd(void);
154void	removedir(const char *);
155void	renamecmd(const char *, const char *);
156char   *renamefrom(const char *);
157void	reply(int, const char *, ...)
158	    __printflike(2, 3);
159void	retrieve(const char *[], const char *);
160void	send_file_list(const char *);
161void	show_chdir_messages(int);
162void	sizecmd(const char *);
163void	statcmd(void);
164void	statfilecmd(const char *);
165void	statxfer(void);
166void	store(const char *, const char *, int);
167void	user(const char *);
168char   *ftpd_strdup(const char *);
169void	yyerror(const char *);
170
171#ifdef SUPPORT_UTMP
172struct utmp;
173
174void	ftpd_initwtmp(void);
175void	ftpd_logwtmp(const char *, const char *, const char *);
176void	ftpd_login(const struct utmp *);
177int	ftpd_logout(const char *);
178#endif
179
180#ifdef SUPPORT_UTMPX
181struct utmpx;
182struct sockinet;
183
184void	ftpd_initwtmpx(void);
185void	ftpd_logwtmpx(const char *, const char *, const char *,
186    struct sockinet *, int, int);
187void	ftpd_loginx(const struct utmpx *);
188int	ftpd_logoutx(const char *, int, int);
189#endif
190
191#include <netinet/in.h>
192
193#if defined(__NetBSD__)
194# define HAVE_SETPROCTITLE		1
195# define HAVE_STRUCT_SOCKADDR_SA_LEN	1
196# define HAVE_SOCKADDR_SNPRINTF		1
197#endif
198
199struct sockinet {
200	union sockunion {
201		struct sockaddr_in  su_sin;
202#ifdef INET6
203		struct sockaddr_in6 su_sin6;
204#endif
205	} si_su;
206#if !defined(HAVE_STRUCT_SOCKADDR_SA_LEN)
207	int	si_len;
208#endif
209};
210
211#if !defined(HAVE_STRUCT_SOCKADDR_SA_LEN)
212# define su_len		si_len
213#else
214# define su_len		si_su.su_sin.sin_len
215#endif
216#define su_addr		si_su.su_sin.sin_addr
217#define su_family	si_su.su_sin.sin_family
218#define su_port		si_su.su_sin.sin_port
219#ifdef INET6
220# define su_6addr	si_su.su_sin6.sin6_addr
221# define su_scope_id	si_su.su_sin6.sin6_scope_id
222#endif
223
224struct tab {
225	const char	*name;
226	short	 	token;
227	short	 	state;
228	short	 	flags;	/* 1 if command implemented, 2 if has options,
229				   4 if can occur OOB */
230	const char	*help;
231	char		*options;
232};
233
234struct ftpconv {
235	struct ftpconv	*next;
236	char 		*suffix;	/* Suffix of requested name */
237	char		*types;		/* Valid file types */
238	char		*disable;	/* File to disable conversions */
239	char		*command;	/* Command to do the conversion */
240};
241
242typedef enum {
243	CLASS_GUEST,
244	CLASS_CHROOT,
245	CLASS_REAL
246} class_ft;
247
248typedef enum {
249	FLAG_checkportcmd =	1<<0,	/* Check port commands */
250	FLAG_denyquick =	1<<1,	/* Check ftpusers(5) before PASS */
251	FLAG_hidesymlinks =	1<<2,	/* For symbolic links, list the file
252					   or directory the link references
253					   rather than the link itself */
254	FLAG_modify =		1<<3,	/* Allow CHMOD, DELE, MKD, RMD, RNFR,
255					   UMASK */
256	FLAG_passive =		1<<4,	/* Allow PASV mode */
257	FLAG_private =		1<<5,	/* Don't publish class info in STAT */
258	FLAG_sanenames =	1<<6,	/* Restrict names of uploaded files */
259	FLAG_upload =		1<<7,	/* As per modify, but also allow
260					   APPE, STOR, STOU */
261} classflag_t;
262
263#define CURCLASS_FLAGS_SET(x)	(curclass.flags |=  (FLAG_ ## x))
264#define CURCLASS_FLAGS_CLR(x)	(curclass.flags &= ~(FLAG_ ## x))
265#define CURCLASS_FLAGS_ISSET(x)	(curclass.flags &   (FLAG_ ## x))
266
267struct ftpclass {
268	struct sockinet	 advertise;	/* PASV address to advertise as */
269	char		*chroot;	/* Directory to chroot(2) to at login */
270	char		*classname;	/* Current class */
271	struct ftpconv	*conversions;	/* List of conversions */
272	char		*display;	/* File to display upon chdir */
273	char		*homedir;	/* Directory to chdir(2) to at login */
274	classflag_t	 flags;		/* Flags; see classflag_t above */
275	LLT		 limit;		/* Max connections (-1 = unlimited) */
276	char		*limitfile;	/* File to display if limit reached */
277	LLT		 maxfilesize;	/* Maximum file size of uploads */
278	LLT		 maxrateget;	/* Maximum get transfer rate throttle */
279	LLT		 maxrateput;	/* Maximum put transfer rate throttle */
280	LLT		 maxtimeout;	/* Maximum permitted timeout */
281	char		*motd;		/* MotD file to display after login */
282	char		*notify;	/* Files to notify about upon chdir */
283	LLT		 portmin;	/* Minimum port for passive mode */
284	LLT		 portmax;	/* Maximum port for passive mode */
285	LLT		 rateget;	/* Get (RETR) transfer rate throttle */
286	LLT		 rateput;	/* Put (STOR) transfer rate throttle */
287	LLT		 timeout;	/* Default timeout */
288	class_ft	 type;		/* Class type */
289	mode_t		 umask;		/* Umask to use */
290	LLT		 mmapsize;	/* mmap window size */
291	LLT		 readsize;	/* data read size */
292	LLT		 writesize;	/* data write size */
293	LLT		 recvbufsize;	/* SO_RCVBUF size */
294	LLT		 sendbufsize;	/* SO_SNDBUF size */
295	LLT		 sendlowat;	/* SO_SNDLOWAT size */
296};
297
298__dead extern void		ftp_loop(void);
299extern void		ftp_handle_line(char *);
300
301#ifndef	GLOBAL
302#define	GLOBAL	extern
303#endif
304
305
306GLOBAL	struct sockinet ctrl_addr;
307GLOBAL	struct sockinet	data_dest;
308GLOBAL	struct sockinet	data_source;
309GLOBAL	struct sockinet	his_addr;
310GLOBAL	struct sockinet	pasv_addr;
311GLOBAL	int		connections;
312GLOBAL	struct ftpclass	curclass;
313GLOBAL	int		ftpd_debug;
314GLOBAL	char		*emailaddr;
315GLOBAL	int		form;
316GLOBAL	int		gidcount;	/* number of entries in gidlist[] */
317GLOBAL	gid_t		*gidlist;
318GLOBAL	int		hasyyerrored;
319GLOBAL	char		hostname[MAXHOSTNAMELEN+1];
320GLOBAL	char		homedir[MAXPATHLEN];
321#ifdef KERBEROS5
322GLOBAL	krb5_context	kcontext;
323#endif
324GLOBAL	int		logged_in;
325GLOBAL	int		logging;
326GLOBAL	int		pdata;			/* for passive mode */
327#if defined(HAVE_SETPROCTITLE)
328GLOBAL	char		proctitle[BUFSIZ];	/* initial part of title */
329#endif
330GLOBAL	struct passwd  *pw;
331GLOBAL	int		quietmessages;
332GLOBAL	char		remotehost[MAXHOSTNAMELEN+1];
333GLOBAL	char		remoteloghost[2 * MAXHOSTNAMELEN + 4];
334GLOBAL	off_t		restart_point;
335GLOBAL	char		tmpline[FTP_BUFLEN];
336GLOBAL	int		type;
337GLOBAL	int		usedefault;		/* for data transfers */
338GLOBAL	const char     *version;
339GLOBAL	int		is_oob;
340
341						/* total file data bytes */
342GLOBAL	off_t		total_data_in,  total_data_out,  total_data;
343						/* total number of data files */
344GLOBAL	off_t		total_files_in, total_files_out, total_files;
345						/* total bytes */
346GLOBAL	off_t		total_bytes_in, total_bytes_out, total_bytes;
347						/* total number of xfers */
348GLOBAL	off_t		total_xfers_in, total_xfers_out, total_xfers;
349
350extern	struct tab	cmdtab[];
351
352#define	INTERNAL_LS	"/bin/ls"
353
354
355#define CMD_IMPLEMENTED(x)	((x)->flags != 0)
356#define CMD_HAS_OPTIONS(x)	((x)->flags & 0x2)
357#define CMD_OOB(x)		((x)->flags & 0x4)
358
359#define	CPUTC(c, f)	do { \
360				putc(c, f); total_bytes++; total_bytes_out++; \
361			} while (0)
362
363#define CURCLASSTYPE	curclass.type == CLASS_GUEST  ? "GUEST"  : \
364			curclass.type == CLASS_CHROOT ? "CHROOT" : \
365			curclass.type == CLASS_REAL   ? "REAL"   : \
366			"<unknown>"
367
368#define ISDOTDIR(x)	(x[0] == '.' && x[1] == '\0')
369#define ISDOTDOTDIR(x)	(x[0] == '.' && x[1] == '.' && x[2] == '\0')
370
371#define EMPTYSTR(p)	((p) == NULL || *(p) == '\0')
372#define NEXTWORD(P, W)	do { \
373				(W) = strsep(&(P), " \t"); \
374			} while ((W) != NULL && *(W) == '\0')
375#define PLURAL(s)	((s) == 1 ? "" : "s")
376#define REASSIGN(X,Y)	do { if (X) free(X); (X)=(Y); } while (/*CONSTCOND*/0)
377
378#ifndef IPPORT_ANONMAX
379# define IPPORT_ANONMAX	65535
380#endif
381