/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-07-14 10:03:10 UTC
  • Revision ID: teddy@recompile.se-20140714100310-omqvn3mujflspggi
mandos.lsm: Fix white space.

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