|
| 1 | +// This file is a part of the IncludeOS unikernel - www.includeos.org |
| 2 | +// |
| 3 | +// Copyright 2015-2017 Oslo and Akershus University College of Applied Sciences |
| 4 | +// and Alfred Bratterud |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | + |
| 18 | +#include <string> |
| 19 | +#include <vector> |
| 20 | +#include <fstream> |
| 21 | +#include <pwd.h> |
| 22 | +#include <info> |
| 23 | +#include <gsl/gsl_assert> |
| 24 | + |
| 25 | +const char* pwd_path = "/etc/passwd"; |
| 26 | + |
| 27 | +/* Applications are not allowed to modify the structure to which the return |
| 28 | + values point, nor any storage areas pointed to by pointers within the |
| 29 | + structure. Returned pointers might be invalidated or the structure/ |
| 30 | + storage areas might be overwritten by subsequent calls. */ |
| 31 | +static struct passwd ret; |
| 32 | + |
| 33 | +class Pwd { |
| 34 | +public: |
| 35 | + static Pwd& instance() { |
| 36 | + static Pwd pwd; |
| 37 | + return pwd; |
| 38 | + } |
| 39 | + |
| 40 | + void rewind() {pos_ = 0;} |
| 41 | + |
| 42 | + void close() {open_ = false;} |
| 43 | + |
| 44 | + struct passwd* find(const char* name) { |
| 45 | + Expects(name != nullptr); |
| 46 | + if (!open_) { |
| 47 | + read(); |
| 48 | + } |
| 49 | + const auto it = std::find_if(begin(entries_), end(entries_), [name](const auto& entry){ |
| 50 | + return (strcmp(name, entry.c_str()) == 0); |
| 51 | + }); |
| 52 | + if (it == end(entries_)) { |
| 53 | + return nullptr; |
| 54 | + } |
| 55 | + extract(*it); |
| 56 | + return &ret; |
| 57 | + } |
| 58 | + |
| 59 | + struct passwd* next() noexcept { |
| 60 | + if (!open_) { |
| 61 | + read(); |
| 62 | + } |
| 63 | + if (pos_ >= entries_.size()) { |
| 64 | + return nullptr; |
| 65 | + } |
| 66 | + try { |
| 67 | + const auto& ent = entries_[pos_]; |
| 68 | + int field_seps = std::count(std::begin(ent), std::end(ent), '\0'); |
| 69 | + if (field_seps != 6) { |
| 70 | + INFO("pwd", "not a valid passwd file entry"); |
| 71 | + return nullptr; |
| 72 | + } |
| 73 | + extract(ent); |
| 74 | + ++pos_; |
| 75 | + return &ret; |
| 76 | + } |
| 77 | + catch (...) { |
| 78 | + INFO("pwd", "error parsing pwd entry"); |
| 79 | + return nullptr; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + void extract(const std::string& ent) { |
| 84 | + ret.pw_name = const_cast<char*>(ent.c_str()); |
| 85 | + size_t pw_pos = ent.find('\0') + 1; |
| 86 | + size_t uid_pos = ent.find('\0', pw_pos) + 1; |
| 87 | + size_t gid_pos = ent.find('\0', uid_pos) + 1; |
| 88 | + size_t info_pos = ent.find('\0', gid_pos) + 1; |
| 89 | + size_t dir_pos = ent.find('\0', info_pos) + 1; |
| 90 | + size_t shell_pos = ent.find('\0', dir_pos) + 1; |
| 91 | + ret.pw_uid = std::stol(ret.pw_name + uid_pos); |
| 92 | + ret.pw_gid = std::stol(ret.pw_name + gid_pos); |
| 93 | + ret.pw_dir = ret.pw_name + dir_pos; |
| 94 | + ret.pw_shell = ret.pw_name + shell_pos; |
| 95 | + } |
| 96 | + |
| 97 | + void print() { |
| 98 | + printf(" instance @%p\n", this); |
| 99 | + printf(" open: %d\n", open_); |
| 100 | + printf(" pos: %d\n", pos_); |
| 101 | + printf(" entries: %d\n", entries_.size()); |
| 102 | + } |
| 103 | + |
| 104 | +private: |
| 105 | + Pwd() : open_ {false}, pos_ {0} {} |
| 106 | + |
| 107 | + void read() { |
| 108 | + std::ifstream is(pwd_path); |
| 109 | + std::string line; |
| 110 | + while (std::getline(is, line)) { |
| 111 | + std::replace(std::begin(line), std::end(line), ':', '\0'); |
| 112 | + entries_.push_back(line); |
| 113 | + } |
| 114 | + rewind(); |
| 115 | + open_ = true; |
| 116 | + } |
| 117 | + bool open_; |
| 118 | + size_t pos_; |
| 119 | + std::vector<std::string> entries_; |
| 120 | +}; |
| 121 | + |
| 122 | +void endpwent(void) { |
| 123 | + Pwd& pwd = Pwd::instance(); |
| 124 | + pwd.close(); |
| 125 | +} |
| 126 | + |
| 127 | +void setpwent(void) { |
| 128 | + Pwd& pwd = Pwd::instance(); |
| 129 | + pwd.rewind(); |
| 130 | +} |
| 131 | + |
| 132 | +struct passwd *getpwent(void) { |
| 133 | + Pwd& pwd = Pwd::instance(); |
| 134 | + return pwd.next(); |
| 135 | +} |
| 136 | + |
| 137 | +struct passwd *getpwnam(const char *name) { |
| 138 | + if (name == nullptr) { |
| 139 | + return nullptr; |
| 140 | + } |
| 141 | + Pwd& pwd = Pwd::instance(); |
| 142 | + return pwd.find(name); |
| 143 | +} |
0 commit comments