/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: 2015-07-06 20:09:47 UTC
  • mto: This revision was merged to the branch mainline in revision 759.
  • Revision ID: teddy@recompile.se-20150706200947-w21u4eq74efgl6r5
Fix minor bugs and typos and add some more debug output.

* Makefile (install-client-nokey): Create plugin-helpers directory and
                                   the mandos-client-iprouteadddel
                                   helper program.
* initramfs-tools-hook (PLUGINHELPERDIR): Fix typo.
* plugins.d/mandos-client.c: Change terminology; routes are "deleted",
                             not "removed".  All occurences changed.
  (add_remove_local_route): Renamed to "add_delete_local_route".  All
                            callers changed.  Also pass "--debug" flag
                            to helper if in debug mode.
  (add_local_route): Add debugging output.
  (remove_local_route): Renamed to "delete_local_route".  All callers
                        changed.  Also pass "--debug" flag to helper
                        if in debug mode.
  (start_mandos_communication): Add debug output when adding route.

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-2014 Teddy Hogeborn
13
 
 * Copyright © 2008-2014 Björn Påhlsson
 
12
 * Copyright © 2008-2015 Teddy Hogeborn
 
13
 * Copyright © 2008-2015 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
234
234
                          .af = af };
235
235
  if(new_server->ip == NULL){
236
236
    perror_plus("strdup");
 
237
    free(new_server);
237
238
    return false;
238
239
  }
239
240
  ret = clock_gettime(CLOCK_MONOTONIC, &(new_server->last_seen));
240
241
  if(ret == -1){
241
242
    perror_plus("clock_gettime");
 
243
#ifdef __GNUC__
 
244
#pragma GCC diagnostic push
 
245
#pragma GCC diagnostic ignored "-Wcast-qual"
 
246
#endif
 
247
    free((char *)(new_server->ip));
 
248
#ifdef __GNUC__
 
249
#pragma GCC diagnostic pop
 
250
#endif
 
251
    free(new_server);
242
252
    return false;
243
253
  }
244
254
  /* Special case of first server */
640
650
static void empty_log(__attribute__((unused)) AvahiLogLevel level,
641
651
                      __attribute__((unused)) const char *txt){}
642
652
 
 
653
/* Set effective uid to 0, return errno */
 
654
__attribute__((warn_unused_result))
 
655
error_t raise_privileges(void){
 
656
  error_t old_errno = errno;
 
657
  error_t ret_errno = 0;
 
658
  if(seteuid(0) == -1){
 
659
    ret_errno = errno;
 
660
  }
 
661
  errno = old_errno;
 
662
  return ret_errno;
 
663
}
 
664
 
 
665
/* Set effective and real user ID to 0.  Return errno. */
 
666
__attribute__((warn_unused_result))
 
667
error_t raise_privileges_permanently(void){
 
668
  error_t old_errno = errno;
 
669
  error_t ret_errno = raise_privileges();
 
670
  if(ret_errno != 0){
 
671
    errno = old_errno;
 
672
    return ret_errno;
 
673
  }
 
674
  if(setuid(0) == -1){
 
675
    ret_errno = errno;
 
676
  }
 
677
  errno = old_errno;
 
678
  return ret_errno;
 
679
}
 
680
 
 
681
/* Set effective user ID to unprivileged saved user ID */
 
682
__attribute__((warn_unused_result))
 
683
error_t lower_privileges(void){
 
684
  error_t old_errno = errno;
 
685
  error_t ret_errno = 0;
 
686
  if(seteuid(uid) == -1){
 
687
    ret_errno = errno;
 
688
  }
 
689
  errno = old_errno;
 
690
  return ret_errno;
 
691
}
 
692
 
 
693
/* Lower privileges permanently */
 
694
__attribute__((warn_unused_result))
 
695
error_t lower_privileges_permanently(void){
 
696
  error_t old_errno = errno;
 
697
  error_t ret_errno = 0;
 
698
  if(setuid(uid) == -1){
 
699
    ret_errno = errno;
 
700
  }
 
701
  errno = old_errno;
 
702
  return ret_errno;
 
703
}
 
