/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

  • Committer: Teddy Hogeborn
  • Date: 2014-01-06 15:56:54 UTC
  • Revision ID: teddy@recompile.se-20140106155654-urx23ytuvy0nxzwp
Update init script to modern standards.

* init.d-mandos (status): Moved to standard location.  Re-added use of
                          -p switch compared to skeleton file.
  (*): Update message to include "status".

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){
1360
1337
    }
1361
1338
    return 0;
1362
1339
  }
1363
 
  free(fullname);
1364
1340
  if(not (S_ISREG(st.st_mode))){
1365
1341
    /* Not a regular file */
1366
1342
    if(debug){
1384
1360
  return 1;
1385
1361
}
1386
1362
 
1387
 
__attribute__((nonnull, warn_unused_result))
1388
1363
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval,
1389
1364
                            mandos_context *mc){
1390
1365
  int ret;
1394
1369
  
1395
1370
  while(true){
1396
1371
    if(mc->current_server == NULL){
1397
 
      if(debug){
 
1372
      if (debug){
1398
1373
        fprintf_plus(stderr, "Wait until first server is found."
1399
1374
                     " No timeout!\n");
1400
1375
      }
1401
1376
      ret = avahi_simple_poll_iterate(s, -1);
1402
1377
    } else {
1403
 
      if(debug){
 
1378
      if (debug){
1404
1379
        fprintf_plus(stderr, "Check current_server if we should run"
1405
1380
                     " it, or wait\n");
1406
1381
      }
1423
1398
                     - ((intmax_t)waited_time.tv_sec * 1000))
1424
1399
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
1425
1400
      
1426
 
      if(debug){
 
1401
      if (debug){
1427
1402
        fprintf_plus(stderr, "Blocking for %" PRIdMAX " ms\n",
1428
1403
                     block_time);
1429
1404
      }
1451
1426
      ret = avahi_simple_poll_iterate(s, (int)block_time);
1452
1427
    }
1453
1428
    if(ret != 0){
1454
 
      if(ret > 0 or errno != EINTR){
 
1429
      if (ret > 0 or errno != EINTR){
1455
1430
        return (ret != 1) ? ret : 0;
1456
1431
      }
1457
1432
    }
1459
1434
}
1460
1435
 
1461
1436
/* Set effective uid to 0, return errno */
1462
 
__attribute__((warn_unused_result))
1463
1437
error_t raise_privileges(void){
1464
1438
  error_t old_errno = errno;
1465
1439
  error_t ret_errno = 0;
1472
1446
}
1473
1447
 
1474
1448
/* Set effective and real user ID to 0.  Return errno. */
1475
 
__attribute__((warn_unused_result))
1476
1449
error_t raise_privileges_permanently(void){
1477
1450
  error_t old_errno = errno;
1478
1451
  error_t ret_errno = raise_privileges();
1489
1462
}
1490
1463
 
1491
1464
/* Set effective user ID to unprivileged saved user ID */
1492
 
__attribute__((warn_unused_result))
1493
1465
error_t lower_privileges(void){
1494
1466
  error_t old_errno = errno;
1495
1467
  error_t ret_errno = 0;
1502
1474
}
1503
1475
 
1504
1476
/* Lower privileges permanently */
1505
 
__attribute__((warn_unused_result))
1506
1477
error_t lower_privileges_permanently(void){
1507
1478
  error_t old_errno = errno;
1508
1479
  error_t ret_errno = 0;
1514
1485
  return ret_errno;
1515
1486
}
1516
1487
 
1517
 
#ifndef O_CLOEXEC
1518
 
/*
1519
 
 * Based on the example in the GNU LibC manual chapter 13.13 "File
1520
 
 * Descriptor Flags".
1521
 
 | [[info:libc:Descriptor%20Flags][File Descriptor Flags]] |
1522
 
 */
1523
 
__attribute__((warn_unused_result))
1524
 
static int set_cloexec_flag(int fd){
1525
 
  int ret = (int)TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD, 0));
1526
 
  /* If reading the flags failed, return error indication now. */
1527
 
  if(ret < 0){
1528
 
    return ret;
1529
 
  }
1530
 
  /* Store modified flag word in the descriptor. */
1531
 
  return (int)TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD,
1532
 
                                       ret | FD_CLOEXEC));
1533
 
}
1534
 
