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