/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-06-07 21:58:56 UTC
  • Revision ID: teddy@recompile.se-20140607215856-9q31dfsc68uuvzo4
Make mandos-client use scandirat() if it exists.

* plugins.d/mandos-client.d (hookdir_fd): File descriptor for hookdir.
  (set_cloexec_flag): Define this function if O_CLOEXEC is undefined.
  (run_network_hooks): Open hookdir_fd.  Use scandirat() if it exists.

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-2013 Teddy Hogeborn
13
 
 * Copyright © 2008-2013 Björn Påhlsson
 
12
 * Copyright © 2008-2014 Teddy Hogeborn
 
13
 * Copyright © 2008-2014 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
 
35
#endif  /* not _LARGEFILE_SOURCE */
36
36
#ifndef _FILE_OFFSET_BITS
37
37
#define _FILE_OFFSET_BITS 64
38
 
#endif
 
38
#endif  /* not _FILE_OFFSET_BITS */
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() */
 
58
                                   inet_pton(), connect(),
 
59
                                   getnameinfo() */
59
60
#include <fcntl.h>              /* open() */
60
61
#include <dirent.h>             /* opendir(), struct dirent, readdir()
61
62
                                 */
73
74
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
74
75
                                   getuid(), getgid(), seteuid(),
75
76
                                   setgid(), pause(), _exit() */
76
 
#include <arpa/inet.h>          /* inet_pton(), htons, inet_ntop() */
 
77
#include <arpa/inet.h>          /* inet_pton(), htons() */
77
78
#include <iso646.h>             /* not, or, and */
78
79
#include <argp.h>               /* struct argp_option, error_t, struct
79
80
                                   argp_state, struct argp,
91
92
                                   argz_delete(), argz_append(),
92
93
                                   argz_stringify(), argz_add(),
93
94
                                   argz_count() */
 
95
#include <netdb.h>              /* getnameinfo(), NI_NUMERICHOST,
 
96
                                   EAI_SYSTEM, gai_strerror() */
94
97
 
95
98
#ifdef __linux__
96
99
#include <sys/klog.h>           /* klogctl() */
138
141
static const char sys_class_net[] = "/sys/class/net";
139
142
char *connect_to = NULL;
140
143
const char *hookdir = HOOKDIR;
 
144
int hookdir_fd = -1;
141
145
uid_t uid = 65534;
142
146
gid_t gid = 65534;
143
147
 
180
184
  perror(print_text);
181
185
}
182
186
 
183
 
__attribute__((format (gnu_printf, 2, 3)))
 
187
__attribute__((format (gnu_printf, 2, 3), nonnull))
184
188
int fprintf_plus(FILE *stream, const char *format, ...){
185
189
  va_list ap;
186
190
  va_start (ap, format);
195
199
 * bytes. "buffer_capacity" is how much is currently allocated,
196
200
 * "buffer_length" is how much is already used.
197
201
 */
 
202
__attribute__((nonnull, warn_unused_result))
198
203
size_t incbuffer(char **buffer, size_t buffer_length,
199
204
                 size_t buffer_capacity){
200
205
  if(buffer_length + BUFFER_SIZE > buffer_capacity){
201
 
    *buffer = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
202
 
    if(buffer == NULL){
 
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;
203
212
      return 0;
204
213
    }
 
214
    *buffer = new_buf;
205
215
    buffer_capacity += BUFFER_SIZE;
206
216
  }
207
217
  return buffer_capacity;
208
218
}
209
219
 
210
220
/* Add server to set of servers to retry periodically */
 
221
__attribute__((nonnull, warn_unused_result))
211
222
bool add_server(const char *ip, in_port_t port, AvahiIfIndex if_index,
212
223
                int af, server **current_server){
213
224
  int ret;
224
235
    perror_plus("strdup");
225
236
    return false;
226
237
  }
 
238
  ret = clock_gettime(CLOCK_MONOTONIC, &(new_server->last_seen));
 
239
  if(ret == -1){
 
240
    perror_plus("clock_gettime");
 
241
    return false;
 
242
  }
227
243
  /* Special case of first server */
228
244
  if(*current_server == NULL){
229
245
    new_server->next = new_server;
230
246
    new_server->prev = new_server;
231
247
    *current_server = new_server;
232
 
  /* Place the new server last in the list */
233
248
  } else {
 
249
    /* Place the new server last in the list */
234
250
    new_server->next = *current_server;
235
251
    new_server->prev = (*current_server)->prev;
236
252
    new_server->prev->next = new_server;
237
253
    (*current_server)->prev = new_server;
238
254
  }
239
 
  ret = clock_gettime(CLOCK_MONOTONIC, &(*current_server)->last_seen);
240
 
  if(ret == -1){
241
 
    perror_plus("clock_gettime");
242
 
    return false;
243
 
  }
244
255
  return true;
245
256
}
246
257
 
