/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to plugins.d/mandos-client.c

  • Committer: Teddy Hogeborn
  • Date: 2014-03-09 03:02:43 UTC
  • Revision ID: teddy@recompile.se-20140309030243-0gc6l2yuqbzgyuyt
Use getnameinfo() instead of inet_ntop() in mandos-client.

* plugins.d/mandos-client.c (start_mandos_communication): Use
                                                          getnameinfo()
                                                          instead of
                                                          inet_ntop().

Show diffs side-by-side

added added

removed removed

Lines of Context:
55
55
                                   opendir(), DIR */
56
56
#include <sys/stat.h>           /* open(), S_ISREG */
57
57
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
58
 
                                   inet_pton(), connect() */
 
58
                                   inet_pton(), connect(),
 
59
                                   getnameinfo() */
59
60
#include <fcntl.h>              /* open() */
60
61
#include <dirent.h>             /* opendir(), struct dirent, readdir()
61
62
                                 */
73
74
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
74
75
                                   getuid(), getgid(), seteuid(),
75
76
                                   setgid(), pause(), _exit() */
76
 
#include <arpa/inet.h>          /* inet_pton(), htons, inet_ntop() */
 
77
#include <arpa/inet.h>          /* inet_pton(), htons() */
77
78
#include <iso646.h>             /* not, or, and */
78
79
#include <argp.h>               /* struct argp_option, error_t, struct
79
80
                                   argp_state, struct argp,
91
92
                                   argz_delete(), argz_append(),
92
93
                                   argz_stringify(), argz_add(),
93
94
                                   argz_count() */
 
95
#include <netdb.h>              /* getnameinfo(), NI_NUMERICHOST,
 
96
                                   EAI_SYSTEM, gai_strerror() */
94
97
 
