blob: ae8cd982e94bbe63ece2d365d248419cf5a8054b [file] [log] [blame]
Tom Cherry6034ef82018-02-02 16:10:07 -08001/*
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
29#ifndef GRP_PWD_FILE_H
30#define GRP_PWD_FILE_H
31
32#include <grp.h>
33#include <pwd.h>
34
35#include "private/bionic_lock.h"
36#include "private/bionic_macros.h"
37#include "private/grp_pwd.h"
38
39class MmapFile {
40 public:
41 MmapFile(const char* filename);
42 ~MmapFile();
43
44 template <typename Line>
45 bool FindById(uid_t uid, Line* line);
46 template <typename Line>
47 bool FindByName(const char* name, Line* line);
48
49 DISALLOW_COPY_AND_ASSIGN(MmapFile);
50
51 private:
52 enum class FileStatus {
53 Uninitialized,
54 Initialized,
55 Error,
56 };
57
58 bool GetFile(const char** start, const char** end);
59 bool DoMmap();
60
61 template <typename Line, typename Predicate>
62 bool Find(Line* line, Predicate predicate);
63
64 FileStatus status_ = FileStatus::Uninitialized;
65 Lock lock_;
66 const char* filename_ = nullptr;
67 const char* start_ = nullptr;
68 const char* end_ = nullptr;
69};
70
71class PasswdFile {
72 public:
73 PasswdFile(const char* filename);
74
75 bool FindById(uid_t id, passwd_state_t* passwd_state);
76 bool FindByName(const char* name, passwd_state_t* passwd_state);
77
78 DISALLOW_COPY_AND_ASSIGN(PasswdFile);
79
80 private:
81 MmapFile mmap_file_;
82};
83
84class GroupFile {
85 public:
86 GroupFile(const char* filename);
87
88 bool FindById(gid_t id, group_state_t* group_state);
89 bool FindByName(const char* name, group_state_t* group_state);
90
91 DISALLOW_COPY_AND_ASSIGN(GroupFile);
92
93 private:
94 MmapFile mmap_file_;
95};
96
97#endif