| 1 | /********************************************************************\ |
|---|
| 2 | * This program is free software; you can redistribute it and/or * |
|---|
| 3 | * modify it under the terms of the GNU General Public License as * |
|---|
| 4 | * published by the Free Software Foundation; either version 2 of * |
|---|
| 5 | * the License, or (at your option) any later version. * |
|---|
| 6 | * * |
|---|
| 7 | * This program is distributed in the hope that it will be useful, * |
|---|
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
|---|
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
|---|
| 10 | * GNU General Public License for more details. * |
|---|
| 11 | * * |
|---|
| 12 | * You should have received a copy of the GNU General Public License* |
|---|
| 13 | * along with this program; if not, contact: * |
|---|
| 14 | * * |
|---|
| 15 | * Free Software Foundation Voice: +1-617-542-5942 * |
|---|
| 16 | * 59 Temple Place - Suite 330 Fax: +1-617-542-2652 * |
|---|
| 17 | * Boston, MA 02111-1307, USA gnu@gnu.org * |
|---|
| 18 | * * |
|---|
| 19 | \********************************************************************/ |
|---|
| 20 | |
|---|
| 21 | /* |
|---|
| 22 | * $Header$ |
|---|
| 23 | */ |
|---|
| 24 | /** |
|---|
| 25 | @file util.c |
|---|
| 26 | @brief Misc utility functions |
|---|
| 27 | @author Copyright (C) 2004 Philippe April <papril777@yahoo.com> |
|---|
| 28 | */ |
|---|
| 29 | |
|---|
| 30 | #define _GNU_SOURCE |
|---|
| 31 | |
|---|
| 32 | #include <stdio.h> |
|---|
| 33 | #include <stdlib.h> |
|---|
| 34 | #include <syslog.h> |
|---|
| 35 | #include <errno.h> |
|---|
| 36 | #include <pthread.h> |
|---|
| 37 | #include <sys/wait.h> |
|---|
| 38 | #include <sys/types.h> |
|---|
| 39 | #include <sys/unistd.h> |
|---|
| 40 | #include <netinet/in.h> |
|---|
| 41 | #include <sys/ioctl.h> |
|---|
| 42 | #include <net/if.h> |
|---|
| 43 | |
|---|
| 44 | #include <string.h> |
|---|
| 45 | #include <pthread.h> |
|---|
| 46 | #include <netdb.h> |
|---|
| 47 | |
|---|
| 48 | #include "safe.h" |
|---|
| 49 | #include "util.h" |
|---|
| 50 | #include "conf.h" |
|---|
| 51 | #include "debug.h" |
|---|
| 52 | |
|---|
| 53 | static pthread_mutex_t ghbn_mutex = PTHREAD_MUTEX_INITIALIZER; |
|---|
| 54 | |
|---|
| 55 | /* XXX Do these need to be locked ? */ |
|---|
| 56 | static time_t last_online_time = 0; |
|---|
| 57 | static time_t last_offline_time = 0; |
|---|
| 58 | |
|---|
| 59 | /** Fork a child and execute a shell command, the parent |
|---|
| 60 | * process waits for the child to return and returns the child's exit() |
|---|
| 61 | * value. |
|---|
| 62 | * @return Return code of the command |
|---|
| 63 | */ |
|---|
| 64 | int |
|---|
| 65 | execute(char *cmd_line, int quiet) |
|---|
| 66 | { |
|---|
| 67 | int pid, |
|---|
| 68 | status, |
|---|
| 69 | rc; |
|---|
| 70 | |
|---|
| 71 | const char *new_argv[4]; |
|---|
| 72 | new_argv[0] = "/bin/sh"; |
|---|
| 73 | new_argv[1] = "-c"; |
|---|
| 74 | new_argv[2] = cmd_line; |
|---|
| 75 | new_argv[3] = NULL; |
|---|
| 76 | |
|---|
| 77 | if ((pid = fork()) < 0) { /* fork a child process */ |
|---|
| 78 | debug(LOG_ERR, "fork(): %s", strerror(errno)); |
|---|
| 79 | exit(1); |
|---|
| 80 | } else if (pid == 0) { /* for the child process: */ |
|---|
| 81 | /* We don't want to see any errors if quiet flag is on */ |
|---|
| 82 | if (quiet) close(2); |
|---|
| 83 | if (execvp("/bin/sh", (char *const *)new_argv) < 0) { /* execute the command */ |
|---|
| 84 | debug(LOG_ERR, "execvp(): %s", strerror(errno)); |
|---|
| 85 | exit(1); |
|---|
| 86 | } |
|---|
| 87 | } else { /* for the parent: */ |
|---|
| 88 | do { |
|---|
| 89 | rc = wait(&status); |
|---|
| 90 | } while (rc != pid && rc != -1); /* wait for completion */ |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | return (WEXITSTATUS(status)); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | struct in_addr * |
|---|
| 97 | wd_gethostbyname(const char *name) |
|---|
| 98 | { |
|---|
| 99 | struct hostent *he; |
|---|
| 100 | struct in_addr *h_addr, *in_addr_temp; |
|---|
| 101 | |
|---|
| 102 | /* XXX Calling function is reponsible for free() */ |
|---|
| 103 | |
|---|
| 104 | h_addr = safe_malloc(sizeof(struct in_addr)); |
|---|
| 105 | |
|---|
| 106 | LOCK_GHBN(); |
|---|
| 107 | |
|---|
| 108 | he = gethostbyname(name); |
|---|
| 109 | |
|---|
| 110 | if (he == NULL) { |
|---|
| 111 | free(h_addr); |
|---|
| 112 | mark_offline(); |
|---|
| 113 | UNLOCK_GHBN(); |
|---|
| 114 | return NULL; |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | mark_online(); |
|---|
| 118 | |
|---|
| 119 | in_addr_temp = (struct in_addr *)he->h_addr_list[0]; |
|---|
| 120 | h_addr->s_addr = in_addr_temp->s_addr; |
|---|
| 121 | |
|---|
| 122 | UNLOCK_GHBN(); |
|---|
| 123 | |
|---|
| 124 | return h_addr; |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | char *get_iface_ip(char *ifname) { |
|---|
| 128 | struct ifreq if_data; |
|---|
| 129 | struct in_addr in; |
|---|
| 130 | char *ip_str; |
|---|
| 131 | int sockd; |
|---|
| 132 | u_int32_t ip; |
|---|
| 133 | |
|---|
| 134 | /* Create a socket */ |
|---|
| 135 | if ((sockd = socket (AF_INET, SOCK_PACKET, htons(0x8086))) < 0) { |
|---|
| 136 | debug(LOG_ERR, "socket(): %s", strerror(errno)); |
|---|
| 137 | return NULL; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | /* Get IP of internal interface */ |
|---|
| 141 | strcpy (if_data.ifr_name, ifname); |
|---|
| 142 | |
|---|
| 143 | /* Get the IP address */ |
|---|
| 144 | if (ioctl (sockd, SIOCGIFADDR, &if_data) < 0) { |
|---|
| 145 | debug(LOG_ERR, "ioctl(): SIOCGIFADDR %s", strerror(errno)); |
|---|
| 146 | return NULL; |
|---|
| 147 | } |
|---|
| 148 | memcpy ((void *) &ip, (void *) &if_data.ifr_addr.sa_data + 2, 4); |
|---|
| 149 | in.s_addr = ip; |
|---|
| 150 | |
|---|
| 151 | ip_str = (char *)inet_ntoa(in); |
|---|
| 152 | return safe_strdup(ip_str); |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | void mark_online() { |
|---|
| 156 | time(&last_online_time); |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | void mark_offline() { |
|---|
| 160 | time(&last_offline_time); |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | int is_online() { |
|---|
| 164 | if (last_online_time == 0 || (last_offline_time - last_online_time) >= (config_get_config()->checkinterval * 2) ) { |
|---|
| 165 | /* We're probably offline */ |
|---|
| 166 | return (0); |
|---|
| 167 | } |
|---|
| 168 | else { |
|---|
| 169 | /* We're probably online */ |
|---|
| 170 | return (1); |
|---|
| 171 | } |
|---|
| 172 | } |
|---|
| 173 | |
|---|