737a971f3978c36ad17cb68cb772c5a36afcd0dc
[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
22 static const char* INTERNET_PRIVILEGE = "http://tizen.org/privilege/internet";
23
24 static int checkInternetPrivilegeCb(const char *privilegeName, void *user_data)
25 {
26         bool* ret = (bool*)user_data;
27         if (!strcmp(privilegeName, INTERNET_PRIVILEGE)) {
28                 *ret = true;
29                 return -1;
30         }
31         return 0;
32 }
33
34 void checkInternetPrivilegeAndDisableIPv6(const char* pkgId, const std::string& rootPath)
35 {
36         bool isInternetPrivilegeExisted = false;
37         pkgmgrinfo_pkginfo_h pkg_handle;
38         int ret = pkgmgrGetPkgInfo(pkgId, &pkg_handle);
39         if (ret != 0) {
40                 return;
41         }
42
43         ret = pkgmgrinfo_pkginfo_foreach_privilege(pkg_handle, checkInternetPrivilegeCb, &isInternetPrivilegeExisted);
44         if (ret != PMINFO_R_OK) {
45                 _SERR("Failed to pkgmgrinfo_pkginfo_foreach_privilege");
46                 pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
47                 return;
48         }
49
50         if (!isInternetPrivilegeExisted) {
51                 std::string ipv6FilePath = rootPath + "/bin/" + DISABLE_IPV6_FILE;
52                 std::ofstream output(ipv6FilePath);
53                 if (exist(ipv6FilePath)) {
54                         copySmackAndOwnership(rootPath + "/bin/", ipv6FilePath);
55                         _INFO("File to disable IPv6 is created successfully");
56                 } else {
57                         _SERR("Failed to create file to disable IPv6 [%s]", pkgId);
58                 }
59                 output.close();
60         }
61
62         pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
63 }
64
65 static int checkAppPrivilegeListCb(pkgmgrinfo_appinfo_h handle, void *user_data)
66 {
67         char *pkgId = NULL;
68         int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
69         if (ret != PMINFO_R_OK || pkgId == NULL) {
70                 _SERR("Failed to get package id");
71                 return 0;
72         }
73
74         char *rootPath = NULL;
75         ret = pkgmgrinfo_appinfo_get_root_path(handle, &rootPath);
76         if (ret != PMINFO_R_OK) {
77                 _SERR("Failed to get root path");
78                 return 0;
79         }
80
81         checkInternetPrivilegeAndDisableIPv6(pkgId, rootPath);
82
83         return 0;
84 }
85
86 static void checkAppPrivilegeByAppType(const char* type)
87 {
88         pkgmgrinfo_appinfo_filter_h filter;
89
90         int ret = pkgmgrinfo_appinfo_filter_create(&filter);
91         if (ret != PMINFO_R_OK) {
92                 _SERR("Failed to create appinfo filter");
93                 return;
94         }
95
96         ret = pkgmgrinfo_appinfo_filter_add_string(filter, PMINFO_APPINFO_PROP_APP_TYPE, type);
97         if (ret != PMINFO_R_OK) {
98                 pkgmgrinfo_appinfo_filter_destroy(filter);
99                 _SERR("Failed to add appinfo filter (%s)", type);
100                 return;
101         }
102
103         ret = pkgmgrinfo_appinfo_filter_foreach_appinfo(filter, checkAppPrivilegeListCb, NULL);
104         if (ret != PMINFO_R_OK) {
105                 _SERR("Failed to pkgmgrinfo_appinfo_filter_foreach_appinfo");
106                 pkgmgrinfo_appinfo_filter_destroy(filter);
107                 return;
108         }
109
110         pkgmgrinfo_appinfo_filter_destroy(filter);
111
112         return;
113 }
114
115 void checkAllAppPrivilege()
116 {
117         std::vector<const char*> appTypeList = {"dotnet", "dotnet-nui", "dotnet-inhouse"};
118
119         for (auto& type : appTypeList) {
120                 checkAppPrivilegeByAppType(type);
121         }
122 }