-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfsipd.c
600 lines (521 loc) · 13.2 KB
/
fsipd.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
/*-
* Copyright (c) 2016, Babak Farrokhi
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#define _POSIX_C_SOURCE 200809L
#include <sys/cdefs.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/file.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <getopt.h>
#include <netdb.h>
#include <signal.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <sysexits.h>
#include <unistd.h>
#define SYSLOG_NAMES
#include <pidutil.h>
#include <pthread.h>
#include <syslog.h>
#include "banned.h"
#include "logfile.h"
#define PORT 5060
#define BACKLOG 1024
#ifndef IPV6_BINDV6ONLY /* Linux does not have IPV6_BINDV6ONLY */
#define IPV6_BINDV6ONLY IPV6_V6ONLY
#endif /* IPV6_BINDV6ONLY */
/*
* Globals
*/
log_t * lfh;
struct pidfh *pfh;
bool use_syslog = false;
char * logfilename = NULL;
int syslog_pri = -1;
struct sockaddr_in t_sa, u_sa;
int t_sockfd, u_sockfd;
#ifdef PF_INET6
struct sockaddr_in6 t6_sa, u6_sa;
int t6_sockfd, u6_sockfd;
#endif /* PF_INET6 */
/*
* trim string from whitespace characters
*/
size_t
chomp(char *s)
{
int i;
/* trim leading spaces */
while (isspace(*s))
s++;
/* All spaces? */
if (*s == 0)
return 0;
/* trim trailing spaces */
i = strlen(s);
while ((i > 0) && (isspace(s[i - 1])))
i--;
s[i] = '\0';
return i;
}
/*
* Prepare for a clean shutdown
*/
void
daemon_shutdown()
{
pidfile_remove(pfh);
if (!use_syslog)
log_close(lfh);
}
/*
* Act upon receiving signals
*/
void
signal_handler(int sig)
{
switch (sig) {
case SIGHUP:
if (!use_syslog)
log_reopen(&lfh); /* necessary for log file
* rotation */
break;
case SIGINT:
case SIGTERM:
daemon_shutdown();
exit(EXIT_SUCCESS);
break;
default:
break;
}
}
void
process_request(int af, struct sockaddr *src, int proto, char *str)
{
char * p_names[] = { "TCP", "UDP", "RAW", "UNKNOWN" };
char * pname;
uint16_t port;
char addr_str[46];
struct sockaddr_in *s_in;
#ifdef PF_INET6
struct sockaddr_in6 *s_in6;
#endif /* PF_INET6 */
switch (proto) {
case SOCK_STREAM:
pname = p_names[0];
break;
case SOCK_DGRAM:
pname = p_names[1];
break;
case SOCK_RAW:
pname = p_names[2];
break;
default:
pname = p_names[3];
;
}
chomp(str);
#ifdef PF_INET6
switch (af) {
case AF_INET6:
s_in6 = (struct sockaddr_in6 *)src;
inet_ntop(af, &s_in6->sin6_addr, addr_str, sizeof(addr_str));
port = ntohs(s_in6->sin6_port);
if (use_syslog) {
syslog(syslog_pri, "From: %s:%d (%s6) - Message: \"%s\"", addr_str, port,
pname, str);
} else {
log_printf(lfh, "%ld,%s6,%s,%d,\"%s\"", time(NULL), pname, addr_str, port,
str);
}
break;
case AF_INET:
s_in = (struct sockaddr_in *)src;
if (use_syslog) {
syslog(syslog_pri, "From: %s:%d (%s4) - Message: \"%s\"",
inet_ntoa(s_in->sin_addr), ntohs(s_in->sin_port), pname, str);
} else {
log_printf(lfh, "%ld,%s4,%s,%d,\"%s\"", time(NULL), pname,
inet_ntoa(s_in->sin_addr), ntohs(s_in->sin_port), str);
}
break;
}
#else
s_in = (struct sockaddr_in *)src;
if (use_syslog) {
syslog(syslog_pri, "From: %s:%d (%s4) - Message: \"%s\"", inet_ntoa(s_in->sin_addr),
ntohs(s_in->sin_port), pname, str);
} else {
log_printf(lfh, "%ld,%s4,%s,%d,\"%s\"", time(NULL), pname,
inet_ntoa(s_in->sin_addr), ntohs(s_in->sin_port), str);
}
#endif
}
/*
* setup TCP listener socket
*/
int
init_tcp()
{
#ifdef PF_INET6
/* Setup TCP6 Listener */
memset(&t6_sa, 0, sizeof(t6_sa));
t6_sa.sin6_port = htons(PORT);
t6_sa.sin6_family = AF_INET6;
t6_sa.sin6_addr = in6addr_any;
t6_sa.sin6_scope_id = 0;
if ((t6_sockfd = socket(PF_INET6, SOCK_STREAM, 0)) < 0) {
perror("tcp6 socket()");
return (EXIT_FAILURE);
}
int on = 1;
setsockopt(t6_sockfd, IPPROTO_IPV6, IPV6_BINDV6ONLY, (char *)&on, sizeof(on));
if (bind(t6_sockfd, (struct sockaddr *)&t6_sa, sizeof(t6_sa)) < 0) {
perror("tcp6 bind()");
return (EXIT_FAILURE);
}
#endif /* PF6_INET */
/* Setup TCP4 Listener */
memset(&t_sa, 0, sizeof(t_sa));
t_sa.sin_port = htons(PORT);
t_sa.sin_family = AF_INET;
t_sa.sin_addr.s_addr = htonl(INADDR_ANY);
if ((t_sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
perror("tcp4 socket()");
return (EXIT_FAILURE);
}
if (bind(t_sockfd, (struct sockaddr *)&t_sa, sizeof(t_sa)) < 0) {
perror("tcp4 bind()");
return (EXIT_FAILURE);
}
return (EXIT_SUCCESS);
}
/*
* setup UDP listener socket
*/
int
init_udp()
{
#ifdef PF_INET6
/* Setup UDP6 Listener */
memset(&u6_sa, 0, sizeof(u6_sa));
u6_sa.sin6_port = htons(PORT);
u6_sa.sin6_family = AF_INET6;
u6_sa.sin6_addr = in6addr_any;
u6_sa.sin6_scope_id = 0;
if ((u6_sockfd = socket(PF_INET6, SOCK_DGRAM, 0)) < 0) {
perror("udp6 socket()");
return (EXIT_FAILURE);
}
int on = 1;
setsockopt(u6_sockfd, IPPROTO_IPV6, IPV6_BINDV6ONLY, (char *)&on, sizeof(on));
if (bind(u6_sockfd, (struct sockaddr *)&u6_sa, sizeof(u6_sa)) < 0) {
perror("udp6 bind()");
return (EXIT_FAILURE);
}
#endif /* PF_INET6 */
/* Setup UDP4 Listener */
memset(&u_sa, 0, sizeof(u_sa));
u_sa.sin_port = htons(PORT);
u_sa.sin_family = AF_INET;
u_sa.sin_addr.s_addr = htonl(INADDR_ANY);
if ((u_sockfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
perror("udp4 socket()");
return (EXIT_FAILURE);
}
if (bind(u_sockfd, (struct sockaddr *)&u_sa, sizeof(u_sa)) < 0) {
perror("udp4 bind()");
return (EXIT_FAILURE);
}
return (EXIT_SUCCESS);
}
void *
tcp4_handler(void *args)
{
int c;
struct sockaddr_in t_other;
FILE * client;
char str[8192];
socklen_t sa_len;
listen(t_sockfd, BACKLOG);
while (1) {
sa_len = sizeof(t_sa);
if ((c = accept(t_sockfd, (struct sockaddr *)&t_other, &sa_len)) < 0) {
perror("tcp accept()");
pthread_exit(NULL);
}
if ((client = fdopen(c, "r")) == NULL) {
perror("tcp fdopen()");
pthread_exit(NULL);
}
memset(str, 0, sizeof(str)); /* just in case */
fgets(str, sizeof(str), client);
process_request(t_other.sin_family, (struct sockaddr *)&t_other, SOCK_STREAM, str);
fclose(client);
}
return (args); /* suppress compiler warning */
}
void *
udp4_handler(void *args)
{
char str[8192];
struct sockaddr_in u_other;
socklen_t sa_len;
ssize_t len;
sa_len = sizeof(u_other);
while (1) {
if ((len = recvfrom(u_sockfd, str, sizeof(str), 0, (struct sockaddr *)&u_other,
&sa_len)) > 0) {
process_request(u_other.sin_family, (struct sockaddr *)&u_other, SOCK_DGRAM,
str);
}
}
return (args); /* suppress compiler warning */
}
#ifdef PF_INET6
void *
tcp6_handler(void *args)
{
int c;
struct sockaddr_in6 t_other;
FILE * client;
char str[8192];
socklen_t sa_len;
listen(t6_sockfd, BACKLOG);
while (1) {
sa_len = sizeof(t6_sa);
if ((c = accept(t6_sockfd, (struct sockaddr *)&t_other, &sa_len)) < 0) {
perror("tcp6 accept()");
pthread_exit(NULL);
}
if ((client = fdopen(c, "r")) == NULL) {
perror("tcp6 fdopen()");
pthread_exit(NULL);
}
memset(str, 0, sizeof(str)); /* just in case */
fgets(str, sizeof(str), client);
process_request(t_other.sin6_family, (struct sockaddr *)&t_other, SOCK_STREAM, str);
fclose(client);
}
return (args); /* suppress compiler warning */
}
void *
udp6_handler(void *args)
{
char str[8192];
struct sockaddr_in6 u_other;
socklen_t sa_len;
ssize_t len;
sa_len = sizeof(u_other);
while (1) {
if ((len = recvfrom(u6_sockfd, str, sizeof(str), 0, (struct sockaddr *)&u_other,
&sa_len)) > 0) {
process_request(u_other.sin6_family, (struct sockaddr *)&u_other,
SOCK_DGRAM, str);
}
}
return (args); /* suppress compiler warning */
}
#endif /* PF_INET6 */
void
init_logger()
{
if (use_syslog) {
/* initialize facility and level parameters */
if (syslog_pri == -1) /* not specidied by user, use default */
syslog_pri = LOG_USER | LOG_NOTICE | LOG_PID;
} else {
/* open a log file in current directory */
if (logfilename == NULL)
logfilename = strdup("fsipd.log");
if ((lfh = log_open(logfilename, 0644)) == NULL)
err(EXIT_FAILURE, "Cannot open log file \"%s\"", logfilename);
}
}
/*
* Daemonize and persist pid
*/
int
daemon_start()
{
struct sigaction sig_action;
sigset_t sig_set;
pid_t otherpid;
int curPID;
pthread_t tcp4_thread, udp4_thread;
pthread_t tcp6_thread, udp6_thread;
/* Check if we can acquire the pid file */
pfh = pidfile_open(NULL, 0644, &otherpid);
if (pfh == NULL) {
if (errno == EEXIST) {
errx(EXIT_FAILURE, "Daemon already running, pid: %jd.", (intmax_t)otherpid);
}
err(EXIT_FAILURE, "Cannot open or create pidfile");
}
init_logger();
/* Initialize TCP46 and UDP46 sockets */
if (init_tcp() == EXIT_FAILURE)
return (EXIT_FAILURE);
if (init_udp() == EXIT_FAILURE)
return (EXIT_FAILURE);
/* start daemonizing */
curPID = fork();
switch (curPID) {
case 0: /* This process is the child */
break;
case -1: /* fork() failed, should exit */
perror("fork");
return (EXIT_FAILURE);
default: /* fork() successful, should exit
* (parent) */
return (EXIT_SUCCESS);
}
/* we are the child, complete the daemonization */
/* Close standard IO */
fclose(stdin);
fclose(stdout);
fclose(stderr);
/* Block unnecessary signals */
sigemptyset(&sig_set);
sigaddset(&sig_set, SIGCHLD); /* ignore child - i.e. we don't need
* to wait for it */
sigaddset(&sig_set, SIGTSTP); /* ignore tty stop signals */
sigaddset(&sig_set, SIGTTOU); /* ignore tty background writes */
sigaddset(&sig_set, SIGTTIN); /* ignore tty background reads */
sigprocmask(SIG_BLOCK, &sig_set, NULL); /* Block the above specified
* signals */
/* Catch necessary signals */
sig_action.sa_handler = signal_handler;
sigemptyset(&sig_action.sa_mask);
sig_action.sa_flags = 0;
sigaction(SIGTERM, &sig_action, NULL);
sigaction(SIGHUP, &sig_action, NULL);
sigaction(SIGINT, &sig_action, NULL);
/* create new session and process group */
setsid();
/* persist pid */
pidfile_write(pfh);
/* Create TCP and UDP listener threads */
pthread_create(&tcp4_thread, NULL, tcp4_handler, NULL);
pthread_create(&udp4_thread, NULL, udp4_handler, NULL);
#ifdef PF_INET6
pthread_create(&tcp6_thread, NULL, tcp6_handler, NULL);
pthread_create(&udp6_thread, NULL, udp6_handler, NULL);
#endif
/*
* Wait for threads to terminate, which normally shouldn't ever
* happen
*/
pthread_join(tcp4_thread, NULL);
pthread_join(udp4_thread, NULL);
#ifdef PF_INET6
pthread_join(tcp6_thread, NULL);
pthread_join(udp6_thread, NULL);
#endif
return (EXIT_SUCCESS);
}
void
usage()
{
printf("usage: fsipd [-h] [-l logfile] [-s] [-p priority] \n");
printf("\t-h: this message\n");
printf("\t-s: use syslog instead of local log file\n");
printf("\t-p: syslog priotiry (default: user.notice)\n");
printf("\t-l: specify output log filename (default: fsipd.log)\n");
}
static int
decode(char *name, const CODE *codetab)
{
const CODE *c;
if (isdigit(*name))
return (atoi(name));
for (c = codetab; c->c_name; c++)
if (!strcasecmp(name, c->c_name))
return (c->c_val);
return (-1);
}
static int
decodepri(char *s)
{
char *save;
int fac, lev;
for (save = s; *s && *s != '.'; ++s)
;
if (*s) {
*s = '\0';
fac = decode(save, facilitynames);
if (fac < 0)
errx(1, "unknown facility name: %s", save);
*s++ = '.';
} else {
fac = 0;
s = save;
}
lev = decode(s, prioritynames);
if (lev < 0)
errx(1, "unknown priority name: %s", save);
return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
}
int
main(int argc, char *argv[])
{
int opt;
while ((opt = getopt(argc, argv, "hl:sp:")) != -1) {
switch (opt) {
case 's':
use_syslog = true;
break;
case 'p':
if (use_syslog) {
syslog_pri = decodepri(optarg) | LOG_PID;
} else {
errx(EX_USAGE, "you need to specify \"-s\".");
}
break;
case 'l':
logfilename = strdup(optarg);
break;
case 'h':
usage();
exit(0);
break;
default:
printf("invalid option: %c\n", opt);
usage();
exit(1);
}
}
return (daemon_start());
}