/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-01-15 20:27:28 UTC
  • Revision ID: teddy@recompile.se-20120115202728-185929ww2r84s8xg
* DBUS-API (se.recompile.Mandos.Client.LastCheckerStatus): New
                                                           property.
* mandos (Client.last_checker_status): Use -2 instead of None to match
                                       D-Bus property.  All users
                                       changed.
  (Client.checked_ok): Remove "timeout" argument and call
                       "self.bump_timeout()" instead.
  (Client.bump_timeout): New; separated out from "checked_ok".
  (ClientDBus.last_checker_status): Hook to a D-Bus property.
  (ClientDBus.LastCheckerStatus_dbus_property): New D-Bus property.
  (ClientHandler.handle): Call client.bump_timeout() instead of
                          client.checked_ok().

Show diffs side-by-side

added added

removed removed

Lines of Context:
41
41
 
42
42
#include <stdio.h>              /* fprintf(), stderr, fwrite(),
43
43
                                   stdout, ferror(), remove() */
44
 
#include <stdint.h>             /* uint16_t, uint32_t, intptr_t */
 
44
#include <stdint.h>             /* uint16_t, uint32_t */
45
45
#include <stddef.h>             /* NULL, size_t, ssize_t */
46
46
#include <stdlib.h>             /* free(), EXIT_SUCCESS, srand(),
47
47
                                   strtof(), abort() */
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{
827
821
    goto mandos_end;
828
822
  }
829
823
  
830
 
  /* This casting via intptr_t is to eliminate warning about casting
831
 
     an int to a pointer type.  This is exactly how the GnuTLS Guile
832
 
     function "set-session-transport-fd!" does it. */
833
 
  gnutls_transport_set_ptr(session,
834
 
                           (gnutls_transport_ptr_t)(intptr_t)tcp_sd);
 
824
  /* Spurious warning from -Wint-to-pointer-cast */
 
825
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
835
826
  
