/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release

« back to all changes in this revision

Viewing changes to plugins.d/mandos-client.c

  • Committer: Teddy Hogeborn
  • Date: 2012-06-01 18:39:03 UTC
  • mfrom: (300.1.1 release)
  • Revision ID: teddy@recompile.se-20120601183903-hydodgfmlau5d109
Tags: version-1.5.5-1
* Makefile (version): Changed to "1.5.5".
* NEWS (Version 1.5.5): New entry.
* debian/changelog (1.5.5-1): - '' -

Show diffs side-by-side

added added

removed removed

Lines of Context:
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;
154
149
 
155
150
/* Used for passing in values through the Avahi callback functions */
156
151
typedef struct {
 
152
  AvahiSimplePoll *simple_poll;
157
153
  AvahiServer *server;
158
154
  gnutls_certificate_credentials_t cred;
159
155
  unsigned int dh_bits;
163
159
  server *current_server;
164
160
} mandos_context;
165
161
 
166
 
/* global so signal handler can reach it*/
167
 
AvahiSimplePoll *simple_poll;
168
 
mandos_context mc = { .server = NULL, .dh_bits = 1024,
169
 
                      .priority = "SECURE256:!CTYPE-X.509:"
170
 
                      "+CTYPE-OPENPGP", .current_server = NULL };
 
162
/* global context so signal handler can reach it*/
 
163
mandos_context mc = { .simple_poll = NULL, .server = NULL,
 
164
                      .dh_bits = 1024, .priority = "SECURE256"
 
165
                      ":!CTYPE-X.509:+CTYPE-OPENPGP",
 
166
                      .current_server = NULL };
171
167
 
172
168
sig_atomic_t quit_now = 0;
173
169
int signal_received = 0;
209
205
}
210
206
 
211
207
/* Add server to set of servers to retry periodically */
212
 
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,
213
209
                int af){
214
210
  int ret;
215
211
  server *new_server = malloc(sizeof(server));
253
249
  gpgme_error_t rc;
254
250
  gpgme_engine_info_t engine_info;
255
251
  
 
252
  
256
253
  /*
257
254
   * Helper function to insert pub and seckey to the engine keyring.
258
255
   */
468
465
}
469
466
 
470
467
static const char * safer_gnutls_strerror(int value){
471
 
  const char *ret = gnutls_strerror(value);
 
468
  const char *ret = gnutls_strerror(value); /* Spurious warning from
 
469
                                               -Wunreachable-code */
472
470
  if(ret == NULL)
473
471
    ret = "(unknown)";
474
472
  return ret;
619
617
                      __attribute__((unused)) const char *txt){}
620
618
 
621
619
/* Called when a Mandos server is found */
622
 
static int start_mandos_communication(const char *ip, in_port_t port,
 
620
static int start_mandos_communication(const char *ip, uint16_t port,
623
621
                                      AvahiIfIndex if_index,
624
622
                                      int af){
625
623
  int ret, tcp_sd = -1;
664
662
  
665
663
  if(debug){
666
664
    fprintf_plus(stderr, "Setting up a TCP connection to %s, port %"
667
 
                 PRIuMAX "\n", ip, (uintmax_t)port);
 
665
                 PRIu16 "\n", ip, port);
668
666
  }
669
667
  
670
668
  tcp_sd = socket(pf, SOCK_STREAM, 0);
701
699
    goto mandos_end;
702
700
  }
703
701
  if(af == AF_INET6){
704
 
    to.in6.sin6_port = htons(port);    
 
702
    to.in6.sin6_port = htons(port); /* Spurious warnings from
 
703
                                       -Wconversion and
 
704
                                       -Wunreachable-code */
 
705
    
705
706
    if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
706
707
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
707
708
                                -Wunreachable-code*/
731
732
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
732
733
        perror_plus("if_indextoname");
733
734
      } else {
734
 
        fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIuMAX
735
 
                     "\n", ip, interface, (uintmax_t)port);
 
735
        fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16
 
736
                     "\n", ip, interface, port);
736
737
      }
737
738
    } else {
738
 
      fprintf_plus(stderr, "Connection to: %s, port %" PRIuMAX "\n",
739
 
                   ip, (uintmax_t)port);
 
739
      fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n",
 
740
                   ip, port);
740
741
    }
741
742
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
742
743
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
1003
1004
                             AVAHI_GCC_UNUSED AvahiLookupResultFlags
1004
1005
                             flags,
1005
1006
                             AVAHI_GCC_UNUSED void* userdata){
1006
 
  if(r == NULL){
1007
 
    return;
1008
 
  }
 
1007
  assert(r);
1009
1008
  
1010
1009
  /* Called whenever a service has been resolved successfully or
1011
1010
     timed out */
1032
1031
                     PRIdMAX ") on port %" PRIu16 "\n", name,
1033
1032
                     host_name, ip, (intmax_t)interface, port);
1034
1033
      }
