/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: Björn Påhlsson
  • Date: 2011-12-25 10:33:25 UTC
  • Revision ID: belorn@recompile.se-20111225103325-wlbwm89iy21c7mg0
renamed some foomax values to what they actually represent
fixed a very small exitstatus bug in plugin-runner

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-2012 Teddy Hogeborn
13
 
 * Copyright © 2008-2012 Björn Påhlsson
 
12
 * Copyright © 2008-2011 Teddy Hogeborn
 
13
 * Copyright © 2008-2011 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, 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() */
61
61
                                 */
62
62
#include <inttypes.h>           /* PRIu16, PRIdMAX, intmax_t,
63
63
                                   strtoimax() */
 
64
#include <assert.h>             /* assert() */
64
65
#include <errno.h>              /* perror(), errno,
65
66
                                   program_invocation_short_name */
66
67
#include <time.h>               /* nanosleep(), time(), sleep() */
87
88
#include <sys/wait.h>           /* waitpid(), WIFEXITED(),
88
89
                                   WEXITSTATUS(), WTERMSIG() */
89
90
#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() */
94
91
 
95
92
#ifdef __linux__
96
93
#include <sys/klog.h>           /* klogctl() */
138
135
static const char sys_class_net[] = "/sys/class/net";
139
136
char *connect_to = NULL;
140
137
const char *hookdir = HOOKDIR;
141
 
uid_t uid = 65534;
142
 
gid_t gid = 65534;
143
138
 
144
139
/* Doubly linked list that need to be circularly linked when used */
145
140
typedef struct server{
146
141
  const char *ip;
147
 
  in_port_t port;
 
142
  uint16_t port;
148
143
  AvahiIfIndex if_index;
149
144
  int af;
150
145
  struct timespec last_seen;
210
205
}
211
206
 
212
207
/* Add server to set of servers to retry periodically */
213
 
bool add_server(const char *ip, in_port_t port, AvahiIfIndex if_index,
 
208
bool add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
214
209
                int af){
215
210
  int ret;
216
211
  server *new_server = malloc(sizeof(server));
470
465
}
471
466
 
472
467
static const char * safer_gnutls_strerror(int value){
473
 
  const char *ret = gnutls_strerror(value);
 
468
  const char *ret = gnutls_strerror(value); /* Spurious warning from
 
469
                                               -Wunreachable-code */
474
470
  if(ret == NULL)
475
471
    ret = "(unknown)";
476
472
  return ret;
621
617
                      __attribute__((unused)) const char *txt){}
622
618
 
623
619
/* Called when a Mandos server is found */
624
 
static int start_mandos_communication(const char *ip, in_port_t port,
 
620
static int start_mandos_communication(const char *ip, uint16_t port,
625
621
                                      AvahiIfIndex if_index,
626
622
                                      int af){
627
623
  int ret, tcp_sd = -1;
666
662
  
667
663
  if(debug){
668
664
    fprintf_plus(stderr, "Setting up a TCP connection to %s, port %"
669
 
                 PRIuMAX "\n", ip, (uintmax_t)port);
 
665
                 PRIu16 "\n", ip, port);
670
666
  }
671
667
  
672
668
  tcp_sd = socket(pf, SOCK_STREAM, 0);
703
699
    goto mandos_end;
704
700
  }
705
701
  if(af == AF_INET6){
706
 
    to.in6.sin6_port = htons(port);    
 
702
    to.in6.sin6_port = htons(port); /* Spurious warnings from
 
703
                                       -Wconversion and
 
704
                                       -Wunreachable-code */
 
705
    
707
706
    if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
708
707
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
709
708
                                -Wunreachable-code*/
733
732
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
734
733
        perror_plus("if_indextoname");
735
734
      } else {
736
 
        fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIuMAX
737
 
                     "\n", ip, interface, (uintmax_t)port);
 
735
        fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16
 
736
                     "\n", ip, interface, port);
738
737
      }
