/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-05 21:38:01 UTC
  • mto: This revision was merged to the branch mainline in revision 759.
  • Revision ID: teddy@recompile.se-20150705213801-pi8vk0ovlumuj826
plugin-helpers/mandos-client-iprouteadddel.c: Fix #include lines.
(main): Fix minor typo in error message.

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
32
32
/* Needed by GPGME, specifically gpgme_data_seek() */
33
33
#ifndef _LARGEFILE_SOURCE
34
34
#define _LARGEFILE_SOURCE
35
 
#endif
 
35
#endif  /* not _LARGEFILE_SOURCE */
36
36
#ifndef _FILE_OFFSET_BITS
37
37
#define _FILE_OFFSET_BITS 64
38
 
#endif
 
38
#endif  /* not _FILE_OFFSET_BITS */
39
39
 
40
40
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), asprintf() */
41
41
 
42
42
#include <stdio.h>              /* fprintf(), stderr, fwrite(),
43
 
                                   stdout, ferror(), remove() */
 
43
                                   stdout, ferror() */
44
44
#include <stdint.h>             /* uint16_t, uint32_t, intptr_t */
45
45
#include <stddef.h>             /* NULL, size_t, ssize_t */
46
46
#include <stdlib.h>             /* free(), EXIT_SUCCESS, srand(),
57
57
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
58
58
                                   inet_pton(), connect(),
59
59
                                   getnameinfo() */
60
 
#include <fcntl.h>              /* open() */
 
60
#include <fcntl.h>              /* open(), unlinkat() */
61
61
#include <dirent.h>             /* opendir(), struct dirent, readdir()
62
62
                                 */
63
63
#include <inttypes.h>           /* PRIu16, PRIdMAX, intmax_t,
73
73
                                */
74
74
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
75
75
                                   getuid(), getgid(), seteuid(),
76
 
                                   setgid(), pause(), _exit() */
 
76
                                   setgid(), pause(), _exit(),
 
77
                                   unlinkat() */
77
78
#include <arpa/inet.h>          /* inet_pton(), htons() */
78
79
#include <iso646.h>             /* not, or, and */
79
80
#include <argp.h>               /* struct argp_option, error_t, struct
141
142
static const char sys_class_net[] = "/sys/class/net";
142
143
char *connect_to = NULL;
143
144
const char *hookdir = HOOKDIR;
 
145
int hookdir_fd = -1;
144
146
uid_t uid = 65534;
145
147
gid_t gid = 65534;
146
148
 
232
234
                          .af = af };
233
235
  if(new_server->ip == NULL){
234
236
    perror_plus("strdup");
 
237
    free(new_server);
235
238
    return false;
236
239
  }
237
240
  ret = clock_gettime(CLOCK_MONOTONIC, &(new_server->last_seen));
238
241
  if(ret == -1){
239
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);
240
252
    return false;
241
253
  }
242
254
  /* Special case of first server */
638
650
static void empty_log(__attribute__((unused)) AvahiLogLevel level,
639
651
                      __attribute__((unused)) const char *txt){}
640
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 remove_local_route() */
 
706
__attribute__((nonnull, warn_unused_result))
 
707
static bool add_remove_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 *pluginhelperdir = getenv("MANDOSPLUGINHELPERDIR");
 
715
  if(pluginhelperdir == NULL){
 
716
    if(debug){
 
717
      fprintf_plus(stderr, "MANDOSPLUGINHELPERDIR environment"
 
718
                   " variable not set; cannot run helper\n");
 
719
    }
 
720
    return false;
 
721
  }
 
722
  
 
723
  char interface[IF_NAMESIZE];
 
724
  if(if_indextoname((unsigned int)if_index, interface) == NULL){
 
725
    perror_plus("if_indextoname");
 
726
    return false;
 
727
  }
 
728
  
 
729
  int devnull = (int)TEMP_FAILURE_RETRY(open("/dev/null", O_RDONLY));
 
730
  if(devnull == -1){
 
731
    perror_plus("open(\"/dev/null\", O_RDONLY)");
 
732
    return false;
 
733
  }
 
734
  pid_t pid = fork();
 
