8b7ba2dba1a29ebfcd32b754f08cfaf85ea8f466
[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 #include <app-runtime.h>
23
24 static const char* INTERNET_PRIVILEGE = "http://tizen.org/privilege/internet";
25 static int UID_OWNER = 5001;
26
27 void checkInternetPrivilegeAndDisableIPv6(const char* pkgId, const std::string& rootPath)
28 {
29         int res = 0;
30         if (security_manager_app_has_privilege(pkgId, INTERNET_PRIVILEGE, UID_OWNER, &res) == SECURITY_MANAGER_SUCCESS) {
31                 if (res != 1) {
32                         std::string filePath = rootPath + "/bin/" + DISABLE_IPV6_FILE;
33                         std::ofstream output(filePath);
34                         if (exist(filePath)) {
35                                 _INFO("File to disable IPv6 is created successfully");
36                         } else {
37                                 _ERR("Failed to create file to disable IPv6 [%s]", pkgId);
38                         }
39                         output.close();
40                 }
41         }
42 }
43
44 static int checkAppPrivilegeListCb(pkgmgrinfo_appinfo_h handle, void *user_data)
45 {
46         char *pkgId = NULL;
47         int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
48         if (ret != PMINFO_R_OK || pkgId == NULL) {
49                 _SERR("Failed to get package id");
50                 return 0;
51         }
52
53         char *rootPath = NULL;
54         ret = pkgmgrinfo_appinfo_get_root_path(handle, &rootPath);
55         if (ret != PMINFO_R_OK) {
56                 _SERR("Failed to get root path");
57                 return 0;
58         }
59
60         checkInternetPrivilegeAndDisableIPv6(pkgId, rootPath);
61
62         return 0;
63 }
64
65 static void checkAppPrivilegeByAppType(const char* type)
66 {
67         pkgmgrinfo_appinfo_filter_h filter;
68
69         int ret = pkgmgrinfo_appinfo_filter_create(&filter);
70         if (ret != PMINFO_R_OK) {
71                 _SERR("Failed to create appinfo filter");
72                 return;
73         }
74
75         ret = pkgmgrinfo_appinfo_filter_add_string(filter, PMINFO_APPINFO_PROP_APP_TYPE, type);
76         if (ret != PMINFO_R_OK) {
77                 pkgmgrinfo_appinfo_filter_destroy(filter);
78                 _SERR("Failed to add appinfo filter (%s)", type);
79                 return;
80         }
81
82         ret = pkgmgrinfo_appinfo_filter_foreach_appinfo(filter, checkAppPrivilegeListCb, NULL);
83         if (ret != PMINFO_R_OK) {
84                 _SERR("Failed to pkgmgrinfo_pkginfo_filter_foreach_pkginfo");
85                 pkgmgrinfo_appinfo_filter_destroy(filter);
86                 return;
87         }
88
89         pkgmgrinfo_appinfo_filter_destroy(filter);
90
91         return;
92 }
93
94 void checkAllAppPrivilege()
95 {
96         std::vector<const char*> appTypeList = {"dotnet", "dotnet-nui", "dotnet-inhouse"};
97
98         for (auto& type : appTypeList) {
99                 checkAppPrivilegeByAppType(type);
100         }
101 }