1 /*
2  * wzdftpd - a modular and cool ftp server
3  * Copyright (C) 2002-2004  Pierre Chifflier
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  *
19  * As a special exemption, Pierre Chifflier
20  * and other respective copyright holders give permission to link this program
21  * with OpenSSL, and distribute the resulting executable, without including
22  * the source code for OpenSSL in the source distribution.
23  */
24 
25 #ifndef __WZD_STRUCTS__
26 #define __WZD_STRUCTS__
27 
28 /** \file wzd_structs.h
29  * \brief Essential structures
30  *
31  * \addtogroup libwzd_core
32  * @{
33  */
34 
35 #include "wzd_hardlimits.h"
36 
37 #include "wzd_types.h"
38 
39 /****************** PRE DECLARATIONS **********************/
40 
41 typedef struct wzd_backend_t wzd_backend_t;
42 typedef struct wzd_backend_def_t wzd_backend_def_t;
43 
44 typedef struct wzd_config_t wzd_config_t;
45 
46 /*********************** ERRORS ***************************/
47 
48 typedef enum {
49   E_OK=0,
50 
51   E_NO_DATA_CTX,	/**< no data connection available */
52 
53   E_PARAM_NULL,		/**< parameter is NULL */
54   E_PARAM_BIG,		/**< parameter is too long */
55   E_PARAM_INVALID,	/**< parameter is invalid */
56   E_PARAM_EXIST,	/**< parameter already exist */
57 
58   E_WRONGPATH,		/**< path is invalid */
59 
60   E_NOTDIR,		/**< not a directory */
61   E_ISDIR,		/**< is a directory */
62 
63   E_NOPERM,		/**< not enough perms */
64 
65   E_TIMEOUT,		/**< timeout on control connection */
66   E_DATATIMEOUT,	/**< timeout on data connection */
67   E_CONNECTTIMEOUT,	/**< timeout on connect() */
68   E_PASV_FAILED,	/**< pasv connection failed */
69   E_PORT_INVALIDIP,	/**< invalid address in PORT */
70   E_XFER_PROGRESS,	/**< transfer in progress */
71   E_XFER_REJECTED,	/**< transfer explicitely rejected by, for ex., script */
72 
73   E_CREDS_INSUFF,	/**< insufficient credits */
74 
75   E_USER_REJECTED,	/**< user rejected */
76   E_USER_NO_HOME,	/**< user has no homedir */
77   E_USER_NOIP,		/**< ip not allowed */
78   E_USER_MAXUSERIP,	/**< max number of ip reached for user */
79   E_USER_MAXGROUPIP,	/**< max number of ip reached for group */
80   E_USER_CLOSED,	/**< site is closed for this login */
81   E_USER_DELETED,	/**< user have been deleted */
82   E_USER_NUMLOGINS,	/**< user has reached user num_logins limit */
83   E_USER_TLSFORCED,	/**< user must be in TLS mode */
84 
85   E_GROUP_NUMLOGINS,	/**< user has reached group num_logins limit */
86 
87   E_PASS_REJECTED,	/**< wrong pass */
88 
89   E_FILE_NOEXIST,	/**< file does not exist */
90   E_FILE_FORBIDDEN,	/**< access to file is forbidden */
91   E_FILE_TYPE,	        /**< file has wrong type for operation */
92 
93   E_USER_IDONTEXIST,	/**< server said i don't exist ! */
94   E_USER_ICANTSUICIDE,	/**< user is trying to kill its connection ! */
95   E_USER_NOBODY,	/**< no user was matched by action */
96 
97   E_MKDIR_PARSE,	/**< directory name parsing gives errors */
98   E_MKDIR_PATHFILTER,	/**< dirname rejected by pathfilter */
99 
100   E_COMMAND_FAILED,     /**< system command failed, check errno */
101 
102 
103   E_NOMEM,              /**< could not allocate memory */
104 } wzd_errno_t;
105 
106 /*********************** RIGHTS ***************************/
107 
108 #define RIGHT_NONE      0x00000000
109 
110 #define RIGHT_LIST      0x00000001
111 #define RIGHT_RETR      0x00000002
112 #define RIGHT_STOR      0x00000004
113 
114 #define RIGHT_DELE      0x00000010
115 
116 
117 /* other rights - should not be used directly ! */
118 #define RIGHT_CWD       0x00010000
119 #define RIGHT_MKDIR     0x00020000
120 #define RIGHT_RMDIR     0x00040000
121 #define RIGHT_RNFR      0x00200000
122 
123 typedef unsigned long wzd_perm_t;
124 
125 /******************** BANDWIDTH LIMIT *********************/
126 
127 /** @brief Limit bandwidth
128  */
129 typedef struct limiter
130 {
131   u32_t maxspeed;
132 #ifndef WIN32
133   struct timeval current_time;
134 #else
135   struct _timeb current_time;
136 #endif
137   int bytes_transfered;
138   float current_speed;
139 } wzd_bw_limiter;
140 
141 /************************ VFS *****************************/
142 typedef struct _wzd_vfs_t {
143   char	* virtual_dir;
144   char	* physical_dir;
145 
146   char	* target;
147 
148   struct _wzd_vfs_t	* prev_vfs, * next_vfs;
149 } wzd_vfs_t;
150 
151 /*********************** DATA *****************************/
152 typedef enum {
153   DATA_PORT,
154   DATA_PASV
155 } data_mode_t;
156 
157 /*********************** STATS ****************************/
158 /** @brief User statistics: number of files downloaded, etc
159  */
160 typedef struct {
161   u64_t             bytes_ul_total;
162   u64_t             bytes_dl_total;
163   unsigned long		files_ul_total;
164   unsigned long		files_dl_total;
165 } wzd_stats_t;
166 
167 /********************** USER, GROUP ***********************/
168 
169 typedef struct wzd_user_t wzd_user_t;
170 
171 typedef struct wzd_group_t wzd_group_t;
172 
173 /*********************** BACKEND **************************/
174 
175 /** IMPORTANT:
176  *
177  * all validation functions have the following return code:
178  *
179  *   0 = success
180  *
181  *   !0 = failure
182  *
183  * the last parameter of all functions is a ptr to current user
184  */
185 
186 
187 
188 typedef int (*backend_init_function_t)(struct wzd_backend_t*);
189 
190 struct wzd_backend_def_t {
191   char * filename;
192 
193   char * param;
194   void * handle;
195 
196   backend_init_function_t fcn_init;
197 
198   struct wzd_backend_t * b;
199 
200   struct wzd_backend_def_t * next_backend;
201 };
202 
203 
204 /************************ FLAGS ***************************/
205 
206 enum wzd_flag_t {
207   FLAG_ANONYMOUS = 'A', /**< anonymous users cannot modify filesystem */
208   FLAG_COLOR = '5',     /**< enable use of colors */
209   FLAG_DELETED = 'D',
210   FLAG_FULLPATH = 'f',  /**< show the complete path to the user */
211   FLAG_FXP_DISABLE = 'F',  /**< disable site-to-site transfer */
212   FLAG_GADMIN = 'G',
213   FLAG_HIDDEN = 'H',
214   FLAG_ULTRAHIDDEN = 'h',
215   FLAG_IDLE = 'I',
216   FLAG_TLS = 'k',       /**< explicit and implicit connections only */
217   FLAG_TLS_DATA = 'K',  /**< user must use encrypted data connection */
218   FLAG_SITEOP = 'O',
219   FLAG_SEE_IP = 's',
220   FLAG_SEE_HOME = 't',
221 };
222 
223 /************************ MODULES *************************/
224 
225 typedef int (*void_fct)(void);
226 
227 typedef struct _wzd_hook_t {
228   unsigned long mask;
229 
230   char *	opt;	/* used by custom site commands */
231 
232   void_fct	hook;
233   char *	external_command;
234 
235   struct _wzd_hook_t	*next_hook;
236 } wzd_hook_t;
237 
238 typedef struct _wzd_module_t {
239   char *	name;
240 
241   void *	handle;
242 
243   struct _wzd_module_t	*next_module;
244 } wzd_module_t;
245 
246 /* defined in binary, combine with OR (|) */
247 
248 /* see also event_tab[] in wzd_mod.c */
249 
250 enum event_id_t {
251   EVENT_NONE          = 0x00000000,
252 
253   EVENT_LOGIN         = 0x00000001,
254   EVENT_LOGOUT        = 0x00000002,
255 
256   EVENT_PREUPLOAD     = 0x00000010,
257   EVENT_POSTUPLOAD    = 0x00000020,
258   EVENT_PREDOWNLOAD   = 0x00000040,
259   EVENT_POSTDOWNLOAD  = 0x00000080,
260 
261   EVENT_PREMKDIR      = 0x00000100,
262   EVENT_MKDIR         = 0x00000200,
263   EVENT_PRERMDIR      = 0x00000400,
264   EVENT_RMDIR         = 0x00000800,
265 
266   EVENT_PREDELE       = 0x00001000,
267   EVENT_DELE          = 0x00002000,
268   EVENT_PREWIPE       = 0x00004000,
269   EVENT_WIPE          = 0x00008000,
270 
271   EVENT_SITE          = 0x00010000,
272   EVENT_CRONTAB       = 0x00100000,
273 
274 };
275 
276 /************************ SECTIONS ************************/
277 
278 typedef struct wzd_section_t wzd_section_t;
279 /** @brief Section: definition, properties */
280 struct wzd_section_t {
281   char *        sectionname;
282   char *        sectionmask;
283   char *        sectionre;
284 
285 /*  regex_t *	pathfilter;*/
286   void *	pathfilter;
287 
288   struct wzd_section_t * next_section;
289 };
290 
291 /********************** SERVER STATS **********************/
292 
293 /** @brief Server statistics: number of connections, etc */
294 typedef struct {
295   unsigned long num_connections; /**< @brief total # of connections since server start */
296   unsigned long num_childs; /**< @brief total # of childs process created since server start */
297 } wzd_server_stat_t;
298 
299 /*************************** IP **************************/
300 
301 #include "wzd_ip.h"
302 
303 /*************************** TLS **************************/
304 
305 typedef enum { TLS_CLEAR, TLS_PRIV } tls_data_mode_t; /* data modes */
306 
307 typedef enum { TLS_SERVER_MODE=0, TLS_CLIENT_MODE } tls_role_t;
308 
309 typedef enum { TLS_NOTYPE=0, TLS_EXPLICIT, TLS_STRICT_EXPLICIT, TLS_IMPLICIT } tls_type_t;
310 
311 typedef enum { TLS_NONE, TLS_READ, TLS_WRITE } ssl_fd_mode_t;
312 
313 typedef struct {
314   void * session;
315   void * data_session;
316 } wzd_tls_t;
317 
318 typedef enum {
319   ASCII=0,
320   BINARY
321 } xfer_t;
322 
323 /************************* CONTEXT ************************/
324 
325 /** important - must not be fffff or d0d0d0, etc.
326  * to make distinction with unallocated zone
327  */
328 #define	CONTEXT_MAGIC	0x0aa87d45
329 
330 /** context::connection_flags field */
331 #define	CONNECTION_TLS	0x00000040
332 #define	CONNECTION_UTF8	0x00000100
333 
334 typedef int (*read_fct_t)(fd_t,char*,size_t,int,unsigned int,void *);
335 typedef int (*write_fct_t)(fd_t,const char*,size_t,int,unsigned int,void *);
336 
337 typedef struct wzd_context_t wzd_context_t;
338 
339 #include "wzd_action.h"
340 
341 /** @brief Connection state
342  */
343 typedef enum {
344   STATE_UNKNOWN=0,
345   STATE_CONNECTING, /* waiting for ident */
346   STATE_LOGGING,
347   STATE_COMMAND,
348   STATE_XFER
349 } connection_state_t;
350 
351 /** @brief Client-specific data
352  */
353 struct wzd_context_t {
354   unsigned long	magic;  /**< \brief magic number, used to test structure integrity */
355 
356   net_family_t  family; /**< \brief IPv4 or IPv6 */
357   unsigned char	hostip[16];
358   int           localport;
359   wzd_ip_t      * peer_ip;
360   char          * ident;
361   char          * idnt_address;
362   connection_state_t state;
363   unsigned char	exitclient;
364   fd_t          controlfd;
365   fd_t          datafd;
366   data_mode_t   datamode;
367   tls_data_mode_t    tls_data_mode;
368   net_family_t  datafamily; /**< \brief IPv4 or IPv6 */
369   unsigned long	pid_child;
370   unsigned long	thread_id;
371 
372   union wzd_thread_t * transfer_thread;
373   u8_t          is_transferring;
374 
375   fd_t          pasvsock;
376   read_fct_t    read_fct;
377   write_fct_t   write_fct;
378   int           dataport;
379   unsigned char dataip[16];
380   u64_t         resume;
381   unsigned long	connection_flags;
382   char          currentpath[WZD_MAX_PATH];
383   u32_t 	userid;
384   xfer_t        current_xfer_type;
385   wzd_action_t	current_action;
386   struct last_file_t	last_file;
387   char          * data_buffer;
388   char          * control_buffer;
389 /*  wzd_bw_limiter * current_limiter;*/
390   wzd_bw_limiter current_ul_limiter;
391   wzd_bw_limiter current_dl_limiter;
392   time_t        login_time;
393   time_t	idle_time_start;
394   time_t	idle_time_data_start;
395   struct wzd_ssl_t * ssl;
396   struct wzd_reply_t * reply;
397   wzd_tls_t   	tls;
398   tls_role_t    tls_role; /**< \brief TLS role: server or client */
399   struct _auth_gssapi_data_t * gssapi_data;
400 };
401 
402 /********************** COMMANDS **************************/
403 
404 #include "wzd_commands.h"
405 
406 /************************ MAIN CONFIG *********************/
407 
408 #include "wzd_backend.h"
409 
410 /* macros used with options */
411 #define CFG_OPT_DENY_ACCESS_FILES_UPLOADED  0x00000001
412 #define CFG_OPT_HIDE_DOTTED_FILES           0x00000002
413 #define CFG_OPT_USE_SYSLOG                  0x00000010
414 #define CFG_OPT_DISABLE_TLS                 0x00000100
415 #define CFG_OPT_DISABLE_IDENT               0x00000200
416 #define CFG_OPT_UTF8_CAPABLE                0x00001000
417 #define CFG_OPT_CHECKIP_LOGIN               0x00010000
418 #define CFG_OPT_REJECT_UNKNOWN_USERS        0x00020000
419 #define CFG_OPT_DYNAMIC_IP                  0x00100000
420 
421 #define CFG_OPT_EXPERIMENTAL                0x10000000
422 
423 #define CFG_CLR_OPTION(c,opt)   (c)->server_opts &= ~(opt)
424 #define CFG_SET_OPTION(c,opt)   (c)->server_opts |= (opt)
425 #define CFG_GET_OPTION(c,opt)   ( (c)->server_opts & (opt) )
426 
427 /** @brief Server config
428  *
429  * Contains all variables specific to a server instance.
430  */
431 struct wzd_config_t {
432   char *	pid_file;
433   char *	config_filename;
434   time_t	server_start;
435   unsigned char	serverstop;
436   unsigned char	site_closed;
437   wzd_backend_def_t * backends;
438   int		max_threads;
439   char *	logfilename;
440   unsigned int	logfilemode;
441   FILE *	logfile;
442   char *	xferlog_name;
443   int		xferlog_fd;
444   int		loglevel;
445   char *        logdir;
446   unsigned int  umask;
447   char *	dir_message;
448   fd_t		controlfd; /**< external control: named pipe, unix socket, or socket */
449   char          ip[MAX_IP_LENGTH];
450   char          dynamic_ip[MAX_IP_LENGTH];
451   unsigned int	port;
452   u32_t         pasv_low_range;
453   u32_t         pasv_high_range;
454   unsigned char	pasv_ip[16];
455   struct wzd_ip_list_t	*login_pre_ip_checks;
456   wzd_vfs_t	*vfs;
457   wzd_hook_t	*hook;
458   wzd_module_t	*module;
459   unsigned int  data_buffer_length; /**< size of buffer used for transfers. This has a great impact on performances */
460   unsigned long	server_opts;
461   wzd_server_stat_t	stats;
462 /*  SSL_CTX *	tls_ctx;*/ /** \todo XXX casting with void* is bad ... use correct type ? */
463   void * tls_ctx;
464   tls_type_t	tls_type;
465   CHTBL          * commands_list;
466   wzd_section_t		* section_list;
467 
468   wzd_bw_limiter	global_ul_limiter;
469   wzd_bw_limiter	global_dl_limiter;
470 
471   struct _wzd_configfile_t * cfg_file;
472 
473   struct wzd_cronjob_t * crontab;
474 
475   struct wzd_event_manager_t * event_mgr;
476 };
477 
478 WZDIMPORT extern wzd_config_t *	mainConfig;
479 WZDIMPORT extern List * context_list;
480 
481 /************************ LIST ****************************/
482 
483 enum list_type_t {
484   LIST_TYPE_NONE   = 0,
485   LIST_TYPE_SHORT  = 1 << 0,
486   LIST_TYPE_LONG   = 1 << 1,
487   LIST_SHOW_HIDDEN = 1 << 2,
488 };
489 
490 /** @} */
491 
492 #endif /* __WZD_STRUCTS__ */
493