/mandos/release

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

« back to all changes in this revision

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

  • Committer: Teddy Hogeborn
  • Date: 2013-12-31 16:02:18 UTC
  • mto: (237.7.272 trunk)
  • mto: This revision was merged to the branch mainline in revision 307.
  • Revision ID: teddy@recompile.se-20131231160218-plmmb0o0iifkd5l2
Specify BusName in systemd service file.

* mandos.service (BusName): Uncommented.

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-2014 Teddy Hogeborn
13
 
 * Copyright © 2008-2014 Björn Påhlsson
 
12
 * Copyright © 2008-2013 Teddy Hogeborn
 
13
 * Copyright © 2008-2013 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
32
32
/* Needed by GPGME, specifically gpgme_data_seek() */
33
33
#ifndef _LARGEFILE_SOURCE
34
34
#define _LARGEFILE_SOURCE
35
 
#endif  /* not _LARGEFILE_SOURCE */
 
35
#endif
36
36
#ifndef _FILE_OFFSET_BITS
37
37
#define _FILE_OFFSET_BITS 64
38
 
#endif  /* not _FILE_OFFSET_BITS */
 
38
#endif
39
39
 
40
40
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), asprintf() */
41
41
 
55
55
                                   opendir(), DIR */
56
56
#include <sys/stat.h>           /* open(), S_ISREG */
57
57
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
58
 
                                   inet_pton(), connect(),
59
 
                                   getnameinfo() */
 
58
                                   inet_pton(), connect() */
60
59
#include <fcntl.h>              /* open() */
61
60
#include <dirent.h>             /* opendir(), struct dirent, readdir()
62
61
                                 */
74
73
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
75
74
                                   getuid(), getgid(), seteuid(),
76
75
                                   setgid(), pause(), _exit() */
77
 
#include <arpa/inet.h>          /* inet_pton(), htons() */
 
76
#include <arpa/inet.h>          /* inet_pton(), htons, inet_ntop() */
78
77
#include <iso646.h>             /* not, or, and */
79
78
#include <argp.h>               /* struct argp_option, error_t, struct
80
79
                                   argp_state, struct argp,
92
91
                                   argz_delete(), argz_append(),
93
92
                                   argz_stringify(), argz_add(),
94
93
                                   argz_count() */
95
 
#include <netdb.h>              /* getnameinfo(), NI_NUMERICHOST,
96
 
                                   EAI_SYSTEM, gai_strerror() */
97
94
 
98
95
#ifdef __linux__
99
96
#include <sys/klog.h>           /* klogctl() */
141
138
static const char sys_class_net[] = "/sys/class/net";
142
139
char *connect_to = NULL;
143
140
const char *hookdir = HOOKDIR;
144
 
int hookdir_fd = -1;
145
141
uid_t uid = 65534;
146
142
gid_t gid = 65534;
147
143
 
184
180
  perror(print_text);
185
181
}
186
182
 
187
 
__attribute__((format (gnu_printf, 2, 3), nonnull))
 