739
738
    } else {
740
 
      fprintf_plus(stderr, "Connection to: %s, port %" PRIuMAX "\n",
741
 
                   ip, (uintmax_t)port);
 
739
      fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n",
 
740
                   ip, port);
742
741
    }
743
742
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
744
743
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
822
821
    goto mandos_end;
823
822
  }
824
823
  
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);
 
824
  /* Spurious warning from -Wint-to-pointer-cast */
 
825
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
830
826
  
831
827
  if(quit_now){
832
828
    errno = EINTR;
1005
1001
                             AVAHI_GCC_UNUSED AvahiLookupResultFlags
1006
1002
                             flags,
1007
1003
                             AVAHI_GCC_UNUSED void* userdata){
1008
 
  if(r == NULL){
1009
 
    return;
1010
 
  }
 
1004
  assert(r);
1011
1005
  
1012
1006
  /* Called whenever a service has been resolved successfully or
1013
1007
     timed out */
1034
1028
                     PRIdMAX ") on port %" PRIu16 "\n", name,
1035
1029
                     host_name, ip, (intmax_t)interface, port);
1036
1030
      }
1037
 
      int ret = start_mandos_communication(ip, (in_port_t)port,
1038
 
                                           interface,
 
1031
      int ret = start_mandos_communication(ip, port, interface,
1039
1032
                                           avahi_proto_to_af(proto));
1040
1033
      if(ret == 0){
1041
1034
        avahi_simple_poll_quit(mc.simple_poll);
1042
1035
      } else {
1043
 
        if(not add_server(ip, (in_port_t)port, interface,
 
1036
        if(not add_server(ip, port, interface,
1044
1037
                          avahi_proto_to_af(proto))){
1045
1038
          fprintf_plus(stderr, "Failed to add server \"%s\" to server"
1046
1039
                       " list\n", name);
1061
1054
                            AVAHI_GCC_UNUSED AvahiLookupResultFlags
1062
1055
                            flags,
1063
1056
                            AVAHI_GCC_UNUSED void* userdata){
1064
 
  if(b == NULL){
1065
 
    return;
1066
 
  }
 
1057
  assert(b);
1067
1058
  
1068
1059
  /* Called whenever a new services becomes available on the LAN or
1069
1060
     is removed from the LAN */
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);
1495
1450
          perror_plus("setenv");
1496
1451
          _exit(EX_OSERR);
1497
1452
        }
1498
 
        ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
 
1453
        ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1499
1454
        if(ret == -1){
1500
1455
          perror_plus("setenv");
1501
1456
          _exit(EX_OSERR);
1518
1473
          _exit(EX_OSERR);
1519
1474
        }
1520
1475
        free(delaystring);
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
1476
        if(execl(fullname, direntry->d_name, mode, NULL) == -1){
1529
1477
          perror_plus("execl");
1530
1478
          _exit(EXIT_FAILURE);
1568
1516
  return true;
1569
1517
}
1570
1518
 
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
1519
int main(int argc, char *argv[]){
1744
1520
  AvahiSServiceBrowser *sb = NULL;
1745
 
  error_t ret_errno;
 
1521
  int error;
1746
1522
  int ret;
1747
1523
  intmax_t tmpmax;
1748
1524
  char *tmp;
1749
1525
  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;
 
1526
  const char *interface = "";
 
1527
  struct ifreq network;
 
1528
  int sd = -1;
 
1529
  bool take_down_interface = false;
 
1530
  uid_t uid;
 
1531
  gid_t gid;
1754
1532
  char tempdir[] = "/tmp/mandosXXXXXX";
1755
1533
  bool tempdir_created = false;
1756
1534
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1757
1535
  const char *seckey = PATHDIR "/" SECKEY;
1758
1536
  const char *pubkey = PATHDIR "/" PUBKEY;
1759
 
  char *interfaces_hooks = NULL;
1760
 
  size_t interfaces_hooks_size = 0;
1761
1537
  
1762
1538
  bool gnutls_initialized = false;
1763
1539
  bool gpgme_initialized = false;
1825
1601
        .group = 2 },
1826
1602
      { .name = "retry", .key = 132,
1827
1603
        .arg = "SECONDS",
1828
 
        .doc = "Retry interval used when denied by the Mandos server",
 
1604
        .doc = "Retry interval used when denied by the mandos server",
1829
1605
        .group = 2 },
1830
1606
      { .name = "network-hook-dir", .key = 133,
1831
1607
        .arg = "DIR",
1854
1630
        connect_to = arg;
1855
1631
        break;
1856
1632
      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
 
        }
 
1633
        interface = arg;
1862
1634
        break;
1863
1635
      case 's':                 /* --seckey */
1864
1636
        seckey = arg;
1942
1714
       <http://bugs.debian.org/633582> */
1943
1715
    
1944
1716
    /* Re-raise priviliges */
1945
 
    if(raise_privileges() == 0){
 
1717
    errno = 0;
 
1718
    ret = seteuid(0);
 
1719
    if(ret == -1){
 
1720
      perror_plus("seteuid");
 
1721
    } else {
1946
1722
      struct stat st;
1947
1723
      
1948
1724
      if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1996
1772
    }
1997
1773
  }
1998
1774
  
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
1775
  /* 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
 
    }
 
1776
  if(not run_network_hooks("start", interface, delay)){
 
1777
    goto end;
2028
1778
  }
2029
1779
  
2030
1780
  if(not debug){
2031
1781
    avahi_set_log_function(empty_log);
2032
1782
  }
2033
1783
  
 
1784
  if(interface[0] == '\0'){
 
1785
    struct dirent **direntries;
 
1786
    /* First look for interfaces that are up */
 
1787
    ret = scandir(sys_class_net, &direntries, up_interface,
 
1788
                  alphasort);
 
1789
    if(ret == 0){
 
1790
      /* No up interfaces, look for any good interfaces */
 
1791
      free(direntries);
 
1792
      ret = scandir(sys_class_net, &direntries, good_interface,
 
1793
                    alphasort);
 
1794
    }
 
1795
    if(ret >= 1){
 
1796
      /* Pick the first interface returned */
 
1797
      interface = strdup(direntries[0]->d_name);
 
1798
      if(debug){
 
1799
        fprintf_plus(stderr, "Using interface \"%s\"\n", interface);
 
1800
      }
 
1801
      if(interface == NULL){
 
1802
        perror_plus("malloc");
 
1803
        free(direntries);
 
1804
        exitcode = EXIT_FAILURE;
 
1805
        goto end;
 
1806
      }
 
1807
      free(direntries);
 
1808
    } else {
 
1809
      free(direntries);
 
1810
      fprintf_plus(stderr, "Could not find a network interface\n");
 
1811
      exitcode = EXIT_FAILURE;
 
1812
      goto end;
 
1813
    }
 
1814
  }
 
1815
  
2034
1816
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
2035
1817
     from the signal handler */
2036
1818
  /* Initialize the pseudo-RNG for Avahi */
2106
1888
    }
2107
1889
  }
