summaryrefslogtreecommitdiff
path: root/minecctl/minecctl.c
blob: d6019800b8f8028ead91cbba9e78fb89d44ab164 (plain)
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
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdarg.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

#include "rcon-protocol.h"
#include "utils.h"
#include "config-parser.h"
#include "server-config-options.h"

void
__debug(enum debug_lvl lvl, const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	vfprintf(stderr, fmt, ap);
	va_end(ap);
}

__attribute__((noreturn)) void
__die(const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	vfprintf(stderr, fmt, ap);
	va_end(ap);

	exit(EXIT_FAILURE);
};

void *
__zmalloc(const char *fn, int line, size_t size)
{
	void *ptr;

	assert_die(!empty_str(fn) && line > 0 && size > 0, "invalid arguments");

	ptr = calloc(1, size);
	if (!ptr)
		die("malloc: %m");
	return ptr;
}

char *
__xstrdup(const char *fn, int line, const char *s)
{
	char *ptr;

	assert_die(!empty_str(fn) && line > 0 && !empty_str(s), "invalid arguments");

	ptr = strdup(s);
	if (!ptr)
		die("strdup: %m");
	return ptr;
}

char *
__xstrndup(const char *fn, int line, const char *s, size_t n)
{
	char *ptr;

	assert_die(!empty_str(fn) && line > 0 && !empty_str(s) && n > 0, "invalid arguments");

	ptr = strndup(s, n);
	if (ptr)
		die("strdup: %m");
	return ptr;
}

void
__xfree(const char *fn, int line, void *ptr)
{
	assert_die(!empty_str(fn) && line > 0, "invalid arguments");

	free(ptr);
}

static void
send_packet(int sfd, const char *buf, size_t len)
{
	size_t off = 0;
	ssize_t r;

	while (true) {
		r = write(sfd, buf + off, len - off);
		if (r < 0) {
			if (errno == EINTR)
				continue;
			else
				die("Failed to write packet: %m");
		}

		off += r;
		if (off == len)
			break;
	}
}

static void
read_packet(int sfd, char *buf, size_t len, int32_t *id, int32_t *type, const char **msg)
{
	size_t off = 0;
	size_t r;
	const char *error;

	while (true) {
		r = read(sfd, buf + off, len - off);
		if (r < 0) {
			if (errno == EINTR)
				continue;
			else
				die("Failed to read reply: %m");
		}

		if (r == 0)
			die("Failed, connection closed");

		off += r;
		if (rcon_protocol_packet_complete(buf, off))
			break;

		if (off >= len)
			die("Reply too large %zu and %zu", off, len);
	}

	if (!rcon_protocol_read_packet(buf, off, id, type, msg, &error))
		die("Failed to parse response: %s", error);
}

static void
send_msg(int sfd, char *buf, size_t len, enum rcon_packet_type type,
	 const char *msg, enum rcon_packet_type *rtype, const char **reply)
{
	static unsigned rcon_packet_id = 1;
	size_t plen;
	int32_t id;

	if (!rcon_protocol_create_packet(buf, len, &plen,
					 rcon_packet_id, type, msg))
		die("Failed to create rcon packet");

	send_packet(sfd, buf, plen);
	read_packet(sfd, buf, len, &id, rtype, reply);

	if (id != rcon_packet_id)
		die("Invalid reply id");

	rcon_packet_id++;
}

static void
send_login(int sfd, const char *password)
{
	char buf[4096];
	int32_t rtype;
	const char *reply;

	send_msg(sfd, buf, sizeof(buf), RCON_PACKET_LOGIN, password, &rtype, &reply);

	if (rtype == RCON_PACKET_LOGIN_OK)
		info("Login ok");
	else if (rtype == RCON_PACKET_LOGIN_FAIL)
		die("Login failure, invalid password?");
	else
		die("Invalid return code: %" PRIi32, rtype);
}

static void
send_cmd(int sfd, const char *cmd)
{
	char buf[4096];
	int32_t rtype;
	const char *reply;

	send_msg(sfd, buf, sizeof(buf), RCON_PACKET_COMMAND, "stop", &rtype, &reply);

	if (rtype == RCON_PACKET_RESPONSE)
		info("Command (%s) sent, reply: %s", cmd, reply);
	else
		die("Invalid return code: %" PRIi32, rtype);
}

static int
connect_any(struct list_head *addrs)
{
	struct saddr *saddr;
	bool connected = false;
	int sfd;

	list_for_each_entry(saddr, addrs, list) {
		sfd = socket(saddr->storage.ss_family, SOCK_STREAM | SOCK_CLOEXEC, 0);
		if (sfd < 0)
			die("socket: %m");

		socket_set_low_latency(sfd, true, true, true);

		if (connect(sfd, (struct sockaddr *)&saddr->storage, saddr->addrlen) < 0) {
			close(sfd);
			continue;
		}

		connected = true;
		break;
	}

	if (!connected)
		die("Failed to connect to remote host");

	return sfd;
}

static void
parse_config(char *buf, const char *filename,
	     const char **password, struct list_head *addrs)
{
	*password = NULL;
	list_init(addrs);

	/* FIXME: filename argument is superfluous */
	if (!config_parse_header(filename, SERVER_CFG_HEADER, &buf))
		die("Unable to parse %s: invalid header", filename);

	/* FIXME: this will cause superflous DNS lookups of other cfg entries */
	while (true) {
		int key;
		const char *keyname;
		struct cfg_value value;

		if (!config_parse_line(filename, &buf, scfg_key_map,
				       &key, &keyname, &value))
			break;

		switch (key) {
		case SCFG_KEY_RCON:
			list_replace(&value.saddrs, addrs);
			break;
		case SCFG_KEY_RCON_PASSWORD:
			*password = value.str;
			break;
		default:
			continue;
		}

		if (*password && !list_empty(addrs))
			break;
	}

	if (!*password)
		die("rcon password not found in %s", filename);

	if (list_empty(addrs))
		die("rcon address not found in %s", filename);
}

static void
read_file(const char *filename, char *buf, size_t len)
{
	int fd;
	size_t off = 0;
	ssize_t r;

	fd = open(filename, O_RDONLY | O_CLOEXEC);
	if (fd < 0)
		die("Failed to open %s: %m", filename);

	while (true) {
		r = read(fd, buf + off, len - off - 1);
		if (r < 0)
			die("Failed to read %s: %m", filename);
		else if (r == 0)
			break;

		off += r;
		if (off == len)
			die("Failed to read %s: file too large", filename);
	}

	buf[off] = '\0';
	close(fd);
}

int
main(int argc, char **argv)
{
	char buf[4096];
	const char *password;
	const char *filename;
	struct list_head addrs;
	struct saddr *saddr;
	int fd;

	debug_mask = DBG_ERROR | DBG_INFO;

	if (argc != 2)
		die("Usage: minecctl CFGFILE");

	filename = argv[1];

	read_file(filename, buf, sizeof(buf));

	parse_config(buf, filename, &password, &addrs);

	info("Password: %s", password);
	list_for_each_entry(saddr, &addrs, list)
		info("Address: %s", saddr->addrstr);

	fd = connect_any(&addrs);

	send_login(fd, password);

	send_cmd(fd, "stop");

	exit(EXIT_SUCCESS);
}