/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

* mandos (Client.runtime_expansions): Add "expires" and (bug fix)
                                      "last_approval_request".
  (main): Don't ignore SIGINT.
* mandos-clients.conf.xml (RUNTIME EXPANSION): Added "expires".

Show diffs side-by-side

added added

removed removed

Lines of Context:
41
41
 
42
42
#include <stdio.h>              /* fprintf(), stderr, fwrite(),
43
43
                                   stdout, ferror(), remove() */
44
 
#include <stdint.h>             /* uint16_t, uint32_t, intptr_t */
 
44
#include <stdint.h>             /* uint16_t, uint32_t */
45
45
#include <stddef.h>             /* NULL, size_t, ssize_t */
46
46
#include <stdlib.h>             /* free(), EXIT_SUCCESS, srand(),
47
47
                                   strtof(), abort() */
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] = "";
820
821
    goto mandos_end;
821
822
  }
822
823
  
823
 
  /* This casting via intptr_t is to eliminate warning about casting
824
 
     an int to a pointer type.  This is exactly how the GnuTLS Guile
825
 
     function "set-session-transport-fd!" does it. */
826
 
  gnutls_transport_set_ptr(session,
827
 
                           (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);
828
826
  
829
827
  if(quit_now){
830
828
    errno = EINTR;
1003
1001
                             AVAHI_GCC_UNUSED AvahiLookupResultFlags
1004
1002
                             flags,
1005
1003
                             AVAHI_GCC_UNUSED void* userdata){
1006
 
  if(r == NULL){
1007
 
    return;
1008
 
  }
 
1004
  assert(r);
1009
1005
  
1010
1006
  /* Called whenever a service has been resolved successfully or
1011
1007
     timed out */
1032
1028
                     PRIdMAX ") on port %" PRIu16 "\n", name,
1033
1029
                     host_name, ip, (intmax_t)interface, port);
1034
1030
      }
