/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-06 02:26:04 UTC
  • Revision ID: teddy@recompile.se-20140306022604-4uc43taz25cflgi3
Bug fix: Free all memory and give better messages when memory is full.

* plugin-runner.c (add_to_char_array): Bug fix: If realloc fails, do
                                       not change old array pointer.
  (add_environment): Bug fix: If realloc fails, do not change old
                     environment pointer.  Also rename "e" to "envdef"
                     for clarity.
  (main): Bug fix: If realloc fails, do not change old pointers.  Also
          wrap "#pragma GCC" with "#ifdef ___GNUC___".
* plugins.d/mandos-client.c (incbuffer): Bug fix: if realloc fails,
                                         free old buffer.
  (run_network_hooks): Moved variables "directory" and "ret" to their
                       innermost possible scope.
  (take_down_interface): Moved variables "sd", "ret_errno", and
                         "ret_setflags" to their innermost possible
                         scope.
  (main): Removed variable "interfaces_hooks_size".  Also, if argz_add
          fails when adding all found interfaces, the error message
          will now be correct.  Also print error message if, after
          having taken up an interface, argz_add fails to add
          interface to list of interfaces to be taken down.
* plugins.d/mandos-client.xml (OPTIONS): Explain better what "none"
                                         means as argument to
                                         "--interface" by negating
                                         sense.
* plugins.d/password-prompt.c (fprintf_plus): Removed (unused).

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
161
161
  const char *priority;
162
162
  gpgme_ctx_t ctx;
163
163
  server *current_server;
 
164
  char *interfaces;
 
165
  size_t interfaces_size;
164
166
} mandos_context;
165
167
 
166
168
/* global so signal handler can reach it*/
185
187
  
186
188
  TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ",
187
189
                             program_invocation_short_name));
188
 
  return TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
 
190
  return (int)TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
189
191
}
190
192
 
191
193
/*
196
198
size_t incbuffer(char **buffer, size_t buffer_length,
197
199
                 size_t buffer_capacity){
198
200
  if(buffer_length + BUFFER_SIZE > buffer_capacity){
199
 
    *buffer = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
200
 
    if(buffer == NULL){
 
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
207
      return 0;
202
208
    }
 
209
    *buffer = new_buf;
203
210
    buffer_capacity += BUFFER_SIZE;
204
211
  }
205
212
  return buffer_capacity;
657
664
    return -1;
658
665
  }
659
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
  
660
699
  ret = init_gnutls_session(&session, mc);
661
700
  if(ret != 0){
662
701
    return -1;
702
741
  }
703
742
  if(af == AF_INET6){
704
743
    to.in6.sin6_port = htons(port);    
 
744
#ifdef __GNUC__
 
745
#pragma GCC diagnostic push
 
746
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
 
747
#endif
705
748
    if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
706
 
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
707
 
                                -Wunreachable-code*/
 
749
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower */
 
750
#ifdef __GNUC__
 
751
#pragma GCC diagnostic pop
 
752
#endif
708
753
      if(if_index == AVAHI_IF_UNSPEC){
709
754
        fprintf_plus(stderr, "An IPv6 link-local address is"
710
755
                     " incomplete without a network interface\n");
715
760
      to.in6.sin6_scope_id = (uint32_t)if_index;
716
761
    }
717
762
  } else {
718
 
    to.in.sin_port = htons(port); /* Spurious warnings from
719
 
                                     -Wconversion and
720
 
                                     -Wunreachable-code */
 
763
    to.in.sin_port = htons(port);
721
764
  }
722
765
  