735
  if(pid == 0){
 
736
    /* Child */
 
737
    /* Raise privileges */
 
738
    errno = raise_privileges_permanently();
 
739
    if(errno != 0){
 
740
      perror_plus("Failed to raise privileges");
 
741
      /* _exit(EX_NOPERM); */
 
742
    } else {
 
743
      /* Set group */
 
744
      errno = 0;
 
745
      ret = setgid(0);
 
746
      if(ret == -1){
 
747
        perror_plus("setgid");
 
748
        _exit(EX_NOPERM);
 
749
      }
 
750
      /* Reset supplementary groups */
 
751
      errno = 0;
 
752
      ret = setgroups(0, NULL);
 
753
      if(ret == -1){
 
754
        perror_plus("setgroups");
 
755
        _exit(EX_NOPERM);
 
756
      }
 
757
    }
 
758
    ret = dup2(devnull, STDIN_FILENO);
 
759
    if(ret == -1){
 
760
      perror_plus("dup2(devnull, STDIN_FILENO)");
 
761
      _exit(EX_OSERR);
 
762
    }
 
763
    ret = (int)TEMP_FAILURE_RETRY(close(devnull));
 
764
    if(ret == -1){
 
765
      perror_plus("close");
 
766
      _exit(EX_OSERR);
 
767
    }
 
768
    ret = dup2(STDERR_FILENO, STDOUT_FILENO);
 
769
    if(ret == -1){
 
770
      perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
 
771
      _exit(EX_OSERR);
 
772
    }
 
773
    int helperdir_fd = (int)TEMP_FAILURE_RETRY(open(pluginhelperdir,
 
774
                                                    O_RDONLY
 
775
                                                    | O_DIRECTORY
 
776
                                                    | O_PATH
 
777
                                                    | O_CLOEXEC));
 
778
    if(helperdir_fd == -1){
 
779
      perror_plus("open");
 
780
      _exit(EX_UNAVAILABLE);
 
781
    }
 
782
    int helper_fd = (int)TEMP_FAILURE_RETRY(openat(helperdir_fd,
 
783
                                                   helper, O_RDONLY));
 
784
    if(helper_fd == -1){
 
785
      perror_plus("openat");
 
786
      _exit(EX_UNAVAILABLE);
 
787
    }
 
788
    TEMP_FAILURE_RETRY(close(helperdir_fd));
 
789
#ifdef __GNUC__
 
790
#pragma GCC diagnostic push
 
791
#pragma GCC diagnostic ignored "-Wcast-qual"
 
792
#endif
 
793
    if(fexecve(helper_fd, (char *const [])
 
794
               { helper, add ? add_arg : delete_arg, (char *)address,
 
795
                   interface, NULL }, environ) == -1){
 
796
#ifdef __GNUC__
 
797
#pragma GCC diagnostic pop
 
798
#endif
 
799
      perror_plus("fexecve");
 
800
      _exit(EXIT_FAILURE);
 
801
    }
 
802
  }
 
803
  if(pid == -1){
 
804
    perror_plus("fork");
 
805
    return false;
 
806
  }
 
807
  int status;
 
808
  pid_t pret = -1;
 
809
  errno = 0;
 
810
  do {
 
811
    pret = waitpid(pid, &status, 0);
 
812
    if(pret == -1 and errno == EINTR and quit_now){
 
813
      int errno_raising = 0;
 
814
      if((errno = raise_privileges()) != 0){
 
815
        errno_raising = errno;
 
816
        perror_plus("Failed to raise privileges in order to"
 
817
                    " kill helper program");
 
818
      }
 
819
      if(kill(pid, SIGTERM) == -1){
 
820
        perror_plus("kill");
 
821
      }
 
822
      if((errno_raising == 0) and (errno = lower_privileges()) != 0){
 
823
        perror_plus("Failed to lower privileges after killing"
 
824
                    " helper program");
 
825
      }
 
826
      return false;
 
827
    }
 
828
  } while(pret == -1 and errno == EINTR);
 
829
  if(pret == -1){
 
830
    perror_plus("waitpid");
 
831
    return false;
 
832
  }
 
833
  if(WIFEXITED(status)){
 
834
    if(WEXITSTATUS(status) != 0){
 
835
      fprintf_plus(stderr, "Error: iprouteadddel exited"
 
836
                   " with status %d\n", WEXITSTATUS(status));
 
837
      return false;
 
838
    }
 
839
    return true;
 
840
  }
 
841
  if(WIFSIGNALED(status)){
 
842
    fprintf_plus(stderr, "Error: iprouteadddel died by"
 
843
                 " signal %d\n", WTERMSIG(status));
 
844
    return false;
 
845
  }
 
846
  fprintf_plus(stderr, "Error: iprouteadddel crashed\n");
 
847
  return false;
 
848
}
 
849
 
 
850
__attribute__((nonnull, warn_unused_result))
 
851
static bool add_local_route(const char *address,
 
852
                            AvahiIfIndex if_index){
 
853
  return add_remove_local_route(true, address, if_index);
 
854
}
 
855
 
 
856
__attribute__((nonnull, warn_unused_result))
 
857
static bool remove_local_route(const char *address,
 
858
                               AvahiIfIndex if_index){
 
859
  return add_remove_local_route(false, address, if_index);
 
860
}
 
861
 
