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