59
59
#include "client_list.h"
60
60
#include "commandline.h"
61
61
62
-
63
62
static int _fw_deny_raw (const char * , const char * , const int );
64
63
65
64
/**
@@ -71,14 +70,14 @@ static int _fw_deny_raw(const char *, const char *, const int);
71
70
* @return Return code of the command
72
71
*/
73
72
int
74
- fw_allow (t_client * client , int new_fw_connection_state )
73
+ fw_allow (t_client * client , int new_fw_connection_state )
75
74
{
76
75
int result ;
77
76
int old_state = client -> fw_connection_state ;
78
77
79
78
debug (LOG_DEBUG , "Allowing %s %s with fw_connection_state %d" , client -> ip , client -> mac , new_fw_connection_state );
80
79
client -> fw_connection_state = new_fw_connection_state ;
81
-
80
+
82
81
/* Grant first */
83
82
result = iptables_fw_access (FW_ACCESS_ALLOW , client -> ip , client -> mac , new_fw_connection_state );
84
83
@@ -112,12 +111,12 @@ fw_allow_host(const char *host)
112
111
* @return Return code of the command
113
112
*/
114
113
int
115
- fw_deny (t_client * client )
114
+ fw_deny (t_client * client )
116
115
{
117
116
int fw_connection_state = client -> fw_connection_state ;
118
117
debug (LOG_DEBUG , "Denying %s %s with fw_connection_state %d" , client -> ip , client -> mac , client -> fw_connection_state );
119
118
120
- client -> fw_connection_state = FW_MARK_NONE ; /* Clear */
119
+ client -> fw_connection_state = FW_MARK_NONE ; /* Clear */
121
120
return _fw_deny_raw (client -> ip , client -> mac , fw_connection_state );
122
121
}
123
122
@@ -138,51 +137,49 @@ _fw_deny_raw(const char *ip, const char *mac, const int mark)
138
137
int
139
138
fw_set_authdown (void )
140
139
{
141
- debug (LOG_DEBUG , "Marking auth server down" );
140
+ debug (LOG_DEBUG , "Marking auth server down" );
142
141
143
- return iptables_fw_auth_unreachable (FW_MARK_AUTH_IS_DOWN );
142
+ return iptables_fw_auth_unreachable (FW_MARK_AUTH_IS_DOWN );
144
143
}
145
144
146
145
/** Remove passthrough for clients when auth server is up */
147
146
int
148
147
fw_set_authup (void )
149
148
{
150
- debug (LOG_DEBUG , "Marking auth server up again" );
149
+ debug (LOG_DEBUG , "Marking auth server up again" );
151
150
152
- return iptables_fw_auth_reachable ();
151
+ return iptables_fw_auth_reachable ();
153
152
}
154
153
155
-
156
-
157
154
/* XXX DCY */
158
155
/**
159
156
* Get an IP's MAC address from the ARP cache.
160
157
* Go through all the entries in /proc/net/arp until we find the requested
161
158
* IP address and return the MAC address bound to it.
162
159
* @todo Make this function portable (using shell scripts?)
163
160
*/
164
- char *
161
+ char *
165
162
arp_get (const char * req_ip )
166
163
{
167
- FILE * proc ;
168
- char ip [16 ];
169
- char mac [18 ];
170
- char * reply ;
164
+ FILE * proc ;
165
+ char ip [16 ];
166
+ char mac [18 ];
167
+ char * reply ;
171
168
172
169
if (!(proc = fopen ("/proc/net/arp" , "r" ))) {
173
170
return NULL ;
174
171
}
175
172
176
173
/* Skip first line */
177
- while (!feof (proc ) && fgetc (proc ) != '\n' );
174
+ while (!feof (proc ) && fgetc (proc ) != '\n' ) ;
178
175
179
- /* Find ip, copy mac in reply */
180
- reply = NULL ;
176
+ /* Find ip, copy mac in reply */
177
+ reply = NULL ;
181
178
while (!feof (proc ) && (fscanf (proc , " %15[0-9.] %*s %*s %17[A-Fa-f0-9:] %*s %*s" , ip , mac ) == 2 )) {
182
- if (strcmp (ip , req_ip ) == 0 ) {
183
- reply = safe_strdup (mac );
184
- break ;
185
- }
179
+ if (strcmp (ip , req_ip ) == 0 ) {
180
+ reply = safe_strdup (mac );
181
+ break ;
182
+ }
186
183
}
187
184
188
185
fclose (proc );
@@ -195,9 +192,9 @@ arp_get(const char *req_ip)
195
192
int
196
193
fw_init (void )
197
194
{
198
- int result = 0 ;
195
+ int result = 0 ;
199
196
int new_fw_state ;
200
- t_client * client = NULL ;
197
+ t_client * client = NULL ;
201
198
202
199
if (!init_icmp_socket ()) {
203
200
return 0 ;
@@ -206,38 +203,38 @@ fw_init(void)
206
203
debug (LOG_INFO , "Initializing Firewall" );
207
204
result = iptables_fw_init ();
208
205
209
- if (restart_orig_pid ) {
210
- debug (LOG_INFO , "Restoring firewall rules for clients inherited from parent" );
211
- LOCK_CLIENT_LIST ();
212
- client = client_get_first_client ();
213
- while (client ) {
206
+ if (restart_orig_pid ) {
207
+ debug (LOG_INFO , "Restoring firewall rules for clients inherited from parent" );
208
+ LOCK_CLIENT_LIST ();
209
+ client = client_get_first_client ();
210
+ while (client ) {
214
211
new_fw_state = client -> fw_connection_state ;
215
212
client -> fw_connection_state = FW_MARK_NONE ;
216
- fw_allow (client , new_fw_state );
217
- client = client -> next ;
218
- }
219
- UNLOCK_CLIENT_LIST ();
220
- }
213
+ fw_allow (client , new_fw_state );
214
+ client = client -> next ;
215
+ }
216
+ UNLOCK_CLIENT_LIST ();
217
+ }
221
218
222
- return result ;
219
+ return result ;
223
220
}
224
221
225
222
/** Remove all auth server firewall whitelist rules
226
223
*/
227
224
void
228
225
fw_clear_authservers (void )
229
226
{
230
- debug (LOG_INFO , "Clearing the authservers list" );
231
- iptables_fw_clear_authservers ();
227
+ debug (LOG_INFO , "Clearing the authservers list" );
228
+ iptables_fw_clear_authservers ();
232
229
}
233
230
234
231
/** Add the necessary firewall rules to whitelist the authservers
235
232
*/
236
233
void
237
234
fw_set_authservers (void )
238
235
{
239
- debug (LOG_INFO , "Setting the authservers list" );
240
- iptables_fw_set_authservers ();
236
+ debug (LOG_INFO , "Setting the authservers list" );
237
+ iptables_fw_set_authservers ();
241
238
}
242
239
243
240
/** Remove the firewall rules
@@ -258,8 +255,8 @@ fw_destroy(void)
258
255
void
259
256
fw_sync_with_authserver (void )
260
257
{
261
- t_authresponse authresponse ;
262
- t_client * p1 , * p2 , * worklist , * tmp ;
258
+ t_authresponse authresponse ;
259
+ t_client * p1 , * p2 , * worklist , * tmp ;
263
260
s_config * config = config_get_config ();
264
261
265
262
if (-1 == iptables_fw_counters_update ()) {
@@ -287,16 +284,19 @@ fw_sync_with_authserver(void)
287
284
icmp_ping (p1 -> ip );
288
285
/* Update the counters on the remote server only if we have an auth server */
289
286
if (config -> auth_servers != NULL ) {
290
- auth_server_request (& authresponse , REQUEST_TYPE_COUNTERS , p1 -> ip , p1 -> mac , p1 -> token , p1 -> counters .incoming , p1 -> counters .outgoing );
287
+ auth_server_request (& authresponse , REQUEST_TYPE_COUNTERS , p1 -> ip , p1 -> mac , p1 -> token , p1 -> counters .incoming ,
288
+ p1 -> counters .outgoing );
291
289
}
292
290
293
- time_t current_time = time (NULL );
294
- debug (LOG_INFO , "Checking client %s for timeout: Last updated %ld (%ld seconds ago), timeout delay %ld seconds, current time %ld, " ,
295
- p1 -> ip , p1 -> counters .last_updated , current_time - p1 -> counters .last_updated , config -> checkinterval * config -> clienttimeout , current_time );
291
+ time_t current_time = time (NULL );
292
+ debug (LOG_INFO ,
293
+ "Checking client %s for timeout: Last updated %ld (%ld seconds ago), timeout delay %ld seconds, current time %ld, " ,
294
+ p1 -> ip , p1 -> counters .last_updated , current_time - p1 -> counters .last_updated ,
295
+ config -> checkinterval * config -> clienttimeout , current_time );
296
296
if (p1 -> counters .last_updated + (config -> checkinterval * config -> clienttimeout ) <= current_time ) {
297
297
/* Timing out user */
298
298
debug (LOG_INFO , "%s - Inactive for more than %ld seconds, removing client and denying in firewall" ,
299
- p1 -> ip , config -> checkinterval * config -> clienttimeout );
299
+ p1 -> ip , config -> checkinterval * config -> clienttimeout );
300
300
LOCK_CLIENT_LIST ();
301
301
tmp = client_list_find_by_client (p1 );
302
302
if (NULL != tmp ) {
@@ -320,56 +320,59 @@ fw_sync_with_authserver(void)
320
320
if (NULL == tmp ) {
321
321
UNLOCK_CLIENT_LIST ();
322
322
debug (LOG_NOTICE , "Client was already removed. Skipping auth processing" );
323
- continue ; /* Next client please */
323
+ continue ; /* Next client please */
324
324
}
325
-
325
+
326
326
if (config -> auth_servers != NULL ) {
327
327
switch (authresponse .authcode ) {
328
- case AUTH_DENIED :
329
- debug (LOG_NOTICE , "%s - Denied. Removing client and firewall rules" , tmp -> ip );
330
- fw_deny (tmp );
331
- client_list_delete (tmp );
332
- break ;
333
-
334
- case AUTH_VALIDATION_FAILED :
335
- debug (LOG_NOTICE , "%s - Validation timeout, now denied. Removing client and firewall rules" , tmp -> ip );
336
- fw_deny (tmp );
337
- client_list_delete (tmp );
338
- break ;
339
-
340
- case AUTH_ALLOWED :
341
- if (tmp -> fw_connection_state != FW_MARK_KNOWN ) {
342
- debug (LOG_INFO , "%s - Access has changed to allowed, refreshing firewall and clearing counters" , tmp -> ip );
343
- //WHY did we deny, then allow!?!? benoitg 2007-06-21
344
- //fw_deny(tmp->ip, tmp->mac, tmp->fw_connection_state); /* XXX this was possibly to avoid dupes. */
345
-
346
- if (tmp -> fw_connection_state != FW_MARK_PROBATION ) {
347
- tmp -> counters .incoming = tmp -> counters .outgoing = 0 ;
348
- }
349
- else {
350
- //We don't want to clear counters if the user was in validation, it probably already transmitted data..
351
- debug (LOG_INFO , "%s - Skipped clearing counters after all, the user was previously in validation" , tmp -> ip );
352
- }
353
- fw_allow (tmp , FW_MARK_KNOWN );
328
+ case AUTH_DENIED :
329
+ debug (LOG_NOTICE , "%s - Denied. Removing client and firewall rules" , tmp -> ip );
330
+ fw_deny (tmp );
331
+ client_list_delete (tmp );
332
+ break ;
333
+
334
+ case AUTH_VALIDATION_FAILED :
335
+ debug (LOG_NOTICE , "%s - Validation timeout, now denied. Removing client and firewall rules" ,
336
+ tmp -> ip );
337
+ fw_deny (tmp );
338
+ client_list_delete (tmp );
339
+ break ;
340
+
341
+ case AUTH_ALLOWED :
342
+ if (tmp -> fw_connection_state != FW_MARK_KNOWN ) {
343
+ debug (LOG_INFO , "%s - Access has changed to allowed, refreshing firewall and clearing counters" ,
344
+ tmp -> ip );
345
+ //WHY did we deny, then allow!?!? benoitg 2007-06-21
346
+ //fw_deny(tmp->ip, tmp->mac, tmp->fw_connection_state); /* XXX this was possibly to avoid dupes. */
347
+
348
+ if (tmp -> fw_connection_state != FW_MARK_PROBATION ) {
349
+ tmp -> counters .incoming = tmp -> counters .outgoing = 0 ;
350
+ } else {
351
+ //We don't want to clear counters if the user was in validation, it probably already transmitted data..
352
+ debug (LOG_INFO ,
353
+ "%s - Skipped clearing counters after all, the user was previously in validation" ,
354
+ tmp -> ip );
354
355
}
355
- break ;
356
-
357
- case AUTH_VALIDATION :
358
- /*
359
- * Do nothing, user
360
- * is in validation
361
- * period
362
- */
363
- debug (LOG_INFO , "%s - User in validation period" , tmp -> ip );
364
- break ;
365
-
366
- case AUTH_ERROR :
367
- debug (LOG_WARNING , "Error communicating with auth server - leaving %s as-is for now" , tmp -> ip );
368
- break ;
369
-
370
- default :
371
- debug (LOG_ERR , "I do not know about authentication code %d" , authresponse .authcode );
372
- break ;
356
+ fw_allow (tmp , FW_MARK_KNOWN );
357
+ }
358
+ break ;
359
+
360
+ case AUTH_VALIDATION :
361
+ /*
362
+ * Do nothing, user
363
+ * is in validation
364
+ * period
365
+ */
366
+ debug (LOG_INFO , "%s - User in validation period" , tmp -> ip );
367
+ break ;
368
+
369
+ case AUTH_ERROR :
370
+ debug (LOG_WARNING , "Error communicating with auth server - leaving %s as-is for now" , tmp -> ip );
371
+ break ;
372
+
373
+ default :
374
+ debug (LOG_ERR , "I do not know about authentication code %d" , authresponse .authcode );
375
+ break ;
373
376
}
374
377
}
375
378
UNLOCK_CLIENT_LIST ();
@@ -378,4 +381,3 @@ fw_sync_with_authserver(void)
378
381
379
382
client_list_destroy (worklist );
380
383
}
381
-
0 commit comments