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 };
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 -i\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 domain.                          [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(" -i (--preinstalledapp)     setcon preinstalled                     [eg: -i]\n");
62     printf("\n");
63 }
64 
SetOptions(int argc,char * argv[],const option * options,TestInput & input)65 static void SetOptions(int argc, char *argv[], const option *options, TestInput &input)
66 {
67     int index = 0;
68     const char *optStr = "hda:p:n:r:m:i";
69     int para = 0;
70     while ((para = getopt_long(argc, argv, optStr, options, &index)) != -1) {
71         switch (para) {
72             case 'h': {
73                 PrintUsage();
74                 exit(0);
75             }
76             case 'a': {
77                 input.apl = optarg;
78                 break;
79             }
80             case 'd': {
81                 input.domain = true;
82                 break;
83             }
84             case 'p': {
85                 input.multiPath.emplace_back(optarg);
86                 break;
87             }
88             case 'm': {
89                 std::stringstream str(optarg);
90                 std::string tmp;
91                 while (str >> tmp) {
92                     input.multiPath.emplace_back(tmp);
93                 }
94                 break;
95             }
96             case 'n': {
97                 input.name = optarg;
98                 break;
99             }
100             case 'r': {
101                 input.recurse = optarg;
102                 break;
103             }
104             case 'i': {
105                 input.isPreinstalledApp = true;
106                 break;
107             }
108             default:
109                 printf("Try 'hap_restorecon -h' for more information.\n");
110                 exit(-1);
111         }
112     }
113 }
114 
main(int argc,char * argv[])115 int main(int argc, char *argv[])
116 {
117     struct option options[] = {
118         {"help", no_argument, nullptr, 'h'},          {"apl", required_argument, nullptr, 'a'},
119         {"name", required_argument, nullptr, 'n'},    {"domain", no_argument, nullptr, 'd'},
120         {"path", required_argument, nullptr, 'p'},    {"mutilpath", required_argument, nullptr, 'm'},
121         {"recurse", required_argument, nullptr, 'r'}, {"preinstalledapp", no_argument, nullptr, 'i'},
122         {nullptr, no_argument, nullptr, 0},
123     };
124 
125     if (argc == 1) {
126         PrintUsage();
127         exit(0);
128     }
129 
130     TestInput testCmd;
131     SetOptions(argc, argv, options, testCmd);
132     HapContext test;
133     int res = 0;
134     if (!testCmd.domain) {
135         HapFileInfo hapFileInfo = {
136             .pathNameOrig = testCmd.multiPath,
137             .apl = testCmd.apl,
138             .packageName = testCmd.name,
139             .flags = atoi(testCmd.recurse.c_str()),
140             .hapFlags = testCmd.isPreinstalledApp ? 1 : 0
141         };
142         res = test.HapFileRestorecon(hapFileInfo);
143         std::cout << GetErrStr(res) << std::endl;
144     } else {
145         HapDomainInfo hapDomainInfo {
146             .apl = testCmd.apl,
147             .packageName = testCmd.name,
148             .hapFlags = testCmd.isPreinstalledApp ? 1 : 0
149         };
150         res = test.HapDomainSetcontext(hapDomainInfo);
151         std::cout << GetErrStr(res) << std::endl;
152         sleep(ALARM_TIME_S);
153     }
154     exit(0);
155 }
156