/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-06-07 22:37:22 UTC
  • mto: (237.7.272 trunk)
  • mto: This revision was merged to the branch mainline in revision 317.
  • Revision ID: teddy@recompile.se-20140607223722-55qmdr3n9x39pvx4
Make mandos-client use fstatat().

* plugins.d/mandos-client.d (runnable_hook): Use fstatat().

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), asprintf() */
41
41
 
42
42
#include <stdio.h>              /* fprintf(), stderr, fwrite(),
43
 
                                   stdout, ferror() */
 
43
                                   stdout, ferror(), remove() */
44
44
#include <stdint.h>             /* uint16_t, uint32_t, intptr_t */
45
45
#include <stddef.h>             /* NULL, size_t, ssize_t */
46
46
#include <stdlib.h>             /* free(), EXIT_SUCCESS, srand(),
57
57
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
58
58
                                   inet_pton(), connect(),
59
59
                                   getnameinfo() */
60
 
#include <fcntl.h>              /* open(), unlinkat() */
 
60
#include <fcntl.h>              /* open() */
61
61
#include <dirent.h>             /* opendir(), struct dirent, readdir()
62
62
                                 */
63
63
#include <inttypes.h>           /* PRIu16, PRIdMAX, intmax_t,
73
73
                                */
74
74
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
75
75
                                   getuid(), getgid(), seteuid(),
76
 
                                   setgid(), pause(), _exit(),
77
 
                                   unlinkat() */
 
76
                                   setgid(), pause(), _exit() */
78
77
#include <arpa/inet.h>          /* inet_pton(), htons() */
79
78
#include <iso646.h>             /* not, or, and */
80
79
#include <argp.h>               /* struct argp_option, error_t, struct
234
233
                          .af = af };
235
234
  if(new_server->ip == NULL){
236
235
    perror_plus("strdup");
237
 
    free(new_server);
238
236
    return false;
239
237
  }
240
238
  ret = clock_gettime(CLOCK_MONOTONIC, &(new_server->last_seen));
241
239
  if(ret == -1){
242
240
    perror_plus("clock_gettime");
243
 
#ifdef __GNUC__
244
 
#pragma GCC diagnostic push
245
 
#pragma GCC diagnostic ignored "-Wcast-qual"
246
 
#endif
247
 
    free((char *)(new_server->ip));
248
 
#ifdef __GNUC__
249
 
#pragma GCC diagnostic pop
250
 
#endif
251
 
    free(new_server);
252
241
    return false;
253
242
  }
254
243
  /* Special case of first server */
493
482
  return plaintext_length;
494
483
}
495
484
 
496
 
__attribute__((warn_unused_result, const))
497
 
static const char *safe_string(const char *str){
498
 
  if(str == NULL)
499
 
    return "(unknown)";
500
 
  return str;
501
 
}
502
 
 
503
485
__attribute__((warn_unused_result))
504
486
static const char *safer_gnutls_strerror(int value){
505
487
  const char *ret = gnutls_strerror(value);
506
 
  return safe_string(ret);
 
488
  if(ret == NULL)
 
489
    ret = "(unknown)";
 
490
  return ret;
507
491
}
508
492
 
509
493
/* GnuTLS log function callback */
518
502
                              const char *seckeyfilename,
519
503
                              mandos_context *mc){
520
504
  int ret;
521
 
  unsigned int uret;
522
505
  
523
506
  if(debug){
524
507
    fprintf_plus(stderr, "Initializing GnuTLS\n");
575
558
                 safer_gnutls_strerror(ret));
576
559
    goto globalfail;
577
560
  }
