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 "privacy_kit.h"
23 #include "token_setproc.h"
24
25 using namespace std;
26 using namespace OHOS::Security::AccessToken;
27
NativeTokenGet()28 static void NativeTokenGet()
29 {
30 uint64_t tokenID;
31 const char **perms = new (std::nothrow) const char *[1]; // size of array
32 perms[0] = "ohos.permission.PERMISSION_USED_STATS"; // 0: index
33
34 NativeTokenInfoParams infoInstance = {
35 .dcapsNum = 0,
36 .permsNum = 1, // size of permission list
37 .aclsNum = 0,
38 .dcaps = nullptr,
39 .perms = perms,
40 .acls = nullptr,
41 .aplStr = "system_core",
42 };
43
44 infoInstance.processName = "AddPermissionUsedRecord";
45 tokenID = GetAccessTokenId(&infoInstance);
46 SetSelfTokenID(tokenID);
47 AccessTokenKit::ReloadNativeTokenInfo();
48 delete[] perms;
49 }
50
main(int argc,char * argv[])51 int32_t main(int argc, char *argv[])
52 {
53 if (argc < 3) { // 3: size
54 std::cout << "Help: ./AddPermissionUsedRecord tokenid permisisionName\n" << std::endl;
55 return 0;
56 }
57
58 NativeTokenGet();
59
60 uint32_t tokenId = static_cast<uint32_t>(atoi(argv[1])); // 1: index
61 std::string permisisionName = argv[2]; // 2: index
62 int32_t ret = PrivacyKit::AddPermissionUsedRecord(tokenId, permisisionName, 1, 0);
63 if (ret == 0) {
64 std::cout << "Success" << ret << std::endl;
65 } else {
66 std::cout << "Failed, error: " << ret << std::endl;
67 }
68 return 0;
69 }
70