1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/String.h>
#include <AK/Vector.h>
#include <grp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
extern "C" {
#define GRDB_STR_MAX_LEN 256
struct group_with_strings : public group {
char name_buffer[GRDB_STR_MAX_LEN];
char passwd_buffer[GRDB_STR_MAX_LEN];
char* members[32];
char members_buffer[32][32];
};
static FILE* __grdb_stream = nullptr;
static unsigned __grdb_line_number = 0;
static struct group_with_strings* __grdb_entry = nullptr;
void setgrent()
{
__grdb_line_number = 0;
if (__grdb_stream) {
rewind(__grdb_stream);
} else {
__grdb_stream = fopen("/etc/group", "r");
if (!__grdb_stream) {
perror("open /etc/group");
}
assert(__grdb_stream);
__grdb_entry = (struct group_with_strings*)mmap_with_name(nullptr, getpagesize(), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0, "setgrent");
}
}
void endgrent()
{
__grdb_line_number = 0;
if (__grdb_stream) {
fclose(__grdb_stream);
__grdb_stream = nullptr;
}
if (__grdb_entry) {
munmap(__grdb_entry, getpagesize());
__grdb_entry = nullptr;
}
}
struct group* getgrgid(gid_t gid)
{
setgrent();
while (auto* gr = getgrent()) {
if (gr->gr_gid == gid)
return gr;
}
return nullptr;
}
struct group* getgrnam(const char* name)
{
setgrent();
while (auto* gr = getgrent()) {
if (!strcmp(gr->gr_name, name))
return gr;
}
return nullptr;
}
struct group* getgrent()
{
if (!__grdb_stream)
setgrent();
assert(__grdb_stream);
if (feof(__grdb_stream))
return nullptr;
next_entry:
char buffer[1024];
++__grdb_line_number;
char* s = fgets(buffer, sizeof(buffer), __grdb_stream);
if (!s)
return nullptr;
assert(__grdb_stream);
if (feof(__grdb_stream))
return nullptr;
String line(s, Chomp);
auto parts = line.split(':', true);
if (parts.size() != 4) {
fprintf(stderr, "getgrent(): Malformed entry on line %u: '%s' has %zu parts\n", __grdb_line_number, line.characters(), parts.size());
goto next_entry;
}
auto& e_name = parts[0];
auto& e_passwd = parts[1];
auto& e_gid_string = parts[2];
auto& e_members_string = parts[3];
auto e_gid = e_gid_string.to_uint();
if (!e_gid.has_value()) {
fprintf(stderr, "getgrent(): Malformed GID on line %u\n", __grdb_line_number);
goto next_entry;
}
auto members = e_members_string.split(',');
__grdb_entry->gr_gid = e_gid.value();
__grdb_entry->gr_name = __grdb_entry->name_buffer;
__grdb_entry->gr_passwd = __grdb_entry->passwd_buffer;
for (size_t i = 0; i < members.size(); ++i) {
__grdb_entry->members[i] = __grdb_entry->members_buffer[i];
strcpy(__grdb_entry->members_buffer[i], members[i].characters());
}
__grdb_entry->members[members.size()] = nullptr;
__grdb_entry->gr_mem = __grdb_entry->members;
strncpy(__grdb_entry->name_buffer, e_name.characters(), GRDB_STR_MAX_LEN - 1);
strncpy(__grdb_entry->passwd_buffer, e_passwd.characters(), GRDB_STR_MAX_LEN - 1);
return __grdb_entry;
}
int initgroups(const char* user, gid_t extra_gid)
{
size_t count = 0;
gid_t gids[32];
bool extra_gid_added = false;
setgrent();
while (auto* gr = getgrent()) {
for (auto* mem = gr->gr_mem; *mem; ++mem) {
if (!strcmp(*mem, user)) {
gids[count++] = gr->gr_gid;
if (gr->gr_gid == extra_gid)
extra_gid_added = true;
break;
}
}
}
endgrent();
if (!extra_gid_added)
gids[count++] = extra_gid;
return setgroups(count, gids);
}
}
|