/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-06-08 00:13:06 UTC
  • Revision ID: teddy@recompile.se-20140608001306-ums93iqhkvztj7u1
Make mandos-client use unlinkat() instead of remove().

* plugins.d/mandos-client.d (main): Replace remove() with unlinkat().

Show diffs side-by-side

added added

removed removed

Lines of Context:
1458
1458
  error_t ret_errno = 0;
1459
1459
  if(seteuid(0) == -1){
1460
1460
    ret_errno = errno;
 
1461
    perror_plus("seteuid");
1461
1462
  }
1462
1463
  errno = old_errno;
1463
1464
  return ret_errno;
1474
1475
  }
1475
1476
  if(setuid(0) == -1){
1476
1477
    ret_errno = errno;
 
1478
    perror_plus("seteuid");
1477
1479
  }
1478
1480
  errno = old_errno;
1479
1481
  return ret_errno;
1486
1488
  error_t ret_errno = 0;
1487
1489
  if(seteuid(uid) == -1){
1488
1490
    ret_errno = errno;
 
1491
    perror_plus("seteuid");
1489
1492
  }
1490
1493
  errno = old_errno;
1491
1494
  return ret_errno;
1498
1501
  error_t ret_errno = 0;
1499
1502
  if(setuid(uid) == -1){
1500
1503
    ret_errno = errno;
 
1504
    perror_plus("setuid");
1501
1505
  }
1502
1506
  errno = old_errno;
1503
1507
  return ret_errno;
1504
1508
}
1505
1509
 
 
1510
#ifndef O_CLOEXEC
 
1511
/*
 
1512
 * Based on the example in the GNU LibC manual chapter 13.13 "File
 
1513
 * Descriptor Flags".
 
1514
 | [[info:libc:Descriptor%20Flags][File Descriptor Flags]] |
 
1515
 */
 
1516
__attribute__((warn_unused_result))
 
1517
static int set_cloexec_flag(int fd){
 
1518
  int ret = (int)TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD, 0));
 
1519
  /* If reading the flags failed, return error indication now. */
 
1520
  if(ret < 0){
 
1521
    return ret;
 
1522
  }
 
1523
  /* Store modified flag word in the descriptor. */
 
1524
  return (int)TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD,
 
1525
                                       ret | FD_CLOEXEC));
 
1526
}
 
1527
#endif  /* not O_CLOEXEC */
 
1528
 
1506
1529
__attribute__((nonnull))
1507
1530
void run_network_hooks(const char *mode, const char *interface,
1508
1531
                       const float delay){
1509
 
  struct dirent **direntries = NULL;
 
1532
  struct dirent **direntries;
1510
1533
  if(hookdir_fd == -1){
1511
 
    hookdir_fd = open(hookdir, O_RDONLY);
 
1534
    hookdir_fd = open(hookdir, O_RDONLY |
 
1535
#ifdef O_CLOEXEC
 
1536
                      O_CLOEXEC
 
1537
#else  /* not O_CLOEXEC */
 
1538
                      0
 
1539
#endif  /* not O_CLOEXEC */
 
1540
                      );
1512
1541
    if(hookdir_fd == -1){
1513
1542
      if(errno == ENOENT){
1514
1543
        if(debug){
1520
1549
      }
1521
1550
      return;
1522
1551
    }
 
1552
#ifndef O_CLOEXEC
 
1553
    if(set_cloexec_flag(hookdir_fd) < 0){
 
1554
      perror_plus("set_cloexec_flag");
 
1555
      if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
 
1556
        perror_plus("close");
 
1557
      } else {
 
1558
        hookdir_fd = -1;
 
1559
      }
 
1560
      return;
 
1561
    }
 
1562
#endif  /* not O_CLOEXEC */
1523
1563
  }
1524
1564
#ifdef __GLIBC__
1525
1565
#if __GLIBC_PREREQ(2, 15)
1550
1590
    if(hook_pid == 0){
1551
1591
      /* Child */
1552
1592
      /* Raise privileges */
1553
 
      errno = raise_privileges_permanently();
1554
 
      if(errno != 0){
 
1593
      if(raise_privileges_permanently() != 0){
1555
1594
        perror_plus("Failed to raise privileges");
1556
1595
        _exit(EX_NOPERM);
1557
1596
      }
1624
1663
          _exit(EX_OSERR);
1625
1664
        }
1626
1665
      }
1627
 
      int hook_fd = openat(hookdir_fd, direntry->d_name, O_RDONLY);
1628
 
      if(hook_fd == -1){
1629
 
        perror_plus("openat");
1630
 
        _exit(EXIT_FAILURE);
1631
 
      }
1632
 
      if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1633
 
        perror_plus("close");
1634
 
        _exit(EXIT_FAILURE);
1635
 
      }