#endif  /* not O_CLOEXEC */
1535
 
 
1536
 
__attribute__((nonnull))
1537
 
void run_network_hooks(const char *mode, const char *interface,
 
1488
bool run_network_hooks(const char *mode, const char *interface,
1538
1489
                       const float delay){
1539
1490
  struct dirent **direntries;
1540
 
  if(hookdir_fd == -1){
1541
 
    hookdir_fd = open(hookdir, O_RDONLY |
1542
 
#ifdef O_CLOEXEC
1543
 
                      O_CLOEXEC
1544
 
#else  /* not O_CLOEXEC */
1545
 
                      0
1546
 
#endif  /* not O_CLOEXEC */
1547
 
                      );
1548
 
    if(hookdir_fd == -1){
1549
 
      if(errno == ENOENT){
1550
 
        if(debug){
1551
 
          fprintf_plus(stderr, "Network hook directory \"%s\" not"
1552
 
                       " found\n", hookdir);
1553
 
        }
1554
 
      } else {
1555
 
        perror_plus("open");
1556
 
      }
1557
 
      return;
1558
 
    }
1559
 
#ifndef O_CLOEXEC
1560
 
    if(set_cloexec_flag(hookdir_fd) < 0){
1561
 
      perror_plus("set_cloexec_flag");
1562
 
      if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1563
 
        perror_plus("close");
1564
 
      } else {
1565
 
        hookdir_fd = -1;
1566
 
      }
1567
 
      return;
1568
 
    }
1569
 
#endif  /* not O_CLOEXEC */
1570
 
  }
1571
 
#ifdef __GLIBC__
1572
 
#if __GLIBC_PREREQ(2, 15)
1573
 
  int numhooks = scandirat(hookdir_fd, ".", &direntries,
1574
 
                           runnable_hook, alphasort);
1575
 
#else  /* not __GLIBC_PREREQ(2, 15) */
1576
 
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1577
 
                         alphasort);
1578
 
#endif  /* not __GLIBC_PREREQ(2, 15) */
1579
 
#else   /* not __GLIBC__ */
1580
 
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1581
 
                         alphasort);
1582
 
#endif  /* not __GLIBC__ */
 
1491
  struct dirent *direntry;
 
1492
  int ret;
 
1493
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
1494
                         alphasort);
1583
1495
  if(numhooks == -1){
1584
 
    perror_plus("scandir");
1585
 
    return;
1586
 
  }
1587
 
  struct dirent *direntry;
1588
 
  int ret;
1589
 
  int devnull = open("/dev/null", O_RDONLY);
