6 #include <netinet/ip6.h>
14 int plugin_api_version
= PLUGIN_API_VERSION
;
15 static struct pluginfuncs
*f
= 0;
17 static int iam_master
= 0; // We're all slaves! Slaves I tell you!
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
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.
48 int garden_session(sessiont
*s
, int flag
, char *newuser
);
50 int plugin_post_auth(struct param_post_auth
*data
)
52 // Ignore if user authentication was successful
53 if (data
->auth_allowed
)
56 f
->log(3, f
->get_id_by_session(data
->s
), data
->s
->tunnel
,
57 "Walled Garden allowing login\n");
59 data
->auth_allowed
= 1;
60 data
->s
->walled_garden
= 1;
64 int plugin_new_session(struct param_new_session
*data
)
67 return PLUGIN_RET_OK
; // Slaves don't do walled garden processing.
69 if (data
->s
->walled_garden
)
70 garden_session(data
->s
, F_GARDEN
, 0);
75 int plugin_kill_session(struct param_new_session
*data
)
78 return PLUGIN_RET_OK
; // Slaves don't do walled garden processing.
80 if (data
->s
->walled_garden
)
81 garden_session(data
->s
, F_CLEANUP
, 0);
86 char *plugin_control_help
[] = {
87 " garden USER|SID Put user into the walled garden",
88 " ungarden SID [USER] Release session from garden",
92 int plugin_control(struct param_control
*data
)
100 return PLUGIN_RET_OK
;
102 if (strcmp(data
->argv
[0], "garden") && strcmp(data
->argv
[0], "ungarden"))
103 return PLUGIN_RET_OK
; // not for us
106 return PLUGIN_RET_NOTMASTER
;
108 flag
= data
->argv
[0][0] == 'g' ? F_GARDEN
: F_UNGARDEN
;
110 if (data
->argc
< 2 || data
->argc
> 3 || (data
->argc
> 2 && flag
== F_GARDEN
))
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";
117 return PLUGIN_RET_STOP
;
120 if (!(session
= strtol(data
->argv
[1], &end
, 10)) || *end
)
123 session
= f
->get_session_by_username(data
->argv
[1]);
125 session
= 0; // can't ungarden by username
129 s
= f
->get_session_by_id(session
);
133 data
->response
= NSCTL_RES_ERR
;
134 data
->additional
= "session not found";
135 return PLUGIN_RET_STOP
;
138 if (s
->walled_garden
== flag
)
140 data
->response
= NSCTL_RES_ERR
;
141 data
->additional
= flag
? "already in walled garden" : "not in walled garden";
142 return PLUGIN_RET_STOP
;
145 garden_session(s
, flag
, data
->argc
> 2 ? data
->argv
[2] : 0);
146 f
->session_changed(session
);
148 data
->response
= NSCTL_RES_OK
;
149 data
->additional
= 0;
151 return PLUGIN_RET_STOP
;
154 int plugin_become_master(void)
157 iam_master
= 1; // We just became the master. Wow!
159 for (i
= 0; up_commands
[i
] && *up_commands
[i
]; i
++)
161 f
->log(3, 0, 0, "Running %s\n", up_commands
[i
]);
162 if (-1 == system(up_commands
[i
])) f
->log(0, 0, 0, "error command %s\n", up_commands
[i
]);
165 return PLUGIN_RET_OK
;
168 // Called for each active session after becoming master
169 int plugin_new_session_master(sessiont
*s
)
171 if (s
->walled_garden
)
172 garden_session(s
, F_GARDEN
, 0);
174 return PLUGIN_RET_OK
;
177 int garden_session(sessiont
*s
, int flag
, char *newuser
)
184 if (!s
->opened
) return 0;
186 sess
= f
->get_id_by_session(s
);
187 if (flag
== F_GARDEN
)
189 f
->log(2, sess
, s
->tunnel
, "Garden user %s (%s)\n", s
->user
,
190 f
->fmtaddr(htonl(s
->ip
), 0));
192 snprintf(cmd
, sizeof(cmd
),
193 "iptables -t nat -A garden_users -s %s -j garden",
194 f
->fmtaddr(htonl(s
->ip
), 0));
196 f
->log(3, sess
, s
->tunnel
, "%s\n", cmd
);
197 status
= system(cmd
);
198 s
->walled_garden
= 1;
206 f
->log(2, sess
, s
->tunnel
, "Un-Garden user %s (%s)\n", s
->user
, f
->fmtaddr(htonl(s
->ip
), 0));
209 snprintf(s
->user
, MAXUSER
, "%s", newuser
);
210 f
->log(2, sess
, s
->tunnel
, " Setting username to %s\n", s
->user
);
213 // Kick off any duplicate usernames
214 // but make sure not to kick off ourself
215 if (s
->ip
&& !s
->die
&& (other
= f
->get_session_by_username(s
->user
)) &&
216 s
!= f
->get_session_by_id(other
))
218 f
->sessionkill(other
,
219 "Duplicate session when user released from walled garden");
222 /* Clean up counters */
223 s
->pin
= s
->pout
= 0;
224 s
->cin
= s
->cout
= 0;
225 s
->cin_delta
= s
->cout_delta
= 0;
226 s
->cin_wrap
= s
->cout_wrap
= 0;
228 snprintf(cmd
, sizeof(cmd
),
229 "iptables -t nat -D garden_users -s %s -j garden",
230 f
->fmtaddr(htonl(s
->ip
), 0));
232 f
->log(3, sess
, s
->tunnel
, "%s\n", cmd
);
235 status
= system(cmd
);
236 if (WEXITSTATUS(status
) != 0) break;
239 s
->walled_garden
= 0;
241 if (flag
!= F_CLEANUP
)
244 uint16_t r
= f
->radiusnew(f
->get_id_by_session(s
));
245 if (r
) f
->radiussend(r
, RADIUSSTART
);
252 int plugin_init(struct pluginfuncs
*funcs
)
262 if ((tables
= fopen("/proc/net/ip_tables_names", "r")))
265 while (fgets(buf
, sizeof(buf
), tables
) && !found_nat
)
266 found_nat
= !strcmp(buf
, "nat\n");
271 /* master killed/crashed? */
275 for (i
= 0; down_commands
[i
] && *down_commands
[i
]; i
++)
277 f
->log(3, 0, 0, "Running %s\n", down_commands
[i
]);
278 if (-1 == system(down_commands
[i
])) f
->log(0, 0, 0, "error command %s\n", down_commands
[i
]);
289 if (!iam_master
) // Never became master. nothing to do.
292 for (i
= 0; down_commands
[i
] && *down_commands
[i
]; i
++)
294 f
->log(3, 0, 0, "Running %s\n", down_commands
[i
]);
295 if (-1 == system(down_commands
[i
])) f
->log(0, 0, 0, "error command %s\n", down_commands
[i
]);