/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release

« 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
  • mto: (237.7.272 trunk)
  • mto: This revision was merged to the branch mainline in revision 317.
  • 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
 
#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
      }
1653
1672
      int status;
1654
1673
      if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1655
1674
        perror_plus("waitpid");
1656
 
        free(direntry);
1657
1675
        continue;
1658
1676
      }
1659
1677
      if(WIFEXITED(status)){
1661
1679
          fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1662
1680
                       " with status %d\n", direntry->d_name,
1663
1681
                       WEXITSTATUS(status));
1664
 
          free(direntry);
1665
1682
          continue;
1666
1683
        }
1667
1684
      } else if(WIFSIGNALED(status)){
1668
1685
        fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1669
1686
                     " signal %d\n", direntry->d_name,
1670
1687
                     WTERMSIG(status));
1671
 
        free(direntry);
1672
1688
        continue;
1673
1689
      } else {
1674
1690
        fprintf_plus(stderr, "Warning: network hook \"%s\""
1675
1691
                     " crashed\n", direntry->d_name);
1676
 
        free(direntry);
1677
1692
        continue;
1678
1693
      }
1679
1694
    }
1681
1696
      fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1682
1697
                   direntry->d_name);
1683
1698
    }
1684
 
    free(direntry);
1685
1699
  }
1686
 
  free(direntries);
1687
1700
  if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1688
1701
    perror_plus("close");
1689
1702
  } else {
1746
1759
    /* Raise privileges */
1747
1760
    ret_errno = raise_privileges();
1748
1761
    if(ret_errno != 0){
1749
 
      errno = ret_errno;
1750
1762
      perror_plus("Failed to raise privileges");
1751
1763
    }
1752
1764
    
1856
1868
    /* Raise privileges */
1857
1869
    ret_errno = raise_privileges();
1858
1870
    if(ret_errno != 0){
1859
 
      errno = ret_errno;
1860
1871
      perror_plus("Failed to raise privileges");
1861
1872
    }
1862
1873
    
2269
2280
  
2270
2281
  /* If no interfaces were specified, make a list */
2271
2282
  if(mc.interfaces == NULL){
2272
 
    struct dirent **direntries = NULL;
 
2283
    struct dirent **direntries;
2273
2284
    /* Look for any good interfaces */
2274
2285
    ret = scandir(sys_class_net, &direntries, good_interface,
2275
2286
                  alphasort);
2281
2292
        if(ret_errno != 0){
2282
2293
          errno = ret_errno;
2283
2294
          perror_plus("argz_add");
2284
 
          free(direntries[i]);
2285
2295
          continue;
2286
2296
        }
2287
2297
        if(debug){
2288
2298
          fprintf_plus(stderr, "Will use interface \"%s\"\n",
2289
2299
                       direntries[i]->d_name);
2290
2300
        }
2291
 
        free(direntries[i]);
2292
2301
      }
2293
2302
      free(direntries);
2294
2303
    } else {
2295
 
      if(ret == 0){
2296
 
        free(direntries);
2297
 
      }
 
2304
      free(direntries);
2298
2305
      fprintf_plus(stderr, "Could not find a network interface\n");
2299
2306
      exitcode = EXIT_FAILURE;
2300
2307
      goto end;
2564
2571
    mc.current_server->prev->next = NULL;
2565
2572
    while(mc.current_server != NULL){
2566
2573
      server *next = mc.current_server->next;
2567
 
#ifdef __GNUC__
2568
 
#pragma GCC diagnostic push
2569
 
#pragma GCC diagnostic ignored "-Wcast-qual"
2570
 
#endif
2571
 
      free((char *)(mc.current_server->ip));
2572
 
#ifdef __GNUC__
2573
 
#pragma GCC diagnostic pop
2574
 
#endif
2575
2574
      free(mc.current_server);
2576
2575
      mc.current_server = next;
2577
2576
    }
2581
2580
  {
2582
2581
    ret_errno = raise_privileges();
2583
2582
    if(ret_errno != 0){
2584
 
      errno = ret_errno;
2585
2583
      perror_plus("Failed to raise privileges");
2586
2584
    } else {
2587
2585
      
2610
2608
    
2611
2609
    ret_errno = lower_privileges_permanently();
2612
2610
    if(ret_errno != 0){
2613
 
      errno = ret_errno;
2614
2611
      perror_plus("Failed to lower privileges permanently");
2615
2612
    }
2616
2613
  }
2621
2618
  /* Removes the GPGME temp directory and all files inside */
2622
2619
  if(tempdir != NULL){
2623
2620
    struct dirent **direntries = NULL;
2624
 
    int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY |
2625
 
                                                  O_NOFOLLOW));
 
2621
    int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY));
2626
2622
    if(tempdir_fd == -1){
2627
2623
      perror_plus("open");
2628
2624
    } else {
2638
2634
      int numentries = scandir(tempdir, &direntries, notdotentries,
2639
2635
                               alphasort);
2640
2636
#endif  /* not __GLIBC__ */
2641
 
      if(numentries >= 0){
 
2637
      if(numentries > 0){
2642
2638
        for(int i = 0; i < numentries; i++){
2643
2639
          ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
2644
2640
          if(ret == -1){
2646
2642
                         " \"%s\", 0): %s\n", tempdir,
2647
2643
                         direntries[i]->d_name, strerror(errno));
2648
2644
          }
2649
 
          free(direntries[i]);
2650
2645
        }
2651
2646
        
2652
2647
        /* need to clean even if 0 because man page doesn't specify */