704
 
 
705
/* Helper function to add_local_route() and delete_local_route() */
 
706
__attribute__((nonnull, warn_unused_result))
 
707
static bool add_delete_local_route(const bool add,
 
708
                                   const char *address,
 
709
                                   AvahiIfIndex if_index){
 
710
  int ret;
 
711
  char helper[] = "mandos-client-iprouteadddel";
 
712
  char add_arg[] = "add";
 
713
  char delete_arg[] = "delete";
 
714
  char debug_flag[] = "--debug";
 
715
  char *pluginhelperdir = getenv("MANDOSPLUGINHELPERDIR");
 
716
  if(pluginhelperdir == NULL){
 
717
    if(debug){
 
718
      fprintf_plus(stderr, "MANDOSPLUGINHELPERDIR environment"
 
719
                   " variable not set; cannot run helper\n");
 
720
    }
 
721
    return false;
 
722
  }
 
723
  
 
724
  char interface[IF_NAMESIZE];
 
725
  if(if_indextoname((unsigned int)if_index, interface) == NULL){
 
726
    perror_plus("if_indextoname");
 
727
    return false;
 
728
  }
 
729
  
 
730
  int devnull = (int)TEMP_FAILURE_RETRY(open("/dev/null", O_RDONLY));
 
731
  if(devnull == -1){
 
732
    perror_plus("open(\"/dev/null\", O_RDONLY)");
 
733
    return false;
 
734
  }
 
735
  pid_t pid = fork();
 
736
  if(pid == 0){
 
737
    /* Child */
 
738
    /* Raise privileges */
 
739
    errno = raise_privileges_permanently();
 
740
    if(errno != 0){
 
741
      perror_plus("Failed to raise privileges");
 
742
      /* _exit(EX_NOPERM); */
 
743
    } else {
 
744
      /* Set group */
 
745
      errno = 0;
 
746
      ret = setgid(0);
 
747
      if(ret == -1){
 
748
        perror_plus("setgid");
 
749
        _exit(EX_NOPERM);
 
750
      }
 
751
      /* Reset supplementary groups */
 
752
      errno = 0;
 
753
      ret = setgroups(0, NULL);
 
754
      if(ret == -1){
 
755
        perror_plus("setgroups");
 
756
        _exit(EX_NOPERM);
 
757
      }
 
758
    }
 
759
    ret = dup2(devnull, STDIN_FILENO);
 
760
    if(ret == -1){
 
761
      perror_plus("dup2(devnull, STDIN_FILENO)");
 
762
      _exit(EX_OSERR);
 
763
    }
 
764
    ret = (int)TEMP_FAILURE_RETRY(close(devnull));
 
765
    if(ret == -1){
 
766
      perror_plus("close");
 
767
      _exit(EX_OSERR);
 
768
    }
 
769
    ret = dup2(STDERR_FILENO, STDOUT_FILENO);
 
770
    if(ret == -1){
 
771
      perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
 
772
      _exit(EX_OSERR);
 
773
    }
 
774
    int helperdir_fd = (int)TEMP_FAILURE_RETRY(open(pluginhelperdir,
 
775
                                                    O_RDONLY
 
776
                                                    | O_DIRECTORY
 
777
                                                    | O_PATH
 
778
                                                    | O_CLOEXEC));
 
779
    if(helperdir_fd == -1){
 
780
      perror_plus("open");
 
781
      _exit(EX_UNAVAILABLE);
 
782
    }
 
783
    int helper_fd = (int)TEMP_FAILURE_RETRY(openat(helperdir_fd,
 
784
                                                   helper, O_RDONLY));
 
785
    if(helper_fd == -1){
 
786
      perror_plus("openat");
 
787
      _exit(EX_UNAVAILABLE);
 
788
    }
 
789
    TEMP_FAILURE_RETRY(close(helperdir_fd));
 
790
#ifdef __GNUC__
 
791
#pragma GCC diagnostic push
 
792
#pragma GCC diagnostic ignored "-Wcast-qual"
 
793
#endif
 
794
    if(fexecve(helper_fd, (char *const [])
 
795
               { helper, add ? add_arg : delete_arg, (char *)address,
 
796
                   interface, debug ? debug_flag : NULL, NULL },
 
797
               environ) == -1){
 
798
#ifdef __GNUC__
 
799
#pragma GCC diagnostic pop
 
800
#endif
 
801
      perror_plus("fexecve");
 
802
      _exit(EXIT_FAILURE);
 
803
    }
 
804
  }
 
805
  if(pid == -1){
 
806
    perror_plus("fork");
 
807
    return false;
 
808
  }
 
809
  int status;
 
810
  pid_t pret = -1;
 
811
  errno = 0;
 
812
  do {
 
813
    pret = waitpid(pid, &status, 0);
 
814
    if(pret == -1 and errno == EINTR and quit_now){
 
815
      int errno_raising = 0;
 
816
      if((errno = raise_privileges()) != 0){
 
817
        errno_raising = errno;
 
818
        perror_plus("Failed to raise privileges in order to"
 
819
                    " kill helper program");
 
820
      }
 
821
      if(kill(pid, SIGTERM) == -1){
 
822
        perror_plus("kill");
 
823
      }
 
824
      if((errno_raising == 0) and (errno = lower_privileges()) != 0){
 
825
        perror_plus("Failed to lower privileges after killing"
 
826
                    " helper program");
 
827
      }
 
828
      return false;
 
829
    }
 
830
  } while(pret == -1 and errno == EINTR);
 
831
  if(pret == -1){
 
832
    perror_plus("waitpid");
 
833
    return false;
 
834
  }
 
835
  if(WIFEXITED(status)){
 
836
    if(WEXITSTATUS(status) != 0){
 
837
      fprintf_plus(stderr, "Error: iprouteadddel exited"
 
838
                   " with status %d\n", WEXITSTATUS(status));
 
839
      return false;
 
840
    }
 
841
    return true;
 
842
  }
 
843
  if(WIFSIGNALED(status)){
 
844
    fprintf_plus(stderr, "Error: iprouteadddel died by"
 
845
                 " signal %d\n", WTERMSIG(status));
 
846
    return false;
 
847
  }
 
848
  fprintf_plus(stderr, "Error: iprouteadddel crashed\n");
 
849
  return false;
 
850
}
 
