/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: 2012-06-17 02:30:59 UTC
  • Revision ID: teddy@recompile.se-20120617023059-em4nfnxg1tsn64xj
* plugins.d/mandos-client (start_mandos_communication): Bug fix; skip
                                                        non-specified
                                                        interfaces.
  (main): Use lower_privileges() consistently.  Bug fix: Don't remove
          "none" from list of interfaces.  Make --interface=none work
          again by not bringing up interfaces specified after "none".
* plugins.d/mandos-client.xml (OPTIONS): Document new meaning of
                                         specifying --interface=none
                                         together with other
                                         interface names,

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
 * "browse_callback", and parts of "main".
10
10
 * 
11
11
 * Everything else is
12
 
 * Copyright © 2008-2013 Teddy Hogeborn
13
 
 * Copyright © 2008-2013 Björn Påhlsson
 
12
 * Copyright © 2008-2012 Teddy Hogeborn
 
13
 * Copyright © 2008-2012 Björn Påhlsson
14
14
 * 
15
15
 * This program is free software: you can redistribute it and/or
16
16
 * modify it under the terms of the GNU General Public License as
187
187
  
188
188
  TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ",
189
189
                             program_invocation_short_name));
190
 
  return (int)TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
 
190
  return TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
191
191
}
192
192
 
193
193
/*
198
198
size_t incbuffer(char **buffer, size_t buffer_length,
199
199
                 size_t buffer_capacity){
200
200
  if(buffer_length + BUFFER_SIZE > buffer_capacity){
201
 
    char *new_buf = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
202
 
    if(new_buf == NULL){
203
 
      int old_errno = errno;
204
 
      free(*buffer);
205
 
      errno = old_errno;
206
 
      *buffer = NULL;
 
201
    *buffer = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
 
202
    if(buffer == NULL){
207
203
      return 0;
208
204
    }
209
 
    *buffer = new_buf;
210
205
    buffer_capacity += BUFFER_SIZE;
211
206
  }
212
207
  return buffer_capacity;
664
659
    return -1;
665
660
  }
666
661
  
667
 
  /* If the interface is specified and we have a list of interfaces */
668
662
  if(if_index != AVAHI_IF_UNSPEC and mc->interfaces != NULL){
669
663
    /* Check if the interface is one of the interfaces we are using */
670
664
    bool match = false;
679
673
      }
680
674
    }
