/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: 2012-06-17 14:55:31 UTC
  • Revision ID: teddy@recompile.se-20120617145531-o24z982oerm6xb6s
* mandos: New "--foreground" option.

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-2012 Teddy Hogeborn
 
13
 * Copyright © 2008-2012 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);
191
187
  
192
188
  TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ",
193
189
                             program_invocation_short_name));
194
 
  return (int)TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
 
190
  return TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
195
191
}
196
192
 
197
193
/*
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
    if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
 
740
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
 
741
                                -Wunreachable-code*/
758
742
      if(if_index == AVAHI_IF_UNSPEC){
759
743
        fprintf_plus(stderr, "An IPv6 link-local address is"
760
744
                     " incomplete without a network interface\n");
762
746
        goto mandos_end;
763
747
      }
764
748
      /* Set the network interface number as scope */
765
 
      ((struct sockaddr_in6 *)&to)->sin6_scope_id = (uint32_t)if_index;
 
749
      to.in6.sin6_scope_id = (uint32_t)if_index;
766
750
    }
767
751
  } else {
768
 
    ((struct sockaddr_in *)&to)->sin_port = htons(port);
 
752
    to.in.sin_port = htons(port); /* Spurious warnings from
 
753
                                     -Wconversion and
 
754
                                     -Wunreachable-code */
769
755
  }
770
756
  
771
757
  if(quit_now){
788
774
    }
789
775
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
790
776
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
 
777
    const char *pcret;
791
778
    if(af == AF_INET6){
792
 
      ret = getnameinfo((struct sockaddr *)&to,
793
 
                        sizeof(struct sockaddr_in6),
794
 
                        addrstr, sizeof(addrstr), NULL, 0,
795
 
                        NI_NUMERICHOST);
 
779
      pcret = inet_ntop(af, &(to.in6.sin6_addr), addrstr,
 
780
                        sizeof(addrstr));
796
781
    } else {
797
 
      ret = getnameinfo((struct sockaddr *)&to,
798
 
                        sizeof(struct sockaddr_in),
799
 
                        addrstr, sizeof(addrstr), NULL, 0,
800
 
                        NI_NUMERICHOST);
 
782
      pcret = inet_ntop(af, &(to.in.sin_addr), addrstr,
 
783
                        sizeof(addrstr));
801
784
    }
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);
 
785
    if(pcret == NULL){
 
786
      perror_plus("inet_ntop");
 
787
    } else {
 
788
      if(strcmp(addrstr, ip) != 0){
 
789
        fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
 
790
      }
808
791
    }
809
792
  }
810
793
  
814
797
  }
815
798
  
816
799
  if(af == AF_INET6){
817
 
    ret = connect(tcp_sd, (struct sockaddr *)&to,
818
 
                  sizeof(struct sockaddr_in6));
 
800
    ret = connect(tcp_sd, &to.in6, sizeof(to));
819
801
  } else {
820
 
    ret = connect(tcp_sd, (struct sockaddr *)&to, /* IPv4 */
821
 
                  sizeof(struct sockaddr_in));
 
802
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
822
803
  }