1590
 
  for(int i = 0; i < numhooks; i++){
1591
 
    direntry = direntries[i];
1592
 
    char *fullname = NULL;
1593
 
    ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1594
 
    if(ret < 0){
1595
 
      perror_plus("asprintf");
1596
 
      continue;
1597
 
    }
1598
 
    if(debug){
1599
 
      fprintf_plus(stderr, "Running network hook \"%s\"\n",
1600
 
                   direntry->d_name);
1601
 
    }
1602
 
    pid_t hook_pid = fork();
1603
 
    if(hook_pid == 0){
1604
 
      /* Child */
1605
 
      /* Raise privileges */
1606
 
      if(raise_privileges_permanently() != 0){
1607
 
        perror_plus("Failed to raise privileges");
1608
 
        _exit(EX_NOPERM);
1609
 
      }
1610
 
      /* Set group */
1611
 
      errno = 0;
1612
 
      ret = setgid(0);
1613
 
      if(ret == -1){
1614
 
        perror_plus("setgid");
1615
 
        _exit(EX_NOPERM);
1616
 
      }
1617
 
      /* Reset supplementary groups */
1618
 
      errno = 0;
1619
 
      ret = setgroups(0, NULL);
1620
 
      if(ret == -1){
1621
 
        perror_plus("setgroups");
1622
 
        _exit(EX_NOPERM);
1623
 
      }
1624
 
      ret = dup2(devnull, STDIN_FILENO);
1625
 
      if(ret == -1){
1626
 
        perror_plus("dup2(devnull, STDIN_FILENO)");
1627
 
        _exit(EX_OSERR);
1628
 
      }
1629
 
      ret = close(devnull);
1630
 
      if(ret == -1){
1631
 
        perror_plus("close");
1632
 
        _exit(EX_OSERR);
1633
 
      }
1634
 
      ret = dup2(STDERR_FILENO, STDOUT_FILENO);
1635
 
      if(ret == -1){
1636
 
        perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
1637
 
        _exit(EX_OSERR);
1638
 
      }
1639
 
      ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1640
 
      if(ret == -1){
1641
 
        perror_plus("setenv");
1642
 
        _exit(EX_OSERR);
1643
 
      }
1644
 
      ret = setenv("DEVICE", interface, 1);
1645
 
      if(ret == -1){
1646
 
        perror_plus("setenv");
1647
 
        _exit(EX_OSERR);
1648
 
      }
1649
 
      ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1650
 
      if(ret == -1){
1651
 
        perror_plus("setenv");
1652
 
        _exit(EX_OSERR);
1653
 
      }
1654
 
      ret = setenv("MODE", mode, 1);
1655
 
      if(ret == -1){
1656
 
        perror_plus("setenv");
1657
 
        _exit(EX_OSERR);
1658
 
      }
1659
 
      char *delaystring;
1660
 
      ret = asprintf(&delaystring, "%f", (double)delay);
1661
 
      if(ret == -1){
 
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");
 
1503
    }
 
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){
1662
1511
        perror_plus("asprintf");
1663
 
        _exit(EX_OSERR);
1664
 
      }
1665
 
      ret = setenv("DELAY", delaystring, 1);
1666
 
      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
        }
1667
1570
        free(delaystring);
1668
 
        perror_plus("setenv");
1669
 
        _exit(EX_OSERR);
1670
 
      }
1671
 
      free(delaystring);
1672
 
      if(connect_to != NULL){
1673
 
        ret = setenv("CONNECT", connect_to, 1);
1674
 
        if(ret == -1){
1675
 
          perror_plus("setenv");
1676
 
          _exit(EX_OSERR);
1677
 
        }
1678
 
      }
1679
 
      if(execl(fullname, direntry->d_name, mode, NULL) == -1){
1680
 
        perror_plus("execl");
1681
 
        _exit(EXIT_FAILURE);
1682
 
      }
1683
 
    } else {
1684
 
      int status;
1685
 
      if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1686
 
        perror_plus("waitpid");
1687
 
        free(fullname);
1688
 
        continue;
1689
 
      }
1690
 
      if(WIFEXITED(status)){
1691
 
        if(WEXITSTATUS(status) != 0){
1692
 
          fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1693
 
                       " with status %d\n", direntry->d_name,
1694
 
                       WEXITSTATUS(status));
1695
 
          free(fullname);
1696
 
          continue;
1697
 
        }
1698
 
      } else if(WIFSIGNALED(status)){
1699
 
        fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1700
 
                     " signal %d\n", direntry->d_name,
1701
 
                     WTERMSIG(status));
1702
 
        free(fullname);
1703
 
        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
        }
1704
1582
      } else {
1705
 
        fprintf_plus(stderr, "Warning: network hook \"%s\""
1706
 
                     " crashed\n", direntry->d_name);
1707
 
        free(fullname);
1708
 
        continue;
1709
 
      }
1710
 
    }
1711
 
    free(fullname);
1712
 
    if(debug){
1713
 
      fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1714
 
                   direntry->d_name);
