/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

* network-hooks.d: New directory.
* network-hooks.d/bridge: New example hook.
* network-hooks.d/bridge.conf: Config file for bridge example hook.

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() */
72
73
                                */
73
74
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
74
75
                                   getuid(), getgid(), seteuid(),
75
 
                                   setgid(), pause(), _exit() */
 
76
                                   setgid(), pause() */
76
77
#include <arpa/inet.h>          /* inet_pton(), htons, inet_ntop() */
77
78
#include <iso646.h>             /* not, or, and */
78
79
#include <argp.h>               /* struct argp_option, error_t, struct
86
87
                                   EX_NOHOST, EX_IOERR, EX_PROTOCOL */
87
88
#include <sys/wait.h>           /* waitpid(), WIFEXITED(),
88
89
                                   WEXITSTATUS(), WTERMSIG() */
89
 
#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
90
 
95
91
#ifdef __linux__
96
92
#include <sys/klog.h>           /* klogctl() */
138
134
static const char sys_class_net[] = "/sys/class/net";
139
135
char *connect_to = NULL;
140
136
const char *hookdir = HOOKDIR;
141
 
uid_t uid = 65534;
142
 
gid_t gid = 65534;
143
137
 
144
138
/* Doubly linked list that need to be circularly linked when used */
145
139
typedef struct server{
146
140
  const char *ip;
147
 
  in_port_t port;
 
141
  uint16_t port;
148
142
  AvahiIfIndex if_index;
149
143
  int af;
150
144
  struct timespec last_seen;
154
148
 
155
149
/* Used for passing in values through the Avahi callback functions */
156
150
typedef struct {
 
151
  AvahiSimplePoll *simple_poll;
157
152
  AvahiServer *server;
158
153
  gnutls_certificate_credentials_t cred;
159
154
  unsigned int dh_bits;
163
158
  server *current_server;
164
159
} mandos_context;
165
160
 
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 };
 
161
/* global context so signal handler can reach it*/
 
162
mandos_context mc = { .simple_poll = NULL, .server = NULL,
 
163
                      .dh_bits = 1024, .priority = "SECURE256"
 
164
                      ":!CTYPE-X.509:+CTYPE-OPENPGP",
 
165
                      .current_server = NULL };
171
166
 
172
167
sig_atomic_t quit_now = 0;
173
168
int signal_received = 0;
174
169
 
175
170
/* Function to use when printing errors */
176
171
void perror_plus(const char *print_text){
177
 
  int e = errno;
178
172
  fprintf(stderr, "Mandos plugin %s: ",
179
173
          program_invocation_short_name);
180
 
  errno = e;
181
174
  perror(print_text);
182
175
}
183
176
 
184
 
__attribute__((format (gnu_printf, 2, 3)))
185
177
int fprintf_plus(FILE *stream, const char *format, ...){
186
178
  va_list ap;
187
179
  va_start (ap, format);
209
201
}
210
202
 
211
203
/* 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){
 
204
int add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
 
205
               int af){
214
206
  int ret;
215
207
  server *new_server = malloc(sizeof(server));
216
208
  if(new_server == NULL){
217
209
    perror_plus("malloc");
218
 
    return false;
 
210
    return -1;
219
211
  }
220
212
  *new_server = (server){ .ip = strdup(ip),
221
213
                          .port = port,
223
215
                          .af = af };
224
216
  if(new_server->ip == NULL){
225
217
    perror_plus("strdup");
226
 
    return false;
 
218
    return -1;
227
219
  }
228
220
  /* Special case of first server */
229
221
  if (mc.current_server == NULL){
240
232
  ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
241
233
  if(ret == -1){
242
234
    perror_plus("clock_gettime");
243
 
    return false;
 
235
    return -1;
244
236
  }
245
 
  return true;
 
237
  return 0;
246
238
}
247
239
 
248
240
/* 
253
245
  gpgme_error_t rc;
254
246
  gpgme_engine_info_t engine_info;
255
247
  
 
248
  
256
249
  /*
257
250
   * Helper function to insert pub and seckey to the engine keyring.
258
251
   */
468
461
}
469
462
 
470
463
static const char * safer_gnutls_strerror(int value){
471
 
  const char *ret = gnutls_strerror(value);
 
464
  const char *ret = gnutls_strerror(value); /* Spurious warning from
 
465
                                               -Wunreachable-code */
472
466
  if(ret == NULL)
473
467
    ret = "(unknown)";
474
468
  return ret;
619
613
                      __attribute__((unused)) const char *txt){}
620
614
 
621
615
/* Called when a Mandos server is found */
622
 
