Add log management classes and remove netlink-related things 03/165603/11
authorSungbae Yoo <sungbae.yoo@samsung.com>
Thu, 15 Mar 2018 10:28:27 +0000 (19:28 +0900)
committerSungbae Yoo <sungbae.yoo@samsung.com>
Thu, 22 Mar 2018 02:34:04 +0000 (11:34 +0900)
Parsing the netlink message header was moved into klay.

Signed-off-by: Sungbae Yoo <sungbae.yoo@samsung.com>
Change-Id: I8eab57a27cb62d9e93d7af3caf597a4946f2402a

20 files changed:
common/CMakeLists.txt
common/audit/audit-log-builder.h [new file with mode: 0644]
common/audit/audit-logger.h [new file with mode: 0644]
common/audit/audit-message-parser.cpp [new file with mode: 0644]
common/audit/audit-message-parser.h [new file with mode: 0644]
common/audit/audit-rule.h
common/audit/audit-system-log.cpp [new file with mode: 0644]
common/audit/audit-system-log.h [new file with mode: 0644]
common/audit/audit-user-log.cpp [new file with mode: 0644]
common/audit/audit-user-log.h [new file with mode: 0644]
common/audit/audit.cpp
common/audit/audit.h
lib/CMakeLists.txt
lib/audit-trail/rule-management.cpp [deleted file]
lib/audit-trail/rule-management.h [deleted file]
lib/audit-trail/rule.cpp [new file with mode: 0644]
lib/audit-trail/rule.h [new file with mode: 0644]
server/rule-management.cpp
server/server.cpp
server/server.h

