• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 #include "nativetoken_json_oper.h"
16 #include <stdlib.h>
17 #include <string.h>
18 #include <securec.h>
19 #include "nativetoken_log.h"
20 
FreeStrArray(char ** arr,int32_t num)21 void FreeStrArray(char **arr, int32_t num)
22 {
23     for (int32_t i = 0; i <= num; i++) {
24         if (arr[i] != NULL) {
25             free(arr[i]);
26             arr[i] = NULL;
27         }
28     }
29 }
30 
GetProcessNameFromJson(cJSON * cjsonItem,NativeTokenList * tokenNode)31 uint32_t GetProcessNameFromJson(cJSON *cjsonItem, NativeTokenList *tokenNode)
32 {
33     cJSON *processNameJson = cJSON_GetObjectItem(cjsonItem, PROCESS_KEY_NAME);
34     if (!cJSON_IsString(processNameJson) || (processNameJson->valuestring == NULL) ||
35         (strlen(processNameJson->valuestring) > MAX_PROCESS_NAME_LEN)) {
36         AT_LOG_ERROR("[ATLIB-%s]:processNameJson is invalid.", __func__);
37         return ATRET_FAILED;
38     }
39 
40     if (strcpy_s(tokenNode->processName, MAX_PROCESS_NAME_LEN + 1, processNameJson->valuestring) != EOK) {
41         AT_LOG_ERROR("[ATLIB-%s]:strcpy_s failed.", __func__);
42         return ATRET_FAILED;
43     }
44     return ATRET_SUCCESS;
45 }
46 
GetTokenIdFromJson(cJSON * cjsonItem,NativeTokenList * tokenNode)47 uint32_t GetTokenIdFromJson(cJSON *cjsonItem, NativeTokenList *tokenNode)
48 {
49     cJSON *tokenIdJson = cJSON_GetObjectItem(cjsonItem, TOKENID_KEY_NAME);
50     if ((!cJSON_IsNumber(tokenIdJson)) || (cJSON_GetNumberValue(tokenIdJson) <= 0)) {
51         AT_LOG_ERROR("[ATLIB-%s]:tokenIdJson is invalid.", __func__);
52         return ATRET_FAILED;
53     }
54 
55     AtInnerInfo *atIdInfo = (AtInnerInfo *)&(tokenIdJson->valueint);
56     if (atIdInfo->type != TOKEN_NATIVE_TYPE && atIdInfo->type != TOKEN_SHELL_TYPE) {
57         AT_LOG_ERROR("[ATLIB-%s]:tokenId type is invalid.", __func__);
58         return ATRET_FAILED;
59     }
60 
61     tokenNode->tokenId = (NativeAtId)tokenIdJson->valueint;
62     return ATRET_SUCCESS;
63 }
64 
GetAplFromJson(cJSON * cjsonItem,NativeTokenList * tokenNode)65 uint32_t GetAplFromJson(cJSON *cjsonItem, NativeTokenList *tokenNode)
66 {
67     cJSON *aplJson = cJSON_GetObjectItem(cjsonItem, APL_KEY_NAME);
68     if (!cJSON_IsNumber(aplJson)) {
69         AT_LOG_ERROR("[ATLIB-%s]:aplJson is invalid.", __func__);
70         return ATRET_FAILED;
71     }
72     int32_t apl = cJSON_GetNumberValue(aplJson);
73     if (apl <= 0 || apl > SYSTEM_CORE) {
74         AT_LOG_ERROR("[ATLIB-%s]:apl = %d in file is invalid.", __func__, apl);
75         return ATRET_FAILED;
76     }
77     tokenNode->apl = aplJson->valueint;
78     return ATRET_SUCCESS;
79 }
80 
GetInfoArrFromJson(cJSON * cjsonItem,char * strArr[],int32_t * strNum,StrArrayAttr * attr)81 uint32_t GetInfoArrFromJson(cJSON *cjsonItem, char *strArr[], int32_t *strNum, StrArrayAttr *attr)
82 {
83     cJSON *strArrJson = cJSON_GetObjectItem(cjsonItem, attr->strKey);
84     int32_t size = cJSON_GetArraySize(strArrJson);
85     if (size > attr->maxStrNum) {
86         AT_LOG_ERROR("[ATLIB-%s]:size = %d is invalid.", __func__, size);
87         return ATRET_FAILED;
88     }
89     *strNum = size;
90 
91     for (int32_t i = 0; i < size; i++) {
92         cJSON *item = cJSON_GetArrayItem(strArrJson, i);
93         if ((item == NULL) || (!cJSON_IsString(item)) || (item->valuestring == NULL)) {
94             AT_LOG_ERROR("[ATLIB-%s]:cJSON_GetArrayItem failed.", __func__);
95             return ATRET_FAILED;
96         }
97         size_t length = strlen(item->valuestring);
98         if (length > attr->maxStrLen) {
99             AT_LOG_ERROR("[ATLIB-%s]:item length %zu is invalid.", __func__, length);
100             return ATRET_FAILED;
101         }
102         strArr[i] = (char *)malloc(sizeof(char) * (length + 1));
103         if (strArr[i] == NULL) {
104             FreeStrArray(strArr, i - 1);
105             AT_LOG_ERROR("[ATLIB-%s]:malloc invalid.", __func__);
106             return ATRET_FAILED;
107         }
108         if (strcpy_s(strArr[i], length + 1, item->valuestring) != EOK) {
109             FreeStrArray(strArr, i);
110             AT_LOG_ERROR("[ATLIB-%s]:strcpy_s failed.", __func__);
111             return ATRET_FAILED;
112         }
113     }
114     return ATRET_SUCCESS;
115 }
116 
AddStrArrayInfo(cJSON * object,char * const strArray[],int32_t strNum,const char * strKey)117 static int32_t AddStrArrayInfo(cJSON *object, char* const strArray[], int32_t strNum, const char *strKey)
118 {
119     cJSON *strJsonArr = cJSON_CreateArray();
120     if (strJsonArr == NULL) {
121         AT_LOG_ERROR("[ATLIB-%s]:CreateArray failed, strKey :%s.", __func__, strKey);
122         return ATRET_FAILED;
123     }
124     for (int32_t i = 0; i < strNum; i++) {
125         cJSON *item =  cJSON_CreateString(strArray[i]);
126         if (item == NULL || !cJSON_AddItemToArray(strJsonArr, item)) {
127             AT_LOG_ERROR("[ATLIB-%s]:AddItemToArray failed, strKey : %s.", __func__, strKey);
128             cJSON_Delete(item);
129             cJSON_Delete(strJsonArr);
130             return ATRET_FAILED;
131         }
132     }
133     if (!cJSON_AddItemToObject(object, strKey, strJsonArr)) {
134         AT_LOG_ERROR("[ATLIB-%s]:AddItemToObject failed, strKey : %s.", __func__, strKey);
135         cJSON_Delete(strJsonArr);
136         return ATRET_FAILED;
137     }
138     return ATRET_SUCCESS;
139 }
140 
SetNativeTokenJsonObject(const NativeTokenList * curr,cJSON * object)141 int32_t SetNativeTokenJsonObject(const NativeTokenList *curr, cJSON *object)
142 {
143     cJSON *item = cJSON_CreateString(curr->processName);
144     if (item == NULL || !cJSON_AddItemToObject(object, PROCESS_KEY_NAME, item)) {
145         AT_LOG_ERROR("[ATLIB-%s]:processName cJSON_AddItemToObject failed.", __func__);
146         cJSON_Delete(item);
147         return ATRET_FAILED;
148     }
149 
150     item = cJSON_CreateNumber(curr->apl);
151     if (item == NULL || !cJSON_AddItemToObject(object, APL_KEY_NAME, item)) {
152         AT_LOG_ERROR("[ATLIB-%s]:APL cJSON_AddItemToObject failed.", __func__);
153         cJSON_Delete(item);
154         return ATRET_FAILED;
155     }
156 
157     item = cJSON_CreateNumber(DEFAULT_AT_VERSION);
158     if (item == NULL || !cJSON_AddItemToObject(object, VERSION_KEY_NAME, item)) {
159         AT_LOG_ERROR("[ATLIB-%s]:version cJSON_AddItemToObject failed.", __func__);
160         cJSON_Delete(item);
161         return ATRET_FAILED;
162     }
163 
164     item = cJSON_CreateNumber(curr->tokenId);
165     if (item == NULL || !cJSON_AddItemToObject(object, TOKENID_KEY_NAME, item)) {
166         AT_LOG_ERROR("[ATLIB-%s]:tokenId cJSON_AddItemToObject failed.", __func__);
167         cJSON_Delete(item);
168         return ATRET_FAILED;
169     }
170 
171     item = cJSON_CreateNumber(0);
172     if (item == NULL || !cJSON_AddItemToObject(object, TOKEN_ATTR_KEY_NAME, item)) {
173         AT_LOG_ERROR("[ATLIB-%s]:tokenAttr cJSON_AddItemToObject failed.", __func__);
174         cJSON_Delete(item);
175         return ATRET_FAILED;
176     }
177 
178     int32_t ret = AddStrArrayInfo(object, curr->dcaps, curr->dcapsNum, DCAPS_KEY_NAME);
179     if (ret != ATRET_SUCCESS) {
180         return ret;
181     }
182 
183     ret = AddStrArrayInfo(object, curr->perms, curr->permsNum, PERMS_KEY_NAME);
184     if (ret != ATRET_SUCCESS) {
185         return ret;
186     }
187 
188     ret = AddStrArrayInfo(object, curr->acls, curr->aclsNum, ACLS_KEY_NAME);
189     return ret;
190 }
191 
CreateNativeTokenJsonObject(const NativeTokenList * curr)192 cJSON *CreateNativeTokenJsonObject(const NativeTokenList *curr)
193 {
194     cJSON *object = cJSON_CreateObject();
195     if (object == NULL) {
196         AT_LOG_ERROR("[ATLIB-%s]:cJSON_CreateObject failed.", __func__);
197         return NULL;
198     }
199     if (SetNativeTokenJsonObject(curr, object) != ATRET_SUCCESS) {
200         cJSON_Delete(object);
201         return NULL;
202     }
203 
204     return object;
205 }
206 
UpdateStrArrayType(char * const strArr[],int32_t strNum,const char * strKey,cJSON * record)207 static uint32_t UpdateStrArrayType(char* const strArr[], int32_t strNum, const char *strKey, cJSON *record)
208 {
209     cJSON *strArrJson = cJSON_CreateArray();
210     if (strArrJson == NULL) {
211         AT_LOG_ERROR("[ATLIB-%s]:cJSON_CreateArray failed.", __func__);
212         return ATRET_FAILED;
213     }
214     for (int32_t i = 0; i < strNum; i++) {
215         cJSON *item =  cJSON_CreateString(strArr[i]);
216         if (item == NULL) {
217             AT_LOG_ERROR("[ATLIB-%s]:cJSON_CreateString failed.", __func__);
218             cJSON_Delete(strArrJson);
219             return ATRET_FAILED;
220         }
221         if (!cJSON_AddItemToArray(strArrJson, item)) {
222             AT_LOG_ERROR("[ATLIB-%s]:cJSON_AddItemToArray failed.", __func__);
223             cJSON_Delete(item);
224             cJSON_Delete(strArrJson);
225             return ATRET_FAILED;
226         }
227     }
228     if (cJSON_GetObjectItem(record, strKey) != NULL) {
229         if (!cJSON_ReplaceItemInObject(record, strKey, strArrJson)) {
230             AT_LOG_ERROR("[ATLIB-%s]:cJSON_ReplaceItemInObject failed.", __func__);
231             cJSON_Delete(strArrJson);
232             return ATRET_FAILED;
233         }
234     } else {
235         if (!cJSON_AddItemToObject(record, strKey, strArrJson)) {
236             AT_LOG_ERROR("[ATLIB-%s]:cJSON_AddItemToObject failed.", __func__);
237             cJSON_Delete(strArrJson);
238             return ATRET_FAILED;
239         }
240     }
241 
242     return ATRET_SUCCESS;
243 }
244 
UpdateItemcontent(const NativeTokenList * tokenNode,cJSON * record)245 static uint32_t UpdateItemcontent(const NativeTokenList *tokenNode, cJSON *record)
246 {
247     cJSON *itemApl =  cJSON_CreateNumber(tokenNode->apl);
248     if (itemApl == NULL) {
249         return ATRET_FAILED;
250     }
251     if (!cJSON_ReplaceItemInObject(record, APL_KEY_NAME, itemApl)) {
252         cJSON_Delete(itemApl);
253         AT_LOG_ERROR("[ATLIB-%s]:APL update failed.", __func__);
254         return ATRET_FAILED;
255     }
256 
257     uint32_t ret = UpdateStrArrayType(tokenNode->dcaps, tokenNode->dcapsNum, DCAPS_KEY_NAME, record);
258     if (ret != ATRET_SUCCESS) {
259         AT_LOG_ERROR("[ATLIB-%s]:dcaps update failed.", __func__);
260         return ATRET_FAILED;
261     }
262 
263     ret = UpdateStrArrayType(tokenNode->perms, tokenNode->permsNum, PERMS_KEY_NAME, record);
264     if (ret != ATRET_SUCCESS) {
265         AT_LOG_ERROR("[ATLIB-%s]:perms update failed.", __func__);
266         return ATRET_FAILED;
267     }
268 
269     ret = UpdateStrArrayType(tokenNode->acls, tokenNode->aclsNum, ACLS_KEY_NAME, record);
270     if (ret != ATRET_SUCCESS) {
271         AT_LOG_ERROR("[ATLIB-%s]:acls update failed.", __func__);
272         return ATRET_FAILED;
273     }
274     return ATRET_SUCCESS;
275 }
276 
UpdateGoalItemFromRecord(const NativeTokenList * tokenNode,cJSON * record)277 uint32_t UpdateGoalItemFromRecord(const NativeTokenList *tokenNode, cJSON *record)
278 {
279     int32_t arraySize = cJSON_GetArraySize(record);
280     for (int32_t i = 0; i < arraySize; i++) {
281         cJSON *cjsonItem = cJSON_GetArrayItem(record, i);
282         if (cjsonItem == NULL) {
283             AT_LOG_ERROR("[ATLIB-%s]:cJSON_GetArrayItem failed.", __func__);
284             return ATRET_FAILED;
285         }
286         cJSON *processNameJson = cJSON_GetObjectItem(cjsonItem, PROCESS_KEY_NAME);
287         if ((processNameJson == NULL) || (!cJSON_IsString(processNameJson)) || (processNameJson->valuestring == NULL)) {
288             AT_LOG_ERROR("[ATLIB-%s]:processNameJson is null.", __func__);
289             return ATRET_FAILED;
290         }
291         if (strcmp(processNameJson->valuestring, tokenNode->processName) == 0) {
292             return UpdateItemcontent(tokenNode, cjsonItem);
293         }
294     }
295     AT_LOG_ERROR("[ATLIB-%s]:cannot find process in config file.", __func__);
296     return ATRET_FAILED;
297 }
298