/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-05-12 21:02:38 UTC
  • Revision ID: teddy@recompile.se-20140512210238-t6o8b1ieeojrs0xl
Do not run self-tests when building arch-indep Debian package.

* debian/rules (override_dh_auto_test-arch): New; does nothing.

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
183
183
  perror(print_text);
184
184
}
185
185
 
186
 
__attribute__((format (gnu_printf, 2, 3)))
 
186
__attribute__((format (gnu_printf, 2, 3), nonnull))
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))
201
202
size_t incbuffer(char **buffer, size_t buffer_length,
202
203
                 size_t buffer_capacity){
203
204
  if(buffer_length + BUFFER_SIZE > buffer_capacity){
216
217
}
217
218
 
218
219
/* Add server to set of servers to retry periodically */
 
220
__attribute__((nonnull, warn_unused_result))
219
221
bool add_server(const char *ip, in_port_t port, AvahiIfIndex if_index,
220
222
                int af, server **current_server){
221
223
  int ret;
242
244
    new_server->next = new_server;
243
245
    new_server->prev = new_server;
244
246
    *current_server = new_server;
245
 
  /* Place the new server last in the list */
246
247
  } else {
 
248
    /* Place the new server last in the list */
247
249
    new_server->next = *current_server;
248
250
    new_server->prev = (*current_server)->prev;
249
251
    new_server->prev->next = new_server;
255
257
/* 
256
258
 * Initialize GPGME.
257
259
 */
258
 
static bool init_gpgme(const char *seckey, const char *pubkey,
259
 
                       const char *tempdir, mandos_context *mc){
 
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){
260
265
  gpgme_error_t rc;
261
266
  gpgme_engine_info_t engine_info;
262
267
  
263
268
  /*
264
269
   * Helper function to insert pub and seckey to the engine keyring.
265
270
   */
266
 
  bool import_key(const char *filename){
 
271
  bool import_key(const char * const filename){
267
272
    int ret;
268
273
    int fd;
269
274
    gpgme_data_t pgp_data;
350
355
 * Decrypt OpenPGP data.
351
356
 * Returns -1 on error
352
357
 */
 
358
__attribute__((nonnull, warn_unused_result))
353
359
static ssize_t pgp_packet_decrypt(const char *cryptotext,
354
360
                                  size_t crypto_size,
355
361
                                  char **plaintext,
475
481
  return plaintext_length;
476
482
}
477
483
 
478
 
static const char * safer_gnutls_strerror(int value){
 
484
__attribute__((warn_unused_result))
 
485
static const char *safer_gnutls_strerror(int value){
479
486
  const char *ret = gnutls_strerror(value);
480
487
  if(ret == NULL)
481
488
    ret = "(unknown)";
483
490
}
484
491
 
485
492
/* GnuTLS log function callback */
 
493
__attribute__((nonnull))
486
494
static void debuggnutls(__attribute__((unused)) int level,
487
495
                        const char* string){
488
496
  fprintf_plus(stderr, "GnuTLS: %s", string);
489
497
}
490
498
 
 
499
__attribute__((nonnull, warn_unused_result))
491
500
static int init_gnutls_global(const char *pubkeyfilename,
492
501
                              const char *seckeyfilename,
493
502
                              mandos_context *mc){
567
576
  return -1;
568
577
}
569
578
 
 
579
__attribute__((nonnull, warn_unused_result))
570
580
static int init_gnutls_session(gnutls_session_t *session,
571
581
                               mandos_context *mc){
572
582
  int ret;
629
639
                      __attribute__((unused)) const char *txt){}
630
640
 
631
641
/* Called when a Mandos server is found */
 
642
__attribute__((nonnull, warn_unused_result))
632
643
static int start_mandos_communication(const char *ip, in_port_t port,
633
644
                                      AvahiIfIndex if_index,
634
645
                                      int af, mandos_context *mc){
740
751
    goto mandos_end;
741
752
  }
742
753
  if(af == AF_INET6){
743
 
    ((struct sockaddr_in6 *)&to)->sin6_port = htons(port);    
 
754
    ((struct sockaddr_in6 *)&to)->sin6_port = htons(port);
744
755
    if(IN6_IS_ADDR_LINKLOCAL
745
756
       (&((struct sockaddr_in6 *)&to)->sin6_addr)){
746
757
      if(if_index == AVAHI_IF_UNSPEC){
1030
1041
  return retval;
1031
1042
}
1032
1043
 
 
1044
__attribute__((nonnull))
1033
1045
static void resolve_callback(AvahiSServiceResolver *r,
1034
1046
                             AvahiIfIndex interface,
1035
1047
                             AvahiProtocol proto,
1043
1055
                             AVAHI_GCC_UNUSED AvahiStringList *txt,
1044
1056
                             AVAHI_GCC_UNUSED AvahiLookupResultFlags
1045
1057
                             flags,
1046
 
                             void* mc){
 
1058
                             void *mc){
1047
1059
  if(r == NULL){
1048
1060
    return;
1049
1061
  }
1102
1114
                            const char *domain,
1103
1115
                            AVAHI_GCC_UNUSED AvahiLookupResultFlags
1104
1116
                            flags,
1105
 
                            void* mc){
 
1117
                            void *mc){
1106
1118
  if(b == NULL){
1107
1119
    return;
1108
1120
  }
1168
1180
  errno = old_errno;
1169
1181
}
1170
1182
 
 
1183
__attribute__((nonnull, warn_unused_result))
1171
1184
bool get_flags(const char *ifname, struct ifreq *ifr){
1172
1185
  int ret;
1173
1186
  error_t ret_errno;
1192
1205
  return true;
1193
1206
}
1194
1207
 
 
1208
__attribute__((nonnull, warn_unused_result))
1195
1209
bool good_flags(const char *ifname, const struct ifreq *ifr){
1196
1210
  
1197
1211
  /* Reject the loopback device */
1239
1253
 * corresponds to an acceptable network device.
1240
1254
 * (This function is passed to scandir(3) as a filter function.)
1241
1255
 */
 
1256
__attribute__((nonnull, warn_unused_result))
1242
1257
int good_interface(const struct dirent *if_entry){
1243
1258
  if(if_entry->d_name[0] == '.'){
1244
1259
    return 0;
1262
1277
/* 
1263
1278
 * This function determines if a network interface is up.
1264
1279
 */
 
1280
__attribute__((nonnull, warn_unused_result))
1265
1281
bool interface_is_up(const char *interface){
1266
1282
  struct ifreq ifr;
1267
1283
  if(not get_flags(interface, &ifr)){
1278
1294
/* 
1279
1295
 * This function determines if a network interface is running
1280
1296
 */
 
1297
__attribute__((nonnull, warn_unused_result))
1281
1298
bool interface_is_running(const char *interface){
1282
1299
  struct ifreq ifr;
1283
1300
  if(not get_flags(interface, &ifr)){
1291
1308
  return (bool)(ifr.ifr_flags & IFF_RUNNING);
1292
1309
}
1293
1310
 
 
1311
__attribute__((nonnull, pure, warn_unused_result))
1294
1312
int notdotentries(const struct dirent *direntry){
1295
1313
  /* Skip "." and ".." */
1296
1314
  if(direntry->d_name[0] == '.'
1303
1321
}
1304
1322
 
1305
1323
/* Is this directory entry a runnable program? */
 
1324
__attribute__((nonnull, warn_unused_result))
1306
1325
int runnable_hook(const struct dirent *direntry){
1307
1326
  int ret;
1308
1327
  size_t sret;
1363
1382
  return 1;
1364
1383
}
1365
1384
 
 
1385
__attribute__((nonnull, warn_unused_result))
1366
1386
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval,
1367
1387
                            mandos_context *mc){
1368
1388
  int ret;
1437
1457
}
1438
1458
 
1439
1459
/* Set effective uid to 0, return errno */
 
1460
__attribute__((warn_unused_result))
1440
1461
error_t raise_privileges(void){
1441
1462
  error_t old_errno = errno;
1442
1463
  error_t ret_errno = 0;
1449
1470
}
1450
1471
 
1451
1472
/* Set effective and real user ID to 0.  Return errno. */
 
1473
__attribute__((warn_unused_result))
1452
1474
error_t raise_privileges_permanently(void){
1453
1475
  error_t old_errno = errno;
1454
1476
  error_t ret_errno = raise_privileges();
1465
1487
}
1466
1488
 
1467
1489
/* Set effective user ID to unprivileged saved user ID */
 
1490
__attribute__((warn_unused_result))
1468
1491
error_t lower_privileges(void){
1469
1492
  error_t old_errno = errno;
1470
1493
  error_t ret_errno = 0;
1477
1500
}
1478
1501
 
1479
1502
/* Lower privileges permanently */
 
1503
__attribute__((warn_unused_result))
1480
1504
error_t lower_privileges_permanently(void){
1481
1505
  error_t old_errno = errno;
1482
1506
  error_t ret_errno = 0;
1488
1512
  return ret_errno;
1489
1513
}
1490
1514
 
1491
 
bool run_network_hooks(const char *mode, const char *interface,
 
1515
__attribute__((nonnull))
 
1516
void run_network_hooks(const char *mode, const char *interface,
1492
1517
                       const float delay){
1493
1518
  struct dirent **direntries;
1494
1519
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1522
1547
      if(hook_pid == 0){
1523
1548
        /* Child */
1524
1549
        /* Raise privileges */
1525
 
        raise_privileges_permanently();
 
1550
        if(raise_privileges_permanently() != 0){
 
1551
          perror_plus("Failed to raise privileges");
 
1552
          _exit(EX_NOPERM);
 
1553
        }
1526
1554
        /* Set group */
1527
1555
        errno = 0;
1528
1556
        ret = setgid(0);
1529
1557
        if(ret == -1){
1530
1558
          perror_plus("setgid");
 
1559
          _exit(EX_NOPERM);
1531
1560
        }
1532
1561
        /* Reset supplementary groups */
1533
1562
        errno = 0;
1534
1563
        ret = setgroups(0, NULL);
1535
1564
        if(ret == -1){
1536
1565
          perror_plus("setgroups");
1537
 
        }
1538
 
        dup2(devnull, STDIN_FILENO);
1539
 
        close(devnull);
1540
 
        dup2(STDERR_FILENO, STDOUT_FILENO);
 
1566
          _exit(EX_NOPERM);
 
1567
        }
 
1568
        ret = dup2(devnull, STDIN_FILENO);
 
1569
        if(ret == -1){
 
1570
          perror_plus("dup2(devnull, STDIN_FILENO)");
 
1571
          _exit(EX_OSERR);
 
1572
        }
 
1573
        ret = close(devnull);
 
1574
        if(ret == -1){
 
1575
          perror_plus("close");
 
1576
          _exit(EX_OSERR);
 
1577
        }
 
1578
        ret = dup2(STDERR_FILENO, STDOUT_FILENO);
 
1579
        if(ret == -1){
 
1580
          perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
 
1581
          _exit(EX_OSERR);
 
1582
        }
1541
1583
        ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1542
1584
        if(ret == -1){
1543
1585
          perror_plus("setenv");
1559
1601
          _exit(EX_OSERR);
1560
1602
        }
1561
1603
        char *delaystring;
1562
 
        ret = asprintf(&delaystring, "%f", delay);
 
1604
        ret = asprintf(&delaystring, "%f", (double)delay);
1563
1605
        if(ret == -1){
1564
1606
          perror_plus("asprintf");
1565
1607
          _exit(EX_OSERR);
1618
1660
    }
1619
1661
    close(devnull);
1620
1662
  }
1621
 
  return true;
1622
1663
}
1623
1664
 
 
1665
__attribute__((nonnull, warn_unused_result))
1624
1666
error_t bring_up_interface(const char *const interface,
1625
1667
                           const float delay){
1626
 
  int sd = -1;
1627
1668
  error_t old_errno = errno;
1628
 
  error_t ret_errno = 0;
1629
 
  int ret, ret_setflags;
 
1669
  int ret;
1630
1670
  struct ifreq network;
1631
1671
  unsigned int if_index = if_nametoindex(interface);
1632
1672
  if(if_index == 0){
1641
1681
  }
1642
1682
  
1643
1683
  if(not interface_is_up(interface)){
1644
 
    if(not get_flags(interface, &network) and debug){
 
1684
    error_t ret_errno = 0, ioctl_errno = 0;
 
1685
    if(not get_flags(interface, &network)){
1645
1686
      ret_errno = errno;
1646
1687
      fprintf_plus(stderr, "Failed to get flags for interface "
1647
1688
                   "\"%s\"\n", interface);
 
1689
      errno = old_errno;
1648
1690
      return ret_errno;
1649
1691
    }
1650
 
    network.ifr_flags |= IFF_UP;
 
1692
    network.ifr_flags |= IFF_UP; /* set flag */
1651
1693
    
1652
 
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1653
 
    if(sd < 0){
 
1694
    int sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
1695
    if(sd == -1){
1654
1696
      ret_errno = errno;
1655
1697
      perror_plus("socket");
1656
1698
      errno = old_errno;
1657
1699
      return ret_errno;
1658
1700
    }
1659
 
  
 
1701
    
1660
1702
    if(quit_now){
1661
 
      close(sd);
 
1703
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1704
      if(ret == -1){
 
1705
        perror_plus("close");
 
1706
      }
1662
1707
      errno = old_errno;
1663
1708
      return EINTR;
1664
1709
    }
1668
1713
                   interface);
1669
1714
    }
1670
1715
    
1671
 
    /* Raise priviliges */
1672
 
    raise_privileges();
 
1716
    /* Raise privileges */
 
1717
    ret_errno = raise_privileges();
 
1718
    if(ret_errno != 0){
 
1719
      perror_plus("Failed to raise privileges");
 
1720
    }
1673
1721
    
1674
1722
#ifdef __linux__
1675
 
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1676
 
       messages about the network interface to mess up the prompt */
1677
 
    int ret_linux = klogctl(8, NULL, 5);
1678
 
    bool restore_loglevel = true;
1679
 
    if(ret_linux == -1){
1680
 
      restore_loglevel = false;
1681
 
      perror_plus("klogctl");
 
1723
    int ret_linux;
 
1724
    bool restore_loglevel = false;
 
1725
    if(ret_errno == 0){
 
1726
      /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
 
1727
         messages about the network interface to mess up the prompt */
 
1728
      ret_linux = klogctl(8, NULL, 5);
 
1729
      if(ret_linux == -1){
 
1730
        perror_plus("klogctl");
 
1731
      } else {
 
1732
        restore_loglevel = true;
 
1733
      }
1682
1734
    }
1683
1735
#endif  /* __linux__ */
1684
 
    ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1685
 
    ret_errno = errno;
 
1736
    int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
 
1737
    ioctl_errno = errno;
1686
1738
#ifdef __linux__
1687
1739
    if(restore_loglevel){
1688
1740
      ret_linux = klogctl(7, NULL, 0);
1692
1744
    }
1693
1745
#endif  /* __linux__ */
1694
1746
    
1695
 
    /* Lower privileges */
1696
 
    lower_privileges();
 
1747
    /* If raise_privileges() succeeded above */
 
1748
    if(ret_errno == 0){
 
1749
      /* Lower privileges */
 
1750
      ret_errno = lower_privileges();
 
1751
      if(ret_errno != 0){
 
1752
        errno = ret_errno;
 
1753
        perror_plus("Failed to lower privileges");
 
1754
      }
 
1755
    }
1697
1756
    
1698
1757
    /* Close the socket */
1699
1758
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
1702
1761
    }
1703
1762
    
1704
1763
    if(ret_setflags == -1){
1705
 
      errno = ret_errno;
 
1764
      errno = ioctl_errno;
1706
1765
      perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1707
1766
      errno = old_errno;
1708
 
      return ret_errno;
 
1767
      return ioctl_errno;
1709
1768
    }
1710
1769
  } else if(debug){
1711
1770
    fprintf_plus(stderr, "Interface \"%s\" is already up; good\n",
1729
1788
  return 0;
1730
1789
}
1731
1790
 
 
1791
__attribute__((nonnull, warn_unused_result))
1732
1792
error_t take_down_interface(const char *const interface){
1733
1793
  error_t old_errno = errno;
1734
1794
  struct ifreq network;
1739
1799
    return ENXIO;
1740
1800
  }
1741
1801
  if(interface_is_up(interface)){
1742
 
    error_t ret_errno = 0;
 
1802
    error_t ret_errno = 0, ioctl_errno = 0;
1743
1803
    if(not get_flags(interface, &network) and debug){
1744
1804
      ret_errno = errno;
1745
1805
      fprintf_plus(stderr, "Failed to get flags for interface "
1746
1806
                   "\"%s\"\n", interface);
 
1807
      errno = old_errno;
1747
1808
      return ret_errno;
1748
1809
    }
1749
1810
    network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1750
1811
    
1751
1812
    int sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1752
 
    if(sd < 0){
 
1813
    if(sd == -1){
1753
1814
      ret_errno = errno;
1754
1815
      perror_plus("socket");
1755
1816
      errno = old_errno;
1761
1822
                   interface);
1762
1823
    }
1763
1824
    
1764
 
    /* Raise priviliges */
1765
 
    raise_privileges();
 
1825
    /* Raise privileges */
 
1826
    ret_errno = raise_privileges();
 
1827
    if(ret_errno != 0){
 
1828
      perror_plus("Failed to raise privileges");
 
1829
    }
1766
1830
    
1767
1831
    int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1768
 
    ret_errno = errno;
 
1832
    ioctl_errno = errno;
1769
1833
    
1770
 
    /* Lower privileges */
1771
 
    lower_privileges();
 
1834
    /* If raise_privileges() succeeded above */
 
1835
    if(ret_errno == 0){
 
1836
      /* Lower privileges */
 
1837
      ret_errno = lower_privileges();
 
1838
      if(ret_errno != 0){
 
1839
        errno = ret_errno;
 
1840
        perror_plus("Failed to lower privileges");
 
1841
      }
 
1842
    }
1772
1843
    
1773
1844
    /* Close the socket */
1774
1845
    int ret = (int)TEMP_FAILURE_RETRY(close(sd));
1777
1848
    }
1778
1849
    
1779
1850
    if(ret_setflags == -1){
1780
 
      errno = ret_errno;
 
1851
      errno = ioctl_errno;
1781
1852
      perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
1782
1853
      errno = old_errno;
1783
 
      return ret_errno;
 
1854
      return ioctl_errno;
1784
1855
    }
1785
1856
  } else if(debug){
1786
1857
    fprintf_plus(stderr, "Interface \"%s\" is already down; odd\n",
1794
1865
int main(int argc, char *argv[]){
1795
1866
  mandos_context mc = { .server = NULL, .dh_bits = 1024,
1796
1867
                        .priority = "SECURE256:!CTYPE-X.509:"
1797
 
                        "+CTYPE-OPENPGP", .current_server = NULL, 
 
1868
                        "+CTYPE-OPENPGP", .current_server = NULL,
1798
1869
                        .interfaces = NULL, .interfaces_size = 0 };
1799
1870
  AvahiSServiceBrowser *sb = NULL;
1800
1871
  error_t ret_errno;
1804
1875
  int exitcode = EXIT_SUCCESS;
1805
1876
  char *interfaces_to_take_down = NULL;
1806
1877
  size_t interfaces_to_take_down_size = 0;
1807
 
  char tempdir[] = "/tmp/mandosXXXXXX";
1808
 
  bool tempdir_created = false;
 
1878
  char run_tempdir[] = "/run/tmp/mandosXXXXXX";
 
1879
  char old_tempdir[] = "/tmp/mandosXXXXXX";
 
1880
  char *tempdir = NULL;
1809
1881
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1810
1882
  const char *seckey = PATHDIR "/" SECKEY;
1811
1883
  const char *pubkey = PATHDIR "/" PUBKEY;
1993
2065
    /* Work around Debian bug #633582:
1994
2066
       <http://bugs.debian.org/633582> */
1995
2067
    
1996
 
    /* Re-raise priviliges */
1997
 
    if(raise_privileges() == 0){
 
2068
    /* Re-raise privileges */
 
2069
    ret_errno = raise_privileges();
 
2070
    if(ret_errno != 0){
 
2071
      errno = ret_errno;
 
2072
      perror_plus("Failed to raise privileges");
 
2073
    } else {
1998
2074
      struct stat st;
1999
2075
      
2000
2076
      if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
2040
2116
      }
2041
2117
    
2042
2118
      /* Lower privileges */
2043
 
      lower_privileges();
 
2119
      ret_errno = lower_privileges();
 
2120
      if(ret_errno != 0){
 
2121
        errno = ret_errno;
 
2122
        perror_plus("Failed to lower privileges");
 
2123
      }
2044
2124
    }
2045
2125
  }
2046
2126
  
2072
2152
      memcpy(interfaces_hooks, mc.interfaces, mc.interfaces_size);
2073
2153
      argz_stringify(interfaces_hooks, mc.interfaces_size, (int)',');
2074
2154
    }
2075
 
    if(not run_network_hooks("start", interfaces_hooks != NULL ?
2076
 
                             interfaces_hooks : "", delay)){
2077
 
      goto end;
2078
 
    }
 
2155
    run_network_hooks("start", interfaces_hooks != NULL ?
 
2156
                      interfaces_hooks : "", delay);
2079
2157
  }
2080
2158
  
2081
2159
  if(not debug){
2209
2287
        break;
2210
2288
      }
2211
2289
      bool interface_was_up = interface_is_up(interface);
2212
 
      ret = bring_up_interface(interface, delay);
 
2290
      errno = bring_up_interface(interface, delay);
2213
2291
      if(not interface_was_up){
2214
 
        if(ret != 0){
2215
 
          errno = ret;
 
2292
        if(errno != 0){
2216
2293
          perror_plus("Failed to bring up interface");
2217
2294
        } else {
2218
 
          ret_errno = argz_add(&interfaces_to_take_down,
2219
 
                               &interfaces_to_take_down_size,
2220
 
                               interface);
2221
 
          if(ret_errno != 0){
2222
 
            errno = ret_errno;
 
2295
          errno = argz_add(&interfaces_to_take_down,
 
2296
                           &interfaces_to_take_down_size,
 
2297
                           interface);
 
2298
          if(errno != 0){
2223
2299
            perror_plus("argz_add");
2224
2300
          }
2225
2301
        }
2256
2332
    goto end;
2257
2333
  }
2258
2334
  
2259
 
  if(mkdtemp(tempdir) == NULL){
 
2335
  /* Try /run/tmp before /tmp */
 
2336
  tempdir = mkdtemp(run_tempdir);
 
2337
  if(tempdir == NULL and errno == ENOENT){
 
2338
      if(debug){
 
2339
        fprintf_plus(stderr, "Tempdir %s did not work, trying %s\n",
 
2340
                     run_tempdir, old_tempdir);
 
2341
      }
 
2342
      tempdir = mkdtemp(old_tempdir);
 
2343
  }
 
2344
  if(tempdir == NULL){
2260
2345
    perror_plus("mkdtemp");
2261
2346
    goto end;
2262
2347
  }
2263
 
  tempdir_created = true;
2264
2348
  
2265
2349
  if(quit_now){
2266
2350
    goto end;
2449
2533
    }
2450
2534
  }
2451
2535
  
2452
 
  /* Re-raise priviliges */
 
2536
  /* Re-raise privileges */
2453
2537
  {
2454
 
    raise_privileges();
2455
 
    
2456
 
    /* Run network hooks */
2457
 
    run_network_hooks("stop", interfaces_hooks != NULL ?
2458
 
                      interfaces_hooks : "", delay);
2459
 
    
2460
 
    /* Take down the network interfaces which were brought up */
2461
 
    {
2462
 
      char *interface = NULL;
2463
 
      while((interface=argz_next(interfaces_to_take_down,
2464
 
                                 interfaces_to_take_down_size,
2465
 
                                 interface))){
2466
 
        ret_errno = take_down_interface(interface);
2467
 
        if(ret_errno != 0){
2468
 
          errno = ret_errno;
2469
 
          perror_plus("Failed to take down interface");
2470
 
        }
2471
 
      }
2472
 
      if(debug and (interfaces_to_take_down == NULL)){
2473
 
        fprintf_plus(stderr, "No interfaces needed to be taken"
2474
 
                     " down\n");
2475
 
      }
2476
 
    }
2477
 
    
2478
 
    lower_privileges_permanently();
 
2538
    ret_errno = raise_privileges();
 
2539
    if(ret_errno != 0){
 
2540
      perror_plus("Failed to raise privileges");
 
2541
    } else {
 
2542
      
 
2543
      /* Run network hooks */
 
2544
      run_network_hooks("stop", interfaces_hooks != NULL ?
 
2545
                        interfaces_hooks : "", delay);
 
2546
      
 
2547
      /* Take down the network interfaces which were brought up */
 
2548
      {
 
2549
        char *interface = NULL;
 
2550
        while((interface=argz_next(interfaces_to_take_down,
 
2551
                                   interfaces_to_take_down_size,
 
2552
                                   interface))){
 
2553
          ret_errno = take_down_interface(interface);
 
2554
          if(ret_errno != 0){
 
2555
            errno = ret_errno;
 
2556
            perror_plus("Failed to take down interface");
 
2557
          }
 
2558
        }
 
2559
        if(debug and (interfaces_to_take_down == NULL)){
 
2560
          fprintf_plus(stderr, "No interfaces needed to be taken"
 
2561
                       " down\n");
 
2562
        }
 
2563
      }
 
2564
    }
 
2565
    
 
2566
    ret_errno = lower_privileges_permanently();
 
2567
    if(ret_errno != 0){
 
2568
      perror_plus("Failed to lower privileges permanently");
 
2569
    }
2479
2570
  }
2480
2571
  
2481
2572
  free(interfaces_to_take_down);
2482
2573
  free(interfaces_hooks);
2483
2574
  
2484
2575
  /* Removes the GPGME temp directory and all files inside */
2485
 
  if(tempdir_created){
 
2576
  if(tempdir != NULL){
2486
2577
    struct dirent **direntries = NULL;
2487
2578
    struct dirent *direntry = NULL;
2488
2579
    int numentries = scandir(tempdir, &direntries, notdotentries,