Changeset 121
- Timestamp:
- 05/07/04 15:59:03 (9 years ago)
- Location:
- trunk/wifidog
- Files:
-
- 9 modified
-
ChangeLog (modified) (1 diff)
-
src/auth.c (modified) (2 diffs)
-
src/centralserver.c (modified) (2 diffs)
-
src/centralserver.h (modified) (1 diff)
-
src/firewall.c (modified) (3 diffs)
-
src/firewall.h (modified) (2 diffs)
-
src/http.c (modified) (2 diffs)
-
src/iptables.c (modified) (6 diffs)
-
src/iptables.h (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/wifidog/ChangeLog
r120 r121 1 1 # $Header$ 2 2004-05-07 Philippe April <wifidog@philippeapril.com> 3 * Now we store both incoming and outgoing counters on server 4 and expire if no activity at all on both 5 * Changed the structure of nodes a little 6 2 7 2004-05-07 Philippe April <wifidog@philippeapril.com> 3 8 * New parameter ExternalInterface -
trunk/wifidog/src/auth.c
r119 r121 102 102 pthread_mutex_unlock(&nodes_mutex); 103 103 104 authenticate(&auth_response, ip, mac, token, 0 );104 authenticate(&auth_response, ip, mac, token, 0, 0); 105 105 106 106 pthread_mutex_lock(&nodes_mutex); … … 143 143 144 144 p1 = node_find_by_ip(node->ip); 145 p1->noactivity = time(NULL);146 145 switch(auth_response.authcode) { 147 146 case AUTH_VALIDATION: -
trunk/wifidog/src/centralserver.c
r119 r121 49 49 50 50 int 51 authenticate(t_authresponse *authresponse, char *ip, char *mac, char *token, long int stats)51 authenticate(t_authresponse *authresponse, char *ip, char *mac, char *token, long int incoming, long int outgoing) 52 52 { 53 53 int sockfd, numbytes; … … 81 81 return(-1); /* non-fatal */ 82 82 } 83 sprintf(buf, "GET %s?ip=%s&mac=%s&token=%s& stats=%ld HTTP/1.1"83 sprintf(buf, "GET %s?ip=%s&mac=%s&token=%s&incoming=%ld&outgoing=%ld HTTP/1.1" 84 84 "\nHost: %s\n\n", config.authserv_path, ip, mac, token, 85 stats, config.authserv_hostname);85 incoming, outgoing, config.authserv_hostname); 86 86 send(sockfd, buf, strlen(buf), 0); 87 87 -
trunk/wifidog/src/centralserver.h
r95 r121 28 28 #define _CENTRALSERVER_H_ 29 29 30 int authenticate(t_authresponse *authresponse, char *ip, char *mac, char *token, long int stats);30 int authenticate(t_authresponse *authresponse, char *ip, char *mac, char *token, long int incoming, long int outgoing); 31 31 32 32 #endif /* _CENTRALSERVER_H_ */ -
trunk/wifidog/src/firewall.c
r119 r121 200 200 fw_counter(void) 201 201 { 202 FILE *output;203 unsigned long int counter;204 202 t_authresponse authresponse; 205 unsigned int tag, rc; 206 char ip[255], 207 mac[255], 208 *script, 209 *token; 210 t_node *p1; 211 212 /* FIXME make iptables a DEFINE or something */ 213 asprintf(&script, "%s %s", "iptables", "-v -x -t mangle -L " TABLE_WIFIDOG_MARK); 214 215 if (!(output = popen(script, "r"))) { 216 debug(LOG_ERR, "popen(): %s", strerror(errno)); 203 char *token, *ip; 204 t_node *p1, *p2; 205 206 if (-1 == iptables_fw_counters()) { 207 debug(LOG_ERR, "Could not get counters from firewall!"); 217 208 return; 218 209 } 219 210 220 free(script); 221 222 /* skip the first two lines */ 223 while (('\n' != fgetc(output)) && !feof(output)) 224 ; 225 while (('\n' != fgetc(output)) && !feof(output)) 226 ; 227 while (output && !(feof(output))) { 228 rc = fscanf(output, "%*s %lu %*s %*s %*s %*s %*s %s %*s %*s %s %*s %*s 0x%u", &counter, ip, mac, &tag); 229 if (4 == rc && EOF != rc) { 230 pthread_mutex_lock(&nodes_mutex); 231 232 p1 = node_find_by_ip(ip); 233 234 if (p1) { 235 token = strdup(p1->token); 236 237 pthread_mutex_unlock(&nodes_mutex); 238 authenticate(&authresponse, ip, mac, token, counter); 239 pthread_mutex_lock(&nodes_mutex); 240 241 free(token); 242 243 p1 = node_find_by_ip(ip); 244 if (p1 == NULL) { 245 /* FIXME We should not continue for this entry past this point */ 246 debug(LOG_DEBUG, "Node %s was freed while being re-validated!", ip); 211 p1 = firstnode; 212 213 pthread_mutex_lock(&nodes_mutex); 214 p2 = firstnode; 215 while (NULL != (p1 = p2)) { 216 p2 = p1->next; 217 ip = strdup(p1->ip); 218 token = strdup(p1->token); 219 pthread_mutex_unlock(&nodes_mutex); 220 authenticate(&authresponse, p1->ip, p1->mac, token, p1->counters.incoming, p1->counters.outgoing); 221 pthread_mutex_lock(&nodes_mutex); 222 223 if (!(p1 = node_find_by_ip(ip))) { 224 debug(LOG_ERR, "Node %s was freed while being re-validated!", ip); 225 } else { 226 if (p1->counters.last_updated + 227 (config.checkinterval * config.clienttimeout) 228 <= time(NULL)) { 229 /* Timing out user */ 230 debug(LOG_INFO, "%s - Inactive for %ld seconds, removing node and denying in firewall", p1->ip, config.checkinterval * config.clienttimeout); 231 fw_deny(p1->ip, p1->mac, p1->tag); 232 node_delete(p1); 233 } else { 234 /* 235 * This handles any change in 236 * the status this allows us 237 * to change the status of a 238 * user while he's connected 239 */ 240 switch (authresponse.authcode) { 241 case AUTH_DENIED: 242 243 case AUTH_VALIDATION_FAILED: 244 debug(LOG_NOTICE, "%s - Validation timeout, now denied. Removing node and firewall rules", p1->ip); 245 fw_deny(p1->ip, p1->mac, p1->tag); 246 node_delete(p1); 247 break; 248 249 case AUTH_ALLOWED: 250 if (p1->tag != MARK_KNOWN) { 251 debug(LOG_INFO, "%s - Access has changed, refreshing firewall and clearing counters", p1->ip); 252 fw_deny(p1->ip, p1->mac, p1->tag); 253 p1->tag = MARK_KNOWN; 254 p1->counters.incoming = p1->counters.outgoing = 0; 255 fw_allow(p1->ip, p1->mac, p1->tag); 256 } 257 break; 258 259 case AUTH_VALIDATION: 260 /* 261 * Do nothing, user 262 * is in validation 263 * period 264 */ 265 debug(LOG_INFO, "%s - User in validation period", p1->ip); 266 break; 267 268 default: 269 debug(LOG_DEBUG, "I do not know about authentication code %d", authresponse.authcode); 270 break; 247 271 } 248 debug(LOG_DEBUG, "%s - Counter currently %ld, new counter %ld", p1->ip, p1->counter, counter);249 if (counter > p1->counter) {250 p1->counter = counter;251 debug(LOG_INFO, "%s - Updated counter to %ld bytes", p1->ip, p1->counter);252 p1->noactivity = time(NULL);253 } else {254 debug(LOG_INFO, "%s - Recorded no activity since %ld", p1->ip, p1->noactivity);255 }256 if (p1->noactivity +257 (config.checkinterval * config.clienttimeout)258 <= time(NULL)) {259 /* Timing out user */260 debug(LOG_INFO, "%s - Inactive for %ld seconds, removing node and denying in firewall", ip, config.checkinterval * config.clienttimeout);261 fw_deny(p1->ip, p1->mac, p1->tag);262 node_delete(p1);263 } else {264 /*265 * This handles any change in266 * the status this allows us267 * to change the status of a268 * user while he's connected269 */270 switch (authresponse.authcode) {271 case AUTH_DENIED:272 273 case AUTH_VALIDATION_FAILED:274 debug(LOG_NOTICE, "%s - Validation timeout, now denied. Removing node and firewall rules", ip);275 fw_deny(p1->ip, p1->mac, p1->tag);276 node_delete(p1);277 break;278 279 case AUTH_ALLOWED:280 if (p1->tag != MARK_KNOWN) {281 debug(LOG_INFO, "%s - Access has changed, refreshing firewall and clearing counters", ip);282 fw_deny(p1->ip, p1->mac, p1->tag);283 p1->tag = MARK_KNOWN;284 p1->counter = 0;285 fw_allow(p1->ip, p1->mac, p1->tag);286 }287 break;288 289 case AUTH_VALIDATION:290 /*291 * Do nothing, user292 * is in validation293 * period294 */295 debug(LOG_INFO, "%s - User in validation period", ip);296 break;297 298 default:299 debug(LOG_DEBUG, "I do not know about authentication code %d", authresponse.authcode);300 break;301 }302 }303 pthread_mutex_unlock(&nodes_mutex);304 } else {305 /* Node was not found in list, FIXME remove from firewall rules */306 debug(LOG_NOTICE, "Node %s was not found in list", ip);307 272 } 308 273 } 309 } 310 pclose(output); 274 275 free(token); 276 free(ip); 277 } 278 pthread_mutex_unlock(&nodes_mutex); 279 280 281 // debug(LOG_DEBUG, "%s - Counter currently %ld, new counter %ld", p1->ip, p1->counter, counters[i]->outgoing); 282 // 283 // if (counters[i]->outgoing > p1->counter) { 284 // p1->counter = counters[i]->outgoing; 285 // debug(LOG_INFO, "%s - Updated counter to %ld bytes", p1->ip, p1->counter); 286 // p1->noactivity = time(NULL); 287 // } else { 288 // debug(LOG_INFO, "%s - Recorded no activity since %ld", p1->ip, p1->noactivity); 289 // } 290 // if (p1->noactivity + 291 // (config.checkinterval * config.clienttimeout) 292 // <= time(NULL)) { 293 // /* Timing out user */ 294 // debug(LOG_INFO, "%s - Inactive for %ld seconds, removing node and denying in firewall", p1->ip, config.checkinterval * config.clienttimeout); 295 // fw_deny(p1->ip, p1->mac, p1->tag); 296 // node_delete(p1); 297 // } else { 298 // /* 299 // * This handles any change in 300 // * the status this allows us 301 // * to change the status of a 302 // * user while he's connected 303 // */ 304 // switch (authresponse.authcode) { 305 // case AUTH_DENIED: 306 // 307 // case AUTH_VALIDATION_FAILED: 308 // debug(LOG_NOTICE, "%s - Validation timeout, now denied. Removing node and firewall rules", p1->ip); 309 // fw_deny(p1->ip, p1->mac, p1->tag); 310 // node_delete(p1); 311 // break; 312 // 313 // case AUTH_ALLOWED: 314 // if (p1->tag != MARK_KNOWN) { 315 // debug(LOG_INFO, "%s - Access has changed, refreshing firewall and clearing counters", p1->ip); 316 // fw_deny(p1->ip, p1->mac, p1->tag); 317 // p1->tag = MARK_KNOWN; 318 // p1->counter = 0; 319 // fw_allow(p1->ip, p1->mac, p1->tag); 320 // } 321 // break; 322 // 323 // case AUTH_VALIDATION: 324 // /* 325 // * Do nothing, user 326 // * is in validation 327 // * period 328 // */ 329 // debug(LOG_INFO, "%s - User in validation period", p1->ip); 330 // break; 331 // 332 // default: 333 // debug(LOG_DEBUG, "I do not know about authentication code %d", authresponse.authcode); 334 // break; 335 // } 336 // } 337 // } else { 338 // /* Node was not found in list, FIXME remove from firewall rules */ 339 // debug(LOG_NOTICE, "Node %s was not found in list", counters[i]->ip); 340 // } 341 // pthread_mutex_unlock(&nodes_mutex); 342 // } 343 311 344 } 312 345 … … 331 364 * @param token Token 332 365 * @param counter Value of the counter at creation (usually 0) 333 * @param active Is the node active, or not334 366 * @return Pointer to the node we just created 335 367 */ 336 368 t_node * 337 node_add(char *ip, char *mac, char *token , long int counter, int active)369 node_add(char *ip, char *mac, char *token) 338 370 { 339 371 t_node *curnode, *prevnode; … … 358 390 curnode->mac = strdup(mac); 359 391 curnode->token = strdup(token); 360 curnode->counter = counter;361 curnode-> active = active;392 curnode->counters.incoming = curnode->counters.outgoing = 0; 393 curnode->counters.last_updated = time(NULL); 362 394 363 395 if (prevnode == NULL) { -
trunk/wifidog/src/firewall.h
r114 r121 34 34 } t_marks; 35 35 36 typedef struct counters_t_ { 37 long int incoming; 38 long int outgoing; 39 long int last_updated; 40 } counters_t; 41 36 42 typedef struct _t_node { 37 43 struct _t_node *next; 38 char *ip, 39 *mac, 40 *token; 41 int active, /* boolean */ 42 noactivity, /* seconds since there has not been activity */ 43 tag, /* the MARK in the firewall */ 44 fd; /* socket */ 45 long int counter; 44 char *ip; 45 char *mac; 46 char *token; 47 48 /* the MARK in the firewall */ 49 unsigned int tag; 50 51 /* socket */ 52 int fd; 53 54 /* the counters */ 55 counters_t counters; 46 56 } t_node; 47 57 … … 55 65 56 66 void node_init(void); 57 t_node *node_add(char *ip, char *mac, char *token, long int counter, 58 int active); 67 t_node *node_add(char *ip, char *mac, char *token); 59 68 t_node *node_find_by_ip(char *ip); 60 69 t_node *node_find_by_token(char *token); -
trunk/wifidog/src/http.c
r119 r121 111 111 debug(LOG_DEBUG, "New node for %s", 112 112 webserver->clientAddr); 113 node_add(webserver->clientAddr, mac, 114 token->value, 0, 0); 113 node_add(webserver->clientAddr, mac, token->value); 115 114 } else { 116 115 debug(LOG_DEBUG, "Node for %s already " … … 130 129 131 130 /* start sub process */ 132 pthread_create(&tid, NULL, (void *)auth_thread, 133 (void *)ip); 131 pthread_create(&tid, NULL, (void *)auth_thread, (void *)ip); 134 132 pthread_detach(tid); 135 133 -
trunk/wifidog/src/iptables.c
r120 r121 31 31 #include <stdlib.h> 32 32 #include <stdarg.h> 33 #include <syslog.h> 34 #include <errno.h> 35 #include <string.h> 36 #include <pthread.h> 33 37 34 38 #include "conf.h" 35 39 #include "iptables.h" 36 40 #include "firewall.h" 37 41 #include "debug.h" 42 43 extern pthread_mutex_t nodes_mutex; 38 44 extern s_config config; 39 45 extern int fw_quiet; … … 100 106 iptables_do_command("-t nat -I PREROUTING 1 -i %s -j " TABLE_WIFIDOG_CLASS, config.gw_interface); 101 107 102 iptables_do_command("-t mangle -N " TABLE_WIFIDOG_ MARK);103 iptables_do_command("-t mangle -I PREROUTING 1 -i %s -j " TABLE_WIFIDOG_ MARK, config.gw_interface);104 105 iptables_do_command("-t mangle -N " TABLE_WIFIDOG_ TRAFFIC);106 iptables_do_command("-t mangle -I FORWARD 1 -i %s -j " TABLE_WIFIDOG_ TRAFFIC, config.external_interface);108 iptables_do_command("-t mangle -N " TABLE_WIFIDOG_OUTGOING); 109 iptables_do_command("-t mangle -I PREROUTING 1 -i %s -j " TABLE_WIFIDOG_OUTGOING, config.gw_interface); 110 111 iptables_do_command("-t mangle -N " TABLE_WIFIDOG_INCOMING); 112 iptables_do_command("-t mangle -I FORWARD 1 -i %s -j " TABLE_WIFIDOG_INCOMING, config.external_interface); 107 113 108 114 return 1; … … 123 129 fw_quiet = 1; 124 130 iptables_do_command("-t nat -F " TABLE_WIFIDOG_CLASS); 125 iptables_do_command("-t mangle -F " TABLE_WIFIDOG_ MARK);126 iptables_do_command("-t mangle -F " TABLE_WIFIDOG_ TRAFFIC);131 iptables_do_command("-t mangle -F " TABLE_WIFIDOG_OUTGOING); 132 iptables_do_command("-t mangle -F " TABLE_WIFIDOG_INCOMING); 127 133 128 134 iptables_do_command("-t nat -F " TABLE_WIFIDOG_VALIDATE); … … 146 152 rc = 0; 147 153 for (tries = 0; tries < 10 && rc == 0; tries++) { 148 rc = iptables_do_command("-t mangle -D PREROUTING -i %s -j " TABLE_WIFIDOG_ MARK, config.gw_interface);149 } 150 iptables_do_command("-t mangle -X " TABLE_WIFIDOG_ MARK);154 rc = iptables_do_command("-t mangle -D PREROUTING -i %s -j " TABLE_WIFIDOG_OUTGOING, config.gw_interface); 155 } 156 iptables_do_command("-t mangle -X " TABLE_WIFIDOG_OUTGOING); 151 157 152 158 rc = 0; 153 159 for (tries = 0; tries < 10 && rc == 0; tries++) { 154 rc = iptables_do_command("-t mangle -D FORWARD -i %s -j " TABLE_WIFIDOG_ TRAFFIC, config.external_interface);155 } 156 iptables_do_command("-t mangle -X " TABLE_WIFIDOG_ TRAFFIC);160 rc = iptables_do_command("-t mangle -D FORWARD -i %s -j " TABLE_WIFIDOG_INCOMING, config.external_interface); 161 } 162 iptables_do_command("-t mangle -X " TABLE_WIFIDOG_INCOMING); 157 163 158 164 return 1; … … 167 173 switch(type) { 168 174 case FW_ACCESS_ALLOW: 169 iptables_do_command("-t mangle -A " TABLE_WIFIDOG_ MARK" -s %s -m mac --mac-source %s -j MARK --set-mark %d", ip, mac, tag);170 rc = iptables_do_command("-t mangle -A " TABLE_WIFIDOG_ TRAFFIC" -d %s -j ACCEPT", ip);175 iptables_do_command("-t mangle -A " TABLE_WIFIDOG_OUTGOING " -s %s -m mac --mac-source %s -j MARK --set-mark %d", ip, mac, tag); 176 rc = iptables_do_command("-t mangle -A " TABLE_WIFIDOG_INCOMING " -d %s -j ACCEPT", ip); 171 177 break; 172 178 case FW_ACCESS_DENY: 173 iptables_do_command("-t mangle -D " TABLE_WIFIDOG_ MARK" -s %s -m mac --mac-source %s -j MARK --set-mark %d", ip, mac, tag);174 rc = iptables_do_command("-t mangle -D " TABLE_WIFIDOG_ TRAFFIC" -d %s -j ACCEPT", ip);179 iptables_do_command("-t mangle -D " TABLE_WIFIDOG_OUTGOING " -s %s -m mac --mac-source %s -j MARK --set-mark %d", ip, mac, tag); 180 rc = iptables_do_command("-t mangle -D " TABLE_WIFIDOG_INCOMING " -d %s -j ACCEPT", ip); 175 181 break; 176 182 default: … … 182 188 } 183 189 190 int 191 iptables_fw_counters(void) 192 { 193 FILE *output; 194 char *script, 195 ip[16], 196 rc; 197 unsigned long int counter; 198 t_node *p1; 199 200 /* Look for outgoing traffic */ 201 asprintf(&script, "%s %s", "iptables", "-v -x -t mangle -L " TABLE_WIFIDOG_OUTGOING); 202 if (!(output = popen(script, "r"))) { 203 debug(LOG_ERR, "popen(): %s", strerror(errno)); 204 return -1; 205 } 206 free(script); 207 208 /* skip the first two lines */ 209 while (('\n' != fgetc(output)) && !feof(output)) 210 ; 211 while (('\n' != fgetc(output)) && !feof(output)) 212 ; 213 while (output && !(feof(output))) { 214 rc = fscanf(output, "%*s %lu %*s %*s %*s %*s %*s %s %*s %*s %*s %*s %*s 0x%*u", &counter, ip); 215 if (2 == rc && EOF != rc) { 216 debug(LOG_DEBUG, "Outgoing %s Bytes=%ld", ip, counter); 217 pthread_mutex_lock(&nodes_mutex); 218 if ((p1 = node_find_by_ip(ip))) { 219 if (p1->counters.outgoing < counter) { 220 p1->counters.outgoing = counter; 221 p1->counters.last_updated = time(NULL); 222 debug(LOG_DEBUG, "%s - Updated counter to %ld bytes", ip, counter); 223 } 224 } else { 225 debug(LOG_ERR, "Could not find %s in node list", ip); 226 } 227 pthread_mutex_unlock(&nodes_mutex); 228 } 229 } 230 pclose(output); 231 232 /* Look for incoming traffic */ 233 asprintf(&script, "%s %s", "iptables", "-v -x -t mangle -L " TABLE_WIFIDOG_INCOMING); 234 if (!(output = popen(script, "r"))) { 235 debug(LOG_ERR, "popen(): %s", strerror(errno)); 236 return -1; 237 } 238 free(script); 239 240 /* skip the first two lines */ 241 while (('\n' != fgetc(output)) && !feof(output)) 242 ; 243 while (('\n' != fgetc(output)) && !feof(output)) 244 ; 245 while (output && !(feof(output))) { 246 rc = fscanf(output, "%*s %lu %*s %*s %*s %*s %*s %*s %s", &counter, ip); 247 if (2 == rc && EOF != rc) { 248 debug(LOG_DEBUG, "Incoming %s Bytes=%ld", ip, counter); 249 pthread_mutex_lock(&nodes_mutex); 250 if ((p1 = node_find_by_ip(ip))) { 251 if (p1->counters.incoming < counter) { 252 p1->counters.incoming = counter; 253 p1->counters.last_updated = time(NULL); 254 debug(LOG_DEBUG, "%s - Updated counter to %ld bytes", ip, counter); 255 } 256 } else { 257 debug(LOG_ERR, "Could not find %s in node list", ip); 258 } 259 pthread_mutex_unlock(&nodes_mutex); 260 } 261 } 262 pclose(output); 263 264 return 1; 265 } 266 -
trunk/wifidog/src/iptables.h
r120 r121 28 28 #define _IPTABLES_H_ 29 29 30 #define TABLE_WIFIDOG_MARK "WiFiDog_Mark" 30 #include "firewall.h" 31 31 32 #define TABLE_WIFIDOG_CLASS "WiFiDog_Class" 32 #define TABLE_WIFIDOG_TRAFFIC "WiFiDog_Traffic" 33 #define TABLE_WIFIDOG_OUTGOING "WiFiDog_Outgoing" 34 #define TABLE_WIFIDOG_INCOMING "WiFiDog_Incoming" 33 35 34 36 #define TABLE_WIFIDOG_VALIDATE "WiFiDog_Validate" … … 46 48 int iptables_fw_destroy(void); 47 49 int iptables_fw_access(fw_access_t type, char *ip, char *mac, int tag); 50 int iptables_fw_counters(void); 48 51 49 52 #endif /* _IPTABLES_H_ */