823
804
  if(ret < 0){
824
 
    if((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
 
805
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
825
806
      int e = errno;
826
807
      perror_plus("connect");
827
808
      errno = e;
1042
1023
  return retval;
1043
1024
}
1044
1025
 
1045
 
__attribute__((nonnull))
1046
1026
static void resolve_callback(AvahiSServiceResolver *r,
1047
1027
                             AvahiIfIndex interface,
1048
1028
                             AvahiProtocol proto,
1056
1036
                             AVAHI_GCC_UNUSED AvahiStringList *txt,
1057
1037
                             AVAHI_GCC_UNUSED AvahiLookupResultFlags
1058
1038
                             flags,
1059
 
                             void *mc){
 
1039
                             void* mc){
1060
1040
  if(r == NULL){
1061
1041
    return;
1062
1042
  }
1115
1095
                            const char *domain,
1116
1096
                            AVAHI_GCC_UNUSED AvahiLookupResultFlags
1117
1097
                            flags,
1118
 
                            void *mc){
 
1098
                            void* mc){
1119
1099
  if(b == NULL){
1120
1100
    return;
1121
1101
  }
1181
1161
  errno = old_errno;
1182
1162
}
1183
1163
 
1184
 
__attribute__((nonnull, warn_unused_result))
1185
1164
bool get_flags(const char *ifname, struct ifreq *ifr){
1186
1165
  int ret;
1187
1166
  error_t ret_errno;
1206
1185
  return true;
1207
1186
}
1208
1187
 
1209
 
__attribute__((nonnull, warn_unused_result))
1210
1188
bool good_flags(const char *ifname, const struct ifreq *ifr){
1211
1189
  
1212
1190
  /* Reject the loopback device */
1254
1232
 * corresponds to an acceptable network device.
1255
1233
 * (This function is passed to scandir(3) as a filter function.)
1256
1234
 */
1257
 
__attribute__((nonnull, warn_unused_result))
1258
1235
int good_interface(const struct dirent *if_entry){
1259
1236
  if(if_entry->d_name[0] == '.'){
1260
1237
    return 0;
1278
1255
/* 
1279
1256
 * This function determines if a network interface is up.
1280
1257
 */
1281
 
__attribute__((nonnull, warn_unused_result))
1282
1258
bool interface_is_up(const char *interface){
1283
1259
  struct ifreq ifr;
1284
1260
  if(not get_flags(interface, &ifr)){
1295
1271
/* 
1296
1272
 * This function determines if a network interface is running
1297
1273
 */
1298
 
__attribute__((nonnull, warn_unused_result))
1299
1274
bool interface_is_running(const char *interface){
1300
1275
  struct ifreq ifr;
1301
1276
  if(not get_flags(interface, &ifr)){
1309
1284
  return (bool)(ifr.ifr_flags & IFF_RUNNING);
1310
1285
}
1311
1286
 
1312
 
__attribute__((nonnull, pure, warn_unused_result))
1313
1287
int notdotentries(const struct dirent *direntry){
1314
1288
  /* Skip "." and ".." */
1315
1289
  if(direntry->d_name[0] == '.'
1322
1296
}
1323
1297
 
1324
1298
/* Is this directory entry a runnable program? */
1325
 
__attribute__((nonnull, warn_unused_result))
1326
1299
int runnable_hook(const struct dirent *direntry){
1327
1300
  int ret;
1328
1301
  size_t sret;
1336
1309
  sret = strspn(direntry->d_name, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1337
1310
                "abcdefghijklmnopqrstuvwxyz"
1338
1311
                "0123456789"
1339
 
                "_.-");
 
1312
                "_-");
1340
1313
  if((direntry->d_name)[sret] != '\0'){
1341
1314
    /* Contains non-allowed characters */
1342
1315
    if(debug){
1360
1333
    }
1361
1334
    return 0;
1362
1335
  }
1363
 
  free(fullname);
1364
1336
  if(not (S_ISREG(st.st_mode))){
1365
1337
    /* Not a regular file */
1366
1338
    if(debug){
1384
1356
  return 1;
1385
1357
}
1386
1358
 
1387
 
__attribute__((nonnull, warn_unused_result))
1388
1359
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval,
1389
1360
                            mandos_context *mc){
1390
1361
  int ret;
1394
1365
  
1395
1366
  while(true){
1396
1367
    if(mc->current_server == NULL){
1397
 
      if(debug){
 
1368
      if (debug){
1398
1369
        fprintf_plus(stderr, "Wait until first server is found."
1399
1370
                     " No timeout!\n");
1400
1371
      }
1401
1372
      ret = avahi_simple_poll_iterate(s, -1);
1402
1373
    } else {
1403
 
      if(debug){
 
1374
      if (debug){
1404
1375
        fprintf_plus(stderr, "Check current_server if we should run"
1405
1376
                     " it, or wait\n");
1406
1377
      }
1423
1394
                     - ((intmax_t)waited_time.tv_sec * 1000))
1424
1395
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
1425
1396
      
1426
 
      if(debug){
 
1397
      if (debug){
1427
1398
        fprintf_plus(stderr, "Blocking for %" PRIdMAX " ms\n",
1428
1399
                     block_time);
1429
1400
      }
1451
1422
      ret = avahi_simple_poll_iterate(s, (int)block_time);
1452
1423
    }
1453
1424
    if(ret != 0){
1454
 
      if(ret > 0 or errno != EINTR){
 
1425
      if (ret > 0 or errno != EINTR){
1455
1426
        return (ret != 1) ? ret : 0;
1456
1427
      }
1457
1428
    }
1459
1430
}
1460
1431
 
1461
1432
/* Set effective uid to 0, return errno */
1462
 
__attribute__((warn_unused_result))
1463
1433
error_t raise_privileges(void){
1464
1434
  error_t old_errno = errno;
1465
1435
  error_t ret_errno = 0;
1472
1442
}
1473
1443
 
1474
1444
/* Set effective and real user ID to 0.  Return errno. */
1475
 
__attribute__((warn_unused_result))
1476
1445
error_t raise_privileges_permanently(void){
1477
1446
  error_t old_errno = errno;
1478
1447
  error_t ret_errno = raise_privileges();
1489
1458
}
1490
1459
 
1491
1460
/* Set effective user ID to unprivileged saved user ID */
1492
 
__attribute__((warn_unused_result))
1493
1461
error_t lower_privileges(void){
1494
1462
  error_t old_errno = errno;
1495
1463
  error_t ret_errno = 0;
1502
1470
}
1503
1471
 
1504
1472
/* Lower privileges permanently */
1505
 
__attribute__((warn_unused_result))
1506
1473
error_t lower_privileges_permanently(void){
1507
1474
  error_t old_errno = errno;
1508
1475
  error_t ret_errno = 0;
1514
1481
  return ret_errno;
1515
1482
}
1516
1483
 
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,
 
1484
bool run_network_hooks(const char *mode, const char *interface,
1538
1485
                       const float delay){
1539
1486
  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__ */
 
1487
  struct dirent *direntry;
 
1488
  int ret;
 
1489
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
1490
                         alphasort);
1583
1491
  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){
 
1492
    if(errno == ENOENT){
 
1493
      if(debug){
 
1494
        fprintf_plus(stderr, "Network hook directory \"%s\" not"
 
1495
                     " found\n", hookdir);
 
1496
      }
 
1497
    } else {
 
1498
      perror_plus("scandir");
 
1499
    }
 
1500
  } else {
 
1501
    int devnull = open("/dev/null", O_RDONLY);
 
1502
    for(int i = 0; i < numhooks; i++){
 
1503
      direntry = direntries[i];
 
1504
      char *fullname = NULL;
 
1505
      ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
 
1506
      if(ret < 0){
1662
1507
        perror_plus("asprintf");
1663
 
        _exit(EX_OSERR);
1664
 
      }
1665
 
      ret = setenv("DELAY", delaystring, 1);
1666
 
      if(ret == -1){
 
1508
        continue;
 
1509
      }
 
1510
      if(debug){
 
1511
        fprintf_plus(stderr, "Running network hook \"%s\"\n",
 
1512
                     direntry->d_name);
 
1513
      }
 
1514
      pid_t hook_pid = fork();
 
1515
      if(hook_pid == 0){
 
1516
        /* Child */
 
1517
        /* Raise privileges */
 
1518
        raise_privileges_permanently();
 
1519
        /* Set group */
 
1520
        errno = 0;
 
1521
        ret = setgid(0);
 
1522
        if(ret == -1){
 
1523
          perror_plus("setgid");
 
1524
        }
 
1525
        /* Reset supplementary groups */
 
1526
        errno = 0;
 
1527
        ret = setgroups(0, NULL);
 
1528
        if(ret == -1){
 
1529
          perror_plus("setgroups");
 
1530
        }
 
1531
        dup2(devnull, STDIN_FILENO);
 
1532
        close(devnull);
 
1533
        dup2(STDERR_FILENO, STDOUT_FILENO);
 
1534
        ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
 
1535
        if(ret == -1){
 
1536
          perror_plus("setenv");
 
1537
          _exit(EX_OSERR);
 
1538
        }
 
1539
        ret = setenv("DEVICE", interface, 1);
 
1540
        if(ret == -1){
 
1541
          perror_plus("setenv");
 
1542
          _exit(EX_OSERR);
 
1543
        }
 
1544
        ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
 
1545
        if(ret == -1){
 
1546
          perror_plus("setenv");
 
1547
          _exit(EX_OSERR);
 
1548
        }
 
1549
        ret = setenv("MODE", mode, 1);
 
1550
        if(ret == -1){
 
1551
          perror_plus("setenv");
 
1552
          _exit(EX_OSERR);
 
1553
        }
 
1554
        char *delaystring;
 
1555
        ret = asprintf(&delaystring, "%f", delay);
 
1556
        if(ret == -1){
 
1557
          perror_plus("asprintf");
 
1558
          _exit(EX_OSERR);
 
1559
        }
 
1560
        ret = setenv("DELAY", delaystring, 1);
 
1561
        if(ret == -1){
 
1562
          free(delaystring);
 
1563
          perror_plus("setenv");
 
1564
          _exit(EX_OSERR);
 
1565
        }
1667
1566
        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;
 
1567
        if(connect_to != NULL){
 
1568
          ret = setenv("CONNECT", connect_to, 1);
 
1569
          if(ret == -1){
 
1570
            perror_plus("setenv");
 
1571
            _exit(EX_OSERR);
 
1572
          }
 
1573
        }
 
1574
        if(execl(fullname, direntry->d_name, mode, NULL) == -1){
 
1575
          perror_plus("execl");
 
1576
          _exit(EXIT_FAILURE);
 
1577
        }
1704
1578
      } 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);
 
1579
        int status;
 
1580
        if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
 
1581
          perror_plus("waitpid");
 
1582
          free(fullname);
 
1583
          continue;
 
1584
        }
 
1585
        if(WIFEXITED(status)){
 
1586
          if(WEXITSTATUS(status) != 0){
 
1587
            fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
 
1588
                         " with status %d\n", direntry->d_name,
 
1589
                         WEXITSTATUS(status));
 
1590
            free(fullname);
 
1591
            continue;
 
1592
          }
 
1593
        } else if(WIFSIGNALED(status)){
 
1594
          fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
 
1595
                       " signal %d\n", direntry->d_name,
 
1596
                       WTERMSIG(status));
 
1597
          free(fullname);
 
1598
          continue;
 
1599
        } else {
 
1600
          fprintf_plus(stderr, "Warning: network hook \"%s\""
 
1601
                       " crashed\n", direntry->d_name);
 
1602
          free(fullname);
 
1603
          continue;
 
1604
        }
 
1605
      }
 
1606
      free(fullname);
 
1607
      if(debug){
 
1608
        fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
 
1609
                     direntry->d_name);
 
1610
      }
 
1611
    }
 
1612
    close(devnull);
 
1613
  }
 
1614
  return true;
1723
1615
}
1724
1616
 
1725
 
__attribute__((nonnull, warn_unused_result))
1726
1617
error_t bring_up_interface(const char *const interface,
1727
1618
                           const float delay){
 
1619
  int sd = -1;
1728
1620
  error_t old_errno = errno;
1729
 
  int ret;
 
1621
  error_t ret_errno = 0;
 
1622
  int ret, ret_setflags;
1730
1623
  struct ifreq network;
1731
1624
  unsigned int if_index = if_nametoindex(interface);
1732
1625
  if(if_index == 0){
1741
1634
  }
1742
1635
  
1743
1636
  if(not interface_is_up(interface)){
1744
 
    error_t ret_errno = 0, ioctl_errno = 0;
1745
 
    if(not get_flags(interface, &network)){
 
1637
    if(not get_flags(interface, &network) and debug){
1746
1638
      ret_errno = errno;
1747
1639
      fprintf_plus(stderr, "Failed to get flags for interface "
1748
1640
                   "\"%s\"\n", interface);
1749
 
      errno = old_errno;
1750
1641
      return ret_errno;
1751
1642
    }
1752
 
    network.ifr_flags |= IFF_UP; /* set flag */
 
1643
    network.ifr_flags |= IFF_UP;
1753
1644
    
1754
 
    int sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1755
 
    if(sd == -1){
 
1645
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
1646
    if(sd < 0){
1756
1647
      ret_errno = errno;
1757
1648
      perror_plus("socket");
1758
1649
      errno = old_errno;
1759
1650
      return ret_errno;
1760
1651
    }
1761
 
    
 
1652
  
1762
1653
    if(quit_now){
1763
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1764
 
      if(ret == -1){
1765
 
        perror_plus("close");
1766
 
      }
 
1654
      close(sd);
1767
1655
      errno = old_errno;
1768
1656
      return EINTR;
1769
1657
    }
1773
1661
                   interface);
1774
1662
    }
1775
1663
    
1776
 
    /* Raise privileges */
1777
 
    ret_errno = raise_privileges();
1778
 
    if(ret_errno != 0){
1779
 
      perror_plus("Failed to raise privileges");
1780
 
    }
 
1664
    /* Raise priviliges */
 
1665
    raise_privileges();
1781
1666
    
1782
1667
#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
 
      }
 
1668
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
 
1669
       messages about the network interface to mess up the prompt */
 
1670
    int ret_linux = klogctl(8, NULL, 5);
 
1671
    bool restore_loglevel = true;
 
1672
    if(ret_linux == -1){
 
1673
      restore_loglevel = false;
 
1674
      perror_plus("klogctl");
1794
1675
    }
1795
1676
#endif  /* __linux__ */
1796
 
    int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1797
 
    ioctl_errno = errno;
 
1677
    ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
 
1678
    ret_errno = errno;
1798
1679
#ifdef __linux__
1799
1680
    if(restore_loglevel){
1800
1681
      ret_linux = klogctl(7, NULL, 0);
1804
1685
    }
1805
1686
#endif  /* __linux__ */
1806
1687
    
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
 
    }
 
1688
    /* Lower privileges */
 
1689
    lower_privileges();
1816
1690
    
1817
1691
    /* Close the socket */
1818
1692
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
1821
1695
    }
1822
1696
    
1823
1697
    if(ret_setflags == -1){
1824
 
      errno = ioctl_errno;
 
1698
      errno = ret_errno;
1825
1699
      perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1826
1700
      errno = old_errno;
1827
 
      return ioctl_errno;
 
1701
      return ret_errno;
1828
1702
    }
1829
1703
  } else if(debug){
1830
1704
    fprintf_plus(stderr, "Interface \"%s\" is already up; good\n",
1848
1722
  return 0;
1849
1723
}
1850
1724
 
1851
 
__attribute__((nonnull, warn_unused_result))
1852
1725
error_t take_down_interface(const char *const interface){
 
1726
  int sd = -1;
1853
1727
  error_t old_errno = errno;
 
1728
  error_t ret_errno = 0;
 
1729
  int ret, ret_setflags;
1854
1730
  struct ifreq network;
1855
1731
  unsigned int if_index = if_nametoindex(interface);
1856
1732
  if(if_index == 0){
1859
1735
    return ENXIO;
1860
1736
  }
1861
1737
  if(interface_is_up(interface)){
1862
 
    error_t ret_errno = 0, ioctl_errno = 0;
1863
1738
    if(not get_flags(interface, &network) and debug){
1864
1739
      ret_errno = errno;
1865
1740
      fprintf_plus(stderr, "Failed to get flags for interface "
1866
1741
                   "\"%s\"\n", interface);
1867
 
      errno = old_errno;
1868
1742
      return ret_errno;
1869
1743
    }
1870
1744
    network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1871
1745
    
1872
 
    int sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1873
 
    if(sd == -1){
 
1746
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
1747
    if(sd < 0){
1874
1748
      ret_errno = errno;
1875
1749
      perror_plus("socket");
1876
1750
      errno = old_errno;
1882
1756
                   interface);
1883
1757
    }
1884
1758
    
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
 
    }
 
1759
    /* Raise priviliges */
 
1760
    raise_privileges();
 
1761
    
 
1762
    ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
 
1763
    ret_errno = errno;
 
1764
    
 
1765
    /* Lower privileges */
 
1766
    lower_privileges();
1903
1767
    
1904
1768
    /* Close the socket */
1905
 
    int ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1769
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
1906
1770
    if(ret == -1){
1907
1771
      perror_plus("close");
1908
1772
    }
1909
1773
    
1910
1774
    if(ret_setflags == -1){
1911
 
      errno = ioctl_errno;
 
1775
      errno = ret_errno;
1912
1776
      perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
1913
1777
      errno = old_errno;
1914
 
      return ioctl_errno;
 
1778
      return ret_errno;
1915
1779
    }
1916
1780
  } else if(debug){
1917
1781
    fprintf_plus(stderr, "Interface \"%s\" is already down; odd\n",
1925
1789
int main(int argc, char *argv[]){
1926
1790
  mandos_context mc = { .server = NULL, .dh_bits = 1024,
1927
1791
                        .priority = "SECURE256:!CTYPE-X.509:"
1928
 
                        "+CTYPE-OPENPGP", .current_server = NULL,
 
1792
                        "+CTYPE-OPENPGP", .current_server = NULL, 
1929
1793
                        .interfaces = NULL, .interfaces_size = 0 };
1930
1794
  AvahiSServiceBrowser *sb = NULL;
1931
1795
  error_t ret_errno;
1935
1799
  int exitcode = EXIT_SUCCESS;
1936
1800
  char *interfaces_to_take_down = NULL;
1937
1801
  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;
 
1802
  char tempdir[] = "/tmp/mandosXXXXXX";
 
1803
  bool tempdir_created = false;
1941
1804
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1942
1805
  const char *seckey = PATHDIR "/" SECKEY;
1943
1806
  const char *pubkey = PATHDIR "/" PUBKEY;
1944
1807
  char *interfaces_hooks = NULL;
 
1808
  size_t interfaces_hooks_size = 0;
1945
1809
  
1946
1810
  bool gnutls_initialized = false;
1947
1811
  bool gpgme_initialized = false;
2125
1989
    /* Work around Debian bug #633582:
2126
1990
       <http://bugs.debian.org/633582> */
2127
1991
    
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 {
 
1992
    /* Re-raise priviliges */
 
1993
    if(raise_privileges() == 0){
2134
1994
      struct stat st;
2135
1995
      
2136
1996
      if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
2176
2036
      }
2177
2037
    
2178
2038
      /* Lower privileges */
2179
 
      ret_errno = lower_privileges();
2180
 
      if(ret_errno != 0){
2181
 
        errno = ret_errno;
2182
 
        perror_plus("Failed to lower privileges");
2183
 
      }
 
2039
      lower_privileges();
2184
2040
    }
2185
2041
  }
2186
2042
  
2210
2066
        goto end;
2211
2067
      }
2212
2068
      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);
 
2069
      interfaces_hooks_size = mc.interfaces_size;
 
2070
      argz_stringify(interfaces_hooks, interfaces_hooks_size,
 
2071
                     (int)',');
 
2072
    }
 
2073
    if(not run_network_hooks("start", interfaces_hooks != NULL ?
 
2074
                             interfaces_hooks : "", delay)){
 
2075
      goto end;
 
2076
    }
2217
2077
  }
2218
2078
  
2219
2079
  if(not debug){
2307
2167
        ret_errno = argz_add(&mc.interfaces, &mc.interfaces_size,
2308
2168
                             direntries[i]->d_name);
2309
2169
        if(ret_errno != 0){
2310
 
          errno = ret_errno;
2311
2170
          perror_plus("argz_add");
2312
2171
          continue;
2313
2172
        }
2347
2206
        break;
2348
2207
      }
2349
2208
      bool interface_was_up = interface_is_up(interface);
2350
 
      errno = bring_up_interface(interface, delay);
 
2209
      ret = bring_up_interface(interface, delay);
2351
2210
      if(not interface_was_up){
2352
 
        if(errno != 0){
 
2211
        if(ret != 0){
 
2212
          errno = ret;
2353
2213
          perror_plus("Failed to bring up interface");
2354
2214
        } 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
 
          }
 
2215
          ret_errno = argz_add(&interfaces_to_take_down,
 
2216
                               &interfaces_to_take_down_size,
 
2217
                               interface);
2361
2218
        }
2362
2219
      }
2363
2220
    }
