/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:44:52 UTC
  • Revision ID: teddy@recompile.se-20140608004452-6p928psyr1gbcawn
Make mandos-client use more scandirat().

* plugins.d/mandos-client.d (main): Use scandirat() when removing
                                    GPGME temp directory.

Show diffs side-by-side

added added

removed removed

Lines of Context:
234
234
                          .af = af };
235
235
  if(new_server->ip == NULL){
236
236
    perror_plus("strdup");
237
 
    free(new_server);
238
237
    return false;
239
238
  }
240
239
  ret = clock_gettime(CLOCK_MONOTONIC, &(new_server->last_seen));
241
240
  if(ret == -1){
242
241
    perror_plus("clock_gettime");
243
 
    free(new_server->ip);
244
 
    free(new_server);
245
242
    return false;
246
243
  }
247
244
  /* Special case of first server */
1461
1458
  error_t ret_errno = 0;
1462
1459
  if(seteuid(0) == -1){
1463
1460
    ret_errno = errno;
 
1461
    perror_plus("seteuid");
1464
1462
  }
1465
1463
  errno = old_errno;
1466
1464
  return ret_errno;
1477
1475
  }
1478
1476
  if(setuid(0) == -1){
1479
1477
    ret_errno = errno;
 
1478
    perror_plus("seteuid");
1480
1479
  }
1481
1480
  errno = old_errno;
1482
1481
  return ret_errno;
1489
1488
  error_t ret_errno = 0;
1490
1489
  if(seteuid(uid) == -1){
1491
1490
    ret_errno = errno;
 
1491
    perror_plus("seteuid");
1492
1492
  }
1493
1493
  errno = old_errno;
1494
1494
  return ret_errno;
1501
1501
  error_t ret_errno = 0;
1502
1502
  if(setuid(uid) == -1){
1503
1503
    ret_errno = errno;
 
1504
    perror_plus("setuid");
1504
1505
  }
1505
1506
  errno = old_errno;
1506
1507
  return ret_errno;
1507
1508
}
1508
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
 
1509
1529
__attribute__((nonnull))
1510
1530
void run_network_hooks(const char *mode, const char *interface,
1511
1531
                       const float delay){
1512
 
  struct dirent **direntries = NULL;
 
1532
  struct dirent **direntries;
1513
1533
  if(hookdir_fd == -1){
1514
 
    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
                      );
1515
1541
    if(hookdir_fd == -1){
1516
1542
      if(errno == ENOENT){
1517
1543
        if(debug){
1523
1549
      }
1524
1550
      return;
1525
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 */
1526
1563
  }
1527
1564
#ifdef __GLIBC__
1528
1565
#if __GLIBC_PREREQ(2, 15)
1553
1590
    if(hook_pid == 0){
1554
1591
      /* Child */
1555
1592
      /* Raise privileges */
1556
 
      errno = raise_privileges_permanently();
1557
 
      if(errno != 0){
 
1593
      if(raise_privileges_permanently() != 0){
1558
1594
        perror_plus("Failed to raise privileges");
1559
1595
        _exit(EX_NOPERM);
1560
1596
      }
1627
1663
          _exit(EX_OSERR);
1628
1664
        }
1629
1665
      }
1630
 
      int hook_fd = openat(hookdir_fd, direntry->d_name, O_RDONLY);
1631
 
      if(hook_fd == -1){
1632
 
        perror_plus("openat");
1633
 
        _exit(EXIT_FAILURE);
1634
 
      }
1635
 
      if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1636
 
        perror_plus("close");
1637
 
        _exit(EXIT_FAILURE);
1638
 
      }
1639
 
      if(fexecve(hook_fd, (char *const []){ direntry->d_name, NULL },
1640
 
                 environ) == -1){
 
1666
      if(fexecve(hookdir_fd, (char *const [])
 
1667
                 { direntry->d_name, NULL }, environ) == -1){
1641
1668
        perror_plus("fexecve");
1642
1669
        _exit(EXIT_FAILURE);
1643
1670
      }
1670
1697
                   direntry->d_name);
1671
1698
    }
1672
1699
  }
1673
 
  free(direntries);
1674
1700
  if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1675
1701
    perror_plus("close");
1676
1702
  } else {
1733
1759
    /* Raise privileges */
1734
1760
    ret_errno = raise_privileges();
1735
1761
    if(ret_errno != 0){
1736
 
      errno = ret_errno;
1737
1762
      perror_plus("Failed to raise privileges");
1738
1763
    }
1739
1764
    
1843
1868
    /* Raise privileges */
1844
1869
    ret_errno = raise_privileges();
1845
1870
    if(ret_errno != 0){
1846
 
      errno = ret_errno;
1847
1871
      perror_plus("Failed to raise privileges");
1848
1872
    }
1849
1873
    
2256
2280
  
2257
2281
  /* If no interfaces were specified, make a list */
2258
2282
  if(mc.interfaces == NULL){
2259
 
    struct dirent **direntries = NULL;
 
2283
    struct dirent **direntries;
2260
2284
    /* Look for any good interfaces */
2261
2285
    ret = scandir(sys_class_net, &direntries, good_interface,
2262
2286
                  alphasort);
2277
2301
      }
2278
2302
      free(direntries);
2279
2303
    } else {
2280
 
      if(ret == 0){
2281
 
        free(direntries);
2282
 
      }
 
2304
      free(direntries);
2283
2305
      fprintf_plus(stderr, "Could not find a network interface\n");
2284
2306
      exitcode = EXIT_FAILURE;
2285
2307
      goto end;
2558
2580
  {
2559
2581
    ret_errno = raise_privileges();
2560
2582
    if(ret_errno != 0){
2561
 
      errno = ret_errno;
2562
2583
      perror_plus("Failed to raise privileges");
2563
2584
    } else {
2564
2585
      
2587
2608
    
2588
2609
    ret_errno = lower_privileges_permanently();
2589
2610
    if(ret_errno != 0){
2590
 
      errno = ret_errno;
2591
2611
      perror_plus("Failed to lower privileges permanently");
2592
2612
    }
2593
2613
  }
2598
2618
  /* Removes the GPGME temp directory and all files inside */
2599
2619
  if(tempdir != NULL){
2600
2620
    struct dirent **direntries = NULL;
2601
 
    int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY |
2602
 
                                                  O_NOFOLLOW));
 
2621
    int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY));
2603
2622
    if(tempdir_fd == -1){
2604
2623
      perror_plus("open");
2605
2624
    } else {
2615
2634
      int numentries = scandir(tempdir, &direntries, notdotentries,
2616
2635
                               alphasort);
2617
2636
#endif  /* not __GLIBC__ */
2618
 
      if(numentries >= 0){
 
2637
      if(numentries > 0){
2619
2638
        for(int i = 0; i < numentries; i++){
2620
2639
          ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
2621
2640
          if(ret == -1){