836
827
  if(quit_now){
837
828
    errno = EINTR;
1125
1116
 
1126
1117
bool get_flags(const char *ifname, struct ifreq *ifr){
1127
1118
  int ret;
1128
 
  error_t ret_errno;
1129
1119
  
1130
1120
  int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1131
1121
  if(s < 0){
1132
 
    ret_errno = errno;
1133
1122
    perror_plus("socket");
1134
 
    errno = ret_errno;
1135
1123
    return false;
1136
1124
  }
1137
1125
  strcpy(ifr->ifr_name, ifname);
1138
1126
  ret = ioctl(s, SIOCGIFFLAGS, ifr);
1139
1127
  if(ret == -1){
1140
1128
    if(debug){
1141
 
      ret_errno = errno;
1142
1129
      perror_plus("ioctl SIOCGIFFLAGS");
1143
 
      errno = ret_errno;
1144
1130
    }
1145
1131
    return false;
1146
1132
  }
1215
1201
}
1216
1202
 
1217
1203
/* 
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);
 
1204
 * This function determines if a directory entry in /sys/class/net
 
1205
 * corresponds to an acceptable network device which is up.
 
1206
 * (This function is passed to scandir(3) as a filter function.)
 
1207
 */
 
1208
int up_interface(const struct dirent *if_entry){
 
1209
  if(if_entry->d_name[0] == '.'){
 
1210
    return 0;
 
1211
  }
 
1212
  
 
1213
  struct ifreq ifr;
 
1214
  if(not get_flags(if_entry->d_name, &ifr)){
 
1215
    if(debug){
 
1216
      fprintf_plus(stderr, "Failed to get flags for interface "
 
1217
                   "\"%s\"\n", if_entry->d_name);
 
1218
    }
 
1219
    return 0;
 
1220
  }
 
1221
  
 
1222
  /* Reject down interfaces */
 
1223
  if(not (ifr.ifr_flags & IFF_UP)){
 
1224
    if(debug){
 
1225
      fprintf_plus(stderr, "Rejecting down interface \"%s\"\n",
 
1226
                   if_entry->d_name);
 
1227
    }
 
1228
    return 0;
 
1229
  }
 
1230
  
 
1231
  /* Reject non-running interfaces */
 
1232
  if(not (ifr.ifr_flags & IFF_RUNNING)){
 
1233
    if(debug){
 
1234
      fprintf_plus(stderr, "Rejecting non-running interface \"%s\"\n",
 
1235
                   if_entry->d_name);
 
1236
    }
 
1237
    return 0;
 
1238
  }
 
1239
  
 
1240
  if(not good_flags(if_entry->d_name, &ifr)){
 
1241
    return 0;
 
1242
  }
 
1243
  return 1;
1247
1244
}
1248
1245
 
1249
1246
int notdotentries(const struct dirent *direntry){
1390
1387
  }
1391
1388
}
1392
1389
 
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
1390
bool run_network_hooks(const char *mode, const char *interface,
1446
1391
                       const float delay){
1447
1392
  struct dirent **direntries;
1469
1414
      if(hook_pid == 0){
1470
1415
        /* Child */
1471
1416
        /* Raise privileges */
1472
 
        raise_privileges_permanently();
 
1417
        errno = 0;
 
1418
        ret = seteuid(0);
 
1419
        if(ret == -1){
 
1420
          perror_plus("seteuid");
 
1421
        }
 
1422
        /* Raise privileges even more */
 
1423
        errno = 0;
 
1424
        ret = setuid(0);
 
1425
        if(ret == -1){
 
1426
          perror_plus("setuid");
 
1427
        }
1473
1428
        /* Set group */
1474
1429
        errno = 0;
1475
1430
        ret = setgid(0);
1568
1523
  return true;
1569
1524
}
1570
1525
 
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
1526
int main(int argc, char *argv[]){
1744
1527
  AvahiSServiceBrowser *sb = NULL;
1745
 
  error_t ret_errno;
 
1528
  int error;
1746
1529
  int ret;
1747
1530
  intmax_t tmpmax;
1748
1531
  char *tmp;
1749
1532
  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;
 
1533
  const char *interface = "";
 
1534
  struct ifreq network;
 
1535
  int sd = -1;
 
1536
  bool take_down_interface = false;
 
1537
  uid_t uid;
 
1538
  gid_t gid;
1754
1539
  char tempdir[] = "/tmp/mandosXXXXXX";
1755
1540
  bool tempdir_created = false;
1756
1541
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1757
1542
  const char *seckey = PATHDIR "/" SECKEY;
1758
1543
  const char *pubkey = PATHDIR "/" PUBKEY;
1759
 
  char *interfaces_hooks = NULL;
1760
 
  size_t interfaces_hooks_size = 0;
1761
1544
  
1762
1545
  bool gnutls_initialized = false;
1763
1546
  bool gpgme_initialized = false;
1854
1637
        connect_to = arg;
1855
1638
        break;
1856
1639
      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
 
        }
 
1640
        interface = arg;
1862
1641
        break;
1863
1642
      case 's':                 /* --seckey */
1864
1643
        seckey = arg;
1942
1721
       <http://bugs.debian.org/633582> */
1943
1722
    
1944
1723
    /* Re-raise priviliges */
1945
 
    if(raise_privileges() == 0){
 
1724
    errno = 0;
 
1725
    ret = seteuid(0);
 
1726
    if(ret == -1){
 
1727
      perror_plus("seteuid");
 
1728
    } else {
1946
1729
      struct stat st;
1947
1730
      
1948
1731
      if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1996
1779
    }
1997
1780
  }
1998
1781
  
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
1782
  /* 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
 
    }
 
1783
  if(not run_network_hooks("start", interface, delay)){
 
1784
    goto end;
2028
1785
  }
2029
1786
  
2030
1787
  if(not debug){
2031
1788
    avahi_set_log_function(empty_log);
2032
1789
  }
2033
1790
  
 
1791
  if(interface[0] == '\0'){
 
1792
    struct dirent **direntries;
 
1793
    /* First look for interfaces that are up */
 
1794
    ret = scandir(sys_class_net, &direntries, up_interface,
 
1795
                  alphasort);
 
1796
    if(ret == 0){
 
1797
      /* No up interfaces, look for any good interfaces */
 
1798
      free(direntries);
 
1799
      ret = scandir(sys_class_net, &direntries, good_interface,
 
1800
                    alphasort);
 
1801
    }
 
1802
    if(ret >= 1){
 
1803
      /* Pick the first interface returned */
 
1804
      interface = strdup(direntries[0]->d_name);
 
1805
      if(debug){
 
1806
        fprintf_plus(stderr, "Using interface \"%s\"\n", interface);
 
1807
      }
 
1808
      if(interface == NULL){
 
1809
        perror_plus("malloc");
 
1810
        free(direntries);
 
1811
        exitcode = EXIT_FAILURE;
 
1812
        goto end;
 
1813
      }
 
1814
      free(direntries);
 
1815
    } else {
 
1816
      free(direntries);
 
1817
      fprintf_plus(stderr, "Could not find a network interface\n");
 
1818
      exitcode = EXIT_FAILURE;
 
1819
      goto end;
 
1820
    }
 
1821
  }
 
