/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: 2012-05-26 22:21:17 UTC
  • mto: This revision was merged to the branch mainline in revision 591.
  • Revision ID: teddy@recompile.se-20120526222117-2n4oeb3hqyq4rjdh
* mandos: Implement "--socket" option.
  (IPv6_TCPServer.__init__): Take new "socketfd" parameter; use it.
  (MandosServer.__init__): Take new "socketfd" parameter.  Pass it on
                           to IPv6_TCPServer constructor.
  (main): Take new "--socket" option.  Also take "socket" parameter in
          "mandos.conf" configuration file.  If set, pass the value to
          MandosServer constructor.
* mandos-options.xml (socket): Document new "socket" option.
* mandos-conf.xml (OPTIONS): - '' -
* mandos.xml (SYNOPSIS, OPTIONS): Document new "--socket" option.

Show diffs side-by-side

added added

removed removed

Lines of Context:
61
61
                                 */
62
62
#include <inttypes.h>           /* PRIu16, PRIdMAX, intmax_t,
63
63
                                   strtoimax() */
 
64
#include <assert.h>             /* assert() */
64
65
#include <errno.h>              /* perror(), errno,
65
66
                                   program_invocation_short_name */
66
67
#include <time.h>               /* nanosleep(), time(), sleep() */
87
88
#include <sys/wait.h>           /* waitpid(), WIFEXITED(),
88
89
                                   WEXITSTATUS(), WTERMSIG() */
89
90
#include <grp.h>                /* setgroups() */
90
 
#include <argz.h>               /* argz_add_sep(), argz_next(),
91
 
                                   argz_delete(), argz_append(),
92
 
                                   argz_stringify(), argz_add(),
93
 
                                   argz_count() */
94
91
 
95
92
#ifdef __linux__
96
93
#include <sys/klog.h>           /* klogctl() */
138
135
static const char sys_class_net[] = "/sys/class/net";
139
136
char *connect_to = NULL;
140
137
const char *hookdir = HOOKDIR;
141
 
uid_t uid = 65534;
142
 
gid_t gid = 65534;
143
138
 
144
139
/* Doubly linked list that need to be circularly linked when used */
145
140
typedef struct server{
146
141
  const char *ip;
147
 
  in_port_t port;
 
142
  uint16_t port;
148
143
  AvahiIfIndex if_index;
149
144
  int af;
150
145
  struct timespec last_seen;
210
205
}
211
206
 
212
207
/* Add server to set of servers to retry periodically */
213
 
bool add_server(const char *ip, in_port_t port, AvahiIfIndex if_index,
 
208
bool add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
214
209
                int af){
215
210
  int ret;
216
211
  server *new_server = malloc(sizeof(server));
470
465
}
471
466
 
472
467
static const char * safer_gnutls_strerror(int value){
473
 
  const char *ret = gnutls_strerror(value);
 
468
  const char *ret = gnutls_strerror(value); /* Spurious warning from
 
469
                                               -Wunreachable-code */
474
470
  if(ret == NULL)
475
471
    ret = "(unknown)";
476
472
  return ret;
621
617
                      __attribute__((unused)) const char *txt){}
622
618
 
623
619
/* Called when a Mandos server is found */
624
 
static int start_mandos_communication(const char *ip, in_port_t port,
 
620
static int start_mandos_communication(const char *ip, uint16_t port,
625
621
                                      AvahiIfIndex if_index,
626
622
                                      int af){
627
623
  int ret, tcp_sd = -1;
666
662
  
667
663
  if(debug){
668
664
    fprintf_plus(stderr, "Setting up a TCP connection to %s, port %"
669
 
                 PRIuMAX "\n", ip, (uintmax_t)port);
 
665
                 PRIu16 "\n", ip, port);
670
666
  }
671
667
  
672
668
  tcp_sd = socket(pf, SOCK_STREAM, 0);
703
699
    goto mandos_end;
704
700
  }
705
701
  if(af == AF_INET6){
706
 
    to.in6.sin6_port = htons(port);    
 
702
    to.in6.sin6_port = htons(port); /* Spurious warnings from
 
703
                                       -Wconversion and
 
704
                                       -Wunreachable-code */
 
705
    
707
706
    if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
708
707
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
709
708
                                -Wunreachable-code*/
733
732
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
734
733
        perror_plus("if_indextoname");
735
734
      } else {
736
 
        fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIuMAX
737
 
                     "\n", ip, interface, (uintmax_t)port);
 
735
        fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16
 
736
                     "\n", ip, interface, port);
738
737
      }
739
738
    } else {
740
 
      fprintf_plus(stderr, "Connection to: %s, port %" PRIuMAX "\n",
741
 
                   ip, (uintmax_t)port);
 
739
      fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n",
 
740
                   ip, port);
742
741
    }