247
258
/* 
248
259
 * Initialize GPGME.
249
260
 */
250
 
static bool init_gpgme(const char *seckey, const char *pubkey,
251
 
                       const char *tempdir, mandos_context *mc){
 
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){
252
266
  gpgme_error_t rc;
253
267
  gpgme_engine_info_t engine_info;
254
268
  
255
269
  /*
256
270
   * Helper function to insert pub and seckey to the engine keyring.
257
271
   */
258
 
  bool import_key(const char *filename){
 
272
  bool import_key(const char * const filename){
259
273
    int ret;
260
274
    int fd;
261
275
    gpgme_data_t pgp_data;
342
356
 * Decrypt OpenPGP data.
343
357
 * Returns -1 on error
344
358
 */
 
359
__attribute__((nonnull, warn_unused_result))
345
360
static ssize_t pgp_packet_decrypt(const char *cryptotext,
346
361
                                  size_t crypto_size,
347
362
                                  char **plaintext,
467
482
  return plaintext_length;
468
483
}
469
484
 
470
 
static const char * safer_gnutls_strerror(int value){
 
485
__attribute__((warn_unused_result))
 
486
static const char *safer_gnutls_strerror(int value){
471
487
  const char *ret = gnutls_strerror(value);
472
488
  if(ret == NULL)
473
489
    ret = "(unknown)";
475
491
}
476
492
 
477
493
/* GnuTLS log function callback */
 
494
__attribute__((nonnull))
478
495
static void debuggnutls(__attribute__((unused)) int level,
479
496
                        const char* string){
480
497
  fprintf_plus(stderr, "GnuTLS: %s", string);
481
498
}
482
499
 
 
500
__attribute__((nonnull, warn_unused_result))
483
501
static int init_gnutls_global(const char *pubkeyfilename,
484
502
                              const char *seckeyfilename,
485
503
                              mandos_context *mc){
559
577
  return -1;
560
578
}
561
579
 
 
580
__attribute__((nonnull, warn_unused_result))
562
581
static int init_gnutls_session(gnutls_session_t *session,
563
582
                               mandos_context *mc){
564
583
  int ret;
621
640
                      __attribute__((unused)) const char *txt){}
622
641
 
623
642
/* Called when a Mandos server is found */
 
643
__attribute__((nonnull, warn_unused_result))
624
644
static int start_mandos_communication(const char *ip, in_port_t port,
625
645
                                      AvahiIfIndex if_index,
626
646
                                      int af, mandos_context *mc){
627
647
  int ret, tcp_sd = -1;
628
648
  ssize_t sret;
629
 
  union {
630
 
    struct sockaddr_in in;
631
 
    struct sockaddr_in6 in6;
632
 
  } to;
 
649
  struct sockaddr_storage to;
633
650
  char *buffer = NULL;
634
651
  char *decrypted_buffer = NULL;
635
652
  size_t buffer_length = 0;
716
733
  
717
734
  memset(&to, 0, sizeof(to));
718
735
  if(af == AF_INET6){
719
 
    to.in6.sin6_family = (sa_family_t)af;
720
 
    ret = inet_pton(af, ip, &to.in6.sin6_addr);
 
736
    ((struct sockaddr_in6 *)&to)->sin6_family = (sa_family_t)af;
 
737
    ret = inet_pton(af, ip, &((struct sockaddr_in6 *)&to)->sin6_addr);
721
738
  } else {                      /* IPv4 */
722
 
    to.in.sin_family = (sa_family_t)af;
723
 
    ret = inet_pton(af, ip, &to.in.sin_addr);
 
739
    ((struct sockaddr_in *)&to)->sin_family = (sa_family_t)af;
 
740
    ret = inet_pton(af, ip, &((struct sockaddr_in *)&to)->sin_addr);
724
741
  }
725
742
  if(ret < 0 ){
726
743
    int e = errno;
735
752
    goto mandos_end;
736
753
  }
737
754
  if(af == AF_INET6){
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*/
 
755
    ((struct sockaddr_in6 *)&to)->sin6_port = htons(port);
 
756
    if(IN6_IS_ADDR_LINKLOCAL
 
757
       (&((struct sockaddr_in6 *)&to)->sin6_addr)){
742
758
      if(if_index == AVAHI_IF_UNSPEC){
743
759
        fprintf_plus(stderr, "An IPv6 link-local address is"
744
760
                     " incomplete without a network interface\n");
746
762
        goto mandos_end;
747
763
      }
748
764
      /* Set the network interface number as scope */
749
 
      to.in6.sin6_scope_id = (uint32_t)if_index;
 
765
      ((struct sockaddr_in6 *)&to)->sin6_scope_id = (uint32_t)if_index;
750
766
    }
751
767
  } else {
752
 
    to.in.sin_port = htons(port); /* Spurious warnings from
753
 
                                     -Wconversion and
754
 
                                     -Wunreachable-code */
 
768
    ((struct sockaddr_in *)&to)->sin_port = htons(port);
755
769
  }
756
770
  
757
771
  if(quit_now){
774
788
    }
775
789
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
776
790
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
777
 
    const char *pcret;
778
791
    if(af == AF_INET6){
779
 
      pcret = inet_ntop(af, &(to.in6.sin6_addr), addrstr,
780
 
                        sizeof(addrstr));
 
792
      ret = getnameinfo((struct sockaddr *)&to,
 
793
                        sizeof(struct sockaddr_in6),
 
794
                        addrstr, sizeof(addrstr), NULL, 0,
 
795
                        NI_NUMERICHOST);
781
796
    } else {
782
 
      pcret = inet_ntop(af, &(to.in.sin_addr), addrstr,
783
 
                        sizeof(addrstr));
 
797
      ret = getnameinfo((struct sockaddr *)&to,
 
798
                        sizeof(struct sockaddr_in),
 
799
                        addrstr, sizeof(addrstr), NULL, 0,
 
800
                        NI_NUMERICHOST);
784
801
    }
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
 
      }
 
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);
791
808
    }
792
809
  }
793
810
  
797
814
  }