641
862
/* Called when a Mandos server is found */
642
863
__attribute__((nonnull, warn_unused_result))
643
864
static int start_mandos_communication(const char *ip, in_port_t port,
654
875
  int retval = -1;
655
876
  gnutls_session_t session;
656
877
  int pf;                       /* Protocol family */
 
878
  bool route_added = false;
657
879
  
658
880
  errno = 0;
659
881
  
717
939
                 PRIuMAX "\n", ip, (uintmax_t)port);
718
940
  }
719
941
  
720
 
  tcp_sd = socket(pf, SOCK_STREAM, 0);
 
942
  tcp_sd = socket(pf, SOCK_STREAM | SOCK_CLOEXEC, 0);
721
943
  if(tcp_sd < 0){
722
944
    int e = errno;
723
945
    perror_plus("socket");
812
1034
    goto mandos_end;
813
1035
  }
814
1036
  
815
 
  if(af == AF_INET6){
816
 
    ret = connect(tcp_sd, (struct sockaddr *)&to,
817
 
                  sizeof(struct sockaddr_in6));
818
 
  } else {
819
 
    ret = connect(tcp_sd, (struct sockaddr *)&to, /* IPv4 */
820
 
                  sizeof(struct sockaddr_in));
821
 
  }
822
 
  if(ret < 0){
823
 
    if((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
824
 
      int e = errno;
825
 
      perror_plus("connect");
826
 
      errno = e;
827
 
    }
828
 
    goto mandos_end;
829
 
  }
830
 
  
831
 
  if(quit_now){
832
 
    errno = EINTR;
833
 
    goto mandos_end;
 
1037
  while(true){
 
1038
    if(af == AF_INET6){
 
1039
      ret = connect(tcp_sd, (struct sockaddr *)&to,
 
1040
                    sizeof(struct sockaddr_in6));
 
1041
    } else {
 
1042
      ret = connect(tcp_sd, (struct sockaddr *)&to, /* IPv4 */
 
1043
                    sizeof(struct sockaddr_in));
 
1044
    }
 
1045
    if(ret < 0){
 
1046
      if(errno == ENETUNREACH
 
1047
         and if_index != AVAHI_IF_UNSPEC
 
1048
         and connect_to == NULL
 
1049
         and not route_added and
 
1050
         ((af == AF_INET6 and not
 
1051
           IN6_IS_ADDR_LINKLOCAL(&(((struct sockaddr_in6 *)
 
1052
                                    &to)->sin6_addr)))
 
1053
          or (af == AF_INET and
 
1054
              /* Not a a IPv4LL address */
 
1055
              (ntohl(((struct sockaddr_in *)&to)->sin_addr.s_addr)
 
1056
               & 0xFFFF0000L) != 0xA9FE0000L))){
 
1057
        /* Work around Avahi bug - Avahi does not announce link-local
 
1058
           addresses if it has a global address, so local hosts with
 
1059
           *only* a link-local address (e.g. Mandos clients) cannot
 
1060
           connect to a Mandos server announced by Avahi on a server
 
1061
           host with a global address.  Work around this by retrying
 
1062
           with an explicit route added with the server's address.
 
1063
           
 
1064
           Avahi bug reference:
 
1065
           http://lists.freedesktop.org/archives/avahi/2010-February/001833.html
 
1066
           https://bugs.debian.org/587961
 
1067
        */
 
1068
        int e = errno;
 
1069
        route_added = add_local_route(ip, if_index);
 
1070
        if(route_added){
 
1071
          continue;
 
1072
        }
 
1073
        errno = e;
 
1074
      }
 
1075
      if(errno != ECONNREFUSED or debug){
 
1076
        int e = errno;
 
1077
        perror_plus("connect");
 
1078
        errno = e;
 
1079
      }
 
1080
      goto mandos_end;
 
1081
    }
 
1082
    
 
1083
    if(quit_now){
 
1084
      errno = EINTR;
 
1085
      goto mandos_end;
 
1086
    }
 
1087
    break;
834
1088
  }
835
1089
  
836
1090
  const char *out = mandos_protocol_version;
1019
1273
  
1020
1274
 mandos_end:
1021
1275
  {
 
1276
    if(route_added){
 
1277
      if(not remove_local_route(ip, if_index)){
 
1278
        fprintf_plus(stderr, "Failed to remove local route to %s on"
 
1279
                     " interface %d", ip, if_index);
 
1280
      }
 
1281
    }
1022
1282
    int e = errno;
1023
1283
    free(decrypted_buffer);
1024
1284
    free(buffer);
1064
1324
     timed out */
1065
1325
  
1066
1326
  if(quit_now){
 
1327
    avahi_s_service_resolver_free(r);
1067
1328
    return;
1068
1329
  }
1069
1330
  
1345
1606
    return 0;
1346
1607
  }
1347
1608
  
1348
 
  char *fullname = NULL;
1349
 
  ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1350
 
  if(ret < 0){
1351
 
    perror_plus("asprintf");
1352
 
    return 0;
1353
 
  }
1354
 
  
1355
 
  ret = stat(fullname, &st);
 
1609
  ret = fstatat(hookdir_fd, direntry->d_name, &st, 0);
1356
1610
  if(ret == -1){
1357
1611
    if(debug){
1358
1612
      perror_plus("Could not stat hook");
1359
1613
    }
1360
1614
    return 0;
1361
1615
  }
1362
 
  free(fullname);
1363
1616
  if(not (S_ISREG(st.st_mode))){
1364
1617
    /* Not a regular file */
1365
1618
    if(debug){
1457
1710
  }
1458
1711
}
1459
1712
 
1460
 
/* Set effective uid to 0, return errno */
1461
 
__attribute__((warn_unused_result))
1462
 
error_t raise_privileges(void){
1463
 
  error_t old_errno = errno;
1464
 
  error_t ret_errno = 0;
1465
 
  if(seteuid(0) == -1){
1466
 
    ret_errno = errno;
1467
 
    perror_plus("seteuid");
1468
 
  }
1469
 
  errno = old_errno;
1470
 
  return ret_errno;
1471
 
}
1472
 
 
1473
 
/* Set effective and real user ID to 0.  Return errno. */
1474
 
__attribute__((warn_unused_result))
1475
 
error_t raise_privileges_permanently(void){
1476
 
  error_t old_errno = errno;
1477
 
  error_t ret_errno = raise_privileges();
1478
 
  if(ret_errno != 0){
1479
 
    errno = old_errno;
1480
 
    return ret_errno;
1481
 
  }
1482
 
  if(setuid(0) == -1){
1483
 
    ret_errno = errno;
1484
 
    perror_plus("seteuid");
1485
 
  }
1486
 
  errno = old_errno;
1487
 
  return ret_errno;
1488
 
}
1489
 
 
1490
 
/* Set effective user ID to unprivileged saved user ID */
1491
 
__attribute__((warn_unused_result))
1492
 
error_t lower_privileges(void){
1493
 
  error_t old_errno = errno;
1494
 
  error_t ret_errno = 0;
1495
 
  if(seteuid(uid) == -1){
1496
 
    ret_errno = errno;
1497
 
    perror_plus("seteuid");
1498
 
  }
1499
 
  errno = old_errno;
1500
 
  return ret_errno;
1501
 
}
1502
 
 
1503
 
/* Lower privileges permanently */
1504
 
__attribute__((warn_unused_result))
1505
 
error_t lower_privileges_permanently(void){
1506
 
  error_t old_errno = errno;
1507
 
  error_t ret_errno = 0;
1508
 
  if(setuid(uid) == -1){
1509
 
    ret_errno = errno;
1510
 
    perror_plus("setuid");
1511
 
  }
1512
 
  errno = old_errno;
1513
 
  return ret_errno;
1514
 
}
1515
 
 
1516
1713
__attribute__((nonnull))
1517
1714
void run_network_hooks(const char *mode, const char *interface,
1518
1715
                       const float delay){
1519
 
  struct dirent **direntries;
1520
 
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1521
 
                         alphasort);
 
1716
  struct dirent **direntries = NULL;
 
1717
  if(hookdir_fd == -1){
 
1718
    hookdir_fd = open(hookdir, O_RDONLY | O_DIRECTORY | O_PATH
 
1719
                      | O_CLOEXEC);
 
1720
    if(hookdir_fd == -1){
 
1721
      if(errno == ENOENT){
 
1722
        if(debug){
 
1723
          fprintf_plus(stderr, "Network hook directory \"%s\" not"
 
1724
                       " found\n", hookdir);
 
1725
        }
 
1726
      } else {
 
1727
        perror_plus("open");
 
1728
      }
 
1729
      return;
 
1730
    }
 
1731
  }
 
1732
#ifdef __GLIBC__
 
1733
#if __GLIBC_PREREQ(2, 15)
 
1734
  int numhooks = scandirat(hookdir_fd, ".", &direntries,
 
1735
                           runnable_hook, alphasort);
 
1736
#else  /* not __GLIBC_PREREQ(2, 15) */
 
1737
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
1738
                         alphasort);
 
1739
#endif  /* not __GLIBC_PREREQ(2, 15) */
 
1740
#else   /* not __GLIBC__ */
 
1741
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
1742
                         alphasort);
 
