oop
[l2tpns.git] / garden.c
1 #include <string.h>
2 #include <malloc.h>
3 #include <stdlib.h>
4 #include <sys/wait.h>
5 #include <sys/types.h>
6 #include "l2tpns.h"
7 #include "plugin.h"
8 #include "control.h"
9
10 /* walled garden */
11
12 char const *cvs_id = "$Id: garden.c,v 1.16 2004-11-18 06:41:03 bodea Exp $";
13
14 int plugin_api_version = PLUGIN_API_VERSION;
15 static struct pluginfuncs *p = 0;
16
17 static int iam_master = 0; // We're all slaves! Slaves I tell you!
18
19 char *up_commands[] = {
20 "iptables -t nat -N garden >/dev/null 2>&1", // Create a chain that all gardened users will go through
21 "iptables -t nat -F garden",
22 ". " PLUGINCONF "/build-garden", // Populate with site-specific DNAT rules
23 "iptables -t nat -N garden_users >/dev/null 2>&1", // Empty chain, users added/removed by garden_session
24 "iptables -t nat -F garden_users",
25 "iptables -t nat -A PREROUTING -j garden_users", // DNAT any users on the garden_users chain
26 "sysctl -w net.ipv4.ip_conntrack_max=256000 >/dev/null", // lots of entries
27 NULL,
28 };
29
30 char *down_commands[] = {
31 "iptables -t nat -F PREROUTING",
32 "iptables -t nat -F garden_users",
33 "iptables -t nat -X garden_users",
34 "iptables -t nat -F garden",
35 "iptables -t nat -X garden",
36 "rmmod iptable_nat", // Should also remove ip_conntrack, but
37 // doing so can take hours... literally.
38 // If a master is re-started as a slave,
39 // either rmmod manually, or reboot.
40 NULL,
41 };
42
43 int garden_session(sessiont *s, int flag);
44
45 int plugin_post_auth(struct param_post_auth *data)
46 {
47 // Ignore if user authentication was successful
48 if (data->auth_allowed) return PLUGIN_RET_OK;
49
50 p->log(3, 0, 0, 0, "Walled Garden allowing login\n");
51 data->auth_allowed = 1;
52 data->s->walled_garden = 1;
53 return PLUGIN_RET_OK;
54 }
55
56 int plugin_new_session(struct param_new_session *data)
57 {
58 if (!iam_master)
59 return PLUGIN_RET_OK; // Slaves don't do walled garden processing.
60
61 if (data->s->walled_garden)
62 garden_session(data->s, 1);
63
64 return PLUGIN_RET_OK;
65 }
66
67 int plugin_kill_session(struct param_new_session *data)
68 {
69 if (!iam_master)
70 return PLUGIN_RET_OK; // Slaves don't do walled garden processing.
71
72 if (data->s->walled_garden)
73 garden_session(data->s, 0);
74
75 return PLUGIN_RET_OK;
76 }
77
78 char *plugin_control_help[] = {
79 " garden USER|SID Put user into the walled garden",
80 " ungarden SID Release session from garden",
81 0
82 };
83
84 int plugin_control(struct param_control *data)
85 {
86 sessionidt session;
87 sessiont *s = 0;
88 int flag;
89 char *end;
90
91 if (data->argc < 1)
92 return PLUGIN_RET_OK;
93
94 if (strcmp(data->argv[0], "garden") && strcmp(data->argv[0], "ungarden"))
95 return PLUGIN_RET_OK; // not for us
96
97 if (!iam_master)
98 return PLUGIN_RET_NOTMASTER;
99
100 flag = data->argv[0][0] != 'u';
101
102 if (data->argc != 2)
103 {
104 data->response = NSCTL_RES_ERR;
105 data->additional = "one argument required: username or session id";
106 return PLUGIN_RET_STOP;
107 }
108
109 if (!(session = strtol(data->argv[1], &end, 10)) || *end)
110 {
111 if (flag)
112 session = p->get_session_by_username(data->argv[1]);
113 else
114 session = 0; // can't ungarden by username
115 }
116
117 if (session)
118 s = p->get_session_by_id(session);
119
120 if (!s || !s->ip)
121 {
122 data->response = NSCTL_RES_ERR;
123 data->additional = "session not found";
124 return PLUGIN_RET_STOP;
125 }
126
127 if (s->walled_garden == flag)
128 {
129 data->response = NSCTL_RES_ERR;
130 data->additional = flag ? "already in walled garden" : "not in walled garden";
131 return PLUGIN_RET_STOP;
132 }
133
134 garden_session(s, flag);
135 p->session_changed(session);
136
137 data->response = NSCTL_RES_OK;
138 data->additional = 0;
139
140 return PLUGIN_RET_STOP;
141 }
142
143 int plugin_become_master(void)
144 {
145 int i;
146 iam_master = 1; // We just became the master. Wow!
147
148 for (i = 0; up_commands[i] && *up_commands[i]; i++)
149 {
150 p->log(3, 0, 0, 0, "Running %s\n", up_commands[i]);
151 system(up_commands[i]);
152 }
153
154 return PLUGIN_RET_OK;
155 }
156
157 // Called for each active session after becoming master
158 int plugin_new_session_master(sessiont *s)
159 {
160 if (s->walled_garden)
161 {
162 s->walled_garden = 0;
163 garden_session(s, 1);
164 }
165
166 return PLUGIN_RET_OK;
167 }
168
169 int garden_session(sessiont *s, int flag)
170 {
171 char cmd[2048];
172
173 if (!s) return 0;
174 if (!s->opened) return 0;
175
176 if (flag == 1)
177 {
178 p->log(2, 0, 0, s->tunnel, "Garden user %s (%s)\n", s->user, p->inet_toa(htonl(s->ip)));
179 snprintf(cmd, sizeof(cmd), "iptables -t nat -A garden_users -s %s -j garden", p->inet_toa(htonl(s->ip)));
180 p->log(3, 0, 0, s->tunnel, "%s\n", cmd);
181 system(cmd);
182 s->walled_garden = 1;
183 }
184 else
185 {
186 sessionidt other;
187 int count = 40;
188
189 // Normal User
190 p->log(2, 0, 0, s->tunnel, "Un-Garden user %s (%s)\n", s->user, p->inet_toa(htonl(s->ip)));
191 // Kick off any duplicate usernames
192 // but make sure not to kick off ourself
193 if (s->ip && !s->die && (other = p->get_session_by_username(s->user)) && s != p->get_session_by_id(other)) {
194 p->sessionkill(other, "Duplicate session when user released from walled garden");
195 }
196 /* Clean up counters */
197 s->cin = s->cout = 0;
198 s->pin = s->pout = 0;
199
200 snprintf(cmd, sizeof(cmd), "iptables -t nat -D garden_users -s %s -j garden", p->inet_toa(htonl(s->ip)));
201 p->log(3, 0, 0, s->tunnel, "%s\n", cmd);
202 while (--count)
203 {
204 int status = system(cmd);
205 if (WEXITSTATUS(status) != 0) break;
206 }
207
208 s->walled_garden = 0;
209
210 if (!s->die) {
211 /* OK, we're up! */
212 u16 r = p->radiusnew(p->get_id_by_session(s));
213 p->radiussend(r, RADIUSSTART);
214 }
215 }
216 s->walled_garden = flag;
217 return 1;
218 }
219
220 int plugin_init(struct pluginfuncs *funcs)
221 {
222 FILE *tables;
223 int found_nat = 0;
224
225 if (!funcs)
226 return 0;
227
228 p = funcs;
229
230 if ((tables = fopen("/proc/net/ip_tables_names", "r")))
231 {
232 char buf[1024];
233 while (fgets(buf, sizeof(buf), tables) && !found_nat)
234 found_nat = !strcmp(buf, "nat\n");
235
236 fclose(tables);
237 }
238
239 /* master killed/crashed? */
240 if (found_nat)
241 {
242 int i;
243 for (i = 0; down_commands[i] && *down_commands[i]; i++)
244 {
245 p->log(3, 0, 0, 0, "Running %s\n", down_commands[i]);
246 system(down_commands[i]);
247 }
248 }
249
250 return 1;
251 }
252
253 void plugin_done()
254 {
255 int i;
256
257 if (!iam_master) // Never became master. nothing to do.
258 return;
259
260 for (i = 0; down_commands[i] && *down_commands[i]; i++)
261 {
262 p->log(3, 0, 0, 0, "Running %s\n", down_commands[i]);
263 system(down_commands[i]);
264 }
265 }
266