/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-13 22:06:57 UTC
  • mto: This revision was merged to the branch mainline in revision 596.
  • Revision ID: teddy@recompile.se-20120613220657-qvq7c7nrndl3t413
* plugins.d/mandos-client.c (get_flags): Don't clobber errno.
  (up_interface): Removed; replaced with "interface_is_up".
  (interface_is_up, interface_is_running,
   lower_privileges_permanently, take_down_interface): New.
  (bring_up_interface): Return "error_t".  Use new functions
                        "interface_is_up", "get_flags", and
                        "interface_is_running".
  (main): Save all interfaces either autodetected or specified with
          --interface in argz vector "interfaces".  Save interfaces to
          take down on exit in argz vector "interfaces_to_take_down".
          Save interface names for DEVICE variable to network hooks as
          argz_vector "interfaces_hooks".  Bug fix: Be privileged
          while stopping network hooks.
* plugins.d/mandos-client.xml (SYNOPSIS): Changed --interface synopsis.
  (DESCRIPTION): Updated to document use of all interfaces.
  (OPTIONS): Updated description of "--interface".
* network-hooks.d/bridge: Parse comma-separated DEVICE environment
                          variable.
* network-hooks.d/openvpn: - '' -
* network-hooks.d/wireless: - '' -

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
 * "browse_callback", and parts of "main".
10
10
 * 
11
11
 * Everything else is
12
 
 * Copyright © 2008-2011 Teddy Hogeborn
13
 
 * Copyright © 2008-2011 Björn Påhlsson
 
12
 * Copyright © 2008-2012 Teddy Hogeborn
 
13
 * Copyright © 2008-2012 Björn Påhlsson
14
14
 * 
15
15
 * This program is free software: you can redistribute it and/or
16
16
 * modify it under the terms of the GNU General Public License as
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() */
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() */
91
95
 
92
96
#ifdef __linux__
93
97
#include <sys/klog.h>           /* klogctl() */
135
139
static const char sys_class_net[] = "/sys/class/net";
136
140
char *connect_to = NULL;
137
141
const char *hookdir = HOOKDIR;
 
142
uid_t uid = 65534;
 
143
gid_t gid = 65534;
138
144
 
139
145
/* Doubly linked list that need to be circularly linked when used */
140
146
typedef struct server{
170
176
 
171
177
/* Function to use when printing errors */
172
178
void perror_plus(const char *print_text){
 
179
  int e = errno;
173
180
  fprintf(stderr, "Mandos plugin %s: ",
174
181
          program_invocation_short_name);
 
182
  errno = e;
175
183
  perror(print_text);
176
184
}
177
185
 
 
186
__attribute__((format (gnu_printf, 2, 3)))
178
187
int fprintf_plus(FILE *stream, const char *format, ...){
179
188
  va_list ap;
180
189
  va_start (ap, format);
202
211
}
203
212
 
204
213
/* Add server to set of servers to retry periodically */
205
 
int add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
206
 
               int af){
 
214
bool add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
 
215
                int af){
207
216
  int ret;
208
217
  server *new_server = malloc(sizeof(server));
209
218
  if(new_server == NULL){
210
219
    perror_plus("malloc");
211
 
    return -1;
 
220
    return false;
212
221
  }
213
222
  *new_server = (server){ .ip = strdup(ip),
214
223
                          .port = port,
216
225
                          .af = af };
217
226
  if(new_server->ip == NULL){
218
227
    perror_plus("strdup");
219
 
    return -1;
 
228
    return false;
220
229
  }
221
230
  /* Special case of first server */
222
231
  if (mc.current_server == NULL){
233
242
  ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
234
243
  if(ret == -1){
235
244
    perror_plus("clock_gettime");
236
 
    return -1;
 
245
    return false;
237
246
  }
238
 
  return 0;
 
247
  return true;
239
248
}
240
249
 
241
250
/* 
818
827
    goto mandos_end;
819
828
  }
820
829
  
821
 
  /* Spurious warning from -Wint-to-pointer-cast */
822
 
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
 
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);
823
835
  
824
836
  if(quit_now){
825
837
    errno = EINTR;
1030
1042
      if(ret == 0){
1031
1043
        avahi_simple_poll_quit(mc.simple_poll);
1032
1044
      } else {
1033
 
        ret = add_server(ip, port, interface,
1034
 
                         avahi_proto_to_af(proto));
 
1045
        if(not add_server(ip, port, interface,
 
1046
                          avahi_proto_to_af(proto))){
 
1047
          fprintf_plus(stderr, "Failed to add server \"%s\" to server"
 
1048
                       " list\n", name);
 
1049
        }
1035
1050
      }
1036
1051
    }