static int start_mandos_communication(const char *ip, in_port_t port,
 
616
static int start_mandos_communication(const char *ip, uint16_t port,
623
617
                                      AvahiIfIndex if_index,
624
618
                                      int af){
625
619
  int ret, tcp_sd = -1;
664
658
  
665
659
  if(debug){
666
660
    fprintf_plus(stderr, "Setting up a TCP connection to %s, port %"
667
 
                 PRIuMAX "\n", ip, (uintmax_t)port);
 
661
                 PRIu16 "\n", ip, port);
668
662
  }
669
663
  
670
664
  tcp_sd = socket(pf, SOCK_STREAM, 0);
701
695
    goto mandos_end;
702
696
  }
703
697
  if(af == AF_INET6){
704
 
    to.in6.sin6_port = htons(port);    
 
698
    to.in6.sin6_port = htons(port); /* Spurious warnings from
 
699
                                       -Wconversion and
 
700
                                       -Wunreachable-code */
 
701
    
705
702
    if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
706
703
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
707
704
                                -Wunreachable-code*/
731
728
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
732
729
        perror_plus("if_indextoname");
733
730
      } else {
734
 
        fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIuMAX
735
 
                     "\n", ip, interface, (uintmax_t)port);
 
731
        fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16
 
732
                     "\n", ip, interface, port);
736
733
      }
737
734
    } else {
738
 
      fprintf_plus(stderr, "Connection to: %s, port %" PRIuMAX "\n",
739
 
                   ip, (uintmax_t)port);
 
735
      fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n",
 
736
                   ip, port);
740
737
    }
741
738
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
742
739
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
820
817
    goto mandos_end;
821
818
  }
822
819
  
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);
 
820
  /* Spurious warning from -Wint-to-pointer-cast */
 
821
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
828
822
  
829
823
  if(quit_now){
830
824
    errno = EINTR;
1003
997
                             AVAHI_GCC_UNUSED AvahiLookupResultFlags
1004
998
                             flags,
1005
999
                             AVAHI_GCC_UNUSED void* userdata){
1006
 
  if(r == NULL){
1007
 
    return;
1008
 
  }
 
1000
  assert(r);
1009
1001
  
1010
1002
  /* Called whenever a service has been resolved successfully or
1011
1003
     timed out */
1032
1024
                     PRIdMAX ") on port %" PRIu16 "\n", name,
1033
1025
                     host_name, ip, (intmax_t)interface, port);
1034
1026
      }
1035
 
      int ret = start_mandos_communication(ip, (in_port_t)port,
1036
 
                                           interface,
 
1027
      int ret = start_mandos_communication(ip, port, interface,
1037
1028
                                           avahi_proto_to_af(proto));
1038
1029
      if(ret == 0){
1039
 
        avahi_simple_poll_quit(simple_poll);
 
1030
        avahi_simple_poll_quit(mc.simple_poll);
1040
1031
      } 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
 
        }
 
1032
        ret = add_server(ip, port, interface,
 
1033
                         avahi_proto_to_af(proto));
1046
1034
      }
1047
1035
    }
1048
1036
  }
1059
1047
                            AVAHI_GCC_UNUSED AvahiLookupResultFlags
1060
1048
                            flags,
