• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/account_delegate.h"
25 #include "clone/clone_backup_info.h"
26 #include "clone/secret_key_backup_data.h"
27 #include "dfx/reporter.h"
28 #include "executor_pool.h"
29 #include "feature_stub_impl.h"
30 #include "ikvstore_data_service.h"
31 #include "ithread_pool.h"
32 #include "kvstore_data_service_stub.h"
33 #include "kvstore_device_listener.h"
34 #include "kvstore_meta_manager.h"
35 #include "metadata/secret_key_meta_data.h"
36 #include "metadata/store_meta_data.h"
37 #include "runtime_config.h"
38 #include "screen/screen_manager.h"
39 #include "security/security.h"
40 #include "system_ability.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>> &params);
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>> &params);
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>> &params);
94 
95     Status RegisterClientDeathObserver(const AppId &appId, sptr<IRemoteObject> observer,
96         const std::string &featureName) override;
97 
98     sptr<IRemoteObject> GetFeatureInterface(const std::string &name) override;
99 
100     int32_t ClearAppStorage(const std::string &bundleName, int32_t userId, int32_t appIndex, int32_t tokenId) override;
101 
102     int32_t Exit(const std::string &featureName) override;
103 
104     void OnDump() override;
105 
106     int Dump(int fd, const std::vector<std::u16string> &args) override;
107 
108     void OnStart() override;
109 
110     void OnStop() override;
111 
112     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
113 
114     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
115 
116     void AccountEventChanged(const AccountEventInfo &eventInfo);
117 
118     void SetCompatibleIdentify(const AppDistributedKv::DeviceInfo &info) const;
119 
120     void OnDeviceOnline(const AppDistributedKv::DeviceInfo &info);
121 
122     void OnDeviceOffline(const AppDistributedKv::DeviceInfo &info);
123 
124     void OnDeviceOnReady(const AppDistributedKv::DeviceInfo &info);
125 
126     void OnSessionReady(const AppDistributedKv::DeviceInfo &info);
127 
128     int32_t OnUninstall(const std::string &bundleName, int32_t user, int32_t index);
129 
130     int32_t OnUpdate(const std::string &bundleName, int32_t user, int32_t index);
131 
132     int32_t OnInstall(const std::string &bundleName, int32_t user, int32_t index);
133 
134     int32_t OnScreenUnlocked(int32_t user);
135 
136     int32_t OnExtension(const std::string &extension, MessageParcel &data, MessageParcel &reply) override;
137     int32_t OnBackup(MessageParcel &data, MessageParcel &reply);
138     int32_t OnRestore(MessageParcel &data, MessageParcel &reply);
139     bool WriteBackupInfo(const std::string &content, const std::string &backupPath);
140     std::string GetSecretKeyBackup(const std::vector<DistributedData::CloneBundleInfo> &bundleInfos,
141         const std::string &userId, const std::vector<uint8_t> &iv);
142 
143 private:
144     void NotifyAccountEvent(const AccountEventInfo &eventInfo);
145     class KvStoreClientDeathObserverImpl {
146     public:
147         KvStoreClientDeathObserverImpl(const AppId &appId, KvStoreDataService &service, sptr<IRemoteObject> observer,
148             const std::string &featureName);
149         explicit KvStoreClientDeathObserverImpl(KvStoreDataService &service);
150         explicit KvStoreClientDeathObserverImpl(KvStoreClientDeathObserverImpl &&impl);
151         KvStoreClientDeathObserverImpl &operator=(KvStoreClientDeathObserverImpl &&impl);
152         bool Insert(sptr<IRemoteObject> observer, const std::string &featureName);
153         bool Delete(const std::string &featureName);
154         bool Empty();
155         std::string GetAppId();
156         virtual ~KvStoreClientDeathObserverImpl();
157 
158         pid_t GetPid() const;
159 
160     private:
161         class KvStoreDeathRecipient : public IRemoteObject::DeathRecipient {
162         public:
163             explicit KvStoreDeathRecipient(KvStoreClientDeathObserverImpl &kvStoreClientDeathObserverImpl);
164             virtual ~KvStoreDeathRecipient();
165             void OnRemoteDied(const wptr<IRemoteObject> &remote) override;
166 
167         private:
168             KvStoreClientDeathObserverImpl &kvStoreClientDeathObserverImpl_;
169             std::atomic_bool clientDead_ = false;
170         };
171         void NotifyClientDie();
172         void Reset();
173         pid_t uid_;
174         pid_t pid_;
175         uint32_t token_;
176         AppId appId_;
177         KvStoreDataService &dataService_;
178         std::map<std::string, sptr<IRemoteObject>> observerProxy_;
179         sptr<KvStoreDeathRecipient> deathRecipient_;
180     };
181 
182     void Initialize();
183 
184     void LoadFeatures();
185 
186     void StartService();
187 
188     void InitSecurityAdapter(std::shared_ptr<ExecutorPool> executors);
189 
190     void OnStoreMetaChanged(const std::vector<uint8_t> &key, const std::vector<uint8_t> &value, CHANGE_FLAG flag);
191 
192     Status AppExit(pid_t uid, pid_t pid, uint32_t token, const AppId &appId);
193 
194     void LoadConfigs();
195 
196     void InitExecutor();
197 
198     std::vector<uint8_t> ReEncryptKey(const std::string &key, SecretKeyMetaData &secretKeyMeta,
199         const StoreMetaData &metaData, const std::vector<uint8_t> &iv);
200 
201     bool ParseSecretKeyFile(MessageParcel &data, SecretKeyBackupData &backupData);
202 
203     bool RestoreSecretKey(const SecretKeyBackupData::BackupItem &item, const std::string &userId,
204         const std::vector<uint8_t> &iv);
205     bool ImportCloneKey(const std::string &keyStr);
206     void DeleteCloneKey();
207 
208     std::string GetBackupReplyCode(int replyCode, const std::string &info = "");
209 
210     int32_t ReplyForRestore(MessageParcel &reply, int32_t result);
211 
212     static constexpr int TEN_SEC = 10;
213 
214     ConcurrentMap<uint32_t, std::map<int32_t, KvStoreClientDeathObserverImpl>> clients_;
215     std::shared_ptr<KvStoreAccountObserver> accountEventObserver_;
216     std::shared_ptr<KvStoreScreenObserver> screenEventObserver_;
217 
218     std::shared_ptr<Security> security_;
219     ConcurrentMap<std::string, sptr<DistributedData::FeatureStubImpl>> features_;
220     std::shared_ptr<KvStoreDeviceListener> deviceInnerListener_;
221     std::shared_ptr<ExecutorPool> executors_;
222     static constexpr int VERSION_WIDTH = 11;
223     static constexpr const char *INDENTATION = "    ";
224     static constexpr int32_t FORMAT_BLANK_SIZE = 32;
225     static constexpr char FORMAT_BLANK_SPACE = ' ';
226     static constexpr int32_t PRINTF_COUNT_2 = 2;
227     static constexpr int MAXIMUM_PARAMETER_LIMIT = 3;
228     static constexpr pid_t INVALID_UID = -1;
229     static constexpr pid_t INVALID_PID = -1;
230     static constexpr uint32_t INVALID_TOKEN = 0;
231 };
232 }
233 #endif  // KVSTORE_DATASERVICE_H