]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blame - src/misc-progs/wirelessctrl.c
fixed wirelesscontrol and included in core 8
[people/teissler/ipfire-2.x.git] / src / misc-progs / wirelessctrl.c
CommitLineData
900832fa
CS
1/* IPCop helper program - wirelessctrl
2 *
3 * This program is distributed under the terms of the GNU General Public
4 * Licence. See the file COPYING for details.
5 *
6 * (c) Alan Hourihane, 2003
7 *
8 * $Id: wirelessctrl.c,v 1.2.2.5 2005/07/11 10:56:47 franck78 Exp $
9 *
10 */
11
12#include "libsmooth.h"
13#include <stdio.h>
14#include <stdlib.h>
15#include <unistd.h>
16#include <fcntl.h>
17#include <string.h>
18#include <sys/types.h>
19#include <sys/stat.h>
20#include <signal.h>
21#include "setuid.h"
42dc0090 22#include <errno.h>
900832fa
CS
23
24FILE *fd = NULL;
25char blue_dev[STRING_SIZE] = "";
26char command[STRING_SIZE];
27
900832fa
CS
28int main(void)
29{
30 char green_dev[STRING_SIZE] = "";
31 char buffer[STRING_SIZE];
32 char *index, *ipaddress, *macaddress, *enabled;
33 struct keyvalue *kv = NULL;
34
35 if (!(initsetuid()))
36 exit(1);
37
38 /* flush wireless iptables */
39 safe_system("/sbin/iptables -F WIRELESSINPUT > /dev/null 2> /dev/null");
40 safe_system("/sbin/iptables -F WIRELESSFORWARD > /dev/null 2> /dev/null");
41
42 memset(buffer, 0, STRING_SIZE);
43
44 /* Init the keyvalue structure */
45 kv=initkeyvalues();
46
47 /* Read in the current values */
48 if (!readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings"))
49 {
50 fprintf(stderr, "Cannot read ethernet settings\n");
51 exit(1);
52 }
53
42dc0090
CS
54 /* Read in the firewall values */
55 if (!readkeyvalues(kv, CONFIG_ROOT "/optionsfw/settings"))
56 {
57 fprintf(stderr, "Cannot read optionsfw settings\n");
58 exit(1);
59 }
60
900832fa
CS
61 /* Get the GREEN interface details */
62 if(!findkey(kv, "GREEN_DEV", green_dev))
63 {
64 fprintf(stderr, "Cannot read GREEN_DEV\n");
65 exit(1);
66 }
67 if (!VALID_DEVICE(green_dev))
68 {
69 fprintf(stderr, "Bad GREEN_DEV: %s\n", green_dev);
70 exit(1);
71 }
72 /* Get the BLUE interface details */
73 if(!findkey(kv, "BLUE_DEV", blue_dev))
74 {
75 fprintf(stderr, "Cannot read BLUE_DEV\n");
76 exit(1);
77 }
78 if (strlen(blue_dev) && !VALID_DEVICE(blue_dev))
79 {
80 fprintf(stderr, "Bad BLUE_DEV: %s\n", blue_dev);
81 exit(1);
82 }
83 if(! strlen(blue_dev) > 0)
84 {
85 fprintf(stderr, "No BLUE interface\n");
86 exit(0);
87 }
88
87f7ed96
CS
89 if(strlen(blue_dev))
90 {
91 if(findkey(kv, "DROPWIRELESSINPUT", buffer) && !strcmp(buffer,"on")){
92 snprintf(command, STRING_SIZE-1, "/sbin/iptables -A WIRELESSINPUT -i %s -j LOG --log-prefix 'DROP_Wirelessinput'", blue_dev);
93 safe_system(command);
94 }
95 if(findkey(kv, "DROPWIRELESSFORWARD", buffer) && !strcmp(buffer,"on")){
96 snprintf(command, STRING_SIZE-1, "/sbin/iptables -A WIRELESSFORWARD -i %s -j LOG --log-prefix 'DROP_Wirelessforward'", blue_dev);
97 safe_system(command);
98 }
99 snprintf(command, STRING_SIZE-1, "/sbin/iptables -A WIRELESSINPUT -i %s -j DROP -m comment --comment 'DROP_Wirelessinput'", blue_dev);
100 safe_system(command);
101 snprintf(command, STRING_SIZE-1, "/sbin/iptables -A WIRELESSFORWARD -i %s -j DROP -m comment --comment 'DROP_Wirelessforward'", blue_dev);
102 safe_system(command);
103 }
104
105 if (fd)
106 fclose(fd);
900832fa
CS
107
108 if (!(fd = fopen(CONFIG_ROOT "/wireless/config", "r")))
109 {
110 exit(0);
111 }
112 while (fgets(buffer, STRING_SIZE, fd))
113 {
114 buffer[strlen(buffer) - 1] = 0;
115
116 index = strtok(buffer, ",");
117 ipaddress = strtok(NULL, ",");
118 macaddress = strtok(NULL, ",");
119 enabled = strtok(NULL, ",");
120
121 if (!strncmp(enabled, "on", 2)) {
122
123 /* both specified, added security */
124 if ((strlen(macaddress) == 17) &&
125 (VALID_IP(ipaddress))) {
126 snprintf(command, STRING_SIZE-1, "/sbin/iptables -A WIRELESSINPUT -m mac --mac-source %s -s %s -i %s -j ACCEPT", macaddress, ipaddress, blue_dev);
127 safe_system(command);
128 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);
129 safe_system(command);
130 snprintf(command, STRING_SIZE-1, "/sbin/iptables -A WIRELESSFORWARD -m mac --mac-source %s -s %s -i %s -j DMZHOLES", macaddress, ipaddress, blue_dev);
131 safe_system(command);
132 } else {
133
134 /* correctly formed mac address is 17 chars */
135 if (strlen(macaddress) == 17) {
136 snprintf(command, STRING_SIZE-1, "/sbin/iptables -A WIRELESSINPUT -m mac --mac-source %s -i %s -j ACCEPT", macaddress, blue_dev);
137 safe_system(command);
138 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);
139 safe_system(command);
140 snprintf(command, STRING_SIZE-1, "/sbin/iptables -A WIRELESSFORWARD -m mac --mac-source %s -i %s -j DMZHOLES", macaddress, blue_dev);
141 safe_system(command);
142 }
143
144 if (VALID_IP(ipaddress)) {
145 snprintf(command, STRING_SIZE-1, "/sbin/iptables -A WIRELESSINPUT -s %s -i %s -j ACCEPT", ipaddress, blue_dev);
146 safe_system(command);
147 snprintf(command, STRING_SIZE-1, "/sbin/iptables -A WIRELESSFORWARD -s %s -i %s -o ! %s -j ACCEPT", ipaddress, blue_dev, green_dev);
148 safe_system(command);
149 snprintf(command, STRING_SIZE-1, "/sbin/iptables -A WIRELESSFORWARD -s %s -i %s -j DMZHOLES", ipaddress, blue_dev);
150 safe_system(command);
151 }
152 }
153 }
154 }
155
156 return 0;
157}