183
__attribute__((format (gnu_printf, 2, 3)))
188
184
int fprintf_plus(FILE *stream, const char *format, ...){
189
185
  va_list ap;
190
186
  va_start (ap, format);
199
195
 * bytes. "buffer_capacity" is how much is currently allocated,
200
196
 * "buffer_length" is how much is already used.
201
197
 */
202
 
__attribute__((nonnull, warn_unused_result))
203
198
size_t incbuffer(char **buffer, size_t buffer_length,
204
199
                 size_t buffer_capacity){
205
200
  if(buffer_length + BUFFER_SIZE > buffer_capacity){
206
 
    char *new_buf = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
207
 
    if(new_buf == NULL){
208
 
      int old_errno = errno;
209
 
      free(*buffer);
210
 
      errno = old_errno;
211
 
      *buffer = NULL;
 
201
    *buffer = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
 
202
    if(buffer == NULL){
212
203
      return 0;
213
204
    }
214
 
    *buffer = new_buf;
215
205
    buffer_capacity += BUFFER_SIZE;
216
206
  }
217
207
  return buffer_capacity;
218
208
}
219
209
 
220
210
/* Add server to set of servers to retry periodically */
221
 
__attribute__((nonnull, warn_unused_result))
222
211
bool add_server(const char *ip, in_port_t port, AvahiIfIndex if_index,
223
212
                int af, server **current_server){
224
213
  int ret;
235
224
    perror_plus("strdup");
236
225
    return false;
237
226
  }
238
 
  ret = clock_gettime(CLOCK_MONOTONIC, &(new_server->last_seen));
239
 
  if(ret == -1){
240
 
    perror_plus("clock_gettime");
241
 
    return false;
242
 
  }
243
227
  /* Special case of first server */
244
228
  if(*current_server == NULL){
245
229
    new_server->next = new_server;
246
230
    new_server->prev = new_server;
247
231
    *current_server = new_server;
 
232
  /* Place the new server last in the list */
248
233
  } else {
249
 
    /* Place the new server last in the list */
250
234
    new_server->next = *current_server;
251
235
    new_server->prev = (*current_server)->prev;
252
236
    new_server->prev->next = new_server;
253
237
    (*current_server)->prev = new_server;
254
238
  }
 
239
  ret = clock_gettime(CLOCK_MONOTONIC, &(*current_server)->last_seen);
 
240
  if(ret == -1){
 
241
    perror_plus("clock_gettime");
 
242
    return false;
 
243
  }
255
244
  return true;
256
245
}
257
246
 
258
247
/* 
259
248
 * Initialize GPGME.
260
249
 */
261
 
__attribute__((nonnull, warn_unused_result))
262
 
static bool init_gpgme(const char * const seckey,
263
 
                       const char * const pubkey,
264
 
                       const char * const tempdir,
265
 
                       mandos_context *mc){
 
250
static bool init_gpgme(const char *seckey, const char *pubkey,
 
251
                       const char *tempdir, mandos_context *mc){
266
252
  gpgme_error_t rc;
267
253
  gpgme_engine_info_t engine_info;
268
254
  
269
255
  /*
270
256
   * Helper function to insert pub and seckey to the engine keyring.
271
257
   */
272
 
  bool import_key(const char * const filename){
 
258
  bool import_key(const char *filename){
273
259
    int ret;
274
260
    int fd;
275
261
    gpgme_data_t pgp_data;
356
342
 * Decrypt OpenPGP data.
357
343
 * Returns -1 on error
358
344
 */
359
 
__attribute__((nonnull, warn_unused_result))
360
345
static ssize_t pgp_packet_decrypt(const char *cryptotext,
361
346
                                  size_t crypto_size,
362
347
                                  char **plaintext,
482
467
  return plaintext_length;
483
468
}
484
469
 
485
 
__attribute__((warn_unused_result))
486
 
static const char *safer_gnutls_strerror(int value){
 
470
static const char * safer_gnutls_strerror(int value){
487
471
  const char *ret = gnutls_strerror(value);
488
472
  if(ret == NULL)
489
473
    ret = "(unknown)";
491
475
}
492
476
 
493
477
/* GnuTLS log function callback */
494
 
__attribute__((nonnull))
495
478
static void debuggnutls(__attribute__((unused)) int level,
496
479
                        const char* string){
497
480
  fprintf_plus(stderr, "GnuTLS: %s", string);
498
481
}
499
482
 
500
 
__attribute__((nonnull, warn_unused_result))
501
483
static int init_gnutls_global(const char *pubkeyfilename,
502
484
                              const char *seckeyfilename,
503
485
                              mandos_context *mc){
577
559
  return -1;
578
560
}
579
561
 
580
 
__attribute__((nonnull, warn_unused_result))
581
562
static int init_gnutls_session(gnutls_session_t *session,
582
563
                               mandos_context *mc){
583
564
  int ret;
640
621
                      __attribute__((unused)) const char *txt){}
641
622
 
642
623
/* Called when a Mandos server is found */
643
 
__attribute__((nonnull, warn_unused_result))
644
624
static int start_mandos_communication(const char *ip, in_port_t port,
645
625
                                      AvahiIfIndex if_index,
646
626
                                      int af, mandos_context *mc){
647
627
  int ret, tcp_sd = -1;
648
628
  ssize_t sret;
649
 
  struct sockaddr_storage to;
 
629
  union {
 
630
    struct sockaddr_in in;
 
631
    struct sockaddr_in6 in6;
 
632
  } to;
650
633
  char *buffer = NULL;
651
634
  char *decrypted_buffer = NULL;
652
635
  size_t buffer_length = 0;
733
716
  
734
717
  memset(&to, 0, sizeof(to));
735
718
  if(af == AF_INET6){
736
 
    ((struct sockaddr_in6 *)&to)->sin6_family = (sa_family_t)af;
737
 
    ret = inet_pton(af, ip, &((struct sockaddr_in6 *)&to)->sin6_addr);
 
719
    to.in6.sin6_family = (sa_family_t)af;
 
720
    ret = inet_pton(af, ip, &to.in6.sin6_addr);
738
721
  } else {                      /* IPv4 */
739
 
    ((struct sockaddr_in *)&to)->sin_family = (sa_family_t)af;
740
 
    ret = inet_pton(af, ip, &((struct sockaddr_in *)&to)->sin_addr);
 
722
    to.in.sin_family = (sa_family_t)af;
 
723
    ret = inet_pton(af, ip, &to.in.sin_addr);
741
724
  }
742
725
  if(ret < 0 ){
743
726
    int e = errno;
752
735
    goto mandos_end;
753
736
  }
754
737
  if(af == AF_INET6){
755
 
    ((struct sockaddr_in6 *)&to)->sin6_port = htons(port);
756
 
    if(IN6_IS_ADDR_LINKLOCAL
757
 
       (&((struct sockaddr_in6 *)&to)->sin6_addr)){
 
738
    to.in6.sin6_port = htons(port);    
 
739
#ifdef __GNUC__
 
740
#pragma GCC diagnostic push
 
741
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
 
742
#endif
 
743
    if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
 
744
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower */
 
745
#ifdef __GNUC__
 
746
#pragma GCC diagnostic pop
 
747
#endif
758
748
      if(if_index == AVAHI_IF_UNSPEC){
759
749
        fprintf_plus(stderr, "An IPv6 link-local address is"
760
750
                     " incomplete without a network interface\n");
762
752
        goto mandos_end;
763
753
      }
764
754
      /* Set the network interface number as scope */
765
 
      ((struct sockaddr_in6 *)&to)->sin6_scope_id = (uint32_t)if_index;
 
755
      to.in6.sin6_scope_id = (uint32_t)if_index;
766
756
    }
767
757
  } else {
768
 
    ((struct sockaddr_in *)&to)->sin_port = htons(port);
 
758
    to.in.sin_port = htons(port);
769
759
  }
770
760
  
771
761
  if(quit_now){
788
778
    }
789
779
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
790
780
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
 
781
    const char *pcret;
791
782
    if(af == AF_INET6){
792
 
      ret = getnameinfo((struct sockaddr *)&to,
793
 
                        sizeof(struct sockaddr_in6),
794
 
                        addrstr, sizeof(addrstr), NULL, 0,
795
 
                        NI_NUMERICHOST);
 
783
      pcret = inet_ntop(af, &(to.in6.sin6_addr), addrstr,
 
784
                        sizeof(addrstr));
796
785
    } else {
797
 
      ret = getnameinfo((struct sockaddr *)&to,
798
 
                        sizeof(struct sockaddr_in),
799
 
                        addrstr, sizeof(addrstr), NULL, 0,
800
 
                        NI_NUMERICHOST);
 
786
      pcret = inet_ntop(af, &(to.in.sin_addr), addrstr,
 
787
                        sizeof(addrstr));
801
788
    }
802
 
    if(ret == EAI_SYSTEM){
803
 
      perror_plus("getnameinfo");
804
 
    } else if(ret != 0) {
805
 
      fprintf_plus(stderr, "getnameinfo: %s", gai_strerror(ret));
806
 
    } else if(strcmp(addrstr, ip) != 0){
807
 
      fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
 
789
    if(pcret == NULL){
 
790
      perror_plus("inet_ntop");
 
791
    } else {
 
792
      if(strcmp(addrstr, ip) != 0){
 
793
        fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
 
794
      }
808
795
    }
809
796
  }
810
797
  
814
801
  }
815
802
  
816
803
  if(af == AF_INET6){
817
 
    ret = connect(tcp_sd, (struct sockaddr *)&to,
818
 
                  sizeof(struct sockaddr_in6));
 
804
    ret = connect(tcp_sd, &to.in6, sizeof(to));
819
805
  } else {
820
 
    ret = connect(tcp_sd, (struct sockaddr *)&to, /* IPv4 */
821
 
                  sizeof(struct sockaddr_in));
 
806
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
822
807
  }
823
808
  if(ret < 0){
824
 
    if((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
 
809
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
825
810
      int e = errno;
826
811
      perror_plus("connect");
827
812
      errno = e;
1042
1027
  return retval;
1043
1028
}
1044
1029
 
1045
 
__attribute__((nonnull))
1046
1030
static void resolve_callback(AvahiSServiceResolver *r,
1047
1031
                             AvahiIfIndex interface,
1048
1032
                             AvahiProtocol proto,
1056
1040
                             AVAHI_GCC_UNUSED AvahiStringList *txt,
1057
1041
                             AVAHI_GCC_UNUSED AvahiLookupResultFlags
1058
1042
                             flags,
1059
 
                             void *mc){
 
1043
                             void* mc){
1060
1044
  if(r == NULL){
1061
1045
    return;
1062
1046
  }
1115
1099
                            const char *domain,
1116
1100
                            AVAHI_GCC_UNUSED AvahiLookupResultFlags
1117
1101
                            flags,
1118
 
                            void *mc){
 
1102
                            void* mc){
1119
1103
  if(b == NULL){
1120
1104
    return;
1121
1105
  }
1181
1165
  errno = old_errno;
1182
1166
}
1183
1167
 
1184
 
__attribute__((nonnull, warn_unused_result))
1185
1168
bool get_flags(const char *ifname, struct ifreq *ifr){
1186
1169
  int ret;
1187
1170
  error_t ret_errno;
1206
1189
  return true;
1207
1190
}
1208
1191
 
1209
 
__attribute__((nonnull, warn_unused_result))
1210
1192
bool good_flags(const char *ifname, const struct ifreq *ifr){
1211
1193
  
1212
1194
  /* Reject the loopback device */
1254
1236
 * corresponds to an acceptable network device.
1255
1237
 * (This function is passed to scandir(3) as a filter function.)
1256
1238
 */
1257
 
__attribute__((nonnull, warn_unused_result))
1258
1239
int good_interface(const struct dirent *if_entry){
1259
1240
  if(if_entry->d_name[0] == '.'){
1260
1241
    return 0;
1278
1259
/* 
1279
1260
 * This function determines if a network interface is up.
1280
1261
 */
1281
 
__attribute__((nonnull, warn_unused_result))
1282
1262
bool interface_is_up(const char *interface){
1283
1263
  struct ifreq ifr;
1284
1264
  if(not get_flags(interface, &ifr)){
1295
1275
/* 
1296
1276
 * This function determines if a network interface is running
1297
1277
 */
1298
 
__attribute__((nonnull, warn_unused_result))
1299
1278
bool interface_is_running(const char *interface){
1300
1279
  struct ifreq ifr;
1301
1280
  if(not get_flags(interface, &ifr)){
1309
1288
  return (bool)(ifr.ifr_flags & IFF_RUNNING);
1310
1289
}
1311
1290
 
1312
 
__attribute__((nonnull, pure, warn_unused_result))
1313
1291
int notdotentries(const struct dirent *direntry){
1314
1292
  /* Skip "." and ".." */
1315
1293
  if(direntry->d_name[0] == '.'
1322
1300
}
1323
1301
 
1324
1302
/* Is this directory entry a runnable program? */
1325
 
__attribute__((nonnull, warn_unused_result))
1326
1303
int runnable_hook(const struct dirent *direntry){
1327
1304
  int ret;
1328
1305
  size_t sret;
1336
1313
  sret = strspn(direntry->d_name, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1337
1314
                "abcdefghijklmnopqrstuvwxyz"
1338
1315
                "0123456789"
1339
 
                "_.-");
 
1316
                "_-");
1340
1317
  if((direntry->d_name)[sret] != '\0'){
1341
1318
    /* Contains non-allowed characters */
1342
1319
    if(debug){
1346
1323
    return 0;
1347
1324
  }
1348
1325
  
1349
 
  ret = fstatat(hookdir_fd, direntry->d_name, &st, 0);
 
1326
  char *fullname = NULL;
 
1327
  ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
 
1328
  if(ret < 0){
 
1329
    perror_plus("asprintf");
 
1330
    return 0;
 
1331
  }
 
1332
  
 
1333
  ret = stat(fullname, &st);
1350
1334
  if(ret == -1){
1351
1335
    if(debug){
1352
1336
      perror_plus("Could not stat hook");
1376
1360
  return 1;
1377
1361
}
1378
1362
 
1379
 
__attribute__((nonnull, warn_unused_result))
1380
1363
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval,
1381
1364
                            mandos_context *mc){
1382
1365
  int ret;
1386
1369
  
1387
1370
  while(true){
1388
1371
    if(mc->current_server == NULL){
1389
 
      if(debug){
 
1372
      if (debug){
1390
1373
        fprintf_plus(stderr, "Wait until first server is found."
1391
1374
                     " No timeout!\n");
1392
1375
      }
1393
1376
      ret = avahi_simple_poll_iterate(s, -1);
1394
1377
    } else {
1395
 
      if(debug){
 
1378
      if (debug){
1396
1379
        fprintf_plus(stderr, "Check current_server if we should run"
1397
1380
                     " it, or wait\n");
1398
1381
      }
1415
1398
                     - ((intmax_t)waited_time.tv_sec * 1000))
1416
1399
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
1417
1400
      
1418
 
      if(debug){
 
1401
      if (debug){
1419
1402
        fprintf_plus(stderr, "Blocking for %" PRIdMAX " ms\n",
1420
1403
                     block_time);
1421
1404
      }
1443
1426
      ret = avahi_simple_poll_iterate(s, (int)block_time);
1444
1427
    }
1445
1428
    if(ret != 0){
1446
 
      if(ret > 0 or errno != EINTR){
 
1429
      if (ret > 0 or errno != EINTR){
1447
1430
        return (ret != 1) ? ret : 0;
1448
1431
      }
1449
1432
    }
1451
1434
}
1452
1435
 
1453
1436
/* Set effective uid to 0, return errno */
1454
 
__attribute__((warn_unused_result))
1455
1437
error_t raise_privileges(void){
1456
1438
  error_t old_errno = errno;
1457
1439
  error_t ret_errno = 0;
1464
1446
}
1465
1447
 
1466
1448
/* Set effective and real user ID to 0.  Return errno. */
1467
 
__attribute__((warn_unused_result))
1468
1449
error_t raise_privileges_permanently(void){
1469
1450
  error_t old_errno = errno;
1470
1451
  error_t ret_errno = raise_privileges();
1481
1462
}
1482
1463
 
1483
1464
/* Set effective user ID to unprivileged saved user ID */
1484
 
__attribute__((warn_unused_result))
1485
1465
error_t lower_privileges(void){
1486
1466
  error_t old_errno = errno;
1487
1467
  error_t ret_errno = 0;
1494
1474
}
1495
1475
 
1496
1476
/* Lower privileges permanently */
1497
 
__attribute__((warn_unused_result))
1498
1477
error_t lower_privileges_permanently(void){
1499
1478
  error_t old_errno = errno;
1500
1479
  error_t ret_errno = 0;
1506
1485
  return ret_errno;
1507
1486
}
1508
1487
 
1509
 
#ifndef O_CLOEXEC
1510
 
/*
1511
 
 * Based on the example in the GNU LibC manual chapter 13.13 "File
1512
 
 * Descriptor Flags".
1513
 
 | [[info:libc:Descriptor%20Flags][File Descriptor Flags]] |
1514
 
 */
1515
 
__attribute__((warn_unused_result))
1516
 
static int set_cloexec_flag(int fd){
1517
 
  int ret = (int)TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD, 0));
1518
 
  /* If reading the flags failed, return error indication now. */
1519
 
  if(ret < 0){
1520
 
    return ret;
1521
 
  }
1522
 
  /* Store modified flag word in the descriptor. */
1523
 
  return (int)TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD,
1524
 
                                       ret | FD_CLOEXEC));
1525
 
}
1526
 
#endif  /* not O_CLOEXEC */
1527
 
 
1528
 
__attribute__((nonnull))
1529
 
void run_network_hooks(const char *mode, const char *interface,
 
1488
bool run_network_hooks(const char *mode, const char *interface,
1530
1489
                       const float delay){
1531
1490
  struct dirent **direntries;
1532
 
  if(hookdir_fd == -1){
1533
 
    hookdir_fd = open(hookdir, O_RDONLY |
1534
 
#ifdef O_CLOEXEC
1535
 
                      O_CLOEXEC
1536
 
#else  /* not O_CLOEXEC */
1537
 
                      0
1538
 
#endif  /* not O_CLOEXEC */
1539
 
                      );
1540
 
    if(hookdir_fd == -1){
1541
 
      if(errno == ENOENT){
1542
 
        if(debug){
1543
 
          fprintf_plus(stderr, "Network hook directory \"%s\" not"
1544
 
                       " found\n", hookdir);
1545
 
        }
1546
 
      } else {
1547
 
        perror_plus("open");
1548
 
      }
1549
 
      return;
1550
 
    }
1551
 
#ifndef O_CLOEXEC
1552
 
    if(set_cloexec_flag(hookdir_fd) < 0){
1553
 
      perror_plus("set_cloexec_flag");
1554
 
      if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1555
 
        perror_plus("close");
1556
 
      } else {
1557
 
        hookdir_fd = -1;
1558
 
      }
1559
 
      return;
1560
 
    }
1561
 
#endif  /* not O_CLOEXEC */
1562
 
  }
1563
 
#ifdef __GLIBC__
1564
 
#if __GLIBC_PREREQ(2, 15)
1565
 
  int numhooks = scandirat(hookdir_fd, ".", &direntries,
1566
 
                           runnable_hook, alphasort);
1567
 
#else  /* not __GLIBC_PREREQ(2, 15) */
1568
 
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1569
 
                         alphasort);
1570
 
#endif  /* not __GLIBC_PREREQ(2, 15) */
1571
 
#else   /* not __GLIBC__ */
1572
 
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1573
 
                         alphasort);
1574
 
#endif  /* not __GLIBC__ */
 
1491
  struct dirent *direntry;
 
1492
  int ret;
 
1493
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
1494
                         alphasort);
1575
1495
  if(numhooks == -1){
1576
 
    perror_plus("scandir");
1577
 
    return;
1578
 
  }
1579
 
  struct dirent *direntry;
1580
 
  int ret;
1581
 
  int devnull = open("/dev/null", O_RDONLY);
1582
 
  for(int i = 0; i < numhooks; i++){
1583
 
    direntry = direntries[i];
1584
 
    if(debug){
1585
 
      fprintf_plus(stderr, "Running network hook \"%s\"\n",
1586
 
                   direntry->d_name);
 
1496
    if(errno == ENOENT){
 
1497
      if(debug){
 
1498
        fprintf_plus(stderr, "Network hook directory \"%s\" not"
 
1499
                     " found\n", hookdir);
 
1500
      }
 
1501
    } else {
 
1502
      perror_plus("scandir");
1587
1503
    }
1588
 
    pid_t hook_pid = fork();
1589
 
    if(hook_pid == 0){
1590
 
      /* Child */
1591
 
      /* Raise privileges */
1592
 
      if(raise_privileges_permanently() != 0){
1593
 
        perror_plus("Failed to raise privileges");
1594
 
        _exit(EX_NOPERM);
1595
 
      }
1596
 
      /* Set group */
1597
 
      errno = 0;
1598
 
      ret = setgid(0);
1599
 
      if(ret == -1){
1600
 
        perror_plus("setgid");
1601
 
        _exit(EX_NOPERM);
1602
 
      }
1603
 
      /* Reset supplementary groups */
1604
 
      errno = 0;
1605
 
      ret = setgroups(0, NULL);
1606
 
      if(ret == -1){
1607
 
        perror_plus("setgroups");
1608
 
        _exit(EX_NOPERM);
1609
 
      }
1610
 
      ret = dup2(devnull, STDIN_FILENO);
1611
 
      if(ret == -1){
1612
 
        perror_plus("dup2(devnull, STDIN_FILENO)");
1613
 
        _exit(EX_OSERR);
1614
 
      }
1615
 
      ret = close(devnull);
1616
 
      if(ret == -1){
1617
 
        perror_plus("close");
1618
 
        _exit(EX_OSERR);
1619
 
      }
1620
 
      ret = dup2(STDERR_FILENO, STDOUT_FILENO);
1621
 
      if(ret == -1){
1622
 
        perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
1623
 
        _exit(EX_OSERR);
1624
 
      }
1625
 
      ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1626
 
      if(ret == -1){
1627
 
        perror_plus("setenv");
1628
 
        _exit(EX_OSERR);
1629
 
      }
1630
 
      ret = setenv("DEVICE", interface, 1);
1631
 
      if(ret == -1){
1632
 
        perror_plus("setenv");
1633
 
        _exit(EX_OSERR);
1634
 
      }
1635
 
      ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1636
 
      if(ret == -1){
1637
 
        perror_plus("setenv");
1638
 
        _exit(EX_OSERR);
1639
 
      }
1640
 
      ret = setenv("MODE", mode, 1);
1641
 
      if(ret == -1){
1642
 
        perror_plus("setenv");
1643
 
        _exit(EX_OSERR);
1644
 
      }
1645
 
      char *delaystring;
1646
 
      ret = asprintf(&delaystring, "%f", (double)delay);
1647
 
      if(ret == -1){
 
1504
  } else {
 
1505
    int devnull = open("/dev/null", O_RDONLY);
 
1506
    for(int i = 0; i < numhooks; i++){
 
1507
      direntry = direntries[i];
 
1508
      char *fullname = NULL;
 
1509
      ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
 
1510
      if(ret < 0){
1648
1511
        perror_plus("asprintf");
1649
 
        _exit(EX_OSERR);
1650
 
      }
1651
 
      ret = setenv("DELAY", delaystring, 1);
1652
 
      if(ret == -1){
 
1512
        continue;
 
1513
      }
 
1514
      if(debug){
 
1515
        fprintf_plus(stderr, "Running network hook \"%s\"\n",
 
1516
                     direntry->d_name);
 
1517
      }
 
1518
      pid_t hook_pid = fork();
 
1519
      if(hook_pid == 0){
 
1520
        /* Child */
 
1521
        /* Raise privileges */
 
1522
        raise_privileges_permanently();
 
1523
        /* Set group */
 
1524
        errno = 0;
 
1525
        ret = setgid(0);
 
1526
        if(ret == -1){
 
1527
          perror_plus("setgid");
 
1528
        }
 
1529
        /* Reset supplementary groups */
 
1530
        errno = 0;
 
1531
        ret = setgroups(0, NULL);
 
1532
        if(ret == -1){
 
1533
          perror_plus("setgroups");
 
1534
        }
 
1535
        dup2(devnull, STDIN_FILENO);
 
1536
        close(devnull);
 
1537
        dup2(STDERR_FILENO, STDOUT_FILENO);
 
1538
        ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
 
1539
        if(ret == -1){
 
1540
          perror_plus("setenv");
 
1541
          _exit(EX_OSERR);
 
1542
        }
 
1543
        ret = setenv("DEVICE", interface, 1);
 
1544
        if(ret == -1){
 
1545
          perror_plus("setenv");
 
1546
          _exit(EX_OSERR);
 
1547
        }
 
1548
        ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
 
1549
        if(ret == -1){
 
1550
          perror_plus("setenv");
 
1551
          _exit(EX_OSERR);
 
1552
        }
 
1553
        ret = setenv("MODE", mode, 1);
 
1554
        if(ret == -1){
 
1555
          perror_plus("setenv");
 
1556
          _exit(EX_OSERR);
 
1557
        }
 
1558
        char *delaystring;
 
1559
        ret = asprintf(&delaystring, "%f", delay);
 
1560
        if(ret == -1){
 
1561
          perror_plus("asprintf");
 
1562
          _exit(EX_OSERR);
 
1563
        }
 
1564
        ret = setenv("DELAY", delaystring, 1);
 
1565
        if(ret == -1){
 
1566
          free(delaystring);
 
1567
          perror_plus("setenv");
 
1568
          _exit(EX_OSERR);
 
1569
        }
1653
1570
        free(delaystring);
1654
 
        perror_plus("setenv");
1655
 
        _exit(EX_OSERR);
1656
 
      }
1657
 
      free(delaystring);
1658
 
      if(connect_to != NULL){
1659
 
        ret = setenv("CONNECT", connect_to, 1);
1660
 
        if(ret == -1){
1661
 
          perror_plus("setenv");
1662
 
          _exit(EX_OSERR);
1663
 
        }
1664
 
      }
1665
 
      if(fexecve(hookdir_fd, (char *const [])
1666
 
                 { direntry->d_name, NULL }, environ) == -1){
1667
 
        perror_plus("fexecve");
1668
 
        _exit(EXIT_FAILURE);
1669
 
      }
1670
 
    } else {
1671
 
      int status;
1672
 
      if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1673
 
        perror_plus("waitpid");
1674
 
        continue;
1675
 
      }
1676
 
      if(WIFEXITED(status)){
1677
 
        if(WEXITSTATUS(status) != 0){
1678
 
          fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1679
 
                       " with status %d\n", direntry->d_name,
1680
 
                       WEXITSTATUS(status));
1681
 
          continue;
1682
 
        }
1683
 
      } else if(WIFSIGNALED(status)){
1684
 
        fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1685
 
                     " signal %d\n", direntry->d_name,
1686
 
                     WTERMSIG(status));
1687
 
        continue;
 
1571
        if(connect_to != NULL){
 
1572
          ret = setenv("CONNECT", connect_to, 1);
 
1573
          if(ret == -1){
 
1574
            perror_plus("setenv");
 
1575
            _exit(EX_OSERR);
 
1576
          }
 
1577
        }
 
1578
        if(execl(fullname, direntry->d_name, mode, NULL) == -1){
 
1579
          perror_plus("execl");
 
1580
          _exit(EXIT_FAILURE);
 
1581
        }
1688
1582
      } else {
1689
 
        fprintf_plus(stderr, "Warning: network hook \"%s\""
1690
 
                     " crashed\n", direntry->d_name);
1691
 
        continue;
1692
 
      }
1693
 
    }
1694
 
    if(debug){
1695
 
      fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1696
 
                   direntry->d_name);
1697
 
    }
1698
 
  }
1699
 
  if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1700
 
    perror_plus("close");
1701
 
  } else {
1702
 
    hookdir_fd = -1;
1703
 
  }
1704
 
  close(devnull);
 
1583
        int status;
 
1584
        if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
 
1585
          perror_plus("waitpid");
 
1586
          free(fullname);
 
1587
          continue;
 
1588
        }
 
1589
        if(WIFEXITED(status)){
 
1590
          if(WEXITSTATUS(status) != 0){
 
1591
            fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
 
1592
                         " with status %d\n", direntry->d_name,
 
1593
                         WEXITSTATUS(status));
 
1594
            free(fullname);
 
1595
            continue;
 
1596
          }
 
1597
        } else if(WIFSIGNALED(status)){
 
1598
          fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
 
1599
                       " signal %d\n", direntry->d_name,
 
1600
                       WTERMSIG(status));
 
1601
          free(fullname);
 
1602
          continue;
 
1603
        } else {
 
1604
          fprintf_plus(stderr, "Warning: network hook \"%s\""
 
1605
                       " crashed\n", direntry->d_name);
 
1606
          free(fullname);
 
1607
          continue;
 
1608
        }
 
1609
      }
 
1610
      free(fullname);
 
1611
      if(debug){
 
1612
        fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
 
1613
                     direntry->d_name);
 
1614
      }
 
1615
    }
 
1616
    close(devnull);
 
1617
  }
 
1618
  return true;
1705
1619
}
1706
1620
 
1707
 
__attribute__((nonnull, warn_unused_result))
1708
1621
error_t bring_up_interface(const char *const interface,
1709
1622
                           const float delay){
 
1623
  int sd = -1;
1710
1624
  error_t old_errno = errno;
1711
 
  int ret;
 
1625
  error_t ret_errno = 0;
 
1626
  int ret, ret_setflags;
1712
1627
  struct ifreq network;
1713
1628
  unsigned int if_index = if_nametoindex(interface);
1714
1629
  if(if_index == 0){
1723
1638
  }
1724
1639
  
1725
1640
  if(not interface_is_up(interface)){
1726
 
    error_t ret_errno = 0, ioctl_errno = 0;
1727
 
    if(not get_flags(interface, &network)){
 
1641
    if(not get_flags(interface, &network) and debug){
1728
1642
      ret_errno = errno;
1729
1643
      fprintf_plus(stderr, "Failed to get flags for interface "
1730
1644
                   "\"%s\"\n", interface);
1731
 
      errno = old_errno;
1732
1645
      return ret_errno;
1733
1646
    }
1734
 
    network.ifr_flags |= IFF_UP; /* set flag */
 
1647
    network.ifr_flags |= IFF_UP;
1735
1648
    
1736
 
    int sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1737
 
    if(sd == -1){
 
1649
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
1650
    if(sd < 0){
1738
1651
      ret_errno = errno;
1739
1652
      perror_plus("socket");
1740
1653
      errno = old_errno;
1741
1654
      return ret_errno;
1742
1655
    }
1743
 
    
 
1656
  
1744
1657
    if(quit_now){
1745
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1746
 
      if(ret == -1){
1747
 
        perror_plus("close");
1748
 
      }
 
1658
      close(sd);
1749
1659
      errno = old_errno;
1750
1660
      return EINTR;
1751
1661
    }
1755
1665
                   interface);
1756
1666
    }
1757
1667
    
1758
 
    /* Raise privileges */
1759
 
    ret_errno = raise_privileges();
1760
 
    if(ret_errno != 0){
1761
 
      perror_plus("Failed to raise privileges");
1762
 
    }
 
1668
    /* Raise priviliges */
 
1669
    raise_privileges();
1763
1670
    
1764
1671
#ifdef __linux__
1765
 
    int ret_linux;
1766
 
    bool restore_loglevel = false;
1767
 
    if(ret_errno == 0){
1768
 
      /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1769
 
         messages about the network interface to mess up the prompt */
1770
 
      ret_linux = klogctl(8, NULL, 5);
1771
 
      if(ret_linux == -1){
1772
 
        perror_plus("klogctl");
1773
 
      } else {
1774
 
        restore_loglevel = true;
1775
 
      }
 
1672
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
 
1673
       messages about the network interface to mess up the prompt */
 
1674
    int ret_linux = klogctl(8, NULL, 5);
 
1675
    bool restore_loglevel = true;
 
1676
    if(ret_linux == -1){
 
1677
      restore_loglevel = false;
 
1678
      perror_plus("klogctl");
1776
1679
    }
1777
1680
#endif  /* __linux__ */
1778
 
    int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1779
 
    ioctl_errno = errno;
 
1681
    ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
 
1682
    ret_errno = errno;
1780
1683
#ifdef __linux__
1781
1684
    if(restore_loglevel){
1782
1685
      ret_linux = klogctl(7, NULL, 0);
1786
1689
    }
1787
1690
#endif  /* __linux__ */
1788
1691
    
1789
 
    /* If raise_privileges() succeeded above */
1790
 
    if(ret_errno == 0){
1791
 
      /* Lower privileges */
1792
 
      ret_errno = lower_privileges();
1793
 
      if(ret_errno != 0){
1794
 
        errno = ret_errno;
1795
 
        perror_plus("Failed to lower privileges");
1796
 
      }
1797
 
    }
 
1692
    /* Lower privileges */
 
1693
    lower_privileges();
1798
1694
    
1799
1695
    /* Close the socket */
1800
1696
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
1803
1699
    }
1804
1700
    
1805
1701
    if(ret_setflags == -1){
1806
 
      errno = ioctl_errno;
 
1702
      errno = ret_errno;
1807
1703
      perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1808
1704
      errno = old_errno;
1809
 
      return ioctl_errno;
 
1705
      return ret_errno;
1810
1706
    }
1811
1707
  } else if(debug){
1812
1708
    fprintf_plus(stderr, "Interface \"%s\" is already up; good\n",
1830
1726
  return 0;
1831
1727
}
1832
1728
 
1833
 
__attribute__((nonnull, warn_unused_result))
1834
1729
error_t take_down_interface(const char *const interface){
 
1730
  int sd = -1;
1835
1731
  error_t old_errno = errno;
 
1732
  error_t ret_errno = 0;
 
1733
  int ret, ret_setflags;
1836
1734
  struct ifreq network;
1837
1735
  unsigned int if_index = if_nametoindex(interface);
1838
1736
  if(if_index == 0){
1841
1739
    return ENXIO;
1842
1740
  }
1843
1741
  if(interface_is_up(interface)){
1844
 
    error_t ret_errno = 0, ioctl_errno = 0;
1845
1742
    if(not get_flags(interface, &network) and debug){
1846
1743
      ret_errno = errno;
1847
1744
      fprintf_plus(stderr, "Failed to get flags for interface "
1848
1745
                   "\"%s\"\n", interface);
1849
 
      errno = old_errno;
1850
1746
      return ret_errno;
1851
1747
    }
1852
1748
    network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1853
1749
    
1854
 
    int sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1855
 
    if(sd == -1){
 
1750
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
1751
    if(sd < 0){
1856
1752
      ret_errno = errno;
1857
1753
      perror_plus("socket");
1858
1754
      errno = old_errno;
1864
1760
                   interface);
1865
1761
    }
1866
1762
    
1867
 
    /* Raise privileges */
1868
 
    ret_errno = raise_privileges();
1869
 
    if(ret_errno != 0){
1870
 
      perror_plus("Failed to raise privileges");
1871
 
    }
1872
 
    
1873
 
    int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1874
 
    ioctl_errno = errno;
1875
 
    
1876
 
    /* If raise_privileges() succeeded above */
1877
 
    if(ret_errno == 0){
1878
 
      /* Lower privileges */
1879
 
      ret_errno = lower_privileges();
1880
 
      if(ret_errno != 0){
1881
 
        errno = ret_errno;
1882
 
        perror_plus("Failed to lower privileges");
1883
 
      }
1884
 
    }
 
1763
    /* Raise priviliges */
 
1764
    raise_privileges();
 
1765
    
 
1766
    ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
 
1767
    ret_errno = errno;
 
1768
    
 
1769
    /* Lower privileges */
 
1770
    lower_privileges();
1885
1771
    
1886
1772
    /* Close the socket */
1887
 
    int ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1773
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
1888
1774
    if(ret == -1){
1889
1775
      perror_plus("close");
1890
1776
    }
1891
1777
    
1892
1778
    if(ret_setflags == -1){
1893
 
      errno = ioctl_errno;
 
1779
      errno = ret_errno;
1894
1780
      perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
1895
1781
      errno = old_errno;
1896
 
      return ioctl_errno;
 
1782
      return ret_errno;
1897
1783
    }
1898
1784
  } else if(debug){
1899
1785
    fprintf_plus(stderr, "Interface \"%s\" is already down; odd\n",
1907
1793
int main(int argc, char *argv[]){
1908
1794
  mandos_context mc = { .server = NULL, .dh_bits = 1024,
1909
1795
                        .priority = "SECURE256:!CTYPE-X.509:"
1910
 
                        "+CTYPE-OPENPGP", .current_server = NULL,
 
1796
                        "+CTYPE-OPENPGP", .current_server = NULL, 
1911
1797
                        .interfaces = NULL, .interfaces_size = 0 };
1912
1798
  AvahiSServiceBrowser *sb = NULL;
1913
1799
  error_t ret_errno;
1917
1803
  int exitcode = EXIT_SUCCESS;
1918
1804
  char *interfaces_to_take_down = NULL;
1919
1805
  size_t interfaces_to_take_down_size = 0;
1920
 
  char run_tempdir[] = "/run/tmp/mandosXXXXXX";
1921
 
  char old_tempdir[] = "/tmp/mandosXXXXXX";
1922
 
  char *tempdir = NULL;
 
1806
  char tempdir[] = "/tmp/mandosXXXXXX";
 
1807
  bool tempdir_created = false;
1923
1808
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1924
1809
  const char *seckey = PATHDIR "/" SECKEY;
1925
1810
  const char *pubkey = PATHDIR "/" PUBKEY;
1926
1811
  char *interfaces_hooks = NULL;
 
1812
  size_t interfaces_hooks_size = 0;
1927
1813
  
1928
1814
  bool gnutls_initialized = false;
1929
1815
  bool gpgme_initialized = false;
2107
1993
    /* Work around Debian bug #633582:
2108
1994
       <http://bugs.debian.org/633582> */
2109
1995
    
2110
 
    /* Re-raise privileges */
2111
 
    ret_errno = raise_privileges();
2112
 
    if(ret_errno != 0){
2113
 
      errno = ret_errno;
2114
 
      perror_plus("Failed to raise privileges");
2115
 
    } else {
 
1996
    /* Re-raise priviliges */
 
1997
    if(raise_privileges() == 0){
2116
1998
      struct stat st;
2117
1999
      
2118
2000
      if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
2158
2040
      }
2159
2041
    
2160
2042
      /* Lower privileges */
2161
 
      ret_errno = lower_privileges();
2162
 
      if(ret_errno != 0){
2163
 
        errno = ret_errno;
2164
 
        perror_plus("Failed to lower privileges");
2165
 
      }
 
2043
      lower_privileges();
2166
2044
    }
2167
2045
  }
2168
2046
  
2192
2070
        goto end;
2193
2071
      }
2194
2072
      memcpy(interfaces_hooks, mc.interfaces, mc.interfaces_size);
2195
 
      argz_stringify(interfaces_hooks, mc.interfaces_size, (int)',');
2196
 
    }
2197
 
    run_network_hooks("start", interfaces_hooks != NULL ?
2198
 
                      interfaces_hooks : "", delay);
 
2073
      interfaces_hooks_size = mc.interfaces_size;
 
2074
      argz_stringify(interfaces_hooks, interfaces_hooks_size,
 
2075
                     (int)',');
 
2076
    }
 
2077
    if(not run_network_hooks("start", interfaces_hooks != NULL ?
 
2078
                             interfaces_hooks : "", delay)){
 
2079
      goto end;
 
2080
    }
2199
2081
  }
2200
2082
  
2201
2083
  if(not debug){
2289
2171
        ret_errno = argz_add(&mc.interfaces, &mc.interfaces_size,
2290
2172
                             direntries[i]->d_name);
2291
2173
        if(ret_errno != 0){
2292
 
          errno = ret_errno;
2293
2174
          perror_plus("argz_add");
2294
2175
          continue;
2295
2176
        }
2329
2210
        break;
2330
2211
      }
2331
2212
      bool interface_was_up = interface_is_up(interface);
2332
 
      errno = bring_up_interface(interface, delay);
 
2213
      ret = bring_up_interface(interface, delay);
2333
2214
      if(not interface_was_up){
2334
 
        if(errno != 0){
 
2215
        if(ret != 0){
 
2216
          errno = ret;
2335
2217
          perror_plus("Failed to bring up interface");
2336
2218
        } else {
2337
 
          errno = argz_add(&interfaces_to_take_down,
2338
 
                           &interfaces_to_take_down_size,
2339
 
                           interface);
2340
 
          if(errno != 0){
2341
 
            perror_plus("argz_add");
2342
 
          }
 
2219
          ret_errno = argz_add(&interfaces_to_take_down,
 
2220
                               &interfaces_to_take_down_size,
 
2221
                               interface);
2343
2222
        }
2344
2223
      }
2345
2224
    }
2374
2253
    goto end;
2375
2254
  }
2376
2255
  
2377
 
  /* Try /run/tmp before /tmp */
2378
 
  tempdir = mkdtemp(run_tempdir);
2379
 
  if(tempdir == NULL and errno == ENOENT){
2380
 
      if(debug){
2381
 
        fprintf_plus(stderr, "Tempdir %s did not work, trying %s\n",
2382
 
                     run_tempdir, old_tempdir);
2383
 
      }
2384
 
      tempdir = mkdtemp(old_tempdir);
2385
 
  }
2386
 
  if(tempdir == NULL){
 
2256
  if(mkdtemp(tempdir) == NULL){
2387
2257
    perror_plus("mkdtemp");
2388
2258
    goto end;
2389
2259
  }
 
2260
  tempdir_created = true;
2390
2261
  
2391
2262
  if(quit_now){
2392
2263
    goto end;
2467
2338
      sleep((unsigned int)retry_interval);
2468
2339
    }
2469
2340
    
2470
 
    if(not quit_now){
 
2341
    if (not quit_now){
2471
2342
      exitcode = EXIT_SUCCESS;
2472
2343
    }
2473
2344
    
2528
2399
  if(debug){
2529
2400
    fprintf_plus(stderr, "Starting Avahi loop search\n");
2530
2401
  }
2531
 
  
 
2402
 
2532
2403
  ret = avahi_loop_with_timeout(simple_poll,
2533
2404
                                (int)(retry_interval * 1000), &mc);
2534
2405
  if(debug){
2575
2446
    }
2576
2447
  }
2577
2448
  
2578
 
  /* Re-raise privileges */
 
2449
  /* Re-raise priviliges */
2579
2450
  {
2580
 
    ret_errno = raise_privileges();
2581
 
    if(ret_errno != 0){
2582
 
      perror_plus("Failed to raise privileges");
2583
 
    } else {
2584
 
      
2585
 
      /* Run network hooks */
2586
 
      run_network_hooks("stop", interfaces_hooks != NULL ?
2587
 
                        interfaces_hooks : "", delay);
2588
 
      
2589
 
      /* Take down the network interfaces which were brought up */
2590
 
      {
2591
 
        char *interface = NULL;
2592
 
        while((interface=argz_next(interfaces_to_take_down,
2593
 
                                   interfaces_to_take_down_size,
2594
 
                                   interface))){
2595
 
          ret_errno = take_down_interface(interface);
2596
 
          if(ret_errno != 0){
2597
 
            errno = ret_errno;
2598
 
            perror_plus("Failed to take down interface");
2599
 
          }
2600
 
        }
2601
 
        if(debug and (interfaces_to_take_down == NULL)){
2602
 
          fprintf_plus(stderr, "No interfaces needed to be taken"
2603
 
                       " down\n");
2604
 
        }
2605
 
      }
2606
 
    }
2607
 
    
2608
 
    ret_errno = lower_privileges_permanently();
2609
 
    if(ret_errno != 0){
2610
 
      perror_plus("Failed to lower privileges permanently");
2611
 
    }
 
2451
    raise_privileges();
 
2452
    
 
2453
    /* Run network hooks */
 
2454
    run_network_hooks("stop", interfaces_hooks != NULL ?
 
2455
                      interfaces_hooks : "", delay);
 
2456
    
 
2457
    /* Take down the network interfaces which were brought up */
 
2458
    {
 
2459
      char *interface = NULL;
 
2460
      while((interface=argz_next(interfaces_to_take_down,
 
2461
                                 interfaces_to_take_down_size,
 
2462
                                 interface))){
 
2463
        ret_errno = take_down_interface(interface);
 
2464
        if(ret_errno != 0){
 
2465
          errno = ret_errno;
 
2466
          perror_plus("Failed to take down interface");
 
2467
        }
 
2468
      }
 
2469
      if(debug and (interfaces_to_take_down == NULL)){
 
2470
        fprintf_plus(stderr, "No interfaces needed to be taken"
 
2471
                     " down\n");
 
2472
      }
 
2473
    }
 
2474
    
 
2475
    lower_privileges_permanently();
2612
2476
  }
2613
2477
  
2614
2478
  free(interfaces_to_take_down);
2615
2479
  free(interfaces_hooks);
2616
2480
  
2617
2481
  /* Removes the GPGME temp directory and all files inside */
2618
 
  if(tempdir != NULL){
 
2482
  if(tempdir_created){
2619
2483
    struct dirent **direntries = NULL;
2620
2484
    struct dirent *direntry = NULL;
2621
2485
    int numentries = scandir(tempdir, &direntries, notdotentries,
2622
2486
                             alphasort);
2623
 
    if(numentries > 0){
 
2487
    if (numentries > 0){
2624
2488
      for(int i = 0; i < numentries; i++){
2625
2489
        direntry = direntries[i];
2626
2490
        char *fullname = NULL;
2638
2502
        free(fullname);
2639
2503
      }
2640
2504
    }
2641
 
    
 
2505
 
2642
2506
    /* need to clean even if 0 because man page doesn't specify */
2643
2507
    free(direntries);
2644
 
    if(numentries == -1){
 
2508
    if (numentries == -1){
2645
2509
      perror_plus("scandir");
2646
2510
    }
2647
2511
    ret = rmdir(tempdir);