95
98
#ifdef __linux__
96
99
#include <sys/klog.h>           /* klogctl() */
198
201
size_t incbuffer(char **buffer, size_t buffer_length,
199
202
                 size_t buffer_capacity){
200
203
  if(buffer_length + BUFFER_SIZE > buffer_capacity){
201
 
    *buffer = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
202
 
    if(buffer == NULL){
 
204
    char *new_buf = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
 
205
    if(new_buf == NULL){
 
206
      int old_errno = errno;
 
207
      free(*buffer);
 
208
      errno = old_errno;
 
209
      *buffer = NULL;
203
210
      return 0;
204
211
    }
 
212
    *buffer = new_buf;
205
213
    buffer_capacity += BUFFER_SIZE;
206
214
  }
207
215
  return buffer_capacity;
778
786
    }
779
787
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
780
788
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
781
 
    const char *pcret;
782
789
    if(af == AF_INET6){
783
 
      pcret = inet_ntop(af, &(to.in6.sin6_addr), addrstr,
784
 
                        sizeof(addrstr));
 
790
      ret = getnameinfo((struct sockaddr *)&(to.in6), sizeof(to.in6),
 
791
                        addrstr, sizeof(addrstr), NULL, 0,
 
792
                        NI_NUMERICHOST);
785
793
    } else {
786
 
      pcret = inet_ntop(af, &(to.in.sin_addr), addrstr,
787
 
                        sizeof(addrstr));
 
794
      ret = getnameinfo((struct sockaddr *)&(to.in), sizeof(to.in),
 
795
                        addrstr, sizeof(addrstr), NULL, 0,
 
796
                        NI_NUMERICHOST);
788
797
    }
789
 
    if(pcret == NULL){
790
 
      perror_plus("inet_ntop");
791
 
    } else {
792
 
      if(strcmp(addrstr, ip) != 0){
793
 
        fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
794
 
      }
 
798
    if(ret == EAI_SYSTEM){
 
799
      perror_plus("getnameinfo");
 
800
    } else if(ret != 0) {
 
801
      fprintf_plus(stderr, "getnameinfo: %s", gai_strerror(ret));
 
802
    } else if(strcmp(addrstr, ip) != 0){
 
803
      fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
795
804
    }
796
805
  }
797
806
  
1488
1497
bool run_network_hooks(const char *mode, const char *interface,
1489
1498
                       const float delay){
1490
1499
  struct dirent **direntries;
1491
 
  struct dirent *direntry;
1492
 
  int ret;
1493
1500
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1494
1501
                         alphasort);
1495
1502
  if(numhooks == -1){
1502
1509
      perror_plus("scandir");
1503
1510
    }
1504
1511
  } else {
 
1512
    struct dirent *direntry;
 
1513
    int ret;
1505
1514
    int devnull = open("/dev/null", O_RDONLY);
1506
1515
    for(int i = 0; i < numhooks; i++){
1507
1516
      direntry = direntries[i];
1727
1736
}
1728
1737
 
1729
1738
error_t take_down_interface(const char *const interface){
1730
 
  int sd = -1;
1731
1739
  error_t old_errno = errno;
1732
 
  error_t ret_errno = 0;
1733
 
  int ret, ret_setflags;
1734
1740
  struct ifreq network;
1735
1741
  unsigned int if_index = if_nametoindex(interface);
1736
1742
  if(if_index == 0){
1739
1745
    return ENXIO;
1740
1746
  }
1741
1747
  if(interface_is_up(interface)){
 
1748
    error_t ret_errno = 0;
1742
1749
    if(not get_flags(interface, &network) and debug){
1743
1750
      ret_errno = errno;
1744
1751
      fprintf_plus(stderr, "Failed to get flags for interface "
1747
1754
    }
1748
1755
    network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1749
1756
    
1750
 
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
1757
    int sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1751
1758
    if(sd < 0){
1752
1759
      ret_errno = errno;
1753
1760
      perror_plus("socket");
1763
1770
    /* Raise priviliges */
1764
1771
    raise_privileges();
1765
1772
    
1766
 
    ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
 
1773
    int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1767
1774
    ret_errno = errno;
1768
1775
    
1769
1776
    /* Lower privileges */
1770
1777
    lower_privileges();
1771
1778
    
1772
1779
    /* Close the socket */
1773
 
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1780
    int ret = (int)TEMP_FAILURE_RETRY(close(sd));
1774
1781
    if(ret == -1){
1775
1782
      perror_plus("close");
1776
1783
    }
1809
1816
  const char *seckey = PATHDIR "/" SECKEY;
1810
1817
  const char *pubkey = PATHDIR "/" PUBKEY;
1811
1818
  char *interfaces_hooks = NULL;
1812
 
  size_t interfaces_hooks_size = 0;
1813
1819
  
1814
1820
  bool gnutls_initialized = false;
1815
1821
  bool gpgme_initialized = false;
2070
2076
        goto end;
2071
2077
      }
2072
2078
      memcpy(interfaces_hooks, mc.interfaces, mc.interfaces_size);
2073
 
      interfaces_hooks_size = mc.interfaces_size;
2074
 
      argz_stringify(interfaces_hooks, interfaces_hooks_size,
2075
 
                     (int)',');
 
2079
      argz_stringify(interfaces_hooks, mc.interfaces_size, (int)',');
2076
2080
    }
2077
2081
    if(not run_network_hooks("start", interfaces_hooks != NULL ?
2078
2082
                             interfaces_hooks : "", delay)){
2171
2175
        ret_errno = argz_add(&mc.interfaces, &mc.interfaces_size,
2172
2176
                             direntries[i]->d_name);
2173
2177
        if(ret_errno != 0){
 
2178
          errno = ret_errno;
2174
2179
          perror_plus("argz_add");
2175
2180
          continue;
2176
2181
        }
2219
2224
          ret_errno = argz_add(&interfaces_to_take_down,
2220
2225
                               &interfaces_to_take_down_size,
2221
2226
                               interface);
 
2227
          if(ret_errno != 0){
 
2228
            errno = ret_errno;
 
2229
            perror_plus("argz_add");
 
2230
          }
2222
2231
        }
2223
2232
      }
2224
2233
    }