/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-15 17:18:34 UTC
  • Revision ID: teddy@recompile.se-20120615171834-gzzgknth003j903u
* plugins.d/mandos-client.c (main): Bug fix: Set DEVICE environment
                                    variable correctly for network
                                    hooks.  Also, don't call
                                    run_network_hooks() with NULL
                                    value.

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 */
 
44
#include <stdint.h>             /* uint16_t, uint32_t, intptr_t */
45
45
#include <stddef.h>             /* NULL, size_t, ssize_t */
46
46
#include <stdlib.h>             /* free(), EXIT_SUCCESS, srand(),
47
47
                                   strtof(), abort() */
61
61
                                 */
62
62
#include <inttypes.h>           /* PRIu16, PRIdMAX, intmax_t,
63
63
                                   strtoimax() */
64
 
#include <assert.h>             /* assert() */
65
64
#include <errno.h>              /* perror(), errno,
66
65
                                   program_invocation_short_name */
67
66
#include <time.h>               /* nanosleep(), time(), sleep() */
88
87
#include <sys/wait.h>           /* waitpid(), WIFEXITED(),
89
88
                                   WEXITSTATUS(), WTERMSIG() */
90
89
#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() */
91
94
 
92
95
#ifdef __linux__
93
96
#include <sys/klog.h>           /* klogctl() */
135
138
static const char sys_class_net[] = "/sys/class/net";
136
139
char *connect_to = NULL;
137
140
const char *hookdir = HOOKDIR;
 
141
uid_t uid = 65534;
 
142
gid_t gid = 65534;
138
143
 
139
144
/* Doubly linked list that need to be circularly linked when used */
140
145
typedef struct server{
141
146
  const char *ip;
142
 
  uint16_t port;
 
147
  in_port_t port;
143
148
  AvahiIfIndex if_index;
144
149
  int af;
145
150
  struct timespec last_seen;
205
210
}
206
211
 
207
212
/* Add server to set of servers to retry periodically */
208
 
bool add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
 
213
bool add_server(const char *ip, in_port_t port, AvahiIfIndex if_index,
209
214
                int af){
210
215
  int ret;
211
216
  server *new_server = malloc(sizeof(server));
465
470
}
466
471
 
467
472
static const char * safer_gnutls_strerror(int value){
468
 
  const char *ret = gnutls_strerror(value); /* Spurious warning from
469
 
                                               -Wunreachable-code */
 
473
  const char *ret = gnutls_strerror(value);
470
474
  if(ret == NULL)
471
475
    ret = "(unknown)";
472
476
  return ret;
617
621
                      __attribute__((unused)) const char *txt){}
618
622
 
619
623
/* Called when a Mandos server is found */
620
 
static int start_mandos_communication(const char *ip, uint16_t port,
 
624
static int start_mandos_communication(const char *ip, in_port_t port,
621
625
                                      AvahiIfIndex if_index,
622
626
                                      int af){
623
627
  int ret, tcp_sd = -1;
662
666
  
663
667
  if(debug){
664
668
    fprintf_plus(stderr, "Setting up a TCP connection to %s, port %"
665
 
                 PRIu16 "\n", ip, port);
 
669
                 PRIuMAX "\n", ip, (uintmax_t)port);
666
670
  }
667
671
  
668
672
  tcp_sd = socket(pf, SOCK_STREAM, 0);
699
703
    goto mandos_end;
700
704
  }
701
705
  if(af == AF_INET6){
702
 
    to.in6.sin6_port = htons(port); /* Spurious warnings from
703
 
                                       -Wconversion and
704
 
                                       -Wunreachable-code */
705
 
    
 
706
    to.in6.sin6_port = htons(port);    
706
707
    if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
707
708
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
708
709
                                -Wunreachable-code*/
732
733
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
733
734
        perror_plus("if_indextoname");
734
735
      } else {
735
 
        fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16
736
 
                     "\n", ip, interface, port);
 
736
        fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIuMAX
 
737
                     "\n", ip, interface, (uintmax_t)port);
737
738
      }
738
739
    } else {
739
 
      fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n",
740
 
                   ip, port);
 
740
      fprintf_plus(stderr, "Connection to: %s, port %" PRIuMAX "\n",
 
741
                   ip, (uintmax_t)port);
741
742
    }
742
743
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
743
744
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
821
822
    goto mandos_end;
822
823
  }
823
824
  
824
 
  /* Spurious warning from -Wint-to-pointer-cast */
825
 
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
 
825
  /* This casting via intptr_t is to eliminate warning about casting
 
826
     an int to a pointer type.  This is exactly how the GnuTLS Guile
 
827
     function "set-session-transport-fd!" does it. */
 
828
  gnutls_transport_set_ptr(session,
 
829
                           (gnutls_transport_ptr_t)(intptr_t)tcp_sd);
