/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: 2015-07-09 20:32:52 UTC
  • Revision ID: teddy@recompile.se-20150709203252-u10p7trfsxc1a9yp
Install client Diffie-Hellman parameters into initramfs.

* debian/control (Package: mandos-client/Recommends): Added
  "gnutls-bin | openssl" (to generate DH parameters).
* debian/mandos-client.postinst (create_dh_params): New function
                                                    called on package
                                                    configure.
* plugins.d/mandos-client.c (main): Bug fix: Work around Debian bug
                                    #633582 also for the
                                    "client-dhparams.pem" file.

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-2016 Teddy Hogeborn
13
 
 * Copyright © 2008-2016 Björn Påhlsson
 
12
 * Copyright © 2008-2015 Teddy Hogeborn
 
13
 * Copyright © 2008-2015 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
46
46
#include <stdlib.h>             /* free(), EXIT_SUCCESS, srand(),
47
47
                                   strtof(), abort() */
48
48
#include <stdbool.h>            /* bool, false, true */
49
 
#include <string.h>             /* strcmp(), strlen(), strerror(),
50
 
                                   asprintf(), strncpy() */
 
49
#include <string.h>             /* memset(), strcmp(), strlen(),
 
50
                                   strerror(), asprintf(), strcpy() */
51
51
#include <sys/ioctl.h>          /* ioctl */
52
52
#include <sys/types.h>          /* socket(), inet_pton(), sockaddr,
53
53
                                   sockaddr_in6, PF_INET6,
57
57
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
58
58
                                   inet_pton(), connect(),
59
59
                                   getnameinfo() */
60
 
#include <fcntl.h>              /* open(), unlinkat(), AT_REMOVEDIR */
 
60
#include <fcntl.h>              /* open(), unlinkat() */
61
61
#include <dirent.h>             /* opendir(), struct dirent, readdir()
62
62
                                 */
63
63
#include <inttypes.h>           /* PRIu16, PRIdMAX, intmax_t,
64
64
                                   strtoimax() */
65
 
#include <errno.h>              /* perror(), errno, EINTR, EINVAL,
66
 
                                   EAI_SYSTEM, ENETUNREACH,
67
 
                                   EHOSTUNREACH, ECONNREFUSED, EPROTO,
68
 
                                   EIO, ENOENT, ENXIO, ENOMEM, EISDIR,
69
 
                                   ENOTEMPTY,
 
65
#include <errno.h>              /* perror(), errno,
70
66
                                   program_invocation_short_name */
71
67
#include <time.h>               /* nanosleep(), time(), sleep() */
72
68
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
309
305
      return false;
310
306
    }
311
307
    
312
 
    ret = close(fd);
 
308
    ret = (int)TEMP_FAILURE_RETRY(close(fd));
313
309
    if(ret == -1){
314
310
      perror_plus("close");
315
311
    }
517
513
  fprintf_plus(stderr, "GnuTLS: %s", string);
518
514
}
519
515
 
520
 
__attribute__((nonnull(1, 2, 4), warn_unused_result))
 
516
__attribute__((nonnull, warn_unused_result))
521
517
static int init_gnutls_global(const char *pubkeyfilename,
522
518
                              const char *seckeyfilename,
523
519
                              const char *dhparamsfilename,
529
525
    fprintf_plus(stderr, "Initializing GnuTLS\n");
530
526
  }
531
527
  
 
528
  ret = gnutls_global_init();
 
529
  if(ret != GNUTLS_E_SUCCESS){
 
530
    fprintf_plus(stderr, "GnuTLS global_init: %s\n",
 
531
                 safer_gnutls_strerror(ret));
 
532
    return -1;
 
533
  }
 
534
  
