4d8e7d9b1edcf0283ad006c08409a448766b073e
[platform/core/security/vist.git] / src / osquery / plugins / logger / filesystem_logger.cpp
1 /**
2  *  Copyright (c) 2014-present, Facebook, Inc.
3  *  All rights reserved.
4  *
5  *  This source code is licensed in accordance with the terms specified in
6  *  the LICENSE file found in the root directory of this source tree.
7  */
8
9 #include "filesystem_logger.h"
10
11 #include <exception>
12
13 #include <osquery/flags.h>
14 #include <osquery/logger.h>
15 #include <osquery/utils/config/default_paths.h>
16
17 namespace fs = boost::filesystem;
18
19 /**
20  * This is the mode that Glog uses for logfiles.
21  * Must be at the top level (i.e. outside of the `osquery` namespace).
22  */
23 DECLARE_int32(logfile_mode);
24
25 namespace osquery {
26
27 FLAG(string,
28      logger_path,
29      OSQUERY_LOG_HOME,
30      "Directory path for ERROR/WARN/INFO and results logging");
31 /// Legacy, backward compatible "osquery_log_dir" CLI option.
32 FLAG_ALIAS(std::string, osquery_log_dir, logger_path);
33
34 FLAG(int32, logger_mode, 0640, "Decimal mode for log files (default '0640')");
35
36 const std::string kFilesystemLoggerFilename = "osqueryd.results.log";
37 const std::string kFilesystemLoggerSnapshots = "osqueryd.snapshots.log";
38
39 Status FilesystemLoggerPlugin::setUp() {
40   log_path_ = fs::path(FLAGS_logger_path);
41
42   // Ensure that the Glog status logs use the same mode as our results log.
43   // Glog 0.3.4 does not support a logfile mode.
44   // FLAGS_logfile_mode = FLAGS_logger_mode;
45
46   // Ensure that we create the results log here.
47   return logStringToFile("", kFilesystemLoggerFilename, true);
48 }
49
50 Status FilesystemLoggerPlugin::logString(const std::string& s) {
51   return logStringToFile(s, kFilesystemLoggerFilename);
52 }
53
54 Status FilesystemLoggerPlugin::logStringToFile(const std::string& s,
55                                                const std::string& filename,
56                                                bool empty) {
57   WriteLock lock(mutex_);
58   Status status;
59   try {
60     status = writeTextFile((log_path_ / filename).string(),
61                            (empty) ? "" : s + '\n',
62                            FLAGS_logger_mode);
63   } catch (const std::exception& e) {
64     return Status(1, e.what());
65   }
66   return status;
67 }
68
69 Status FilesystemLoggerPlugin::logStatus(
70     const std::vector<StatusLogLine>& log) {
71   for (const auto& item : log) {
72     // Emit this intermediate log to the Glog filesystem logger.
73     google::LogMessage(item.filename.c_str(),
74                        static_cast<int>(item.line),
75                        (google::LogSeverity)item.severity)
76             .stream()
77         << item.message;
78   }
79
80   return Status(0, "OK");
81 }
82
83 Status FilesystemLoggerPlugin::logSnapshot(const std::string& s) {
84   // Send the snapshot data to a separate filename.
85   return logStringToFile(s, kFilesystemLoggerSnapshots);
86 }
87
88 void FilesystemLoggerPlugin::init(const std::string& name,
89                                   const std::vector<StatusLogLine>& log) {
90   // Stop the internal Glog facilities.
91   google::ShutdownGoogleLogging();
92
93   // The log dir is used for status logging and the filesystem results logs.
94   if (isWritable(log_path_.string()).ok()) {
95     FLAGS_log_dir = log_path_.string();
96     FLAGS_logtostderr = false;
97   } else {
98     // If we cannot write logs to the filesystem, fallback to stderr.
99     // The caller (flags/options) might 'also' be logging to stderr using
100     // debug, verbose, etc.
101     FLAGS_logtostderr = true;
102   }
103
104   // Restart the Glog facilities using the name `init` was provided.
105   google::InitGoogleLogging(name.c_str());
106
107   // We may violate Glog global object assumptions. So set names manually.
108   auto basename = (log_path_ / name).string();
109
110   google::SetLogDestination(google::GLOG_INFO, (basename + ".INFO.").c_str());
111   google::SetLogDestination(google::GLOG_WARNING,
112                             (basename + ".WARNING.").c_str());
113   google::SetLogDestination(google::GLOG_ERROR, (basename + ".ERROR.").c_str());
114
115   // Store settings for logging to stderr.
116   bool log_to_stderr = FLAGS_logtostderr;
117   bool also_log_to_stderr = FLAGS_alsologtostderr;
118   int stderr_threshold = FLAGS_stderrthreshold;
119   FLAGS_alsologtostderr = false;
120   FLAGS_logtostderr = false;
121   FLAGS_stderrthreshold = 5;
122
123   // Now funnel the intermediate status logs provided to `init`.
124   logStatus(log);
125
126   // The filesystem logger cheats and uses Glog to log to the filesystem so
127   // we can return failure here and stop the custom log sink.
128   // Restore settings for logging to stderr.
129   FLAGS_logtostderr = log_to_stderr;
130   FLAGS_alsologtostderr = also_log_to_stderr;
131   FLAGS_stderrthreshold = stderr_threshold;
132 }
133 }