826
830
  
827
831
  if(quit_now){
828
832
    errno = EINTR;
1001
1005
                             AVAHI_GCC_UNUSED AvahiLookupResultFlags
1002
1006
                             flags,
1003
1007
                             AVAHI_GCC_UNUSED void* userdata){
1004
 
  assert(r);
 
1008
  if(r == NULL){
 
1009
    return;
 
1010
  }
1005
1011
  
1006
1012
  /* Called whenever a service has been resolved successfully or
1007
1013
     timed out */
1028
1034
                     PRIdMAX ") on port %" PRIu16 "\n", name,
1029
1035
                     host_name, ip, (intmax_t)interface, port);
1030
1036
      }
1031
 
      int ret = start_mandos_communication(ip, port, interface,
 
1037
      int ret = start_mandos_communication(ip, (in_port_t)port,
 
1038
                                           interface,
1032
1039
                                           avahi_proto_to_af(proto));
1033
1040
      if(ret == 0){
1034
1041
        avahi_simple_poll_quit(mc.simple_poll);
1035
1042
      } else {
1036
 
        if(not add_server(ip, port, interface,
 
1043
        if(not add_server(ip, (in_port_t)port, interface,
1037
1044
                          avahi_proto_to_af(proto))){
1038
1045
          fprintf_plus(stderr, "Failed to add server \"%s\" to server"
1039
1046
                       " list\n", name);
1054
1061
                            AVAHI_GCC_UNUSED AvahiLookupResultFlags
1055
1062
                            flags,
1056
1063
                            AVAHI_GCC_UNUSED void* userdata){
1057
 
  assert(b);
 
1064
  if(b == NULL){
 
1065
    return;
 
1066
  }
1058
1067
  
1059
1068
  /* Called whenever a new services becomes available on the LAN or
1060
1069
     is removed from the LAN */
1116
1125
 
1117
1126
bool get_flags(const char *ifname, struct ifreq *ifr){
1118
1127
  int ret;
 
1128
  error_t ret_errno;
1119
1129
  
1120
1130
  int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1121
1131
  if(s < 0){
 
1132
    ret_errno = errno;
1122
1133
    perror_plus("socket");
 
1134
    errno = ret_errno;
1123
1135
    return false;
1124
1136
  }
1125
1137
  strcpy(ifr->ifr_name, ifname);
1126
1138
  ret = ioctl(s, SIOCGIFFLAGS, ifr);
1127
1139
  if(ret == -1){
1128
1140
    if(debug){
 
1141
      ret_errno = errno;
1129
1142
      perror_plus("ioctl SIOCGIFFLAGS");
 
1143
      errno = ret_errno;
1130
1144
    }
1131
1145
    return false;
1132
1146
  }
1201
1215
}
1202
1216
 
1203
1217
/* 
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;
 
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);
1244
1247
}
1245
1248
 
1246
1249
int notdotentries(const struct dirent *direntry){
1387
1390
  }
1388
1391
}
1389
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
 
1390
1445
bool run_network_hooks(const char *mode, const char *interface,
1391
1446
                       const float delay){
1392
1447
  struct dirent **direntries;
1395
1450
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1396
1451
                         alphasort);
1397
1452
  if(numhooks == -1){
1398
 
    perror_plus("scandir");
 
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
    }
1399
1461
  } else {
1400
1462
    int devnull = open("/dev/null", O_RDONLY);
1401
1463
    for(int i = 0; i < numhooks; i++){
1414
1476
      if(hook_pid == 0){
1415
1477
        /* Child */
1416
1478
        /* Raise privileges */
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
 
        }
 
1479
        raise_privileges_permanently();
1428
1480
        /* Set group */
1429
1481
        errno = 0;
1430
1482
        ret = setgid(0);
1523
1575
  return true;
1524
1576
}
1525
1577
 
 
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
 
1526
1750
int main(int argc, char *argv[]){
1527
1751
  AvahiSServiceBrowser *sb = NULL;
1528
 
  int error;
 
1752
  error_t ret_errno;
1529
1753
  int ret;
1530
1754
  intmax_t tmpmax;
1531
1755
  char *tmp;
1532
1756
  int exitcode = EXIT_SUCCESS;
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;
 
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;
1539
1761
  char tempdir[] = "/tmp/mandosXXXXXX";
1540
1762
  bool tempdir_created = false;
1541
1763
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1542
1764
  const char *seckey = PATHDIR "/" SECKEY;
1543
1765
  const char *pubkey = PATHDIR "/" PUBKEY;
 
1766
  char *interfaces_hooks = NULL;
 
1767
  size_t interfaces_hooks_size = 0;
1544
1768
  
1545
1769
  bool gnutls_initialized = false;
1546
1770
  bool gpgme_initialized = false;
1637
1861
        connect_to = arg;
1638
1862
        break;
1639
1863
      case 'i':                 /* --interface */
1640
 
        interface = arg;
 
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
        }
1641
1869
        break;
1642
1870
      case 's':                 /* --seckey */
1643
1871
        seckey = arg;
1721
1949
       <http://bugs.debian.org/633582> */
1722
1950
    
1723
1951
    /* Re-raise priviliges */
1724
 
    errno = 0;
1725
 
    ret = seteuid(0);
1726
 
    if(ret == -1){
1727
 
      perror_plus("seteuid");
1728
 
    } else {
 
1952
    if(raise_privileges() == 0){
1729
1953
      struct stat st;
1730
1954
      
1731
1955
      if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1779
2003
    }
1780
2004
  }
1781
2005
  
 
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
  
1782
2022
  /* Run network hooks */
1783
 
  if(not run_network_hooks("start", interface, delay)){
1784
 
    goto end;
 
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
    }
1785
2040
  }
1786
2041
  
1787
2042
  if(not debug){
1788
2043
    avahi_set_log_function(empty_log);
1789
2044
  }
1790
2045
  
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
 
  
1823
2046
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
1824
2047
     from the signal handler */
1825
2048
  /* Initialize the pseudo-RNG for Avahi */
1895
2118
    }
1896
2119
  }
