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