743
742
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
744
743
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
1005
1004
                             AVAHI_GCC_UNUSED AvahiLookupResultFlags
1006
1005
                             flags,
1007
1006
                             AVAHI_GCC_UNUSED void* userdata){
1008
 
  if(r == NULL){
1009
 
    return;
1010
 
  }
 
1007
  assert(r);
1011
1008
  
1012
1009
  /* Called whenever a service has been resolved successfully or
1013
1010
     timed out */
1034
1031
                     PRIdMAX ") on port %" PRIu16 "\n", name,
1035
1032
                     host_name, ip, (intmax_t)interface, port);
1036
1033
      }
1037
 
      int ret = start_mandos_communication(ip, (in_port_t)port,
1038
 
                                           interface,
 
1034
      int ret = start_mandos_communication(ip, port, interface,
1039
1035
                                           avahi_proto_to_af(proto));
1040
1036
      if(ret == 0){
1041
1037
        avahi_simple_poll_quit(mc.simple_poll);
1042
1038
      } else {
1043
 
        if(not add_server(ip, (in_port_t)port, interface,
 
1039
        if(not add_server(ip, port, interface,
1044
1040
                          avahi_proto_to_af(proto))){
1045
1041
          fprintf_plus(stderr, "Failed to add server \"%s\" to server"
1046
1042
                       " list\n", name);
1061
1057
                            AVAHI_GCC_UNUSED AvahiLookupResultFlags
1062
1058
                            flags,
1063
1059
                            AVAHI_GCC_UNUSED void* userdata){
1064
 
  if(b == NULL){
1065
 
    return;
1066
 
  }
 
1060
  assert(b);
1067
1061
  
1068
1062
  /* Called whenever a new services becomes available on the LAN or
1069
1063
     is removed from the LAN */
1125
1119
 
1126
1120
bool get_flags(const char *ifname, struct ifreq *ifr){
1127
1121
  int ret;
1128
 
  error_t ret_errno;
1129
1122
  
1130
1123
  int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1131
1124
  if(s < 0){
1132
 
    ret_errno = errno;
1133
1125
    perror_plus("socket");
1134
 
    errno = ret_errno;
1135
1126
    return false;
1136
1127
  }
1137
1128
  strcpy(ifr->ifr_name, ifname);
1138
1129
  ret = ioctl(s, SIOCGIFFLAGS, ifr);
1139
1130
  if(ret == -1){
1140
1131
    if(debug){
1141
 
      ret_errno = errno;
1142
1132
      perror_plus("ioctl SIOCGIFFLAGS");
1143
 
      errno = ret_errno;
1144
1133
    }
1145
1134
    return false;
1146
1135
  }
1215
1204
}
1216
1205
 
1217
1206
/* 
1218
 
 * This function determines if a network interface is up.
1219
 
 */
1220
 
bool interface_is_up(const char *interface){
1221
 
  struct ifreq ifr;
1222
 
  if(not get_flags(interface, &ifr)){
1223
 
    if(debug){
1224
 
      fprintf_plus(stderr, "Failed to get flags for interface "
1225
 
                   "\"%s\"\n", interface);
1226
 
    }
1227
 
    return false;
1228
 
  }
1229
 
  
1230
 
  return (bool)(ifr.ifr_flags & IFF_UP);
1231
 
}
1232
 
 
1233
 
/* 
1234
 
 * This function determines if a network interface is running
1235
 
 */
1236
 
bool interface_is_running(const char *interface){
1237
 
  struct ifreq ifr;
1238
 
  if(not get_flags(interface, &ifr)){
1239
 
    if(debug){
1240
 
      fprintf_plus(stderr, "Failed to get flags for interface "
1241
 
                   "\"%s\"\n", interface);
1242
 
    }
1243
 
    return false;
1244
 
  }
1245
 
  
1246
 
  return (bool)(ifr.ifr_flags & IFF_RUNNING);
 
1207
 * This function determines if a directory entry in /sys/class/net
 
1208
 * corresponds to an acceptable network device which is up.
 
1209
 * (This function is passed to scandir(3) as a filter function.)
 
1210
 */
 
1211
int up_interface(const struct dirent *if_entry){
 
1212
  if(if_entry->d_name[0] == '.'){
 
1213
    return 0;
 
1214
  }
 
1215
  
 
1216
  struct ifreq ifr;
 
1217
  if(not get_flags(if_entry->d_name, &ifr)){
 
1218
    if(debug){
 
1219
      fprintf_plus(stderr, "Failed to get flags for interface "
 
1220
                   "\"%s\"\n", if_entry->d_name);
 
1221
    }
 
1222
    return 0;
 
1223
  }
 
1224
  
 
1225
  /* Reject down interfaces */
 
1226
  if(not (ifr.ifr_flags & IFF_UP)){
 
1227
    if(debug){
 
1228
      fprintf_plus(stderr, "Rejecting down interface \"%s\"\n",
 
1229
                   if_entry->d_name);
 
1230
    }
 
1231
    return 0;
 
1232
  }
 
1233
  
 
1234
  /* Reject non-running interfaces */
 
1235
  if(not (ifr.ifr_flags & IFF_RUNNING)){
 
1236
    if(debug){
 
1237
      fprintf_plus(stderr, "Rejecting non-running interface \"%s\"\n",
 
1238
                   if_entry->d_name);
 
1239
    }
 
1240
    return 0;
 
1241
  }
 
1242
  
 
1243
  if(not good_flags(if_entry->d_name, &ifr)){
 
1244
    return 0;
 
1245
  }
 
1246
  return 1;
1247
1247
}
1248
1248
 
1249
1249
int notdotentries(const struct dirent *direntry){
1390
1390
  }
1391
1391
}
1392
1392
 
1393
 
/* Set effective uid to 0, return errno */
1394
 
error_t raise_privileges(void){
1395
 
  error_t old_errno = errno;
1396
 
  error_t ret_errno = 0;
1397
 
  if(seteuid(0) == -1){
1398
 
    ret_errno = errno;
1399
 
    perror_plus("seteuid");
1400
 
  }
1401
 
  errno = old_errno;
1402
 
  return ret_errno;
1403
 
}
1404
 
 
1405
 
/* Set effective and real user ID to 0.  Return errno. */
1406
 
error_t raise_privileges_permanently(void){
1407
 
  error_t old_errno = errno;
1408
 
  error_t ret_errno = raise_privileges();
1409
 
  if(ret_errno != 0){
1410
 
    errno = old_errno;
1411
 
    return ret_errno;
1412
 
  }
1413
 
  if(setuid(0) == -1){
1414
 
    ret_errno = errno;
1415
 
    perror_plus("seteuid");
1416
 
  }
1417
 
  errno = old_errno;
1418
 
  return ret_errno;
1419
 
}
1420
 
 
1421
 
/* Set effective user ID to unprivileged saved user ID */
1422
 
error_t lower_privileges(void){
1423
 
  error_t old_errno = errno;
1424
 
  error_t ret_errno = 0;
1425
 
  if(seteuid(uid) == -1){
1426
 
    ret_errno = errno;
1427
 
    perror_plus("seteuid");
1428
 
  }
1429
 
  errno = old_errno;
1430
 
  return ret_errno;
1431
 
}
1432
 
 
1433
 
/* Lower privileges permanently */
1434
 
error_t lower_privileges_permanently(void){
1435
 
  error_t old_errno = errno;
1436
 
  error_t ret_errno = 0;
1437
 
  if(setuid(uid) == -1){
1438
 
    ret_errno = errno;
1439
 
    perror_plus("setuid");
1440
 
  }
1441
 
  errno = old_errno;
1442
 
  return ret_errno;
1443
 
}
1444
 
 
1445
1393
bool run_network_hooks(const char *mode, const char *interface,
1446
1394
                       const float delay){
1447
1395
  struct dirent **direntries;
1450
1398
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1451
1399
                         alphasort);
1452
1400
  if(numhooks == -1){
1453
 
    if(errno == ENOENT){
1454
 
      if(debug){
1455
 
        fprintf_plus(stderr, "Network hook directory \"%s\" not"
1456
 
                     " found\n", hookdir);
1457
 
      }
1458
 
    } else {
1459
 
      perror_plus("scandir");
1460
 
    }
 
1401
    perror_plus("scandir");
1461
1402
  } else {
1462
1403
    int devnull = open("/dev/null", O_RDONLY);
1463
1404
    for(int i = 0; i < numhooks; i++){
1476
1417
      if(hook_pid == 0){
1477
1418
        /* Child */
1478
1419
        /* Raise privileges */
1479
 
        raise_privileges_permanently();
 
1420
        errno = 0;
 
1421
        ret = seteuid(0);
 
1422
        if(ret == -1){
 
1423
          perror_plus("seteuid");
 
1424
        }
 
1425
        /* Raise privileges even more */
 
1426
        errno = 0;
 
1427
        ret = setuid(0);
 
1428
        if(ret == -1){
 
1429
          perror_plus("setuid");
 
1430
        }
1480
1431
        /* Set group */
1481
1432
        errno = 0;
1482
1433
        ret = setgid(0);
1575
1526
  return true;
1576
1527
}
1577
1528
 
1578
 
error_t bring_up_interface(const char *const interface,
1579
 
                           const float delay){
1580
 
  int sd = -1;
1581
 
  error_t old_errno = errno;
1582
 
  error_t ret_errno = 0;
1583
 
  int ret, ret_setflags;
1584
 
  struct ifreq network;
1585
 
  unsigned int if_index = if_nametoindex(interface);
1586
 
  if(if_index == 0){
1587
 
    fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1588
 
    errno = old_errno;
1589
 
    return ENXIO;
1590
 
  }
1591
 
  
1592
 
  if(quit_now){
1593
 
    errno = old_errno;
1594
 
    return EINTR;
1595
 
  }
1596
 
  
1597
 
  if(not interface_is_up(interface)){
1598
 
    if(not get_flags(interface, &network) and debug){
1599
 
      ret_errno = errno;
1600
 
      fprintf_plus(stderr, "Failed to get flags for interface "
1601
 
                   "\"%s\"\n", interface);
1602
 
      return ret_errno;
1603
 
    }
1604
 
    network.ifr_flags |= IFF_UP;
1605
 
    
1606
 
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1607
 
    if(sd < 0){
1608
 
      ret_errno = errno;
1609
 
      perror_plus("socket");
1610
 
      errno = old_errno;
1611
 
      return ret_errno;
1612
 
    }
1613
 
  
1614
 
    if(quit_now){
1615
 
      close(sd);
1616
 
      errno = old_errno;
1617
 
      return EINTR;
1618
 
    }
1619
 
    
1620
 
    if(debug){
1621
 
      fprintf_plus(stderr, "Bringing up interface \"%s\"\n",
1622
 
                   interface);
1623
 
    }
1624
 
    
1625
 
    /* Raise priviliges */
1626
 
    raise_privileges();
1627
 
    
1628
 
#ifdef __linux__
1629
 
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1630
 
       messages about the network interface to mess up the prompt */
1631
 
    int ret_linux = klogctl(8, NULL, 5);
1632
 
    bool restore_loglevel = true;
1633
 
    if(ret_linux == -1){
1634
 
      restore_loglevel = false;
1635
 
      perror_plus("klogctl");
1636
 
    }
1637
 
#endif  /* __linux__ */
1638
 
    ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1639
 
    ret_errno = errno;
1640
 
#ifdef __linux__
1641
 
    if(restore_loglevel){
1642
 
      ret_linux = klogctl(7, NULL, 0);
1643
 
      if(ret_linux == -1){
1644
 
        perror_plus("klogctl");
1645
 
      }
1646
 
    }
1647
 
#endif  /* __linux__ */
1648
 
    
1649
 
    /* Lower privileges */
1650
 
    lower_privileges();
1651
 
    
1652
 
    /* Close the socket */
1653
 
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
1654
 
    if(ret == -1){
1655
 
      perror_plus("close");
1656
 
    }
1657
 
    
1658
 
    if(ret_setflags == -1){
1659
 
      errno = ret_errno;
1660
 
      perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1661
 
      errno = old_errno;
1662
 
      return ret_errno;
1663
 
    }
1664
 
  } else if(debug){
1665
 
    fprintf_plus(stderr, "Interface \"%s\" is already up; good\n",
1666
 
                 interface);
1667
 
  }
1668
 
  
1669
 
  /* Sleep checking until interface is running.
1670
 
     Check every 0.25s, up to total time of delay */
1671
 
  for(int i=0; i < delay * 4; i++){
1672
 
    if(interface_is_running(interface)){
1673
 
      break;
1674
 
    }
1675
 
    struct timespec sleeptime = { .tv_nsec = 250000000 };
1676
 
    ret = nanosleep(&sleeptime, NULL);
1677
 
    if(ret == -1 and errno != EINTR){
1678
 
      perror_plus("nanosleep");
1679
 
    }
1680
 
  }
1681
 
  
1682
 
  errno = old_errno;
1683
 
  return 0;
1684
 
}
1685
 
 
1686
 
error_t take_down_interface(const char *const interface){
1687
 
  int sd = -1;
1688
 
  error_t old_errno = errno;
1689
 
  error_t ret_errno = 0;
1690
 
  int ret, ret_setflags;
1691
 
  struct ifreq network;
1692
 
  unsigned int if_index = if_nametoindex(interface);
1693
 
  if(if_index == 0){
1694
 
    fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1695
 
    errno = old_errno;
1696
 
    return ENXIO;
1697
 
  }
1698
 
  if(interface_is_up(interface)){
1699
 
    if(not get_flags(interface, &network) and debug){
1700
 
      ret_errno = errno;
1701
 
      fprintf_plus(stderr, "Failed to get flags for interface "
1702
 
                   "\"%s\"\n", interface);
1703
 
      return ret_errno;
1704
 
    }
1705
 
    network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1706
 
    
1707
 
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1708
 
    if(sd < 0){
1709
 
      ret_errno = errno;
1710
 
      perror_plus("socket");
1711
 
      errno = old_errno;
1712
 
      return ret_errno;
1713
 
    }
1714
 
    
1715
 
    if(debug){
1716
 
      fprintf_plus(stderr, "Taking down interface \"%s\"\n",
1717
 
                   interface);
1718
 
    }
1719
 
    
1720
 
    /* Raise priviliges */
1721
 
    raise_privileges();
1722
 
    
1723
 
    ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1724
 
    ret_errno = errno;
1725
 
    
1726
 
    /* Lower privileges */
1727
 
    lower_privileges();
1728
 
    
1729
 
    /* Close the socket */
1730
 
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
1731
 
    if(ret == -1){
1732
 
      perror_plus("close");
1733
 
    }
1734
 
    
1735
 
    if(ret_setflags == -1){
1736
 
      errno = ret_errno;
1737
 
      perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
1738
 
      errno = old_errno;
1739
 
      return ret_errno;
1740
 
    }
1741
 
  } else if(debug){
1742
 
    fprintf_plus(stderr, "Interface \"%s\" is already down; odd\n",
1743
 
                 interface);
1744
 
  }
1745
 
  
1746
 
  errno = old_errno;
1747
 
  return 0;
1748
 
}
1749
 
 
1750
1529
int main(int argc, char *argv[]){
1751
1530
  AvahiSServiceBrowser *sb = NULL;
1752
 
  error_t ret_errno;
 
1531
  int error;
1753
1532
  int ret;
1754
1533
  intmax_t tmpmax;
1755
1534
  char *tmp;
1756
1535
  int exitcode = EXIT_SUCCESS;
1757
 
  char *interfaces = NULL;
1758
 
  size_t interfaces_size = 0;
1759
 
  char *interfaces_to_take_down = NULL;
1760
 
  size_t interfaces_to_take_down_size = 0;
 
1536
  const char *interface = "";
 
1537
  struct ifreq network;
 
1538
  int sd = -1;
 
1539
  bool take_down_interface = false;
 
1540
  uid_t uid;
 
1541
  gid_t gid;
1761
1542
  char tempdir[] = "/tmp/mandosXXXXXX";
1762
1543
  bool tempdir_created = false;
1763
1544
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1764
1545
  const char *seckey = PATHDIR "/" SECKEY;
1765
1546
  const char *pubkey = PATHDIR "/" PUBKEY;
1766
 
  char *interfaces_hooks = NULL;
1767
 
  size_t interfaces_hooks_size = 0;
1768
1547
  
1769
1548
  bool gnutls_initialized = false;
1770
1549
  bool gpgme_initialized = false;
1861
1640
        connect_to = arg;
1862
1641
        break;
1863
1642
      case 'i':                 /* --interface */
1864
 
        ret_errno = argz_add_sep(&interfaces, &interfaces_size, arg,
1865
 
                                 (int)',');
1866
 
        if(ret_errno != 0){
1867
 
          argp_error(state, "%s", strerror(ret_errno));
1868
 
        }
 
1643
        interface = arg;
1869
1644
        break;
1870
1645
      case 's':                 /* --seckey */
1871
1646
        seckey = arg;
1949
1724
       <http://bugs.debian.org/633582> */
1950
1725
    
1951
1726
    /* Re-raise priviliges */
1952
 
    if(raise_privileges() == 0){
 
1727
    errno = 0;
 
1728
    ret = seteuid(0);
 
1729
    if(ret == -1){
 
1730
      perror_plus("seteuid");
 
1731
    } else {
1953
1732
      struct stat st;
1954
1733
      
1955
1734
      if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
2003
1782
    }
2004
1783
  }
2005
1784
  
2006
 
  /* Remove empty interface names */
2007
 
  {
2008
 
    char *interface = NULL;
2009
 
    while((interface = argz_next(interfaces, interfaces_size,
2010
 
                                 interface))){
2011
 
      if(if_nametoindex(interface) == 0){
2012
 
        if(interface[0] != '\0' and strcmp(interface, "none") != 0){
2013
 
          fprintf_plus(stderr, "Not using nonexisting interface"
2014
 
                       " \"%s\"\n", interface);
2015
 
        }
2016
 
        argz_delete(&interfaces, &interfaces_size, interface);
2017
 
        interface = NULL;
2018
 
      }
2019
 
    }
2020
 
  }
2021
 
  
2022
1785
  /* Run network hooks */
2023
 
  {
2024
 
    
2025
 
    if(interfaces != NULL){
2026
 
      interfaces_hooks = malloc(interfaces_size);
2027
 
      if(interfaces_hooks == NULL){
2028
 
        perror_plus("malloc");
2029
 
        goto end;
2030
 
      }
2031
 
      memcpy(interfaces_hooks, interfaces, interfaces_size);
2032
 
      interfaces_hooks_size = interfaces_size;
2033
 
      argz_stringify(interfaces_hooks, interfaces_hooks_size,
2034
 
                     (int)',');
2035
 
    }
2036
 
    if(not run_network_hooks("start", interfaces_hooks != NULL ?
2037
 
                             interfaces_hooks : "", delay)){
2038
 
      goto end;
2039
 
    }
 
1786
  if(not run_network_hooks("start", interface, delay)){
 
1787
    goto end;
2040
1788
  }
2041
1789
  
2042
1790
  if(not debug){
2043
1791
    avahi_set_log_function(empty_log);
2044
1792
  }
2045
1793
  
 
1794
  if(interface[0] == '\0'){
 
1795
    struct dirent **direntries;
 
1796
    /* First look for interfaces that are up */
 
1797
    ret = scandir(sys_class_net, &direntries, up_interface,
 
1798
                  alphasort);
 
1799
    if(ret == 0){
 
1800
      /* No up interfaces, look for any good interfaces */
 
1801
      free(direntries);
 
1802
      ret = scandir(sys_class_net, &direntries, good_interface,
 
1803
                    alphasort);
 
1804
    }
 
1805
    if(ret >= 1){
 
1806
      /* Pick the first interface returned */
 
1807
      interface = strdup(direntries[0]->d_name);
 
1808
      if(debug){
 
1809
        fprintf_plus(stderr, "Using interface \"%s\"\n", interface);
 
1810
      }
 
1811
      if(interface == NULL){
 
1812
        perror_plus("malloc");
 
1813
        free(direntries);
 
1814
        exitcode = EXIT_FAILURE;
 
1815
        goto end;
 
1816
      }
 
1817
      free(direntries);
 
1818
    } else {
 
1819
      free(direntries);
 
1820
      fprintf_plus(stderr, "Could not find a network interface\n");
 
1821
      exitcode = EXIT_FAILURE;
 
1822
      goto end;
 
1823
    }
 
1824
  }
 
1825
  
2046
1826
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
2047
1827
     from the signal handler */
2048
1828
  /* Initialize the pseudo-RNG for Avahi */
2118
1898
    }
2119
1899
  }
2120
1900
  
2121
 
  /* If no interfaces were specified, make a list */
2122
 
  if(interfaces == NULL){
2123
 
    struct dirent **direntries;
2124
 
    /* Look for any good interfaces */
2125
 
    ret = scandir(sys_class_net, &direntries, good_interface,
2126
 
                  alphasort);
2127
 
    if(ret >= 1){
2128
 
      /* Add all found interfaces to interfaces list */
2129
 
      for(int i = 0; i < ret; ++i){
2130
 
        ret_errno = argz_add(&interfaces, &interfaces_size,
2131
 
                             direntries[i]->d_name);
2132
 
        if(ret_errno != 0){
2133
 
          perror_plus("argz_add");
2134
 
          continue;
2135
 
        }
2136
 
        if(debug){
2137
 
          fprintf_plus(stderr, "Will use interface \"%s\"\n",
2138
 
                       direntries[i]->d_name);
2139
 
        }
2140
 
      }
2141
 
      free(direntries);
2142
 
    } else {
2143
 
      free(direntries);
2144
 
      fprintf_plus(stderr, "Could not find a network interface\n");
2145
 
      exitcode = EXIT_FAILURE;
2146
 
      goto end;
2147
 
    }
2148
 
  }
2149
 
  
2150
 
  /* If we only got one interface, explicitly use only that one */
2151
 
  if(argz_count(interfaces, interfaces_size) == 1){
2152
 
    if(debug){
2153
 
      fprintf_plus(stderr, "Using only interface \"%s\"\n",
2154
 
                   interfaces);
2155
 
    }
2156
 
    if_index = (AvahiIfIndex)if_nametoindex(interfaces);
2157
 
  }
2158
 
  
2159
 
  /* Bring up interfaces which are down */
2160
 
  if(not (argz_count(interfaces, interfaces_size) == 1
2161
 
          and strcmp(interfaces, "none") == 0)){
2162
 
    char *interface = NULL;
2163
 
    while((interface = argz_next(interfaces, interfaces_size,
2164
 
                                 interface))){
2165
 
      bool interface_was_up = interface_is_up(interface);
2166
 
      ret = bring_up_interface(interface, delay);
2167
 
      if(not interface_was_up){
2168
 
        if(ret != 0){
2169
 
          errno = ret;
2170
 
          perror_plus("Failed to bring up interface");
2171
 
        } else {
2172
 
          ret_errno = argz_add(&interfaces_to_take_down,
2173
 
                               &interfaces_to_take_down_size,
2174
 
                               interface);
2175
 
        }
2176
 
      }
2177
 
    }
2178
 
    free(interfaces);
2179
 
    interfaces = NULL;
2180
 
    interfaces_size = 0;
2181
 
    if(debug and (interfaces_to_take_down == NULL)){
2182
 
      fprintf_plus(stderr, "No interfaces were brought up\n");
 
1901
  /* If the interface is down, bring it up */
 
1902
  if(strcmp(interface, "none") != 0){
 
1903
    if_index = (AvahiIfIndex) if_nametoindex(interface);
 
1904
    if(if_index == 0){
 
1905
      fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
 
1906
      exitcode = EX_UNAVAILABLE;
 
1907
      goto end;
 
1908
    }
 
1909
    
 
1910
    if(quit_now){
 
1911
      goto end;
 
1912
    }
 
1913
    
 
1914
    /* Re-raise priviliges */
 
1915
    errno = 0;
 
1916
    ret = seteuid(0);
 
1917
    if(ret == -1){
 
1918
      perror_plus("seteuid");
 
1919
    }
 
1920
    
 
1921
#ifdef __linux__
 
1922
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
 
1923
       messages about the network interface to mess up the prompt */
 
1924
    ret = klogctl(8, NULL, 5);
 
1925
    bool restore_loglevel = true;
 
1926
    if(ret == -1){
 
1927
      restore_loglevel = false;
 
1928
      perror_plus("klogctl");
 
1929
    }
 
1930
#endif  /* __linux__ */
 
1931
    
 
1932
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
1933
    if(sd < 0){
 
1934
      perror_plus("socket");
 
1935
      exitcode = EX_OSERR;
 
1936
#ifdef __linux__
 
1937
      if(restore_loglevel){
 
1938
        ret = klogctl(7, NULL, 0);
 
1939
        if(ret == -1){
 
1940
          perror_plus("klogctl");
 
1941
        }
 
1942
      }
 
1943
#endif  /* __linux__ */
 
1944
      /* Lower privileges */
 
1945
      errno = 0;
 
1946
      ret = seteuid(uid);
 
1947
      if(ret == -1){
 
1948
        perror_plus("seteuid");
 
1949
      }
 
1950
      goto end;
 
1951
    }
 
1952
    strcpy(network.ifr_name, interface);
 
1953
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
1954
    if(ret == -1){
 
1955
      perror_plus("ioctl SIOCGIFFLAGS");
 
1956
#ifdef __linux__
 
1957
      if(restore_loglevel){
 
1958
        ret = klogctl(7, NULL, 0);
 
1959
        if(ret == -1){
 
1960
          perror_plus("klogctl");
 
1961
        }
 
1962
      }
 
1963
#endif  /* __linux__ */
 
1964
      exitcode = EX_OSERR;
 
1965
      /* Lower privileges */
 
1966
      errno = 0;
 
1967
      ret = seteuid(uid);
 
1968
      if(ret == -1){
 
1969
        perror_plus("seteuid");
 
1970
      }
 
1971
      goto end;
 
1972
    }
 
1973
    if((network.ifr_flags & IFF_UP) == 0){
 
1974
      network.ifr_flags |= IFF_UP;
 
1975
      take_down_interface = true;
 
1976
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
1977
      if(ret == -1){
 
1978
        take_down_interface = false;
 
1979
        perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
 
1980
        exitcode = EX_OSERR;
 
1981
#ifdef __linux__
 
1982
        if(restore_loglevel){
 
1983
          ret = klogctl(7, NULL, 0);
 
1984
          if(ret == -1){
 
1985
            perror_plus("klogctl");
 
1986
          }
 
1987
        }
 
1988
#endif  /* __linux__ */
 
1989
        /* Lower privileges */
 
1990
        errno = 0;
 
1991
        ret = seteuid(uid);
 
1992
        if(ret == -1){
 
1993
          perror_plus("seteuid");
 
1994
        }
 
1995
        goto end;
 
1996
      }
 
1997
    }
 
1998
    /* Sleep checking until interface is running.
 
1999
       Check every 0.25s, up to total time of delay */
 
2000
    for(int i=0; i < delay * 4; i++){
 
2001
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
2002
      if(ret == -1){
 
2003
        perror_plus("ioctl SIOCGIFFLAGS");
 
2004
      } else if(network.ifr_flags & IFF_RUNNING){
 
2005
        break;
 
2006
      }
 
2007
      struct timespec sleeptime = { .tv_nsec = 250000000 };
 
2008
      ret = nanosleep(&sleeptime, NULL);
 
2009
      if(ret == -1 and errno != EINTR){
 
2010
        perror_plus("nanosleep");
 
2011
      }
 
2012
    }
 
2013
    if(not take_down_interface){
 
2014
      /* We won't need the socket anymore */
 
2015
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
2016
      if(ret == -1){
 
2017
        perror_plus("close");
 
2018
      }
 
2019
    }
 
2020
#ifdef __linux__
 
2021
    if(restore_loglevel){
 
2022
      /* Restores kernel loglevel to default */
 
2023
      ret = klogctl(7, NULL, 0);
 
2024
      if(ret == -1){
 
2025
        perror_plus("klogctl");
 
2026
      }
 
2027
    }
 
2028
#endif  /* __linux__ */
 
2029
    /* Lower privileges */
 
2030
    errno = 0;
 
2031
    /* Lower privileges */
 
2032
    ret = seteuid(uid);
 
2033
    if(ret == -1){
 
2034
      perror_plus("seteuid");
2183
2035
    }
2184
2036
  }
2185
2037
  
2226
2078
    /* Connect directly, do not use Zeroconf */
2227
2079
    /* (Mainly meant for debugging) */
2228
2080
    char *address = strrchr(connect_to, ':');
2229
 
    
2230
2081
    if(address == NULL){
2231
2082
      fprintf_plus(stderr, "No colon in address\n");
2232
2083
      exitcode = EX_USAGE;
2237
2088
      goto end;
2238
2089
    }
2239
2090
    
2240
 
    in_port_t port;
 
2091
    uint16_t port;
2241
2092
    errno = 0;
2242
2093
    tmpmax = strtoimax(address+1, &tmp, 10);
2243
2094
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
2244
 
       or tmpmax != (in_port_t)tmpmax){
 
2095
       or tmpmax != (uint16_t)tmpmax){
2245
2096
      fprintf_plus(stderr, "Bad port number\n");
2246
2097
      exitcode = EX_USAGE;
2247
2098
      goto end;
2251
2102
      goto end;
2252
2103
    }
2253
2104
    
2254
 
    port = (in_port_t)tmpmax;
 
2105
    port = (uint16_t)tmpmax;
2255
2106
    *address = '\0';
2256
2107
    /* Colon in address indicates IPv6 */
2257
2108
    int af;
2307
2158
    /* Allocate a new server */
2308
2159
    mc.server = avahi_server_new(avahi_simple_poll_get
2309
2160
                                 (mc.simple_poll), &config, NULL,
2310
 
                                 NULL, &ret_errno);
 
2161
                                 NULL, &error);
2311
2162
    
2312
2163
    /* Free the Avahi configuration data */
2313
2164
    avahi_server_config_free(&config);
2316
2167
  /* Check if creating the Avahi server object succeeded */
2317
2168
  if(mc.server == NULL){
2318
2169
    fprintf_plus(stderr, "Failed to create Avahi server: %s\n",
2319
 
                 avahi_strerror(ret_errno));
 
2170
                 avahi_strerror(error));
2320
2171
    exitcode = EX_UNAVAILABLE;
2321
2172
    goto end;
2322
2173
  }
2390
2241
    }
2391
2242
  }
2392
2243
  
 
2244
  /* Run network hooks */
 
2245
  run_network_hooks("stop", interface, delay);
 
2246
  
2393
2247
  /* Re-raise priviliges */
2394
2248
  {
2395
 
    raise_privileges();
2396
 
    
2397
 
    /* Run network hooks */
2398
 
    run_network_hooks("stop", interfaces_hooks != NULL ?
2399
 
                      interfaces_hooks : "", delay);
2400
 
    
2401
 
    /* Take down the network interfaces which were brought up */
2402
 
    {
2403
 
      char *interface = NULL;
2404
 
      while((interface=argz_next(interfaces_to_take_down,
2405
 
                                 interfaces_to_take_down_size,
2406
 
                                 interface))){
2407
 
        ret_errno = take_down_interface(interface);
2408
 
        if(ret_errno != 0){
2409
 
          errno = ret_errno;
2410
 
          perror_plus("Failed to take down interface");
 
2249
    errno = 0;
 
2250
    ret = seteuid(0);
 
2251
    if(ret == -1){
 
2252
      perror_plus("seteuid");
 
2253
    }
 
2254
    
 
2255
    /* Take down the network interface */
 
2256
    if(take_down_interface and geteuid() == 0){
 
2257
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
2258
      if(ret == -1){
 
2259
        perror_plus("ioctl SIOCGIFFLAGS");
 
2260
      } else if(network.ifr_flags & IFF_UP){
 
2261
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
 
2262
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
2263
        if(ret == -1){
 
2264
          perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
2411
2265
        }
2412
2266
      }
2413
 
      if(debug and (interfaces_to_take_down == NULL)){
2414
 
        fprintf_plus(stderr, "No interfaces needed to be taken"
2415
 
                     " down\n");
 
2267
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
2268
      if(ret == -1){
 
2269
        perror_plus("close");
2416
2270
      }
2417
2271
    }
2418
 
    
2419
 
    lower_privileges_permanently();
2420
 
  }
2421
 
  
2422
 
  free(interfaces_to_take_down);
2423
 
  free(interfaces_hooks);
 
2272
  }
 
2273
  /* Lower privileges permanently */
 
2274
  errno = 0;
 
2275
  ret = setuid(uid);
 
2276
  if(ret == -1){
 
2277
    perror_plus("setuid");
 
2278
  }
2424
2279
  
2425
2280
  /* Removes the GPGME temp directory and all files inside */
2426
2281
  if(tempdir_created){