1 /*
2 * Copyright (c) 2024 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 "permission.h"
17
18 #include <iostream>
19 #include <vector>
20
21 #include "accesstoken_kit.h"
22 #include "nativetoken_kit.h"
23 #include "token_setproc.h"
24
SetAccessTokenPermission()25 void SetAccessTokenPermission()
26 {
27 auto permissions = std::vector<std::string>();
28 permissions.push_back("ohos.permission.INTERNET");
29 permissions.push_back("ohos.permission.GET_NETWORK_INFO");
30 permissions.push_back("ohos.permission.READ_MEDIA");
31 permissions.push_back("ohos.permission.WRITE_MEDIA");
32 permissions.push_back("ohos.permission.RUNNING_STATE_OBSERVER");
33 permissions.push_back("ohos.permission.GET_NETWORK_INFO");
34 permissions.push_back("ohos.permission.CONNECTIVITY_INTERNAL");
35 permissions.push_back("ohos.permission.SEND_TASK_COMPLETE_EVENT");
36 permissions.push_back("ohos.permission.ACCESS_CERT_MANAGER");
37 permissions.push_back("ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS");
38 permissions.push_back("ohos.permission.MANAGE_LOCAL_ACCOUNTS");
39
40 auto processName = std::string("rust_request_test");
41 auto perms = std::make_unique<const char *[]>(permissions.size());
42 for (size_t i = 0; i < permissions.size(); i++) {
43 perms[i] = permissions[i].c_str();
44 }
45
46 NativeTokenInfoParams infoInstance = {
47 .dcapsNum = 0,
48 .permsNum = permissions.size(),
49 .aclsNum = 0,
50 .dcaps = nullptr,
51 .perms = perms.get(),
52 .acls = nullptr,
53 .processName = processName.c_str(),
54 .aplStr = "system_core",
55 };
56 auto tokenId = GetAccessTokenId(&infoInstance);
57 if (tokenId == 0) {
58 std::cout << "GetAccessTokenId failed" << std::endl;
59 return;
60 }
61 int ret = SetSelfTokenID(tokenId);
62 if (ret != 0) {
63 return;
64 }
65 ret = OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
66 if (ret < 0) {
67 return;
68 }
69 }
70