1037
1052
  }
1110
1125
 
1111
1126
bool get_flags(const char *ifname, struct ifreq *ifr){
1112
1127
  int ret;
 
1128
  error_t ret_errno;
1113
1129
  
1114
1130
  int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1115
1131
  if(s < 0){
 
1132
    ret_errno = errno;
1116
1133
    perror_plus("socket");
 
1134
    errno = ret_errno;
1117
1135
    return false;
1118
1136
  }
1119
1137
  strcpy(ifr->ifr_name, ifname);
1120
1138
  ret = ioctl(s, SIOCGIFFLAGS, ifr);
1121
1139
  if(ret == -1){
1122
1140
    if(debug){
 
1141
      ret_errno = errno;
1123
1142
      perror_plus("ioctl SIOCGIFFLAGS");
 
1143
      errno = ret_errno;
1124
1144
    }
1125
1145
    return false;
1126
1146
  }
1195
1215
}
1196
1216
 
1197
1217
/* 
1198
 
 * This function determines if a directory entry in /sys/class/net
1199
 
 * corresponds to an acceptable network device which is up.
1200
 
 * (This function is passed to scandir(3) as a filter function.)
1201
 
 */
1202
 
int up_interface(const struct dirent *if_entry){
1203
 
  if(if_entry->d_name[0] == '.'){
1204
 
    return 0;
1205
 
  }
1206
 
  
1207
 
  struct ifreq ifr;
1208
 
  if(not get_flags(if_entry->d_name, &ifr)){
1209
 
    if(debug){
1210
 
      fprintf_plus(stderr, "Failed to get flags for interface "
1211
 
                   "\"%s\"\n", if_entry->d_name);
1212
 
    }
1213
 
    return 0;
1214
 
  }
1215
 
  
1216
 
  /* Reject down interfaces */
1217
 
  if(not (ifr.ifr_flags & IFF_UP)){
1218
 
    if(debug){
1219
 
      fprintf_plus(stderr, "Rejecting down interface \"%s\"\n",
1220
 
                   if_entry->d_name);
1221
 
    }
1222
 
    return 0;
1223
 
  }
1224
 
  
1225
 
  /* Reject non-running interfaces */
1226
 
  if(not (ifr.ifr_flags & IFF_RUNNING)){
1227
 
    if(debug){
1228
 
      fprintf_plus(stderr, "Rejecting non-running interface \"%s\"\n",
1229
 
                   if_entry->d_name);
1230
 
    }
1231
 
    return 0;
1232
 
  }
1233
 
  
1234
 
  if(not good_flags(if_entry->d_name, &ifr)){
1235
 
    return 0;
1236
 
  }
1237
 
  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);
1238
1247
}
1239
1248
 
1240
1249
int notdotentries(const struct dirent *direntry){
1381
1390
  }
1382
1391
}
1383
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
 
1384
1445
bool run_network_hooks(const char *mode, const char *interface,
1385
1446
                       const float delay){
1386
1447
  struct dirent **direntries;
1408
1469
      if(hook_pid == 0){
1409
1470
        /* Child */
1410
1471
        /* Raise privileges */
1411
 
        errno = 0;
1412
 
        ret = seteuid(0);
1413
 
        if(ret == -1){
1414
 
          perror_plus("seteuid");
1415
 
        }
1416
 
        /* Raise privileges even more */
1417
 
        errno = 0;
1418
 
        ret = setuid(0);
1419
 
        if(ret == -1){
1420
 
          perror_plus("setuid");
1421
 
        }
 
1472
        raise_privileges_permanently();
1422
1473
        /* Set group */
1423
1474
        errno = 0;
1424
1475
        ret = setgid(0);
1444
1495
          perror_plus("setenv");
1445
1496
          _exit(EX_OSERR);
1446
1497
        }
1447
 
        ret = setenv("VERBOSE", debug ? "1" : "0", 1);
 
1498
        ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1448
1499
        if(ret == -1){
1449
1500
          perror_plus("setenv");
1450
1501
          _exit(EX_OSERR);
1467
1518
          _exit(EX_OSERR);
1468
1519
        }
1469
1520
        free(delaystring);
1470
 
        ret = execl(fullname, direntry->d_name, mode, NULL);
1471
 
        perror_plus("execl");
 
1521
        if(connect_to != NULL){
 
1522
          ret = setenv("CONNECT", connect_to, 1);
 
1523
          if(ret == -1){
 
1524
            perror_plus("setenv");
 
1525
            _exit(EX_OSERR);
 
1526
          }
 
1527
        }
 
1528
        if(execl(fullname, direntry->d_name, mode, NULL) == -1){
 
1529
          perror_plus("execl");
 
1530
          _exit(EXIT_FAILURE);
 
1531
        }
1472
1532
      } else {
1473
1533
        int status;
1474
1534
        if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1508
1568
  return true;
1509
1569
}
1510
1570
 
 
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
 
