Debian Codegraph @

contrib/mod_dynmasq.c

raw symbols SCIP
1 /*
2 * ProFTPD: mod_dynmasq -- a module for dynamically updating MasqueradeAddress
3 * configurations, as when DynDNS names are used
4 * Copyright (c) 2004-2023 TJ Saunders
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
19 *
20 * As a special exemption, TJ Saunders and other respective copyright holders
21 * give permission to link this program with OpenSSL, and distribute the
22 * resulting executable, without including the source code for OpenSSL in the
23 * source distribution.
24 *
25 * This is mod_dynmasq, contrib software for proftpd 1.3.x and above.
26 * For more information contact TJ Saunders <[email protected]>.
27 */
28
29 #include "conf.h"
30
31 #ifdef PR_USE_CTRLS
32 # include "mod_ctrls.h"
33 #endif
34
35 #define MOD_DYNMASQ_VERSION "mod_dynmasq/0.5"
36
37 /* Make sure the version of proftpd is as necessary. */
38 #if PROFTPD_VERSION_NUMBER < 0x0001030602
39 # error "ProFTPD 1.3.6rc2 or later required"
40 #endif
41
42 extern xaset_t *server_list;
43 module dynmasq_module;
44 static int dynmasq_timer_id = -1;
45 static int dynmasq_timer_interval = -1;
46
47 #ifdef PR_USE_CTRLS
48 static pool *dynmasq_act_pool = NULL;
49 static ctrls_acttab_t dynmasq_acttab[];
50 #endif /* PR_USE_CTRLS */
51
52 static void dynmasq_refresh(void) {
53 server_rec *s;
54
55 pr_log_debug(DEBUG2, MOD_DYNMASQ_VERSION
56 ": resolving all MasqueradeAddress directives (could take a little while)");
57
58 for (s = (server_rec *) server_list->xas_list; s; s = s->next) {
59 config_rec *c;
60
61 c = find_config(s->conf, CONF_PARAM, "MasqueradeAddress", FALSE);
62 if (c != NULL) {
63 const char *masq_addr;
64 const pr_netaddr_t *na;
65
66 masq_addr = c->argv[1];
67
68 pr_netaddr_clear_ipcache(masq_addr);
69 na = pr_netaddr_get_addr(s->pool, masq_addr, NULL);
70 if (na != NULL) {
71 /* Compare the obtained netaddr with the one already present.
72 * Only update the "live" netaddr if they differ.
73 */
74 pr_log_debug(DEBUG2, MOD_DYNMASQ_VERSION
75 ": resolved MasqueradeAddress '%s' to IP address %s", masq_addr,
76 pr_netaddr_get_ipstr(na));
77
78 if (pr_netaddr_cmp(c->argv[0], na) != 0) {
79 pr_log_pri(PR_LOG_DEBUG, MOD_DYNMASQ_VERSION
80 ": MasqueradeAddress '%s' updated for new address %s (was %s)",
81 masq_addr, pr_netaddr_get_ipstr(na),
82 pr_netaddr_get_ipstr(c->argv[0]));
83
84 /* Overwrite the old netaddr pointer. Note that this constitutes
85 * a minor memory leak, as there currently isn't a way to free
86 * the memory used by a netaddr object. Hrm.
87 */
88 c->argv[0] = (void *) na;
89
90 } else {
91 pr_log_debug(DEBUG2, MOD_DYNMASQ_VERSION
92 ": MasqueradeAddress '%s' has not changed addresses", masq_addr);
93 }
94
95 } else {
96 pr_log_pri(PR_LOG_INFO, MOD_DYNMASQ_VERSION
97 ": unable to resolve '%s', keeping previous address", masq_addr);
98 }
99 }
100 }
101
102 return;
103 }
104
105 #if defined(PR_USE_CTRLS)
106 static int dynmasq_handle_refresh(pr_ctrls_t *ctrl, int reqargc,
107 char **reqargv) {
108 dynmasq_refresh();
109
110 pr_ctrls_add_response(ctrl, "dynmasq: refreshed");
111 return PR_CTRLS_STATUS_OK;
112 }
113
114 static int dynmasq_handle_dynmasq(pr_ctrls_t *ctrl, int reqargc,
115 char **reqargv) {
116 /* Sanity check */
117 if (reqargc == 0 ||
118 reqargv == NULL) {
119 pr_ctrls_add_response(ctrl, "missing required parameters");
120 return PR_CTRLS_STATUS_WRONG_PARAMETERS;
121 }
122
123 if (strcmp(reqargv[0], "refresh") == 0) {
124
125 /* Check the ACLs. */
126 if (pr_ctrls_check_acl(ctrl, dynmasq_acttab, "refresh") != TRUE) {
127 pr_ctrls_add_response(ctrl, "access denied");
128 return PR_CTRLS_STATUS_ACCESS_DENIED;
129 }
130
131 return dynmasq_handle_refresh(ctrl, --reqargc, ++reqargv);
132 }
133
134 pr_ctrls_add_response(ctrl, "unknown dynmasq action: '%s'", reqargv[0]);
135 return PR_CTRLS_STATUS_UNSUPPORTED_OPERATION;
136 }
137 #endif /* PR_USE_CTRLS */
138
139 /* Configuration handlers
140 */
141
142 /* usage: DynMasqControlsACLs actions|all allow|deny user|group list */
143 MODRET set_dynmasqctrlsacls(cmd_rec *cmd) {
144 #ifdef PR_USE_CTRLS
145 char *bad_action = NULL, **actions = NULL;
146
147 CHECK_ARGS(cmd, 4);
148 CHECK_CONF(cmd, CONF_ROOT);
149
150 actions = pr_ctrls_parse_acl(cmd->tmp_pool, cmd->argv[1]);
151
152 /* Check the second parameter to make sure it is "allow" or "deny" */
153 if (strcmp(cmd->argv[2], "allow") != 0 &&
154 strcmp(cmd->argv[2], "deny") != 0) {
155 CONF_ERROR(cmd, "second parameter must be 'allow' or 'deny'");
156 }
157
158 /* Check the third parameter to make sure it is "user" or "group" */
159 if (strcmp(cmd->argv[3], "user") != 0 &&
160 strcmp(cmd->argv[3], "group") != 0) {
161 CONF_ERROR(cmd, "third parameter must be 'user' or 'group'");
162 }
163
164 bad_action = pr_ctrls_set_module_acls(dynmasq_acttab, dynmasq_act_pool,
165 actions, cmd->argv[2], cmd->argv[3], cmd->argv[4]);
166 if (bad_action != NULL) {
167 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, ": unknown action: '",
168 bad_action, "'", NULL));
169 }
170
171 return PR_HANDLED(cmd);
172 #else
173 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "The ", cmd->argv[0],
174 " directive requires Controls support (--enable-ctrls)", NULL));
175 #endif /* PR_USE_CTRLS */
176 }
177
178 /* usage: DynMasqRefresh <seconds> */
179 MODRET set_dynmasqrefresh(cmd_rec *cmd) {
180 CHECK_CONF(cmd, CONF_ROOT);
181 CHECK_ARGS(cmd, 1);
182
183 dynmasq_timer_interval = atoi(cmd->argv[1]);
184 if (dynmasq_timer_interval < 1)
185 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
186 "must be greater than zero: '", cmd->argv[1], "'", NULL));
187
188 return PR_HANDLED(cmd);
189 }
190
191 /* Timers
192 */
193
194 static int dynmasq_refresh_cb(CALLBACK_FRAME) {
195 dynmasq_refresh();
196 return 1;
197 }
198
199 /* Event handlers
200 */
201
202 #if defined(PR_SHARED_MODULE)
203 static void dynmasq_mod_unload_ev(const void *event_data, void *user_data) {
204 if (strcmp("mod_dynmasq.c", (const char *) event_data) == 0) {
205 pr_event_unregister(&dynmasq_module, NULL, NULL);
206
207 # ifdef PR_USE_CTRLS
208 /* Unregister any control actions. */
209 pr_ctrls_unregister(&dynmasq_module, "dynmasq");
210
211 destroy_pool(dynmasq_act_pool);
212 dynmasq_act_pool = NULL;
213 # endif /* PR_USE_CTRLS */
214
215 pr_timer_remove(dynmasq_timer_id, &dynmasq_module);
216 dynmasq_timer_id = -1;
217
218 }
219 }
220 #endif /* !PR_SHARED_MODULE */
221
222 static void dynmasq_postparse_ev(const void *event_data, void *user_data) {
223 if (dynmasq_timer_interval != -1) {
224 dynmasq_timer_id = pr_timer_add(dynmasq_timer_interval, -1,
225 &dynmasq_module, dynmasq_refresh_cb, "dynmasq address refresh");
226 }
227 }
228
229 static void dynmasq_restart_ev(const void *event_data, void *user_data) {
230 #ifdef PR_USE_CTRLS
231 register unsigned int i;
232 #endif /* PR_USE_CTRLS */
233
234 if (dynmasq_timer_id != -1) {
235 pr_timer_remove(dynmasq_timer_id, &dynmasq_module);
236 dynmasq_timer_id = -1;
237 }
238
239 #ifdef PR_USE_CTRLS
240 if (dynmasq_act_pool) {
241 destroy_pool(dynmasq_act_pool);
242 dynmasq_act_pool = NULL;
243 }
244
245 dynmasq_act_pool = make_sub_pool(permanent_pool);
246 pr_pool_tag(dynmasq_act_pool, "DynMasq Controls Pool");
247
248 /* Re-create the controls ACLs. */
249 for (i = 0; dynmasq_acttab[i].act_action; i++) {
250 dynmasq_acttab[i].act_acl = palloc(dynmasq_act_pool, sizeof(ctrls_acl_t));
251 pr_ctrls_init_acl(dynmasq_acttab[i].act_acl);
252 }
253 #endif /* PR_USE_CTRLS */
254 }
255
256 /* Initialization functions
257 */
258
259 static int dynmasq_init(void) {
260 #if defined(PR_SHARED_MODULE)
261 pr_event_register(&dynmasq_module, "core.module-unload",
262 dynmasq_mod_unload_ev, NULL);
263 #endif /* !PR_SHARED_MODULE */
264
265 pr_event_register(&dynmasq_module, "core.postparse", dynmasq_postparse_ev,
266 NULL);
267 pr_event_register(&dynmasq_module, "core.restart", dynmasq_restart_ev,
268 NULL);
269
270 #ifdef PR_USE_CTRLS
271 if (pr_ctrls_register(&dynmasq_module, "dynmasq", "mod_dynmasq controls",
272 dynmasq_handle_dynmasq) < 0) {
273 pr_log_pri(PR_LOG_NOTICE, MOD_DYNMASQ_VERSION
274 ": error registering 'dynmasq' control: %s", strerror(errno));
275
276 } else {
277 register unsigned int i;
278
279 dynmasq_act_pool = make_sub_pool(permanent_pool);
280 pr_pool_tag(dynmasq_act_pool, "DynMasq Controls Pool");
281
282 for (i = 0; dynmasq_acttab[i].act_action; i++) {
283 dynmasq_acttab[i].act_acl = palloc(dynmasq_act_pool, sizeof(ctrls_acl_t));
284 pr_ctrls_init_acl(dynmasq_acttab[i].act_acl);
285 }
286 }
287 #endif /* PR_USE_CTRLS */
288
289 return 0;
290 }
291
292 static int dynmasq_sess_init(void) {
293
294 /* Ensure that the timer only fires on the daemon process. */
295 pr_timer_remove(dynmasq_timer_id, &dynmasq_module);
296 dynmasq_timer_id = -1;
297
298 pr_event_unregister(&dynmasq_module, "core.restart", NULL);
299
300 return 0;
301 }
302
303 /* Module API tables
304 */
305
306 #ifdef PR_USE_CTRLS
307 static ctrls_acttab_t dynmasq_acttab[] = {
308 { "refresh", NULL, NULL, NULL },
309 { NULL, NULL, NULL, NULL }
310 };
311 #endif /* PR_USE_CTRLS */
312
313 static conftable dynmasq_conftab[] = {
314 { "DynMasqControlsACLs", set_dynmasqctrlsacls, NULL },
315 { "DynMasqRefresh", set_dynmasqrefresh, NULL },
316 { NULL }
317 };
318
319 module dynmasq_module = {
320 NULL, NULL,
321
322 /* Module API version 2.0 */
323 0x20,
324
325 /* Module name */
326 "dynmasq",
327
328 /* Module configuration handler table */
329 dynmasq_conftab,
330
331 /* Module command handler table */
332 NULL,
333
334 /* Module authentication handler table */
335 NULL,
336
337 /* Module initialization function */
338 dynmasq_init,
339
340 /* Session initialization function */
341 dynmasq_sess_init,
342
343 /* Module version */
344 MOD_DYNMASQ_VERSION
345 };