1035
 
      int ret = start_mandos_communication(ip, (in_port_t)port,
1036
 
                                           interface,
 
1034
      int ret = start_mandos_communication(ip, port, interface,
1037
1035
                                           avahi_proto_to_af(proto));
1038
1036
      if(ret == 0){
1039
 
        avahi_simple_poll_quit(simple_poll);
 
1037
        avahi_simple_poll_quit(mc.simple_poll);
1040
1038
      } else {
1041
 
        if(not add_server(ip, (in_port_t)port, interface,
 
1039
        if(not add_server(ip, port, interface,
1042
1040
                          avahi_proto_to_af(proto))){
1043
1041
          fprintf_plus(stderr, "Failed to add server \"%s\" to server"
1044
1042
                       " list\n", name);
1059
1057
                            AVAHI_GCC_UNUSED AvahiLookupResultFlags
1060
1058
                            flags,
1061
1059
                            AVAHI_GCC_UNUSED void* userdata){
1062
 
  if(b == NULL){
1063
 
    return;
1064
 
  }
 
1060
  assert(b);
1065
1061
  
1066
1062
  /* Called whenever a new services becomes available on the LAN or
1067
1063
     is removed from the LAN */
1076
1072
    
1077
1073
    fprintf_plus(stderr, "(Avahi browser) %s\n",
1078
1074
                 avahi_strerror(avahi_server_errno(mc.server)));
1079
 
    avahi_simple_poll_quit(simple_poll);
 
1075
    avahi_simple_poll_quit(mc.simple_poll);
1080
1076
    return;
1081
1077
    
1082
1078
  case AVAHI_BROWSER_NEW:
1115
1111
  signal_received = sig;
1116
1112
  int old_errno = errno;
1117
1113
  /* set main loop to exit */
1118
 
  if(simple_poll != NULL){
1119
 
    avahi_simple_poll_quit(simple_poll);
 
1114
  if(mc.simple_poll != NULL){
 
1115
    avahi_simple_poll_quit(mc.simple_poll);
1120
1116
  }
1121
1117
  errno = old_errno;
1122
1118
}
1123
1119
 
1124
1120
bool get_flags(const char *ifname, struct ifreq *ifr){
1125
1121
  int ret;
1126
 
  error_t ret_errno;
1127
1122
  
1128
1123
  int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1129
1124
  if(s < 0){
1130
 
    ret_errno = errno;
1131
1125
    perror_plus("socket");
1132
 
    errno = ret_errno;
1133
1126
    return false;
1134
1127
  }
1135
1128
  strcpy(ifr->ifr_name, ifname);
1136
1129
  ret = ioctl(s, SIOCGIFFLAGS, ifr);
1137
1130
  if(ret == -1){
1138
1131
    if(debug){
1139
 
      ret_errno = errno;
1140
1132
      perror_plus("ioctl SIOCGIFFLAGS");
1141
 
      errno = ret_errno;
1142
1133
    }
1143
1134
    return false;
1144
1135
  }
1213
1204
}
1214
1205
 
1215
1206
/* 
1216
 
 * This function determines if a network interface is up.
1217
 
 */
1218
 
bool interface_is_up(const char *interface){
1219
 
  struct ifreq ifr;
1220
 
  if(not get_flags(interface, &ifr)){
1221
 
    if(debug){
1222
 
      fprintf_plus(stderr, "Failed to get flags for interface "
1223
 
                   "\"%s\"\n", interface);
1224
 
    }
1225
 
    return false;
1226
 
  }
1227
 
  
1228
 
  return (bool)(ifr.ifr_flags & IFF_UP);
1229
 
}
1230
 
 
1231
 
/* 
1232
 
 * This function determines if a network interface is running
1233
 
 */
1234
 
bool interface_is_running(const char *interface){
1235
 
  struct ifreq ifr;
1236
 
  if(not get_flags(interface, &ifr)){
1237
 
    if(debug){
1238
 
      fprintf_plus(stderr, "Failed to get flags for interface "
1239
 
                   "\"%s\"\n", interface);
1240
 
    }
1241
 
    return false;
1242
 
  }
1243
 
  
1244
 
  return (bool)(ifr.ifr_flags & IFF_RUNNING);
 
1207
 * This function determines if a directory entry in /sys/class/net
 
1208
 * corresponds to an acceptable network device which is up.
 
1209
 * (This function is passed to scandir(3) as a filter function.)
 
1210
 */
 
1211
int up_interface(const struct dirent *if_entry){
 
1212
  if(if_entry->d_name[0] == '.'){
 
1213
    return 0;
 
1214
  }
 
1215
  
 
1216
  struct ifreq ifr;
 
1217
  if(not get_flags(if_entry->d_name, &ifr)){
 
1218
    if(debug){
 
1219
      fprintf_plus(stderr, "Failed to get flags for interface "
 
1220
                   "\"%s\"\n", if_entry->d_name);
 
1221
    }
 
1222
    return 0;
 
1223
  }
 
1224
  
 
1225
  /* Reject down interfaces */
 
1226
  if(not (ifr.ifr_flags & IFF_UP)){
 
1227
    if(debug){
 
1228
      fprintf_plus(stderr, "Rejecting down interface \"%s\"\n",
 
1229
                   if_entry->d_name);
 
1230
    }
 
1231
    return 0;
 
1232
  }
 
1233
  
 
1234
  /* Reject non-running interfaces */
 
1235
  if(not (ifr.ifr_flags & IFF_RUNNING)){
 
1236
    if(debug){
 
1237
      fprintf_plus(stderr, "Rejecting non-running interface \"%s\"\n",
 
1238
                   if_entry->d_name);
 
1239
    }
 
1240
    return 0;
 
1241
  }
 
1242
  
 
1243
  if(not good_flags(if_entry->d_name, &ifr)){
 
1244
    return 0;
 
1245
  }
 
1246
  return 1;
1245
1247
}
1246
1248
 
1247
1249
int notdotentries(const struct dirent *direntry){
1364
1366
                                         mc.current_server->if_index,
1365
1367
                                         mc.current_server->af);
1366
1368
        if(ret == 0){
1367
 
          avahi_simple_poll_quit(simple_poll);
 
1369
          avahi_simple_poll_quit(mc.simple_poll);
1368
1370
          return 0;
1369
1371
        }
1370
1372
        ret = clock_gettime(CLOCK_MONOTONIC,
1388
1390
  }
1389
1391
}
1390
1392
 
1391
 
/* Set effective uid to 0, return errno */
1392
 
error_t raise_privileges(void){
1393
 
  error_t old_errno = errno;
1394
 
  error_t ret_errno = 0;
1395
 
  if(seteuid(0) == -1){
1396
 
    ret_errno = errno;
1397
 
    perror_plus("seteuid");
1398
 
  }
1399
 
  errno = old_errno;
1400
 
  return ret_errno;
1401
 
}
1402
 
 
1403
 
/* Set effective and real user ID to 0.  Return errno. */
1404
 
error_t raise_privileges_permanently(void){
1405
 
  error_t old_errno = errno;
1406
 
  error_t ret_errno = raise_privileges();
1407
 
  if(ret_errno != 0){
1408
 
    errno = old_errno;
1409
 
    return ret_errno;
1410
 
  }
1411
 
  if(setuid(0) == -1){
1412
 
    ret_errno = errno;
1413
 
    perror_plus("seteuid");
1414
 
  }
1415
 
  errno = old_errno;
1416
 
  return ret_errno;
1417
 
}
1418
 
 
1419
 
/* Set effective user ID to unprivileged saved user ID */
1420
 
error_t lower_privileges(void){
1421
 
  error_t old_errno = errno;
1422
 
  error_t ret_errno = 0;
1423
 
  if(seteuid(uid) == -1){
1424
 
    ret_errno = errno;
1425
 
    perror_plus("seteuid");
1426
 
  }
1427
 
  errno = old_errno;
1428
 
  return ret_errno;
1429
 
}
1430
 
 
1431
 
/* Lower privileges permanently */
1432
 
error_t lower_privileges_permanently(void){
1433
 
  error_t old_errno = errno;
1434
 
  error_t ret_errno = 0;
1435
 
  if(setuid(uid) == -1){
1436
 
    ret_errno = errno;
1437
 
    perror_plus("setuid");
1438
 
  }
1439
 
  errno = old_errno;
1440
 
  return ret_errno;
1441
 
}
1442
 
 
1443
1393
bool run_network_hooks(const char *mode, const char *interface,
1444
1394
                       const float delay){
1445
1395
  struct dirent **direntries;
1448
1398
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1449
1399
                         alphasort);
1450
1400
  if(numhooks == -1){
1451
 
    if(errno == ENOENT){
1452
 
      if(debug){
1453
 
        fprintf_plus(stderr, "Network hook directory \"%s\" not"
1454
 
                     " found\n", hookdir);
1455
 
      }
1456
 
    } else {
1457
 
      perror_plus("scandir");
1458
 
    }
 
1401
    perror_plus("scandir");
1459
1402
  } else {
1460
1403
    int devnull = open("/dev/null", O_RDONLY);
1461
1404
    for(int i = 0; i < numhooks; i++){
1474
1417
      if(hook_pid == 0){
1475
1418
        /* Child */
1476
1419
        /* Raise privileges */
1477
 
        raise_privileges_permanently();
 
1420
        errno = 0;
 
1421
        ret = seteuid(0);
 
1422
        if(ret == -1){
 
1423
          perror_plus("seteuid");
 
1424
        }
 
1425
        /* Raise privileges even more */
 
1426
        errno = 0;
 
1427
        ret = setuid(0);
 
1428
        if(ret == -1){
 
1429
          perror_plus("setuid");
 
1430
        }
1478
1431
        /* Set group */
1479
1432
        errno = 0;
1480
1433
        ret = setgid(0);
1573
1526
  return true;
1574
1527
}
1575
1528
 
1576
 
error_t bring_up_interface(const char *const interface,
1577
 
                           const float delay){
1578
 
  int sd = -1;
1579
 
  error_t old_errno = errno;
1580
 
  error_t ret_errno = 0;
1581
 
  int ret, ret_setflags;
1582
 
  struct ifreq network;
1583
 
  unsigned int if_index = if_nametoindex(interface);
1584
 
  if(if_index == 0){
1585
 
    fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1586
 
    errno = old_errno;
1587
 
    return ENXIO;
1588
 
  }
1589
 
  
1590
 
  if(quit_now){
1591
 
    errno = old_errno;
1592
 
    return EINTR;
1593
 
  }
1594
 
  
1595
 
  if(not interface_is_up(interface)){
1596
 
    if(not get_flags(interface, &network) and debug){
1597
 
      ret_errno = errno;
1598
 
      fprintf_plus(stderr, "Failed to get flags for interface "
1599
 
                   "\"%s\"\n", interface);
1600
 
      return ret_errno;
1601
 
    }
1602
 
    network.ifr_flags |= IFF_UP;
1603
 
    
1604
 
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1605
 
    if(sd < 0){
1606
 
      ret_errno = errno;
1607
 
      perror_plus("socket");
1608
 
      errno = old_errno;
1609
 
      return ret_errno;
1610
 
    }
1611
 
  
1612
 
    if(quit_now){
1613
 
      close(sd);
1614
 
      errno = old_errno;
1615
 
      return EINTR;
1616
 
    }
1617
 
    
1618
 
    if(debug){
1619
 
      fprintf_plus(stderr, "Bringing up interface \"%s\"\n",
1620
 
                   interface);
1621
 
    }
1622
 
    
1623
 
    /* Raise priviliges */
1624
 
    raise_privileges();
1625
 
    
1626
 
#ifdef __linux__
1627
 
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1628
 
       messages about the network interface to mess up the prompt */
1629
 
    int ret_linux = klogctl(8, NULL, 5);
1630
 
    bool restore_loglevel = true;
1631
 
    if(ret_linux == -1){
1632
 
      restore_loglevel = false;
1633
 
      perror_plus("klogctl");
1634
 
    }
1635
 
#endif  /* __linux__ */
1636
 
    ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1637
 
    ret_errno = errno;
1638
 
#ifdef __linux__
1639
 
    if(restore_loglevel){
1640
 
      ret_linux = klogctl(7, NULL, 0);
1641
 
      if(ret_linux == -1){
1642
 
        perror_plus("klogctl");
1643
 
      }
1644
 
    }
1645
 
#endif  /* __linux__ */
1646
 
    
1647
 
    /* Lower privileges */
1648
 
    lower_privileges();
1649
 
    
1650
 
    /* Close the socket */
1651
 
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
1652
 
    if(ret == -1){
1653
 
      perror_plus("close");
1654
 
    }
1655
 
    
1656
 
    if(ret_setflags == -1){
1657
 
      errno = ret_errno;
1658
 
      perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1659
 
      errno = old_errno;
1660
 
      return ret_errno;
1661
 
    }
1662
 
  } else if(debug){
1663
 
    fprintf_plus(stderr, "Interface \"%s\" is already up; good\n",
1664
 
                 interface);
1665
 
  }
1666
 
  
1667
 
  /* Sleep checking until interface is running.
1668
 
     Check every 0.25s, up to total time of delay */
1669
 
  for(int i=0; i < delay * 4; i++){
1670
 
    if(interface_is_running(interface)){
1671
 
      break;
1672
 
    }
1673
 
    struct timespec sleeptime = { .tv_nsec = 250000000 };
1674
 
    ret = nanosleep(&sleeptime, NULL);
1675
 
    if(ret == -1 and errno != EINTR){
1676
 
      perror_plus("nanosleep");
1677
 
    }
1678
 
  }
1679
 
  
1680
 
  errno = old_errno;
1681
 
  return 0;
1682
 
}
1683
 
 
1684
 
error_t take_down_interface(const char *const interface){
1685
 
  int sd = -1;
1686
 
  error_t old_errno = errno;
1687
 
  error_t ret_errno = 0;
1688
 
  int ret, ret_setflags;
1689
 
  struct ifreq network;
1690
 
  unsigned int if_index = if_nametoindex(interface);
1691
 
  if(if_index == 0){
1692
 
    fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1693
 
    errno = old_errno;
1694
 
    return ENXIO;
1695
 
  }
1696
 
  if(interface_is_up(interface)){
1697
 
    if(not get_flags(interface, &network) and debug){
1698
 
      ret_errno = errno;
1699
 
      fprintf_plus(stderr, "Failed to get flags for interface "
1700
 
                   "\"%s\"\n", interface);
1701
 
      return ret_errno;
1702
 
    }
1703
 
    network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1704
 
    
1705
 
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1706
 
    if(sd < 0){
1707
 
      ret_errno = errno;
1708
 
      perror_plus("socket");
1709
 
      errno = old_errno;
1710
 
      return ret_errno;
1711
 
    }
1712
 
    
1713
 
    if(debug){
1714
 
      fprintf_plus(stderr, "Taking down interface \"%s\"\n",
1715
 
                   interface);
1716
 
    }
1717
 
    
1718
 
    /* Raise priviliges */
1719
 
    raise_privileges();
1720
 
    
1721
 
    ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1722
 
    ret_errno = errno;
1723
 
    
1724
 
    /* Lower privileges */
1725
 
    lower_privileges();
1726
 
    
1727
 
    /* Close the socket */
1728
 
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
1729
 
    if(ret == -1){
1730
 
      perror_plus("close");
1731
 
    }
1732
 
    
1733
 
    if(ret_setflags == -1){
1734
 
      errno = ret_errno;
1735
 
      perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
1736
 
      errno = old_errno;
1737
 
      return ret_errno;
1738
 
    }
1739
 
  } else if(debug){
1740
 
    fprintf_plus(stderr, "Interface \"%s\" is already down; odd\n",
1741
 
                 interface);
1742
 
  }
1743
 
  
1744
 
  errno = old_errno;
1745
 
  return 0;
1746
 
}
1747
 
 
1748
1529
int main(int argc, char *argv[]){
1749
1530
  AvahiSServiceBrowser *sb = NULL;
1750
 
  error_t ret_errno;
 
1531
  int error;
1751
1532
  int ret;
1752
1533
  intmax_t tmpmax;
1753
1534
  char *tmp;
1754
1535
  int exitcode = EXIT_SUCCESS;
1755
 
  char *interfaces = NULL;
1756
 
  size_t interfaces_size = 0;
1757
 
  char *interfaces_to_take_down = NULL;
1758
 
  size_t interfaces_to_take_down_size = 0;
 
1536
  const char *interface = "";
 
1537
  struct ifreq network;
 
1538
  int sd = -1;
 
1539
  bool take_down_interface = false;
 
1540
  uid_t uid;
 
1541
  gid_t gid;
1759
1542
  char tempdir[] = "/tmp/mandosXXXXXX";
1760
1543
  bool tempdir_created = false;
1761
1544
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1762
1545
  const char *seckey = PATHDIR "/" SECKEY;
1763
1546
  const char *pubkey = PATHDIR "/" PUBKEY;
1764
 
  char *interfaces_hooks = NULL;
1765
 
  size_t interfaces_hooks_size = 0;
1766
1547
  
1767
1548
  bool gnutls_initialized = false;
1768
1549
  bool gpgme_initialized = false;
1859
1640
        connect_to = arg;
1860
1641
        break;
1861
1642
      case 'i':                 /* --interface */
1862
 
        ret_errno = argz_add_sep(&interfaces, &interfaces_size, arg,
1863
 
                                 (int)',');
1864
 
        if(ret_errno != 0){
1865
 
          argp_error(state, "%s", strerror(ret_errno));
1866
 
        }
 
1643
        interface = arg;
1867
1644
        break;
1868
1645
      case 's':                 /* --seckey */
1869
1646
        seckey = arg;
1947
1724
       <http://bugs.debian.org/633582> */
1948
1725
    
1949
1726
    /* Re-raise priviliges */
1950
 
    if(raise_privileges() == 0){
 
1727
    errno = 0;
 
1728
    ret = seteuid(0);
 
1729
    if(ret == -1){
 
1730
      perror_plus("seteuid");
 
1731
    } else {
1951
1732
      struct stat st;
1952
1733
      
1953
1734
      if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
2001
1782
    }
2002
1783
  }
2003
1784
  
2004
 
  /* Remove empty interface names */
2005
 
  {
2006
 
    char *interface = NULL;
2007
 
    while((interface = argz_next(interfaces, interfaces_size,
2008
 
                                 interface))){
2009
 
      if(if_nametoindex(interface) == 0){
2010
 
        if(interface[0] != '\0' and strcmp(interface, "none") != 0){
2011
 
          fprintf_plus(stderr, "Not using nonexisting interface"
2012
 
                       " \"%s\"\n", interface);
2013
 
        }
2014
 
        argz_delete(&interfaces, &interfaces_size, interface);
2015
 
        interface = NULL;
2016
 
      }
2017
 
    }
2018
 
  }
2019
 
  
2020
1785
  /* Run network hooks */
2021
 
  {
2022
 
    
2023
 
    if(interfaces != NULL){
2024
 
      interfaces_hooks = malloc(interfaces_size);
2025
 
      if(interfaces_hooks == NULL){
2026
 
        perror_plus("malloc");
2027
 
        goto end;
2028
 
      }
2029
 
      memcpy(interfaces_hooks, interfaces, interfaces_size);
2030
 
      interfaces_hooks_size = interfaces_size;
2031
 
      argz_stringify(interfaces_hooks, interfaces_hooks_size,
2032
 
                     (int)',');
2033
 
    }
2034
 
    if(not run_network_hooks("start", interfaces_hooks != NULL ?
2035
 
                             interfaces_hooks : "", delay)){
2036
 
      goto end;
2037
 
    }
 
1786
  if(not run_network_hooks("start", interface, delay)){
 
1787
    goto end;
2038
1788
  }
2039
1789
  
2040
1790
  if(not debug){
2041
1791
    avahi_set_log_function(empty_log);
2042
1792
  }
2043
1793
  
 
1794
  if(interface[0] == '\0'){
 
1795
    struct dirent **direntries;
 
1796
    /* First look for interfaces that are up */
 
1797
    ret = scandir(sys_class_net, &direntries, up_interface,
 
1798
                  alphasort);
 
1799
    if(ret == 0){
 
1800
      /* No up interfaces, look for any good interfaces */
 
1801
      free(direntries);
 
1802
      ret = scandir(sys_class_net, &direntries, good_interface,
 
1803
                    alphasort);
 
1804
    }
 
1805
    if(ret >= 1){
 
1806
      /* Pick the first interface returned */
 
1807
      interface = strdup(direntries[0]->d_name);
 
1808
      if(debug){
 
1809
        fprintf_plus(stderr, "Using interface \"%s\"\n", interface);
 
1810
      }
 
1811
      if(interface == NULL){
 
1812
        perror_plus("malloc");
 
1813
        free(direntries);
 
1814
        exitcode = EXIT_FAILURE;
 
1815
        goto end;
 
1816
      }
 
1817
      free(direntries);
 
1818
    } else {
 
1819
      free(direntries);
 
1820
      fprintf_plus(stderr, "Could not find a network interface\n");
 
1821
      exitcode = EXIT_FAILURE;
 
1822
      goto end;
 
1823
    }
 
1824
  }
 
1825
  
2044
1826
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
2045
1827
     from the signal handler */
2046
1828
  /* Initialize the pseudo-RNG for Avahi */
2047
1829
  srand((unsigned int) time(NULL));
2048
 
  simple_poll = avahi_simple_poll_new();
2049
 
  if(simple_poll == NULL){
 
1830
  mc.simple_poll = avahi_simple_poll_new();
 
1831
  if(mc.simple_poll == NULL){
2050
1832
    fprintf_plus(stderr,
2051
1833
                 "Avahi: Failed to create simple poll object.\n");
2052
1834
    exitcode = EX_UNAVAILABLE;
2116
1898
    }
2117
1899
  }
2118
1900
  
2119
 
  /* If no interfaces were specified, make a list */
2120
 
  if(interfaces == NULL){
2121
 
    struct dirent **direntries;
2122
 
    /* Look for any good interfaces */
2123
 
    ret = scandir(sys_class_net, &direntries, good_interface,
2124
 
                  alphasort);
2125
 
    if(ret >= 1){
2126
 
      /* Add all found interfaces to interfaces list */
2127
 
      for(int i = 0; i < ret; ++i){
2128
 
        ret_errno = argz_add(&interfaces, &interfaces_size,
2129
 
                             direntries[i]->d_name);
2130
 
        if(ret_errno != 0){
2131
 
          perror_plus("argz_add");
2132
 
          continue;
2133
 
        }
2134
 
        if(debug){
2135
 
          fprintf_plus(stderr, "Will use interface \"%s\"\n",
2136
 
                       direntries[i]->d_name);
2137
 
        }
2138
 
      }
2139
 
      free(direntries);
2140
 
    } else {
2141
 
      free(direntries);
2142
 
      fprintf_plus(stderr, "Could not find a network interface\n");
2143
 
      exitcode = EXIT_FAILURE;
2144
 
      goto end;
2145
 
    }
2146
 
  }
2147
 
  
2148
 
  /* If we only got one interface, explicitly use only that one */
2149
 
  if(argz_count(interfaces, interfaces_size) == 1){
2150
 
    if(debug){
2151
 
      fprintf_plus(stderr, "Using only interface \"%s\"\n",
2152
 
                   interfaces);
2153
 
    }
2154
 
    if_index = (AvahiIfIndex)if_nametoindex(interfaces);
2155
 
  }
2156
 
  
2157
 
  /* Bring up interfaces which are down */
2158
 
  if(not (argz_count(interfaces, interfaces_size) == 1
2159
 
          and strcmp(interfaces, "none") == 0)){
2160
 
    char *interface = NULL;
2161
 
    while((interface = argz_next(interfaces, interfaces_size,
2162
 
                                 interface))){
2163
 
      bool interface_was_up = interface_is_up(interface);
2164
 
      ret = bring_up_interface(interface, delay);
2165
 
      if(not interface_was_up){
2166
 
        if(ret != 0){
2167
 
          errno = ret;
2168
 
          perror_plus("Failed to bring up interface");
2169
 
        } else {
2170
 
          ret_errno = argz_add(&interfaces_to_take_down,
2171
 
                               &interfaces_to_take_down_size,
2172
 
                               interface);
2173
 
        }
2174
 
      }
2175
 
    }
2176
 
    free(interfaces);
2177
 
    interfaces = NULL;
2178
 
    interfaces_size = 0;
2179
 
    if(debug and (interfaces_to_take_down == NULL)){
2180
 
      fprintf_plus(stderr, "No interfaces were brought up\n");
 
1901
  /* If the interface is down, bring it up */
 
1902
  if(strcmp(interface, "none") != 0){
 
1903
    if_index = (AvahiIfIndex) if_nametoindex(interface);
 
1904
    if(if_index == 0){
 
1905
      fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
 
1906
      exitcode = EX_UNAVAILABLE;
 
1907
      goto end;
 
1908
    }
 
1909
    
 
1910
    if(quit_now){
 
1911
      goto end;
 
1912
    }
 
1913
    
 
1914
    /* Re-raise priviliges */
 
1915
    errno = 0;
 
1916
    ret = seteuid(0);
 
1917
    if(ret == -1){
 
1918
      perror_plus("seteuid");
 
1919
    }
 
1920
    
 
1921
#ifdef __linux__
 
1922
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
 
1923
       messages about the network interface to mess up the prompt */
 
1924
    ret = klogctl(8, NULL, 5);
 
1925
    bool restore_loglevel = true;
 
1926
    if(ret == -1){
 
1927
      restore_loglevel = false;
 
1928
      perror_plus("klogctl");
 
1929
    }
 
1930
#endif  /* __linux__ */
 
1931
    
 
1932
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
1933
    if(sd < 0){
 
1934
      perror_plus("socket");
 
1935
      exitcode = EX_OSERR;
 
1936
#ifdef __linux__
 
1937
      if(restore_loglevel){
 
1938
        ret = klogctl(7, NULL, 0);
 
1939
        if(ret == -1){
 
1940
          perror_plus("klogctl");
 
1941
        }
 
1942
      }
 
1943
#endif  /* __linux__ */
 
1944
      /* Lower privileges */
 
1945
      errno = 0;
 
1946
      ret = seteuid(uid);
 
1947
      if(ret == -1){
 
1948
        perror_plus("seteuid");
 
1949
      }
 
1950
      goto end;
 
1951
    }
 
1952
    strcpy(network.ifr_name, interface);
 
1953
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
1954
    if(ret == -1){
 
1955
      perror_plus("ioctl SIOCGIFFLAGS");
 
1956
#ifdef __linux__
 
1957
      if(restore_loglevel){
 
1958
        ret = klogctl(7, NULL, 0);
 
1959
        if(ret == -1){
 
1960
          perror_plus("klogctl");
 
1961
        }
 
1962
      }
 
1963
#endif  /* __linux__ */
 
1964
      exitcode = EX_OSERR;
 
1965
      /* Lower privileges */
 
1966
      errno = 0;
 
1967
      ret = seteuid(uid);
 
1968
      if(ret == -1){
 
1969
        perror_plus("seteuid");
 
1970
      }
 
1971
      goto end;
 
1972
    }
 
1973
    if((network.ifr_flags & IFF_UP) == 0){
 
1974
      network.ifr_flags |= IFF_UP;
 
1975
      take_down_interface = true;
 
1976
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
1977
      if(ret == -1){
 
1978
        take_down_interface = false;
 
1979
        perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
 
1980
        exitcode = EX_OSERR;
 
1981
#ifdef __linux__
 
1982
        if(restore_loglevel){
 
1983
          ret = klogctl(7, NULL, 0);
 
1984
          if(ret == -1){
 
1985
            perror_plus("klogctl");
 
1986
          }
 
1987
        }
 
1988
#endif  /* __linux__ */
 
1989
        /* Lower privileges */
 
1990
        errno = 0;
 
1991
        ret = seteuid(uid);
 
1992
        if(ret == -1){
 
1993
          perror_plus("seteuid");
 
1994
        }
 
1995
        goto end;
 
1996
      }
 
1997
    }
 
1998
    /* Sleep checking until interface is running.
 
1999
       Check every 0.25s, up to total time of delay */
 
2000
    for(int i=0; i < delay * 4; i++){
 
2001
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
2002
      if(ret == -1){
 
2003
        perror_plus("ioctl SIOCGIFFLAGS");
 
2004
      } else if(network.ifr_flags & IFF_RUNNING){
 
2005
        break;
 
2006
      }
 
2007
      struct timespec sleeptime = { .tv_nsec = 250000000 };
 
2008
      ret = nanosleep(&sleeptime, NULL);
 
2009
      if(ret == -1 and errno != EINTR){
 
2010
        perror_plus("nanosleep");
 
2011
      }
 
2012
    }
 
2013
    if(not take_down_interface){
 
2014
      /* We won't need the socket anymore */
 
2015
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
2016
      if(ret == -1){
 
2017
        perror_plus("close");
 
2018
      }
 
2019
    }
 
2020
#ifdef __linux__
 
2021
    if(restore_loglevel){
 
2022
      /* Restores kernel loglevel to default */
 
2023
      ret = klogctl(7, NULL, 0);
 
2024
      if(ret == -1){
 
2025
        perror_plus("klogctl");
 
2026
      }
 
2027
    }
 
2028
#endif  /* __linux__ */
 
2029
    /* Lower privileges */
 
2030
    errno = 0;
 
2031
    /* Lower privileges */
 
2032
    ret = seteuid(uid);
 
2033
    if(ret == -1){
 
2034
      perror_plus("seteuid");
2181
2035
    }
2182
2036
  }
2183
2037
  
2224
2078
    /* Connect directly, do not use Zeroconf */
2225
2079
    /* (Mainly meant for debugging) */
2226
2080
    char *address = strrchr(connect_to, ':');
2227
 
    
2228
2081
    if(address == NULL){
2229
2082
      fprintf_plus(stderr, "No colon in address\n");
2230
2083
      exitcode = EX_USAGE;
2235
2088
      goto end;
2236
2089
    }
2237
2090
    
2238
 
    in_port_t port;
 
2091
    uint16_t port;
2239
2092
    errno = 0;
2240
2093
    tmpmax = strtoimax(address+1, &tmp, 10);
2241
2094
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
2242
 
       or tmpmax != (in_port_t)tmpmax){
 
2095
       or tmpmax != (uint16_t)tmpmax){
2243
2096
      fprintf_plus(stderr, "Bad port number\n");
2244
2097
      exitcode = EX_USAGE;
2245
2098
      goto end;
2249
2102
      goto end;
2250
2103
    }
2251
2104
    
2252
 
    port = (in_port_t)tmpmax;
 
2105
    port = (uint16_t)tmpmax;
2253
2106
    *address = '\0';
2254
2107
    /* Colon in address indicates IPv6 */
2255
2108
    int af;
2303
2156
    config.publish_domain = 0;
2304
2157
    
2305
2158
    /* Allocate a new server */
2306
 
    mc.server = avahi_server_new(avahi_simple_poll_get(simple_poll),
2307
 
                                 &config, NULL, NULL, &ret_errno);
 
2159
    mc.server = avahi_server_new(avahi_simple_poll_get
 
2160
                                 (mc.simple_poll), &config, NULL,
 
2161
                                 NULL, &error);
2308
2162
    
2309
2163
    /* Free the Avahi configuration data */
2310
2164
    avahi_server_config_free(&config);
2313
2167
  /* Check if creating the Avahi server object succeeded */
2314
2168
  if(mc.server == NULL){
2315
2169
    fprintf_plus(stderr, "Failed to create Avahi server: %s\n",
2316
 
                 avahi_strerror(ret_errno));
 
2170
                 avahi_strerror(error));
2317
2171
    exitcode = EX_UNAVAILABLE;
2318
2172
    goto end;
2319
2173
  }
2343
2197
    fprintf_plus(stderr, "Starting Avahi loop search\n");
2344
2198
  }
2345
2199
 
2346
 
  ret = avahi_loop_with_timeout(simple_poll,
 
2200
  ret = avahi_loop_with_timeout(mc.simple_poll,
2347
2201
                                (int)(retry_interval * 1000));
2348
2202
  if(debug){
2349
2203
    fprintf_plus(stderr, "avahi_loop_with_timeout exited %s\n",
2363
2217
  if(mc.server != NULL)
2364
2218
    avahi_server_free(mc.server);
2365
2219
  
2366
 
  if(simple_poll != NULL)
2367
 
    avahi_simple_poll_free(simple_poll);
 
2220
  if(mc.simple_poll != NULL)
 
2221
    avahi_simple_poll_free(mc.simple_poll);
2368
2222
  
2369
2223
  if(gnutls_initialized){
2370
2224
    gnutls_certificate_free_credentials(mc.cred);
2387
2241
    }
2388
2242
  }
2389
2243
  
 
2244
  /* Run network hooks */
 
2245
  run_network_hooks("stop", interface, delay);
 
2246
  
2390
2247
  /* Re-raise priviliges */
2391
2248
  {
2392
 
    raise_privileges();
2393
 
    
2394
 
    /* Run network hooks */
2395
 
    run_network_hooks("stop", interfaces_hooks != NULL ?
2396
 
                      interfaces_hooks : "", delay);
2397
 
    
2398
 
    /* Take down the network interfaces which were brought up */
2399
 
    {
2400
 
      char *interface = NULL;
2401
 
      while((interface=argz_next(interfaces_to_take_down,
2402
 
                                 interfaces_to_take_down_size,
2403
 
                                 interface))){
2404
 
        ret_errno = take_down_interface(interface);
2405
 
        if(ret_errno != 0){
2406
 
          errno = ret_errno;
2407
 
          perror_plus("Failed to take down interface");
 
2249
    errno = 0;
 
2250
    ret = seteuid(0);
 
2251
    if(ret == -1){
 
2252
      perror_plus("seteuid");
 
2253
    }
 
2254
    
 
2255
    /* Take down the network interface */
 
2256
    if(take_down_interface and geteuid() == 0){
 
2257
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
2258
      if(ret == -1){
 
2259
        perror_plus("ioctl SIOCGIFFLAGS");
 
2260
      } else if(network.ifr_flags & IFF_UP){
 
2261
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
 
2262
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
2263
        if(ret == -1){
 
2264
          perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
2408
2265
        }
2409
2266
      }
2410
 
      if(debug and (interfaces_to_take_down == NULL)){
2411
 
        fprintf_plus(stderr, "No interfaces needed to be taken"
2412
 
                     " down\n");
 
2267
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
2268
      if(ret == -1){
 
2269
        perror_plus("close");
2413
2270
      }
2414
2271
    }
2415
 
    
2416
 
    lower_privileges_permanently();
2417
 
  }
2418
 
  
2419
 
  free(interfaces_to_take_down);
2420
 
  free(interfaces_hooks);
 
2272
  }
 
2273
  /* Lower privileges permanently */
 
2274
  errno = 0;
 
2275
  ret = setuid(uid);
 
2276
  if(ret == -1){
 
2277
    perror_plus("setuid");
 
2278
  }
2421
2279
  
2422
2280
  /* Removes the GPGME temp directory and all files inside */
2423
2281
  if(tempdir_created){