532
535
  if(debug){
533
536
    /* "Use a log level over 10 to enable all debugging options."
534
537
     * - GnuTLS manual
542
545
  if(ret != GNUTLS_E_SUCCESS){
543
546
    fprintf_plus(stderr, "GnuTLS memory error: %s\n",
544
547
                 safer_gnutls_strerror(ret));
 
548
    gnutls_global_deinit();
545
549
    return -1;
546
550
  }
547
551
  
751
755
 globalfail:
752
756
  
753
757
  gnutls_certificate_free_credentials(mc->cred);
 
758
  gnutls_global_deinit();
754
759
  gnutls_dh_params_deinit(mc->dh_params);
755
760
  return -1;
756
761
}
926
931
      perror_plus("dup2(devnull, STDIN_FILENO)");
927
932
      _exit(EX_OSERR);
928
933
    }
929
 
    ret = close(devnull);
 
934
    ret = (int)TEMP_FAILURE_RETRY(close(devnull));
930
935
    if(ret == -1){
931
936
      perror_plus("close");
932
937
      _exit(EX_OSERR);
949
954
                                                   helper, O_RDONLY));
950
955
    if(helper_fd == -1){
951
956
      perror_plus("openat");
952
 
      close(helperdir_fd);
953
957
      _exit(EX_UNAVAILABLE);
954
958
    }
955
 
    close(helperdir_fd);
 
959
    TEMP_FAILURE_RETRY(close(helperdir_fd));
956
960
#ifdef __GNUC__
957
961
#pragma GCC diagnostic push
958
962
#pragma GCC diagnostic ignored "-Wcast-qual"
1126
1130
    goto mandos_end;
1127
1131
  }
1128
1132
  
 
1133
  memset(&to, 0, sizeof(to));
1129
1134
  if(af == AF_INET6){
1130
 
    struct sockaddr_in6 *to6 = (struct sockaddr_in6 *)&to;
1131
 
    *to6 = (struct sockaddr_in6){ .sin6_family = (sa_family_t)af };
1132
 
    ret = inet_pton(af, ip, &to6->sin6_addr);
 
1135
    ((struct sockaddr_in6 *)&to)->sin6_family = (sa_family_t)af;
 
1136
    ret = inet_pton(af, ip, &((struct sockaddr_in6 *)&to)->sin6_addr);
1133
1137
  } else {                      /* IPv4 */
1134
 
    struct sockaddr_in *to4 = (struct sockaddr_in *)&to;
1135
 
    *to4 = (struct sockaddr_in){ .sin_family = (sa_family_t)af };
1136
 
    ret = inet_pton(af, ip, &to4->sin_addr);
 
1138
    ((struct sockaddr_in *)&to)->sin_family = (sa_family_t)af;
 
1139
    ret = inet_pton(af, ip, &((struct sockaddr_in *)&to)->sin_addr);
1137
1140
  }
1138
1141
  if(ret < 0 ){
1139
1142
    int e = errno;
1218
1221
                    sizeof(struct sockaddr_in));
1219
1222
    }
1220
1223
    if(ret < 0){
1221
 
      if(((errno == ENETUNREACH) or (errno == EHOSTUNREACH))
 
1224
      if(errno == ENETUNREACH
1222
1225
         and if_index != AVAHI_IF_UNSPEC
1223
1226
         and connect_to == NULL
1224
1227
         and not route_added and
1462
1465
    free(decrypted_buffer);
1463
1466
    free(buffer);
1464
1467
    if(tcp_sd >= 0){
1465
 
      ret = close(tcp_sd);
 
1468
      ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
1466
1469
    }
1467
1470
    if(ret == -1){
1468
1471
      if(e == 0){
1632
1635
    errno = ret_errno;
1633
1636
    return false;
1634
1637
  }
1635
 
  strncpy(ifr->ifr_name, ifname, IF_NAMESIZE);
1636
 
  ifr->ifr_name[IF_NAMESIZE-1] = '\0'; /* NUL terminate */
 
1638
  strcpy(ifr->ifr_name, ifname);
1637
1639
  ret = ioctl(s, SIOCGIFFLAGS, ifr);
1638
1640
  if(ret == -1){
1639
1641
    if(debug){
1909
1911
      return;
1910
1912
    }
1911
1913
  }
 
1914
#ifdef __GLIBC__
 
1915
#if __GLIBC_PREREQ(2, 15)
1912
1916
  int numhooks = scandirat(hookdir_fd, ".", &direntries,
1913
1917
                           runnable_hook, alphasort);
 
1918
#else  /* not __GLIBC_PREREQ(2, 15) */
 
1919
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
1920
                         alphasort);
 
1921
#endif  /* not __GLIBC_PREREQ(2, 15) */
 
1922
#else   /* not __GLIBC__ */
 
1923
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
1924
                         alphasort);
 
1925
#endif  /* not __GLIBC__ */
1914
1926
  if(numhooks == -1){
1915
1927
    perror_plus("scandir");
1916
1928
    return;
1998
2010
        perror_plus("openat");
1999
2011
        _exit(EXIT_FAILURE);
2000
2012
      }
2001
 
      if(close(hookdir_fd) == -1){
 
2013
      if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
2002
2014
        perror_plus("close");
2003
2015
        _exit(EXIT_FAILURE);
2004
2016
      }
2007
2019
        perror_plus("dup2(devnull, STDIN_FILENO)");
2008
2020
        _exit(EX_OSERR);
2009
2021
      }
2010
 
      ret = close(devnull);
 
2022
      ret = (int)TEMP_FAILURE_RETRY(close(devnull));
2011
2023
      if(ret == -1){
2012
2024
        perror_plus("close");
2013
2025
        _exit(EX_OSERR);
2062
2074
    free(direntry);
2063
2075
  }
2064
2076
  free(direntries);
2065
 
  if(close(hookdir_fd) == -1){
 
2077
  if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
2066
2078
    perror_plus("close");
2067
2079
  } else {
2068
2080
    hookdir_fd = -1;
2108
2120
    }
2109
2121
    
2110
2122
    if(quit_now){
2111
 
      ret = close(sd);
 
2123
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
2112
2124
      if(ret == -1){
2113
2125
        perror_plus("close");
2114
2126
      }
2164
2176
    }
2165
2177
    
2166
2178
    /* Close the socket */
2167
 
    ret = close(sd);
 
2179
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
2168
2180
    if(ret == -1){
2169
2181
      perror_plus("close");
2170
2182
    }
2252
2264
    }
