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, "load fail");
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:%{public}s, linkReserved:%{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:%{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 }
189 if (jsonUriObject.contains(PORT_NAME) && jsonUriObject.at(PORT_NAME).is_string()) {
190 std::string portName = jsonUriObject.at(PORT_NAME).get<std::string>();
191 reserveUri.port = portName;
192 TAG_LOGD(AAFwkTag::ABILITYMGR, "port:%{public}s", reserveUri.port.c_str());
193 }
194 if (jsonUriObject.contains(PATH_NAME) && jsonUriObject.at(PATH_NAME).is_string()) {
195 std::string pathName = jsonUriObject.at(PATH_NAME).get<std::string>();
196 reserveUri.path = PATH_NAME;
197 TAG_LOGD(AAFwkTag::ABILITYMGR, "path:%{public}s", reserveUri.path.c_str());
198 }
199 if (jsonUriObject.contains(PATH_START_WITH_NAME) && jsonUriObject.at(PATH_START_WITH_NAME).is_string()) {
200 std::string pathStartWithName = jsonUriObject.at(PATH_START_WITH_NAME).get<std::string>();
201 reserveUri.pathStartWith = pathStartWithName;
202 TAG_LOGD(AAFwkTag::ABILITYMGR, "pathStartWith:%{public}s", reserveUri.pathStartWith.c_str());
203 }
204 if (jsonUriObject.contains(PATH_REGEX_NAME) && jsonUriObject.at(PATH_REGEX_NAME).is_string()) {
205 std::string pathRegexName = jsonUriObject.at(PATH_REGEX_NAME).get<std::string>();
206 reserveUri.pathRegex = pathRegexName;
207 TAG_LOGD(AAFwkTag::ABILITYMGR, "pathRegex:%{public}s", reserveUri.pathRegex.c_str());
208 }
209 if (jsonUriObject.contains(TYPE_NAME) && jsonUriObject.at(TYPE_NAME).is_string()) {
210 std::string typeName = jsonUriObject.at(TYPE_NAME).get<std::string>();
211 reserveUri.type = typeName;
212 TAG_LOGD(AAFwkTag::ABILITYMGR, "type:%{public}s", reserveUri.type.c_str());
213 }
214 if (jsonUriObject.contains(UTD_NAME) && jsonUriObject.at(UTD_NAME).is_string()) {
215 std::string utdName = jsonUriObject.at(UTD_NAME).get<std::string>();
216 reserveUri.utd = utdName;
217 TAG_LOGD(AAFwkTag::ABILITYMGR, "utd:%{public}s", reserveUri.utd.c_str());
218 }
219
220 uriList.emplace_back(reserveUri);
221 }
222
LoadReservedUriList(const nlohmann::json & object)223 bool DeepLinkReserveConfig::LoadReservedUriList(const nlohmann::json &object)
224 {
225 if (!object.contains(DEEPLINK_RESERVED_URI_NAME) || !object.at(DEEPLINK_RESERVED_URI_NAME).is_array()) {
226 TAG_LOGE(AAFwkTag::ABILITYMGR, "uri config absent");
227 return false;
228 }
229
230 for (auto &item : object.at(DEEPLINK_RESERVED_URI_NAME).items()) {
231 const nlohmann::json& jsonObject = item.value();
232 if (!jsonObject.contains(BUNDLE_NAME) || !jsonObject.at(BUNDLE_NAME).is_string()) {
233 TAG_LOGE(AAFwkTag::ABILITYMGR, "reserve bundleName fail");
234 return false;
235 }
236 if (!jsonObject.contains(URIS_NAME) || !jsonObject.at(URIS_NAME).is_array()) {
237 TAG_LOGE(AAFwkTag::ABILITYMGR, "reserve uris fail");
238 return false;
239 }
240 std::string bundleName = jsonObject.at(BUNDLE_NAME).get<std::string>();
241 std::vector<ReserveUri> uriList;
242 for (auto &uriItem : jsonObject.at(URIS_NAME).items()) {
243 const nlohmann::json& jsonUriObject = uriItem.value();
244 LoadReservedUrilItem(jsonUriObject, uriList);
245 }
246 deepLinkReserveUris_.insert(std::make_pair(bundleName, uriList));
247 }
248 return true;
249 }
250
ReadFileInfoJson(const std::string & filePath,nlohmann::json & jsonBuf)251 bool DeepLinkReserveConfig::ReadFileInfoJson(const std::string &filePath, nlohmann::json &jsonBuf)
252 {
253 if (access(filePath.c_str(), F_OK) != 0) {
254 TAG_LOGE(AAFwkTag::ABILITYMGR, "reserve config absent");
255 return false;
256 }
257
258 if (filePath.empty()) {
259 TAG_LOGE(AAFwkTag::ABILITYMGR, "file path empty");
260 return false;
261 }
262
263 char path[PATH_MAX] = {0};
264 if (realpath(filePath.c_str(), path) == nullptr) {
265 TAG_LOGE(AAFwkTag::ABILITYMGR, "realpath error:%{public}d", errno);
266 return false;
267 }
268
269 std::fstream in;
270 char errBuf[256];
271 errBuf[0] = '\0';
272 in.open(path, std::ios_base::in);
273 if (!in.is_open()) {
274 strerror_r(errno, errBuf, sizeof(errBuf));
275 TAG_LOGE(AAFwkTag::ABILITYMGR, "open error:%{public}s", errBuf);
276 return false;
277 }
278
279 in.seekg(0, std::ios::end);
280 int64_t size = in.tellg();
281 if (size <= 0) {
282 TAG_LOGE(AAFwkTag::ABILITYMGR, "file empty");
283 in.close();
284 return false;
285 }
286
287 in.seekg(0, std::ios::beg);
288 jsonBuf = nlohmann::json::parse(in, nullptr, false);
289 in.close();
290 if (jsonBuf.is_discarded()) {
291 TAG_LOGE(AAFwkTag::ABILITYMGR, "bad profile file");
292 return false;
293 }
294
295 return true;
296 }
297 }
298 }