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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
// SPDX-FileCopyrightText: 2025 Markus Uhlin <maxxe@rpblc.net>
// SPDX-License-Identifier: ISC
#include <string>
#include <vector>
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "addgroup.h"
#define SELF_TEST 0
struct group_info {
std::string name;
std::string password;
int gid;
std::string members;
group_info()
{
this->name.assign("");
this->password.assign("");
this->gid = 0;
this->members.assign("");
}
group_info(const char *p_name, const char *p_password, const int p_gid,
const char *p_members)
{
this->name.assign(p_name);
this->password.assign(p_password);
this->gid = p_gid;
this->members.assign(p_members);
}
};
static std::vector<group_info> groups;
static const int FIRST_SYSTEM_GID = 100;
static const int LAST_SYSTEM_GID = 999;
static bool
is_free_gid(const int gid)
{
for (auto it = groups.begin(); it != groups.end(); ++it) {
if ((*it).gid == gid)
return false;
}
return true;
}
static int
get_free_gid(void)
{
if (groups.empty())
return -1;
for (int gid = FIRST_SYSTEM_GID; gid <= LAST_SYSTEM_GID; gid++) {
if (is_free_gid(gid))
return gid;
}
return -1;
}
int
fics_addgroup(const char *name)
{
int fd;
int gid;
if (name == nullptr || strcmp(name, "") == 0)
return -1;
else if (group_exists(name))
return 0;
else if ((gid = get_free_gid()) == -1)
return -1;
fd = open("/etc/group", (O_RDWR|O_APPEND));
if (fd < 0)
return -1;
struct group_info group(name, "*", gid, "");
dprintf(fd, "%s:%s:%d:%s\n", group.name.c_str(), group.password.c_str(),
group.gid, group.members.c_str());
close(fd);
groups.push_back(group);
return 0;
}
bool
get_group_id(const char *name, int *gid)
{
if (name == nullptr || strcmp(name, "") == 0 || gid == nullptr ||
groups.empty()) {
if (gid)
*gid = 0;
return false;
}
for (auto it = groups.begin(); it != groups.end(); ++it) {
if (strcmp((*it).name.c_str(), name) == 0) {
*gid = (*it).gid;
return true;
}
}
*gid = 0;
return false;
}
bool
get_next_line_from_file(FILE *fp, char **line)
{
const int LINE_MAX_LEN = 2048;
if (fp == nullptr || line == nullptr)
errx(1, "%s: invalid argument", __func__);
if (*line) {
delete[] *line;
*line = nullptr;
}
*line = new char[LINE_MAX_LEN];
return (fgets(*line, LINE_MAX_LEN, fp) ? true : false);
}
bool
group_exists(const char *name)
{
if (name == nullptr || strcmp(name, "") == 0 || groups.empty())
return false;
for (auto it = groups.begin(); it != groups.end(); ++it) {
if (strcmp((*it).name.c_str(), name) == 0)
return true;
}
return false;
}
int
read_the_group_permissions_file(const char *path)
{
FILE *fp = nullptr;
bool read_ok = false;
char *line = nullptr;
const char *token[4];
const char delim[] = ":";
int gid = 0;
if (!groups.empty())
return 0;
if ((fp = fopen(path, "r")) == nullptr)
return -1;
while (get_next_line_from_file(fp, &line)) {
token[0] = strsep(&line, delim);
token[1] = strsep(&line, delim);
token[2] = strsep(&line, delim);
token[3] = strsep(&line, delim);
if (token[0] == nullptr ||
token[1] == nullptr ||
token[2] == nullptr ||
token[3] == nullptr) {
warnx("%s: too few tokens", __func__);
continue;
} else if (sscanf(token[2], "%d", &gid) != 1) {
warnx("%s: sscanf() error", __func__);
continue;
}
struct group_info group(token[0], token[1], gid, token[3]);
groups.push_back(group);
}
if (feof(fp))
read_ok = true;
fclose(fp);
delete[] line;
return (read_ok ? 0 : -1);
}
#if SELF_TEST
int
main(void)
{
if (read_the_group_permissions_file("/etc/group") == -1)
errx(1, "failed to read the group permissions file");
if (fics_addgroup("chess") == -1)
errx(1, "failed to add a group");
puts("ok");
return 0;
}
#endif
|