1 /*
2  * Copyright (c) 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 "get_self_permissions.h"
16 
17 #include <thread>
18 
19 #include "accesstoken_kit.h"
20 #include "storage_service_log.h"
21 #include "nativetoken_kit.h"
22 #include "token_setproc.h"
23 
24 namespace OHOS {
25 namespace StorageManager {
SetAccessTokenPermission(const std::string & processName,const std::vector<std::string> & permission,uint64_t & tokenId)26 void PermissionUtilsTest::SetAccessTokenPermission(const std::string &processName,
27     const std::vector<std::string> &permission, uint64_t &tokenId)
28 {
29     auto perms = std::make_unique<const char *[]>(permission.size());
30     for (size_t i = 0; i < permission.size(); i++) {
31         perms[i] = permission[i].c_str();
32     }
33 
34     NativeTokenInfoParams infoInstance = {
35         .dcapsNum = 0,
36         .permsNum = permission.size(),
37         .aclsNum = 0,
38         .dcaps = nullptr,
39         .perms = perms.get(),
40         .acls = nullptr,
41         .processName = processName.c_str(),
42         .aplStr = "system_basic",
43     };
44     tokenId = GetAccessTokenId(&infoInstance);
45     if (tokenId == 0) {
46         LOGE("Get Acess Token Id Failed");
47         return;
48     }
49     int ret = SetSelfTokenID(tokenId);
50     if (ret != 0) {
51         LOGE("Set Acess Token Id Failed");
52         return;
53     }
54     ret = Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
55     if (ret < 0) {
56         LOGE("Reload Native Token Info Failed");
57         return;
58     }
59 }
60 } // namespace StorageManager
61 } // namespace OHOS
62 
63