2253
2265
    
2254
2266
    /* Close the socket */
2255
 
    int ret = close(sd);
 
2267
    int ret = (int)TEMP_FAILURE_RETRY(close(sd));
2256
2268
    if(ret == -1){
2257
2269
      perror_plus("close");
2258
2270
    }
2274
2286
 
2275
2287
int main(int argc, char *argv[]){
2276
2288
  mandos_context mc = { .server = NULL, .dh_bits = 0,
2277
 
                        .priority = "SECURE256:!CTYPE-X.509"
2278
 
                        ":+CTYPE-OPENPGP:!RSA:+SIGN-DSA-SHA256",
2279
 
                        .current_server = NULL, .interfaces = NULL,
2280
 
                        .interfaces_size = 0 };
 
2289
                        .priority = "SECURE256:!CTYPE-X.509:"
 
2290
                        "+CTYPE-OPENPGP:!RSA", .current_server = NULL,
 
2291
                        .interfaces = NULL, .interfaces_size = 0 };
2281
2292
  AvahiSServiceBrowser *sb = NULL;
2282
2293
  error_t ret_errno;
2283
2294
  int ret;
2510
2521
              }
2511
2522
            }
2512
2523
          }
2513
 
          close(seckey_fd);
 
2524
          TEMP_FAILURE_RETRY(close(seckey_fd));
2514
2525
        }
2515
2526
      }
2516
2527
      
2531
2542
              }
2532
2543
            }
2533
2544
          }
2534
 
          close(pubkey_fd);
 
2545
          TEMP_FAILURE_RETRY(close(pubkey_fd));
2535
2546
        }
2536
2547
      }
2537
2548
      
2538
 
      if(dh_params_file != NULL
2539
 
         and strcmp(dh_params_file, PATHDIR "/dhparams.pem" ) == 0){
 
2549
      if(strcmp(dh_params_file, PATHDIR "/client-dhparams.pem" )
 
2550
         == 0){
2540
2551
        int dhparams_fd = open(dh_params_file, O_RDONLY);
2541
2552
        if(dhparams_fd == -1){
2542
2553
          perror_plus("open");
2553
2564
              }
2554
2565
            }
2555
2566
          }
2556
 
          close(dhparams_fd);
 
2567
          TEMP_FAILURE_RETRY(close(dhparams_fd));
2557
2568
        }
2558
2569
      }
2559
2570
      
2736
2747
      errno = bring_up_interface(interface, delay);
