Fix parameter type and add methods to get field value 35/177735/6
authorseolheui kim <s414.kim@samsung.com>
Thu, 3 May 2018 08:04:56 +0000 (17:04 +0900)
committerseolheui kim <s414.kim@samsung.com>
Tue, 8 May 2018 03:08:56 +0000 (12:08 +0900)
Change-Id: Ibb2ec8eb0e219b8d7d63d25367cd79286cbbaff6
Signed-off-by: seolheui kim <s414.kim@samsung.com>
lib/audit-rule/field.h
lib/audit-rule/rule.cpp
lib/audit-rule/rule.h
lib/audit-trail/rule.cpp

index 181185cb425b7c7787056d0769d021f97d76c6cb..e37070af81b493b0b608da3efc32395645fa3dec 100644 (file)
@@ -25,9 +25,7 @@
 class name : public Field<int> { \
 public:        \
        name() : Field(FieldType::name) {} \
-       name(int value) : Field(FieldType::name) { \
-               _op = static_cast<unsigned int>(Operator::Equal); \
-               _value = value; \
+       name(int value) : Field(FieldType::name, Operator::Equal, value) {\
        }       \
 };
 
@@ -35,9 +33,7 @@ public:       \
 class name : public Field<std::string> { \
 public: \
        name() : Field(FieldType::name) {} \
-       name(std::string value) : Field(FieldType::name) { \
-               _op = static_cast<unsigned int>(Operator::Equal); \
-               _value = value; \
+       name(std::string value) : Field(FieldType::name, Operator::Equal, value) { \
        }       \
 };
 
@@ -92,6 +88,7 @@ enum class FieldType : unsigned int {
 };
 
 enum class Operator : unsigned int {
+       Default = AUDIT_OPERATORS,
        Equal = AUDIT_EQUAL,
        NotEqual = AUDIT_NOT_EQUAL,
        LessThan = AUDIT_LESS_THAN,
@@ -105,16 +102,16 @@ enum class Operator : unsigned int {
 class FieldBase {
 public:
        virtual void emit(std::vector<char> &rule) const = 0;
-       virtual unsigned int type() const = 0;
+       virtual FieldType type() const = 0;
+       virtual Operator op() const = 0;
 
-       static bool isString(unsigned int type)
+       static bool isString(FieldType type)
        {
-               FieldType ftype = static_cast<FieldType>(type);
-               return (ftype == FieldType::Tag) ||
-                       (ftype == FieldType::WatchPath) ||
-                       (ftype == FieldType::WatchDir) ||
-                       (ftype == FieldType::Arch) ||
-                       (ftype >= FieldType::SubjectUser && ftype <= FieldType::ObjectLevHigh);
+               return (type == FieldType::Tag) ||
+                       (type == FieldType::WatchPath) ||
+                       (type == FieldType::WatchDir) ||
+                       (type == FieldType::Arch) ||
+                       (type >= FieldType::SubjectUser && type <= FieldType::ObjectLevHigh);
        }
 };
 
@@ -124,12 +121,11 @@ public:
        using RuleData = struct audit_rule_data;
 
        Field(FieldType type)
-               : _type(static_cast<unsigned int>(type)),
-               _op(0), _value()
+               : _type(type), _op(Operator::Default), _value()
        {
        }
        /* TODO: to be removed below constructor */
-       Field(unsigned int type, unsigned int op, T value)
+       Field(FieldType type, Operator op, T value)
                : _type(type), _op(op), _value(value)
        {
                if (FieldBase::isString(type) && std::is_same<int, T>::value)
@@ -139,11 +135,21 @@ public:
        {
        }
 
-       unsigned int type() const
+       FieldType type() const
        {
                return _type;
        }
 
+       Operator op() const
+       {
+               return _op;
+       }
+
+       T value() const
+       {
+               return _value;
+       }
+
        Field operator == (T value) {
                return create(value, Operator::Equal);
        }
@@ -178,14 +184,14 @@ public:
 
        void emit(std::vector<char> &rule) const {
                auto r = reinterpret_cast<RuleData *>(rule.data());
-               r->fields[r->field_count] = _type;
-               r->fieldflags[r->field_count] = _op;
+               r->fields[r->field_count] = static_cast<unsigned int>(_type);
+               r->fieldflags[r->field_count] = static_cast<unsigned int>(_op);
                emitValue(rule, _value);
        }
 
 private:
        Field create(T value, Operator op) {
-               _op = static_cast<unsigned int>(op);
+               _op = op;
                _value = value;
                return *this;
        }
@@ -208,8 +214,8 @@ private:
        }
 
 protected:
-       unsigned int _type;
-       unsigned int _op;
+       FieldType _type;
+       Operator _op;
        T _value;
 };
 
index aa1478169bdc8f7027c953c761e33dc5c6820b58..a347e49fcf052a5f28f07937055e302394d9933a 100644 (file)
@@ -73,12 +73,12 @@ void Rule::setTag(const std::string &tag)
 
 void Rule::set(Action action)
 {
-       ruleData()->action = underlying_t(action);
+       ruleData()->action = static_cast<unsigned int>(action);
 }
 
 void Rule::set(Filter filter)
 {
-       ruleData()->flags = underlying_t(filter);
+       ruleData()->flags = static_cast<unsigned int>(filter);
 }
 
 void Rule::setMask()
@@ -141,12 +141,16 @@ void Rule::setComponents(const std::vector<char> &rule)
        }
 
        for (unsigned int i = 0; i < r->field_count; i++) {
-               if (FieldBase::isString(r->fields[i])) {
+               if (FieldBase::isString(FieldType(r->fields[i]))) {
                        std::string value(ruleBuf, ruleBuf + r->values[i]);
-                       conditions[r->fields[i]].reset(new(std::nothrow) Field<std::string>(r->fields[i], r->fieldflags[i], value));
+                       conditions[FieldType(r->fields[i])].reset(
+                                       new(std::nothrow) Field<std::string>(
+                                               FieldType(r->fields[i]), Operator(r->fieldflags[i]), value));
                        ruleBuf += r->values[i];
                } else {
-                       conditions[r->fields[i]].reset(new(std::nothrow) Field<int>(r->fields[i], r->fieldflags[i], r->values[i]));
+                       conditions[FieldType(r->fields[i])].reset(
+                                       new(std::nothrow) Field<int>(
+                                               FieldType(r->fields[i]), Operator(r->fieldflags[i]), r->values[i]));
                }
        }
 }
index 6c1ea4329a494b8eaf777ccc394de0447478464a..9fac288ba97a05dd0f5f7e8ff2e6ef303b9ee7c2 100644 (file)
@@ -19,6 +19,7 @@
 
 #include <map>
 #include <memory>
+#include <type_traits>
 
 #include <linux/audit.h>
 #include <klay/error.h>
@@ -99,6 +100,8 @@ public:
        void setCondition(const Field<T> &field);
        template <typename T>
        void unsetCondition(const Field<T> &field);
+       template <typename T>
+       void getConditionValue(FieldType type, T &ret);
 
        void setMask();
        void setMask(unsigned int syscall);
@@ -111,13 +114,10 @@ protected:
        void set(Action action);
        void set(Filter filter);
 
-private:
-       template <typename E>
-       constexpr typename std::underlying_type<E>::type underlying_t(E value) noexcept
-       {
-               return static_cast<typename std::underlying_type<E>::type>(value);
-       }
+protected:
+       std::map<FieldType, std::shared_ptr<FieldBase>> conditions;
 
+private:
        RuleData *ruleData()
        {
                return reinterpret_cast<RuleData*>(buf.data());
@@ -127,7 +127,6 @@ private:
 private:
        RuleType _type;
        std::vector<char> buf;
-       std::map<unsigned int, std::shared_ptr<FieldBase>> conditions;
 };
 
 template <typename T>
@@ -143,4 +142,22 @@ void Rule::unsetCondition(const Field<T> &field)
                conditions.erase(field.type());
 }
 
+template <typename T>
+void Rule::getConditionValue(FieldType type, T &ret)
+{
+       auto condition = conditions[type].get();
+       if (condition == nullptr) {
+               //INFO("Condition isn't exist");
+               return;
+       }
+
+       if (!FieldBase::isString(type)
+                       && std::is_same<std::string, T>::value) {
+               //INFO("Invalid field value type");
+               return;
+       }
+       Field<T> *field = reinterpret_cast<Field<T>*>(condition);
+       ret = field->value();
+}
+
 #endif /*__AUDIT_RULE_H__*/
index edda040e09dba043497a7ba3f19f09f8023553a9..85586e4c02ccc540ee8d54b33ac501a893f9713e 100644 (file)
@@ -95,12 +95,14 @@ int audit_rule_add_condition(audit_rule_h handle, unsigned int field,
        RET_ON_FAILURE(handle, AUDIT_TRAIL_ERROR_INVALID_PARAMETER);
 
        try {
-               GetAuditRule(handle).setCondition(Field<int>{field, op, (int)(intptr_t)value});
+               GetAuditRule(handle).setCondition(
+                               Field<int>{FieldType(field), Operator(op), (int)(intptr_t)value});
                return AUDIT_TRAIL_ERROR_NONE;
        } catch (std::exception &e) {}
 
        try {
-               GetAuditRule(handle).setCondition(Field<std::string>{field, op, (char *)value});
+               GetAuditRule(handle).setCondition(
+                               Field<std::string>{FieldType(field), Operator(op), (char *)value});
                return AUDIT_TRAIL_ERROR_NONE;
        } catch (std::exception &e) {}
 
@@ -113,12 +115,14 @@ int audit_rule_remove_condition(audit_rule_h handle, unsigned int field,
        RET_ON_FAILURE(handle, AUDIT_TRAIL_ERROR_INVALID_PARAMETER);
 
        try {
-               GetAuditRule(handle).unsetCondition(Field<int>{field, op, (int)(intptr_t)value});
+               GetAuditRule(handle).unsetCondition(
+                               Field<int>{FieldType(field), Operator(op), (int)(intptr_t)value});
                return AUDIT_TRAIL_ERROR_NONE;
        } catch (std::exception &e) {}
 
        try {
-               GetAuditRule(handle).unsetCondition(Field<std::string>{field, op, (char *)value});
+               GetAuditRule(handle).unsetCondition(
+                               Field<std::string>{FieldType(field), Operator(op), (char *)value});
                return AUDIT_TRAIL_ERROR_NONE;
        } catch (std::exception &e) {}
 
@@ -153,7 +157,7 @@ int audit_rule_foreach_condition(audit_rule_h handle,
        char *tmp = r->buf;
 
        for (unsigned int i = 0; i < r->field_count; i++) {
-               if (FieldBase::isString(r->fields[i])) {
+               if (FieldBase::isString(FieldType(r->fields[i]))) {
                        std::string value(tmp, tmp + r->values[i]);
                        callback(r->fields[i], r->fieldflags[i],
                                        (void*)(value.c_str()), user_data);