6 #include <linux/rtnetlink.h>
7 #include <netinet/ip6.h>
16 int plugin_api_version
= PLUGIN_API_VERSION
;
17 static struct pluginfuncs
*f
= 0;
19 static int iam_master
= 0; // We're all slaves! Slaves I tell you!
21 char *up_commands
[] = {
22 "iptables -t nat -N garden >/dev/null 2>&1", // Create a chain that all gardened users will go through
23 "iptables -t nat -F garden",
24 ". " PLUGINCONF
"/build-garden", // Populate with site-specific DNAT rules
25 "iptables -t nat -N garden_users >/dev/null 2>&1", // Empty chain, users added/removed by garden_session
26 "iptables -t nat -F garden_users",
27 "iptables -t nat -A PREROUTING -j garden_users", // DNAT any users on the garden_users chain
28 "sysctl -w net.ipv4.netfilter.ip_conntrack_max=512000" // lots of entries
29 " net.ipv4.netfilter.ip_conntrack_tcp_timeout_established=18000 >/dev/null", // 5hrs
33 char *down_commands
[] = {
34 "iptables -t nat -F PREROUTING",
35 "iptables -t nat -F garden_users",
36 "iptables -t nat -X garden_users",
37 "iptables -t nat -F garden",
38 "iptables -t nat -X garden",
39 "rmmod iptable_nat", // Should also remove ip_conntrack, but
40 // doing so can take hours... literally.
41 // If a master is re-started as a slave,
42 // either rmmod manually, or reboot.
50 int garden_session(sessiont
*s
, int flag
, char *newuser
);
52 int plugin_post_auth(struct param_post_auth
*data
)
54 // Ignore if user authentication was successful
55 if (data
->auth_allowed
)
58 f
->log(3, f
->get_id_by_session(data
->s
), data
->s
->tunnel
,
59 "Walled Garden allowing login\n");
61 data
->auth_allowed
= 1;
62 data
->s
->walled_garden
= 1;
66 int plugin_new_session(struct param_new_session
*data
)
69 return PLUGIN_RET_OK
; // Slaves don't do walled garden processing.
71 if (data
->s
->walled_garden
)
72 garden_session(data
->s
, F_GARDEN
, 0);
77 int plugin_kill_session(struct param_new_session
*data
)
80 return PLUGIN_RET_OK
; // Slaves don't do walled garden processing.
82 if (data
->s
->walled_garden
)
83 garden_session(data
->s
, F_CLEANUP
, 0);
88 char *plugin_control_help
[] = {
89 " garden USER|SID Put user into the walled garden",
90 " ungarden SID [USER] Release session from garden",
94 int plugin_control(struct param_control
*data
)
102 return PLUGIN_RET_OK
;
104 if (strcmp(data
->argv
[0], "garden") && strcmp(data
->argv
[0], "ungarden"))
105 return PLUGIN_RET_OK
; // not for us
108 return PLUGIN_RET_NOTMASTER
;
110 flag
= data
->argv
[0][0] == 'g' ? F_GARDEN
: F_UNGARDEN
;
112 if (data
->argc
< 2 || data
->argc
> 3 || (data
->argc
> 2 && flag
== F_GARDEN
))
114 data
->response
= NSCTL_RES_ERR
;
115 data
->additional
= flag
== F_GARDEN
116 ? "requires username or session id"
117 : "requires session id and optional username";
119 return PLUGIN_RET_STOP
;
122 if (!(session
= strtol(data
->argv
[1], &end
, 10)) || *end
)
125 session
= f
->get_session_by_username(data
->argv
[1]);
127 session
= 0; // can't ungarden by username
131 s
= f
->get_session_by_id(session
);
135 data
->response
= NSCTL_RES_ERR
;
136 data
->additional
= "session not found";
137 return PLUGIN_RET_STOP
;
140 if (s
->walled_garden
== flag
)
142 data
->response
= NSCTL_RES_ERR
;
143 data
->additional
= flag
? "already in walled garden" : "not in walled garden";
144 return PLUGIN_RET_STOP
;
147 garden_session(s
, flag
, data
->argc
> 2 ? data
->argv
[2] : 0);
148 f
->session_changed(session
);
150 data
->response
= NSCTL_RES_OK
;
151 data
->additional
= 0;
153 return PLUGIN_RET_STOP
;
156 int plugin_become_master(void)
159 iam_master
= 1; // We just became the master. Wow!
161 for (i
= 0; up_commands
[i
] && *up_commands
[i
]; i
++)
163 f
->log(3, 0, 0, "Running %s\n", up_commands
[i
]);
164 if (-1 == system(up_commands
[i
])) f
->log(0, 0, 0, "error command %s\n", up_commands
[i
]);
167 return PLUGIN_RET_OK
;
170 // Called for each active session after becoming master
171 int plugin_new_session_master(sessiont
*s
)
173 if (s
->walled_garden
)
174 garden_session(s
, F_GARDEN
, 0);
176 return PLUGIN_RET_OK
;
179 int garden_session(sessiont
*s
, int flag
, char *newuser
)
186 if (!s
->opened
) return 0;
188 sess
= f
->get_id_by_session(s
);
189 if (flag
== F_GARDEN
)
191 f
->log(2, sess
, s
->tunnel
, "Garden user %s (%s)\n", s
->user
,
192 f
->fmtaddr(htonl(s
->ip
), 0));
194 snprintf(cmd
, sizeof(cmd
),
195 "iptables -t nat -A garden_users -s %s -j garden",
196 f
->fmtaddr(htonl(s
->ip
), 0));
198 f
->log(3, sess
, s
->tunnel
, "%s\n", cmd
);
199 status
= system(cmd
);
200 s
->walled_garden
= 1;
208 f
->log(2, sess
, s
->tunnel
, "Un-Garden user %s (%s)\n", s
->user
, f
->fmtaddr(htonl(s
->ip
), 0));
211 snprintf(s
->user
, MAXUSER
, "%s", newuser
);
212 f
->log(2, sess
, s
->tunnel
, " Setting username to %s\n", s
->user
);
215 // Kick off any duplicate usernames
216 // but make sure not to kick off ourself
217 if (s
->ip
&& !s
->die
&& (other
= f
->get_session_by_username(s
->user
)) &&
218 s
!= f
->get_session_by_id(other
))
220 f
->sessionkill(other
,
221 "Duplicate session when user released from walled garden");
224 /* Clean up counters */
225 s
->pin
= s
->pout
= 0;
226 s
->cin
= s
->cout
= 0;
227 s
->cin_delta
= s
->cout_delta
= 0;
228 s
->cin_wrap
= s
->cout_wrap
= 0;
230 snprintf(cmd
, sizeof(cmd
),
231 "iptables -t nat -D garden_users -s %s -j garden",
232 f
->fmtaddr(htonl(s
->ip
), 0));
234 f
->log(3, sess
, s
->tunnel
, "%s\n", cmd
);
237 status
= system(cmd
);
238 if (WEXITSTATUS(status
) != 0) break;
241 s
->walled_garden
= 0;
243 if (flag
!= F_CLEANUP
)
246 uint16_t r
= f
->radiusnew(f
->get_id_by_session(s
));
247 if (r
) f
->radiussend(r
, RADIUSSTART
);
254 int plugin_init(struct pluginfuncs
*funcs
)
264 if ((tables
= fopen("/proc/net/ip_tables_names", "r")))
267 while (fgets(buf
, sizeof(buf
), tables
) && !found_nat
)
268 found_nat
= !strcmp(buf
, "nat\n");
273 /* master killed/crashed? */
277 for (i
= 0; down_commands
[i
] && *down_commands
[i
]; i
++)
279 f
->log(3, 0, 0, "Running %s\n", down_commands
[i
]);
280 if (-1 == system(down_commands
[i
])) f
->log(0, 0, 0, "error command %s\n", down_commands
[i
]);
291 if (!iam_master
) // Never became master. nothing to do.
294 for (i
= 0; down_commands
[i
] && *down_commands
[i
]; i
++)
296 f
->log(3, 0, 0, "Running %s\n", down_commands
[i
]);
297 if (-1 == system(down_commands
[i
])) f
->log(0, 0, 0, "error command %s\n", down_commands
[i
]);