/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-10 07:04:09 UTC
  • Revision ID: teddy@recompile.se-20140310070409-ho6p1wrnpopi2ny1
White space fix: change "if (" to "if(" in C code.

* plugins.d/askpass-fifo.c (error_plus): White space fix.
* plugins.d/password-prompt.c (error_plus, conflict_detection,
                               main): - '' -
* plugins.d/plymouth.c (error_plus, exec_and_wait, get_pid): - '' -
* plugins.d/splashy.c (error_plus): - '' -
* plugins.d/usplash.c (error_plus): - '' -

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-2012 Teddy Hogeborn
13
 
 * Copyright © 2008-2012 Björn Påhlsson
 
12
 * Copyright © 2008-2013 Teddy Hogeborn
 
13
 * Copyright © 2008-2013 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
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() */
187
190
  
188
191
  TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ",
189
192
                             program_invocation_short_name));
190
 
  return TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
 
193
  return (int)TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
191
194
}
192
195
 
193
196
/*
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;
224
232
    perror_plus("strdup");
225
233
    return false;
226
234
  }
 
235
  ret = clock_gettime(CLOCK_MONOTONIC, &(new_server->last_seen));
 
236
  if(ret == -1){
 
237
    perror_plus("clock_gettime");
 
238
    return false;
 
239
  }
227
240
  /* Special case of first server */
228
241
  if(*current_server == NULL){
229
242
    new_server->next = new_server;
236
249
    new_server->prev->next = new_server;
237
250
    (*current_server)->prev = new_server;
238
251
  }
239
 
  ret = clock_gettime(CLOCK_MONOTONIC, &(*current_server)->last_seen);
240
 
  if(ret == -1){
241
 
    perror_plus("clock_gettime");
242
 
    return false;
243
 
  }
244
252
  return true;
245
253
}
246
254
 
626
634
                                      int af, mandos_context *mc){
627
635
  int ret, tcp_sd = -1;
628
636
  ssize_t sret;
629
 
  union {
630
 
    struct sockaddr_in in;
631
 
    struct sockaddr_in6 in6;
632
 
  } to;
 
637
  struct sockaddr_storage to;
633
638
  char *buffer = NULL;
634
639
  char *decrypted_buffer = NULL;
635
640
  size_t buffer_length = 0;
659
664
    return -1;
660
665
  }
661
666
  
 
667
  /* If the interface is specified and we have a list of interfaces */
 
668
  if(if_index != AVAHI_IF_UNSPEC and mc->interfaces != NULL){
 
669
    /* Check if the interface is one of the interfaces we are using */
 
670
    bool match = false;
 
671
    {
 
672
      char *interface = NULL;
 
673
      while((interface=argz_next(mc->interfaces, mc->interfaces_size,
 
674
                                 interface))){
 
675
        if(if_nametoindex(interface) == (unsigned int)if_index){
 
676
          match = true;
 
677
          break;
 
678
        }
 
679
      }
 
680
    }
 
681
    if(not match){
 
682
      /* This interface does not match any in the list, so we don't
 
683
         connect to the server */
 
684
      if(debug){
 
685
        char interface[IF_NAMESIZE];
 
686
        if(if_indextoname((unsigned int)if_index, interface) == NULL){
 
687
          perror_plus("if_indextoname");
 
688
        } else {
 
689
          fprintf_plus(stderr, "Skipping server on non-used interface"
 
690
                       " \"%s\"\n",
 
691
                       if_indextoname((unsigned int)if_index,
 
692
                                      interface));
 
693
        }
 
694
      }
 
695
      return -1;
 
696
    }
 
697
  }
 
698
  
662
699
  ret = init_gnutls_session(&session, mc);