578
 
  if(mc->dh_bits == 0){
579
 
    /* Find out the optimal number of DH bits */
580
 
    /* Try to read the private key file */
581
 
    gnutls_datum_t buffer = { .data = NULL, .size = 0 };
582
 
    {
583
 
      int secfile = open(seckeyfilename, O_RDONLY);
584
 
      size_t buffer_capacity = 0;
585
 
      while(true){
586
 
        buffer_capacity = incbuffer((char **)&buffer.data,
587
 
                                    (size_t)buffer.size,
588
 
                                    (size_t)buffer_capacity);
589
 
        if(buffer_capacity == 0){
590
 
          perror_plus("incbuffer");
591
 
          free(buffer.data);
592
 
          buffer.data = NULL;
593
 
          break;
594
 
        }
595
 
        ssize_t bytes_read = read(secfile, buffer.data + buffer.size,
596
 
                                  BUFFER_SIZE);
597
 
        /* EOF */
598
 
        if(bytes_read == 0){
599
 
          break;
600
 
        }
601
 
        /* check bytes_read for failure */
602
 
        if(bytes_read < 0){
603
 
          perror_plus("read");
604
 
          free(buffer.data);
605
 
          buffer.data = NULL;
606
 
          break;
607
 
        }
608
 
        buffer.size += (unsigned int)bytes_read;
609
 
      }
610
 
      close(secfile);
611
 
    }
612
 
    /* If successful, use buffer to parse private key */
613
 
    gnutls_sec_param_t sec_param = GNUTLS_SEC_PARAM_ULTRA;
614
 
    if(buffer.data != NULL){
615
 
      {
616
 
        gnutls_openpgp_privkey_t privkey = NULL;
617
 
        ret = gnutls_openpgp_privkey_init(&privkey);
618
 
        if(ret != GNUTLS_E_SUCCESS){
619
 
          fprintf_plus(stderr, "Error initializing OpenPGP key"
620
 
                       " structure: %s", safer_gnutls_strerror(ret));
621
 
          free(buffer.data);
622
 
          buffer.data = NULL;
623
 
        } else {
624
 
          ret = gnutls_openpgp_privkey_import(privkey, &buffer,
625
 
                                            GNUTLS_OPENPGP_FMT_BASE64,
626
 
                                              "", 0);
627
 
          if(ret != GNUTLS_E_SUCCESS){
628
 
            fprintf_plus(stderr, "Error importing OpenPGP key : %s",
629
 
                         safer_gnutls_strerror(ret));
630
 
            privkey = NULL;
631
 
          }
632
 
          free(buffer.data);
633
 
          buffer.data = NULL;
634
 
          if(privkey != NULL){
635
 
            /* Use private key to suggest an appropriate sec_param */
636
 
            sec_param = gnutls_openpgp_privkey_sec_param(privkey);
637
 
            gnutls_openpgp_privkey_deinit(privkey);
638
 
            if(debug){
639
 
              fprintf_plus(stderr, "This OpenPGP key implies using a"
640
 
                           " GnuTLS security parameter \"%s\".\n",
641
 
                           safe_string(gnutls_sec_param_get_name
642
 
                                       (sec_param)));
643
 
            }
644
 
          }
645
 
        }
646
 
      }
647
 
      if(sec_param == GNUTLS_SEC_PARAM_UNKNOWN){
648
 
        /* Err on the side of caution */
649
 
        sec_param = GNUTLS_SEC_PARAM_ULTRA;
650
 
        if(debug){
651
 
          fprintf_plus(stderr, "Falling back to security parameter"
652
 
                       " \"%s\"\n",
653
 
                       safe_string(gnutls_sec_param_get_name
654
 
                                   (sec_param)));
655
 
        }
656
 
      }
657
 
    }
658
 
    uret = gnutls_sec_param_to_pk_bits(GNUTLS_PK_DH, sec_param);
659
 
    if(uret != 0){
660
 
      mc->dh_bits = uret;
661
 
      if(debug){
662
 
        fprintf_plus(stderr, "A \"%s\" GnuTLS security parameter"
663
 
                     " implies %u DH bits; using that.\n",
664
 
                     safe_string(gnutls_sec_param_get_name
665
 
                                 (sec_param)),
666
 
                     mc->dh_bits);
667
 
      }
668
 
    } else {
669
 
      fprintf_plus(stderr, "Failed to get implied number of DH"
670
 
                   " bits for security parameter \"%s\"): %s\n",
671
 
                   safe_string(gnutls_sec_param_get_name(sec_param)),
672
 
                   safer_gnutls_strerror(ret));
673
 
      goto globalfail;
674
 
    }
675
 
  } else if(debug){
676
 
    fprintf_plus(stderr, "DH bits explicitly set to %u\n",
677
 
                 mc->dh_bits);
678
 
  }
679
561
  ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
680
562
  if(ret != GNUTLS_E_SUCCESS){
681
 
    fprintf_plus(stderr, "Error in GnuTLS prime generation (%u bits):"
682
 
                 " %s\n", mc->dh_bits, safer_gnutls_strerror(ret));
 
563
    fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
 
564
                 safer_gnutls_strerror(ret));
683
565
    goto globalfail;
684
566
  }
685
567
  
1183
1065
     timed out */
1184
1066
  
1185
1067
  if(quit_now){
1186
 
    avahi_s_service_resolver_free(r);
1187
1068
    return;
1188
1069
  }
