/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release

« back to all changes in this revision

Viewing changes to plugins.d/mandos-client.c

  • Committer: Teddy Hogeborn
  • Date: 2014-05-11 20:18:48 UTC
  • Revision ID: teddy@recompile.se-20140511201848-blw9pl5c76upkhdc
Tags: version-1.6.5-1
* Makefile (version): Changed to "1.6.5".
* NEWS (Version 1.6.5): New entry.
* debian/changelog (1.6.5-1): - '' -

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
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() */
180
183
  perror(print_text);
181
184
}
182
185
 
183
 
__attribute__((format (gnu_printf, 2, 3)))
 
186
__attribute__((format (gnu_printf, 2, 3), nonnull))
184
187
int fprintf_plus(FILE *stream, const char *format, ...){
185
188
  va_list ap;
186
189
  va_start (ap, format);
195
198
 * bytes. "buffer_capacity" is how much is currently allocated,
196
199
 * "buffer_length" is how much is already used.
197
200
 */
 
201
__attribute__((nonnull, warn_unused_result))
198
202
size_t incbuffer(char **buffer, size_t buffer_length,
199
203
                 size_t buffer_capacity){
200
204
  if(buffer_length + BUFFER_SIZE > buffer_capacity){
213
217
}
214
218
 
215
219
/* Add server to set of servers to retry periodically */
 
220
__attribute__((nonnull, warn_unused_result))
216
221
bool add_server(const char *ip, in_port_t port, AvahiIfIndex if_index,
217
222
                int af, server **current_server){
218
223
  int ret;
229
234
    perror_plus("strdup");
230
235
    return false;
231
236
  }
 
237
  ret = clock_gettime(CLOCK_MONOTONIC, &(new_server->last_seen));
 
238
  if(ret == -1){
 
239
    perror_plus("clock_gettime");
 
240
    return false;
 
241
  }
232
242
  /* Special case of first server */
233
243
  if(*current_server == NULL){
234
244
    new_server->next = new_server;
235
245
    new_server->prev = new_server;
236
246
    *current_server = new_server;
237
 
  /* Place the new server last in the list */
238
247
  } else {
 
248
    /* Place the new server last in the list */
239
249
    new_server->next = *current_server;
240
250
    new_server->prev = (*current_server)->prev;
241
251
    new_server->prev->next = new_server;
242
252
    (*current_server)->prev = new_server;
243
253
  }
244
 
  ret = clock_gettime(CLOCK_MONOTONIC, &(*current_server)->last_seen);
245
 
  if(ret == -1){
246
 
    perror_plus("clock_gettime");
247
 
    return false;
248
 
  }
249
254
  return true;
250
255
}
251
256
 
252
257
/* 
253
258
 * Initialize GPGME.
254
259
 */
255
 
static bool init_gpgme(const char *seckey, const char *pubkey,
256
 
                       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){
257
265
  gpgme_error_t rc;
258
266
  gpgme_engine_info_t engine_info;
259
267
  
260
268
  /*
261
269
   * Helper function to insert pub and seckey to the engine keyring.
262
270
   */
263
 
  bool import_key(const char *filename){
 
271
  bool import_key(const char * const filename){
264
272
    int ret;
265
273
    int fd;
266
274
    gpgme_data_t pgp_data;
347
355
 * Decrypt OpenPGP data.
348
356
 * Returns -1 on error
349
357
 */
 
358
__attribute__((nonnull, warn_unused_result))
350
359
static ssize_t pgp_packet_decrypt(const char *cryptotext,
351
360
                                  size_t crypto_size,
352
361
                                  char **plaintext,
472
481
  return plaintext_length;
473
482
}
474
483
 
475
 
static const char * safer_gnutls_strerror(int value){
 
484
__attribute__((warn_unused_result))
 
485
static const char *safer_gnutls_strerror(int value){
476
486
  const char *ret = gnutls_strerror(value);
477
487
  if(ret == NULL)
478
488
    ret = "(unknown)";
480
490
}
481
491
 
482
492
/* GnuTLS log function callback */
 
493
__attribute__((nonnull))
483
494
static void debuggnutls(__attribute__((unused)) int level,
484
495
                        const char* string){
485
496
  fprintf_plus(stderr, "GnuTLS: %s", string);
486
497
}
487
498
 
 
499
__attribute__((nonnull, warn_unused_result))
488
500
static int init_gnutls_global(const char *pubkeyfilename,
489
501
                              const char *seckeyfilename,
490
502
                              mandos_context *mc){
564
576
  return -1;
565
577
}
566
578
 
 
579
__attribute__((nonnull, warn_unused_result))
567
580
static int init_gnutls_session(gnutls_session_t *session,
568
581
                               mandos_context *mc){
569
582
  int ret;
626
639
                      __attribute__((unused)) const char *txt){}
627
640
 
628
641
/* Called when a Mandos server is found */
 
642
__attribute__((nonnull, warn_unused_result))
629
643
static int start_mandos_communication(const char *ip, in_port_t port,
630
644
                                      AvahiIfIndex if_index,
631
645
                                      int af, mandos_context *mc){
632
646
  int ret, tcp_sd = -1;
633
647
  ssize_t sret;
634
 
  union {
635
 
    struct sockaddr_in in;
636
 
    struct sockaddr_in6 in6;
637
 
  } to;
 
648
  struct sockaddr_storage to;
638
649
  char *buffer = NULL;
639
650
  char *decrypted_buffer = NULL;
640
651
  size_t buffer_length = 0;
721
732
  
722
733
  memset(&to, 0, sizeof(to));
723
734
  if(af == AF_INET6){
724
 
    to.in6.sin6_family = (sa_family_t)af;
725
 
    ret = inet_pton(af, ip, &to.in6.sin6_addr);
 
735
    ((struct sockaddr_in6 *)&to)->sin6_family = (sa_family_t)af;
 
736
    ret = inet_pton(af, ip, &((struct sockaddr_in6 *)&to)->sin6_addr);
726
737
  } else {                      /* IPv4 */
727
 
    to.in.sin_family = (sa_family_t)af;
728
 
    ret = inet_pton(af, ip, &to.in.sin_addr);
 
738
    ((struct sockaddr_in *)&to)->sin_family = (sa_family_t)af;
 
739
    ret = inet_pton(af, ip, &((struct sockaddr_in *)&to)->sin_addr);
729
740
  }
730
741
  if(ret < 0 ){
731
742
    int e = errno;
740
751
    goto mandos_end;
741
752
  }
742
753
  if(af == AF_INET6){
743
 
    to.in6.sin6_port = htons(port);    
744
 
#ifdef __GNUC__
745
 
#pragma GCC diagnostic push
746
 
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
747
 
#endif
748
 
    if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
749
 
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower */
750
 
#ifdef __GNUC__
751
 
#pragma GCC diagnostic pop
752
 
#endif
 
754
    ((struct sockaddr_in6 *)&to)->sin6_port = htons(port);
 
755
    if(IN6_IS_ADDR_LINKLOCAL
 
756
       (&((struct sockaddr_in6 *)&to)->sin6_addr)){
753
757
      if(if_index == AVAHI_IF_UNSPEC){
754
758
        fprintf_plus(stderr, "An IPv6 link-local address is"
755
759
                     " incomplete without a network interface\n");
757
761
        goto mandos_end;
758
762
      }
759
763
      /* Set the network interface number as scope */
760
 
      to.in6.sin6_scope_id = (uint32_t)if_index;
 
764
      ((struct sockaddr_in6 *)&to)->sin6_scope_id = (uint32_t)if_index;
761
765
    }
762
766
  } else {
763
 
    to.in.sin_port = htons(port);
 
767
    ((struct sockaddr_in *)&to)->sin_port = htons(port);
764
768
  }
765
769
  
766
770
  if(quit_now){
783
787
    }
784
788
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
785
789
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
786
 
    const char *pcret;
787
790
    if(af == AF_INET6){
788
 
      pcret = inet_ntop(af, &(to.in6.sin6_addr), addrstr,
789
 
                        sizeof(addrstr));
 
791
      ret = getnameinfo((struct sockaddr *)&to,
 
792
                        sizeof(struct sockaddr_in6),
 
793
                        addrstr, sizeof(addrstr), NULL, 0,
 
794
                        NI_NUMERICHOST);
790
795
    } else {
791
 
      pcret = inet_ntop(af, &(to.in.sin_addr), addrstr,
792
 
                        sizeof(addrstr));
 
796
      ret = getnameinfo((struct sockaddr *)&to,
 
797
                        sizeof(struct sockaddr_in),
 
798
                        addrstr, sizeof(addrstr), NULL, 0,
 
799
                        NI_NUMERICHOST);
793
800
    }
794
 
    if(pcret == NULL){
795
 
      perror_plus("inet_ntop");
796
 
    } else {
797
 
      if(strcmp(addrstr, ip) != 0){
798
 
        fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
799
 
      }
 
801
    if(ret == EAI_SYSTEM){
 
802
      perror_plus("getnameinfo");
 
803
    } else if(ret != 0) {
 
804
      fprintf_plus(stderr, "getnameinfo: %s", gai_strerror(ret));
 
805
    } else if(strcmp(addrstr, ip) != 0){
 
806
      fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
800
807
    }
801
808
  }
802
809
  
806
813
  }
807
814
  
808
815
  if(af == AF_INET6){
809
 
    ret = connect(tcp_sd, &to.in6, sizeof(to));
 
816
    ret = connect(tcp_sd, (struct sockaddr *)&to,
 
817
                  sizeof(struct sockaddr_in6));
810
818
  } else {
811
 
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
 
819
    ret = connect(tcp_sd, (struct sockaddr *)&to, /* IPv4 */
 
820
                  sizeof(struct sockaddr_in));
812
821
  }
813
822
  if(ret < 0){
814
 
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
 
823
    if((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
815
824
      int e = errno;
816
825
      perror_plus("connect");
817
826
      errno = e;
1032
1041
  return retval;
1033
1042
}
1034
1043
 
 
1044
__attribute__((nonnull))
1035
1045
static void resolve_callback(AvahiSServiceResolver *r,
1036
1046
                             AvahiIfIndex interface,
1037
1047
                             AvahiProtocol proto,
1045
1055
                             AVAHI_GCC_UNUSED AvahiStringList *txt,
1046
1056
                             AVAHI_GCC_UNUSED AvahiLookupResultFlags
1047
1057
                             flags,
1048
 
                             void* mc){
 
1058
                             void *mc){
1049
1059
  if(r == NULL){
1050
1060
    return;
1051
1061
  }
1104
1114
                            const char *domain,
1105
1115
                            AVAHI_GCC_UNUSED AvahiLookupResultFlags
1106
1116
                            flags,
1107
 
                            void* mc){
 
1117
                            void *mc){
1108
1118
  if(b == NULL){
1109
1119
    return;
1110
1120
  }
1170
1180
  errno = old_errno;
1171
1181
}
1172
1182
 
 
1183
__attribute__((nonnull, warn_unused_result))
1173
1184
bool get_flags(const char *ifname, struct ifreq *ifr){
1174
1185
  int ret;
1175
1186
  error_t ret_errno;
1194
1205
  return true;
1195
1206
}
1196
1207
 
 
1208
__attribute__((nonnull, warn_unused_result))
1197
1209
bool good_flags(const char *ifname, const struct ifreq *ifr){
1198
1210
  
1199
1211
  /* Reject the loopback device */
1241
1253
 * corresponds to an acceptable network device.
1242
1254
 * (This function is passed to scandir(3) as a filter function.)
1243
1255
 */
 
1256
__attribute__((nonnull, warn_unused_result))
1244
1257
int good_interface(const struct dirent *if_entry){
1245
1258
  if(if_entry->d_name[0] == '.'){
1246
1259
    return 0;
1264
1277
/* 
1265
1278
 * This function determines if a network interface is up.
1266
1279
 */
 
1280
__attribute__((nonnull, warn_unused_result))
1267
1281
bool interface_is_up(const char *interface){
1268
1282
  struct ifreq ifr;
1269
1283
  if(not get_flags(interface, &ifr)){
1280
1294
/* 
1281
1295
 * This function determines if a network interface is running
1282
1296
 */
 
1297
__attribute__((nonnull, warn_unused_result))
1283
1298
bool interface_is_running(const char *interface){
1284
1299
  struct ifreq ifr;
1285
1300
  if(not get_flags(interface, &ifr)){
1293
1308
  return (bool)(ifr.ifr_flags & IFF_RUNNING);
1294
1309
}
1295
1310
 
 
1311
__attribute__((nonnull, pure, warn_unused_result))
1296
1312
int notdotentries(const struct dirent *direntry){
1297
1313
  /* Skip "." and ".." */
1298
1314
  if(direntry->d_name[0] == '.'
1305
1321
}
1306
1322
 
1307
1323
/* Is this directory entry a runnable program? */
 
1324
__attribute__((nonnull, warn_unused_result))
1308
1325
int runnable_hook(const struct dirent *direntry){
1309
1326
  int ret;
1310
1327
  size_t sret;
1365
1382
  return 1;
1366
1383
}
1367
1384
 
 
1385
__attribute__((nonnull, warn_unused_result))
1368
1386
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval,
1369
1387
                            mandos_context *mc){
1370
1388
  int ret;
1374
1392
  
1375
1393
  while(true){
1376
1394
    if(mc->current_server == NULL){
1377
 
      if (debug){
 
1395
      if(debug){
1378
1396
        fprintf_plus(stderr, "Wait until first server is found."
1379
1397
                     " No timeout!\n");
1380
1398
      }
1381
1399
      ret = avahi_simple_poll_iterate(s, -1);
1382
1400
    } else {
1383
 
      if (debug){
 
1401
      if(debug){
1384
1402
        fprintf_plus(stderr, "Check current_server if we should run"
1385
1403
                     " it, or wait\n");
1386
1404
      }
1403
1421
                     - ((intmax_t)waited_time.tv_sec * 1000))
1404
1422
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
1405
1423
      
1406
 
      if (debug){
 
1424
      if(debug){
1407
1425
        fprintf_plus(stderr, "Blocking for %" PRIdMAX " ms\n",
1408
1426
                     block_time);
1409
1427
      }
1431
1449
      ret = avahi_simple_poll_iterate(s, (int)block_time);
1432
1450
    }
1433
1451
    if(ret != 0){
1434
 
      if (ret > 0 or errno != EINTR){
 
1452
      if(ret > 0 or errno != EINTR){
1435
1453
        return (ret != 1) ? ret : 0;
1436
1454
      }
1437
1455
    }
1439
1457
}
1440
1458
 
1441
1459
/* Set effective uid to 0, return errno */
 
1460
__attribute__((warn_unused_result))
1442
1461
error_t raise_privileges(void){
1443
1462
  error_t old_errno = errno;
1444
1463
  error_t ret_errno = 0;
1451
1470
}
1452
1471
 
1453
1472
/* Set effective and real user ID to 0.  Return errno. */
 
1473
__attribute__((warn_unused_result))
1454
1474
error_t raise_privileges_permanently(void){
1455
1475
  error_t old_errno = errno;
1456
1476
  error_t ret_errno = raise_privileges();
1467
1487
}
1468
1488
 
1469
1489
/* Set effective user ID to unprivileged saved user ID */
 
1490
__attribute__((warn_unused_result))
1470
1491
error_t lower_privileges(void){
1471
1492
  error_t old_errno = errno;
1472
1493
  error_t ret_errno = 0;
1479
1500
}
1480
1501
 
1481
1502
/* Lower privileges permanently */
 
1503
__attribute__((warn_unused_result))
1482
1504
error_t lower_privileges_permanently(void){
1483
1505
  error_t old_errno = errno;
1484
1506
  error_t ret_errno = 0;
1490
1512
  return ret_errno;
1491
1513
}
1492
1514
 
1493
 
bool run_network_hooks(const char *mode, const char *interface,
 
1515
__attribute__((nonnull))
 
1516
void run_network_hooks(const char *mode, const char *interface,
1494
1517
                       const float delay){
1495
1518
  struct dirent **direntries;
1496
1519
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1524
1547
      if(hook_pid == 0){
1525
1548
        /* Child */
1526
1549
        /* Raise privileges */
1527
 
        raise_privileges_permanently();
 
1550
        if(raise_privileges_permanently() != 0){
 
1551
          perror_plus("Failed to raise privileges");
 
1552
          _exit(EX_NOPERM);
 
1553
        }
1528
1554
        /* Set group */
1529
1555
        errno = 0;
1530
1556
        ret = setgid(0);
1531
1557
        if(ret == -1){
1532
1558
          perror_plus("setgid");
 
1559
          _exit(EX_NOPERM);
1533
1560
        }
1534
1561
        /* Reset supplementary groups */
1535
1562
        errno = 0;
1536
1563
        ret = setgroups(0, NULL);
1537
1564
        if(ret == -1){
1538
1565
          perror_plus("setgroups");
1539
 
        }
1540
 
        dup2(devnull, STDIN_FILENO);
1541
 
        close(devnull);
1542
 
        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
        }
1543
1583
        ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1544
1584
        if(ret == -1){
1545
1585
          perror_plus("setenv");
1561
1601
          _exit(EX_OSERR);
1562
1602
        }
1563
1603
        char *delaystring;
1564
 
        ret = asprintf(&delaystring, "%f", delay);
 
1604
        ret = asprintf(&delaystring, "%f", (double)delay);
1565
1605
        if(ret == -1){
1566
1606
          perror_plus("asprintf");
1567
1607
          _exit(EX_OSERR);
1620
1660
    }
1621
1661
    close(devnull);
1622
1662
  }
1623
 
  return true;
1624
1663
}
1625
1664
 
 
1665
__attribute__((nonnull, warn_unused_result))
1626
1666
error_t bring_up_interface(const char *const interface,
1627
1667
                           const float delay){
1628
 
  int sd = -1;
1629
1668
  error_t old_errno = errno;
1630
 
  error_t ret_errno = 0;
1631
 
  int ret, ret_setflags;
 
1669
  int ret;
1632
1670
  struct ifreq network;
1633
1671
  unsigned int if_index = if_nametoindex(interface);
1634
1672
  if(if_index == 0){
1643
1681
  }
1644
1682
  
1645
1683
  if(not interface_is_up(interface)){
1646
 
    if(not get_flags(interface, &network) and debug){
 
1684
    error_t ret_errno = 0, ioctl_errno = 0;
 
1685
    if(not get_flags(interface, &network)){
1647
1686
      ret_errno = errno;
1648
1687
      fprintf_plus(stderr, "Failed to get flags for interface "
1649
1688
                   "\"%s\"\n", interface);
 
1689
      errno = old_errno;
1650
1690
      return ret_errno;
1651
1691
    }
1652
 
    network.ifr_flags |= IFF_UP;
 
1692
    network.ifr_flags |= IFF_UP; /* set flag */
1653
1693
    
1654
 
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1655
 
    if(sd < 0){
 
1694
    int sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
1695
    if(sd == -1){
1656
1696
      ret_errno = errno;
1657
1697
      perror_plus("socket");
1658
1698
      errno = old_errno;
1659
1699
      return ret_errno;
1660
1700
    }
1661
 
  
 
1701
    
1662
1702
    if(quit_now){
1663
 
      close(sd);
 
1703
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1704
      if(ret == -1){
 
1705
        perror_plus("close");
 
1706
      }
1664
1707
      errno = old_errno;
1665
1708
      return EINTR;
1666
1709
    }
1670
1713
                   interface);
1671
1714
    }
1672
1715
    
1673
 
    /* Raise priviliges */
1674
 
    raise_privileges();
 
1716
    /* Raise privileges */
 
1717
    ret_errno = raise_privileges();
 
1718
    if(ret_errno != 0){
 
1719
      perror_plus("Failed to raise privileges");
 
1720
    }
1675
1721
    
1676
1722
#ifdef __linux__
1677
 
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1678
 
       messages about the network interface to mess up the prompt */
1679
 
    int ret_linux = klogctl(8, NULL, 5);
1680
 
    bool restore_loglevel = true;
1681
 
    if(ret_linux == -1){
1682
 
      restore_loglevel = false;
1683
 
      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
      }
1684
1734
    }
1685
1735
#endif  /* __linux__ */
1686
 
    ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1687
 
    ret_errno = errno;
 
1736
    int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
 
1737
    ioctl_errno = errno;
1688
1738
#ifdef __linux__
1689
1739
    if(restore_loglevel){
1690
1740
      ret_linux = klogctl(7, NULL, 0);
1694
1744
    }
1695
1745
#endif  /* __linux__ */
1696
1746
    
1697
 
    /* Lower privileges */
1698
 
    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
    }
1699
1756
    
1700
1757
    /* Close the socket */
1701
1758
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
1704
1761
    }
1705
1762
    
1706
1763
    if(ret_setflags == -1){
1707
 
      errno = ret_errno;
 
1764
      errno = ioctl_errno;
1708
1765
      perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1709
1766
      errno = old_errno;
1710
 
      return ret_errno;
 
1767
      return ioctl_errno;
1711
1768
    }
1712
1769
  } else if(debug){
1713
1770
    fprintf_plus(stderr, "Interface \"%s\" is already up; good\n",
1731
1788
  return 0;
1732
1789
}
1733
1790
 
 
1791
__attribute__((nonnull, warn_unused_result))
1734
1792
error_t take_down_interface(const char *const interface){
1735
1793
  error_t old_errno = errno;
1736
1794
  struct ifreq network;
1741
1799
    return ENXIO;
1742
1800
  }
1743
1801
  if(interface_is_up(interface)){
1744
 
    error_t ret_errno = 0;
 
1802
    error_t ret_errno = 0, ioctl_errno = 0;
1745
1803
    if(not get_flags(interface, &network) and debug){
1746
1804
      ret_errno = errno;
1747
1805
      fprintf_plus(stderr, "Failed to get flags for interface "
1748
1806
                   "\"%s\"\n", interface);
 
1807
      errno = old_errno;
1749
1808
      return ret_errno;
1750
1809
    }
1751
1810
    network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1752
1811
    
1753
1812
    int sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1754
 
    if(sd < 0){
 
1813
    if(sd == -1){
1755
1814
      ret_errno = errno;
1756
1815
      perror_plus("socket");
1757
1816
      errno = old_errno;
1763
1822
                   interface);
1764
1823
    }
1765
1824
    
1766
 
    /* Raise priviliges */
1767
 
    raise_privileges();
 
1825
    /* Raise privileges */
 
1826
    ret_errno = raise_privileges();
 
1827
    if(ret_errno != 0){
 
1828
      perror_plus("Failed to raise privileges");
 
1829
    }
1768
1830
    
1769
1831
    int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1770
 
    ret_errno = errno;
 
1832
    ioctl_errno = errno;
1771
1833
    
1772
 
    /* Lower privileges */
1773
 
    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
    }
1774
1843
    
1775
1844
    /* Close the socket */
1776
1845
    int ret = (int)TEMP_FAILURE_RETRY(close(sd));
1779
1848
    }
1780
1849
    
1781
1850
    if(ret_setflags == -1){
1782
 
      errno = ret_errno;
 
1851
      errno = ioctl_errno;
1783
1852
      perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
1784
1853
      errno = old_errno;
1785
 
      return ret_errno;
 
1854
      return ioctl_errno;
1786
1855
    }
1787
1856
  } else if(debug){
1788
1857
    fprintf_plus(stderr, "Interface \"%s\" is already down; odd\n",
1796
1865
int main(int argc, char *argv[]){
1797
1866
  mandos_context mc = { .server = NULL, .dh_bits = 1024,
1798
1867
                        .priority = "SECURE256:!CTYPE-X.509:"
1799
 
                        "+CTYPE-OPENPGP", .current_server = NULL, 
 
1868
                        "+CTYPE-OPENPGP", .current_server = NULL,
1800
1869
                        .interfaces = NULL, .interfaces_size = 0 };
1801
1870
  AvahiSServiceBrowser *sb = NULL;
1802
1871
  error_t ret_errno;
1806
1875
  int exitcode = EXIT_SUCCESS;
1807
1876
  char *interfaces_to_take_down = NULL;
1808
1877
  size_t interfaces_to_take_down_size = 0;
1809
 
  char tempdir[] = "/tmp/mandosXXXXXX";
1810
 
  bool tempdir_created = false;
 
1878
  char run_tempdir[] = "/run/tmp/mandosXXXXXX";
 
1879
  char old_tempdir[] = "/tmp/mandosXXXXXX";
 
1880
  char *tempdir = NULL;
1811
1881
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1812
1882
  const char *seckey = PATHDIR "/" SECKEY;
1813
1883
  const char *pubkey = PATHDIR "/" PUBKEY;
1995
2065
    /* Work around Debian bug #633582:
1996
2066
       <http://bugs.debian.org/633582> */
1997
2067
    
1998
 
    /* Re-raise priviliges */
1999
 
    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 {
2000
2074
      struct stat st;
2001
2075
      
2002
2076
      if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
2042
2116
      }
2043
2117
    
2044
2118
      /* Lower privileges */
2045
 
      lower_privileges();
 
2119
      ret_errno = lower_privileges();
 
2120
      if(ret_errno != 0){
 
2121
        errno = ret_errno;
 
2122
        perror_plus("Failed to lower privileges");
 
2123
      }
2046
2124
    }
2047
2125
  }
2048
2126
  
2074
2152
      memcpy(interfaces_hooks, mc.interfaces, mc.interfaces_size);
2075
2153
      argz_stringify(interfaces_hooks, mc.interfaces_size, (int)',');
2076
2154
    }
2077
 
    if(not run_network_hooks("start", interfaces_hooks != NULL ?
2078
 
                             interfaces_hooks : "", delay)){
2079
 
      goto end;
2080
 
    }
 
2155
    run_network_hooks("start", interfaces_hooks != NULL ?
 
2156
                      interfaces_hooks : "", delay);
2081
2157
  }
2082
2158
  
2083
2159
  if(not debug){
2211
2287
        break;
2212
2288
      }
2213
2289
      bool interface_was_up = interface_is_up(interface);
2214
 
      ret = bring_up_interface(interface, delay);
 
2290
      errno = bring_up_interface(interface, delay);
2215
2291
      if(not interface_was_up){
2216
 
        if(ret != 0){
2217
 
          errno = ret;
 
2292
        if(errno != 0){
2218
2293
          perror_plus("Failed to bring up interface");
2219
2294
        } else {
2220
 
          ret_errno = argz_add(&interfaces_to_take_down,
2221
 
                               &interfaces_to_take_down_size,
2222
 
                               interface);
2223
 
          if(ret_errno != 0){
2224
 
            errno = ret_errno;
 
2295
          errno = argz_add(&interfaces_to_take_down,
 
2296
                           &interfaces_to_take_down_size,
 
2297
                           interface);
 
2298
          if(errno != 0){
2225
2299
            perror_plus("argz_add");
2226
2300
          }
2227
2301
        }
2258
2332
    goto end;
2259
2333
  }
2260
2334
  
2261
 
  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){
2262
2345
    perror_plus("mkdtemp");
2263
2346
    goto end;
2264
2347
  }
2265
 
  tempdir_created = true;
2266
2348
  
2267
2349
  if(quit_now){
2268
2350
    goto end;
2343
2425
      sleep((unsigned int)retry_interval);
2344
2426
    }
2345
2427
    
2346
 
    if (not quit_now){
 
2428
    if(not quit_now){
2347
2429
      exitcode = EXIT_SUCCESS;
2348
2430
    }
2349
2431
    
2451
2533
    }
2452
2534
  }
2453
2535
  
2454
 
  /* Re-raise priviliges */
 
2536
  /* Re-raise privileges */
2455
2537
  {
2456
 
    raise_privileges();
2457
 
    
2458
 
    /* Run network hooks */
2459
 
    run_network_hooks("stop", interfaces_hooks != NULL ?
2460
 
                      interfaces_hooks : "", delay);
2461
 
    
2462
 
    /* Take down the network interfaces which were brought up */
2463
 
    {
2464
 
      char *interface = NULL;
2465
 
      while((interface=argz_next(interfaces_to_take_down,
2466
 
                                 interfaces_to_take_down_size,
2467
 
                                 interface))){
2468
 
        ret_errno = take_down_interface(interface);
2469
 
        if(ret_errno != 0){
2470
 
          errno = ret_errno;
2471
 
          perror_plus("Failed to take down interface");
2472
 
        }
2473
 
      }
2474
 
      if(debug and (interfaces_to_take_down == NULL)){
2475
 
        fprintf_plus(stderr, "No interfaces needed to be taken"
2476
 
                     " down\n");
2477
 
      }
2478
 
    }
2479
 
    
2480
 
    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
    }
2481
2570
  }
2482
2571
  
2483
2572
  free(interfaces_to_take_down);
2484
2573
  free(interfaces_hooks);
2485
2574
  
2486
2575
  /* Removes the GPGME temp directory and all files inside */
2487
 
  if(tempdir_created){
 
2576
  if(tempdir != NULL){
2488
2577
    struct dirent **direntries = NULL;
2489
2578
    struct dirent *direntry = NULL;
2490
2579
    int numentries = scandir(tempdir, &direntries, notdotentries,
2491
2580
                             alphasort);
2492
 
    if (numentries > 0){
 
2581
    if(numentries > 0){
2493
2582
      for(int i = 0; i < numentries; i++){
2494
2583
        direntry = direntries[i];
2495
2584
        char *fullname = NULL;
2510
2599
 
2511
2600
    /* need to clean even if 0 because man page doesn't specify */
2512
2601
    free(direntries);
2513
 
    if (numentries == -1){
 
2602
    if(numentries == -1){
2514
2603
      perror_plus("scandir");
2515
2604
    }
2516
2605
    ret = rmdir(tempdir);