summaryrefslogtreecommitdiff
path: root/test/common/utility.cpp
blob: 478deeb56e676a5c4352878e5365677b48a474d8 (plain)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Copyright (C) 2022 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include "utility.hpp"

void
utility::load_policy_data(std::string _input,
    std::vector<vsomeip_v3::configuration_element> &_elements,
    std::set<std::string> &_failed) {

    boost::property_tree::ptree its_tree;
    try {
        boost::property_tree::json_parser::read_json(_input, its_tree);
        _elements.push_back({ _input, its_tree });
    }
    catch (boost::property_tree::json_parser_error &e) {
        _failed.insert(_input);
    }
}

void
utility::read_data(const std::set<std::string> &_input,
    std::vector<vsomeip_v3::configuration_element> &_elements,
    std::set<std::string> &_failed) {

    for (auto i : _input) {
        if (vsomeip_v3::utility::is_file(i)) {
            load_policy_data(i, _elements, _failed);
        } else if (vsomeip_v3::utility::is_folder(i)) {
            std::map<std::string, bool> its_names;
            boost::filesystem::path its_path(i);
            for (auto j = boost::filesystem::directory_iterator(its_path);
                    j != boost::filesystem::directory_iterator();
                    j++) {
                std::string name = j->path().string() + "/vsomeip_security.json";
                if (vsomeip_v3::utility::is_file(name))
                    its_names[name] = true;
            }

            for (const auto& n : its_names)
                load_policy_data(n.first, _elements, _failed);
        }
    }
}

std::set<std::string>
utility::get_all_files_in_dir(const std::string &_dir_path,
        const std::vector<std::string> &_dir_skip_list) {

    // Create a vector of string
    std::set<std::string> list_of_files;
    try {
        // Check if given path exists and points to a directory
        if (boost::filesystem::exists(_dir_path)
                && boost::filesystem::is_directory(_dir_path)) {
            // Create a Recursive Directory Iterator object and points to the
            // starting of directory
            boost::filesystem::recursive_directory_iterator iter(_dir_path);
            // Create a Recursive Directory Iterator object pointing to end.
            boost::filesystem::recursive_directory_iterator end;
            // Iterate till end
            while (iter != end) {
                // Check if current entry is a directory and if exists in
                // skip list
                if (boost::filesystem::is_directory(iter->path())
                        && (std::find(_dir_skip_list.begin(),
                                _dir_skip_list.end(), iter->path().filename())
                                != _dir_skip_list.end())) {
                    // Boost Filesystem  API to skip current directory iteration
#if VSOMEIP_BOOST_VERSION < 108100
                    iter.no_push();
#else
                    iter.disable_recursion_pending();
#endif
                } else {
                    // Add the name in vector
                    list_of_files.insert(iter->path().string());
                }
                boost::system::error_code ec;
                // Increment the iterator to point to next entry in recursive iteration
                iter.increment(ec);
                if (ec) {
                    std::cerr << "Error While Accessing : " << iter->path().string() << " :: " << ec.message() << '\n';
                }
            }
        }
    }
    catch (std::system_error & e) {
        std::cerr << "Exception :: " << e.what();
    }
    return list_of_files;
}

std::string
utility::get_policies_path() {

    return boost::filesystem::canonical(
            boost::filesystem::current_path()).string()
            + "/../test/common/examples_policies";
}

vsomeip_sec_client_t
utility::create_uds_client(uid_t user, gid_t group) {
    vsomeip_sec_client_t result;
    result.client_type = VSOMEIP_CLIENT_UDS;
    result.client.uds_client = { user, group };
    return result;
}

void
utility::force_check_credentials(
        std::vector<vsomeip_v3::configuration_element> &_policy_elements,
        std::string _value) {

    for(auto &i : _policy_elements) {
        try {
            boost::property_tree::ptree &security
                = i.tree_.get_child("security");
            boost::property_tree::ptree &credentials
                = security.get_child("check_credentials");
            if (credentials.get_value<std::string>().compare(_value)) {
                security.erase("check_credentials");
                credentials.put("check_credentials", _value);
            }
        }
        catch(...) {}
    }
        }

