Remove gflags dependency on osquery
[platform/core/security/vist.git] / src / osquery / logger / plugins / filesystem.cpp
1 /*
2  *  Copyright (c) 2014, Facebook, Inc.
3  *  All rights reserved.
4  *
5  *  This source code is licensed under the BSD-style license found in the
6  *  LICENSE file in the root directory of this source tree. An additional grant 
7  *  of patent rights can be found in the PATENTS file in the same directory.
8  *
9  */
10
11 #include <exception>
12 #include <mutex>
13
14 #include <osquery/filesystem.h>
15 #include <osquery/logger.h>
16
17 namespace pt = boost::property_tree;
18 namespace fs = boost::filesystem;
19
20 namespace osquery {
21
22 const std::string kFilesystemLoggerFilename = "osqueryd.results.log";
23 const std::string kFilesystemLoggerSnapshots = "osqueryd.snapshots.log";
24 const std::string kFilesystemLoggerHealth = "osqueryd.health.log";
25
26 std::mutex filesystemLoggerPluginMutex;
27
28 class FilesystemLoggerPlugin : public LoggerPlugin {
29  public:
30   Status setUp();
31   Status logString(const std::string& s);
32   Status logStringToFile(const std::string& s, const std::string& filename);
33   Status logSnapshot(const std::string& s);
34   Status logHealth(const std::string& s);
35   Status init(const std::string& name, const std::vector<StatusLogLine>& log);
36   Status logStatus(const std::vector<StatusLogLine>& log);
37
38  private:
39   fs::path log_path_;
40 };
41
42 REGISTER(FilesystemLoggerPlugin, "logger", "filesystem");
43
44 Status FilesystemLoggerPlugin::setUp() {
45   return Status(0, "OK");
46 }
47
48 Status FilesystemLoggerPlugin::logString(const std::string& s) {
49   return logStringToFile(s, kFilesystemLoggerFilename);
50 }
51
52 Status FilesystemLoggerPlugin::logStringToFile(const std::string& s,
53                                                const std::string& filename) {
54   std::lock_guard<std::mutex> lock(filesystemLoggerPluginMutex);
55   try {
56     // The results log may contain sensitive information if run as root.
57     auto status = writeTextFile((log_path_ / filename).string(), s, 0640, true);
58     if (!status.ok()) {
59       return status;
60     }
61   } catch (const std::exception& e) {
62     return Status(1, e.what());
63   }
64   return Status(0, "OK");
65 }
66
67 Status FilesystemLoggerPlugin::logStatus(
68     const std::vector<StatusLogLine>& log) {
69   for (const auto& item : log) {
70     // Emit this intermediate log to the Glog filesystem logger.
71     google::LogMessage(item.filename.c_str(),
72                        item.line,
73                        (google::LogSeverity)item.severity).stream()
74         << item.message;
75   }
76
77   return Status(0, "OK");
78 }
79
80 Status FilesystemLoggerPlugin::logSnapshot(const std::string& s) {
81   // Send the snapshot data to a separate filename.
82   return logStringToFile(s, kFilesystemLoggerSnapshots);
83 }
84
85 Status FilesystemLoggerPlugin::logHealth(const std::string& s) {
86   return logStringToFile(s, kFilesystemLoggerHealth);
87 }
88
89 Status FilesystemLoggerPlugin::init(const std::string& name,
90                                     const std::vector<StatusLogLine>& log) {
91   // Stop the internal Glog facilities.
92   google::ShutdownGoogleLogging();
93
94   // Restart the Glog facilities using the name `init` was provided.
95   google::InitGoogleLogging(name.c_str());
96
97   // We may violate Glog global object assumptions. So set names manually.
98   auto basename = (log_path_ / name).string();
99   google::SetLogDestination(google::INFO, (basename + ".INFO.").c_str());
100   google::SetLogDestination(google::WARNING, (basename + ".WARNING.").c_str());
101   google::SetLogDestination(google::ERROR, (basename + ".ERROR.").c_str());
102
103   // Now funnel the intermediate status logs provided to `init`.
104   logStatus(log);
105
106   // The filesystem logger cheats and uses Glog to log to the filesystem so
107   // we can return failure here and stop the custom log sink.
108   return Status(1, "No status logger used for filesystem");
109 }
110 }