1189
1070
  
1576
1457
  error_t ret_errno = 0;
1577
1458
  if(seteuid(0) == -1){
1578
1459
    ret_errno = errno;
 
1460
    perror_plus("seteuid");
1579
1461
  }
1580
1462
  errno = old_errno;
1581
1463
  return ret_errno;
1592
1474
  }
1593
1475
  if(setuid(0) == -1){
1594
1476
    ret_errno = errno;
 
1477
    perror_plus("seteuid");
1595
1478
  }
1596
1479
  errno = old_errno;
1597
1480
  return ret_errno;
1604
1487
  error_t ret_errno = 0;
1605
1488
  if(seteuid(uid) == -1){
1606
1489
    ret_errno = errno;
 
1490
    perror_plus("seteuid");
1607
1491
  }
1608
1492
  errno = old_errno;
1609
1493
  return ret_errno;
1616
1500
  error_t ret_errno = 0;
1617
1501
  if(setuid(uid) == -1){
1618
1502
    ret_errno = errno;
 
1503
    perror_plus("setuid");
1619
1504
  }
1620
1505
  errno = old_errno;
1621
1506
  return ret_errno;
1622
1507
}
1623
1508
 
 
1509
#ifndef O_CLOEXEC
 
1510
/*
 
1511
 * Based on the example in the GNU LibC manual chapter 13.13 "File
 
1512
 * Descriptor Flags".
 
1513
 | [[info:libc:Descriptor%20Flags][File Descriptor Flags]] |
 
1514
 */
 
1515
__attribute__((warn_unused_result))
 
1516
static int set_cloexec_flag(int fd){
 
1517
  int ret = (int)TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD, 0));
 
1518
  /* If reading the flags failed, return error indication now. */
 
1519
  if(ret < 0){
 
1520
    return ret;
 
1521
  }
 
1522
  /* Store modified flag word in the descriptor. */
 
1523
  return (int)TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD,
 
1524
                                       ret | FD_CLOEXEC));
 
1525
}
 
1526
#endif  /* not O_CLOEXEC */
 
1527
 
1624
1528
__attribute__((nonnull))
1625
1529
void run_network_hooks(const char *mode, const char *interface,
1626
1530
                       const float delay){
1627
 
  struct dirent **direntries = NULL;
 
1531
  struct dirent **direntries;
1628
1532
  if(hookdir_fd == -1){
1629
 
    hookdir_fd = open(hookdir, O_RDONLY);
 
1533
    hookdir_fd = open(hookdir, O_RDONLY |
 
1534
#ifdef O_CLOEXEC
 
1535
                      O_CLOEXEC
 
1536
#else  /* not O_CLOEXEC */
 
1537
                      0
 
1538
#endif  /* not O_CLOEXEC */
 
1539
                      );
1630
1540
    if(hookdir_fd == -1){
1631
1541
      if(errno == ENOENT){
1632
1542
        if(debug){
1638
1548
      }
1639
1549
      return;
1640
1550
    }
 
1551
#ifndef O_CLOEXEC
 
1552
    if(set_cloexec_flag(hookdir_fd) < 0){
 
1553
      perror_plus("set_cloexec_flag");
 
1554
      if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
 
1555
        perror_plus("close");
 
1556
      } else {
 
1557
        hookdir_fd = -1;
 
1558
      }
 
1559
      return;
 
1560
    }
 
1561
#endif  /* not O_CLOEXEC */
1641
1562
  }
1642
1563
#ifdef __GLIBC__
1643
1564
#if __GLIBC_PREREQ(2, 15)
1668
1589
    if(hook_pid == 0){
1669
1590
      /* Child */
1670
1591
      /* Raise privileges */
1671
 
      errno = raise_privileges_permanently();
1672
 
      if(errno != 0){
 
1592
      if(raise_privileges_permanently() != 0){
1673
1593
        perror_plus("Failed to raise privileges");
1674
1594
        _exit(EX_NOPERM);
1675
1595
      }
1742
1662
          _exit(EX_OSERR);
1743
1663
        }
1744
1664
      }
1745
 
      int hook_fd = openat(hookdir_fd, direntry->d_name, O_RDONLY);
1746
 
      if(hook_fd == -1){
1747
 
        perror_plus("openat");
1748
 
        _exit(EXIT_FAILURE);
1749
 
      }
1750
 
      if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1751
 
        perror_plus("close");
1752
 
        _exit(EXIT_FAILURE);
1753
 
      }
