/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

* initramfs-tools-hook: Set DEVICE for network hooks.

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;
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;
174
170
 
175
171
/* Function to use when printing errors */
176
172
void perror_plus(const char *print_text){
177
 
  int e = errno;
178
173
  fprintf(stderr, "Mandos plugin %s: ",
179
174
          program_invocation_short_name);
180
 
  errno = e;
181
175
  perror(print_text);
182
176
}
183
177
 
184
 
__attribute__((format (gnu_printf, 2, 3)))
185
178
int fprintf_plus(FILE *stream, const char *format, ...){
186
179
  va_list ap;
187
180
  va_start (ap, format);
209
202
}
210
203
 
211
204
/* Add server to set of servers to retry periodically */
212
 
bool add_server(const char *ip, in_port_t port, AvahiIfIndex if_index,
213
 
                int af){
 
205
int add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
 
206
               int af){
214
207
  int ret;
215
208
  server *new_server = malloc(sizeof(server));
216
209
  if(new_server == NULL){
217
210
    perror_plus("malloc");
218
 
    return false;
 
211
    return -1;
219
212
  }
220
213
  *new_server = (server){ .ip = strdup(ip),
221
214
                          .port = port,
223
216
                          .af = af };
224
217
  if(new_server->ip == NULL){
225
218
    perror_plus("strdup");
226
 
    return false;
 
219
    return -1;
227
220
  }
228
221
  /* Special case of first server */
229
222
  if (mc.current_server == NULL){
240
233
  ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
241
234
  if(ret == -1){
242
235
    perror_plus("clock_gettime");
243
 
    return false;
 
236
    return -1;
244
237
  }
245
 
  return true;
 
238
  return 0;
246
239
}
247
240
 
248
241
/* 
253
246
  gpgme_error_t rc;
254
247
  gpgme_engine_info_t engine_info;
255
248
  
 
249
  
256
250
  /*
257
251
   * Helper function to insert pub and seckey to the engine keyring.
258
252
   */
468
462
}
469
463
 
470
464
static const char * safer_gnutls_strerror(int value){
471
 
  const char *ret = gnutls_strerror(value);
 
465
  const char *ret = gnutls_strerror(value); /* Spurious warning from
 
466
                                               -Wunreachable-code */
472
467
  if(ret == NULL)
473
468
    ret = "(unknown)";
474
469
  return ret;
619
614
                      __attribute__((unused)) const char *txt){}
620
615
 
621
616
/* Called when a Mandos server is found */
622
 
static int start_mandos_communication(const char *ip, in_port_t port,
 
617
static int start_mandos_communication(const char *ip, uint16_t port,
623
618
                                      AvahiIfIndex if_index,
624
619
                                      int af){
625
620
  int ret, tcp_sd = -1;
664
659
  
665
660
  if(debug){
666
661
    fprintf_plus(stderr, "Setting up a TCP connection to %s, port %"
667
 
                 PRIuMAX "\n", ip, (uintmax_t)port);
 
662
                 PRIu16 "\n", ip, port);
668
663
  }
669
664
  
670
665
  tcp_sd = socket(pf, SOCK_STREAM, 0);
701
696
    goto mandos_end;
702
697
  }
703
698
  if(af == AF_INET6){
704
 
    to.in6.sin6_port = htons(port);    
 
699
    to.in6.sin6_port = htons(port); /* Spurious warnings from
 
700
                                       -Wconversion and
 
701
                                       -Wunreachable-code */
 
702
    
705
703
    if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
706
704
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
707
705
                                -Wunreachable-code*/
731
729
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
732
730
        perror_plus("if_indextoname");
733
731
      } else {
734
 
        fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIuMAX
735
 
                     "\n", ip, interface, (uintmax_t)port);
 
732
        fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16
 
733
                     "\n", ip, interface, port);
736
734
      }
737
735
    } else {
738
 
      fprintf_plus(stderr, "Connection to: %s, port %" PRIuMAX "\n",
739
 
                   ip, (uintmax_t)port);
 
736
      fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n",
 
737
                   ip, port);
