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