/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-03-04 22:07:35 UTC
  • Revision ID: teddy@recompile.se-20160304220735-4xeeqt5p4nhw5cuh
Restrict the Mandos server daemon in the systemd service file.

* mandos.service ([Service]/ProtectSystem): Set to "full".
 ([Service]/PrivateTmp, [Service]/PrivateDevices,
  [Service]/ProtectHome): Set to "yes".
 ([Service]/CapabilityBoundingSet): Set to "CAP_SETUID
                                    CAP_DAC_OVERRIDE CAP_NET_RAW".

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-2017 Teddy Hogeborn
13
 
 * Copyright © 2008-2017 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(), strncpy(), strsignal()
51
 
                                */
 
50
                                   asprintf(), strncpy() */
52
51
#include <sys/ioctl.h>          /* ioctl */
53
52
#include <sys/types.h>          /* socket(), inet_pton(), sockaddr,
54
53
                                   sockaddr_in6, PF_INET6,
626
625
                     safer_gnutls_strerror(ret));
627
626
        dhparamsfilename = NULL;
628
627
      }
629
 
      free(params.data);
630
628
    } while(false);
631
629
  }
632
630
  if(dhparamsfilename == NULL){
819
817
 
820
818
/* Set effective uid to 0, return errno */
821
819
__attribute__((warn_unused_result))
822
 
int raise_privileges(void){
823
 
  int old_errno = errno;
824
 
  int ret = 0;
 
820
error_t raise_privileges(void){
 
821
  error_t old_errno = errno;
 
822
  error_t ret_errno = 0;
825
823
  if(seteuid(0) == -1){
826
 
    ret = errno;
 
824
    ret_errno = errno;
827
825
  }
828
826
  errno = old_errno;
829
 
  return ret;
 
827
  return ret_errno;
830
828
}
831
829
 
832
830
/* Set effective and real user ID to 0.  Return errno. */
833
831
__attribute__((warn_unused_result))
834
 
int raise_privileges_permanently(void){
835
 
  int old_errno = errno;
836
 
  int ret = raise_privileges();
837
 
  if(ret != 0){
 
832
error_t raise_privileges_permanently(void){
 
833
  error_t old_errno = errno;
 
834
  error_t ret_errno = raise_privileges();
 
835
  if(ret_errno != 0){
838
836
    errno = old_errno;
839
 
    return ret;
 
837
    return ret_errno;
840
838
  }
841
839
  if(setuid(0) == -1){
842
 
    ret = errno;
 
840
    ret_errno = errno;
843
841
  }
844
842
  errno = old_errno;
845
 
  return ret;
 
843
  return ret_errno;
846
844
}
847
845
 
848
846
/* Set effective user ID to unprivileged saved user ID */
849
847
__attribute__((warn_unused_result))
850
 
int lower_privileges(void){
851
 
  int old_errno = errno;
852
 
  int ret = 0;
 
848
error_t lower_privileges(void){
 
849
  error_t old_errno = errno;
 
850
  error_t ret_errno = 0;
853
851
  if(seteuid(uid) == -1){
854
 
    ret = errno;
 
852
    ret_errno = errno;
855
853
  }
856
854
  errno = old_errno;
857
 
  return ret;
 
855
  return ret_errno;
858
856
}
859
857
 
860
858
/* Lower privileges permanently */
861
859
__attribute__((warn_unused_result))
862
 
int lower_privileges_permanently(void){
863
 
  int old_errno = errno;
864
 
  int ret = 0;
 
860
error_t lower_privileges_permanently(void){
 
861
  error_t old_errno = errno;
 
862
  error_t ret_errno = 0;
865
863
  if(setuid(uid) == -1){
866
 
    ret = errno;
 
864
    ret_errno = errno;
867
865
  }
868
866
  errno = old_errno;
869
 
  return ret;
 
867
  return ret_errno;
870
868
}
871
869
 
872
870
/* Helper function to add_local_route() and delete_local_route() */
1079
1077
    bool match = false;
1080
1078
    {
1081
1079
      char *interface = NULL;
1082
 
      while((interface = argz_next(mc->interfaces,
1083
 
                                   mc->interfaces_size,
1084
 
                                   interface))){
 
1080
      while((interface=argz_next(mc->interfaces, mc->interfaces_size,
 
1081
                                 interface))){
1085
1082
        if(if_nametoindex(interface) == (unsigned int)if_index){
1086
1083
          match = true;
1087
1084
          break;
1240
1237
           with an explicit route added with the server's address.
1241
1238
           
1242
1239
           Avahi bug reference:
1243
 
           https://lists.freedesktop.org/archives/avahi/2010-February/001833.html
 
1240
           http://lists.freedesktop.org/archives/avahi/2010-February/001833.html
1244
1241
           https://bugs.debian.org/587961
1245
1242
        */
1246
1243
        if(debug){
1426
1423
                                               &decrypted_buffer, mc);
1427
1424
    if(decrypted_buffer_size >= 0){
1428
1425
      
1429
 
      clearerr(stdout);
1430
1426
      written = 0;
1431
1427
      while(written < (size_t) decrypted_buffer_size){
1432
1428
        if(quit_now){
1448
1444
        }
1449
1445
        written += (size_t)ret;
1450
1446
      }
1451
 
      ret = fflush(stdout);
1452
 
      if(ret != 0){
1453
 
        int e = errno;
1454
 
        if(debug){
1455
 
          fprintf_plus(stderr, "Error writing encrypted data: %s\n",
1456
 
                       strerror(errno));
1457
 
        }
1458
 
        errno = e;
1459
 
        goto mandos_end;
1460
 
      }
1461
1447
      retval = 0;
1462
1448
    }
1463
1449
  }
1494
1480
  return retval;
1495
1481
}
1496
1482
 
 
1483
__attribute__((nonnull))
1497
1484
static void resolve_callback(AvahiSServiceResolver *r,
1498
1485
                             AvahiIfIndex interface,
1499
1486
                             AvahiProtocol proto,
1636
1623
__attribute__((nonnull, warn_unused_result))
1637
1624
bool get_flags(const char *ifname, struct ifreq *ifr){
1638
1625
  int ret;
1639
 
  int old_errno;
 
1626
  error_t ret_errno;
1640
1627
  
1641
1628
  int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1642
1629
  if(s < 0){
1643
 
    old_errno = errno;
 
1630
    ret_errno = errno;
1644
1631
    perror_plus("socket");
1645
 
    errno = old_errno;
 
1632
    errno = ret_errno;
1646
1633
    return false;
1647
1634
  }
1648
1635
  strncpy(ifr->ifr_name, ifname, IF_NAMESIZE);
1650
1637
  ret = ioctl(s, SIOCGIFFLAGS, ifr);
1651
1638
  if(ret == -1){
1652
1639
    if(debug){
1653
 
      old_errno = errno;
 
1640
      ret_errno = errno;
1654
1641
      perror_plus("ioctl SIOCGIFFLAGS");
1655
 
      errno = old_errno;
 
1642
      errno = ret_errno;
1656
1643
    }
1657
1644
    return false;
1658
1645
  }
2084
2071
}
2085
2072
 
2086
2073
__attribute__((nonnull, warn_unused_result))
2087
 
int bring_up_interface(const char *const interface,
2088
 
                       const float delay){
2089
 
  int old_errno = errno;
 
2074
error_t bring_up_interface(const char *const interface,
 
2075
                           const float delay){
 
2076
  error_t old_errno = errno;
2090
2077
  int ret;
2091
2078
  struct ifreq network;
2092
2079
  unsigned int if_index = if_nametoindex(interface);
2102
2089
  }
2103
2090
  
2104
2091
  if(not interface_is_up(interface)){
2105
 
    int ret_errno = 0;
2106
 
    int ioctl_errno = 0;
 
2092
    error_t ret_errno = 0, ioctl_errno = 0;
2107
2093
    if(not get_flags(interface, &network)){
2108
2094
      ret_errno = errno;
2109
2095
      fprintf_plus(stderr, "Failed to get flags for interface "
2196
2182
  
2197
2183
  /* Sleep checking until interface is running.
2198
2184
     Check every 0.25s, up to total time of delay */
2199
 
  for(int i = 0; i < delay * 4; i++){
 
2185
  for(int i=0; i < delay * 4; i++){
2200
2186
    if(interface_is_running(interface)){
2201
2187
      break;
2202
2188
    }
2212
2198
}
2213
2199
 
2214
2200
__attribute__((nonnull, warn_unused_result))
2215
 
int take_down_interface(const char *const interface){
2216
 
  int old_errno = errno;
 
2201
error_t take_down_interface(const char *const interface){
 
2202
  error_t old_errno = errno;
2217
2203
  struct ifreq network;
2218
2204
  unsigned int if_index = if_nametoindex(interface);
2219
2205
  if(if_index == 0){
2222
2208
    return ENXIO;
2223
2209
  }
2224
2210
  if(interface_is_up(interface)){
2225
 
    int ret_errno = 0;
2226
 
    int ioctl_errno = 0;
 
2211
    error_t ret_errno = 0, ioctl_errno = 0;
2227
2212
    if(not get_flags(interface, &network) and debug){
2228
2213
      ret_errno = errno;
2229
2214
      fprintf_plus(stderr, "Failed to get flags for interface "
2479
2464
                         .args_doc = "",
2480
2465
                         .doc = "Mandos client -- Get and decrypt"
2481
2466
                         " passwords from a Mandos server" };
2482
 
    ret_errno = argp_parse(&argp, argc, argv,
2483
 
                           ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
2484
 
    switch(ret_errno){
 
2467
    ret = argp_parse(&argp, argc, argv,
 
2468
                     ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
 
2469
    switch(ret){
2485
2470
    case 0:
2486
2471
      break;
2487
2472
    case ENOMEM:
2488
2473
    default:
2489
 
      errno = ret_errno;
 
2474
      errno = ret;
2490
2475
      perror_plus("argp_parse");
2491
2476
      exitcode = EX_OSERR;
2492
2477
      goto end;
2498
2483
  
2499
2484
  {
2500
2485
    /* Work around Debian bug #633582:
2501
 
       <https://bugs.debian.org/633582> */
 
2486
       <http://bugs.debian.org/633582> */
2502
2487
    
2503
2488
    /* Re-raise privileges */
2504
 
    ret = raise_privileges();
2505
 
    if(ret != 0){
2506
 
      errno = ret;
 
2489
    ret_errno = raise_privileges();
 
2490
    if(ret_errno != 0){
 
2491
      errno = ret_errno;
2507
2492
      perror_plus("Failed to raise privileges");
2508
2493
    } else {
2509
2494
      struct stat st;
2573
2558
      }
2574
2559
      
2575
2560
      /* Lower privileges */
2576
 
      ret = lower_privileges();
2577
 
      if(ret != 0){
2578
 
        errno = ret;
 
2561
      ret_errno = lower_privileges();
 
2562
      if(ret_errno != 0){
 
2563
        errno = ret_errno;
2579
2564
        perror_plus("Failed to lower privileges");
2580
2565
      }
2581
2566
    }
2909
2894
    
2910
2895
    /* Allocate a new server */
2911
2896
    mc.server = avahi_server_new(avahi_simple_poll_get(simple_poll),
2912
 
                                 &config, NULL, NULL, &ret);
 
2897
                                 &config, NULL, NULL, &ret_errno);
2913
2898
    
2914
2899
    /* Free the Avahi configuration data */
2915
2900
    avahi_server_config_free(&config);
2918
2903
  /* Check if creating the Avahi server object succeeded */
2919
2904
  if(mc.server == NULL){
2920
2905
    fprintf_plus(stderr, "Failed to create Avahi server: %s\n",
2921
 
                 avahi_strerror(ret));
 
2906
                 avahi_strerror(ret_errno));
2922
2907
    exitcode = EX_UNAVAILABLE;
2923
2908
    goto end;
2924
2909
  }
2959
2944
 end:
2960
2945
  
2961
2946
  if(debug){
2962
 
    if(signal_received){
2963
 
      fprintf_plus(stderr, "%s exiting due to signal %d: %s\n",
2964
 
                   argv[0], signal_received,
2965
 
                   strsignal(signal_received));
2966
 
    } else {
2967
 
      fprintf_plus(stderr, "%s exiting\n", argv[0]);
2968
 
    }
 
2947
    fprintf_plus(stderr, "%s exiting\n", argv[0]);
2969
2948
  }
2970
2949
  
2971
2950
  /* Cleanup things */
3010
2989
  
3011
2990
  /* Re-raise privileges */
3012
2991
  {
3013
 
    ret = raise_privileges();
3014
 
    if(ret != 0){
3015
 
      errno = ret;
 
2992
    ret_errno = raise_privileges();
 
2993
    if(ret_errno != 0){
 
2994
      errno = ret_errno;
3016
2995
      perror_plus("Failed to raise privileges");
3017
2996
    } else {
3018
2997
      
3023
3002
      /* Take down the network interfaces which were brought up */
3024
3003
      {
3025
3004
        char *interface = NULL;
3026
 
        while((interface = argz_next(interfaces_to_take_down,
3027
 
                                     interfaces_to_take_down_size,
3028
 
                                     interface))){
3029
 
          ret = take_down_interface(interface);
3030
 
          if(ret != 0){
3031
 
            errno = ret;
 
3005
        while((interface=argz_next(interfaces_to_take_down,
 
3006
                                   interfaces_to_take_down_size,
 
3007
                                   interface))){
 
3008
          ret_errno = take_down_interface(interface);
 
3009
          if(ret_errno != 0){
 
3010
            errno = ret_errno;
3032
3011
            perror_plus("Failed to take down interface");
3033
3012
          }
3034
3013
        }
3039
3018
      }
3040
3019
    }
3041
3020
    
3042
 
    ret = lower_privileges_permanently();
3043
 
    if(ret != 0){
3044
 
      errno = ret;
 
3021
    ret_errno = lower_privileges_permanently();
 
3022
    if(ret_errno != 0){
 
3023
      errno = ret_errno;
3045
3024
      perror_plus("Failed to lower privileges permanently");
3046
3025
    }
3047
3026
  }