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