663
700
  if(ret != 0){
664
701
    return -1;
684
721
  
685
722
  memset(&to, 0, sizeof(to));
686
723
  if(af == AF_INET6){
687
 
    to.in6.sin6_family = (sa_family_t)af;
688
 
    ret = inet_pton(af, ip, &to.in6.sin6_addr);
 
724
    ((struct sockaddr_in6 *)&to)->sin6_family = (sa_family_t)af;
 
725
    ret = inet_pton(af, ip, &((struct sockaddr_in6 *)&to)->sin6_addr);
689
726
  } else {                      /* IPv4 */
690
 
    to.in.sin_family = (sa_family_t)af;
691
 
    ret = inet_pton(af, ip, &to.in.sin_addr);
 
727
    ((struct sockaddr_in *)&to)->sin_family = (sa_family_t)af;
 
728
    ret = inet_pton(af, ip, &((struct sockaddr_in *)&to)->sin_addr);
692
729
  }
693
730
  if(ret < 0 ){
694
731
    int e = errno;
703
740
    goto mandos_end;
704
741
  }
705
742
  if(af == AF_INET6){
706
 
    to.in6.sin6_port = htons(port);    
707
 
    if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
708
 
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
709
 
                                -Wunreachable-code*/
 
743
    ((struct sockaddr_in6 *)&to)->sin6_port = htons(port);    
 
744
    if(IN6_IS_ADDR_LINKLOCAL
 
745
       (&((struct sockaddr_in6 *)&to)->sin6_addr)){
710
746
      if(if_index == AVAHI_IF_UNSPEC){
711
747
        fprintf_plus(stderr, "An IPv6 link-local address is"
712
748
                     " incomplete without a network interface\n");
714
750
        goto mandos_end;
715
751
      }
716
752
      /* Set the network interface number as scope */
717
 
      to.in6.sin6_scope_id = (uint32_t)if_index;
 
753
      ((struct sockaddr_in6 *)&to)->sin6_scope_id = (uint32_t)if_index;
718
754
    }
719
755
  } else {
720
 
    to.in.sin_port = htons(port); /* Spurious warnings from
721
 
                                     -Wconversion and
722
 
                                     -Wunreachable-code */
 
756
    ((struct sockaddr_in *)&to)->sin_port = htons(port);
723
757
  }
724
758
  
725
759
  if(quit_now){
742
776
    }
743
777
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
744
778
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
745
 
    const char *pcret;
746
779
    if(af == AF_INET6){
747
 
      pcret = inet_ntop(af, &(to.in6.sin6_addr), addrstr,
748
 
                        sizeof(addrstr));
 
780
      ret = getnameinfo((struct sockaddr *)&to,
 
781
                        sizeof(struct sockaddr_in6),
 
782
                        addrstr, sizeof(addrstr), NULL, 0,
 
783
                        NI_NUMERICHOST);
749
784
    } else {
750
 
      pcret = inet_ntop(af, &(to.in.sin_addr), addrstr,
751
 
                        sizeof(addrstr));
 
785
      ret = getnameinfo((struct sockaddr *)&to,
 
786
                        sizeof(struct sockaddr_in),
 
787
                        addrstr, sizeof(addrstr), NULL, 0,
 
788
                        NI_NUMERICHOST);
752
789
    }
753
 
    if(pcret == NULL){
754
 
      perror_plus("inet_ntop");
755
 
    } else {
756
 
      if(strcmp(addrstr, ip) != 0){
757
 
        fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
758
 
      }
 
790
    if(ret == EAI_SYSTEM){
 
791
      perror_plus("getnameinfo");
 
792
    } else if(ret != 0) {
 
793
      fprintf_plus(stderr, "getnameinfo: %s", gai_strerror(ret));
 
794
    } else if(strcmp(addrstr, ip) != 0){
 
795
      fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
759
796
    }
760
797
  }
761
798
  
765
802
  }
766
803
  
767
804
  if(af == AF_INET6){
768
 
    ret = connect(tcp_sd, &to.in6, sizeof(to));
 
805
    ret = connect(tcp_sd, (struct sockaddr *)&to,
 
806
                  sizeof(struct sockaddr_in6));
769
807
  } else {
770
 
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
 
808
    ret = connect(tcp_sd, (struct sockaddr *)&to, /* IPv4 */
 
809
                  sizeof(struct sockaddr_in));
771
810
  }
