-
Notifications
You must be signed in to change notification settings - Fork 1
/
users.c
355 lines (297 loc) · 7.12 KB
/
users.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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#define _POSIX_C_SOURCE 200809L
#define _XOPEN_SOURCE 700
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <err.h>
#include <pwd.h>
#include <syslog.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include "config.h"
#include "users.h"
extern struct config config;
static struct userlist ulist;
static int ulistnum = 0;
static const char *user_blacklist[] = {
"nfsnobody",
"nobody",
NULL,
};
static int users_port_bind(
uint16_t port,
char try)
{
struct addrinfo *ai, hints;
char s_port[64];
int fd = -1;
int rc;
memset(&hints, 0, sizeof(hints));
memset(s_port, 0, sizeof(s_port));
hints.ai_flags = AI_PASSIVE;
hints.ai_family = AF_INET6;
snprintf(s_port, 64, "%hu", port);
if (port < PRIVPORTS || port > 65535) {
syslog(LOG_WARNING, "Cannot bind to port %d, invalid port range: %s", port, strerror(errno));
return -1;
}
rc = getaddrinfo(NULL, s_port, &hints, &ai);
if (rc) {
syslog(LOG_WARNING, "Cannot resolve address to bind to: %s", gai_strerror(rc));
return -1;
}
fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
if (fd < 0) {
syslog(LOG_WARNING, "Cannot allocate socket: %s", strerror(errno));
goto fail;
}
rc = 1;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &rc, sizeof(rc)) < 0) {
syslog(LOG_WARNING, "Cannot set socket options: %s", strerror(errno));
goto fail;
}
if (bind(fd, ai->ai_addr, ai->ai_addrlen) < 0) {
if (!try)
syslog(LOG_WARNING, "Cannot bind to address: %s", strerror(errno));
goto fail;
}
end:
freeaddrinfo(ai);
return fd;
fail:
if (ai)
freeaddrinfo(ai);
if (fd >= 0)
close(fd);
return -1;
}
static int users_search(
struct passwd *p)
{
struct reserved_port *rp = NULL;
if (!p)
return 0;
for (rp = ulist.lh_first; rp != NULL; rp = rp->entries.le_next) {
if (p->pw_uid == rp->uid)
return 1;
}
return 0;
}
static int users_add(
struct passwd *p)
{
struct reserved_port *rp = NULL;
if (!p)
goto fail;
/* User already exists */
if (users_search(p))
goto fail;
rp = malloc(sizeof(*rp));
if (!rp) {
syslog(LOG_WARNING, "Cannot allocate memory for user %s to bind to port: %s", p->pw_name, strerror(errno));
goto fail;
}
rp->username = strdup(p->pw_name);
if (!rp->username) {
syslog(LOG_WARNING, "Cannot allocate memory for username %s to bind to port: %s", p->pw_name, strerror(errno));
goto fail;
}
rp->uid = p->pw_uid;
rp->fd = -1;
rp->dont_reacquire = 0;
rp->reacquire_time = 0;
rp->port = config.port_offset + p->pw_uid;
/* Dont allow overflow */
if ((int)rp->port < (int)config.port_offset) {
syslog(LOG_WARNING, "Cannot bind port, integer overflow");
goto fail;
}
if ((rp->fd = users_port_bind(rp->port, 0)) < 0)
goto fail;
rp->released = 0;
LIST_INSERT_HEAD(&ulist, rp, entries);
ulistnum++;
syslog(LOG_NOTICE, "Added port %d for user %s", rp->port, rp->username);
return 1;
fail:
if (rp) {
if (rp->username)
free(rp->username);
if (rp->fd > -1)
close(rp->fd);
}
return 0;
}
static int users_delete(
uid_t uid)
{
struct reserved_port *rp = NULL;
for (rp = ulist.lh_first; rp != NULL; rp = rp->entries.le_next) {
if (rp->uid == uid)
break;
}
if (!rp)
return 0;
syslog(LOG_NOTICE, "Deleting %s", rp->username);
if (rp->released == 0)
close(rp->fd);
free(rp->username);
LIST_REMOVE(rp, entries);
ulistnum--;
free(rp);
return 1;
}
void users_init(
void)
{
LIST_INIT(&ulist);
}
void users_sync(
void)
{
struct passwd *p = NULL;
struct reserved_port *rp = NULL, *rp2 = NULL;
char **blacklist;
/* Add any users we are unaware of */
while ((p = getpwent())) {
/* Make sure the blacklist does not match */
for (blacklist = (char **)user_blacklist; *blacklist != NULL; blacklist++) {
if (strcmp(*blacklist, p->pw_name) == 0)
goto next;
}
/* Dont register users below the system user threshold */
if (p->pw_uid < config.system_user_threshold)
goto next;
users_add(p);
next:
continue;
}
/* Delete any users that no longer exist */
for (rp = ulist.lh_first; rp != NULL; rp = rp->entries.le_next) {
restart:
p = getpwuid(rp->uid);
if (!p) {
/* Once the entry is deleted, we need to short-circuit the loop */
rp2 = rp->entries.le_next;
users_delete(rp->uid);
rp = rp2;
if (!rp)
break;
goto restart;
}
}
endpwent();
}
void users_reacquire_ports(
void)
{
struct reserved_port *rp = NULL;
time_t now = time(NULL);
int tmp;
for (rp = ulist.lh_first; rp != NULL; rp = rp->entries.le_next) {
if (rp->released && rp->reacquire_time < now && rp->dont_reacquire == 0) {
tmp = users_port_bind(rp->port, 1);
if (errno == EADDRINUSE) {
rp->reacquire_time += DEFAULT_REACQUIRE_TIMEOUT;
return;
}
syslog(LOG_NOTICE, "Re-acquired port %d for user %s", rp->port, rp->username);
rp->fd = tmp;
rp->reacquire_time = 0;
rp->released = 0;
}
}
}
int users_port_request(
uid_t uid,
uint16_t port)
{
struct reserved_port *rp;
for (rp = ulist.lh_first; rp != NULL; rp = rp->entries.le_next) {
if (rp->uid == uid) {
if (port == 0)
port= rp->port;
else if (rp->port != port)
return -EINVAL;
if (rp->released) {
rp->fd = users_port_bind(port, 0);
if (rp->fd >= 0) {
rp->released = 0;
rp->reacquire_time = 0;
}
return -errno;
}
return -EADDRINUSE;
}
}
return -ENOENT;
}
int users_port_release(
uid_t uid,
uint16_t port)
{
struct reserved_port *rp;
for (rp = ulist.lh_first; rp != NULL; rp = rp->entries.le_next) {
if (rp->uid == uid) {
if (port == 0)
port= rp->port;
else if (rp->port != port)
return -EINVAL;
if (!rp->released) {
close(rp->fd);
rp->fd = -1;
rp->released = 1;
rp->reacquire_time = time(NULL) + DEFAULT_REACQUIRE_TIMEOUT;
return -errno;
}
return -ENOTCONN;
}
}
return -ENOENT;
}
int users_port_acquire_policy(
uid_t uid,
uint8_t dont_reacquire)
{
struct reserved_port *rp;
for (rp = ulist.lh_first; rp != NULL; rp = rp->entries.le_next) {
if (rp->uid == uid) {
rp->dont_reacquire = dont_reacquire;
return 0;
}
}
return -ENOENT;
}
int users_port_list(
uid_t uid,
struct portinfo **info,
uint16_t *len)
{
struct portinfo *pi = NULL;
struct reserved_port *rp;
int i=0;
pi = calloc(ulistnum, sizeof(*pi));
if (!pi)
return -errno;
for (rp = ulist.lh_first; rp != NULL; rp = rp->entries.le_next) {
pi[i].uid = rp->uid;
pi[i].port = rp->port;
/* Dont share reserve status with unauthorized users */
if (uid == rp->uid || uid == 0) {
pi[i].status = rp->released;
pi[i].dont_reacquire = rp->dont_reacquire;
}
else {
pi[i].status = STATUS_UNKNOWN;
pi[i].dont_reacquire = REACQUIRE_UNKNOWN;
}
i++;
}
*info = pi;
*len = ulistnum;
return 0;
}