diff --git a/BUILD.gn b/BUILD.gn index b9397ed3d2bec2db9b9e9bd00f15a3198ca93f67..1a7fc27f66d1b0a902f70b9c981c1c839618bf9b 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -19,6 +19,7 @@ group("accesstoken_build_module") { deps = [ "//base/security/access_token/interfaces/innerkits/accesstoken:libaccesstoken_sdk", "//base/security/access_token/interfaces/innerkits/nativetoken:libaccesstoken_lib", + "//base/security/access_token/interfaces/innerkits/token_setproc:libtoken_setproc", "//base/security/access_token/services/accesstokenmanager:accesstoken_manager_service", "//base/security/access_token/services/accesstokenmanager/main/sa_profile:accesstoken_sa_profile_standard", ] @@ -43,6 +44,7 @@ group("accesstoken_build_module_test") { "//base/security/access_token/interfaces/innerkits/accesstoken/test:unittest", "//base/security/access_token/interfaces/innerkits/nativetoken/test:unittest", "//base/security/access_token/interfaces/innerkits/tokensync/test:unittest", + "//base/security/access_token/interfaces/innerkits/token_setproc/test:unittest", "//base/security/access_token/services/accesstokenmanager/test:unittest", ] } diff --git a/interfaces/innerkits/token_setproc/.keep b/interfaces/innerkits/token_setproc/.keep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/interfaces/innerkits/token_setproc/BUILD.gn b/interfaces/innerkits/token_setproc/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..f3fd592dc937e6b438b50c53fd1b0b382588bb71 --- /dev/null +++ b/interfaces/innerkits/token_setproc/BUILD.gn @@ -0,0 +1,46 @@ +# Copyright (C) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") + +################################################################ +# C, Main, source file here. +################################################################ +config("token_setproc") { + visibility = [ ":*" ] + include_dirs = [ "include" ] +} + +ohos_static_library("libtoken_setproc") { + subsystem_name = "security" + part_name = "access_token" + + output_name = "libtoken_setproc" + + public_configs = [ ":token_setproc" ] + + cflags = [ "-Wall" ] + + include_dirs = [ + "include", + "src", + ] + + sources = [ + "src/token_setproc.c", + ] + + deps = [ + + ] +} \ No newline at end of file diff --git a/interfaces/innerkits/token_setproc/include/.keep b/interfaces/innerkits/token_setproc/include/.keep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/interfaces/innerkits/token_setproc/include/token_setproc.h b/interfaces/innerkits/token_setproc/include/token_setproc.h new file mode 100644 index 0000000000000000000000000000000000000000..57c05e2af708265bbce3b7219069c419cdf14a13 --- /dev/null +++ b/interfaces/innerkits/token_setproc/include/token_setproc.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#ifndef TOKEN_SETPROC_H +#define TOKEN_SETPROC_H +#include + +#ifdef __cplusplus +extern "C"{ +#endif + +uint64_t GetSelfTokenID(); + +int SetSelfTokenID(uint64_t tokenID); + +uint64_t GetFirstCallerTokenID(); + +int SetFirstCallerTokenID(uint64_t tokenID); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/interfaces/innerkits/token_setproc/src/.keep b/interfaces/innerkits/token_setproc/src/.keep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/interfaces/innerkits/token_setproc/src/token_setproc.c b/interfaces/innerkits/token_setproc/src/token_setproc.c new file mode 100644 index 0000000000000000000000000000000000000000..af0c0d8c2848764f0ec8d8c52c6f58c9462adebd --- /dev/null +++ b/interfaces/innerkits/token_setproc/src/token_setproc.c @@ -0,0 +1,117 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "token_setproc.h" +#include +#include +#include +#include +#include + + + + +#define ACCESS_TOKEN_ID_IOCTL_BASE 'A' + +enum { + GET_TOKEN_ID = 1, + SET_TOKEN_ID, + GET_FTOKEN_ID, + SET_FTOKEN_ID, + ACCESS_TOKENID_MAX_NR, +}; + +#define ACCESS_TOKENID_GET_TOKENID \ + _IOR(ACCESS_TOKEN_ID_IOCTL_BASE,GET_TOKEN_ID,unsigned long long) +#define ACCESS_TOKENID_SET_TOKENID \ + _IOW(ACCESS_TOKEN_ID_IOCTL_BASE,SET_TOKEN_ID,unsigned long long) +#define ACCESS_TOKENID_GET_FTOKENID\ + _IOR(ACCESS_TOKEN_ID_IOCTL_BASE,GET_FTOKEN_ID,unsigned long long) +#define ACCESS_TOKENID_SET_FTOKENID \ + _IOW(ACCESS_TOKEN_ID_IOCTL_BASE,SET_FTOKEN_ID,unsigned long long) + +#define ACCESS_TOKEN_OK 0 +#define ACCESS_TOKEN_ERROR (-1) + +#define INVAL_TOKEN_ID 0x0 +#define TOKEN_ID_LOWMASK 0xffffffff + +#define TOKENID_DEVNODE "/dev/access_token_id" + +uint64_t GetSelfTokenID() +{ + uint64_t token = INVAL_TOKEN_ID; + int fd = open(TOKENID_DEVNODE,O_RDWR); + if (fd < 0) { + return INVAL_TOKEN_ID; + } + int ret =ioctl(fd,ACCESS_TOKENID_GET_TOKENID,&token); + if (ret) { + close(fd); + return INVAL_TOKEN_ID; + } + + close(fd); + return token; +} + +int SetSelfTokenID(uint64_t tokenID) +{ + int fd = open(TOKENID_DEVNODE,O_RDWR); + if (fd < 0) { + return ACCESS_TOKEN_ERROR; + } + int ret = ioctl(fd,ACCESS_TOKENID_SET_TOKENID,&tokenID); + if (ret) { + close(fd); + return ACCESS_TOKEN_ERROR; + } + + close(fd); + return ACCESS_TOKEN_OK; +} + +uint64_t GetFirstCallerTokenID() +{ + uint64_t token = INVAL_TOKEN_ID; + int fd = open(TOKENID_DEVNODE,O_RDWR); + if (fd < 0) { + return INVAL_TOKEN_ID; + } + int ret = ioctl(fd,ACCESS_TOKENID_GET_FTOKENID,&token); + if (ret) { + close(fd); + return INVAL_TOKEN_ID; + } + + close(fd); + return token; +} + +int SetFirstCallerTokenID(uint64_t tokenID) +{ + int fd = open(TOKENID_DEVNODE,O_RDWR); + if (fd < 0) { + return ACCESS_TOKEN_ERROR; + } + int ret = ioctl(fd,ACCESS_TOKENID_SET_FTOKENID,&tokenID); + if (ret) { + close(fd); + return ACCESS_TOKEN_ERROR; + } + + close(fd); + return ACCESS_TOKEN_OK; +} \ No newline at end of file diff --git a/interfaces/innerkits/token_setproc/test/.keep b/interfaces/innerkits/token_setproc/test/.keep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/interfaces/innerkits/token_setproc/test/BUILD.gn b/interfaces/innerkits/token_setproc/test/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..50c911d848673594261e4a10197c0083a9b59a25 --- /dev/null +++ b/interfaces/innerkits/token_setproc/test/BUILD.gn @@ -0,0 +1,39 @@ +# Copyright (C) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/test.gni") + +ohos_unittest("libtoken_setproc_test") { + subsystem_name = "security" + part_name = "access_token" + module_out_path = part_name + "/" + part_name + + include_dirs = [ + "//base/security/access_token/interfaces/innerkits/token_setproc/include" + ] + + sources = [ "unittest/cpp/src/tokensetproc_kit_test.cpp" ] + + cflags_cc = [ "-fexceptions" ] + + deps = [ + "//third_party/googletest:gmock", + "//third_party/googletest:gtest", + "//base/security/access_token/interfaces/innerkits/token_setproc:libtoken_setproc", + ] +} + +group("unittest") { + testonly = true + deps = [ ":libtoken_setproc_test" ] +} \ No newline at end of file diff --git a/interfaces/innerkits/token_setproc/test/unittest/.keep b/interfaces/innerkits/token_setproc/test/unittest/.keep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/interfaces/innerkits/token_setproc/test/unittest/cpp/.keep b/interfaces/innerkits/token_setproc/test/unittest/cpp/.keep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/interfaces/innerkits/token_setproc/test/unittest/cpp/src/.keep b/interfaces/innerkits/token_setproc/test/unittest/cpp/src/.keep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/interfaces/innerkits/token_setproc/test/unittest/cpp/src/tokensetproc_kit_test.cpp b/interfaces/innerkits/token_setproc/test/unittest/cpp/src/tokensetproc_kit_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4463b0954af40fe887bfef94071d4a06dfa22280 --- /dev/null +++ b/interfaces/innerkits/token_setproc/test/unittest/cpp/src/tokensetproc_kit_test.cpp @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "tokensetproc_kit_test.h" + +#include "token_setproc.h" + +using namespace testing::ext; +using namespace OHOS::Security; + +void TokensetprocKitTest::SetUpTestCase() +{} + +void TokensetprocKitTest::TearDownTestCase() +{} + +void TokensetprocKitTest::SetUp() +{} + +void TokensetprocKitTest::TearDown() +{} \ No newline at end of file diff --git a/interfaces/innerkits/token_setproc/test/unittest/cpp/src/tokensetproc_kit_test.h b/interfaces/innerkits/token_setproc/test/unittest/cpp/src/tokensetproc_kit_test.h new file mode 100644 index 0000000000000000000000000000000000000000..63a7cd567a3be1fd152769d5d3d859cba8abcef3 --- /dev/null +++ b/interfaces/innerkits/token_setproc/test/unittest/cpp/src/tokensetproc_kit_test.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef TOKENSYNC_KIT_TEST_H +#define TOKENSYNC_KIT_TEST_H + +#include + +namespace OHOS { +namespace Security { +class TokensetprocKitTest : public testing::Test { +public: + static void SetUpTestCase(); + + static void TearDownTestCase(); + + void SetUp(); + + void TearDown(); +}; +} //namespace Security +} //namespace OHOS +#endif \ No newline at end of file