This repository has been archived by the owner on Jan 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdhcpctl.c
418 lines (370 loc) · 9.17 KB
/
dhcpctl.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sqlite3.h>
#include "db.h"
#include "error.h"
sqlite3 *db = NULL;
extern char **environ;
static int main_lease(int argc, char **argv);
static int main_lease_add(int argc, char **argv);
static int main_lease_show(int argc, char **argv);
static int main_lease_remove(int argc, char **argv);
static int main_start(int argc, char **argv);
static int main_stop(int argc, char **argv);
static int main_restart(int argc, char **argv);
static int main_flush(int argc, char **argv);
static int main_help(int argc, char **argv);
static int main_mkdb(int argc, char **argv);
int main(int argc, char **argv)
{
int (*command)(int, char **) = main_help;
if (1 < argc)
{
char *arg = argv[1];
if (!strcmp(arg, "lease"))
command = main_lease;
else if (!strcmp(arg, "leases"))
command = main_lease;
else if (!strcmp(arg, "start"))
command = main_start;
else if (!strcmp(arg, "stop"))
command = main_stop;
else if (!strcmp(arg, "restart"))
command = main_restart;
else if (!strcmp(arg, "flush"))
command = main_flush;
else if (!strcmp(arg, "help"))
command = main_help;
else if (!strcmp(arg, "mkdb"))
command = main_mkdb;
}
return command(argc - 1, argv + 1);
}
static int main_mkdb(int argc, char **argv)
{
if (argc < 3)
return main_help(argc, argv);
if (!strcmp(argv[1], "file"))
{
if (sqlite3_open(argv[2], &db) != SQLITE_OK)
dhcpd_error(1, 0, "Error while opening lease database: %s", sqlite3_errmsg(db));
}
else if (!strcmp(argv[1], "interface") || !strcmp(argv[1], "if"))
{
size_t db_file_len = strlen(argv[2]) + sizeof ".db" + 1;
char db_file[db_file_len];
strcpy(db_file, argv[2]);
strcpy(db_file + db_file_len - sizeof ".db" - 1, ".db");
db_file[db_file_len - 1] = 0;
if (sqlite3_open(db_file, &db) != SQLITE_OK)
dhcpd_error(1, 0, "Error while opening lease database: %s", sqlite3_errmsg(db));
}
db_init(db);
sqlite3_close(db);
return 0;
}
static int main_lease(int argc, char **argv)
{
(void)argc, (void)argv;
int (*command)(int, char **) = NULL;
struct {
char *db;
char *interface;
char *command;
} dhcpctl_lease = {
.db = NULL,
.interface = NULL,
.command = NULL
};
int i = 1;
{
int state = 0;
for (; i < argc; ++i)
{
char *arg = argv[i];
switch (state)
{
case 0:
if (arg[0] != '-')
{
dhcpctl_lease.command = arg;
goto end_loop;
}
if (!strcmp(arg, "-db"))
state = 1;
else if (!strcmp(arg, "-interface"))
state = 2;
else if (!strcmp(arg, "-if"))
state = 2;
break;
case 1:
dhcpctl_lease.db = arg;
state = 0;
break;
case 2:
dhcpctl_lease.interface = arg;
state = 0;
break;
}
continue;
end_loop:
break;
}
}
if (dhcpctl_lease.command)
{
if (!strcmp(dhcpctl_lease.command, "add"))
command = main_lease_add;
else if (!strcmp(dhcpctl_lease.command, "get"))
command = main_lease_show;
else if (!strcmp(dhcpctl_lease.command, "show"))
command = main_lease_show;
else if (!strcmp(dhcpctl_lease.command, "remove"))
command = main_lease_remove;
}
if (dhcpctl_lease.db)
{
if (sqlite3_open(dhcpctl_lease.db, &db) != SQLITE_OK)
dhcpd_error(1, 0, "Error while opening lease database: %s", sqlite3_errmsg(db));
}
else if (dhcpctl_lease.interface)
{
size_t db_file_len = strlen(dhcpctl_lease.interface) + sizeof ".db" + 1;
char db_file[db_file_len];
strcpy(db_file, dhcpctl_lease.interface);
strcpy(db_file + db_file_len - sizeof ".db" - 1, ".db");
db_file[db_file_len - 1] = 0;
if (sqlite3_open(db_file, &db) != SQLITE_OK)
dhcpd_error(1, 0, "Error while opening lease database: %s", sqlite3_errmsg(db));
}
else
return main_help(argc, argv);
db_init(db);
if (command)
return command(argc - i, argv + i);
return main_help(argc, argv);
}
static int main_lease_add(int argc, char **argv)
{
struct {
char *hwaddr;
char *ipaddr;
char **routers;
size_t routers_cnt;
char **nameservers;
size_t nameservers_cnt;
uint32_t leasetime;
uint8_t prefix;
} dhcpctl_lease_add = {
.hwaddr = NULL,
.ipaddr = NULL,
.routers = NULL,
.routers_cnt = 0,
.nameservers = NULL,
.nameservers_cnt = 0,
.leasetime = 3600,
.prefix = 24
};
(void)dhcpctl_lease_add;
if (argc < 3)
return main_help(argc, argv);
dhcpctl_lease_add.hwaddr = argv[1];
dhcpctl_lease_add.ipaddr = argv[2];
{
int state = 0;
for (int i = 3; i < argc; ++i)
{
char *arg = argv[i];
switch (state)
{
case 0:
if (!strcmp(arg, "router"))
state = 1;
else if (!strcmp(arg, "nameserver"))
state = 2;
else if (!strcmp(arg, "leasetime"))
state = 3;
else if (!strcmp(arg, "prefix"))
state = 4;
break;
case 1:
dhcpctl_lease_add.routers = realloc(dhcpctl_lease_add.routers,
++dhcpctl_lease_add.routers_cnt * sizeof(char*));
dhcpctl_lease_add.routers[dhcpctl_lease_add.routers_cnt-1] = arg;
state = 0;
break;
case 2:
dhcpctl_lease_add.nameservers = realloc(dhcpctl_lease_add.nameservers,
++dhcpctl_lease_add.nameservers_cnt * sizeof(char*));
dhcpctl_lease_add.nameservers[dhcpctl_lease_add.nameservers_cnt-1] = arg;
state = 0;
break;
case 3:
dhcpctl_lease_add.leasetime = atoi(arg);
state = 0;
break;
case 4:
dhcpctl_lease_add.prefix = atoi(arg);
state = 0;
break;
}
}
}
struct db_lease db_lease = {
.id = 0,
.address = dhcpctl_lease_add.ipaddr,
.prefixlen = dhcpctl_lease_add.prefix,
.hwaddr = dhcpctl_lease_add.hwaddr,
.routers = (dhcpctl_lease_add.routers_cnt > 0) ?
dhcpctl_lease_add.routers[0] : NULL,
.nameservers = (dhcpctl_lease_add.nameservers_cnt > 0) ?
dhcpctl_lease_add.nameservers[0] : NULL,
.leasetime = dhcpctl_lease_add.leasetime,
.allocated = 0,
.allocated_at = 0
};
int sqlerr;
if ((sqlerr = db_insert(db, &db_lease)) != SQLITE_DONE)
dhcpd_error(1, 0, "sqlite3: %s\n", sqlite3_errstr(sqlerr));
return 0;
}
static int main_lease_show(int argc, char **argv)
{
(void)argc, (void)argv;
return 0;
}
static int main_lease_remove(int argc, char **argv)
{
struct db_lease db_lease = DB_LEASE_EMPTY;
for (int i = 1; i < argc; ++i)
{
db_lease.id = atoi(argv[i]);
db_lease_delete(db, &db_lease);
}
return 0;
}
static int main_start(int argc, char **argv)
{
struct {
bool daemon;
char *binary;
char *pidfile;
char *config;
int argc;
char **argv;
} dhcpctl_start = {
.daemon = true,
.binary = "dhcpd",
.pidfile = "dhcpd.pid",
.config = NULL,
.argc = 0,
.argv = NULL
};
{
int state = 0;
for (int i = 1; i < argc; ++i)
{
char *arg = argv[i];
switch (state)
{
case 0:
if (!strcmp(arg, "nodaemon"))
dhcpctl_start.daemon = false;
else if (!strcmp(arg, "daemon"))
dhcpctl_start.daemon = true;
else if (!strcmp(arg, "nopidfile"))
dhcpctl_start.pidfile = NULL;
else if (!strcmp(arg, "binary"))
state = 1;
else if (!strcmp(arg, "pidfile"))
state = 2;
else if (!strcmp(arg, "config"))
state = 3;
else if (!strcmp(arg, "--"))
{
dhcpctl_start.argc = argc - i - 1;
dhcpctl_start.argv = argv + i + 1;
goto loop_end;
}
break;
case 1:
dhcpctl_start.binary = arg;
state = 0;
break;
case 2:
dhcpctl_start.pidfile = arg;
state = 0;
break;
case 3:
dhcpctl_start.config = arg;
state = 0;
break;
}
continue;
loop_end:
break;
}
}
if (dhcpctl_start.daemon)
{
pid_t pid = fork();
if (pid == -1)
dhcpd_error(1, errno, "Fork failed");
else if (pid != 0)
return 0;
}
if (dhcpctl_start.pidfile)
{
FILE *pidfile = fopen(dhcpctl_start.pidfile, "w");
if (!pidfile)
dhcpd_error(1, errno, "Opening pidfile %s failed", dhcpctl_start.pidfile);
fprintf(pidfile, "%u\n", getpid());
fclose(pidfile);
}
if (dhcpctl_start.argc < 0)
dhcpctl_start.argc = 0;
char **execargv = malloc(sizeof(char*) * (1 + dhcpctl_start.argc + 1));
execargv[0] = dhcpctl_start.binary;
execargv[1 + dhcpctl_start.argc] = NULL;
if (dhcpctl_start.argc > 0)
memcpy(execargv + 1, dhcpctl_start.argv, dhcpctl_start.argc * sizeof(char*));
execve(dhcpctl_start.binary, execargv, environ);
dhcpd_error(1, errno, "execve failed");
return 0;
}
static int main_stop(int argc, char **argv)
{
(void)argc, (void)argv;
return 0;
}
static int main_restart(int argc, char **argv)
{
(void)argc, (void)argv;
return 0;
}
static int main_flush(int argc, char **argv)
{
(void)argc, (void)argv;
return 0;
}
static int main_help(int argc, char **argv)
{
(void)argc, (void)argv;
printf(
"dhcpctl leases [-db FILE] [-if IF] show [ID]...\n"
"dhcpctl leases [-db FILE] [-if IF] add HWADDR IPADDR [router IPADDR]...\n"
"\t[namserver IPADDR]... [leasetime INT] [prefix INT]\n"
"dhcpctl leases [-db FILE] [-if IF] remove ID...\n"
"dhcpctl start [nodaemon] [binary FILE] [pidfile FILE] [config FILE] [-- [ARG]...]\n"
"dhcpctl stop [pidfile FILE]\n"
"dhcpctl restart [nodaemon] [binary FILE] [pidfile FILE] [config FILE] [-- [ARG]...]\n"
"dhcpctl flush [pidfile FILE]\n"
"dhcpctl mkdb [interface IF|file FILE]\n"
"dhcpctl help\n");
return 0;
}