/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:
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
 
#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
242
    return false;
253
243
  }
254
244
  /* Special case of first server */
1076
1066
     timed out */
1077
1067
  
1078
1068
  if(quit_now){
1079
 
    avahi_s_service_resolver_free(r);
1080
1069
    return;
1081
1070
  }
1082
1071
  
1469
1458
  error_t ret_errno = 0;
1470
1459
  if(seteuid(0) == -1){
1471
1460
    ret_errno = errno;
 
1461
    perror_plus("seteuid");
1472
1462
  }
1473
1463
  errno = old_errno;
1474
1464
  return ret_errno;
1485
1475
  }
1486
1476
  if(setuid(0) == -1){
1487
1477
    ret_errno = errno;
 
1478
    perror_plus("seteuid");
1488
1479
  }
1489
1480
  errno = old_errno;
1490
1481
  return ret_errno;
1497
1488
  error_t ret_errno = 0;
1498
1489
  if(seteuid(uid) == -1){
1499
1490
    ret_errno = errno;
 
1491
    perror_plus("seteuid");
1500
1492
  }
1501
1493
  errno = old_errno;
1502
1494
  return ret_errno;
1509
1501
  error_t ret_errno = 0;
1510
1502
  if(setuid(uid) == -1){
1511
1503
    ret_errno = errno;
 
1504
    perror_plus("setuid");
1512
1505
  }
1513
1506
  errno = old_errno;
1514
1507
  return ret_errno;
1515
1508
}
1516
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
 
1517
1529
__attribute__((nonnull))
1518
1530
void run_network_hooks(const char *mode, const char *interface,
1519
1531
                       const float delay){
1520
 
  struct dirent **direntries = NULL;
 
1532
  struct dirent **direntries;
1521
1533
  if(hookdir_fd == -1){
1522
 
    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
                      );
1523
1541
    if(hookdir_fd == -1){
1524
1542
      if(errno == ENOENT){
1525
1543
        if(debug){
1531
1549
      }
1532
1550
      return;
1533
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 */
1534
1563
  }
1535
1564
#ifdef __GLIBC__
1536
1565
#if __GLIBC_PREREQ(2, 15)
1561
1590
    if(hook_pid == 0){
1562
1591
      /* Child */
1563
1592
      /* Raise privileges */
1564
 
      errno = raise_privileges_permanently();
1565
 
      if(errno != 0){
 
1593
      if(raise_privileges_permanently() != 0){
1566
1594
        perror_plus("Failed to raise privileges");
1567
1595
        _exit(EX_NOPERM);
1568
1596
      }
1635
1663
          _exit(EX_OSERR);
1636
1664
        }
1637
1665
      }
1638
 
      int hook_fd = openat(hookdir_fd, direntry->d_name, O_RDONLY);
1639
 
      if(hook_fd == -1){
1640
 
        perror_plus("openat");
1641
 
        _exit(EXIT_FAILURE);
1642
 
      }
1643
 
      if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1644
 
        perror_plus("close");
1645
 
        _exit(EXIT_FAILURE);
1646
 
      }
1647
 
      if(fexecve(hook_fd, (char *const []){ direntry->d_name, NULL },
1648
 
                 environ) == -1){
 
1666
      if(fexecve(hookdir_fd, (char *const [])
 
1667
                 { direntry->d_name, NULL }, environ) == -1){
1649
1668
        perror_plus("fexecve");
1650
1669
        _exit(EXIT_FAILURE);
1651
1670
      }
1652
1671
    } else {
1653
 
      if(hook_pid == -1){
1654
 
        perror_plus("fork");
1655
 
        free(direntry);
1656
 
        continue;
1657
 
      }
1658
1672
      int status;
1659
1673
      if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1660
1674
        perror_plus("waitpid");
1661
 
        free(direntry);
1662
1675
        continue;
1663
1676
      }
1664
1677
      if(WIFEXITED(status)){
1666
1679
          fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1667
1680
                       " with status %d\n", direntry->d_name,
1668
1681
                       WEXITSTATUS(status));
1669
 
          free(direntry);
1670
1682
          continue;
1671
1683
        }
1672
1684
      } else if(WIFSIGNALED(status)){
1673
1685
        fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1674
1686
                     " signal %d\n", direntry->d_name,
1675
1687
                     WTERMSIG(status));
1676
 
        free(direntry);
1677
1688
        continue;
1678
1689
      } else {
1679
1690
        fprintf_plus(stderr, "Warning: network hook \"%s\""
1680
1691
                     " crashed\n", direntry->d_name);
1681
 
        free(direntry);
1682
1692
        continue;
1683
1693
      }
1684
1694
    }
1686
1696
      fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1687
1697
                   direntry->d_name);
1688
1698
    }
1689
 
    free(direntry);
1690
1699
  }
1691
 
  free(direntries);
1692
1700
  if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1693
