/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-06-06 19:08:17 UTC
  • mto: This revision was merged to the branch mainline in revision 596.
  • Revision ID: teddy@recompile.se-20120606190817-c2x5o98vzn07xufd
* plugins.d/mandos-client.c: Refactoring.
  (uid, gid): New global variables.
  (raise_privileges, raise_privileges_permanently, lower_privileges,
  bring_up_interface): New functions.
  (main): Changed to use new functions.

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() */
1125
1121
 
1126
1122
bool get_flags(const char *ifname, struct ifreq *ifr){
1127
1123
  int ret;
1128
 
  error_t ret_errno;
1129
1124
  
1130
1125
  int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1131
1126
  if(s < 0){
1132
 
    ret_errno = errno;
1133
1127
    perror_plus("socket");
1134
 
    errno = ret_errno;
1135
1128
    return false;
1136
1129
  }
1137
1130
  strcpy(ifr->ifr_name, ifname);
1138
1131
  ret = ioctl(s, SIOCGIFFLAGS, ifr);
1139
1132
  if(ret == -1){
1140
1133
    if(debug){
1141
 
      ret_errno = errno;
1142
1134
      perror_plus("ioctl SIOCGIFFLAGS");
1143
 
      errno = ret_errno;
1144
1135
    }
1145
1136
    return false;
1146
1137
  }
1215
1206
}
1216
1207
 
1217
1208
/* 
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);
 
1209
 * This function determines if a directory entry in /sys/class/net
 
1210
 * corresponds to an acceptable network device which is up.
 
1211
 * (This function is passed to scandir(3) as a filter function.)
 
1212
 */
 
1213
int up_interface(const struct dirent *if_entry){
 
1214
  if(if_entry->d_name[0] == '.'){
 
1215
    return 0;
 
1216
  }
 
1217
  
 
1218
  struct ifreq ifr;
 
1219
  if(not get_flags(if_entry->d_name, &ifr)){
 
1220
    if(debug){
 
1221
      fprintf_plus(stderr, "Failed to get flags for interface "
 
1222
                   "\"%s\"\n", if_entry->d_name);
 
1223
    }
 
1224
    return 0;
 
1225
  }
 
1226
  
 
1227
  /* Reject down interfaces */
 
1228
  if(not (ifr.ifr_flags & IFF_UP)){
 
1229
    if(debug){
 
1230
      fprintf_plus(stderr, "Rejecting down interface \"%s\"\n",
 
1231
                   if_entry->d_name);
 
1232
    }
 
1233
    return 0;
 
1234
  }
 
1235
  
 
1236
  /* Reject non-running interfaces */
 
1237
  if(not (ifr.ifr_flags & IFF_RUNNING)){
 
1238
    if(debug){
 
1239
      fprintf_plus(stderr, "Rejecting non-running interface \"%s\"\n",
 
1240
                   if_entry->d_name);
 
1241
    }
 
1242
    return 0;
 
1243
  }
 
1244
  
 
1245
  if(not good_flags(if_entry->d_name, &ifr)){
 
1246
    return 0;
 
1247
  }
 
1248
  return 1;
1247
1249
}
1248
1250
 
1249
1251
int notdotentries(const struct dirent *direntry){
1391
1393
}
1392
1394
 