740
738
    }
741
739
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
742
740
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
820
818
    goto mandos_end;
821
819
  }
822
820
  
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);
 
821
  /* Spurious warning from -Wint-to-pointer-cast */
 
822
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
828
823
  
829
824
  if(quit_now){
830
825
    errno = EINTR;
1003
998
                             AVAHI_GCC_UNUSED AvahiLookupResultFlags
1004
999
                             flags,
1005
1000
                             AVAHI_GCC_UNUSED void* userdata){
1006
 
  if(r == NULL){
1007
 
    return;
1008
 
  }
 
1001
  assert(r);
1009
1002
  
1010
1003
  /* Called whenever a service has been resolved successfully or
1011
1004
     timed out */
1032
1025
                     PRIdMAX ") on port %" PRIu16 "\n", name,
1033
1026
                     host_name, ip, (intmax_t)interface, port);
1034
1027
      }
1035
 
      int ret = start_mandos_communication(ip, (in_port_t)port,
1036
 
                                           interface,
 
1028
      int ret = start_mandos_communication(ip, port, interface,
1037
1029
                                           avahi_proto_to_af(proto));
1038
1030
      if(ret == 0){
1039
 
        avahi_simple_poll_quit(simple_poll);
 
1031
        avahi_simple_poll_quit(mc.simple_poll);
1040
1032
      } else {
1041
 
        if(not add_server(ip, (in_port_t)port, interface,
1042
 
                          avahi_proto_to_af(proto))){
1043
 
          fprintf_plus(stderr, "Failed to add server \"%s\" to server"
1044
 
                       " list\n", name);
1045
 
        }
 
1033
        ret = add_server(ip, port, interface,
 
1034
                         avahi_proto_to_af(proto));
1046
1035
      }
1047
1036
    }
1048
1037
  }
1059
1048
                            AVAHI_GCC_UNUSED AvahiLookupResultFlags
1060
1049
                            flags,
