Debian Codegraph @

doc/mod_sample.c

raw symbols SCIP
1 /*
2 * The following is an *EXAMPLE* ProFTPD module. While it can be compiled
3 * in to ProFTPD, it is not by default, and doesn't really do anything all
4 * that terribly functional.
5 */
6
7 /*
8 * ProFTPD - FTP server daemon
9 * Copyright (c) 1997, 1998 Public Flood Software
10 * Copyright (c) 1999, 2000 MacGyver aka Habeeb J. Dihu <[email protected]>
11 * Copyright (c) 2001-2024 The ProFTPD Project team
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
26 *
27 * As a special exemption, Public Flood Software/MacGyver aka Habeeb J. Dihu
28 * and other respective copyright holders give permission to link this program
29 * with OpenSSL, and distribute the resulting executable, without including
30 * the source code for OpenSSL in the source distribution.
31 */
32
33 /* Sample module for ProFTPD */
34
35 #include "conf.h"
36
37 #define MOD_SAMPLE_VERSION "mod_sample/0.0"
38
39 /* Command handlers
40 */
41
42 /* Example of a PRE_CMD handler here, which simply logs all received
43 * commands via pr_log_debug(). We are careful to return PR_DECLINED,
44 * otherwise other PRE_CMD handlers would not get the request. Note that in
45 * order for this to work properly, this module would need to be loaded _last_,
46 * or after any other modules which don't return PR_DECLINED for all
47 * their precmds. In practice you should always return PR_DECLINED unless
48 * you plan on having your module actually handle the command (or deny it).
49 */
50 MODRET sample_pre_any(cmd_rec *cmd) {
51 pr_log_debug(DEBUG0, "RECEIVED: command '%s', arguments '%s'.",
52 cmd->argv[0], cmd->arg);
53
54 return PR_DECLINED(cmd);
55 }
56
57 /* Next, an example of a LOG_CMD handler, which receives all commands
58 * _after_ they have been processed, and additional only IF they were
59 * successful.
60 */
61 MODRET sample_log_any(cmd_rec *cmd) {
62 pr_log_debug(DEBUG0, "SUCCESSFUL: command '%s', arguments '%s'.",
63 cmd->argv[0], cmd->arg);
64
65 return PR_DECLINED(cmd);
66 }
67
68 /* Now, a _slightly_ more useful handler. We define POST_CMD handlers
69 * for RETR, STOR and LIST/NLST, so we can calculate total data transfer
70 * for a session.
71 */
72 static unsigned long total_rx = 0, total_tx = 0;
73
74 MODRET sample_post_retr(cmd_rec *cmd) {
75
76 /* The global variable 'session' contains lots of important data after
77 * a file/directory transfer of any kind. It doesn't get cleared until
78 * mod_xfer gets a LOG_CMD, so we can still get to it here.
79 */
80 total_tx += session.xfer.total_bytes;
81
82 return PR_DECLINED(cmd);
83 }
84
85 MODRET sample_post_stor(cmd_rec *cmd) {
86 total_rx += session.xfer.total_bytes;
87 return PR_DECLINED(cmd);
88 }
89
90 MODRET sample_post_list(cmd_rec *cmd) {
91 return sample_post_retr(cmd);
92 }
93
94 MODRET sample_post_nlst(cmd_rec *cmd) {
95 return sample_post_retr(cmd);
96 }
97
98 /* This command handler is for a non-standard FTP command, "XFOO". It
99 * illustrates how one can write a module that handles such non-standard
100 * commands.
101 */
102 MODRET sample_xfoo(cmd_rec *cmd) {
103 char *path = NULL;
104
105 if (cmd->argc < 2) {
106 pr_response_add_err(R_500, "XFOO command needs at least one argument");
107 return PR_ERROR(cmd);
108 }
109
110 /* We call pr_fs_decode_path() on the argument here, assuming that the
111 * argument to this fictional XFOO command is indeed a path. RFC2640
112 * states that clients can encode paths as UTF8 strings; the
113 * pr_fs_decode_path() function converts from UTF8 strings to the local
114 * character set.
115 */
116 path = dir_realpath(cmd->tmp_pool,
117 pr_fs_decode_path(cmd->tmp_pool, cmd->arg));
118
119 if (!path) {
120 pr_response_add_err(R_500, "It appears that '%s' does not exist",
121 cmd->arg);
122 return PR_ERROR(cmd);
123 }
124
125 pr_response_add_err(R_200, "XFOO command successful (yeah right!)");
126 return PR_HANDLED(cmd);
127 }
128
129 /* Configuration handlers
130 */
131
132 /* This sample configuration directive handler will get called
133 * whenever the "FooBarDirective" directive is encountered in the
134 * configuration file.
135 */
136
137 MODRET set_foobardirective(cmd_rec *cmd) {
138 int foo_bar = 1;
139 config_rec *c = NULL;
140
141 /* The CHECK_ARGS macro checks the number of arguments passed to the
142 * directive against what we want. Note that this is *one* less than
143 * cmd->argc, because cmd->argc includes cmd->argv[0] (the directive
144 * itself). If CHECK_ARGS fails, a generic error is sent to the user
145 */
146 CHECK_ARGS(cmd, 1);
147
148 /* The CHECK_CONF macro makes sure that this directive is not being
149 * "used" in the wrong context (i.e. if the directive is only available
150 * or applicable inside certain contexts). In this case, we are allowing
151 * the directive inside of <Anonymous> and <Limit>, but nowhere else.
152 * If this macro fails a generic error is logged and the handler aborts.
153 */
154 CHECK_CONF(cmd, CONF_ANON|CONF_LIMIT);
155
156 /* Get the Boolean value of the first directive parameter. */
157 foo_bar = get_boolean(cmd, 1);
158 if (foo_bar == -1) {
159
160 /* The get_boolean() function returns -1 if the parameter was not a
161 * recognized Boolean parameter.
162 */
163 CONF_ERROR(cmd, "requires a Boolean parameter");
164 }
165
166 /* add_config_param() adds a configuration parameter record to our current
167 * configuration context. We're initially setting the value stored in
168 * the config_rec to be NULL, so that we can allocate memory of the
169 * proper size for storing the Boolean value.
170 */
171 c = add_config_param(cmd->argv[0], 1, NULL);
172
173 /* Allocate space for the Boolean value. The smallest data type in C
174 * is an unsigned char (1 byte), and a Boolean will easily fit within
175 * that space.
176 */
177 c->argv[0] = pcalloc(c->pool, sizeof(unsigned char));
178 *((unsigned char *) c->argv[0]) = foo_bar;
179
180 /* By adding the CF_MERGEDOWN flag to the parameter we just created
181 * we are telling proftpd that this parameter should be copied and
182 * "merged" into all "lower" contexts until it either hits a
183 * parameter w/ the same name or bottoms out.
184 *
185 * Example _without_ CF_MERGEDOWN:
186 *
187 * <VirtualHost>
188 * |----------\
189 * <Anonymous>
190 * | - FooBarDirective <------- Config places it here
191 * |-----------\
192 * <Directory> <------- Doesn't apply here
193 * |-------------\
194 * <Limit> <--- Or here.....
195 *
196 * Now, if we specify CF_MERGDOWN, the tree ends up looking like:
197 *
198 * <VirtualHost>
199 * |----------\
200 * <Anonymous>
201 * | - FooBarDirective <------- Config places it here
202 * |-----------\
203 * <Directory> <------- Now, it DOES apply here
204 * | - FooBarDirective
205 * |-------------\
206 * <Limit> <-------- And here ...
207 * | - FooBarDirective
208 *
209 */
210
211 c->flags |= CF_MERGEDOWN;
212
213 /* Tell proftpd that we handled the request w/ no problems.
214 */
215 return PR_HANDLED(cmd);
216 }
217
218 /* Initialization routines
219 */
220
221 /* Each module can supply up to two initialization routines (via
222 * the module structure at the bottom of this file). The first
223 * init function is called immediately after the module is loaded,
224 * while the second is called after proftpd is connected to a client,
225 * and the main proftpd server (if not in inetd mode) has forked off.
226 * The second init function's purpose is to let the module perform
227 * any necessary work for initializing a session, once a client is connected
228 * and the daemon is ready to service the new client. In inetd mode, the
229 * session initialization function will be called immediately after proftpd is
230 * loaded, because proftpd is _always_ in "child mode" when run from inetd.
231 * Note that both of these initialization routines are optional. If you don't
232 * need them (or only need one), simply set the function pointer to NULL
233 * in the module structure.
234 */
235
236 static int sample_init(void) {
237 /* do something useful here, right? */
238
239 return 0;
240 }
241
242 static int sample_sess_init(void) {
243 /* same here */
244
245 return 0;
246 }
247
248 /* Module API tables
249 *
250 * There are three tables which act as the "glue" between proftpd and
251 * a module. None of the tables are _required_ (however having none would
252 * make the module fairly useless).
253 *
254 * The first table is the configuration directive handler table. It specifies
255 * handler routines in the module which will be used during configuration
256 * file parsing.
257 */
258
259 static conftable sample_conftab[] = {
260 { "FooBarDirective", set_foobardirective, NULL },
261 { NULL }
262 };
263
264 /* The command handler table:
265 * first : command "type" (see the doc/API for more info)
266 *
267 * second : command "name", or the actual null-terminated ascii text
268 * sent by a client (in uppercase) for this command. see
269 * include/ftp.h for macros which define all rfced FTP protocol
270 * commands. Can also be the special macro C_ANY, which receives
271 * ALL commands.
272 *
273 * third : command "group" (used for access control via Limit directives),
274 * this can be either G_DIRS (for commands related to directory
275 * listing), G_READ (for commands related to reading files),
276 * G_WRITE (for commands related to file writing), or the
277 * special G_NONE for those commands against which the
278 * special <Limit READ|WRITE|DIRS> will not be applied.
279 *
280 * fourth : function pointer to your handler
281 *
282 * fifth : TRUE if the command cannot be used before authentication
283 * (via USER/PASS), otherwise FALSE.
284 *
285 * sixth : TRUE if the command can be sent during a file transfer
286 * (note: as of 1.1.5, this is obsolete)
287 *
288 */
289
290 static cmdtable sample_cmdtab[] = {
291 { PRE_CMD, C_ANY, G_NONE, sample_pre_any, FALSE, FALSE },
292 { LOG_CMD, C_ANY, G_NONE, sample_log_any, FALSE, FALSE },
293 { POST_CMD, C_RETR, G_NONE, sample_post_retr, FALSE, FALSE },
294 { POST_CMD, C_STOR, G_NONE, sample_post_stor, FALSE, FALSE },
295 { POST_CMD, C_APPE, G_NONE, sample_post_stor, FALSE, FALSE },
296 { POST_CMD, C_LIST, G_NONE, sample_post_list, FALSE, FALSE },
297 { POST_CMD, C_NLST, G_NONE, sample_post_nlst, FALSE, FALSE },
298 { CMD, "XFOO", G_DIRS, sample_xfoo, TRUE, FALSE },
299 { 0, NULL }
300 };
301
302 module sample_module = {
303
304 /* Always NULL */
305 NULL, NULL,
306
307 /* Module API version (2.0) */
308 0x20,
309
310 /* Module name */
311 "sample",
312
313 /* Module configuration directive handlers */
314 sample_conftab,
315
316 /* Module command handlers */
317 sample_cmdtab,
318
319 /* Module authentication handlers (none in this case) */
320 NULL,
321
322 /* Module initialization */
323 sample_init,
324
325 /* Session initialization */
326 sample_sess_init,
327
328 /* Module version */
329 MOD_SAMPLE_VERSION
330 };