723
766
  if(quit_now){
1450
1493
bool run_network_hooks(const char *mode, const char *interface,
1451
1494
                       const float delay){
1452
1495
  struct dirent **direntries;
1453
 
  struct dirent *direntry;
1454
 
  int ret;
1455
1496
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1456
1497
                         alphasort);
1457
1498
  if(numhooks == -1){
1464
1505
      perror_plus("scandir");
1465
1506
    }
1466
1507
  } else {
 
1508
    struct dirent *direntry;
 
1509
    int ret;
1467
1510
    int devnull = open("/dev/null", O_RDONLY);
1468
1511
    for(int i = 0; i < numhooks; i++){
1469
1512
      direntry = direntries[i];
1689
1732
}
1690
1733
 
1691
1734
error_t take_down_interface(const char *const interface){
1692
 
  int sd = -1;
1693
1735
  error_t old_errno = errno;
1694
 
  error_t ret_errno = 0;
1695
 
  int ret, ret_setflags;
1696
1736
  struct ifreq network;
1697
1737
  unsigned int if_index = if_nametoindex(interface);
1698
1738
  if(if_index == 0){
1701
1741
    return ENXIO;
1702
1742
  }
1703
1743
  if(interface_is_up(interface)){
 
1744
    error_t ret_errno = 0;
1704
1745
    if(not get_flags(interface, &network) and debug){
1705
1746
      ret_errno = errno;
1706
1747
      fprintf_plus(stderr, "Failed to get flags for interface "
1709
1750
    }
1710
1751
    network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1711
1752
    
1712
 
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
1753
    int sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1713
1754
    if(sd < 0){
1714
1755
      ret_errno = errno;
1715
1756
      perror_plus("socket");
1725
1766
    /* Raise priviliges */
1726
1767
    raise_privileges();
1727
1768
    
1728
 
    ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
 
1769
    int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1729
1770
    ret_errno = errno;
1730
1771
    
1731
1772
    /* Lower privileges */
1732
1773
    lower_privileges();
1733
1774
    
1734
1775
    /* Close the socket */
1735
 
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1776
    int ret = (int)TEMP_FAILURE_RETRY(close(sd));
1736
1777
    if(ret == -1){
1737
1778
      perror_plus("close");
1738
1779
    }
1755
1796
int main(int argc, char *argv[]){
1756
1797
  mandos_context mc = { .server = NULL, .dh_bits = 1024,
1757
1798
                        .priority = "SECURE256:!CTYPE-X.509:"
1758
 
                        "+CTYPE-OPENPGP", .current_server = NULL };
 
1799
                        "+CTYPE-OPENPGP", .current_server = NULL, 
 
1800
                        .interfaces = NULL, .interfaces_size = 0 };
1759
1801
  AvahiSServiceBrowser *sb = NULL;
1760
1802
  error_t ret_errno;
1761
1803
  int ret;
1762
1804
  intmax_t tmpmax;
1763
1805
  char *tmp;
1764
1806
  int exitcode = EXIT_SUCCESS;
1765
 
  char *interfaces = NULL;
1766
 
  size_t interfaces_size = 0;
1767
1807
  char *interfaces_to_take_down = NULL;
1768
1808
  size_t interfaces_to_take_down_size = 0;
1769
1809
  char tempdir[] = "/tmp/mandosXXXXXX";
1772
1812
  const char *seckey = PATHDIR "/" SECKEY;
1773
1813
  const char *pubkey = PATHDIR "/" PUBKEY;
1774
1814
  char *interfaces_hooks = NULL;
1775
 
  size_t interfaces_hooks_size = 0;
1776
1815
  
1777
1816
  bool gnutls_initialized = false;
1778
1817
  bool gpgme_initialized = false;
1869
1908
        connect_to = arg;
1870
1909
        break;
1871
1910
      case 'i':                 /* --interface */
1872
 
        ret_errno = argz_add_sep(&interfaces, &interfaces_size, arg,
1873
 
                                 (int)',');
 
1911
        ret_errno = argz_add_sep(&mc.interfaces, &mc.interfaces_size,
 
1912
                                 arg, (int)',');
1874
1913
        if(ret_errno != 0){
1875
1914
          argp_error(state, "%s", strerror(ret_errno));
1876
1915
        }
2003
2042
      }
2004
2043
    
2005
2044
      /* Lower privileges */
2006
 
      errno = 0;
2007
 
      ret = seteuid(uid);
2008
 
      if(ret == -1){
2009
 
        perror_plus("seteuid");
2010
 
      }
 
2045
      lower_privileges();
2011
2046
    }
2012
2047
  }
2013
2048
  
2014
 
  /* Remove empty interface names */
 
2049
  /* Remove invalid interface names (except "none") */
2015
2050
  {
2016
2051
    char *interface = NULL;
2017
 
    while((interface = argz_next(interfaces, interfaces_size,
 
2052
    while((interface = argz_next(mc.interfaces, mc.interfaces_size,
2018
2053
                                 interface))){
2019
 
      if(if_nametoindex(interface) == 0){
2020
 
        if(interface[0] != '\0' and strcmp(interface, "none") != 0){
 
2054
      if(strcmp(interface, "none") != 0
 
2055
         and if_nametoindex(interface) == 0){
 
2056
        if(interface[0] != '\0'){
2021
2057
          fprintf_plus(stderr, "Not using nonexisting interface"
2022
2058
                       " \"%s\"\n", interface);
2023
2059
        }
2024
 
        argz_delete(&interfaces, &interfaces_size, interface);
 
2060
        argz_delete(&mc.interfaces, &mc.interfaces_size, interface);
2025
2061
        interface = NULL;
2026
2062
      }
2027
2063
    }
2029
2065
  
2030
2066
  /* Run network hooks */
2031
2067
  {
2032
 
    
2033
 
    if(interfaces != NULL){
2034
 
      interfaces_hooks = malloc(interfaces_size);
 
2068
    if(mc.interfaces != NULL){
 
2069
      interfaces_hooks = malloc(mc.interfaces_size);
2035
2070
      if(interfaces_hooks == NULL){
2036
2071
        perror_plus("malloc");
2037
2072
        goto end;
2038
2073
      }
2039
 
      memcpy(interfaces_hooks, interfaces, interfaces_size);
2040
 
      interfaces_hooks_size = interfaces_size;
2041
 
      argz_stringify(interfaces_hooks, interfaces_hooks_size,
2042
 
                     (int)',');
 
2074
      memcpy(interfaces_hooks, mc.interfaces, mc.interfaces_size);
 
2075
      argz_stringify(interfaces_hooks, mc.interfaces_size, (int)',');
2043
2076
    }
2044
2077
    if(not run_network_hooks("start", interfaces_hooks != NULL ?
2045
2078
                             interfaces_hooks : "", delay)){
2127
2160
  }
2128
2161
  
2129
2162
  /* If no interfaces were specified, make a list */
2130
 
  if(interfaces == NULL){
 
2163
  if(mc.interfaces == NULL){
2131
2164
    struct dirent **direntries;
2132
2165
    /* Look for any good interfaces */
2133
2166
    ret = scandir(sys_class_net, &direntries, good_interface,
2135
2168
    if(ret >= 1){
2136
2169
      /* Add all found interfaces to interfaces list */
2137
2170
      for(int i = 0; i < ret; ++i){
2138
 
        ret_errno = argz_add(&interfaces, &interfaces_size,
 
2171
        ret_errno = argz_add(&mc.interfaces, &mc.interfaces_size,
2139
2172
                             direntries[i]->d_name);
2140
2173
        if(ret_errno != 0){
 
2174
          errno = ret_errno;
2141
2175
          perror_plus("argz_add");
2142
2176
          continue;
2143
2177
        }
2155
2189
    }
2156
2190
  }
2157
2191
  
2158
 
  /* If we only got one interface, explicitly use only that one */
2159
 
  if(argz_count(interfaces, interfaces_size) == 1){
2160
 
    if(debug){
2161
 
      fprintf_plus(stderr, "Using only interface \"%s\"\n",
2162
 
                   interfaces);
2163
 
    }
2164
 
    if_index = (AvahiIfIndex)if_nametoindex(interfaces);
2165
 
  }
2166
 
  
2167
 
  /* Bring up interfaces which are down */
2168
 
  if(not (argz_count(interfaces, interfaces_size) == 1
2169
 
          and strcmp(interfaces, "none") == 0)){
 
2192
  /* Bring up interfaces which are down, and remove any "none"s */
 
2193
  {
2170
2194
    char *interface = NULL;
2171
 
    while((interface = argz_next(interfaces, interfaces_size,
 
2195
    while((interface = argz_next(mc.interfaces, mc.interfaces_size,
2172
2196
                                 interface))){
 
2197
      /* If interface name is "none", stop bringing up interfaces.
 
2198
         Also remove all instances of "none" from the list */
 
2199
      if(strcmp(interface, "none") == 0){
 
2200
        argz_delete(&mc.interfaces, &mc.interfaces_size,
 
2201
                    interface);
 
2202
        interface = NULL;
 
2203
        while((interface = argz_next(mc.interfaces,
 
2204
                                     mc.interfaces_size, interface))){
 
2205
          if(strcmp(interface, "none") == 0){
 
2206
            argz_delete(&mc.interfaces, &mc.interfaces_size,
 
2207
                        interface);
 
2208
            interface = NULL;
 
2209
          }
 
2210
        }
 
2211
        break;
 
2212
      }
2173
2213
      bool interface_was_up = interface_is_up(interface);
2174
2214
      ret = bring_up_interface(interface, delay);
2175
2215
      if(not interface_was_up){
2180
2220
          ret_errno = argz_add(&interfaces_to_take_down,
2181
2221
                               &interfaces_to_take_down_size,
2182
2222
                               interface);
 
2223
          if(ret_errno != 0){
 
2224
            errno = ret_errno;
 
2225
            perror_plus("argz_add");
 
2226
          }
2183
2227
        }
2184
2228
      }
2185
2229
    }
2186
 
    free(interfaces);
2187
 
    interfaces = NULL;
2188
 
    interfaces_size = 0;
2189
2230
    if(debug and (interfaces_to_take_down == NULL)){
2190
2231
      fprintf_plus(stderr, "No interfaces were brought up\n");
2191
2232
    }
2192
2233
  }
2193
2234
  
 
2235
  /* If we only got one interface, explicitly use only that one */
 
2236
  if(argz_count(mc.interfaces, mc.interfaces_size) == 1){
 
2237
    if(debug){
 
2238
      fprintf_plus(stderr, "Using only interface \"%s\"\n",
 
2239
                   mc.interfaces);
 
2240
    }
 
2241
    if_index = (AvahiIfIndex)if_nametoindex(mc.interfaces);
 
2242
  }
 
2243
  
2194
2244
  if(quit_now){
2195
2245
    goto end;
2196
2246
  }
2254
2304
      exitcode = EX_USAGE;
2255
2305
      goto end;
2256
2306
    }
2257
 
  
 
2307
    
2258
2308
    if(quit_now){
2259
2309
      goto end;
2260
2310
    }
2290
2340
        fprintf_plus(stderr, "Retrying in %d seconds\n",
2291
2341
                     (int)retry_interval);
2292
2342
      }
2293
 
      sleep((int)retry_interval);
 
2343
      sleep((unsigned int)retry_interval);
2294
2344
    }
2295
2345
    
2296
2346
    if (not quit_now){
2369
2419
  }
2370
2420
  
2371
2421
  /* Cleanup things */
 
2422
  free(mc.interfaces);
 
2423
  
2372
2424
  if(sb != NULL)
2373
2425
    avahi_s_service_browser_free(sb);
2374
2426