1061
1049
                            AVAHI_GCC_UNUSED void* userdata){
1062
 
  if(b == NULL){
1063
 
    return;
1064
 
  }
 
1050
  assert(b);
1065
1051
  
1066
1052
  /* Called whenever a new services becomes available on the LAN or
1067
1053
     is removed from the LAN */
1076
1062
    
1077
1063
    fprintf_plus(stderr, "(Avahi browser) %s\n",
1078
1064
                 avahi_strerror(avahi_server_errno(mc.server)));
1079
 
    avahi_simple_poll_quit(simple_poll);
 
1065
    avahi_simple_poll_quit(mc.simple_poll);
1080
1066
    return;
1081
1067
    
1082
1068
  case AVAHI_BROWSER_NEW:
1115
1101
  signal_received = sig;
1116
1102
  int old_errno = errno;
1117
1103
  /* set main loop to exit */
1118
 
  if(simple_poll != NULL){
1119
 
    avahi_simple_poll_quit(simple_poll);
 
1104
  if(mc.simple_poll != NULL){
 
1105
    avahi_simple_poll_quit(mc.simple_poll);
1120
1106
  }
1121
1107
  errno = old_errno;
1122
1108
}
1123
1109
 
1124
1110
bool get_flags(const char *ifname, struct ifreq *ifr){
1125
1111
  int ret;
1126
 
  error_t ret_errno;
1127
1112
  
1128
1113
  int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1129
1114
  if(s < 0){
1130
 
    ret_errno = errno;
1131
1115
    perror_plus("socket");
1132
 
    errno = ret_errno;
1133
1116
    return false;
1134
1117
  }
1135
1118
  strcpy(ifr->ifr_name, ifname);
1136
1119
  ret = ioctl(s, SIOCGIFFLAGS, ifr);
1137
1120
  if(ret == -1){
1138
1121
    if(debug){
1139
 
      ret_errno = errno;
1140
1122
      perror_plus("ioctl SIOCGIFFLAGS");
1141
 
      errno = ret_errno;
1142
1123
    }
1143
1124
    return false;
1144
1125
  }
1213
1194
}
1214
1195
 
1215
1196
/* 
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);
 
1197
 * This function determines if a directory entry in /sys/class/net
 
1198
 * corresponds to an acceptable network device which is up.
 
1199
 * (This function is passed to scandir(3) as a filter function.)
 
1200
 */
 
1201
int up_interface(const struct dirent *if_entry){
 
1202
  if(if_entry->d_name[0] == '.'){
 
1203
    return 0;
 
1204
  }
 
1205
  
 
1206
  struct ifreq ifr;
 
1207
  if(not get_flags(if_entry->d_name, &ifr)){
 
1208
    if(debug){
 
1209
      fprintf_plus(stderr, "Failed to get flags for interface "
 
1210
                   "\"%s\"\n", if_entry->d_name);
 
1211
    }
 
1212
    return 0;
 
1213
  }
 
1214
  
 
1215
  /* Reject down interfaces */
 
1216
  if(not (ifr.ifr_flags & IFF_UP)){
 
1217
    if(debug){
 
1218
      fprintf_plus(stderr, "Rejecting down interface \"%s\"\n",
 
1219
                   if_entry->d_name);
 
1220
    }
 
1221
    return 0;
 
1222
  }
 
1223
  
 
1224
  /* Reject non-running interfaces */
 
1225
  if(not (ifr.ifr_flags & IFF_RUNNING)){
 
1226
    if(debug){
 
1227
      fprintf_plus(stderr, "Rejecting non-running interface \"%s\"\n",
 
1228
                   if_entry->d_name);
 
1229
    }
 
1230
    return 0;
 
1231
  }
 
1232
  
 
1233
  if(not good_flags(if_entry->d_name, &ifr)){
 
1234
    return 0;
 
1235
  }
 
1236
  return 1;
1245
1237
}
1246
1238
 
1247
1239
int notdotentries(const struct dirent *direntry){
1309
1301
    }
1310
1302
    return 0;
1311
1303
  }
1312
 
  if(debug){
1313
 
    fprintf_plus(stderr, "Hook \"%s\" is acceptable\n",
1314
 
                 direntry->d_name);
1315
 
  }
1316
1304
  return 1;
1317
1305
}
1318
1306
 
1364
1352
                                         mc.current_server->if_index,
1365
1353
                                         mc.current_server->af);
1366
1354
        if(ret == 0){
1367
 
          avahi_simple_poll_quit(simple_poll);
 
1355
          avahi_simple_poll_quit(mc.simple_poll);
1368
1356
          return 0;
1369
1357
        }
1370
1358
        ret = clock_gettime(CLOCK_MONOTONIC,
1388
1376
  }
1389
1377
}
1390
1378
 
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
1379
bool run_network_hooks(const char *mode, const char *interface,
1444
1380
                       const float delay){
1445
1381
  struct dirent **direntries;
1448
1384
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1449
1385
                         alphasort);
1450
1386
  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
 
    }
 
1387
    perror_plus("scandir");