1715
 
    }
1716
 
  }
1717
 
  if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1718
 
    perror_plus("close");
1719
 
  } else {
1720
 
    hookdir_fd = -1;
1721
 
  }
1722
 
  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;
1723
1619
}
1724
1620
 
1725
 
__attribute__((nonnull, warn_unused_result))
1726
1621
error_t bring_up_interface(const char *const interface,
1727
1622
                           const float delay){
 
1623
  int sd = -1;
1728
1624
  error_t old_errno = errno;
1729
 
  int ret;
 
1625
  error_t ret_errno = 0;
 
1626
  int ret, ret_setflags;
1730
1627
  struct ifreq network;
1731
1628
  unsigned int if_index = if_nametoindex(interface);
1732
1629
  if(if_index == 0){
1741
1638
  }
1742
1639
  
1743
1640
  if(not interface_is_up(interface)){
1744
 
    error_t ret_errno = 0, ioctl_errno = 0;
1745
 
    if(not get_flags(interface, &network)){
 
1641
    if(not get_flags(interface, &network) and debug){
1746
1642
      ret_errno = errno;
1747
1643
      fprintf_plus(stderr, "Failed to get flags for interface "
1748
1644
                   "\"%s\"\n", interface);
1749
 
      errno = old_errno;
1750
1645
      return ret_errno;
1751
1646
    }
1752
 
    network.ifr_flags |= IFF_UP; /* set flag */
 
1647
    network.ifr_flags |= IFF_UP;
1753
1648
    
1754
 
    int sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1755
 
    if(sd == -1){
 
1649
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
1650
    if(sd < 0){
1756
1651
      ret_errno = errno;
1757
1652
      perror_plus("socket");
1758
1653
      errno = old_errno;
1759
1654
      return ret_errno;
1760
1655
    }
1761
 
    
 
1656
  
1762
1657
    if(quit_now){
1763
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1764
 
      if(ret == -1){
1765
 
        perror_plus("close");
1766
 
      }
 
1658
      close(sd);
1767
1659
      errno = old_errno;
1768
1660
      return EINTR;
1769
1661
    }
1773
1665
                   interface);
1774
1666
    }
1775
1667
    
1776
 
    /* Raise privileges */
1777
 
    ret_errno = raise_privileges();
1778
 
    if(ret_errno != 0){
1779
 
      perror_plus("Failed to raise privileges");
1780
 
    }
 
1668
    /* Raise priviliges */
 
1669
    raise_privileges();
1781
1670
    
1782
1671
#ifdef __linux__
1783
 
    int ret_linux;
1784
 
    bool restore_loglevel = false;
1785
 
    if(ret_errno == 0){
1786
 
      /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1787
 
         messages about the network interface to mess up the prompt */
1788
 
      ret_linux = klogctl(8, NULL, 5);
1789
 
      if(ret_linux == -1){
1790
 
        perror_plus("klogctl");
1791
 
      } else {
1792
 
        restore_loglevel = true;
1793
 
      }
 
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");
1794
1679
    }
1795
1680
#endif  /* __linux__ */
1796
 
    int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1797
 
    ioctl_errno = errno;
 
1681
    ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
 
1682
    ret_errno = errno;
1798
1683
#ifdef __linux__
1799
1684
    if(restore_loglevel){
1800
1685
      ret_linux = klogctl(7, NULL, 0);
1804
1689
    }
1805
1690
#endif  /* __linux__ */
1806
1691
    
1807
 
    /* If raise_privileges() succeeded above */
1808
 
    if(ret_errno == 0){
1809
 
      /* Lower privileges */
1810
 
      ret_errno = lower_privileges();
1811
 
      if(ret_errno != 0){
1812
 
        errno = ret_errno;
1813
 
        perror_plus("Failed to lower privileges");
1814
 
      }
1815
 
    }
 
1692
    /* Lower privileges */
 
1693
    lower_privileges();
1816
1694
    
1817
1695
    /* Close the socket */
1818
1696
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
1821
1699
    }
1822
1700
    
1823
1701
    if(ret_setflags == -1){
1824
 
      errno = ioctl_errno;
 
1702
      errno = ret_errno;
1825
1703
      perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1826
1704
      errno = old_errno;
1827
 
      return ioctl_errno;
 
1705
      return ret_errno;
1828
1706
    }
1829
1707
  } else if(debug){
1830
1708
    fprintf_plus(stderr, "Interface \"%s\" is already up; good\n",
1848
1726
  return 0;
1849
1727
}
1850
1728
 
1851
 
__attribute__((nonnull, warn_unused_result))
1852
1729
error_t take_down_interface(const char *const interface){
 
1730
  int sd = -1;
1853
1731
  error_t old_errno = errno;
 
1732
  error_t ret_errno = 0;
 
1733
  int ret, ret_setflags;
1854
1734
  struct ifreq network;
1855
1735
  unsigned int if_index = if_nametoindex(interface);
1856
1736
  if(if_index == 0){
1859
1739
    return ENXIO;
1860
1740
  }
1861
1741
  if(interface_is_up(interface)){
1862
 
    error_t ret_errno = 0, ioctl_errno = 0;
1863
1742
    if(not get_flags(interface, &network) and debug){
1864
1743
      ret_errno = errno;
1865
1744
      fprintf_plus(stderr, "Failed to get flags for interface "
1866
1745
                   "\"%s\"\n", interface);
1867
 
      errno = old_errno;
1868
1746
      return ret_errno;
1869
1747
    }
1870
1748
    network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1871
1749
    
1872
 
    int sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1873
 
    if(sd == -1){
 
1750
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
1751
    if(sd < 0){
1874
1752
      ret_errno = errno;
1875
1753
      perror_plus("socket");
1876
1754
      errno = old_errno;
1882
1760
                   interface);
1883
1761
    }
1884
1762
    
1885
 
    /* Raise privileges */
1886
 
    ret_errno = raise_privileges();
1887
 
    if(ret_errno != 0){
1888
 
      perror_plus("Failed to raise privileges");
1889
 
    }
1890
 
    
1891
 
    int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1892
 
    ioctl_errno = errno;
1893
 
    
1894
 
    /* If raise_privileges() succeeded above */
1895
 
    if(ret_errno == 0){
1896
 
      /* Lower privileges */
1897
 
      ret_errno = lower_privileges();
1898
 
      if(ret_errno != 0){
1899
 
        errno = ret_errno;
1900
 
        perror_plus("Failed to lower privileges");
1901
 
      }
1902
 
    }
 
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();
1903
1771
    
1904
1772
    /* Close the socket */
1905
 
    int ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1773
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
1906
1774
    if(ret == -1){
1907
1775
      perror_plus("close");
1908
1776
    }
1909
1777
    
1910
1778
    if(ret_setflags == -1){
1911
 
      errno = ioctl_errno;
 
1779
      errno = ret_errno;
1912
1780
      perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
1913
1781
      errno = old_errno;
1914
 
      return ioctl_errno;
 
1782
      return ret_errno;
1915
1783
    }
1916
1784
  } else if(debug){
1917
1785
    fprintf_plus(stderr, "Interface \"%s\" is already down; odd\n",
1925
1793
int main(int argc, char *argv[]){
1926
1794
  mandos_context mc = { .server = NULL, .dh_bits = 1024,
1927
1795
                        .priority = "SECURE256:!CTYPE-X.509:"
1928
 
                        "+CTYPE-OPENPGP", .current_server = NULL,
 
1796
                        "+CTYPE-OPENPGP", .current_server = NULL, 
1929
1797
                        .interfaces = NULL, .interfaces_size = 0 };
1930
1798
  AvahiSServiceBrowser *sb = NULL;
1931
1799
  error_t ret_errno;
1935
1803
  int exitcode = EXIT_SUCCESS;
1936
1804
  char *interfaces_to_take_down = NULL;
1937
1805
  size_t interfaces_to_take_down_size = 0;
1938
 
  char run_tempdir[] = "/run/tmp/mandosXXXXXX";
1939
 
  char old_tempdir[] = "/tmp/mandosXXXXXX";
1940
 
  char *tempdir = NULL;
 
1806
  char tempdir[] = "/tmp/mandosXXXXXX";
 
1807
  bool tempdir_created = false;
1941
1808
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1942
1809
  const char *seckey = PATHDIR "/" SECKEY;
1943
1810
  const char *pubkey = PATHDIR "/" PUBKEY;
1944
1811
  char *interfaces_hooks = NULL;
 
1812
  size_t interfaces_hooks_size = 0;
1945
1813
  
1946
1814
  bool gnutls_initialized = false;
1947
1815
  bool gpgme_initialized = false;
2125
1993
    /* Work around Debian bug #633582:
2126
1994
       <http://bugs.debian.org/633582> */
2127
1995
    
2128
 
    /* Re-raise privileges */
2129
 
    ret_errno = raise_privileges();
2130
 
    if(ret_errno != 0){
2131
 
      errno = ret_errno;
2132
 
      perror_plus("Failed to raise privileges");
2133
 
    } else {
 
1996
    /* Re-raise priviliges */
 
1997
    if(raise_privileges() == 0){
2134
1998
      struct stat st;
2135
1999
      
2136
2000
      if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
2176
2040
      }
2177
2041
    
2178
2042
      /* Lower privileges */
2179
 
      ret_errno = lower_privileges();
2180
 
      if(ret_errno != 0){
2181
 
        errno = ret_errno;
2182
 
        perror_plus("Failed to lower privileges");
2183
 
      }
 
2043
      lower_privileges();
2184
2044
    }
2185
2045
  }
2186
2046
  
2210
2070
        goto end;
2211
2071
      }
2212
2072
      memcpy(interfaces_hooks, mc.interfaces, mc.interfaces_size);
2213
 
      argz_stringify(interfaces_hooks, mc.interfaces_size, (int)',');
2214
 
    }
2215
 
    run_network_hooks("start", interfaces_hooks != NULL ?
2216
 
                      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
    }
2217
2081
  }
2218
2082
  
2219
2083
  if(not debug){
2307
2171
        ret_errno = argz_add(&mc.interfaces, &mc.interfaces_size,
2308
2172
                             direntries[i]->d_name);
2309
2173
        if(ret_errno != 0){
2310
 
          errno = ret_errno;
2311
2174
          perror_plus("argz_add");
2312
2175
          continue;
2313
2176
        }
2347
2210
        break;
2348
2211
      }
2349
2212
      bool interface_was_up = interface_is_up(interface);
2350
 
      errno = bring_up_interface(interface, delay);
 
2213
      ret = bring_up_interface(interface, delay);
2351
2214
      if(not interface_was_up){
2352
 
        if(errno != 0){
 
2215
        if(ret != 0){
 
2216
          errno = ret;
2353
2217
          perror_plus("Failed to bring up interface");
2354
2218
        } else {
2355
 
          errno = argz_add(&interfaces_to_take_down,
2356
 
                           &interfaces_to_take_down_size,
2357
 
                           interface);
2358
 
          if(errno != 0){
2359
 
            perror_plus("argz_add");
2360
 
          }
 
2219
          ret_errno = argz_add(&interfaces_to_take_down,
 
2220
                               &interfaces_to_take_down_size,
 
2221
                               interface);
2361
2222
        }
2362
2223
      }
2363
2224
    }
2392
2253
    goto end;
2393
2254
  }
