/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 02:56:00 UTC
  • Revision ID: teddy@recompile.se-20160703025600-bn12vpzejr6zxx5v
mandos-client: Bug fix: flush stdout to detect write errors.

* plugins.d/mandos-client.c (start_mandos_communication): Do
                            fflush(stdout) to detect any write errors.

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
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() */
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
1646
  strncpy(ifr->ifr_name, ifname, IF_NAMESIZE);
1642
1648
  ret = ioctl(s, SIOCGIFFLAGS, ifr);
1643
1649
  if(ret == -1){
1644
1650
    if(debug){
1645
 
      ret_errno = errno;
 
1651
      old_errno = errno;
1646
1652
      perror_plus("ioctl SIOCGIFFLAGS");
1647
 
      errno = ret_errno;
 
1653
      errno = old_errno;
1648
1654
    }
1649
1655
    return false;
1650
1656
  }
1914
1920
      return;
1915
1921
    }
1916
1922
  }
1917
 
#ifdef __GLIBC__
1918
 
#if __GLIBC_PREREQ(2, 15)
1919
1923
  int numhooks = scandirat(hookdir_fd, ".", &direntries,
1920
1924
                           runnable_hook, alphasort);
1921
 
#else  /* not __GLIBC_PREREQ(2, 15) */
1922
 
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1923
 
                         alphasort);
1924
 
#endif  /* not __GLIBC_PREREQ(2, 15) */
1925
 
#else   /* not __GLIBC__ */
1926
 
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1927
 
                         alphasort);
1928
 
#endif  /* not __GLIBC__ */
1929
1925
  if(numhooks == -1){
1930
1926
    perror_plus("scandir");
1931
1927
    return;
2086
2082
}
2087
2083
 
2088
2084
__attribute__((nonnull, warn_unused_result))
2089
 