1035
 
      int ret = start_mandos_communication(ip, (in_port_t)port,
1036
 
                                           interface,
 
1031
      int ret = start_mandos_communication(ip, port, interface,
1037
1032
                                           avahi_proto_to_af(proto));
1038
1033
      if(ret == 0){
1039
 
        avahi_simple_poll_quit(simple_poll);
 
1034
        avahi_simple_poll_quit(mc.simple_poll);
1040
1035
      } else {
1041
 
        if(not add_server(ip, (in_port_t)port, interface,
 
1036
        if(not add_server(ip, port, interface,
1042
1037
                          avahi_proto_to_af(proto))){
1043
1038
          fprintf_plus(stderr, "Failed to add server \"%s\" to server"
1044
1039
                       " list\n", name);
1059
1054
                            AVAHI_GCC_UNUSED AvahiLookupResultFlags
1060
1055
                            flags,
1061
1056
                            AVAHI_GCC_UNUSED void* userdata){
1062
 
  if(b == NULL){
1063
 
    return;
1064
 
  }
 
1057
  assert(b);
1065
1058
  
1066
1059
  /* Called whenever a new services becomes available on the LAN or
1067
1060
     is removed from the LAN */
1076
1069
    
1077
1070
    fprintf_plus(stderr, "(Avahi browser) %s\n",
1078
1071
                 avahi_strerror(avahi_server_errno(mc.server)));
1079
 
    avahi_simple_poll_quit(simple_poll);
 
1072
    avahi_simple_poll_quit(mc.simple_poll);
1080
1073
    return;
1081
1074
    
1082
1075
  case AVAHI_BROWSER_NEW:
1115
1108
  signal_received = sig;
1116
1109
  int old_errno = errno;
1117
1110
  /* set main loop to exit */
1118
 
  if(simple_poll != NULL){
1119
 
    avahi_simple_poll_quit(simple_poll);
 
1111
  if(mc.simple_poll != NULL){
 
1112
    avahi_simple_poll_quit(mc.simple_poll);
1120
1113
  }
1121
1114
  errno = old_errno;
1122
1115
}
1123
1116
 
1124
1117
bool get_flags(const char *ifname, struct ifreq *ifr){
1125
1118
  int ret;
1126
 
  error_t ret_errno;
1127
1119
  
1128
1120
  int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1129
1121
  if(s < 0){
1130
 
    ret_errno = errno;
1131
1122
    perror_plus("socket");
1132
 
    errno = ret_errno;
1133
1123
    return false;
1134
1124
  }
1135
1125
  strcpy(ifr->ifr_name, ifname);
1136
1126
  ret = ioctl(s, SIOCGIFFLAGS, ifr);
1137
1127
  if(ret == -1){
1138
1128
    if(debug){
1139
 
      ret_errno = errno;
1140
1129
      perror_plus("ioctl SIOCGIFFLAGS");
1141
 
      errno = ret_errno;
1142
1130
    }
1143
1131
    return false;
1144
1132
  }
1213
1201
}
1214
1202
 
1215
1203
/* 
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);
 
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;
1245
1244
}
1246
1245
 
1247
1246
int notdotentries(const struct dirent *direntry){
1364
1363
                                         mc.current_server->if_index,
1365
1364
                                         mc.current_server->af);
1366
1365
        if(ret == 0){
1367
 
          avahi_simple_poll_quit(simple_poll);
 
1366
          avahi_simple_poll_quit(mc.simple_poll);
1368
1367
          return 0;
1369
1368
        }
1370
1369
        ret = clock_gettime(CLOCK_MONOTONIC,
1388
1387
  }
1389
1388
}
1390
1389
 
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
1390
bool run_network_hooks(const char *mode, const char *interface,
1444
1391
                       const float delay){
1445
1392
  struct dirent **direntries;
1448
1395
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1449
1396
                         alphasort);
1450
1397
  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
 
    }
 
1398
    perror_plus("scandir");
1459
1399
  } else {
1460
1400
    int devnull = open("/dev/null", O_RDONLY);
1461
1401
    for(int i = 0; i < numhooks; i++){
1474
1414
      if(hook_pid == 0){
1475
1415
        /* Child */
1476
1416
        /* Raise privileges */
1477
 
        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
        }
1478
1428
        /* Set group */
1479
1429
        errno = 0;
1480
1430
        ret = setgid(0);
1573
1523
  return true;
1574
1524
}
1575
1525
 
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
1526
int main(int argc, char *argv[]){
1749
1527
  AvahiSServiceBrowser *sb = NULL;
1750
 
  error_t ret_errno;
 
1528
  int error;
1751
1529
  int ret;
1752
1530
  intmax_t tmpmax;
1753
1531
  char *tmp;
1754
1532
  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;
 
1533
  const char *interface = "";
 
1534
  struct ifreq network;
 
1535
  int sd = -1;
 
1536
  bool take_down_interface = false;
 
1537
  uid_t uid;
 
1538
  gid_t gid;
1759
1539
  char tempdir[] = "/tmp/mandosXXXXXX";
1760
1540
  bool tempdir_created = false;
1761
1541
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1762
1542
  const char *seckey = PATHDIR "/" SECKEY;
1763
1543
  const char *pubkey = PATHDIR "/" PUBKEY;
1764
 
  char *interfaces_hooks = NULL;
1765
 
  size_t interfaces_hooks_size = 0;
1766
1544
  
1767
1545
  bool gnutls_initialized = false;
1768
1546
  bool gpgme_initialized = false;
1859
1637
        connect_to = arg;
1860
1638
        break;
1861
1639
      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
 
        }
 
1640
        interface = arg;
1867
1641
        break;
1868
1642
      case 's':                 /* --seckey */
1869
1643
        seckey = arg;
1947
1721
       <http://bugs.debian.org/633582> */
1948
1722
    
1949
1723
    /* Re-raise priviliges */
1950
 
    if(raise_privileges() == 0){
 
1724
    errno = 0;
 
1725
    ret = seteuid(0);
 
1726
    if(ret == -1){
 
1727
      perror_plus("seteuid");
 
1728
    } else {
1951
1729
      struct stat st;
1952
1730
      
1953
1731
      if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
2001
1779
    }
2002
1780
  }
2003
1781
  
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
1782
  /* 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
 
    }
 
1783
  if(not run_network_hooks("start", interface, delay)){
 
1784
    goto end;
2038
1785
  }
2039
1786
  
2040
1787
  if(not debug){
2041
1788
    avahi_set_log_function(empty_log);
2042
1789
  }
2043
1790
  
 
1791
  if(interface[0] == '\0'){
 
1792
    struct dirent **direntries;
 
1793
    /* First look for interfaces that are up */
 
1794
    ret = scandir(sys_class_net, &direntries, up_interface,
 
1795
                  alphasort);
 
1796
    if(ret == 0){
 
1797
      /* No up interfaces, look for any good interfaces */
 
1798
      free(direntries);
 
1799
      ret = scandir(sys_class_net, &direntries, good_interface,
 
1800
                    alphasort);
 
1801
    }
 
1802
    if(ret >= 1){
 
1803
      /* Pick the first interface returned */
 
1804
      interface = strdup(direntries[0]->d_name);
 
1805
      if(debug){
 
1806
        fprintf_plus(stderr, "Using interface \"%s\"\n", interface);
 
1807
      }
 
1808
      if(interface == NULL){
 
1809
        perror_plus("malloc");
 
1810
        free(direntries);
 
1811
        exitcode = EXIT_FAILURE;
 
1812
        goto end;
 
1813
      }
 
1814
      free(direntries);
 
1815
    } else {
 
1816
      free(direntries);
 
1817
      fprintf_plus(stderr, "Could not find a network interface\n");
 
1818
      exitcode = EXIT_FAILURE;
 
1819
      goto end;
 
1820
    }
 
1821
  }
 
1822
  
2044
1823
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
2045
1824
     from the signal handler */
2046
1825
  /* Initialize the pseudo-RNG for Avahi */
2047
1826
  srand((unsigned int) time(NULL));
2048
 
  simple_poll = avahi_simple_poll_new();
2049
 
  if(simple_poll == NULL){
 
1827
  mc.simple_poll = avahi_simple_poll_new();
 
1828
  if(mc.simple_poll == NULL){
2050
1829
    fprintf_plus(stderr,
2051
1830
                 "Avahi: Failed to create simple poll object.\n");
2052
1831
    exitcode = EX_UNAVAILABLE;
2116
1895
    }
2117
1896
  }
2118
1897
  
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");
 
1898
  /* If the interface is down, bring it up */
 
1899
  if(strcmp(interface, "none") != 0){
 
1900
    if_index = (AvahiIfIndex) if_nametoindex(interface);
 
1901
    if(if_index == 0){
 
1902
      fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
 
1903
      exitcode = EX_UNAVAILABLE;
 
1904
      goto end;
 
1905
    }
 
1906
    
 
1907
    if(quit_now){
 
1908
      goto end;
 
1909
    }
 
1910
    
 
1911
    /* Re-raise priviliges */
 
1912
    errno = 0;
 
1913
    ret = seteuid(0);
 
1914
    if(ret == -1){
 
1915
      perror_plus("seteuid");
 
1916
    }
 
1917
    
 
1918
#ifdef __linux__
 
1919
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
 
1920
       messages about the network interface to mess up the prompt */
 
1921
    ret = klogctl(8, NULL, 5);
 
1922
    bool restore_loglevel = true;
 
1923
    if(ret == -1){
 
1924
      restore_loglevel = false;
 
1925
      perror_plus("klogctl");
 
1926
    }
 
1927
#endif  /* __linux__ */
 
1928
    
 
1929
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
1930
    if(sd < 0){
 
1931
      perror_plus("socket");
 
1932
      exitcode = EX_OSERR;
 
1933
#ifdef __linux__
 
1934
      if(restore_loglevel){
 
1935
        ret = klogctl(7, NULL, 0);
 
1936
        if(ret == -1){
 
1937
          perror_plus("klogctl");
 
1938
        }
 
1939
      }
 
1940
#endif  /* __linux__ */
 
1941
      /* Lower privileges */
 
1942
      errno = 0;
 
1943
      ret = seteuid(uid);
 
1944
      if(ret == -1){
 
1945
        perror_plus("seteuid");
 
1946
      }
 
1947
      goto end;
 
1948
    }
 
1949
    strcpy(network.ifr_name, interface);
 
1950
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
1951
    if(ret == -1){
 
1952
      perror_plus("ioctl SIOCGIFFLAGS");
 
1953
#ifdef __linux__
 
1954
      if(restore_loglevel){
 
1955
        ret = klogctl(7, NULL, 0);
 
1956
        if(ret == -1){
 
1957
          perror_plus("klogctl");
 
1958
        }
 
1959
      }
 
1960
#endif  /* __linux__ */
 
1961
      exitcode = EX_OSERR;
 
1962
      /* Lower privileges */
 
1963
      errno = 0;
 
1964
      ret = seteuid(uid);
 
1965
      if(ret == -1){
 
1966
        perror_plus("seteuid");
 
1967
      }
 
1968
      goto end;
 
1969
    }
 
1970
    if((network.ifr_flags & IFF_UP) == 0){
 
1971
      network.ifr_flags |= IFF_UP;
 
1972
      take_down_interface = true;
 
1973
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
1974
      if(ret == -1){
 
1975
        take_down_interface = false;
 
1976
        perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
 
1977
        exitcode = EX_OSERR;
 
1978
#ifdef __linux__
 
1979
        if(restore_loglevel){
 
1980
          ret = klogctl(7, NULL, 0);
 
1981
          if(ret == -1){
 
1982
            perror_plus("klogctl");
 
1983
          }
 
1984
        }
 
1985
#endif  /* __linux__ */
 
1986
        /* Lower privileges */
 
1987
        errno = 0;
 
1988
        ret = seteuid(uid);
 
1989
        if(ret == -1){
 
1990
          perror_plus("seteuid");
 
1991
        }
 
1992
        goto end;
 
1993
      }
 
1994
    }
 
1995
    /* Sleep checking until interface is running.
 
1996
       Check every 0.25s, up to total time of delay */
 
1997
    for(int i=0; i < delay * 4; i++){
 
1998
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
1999
      if(ret == -1){
 
2000
        perror_plus("ioctl SIOCGIFFLAGS");
 
2001
      } else if(network.ifr_flags & IFF_RUNNING){
 
2002
        break;
 
2003
      }
 
2004
      struct timespec sleeptime = { .tv_nsec = 250000000 };
 
2005
      ret = nanosleep(&sleeptime, NULL);
 
2006
      if(ret == -1 and errno != EINTR){
 
2007
        perror_plus("nanosleep");
 
2008
      }
 
2009
    }
 
2010
    if(not take_down_interface){
 
2011
      /* We won't need the socket anymore */
 
2012
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
2013
      if(ret == -1){
 
2014
        perror_plus("close");
 
2015
      }
 
2016
    }
 
2017
#ifdef __linux__
 
2018
    if(restore_loglevel){
 
2019
      /* Restores kernel loglevel to default */
 
2020
      ret = klogctl(7, NULL, 0);
 
2021
      if(ret == -1){
 
2022
        perror_plus("klogctl");
 
2023
      }
 
2024
    }
 
2025
#endif  /* __linux__ */
 
2026
    /* Lower privileges */
 
2027
    errno = 0;
 
2028
    /* Lower privileges */
 
2029
    ret = seteuid(uid);
 
2030
    if(ret == -1){
 
2031
      perror_plus("seteuid");
2181
2032
    }
2182
2033
  }
2183
2034
  
2224
2075
    /* Connect directly, do not use Zeroconf */
2225
2076
    /* (Mainly meant for debugging) */
2226
2077
    char *address = strrchr(connect_to, ':');
2227
 
    
2228
2078
    if(address == NULL){
2229
2079
      fprintf_plus(stderr, "No colon in address\n");
2230
2080
      exitcode = EX_USAGE;
2235
2085
      goto end;
2236
2086
    }
2237
2087
    
2238
 
    in_port_t port;
 
2088
    uint16_t port;
2239
2089
    errno = 0;
2240
2090
    tmpmax = strtoimax(address+1, &tmp, 10);
2241
2091
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
2242
 
       or tmpmax != (in_port_t)tmpmax){
 
2092
       or tmpmax != (uint16_t)tmpmax){
2243
2093
      fprintf_plus(stderr, "Bad port number\n");
2244
2094
      exitcode = EX_USAGE;
2245
2095
      goto end;
2249
2099
      goto end;
2250
2100
    }
2251
2101
    
2252
 
    port = (in_port_t)tmpmax;
 
2102
    port = (uint16_t)tmpmax;
2253
2103
    *address = '\0';
2254
2104
    /* Colon in address indicates IPv6 */
2255
2105
    int af;
2303
2153
    config.publish_domain = 0;
2304
2154
    
2305
2155
    /* Allocate a new server */
2306
 
    mc.server = avahi_server_new(avahi_simple_poll_get(simple_poll),
2307
 
                                 &config, NULL, NULL, &ret_errno);
 
2156
    mc.server = avahi_server_new(avahi_simple_poll_get
 
2157
                                 (mc.simple_poll), &config, NULL,
 
2158
                                 NULL, &error);
2308
2159
    
2309
2160
    /* Free the Avahi configuration data */
2310
2161
    avahi_server_config_free(&config);
2313
2164
  /* Check if creating the Avahi server object succeeded */
2314
2165
  if(mc.server == NULL){
2315
2166
    fprintf_plus(stderr, "Failed to create Avahi server: %s\n",
2316
 
                 avahi_strerror(ret_errno));
 
2167
                 avahi_strerror(error));
2317
2168
    exitcode = EX_UNAVAILABLE;
2318
2169
    goto end;
2319
2170
  }