2394
2255
  
2395
 
  /* Try /run/tmp before /tmp */
2396
 
  tempdir = mkdtemp(run_tempdir);
2397
 
  if(tempdir == NULL and errno == ENOENT){
2398
 
      if(debug){
2399
 
        fprintf_plus(stderr, "Tempdir %s did not work, trying %s\n",
2400
 
                     run_tempdir, old_tempdir);
2401
 
      }
2402
 
      tempdir = mkdtemp(old_tempdir);
2403
 
  }
2404
 
  if(tempdir == NULL){
 
2256
  if(mkdtemp(tempdir) == NULL){
2405
2257
    perror_plus("mkdtemp");
2406
2258
    goto end;
2407
2259
  }
 
2260
  tempdir_created = true;
2408
2261
  
2409
2262
  if(quit_now){
2410
2263
    goto end;
2485
2338
      sleep((unsigned int)retry_interval);
2486
2339
    }
2487
2340
    
2488
 
    if(not quit_now){
 
2341
    if (not quit_now){
2489
2342
      exitcode = EXIT_SUCCESS;
2490
2343
    }
2491
2344
    
2546
2399
  if(debug){
2547
2400
    fprintf_plus(stderr, "Starting Avahi loop search\n");
2548
2401
  }
2549
 
  
 
2402
 
2550
2403
  ret = avahi_loop_with_timeout(simple_poll,
2551
2404
                                (int)(retry_interval * 1000), &mc);
2552
2405
  if(debug){
2593
2446
    }
2594
2447
  }
2595
2448
  
2596
 
  /* Re-raise privileges */
 
2449
  /* Re-raise priviliges */
2597
2450
  {
2598
 
    ret_errno = raise_privileges();
2599
 
    if(ret_errno != 0){
2600
 
      perror_plus("Failed to raise privileges");
2601
 
    } else {
2602
 
      
2603
 
      /* Run network hooks */
2604
 
      run_network_hooks("stop", interfaces_hooks != NULL ?
2605
 
                        interfaces_hooks : "", delay);
2606
 
      
2607
 
      /* Take down the network interfaces which were brought up */
2608
 
      {
2609
 
        char *interface = NULL;
2610
 
        while((interface=argz_next(interfaces_to_take_down,
2611
 
                                   interfaces_to_take_down_size,
2612
 
                                   interface))){
2613
 
          ret_errno = take_down_interface(interface);
2614
 
          if(ret_errno != 0){
2615
 
            errno = ret_errno;
2616
 
            perror_plus("Failed to take down interface");
2617
 
          }
2618
 
        }
2619
 
        if(debug and (interfaces_to_take_down == NULL)){
2620
 
          fprintf_plus(stderr, "No interfaces needed to be taken"
2621
 
                       " down\n");
2622
 
        }
2623
 
      }
2624
 
    }
2625
 
    
2626
 
    ret_errno = lower_privileges_permanently();
2627
 
    if(ret_errno != 0){
2628
 
      perror_plus("Failed to lower privileges permanently");
2629
 
    }
 
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();
2630
2476
  }
2631
2477
  
2632
2478
  free(interfaces_to_take_down);
2633
2479
  free(interfaces_hooks);
2634
2480
  
2635
2481
  /* Removes the GPGME temp directory and all files inside */
2636
 
  if(tempdir != NULL){
 
2482
  if(tempdir_created){
2637
2483
    struct dirent **direntries = NULL;
2638
2484
    struct dirent *direntry = NULL;
2639
2485
    int numentries = scandir(tempdir, &direntries, notdotentries,
2640
2486
                             alphasort);
2641
 
    if(numentries > 0){
 
2487
    if (numentries > 0){
2642
2488
      for(int i = 0; i < numentries; i++){
2643
2489
        direntry = direntries[i];
2644
2490
        char *fullname = NULL;
2656
2502
        free(fullname);
2657
2503
      }
2658
2504
    }
2659
 
    
 
2505
 
2660
2506
    /* need to clean even if 0 because man page doesn't specify */
2661
2507
    free(direntries);
2662
 
    if(numentries == -1){
 
2508
    if (numentries == -1){
2663
2509
      perror_plus("scandir");
2664
2510
    }
2665
2511
    ret = rmdir(tempdir);