772
811
  if(ret < 0){
773
812
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
1452
1491
bool run_network_hooks(const char *mode, const char *interface,
1453
1492
                       const float delay){
1454
1493
  struct dirent **direntries;
1455
 
  struct dirent *direntry;
1456
 
  int ret;
1457
1494
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1458
1495
                         alphasort);
1459
1496
  if(numhooks == -1){
1466
1503
      perror_plus("scandir");
1467
1504
    }
1468
1505
  } else {
 
1506
    struct dirent *direntry;
 
1507
    int ret;
1469
1508
    int devnull = open("/dev/null", O_RDONLY);
1470
1509
    for(int i = 0; i < numhooks; i++){
1471
1510
      direntry = direntries[i];
1691
1730
}
1692
1731
 
1693
1732
error_t take_down_interface(const char *const interface){
1694
 
  int sd = -1;
1695
1733
  error_t old_errno = errno;
1696
 
  error_t ret_errno = 0;
1697
 
  int ret, ret_setflags;
1698
1734
  struct ifreq network;
1699
1735
  unsigned int if_index = if_nametoindex(interface);
1700
1736
  if(if_index == 0){
1703
1739
    return ENXIO;
1704
1740
  }
1705
1741
  if(interface_is_up(interface)){
 
1742
    error_t ret_errno = 0;
1706
1743
    if(not get_flags(interface, &network) and debug){
1707
1744
      ret_errno = errno;
1708
1745
      fprintf_plus(stderr, "Failed to get flags for interface "
1711
1748
    }
1712
1749
    network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1713
1750
    
1714
 
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
1751
    int sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1715
1752
    if(sd < 0){
1716
1753
      ret_errno = errno;
1717
1754
      perror_plus("socket");
1727
1764
    /* Raise priviliges */
1728
1765
    raise_privileges();
1729
1766
    
1730
 
    ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
 
1767
    int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1731
1768
    ret_errno = errno;
1732
1769
    
1733
1770
    /* Lower privileges */
1734
1771
    lower_privileges();
1735
1772
    
1736
1773
    /* Close the socket */
1737
 
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1774
    int ret = (int)TEMP_FAILURE_RETRY(close(sd));
1738
1775
    if(ret == -1){
1739
1776
      perror_plus("close");
1740
1777
    }
1773
1810
  const char *seckey = PATHDIR "/" SECKEY;
1774
1811
  const char *pubkey = PATHDIR "/" PUBKEY;
1775
1812
  char *interfaces_hooks = NULL;
1776
 
  size_t interfaces_hooks_size = 0;
1777
1813
  
1778
1814
  bool gnutls_initialized = false;
1779
1815
  bool gpgme_initialized = false;
2004
2040
      }
2005
2041
    
2006
2042
      /* Lower privileges */
2007
 
      errno = 0;
2008
 
      ret = seteuid(uid);
2009
 
      if(ret == -1){
2010
 
        perror_plus("seteuid");
2011
 
      }
 
2043
      lower_privileges();
2012
2044
    }
2013
2045
  }
2014
2046
  
2015
 
  /* Remove empty interface names */
 
2047
  /* Remove invalid interface names (except "none") */
2016
2048
  {
2017
2049
    char *interface = NULL;
2018
2050
    while((interface = argz_next(mc.interfaces, mc.interfaces_size,
2019
2051
                                 interface))){
2020
 
      if(if_nametoindex(interface) == 0){
2021
 
        if(interface[0] != '\0' and strcmp(interface, "none") != 0){
 
2052
      if(strcmp(interface, "none") != 0
 
2053
         and if_nametoindex(interface) == 0){
 
2054
        if(interface[0] != '\0'){
2022
2055
          fprintf_plus(stderr, "Not using nonexisting interface"
2023
2056
                       " \"%s\"\n", interface);
2024
2057
        }
2030
2063
  
2031
2064
  /* Run network hooks */
2032
2065
  {
2033
 
    
2034
2066
    if(mc.interfaces != NULL){
2035
2067
      interfaces_hooks = malloc(mc.interfaces_size);
2036
2068
      if(interfaces_hooks == NULL){
2038
2070
        goto end;
2039
2071
      }
2040
2072
      memcpy(interfaces_hooks, mc.interfaces, mc.interfaces_size);
2041
 
      interfaces_hooks_size = mc.interfaces_size;
2042
 
      argz_stringify(interfaces_hooks, interfaces_hooks_size,
2043
 
                     (int)',');
 
2073
      argz_stringify(interfaces_hooks, mc.interfaces_size, (int)',');
2044
2074
    }
2045
2075
    if(not run_network_hooks("start", interfaces_hooks != NULL ?
2046
2076
                             interfaces_hooks : "", delay)){
2139
2169
        ret_errno = argz_add(&mc.interfaces, &mc.interfaces_size,
2140
2170
                             direntries[i]->d_name);
2141
2171
        if(ret_errno != 0){
 
2172
          errno = ret_errno;
2142
2173
          perror_plus("argz_add");
2143
2174
          continue;
2144
2175
        }
2156
2187
    }
2157
2188
  }
2158
2189
  
2159
 
  /* If we only got one interface, explicitly use only that one */
2160
 
  if(argz_count(mc.interfaces, mc.interfaces_size) == 1){
2161
 
    if(debug){
2162
 
      fprintf_plus(stderr, "Using only interface \"%s\"\n",
2163
 
                   mc.interfaces);
2164
 
    }
2165
 
    if_index = (AvahiIfIndex)if_nametoindex(mc.interfaces);
2166
 
  }
2167
 
  
2168
 
  /* Bring up interfaces which are down */
2169
 
  if(not (argz_count(mc.interfaces, mc.interfaces_size) == 1
2170
 
          and strcmp(mc.interfaces, "none") == 0)){
 
2190
  /* Bring up interfaces which are down, and remove any "none"s */
 
2191
  {
2171
2192
    char *interface = NULL;
2172
2193
    while((interface = argz_next(mc.interfaces, mc.interfaces_size,
2173
2194
                                 interface))){
 
2195
      /* If interface name is "none", stop bringing up interfaces.
 
2196
         Also remove all instances of "none" from the list */
 
2197
      if(strcmp(interface, "none") == 0){
 
2198
        argz_delete(&mc.interfaces, &mc.interfaces_size,
 
2199
                    interface);
 
2200
        interface = NULL;
 
2201
        while((interface = argz_next(mc.interfaces,
 
2202
                                     mc.interfaces_size, interface))){
 
2203
          if(strcmp(interface, "none") == 0){
 
2204
            argz_delete(&mc.interfaces, &mc.interfaces_size,
 
2205
                        interface);
 
2206
            interface = NULL;
 
2207
          }
 
2208
        }
 
2209
        break;
 
2210
      }
2174
2211
      bool interface_was_up = interface_is_up(interface);
2175
2212
      ret = bring_up_interface(interface, delay);
2176
2213
      if(not interface_was_up){
2181
2218
          ret_errno = argz_add(&interfaces_to_take_down,
2182
2219
                               &interfaces_to_take_down_size,
2183
2220
                               interface);
 
2221
          if(ret_errno != 0){
 
2222
            errno = ret_errno;
 
2223
            perror_plus("argz_add");
 
2224
          }
2184
2225
        }
2185
2226
      }
2186
2227
    }
2187
 
    free(mc.interfaces);
2188
 
    mc.interfaces = NULL;
2189
 
    mc.interfaces_size = 0;
2190
2228
    if(debug and (interfaces_to_take_down == NULL)){
2191
2229
      fprintf_plus(stderr, "No interfaces were brought up\n");
2192
2230
    }
2193
2231
  }
2194
2232
  
 
2233
  /* If we only got one interface, explicitly use only that one */
 
2234
  if(argz_count(mc.interfaces, mc.interfaces_size) == 1){
 
2235
    if(debug){
 
2236
      fprintf_plus(stderr, "Using only interface \"%s\"\n",
 
2237
                   mc.interfaces);
 
2238
    }
 
2239
    if_index = (AvahiIfIndex)if_nametoindex(mc.interfaces);
 
2240
  }
 
2241
  
2195
2242
  if(quit_now){
2196
2243
    goto end;
2197
2244
  }
2255
2302
      exitcode = EX_USAGE;
2256
2303
      goto end;
2257
2304
    }
2258
 
  
 
2305
    
2259
2306
    if(quit_now){
2260
2307
      goto end;
2261
2308
    }
2291
2338
        fprintf_plus(stderr, "Retrying in %d seconds\n",
2292
2339
                     (int)retry_interval);
2293
2340
      }
2294
 
      sleep((int)retry_interval);
 
2341
      sleep((unsigned int)retry_interval);
2295
2342
    }
2296
2343
    
2297
2344
    if (not quit_now){
2370
2417
  }
2371
2418
  
2372
2419
  /* Cleanup things */
 
2420
  free(mc.interfaces);
 
2421
  
2373
2422
  if(sb != NULL)
2374
2423
    avahi_s_service_browser_free(sb);
2375
2424