blob: 4b8b8a99af93762b2da012bfa879b8d61322186c [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#include "grp_pwd_file.h"
30
31#include <fcntl.h>
32#include <stdlib.h>
33#include <sys/mman.h>
34#include <sys/stat.h>
35
36#include "private/ErrnoRestorer.h"
37
38// This file mmap's /*/etc/passwd and /*/etc/group in order to return their contents without any
39// allocations. Note that these files and the strings contained within them are explicitly not
40// null-terminated. ':'s are used to deliminate fields and '\n's are used to deliminate lines.
41// There is a check that the file ends with '\n', such that terminating loops at '\n' ensures that
42// memory will be not ready before the mmap region.
43
44namespace {
45
46void CopyFieldToString(char* dest, const char* source, size_t max) {
47 while (*source != ':' && *source != '\n' && max > 1) {
48 *dest++ = *source++;
49 --max;
50 }
51 *dest = '\0';
52}
53
54bool FieldToUid(const char* field, uid_t* uid) {
55 if (field == nullptr) {
56 return false;
57 }
58
59 char* end = nullptr;
60 errno = 0;
61 uid_t result = strtoul(field, &end, 0);
62 if (errno != 0 || field == end || *end != ':') {
63 return false;
64 }
65 *uid = result;
66 return true;
67}
68
69// Returns a pointer to one past the end of line.
70const char* ParseLine(const char* begin, const char* end, const char** fields, size_t num_fields) {
71 size_t fields_written = 0;
72 const char* position = begin;
73 fields[fields_written++] = position;
74
75 while (position < end && fields_written < num_fields) {
76 if (*position == '\n') {
77 return position + 1;
78 }
79 if (*position == ':') {
80 fields[fields_written++] = position + 1;
81 }
82 position++;
83 }
84
85 while (position < end && *position != '\n') {
86 position++;
87 }
88
89 return position + 1;
90}
91
92struct PasswdLine {
93 const char* name() const {
94 return fields[0];
95 }
96 // Password is not supported.
97 const char* uid() const {
98 return fields[2];
99 }
100 const char* gid() const {
101 return fields[3];
102 }
103 // User Info is not supported
104 const char* dir() const {
105 return fields[5];
106 }
107 const char* shell() const {
108 return fields[6];
109 }
110
111 bool ToPasswdState(passwd_state_t* passwd_state) {
112 if (name() == nullptr || dir() == nullptr || shell() == nullptr) {
113 return false;
114 }
115
116 uid_t uid;
117 if (!FieldToUid(this->uid(), &uid)) {
118 return false;
119 }
120
121 gid_t gid;
122 if (!FieldToUid(this->gid(), &gid)) {
123 return false;
124 }
125
126 passwd_state->passwd_.pw_uid = uid;
127 passwd_state->passwd_.pw_gid = gid;
128
129 CopyFieldToString(passwd_state->name_buffer_, name(), sizeof(passwd_state->name_buffer_));
130 passwd_state->passwd_.pw_name = passwd_state->name_buffer_;
131
132 passwd_state->passwd_.pw_passwd = nullptr;
133
134#ifdef __LP64__
135 passwd_state->passwd_.pw_gecos = nullptr;
136#endif
137
138 CopyFieldToString(passwd_state->dir_buffer_, dir(), sizeof(passwd_state->dir_buffer_));
139 passwd_state->passwd_.pw_dir = passwd_state->dir_buffer_;
140
141 CopyFieldToString(passwd_state->sh_buffer_, shell(), sizeof(passwd_state->sh_buffer_));
142 passwd_state->passwd_.pw_shell = passwd_state->sh_buffer_;
143
144 return true;
145 }
146
147 static constexpr size_t kNumFields = 7;
148 const char* fields[kNumFields] = {};
149};
150
151struct GroupLine {
152 const char* name() const {
153 return fields[0];
154 }
155 // Password is not supported.
156 const char* gid() const {
157 return fields[2];
158 }
159 // User list is not supported (returns simply name)
160
161 bool ToGroupState(group_state_t* group_state) {
162 if (name() == nullptr || gid() == nullptr) {
163 return false;
164 }
165
166 gid_t gid;
167 if (!FieldToUid(this->gid(), &gid)) {
168 return false;
169 }
170
171 group_state->group_.gr_gid = gid;
172
173 CopyFieldToString(group_state->group_name_buffer_, name(),
174 sizeof(group_state->group_name_buffer_));
175 group_state->group_.gr_name = group_state->group_name_buffer_;
176
177 group_state->group_.gr_passwd = nullptr;
178
179 group_state->group_.gr_mem = group_state->group_members_;
180 group_state->group_.gr_mem[0] = group_state->group_.gr_name;
181 group_state->group_.gr_mem[1] = nullptr;
182
183 return true;
184 }
185
186 static constexpr size_t kNumFields = 4;
187 const char* fields[kNumFields] = {};
188};
189
190} // namespace
191
192MmapFile::MmapFile(const char* filename) : filename_(filename) {
193 lock_.init(false);
194}
195
196MmapFile::~MmapFile() {
197 if (status_ == FileStatus::Initialized) {
198 size_t size = end_ - start_ + 1;
199 munmap(const_cast<char*>(start_), size);
200 }
201}
202
203bool MmapFile::GetFile(const char** start, const char** end) {
204 LockGuard guard(lock_);
205 if (status_ == FileStatus::Initialized) {
206 *start = start_;
207 *end = end_;
208 return true;
209 }
210 if (status_ == FileStatus::Error) {
211 return false;
212 }
213
214 if (!DoMmap()) {
215 status_ = FileStatus::Error;
216 return false;
217 }
218
219 status_ = FileStatus::Initialized;
220 *start = start_;
221 *end = end_;
222 return true;
223}
224
225bool MmapFile::DoMmap() {
226 int fd = open(filename_, O_CLOEXEC | O_NOFOLLOW | O_RDONLY);
227
228 struct stat fd_stat;
229 if (fstat(fd, &fd_stat) == -1) {
230 close(fd);
231 return false;
232 }
233
234 auto mmap_size = fd_stat.st_size;
235
Tom Cherrycb4d4212018-02-20 15:50:04 -0800236 void* map_result = mmap(nullptr, mmap_size, PROT_READ, MAP_SHARED, fd, 0);
Tom Cherry6034ef82018-02-02 16:10:07 -0800237 close(fd);
238
239 if (map_result == MAP_FAILED) {
240 return false;
241 }
242
243 start_ = static_cast<const char*>(map_result);
244 end_ = start_ + mmap_size - 1;
245
Tom Cherrycb4d4212018-02-20 15:50:04 -0800246 if (*end_ != '\n') {
247 munmap(map_result, mmap_size);
248 return false;
249 }
250
251 return true;
Tom Cherry6034ef82018-02-02 16:10:07 -0800252}
253
254template <typename Line, typename Predicate>
255bool MmapFile::Find(Line* line, Predicate predicate) {
256 const char* start;
257 const char* end;
258 if (!GetFile(&start, &end)) {
259 return false;
260 }
261
262 const char* line_beginning = start;
263
264 while (line_beginning < end) {
265 line_beginning = ParseLine(line_beginning, end, line->fields, line->kNumFields);
266 if (predicate(line)) return true;
267 }
268
269 return false;
270}
271
272template <typename Line>
273bool MmapFile::FindById(uid_t uid, Line* line) {
274 return Find(line, [uid](const auto& line) {
275 uid_t line_id;
276 if (!FieldToUid(line->fields[2], &line_id)) {
277 return false;
278 }
279
280 return line_id == uid;
281 });
282}
283
284template <typename Line>
285bool MmapFile::FindByName(const char* name, Line* line) {
286 return Find(line, [name](const auto& line) {
287 const char* line_name = line->fields[0];
288 if (line_name == nullptr) {
289 return false;
290 }
291
292 const char* match_name = name;
293 while (*line_name != '\n' && *line_name != ':' && *match_name != '\0') {
294 if (*line_name++ != *match_name++) {
295 return false;
296 }
297 }
298
299 return *line_name == ':' && *match_name == '\0';
300 });
301}
302
303PasswdFile::PasswdFile(const char* filename) : mmap_file_(filename) {
304}
305
306bool PasswdFile::FindById(uid_t id, passwd_state_t* passwd_state) {
307 ErrnoRestorer errno_restorer;
308 PasswdLine passwd_line;
309 return mmap_file_.FindById(id, &passwd_line) && passwd_line.ToPasswdState(passwd_state);
310}
311
312bool PasswdFile::FindByName(const char* name, passwd_state_t* passwd_state) {
313 ErrnoRestorer errno_restorer;
314 PasswdLine passwd_line;
315 return mmap_file_.FindByName(name, &passwd_line) && passwd_line.ToPasswdState(passwd_state);
316}
317
318GroupFile::GroupFile(const char* filename) : mmap_file_(filename) {
319}
320
321bool GroupFile::FindById(gid_t id, group_state_t* group_state) {
322 ErrnoRestorer errno_restorer;
323 GroupLine group_line;
324 return mmap_file_.FindById(id, &group_line) && group_line.ToGroupState(group_state);
325}
326
327bool GroupFile::FindByName(const char* name, group_state_t* group_state) {
328 ErrnoRestorer errno_restorer;
329 GroupLine group_line;
330 return mmap_file_.FindByName(name, &group_line) && group_line.ToGroupState(group_state);
331}