Tom Cherry | 6034ef8 | 2018-02-02 16:10:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
Elliott Hughes | cbc80ba | 2018-02-13 14:26:29 -0800 | [diff] [blame] | 29 | #pragma once |
Tom Cherry | 6034ef8 | 2018-02-02 16:10:07 -0800 | [diff] [blame] | 30 | |
| 31 | #include <grp.h> |
| 32 | #include <pwd.h> |
| 33 | |
| 34 | #include "private/bionic_lock.h" |
Josh Gao | 4956c37 | 2019-12-19 16:35:51 -0800 | [diff] [blame] | 35 | #include "platform/bionic/macros.h" |
Tom Cherry | 6034ef8 | 2018-02-02 16:10:07 -0800 | [diff] [blame] | 36 | #include "private/grp_pwd.h" |
| 37 | |
| 38 | class MmapFile { |
| 39 | public: |
Tom Cherry | c2b9fec | 2018-05-10 13:33:06 -0700 | [diff] [blame] | 40 | MmapFile(const char* filename, const char* required_prefix); |
Tom Cherry | 6034ef8 | 2018-02-02 16:10:07 -0800 | [diff] [blame] | 41 | |
| 42 | template <typename Line> |
| 43 | bool FindById(uid_t uid, Line* line); |
| 44 | template <typename Line> |
| 45 | bool FindByName(const char* name, Line* line); |
Tom Cherry | 5fe7326 | 2018-02-20 15:40:59 -0800 | [diff] [blame] | 46 | void Unmap(); |
Tom Cherry | 6034ef8 | 2018-02-02 16:10:07 -0800 | [diff] [blame] | 47 | |
Elliott Hughes | 5e62b34 | 2018-10-25 11:00:00 -0700 | [diff] [blame] | 48 | BIONIC_DISALLOW_IMPLICIT_CONSTRUCTORS(MmapFile); |
Tom Cherry | 6034ef8 | 2018-02-02 16:10:07 -0800 | [diff] [blame] | 49 | |
| 50 | private: |
| 51 | enum class FileStatus { |
| 52 | Uninitialized, |
| 53 | Initialized, |
| 54 | Error, |
| 55 | }; |
| 56 | |
| 57 | bool GetFile(const char** start, const char** end); |
| 58 | bool DoMmap(); |
| 59 | |
| 60 | template <typename Line, typename Predicate> |
| 61 | bool Find(Line* line, Predicate predicate); |
| 62 | |
| 63 | FileStatus status_ = FileStatus::Uninitialized; |
| 64 | Lock lock_; |
| 65 | const char* filename_ = nullptr; |
| 66 | const char* start_ = nullptr; |
| 67 | const char* end_ = nullptr; |
Jiyong Park | bf38328 | 2020-10-27 03:23:02 +0900 | [diff] [blame] | 68 | #pragma clang diagnostic push |
| 69 | #pragma clang diagnostic ignored "-Wunused-private-field" |
Tom Cherry | c2b9fec | 2018-05-10 13:33:06 -0700 | [diff] [blame] | 70 | const char* required_prefix_; |
Jiyong Park | bf38328 | 2020-10-27 03:23:02 +0900 | [diff] [blame] | 71 | #pragma clang diagnostic pop |
Tom Cherry | 6034ef8 | 2018-02-02 16:10:07 -0800 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | class PasswdFile { |
| 75 | public: |
Tom Cherry | c2b9fec | 2018-05-10 13:33:06 -0700 | [diff] [blame] | 76 | PasswdFile(const char* filename, const char* required_prefix); |
Tom Cherry | 6034ef8 | 2018-02-02 16:10:07 -0800 | [diff] [blame] | 77 | |
| 78 | bool FindById(uid_t id, passwd_state_t* passwd_state); |
| 79 | bool FindByName(const char* name, passwd_state_t* passwd_state); |
Tom Cherry | 5fe7326 | 2018-02-20 15:40:59 -0800 | [diff] [blame] | 80 | void Unmap() { |
| 81 | mmap_file_.Unmap(); |
| 82 | } |
Tom Cherry | 6034ef8 | 2018-02-02 16:10:07 -0800 | [diff] [blame] | 83 | |
Elliott Hughes | 5e62b34 | 2018-10-25 11:00:00 -0700 | [diff] [blame] | 84 | BIONIC_DISALLOW_IMPLICIT_CONSTRUCTORS(PasswdFile); |
Tom Cherry | 6034ef8 | 2018-02-02 16:10:07 -0800 | [diff] [blame] | 85 | |
| 86 | private: |
| 87 | MmapFile mmap_file_; |
| 88 | }; |
| 89 | |
| 90 | class GroupFile { |
| 91 | public: |
Tom Cherry | c2b9fec | 2018-05-10 13:33:06 -0700 | [diff] [blame] | 92 | GroupFile(const char* filename, const char* required_prefix); |
Tom Cherry | 6034ef8 | 2018-02-02 16:10:07 -0800 | [diff] [blame] | 93 | |
| 94 | bool FindById(gid_t id, group_state_t* group_state); |
| 95 | bool FindByName(const char* name, group_state_t* group_state); |
Tom Cherry | 5fe7326 | 2018-02-20 15:40:59 -0800 | [diff] [blame] | 96 | void Unmap() { |
| 97 | mmap_file_.Unmap(); |
| 98 | } |
Tom Cherry | 6034ef8 | 2018-02-02 16:10:07 -0800 | [diff] [blame] | 99 | |
Elliott Hughes | 5e62b34 | 2018-10-25 11:00:00 -0700 | [diff] [blame] | 100 | BIONIC_DISALLOW_IMPLICIT_CONSTRUCTORS(GroupFile); |
Tom Cherry | 6034ef8 | 2018-02-02 16:10:07 -0800 | [diff] [blame] | 101 | |
| 102 | private: |
| 103 | MmapFile mmap_file_; |
| 104 | }; |