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 1162 2007-01-06 23:51:02Z 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 int icmp_fd = 0;
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                                 fw_deny(p1->ip, p1->mac, p1->fw_connection_state);
00299                                 p1->fw_connection_state = FW_MARK_KNOWN;
00300                                 p1->counters.incoming = p1->counters.outgoing = 0;
00301                                 fw_allow(p1->ip, p1->mac, p1->fw_connection_state);
00302                             }
00303                             break;
00304 
00305                         case AUTH_VALIDATION:
00306                             /*
00307                              * Do nothing, user
00308                              * is in validation
00309                              * period
00310                              */
00311                             debug(LOG_INFO, "%s - User in validation period", p1->ip);
00312                             break;
00313 
00314                               case AUTH_ERROR:
00315                                     debug(LOG_WARNING, "Error communicating with auth server - leaving %s as-is for now", p1->ip);
00316                                     break;
00317 
00318                         default:
00319                             debug(LOG_DEBUG, "I do not know about authentication code %d", authresponse.authcode);
00320                             break;
00321                     }
00322                 }
00323             }
00324         }
00325 
00326         free(token);
00327         free(ip);
00328         free(mac);
00329     }
00330     UNLOCK_CLIENT_LIST();
00331 }
00332 
00333 void icmp_ping(char *host) {
00334   struct sockaddr_in saddr;
00335 #ifdef __linux__
00336   struct { 
00337     struct ip ip;
00338     struct icmp icmp;
00339   } packet;
00340 #endif
00341   unsigned int i, j;
00342   int opt = 2000;
00343   unsigned short id = rand16();
00344 
00345   saddr.sin_family = AF_INET;
00346   saddr.sin_port = 0;
00347   inet_aton(host, &saddr.sin_addr);
00348 #ifdef HAVE_SOCKADDR_SA_LEN
00349   saddr.sin_len = sizeof(struct sockaddr_in);
00350 #endif
00351 
00352   memset(&(saddr.sin_zero), '\0', sizeof(saddr.sin_zero));
00353 
00354 #ifdef __linux__
00355   memset(&packet.icmp, 0, sizeof(packet.icmp));
00356   packet.icmp.icmp_type = ICMP_ECHO;
00357   packet.icmp.icmp_id = id;
00358   for (j = 0, i = 0; i < sizeof(struct icmp) / 2; i++)
00359     j += ((unsigned short *)&packet.icmp)[i];
00360   while (j>>16)
00361     j = (j & 0xffff) + (j >> 16);  
00362   packet.icmp.icmp_cksum = (j == 0xffff) ? j : ~j;
00363 
00364   if (setsockopt(icmp_fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt)) == -1) {
00365       debug(LOG_ERR, "setsockopt(): %s", strerror(errno));
00366   }
00367   if (sendto(icmp_fd, (char *)&packet.icmp, sizeof(struct icmp), 0, (struct sockaddr *)&saddr, sizeof(saddr)) == -1) {
00368       debug(LOG_ERR, "sendto(): %s", strerror(errno));
00369   }
00370   opt = 1;
00371   if (setsockopt(icmp_fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt)) == -1) {
00372       debug(LOG_ERR, "setsockopt(): %s", strerror(errno));
00373   }
00374 #endif
00375 
00376   return;
00377 }
00378 
00379 unsigned short rand16(void) {
00380   static int been_seeded = 0;
00381 
00382   if (!been_seeded) {
00383     int fd, n = 0;
00384     unsigned int c = 0, seed = 0;
00385     char sbuf[sizeof(seed)];
00386     char *s;
00387     struct timeval now;
00388 
00389     /* not a very good seed but what the heck, it needs to be quickly acquired */
00390     gettimeofday(&now, NULL);
00391     seed = now.tv_sec ^ now.tv_usec ^ (getpid() << 16);
00392 
00393     srand(seed);
00394     been_seeded = 1;
00395     }
00396 
00397     /* Some rand() implementations have less randomness in low bits
00398      * than in high bits, so we only pay attention to the high ones.
00399      * But most implementations don't touch the high bit, so we 
00400      * ignore that one.
00401      **/
00402       return( (unsigned short) (rand() >> 15) );
00403 }

Generated on Sat Jan 6 18:51:44 2007 for WifiDog by  doxygen 1.5.1