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 "netstack_apipolicy_utils.h"
17
18 #include <dlfcn.h>
19 #include <mutex>
20
21 #include "netstack_log.h"
22
23 namespace OHOS::NetStack::ApiPolicyUtils {
24 namespace {
25 constexpr const uint32_t RESULT_ACCEPT = 0;
26 }
27
28 #ifdef __LP64__
29 const std::string APIPOLICY_SO_PATH = "/system/lib64/platformsdk/libapipolicy_client.z.so";
30 #else
31 const std::string APIPOLICY_SO_PATH = "/system/lib/platformsdk/libapipolicy_client.z.so";
32 #endif
33
IsAllowedHostname(const std::string & bundleName,const std::string & domainType,const std::string & hostname)34 bool IsAllowedHostname(const std::string &bundleName, const std::string &domainType, const std::string &hostname)
35 {
36 void *libHandle = dlopen(APIPOLICY_SO_PATH.c_str(), RTLD_NOW);
37 if (!libHandle) {
38 const char *err = dlerror();
39 NETSTACK_LOGE("apipolicy so dlopen failed: %{public}s", err ? err : "unknown");
40 return true;
41 }
42 using CheckUrlFunc = int32_t (*)(std::string, std::string, std::string);
43 auto func = reinterpret_cast<CheckUrlFunc>(dlsym(libHandle, "CheckUrl"));
44 if (func == nullptr) {
45 const char *err = dlerror();
46 NETSTACK_LOGE("apipolicy dlsym CheckUrl failed: %{public}s", err ? err : "unknown");
47 dlclose(libHandle);
48 return true;
49 }
50 int32_t res = func(bundleName, domainType, hostname);
51 NETSTACK_LOGD("ApiPolicy CheckHttpUrl result=%{public}d", res);
52 dlclose(libHandle);
53 return res == RESULT_ACCEPT;
54 }
55 };