summaryrefslogtreecommitdiff
path: root/minecctl/misc-commands.c
blob: 752b0e9a0a62107d7b7c9bec1c84abbca42d27d2 (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
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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
/* SPDX-License-Identifier: GPL-2.0 */
#include <limits.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <sys/random.h>
#include <inttypes.h>

#include "shared/utils.h"
#include "minecctl.h"
#include "server.h"
#include "misc-commands.h"
#include "rcon-commands.h"
#include "mc-commands.h"
#include "shared/systemd.h"
#include "misc.h"

#include "examples/eula.txt.h"
#include "examples/minecctl.conf.h"
#include "examples/minecproxy.conf.h"
#include "examples/minecproxy.service.h"
#include "examples/minecserver@.service.h"
#include "examples/README.TXT.h"

static bool create_link(int dfd, const char *source, const char *target)
{
	if (symlinkat(target, dfd, source) < 0) {
		error("Unable to create link %s -> %s: %m", source, target);
		return false;
	}

	return true;
}

static bool write_cfg_file(int dfd, const char *name,
			   const unsigned char *content,
			   size_t len)
{
	_cleanup_close_ int fd;
	ssize_t done = 0, r;
	bool rv = true;

	fd = openat(dfd, name, O_WRONLY | O_CLOEXEC | O_CREAT | O_EXCL, 0644);
	if (fd < 0) {
		error("Unable to create file %s: %m", name);
		return false;
	}

	while (done < len) {
		r = write(fd, content + done, len - done);
		if (r < 0) {
		       	if (errno == EAGAIN || errno == EINTR)
				continue;
			error("Unable to write file %s: %m", name);
			rv = false;
			break;
		}

		done += r;
	}

	return rv;
}

static bool find_user_service(int xfd, const char *service)
{
	_cleanup_close_ int dfd = -1;
	_cleanup_free_ char *dpath = NULL;
	/* FIXME: Make this a macro, make paths #defines */
	char sub_path[STRLEN("systemd/user/") + strlen(service) + 1];
	char etc_path[STRLEN("/etc/systemd/user/") + strlen(service) + 1];
	char usr_path[STRLEN("/usr/lib/systemd/user/") + strlen(service) + 1];

	sprintf(sub_path, "systemd/user/%s", service);
	sprintf(etc_path, "/etc/systemd/user/%s", service);
	sprintf(usr_path, "/usr/lib/systemd/user/%s", service);

	/*
	 * We need to check (in order of precedence):
	 * ~/.config/systemd/user/	- user-created
	 * /etc/systemd/user/		- admin-created
	 * ~/.local/share/systemd/user/	- user-installed packages
	 * /usr/lib/systemd/user/	- system-installed packages
	 */

	if (faccessat(xfd, sub_path, R_OK, 0) == 0) {
		info("User service %s already installed in "
		     "$XDG_CONFIG_HOME/systemd/user/", service);
		return true;
	}

	if (access(etc_path, R_OK) == 0) {
		info("User service %s already installed in "
		     "/etc/systemd/user/", service);
		return true;
	}

	dfd = open_xdg_data_dir(false, &dpath);
	if (dfd >= 0) {
		if (faccessat(dfd, sub_path, R_OK, 0) == 0) {
			info("User service %s already installed in "
			     "%s/systemd/user/", service, dpath);
			return true;
		}
	}

	if (access(usr_path, R_OK) == 0) {
		info("User service %s already installed in "
		     "/usr/lib/systemd/user/", service);
		return true;
	}

	return false;
}

static bool create_user_service(int xfd, const char *service,
				unsigned char *content, size_t len)
{
	_cleanup_close_ int sfd = -1;
	_cleanup_close_ int ufd = -1;

	if (find_user_service(xfd, service))
		return true;

	sfd = open_subdir(xfd, "systemd", true);
	if (sfd < 0)
		return false;

	ufd = open_subdir(sfd, "user", true);
	if (ufd < 0)
		return false;

	if (!write_cfg_file(ufd, service, content, len))
		return false;

	info("Created user service file $XDG_CONFIG_HOME/systemd/user/%s",
	     service);
	return true;
}

static bool write_server_cfg(struct cfg *cfg, const char *name,
			     const char *filename,
			     const unsigned char *properties,
			     size_t properties_len,
			     const unsigned char *mcserver,
			     size_t mcserver_len)
{
	_cleanup_close_ int sfd = -1;
	int cfd, dfd;

	if (!cfg->cfg_dir || !cfg->data_dir)
		return false;

	cfd = dirfd(cfg->cfg_dir);
	dfd = dirfd(cfg->data_dir);

	sfd = open_subdir(dfd, name, true);
	if (sfd < 0) {
		error("Failed to create server directory \"%s\"", name);
		return false;
	}

	if (!write_cfg_file(cfd, filename, mcserver, mcserver_len))
		return false;

	if (!write_cfg_file(sfd, "eula.txt", ___examples_eula_txt,
			    ___examples_eula_txt_len))
		return false;

	if (!write_cfg_file(sfd, MC_SERVER_PROPERTIES,
			    properties, properties_len))
		return false;

	if (!create_link(sfd, "server.jar", "../server.jar"))
		return false;

	return true;
}

bool do_init(_unused_ struct cfg *cfg)
{
	_cleanup_close_ int xcfd = -1;
	_cleanup_close_ int mcfd = -1;
	_cleanup_close_ int xdfd = -1;
	_cleanup_close_ int mdfd = -1;

	xcfd = open_xdg_cfg_dir(true, NULL);
	if (xcfd < 0)
		return false;

	mcfd = open_subdir(xcfd, "minecproxy", true);
	if (mcfd < 0)
		return false;

	if (!write_cfg_file(mcfd, MINECPROXY_CFG_FILE,
			    ___examples_minecproxy_conf,
			    ___examples_minecproxy_conf_len))
		return false;

	if (!write_cfg_file(mcfd, MINECCTL_CFG_FILE,
			    ___examples_minecctl_conf,
			    ___examples_minecctl_conf_len))
		return false;

	if (!create_user_service(xcfd, SERVER_SYSTEMD_SERVICE,
				 ___examples_minecserver__service,
				 ___examples_minecserver__service_len))
		return false;

	if (!create_user_service(xcfd, MINECPROXY_SYSTEMD_SERVICE,
				 ___examples_minecproxy_service,
				 ___examples_minecproxy_service_len))
		return false;

	xdfd = open_xdg_data_dir(true, NULL);
	if (xdfd < 0)
		return false;

	mdfd = open_subdir(xdfd, "minecproxy", true);
	if (mdfd < 0)
		return false;

	if (!write_cfg_file(mdfd, "README.TXT", ___examples_README_TXT,
			    ___examples_README_TXT_len))
		return false;

	return true;
}

static bool valid_name(const char *name)
{
	const char *f = name;

	if (empty_str(name)) {
		error("Empty server name given");
		return false;
	}

	while (*f != '\0') {
		if ((*f >= 'a' && *f <= 'z') ||
		    (*f >= 'A' && *f <= 'Z') ||
		    (*f >= '0' && *f <= '9') ||
		    (*f == ':') || (*f == '-') ||
		    (*f == '_') || (*f == '.') ||
		    (*f == '\\')) {
			f++;
			continue;
		}
		error("Server name \"%s\" contains invalid characters", name);
		return false;
	}

	if ((f - name) > (256 - STRLEN("minecserver@"))) {
		error("Server name \"%s\" is too long", name);
		return false;
	}

	return true;
}

/* FIXME: Share this */
static inline char tohex(uint8_t val)
{
	static const char hex[] = "0123456789abcdef";

	return hex[val & 0x0f];
}

static bool generate_random_password(char *buf, size_t len)
{
	size_t written;
	ssize_t r;
	const char *s;
	char *d;

	if (len < 3)
		return false;

	char rnd[(len - 1) / 2];

	for (written = 0; written < sizeof(rnd); written += r) {
		r = getrandom(rnd + written, sizeof(rnd) - written, 0);
		if (r < 0) {
			if (errno == EAGAIN || errno == EINTR)
				continue;

			error("getrandom failure: %m");
			return false;
		}
	}

	for (s = rnd, d = buf; s < (rnd + sizeof(rnd)); s++) {
		*(d++) = tohex(*s >> 4);
		*(d++) = tohex(*s);
	}
	*d = '\0';

	return true;
}

static unsigned char *create_mc_server(const char *name, uint16_t local_port,
				       size_t *len)
{
	char *mcserver;

	/* FIXME: Add comments, commented out options */
	mcserver = xsprintf(len,
			    "[%s]\n"
			    "type = proxy\n"
			    "name = %s\n"
			    "local = %" PRIu16 "\n"
			    "idle_timeout = 600\n"
			    "start_method = systemd\n"
			    "stop_method = systemd\n",
			    SERVER_CONFIG_FILE_HEADER, name, local_port);
	if (!mcserver)
		error("xsprintf: %m");

	return (unsigned char *)mcserver;
}

static unsigned char *create_mc_properties(const char *name, uint16_t mc_port,
					   uint16_t rcon_port,
					   const char *rcon_password,
					   size_t *len)
{
	char hexrnd[16 + 1];
	char *prop;

	if (!rcon_password) {
		if (!generate_random_password(hexrnd, sizeof(hexrnd))) {
			error("Failed to generate random password");
			return NULL;
		}
	}

	prop = xsprintf(len,
			"#Minecraft server properties\n"
			"# This is a partial file, it will be replaced with a\n"
			"# fleshed out version the first time the Minecraft\n"
			"# server is executed.\n"
			"motd=%s\n"
			"#maybe uncomment the next line if you use minecproxy\n"
			"#server-ip=127.0.0.1\n"
			"server-port=%" PRIu16 "\n"
			"enable-rcon=true\n"
			"rcon.port=%" PRIu16 "\n"
			"rcon.password=%s\n",
			name, mc_port, rcon_port,
			rcon_password ? rcon_password : hexrnd);
	if (!prop)
		error("xsprintf: %m");

	return (unsigned char *)prop;
}

static uint16_t get_port(struct list_head *list)
{
	struct saddr *saddr;

	if (!list || list_empty(list))
		return 0;

	list_for_each_entry(saddr, list, list) {
		switch (saddr->st.ss_family) {
		case AF_INET:
			if (saddr->in4.sin_addr.s_addr == htonl(INADDR_LOOPBACK) ||
			    saddr->in4.sin_addr.s_addr == htonl(INADDR_ANY) ||
			    saddr->in4.sin_addr.s_addr == htonl(INADDR_BROADCAST))
				return htons(saddr->in4.sin_port);
			break;
		case AF_INET6:
			if (!memcmp(&saddr->in6.sin6_addr, &in6addr_any,
				    sizeof(saddr->in6.sin6_addr)) ||
			    !memcmp(&saddr->in6.sin6_addr, &in6addr_loopback,
				    sizeof(saddr->in6.sin6_addr)))
				return htons(saddr->in6.sin6_port);
			break;
		}
	}

	return 0;
}

static bool saddr_port_match(struct list_head *list, uint16_t port)
{
	struct saddr *a;

	list_for_each_entry(a, list, list) {
		switch (a->st.ss_family) {
		case AF_INET:
			if (htons(a->in4.sin_port) == port)
				return true;
			break;

		case AF_INET6:
			if (htons(a->in6.sin6_port) == port)
				return true;
			break;

		default:
			break;
		}
	}

	return false;
}

static bool select_free_ports(struct cfg *cfg, uint16_t *listen_port,
			      uint16_t *mc_port, uint16_t *rcon_port)
{
	uint16_t lport = cfg->listen_port_min;
	uint16_t mport = cfg->mc_port_min;
	uint16_t rport = cfg->rcon_port_min;
	struct server *server;
	bool used;

	for (lport = cfg->listen_port_min,
	     mport = cfg->mc_port_min,
	     rport = cfg->rcon_port_min;
	     lport <= cfg->listen_port_max &&
	     mport <= cfg->mc_port_max &&
	     rport <= cfg->rcon_port_max;
	     lport++, mport++, rport++) {

		used = false;

		list_for_each_entry(server, &cfg->servers, list) {
			if (!server->scfg.filename)
				continue;

			if (saddr_port_match(&server->scfg.locals, lport)) {
				used = true;
				break;
			}

			if (saddr_port_match(&server->scfg.remotes, mport)) {
				used = true;
				break;
			}

			if (saddr_port_match(&server->scfg.rcons, rport)) {
				used = true;
				break;
			}
		}

		if (!used) {
			debug(DBG_CFG, "found unused port, "
			      "listen: %" PRIu16 ", "
			      "mc: %" PRIu16 ", "
			      "rcon: %" PRIu16,
			      lport, mport, rport);

			if (listen_port && *listen_port == 0)
				*listen_port = lport;

			if (mc_port && *mc_port == 0)
				*mc_port = mport;

			if (rcon_port && *rcon_port == 0)
				*rcon_port = rport;

			return true;
		}
	}

	return false;
}

bool do_new(struct cfg *cfg)
{
	const char *name = cfg->commands[0];
	struct server *server;
	struct server *defserver = NULL;
	uint16_t local_port = 0, mc_port = 0, rcon_port = 0;
	const char *rcon_password = NULL;
	_cleanup_free_ unsigned char *properties = NULL;
	size_t properties_len;
	_cleanup_free_ unsigned char *mcserver = NULL;
	size_t mcserver_len;

	if (!valid_name(name))
		return false;

	char filename[strlen(name) + STRLEN(".") + STRLEN(SERVER_CONFIG_FILE_SUFFIX) + 1];
	sprintf(filename, "%s.%s", name, SERVER_CONFIG_FILE_SUFFIX);

	if (!server_read_all_configs(cfg, false)) {
		error("Failed to read all existing server configurations, "
		      "try running the \"lint\" command.");
		return false;
	}

	if (!cfg->cfg_dir || !cfg->data_dir)
		return false;

	list_for_each_entry(server, &cfg->servers, list) {
		if (!server->scfg.filename) {
			defserver = server;
			continue;
		}

		if (streq(server->scfg.filename, filename)) {
			error("Server \"%s\" already exists", name);
			return false;
		}
	}

	if (defserver) {
		rcon_port = get_port(&defserver->scfg.rcons);
		mc_port = get_port(&defserver->scfg.remotes);
		rcon_password = defserver->scfg.rcon_password;
	} else if (cfg->rcon_password)
		rcon_password = cfg->rcon_password;

	dump_config(cfg);

	if (!select_free_ports(cfg, &local_port, &mc_port, &rcon_port)) {
		error("Failed to find free port(s)");
		return false;
	}

	properties = create_mc_properties(name, mc_port, rcon_port,
					  rcon_password, &properties_len);
	if (!properties)
		return false;

	mcserver = create_mc_server(name, local_port, &mcserver_len);
	if (!mcserver)
		return false;

	if (!write_server_cfg(cfg, name, filename,
			      properties, properties_len,
			      mcserver, mcserver_len))
		return false;

	info("Created server configuration %s", name);
	return true;
}

bool do_list(struct cfg *cfg)
{
	struct server *server;

	server_load_all_known(cfg);

	/* server->scfg.filename check excludes servers created from cmdline */
	list_for_each_entry(server, &cfg->servers, list)
		if (server->scfg.filename)
			info("• %s", server->scfg.name);

	return true;
}

static bool saddr_match(struct list_head *la, struct list_head *lb)
{
	struct saddr *a, *b;

	list_for_each_entry(a, la, list) {
		list_for_each_entry(b, lb, list) {
			if (a->st.ss_family != b->st.ss_family)
				continue;

			switch (a->st.ss_family) {
			case AF_INET:
				if (memcmp(&a->in4.sin_addr,
					   &b->in4.sin_addr,
					   sizeof(a->in4.sin_addr)))
					continue;
				if (a->in4.sin_port != b->in4.sin_port)
					continue;
				return true;

			case AF_INET6:
				if (memcmp(&a->in6.sin6_addr,
					   &b->in6.sin6_addr,
					   sizeof(a->in6.sin6_addr)))
					continue;
				if (a->in6.sin6_port != b->in6.sin6_port)
					continue;
				return true;

			default:
				continue;
			}
		}
	}

	return false;
}

bool do_lint(struct cfg *cfg)
{
	struct server *a, *b;
	unsigned ia, ib;
	bool rv = true;

	rv = server_read_all_configs(cfg, true);

	dump_config(cfg);

	ia = 0;
	list_for_each_entry(a, &cfg->servers, list) {
		ib = 0;
		list_for_each_entry(b, &cfg->servers, list) {
			if (ib <= ia) {
				ib++;
				continue;
			}

			if (a->scfg.announce_port != 0 &&
			    b->scfg.announce_port != 0 &&
			    a->scfg.announce_port == b->scfg.announce_port)
				info("%sNote:%s %s and %s appear to have the "
				     "same announce port", ansi_red,
				     ansi_normal, a->scfg.name, b->scfg.name);

			if (saddr_match(&a->scfg.locals, &b->scfg.locals))
				info("%sNote:%s %s and %s appear to share at "
				     "least one local address/port pair",
				     ansi_red, ansi_normal, a->scfg.name,
				     b->scfg.name);

			if (saddr_match(&a->scfg.remotes, &b->scfg.remotes))
				info("%sNote:%s %s and %s appear to share at "
				     "least one remote address/port pair",
				     ansi_red, ansi_normal, a->scfg.name,
				     b->scfg.name);

			if (saddr_match(&a->scfg.rcons, &b->scfg.rcons))
				info("%sNote:%s %s and %s appear to share at "
				     "least one rcon address/port pair",
				     ansi_red, ansi_normal, a->scfg.name,
				     b->scfg.name);

			ib++;
		}
		ia++;
	}

	return rv;
}

bool do_pcount(struct cfg *cfg)
{
	unsigned online, max;
	struct server *server;
	const char *error;

	server = server_get_default(cfg);
	if (!server)
		return false;

	if (do_rcon_pcount(cfg, server, &online, &max, &error))
		info("Rcon says %u/%u", online, max);

	if (do_mc_pcount(cfg, server, &online, &max, &error))
		info("MC says %u/%u", online, max);

	return true;
}

static bool do_one_status(struct cfg *cfg, struct server *server)
{
	unsigned online, max;
	const char *error;
	bool rv = true;

	info("• %s", server->scfg.name);

	if (list_empty(&server->scfg.rcons))
		info("  rcon            : not configured");
	else if (do_rcon_pcount(cfg, server, &online, &max, &error))
		info("  rcon            : %sok%s", ansi_green, ansi_normal);
	else {
		info("  rcon            : %sfail%s (%s)",
		     ansi_red, ansi_normal, error);
		rv = false;
	}

	if (list_empty(&server->scfg.remotes))
		info("  mc              : not configured");
	else if (do_mc_pcount(cfg, server, &online, &max, &error))
		info("  mc              : %sok%s", ansi_green, ansi_normal);
	else {
		info("  mc              : %sfail%s (%s)",
		     ansi_red, ansi_normal, error);
		rv = false;
	}

	if (!server->scfg.systemd_service || !server->scfg.systemd_obj)
		info("  systemd service : not configured");
	else if (systemd_service_running(&server->scfg, &error))
		info("  systemd service : %sactive%s", ansi_green, ansi_normal);
	else {
		info("  systemd service : %sfail%s (%s)",
		     ansi_red, ansi_normal, error);
		rv = false;
	}
	info("");	

	return rv;
}

bool do_status(struct cfg *cfg)
{
	struct server *server;

	if (cfg->default_set) {
		server = server_get_default(cfg);
		if (!server) {
			error("failed to get default server");
			return false;
		}

		do_one_status(cfg, server);
	} else {
		server_read_all_configs(cfg, false);
		list_for_each_entry(server, &cfg->servers, list)
			do_one_status(cfg, server);
	}

	systemd_delete();

	return true;
}