1511
1743
int main(int argc, char *argv[]){
1512
1744
  AvahiSServiceBrowser *sb = NULL;
1513
 
  int error;
 
1745
  error_t ret_errno;
1514
1746
  int ret;
1515
1747
  intmax_t tmpmax;
1516
1748
  char *tmp;
1517
1749
  int exitcode = EXIT_SUCCESS;
1518
 
  const char *interface = "";
1519
 
  struct ifreq network;
1520
 
  int sd = -1;
1521
 
  bool take_down_interface = false;
1522
 
  uid_t uid;
1523
 
  gid_t gid;
 
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;
1524
1754
  char tempdir[] = "/tmp/mandosXXXXXX";
1525
1755
  bool tempdir_created = false;
1526
1756
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1527
1757
  const char *seckey = PATHDIR "/" SECKEY;
1528
1758
  const char *pubkey = PATHDIR "/" PUBKEY;
 
1759
  char *interfaces_hooks = NULL;
 
1760
  size_t interfaces_hooks_size = 0;
1529
1761
  
1530
1762
  bool gnutls_initialized = false;
1531
1763
  bool gpgme_initialized = false;
1593
1825
        .group = 2 },
1594
1826
      { .name = "retry", .key = 132,
1595
1827
        .arg = "SECONDS",
1596
 
        .doc = "Retry interval used when denied by the mandos server",
 
1828
        .doc = "Retry interval used when denied by the Mandos server",
1597
1829
        .group = 2 },
1598
1830
      { .name = "network-hook-dir", .key = 133,
1599
1831
        .arg = "DIR",
1622
1854
        connect_to = arg;
1623
1855
        break;
1624
1856
      case 'i':                 /* --interface */
1625
 
        interface = arg;
 
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
        }
1626
1862
        break;
1627
1863
      case 's':                 /* --seckey */
1628
1864
        seckey = arg;
1671
1907
        argp_state_help(state, state->out_stream,
1672
1908
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1673
1909
      case 'V':                 /* --version */
1674
 
        fprintf_plus(state->out_stream,
1675
 
                     "Mandos plugin mandos-client: ");
1676
1910
        fprintf_plus(state->out_stream, "%s\n", argp_program_version);
1677
1911
        exit(argp_err_exit_status);
1678
1912
        break;
1708
1942
       <http://bugs.debian.org/633582> */
1709
1943
    
1710
1944
    /* Re-raise priviliges */
1711
 
    errno = 0;
1712
 
    ret = seteuid(0);
1713
 
    if(ret == -1){
1714
 
      perror_plus("seteuid");
1715
 
    } else {
 
1945
    if(raise_privileges() == 0){
1716
1946
      struct stat st;
1717
1947
      
1718
1948
      if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1766
1996
    }
1767
1997
  }
1768
1998
  
 
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
  
1769
2015
  /* Run network hooks */
1770
 
  if(not run_network_hooks("start", interface, delay)){
1771
 
    goto end;
 
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
    }
1772
2028
  }
1773
2029
  
1774
2030
  if(not debug){
1775
2031
    avahi_set_log_function(empty_log);
1776
2032
  }
1777
2033
  
1778
 
  if(interface[0] == '\0'){
1779
 
    struct dirent **direntries;
1780
 
    /* First look for interfaces that are up */
1781
 
    ret = scandir(sys_class_net, &direntries, up_interface,
1782
 
                  alphasort);
1783
 
    if(ret == 0){
1784
 
      /* No up interfaces, look for any good interfaces */
1785
 
      free(direntries);
1786
 
      ret = scandir(sys_class_net, &direntries, good_interface,
1787
 
                    alphasort);
1788
 
    }
1789
 
    if(ret >= 1){
1790
 
      /* Pick the first interface returned */
1791
 
      interface = strdup(direntries[0]->d_name);
1792
 
      if(debug){
1793
 
        fprintf_plus(stderr, "Using interface \"%s\"\n", interface);
1794
 
      }
1795
 
      if(interface == NULL){
1796
 
        perror_plus("malloc");
1797
 
        free(direntries);
1798
 
        exitcode = EXIT_FAILURE;
1799
 
        goto end;
1800
 
      }
1801
 
      free(direntries);
1802
 
    } else {
1803
 
      free(direntries);
1804
 
      fprintf_plus(stderr, "Could not find a network interface\n");
1805
 
      exitcode = EXIT_FAILURE;
1806
 
      goto end;
1807
 
    }
1808
 
  }
