Merge "Add ability to read /etc/passwd and /etc/group"
am: ab8c861597
Change-Id: I05b9b2559dbf30f2dedfa722584433b95d1215a1
This commit is contained in:
commit
69bd70122a
7 changed files with 653 additions and 2 deletions
|
@ -1278,6 +1278,7 @@ cc_library_static {
|
||||||
"bionic/getpriority.cpp",
|
"bionic/getpriority.cpp",
|
||||||
"bionic/gettid.cpp",
|
"bionic/gettid.cpp",
|
||||||
"bionic/grp_pwd.cpp",
|
"bionic/grp_pwd.cpp",
|
||||||
|
"bionic/grp_pwd_file.cpp",
|
||||||
"bionic/iconv.cpp",
|
"bionic/iconv.cpp",
|
||||||
"bionic/icu_wrappers.cpp",
|
"bionic/icu_wrappers.cpp",
|
||||||
"bionic/ifaddrs.cpp",
|
"bionic/ifaddrs.cpp",
|
||||||
|
|
|
@ -44,6 +44,10 @@
|
||||||
|
|
||||||
// Generated android_ids array
|
// Generated android_ids array
|
||||||
#include "generated_android_ids.h"
|
#include "generated_android_ids.h"
|
||||||
|
#include "grp_pwd_file.h"
|
||||||
|
|
||||||
|
static PasswdFile vendor_passwd("/vendor/etc/passwd");
|
||||||
|
static GroupFile vendor_group("/vendor/etc/group");
|
||||||
|
|
||||||
// POSIX seems to envisage an implementation where the <pwd.h> functions are
|
// POSIX seems to envisage an implementation where the <pwd.h> functions are
|
||||||
// implemented by brute-force searching with getpwent(3), and the <grp.h>
|
// implemented by brute-force searching with getpwent(3), and the <grp.h>
|
||||||
|
@ -422,7 +426,11 @@ static id_t oem_id_from_name(const char* name) {
|
||||||
|
|
||||||
static passwd* oem_id_to_passwd(uid_t uid, passwd_state_t* state) {
|
static passwd* oem_id_to_passwd(uid_t uid, passwd_state_t* state) {
|
||||||
if (!is_oem_id(uid)) {
|
if (!is_oem_id(uid)) {
|
||||||
return NULL;
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (vendor_passwd.FindById(uid, state)) {
|
||||||
|
return &state->passwd_;
|
||||||
}
|
}
|
||||||
|
|
||||||
snprintf(state->name_buffer_, sizeof(state->name_buffer_), "oem_%u", uid);
|
snprintf(state->name_buffer_, sizeof(state->name_buffer_), "oem_%u", uid);
|
||||||
|
@ -440,7 +448,11 @@ static passwd* oem_id_to_passwd(uid_t uid, passwd_state_t* state) {
|
||||||
|
|
||||||
static group* oem_id_to_group(gid_t gid, group_state_t* state) {
|
static group* oem_id_to_group(gid_t gid, group_state_t* state) {
|
||||||
if (!is_oem_id(gid)) {
|
if (!is_oem_id(gid)) {
|
||||||
return NULL;
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (vendor_group.FindById(gid, state)) {
|
||||||
|
return &state->group_;
|
||||||
}
|
}
|
||||||
|
|
||||||
snprintf(state->group_name_buffer_, sizeof(state->group_name_buffer_),
|
snprintf(state->group_name_buffer_, sizeof(state->group_name_buffer_),
|
||||||
|
@ -530,6 +542,13 @@ passwd* getpwnam(const char* login) { // NOLINT: implementing bad function.
|
||||||
if (pw != NULL) {
|
if (pw != NULL) {
|
||||||
return pw;
|
return pw;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (vendor_passwd.FindByName(login, state)) {
|
||||||
|
if (is_oem_id(state->passwd_.pw_uid)) {
|
||||||
|
return &state->passwd_;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Handle OEM range.
|
// Handle OEM range.
|
||||||
pw = oem_id_to_passwd(oem_id_from_name(login), state);
|
pw = oem_id_to_passwd(oem_id_from_name(login), state);
|
||||||
if (pw != NULL) {
|
if (pw != NULL) {
|
||||||
|
@ -640,6 +659,13 @@ static group* getgrnam_internal(const char* name, group_state_t* state) {
|
||||||
if (grp != NULL) {
|
if (grp != NULL) {
|
||||||
return grp;
|
return grp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (vendor_group.FindByName(name, state)) {
|
||||||
|
if (is_oem_id(state->group_.gr_gid)) {
|
||||||
|
return &state->group_;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Handle OEM range.
|
// Handle OEM range.
|
||||||
grp = oem_id_to_group(oem_id_from_name(name), state);
|
grp = oem_id_to_group(oem_id_from_name(name), state);
|
||||||
if (grp != NULL) {
|
if (grp != NULL) {
|
||||||
|
|
326
libc/bionic/grp_pwd_file.cpp
Normal file
326
libc/bionic/grp_pwd_file.cpp
Normal file
|
@ -0,0 +1,326 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2018 The Android Open Source Project
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* * Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in
|
||||||
|
* the documentation and/or other materials provided with the
|
||||||
|
* distribution.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||||
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||||
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||||
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||||
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||||
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||||
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||||
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
|
* SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "grp_pwd_file.h"
|
||||||
|
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <sys/mman.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
|
#include "private/ErrnoRestorer.h"
|
||||||
|
|
||||||
|
// This file mmap's /*/etc/passwd and /*/etc/group in order to return their contents without any
|
||||||
|
// allocations. Note that these files and the strings contained within them are explicitly not
|
||||||
|
// null-terminated. ':'s are used to deliminate fields and '\n's are used to deliminate lines.
|
||||||
|
// There is a check that the file ends with '\n', such that terminating loops at '\n' ensures that
|
||||||
|
// memory will be not ready before the mmap region.
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
void CopyFieldToString(char* dest, const char* source, size_t max) {
|
||||||
|
while (*source != ':' && *source != '\n' && max > 1) {
|
||||||
|
*dest++ = *source++;
|
||||||
|
--max;
|
||||||
|
}
|
||||||
|
*dest = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
bool FieldToUid(const char* field, uid_t* uid) {
|
||||||
|
if (field == nullptr) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* end = nullptr;
|
||||||
|
errno = 0;
|
||||||
|
uid_t result = strtoul(field, &end, 0);
|
||||||
|
if (errno != 0 || field == end || *end != ':') {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
*uid = result;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns a pointer to one past the end of line.
|
||||||
|
const char* ParseLine(const char* begin, const char* end, const char** fields, size_t num_fields) {
|
||||||
|
size_t fields_written = 0;
|
||||||
|
const char* position = begin;
|
||||||
|
fields[fields_written++] = position;
|
||||||
|
|
||||||
|
while (position < end && fields_written < num_fields) {
|
||||||
|
if (*position == '\n') {
|
||||||
|
return position + 1;
|
||||||
|
}
|
||||||
|
if (*position == ':') {
|
||||||
|
fields[fields_written++] = position + 1;
|
||||||
|
}
|
||||||
|
position++;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (position < end && *position != '\n') {
|
||||||
|
position++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return position + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct PasswdLine {
|
||||||
|
const char* name() const {
|
||||||
|
return fields[0];
|
||||||
|
}
|
||||||
|
// Password is not supported.
|
||||||
|
const char* uid() const {
|
||||||
|
return fields[2];
|
||||||
|
}
|
||||||
|
const char* gid() const {
|
||||||
|
return fields[3];
|
||||||
|
}
|
||||||
|
// User Info is not supported
|
||||||
|
const char* dir() const {
|
||||||
|
return fields[5];
|
||||||
|
}
|
||||||
|
const char* shell() const {
|
||||||
|
return fields[6];
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ToPasswdState(passwd_state_t* passwd_state) {
|
||||||
|
if (name() == nullptr || dir() == nullptr || shell() == nullptr) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
uid_t uid;
|
||||||
|
if (!FieldToUid(this->uid(), &uid)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
gid_t gid;
|
||||||
|
if (!FieldToUid(this->gid(), &gid)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
passwd_state->passwd_.pw_uid = uid;
|
||||||
|
passwd_state->passwd_.pw_gid = gid;
|
||||||
|
|
||||||
|
CopyFieldToString(passwd_state->name_buffer_, name(), sizeof(passwd_state->name_buffer_));
|
||||||
|
passwd_state->passwd_.pw_name = passwd_state->name_buffer_;
|
||||||
|
|
||||||
|
passwd_state->passwd_.pw_passwd = nullptr;
|
||||||
|
|
||||||
|
#ifdef __LP64__
|
||||||
|
passwd_state->passwd_.pw_gecos = nullptr;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
CopyFieldToString(passwd_state->dir_buffer_, dir(), sizeof(passwd_state->dir_buffer_));
|
||||||
|
passwd_state->passwd_.pw_dir = passwd_state->dir_buffer_;
|
||||||
|
|
||||||
|
CopyFieldToString(passwd_state->sh_buffer_, shell(), sizeof(passwd_state->sh_buffer_));
|
||||||
|
passwd_state->passwd_.pw_shell = passwd_state->sh_buffer_;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static constexpr size_t kNumFields = 7;
|
||||||
|
const char* fields[kNumFields] = {};
|
||||||
|
};
|
||||||
|
|
||||||
|
struct GroupLine {
|
||||||
|
const char* name() const {
|
||||||
|
return fields[0];
|
||||||
|
}
|
||||||
|
// Password is not supported.
|
||||||
|
const char* gid() const {
|
||||||
|
return fields[2];
|
||||||
|
}
|
||||||
|
// User list is not supported (returns simply name)
|
||||||
|
|
||||||
|
bool ToGroupState(group_state_t* group_state) {
|
||||||
|
if (name() == nullptr || gid() == nullptr) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
gid_t gid;
|
||||||
|
if (!FieldToUid(this->gid(), &gid)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
group_state->group_.gr_gid = gid;
|
||||||
|
|
||||||
|
CopyFieldToString(group_state->group_name_buffer_, name(),
|
||||||
|
sizeof(group_state->group_name_buffer_));
|
||||||
|
group_state->group_.gr_name = group_state->group_name_buffer_;
|
||||||
|
|
||||||
|
group_state->group_.gr_passwd = nullptr;
|
||||||
|
|
||||||
|
group_state->group_.gr_mem = group_state->group_members_;
|
||||||
|
group_state->group_.gr_mem[0] = group_state->group_.gr_name;
|
||||||
|
group_state->group_.gr_mem[1] = nullptr;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static constexpr size_t kNumFields = 4;
|
||||||
|
const char* fields[kNumFields] = {};
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
MmapFile::MmapFile(const char* filename) : filename_(filename) {
|
||||||
|
lock_.init(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
MmapFile::~MmapFile() {
|
||||||
|
if (status_ == FileStatus::Initialized) {
|
||||||
|
size_t size = end_ - start_ + 1;
|
||||||
|
munmap(const_cast<char*>(start_), size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool MmapFile::GetFile(const char** start, const char** end) {
|
||||||
|
LockGuard guard(lock_);
|
||||||
|
if (status_ == FileStatus::Initialized) {
|
||||||
|
*start = start_;
|
||||||
|
*end = end_;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (status_ == FileStatus::Error) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!DoMmap()) {
|
||||||
|
status_ = FileStatus::Error;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
status_ = FileStatus::Initialized;
|
||||||
|
*start = start_;
|
||||||
|
*end = end_;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool MmapFile::DoMmap() {
|
||||||
|
int fd = open(filename_, O_CLOEXEC | O_NOFOLLOW | O_RDONLY);
|
||||||
|
|
||||||
|
struct stat fd_stat;
|
||||||
|
if (fstat(fd, &fd_stat) == -1) {
|
||||||
|
close(fd);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto mmap_size = fd_stat.st_size;
|
||||||
|
|
||||||
|
const void* map_result = mmap(nullptr, mmap_size, PROT_READ, MAP_SHARED, fd, 0);
|
||||||
|
close(fd);
|
||||||
|
|
||||||
|
if (map_result == MAP_FAILED) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
start_ = static_cast<const char*>(map_result);
|
||||||
|
end_ = start_ + mmap_size - 1;
|
||||||
|
|
||||||
|
return *end_ == '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Line, typename Predicate>
|
||||||
|
bool MmapFile::Find(Line* line, Predicate predicate) {
|
||||||
|
const char* start;
|
||||||
|
const char* end;
|
||||||
|
if (!GetFile(&start, &end)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* line_beginning = start;
|
||||||
|
|
||||||
|
while (line_beginning < end) {
|
||||||
|
line_beginning = ParseLine(line_beginning, end, line->fields, line->kNumFields);
|
||||||
|
if (predicate(line)) return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Line>
|
||||||
|
bool MmapFile::FindById(uid_t uid, Line* line) {
|
||||||
|
return Find(line, [uid](const auto& line) {
|
||||||
|
uid_t line_id;
|
||||||
|
if (!FieldToUid(line->fields[2], &line_id)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return line_id == uid;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Line>
|
||||||
|
bool MmapFile::FindByName(const char* name, Line* line) {
|
||||||
|
return Find(line, [name](const auto& line) {
|
||||||
|
const char* line_name = line->fields[0];
|
||||||
|
if (line_name == nullptr) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* match_name = name;
|
||||||
|
while (*line_name != '\n' && *line_name != ':' && *match_name != '\0') {
|
||||||
|
if (*line_name++ != *match_name++) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return *line_name == ':' && *match_name == '\0';
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
PasswdFile::PasswdFile(const char* filename) : mmap_file_(filename) {
|
||||||
|
}
|
||||||
|
|
||||||
|
bool PasswdFile::FindById(uid_t id, passwd_state_t* passwd_state) {
|
||||||
|
ErrnoRestorer errno_restorer;
|
||||||
|
PasswdLine passwd_line;
|
||||||
|
return mmap_file_.FindById(id, &passwd_line) && passwd_line.ToPasswdState(passwd_state);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool PasswdFile::FindByName(const char* name, passwd_state_t* passwd_state) {
|
||||||
|
ErrnoRestorer errno_restorer;
|
||||||
|
PasswdLine passwd_line;
|
||||||
|
return mmap_file_.FindByName(name, &passwd_line) && passwd_line.ToPasswdState(passwd_state);
|
||||||
|
}
|
||||||
|
|
||||||
|
GroupFile::GroupFile(const char* filename) : mmap_file_(filename) {
|
||||||
|
}
|
||||||
|
|
||||||
|
bool GroupFile::FindById(gid_t id, group_state_t* group_state) {
|
||||||
|
ErrnoRestorer errno_restorer;
|
||||||
|
GroupLine group_line;
|
||||||
|
return mmap_file_.FindById(id, &group_line) && group_line.ToGroupState(group_state);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool GroupFile::FindByName(const char* name, group_state_t* group_state) {
|
||||||
|
ErrnoRestorer errno_restorer;
|
||||||
|
GroupLine group_line;
|
||||||
|
return mmap_file_.FindByName(name, &group_line) && group_line.ToGroupState(group_state);
|
||||||
|
}
|
97
libc/bionic/grp_pwd_file.h
Normal file
97
libc/bionic/grp_pwd_file.h
Normal file
|
@ -0,0 +1,97 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2018 The Android Open Source Project
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* * Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in
|
||||||
|
* the documentation and/or other materials provided with the
|
||||||
|
* distribution.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||||
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||||
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||||
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||||
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||||
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||||
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||||
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
|
* SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef GRP_PWD_FILE_H
|
||||||
|
#define GRP_PWD_FILE_H
|
||||||
|
|
||||||
|
#include <grp.h>
|
||||||
|
#include <pwd.h>
|
||||||
|
|
||||||
|
#include "private/bionic_lock.h"
|
||||||
|
#include "private/bionic_macros.h"
|
||||||
|
#include "private/grp_pwd.h"
|
||||||
|
|
||||||
|
class MmapFile {
|
||||||
|
public:
|
||||||
|
MmapFile(const char* filename);
|
||||||
|
~MmapFile();
|
||||||
|
|
||||||
|
template <typename Line>
|
||||||
|
bool FindById(uid_t uid, Line* line);
|
||||||
|
template <typename Line>
|
||||||
|
bool FindByName(const char* name, Line* line);
|
||||||
|
|
||||||
|
DISALLOW_COPY_AND_ASSIGN(MmapFile);
|
||||||
|
|
||||||
|
private:
|
||||||
|
enum class FileStatus {
|
||||||
|
Uninitialized,
|
||||||
|
Initialized,
|
||||||
|
Error,
|
||||||
|
};
|
||||||
|
|
||||||
|
bool GetFile(const char** start, const char** end);
|
||||||
|
bool DoMmap();
|
||||||
|
|
||||||
|
template <typename Line, typename Predicate>
|
||||||
|
bool Find(Line* line, Predicate predicate);
|
||||||
|
|
||||||
|
FileStatus status_ = FileStatus::Uninitialized;
|
||||||
|
Lock lock_;
|
||||||
|
const char* filename_ = nullptr;
|
||||||
|
const char* start_ = nullptr;
|
||||||
|
const char* end_ = nullptr;
|
||||||
|
};
|
||||||
|
|
||||||
|
class PasswdFile {
|
||||||
|
public:
|
||||||
|
PasswdFile(const char* filename);
|
||||||
|
|
||||||
|
bool FindById(uid_t id, passwd_state_t* passwd_state);
|
||||||
|
bool FindByName(const char* name, passwd_state_t* passwd_state);
|
||||||
|
|
||||||
|
DISALLOW_COPY_AND_ASSIGN(PasswdFile);
|
||||||
|
|
||||||
|
private:
|
||||||
|
MmapFile mmap_file_;
|
||||||
|
};
|
||||||
|
|
||||||
|
class GroupFile {
|
||||||
|
public:
|
||||||
|
GroupFile(const char* filename);
|
||||||
|
|
||||||
|
bool FindById(gid_t id, group_state_t* group_state);
|
||||||
|
bool FindByName(const char* name, group_state_t* group_state);
|
||||||
|
|
||||||
|
DISALLOW_COPY_AND_ASSIGN(GroupFile);
|
||||||
|
|
||||||
|
private:
|
||||||
|
MmapFile mmap_file_;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
|
@ -76,4 +76,19 @@ class Lock {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class LockGuard {
|
||||||
|
public:
|
||||||
|
LockGuard(Lock& lock) : lock_(lock) {
|
||||||
|
lock_.lock();
|
||||||
|
}
|
||||||
|
~LockGuard() {
|
||||||
|
lock_.unlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
DISALLOW_COPY_AND_ASSIGN(LockGuard);
|
||||||
|
|
||||||
|
private:
|
||||||
|
Lock& lock_;
|
||||||
|
};
|
||||||
|
|
||||||
#endif // _BIONIC_LOCK_H
|
#endif // _BIONIC_LOCK_H
|
||||||
|
|
|
@ -75,6 +75,7 @@ cc_test_library {
|
||||||
"getcwd_test.cpp",
|
"getcwd_test.cpp",
|
||||||
"glob_test.cpp",
|
"glob_test.cpp",
|
||||||
"grp_pwd_test.cpp",
|
"grp_pwd_test.cpp",
|
||||||
|
"grp_pwd_file_test.cpp",
|
||||||
"iconv_test.cpp",
|
"iconv_test.cpp",
|
||||||
"ifaddrs_test.cpp",
|
"ifaddrs_test.cpp",
|
||||||
"inttypes_test.cpp",
|
"inttypes_test.cpp",
|
||||||
|
|
185
tests/grp_pwd_file_test.cpp
Normal file
185
tests/grp_pwd_file_test.cpp
Normal file
|
@ -0,0 +1,185 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2018 The Android Open Source Project
|
||||||
|
*
|
||||||
|
* 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 <unistd.h>
|
||||||
|
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
|
#include "TemporaryFile.h"
|
||||||
|
|
||||||
|
#if defined(__BIONIC__)
|
||||||
|
#include "../libc/bionic/grp_pwd_file.cpp"
|
||||||
|
|
||||||
|
void FindAndCheckPasswdEntry(PasswdFile* file, const char* name, uid_t uid, gid_t gid,
|
||||||
|
const char* dir, const char* shell) {
|
||||||
|
passwd_state_t name_passwd_state;
|
||||||
|
ASSERT_TRUE(file->FindByName(name, &name_passwd_state)) << name;
|
||||||
|
|
||||||
|
passwd& name_passwd = name_passwd_state.passwd_;
|
||||||
|
EXPECT_STREQ(name, name_passwd.pw_name);
|
||||||
|
EXPECT_EQ(nullptr, name_passwd.pw_passwd);
|
||||||
|
EXPECT_EQ(uid, name_passwd.pw_uid);
|
||||||
|
EXPECT_EQ(gid, name_passwd.pw_gid);
|
||||||
|
EXPECT_EQ(nullptr, name_passwd.pw_gecos);
|
||||||
|
EXPECT_STREQ(dir, name_passwd.pw_dir);
|
||||||
|
EXPECT_STREQ(shell, name_passwd.pw_shell);
|
||||||
|
|
||||||
|
passwd_state_t id_passwd_state;
|
||||||
|
ASSERT_TRUE(file->FindById(uid, &id_passwd_state)) << uid;
|
||||||
|
|
||||||
|
passwd& id_passwd = id_passwd_state.passwd_;
|
||||||
|
EXPECT_STREQ(name, id_passwd.pw_name);
|
||||||
|
EXPECT_EQ(nullptr, id_passwd.pw_passwd);
|
||||||
|
EXPECT_EQ(uid, id_passwd.pw_uid);
|
||||||
|
EXPECT_EQ(gid, id_passwd.pw_gid);
|
||||||
|
EXPECT_EQ(nullptr, id_passwd.pw_gecos);
|
||||||
|
EXPECT_STREQ(dir, id_passwd.pw_dir);
|
||||||
|
EXPECT_STREQ(shell, id_passwd.pw_shell);
|
||||||
|
}
|
||||||
|
|
||||||
|
void FindAndCheckGroupEntry(GroupFile* file, const char* name, gid_t gid) {
|
||||||
|
group_state_t name_group_state;
|
||||||
|
ASSERT_TRUE(file->FindByName(name, &name_group_state)) << name;
|
||||||
|
|
||||||
|
group& name_group = name_group_state.group_;
|
||||||
|
EXPECT_STREQ(name, name_group.gr_name);
|
||||||
|
EXPECT_EQ(nullptr, name_group.gr_passwd);
|
||||||
|
EXPECT_EQ(gid, name_group.gr_gid);
|
||||||
|
EXPECT_EQ(name_group.gr_name, name_group.gr_mem[0]);
|
||||||
|
EXPECT_EQ(nullptr, name_group.gr_mem[1]);
|
||||||
|
|
||||||
|
group_state_t id_group_state;
|
||||||
|
ASSERT_TRUE(file->FindById(gid, &id_group_state)) << gid;
|
||||||
|
|
||||||
|
group& id_group = id_group_state.group_;
|
||||||
|
EXPECT_STREQ(name, id_group.gr_name);
|
||||||
|
EXPECT_EQ(nullptr, id_group.gr_passwd);
|
||||||
|
EXPECT_EQ(gid, id_group.gr_gid);
|
||||||
|
EXPECT_EQ(id_group.gr_name, id_group.gr_mem[0]);
|
||||||
|
EXPECT_EQ(nullptr, id_group.gr_mem[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // __BIONIC__
|
||||||
|
|
||||||
|
TEST(grp_pwd_file, passwd_file_one_entry) {
|
||||||
|
#if defined(__BIONIC__)
|
||||||
|
TemporaryFile file;
|
||||||
|
ASSERT_NE(-1, file.fd);
|
||||||
|
static const char test_string[] = "name:password:1:2:user_info:dir:shell\n";
|
||||||
|
write(file.fd, test_string, sizeof(test_string) - 1);
|
||||||
|
|
||||||
|
PasswdFile passwd_file(file.filename);
|
||||||
|
|
||||||
|
FindAndCheckPasswdEntry(&passwd_file, "name", 1, 2, "dir", "shell");
|
||||||
|
|
||||||
|
EXPECT_FALSE(passwd_file.FindByName("not_name", nullptr));
|
||||||
|
EXPECT_FALSE(passwd_file.FindById(3, nullptr));
|
||||||
|
|
||||||
|
#else // __BIONIC__
|
||||||
|
GTEST_LOG_(INFO) << "This test does nothing.\n";
|
||||||
|
#endif // __BIONIC__
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(grp_pwd_file, group_file_one_entry) {
|
||||||
|
#if defined(__BIONIC__)
|
||||||
|
TemporaryFile file;
|
||||||
|
ASSERT_NE(-1, file.fd);
|
||||||
|
static const char test_string[] = "name:password:1:one,two,three\n";
|
||||||
|
write(file.fd, test_string, sizeof(test_string) - 1);
|
||||||
|
|
||||||
|
GroupFile group_file(file.filename);
|
||||||
|
|
||||||
|
FindAndCheckGroupEntry(&group_file, "name", 1);
|
||||||
|
|
||||||
|
EXPECT_FALSE(group_file.FindByName("not_name", nullptr));
|
||||||
|
EXPECT_FALSE(group_file.FindById(3, nullptr));
|
||||||
|
|
||||||
|
#else // __BIONIC__
|
||||||
|
GTEST_LOG_(INFO) << "This test does nothing.\n";
|
||||||
|
#endif // __BIONIC__
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(grp_pwd_file, passwd_file_many_entries) {
|
||||||
|
#if defined(__BIONIC__)
|
||||||
|
TemporaryFile file;
|
||||||
|
ASSERT_NE(-1, file.fd);
|
||||||
|
static const char test_string[] =
|
||||||
|
"first:x:1:2::dir:shell\n"
|
||||||
|
"abc1::3:4::def:abc\n"
|
||||||
|
"abc2::5:4:abc::abc\n"
|
||||||
|
"abc3::7:4:abc:def:\n"
|
||||||
|
"abc4::9:4:::abc\n"
|
||||||
|
"abc5::11:4:abc:def:abc\n"
|
||||||
|
"middle-ish::13:4::/:/system/bin/sh\n"
|
||||||
|
"abc7::15:4:abc::\n"
|
||||||
|
"abc8::17:4:::\n"
|
||||||
|
"abc9::19:4:abc:def:abc\n"
|
||||||
|
"abc10::21:4:abc:def:abc\n"
|
||||||
|
"abc11::23:4:abc:def:abc\n"
|
||||||
|
"abc12::25:4:abc:def:abc\n"
|
||||||
|
"abc13::27:4:abc:def:abc\n"
|
||||||
|
"last::29:4::last_user_dir:last_user_shell\n";
|
||||||
|
|
||||||
|
write(file.fd, test_string, sizeof(test_string) - 1);
|
||||||
|
|
||||||
|
PasswdFile passwd_file(file.filename);
|
||||||
|
|
||||||
|
FindAndCheckPasswdEntry(&passwd_file, "first", 1, 2, "dir", "shell");
|
||||||
|
FindAndCheckPasswdEntry(&passwd_file, "middle-ish", 13, 4, "/", "/system/bin/sh");
|
||||||
|
FindAndCheckPasswdEntry(&passwd_file, "last", 29, 4, "last_user_dir", "last_user_shell");
|
||||||
|
|
||||||
|
EXPECT_FALSE(passwd_file.FindByName("not_name", nullptr));
|
||||||
|
EXPECT_FALSE(passwd_file.FindById(50, nullptr));
|
||||||
|
|
||||||
|
#else // __BIONIC__
|
||||||
|
GTEST_LOG_(INFO) << "This test does nothing.\n";
|
||||||
|
#endif // __BIONIC__
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(grp_pwd_file, group_file_many_entries) {
|
||||||
|
#if defined(__BIONIC__)
|
||||||
|
TemporaryFile file;
|
||||||
|
ASSERT_NE(-1, file.fd);
|
||||||
|
static const char test_string[] =
|
||||||
|
"first:password:1:one,two,three\n"
|
||||||
|
"abc:def:2:group1,group2,group3\n"
|
||||||
|
"abc:def:3:\n"
|
||||||
|
"abc:def:4:\n"
|
||||||
|
"abc:def:5:\n"
|
||||||
|
"middle-ish:def_a_password_that_is_over_32_characters_long:6:\n"
|
||||||
|
"abc:def:7:\n"
|
||||||
|
"abc:def:8:\n"
|
||||||
|
"abc:def:20:\n"
|
||||||
|
"abc:def:25:\n"
|
||||||
|
"abc:def:27:\n"
|
||||||
|
"abc:def:52:\n"
|
||||||
|
"last::800:\n";
|
||||||
|
|
||||||
|
write(file.fd, test_string, sizeof(test_string) - 1);
|
||||||
|
|
||||||
|
GroupFile group_file(file.filename);
|
||||||
|
|
||||||
|
FindAndCheckGroupEntry(&group_file, "first", 1);
|
||||||
|
FindAndCheckGroupEntry(&group_file, "middle-ish", 6);
|
||||||
|
FindAndCheckGroupEntry(&group_file, "last", 800);
|
||||||
|
|
||||||
|
EXPECT_FALSE(group_file.FindByName("not_name", nullptr));
|
||||||
|
EXPECT_FALSE(group_file.FindById(799, nullptr));
|
||||||
|
|
||||||
|
#else // __BIONIC__
|
||||||
|
GTEST_LOG_(INFO) << "This test does nothing.\n";
|
||||||
|
#endif // __BIONIC__
|
||||||
|
}
|
Loading…
Reference in a new issue