1459
1388
  } else {
1460
1389
    int devnull = open("/dev/null", O_RDONLY);
1461
1390
    for(int i = 0; i < numhooks; i++){
1462
 
      direntry = direntries[i];
 
1391
      direntry = direntries[0];
1463
1392
      char *fullname = NULL;
1464
1393
      ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1465
1394
      if(ret < 0){
1466
1395
        perror_plus("asprintf");
1467
1396
        continue;
1468
1397
      }
1469
 
      if(debug){
1470
 
        fprintf_plus(stderr, "Running network hook \"%s\"\n",
1471
 
                     direntry->d_name);
1472
 
      }
1473
1398
      pid_t hook_pid = fork();
1474
1399
      if(hook_pid == 0){
1475
1400
        /* Child */
1476
 
        /* Raise privileges */
1477
 
        raise_privileges_permanently();
1478
 
        /* Set group */
1479
 
        errno = 0;
1480
 
        ret = setgid(0);
1481
 
        if(ret == -1){
1482
 
          perror_plus("setgid");
1483
 
        }
1484
 
        /* Reset supplementary groups */
1485
 
        errno = 0;
1486
 
        ret = setgroups(0, NULL);
1487
 
        if(ret == -1){
1488
 
          perror_plus("setgroups");
1489
 
        }
1490
1401
        dup2(devnull, STDIN_FILENO);
1491
1402
        close(devnull);
1492
1403
        dup2(STDERR_FILENO, STDOUT_FILENO);
1493
1404
        ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1494
1405
        if(ret == -1){
1495
1406
          perror_plus("setenv");
1496
 
          _exit(EX_OSERR);
 
1407
          return false;
1497
1408
        }
1498
1409
        ret = setenv("DEVICE", interface, 1);
1499
1410
        if(ret == -1){
1500
1411
          perror_plus("setenv");
1501
 
          _exit(EX_OSERR);
 
1412
          return false;
1502
1413
        }
1503
 
        ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
 
1414
        ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1504
1415
        if(ret == -1){
1505
1416
          perror_plus("setenv");
1506
 
          _exit(EX_OSERR);
 
1417
          return false;
1507
1418
        }
1508
1419
        ret = setenv("MODE", mode, 1);
1509
1420
        if(ret == -1){
1510
1421
          perror_plus("setenv");
1511
 
          _exit(EX_OSERR);
 
1422
          return false;
1512
1423
        }
1513
1424
        char *delaystring;
1514
1425
        ret = asprintf(&delaystring, "%f", delay);
1515
1426
        if(ret == -1){
1516
1427
          perror_plus("asprintf");
1517
 
          _exit(EX_OSERR);
 
1428
          return false;
1518
1429
        }
1519
1430
        ret = setenv("DELAY", delaystring, 1);
1520
1431
        if(ret == -1){
1521
1432
          free(delaystring);
1522
1433
          perror_plus("setenv");
1523
 
          _exit(EX_OSERR);
 
1434
          return false;
1524
1435
        }
1525
1436
        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
 
        }
 
1437
        ret = execl(fullname, direntry->d_name, mode, NULL);
 
1438
        perror_plus("execl");
1537
1439
      } else {
1538
1440
        int status;
1539
1441
        if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1563
1465
        }
1564
1466
      }
1565
1467
      free(fullname);
1566
 
      if(debug){
1567
 
        fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1568
 
                     direntry->d_name);
 
1468
      if(quit_now){
 
1469
        break;
1569
1470
      }
1570
1471
    }
1571
1472
    close(devnull);
1573
1474
  return true;
1574
1475
}
1575
1476
 
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
1477
int main(int argc, char *argv[]){
1749
1478
  AvahiSServiceBrowser *sb = NULL;
1750
 
  error_t ret_errno;
 
1479
  int error;
1751
1480
  int ret;
1752
1481
  intmax_t tmpmax;
1753
1482
  char *tmp;
1754
1483
  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;
 
1484
  const char *interface = "";
 
1485
  struct ifreq network;
 
1486
  int sd = -1;
 
1487
  bool take_down_interface = false;
 
1488
  uid_t uid;
 
1489
  gid_t gid;
1759
1490
  char tempdir[] = "/tmp/mandosXXXXXX";
1760
1491
  bool tempdir_created = false;
1761
1492
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1762
1493
  const char *seckey = PATHDIR "/" SECKEY;
1763
1494
  const char *pubkey = PATHDIR "/" PUBKEY;
1764
 
  char *interfaces_hooks = NULL;
1765
 
  size_t interfaces_hooks_size = 0;
1766
1495
  
1767
1496
  bool gnutls_initialized = false;
1768
1497
  bool gpgme_initialized = false;
1830
1559
        .group = 2 },
1831
1560
      { .name = "retry", .key = 132,
1832
1561
        .arg = "SECONDS",
1833
 
        .doc = "Retry interval used when denied by the Mandos server",
 
1562
        .doc = "Retry interval used when denied by the mandos server",
1834
1563
        .group = 2 },
1835
1564
      { .name = "network-hook-dir", .key = 133,
1836
1565
        .arg = "DIR",
1859
1588
        connect_to = arg;
1860
1589
        break;
1861
1590
      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
 
        }
 
1591
        interface = arg;
1867
1592
        break;
1868
1593
      case 's':                 /* --seckey */
1869
1594
        seckey = arg;
