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