-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
cmdline.c
352 lines (310 loc) · 10.4 KB
/
cmdline.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <getopt.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#include <limits.h>
#include <libgen.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "globals.h"
#include "cmdline.h"
static struct option long_options[] = {
{ "address", required_argument, NULL, 'b' },
{ "port", required_argument, NULL, 'p' },
{ "pid", required_argument, NULL, 'P' },
{ "name", required_argument, NULL, 'n' },
{ "user", required_argument, NULL, 'u' },
{ "group", required_argument, NULL, 'g' },
{ "chroot", required_argument, NULL, 'c' },
{ "delay", required_argument, NULL, 'd' },
{ "foreground", no_argument, NULL, 'f' },
{ "no-syslog", no_argument, NULL, 'x' },
{ "setver", required_argument, NULL, 's' },
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 'v' }
};
#if defined(__GNUC__) || defined(__clang__)
__attribute__((noreturn))
#endif
static void usage()
{
printf(
"Usage: mysql-honeypotd [options]...\n\n"
"Low-interaction MySQL honeypot\n\n"
"Mandatory arguments to long options are mandatory for short options too.\n"
" -b, --address ADDRESS the IP address to bind to (default: 0.0.0.0)\n"
" (can be specified multiple times)\n"
" -p, --port PORT the port to bind to (default: 3306)\n"
#ifndef MINIMALISTIC_BUILD
" -P, --pid FILE the PID file\n"
" -n, --name NAME the name of the daemon for syslog\n"
" (default: mysql-honeypotd)\n"
" -u, --user USER drop privileges and switch to this USER\n"
" (default: daemon or nobody)\n"
" -g, --group GROUP drop privileges and switch to this GROUP\n"
" (default: daemon or nogroup)\n"
" -c, --chroot DIR chroot() into the specified DIR\n"
" -f, --foreground do not daemonize\n"
" (forced if no PID file specified)\n"
" -x, --no-syslog log messages only to stderr\n"
" (only works with --foreground)\n"
#endif
" -s, --setver VERSION report this MySQL server version\n"
" (default: 8.0.19)\n"
" -d, --delay DELAY Add DELAY seconds after each login attempt\n"
" -h, --help display this help and exit\n"
" -v, --version output version information and exit\n\n"
#ifndef MINIMALISTIC_BUILD
"NOTES:\n"
" 1. --user, --group, and --chroot options are honored only if\n"
" mysql-honeypotd is run as root\n"
" 2. PID file can be outside of chroot\n"
" 3. When using --name and/or --group, please make sure that\n"
" the PID file can be deleted by the target user\n"
"\n"
#endif
"Please report bugs here: <https://github.com/sjinks/mysql-honeypotd/issues>\n"
);
exit(EXIT_SUCCESS);
}
#if defined(__GNUC__) || defined(__clang__)
__attribute__((noreturn))
#endif
static void version()
{
printf(
"mysql-honeypotd 1.0.0\n"
"Copyright (c) 2017, 2021 Volodymyr Kolesnykov <volodymyr@wildwolf.name>\n"
"License: MIT <http://opensource.org/licenses/MIT>\n"
);
exit(EXIT_SUCCESS);
}
static void check_alloc(void* p, const char* api)
{
if (!p) {
perror(api);
exit(EXIT_FAILURE);
}
}
#if defined(__GNUC__) || defined(__clang__)
__attribute__((malloc, returns_nonnull))
#endif
static char* my_strdup(const char *s)
{
char* retval = strdup(s);
check_alloc(retval, "strdup");
return retval;
}
#if defined(__GNUC__) || defined(__clang__)
__attribute__((malloc, returns_nonnull))
#endif
static char* my_strndup(const char *s, size_t n)
{
char* retval = strndup(s, n);
check_alloc(retval, "strndup");
return retval;
}
static void set_defaults(struct globals_t* g)
{
if (!g->server_ver) {
g->server_ver = my_strdup("8.0.19");
}
#ifndef MINIMALISTIC_BUILD
if (!g->daemon_name) {
g->daemon_name = my_strdup("mysql-honeypotd");
}
#endif
if (!g->nsockets) {
g->nalloc = 1;
g->nsockets = 1;
g->bind_addresses = calloc(g->nalloc, sizeof(char*));
check_alloc(g->bind_addresses, "calloc");
g->bind_addresses[0] = my_strdup("0.0.0.0");
}
if (!g->bind_port) {
g->bind_port = my_strdup("3306");
}
}
#ifndef MINIMALISTIC_BUILD
static void resolve_pid_file(struct globals_t* g)
{
if (g->pid_file) {
if (g->pid_file[0] != '/') {
char buf[PATH_MAX+1];
char* cwd = getcwd(buf, PATH_MAX + 1);
char* newbuf = NULL;
/* If the current directory is not below the root directory of
* the current process (e.g., because the process set a new
* filesystem root using chroot(2) without changing its current
* directory into the new root), then, since Linux 2.6.36,
* the returned path will be prefixed with the string
* "(unreachable)". Such behavior can also be caused by
* an unprivileged user by changing the current directory into
* another mount namespace. When dealing with paths from
* untrusted sources, callers of these functions should consider
* checking whether the returned path starts with '/' or '('
* to avoid misinterpreting an unreachable path as a relative path.
*/
if (cwd && cwd[0] == '/') {
size_t cwd_len = strlen(cwd);
size_t pid_len = strlen(g->pid_file);
newbuf = calloc(cwd_len + pid_len + 2, 1);
check_alloc(newbuf, "calloc");
memcpy(newbuf, cwd, cwd_len);
newbuf[cwd_len] = '/';
memcpy(newbuf + cwd_len + 1, g->pid_file, pid_len);
free(g->pid_file);
g->pid_file = newbuf;
}
else {
fprintf(stderr, "ERROR: Failed to get the current directory: %s\n", strerror(errno));
free(g->pid_file);
exit(EXIT_FAILURE);
}
}
{
int e;
char* pid_dir = my_strdup(g->pid_file);
char* pid_fil = my_strdup(g->pid_file);
char* dir = dirname(pid_dir);
char* file = basename(pid_fil);
g->pid_base = my_strdup(file);
g->piddir_fd = open(dir, O_DIRECTORY);
e = errno;
free(pid_fil);
if (g->piddir_fd == -1) {
fprintf(stderr, "ERROR: Failed to open directory %s: %s\n", dir, strerror(e));
free(g->pid_file);
free(pid_dir);
g->pid_file = NULL;
exit(EXIT_FAILURE);
}
free(pid_dir);
}
}
}
#endif
void parse_options(int argc, char** argv, struct globals_t* g)
{
while (1) {
int option_index = 0;
int c = getopt_long(
argc,
argv,
(
"b:p:d:s:hv"
#ifndef MINIMALISTIC_BUILD
"P:n:u:g:c:fx"
#endif
),
long_options,
&option_index
);
if (-1 == c) {
break;
}
switch (c) {
case 'b':
++g->nsockets;
if (g->nsockets > g->nalloc) {
char** tmp;
g->nalloc += 16;
tmp = realloc(g->bind_addresses, g->nalloc * sizeof(char*));
check_alloc(tmp, "realloc");
g->bind_addresses = tmp;
}
g->bind_addresses[g->nsockets-1] = my_strdup(optarg);
break;
case 'p':
free(g->bind_port);
g->bind_port = my_strdup(optarg);
break;
case 'd':
g->delay = atoi(optarg);
if (g->delay < 0) {
g->delay = 0;
}
break;
case 's':
free(g->server_ver);
g->server_ver = my_strndup(optarg, 63);
break;
case 'h':
usage();
/* unreachable */
/* no break */
case 'v':
version();
/* unreachable */
/* no break */
#ifndef MINIMALISTIC_BUILD
case 'P':
free(g->pid_file);
g->pid_file = my_strdup(optarg);
break;
case 'n':
free(g->daemon_name);
g->daemon_name = my_strdup(optarg);
break;
case 'f':
g->foreground = 1;
break;
case 'x':
g->no_syslog = 1;
break;
case 'u': {
struct passwd* pwd = getpwnam(optarg);
if (!pwd) {
fprintf(stderr, "WARNING: unknown user %s\n", optarg);
}
else {
g->uid = pwd->pw_uid;
g->uid_set = 1;
if (!g->gid_set) {
g->gid = pwd->pw_gid;
g->gid_set = 1;
}
}
break;
}
case 'g': {
struct group* grp = getgrnam(optarg);
if (!grp) {
fprintf(stderr, "WARNING: unknown group %s\n", optarg);
}
else {
g->gid = grp->gr_gid;
g->gid_set = 1;
}
break;
}
case 'c':
free(g->chroot_dir);
g->chroot_dir = my_strdup(optarg);
break;
#endif
case '?':
default:
break;
}
}
while (optind < argc) {
fprintf(stderr, "WARNING: unrecognized option: %s\n", argv[optind]);
++optind;
}
set_defaults(g);
#ifndef MINIMALISTIC_BUILD
resolve_pid_file(g);
if (!g->pid_file) {
g->foreground = 1;
}
if (!g->foreground) {
g->no_syslog = 0;
}
#endif
}