/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 20:50:45 UTC
  • 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-2018 Teddy Hogeborn
13
 
 * Copyright © 2008-2018 Björn Påhlsson
14
 
 * 
15
 
 * This file is part of Mandos.
16
 
 * 
17
 
 * Mandos is free software: you can redistribute it and/or modify it
18
 
 * under the terms of the GNU General Public License as published by
19
 
 * the Free Software Foundation, either version 3 of the License, or
20
 
 * (at your option) any later version.
21
 
 * 
22
 
 * Mandos is distributed in the hope that it will be useful, but
 
12
 * Copyright © 2008-2016 Teddy Hogeborn
 
13
 * Copyright © 2008-2016 Björn Påhlsson
 
14
 * 
 
15
 * This program is free software: you can redistribute it and/or
 
16
 * modify it under the terms of the GNU General Public License as
 
17
 * published by the Free Software Foundation, either version 3 of the
 
18
 * License, or (at your option) any later version.
 
19
 * 
 
20
 * This program is distributed in the hope that it will be useful, but
23
21
 * WITHOUT ANY WARRANTY; without even the implied warranty of
24
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25
23
 * General Public License for more details.
26
24
 * 
27
25
 * You should have received a copy of the GNU General Public License
28
 
 * along with Mandos.  If not, see <http://www.gnu.org/licenses/>.
 
26
 * along with this program.  If not, see
 
27
 * <http://www.gnu.org/licenses/>.
29
28
 * 
30
29
 * Contact the authors at <mandos@recompile.se>.
31
30
 */
48
47
                                   strtof(), abort() */
49
48
#include <stdbool.h>            /* bool, false, true */
50
49
#include <string.h>             /* strcmp(), strlen(), strerror(),
51
 
                                   asprintf(), strncpy(), strsignal()
52
 
                                */
 
50
                                   asprintf(), strncpy() */
53
51
#include <sys/ioctl.h>          /* ioctl */
54
52
#include <sys/types.h>          /* socket(), inet_pton(), sockaddr,
55
53
                                   sockaddr_in6, PF_INET6,
613
611
        }
614
612
        params.size += (unsigned int)bytes_read;
615
613
      }
616
 
      ret = close(dhpfile);
617
 
      if(ret == -1){
618
 
        perror_plus("close");
619
 
      }
620
614
      if(params.data == NULL){
621
615
        dhparamsfilename = NULL;
622
616
      }
631
625
                     safer_gnutls_strerror(ret));
632
626
        dhparamsfilename = NULL;
633
627
      }
634
 
      free(params.data);
635
628
    } while(false);
636
629
  }
