1 #ifndef __FTPD_H__
2 #define __FTPD_H__ 1
3 
4 #ifndef __GNUC__
5 # ifdef __attribute__
6 #  undef __attribute__
7 # endif
8 # define __attribute__(a)
9 #endif
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <stddef.h>
14 #include <stdarg.h>
15 #include <sys/types.h>
16 #ifdef HAVE_STDINT_H
17 # include <stdint.h>
18 #endif
19 #ifdef HAVE_LOCALE_H
20 # include <locale.h>
21 #endif
22 #ifdef HAVE_STRING_H
23 # if !STDC_HEADERS && HAVE_MEMORY_H
24 #  include <memory.h>
25 # endif
26 # include <string.h>
27 #else
28 # if HAVE_STRINGS_H
29 #  include <strings.h>
30 # endif
31 #endif
32 #include <limits.h>
33 #include <errno.h>
34 #include <ctype.h>
35 #include <signal.h>
36 #ifdef HAVE_UNISTD_H
37 # include <unistd.h>
38 #endif
39 #if HAVE_SYS_TIME_H
40 # include <sys/time.h>
41 #endif
42 #include <time.h>
43 #include <sys/stat.h>
44 #ifdef HAVE_FCNTL_H
45 # include <fcntl.h>
46 #elif defined(HAVE_SYS_FCNTL_H)
47 # include <sys/fcntl.h>
48 #endif
49 #ifdef HAVE_IOCTL_H
50 # include <ioctl.h>
51 #elif defined(HAVE_SYS_IOCTL_H)
52 # include <sys/ioctl.h>
53 #endif
54 #include <sys/socket.h>
55 #include <poll.h>
56 #ifdef HAVE_NETINET_IN_SYSTM_H
57 # include <netinet/in_systm.h>
58 #endif
59 #include <netinet/in.h>
60 #include <netinet/ip.h>
61 #include <netinet/tcp.h>
62 #include <sys/mman.h>
63 #ifdef HAVE_SYS_RESOURCE_H
64 # include <sys/resource.h>
65 #endif
66 #ifdef HAVE_SYS_PARAM_H
67 # include <sys/param.h>
68 #endif
69 #ifdef HAVE_SYS_MOUNT_H
70 # include <sys/mount.h>
71 #endif
72 #include <pwd.h>
73 #include <grp.h>
74 #include <arpa/inet.h>
75 #include <netdb.h>
76 #ifdef HAVE_SYS_VFS_H
77 # ifndef STATFS_ALREADY_DEFINED
78 #  include <sys/vfs.h>
79 # endif
80 #endif
81 #ifdef HAVE_SYS_STATVFS_H
82 # include <sys/statvfs.h>
83 #endif
84 #ifdef HAVE_SYS_LOADAVG_H
85 # include <sys/loadavg.h>
86 #endif
87 #ifdef HAVE_SYS_SYSMP_H
88 # include <sys/sysmp.h>
89 #endif
90 #ifdef HAVE_SYS_SYSGET_H
91 # include <sys/sysget.h>
92 #endif
93 #ifdef HAVE_UTIME_H
94 # include <utime.h>
95 #endif
96 
97 #include "mysnprintf.h"
98 
99 #if !defined(HAVE_GETADDRINFO) || !defined(HAVE_GETNAMEINFO) || (!defined(HAVE_INET_NTOP) && !defined(inet_ntop)) || (!defined(HAVE_INET_PTON) && !defined(inet_pton))
100 # define OLD_IP_STACK 1
101 #endif
102 
103 #include "ipstack.h"
104 
105 /* We can't have more than one implementation, so if more than one were
106  * found, the configure test failed - Use none */
107 #ifdef VIRTUOZZO
108 # ifndef DISABLE_SENDFILE
109 #  define DISABLE_SENDFILE 1
110 # endif
111 #endif
112 /* Solaris now supports both sendfilev() and Linux-like sendfile().
113  * Prefer sendfile() if available. */
114 #if defined(SENDFILEV_SOLARIS) && defined(SENDFILE_LINUX)
115 # undef SENDFILEV_SOLARIS
116 #endif
117 #if defined(DISABLE_SENDFILE) || \
118   (defined(SENDFILE_FREEBSD) && (defined(SENDFILE_LINUX) || defined(SENDFILEV_SOLARIS) || defined(SENDFILE_HPUX))) || \
119   (defined(SENDFILE_LINUX) && (defined(SENDFILEV_SOLARIS) || defined(SENDFILE_HPUX))) || \
120   (defined(SENDFILEV_SOLARIS) && defined(SENDFILE_HPUX))
121 # undef SENDFILE_FREEBSD
122 # undef SENDFILE_LINUX
123 # undef SENDFILE64_LINUX
124 # undef SENDFILE_HPUX
125 # undef SENDFILEV_SOLARIS
126 #endif
127 #if defined(SENDFILE_FREEBSD) || defined(SENDFILEV_SOLARIS) || \
128     defined(SENDFILE_HPUX) || \
129     (defined(SENDFILE_LINUX) && defined(SENDFILE64_LINUX))
130 /* Old Linux kernels/glibcs that can't handle 64-bits sendfile() aren't
131  * supported any more. */
132 #else
133 # define SENDFILE_NONE
134 #endif
135 
136 #ifdef HAVE_SYS_UIO_H
137 # include <sys/uio.h>
138 #endif
139 
140 /*
141  * sendfile() is very kernel dependant. It's probable that you have platforms
142  * that require specific #include before sys/sendfile.h . So to enhance
143  * portability, we only include sys/sendfile.h on operating systems known
144  * to be supported
145  */
146 #if (defined(SENDFILE_LINUX) || defined(SENDFILEV_SOLARIS)) && defined(HAVE_SYS_SENDFILE_H)
147 # include <sys/sendfile.h>
148 #endif
149 
150 #ifdef HAVE_ALLOCA
151 # ifdef HAVE_ALLOCA_H
152 #  include <alloca.h>
153 # endif
154 # define ALLOCA(X) alloca(X)
155 # define ALLOCA_FREE(X) do { } while (0)
156 #else
157 # define ALLOCA(X) malloc(X)
158 # define ALLOCA_FREE(X) free(X)
159 #endif
160 
161 #ifdef __FTPD_P_H__
162 # define SYSLOG_NAMES    1        /* for -f */
163 #endif
164 #include <syslog.h>
165 #ifndef HAVE_SYSLOG_NAMES
166 # include "syslognames.h"
167 #endif
168 
169 #ifdef HAVE_CRYPT_H
170 # include <crypt.h>
171 #endif
172 
173 #ifdef USE_SHADOW
174 # ifdef HAVE_SHADOW_H
175 #  include <shadow.h>
176 # endif
177 #endif
178 
179 #if defined(__svr4__) && defined(__sun__) /* Solaris 2 aka SunOS 5 */
180 # include <kvm.h>
181 # include <nlist.h>
182 #endif
183 
184 #ifdef HAVE_SYS_PSTAT_H
185 # include <sys/pstat.h>
186 #endif
187 
188 #ifndef O_NOFOLLOW
189 # define O_NOFOLLOW 0
190 #endif
191 
192 #ifndef O_DIRECTORY
193 # define O_DIRECTORY 0
194 #endif
195 
196 #ifndef MAP_FILE
197 # define MAP_FILE 0
198 #endif
199 
200 /*
201  * It's a hell. Some OS (Linux, BSD) have statfs.
202  * Other OS (Solaris, Irix) have statvfs and statvfs64.
203  * They do the same thing, but with a different syntax.
204  */
205 
206 #if defined(HAVE_STATVFS) || defined(HAVE_STATVFS64)
207 # ifdef HAVE_STATVFS64
208 #  define STATFS_STRUCT struct statvfs64
209 #  define STATFS(PATH, STR) statvfs64(PATH, STR)
210 #  define FSTATFS(FD, STR) fstatvfs64(FD, STR)
211 #  define STATFS_TYPE 1
212 # else
213 #  define STATFS_STRUCT struct statvfs
214 #  define STATFS(PATH, STR) statvfs(PATH, STR)
215 #  define FSTATFS(FD, STR) fstatvfs(FD, STR)
216 #  define STATFS_TYPE 2
217 # endif
218 # define STATFS_BAVAIL(X) ((X).f_bavail)
219 # define STATFS_BLOCKS(X) ((X).f_blocks)
220 # define STATFS_BSIZE(X) ((X).f_bsize)
221 # define STATFS_FRSIZE(X) ((X).f_frsize)
222 #elif defined(HAVE_STATFS)
223 # define STATFS_STRUCT struct statfs
224 # define STATFS(PATH, STR) statfs(PATH, STR)
225 # define FSTATFS(FD, STR) fstatfs(FD, STR)
226 # define STATFS_TYPE 3
227 # define STATFS_BAVAIL(X) ((X).f_bavail)
228 # define STATFS_BLOCKS(X) ((X).f_blocks)
229 # define STATFS_FRSIZE(X) ((X).f_bsize)
230 # define STATFS_BSIZE(X) ((X).f_bsize)
231 #else
232 # define STATFS_STRUCT int
233 # define STATFS(PATH, STR) (-1)
234 # define FSTATFS(FD, STR) (-1)
235 # define STATFS_TYPE -1
236 # define STATFS_BAVAIL(X) (0)
237 # define STATFS_BLOCKS(X) (0)
238 # define STATFS_BSIZE(X) (0)
239 # define STATFS_FRSIZE(X) (0)
240 #endif
241 
242 #ifndef errno
243 extern int errno;
244 #endif
245 #ifndef environ
246 # ifdef __APPLE_CC__
247 #  include <crt_externs.h>
248 #  define environ (*_NSGetEnviron())
249 # else
250 extern char **environ;
251 # endif
252 #endif
253 
254 typedef struct AuthResult_ {
255     int auth_ok;                       /* 0=no auth/login not found,1=ok,-1=auth failed */
256     uid_t uid;
257     gid_t gid;
258     const char *dir;
259     int slow_tilde_expansion;
260     void *backend_data;
261 #ifdef THROTTLING
262     unsigned long throttling_bandwidth_ul;
263     unsigned long throttling_bandwidth_dl;
264     int throttling_ul_changed;
265     int throttling_dl_changed;
266 #endif
267 #ifdef QUOTAS
268     unsigned long long user_quota_size;
269     unsigned long long user_quota_files;
270     int quota_size_changed;
271     int quota_files_changed;
272 #endif
273 #ifdef RATIOS
274     unsigned int ratio_upload;
275     unsigned int ratio_download;
276     int ratio_ul_changed;
277     int ratio_dl_changed;
278 #endif
279 #ifdef PER_USER_LIMITS
280     unsigned int per_user_max;
281 #endif
282 } AuthResult;
283 
284 typedef struct PureFileInfo_ {
285     char **names_pnt;
286     size_t name_offset;
287     off_t size;
288     time_t mtime;
289     mode_t mode;
290     nlink_t nlink;
291     uid_t uid;
292     gid_t gid;
293 } PureFileInfo;
294 
295 #define FI_NAME(X) (*((X)->names_pnt) + (X)->name_offset)
296 
297 typedef enum {
298     ALTLOG_NONE, ALTLOG_CLF, ALTLOG_STATS, ALTLOG_W3C, ALTLOG_XFERLOG
299 } AltLogFormat;
300 
301 typedef struct AltLogPrefixes_ {
302     const char *prefix;
303     const AltLogFormat format;
304 } AltLogPrefixes;
305 
306 typedef enum {
307     CPL_NONE, CPL_CLEAR, CPL_SAFE, CPL_CONFIDENTIAL, CPL_PRIVATE
308 } ChannelProtectionLevel;
309 
310 int pureftpd_start(int argc, char *argv[], const char *home_directory);
311 #ifdef WITH_TLS
312 ssize_t secure_safe_write(void * const tls_fd, const void *buf_, size_t count);
313 #endif
314 void parser(void);
315 void stripctrl(char * const buf, size_t len);
316 void dobanner(const int type);
317 void douser(const char *name);
318 void dopass(char *password);
319 void docwd(const char *dir);
320 void doretr(char *name);
321 void dorest(const char *name);
322 void dodele(char *name);
323 void dostor(char *name, const int append, const int autorename);
324 void domkd(char *name);
325 void dormd(char *name);
326 void domdtm(const char *name);
327 void dosize(const char *name);
328 void doeprt(char *p);
329 void doport(const char *arg);
330 void doport2(struct sockaddr_storage a, unsigned int p);
331 #ifndef MINIMAL
332 void doesta(void);
333 void doestp(void);
334 void doallo(const off_t size);
335 #endif
336 void dopasv(int);
337 void doopts(char *args);
338 void dochmod(char *name, mode_t mode);
339 void doutime(char *name, const char * const wanted_time);
340 void error(int n, const char *msg);
341 void domode(const char *arg);
342 void dostru(const char *arg);
343 void dotype(const char *arg);
344 void donoop(void);
345 void dornfr(char *name);
346 void dornto(char *name);
347 void dostou(void);
348 void dofeat(void);
349 void domlst(const char * const file);
350 void domlsd(const char * const base);
351 void dositetime(void);
352 int ul_check_free_space(const char *name, const double min_space);
353 void disablesignals(void);
354 void getnames(void);
355 void dolist(char *arg, const int on_ctrlconn);
356 void donlst(const char *base);
357 void opendata(void);
358 void closedata(void);
359 void client_fflush(void);
360 void client_printf(const char * const format, ...)
361     __attribute__ ((format(printf, 1, 2)));
362 void addreply(const int code, const char * const line, ...)
363     __attribute__ ((format(printf, 2, 3)));
364 void addreply_noformat(const int code, const char * const line);
365 void doreply(void);
366 void sighandler(int sig);
367 void prevent(char *arg);
368 unsigned int daemons(in_port_t server_port);
369 void logfile(const int facility, const char *format, ...)
370     __attribute__ ((format(printf, 2, 3)));
371 char *skip_telnet_controls(const char *str);
372 void die(const int err, const int priority, const char * const format, ...)
373     __attribute__ ((format(printf, 3, 4))) __attribute__ ((noreturn));
374 void die_mem(void) __attribute__ ((noreturn));
375 void _EXIT(const int status) __attribute__ ((noreturn));
376 void setprocessname(const char * const title);
377 int modernformat(const char *file, char *target, size_t target_size,
378                  const char * const prefix);
379 int sfgets(void);
380 const char *getgroup(const gid_t gid);
381 const char *getname(const uid_t uid);
382 unsigned int zrand(void);
383 void simplify(char *subdir);
384 int checkprintable(register const char *s);
385 void delete_atomic_file(void);
386 void usleep2(const unsigned long microsec);
387 int mysnprintf(char *str, size_t size, const char *format, ...);
388 
389 extern int opt_a, opt_C, opt_d, opt_F, opt_l, opt_R;
390 
391 #ifndef CONFDIR
392 # define CONFDIR "/etc"
393 #endif
394 
395 #ifndef VHOST_PATH
396 # define VHOST_PATH CONFDIR "/pure-ftpd"
397 #endif
398 
399 #ifdef WITH_TLS
400 # ifndef TLS_CONFDIR
401 #  define TLS_CONFDIR "/etc/ssl/private"
402 # endif
403 # ifndef TLS_CERTIFICATE_FILE
404 #  define TLS_CERTIFICATE_FILE TLS_CONFDIR "/pure-ftpd.pem"
405 # endif
406 # ifndef TLS_KEY_FILE
407 #  define TLS_KEY_FILE TLS_CERTIFICATE_FILE
408 # endif
409 #endif
410 
411 #ifndef TLS_DEFAULT_CIPHER_SUITE
412 # define TLS_DEFAULT_CIPHER_SUITE \
413     "ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:!DSS"
414 #endif
415 
416 #ifndef TLS_DEFAULT_ECDH_CURVE
417 # define TLS_DEFAULT_ECDH_CURVE "prime256v1"
418 #endif
419 
420 #define FAKE_SHELL "ftp"
421 
422 #ifndef PID_FILE
423 # ifdef NON_ROOT_FTP
424 #  define PID_FILE CONFDIR "/pure-ftpd.pid"
425 # else
426 #  define PID_FILE STATEDIR "/run/pure-ftpd.pid"
427 # endif
428 #endif
429 
430 #ifndef UPLOADSCRIPT_PID_FILE
431 # ifdef NON_ROOT_FTP
432 #  define UPLOADSCRIPT_PID_FILE CONFDIR "/pure-uploadscript.pid"
433 # else
434 #  define UPLOADSCRIPT_PID_FILE STATEDIR "/run/pure-uploadscript.pid"
435 # endif
436 #endif
437 
438 #ifndef AUTHD_PID_FILE
439 # ifdef NON_ROOT_FTP
440 #  define AUTHD_PID_FILE CONFDIR "/pure-authd.pid"
441 # else
442 #  define AUTHD_PID_FILE STATEDIR "/run/pure-authd.pid"
443 # endif
444 #endif
445 
446 #ifndef CERTD_PID_FILE
447 # ifdef NON_ROOT_FTP
448 #  define CERTD_PID_FILE CONFDIR "/pure-certd.pid"
449 # else
450 #  define CERTD_PID_FILE STATEDIR "/run/pure-certd.pid"
451 # endif
452 #endif
453 
454 #ifndef NON_ROOT_FTP
455 # ifdef IMPLICIT_TLS
456 #  define DEFAULT_FTP_PORT_S "990"
457 # else
458 #  define DEFAULT_FTP_PORT_S "21"
459 # endif
460 #elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
461 # ifdef IMPLICIT_TLS
462 #  define DEFAULT_FTP_PORT_S "990"
463 # else
464 #  define DEFAULT_FTP_PORT_S "21"
465 # endif
466 #else
467 # ifdef IMPLICIT_TLS
468 #  define DEFAULT_FTP_PORT_S "9990"
469 # else
470 #  define DEFAULT_FTP_PORT_S "2121"
471 # endif
472 #endif
473 
474 /*
475  * Some operating systems (at least Solaris > 2.7 and FreeBSD) have strange
476  * troubles with reusing TCP ports, even when SO_REUSEADDR is enabled.
477  * As a workaround, we try several unassigned privileged ports.
478  * The last way is to let the OS assign a port.
479  * For egress filtering, you can accept connections from ports <= 20
480  * to ports >= 1024.
481  */
482 
483 #define FTP_ACTIVE_SOURCE_PORTS { \
484     DEFAULT_FTP_DATA_PORT, 2U, 3U, 4U, 5U, 6U, 10U, 14U, 16U, 0U \
485 }
486 
487 #ifndef PATH_MAX
488 # ifdef MAXPATHLEN
489 #  define PATH_MAX MAXPATHLEN
490 # else
491 #  define PATH_MAX 65536U
492 #  error Warning: neither PATH_MAX nor MAXPATHLEN were found.
493 #  error Remove these lines if you really want to compile the server, but
494 #  error the server may be insecure if a wrong value is set here.
495 # endif
496 #endif
497 #if (PATH_MAX) >= (INT_MAX)
498 Your platform has a very large PATH_MAX, we should not trust it.
499 #endif
500 
501 #ifndef MAX_PASSWORD_LEN
502 # define MAX_PASSWORD_LEN 512
503 #endif
504 #ifndef DEFAULT_MAX_USERS
505 # define DEFAULT_MAX_USERS 50
506 #endif
507 #ifndef DEFAULT_FTP_DATA_PORT
508 # ifdef IMPLICIT_TLS
509 #  define DEFAULT_FTP_DATA_PORT 989
510 # else
511 #  define DEFAULT_FTP_DATA_PORT 20
512 # endif
513 #endif
514 #ifndef MAX_SYSLOG_LINE
515 # define MAX_SYSLOG_LINE (PATH_MAX + 512U)
516 #endif
517 #ifndef DEFAULT_IDLE
518 # define DEFAULT_IDLE (15UL * 60UL)
519 #endif
520 #ifndef MAX_SITE_IDLE
521 # define MAX_SITE_IDLE (42UL * 60UL)
522 #endif
523 #ifndef DEFAULT_MAX_LS_FILES
524 # define DEFAULT_MAX_LS_FILES 10000U
525 #endif
526 #ifndef DEFAULT_MAX_LS_DEPTH
527 # define DEFAULT_MAX_LS_DEPTH 5U
528 #endif
529 #ifndef GLOB_TIMEOUT
530 # define GLOB_TIMEOUT 17                   /* Max user time for a 'ls' to complete */
531 #endif
532 #ifndef MAX_CPU_TIME
533 # define MAX_CPU_TIME (4 * 60 * 60)           /* Max allowed CPU time per session */
534 #endif
535 #ifndef MAX_SESSION_XFER_IDLE
536 # define MAX_SESSION_XFER_IDLE (24 * 60 * 60)   /* Max duration of a transfer */
537 #endif
538 #ifndef MAX_USER_LENGTH
539 # define MAX_USER_LENGTH 127U
540 #endif
541 
542 #ifdef LOG_FTP
543 # define DEFAULT_FACILITY LOG_FTP
544 #else
545 # define DEFAULT_FACILITY LOG_LOCAL2
546 #endif
547 
548 #ifndef MAX_DATA_SIZE
549 # ifdef HAVE_LIBSODIUM
550 #  define MAX_DATA_SIZE (70 * 1024 * 1024)
551 # elif defined(WITH_LDAP) || defined(WITH_MYSQL) || defined(WITH_PGSQL)
552 #  define MAX_DATA_SIZE (16 * 1024 * 1024)       /* Max memory usage - SQL/LDAP need more */
553 # else
554 #  define MAX_DATA_SIZE (8 * 1024 * 1024)       /* Max memory usage */
555 # endif
556 #endif
557 
558 #if CONF_TCP_SO_RCVBUF < 65536
559 # undef CONF_TCP_SO_RCVBUF
560 # define CONF_TCP_SO_RCVBUF 65536
561 #endif
562 #if CONF_TCP_SO_SNDBUF < 65536
563 # undef CONF_TCP_SO_SNDBUF
564 # define CONF_TCP_SO_SNDBUF 65536
565 #endif
566 
567 #ifndef DL_MIN_CHUNK_SIZE
568 # define DL_MIN_CHUNK_SIZE (8 * 1024)
569 #endif
570 #ifndef DL_DEFAULT_CHUNK_SIZE
571 # define DL_DEFAULT_CHUNK_SIZE 49152UL
572 #endif
573 #ifndef DL_DEFAULT_CHUNK_SIZE_ASCII
574 # define DL_DEFAULT_CHUNK_SIZE_ASCII 32768UL
575 #endif
576 #ifndef DL_MAX_CHUNK_SIZE
577 # define DL_MAX_CHUNK_SIZE (128 * 1024UL)
578 #endif
579 #ifndef DL_DLMAP_SIZE
580 # define DL_DLMAP_SIZE (128 * 1024UL)
581 #endif
582 #if DL_DEFAULT_CHUNK_SIZE > DL_MAX_CHUNK_SIZE || DL_MIN_CHUNK_SIZE > DL_MAX_CHUNK_SIZE
583 # error DL_MAX_CHUNK_SIZE shouldnt be <= DL_MIN_CHUNK_SIZE or <= DL_DEFAULT_CHUNK_SIZE
584 #endif
585 #if DL_DLMAP_SIZE < DL_MAX_CHUNK_SIZE
586 # error DL_DLMAP_SIZE should be >= DL_MAX_CHUNK_SIZE
587 #endif
588 
589 #ifndef UL_MIN_CHUNK_SIZE
590 # define UL_MIN_CHUNK_SIZE CONF_TCP_SO_RCVBUF
591 #endif
592 #ifndef UL_DEFAULT_CHUNK_SIZE
593 # define UL_DEFAULT_CHUNK_SIZE (CONF_TCP_SO_RCVBUF * 2)
594 #endif
595 #ifndef UL_DEFAULT_CHUNK_SIZE_ASCII
596 # if CONF_TCP_SO_RCVBUF < 65536
597 #  define UL_DEFAULT_CHUNK_SIZE_ASCII CONF_TCP_SO_RCVBUF
598 # else
599 #  define UL_DEFAULT_CHUNK_SIZE_ASCII 65536UL
600 # endif
601 #endif
602 #ifndef UL_MAX_CHUNK_SIZE
603 # define UL_MAX_CHUNK_SIZE (512 * 1024UL)
604 #endif
605 
606 #define VHOST_PREFIX_MAX_LEN 64
607 
608 #define PUREFTPD_TMPFILE_PREFIX ".pureftpd-"
609 #define ATOMIC_PREFIX_PREFIX PUREFTPD_TMPFILE_PREFIX "upload."
610 #define WIN32_ANON_DIR "/ftp"
611 
612 #define STORAGE_PORT(X)  (*storage_port(&(X)))
613 #define STORAGE_PORT_CONST(X)  (*storage_port_const(&(X)))
614 #define STORAGE_PORT6(X) (*storage_port6(&(X)))
615 #define STORAGE_PORT6_CONST(X) (*storage_port6_const(&(X)))
616 #define STORAGE_SIN_ADDR(X) (storage_sin_addr(&(X))->s_addr)
617 #define STORAGE_SIN_ADDR_CONST(X) (storage_sin_addr_const(&(X))->s_addr)
618 #define STORAGE_SIN_ADDR6(X) (storage_sin_addr6(&(X))->s6_addr)
619 #define STORAGE_SIN_ADDR6_CONST(X) (storage_sin_addr6_const(&(X))->s6_addr)
620 #define STORAGE_SIN_ADDR6_NF(X) (*(storage_sin_addr6(&(X))))
621 #define STORAGE_SIN_ADDR6_NF_CONST(X) (*(storage_sin_addr6_const(&(X))))
622 
623 #ifdef HAVE_SS_LEN
624 # define STORAGE_LEN(X) ((X).ss_len)
625 # define SET_STORAGE_LEN(X, Y) do { STORAGE_LEN(X) = (Y); } while(0)
626 #elif defined(HAVE___SS_LEN)
627 # define STORAGE_LEN(X) ((X).__ss_len)
628 # define SET_STORAGE_LEN(X, Y) do { STORAGE_LEN(X) = (Y); } while(0)
629 #else
630 # define STORAGE_LEN(X) (STORAGE_FAMILY(X) == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6))
631 # define SET_STORAGE_LEN(X, Y) (void) 0
632 #endif
633 
634 #ifdef HAVE___SS_FAMILY
635 # define STORAGE_FAMILY(X) ((X).__ss_family)
636 #else
637 # define STORAGE_FAMILY(X) ((X).ss_family)
638 #endif
639 
640 #ifndef SOL_IP
641 # define SOL_IP IPPROTO_IP
642 #endif
643 #ifndef SOL_TCP
644 # define SOL_TCP IPPROTO_TCP
645 #endif
646 
647 #ifndef INADDR_NONE
648 # define INADDR_NONE 0
649 #endif
650 
651 #if !defined(O_NDELAY) && defined(O_NONBLOCK)
652 # define O_NDELAY O_NONBLOCK
653 #endif
654 
655 #ifndef FNDELAY
656 # define FNDELAY O_NDELAY
657 #endif
658 
659 #ifndef MAP_FAILED
660 # define MAP_FAILED ((void *) -1)
661 #endif
662 
663 #ifndef HAVE_STRTOULL
664 # ifdef HAVE_STRTOQ
665 #  define strtoull(X, Y, Z) strtoq(X, Y, Z)
666 # else
667 #  define strtoull(X, Y, Z) strtoul(X, Y, Z)
668 # endif
669 #endif
670 
671 #ifndef ULONG_LONG_MAX
672 # define ULONG_LONG_MAX (1ULL << 63)
673 #endif
674 
675 #ifdef HAVE_DIRENT_H
676 # include <dirent.h>
677 #endif
678 #ifdef HAVE_SYS_NDIR_H
679 # include <sys/ndir.h>
680 #endif
681 #ifdef HAVE_NDIR_H
682 # include <ndir.h>
683 #endif
684 
685 #ifdef STAT_MACROS_BROKEN
686 # undef S_ISBLK
687 # undef S_ISCHR
688 # undef S_ISDIR
689 # undef S_ISFIFO
690 # undef S_ISLNK
691 # undef S_ISMPB
692 # undef S_ISMPC
693 # undef S_ISNWK
694 # undef S_ISREG
695 # undef S_ISSOCK
696 #endif                            /* STAT_MACROS_BROKEN.  */
697 
698 #ifndef S_IFMT
699 # define S_IFMT 0170000
700 #endif
701 #if !defined(S_ISBLK) && defined(S_IFBLK)
702 # define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
703 #endif
704 #if !defined(S_ISCHR) && defined(S_IFCHR)
705 # define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
706 #endif
707 #if !defined(S_ISDIR) && defined(S_IFDIR)
708 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
709 #endif
710 #if !defined(S_ISREG) && defined(S_IFREG)
711 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
712 #endif
713 #if !defined(S_ISFIFO) && defined(S_IFIFO)
714 # define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
715 #endif
716 #if !defined(S_ISLNK) && defined(S_IFLNK)
717 # define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
718 #endif
719 #if !defined(S_ISSOCK) && defined(S_IFSOCK)
720 # define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
721 #endif
722 #if !defined(S_ISMPB) && defined(S_IFMPB)    /* V7 */
723 # define S_ISMPB(m) (((m) & S_IFMT) == S_IFMPB)
724 # define S_ISMPC(m) (((m) & S_IFMT) == S_IFMPC)
725 #endif
726 #if !defined(S_ISNWK) && defined(S_IFNWK)    /* HP/UX */
727 # define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK)
728 #endif
729 
730 #ifndef S_IEXEC
731 # define S_IEXEC S_IXUSR
732 #endif
733 
734 #ifndef S_IXUSR
735 # define S_IXUSR S_IEXEC
736 #endif
737 #ifndef S_IXGRP
738 # define S_IXGRP (S_IEXEC >> 3)
739 #endif
740 #ifndef S_IXOTH
741 # define S_IXOTH (S_IEXEC >> 6)
742 #endif
743 #ifndef S_IXUGO
744 # define S_IXUGO (S_IXUSR | S_IXGRP | S_IXOTH)
745 #endif
746 
747 #ifdef DISABLE_UNICODE_CONTROL_CHARS
748 # define ISCTRLCODE(X) ((X) == 0x7f || !(((unsigned char) (X)) & 0x60))
749 #else
750 # define ISCTRLCODE(X) ((X) == 0x7f || ((unsigned char) (X)) < 32U)
751 #endif
752 
753 #ifndef HAVE_MUNMAP
754 # define munmap(A, B) (0)
755 #endif
756 
757 #ifndef HAVE_GETHOSTNAME
758 # define gethostname(A, B) (-1)
759 #endif
760 
761 #ifndef HAVE_SETEUID
762 # ifdef HAVE_SETREUID
763 #  define seteuid(X) setreuid(-1, (X))
764 # elif defined(HAVE_SETRESUID)
765 #  define seteuid(X) setresuid(-1, (X), -1)
766 # else
767 #  define seteuid(X) (-1)
768 # endif
769 #endif
770 #ifndef HAVE_SETEGID
771 # ifdef HAVE_SETREGID
772 #  define setegid(X) setregid(-1, (X))
773 # elif defined(HAVE_SETRESGID)
774 #  define setegid(X) setresgid(-1, (X), -1)
775 # else
776 #  define setegid(X) (-1)
777 # endif
778 #endif
779 
780 #if defined(HAVE_DEV_URANDOM) || defined(HAVE_DEV_RANDOM)
781 # define HAVE_RANDOM_DEV 1
782 #endif
783 
784 #define CRLF "\r\n"
785 
786 #ifdef WITH_DMALLOC
787 # define _exit(X) exit(X)
788 #endif
789 
790 #ifndef offsetof
791 # define offsetof(type, member) ((size_t) &((type*) NULL)->member)
792 #endif
793 
794 #include "bsd-realpath.h"
795 #include "fakechroot.h"
796 
797 #endif
798