851
 
 
852
__attribute__((nonnull, warn_unused_result))
 
853
static bool add_local_route(const char *address,
 
854
                            AvahiIfIndex if_index){
 
855
  if(debug){
 
856
    fprintf_plus(stderr, "Adding route to %s\n", address);
 
857
  }
 
858
  return add_delete_local_route(true, address, if_index);
 
859
}
 
860
 
 
861
__attribute__((nonnull, warn_unused_result))
 
862
static bool delete_local_route(const char *address,
 
863
                               AvahiIfIndex if_index){
 
864
  if(debug){
 
865
    fprintf_plus(stderr, "Removing route to %s\n", address);
 
866
  }
 
867
  return add_delete_local_route(false, address, if_index);
 
868
}
 
869
 
643
870
/* Called when a Mandos server is found */
644
871
__attribute__((nonnull, warn_unused_result))
645
872
static int start_mandos_communication(const char *ip, in_port_t port,
656
883
  int retval = -1;
657
884
  gnutls_session_t session;
658
885
  int pf;                       /* Protocol family */
 
886
  bool route_added = false;
659
887
  
660
888
  errno = 0;
661
889
  
719
947
                 PRIuMAX "\n", ip, (uintmax_t)port);
720
948
  }
721
949
  
722
 
  tcp_sd = socket(pf, SOCK_STREAM, 0);
 
950
  tcp_sd = socket(pf, SOCK_STREAM | SOCK_CLOEXEC, 0);
723
951
  if(tcp_sd < 0){
724
952
    int e = errno;
725
953
    perror_plus("socket");
814
1042
    goto mandos_end;
815
1043
  }
