00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
00064 extern time_t started_time;
00065
00066
00067 extern pthread_mutex_t client_list_mutex;
00068 extern pthread_mutex_t config_mutex;
00069
00070
00071 extern pid_t restart_orig_pid;
00072
00073
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) {
00101
00102 if (quiet) close(2);
00103 if (execvp("/bin/sh", (char *const *)new_argv) < 0) {
00104 debug(LOG_ERR, "execvp(): %s", strerror(errno));
00105 exit(1);
00106 }
00107 }
00108 else {
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
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
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
00165 strcpy (if_data.ifr_name, ifname);
00166
00167
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 close(sockd);
00177 return safe_strdup(ip_str);
00178 #else
00179 return safe_strdup("0.0.0.0");
00180 #endif
00181 }
00182
00183 char *get_iface_mac (char *ifname) {
00184 #ifdef __linux__
00185 int r, s;
00186 struct ifreq ifr;
00187 char *hwaddr, mac[13];
00188
00189 strcpy(ifr.ifr_name, ifname);
00190
00191 s = socket(PF_INET, SOCK_DGRAM, 0);
00192 if (-1 == s) {
00193 debug(LOG_ERR, "get_iface_mac socket: %s", strerror(errno));
00194 return NULL;
00195 }
00196
00197 r = ioctl(s, SIOCGIFHWADDR, &ifr);
00198 if (r == -1) {
00199 debug(LOG_ERR, "get_iface_mac ioctl(SIOCGIFHWADDR): %s", strerror(errno));
00200 close(s);
00201 return NULL;
00202 }
00203
00204 hwaddr = ifr.ifr_hwaddr.sa_data;
00205 snprintf(mac, 13, "%02X%02X%02X%02X%02X%02X",
00206 hwaddr[0] & 0xFF,
00207 hwaddr[1] & 0xFF,
00208 hwaddr[2] & 0xFF,
00209 hwaddr[3] & 0xFF,
00210 hwaddr[4] & 0xFF,
00211 hwaddr[5] & 0xFF
00212 );
00213
00214 close(s);
00215 return safe_strdup(mac);
00216 #else
00217 return NULL;
00218 #endif
00219 }
00220
00221 char *get_ext_iface (void) {
00222 #ifdef __linux__
00223 FILE *input;
00224 char *device, *gw;
00225 int i;
00226 int keep_detecting = 1;
00227 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
00228 pthread_mutex_t cond_mutex = PTHREAD_MUTEX_INITIALIZER;
00229 struct timespec timeout;
00230 device = (char *)malloc(16);
00231 gw = (char *)malloc(16);
00232 debug(LOG_DEBUG, "get_ext_iface(): Autodectecting the external interface from routing table");
00233 while(keep_detecting) {
00234 input = fopen("/proc/net/route", "r");
00235 while (!feof(input)) {
00236 fscanf(input, "%s %s %*s %*s %*s %*s %*s %*s %*s %*s %*s\n", device, gw);
00237 if (strcmp(gw, "00000000") == 0) {
00238 free(gw);
00239 debug(LOG_INFO, "get_ext_iface(): Detected %s as the default interface after try %d", device, i);
00240 return device;
00241 }
00242 }
00243 fclose(input);
00244 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);
00245
00246 timeout.tv_sec = time(NULL) + EXT_INTERFACE_DETECT_RETRY_INTERVAL;
00247 timeout.tv_nsec = 0;
00248
00249 pthread_mutex_lock(&cond_mutex);
00250
00251 pthread_cond_timedwait(&cond, &cond_mutex, &timeout);
00252
00253 pthread_mutex_unlock(&cond_mutex);
00254
00255 if (NUM_EXT_INTERFACE_DETECT_RETRY != 0 && i>=NUM_EXT_INTERFACE_DETECT_RETRY) {
00256 keep_detecting = 0;
00257 }
00258 }
00259 debug(LOG_ERR, "get_ext_iface(): Failed to detect the external interface after %d tries, aborting", NUM_EXT_INTERFACE_DETECT_RETRY);
00260 exit(1);
00261 free(device);
00262 free(gw);
00263 #endif
00264 return NULL;
00265 }
00266
00267 void mark_online() {
00268 int before;
00269 int after;
00270
00271 before = is_online();
00272 time(&last_online_time);
00273 after = is_online();
00274
00275 if (before != after) {
00276 debug(LOG_INFO, "ONLINE status became %s", (after ? "ON" : "OFF"));
00277 }
00278
00279 }
00280
00281 void mark_offline() {
00282 int before;
00283 int after;
00284
00285 before = is_online();
00286 time(&last_offline_time);
00287 after = is_online();
00288
00289 if (before != after) {
00290 debug(LOG_INFO, "ONLINE status became %s", (after ? "ON" : "OFF"));
00291 }
00292
00293
00294 mark_auth_offline();
00295
00296 }
00297
00298 int is_online() {
00299 if (last_online_time == 0 || (last_offline_time - last_online_time) >= (config_get_config()->checkinterval * 2) ) {
00300
00301 return (0);
00302 }
00303 else {
00304
00305 return (1);
00306 }
00307 }
00308
00309 void mark_auth_online() {
00310 int before;
00311 int after;
00312
00313 before = is_auth_online();
00314 time(&last_auth_online_time);
00315 after = is_auth_online();
00316
00317 if (before != after) {
00318 debug(LOG_INFO, "AUTH_ONLINE status became %s", (after ? "ON" : "OFF"));
00319 }
00320
00321
00322 mark_online();
00323
00324 }
00325
00326 void mark_auth_offline() {
00327 int before;
00328 int after;
00329
00330 before = is_auth_online();
00331 time(&last_auth_offline_time);
00332 after = is_auth_online();
00333
00334 if (before != after) {
00335 debug(LOG_INFO, "AUTH_ONLINE status became %s", (after ? "ON" : "OFF"));
00336 }
00337
00338 }
00339
00340 int is_auth_online() {
00341 if (!is_online()) {
00342
00343 return (0);
00344 }
00345 else if (last_auth_online_time == 0 || (last_auth_offline_time - last_auth_online_time) >= (config_get_config()->checkinterval * 2) ) {
00346
00347 return (0);
00348 }
00349 else {
00350
00351 return (1);
00352 }
00353 }
00354
00355
00356
00357
00358 char * get_status_text() {
00359 char buffer[STATUS_BUF_SIZ];
00360 ssize_t len;
00361 s_config *config;
00362 t_auth_serv *auth_server;
00363 t_client *first;
00364 int count;
00365 unsigned long int uptime = 0;
00366 unsigned int days = 0, hours = 0, minutes = 0, seconds = 0;
00367 t_trusted_mac *p;
00368
00369 len = 0;
00370 snprintf(buffer, (sizeof(buffer) - len), "WiFiDog status\n\n");
00371 len = strlen(buffer);
00372
00373 uptime = time(NULL) - started_time;
00374 days = uptime / (24 * 60 * 60);
00375 uptime -= days * (24 * 60 * 60);
00376 hours = uptime / (60 * 60);
00377 uptime -= hours * (60 * 60);
00378 minutes = uptime / 60;
00379 uptime -= minutes * 60;
00380 seconds = uptime;
00381
00382 snprintf((buffer + len), (sizeof(buffer) - len), "Version: " VERSION "\n");
00383 len = strlen(buffer);
00384
00385 snprintf((buffer + len), (sizeof(buffer) - len), "Uptime: %ud %uh %um %us\n", days, hours, minutes, seconds);
00386 len = strlen(buffer);
00387
00388 snprintf((buffer + len), (sizeof(buffer) - len), "Has been restarted: ");
00389 len = strlen(buffer);
00390 if (restart_orig_pid) {
00391 snprintf((buffer + len), (sizeof(buffer) - len), "yes (from PID %d)\n", restart_orig_pid);
00392 len = strlen(buffer);
00393 }
00394 else {
00395 snprintf((buffer + len), (sizeof(buffer) - len), "no\n");
00396 len = strlen(buffer);
00397 }
00398
00399 snprintf((buffer + len), (sizeof(buffer) - len), "Internet Connectivity: %s\n", (is_online() ? "yes" : "no"));
00400 len = strlen(buffer);
00401
00402 snprintf((buffer + len), (sizeof(buffer) - len), "Auth server reachable: %s\n", (is_auth_online() ? "yes" : "no"));
00403 len = strlen(buffer);
00404
00405 snprintf((buffer + len), (sizeof(buffer) - len), "Clients served this session: %lu\n\n", served_this_session);
00406 len = strlen(buffer);
00407
00408 LOCK_CLIENT_LIST();
00409
00410 first = client_get_first_client();
00411
00412 if (first == NULL) {
00413 count = 0;
00414 } else {
00415 count = 1;
00416 while (first->next != NULL) {
00417 first = first->next;
00418 count++;
00419 }
00420 }
00421
00422 snprintf((buffer + len), (sizeof(buffer) - len), "%d clients "
00423 "connected.\n", count);
00424 len = strlen(buffer);
00425
00426 first = client_get_first_client();
00427
00428 count = 0;
00429 while (first != NULL) {
00430 snprintf((buffer + len), (sizeof(buffer) - len), "\nClient %d\n", count);
00431 len = strlen(buffer);
00432
00433 snprintf((buffer + len), (sizeof(buffer) - len), " IP: %s MAC: %s\n", first->ip, first->mac);
00434 len = strlen(buffer);
00435
00436 snprintf((buffer + len), (sizeof(buffer) - len), " Token: %s\n", first->token);
00437 len = strlen(buffer);
00438
00439 snprintf((buffer + len), (sizeof(buffer) - len), " Downloaded: %llu\n Uploaded: %llu\n" , first->counters.incoming, first->counters.outgoing);
00440 len = strlen(buffer);
00441
00442 count++;
00443 first = first->next;
00444 }
00445
00446 UNLOCK_CLIENT_LIST();
00447
00448 config = config_get_config();
00449
00450 if (config->trustedmaclist != NULL) {
00451 snprintf((buffer + len), (sizeof(buffer) - len), "\nTrusted MAC addresses:\n");
00452 len = strlen(buffer);
00453
00454 for (p = config->trustedmaclist; p != NULL; p = p->next) {
00455 snprintf((buffer + len), (sizeof(buffer) - len), " %s\n", p->mac);
00456 len = strlen(buffer);
00457 }
00458 }
00459
00460 snprintf((buffer + len), (sizeof(buffer) - len), "\nAuthentication servers:\n");
00461 len = strlen(buffer);
00462
00463 LOCK_CONFIG();
00464
00465 for (auth_server = config->auth_servers; auth_server != NULL; auth_server = auth_server->next) {
00466 snprintf((buffer + len), (sizeof(buffer) - len), " Host: %s (%s)\n", auth_server->authserv_hostname, auth_server->last_ip);
00467 len = strlen(buffer);
00468 }
00469
00470 UNLOCK_CONFIG();
00471
00472 return safe_strdup(buffer);
00473 }