1809
 
  
1810
2034
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
1811
2035
     from the signal handler */
1812
2036
  /* Initialize the pseudo-RNG for Avahi */
1882
2106
    }
1883
2107
  }
1884
2108
  
1885
 
  /* If the interface is down, bring it up */
1886
 
  if(strcmp(interface, "none") != 0){
1887
 
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1888
 
    if(if_index == 0){
1889
 
      fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1890
 
      exitcode = EX_UNAVAILABLE;
1891
 
      goto end;
1892
 
    }
1893
 
    
1894
 
    if(quit_now){
1895
 
      goto end;
1896
 
    }
1897
 
    
1898
 
    /* Re-raise priviliges */
1899
 
    errno = 0;
1900
 
    ret = seteuid(0);
1901
 
    if(ret == -1){
1902
 
      perror_plus("seteuid");
1903
 
    }
1904
 
    
1905
 
#ifdef __linux__
1906
 
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1907
 
       messages about the network interface to mess up the prompt */
1908
 
    ret = klogctl(8, NULL, 5);
1909
 
    bool restore_loglevel = true;
1910
 
    if(ret == -1){
1911
 
      restore_loglevel = false;
1912
 
      perror_plus("klogctl");
1913
 
    }
1914
 
#endif  /* __linux__ */
1915
 
    
1916
 
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1917
 
    if(sd < 0){
1918
 
      perror_plus("socket");
1919
 
      exitcode = EX_OSERR;
1920
 
#ifdef __linux__
1921
 
      if(restore_loglevel){
1922
 
        ret = klogctl(7, NULL, 0);
1923
 
        if(ret == -1){
1924
 
          perror_plus("klogctl");
1925
 
        }
1926
 
      }
1927
 
#endif  /* __linux__ */
1928
 
      /* Lower privileges */
1929
 
      errno = 0;
1930
 
      ret = seteuid(uid);
1931
 
      if(ret == -1){
1932
 
        perror_plus("seteuid");
1933
 
      }
1934
 
      goto end;
1935
 
    }
1936
 
    strcpy(network.ifr_name, interface);
1937
 
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
1938
 
    if(ret == -1){
1939
 
      perror_plus("ioctl SIOCGIFFLAGS");
1940
 
#ifdef __linux__
1941
 
      if(restore_loglevel){
1942
 
        ret = klogctl(7, NULL, 0);
1943
 
        if(ret == -1){
1944
 
          perror_plus("klogctl");
1945
 
        }
1946
 
      }
1947
 
#endif  /* __linux__ */
1948
 
      exitcode = EX_OSERR;
1949
 
      /* Lower privileges */
1950
 
      errno = 0;
1951
 
      ret = seteuid(uid);
1952
 
      if(ret == -1){
1953
 
        perror_plus("seteuid");
1954
 
      }
1955
 
      goto end;
1956
 
    }
1957
 
    if((network.ifr_flags & IFF_UP) == 0){
1958
 
      network.ifr_flags |= IFF_UP;
1959
 
      take_down_interface = true;
1960
 
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1961
 
      if(ret == -1){
1962
 
        take_down_interface = false;
1963
 
        perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1964
 
        exitcode = EX_OSERR;
1965
 
#ifdef __linux__
1966
 
        if(restore_loglevel){
1967
 
          ret = klogctl(7, NULL, 0);
1968
 
          if(ret == -1){
1969
 
            perror_plus("klogctl");
1970
 
          }
1971
 
        }
1972
 
#endif  /* __linux__ */
1973
 
        /* Lower privileges */
1974
 
        errno = 0;
1975
 
        ret = seteuid(uid);
1976
 
        if(ret == -1){
1977
 
          perror_plus("seteuid");
1978
 
        }
1979
 
        goto end;
1980
 
      }
1981
 
    }
1982
 
    /* Sleep checking until interface is running.
1983
 
       Check every 0.25s, up to total time of delay */
1984
 
    for(int i=0; i < delay * 4; i++){
1985
 
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1986
 
      if(ret == -1){
1987
 
        perror_plus("ioctl SIOCGIFFLAGS");
1988
 
      } else if(network.ifr_flags & IFF_RUNNING){
1989
 
        break;
1990
 
      }
1991
 
      struct timespec sleeptime = { .tv_nsec = 250000000 };
1992
 
      ret = nanosleep(&sleeptime, NULL);
1993
 
      if(ret == -1 and errno != EINTR){
1994
 
        perror_plus("nanosleep");
1995
 
      }
1996
 
    }
1997
 
    if(not take_down_interface){
1998
 
      /* We won't need the socket anymore */
1999
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
2000
 
      if(ret == -1){
2001
 
        perror_plus("close");
2002
 
      }
2003
 
    }
2004
 
#ifdef __linux__
2005
 
    if(restore_loglevel){
2006
 
      /* Restores kernel loglevel to default */
2007
 
      ret = klogctl(7, NULL, 0);
2008
 
      if(ret == -1){
2009
 
        perror_plus("klogctl");
2010
 
      }
2011
 
    }
2012
 
#endif  /* __linux__ */
2013
 
    /* Lower privileges */
2014
 
    errno = 0;
2015
 
    /* Lower privileges */
2016
 
    ret = seteuid(uid);
2017
 
    if(ret == -1){
2018
 
      perror_plus("seteuid");
 
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");
2019
2171
    }
2020
2172
  }
