/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:
88
88
#include <sys/wait.h>           /* waitpid(), WIFEXITED(),
89
89
                                   WEXITSTATUS(), WTERMSIG() */
90
90
#include <grp.h>                /* setgroups() */
91
 
#include <argz.h>               /* argz_add_sep(), argz_next(),
92
 
                                   argz_delete(), argz_append(),
93
 
                                   argz_stringify(), argz_add(),
94
 
                                   argz_count() */
95
91
 
96
92
#ifdef __linux__
97
93
#include <sys/klog.h>           /* klogctl() */
139
135
static const char sys_class_net[] = "/sys/class/net";
140
136
char *connect_to = NULL;
141
137
const char *hookdir = HOOKDIR;
142
 
uid_t uid = 65534;
143
 
gid_t gid = 65534;
144
138
 
145
139
/* Doubly linked list that need to be circularly linked when used */
146
140
typedef struct server{
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;
1469
1417
      if(hook_pid == 0){
1470
1418
        /* Child */
1471
1419
        /* Raise privileges */
1472
 
        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
        }
1473
1431
        /* Set group */
1474
1432
        errno = 0;
1475
1433
        ret = setgid(0);
1568
1526
  return true;
1569
1527
}
1570
1528
 
1571
 
error_t bring_up_interface(const char *const interface,
1572
 
                           const float delay){
1573
 
  int sd = -1;
1574
 
  error_t old_errno = errno;
1575
 
  error_t ret_errno = 0;
1576
 
  int ret, ret_setflags;
1577
 
  struct ifreq network;
1578
 
  unsigned int if_index = if_nametoindex(interface);
1579
 
  if(if_index == 0){
1580
 
    fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1581
 
    errno = old_errno;
1582
 
    return ENXIO;
1583
 
  }
1584
 
  
1585
 
  if(quit_now){
1586
 
    errno = old_errno;
1587
 
    return EINTR;
1588
 
  }
1589
 
  
1590
 
  if(not interface_is_up(interface)){
1591
 
    if(not get_flags(interface, &network) and debug){
1592
 
      ret_errno = errno;
1593
 
      fprintf_plus(stderr, "Failed to get flags for interface "
1594
 
                   "\"%s\"\n", interface);
1595
 
      return ret_errno;
1596
 
    }
1597
 
    network.ifr_flags |= IFF_UP;
1598
 
    
1599
 
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1600
 
    if(sd < 0){
1601
 
      ret_errno = errno;
1602
 
      perror_plus("socket");
1603
 
      errno = old_errno;
1604
 
      return ret_errno;
1605
 
    }
1606
 
  
1607
 
    if(quit_now){
1608
 
      close(sd);
1609
 
      errno = old_errno;
1610
 
      return EINTR;
1611
 
    }
1612
 
    
1613
 
    if(debug){
1614
 
      fprintf_plus(stderr, "Bringing up interface \"%s\"\n",
1615
 
                   interface);
1616
 
    }
1617
 
    
1618
 
    /* Raise priviliges */
1619
 
    raise_privileges();
1620
 
    
1621
 
#ifdef __linux__
1622
 
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1623
 
       messages about the network interface to mess up the prompt */
1624
 
    int ret_linux = klogctl(8, NULL, 5);
1625
 
    bool restore_loglevel = true;
1626
 
    if(ret_linux == -1){
1627
 
      restore_loglevel = false;
1628
 
      perror_plus("klogctl");
1629
 
    }
1630
 
#endif  /* __linux__ */
1631
 
    ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1632
 
    ret_errno = errno;
1633
 
#ifdef __linux__
1634
 
    if(restore_loglevel){
1635
 
      ret_linux = klogctl(7, NULL, 0);
1636
 
      if(ret_linux == -1){
1637
 
        perror_plus("klogctl");
1638
 
      }
1639
 
    }
1640
 
#endif  /* __linux__ */
1641
 
    
1642
 
    /* Lower privileges */
1643
 
    lower_privileges();
1644
 
    
1645
 
    /* Close the socket */
1646
 
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
1647
 
    if(ret == -1){
1648
 
      perror_plus("close");
1649
 
    }
1650
 
    
1651
 
    if(ret_setflags == -1){
1652
 
      errno = ret_errno;
1653
 
      perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1654
 
      errno = old_errno;
1655
 
      return ret_errno;
1656
 
    }
1657
 
  } else if(debug){
1658
 
    fprintf_plus(stderr, "Interface \"%s\" is already up; good\n",
1659
 
                 interface);
1660
 
  }
1661
 
  
1662
 
  /* Sleep checking until interface is running.
1663
 
     Check every 0.25s, up to total time of delay */
1664
 
  for(int i=0; i < delay * 4; i++){
1665
 
    if(interface_is_running(interface)){
1666
 
      break;
1667
 
    }
1668
 
    struct timespec sleeptime = { .tv_nsec = 250000000 };
1669
 
    ret = nanosleep(&sleeptime, NULL);
1670
 
    if(ret == -1 and errno != EINTR){
1671
 
      perror_plus("nanosleep");
1672
 
    }
1673
 
  }
1674
 
  
1675
 
  errno = old_errno;
1676
 
  return 0;
1677
 
}
1678
 
 
1679
 
