/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: 2013-10-20 21:04:35 UTC
  • Revision ID: teddy@recompile.se-20131020210435-jc94ovr5i4sve16r
* Makefile (install-client-nokey): Install "mandos-change-keytype".
  (uninstall-client): Uninstall "mandos-change-keytype".
* mandos (priority): Changed back default GnuTLS priority string to
                     "SECURE256:!CTYPE-X.509:+CTYPE-OPENPGP".
* mandos-change-keytype: New script which, assuming the Mandos server
                         is responding, will generate a new key with
                         the new mandos-keygen default parameters
                         (currently 4096-bit RSA), and output a
                         replacement section for clients.conf on the
                         Mandos server.
* mandos-keygen: Bug fix: output password prompt even when redirecting
                 stdout.
* mandos-options.xml (priority): Document new default value.
* mandos.conf (priority): - '' -

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
/*
657
659
    return -1;
658
660
  }
659
661
  
 
662
  /* If the interface is specified and we have a list of interfaces */
 
663
  if(if_index != AVAHI_IF_UNSPEC and mc->interfaces != NULL){
 
664
    /* Check if the interface is one of the interfaces we are using */
 
665
    bool match = false;
 
666
    {
 
667
      char *interface = NULL;
 
668
      while((interface=argz_next(mc->interfaces, mc->interfaces_size,
 
669
                                 interface))){
 
670
        if(if_nametoindex(interface) == (unsigned int)if_index){
 
671
          match = true;
 
672
          break;
 
673
        }
 
674
      }
 
675
    }
 
676
    if(not match){
 
677
      /* This interface does not match any in the list, so we don't
 
678
         connect to the server */
 
679
      if(debug){
 
680
        char interface[IF_NAMESIZE];
 
681
        if(if_indextoname((unsigned int)if_index, interface) == NULL){
 
682
          perror_plus("if_indextoname");
 
683
        } else {
 
684
          fprintf_plus(stderr, "Skipping server on non-used interface"
 
685
                       " \"%s\"\n",
 
686
                       if_indextoname((unsigned int)if_index,
 
687
                                      interface));
 
688
        }
 
689
      }
 
690
      return -1;
 
691
    }
 
692
  }
 
693
  
660
694
  ret = init_gnutls_session(&session, mc);
661
695
  if(ret != 0){
662
696
    return -1;
1755
1789
int main(int argc, char *argv[]){
1756
1790
  mandos_context mc = { .server = NULL, .dh_bits = 1024,
1757
1791
                        .priority = "SECURE256:!CTYPE-X.509:"
1758
 
                        "+CTYPE-OPENPGP", .current_server = NULL };
 
1792
                        "+CTYPE-OPENPGP", .current_server = NULL, 
 
1793
                        .interfaces = NULL, .interfaces_size = 0 };
1759
1794
  AvahiSServiceBrowser *sb = NULL;
1760
1795
  error_t ret_errno;
1761
1796
  int ret;
1762
1797
  intmax_t tmpmax;
1763
1798
  char *tmp;
1764
1799
  int exitcode = EXIT_SUCCESS;
1765
 
  char *interfaces = NULL;
1766
 
  size_t interfaces_size = 0;
1767
1800
  char *interfaces_to_take_down = NULL;
1768
1801
  size_t interfaces_to_take_down_size = 0;
1769
1802
  char tempdir[] = "/tmp/mandosXXXXXX";
1869
1902
        connect_to = arg;
1870
1903
        break;
1871
1904
      case 'i':                 /* --interface */
1872
 
        ret_errno = argz_add_sep(&interfaces, &interfaces_size, arg,
1873
 
                                 (int)',');
 
1905
        ret_errno = argz_add_sep(&mc.interfaces, &mc.interfaces_size,
 
1906
                                 arg, (int)',');
1874
1907
        if(ret_errno != 0){
1875
1908
          argp_error(state, "%s", strerror(ret_errno));
1876
1909
        }
2003
2036
      }
2004
2037
    
2005
2038
      /* Lower privileges */
2006
 
      errno = 0;
2007
 
      ret = seteuid(uid);
2008
 
      if(ret == -1){
2009
 
        perror_plus("seteuid");
2010
 
      }
 
2039
      lower_privileges();
2011
2040
    }
2012
2041
  }
2013
2042
  
2014
 
  /* Remove empty interface names */
 
2043
  /* Remove invalid interface names (except "none") */
