*** empty log message ***
[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.11 2004/11/05 04:55:27 bodea Exp $";
13
14 int __plugin_api_version = 1;
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 int plugin_control(struct param_control *data)
79 {
80 sessiont *s;
81 sessionidt session;
82
83 if (!iam_master) // All garden processing happens on the master.
84 return PLUGIN_RET_OK;
85
86 if (data->type != PKT_GARDEN && data->type != PKT_UNGARDEN)
87 return PLUGIN_RET_OK;
88
89 if (!data->data && data->data_length)
90 return PLUGIN_RET_OK;
91
92 session = atoi((char*)(data->data));
93 if (!session)
94 return PLUGIN_RET_OK;
95
96 data->send_response = 1;
97 s = p->get_session_by_id(session);
98 if (!s || !s->ip)
99 {
100 char *errormsg = "Session not connected";
101 *(short *)(data->response + 2) = ntohs(PKT_RESP_ERROR);
102 sprintf((data->response + data->response_length), "%s", errormsg);
103 data->response_length += strlen(errormsg) + 1;
104
105 p->log(3, 0, 0, 0, "Unknown session %d\n", session);
106 return PLUGIN_RET_STOP;
107 }
108 *(short *)(data->response + 2) = ntohs(PKT_RESP_OK);
109
110 if (!(garden_session(s, (data->type == PKT_GARDEN))))
111 {
112 char *errormsg = "User not connected";
113 *(short *)(data->response + 2) = ntohs(PKT_RESP_ERROR);
114 sprintf((data->response + data->response_length), "%s", errormsg);
115 data->response_length += strlen(errormsg) + 1;
116 }
117
118 return PLUGIN_RET_STOP;
119 }
120
121 int plugin_become_master(void)
122 {
123 int i;
124 iam_master = 1; // We just became the master. Wow!
125
126 for (i = 0; up_commands[i] && *up_commands[i]; i++)
127 {
128 p->log(3, 0, 0, 0, "Running %s\n", up_commands[i]);
129 system(up_commands[i]);
130 }
131
132 return PLUGIN_RET_OK;
133 }
134
135 // Called for each active session after becoming master
136 int plugin_new_session_master(sessiont * s)
137 {
138 if (s->walled_garden)
139 garden_session(s, 1);
140
141 return PLUGIN_RET_OK;
142 }
143
144 int garden_session(sessiont *s, int flag)
145 {
146 char cmd[2048];
147
148 if (!s) return 0;
149 if (!s->opened) return 0;
150
151 if (flag == 1)
152 {
153 p->log(2, 0, 0, s->tunnel, "Garden user %s (%s)\n", s->user, p->inet_toa(htonl(s->ip)));
154 snprintf(cmd, sizeof(cmd), "iptables -t nat -A garden_users -s %s -j garden", p->inet_toa(htonl(s->ip)));
155 p->log(3, 0, 0, s->tunnel, "%s\n", cmd);
156 system(cmd);
157 s->walled_garden = 1;
158 }
159 else
160 {
161 sessionidt other;
162 int count = 40;
163
164 // Normal User
165 p->log(2, 0, 0, s->tunnel, "Un-Garden user %s (%s)\n", s->user, p->inet_toa(htonl(s->ip)));
166 // Kick off any duplicate usernames
167 // but make sure not to kick off ourself
168 if (s->ip && !s->die && (other = p->get_session_by_username(s->user)) && s != p->get_session_by_id(other)) {
169 p->sessionkill(other, "Duplicate session when user released from walled garden");
170 }
171 /* Clean up counters */
172 s->cin = s->cout = 0;
173 s->pin = s->pout = 0;
174
175 snprintf(cmd, sizeof(cmd), "iptables -t nat -D garden_users -s %s -j garden", p->inet_toa(htonl(s->ip)));
176 p->log(3, 0, 0, s->tunnel, "%s\n", cmd);
177 while (--count)
178 {
179 int status = system(cmd);
180 if (WEXITSTATUS(status) != 0) break;
181 }
182
183 s->walled_garden = 0;
184
185 if (!s->die) {
186 /* OK, we're up! */
187 u16 r = p->radiusnew(p->get_id_by_session(s));
188 p->radiussend(r, RADIUSSTART);
189 }
190 }
191 s->walled_garden = flag;
192 return 1;
193 }
194
195 int plugin_init(struct pluginfuncs *funcs)
196 {
197 FILE *tables;
198 int found_nat = 0;
199
200 if (!funcs)
201 return 0;
202
203 p = funcs;
204
205 if ((tables = fopen("/proc/net/ip_tables_names", "r")))
206 {
207 char buf[1024];
208 while (fgets(buf, sizeof(buf), tables) && !found_nat)
209 found_nat = !strcmp(buf, "nat\n");
210
211 fclose(tables);
212 }
213
214 /* master killed/crashed? */
215 if (found_nat)
216 {
217 int i;
218 for (i = 0; down_commands[i] && *down_commands[i]; i++)
219 {
220 p->log(3, 0, 0, 0, "Running %s\n", down_commands[i]);
221 system(down_commands[i]);
222 }
223 }
224
225 return 1;
226 }
227
228 void plugin_done()
229 {
230 int i;
231
232 if (!iam_master) // Never became master. nothing to do.
233 return;
234
235 for (i = 0; down_commands[i] && *down_commands[i]; i++)
236 {
237 p->log(3, 0, 0, 0, "Running %s\n", down_commands[i]);
238 system(down_commands[i]);
239 }
240 }
241