/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-03-10 06:34:36 UTC
  • Revision ID: teddy@recompile.se-20140310063436-zksdghqvv6k1pbyg
Do not add a new server to server list if clock_gettime() fails

* plugins.d/mandos-client.c (add_server): Set server.last_seen before
                                          adding new server, in case
                                          clock_gettime() fails.

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
 * "browse_callback", and parts of "main".
10
10
 * 
11
11
 * Everything else is
12
 
 * Copyright © 2008-2014 Teddy Hogeborn
13
 
 * Copyright © 2008-2014 Björn Påhlsson
 
12
 * Copyright © 2008-2013 Teddy Hogeborn
 
13
 * Copyright © 2008-2013 Björn Påhlsson
14
14
 * 
15
15
 * This program is free software: you can redistribute it and/or
16
16
 * modify it under the terms of the GNU General Public License as
183
183
  perror(print_text);
184
184
}
185
185
 
186
 
__attribute__((format (gnu_printf, 2, 3), nonnull))
 
186
__attribute__((format (gnu_printf, 2, 3)))
187
187
int fprintf_plus(FILE *stream, const char *format, ...){
188
188
  va_list ap;
189
189
  va_start (ap, format);
198
198
 * bytes. "buffer_capacity" is how much is currently allocated,
199
199
 * "buffer_length" is how much is already used.
200
200
 */
201
 
__attribute__((nonnull, warn_unused_result))
202
201
size_t incbuffer(char **buffer, size_t buffer_length,
203
202
                 size_t buffer_capacity){
204
203
  if(buffer_length + BUFFER_SIZE > buffer_capacity){
217
216
}
218
217
 
219
218
/* Add server to set of servers to retry periodically */
220
 
__attribute__((nonnull, warn_unused_result))
221
219
bool add_server(const char *ip, in_port_t port, AvahiIfIndex if_index,
222
220
                int af, server **current_server){
223
221
  int ret;
244
242
    new_server->next = new_server;
245
243
    new_server->prev = new_server;
246
244
    *current_server = new_server;
 
245
  /* Place the new server last in the list */
247
246
  } else {
248
 
    /* Place the new server last in the list */
249
247
    new_server->next = *current_server;
250
248
    new_server->prev = (*current_server)->prev;
251
249
    new_server->prev->next = new_server;
257
255
/* 
258
256
 * Initialize GPGME.
259
257
 */
260
 
__attribute__((nonnull, warn_unused_result))
261
 
static bool init_gpgme(const char * const seckey,
262
 
                       const char * const pubkey,
263
 
                       const char * const tempdir,
264
 
                       mandos_context *mc){
 
258
static bool init_gpgme(const char *seckey, const char *pubkey,
 
259
                       const char *tempdir, mandos_context *mc){
265
260
  gpgme_error_t rc;
266
261
  gpgme_engine_info_t engine_info;
267
262
  
268
263
  /*
269
264
   * Helper function to insert pub and seckey to the engine keyring.
270
265
   */
271
 
  bool import_key(const char * const filename){
 
266
  bool import_key(const char *filename){
272
267
    int ret;
273
268
    int fd;
274
269
    gpgme_data_t pgp_data;
355
350
 * Decrypt OpenPGP data.
356
351
 * Returns -1 on error
357
352
 */
358
 
__attribute__((nonnull, warn_unused_result))
359
353
static ssize_t pgp_packet_decrypt(const char *cryptotext,
360
354
                                  size_t crypto_size,
361
355
                                  char **plaintext,
481
475
  return plaintext_length;
482
476
}
483
477
 
484
 
__attribute__((warn_unused_result))
485
 
static const char *safer_gnutls_strerror(int value){
 
478
static const char * safer_gnutls_strerror(int value){
486
479
  const char *ret = gnutls_strerror(value);
487
480
  if(ret == NULL)
488
481
    ret = "(unknown)";
490
483
}
491
484
 
492
485
/* GnuTLS log function callback */
493
 
__attribute__((nonnull))
494
486
static void debuggnutls(__attribute__((unused)) int level,
495
487
                        const char* string){
496
488
  fprintf_plus(stderr, "GnuTLS: %s", string);
497
489
}
498
490
 
499
 
__attribute__((nonnull, warn_unused_result))
500
491
static int init_gnutls_global(const char *pubkeyfilename,
501
492
                              const char *seckeyfilename,
502
493
                              mandos_context *mc){
576
567
  return -1;
577
568
}
578
569
 
579
 
__attribute__((nonnull, warn_unused_result))
580
570
static int init_gnutls_session(gnutls_session_t *session,
581
571
                               mandos_context *mc){
582
572
  int ret;
639
629
                      __attribute__((unused)) const char *txt){}
640
630
 
641
631
/* Called when a Mandos server is found */
642
 
__attribute__((nonnull, warn_unused_result))
643
632
static int start_mandos_communication(const char *ip, in_port_t port,
644
633
                                      AvahiIfIndex if_index,
645
634
                                      int af, mandos_context *mc){
646
635
  int ret, tcp_sd = -1;
647
636
  ssize_t sret;
648
 
  struct sockaddr_storage to;
 
637
  union {
 
638
    struct sockaddr_in in;
 
639
    struct sockaddr_in6 in6;
 
640
  } to;
649
641
  char *buffer = NULL;
650
642
  char *decrypted_buffer = NULL;
651
643
  size_t buffer_length = 0;
732
724
  
733
725
  memset(&to, 0, sizeof(to));
734
726
  if(af == AF_INET6){
735
 
    ((struct sockaddr_in6 *)&to)->sin6_family = (sa_family_t)af;
736
 
    ret = inet_pton(af, ip, &((struct sockaddr_in6 *)&to)->sin6_addr);
 
727
    to.in6.sin6_family = (sa_family_t)af;
 
728
    ret = inet_pton(af, ip, &to.in6.sin6_addr);
737
729
  } else {                      /* IPv4 */
738
 
    ((struct sockaddr_in *)&to)->sin_family = (sa_family_t)af;
739
 
    ret = inet_pton(af, ip, &((struct sockaddr_in *)&to)->sin_addr);
 
730
    to.in.sin_family = (sa_family_t)af;
 
731
    ret = inet_pton(af, ip, &to.in.sin_addr);
740
732
  }
741
733
  if(ret < 0 ){
742
734
    int e = errno;
751
743
    goto mandos_end;
752
744
  }
753
745
  if(af == AF_INET6){
754
 
    ((struct sockaddr_in6 *)&to)->sin6_port = htons(port);
755
 
    if(IN6_IS_ADDR_LINKLOCAL
756
 
       (&((struct sockaddr_in6 *)&to)->sin6_addr)){
 
746
    to.in6.sin6_port = htons(port);    
 
747
#ifdef __GNUC__
 
748
#pragma GCC diagnostic push
 
749
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
 
750
#endif
 
751
    if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
 
752
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower */
 
753
#ifdef __GNUC__
 
754
#pragma GCC diagnostic pop
 
755
#endif
757
756
      if(if_index == AVAHI_IF_UNSPEC){
758
757
        fprintf_plus(stderr, "An IPv6 link-local address is"
759
758
                     " incomplete without a network interface\n");
761
760
        goto mandos_end;
762
761
      }
763
762
      /* Set the network interface number as scope */
764
 
      ((struct sockaddr_in6 *)&to)->sin6_scope_id = (uint32_t)if_index;
 
763
      to.in6.sin6_scope_id = (uint32_t)if_index;
765
764
    }
766
765
  } else {
767
 
    ((struct sockaddr_in *)&to)->sin_port = htons(port);
 
766
    to.in.sin_port = htons(port);
768
767
  }
769
768
  
770
769
  if(quit_now){
788
787
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
789
788
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
790
789
    if(af == AF_INET6){
791
 
      ret = getnameinfo((struct sockaddr *)&to,
792
 
                        sizeof(struct sockaddr_in6),
 
790
      ret = getnameinfo((struct sockaddr *)&(to.in6), sizeof(to.in6),
793
791
                        addrstr, sizeof(addrstr), NULL, 0,
794
792
                        NI_NUMERICHOST);
795
793
    } else {
796
 
      ret = getnameinfo((struct sockaddr *)&to,
797
 
                        sizeof(struct sockaddr_in),
 
794
      ret = getnameinfo((struct sockaddr *)&(to.in), sizeof(to.in),
798
795
                        addrstr, sizeof(addrstr), NULL, 0,
799
796
                        NI_NUMERICHOST);
800
797
    }
813
810
  }
814
811
  
815
812
  if(af == AF_INET6){
816
 
    ret = connect(tcp_sd, (struct sockaddr *)&to,
817
 
                  sizeof(struct sockaddr_in6));
 
813
    ret = connect(tcp_sd, &to.in6, sizeof(to));
818
814
  } else {
819
 
    ret = connect(tcp_sd, (struct sockaddr *)&to, /* IPv4 */
820
 
                  sizeof(struct sockaddr_in));
 
815
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
821
816
  }
822
817
  if(ret < 0){
823
 
    if((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
 
818
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
824
819
      int e = errno;
825
820
      perror_plus("connect");
826
821
      errno = e;
1041
1036
  return retval;
1042
1037
}
1043
1038
 
1044
 
__attribute__((nonnull))
1045
1039
static void resolve_callback(AvahiSServiceResolver *r,
1046
1040
                             AvahiIfIndex interface,
1047
1041
                             AvahiProtocol proto,
1055
1049
                             AVAHI_GCC_UNUSED AvahiStringList *txt,
1056
1050
                             AVAHI_GCC_UNUSED AvahiLookupResultFlags
1057
1051
                             flags,
1058
 
                             void *mc){
 
1052
                             void* mc){
1059
1053
  if(r == NULL){
1060
1054
    return;
1061
1055
  }
1114
1108
                            const char *domain,
1115
1109
                            AVAHI_GCC_UNUSED AvahiLookupResultFlags
1116
1110
                            flags,
1117
 
                            void *mc){
 
1111
                            void* mc){
1118
1112
  if(b == NULL){
1119
1113
    return;
1120
1114
  }
1180
1174
  errno = old_errno;
1181
1175
}
1182
1176
 
1183
 
__attribute__((nonnull, warn_unused_result))
1184
1177
bool get_flags(const char *ifname, struct ifreq *ifr){
1185
1178
  int ret;
1186
1179
  error_t ret_errno;
1205
1198
  return true;
1206
1199
}
1207
1200
 
1208
 
__attribute__((nonnull, warn_unused_result))
1209
1201
bool good_flags(const char *ifname, const struct ifreq *ifr){
1210
1202
  
1211
1203
  /* Reject the loopback device */
1253
1245
 * corresponds to an acceptable network device.
1254
1246
 * (This function is passed to scandir(3) as a filter function.)
1255
1247
 */
1256
 
__attribute__((nonnull, warn_unused_result))
1257
1248
int good_interface(const struct dirent *if_entry){
1258
1249
  if(if_entry->d_name[0] == '.'){
1259
1250
    return 0;
1277
1268
/* 
1278
1269
 * This function determines if a network interface is up.
1279
1270
 */
1280
 
__attribute__((nonnull, warn_unused_result))
1281
1271
bool interface_is_up(const char *interface){
1282
1272
  struct ifreq ifr;
1283
1273
  if(not get_flags(interface, &ifr)){
1294
1284
/* 
1295
1285
 * This function determines if a network interface is running
1296
1286
 */
1297
 
__attribute__((nonnull, warn_unused_result))
1298
1287
bool interface_is_running(const char *interface){
1299
1288
  struct ifreq ifr;
1300
1289
  if(not get_flags(interface, &ifr)){
1308
1297
  return (bool)(ifr.ifr_flags & IFF_RUNNING);
1309
1298
}
1310
1299
 
1311
 
__attribute__((nonnull, pure, warn_unused_result))
1312
1300
int notdotentries(const struct dirent *direntry){
1313
1301
  /* Skip "." and ".." */
1314
1302
  if(direntry->d_name[0] == '.'
1321
1309
}
1322
1310
 
1323
1311
/* Is this directory entry a runnable program? */
1324
 
__attribute__((nonnull, warn_unused_result))
1325
1312
int runnable_hook(const struct dirent *direntry){
1326
1313
  int ret;
1327
1314
  size_t sret;
1335
1322
  sret = strspn(direntry->d_name, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1336
1323
                "abcdefghijklmnopqrstuvwxyz"
1337
1324
                "0123456789"
1338
 
                "_.-");
 
1325
                "_-");
1339
1326
  if((direntry->d_name)[sret] != '\0'){
1340
1327
    /* Contains non-allowed characters */
1341
1328
    if(debug){
1359
1346
    }
1360
1347
    return 0;
1361
1348
  }
1362
 
  free(fullname);
1363
1349
  if(not (S_ISREG(st.st_mode))){
1364
1350
    /* Not a regular file */
1365
1351
    if(debug){
1383
1369
  return 1;
1384
1370
}
1385
1371
 
1386
 
__attribute__((nonnull, warn_unused_result))
1387
1372
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval,
1388
1373
                            mandos_context *mc){
1389
1374
  int ret;
1393
1378
  
1394
1379
  while(true){
1395
1380
    if(mc->current_server == NULL){
1396
 
      if(debug){
 
1381
      if (debug){
1397
1382
        fprintf_plus(stderr, "Wait until first server is found."
1398
1383
                     " No timeout!\n");
1399
1384
      }
1400
1385
      ret = avahi_simple_poll_iterate(s, -1);
1401
1386
    } else {
1402
 
      if(debug){
 
1387
      if (debug){
1403
1388
        fprintf_plus(stderr, "Check current_server if we should run"
1404
1389
                     " it, or wait\n");
1405
1390
      }
1422
1407
                     - ((intmax_t)waited_time.tv_sec * 1000))
1423
1408
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
1424
1409
      
1425
 
      if(debug){
 
1410
      if (debug){
1426
1411
        fprintf_plus(stderr, "Blocking for %" PRIdMAX " ms\n",
1427
1412
                     block_time);
1428
1413
      }
1450
1435
      ret = avahi_simple_poll_iterate(s, (int)block_time);
1451
1436
    }
1452
1437
    if(ret != 0){
1453
 
      if(ret > 0 or errno != EINTR){
 
1438
      if (ret > 0 or errno != EINTR){
1454
1439
        return (ret != 1) ? ret : 0;
1455
1440
      }
1456
1441
    }
1458
1443
}
1459
1444
 
1460
1445
/* Set effective uid to 0, return errno */
1461
 
__attribute__((warn_unused_result))
1462
1446
error_t raise_privileges(void){
1463
1447
  error_t old_errno = errno;
1464
1448
  error_t ret_errno = 0;
1471
1455
}
1472
1456
 
1473
1457
/* Set effective and real user ID to 0.  Return errno. */
1474
 
__attribute__((warn_unused_result))
1475
1458
error_t raise_privileges_permanently(void){
1476
1459
  error_t old_errno = errno;
1477
1460
  error_t ret_errno = raise_privileges();
1488
1471
}
1489
1472
 
1490
1473
/* Set effective user ID to unprivileged saved user ID */
1491
 
__attribute__((warn_unused_result))
1492
1474
error_t lower_privileges(void){
1493
1475
  error_t old_errno = errno;
1494
1476
  error_t ret_errno = 0;
1501
1483
}
1502
1484
 
1503
1485
/* Lower privileges permanently */
1504
 
__attribute__((warn_unused_result))
1505
1486
error_t lower_privileges_permanently(void){
1506
1487
  error_t old_errno = errno;
1507
1488
  error_t ret_errno = 0;
1513
1494
  return ret_errno;
1514
1495
}
1515
1496
 
1516
 
__attribute__((nonnull))
1517
 
void run_network_hooks(const char *mode, const char *interface,
 
1497
bool run_network_hooks(const char *mode, const char *interface,
1518
1498
                       const float delay){
1519
1499
  struct dirent **direntries;
1520
1500
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1548
1528
      if(hook_pid == 0){
1549
1529
        /* Child */
1550
1530
        /* Raise privileges */
1551
 
        if(raise_privileges_permanently() != 0){
1552
 
          perror_plus("Failed to raise privileges");
1553
 
          _exit(EX_NOPERM);
1554
 
        }
 
1531
        raise_privileges_permanently();
1555
1532
        /* Set group */
1556
1533
        errno = 0;
1557
1534
        ret = setgid(0);
1558
1535
        if(ret == -1){
1559
1536
          perror_plus("setgid");
1560
 
          _exit(EX_NOPERM);
1561
1537
        }
1562
1538
        /* Reset supplementary groups */
1563
1539
        errno = 0;
1564
1540
        ret = setgroups(0, NULL);
1565
1541
        if(ret == -1){
1566
1542
          perror_plus("setgroups");
1567
 
          _exit(EX_NOPERM);
1568
 
        }
1569
 
        ret = dup2(devnull, STDIN_FILENO);
1570
 
        if(ret == -1){
1571
 
          perror_plus("dup2(devnull, STDIN_FILENO)");
1572
 
          _exit(EX_OSERR);
1573
 
        }
1574
 
        ret = close(devnull);
1575
 
        if(ret == -1){
1576
 
          perror_plus("close");
1577
 
          _exit(EX_OSERR);
1578
 
        }
1579
 
        ret = dup2(STDERR_FILENO, STDOUT_FILENO);
1580
 
        if(ret == -1){
1581
 
          perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
1582
 
          _exit(EX_OSERR);
1583
 
        }
 
1543
        }
 
1544
        dup2(devnull, STDIN_FILENO);
 
1545
        close(devnull);
 
1546
        dup2(STDERR_FILENO, STDOUT_FILENO);
1584
1547
        ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1585
1548
        if(ret == -1){
1586
1549
          perror_plus("setenv");
1602
1565
          _exit(EX_OSERR);
1603
1566
        }
1604
1567
        char *delaystring;
1605
 
        ret = asprintf(&delaystring, "%f", (double)delay);
 
1568
        ret = asprintf(&delaystring, "%f", delay);
1606
1569
        if(ret == -1){
1607
1570
          perror_plus("asprintf");
1608
1571
          _exit(EX_OSERR);
1661
1624
    }
1662
1625
    close(devnull);
1663
1626
  }
 
1627
  return true;
1664
1628
}
1665
1629
 
1666
 
__attribute__((nonnull, warn_unused_result))
1667
1630
error_t bring_up_interface(const char *const interface,
1668
1631
                           const float delay){
 
1632
  int sd = -1;
1669
1633
  error_t old_errno = errno;
1670
 
  int ret;
 
1634
  error_t ret_errno = 0;
 
1635
  int ret, ret_setflags;
1671
1636
  struct ifreq network;
1672
1637
  unsigned int if_index = if_nametoindex(interface);
1673
1638
  if(if_index == 0){
1682
1647
  }
1683
1648
  
1684
1649
  if(not interface_is_up(interface)){
1685
 
    error_t ret_errno = 0, ioctl_errno = 0;
1686
 
    if(not get_flags(interface, &network)){
 
1650
    if(not get_flags(interface, &network) and debug){
1687
1651
      ret_errno = errno;
1688
1652
      fprintf_plus(stderr, "Failed to get flags for interface "
1689
1653
                   "\"%s\"\n", interface);
1690
 
      errno = old_errno;
1691
1654
      return ret_errno;
1692
1655
    }
1693
 
    network.ifr_flags |= IFF_UP; /* set flag */
 
1656
    network.ifr_flags |= IFF_UP;
1694
1657
    
1695
 
    int sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1696
 
    if(sd == -1){
 
1658
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
1659
    if(sd < 0){
1697
1660
      ret_errno = errno;
1698
1661
      perror_plus("socket");
1699
1662
      errno = old_errno;
1700
1663
      return ret_errno;
1701
1664
    }
1702
 
    
 
1665
  
1703
1666
    if(quit_now){
1704
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1705
 
      if(ret == -1){
1706
 
        perror_plus("close");
1707
 
      }
 
1667
      close(sd);
1708
1668
      errno = old_errno;
1709
1669
      return EINTR;
1710
1670
    }
1714
1674
                   interface);
1715
1675
    }
1716
1676
    
1717
 
    /* Raise privileges */
1718
 
    ret_errno = raise_privileges();
1719
 
    if(ret_errno != 0){
1720
 
      perror_plus("Failed to raise privileges");
1721
 
    }
 
1677
    /* Raise priviliges */
 
1678
    raise_privileges();
1722
1679
    
1723
1680
#ifdef __linux__
1724
 
    int ret_linux;
1725
 
    bool restore_loglevel = false;
1726
 
    if(ret_errno == 0){
1727
 
      /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1728
 
         messages about the network interface to mess up the prompt */
1729
 
      ret_linux = klogctl(8, NULL, 5);
1730
 
      if(ret_linux == -1){
1731
 
        perror_plus("klogctl");
1732
 
      } else {
1733
 
        restore_loglevel = true;
1734
 
      }
 
1681
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
 
1682
       messages about the network interface to mess up the prompt */
 
1683
    int ret_linux = klogctl(8, NULL, 5);
 
1684
    bool restore_loglevel = true;
 
1685
    if(ret_linux == -1){
 
1686
      restore_loglevel = false;
 
1687
      perror_plus("klogctl");
1735
1688
    }
1736
1689
#endif  /* __linux__ */
1737
 
    int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1738
 
    ioctl_errno = errno;
 
1690
    ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
 
1691
    ret_errno = errno;
1739
1692
#ifdef __linux__
1740
1693
    if(restore_loglevel){
1741
1694
      ret_linux = klogctl(7, NULL, 0);
1745
1698
    }
1746
1699
#endif  /* __linux__ */
1747
1700
    
1748
 
    /* If raise_privileges() succeeded above */
1749
 
    if(ret_errno == 0){
1750
 
      /* Lower privileges */
1751
 
      ret_errno = lower_privileges();
1752
 
      if(ret_errno != 0){
1753
 
        errno = ret_errno;
1754
 
        perror_plus("Failed to lower privileges");
1755
 
      }
1756
 
    }
 
1701
    /* Lower privileges */
 
1702
    lower_privileges();
1757
1703
    
1758
1704
    /* Close the socket */
1759
1705
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
1762
1708
    }
1763
1709
    
1764
1710
    if(ret_setflags == -1){
1765
 
      errno = ioctl_errno;
 
1711
      errno = ret_errno;
1766
1712
      perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1767
1713
      errno = old_errno;
1768
 
      return ioctl_errno;
 
1714
      return ret_errno;
1769
1715
    }
1770
1716
  } else if(debug){
1771
1717
    fprintf_plus(stderr, "Interface \"%s\" is already up; good\n",
1789
1735
  return 0;
1790
1736
}
1791
1737
 
1792
 
__attribute__((nonnull, warn_unused_result))
1793
1738
error_t take_down_interface(const char *const interface){
1794
1739
  error_t old_errno = errno;
1795
1740
  struct ifreq network;
1800
1745
    return ENXIO;
1801
1746
  }
1802
1747
  if(interface_is_up(interface)){
1803
 
    error_t ret_errno = 0, ioctl_errno = 0;
 
1748
    error_t ret_errno = 0;
1804
1749
    if(not get_flags(interface, &network) and debug){
1805
1750
      ret_errno = errno;
1806
1751
      fprintf_plus(stderr, "Failed to get flags for interface "
1807
1752
                   "\"%s\"\n", interface);
1808
 
      errno = old_errno;
1809
1753
      return ret_errno;
1810
1754
    }
1811
1755
    network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1812
1756
    
1813
1757
    int sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1814
 
    if(sd == -1){
 
1758
    if(sd < 0){
1815
1759
      ret_errno = errno;
1816
1760
      perror_plus("socket");
1817
1761
      errno = old_errno;
1823
1767
                   interface);
1824
1768
    }
1825
1769
    
1826
 
    /* Raise privileges */
1827
 
    ret_errno = raise_privileges();
1828
 
    if(ret_errno != 0){
1829
 
      perror_plus("Failed to raise privileges");
1830
 
    }
 
1770
    /* Raise priviliges */
 
1771
    raise_privileges();
1831
1772
    
1832
1773
    int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1833
 
    ioctl_errno = errno;
 
1774
    ret_errno = errno;
1834
1775
    
1835
 
    /* If raise_privileges() succeeded above */
1836
 
    if(ret_errno == 0){
1837
 
      /* Lower privileges */
1838
 
      ret_errno = lower_privileges();
1839
 
      if(ret_errno != 0){
1840
 
        errno = ret_errno;
1841
 
        perror_plus("Failed to lower privileges");
1842
 
      }
1843
 
    }
 
1776
    /* Lower privileges */
 
1777
    lower_privileges();
1844
1778
    
1845
1779
    /* Close the socket */
1846
1780
    int ret = (int)TEMP_FAILURE_RETRY(close(sd));
1849
1783
    }
1850
1784
    
1851
1785
    if(ret_setflags == -1){
1852
 
      errno = ioctl_errno;
 
1786
      errno = ret_errno;
1853
1787
      perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
1854
1788
      errno = old_errno;
1855
 
      return ioctl_errno;
 
1789
      return ret_errno;
1856
1790
    }
1857
1791
  } else if(debug){
1858
1792
    fprintf_plus(stderr, "Interface \"%s\" is already down; odd\n",
1866
1800
int main(int argc, char *argv[]){
1867
1801
  mandos_context mc = { .server = NULL, .dh_bits = 1024,
1868
1802
                        .priority = "SECURE256:!CTYPE-X.509:"
1869
 
                        "+CTYPE-OPENPGP", .current_server = NULL,
 
1803
                        "+CTYPE-OPENPGP", .current_server = NULL, 
1870
1804
                        .interfaces = NULL, .interfaces_size = 0 };
1871
1805
  AvahiSServiceBrowser *sb = NULL;
1872
1806
  error_t ret_errno;
1876
1810
  int exitcode = EXIT_SUCCESS;
1877
1811
  char *interfaces_to_take_down = NULL;
1878
1812
  size_t interfaces_to_take_down_size = 0;
1879
 
  char run_tempdir[] = "/run/tmp/mandosXXXXXX";
1880
 
  char old_tempdir[] = "/tmp/mandosXXXXXX";
1881
 
  char *tempdir = NULL;
 
1813
  char tempdir[] = "/tmp/mandosXXXXXX";
 
1814
  bool tempdir_created = false;
1882
1815
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1883
1816
  const char *seckey = PATHDIR "/" SECKEY;
1884
1817
  const char *pubkey = PATHDIR "/" PUBKEY;
2066
1999
    /* Work around Debian bug #633582:
2067
2000
       <http://bugs.debian.org/633582> */
2068
2001
    
2069
 
    /* Re-raise privileges */
2070
 
    ret_errno = raise_privileges();
2071
 
    if(ret_errno != 0){
2072
 
      errno = ret_errno;
2073
 
      perror_plus("Failed to raise privileges");
2074
 
    } else {
 
2002
    /* Re-raise priviliges */
 
2003
    if(raise_privileges() == 0){
2075
2004
      struct stat st;
2076
2005
      
2077
2006
      if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
2117
2046
      }
2118
2047
    
2119
2048
      /* Lower privileges */
2120
 
      ret_errno = lower_privileges();
2121
 
      if(ret_errno != 0){
2122
 
        errno = ret_errno;
2123
 
        perror_plus("Failed to lower privileges");
2124
 
      }
 
2049
      lower_privileges();
2125
2050
    }
2126
2051
  }
2127
2052
  
2153
2078
      memcpy(interfaces_hooks, mc.interfaces, mc.interfaces_size);
2154
2079
      argz_stringify(interfaces_hooks, mc.interfaces_size, (int)',');
2155
2080
    }
2156
 
    run_network_hooks("start", interfaces_hooks != NULL ?
2157
 
                      interfaces_hooks : "", delay);
 
2081
    if(not run_network_hooks("start", interfaces_hooks != NULL ?
 
2082
                             interfaces_hooks : "", delay)){
 
2083
      goto end;
 
2084
    }
2158
2085
  }
2159
2086
  
2160
2087
  if(not debug){
2288
2215
        break;
2289
2216
      }
2290
2217
      bool interface_was_up = interface_is_up(interface);
2291
 
      errno = bring_up_interface(interface, delay);
 
2218
      ret = bring_up_interface(interface, delay);
2292
2219
      if(not interface_was_up){
2293
 
        if(errno != 0){
 
2220
        if(ret != 0){
 
2221
          errno = ret;
2294
2222
          perror_plus("Failed to bring up interface");
2295
2223
        } else {
2296
 
          errno = argz_add(&interfaces_to_take_down,
2297
 
                           &interfaces_to_take_down_size,
2298
 
                           interface);
2299
 
          if(errno != 0){
 
2224
          ret_errno = argz_add(&interfaces_to_take_down,
 
2225
                               &interfaces_to_take_down_size,
 
2226
                               interface);
 
2227
          if(ret_errno != 0){
 
2228
            errno = ret_errno;
2300
2229
            perror_plus("argz_add");
2301
2230
          }
2302
2231
        }
2333
2262
    goto end;
2334
2263
  }
2335
2264
  
2336
 
  /* Try /run/tmp before /tmp */
2337
 
  tempdir = mkdtemp(run_tempdir);
2338
 
  if(tempdir == NULL and errno == ENOENT){
2339
 
      if(debug){
2340
 
        fprintf_plus(stderr, "Tempdir %s did not work, trying %s\n",
2341
 
                     run_tempdir, old_tempdir);
2342
 
      }
2343
 
      tempdir = mkdtemp(old_tempdir);
2344
 
  }
2345
 
  if(tempdir == NULL){
 
2265
  if(mkdtemp(tempdir) == NULL){
2346
2266
    perror_plus("mkdtemp");
2347
2267
    goto end;
2348
2268
  }
 
2269
  tempdir_created = true;
2349
2270
  
2350
2271
  if(quit_now){
2351
2272
    goto end;
2426
2347
      sleep((unsigned int)retry_interval);
2427
2348
    }
2428
2349
    
2429
 
    if(not quit_now){
 
2350
    if (not quit_now){
2430
2351
      exitcode = EXIT_SUCCESS;
2431
2352
    }
2432
2353
    
2487
2408
  if(debug){
2488
2409
    fprintf_plus(stderr, "Starting Avahi loop search\n");
2489
2410
  }
2490
 
  
 
2411
 
2491
2412
  ret = avahi_loop_with_timeout(simple_poll,
2492
2413
                                (int)(retry_interval * 1000), &mc);
2493
2414
  if(debug){
2534
2455
    }
2535
2456
  }
2536
2457
  
2537
 
  /* Re-raise privileges */
 
2458
  /* Re-raise priviliges */
2538
2459
  {
2539
 
    ret_errno = raise_privileges();
2540
 
    if(ret_errno != 0){
2541
 
      perror_plus("Failed to raise privileges");
2542
 
    } else {
2543
 
      
2544
 
      /* Run network hooks */
2545
 
      run_network_hooks("stop", interfaces_hooks != NULL ?
2546
 
                        interfaces_hooks : "", delay);
2547
 
      
2548
 
      /* Take down the network interfaces which were brought up */
2549
 
      {
2550
 
        char *interface = NULL;
2551
 
        while((interface=argz_next(interfaces_to_take_down,
2552
 
                                   interfaces_to_take_down_size,
2553
 
                                   interface))){
2554
 
          ret_errno = take_down_interface(interface);
2555
 
          if(ret_errno != 0){
2556
 
            errno = ret_errno;
2557
 
            perror_plus("Failed to take down interface");
2558
 
          }
2559
 
        }
2560
 
        if(debug and (interfaces_to_take_down == NULL)){
2561
 
          fprintf_plus(stderr, "No interfaces needed to be taken"
2562
 
                       " down\n");
2563
 
        }
2564
 
      }
2565
 
    }
2566
 
    
2567
 
    ret_errno = lower_privileges_permanently();
2568
 
    if(ret_errno != 0){
2569
 
      perror_plus("Failed to lower privileges permanently");
2570
 
    }
 
2460
    raise_privileges();
 
2461
    
 
2462
    /* Run network hooks */
 
2463
    run_network_hooks("stop", interfaces_hooks != NULL ?
 
2464
                      interfaces_hooks : "", delay);
 
2465
    
 
2466
    /* Take down the network interfaces which were brought up */
 
2467
    {
 
2468
      char *interface = NULL;
 
2469
      while((interface=argz_next(interfaces_to_take_down,
 
2470
                                 interfaces_to_take_down_size,
 
2471
                                 interface))){
 
2472
        ret_errno = take_down_interface(interface);
 
2473
        if(ret_errno != 0){
 
2474
          errno = ret_errno;
 
2475
          perror_plus("Failed to take down interface");
 
2476
        }
 
2477
      }
 
2478
      if(debug and (interfaces_to_take_down == NULL)){
 
2479
        fprintf_plus(stderr, "No interfaces needed to be taken"
 
2480
                     " down\n");
 
2481
      }
 
2482
    }
 
2483
    
 
2484
    lower_privileges_permanently();
2571
2485
  }
2572
2486
  
2573
2487
  free(interfaces_to_take_down);
2574
2488
  free(interfaces_hooks);
2575
2489
  
2576
2490
  /* Removes the GPGME temp directory and all files inside */
2577
 
  if(tempdir != NULL){
 
2491
  if(tempdir_created){
2578
2492
    struct dirent **direntries = NULL;
2579
2493
    struct dirent *direntry = NULL;
2580
2494
    int numentries = scandir(tempdir, &direntries, notdotentries,
2581
2495
                             alphasort);
2582
 
    if(numentries > 0){
 
2496
    if (numentries > 0){
2583
2497
      for(int i = 0; i < numentries; i++){
2584
2498
        direntry = direntries[i];
2585
2499
        char *fullname = NULL;
2597
2511
        free(fullname);
2598
2512
      }
2599
2513
    }
2600
 
    
 
2514
 
2601
2515
    /* need to clean even if 0 because man page doesn't specify */
2602
2516
    free(direntries);
2603
 
    if(numentries == -1){
 
2517
    if (numentries == -1){
2604
2518
      perror_plus("scandir");
2605
2519
    }
2606
2520
    ret = rmdir(tempdir);