Browse files

Temporary NOT WORKING RELEASE, just for show how the new configuratio…

…n style will works
  • Loading branch information...
1 parent 6dabcd7 commit eb7e0db12659381e09e93a97a040b6ee6821c23a @vecna vecna committed Sep 10, 2011
Showing with 573 additions and 573 deletions.
  1. +304 −0 src/old_os_cmds.c
  2. +269 −218 src/os_cmds.c
  3. +0 −355 src/os_cmds_under_study.c
View
304 src/old_os_cmds.c
@@ -0,0 +1,304 @@
+/*
+ * Janus, a portable, unified and lightweight interface for mitm
+ * applications over the traffic directed to the default gateway.
+ *
+ * Copyright (C) 2011 evilaliv3 <giovanni.pellerano@evilaliv3.org>
+ * vecna <vecna@delirandom.net>
+ *
+ * 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 3 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config_macros.h"
+#include "janus.h"
+#include "string.h"
+
+struct cmd_sw
+{
+ char* cmd_test;
+ void (*cmd_ex)(char* buf, size_t bufsize);
+};
+
+static void execOSCmd(char *buf, size_t bufsize, const char *format, ...)
+{
+ char cmd[CONST_JANUS_BUFSIZE] = {0};
+ FILE *stream = NULL;
+
+ va_list arguments;
+ va_start(arguments, format);
+ vsnprintf(cmd, sizeof (cmd), format, arguments);
+ va_end(arguments);
+
+ printf("executing cmd: [%s]\n", cmd);
+
+ stream = popen(cmd, "r");
+ if (stream != NULL)
+ {
+ if (buf != NULL)
+ {
+ memset(buf, 0, bufsize);
+
+ if (fgets(buf, bufsize, stream) != NULL)
+ {
+ const size_t len = strlen(buf);
+
+ if (len && buf[len - 1] == '\n')
+ buf[len - 1] = '\0';
+ }
+ }
+
+ pclose(stream);
+ }
+}
+
+static void (*bindCmd(struct cmd_sw cmd[]))(char* buf, size_t bufsize)
+{
+ char test[CONST_JANUS_BUFSIZE] = {0};
+
+ uint8_t i = 0;
+ while (cmd[i].cmd_test != NULL)
+ {
+ execOSCmd(test, sizeof (test), "which %s", cmd[i].cmd_test);
+ if (strlen(test))
+ {
+ printf("binding executed using: %s\n", cmd[i].cmd_test);
+ return cmd[i].cmd_ex;
+ }
+
+ ++i;
+ }
+
+ return NULL;
+}
+
+static void cmd0_route(char* buf, size_t bufsize)
+{
+ execOSCmd(buf, bufsize, "route -n | sed -n 's/^\\(0.0.0.0\\).* \\([0-9.]\\{7,15\\}\\) .*\\(0.0.0.0\\).*UG.* \\(.*\\)$/\\4/p'");
+}
+
+static void cmd1_ifconfig(char* buf, size_t bufsize)
+{
+ execOSCmd(buf, bufsize, "ifconfig %s | sed -n 's/.*inet addr:\\([0-9.]\\+\\) .*$/\\1/p'", str_map[STR_NET_IF]);
+}
+
+static void cmd2_ifconfig(char* buf, size_t bufsize)
+{
+ execOSCmd(buf, bufsize, "ifconfig %s | sed -n 's/^.* HWaddr \\([a-fA-F0-9:]\\{17,17\\}\\).*$/\\1/p'", str_map[STR_NET_IF]);
+}
+
+static void cmd3_ifconfig(char* buf, size_t bufsize)
+{
+ execOSCmd(buf, bufsize, "ifconfig -a %s | sed -n 's/^.* MTU:\\([0-9]*\\) .*$/\\1/p'", str_map[STR_NET_IF]);
+}
+
+static void cmd4_route(char* buf, size_t bufsize)
+{
+ execOSCmd(buf, bufsize, "route -n | sed -n 's/^\\(0.0.0.0\\).* \\([0-9.]\\{7,15\\}\\) .*\\(0.0.0.0\\).*UG.* %s$/\\2/p'", str_map[STR_NET_IF]);
+}
+
+static void cmd5_arp(char* buf, size_t bufsize)
+{
+ execOSCmd(buf, bufsize, "arp -ni %s %s | sed -n 's/^.*\\([a-fA-F0-9:]\\{17,17\\}\\).*$/\\1/p'", str_map[STR_NET_IF], str_map[STR_GW_IP]);
+}
+
+static void cmd5_arping(char* buf, size_t bufsize)
+{
+ execOSCmd(buf, bufsize, "arping -f -I %s %s | sed -n 's/^.*\\([a-fA-F0-9:]\\{16,16\\}\\)\\].*$/0\\1/p'", str_map[STR_NET_IF], str_map[STR_GW_IP]);
+}
+
+static void cmd6_route(char* buf, size_t bufsize)
+{
+ execOSCmd(NULL, 0, "route add default gw %s dev %s", str_map[STR_GW_IP], str_map[STR_NET_IF]);
+}
+
+static void cmd7_route(char* buf, size_t bufsize)
+{
+ execOSCmd(NULL, 0, "route del default gw %s dev %s", str_map[STR_GW_IP], str_map[STR_NET_IF]);
+}
+
+static void cmd8_route(char* buf, size_t bufsize)
+{
+ execOSCmd(NULL, 0, "route add default gw %s dev %s", str_map[STR_TUN_IP], str_map[STR_TUN_IF]);
+}
+
+static void cmd9_route(char* buf, size_t bufsize)
+{
+ execOSCmd(NULL, 0, "route del default gw %s dev %s", str_map[STR_TUN_IP], str_map[STR_TUN_IF]);
+}
+
+static void cmd10_iptables(char* buf, size_t bufsize)
+{
+ execOSCmd(NULL, 0, "iptables -A INPUT -i %s -m mac --mac-source %s -j DROP", str_map[STR_NET_IF], str_map[STR_GW_MAC]);
+}
+
+static void cmd11_iptables(char* buf, size_t bufsize)
+{
+ execOSCmd(NULL, 0, "iptables -D INPUT -i %s -m mac --mac-source %s -j DROP", str_map[STR_NET_IF], str_map[STR_GW_MAC]);
+}
+
+static void cmd12_iptables(char* buf, size_t bufsize)
+{
+ execOSCmd(NULL, 0, "iptables -A FORWARD -i %s -m mac --mac-source %s -j DROP", str_map[STR_NET_IF], str_map[STR_GW_MAC]);
+}
+
+static void cmd13_iptables(char* buf, size_t bufsize)
+{
+ execOSCmd(NULL, 0, "iptables -D FORWARD -i %s -m mac --mac-source %s -j DROP", str_map[STR_NET_IF], str_map[STR_GW_MAC]);
+}
+
+static void cmd14_iptables(char* buf, size_t bufsize)
+{
+ execOSCmd(NULL, 0, "iptables -A POSTROUTING -o %s -t nat -j MASQUERADE", str_map[STR_TUN_IF]);
+}
+
+static void cmd15_iptables(char* buf, size_t bufsize)
+{
+ execOSCmd(NULL, 0, "iptables -D POSTROUTING -o %s -t nat -j MASQUERADE", str_map[STR_TUN_IF]);
+}
+
+static void cmd16_ifconfig(char* buf, size_t bufsize)
+{
+ execOSCmd(NULL, 0, "ifconfig %s %s pointopoint %s mtu %s", str_map[STR_TUN_IF], str_map[STR_NET_IP], str_map[STR_TUN_IP], str_map[STR_TUN_MTU]);
+}
+
+static struct cmd_sw cmd0_sw[] = {
+ {"route", cmd0_route},
+ {NULL, NULL}
+};
+
+static struct cmd_sw cmd1_sw[] = {
+ {"ifconfig", cmd1_ifconfig},
+ {NULL, NULL}
+};
+
+static struct cmd_sw cmd2_sw[] = {
+ {"ifconfig", cmd2_ifconfig},
+ {NULL, NULL}
+};
+
+static struct cmd_sw cmd3_sw[] = {
+ {"ifconfig", cmd3_ifconfig},
+ {NULL, NULL}
+};
+
+static struct cmd_sw cmd4_sw[] = {
+ {"route", cmd4_route},
+ {NULL, NULL}
+};
+
+static struct cmd_sw cmd5_sw[] = {
+ {"arp", cmd5_arp},
+ {"arping", cmd5_arping},
+ {NULL, NULL}
+};
+
+static struct cmd_sw cmd6_sw[] = {
+ {"route", cmd6_route},
+ {NULL, NULL}
+};
+
+static struct cmd_sw cmd7_sw[] = {
+ {"route", cmd7_route},
+ {NULL, NULL}
+};
+
+static struct cmd_sw cmd8_sw[] = {
+ {"route", cmd8_route},
+ {NULL, NULL}
+};
+
+static struct cmd_sw cmd9_sw[] = {
+ {"route", cmd9_route},
+ {NULL, NULL}
+};
+
+static struct cmd_sw cmd10_sw[] = {
+ {"iptables", cmd10_iptables},
+ {NULL, NULL}
+};
+
+static struct cmd_sw cmd11_sw[] = {
+ {"iptables", cmd11_iptables},
+ {NULL, NULL}
+};
+
+static struct cmd_sw cmd12_sw[] = {
+ {"iptables", cmd12_iptables},
+ {NULL, NULL}
+};
+
+static struct cmd_sw cmd13_sw[] = {
+ {"iptables", cmd13_iptables},
+ {NULL, NULL}
+};
+
+static struct cmd_sw cmd14_sw[] = {
+ {"iptables", cmd14_iptables},
+ {NULL, NULL}
+};
+
+static struct cmd_sw cmd15_sw[] = {
+ {"iptables", cmd15_iptables},
+ {NULL, NULL}
+};
+
+static struct cmd_sw cmd16_sw[] = {
+ {"ifconfig", cmd16_ifconfig},
+ {NULL, NULL}
+};
+
+static struct
+{
+ struct cmd_sw *sw;
+} cmd_sw_table[] = {
+ {cmd0_sw},
+ {cmd1_sw},
+ {cmd2_sw},
+ {cmd3_sw},
+ {cmd4_sw},
+ {cmd5_sw},
+ {cmd6_sw},
+ {cmd7_sw},
+ {cmd8_sw},
+ {cmd9_sw},
+ {cmd10_sw},
+ {cmd11_sw},
+ {cmd12_sw},
+ {cmd13_sw},
+ {cmd14_sw},
+ {cmd15_sw},
+ {cmd16_sw},
+ {0}
+};
+
+static void (*cmd[COMMANDS_NUM])(char* buf, size_t bufsize);
+
+static void bindCmds(void)
+{
+ char test[CONST_JANUS_BUFSIZE] = {0};
+
+ uint8_t i;
+
+ printf("checking sed command presence\n");
+ execOSCmd(test, sizeof (test), "which sed");
+ if (!strlen(test))
+ runtime_exception("unable to find sed command");
+
+ for (i = 0; cmd_sw_table[i].sw != NULL; ++i)
+ {
+ printf("binding cmd %u to a system command\n", i);
+ cmd[i] = bindCmd(cmd_sw_table[i].sw);
+ if (cmd[i] == NULL)
+ runtime_exception("unable to bind cmd %u to a system command", i);
+ }
+}
View
487 src/os_cmds.c
@@ -15,290 +15,341 @@
* 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, see <http://www.gnu.org/licenses/>.
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
*/
-
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include <ctype.h>
#include "config_macros.h"
-#include "janus.h"
-#include "string.h"
-struct cmd_sw
+struct target_map
{
- char* cmd_test;
- void (*cmd_ex)(char* buf, size_t bufsize);
+ char *string;
+ uint8_t index;
+ void *command;
+ void *output;
+ void *test;
+} os_cfg[] = {
+ { "local interface name", STR_NET_IF, NULL, NULL, NULL },
+ { "local interface IP", STR_NET_IP, NULL, NULL, NULL },
+ { "local interface MAC", STR_NET_MAC, NULL, NULL, NULL },
+ { "local interface MTU", STR_NET_MTU, NULL, NULL, NULL },
+ { "tunnel interface name", STR_TUN_IF, NULL, NULL, NULL },
+ { "tunnel interface IP", STR_TUN_IP, NULL, NULL, NULL },
+ { "get local iface name", CMD_GET_NETIF, NULL, NULL, NULL },
+ { "get local iface IP", CMD_GET_NETIP, NULL, NULL, NULL },
+ { "get gateway IP", CMD_GET_GWIP, NULL, NULL, NULL },
+ { "get gateway MAC", CMD_GET_GWMAC, NULL, NULL, NULL },
+ { "set gateway route", CMD_ADD_REAL_DEFAULT_ROUTE, NULL, NULL, NULL },
+ { "del gateway route", CMD_DEL_REAL_DEFAULT_ROUTE, NULL, NULL, NULL },
+ { "set tunnel route", CMD_ADD_FAKE_DEFAULT_ROUTE, NULL, NULL, NULL },
+ { "del tunnel route", CMD_DEL_FAKE_DEFAULT_ROUTE, NULL, NULL, NULL },
+ { "add incoming filter", CMD_ADD_INCOMING_FILTER, NULL, NULL, NULL },
+ { "del incoming filter", CMD_DEL_INCOMING_FILTER, NULL, NULL, NULL },
+ { "add forward filter", CMD_ADD_FORWARD_FILTER, NULL, NULL, NULL },
+ { "del incoming filter", CMD_DEL_FORWARD_FILTER, NULL, NULL, NULL },
+ { "add tun masquerade", CMD_ADD_TUN_MASQUERADE, NULL, NULL, NULL },
+ { "del tun masquerade", CMD_DEL_TUN_MASQUERADE, NULL, NULL, NULL },
+ { "set tun", CMD_SETUP_TUN, NULL, NULL, NULL }
};
-static void execOSCmd(char *buf, size_t bufsize, const char *format, ...)
-{
- char cmd[CONST_JANUS_BUFSIZE] = {0};
- FILE *stream = NULL;
-
- va_list arguments;
- va_start(arguments, format);
- vsnprintf(cmd, sizeof (cmd), format, arguments);
- va_end(arguments);
-
- printf("executing cmd: [%s]\n", cmd);
-
- stream = popen(cmd, "r");
- if (stream != NULL)
- {
- if (buf != NULL)
- {
- memset(buf, 0, bufsize);
+/*
+ * with janus package will became installed:
+ * /etc/janus
+ * /etc/janus/current-os -> /etc/janus/os-commands/Linux-whatever.janus
+ *
+ * Linux-whatever.janus is the configuration file containing the commands
+ * executed to obtain the required effect on the running system.
+ *
+ * every new operating system must have a checked configuration file, with
+ * the execution of janus-tester, an executable obtained compiling this file
+ * with -DJANUS-TESTER in the command line.
+ */
+#define OSSELECTED "/etc/janus/current-os"
+#define LINESIZE 256
- if (fgets(buf, bufsize, stream) != NULL)
- {
- const size_t len = strlen(buf);
+/* two, and only two "#" are expected in a command line */
+int cmd_test_check(char *line)
+{
+ int cnt = 0, i = 0;
- if (len && buf[len - 1] == '\n')
- buf[len - 1] = '\0';
- }
- }
+ for(i = 0 ; i < LINESIZE || line[i] == 0x00; i++)
+ if(line[i] == '#')
+ cnt++;
- pclose(stream);
- }
+ return (cnt == 2);
}
-static void (*bindCmd(struct cmd_sw cmd[]))(char* buf, size_t bufsize)
+void *perm_extract(char *line)
{
- char test[CONST_JANUS_BUFSIZE] = {0};
+ char swapL[LINESIZE];
+ int i, j = 0;
+ int good = 0;
+
+ memset(&swapL, 0x00, LINESIZE);
- uint8_t i = 0;
- while (cmd[i].cmd_test != NULL)
+ for(i = 0; i < strlen(line); i++)
{
- execOSCmd(test, sizeof (test), "which %s", cmd[i].cmd_test);
- if (strlen(test))
- {
- printf("binding executed using: %s\n", cmd[i].cmd_test);
- return cmd[i].cmd_ex;
+ if( good == 0 && line[i] == '#') {
+ good = 1;
+ continue;
}
- ++i;
+ if( good == 1 && line[i] == '#') {
+ good = 0;
+ break;
+ }
+
+ if( good )
+ swapL[j++] = line[i];
}
- return NULL;
+ return (void *)strdup(swapL);
}
-static void cmd0_route(char* buf, size_t bufsize)
+int get_code_index(char *inpline, int *readed, uint8_t *mean)
{
- execOSCmd(buf, bufsize, "route -n | sed -n 's/^\\(0.0.0.0\\).* \\([0-9.]\\{7,15\\}\\) .*\\(0.0.0.0\\).*UG.* \\(.*\\)$/\\4/p'");
-}
+ if( strlen(inpline) < 8 || !isdigit(inpline[0]) || !isdigit(inpline[1]) ||
+ (inpline[2] != 'C' && inpline[2] != 'T') || inpline[3] != ' ' || inpline[4] != '#')
+ {
+ printf("invalid format in line: require \"DDC #\": digit digit code (T|C) space and #\n");
+ return 0;
+ }
-static void cmd1_ifconfig(char* buf, size_t bufsize)
-{
- execOSCmd(buf, bufsize, "ifconfig %s | sed -n 's/.*inet addr:\\([0-9.]\\+\\) .*$/\\1/p'", str_map[STR_NET_IF]);
-}
+ *readed = 0;
+ /* the -48 is because 48 is the ASCII value of '0', this is not propery clean ;P */
+ *readed += (((int)inpline[0] - 48) * 10);
+ *readed += ((int)inpline[1] - 48);
-static void cmd2_ifconfig(char* buf, size_t bufsize)
-{
- execOSCmd(buf, bufsize, "ifconfig %s | sed -n 's/^.* HWaddr \\([a-fA-F0-9:]\\{17,17\\}\\).*$/\\1/p'", str_map[STR_NET_IF]);
-}
+ *mean = inpline[2];
-static void cmd3_ifconfig(char* buf, size_t bufsize)
-{
- execOSCmd(buf, bufsize, "ifconfig -a %s | sed -n 's/^.* MTU:\\([0-9]*\\) .*$/\\1/p'", str_map[STR_NET_IF]);
+ return 1;
}
-static void cmd4_route(char* buf, size_t bufsize)
+/* janus configuration has a number and a "meaning"
+ * 1C #command#
+ * 1T #command showing the test of successful working of 1C#
+ * usage of format: the output of a command will be inserted with ~[number of command]
+ *
+ * example, 1 is "local interface name":
+ * 1C #route -n | grep "0.0.0.0" | awk {'print $5'}#
+ * 1T ##
+ *
+ * new example + remind: 4 is the number of "tunnel interface name"
+ * + remind: 10 is the number of "set tunnel gateway"
+ * 10C #route add default gw ~4#
+ * 10T #route -n#
+ * _______ ______________________________________
+ * special characters in the configuration file: # and ~, THEY ARE NOT USABLE INSIDE THE COMMANS
+ * ^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+ */
+int janus_commands_file_setup(FILE *oscmds)
{
- execOSCmd(buf, bufsize, "route -n | sed -n 's/^\\(0.0.0.0\\).* \\([0-9.]\\{7,15\\}\\) .*\\(0.0.0.0\\).*UG.* %s$/\\2/p'", str_map[STR_NET_IF]);
-}
+ int fndx = 0, ourndx = 0;
+ uint8_t ourmean;
-static void cmd5_arp(char* buf, size_t bufsize)
-{
- execOSCmd(buf, bufsize, "arp -ni %s %s | sed -n 's/^.*\\([a-fA-F0-9:]\\{17,17\\}\\).*$/\\1/p'", str_map[STR_NET_IF], str_map[STR_GW_IP]);
-}
+ while(!feof(oscmds))
+ {
+ char rdLine[LINESIZE];
-static void cmd5_arping(char* buf, size_t bufsize)
-{
- execOSCmd(buf, bufsize, "arping -f -I %s %s | sed -n 's/^.*\\([a-fA-F0-9:]\\{16,16\\}\\)\\].*$/0\\1/p'", str_map[STR_NET_IF], str_map[STR_GW_IP]);
-}
+ memset(&rdLine[0], 0x00, LINESIZE);
+ fgets(rdLine, LINESIZE, oscmds);
+ fndx++;
-static void cmd6_route(char* buf, size_t bufsize)
-{
- execOSCmd(NULL, 0, "route add default gw %s dev %s", str_map[STR_GW_IP], str_map[STR_NET_IF]);
-}
+ if(strlen(rdLine) > (LINESIZE - 1) || strlen(rdLine) < 4 || rdLine[0] == '#')
+ continue;
-static void cmd7_route(char* buf, size_t bufsize)
-{
- execOSCmd(NULL, 0, "route del default gw %s dev %s", str_map[STR_GW_IP], str_map[STR_NET_IF]);
-}
+ if(rdLine[0] < '0' || rdLine[0] > '9') {
+ printf("invalid non-number at the start of line %d\n", fndx);
+ return 0;
+ }
-static void cmd8_route(char* buf, size_t bufsize)
-{
- execOSCmd(NULL, 0, "route add default gw %s dev %s", str_map[STR_TUN_IP], str_map[STR_TUN_IF]);
-}
+ if(!get_code_index(rdLine, &ourndx, &ourmean)) {
+ printf("invalid number or code at the start of the line %d\n", fndx);
+ return 0;
+ }
-static void cmd9_route(char* buf, size_t bufsize)
-{
- execOSCmd(NULL, 0, "route del default gw %s dev %s", str_map[STR_TUN_IP], str_map[STR_TUN_IF]);
-}
+ if(ourndx >= COMMANDS_NUM) {
+ printf("command code too much higter (%d with a limit of %d), line %d\n",
+ ourndx, COMMANDS_NUM, fndx);
+ return 0;
+ }
-static void cmd10_iptables(char* buf, size_t bufsize)
-{
- execOSCmd(NULL, 0, "iptables -A INPUT -i %s -m mac --mac-source %s -j DROP", str_map[STR_NET_IF], str_map[STR_GW_MAC]);
-}
+ if(!cmd_test_check(rdLine)) {
+ printf("incorrect use of #..# at line %d [%s]\n", fndx, rdLine);
+ return 0;
+ }
-static void cmd11_iptables(char* buf, size_t bufsize)
-{
- execOSCmd(NULL, 0, "iptables -D INPUT -i %s -m mac --mac-source %s -j DROP", str_map[STR_NET_IF], str_map[STR_GW_MAC]);
-}
+ if(ourmean == 'C') /* command */
+ {
+ if((os_cfg[ourndx].command = perm_extract(rdLine)) == NULL) {
+ printf("unable to parse correctly the \"command\" at line %d\n", fndx);
+ return 0;
+ }
+ }
-static void cmd12_iptables(char* buf, size_t bufsize)
-{
- execOSCmd(NULL, 0, "iptables -A FORWARD -i %s -m mac --mac-source %s -j DROP", str_map[STR_NET_IF], str_map[STR_GW_MAC]);
-}
+ if(ourmean == 'T') /* test */
+ {
+ if((os_cfg[ourndx].test = perm_extract(rdLine)) == NULL) {
+ printf("unable to parse correctly the \"test\" at line %d\n", fndx);
+ return 0;
+ }
+ }
+ }
-static void cmd13_iptables(char* buf, size_t bufsize)
-{
- execOSCmd(NULL, 0, "iptables -D FORWARD -i %s -m mac --mac-source %s -j DROP", str_map[STR_NET_IF], str_map[STR_GW_MAC]);
+ return 1;
}
-static void cmd14_iptables(char* buf, size_t bufsize)
+char *expand_command(char *original_rawcmd)
{
- execOSCmd(NULL, 0, "iptables -A POSTROUTING -o %s -t nat -j MASQUERADE", str_map[STR_TUN_IF]);
-}
+ /* as first: rawcmd is the stored buffer in the os_cfg global struct: must not be touched */
+ char *tofree = strdup(original_rawcmd);
+ char *rawcmd = tofree;
-static void cmd15_iptables(char* buf, size_t bufsize)
-{
- execOSCmd(NULL, 0, "iptables -D POSTROUTING -o %s -t nat -j MASQUERADE", str_map[STR_TUN_IF]);
-}
+ static char retbuf[LINESIZE];
+ char *p = strchr( rawcmd, (int)'~');
+ int readVal = 0, j = 0;
-static void cmd16_ifconfig(char* buf, size_t bufsize)
-{
- execOSCmd(NULL, 0, "ifconfig %s %s pointopoint %s mtu %s", str_map[STR_TUN_IF], str_map[STR_NET_IP], str_map[STR_TUN_IP], str_map[STR_TUN_MTU]);
-}
+ if(p == NULL) {
+ free(tofree);
+ return original_rawcmd;
+ }
-static struct cmd_sw cmd0_sw[] = {
- {"route", cmd0_route},
- {NULL, NULL}
-};
+ printf("from [%s]\n", rawcmd);
-static struct cmd_sw cmd1_sw[] = {
- {"ifconfig", cmd1_ifconfig},
- {NULL, NULL}
-};
+ memset(&retbuf[0], ' ', LINESIZE);
-static struct cmd_sw cmd2_sw[] = {
- {"ifconfig", cmd2_ifconfig},
- {NULL, NULL}
-};
+ for( ; p != NULL ; p = strchr(rawcmd, '~') )
+ {
+ *p = 0x00;
-static struct cmd_sw cmd3_sw[] = {
- {"ifconfig", cmd3_ifconfig},
- {NULL, NULL}
-};
+/* printf(" %d ", j); */
+ memcpy( &retbuf[j], rawcmd, strlen(rawcmd) );
+ j += strlen(rawcmd) ;
-static struct cmd_sw cmd4_sw[] = {
- {"route", cmd4_route},
- {NULL, NULL}
-};
+/* printf(" %d (%d) [%s]", j, strlen(rawcmd), rawcmd); */
+ readVal = (10 * ((int)*++p - 48) );
+ readVal += (*++p - 48);
+ rawcmd = ++p;
-static struct cmd_sw cmd5_sw[] = {
- {"arp", cmd5_arp},
- {"arping", cmd5_arping},
- {NULL, NULL}
-};
+ /* remind: has to became _readVal_instead_of_0_ */
+ memcpy( &retbuf[j], os_cfg[readVal].output, strlen(os_cfg[readVal].output) );
+ j += strlen(os_cfg[readVal].output);
-static struct cmd_sw cmd6_sw[] = {
- {"route", cmd6_route},
- {NULL, NULL}
-};
+/* printf(" %d (%d) [%s] (di %d)\n", j, strlen(os_cfg[readVal].output), os_cfg[readVal].output, readVal); */
+ }
-static struct cmd_sw cmd7_sw[] = {
- {"route", cmd7_route},
- {NULL, NULL}
-};
+ memcpy(&retbuf[j], rawcmd, strlen(rawcmd));
+ j += strlen(rawcmd);
+ retbuf[j] = 0x00;
-static struct cmd_sw cmd8_sw[] = {
- {"route", cmd8_route},
- {NULL, NULL}
-};
+ printf("to [%s]\n", retbuf);
+ /* remind: rawcmd is the working copy to destroy, but the ptr *rawcmd is moved. "tofree" kept track */
+ free(tofree);
-static struct cmd_sw cmd9_sw[] = {
- {"route", cmd9_route},
- {NULL, NULL}
-};
+ return &retbuf[0];
+}
-static struct cmd_sw cmd10_sw[] = {
- {"iptables", cmd10_iptables},
- {NULL, NULL}
-};
+void clean_retbuf(char *retbuf, char *arrayofstrip)
+{
+ int i;
+ for(i =0; i < strlen(arrayofstrip); i++)
+ {
+ char *underCheck = strchr(retbuf, arrayofstrip[i]);
-static struct cmd_sw cmd11_sw[] = {
- {"iptables", cmd11_iptables},
- {NULL, NULL}
-};
+ if(underCheck != NULL)
+ *underCheck = 0x00;
+ }
+}
-static struct cmd_sw cmd12_sw[] = {
- {"iptables", cmd12_iptables},
- {NULL, NULL}
-};
+char *do_popen(char *command)
+{
+#define SIZEBULK 4096
+ char buffer[SIZEBULK];
+ FILE *outshell;
-static struct cmd_sw cmd13_sw[] = {
- {"iptables", cmd13_iptables},
- {NULL, NULL}
-};
+ memset(buffer, 0x00, SIZEBULK);
-static struct cmd_sw cmd14_sw[] = {
- {"iptables", cmd14_iptables},
- {NULL, NULL}
-};
+ printf("the command is [%s]\n", command);
-static struct cmd_sw cmd15_sw[] = {
- {"iptables", cmd15_iptables},
- {NULL, NULL}
-};
+ if((outshell = popen(command, "r")) == NULL) {
+ printf("command [%s] no pipe open!\n", command);
+ return NULL;
+ }
-static struct cmd_sw cmd16_sw[] = {
- {"ifconfig", cmd16_ifconfig},
- {NULL, NULL}
-};
+ if(fgets(buffer, SIZEBULK, outshell) == NULL)
+ {
+ printf("command [%s] return no any answer!\n", command);
+ return NULL;
+ }
+ pclose(outshell);
+
+ clean_retbuf(buffer, "\r\n");
+ return (char *)strdup(buffer);
+}
-static struct
+char *do_os_command(int cmd_code)
{
- struct cmd_sw *sw;
-} cmd_sw_table[] = {
- {cmd0_sw},
- {cmd1_sw},
- {cmd2_sw},
- {cmd3_sw},
- {cmd4_sw},
- {cmd5_sw},
- {cmd6_sw},
- {cmd7_sw},
- {cmd8_sw},
- {cmd9_sw},
- {cmd10_sw},
- {cmd11_sw},
- {cmd12_sw},
- {cmd13_sw},
- {cmd14_sw},
- {cmd15_sw},
- {cmd16_sw},
- {0}
-};
+ int i;
-static void (*cmd[COMMANDS_NUM])(char* buf, size_t bufsize);
+ /* get every possibile required string */
+ for(i = 0; i < STRINGS_NUM; i++)
+ {
+ if(os_cfg[i].output == NULL && (os_cfg[i].command != NULL))
+ {
+ printf("trying to expand %d: [%s]\n", i, (char *)os_cfg[i].command);
+ os_cfg[i].output = do_popen(expand_command(os_cfg[i].command));
+ if(os_cfg[i].output == NULL)
+ {
+ printf("unable to fucking execute and read an answer from [%s]!!\n", (char *)os_cfg[i].command);
+ return NULL;
+ }
+ printf("debug: output for %d is [%s]\n", i, (char *)os_cfg[i].output);
+ }
+ }
+
+ printf("executing the requested command: %d [%s]\n", cmd_code, (char *)os_cfg[cmd_code].command);
+
+ os_cfg[cmd_code].output = do_popen(expand_command(os_cfg[cmd_code].command));
+ return os_cfg[cmd_code].output;
+}
-static void bindCmds(void)
+#ifdef JANUSTESTER
+int main(int argc, char **argv)
{
- char test[CONST_JANUS_BUFSIZE] = {0};
+ int i;
+ FILE *input;
- uint8_t i;
+ if(argc != 2)
+ return printf("%s [os selected command specification file]\ncheck janus(8) manpage!\n", argv[0]);
- printf("checking sed command presence\n");
- execOSCmd(test, sizeof (test), "which sed");
- if (!strlen(test))
- runtime_exception("unable to find sed command");
+ if((input = fopen(argv[1], "r")) == NULL)
+ return printf("unable to open %s\n", argv[1]);
- for (i = 0; cmd_sw_table[i].sw != NULL; ++i)
- {
- printf("binding cmd %u to a system command\n", i);
- cmd[i] = bindCmd(cmd_sw_table[i].sw);
- if (cmd[i] == NULL)
- runtime_exception("unable to bind cmd %u to a system command", i);
+ if(!janus_commands_file_setup(input))
+ return;
+
+ printf("testing of %s: extracting infos\n", argv[1]);
+
+ for( i = 0; i < STRINGS_NUM; i++) {
+ if(os_cfg[i].command != NULL)
+ printf("%s: %s\n", os_cfg[i].string, do_os_command(i) );
+ else
+ printf("%s: not configured!\n");
}
+
+ printf("testing of %s: executing set/del commands\n", argv[1]);
+
+ for( i = FIRST_CMD_NUMBER; i < LAST_CMD_NUMBER; i++) {
+ if(os_cfg[i].command != NULL)
+ printf("%s: %s\n", os_cfg[i].string, do_os_command(i) );
+ else
+ printf("%s: not configured!\n");
+ }
+
+ return 0;
}
+#endif
View
355 src/os_cmds_under_study.c
@@ -1,355 +0,0 @@
-/*
- * Janus, a portable, unified and lightweight interface for mitm
- * applications over the traffic directed to the default gateway.
- *
- * Copyright (C) 2011 evilaliv3 <giovanni.pellerano@evilaliv3.org>
- * vecna <vecna@delirandom.net>
- *
- * 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 3 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, see <http://www.gnu.org/licenses/>.
- *
- */
-#include <stdint.h>
-#include <stdio.h>
-#include <string.h>
-#include <ctype.h>
-#include "config_macros.h"
-
-struct target_map
-{
- char *string;
- uint8_t index;
- void *command;
- void *output;
- void *test;
-} os_cfg[] = {
- { "local interface name", STR_NET_IF, NULL, NULL, NULL },
- { "local interface IP", STR_NET_IP, NULL, NULL, NULL },
- { "local interface MAC", STR_NET_MAC, NULL, NULL, NULL },
- { "local interface MTU", STR_NET_MTU, NULL, NULL, NULL },
- { "tunnel interface name", STR_TUN_IF, NULL, NULL, NULL },
- { "tunnel interface IP", STR_TUN_IP, NULL, NULL, NULL },
- { "get local iface name", CMD_GET_NETIF, NULL, NULL, NULL },
- { "get local iface IP", CMD_GET_NETIP, NULL, NULL, NULL },
- { "get gateway IP", CMD_GET_GWIP, NULL, NULL, NULL },
- { "get gateway MAC", CMD_GET_GWMAC, NULL, NULL, NULL },
- { "set gateway route", CMD_ADD_REAL_DEFAULT_ROUTE, NULL, NULL, NULL },
- { "del gateway route", CMD_DEL_REAL_DEFAULT_ROUTE, NULL, NULL, NULL },
- { "set tunnel route", CMD_ADD_FAKE_DEFAULT_ROUTE, NULL, NULL, NULL },
- { "del tunnel route", CMD_DEL_FAKE_DEFAULT_ROUTE, NULL, NULL, NULL },
- { "add incoming filter", CMD_ADD_INCOMING_FILTER, NULL, NULL, NULL },
- { "del incoming filter", CMD_DEL_INCOMING_FILTER, NULL, NULL, NULL },
- { "add forward filter", CMD_ADD_FORWARD_FILTER, NULL, NULL, NULL },
- { "del incoming filter", CMD_DEL_FORWARD_FILTER, NULL, NULL, NULL },
- { "add tun masquerade", CMD_ADD_TUN_MASQUERADE, NULL, NULL, NULL },
- { "del tun masquerade", CMD_DEL_TUN_MASQUERADE, NULL, NULL, NULL },
- { "set tun", CMD_SETUP_TUN, NULL, NULL, NULL }
-};
-
-/*
- * with janus package will became installed:
- * /etc/janus
- * /etc/janus/current-os -> /etc/janus/os-commands/Linux-whatever.janus
- *
- * Linux-whatever.janus is the configuration file containing the commands
- * executed to obtain the required effect on the running system.
- *
- * every new operating system must have a checked configuration file, with
- * the execution of janus-tester, an executable obtained compiling this file
- * with -DJANUS-TESTER in the command line.
- */
-#define OSSELECTED "/etc/janus/current-os"
-#define LINESIZE 256
-
-/* two, and only two "#" are expected in a command line */
-int cmd_test_check(char *line)
-{
- int cnt = 0, i = 0;
-
- for(i = 0 ; i < LINESIZE || line[i] == 0x00; i++)
- if(line[i] == '#')
- cnt++;
-
- return (cnt == 2);
-}
-
-void *perm_extract(char *line)
-{
- char swapL[LINESIZE];
- int i, j = 0;
- int good = 0;
-
- memset(&swapL, 0x00, LINESIZE);
-
- for(i = 0; i < strlen(line); i++)
- {
- if( good == 0 && line[i] == '#') {
- good = 1;
- continue;
- }
-
- if( good == 1 && line[i] == '#') {
- good = 0;
- break;
- }
-
- if( good )
- swapL[j++] = line[i];
- }
-
- return (void *)strdup(swapL);
-}
-
-int get_code_index(char *inpline, int *readed, uint8_t *mean)
-{
- if( strlen(inpline) < 8 || !isdigit(inpline[0]) || !isdigit(inpline[1]) ||
- (inpline[2] != 'C' && inpline[2] != 'T') || inpline[3] != ' ' || inpline[4] != '#')
- {
- printf("invalid format in line: require \"DDC #\": digit digit code (T|C) space and #\n");
- return 0;
- }
-
- *readed = 0;
- /* the -48 is because 48 is the ASCII value of '0', this is not propery clean ;P */
- *readed += (((int)inpline[0] - 48) * 10);
- *readed += ((int)inpline[1] - 48);
-
- *mean = inpline[2];
-
- return 1;
-}
-
-/* janus configuration has a number and a "meaning"
- * 1C #command#
- * 1T #command showing the test of successful working of 1C#
- * usage of format: the output of a command will be inserted with ~[number of command]
- *
- * example, 1 is "local interface name":
- * 1C #route -n | grep "0.0.0.0" | awk {'print $5'}#
- * 1T ##
- *
- * new example + remind: 4 is the number of "tunnel interface name"
- * + remind: 10 is the number of "set tunnel gateway"
- * 10C #route add default gw ~4#
- * 10T #route -n#
- * _______ ______________________________________
- * special characters in the configuration file: # and ~, THEY ARE NOT USABLE INSIDE THE COMMANS
- * ^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- */
-int janus_commands_file_setup(FILE *oscmds)
-{
- int fndx = 0, ourndx = 0;
- uint8_t ourmean;
-
- while(!feof(oscmds))
- {
- char rdLine[LINESIZE];
-
- memset(&rdLine[0], 0x00, LINESIZE);
- fgets(rdLine, LINESIZE, oscmds);
- fndx++;
-
- if(strlen(rdLine) > (LINESIZE - 1) || strlen(rdLine) < 4 || rdLine[0] == '#')
- continue;
-
- if(rdLine[0] < '0' || rdLine[0] > '9') {
- printf("invalid non-number at the start of line %d\n", fndx);
- return 0;
- }
-
- if(!get_code_index(rdLine, &ourndx, &ourmean)) {
- printf("invalid number or code at the start of the line %d\n", fndx);
- return 0;
- }
-
- if(ourndx >= COMMANDS_NUM) {
- printf("command code too much higter (%d with a limit of %d), line %d\n",
- ourndx, COMMANDS_NUM, fndx);
- return 0;
- }
-
- if(!cmd_test_check(rdLine)) {
- printf("incorrect use of #..# at line %d [%s]\n", fndx, rdLine);
- return 0;
- }
-
- if(ourmean == 'C') /* command */
- {
- if((os_cfg[ourndx].command = perm_extract(rdLine)) == NULL) {
- printf("unable to parse correctly the \"command\" at line %d\n", fndx);
- return 0;
- }
- }
-
- if(ourmean == 'T') /* test */
- {
- if((os_cfg[ourndx].test = perm_extract(rdLine)) == NULL) {
- printf("unable to parse correctly the \"test\" at line %d\n", fndx);
- return 0;
- }
- }
- }
-
- return 1;
-}
-
-char *expand_command(char *original_rawcmd)
-{
- /* as first: rawcmd is the stored buffer in the os_cfg global struct: must not be touched */
- char *tofree = strdup(original_rawcmd);
- char *rawcmd = tofree;
-
- static char retbuf[LINESIZE];
- char *p = strchr( rawcmd, (int)'~');
- int readVal = 0, j = 0;
-
- if(p == NULL) {
- free(tofree);
- return original_rawcmd;
- }
-
- printf("from [%s]\n", rawcmd);
-
- memset(&retbuf[0], ' ', LINESIZE);
-
- for( ; p != NULL ; p = strchr(rawcmd, '~') )
- {
- *p = 0x00;
-
-/* printf(" %d ", j); */
- memcpy( &retbuf[j], rawcmd, strlen(rawcmd) );
- j += strlen(rawcmd) ;
-
-/* printf(" %d (%d) [%s]", j, strlen(rawcmd), rawcmd); */
- readVal = (10 * ((int)*++p - 48) );
- readVal += (*++p - 48);
- rawcmd = ++p;
-
- /* remind: has to became _readVal_instead_of_0_ */
- memcpy( &retbuf[j], os_cfg[readVal].output, strlen(os_cfg[readVal].output) );
- j += strlen(os_cfg[readVal].output);
-
-/* printf(" %d (%d) [%s] (di %d)\n", j, strlen(os_cfg[readVal].output), os_cfg[readVal].output, readVal); */
- }
-
- memcpy(&retbuf[j], rawcmd, strlen(rawcmd));
- j += strlen(rawcmd);
- retbuf[j] = 0x00;
-
- printf("to [%s]\n", retbuf);
- /* remind: rawcmd is the working copy to destroy, but the ptr *rawcmd is moved. "tofree" kept track */
- free(tofree);
-
- return &retbuf[0];
-}
-
-void clean_retbuf(char *retbuf, char *arrayofstrip)
-{
- int i;
- for(i =0; i < strlen(arrayofstrip); i++)
- {
- char *underCheck = strchr(retbuf, arrayofstrip[i]);
-
- if(underCheck != NULL)
- *underCheck = 0x00;
- }
-}
-
-char *do_popen(char *command)
-{
-#define SIZEBULK 4096
- char buffer[SIZEBULK];
- FILE *outshell;
-
- memset(buffer, 0x00, SIZEBULK);
-
- printf("the command is [%s]\n", command);
-
- if((outshell = popen(command, "r")) == NULL) {
- printf("command [%s] no pipe open!\n", command);
- return NULL;
- }
-
- if(fgets(buffer, SIZEBULK, outshell) == NULL)
- {
- printf("command [%s] return no any answer!\n", command);
- return NULL;
- }
- pclose(outshell);
-
- clean_retbuf(buffer, "\r\n");
- return (char *)strdup(buffer);
-}
-
-char *do_os_command(int cmd_code)
-{
- int i;
-
- /* get every possibile required string */
- for(i = 0; i < STRINGS_NUM; i++)
- {
- if(os_cfg[i].output == NULL && (os_cfg[i].command != NULL))
- {
- printf("trying to expand %d: [%s]\n", i, (char *)os_cfg[i].command);
- os_cfg[i].output = do_popen(expand_command(os_cfg[i].command));
- if(os_cfg[i].output == NULL)
- {
- printf("unable to fucking execute and read an answer from [%s]!!\n", (char *)os_cfg[i].command);
- return NULL;
- }
- printf("debug: output for %d is [%s]\n", i, (char *)os_cfg[i].output);
- }
- }
-
- printf("executing the requested command: %d [%s]\n", cmd_code, (char *)os_cfg[cmd_code].command);
-
- os_cfg[cmd_code].output = do_popen(expand_command(os_cfg[cmd_code].command));
- return os_cfg[cmd_code].output;
-}
-
-#ifdef JANUSTESTER
-int main(int argc, char **argv)
-{
- int i;
- FILE *input;
-
- if(argc != 2)
- return printf("%s [os selected command specification file]\ncheck janus(8) manpage!\n", argv[0]);
-
- if((input = fopen(argv[1], "r")) == NULL)
- return printf("unable to open %s\n", argv[1]);
-
- if(!janus_commands_file_setup(input))
- return;
-
- printf("testing of %s: extracting infos\n", argv[1]);
-
- for( i = 0; i < STRINGS_NUM; i++) {
- if(os_cfg[i].command != NULL)
- printf("%s: %s\n", os_cfg[i].string, do_os_command(i) );
- else
- printf("%s: not configured!\n");
- }
-
- printf("testing of %s: executing set/del commands\n", argv[1]);
-
- for( i = FIRST_CMD_NUMBER; i < LAST_CMD_NUMBER; i++) {
- if(os_cfg[i].command != NULL)
- printf("%s: %s\n", os_cfg[i].string, do_os_command(i) );
- else
- printf("%s: not configured!\n");
- }
-
- return 0;
-}
-#endif

0 comments on commit eb7e0db

Please sign in to comment.