firewall.c

00001 /********************************************************************\
00002  * This program is free software; you can redistribute it and/or    *
00003  * modify it under the terms of the GNU General Public License as   *
00004  * published by the Free Software Foundation; either version 2 of   *
00005  * the License, or (at your option) any later version.              *
00006  *                                                                  *
00007  * This program is distributed in the hope that it will be useful,  *
00008  * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
00009  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
00010  * GNU General Public License for more details.                     *
00011  *                                                                  *
00012  * You should have received a copy of the GNU General Public License*
00013  * along with this program; if not, contact:                        *
00014  *                                                                  *
00015  * Free Software Foundation           Voice:  +1-617-542-5942       *
00016  * 59 Temple Place - Suite 330        Fax:    +1-617-542-2652       *
00017  * Boston, MA  02111-1307,  USA       gnu@gnu.org                   *
00018  *                                                                  *
00019  \********************************************************************/
00020 
00021 /*
00022  * $Id: firewall.c 1241 2007-06-24 04:13:13Z benoitg $
00023  */
00031 #define _GNU_SOURCE
00032 
00033 #include <stdio.h>
00034 #include <stdlib.h>
00035 #include <syslog.h>
00036 #include <errno.h>
00037 #include <pthread.h>
00038 #include <sys/wait.h>
00039 #include <sys/types.h>
00040 #include <sys/unistd.h>
00041 
00042 #include <string.h>
00043 
00044 #include <sys/socket.h>
00045 #include <netinet/in.h>
00046 #include <arpa/inet.h>
00047 #include <unistd.h>
00048 #include <sys/uio.h>
00049 #include <fcntl.h>
00050 #include <netdb.h>
00051 #include <sys/time.h>
00052 
00053 #ifdef __linux__
00054 #include <net/ethernet.h>
00055 #include <netinet/ip.h>
00056 #include <netinet/ip_icmp.h>
00057 #include <netpacket/packet.h>
00058 #endif
00059 
00060 #include "httpd.h"
00061 #include "safe.h"
00062 #include "debug.h"
00063 #include "conf.h"
00064 #include "firewall.h"
00065 #include "fw_iptables.h"
00066 #include "auth.h"
00067 #include "centralserver.h"
00068 #include "client_list.h"
00069 
00070 extern pthread_mutex_t client_list_mutex;
00071 
00072 /* from commandline.c */
00073 extern pid_t restart_orig_pid;
00074 
00075 
00076 
00085 int
00086 fw_allow(char *ip, char *mac, int fw_connection_state)
00087 {
00088     debug(LOG_DEBUG, "Allowing %s %s with fw_connection_state %d", ip, mac, fw_connection_state);
00089 
00090     return iptables_fw_access(FW_ACCESS_ALLOW, ip, mac, fw_connection_state);
00091 }
00092 
00100 int
00101 fw_deny(char *ip, char *mac, int fw_connection_state)
00102 {
00103     debug(LOG_DEBUG, "Denying %s %s with fw_connection_state %d", ip, mac, fw_connection_state);
00104 
00105     return iptables_fw_access(FW_ACCESS_DENY, ip, mac, fw_connection_state);
00106 }
00107 
00114 char           *
00115 arp_get(char *req_ip)
00116 {
00117     FILE           *proc;
00118          char ip[16];
00119          char mac[18];
00120          char * reply = NULL;
00121 
00122     if (!(proc = fopen("/proc/net/arp", "r"))) {
00123         return NULL;
00124     }
00125 
00126     /* Skip first line */
00127          while (!feof(proc) && fgetc(proc) != '\n');
00128 
00129          /* Find ip, copy mac in reply */
00130          reply = NULL;
00131     while (!feof(proc) && (fscanf(proc, " %15[0-9.] %*s %*s %17[A-F0-9:] %*s %*s", ip, mac) == 2)) {
00132                   if (strcmp(ip, req_ip) == 0) {
00133                                 reply = safe_strdup(mac);
00134                                 break;
00135                   }
00136     }
00137 
00138     fclose(proc);
00139 
00140     return reply;
00141 }
00142 
00145 int
00146 fw_init(void)
00147 {
00148     int flags, oneopt = 1, zeroopt = 0;
00149          int result = 0;
00150          t_client * client = NULL;
00151 
00152     debug(LOG_INFO, "Creating ICMP socket");
00153     if ((icmp_fd = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP)) == -1 ||
00154             (flags = fcntl(icmp_fd, F_GETFL, 0)) == -1 ||
00155              fcntl(icmp_fd, F_SETFL, flags | O_NONBLOCK) == -1 ||
00156              setsockopt(icmp_fd, SOL_SOCKET, SO_RCVBUF, &oneopt, sizeof(oneopt)) ||
00157              setsockopt(icmp_fd, SOL_SOCKET, SO_DONTROUTE, &zeroopt, sizeof(zeroopt)) == -1) {
00158         debug(LOG_ERR, "Cannot create ICMP raw socket.");
00159         return;
00160     }
00161 
00162     debug(LOG_INFO, "Initializing Firewall");
00163     result = iptables_fw_init();
00164 
00165          if (restart_orig_pid) {
00166                  debug(LOG_INFO, "Restoring firewall rules for clients inherited from parent");
00167                  LOCK_CLIENT_LIST();
00168                  client = client_get_first_client();
00169                  while (client) {
00170                          fw_allow(client->ip, client->mac, client->fw_connection_state);
00171                          client = client->next;
00172                  }
00173                  UNLOCK_CLIENT_LIST();
00174          }
00175 
00176          return result;
00177 }
00178 
00181 void
00182 fw_clear_authservers(void)
00183 {
00184         debug(LOG_INFO, "Clearing the authservers list");
00185         iptables_fw_clear_authservers();
00186 }
00187 
00190 void
00191 fw_set_authservers(void)
00192 {
00193         debug(LOG_INFO, "Setting the authservers list");
00194         iptables_fw_set_authservers();
00195 }
00196 
00201 int
00202 fw_destroy(void)
00203 {
00204     if (icmp_fd != 0) {
00205         debug(LOG_INFO, "Closing ICMP socket");
00206         close(icmp_fd);
00207     }
00208 
00209     debug(LOG_INFO, "Removing Firewall rules");
00210     return iptables_fw_destroy();
00211 }
00212 
00216 void
00217 fw_sync_with_authserver(void)
00218 {
00219     t_authresponse  authresponse;
00220     char            *token, *ip, *mac;
00221     t_client        *p1, *p2;
00222     unsigned long long      incoming, outgoing;
00223     s_config *config = config_get_config();
00224 
00225     if (-1 == iptables_fw_counters_update()) {
00226         debug(LOG_ERR, "Could not get counters from firewall!");
00227         return;
00228     }
00229 
00230     LOCK_CLIENT_LIST();
00231 
00232     for (p1 = p2 = client_get_first_client(); NULL != p1; p1 = p2) {
00233         p2 = p1->next;
00234 
00235         ip = safe_strdup(p1->ip);
00236         token = safe_strdup(p1->token);
00237         mac = safe_strdup(p1->mac);
00238             outgoing = p1->counters.outgoing;
00239             incoming = p1->counters.incoming;
00240 
00241             UNLOCK_CLIENT_LIST();
00242         /* Ping the client, if he responds it'll keep activity on the link.
00243          * However, if the firewall blocks it, it will not help.  The suggested
00244          * way to deal witht his is to keep the DHCP lease time extremely 
00245          * short:  Shorter than config->checkinterval * config->clienttimeout */
00246         icmp_ping(ip);
00247         /* Update the counters on the remote server only if we have an auth server */
00248         if (config->auth_servers != NULL) {
00249             auth_server_request(&authresponse, REQUEST_TYPE_COUNTERS, ip, mac, token, incoming, outgoing);
00250         }
00251             LOCK_CLIENT_LIST();
00252         
00253         if (!(p1 = client_list_find(ip, mac))) {
00254             debug(LOG_ERR, "Node %s was freed while being re-validated!", ip);
00255         } else {
00256             if (p1->counters.last_updated +
00257                                 (config->checkinterval * config->clienttimeout)
00258                                 <= time(NULL)) {
00259                 /* Timing out user */
00260                 debug(LOG_INFO, "%s - Inactive for %ld seconds, removing client and denying in firewall",
00261                         p1->ip, config->checkinterval * config->clienttimeout);
00262                 fw_deny(p1->ip, p1->mac, p1->fw_connection_state);
00263                 client_list_delete(p1);
00264 
00265                 /* Advertise the logout if we have an auth server */
00266                 if (config->auth_servers != NULL) {
00267                                         UNLOCK_CLIENT_LIST();
00268                                         auth_server_request(&authresponse, REQUEST_TYPE_LOGOUT, ip, mac, token, 0, 0);
00269                                         LOCK_CLIENT_LIST();
00270                 }
00271             } else {
00272                 /*
00273                  * This handles any change in
00274                  * the status this allows us
00275                  * to change the status of a
00276                  * user while he's connected
00277                  *
00278                  * Only run if we have an auth server
00279                  * configured!
00280                  */
00281                 if (config->auth_servers != NULL) {
00282                     switch (authresponse.authcode) {
00283                         case AUTH_DENIED:
00284                             debug(LOG_NOTICE, "%s - Denied. Removing client and firewall rules", p1->ip);
00285                             fw_deny(p1->ip, p1->mac, p1->fw_connection_state);
00286                             client_list_delete(p1);
00287                             break;
00288 
00289                         case AUTH_VALIDATION_FAILED:
00290                             debug(LOG_NOTICE, "%s - Validation timeout, now denied. Removing client and firewall rules", p1->ip);
00291                             fw_deny(p1->ip, p1->mac, p1->fw_connection_state);
00292                             client_list_delete(p1);
00293                             break;
00294 
00295                         case AUTH_ALLOWED:
00296                             if (p1->fw_connection_state != FW_MARK_KNOWN) {
00297                                 debug(LOG_INFO, "%s - Access has changed to allowed, refreshing firewall and clearing counters", p1->ip);
00298                                 //WHY did we deny, then allow!?!? benoitg 2007-06-21
00299                                 //fw_deny(p1->ip, p1->mac, p1->fw_connection_state);
00300 
00301                                 if (p1->fw_connection_state != FW_MARK_PROBATION) {
00302      p1->counters.incoming = p1->counters.outgoing = 0;
00303                                 }
00304                                 else {
00305                                         //We don't want to clear counters if the user was in validation, it probably already transmitted data..
00306                                     debug(LOG_INFO, "%s - Skipped clearing counters after all, the user was previously in validation", p1->ip);
00307                                 }
00308                                 p1->fw_connection_state = FW_MARK_KNOWN;
00309                                 fw_allow(p1->ip, p1->mac, p1->fw_connection_state);
00310                             }
00311                             break;
00312 
00313                         case AUTH_VALIDATION:
00314                             /*
00315                              * Do nothing, user
00316                              * is in validation
00317                              * period
00318                              */
00319                             debug(LOG_INFO, "%s - User in validation period", p1->ip);
00320                             break;
00321 
00322                               case AUTH_ERROR:
00323                                     debug(LOG_WARNING, "Error communicating with auth server - leaving %s as-is for now", p1->ip);
00324                                     break;
00325 
00326                         default:
00327                             debug(LOG_ERR, "I do not know about authentication code %d", authresponse.authcode);
00328                             break;
00329                     }
00330                 }
00331             }
00332         }
00333 
00334         free(token);
00335         free(ip);
00336         free(mac);
00337     }
00338     UNLOCK_CLIENT_LIST();
00339 }
00340 
00341 void icmp_ping(char *host) {
00342   struct sockaddr_in saddr;
00343 #ifdef __linux__
00344   struct { 
00345     struct ip ip;
00346     struct icmp icmp;
00347   } packet;
00348 #endif
00349   unsigned int i, j;
00350   int opt = 2000;
00351   unsigned short id = rand16();
00352 
00353   saddr.sin_family = AF_INET;
00354   saddr.sin_port = 0;
00355   inet_aton(host, &saddr.sin_addr);
00356 #ifdef HAVE_SOCKADDR_SA_LEN
00357   saddr.sin_len = sizeof(struct sockaddr_in);
00358 #endif
00359 
00360   memset(&(saddr.sin_zero), '\0', sizeof(saddr.sin_zero));
00361 
00362 #ifdef __linux__
00363   memset(&packet.icmp, 0, sizeof(packet.icmp));
00364   packet.icmp.icmp_type = ICMP_ECHO;
00365   packet.icmp.icmp_id = id;
00366   for (j = 0, i = 0; i < sizeof(struct icmp) / 2; i++)
00367     j += ((unsigned short *)&packet.icmp)[i];
00368   while (j>>16)
00369     j = (j & 0xffff) + (j >> 16);  
00370   packet.icmp.icmp_cksum = (j == 0xffff) ? j : ~j;
00371 
00372   if (setsockopt(icmp_fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt)) == -1) {
00373       debug(LOG_ERR, "setsockopt(): %s", strerror(errno));
00374   }
00375   if (sendto(icmp_fd, (char *)&packet.icmp, sizeof(struct icmp), 0, (struct sockaddr *)&saddr, sizeof(saddr)) == -1) {
00376       debug(LOG_ERR, "sendto(): %s", strerror(errno));
00377   }
00378   opt = 1;
00379   if (setsockopt(icmp_fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt)) == -1) {
00380       debug(LOG_ERR, "setsockopt(): %s", strerror(errno));
00381   }
00382 #endif
00383 
00384   return;
00385 }
00386 
00387 unsigned short rand16(void) {
00388   static int been_seeded = 0;
00389 
00390   if (!been_seeded) {
00391     int fd, n = 0;
00392     unsigned int c = 0, seed = 0;
00393     char sbuf[sizeof(seed)];
00394     char *s;
00395     struct timeval now;
00396 
00397     /* not a very good seed but what the heck, it needs to be quickly acquired */
00398     gettimeofday(&now, NULL);
00399     seed = now.tv_sec ^ now.tv_usec ^ (getpid() << 16);
00400 
00401     srand(seed);
00402     been_seeded = 1;
00403     }
00404 
00405     /* Some rand() implementations have less randomness in low bits
00406      * than in high bits, so we only pay attention to the high ones.
00407      * But most implementations don't touch the high bit, so we 
00408      * ignore that one.
00409      **/
00410       return( (unsigned short) (rand() >> 15) );
00411 }

Generated on Sun Jun 24 00:29:52 2007 for WifiDog by  doxygen 1.5.1