1754
 
      if(fexecve(hook_fd, (char *const []){ direntry->d_name, NULL },
1755
 
                 environ) == -1){
 
1665
      if(fexecve(hookdir_fd, (char *const [])
 
1666
                 { direntry->d_name, NULL }, environ) == -1){
1756
1667
        perror_plus("fexecve");
1757
1668
        _exit(EXIT_FAILURE);
1758
1669
      }
1759
1670
    } else {
1760
 
      if(hook_pid == -1){
1761
 
        perror_plus("fork");
1762
 
        free(direntry);
1763
 
        continue;
1764
 
      }
1765
1671
      int status;
1766
1672
      if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1767
1673
        perror_plus("waitpid");
1768
 
        free(direntry);
1769
1674
        continue;
1770
1675
      }
1771
1676
      if(WIFEXITED(status)){
1773
1678
          fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1774
1679
                       " with status %d\n", direntry->d_name,
1775
1680
                       WEXITSTATUS(status));
1776
 
          free(direntry);
1777
1681
          continue;
1778
1682
        }
1779
1683
      } else if(WIFSIGNALED(status)){
1780
1684
        fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1781
1685
                     " signal %d\n", direntry->d_name,
1782
1686
                     WTERMSIG(status));
1783
 
        free(direntry);
1784
1687
        continue;
1785
1688
      } else {
1786
1689
        fprintf_plus(stderr, "Warning: network hook \"%s\""
1787
1690
                     " crashed\n", direntry->d_name);
1788
 
        free(direntry);
1789
1691
        continue;
1790
1692
      }
1791
1693
    }
1793
1695
      fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1794
1696
                   direntry->d_name);
1795
1697
    }
1796
 
    free(direntry);
1797
1698
  }
1798
 
  free(direntries);
1799
1699
  if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1800
1700
    perror_plus("close");
1801
1701
  } else {
1858
1758
    /* Raise privileges */
1859
1759
    ret_errno = raise_privileges();
1860
1760
    if(ret_errno != 0){
1861
 
      errno = ret_errno;
1862
1761
      perror_plus("Failed to raise privileges");
1863
1762
    }
1864
1763
    
1968
1867
    /* Raise privileges */
1969
1868
    ret_errno = raise_privileges();
1970
1869
    if(ret_errno != 0){
1971
 
      errno = ret_errno;
1972
1870
      perror_plus("Failed to raise privileges");
1973
1871
    }
1974
1872
    
2007
1905
}
2008
1906
 