681
675
    if(not match){
682
 
      /* This interface does not match any in the list, so we don't
683
 
         connect to the server */
684
676
      if(debug){
685
677
        char interface[IF_NAMESIZE];
686
678
        if(if_indextoname((unsigned int)if_index, interface) == NULL){
741
733
  }
742
734
  if(af == AF_INET6){
743
735
    to.in6.sin6_port = htons(port);    
744
 
#ifdef __GNUC__
745
 
#pragma GCC diagnostic push
746
 
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
747
 
#endif
748
736
    if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
749
 
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower */
750
 
#ifdef __GNUC__
751
 
#pragma GCC diagnostic pop
752
 
#endif
 
737
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
 
738
                                -Wunreachable-code*/
753
739
      if(if_index == AVAHI_IF_UNSPEC){
754
740
        fprintf_plus(stderr, "An IPv6 link-local address is"
755
741
                     " incomplete without a network interface\n");
760
746
      to.in6.sin6_scope_id = (uint32_t)if_index;
761
747
    }
762
748
  } else {
763
 
    to.in.sin_port = htons(port);
 
749
    to.in.sin_port = htons(port); /* Spurious warnings from
 
750
                                     -Wconversion and
 
751
                                     -Wunreachable-code */
764
752
  }
765
753
  
766
754
  if(quit_now){
1493
1481
bool run_network_hooks(const char *mode, const char *interface,
1494
1482
                       const float delay){
1495
1483
  struct dirent **direntries;
 
1484
  struct dirent *direntry;
 
1485
  int ret;
1496
1486
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1497
1487
                         alphasort);
1498
1488
  if(numhooks == -1){
1505
1495
      perror_plus("scandir");
1506
1496
    }
1507
1497
  } else {
1508
 
    struct dirent *direntry;
1509
 
    int ret;
1510
1498
    int devnull = open("/dev/null", O_RDONLY);
1511
1499
    for(int i = 0; i < numhooks; i++){
1512
1500
      direntry = direntries[i];
1732
1720
}
1733
1721
 
1734
1722
error_t take_down_interface(const char *const interface){
 
1723
  int sd = -1;
1735
1724
  error_t old_errno = errno;
 
1725
  error_t ret_errno = 0;
 
1726
  int ret, ret_setflags;
1736
1727
  struct ifreq network;
1737
1728
  unsigned int if_index = if_nametoindex(interface);
1738
1729
  if(if_index == 0){
1741
1732
    return ENXIO;
1742
1733
  }
1743
1734
  if(interface_is_up(interface)){
1744
 
    error_t ret_errno = 0;
1745
1735
    if(not get_flags(interface, &network) and debug){
1746
1736
      ret_errno = errno;
1747
1737
      fprintf_plus(stderr, "Failed to get flags for interface "
1750
1740
    }
1751
1741
    network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1752
1742
    
1753
 
    int sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
1743
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1754
1744
    if(sd < 0){
1755
1745
      ret_errno = errno;
1756
1746
      perror_plus("socket");
1766
1756
    /* Raise priviliges */
1767
1757
    raise_privileges();
1768
1758
    
1769
 
    int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
 
1759
    ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1770
1760
    ret_errno = errno;
1771
1761
    
1772
1762
    /* Lower privileges */
1773
1763
    lower_privileges();
1774
1764
    
1775
1765
    /* Close the socket */
1776
 
    int ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1766
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
1777
1767
    if(ret == -1){
1778
1768
      perror_plus("close");
1779
1769
    }
1812
1802
  const char *seckey = PATHDIR "/" SECKEY;
1813
1803
  const char *pubkey = PATHDIR "/" PUBKEY;
1814
1804
  char *interfaces_hooks = NULL;
 
1805
  size_t interfaces_hooks_size = 0;
1815
1806
  
1816
1807
  bool gnutls_initialized = false;
1817
1808
  bool gpgme_initialized = false;
2072
2063
        goto end;
2073
2064
      }
2074
2065
      memcpy(interfaces_hooks, mc.interfaces, mc.interfaces_size);
2075
 
      argz_stringify(interfaces_hooks, mc.interfaces_size, (int)',');
 
2066
      interfaces_hooks_size = mc.interfaces_size;
 
2067
      argz_stringify(interfaces_hooks, interfaces_hooks_size,
 
2068
                     (int)',');
2076
2069
    }
2077
2070
    if(not run_network_hooks("start", interfaces_hooks != NULL ?
2078
2071
                             interfaces_hooks : "", delay)){
2171
2164
        ret_errno = argz_add(&mc.interfaces, &mc.interfaces_size,
2172
2165
                             direntries[i]->d_name);
2173
2166
        if(ret_errno != 0){
2174
 
          errno = ret_errno;
2175
2167
          perror_plus("argz_add");
2176
2168
          continue;
2177
2169
        }
2189
2181
    }
2190
2182
  }
2191
2183
  
2192
 
  /* Bring up interfaces which are down, and remove any "none"s */
 
2184
  /* Bring up interfaces which are down */
2193
2185
  {
2194
2186
    char *interface = NULL;
2195
2187
    while((interface = argz_next(mc.interfaces, mc.interfaces_size,
2220
2212
          ret_errno = argz_add(&interfaces_to_take_down,
2221
2213
                               &interfaces_to_take_down_size,
2222
2214
                               interface);
2223
 
          if(ret_errno != 0){
2224
 
            errno = ret_errno;
2225
 
            perror_plus("argz_add");
2226
 
          }
2227
2215
        }
2228
2216
      }
2229
2217
    }
2340
2328
        fprintf_plus(stderr, "Retrying in %d seconds\n",
2341
2329
                     (int)retry_interval);
2342
2330
      }
2343
 
      sleep((unsigned int)retry_interval);
 
2331
      sleep((int)retry_interval);
2344
2332
    }
2345
2333
    
2346
2334
    if (not quit_now){