1636
 
      if(fexecve(hook_fd, (char *const []){ direntry->d_name, NULL },
1637
 
                 environ) == -1){
 
1666
      if(fexecve(hookdir_fd, (char *const [])
 
1667
                 { direntry->d_name, NULL }, environ) == -1){
1638
1668
        perror_plus("fexecve");
1639
1669
        _exit(EXIT_FAILURE);
1640
1670
      }
1667
1697
                   direntry->d_name);
1668
1698
    }
1669
1699
  }
1670
 
  free(direntries);
1671
1700
  if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1672
1701
    perror_plus("close");
1673
1702
  } else {
1730
1759
    /* Raise privileges */
1731
1760
    ret_errno = raise_privileges();
1732
1761
    if(ret_errno != 0){
1733
 
      errno = ret_errno;
1734
1762
      perror_plus("Failed to raise privileges");
1735
1763
    }
1736
1764
    
1840
1868
    /* Raise privileges */
1841
1869
    ret_errno = raise_privileges();
1842
1870
    if(ret_errno != 0){
1843
 
      errno = ret_errno;
1844
1871
      perror_plus("Failed to raise privileges");
1845
1872
    }
1846
1873
    
2253
2280
  
2254
2281
  /* If no interfaces were specified, make a list */
2255
2282
  if(mc.interfaces == NULL){
2256
 
    struct dirent **direntries = NULL;
 
2283
    struct dirent **direntries;
2257
2284
    /* Look for any good interfaces */
2258
2285
    ret = scandir(sys_class_net, &direntries, good_interface,
2259
2286
                  alphasort);
2274
2301
      }
2275
2302
      free(direntries);
2276
2303
    } else {
2277
 
      if(ret == 0){
2278
 
        free(direntries);
2279
 
      }
 
2304
      free(direntries);
2280
2305
      fprintf_plus(stderr, "Could not find a network interface\n");
2281
2306
      exitcode = EXIT_FAILURE;
2282
2307
      goto end;
2555
2580
  {
2556
2581
    ret_errno = raise_privileges();
2557
2582
    if(ret_errno != 0){
2558
 
      errno = ret_errno;
2559
2583
      perror_plus("Failed to raise privileges");
2560
2584
    } else {
2561
2585
      
2584
2608
    
2585
2609
    ret_errno = lower_privileges_permanently();
2586
2610
    if(ret_errno != 0){
2587
 
      errno = ret_errno;
2588
2611
      perror_plus("Failed to lower privileges permanently");
2589
2612
    }
2590
2613
  }
2595
2618
  /* Removes the GPGME temp directory and all files inside */
2596
2619
  if(tempdir != NULL){
2597
2620
    struct dirent **direntries = NULL;
2598
 
    int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY |
2599
 
                                                  O_NOFOLLOW));
 
2621
    int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY));
2600
2622
    if(tempdir_fd == -1){
2601
2623
      perror_plus("open");
2602
2624
    } else {
2603
 
#ifdef __GLIBC__
2604
 
#if __GLIBC_PREREQ(2, 15)
2605
 
      int numentries = scandirat(tempdir_fd, ".", &direntries,
2606
 
                                 notdotentries, alphasort);
2607
 
#else  /* not __GLIBC_PREREQ(2, 15) */
2608
 
      int numentries = scandir(tempdir, &direntries, notdotentries,
2609
 
                               alphasort);
2610
 
#endif  /* not __GLIBC_PREREQ(2, 15) */
2611
 
#else   /* not __GLIBC__ */
2612
 
      int numentries = scandir(tempdir, &direntries, notdotentries,
2613
 
                               alphasort);
2614
 
#endif  /* not __GLIBC__ */
2615
 
      if(numentries >= 0){
 
2625
      int numentries = scandir(tempdir, &direntries, notdotentries,
 
2626
                               alphasort);
 
2627
      if(numentries > 0){
2616
2628
        for(int i = 0; i < numentries; i++){
2617
2629
          ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
2618
2630
          if(ret == -1){