816
1044
  
817
 
  if(af == AF_INET6){
818
 
    ret = connect(tcp_sd, (struct sockaddr *)&to,
819
 
                  sizeof(struct sockaddr_in6));
820
 
  } else {
821
 
    ret = connect(tcp_sd, (struct sockaddr *)&to, /* IPv4 */
822
 
                  sizeof(struct sockaddr_in));
823
 
  }
824
 
  if(ret < 0){
825
 
    if((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
826
 
      int e = errno;
827
 
      perror_plus("connect");
828
 
      errno = e;
829
 
    }
830
 
    goto mandos_end;
831
 
  }
832
 
  
833
 
  if(quit_now){
834
 
    errno = EINTR;
835
 
    goto mandos_end;
 
1045
  while(true){
 
1046
    if(af == AF_INET6){
 
1047
      ret = connect(tcp_sd, (struct sockaddr *)&to,
 
1048
                    sizeof(struct sockaddr_in6));
 
1049
    } else {
 
1050
      ret = connect(tcp_sd, (struct sockaddr *)&to, /* IPv4 */
 
1051
                    sizeof(struct sockaddr_in));
 
1052
    }
 
1053
    if(ret < 0){
 
1054
      if(errno == ENETUNREACH
 
1055
         and if_index != AVAHI_IF_UNSPEC
 
1056
         and connect_to == NULL
 
1057
         and not route_added and
 
1058
         ((af == AF_INET6 and not
 
1059
           IN6_IS_ADDR_LINKLOCAL(&(((struct sockaddr_in6 *)
 
1060
                                    &to)->sin6_addr)))
 
1061
          or (af == AF_INET and
 
1062
              /* Not a a IPv4LL address */
 
1063
              (ntohl(((struct sockaddr_in *)&to)->sin_addr.s_addr)
 
1064
               & 0xFFFF0000L) != 0xA9FE0000L))){
 
1065
        /* Work around Avahi bug - Avahi does not announce link-local
 
1066
           addresses if it has a global address, so local hosts with
 
1067
           *only* a link-local address (e.g. Mandos clients) cannot
 
1068
           connect to a Mandos server announced by Avahi on a server
 
1069
           host with a global address.  Work around this by retrying
 
1070
           with an explicit route added with the server's address.
 
1071
           
 
1072
           Avahi bug reference:
 
1073
           http://lists.freedesktop.org/archives/avahi/2010-February/001833.html
 
1074
           https://bugs.debian.org/587961
 
1075
        */
 
1076
        if(debug){
 
1077
          fprintf_plus(stderr, "Mandos server unreachable, trying"
 
1078
                       " direct route\n");
 
1079
        }
 
1080
        int e = errno;
 
1081
        route_added = add_local_route(ip, if_index);
 
1082
        if(route_added){
 
1083
          continue;
 
1084
        }
 
1085
        errno = e;
 
1086
      }
 
1087
      if(errno != ECONNREFUSED or debug){
 
1088
        int e = errno;
 
1089
        perror_plus("connect");
 
1090
        errno = e;
 
1091
      }
 
1092
      goto mandos_end;
 
1093
    }
 
1094
    
 
1095
    if(quit_now){
 
1096
      errno = EINTR;
 
1097
      goto mandos_end;
 
1098
    }
 
1099
    break;
836
1100
  }
837
1101
  
838
1102
  const char *out = mandos_protocol_version;
1021
1285
  
1022
1286
 mandos_end:
1023
1287
  {
 
1288
    if(route_added){
 
1289
      if(not delete_local_route(ip, if_index)){
 
1290
        fprintf_plus(stderr, "Failed to delete local route to %s on"
 
1291
                     " interface %d", ip, if_index);
 
1292
      }
 
1293
    }
1024
1294
    int e = errno;
1025
1295
    free(decrypted_buffer);
1026
1296
    free(buffer);
1066
1336
     timed out */
1067
1337
  
1068
1338
  if(quit_now){
 
1339
    avahi_s_service_resolver_free(r);
1069
1340
    return;
1070
1341
  }
1071
1342
  
1451
1722
  }
1452
1723
}
1453
1724
 
1454
 
/* Set effective uid to 0, return errno */
1455
 
__attribute__((warn_unused_result))
1456
 
error_t raise_privileges(void){
1457
 
  error_t old_errno = errno;
1458
 
  error_t ret_errno = 0;
1459
 
  if(seteuid(0) == -1){
1460
 
    ret_errno = errno;
1461
 
    perror_plus("seteuid");
1462
 
  }
1463
 
  errno = old_errno;
1464
 
  return ret_errno;
1465
 
}
1466
 
 
1467
 
/* Set effective and real user ID to 0.  Return errno. */
1468
 
__attribute__((warn_unused_result))
1469
 
error_t raise_privileges_permanently(void){
1470
 
  error_t old_errno = errno;
1471
 
  error_t ret_errno = raise_privileges();
1472
 
  if(ret_errno != 0){
1473
 
    errno = old_errno;
1474
 
    return ret_errno;
1475
 
  }
1476
 
  if(setuid(0) == -1){
1477
 
    ret_errno = errno;
1478
 
    perror_plus("seteuid");
1479
 
  }
1480
 
  errno = old_errno;
1481
 
  return ret_errno;
1482
 
}
1483
 
 
1484
 
/* Set effective user ID to unprivileged saved user ID */
1485
 
__attribute__((warn_unused_result))
1486
 
error_t lower_privileges(void){
1487
 
  error_t old_errno = errno;
1488
 
  error_t ret_errno = 0;
1489
 
  if(seteuid(uid) == -1){
1490
 
    ret_errno = errno;
1491
 
    perror_plus("seteuid");
1492
 
  }
1493
 
  errno = old_errno;
1494
 
  return ret_errno;
1495
 
}
1496
 
 
1497
 
/* Lower privileges permanently */
1498
 
__attribute__((warn_unused_result))
1499
 
error_t lower_privileges_permanently(void){
1500
 
  error_t old_errno = errno;
1501
 
  error_t ret_errno = 0;
1502
 
  if(setuid(uid) == -1){
1503
 
    ret_errno = errno;
1504
 
    perror_plus("setuid");
1505
 
  }
1506
 
  errno = old_errno;
1507
 
  return ret_errno;
1508
 
}
1509
 
 
1510
1725
__attribute__((nonnull))
1511
1726
void run_network_hooks(const char *mode, const char *interface,
1512
1727
                       const float delay){
1513
1728
  struct dirent **direntries = NULL;
1514
1729
  if(hookdir_fd == -1){
1515
 
    hookdir_fd = open(hookdir, O_RDONLY);
 
1730
    hookdir_fd = open(hookdir, O_RDONLY | O_DIRECTORY | O_PATH
 
1731
                      | O_CLOEXEC);
1516
1732
    if(hookdir_fd == -1){
1517
1733
      if(errno == ENOENT){
1518
1734
        if(debug){
1543
1759
  }
1544
1760
  struct dirent *direntry;
1545
1761
  int ret;
1546
 
  int devnull = open("/dev/null", O_RDONLY);
 
1762
  int devnull = (int)TEMP_FAILURE_RETRY(open("/dev/null", O_RDONLY));
 
1763
  if(devnull == -1){
 
1764
    perror_plus("open(\"/dev/null\", O_RDONLY)");
 
1765
    return;
 
1766
  }
1547
1767
  for(int i = 0; i < numhooks; i++){
1548
1768
    direntry = direntries[i];
1549
1769
    if(debug){
1554
1774
    if(hook_pid == 0){
1555
1775
      /* Child */
1556
1776
      /* Raise privileges */
1557
 
      if(raise_privileges_permanently() != 0){
 
1777
      errno = raise_privileges_permanently();
 
1778
      if(errno != 0){
1558
1779
        perror_plus("Failed to raise privileges");
1559
1780
        _exit(EX_NOPERM);
1560
1781
      }
1572
1793
        perror_plus("setgroups");
1573
1794
        _exit(EX_NOPERM);
1574
1795
      }
1575
 
      ret = dup2(devnull, STDIN_FILENO);
1576
 
      if(ret == -1){
1577
 
        perror_plus("dup2(devnull, STDIN_FILENO)");
1578
 
        _exit(EX_OSERR);
1579
 
      }
1580
 
      ret = close(devnull);
1581
 
      if(ret == -1){
1582
 
        perror_plus("close");
1583
 
        _exit(EX_OSERR);
1584
 
      }
1585
 
      ret = dup2(STDERR_FILENO, STDOUT_FILENO);
1586
 
      if(ret == -1){
1587
 
        perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
1588
 
        _exit(EX_OSERR);
1589
 
      }
1590
1796
      ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1591
1797
      if(ret == -1){
1592
1798
        perror_plus("setenv");
1627
1833
          _exit(EX_OSERR);
1628
1834
        }
1629
1835
      }
1630
 
      int hook_fd = openat(hookdir_fd, direntry->d_name, O_RDONLY);
 
1836
      int hook_fd = (int)TEMP_FAILURE_RETRY(openat(hookdir_fd,
 
1837
                                                   direntry->d_name,
 
1838
                                                   O_RDONLY));
1631
1839
      if(hook_fd == -1){
1632
1840
        perror_plus("openat");
1633
1841
        _exit(EXIT_FAILURE);
1636
1844
        perror_plus("close");
1637
1845
        _exit(EXIT_FAILURE);
1638
1846
      }
 
1847
      ret = dup2(devnull, STDIN_FILENO);
 
1848
      if(ret == -1){
 
1849
        perror_plus("dup2(devnull, STDIN_FILENO)");
 
1850
        _exit(EX_OSERR);
 
1851
      }
 
1852
      ret = (int)TEMP_FAILURE_RETRY(close(devnull));
 
1853
      if(ret == -1){
 
1854
        perror_plus("close");
 
1855
        _exit(EX_OSERR);
 
1856
      }
 
1857
      ret = dup2(STDERR_FILENO, STDOUT_FILENO);
 
1858
      if(ret == -1){
 
1859
        perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
 
1860
        _exit(EX_OSERR);
 
1861
      }
1639
1862
      if(fexecve(hook_fd, (char *const []){ direntry->d_name, NULL },
1640
1863
                 environ) == -1){
1641
1864
        perror_plus("fexecve");
1642
1865
        _exit(EXIT_FAILURE);
1643
1866
      }
1644
1867
    } else {
 
1868
      if(hook_pid == -1){
 
1869
        perror_plus("fork");
 
1870
        free(direntry);
 
1871
        continue;
 
1872
      }
1645
1873
      int status;
1646
1874
      if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1647
1875
        perror_plus("waitpid");
 
1876
        free(direntry);
1648
1877
        continue;
1649
1878
      }
1650
1879
      if(WIFEXITED(status)){
1652
1881
          fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1653
1882
                       " with status %d\n", direntry->d_name,
1654
1883
                       WEXITSTATUS(status));
 
1884
          free(direntry);
1655
1885
          continue;
1656
1886
        }
1657
1887
      } else if(WIFSIGNALED(status)){
1658
1888
        fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1659
1889
                     " signal %d\n", direntry->d_name,
1660
1890
                     WTERMSIG(status));
 
1891
        free(direntry);
1661
1892
        continue;
1662
1893
      } else {
1663
1894
        fprintf_plus(stderr, "Warning: network hook \"%s\""
1664
1895
                     " crashed\n", direntry->d_name);
 
1896
        free(direntry);
1665
1897
        continue;
1666
1898
      }
1667
1899
    }
1669
1901
      fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1670
1902
                   direntry->d_name);
1671
1903
    }
 