1897
2120
  
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");
 
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");
2032
2183
    }
2033
2184
  }
2034
2185
  
2075
2226
    /* Connect directly, do not use Zeroconf */
2076
2227
    /* (Mainly meant for debugging) */
2077
2228
    char *address = strrchr(connect_to, ':');
 
2229
    
2078
2230
    if(address == NULL){
2079
2231
      fprintf_plus(stderr, "No colon in address\n");
2080
2232
      exitcode = EX_USAGE;
2085
2237
      goto end;
2086
2238
    }
2087
2239
    
2088
 
    uint16_t port;
 
2240
    in_port_t port;
2089
2241
    errno = 0;
2090
2242
    tmpmax = strtoimax(address+1, &tmp, 10);
2091
2243
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
2092
 
       or tmpmax != (uint16_t)tmpmax){
 
2244
       or tmpmax != (in_port_t)tmpmax){
2093
2245
      fprintf_plus(stderr, "Bad port number\n");
2094
2246
      exitcode = EX_USAGE;
2095
2247
      goto end;
2099
2251
      goto end;
2100
2252
    }
2101
2253
    
2102
 
    port = (uint16_t)tmpmax;
 
2254
    port = (in_port_t)tmpmax;
2103
2255
    *address = '\0';
2104
2256
    /* Colon in address indicates IPv6 */
2105
2257
    int af;
2155
2307
    /* Allocate a new server */
2156
2308
    mc.server = avahi_server_new(avahi_simple_poll_get
2157
2309
                                 (mc.simple_poll), &config, NULL,
2158
 
                                 NULL, &error);
 
2310
                                 NULL, &ret_errno);
2159
2311
    
2160
2312
    /* Free the Avahi configuration data */
2161
2313
    avahi_server_config_free(&config);
2164
2316
  /* Check if creating the Avahi server object succeeded */
2165
2317
  if(mc.server == NULL){
2166
2318
    fprintf_plus(stderr, "Failed to create Avahi server: %s\n",
2167
 
                 avahi_strerror(error));
 
2319
                 avahi_strerror(ret_errno));
2168
2320
    exitcode = EX_UNAVAILABLE;
2169
2321
    goto end;
2170
2322
  }
2238
2390
    }
2239
2391
  }
2240
2392
  
2241
 
  /* Run network hooks */
2242
 
  run_network_hooks("stop", interface, delay);
2243
 
  
2244
2393
  /* Re-raise priviliges */
2245
2394
  {
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");
 
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");
2262
2411
        }
2263
2412
      }
2264
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
2265
 
      if(ret == -1){
2266
 
        perror_plus("close");
 
2413
      if(debug and (interfaces_to_take_down == NULL)){
 
2414
        fprintf_plus(stderr, "No interfaces needed to be taken"
 
2415
                     " down\n");
2267
2416
      }
2268
2417
    }
2269
 
  }
2270
 
  /* Lower privileges permanently */
2271
 
  errno = 0;
2272
 
  ret = setuid(uid);
2273
 
  if(ret == -1){
2274
 
    perror_plus("setuid");
2275
 
  }
 
2418
    
 
2419
    lower_privileges_permanently();
 
2420
  }
 
2421
  
 
2422
  free(interfaces_to_take_down);
 
2423
  free(interfaces_hooks);
2276
2424
  
2277
2425
  /* Removes the GPGME temp directory and all files inside */
2278
2426
  if(tempdir_created){