2392
2249
    goto end;
2393
2250
  }
2394
2251
  
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){
 
2252
  if(mkdtemp(tempdir) == NULL){
2405
2253
    perror_plus("mkdtemp");
2406
2254
    goto end;
2407
2255
  }
 
2256
  tempdir_created = true;
2408
2257
  
2409
2258
  if(quit_now){
2410
2259
    goto end;
2482
2331
        fprintf_plus(stderr, "Retrying in %d seconds\n",
2483
2332
                     (int)retry_interval);
2484
2333
      }
2485
 
      sleep((unsigned int)retry_interval);
 
2334
      sleep((int)retry_interval);
2486
2335
    }
2487
2336
    
2488
 
    if(not quit_now){
 
2337
    if (not quit_now){
2489
2338
      exitcode = EXIT_SUCCESS;
2490
2339
    }
2491
2340
    
2546
2395
  if(debug){
2547
2396
    fprintf_plus(stderr, "Starting Avahi loop search\n");
2548
2397
  }
2549
 
  
 
2398
 
2550
2399
  ret = avahi_loop_with_timeout(simple_poll,
2551
2400
                                (int)(retry_interval * 1000), &mc);
2552
2401
  if(debug){
2593
2442
    }
2594
2443
  }
2595
2444
  
2596
 
  /* Re-raise privileges */
 