1743
#endif  /* not __GLIBC__ */
1522
1744
  if(numhooks == -1){
1523
 
    if(errno == ENOENT){
1524
 
      if(debug){
1525
 
        fprintf_plus(stderr, "Network hook directory \"%s\" not"
1526
 
                     " found\n", hookdir);
1527
 
      }
1528
 
    } else {
1529
 
      perror_plus("scandir");
 
1745
    perror_plus("scandir");
 
1746
    return;
 
1747
  }
 
1748
  struct dirent *direntry;
 
1749
  int ret;
 
1750
  int devnull = (int)TEMP_FAILURE_RETRY(open("/dev/null", O_RDONLY));
 
1751
  if(devnull == -1){
 
1752
    perror_plus("open(\"/dev/null\", O_RDONLY)");
 
1753
    return;
 
1754
  }
 
1755
  for(int i = 0; i < numhooks; i++){
 
1756
    direntry = direntries[i];
 
1757
    if(debug){
 
1758
      fprintf_plus(stderr, "Running network hook \"%s\"\n",
 
1759
                   direntry->d_name);
1530
1760
    }
1531
 
  } else {
1532
 
    struct dirent *direntry;
1533
 
    int ret;
1534
 
    int devnull = open("/dev/null", O_RDONLY);
1535
 
    for(int i = 0; i < numhooks; i++){
1536
 
      direntry = direntries[i];
1537
 
      char *fullname = NULL;
1538
 
      ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1539
 
      if(ret < 0){
 
1761
    pid_t hook_pid = fork();
 
1762
    if(hook_pid == 0){
 
1763
      /* Child */
 
1764
      /* Raise privileges */
 
1765
      errno = raise_privileges_permanently();
 
1766
      if(errno != 0){
 
1767
        perror_plus("Failed to raise privileges");
 
1768
        _exit(EX_NOPERM);
 
1769
      }
 
1770
      /* Set group */
 
1771
      errno = 0;
 
1772
      ret = setgid(0);
 
1773
      if(ret == -1){
 
1774
        perror_plus("setgid");
 
1775
        _exit(EX_NOPERM);
 
1776
      }
 
1777
      /* Reset supplementary groups */
 
1778
      errno = 0;
 
1779
      ret = setgroups(0, NULL);
 
1780
      if(ret == -1){
 
1781
        perror_plus("setgroups");
 
1782
        _exit(EX_NOPERM);
 
1783
      }
 
1784
      ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
 
1785
      if(ret == -1){
 
1786
        perror_plus("setenv");
 
1787
        _exit(EX_OSERR);
 
1788
      }
 
1789
      ret = setenv("DEVICE", interface, 1);
 
1790
      if(ret == -1){
 
1791
        perror_plus("setenv");
 
1792
        _exit(EX_OSERR);
 
1793
      }
 
1794
      ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
 
1795
      if(ret == -1){
 
1796
        perror_plus("setenv");
 
1797
        _exit(EX_OSERR);
 
1798
      }
 
1799
      ret = setenv("MODE", mode, 1);
 
1800
      if(ret == -1){
 
1801
        perror_plus("setenv");
 
1802
        _exit(EX_OSERR);
 
1803
      }
 
1804
      char *delaystring;
 
1805
      ret = asprintf(&delaystring, "%f", (double)delay);
 
1806
      if(ret == -1){
1540
1807
        perror_plus("asprintf");
1541
 
        continue;
1542
 
      }
1543
 
      if(debug){
1544
 
        fprintf_plus(stderr, "Running network hook \"%s\"\n",
1545
 
                     direntry->d_name);
1546
 
      }
1547
 
      pid_t hook_pid = fork();
1548
 
      if(hook_pid == 0){
1549
 
        /* Child */
1550
 
        /* Raise privileges */
1551
 
        if(raise_privileges_permanently() != 0){
1552
 
          perror_plus("Failed to raise privileges");
1553
 
          _exit(EX_NOPERM);
1554
 
        }
1555
 
        /* Set group */
1556
 
        errno = 0;
1557
 
        ret = setgid(0);
1558
 
        if(ret == -1){
1559
 
          perror_plus("setgid");
1560
 
          _exit(EX_NOPERM);
1561
 
        }
1562
 
        /* Reset supplementary groups */
1563
 
        errno = 0;
1564
 
        ret = setgroups(0, NULL);
1565
 
        if(ret == -1){
1566
 
          perror_plus("setgroups");
1567
 
          _exit(EX_NOPERM);
1568
 
        }
1569
 
        ret = dup2(devnull, STDIN_FILENO);
1570
 
        if(ret == -1){
1571
 
          perror_plus("dup2(devnull, STDIN_FILENO)");
1572
 
          _exit(EX_OSERR);
1573
 
        }
1574
 
        ret = close(devnull);
1575
 
        if(ret == -1){
1576
 
          perror_plus("close");
1577
 
          _exit(EX_OSERR);
1578
 
        }
1579
 
        ret = dup2(STDERR_FILENO, STDOUT_FILENO);
1580
 
        if(ret == -1){
1581
 
          perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
1582
 
          _exit(EX_OSERR);
1583
 
        }
1584
 
        ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1585
 
        if(ret == -1){
1586
 
          perror_plus("setenv");
1587
 
          _exit(EX_OSERR);
1588
 
        }
1589
 
        ret = setenv("DEVICE", interface, 1);
1590
 
        if(ret == -1){
1591
 
          perror_plus("setenv");
1592
 
          _exit(EX_OSERR);
1593
 
        }
1594
 
        ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1595
 
        if(ret == -1){
1596
 
          perror_plus("setenv");
1597
 
          _exit(EX_OSERR);
1598
 
        }
1599
 
        ret = setenv("MODE", mode, 1);
1600
 
        if(ret == -1){
1601
 
          perror_plus("setenv");
1602
 
          _exit(EX_OSERR);
1603
 
        }
1604
 
        char *delaystring;
1605
 
        ret = asprintf(&delaystring, "%f", (double)delay);
1606
 
        if(ret == -1){
1607
 
          perror_plus("asprintf");
1608
 
          _exit(EX_OSERR);
1609
 
        }
1610
 
        ret = setenv("DELAY", delaystring, 1);
1611
 
        if(ret == -1){
1612
 
          free(delaystring);
1613
 
          perror_plus("setenv");
1614
 
          _exit(EX_OSERR);
1615
 
        }
 
1808
        _exit(EX_OSERR);
 
1809
      }
 
1810
      ret = setenv("DELAY", delaystring, 1);
 
1811
      if(ret == -1){
1616
1812
        free(delaystring);
1617
 
        if(connect_to != NULL){
1618
 
          ret = setenv("CONNECT", connect_to, 1);
1619
 
          if(ret == -1){
1620
 
            perror_plus("setenv");
1621
 
            _exit(EX_OSERR);
1622
 
          }
1623
 
        }
1624
 
        if(execl(fullname, direntry->d_name, mode, NULL) == -1){
1625
 
          perror_plus("execl");
1626
 
          _exit(EXIT_FAILURE);
1627
 
        }
 
1813
        perror_plus("setenv");
 
1814
        _exit(EX_OSERR);
 
1815
      }
 
1816
      free(delaystring);
 
1817
      if(connect_to != NULL){
 
1818
        ret = setenv("CONNECT", connect_to, 1);
 
1819
        if(ret == -1){
 
1820
          perror_plus("setenv");
 
1821
          _exit(EX_OSERR);
 
1822
        }
 
1823
      }
 
1824
      int hook_fd = (int)TEMP_FAILURE_RETRY(openat(hookdir_fd,
 
1825
                                                   direntry->d_name,
 
1826
                                                   O_RDONLY));
 
1827
      if(hook_fd == -1){
 
1828
        perror_plus("openat");
 
1829
        _exit(EXIT_FAILURE);
 
1830
      }
 
1831
      if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
 
1832
        perror_plus("close");
 
1833
        _exit(EXIT_FAILURE);
 
1834
      }
 
1835
      ret = dup2(devnull, STDIN_FILENO);
 
1836
      if(ret == -1){
 
1837
        perror_plus("dup2(devnull, STDIN_FILENO)");
 
1838
        _exit(EX_OSERR);
 
1839
      }
 
1840
      ret = (int)TEMP_FAILURE_RETRY(close(devnull));
 
1841
      if(ret == -1){
 
1842
        perror_plus("close");
 
1843
        _exit(EX_OSERR);
 
1844
      }
 
1845
      ret = dup2(STDERR_FILENO, STDOUT_FILENO);
 
1846
      if(ret == -1){
 
1847
        perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
 
1848
        _exit(EX_OSERR);
 
1849
      }
 
1850
      if(fexecve(hook_fd, (char *const []){ direntry->d_name, NULL },
 
1851
                 environ) == -1){
 
1852
        perror_plus("fexecve");
 
1853
        _exit(EXIT_FAILURE);
 
1854
      }
 
1855
    } else {
 
1856
      if(hook_pid == -1){
 
1857
        perror_plus("fork");
 
1858
        free(direntry);
 
1859
        continue;
 
1860
      }
 
1861
      int status;
 
1862
      if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
 
1863
        perror_plus("waitpid");
 
1864
        free(direntry);
 
1865
        continue;
 
1866
      }
 
1867
      if(WIFEXITED(status)){
 
1868
        if(WEXITSTATUS(status) != 0){
 
1869
          fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
 
1870
                       " with status %d\n", direntry->d_name,
 
1871
                       WEXITSTATUS(status));
 
1872
          free(direntry);
 
1873
          continue;
 
1874
        }
 
1875
      } else if(WIFSIGNALED(status)){
 
1876
        fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
 
1877
                     " signal %d\n", direntry->d_name,
 
1878
                     WTERMSIG(status));
 
1879
        free(direntry);
 
1880
        continue;
1628
1881
      } else {
1629
 
        int status;
1630
 
        if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1631
 
          perror_plus("waitpid");
1632
 
          free(fullname);
1633
 
          continue;
1634
 
        }
1635
 
        if(WIFEXITED(status)){
1636
 
          if(WEXITSTATUS(status) != 0){
1637
 
            fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1638
 
                         " with status %d\n", direntry->d_name,
1639
 
                         WEXITSTATUS(status));
1640
 
            free(fullname);
1641
 
            continue;
1642
 
          }
1643
 
        } else if(WIFSIGNALED(status)){
1644
 
          fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1645
 
                       " signal %d\n", direntry->d_name,
1646
 
                       WTERMSIG(status));
