/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release

« back to all changes in this revision

Viewing changes to plugins.d/mandos-client.c

  • Committer: Teddy Hogeborn
  • Date: 2016-03-04 20:50:45 UTC
  • mto: (237.7.594 trunk)
  • mto: This revision was merged to the branch mainline in revision 335.
  • Revision ID: teddy@recompile.se-20160304205045-jksfb2qzv9erkf21
Only use -fsanitize=... options if they work.

* Makefile (ALL_SANITIZE_OPTIONS): New.
  (SANITIZE): Loop through all known sanitizing options and only use
              those which do not give an error.

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-2015 Teddy Hogeborn
13
 
 * Copyright © 2008-2015 Björn Påhlsson
 
12
 * Copyright © 2008-2016 Teddy Hogeborn
 
13
 * Copyright © 2008-2016 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
47
47
                                   strtof(), abort() */
48
48
#include <stdbool.h>            /* bool, false, true */
49
49
#include <string.h>             /* strcmp(), strlen(), strerror(),
50
 
                                   asprintf(), strcpy() */
 
50
                                   asprintf(), strncpy() */
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() */
 
60
#include <fcntl.h>              /* open(), unlinkat(), AT_REMOVEDIR */
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,
 
65
#include <errno.h>              /* perror(), errno, EINTR, EINVAL,
 
66
                                   EAI_SYSTEM, ENETUNREACH,
 
67
                                   EHOSTUNREACH, ECONNREFUSED, EPROTO,
 
68
                                   EIO, ENOENT, ENXIO, ENOMEM, EISDIR,
 
69
                                   ENOTEMPTY,
66
70
                                   program_invocation_short_name */
67
71
#include <time.h>               /* nanosleep(), time(), sleep() */
68
72
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
513
517
  fprintf_plus(stderr, "GnuTLS: %s", string);
514
518
}
515
519
 
516
 
__attribute__((nonnull, warn_unused_result))
 
520
__attribute__((nonnull(1, 2, 4), warn_unused_result))
517
521
static int init_gnutls_global(const char *pubkeyfilename,
518
522
                              const char *seckeyfilename,
519
523
                              const char *dhparamsfilename,
525
529
    fprintf_plus(stderr, "Initializing GnuTLS\n");
526
530
  }
527
531
  
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
 
  
535
532
  if(debug){
536
533
    /* "Use a log level over 10 to enable all debugging options."
537
534
     * - GnuTLS manual
545
542
  if(ret != GNUTLS_E_SUCCESS){
546
543
    fprintf_plus(stderr, "GnuTLS memory error: %s\n",
547
544
                 safer_gnutls_strerror(ret));
548
 
    gnutls_global_deinit();
549
545
    return -1;
550
546
  }
551
547
  
755
751
 globalfail:
756
752
  
757
753
  gnutls_certificate_free_credentials(mc->cred);
758
 
  gnutls_global_deinit();
759
754
  gnutls_dh_params_deinit(mc->dh_params);
760
755
  return -1;
761
756
}
1223
1218
                    sizeof(struct sockaddr_in));
1224
1219
    }
1225
1220
    if(ret < 0){
1226
 
      if(errno == ENETUNREACH
 
1221
      if(((errno == ENETUNREACH) or (errno == EHOSTUNREACH))
1227
1222
         and if_index != AVAHI_IF_UNSPEC
1228
1223
         and connect_to == NULL
1229
1224
         and not route_added and
1637
1632
    errno = ret_errno;
1638
1633
    return false;
1639
1634
  }
1640
 
  strcpy(ifr->ifr_name, ifname);
 
1635
  strncpy(ifr->ifr_name, ifname, IF_NAMESIZE);
 
1636
  ifr->ifr_name[IF_NAMESIZE-1] = '\0'; /* NUL terminate */
1641
1637
  ret = ioctl(s, SIOCGIFFLAGS, ifr);
1642
1638
  if(ret == -1){
1643
1639
    if(debug){
1913
1909
      return;
1914
1910
    }
1915
1911
  }
1916
 
#ifdef __GLIBC__
1917
 
#if __GLIBC_PREREQ(2, 15)
1918
1912
  int numhooks = scandirat(hookdir_fd, ".", &direntries,
1919
1913
                           runnable_hook, alphasort);
1920
 
#else  /* not __GLIBC_PREREQ(2, 15) */
1921
 
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1922
 
                         alphasort);