1912
1637
        argp_state_help(state, state->out_stream,
1913
1638
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1914
1639
      case 'V':                 /* --version */
 
1640
        fprintf_plus(state->out_stream,
 
1641
                     "Mandos plugin mandos-client: ");
1915
1642
        fprintf_plus(state->out_stream, "%s\n", argp_program_version);
1916
1643
        exit(argp_err_exit_status);
1917
1644
        break;
1945
1672
  {
1946
1673
    /* Work around Debian bug #633582:
1947
1674
       <http://bugs.debian.org/633582> */
 
1675
    struct stat st;
1948
1676
    
1949
1677
    /* Re-raise priviliges */
1950
 
    if(raise_privileges() == 0){
1951
 
      struct stat st;
1952
 
      
1953
 
      if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1954
 
        int seckey_fd = open(seckey, O_RDONLY);
1955
 
        if(seckey_fd == -1){
1956
 
          perror_plus("open");
1957
 
        } else {
1958
 
          ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1959
 
          if(ret == -1){
1960
 
            perror_plus("fstat");
1961
 
          } else {
1962
 
            if(S_ISREG(st.st_mode)
1963
 
               and st.st_uid == 0 and st.st_gid == 0){
1964
 
              ret = fchown(seckey_fd, uid, gid);
1965
 
              if(ret == -1){
1966
 
                perror_plus("fchown");
1967
 
              }
1968
 
            }
1969
 
          }
1970
 
          TEMP_FAILURE_RETRY(close(seckey_fd));
1971
 
        }
1972
 
      }
1973
 
    
1974
 
      if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1975
 
        int pubkey_fd = open(pubkey, O_RDONLY);
1976
 
        if(pubkey_fd == -1){
1977
 
          perror_plus("open");
1978
 
        } else {
1979
 
          ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1980
 
          if(ret == -1){
1981
 
            perror_plus("fstat");
1982
 
          } else {
1983
 
            if(S_ISREG(st.st_mode)
1984
 
               and st.st_uid == 0 and st.st_gid == 0){
1985
 
              ret = fchown(pubkey_fd, uid, gid);
1986
 
              if(ret == -1){
1987
 
                perror_plus("fchown");
1988
 
              }
1989
 
            }
1990
 
          }
1991
 
          TEMP_FAILURE_RETRY(close(pubkey_fd));
1992
 
        }
1993
 
      }
1994
 
    
1995
 
      /* Lower privileges */
1996
 
      errno = 0;
1997
 
      ret = seteuid(uid);
1998
 
      if(ret == -1){
1999
 
        perror_plus("seteuid");
2000
 
      }
2001
 
    }
2002
 
  }
2003
 
  
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
 
      }
 
1678
    errno = 0;
 
1679
    ret = seteuid(0);
 
1680
    if(ret == -1){
 
1681
      perror_plus("seteuid");
 
1682
    }
 
1683
    
 
1684
    if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
 
1685
      int seckey_fd = open(seckey, O_RDONLY);
 
1686
      if(seckey_fd == -1){
 
1687
        perror_plus("open");
 
1688
      } else {
 
1689
        ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
 
1690
        if(ret == -1){
 
1691
          perror_plus("fstat");
 
1692
        } else {
 
1693
          if(S_ISREG(st.st_mode)
 
1694
             and st.st_uid == 0 and st.st_gid == 0){
 
1695
            ret = fchown(seckey_fd, uid, gid);
 
1696
            if(ret == -1){
 
1697
              perror_plus("fchown");
 
1698
            }
 
1699
          }
 
1700
        }
 
1701
        TEMP_FAILURE_RETRY(close(seckey_fd));
 
1702
      }
 
1703
    }
 
1704
    
 
1705
    if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
 
1706
      int pubkey_fd = open(pubkey, O_RDONLY);
 
1707
      if(pubkey_fd == -1){
 
1708
        perror_plus("open");
 
1709
      } else {
 
1710
        ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
 
1711
        if(ret == -1){
 
1712
          perror_plus("fstat");
 
1713
        } else {
 
1714
          if(S_ISREG(st.st_mode)
 
1715
             and st.st_uid == 0 and st.st_gid == 0){
 
1716
            ret = fchown(pubkey_fd, uid, gid);
 
1717
            if(ret == -1){
 
1718
              perror_plus("fchown");
 
1719
            }
 
1720
          }
 
1721
        }
 
1722
        TEMP_FAILURE_RETRY(close(pubkey_fd));
 
1723
      }
 
1724
    }
 
1725
    
 
1726
    /* Lower privileges */
 
1727
    errno = 0;
 
1728
    ret = seteuid(uid);
 
1729
    if(ret == -1){
 
1730
      perror_plus("seteuid");
2017
1731
    }
2018
1732
  }