error_t take_down_interface(const char *const interface){
1680
 
  int sd = -1;
1681
 
  error_t old_errno = errno;
1682
 
  error_t ret_errno = 0;
1683
 
  int ret, ret_setflags;
1684
 
  struct ifreq network;
1685
 
  unsigned int if_index = if_nametoindex(interface);
1686
 
  if(if_index == 0){
1687
 
    fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1688
 
    errno = old_errno;
1689
 
    return ENXIO;
1690
 
  }
1691
 
  if(interface_is_up(interface)){
1692
 
    if(not get_flags(interface, &network) and debug){
1693
 
      ret_errno = errno;
1694
 
      fprintf_plus(stderr, "Failed to get flags for interface "
1695
 
                   "\"%s\"\n", interface);
1696
 
      return ret_errno;
1697
 
    }
1698
 
    network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1699
 
    
1700
 
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1701
 
    if(sd < 0){
1702
 
      ret_errno = errno;
1703
 
      perror_plus("socket");
1704
 
      errno = old_errno;
1705
 
      return ret_errno;
1706
 
    }
1707
 
    
1708
 
    if(debug){
1709
 
      fprintf_plus(stderr, "Taking down interface \"%s\"\n",
1710
 
                   interface);
1711
 
    }
1712
 
    
1713
 
    /* Raise priviliges */
1714
 
    raise_privileges();
1715
 
    
1716
 
    ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1717
 
    ret_errno = errno;
1718
 
    
1719
 
    /* Lower privileges */
1720
 
    lower_privileges();
1721
 
    
1722
 
    /* Close the socket */
1723
 
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
1724
 
    if(ret == -1){
1725
 
      perror_plus("close");
1726
 
    }
1727
 
    
1728
 
    if(ret_setflags == -1){
1729
 
      errno = ret_errno;
1730
 
      perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
1731
 
      errno = old_errno;
1732
 
      return ret_errno;
1733
 
    }
1734
 
  } else if(debug){
1735
 
    fprintf_plus(stderr, "Interface \"%s\" is already down; odd\n",
1736
 
                 interface);
1737
 
  }
1738
 
  
1739
 
  errno = old_errno;
1740
 
  return 0;
1741
 
}
1742
 
 
1743
1529
int main(int argc, char *argv[]){
1744
1530
  AvahiSServiceBrowser *sb = NULL;
1745
 
  error_t ret_errno;
 
1531
  int error;
1746
1532
  int ret;
1747
1533
  intmax_t tmpmax;
1748
1534
  char *tmp;
1749
1535
  int exitcode = EXIT_SUCCESS;
1750
 
  char *interfaces = NULL;
1751
 
  size_t interfaces_size = 0;
1752
 
  char *interfaces_to_take_down = NULL;
1753
 
  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;
1754
1542
  char tempdir[] = "/tmp/mandosXXXXXX";
1755
1543
  bool tempdir_created = false;
1756
1544
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1757
1545
  const char *seckey = PATHDIR "/" SECKEY;
1758
1546
  const char *pubkey = PATHDIR "/" PUBKEY;
1759
 
  char *interfaces_hooks = NULL;
1760
 
  size_t interfaces_hooks_size = 0;
1761
1547
  
1762
1548
  bool gnutls_initialized = false;
1763
1549
  bool gpgme_initialized = false;
1854
1640
        connect_to = arg;
1855
1641
        break;
1856
1642
      case 'i':                 /* --interface */
1857
 
        ret_errno = argz_add_sep(&interfaces, &interfaces_size, arg,
1858
 
                                 (int)',');
1859
 
        if(ret_errno != 0){
1860
 
          argp_error(state, "%s", strerror(ret_errno));
1861
 
        }
 
1643
        interface = arg;
1862
1644
        break;
1863
1645
      case 's':                 /* --seckey */
1864
1646
        seckey = arg;
1942
1724
       <http://bugs.debian.org/633582> */
1943
1725
    
1944
1726
    /* Re-raise priviliges */
1945
 
    if(raise_privileges() == 0){
 
1727
    errno = 0;
 
1728
    ret = seteuid(0);
 
1729
    if(ret == -1){
 
1730
      perror_plus("seteuid");
 
1731
    } else {
1946
1732
      struct stat st;
1947
1733
      
1948
1734
      if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1996
1782
    }
1997
1783
  }
1998
1784
  
1999
 
  /* Remove empty interface names */
2000
 
  {
2001
 
    char *interface = NULL;
2002
 
    while((interface = argz_next(interfaces, interfaces_size,
2003
 
                                 interface))){
2004
 
      if(if_nametoindex(interface) == 0){
2005
 
        if(interface[0] != '\0' and strcmp(interface, "none") != 0){
2006
 
          fprintf_plus(stderr, "Not using nonexisting interface"
2007
 
                       " \"%s\"\n", interface);
2008
 
        }
2009
 
        argz_delete(&interfaces, &interfaces_size, interface);
2010
 
        interface = NULL;
2011
 
      }
2012
 
    }
2013
 
  }
2014
 
  
2015
1785
  /* Run network hooks */
2016
 
  {
2017
 
    ret_errno = argz_append(&interfaces_hooks, &interfaces_hooks_size,
2018
 
                            interfaces, interfaces_size);
2019
 
    if(ret_errno != 0){
2020
 
      errno = ret_errno;
2021
 
      perror_plus("argz_append");
2022
 
      goto end;
2023
 
    }
2024
 
    argz_stringify(interfaces_hooks, interfaces_hooks_size, (int)',');
2025
 
    if(not run_network_hooks("start", interfaces_hooks, delay)){
2026
 
      goto end;
2027
 
    }
 
1786
  if(not run_network_hooks("start", interface, delay)){
 
1787
    goto end;
2028
1788
  }
2029
1789
  
2030
1790
  if(not debug){
2031
1791
    avahi_set_log_function(empty_log);
2032
1792
  }
2033
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
  
2034
1826
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
2035
1827
     from the signal handler */
2036
1828
  /* Initialize the pseudo-RNG for Avahi */
2106
1898
    }
2107
1899
  }
