/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-14 23:43:07 UTC
  • Revision ID: teddy@recompile.se-20140614234307-i0mh9r2n4orkqtki
plugin-runner: Release memory and close FD's correctly in all cases.

* plugins.d/mandos-client.c (main): Init "direntries" to NULL.  Close
                                    "dir_fd" at fallback label.  Do
                                    not free "direntries" if
                                    scandirat() or scandir() failed.
                                    Do free(direntries) at fallback
                                    label.

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;
1561
1554
    if(hook_pid == 0){
1562
1555
      /* Child */
1563
1556
      /* Raise privileges */
1564
 
      errno = raise_privileges_permanently();
1565
 
      if(errno != 0){
 
1557
      if(raise_privileges_permanently() != 0){
1566
1558
        perror_plus("Failed to raise privileges");
1567
1559
        _exit(EX_NOPERM);
1568
1560
      }
1650
1642
        _exit(EXIT_FAILURE);
1651
1643
      }
1652
1644
    } else {
1653
 
      if(hook_pid == -1){
1654
 
        perror_plus("fork");
1655
 
        free(direntry);
1656
 
        continue;
1657
 
      }
1658
1645
      int status;
1659
1646
      if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1660
1647
        perror_plus("waitpid");
1661
 
        free(direntry);
1662
1648
        continue;
1663
1649
      }
1664
1650
      if(WIFEXITED(status)){
1666
1652
          fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1667
1653
                       " with status %d\n", direntry->d_name,
1668
1654
                       WEXITSTATUS(status));
1669
 
          free(direntry);
1670
1655
          continue;
1671
1656
        }
1672
1657
      } else if(WIFSIGNALED(status)){
1673
1658
        fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1674
1659
                     " signal %d\n", direntry->d_name,
1675
1660
                     WTERMSIG(status));
1676
 
        free(direntry);
1677
1661
        continue;
1678
1662
      } else {
1679
1663
        fprintf_plus(stderr, "Warning: network hook \"%s\""
1680
1664
                     " crashed\n", direntry->d_name);
1681
 
        free(direntry);
1682
1665
        continue;
1683
1666
      }
1684
1667
    }
1686
1669
      fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1687
1670
                   direntry->d_name);
1688
1671
    }
1689
 
    free(direntry);
1690
1672
  }
1691
1673
  free(direntries);
1692
1674
  if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1751
1733
    /* Raise privileges */
1752
1734
    ret_errno = raise_privileges();
1753
1735
    if(ret_errno != 0){
1754
 
      errno = ret_errno;
1755
1736
      perror_plus("Failed to raise privileges");
1756
1737
    }
1757
1738
    
1861
1842
    /* Raise privileges */
1862
1843
    ret_errno = raise_privileges();
1863
1844
    if(ret_errno != 0){
1864
 
      errno = ret_errno;
1865
1845
      perror_plus("Failed to raise privileges");
1866
1846
    }
1867
1847
    
2286
2266
        if(ret_errno != 0){
2287
2267
          errno = ret_errno;
2288
2268
          perror_plus("argz_add");
2289
 
          free(direntries[i]);
2290
2269
          continue;
2291
2270
        }
2292
2271
        if(debug){
2293
2272
          fprintf_plus(stderr, "Will use interface \"%s\"\n",
2294
2273
                       direntries[i]->d_name);
2295
2274
        }
2296
 
        free(direntries[i]);
2297
2275
      }
2298
2276
      free(direntries);
2299
2277
    } else {
2569
2547
    mc.current_server->prev->next = NULL;
2570
2548
    while(mc.current_server != NULL){
2571
2549
      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
2550
      free(mc.current_server);
2581
2551
      mc.current_server = next;
2582
2552
    }
2586
2556
  {
2587
2557
    ret_errno = raise_privileges();
2588
2558
    if(ret_errno != 0){
2589
 
      errno = ret_errno;
2590
2559
      perror_plus("Failed to raise privileges");
2591
2560
    } else {
2592
2561
      
2615
2584
    
2616
2585
    ret_errno = lower_privileges_permanently();
2617
2586
    if(ret_errno != 0){
2618
 
      errno = ret_errno;
2619
2587
      perror_plus("Failed to lower privileges permanently");
2620
2588
    }
2621
2589
  }
2651
2619
                         " \"%s\", 0): %s\n", tempdir,
2652
2620
                         direntries[i]->d_name, strerror(errno));
2653
2621
          }
2654
 
          free(direntries[i]);
2655
2622
        }
2656
2623
        
2657
2624
        /* need to clean even if 0 because man page doesn't specify */