/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: 2018-04-01 19:27:30 UTC
  • Revision ID: teddy@recompile.se-20180401192730-jwr4io5cb3x5u268
Don't print NULL string pointers in error message output

* plugins.d/mandos-client.c (pgp_packet_decrypt): If decryption fails,
  don't print unsupported_algorithm if it is NULL.

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
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
 
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
21
23
 * WITHOUT ANY WARRANTY; without even the implied warranty of
22
24
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23
25
 * General Public License for more details.
24
26
 * 
25
27
 * You should have received a copy of the GNU General Public License
26
 
 * along with this program.  If not, see
27
 
 * <http://www.gnu.org/licenses/>.
 
28
 * along with Mandos.  If not, see <http://www.gnu.org/licenses/>.
28
29
 * 
29
30
 * Contact the authors at <mandos@recompile.se>.
30
31
 */
46
47
#include <stdlib.h>             /* free(), EXIT_SUCCESS, srand(),
47
48
                                   strtof(), abort() */
48
49
#include <stdbool.h>            /* bool, false, true */
49
 
#include <string.h>             /* memset(), strcmp(), strlen(),
50
 
                                   strerror(), asprintf(), strcpy() */
 
50
#include <string.h>             /* strcmp(), strlen(), strerror(),
 
51
                                   asprintf(), strncpy(), strsignal()
 
52
                                */
51
53
#include <sys/ioctl.h>          /* ioctl */
52
54
#include <sys/types.h>          /* socket(), inet_pton(), sockaddr,
53
55
                                   sockaddr_in6, PF_INET6,
57
59
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
58
60
                                   inet_pton(), connect(),
59
61
                                   getnameinfo() */
60
 
#include <fcntl.h>              /* open(), unlinkat() */
 
62
#include <fcntl.h>              /* open(), unlinkat(), AT_REMOVEDIR */
61
63
#include <dirent.h>             /* opendir(), struct dirent, readdir()
62
64
                                 */
63
65
#include <inttypes.h>           /* PRIu16, PRIdMAX, intmax_t,
64
66
                                   strtoimax() */
65
 
#include <errno.h>              /* perror(), errno,
 
67
#include <errno.h>              /* perror(), errno, EINTR, EINVAL,
 
68
                                   EAI_SYSTEM, ENETUNREACH,
 
69
                                   EHOSTUNREACH, ECONNREFUSED, EPROTO,
 
70
                                   EIO, ENOENT, ENXIO, ENOMEM, EISDIR,
 
71
                                   ENOTEMPTY,
66
72
                                   program_invocation_short_name */
67
73
#include <time.h>               /* nanosleep(), time(), sleep() */
68
74
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
305
311
      return false;
306
312
    }
307
313
    
308
 
    ret = (int)TEMP_FAILURE_RETRY(close(fd));
 
314
    ret = close(fd);
309
315
    if(ret == -1){
310
316
      perror_plus("close");
311
317
    }
