-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgrp.c
168 lines (158 loc) · 3.4 KB
/
grp.c
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
#include "grp.h"
#include "fcntl.h"
#include "user.h"
#include "errno.h"
static struct group current_group;
static char current_line[MAX_GROUP_LINE_LENGTH];
static char* group_members_names[MAX_GROUP_MEMBERS + 1];
static int fd;
// This function assumes that fd is already open
// Returns -1 if could not read next entry
static int
get_and_parse_grent(void)
{
char* tokens[NUMBER_OF_GROUP_TOKENS];
int ok = 0;
fgets(current_line, MAX_GROUP_LINE_LENGTH, fd);
int length = strlen(current_line);
if (current_line[length - 1] == '\n' ||
current_line[length - 1] == '\r') {
current_line[length - 1] = 0;
}
int next_token = 0;
int i;
int next_group_name = 0;
int comma = 0;
for (i = 0; current_line[i]; ++i) {
if (current_line[i] == ':') {
if (ok == 0) {
tokens[next_token++] = current_line + i;
}
current_line[i] = 0;
ok = 0;
} else if (ok == 0 && next_token < NUMBER_OF_GROUP_TOKENS) {
ok = 1;
tokens[next_token++] = current_line + i;
if (next_token == NUMBER_OF_GROUP_TOKENS) {
group_members_names[next_group_name++] = current_line + i;
}
} else if (current_line[i] == ',' && next_token == NUMBER_OF_GROUP_TOKENS) {
current_line[i] = 0;
comma = 1;
} else if (comma) {
group_members_names[next_group_name++] = current_line + i;
comma = 0;
}
}
if (i == 0) return -1;
current_group.gr_name = tokens[0];
current_group.gr_passwd = tokens[1];
current_group.gr_gid = atoi(tokens[2]);
current_group.gr_mem = group_members_names;
group_members_names[next_group_name] = 0;
return 0;
}
struct group*
getgrent(void)
{
if (fd == 0) {
fd = open(GROUP_FILE, O_RDONLY);
if (fd < 0) {
fd = 0;
return 0;
}
}
if (get_and_parse_grent() == -1) {
return 0;
}
return ¤t_group;
}
void
setgrent(void)
{
if (fd != 0) {
close(fd);
fd = 0;
}
fd = open(GROUP_FILE, O_RDONLY);
if (fd < 0) fd = 0;
}
void
endgrent(void)
{
if (fd != 0) {
close(fd);
fd = 0;
}
}
struct group*
getgrnam(const char* name)
{
setgrent();
while (getgrent()) {
if (strcmp(name, current_group.gr_name) == 0) {
endgrent();
return ¤t_group;
}
}
endgrent();
return 0;
}
struct group*
getgrgid(gid_t gid)
{
setgrent();
while (getgrent()) {
if (current_group.gr_gid == gid) {
endgrent();
return ¤t_group;
}
}
endgrent();
return 0;
}
int
putgrent(struct group* grp, int fd)
{
if (grp == 0) return -1;
printf(fd, "%s:%s:%d:",
grp->gr_name,
grp->gr_passwd,
grp->gr_gid);
for (int i = 0; grp->gr_mem[i]; ++i)
{
if (i != 0) printf(fd, ",");
printf(fd, "%s", grp->gr_mem[i]);
}
printf(fd, "\n");
return 0;
}
int
initgroups(const char* user, gid_t additional_group)
{
if (geteuid() != 0) {
errno = EPERM;
return -1;
}
const int max_number_of_groups = 32;
gid_t groups[max_number_of_groups + 1];
setgrent();
struct group* group;
int next_to_add = 0;
int add_additional = 1;
while ((group = getgrent())) {
for (int i = 0; group->gr_mem[i]; ++i) {
if (strcmp(group->gr_mem[i], user) == 0) {
groups[next_to_add++] = group->gr_gid;
if (group->gr_gid == additional_group) {
add_additional = 0;
}
}
}
}
if (add_additional) {
groups[next_to_add++] = additional_group;
}
setgroups(next_to_add, groups);
return 0;
}