2019
1733
  
2020
1734
  /* Run network hooks */
2021
1735
  {
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)',');
 
1736
    /* Re-raise priviliges */
 
1737
    errno = 0;
 
1738
    ret = seteuid(0);
 
1739
    if(ret == -1){
 
1740
      perror_plus("seteuid");
2033
1741
    }
2034
 
    if(not run_network_hooks("start", interfaces_hooks != NULL ?
2035
 
                             interfaces_hooks : "", delay)){
 
1742
    if(not run_network_hooks("start", interface, delay)){
2036
1743
      goto end;
2037
1744
    }
 
1745
    /* Lower privileges */
 
1746
    errno = 0;
 
1747
    ret = seteuid(uid);
 
1748
    if(ret == -1){
 
1749
      perror_plus("seteuid");
 
1750
    }
2038
1751
  }
2039
1752
  
2040
1753
  if(not debug){
2041
1754
    avahi_set_log_function(empty_log);
2042
1755
  }
2043
1756
  
 
1757
  if(interface[0] == '\0'){
 
1758
    struct dirent **direntries;
 
1759
    /* First look for interfaces that are up */
 
1760
    ret = scandir(sys_class_net, &direntries, up_interface,
 
1761
                  alphasort);
 
1762
    if(ret == 0){
 
1763
      /* No up interfaces, look for any good interfaces */
 
1764
      free(direntries);
 
1765
      ret = scandir(sys_class_net, &direntries, good_interface,
 
1766
                    alphasort);
 
1767
    }
 
1768
    if(ret >= 1){
 
1769
      /* Pick the first interface returned */
 
1770
      interface = strdup(direntries[0]->d_name);
 
1771
      if(debug){
 
1772
        fprintf_plus(stderr, "Using interface \"%s\"\n", interface);
 
1773
      }
 
1774
      if(interface == NULL){
 
1775
        perror_plus("malloc");
 
1776
        free(direntries);
 
1777
        exitcode = EXIT_FAILURE;
 
1778
        goto end;
 
1779
      }
 
1780
      free(direntries);
 
1781
    } else {
 
1782
      free(direntries);
 
1783
      fprintf_plus(stderr, "Could not find a network interface\n");
 
1784
      exitcode = EXIT_FAILURE;
 
1785
      goto end;
 
1786
    }
 
1787
  }
 
1788
  
2044
1789
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
2045
1790
     from the signal handler */
2046
1791
  /* Initialize the pseudo-RNG for Avahi */
2047
1792
  srand((unsigned int) time(NULL));
2048
 
  simple_poll = avahi_simple_poll_new();
2049
 
  if(simple_poll == NULL){
 
1793
  mc.simple_poll = avahi_simple_poll_new();
 
1794
  if(mc.simple_poll == NULL){
2050
1795
    fprintf_plus(stderr,
2051
1796
                 "Avahi: Failed to create simple poll object.\n");
2052
1797
    exitcode = EX_UNAVAILABLE;
2116
1861
    }
2117
1862
  }
2118
1863
  
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");
 
1864
  /* If the interface is down, bring it up */
 