1923
 
#endif  /* not __GLIBC_PREREQ(2, 15) */
1924
 
#else   /* not __GLIBC__ */
1925
 
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1926
 
                         alphasort);
1927
 
#endif  /* not __GLIBC__ */
1928
1914
  if(numhooks == -1){
1929
1915
    perror_plus("scandir");
1930
1916
    return;
2288
2274
 
2289
2275
int main(int argc, char *argv[]){
2290
2276
  mandos_context mc = { .server = NULL, .dh_bits = 0,
2291
 
                        .priority = "SECURE256:!CTYPE-X.509:"
2292
 
                        "+CTYPE-OPENPGP:!RSA", .current_server = NULL,
2293
 
                        .interfaces = NULL, .interfaces_size = 0 };
 
2277
                        .priority = "SECURE256:!CTYPE-X.509"
 
2278
                        ":+CTYPE-OPENPGP:!RSA:+SIGN-DSA-SHA256",
 
2279
                        .current_server = NULL, .interfaces = NULL,
 
2280
                        .interfaces_size = 0 };
2294
2281
  AvahiSServiceBrowser *sb = NULL;
2295
2282
  error_t ret_errno;
2296
2283
  int ret;
2974
2961
  
2975
2962
  if(gnutls_initialized){
2976
2963
    gnutls_certificate_free_credentials(mc.cred);
2977
 
    gnutls_global_deinit();
2978
2964
    gnutls_dh_params_deinit(mc.dh_params);
2979
2965
  }
2980
2966
  
3042
3028
  free(interfaces_to_take_down);
3043
3029
  free(interfaces_hooks);
3044
3030
  
 
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
  
3045
3087
  /* Removes the GPGME temp directory and all files inside */
3046
3088
  if(tempdir != NULL){
3047
 
    struct dirent **direntries = NULL;
3048
 
    int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY
3049
 
                                                  | O_NOFOLLOW
3050
 
                                                  | O_DIRECTORY
3051
 
                                                  | O_PATH));
3052
 
    if(tempdir_fd == -1){
3053
 
      perror_plus("open");
3054
 
    } else {
3055
 
#ifdef __GLIBC__
3056
 
#if __GLIBC_PREREQ(2, 15)
3057
 
      int numentries = scandirat(tempdir_fd, ".", &direntries,
3058
 
                                 notdotentries, alphasort);
3059
 
#else  /* not __GLIBC_PREREQ(2, 15) */
3060
 
      int numentries = scandir(tempdir, &direntries, notdotentries,
3061
 
                               alphasort);
3062
 
#endif  /* not __GLIBC_PREREQ(2, 15) */
3063
 
#else   /* not __GLIBC__ */
3064
 
      int numentries = scandir(tempdir, &direntries, notdotentries,
3065
 
                               alphasort);
3066
 
#endif  /* not __GLIBC__ */
3067
 
      if(numentries >= 0){
3068
 
        for(int i = 0; i < numentries; i++){
3069
 
          ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
3070
 
          if(ret == -1){
3071
 
            fprintf_plus(stderr, "unlinkat(open(\"%s\", O_RDONLY),"
3072
 
                         " \"%s\", 0): %s\n", tempdir,
3073
 
                         direntries[i]->d_name, strerror(errno));
3074
 
          }
3075
 
          free(direntries[i]);
3076
 
        }
3077
 
        
3078
 
        /* need to clean even if 0 because man page doesn't specify */
3079
 
        free(direntries);
3080
 
        if(numentries == -1){
3081
 
          perror_plus("scandir");
3082
 
        }
3083
 
        ret = rmdir(tempdir);
3084
 
        if(ret == -1 and errno != ENOENT){
3085
 
          perror_plus("rmdir");
3086
 
        }
3087
 
      }
3088
 
      close(tempdir_fd);
3089
 
    }
 
3089
    clean_dir_at(-1, tempdir, 0);
3090
3090
  }
3091
3091
  
3092
3092
  if(quit_now){