1061
1050
                            AVAHI_GCC_UNUSED void* userdata){
1062
 
  if(b == NULL){
1063
 
    return;
1064
 
  }
 
1051
  assert(b);
1065
1052
  
1066
1053
  /* Called whenever a new services becomes available on the LAN or
1067
1054
     is removed from the LAN */
1076
1063
    
1077
1064
    fprintf_plus(stderr, "(Avahi browser) %s\n",
1078
1065
                 avahi_strerror(avahi_server_errno(mc.server)));
1079
 
    avahi_simple_poll_quit(simple_poll);
 
1066
    avahi_simple_poll_quit(mc.simple_poll);
1080
1067
    return;
1081
1068
    
1082
1069
  case AVAHI_BROWSER_NEW:
1115
1102
  signal_received = sig;
1116
1103
  int old_errno = errno;
1117
1104
  /* set main loop to exit */
1118
 
  if(simple_poll != NULL){
1119
 
    avahi_simple_poll_quit(simple_poll);
 
1105
  if(mc.simple_poll != NULL){
 
1106
    avahi_simple_poll_quit(mc.simple_poll);
1120
1107
  }
1121
1108
  errno = old_errno;
1122
1109
}
1123
1110
 
1124
1111
bool get_flags(const char *ifname, struct ifreq *ifr){
1125
1112
  int ret;
1126
 
  error_t ret_errno;
1127
1113
  
1128
1114
  int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1129
1115
  if(s < 0){
1130
 
    ret_errno = errno;
1131
1116
    perror_plus("socket");
1132
 
    errno = ret_errno;
1133
1117
    return false;
1134
1118
  }
1135
1119
  strcpy(ifr->ifr_name, ifname);
1136
1120
  ret = ioctl(s, SIOCGIFFLAGS, ifr);
1137
1121
  if(ret == -1){
1138
1122
    if(debug){
1139
 
      ret_errno = errno;
1140
1123
      perror_plus("ioctl SIOCGIFFLAGS");
1141
 
      errno = ret_errno;
1142
1124
    }
1143
1125
    return false;
1144
1126
  }
1213
1195
}
1214
1196
 
1215
1197
/* 
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);
 
1198
 * This function determines if a directory entry in /sys/class/net
 
1199
 * corresponds to an acceptable network device which is up.
 
1200
 * (This function is passed to scandir(3) as a filter function.)
 
1201
 */
 
1202
int up_interface(const struct dirent *if_entry){
 
1203
  if(if_entry->d_name[0] == '.'){
 
1204
    return 0;
 
1205
  }
 
1206
  
 
1207
  struct ifreq ifr;
 
1208
  if(not get_flags(if_entry->d_name, &ifr)){
 
1209
    if(debug){
 
1210
      fprintf_plus(stderr, "Failed to get flags for interface "
 
1211
                   "\"%s\"\n", if_entry->d_name);
 
1212
    }
 
1213
    return 0;
 
1214
  }
 
1215
  
 
1216
  /* Reject down interfaces */
 
1217
  if(not (ifr.ifr_flags & IFF_UP)){
 
1218
    if(debug){
 
1219
      fprintf_plus(stderr, "Rejecting down interface \"%s\"\n",
 
1220
                   if_entry->d_name);
 
1221
    }
 
1222
    return 0;
 
1223
  }
 
1224
  
 
1225
  /* Reject non-running interfaces */
 
1226
  if(not (ifr.ifr_flags & IFF_RUNNING)){
 
1227
    if(debug){
 
1228
      fprintf_plus(stderr, "Rejecting non-running interface \"%s\"\n",
 
1229
                   if_entry->d_name);
 
1230
    }
 
1231
    return 0;
 
1232
  }
 
1233
  
 
1234
  if(not good_flags(if_entry->d_name, &ifr)){
 
1235
    return 0;
 
1236
  }
 
1237
  return 1;
1245
1238
}
1246
1239
 
1247
1240
int notdotentries(const struct dirent *direntry){
1364
1357
                                         mc.current_server->if_index,
1365
1358
                                         mc.current_server->af);
1366
1359
        if(ret == 0){
1367
 
          avahi_simple_poll_quit(simple_poll);
 
1360
          avahi_simple_poll_quit(mc.simple_poll);
1368
1361
          return 0;
1369
1362
        }
1370
1363
        ret = clock_gettime(CLOCK_MONOTONIC,
1388
1381
  }
1389
1382
}
1390
1383
 
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
1384
bool run_network_hooks(const char *mode, const char *interface,
1444
1385
                       const float delay){
1445
1386
  struct dirent **direntries;
1448
1389
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1449
1390
                         alphasort);
1450
1391
  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
 
    }
 
1392
    perror_plus("scandir");