1393
1395
/* 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;
 
1396
int raise_privileges(void){
 
1397
  int old_errno = errno;
 
1398
  int ret_errno = 0;
 
1399
  errno = 0;
1397
1400
  if(seteuid(0) == -1){
1398
 
    ret_errno = errno;
1399
1401
    perror_plus("seteuid");
1400
1402
  }
 
1403
  ret_errno = errno;
1401
1404
  errno = old_errno;
1402
1405
  return ret_errno;
1403
1406
}
1404
1407
 
1405
1408
/* 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
int raise_privileges_permanently(void){
 
1410
  int old_errno = errno;
 
1411
  int ret_errno = raise_privileges();
1409
1412
  if(ret_errno != 0){
1410
1413
    errno = old_errno;
1411
1414
    return ret_errno;
1412
1415
  }
 
1416
  errno = 0;
1413
1417
  if(setuid(0) == -1){
1414
 
    ret_errno = errno;
1415
1418
    perror_plus("seteuid");
1416
1419
  }
 
1420
  ret_errno = errno;
1417
1421
  errno = old_errno;
1418
1422
  return ret_errno;
1419
1423
}
1420
1424
 
1421
1425
/* 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;
 
1426
int lower_privileges(void){
 
1427
  int old_errno = errno;
 
1428
  int ret_errno = 0;
 
1429
  errno = 0;
1425
1430
  if(seteuid(uid) == -1){
1426
 
    ret_errno = errno;
1427
1431
    perror_plus("seteuid");
1428
1432
  }
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
 
  }
 
1433
  ret_errno = errno;
1441
1434
  errno = old_errno;
1442
1435
  return ret_errno;
1443
1436
}
1568
1561
  return true;
1569
1562
}
1570
1563
 
1571
 
error_t bring_up_interface(const char *const interface,
1572
 
                           const float delay){
 
1564
int bring_up_interface(const char * const interface, const float delay){
1573
1565
  int sd = -1;
1574
 
  error_t old_errno = errno;
1575
 
  error_t ret_errno = 0;
1576
 
  int ret, ret_setflags;
 
1566
  int ret;
1577
1567
  struct ifreq network;
1578
 
  unsigned int if_index = if_nametoindex(interface);
 
1568
  AvahiIfIndex if_index = (AvahiIfIndex)if_nametoindex(interface);
1579
1569
  if(if_index == 0){
1580
1570
    fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1581
 
    errno = old_errno;
1582
 
    return ENXIO;
 
1571
    return EX_UNAVAILABLE;
1583
1572
  }
1584
1573
  
1585
1574
  if(quit_now){
1586
 
    errno = old_errno;
1587
1575
    return EINTR;
1588
1576
  }
1589
1577
  
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
 
    }
 
1578
  /* Re-raise priviliges */
 
1579
  raise_privileges();
 
1580
    
 
1581
#ifdef __linux__
 
1582
  /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
 
1583
     messages about the network interface to mess up the prompt */
 
1584
  ret = klogctl(8, NULL, 5);
 
1585
  bool restore_loglevel = true;
 
1586
  if(ret == -1){
 
1587
    restore_loglevel = false;
 
1588
    perror_plus("klogctl");
 
1589
  }
 
1590
#endif  /* __linux__ */
 
1591
    
 
1592
  sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
1593
  if(sd < 0){
 
1594
    perror_plus("socket");
 
1595
#ifdef __linux__
 
1596
    if(restore_loglevel){
 
1597
      ret = klogctl(7, NULL, 0);
 
1598
      if(ret == -1){
 
1599
        perror_plus("klogctl");
 
1600
      }
 
1601
    }
 
1602
#endif  /* __linux__ */
 
1603
    /* Lower privileges */
 
1604
    lower_privileges();
 
1605
    return EX_OSERR;
 
1606
;
 
1607
  }
 
1608
  strcpy(network.ifr_name, interface);
 
1609
  ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
1610
  if(ret == -1){
 
1611
    perror_plus("ioctl SIOCGIFFLAGS");
 
1612
#ifdef __linux__
 
1613
    if(restore_loglevel){
 
1614
      ret = klogctl(7, NULL, 0);
 
1615
      if(ret == -1){
 
1616
        perror_plus("klogctl");
 
1617
      }
 
1618
    }
 
1619
#endif  /* __linux__ */
 
1620
    /* Lower privileges */
 
1621
    lower_privileges();
 
1622
    return EX_OSERR;
 
1623
  }
 
1624
  if((network.ifr_flags & IFF_UP) == 0){
1597
1625
    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));
 
1626
    ret = ioctl(sd, SIOCSIFFLAGS, &network);
1647
1627
    if(ret == -1){
1648
 
      perror_plus("close");
1649
 
    }
