Serenity Operating System
at portability 180 lines 5.7 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 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 are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <AK/String.h> 28#include <AK/Vector.h> 29#include <pwd.h> 30#include <stdio.h> 31#include <stdlib.h> 32#include <sys/mman.h> 33#include <unistd.h> 34 35extern "C" { 36 37#define PWDB_STR_MAX_LEN 256 38 39struct passwd_with_strings : public passwd { 40 char name_buffer[PWDB_STR_MAX_LEN]; 41 char passwd_buffer[PWDB_STR_MAX_LEN]; 42 char gecos_buffer[PWDB_STR_MAX_LEN]; 43 char dir_buffer[PWDB_STR_MAX_LEN]; 44 char shell_buffer[PWDB_STR_MAX_LEN]; 45}; 46 47static FILE* __pwdb_stream = nullptr; 48static unsigned __pwdb_line_number = 0; 49static struct passwd_with_strings* __pwdb_entry = nullptr; 50 51void setpwent() 52{ 53 __pwdb_line_number = 0; 54 if (__pwdb_stream) { 55 rewind(__pwdb_stream); 56 } else { 57 __pwdb_stream = fopen("/etc/passwd", "r"); 58 if (!__pwdb_stream) { 59 perror("open /etc/passwd"); 60 } 61 assert(__pwdb_stream); 62 __pwdb_entry = (struct passwd_with_strings*)mmap_with_name(nullptr, getpagesize(), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0, "setpwent"); 63 } 64} 65 66void endpwent() 67{ 68 __pwdb_line_number = 0; 69 if (__pwdb_stream) { 70 fclose(__pwdb_stream); 71 __pwdb_stream = nullptr; 72 } 73 if (__pwdb_entry) { 74 munmap(__pwdb_entry, getpagesize()); 75 __pwdb_entry = nullptr; 76 } 77} 78 79struct passwd* getpwuid(uid_t uid) 80{ 81 setpwent(); 82 while (auto* pw = getpwent()) { 83 if (pw->pw_uid == uid) 84 return pw; 85 } 86 return nullptr; 87} 88 89struct passwd* getpwnam(const char* name) 90{ 91 setpwent(); 92 while (auto* pw = getpwent()) { 93 if (!strcmp(pw->pw_name, name)) 94 return pw; 95 } 96 return nullptr; 97} 98 99struct passwd* getpwent() 100{ 101 if (!__pwdb_stream) 102 setpwent(); 103 104 assert(__pwdb_stream); 105 if (feof(__pwdb_stream)) 106 return nullptr; 107 108next_entry: 109 char buffer[1024]; 110 ++__pwdb_line_number; 111 char* s = fgets(buffer, sizeof(buffer), __pwdb_stream); 112 if (!s) 113 return nullptr; 114 assert(__pwdb_stream); 115 if (feof(__pwdb_stream)) 116 return nullptr; 117 String line(s, Chomp); 118 auto parts = line.split(':', true); 119 if (parts.size() != 7) { 120 fprintf(stderr, "getpwent(): Malformed entry on line %u\n", __pwdb_line_number); 121 goto next_entry; 122 } 123 auto& e_name = parts[0]; 124 auto& e_passwd = parts[1]; 125 auto& e_uid_string = parts[2]; 126 auto& e_gid_string = parts[3]; 127 auto& e_gecos = parts[4]; 128 auto& e_dir = parts[5]; 129 auto& e_shell = parts[6]; 130 bool ok; 131 uid_t e_uid = e_uid_string.to_uint(ok); 132 if (!ok) { 133 fprintf(stderr, "getpwent(): Malformed UID on line %u\n", __pwdb_line_number); 134 goto next_entry; 135 } 136 gid_t e_gid = e_gid_string.to_uint(ok); 137 if (!ok) { 138 fprintf(stderr, "getpwent(): Malformed GID on line %u\n", __pwdb_line_number); 139 goto next_entry; 140 } 141 __pwdb_entry->pw_uid = e_uid; 142 __pwdb_entry->pw_gid = e_gid; 143 __pwdb_entry->pw_name = __pwdb_entry->name_buffer; 144 __pwdb_entry->pw_passwd = __pwdb_entry->passwd_buffer; 145 __pwdb_entry->pw_gecos = __pwdb_entry->gecos_buffer; 146 __pwdb_entry->pw_dir = __pwdb_entry->dir_buffer; 147 __pwdb_entry->pw_shell = __pwdb_entry->shell_buffer; 148 strncpy(__pwdb_entry->name_buffer, e_name.characters(), PWDB_STR_MAX_LEN); 149 strncpy(__pwdb_entry->passwd_buffer, e_passwd.characters(), PWDB_STR_MAX_LEN); 150 strncpy(__pwdb_entry->gecos_buffer, e_gecos.characters(), PWDB_STR_MAX_LEN); 151 strncpy(__pwdb_entry->dir_buffer, e_dir.characters(), PWDB_STR_MAX_LEN); 152 strncpy(__pwdb_entry->shell_buffer, e_shell.characters(), PWDB_STR_MAX_LEN); 153 return __pwdb_entry; 154} 155 156int putpwent(const struct passwd* p, FILE* stream) 157{ 158 if (!p || !stream || !p->pw_name || !p->pw_dir || !p->pw_gecos || !p->pw_shell) { 159 errno = EINVAL; 160 return -1; 161 } 162 163 auto is_valid_field = [](const char* str) { 164 return str && !strpbrk(str, ":\n"); 165 }; 166 167 if (!is_valid_field(p->pw_name) || !is_valid_field(p->pw_dir) || !is_valid_field(p->pw_gecos) || !is_valid_field(p->pw_shell)) { 168 errno = EINVAL; 169 return -1; 170 } 171 172 int nwritten = fprintf(stream, "%s:x:%u:%u:%s,,,:%s:%s\n", p->pw_name, p->pw_uid, p->pw_gid, p->pw_gecos, p->pw_dir, p->pw_shell); 173 if (!nwritten || nwritten < 0) { 174 errno = ferror(stream); 175 return -1; 176 } 177 178 return 0; 179} 180}