1 /*
2 * Copyright (c) 2022 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 <cstdio>
17 #include <cstdlib>
18 #include <getopt.h>
19 #include <iosfwd>
20 #include <iostream>
21 #include <istream>
22 #include <ostream>
23 #include <sstream>
24 #include <streambuf>
25 #include <string>
26 #include <unistd.h>
27 #include <vector>
28
29 #include "hap_restorecon.h"
30 #include "selinux_error.h"
31
32 using namespace Selinux;
33
34 static const int ALARM_TIME_S = 5;
35 static const uint32_t TEST_UID = 20190166;
36 struct TestInput {
37 std::string name = "";
38 std::string apl = "";
39 std::vector<std::string> multiPath;
40 bool domain = false;
41 std::string recurse = "1";
42 bool isPreinstalledApp = false;
43 std::string extension = "";
44 uint32_t uid = 0;
45 };
46
PrintUsage()47 static void PrintUsage()
48 {
49 printf("Usage:\n");
50 printf("hap_restorecon -p /data/app/el1/100/base/com.ohos.test -n com.ohos.test -a normal -r 0\n");
51 printf("hap_restorecon -d -n com.ohos.test -a normal -i\n");
52 printf("\n");
53 printf("Options:\n");
54 printf(" -h (--help) show the help information. [eg: hap_restorecon -h]\n");
55 printf(" -p (--path) path to restorecon. [eg: -p "
56 "/data/app/el1/100/base/com.ohos.test]\n");
57 printf(" -r (--recurse) recurse? [eg: -r 0]\n");
58 printf(" -a (--apl) apl info. [eg: -a normal]\n");
59 printf(" -n (--name) package name. [eg: -n com.ohos.test]\n");
60 printf(" -d (--domain) setcon domain. [eg: -d]\n");
61 printf(" -m (--multipath) paths to restorecon. [eg: -m "
62 "/data/app/el1/100/base/com.ohos.test1 "
63 "/data/app/el1/100/base/com.ohos.test2]\n");
64 printf(" -i (--preinstalledapp) setcon preinstalled [eg: -i]\n");
65 printf(" -e (--extension) extension info. [eg: -e extension_info]\n");
66 printf("\n");
67 }
68
SetOptions(int argc,char * argv[],const option * options,TestInput & input)69 static void SetOptions(int argc, char *argv[], const option *options, TestInput &input)
70 {
71 int index = 0;
72 const char *optStr = "hda:p:n:r:m:ie:u";
73 int para = 0;
74 while ((para = getopt_long(argc, argv, optStr, options, &index)) != -1) {
75 switch (para) {
76 case 'h':
77 PrintUsage();
78 exit(0);
79 case 'a': input.apl = optarg; break;
80 case 'd': input.domain = true; break;
81 case 'p': input.multiPath.emplace_back(optarg); break;
82 case 'm': {
83 std::stringstream str(optarg);
84 std::string tmp;
85 while (str >> tmp) {
86 input.multiPath.emplace_back(tmp);
87 }
88 break;
89 }
90 case 'n': input.name = optarg; break;
91 case 'r': input.recurse = optarg; break;
92 case 'i': input.isPreinstalledApp = true; break;
93 case 'e': input.extension = optarg; break;
94 case 'u': input.uid = TEST_UID; break;
95 default:
96 printf("Try 'hap_restorecon -h' for more information.\n");
97 exit(-1);
98 }
99 }
100 }
101
main(int argc,char * argv[])102 int main(int argc, char *argv[])
103 {
104 struct option options[] = {
105 {"help", no_argument, nullptr, 'h'}, {"apl", required_argument, nullptr, 'a'},
106 {"name", required_argument, nullptr, 'n'}, {"domain", no_argument, nullptr, 'd'},
107 {"path", required_argument, nullptr, 'p'}, {"mutilpath", required_argument, nullptr, 'm'},
108 {"recurse", required_argument, nullptr, 'r'}, {"preinstalledapp", no_argument, nullptr, 'i'},
109 {"extension", required_argument, nullptr, 'e'},
110 {nullptr, no_argument, nullptr, 0},
111 };
112
113 if (argc == 1) {
114 PrintUsage();
115 exit(0);
116 }
117
118 TestInput testCmd;
119 SetOptions(argc, argv, options, testCmd);
120 HapContext test;
121 int res = 0;
122 if (!testCmd.domain) {
123 HapFileInfo hapFileInfo = {
124 .pathNameOrig = testCmd.multiPath,
125 .apl = testCmd.apl,
126 .packageName = testCmd.name,
127 .flags = atoi(testCmd.recurse.c_str()),
128 .hapFlags = testCmd.isPreinstalledApp ? 1 : 0,
129 .uid = testCmd.uid
130 };
131 res = test.HapFileRestorecon(hapFileInfo);
132 std::cout << GetErrStr(res) << std::endl;
133 } else {
134 HapDomainInfo hapDomainInfo {
135 .apl = testCmd.apl,
136 .packageName = testCmd.name,
137 .extensionType = testCmd.extension,
138 .hapFlags = testCmd.isPreinstalledApp ? 1 : 0,
139 .uid = testCmd.uid
140 };
141 res = test.HapDomainSetcontext(hapDomainInfo);
142 std::cout << GetErrStr(res) << std::endl;
143 sleep(ALARM_TIME_S);
144 }
145 exit(0);
146 }
147