1650
 
    
1651
 
    if(ret_setflags == -1){
1652
 
      errno = ret_errno;
1653
1628
      perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1654
 
      errno = old_errno;
1655
 
      return ret_errno;
 
1629
#ifdef __linux__
 
1630
      if(restore_loglevel){
 
1631
        ret = klogctl(7, NULL, 0);
 
1632
        if(ret == -1){
 
1633
          perror_plus("klogctl");
 
1634
        }
 
1635
      }
 
1636
#endif  /* __linux__ */
 
1637
        /* Lower privileges */
 
1638
      lower_privileges();
 
1639
      return EX_OSERR;
1656
1640
    }
1657
 
  } else if(debug){
1658
 
    fprintf_plus(stderr, "Interface \"%s\" is already up; good\n",
1659
 
                 interface);
1660
1641
  }
1661
 
  
1662
1642
  /* Sleep checking until interface is running.
1663
1643
     Check every 0.25s, up to total time of delay */
1664
1644
  for(int i=0; i < delay * 4; i++){
1665
 
    if(interface_is_running(interface)){
 
1645
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
1646
    if(ret == -1){
 
1647
      perror_plus("ioctl SIOCGIFFLAGS");
 
1648
    } else if(network.ifr_flags & IFF_RUNNING){
1666
1649
      break;
1667
1650
    }
1668
1651
    struct timespec sleeptime = { .tv_nsec = 250000000 };
1671
1654
      perror_plus("nanosleep");
1672
1655
    }
1673
1656
  }
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;
 
1657
  /* Close the socket */
 
1658
  ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1659
  if(ret == -1){
 
1660
    perror_plus("close");
1690
1661
  }
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));
 
1662
#ifdef __linux__
 
1663
  if(restore_loglevel){
 
1664
    /* Restores kernel loglevel to default */
 
1665
    ret = klogctl(7, NULL, 0);
1724
1666
    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);
 
1667
      perror_plus("klogctl");
 
1668
    }
1737
1669
  }
1738
 
  
1739
 
  errno = old_errno;
1740
 
  return 0;
 
1670
#endif  /* __linux__ */
 
1671
  /* Lower privileges */
 
1672
  lower_privileges();
 
1673
  return errno;
1741
1674
}
1742
1675
 
1743
1676
int main(int argc, char *argv[]){
1744
1677
  AvahiSServiceBrowser *sb = NULL;
1745
 
  error_t ret_errno;
 
1678
  int error;
1746
1679
  int ret;
1747
1680
  intmax_t tmpmax;
1748
1681
  char *tmp;
1749
1682
  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;
 
1683
  const char *interface = "";
 
1684
  struct ifreq network;
 
1685
  int sd = -1;
 
1686
  bool take_down_interface = false;
1754
1687
  char tempdir[] = "/tmp/mandosXXXXXX";
1755
1688
  bool tempdir_created = false;
1756
1689
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1757
1690
  const char *seckey = PATHDIR "/" SECKEY;
1758
1691
  const char *pubkey = PATHDIR "/" PUBKEY;
1759
 
  char *interfaces_hooks = NULL;
1760
 
  size_t interfaces_hooks_size = 0;
1761
1692
  
1762
1693
  bool gnutls_initialized = false;
1763
1694
  bool gpgme_initialized = false;
1854
1785
        connect_to = arg;
1855
1786
        break;
1856
1787
      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
 
        }
 
1788
        interface = arg;
1862
1789
        break;
1863
1790
      case 's':                 /* --seckey */
1864
1791
        seckey = arg;
1996
1923
    }
1997
1924
  }
1998
1925
  
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
1926
  /* 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
 
    }
 
1927
  if(not run_network_hooks("start", interface, delay)){
 
1928
    goto end;
2028
1929
  }
2029
1930
  
2030
1931
  if(not debug){
2031
1932
    avahi_set_log_function(empty_log);
2032
1933
  }
2033
1934
  
 
1935
  if(interface[0] == '\0'){
 
1936
    struct dirent **direntries;
 
1937
    /* First look for interfaces that are up */
 
1938
    ret = scandir(sys_class_net, &direntries, up_interface,
 
1939
                  alphasort);
 