1904
    free(direntry);
1672
1905
  }
1673
1906
  free(direntries);
1674
1907
  if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1733
1966
    /* Raise privileges */
1734
1967
    ret_errno = raise_privileges();
1735
1968
    if(ret_errno != 0){
 
1969
      errno = ret_errno;
1736
1970
      perror_plus("Failed to raise privileges");
1737
1971
    }
1738
1972
    
1842
2076
    /* Raise privileges */
1843
2077
    ret_errno = raise_privileges();
1844
2078
    if(ret_errno != 0){
 
2079
      errno = ret_errno;
1845
2080
      perror_plus("Failed to raise privileges");
1846
2081
    }
1847
2082
    
2077
2312
      goto end;
2078
2313
    }
2079
2314
  }
2080
 
    
 
2315
  
2081
2316
  {
2082
2317
    /* Work around Debian bug #633582:
2083
2318
       <http://bugs.debian.org/633582> */
2110
2345
          TEMP_FAILURE_RETRY(close(seckey_fd));
2111
2346
        }
2112
2347
      }
2113
 
    
 
2348
      
2114
2349
      if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
2115
2350
        int pubkey_fd = open(pubkey, O_RDONLY);
2116
2351
        if(pubkey_fd == -1){
2131
2366
          TEMP_FAILURE_RETRY(close(pubkey_fd));
2132
2367
        }