error_t bring_up_interface(const char *const interface,
2090
 
                           const float delay){
2091
 
  error_t old_errno = errno;
 
2085
int bring_up_interface(const char *const interface,
 
2086
                       const float delay){
 
2087
  int old_errno = errno;
2092
2088
  int ret;
2093
2089
  struct ifreq network;
2094
2090
  unsigned int if_index = if_nametoindex(interface);
2104
2100
  }
2105
2101
  
2106
2102
  if(not interface_is_up(interface)){
2107
 
    error_t ret_errno = 0, ioctl_errno = 0;
 
2103
    int ret_errno = 0;
 
2104
    int ioctl_errno = 0;
2108
2105
    if(not get_flags(interface, &network)){
2109
2106
      ret_errno = errno;
2110
2107
      fprintf_plus(stderr, "Failed to get flags for interface "
2213
2210
}
2214
2211
 
2215
2212
__attribute__((nonnull, warn_unused_result))
2216
 
error_t take_down_interface(const char *const interface){
2217
 
  error_t old_errno = errno;
 
2213
int take_down_interface(const char *const interface){
 
2214
  int old_errno = errno;
2218
2215
  struct ifreq network;
2219
2216
  unsigned int if_index = if_nametoindex(interface);
2220
2217
  if(if_index == 0){
2223
2220
    return ENXIO;
2224
2221
  }
2225
2222
  if(interface_is_up(interface)){
2226
 
    error_t ret_errno = 0, ioctl_errno = 0;
 
2223
    int ret_errno = 0;
 
2224
    int ioctl_errno = 0;
2227
2225
    if(not get_flags(interface, &network) and debug){
2228
2226
      ret_errno = errno;
2229
2227
      fprintf_plus(stderr, "Failed to get flags for interface "
2479
2477
                         .args_doc = "",
2480
2478
                         .doc = "Mandos client -- Get and decrypt"
2481
2479
                         " passwords from a Mandos server" };
2482
 
    ret = argp_parse(&argp, argc, argv,
2483
 
                     ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
2484
 
    switch(ret){
 
2480
    ret_errno = argp_parse(&argp, argc, argv,
 
2481
                           ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
 
2482
    switch(ret_errno){
2485
2483
    case 0:
2486
2484
      break;
2487
2485
    case ENOMEM:
2488
2486
    default:
2489
 
      errno = ret;
 
2487
      errno = ret_errno;
2490
2488
      perror_plus("argp_parse");
2491
2489
      exitcode = EX_OSERR;
2492
2490
      goto end;
2501
2499
       <http://bugs.debian.org/633582> */
2502
2500
    
2503
2501
    /* Re-raise privileges */
2504
 
    ret_errno = raise_privileges();
2505
 
    if(ret_errno != 0){
2506
 
      errno = ret_errno;
 
2502
    ret = raise_privileges();
 
2503
    if(ret != 0){
 
2504
      errno = ret;
2507
2505
      perror_plus("Failed to raise privileges");
2508
2506
    } else {
2509
2507
      struct stat st;
2573
2571
      }
2574
2572
      
2575
2573
      /* Lower privileges */
2576
 
      ret_errno = lower_privileges();
2577
 
      if(ret_errno != 0){
2578
 
        errno = ret_errno;
 
2574
      ret = lower_privileges();
 
2575
      if(ret != 0){
 
2576
        errno = ret;
2579
2577
        perror_plus("Failed to lower privileges");
2580
2578
      }
2581
2579
    }
2909
2907
    
2910
2908
    /* Allocate a new server */
2911
2909
    mc.server = avahi_server_new(avahi_simple_poll_get(simple_poll),
2912
 
                                 &config, NULL, NULL, &ret_errno);
 
2910
                                 &config, NULL, NULL, &ret);
2913
2911
    
2914
2912
    /* Free the Avahi configuration data */
2915
2913
    avahi_server_config_free(&config);
2918
2916
  /* Check if creating the Avahi server object succeeded */
2919
2917
  if(mc.server == NULL){
2920
2918
    fprintf_plus(stderr, "Failed to create Avahi server: %s\n",
2921
 
                 avahi_strerror(ret_errno));
 
2919
                 avahi_strerror(ret));
2922
2920
    exitcode = EX_UNAVAILABLE;
2923
2921
    goto end;
2924
2922
  }
2976
2974
  
2977
2975
  if(gnutls_initialized){
2978
2976
    gnutls_certificate_free_credentials(mc.cred);
2979
 
    gnutls_global_deinit();
2980
2977
    gnutls_dh_params_deinit(mc.dh_params);
2981
2978
  }
2982
2979
  
3005
3002
  
3006
3003
  /* Re-raise privileges */
3007
3004
  {
3008
 
    ret_errno = raise_privileges();
3009
 
    if(ret_errno != 0){
3010
 
      errno = ret_errno;
 
3005
    ret = raise_privileges();
 
3006
    if(ret != 0){
 
3007
      errno = ret;
3011
3008
      perror_plus("Failed to raise privileges");
3012
3009
    } else {
3013
3010
      
3021
3018
        while((interface=argz_next(interfaces_to_take_down,
3022
3019
                                   interfaces_to_take_down_size,
3023
3020
                                   interface))){
3024
 
          ret_errno = take_down_interface(interface);
3025
 
          if(ret_errno != 0){
3026
 
            errno = ret_errno;
 
3021
          ret = take_down_interface(interface);
 
3022
          if(ret != 0){
 
3023
            errno = ret;
3027
3024
            perror_plus("Failed to take down interface");
3028
3025
          }
3029
3026
        }
3034
3031
      }
3035
3032
    }
3036
3033
    
3037
 
    ret_errno = lower_privileges_permanently();
3038
 
    if(ret_errno != 0){
3039
 
      errno = ret_errno;
 
3034
    ret = lower_privileges_permanently();
 
3035
    if(ret != 0){
 
3036
      errno = ret;
3040
3037
      perror_plus("Failed to lower privileges permanently");
3041
3038
    }
3042
3039
  }
3044
3041
  free(interfaces_to_take_down);
3045
3042
  free(interfaces_hooks);
3046
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
  
3047
3100
  /* Removes the GPGME temp directory and all files inside */
3048
3101
  if(tempdir != NULL){
3049
 
    struct dirent **direntries = NULL;
3050
 
    int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY
3051
 
                                                  | O_NOFOLLOW
3052
 
                                                  | O_DIRECTORY
3053
 
                                                  | O_PATH));
3054
 
    if(tempdir_fd == -1){
3055
 
      perror_plus("open");
3056
 
    } else {
3057
 
#ifdef __GLIBC__
3058
 
#if __GLIBC_PREREQ(2, 15)
3059
 
      int numentries = scandirat(tempdir_fd, ".", &direntries,
3060
 
                                 notdotentries, alphasort);
3061
 
#else  /* not __GLIBC_PREREQ(2, 15) */
3062
 
      int numentries = scandir(tempdir, &direntries, notdotentries,
3063
 
                               alphasort);
3064
 
#endif  /* not __GLIBC_PREREQ(2, 15) */
3065
 
#else   /* not __GLIBC__ */
3066
 
      int numentries = scandir(tempdir, &direntries, notdotentries,
3067
 
                               alphasort);
3068
 
#endif  /* not __GLIBC__ */
3069
 
      if(numentries >= 0){
3070
 
        for(int i = 0; i < numentries; i++){
3071
 
          ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
3072
 
          if(ret == -1){
3073
 
            fprintf_plus(stderr, "unlinkat(open(\"%s\", O_RDONLY),"
3074
 
                         " \"%s\", 0): %s\n", tempdir,
3075
 
                         direntries[i]->d_name, strerror(errno));
3076
 
          }
3077
 
          free(direntries[i]);
3078
 
        }
3079
 
        
3080
 
        /* need to clean even if 0 because man page doesn't specify */
3081
 
        free(direntries);
3082
 
        if(numentries == -1){
3083
 
          perror_plus("scandir");
3084
 
        }
3085
 
        ret = rmdir(tempdir);
3086
 
        if(ret == -1 and errno != ENOENT){
3087
 
          perror_plus("rmdir");
3088
 
        }
3089
 
      }
3090
 
      close(tempdir_fd);
3091
 
    }
 
3102
    clean_dir_at(-1, tempdir, 0);
3092
3103
  }
3093
3104
  
3094
3105
  if(quit_now){