osquery-1/osquery/tables/networking/etc_hosts.cpp

57 lines
1.2 KiB
C++
Raw Normal View History

// Copyright 2004-present Facebook. All Rights Reserved.
#include "osquery/tables/networking/etc_hosts.h"
#include <vector>
#include <string>
2014-08-06 22:55:46 +00:00
#include <boost/algorithm/string/join.hpp>
2014-08-04 21:12:06 +00:00
#include <boost/algorithm/string/predicate.hpp>
#include <glog/logging.h>
#include "osquery/core.h"
#include "osquery/filesystem.h"
using namespace osquery::core;
using namespace osquery::db;
2014-08-04 21:12:06 +00:00
using namespace osquery::fs;
namespace osquery { namespace tables {
QueryData genEtcHosts() {
2014-08-04 21:12:06 +00:00
std::string content;
auto s = readFile("/etc/hosts", content);
if (s.ok()) {
return parseEtcHostsContent(content);
} else {
LOG(ERROR) << "Error reading /etc/hosts: " << s.toString();
return {};
}
}
QueryData parseEtcHostsContent(const std::string& content) {
QueryData results;
2014-08-04 21:12:06 +00:00
2014-08-04 23:08:49 +00:00
for (const auto& i : split(content, "\n")) {
auto line = split(i);
2014-08-04 21:12:06 +00:00
if (line.size() == 0 || boost::starts_with(line[0], "#")) {
continue;
}
Row r;
r["address"] = line[0];
2014-08-04 23:08:49 +00:00
if (line.size() > 1) {
std::vector<std::string> hostnames;
for (int i = 1; i < line.size(); ++i) {
hostnames.push_back(line[i]);
}
2014-08-06 22:55:46 +00:00
r["hostnames"] = boost::algorithm::join(hostnames, " ");
2014-08-04 23:08:49 +00:00
}
2014-08-04 21:12:06 +00:00
results.push_back(r);
}
return results;
}
}}