1 /*
2 * Copyright (c) 2021 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 "trans_server_stub.h"
17
18 #include "ipc_skeleton.h"
19 #include "lnn_connection_addr_utils.h"
20 #include "securec.h"
21 #include "softbus_common.h"
22 #include "softbus_conn_interface.h"
23 #include "softbus_errcode.h"
24 #include "softbus_log.h"
25 #include "softbus_permission.h"
26 #include "softbus_proxychannel_manager.h"
27 #include "softbus_trans_def.h"
28 #include "trans_auth_manager.h"
29 #include "trans_channel_manager.h"
30 #include "trans_session_manager.h"
31 #include "trans_session_service.h"
32
33
ServerCreateSessionServer(IpcIo * req,IpcIo * reply)34 int32_t ServerCreateSessionServer(IpcIo *req, IpcIo *reply)
35 {
36 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_INFO, "create session server ipc server pop");
37 if (req == NULL || reply == NULL) {
38 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "invalid param");
39 return SOFTBUS_INVALID_PARAM;
40 }
41 uint32_t size;
42 const char *pkgName = (const char*)ReadString(req, &size);
43 const char *sessionName = (const char *)ReadString(req, &size);
44 int32_t callingUid = GetCallingUid();
45 int32_t callingPid = GetCallingPid();
46 if (CheckTransPermission(callingUid, callingPid, pkgName, sessionName, ACTION_CREATE) != SOFTBUS_OK) {
47 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "ServerCreateSessionServer no permission");
48 WriteInt32(reply, SOFTBUS_PERMISSION_DENIED);
49 return SOFTBUS_PERMISSION_DENIED;
50 }
51 int32_t ret = TransCreateSessionServer(pkgName, sessionName, callingUid, callingPid);
52 WriteInt32(reply, ret);
53 return ret;
54 }
55
ServerRemoveSessionServer(IpcIo * req,IpcIo * reply)56 int32_t ServerRemoveSessionServer(IpcIo *req, IpcIo *reply)
57 {
58 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_INFO, "remove session server ipc server pop");
59 if (req == NULL || reply == NULL) {
60 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "invalid param");
61 return SOFTBUS_INVALID_PARAM;
62 }
63 uint32_t size;
64 const char *pkgName = (const char*)ReadString(req, &size);
65 const char *sessionName = (const char *)ReadString(req, &size);
66 int32_t callingUid = GetCallingUid();
67 int32_t callingPid = GetCallingPid();
68 if (CheckTransPermission(callingUid, callingPid, pkgName, sessionName, ACTION_CREATE) != SOFTBUS_OK) {
69 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "ServerRemoveSessionServer no permission");
70 WriteInt32(reply, SOFTBUS_PERMISSION_DENIED);
71 return SOFTBUS_PERMISSION_DENIED;
72 }
73 int32_t ret = TransRemoveSessionServer(pkgName, sessionName);
74 WriteInt32(reply, ret);
75 return ret;
76 }
77
CheckOpenSessionPremission(const char * sessionName,const char * peerSessionName)78 static int32_t CheckOpenSessionPremission(const char *sessionName, const char *peerSessionName)
79 {
80 char pkgName[PKG_NAME_SIZE_MAX];
81 if (TransGetPkgNameBySessionName(sessionName, pkgName, PKG_NAME_SIZE_MAX) != SOFTBUS_OK) {
82 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "OpenSession TransGetPkgNameBySessionName failed");
83 return SOFTBUS_INVALID_PARAM;
84 }
85
86 int32_t callingUid = GetCallingUid();
87 int32_t callingPid = GetCallingPid();
88 if (CheckTransPermission(callingUid, callingPid, pkgName, sessionName, ACTION_OPEN) != SOFTBUS_OK) {
89 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "OpenSession no permission");
90 return SOFTBUS_PERMISSION_DENIED;
91 }
92
93 if (CheckTransSecLevel(sessionName, peerSessionName) != SOFTBUS_OK) {
94 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "OpenSession sec level invalid");
95 return SOFTBUS_PERMISSION_DENIED;
96 }
97 return SOFTBUS_OK;
98 }
99
ServerOpenSession(IpcIo * req,IpcIo * reply)100 int32_t ServerOpenSession(IpcIo *req, IpcIo *reply)
101 {
102 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_INFO, "open session ipc server pop");
103 if (req == NULL || reply == NULL) {
104 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "invalid param");
105 return SOFTBUS_INVALID_PARAM;
106 }
107
108 int32_t ret;
109 uint32_t size;
110 SessionParam param;
111 TransSerializer transSerializer;
112 transSerializer.transInfo.channelId = INVALID_CHANNEL_ID;
113 transSerializer.transInfo.channelType = CHANNEL_TYPE_BUTT;
114 param.sessionName = (const char*)ReadString(req, &size);
115 param.peerSessionName = (const char *)ReadString(req, &size);
116 param.peerDeviceId = (const char *)ReadString(req, &size);
117 param.groupId = (const char *)ReadString(req, &size);
118 param.attr = (SessionAttribute *)ReadRawData(req, sizeof(SessionAttribute));
119
120 ret = CheckOpenSessionPremission(param.sessionName, param.peerSessionName);
121 if (ret != SOFTBUS_OK) {
122 transSerializer.ret = ret;
123 WriteUint32(reply, sizeof(TransSerializer));
124 bool value = WriteBuffer(reply, (void *)&transSerializer, sizeof(TransSerializer));
125 if (!value) {
126 return SOFTBUS_ERR;
127 }
128 return ret;
129 }
130
131 ret = TransOpenSession(¶m, &(transSerializer.transInfo));
132 transSerializer.ret = ret;
133 WriteUint32(reply, sizeof(TransSerializer));
134 bool value = WriteBuffer(reply, (void *)&transSerializer, sizeof(TransSerializer));
135 if (!value) {
136 return SOFTBUS_ERR;
137 }
138 return ret;
139 }
140
ServerOpenAuthSession(IpcIo * req,IpcIo * reply)141 int32_t ServerOpenAuthSession(IpcIo *req, IpcIo *reply)
142 {
143 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "open non encrypt session ipc server pop");
144 if (req == NULL || reply == NULL) {
145 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "invalid param");
146 return SOFTBUS_INVALID_PARAM;
147 }
148 int32_t ret;
149 uint32_t size;
150 ConnectOption connOpt;
151 const char *sessionName = (const char*)ReadString(req, &size);
152 ConnectionAddr *addr = (ConnectionAddr *)ReadRawData(req, sizeof(ConnectionAddr));
153 if (!LnnConvertAddrToOption(addr, &connOpt)) {
154 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "LnnConvertAddrToOption fail");
155 WriteInt32(reply, SOFTBUS_ERR);
156 return SOFTBUS_ERR;
157 }
158 ret = CheckOpenSessionPremission(sessionName, sessionName);
159 if (ret != SOFTBUS_OK) {
160 WriteInt32(reply, ret);
161 return ret;
162 }
163 ret = TransOpenAuthChannel(sessionName, &connOpt);
164 WriteInt32(reply, ret);
165 return ret;
166 }
167
ServerNotifyAuthSuccess(IpcIo * req,IpcIo * reply)168 int32_t ServerNotifyAuthSuccess(IpcIo *req, IpcIo *reply)
169 {
170 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "set auth result server pop");
171 if (req == NULL || reply == NULL) {
172 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "invalid param");
173 return SOFTBUS_INVALID_PARAM;
174 }
175 int32_t channelId = 0;
176 int32_t channelType = -1;
177 ReadInt32(req, &channelId);
178 ReadInt32(req, &channelType);
179 int32_t callingUid = GetCallingUid();
180 int32_t callingPid = GetCallingPid();
181 char pkgName[PKG_NAME_SIZE_MAX];
182 char sessionName[SESSION_NAME_SIZE_MAX];
183 if (TransAuthGetNameByChanId(channelId, pkgName, sessionName,
184 PKG_NAME_SIZE_MAX, SESSION_NAME_SIZE_MAX) != SOFTBUS_OK) {
185 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "get session name fail");
186 WriteInt32(reply, SOFTBUS_TRANS_UDP_CLOSE_CHANNELID_INVALID);
187 return SOFTBUS_TRANS_UDP_CLOSE_CHANNELID_INVALID;
188 }
189 if (CheckTransPermission(callingUid, callingPid, pkgName, sessionName, ACTION_OPEN) != SOFTBUS_OK) {
190 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "ServerCloseChannel no permission");
191 WriteInt32(reply, SOFTBUS_PERMISSION_DENIED);
192 return SOFTBUS_PERMISSION_DENIED;
193 }
194
195 int32_t ret = TransNotifyAuthSuccess(channelId, channelType);
196 WriteInt32(reply, ret);
197 return ret;
198 }
199
ServerCloseChannel(IpcIo * req,IpcIo * reply)200 int32_t ServerCloseChannel(IpcIo *req, IpcIo *reply)
201 {
202 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "close channel ipc server pop");
203 if (req == NULL || reply == NULL) {
204 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "invalid param");
205 return SOFTBUS_INVALID_PARAM;
206 }
207
208 int32_t ret;
209 TransInfo info;
210 int32_t channelId = 0;
211 int32_t channelType = 0;
212 ReadInt32(req, &channelId);
213 ReadInt32(req, &channelType);
214
215 info.channelId = channelId;
216 info.channelType = channelType;
217 ret = TransCloseChannel(channelId, channelType);
218
219 WriteInt32(reply, ret);
220 return ret;
221 }
222
ServerSendSessionMsg(IpcIo * req,IpcIo * reply)223 int32_t ServerSendSessionMsg(IpcIo *req, IpcIo *reply)
224 {
225 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "server send session msg ipc server pop");
226 if (req == NULL || reply == NULL) {
227 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "invalid param");
228 return SOFTBUS_INVALID_PARAM;
229 }
230 int32_t channelId = 0;
231 int32_t channelType = 0;
232 int32_t msgType = 0;
233 ReadInt32(req, &channelId);
234 ReadInt32(req, &channelType);
235 ReadInt32(req, &msgType);
236 uint32_t size = 0;
237 ReadUint32(req, &size);
238 const void *data = (const void *)ReadBuffer(req, size);
239 int32_t ret = TransSendMsg(channelId, channelType, data, size, msgType);
240 WriteInt32(reply, ret);
241 return ret;
242 }
243