1865
  if(strcmp(interface, "none") != 0){
 
1866
    if_index = (AvahiIfIndex) if_nametoindex(interface);
 
1867
    if(if_index == 0){
 
1868
      fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
 
1869
      exitcode = EX_UNAVAILABLE;
 
1870
      goto end;
 
1871
    }
 
1872
    
 
1873
    if(quit_now){
 
1874
      goto end;
 
1875
    }
 
1876
    
 
1877
    /* Re-raise priviliges */
 
1878
    errno = 0;
 
1879
    ret = seteuid(0);
 
1880
    if(ret == -1){
 
1881
      perror_plus("seteuid");
 
1882
    }
 
1883
    
 
1884
#ifdef __linux__
 
1885
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
 
1886
       messages about the network interface to mess up the prompt */
 
1887
    ret = klogctl(8, NULL, 5);
 
1888
    bool restore_loglevel = true;
 
1889
    if(ret == -1){
 
1890
      restore_loglevel = false;
 
1891
      perror_plus("klogctl");
 
1892
    }
 
1893
#endif  /* __linux__ */
 
1894
    
 
1895
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
1896
    if(sd < 0){
 
1897
      perror_plus("socket");
 
1898
      exitcode = EX_OSERR;
 
1899
#ifdef __linux__
 
1900
      if(restore_loglevel){
 
1901
        ret = klogctl(7, NULL, 0);
 
1902
        if(ret == -1){
 
1903
          perror_plus("klogctl");
 
1904
        }
 
1905
      }
 
1906
#endif  /* __linux__ */
 
1907
      /* Lower privileges */
 
1908
      errno = 0;
 
1909
      ret = seteuid(uid);
 
1910
      if(ret == -1){
 
1911
        perror_plus("seteuid");
 
1912
      }
 
1913
      goto end;
 
1914
    }
 
1915
    strcpy(network.ifr_name, interface);
 
1916
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
1917
    if(ret == -1){
 
1918
      perror_plus("ioctl SIOCGIFFLAGS");
 
1919
#ifdef __linux__
 
1920
      if(restore_loglevel){
 
1921
        ret = klogctl(7, NULL, 0);
 
1922
        if(ret == -1){
 
1923
          perror_plus("klogctl");
 
1924
        }
 
1925
      }
 
1926
#endif  /* __linux__ */
 
1927
      exitcode = EX_OSERR;
 
1928
      /* Lower privileges */
 
1929
      errno = 0;
 
1930
      ret = seteuid(uid);
 
1931
      if(ret == -1){
 
1932
        perror_plus("seteuid");
 
1933
      }
 
1934
      goto end;
 
1935
    }
 
1936
    if((network.ifr_flags & IFF_UP) == 0){
 
1937
      network.ifr_flags |= IFF_UP;
 
1938
      take_down_interface = true;
 
1939
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
1940
      if(ret == -1){
 
1941
        take_down_interface = false;
 
1942
        perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
 
1943
        exitcode = EX_OSERR;
 
1944
#ifdef __linux__
 
1945
        if(restore_loglevel){
 
1946
          ret = klogctl(7, NULL, 0);
 
1947
          if(ret == -1){
 
1948
            perror_plus("klogctl");
 
1949
          }
 
1950
        }
 
1951
#endif  /* __linux__ */
 
1952
        /* Lower privileges */
 
1953
        errno = 0;
 
1954
        ret = seteuid(uid);
 
1955
        if(ret == -1){
 
1956
          perror_plus("seteuid");
 
1957
        }
 
1958
        goto end;
 
1959
      }
 
1960
    }
 
1961
    /* Sleep checking until interface is running.
 
1962
       Check every 0.25s, up to total time of delay */
 
1963
    for(int i=0; i < delay * 4; i++){
 
1964
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
1965
      if(ret == -1){
 
1966
        perror_plus("ioctl SIOCGIFFLAGS");
 
1967
      } else if(network.ifr_flags & IFF_RUNNING){
 
1968
        break;
 
1969
      }
 
1970
      struct timespec sleeptime = { .tv_nsec = 250000000 };
 
1971
      ret = nanosleep(&sleeptime, NULL);
 
1972
      if(ret == -1 and errno != EINTR){
 
1973
        perror_plus("nanosleep");
 
1974
      }
 
1975
    }
 
1976
    if(not take_down_interface){
 
1977
      /* We won't need the socket anymore */
 
1978
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1979
      if(ret == -1){
 
1980
        perror_plus("close");
 
1981
      }
 
1982
    }
 
1983
#ifdef __linux__
 
1984
    if(restore_loglevel){
 
1985
      /* Restores kernel loglevel to default */
 
1986
      ret = klogctl(7, NULL, 0);
 
1987
      if(ret == -1){
 
1988
        perror_plus("klogctl");
 
1989
      }
 
1990
    }
 
1991
#endif  /* __linux__ */
 
1992
    /* Lower privileges */
 
1993
    errno = 0;
 
1994
    /* Lower privileges */
 
1995
    ret = seteuid(uid);
 
1996
    if(ret == -1){
 
1997
      perror_plus("seteuid");
2181
1998
    }
2182
1999
  }
2183
2000
  
2224
2041
    /* Connect directly, do not use Zeroconf */
2225
2042
    /* (Mainly meant for debugging) */
2226
2043
    char *address = strrchr(connect_to, ':');
2227
 
    
2228
2044
    if(address == NULL){
2229
2045
      fprintf_plus(stderr, "No colon in address\n");
2230
2046
      exitcode = EX_USAGE;
2235
2051
      goto end;
2236
2052
    }
2237
2053
    
2238
 
    in_port_t port;
 
2054
    uint16_t port;
2239
2055
    errno = 0;
2240
2056
    tmpmax = strtoimax(address+1, &tmp, 10);
2241
2057
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
2242
 
       or tmpmax != (in_port_t)tmpmax){
 
2058
       or tmpmax != (uint16_t)tmpmax){
2243
2059
      fprintf_plus(stderr, "Bad port number\n");
2244
2060
      exitcode = EX_USAGE;
2245
2061
      goto end;
2249
2065
      goto end;
2250
2066
    }
