1 /*
2  * ProFTPD - FTP server daemon
3  * Copyright (c) 1997, 1998 Public Flood Software
4  * Copyright (c) 1999, 2000 MacGyver aka Habeeb J. Dihu <macgyver@tos.net>
5  * Copyright (c) 2001-2020 The ProFTPD Project team
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
20  *
21  * As a special exemption, Public Flood Software/MacGyver aka Habeeb J. Dihu
22  * and other respective copyright holders give permission to link this program
23  * with OpenSSL, and distribute the resulting executable, without including
24  * the source code for OpenSSL in the source distribution.
25  */
26 
27 /* Non-specific support functions. */
28 
29 #ifndef PR_SUPPORT_H
30 #define PR_SUPPORT_H
31 
32 #include <sys/time.h>
33 #include <time.h>
34 
35 #if defined(NAME_MAX)
36 # define NAME_MAX_GUESS		(NAME_MAX)
37 #elif defined(MAXNAMELEN)
38 # define NAME_MAX_GUESS		(MAXNAMELEN - 1)
39 #else
40 # define NAME_MAX_GUESS		(255)
41 #endif
42 
43 /* Functions [optionally] provided by libsupp.a */
44 #ifndef HAVE_GETOPT
45 int getopt(int, char * const [], const char *);
46 extern char *optarg;
47 extern int optind,opterr,optopt;
48 
49 #ifndef HAVE_GETOPT_LONG
50 struct option {
51   const char *name;
52   int has_arg;
53   int *flag;
54   int val;
55 };
56 
57 int getopt_long(int, char * const [], const char *, const struct option *,
58   int *);
59 # endif /* !HAVE_GETOPT_LONG */
60 #endif /* !HAVE_GETOPT */
61 
62 char *dir_interpolate(pool *, const char *);
63 char *dir_abs_path(pool *, const char *, int);
64 
65 /* Performs chroot-aware handling of symlinks. */
66 int dir_readlink(pool *, const char *, char *, size_t, int);
67 #define PR_DIR_READLINK_FL_HANDLE_REL_PATH		0x0001
68 
69 char *dir_realpath(pool *, const char *);
70 char *dir_canonical_path(pool *, const char *);
71 char *dir_canonical_vpath(pool *, const char *);
72 char *dir_best_path(pool *, const char *);
73 
74 /* Schedulables. */
75 void schedule(void (*f)(void *, void *, void *, void *), int, void *, void *,
76   void *, void *);
77 void run_schedule(void);
78 void restart_daemon(void *, void *, void *, void *);
79 void shutdown_end_session(void *, void *, void *, void *);
80 
81 long get_name_max(char *path, int fd);
82 
83 mode_t file_mode(const char *);
84 mode_t file_mode2(pool *, const char *);
85 
86 mode_t symlink_mode(const char *);
87 mode_t symlink_mode2(pool *, const char *);
88 
89 int file_exists(const char *);
90 int file_exists2(pool *, const char *);
91 
92 int dir_exists(const char *);
93 int dir_exists2(pool *, const char *);
94 
95 int exists(const char *);
96 int exists2(pool *, const char *);
97 
98 char *safe_token(char **);
99 int check_shutmsg(pool *, const char *, time_t *, time_t *, time_t *, char *,
100   size_t);
101 
102 void pr_memscrub(void *, size_t);
103 
104 void pr_getopt_reset(void);
105 struct tm *pr_gmtime(pool *, const time_t *);
106 struct tm *pr_localtime(pool *, const time_t *);
107 const char *pr_strtime(time_t);
108 const char *pr_strtime2(time_t, int);
109 
110 /* Preferred version.  Allocates the returned string out of the given pool. */
111 const char *pr_strtime3(pool *, time_t, int);
112 
113 int pr_gettimeofday_millis(uint64_t *);
114 int pr_timeval2millis(struct timeval *, uint64_t *);
115 
116 /* Wrappers around snprintf(3)/vsnprintf(3) which carefully check the
117  * return values.
118  */
119 
120 int pr_snprintf(char *buf, size_t bufsz, const char *fmt, ...)
121 #ifdef __GNUC__
122   __attribute__ ((format (printf, 3, 4)));
123 #else
124   ;
125 #endif
126 
127 /* Just like pr_snprintf(), except that the caller can provide their
128  * source code location.
129  */
130 int pr_snprintfl(const char *file, int lineno, char *buf, size_t bufsz,
131   const char *fmt, ...)
132 #ifdef __GNUC__
133   __attribute__ ((format (printf, 5, 6)));
134 #else
135   ;
136 #endif
137 
138 int pr_vsnprintf(char *buf, size_t bufsz, const char *fmt, va_list msg);
139 int pr_vsnprintfl(const char *file, int lineno, char *buf, size_t bufsz,
140   const char *fmt, va_list msg);
141 
142 /* Resolve/substitute any "%u" variables in the path.  Returns the resolved
143  * path, or NULL if there was an error.
144  */
145 const char *path_subst_uservar(pool *p, const char **path);
146 
147 #endif /* PR_SUPPORT_H */
148