12 char const *cvs_id
= "$Id: garden.c,v 1.19 2004/12/01 02:52:46 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=512000 >/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.
47 int garden_session(sessiont
*s
, int flag
);
49 int plugin_post_auth(struct param_post_auth
*data
)
51 // Ignore if user authentication was successful
52 if (data
->auth_allowed
) return PLUGIN_RET_OK
;
54 p
->log(3, p
->get_id_by_session(data
->s
), data
->s
->tunnel
, "Walled Garden allowing login\n");
55 data
->auth_allowed
= 1;
56 data
->s
->walled_garden
= 1;
60 int plugin_new_session(struct param_new_session
*data
)
63 return PLUGIN_RET_OK
; // Slaves don't do walled garden processing.
65 if (data
->s
->walled_garden
)
66 garden_session(data
->s
, F_GARDEN
);
71 int plugin_kill_session(struct param_new_session
*data
)
74 return PLUGIN_RET_OK
; // Slaves don't do walled garden processing.
76 if (data
->s
->walled_garden
)
77 garden_session(data
->s
, F_CLEANUP
);
82 char *plugin_control_help
[] = {
83 " garden USER|SID Put user into the walled garden",
84 " ungarden SID Release session from garden",
88 int plugin_control(struct param_control
*data
)
98 if (strcmp(data
->argv
[0], "garden") && strcmp(data
->argv
[0], "ungarden"))
99 return PLUGIN_RET_OK
; // not for us
102 return PLUGIN_RET_NOTMASTER
;
104 flag
= data
->argv
[0][0] == 'g' ? F_GARDEN
: F_UNGARDEN
;
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
)
116 session
= p
->get_session_by_username(data
->argv
[1]);
118 session
= 0; // can't ungarden by username
122 s
= p
->get_session_by_id(session
);
126 data
->response
= NSCTL_RES_ERR
;
127 data
->additional
= "session not found";
128 return PLUGIN_RET_STOP
;
131 if (s
->walled_garden
== flag
)
133 data
->response
= NSCTL_RES_ERR
;
134 data
->additional
= flag
? "already in walled garden" : "not in walled garden";
135 return PLUGIN_RET_STOP
;
138 garden_session(s
, flag
);
139 p
->session_changed(session
);
141 data
->response
= NSCTL_RES_OK
;
142 data
->additional
= 0;
144 return PLUGIN_RET_STOP
;
147 int plugin_become_master(void)
150 iam_master
= 1; // We just became the master. Wow!
152 for (i
= 0; up_commands
[i
] && *up_commands
[i
]; i
++)
154 p
->log(3, 0, 0, "Running %s\n", up_commands
[i
]);
155 system(up_commands
[i
]);
158 return PLUGIN_RET_OK
;
161 // Called for each active session after becoming master
162 int plugin_new_session_master(sessiont
*s
)
164 if (s
->walled_garden
)
165 garden_session(s
, F_GARDEN
);
167 return PLUGIN_RET_OK
;
170 int garden_session(sessiont
*s
, int flag
)
176 if (!s
->opened
) return 0;
178 sess
= p
->get_id_by_session(s
);
179 if (flag
== F_GARDEN
)
181 p
->log(2, sess
, s
->tunnel
, "Garden user %s (%s)\n", s
->user
, p
->fmtaddr(htonl(s
->ip
), 0));
182 snprintf(cmd
, sizeof(cmd
), "iptables -t nat -A garden_users -s %s -j garden", p
->fmtaddr(htonl(s
->ip
), 0));
183 p
->log(3, sess
, s
->tunnel
, "%s\n", cmd
);
185 s
->walled_garden
= 1;
193 p
->log(2, sess
, s
->tunnel
, "Un-Garden user %s (%s)\n", s
->user
, p
->fmtaddr(htonl(s
->ip
), 0));
194 // Kick off any duplicate usernames
195 // but make sure not to kick off ourself
196 if (s
->ip
&& !s
->die
&& (other
= p
->get_session_by_username(s
->user
)) && s
!= p
->get_session_by_id(other
)) {
197 p
->sessionkill(other
, "Duplicate session when user released from walled garden");
199 /* Clean up counters */
200 s
->cin
= s
->cout
= 0;
201 s
->pin
= s
->pout
= 0;
203 snprintf(cmd
, sizeof(cmd
), "iptables -t nat -D garden_users -s %s -j garden", p
->fmtaddr(htonl(s
->ip
), 0));
204 p
->log(3, sess
, s
->tunnel
, "%s\n", cmd
);
207 int status
= system(cmd
);
208 if (WEXITSTATUS(status
) != 0) break;
211 s
->walled_garden
= 0;
213 if (flag
!= F_CLEANUP
)
216 u16 r
= p
->radiusnew(p
->get_id_by_session(s
));
217 p
->radiussend(r
, RADIUSSTART
);
224 int plugin_init(struct pluginfuncs
*funcs
)
234 if ((tables
= fopen("/proc/net/ip_tables_names", "r")))
237 while (fgets(buf
, sizeof(buf
), tables
) && !found_nat
)
238 found_nat
= !strcmp(buf
, "nat\n");
243 /* master killed/crashed? */
247 for (i
= 0; down_commands
[i
] && *down_commands
[i
]; i
++)
249 p
->log(3, 0, 0, "Running %s\n", down_commands
[i
]);
250 system(down_commands
[i
]);
261 if (!iam_master
) // Never became master. nothing to do.
264 for (i
= 0; down_commands
[i
] && *down_commands
[i
]; i
++)
266 p
->log(3, 0, 0, "Running %s\n", down_commands
[i
]);
267 system(down_commands
[i
]);