/mandos/trunk

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

« back to all changes in this revision

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

Hooks take new "modules" argument, and hook names can contain periods.

* debian/mandos-client.README.Debian: Adjust wording.
* debian/rules (binary-common): Exclude nework-hooks.d directory from
                                dh_fixperms.
* initramfs-tools-hook: Also create network hook directory.  Allow
                        periods in hook names.  Only run executable
                        files in hook directory.  Copy needed modules.
* network-hooks.d/bridge: Fix "/usr/bin/brctl" to "/usr/sbin/brctl".
                          Also take "modules" argument.
* plugins.d/mandos-client.xml (NETWORK HOOKS/REQUIREMENTS): Document
                                                            the
                                                            allowing
                                                            of periods
                                                            in hook
                                                            names.
  (NETWORK HOOKS/REQUIREMENTS/files): Adjust wording.
  (NETWORK HOOKS/REQUIREMENTS/modules): New.
  (NETWORK HOOKS/REQUIREMENTS/MANDOSNETHOOKDIR): Clarify.
  (NETWORK HOOKS/REQUIREMENTS/MODE): Add "modules".

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){
1364
1356
                                         mc.current_server->if_index,
1365
1357
                                         mc.current_server->af);
1366
1358
        if(ret == 0){
1367
 
          avahi_simple_poll_quit(simple_poll);
 
1359
          avahi_simple_poll_quit(mc.simple_poll);
1368
1360
          return 0;
1369
1361
        }
1370
1362
        ret = clock_gettime(CLOCK_MONOTONIC,
1388
1380
  }
1389
1381
}
1390
1382
 
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
1383
bool run_network_hooks(const char *mode, const char *interface,
1444
1384
                       const float delay){
1445
1385
  struct dirent **direntries;
1448
1388
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1449
1389
                         alphasort);
1450
1390
  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
 
    }
 
1391
    perror_plus("scandir");