1822
  
2034
1823
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
2035
1824
     from the signal handler */
2036
1825
  /* Initialize the pseudo-RNG for Avahi */
2106
1895
    }
2107
1896
  }
2108
1897
  
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");
 
1898
  /* If the interface is down, bring it up */
 
1899
  if(strcmp(interface, "none") != 0){
 
1900
    if_index = (AvahiIfIndex) if_nametoindex(interface);
 
1901
    if(if_index == 0){
 
1902
      fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
 
1903
      exitcode = EX_UNAVAILABLE;
 
1904
      goto end;
 
1905
    }
 
1906
    
 
1907
    if(quit_now){
 
1908
      goto end;
 
1909
    }
 
1910
    
 
1911
    /* Re-raise priviliges */
 
1912
    errno = 0;
 
1913
    ret = seteuid(0);
 
1914
    if(ret == -1){
 
1915
      perror_plus("seteuid");
 
1916
    }
 
1917
    
 
1918
#ifdef __linux__
 
1919
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
 
1920
       messages about the network interface to mess up the prompt */
 
1921
    ret = klogctl(8, NULL, 5);
 
1922
    bool restore_loglevel = true;
 
1923
    if(ret == -1){
 
1924
      restore_loglevel = false;
 
1925
      perror_plus("klogctl");
 
1926
    }
 
1927
#endif  /* __linux__ */
 
1928
    
 
1929
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
1930
    if(sd < 0){
 
1931
      perror_plus("socket");
 
1932
      exitcode = EX_OSERR;
 
1933
#ifdef __linux__
 
1934
      if(restore_loglevel){
 
1935
        ret = klogctl(7, NULL, 0);
 
1936
        if(ret == -1){
 
1937
          perror_plus("klogctl");
 
1938
        }
 
1939
      }
 
1940
#endif  /* __linux__ */
 
1941
      /* Lower privileges */
 
1942
      errno = 0;
 
1943
      ret = seteuid(uid);
 
1944
      if(ret == -1){
 
1945
        perror_plus("seteuid");
 
1946
      }
 
1947
      goto end;
 
1948
    }
 
1949
    strcpy(network.ifr_name, interface);
 
1950
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
1951
    if(ret == -1){
 
1952
      perror_plus("ioctl SIOCGIFFLAGS");
 
1953
#ifdef __linux__
 
1954
      if(restore_loglevel){
 
1955
        ret = klogctl(7, NULL, 0);
 
1956
        if(ret == -1){
 
1957
          perror_plus("klogctl");
 
1958
        }
 
1959
      }
 
1960
#endif  /* __linux__ */
 
1961
      exitcode = EX_OSERR;
 
1962
      /* Lower privileges */
 
1963
      errno = 0;
 
1964
      ret = seteuid(uid);
 
1965
      if(ret == -1){
 
1966
        perror_plus("seteuid");
 
1967
      }
 
1968
      goto end;
 
1969
    }
 
1970
    if((network.ifr_flags & IFF_UP) == 0){
 
1971
      network.ifr_flags |= IFF_UP;
 
1972
      take_down_interface = true;
 
1973
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
1974
      if(ret == -1){
 
1975
        take_down_interface = false;
 
1976
        perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
 
1977
        exitcode = EX_OSERR;
 
1978
#ifdef __linux__
 
1979
        if(restore_loglevel){
 
1980
          ret = klogctl(7, NULL, 0);
 
1981
          if(ret == -1){
 
1982
            perror_plus("klogctl");
 
1983
          }
 
1984
        }
 
1985
#endif  /* __linux__ */
 
1986
        /* Lower privileges */
 
1987
        errno = 0;
 
1988
        ret = seteuid(uid);
 
1989
        if(ret == -1){
 
1990
          perror_plus("seteuid");
 
1991
        }
 
1992
        goto end;
 
1993
      }
 
1994
    }
 
1995
    /* Sleep checking until interface is running.
 
1996
       Check every 0.25s, up to total time of delay */
 
1997
    for(int i=0; i < delay * 4; i++){
 
1998
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
1999
      if(ret == -1){
 
2000
        perror_plus("ioctl SIOCGIFFLAGS");
 
2001
      } else if(network.ifr_flags & IFF_RUNNING){
 
2002
        break;
 
2003
      }
 
2004
      struct timespec sleeptime = { .tv_nsec = 250000000 };
 
2005
      ret = nanosleep(&sleeptime, NULL);
 
2006
      if(ret == -1 and errno != EINTR){
 
2007
        perror_plus("nanosleep");
 
2008
      }
 
2009
    }
 
2010
    if(not take_down_interface){
 
2011
      /* We won't need the socket anymore */
 
2012
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
2013
      if(ret == -1){
 
2014
        perror_plus("close");
 
2015
      }
 
2016
    }
 
2017
#ifdef __linux__
 
2018
    if(restore_loglevel){
 
2019
      /* Restores kernel loglevel to default */
 
2020
      ret = klogctl(7, NULL, 0);
 
2021
      if(ret == -1){
 
2022
        perror_plus("klogctl");
 
2023
      }
 
2024
    }
 
2025
#endif  /* __linux__ */
 
2026
    /* Lower privileges */
 
2027
    errno = 0;
 
2028
    /* Lower privileges */
 
2029
    ret = seteuid(uid);
 
2030
    if(ret == -1){
 
2031
      perror_plus("seteuid");
2171
2032
    }
2172
2033
  }
