]>
git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/misc-progs/wirelessctrl.c
1 /* IPCop helper program - wirelessctrl
3 * This program is distributed under the terms of the GNU General Public
4 * Licence. See the file COPYING for details.
6 * (c) Alan Hourihane, 2003
8 * $Id: wirelessctrl.c,v 1.2.2.5 2005/07/11 10:56:47 franck78 Exp $
12 #include "libsmooth.h"
18 #include <sys/types.h>
25 char blue_dev
[STRING_SIZE
] = "";
26 char command
[STRING_SIZE
];
28 void exithandler(void)
30 struct keyvalue
*kv
= NULL
;
31 char buffer
[STRING_SIZE
];
34 if(findkey(kv
, "DROPWIRELESSINPUT", buffer
) && !strcmp(buffer
,"on")){
35 snprintf(command
, STRING_SIZE
-1, "/sbin/iptables -A WIRELESSINPUT -i %s -j LOG --log-prefix 'DROP_Wirelessinput'", blue_dev
);
38 if(findkey(kv
, "DROPWIRELESSFORWARD", buffer
) && !strcmp(buffer
,"on")){
39 snprintf(command
, STRING_SIZE
-1, "/sbin/iptables -A WIRELESSFORWARD -i %s -j LOG --log-prefix 'DROP_Wirelessforward'", blue_dev
);
42 snprintf(command
, STRING_SIZE
-1, "/sbin/iptables -A WIRELESSINPUT -i %s -j DROP -m comment --comment 'DROP_Wirelessinput'", blue_dev
);
44 snprintf(command
, STRING_SIZE
-1, "/sbin/iptables -A WIRELESSINPUT -i %s -j DROP -m comment --comment 'DROP_Wirelessforward'", blue_dev
);
54 char green_dev
[STRING_SIZE
] = "";
55 char buffer
[STRING_SIZE
];
56 char *index
, *ipaddress
, *macaddress
, *enabled
;
57 struct keyvalue
*kv
= NULL
;
62 /* flush wireless iptables */
63 safe_system("/sbin/iptables -F WIRELESSINPUT > /dev/null 2> /dev/null");
64 safe_system("/sbin/iptables -F WIRELESSFORWARD > /dev/null 2> /dev/null");
66 memset(buffer
, 0, STRING_SIZE
);
68 /* Init the keyvalue structure */
71 /* Read in the current values */
72 if (!readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings"))
74 fprintf(stderr
, "Cannot read ethernet settings\n");
78 /* Read in the firewall values */
79 if (!readkeyvalues(kv
, CONFIG_ROOT
"/optionsfw/settings"))
81 fprintf(stderr
, "Cannot read optionsfw settings\n");
85 /* Get the GREEN interface details */
86 if(!findkey(kv
, "GREEN_DEV", green_dev
))
88 fprintf(stderr
, "Cannot read GREEN_DEV\n");
91 if (!VALID_DEVICE(green_dev
))
93 fprintf(stderr
, "Bad GREEN_DEV: %s\n", green_dev
);
96 /* Get the BLUE interface details */
97 if(!findkey(kv
, "BLUE_DEV", blue_dev
))
99 fprintf(stderr
, "Cannot read BLUE_DEV\n");
102 if (strlen(blue_dev
) && !VALID_DEVICE(blue_dev
))
104 fprintf(stderr
, "Bad BLUE_DEV: %s\n", blue_dev
);
107 if(! strlen(blue_dev
) > 0)
109 fprintf(stderr
, "No BLUE interface\n");
113 /* register exit handler to ensure the block rule is always present */
116 if (!(fd
= fopen(CONFIG_ROOT
"/wireless/config", "r")))
120 while (fgets(buffer
, STRING_SIZE
, fd
))
122 buffer
[strlen(buffer
) - 1] = 0;
124 index
= strtok(buffer
, ",");
125 ipaddress
= strtok(NULL
, ",");
126 macaddress
= strtok(NULL
, ",");
127 enabled
= strtok(NULL
, ",");
129 if (!strncmp(enabled
, "on", 2)) {
131 /* both specified, added security */
132 if ((strlen(macaddress
) == 17) &&
133 (VALID_IP(ipaddress
))) {
134 snprintf(command
, STRING_SIZE
-1, "/sbin/iptables -A WIRELESSINPUT -m mac --mac-source %s -s %s -i %s -j ACCEPT", macaddress
, ipaddress
, blue_dev
);
135 safe_system(command
);
136 snprintf(command
, STRING_SIZE
-1, "/sbin/iptables -A WIRELESSFORWARD -m mac --mac-source %s -s %s -i %s -o ! %s -j ACCEPT", macaddress
, ipaddress
, blue_dev
, green_dev
);
137 safe_system(command
);
138 snprintf(command
, STRING_SIZE
-1, "/sbin/iptables -A WIRELESSFORWARD -m mac --mac-source %s -s %s -i %s -j DMZHOLES", macaddress
, ipaddress
, blue_dev
);
139 safe_system(command
);
142 /* correctly formed mac address is 17 chars */
143 if (strlen(macaddress
) == 17) {
144 snprintf(command
, STRING_SIZE
-1, "/sbin/iptables -A WIRELESSINPUT -m mac --mac-source %s -i %s -j ACCEPT", macaddress
, blue_dev
);
145 safe_system(command
);
146 snprintf(command
, STRING_SIZE
-1, "/sbin/iptables -A WIRELESSFORWARD -m mac --mac-source %s -i %s -o ! %s -j ACCEPT", macaddress
, blue_dev
, green_dev
);
147 safe_system(command
);
148 snprintf(command
, STRING_SIZE
-1, "/sbin/iptables -A WIRELESSFORWARD -m mac --mac-source %s -i %s -j DMZHOLES", macaddress
, blue_dev
);
149 safe_system(command
);
152 if (VALID_IP(ipaddress
)) {
153 snprintf(command
, STRING_SIZE
-1, "/sbin/iptables -A WIRELESSINPUT -s %s -i %s -j ACCEPT", ipaddress
, blue_dev
);
154 safe_system(command
);
155 snprintf(command
, STRING_SIZE
-1, "/sbin/iptables -A WIRELESSFORWARD -s %s -i %s -o ! %s -j ACCEPT", ipaddress
, blue_dev
, green_dev
);
156 safe_system(command
);
157 snprintf(command
, STRING_SIZE
-1, "/sbin/iptables -A WIRELESSFORWARD -s %s -i %s -j DMZHOLES", ipaddress
, blue_dev
);
158 safe_system(command
);