1701
    perror_plus("close");
1694
1702
  } else {
1751
1759
    /* Raise privileges */
1752
1760
    ret_errno = raise_privileges();
1753
1761
    if(ret_errno != 0){
1754
 
      errno = ret_errno;
1755
1762
      perror_plus("Failed to raise privileges");
1756
1763
    }
1757
1764
    
1861
1868
    /* Raise privileges */
1862
1869
    ret_errno = raise_privileges();
1863
1870
    if(ret_errno != 0){
1864
 
      errno = ret_errno;
1865
1871
      perror_plus("Failed to raise privileges");
1866
1872
    }
1867
1873
    
2274
2280
  
2275
2281
  /* If no interfaces were specified, make a list */
2276
2282
  if(mc.interfaces == NULL){
2277
 
    struct dirent **direntries = NULL;
 
2283
    struct dirent **direntries;
2278
2284
    /* Look for any good interfaces */
2279
2285
    ret = scandir(sys_class_net, &direntries, good_interface,
2280
2286
                  alphasort);
2286
2292
        if(ret_errno != 0){
2287
2293
          errno = ret_errno;
2288
2294
          perror_plus("argz_add");
2289
 
          free(direntries[i]);
2290
2295
          continue;
2291
2296
        }
2292
2297
        if(debug){
2293
2298
          fprintf_plus(stderr, "Will use interface \"%s\"\n",
2294
2299
                       direntries[i]->d_name);
2295
2300
        }
2296
 
        free(direntries[i]);
2297
2301
      }
2298
2302
      free(direntries);
2299
2303
    } else {
2300
 
      if(ret == 0){
2301
 
        free(direntries);
2302
 
      }
 
2304
      free(direntries);
2303
2305
      fprintf_plus(stderr, "Could not find a network interface\n");
2304
2306
      exitcode = EXIT_FAILURE;
2305
2307
      goto end;
2569
2571
    mc.current_server->prev->next = NULL;
2570
2572
    while(mc.current_server != NULL){
2571
2573
      server *next = mc.current_server->next;
2572
 
#ifdef __GNUC__
2573
 
#pragma GCC diagnostic push
2574
 
#pragma GCC diagnostic ignored "-Wcast-qual"
2575
 
#endif
2576
 
      free((char *)(mc.current_server->ip));
2577
 
#ifdef __GNUC__
2578
 
#pragma GCC diagnostic pop
2579
 
#endif
2580
2574
      free(mc.current_server);
2581
2575
      mc.current_server = next;
2582
2576
    }
2586
2580
  {
2587
2581
    ret_errno = raise_privileges();
2588
2582
    if(ret_errno != 0){
2589
 
      errno = ret_errno;
2590
2583
      perror_plus("Failed to raise privileges");
2591
2584
    } else {
2592
2585
      
2615
2608
    
2616
2609
    ret_errno = lower_privileges_permanently();
2617
2610
    if(ret_errno != 0){
2618
 
      errno = ret_errno;
2619
2611
      perror_plus("Failed to lower privileges permanently");
2620
2612
    }
2621
2613
  }
2626
2618
  /* Removes the GPGME temp directory and all files inside */
2627
2619
  if(tempdir != NULL){
2628
2620
    struct dirent **direntries = NULL;
2629
 
    int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY |
2630
 
                                                  O_NOFOLLOW));
 
2621
    int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY));
2631
2622
    if(tempdir_fd == -1){
2632
2623
      perror_plus("open");
2633
2624
    } else {
2634
 
#ifdef __GLIBC__
2635
 
#if __GLIBC_PREREQ(2, 15)
2636
 
      int numentries = scandirat(tempdir_fd, ".", &direntries,
2637
 
                                 notdotentries, alphasort);
2638
 
#else  /* not __GLIBC_PREREQ(2, 15) */
2639
 
      int numentries = scandir(tempdir, &direntries, notdotentries,
2640
 
                               alphasort);
2641
 
#endif  /* not __GLIBC_PREREQ(2, 15) */
2642
 
#else   /* not __GLIBC__ */
2643
 
      int numentries = scandir(tempdir, &direntries, notdotentries,
2644
 
                               alphasort);
2645
 
#endif  /* not __GLIBC__ */
2646
 
      if(numentries >= 0){
 
2625
      int numentries = scandir(tempdir, &direntries, notdotentries,
 
2626
                               alphasort);
 
2627
      if(numentries > 0){
2647
2628
        for(int i = 0; i < numentries; i++){
2648
2629
          ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
2649
2630
          if(ret == -1){
2651
2632
                         " \"%s\", 0): %s\n", tempdir,
2652
2633
                         direntries[i]->d_name, strerror(errno));
2653
2634
          }
2654
 
          free(direntries[i]);
2655
2635
        }
2656
2636
        
2657
2637
        /* need to clean even if 0 because man page doesn't specify */