1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef KVSTORE_DATASERVICE_H 17 #define KVSTORE_DATASERVICE_H 18 19 #include <fcntl.h> 20 #include <map> 21 #include <mutex> 22 #include <set> 23 24 #include "account_delegate.h" 25 #include "clone/clone_backup_info.h" 26 #include "clone/secret_key_backup_data.h" 27 #include "feature_stub_impl.h" 28 #include "ikvstore_data_service.h" 29 #include "ithread_pool.h" 30 #include "kvstore_device_listener.h" 31 #include "kvstore_meta_manager.h" 32 #include "kvstore_data_service_stub.h" 33 #include "metadata/secret_key_meta_data.h" 34 #include "metadata/store_meta_data.h" 35 #include "reporter.h" 36 #include "runtime_config.h" 37 #include "screen/screen_manager.h" 38 #include "security/security.h" 39 #include "system_ability.h" 40 #include "executor_pool.h" 41 #include "types.h" 42 #include "unique_fd.h" 43 44 namespace OHOS::DistributedKv { 45 using namespace DistributedData; 46 class KvStoreAccountObserver; 47 class KvStoreScreenObserver; 48 class KvStoreDataService : public SystemAbility, public KvStoreDataServiceStub { 49 DECLARE_SYSTEM_ABILITY(KvStoreDataService); 50 using Handler = std::function<void(int, std::map<std::string, std::vector<std::string>> &)>; 51 52 public: 53 struct UserInfo { 54 std::string userId; 55 std::set<std::string> bundles; 56 }; 57 struct BundleInfo { 58 std::string bundleName; 59 std::string appId; 60 std::string type; 61 int32_t uid; 62 uint32_t tokenId; 63 std::string userId; 64 std::set<std::string> storeIDs; 65 }; 66 using StoreMetaData = DistributedData::StoreMetaData; 67 using SecretKeyBackupData = DistributedData::SecretKeyBackupData; 68 using CloneBackupInfo = DistributedData::CloneBackupInfo; 69 // record kvstore meta version for compatible, should update when modify kvstore meta structure. 70 static constexpr uint32_t STORE_VERSION = 0x03000001; 71 72 explicit KvStoreDataService(bool runOnCreate = false); 73 explicit KvStoreDataService(int32_t systemAbilityId, bool runOnCreate = false); 74 virtual ~KvStoreDataService(); 75 76 void RegisterHandler(const std::string &name, Handler &handler); 77 void RegisterStoreInfo(); 78 bool IsExist(const std::string &infoName, std::map<std::string, std::vector<std::string>> &filterInfo, 79 std::string &metaParam); 80 void DumpStoreInfo(int fd, std::map<std::string, std::vector<std::string>> ¶ms); 81 void FilterData(std::vector<StoreMetaData> &metas, std::map<std::string, std::vector<std::string>> &filterInfo); 82 void PrintfInfo(int fd, const std::vector<StoreMetaData> &metas); 83 std::string GetIndentation(int size); 84 85 void RegisterUserInfo(); 86 void BuildData(std::map<std::string, UserInfo> &datas, const std::vector<StoreMetaData> &metas); 87 void PrintfInfo(int fd, const std::map<std::string, UserInfo> &datas); 88 void DumpUserInfo(int fd, std::map<std::string, std::vector<std::string>> ¶ms); 89 90 void RegisterBundleInfo(); 91 void BuildData(std::map<std::string, BundleInfo> &datas, const std::vector<StoreMetaData> &metas); 92 void PrintfInfo(int fd, const std::map<std::string, BundleInfo> &datas); 93 void DumpBundleInfo(int fd, std::map<std::string, std::vector<std::string>> ¶ms); 94 95 Status RegisterClientDeathObserver(const AppId &appId, sptr<IRemoteObject> observer) override; 96 97 sptr<IRemoteObject> GetFeatureInterface(const std::string &name) override; 98 99 int32_t ClearAppStorage(const std::string &bundleName, int32_t userId, int32_t appIndex, int32_t tokenId) override; 100 101 void OnDump() override; 102 103 int Dump(int fd, const std::vector<std::u16string> &args) override; 104 105 void OnStart() override; 106 107 void OnStop() override; 108 109 void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 110 111 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 112 113 void AccountEventChanged(const AccountEventInfo &eventInfo); 114 115 void SetCompatibleIdentify(const AppDistributedKv::DeviceInfo &info) const; 116 117 void OnDeviceOnline(const AppDistributedKv::DeviceInfo &info); 118 119 void OnDeviceOffline(const AppDistributedKv::DeviceInfo &info); 120 121 void OnDeviceOnReady(const AppDistributedKv::DeviceInfo &info); 122 123 void OnSessionReady(const AppDistributedKv::DeviceInfo &info); 124 125 int32_t OnUninstall(const std::string &bundleName, int32_t user, int32_t index); 126 127 int32_t OnUpdate(const std::string &bundleName, int32_t user, int32_t index); 128 129 int32_t OnInstall(const std::string &bundleName, int32_t user, int32_t index); 130 131 int32_t OnScreenUnlocked(int32_t user); 132 133 int32_t OnExtension(const std::string &extension, MessageParcel &data, MessageParcel &reply) override; 134 int32_t OnBackup(MessageParcel &data, MessageParcel &reply); 135 int32_t OnRestore(MessageParcel &data, MessageParcel &reply); 136 bool WriteBackupInfo(const std::string &content, const std::string &backupPath); 137 bool GetSecretKeyBackup( 138 const std::vector<DistributedData::CloneBundleInfo> &bundleInfos, 139 const std::string &userId, const std::vector<uint8_t> &iv, std::string &content); 140 141 private: 142 void NotifyAccountEvent(const AccountEventInfo &eventInfo); 143 class KvStoreClientDeathObserverImpl { 144 public: 145 KvStoreClientDeathObserverImpl(const AppId &appId, KvStoreDataService &service, sptr<IRemoteObject> observer); 146 explicit KvStoreClientDeathObserverImpl(KvStoreDataService &service); 147 explicit KvStoreClientDeathObserverImpl(KvStoreClientDeathObserverImpl &&impl); 148 KvStoreClientDeathObserverImpl &operator=(KvStoreClientDeathObserverImpl &&impl); 149 150 virtual ~KvStoreClientDeathObserverImpl(); 151 152 pid_t GetPid() const; 153 154 private: 155 class KvStoreDeathRecipient : public IRemoteObject::DeathRecipient { 156 public: 157 explicit KvStoreDeathRecipient(KvStoreClientDeathObserverImpl &kvStoreClientDeathObserverImpl); 158 virtual ~KvStoreDeathRecipient(); 159 void OnRemoteDied(const wptr<IRemoteObject> &remote) override; 160 161 private: 162 KvStoreClientDeathObserverImpl &kvStoreClientDeathObserverImpl_; 163 }; 164 void NotifyClientDie(); 165 void Reset(); 166 pid_t uid_; 167 pid_t pid_; 168 uint32_t token_; 169 AppId appId_; 170 KvStoreDataService &dataService_; 171 sptr<IRemoteObject> observerProxy_; 172 sptr<KvStoreDeathRecipient> deathRecipient_; 173 }; 174 175 void Initialize(); 176 177 void LoadFeatures(); 178 179 void StartService(); 180 181 void InitSecurityAdapter(std::shared_ptr<ExecutorPool> executors); 182 183 void OnStoreMetaChanged(const std::vector<uint8_t> &key, const std::vector<uint8_t> &value, CHANGE_FLAG flag); 184 185 Status AppExit(pid_t uid, pid_t pid, uint32_t token, const AppId &appId); 186 187 void LoadConfigs(); 188 189 void InitExecutor(); 190 191 std::vector<uint8_t> ReEncryptKey(const std::string &key, SecretKeyMetaData &secretKeyMeta, 192 const StoreMetaData &metaData, const std::vector<uint8_t> &iv); 193 194 bool ParseSecretKeyFile(MessageParcel &data, SecretKeyBackupData &backupData); 195 196 bool RestoreSecretKey(const SecretKeyBackupData::BackupItem &item, const std::string &userId, 197 const std::vector<uint8_t> &iv); 198 bool ImportCloneKey(const std::string &keyStr); 199 void DeleteCloneKey(); 200 201 std::string GetBackupReplyCode(int replyCode, const std::string &info = ""); 202 203 int32_t ReplyForRestore(MessageParcel &reply, int32_t result); 204 205 static constexpr int TEN_SEC = 10; 206 207 ConcurrentMap<uint32_t, std::map<int32_t, KvStoreClientDeathObserverImpl>> clients_; 208 std::shared_ptr<KvStoreAccountObserver> accountEventObserver_; 209 std::shared_ptr<KvStoreScreenObserver> screenEventObserver_; 210 211 std::shared_ptr<Security> security_; 212 ConcurrentMap<std::string, sptr<DistributedData::FeatureStubImpl>> features_; 213 std::shared_ptr<KvStoreDeviceListener> deviceInnerListener_; 214 std::shared_ptr<ExecutorPool> executors_; 215 static constexpr int VERSION_WIDTH = 11; 216 static constexpr const char *INDENTATION = " "; 217 static constexpr int32_t FORMAT_BLANK_SIZE = 32; 218 static constexpr char FORMAT_BLANK_SPACE = ' '; 219 static constexpr int32_t PRINTF_COUNT_2 = 2; 220 static constexpr int MAXIMUM_PARAMETER_LIMIT = 3; 221 static constexpr pid_t INVALID_UID = -1; 222 static constexpr pid_t INVALID_PID = -1; 223 static constexpr uint32_t INVALID_TOKEN = 0; 224 }; 225 } 226 #endif // KVSTORE_DATASERVICE_H