2133
2368
      }
2134
 
    
 
2369
      
2135
2370
      /* Lower privileges */
2136
2371
      ret_errno = lower_privileges();
2137
2372
      if(ret_errno != 0){
2266
2501
        if(ret_errno != 0){
2267
2502
          errno = ret_errno;
2268
2503
          perror_plus("argz_add");
 
2504
          free(direntries[i]);
2269
2505
          continue;
2270
2506
        }
2271
2507
        if(debug){
2272
2508
          fprintf_plus(stderr, "Will use interface \"%s\"\n",
2273
2509
                       direntries[i]->d_name);
2274
2510
        }
 
2511
        free(direntries[i]);
2275
2512
      }
2276
2513
      free(direntries);
2277
2514
    } else {
2547
2784
    mc.current_server->prev->next = NULL;
2548
2785
    while(mc.current_server != NULL){
2549
2786
      server *next = mc.current_server->next;
 
2787
#ifdef __GNUC__
 
2788
#pragma GCC diagnostic push
 
2789
#pragma GCC diagnostic ignored "-Wcast-qual"
 
2790
#endif
 
2791
      free((char *)(mc.current_server->ip));
 
2792
#ifdef __GNUC__
 
2793
#pragma GCC diagnostic pop
 
2794
#endif
2550
2795
      free(mc.current_server);
2551
2796
      mc.current_server = next;
2552
2797
    }
2556
2801
  {
2557
2802
    ret_errno = raise_privileges();
2558
2803
    if(ret_errno != 0){
 
2804
      errno = ret_errno;
2559
2805
      perror_plus("Failed to raise privileges");
2560
2806
    } else {
2561
2807
      
2584
2830
    
2585
2831
    ret_errno = lower_privileges_permanently();
2586
2832
    if(ret_errno != 0){
 
2833
      errno = ret_errno;
2587
2834
      perror_plus("Failed to lower privileges permanently");
2588
2835
    }
2589
2836
  }
2594
2841
  /* Removes the GPGME temp directory and all files inside */
2595
2842
  if(tempdir != NULL){
2596
2843
    struct dirent **direntries = NULL;
2597
 
    int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY |
2598
 
                                                  O_NOFOLLOW));
 
2844
    int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY
 
2845
                                                  | O_NOFOLLOW
 
2846
                                                  | O_DIRECTORY
 
2847
                                                  | O_PATH));
2599
2848
    if(tempdir_fd == -1){
2600
2849
      perror_plus("open");
2601
2850
    } else {
2619
2868
                         " \"%s\", 0): %s\n", tempdir,
2620
2869
                         direntries[i]->d_name, strerror(errno));
2621
2870
          }
 
2871
          free(direntries[i]);
2622
2872
        }
2623
2873
        
2624
2874
        /* need to clean even if 0 because man page doesn't specify */