1459
1393
  } else {
1460
1394
    int devnull = open("/dev/null", O_RDONLY);
1461
1395
    for(int i = 0; i < numhooks; i++){
1474
1408
      if(hook_pid == 0){
1475
1409
        /* Child */
1476
1410
        /* Raise privileges */
1477
 
        raise_privileges_permanently();
 
1411
        errno = 0;
 
1412
        ret = seteuid(0);
 
1413
        if(ret == -1){
 
1414
          perror_plus("seteuid");
 
1415
        }
 
1416
        /* Raise privileges even more */
 
1417
        errno = 0;
 
1418
        ret = setuid(0);
 
1419
        if(ret == -1){
 
1420
          perror_plus("setuid");
 
1421
        }
1478
1422
        /* Set group */
1479
1423
        errno = 0;
1480
1424
        ret = setgid(0);
1500
1444
          perror_plus("setenv");
1501
1445
          _exit(EX_OSERR);
1502
1446
        }
1503
 
        ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
 
1447
        ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1504
1448
        if(ret == -1){
1505
1449
          perror_plus("setenv");
1506
1450
          _exit(EX_OSERR);
1523
1467
          _exit(EX_OSERR);
1524
1468
        }
1525
1469
        free(delaystring);
1526
 
        if(connect_to != NULL){
1527
 
          ret = setenv("CONNECT", connect_to, 1);
1528
 
          if(ret == -1){
1529
 
            perror_plus("setenv");
1530
 
            _exit(EX_OSERR);
1531
 
          }
1532
 
        }
1533
 
        if(execl(fullname, direntry->d_name, mode, NULL) == -1){
1534
 
          perror_plus("execl");
1535
 
          _exit(EXIT_FAILURE);
1536
 
        }
 
1470
        ret = execl(fullname, direntry->d_name, mode, NULL);
 
1471
        perror_plus("execl");
1537
1472
      } else {
1538
1473
        int status;
1539
1474
        if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1573
1508
  return true;
1574
1509
}
1575
1510
 
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
1511
int main(int argc, char *argv[]){
1749
1512
  AvahiSServiceBrowser *sb = NULL;
1750
 
  error_t ret_errno;
 
1513
  int error;
1751
1514
  int ret;
1752
1515
  intmax_t tmpmax;
1753
1516
  char *tmp;
1754
1517
  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;
 
1518
  const char *interface = "";
 
1519
  struct ifreq network;
 
1520
  int sd = -1;
 
1521
  bool take_down_interface = false;
 
1522
  uid_t uid;
 
1523
  gid_t gid;
1759
1524
  char tempdir[] = "/tmp/mandosXXXXXX";
1760
1525
  bool tempdir_created = false;
1761
1526
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1762
1527
  const char *seckey = PATHDIR "/" SECKEY;
1763
1528
  const char *pubkey = PATHDIR "/" PUBKEY;
1764
 
  char *interfaces_hooks = NULL;
1765
 
  size_t interfaces_hooks_size = 0;
1766
1529
  
1767
1530
  bool gnutls_initialized = false;
1768
1531
  bool gpgme_initialized = false;
1830
1593
        .group = 2 },
1831
1594
      { .name = "retry", .key = 132,
1832
1595
        .arg = "SECONDS",
1833
 
        .doc = "Retry interval used when denied by the Mandos server",
 
1596
        .doc = "Retry interval used when denied by the mandos server",
1834
1597
        .group = 2 },
1835
1598
      { .name = "network-hook-dir", .key = 133,
1836
1599
        .arg = "DIR",
1859
1622
        connect_to = arg;
1860
1623
        break;
1861
1624
      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
 
        }
 
1625
        interface = arg;
1867
1626
        break;
1868
1627
      case 's':                 /* --seckey */
1869
1628
        seckey = arg;
1912
1671
        argp_state_help(state, state->out_stream,
1913
1672
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1914
1673
      case 'V':                 /* --version */
 
1674
        fprintf_plus(state->out_stream,
 
1675
                     "Mandos plugin mandos-client: ");
1915
1676
        fprintf_plus(state->out_stream, "%s\n", argp_program_version);
1916
1677
        exit(argp_err_exit_status);
1917
1678
        break;
1947
1708
       <http://bugs.debian.org/633582> */
1948
1709
    
1949
1710
    /* Re-raise priviliges */
1950
 
    if(raise_privileges() == 0){
 
1711
    errno = 0;
 
1712
    ret = seteuid(0);
 
1713
    if(ret == -1){
 
1714
      perror_plus("seteuid");
 
1715
    } else {
1951
1716
      struct stat st;
1952
1717
      
1953
1718
      if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
2001
1766
    }
2002
1767
  }
2003
1768
  
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
1769
  /* 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
 
    }
 
1770
  if(not run_network_hooks("start", interface, delay)){
 
1771
    goto end;
2038
1772
  }
2039
1773
  
2040
1774
  if(not debug){
2041
1775
    avahi_set_log_function(empty_log);
2042
1776
  }
2043
1777
  
 
1778
  if(interface[0] == '\0'){
 
1779
    struct dirent **direntries;
 
1780
    /* First look for interfaces that are up */
 
1781
    ret = scandir(sys_class_net, &direntries, up_interface,
 
1782
                  alphasort);
 
1783
    if(ret == 0){
 
1784
      /* No up interfaces, look for any good interfaces */
 
1785
      free(direntries);
 
1786
      ret = scandir(sys_class_net, &direntries, good_interface,
 
1787
                    alphasort);
 
1788
    }
 
1789
    if(ret >= 1){
 
1790
      /* Pick the first interface returned */
 
1791
      interface = strdup(direntries[0]->d_name);
 
1792
      if(debug){
 
1793
        fprintf_plus(stderr, "Using interface \"%s\"\n", interface);
 
1794
      }
 
1795
      if(interface == NULL){
 
1796
        perror_plus("malloc");
 
1797
        free(direntries);
 
1798
        exitcode = EXIT_FAILURE;
 
1799
        goto end;
 
1800
      }
 
1801
      free(direntries);
 
1802
    } else {
 
1803
      free(direntries);
 
1804
      fprintf_plus(stderr, "Could not find a network interface\n");
 
1805
      exitcode = EXIT_FAILURE;
 
1806
      goto end;
 
1807
    }
 
1808
  }
 
1809
  
2044
1810
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
2045
1811
     from the signal handler */
2046
1812
  /* Initialize the pseudo-RNG for Avahi */
2047
1813
  srand((unsigned int) time(NULL));
2048
 
  simple_poll = avahi_simple_poll_new();
2049
 
  if(simple_poll == NULL){
 
1814
  mc.simple_poll = avahi_simple_poll_new();
 
1815
  if(mc.simple_poll == NULL){
2050
1816
    fprintf_plus(stderr,
2051
1817
                 "Avahi: Failed to create simple poll object.\n");
2052
1818
    exitcode = EX_UNAVAILABLE;
2116
1882
    }
2117
1883
  }
2118
1884
  
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");
 
1885
  /* If the interface is down, bring it up */
 
1886
  if(strcmp(interface, "none") != 0){
 
1887
    if_index = (AvahiIfIndex) if_nametoindex(interface);
 
1888
    if(if_index == 0){
 
1889
      fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
 
1890
      exitcode = EX_UNAVAILABLE;
 
1891
      goto end;
 
1892
    }
 
1893
    
 
1894
    if(quit_now){
 
1895
      goto end;
 
1896
    }
 
1897
    
 
1898
    /* Re-raise priviliges */
 
1899
    errno = 0;
 
1900
    ret = seteuid(0);
 
1901
    if(ret == -1){
 
1902
      perror_plus("seteuid");
 
1903
    }
 
1904
    
 
1905
#ifdef __linux__
 
1906
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
 
1907
       messages about the network interface to mess up the prompt */
 
1908
    ret = klogctl(8, NULL, 5);
 
1909
    bool restore_loglevel = true;
 
1910
    if(ret == -1){
 
1911
      restore_loglevel = false;
 
1912
      perror_plus("klogctl");
 
1913
    }
 
1914
#endif  /* __linux__ */
 
1915
    
 
1916
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
1917
    if(sd < 0){
 
1918
      perror_plus("socket");
 
1919
      exitcode = EX_OSERR;
 
1920
#ifdef __linux__
 
1921
      if(restore_loglevel){
 
1922
        ret = klogctl(7, NULL, 0);
 
1923
        if(ret == -1){
 
1924
          perror_plus("klogctl");
 
1925
        }
 
1926
      }
 
1927
#endif  /* __linux__ */
 
1928
      /* Lower privileges */
 
1929
      errno = 0;
 
1930
      ret = seteuid(uid);
 
1931
      if(ret == -1){
 
1932
        perror_plus("seteuid");
 
1933
      }
 
1934
      goto end;
 
1935
    }
 
1936
    strcpy(network.ifr_name, interface);
 
1937
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
1938
    if(ret == -1){
 
1939
      perror_plus("ioctl SIOCGIFFLAGS");
 
1940
#ifdef __linux__
 
1941
      if(restore_loglevel){
 
1942
        ret = klogctl(7, NULL, 0);
 
1943
        if(ret == -1){
 
1944
          perror_plus("klogctl");
 
1945
        }
 
1946
      }
 
1947
#endif  /* __linux__ */
 
1948
      exitcode = EX_OSERR;
 
1949
      /* Lower privileges */
 
1950
      errno = 0;
 
1951
      ret = seteuid(uid);
 
1952
      if(ret == -1){
 
1953
        perror_plus("seteuid");
 
1954
      }
 
1955
      goto end;
 
1956
    }
 
1957
    if((network.ifr_flags & IFF_UP) == 0){
 
1958
      network.ifr_flags |= IFF_UP;
 
1959
      take_down_interface = true;
 
1960
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
1961
      if(ret == -1){
 
1962
        take_down_interface = false;
 
1963
        perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
 
1964
        exitcode = EX_OSERR;
 
1965
#ifdef __linux__
 
1966
        if(restore_loglevel){
 
1967
          ret = klogctl(7, NULL, 0);
 
1968
          if(ret == -1){
 
1969
            perror_plus("klogctl");
 
1970
          }
 
1971
        }
 
1972
#endif  /* __linux__ */
 
1973
        /* Lower privileges */
 
1974
        errno = 0;
 
1975
        ret = seteuid(uid);
 
1976
        if(ret == -1){
 
1977
          perror_plus("seteuid");
 
1978
        }
 
1979
        goto end;
 
1980
      }
 
1981
    }
 
1982
    /* Sleep checking until interface is running.
 
1983
       Check every 0.25s, up to total time of delay */
 
1984
    for(int i=0; i < delay * 4; i++){
 
1985
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
1986
      if(ret == -1){
 
1987
        perror_plus("ioctl SIOCGIFFLAGS");
 
1988
      } else if(network.ifr_flags & IFF_RUNNING){
 
1989
        break;
 
1990
      }
 
1991
      struct timespec sleeptime = { .tv_nsec = 250000000 };
 
1992
      ret = nanosleep(&sleeptime, NULL);
 
1993
      if(ret == -1 and errno != EINTR){
 
1994
        perror_plus("nanosleep");
 
1995
      }
 
1996
    }
 
1997
    if(not take_down_interface){
 
1998
      /* We won't need the socket anymore */
 
1999
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
2000
      if(ret == -1){
 
2001
        perror_plus("close");
 
2002
      }
 
2003
    }
 
2004
#ifdef __linux__
 
2005
    if(restore_loglevel){
 
2006
      /* Restores kernel loglevel to default */
 
2007
      ret = klogctl(7, NULL, 0);
 
2008
      if(ret == -1){
 
2009
        perror_plus("klogctl");
 
2010
      }
 
2011
    }
 
2012
#endif  /* __linux__ */
 
2013
    /* Lower privileges */
 
2014
    errno = 0;
 
2015
    /* Lower privileges */
 
2016
    ret = seteuid(uid);
 
2017
    if(ret == -1){
 
2018
      perror_plus("seteuid");
2181
2019
    }
2182
2020
  }
2183
2021
  
2224
2062
    /* Connect directly, do not use Zeroconf */
2225
2063
    /* (Mainly meant for debugging) */
2226
2064
    char *address = strrchr(connect_to, ':');
2227
 
    
2228
2065
    if(address == NULL){
2229
2066
      fprintf_plus(stderr, "No colon in address\n");
2230
2067
      exitcode = EX_USAGE;
2235
2072
      goto end;
2236
2073
    }
2237
2074
    
2238
 
    in_port_t port;
 
2075
    uint16_t port;
2239
2076
    errno = 0;
2240
2077
    tmpmax = strtoimax(address+1, &tmp, 10);
2241
2078
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
2242
 
       or tmpmax != (in_port_t)tmpmax){
 
2079
       or tmpmax != (uint16_t)tmpmax){
2243
2080
      fprintf_plus(stderr, "Bad port number\n");
2244
2081
      exitcode = EX_USAGE;
2245
2082
      goto end;
2249
2086
      goto end;
2250
2087
    }
