10 char const *cvs_id
= "$Id: garden.c,v 1.8 2004-10-30 07:35:31 bodea Exp $";
12 int __plugin_api_version
= 1;
13 static struct pluginfuncs
*p
= 0;
15 static int iam_master
= 0; // We're all slaves! Slaves I tell you!
17 char *up_commands
[] = {
18 "iptables -t nat -N garden >/dev/null 2>&1", // Create a chain that all gardened users will go through
19 "iptables -t nat -F garden",
20 ". " PLUGINCONF
"/build-garden", // Populate with site-specific DNAT rules
21 "iptables -t nat -N garden_users >/dev/null 2>&1", // Empty chain, users added/removed by garden_session
22 "iptables -t nat -F garden_users",
23 "iptables -t nat -A PREROUTING -j garden_users", // DNAT any users on the garden_users chain
24 "sysctl -w net.ipv4.ip_conntrack_max=256000 >/dev/null", // lots of entries
28 char *down_commands
[] = {
29 "iptables -t nat -F PREROUTING",
30 "iptables -t nat -F garden_users",
31 "iptables -t nat -X garden_users",
32 "iptables -t nat -F garden",
33 "iptables -t nat -X garden",
34 "rmmod iptable_nat", // Should also remove ip_conntrack, but
35 // doing so can take hours... literally.
36 // If a master is re-started as a slave,
37 // either rmmod manually, or reboot.
41 int garden_session(sessiont
*s
, int flag
);
43 int plugin_post_auth(struct param_post_auth
*data
)
45 // Ignore if user authentication was successful
46 if (data
->auth_allowed
) return PLUGIN_RET_OK
;
48 p
->log(3, 0, 0, 0, "Walled Garden allowing login\n");
49 data
->auth_allowed
= 1;
50 data
->s
->walled_garden
= 1;
54 int plugin_new_session(struct param_new_session
*data
)
57 return PLUGIN_RET_OK
; // Slaves don't do walled garden processing.
59 if (data
->s
->walled_garden
)
60 garden_session(data
->s
, 1);
65 int plugin_kill_session(struct param_new_session
*data
)
68 return PLUGIN_RET_OK
; // Slaves don't do walled garden processing.
70 if (data
->s
->walled_garden
)
71 garden_session(data
->s
, 0);
76 int plugin_control(struct param_control
*data
)
81 if (!iam_master
) // All garden processing happens on the master.
84 if (data
->type
!= PKT_GARDEN
&& data
->type
!= PKT_UNGARDEN
)
87 if (!data
->data
&& data
->data_length
)
90 session
= atoi((char*)(data
->data
));
94 data
->send_response
= 1;
95 s
= p
->get_session_by_id(session
);
98 char *errormsg
= "Session not connected";
99 *(short *)(data
->response
+ 2) = ntohs(PKT_RESP_ERROR
);
100 sprintf((data
->response
+ data
->response_length
), "%s", errormsg
);
101 data
->response_length
+= strlen(errormsg
) + 1;
103 p
->log(3, 0, 0, 0, "Unknown session %d\n", session
);
104 return PLUGIN_RET_STOP
;
106 *(short *)(data
->response
+ 2) = ntohs(PKT_RESP_OK
);
108 if (!(garden_session(s
, (data
->type
== PKT_GARDEN
))))
110 char *errormsg
= "User not connected";
111 *(short *)(data
->response
+ 2) = ntohs(PKT_RESP_ERROR
);
112 sprintf((data
->response
+ data
->response_length
), "%s", errormsg
);
113 data
->response_length
+= strlen(errormsg
) + 1;
116 return PLUGIN_RET_STOP
;
119 int plugin_become_master(void)
122 iam_master
= 1; // We just became the master. Wow!
124 for (i
= 0; up_commands
[i
] && *up_commands
[i
]; i
++)
126 p
->log(3, 0, 0, 0, "Running %s\n", up_commands
[i
]);
127 system(up_commands
[i
]);
130 return PLUGIN_RET_OK
;
133 // Called for each active session after becoming master
134 int plugin_new_session_master(sessiont
* s
)
136 if (s
->walled_garden
)
137 garden_session(s
, 1);
139 return PLUGIN_RET_OK
;
142 int garden_session(sessiont
*s
, int flag
)
147 if (!s
->opened
) return 0;
151 p
->log(2, 0, 0, s
->tunnel
, "Garden user %s (%s)\n", s
->user
, p
->inet_toa(htonl(s
->ip
)));
152 snprintf(cmd
, sizeof(cmd
), "iptables -t nat -A garden_users -s %s -j garden", p
->inet_toa(htonl(s
->ip
)));
153 p
->log(3, 0, 0, s
->tunnel
, "%s\n", cmd
);
155 s
->walled_garden
= 1;
163 p
->log(2, 0, 0, s
->tunnel
, "Un-Garden user %s (%s)\n", s
->user
, p
->inet_toa(htonl(s
->ip
)));
164 // Kick off any duplicate usernames
165 // but make sure not to kick off ourself
166 if (s
->ip
&& !s
->die
&& (other
= p
->get_session_by_username(s
->user
)) && s
!= p
->get_session_by_id(other
)) {
167 p
->sessionkill(other
, "Duplicate session when user released from walled garden");
169 /* Clean up counters */
170 s
->cin
= s
->cout
= 0;
171 s
->pin
= s
->pout
= 0;
173 snprintf(cmd
, sizeof(cmd
), "iptables -t nat -D garden_users -s %s -j garden", p
->inet_toa(htonl(s
->ip
)));
174 p
->log(3, 0, 0, s
->tunnel
, "%s\n", cmd
);
177 int status
= system(cmd
);
178 if (WEXITSTATUS(status
) != 0) break;
181 s
->walled_garden
= 0;
185 u16 r
= p
->radiusnew(p
->get_id_by_session(s
));
186 p
->radiussend(r
, RADIUSSTART
);
189 s
->walled_garden
= flag
;
193 int plugin_init(struct pluginfuncs
*funcs
)
203 if ((tables
= fopen("/proc/net/ip_tables_names", "r")))
206 while (fgets(buf
, sizeof(buf
), tables
) && !found_nat
)
207 found_nat
= !strcmp(buf
, "nat\n");
212 /* master killed/crashed? */
216 for (i
= 0; down_commands
[i
] && *down_commands
[i
]; i
++)
218 p
->log(3, 0, 0, 0, "Running %s\n", down_commands
[i
]);
219 system(down_commands
[i
]);
230 if (!iam_master
) // Never became master. nothing to do.
233 for (i
= 0; down_commands
[i
] && *down_commands
[i
]; i
++)
235 p
->log(3, 0, 0, 0, "Running %s\n", down_commands
[i
]);
236 system(down_commands
[i
]);