util.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: util.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 #include <netinet/in.h>
00042 #include <sys/ioctl.h>
00043 
00044 #ifdef __linux__
00045 #include <net/if.h>
00046 #endif
00047 
00048 #include <string.h>
00049 #include <pthread.h>
00050 #include <netdb.h>
00051 
00052 #include "common.h"
00053 #include "client_list.h"
00054 #include "safe.h"
00055 #include "util.h"
00056 #include "conf.h"
00057 #include "debug.h"
00058 
00059 #include "../config.h"
00060 
00061 static pthread_mutex_t ghbn_mutex = PTHREAD_MUTEX_INITIALIZER;
00062 
00063 /* Defined in ping_thread.c */
00064 extern time_t started_time;
00065 
00066 /* Defined in clientlist.c */
00067 extern  pthread_mutex_t client_list_mutex;
00068 extern  pthread_mutex_t config_mutex;
00069 
00070 /* Defined in commandline.c */
00071 extern pid_t restart_orig_pid;
00072 
00073 /* XXX Do these need to be locked ? */
00074 static time_t last_online_time = 0;
00075 static time_t last_offline_time = 0;
00076 static time_t last_auth_online_time = 0;
00077 static time_t last_auth_offline_time = 0;
00078 
00079 long served_this_session = 0;
00080 
00086 int
00087 execute(char *cmd_line, int quiet)
00088 {
00089     int pid,
00090         status,
00091         rc;
00092 
00093     const char *new_argv[4];
00094     new_argv[0] = "/bin/sh";
00095     new_argv[1] = "-c";
00096     new_argv[2] = cmd_line;
00097     new_argv[3] = NULL;
00098 
00099          pid = safe_fork();
00100          if (pid == 0) {    /* for the child process:         */
00101         /* We don't want to see any errors if quiet flag is on */
00102         if (quiet) close(2);
00103         if (execvp("/bin/sh", (char *const *)new_argv) < 0) {    /* execute the command  */
00104             debug(LOG_ERR, "execvp(): %s", strerror(errno));
00105             exit(1);
00106         }
00107     }
00108          else {        /* for the parent:      */
00109                 debug(LOG_DEBUG, "Waiting for PID %d to exit", pid);
00110                 rc = waitpid(pid, &status, 0);
00111                 debug(LOG_DEBUG, "Process PID %d exited", rc);
00112     }
00113 
00114     return (WEXITSTATUS(status));
00115 }
00116 
00117 struct in_addr *
00118 wd_gethostbyname(const char *name)
00119 {
00120         struct hostent *he;
00121         struct in_addr *h_addr, *in_addr_temp;
00122 
00123         /* XXX Calling function is reponsible for free() */
00124 
00125         h_addr = safe_malloc(sizeof(struct in_addr));
00126         
00127         LOCK_GHBN();
00128 
00129         he = gethostbyname(name);
00130 
00131         if (he == NULL) {
00132                 free(h_addr);
00133                 UNLOCK_GHBN();
00134                 return NULL;
00135         }
00136 
00137         mark_online();
00138 
00139         in_addr_temp = (struct in_addr *)he->h_addr_list[0];
00140         h_addr->s_addr = in_addr_temp->s_addr;
00141         
00142         UNLOCK_GHBN();
00143 
00144         return h_addr;
00145 }
00146 
00147 char *get_iface_ip(char *ifname) {
00148 #ifdef __linux__
00149     struct ifreq if_data;
00150 #endif
00151     struct in_addr in;
00152     char *ip_str;
00153     int sockd;
00154     u_int32_t ip;
00155 
00156 #ifdef __linux__
00157     
00158     /* Create a socket */
00159     if ((sockd = socket (AF_INET, SOCK_PACKET, htons(0x8086))) < 0) {
00160         debug(LOG_ERR, "socket(): %s", strerror(errno));
00161         return NULL;
00162     }
00163 
00164     /* Get IP of internal interface */
00165     strcpy (if_data.ifr_name, ifname);
00166 
00167     /* Get the IP address */
00168     if (ioctl (sockd, SIOCGIFADDR, &if_data) < 0) {
00169         debug(LOG_ERR, "ioctl(): SIOCGIFADDR %s", strerror(errno));
00170         return NULL;
00171     }
00172     memcpy ((void *) &ip, (void *) &if_data.ifr_addr.sa_data + 2, 4);
00173     in.s_addr = ip;
00174 
00175     ip_str = (char *)inet_ntoa(in);
00176     return safe_strdup(ip_str);
00177 #else
00178     return safe_strdup("0.0.0.0");
00179 #endif
00180 }
00181 
00182 char *get_iface_mac (char *ifname) {
00183 #ifdef __linux__
00184     int r, s;
00185     struct ifreq ifr;
00186     char *hwaddr, mac[13];
00187     
00188     strcpy(ifr.ifr_name, ifname);
00189 
00190     s = socket(PF_INET, SOCK_DGRAM, 0);
00191     if (-1 == s) {
00192        debug(LOG_ERR, "get_iface_mac socket: %s", strerror(errno));
00193        return NULL;
00194     }
00195 
00196     r = ioctl(s, SIOCGIFHWADDR, &ifr);
00197     if (r == -1) {
00198        debug(LOG_ERR, "get_iface_mac ioctl(SIOCGIFHWADDR): %s", strerror(errno));
00199        close(s);
00200        return NULL;
00201     }
00202 
00203     hwaddr = ifr.ifr_hwaddr.sa_data;
00204     snprintf(mac, 13, "%02X%02X%02X%02X%02X%02X", 
00205        hwaddr[0] & 0xFF,
00206        hwaddr[1] & 0xFF,
00207        hwaddr[2] & 0xFF,
00208        hwaddr[3] & 0xFF,
00209        hwaddr[4] & 0xFF,
00210        hwaddr[5] & 0xFF
00211        );
00212        
00213     close(s);
00214     return safe_strdup(mac);
00215 #else
00216     return NULL;
00217 #endif
00218 }
00219 
00220 char *get_ext_iface (void) {
00221 #ifdef __linux__
00222     FILE *input;
00223     char *device, *gw;
00224     int i;
00225     pthread_cond_t              cond = PTHREAD_COND_INITIALIZER;
00226     pthread_mutex_t             cond_mutex = PTHREAD_MUTEX_INITIALIZER;
00227     struct      timespec        timeout;
00228     device = (char *)malloc(16);
00229     gw = (char *)malloc(16);
00230     debug(LOG_DEBUG, "get_ext_iface(): Autodectecting the external interface from routing table");
00231     for (i=1; i<=NUM_EXT_INTERFACE_DETECT_RETRY; i++) {
00232         input = fopen("/proc/net/route", "r");
00233         while (!feof(input)) {
00234             fscanf(input, "%s %s %*s %*s %*s %*s %*s %*s %*s %*s %*s\n", device, gw);
00235             if (strcmp(gw, "00000000") == 0) {
00236                 free(gw);
00237                 debug(LOG_INFO, "get_ext_iface(): Detected %s as the default interface after try %d", device, i);
00238                 return device;
00239             }
00240         }
00241         fclose(input);
00242         debug(LOG_ERR, "get_ext_iface(): Failed to detect the external interface after try %d of %d (maybe the interface is not up yet?)", i, NUM_EXT_INTERFACE_DETECT_RETRY);
00243         /* Sleep for EXT_INTERFACE_DETECT_RETRY_INTERVAL seconds */
00244         timeout.tv_sec = time(NULL) + EXT_INTERFACE_DETECT_RETRY_INTERVAL;
00245         timeout.tv_nsec = 0;
00246         /* Mutex must be locked for pthread_cond_timedwait... */
00247         pthread_mutex_lock(&cond_mutex);        
00248         /* Thread safe "sleep" */
00249         pthread_cond_timedwait(&cond, &cond_mutex, &timeout);
00250         /* No longer needs to be locked */
00251         pthread_mutex_unlock(&cond_mutex);
00252     }
00253     debug(LOG_ERR, "get_ext_iface(): Failed to detect the external interface after %d tries, aborting", NUM_EXT_INTERFACE_DETECT_RETRY);
00254     exit(1);
00255     free(device);
00256     free(gw);
00257 #endif
00258     return NULL;
00259 }
00260 
00261 void mark_online() {
00262         int before;
00263         int after;
00264 
00265         before = is_online();
00266         time(&last_online_time);
00267         after = is_online();
00268 
00269         if (before != after) {
00270                 debug(LOG_INFO, "ONLINE status became %s", (after ? "ON" : "OFF"));
00271         }
00272 
00273 }
00274 
00275 void mark_offline() {
00276         int before;
00277         int after;
00278 
00279         before = is_online();
00280         time(&last_offline_time);
00281         after = is_online();
00282 
00283         if (before != after) {
00284                 debug(LOG_INFO, "ONLINE status became %s", (after ? "ON" : "OFF"));
00285         }
00286 
00287         /* If we're offline it definately means the auth server is offline */
00288         mark_auth_offline();
00289 
00290 }
00291 
00292 int is_online() {
00293         if (last_online_time == 0 || (last_offline_time - last_online_time) >= (config_get_config()->checkinterval * 2) ) {
00294                 /* We're probably offline */
00295                 return (0);
00296         }
00297         else {
00298                 /* We're probably online */
00299                 return (1);
00300         }
00301 }
00302 
00303 void mark_auth_online() {
00304         int before;
00305         int after;
00306 
00307         before = is_auth_online();
00308         time(&last_auth_online_time);
00309         after = is_auth_online();
00310 
00311         if (before != after) {
00312                 debug(LOG_INFO, "AUTH_ONLINE status became %s", (after ? "ON" : "OFF"));
00313         }
00314 
00315         /* If auth server is online it means we're definately online */
00316         mark_online();
00317 
00318 }
00319 
00320 void mark_auth_offline() {
00321         int before;
00322         int after;
00323 
00324         before = is_auth_online();
00325         time(&last_auth_offline_time);
00326         after = is_auth_online();
00327 
00328         if (before != after) {
00329                 debug(LOG_INFO, "AUTH_ONLINE status became %s", (after ? "ON" : "OFF"));
00330         }
00331 
00332 }
00333 
00334 int is_auth_online() {
00335         if (!is_online()) {
00336                 /* If we're not online auth is definately not online :) */
00337                 return (0);
00338         }
00339         else if (last_auth_online_time == 0 || (last_auth_offline_time - last_auth_online_time) >= (config_get_config()->checkinterval * 2) ) {
00340                 /* Auth is  probably offline */
00341                 return (0);
00342         }
00343         else {
00344                 /* Auth is probably online */
00345                 return (1);
00346         }
00347 }
00348 
00349 /*
00350  * @return A string containing human-readable status text. MUST BE free()d by caller
00351  */
00352 char * get_status_text() {
00353         char buffer[STATUS_BUF_SIZ];
00354         ssize_t len;
00355         s_config *config;
00356         t_auth_serv *auth_server;
00357         t_client        *first;
00358         int             count;
00359         unsigned long int uptime = 0;
00360         unsigned int days = 0, hours = 0, minutes = 0, seconds = 0;
00361      t_trusted_mac *p;
00362         
00363         len = 0;
00364         snprintf(buffer, (sizeof(buffer) - len), "WiFiDog status\n\n");
00365         len = strlen(buffer);
00366 
00367         uptime = time(NULL) - started_time;
00368         days    = uptime / (24 * 60 * 60);
00369         uptime -= days * (24 * 60 * 60);
00370         hours   = uptime / (60 * 60);
00371         uptime -= hours * (60 * 60);
00372         minutes = uptime / 60;
00373         uptime -= minutes * 60;
00374         seconds = uptime;
00375 
00376         snprintf((buffer + len), (sizeof(buffer) - len), "Version: " VERSION "\n");
00377         len = strlen(buffer);
00378 
00379         snprintf((buffer + len), (sizeof(buffer) - len), "Uptime: %ud %uh %um %us\n", days, hours, minutes, seconds);
00380         len = strlen(buffer);
00381 
00382         snprintf((buffer + len), (sizeof(buffer) - len), "Has been restarted: ");
00383         len = strlen(buffer);
00384         if (restart_orig_pid) {
00385                 snprintf((buffer + len), (sizeof(buffer) - len), "yes (from PID %d)\n", restart_orig_pid);
00386                 len = strlen(buffer);
00387         }
00388         else {
00389                 snprintf((buffer + len), (sizeof(buffer) - len), "no\n");
00390                 len = strlen(buffer);
00391         }
00392         
00393         snprintf((buffer + len), (sizeof(buffer) - len), "Internet Connectivity: %s\n", (is_online() ? "yes" : "no"));
00394         len = strlen(buffer);
00395         
00396         snprintf((buffer + len), (sizeof(buffer) - len), "Auth server reachable: %s\n", (is_auth_online() ? "yes" : "no"));
00397         len = strlen(buffer);
00398 
00399         snprintf((buffer + len), (sizeof(buffer) - len), "Clients served this session: %lu\n\n", served_this_session);
00400         len = strlen(buffer);
00401 
00402         LOCK_CLIENT_LIST();
00403         
00404         first = client_get_first_client();
00405         
00406         if (first == NULL) {
00407                 count = 0;
00408         } else {
00409                 count = 1;
00410                 while (first->next != NULL) {
00411                         first = first->next;
00412                         count++;
00413                 }
00414         }
00415         
00416         snprintf((buffer + len), (sizeof(buffer) - len), "%d clients "
00417                         "connected.\n", count);
00418         len = strlen(buffer);
00419 
00420         first = client_get_first_client();
00421 
00422         count = 0;
00423         while (first != NULL) {
00424                 snprintf((buffer + len), (sizeof(buffer) - len), "\nClient %d\n", count);
00425                 len = strlen(buffer);
00426 
00427                 snprintf((buffer + len), (sizeof(buffer) - len), "  IP: %s MAC: %s\n", first->ip, first->mac);
00428                 len = strlen(buffer);
00429 
00430                 snprintf((buffer + len), (sizeof(buffer) - len), "  Token: %s\n", first->token);
00431                 len = strlen(buffer);
00432 
00433                 snprintf((buffer + len), (sizeof(buffer) - len), "  Downloaded: %llu\n  Uploaded: %llu\n" , first->counters.incoming, first->counters.outgoing);
00434                 len = strlen(buffer);
00435 
00436                 count++;
00437                 first = first->next;
00438         }
00439 
00440         UNLOCK_CLIENT_LIST();
00441 
00442     config = config_get_config();
00443     
00444     if (config->trustedmaclist != NULL) {
00445         snprintf((buffer + len), (sizeof(buffer) - len), "\nTrusted MAC addresses:\n");
00446         len = strlen(buffer);
00447 
00448         for (p = config->trustedmaclist; p != NULL; p = p->next) {
00449             snprintf((buffer + len), (sizeof(buffer) - len), "  %s\n", p->mac);
00450             len = strlen(buffer);
00451         }
00452     }
00453 
00454     snprintf((buffer + len), (sizeof(buffer) - len), "\nAuthentication servers:\n");
00455     len = strlen(buffer);
00456 
00457     LOCK_CONFIG();
00458 
00459     for (auth_server = config->auth_servers; auth_server != NULL; auth_server = auth_server->next) {
00460         snprintf((buffer + len), (sizeof(buffer) - len), "  Host: %s (%s)\n", auth_server->authserv_hostname, auth_server->last_ip);
00461         len = strlen(buffer);
00462     }
00463 
00464     UNLOCK_CONFIG();
00465 
00466         return safe_strdup(buffer);
00467 }

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