2445
  /* Re-raise priviliges */
2597
2446
  {
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
 
    }
 
2447
    raise_privileges();
 
2448
    
 
2449
    /* Run network hooks */
 
2450
    run_network_hooks("stop", interfaces_hooks != NULL ?
 
2451
                      interfaces_hooks : "", delay);
 
2452
    
 
2453
    /* Take down the network interfaces which were brought up */
 
2454
    {
 
2455
      char *interface = NULL;
 
2456
      while((interface=argz_next(interfaces_to_take_down,
 
2457
                                 interfaces_to_take_down_size,
 
2458
                                 interface))){
 
2459
        ret_errno = take_down_interface(interface);
 
2460
        if(ret_errno != 0){
 
2461
          errno = ret_errno;
 
2462
          perror_plus("Failed to take down interface");
 
2463
        }
 
2464
      }
 
2465
      if(debug and (interfaces_to_take_down == NULL)){
 
2466
        fprintf_plus(stderr, "No interfaces needed to be taken"
 
2467
                     " down\n");
 
2468
      }
 
2469
    }
 
2470
    
 
2471
    lower_privileges_permanently();
2630
2472
  }
2631
2473
  
2632
2474
  free(interfaces_to_take_down);
2633
2475
  free(interfaces_hooks);
2634
2476
  
2635
2477
  /* Removes the GPGME temp directory and all files inside */
2636
 
  if(tempdir != NULL){
 
2478
  if(tempdir_created){
2637
2479
    struct dirent **direntries = NULL;
2638
2480
    struct dirent *direntry = NULL;
2639
2481
    int numentries = scandir(tempdir, &direntries, notdotentries,
2640
2482
                             alphasort);
2641
 
    if(numentries > 0){
 
2483
    if (numentries > 0){
2642
2484
      for(int i = 0; i < numentries; i++){
2643
2485
        direntry = direntries[i];
2644
2486
        char *fullname = NULL;
2656
2498
        free(fullname);
2657
2499
      }
2658
2500
    }
2659
 
    
 
2501
 
2660
2502
    /* need to clean even if 0 because man page doesn't specify */
2661
2503
    free(direntries);
2662
 
    if(numentries == -1){
 
2504
    if (numentries == -1){
2663
2505
      perror_plus("scandir");
2664
2506
    }
2665
2507
    ret = rmdir(tempdir);