1 /*
2 * Copyright 2023 Unionman Technology 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 #include <iostream>
16 #include <fstream>
17 #include <ctime> // 包含时间和日期头文件
18 #include <sstream>
19 #include <string>
20 #include <filesystem>
21 #include <cstdio>
22 #include <cstdlib>
23 #include <securec.h>
24 #include "iremote_broker.h"
25 #include "iremote_stub.h"
26 #include "iremote_proxy.h"
27 #include "iremote_object.h"
28 #include "if_system_ability_manager.h"
29 #include "iservice_registry.h"
30 #include "system_ability_definition.h"
31 #include "nativetoken_kit.h"
32 #include "token_setproc.h"
33 #include "ipc_skeleton.h"
34 #include "bundle_mgr_client.h"
35 #include "cJSON.h"
36 #include "hilog/log.h"
37
38 #define ZLOGE(LOG_LABEL, fmt, args...) \
39 (void)OHOS::HiviewDFX::HiLog::Error(LOG_LABEL, "%{public}d: " fmt, __LINE__, ##args)
40 #define ZLOGW(LOG_LABEL, fmt, args...) \
41 (void)OHOS::HiviewDFX::HiLog::Warn(LOG_LABEL, "%{public}d: " fmt, __LINE__, ##args)
42 #define ZLOGI(LOG_LABEL, fmt, args...) \
43 (void)OHOS::HiviewDFX::HiLog::Info(LOG_LABEL, "%{public}d: " fmt, __LINE__, ##args)
44 #define ZLOGD(LOG_LABEL, fmt, args...) \
45 (void)OHOS::HiviewDFX::HiLog::Debug(LOG_LABEL, "%{public}d: " fmt, __LINE__, ##args)
46
47 static bool flag = true;
48 using namespace OHOS;
49 // 定义消息码
50 static HiviewDFX::HiLogLabel LABEL = {LOG_APP, 0x0003, "ShellServer"};
51 const int ABILITY_SHELL = 4;
52 const int ABILITY_YLOLO = 5;
53 const int ABILITY_LENET = 6;
54
55 class IShellAbility : public IRemoteBroker {
56 public:
57 DECLARE_INTERFACE_DESCRIPTOR(u"shell.Ability");
58 virtual std::string executeCommand(const std::string &dummy) = 0; // 定义业务函数
59 virtual std::string yolo5s(const std::string &dummy) = 0;
60 virtual std::string lenet(const std::string &dummy) = 0;
61 };
62
63 class ShellAbilityStub : public IRemoteStub<IShellAbility> {
64 public:
65 virtual int OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
66 MessageOption &option) override;
67 };
68
69 class ShellAbility : public ShellAbilityStub {
70 public:
71 std::string executeCommand(const std::string &dummy) override;
72 std::string yolo5s(const std::string &dummy) override;
73 std::string lenet(const std::string &dummy) override;
74 };
75
76 // 读取文件内容到缓冲区
readFile(const char * path,size_t * fileSize)77 static char* readFile(const char *path, size_t *fileSize)
78 {
79 FILE *file = fopen(path, "r");
80 if (!file) {
81 return nullptr;
82 }
83
84 if (fseek(file, 0, SEEK_END) != 0) {
85 fclose(file);
86 return -1;
87 }
88 *fileSize = ftell(file);
89 if (fseek(file, 0, SEEK_SET) != 0) {
90 fclose(file);
91 return -1;
92 }
93
94 char *fileContent = (char *)malloc(*fileSize + 1);
95 if (!fileContent) {
96 fclose(file);
97 return nullptr;
98 }
99
100 if (fread(fileContent, 1, *fileSize, file) != *fileSize) {
101 fclose(file);
102 free(fileContent);
103 return nullptr;
104 }
105
106 fileContent[*fileSize] = '\0';
107 if (fclose(file) != 0) {
108 return -1;
109 }
110 return fileContent;
111 }
112
113 // 处理 JSON 数据
processJSONData(const char * jsonContent,const char * uName)114 static int processJSONData(const char *jsonContent, const char *uName)
115 {
116 cJSON *json = cJSON_Parse(jsonContent);
117 if (!json) {
118 return -1;
119 }
120
121 cJSON *privilegeBundleList = cJSON_GetObjectItem(json, "Privilege Bundlname List");
122 if (privilegeBundleList && cJSON_IsArray(privilegeBundleList)) {
123 for (int i = 0; i < cJSON_GetArraySize(privilegeBundleList); i++) {
124 cJSON *element = cJSON_GetArrayItem(privilegeBundleList, i);
125 if (element && cJSON_IsString(element) && strcmp(element->valuestring, uName) == 0) {
126 cJSON_Delete(json);
127 return 0;
128 }
129 }
130 }
131 cJSON_Delete(json);
132 return -1;
133 }
134
135 // 组合两个函数
processJSONFile(const char * path,const char * uName)136 static int processJSONFile(const char *path, const char *uName)
137 {
138 size_t fileSize;
139 char *fileContent = readFile(path, &fileSize);
140 if (!fileContent) {
141 return -1;
142 }
143
144 int result = processJSONData(fileContent, uName);
145
146 free(fileContent);
147 return result;
148 }
149
bmgrGetUersNameForUidAndVerify()150 static int bmgrGetUersNameForUidAndVerify()
151 {
152 std::vector<std::string> fileLines;
153 std::string uersName = " ";
154 int uid = 1;
155 uid = IPCSkeleton::GetCallingUid();
156 std::unique_ptr<AppExecFwk::BundleMgrClient> ShellClient;
157 ShellClient = std::make_unique<AppExecFwk::BundleMgrClient>();
158 ShellClient->GetBundleNameForUid(uid, uersName);
159 ZLOGW(LABEL, "%{public}s: get calling BundleName:%{public}s", __func__, uersName.c_str());
160 int result = processJSONFile("/etc/Applist.json", uersName.c_str());
161 if (result == 0) {
162 return 0;
163 }
164 return -1;
165 }
166
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)167 int ShellAbilityStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
168 {
169 switch (code) {
170 case ABILITY_SHELL: {
171 int ver = bmgrGetUersNameForUidAndVerify();
172 if (ver == 0) {
173 std::string dummy = data.ReadString();
174 std::string result = executeCommand(dummy);
175 reply.WriteString(result);
176 return 0;
177 } else {
178 return -1;
179 }
180 }
181 case ABILITY_YLOLO: {
182 int ver = bmgrGetUersNameForUidAndVerify();
183 if (ver == 0) {
184 std::string dummy = data.ReadString();
185 std::string result = yolo5s(dummy);
186 reply.WriteString(result);
187 return 0;
188 } else {
189 return -1;
190 }
191 }
192 case ABILITY_LENET: {
193 int ver = bmgrGetUersNameForUidAndVerify();
194 if (ver == 0) {
195 std::string dummy = data.ReadString();
196 std::string result = lenet(dummy);
197 reply.WriteString(result);
198 return 0;
199 } else {
200 return -1;
201 }
202 }
203 default:
204 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
205 }
206 }
207
executeCommand(const std::string & dummy)208 std::string ShellAbility::executeCommand(const std::string &dummy)
209 {
210 ZLOGW(LABEL, "ipc_shell_server working");
211 std::string cmd = dummy + " 2>&1";
212 char buffer[128];
213 std::string result = "successful:";
214 FILE *pipe = popen(cmd.c_str(), "r");
215
216 if (!pipe) {
217 ZLOGW(LABEL, "%{public}s: popen error", __func__);
218 std::cerr << "命令执行失败" << std::endl;
219 return "erro";
220 }
221
222 while (fgets(buffer, sizeof(buffer), pipe) != nullptr) {
223 result += buffer;
224 }
225 pclose(pipe);
226 ZLOGW(LABEL, "%{public}s: popen successful", __func__);
227 return result;
228 }
229
yolo5s(const std::string & dummy)230 std::string ShellAbility::yolo5s(const std::string &dummy)
231 {
232 std::string cmd = "ld-linux-aarch64.so.1 /bin/sdk19_64 etc/yolov5s.nb "+ dummy + " 2>&1";
233 char buffer[128];
234 std::string result = "successful:";
235 FILE *pipe = popen(cmd.c_str(), "r");
236
237 if (!pipe) {
238 ZLOGW(LABEL, "%{public}s: popen error", __func__);
239 std::cerr << "命令执行失败" << std::endl;
240 return "erro";
241 }
242
243 while (fgets(buffer, sizeof(buffer), pipe) != nullptr) {
244 result += buffer;
245 }
246 pclose(pipe);
247 return result;
248 }
249
AddPermission()250 static void AddPermission()
251 {
252 if (flag) {
253 uint64_t tokenId;
254 NativeTokenInfoParams infoInstance = {
255 .dcapsNum = 0,
256 .permsNum = 0,
257 .aclsNum = 0,
258 .dcaps = NULL,
259 .perms = NULL,
260 .acls = NULL,
261 .processName = "com.ipc.Shell",
262 .aplStr = "normal",
263 };
264 tokenId = GetAccessTokenId(&infoInstance);
265 SetSelfTokenID(tokenId);
266 flag = false;
267 }
268 }
269
270 // 注册到本设备内
main()271 int main()
272 {
273 AddPermission();
274 ZLOGW(LABEL, "ipc_shell_server init");
275 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
276 sptr<IRemoteObject> newInstance = new ShellAbility();
277 int result = samgr->AddSystemAbility(1602, newInstance);
278 ZLOGW(LABEL, "Add SystemAbility ipc_shell_server result:%{public}d", result);
279 IPCSkeleton::JoinWorkThread();
280 return 0;
281 }
282