ba228cd391ca800988c84ca7a5eac1ccc1d345dd
[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.13 2004-11-17 08:23:34 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 USER|SID Release user",
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 || (strcmp(data->argv[0], "garden") && strcmp(data->argv[0], "ungarden")))
92 return PLUGIN_RET_OK; // not for us
93
94 flag = data->argv[0][0] == 'g';
95
96 if (!iam_master) // All garden processing happens on the master.
97 {
98 data->response = NSCTL_RES_ERR;
99 data->additional = "must be run on the cluster master";
100 return PLUGIN_RET_STOP;
101 }
102
103 if (data->argc != 2)
104 {
105 data->response = NSCTL_RES_ERR;
106 data->additional = "one argument required: username or session id";
107 return PLUGIN_RET_STOP;
108 }
109
110 if (!(session = strtol(data->argv[0], &end, 10)) || *end)
111 session = p->get_session_by_username(data->argv[0]);
112
113 if (session)
114 s = p->get_session_by_id(session);
115
116 if (!s || !s->ip)
117 {
118 data->response = NSCTL_RES_ERR;
119 data->additional = "session not found";
120 return PLUGIN_RET_STOP;
121 }
122
123 if (s->walled_garden == flag)
124 {
125 data->response = NSCTL_RES_ERR;
126 data->additional = flag ? "already in walled garden" : "not in walled garden";
127 return PLUGIN_RET_STOP;
128 }
129
130 garden_session(s, flag);
131 data->response = NSCTL_RES_OK;
132 data->additional = 0;
133
134 return PLUGIN_RET_STOP;
135 }
136
137 int plugin_become_master(void)
138 {
139 int i;
140 iam_master = 1; // We just became the master. Wow!
141
142 for (i = 0; up_commands[i] && *up_commands[i]; i++)
143 {
144 p->log(3, 0, 0, 0, "Running %s\n", up_commands[i]);
145 system(up_commands[i]);
146 }
147
148 return PLUGIN_RET_OK;
149 }
150
151 // Called for each active session after becoming master
152 int plugin_new_session_master(sessiont * s)
153 {
154 if (s->walled_garden)
155 {
156 s->walled_garden = 0;
157 garden_session(s, 1);
158 }
159
160 return PLUGIN_RET_OK;
161 }
162
163 int garden_session(sessiont *s, int flag)
164 {
165 char cmd[2048];
166
167 if (!s) return 0;
168 if (!s->opened) return 0;
169
170 if (flag == 1)
171 {
172 p->log(2, 0, 0, s->tunnel, "Garden user %s (%s)\n", s->user, p->inet_toa(htonl(s->ip)));
173 snprintf(cmd, sizeof(cmd), "iptables -t nat -A garden_users -s %s -j garden", p->inet_toa(htonl(s->ip)));
174 p->log(3, 0, 0, s->tunnel, "%s\n", cmd);
175 system(cmd);
176 s->walled_garden = 1;
177 }
178 else
179 {
180 sessionidt other;
181 int count = 40;
182
183 // Normal User
184 p->log(2, 0, 0, s->tunnel, "Un-Garden user %s (%s)\n", s->user, p->inet_toa(htonl(s->ip)));
185 // Kick off any duplicate usernames
186 // but make sure not to kick off ourself
187 if (s->ip && !s->die && (other = p->get_session_by_username(s->user)) && s != p->get_session_by_id(other)) {
188 p->sessionkill(other, "Duplicate session when user released from walled garden");
189 }
190 /* Clean up counters */
191 s->cin = s->cout = 0;
192 s->pin = s->pout = 0;
193
194 snprintf(cmd, sizeof(cmd), "iptables -t nat -D garden_users -s %s -j garden", p->inet_toa(htonl(s->ip)));
195 p->log(3, 0, 0, s->tunnel, "%s\n", cmd);
196 while (--count)
197 {
198 int status = system(cmd);
199 if (WEXITSTATUS(status) != 0) break;
200 }
201
202 s->walled_garden = 0;
203
204 if (!s->die) {
205 /* OK, we're up! */
206 u16 r = p->radiusnew(p->get_id_by_session(s));
207 p->radiussend(r, RADIUSSTART);
208 }
209 }
210 s->walled_garden = flag;
211 return 1;
212 }
213
214 int plugin_init(struct pluginfuncs *funcs)
215 {
216 FILE *tables;
217 int found_nat = 0;
218
219 if (!funcs)
220 return 0;
221
222 p = funcs;
223
224 if ((tables = fopen("/proc/net/ip_tables_names", "r")))
225 {
226 char buf[1024];
227 while (fgets(buf, sizeof(buf), tables) && !found_nat)
228 found_nat = !strcmp(buf, "nat\n");
229
230 fclose(tables);
231 }
232
233 /* master killed/crashed? */
234 if (found_nat)
235 {
236 int i;
237 for (i = 0; down_commands[i] && *down_commands[i]; i++)
238 {
239 p->log(3, 0, 0, 0, "Running %s\n", down_commands[i]);
240 system(down_commands[i]);
241 }
242 }
243
244 return 1;
245 }
246
247 void plugin_done()
248 {
249 int i;
250
251 if (!iam_master) // Never became master. nothing to do.
252 return;
253
254 for (i = 0; down_commands[i] && *down_commands[i]; i++)
255 {
256 p->log(3, 0, 0, 0, "Running %s\n", down_commands[i]);
257 system(down_commands[i]);
258 }
259 }
260