2251
2067
    
2252
 
    port = (in_port_t)tmpmax;
 
2068
    port = (uint16_t)tmpmax;
2253
2069
    *address = '\0';
2254
2070
    /* Colon in address indicates IPv6 */
2255
2071
    int af;
2303
2119
    config.publish_domain = 0;
2304
2120
    
2305
2121
    /* Allocate a new server */
2306
 
    mc.server = avahi_server_new(avahi_simple_poll_get(simple_poll),
2307
 
                                 &config, NULL, NULL, &ret_errno);
 
2122
    mc.server = avahi_server_new(avahi_simple_poll_get
 
2123
                                 (mc.simple_poll), &config, NULL,
 
2124
                                 NULL, &error);
2308
2125
    
2309
2126
    /* Free the Avahi configuration data */
2310
2127
    avahi_server_config_free(&config);
2313
2130
  /* Check if creating the Avahi server object succeeded */
2314
2131
  if(mc.server == NULL){
2315
2132
    fprintf_plus(stderr, "Failed to create Avahi server: %s\n",
2316
 
                 avahi_strerror(ret_errno));
 
2133
                 avahi_strerror(error));
2317
2134
    exitcode = EX_UNAVAILABLE;
2318
2135
    goto end;
2319
2136
  }
2343
2160
    fprintf_plus(stderr, "Starting Avahi loop search\n");
2344
2161
  }
2345
2162
 
2346
 
  ret = avahi_loop_with_timeout(simple_poll,
 
2163
  ret = avahi_loop_with_timeout(mc.simple_poll,
2347
2164
                                (int)(retry_interval * 1000));
2348
2165
  if(debug){
2349
2166
    fprintf_plus(stderr, "avahi_loop_with_timeout exited %s\n",
2363
2180
  if(mc.server != NULL)
2364
2181
    avahi_server_free(mc.server);
2365
2182
  
2366
 
  if(simple_poll != NULL)
2367
 
    avahi_simple_poll_free(simple_poll);
 
2183
  if(mc.simple_poll != NULL)
 
2184
    avahi_simple_poll_free(mc.simple_poll);
2368
2185
  
2369
2186
  if(gnutls_initialized){
2370
2187
    gnutls_certificate_free_credentials(mc.cred);
2375
2192
  if(gpgme_initialized){
2376
2193
    gpgme_release(mc.ctx);
2377
2194
  }
2378
 
  
 
2195
 
2379
2196
  /* Cleans up the circular linked list of Mandos servers the client
2380
2197
     has seen */
2381
2198
  if(mc.current_server != NULL){
2389
2206
  
2390
2207
  /* Re-raise priviliges */
2391
2208
  {
2392
 
    raise_privileges();
2393
 
    
 
2209
    errno = 0;
 
2210
    ret = seteuid(0);
 
2211
    if(ret == -1){
 
2212
      perror_plus("seteuid");
 
2213
    }
2394
2214
    /* Run network hooks */
2395
 
    run_network_hooks("stop", interfaces_hooks != NULL ?
2396
 
                      interfaces_hooks : "", delay);
 
2215
    if(not run_network_hooks("stop", interface, delay)){
 
2216
      goto end;
 
2217
    }
2397
2218
    
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");
 
2219
    /* Take down the network interface */
 
2220
    if(take_down_interface and geteuid() == 0){
 
2221
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
2222
      if(ret == -1){
 
2223
        perror_plus("ioctl SIOCGIFFLAGS");
 
2224
      } else if(network.ifr_flags & IFF_UP){
 
2225
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
 
2226
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
2227
        if(ret == -1){
 
2228
          perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
2408
2229
        }
2409
2230
      }
2410
 
      if(debug and (interfaces_to_take_down == NULL)){
2411
 
        fprintf_plus(stderr, "No interfaces needed to be taken"
2412
 
                     " down\n");
 
2231
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
2232
      if(ret == -1){
 
2233
        perror_plus("close");
2413
2234
      }
2414
2235
    }
2415
 
    
2416
 
    lower_privileges_permanently();
2417
 
  }
2418
 
  
2419
 
  free(interfaces_to_take_down);
2420
 
  free(interfaces_hooks);
 
2236
  }
 
2237
  /* Lower privileges permanently */
 
2238
  errno = 0;
 
2239
  ret = setuid(uid);
 
2240
  if(ret == -1){
 
2241
    perror_plus("setuid");
 
2242
  }
2421
2243
  
2422
2244
  /* Removes the GPGME temp directory and all files inside */
2423
2245
  if(tempdir_created){