1647
 
          free(fullname);
1648
 
          continue;
1649
 
        } else {
1650
 
          fprintf_plus(stderr, "Warning: network hook \"%s\""
1651
 
                       " crashed\n", direntry->d_name);
1652
 
          free(fullname);
1653
 
          continue;
1654
 
        }
1655
 
      }
1656
 
      free(fullname);
1657
 
      if(debug){
1658
 
        fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1659
 
                     direntry->d_name);
1660
 
      }
1661
 
    }
1662
 
    close(devnull);
1663
 
  }
 
1882
        fprintf_plus(stderr, "Warning: network hook \"%s\""
 
1883
                     " crashed\n", direntry->d_name);
 
1884
        free(direntry);
 
1885
        continue;
 
1886
      }
 
1887
    }
 
1888
    if(debug){
 
1889
      fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
 
1890
                   direntry->d_name);
 
1891
    }
 
1892
    free(direntry);
 
1893
  }
 
1894
  free(direntries);
 
1895
  if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
 
1896
    perror_plus("close");
 
1897
  } else {
 
1898
    hookdir_fd = -1;
 
1899
  }
 
1900
  close(devnull);
1664
1901
}
1665
1902
 
1666
1903
__attribute__((nonnull, warn_unused_result))
1717
1954
    /* Raise privileges */