414
420
      if(result == NULL){
415
421
        fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
416
422
      } else {
417
 
        fprintf_plus(stderr, "Unsupported algorithm: %s\n",
418
 
                     result->unsupported_algorithm);
 
423
        if(result->unsupported_algorithm != NULL) {
 
424
          fprintf_plus(stderr, "Unsupported algorithm: %s\n",
 
425
                       result->unsupported_algorithm);
 
426
        }
419
427
        fprintf_plus(stderr, "Wrong key usage: %u\n",
420
428
                     result->wrong_key_usage);
421
429
        if(result->file_name != NULL){
513
521
  fprintf_plus(stderr, "GnuTLS: %s", string);
514
522
}
515
523
 
516
 
__attribute__((nonnull, warn_unused_result))
 
524
__attribute__((nonnull(1, 2, 4), warn_unused_result))
517
525
static int init_gnutls_global(const char *pubkeyfilename,
518
526
                              const char *seckeyfilename,
519
527
                              const char *dhparamsfilename,
525
533
    fprintf_plus(stderr, "Initializing GnuTLS\n");
526
534
  }
527
535
  
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
536
  if(debug){
536
537
    /* "Use a log level over 10 to enable all debugging options."
537
538
     * - GnuTLS manual
545
546
  if(ret != GNUTLS_E_SUCCESS){
546
547
    fprintf_plus(stderr, "GnuTLS memory error: %s\n",
547
548
                 safer_gnutls_strerror(ret));
548
 
    gnutls_global_deinit();
549
549
    return -1;
550
550
  }
551
551
  
615
615
        }
616
616
        params.size += (unsigned int)bytes_read;
617
617
      }
 
618
      ret = close(dhpfile);
 
619
      if(ret == -1){
 
620
        perror_plus("close");
 
621
      }
618
622
      if(params.data == NULL){
619
623
        dhparamsfilename = NULL;
620
624
      }
629
633
                     safer_gnutls_strerror(ret));
630
634
        dhparamsfilename = NULL;
631
635
      }
 
636
      free(params.data);
632
637
    } while(false);
633
638
  }
634
639
  if(dhparamsfilename == NULL){
755
760
 globalfail:
756
761
  
757
762
  gnutls_certificate_free_credentials(mc->cred);
758
 
  gnutls_global_deinit();
759
763
  gnutls_dh_params_deinit(mc->dh_params);
760
764
  return -1;
761
765
}
822
826
 
823
827
/* Set effective uid to 0, return errno */
824
828
__attribute__((warn_unused_result))
825
 
error_t raise_privileges(void){
826
 
  error_t old_errno = errno;
827
 
  error_t ret_errno = 0;
 
829
int raise_privileges(void){
 
830
  int old_errno = errno;
 
831
  int ret = 0;
828
832
  if(seteuid(0) == -1){
829
 
    ret_errno = errno;
 
833
    ret = errno;
830
834
  }
831
835
  errno = old_errno;
832
 
  return ret_errno;
 
836
  return ret;
833
837
}
834
838
 
835
839
/* Set effective and real user ID to 0.  Return errno. */
836
840
__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){
 
841
int raise_privileges_permanently(void){
 
842
  int old_errno = errno;
 
843
  int ret = raise_privileges();
 
844
  if(ret != 0){
841
845
    errno = old_errno;
842
 
    return ret_errno;
 
846
    return ret;
843
847
  }
844
848
  if(setuid(0) == -1){
845
 
    ret_errno = errno;
 
849
    ret = errno;
846
850
  }
847
851
  errno = old_errno;
848
 
  return ret_errno;
 
852
  return ret;
849
853
}
850
854
 
851
855
/* Set effective user ID to unprivileged saved user ID */
852
856
__attribute__((warn_unused_result))
853
 
error_t lower_privileges(void){
854
 
  error_t old_errno = errno;
855
 
  error_t ret_errno = 0;
 
857
int lower_privileges(void){
 
858
  int old_errno = errno;
 
859
  int ret = 0;
856
860
  if(seteuid(uid) == -1){
857
 
    ret_errno = errno;
 
861
    ret = errno;
858
862
  }
859
863
  errno = old_errno;
860
 
  return ret_errno;
 
864
  return ret;
861
865
}
862
866
 
863
867
/* Lower privileges permanently */
864
868
__attribute__((warn_unused_result))
865
 
error_t lower_privileges_permanently(void){
866
 
  error_t old_errno = errno;
867
 
  error_t ret_errno = 0;
 
869
int lower_privileges_permanently(void){
 
870
  int old_errno = errno;
 
871
  int ret = 0;
868
872
  if(setuid(uid) == -1){
869
 
    ret_errno = errno;
 
873
    ret = errno;
870
874
  }
871
875
  errno = old_errno;
872
 
  return ret_errno;
 
876
  return ret;
873
877
}
874
878
 
875
879
/* Helper function to add_local_route() and delete_local_route() */
931
935
      perror_plus("dup2(devnull, STDIN_FILENO)");
932
936
      _exit(EX_OSERR);
933
937
    }
934
 
    ret = (int)TEMP_FAILURE_RETRY(close(devnull));
 
938
    ret = close(devnull);
935
939
    if(ret == -1){
936
940
      perror_plus("close");
937
941
      _exit(EX_OSERR);
954
958
                                                   helper, O_RDONLY));
955
959
    if(helper_fd == -1){
956
960
      perror_plus("openat");
 
961
      close(helperdir_fd);
957
962
      _exit(EX_UNAVAILABLE);
958
963
    }
959
 
    TEMP_FAILURE_RETRY(close(helperdir_fd));
 
964
    close(helperdir_fd);
960
965
#ifdef __GNUC__
961
966
#pragma GCC diagnostic push
962
967
#pragma GCC diagnostic ignored "-Wcast-qual"
1081
1086
    bool match = false;
1082
1087
    {
1083
1088
      char *interface = NULL;
1084
 
      while((interface=argz_next(mc->interfaces, mc->interfaces_size,
1085
 
                                 interface))){
 
1089
      while((interface = argz_next(mc->interfaces,
 
1090
                                   mc->interfaces_size,
 
1091
                                   interface))){
1086
1092
        if(if_nametoindex(interface) == (unsigned int)if_index){
1087
1093
          match = true;
1088
1094
          break;
1130
1136
    goto mandos_end;
1131
1137
  }
1132
1138
  
1133
 
  memset(&to, 0, sizeof(to));
1134
1139
  if(af == AF_INET6){
1135
 
    ((struct sockaddr_in6 *)&to)->sin6_family = (sa_family_t)af;
1136
 
    ret = inet_pton(af, ip, &((struct sockaddr_in6 *)&to)->sin6_addr);
 
1140
    struct sockaddr_in6 *to6 = (struct sockaddr_in6 *)&to;
 
1141
    *to6 = (struct sockaddr_in6){ .sin6_family = (sa_family_t)af };
 
1142
    ret = inet_pton(af, ip, &to6->sin6_addr);
1137
1143
  } else {                      /* IPv4 */
1138
 
    ((struct sockaddr_in *)&to)->sin_family = (sa_family_t)af;
1139
 
    ret = inet_pton(af, ip, &((struct sockaddr_in *)&to)->sin_addr);
 
1144
    struct sockaddr_in *to4 = (struct sockaddr_in *)&to;
 
1145
    *to4 = (struct sockaddr_in){ .sin_family = (sa_family_t)af };
 
1146
    ret = inet_pton(af, ip, &to4->sin_addr);
1140
1147
  }
1141
1148
  if(ret < 0 ){
1142
1149
    int e = errno;
1221
1228
                    sizeof(struct sockaddr_in));
1222
1229
    }
1223
1230
    if(ret < 0){
1224
 
      if(errno == ENETUNREACH
 
1231
      if(((errno == ENETUNREACH) or (errno == EHOSTUNREACH))
1225
1232
         and if_index != AVAHI_IF_UNSPEC
1226
1233
         and connect_to == NULL
1227
1234
         and not route_added and
1240
1247
           with an explicit route added with the server's address.
1241
1248
           
1242
1249
           Avahi bug reference:
1243
 
           http://lists.freedesktop.org/archives/avahi/2010-February/001833.html
 
1250
           https://lists.freedesktop.org/archives/avahi/2010-February/001833.html
1244
1251
           https://bugs.debian.org/587961
1245
1252
        */
1246
1253
        if(debug){
1426
1433
                                               &decrypted_buffer, mc);
1427
1434
    if(decrypted_buffer_size >= 0){
1428
1435
      
 
1436
      clearerr(stdout);
1429
1437
      written = 0;
1430
1438
      while(written < (size_t) decrypted_buffer_size){
1431
1439
        if(quit_now){
1447
1455
        }
1448
1456
        written += (size_t)ret;
1449
1457
      }
 
1458
      ret = fflush(stdout);
 
1459
      if(ret != 0){
 
1460
        int e = errno;
 
1461
        if(debug){
 
1462
          fprintf_plus(stderr, "Error writing encrypted data: %s\n",
 
1463
                       strerror(errno));
 
1464
        }
 
1465
        errno = e;
 
1466
        goto mandos_end;
 
1467
      }
1450
1468
      retval = 0;
1451
1469
    }
1452
1470
  }
1465
1483
    free(decrypted_buffer);
1466
1484
    free(buffer);
1467
1485
    if(tcp_sd >= 0){
1468
 
      ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
 
1486
      ret = close(tcp_sd);
1469
1487
    }
1470
1488
    if(ret == -1){
1471
1489
      if(e == 0){
1483
1501
  return retval;
1484
1502
}
1485
1503
 
1486
 
__attribute__((nonnull))
1487
1504
static void resolve_callback(AvahiSServiceResolver *r,
1488
1505
                             AvahiIfIndex interface,
1489
1506
                             AvahiProtocol proto,
1626
1643
__attribute__((nonnull, warn_unused_result))
1627
1644
bool get_flags(const char *ifname, struct ifreq *ifr){
1628
1645
  int ret;
1629
 
  error_t ret_errno;
 
1646
  int old_errno;
1630
1647
  
1631
1648
  int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1632
1649
  if(s < 0){
1633
 
    ret_errno = errno;
 
1650
    old_errno = errno;
1634
1651
    perror_plus("socket");
1635
 
    errno = ret_errno;
 
1652
    errno = old_errno;
1636
1653
    return false;
1637
1654
  }
1638
 
  strcpy(ifr->ifr_name, ifname);
 
1655
  strncpy(ifr->ifr_name, ifname, IF_NAMESIZE);
 
1656
  ifr->ifr_name[IF_NAMESIZE-1] = '\0'; /* NUL terminate */
1639
1657
  ret = ioctl(s, SIOCGIFFLAGS, ifr);
1640
1658
  if(ret == -1){
1641
1659
    if(debug){
1642
 
      ret_errno = errno;
 
1660
      old_errno = errno;
1643
1661
      perror_plus("ioctl SIOCGIFFLAGS");
1644
 
      errno = ret_errno;
 
1662
      errno = old_errno;
 
1663
    }
 
1664
    if((close(s) == -1) and debug){
 
1665
      old_errno = errno;
 
1666
      perror_plus("close");
 
1667
      errno = old_errno;
1645
1668
    }
1646
1669
    return false;
1647
1670
  }
 
1671
  if((close(s) == -1) and debug){
 
1672
    old_errno = errno;
 
1673
    perror_plus("close");
 
1674
    errno = old_errno;
 
1675
  }
1648
1676
  return true;
1649
1677
}
1650
1678
 
1911
1939
      return;
1912
1940
    }
1913
1941
  }
1914
 
#ifdef __GLIBC__
1915
 
#if __GLIBC_PREREQ(2, 15)
 
1942
  int devnull = (int)TEMP_FAILURE_RETRY(open("/dev/null", O_RDONLY));
 
1943
  if(devnull == -1){
 
1944
    perror_plus("open(\"/dev/null\", O_RDONLY)");
 
1945
    return;
 
1946
  }
1916
1947
  int numhooks = scandirat(hookdir_fd, ".", &direntries,
1917
1948
                           runnable_hook, alphasort);
1918
 
#else  /* not __GLIBC_PREREQ(2, 15) */
1919
 
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1920
 
                         alphasort);
1921
 
#endif  /* not __GLIBC_PREREQ(2, 15) */
1922
 
#else   /* not __GLIBC__ */
1923
 
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1924
 
                         alphasort);
1925
 
#endif  /* not __GLIBC__ */
1926
1949
  if(numhooks == -1){
1927
1950
    perror_plus("scandir");
 
1951
    close(devnull);
1928
1952
    return;
1929
1953
  }
1930
1954
  struct dirent *direntry;
1931
1955
  int ret;
1932
 
  int devnull = (int)TEMP_FAILURE_RETRY(open("/dev/null", O_RDONLY));
1933
 
  if(devnull == -1){
1934
 
    perror_plus("open(\"/dev/null\", O_RDONLY)");
1935
 
    return;
1936
 
  }
1937
1956
  for(int i = 0; i < numhooks; i++){
1938
1957
    direntry = direntries[i];
1939
1958
    if(debug){
2010
2029
        perror_plus("openat");
2011
2030
        _exit(EXIT_FAILURE);
2012
2031
      }
2013
 
      if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
 
2032
      if(close(hookdir_fd) == -1){
2014
2033
        perror_plus("close");
2015
2034
        _exit(EXIT_FAILURE);
2016
2035
      }
2019
2038
        perror_plus("dup2(devnull, STDIN_FILENO)");
2020
2039
        _exit(EX_OSERR);
2021
2040
      }
2022
 
      ret = (int)TEMP_FAILURE_RETRY(close(devnull));
 
2041
      ret = close(devnull);
2023
2042
      if(ret == -1){
2024
2043
        perror_plus("close");
2025
2044
        _exit(EX_OSERR);
2074
2093
    free(direntry);
2075
2094
  }
2076
2095
  free(direntries);
2077
 
  if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
 
2096
  if(close(hookdir_fd) == -1){
2078
2097
    perror_plus("close");
2079
2098
  } else {
2080
2099
    hookdir_fd = -1;
2083
2102
}
2084
2103
 
2085
2104
__attribute__((nonnull, warn_unused_result))
2086
 
error_t bring_up_interface(const char *const interface,
2087
 
                           const float delay){
2088
 
  error_t old_errno = errno;
 
2105
int bring_up_interface(const char *const interface,
 
2106
                       const float delay){
 
2107
  int old_errno = errno;
2089
2108
  int ret;
2090
2109
  struct ifreq network;
2091
2110
  unsigned int if_index = if_nametoindex(interface);
2101
2120
  }
2102
2121
  
2103
2122
  if(not interface_is_up(interface)){
2104
 
    error_t ret_errno = 0, ioctl_errno = 0;
 
2123
    int ret_errno = 0;
 
2124
    int ioctl_errno = 0;
2105
2125
    if(not get_flags(interface, &network)){
2106
2126
      ret_errno = errno;
2107
2127
      fprintf_plus(stderr, "Failed to get flags for interface "
2120
2140
    }
2121
2141
    
2122
2142
    if(quit_now){
2123
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
2143
      ret = close(sd);
2124
2144
      if(ret == -1){
2125
2145
        perror_plus("close");
2126
2146
      }
2176
2196
    }
2177
2197
    
2178
2198
    /* Close the socket */
2179
 
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
2199
    ret = close(sd);
2180
2200
    if(ret == -1){
2181
2201
      perror_plus("close");
2182
2202
    }
2194
2214
  
2195
2215
  /* Sleep checking until interface is running.
2196
2216
     Check every 0.25s, up to total time of delay */
2197
 
  for(int i=0; i < delay * 4; i++){
 
2217
  for(int i = 0; i < delay * 4; i++){
2198
2218
    if(interface_is_running(interface)){
2199
2219
      break;
2200
2220
    }
2210
2230
}
2211
2231
 
2212
2232
__attribute__((nonnull, warn_unused_result))
2213
 
error_t take_down_interface(const char *const interface){
2214
 
  error_t old_errno = errno;
 
2233
int take_down_interface(const char *const interface){
 
2234
  int old_errno = errno;
2215
2235
  struct ifreq network;
2216
2236
  unsigned int if_index = if_nametoindex(interface);
2217
2237
  if(if_index == 0){
2220
2240
    return ENXIO;
2221
2241
  }
2222
2242
  if(interface_is_up(interface)){
2223
 
    error_t ret_errno = 0, ioctl_errno = 0;
 
2243
    int ret_errno = 0;
 
2244
    int ioctl_errno = 0;
2224
2245
    if(not get_flags(interface, &network) and debug){
2225
2246
      ret_errno = errno;
2226
2247
      fprintf_plus(stderr, "Failed to get flags for interface "
2264
2285
    }
2265
2286
    
2266
2287
    /* Close the socket */
2267
 
    int ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
2288
    int ret = close(sd);
2268
2289
    if(ret == -1){
2269
2290
      perror_plus("close");
2270
2291
    }
2286
2307
 
2287
2308
int main(int argc, char *argv[]){
2288
2309
  mandos_context mc = { .server = NULL, .dh_bits = 0,
2289
 
                        .priority = "SECURE256:!CTYPE-X.509:"
2290
 
                        "+CTYPE-OPENPGP:!RSA", .current_server = NULL,
2291
 
                        .interfaces = NULL, .interfaces_size = 0 };
 
2310
                        .priority = "SECURE256:!CTYPE-X.509"
 
2311
                        ":+CTYPE-OPENPGP:!RSA:+SIGN-DSA-SHA256",
 
2312
                        .current_server = NULL, .interfaces = NULL,
 
2313
                        .interfaces_size = 0 };
2292
2314
  AvahiSServiceBrowser *sb = NULL;
2293
2315
  error_t ret_errno;
2294
2316
  int ret;
2475
2497
                         .args_doc = "",
2476
2498
                         .doc = "Mandos client -- Get and decrypt"
2477
2499
                         " passwords from a Mandos server" };
2478
 
    ret = argp_parse(&argp, argc, argv,
2479
 
                     ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
2480
 
    switch(ret){
 
2500
    ret_errno = argp_parse(&argp, argc, argv,
 
2501
                           ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
 
2502
    switch(ret_errno){
2481
2503
    case 0:
2482
2504
      break;
2483
2505
    case ENOMEM:
2484
2506
    default:
2485
 
      errno = ret;
 
2507
      errno = ret_errno;
2486
2508
      perror_plus("argp_parse");
2487
2509
      exitcode = EX_OSERR;
2488
2510
      goto end;
2494
2516
  
2495
2517
  {
2496
2518
    /* Work around Debian bug #633582:
2497
 
       <http://bugs.debian.org/633582> */
 
2519
       <https://bugs.debian.org/633582> */
2498
2520
    
2499
2521
    /* Re-raise privileges */
2500
 
    ret_errno = raise_privileges();
2501
 
    if(ret_errno != 0){
2502
 
      errno = ret_errno;
 
2522
    ret = raise_privileges();
 
2523
    if(ret != 0){
 
2524
      errno = ret;
2503
2525
      perror_plus("Failed to raise privileges");
2504
2526
    } else {
2505
2527
      struct stat st;
2521
2543
              }
2522
2544
            }
2523
2545
          }
2524
 
          TEMP_FAILURE_RETRY(close(seckey_fd));
 
2546
          close(seckey_fd);
2525
2547
        }
2526
2548
      }
2527
2549
      
2542
2564
              }
2543
2565
            }
2544
2566
          }
2545
 
          TEMP_FAILURE_RETRY(close(pubkey_fd));
 
2567
          close(pubkey_fd);
2546
2568
        }
2547
2569
      }
2548
2570
      
2549
 
      if(strcmp(dh_params_file, PATHDIR "/dhparams.pem" ) == 0){
 
2571
      if(dh_params_file != NULL
 
2572
         and strcmp(dh_params_file, PATHDIR "/dhparams.pem" ) == 0){
2550
2573
        int dhparams_fd = open(dh_params_file, O_RDONLY);
2551
2574
        if(dhparams_fd == -1){
2552
2575
          perror_plus("open");
2563
2586
              }
2564
2587
            }
2565
2588
          }
2566
 
          TEMP_FAILURE_RETRY(close(dhparams_fd));
 
2589
          close(dhparams_fd);
2567
2590
        }
2568
2591
      }
2569
2592
      
2570
2593
      /* Lower privileges */
2571
 
      ret_errno = lower_privileges();
2572
 
      if(ret_errno != 0){
2573
 
        errno = ret_errno;
 
2594
      ret = lower_privileges();
 
2595
      if(ret != 0){
 
2596
        errno = ret;
2574
2597
        perror_plus("Failed to lower privileges");
2575
2598
      }
2576
2599
    }
2904
2927
    
2905
2928
    /* Allocate a new server */
2906
2929
    mc.server = avahi_server_new(avahi_simple_poll_get(simple_poll),
2907
 
                                 &config, NULL, NULL, &ret_errno);
 
2930
                                 &config, NULL, NULL, &ret);
2908
2931
    
2909
2932
    /* Free the Avahi configuration data */
2910
2933
    avahi_server_config_free(&config);
2913
2936
  /* Check if creating the Avahi server object succeeded */
2914
2937
  if(mc.server == NULL){
2915
2938
    fprintf_plus(stderr, "Failed to create Avahi server: %s\n",
2916
 
                 avahi_strerror(ret_errno));
 
2939
                 avahi_strerror(ret));
2917
2940
    exitcode = EX_UNAVAILABLE;
2918
2941
    goto end;
2919
2942
  }
2954
2977
 end:
2955
2978
  
2956
2979
  if(debug){
2957
 
    fprintf_plus(stderr, "%s exiting\n", argv[0]);
 
2980
    if(signal_received){
 
2981
      fprintf_plus(stderr, "%s exiting due to signal %d: %s\n",
 
2982
                   argv[0], signal_received,
 
2983
                   strsignal(signal_received));
 
2984
    } else {
 
2985
      fprintf_plus(stderr, "%s exiting\n", argv[0]);
 
2986
    }
2958
2987
  }
2959
2988
  
2960
2989
  /* Cleanup things */
2971
3000
  
2972
3001
  if(gnutls_initialized){
2973
3002
    gnutls_certificate_free_credentials(mc.cred);
2974
 
    gnutls_global_deinit();
2975
3003
    gnutls_dh_params_deinit(mc.dh_params);
2976
3004
  }
2977
3005
  
3000
3028
  
3001
3029
  /* Re-raise privileges */
3002
3030
  {
3003
 
    ret_errno = raise_privileges();
3004
 
    if(ret_errno != 0){
3005
 
      errno = ret_errno;
 
3031
    ret = raise_privileges();
 
3032
    if(ret != 0){
 
3033
      errno = ret;
3006
3034
      perror_plus("Failed to raise privileges");
3007
3035
    } else {
3008
3036
      
3013
3041
      /* Take down the network interfaces which were brought up */
3014
3042
      {
3015
3043
        char *interface = NULL;
3016
 
        while((interface=argz_next(interfaces_to_take_down,
3017
 
                                   interfaces_to_take_down_size,
3018
 
                                   interface))){
3019
 
          ret_errno = take_down_interface(interface);
3020
 
          if(ret_errno != 0){
3021
 
            errno = ret_errno;
 
3044
        while((interface = argz_next(interfaces_to_take_down,
 
3045
                                     interfaces_to_take_down_size,
 
3046
                                     interface))){
 
3047
          ret = take_down_interface(interface);
 
3048
          if(ret != 0){
 
3049
            errno = ret;
3022
3050
            perror_plus("Failed to take down interface");
3023
3051
          }
3024
3052
        }
3029
3057
      }
3030
3058
    }
3031
3059
    
3032
 
    ret_errno = lower_privileges_permanently();
3033
 
    if(ret_errno != 0){
3034
 
      errno = ret_errno;
 
3060
    ret = lower_privileges_permanently();
 
3061
    if(ret != 0){
 
3062
      errno = ret;
3035
3063
      perror_plus("Failed to lower privileges permanently");
3036
3064
    }
3037
3065
  }
3039
3067
  free(interfaces_to_take_down);
3040
3068
  free(interfaces_hooks);
3041
3069
  
 
3070
  void clean_dir_at(int base, const char * const dirname,
 
3071
                    uintmax_t level){
 
3072
    struct dirent **direntries = NULL;
 
3073
    int dret;
 
3074
    int dir_fd = (int)TEMP_FAILURE_RETRY(openat(base, dirname,
 
3075
                                                O_RDONLY
 
3076
                                                | O_NOFOLLOW
 
3077
                                                | O_DIRECTORY
 
3078
                                                | O_PATH));
 
3079
    if(dir_fd == -1){
 
3080
      perror_plus("open");
 
3081
      return;
 
3082
    }
 
3083
    int numentries = scandirat(dir_fd, ".", &direntries,
 
3084
                               notdotentries, alphasort);
 
3085
    if(numentries >= 0){
 
3086
      for(int i = 0; i < numentries; i++){
 
3087
        if(debug){
 
3088
          fprintf_plus(stderr, "Unlinking \"%s/%s\"\n",
 
3089
                       dirname, direntries[i]->d_name);
 
3090
        }
 
3091
        dret = unlinkat(dir_fd, direntries[i]->d_name, 0);
 
3092
        if(dret == -1){
 
3093
          if(errno == EISDIR){
 
3094
              dret = unlinkat(dir_fd, direntries[i]->d_name,
 
3095
                              AT_REMOVEDIR);
 
3096
          }         
 
3097
          if((dret == -1) and (errno == ENOTEMPTY)
 
3098
             and (strcmp(direntries[i]->d_name, "private-keys-v1.d")
 
3099
                  == 0) and (level == 0)){
 
3100
            /* Recurse only in this special case */
 
3101
            clean_dir_at(dir_fd, direntries[i]->d_name, level+1);
 
3102
            dret = 0;
 
3103
          }
 
3104
          if((dret == -1) and (errno != ENOENT)){
 
3105
            fprintf_plus(stderr, "unlink(\"%s/%s\"): %s\n", dirname,
 
3106
                         direntries[i]->d_name, strerror(errno));
 
3107
          }
 
3108
        }
 
3109
        free(direntries[i]);
 
3110
      }
 
3111
      
 
3112
      /* need to clean even if 0 because man page doesn't specify */
 
3113
      free(direntries);
 
3114
      dret = unlinkat(base, dirname, AT_REMOVEDIR);
 
3115
      if(dret == -1 and errno != ENOENT){
 
3116
        perror_plus("rmdir");
 
3117
      }
 
3118
    } else {
 
3119
      perror_plus("scandirat");
 
3120
    }
 
3121
    close(dir_fd);
 
3122
  }
 
3123
  
3042
3124
  /* Removes the GPGME temp directory and all files inside */
3043
3125
  if(tempdir != NULL){
3044
 
    struct dirent **direntries = NULL;
3045
 
    int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY
3046
 
                                                  | O_NOFOLLOW
3047
 
                                                  | O_DIRECTORY
3048
 
                                                  | O_PATH));
3049
 
    if(tempdir_fd == -1){
3050
 
      perror_plus("open");
3051
 
    } else {
3052
 
#ifdef __GLIBC__
3053
 
#if __GLIBC_PREREQ(2, 15)
3054
 
      int numentries = scandirat(tempdir_fd, ".", &direntries,
3055
 
                                 notdotentries, alphasort);
3056
 
#else  /* not __GLIBC_PREREQ(2, 15) */
3057
 
      int numentries = scandir(tempdir, &direntries, notdotentries,
3058
 
                               alphasort);
3059
 
#endif  /* not __GLIBC_PREREQ(2, 15) */
3060
 
#else   /* not __GLIBC__ */
3061
 
      int numentries = scandir(tempdir, &direntries, notdotentries,
3062
 
                               alphasort);
3063
 
#endif  /* not __GLIBC__ */
3064
 
      if(numentries >= 0){
3065
 
        for(int i = 0; i < numentries; i++){
3066
 
          ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
3067
 
          if(ret == -1){
3068
 
            fprintf_plus(stderr, "unlinkat(open(\"%s\", O_RDONLY),"
3069
 
                         " \"%s\", 0): %s\n", tempdir,
3070
 
                         direntries[i]->d_name, strerror(errno));
3071
 
          }
3072
 
          free(direntries[i]);
3073
 
        }
3074
 
        
3075
 
        /* need to clean even if 0 because man page doesn't specify */
3076
 
        free(direntries);
3077
 
        if(numentries == -1){
3078
 
          perror_plus("scandir");
3079
 
        }
3080
 
        ret = rmdir(tempdir);
3081
 
        if(ret == -1 and errno != ENOENT){
3082
 
          perror_plus("rmdir");
3083
 
        }
3084
 
      }
3085
 
      TEMP_FAILURE_RETRY(close(tempdir_fd));
3086
 
    }
 
3126
    clean_dir_at(-1, tempdir, 0);
3087
3127
  }
3088
3128
  
3089
3129
  if(quit_now){