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 #include <cstdio>
16 #include <cstdlib>
17 #include <iostream>
18 #include <memory>
19 #include <string>
20 #include "accesstoken_kit.h"
21 #include "nativetoken_kit.h"
22 #include "token_setproc.h"
23
24 using namespace std;
25 using namespace OHOS::Security::AccessToken;
26
NativeTokenGet()27 static void NativeTokenGet()
28 {
29 uint64_t tokenID;
30 const char **perms = new const char *[5]; // size of array
31 perms[0] = "ohos.permission.DISTRIBUTED_DATASYNC"; // 0: index
32 perms[1] = "ohos.permission.GRANT_SENSITIVE_PERMISSIONS"; // 1: index
33 perms[2] = "ohos.permission.REVOKE_SENSITIVE_PERMISSIONS"; // 2: index
34 perms[3] = "ohos.permission.GET_SENSITIVE_PERMISSIONS"; // 3: index
35 perms[4] = "ohos.permission.DISABLE_PERMISSION_DIALOG"; // 4: index
36
37 NativeTokenInfoParams infoInstance = {
38 .dcapsNum = 0,
39 .permsNum = 5, // size of permission list
40 .aclsNum = 0,
41 .dcaps = nullptr,
42 .perms = perms,
43 .acls = nullptr,
44 .aplStr = "system_core",
45 };
46
47 infoInstance.processName = "SetPermDialogCapTest";
48 tokenID = GetAccessTokenId(&infoInstance);
49 SetSelfTokenID(tokenID);
50 AccessTokenKit::ReloadNativeTokenInfo();
51 delete[] perms;
52 }
53
main(int argc,char * argv[])54 int32_t main(int argc, char *argv[])
55 {
56 if (argc < 3) { // 3: size
57 std::cout << "Help: ./SetPermDialogCapTest bundleName 0/1 (0: allow, 1: forbid)\n" << std::endl;
58 }
59
60 NativeTokenGet();
61
62 std::string bundle = argv[1];
63 bool isForbidden = static_cast<bool>(atoi(argv[2]));
64 HapBaseInfo baseInfo;
65 baseInfo.bundleName = bundle;
66 baseInfo.instIndex = 0;
67 baseInfo.userID = 100; // 100: user id
68 AccessTokenKit::SetPermDialogCap(baseInfo, isForbidden);
69 return 0;
70 }
71