summaryrefslogtreecommitdiffstats
path: root/mulderd.c
blob: 4843ff69009600adce46234b47915f87f7f71fad (plain) (blame)
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
/*
 * mulderd.c
 * 
 * The truth is out there.
 *
 * I want to believe.
 *
 *
 * Copyright 2012 Jason A. Donenfeld <Jason@zx2c4.com>.
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <pwd.h>
#include <getopt.h>
#include <fcntl.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/prctl.h>
#include <sys/stat.h>
#include <sys/resource.h>
#include <netinet/in.h>

static char truth[] = "\033[32;1m\n\n"
"                 _,--=--._\n"
"               ,'    _    `.\n"
"              -    _(_)_o   -\n"
"         ____'    /_  _/]    `____\n"
"  -=====::(+):::::::::::::::::(+)::=====-\n"
"           (+).\"\"\"\"\"\"\"\"\"\"\"\"\",(+)\n"
"               .           ,\n"
"                 `  -=-  '\n"
"\n\n\n"
"          THE TRUTH IS OUT THERE\n\n\033[0m";

/*
 * Drops us into a chroot, if possible, and drops privs.
 */
void drop_privileges()
{
	struct passwd *user;
	struct rlimit limit;
	
	if (!geteuid()) {
		user = getpwnam("nobody");
		if (!user) {
			perror("getpwnam");
			exit(EXIT_FAILURE);
		}
		if (chroot("/var/empty")) {
			perror("chroot");
			exit(EXIT_FAILURE);
		}
		if (chdir("/")) {
			perror("chdir");
			exit(EXIT_FAILURE);
		}
		if (setresgid(user->pw_gid, user->pw_gid, user->pw_gid)) {
			perror("setresgid");
			exit(EXIT_FAILURE);
		}
		if (setgroups(1, &user->pw_gid)) {
			perror("setgroups");
			exit(EXIT_FAILURE);
		}
		if (setresuid(user->pw_uid, user->pw_uid, user->pw_uid)) {
			perror("setresuid");
			exit(EXIT_FAILURE);
		}
	}
	limit.rlim_cur = limit.rlim_max = 0;
	setrlimit(RLIMIT_CORE, &limit);
	setrlimit(RLIMIT_NPROC, &limit);
	if (!geteuid() || !getegid()) {
		fprintf(stderr, "Mysteriously still running as root... Goodbye.\n");
		exit(EXIT_FAILURE);
	}
}

