-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusyslogd.c
71 lines (62 loc) · 1.86 KB
/
usyslogd.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
/* (c) 2019 - Steve Schnepp <[email protected]> - Apache License 2.0 */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pwd.h>
int main(int argc, char **argv) {
/* create datagram socket */
int fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
printf("failed to open socket; errno: %d\n", errno);
return -1;
}
struct sockaddr_in sa;
sa.sin_family = AF_INET;
sa.sin_port = htons(514);
sa.sin_addr.s_addr = INADDR_ANY;
/* bind socket to port */
int r = bind(fd, (struct sockaddr *) &sa, sizeof(struct sockaddr_in));
if (r < 0) {
printf("bind failed; errno: %d\n", errno);
return -1;
}
/* Drop privileges */
if (geteuid() == 0) {
struct passwd *pw = getpwnam("nobody");
if (! pw) {
printf("getpwnam failed; errno: %d\n", errno);
return -2;
}
if ( setgid(pw->pw_gid) == -1 ) {
printf("setgid failed; errno: %d\n", errno);
return -3;
}
if ( setuid(pw->pw_uid) == -1 ) {
printf("setgid failed; errno: %d\n", errno);
return -4;
}
}
// on each message
while(1) {
char buffer[64 * 1024] = {0}; // 64kIB
struct sockaddr_in src_addr;
socklen_t src_addr_len = sizeof(src_addr);
int flags = 0;
ssize_t count = recvfrom(fd, &buffer, sizeof(buffer) - 1, flags, (struct sockaddr*) &src_addr, &src_addr_len);
if (count == -1) {
fprintf(stderr, "%s\n", strerror(errno));
} else {
// NUL-close the string
buffer[count] = '\0';
// Parse the logging message
// A typical message recieved is "<12>cron: this is some message"
int priority = LOG_NOTICE;
syslog(priority, "[%s:%d] %s", inet_ntoa(src_addr.sin_addr), ntohs(src_addr.sin_port), buffer);
}
}
}