From: Aleksander Zdyb Date: Tue, 18 Nov 2014 10:07:05 +0000 (+0100) Subject: Add PasswdAccess X-Git-Tag: security-manager_5.5_testing~140 X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Ftest%2Fsecurity-tests.git;a=commitdiff_plain;h=bb01853c3460b66187112962ed9d710a01e02c47 Add PasswdAccess A wrapper on getpwnam() and getgrnam(). Change-Id: Ib833b6c1922ad45de3f713f29a904283d094600f --- diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 49c2d9f..8c35483 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -21,6 +21,7 @@ SET(COMMON_TARGET_TEST_SOURCES ${PROJECT_SOURCE_DIR}/src/common/memory.cpp ${PROJECT_SOURCE_DIR}/src/common/db_sqlite.cpp ${PROJECT_SOURCE_DIR}/src/common/fs_label_manager.cpp + ${PROJECT_SOURCE_DIR}/src/common/passwd_access.cpp ) #system and local includes diff --git a/src/common/passwd_access.cpp b/src/common/passwd_access.cpp new file mode 100644 index 0000000..6f55549 --- /dev/null +++ b/src/common/passwd_access.cpp @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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. + */ +/** + * @file passwd_access.cpp + * @author Aleksander Zdyb + * @version 1.0 + * @brief Provides access to UID and GID + */ + +#include +#include +#include + +#include + +#include "passwd_access.h" + +namespace PasswdAccess { + uid_t uid(const std::string &username) { + struct passwd *passwd = nullptr; + do { + errno = 0; + passwd = getpwnam(username.c_str()); + } while (passwd == nullptr && errno == EINTR); + RUNNER_ASSERT_ERRNO_MSG(passwd != nullptr, "Error in getpwnam(). Username: " << username); + return passwd->pw_uid; + } + + gid_t gid(const std::string &groupname) { + struct group *group = nullptr; + do { + errno = 0; + group = getgrnam(groupname.c_str()); + } while (group == nullptr && errno == EINTR); + RUNNER_ASSERT_ERRNO_MSG(group != nullptr, "Error in getgrnam(). Groupname: " << groupname); + return group->gr_gid; + } +} // namespace PasswdAccess diff --git a/src/common/passwd_access.h b/src/common/passwd_access.h new file mode 100644 index 0000000..3638828 --- /dev/null +++ b/src/common/passwd_access.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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. + */ +/** + * @file passwd_access.h + * @author Aleksander Zdyb + * @version 1.0 + * @brief Provides access to UID and GID + */ + +#ifndef TESTS_COMMON_PASSWD_ACCESS_H_ +#define TESTS_COMMON_PASSWD_ACCESS_H_ + +#include +#include + +namespace PasswdAccess { + uid_t uid(const std::string &username); + gid_t gid(const std::string &groupname); +} // namespace PasswdAccess + +#endif // TESTS_COMMON_PASSWD_ACCESS_H_