Bug-fix: fix target dll searching logic
[platform/core/dotnet/launcher.git] / NativeLauncher / tool / privilege_common.cc
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "log.h"
18 #include "utils.h"
19 #include "privilege_common.h"
20 #include "launcher_env.h"
21 #include <fstream>
22
23 static const char* INTERNET_PRIVILEGE = "http://tizen.org/privilege/internet";
24
25 static int checkInternetPrivilegeCb(const char *privilegeName, void *user_data)
26 {
27         bool* ret = (bool*)user_data;
28         if (!strcmp(privilegeName, INTERNET_PRIVILEGE)) {
29                 *ret = true;
30                 return -1;
31         }
32         return 0;
33 }
34
35 void checkInternetPrivilegeAndDisableIPv6(const char* pkgId, const std::string& rootPath)
36 {
37         bool isInternetPrivilegeExisted = false;
38         pkgmgrinfo_pkginfo_h pkg_handle;
39         int ret = pkgmgrGetPkgInfo(pkgId, &pkg_handle);
40         if (ret != 0) {
41                 return;
42         }
43
44         ret = pkgmgrinfo_pkginfo_foreach_privilege(pkg_handle, checkInternetPrivilegeCb, &isInternetPrivilegeExisted);
45         if (ret != PMINFO_R_OK) {
46                 _SERR("Failed to pkgmgrinfo_pkginfo_foreach_privilege");
47                 pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
48                 return;
49         }
50
51         if (!isInternetPrivilegeExisted) {
52                 std::string ipv6FilePath = rootPath + "/bin/" + DISABLE_IPV6_FILE;
53                 std::ofstream output(ipv6FilePath);
54                 if (exist(ipv6FilePath)) {
55                         copySmackAndOwnership(rootPath + "/bin/", ipv6FilePath);
56                         _INFO("File to disable IPv6 is created successfully");
57                 } else {
58                         _SERR("Failed to create file to disable IPv6 [%s]", pkgId);
59                 }
60                 output.close();
61         }
62
63         pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
64 }
65
66 static int checkAppPrivilegeListCb(pkgmgrinfo_appinfo_h handle, void *user_data)
67 {
68         int ret = 0;
69         char* appType = NULL;
70
71         ret = pkgmgrinfo_appinfo_get_apptype(handle, &appType);
72         if (ret != PMINFO_R_OK) {
73                 _SERR("Failed to get apptype");
74                 return 0;
75         }
76
77         // skip if appType is NULL or appType does not contains "dotnet",
78         if (appType == NULL || (appType != NULL && strstr(appType, "dotnet") == NULL)) {
79                 return 0;
80         }
81
82         char *pkgId = NULL;
83         ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
84         if (ret != PMINFO_R_OK || pkgId == NULL) {
85                 _SERR("Failed to get package id");
86                 return 0;
87         }
88
89         char *rootPath = NULL;
90         ret = pkgmgrinfo_appinfo_get_root_path(handle, &rootPath);
91         if (ret != PMINFO_R_OK) {
92                 _SERR("Failed to get root path");
93                 return 0;
94         }
95
96         checkInternetPrivilegeAndDisableIPv6(pkgId, rootPath);
97
98         return 0;
99 }
100
101 void checkAllAppPrivilege()
102 {
103         int ret = pkgmgrinfo_appinfo_get_installed_list(checkAppPrivilegeListCb, NULL);
104         if (ret != PMINFO_R_OK) {
105                 _SERR("Failed to get installed list");
106         }
107 }