2251
2088
    
2252
 
    port = (in_port_t)tmpmax;
 
2089
    port = (uint16_t)tmpmax;
2253
2090
    *address = '\0';
2254
2091
    /* Colon in address indicates IPv6 */
2255
2092
    int af;
2303
2140
    config.publish_domain = 0;
2304
2141
    
2305
2142
    /* Allocate a new server */
2306
 
    mc.server = avahi_server_new(avahi_simple_poll_get(simple_poll),
2307
 
                                 &config, NULL, NULL, &ret_errno);
 
2143
    mc.server = avahi_server_new(avahi_simple_poll_get
 
2144
                                 (mc.simple_poll), &config, NULL,
 
2145
                                 NULL, &error);
2308
2146
    
2309
2147
    /* Free the Avahi configuration data */
2310
2148
    avahi_server_config_free(&config);
2313
2151
  /* Check if creating the Avahi server object succeeded */
2314
2152
  if(mc.server == NULL){
2315
2153
    fprintf_plus(stderr, "Failed to create Avahi server: %s\n",
2316
 
                 avahi_strerror(ret_errno));
 
2154
                 avahi_strerror(error));
2317
2155
    exitcode = EX_UNAVAILABLE;
2318
2156
    goto end;
2319
2157
  }
2343
2181
    fprintf_plus(stderr, "Starting Avahi loop search\n");
