blob: 2fb09c17a4c611ce513a6799f995f7813310a230 [file] [log] [blame]
Janis Danisevskis6d449e82017-06-07 18:03:31 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "grant_store.h"
18
19#include <algorithm>
20#include <sstream>
21
22namespace keystore {
23
24static constexpr uint64_t kInvalidGrantNo = std::numeric_limits<uint64_t>::max();
25static const char* kKeystoreGrantInfix = "_KEYSTOREGRANT_";
26static constexpr size_t kKeystoreGrantInfixLength = 15;
27
Janis Danisevskisd3024ed2017-09-01 13:24:23 -070028Grant::Grant(const std::string& alias, const std::string& owner_dir_name, const uid_t owner_uid,
29 const uint64_t grant_no)
30 : alias_(alias), owner_dir_name_(owner_dir_name), owner_uid_(owner_uid),
31 grant_no_(grant_no) {}
Janis Danisevskis6d449e82017-06-07 18:03:31 -070032
33static std::pair<uint64_t, std::string> parseGrantAlias(const std::string& grantAlias) {
34 auto pos = grantAlias.rfind(kKeystoreGrantInfix);
35 if (pos == std::string::npos) return {kInvalidGrantNo, ""};
36 std::stringstream s(grantAlias.substr(pos + kKeystoreGrantInfixLength));
37 std::string wrapped_alias = grantAlias.substr(0, pos);
38 uint64_t grant_no = kInvalidGrantNo;
39 s >> grant_no;
40 if (s.fail() || grant_no == kInvalidGrantNo) return {kInvalidGrantNo, ""};
41 return {grant_no, wrapped_alias};
42}
43
Janis Danisevskisd3024ed2017-09-01 13:24:23 -070044std::string GrantStore::put(const uid_t uid, const std::string& alias,
45 const std::string& owner_dir_name, const uid_t owner_uid) {
Janis Danisevskis6d449e82017-06-07 18:03:31 -070046 std::stringstream s;
47 s << alias << kKeystoreGrantInfix;
48 auto& uid_grant_list = grants_[uid];
49
50 bool success = false;
51 auto iterator = std::find_if(uid_grant_list.begin(), uid_grant_list.end(),
52 [&](auto& entry) {
Janis Danisevskisd3024ed2017-09-01 13:24:23 -070053 return success = entry.alias_ == alias && entry.owner_dir_name_ == owner_dir_name
54 && entry.owner_uid_ == owner_uid;
Janis Danisevskis6d449e82017-06-07 18:03:31 -070055 });
56 while (!success) {
Janis Danisevskisd3024ed2017-09-01 13:24:23 -070057 std::tie(iterator, success) = uid_grant_list.emplace(alias, owner_dir_name, owner_uid,
58 std::rand());
Janis Danisevskis6d449e82017-06-07 18:03:31 -070059 }
60 s << iterator->grant_no_;
61 return s.str();
62}
63
64const Grant* GrantStore::get(const uid_t uid, const std::string& alias) const {
65 uint64_t grant_no;
66 std::string wrappedAlias;
67 std::tie(grant_no, wrappedAlias) = parseGrantAlias(alias);
68 if (grant_no == kInvalidGrantNo) return nullptr;
69 auto uid_set_iter = grants_.find(uid);
70 if (uid_set_iter == grants_.end()) return nullptr;
71 auto& uid_grant_list = uid_set_iter->second;
72 auto grant = uid_grant_list.find(grant_no);
73 if (grant == uid_grant_list.end()) return nullptr;
74 if (grant->alias_ != wrappedAlias) return nullptr;
75 return &(*grant);
76}
77
Janis Danisevskisd3024ed2017-09-01 13:24:23 -070078bool GrantStore::removeByFileAlias(const uid_t uid, const std::string& alias) {
79 auto& uid_grant_list = grants_[uid];
Janis Danisevskis6d449e82017-06-07 18:03:31 -070080 for (auto i = uid_grant_list.begin(); i != uid_grant_list.end(); ++i) {
Janis Danisevskisd3024ed2017-09-01 13:24:23 -070081 if (i->alias_ == alias) {
Janis Danisevskis6d449e82017-06-07 18:03:31 -070082 uid_grant_list.erase(i);
83 return true;
84 }
85 }
86 return false;
87}
88
89} // namespace keystore