index 89eb13a88768b3081a9e5c2ac75378a0172caafe..b943b36de2c64146860a615ddd044f8355c0c928 100644 (file)
@@ -15,6 +15,9 @@
 #
 SET(COMMON_SRCS        audit/audit.cpp
                                audit/audit-rule.cpp
+                               audit/audit-user-log.cpp
+                               audit/audit-system-log.cpp
+                               audit/audit-message-parser.cpp
 )
 
 SET(DEPENDENCY klay
diff --git a/common/audit/audit-log-builder.h b/common/audit/audit-log-builder.h
new file mode 100644 (file)
index 0000000..cfd80ef
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ *  Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+
+#ifndef __AUDIT_TRAIL_AUDIT_LOG_BUILDER_H__
+#define __AUDIT_TRAIL_AUDIT_LOG_BUILDER_H__
+
+#include <string>
+
+template <typename T>
+class AuditLogBuilder final {
+public:
+       AuditLogBuilder() :
+               completed(false)
+       {
+       }
+
+       void addMessage(int type, const std::string &log);
+
+       bool isCompleted() const
+        {
+               return completed;
+       }
+
+       T pop();
+
+private:
+       bool completed;
+       T instance;
+};
+
+#endif //!__AUDIT_TRAIL_AUDIT_LOG_BUILDER_H__
diff --git a/common/audit/audit-logger.h b/common/audit/audit-logger.h
new file mode 100644 (file)
index 0000000..906fff5
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ *  Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+
+#ifndef __AUDIT_TRAIL_AUDIT_LOGGER_H__
+#define __AUDIT_TRAIL_AUDIT_LOGGER_H__
+
+#include <string>
+#include <vector>
+#include <algorithm>
+
+#include "audit/audit-log-builder.h"
+
+template <typename T>
+class AuditLogger final {
+public:
+       const std::vector<T> &get() const
+       {
+               return logs;
+       }
+
+       unsigned int size()
+       {
+               return logs.size();
+       }
+
+       void clear()
+       {
+               logs.clear();
+       }
+
+       void setCallback(std::function<void(T&)> cb)
+       {
+               callback = cb;
+       }
+
+       void unsetCallback()
+       {
+               callback = nullptr;
+       }
+
+       void addMessage(int type, const std::string &log)
+       {
+               builder.addMessage(type, log);
+               if (builder.isCompleted()) {
+                       logs.push_back(builder.pop());
+                       if (callback)
+                               callback(logs.back());
+               }
+       }
+
+private:
+       std::vector<T> logs;
+       AuditLogBuilder<T> builder;
+       std::function<void(T&)> callback;
+
+};
+
+#endif //!__AUDIT_TRAIL_AUDIT_LOGGER_H__
diff --git a/common/audit/audit-message-parser.cpp b/common/audit/audit-message-parser.cpp
new file mode 100644 (file)
index 0000000..63f9cf3
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ *  Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+#include <poll.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <linux/audit.h>
+
+#include <cstring>
+#include <sstream>
+#include <iostream>
+
+#include <klay/error.h>
+#include <klay/exception.h>
+
+#include "audit-message-parser.h"
+
+AuditMessageParser::AuditMessageParser(Audit &audit, runtime::Mainloop &ml) :
+       nl(audit.nl), mainloop(ml)
+{
+       int fd = nl.getFd();
+
+       mainloop.removeEventSource(fd);
+
+       mainloop.addEventSource(nl.getFd(), POLLIN,
+                                                               [this](int fd, runtime::Mainloop::Event event) {
+                                                                       parse();
+                                                               });
+}
+
+AuditMessageParser::~AuditMessageParser()
+{
+       mainloop.removeEventSource(nl.getFd());
+}
+
+
+
+void AuditMessageParser::parse()
+{
+       while (1) {
+               try {
+                       auto msg = nl.recv(MSG_DONTWAIT);
+
+                       int type = msg.first;
+                       std::string log(msg.second.begin(), msg.second.end());
+
+                       userLogs.addMessage(type, log);
+                       systemLogs.addMessage(type, log);
+               } catch (runtime::Exception &e) {
+                       break;
+               }
+       }
+}
diff --git a/common/audit/audit-message-parser.h b/common/audit/audit-message-parser.h
new file mode 100644 (file)
index 0000000..579a854
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ *  Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+
+#ifndef __AUDIT_TRAIL_AUDIT_MESSAGE_PARSER_H__
+#define __AUDIT_TRAIL_AUDIT_MESSAGE_PARSER_H__
+
+#include <vector>
+
+#include <klay/mainloop.h>
+#include <klay/netlink/netlink.h>
+
+#include "audit/audit.h"
+
+#include "audit/audit-logger.h"
+#include "audit/audit-user-log.h"
+#include "audit/audit-system-log.h"
+
+class AuditMessageParser final {
+public:
+       AuditMessageParser(Audit &audit, runtime::Mainloop &ml);
+       AuditMessageParser(AuditMessageParser&&) = delete;
+       AuditMessageParser(const AuditMessageParser&) = delete;
+       ~AuditMessageParser();
+
+       AuditLogger<AuditUserLog> userLogs;
+       AuditLogger<AuditSystemLog> systemLogs;
+
+private:
+       void parse();
+
+       netlink::Netlink &nl;
+       runtime::Mainloop &mainloop;
+};
+
+#endif //!__AUDIT_TRAIL_AUDIT_MESSAGE_PARSER_H__
index 6c24928d39c0c080c0266e05d1dfd6735ca1e93e..92c2b0b63e9565293309a67831c81c6e74f36b81 100644 (file)
@@ -92,15 +92,11 @@ public:
        template <typename T>
        std::vector<Condition<T>> getConditions() const;
 
-       const char *data() const
+       const std::vector<char> &data() const
        {
-               return buf.data();
+               return buf;
        }
 
-       unsigned int size() const
-       {
-               return buf.size();
-       }
 
 private:
        std::vector<char> buf;
diff --git a/common/audit/audit-system-log.cpp b/common/audit/audit-system-log.cpp
new file mode 100644 (file)
index 0000000..9e44e6e
--- /dev/null
@@ -0,0 +1,207 @@
+/*
+ *  Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+#include <poll.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <linux/audit.h>
+
+#include <cstring>
+#include <sstream>
+#include <iostream>
+
+#include <klay/error.h>
+#include <klay/exception.h>
+
+#include "audit-system-log.h"
+#include "audit-log-builder.h"
+
+namespace {
+
+std::pair<std::string, std::string> getNameValuePair(std::istream &stream)
+{
+       std::string name, value, word;
+
+       if (getline(stream, word, ' ')) {
+               size_t equal = word.find_first_of('=');
+               name = word.substr(0, equal);
+               value = word.substr(equal + 1);
+
+               if (value.front() == '\"') {
+                       if (value.back() != '\"') {
+                               getline(stream, word, '\"');
+                               value += ' ' + word + '\"';
+                       }
+                       value = value.substr(1, value.size() - 2);
+               } else if (value.front() == '\'') {
+                       if (value.back() != '\'') {
+                               getline(stream, word, '\'');
+                               value += ' ' + word + '\'';
+                       }
+                       value = value.substr(1, value.size() - 2);
+               }
+       }
+
+       return std::make_pair(name, value);
+}
+
+}// namespace
+
+template <>
+void AuditLogBuilder<AuditSystemLog>::addMessage(int type, const std::string &log)
+{
+       if (type < AUDIT_SYSCALL || type > AUDIT_AVC) {
+               return;
+       }
+
+       switch (type) {
+       case AUDIT_EOE:
+               {
+                       std::string word = log.substr(sizeof("audit(") - 1);
+                       size_t dot = word.find_first_of('.');
+                       instance.time.time = std::stoll(word.substr(0, dot));
+                       instance.time.millisec = std::stoi(word.substr(dot + 1, 3));
+
+                       completed = true;
+               }
+               break;
+       case AUDIT_SYSCALL:
+               {
+                       std::stringstream tok(log);
+                       while (!tok.eof()) {
+                               auto pair = getNameValuePair(tok);
+                               const auto &name = pair.first;
+                               const auto &value = pair.second;
+
+                               if (name == "syscall") { /* action */
+                                       instance.action.systemCall = std::stoul(value);
+                               } else if (name == "exit") {
+                                       instance.action.exitCode = std::stoi(value);
+                               } else if (name == "a0") {
+                                       instance.action.args[0] = std::stoul(value, 0, 16);
+                               } else if (name == "a1") {
+                                       instance.action.args[1] = std::stoul(value, 0, 16);
+                               } else if (name == "a2") {
+                                       instance.action.args[2] = std::stoul(value, 0, 16);
+                               } else if (name == "a3") {
+                                       instance.action.args[3] = std::stoul(value, 0, 16);
+                               } else if (name == "uid") { /* subject */
+                                       instance.subject.uid = std::stoul(value);
+                               } else if (name == "euid") {
+                                       instance.subject.euid = std::stoul(value);
+                               } else if (name == "gid") {
+                                       instance.subject.gid = std::stoul(value);
+                               } else if (name == "egid") {
+                                       instance.subject.egid = std::stoul(value);
+                               } else if (name == "pid") {
+                                       instance.subject.pid = std::stoul(value);
+                               } else if (name == "exe") {
+                                       instance.subject.name = value;
+                               } else if (name == "subj") {
+                                       instance.subject.label = value;
+                               } else if (name == "key") { /* tag */
+                                       if (value != "(null)" && instance.tag != "smack") {
+                                               instance.tag = value;
+                                       }
+                               }
+                       }
+               }
+               break;
+       case AUDIT_AVC:
+               {
+                       std::stringstream tok(log);
+                       while (!tok.eof()) {
+                               auto pair = getNameValuePair(tok);
+                               const auto &name = pair.first;
+                               const auto &value = pair.second;
+
+                               if (name == "object") { /* object */
+                                       instance.object.label = value;
+                               } else if (name == "path") {
+                                       instance.object.name = value;
+                                       instance.object.type = AuditSystemLog::FileObject;
+                               } else if (name == "path") {
+                                       instance.object.name = value;
+                                       instance.object.type = AuditSystemLog::FileObject;
+                               } else if (name == "inode") {
+                                       instance.object.inode = std::stoul(value);
+                                       instance.object.type = AuditSystemLog::FileObject;
+                               }
+                       }
+                       instance.tag = "smack";
+               }
+               break;
+       case AUDIT_PATH:
+               {
+                       std::stringstream tok(log);
+                       while (!tok.eof()) {
+                               auto pair = getNameValuePair(tok);
+                               const auto &name = pair.first;
+                               const auto &value = pair.second;
+
+                               if (name == "name") { /* object */
+                                       instance.object.name = value;
+                               } else if (name == "obj") {
+                                       instance.object.label = value;
+                               } else if (name == "inode") {
+                                       instance.object.inode = std::stoul(value);
+                               } else if (name == "mode") {
+                                       instance.object.mode = std::stoul(value, 0, 8);
+                               } else if (name == "ouid") {
+                                       instance.subject.uid = std::stoul(value);
+                               } else if (name == "ogid") {
+                                       instance.subject.gid = std::stoul(value);
+                               }
+                       }
+                       instance.object.type = AuditSystemLog::FileObject;
+               }
+               break;
+       case AUDIT_OBJ_PID:
+               {
+                       std::stringstream tok(log);
+                       while (!tok.eof()) {
+                               auto pair = getNameValuePair(tok);
+                               const auto &name = pair.first;
+                               const auto &value = pair.second;
+
+                               if (name == "ocomm") { /* object */
+                                       instance.object.name = value;
+                               } else if (name == "obj") {
+                                       instance.object.label = value;
+                               } else if (name == "opid") {
+                                       instance.object.inode = std::stoul(value);
+                               } else if (name == "ouid") {
+                                       instance.object.uid = std::stoul(value);
+                               }
+                       }
+                       instance.object.type = AuditSystemLog::ProcessObject;
+               }
+               break;
+       default:
+               break;
+       }
+}
+
+template <>
+AuditSystemLog AuditLogBuilder<AuditSystemLog>::pop()
+{
+       AuditSystemLog result = instance;
+
+       instance = AuditSystemLog();
+       completed = false;
+
+       return result;
+}
diff --git a/common/audit/audit-system-log.h b/common/audit/audit-system-log.h
new file mode 100644 (file)
index 0000000..1bbeb6b
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ *  Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+
+#ifndef __AUDIT_TRAIL_AUDIT_SYSTEM_LOG_H__
+#define __AUDIT_TRAIL_AUDIT_SYSTEM_LOG_H__
+
+#include <string>
+#include <limits.h>
+
+struct AuditSystemLog final {
+       enum ObjectType {
+               NoObject,
+               ProcessObject,
+               FileObject,
+       };
+
+       std::string tag;
+       struct {
+               time_t time = 0;
+               unsigned short millisec = 0;
+       } time;
+
+       struct {
+               uid_t uid = UINT_MAX, euid = UINT_MAX;
+               gid_t gid = UINT_MAX, egid = UINT_MAX;
+               std::string label;
+               std::string name;
+               pid_t pid = UINT_MAX;
+       } subject;
+
+       struct {
+               int type = NoObject;
+               uid_t uid = UINT_MAX;
+               gid_t gid = UINT_MAX;
+               mode_t mode = UINT_MAX;
+               std::string label;
+               std::string name;
+               pid_t pid = UINT_MAX;
+               ino_t inode = UINT_MAX;
+       } object;
+
+       struct {
+               unsigned int systemCall = 0;
+               unsigned int args[4] = {0,};
+               int exitCode = 0;
+       } action;
+};
+
+#endif //!__AUDIT_TRAIL_AUDIT_SYSTEM_LOG_H__
diff --git a/common/audit/audit-user-log.cpp b/common/audit/audit-user-log.cpp
new file mode 100644 (file)
index 0000000..6a9b2f9
--- /dev/null
@@ -0,0 +1,105 @@
+/*
+ *  Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+#include <poll.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <linux/audit.h>
+
+#include <cstring>
+#include <sstream>
+
+#include <klay/error.h>
+#include <klay/exception.h>
+
+#include "audit-user-log.h"
+#include "audit-log-builder.h"
+
+namespace {
+
+std::pair<std::string, std::string> getNameValuePair(std::istream &stream)
+{
+       std::string name, value, word;
+
+       if (getline(stream, word, ' ')) {
+               size_t equal = word.find_first_of('=');
+               name = word.substr(0, equal);
+               value = word.substr(equal + 1);
+
+               if (value.front() == '\"') {
+                       if (value.back()!= '\"') {
+                               getline(stream, word, '\"');
+                               value += ' ' + word + '\"';
+                       }
+                       value = value.substr(1, value.size() - 2);
+               }
+               if (value.front() == '\'') {
+                       if (value.back()!= '\'') {
+                               getline(stream, word, '\'');
+                               value += ' ' + word + '\'';
+                       }
+                       value = value.substr(1, value.size() - 2);
+               }
+       }
+
+       return std::make_pair(name, value);
+}
+
+}// namespace
+
+template <>
+void AuditLogBuilder<AuditUserLog>::addMessage(int type, const std::string &log)
+{
+       if (!(type >= AUDIT_FIRST_USER_MSG && type <= AUDIT_LAST_USER_MSG) &&
+                       !(type >= AUDIT_FIRST_USER_MSG2 && type <= AUDIT_LAST_USER_MSG2)) {
+               return;
+       }
+
+       std::stringstream tok(log);
+       std::string word, msg;
+
+       getline(tok, word, ' ');
+       word = word.substr(sizeof("audit(") - 1);
+       size_t dot = word.find_first_of('.');
+       instance.time.time = std::stoll(word.substr(0, dot));
+       instance.time.millisec = std::stoi(word.substr(dot + 1, 3));
+
+       while (!tok.eof()) {
+               auto pair = getNameValuePair(tok);
+               const auto &name = pair.first;
+               const auto &value = pair.second;
+
+               if (name == "msg") {
+                       msg = value;
+               }
+       }
+
+       instance.log.type = type;
+       instance.log.text = msg;
+
+       completed = true;
+}
+
+template <>
+AuditUserLog AuditLogBuilder<AuditUserLog>::pop()
+{
+       AuditUserLog result = instance;
+
+       instance = AuditUserLog();
+       completed = false;
+
+       return result;
+}
diff --git a/common/audit/audit-user-log.h b/common/audit/audit-user-log.h
new file mode 100644 (file)
index 0000000..b28931d
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ *  Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+
+#ifndef __AUDIT_TRAIL_AUDIT_USER_LOG_H__
+#define __AUDIT_TRAIL_AUDIT_USER_LOG_H__
+
+#include <string>
+
+struct AuditUserLog final {
+       struct {
+               time_t time = 0;
+               unsigned short millisec = 0;
+       } time;
+       struct {
+               int type = 0;
+               std::string text;
+       } log;
+};
+
+#endif //!__AUDIT_TRAIL_AUDIT_USER_LOG_H__
index 76f61cfa080ceb0f1fa7c1d54da64bddb5876f11..5bdf0ed6d93389324338209a933aeeeba75e60fb 100644 (file)
 
 #include "audit.h"
 
-using namespace std::placeholders;
+namespace {
 
-Audit::Audit() :
-       nl(NETLINK_AUDIT), sequence(0), mainloop(nullptr)
-{
+template <typename T>
+std::vector<T> arrayToVector(T *buf, size_t size) {
+       return std::vector<T>(buf, buf + size);
 }
 
-Audit::Audit(Audit&& audit) :
-       nl(std::move(audit.nl)), sequence(audit.sequence), mainloop(audit.mainloop)
+};
+
+Audit::Audit() :
+       nl(NETLINK_AUDIT)
 {
 }
 
 Audit::~Audit()
 {
-       if (mainloop != nullptr) {
-               mainloop->removeEventSource(nl.getFd());
-       }
 }
 
 void Audit::setPID(pid_t pid)
@@ -52,7 +51,10 @@ void Audit::setPID(pid_t pid)
        s.mask  = AUDIT_STATUS_PID;
        s.pid   = pid;
 
-       send(AUDIT_SET, &s, sizeof(s));
+       nl.send(AUDIT_SET, arrayToVector((char*)&s, sizeof(s)));
+
+       //Remove the log for a PID change
+       nl.recv();
 }
 
 void Audit::setEnabled(int enabled)
@@ -63,21 +65,18 @@ void Audit::setEnabled(int enabled)
        s.mask          = AUDIT_STATUS_ENABLED;
        s.enabled       = enabled;
 
-       send(AUDIT_SET, &s, sizeof(s));
+       nl.send(AUDIT_SET, arrayToVector((char*)&s, sizeof(s)));
 }
 
 int Audit::isEnabled()
 {
        int ret = 0;
-       send(AUDIT_GET, NULL, 0);
+       nl.send(AUDIT_GET, std::vector<char>());
 
        while (1) {
-               auto msg = recv();
+               auto msg = nl.recv();
 
                switch (msg.first) {
-               case NLMSG_DONE:
-               case NLMSG_ERROR:
-                       throw runtime::Exception("Failed to get audit state");
                case AUDIT_GET:
                        ret = ((struct audit_status*)msg.second.data())->enabled;
                        break;
@@ -95,16 +94,14 @@ std::vector<AuditRule> Audit::getRules()
 {
        std::vector<AuditRule> ret;
 
-       send(AUDIT_LIST_RULES, NULL, 0);
+       nl.send(AUDIT_LIST_RULES, std::vector<char>());
 
        while (1) {
-               auto msg = recv();
+               auto msg = nl.recv();
 
                switch (msg.first) {
                case NLMSG_DONE:
                        break;
-               case NLMSG_ERROR:
-                       throw runtime::Exception("Failed to get a list of audit rules");
                case AUDIT_LIST_RULES:
                        ret.push_back(msg.second);
                default:
@@ -118,103 +115,10 @@ std::vector<AuditRule> Audit::getRules()
 
 void Audit::addRule(const AuditRule& rule)
 {
-       send(AUDIT_ADD_RULE, rule.data(), rule.size());
+       nl.send(AUDIT_ADD_RULE, rule.data());
 }
 
 void Audit::removeRule(const AuditRule& rule)
 {
-       send(AUDIT_DEL_RULE, rule.data(), rule.size());
-}
-
-void Audit::setMainloop(runtime::Mainloop *ml)
-{
-       int fd = nl.getFd();
-
-       if (mainloop != nullptr) {
-               mainloop->removeEventSource(fd);
-       }
-
-       mainloop = ml;
-       mainloop->addEventSource(fd, POLLIN, std::bind(&Audit::processMessage,
-                                                                                                       this, _1, _2));
-}
-
-void Audit::setMessageHandler(MessageHandler&& handler)
-{
-       messageHandler = handler;
-}
-
-void Audit::send(int type, const void *data, unsigned int size)
-{
-       char buf[NLMSG_SPACE(size)];
-       auto *nlh = (struct nlmsghdr *)buf;
-
-       ::memset(buf, 0, sizeof(buf));
-
-       nlh->nlmsg_len = sizeof(buf);
-       nlh->nlmsg_type = type;
-       nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
-       nlh->nlmsg_seq = ++sequence;
-
-       if (sequence == 0) {
-               sequence++;
-       }
-
-       if (size && data) {
-               ::memcpy(NLMSG_DATA(buf), data, size);
-       }
-
-       nl.send(buf, sizeof(buf));
-
-       if (recv(MSG_PEEK).first == NLMSG_ERROR) {
-               auto reply = recv().second;
-               auto err = (struct nlmsgerr*)reply.data();
-               if (err->error) {
-                       throw runtime::Exception("Audit netlink error: " +
-                                                                               std::to_string(err->error));
-               }
-       }
-}
-
-Audit::Message Audit::recv(int options)
-{
-       struct nlmsghdr nlh;
-
-       processMessage(nl.getFd(), POLLIN);
-
-       //To get message size
-       nl.recv(&nlh, sizeof(nlh), options | MSG_PEEK);
-
-       char buf[nlh.nlmsg_len + NLMSG_HDRLEN];
-       nl.recv(buf, sizeof(buf), options);
-
-       Message msg = {nlh.nlmsg_type, std::vector<char>(nlh.nlmsg_len)};
-
-       ::memcpy(msg.second.data(), NLMSG_DATA(buf), msg.second.size());
-       return msg;
-}
-
-void Audit::processMessage(int fd, runtime::Mainloop::Event event)
-{
-       struct nlmsghdr nlh;
-
-       nl.recv(&nlh, sizeof(nlh), MSG_PEEK);
-
-       while (nlh.nlmsg_seq == 0) {
-               char buf[nlh.nlmsg_len + NLMSG_HDRLEN];
-               nl.recv(buf, sizeof(buf));
-
-               Message msg = {nlh.nlmsg_type, std::vector<char>(nlh.nlmsg_len)};
-
-               ::memcpy(msg.second.data(), NLMSG_DATA(buf), msg.second.size());
-               if (messageHandler) {
-                       messageHandler(msg);
-               }
-
-               try {
-                       nl.recv(&nlh, sizeof(nlh), MSG_PEEK | MSG_DONTWAIT);
-               } catch (runtime::Exception &e) {
-                       break;
-               }
-       }
+       nl.send(AUDIT_DEL_RULE, rule.data());
 }
index 639e9b79e4c4368ef8a20b7d72f017b1e03009e8..09f1d55ffcf96014a64d612a9483e94968190b47 100644 (file)
 #ifndef __AUDIT_TRAIL_AUDIT_H__
 #define __AUDIT_TRAIL_AUDIT_H__
 
-#include <list>
 #include <vector>
 
-#include <linux/audit.h>
-
-#include <klay/mainloop.h>
 #include <klay/netlink/netlink.h>
 
 #include "audit/audit-rule.h"
 class Audit final {
 public:
        typedef std::pair<int, std::vector<char>> Message;
-       typedef std::function<void(Message&)> MessageHandler;
 
        Audit();
-       Audit(Audit&&);
+       Audit(Audit&&) = delete;
        Audit(const Audit&) = delete;
        ~Audit();
 
-       Audit& operator=(const Audit&) = delete;
-
        void setPID(pid_t pid);
        void setEnabled(int enabled);
        int isEnabled();
@@ -47,19 +40,16 @@ public:
        void addRule(const AuditRule& rule);
        void removeRule(const AuditRule& rule);
 
-       void setMainloop(runtime::Mainloop *ml);
-       void setMessageHandler(MessageHandler&& handler);
+       int getFd()
+       {
+               return nl.getFd();
+       }
 
 private:
-       void send(int type, const void *data, unsigned int size);
-       Message recv(int options = 0);
-
-       void processMessage(int fd, runtime::Mainloop::Event event);
-
        netlink::Netlink nl;
-       unsigned int sequence = 0;
-       runtime::Mainloop *mainloop;
-       MessageHandler messageHandler;
+
+       friend class AuditMessageParser;
 };
 
+
 #endif //!__AUDIT_TRAIL_AUDIT_H__
index 7e4cde3809e54aeaa5517df1307bc8612361c24e..d09c7b882ff6aacef67898a218ec3092ebd4a26d 100755 (executable)
@@ -21,11 +21,11 @@ SET(PC_FILE "${PROJECT_NAME}.pc")
 SET(SOURCES client.cpp
                        rule-management.cpp
                        audit-trail/audit-trail.cpp
-                       audit-trail/rule-management.cpp
+                       audit-trail/rule.cpp
 )
 
 SET(CAPI_INCLUDE_FILES  audit-trail/audit-trail.h
-                                               audit-trail/rule-management.h
+                                               audit-trail/rule.h
 )
 
 
diff --git a/lib/audit-trail/rule-management.cpp b/lib/audit-trail/rule-management.cpp
deleted file mode 100644 (file)
index 7a6317a..0000000
+++ /dev/null
@@ -1,203 +0,0 @@
-/*
- *  Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
- *
- *  Licensed under the Apache License, Version 2.0 (the "License");
- *  you may not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *       http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS,
- *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License
- */
-#include <cstring>
-
-#include "debug.h"
-#include "rule-management.h"
-
-#include "client.h"
-#include "audit/audit-rule.h"
-#include "rmi/rule-management.h"
-
-using namespace AuditTrail;
-
-static inline AuditRule& GetAuditRule(void* handle)
-{
-    return *reinterpret_cast<AuditRule*>(handle);
-}
-
-int audit_rule_create(audit_rule_h* handle)
-{
-       RET_ON_FAILURE(handle, AUDIT_TRAIL_ERROR_INVALID_PARAMETER);
-       *handle = reinterpret_cast<audit_rule_h>(new AuditRule());
-
-       return AUDIT_TRAIL_ERROR_NONE;
-}
-
-int audit_rule_destroy(audit_rule_h handle)
-{
-       RET_ON_FAILURE(handle, AUDIT_TRAIL_ERROR_INVALID_PARAMETER);
-
-       delete &GetAuditRule(handle);
-
-       return AUDIT_TRAIL_ERROR_NONE;
-}
-
-int audit_rule_add_systemcall(audit_rule_h handle, unsigned int syscall)
-{
-       RET_ON_FAILURE(handle, AUDIT_TRAIL_ERROR_INVALID_PARAMETER);
-
-       GetAuditRule(handle).addSystemcall(syscall);
-
-       return AUDIT_TRAIL_ERROR_NONE;
-}
-
-int audit_rule_remove_systemcall(audit_rule_h handle, unsigned int syscall)
-{
-       RET_ON_FAILURE(handle, AUDIT_TRAIL_ERROR_INVALID_PARAMETER);
-
-       GetAuditRule(handle).removeSystemcall(syscall);
-
-       return AUDIT_TRAIL_ERROR_NONE;
-}
-
-int audit_rule_add_all_systemcalls(audit_rule_h handle)
-{
-       RET_ON_FAILURE(handle, AUDIT_TRAIL_ERROR_INVALID_PARAMETER);
-
-       GetAuditRule(handle).addAllSystemcalls();
-
-       return AUDIT_TRAIL_ERROR_NONE;
-}
-
-int audit_rule_remove_all_systemcall(audit_rule_h handle)
-{
-       RET_ON_FAILURE(handle, AUDIT_TRAIL_ERROR_INVALID_PARAMETER);
-
-       GetAuditRule(handle).removeAllSystemcalls();
-
-       return AUDIT_TRAIL_ERROR_NONE;
-}
-
-int audit_rule_add_condition(audit_rule_h handle, unsigned int field,
-                                                               unsigned int op, void *value)
-{
-       RET_ON_FAILURE(handle, AUDIT_TRAIL_ERROR_INVALID_PARAMETER);
-
-       try {
-               GetAuditRule(handle).addCondition({field, op, (int)(intptr_t)value});
-               return AUDIT_TRAIL_ERROR_NONE;
-       } catch (std::exception &e) {}
-
-       try {
-               GetAuditRule(handle).addCondition({field, op, (char *)value});
-               return AUDIT_TRAIL_ERROR_NONE;
-       } catch (std::exception &e) {}
-
-       return AUDIT_TRAIL_ERROR_INVALID_PARAMETER;
-}
-
-int audit_rule_remove_condition(audit_rule_h handle, unsigned int field,
-                                                               unsigned int op, void *value)
-{
-       RET_ON_FAILURE(handle, AUDIT_TRAIL_ERROR_INVALID_PARAMETER);
-
-       try {
-               GetAuditRule(handle).addCondition({field, op, (int)(intptr_t)value});
-               return AUDIT_TRAIL_ERROR_NONE;
-       } catch (std::exception &e) {}
-
-       try {
-               GetAuditRule(handle).addCondition({field, op, (char *)value});
-               return AUDIT_TRAIL_ERROR_NONE;
-       } catch (std::exception &e) {}
-
-       return AUDIT_TRAIL_ERROR_INVALID_PARAMETER;
-}
-
-int audit_rule_foreach_systemcall(audit_rule_h handle,
-                                                                       audit_rule_systemcall_cb callback,
-                                                                       void *user_data)
-{
-       RET_ON_FAILURE(handle, AUDIT_TRAIL_ERROR_INVALID_PARAMETER);
-       RET_ON_FAILURE(callback, AUDIT_TRAIL_ERROR_INVALID_PARAMETER);
-
-       auto syscalls = GetAuditRule(handle).getSystemcalls();
-       for (auto syscall : syscalls) {
-               callback(syscall, user_data);
-       }
-
-       return AUDIT_TRAIL_ERROR_NONE;
-}
-
-int audit_rule_foreach_condition(audit_rule_h handle,
-                                                                       audit_rule_condition_cb callback,
-                                                                       void *user_data)
-{
-       RET_ON_FAILURE(handle, AUDIT_TRAIL_ERROR_INVALID_PARAMETER);
-       RET_ON_FAILURE(callback, AUDIT_TRAIL_ERROR_INVALID_PARAMETER);
-
-       auto intConds = GetAuditRule(handle).getConditions<int>();
-       auto strConds = GetAuditRule(handle).getConditions<std::string>();
-
-       for (auto cond : intConds) {
-               callback(cond.getField(), cond.getOperator(),
-                                       (void*)(intptr_t)cond.getValue(), user_data);
-       }
-
-       for (auto cond : strConds) {
-               callback(cond.getField(), cond.getOperator(),
-                                       (void*)cond.getValue().c_str(), user_data);
-       }
-
-       return AUDIT_TRAIL_ERROR_NONE;
-}
-
-int audit_add_rule(audit_trail_h handle, audit_rule_h rule)
-{
-       RET_ON_FAILURE(handle, AUDIT_TRAIL_ERROR_INVALID_PARAMETER);
-       RET_ON_FAILURE(rule, AUDIT_TRAIL_ERROR_INVALID_PARAMETER);
-
-       AuditTrailContext &client = GetAuditTrailContext(handle);
-       auto manager = client.createInterface<RuleManagement>();
-       auto r = GetAuditRule(rule);
-
-       manager.addRule(std::vector<char>(r.data(), r.data() + r.size()));
-
-       return AUDIT_TRAIL_ERROR_NONE;
-}
-
-int audit_remove_rule(audit_trail_h handle, audit_rule_h rule)
-{
-       RET_ON_FAILURE(handle, AUDIT_TRAIL_ERROR_INVALID_PARAMETER);
-       RET_ON_FAILURE(rule, AUDIT_TRAIL_ERROR_INVALID_PARAMETER);
-
-       AuditTrailContext &client = GetAuditTrailContext(handle);
-       auto manager = client.createInterface<RuleManagement>();
-       auto r = GetAuditRule(rule);
-
-       manager.removeRule(std::vector<char>(r.data(), r.data() + r.size()));
-
-       return AUDIT_TRAIL_ERROR_NONE;
-}
-
-int audit_foreach_rule(audit_trail_h handle,
-                        audit_rule_cb callback, void *user_data)
-{
-       RET_ON_FAILURE(handle, AUDIT_TRAIL_ERROR_INVALID_PARAMETER);
-       RET_ON_FAILURE(callback, AUDIT_TRAIL_ERROR_INVALID_PARAMETER);
-
-       AuditTrailContext &client = GetAuditTrailContext(handle);
-       auto manager = client.createInterface<RuleManagement>();
-       auto rulesData = manager.getRules();
-
-       for (auto data : rulesData) {
-               callback(reinterpret_cast<audit_rule_h>(new AuditRule(data)),
-                                       user_data);
-       }
-
-       return AUDIT_TRAIL_ERROR_NONE;
-}
diff --git a/lib/audit-trail/rule-management.h b/lib/audit-trail/rule-management.h
deleted file mode 100644 (file)
index e4666b0..0000000
+++ /dev/null
@@ -1,350 +0,0 @@
-/*
- *  Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
- *
- *  Licensed under the Apache License, Version 2.0 (the "License");
- *  you may not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS,
- *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License
- */
-
-#ifndef __CAPI_AUDIT_TRAIL_RULE_MANAGEMENT_H__
-#define __CAPI_AUDIT_TRAIL_RULE_MANAGEMENT_H__
-
-#include <audit-trail/audit-trail.h>
-
-/**
- * @file user.h
- * @brief This file provides APIs to manage audit rules
- */
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/**
- * @addtogroup  Rule-management
- * @{
- */
-
-/**
- * @brief       The audit rule handle
- * @details     The audit rule handle is an abstraction of the audit rule.
- *              The audit rule handle must be created by using
- *              audit_rule_create() before attempting to use almost any of
- *              audit rule related APIs, and it should be freed when the audit
- *              rule is no longer required.
- *              To release the handle, use audit_rule_destroy().
- * @since_tizen 5.0
- * @see         audit_rule_create()
- * @see         audit_rule_destroy()
- */
-typedef void* audit_rule_h;
-
-/**
- * @brief       Creates the audit rule handle.
- * @details     This API creates audit rule handle required by the audit rule
- *              related APIs.
- *              This API is also used to verify whether caller is authorized
- *              or not.
- * @since_tizen 5.0
- * @param[out]  handle The audit rule handle
- * @return      #AUDIT_TRAIL_ERROR_NONE on success, otherwise a negative value
- * @retval      #AUDIT_TRAIL_ERROR_NONE Successful
- * @retval      #AUDIT_TRAIL_ERROR_INVALID_PARAMETER Invalid parameter
- * @retval      #AUDIT_TRAIL_ERROR_TIMED_OUT Time out
- * @see         audit_rule_destroy()
- */
-AUDIT_TRAIL_API int audit_rule_create(audit_rule_h *handle);
-
-/**
- * @brief       Releases the audit rule handle.
- * @details     This API must be called if operations of the audit rule is
- *              no longer required.
- * @since_tizen 5.0
- * @param[in]   handle The audit rule handle
- * @return      #AUDIT_TRAIL_ERROR_NONE on success, otherwise a negative value
- * @retval      #AUDIT_TRAIL_ERROR_NONE Successful
- * @retval      #AUDIT_TRAIL_ERROR_INVALID_PARAMETER Invalid parameter
- * @pre         The handle must be created by audit_rule_create()
- * @see         audit_rule_create()
- */
-AUDIT_TRAIL_API int audit_rule_destroy(audit_rule_h handle);
-
-/**
- * @brief       Add a system call to be monitored.
- * @details     This API sets a system call to be monitored on the audit rule.
- *              Parameter requires a system call number, which is defined in
- *              asm/unistd.h typically.
- * @since_tizen 5.0
- * @param[in]   handle The audit rule handle
- * @param[in]   syscall The system call number
- * @return      #AUDIT_TRAIL_ERROR_NONE on success, otherwise a negative value
- * @retval      #AUDIT_TRAIL_ERROR_NONE Successful
- * @retval      #AUDIT_TRAIL_ERROR_INVALID_PARAMETER Invalid parameter
- * @pre         The handle must be created by audit_rule_create()
- * @see         audit_rule_create()
- * @see         audit_rule_remove_systemcall()
- * @see         audit_rule_add_all_systemcalls()
- * @see         audit_rule_remove_all_systemcalls()
- */
-AUDIT_TRAIL_API int audit_rule_add_systemcall(audit_rule_h handle,
-                                                                                               unsigned int syscall);
-
-/**
- * @brief       Remove a system call to be monitored.
- * @details     This API sets a system call not to be monitored anymore
- *              on the audit rule.
- *              Parameter requires a system call number, which is
- *              defined in asm/unistd.h typically.
- * @since_tizen 5.0
- * @param[in]   handle The audit rule handle
- * @param[in]   syscall The system call number
- * @return      #AUDIT_TRAIL_ERROR_NONE on success, otherwise a negative value
- * @retval      #AUDIT_TRAIL_ERROR_NONE Successful
- * @retval      #AUDIT_TRAIL_ERROR_INVALID_PARAMETER Invalid parameter
- * @pre         The handle must be created by audit_rule_create()
- * @see         audit_rule_create()
- * @see         audit_rule_add_systemcall()
- * @see         audit_rule_add_all_systemcalls()
- * @see         audit_rule_remove_all_systemcalls()
- */
-AUDIT_TRAIL_API int audit_rule_remove_systemcall(audit_rule_h handle,
-                                                                                                       unsigned int syscall);
-
-/**
- * @brief       Add all system calls to be monitored.
- * @details     This API sets all system calls to be monitored on the audit
- *              rule.
- * @since_tizen 5.0
- * @param[in]   handle The audit rule handle
- * @return      #AUDIT_TRAIL_ERROR_NONE on success, otherwise a negative value
- * @retval      #AUDIT_TRAIL_ERROR_NONE Successful
- * @retval      #AUDIT_TRAIL_ERROR_INVALID_PARAMETER Invalid parameter
- * @pre         The handle must be created by audit_rule_create()
- * @see         audit_rule_create()
- * @see         audit_rule_add_systemcall()
- * @see         audit_rule_remove_systemcall()
- * @see         audit_rule_remove_all_systemcallis()
- */
-AUDIT_TRAIL_API int audit_rule_add_all_systemcalls(audit_rule_h handle);
-
-/**
- * @brief       Remove all system calls to be monitored.
- * @details     This API sets all system calls not to be monitored anymore
- *              on the audit rule.
- * @since_tizen 5.0
- * @param[in]   handle The audit rule handle
- * @return      #AUDIT_TRAIL_ERROR_NONE on success, otherwise a negative value
- * @retval      #AUDIT_TRAIL_ERROR_NONE Successful
- * @retval      #AUDIT_TRAIL_ERROR_INVALID_PARAMETER Invalid parameter
- * @pre         The handle must be created by audit_rule_create()
- * @see         audit_rule_create()
- * @see         audit_rule_add_systemcall()
- * @see         audit_rule_remove_systemcall()
- * @see         audit_rule_add_all_systemcallis()
- */
-AUDIT_TRAIL_API int audit_rule_remove_all_systemcalls(audit_rule_h handle);
-
-/**
- * @brief       Called to get system call numbers in an array
- * @since_tizen 5.0
- * @param[in]   syscall The system call number
- * @param[in]   user_data The user data passed from the function
- * @see         audit_rule_foreach_systemcall
- */
-typedef void (*audit_rule_systemcall_cb)(unsigned int syscall, void* user_data);
-
-/**
- * @brief       Retrieves all added system call nummbers in the audit rule.
- * @details     This API calls audit_rule_systemcall_cb() once for each added
- *               system calls in the audit rule.
- * @since_tizen 5.0
- * @param[in]   handle The audit rule handle
- * @param[in]   callback The iteration callback function
- * @param[in]   user_data The user data passed to the callback function
- * @return      #AUDIT_TRAIL_ERROR_NONE on success, otherwise a negative value
- * @retval      #AUDIT_TRAIL_ERROR_NONE Successful
- * @retval      #AUDIT_TRAIL_ERROR_TIMED_OUT Time out
- * @retval      #AUDIT_TRAIL_ERROR_INVALID_PARAMETER Invalid parameter
- * @pre         The handle must be created by audit_trail_create().
- * @see         audit_rule_create()
- * @see         audit_rule_add_syscall()
- * @see         audit_rule_add_all_syscall()
- */
-AUDIT_TRAIL_API int audit_rule_foreach_systemcall(audit_rule_h handle,
-                                               audit_rule_systemcall_cb callback, void *user_data);
-
-
-/**
- * @brief       Add a condition to be monitored.
- * @details     This API sets a condition to be monitored on the audit rule.
- *              A condition consists of 3 parameters: field, operator, value.
- *              Both fields and operators are defined in linux/audit.h.
- *              Value type should be different regarding to field.
- *              But the most used type is integer type.
- * @since_tizen 5.0
- * @param[in]   handle The audit rule handle
- * @param[in]   field The field number
- * @param[in]   op The operator number
- * @param[in]   value The value
- * @return      #AUDIT_TRAIL_ERROR_NONE on success, otherwise a negative value
- * @retval      #AUDIT_TRAIL_ERROR_NONE Successful
- * @retval      #AUDIT_TRAIL_ERROR_INVALID_PARAMETER Invalid parameter
- * @pre         The handle must be created by audit_rule_create()
- * @see         audit_rule_create()
- * @see         audit_rule_remove_condition()
- */
-AUDIT_TRAIL_API int audit_rule_add_condition(audit_rule_h handle,
-                                                                                               unsigned int field,
-                                                                                               unsigned int op,
-                                                                                               void *value);
-
-/**
- * @brief       Remove a condition to be monitored.
- * @details     This API sets a condition not to be monitored anymore
- *              on the audit rule.
- *              A condition consists of 3 parameters: field, operator, value.
- *              Both fields and operators are defined in linux/audit.h.
- *              Value type should be different regarding to field.
- *              But the most used type is integer type.
- * @since_tizen 5.0
- * @param[in]   handle The audit rule handle
- * @param[in]   field The field number
- * @param[in]   op The operator number
- * @param[in]   value The value
- * @return      #AUDIT_TRAIL_ERROR_NONE on success, otherwise a negative value
- * @retval      #AUDIT_TRAIL_ERROR_NONE Successful
- * @retval      #AUDIT_TRAIL_ERROR_INVALID_PARAMETER Invalid parameter
- * @pre         The handle must be created by audit_rule_create()
- * @see         audit_rule_create()
- * @see         audit_rule_add_condition()
- */
-AUDIT_TRAIL_API int audit_rule_remove_condition(audit_rule_h handle,
-                                                                                               unsigned int field,
-                                                                                               unsigned int op,
-                                                                                               void *value);
-
-/**
- * @brief       Called to get a audit rule condition in an array
- *              A condition consists of 3 parameters: field, operator, value.
- *              Both fields and operators are defined in linux/audit.h.
- *              Value type should be different regarding to field.
- *              But the most used type is integer type.
- * @since_tizen 5.0
- * @param[in]   field The field number
- * @param[in]   op The operator number
- * @param[in]   value The value
- * @param[in]   user_data The user data passed from the function
- * @see         audit_rule_foreach_condition
- */
-typedef void (*audit_rule_condition_cb)(unsigned int field, unsigned int op,
-                                                                               const void* value, void* user_data);
-
-/**
- * @brief       Retrieves all added conditions in the audit rule.
- * @details     This API calls audit_rule_condition_cb() once for each added
- *              conditions in the audit rule.
- * @since_tizen 5.0
- * @param[in]   handle The audit rule handle
- * @param[in]   callback The iteration callback function
- * @param[in]   user_data The user data passed to the callback function
- * @return      #AUDIT_TRAIL_ERROR_NONE on success, otherwise a negative value
- * @retval      #AUDIT_TRAIL_ERROR_NONE Successful
- * @retval      #AUDIT_TRAIL_ERROR_TIMED_OUT Time out
- * @retval      #AUDIT_TRAIL_ERROR_INVALID_PARAMETER Invalid parameter
- * @pre         The handle must be created by audit_trail_create().
- * @see         audit_rule_create()
- * @see         audit_rule_add_condition()
- */
-AUDIT_TRAIL_API int audit_rule_foreach_condition(audit_rule_h handle,
-                                               audit_rule_condition_cb callback, void *user_data);
-
-/**
- * @brief       Add a audit rule to apply to collect the audit logs.
- * @details     This API applie a audit rule to collect the audit logs.
- *              The audit rules not wanted to be applied have to be removed by
- *              audit_remove_rule().
- * @since_tizen 5.0
- * @param[in]   handle The audit handle
- * @param[in]   rule The audit rule handle
- * @return      #AUDIT_TRAIL_ERROR_NONE on success, otherwise a negative value
- * @retval      #AUDIT_TRAIL_ERROR_NONE Successful
- * @retval      #AUDIT_TRAIL_ERROR_TIMED_OUT Time out
- * @retval      #AUDIT_TRAIL_ERROR_INVALID_PARAMETER Invalid parameter
- * @pre         The handle must be created by audit_trail_create().
- * @pre         The rule must be created by audit_rule_create().
- * @see         audit_trail_create()
- * @see         audit_rule_create()
- * @see         audit_remove_rule()
- */
-AUDIT_TRAIL_API int audit_add_rule(audit_trail_h handle, audit_rule_h rule);
-
-/**
- * @brief       Remove a audit rule not to be used to collect the audit logs.
- * @details     This API removes a audit rule not wanted anymore to be applied.
- *              If there is no added rule to match, This API will be ignored.
- * @since_tizen 5.0
- * @param[in]   handle The audit handle
- * @param[in]   rule The audit rule handle
- * @return      #AUDIT_TRAIL_ERROR_NONE on success, otherwise a negative value
- * @retval      #AUDIT_TRAIL_ERROR_NONE Successful
- * @retval      #AUDIT_TRAIL_ERROR_TIMED_OUT Time out
- * @retval      #AUDIT_TRAIL_ERROR_INVALID_PARAMETER Invalid parameter
- * @pre         The handle must be created by audit_trail_create().
- * @pre         The rule must be created by audit_rule_create().
- * @see         audit_trail_create()
- * @see         audit_rule_create()
- * @see         audit_add_rule()
- */
-AUDIT_TRAIL_API int audit_remove_rule(audit_trail_h handle, audit_rule_h rule);
-
-/**
- * @brief       Called to get a audit rules an array
- *              This function is called with audit rule handles, which can be
- *              used to get system calls to be monitored by the rule and severa;
- *              conditions. and also it must not be freed by
- *              audit_rule_destroy().
- * @since_tizen 5.0
- * @param[in]   rule The audit rule handle
- * @param[in]   user_data The user data passed from the function
- * @see         audit_foreach_rule
- * @see         audit_rule_foreach_systemcall
- * @see         audit_rule_foreach_condition
- */
-typedef void (*audit_rule_cb)(audit_rule_h rule, void* user_data);
-
-/**
- * @brief       Retrieves all added audit ruless in this device.
- * @details     This API calls audit_rule_cb() once for each added audit rules
- *              in this system
- * @since_tizen 5.0
- * @param[in]   handle The audit handle
- * @param[in]   callback The iteration callback function
- * @param[in]   user_data The user data passed to the callback function
- * @return      #AUDIT_TRAIL_ERROR_NONE on success, otherwise a negative value
- * @retval      #AUDIT_TRAIL_ERROR_NONE Successful
- * @retval      #AUDIT_TRAIL_ERROR_TIMED_OUT Time out
- * @retval      #AUDIT_TRAIL_ERROR_INVALID_PARAMETER Invalid parameter
- * @pre         The handle must be created by audit_trail_create().
- * @see         audit_rule_add_condition()
- * @see         audit_rule_remove_condition()
- * @see         audit_rule_cb()
- */
-AUDIT_TRAIL_API int audit_foreach_rule(audit_trail_h handle,
-                                               audit_rule_cb callback, void *user_data);
-/**
- * @}
- */
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* __CAPI_AUDIT_TRAIL_RULE_MANAGEMENT_H__ */
diff --git a/lib/audit-trail/rule.cpp b/lib/audit-trail/rule.cpp
new file mode 100644 (file)
index 0000000..32a2207
--- /dev/null
@@ -0,0 +1,204 @@
+/*
+ *  Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+#include <cstring>
+
+#include "debug.h"
+#include "rule.h"
+
+#include "client.h"
+#include "audit/audit-rule.h"
+
+#include "rmi/rule-management.h"
+
+using namespace AuditTrail;
+
+static inline AuditRule& GetAuditRule(void* handle)
+{
+    return *reinterpret_cast<AuditRule*>(handle);
+}
+
+int audit_rule_create(audit_rule_h* handle)
+{
+       RET_ON_FAILURE(handle, AUDIT_TRAIL_ERROR_INVALID_PARAMETER);
+       *handle = reinterpret_cast<audit_rule_h>(new AuditRule());
+
+       return AUDIT_TRAIL_ERROR_NONE;
+}
+
+int audit_rule_destroy(audit_rule_h handle)
+{
+       RET_ON_FAILURE(handle, AUDIT_TRAIL_ERROR_INVALID_PARAMETER);
+
+       delete &GetAuditRule(handle);
+
+       return AUDIT_TRAIL_ERROR_NONE;
+}
+
+int audit_rule_add_systemcall(audit_rule_h handle, unsigned int syscall)
+{
+       RET_ON_FAILURE(handle, AUDIT_TRAIL_ERROR_INVALID_PARAMETER);
+
+       GetAuditRule(handle).addSystemcall(syscall);
+
+       return AUDIT_TRAIL_ERROR_NONE;
+}
+
+int audit_rule_remove_systemcall(audit_rule_h handle, unsigned int syscall)
+{
+       RET_ON_FAILURE(handle, AUDIT_TRAIL_ERROR_INVALID_PARAMETER);
+
+       GetAuditRule(handle).removeSystemcall(syscall);
+
+       return AUDIT_TRAIL_ERROR_NONE;
+}
+
+int audit_rule_add_all_systemcalls(audit_rule_h handle)
+{
+       RET_ON_FAILURE(handle, AUDIT_TRAIL_ERROR_INVALID_PARAMETER);
+
+       GetAuditRule(handle).addAllSystemcalls();
+
+       return AUDIT_TRAIL_ERROR_NONE;
+}
+
+int audit_rule_remove_all_systemcall(audit_rule_h handle)
+{
+       RET_ON_FAILURE(handle, AUDIT_TRAIL_ERROR_INVALID_PARAMETER);
+
+       GetAuditRule(handle).removeAllSystemcalls();
+
+       return AUDIT_TRAIL_ERROR_NONE;
+}
+
+int audit_rule_add_condition(audit_rule_h handle, unsigned int field,
+                                                               unsigned int op, void *value)
+{
+       RET_ON_FAILURE(handle, AUDIT_TRAIL_ERROR_INVALID_PARAMETER);
+
+       try {
+               GetAuditRule(handle).addCondition({field, op, (int)(intptr_t)value});
+               return AUDIT_TRAIL_ERROR_NONE;
+       } catch (std::exception &e) {}
+
+       try {
+               GetAuditRule(handle).addCondition({field, op, (char *)value});
+               return AUDIT_TRAIL_ERROR_NONE;
+       } catch (std::exception &e) {}
+
+       return AUDIT_TRAIL_ERROR_INVALID_PARAMETER;
+}
+
+int audit_rule_remove_condition(audit_rule_h handle, unsigned int field,
+                                                               unsigned int op, void *value)
+{
+       RET_ON_FAILURE(handle, AUDIT_TRAIL_ERROR_INVALID_PARAMETER);
+
+       try {
+               GetAuditRule(handle).addCondition({field, op, (int)(intptr_t)value});
+               return AUDIT_TRAIL_ERROR_NONE;
+       } catch (std::exception &e) {}
+
+       try {
+               GetAuditRule(handle).addCondition({field, op, (char *)value});
+               return AUDIT_TRAIL_ERROR_NONE;
+       } catch (std::exception &e) {}
+
+       return AUDIT_TRAIL_ERROR_INVALID_PARAMETER;
+}
+
+int audit_rule_foreach_systemcall(audit_rule_h handle,
+                                                                       audit_rule_systemcall_cb callback,
+                                                                       void *user_data)
+{
+       RET_ON_FAILURE(handle, AUDIT_TRAIL_ERROR_INVALID_PARAMETER);
+       RET_ON_FAILURE(callback, AUDIT_TRAIL_ERROR_INVALID_PARAMETER);
+
+       auto syscalls = GetAuditRule(handle).getSystemcalls();
+       for (auto syscall : syscalls) {
+               callback(syscall, user_data);
+       }
+
+       return AUDIT_TRAIL_ERROR_NONE;
+}
+
+int audit_rule_foreach_condition(audit_rule_h handle,
+                                                                       audit_rule_condition_cb callback,
+                                                                       void *user_data)
+{
+       RET_ON_FAILURE(handle, AUDIT_TRAIL_ERROR_INVALID_PARAMETER);
+       RET_ON_FAILURE(callback, AUDIT_TRAIL_ERROR_INVALID_PARAMETER);
+
+       auto intConds = GetAuditRule(handle).getConditions<int>();
+       auto strConds = GetAuditRule(handle).getConditions<std::string>();
+
+       for (auto cond : intConds) {
+               callback(cond.getField(), cond.getOperator(),
+                                       (void*)(intptr_t)cond.getValue(), user_data);
+       }
+
+       for (auto cond : strConds) {
+               callback(cond.getField(), cond.getOperator(),
+                                       (void*)cond.getValue().c_str(), user_data);
+       }
+
+       return AUDIT_TRAIL_ERROR_NONE;
+}
+
+int audit_trail_add_rule(audit_trail_h handle, audit_rule_h rule)
+{
+       RET_ON_FAILURE(handle, AUDIT_TRAIL_ERROR_INVALID_PARAMETER);
+       RET_ON_FAILURE(rule, AUDIT_TRAIL_ERROR_INVALID_PARAMETER);
+
+       AuditTrailContext &client = GetAuditTrailContext(handle);
+       auto manager = client.createInterface<RuleManagement>();
+       auto r = GetAuditRule(rule);
+
+       manager.addRule(r.data());
+
+       return AUDIT_TRAIL_ERROR_NONE;
+}
+
+int audit_trail_remove_rule(audit_trail_h handle, audit_rule_h rule)
+{
+       RET_ON_FAILURE(handle, AUDIT_TRAIL_ERROR_INVALID_PARAMETER);
+       RET_ON_FAILURE(rule, AUDIT_TRAIL_ERROR_INVALID_PARAMETER);
+
+       AuditTrailContext &client = GetAuditTrailContext(handle);
+       auto manager = client.createInterface<RuleManagement>();
+       auto r = GetAuditRule(rule);
+
+       manager.removeRule(r.data());
+
+       return AUDIT_TRAIL_ERROR_NONE;
+}
+
+int audit_trail_foreach_rule(audit_trail_h handle,
+                        audit_rule_cb callback, void *user_data)
+{
+       RET_ON_FAILURE(handle, AUDIT_TRAIL_ERROR_INVALID_PARAMETER);
+       RET_ON_FAILURE(callback, AUDIT_TRAIL_ERROR_INVALID_PARAMETER);
+
+       AuditTrailContext &client = GetAuditTrailContext(handle);
+       auto manager = client.createInterface<RuleManagement>();
+       auto rulesData = manager.getRules();
+
+       for (auto data : rulesData) {
+               callback(reinterpret_cast<audit_rule_h>(new AuditRule(data)),
+                                       user_data);
+       }
+
+       return AUDIT_TRAIL_ERROR_NONE;
+}
diff --git a/lib/audit-trail/rule.h b/lib/audit-trail/rule.h
new file mode 100644 (file)
index 0000000..a4dc716
--- /dev/null
@@ -0,0 +1,350 @@
+/*
+ *  Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+
+#ifndef __CAPI_AUDIT_TRAIL_RULE_H__
+#define __CAPI_AUDIT_TRAIL_RULE_H__
+
+#include <audit-trail/audit-trail.h>
+
+/**
+ * @file user.h
+ * @brief This file provides APIs to manage audit rules
+ */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * @addtogroup  Rule-management
+ * @{
+ */
+
+/**
+ * @brief       The audit rule handle
+ * @details     The audit rule handle is an abstraction of the audit rule.
+ *              The audit rule handle must be created by using
+ *              audit_rule_create() before attempting to use almost any of
+ *              audit rule related APIs, and it should be freed when the audit
+ *              rule is no longer required.
+ *              To release the handle, use audit_rule_destroy().
+ * @since_tizen 5.0
+ * @see         audit_rule_create()
+ * @see         audit_rule_destroy()
+ */
+typedef void* audit_rule_h;
+
+/**
+ * @brief       Creates the audit rule handle.
+ * @details     This API creates audit rule handle required by the audit rule
+ *              related APIs.
+ *              This API is also used to verify whether caller is authorized
+ *              or not.
+ * @since_tizen 5.0
+ * @param[out]  handle The audit rule handle
+ * @return      #AUDIT_TRAIL_ERROR_NONE on success, otherwise a negative value
+ * @retval      #AUDIT_TRAIL_ERROR_NONE Successful
+ * @retval      #AUDIT_TRAIL_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval      #AUDIT_TRAIL_ERROR_TIMED_OUT Time out
+ * @see         audit_rule_destroy()
+ */
+AUDIT_TRAIL_API int audit_rule_create(audit_rule_h *handle);
+
+/**
+ * @brief       Releases the audit rule handle.
+ * @details     This API must be called if operations of the audit rule is
+ *              no longer required.
+ * @since_tizen 5.0
+ * @param[in]   handle The audit rule handle
+ * @return      #AUDIT_TRAIL_ERROR_NONE on success, otherwise a negative value
+ * @retval      #AUDIT_TRAIL_ERROR_NONE Successful
+ * @retval      #AUDIT_TRAIL_ERROR_INVALID_PARAMETER Invalid parameter
+ * @pre         The handle must be created by audit_rule_create()
+ * @see         audit_rule_create()
+ */
+AUDIT_TRAIL_API int audit_rule_destroy(audit_rule_h handle);
+
+/**
+ * @brief       Add a system call to be monitored.
+ * @details     This API sets a system call to be monitored on the audit rule.
+ *              Parameter requires a system call number, which is defined in
+ *              asm/unistd.h typically.
+ * @since_tizen 5.0
+ * @param[in]   handle The audit rule handle
+ * @param[in]   syscall The system call number
+ * @return      #AUDIT_TRAIL_ERROR_NONE on success, otherwise a negative value
+ * @retval      #AUDIT_TRAIL_ERROR_NONE Successful
+ * @retval      #AUDIT_TRAIL_ERROR_INVALID_PARAMETER Invalid parameter
+ * @pre         The handle must be created by audit_rule_create()
+ * @see         audit_rule_create()
+ * @see         audit_rule_remove_systemcall()
+ * @see         audit_rule_add_all_systemcalls()
+ * @see         audit_rule_remove_all_systemcalls()
+ */
+AUDIT_TRAIL_API int audit_rule_add_systemcall(audit_rule_h handle,
+                                                                                               unsigned int syscall);
+
+/**
+ * @brief       Remove a system call to be monitored.
+ * @details     This API sets a system call not to be monitored anymore
+ *              on the audit rule.
+ *              Parameter requires a system call number, which is
+ *              defined in asm/unistd.h typically.
+ * @since_tizen 5.0
+ * @param[in]   handle The audit rule handle
+ * @param[in]   syscall The system call number
+ * @return      #AUDIT_TRAIL_ERROR_NONE on success, otherwise a negative value
+ * @retval      #AUDIT_TRAIL_ERROR_NONE Successful
+ * @retval      #AUDIT_TRAIL_ERROR_INVALID_PARAMETER Invalid parameter
+ * @pre         The handle must be created by audit_rule_create()
+ * @see         audit_rule_create()
+ * @see         audit_rule_add_systemcall()
+ * @see         audit_rule_add_all_systemcalls()
+ * @see         audit_rule_remove_all_systemcalls()
+ */
+AUDIT_TRAIL_API int audit_rule_remove_systemcall(audit_rule_h handle,
+                                                                                                       unsigned int syscall);
+
+/**
+ * @brief       Add all system calls to be monitored.
+ * @details     This API sets all system calls to be monitored on the audit
+ *              rule.
+ * @since_tizen 5.0
+ * @param[in]   handle The audit rule handle
+ * @return      #AUDIT_TRAIL_ERROR_NONE on success, otherwise a negative value
+ * @retval      #AUDIT_TRAIL_ERROR_NONE Successful
+ * @retval      #AUDIT_TRAIL_ERROR_INVALID_PARAMETER Invalid parameter
+ * @pre         The handle must be created by audit_rule_create()
+ * @see         audit_rule_create()
+ * @see         audit_rule_add_systemcall()
+ * @see         audit_rule_remove_systemcall()
+ * @see         audit_rule_remove_all_systemcallis()
+ */
+AUDIT_TRAIL_API int audit_rule_add_all_systemcalls(audit_rule_h handle);
+
+/**
+ * @brief       Remove all system calls to be monitored.
+ * @details     This API sets all system calls not to be monitored anymore
+ *              on the audit rule.
+ * @since_tizen 5.0
+ * @param[in]   handle The audit rule handle
+ * @return      #AUDIT_TRAIL_ERROR_NONE on success, otherwise a negative value
+ * @retval      #AUDIT_TRAIL_ERROR_NONE Successful
+ * @retval      #AUDIT_TRAIL_ERROR_INVALID_PARAMETER Invalid parameter
+ * @pre         The handle must be created by audit_rule_create()
+ * @see         audit_rule_create()
+ * @see         audit_rule_add_systemcall()
+ * @see         audit_rule_remove_systemcall()
+ * @see         audit_rule_add_all_systemcallis()
+ */
+AUDIT_TRAIL_API int audit_rule_remove_all_systemcalls(audit_rule_h handle);
+
+/**
+ * @brief       Called to get system call numbers in an array
+ * @since_tizen 5.0
+ * @param[in]   syscall The system call number
+ * @param[in]   user_data The user data passed from the function
+ * @see         audit_rule_foreach_systemcall
+ */
+typedef void (*audit_rule_systemcall_cb)(unsigned int syscall, void* user_data);
+
+/**
+ * @brief       Retrieves all added system call nummbers in the audit rule.
+ * @details     This API calls audit_rule_systemcall_cb() once for each added
+ *               system calls in the audit rule.
+ * @since_tizen 5.0
+ * @param[in]   handle The audit rule handle
+ * @param[in]   callback The iteration callback function
+ * @param[in]   user_data The user data passed to the callback function
+ * @return      #AUDIT_TRAIL_ERROR_NONE on success, otherwise a negative value
+ * @retval      #AUDIT_TRAIL_ERROR_NONE Successful
+ * @retval      #AUDIT_TRAIL_ERROR_TIMED_OUT Time out
+ * @retval      #AUDIT_TRAIL_ERROR_INVALID_PARAMETER Invalid parameter
+ * @pre         The handle must be created by audit_trail_create().
+ * @see         audit_rule_create()
+ * @see         audit_rule_add_syscall()
+ * @see         audit_rule_add_all_syscall()
+ */
+AUDIT_TRAIL_API int audit_rule_foreach_systemcall(audit_rule_h handle,
+                                               audit_rule_systemcall_cb callback, void *user_data);
+
+
+/**
+ * @brief       Add a condition to be monitored.
+ * @details     This API sets a condition to be monitored on the audit rule.
+ *              A condition consists of 3 parameters: field, operator, value.
+ *              Both fields and operators are defined in linux/audit.h.
+ *              Value type should be different regarding to field.
+ *              But the most used type is integer type.
+ * @since_tizen 5.0
+ * @param[in]   handle The audit rule handle
+ * @param[in]   field The field number
+ * @param[in]   op The operator number
+ * @param[in]   value The value
+ * @return      #AUDIT_TRAIL_ERROR_NONE on success, otherwise a negative value
+ * @retval      #AUDIT_TRAIL_ERROR_NONE Successful
+ * @retval      #AUDIT_TRAIL_ERROR_INVALID_PARAMETER Invalid parameter
+ * @pre         The handle must be created by audit_rule_create()
+ * @see         audit_rule_create()
+ * @see         audit_rule_remove_condition()
+ */
+AUDIT_TRAIL_API int audit_rule_add_condition(audit_rule_h handle,
+                                                                                               unsigned int field,
+                                                                                               unsigned int op,
+                                                                                               void *value);
+
+/**
+ * @brief       Remove a condition to be monitored.
+ * @details     This API sets a condition not to be monitored anymore
+ *              on the audit rule.
+ *              A condition consists of 3 parameters: field, operator, value.
+ *              Both fields and operators are defined in linux/audit.h.
+ *              Value type should be different regarding to field.
+ *              But the most used type is integer type.
+ * @since_tizen 5.0
+ * @param[in]   handle The audit rule handle
+ * @param[in]   field The field number
+ * @param[in]   op The operator number
+ * @param[in]   value The value
+ * @return      #AUDIT_TRAIL_ERROR_NONE on success, otherwise a negative value
+ * @retval      #AUDIT_TRAIL_ERROR_NONE Successful
+ * @retval      #AUDIT_TRAIL_ERROR_INVALID_PARAMETER Invalid parameter
+ * @pre         The handle must be created by audit_rule_create()
+ * @see         audit_rule_create()
+ * @see         audit_rule_add_condition()
+ */
+AUDIT_TRAIL_API int audit_rule_remove_condition(audit_rule_h handle,
+                                                                                               unsigned int field,
+                                                                                               unsigned int op,
+                                                                                               void *value);
+
+/**
+ * @brief       Called to get a audit rule condition in an array
+ *              A condition consists of 3 parameters: field, operator, value.
+ *              Both fields and operators are defined in linux/audit.h.
+ *              Value type should be different regarding to field.
+ *              But the most used type is integer type.
+ * @since_tizen 5.0
+ * @param[in]   field The field number
+ * @param[in]   op The operator number
+ * @param[in]   value The value
+ * @param[in]   user_data The user data passed from the function
+ * @see         audit_rule_foreach_condition
+ */
+typedef void (*audit_rule_condition_cb)(unsigned int field, unsigned int op,
+                                                                               const void* value, void* user_data);
+
+/**
+ * @brief       Retrieves all added conditions in the audit rule.
+ * @details     This API calls audit_rule_condition_cb() once for each added
+ *              conditions in the audit rule.
+ * @since_tizen 5.0
+ * @param[in]   handle The audit rule handle
+ * @param[in]   callback The iteration callback function
+ * @param[in]   user_data The user data passed to the callback function
+ * @return      #AUDIT_TRAIL_ERROR_NONE on success, otherwise a negative value
+ * @retval      #AUDIT_TRAIL_ERROR_NONE Successful
+ * @retval      #AUDIT_TRAIL_ERROR_TIMED_OUT Time out
+ * @retval      #AUDIT_TRAIL_ERROR_INVALID_PARAMETER Invalid parameter
+ * @pre         The handle must be created by audit_trail_create().
+ * @see         audit_rule_create()
+ * @see         audit_rule_add_condition()
+ */
+AUDIT_TRAIL_API int audit_rule_foreach_condition(audit_rule_h handle,
+                                               audit_rule_condition_cb callback, void *user_data);
+
+/**
+ * @brief       Add a audit rule to apply to collect the audit logs.
+ * @details     This API applie a audit rule to collect the audit logs.
+ *              The audit rules not wanted to be applied have to be removed by
+ *              audit_remove_rule().
+ * @since_tizen 5.0
+ * @param[in]   handle The audit handle
+ * @param[in]   rule The audit rule handle
+ * @return      #AUDIT_TRAIL_ERROR_NONE on success, otherwise a negative value
+ * @retval      #AUDIT_TRAIL_ERROR_NONE Successful
+ * @retval      #AUDIT_TRAIL_ERROR_TIMED_OUT Time out
+ * @retval      #AUDIT_TRAIL_ERROR_INVALID_PARAMETER Invalid parameter
+ * @pre         The handle must be created by audit_trail_create().
+ * @pre         The rule must be created by audit_rule_create().
+ * @see         audit_trail_create()
+ * @see         audit_rule_create()
+ * @see         audit_trail_remove_rule()
+ */
+AUDIT_TRAIL_API int audit_trail_add_rule(audit_trail_h handle,
+                                                                                       audit_rule_h rule);
+
+/**
+ * @brief       Remove a audit rule not to be used to collect the audit logs.
+ * @details     This API removes a audit rule not wanted anymore to be applied.
+ *              If there is no added rule to match, This API will be ignored.
+ * @since_tizen 5.0
+ * @param[in]   handle The audit handle
+ * @param[in]   rule The audit rule handle
+ * @return      #AUDIT_TRAIL_ERROR_NONE on success, otherwise a negative value
+ * @retval      #AUDIT_TRAIL_ERROR_NONE Successful
+ * @retval      #AUDIT_TRAIL_ERROR_TIMED_OUT Time out
+ * @retval      #AUDIT_TRAIL_ERROR_INVALID_PARAMETER Invalid parameter
+ * @pre         The handle must be created by audit_trail_create().
+ * @pre         The rule must be created by audit_rule_create().
+ * @see         audit_trail_create()
+ * @see         audit_rule_create()
+ * @see         audit_trail_add_rule()
+ */
+AUDIT_TRAIL_API int audit_trail_remove_rule(audit_trail_h handle,
+                                                                                       audit_rule_h rule);
+
+/**
+ * @brief       Called to get a audit rules an array
+ *              This function is called with audit rule handles, which can be
+ *              used to get system calls to be monitored by the rule and severa;
+ *              conditions. and also it must not be freed by
+ *              audit_rule_destroy().
+ * @since_tizen 5.0
+ * @param[in]   rule The audit rule handle
+ * @param[in]   user_data The user data passed from the function
+ * @see         audit_trail_foreach_rule
+ */
+typedef void (*audit_rule_cb)(audit_rule_h rule, void* user_data);
+
+/**
+ * @brief       Retrieves all added audit ruless in this device.
+ * @details     This API calls audit_rule_cb() once for each added audit rules
+ *              in this system
+ * @since_tizen 5.0
+ * @param[in]   handle The audit handle
+ * @param[in]   callback The iteration callback function
+ * @param[in]   user_data The user data passed to the callback function
+ * @return      #AUDIT_TRAIL_ERROR_NONE on success, otherwise a negative value
+ * @retval      #AUDIT_TRAIL_ERROR_NONE Successful
+ * @retval      #AUDIT_TRAIL_ERROR_TIMED_OUT Time out
+ * @retval      #AUDIT_TRAIL_ERROR_INVALID_PARAMETER Invalid parameter
+ * @pre         The handle must be created by audit_trail_create().
+ * @see         audit_trail_add_rule()
+ * @see         audit_trail_remove_rule()
+ * @see         audit_rule_cb()
+ */
+AUDIT_TRAIL_API int audit_trail_foreach_rule(audit_trail_h handle,
+                                                                       audit_rule_cb callback, void *user_data);
+/**
+ * @}
+ */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __CAPI_AUDIT_TRAIL_RULE_H__ */
index 3c94204a3e5999b02a6b4f1c33d9f0864128b01a..efa114746f12c3a81b4f21624a4becaa2dcde3cf 100644 (file)
@@ -36,22 +36,22 @@ RuleManagement::~RuleManagement()
 
 int RuleManagement::addRule(std::vector<char> data)
 {
-       context.addAuditRule(AuditRule(data));
+       context.getAudit().addRule(AuditRule(data));
        return 0;
 }
 
 int RuleManagement::removeRule(std::vector<char> data)
 {
-       context.removeAuditRule(AuditRule(data));
+       context.getAudit().removeRule(AuditRule(data));
        return 0;
 }
 
 std::vector<std::vector<char>> RuleManagement::getRules()
 {
        std::vector<std::vector<char>> ret;
-       auto rules = context.getAuditRules();
+       auto rules = context.getAudit().getRules();
        for (const auto& rule : rules) {
-               ret.push_back({rule.data(), rule.data() + rule.size()});
+               ret.push_back(rule.data());
        }
 
        return ret;
index cee926762cc544b899c33ffbd160411e4d40e19a..18223b1f1fec2c3b43dcada1d6e50bf4bc00ef2f 100644 (file)
 #include <cynara-client.h>
 #include <cynara-session.h>
 
+#include "rmi/rule-management.h"
+
 #include "server.h"
 
 using namespace std::placeholders;
 
 namespace {
 
-const std::string AUDIT_RAIL_MANAGER_ADDRESS = "/tmp/.audit-trail.sock";
+const std::string AUDIT_TRAIL_MANAGER_ADDRESS = "/tmp/.audit-trail.sock";
+
+std::unique_ptr<AuditTrail::RuleManagement> rule;
 
 } // namespace
 
 Server::Server()
 {
-       service.reset(new rmi::Service(AUDIT_RAIL_MANAGER_ADDRESS));
+       service.reset(new rmi::Service(AUDIT_TRAIL_MANAGER_ADDRESS));
 
        service->setPrivilegeChecker(std::bind(&Server::checkPeerPrivilege, this, _1, _2));
 
        service->expose(this, "", (runtime::FileDescriptor)(Server::registerNotificationSubscriber)(std::string));
        service->expose(this, "", (int)(Server::unregisterNotificationSubscriber)(std::string, int));
 
-       audit.setPID(::getpid());
-       audit.setEnabled(1);
-       audit.setMainloop(&service->mainloop);
-       audit.setMessageHandler([this] (Audit::Message &msg) {
-               for (auto handler : this->auditHandlers) {
-                       handler(msg.first, msg.second);
-               }
-       });
+       audit.reset(new Audit());
+       audit->setEnabled(1);
+       audit->setPID(::getpid());
+
+       auditParser.reset(new AuditMessageParser(*audit, service->mainloop));
+       rule.reset(new AuditTrail::RuleManagement(*this));
 }
 
 Server::~Server()
index d9c8f188eb7e2f2fe0ad35e634abf8e4665f0069..b2a541a72d07d2f11d2d94a19c21fb8594e49665 100644 (file)
@@ -25,6 +25,7 @@
 #include <klay/rmi/service.h>
 
 #include "audit/audit.h"
+#include "audit/audit-message-parser.h"
 
 class Server final {
 public:
@@ -75,31 +76,22 @@ public:
        runtime::FileDescriptor registerNotificationSubscriber(const std::string& name);
        int unregisterNotificationSubscriber(const std::string& name, int id);
 
-       void setAuditHandler(AuditHandler&& handler)
+       Audit &getAudit()
        {
-               auditHandlers.push_back(handler);
+               return *audit;
        }
 
-       void addAuditRule(const AuditRule &rule)
+       AuditMessageParser &getAuditParser()
        {
-               audit.addRule(rule);
-       }
-
-       void removeAuditRule(const AuditRule &rule)
-       {
-               audit.removeRule(rule);
-       }
-
-       std::vector<AuditRule> getAuditRules()
-       {
-               return audit.getRules();
+               return *auditParser;
        }
 
 private:
-       Audit audit;
+       std::unique_ptr<Audit> audit;
+       std::unique_ptr<AuditMessageParser> auditParser;
+
        std::string securityLabel;
        std::unique_ptr<rmi::Service> service;
-       std::vector<AuditHandler> auditHandlers;
 };
 
 #endif //__AUDIT_TRAIL_SERVER_H__