ca41d144e55d4b1ed5c8ebffc2f79b50be834664
12 char const *cvs_id
= "$Id: garden.c,v 1.14 2004-11-17 15:08:19 bodea Exp $";
14 int plugin_api_version
= PLUGIN_API_VERSION
;
15 static struct pluginfuncs
*p
= 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.ip_conntrack_max=256000 >/dev/null", // lots of entries
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.
43 int garden_session(sessiont
*s
, int flag
);
45 int plugin_post_auth(struct param_post_auth
*data
)
47 // Ignore if user authentication was successful
48 if (data
->auth_allowed
) return PLUGIN_RET_OK
;
50 p
->log(3, 0, 0, 0, "Walled Garden allowing login\n");
51 data
->auth_allowed
= 1;
52 data
->s
->walled_garden
= 1;
56 int plugin_new_session(struct param_new_session
*data
)
59 return PLUGIN_RET_OK
; // Slaves don't do walled garden processing.
61 if (data
->s
->walled_garden
)
62 garden_session(data
->s
, 1);
67 int plugin_kill_session(struct param_new_session
*data
)
70 return PLUGIN_RET_OK
; // Slaves don't do walled garden processing.
72 if (data
->s
->walled_garden
)
73 garden_session(data
->s
, 0);
78 char *plugin_control_help
[] = {
79 " garden USER|SID Put user into the walled garden",
80 " ungarden USER|SID Release user from garden",
84 int plugin_control(struct param_control
*data
)
94 if (strcmp(data
->argv
[0], "garden") && strcmp(data
->argv
[0], "ungarden"))
95 return PLUGIN_RET_OK
; // not for us
97 flag
= data
->argv
[0][0] != 'u';
99 if (!iam_master
) // All garden processing happens on the master.
101 data
->response
= NSCTL_RES_ERR
;
102 data
->additional
= "must be run on the cluster master";
103 return PLUGIN_RET_STOP
;
108 data
->response
= NSCTL_RES_ERR
;
109 data
->additional
= "one argument required: username or session id";
110 return PLUGIN_RET_STOP
;
113 if (!(session
= strtol(data
->argv
[1], &end
, 10)) || *end
)
114 session
= p
->get_session_by_username(data
->argv
[1]);
117 s
= p
->get_session_by_id(session
);
121 data
->response
= NSCTL_RES_ERR
;
122 data
->additional
= "session not found";
123 return PLUGIN_RET_STOP
;
126 if (s
->walled_garden
== flag
)
128 data
->response
= NSCTL_RES_ERR
;
129 data
->additional
= flag
? "already in walled garden" : "not in walled garden";
130 return PLUGIN_RET_STOP
;
133 garden_session(s
, flag
);
134 p
->sesssion_changed(session
);
136 data
->response
= NSCTL_RES_OK
;
137 data
->additional
= 0;
139 return PLUGIN_RET_STOP
;
142 int plugin_become_master(void)
145 iam_master
= 1; // We just became the master. Wow!
147 for (i
= 0; up_commands
[i
] && *up_commands
[i
]; i
++)
149 p
->log(3, 0, 0, 0, "Running %s\n", up_commands
[i
]);
150 system(up_commands
[i
]);
153 return PLUGIN_RET_OK
;
156 // Called for each active session after becoming master
157 int plugin_new_session_master(sessiont
*s
)
159 if (s
->walled_garden
)
161 s
->walled_garden
= 0;
162 garden_session(s
, 1);
165 return PLUGIN_RET_OK
;
168 int garden_session(sessiont
*s
, int flag
)
173 if (!s
->opened
) return 0;
177 p
->log(2, 0, 0, s
->tunnel
, "Garden user %s (%s)\n", s
->user
, p
->inet_toa(htonl(s
->ip
)));
178 snprintf(cmd
, sizeof(cmd
), "iptables -t nat -A garden_users -s %s -j garden", p
->inet_toa(htonl(s
->ip
)));
179 p
->log(3, 0, 0, s
->tunnel
, "%s\n", cmd
);
181 s
->walled_garden
= 1;
189 p
->log(2, 0, 0, s
->tunnel
, "Un-Garden user %s (%s)\n", s
->user
, p
->inet_toa(htonl(s
->ip
)));
190 // Kick off any duplicate usernames
191 // but make sure not to kick off ourself
192 if (s
->ip
&& !s
->die
&& (other
= p
->get_session_by_username(s
->user
)) && s
!= p
->get_session_by_id(other
)) {
193 p
->sessionkill(other
, "Duplicate session when user released from walled garden");
195 /* Clean up counters */
196 s
->cin
= s
->cout
= 0;
197 s
->pin
= s
->pout
= 0;
199 snprintf(cmd
, sizeof(cmd
), "iptables -t nat -D garden_users -s %s -j garden", p
->inet_toa(htonl(s
->ip
)));
200 p
->log(3, 0, 0, s
->tunnel
, "%s\n", cmd
);
203 int status
= system(cmd
);
204 if (WEXITSTATUS(status
) != 0) break;
207 s
->walled_garden
= 0;
211 u16 r
= p
->radiusnew(p
->get_id_by_session(s
));
212 p
->radiussend(r
, RADIUSSTART
);
215 s
->walled_garden
= flag
;
219 int plugin_init(struct pluginfuncs
*funcs
)
229 if ((tables
= fopen("/proc/net/ip_tables_names", "r")))
232 while (fgets(buf
, sizeof(buf
), tables
) && !found_nat
)
233 found_nat
= !strcmp(buf
, "nat\n");
238 /* master killed/crashed? */
242 for (i
= 0; down_commands
[i
] && *down_commands
[i
]; i
++)
244 p
->log(3, 0, 0, 0, "Running %s\n", down_commands
[i
]);
245 system(down_commands
[i
]);
256 if (!iam_master
) // Never became master. nothing to do.
259 for (i
= 0; down_commands
[i
] && *down_commands
[i
]; i
++)
261 p
->log(3, 0, 0, 0, "Running %s\n", down_commands
[i
]);
262 system(down_commands
[i
]);