2108
1890
  
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");
 
1891
  /* If the interface is down, bring it up */
 
1892
  if(strcmp(interface, "none") != 0){
 
1893
    if_index = (AvahiIfIndex) if_nametoindex(interface);
 
1894
    if(if_index == 0){
 
1895
      fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
 
1896
      exitcode = EX_UNAVAILABLE;
 
1897
      goto end;
 
1898
    }
 
1899
    
 
1900
    if(quit_now){
 
1901
      goto end;
 
1902
    }
 
1903
    
 
1904
    /* Re-raise priviliges */
 
1905
    errno = 0;
 
1906
    ret = seteuid(0);
 
1907
    if(ret == -1){
 
1908
      perror_plus("seteuid");
 
1909
    }
 
1910
    
 
1911
#ifdef __linux__
 
1912
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
 
1913
       messages about the network interface to mess up the prompt */
 
1914
    ret = klogctl(8, NULL, 5);
 
1915
    bool restore_loglevel = true;
 
1916
    if(ret == -1){
 
1917
      restore_loglevel = false;
 
1918
      perror_plus("klogctl");
 
1919
    }
 
1920
#endif  /* __linux__ */
 
1921
    
 
1922
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
1923
    if(sd < 0){
 
1924
      perror_plus("socket");
 
1925
      exitcode = EX_OSERR;
 
1926
#ifdef __linux__
 
1927
      if(restore_loglevel){
 
1928
        ret = klogctl(7, NULL, 0);
 
1929
        if(ret == -1){
 
1930
          perror_plus("klogctl");
 
1931
        }
 
1932
      }
 
1933
#endif  /* __linux__ */
 
1934
      /* Lower privileges */
 
1935
      errno = 0;
 
1936
      ret = seteuid(uid);
 
1937
      if(ret == -1){
 
1938
        perror_plus("seteuid");
 
1939
      }
 
1940
      goto end;
 
1941
    }
 
1942
    strcpy(network.ifr_name, interface);
 
1943
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
1944
    if(ret == -1){
 
1945
      perror_plus("ioctl SIOCGIFFLAGS");
 
1946
#ifdef __linux__
 
1947
      if(restore_loglevel){
 
1948
        ret = klogctl(7, NULL, 0);
 
1949
        if(ret == -1){
 
1950
          perror_plus("klogctl");
 
1951
        }
 
1952
      }
 
1953
#endif  /* __linux__ */
 
1954
      exitcode = EX_OSERR;
 
1955
      /* Lower privileges */
 
1956
      errno = 0;
 
1957
      ret = seteuid(uid);
 
1958
      if(ret == -1){
 
1959
        perror_plus("seteuid");
 
1960
      }
 
1961
      goto end;
 
1962
    }
 
1963
    if((network.ifr_flags & IFF_UP) == 0){
 
1964
      network.ifr_flags |= IFF_UP;
 
1965
      take_down_interface = true;
 
1966
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
1967
      if(ret == -1){
 
1968
        take_down_interface = false;
 
1969
        perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
 
1970
        exitcode = EX_OSERR;
 
1971
#ifdef __linux__
 
1972
        if(restore_loglevel){
 
1973
          ret = klogctl(7, NULL, 0);
 
1974
          if(ret == -1){
 
1975
            perror_plus("klogctl");
 
1976
          }
 
1977
        }
 
1978
#endif  /* __linux__ */
 
1979
        /* Lower privileges */
 
1980
        errno = 0;
 
1981
        ret = seteuid(uid);
 
1982
        if(ret == -1){
 
1983
          perror_plus("seteuid");
 
1984
        }
 
1985
        goto end;
 
1986
      }
 
1987
    }
 
1988
    /* Sleep checking until interface is running.
 
1989
       Check every 0.25s, up to total time of delay */
 
1990
    for(int i=0; i < delay * 4; i++){
 
1991
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
1992
      if(ret == -1){
 
1993
        perror_plus("ioctl SIOCGIFFLAGS");
 
1994
      } else if(network.ifr_flags & IFF_RUNNING){
 
1995
        break;
 
1996
      }
 
1997
      struct timespec sleeptime = { .tv_nsec = 250000000 };
 
1998
      ret = nanosleep(&sleeptime, NULL);
 
1999
      if(ret == -1 and errno != EINTR){
 
2000
        perror_plus("nanosleep");
 
2001
      }
 
2002
    }
 
2003
    if(not take_down_interface){
 
2004
      /* We won't need the socket anymore */
 
2005
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
2006
      if(ret == -1){
 
2007
        perror_plus("close");
 
2008
      }
 
2009
    }
 
2010
#ifdef __linux__
 
2011
    if(restore_loglevel){
 
2012
      /* Restores kernel loglevel to default */
 
2013
      ret = klogctl(7, NULL, 0);
 
2014
      if(ret == -1){
 
2015
        perror_plus("klogctl");
 
2016
      }
 
2017
    }
 
2018
#endif  /* __linux__ */
 
2019
    /* Lower privileges */
 
2020
    errno = 0;
 
2021
    /* Lower privileges */
 
2022
    ret = seteuid(uid);
 
2023
    if(ret == -1){
 
2024
      perror_plus("seteuid");
2171
2025
    }
2172
2026
  }