2344
2182
  }
2345
2183
 
2346
 
  ret = avahi_loop_with_timeout(simple_poll,
 
2184
  ret = avahi_loop_with_timeout(mc.simple_poll,
2347
2185
                                (int)(retry_interval * 1000));
2348
2186
  if(debug){
2349
2187
    fprintf_plus(stderr, "avahi_loop_with_timeout exited %s\n",
2363
2201
  if(mc.server != NULL)
2364
2202
    avahi_server_free(mc.server);
2365
2203
  
2366
 
  if(simple_poll != NULL)
2367
 
    avahi_simple_poll_free(simple_poll);
 
2204
  if(mc.simple_poll != NULL)
 
2205
    avahi_simple_poll_free(mc.simple_poll);
2368
2206
  
2369
2207
  if(gnutls_initialized){
2370
2208
    gnutls_certificate_free_credentials(mc.cred);
2387
2225
    }
2388
2226
  }
2389
2227
  
 
2228
  /* Run network hooks */
 
2229
  run_network_hooks("stop", interface, delay);
 
2230
  
2390
2231
  /* Re-raise priviliges */
2391
2232
  {
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");
 
2233
    errno = 0;
 
2234
    ret = seteuid(0);
 
2235
    if(ret == -1){
 
2236
      perror_plus("seteuid");
 
2237
    }
 
2238
    
 
2239
    /* Take down the network interface */
 
2240
    if(take_down_interface and geteuid() == 0){
 
2241
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
2242
      if(ret == -1){
 
2243
        perror_plus("ioctl SIOCGIFFLAGS");
 
2244
      } else if(network.ifr_flags & IFF_UP){
 
2245
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
 
2246
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
2247
        if(ret == -1){
 
2248
          perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
2408
2249
        }
2409
2250
      }
2410
 
      if(debug and (interfaces_to_take_down == NULL)){
2411
 
        fprintf_plus(stderr, "No interfaces needed to be taken"
2412
 
                     " down\n");
 
2251
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
2252
      if(ret == -1){
 
2253
        perror_plus("close");
2413
2254
      }
2414
2255
    }
2415
 
    
2416
 
    lower_privileges_permanently();
2417
 
  }
2418
 
  
2419
 
  free(interfaces_to_take_down);
2420
 
  free(interfaces_hooks);
 
2256
  }
 
2257
  /* Lower privileges permanently */
 
2258
  errno = 0;
 
2259
  ret = setuid(uid);
 
2260
  if(ret == -1){
 
2261
    perror_plus("setuid");
 
2262
  }
2421
2263
  
2422
2264
  /* Removes the GPGME temp directory and all files inside */
2423
2265
  if(tempdir_created){