1718
1955
    ret_errno = raise_privileges();
1719
1956
    if(ret_errno != 0){
 
1957
      errno = ret_errno;
1720
1958
      perror_plus("Failed to raise privileges");
1721
1959
    }
1722
1960
    
1826
2064
    /* Raise privileges */
1827
2065
    ret_errno = raise_privileges();
1828
2066
    if(ret_errno != 0){
 
2067
      errno = ret_errno;
1829
2068
      perror_plus("Failed to raise privileges");
1830
2069
    }
1831
2070
    
2061
2300
      goto end;
2062
2301
    }
2063
2302
  }
2064
 
    
 
2303
  
2065
2304
  {
2066
2305
    /* Work around Debian bug #633582:
2067
2306
       <http://bugs.debian.org/633582> */
2094
2333
          TEMP_FAILURE_RETRY(close(seckey_fd));
2095
2334
        }
2096
2335
      }
2097
 
    
 
2336
      
2098
2337
      if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
2099
2338
        int pubkey_fd = open(pubkey, O_RDONLY);
2100
2339
        if(pubkey_fd == -1){
2115
2354
          TEMP_FAILURE_RETRY(close(pubkey_fd));
2116
2355
        }
2117
2356
      }
2118
 
    
 
2357
      
2119
2358
      /* Lower privileges */
