/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-16 19:30:08 UTC
  • Revision ID: teddy@recompile.se-20120616193008-2erbfaw5a5y3yay1
* debian/mandos-client.README.Debian: Update documentation for using
                                      multiple interfaces.

Show diffs side-by-side

added added

removed removed

Lines of Context:
61
61
                                 */
62
62
#include <inttypes.h>           /* PRIu16, PRIdMAX, intmax_t,
63
63
                                   strtoimax() */
64
 
#include <assert.h>             /* assert() */
65
64
#include <errno.h>              /* perror(), errno,
66
65
                                   program_invocation_short_name */
67
66
#include <time.h>               /* nanosleep(), time(), sleep() */
145
144
/* Doubly linked list that need to be circularly linked when used */
146
145
typedef struct server{
147
146
  const char *ip;
148
 
  uint16_t port;
 
147
  in_port_t port;
149
148
  AvahiIfIndex if_index;
150
149
  int af;
151
150
  struct timespec last_seen;
211
210
}
212
211
 
213
212
/* Add server to set of servers to retry periodically */
214
 
bool add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
 
213
bool add_server(const char *ip, in_port_t port, AvahiIfIndex if_index,
215
214
                int af){
216
215
  int ret;
217
216
  server *new_server = malloc(sizeof(server));
471
470
}
472
471
 
473
472
static const char * safer_gnutls_strerror(int value){
474
 
  const char *ret = gnutls_strerror(value); /* Spurious warning from
475
 
                                               -Wunreachable-code */
 
473
  const char *ret = gnutls_strerror(value);
476
474
  if(ret == NULL)
477
475
    ret = "(unknown)";
478
476
  return ret;
623
621
                      __attribute__((unused)) const char *txt){}
624
622
 
625
623
/* Called when a Mandos server is found */
626
 
static int start_mandos_communication(const char *ip, uint16_t port,
 
624
static int start_mandos_communication(const char *ip, in_port_t port,
627
625
                                      AvahiIfIndex if_index,
628
626
                                      int af){
629
627
  int ret, tcp_sd = -1;
668
666
  
669
667
  if(debug){
670
668
    fprintf_plus(stderr, "Setting up a TCP connection to %s, port %"
671
 
                 PRIu16 "\n", ip, port);
 
669
                 PRIuMAX "\n", ip, (uintmax_t)port);
672
670
  }
673
671
  
674
672
  tcp_sd = socket(pf, SOCK_STREAM, 0);
705
703
    goto mandos_end;
706
704
  }
707
705
  if(af == AF_INET6){
708
 
    to.in6.sin6_port = htons(port); /* Spurious warnings from
709
 
                                       -Wconversion and
710
 
                                       -Wunreachable-code */
711
 
    
 
706
    to.in6.sin6_port = htons(port);    
712
707
    if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
713
708
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
714
709
                                -Wunreachable-code*/
738
733
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
739
734
        perror_plus("if_indextoname");
740
735
      } else {
741
 
        fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16
742
 
                     "\n", ip, interface, port);
 
736
        fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIuMAX
 
737
                     "\n", ip, interface, (uintmax_t)port);
743
738
      }
744
739
    } else {
745
 
      fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n",
746
 
                   ip, port);
 
740
      fprintf_plus(stderr, "Connection to: %s, port %" PRIuMAX "\n",
 
741
                   ip, (uintmax_t)port);
747
742
    }
748
743
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
749
744
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
1010
1005
                             AVAHI_GCC_UNUSED AvahiLookupResultFlags
1011
1006
                             flags,
1012
1007
                             AVAHI_GCC_UNUSED void* userdata){
1013
 
  assert(r);
 
1008
  if(r == NULL){
 
1009
    return;
 
1010
  }
1014
1011
  
1015
1012
  /* Called whenever a service has been resolved successfully or
1016
1013
     timed out */
1037
1034
                     PRIdMAX ") on port %" PRIu16 "\n", name,
1038
1035
                     host_name, ip, (intmax_t)interface, port);
1039
1036
      }
