Home | History | Annotate | Line # | Download | only in ftpd
extern.h revision 1.58
      1 /*	$NetBSD: extern.h,v 1.58 2008/09/13 03:30:35 lukem 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 #endif
    199 
    200 struct sockinet {
    201 	union sockunion {
    202 		struct sockaddr_in  su_sin;
    203 #ifdef INET6
    204 		struct sockaddr_in6 su_sin6;
    205 #endif
    206 	} si_su;
    207 #if !defined(HAVE_STRUCT_SOCKADDR_SA_LEN)
    208 	int	si_len;
    209 #endif
    210 };
    211 
    212 #if !defined(HAVE_STRUCT_SOCKADDR_SA_LEN)
    213 # define su_len		si_len
    214 #else
    215 # define su_len		si_su.su_sin.sin_len
    216 #endif
    217 #define su_addr		si_su.su_sin.sin_addr
    218 #define su_family	si_su.su_sin.sin_family
    219 #define su_port		si_su.su_sin.sin_port
    220 #ifdef INET6
    221 # define su_6addr	si_su.su_sin6.sin6_addr
    222 # define su_scope_id	si_su.su_sin6.sin6_scope_id
    223 #endif
    224 
    225 struct tab {
    226 	char	*name;
    227 	short	 token;
    228 	short	 state;
    229 	short	 flags;	/* 1 if command implemented, 2 if has options,
    230 	                   4 if can occur OOB */
    231 	char	*help;
    232 	char	*options;
    233 };
    234 
    235 struct ftpconv {
    236 	struct ftpconv	*next;
    237 	char		*suffix;	/* Suffix of requested name */
    238 	char		*types;		/* Valid file types */
    239 	char		*disable;	/* File to disable conversions */
    240 	char		*command;	/* Command to do the conversion */
    241 };
    242 
    243 typedef enum {
    244 	CLASS_GUEST,
    245 	CLASS_CHROOT,
    246 	CLASS_REAL
    247 } class_ft;
    248 
    249 typedef enum {
    250 	FLAG_checkportcmd =	1<<0,	/* Check port commands */
    251 	FLAG_denyquick =	1<<1,	/* Check ftpusers(5) before PASS */
    252 	FLAG_hidesymlinks =	1<<2,	/* For symbolic links, list the file
    253 					   or directory the link references
    254 					   rather than the link itself */
    255 	FLAG_modify =		1<<3,	/* Allow CHMOD, DELE, MKD, RMD, RNFR,
    256 					   UMASK */
    257 	FLAG_passive =		1<<4,	/* Allow PASV mode */
    258 	FLAG_private =		1<<5,	/* Don't publish class info in STAT */
    259 	FLAG_sanenames =	1<<6,	/* Restrict names of uploaded files */
    260 	FLAG_upload =		1<<7,	/* As per modify, but also allow
    261 					   APPE, STOR, STOU */
    262 } classflag_t;
    263 
    264 #define CURCLASS_FLAGS_SET(x)	(curclass.flags |=  (FLAG_ ## x))
    265 #define CURCLASS_FLAGS_CLR(x)	(curclass.flags &= ~(FLAG_ ## x))
    266 #define CURCLASS_FLAGS_ISSET(x)	(curclass.flags &   (FLAG_ ## x))
    267 
    268 struct ftpclass {
    269 	struct sockinet	 advertise;	/* PASV address to advertise as */
    270 	char		*chroot;	/* Directory to chroot(2) to at login */
    271 	char		*classname;	/* Current class */
    272 	struct ftpconv	*conversions;	/* List of conversions */
    273 	char		*display;	/* File to display upon chdir */
    274 	char		*homedir;	/* Directory to chdir(2) to at login */
    275 	classflag_t	 flags;		/* Flags; see classflag_t above */
    276 	LLT		 limit;		/* Max connections (-1 = unlimited) */
    277 	char		*limitfile;	/* File to display if limit reached */
    278 	LLT		 maxfilesize;	/* Maximum file size of uploads */
    279 	LLT		 maxrateget;	/* Maximum get transfer rate throttle */
    280 	LLT		 maxrateput;	/* Maximum put transfer rate throttle */
    281 	LLT		 maxtimeout;	/* Maximum permitted timeout */
    282 	char		*motd;		/* MotD file to display after login */
    283 	char		*notify;	/* Files to notify about upon chdir */
    284 	LLT		 portmin;	/* Minumum port for passive mode */
    285 	LLT		 portmax;	/* Maximum port for passive mode */
    286 	LLT		 rateget;	/* Get (RETR) transfer rate throttle */
    287 	LLT		 rateput;	/* Put (STOR) transfer rate throttle */
    288 	LLT		 timeout;	/* Default timeout */
    289 	class_ft	 type;		/* Class type */
    290 	mode_t		 umask;		/* Umask to use */
    291 	LLT		 mmapsize;	/* mmap window size */
    292 	LLT		 readsize;	/* data read size */
    293 	LLT		 writesize;	/* data write size */
    294 	LLT		 recvbufsize;	/* SO_RCVBUF size */
    295 	LLT		 sendbufsize;	/* SO_SNDBUF size */
    296 	LLT		 sendlowat;	/* SO_SNDLOWAT size */
    297 };
    298 
    299 extern void		ftp_loop(void) __attribute__ ((noreturn));
    300 extern void		ftp_handle_line(char *);
    301 
    302 #ifndef	GLOBAL
    303 #define	GLOBAL	extern
    304 #endif
    305 
    306 
    307 GLOBAL	struct sockinet ctrl_addr;
    308 GLOBAL	struct sockinet	data_dest;
    309 GLOBAL	struct sockinet	data_source;
    310 GLOBAL	struct sockinet	his_addr;
    311 GLOBAL	struct sockinet	pasv_addr;
    312 GLOBAL	int		connections;
    313 GLOBAL	struct ftpclass	curclass;
    314 GLOBAL	int		ftpd_debug;
    315 GLOBAL	char		*emailaddr;
    316 GLOBAL	int		form;
    317 GLOBAL	int		gidcount;	/* number of entries in gidlist[] */
    318 GLOBAL	gid_t		*gidlist;
    319 GLOBAL	int		hasyyerrored;
    320 GLOBAL	char		hostname[MAXHOSTNAMELEN+1];
    321 GLOBAL	char		homedir[MAXPATHLEN];
    322 #ifdef KERBEROS5
    323 GLOBAL	krb5_context	kcontext;
    324 #endif
    325 GLOBAL	int		logged_in;
    326 GLOBAL	int		logging;
    327 GLOBAL	int		pdata;			/* for passive mode */
    328 #if defined(HAVE_SETPROCTITLE)
    329 GLOBAL	char		proctitle[BUFSIZ];	/* initial part of title */
    330 #endif
    331 GLOBAL	struct passwd  *pw;
    332 GLOBAL	int		quietmessages;
    333 GLOBAL	char		remotehost[MAXHOSTNAMELEN+1];
    334 GLOBAL	off_t		restart_point;
    335 GLOBAL	char		tmpline[FTP_BUFLEN];
    336 GLOBAL	int		type;
    337 GLOBAL	int		usedefault;		/* for data transfers */
    338 GLOBAL	const char     *version;
    339 GLOBAL	int		is_oob;
    340 
    341 						/* total file data bytes */
    342 GLOBAL	off_t		total_data_in,  total_data_out,  total_data;
    343 						/* total number of data files */
    344 GLOBAL	off_t		total_files_in, total_files_out, total_files;
    345 						/* total bytes */
    346 GLOBAL	off_t		total_bytes_in, total_bytes_out, total_bytes;
    347 						/* total number of xfers */
    348 GLOBAL	off_t		total_xfers_in, total_xfers_out, total_xfers;
    349 
    350 extern	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