2173
2034
  
2214
2075
    /* Connect directly, do not use Zeroconf */
2215
2076
    /* (Mainly meant for debugging) */
2216
2077
    char *address = strrchr(connect_to, ':');
2217
 
    
2218
2078
    if(address == NULL){
2219
2079
      fprintf_plus(stderr, "No colon in address\n");
2220
2080
      exitcode = EX_USAGE;
2295
2155
    /* Allocate a new server */
2296
2156
    mc.server = avahi_server_new(avahi_simple_poll_get
2297
2157
                                 (mc.simple_poll), &config, NULL,
2298
 
                                 NULL, &ret_errno);
 
2158
                                 NULL, &error);
2299
2159
    
2300
2160
    /* Free the Avahi configuration data */
2301
2161
    avahi_server_config_free(&config);
2304
2164
  /* Check if creating the Avahi server object succeeded */
2305
2165
  if(mc.server == NULL){
2306
2166
    fprintf_plus(stderr, "Failed to create Avahi server: %s\n",
2307
 
                 avahi_strerror(ret_errno));
 
2167
                 avahi_strerror(error));
2308
2168
    exitcode = EX_UNAVAILABLE;
2309
2169
    goto end;
2310
2170
  }
2378
2238
    }
2379
2239
  }
2380
2240
  
 
2241
  /* Run network hooks */
 
2242
  run_network_hooks("stop", interface, delay);
 
2243
  
2381
2244
  /* Re-raise priviliges */
2382
2245
  {
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");
 
2246
    errno = 0;
 
2247
    ret = seteuid(0);
 
2248
    if(ret == -1){
 
2249
      perror_plus("seteuid");
 
2250
    }
 
2251
    
 
2252
    /* Take down the network interface */
 
2253
    if(take_down_interface and geteuid() == 0){
 
2254
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
2255
      if(ret == -1){
 
2256
        perror_plus("ioctl SIOCGIFFLAGS");
 
2257
      } else if(network.ifr_flags & IFF_UP){
 
2258
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
 
2259
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
2260
        if(ret == -1){
 
2261
          perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
2398
2262
        }
2399
2263
      }
2400
 
      if(debug and (interfaces_to_take_down == NULL)){
2401
 
        fprintf_plus(stderr, "No interfaces needed to be taken"
2402
 
                     " down\n");
 
2264
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
2265
      if(ret == -1){
 
2266
        perror_plus("close");
2403
2267
      }
2404
2268
    }
2405
 
    
2406
 
    lower_privileges_permanently();
2407
 
  }
2408
 
  
2409
 
  free(interfaces_to_take_down);
2410
 
  free(interfaces_hooks);
 
2269
  }
 
2270
  /* Lower privileges permanently */
 
2271
  errno = 0;
 
2272
  ret = setuid(uid);
 
2273
  if(ret == -1){
 
2274
    perror_plus("setuid");
 
2275
  }
2411
2276
  
2412
2277
  /* Removes the GPGME temp directory and all files inside */
2413
2278
  if(tempdir_created){