/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: 2015-04-02 21:56:15 UTC
  • Revision ID: teddy@recompile.se-20150402215615-v2ll1pgbae2dqdfx
mandos-ctl: Do minor formatting and whitespace adjustments.

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(), remove() */
 
43
                                   stdout, ferror() */
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() */
 
60
#include <fcntl.h>              /* open(), unlinkat() */
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() */
 
76
                                   setgid(), pause(), _exit(),
 
77
                                   unlinkat() */
77
78
#include <arpa/inet.h>          /* inet_pton(), htons() */
78
79
#include <iso646.h>             /* not, or, and */
79
80
#include <argp.h>               /* struct argp_option, error_t, struct
233
234
                          .af = af };
234
235
  if(new_server->ip == NULL){
235
236
    perror_plus("strdup");
 
237
    free(new_server);
236
238
    return false;
237
239
  }
238
240
  ret = clock_gettime(CLOCK_MONOTONIC, &(new_server->last_seen));
239
241
  if(ret == -1){
240
242
    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);
241
252
    return false;
242
253
  }
243
254
  /* Special case of first server */
482
493
  return plaintext_length;
483
494
}
484
495
 
 
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
 
485
503
__attribute__((warn_unused_result))
486
504
static const char *safer_gnutls_strerror(int value){
487
505
  const char *ret = gnutls_strerror(value);
488
 
  if(ret == NULL)
489
 
    ret = "(unknown)";
490
 
  return ret;
 
506
  return safe_string(ret);
491
507
}
492
508
 
493
509
/* GnuTLS log function callback */
502
518
                              const char *seckeyfilename,
503
519
                              mandos_context *mc){
504
520
  int ret;
 
521
  unsigned int uret;
505
522
  
506
523
  if(debug){
507
524
    fprintf_plus(stderr, "Initializing GnuTLS\n");
558
575
                 safer_gnutls_strerror(ret));
559
576
    goto globalfail;
560
577
  }
 
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
  }
561
679
  ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
562
680
  if(ret != GNUTLS_E_SUCCESS){
563
 
    fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
564
 
                 safer_gnutls_strerror(ret));
 
681
    fprintf_plus(stderr, "Error in GnuTLS prime generation (%u bits):"
 
682
                 " %s\n", mc->dh_bits, safer_gnutls_strerror(ret));
565
683
    goto globalfail;
566
684
  }
567
685
  
1065
1183
     timed out */
1066
1184
  
1067
1185
  if(quit_now){
 
1186
    avahi_s_service_resolver_free(r);
1068
1187
    return;
1069
1188
  }
1070
1189
  
1346
1465
    return 0;
1347
1466
  }
1348
1467
  
1349
 
  char *fullname = NULL;
1350
 
  ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1351
 
  if(ret < 0){
1352
 
    perror_plus("asprintf");
1353
 
    return 0;
1354
 
  }
1355
 
  
1356
 
  ret = stat(fullname, &st);
 
1468
  ret = fstatat(hookdir_fd, direntry->d_name, &st, 0);
1357
1469
  if(ret == -1){
1358
1470
    if(debug){
1359
1471
      perror_plus("Could not stat hook");
1360
1472
    }
1361
1473
    return 0;
1362
1474
  }
1363
 
  free(fullname);
1364
1475
  if(not (S_ISREG(st.st_mode))){
1365
1476
    /* Not a regular file */
1366
1477
    if(debug){
1465
1576
  error_t ret_errno = 0;
1466
1577
  if(seteuid(0) == -1){
1467
1578
    ret_errno = errno;
1468
 
    perror_plus("seteuid");
1469
1579
  }
1470
1580
  errno = old_errno;
1471
1581
  return ret_errno;
1482
1592
  }
1483
1593
  if(setuid(0) == -1){
1484
1594
    ret_errno = errno;
1485
 
    perror_plus("seteuid");
1486
1595
  }
1487
1596
  errno = old_errno;
1488
1597
  return ret_errno;
1495
1604
  error_t ret_errno = 0;
1496
1605
  if(seteuid(uid) == -1){
1497
1606
    ret_errno = errno;
1498
 
    perror_plus("seteuid");
1499
1607
  }
1500
1608
  errno = old_errno;
1501
1609
  return ret_errno;
1508
1616
  error_t ret_errno = 0;
1509
1617
  if(setuid(uid) == -1){
1510
1618
    ret_errno = errno;
1511
 
    perror_plus("setuid");
1512
1619
  }
1513
1620
  errno = old_errno;
1514
1621
  return ret_errno;
1515
1622
}
1516
1623
 
1517
 
#ifndef O_CLOEXEC
1518
 
