Janis Danisevskis | 6d449e8 | 2017-06-07 18:03:31 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 22 | namespace keystore { |
| 23 | |
| 24 | static constexpr uint64_t kInvalidGrantNo = std::numeric_limits<uint64_t>::max(); |
| 25 | static const char* kKeystoreGrantInfix = "_KEYSTOREGRANT_"; |
| 26 | static constexpr size_t kKeystoreGrantInfixLength = 15; |
| 27 | |
Janis Danisevskis | d3024ed | 2017-09-01 13:24:23 -0700 | [diff] [blame] | 28 | Grant::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 Danisevskis | 6d449e8 | 2017-06-07 18:03:31 -0700 | [diff] [blame] | 32 | |
| 33 | static 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 Danisevskis | d3024ed | 2017-09-01 13:24:23 -0700 | [diff] [blame] | 44 | std::string GrantStore::put(const uid_t uid, const std::string& alias, |
| 45 | const std::string& owner_dir_name, const uid_t owner_uid) { |
Janis Danisevskis | 6d449e8 | 2017-06-07 18:03:31 -0700 | [diff] [blame] | 46 | 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 Danisevskis | d3024ed | 2017-09-01 13:24:23 -0700 | [diff] [blame] | 53 | return success = entry.alias_ == alias && entry.owner_dir_name_ == owner_dir_name |
| 54 | && entry.owner_uid_ == owner_uid; |
Janis Danisevskis | 6d449e8 | 2017-06-07 18:03:31 -0700 | [diff] [blame] | 55 | }); |
| 56 | while (!success) { |
Janis Danisevskis | d3024ed | 2017-09-01 13:24:23 -0700 | [diff] [blame] | 57 | std::tie(iterator, success) = uid_grant_list.emplace(alias, owner_dir_name, owner_uid, |
| 58 | std::rand()); |
Janis Danisevskis | 6d449e8 | 2017-06-07 18:03:31 -0700 | [diff] [blame] | 59 | } |
| 60 | s << iterator->grant_no_; |
| 61 | return s.str(); |
| 62 | } |
| 63 | |
| 64 | const 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 Danisevskis | d3024ed | 2017-09-01 13:24:23 -0700 | [diff] [blame] | 78 | bool GrantStore::removeByFileAlias(const uid_t uid, const std::string& alias) { |
| 79 | auto& uid_grant_list = grants_[uid]; |
Janis Danisevskis | 6d449e8 | 2017-06-07 18:03:31 -0700 | [diff] [blame] | 80 | for (auto i = uid_grant_list.begin(); i != uid_grant_list.end(); ++i) { |
Janis Danisevskis | d3024ed | 2017-09-01 13:24:23 -0700 | [diff] [blame] | 81 | if (i->alias_ == alias) { |
Janis Danisevskis | 6d449e8 | 2017-06-07 18:03:31 -0700 | [diff] [blame] | 82 | uid_grant_list.erase(i); |
| 83 | return true; |
| 84 | } |
| 85 | } |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | } // namespace keystore |