2343
2194
    fprintf_plus(stderr, "Starting Avahi loop search\n");
2344
2195
  }
2345
2196
 
2346
 
  ret = avahi_loop_with_timeout(simple_poll,
 
2197
  ret = avahi_loop_with_timeout(mc.simple_poll,
2347
2198
                                (int)(retry_interval * 1000));
2348
2199
  if(debug){
2349
2200
    fprintf_plus(stderr, "avahi_loop_with_timeout exited %s\n",
2363
2214
  if(mc.server != NULL)
2364
2215
    avahi_server_free(mc.server);
2365
2216
  
2366
 
  if(simple_poll != NULL)
2367
 
    avahi_simple_poll_free(simple_poll);
 
2217
  if(mc.simple_poll != NULL)
 
2218
    avahi_simple_poll_free(mc.simple_poll);
2368
2219
  
2369
2220
  if(gnutls_initialized){
2370
2221
    gnutls_certificate_free_credentials(mc.cred);
2387
2238
    }
2388
2239
  }
2389
2240
  
 
2241
  /* Run network hooks */
 
2242
  run_network_hooks("stop", interface, delay);
 
2243
  
2390
2244
  /* Re-raise priviliges */
2391
2245
  {
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");
 
2246
    errno = 0;
 
2247
    ret = seteuid(0);
 
2248
    if(ret == -1){
 
2249
      perror_plus("seteuid");
 
2250
    }
 
2251
    
 
2252
    /* Take down the network interface */
 
2253
    if(take_down_interface and geteuid() == 0){
 
2254
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
2255
      if(ret == -1){
 
2256
        perror_plus("ioctl SIOCGIFFLAGS");
 
2257
      } else if(network.ifr_flags & IFF_UP){
 
2258
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
 
2259
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
2260
        if(ret == -1){
 
2261
          perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
2408
2262
        }
2409
2263
      }
2410
 
      if(debug and (interfaces_to_take_down == NULL)){
2411
 
        fprintf_plus(stderr, "No interfaces needed to be taken"
2412
 
                     " down\n");
 
2264
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
2265
      if(ret == -1){
 
2266
        perror_plus("close");
2413
2267
      }
2414
2268
    }
2415
 
    
2416
 
    lower_privileges_permanently();
2417
 
  }
2418
 
  
2419
 
  free(interfaces_to_take_down);
2420
 
  free(interfaces_hooks);
 
2269
  }
 
2270
  /* Lower privileges permanently */
 
2271
  errno = 0;
 
2272
  ret = setuid(uid);
 
2273
  if(ret == -1){
 
2274
    perror_plus("setuid");
 
2275
  }
2421
2276
  
2422
2277
  /* Removes the GPGME temp directory and all files inside */
2423
2278
  if(tempdir_created){