• 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 "feature.h"
22 #include "iproxy_client.h"
23 #include "iproxy_server.h"
24 #include "iunknown.h"
25 #include "liteipc_adapter.h"
26 #include "log.h"
27 
28 #include "pms.h"
29 #include "pms_common.h"
30 #include "pms_types.h"
31 #include "samgr_lite.h"
32 #include "service.h"
33 
34 typedef struct PermLiteApi {
35     INHERIT_SERVER_IPROXY;
36     int (*CheckPermission)(int uid, const char *permissionName);
37     int (*QueryPermission)(const char *identifier, PermissionSaved **permissions, int *permNum);
38 } PermLiteApi;
39 
40 typedef struct PermLite {
41     INHERIT_FEATURE;
42     INHERIT_IUNKNOWNENTRY(PermLiteApi);
43     Identity identity;
44 } PermLite;
45 
46 enum FUNCID {
47     ID_CHECK_SELF = 0,
48     ID_QUERY,
49 };
50 
51 static void Init(void);
52 static const char *GetName(Feature *feature);
53 static void OnInitialize(Feature *feature, Service *parent, Identity identity);
54 static void OnStop(Feature *feature, Identity identity);
55 static BOOL OnMessage(Feature *feature, Request *request);
56 static int32 Invoke(IServerProxy *iProxy, int funcId, void *origin, IpcIo *req, IpcIo *reply);
57 
58 static PermLite g_permlite = {
59     .GetName = GetName,
60     .OnInitialize = OnInitialize,
61     .OnStop = OnStop,
62     .OnMessage = OnMessage,
63     SERVER_IPROXY_IMPL_BEGIN,
64     .Invoke = Invoke,
65     .CheckPermission = CheckPermissionStat,
66     .QueryPermission = QueryPermission,
67     IPROXY_END,
68     .identity = {-1, -1, NULL},
69 };
70 
Init(void)71 static void Init(void)
72 {
73     SAMGR_GetInstance()->RegisterFeature(PERMISSION_SERVICE, (Feature *)&g_permlite);
74     SAMGR_GetInstance()->RegisterFeatureApi(PERMISSION_SERVICE, PERM_FEATURE, GET_IUNKNOWN(g_permlite));
75     HILOG_INFO(HILOG_MODULE_APP, "Init pms lite feature success");
76 }
77 APP_FEATURE_INIT(Init);
78 
GetName(Feature * feature)79 static const char *GetName(Feature *feature)
80 {
81     (void)feature;
82     return PERM_FEATURE;
83 }
84 
OnInitialize(Feature * feature,Service * parent,Identity identity)85 static void OnInitialize(Feature *feature, Service *parent, Identity identity)
86 {
87     (void)parent;
88     if (feature == NULL) {
89         return;
90     }
91     PermLite *permlite = (PermLite *)feature;
92     permlite->identity = identity;
93     HILOG_INFO(HILOG_MODULE_APP, "onInitialize pms lite feature");
94 }
95 
OnStop(Feature * feature,Identity identity)96 static void OnStop(Feature *feature, Identity identity)
97 {
98     (void)feature;
99     (void)identity;
100 }
101 
OnMessage(Feature * feature,Request * request)102 static BOOL OnMessage(Feature *feature, Request *request)
103 {
104     if (feature == NULL || request == NULL) {
105         return FALSE;
106     }
107     // call func
108     return TRUE;
109 }
110 
InnerFreeDataBuff(void * ptr)111 void static InnerFreeDataBuff(void *ptr)
112 {
113     if (ptr != NULL) {
114         free(ptr);
115     }
116 }
117 
ReplyCheckSelfPermission(const void * origin,IpcIo * req,IpcIo * reply,PermLiteApi * api)118 static void ReplyCheckSelfPermission(const void *origin, IpcIo *req, IpcIo *reply, PermLiteApi* api)
119 {
120     pid_t callingPid = GetCallingPid(origin);
121     uid_t callingUid = GetCallingUid(origin);
122     HILOG_INFO(HILOG_MODULE_APP, "Enter ID_CHECKSELF, [callerPid: %d][callerUid: %u]", callingPid, callingUid);
123 
124     size_t permLen = 0;
125     char *permName = (char *)IpcIoPopString(req, &permLen);
126     int32_t ret = api->CheckPermission(callingUid, permName);
127     HILOG_INFO(HILOG_MODULE_APP, "check self permission, [uid: %u][perm: %s][ret: %d]", callingUid, permName, ret);
128     IpcIoPushInt32(reply, ret);
129 }
130 
ReplyQueryPermission(const void * origin,IpcIo * req,IpcIo * reply)131 static void ReplyQueryPermission(const void *origin, IpcIo *req, IpcIo *reply)
132 {
133     pid_t callingPid = GetCallingPid(origin);
134     uid_t callingUid = GetCallingUid(origin);
135     HILOG_INFO(HILOG_MODULE_APP, "Enter ID_Query, [callerPid: %d][callerUid: %u]", callingPid, callingUid);
136     size_t idLen = 0;
137     int ret = 0;
138     char *identifier = (char *)IpcIoPopString(req, &idLen);
139     char *jsonStr = QueryPermissionString(identifier, &ret);
140     if (jsonStr == NULL) {
141         return;
142     }
143 
144     IpcIoPushInt32(reply, ret);
145     if (ret != PERM_ERRORCODE_SUCCESS) {
146         free(jsonStr);
147         return;
148     }
149 #ifndef __LINUX__
150     BuffPtr dataBuff = {
151         .buffSz = strlen(jsonStr) + 1,
152         .buff = (char *)jsonStr,
153     };
154     IpcIoPushDataBuffWithFree(reply, &dataBuff, InnerFreeDataBuff);
155 #else
156     IpcIoPushString(reply, jsonStr);
157     free(jsonStr);
158 #endif
159 }
160 
Invoke(IServerProxy * iProxy,int funcId,void * origin,IpcIo * req,IpcIo * reply)161 static int32 Invoke(IServerProxy *iProxy, int funcId, void *origin, IpcIo *req, IpcIo *reply)
162 {
163     PermLiteApi *api = (PermLiteApi *)iProxy;
164     switch (funcId) {
165         case ID_CHECK_SELF:
166             ReplyCheckSelfPermission(origin, req, reply, api);
167             break;
168         case ID_QUERY:
169             ReplyQueryPermission(origin, req, reply);
170             break;
171         default:
172             break;
173     }
174     return EC_SUCCESS;
175 }
176