2173
2027
  
2214
2068
    /* Connect directly, do not use Zeroconf */
2215
2069
    /* (Mainly meant for debugging) */
2216
2070
    char *address = strrchr(connect_to, ':');
2217
 
    
2218
2071
    if(address == NULL){
2219
2072
      fprintf_plus(stderr, "No colon in address\n");
2220
2073
      exitcode = EX_USAGE;
2225
2078
      goto end;
2226
2079
    }
2227
2080
    
2228
 
    in_port_t port;
 
2081
    uint16_t port;
2229
2082
    errno = 0;
2230
2083
    tmpmax = strtoimax(address+1, &tmp, 10);
2231
2084
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
2232
 
       or tmpmax != (in_port_t)tmpmax){
 
2085
       or tmpmax != (uint16_t)tmpmax){
2233
2086
      fprintf_plus(stderr, "Bad port number\n");
2234
2087
      exitcode = EX_USAGE;
2235
2088
      goto end;
2239
2092
      goto end;
2240
2093
    }
2241
2094
    
2242
 
    port = (in_port_t)tmpmax;
 
2095
    port = (uint16_t)tmpmax;
2243
2096
    *address = '\0';
2244
2097
    /* Colon in address indicates IPv6 */
2245
2098
    int af;
2295
2148
    /* Allocate a new server */
