/* * ProFTPD: mod_xclient * * Copyright (c) 2018 Manuel Mausz * * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ #define MOD_XCLIENT_VERSION "mod_xclient/0.2.0" #include "conf.h" #include "privs.h" static const char *trace_channel = "xclient"; static int xclient_engine = FALSE; static int xclient_rewrite = FALSE; /* command handlers */ MODRET xclient_xclient(cmd_rec *cmd) { if (xclient_engine == FALSE) return PR_DECLINED(cmd); if (cmd->argc < 2) return PR_DECLINED(cmd); if (strncasecmp(cmd->argv[1], "XCLIENT", 7) == 0) { if (cmd->argc < 4) { pr_response_add_err(R_501, _("Invalid number of parameters")); pr_cmd_set_errno(cmd, EINVAL); errno = EINVAL; return PR_ERROR(cmd); } const pr_netaddr_t *remote_addr = NULL; unsigned int remote_port = 0; for (unsigned int i = 2; i < cmd->argc; ++i) { const char *param = cmd->argv[i]; pr_trace_msg(trace_channel, 5, "parsing parameter '%s'", param); if (strncasecmp(param, "ADDR=", 5) == 0) { remote_addr = pr_netaddr_get_addr2(session.pool, param + 5, NULL, PR_NETADDR_GET_ADDR_FL_EXCL_DNS); if (remote_addr == NULL) { pr_response_add_err(R_501, _("%s: Invalid IP address provided"), param + 5); pr_cmd_set_errno(cmd, EINVAL); errno = EINVAL; return PR_ERROR(cmd); } } else if (strncasecmp(param, "PORT=", 5) == 0) { char *tmp = NULL; remote_port = strtoul(param + 5, &tmp, 10); if (tmp == NULL || *tmp != '\0' || remote_port <= 0 || remote_port > 65535) { pr_response_add_err(R_501, _("%s: Invalid port provided"), param + 5); pr_cmd_set_errno(cmd, EINVAL); errno = EINVAL; return PR_ERROR(cmd); } } else { pr_response_add_err(R_500, _("'%s' not understood"), param); pr_cmd_set_errno(cmd, EINVAL); errno = EINVAL; return PR_ERROR(cmd); } } if (remote_addr != NULL && remote_port > 0) { const char *addr = pr_netaddr_get_ipstr(remote_addr); (void) pr_table_remove(session.notes, "mod_xclient.addr", NULL); if (pr_table_add_dup(session.notes, "mod_xclient.addr", addr, 0) < 0) { pr_trace_msg(trace_channel, 3, "error stashing 'mod_xclient.addr' in session.notes: %s", strerror(errno)); } char port[6]; pr_snprintf(port, sizeof(port), "%u", remote_port); (void) pr_table_remove(session.notes, "mod_xclient.port", NULL); if (pr_table_add_dup(session.notes, "mod_xclient.port", port, 0) < 0) { pr_trace_msg(trace_channel, 3, "error stashing 'mod_xclient.port' in session.notes: %s", strerror(errno)); } if (xclient_rewrite) { session.c->remote_addr = remote_addr; session.c->remote_port = remote_port; /* now perform reverse DNS lookups */ if (ServerUseReverseDNS) { int reverse_dns = pr_netaddr_set_reverse_dns(ServerUseReverseDNS); session.c->remote_name = pr_netaddr_get_dnsstr(session.c->remote_addr); pr_netaddr_set_reverse_dns(reverse_dns); } else session.c->remote_name = pr_netaddr_get_ipstr(session.c->remote_addr); pr_netaddr_set_sess_addrs(); /* find the new class for this session */ session.conn_class = pr_class_match_addr(session.c->remote_addr); } pr_log_debug(DEBUG0, MOD_XCLIENT_VERSION ": stored%s client remote address: %s", (xclient_rewrite) ? "+rewrote" : "", addr); pr_trace_msg(trace_channel, 5, ": stored" ": stored%s client remote address: %s", (xclient_rewrite) ? "+rewrote" : "", addr); } pr_response_add(R_200, _("SITE %s command successful"), (char *)cmd->argv[1]); return PR_HANDLED(cmd); } return PR_DECLINED(cmd); } /* configuration handlers */ MODRET set_xclient_engine(cmd_rec *cmd) { CHECK_ARGS(cmd, 1); CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL); int engine = get_boolean(cmd, 1); if (engine == -1) CONF_ERROR(cmd, "expected Boolean parameter"); config_rec *c = add_config_param(cmd->argv[0], 1, NULL); c->argv[0] = palloc(c->pool, sizeof(int)); *((int *)c->argv[0]) = engine; return PR_HANDLED(cmd); } MODRET set_xclient_rewrite(cmd_rec *cmd) { CHECK_ARGS(cmd, 1); CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL); int rewrite = get_boolean(cmd, 1); if (rewrite == -1) CONF_ERROR(cmd, "expected Boolean parameter"); config_rec *c = add_config_param(cmd->argv[0], 1, NULL); c->argv[0] = palloc(c->pool, sizeof(int)); *((int *)c->argv[0]) = rewrite; return PR_HANDLED(cmd); } /* initialization routines */ static int xclient_sess_init(void) { config_rec *c = NULL; c = find_config(main_server->conf, CONF_PARAM, "XClientEngine", FALSE); if (c != NULL) xclient_engine = *((int *)c->argv[0]); c = find_config(main_server->conf, CONF_PARAM, "XClientRewrite", FALSE); if (c != NULL) xclient_rewrite = *((int *)c->argv[0]); return 0; } /* module api tables */ static conftable xclient_conftab[] = { { "XClientEngine", set_xclient_engine, NULL }, { "XClientRewrite", set_xclient_rewrite, NULL }, { NULL } }; static cmdtable xclient_cmdtab[] = { { CMD, C_SITE, G_NONE, xclient_xclient, FALSE, FALSE, CL_MISC }, { 0, NULL } }; module xclient_module = { /* always NULL */ NULL, NULL, /* module api version 2.0 */ 0x20, /* module name */ "xclient", /* module configuration handler table */ xclient_conftab, /* module command handler table */ xclient_cmdtab, /* module authentication handler table */ NULL, /* module initialization function */ NULL, /* module session initialization function */ xclient_sess_init, /* module version */ MOD_XCLIENT_VERSION };