xref: /freebsd/contrib/tnftp/tnftp.h (revision f4c129f5fb1669b284d76a838b1e509f97c2d299)
1 /*	$NetBSD: tnftp.h,v 1.33 2009/11/14 08:32:42 lukem Exp $	*/
2 
3 #define	FTP_PRODUCT	PACKAGE_NAME
4 #define	FTP_VERSION	PACKAGE_VERSION
5 
6 #include "tnftp_config.h"
7 
8 #include <stdio.h>
9 #include <ctype.h>
10 #include <errno.h>
11 
12 #if defined(HAVE_SYS_TYPES_H)
13 # include <sys/types.h>
14 #endif
15 #if defined(STDC_HEADERS)
16 # include <stdarg.h>
17 # include <stdlib.h>
18 # include <string.h>
19 #endif
20 #if defined(HAVE_LIBGEN_H)
21 # include <libgen.h>
22 #endif
23 #if defined(HAVE_UNISTD_H)
24 # include <unistd.h>
25 #endif
26 #if defined(HAVE_POLL_H)
27 # include <poll.h>
28 #elif defined(HAVE_SYS_POLL_H)
29 # include <sys/poll.h>
30 #endif
31 #if defined(HAVE_SYS_SOCKET_H)
32 # include <sys/socket.h>
33 #endif
34 #if defined(HAVE_NETINET_IN_H)
35 # include <netinet/in.h>
36 #endif
37 #if defined(HAVE_NETINET_IN_SYSTM_H)
38 # include <netinet/in_systm.h>
39 #endif
40 #if defined(HAVE_NETINET_IP_H)
41 # include <netinet/ip.h>
42 #endif
43 #if defined(HAVE_NETDB_H)
44 # if HAVE_DECL_AI_NUMERICHOST
45 #  include <netdb.h>
46 # else	/* !HAVE_DECL_AI_NUMERICHOST */
47 #  define getaddrinfo non_rfc2553_getaddrinfo
48 #  include <netdb.h>
49 #  undef getaddrinfo
50 # endif	/* !HAVE_DECL_AI_NUMERICHOST */
51 #endif
52 #if defined(HAVE_ARPA_INET_H)
53 # include <arpa/inet.h>
54 #endif
55 #if defined(HAVE_DIRENT_H)
56 # include <dirent.h>
57 #else
58 # define dirent direct
59 # if defined(HAVE_SYS_NDIR_H)
60 #  include <sys/ndir.h>
61 # endif
62 # if defined(HAVE_SYS_DIR_H)
63 #  include <sys/dir.h>
64 # endif
65 # if defined(HAVE_NDIR_H)
66 #  include <ndir.h>
67 # endif
68 #endif
69 
70 #if defined(HAVE_SYS_IOCTL_H)
71 # include <sys/ioctl.h>
72 #endif
73 #if defined(HAVE_SYS_PARAM_H)
74 # include <sys/param.h>
75 #endif
76 #if defined(HAVE_SYS_STAT_H)
77 # include <sys/stat.h>
78 #endif
79 #if defined(HAVE_SYS_SYSLIMITS_H)
80 # include <sys/syslimits.h>
81 #endif
82 #if defined(HAVE_SYS_WAIT_H)
83 # include <sys/wait.h>
84 #endif
85 
86 #if defined(HAVE_ARPA_FTP_H)
87 # include <arpa/ftp.h>
88 #endif
89 
90 #if defined(HAVE_FCNTL_H)
91 # include <fcntl.h>
92 #endif
93 #if defined(HAVE_LIMITS_H)
94 # include <limits.h>
95 #endif
96 #if defined(HAVE_PWD_H)
97 # include <pwd.h>
98 #endif
99 #if defined(HAVE_SETJMP_H)
100 # include <setjmp.h>
101 #endif
102 #if defined(HAVE_SIGNAL_H)
103 # include <signal.h>
104 #endif
105 #if defined(HAVE_STDDEF_H)
106 # include <stddef.h>
107 #endif
108 #if defined(HAVE_TERMIOS_H)
109 # include <termios.h>
110 #endif
111 
112 #if defined(HAVE_POLL)
113 /* we use poll */
114 #elif defined(HAVE_SELECT)
115 /* we use select */
116 #else /* !defined(HAVE_POLL) && !defined(HAVE_SELECT) */
117 # error "no poll() or select() found"
118 #endif
119 #if !defined(POLLIN)
120 # define POLLIN		0x0001
121 #endif
122 #if !defined(POLLOUT)
123 # define POLLOUT	0x0004
124 #endif
125 #if !defined(POLLRDNORM)
126 # define POLLRDNORM	0x0040
127 #endif
128 #if !defined(POLLWRNORM)
129 # define POLLWRNORM	POLLOUT
130 #endif
131 #if !defined(POLLRDBAND)
132 # define POLLRDBAND	0x0080
133 #endif
134 #if !defined(INFTIM)
135 # define INFTIM -1
136 #endif
137 #if !defined(HAVE_STRUCT_POLLFD)
138 struct pollfd {
139 	int	fd;
140 	short	events;
141 	short	revents;
142 };
143 #endif
144 
145 #if defined(TIME_WITH_SYS_TIME)
146 # include <sys/time.h>
147 # include <time.h>
148 #else
149 # if defined(HAVE_SYS_TIME_H)
150 #  include <sys/time.h>
151 # else
152 #  include <time.h>
153 # endif
154 #endif
155 
156 #if defined(HAVE_ERR_H)
157 # include <err.h>
158 #endif
159 
160 #if defined(USE_GLOB_H)	/* not set by configure; used by other build systems */
161 # include <glob.h>
162 #else
163 # include "ftpglob.h"
164 #endif
165 
166 #if defined(HAVE_PATHS_H)
167 # include <paths.h>
168 #endif
169 #if !defined(_PATH_BSHELL)
170 # define _PATH_BSHELL	"/bin/sh"
171 #endif
172 #if !defined(_PATH_TMP)
173 # define _PATH_TMP	"/tmp/"
174 #endif
175 
176 typedef struct _stringlist {
177 	char	**sl_str;
178 	size_t	  sl_max;
179 	size_t	  sl_cur;
180 } StringList;
181 
182 StringList *sl_init(void);
183 int	 sl_add(StringList *, char *);
184 void	 sl_free(StringList *, int);
185 char	*sl_find(StringList *, char *);
186 
187 #if defined(HAVE_TERMCAP_H)
188 # include <termcap.h>
189 #else
190 int	 tgetent(char *, const char *);
191 char	*tgetstr(const char *, char **);
192 int	 tgetflag(const char *);
193 int	 tgetnum(const char *);
194 char	*tgoto(const char *, int, int);
195 void	 tputs(const char *, int, int (*)(int));
196 #endif /* !HAVE_TERMCAP_H */
197 
198 #if defined(HAVE_VIS_H) && defined(HAVE_STRVIS) && defined(HAVE_STRUNVIS)
199 # include <vis.h>
200 #else
201 # include "ftpvis.h"
202 #endif
203 
204 #if !defined(HAVE_IN_PORT_T)
205 typedef unsigned short in_port_t;
206 #endif
207 
208 #if !defined(HAVE_SA_FAMILY_T)
209 typedef unsigned short sa_family_t;
210 #endif
211 
212 #if !defined(HAVE_SOCKLEN_T)
213 typedef unsigned int socklen_t;
214 #endif
215 
216 #if defined(USE_INET6)
217 # define INET6
218 #endif
219 
220 #if !HAVE_DECL_AI_NUMERICHOST
221 
222 				/* RFC 2553 */
223 #undef	EAI_ADDRFAMILY
224 #define	EAI_ADDRFAMILY	 1	/* address family for hostname not supported */
225 #undef	EAI_AGAIN
226 #define	EAI_AGAIN	 2	/* temporary failure in name resolution */
227 #undef	EAI_BADFLAGS
228 #define	EAI_BADFLAGS	 3	/* invalid value for ai_flags */
229 #undef	EAI_FAIL
230 #define	EAI_FAIL	 4	/* non-recoverable failure in name resolution */
231 #undef	EAI_FAMILY
232 #define	EAI_FAMILY	 5	/* ai_family not supported */
233 #undef	EAI_MEMORY
234 #define	EAI_MEMORY	 6	/* memory allocation failure */
235 #undef	EAI_NODATA
236 #define	EAI_NODATA	 7	/* no address associated with hostname */
237 #undef	EAI_NONAME
238 #define	EAI_NONAME	 8	/* hostname nor servname provided, or not known */
239 #undef	EAI_SERVICE
240 #define	EAI_SERVICE	 9	/* servname not supported for ai_socktype */
241 #undef	EAI_SOCKTYPE
242 #define	EAI_SOCKTYPE	10	/* ai_socktype not supported */
243 #undef	EAI_SYSTEM
244 #define	EAI_SYSTEM	11	/* system error returned in errno */
245 
246 				/* KAME extensions? */
247 #undef	EAI_BADHINTS
248 #define	EAI_BADHINTS	12
249 #undef	EAI_PROTOCOL
250 #define	EAI_PROTOCOL	13
251 #undef	EAI_MAX
252 #define	EAI_MAX		14
253 
254 				/* RFC 2553 */
255 #undef	NI_MAXHOST
256 #define	NI_MAXHOST	1025
257 #undef	NI_MAXSERV
258 #define	NI_MAXSERV	32
259 
260 #undef	NI_NOFQDN
261 #define	NI_NOFQDN	0x00000001
262 #undef	NI_NUMERICHOST
263 #define	NI_NUMERICHOST	0x00000002
264 #undef	NI_NAMEREQD
265 #define	NI_NAMEREQD	0x00000004
266 #undef	NI_NUMERICSERV
267 #define	NI_NUMERICSERV	0x00000008
268 #undef	NI_DGRAM
269 #define	NI_DGRAM	0x00000010
270 
271 				/* RFC 2553 */
272 #undef	AI_PASSIVE
273 #define	AI_PASSIVE	0x00000001 /* get address to use bind() */
274 #undef	AI_CANONNAME
275 #define	AI_CANONNAME	0x00000002 /* fill ai_canonname */
276 
277 				/* KAME extensions ? */
278 #undef	AI_NUMERICHOST
279 #define	AI_NUMERICHOST	0x00000004 /* prevent name resolution */
280 #undef	AI_MASK
281 #define	AI_MASK		(AI_PASSIVE | AI_CANONNAME | AI_NUMERICHOST)
282 
283 				/* RFC 2553 */
284 #undef	AI_ALL
285 #define	AI_ALL		0x00000100 /* IPv6 and IPv4-mapped (with AI_V4MAPPED) */
286 #undef	AI_V4MAPPED_CFG
287 #define	AI_V4MAPPED_CFG	0x00000200 /* accept IPv4-mapped if kernel supports */
288 #undef	AI_ADDRCONFIG
289 #define	AI_ADDRCONFIG	0x00000400 /* only if any address is assigned */
290 #undef	AI_V4MAPPED
291 #define	AI_V4MAPPED	0x00000800 /* accept IPv4-mapped IPv6 address */
292 
293 #endif /* !HAVE_DECL_AI_NUMERICHOST */
294 
295 
296 #if !HAVE_DECL_AI_NUMERICHOST && !defined(HAVE_STRUCT_ADDRINFO) \
297     && !defined(USE_SOCKS)
298 
299 struct addrinfo {
300 	int		ai_flags;	/* AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST */
301 	int		ai_family;	/* PF_xxx */
302 	int		ai_socktype;	/* SOCK_xxx */
303 	int		ai_protocol;	/* 0 or IPPROTO_xxx for IPv4 and IPv6 */
304 	socklen_t	ai_addrlen;	/* length of ai_addr */
305 	char		*ai_canonname;	/* canonical name for hostname */
306 	struct sockaddr *ai_addr;	/* binary address */
307 	struct addrinfo *ai_next;	/* next structure in linked list */
308 };
309 
310 int	getaddrinfo(const char *, const char *,
311 	    const struct addrinfo *, struct addrinfo **);
312 int	getnameinfo(const struct sockaddr *, socklen_t,
313 	    char *, size_t, char *, size_t, int);
314 void	freeaddrinfo(struct addrinfo *);
315 const char *gai_strerror(int);
316 
317 #endif /* !HAVE_DECL_AI_NUMERICHOST && !defined(HAVE_STRUCT_ADDRINFO) \
318 	&& !defined(USE_SOCKS) */
319 
320 #if !defined(HAVE_STRUCT_DIRENT_D_NAMLEN)
321 # define DIRENT_MISSING_D_NAMLEN
322 #endif
323 
324 #if !HAVE_DECL_H_ERRNO
325 extern int	h_errno;
326 #endif
327 #define HAVE_H_ERRNO	1		/* XXX: an assumption for now... */
328 
329 #if !HAVE_DECL_FCLOSE
330 int	fclose(FILE *);
331 #endif
332 
333 #if !HAVE_DECL_GETPASS
334 char	*getpass(const char *);
335 #endif
336 
337 #if !HAVE_DECL_OPTARG
338 extern char    *optarg;
339 #endif
340 
341 #if !HAVE_DECL_OPTIND
342 extern int	optind;
343 #endif
344 
345 #if !HAVE_DECL_PCLOSE
346 int	pclose(FILE *);
347 #endif
348 
349 #if !HAVE_DECL_DIRNAME
350 char	*dirname(char *);
351 #endif
352 
353 #if !defined(HAVE_ERR)
354 void	err(int, const char *, ...);
355 void	errx(int, const char *, ...);
356 void	warn(const char *, ...);
357 void	warnx(const char *, ...);
358 #endif
359 
360 #if !defined(HAVE_FGETLN)
361 char   *fgetln(FILE *, size_t *);
362 #endif
363 
364 #if !defined(HAVE_FSEEKO)
365 int	fseeko(FILE *, off_t, int);
366 #endif
367 
368 #if !defined(HAVE_INET_NTOP)
369 const char *inet_ntop(int, const void *, char *, socklen_t);
370 #endif
371 
372 #if !defined(HAVE_INET_PTON)
373 int inet_pton(int, const char *, void *);
374 #endif
375 
376 #if !defined(HAVE_MKSTEMP)
377 int	mkstemp(char *);
378 #endif
379 
380 #if !defined(HAVE_SETPROGNAME)
381 const char *getprogname(void);
382 void	setprogname(const char *);
383 #endif
384 
385 #if !defined(HAVE_SNPRINTF)
386 int	snprintf(char *, size_t, const char *, ...);
387 #endif
388 
389 #if !defined(HAVE_STRDUP)
390 char   *strdup(const char *);
391 #endif
392 
393 #if !defined(HAVE_STRERROR)
394 char   *strerror(int);
395 #endif
396 
397 #if !defined(HAVE_STRPTIME) || !HAVE_DECL_STRPTIME
398 char   *strptime(const char *, const char *, struct tm *);
399 #endif
400 
401 #if defined(HAVE_PRINTF_LONG_LONG) && defined(HAVE_LONG_LONG_INT)
402 # if !defined(HAVE_STRTOLL)
403 long long strtoll(const char *, char **, int);
404 # endif
405 # if !defined(LLONG_MIN)
406 #  define LLONG_MIN	(-0x7fffffffffffffffLL-1)
407 # endif
408 # if !defined(LLONG_MAX)
409 #  define LLONG_MAX	(0x7fffffffffffffffLL)
410 # endif
411 #else  /* !(defined(HAVE_PRINTF_LONG_LONG) && defined(HAVE_LONG_LONG_INT)) */
412 # define NO_LONG_LONG	1
413 #endif /* !(defined(HAVE_PRINTF_LONG_LONG) && defined(HAVE_LONG_LONG_INT)) */
414 
415 #if !defined(HAVE_TIMEGM)
416 time_t	timegm(struct tm *);
417 #endif
418 
419 #if !defined(HAVE_STRLCAT)
420 size_t	strlcat(char *, const char *, size_t);
421 #endif
422 
423 #if !defined(HAVE_STRLCPY)
424 size_t	strlcpy(char *, const char *, size_t);
425 #endif
426 
427 #if !defined(HAVE_STRSEP)
428 char   *strsep(char **stringp, const char *delim);
429 #endif
430 
431 #if !defined(HAVE_UTIMES)
432 int utimes(const char *, const struct timeval *);
433 #endif
434 
435 #if !defined(HAVE_MEMMOVE)
436 # define memmove(a,b,c)	bcopy((b),(a),(c))
437 	/* XXX: add others #defines for borken systems? */
438 #endif
439 
440 #if defined(HAVE_GETPASSPHRASE)
441 # define getpass getpassphrase
442 #endif
443 
444 #if !defined(MIN)
445 # define MIN(a, b)	((a) < (b) ? (a) : (b))
446 #endif
447 #if !defined(MAX)
448 # define MAX(a, b)	((a) < (b) ? (b) : (a))
449 #endif
450 
451 #if !defined(timersub)
452 # define timersub(tvp, uvp, vvp)					\
453 	do {								\
454 		(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;		\
455 		(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;	\
456 		if ((vvp)->tv_usec < 0) {				\
457 			(vvp)->tv_sec--;				\
458 			(vvp)->tv_usec += 1000000;			\
459 		}							\
460 	} while (0)
461 #endif
462 
463 #if !defined(S_ISLNK)
464 # define S_ISLNK(m)	((m & S_IFMT) == S_IFLNK)
465 #endif
466 
467 #define	EPOCH_YEAR	1970
468 #define	SECSPERHOUR	3600
469 #define	SECSPERDAY	86400
470 #define	TM_YEAR_BASE	1900
471 
472 #if defined(USE_SOCKS)		/* (Dante) SOCKS5 */
473 #define connect		Rconnect
474 #define bind		Rbind
475 #define getsockname	Rgetsockname
476 #define getpeername	Rgetpeername
477 #define accept		Raccept
478 #define rresvport	Rrresvport
479 #define bindresvport	Rbindresvport
480 #define gethostbyname	Rgethostbyname
481 #define gethostbyname2	Rgethostbyname2
482 #define sendto		Rsendto
483 #define recvfrom	Rrecvfrom
484 #define recvfrom	Rrecvfrom
485 #define write		Rwrite
486 #define writev		Rwritev
487 #define send		Rsend
488 #define sendmsg		Rsendmsg
489 #define read		Rread
490 #define readv		Rreadv
491 #define recv		Rrecv
492 #define recvmsg		Rrecvmsg
493 #define getaddrinfo	Rgetaddrinfo
494 #define getipnodebyname	Rgetipnodebyname
495 #endif /* defined(USE_SOCKS) */
496