• 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.h"
16 
17 #ifdef WITH_SELINUX
18 #include <policycoreutils.h>
19 #endif // WITH_SELINUX
20 
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include "securec.h"
29 #include "nativetoken_json_oper.h"
30 #include "nativetoken_kit.h"
31 #include "nativetoken_log.h"
32 
33 
34 NativeTokenList *g_tokenListHead;
35 int32_t g_isNativeTokenInited = 0;
36 
GetFileBuff(const char * cfg,char ** retBuff)37 int32_t GetFileBuff(const char *cfg, char **retBuff)
38 {
39     struct stat fileStat;
40 
41     char filePath[PATH_MAX_LEN + 1] = {0};
42     if (realpath(cfg, filePath) == NULL) {
43         if (errno == ENOENT) {
44             /* file doesn't exist */
45             *retBuff = NULL;
46             return ATRET_SUCCESS;
47         }
48         AT_LOG_ERROR("[ATLIB-%s]:invalid filePath.", __func__);
49         return ATRET_FAILED;
50     }
51 
52     if (stat(filePath, &fileStat) != 0) {
53         AT_LOG_ERROR("[ATLIB-%s]:stat file failed.", __func__);
54         return ATRET_FAILED;
55     }
56 
57     if (fileStat.st_size == 0) {
58         *retBuff = NULL;
59         return ATRET_SUCCESS;
60     }
61 
62     if (fileStat.st_size > MAX_JSON_FILE_LEN) {
63         AT_LOG_ERROR("[ATLIB-%s]:stat file size is invalid.", __func__);
64         return ATRET_FAILED;
65     }
66 
67     size_t fileSize = (unsigned)fileStat.st_size;
68 
69     FILE *cfgFd = fopen(filePath, "r");
70     if (cfgFd == NULL) {
71         AT_LOG_ERROR("[ATLIB-%s]:fopen file failed.", __func__);
72         return ATRET_FAILED;
73     }
74 
75     char *buff = (char *)malloc((size_t)(fileSize + 1));
76     if (buff == NULL) {
77         AT_LOG_ERROR("[ATLIB-%s]:memory alloc failed.", __func__);
78         fclose(cfgFd);
79         return ATRET_FAILED;
80     }
81 
82     if (fread(buff, fileSize, 1, cfgFd) != 1) {
83         AT_LOG_ERROR("[ATLIB-%s]:fread failed.", __func__);
84         free(buff);
85         buff = NULL;
86         fclose(cfgFd);
87         return ATRET_FAILED;
88     }
89     buff[fileSize] = '\0';
90     *retBuff = buff;
91     fclose(cfgFd);
92     return ATRET_SUCCESS;
93 }
94 
StrAttrSet(StrArrayAttr * attr,uint32_t maxStrLen,int32_t maxStrNum,const char * strKey)95 static void StrAttrSet(StrArrayAttr *attr, uint32_t maxStrLen, int32_t maxStrNum, const char *strKey)
96 {
97     attr->maxStrLen = maxStrLen;
98     attr->maxStrNum = maxStrNum;
99     attr->strKey = strKey;
100 }
101 
GetNativeTokenFromJson(cJSON * cjsonItem,NativeTokenList * tokenNode)102 static int32_t GetNativeTokenFromJson(cJSON *cjsonItem, NativeTokenList *tokenNode)
103 {
104     uint32_t ret;
105     StrArrayAttr attr;
106 
107     ret = GetProcessNameFromJson(cjsonItem, tokenNode);
108     ret |= GetTokenIdFromJson(cjsonItem, tokenNode);
109     ret |= GetAplFromJson(cjsonItem, tokenNode);
110 
111     StrAttrSet(&attr, MAX_DCAP_LEN, MAX_DCAPS_NUM, DCAPS_KEY_NAME);
112     ret |= GetInfoArrFromJson(cjsonItem, tokenNode->dcaps, &(tokenNode->dcapsNum), &attr);
113     if (ret != ATRET_SUCCESS) {
114         AT_LOG_ERROR("[ATLIB-%s]:GetInfoArrFromJson failed for dcaps.", __func__);
115         return ATRET_FAILED;
116     }
117 
118     StrAttrSet(&attr, MAX_PERM_LEN, MAX_PERM_NUM, PERMS_KEY_NAME);
119     ret = GetInfoArrFromJson(cjsonItem, tokenNode->perms, &(tokenNode->permsNum), &attr);
120     if (ret != ATRET_SUCCESS) {
121         FreeStrArray(tokenNode->dcaps, tokenNode->dcapsNum - 1);
122         AT_LOG_ERROR("[ATLIB-%s]:GetInfoArrFromJson failed for perms.", __func__);
123         return ATRET_FAILED;
124     }
125 
126     StrAttrSet(&attr, MAX_PERM_LEN, MAX_PERM_NUM, ACLS_KEY_NAME);
127     ret = GetInfoArrFromJson(cjsonItem, tokenNode->acls, &(tokenNode->aclsNum), &attr);
128     if (ret != ATRET_SUCCESS) {
129         FreeStrArray(tokenNode->dcaps, tokenNode->dcapsNum - 1);
130         FreeStrArray(tokenNode->perms, tokenNode->permsNum - 1);
131         AT_LOG_ERROR("[ATLIB-%s]:GetInfoArrFromJson failed for acls.", __func__);
132         return ATRET_FAILED;
133     }
134     return ATRET_SUCCESS;
135 }
136 
GetTokenList(const cJSON * object)137 static int32_t GetTokenList(const cJSON *object)
138 {
139     NativeTokenList *tmp = NULL;
140 
141     if (object == NULL) {
142         AT_LOG_ERROR("[ATLIB-%s]:object is null.", __func__);
143         return ATRET_FAILED;
144     }
145     int32_t arraySize = cJSON_GetArraySize(object);
146 
147     for (int32_t i = 0; i < arraySize; i++) {
148         tmp = (NativeTokenList *)malloc(sizeof(NativeTokenList));
149         if (tmp == NULL) {
150             AT_LOG_ERROR("[ATLIB-%s]:memory alloc failed.", __func__);
151             return ATRET_FAILED;
152         }
153         cJSON *cjsonItem = cJSON_GetArrayItem(object, i);
154         if (cjsonItem == NULL) {
155             free(tmp);
156             AT_LOG_ERROR("[ATLIB-%s]:cJSON_GetArrayItem failed.", __func__);
157             return ATRET_FAILED;
158         }
159         if (GetNativeTokenFromJson(cjsonItem, tmp) != ATRET_SUCCESS) {
160             free(tmp);
161             return ATRET_FAILED;
162         }
163 
164         tmp->next = g_tokenListHead->next;
165         g_tokenListHead->next = tmp;
166     }
167     return ATRET_SUCCESS;
168 }
169 
ParseTokenInfo(void)170 static int32_t ParseTokenInfo(void)
171 {
172     char *fileBuff = NULL;
173     cJSON *record = NULL;
174     int32_t ret;
175 
176     ret = GetFileBuff(TOKEN_ID_CFG_FILE_PATH, &fileBuff);
177     if (ret != ATRET_SUCCESS) {
178         return ret;
179     }
180     if (fileBuff == NULL) {
181         return ATRET_SUCCESS;
182     }
183     record = cJSON_Parse(fileBuff);
184     free(fileBuff);
185     fileBuff = NULL;
186 
187     ret = GetTokenList(record);
188     cJSON_Delete(record);
189 
190     return ret;
191 }
192 
CreateCfgFile(void)193 static int32_t CreateCfgFile(void)
194 {
195     int32_t fd = open(TOKEN_ID_CFG_FILE_PATH, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP);
196     if (fd < 0) {
197         AT_LOG_ERROR("[ATLIB-%s]:open failed.", __func__);
198         return ATRET_FAILED;
199     }
200 
201 #ifdef WITH_SELINUX
202     Restorecon(TOKEN_ID_CFG_FILE_PATH);
203 #endif // WITH_SELINUX
204 
205     close(fd);
206     fd = -1;
207 
208     struct stat buf;
209     if (stat(TOKEN_ID_CFG_DIR_PATH, &buf) != 0) {
210         AT_LOG_ERROR("[ATLIB-%s]:stat folder path is invalid %d.",
211             __func__, errno);
212         return ATRET_FAILED;
213     }
214     if (chown(TOKEN_ID_CFG_FILE_PATH, buf.st_uid, buf.st_gid) != 0) {
215         AT_LOG_ERROR("[ATLIB-%s]:chown failed, errno is %d.", __func__, errno);
216         return ATRET_FAILED;
217     }
218 
219     return ATRET_SUCCESS;
220 }
221 
AtlibInit(void)222 int32_t AtlibInit(void)
223 {
224     g_tokenListHead = (NativeTokenList *)malloc(sizeof(NativeTokenList));
225     if (g_tokenListHead == NULL) {
226         AT_LOG_ERROR("[ATLIB-%s]:g_tokenListHead memory alloc failed.", __func__);
227         return ATRET_FAILED;
228     }
229     g_tokenListHead->next = NULL;
230 
231     int32_t ret = ParseTokenInfo();
232     if (ret != ATRET_SUCCESS) {
233         free(g_tokenListHead);
234         g_tokenListHead = NULL;
235         return ret;
236     }
237 
238     if (g_tokenListHead->next == NULL) {
239         if (CreateCfgFile() != ATRET_SUCCESS) {
240             return ATRET_FAILED;
241         }
242     }
243     g_isNativeTokenInited = 1;
244 
245     return ATRET_SUCCESS;
246 }
247 
GetRandomTokenId(uint32_t * randNum)248 static int32_t GetRandomTokenId(uint32_t *randNum)
249 {
250     uint32_t random;
251     ssize_t len;
252     int32_t fd = open("/dev/urandom", O_RDONLY);
253     if (fd < 0) {
254         return ATRET_FAILED;
255     }
256     len = read(fd, &random, sizeof(random));
257     (void)close(fd);
258 
259     if (len != sizeof(random)) {
260         AT_LOG_ERROR("[ATLIB-%s]:read failed.", __func__);
261         return ATRET_FAILED;
262     }
263     *randNum = random;
264     return ATRET_SUCCESS;
265 }
266 
IsTokenUniqueIdExist(uint32_t tokenUniqueId)267 static int32_t IsTokenUniqueIdExist(uint32_t tokenUniqueId)
268 {
269     NativeTokenList *tokenNode = g_tokenListHead->next;
270     while (tokenNode != NULL) {
271         AtInnerInfo *existToken = (AtInnerInfo *)&(tokenNode->tokenId);
272         if (tokenUniqueId == existToken->tokenUniqueId) {
273             return 1;
274         }
275         tokenNode = tokenNode->next;
276     }
277     return 0;
278 }
279 
CreateNativeTokenId(const char * processName)280 static NativeAtId CreateNativeTokenId(const char *processName)
281 {
282     uint32_t rand;
283     NativeAtId tokenId;
284     AtInnerInfo *innerId = (AtInnerInfo *)(&tokenId);
285     int32_t retry = MAX_RETRY_TIMES;
286 
287     while (retry > 0) {
288         if (GetRandomTokenId(&rand) != ATRET_SUCCESS) {
289             return INVALID_TOKEN_ID;
290         }
291         if (IsTokenUniqueIdExist(rand & (TOKEN_RANDOM_MASK)) == 0) {
292             break;
293         }
294         retry--;
295     }
296     if (retry == 0) {
297         AT_LOG_ERROR("[ATLIB-%s]:retry times is 0.", __func__);
298         return INVALID_TOKEN_ID;
299     }
300 
301     innerId->reserved = 0;
302     innerId->tokenUniqueId = rand & (TOKEN_RANDOM_MASK);
303     innerId->version = 1;
304 
305     if (strcmp(processName, HDC_PROCESS_NAME) == 0) {
306         innerId->type = TOKEN_SHELL_TYPE;
307     } else {
308         innerId->type = TOKEN_NATIVE_TYPE;
309     }
310 
311     return tokenId;
312 }
313 
GetAplLevel(const char * aplStr)314 static int32_t GetAplLevel(const char *aplStr)
315 {
316     if (aplStr == NULL) {
317         return 0;
318     }
319     if (strcmp(aplStr, "system_core") == 0) {
320         return SYSTEM_CORE; // system_core means apl level is 3
321     }
322     if (strcmp(aplStr, "system_basic") == 0) {
323         return SYSTEM_BASIC; // system_basic means apl level is 2
324     }
325     if (strcmp(aplStr, "normal") == 0) {
326         return NORMAL;
327     }
328     AT_LOG_ERROR("[ATLIB-%s]:aplStr is invalid.", __func__);
329     return 0;
330 }
331 
WriteToFile(const cJSON * root)332 static void WriteToFile(const cJSON *root)
333 {
334     char *jsonStr = NULL;
335     jsonStr = cJSON_PrintUnformatted(root);
336     if (jsonStr == NULL) {
337         AT_LOG_ERROR("[ATLIB-%s]:cJSON_PrintUnformatted failed.", __func__);
338         return;
339     }
340 
341     do {
342         int32_t fd = open(TOKEN_ID_CFG_FILE_PATH, O_RDWR | O_CREAT | O_TRUNC,
343                           S_IRUSR | S_IWUSR | S_IRGRP);
344         if (fd < 0) {
345             AT_LOG_ERROR("[ATLIB-%s]:open failed.", __func__);
346             break;
347         }
348         size_t strLen = strlen(jsonStr);
349         ssize_t writtenLen = write(fd, (void *)jsonStr, (size_t)strLen);
350         close(fd);
351         if (writtenLen < 0 || (size_t)writtenLen != strLen) {
352             AT_LOG_ERROR("[ATLIB-%s]:write failed, writtenLen is %zu.", __func__, writtenLen);
353             break;
354         }
355     } while (0);
356 
357     cJSON_free(jsonStr);
358     return;
359 }
360 
SaveTokenIdToCfg(const NativeTokenList * curr)361 static void SaveTokenIdToCfg(const NativeTokenList *curr)
362 {
363     char *fileBuff = NULL;
364     cJSON *record = NULL;
365     int32_t ret;
366 
367     ret = GetFileBuff(TOKEN_ID_CFG_FILE_PATH, &fileBuff);
368     if (ret != ATRET_SUCCESS) {
369         return;
370     }
371 
372     if (fileBuff == NULL) {
373         record = cJSON_CreateArray();
374     } else {
375         record = cJSON_Parse(fileBuff);
376         free(fileBuff);
377         fileBuff = NULL;
378     }
379 
380     if (record == NULL) {
381         AT_LOG_ERROR("[ATLIB-%s]:get record failed.", __func__);
382         return;
383     }
384 
385     cJSON *node = CreateNativeTokenJsonObject(curr);
386     if (node == NULL) {
387         cJSON_Delete(record);
388         return;
389     }
390     cJSON_AddItemToArray(record, node);
391 
392     WriteToFile(record);
393     cJSON_Delete(record);
394     return;
395 }
396 
CheckStrArray(const char ** strArray,int32_t strNum,int32_t maxNum,uint32_t maxInfoLen)397 static uint32_t CheckStrArray(const char **strArray, int32_t strNum, int32_t maxNum, uint32_t maxInfoLen)
398 {
399     if (((strArray == NULL) && (strNum != 0)) ||
400         (strNum > maxNum) || (strNum < 0)) {
401         AT_LOG_ERROR("[ATLIB-%s]:strArray is null or strNum is invalid.", __func__);
402         return ATRET_FAILED;
403     }
404     for (int32_t i = 0; i < strNum; i++) {
405         if ((strArray[i] == NULL) || (strlen(strArray[i]) > maxInfoLen) || (strlen(strArray[i]) == 0)) {
406             AT_LOG_ERROR("[ATLIB-%s]:strArray[%d] length is invalid.", __func__, i);
407             return ATRET_FAILED;
408         }
409     }
410     return ATRET_SUCCESS;
411 }
412 
CheckProcessInfo(NativeTokenInfoParams * tokenInfo,int32_t * aplRet)413 static uint32_t CheckProcessInfo(NativeTokenInfoParams *tokenInfo, int32_t *aplRet)
414 {
415     if ((tokenInfo->processName == NULL) || strlen(tokenInfo->processName) > MAX_PROCESS_NAME_LEN ||
416         strlen(tokenInfo->processName) == 0) {
417         AT_LOG_ERROR("[ATLIB-%s]:processName is invalid.", __func__);
418         return ATRET_FAILED;
419     }
420     uint32_t retDcap = CheckStrArray(tokenInfo->dcaps, tokenInfo->dcapsNum, MAX_DCAPS_NUM, MAX_DCAP_LEN);
421     if (retDcap != ATRET_SUCCESS) {
422         AT_LOG_ERROR("[ATLIB-%s]:dcaps is invalid.", __func__);
423         return ATRET_FAILED;
424     }
425     uint32_t retPerm = CheckStrArray(tokenInfo->perms, tokenInfo->permsNum, MAX_PERM_NUM, MAX_PERM_LEN);
426     if (retPerm != ATRET_SUCCESS) {
427         AT_LOG_ERROR("[ATLIB-%s]:perms is invalid.", __func__);
428         return ATRET_FAILED;
429     }
430 
431     uint32_t retAcl = CheckStrArray(tokenInfo->acls, tokenInfo->aclsNum, MAX_PERM_NUM, MAX_PERM_LEN);
432     if (retAcl != ATRET_SUCCESS) {
433         AT_LOG_ERROR("[ATLIB-%s]:acls is invalid.", __func__);
434         return ATRET_FAILED;
435     }
436 
437     if (tokenInfo->aclsNum > tokenInfo->permsNum) {
438         AT_LOG_ERROR("[ATLIB-%s]:aclsNum is invalid.", __func__);
439         return ATRET_FAILED;
440     }
441     int32_t apl = GetAplLevel(tokenInfo->aplStr);
442     if (apl == 0) {
443         return ATRET_FAILED;
444     }
445     *aplRet = apl;
446     return ATRET_SUCCESS;
447 }
448 
CreateStrArray(int32_t num,const char ** strArr,char ** strArrRes)449 static uint32_t CreateStrArray(int32_t num, const char **strArr, char **strArrRes)
450 {
451     for (int32_t i = 0; i < num; i++) {
452         strArrRes[i] = (char *)malloc(sizeof(char) * (strlen(strArr[i]) + 1));
453         if (strArrRes[i] == NULL ||
454             (strcpy_s(strArrRes[i], strlen(strArr[i]) + 1, strArr[i]) != EOK)) {
455             AT_LOG_ERROR("[ATLIB-%s]:copy strArr[%d] failed.", __func__, i);
456             FreeStrArray(strArrRes, i);
457             return ATRET_FAILED;
458         }
459     }
460     return ATRET_SUCCESS;
461 }
462 
AddNewTokenToListAndFile(const NativeTokenInfoParams * tokenInfo,int32_t aplIn,NativeAtId * tokenId)463 static uint32_t AddNewTokenToListAndFile(const NativeTokenInfoParams *tokenInfo,
464     int32_t aplIn, NativeAtId *tokenId)
465 {
466     NativeTokenList *tokenNode;
467     NativeAtId id;
468 
469     id = CreateNativeTokenId(tokenInfo->processName);
470     if (id == INVALID_TOKEN_ID) {
471         return ATRET_FAILED;
472     }
473 
474     tokenNode = (NativeTokenList *)malloc(sizeof(NativeTokenList));
475     if (tokenNode == NULL) {
476         AT_LOG_ERROR("[ATLIB-%s]:memory alloc failed.", __func__);
477         return ATRET_FAILED;
478     }
479     tokenNode->tokenId = id;
480     tokenNode->apl = aplIn;
481     if (strcpy_s(tokenNode->processName, MAX_PROCESS_NAME_LEN + 1, tokenInfo->processName) != EOK) {
482         AT_LOG_ERROR("[ATLIB-%s]:strcpy_s failed.", __func__);
483         free(tokenNode);
484         return ATRET_FAILED;
485     }
486     tokenNode->dcapsNum = tokenInfo->dcapsNum;
487     tokenNode->permsNum = tokenInfo->permsNum;
488     tokenNode->aclsNum = tokenInfo->aclsNum;
489 
490     if (CreateStrArray(tokenInfo->dcapsNum, tokenInfo->dcaps, tokenNode->dcaps) != ATRET_SUCCESS) {
491         free(tokenNode);
492         return ATRET_FAILED;
493     }
494     if (CreateStrArray(tokenInfo->permsNum, tokenInfo->perms, tokenNode->perms) != ATRET_SUCCESS) {
495         FreeStrArray(tokenNode->dcaps, tokenInfo->dcapsNum - 1);
496         free(tokenNode);
497         return ATRET_FAILED;
498     }
499     if (CreateStrArray(tokenInfo->aclsNum, tokenInfo->acls, tokenNode->acls) != ATRET_SUCCESS) {
500         FreeStrArray(tokenNode->dcaps, tokenInfo->dcapsNum - 1);
501         FreeStrArray(tokenNode->perms, tokenInfo->permsNum - 1);
502         free(tokenNode);
503         return ATRET_FAILED;
504     }
505 
506     tokenNode->next = g_tokenListHead->next;
507     g_tokenListHead->next = tokenNode;
508 
509     *tokenId = id;
510 
511     SaveTokenIdToCfg(tokenNode);
512     return ATRET_SUCCESS;
513 }
514 
CompareTokenInfo(const NativeTokenList * tokenNode,const char ** dcapsIn,int32_t dcapNumIn,int32_t aplIn)515 static int32_t CompareTokenInfo(const NativeTokenList *tokenNode,
516                                 const char **dcapsIn, int32_t dcapNumIn, int32_t aplIn)
517 {
518     if (tokenNode->apl != aplIn) {
519         return 1;
520     }
521     if (tokenNode->dcapsNum != dcapNumIn) {
522         return 1;
523     }
524     for (int32_t i = 0; i < dcapNumIn; i++) {
525         if (strcmp(tokenNode->dcaps[i], dcapsIn[i]) != 0) {
526             return 1;
527         }
528     }
529     return 0;
530 }
531 
ComparePermsInfo(const NativeTokenList * tokenNode,const char ** permsIn,int32_t permsNumIn)532 static int32_t ComparePermsInfo(const NativeTokenList *tokenNode,
533                                 const char **permsIn, int32_t permsNumIn)
534 {
535     if (tokenNode->permsNum != permsNumIn) {
536         return 1;
537     }
538     for (int32_t i = 0; i < permsNumIn; i++) {
539         if (strcmp(tokenNode->perms[i], permsIn[i]) != 0) {
540             return 1;
541         }
542     }
543     return 0;
544 }
545 
UpdateStrArrayInList(char * strArr[],int32_t * strNum,const char ** strArrNew,int32_t strNumNew)546 static uint32_t UpdateStrArrayInList(char *strArr[], int32_t *strNum,
547     const char **strArrNew, int32_t strNumNew)
548 {
549     for (int32_t i = 0; i < *strNum; i++) {
550         free(strArr[i]);
551         strArr[i] = NULL;
552     }
553 
554     *strNum = strNumNew;
555     for (int32_t i = 0; i < strNumNew; i++) {
556         size_t len = strlen(strArrNew[i]) + 1;
557         strArr[i] = (char *)malloc(sizeof(char) * len);
558         if (strArr[i] == NULL || (strcpy_s(strArr[i], len, strArrNew[i]) != EOK)) {
559             AT_LOG_ERROR("[ATLIB-%s]:copy strArr[%d] failed.", __func__, i);
560             FreeStrArray(strArr, i);
561             return ATRET_FAILED;
562         }
563     }
564     return ATRET_SUCCESS;
565 }
566 
UpdateTokenInfoInList(NativeTokenList * tokenNode,const NativeTokenInfoParams * tokenInfo)567 static uint32_t UpdateTokenInfoInList(NativeTokenList *tokenNode,
568                                       const NativeTokenInfoParams *tokenInfo)
569 {
570     tokenNode->apl = GetAplLevel(tokenInfo->aplStr);
571 
572     uint32_t ret = UpdateStrArrayInList(tokenNode->dcaps, &(tokenNode->dcapsNum),
573         tokenInfo->dcaps, tokenInfo->dcapsNum);
574     if (ret != ATRET_SUCCESS) {
575         return ret;
576     }
577     ret = UpdateStrArrayInList(tokenNode->perms, &(tokenNode->permsNum),
578         tokenInfo->perms, tokenInfo->permsNum);
579     if (ret != ATRET_SUCCESS) {
580         FreeStrArray(tokenNode->dcaps, tokenNode->dcapsNum - 1);
581     }
582     ret = UpdateStrArrayInList(tokenNode->acls, &(tokenNode->aclsNum),
583         tokenInfo->acls, tokenInfo->aclsNum);
584     if (ret != ATRET_SUCCESS) {
585         FreeStrArray(tokenNode->dcaps, tokenNode->dcapsNum - 1);
586         FreeStrArray(tokenNode->perms, tokenNode->permsNum - 1);
587     }
588     return ret;
589 }
590 
UpdateInfoInCfgFile(const NativeTokenList * tokenNode)591 static uint32_t UpdateInfoInCfgFile(const NativeTokenList *tokenNode)
592 {
593     cJSON *record = NULL;
594     char *fileBuff = NULL;
595     uint32_t ret;
596 
597     if (GetFileBuff(TOKEN_ID_CFG_FILE_PATH, &fileBuff) != ATRET_SUCCESS) {
598         return ATRET_FAILED;
599     }
600 
601     if (fileBuff == NULL) {
602         record = cJSON_CreateArray();
603     } else {
604         record = cJSON_Parse(fileBuff);
605         free(fileBuff);
606         fileBuff = NULL;
607     }
608 
609     if (record == NULL) {
610         AT_LOG_ERROR("[ATLIB-%s]:get record failed.", __func__);
611         return ATRET_FAILED;
612     }
613 
614     ret = UpdateGoalItemFromRecord(tokenNode, record);
615     if (ret != ATRET_SUCCESS) {
616         AT_LOG_ERROR("[ATLIB-%s]:UpdateGoalItemFromRecord failed.", __func__);
617         cJSON_Delete(record);
618         return ATRET_FAILED;
619     }
620 
621     WriteToFile(record);
622     cJSON_Delete(record);
623     return ATRET_SUCCESS;
624 }
625 
GetAccessTokenId(NativeTokenInfoParams * tokenInfo)626 uint64_t GetAccessTokenId(NativeTokenInfoParams *tokenInfo)
627 {
628     NativeAtId tokenId = 0;
629     uint64_t result = 0;
630     int32_t apl;
631     NativeAtIdEx *atPoint = (NativeAtIdEx *)(&result);
632 
633     if ((g_isNativeTokenInited == 0) && (AtlibInit() != ATRET_SUCCESS)) {
634         return INVALID_TOKEN_ID;
635     }
636     uint32_t ret = CheckProcessInfo(tokenInfo, &apl);
637     if (ret != ATRET_SUCCESS) {
638         return INVALID_TOKEN_ID;
639     }
640 
641     NativeTokenList *tokenNode = g_tokenListHead->next;
642     while (tokenNode != NULL) {
643         if (strcmp(tokenNode->processName, tokenInfo->processName) == 0) {
644             tokenId = tokenNode->tokenId;
645             break;
646         }
647         tokenNode = tokenNode->next;
648     }
649 
650     if (tokenNode == NULL) {
651         ret = AddNewTokenToListAndFile(tokenInfo, apl, &tokenId);
652     } else {
653         int32_t needTokenUpdate = CompareTokenInfo(tokenNode, tokenInfo->dcaps, tokenInfo->dcapsNum, apl);
654         int32_t needPermUpdate = ComparePermsInfo(tokenNode, tokenInfo->perms, tokenInfo->permsNum);
655         if ((needTokenUpdate != 0) || (needPermUpdate != 0)) {
656             ret = UpdateTokenInfoInList(tokenNode, tokenInfo);
657             ret |= UpdateInfoInCfgFile(tokenNode);
658         }
659     }
660     if (ret != ATRET_SUCCESS) {
661         return INVALID_TOKEN_ID;
662     }
663 
664     atPoint->tokenId = tokenId;
665     atPoint->tokenAttr = 0;
666     return result;
667 }
668