798
815
  
799
816
  if(af == AF_INET6){
800
 
    ret = connect(tcp_sd, &to.in6, sizeof(to));
 
817
    ret = connect(tcp_sd, (struct sockaddr *)&to,
 
818
                  sizeof(struct sockaddr_in6));
801
819
  } else {
802
 
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
 
820
    ret = connect(tcp_sd, (struct sockaddr *)&to, /* IPv4 */
 
821
                  sizeof(struct sockaddr_in));
803
822
  }
804
823
  if(ret < 0){
805
 
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
 
824
    if((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
806
825
      int e = errno;
807
826
      perror_plus("connect");
808
827
      errno = e;
1023
1042
  return retval;
1024
1043
}
1025
1044
 
 
1045
__attribute__((nonnull))
1026
1046
static void resolve_callback(AvahiSServiceResolver *r,
1027
1047
                             AvahiIfIndex interface,
1028
1048
                             AvahiProtocol proto,
1036
1056
                             AVAHI_GCC_UNUSED AvahiStringList *txt,
1037
1057
                             AVAHI_GCC_UNUSED AvahiLookupResultFlags
1038
1058
                             flags,
1039
 
                             void* mc){
 
1059
                             void *mc){
1040
1060
  if(r == NULL){
1041
1061
    return;
1042
1062
  }
1095
1115
                            const char *domain,
1096
1116
                            AVAHI_GCC_UNUSED AvahiLookupResultFlags
1097
1117
                            flags,
1098
 
                            void* mc){
 
1118
                            void *mc){
1099
1119
  if(b == NULL){
1100
1120
    return;
1101
1121
  }
1161
1181
  errno = old_errno;
1162
1182
}
1163
1183
 
 
1184
__attribute__((nonnull, warn_unused_result))
1164
1185
bool get_flags(const char *ifname, struct ifreq *ifr){
1165
1186
  int ret;
1166
1187
  error_t ret_errno;
1185
1206
  return true;
1186
1207
}
1187
1208
 
 
1209
__attribute__((nonnull, warn_unused_result))
1188
1210
bool good_flags(const char *ifname, const struct ifreq *ifr){
1189
1211
  
1190
1212
  /* Reject the loopback device */
1232
1254
 * corresponds to an acceptable network device.
1233
1255
 * (This function is passed to scandir(3) as a filter function.)
1234
1256
 */
 
1257
__attribute__((nonnull, warn_unused_result))
1235
1258
int good_interface(const struct dirent *if_entry){
1236
1259
  if(if_entry->d_name[0] == '.'){
1237
1260
    return 0;
1255
1278
/* 
1256
1279
 * This function determines if a network interface is up.
1257
1280
 */
 
1281
__attribute__((nonnull, warn_unused_result))
1258
1282
bool interface_is_up(const char *interface){
1259
1283
  struct ifreq ifr;
1260
1284
  if(not get_flags(interface, &ifr)){
1271
1295
/* 
1272
1296
 * This function determines if a network interface is running
1273
1297
 */
 
1298
__attribute__((nonnull, warn_unused_result))
1274
1299
bool interface_is_running(const char *interface){
1275
1300
  struct ifreq ifr;
1276
1301
  if(not get_flags(interface, &ifr)){
1284
1309
  return (bool)(ifr.ifr_flags & IFF_RUNNING);
1285
1310
}
1286
1311
 
 
1312
__attribute__((nonnull, pure, warn_unused_result))
1287
1313
int notdotentries(const struct dirent *direntry){
1288
1314
  /* Skip "." and ".." */
1289
1315
  if(direntry->d_name[0] == '.'
1296
1322
}
1297
1323
 
1298
1324
/* Is this directory entry a runnable program? */
 
1325
__attribute__((nonnull, warn_unused_result))
1299
1326
int runnable_hook(const struct dirent *direntry){
1300
1327
  int ret;
1301
1328
  size_t sret;
1309
1336
  sret = strspn(direntry->d_name, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1310
1337
                "abcdefghijklmnopqrstuvwxyz"
1311
1338
                "0123456789"
1312
 
                "_-");
 
1339
                "_.-");
1313
1340
  if((direntry->d_name)[sret] != '\0'){
1314
1341
    /* Contains non-allowed characters */
1315
1342
    if(debug){
1333
1360
    }
1334
1361
    return 0;
1335
1362
  }
 
1363
  free(fullname);
1336
1364
  if(not (S_ISREG(st.st_mode))){
1337
1365
    /* Not a regular file */
1338
1366
    if(debug){
1356
1384
  return 1;
1357
1385
}
1358
1386
 
 
1387
__attribute__((nonnull, warn_unused_result))
1359
1388
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval,
1360
1389
                            mandos_context *mc){
1361
1390
  int ret;
1365
1394
  
1366
1395
  while(true){
1367
1396
    if(mc->current_server == NULL){
1368
 
      if (debug){
 
1397
      if(debug){
1369
1398
        fprintf_plus(stderr, "Wait until first server is found."
1370
1399
                     " No timeout!\n");
1371
1400
      }
1372
1401
      ret = avahi_simple_poll_iterate(s, -1);
1373
1402
    } else {
1374
 
      if (debug){
 
1403
      if(debug){
1375
1404
        fprintf_plus(stderr, "Check current_server if we should run"
1376
1405
                     " it, or wait\n");
1377
1406
      }
1394
1423
                     - ((intmax_t)waited_time.tv_sec * 1000))
1395
1424
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
1396
1425
      
1397
 
      if (debug){
 
1426
      if(debug){
1398
1427
        fprintf_plus(stderr, "Blocking for %" PRIdMAX " ms\n",
1399
1428
                     block_time);
1400
1429
      }
1422
1451
      ret = avahi_simple_poll_iterate(s, (int)block_time);
1423
1452
    }
1424
1453
    if(ret != 0){
1425
 
      if (ret > 0 or errno != EINTR){
 
1454
      if(ret > 0 or errno != EINTR){
1426
1455
        return (ret != 1) ? ret : 0;
1427
1456
      }
1428
1457
    }
1430
1459
}
1431
1460
 
1432
1461
/* Set effective uid to 0, return errno */
 
1462
__attribute__((warn_unused_result))
1433
1463
error_t raise_privileges(void){
1434
1464
  error_t old_errno = errno;
1435
1465
  error_t ret_errno = 0;
1442
1472
}
1443
1473
 
1444
1474
/* Set effective and real user ID to 0.  Return errno. */
 
1475
__attribute__((warn_unused_result))
1445
1476
error_t raise_privileges_permanently(void){
1446
1477
  error_t old_errno = errno;
1447
1478
  error_t ret_errno = raise_privileges();
1458
1489
}
1459
1490
 
1460
1491
/* Set effective user ID to unprivileged saved user ID */
 
1492
__attribute__((warn_unused_result))
1461
1493
error_t lower_privileges(void){
1462
1494
  error_t old_errno = errno;
1463
1495
  error_t ret_errno = 0;
1470
1502
}
1471
1503
 
1472
1504
/* Lower privileges permanently */
 
1505
__attribute__((warn_unused_result))
1473
1506
error_t lower_privileges_permanently(void){
1474
1507
  error_t old_errno = errno;
1475
1508
  error_t ret_errno = 0;
1481
1514
  return ret_errno;
1482
1515
}
1483
1516
 
1484
 
bool run_network_hooks(const char *mode, const char *interface,
 
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,
1485
1538
                       const float delay){
1486
1539
  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__ */
 
1583
  if(numhooks == -1){
 
1584
    perror_plus("scandir");
 
1585
    return;
 
1586
  }
1487
1587
  struct dirent *direntry;
1488
1588
  int ret;
1489
 
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1490
 
                         alphasort);
1491
 
  if(numhooks == -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){
 
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){
1507
1662
        perror_plus("asprintf");
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
 
        }
 
1663
        _exit(EX_OSERR);
 
1664
      }
 
1665
      ret = setenv("DELAY", delaystring, 1);
 
1666
      if(ret == -1){
1566
1667
        free(delaystring);
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
 
        }
 
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;
1578
1704
      } else {
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;
 
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);
1615
1723
}
1616
1724
 
 
1725
__attribute__((nonnull, warn_unused_result))
1617
1726
error_t bring_up_interface(const char *const interface,
1618
1727
                           const float delay){
1619
 
  int sd = -1;
1620
1728
  error_t old_errno = errno;
1621
 
  error_t ret_errno = 0;
1622
 
  int ret, ret_setflags;
 
1729
  int ret;
1623
1730
  struct ifreq network;
1624
1731
  unsigned int if_index = if_nametoindex(interface);
1625
1732
  if(if_index == 0){
1634
1741
  }
1635
1742
  
1636
1743
  if(not interface_is_up(interface)){
1637
 
    if(not get_flags(interface, &network) and debug){
 
1744
    error_t ret_errno = 0, ioctl_errno = 0;
 
1745
    if(not get_flags(interface, &network)){
1638
1746
      ret_errno = errno;
1639
1747
      fprintf_plus(stderr, "Failed to get flags for interface "
1640
1748
                   "\"%s\"\n", interface);
 
1749
      errno = old_errno;
1641
1750
      return ret_errno;
1642
1751
    }
1643
 
    network.ifr_flags |= IFF_UP;
 
1752
    network.ifr_flags |= IFF_UP; /* set flag */
1644
1753
    
1645
 
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1646
 
    if(sd < 0){
 
1754
    int sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
1755
    if(sd == -1){
1647
1756
      ret_errno = errno;
1648
1757
      perror_plus("socket");
1649
1758
      errno = old_errno;
1650
1759
      return ret_errno;
1651
1760
    }
1652
 
  
 
1761
    
1653
1762
    if(quit_now){
1654
 
      close(sd);
 
1763
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1764
      if(ret == -1){
 
1765
        perror_plus("close");
 
1766
      }
1655
1767
      errno = old_errno;
1656
1768
      return EINTR;
1657
1769
    }
1661
1773
                   interface);
1662
1774
    }
1663
1775
    
1664
 
    /* Raise priviliges */
1665
 
    raise_privileges();
 
1776
    /* Raise privileges */
 
1777
    ret_errno = raise_privileges();
 
1778
    if(ret_errno != 0){
 
1779
      perror_plus("Failed to raise privileges");
 
1780
    }
1666
1781
    
1667
1782
#ifdef __linux__
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");
 
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
      }
1675
1794
    }
1676
1795
#endif  /* __linux__ */
1677
 
    ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1678
 
    ret_errno = errno;
 
1796
    int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
 
1797
    ioctl_errno = errno;
1679
1798
#ifdef __linux__
1680
1799
    if(restore_loglevel){
1681
1800
      ret_linux = klogctl(7, NULL, 0);
1685
1804
    }
1686
1805
#endif  /* __linux__ */
1687
1806
    
1688
 
    /* Lower privileges */
1689
 
    lower_privileges();
 
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
    }
1690
1816
    
1691
1817
    /* Close the socket */
1692
1818
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
1695
1821
    }
1696
1822
    
1697
1823
    if(ret_setflags == -1){
1698
 
      errno = ret_errno;
 
1824
      errno = ioctl_errno;
1699
1825
      perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1700
1826
      errno = old_errno;
1701
 
      return ret_errno;
 
1827
      return ioctl_errno;
1702
1828
    }
1703
1829
  } else if(debug){
1704
1830
    fprintf_plus(stderr, "Interface \"%s\" is already up; good\n",
1722
1848
  return 0;
1723
1849
}
1724
1850
 
 
1851
__attribute__((nonnull, warn_unused_result))
1725
1852
error_t take_down_interface(const char *const interface){
1726
 
  int sd = -1;
1727
1853
  error_t old_errno = errno;
1728
 
  error_t ret_errno = 0;
1729
 
  int ret, ret_setflags;
1730
1854
  struct ifreq network;
1731
1855
  unsigned int if_index = if_nametoindex(interface);
1732
1856
  if(if_index == 0){
1735
1859
    return ENXIO;
1736
1860
  }
1737
1861
  if(interface_is_up(interface)){
 
1862
    error_t ret_errno = 0, ioctl_errno = 0;
1738
1863
    if(not get_flags(interface, &network) and debug){
1739
1864
      ret_errno = errno;
1740
1865
      fprintf_plus(stderr, "Failed to get flags for interface "
1741
1866
                   "\"%s\"\n", interface);
 
1867
      errno = old_errno;
1742
1868
      return ret_errno;
1743
1869
    }
1744
1870
    network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1745
1871
    
1746
 
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1747
 
    if(sd < 0){
 
1872
    int sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
1873
    if(sd == -1){
1748
1874
      ret_errno = errno;
1749
1875
      perror_plus("socket");
1750
1876
      errno = old_errno;
1756
1882
                   interface);
1757
1883
    }
1758
1884
    
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();
 
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
    }
1767
1903
    
1768
1904
    /* Close the socket */
1769
 
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1905
    int ret = (int)TEMP_FAILURE_RETRY(close(sd));
1770
1906
    if(ret == -1){
1771
1907
      perror_plus("close");
1772
1908
    }
1773
1909
    
1774
1910
    if(ret_setflags == -1){
1775
 
      errno = ret_errno;
 
1911
      errno = ioctl_errno;
1776
1912
      perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
1777
1913
      errno = old_errno;
1778
 
      return ret_errno;
 
1914
      return ioctl_errno;
1779
1915
    }
1780
1916
  } else if(debug){
1781
1917
    fprintf_plus(stderr, "Interface \"%s\" is already down; odd\n",
1789
1925
int main(int argc, char *argv[]){
1790
1926
  mandos_context mc = { .server = NULL, .dh_bits = 1024,
1791
1927
                        .priority = "SECURE256:!CTYPE-X.509:"
1792
 
                        "+CTYPE-OPENPGP", .current_server = NULL, 
 
1928
                        "+CTYPE-OPENPGP", .current_server = NULL,
1793
1929
                        .interfaces = NULL, .interfaces_size = 0 };
1794
1930
  AvahiSServiceBrowser *sb = NULL;
1795
1931
  error_t ret_errno;
1799
1935
  int exitcode = EXIT_SUCCESS;
1800
1936
  char *interfaces_to_take_down = NULL;
1801
1937
  size_t interfaces_to_take_down_size = 0;
1802
 
  char tempdir[] = "/tmp/mandosXXXXXX";
1803
 
  bool tempdir_created = false;
 
1938
  char run_tempdir[] = "/run/tmp/mandosXXXXXX";
 
1939
  char old_tempdir[] = "/tmp/mandosXXXXXX";
 
1940
  char *tempdir = NULL;
1804
1941
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1805
1942
  const char *seckey = PATHDIR "/" SECKEY;
1806
1943
  const char *pubkey = PATHDIR "/" PUBKEY;
1807
1944
  char *interfaces_hooks = NULL;
1808
 
  size_t interfaces_hooks_size = 0;
1809
1945
  
1810
1946
  bool gnutls_initialized = false;
1811
1947
  bool gpgme_initialized = false;
1989
2125
    /* Work around Debian bug #633582:
1990
2126
       <http://bugs.debian.org/633582> */
1991
2127
    
1992
 
    /* Re-raise priviliges */
1993
 
    if(raise_privileges() == 0){
 
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 {
1994
2134
      struct stat st;
1995
2135
      
1996
2136
      if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
2036
2176
      }
2037
2177
    
2038
2178
      /* Lower privileges */
2039
 
      lower_privileges();
 
2179
      ret_errno = lower_privileges();
 
2180
      if(ret_errno != 0){
 
2181
        errno = ret_errno;
 
2182
        perror_plus("Failed to lower privileges");
 
2183
      }
2040
2184
    }
2041
2185
  }
2042
2186
  
2066
2210
        goto end;
2067
2211
      }
2068
2212
      memcpy(interfaces_hooks, mc.interfaces, mc.interfaces_size);
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
 
    }
 
2213
      argz_stringify(interfaces_hooks, mc.interfaces_size, (int)',');
 
2214
    }
 
2215
    run_network_hooks("start", interfaces_hooks != NULL ?
 
2216
                      interfaces_hooks : "", delay);
2077
2217
  }
2078
2218
  
2079
2219
  if(not debug){
2167
2307
        ret_errno = argz_add(&mc.interfaces, &mc.interfaces_size,
2168
2308
                             direntries[i]->d_name);
2169
2309
        if(ret_errno != 0){
 
2310
          errno = ret_errno;
2170
2311
          perror_plus("argz_add");
2171
2312
          continue;
2172
2313
        }
2206
2347
        break;
2207
2348
      }
2208
2349
      bool interface_was_up = interface_is_up(interface);
2209
 
      ret = bring_up_interface(interface, delay);
 
2350
      errno = bring_up_interface(interface, delay);
2210
2351
      if(not interface_was_up){
2211
 
        if(ret != 0){
2212
 
          errno = ret;
 
2352
        if(errno != 0){
2213
2353
          perror_plus("Failed to bring up interface");
2214
2354
        } else {
2215
 
          ret_errno = argz_add(&interfaces_to_take_down,
2216
 
                               &interfaces_to_take_down_size,
2217
 
                               interface);
 
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
          }
2218
2361
        }
2219
2362
      }
2220
2363
    }
2249
2392
    goto end;
2250
2393
  }
2251
2394
  
2252
 
  if(mkdtemp(tempdir) == NULL){
 
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){
2253
2405
    perror_plus("mkdtemp");
2254
2406
    goto end;
2255
2407
  }
2256
 
  tempdir_created = true;
2257
2408
  
2258
2409
  if(quit_now){
2259
2410
    goto end;
2334
2485
      sleep((unsigned int)retry_interval);
2335
2486
    }
2336
2487
    
2337
 
    if (not quit_now){
 
2488
    if(not quit_now){
2338
2489
      exitcode = EXIT_SUCCESS;
2339
2490
    }
2340
2491
    
2395
2546
  if(debug){
2396
2547
    fprintf_plus(stderr, "Starting Avahi loop search\n");
2397
2548
  }
2398
 
 
 
2549
  
2399
2550
  ret = avahi_loop_with_timeout(simple_poll,
2400
2551
                                (int)(retry_interval * 1000), &mc);
2401
2552
  if(debug){
2442
2593
    }
2443
2594
  }
2444
2595
  
2445
 
  /* Re-raise priviliges */
 
2596
  /* Re-raise privileges */
2446
2597
  {
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();
 
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
    }
2472
2630
  }
2473
2631
  
2474
2632
  free(interfaces_to_take_down);
2475
2633
  free(interfaces_hooks);
2476
2634
  
2477
2635
  /* Removes the GPGME temp directory and all files inside */
2478
 
  if(tempdir_created){
 
2636
  if(tempdir != NULL){
2479
2637
    struct dirent **direntries = NULL;
2480
2638
    struct dirent *direntry = NULL;
2481
2639
    int numentries = scandir(tempdir, &direntries, notdotentries,
2482
2640
                             alphasort);
2483
 
    if (numentries > 0){
 
2641
    if(numentries > 0){
2484
2642
      for(int i = 0; i < numentries; i++){
2485
2643
        direntry = direntries[i];
2486
2644
        char *fullname = NULL;
2498
2656
        free(fullname);
2499
2657
      }
2500
2658
    }
2501
 
 
 
2659
    
2502
2660
    /* need to clean even if 0 because man page doesn't specify */
2503
2661
    free(direntries);
2504
 
    if (numentries == -1){
 
2662
    if(numentries == -1){
2505
2663
      perror_plus("scandir");
2506
2664
    }
2507
2665
    ret = rmdir(tempdir);