2021
2173
  
2062
2214
    /* Connect directly, do not use Zeroconf */
2063
2215
    /* (Mainly meant for debugging) */
2064
2216
    char *address = strrchr(connect_to, ':');
 
2217
    
2065
2218
    if(address == NULL){
2066
2219
      fprintf_plus(stderr, "No colon in address\n");
2067
2220
      exitcode = EX_USAGE;
2142
2295
    /* Allocate a new server */
2143
2296
    mc.server = avahi_server_new(avahi_simple_poll_get
2144
2297
                                 (mc.simple_poll), &config, NULL,
2145
 
                                 NULL, &error);
 
2298
                                 NULL, &ret_errno);
2146
2299
    
2147
2300
    /* Free the Avahi configuration data */
2148
2301
    avahi_server_config_free(&config);
2151
2304
  /* Check if creating the Avahi server object succeeded */
2152
2305
  if(mc.server == NULL){
2153
2306
    fprintf_plus(stderr, "Failed to create Avahi server: %s\n",
2154
 
                 avahi_strerror(error));
 
2307
                 avahi_strerror(ret_errno));
2155
2308
    exitcode = EX_UNAVAILABLE;
2156
2309
    goto end;
2157
2310
  }
2225
2378
    }
2226
2379
  }
2227
2380
  
2228
 
  /* Run network hooks */
2229
 
  run_network_hooks("stop", interface, delay);
2230
 
  
2231
2381
  /* Re-raise priviliges */
2232
2382
  {
2233
 
    errno = 0;
2234
 
    ret = seteuid(0);
2235
 
    if(ret == -1){
2236
 
      perror_plus("seteuid");
2237
 
    }
2238
 
    
2239
 
    /* Take down the network interface */
2240
 
    if(take_down_interface and geteuid() == 0){
2241
 
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
2242
 
      if(ret == -1){
2243
 
        perror_plus("ioctl SIOCGIFFLAGS");
2244
 
      } else if(network.ifr_flags & IFF_UP){
2245
 
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
2246
 
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
2247
 
        if(ret == -1){
2248
 
          perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
 
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
2398
        }
2250
2399
      }
2251
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
2252
 
      if(ret == -1){
2253
 
        perror_plus("close");
 
2400
      if(debug and (interfaces_to_take_down == NULL)){
 
2401
        fprintf_plus(stderr, "No interfaces needed to be taken"
 
2402
                     " down\n");
2254
2403
      }
2255
2404
    }
2256
 
  }
2257
 
  /* Lower privileges permanently */
2258
 
  errno = 0;
2259
 
  ret = setuid(uid);
2260
 
  if(ret == -1){
2261
 
    perror_plus("setuid");
2262
 
  }
 
2405
    
 
2406
    lower_privileges_permanently();
 
2407
  }
 
2408
  
 
2409
  free(interfaces_to_take_down);
 
2410
  free(interfaces_hooks);
2263
2411
  
2264
2412
  /* Removes the GPGME temp directory and all files inside */
2265
2413
  if(tempdir_created){