1040
 
      int ret = start_mandos_communication(ip, port, interface,
 
1037
      int ret = start_mandos_communication(ip, (in_port_t)port,
 
1038
                                           interface,
1041
1039
                                           avahi_proto_to_af(proto));
1042
1040
      if(ret == 0){
1043
1041
        avahi_simple_poll_quit(mc.simple_poll);
1044
1042
      } else {
1045
 
        if(not add_server(ip, port, interface,
 
1043
        if(not add_server(ip, (in_port_t)port, interface,
1046
1044
                          avahi_proto_to_af(proto))){
1047
1045
          fprintf_plus(stderr, "Failed to add server \"%s\" to server"
1048
1046
                       " list\n", name);
1063
1061
                            AVAHI_GCC_UNUSED AvahiLookupResultFlags
1064
1062
                            flags,
1065
1063
                            AVAHI_GCC_UNUSED void* userdata){
1066
 
  assert(b);
 
1064
  if(b == NULL){
 
1065
    return;
 
1066
  }
1067
1067
  
1068
1068
  /* Called whenever a new services becomes available on the LAN or
1069
1069
     is removed from the LAN */
1450
1450
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1451
1451
                         alphasort);
1452
1452
  if(numhooks == -1){
1453
 
    perror_plus("scandir");
 
1453
    if(errno == ENOENT){
 
1454
      if(debug){
 
1455
        fprintf_plus(stderr, "Network hook directory \"%s\" not"
 
1456
                     " found\n", hookdir);
 
1457
      }
 
1458
    } else {
 
1459
      perror_plus("scandir");
 
1460
    }
1454
1461
  } else {
1455
1462
    int devnull = open("/dev/null", O_RDONLY);
1456
1463
    for(int i = 0; i < numhooks; i++){
2014
2021
  
2015
2022
  /* Run network hooks */
2016
2023
  {
2017
 
    ret_errno = argz_append(&interfaces_hooks, &interfaces_hooks_size,
2018
 
                            interfaces, interfaces_size);
2019
 
    if(ret_errno != 0){
2020
 
      errno = ret_errno;
2021
 
      perror_plus("argz_append");
2022
 
      goto end;
 
2024
    
 
2025
    if(interfaces != NULL){
 
2026
      interfaces_hooks = malloc(interfaces_size);
 
2027
      if(interfaces_hooks == NULL){
 
2028
        perror_plus("malloc");
 
2029
        goto end;
 
2030
      }
 
2031
      memcpy(interfaces_hooks, interfaces, interfaces_size);
 
2032
      interfaces_hooks_size = interfaces_size;
 
2033
      argz_stringify(interfaces_hooks, interfaces_hooks_size,
 
2034
                     (int)',');
2023
2035
    }
2024
 
    argz_stringify(interfaces_hooks, interfaces_hooks_size, (int)',');
2025
 
    if(not run_network_hooks("start", interfaces_hooks, delay)){
 
2036
    if(not run_network_hooks("start", interfaces_hooks != NULL ?
 
2037
                             interfaces_hooks : "", delay)){
2026
2038
      goto end;
2027
2039
    }
2028
2040
  }
2225
2237
      goto end;
2226
2238
    }
2227
2239
    
2228
 
    uint16_t port;
 
2240
    in_port_t port;
2229
2241
    errno = 0;
2230
2242
    tmpmax = strtoimax(address+1, &tmp, 10);
2231
2243
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
2232
 
       or tmpmax != (uint16_t)tmpmax){
 
2244
       or tmpmax != (in_port_t)tmpmax){
2233
2245
      fprintf_plus(stderr, "Bad port number\n");
2234
2246
      exitcode = EX_USAGE;
2235
2247
      goto end;
2239
2251
      goto end;
2240
2252
    }
2241
2253
    
2242
 
    port = (uint16_t)tmpmax;
 
2254
    port = (in_port_t)tmpmax;
2243
2255
    *address = '\0';
2244
2256
    /* Colon in address indicates IPv6 */
2245
2257
    int af;
2383
2395
    raise_privileges();
2384
2396
    
2385
2397
    /* Run network hooks */
2386
 
    run_network_hooks("stop", interfaces_hooks, delay);
 
2398
    run_network_hooks("stop", interfaces_hooks != NULL ?
 
2399
                      interfaces_hooks : "", delay);
2387
2400
    
2388
2401
    /* Take down the network interfaces which were brought up */
2389
2402
    {