1459
1392
  } else {
1460
1393
    int devnull = open("/dev/null", O_RDONLY);
1461
1394
    for(int i = 0; i < numhooks; i++){
1473
1406
      pid_t hook_pid = fork();
1474
1407
      if(hook_pid == 0){
1475
1408
        /* 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
1409
        dup2(devnull, STDIN_FILENO);
1491
1410
        close(devnull);
1492
1411
        dup2(STDERR_FILENO, STDOUT_FILENO);
1493
1412
        ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1494
1413
        if(ret == -1){
1495
1414
          perror_plus("setenv");
1496
 
          _exit(EX_OSERR);
 
1415
          return false;
1497
1416
        }
1498
1417
        ret = setenv("DEVICE", interface, 1);
1499
1418
        if(ret == -1){
1500
1419
          perror_plus("setenv");
1501
 
          _exit(EX_OSERR);
 
1420
          return false;
1502
1421
        }
1503
 
        ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
 
1422
        ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1504
1423
        if(ret == -1){
1505
1424
          perror_plus("setenv");
1506
 
          _exit(EX_OSERR);
 
1425
          return false;
1507
1426
        }
1508
1427
        ret = setenv("MODE", mode, 1);
1509
1428
        if(ret == -1){
1510
1429
          perror_plus("setenv");
1511
 
          _exit(EX_OSERR);
 
1430
          return false;
1512
1431
        }
1513
1432
        char *delaystring;
1514
1433
        ret = asprintf(&delaystring, "%f", delay);
1515
1434
        if(ret == -1){
1516
1435
          perror_plus("asprintf");
1517
 
          _exit(EX_OSERR);
 
1436
          return false;
1518
1437
        }
1519
1438
        ret = setenv("DELAY", delaystring, 1);
1520
1439
        if(ret == -1){
1521
1440
          free(delaystring);
1522
1441
          perror_plus("setenv");
1523
 
          _exit(EX_OSERR);
 
1442
          return false;
1524
1443
        }
1525
1444
        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
 
        }
 
1445
        ret = execl(fullname, direntry->d_name, mode, NULL);
 
1446
        perror_plus("execl");
1537
1447
      } else {
1538
1448
        int status;
1539
1449
        if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1573
1483
  return true;
1574
1484
}
1575
1485
 
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
1486
int main(int argc, char *argv[]){
1749
1487
  AvahiSServiceBrowser *sb = NULL;
1750
 
  error_t ret_errno;
 
1488
  int error;
1751
1489
  int ret;
1752
1490
  intmax_t tmpmax;
1753
1491
  char *tmp;
1754
1492
  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;
 
1493
  const char *interface = "";
 
1494
  struct ifreq network;
 
1495
  int sd = -1;
 
1496
  bool take_down_interface = false;
 
1497
  uid_t uid;
 
1498
  gid_t gid;
1759
1499
  char tempdir[] = "/tmp/mandosXXXXXX";
1760
1500
  bool tempdir_created = false;
1761
1501
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1762
1502
  const char *seckey = PATHDIR "/" SECKEY;
1763
1503
  const char *pubkey = PATHDIR "/" PUBKEY;
1764
 
  char *interfaces_hooks = NULL;
1765
 
  size_t interfaces_hooks_size = 0;
1766
1504
  
1767
1505
  bool gnutls_initialized = false;
1768
1506
  bool gpgme_initialized = false;
1830
1568
        .group = 2 },
1831
1569
      { .name = "retry", .key = 132,
1832
1570
        .arg = "SECONDS",
1833
 
        .doc = "Retry interval used when denied by the Mandos server",
 
1571
        .doc = "Retry interval used when denied by the mandos server",
1834
1572
        .group = 2 },
1835
1573
      { .name = "network-hook-dir", .key = 133,
1836
1574
        .arg = "DIR",
1859
1597
        connect_to = arg;
1860
1598
        break;
1861
1599
      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
 
        }
 
1600
        interface = arg;
1867
1601
        break;
1868
1602
      case 's':                 /* --seckey */
1869
1603
        seckey = arg;
1912
1646
        argp_state_help(state, state->out_stream,
1913
1647
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1914
1648
      case 'V':                 /* --version */
 
1649
        fprintf_plus(state->out_stream,
 
1650
                     "Mandos plugin mandos-client: ");
1915
1651
        fprintf_plus(state->out_stream, "%s\n", argp_program_version);
1916
1652
        exit(argp_err_exit_status);
1917
1653
        break;
1942
1678
    }
1943
1679
  }
1944
1680
    
1945
 
  {
 
1681
  if(getuid() == 0){
1946
1682
    /* Work around Debian bug #633582:
1947
1683
       <http://bugs.debian.org/633582> */
 
1684
    struct stat st;
1948
1685
    
1949
1686
    /* 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
 
      }
 
1687
    errno = 0;
 
1688
    ret = seteuid(0);
 
1689
    if(ret == -1){
 
1690
      perror_plus("seteuid");
 
1691
    }
 
1692
    
 
1693
    if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
 
1694
      int seckey_fd = open(seckey, O_RDONLY);
 
1695
      if(seckey_fd == -1){
 
1696
        perror_plus("open");
 
1697
      } else {
 
1698
        ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
 
1699
        if(ret == -1){
 
1700
          perror_plus("fstat");
 
1701
        } else {
 
1702
          if(S_ISREG(st.st_mode)
 
1703
             and st.st_uid == 0 and st.st_gid == 0){
 
1704
            ret = fchown(seckey_fd, uid, gid);
 
1705
            if(ret == -1){
 
1706
              perror_plus("fchown");
 
1707
            }
 
1708
          }
 
1709
        }
 
1710
        TEMP_FAILURE_RETRY(close(seckey_fd));
 
1711
      }
 
1712
    }
 
1713
    
 
1714
    if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
 
1715
      int pubkey_fd = open(pubkey, O_RDONLY);
 
1716
      if(pubkey_fd == -1){
 
1717
        perror_plus("open");
 
1718
      } else {
 
1719
        ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
 
1720
        if(ret == -1){
 
1721
          perror_plus("fstat");
 
1722
        } else {
 
1723
          if(S_ISREG(st.st_mode)
 
1724
             and st.st_uid == 0 and st.st_gid == 0){
 
1725
            ret = fchown(pubkey_fd, uid, gid);
 
1726
            if(ret == -1){
 
1727
              perror_plus("fchown");
 
1728
            }
 
1729
          }
 
1730
        }
 
1731
        TEMP_FAILURE_RETRY(close(pubkey_fd));
 
1732
      }
 
1733
    }
 
1734
    
 
1735
    /* Lower privileges */
 
1736
    errno = 0;
 
1737
    ret = seteuid(uid);
 
1738
    if(ret == -1){
 
1739
      perror_plus("seteuid");
2017
1740
    }
2018
1741
  }
2019
1742
  
2020
1743
  /* Run network hooks */
2021
1744
  {
2022
 
    
2023
 
    if(interfaces != NULL){
2024
 
      interfaces_hooks = malloc(interfaces_size);
2025
 
      if(interfaces_hooks == NULL){
2026
 
        perror_plus("malloc");
2027
 
        goto end;
 
1745
    if(getuid() == 0){
 
1746
      /* Re-raise priviliges */
 
1747
      errno = 0;
 
1748
      ret = seteuid(0);
 
1749
      if(ret == -1){
 
1750
        perror_plus("seteuid");
2028
1751
      }
2029
 
      memcpy(interfaces_hooks, interfaces, interfaces_size);
2030
 
      interfaces_hooks_size = interfaces_size;
2031
 
      argz_stringify(interfaces_hooks, interfaces_hooks_size,
2032
 
                     (int)',');
2033
1752
    }
2034
 
    if(not run_network_hooks("start", interfaces_hooks != NULL ?
2035
 
                             interfaces_hooks : "", delay)){
 
1753
    if(not run_network_hooks("start", interface, delay)){
2036
1754
      goto end;
2037
1755
    }
 
1756
    if(getuid() == 0){
 
1757
      /* Lower privileges */
 
1758
      errno = 0;
 
1759
      ret = seteuid(uid);
 
1760
      if(ret == -1){
 
1761
        perror_plus("seteuid");
 
1762
      }
 
1763
    }
2038
1764
  }
2039
1765
  
2040
1766
  if(not debug){
2041
1767
    avahi_set_log_function(empty_log);
2042
1768
  }
2043
1769
  
 
1770
  if(interface[0] == '\0'){
 
1771
    struct dirent **direntries;
 
1772
    /* First look for interfaces that are up */
 
1773
    ret = scandir(sys_class_net, &direntries, up_interface,
 
1774
                  alphasort);
 
1775
    if(ret == 0){
 
1776
      /* No up interfaces, look for any good interfaces */
 
1777
      free(direntries);
 
1778
      ret = scandir(sys_class_net, &direntries, good_interface,
 
1779
                    alphasort);
 
1780
    }
 
1781
    if(ret >= 1){
 
1782
      /* Pick the first interface returned */
 
1783
      interface = strdup(direntries[0]->d_name);
 
1784
      if(debug){
 
1785
        fprintf_plus(stderr, "Using interface \"%s\"\n", interface);
 
1786
      }
 
1787
      if(interface == NULL){
 
1788
        perror_plus("malloc");
 
1789
        free(direntries);
 
1790
        exitcode = EXIT_FAILURE;
 
1791
        goto end;
 
1792
      }
 
1793
      free(direntries);
 
1794
    } else {
 
1795
      free(direntries);
 
1796
      fprintf_plus(stderr, "Could not find a network interface\n");
 
1797
      exitcode = EXIT_FAILURE;
 
1798
      goto end;
 
1799
    }
 
1800
  }
 
1801
  
2044
1802
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
2045
1803
     from the signal handler */
2046
1804
  /* Initialize the pseudo-RNG for Avahi */
2047
1805
  srand((unsigned int) time(NULL));
2048
 
  simple_poll = avahi_simple_poll_new();
2049
 
  if(simple_poll == NULL){
 
1806
  mc.simple_poll = avahi_simple_poll_new();
 
1807
  if(mc.simple_poll == NULL){
2050
1808
    fprintf_plus(stderr,
2051
1809
                 "Avahi: Failed to create simple poll object.\n");
2052
1810
    exitcode = EX_UNAVAILABLE;
2116
1874
    }
2117
1875
  }
2118
1876
  
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");
 
1877
  /* If the interface is down, bring it up */
 
1878
  if(strcmp(interface, "none") != 0){
 
1879
    if_index = (AvahiIfIndex) if_nametoindex(interface);
 
1880
    if(if_index == 0){
 
1881
      fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
 
1882
      exitcode = EX_UNAVAILABLE;
 
1883
      goto end;
 
1884
    }
 
1885
    
 
1886
    if(quit_now){
 
1887
      goto end;
 
1888
    }
 
1889
    
 
1890
    /* Re-raise priviliges */
 
1891
    errno = 0;
 
1892
    ret = seteuid(0);
 
1893
    if(ret == -1){
 
1894
      perror_plus("seteuid");
 
1895
    }
 
1896
    
 
1897
#ifdef __linux__
 
1898
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
 
1899
       messages about the network interface to mess up the prompt */
 
1900
    ret = klogctl(8, NULL, 5);
 
1901
    bool restore_loglevel = true;
 
1902
    if(ret == -1){
 
1903
      restore_loglevel = false;
 
1904
      perror_plus("klogctl");
 
1905
    }
 
1906
#endif  /* __linux__ */
 
1907
    
 
1908
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
1909
    if(sd < 0){
 
1910
      perror_plus("socket");
 
1911
      exitcode = EX_OSERR;
 
1912
#ifdef __linux__
 
1913
      if(restore_loglevel){
 
1914
        ret = klogctl(7, NULL, 0);
 
1915
        if(ret == -1){
 
1916
          perror_plus("klogctl");
 
1917
        }
 
1918
      }
 
1919
#endif  /* __linux__ */
 
1920
      /* Lower privileges */
 
1921
      errno = 0;
 
1922
      ret = seteuid(uid);
 
1923
      if(ret == -1){
 
1924
        perror_plus("seteuid");
 
1925
      }
 
1926
      goto end;
 
1927
    }
 
1928
    strcpy(network.ifr_name, interface);
 
1929
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
1930
    if(ret == -1){
 
1931
      perror_plus("ioctl SIOCGIFFLAGS");
 
1932
#ifdef __linux__
 
1933
      if(restore_loglevel){
 
1934
        ret = klogctl(7, NULL, 0);
 
1935
        if(ret == -1){
 
1936
          perror_plus("klogctl");
 
1937
        }
 
1938
      }
 
1939
#endif  /* __linux__ */
 
1940
      exitcode = EX_OSERR;
 
1941
      /* Lower privileges */
 
1942
      errno = 0;
 
1943
      ret = seteuid(uid);
 
1944
      if(ret == -1){
 
1945
        perror_plus("seteuid");
 
1946
      }
 
1947
      goto end;
 
1948
    }
 
1949
    if((network.ifr_flags & IFF_UP) == 0){
 
1950
      network.ifr_flags |= IFF_UP;
 
1951
      take_down_interface = true;
 
1952
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
1953
      if(ret == -1){
 
1954
        take_down_interface = false;
 
1955
        perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
 
1956
        exitcode = EX_OSERR;
 
1957
#ifdef __linux__
 
1958
        if(restore_loglevel){
 
1959
          ret = klogctl(7, NULL, 0);
 
1960
          if(ret == -1){
 
1961
            perror_plus("klogctl");
 
1962
          }
 
1963
        }
 
1964
#endif  /* __linux__ */
 
1965
        /* Lower privileges */
 
1966
        errno = 0;
 
1967
        ret = seteuid(uid);
 
1968
        if(ret == -1){
 
1969
          perror_plus("seteuid");
 
1970
        }
 
1971
        goto end;
 
1972
      }
 
1973
    }
 
1974
    /* Sleep checking until interface is running.
 
1975
       Check every 0.25s, up to total time of delay */
 
1976
    for(int i=0; i < delay * 4; i++){
 
1977
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
1978
      if(ret == -1){
 
1979
        perror_plus("ioctl SIOCGIFFLAGS");
 
1980
      } else if(network.ifr_flags & IFF_RUNNING){
 
1981
        break;
 
1982
      }
 
1983
      struct timespec sleeptime = { .tv_nsec = 250000000 };
 
1984
      ret = nanosleep(&sleeptime, NULL);
 
1985
      if(ret == -1 and errno != EINTR){
 
1986
        perror_plus("nanosleep");
 
1987
      }
 
1988
    }
 
1989
    if(not take_down_interface){
 
1990
      /* We won't need the socket anymore */
 
1991
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1992
      if(ret == -1){
 
1993
        perror_plus("close");
 
1994
      }
 
1995
    }
 
1996
#ifdef __linux__
 
1997
    if(restore_loglevel){
 
1998
      /* Restores kernel loglevel to default */
 
1999
      ret = klogctl(7, NULL, 0);
 
2000
      if(ret == -1){
 
2001
        perror_plus("klogctl");
 
2002
      }
 
2003
    }
 
2004
#endif  /* __linux__ */
 
2005
    /* Lower privileges */
 
2006
    errno = 0;
 
2007
    /* Lower privileges */
 
2008
    ret = seteuid(uid);
 
2009
    if(ret == -1){
 
2010
      perror_plus("seteuid");
2181
2011
    }
2182
2012
  }
2183
2013
  
2224
2054
    /* Connect directly, do not use Zeroconf */
2225
2055
    /* (Mainly meant for debugging) */
2226
2056
    char *address = strrchr(connect_to, ':');
2227
 
    
2228
2057
    if(address == NULL){
2229
2058
      fprintf_plus(stderr, "No colon in address\n");
2230
2059
      exitcode = EX_USAGE;
2235
2064
      goto end;
2236
2065
    }
2237
2066
    
2238
 
    in_port_t port;
 
2067
    uint16_t port;
2239
2068
    errno = 0;
2240
2069
    tmpmax = strtoimax(address+1, &tmp, 10);
2241
2070
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
2242
 
       or tmpmax != (in_port_t)tmpmax){
 
2071
       or tmpmax != (uint16_t)tmpmax){
2243
2072
      fprintf_plus(stderr, "Bad port number\n");
2244
2073
      exitcode = EX_USAGE;
2245
2074
      goto end;
2249
2078
      goto end;
2250
2079
    }
2251
2080
    
2252
 
    port = (in_port_t)tmpmax;
 
2081
    port = (uint16_t)tmpmax;
2253
2082
    *address = '\0';
2254
2083
    /* Colon in address indicates IPv6 */
2255
2084
    int af;
2303
2132
    config.publish_domain = 0;
2304
2133
    
2305
2134
    /* Allocate a new server */
2306
 
    mc.server = avahi_server_new(avahi_simple_poll_get(simple_poll),
2307
 
                                 &config, NULL, NULL, &ret_errno);
 
2135
    mc.server = avahi_server_new(avahi_simple_poll_get
 
2136
                                 (mc.simple_poll), &config, NULL,
 
2137
                                 NULL, &error);
2308
2138
    
2309
2139
    /* Free the Avahi configuration data */
2310
2140
    avahi_server_config_free(&config);
2313
2143
  /* Check if creating the Avahi server object succeeded */
2314
2144
  if(mc.server == NULL){
2315
2145
    fprintf_plus(stderr, "Failed to create Avahi server: %s\n",
2316
 
                 avahi_strerror(ret_errno));
 
2146
                 avahi_strerror(error));
2317
2147
    exitcode = EX_UNAVAILABLE;
2318
2148
    goto end;
2319
2149
  }
2343
2173
    fprintf_plus(stderr, "Starting Avahi loop search\n");
2344
2174
  }
2345
2175
 
2346
 
  ret = avahi_loop_with_timeout(simple_poll,
 
2176
  ret = avahi_loop_with_timeout(mc.simple_poll,
2347
2177
                                (int)(retry_interval * 1000));
2348
2178
  if(debug){
2349
2179
    fprintf_plus(stderr, "avahi_loop_with_timeout exited %s\n",
2363
2193
  if(mc.server != NULL)
2364
2194
    avahi_server_free(mc.server);
2365
2195
  
2366
 
  if(simple_poll != NULL)
2367
 
    avahi_simple_poll_free(simple_poll);
 
2196
  if(mc.simple_poll != NULL)
 
2197
    avahi_simple_poll_free(mc.simple_poll);
2368
2198
  
2369
2199
  if(gnutls_initialized){
2370
2200
    gnutls_certificate_free_credentials(mc.cred);
2389
2219
  
2390
2220
  /* Re-raise priviliges */
2391
2221
  {
2392
 
    raise_privileges();
 
2222
    if(getuid() == 0){
 
2223
      errno = 0;
 
2224
      ret = seteuid(0);
 
2225
      if(ret == -1){
 
2226
        perror_plus("seteuid");
 
2227
      }
 
2228
    }
2393
2229
    
2394
2230
    /* Run network hooks */
2395
 
    run_network_hooks("stop", interfaces_hooks != NULL ?
2396
 
                      interfaces_hooks : "", delay);
 
2231
    run_network_hooks("stop", interface, delay);
2397
2232
    
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
    /* Take down the network interface */
 
2234
    if(take_down_interface and geteuid() == 0){
 
2235
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
2236
      if(ret == -1){
 
2237
        perror_plus("ioctl SIOCGIFFLAGS");
 
2238
      } else if(network.ifr_flags & IFF_UP){
 
2239
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
 
2240
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
2241
        if(ret == -1){
 
2242
          perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
2408
2243
        }
2409
2244
      }
2410
 
      if(debug and (interfaces_to_take_down == NULL)){
2411
 
        fprintf_plus(stderr, "No interfaces needed to be taken"
2412
 
                     " down\n");
 
2245
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
2246
      if(ret == -1){
 
2247
        perror_plus("close");
2413
2248
      }
2414
2249
    }
2415
 
    
2416
 
    lower_privileges_permanently();
2417
 
  }
2418
 
  
2419
 
  free(interfaces_to_take_down);
2420
 
  free(interfaces_hooks);
 
2250
  }
 
2251
  if(getuid() == 0){
 
2252
    /* Lower privileges permanently */
 
2253
    errno = 0;
 
2254
    ret = setuid(uid);
 
2255
    if(ret == -1){
 
2256
      perror_plus("setuid");
 
2257
    }
 
2258
  }
2421
2259
  
2422
2260
  /* Removes the GPGME temp directory and all files inside */
2423
2261
  if(tempdir_created){