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