2009
1907
int main(int argc, char *argv[]){
2010
 
  mandos_context mc = { .server = NULL, .dh_bits = 0,
 
1908
  mandos_context mc = { .server = NULL, .dh_bits = 1024,
2011
1909
                        .priority = "SECURE256:!CTYPE-X.509:"
2012
 
                        "+CTYPE-OPENPGP:!RSA", .current_server = NULL,
 
1910
                        "+CTYPE-OPENPGP", .current_server = NULL,
2013
1911
                        .interfaces = NULL, .interfaces_size = 0 };
2014
1912
  AvahiSServiceBrowser *sb = NULL;
2015
1913
  error_t ret_errno;
2381
2279
  
2382
2280
  /* If no interfaces were specified, make a list */
2383
2281
  if(mc.interfaces == NULL){
2384
 
    struct dirent **direntries = NULL;
 
2282
    struct dirent **direntries;
2385
2283
    /* Look for any good interfaces */
2386
2284
    ret = scandir(sys_class_net, &direntries, good_interface,
2387
2285
                  alphasort);
2393
2291
        if(ret_errno != 0){
2394
2292
          errno = ret_errno;
2395
2293
          perror_plus("argz_add");
2396
 
          free(direntries[i]);
2397
2294
          continue;
2398
2295
        }
2399
2296
        if(debug){
2400
2297
          fprintf_plus(stderr, "Will use interface \"%s\"\n",
2401
2298
                       direntries[i]->d_name);
2402
2299
        }
2403
 
        free(direntries[i]);
2404
2300
      }
2405
2301
      free(direntries);
2406
2302
    } else {
2407
 
      if(ret == 0){
2408
 
        free(direntries);
2409
 
      }
 
2303
      free(direntries);
2410
2304
      fprintf_plus(stderr, "Could not find a network interface\n");
2411
2305
      exitcode = EXIT_FAILURE;
2412
2306
      goto end;
2676
2570
    mc.current_server->prev->next = NULL;
2677
2571
    while(mc.current_server != NULL){
2678
2572
      server *next = mc.current_server->next;
2679
 
#ifdef __GNUC__
2680
 
#pragma GCC diagnostic push
2681
 
#pragma GCC diagnostic ignored "-Wcast-qual"
2682
 
#endif
2683
 
      free((char *)(mc.current_server->ip));
2684
 
#ifdef __GNUC__
2685
 
#pragma GCC diagnostic pop
2686
 
#endif
2687
2573
      free(mc.current_server);
2688
2574
      mc.current_server = next;
2689
2575
    }
2693
2579
  {
2694
2580
    ret_errno = raise_privileges();
2695
2581
    if(ret_errno != 0){
2696
 
      errno = ret_errno;
2697
2582
      perror_plus("Failed to raise privileges");
2698
2583
    } else {
2699
2584
      
2722
2607
    
2723
2608
    ret_errno = lower_privileges_permanently();
2724
2609
    if(ret_errno != 0){
2725
 
      errno = ret_errno;
2726
2610
      perror_plus("Failed to lower privileges permanently");
2727
2611
    }
2728
2612
  }
2733
2617
  /* Removes the GPGME temp directory and all files inside */
2734
2618
  if(tempdir != NULL){
2735
2619
    struct dirent **direntries = NULL;
2736
 
    int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY |
2737
 
                                                  O_NOFOLLOW));
2738
 
    if(tempdir_fd == -1){
2739
 
      perror_plus("open");
2740
 
    } else {
2741
 
#ifdef __GLIBC__
2742
 
#if __GLIBC_PREREQ(2, 15)
2743
 
      int numentries = scandirat(tempdir_fd, ".", &direntries,
2744
 
                                 notdotentries, alphasort);
2745
 
#else  /* not __GLIBC_PREREQ(2, 15) */
2746
 
      int numentries = scandir(tempdir, &direntries, notdotentries,
2747
 
                               alphasort);
2748
 
#endif  /* not __GLIBC_PREREQ(2, 15) */
2749
 
#else   /* not __GLIBC__ */
2750
 
      int numentries = scandir(tempdir, &direntries, notdotentries,
2751
 
                               alphasort);
2752
 
#endif  /* not __GLIBC__ */
2753
 
      if(numentries >= 0){
2754
 
        for(int i = 0; i < numentries; i++){
2755
 
          ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
2756
 
          if(ret == -1){
2757
 
            fprintf_plus(stderr, "unlinkat(open(\"%s\", O_RDONLY),"
2758
 
                         " \"%s\", 0): %s\n", tempdir,
2759
 
                         direntries[i]->d_name, strerror(errno));
2760
 
          }
2761
 
          free(direntries[i]);
2762
 
        }
2763
 
        
2764
 
        /* need to clean even if 0 because man page doesn't specify */
2765
 
        free(direntries);
2766
 
        if(numentries == -1){
2767
 
          perror_plus("scandir");
2768
 
        }
2769
 
        ret = rmdir(tempdir);
2770
 
        if(ret == -1 and errno != ENOENT){
2771
 
          perror_plus("rmdir");
2772
 
        }
 
2620
    struct dirent *direntry = NULL;
 
2621
    int numentries = scandir(tempdir, &direntries, notdotentries,
 
2622
                             alphasort);
 
2623
    if(numentries > 0){
 
2624
      for(int i = 0; i < numentries; i++){
 
2625
        direntry = direntries[i];
 
2626
        char *fullname = NULL;
 
2627
        ret = asprintf(&fullname, "%s/%s", tempdir,
 
2628
                       direntry->d_name);
 
2629
        if(ret < 0){
 
2630
          perror_plus("asprintf");
 
2631
          continue;
 
2632
        }
 
2633
        ret = remove(fullname);
 
2634
        if(ret == -1){
 
2635
          fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
 
2636
                       strerror(errno));
 
2637
        }
 
2638
        free(fullname);
2773
2639
      }
2774
 
      TEMP_FAILURE_RETRY(close(tempdir_fd));
 
2640
    }
 
2641
    
 
2642
    /* need to clean even if 0 because man page doesn't specify */
 
2643
    free(direntries);
 
2644
    if(numentries == -1){
 
2645
      perror_plus("scandir");
 
2646
    }
 
2647
    ret = rmdir(tempdir);
 
2648
    if(ret == -1 and errno != ENOENT){
 
2649
      perror_plus("rmdir");
2775
2650
    }
2776
2651
  }
2777
2652