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-2017 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 /* ProFTPD module definitions. */
28 
29 #ifndef PR_MODULES_H
30 #define PR_MODULES_H
31 
32 typedef struct module_struc	module;
33 typedef struct modret_struc	modret_t;
34 
35 struct modret_struc {
36   module *mr_handler_module;		/* which module handled this? */
37   int mr_error;				/* !0 if error */
38   const char *mr_numeric;		/* numeric error code */
39   const char *mr_message;		/* text message */
40   void *data;				/* add'l data -- undefined */
41 };
42 
43 /* The following macros are for creating basic modret_t, and can
44  * only be used inside of module handlers
45  */
46 
47 #define MODRET				static modret_t*
48 #define PR_HANDLED(cmd)			mod_create_ret((cmd),\
49 					0,NULL,NULL)
50 #define PR_DECLINED(cmd)		(modret_t*)NULL
51 #define PR_ERROR(cmd)			mod_create_ret((cmd),\
52 					1,NULL,NULL)
53 #define PR_ERROR_MSG(cmd,n,m)		mod_create_ret((cmd),\
54 					1,(n),(m))
55 #define PR_ERROR_INT(cmd,n)		mod_create_error((cmd),(n))
56 
57 #define MODRET_ISDECLINED(x)		((x) == NULL)
58 #define MODRET_ISHANDLED(x)		((x) && !(x)->mr_error)
59 #define MODRET_ISERROR(x)		((x) && (x)->mr_error)
60 #define MODRET_HASNUM(x)		((x) && (x)->mr_numeric)
61 #define MODRET_HASMSG(x)		((x) && (x)->mr_message)
62 #define MODRET_ERROR(x)			((x) ? (x)->mr_error : 0)
63 #define MODRET_ERRNUM(x)		((x) ? (x)->mr_numeric : NULL)
64 #define MODRET_ERRMSG(x)		((x) ? (x)->mr_message : NULL)
65 #define MODRET_HASDATA(x)		((x) ? ((x)->data ? TRUE : FALSE) : FALSE)
66 
67 typedef struct conftab_rec {
68   char *directive;
69   modret_t *(*handler)(cmd_rec *);
70 
71   module *m;				/* Reference to owning module
72 					 * set when module is initialized
73 					 */
74 
75 } conftable;
76 
77 /* Classes of commands.  These are used as logging categories as well. */
78 #define CL_NONE		0x0000
79 #define CL_AUTH		0x0001  /* USER, PASS */
80 #define CL_INFO		0x0002  /* Informational commands (PWD, SYST, etc) */
81 #define CL_DIRS		0x0004  /* Directory commands (LIST, NLST, CWD, etc) */
82 #define CL_READ		0x0008  /* File reading commands (RETR) */
83 #define CL_WRITE	0x0010  /* Writing commands (STOR, MKD, etc) */
84 #define CL_MISC		0x0020  /* Miscellaneous (RNFR/RNTO, SITE, etc) */
85 #define CL_SEC		0x0040  /* RFC2228 Security commands */
86 #define CL_CONNECT	0x0080  /* Session start */
87 #define CL_DISCONNECT	0x0100  /* Session end */
88 #define CL_SSH		0x0200  /* SSH requests */
89 #define CL_SFTP		0x0400  /* SFTP requests */
90 
91 /* Note that CL_ALL explicitly does NOT include CL_DISCONNECT; this is to
92  * preserve backward compatible behavior.
93  */
94 #define CL_ALL\
95   (CL_AUTH|CL_INFO|CL_DIRS|CL_READ|\
96    CL_WRITE|CL_MISC|CL_SEC|CL_SSH|CL_SFTP)
97 
98 /* Command handler types for command table */
99 #define PRE_CMD				1
100 #define CMD				2
101 #define POST_CMD			3
102 #define POST_CMD_ERR			4
103 #define LOG_CMD				5
104 #define LOG_CMD_ERR			6
105 #define HOOK				7
106 
107 typedef struct cmdtab_rec {
108 
109   /* See above for cmd types. */
110   unsigned char cmd_type;
111   const char *command;
112 
113   /* Command group. */
114   const char *group;
115   modret_t *(*handler)(cmd_rec *);
116 
117   /* Does this command require authentication? */
118   unsigned char requires_auth;
119 
120   /* Can this command be issued during a transfer? (Now obsolete) */
121   unsigned char interrupt_xfer;
122 
123   int cmd_class;
124   module *m;
125 
126 } cmdtable;
127 
128 typedef struct authtab_rec {
129   int auth_flags;			/* future use */
130   const char *name;
131   modret_t *(*handler)(cmd_rec *);
132 
133   module *m;
134 } authtable;
135 
136 #define PR_AUTH_FL_REQUIRED		0x00001
137 
138 struct module_struc {
139   module *next, *prev;
140 
141   int api_version;			/* API version _not_ module version */
142   const char *name;			/* Module name */
143 
144   struct conftab_rec *conftable;	/* Configuration directive table */
145   struct cmdtab_rec *cmdtable;		/* Command table */
146   struct authtab_rec *authtable; 	/* Authentication handler table */
147 
148   int (*init)(void); 			/* Module initialization */
149   int (*sess_init)(void);		/* Session initialization */
150 
151   const char *module_version;		/* Module version */
152   void *handle;				/* Module handle */
153 
154   /* Internal use; high number == higher priority. */
155   int priority;
156 };
157 
158 #define ANY_MODULE			((module*)0xffffffff)
159 
160 /* Prototypes */
161 
162 unsigned char command_exists(const char *);
163 int modules_init(void);
164 void modules_list(int flags);
165 void modules_list2(int (*listf)(const char *, ...), int flags);
166 #define PR_MODULES_LIST_FL_SHOW_VERSION		0x00001
167 #define PR_MODULES_LIST_FL_SHOW_STATIC		0x00002
168 
169 int modules_session_init(void);
170 
171 unsigned char pr_module_exists(const char *);
172 module *pr_module_get(const char *);
173 int pr_module_load(module *m);
174 int pr_module_unload(module *m);
175 
176 /* Load the various symbol tables from this module. */
177 int pr_module_load_authtab(module *m);
178 int pr_module_load_cmdtab(module *m);
179 int pr_module_load_conftab(module *m);
180 
181 modret_t *pr_module_call(module *, modret_t *(*)(cmd_rec *), cmd_rec *);
182 
183 /* This function is in main.c, but is prototyped here */
184 void set_auth_check(int (*ck)(cmd_rec *));
185 
186 /* This callback is defined/stored in src/main.c */
187 extern int (*cmd_auth_chk)(cmd_rec *);
188 
189 /* For use from inside module handler functions */
190 modret_t *mod_create_ret(cmd_rec *, unsigned char, const char *, const char *);
191 modret_t *mod_create_error(cmd_rec *, int);
192 modret_t *mod_create_data(cmd_rec *, void *);
193 
194 /* Implemented in mod_core.c */
195 int core_chgrp(cmd_rec *, const char *, uid_t, gid_t);
196 int core_chmod(cmd_rec *, const char *, mode_t);
197 
198 #endif /* PR_MODULES_H */
199