int main(int argc, char *argv[])
{
	int listen_fd, connection_fd, flag;
	struct sockaddr_in6 listen_addr;
	struct sockaddr_storage connection_addr;
	struct timeval timeout;
	char ipaddr[INET6_ADDRSTRLEN];
	struct in6_addr *v6;
	time_t now;
	socklen_t connection_addr_len;
	pid_t child;
	int daemonize = 0, option_index = 0, port = 17, connection_file = -1, option;
	char *pid_file = 0, *connection_log = 0, *time_str;
	FILE *pidfile;

	static struct option long_options[] = {
		{"daemonize", no_argument, NULL, 'd'},
		{"foreground", no_argument, NULL, 'f'},
		{"port", required_argument, NULL, 'p'},
		{"pid-file", required_argument, NULL, 'P'},
		{"log-file", required_argument, NULL, 'l'},
		{"help", no_argument, NULL, 'h'},
		{0, 0, 0, 0}
	};

	close(STDIN_FILENO);

	while ((option = getopt_long(argc, argv, "dfP:l:p:h", long_options, &option_index)) != -1) {
		switch (option) {
			case 'd':
				daemonize = 1;
				break;
			case 'f':
				daemonize = 0;
				break;
			case 'p':
				port = atoi(optarg);
				break;
			case 'l':
				connection_log = optarg;
				break;
			case 'P':
				pid_file = optarg;
				break;
			case 'h':
			case '?':
			default:
				fprintf(stderr, "I Want to Believe by zx2c4\n\n");
				fprintf(stderr, "Usage: %s [OPTION]...\n", argv[0]);
				fprintf(stderr, "  -d, --daemonize              run as a background daemon\n");
				fprintf(stderr, "  -f, --foreground             run in the foreground (default)\n");
				fprintf(stderr, "  -P FILE, --pid-file=FILE     write pid of listener process to FILE\n");
				fprintf(stderr, "  -l FILE, --log-file=FILE     write connection log to FILE instead of stdout/stderr\n");
				fprintf(stderr, "  -p PORT, --port=PORT         listen on port PORT (default=17)\n");
				fprintf(stderr, "  -h, --help                   display this message\n");
				return option == 'h' ? EXIT_SUCCESS : EXIT_FAILURE;
		}
	}
	if (connection_log) {
		connection_file = open(connection_log, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
		if (connection_file < 0) {
			perror("open");
			return EXIT_FAILURE;
		}
		if (dup2(connection_file, STDOUT_FILENO) < 0) {
			perror("dup2");
			return EXIT_FAILURE;
		}
		if (dup2(connection_file, STDERR_FILENO) < 0) {
			perror("dup2");
			return EXIT_FAILURE;
		}
		close(connection_file);
		setbuf(stdout, NULL);
		setbuf(stderr, NULL);
	}
	
	listen_fd = socket(AF_INET6, SOCK_STREAM, 0);
	if (listen_fd < 0) {
		perror("socket");
		return EXIT_FAILURE;
	}
	flag = 1;
	setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag));
	flag = 0;
	setsockopt(listen_fd, IPPROTO_IPV6, IPV6_V6ONLY, &flag, sizeof(flag));
	
	memset(&listen_addr, 0, sizeof(listen_addr));
	listen_addr.sin6_family = AF_INET6;
	listen_addr.sin6_addr = in6addr_any;
	listen_addr.sin6_port = htons(port);
	if (bind(listen_fd, (struct sockaddr *)&listen_addr, sizeof(listen_addr)) < 0) {
		perror("bind");
		return EXIT_FAILURE;
	}
	if (listen(listen_fd, 15) < 0) {
		perror("listen");
		return EXIT_FAILURE;
	}

	if (pid_file) {
		pidfile = fopen(pid_file, "w");
		if (!pidfile) {
			perror("fopen");
			return EXIT_FAILURE;
		}
	}
	if (daemonize) {
		if (daemon(0, 1) < 0) {
			perror("daemon");
			return EXIT_FAILURE;
		}
		if (connection_file == -1) {
			close(STDERR_FILENO);
			close(STDOUT_FILENO);
		}
	}
	if (pid_file) {
		if (fprintf(pidfile, "%d\n", getpid()) < 0) {
			perror("fprintf");
			return EXIT_FAILURE;
		}
		fclose(pidfile);
	}
	
	drop_privileges();
	prctl(PR_SET_NAME, "truth beacon");
	timeout.tv_sec = 1;
	timeout.tv_usec = 0;

	while ((connection_addr_len = sizeof(connection_addr)) &&
		(connection_fd = accept(listen_fd, (struct sockaddr *)&connection_addr, &connection_addr_len)) >= 0) {
		now = time(NULL);
		if (connection_addr.ss_family == AF_INET6) {
			v6 = &(((struct sockaddr_in6 *)&connection_addr)->sin6_addr);
			if (v6->s6_addr32[0] == 0 && v6->s6_addr32[1] == 0 && v6->s6_addr16[4] == 0 && v6->s6_addr16[5] == 0xFFFF)
				inet_ntop(AF_INET, &v6->s6_addr32[3], ipaddr, INET_ADDRSTRLEN);
			else
				inet_ntop(AF_INET6, v6, ipaddr, INET6_ADDRSTRLEN);
		} else if (connection_addr.ss_family == AF_INET)
			inet_ntop(AF_INET, &(((struct sockaddr_in *)&connection_addr)->sin_addr), ipaddr, INET_ADDRSTRLEN);

		if (setsockopt(connection_fd, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)) < 0) {
			close(connection_fd);
			perror("setsockopt");
			continue;
		}
		time_str = ctime(&now);
		time_str[strlen(time_str) - 1] = 0;
		printf("%s\t%s\t%d\n", time_str, ipaddr, ntohs(((struct sockaddr_in6 *)&connection_addr)->sin6_port));
		if (write(connection_fd, truth, sizeof(truth)) < 0)
			perror("write");
		close(connection_fd);
	}

	close(listen_fd);
	return 0;
}