2120
2359
      ret_errno = lower_privileges();
2121
2360
      if(ret_errno != 0){
2238
2477
  
2239
2478
  /* If no interfaces were specified, make a list */
2240
2479
  if(mc.interfaces == NULL){
2241
 
    struct dirent **direntries;
 
2480
    struct dirent **direntries = NULL;
2242
2481
    /* Look for any good interfaces */
2243
2482
    ret = scandir(sys_class_net, &direntries, good_interface,
2244
2483
                  alphasort);
2250
2489
        if(ret_errno != 0){
2251
2490
          errno = ret_errno;
2252
2491
          perror_plus("argz_add");
 
2492
          free(direntries[i]);
2253
2493
          continue;
2254
2494
        }
2255
2495
        if(debug){
2256
2496
          fprintf_plus(stderr, "Will use interface \"%s\"\n",
2257
2497
                       direntries[i]->d_name);
2258
2498
        }
 
2499
        free(direntries[i]);
2259
2500
      }
2260
2501
      free(direntries);
2261
2502
    } else {
2262
 
      free(direntries);
 
2503
      if(ret == 0){
 
2504
        free(direntries);
 
2505
      }
2263
2506
      fprintf_plus(stderr, "Could not find a network interface\n");
2264
2507
      exitcode = EXIT_FAILURE;
2265
2508
      goto end;
2529
2772
    mc.current_server->prev->next = NULL;
2530
2773
    while(mc.current_server != NULL){
2531
2774
      server *next = mc.current_server->next;
 
2775
#ifdef __GNUC__
 
2776
#pragma GCC diagnostic push
 
2777
#pragma GCC diagnostic ignored "-Wcast-qual"
 
2778
#endif
 
2779
      free((char *)(mc.current_server->ip));
 
2780
#ifdef __GNUC__
 
2781
#pragma GCC diagnostic pop
 
2782
#endif
2532
2783
      free(mc.current_server);
2533
2784
      mc.current_server = next;
2534
2785
    }
2538
2789
  {
2539
2790
    ret_errno = raise_privileges();
2540
2791
    if(ret_errno != 0){
 
2792
      errno = ret_errno;
2541
2793
      perror_plus("Failed to raise privileges");
2542
2794
    } else {
2543
2795
      
2566
2818
    
2567
2819
    ret_errno = lower_privileges_permanently();
2568
2820
    if(ret_errno != 0){
 
2821
      errno = ret_errno;
2569
2822
      perror_plus("Failed to lower privileges permanently");
2570
2823
    }
2571
2824
  }
2576
2829
  /* Removes the GPGME temp directory and all files inside */
2577
2830
  if(tempdir != NULL){
2578
2831
    struct dirent **direntries = NULL;
2579
 
    struct dirent *direntry = NULL;
2580
 
    int numentries = scandir(tempdir, &direntries, notdotentries,
2581
 
                             alphasort);
2582
 
    if(numentries > 0){
2583
 
      for(int i = 0; i < numentries; i++){
2584
 
        direntry = direntries[i];
2585
 
        char *fullname = NULL;
2586
 
        ret = asprintf(&fullname, "%s/%s", tempdir,
2587
 
                       direntry->d_name);
2588
 
        if(ret < 0){
2589
 
          perror_plus("asprintf");
2590
 
          continue;
2591
 
        }
2592
 
        ret = remove(fullname);
2593
 
        if(ret == -1){
2594
 
          fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
2595
 
                       strerror(errno));
2596
 
        }
2597
 
        free(fullname);
 
2832
    int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY
 
2833
                                                  | O_NOFOLLOW
 
2834
                                                  | O_DIRECTORY
 
2835
                                                  | O_PATH));
 
2836
    if(tempdir_fd == -1){
 
2837
      perror_plus("open");
 
2838
    } else {
 
2839
#ifdef __GLIBC__
 
2840
#if __GLIBC_PREREQ(2, 15)
 
2841
      int numentries = scandirat(tempdir_fd, ".", &direntries,
 
2842
                                 notdotentries, alphasort);
 
2843
#else  /* not __GLIBC_PREREQ(2, 15) */
 
2844
      int numentries = scandir(tempdir, &direntries, notdotentries,
 
2845
                               alphasort);
 
2846
#endif  /* not __GLIBC_PREREQ(2, 15) */
 
2847
#else   /* not __GLIBC__ */
 
2848
      int numentries = scandir(tempdir, &direntries, notdotentries,
 
2849
                               alphasort);
 
2850
#endif  /* not __GLIBC__ */
 
2851
      if(numentries >= 0){
 
2852
        for(int i = 0; i < numentries; i++){
 
2853
          ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
 
2854
          if(ret == -1){
 
2855
            fprintf_plus(stderr, "unlinkat(open(\"%s\", O_RDONLY),"
 
2856
                         " \"%s\", 0): %s\n", tempdir,
 
2857
                         direntries[i]->d_name, strerror(errno));
 
2858
          }
 
2859
          free(direntries[i]);
 
2860
        }
 
2861
        
 
2862
        /* need to clean even if 0 because man page doesn't specify */
 
2863
        free(direntries);
 
2864
        if(numentries == -1){
 
2865
          perror_plus("scandir");
 
2866
        }
 
2867
        ret = rmdir(tempdir);
 
2868
        if(ret == -1 and errno != ENOENT){
 
2869
          perror_plus("rmdir");
 
2870
        }
2598
2871
      }
2599
 
    }
2600
 
    
2601
 
    /* need to clean even if 0 because man page doesn't specify */
2602
 
    free(direntries);
2603
 
    if(numentries == -1){
2604
 
      perror_plus("scandir");
2605
 
    }
2606
 
    ret = rmdir(tempdir);
2607
 
    if(ret == -1 and errno != ENOENT){
2608
 
      perror_plus("rmdir");
 
2872
      TEMP_FAILURE_RETRY(close(tempdir_fd));
2609
2873
    }
2610
2874
  }
2611
2875