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 
16 //! This crate implements the accesstoken init function FFI binding.
17 
18 use std::ptr;
19 use std::ffi::{c_char, CString};
20 
21 #[repr(C)]
22 struct TokenInfoParams {
23     dcaps_num: i32,
24     perms_num: i32,
25     acls_num: i32,
26     dcaps: *const *const c_char,
27     perms: *const *const c_char,
28     acls: *const *const c_char,
29     process_name: *const c_char,
30     apl_str: *const c_char,
31 }
32 
33 extern "C" {
GetAccessTokenId(tokenInfo: *mut TokenInfoParams) -> u6434     fn GetAccessTokenId(tokenInfo: *mut TokenInfoParams) -> u64;
SetSelfTokenID(tokenID: u64) -> i3235     fn SetSelfTokenID(tokenID: u64) -> i32;
36 }
37 
38 /// Init access token ID for current process
init_access_token()39 pub fn init_access_token()
40 {
41     let name = CString::new("com.ipc.calc").expect("process name is invalid");
42     let apl = CString::new("normal").expect("apl string is invalid");
43     let mut param = TokenInfoParams {
44         dcaps_num: 0,
45         perms_num: 0,
46         acls_num: 0,
47         dcaps: ptr::null(),
48         perms: ptr::null(),
49         acls: ptr::null(),
50         process_name: name.as_ptr(),
51         apl_str: apl.as_ptr(),
52     };
53 
54     unsafe {
55         let token_id = GetAccessTokenId(&mut param as *mut TokenInfoParams);
56         SetSelfTokenID(token_id);
57     }
58 }