1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright (C) 2018-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Kea Hooks Basic
// Commercial End User License Agreement v2.0. See COPYING file in the premium/
// directory.

#include <config.h>

#include <cfg_attribute.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <eval/evaluate.h>
#include <util/encode/encode.h>
#include <sstream><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <iomanip><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace std;
using namespace isc;
using namespace isc::data;
using namespace isc::dhcp;

namespace isc {
namespace radius {

void
CfgAttributes::add(const AttrDefPtr& def,
                   const ConstAttributePtr& attr,
                   const dhcp::ExpressionPtr& expr,
                   const std::string& test) {
    if (!def) {
        isc_throw(BadValue, "no attribute definition");
    }
    container_.insert(pair<const uint8_t, AttributeValue>
                (def->type_, AttributeValue(def, attr, expr, test)));
}

bool
CfgAttributes::del(const uint8_t type) {
    auto it = container_.find(type);
    if (it != container_.end()) {
        container_.erase(it);
        return (true);
    }
    return (false);
}

AttrDefPtr
CfgAttributes::getDef(const uint8_t type) const {
    auto it = container_.find(type);
    if (it == container_.end()) {
        return (AttrDefPtr());
    }
    return (it->second.def_);
}

ConstAttributePtr
CfgAttributes::get(const uint8_t type) const {
    auto it = container_.find(type);
    if (it == container_.end()) {
        return (AttributePtr());
    }
    return (it->second.attr_);
}

ExpressionPtr
CfgAttributes::getExpr(const uint8_t type) const {
    auto it = container_.find(type);
    if (it == container_.end()) {
        return (ExpressionPtr());
    }
    return (it->second.expr_);
}

string
CfgAttributes::getTest(const uint8_t type) const {
    auto it = container_.find(type);
    if (it == container_.end()) {
        return ("");
    }
    return (it->second.test_);
}

Attributes
CfgAttributes::getAll() const {
    Attributes attrs;
    for (auto const& it : container_) {
        attrs.add(it.second.attr_);
    }
    return (attrs);
}

Attributes
CfgAttributes::getEvalAll(Pkt& pkt) {
    Attributes attrs;
    for (auto const& it : container_) {
        const ExpressionPtr& match_expr = it.second.expr_;
        if (!match_expr) {
            attrs.add(it.second.attr_);
            continue;
        }
        string value = evaluateString(*match_expr, pkt);
        if (value.empty()) {
            continue;
        }
        AttrDefPtr def = it.second.def_;
        if (!def) {
            continue;
        }
        AttributePtr attr;
        vector<uint8_t> binary;
        binary.resize(value.size());
        memmove(&binary[0], value.c_str(), binary.size());
        attrs.add(Attribute::fromBytes(def, binary));
    }
    return (attrs);
}

ElementPtr
CfgAttributes::toElement() const {
    ElementPtr result = Element::createList();
    for (auto const& it : container_) {
        AttrDefPtr def = it.second.def_;
        if (!def) {
            continue;
        }
        ElementPtr map;
        if (!it.second.test_.empty()) {
            map = Element::createMap();
            map->set("type", Element::create(static_cast<int>(it.first)));
            map->set("expr", Element::create(it.second.test_));
            map->set("name", Element::create(def->name_));
        } else if (it.second.attr_) {
            map = it.second.attr_->toElement();
        }
        result->add(map);
    }
    return (result);
}

} // end of namespace isc::radius
} // end of namespace isc