Changeset 18
- Timestamp:
- 03/13/04 00:18:22 (9 years ago)
- Location:
- trunk/wifidog
- Files:
-
- 8 modified
-
ChangeLog (modified) (1 diff)
-
src/common.h (modified) (2 diffs)
-
src/conf.c (modified) (4 diffs)
-
src/conf.h (modified) (1 diff)
-
src/firewall.c (modified) (5 diffs)
-
src/firewall.h (modified) (2 diffs)
-
src/gateway.c (modified) (2 diffs)
-
src/http.c (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/wifidog/ChangeLog
r17 r18 1 1 # $Header$ 2 3 2004-03-13 Philippe April <papril777@yahoo.com> 4 * Modified the way firewall scripts are called so we can configure 5 them in the config file (a bit more modular than it was) 6 * Added simple linked list to keep track of clients and to 7 keep a counter of the utilization and send it to the auth 8 server 9 * Fixed CRLF/formatting in phpauth/auth/index.php 10 * Hacked phpauth/auth/index.php to handle very basic utilization tracking 2 11 3 12 2004-03-12 Philippe April <papril777@yahoo.com> -
trunk/wifidog/src/common.h
r17 r18 32 32 #include <sys/types.h> 33 33 #include <sys/socket.h> 34 #include <sys/stat.h> 34 35 #include <netinet/in.h> 35 36 #include <arpa/inet.h> … … 53 54 #define MAX_BUF 4096 54 55 56 #define SCRIPT_FWINIT "fw.init" 57 #define SCRIPT_FWACCESS "fw.access" 58 #define SCRIPT_FWDESTROY "fw.destroy" 59 #define SCRIPT_FWCOUNTERS "fw.counters" 60 55 61 #endif /* _COMMON_H_ */ -
trunk/wifidog/src/conf.c
r9 r18 36 36 #define DEFAULT_CLIENTTIMEOUT 5 37 37 #define DEFAULT_CHECKINTERVAL 5 38 #define DEFAULT_FWSCRIPTS_PATH "." 39 #define DEFAULT_FWTYPE "." 38 40 39 41 s_config config; … … 56 58 oClientTimeout, 57 59 oCheckInterval, 60 oFWScriptsPath, 61 oFWType, 58 62 } OpCodes; 59 63 … … 77 81 { "clienttimeout", oClientTimeout }, 78 82 { "checkinterval", oCheckInterval }, 83 { "fwscriptspath", oFWScriptsPath }, 84 { "fwtype", oFWType }, 79 85 { NULL, oBadOption }, 80 86 }; … … 100 106 config.clienttimeout = DEFAULT_CLIENTTIMEOUT; 101 107 config.checkinterval = DEFAULT_CHECKINTERVAL; 108 config.fwscripts_path = DEFAULT_FWSCRIPTS_PATH; 109 config.fwtype = DEFAULT_FWTYPE; 102 110 } 103 111 -
trunk/wifidog/src/conf.h
r9 r18 51 51 int clienttimeout; 52 52 int checkinterval; 53 char *fwscripts_path; 54 char *fwtype; 53 55 } s_config; 54 56 -
trunk/wifidog/src/firewall.c
r17 r18 30 30 extern s_config config; 31 31 32 t_node list; 33 t_node *firstnode; 34 t_node *curnode; 35 32 36 int 33 37 fw_allow(char *ip, char *mac, int profile) 34 38 { 35 char buf[MAX_BUF]; 36 char *command[] = {"./fw.access", "allow", ip, mac, buf, NULL}; 37 38 sprintf(buf, "%d", profile); 39 char s_profile[16]; 40 char script[MAX_BUF]; 41 struct stat st; 42 char *command[] = {script, "allow", ip, mac, s_profile, NULL}; 43 44 sprintf(s_profile, "%-10d", profile); 45 sprintf(script, "%s/%s/%s", config.fwscripts_path, config.fwtype, SCRIPT_FWACCESS); 46 47 if (-1 == (stat(script, &st))) { 48 debug(D_LOG_ERR, "Could not find %s: %s", script, strerror(errno)); 49 return(1); 50 } 39 51 40 52 return(execute(command)); … … 44 56 fw_deny(char *ip, char *mac, int profile) 45 57 { 46 char buf[MAX_BUF]; 47 char *command[] = {"./fw.access", "deny", ip, mac, buf, NULL}; 48 49 sprintf(buf, "%d", profile); 58 char s_profile[16]; 59 char script[MAX_BUF]; 60 struct stat st; 61 char *command[] = {script, "deny", ip, mac, s_profile, NULL}; 62 63 sprintf(s_profile, "%-10d", profile); 64 sprintf(script, "%s/%s/%s", config.fwscripts_path, config.fwtype, SCRIPT_FWACCESS); 65 66 if (-1 == (stat(script, &st))) { 67 debug(D_LOG_ERR, "Could not find %s: %s", script, strerror(errno)); 68 return(1); 69 } 50 70 51 71 return(execute(command)); … … 104 124 fw_init(void) 105 125 { 106 char port[255]; 107 char *command[] = {"./fw.init", config.gw_interface, config.gw_address, port, config.authserv_hostname, NULL}; 108 109 sprintf(port, "%d", config.gw_port); 110 126 char port[16]; 127 char script[MAX_BUF]; 128 int rc; 129 struct stat st; 130 char *command[] = {script, config.gw_interface, config.gw_address, port, config.authserv_hostname, NULL}; 131 132 sprintf(port, "%-5d", config.gw_port); 133 sprintf(script, "%s/%s/%s", config.fwscripts_path, config.fwtype, SCRIPT_FWINIT); 134 135 if (-1 == (stat(script, &st))) { 136 debug(D_LOG_ERR, "Could not find %s: %s", script, strerror(errno)); 137 debug(D_LOG_ERR, "Exiting..."); 138 exit(1); 139 } 111 140 112 141 debug(D_LOG_INFO, "Setting firewall rules"); 113 142 114 if ( execute(command) != 0) {143 if ((rc = execute(command)) != 0) { 115 144 debug(D_LOG_ERR, "Could not setup firewall, exiting..."); 116 145 exit(1); 117 146 } 118 147 119 return( 0);148 return(rc); 120 149 } 121 150 … … 123 152 fw_destroy(void) 124 153 { 125 char *command[] = {"./fw.destroy", NULL}; 154 char script[MAX_BUF]; 155 struct stat st; 156 char *command[] = {script, NULL}; 157 158 sprintf(script, "%s/%s/%s", config.fwscripts_path, config.fwtype, SCRIPT_FWDESTROY); 159 160 if (-1 == (stat(script, &st))) { 161 debug(D_LOG_ERR, "Could not find %s: %s", script, strerror(errno)); 162 return(1); 163 } 126 164 127 165 debug(D_LOG_INFO, "Flushing firewall rules"); 128 execute(command); 129 130 return(0); 166 167 return(execute(command)); 131 168 } 132 169 … … 138 175 int profile, rc; 139 176 char ip[255], mac[255]; 140 141 if (!(output = popen("./fw.counters", "r"))) { 177 char script[MAX_BUF]; 178 t_node *p1; 179 180 sprintf(script, "%s/%s/%s", config.fwscripts_path, config.fwtype, SCRIPT_FWCOUNTERS); 181 182 if (!(output = popen(script, "r"))) { 142 183 debug(D_LOG_ERR, "popen(): %s", strerror(errno)); 143 } 144 while (!(feof(output)) && output) { 145 rc = fscanf(output, "%ld %s %s %d", &counter, ip, mac, &profile); 146 if (rc == 4 && rc != EOF) { 147 148 /* TODO Update the counter onthe auth server */ 149 /* but to do that we will need to keep track of the */ 150 /* token to associate it with the session */ 151 152 /* TODO If the client is not active for x seconds */ 153 /* timeout the client and destroy token */ 154 debug(D_LOG_DEBUG, "Counter for %s: %ld bytes", ip, counter); 184 } else { 185 while (!(feof(output)) && output) { 186 rc = fscanf(output, "%ld %s %s %d", &counter, ip, mac, &profile); 187 if (rc == 4 && rc != EOF) { 188 189 /* TODO If the client is not active for x seconds 190 * timeout the client and destroy token. 191 * Maybe this should be done on the auth server*/ 192 193 p1 = node_find_by_ip(ip); 194 if (!(p1)) { 195 debug(D_LOG_DEBUG, "Client %s not found in linked list", ip); 196 } else { 197 p1->counter = counter; 198 if ((profile = auth(p1->ip, p1->mac, p1->token, p1->counter)) == -1) { 199 /* User has to be kicked out */ 200 } 201 debug(D_LOG_DEBUG, "Updated client %s counter to %ld bytes", ip, counter); 202 } 203 } 155 204 } 156 } 157 pclose(output); 158 } 159 205 pclose(output); 206 } 207 } 208 209 void 210 node_init(void) 211 { 212 firstnode = curnode = &list; 213 firstnode->next = NULL; 214 } 215 216 t_node * 217 node_add(char *ip, char *mac, char *token, long int counter) 218 { 219 void *ptr; 220 221 ptr = curnode; 222 223 strcpy(curnode->ip, ip); 224 strcpy(curnode->mac, mac); 225 strcpy(curnode->token, token); 226 curnode->counter = 0; 227 228 curnode->next = (t_node *)malloc(sizeof(t_node)); 229 curnode = curnode->next; 230 231 debug(D_LOG_DEBUG, "Added a new node to linked list: IP: %s Token: %s", ip, token); 232 233 return ptr; 234 } 235 236 t_node * 237 node_find_by_ip(char *ip) 238 { 239 t_node *ptr; 240 241 ptr = firstnode; 242 while (NULL != ptr->next) { 243 if (0 == strcmp(ptr->ip, ip)) 244 return ptr; 245 ptr = ptr->next; 246 } 247 248 return NULL; 249 } 250 251 t_node * 252 node_find_by_token(char *token) 253 { 254 t_node *ptr; 255 256 ptr = firstnode; 257 while (NULL != ptr->next) { 258 if (0 == strcmp(ptr->token, token)) 259 return ptr; 260 ptr = ptr->next; 261 } 262 263 return NULL; 264 } 265 -
trunk/wifidog/src/firewall.h
r9 r18 28 28 #define _FIREWALL_H_ 29 29 30 typedef struct { 31 void *next; 32 char ip[16]; 33 char mac[32]; 34 char token[33]; 35 long int counter; 36 } t_node; 37 30 38 int fw_init(void); 31 39 int fw_destroy(void); … … 36 44 char *arp_get(char *req_ip); 37 45 46 void node_init(void); 47 t_node *node_add(char *ip, char *mac, char *token, long int counter); 48 t_node *node_find_by_ip(char *ip); 49 t_node *node_find_by_token(char *token); 50 38 51 #endif /* _FIREWALL_H_ */ -
trunk/wifidog/src/gateway.c
r17 r18 42 42 int fdmax, i, cnt_last_check; 43 43 44 /* Initialize the linked list */ 45 node_init(); 46 44 47 FD_ZERO(&master); 45 48 FD_ZERO(&read_fds); … … 110 113 111 114 fw_init(); 112 tv.tv_sec = config.checkinterval;113 115 last_checked = time(NULL); 114 116 115 117 while(1) { 118 tv.tv_sec = config.checkinterval; 119 tv.tv_usec = 0; 116 120 read_fds = master; 117 121 if (select(fdmax + 1, &read_fds, NULL, NULL, &tv) == -1) { -
trunk/wifidog/src/http.c
r17 r18 95 95 if ((rc = fw_allow(ip, mac, profile)) == 0) { 96 96 http_body(body, "You %s at %s, have been granted profile %d!", ip, mac, profile); 97 98 /* Add client's IP and token into a linked list so we can keep 99 * track of it on the auth server, only if he's not there already */ 100 if (!node_find_by_ip(ip)) { 101 node_add(ip, mac, token, 0); 102 } 97 103 } else { 98 104 http_body(body, "Authentication was succesful, but the firewall could not be modified, I got return code %d, please contact the systems administrators"); … … 213 219 memset(&(their_addr.sin_zero), '\0', 8); 214 220 221 debug(D_LOG_DEBUG, "Connecting to auth server %s on port %d", config.authserv_hostname, config.authserv_port); 222 215 223 if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1) { 216 224 debug(D_LOG_ERR, "connect(): %s", strerror(errno)); … … 218 226 } 219 227 220 sprintf(buf, "GET %s?ip=%s&mac=%s&token=%s&stats=%ld \n\n", config.authserv_path, ip, mac, token, stats);228 sprintf(buf, "GET %s?ip=%s&mac=%s&token=%s&stats=%ld HTTP/1.1\nHost: %s\n\n", config.authserv_path, ip, mac, token, stats, config.authserv_hostname); 221 229 sock_send(sockfd, buf); 230 231 debug(D_LOG_DEBUG, "Sending HTTP request:\n#####\n%s\n#####", buf); 222 232 223 233 if ((numbytes = recv(sockfd, buf, MAX_BUF - 1, 0)) == -1) { … … 232 242 if ((p1 = strstr(buf, "Profile: "))) { 233 243 if (sscanf(p1, "Profile: %d", &profile) == 1) { 244 debug(D_LOG_DEBUG, "Auth server returned profile %d", profile); 234 245 return(profile); 235 246 } else { 247 debug(D_LOG_DEBUG, "Auth server did not return expected information"); 236 248 return(-1); 237 249 }
