fix checksum recalc
[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.24 2005/10/11 09:04:53 bodea Exp $";
13
14 int plugin_api_version = PLUGIN_API_VERSION;
15 static struct pluginfuncs *f = 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=512000 >/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 #define F_UNGARDEN 0
44 #define F_GARDEN 1
45 #define F_CLEANUP 2
46
47 int garden_session(sessiont *s, int flag, char *newuser);
48
49 int plugin_post_auth(struct param_post_auth *data)
50 {
51 // Ignore if user authentication was successful
52 if (data->auth_allowed)
53 return PLUGIN_RET_OK;
54
55 f->log(3, f->get_id_by_session(data->s), data->s->tunnel,
56 "Walled Garden allowing login\n");
57
58 data->auth_allowed = 1;
59 data->s->walled_garden = 1;
60 return PLUGIN_RET_OK;
61 }
62
63 int plugin_new_session(struct param_new_session *data)
64 {
65 if (!iam_master)
66 return PLUGIN_RET_OK; // Slaves don't do walled garden processing.
67
68 if (data->s->walled_garden)
69 garden_session(data->s, F_GARDEN, 0);
70
71 return PLUGIN_RET_OK;
72 }
73
74 int plugin_kill_session(struct param_new_session *data)
75 {
76 if (!iam_master)
77 return PLUGIN_RET_OK; // Slaves don't do walled garden processing.
78
79 if (data->s->walled_garden)
80 garden_session(data->s, F_CLEANUP, 0);
81
82 return PLUGIN_RET_OK;
83 }
84
85 char *plugin_control_help[] = {
86 " garden USER|SID Put user into the walled garden",
87 " ungarden SID [USER] Release session from garden",
88 0
89 };
90
91 int plugin_control(struct param_control *data)
92 {
93 sessionidt session;
94 sessiont *s = 0;
95 int flag;
96 char *end;
97
98 if (data->argc < 1)
99 return PLUGIN_RET_OK;
100
101 if (strcmp(data->argv[0], "garden") && strcmp(data->argv[0], "ungarden"))
102 return PLUGIN_RET_OK; // not for us
103
104 if (!iam_master)
105 return PLUGIN_RET_NOTMASTER;
106
107 flag = data->argv[0][0] == 'g' ? F_GARDEN : F_UNGARDEN;
108
109 if (data->argc < 2 || data->argc > 3 || (data->argc > 2 && flag == F_GARDEN))
110 {
111 data->response = NSCTL_RES_ERR;
112 data->additional = flag == F_GARDEN
113 ? "requires username or session id"
114 : "requires session id and optional username";
115
116 return PLUGIN_RET_STOP;
117 }
118
119 if (!(session = strtol(data->argv[1], &end, 10)) || *end)
120 {
121 if (flag)
122 session = f->get_session_by_username(data->argv[1]);
123 else
124 session = 0; // can't ungarden by username
125 }
126
127 if (session)
128 s = f->get_session_by_id(session);
129
130 if (!s || !s->ip)
131 {
132 data->response = NSCTL_RES_ERR;
133 data->additional = "session not found";
134 return PLUGIN_RET_STOP;
135 }
136
137 if (s->walled_garden == flag)
138 {
139 data->response = NSCTL_RES_ERR;
140 data->additional = flag ? "already in walled garden" : "not in walled garden";
141 return PLUGIN_RET_STOP;
142 }
143
144 garden_session(s, flag, data->argc > 2 ? data->argv[2] : 0);
145 f->session_changed(session);
146
147 data->response = NSCTL_RES_OK;
148 data->additional = 0;
149
150 return PLUGIN_RET_STOP;
151 }
152
153 int plugin_become_master(void)
154 {
155 int i;
156 iam_master = 1; // We just became the master. Wow!
157
158 for (i = 0; up_commands[i] && *up_commands[i]; i++)
159 {
160 f->log(3, 0, 0, "Running %s\n", up_commands[i]);
161 system(up_commands[i]);
162 }
163
164 return PLUGIN_RET_OK;
165 }
166
167 // Called for each active session after becoming master
168 int plugin_new_session_master(sessiont *s)
169 {
170 if (s->walled_garden)
171 garden_session(s, F_GARDEN, 0);
172
173 return PLUGIN_RET_OK;
174 }
175
176 int garden_session(sessiont *s, int flag, char *newuser)
177 {
178 char cmd[2048];
179 sessionidt sess;
180
181 if (!s) return 0;
182 if (!s->opened) return 0;
183
184 sess = f->get_id_by_session(s);
185 if (flag == F_GARDEN)
186 {
187 f->log(2, sess, s->tunnel, "Garden user %s (%s)\n", s->user,
188 f->fmtaddr(htonl(s->ip), 0));
189
190 snprintf(cmd, sizeof(cmd),
191 "iptables -t nat -A garden_users -s %s -j garden",
192 f->fmtaddr(htonl(s->ip), 0));
193
194 f->log(3, sess, s->tunnel, "%s\n", cmd);
195 system(cmd);
196 s->walled_garden = 1;
197 }
198 else
199 {
200 sessionidt other;
201 int count = 40;
202
203 // Normal User
204 f->log(2, sess, s->tunnel, "Un-Garden user %s (%s)\n", s->user, f->fmtaddr(htonl(s->ip), 0));
205 if (newuser)
206 {
207 snprintf(s->user, MAXUSER, "%s", newuser);
208 f->log(2, sess, s->tunnel, " Setting username to %s\n", s->user);
209 }
210
211 // Kick off any duplicate usernames
212 // but make sure not to kick off ourself
213 if (s->ip && !s->die && (other = f->get_session_by_username(s->user)) &&
214 s != f->get_session_by_id(other))
215 {
216 f->sessionkill(other,
217 "Duplicate session when user released from walled garden");
218 }
219
220 /* Clean up counters */
221 s->pin = s->pout = 0;
222 s->cin = s->cout = 0;
223 s->cin_delta = s->cout_delta = 0;
224 s->cin_wrap = s->cout_wrap = 0;
225
226 snprintf(cmd, sizeof(cmd),
227 "iptables -t nat -D garden_users -s %s -j garden",
228 f->fmtaddr(htonl(s->ip), 0));
229
230 f->log(3, sess, s->tunnel, "%s\n", cmd);
231 while (--count)
232 {
233 int status = system(cmd);
234 if (WEXITSTATUS(status) != 0) break;
235 }
236
237 s->walled_garden = 0;
238
239 if (flag != F_CLEANUP)
240 {
241 /* OK, we're up! */
242 uint16_t r = f->radiusnew(f->get_id_by_session(s));
243 if (r) f->radiussend(r, RADIUSSTART);
244 }
245 }
246
247 return 1;
248 }
249
250 int plugin_init(struct pluginfuncs *funcs)
251 {
252 FILE *tables;
253 int found_nat = 0;
254
255 if (!funcs)
256 return 0;
257
258 f = funcs;
259
260 if ((tables = fopen("/proc/net/ip_tables_names", "r")))
261 {
262 char buf[1024];
263 while (fgets(buf, sizeof(buf), tables) && !found_nat)
264 found_nat = !strcmp(buf, "nat\n");
265
266 fclose(tables);
267 }
268
269 /* master killed/crashed? */
270 if (found_nat)
271 {
272 int i;
273 for (i = 0; down_commands[i] && *down_commands[i]; i++)
274 {
275 f->log(3, 0, 0, "Running %s\n", down_commands[i]);
276 system(down_commands[i]);
277 }
278 }
279
280 return 1;
281 }
282
283 void plugin_done()
284 {
285 int i;
286
287 if (!iam_master) // Never became master. nothing to do.
288 return;
289
290 for (i = 0; down_commands[i] && *down_commands[i]; i++)
291 {
292 f->log(3, 0, 0, "Running %s\n", down_commands[i]);
293 system(down_commands[i]);
294 }
295 }
296