/*
1519
 
 * Based on the example in the GNU LibC manual chapter 13.13 "File
1520
 
 * Descriptor Flags".
1521
 
 | [[info:libc:Descriptor%20Flags][File Descriptor Flags]] |
1522
 
 */
1523
 
__attribute__((warn_unused_result))
1524
 
static int set_cloexec_flag(int fd){
1525
 
  int ret = (int)TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD, 0));
1526
 
  /* If reading the flags failed, return error indication now. */
1527
 
  if(ret < 0){
1528
 
    return ret;
1529
 
  }
1530
 
  /* Store modified flag word in the descriptor. */
1531
 
  return (int)TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD,
1532
 
                                       ret | FD_CLOEXEC));
1533
 
}
1534
 
#endif  /* not O_CLOEXEC */
1535
 
 
1536
1624
__attribute__((nonnull))
1537
1625
void run_network_hooks(const char *mode, const char *interface,
1538
1626
                       const float delay){
1539
 
  struct dirent **direntries;
 
1627
  struct dirent **direntries = NULL;
1540
1628
  if(hookdir_fd == -1){
1541
 
    hookdir_fd = open(hookdir, O_RDONLY |
1542
 
#ifdef O_CLOEXEC
1543
 
                      O_CLOEXEC
1544
 
#else  /* not O_CLOEXEC */
1545
 
                      0
1546
 
#endif  /* not O_CLOEXEC */
1547
 
                      );
 
1629
    hookdir_fd = open(hookdir, O_RDONLY);
1548
1630
    if(hookdir_fd == -1){
1549
1631
      if(errno == ENOENT){
1550
1632
        if(debug){
1556
1638
      }
1557
1639
      return;
1558
1640
    }
1559
 
#ifndef O_CLOEXEC
1560
 
    if(set_cloexec_flag(hookdir_fd) < 0){
1561
 
      perror_plus("set_cloexec_flag");
1562
 
      if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1563
 
        perror_plus("close");
1564
 
      } else {
1565
 
        hookdir_fd = -1;
1566
 
      }
1567
 
      return;
1568
 
    }
1569
 
#endif  /* not O_CLOEXEC */
1570
1641
  }
1571
1642
#ifdef __GLIBC__
1572
1643
#if __GLIBC_PREREQ(2, 15)
1597
1668
    if(hook_pid == 0){
1598
1669
      /* Child */
1599
1670
      /* Raise privileges */
1600
 
      if(raise_privileges_permanently() != 0){
 
1671
      errno = raise_privileges_permanently();
 
1672
      if(errno != 0){
1601
1673
        perror_plus("Failed to raise privileges");
1602
1674
        _exit(EX_NOPERM);
1603
1675
      }
1670
1742
          _exit(EX_OSERR);
1671
1743
        }
1672
1744
      }
1673
 
      if(fexecve(hookdir_fd, (char *const [])
1674
 
                 { direntry->d_name, NULL }, environ) == -1){
 
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){
1675
1756
        perror_plus("fexecve");
1676
1757
        _exit(EXIT_FAILURE);
1677
1758
      }
1678
1759
    } else {
 
1760
      if(hook_pid == -1){
 
1761
        perror_plus("fork");
 
1762
        free(direntry);
 
1763
        continue;
 
1764
      }
1679
1765
      int status;
1680
1766
      if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1681
1767
        perror_plus("waitpid");
 
1768
        free(direntry);
1682
1769
        continue;
1683
1770
      }
1684
1771
      if(WIFEXITED(status)){
1686
1773
          fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1687
1774
                       " with status %d\n", direntry->d_name,
1688
1775
                       WEXITSTATUS(status));
 
1776
          free(direntry);
1689
1777
          continue;
1690
1778
        }
1691
1779
      } else if(WIFSIGNALED(status)){
1692
1780
        fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1693
1781
                     " signal %d\n", direntry->d_name,
1694
1782
                     WTERMSIG(status));
 
1783
        free(direntry);
1695
1784
        continue;
1696
1785
      } else {
1697
1786
        fprintf_plus(stderr, "Warning: network hook \"%s\""
1698
1787
                     " crashed\n", direntry->d_name);
 
1788
        free(direntry);
1699
1789
        continue;
1700
1790
      }
1701
1791
    }
1703
1793
      fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1704
1794
                   direntry->d_name);
1705
1795
    }
 
1796
    free(direntry);
1706
1797
  }
 
1798
  free(direntries);
1707
1799
  if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1708
1800
    perror_plus("close");
