• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-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 <ohos_errno.h>
17 #include <ohos_init.h>
18 #include <pthread.h>
19 #include <string.h>
20 
21 #include "log.h"
22 
23 #include "feature.h"
24 #include "iproxy_client.h"
25 #include "iproxy_server.h"
26 #include "iunknown.h"
27 #include "liteipc_adapter.h"
28 #include "samgr_lite.h"
29 #include "service.h"
30 
31 #include "pms.h"
32 #include "pms_common.h"
33 #include "pms_types.h"
34 
35 typedef struct InnerPermLiteApi {
36     INHERIT_SERVER_IPROXY;
37     int (*CheckPermission)(int uid, const char *permissionName);
38     int (*GrantPermission)(const char *identifier, const char *permName);
39     int (*RevokePermission)(const char *identifier, const char *permName);
40     int (*GrantRuntimePermission)(int uid, const char *permissionName);
41     int (*RevokeRuntimePermission)(int uid, const char *permissionName);
42     int (*UpdatePermissionFlags)(const char *identifier, const char *permissionName, const int flags);
43 } InnerPermLiteApi;
44 
45 typedef struct InnerPermLite {
46     INHERIT_FEATURE;
47     INHERIT_IUNKNOWNENTRY(InnerPermLiteApi);
48     Identity identity;
49 } InnerPermLite;
50 
51 enum INNERFUNCID {
52     ID_CHECK = 10,
53     ID_GRANT,
54     ID_REVOKE,
55     ID_GRANT_RUNTIME,
56     ID_REVOKE_RUNTIME,
57     ID_UPDATE_PERMS_FLAGS,
58 };
59 
60 static void Init();
61 static const char *GetName(Feature *feature);
62 static void OnInitialize(Feature *feature, Service *parent, Identity identity);
63 static void OnStop(Feature *feature, Identity identity);
64 static BOOL OnMessage(Feature *feature, Request *request);
65 static int32 Invoke(IServerProxy *iProxy, int funcId, void *origin, IpcIo *req, IpcIo *reply);
66 
67 static InnerPermLite g_permlite = {
68     .GetName = GetName,
69     .OnInitialize = OnInitialize,
70     .OnStop = OnStop,
71     .OnMessage = OnMessage,
72     SERVER_IPROXY_IMPL_BEGIN,
73     .Invoke = Invoke,
74     .CheckPermission = CheckPermissionStat,
75     .GrantPermission = GrantPermission,
76     .RevokePermission = RevokePermission,
77     .GrantRuntimePermission = GrantRuntimePermission,
78     .RevokeRuntimePermission = RevokeRuntimePermission,
79     .UpdatePermissionFlags = UpdatePermissionFlags,
80     IPROXY_END,
81     .identity = {-1, -1, NULL},
82 };
83 
Init()84 static void Init()
85 {
86     SAMGR_GetInstance()->RegisterFeature(PERMISSION_SERVICE, (Feature *)&g_permlite);
87     SAMGR_GetInstance()->RegisterFeatureApi(PERMISSION_SERVICE, PERM_INNER_FEATURE, GET_IUNKNOWN(g_permlite));
88     HILOG_INFO(HILOG_MODULE_APP, "Init pms lite inner feature success");
89 }
90 APP_FEATURE_INIT(Init);
91 
GetName(Feature * feature)92 static const char *GetName(Feature *feature)
93 {
94     (void)feature;
95     return PERM_INNER_FEATURE;
96 }
97 
OnInitialize(Feature * feature,Service * parent,Identity identity)98 static void OnInitialize(Feature *feature, Service *parent, Identity identity)
99 {
100     (void)parent;
101     if (feature == NULL) {
102         return;
103     }
104     InnerPermLite *permlite = (InnerPermLite *)feature;
105     permlite->identity = identity;
106     HILOG_INFO(HILOG_MODULE_APP, "onInitialize pms lite inner feature");
107 }
108 
OnStop(Feature * feature,Identity identity)109 static void OnStop(Feature *feature, Identity identity)
110 {
111     (void)feature;
112     (void)identity;
113 }
114 
OnMessage(Feature * feature,Request * request)115 static BOOL OnMessage(Feature *feature, Request *request)
116 {
117     if (feature == NULL || request == NULL) {
118         return FALSE;
119     }
120     // call func
121     return TRUE;
122 }
123 
ReplyCheckPermission(const void * origin,IpcIo * req,IpcIo * reply,InnerPermLiteApi * api)124 static void ReplyCheckPermission(const void *origin, IpcIo *req, IpcIo *reply, InnerPermLiteApi* api)
125 {
126     pid_t callingPid = GetCallingPid(origin);
127     uid_t callingUid = GetCallingUid(origin);
128     HILOG_INFO(HILOG_MODULE_APP, "Enter ID_CHECK, [callerPid: %d][callerUid: %u]", callingPid, callingUid);
129 
130     size_t permLen = 0;
131     int64_t uid = IpcIoPopInt64(req);
132     char *permName = (char *)IpcIoPopString(req, &permLen);
133     int32_t ret = api->CheckPermission(uid, permName);
134     HILOG_INFO(HILOG_MODULE_APP, "check permission, [uid: %lld][perm: %s][ret: %d]", uid, permName, ret);
135     IpcIoPushInt32(reply, ret);
136 }
137 
ReplyGrantPermission(const void * origin,IpcIo * req,IpcIo * reply,InnerPermLiteApi * api)138 static void ReplyGrantPermission(const void *origin, IpcIo *req, IpcIo *reply, InnerPermLiteApi* api)
139 {
140     pid_t callingPid = GetCallingPid(origin);
141     uid_t callingUid = GetCallingUid(origin);
142     HILOG_INFO(HILOG_MODULE_APP, "Enter ID_GRANT, [callerPid: %d][callerUid: %u]", callingPid, callingUid);
143     size_t permLen = 0;
144     size_t idLen = 0;
145     char *identifier = (char *)IpcIoPopString(req, &idLen);
146     char *permName = (char *)IpcIoPopString(req, &permLen);
147     int32_t ret = api->GrantPermission(identifier, permName);
148     HILOG_INFO(HILOG_MODULE_APP, "grant permission, [id: %s][perm: %s][ret: %d]", identifier, permName, ret);
149     IpcIoPushInt32(reply, ret);
150 }
151 
ReplyRevokePermission(const void * origin,IpcIo * req,IpcIo * reply,InnerPermLiteApi * api)152 static void ReplyRevokePermission(const void *origin, IpcIo *req, IpcIo *reply, InnerPermLiteApi* api)
153 {
154     pid_t callingPid = GetCallingPid(origin);
155     uid_t callingUid = GetCallingUid(origin);
156     HILOG_INFO(HILOG_MODULE_APP, "Enter ID_REVOKE, [callerPid: %d][callerUid: %u]", callingPid, callingUid);
157     size_t permLen = 0;
158     size_t idLen = 0;
159     char *identifier = (char *)IpcIoPopString(req, &idLen);
160     char *permName = (char *)IpcIoPopString(req, &permLen);
161     int32_t ret = api->RevokePermission(identifier, permName);
162     HILOG_INFO(HILOG_MODULE_APP, "revoke permission, [id: %s][perm: %s][ret: %d]", identifier, permName, ret);
163     IpcIoPushInt32(reply, ret);
164 }
165 
ReplyGrantRuntimePermission(const void * origin,IpcIo * req,IpcIo * reply,InnerPermLiteApi * api)166 static void ReplyGrantRuntimePermission(const void *origin, IpcIo *req, IpcIo *reply, InnerPermLiteApi* api)
167 {
168     pid_t callingPid = GetCallingPid(origin);
169     uid_t callingUid = GetCallingUid(origin);
170     HILOG_INFO(HILOG_MODULE_APP, "Enter ID_GRANTRUNTIME, [callerPid: %d][callerUid: %u]", callingPid, callingUid);
171     size_t permLen = 0;
172     int64_t uid = IpcIoPopInt64(req);
173     char *permName = (char *)IpcIoPopString(req, &permLen);
174     int32_t ret = api->GrantRuntimePermission(uid, permName);
175     HILOG_INFO(HILOG_MODULE_APP, "grant runtime permission, [uid: %lld][perm: %s][ret: %d]", uid, permName, ret);
176     IpcIoPushInt32(reply, ret);
177 }
178 
ReplyRevokeRuntimePermission(const void * origin,IpcIo * req,IpcIo * reply,InnerPermLiteApi * api)179 static void ReplyRevokeRuntimePermission(const void *origin, IpcIo *req, IpcIo *reply, InnerPermLiteApi* api)
180 {
181     pid_t callingPid = GetCallingPid(origin);
182     uid_t callingUid = GetCallingUid(origin);
183     HILOG_INFO(HILOG_MODULE_APP, "Enter ID_REVOKERUNTIME, [callerPid: %d][callerUid: %u]", callingPid, callingUid);
184     size_t permLen = 0;
185     int64_t uid = IpcIoPopInt64(req);
186     char *permName = (char *)IpcIoPopString(req, &permLen);
187     int32_t ret = api->RevokeRuntimePermission(uid, permName);
188     HILOG_INFO(HILOG_MODULE_APP, "revoke runtime permission, [uid: %lld][perm: %s][ret: %d]", uid, permName, ret);
189     IpcIoPushInt32(reply, ret);
190 }
191 
ReplyUpdatePermissionFlags(const void * origin,IpcIo * req,IpcIo * reply,const InnerPermLiteApi * api)192 static void ReplyUpdatePermissionFlags(const void *origin, IpcIo *req, IpcIo *reply, const InnerPermLiteApi *api)
193 {
194     pid_t callingPid = GetCallingPid(origin);
195     uid_t callingUid = GetCallingUid(origin);
196     HILOG_INFO(HILOG_MODULE_APP, "Enter ID_UPDATE_PERMS_FLAGS, [callerPid: %d][callerUid: %u]", callingPid, callingUid);
197     size_t permLen = 0;
198     size_t idLen = 0;
199     char *identifier = (char *)IpcIoPopString(req, &idLen);
200     char *permName = (char *)IpcIoPopString(req, &permLen);
201     int32_t flags = IpcIoPopInt32(req);
202     int32_t ret = api->UpdatePermissionFlags(identifier, permName, flags);
203     HILOG_INFO(HILOG_MODULE_APP, "update runtime permission flags, [identifier: %s][perm: %s][flags:%d][ret: %d]",
204         identifier, permName, flags, ret);
205     IpcIoPushInt32(reply, ret);
206 }
207 
Invoke(IServerProxy * iProxy,int funcId,void * origin,IpcIo * req,IpcIo * reply)208 static int32 Invoke(IServerProxy *iProxy, int funcId, void *origin, IpcIo *req, IpcIo *reply)
209 {
210     InnerPermLiteApi *api = (InnerPermLiteApi *)iProxy;
211     switch (funcId) {
212         case ID_CHECK:
213             ReplyCheckPermission(origin, req, reply, api);
214             break;
215         case ID_GRANT:
216             ReplyGrantPermission(origin, req, reply, api);
217             break;
218         case ID_REVOKE:
219             ReplyRevokePermission(origin, req, reply, api);
220             break;
221         case ID_GRANT_RUNTIME:
222             ReplyGrantRuntimePermission(origin, req, reply, api);
223             break;
224         case ID_REVOKE_RUNTIME:
225             ReplyRevokeRuntimePermission(origin, req, reply, api);
226             break;
227         case ID_UPDATE_PERMS_FLAGS:
228             ReplyUpdatePermissionFlags(origin, req, reply, api);
229             break;
230         default:
231             break;
232     }
233     return EC_SUCCESS;
234 }
235