• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cstdlib>
17 #include <getopt.h>
18 #include <iosfwd>
19 #include <iostream>
20 #include <istream>
21 #include <memory>
22 #include <ostream>
23 #include <string>
24 #include <unistd.h>
25 
26 #include "hdf_service_checker.h"
27 #include "selinux_error.h"
28 #include "service_checker.h"
29 
30 using namespace Selinux;
31 
32 static std::unique_ptr<ServiceChecker> g_service = nullptr;
33 
34 struct testInput {
35     char cmd = '\0';
36     bool isHdf = false;
37     std::string serviceName;
38 };
39 
PrintUsage()40 static void PrintUsage()
41 {
42     std::cout << "Options:" << std::endl;
43     std::cout << " -h (--help)           show the help information.        [eg: service_check -h]" << std::endl;
44     std::cout << "***********************optinal*************************************************" << std::endl;
45     std::cout << " -d (--isHdf)          service or hdf_service.           [eg: service_check -d]" << std::endl;
46     std::cout << " -n (--serviceName)    serviceName.                      [eg: service_check -n service_name]"
47               << std::endl;
48     std::cout << "***********************requered: 1 in 4****************************************" << std::endl;
49     std::cout << " -a (--add)            add service check.                [eg: service_check -a]" << std::endl;
50     std::cout << " -g (--get)            get service check.                [eg: service_check -g]" << std::endl;
51     std::cout << " -r (--get_remote)     get remote service check.         [eg: service_check -r]" << std::endl;
52     std::cout << " -l (--list)           list service check.               [eg: service_check -l]" << std::endl;
53     std::cout << "" << std::endl;
54     std::cout << "Usage:" << std::endl;
55     std::cout << ">>>>>>> choice 1: continuous input parameters" << std::endl;
56     std::cout << "step 1:" << std::endl;
57     std::cout << "service_check (-d) -a|-g|-r|-l" << std::endl;
58     std::cout << "step 2:" << std::endl;
59     std::cout << "input service name and press 'enter' to continue, or ctrl+C to end process" << std::endl;
60     std::cout << ">>>>>>> choice 2: single input parameter" << std::endl;
61     std::cout << "service_check (-d) -a|-g|-r|-l -n service_name" << std::endl;
62     std::cout << "" << std::endl;
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 = "dhlagrn:";
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 'n': {
77                 input.serviceName = optarg;
78                 break;
79             }
80             case 'd': {
81                 input.isHdf = true;
82                 break;
83             }
84             case 'a': {
85                 input.cmd = 'a';
86                 break;
87             }
88             case 'g': {
89                 input.cmd = 'g';
90                 break;
91             }
92             case 'r': {
93                 input.cmd = 'r';
94                 break;
95             }
96             case 'l': {
97                 input.cmd = 'l';
98                 break;
99             }
100             default:
101                 std::cout << "Try 'service_check -h' for more information." << std::endl;
102                 exit(-1);
103         }
104     }
105 }
106 
TestAddService(bool isHdf,const std::string & serviceName)107 static void TestAddService(bool isHdf, const std::string &serviceName)
108 {
109     if (!serviceName.empty()) {
110         std::cout << GetErrStr(isHdf ? HdfAddServiceCheck(getpid(), serviceName.c_str())
111                                      : g_service->AddServiceCheck(getpid(), serviceName))
112                   << std::endl;
113         exit(0);
114     }
115     std::string serName;
116     while (std::cin >> serName) {
117         std::cout << GetErrStr(isHdf ? HdfAddServiceCheck(getpid(), serName.c_str())
118                                      : g_service->AddServiceCheck(getpid(), serName))
119                   << std::endl;
120     }
121 }
122 
TestGetService(bool isHdf,const std::string & serviceName)123 static void TestGetService(bool isHdf, const std::string &serviceName)
124 {
125     if (!serviceName.empty()) {
126         std::cout << GetErrStr(isHdf ? HdfGetServiceCheck(getpid(), serviceName.c_str())
127                                      : g_service->GetServiceCheck(getpid(), serviceName))
128                   << std::endl;
129         exit(0);
130     }
131     std::string serName;
132     while (std::cin >> serName) {
133         std::cout << GetErrStr(isHdf ? HdfGetServiceCheck(getpid(), serName.c_str())
134                                      : g_service->GetServiceCheck(getpid(), serName))
135                   << std::endl;
136     }
137 }
138 
TestGetRemoteService(bool isHdf,const std::string & serviceName)139 static void TestGetRemoteService(bool isHdf, const std::string &serviceName)
140 {
141     if (!serviceName.empty()) {
142         std::cout << GetErrStr(isHdf ? SELINUX_PERMISSION_DENY
143                                      : g_service->GetRemoteServiceCheck(getpid(), serviceName))
144                   << std::endl;
145         exit(0);
146     }
147     std::string serName;
148     while (std::cin >> serName) {
149         std::cout << GetErrStr(isHdf ? SELINUX_PERMISSION_DENY : g_service->GetRemoteServiceCheck(getpid(), serName))
150                   << std::endl;
151     }
152 }
153 
TestListService(bool isHdf)154 static void TestListService(bool isHdf)
155 {
156     std::cout << GetErrStr(isHdf ? HdfListServiceCheck(getpid()) : g_service->ListServiceCheck(getpid())) << std::endl;
157 }
158 
Test(const testInput & testCmd)159 static void Test(const testInput &testCmd)
160 {
161     switch (testCmd.cmd) {
162         case 'a': {
163             TestAddService(testCmd.isHdf, testCmd.serviceName);
164             exit(0);
165         }
166         case 'g': {
167             TestGetService(testCmd.isHdf, testCmd.serviceName);
168             exit(0);
169         }
170         case 'r': {
171             TestGetRemoteService(testCmd.isHdf, testCmd.serviceName);
172             exit(0);
173         }
174         case 'l': {
175             TestListService(testCmd.isHdf);
176             exit(0);
177         }
178         default:
179             exit(-1);
180     }
181 }
182 
main(int argc,char * argv[])183 int main(int argc, char *argv[])
184 {
185     struct option options[] = {
186         {"help", no_argument, nullptr, 'h'},  {"add", no_argument, nullptr, 'a'},
187         {"get", no_argument, nullptr, 'g'},   {"get_remote", no_argument, nullptr, 'r'},
188         {"isHdf", no_argument, nullptr, 'd'}, {"list", no_argument, nullptr, 'l'},
189         {"serviceName", required_argument, nullptr, 'n'}, {nullptr, no_argument, nullptr, 0},
190     };
191 
192     if (argc == 1) {
193         PrintUsage();
194         exit(0);
195     }
196 
197     testInput input;
198     SetOptions(argc, argv, options, input);
199     if (!input.isHdf) {
200         g_service = std::make_unique<ServiceChecker>(false);
201     }
202     Test(input);
203 
204     exit(0);
205 }
206