1 /*
2 * Copyright (c) 2024 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 #include "deeplink_reserve/deeplink_reserve_config.h"
17
18 #include <fstream>
19 #include <unistd.h>
20 #include <regex>
21
22 #include "config_policy_utils.h"
23 #include "hilog_tag_wrapper.h"
24
25 namespace OHOS {
26 namespace AAFwk {
27 namespace {
28 const std::string CONFIG_PATH = "/etc/ability_runtime/deeplink_reserve_config.json";
29 const std::string DEFAULT_RESERVE_CONFIG_PATH = "/system/etc/deeplink_reserve_config.json";
30 const std::string DEEPLINK_RESERVED_URI_NAME = "deepLinkReservedUri";
31 const std::string BUNDLE_NAME = "bundleName";
32 const std::string URIS_NAME = "uris";
33 const std::string SCHEME_NAME = "scheme";
34 const std::string HOST_NAME = "host";
35 const std::string PORT_NAME = "port";
36 const std::string PATH_NAME = "path";
37 const std::string PATH_START_WITH_NAME = "pathStartWith";
38 const std::string PATH_REGEX_NAME = "pathRegex";
39 const std::string TYPE_NAME = "type";
40 const std::string UTD_NAME = "utd";
41 const std::string PORT_SEPARATOR = ":";
42 const std::string SCHEME_SEPARATOR = "://";
43 const std::string PATH_SEPARATOR = "/";
44 const std::string PARAM_SEPARATOR = "?";
45 }
46
GetConfigPath()47 std::string DeepLinkReserveConfig::GetConfigPath()
48 {
49 char buf[MAX_PATH_LEN] = { 0 };
50 char *configPath = GetOneCfgFile(CONFIG_PATH.c_str(), buf, MAX_PATH_LEN);
51 if (configPath == nullptr || configPath[0] == '\0' || strlen(configPath) > MAX_PATH_LEN) {
52 return DEFAULT_RESERVE_CONFIG_PATH;
53 }
54 return configPath;
55 }
56
LoadConfiguration()57 bool DeepLinkReserveConfig::LoadConfiguration()
58 {
59 TAG_LOGD(AAFwkTag::ABILITYMGR, "call");
60 std::string configPath = GetConfigPath();
61 TAG_LOGD(AAFwkTag::ABILITYMGR, "Deeplink reserve config path is: %{public}s", configPath.c_str());
62 nlohmann::json jsonBuf;
63 if (!ReadFileInfoJson(configPath, jsonBuf)) {
64 return false;
65 }
66 if (!LoadReservedUriList(jsonBuf)) {
67 TAG_LOGE(AAFwkTag::ABILITYMGR, "LoadConfiguration failed.");
68 return false;
69 }
70
71 return true;
72 }
73
isLinkReserved(const std::string & linkString,std::string & bundleName)74 bool DeepLinkReserveConfig::isLinkReserved(const std::string &linkString, std::string &bundleName)
75 {
76 TAG_LOGD(AAFwkTag::ABILITYMGR, "call");
77 for (auto it = deepLinkReserveUris_.begin(); it != deepLinkReserveUris_.end(); ++it) {
78 for (auto &itemUri : it->second) {
79 if (isUriMatched(itemUri, linkString)) {
80 TAG_LOGI(AAFwkTag::ABILITYMGR, "link is: %{public}s, linkReserved is: %{public}s, matched!",
81 linkString.c_str(), itemUri.scheme.c_str());
82 bundleName = it->first;
83 return true;
84 }
85 }
86 }
87
88 return false;
89 }
90
GetOptParamUri(const std::string & linkString)91 static std::string GetOptParamUri(const std::string &linkString)
92 {
93 std::size_t pos = linkString.rfind(PARAM_SEPARATOR);
94 if (pos == std::string::npos) {
95 return linkString;
96 }
97 return linkString.substr(0, pos);
98 }
99
StartsWith(const std::string & sourceString,const std::string & targetPrefix)100 static bool StartsWith(const std::string &sourceString, const std::string &targetPrefix)
101 {
102 return sourceString.rfind(targetPrefix, 0) == 0;
103 }
104
105
isUriMatched(const ReserveUri & reservedUri,const std::string & link)106 bool DeepLinkReserveConfig::isUriMatched(const ReserveUri &reservedUri, const std::string &link)
107 {
108 if (reservedUri.scheme.empty()) {
109 return false;
110 }
111 if (reservedUri.host.empty()) {
112 // config uri is : scheme
113 // belows are param uri matched conditions:
114 // 1.scheme
115 // 2.scheme:
116 // 3.scheme:/
117 // 4.scheme://
118 return link == reservedUri.scheme || StartsWith(link, reservedUri.scheme + PORT_SEPARATOR);
119 }
120 std::string optParamUri = GetOptParamUri(link);
121 std::string reservedUriString;
122 reservedUriString.append(reservedUri.scheme).append(SCHEME_SEPARATOR).append(reservedUri.host);
123 if (!reservedUri.port.empty()) {
124 reservedUriString.append(PORT_SEPARATOR).append(reservedUri.port);
125 }
126 if (reservedUri.path.empty() && reservedUri.pathStartWith.empty() && reservedUri.pathRegex.empty()) {
127 // with port, config uri is : scheme://host:port
128 // belows are param uri matched conditions:
129 // 1.scheme://host:port
130 // 2.scheme://host:port/path
131
132 // without port, config uri is : scheme://host
133 // belows are param uri matched conditions:
134 // 1.scheme://host
135 // 2.scheme://host/path
136 // 3.scheme://host:port scheme://host:port/path
137 bool ret = (optParamUri == reservedUriString || StartsWith(optParamUri, reservedUriString + PATH_SEPARATOR));
138 if (reservedUri.port.empty()) {
139 ret = ret || StartsWith(optParamUri, reservedUriString + PORT_SEPARATOR);
140 }
141 return ret;
142 }
143 reservedUriString.append(PATH_SEPARATOR);
144 // if one of path, pathStartWith, pathRegex match, then match
145 if (!reservedUri.path.empty()) {
146 // path match
147 std::string pathUri(reservedUriString);
148 pathUri.append(reservedUri.path);
149 if (optParamUri == pathUri) {
150 return true;
151 }
152 }
153 if (!reservedUri.pathStartWith.empty()) {
154 // pathStartWith match
155 std::string pathStartWithUri(reservedUriString);
156 pathStartWithUri.append(reservedUri.pathStartWith);
157 if (StartsWith(optParamUri, pathStartWithUri)) {
158 return true;
159 }
160 }
161 if (!reservedUri.pathRegex.empty()) {
162 // pathRegex match
163 std::string pathRegexUri(reservedUriString);
164 pathRegexUri.append(reservedUri.pathRegex);
165 try {
166 std::regex regex(pathRegexUri);
167 if (regex_match(optParamUri, regex)) {
168 return true;
169 }
170 } catch(...) {
171 TAG_LOGE(AAFwkTag::ABILITYMGR, "regex error");
172 }
173 }
174 return false;
175 }
176
LoadReservedUrilItem(const nlohmann::json & jsonUriObject,std::vector<ReserveUri> & uriList)177 void DeepLinkReserveConfig::LoadReservedUrilItem(const nlohmann::json &jsonUriObject, std::vector<ReserveUri> &uriList)
178 {
179 ReserveUri reserveUri;
180 if (jsonUriObject.contains(SCHEME_NAME) && jsonUriObject.at(SCHEME_NAME).is_string()) {
181 std::string schemeName = jsonUriObject.at(SCHEME_NAME).get<std::string>();
182 reserveUri.scheme = schemeName;
183 TAG_LOGD(AAFwkTag::ABILITYMGR, "scheme is: %{public}s", reserveUri.scheme.c_str());
184 }
185 if (jsonUriObject.contains(HOST_NAME) && jsonUriObject.at(HOST_NAME).is_string()) {
186 std::string hostName = jsonUriObject.at(HOST_NAME).get<std::string>();
187 reserveUri.host = hostName;
188 TAG_LOGD(AAFwkTag::ABILITYMGR, "host is: %{public}s", reserveUri.host.c_str());
189 }
190 if (jsonUriObject.contains(PORT_NAME) && jsonUriObject.at(PORT_NAME).is_string()) {
191 std::string portName = jsonUriObject.at(PORT_NAME).get<std::string>();
192 reserveUri.port = portName;
193 TAG_LOGD(AAFwkTag::ABILITYMGR, "port is: %{public}s", reserveUri.port.c_str());
194 }
195 if (jsonUriObject.contains(PATH_NAME) && jsonUriObject.at(PATH_NAME).is_string()) {
196 std::string pathName = jsonUriObject.at(PATH_NAME).get<std::string>();
197 reserveUri.path = PATH_NAME;
198 TAG_LOGD(AAFwkTag::ABILITYMGR, "path is: %{public}s", reserveUri.path.c_str());
199 }
200 if (jsonUriObject.contains(PATH_START_WITH_NAME) && jsonUriObject.at(PATH_START_WITH_NAME).is_string()) {
201 std::string pathStartWithName = jsonUriObject.at(PATH_START_WITH_NAME).get<std::string>();
202 reserveUri.pathStartWith = pathStartWithName;
203 TAG_LOGD(AAFwkTag::ABILITYMGR, "pathStartWith is: %{public}s", reserveUri.pathStartWith.c_str());
204 }
205 if (jsonUriObject.contains(PATH_REGEX_NAME) && jsonUriObject.at(PATH_REGEX_NAME).is_string()) {
206 std::string pathRegexName = jsonUriObject.at(PATH_REGEX_NAME).get<std::string>();
207 reserveUri.pathRegex = pathRegexName;
208 TAG_LOGD(AAFwkTag::ABILITYMGR, "pathRegex is: %{public}s", reserveUri.pathRegex.c_str());
209 }
210 if (jsonUriObject.contains(TYPE_NAME) && jsonUriObject.at(TYPE_NAME).is_string()) {
211 std::string typeName = jsonUriObject.at(TYPE_NAME).get<std::string>();
212 reserveUri.type = typeName;
213 TAG_LOGD(AAFwkTag::ABILITYMGR, "type is: %{public}s", reserveUri.type.c_str());
214 }
215 if (jsonUriObject.contains(UTD_NAME) && jsonUriObject.at(UTD_NAME).is_string()) {
216 std::string utdName = jsonUriObject.at(UTD_NAME).get<std::string>();
217 reserveUri.utd = utdName;
218 TAG_LOGD(AAFwkTag::ABILITYMGR, "utd is: %{public}s", reserveUri.utd.c_str());
219 }
220
221 uriList.emplace_back(reserveUri);
222 }
223
LoadReservedUriList(const nlohmann::json & object)224 bool DeepLinkReserveConfig::LoadReservedUriList(const nlohmann::json &object)
225 {
226 if (!object.contains(DEEPLINK_RESERVED_URI_NAME)) {
227 TAG_LOGE(AAFwkTag::ABILITYMGR, "Deeplink reserved uri config not existed.");
228 return false;
229 }
230
231 for (auto &item : object.at(DEEPLINK_RESERVED_URI_NAME).items()) {
232 const nlohmann::json& jsonObject = item.value();
233 if (!jsonObject.contains(BUNDLE_NAME) || !jsonObject.at(BUNDLE_NAME).is_string()) {
234 TAG_LOGE(AAFwkTag::ABILITYMGR, "Wrong deeplink reserve bundleName.");
235 return false;
236 }
237 if (!jsonObject.contains(URIS_NAME) || !jsonObject.at(URIS_NAME).is_array()) {
238 TAG_LOGE(AAFwkTag::ABILITYMGR, "Wrong deeplink reserve uris.");
239 return false;
240 }
241 std::string bundleName = jsonObject.at(BUNDLE_NAME).get<std::string>();
242 std::vector<ReserveUri> uriList;
243 for (auto &uriItem : jsonObject.at(URIS_NAME).items()) {
244 const nlohmann::json& jsonUriObject = uriItem.value();
245 LoadReservedUrilItem(jsonUriObject, uriList);
246 }
247 deepLinkReserveUris_.insert(std::make_pair(bundleName, uriList));
248 }
249 return true;
250 }
251
ReadFileInfoJson(const std::string & filePath,nlohmann::json & jsonBuf)252 bool DeepLinkReserveConfig::ReadFileInfoJson(const std::string &filePath, nlohmann::json &jsonBuf)
253 {
254 if (access(filePath.c_str(), F_OK) != 0) {
255 TAG_LOGE(AAFwkTag::ABILITYMGR, "Deeplink reserve config not exist.");
256 return false;
257 }
258
259 if (filePath.empty()) {
260 TAG_LOGE(AAFwkTag::ABILITYMGR, "File path is empty.");
261 return false;
262 }
263
264 char path[PATH_MAX] = {0};
265 if (realpath(filePath.c_str(), path) == nullptr) {
266 TAG_LOGE(AAFwkTag::ABILITYMGR, "realpath error, errno is %{public}d.", errno);
267 return false;
268 }
269
270 std::fstream in;
271 char errBuf[256];
272 errBuf[0] = '\0';
273 in.open(path, std::ios_base::in);
274 if (!in.is_open()) {
275 strerror_r(errno, errBuf, sizeof(errBuf));
276 TAG_LOGE(AAFwkTag::ABILITYMGR, "the file cannot be open due to %{public}s", errBuf);
277 return false;
278 }
279
280 in.seekg(0, std::ios::end);
281 int64_t size = in.tellg();
282 if (size <= 0) {
283 TAG_LOGE(AAFwkTag::ABILITYMGR, "the file is an empty file");
284 in.close();
285 return false;
286 }
287
288 in.seekg(0, std::ios::beg);
289 jsonBuf = nlohmann::json::parse(in, nullptr, false);
290 in.close();
291 if (jsonBuf.is_discarded()) {
292 TAG_LOGE(AAFwkTag::ABILITYMGR, "bad profile file");
293 return false;
294 }
295
296 return true;
297 }
298 }
299 }