1709
1801
  } else {
1766
1858
    /* Raise privileges */
1767
1859
    ret_errno = raise_privileges();
1768
1860
    if(ret_errno != 0){
 
1861
      errno = ret_errno;
1769
1862
      perror_plus("Failed to raise privileges");
1770
1863
    }
1771
1864
    
1875
1968
    /* Raise privileges */
1876
1969
    ret_errno = raise_privileges();
1877
1970
    if(ret_errno != 0){
 
1971
      errno = ret_errno;
1878
1972
      perror_plus("Failed to raise privileges");
1879
1973
    }
1880
1974
    
1913
2007
}
1914
2008
 
1915
2009
int main(int argc, char *argv[]){
1916
 
  mandos_context mc = { .server = NULL, .dh_bits = 1024,
 
2010
  mandos_context mc = { .server = NULL, .dh_bits = 0,
1917
2011
                        .priority = "SECURE256:!CTYPE-X.509:"
1918
 
                        "+CTYPE-OPENPGP", .current_server = NULL,
 
2012
                        "+CTYPE-OPENPGP:!RSA", .current_server = NULL,
1919
2013
                        .interfaces = NULL, .interfaces_size = 0 };
1920
2014
  AvahiSServiceBrowser *sb = NULL;
1921
2015
  error_t ret_errno;
2287
2381
  
2288
2382
  /* If no interfaces were specified, make a list */
2289
2383
  if(mc.interfaces == NULL){
2290
 
    struct dirent **direntries;
 
2384
    struct dirent **direntries = NULL;
2291
2385
    /* Look for any good interfaces */
2292
2386
    ret = scandir(sys_class_net, &direntries, good_interface,
2293
2387
                  alphasort);
2299
2393
        if(ret_errno != 0){
2300
2394
          errno = ret_errno;
2301
2395
          perror_plus("argz_add");
 
2396
          free(direntries[i]);
2302
2397
          continue;
2303
2398
        }
2304
2399
        if(debug){
2305
2400
          fprintf_plus(stderr, "Will use interface \"%s\"\n",
2306
2401
                       direntries[i]->d_name);
2307
2402
        }
 
2403
        free(direntries[i]);
2308
2404
      }
2309
2405
      free(direntries);
2310
2406
    } else {
2311
 
      free(direntries);
 
2407
      if(ret == 0){
 
2408
        free(direntries);
 
2409
      }
2312
2410
      fprintf_plus(stderr, "Could not find a network interface\n");
2313
2411
      exitcode = EXIT_FAILURE;
2314
2412
      goto end;
2578
2676
    mc.current_server->prev->next = NULL;
2579
2677
    while(mc.current_server != NULL){
2580
2678
      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
2581
2687
      free(mc.current_server);
2582
2688
      mc.current_server = next;
2583
2689
    }
2587
2693
  {
2588
2694
    ret_errno = raise_privileges();
2589
2695
    if(ret_errno != 0){
 
2696
      errno = ret_errno;
2590
2697
      perror_plus("Failed to raise privileges");
2591
2698
    } else {
2592
2699
      
2615
2722
    
2616
2723
    ret_errno = lower_privileges_permanently();
2617
2724
    if(ret_errno != 0){
 
2725
      errno = ret_errno;
2618
2726
      perror_plus("Failed to lower privileges permanently");
2619
2727
    }
2620
2728
  }
2625
2733
  /* Removes the GPGME temp directory and all files inside */
2626
2734
  if(tempdir != NULL){
2627
2735
    struct dirent **direntries = NULL;
2628
 
    struct dirent *direntry = NULL;
2629
 
    int numentries = scandir(tempdir, &direntries, notdotentries,
2630
 
                             alphasort);
2631
 
    if(numentries > 0){
2632
 
      for(int i = 0; i < numentries; i++){
2633
 
        direntry = direntries[i];
2634
 
        char *fullname = NULL;
2635
 
        ret = asprintf(&fullname, "%s/%s", tempdir,
2636
 
                       direntry->d_name);
2637
 
        if(ret < 0){
2638
 
          perror_plus("asprintf");
2639
 
          continue;
2640
 
        }
2641
 
        ret = remove(fullname);
2642
 
        if(ret == -1){
2643
 
          fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
2644
 
                       strerror(errno));
2645
 
        }
2646
 
        free(fullname);
 
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
        }
2647
2773
      }
2648
 
    }
2649
 
    
2650
 
    /* need to clean even if 0 because man page doesn't specify */
2651
 
    free(direntries);
2652
 
    if(numentries == -1){
2653
 
      perror_plus("scandir");
2654
 
    }
2655
 
    ret = rmdir(tempdir);
2656
 
    if(ret == -1 and errno != ENOENT){
2657
 
      perror_plus("rmdir");
 
2774
      TEMP_FAILURE_RETRY(close(tempdir_fd));
2658
2775
    }
2659
2776
  }
2660
2777