2737
2748
      if(not interface_was_up){
2738
2749
        if(errno != 0){
2739
 
          fprintf_plus(stderr, "Failed to bring up interface \"%s\":"
2740
 
                       " %s\n", interface, strerror(errno));
 
2750
          perror_plus("Failed to bring up interface");
2741
2751
        } else {
2742
2752
          errno = argz_add(&interfaces_to_take_down,
2743
2753
                           &interfaces_to_take_down_size,
2961
2971
  
2962
2972
  if(gnutls_initialized){
2963
2973
    gnutls_certificate_free_credentials(mc.cred);
 
2974
    gnutls_global_deinit();
2964
2975
    gnutls_dh_params_deinit(mc.dh_params);
2965
2976
  }
2966
2977
  
3028
3039
  free(interfaces_to_take_down);
3029
3040
  free(interfaces_hooks);
3030
3041
  
3031
 
  void clean_dir_at(int base, const char * const dirname,
3032
 
                    uintmax_t level){
3033
 
    struct dirent **direntries = NULL;
3034
 
    int dret;
3035
 
    int dir_fd = (int)TEMP_FAILURE_RETRY(openat(base, dirname,
3036
 
                                                O_RDONLY
3037
 
                                                | O_NOFOLLOW
3038
 
                                                | O_DIRECTORY
3039
 
                                                | O_PATH));
3040
 
    if(dir_fd == -1){
3041
 
      perror_plus("open");
3042
 
    }
3043
 
    int numentries = scandirat(dir_fd, ".", &direntries,
3044
 
                               notdotentries, alphasort);
3045
 
    if(numentries >= 0){
3046
 
      for(int i = 0; i < numentries; i++){
3047
 
        if(debug){
3048
 
          fprintf_plus(stderr, "Unlinking \"%s/%s\"\n",
3049
 
                       dirname, direntries[i]->d_name);
3050
 
        }
3051
 
        dret = unlinkat(dir_fd, direntries[i]->d_name, 0);
3052
 
        if(dret == -1){
3053
 
          if(errno == EISDIR){
3054
 
              dret = unlinkat(dir_fd, direntries[i]->d_name,
3055
 
                              AT_REMOVEDIR);
3056
 
          }         
3057
 
          if((dret == -1) and (errno == ENOTEMPTY)
3058
 
             and (strcmp(direntries[i]->d_name, "private-keys-v1.d")
3059
 
                  == 0) and (level == 0)){
3060
 
            /* Recurse only in this special case */
3061
 
            clean_dir_at(dir_fd, direntries[i]->d_name, level+1);
3062
 
            dret = 0;
3063
 
          }
3064
 
          if(dret == -1){
3065
 
            fprintf_plus(stderr, "unlink(\"%s/%s\"): %s\n", dirname,
3066
 
                         direntries[i]->d_name, strerror(errno));
3067
 
          }
3068
 
        }
3069
 
        free(direntries[i]);
3070
 
      }
3071
 
      
3072
 
      /* need to clean even if 0 because man page doesn't specify */
3073
 
      free(direntries);
3074
 
      if(numentries == -1){
3075
 
        perror_plus("scandirat");
3076
 
      }
3077
 
      dret = unlinkat(base, dirname, AT_REMOVEDIR);
3078
 
      if(dret == -1 and errno != ENOENT){
3079
 
        perror_plus("rmdir");
3080
 
      }
3081
 
    } else {
3082
 
      perror_plus("scandirat");
3083
 
    }
3084
 
    close(dir_fd);
3085
 
  }
3086
 
  
3087
3042
  /* Removes the GPGME temp directory and all files inside */
3088
3043
  if(tempdir != NULL){
3089
 
    clean_dir_at(-1, tempdir, 0);
 
3044
    struct dirent **direntries = NULL;
 
3045
    int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY
 
3046
                                                  | O_NOFOLLOW
 
3047
                                                  | O_DIRECTORY
 
3048
                                                  | O_PATH));
 
3049
    if(tempdir_fd == -1){
 
3050
      perror_plus("open");
 
3051
    } else {
 
3052
#ifdef __GLIBC__
 
3053
#if __GLIBC_PREREQ(2, 15)
 
3054
      int numentries = scandirat(tempdir_fd, ".", &direntries,
 
3055
                                 notdotentries, alphasort);
 
3056
#else  /* not __GLIBC_PREREQ(2, 15) */
 
3057
      int numentries = scandir(tempdir, &direntries, notdotentries,
 
3058
                               alphasort);
 
3059
#endif  /* not __GLIBC_PREREQ(2, 15) */
 
3060
#else   /* not __GLIBC__ */
 
3061
      int numentries = scandir(tempdir, &direntries, notdotentries,
 
3062
                               alphasort);
 
3063
#endif  /* not __GLIBC__ */
 
3064
      if(numentries >= 0){
 
3065
        for(int i = 0; i < numentries; i++){
 
3066
          ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
 
3067
          if(ret == -1){
 
3068
            fprintf_plus(stderr, "unlinkat(open(\"%s\", O_RDONLY),"
 
3069
                         " \"%s\", 0): %s\n", tempdir,
 
3070
                         direntries[i]->d_name, strerror(errno));
 
3071
          }
 
3072
          free(direntries[i]);
 
3073
        }
 
3074
        
 
3075
        /* need to clean even if 0 because man page doesn't specify */
 
3076
        free(direntries);
 
3077
        if(numentries == -1){
 
3078
          perror_plus("scandir");
 
3079
        }
 
3080
        ret = rmdir(tempdir);
 
3081
        if(ret == -1 and errno != ENOENT){
 
3082
          perror_plus("rmdir");
 
3083
        }
 
3084
      }
 
3085
      TEMP_FAILURE_RETRY(close(tempdir_fd));
 
3086
    }
3090
3087
  }
3091
3088
  
3092
3089
  if(quit_now){