637
630
  if(dhparamsfilename == NULL){
824
817
 
825
818
/* Set effective uid to 0, return errno */
826
819
__attribute__((warn_unused_result))
827
 
int raise_privileges(void){
828
 
  int old_errno = errno;
829
 
  int ret = 0;
 
820
error_t raise_privileges(void){
 
821
  error_t old_errno = errno;
 
822
  error_t ret_errno = 0;
830
823
  if(seteuid(0) == -1){
831
 
    ret = errno;
 
824
    ret_errno = errno;
832
825
  }
833
826
  errno = old_errno;
834
 
  return ret;
 
827
  return ret_errno;
835
828
}
836
829
 
837
830
/* Set effective and real user ID to 0.  Return errno. */
838
831
__attribute__((warn_unused_result))
839
 
int raise_privileges_permanently(void){
840
 
  int old_errno = errno;
841
 
  int ret = raise_privileges();
842
 
  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){
843
836
    errno = old_errno;
844
 
    return ret;
 
837
    return ret_errno;
845
838
  }
846
839
  if(setuid(0) == -1){
847
 
    ret = errno;
 
840
    ret_errno = errno;
848
841
  }
849
842
  errno = old_errno;
850
 
  return ret;
 
843
  return ret_errno;
851
844
}
852
845
 
853
846
/* Set effective user ID to unprivileged saved user ID */
854
847
__attribute__((warn_unused_result))
855
 
int lower_privileges(void){
856
 
  int old_errno = errno;
857
 
  int ret = 0;
 
848
error_t lower_privileges(void){
 
849
  error_t old_errno = errno;
 
850
  error_t ret_errno = 0;
858
851
  if(seteuid(uid) == -1){
859
 
    ret = errno;
 
852
    ret_errno = errno;
860
853
  }
861
854
  errno = old_errno;
862
 
  return ret;
 
855
  return ret_errno;
863
856
}
864
857
 
865
858
/* Lower privileges permanently */
866
859
__attribute__((warn_unused_result))
867
 
int lower_privileges_permanently(void){
868
 
  int old_errno = errno;
869
 
  int ret = 0;
 
860
error_t lower_privileges_permanently(void){
 
861
  error_t old_errno = errno;
 
862
  error_t ret_errno = 0;
870
863
  if(setuid(uid) == -1){
871
 
    ret = errno;
 
864
    ret_errno = errno;
872
865
  }
873
866
  errno = old_errno;
874
 
  return ret;
 
867
  return ret_errno;
875
868
}
876
869
 
877
870
/* Helper function to add_local_route() and delete_local_route() */
1084
1077
    bool match = false;
1085
1078
    {
1086
1079
      char *interface = NULL;
1087
 
      while((interface = argz_next(mc->interfaces,
1088
 
                                   mc->interfaces_size,
1089
 
                                   interface))){
 
1080
      while((interface=argz_next(mc->interfaces, mc->interfaces_size,
 
1081
                                 interface))){
1090
1082
        if(if_nametoindex(interface) == (unsigned int)if_index){
1091
1083
          match = true;
1092
1084
          break;
1245
1237
           with an explicit route added with the server's address.
1246
1238
           
1247
1239
           Avahi bug reference:
1248
 
           https://lists.freedesktop.org/archives/avahi/2010-February/001833.html
 
1240
           http://lists.freedesktop.org/archives/avahi/2010-February/001833.html
1249
1241
           https://bugs.debian.org/587961
1250
1242
        */
1251
1243
        if(debug){
1431
1423
                                               &decrypted_buffer, mc);
1432
1424
    if(decrypted_buffer_size >= 0){
1433
1425
      
1434
 
      clearerr(stdout);
1435
1426
      written = 0;
1436
1427
      while(written < (size_t) decrypted_buffer_size){
1437
1428
        if(quit_now){
1453
1444
        }
1454
1445
        written += (size_t)ret;
1455
1446
      }
1456
 
      ret = fflush(stdout);
1457
 
      if(ret != 0){
1458
 
        int e = errno;
1459
 
        if(debug){
1460
 
          fprintf_plus(stderr, "Error writing encrypted data: %s\n",
1461
 
                       strerror(errno));
1462
 
        }
1463
 
        errno = e;
1464
 
        goto mandos_end;
1465
 
      }
1466
1447
      retval = 0;
1467
1448
    }
1468
1449
  }
1499
1480
  return retval;
1500
1481
}
1501
1482
 
 
1483
__attribute__((nonnull))
1502
1484
static void resolve_callback(AvahiSServiceResolver *r,
1503
1485
                             AvahiIfIndex interface,
1504
1486
                             AvahiProtocol proto,
1641
1623
__attribute__((nonnull, warn_unused_result))
1642
1624
bool get_flags(const char *ifname, struct ifreq *ifr){
1643
1625
  int ret;
1644
 
  int old_errno;
 
1626
  error_t ret_errno;
1645
1627
  
1646
1628
  int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1647
1629
  if(s < 0){
1648
 
    old_errno = errno;
 
1630
    ret_errno = errno;
1649
1631
    perror_plus("socket");
1650
 
    errno = old_errno;
 
1632
    errno = ret_errno;
1651
1633
    return false;
1652
1634
  }
1653
1635
  strncpy(ifr->ifr_name, ifname, IF_NAMESIZE);
1655
1637
  ret = ioctl(s, SIOCGIFFLAGS, ifr);
1656
1638
  if(ret == -1){
1657
1639
    if(debug){
1658
 
      old_errno = errno;
 
1640
      ret_errno = errno;
1659
1641
      perror_plus("ioctl SIOCGIFFLAGS");
1660
 
      errno = old_errno;
1661
 
    }
1662
 
    if((close(s) == -1) and debug){
1663
 
      old_errno = errno;
1664
 
      perror_plus("close");
1665
 
      errno = old_errno;
 
1642
      errno = ret_errno;
1666
1643
    }
1667
1644
    return false;
1668
1645
  }
1669
 
  if((close(s) == -1) and debug){
1670
 
    old_errno = errno;
1671
 
    perror_plus("close");
1672
 
    errno = old_errno;
1673
 
  }
1674
1646
  return true;
1675
1647
}
1676
1648
 
1937
1909
      return;
1938
1910
    }
1939
1911
  }
1940
 
  int devnull = (int)TEMP_FAILURE_RETRY(open("/dev/null", O_RDONLY));
1941
 
  if(devnull == -1){
1942
 
    perror_plus("open(\"/dev/null\", O_RDONLY)");
1943
 
    return;
1944
 
  }
1945
1912
  int numhooks = scandirat(hookdir_fd, ".", &direntries,
1946
1913
                           runnable_hook, alphasort);
1947
1914
  if(numhooks == -1){
1948
1915
    perror_plus("scandir");
1949
 
    close(devnull);
1950
1916
    return;
1951
1917
  }
1952
1918
  struct dirent *direntry;
1953
1919
  int ret;
 
1920
  int devnull = (int)TEMP_FAILURE_RETRY(open("/dev/null", O_RDONLY));
 
1921
  if(devnull == -1){
 
1922
    perror_plus("open(\"/dev/null\", O_RDONLY)");
 
1923
    return;
 
1924
  }
1954
1925
  for(int i = 0; i < numhooks; i++){
1955
1926
    direntry = direntries[i];
1956
1927
    if(debug){
2100
2071
}
2101
2072
 
2102
2073
__attribute__((nonnull, warn_unused_result))
2103
 
int bring_up_interface(const char *const interface,
2104
 
                       const float delay){
2105
 
  int old_errno = errno;
 
2074
error_t bring_up_interface(const char *const interface,
 
2075
                           const float delay){
 
2076
  error_t old_errno = errno;
2106
2077
  int ret;
2107
2078
  struct ifreq network;
2108
2079
  unsigned int if_index = if_nametoindex(interface);
2118
2089
  }
2119
2090
  
2120
2091
  if(not interface_is_up(interface)){
2121
 
    int ret_errno = 0;
2122
 
    int ioctl_errno = 0;
 
2092
    error_t ret_errno = 0, ioctl_errno = 0;
2123
2093
    if(not get_flags(interface, &network)){
2124
2094
      ret_errno = errno;
2125
2095
      fprintf_plus(stderr, "Failed to get flags for interface "
2212
2182
  
2213
2183
  /* Sleep checking until interface is running.
2214
2184
     Check every 0.25s, up to total time of delay */
2215
 
  for(int i = 0; i < delay * 4; i++){
 
2185
  for(int i=0; i < delay * 4; i++){
2216
2186
    if(interface_is_running(interface)){
2217
2187
      break;
2218
2188
    }
2228
2198
}
2229
2199
 
2230
2200
__attribute__((nonnull, warn_unused_result))
2231
 
int take_down_interface(const char *const interface){
2232
 
  int old_errno = errno;
 
2201
error_t take_down_interface(const char *const interface){
 
2202
  error_t old_errno = errno;
2233
2203
  struct ifreq network;
2234
2204
  unsigned int if_index = if_nametoindex(interface);
2235
2205
  if(if_index == 0){
2238
2208
    return ENXIO;
2239
2209
  }
2240
2210
  if(interface_is_up(interface)){
2241
 
    int ret_errno = 0;
2242
 
    int ioctl_errno = 0;
 
2211
    error_t ret_errno = 0, ioctl_errno = 0;
2243
2212
    if(not get_flags(interface, &network) and debug){
2244
2213
      ret_errno = errno;
2245
2214
      fprintf_plus(stderr, "Failed to get flags for interface "
2495
2464
                         .args_doc = "",
2496
2465
                         .doc = "Mandos client -- Get and decrypt"
2497
2466
                         " passwords from a Mandos server" };
2498
 
    ret_errno = argp_parse(&argp, argc, argv,
2499
 
                           ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
2500
 
    switch(ret_errno){
 
2467
    ret = argp_parse(&argp, argc, argv,
 
2468
                     ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
 
2469
    switch(ret){
2501
2470
    case 0:
2502
2471
      break;
2503
2472
    case ENOMEM:
2504
2473
    default:
2505
 
      errno = ret_errno;
 
2474
      errno = ret;
2506
2475
      perror_plus("argp_parse");
2507
2476
      exitcode = EX_OSERR;
2508
2477
      goto end;
2514
2483
  
2515
2484
  {
2516
2485
    /* Work around Debian bug #633582:
2517
 
       <https://bugs.debian.org/633582> */
 
2486
       <http://bugs.debian.org/633582> */
2518
2487
    
2519
2488
    /* Re-raise privileges */
2520
 
    ret = raise_privileges();
2521
 
    if(ret != 0){
2522
 
      errno = ret;
 
2489
    ret_errno = raise_privileges();
 
2490
    if(ret_errno != 0){
 
2491
      errno = ret_errno;
2523
2492
      perror_plus("Failed to raise privileges");
2524
2493
    } else {
2525
2494
      struct stat st;
2589
2558
      }
2590
2559
      
2591
2560
      /* Lower privileges */
2592
 
      ret = lower_privileges();
2593
 
      if(ret != 0){
2594
 
        errno = ret;
 
2561
      ret_errno = lower_privileges();
 
2562
      if(ret_errno != 0){
 
2563
        errno = ret_errno;
2595
2564
        perror_plus("Failed to lower privileges");
2596
2565
      }
2597
2566
    }
2925
2894
    
2926
2895
    /* Allocate a new server */
2927
2896
    mc.server = avahi_server_new(avahi_simple_poll_get(simple_poll),
2928
 
                                 &config, NULL, NULL, &ret);
 
2897
                                 &config, NULL, NULL, &ret_errno);
2929
2898
    
2930
2899
    /* Free the Avahi configuration data */
2931
2900
    avahi_server_config_free(&config);
2934
2903
  /* Check if creating the Avahi server object succeeded */
2935
2904
  if(mc.server == NULL){
2936
2905
    fprintf_plus(stderr, "Failed to create Avahi server: %s\n",
2937
 
                 avahi_strerror(ret));
 
2906
                 avahi_strerror(ret_errno));
2938
2907
    exitcode = EX_UNAVAILABLE;
2939
2908
    goto end;
2940
2909
  }
2975
2944
 end:
2976
2945
  
2977
2946
  if(debug){
2978
 
    if(signal_received){
2979
 
      fprintf_plus(stderr, "%s exiting due to signal %d: %s\n",
2980
 
                   argv[0], signal_received,
2981
 
                   strsignal(signal_received));
2982
 
    } else {
2983
 
      fprintf_plus(stderr, "%s exiting\n", argv[0]);
2984
 
    }
 
2947
    fprintf_plus(stderr, "%s exiting\n", argv[0]);
2985
2948
  }
2986
2949
  
2987
2950
  /* Cleanup things */
3026
2989
  
3027
2990
  /* Re-raise privileges */
3028
2991
  {
3029
 
    ret = raise_privileges();
3030
 
    if(ret != 0){
3031
 
      errno = ret;
 
2992
    ret_errno = raise_privileges();
 
2993
    if(ret_errno != 0){
 
2994
      errno = ret_errno;
3032
2995
      perror_plus("Failed to raise privileges");
3033
2996
    } else {
3034
2997
      
3039
3002
      /* Take down the network interfaces which were brought up */
3040
3003
      {
3041
3004
        char *interface = NULL;
3042
 
        while((interface = argz_next(interfaces_to_take_down,
3043
 
                                     interfaces_to_take_down_size,
3044
 
                                     interface))){
3045
 
          ret = take_down_interface(interface);
3046
 
          if(ret != 0){
3047
 
            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;
3048
3011
            perror_plus("Failed to take down interface");
3049
3012
          }
3050
3013
        }
3055
3018
      }
3056
3019
    }
3057
3020
    
3058
 
    ret = lower_privileges_permanently();
3059
 
    if(ret != 0){
3060
 
      errno = ret;
 
3021
    ret_errno = lower_privileges_permanently();
 
3022
    if(ret_errno != 0){
 
3023
      errno = ret_errno;
3061
3024
      perror_plus("Failed to lower privileges permanently");
3062
3025
    }
3063
3026
  }
3076
3039
                                                | O_PATH));
3077
3040
    if(dir_fd == -1){
3078
3041
      perror_plus("open");
3079
 
      return;
3080
3042
    }
3081
3043
    int numentries = scandirat(dir_fd, ".", &direntries,
3082
3044
                               notdotentries, alphasort);
3099
3061
            clean_dir_at(dir_fd, direntries[i]->d_name, level+1);
3100
3062
            dret = 0;
3101
3063
          }
3102
 
          if((dret == -1) and (errno != ENOENT)){
 
3064
          if(dret == -1){
3103
3065
            fprintf_plus(stderr, "unlink(\"%s/%s\"): %s\n", dirname,
3104
3066
                         direntries[i]->d_name, strerror(errno));
3105
3067
          }
3109
3071
      
3110
3072
      /* need to clean even if 0 because man page doesn't specify */
3111
3073
      free(direntries);
 
3074
      if(numentries == -1){
 
3075
        perror_plus("scandirat");
 
3076
      }
3112
3077
      dret = unlinkat(base, dirname, AT_REMOVEDIR);
3113
3078
      if(dret == -1 and errno != ENOENT){
3114
3079
        perror_plus("rmdir");