2108
1900
  
2109
 
  /* If no interfaces were specified, make a list */
2110
 
  if(interfaces == NULL){
2111
 
    struct dirent **direntries;
2112
 
    /* Look for any good interfaces */
2113
 
    ret = scandir(sys_class_net, &direntries, good_interface,
2114
 
                  alphasort);
2115
 
    if(ret >= 1){
2116
 
      /* Add all found interfaces to interfaces list */
2117
 
      for(int i = 0; i < ret; ++i){
2118
 
        ret_errno = argz_add(&interfaces, &interfaces_size,
2119
 
                             direntries[i]->d_name);
2120
 
        if(ret_errno != 0){
2121
 
          perror_plus("argz_add");
2122
 
          continue;
2123
 
        }
2124
 
        if(debug){
2125
 
          fprintf_plus(stderr, "Will use interface \"%s\"\n",
2126
 
                       direntries[i]->d_name);
2127
 
        }
2128
 
      }
2129
 
      free(direntries);
2130
 
    } else {
2131
 
      free(direntries);
2132
 
      fprintf_plus(stderr, "Could not find a network interface\n");
2133
 
      exitcode = EXIT_FAILURE;
2134
 
      goto end;
2135
 
    }
2136
 
  }
2137
 
  
2138
 
  /* If we only got one interface, explicitly use only that one */
2139
 
  if(argz_count(interfaces, interfaces_size) == 1){
2140
 
    if(debug){
2141
 
      fprintf_plus(stderr, "Using only interface \"%s\"\n",
2142
 
                   interfaces);
2143
 
    }
2144
 
    if_index = (AvahiIfIndex)if_nametoindex(interfaces);
2145
 
  }