2015
2044
  {
2016
2045
    char *interface = NULL;
2017
 
    while((interface = argz_next(interfaces, interfaces_size,
 
2046
    while((interface = argz_next(mc.interfaces, mc.interfaces_size,
2018
2047
                                 interface))){
2019
 
      if(if_nametoindex(interface) == 0){
2020
 
        if(interface[0] != '\0' and strcmp(interface, "none") != 0){
 
2048
      if(strcmp(interface, "none") != 0
 
2049
         and if_nametoindex(interface) == 0){
 
2050
        if(interface[0] != '\0'){
2021
2051
          fprintf_plus(stderr, "Not using nonexisting interface"
2022
2052
                       " \"%s\"\n", interface);
2023
2053
        }
2024
 
        argz_delete(&interfaces, &interfaces_size, interface);
 
2054
        argz_delete(&mc.interfaces, &mc.interfaces_size, interface);
2025
2055
        interface = NULL;
2026
2056
      }
2027
2057
    }
2029
2059
  
2030
2060
  /* Run network hooks */
2031
2061
  {
2032
 
    
2033
 
    if(interfaces != NULL){
2034
 
      interfaces_hooks = malloc(interfaces_size);
 
2062
    if(mc.interfaces != NULL){
 
2063
      interfaces_hooks = malloc(mc.interfaces_size);
2035
2064
      if(interfaces_hooks == NULL){
2036
2065
        perror_plus("malloc");
2037
2066
        goto end;
2038
2067
      }
2039
 
      memcpy(interfaces_hooks, interfaces, interfaces_size);
2040
 
      interfaces_hooks_size = interfaces_size;
 
2068
      memcpy(interfaces_hooks, mc.interfaces, mc.interfaces_size);
 
2069
      interfaces_hooks_size = mc.interfaces_size;
2041
2070
      argz_stringify(interfaces_hooks, interfaces_hooks_size,
2042
2071
                     (int)',');
2043
2072
    }
2127
2156
  }
2128
2157
  
2129
2158
  /* If no interfaces were specified, make a list */
2130
 
  if(interfaces == NULL){
 
2159
  if(mc.interfaces == NULL){
2131
2160
    struct dirent **direntries;
2132
2161
    /* Look for any good interfaces */
2133
2162
    ret = scandir(sys_class_net, &direntries, good_interface,
2135
2164
    if(ret >= 1){
2136
2165
      /* Add all found interfaces to interfaces list */
2137
2166
      for(int i = 0; i < ret; ++i){
2138
 
        ret_errno = argz_add(&interfaces, &interfaces_size,
 
2167
        ret_errno = argz_add(&mc.interfaces, &mc.interfaces_size,
2139
2168
                             direntries[i]->d_name);
2140
2169
        if(ret_errno != 0){
2141
2170
          perror_plus("argz_add");
2155
2184
    }
2156
2185
  }
2157
2186
  
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)){
 
2187
  /* Bring up interfaces which are down, and remove any "none"s */
 
2188
  {
2170
2189
    char *interface = NULL;
2171
 
    while((interface = argz_next(interfaces, interfaces_size,
 
2190
    while((interface = argz_next(mc.interfaces, mc.interfaces_size,
2172
2191
                                 interface))){
 
2192
      /* If interface name is "none", stop bringing up interfaces.
 
2193
         Also remove all instances of "none" from the list */
 
2194
      if(strcmp(interface, "none") == 0){
 
2195
        argz_delete(&mc.interfaces, &mc.interfaces_size,
 
2196
                    interface);
 
2197
        interface = NULL;
 
2198
        while((interface = argz_next(mc.interfaces,
 
2199
                                     mc.interfaces_size, interface))){
 
2200
          if(strcmp(interface, "none") == 0){
 
2201
            argz_delete(&mc.interfaces, &mc.interfaces_size,
 
2202
                        interface);
 
2203
            interface = NULL;
 
2204
          }
 
2205
        }
 
2206
        break;
 
2207
      }
2173
2208
      bool interface_was_up = interface_is_up(interface);
2174
2209
      ret = bring_up_interface(interface, delay);
2175
2210
      if(not interface_was_up){
2183
2218
        }
2184
2219
      }
2185
2220
    }
2186
 
    free(interfaces);
2187
 
    interfaces = NULL;
2188
 
    interfaces_size = 0;
2189
2221
    if(debug and (interfaces_to_take_down == NULL)){
2190
2222
      fprintf_plus(stderr, "No interfaces were brought up\n");
2191
2223
    }
2192
2224
  }
2193
2225
  
 
2226
  /* If we only got one interface, explicitly use only that one */
 
2227
  if(argz_count(mc.interfaces, mc.interfaces_size) == 1){
 
2228
    if(debug){
 
2229
      fprintf_plus(stderr, "Using only interface \"%s\"\n",
 
2230
                   mc.interfaces);
 
2231
    }
 
2232
    if_index = (AvahiIfIndex)if_nametoindex(mc.interfaces);
 
2233
  }
 
2234
  
2194
2235
  if(quit_now){
2195
2236
    goto end;
2196
2237
  }
2254
2295
      exitcode = EX_USAGE;
2255
2296
      goto end;
2256
2297
    }
2257
 
  
 
2298
    
2258
2299
    if(quit_now){
2259
2300
      goto end;
2260
2301
    }
2290
2331
        fprintf_plus(stderr, "Retrying in %d seconds\n",
2291
2332
                     (int)retry_interval);
2292
2333
      }
2293
 
      sleep((int)retry_interval);
 
2334
      sleep((unsigned int)retry_interval);
2294
2335
    }
2295
2336
    
2296
2337
    if (not quit_now){
2369
2410
  }
2370
2411
  
2371
2412
  /* Cleanup things */
 
2413
  free(mc.interfaces);
 
2414
  
2372
2415
  if(sb != NULL)
2373
2416
    avahi_s_service_browser_free(sb);
2374
2417