void utility::get_policy_uids(vsomeip_v3::configuration_element &_policy_element,
                              std::vector<vsomeip_v3::uid_t> &_out_uids)
{
    try {
        std::vector<std::string> user_ids;
        auto policy_tree = _policy_element.tree_.get_child("security.policies");
        for (auto policy_node : policy_tree) {
            auto optional_credential_node =
                    policy_node.second.get_child_optional("credentials.uid");
            if (optional_credential_node) {
                auto optional_user_id =
                        optional_credential_node.get().get_value_optional<std::string>();
                if (optional_user_id) {
                    user_ids.push_back(optional_user_id.get());
                }
            }
        }
        for (const std::string &uid_string : user_ids) {
            _out_uids.push_back((vsomeip_v3::uid_t)std::strtoul(uid_string.c_str(), NULL, 0));
        }
    } catch (...) {
        std::cerr << "Caught exception while reading user ids in policy element \""
                  << _policy_element.name_ << "\"!" << std::endl;
    }
}

void utility::get_policy_services(vsomeip_v3::configuration_element &_policy_element,
                                  std::vector<vsomeip_v3::service_t> &_out_services)
{
    try {
        std::vector<std::string> services;
        auto policy_tree = _policy_element.tree_.get_child("security.policies");
        for (auto policy_node : policy_tree) {
            // Get allowed request services.
            auto allow_requests = policy_node.second.get_child_optional("allow.requests");
            if (allow_requests) {
                for (auto &request_node : allow_requests.get()) {
                    auto optional_service = request_node.second.get_child("service")
                                                    .get_value_optional<std::string>();
                    if (optional_service) {
                        services.push_back(optional_service.get());
                    }
                }
            }
            // Get denied request services.
            auto deny_requests = policy_node.second.get_child_optional("deny.requests");
            if (deny_requests) {
                for (auto &request_node : deny_requests.get()) {
                    auto optional_service = request_node.second.get_child("service")
                                                    .get_value_optional<std::string>();
                    if (optional_service) {
                        services.push_back(optional_service.get());
                    }
                }
            }
        }
        for (const std::string &service_str : services) {
            _out_services.push_back(
                    (vsomeip_v3::service_t)std::strtoul(service_str.c_str(), NULL, 0));
        }
    } catch (...) {
        std::cerr << "Caught exception while reading services in policy element \""
                  << _policy_element.name_ << "\"!" << std::endl;
    }
}

void utility::add_security_whitelist(vsomeip_v3::configuration_element &_policy_element,
                                     const bool _check_whitelist)
{
    std::vector<vsomeip_v3::uid_t> user_ids;
    get_policy_uids(_policy_element, user_ids);

    std::vector<vsomeip_v3::service_t> services;
    get_policy_services(_policy_element, services);

    add_security_whitelist(_policy_element, user_ids, services, _check_whitelist);
}

void utility::add_security_whitelist(vsomeip_v3::configuration_element &_policy_element,
                                     const std::vector<vsomeip_v3::uid_t> &_user_ids,
                                     const std::vector<vsomeip_v3::service_t> &_services,
                                     const bool _check_whitelist)
{
    // Add the user ids to the whitelist.
    boost::property_tree::ptree id_array_node;
    for (auto user_id : _user_ids) {
        boost::property_tree::ptree id_node;
        id_node.put("", user_id);
        id_array_node.push_back(std::make_pair("", id_node));
    }
    _policy_element.tree_.add_child("security-update-whitelist.uids", id_array_node);

    // Add the services to the whitelist.
    boost::property_tree::ptree service_array_node;
    for (auto service : _services) {
        boost::property_tree::ptree service_node;
        service_node.put("", service);
        service_array_node.push_back(std::make_pair("", service_node));
    }
    _policy_element.tree_.add_child("security-update-whitelist.services", service_array_node);

    // Update the 'check_whitelist' flag.
    _policy_element.tree_.add<bool>("security-update-whitelist.check-whitelist", _check_whitelist);
}