/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: 2016-07-03 03:32:28 UTC
  • Revision ID: teddy@recompile.se-20160703033228-cafpnnfs53kdg52g
Change all http:// URLs to https:// wherever possible.

* INSTALL (Prerequisites/Libraries/Mandos Server): Change "http://" to
  "https://" wherever possible.
* Makefile (FORTIFY): - '' -
* mandos.xml (SEE ALSO): - '' -
* plugin-runner.c (main): - '' -
* plugins.d/mandos-client.c (start_mandos_communication): - '' -
* plugins.d/mandos-client.xml (SEE ALSO): - '' -

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
}
822
817
 
823
818
/* Set effective uid to 0, return errno */
824
819
__attribute__((warn_unused_result))
825
 
error_t raise_privileges(void){
826
 
  error_t old_errno = errno;
827
 
  error_t ret_errno = 0;
 
820
int raise_privileges(void){
 
821
  int old_errno = errno;
 
822
  int ret = 0;
828
823
  if(seteuid(0) == -1){
829
 
    ret_errno = errno;
 
824
    ret = errno;
830
825
  }
831
826
  errno = old_errno;
832
 
  return ret_errno;
 
827
  return ret;
833
828
}
834
829
 
835
830
/* Set effective and real user ID to 0.  Return errno. */
836
831
__attribute__((warn_unused_result))
837
 
error_t raise_privileges_permanently(void){
838
 
  error_t old_errno = errno;
839
 
  error_t ret_errno = raise_privileges();
840
 
  if(ret_errno != 0){
 
832
int raise_privileges_permanently(void){
 
833
  int old_errno = errno;
 
834
  int ret = raise_privileges();
 
835
  if(ret != 0){
841
836
    errno = old_errno;
842
 
    return ret_errno;
 
837
    return ret;
843
838
  }
844
839
  if(setuid(0) == -1){
845
 
    ret_errno = errno;
 
840
    ret = errno;
846
841
  }
847
842
  errno = old_errno;
848
 
  return ret_errno;
 
843
  return ret;
849
844
}
850
845
 
851
846
/* Set effective user ID to unprivileged saved user ID */
852
847
__attribute__((warn_unused_result))
853
 
error_t lower_privileges(void){
854
 
  error_t old_errno = errno;
855
 
  error_t ret_errno = 0;
 
848
int lower_privileges(void){
 
849
  int old_errno = errno;
 
850
  int ret = 0;
856
851
  if(seteuid(uid) == -1){
857
 
    ret_errno = errno;
 
852
    ret = errno;
858
853
  }
859
854
  errno = old_errno;
860
 
  return ret_errno;
 
855
  return ret;
861
856
}
862
857
 
863
858
/* Lower privileges permanently */
864
859
__attribute__((warn_unused_result))
865
 
error_t lower_privileges_permanently(void){
866
 
  error_t old_errno = errno;
867
 
  error_t ret_errno = 0;
 
860
int lower_privileges_permanently(void){
 
861
  int old_errno = errno;
 
862
  int ret = 0;
868
863
  if(setuid(uid) == -1){
869
 
    ret_errno = errno;
 
864
    ret = errno;
870
865
  }
871
866
  errno = old_errno;
872
 
  return ret_errno;
 
867
  return ret;
873
868
}
874
869
 
875
870
/* Helper function to add_local_route() and delete_local_route() */
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
1242
1237
           with an explicit route added with the server's address.
1243
1238
           
1244
1239
           Avahi bug reference:
1245
 
           http://lists.freedesktop.org/archives/avahi/2010-February/001833.html
 
1240
           https://lists.freedesktop.org/archives/avahi/2010-February/001833.html
1246
1241
           https://bugs.debian.org/587961
1247
1242
        */
1248
1243
        if(debug){
1428
1423
                                               &decrypted_buffer, mc);
1429
1424
    if(decrypted_buffer_size >= 0){
1430
1425
      
 
1426
      clearerr(stdout);
1431
1427
      written = 0;
1432
1428
      while(written < (size_t) decrypted_buffer_size){
1433
1429
        if(quit_now){
1449
1445
        }
1450
1446
        written += (size_t)ret;
1451
1447
      }
 
1448
      ret = fflush(stdout);
 
1449
      if(ret != 0){
 
1450
        int e = errno;
 
1451
        if(debug){
 
1452
          fprintf_plus(stderr, "Error writing encrypted data: %s\n",
 
1453
                       strerror(errno));
 
1454
        }
 
1455
        errno = e;
 
1456
        goto mandos_end;
 
1457
      }
1452
1458
      retval = 0;
1453
1459
    }
1454
1460
  }
1628
1634
__attribute__((nonnull, warn_unused_result))
1629
1635
bool get_flags(const char *ifname, struct ifreq *ifr){
1630
1636
  int ret;
1631
 
  error_t ret_errno;
 
1637
  int old_errno;
1632
1638
  
1633
1639
  int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1634
1640
  if(s < 0){
1635
 
    ret_errno = errno;
 
1641
    old_errno = errno;
1636
1642
    perror_plus("socket");
1637
 
    errno = ret_errno;
 
1643
    errno = old_errno;
1638
1644
    return false;
1639
1645
  }
1640
 
  strcpy(ifr->ifr_name, ifname);
 
1646
  strncpy(ifr->ifr_name, ifname, IF_NAMESIZE);
 
1647
  ifr->ifr_name[IF_NAMESIZE-1] = '\0'; /* NUL terminate */
1641
1648
  ret = ioctl(s, SIOCGIFFLAGS, ifr);
1642
1649
  if(ret == -1){
1643
1650
    if(debug){
1644
 
      ret_errno = errno;
 
1651
      old_errno = errno;
1645
1652
      perror_plus("ioctl SIOCGIFFLAGS");
1646
 
      errno = ret_errno;
 
1653
      errno = old_errno;
1647
1654
    }
1648
1655
    return false;
1649
1656
  }
1913
1920
      return;
1914
1921
    }
1915
1922
  }
1916
 
#ifdef __GLIBC__
1917
 
#if __GLIBC_PREREQ(2, 15)
1918
1923
  int numhooks = scandirat(hookdir_fd, ".", &direntries,
1919
1924
                           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
1925
  if(numhooks == -1){
1929
1926
    perror_plus("scandir");
1930
1927
    return;
2085
2082
}
2086
2083
 
2087
2084
__attribute__((nonnull, warn_unused_result))
2088
 
error_t bring_up_interface(const char *const interface,
2089
 
                           const float delay){
2090
 
  error_t old_errno = errno;
 
2085
int bring_up_interface(const char *const interface,
 
2086
                       const float delay){
 
2087
  int old_errno = errno;
2091
2088
  int ret;
2092
2089
  struct ifreq network;
2093
2090
  unsigned int if_index = if_nametoindex(interface);
2103
2100
  }
2104
2101
  
2105
2102
  if(not interface_is_up(interface)){
2106
 
    error_t ret_errno = 0, ioctl_errno = 0;
 
2103
    int ret_errno = 0;
 
2104
    int ioctl_errno = 0;
2107
2105
    if(not get_flags(interface, &network)){
2108
2106
      ret_errno = errno;
2109
2107
      fprintf_plus(stderr, "Failed to get flags for interface "
2212
2210
}
2213
2211
 
2214
2212
__attribute__((nonnull, warn_unused_result))
2215
 
error_t take_down_interface(const char *const interface){
2216
 
  error_t old_errno = errno;
 
2213
int take_down_interface(const char *const interface){
 
2214
  int old_errno = errno;
2217
2215
  struct ifreq network;
2218
2216
  unsigned int if_index = if_nametoindex(interface);
2219
2217
  if(if_index == 0){
2222
2220
    return ENXIO;
2223
2221
  }
2224
2222
  if(interface_is_up(interface)){
2225
 
    error_t ret_errno = 0, ioctl_errno = 0;
 
2223
    int ret_errno = 0;
 
2224
    int ioctl_errno = 0;
2226
2225
    if(not get_flags(interface, &network) and debug){
2227
2226
      ret_errno = errno;
2228
2227
      fprintf_plus(stderr, "Failed to get flags for interface "
2288
2287
 
2289
2288
int main(int argc, char *argv[]){
2290
2289
  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 };
 
2290
                        .priority = "SECURE256:!CTYPE-X.509"
 
2291
                        ":+CTYPE-OPENPGP:!RSA:+SIGN-DSA-SHA256",
 
2292
                        .current_server = NULL, .interfaces = NULL,
 
2293
                        .interfaces_size = 0 };
2294
2294
  AvahiSServiceBrowser *sb = NULL;
2295
2295
  error_t ret_errno;
2296
2296
  int ret;
2477
2477
                         .args_doc = "",
2478
2478
                         .doc = "Mandos client -- Get and decrypt"
2479
2479
                         " passwords from a Mandos server" };
2480
 
    ret = argp_parse(&argp, argc, argv,
2481
 
                     ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
2482
 
    switch(ret){
 
2480
    ret_errno = argp_parse(&argp, argc, argv,
 
2481
                           ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
 
2482
    switch(ret_errno){
2483
2483
    case 0:
2484
2484
      break;
2485
2485
    case ENOMEM:
2486
2486
    default:
2487
 
      errno = ret;
 
2487
      errno = ret_errno;
2488
2488
      perror_plus("argp_parse");
2489
2489
      exitcode = EX_OSERR;
2490
2490
      goto end;
2496
2496
  
2497
2497
  {
2498
2498
    /* Work around Debian bug #633582:
2499
 
       <http://bugs.debian.org/633582> */
 
2499
       <https://bugs.debian.org/633582> */
2500
2500
    
2501
2501
    /* Re-raise privileges */
2502
 
    ret_errno = raise_privileges();
2503
 
    if(ret_errno != 0){
2504
 
      errno = ret_errno;
 
2502
    ret = raise_privileges();
 
2503
    if(ret != 0){
 
2504
      errno = ret;
2505
2505
      perror_plus("Failed to raise privileges");
2506
2506
    } else {
2507
2507
      struct stat st;
2571
2571
      }
2572
2572
      
2573
2573
      /* Lower privileges */
2574
 
      ret_errno = lower_privileges();
2575
 
      if(ret_errno != 0){
2576
 
        errno = ret_errno;
 
2574
      ret = lower_privileges();
 
2575
      if(ret != 0){
 
2576
        errno = ret;
2577
2577
        perror_plus("Failed to lower privileges");
2578
2578
      }
2579
2579
    }
2907
2907
    
2908
2908
    /* Allocate a new server */
2909
2909
    mc.server = avahi_server_new(avahi_simple_poll_get(simple_poll),
2910
 
                                 &config, NULL, NULL, &ret_errno);
 
2910
                                 &config, NULL, NULL, &ret);
2911
2911
    
2912
2912
    /* Free the Avahi configuration data */
2913
2913
    avahi_server_config_free(&config);
2916
2916
  /* Check if creating the Avahi server object succeeded */
2917
2917
  if(mc.server == NULL){
2918
2918
    fprintf_plus(stderr, "Failed to create Avahi server: %s\n",
2919
 
                 avahi_strerror(ret_errno));
 
2919
                 avahi_strerror(ret));
2920
2920
    exitcode = EX_UNAVAILABLE;
2921
2921
    goto end;
2922
2922
  }
2974
2974
  
2975
2975
  if(gnutls_initialized){
2976
2976
    gnutls_certificate_free_credentials(mc.cred);
2977
 
    gnutls_global_deinit();
2978
2977
    gnutls_dh_params_deinit(mc.dh_params);
2979
2978
  }
2980
2979
  
3003
3002
  
3004
3003
  /* Re-raise privileges */
3005
3004
  {
3006
 
    ret_errno = raise_privileges();
3007
 
    if(ret_errno != 0){
3008
 
      errno = ret_errno;
 
3005
    ret = raise_privileges();
 
3006
    if(ret != 0){
 
3007
      errno = ret;
3009
3008
      perror_plus("Failed to raise privileges");
3010
3009
    } else {
3011
3010
      
3019
3018
        while((interface=argz_next(interfaces_to_take_down,
3020
3019
                                   interfaces_to_take_down_size,
3021
3020
                                   interface))){
3022
 
          ret_errno = take_down_interface(interface);
3023
 
          if(ret_errno != 0){
3024
 
            errno = ret_errno;
 
3021
          ret = take_down_interface(interface);
 
3022
          if(ret != 0){
 
3023
            errno = ret;
3025
3024
            perror_plus("Failed to take down interface");
3026
3025
          }
3027
3026
        }
3032
3031
      }
3033
3032
    }
3034
3033
    
3035
 
    ret_errno = lower_privileges_permanently();
3036
 
    if(ret_errno != 0){
3037
 
      errno = ret_errno;
 
3034
    ret = lower_privileges_permanently();
 
3035
    if(ret != 0){
 
3036
      errno = ret;
3038
3037
      perror_plus("Failed to lower privileges permanently");
3039
3038
    }
3040
3039
  }
3042
3041
  free(interfaces_to_take_down);
3043
3042
  free(interfaces_hooks);
3044
3043
  
 
3044
  void clean_dir_at(int base, const char * const dirname,
 
3045
                    uintmax_t level){
 
3046
    struct dirent **direntries = NULL;
 
3047
    int dret;
 
3048
    int dir_fd = (int)TEMP_FAILURE_RETRY(openat(base, dirname,
 
3049
                                                O_RDONLY
 
3050
                                                | O_NOFOLLOW
 
3051
                                                | O_DIRECTORY
 
3052
                                                | O_PATH));
 
3053
    if(dir_fd == -1){
 
3054
      perror_plus("open");
 
3055
    }
 
3056
    int numentries = scandirat(dir_fd, ".", &direntries,
 
3057
                               notdotentries, alphasort);
 
3058
    if(numentries >= 0){
 
3059
      for(int i = 0; i < numentries; i++){
 
3060
        if(debug){
 
3061
          fprintf_plus(stderr, "Unlinking \"%s/%s\"\n",
 
3062
                       dirname, direntries[i]->d_name);
 
3063
        }
 
3064
        dret = unlinkat(dir_fd, direntries[i]->d_name, 0);
 
3065
        if(dret == -1){
 
3066
          if(errno == EISDIR){
 
3067
              dret = unlinkat(dir_fd, direntries[i]->d_name,
 
3068
                              AT_REMOVEDIR);
 
3069
          }         
 
3070
          if((dret == -1) and (errno == ENOTEMPTY)
 
3071
             and (strcmp(direntries[i]->d_name, "private-keys-v1.d")
 
3072
                  == 0) and (level == 0)){
 
3073
            /* Recurse only in this special case */
 
3074
            clean_dir_at(dir_fd, direntries[i]->d_name, level+1);
 
3075
            dret = 0;
 
3076
          }
 
3077
          if(dret == -1){
 
3078
            fprintf_plus(stderr, "unlink(\"%s/%s\"): %s\n", dirname,
 
3079
                         direntries[i]->d_name, strerror(errno));
 
3080
          }
 
3081
        }
 
3082
        free(direntries[i]);
 
3083
      }
 
3084
      
 
3085
      /* need to clean even if 0 because man page doesn't specify */
 
3086
      free(direntries);
 
3087
      if(numentries == -1){
 
3088
        perror_plus("scandirat");
 
3089
      }
 
3090
      dret = unlinkat(base, dirname, AT_REMOVEDIR);
 
3091
      if(dret == -1 and errno != ENOENT){
 
3092
        perror_plus("rmdir");
 
3093
      }
 
3094
    } else {
 
3095
      perror_plus("scandirat");
 
3096
    }
 
3097
    close(dir_fd);
 
3098
  }
 
3099
  
3045
3100
  /* Removes the GPGME temp directory and all files inside */
3046
3101
  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
 
    }
 
3102
    clean_dir_at(-1, tempdir, 0);
3090
3103
  }
3091
3104
  
3092
3105
  if(quit_now){