2296
2149
    mc.server = avahi_server_new(avahi_simple_poll_get
2297
2150
                                 (mc.simple_poll), &config, NULL,
2298
 
                                 NULL, &ret_errno);
 
2151
                                 NULL, &error);
2299
2152
    
2300
2153
    /* Free the Avahi configuration data */
2301
2154
    avahi_server_config_free(&config);
2304
2157
  /* Check if creating the Avahi server object succeeded */
2305
2158
  if(mc.server == NULL){
2306
2159
    fprintf_plus(stderr, "Failed to create Avahi server: %s\n",
2307
 
                 avahi_strerror(ret_errno));
 
2160
                 avahi_strerror(error));
2308
2161
    exitcode = EX_UNAVAILABLE;
2309
2162
    goto end;
2310
2163
  }
2378
2231
    }
2379
2232
  }
2380
2233
  
 
2234
  /* Run network hooks */
 
2235
  run_network_hooks("stop", interface, delay);
 
2236
  
2381
2237
  /* Re-raise priviliges */
2382
2238
  {
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");
 
2239
    errno = 0;
 
2240
    ret = seteuid(0);
 
2241
    if(ret == -1){
 
2242
      perror_plus("seteuid");
 
2243
    }
 
2244
    
 
2245
    /* Take down the network interface */
 
2246
    if(take_down_interface and geteuid() == 0){
 
2247
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
2248
      if(ret == -1){
 
2249
        perror_plus("ioctl SIOCGIFFLAGS");
 
2250
      } else if(network.ifr_flags & IFF_UP){
 
2251
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
 
2252
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
2253
        if(ret == -1){
 
2254
          perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
2398
2255
        }
2399
2256
      }
2400
 
      if(debug and (interfaces_to_take_down == NULL)){
2401
 
        fprintf_plus(stderr, "No interfaces needed to be taken"
2402
 
                     " down\n");
 
2257
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
2258
      if(ret == -1){
 
2259
        perror_plus("close");
2403
2260
      }
2404
2261
    }
2405
 
    
2406
 
    lower_privileges_permanently();
2407
 
  }
2408
 
  
2409
 
  free(interfaces_to_take_down);
2410
 
  free(interfaces_hooks);
 
2262
  }
 
2263
  /* Lower privileges permanently */
 
2264
  errno = 0;
 
2265
  ret = setuid(uid);
 
2266
  if(ret == -1){
 
2267
    perror_plus("setuid");
 
2268
  }
2411
2269
  
2412
2270
  /* Removes the GPGME temp directory and all files inside */
2413
2271
  if(tempdir_created){