2146
 
  
2147
 
  /* Bring up interfaces which are down */
2148
 
  if(not (argz_count(interfaces, interfaces_size) == 1
2149
 
          and strcmp(interfaces, "none") == 0)){
2150
 
    char *interface = NULL;
2151
 
    while((interface = argz_next(interfaces, interfaces_size,
2152
 
                                 interface))){
2153
 
      bool interface_was_up = interface_is_up(interface);
2154
 
      ret = bring_up_interface(interface, delay);
2155
 
      if(not interface_was_up){
2156
 
        if(ret != 0){
2157
 
          errno = ret;
2158
 
          perror_plus("Failed to bring up interface");
2159
 
        } else {
2160
 
          ret_errno = argz_add(&interfaces_to_take_down,
2161
 
                               &interfaces_to_take_down_size,
2162
 
                               interface);
2163
 
        }
2164
 
      }
2165
 
    }
2166
 
    free(interfaces);
2167
 
    interfaces = NULL;
2168
 
    interfaces_size = 0;
2169
 
    if(debug and (interfaces_to_take_down == NULL)){
2170
 
      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");
2171
2035
    }
2172
2036
  }
2173
2037
  
2214
2078
    /* Connect directly, do not use Zeroconf */
2215
2079
    /* (Mainly meant for debugging) */
2216
2080
    char *address = strrchr(connect_to, ':');
2217
 
    
2218
2081
    if(address == NULL){
2219
2082
      fprintf_plus(stderr, "No colon in address\n");
2220
2083
      exitcode = EX_USAGE;
2295
2158
    /* Allocate a new server */
2296
2159
    mc.server = avahi_server_new(avahi_simple_poll_get
2297
2160
                                 (mc.simple_poll), &config, NULL,
2298
 
                                 NULL, &ret_errno);
 
2161
                                 NULL, &error);
2299
2162
    
2300
2163
    /* Free the Avahi configuration data */
2301
2164
    avahi_server_config_free(&config);
2304
2167
  /* Check if creating the Avahi server object succeeded */
2305
2168
  if(mc.server == NULL){
2306
2169
    fprintf_plus(stderr, "Failed to create Avahi server: %s\n",
2307
 
                 avahi_strerror(ret_errno));
 
2170
                 avahi_strerror(error));
2308
2171
    exitcode = EX_UNAVAILABLE;
2309
2172
    goto end;
2310
2173
  }
2378
2241
    }
2379
2242
  }
2380
2243
  
 
2244
  /* Run network hooks */
 
2245
  run_network_hooks("stop", interface, delay);
 
2246
  
2381
2247
  /* Re-raise priviliges */
2382
2248
  {
2383
 
    raise_privileges();
2384
 
    
2385
 
    /* Run network hooks */
2386
 
    run_network_hooks("stop", interfaces_hooks, delay);
2387
 
    
2388
 
    /* Take down the network interfaces which were brought up */
2389
 
    {
2390
 
      char *interface = NULL;
2391
 
      while((interface=argz_next(interfaces_to_take_down,
2392
 
                                 interfaces_to_take_down_size,
2393
 
                                 interface))){
2394
 
        ret_errno = take_down_interface(interface);
2395
 
        if(ret_errno != 0){
2396
 
          errno = ret_errno;
2397
 
          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");
2398
2265
        }
2399
2266
      }
2400
 
      if(debug and (interfaces_to_take_down == NULL)){
2401
 
        fprintf_plus(stderr, "No interfaces needed to be taken"
2402
 
                     " down\n");
 
2267
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
2268
      if(ret == -1){
 
2269
        perror_plus("close");
2403
2270
      }
2404
2271
    }
2405
 
    
2406
 
    lower_privileges_permanently();
2407
 
  }
2408
 
  
2409
 
  free(interfaces_to_take_down);
2410
 
  free(interfaces_hooks);
 
2272
  }
 
2273
  /* Lower privileges permanently */
 
2274
  errno = 0;
 
2275
  ret = setuid(uid);
 
2276
  if(ret == -1){
 
2277
    perror_plus("setuid");
 
2278
  }
2411
2279
  
2412
2280
  /* Removes the GPGME temp directory and all files inside */
2413
2281
  if(tempdir_created){