1940
    if(ret == 0){
 
1941
      /* No up interfaces, look for any good interfaces */
 
1942
      free(direntries);
 
1943
      ret = scandir(sys_class_net, &direntries, good_interface,
 
1944
                    alphasort);
 
1945
    }
 
1946
    if(ret >= 1){
 
1947
      /* Pick the first interface returned */
 
1948
      interface = strdup(direntries[0]->d_name);
 
1949
      if(debug){
 
1950
        fprintf_plus(stderr, "Using interface \"%s\"\n", interface);
 
1951
      }
 
1952
      if(interface == NULL){
 
1953
        perror_plus("malloc");
 
1954
        free(direntries);
 
1955
        exitcode = EXIT_FAILURE;
 
1956
        goto end;
 
1957
      }
 
1958
      free(direntries);
 
1959
    } else {
 
1960
      free(direntries);
 
1961
      fprintf_plus(stderr, "Could not find a network interface\n");
 
1962
      exitcode = EXIT_FAILURE;
 
1963
      goto end;
 
1964
    }
 
1965
  }
 
1966
  
2034
1967
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
2035
1968
     from the signal handler */
2036
1969
  /* Initialize the pseudo-RNG for Avahi */
2106
2039
    }
2107
2040
  }
2108
2041
  
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");
 
2042
  /* If the interface is down, bring it up */
 
2043
  if((interface[0] != '\0') and (strcmp(interface, "none") != 0)){
 
2044
    ret = bring_up_interface(interface, delay);
 
2045
    if(ret){
 
2046
      errno = ret;
 
2047
      perror_plus("Failed to bring up interface");
2171
2048
    }
2172
2049
  }
2173
2050
  
2295
2172
    /* Allocate a new server */
2296
2173
    mc.server = avahi_server_new(avahi_simple_poll_get
2297
2174
                                 (mc.simple_poll), &config, NULL,
2298
 
                                 NULL, &ret_errno);
 
2175
                                 NULL, &error);
2299
2176
    
2300
2177
    /* Free the Avahi configuration data */
2301
2178
    avahi_server_config_free(&config);
2304
2181
  /* Check if creating the Avahi server object succeeded */
2305
2182
  if(mc.server == NULL){
2306
2183
    fprintf_plus(stderr, "Failed to create Avahi server: %s\n",
2307
 
                 avahi_strerror(ret_errno));
 
2184
                 avahi_strerror(error));
2308
2185
    exitcode = EX_UNAVAILABLE;
2309
2186
    goto end;
2310
2187
  }
2378
2255
    }
2379
2256
  }
2380
2257
  
 
2258
  /* Run network hooks */
 
2259
  run_network_hooks("stop", interface, delay);
 
2260
  
2381
2261
  /* Re-raise priviliges */
2382
2262
  {
2383
2263
    raise_privileges();
2384
2264
    
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");
 
2265
    /* Take down the network interface */
 
2266
    if(take_down_interface and geteuid() == 0){
 
2267
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
2268
      if(ret == -1){
 
2269
        perror_plus("ioctl SIOCGIFFLAGS");
 
2270
      } else if(network.ifr_flags & IFF_UP){
 
2271
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
 
2272
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
2273
        if(ret == -1){
 
2274
          perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
2398
2275
        }
2399
2276
      }
2400
 
      if(debug and (interfaces_to_take_down == NULL)){
2401
 
        fprintf_plus(stderr, "No interfaces needed to be taken"
2402
 
                     " down\n");
 
2277
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
2278
      if(ret == -1){
 
2279
        perror_plus("close");
2403
2280
      }
2404
2281
    }
2405
 
    
2406
 
    lower_privileges_permanently();
2407
 
  }
2408
 
  
2409
 
  free(interfaces_to_take_down);
2410
 
  free(interfaces_hooks);
 
2282
  }
 
2283
  /* Lower privileges permanently */
 
2284
  errno = 0;
 
2285
  ret = setuid(uid);
 
2286
  if(ret == -1){
 
2287
    perror_plus("setuid");
 
2288
  }
2411
2289
  
2412
2290
  /* Removes the GPGME temp directory and all files inside */
2413
2291
  if(tempdir_created){