-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsrcon.c
More file actions
491 lines (404 loc) · 9.88 KB
/
srcon.c
File metadata and controls
491 lines (404 loc) · 9.88 KB
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
/*
Source Server Remote Console v1.0.5
by lxndr (lxndr87i@gmail.com)
GPLv3
Home page: https://github.com/lxndr/srcon
*/
#define _GNU_SOURCE
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>
#include <signal.h>
#include <poll.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <readline/readline.h>
#include <readline/history.h>
#define RCON_OUT_AUTH 3
#define RCON_OUT_EXEC 2
#define RCON_IN_AUTH 2
#define RCON_IN_RESPONSE 0
#define RCON_ID 0
#define RCON_END_ID 1
#define EXIT_AUTHFAIL 2
static char *prompt = NULL;
static int running = 1;
static int interactive = 0;
static int quiet = 0;
static int sock = 0;
static char *command = NULL;
static void
rl_clean ()
{
rl_set_prompt ("");
rl_replace_line ("", 0);
rl_redisplay ();
}
static void
print (int quiet, int inside_readline, const char *fmt, ...)
{
int point;
char *line;
if (quiet)
return;
if (!interactive)
inside_readline = 0;
/* save readline state */
if (inside_readline) {
point = rl_point;
line = rl_copy_text (0, rl_end);
rl_clean ();
}
/* print */
va_list va;
va_start (va, fmt);
vprintf (fmt, va);
va_end (va);
/* restore readline state */
if (inside_readline) {
rl_set_prompt (prompt);
rl_replace_line (line, 0);
rl_point = point;
rl_redisplay ();
free (line);
}
}
static void
parse_address (const char *addr, char *node, int maxlen, char **port)
{
int len;
char *f = strchr (addr, ':');
len = f ? f - addr : strlen (addr);
if (len > maxlen - 1)
len = maxlen - 1;
strncpy (node, addr, len);
node[len] = 0;
*port = f ? (f + 1) : "27015";
}
static int
establish_connection (const char *node, const char *service)
{
print (quiet, 0, "Connecting to %s:%s... ", node, service);
fflush (stdout);
struct addrinfo *res = NULL;
int ret = getaddrinfo (node, service, NULL, &res);
if (ret != 0) {
print (quiet, 0, "FAILED!\n");
print (quiet, 0, "Could not resolve the address: %s\n", gai_strerror (ret));
return 0;
}
sock = socket (res->ai_family, res->ai_socktype, res->ai_protocol);
if (connect (sock, res->ai_addr, res->ai_addrlen) == -1) {
print (quiet, 0, "FAILED!\n");
print (quiet, 0, "Could not connect to the server: %s\n", strerror (errno));
return 0;
}
freeaddrinfo (res);
print (quiet, 0, "done.\n");
return 1;
}
static int
send_packet (int id, int type, const char *cmd)
{
size_t len = strlen (cmd);
int size = len + 10;
/* form packet data */
char *data = malloc (size + 4);
* (int *) (data + 0) = size;
* (int *) (data + 4) = id;
* (int *) (data + 8) = type;
memcpy (data + 12, cmd, len);
* (short *) (data + size + 2) = 0;
int ret = send (sock, data, size + 4, 0);
if (ret == 0) {
print (quiet, 0, "Connection closed\n");
running = 0;
} else if (ret == -1) {
print (quiet, 0, "Error while sending: %s\n", strerror (errno));
running = 0;
}
free (data);
return ret;
}
static int
recv_packet (int *id, int *type, char *text)
{
int size, ret;
ret = recv (sock, &size, 4, MSG_WAITALL);
if (ret <= 0)
return ret;
char *data = malloc (size);
ret = recv (sock, data, size, MSG_WAITALL);
if (ret <= 0)
return ret;
*id = * (int *) data;
*type = * (int *) (data + 4);
memcpy (text, data + 8, size - 9); /* this also copies terminating null */
free (data);
return ret;
}
static void
on_ready ()
{
/* send commands specified on command line with (-c option) */
if (command) {
print (quiet, 0, "Sending commands from option...\n");
send_packet (RCON_ID, RCON_OUT_EXEC, command);
command = NULL;
}
/* in interactive mode, readline takes care about stdin */
if (!interactive) {
if (!isatty (fileno(stdin))) { /* pipe is used */
print (quiet, 0, "Sending commands from stdin...\n");
char line[1024];
while (!feof (stdin) && fgets (line, 1024, stdin)) {
/* PARANOID: fgets also gets a \n. a few tests showed that
servers don't care about it. but just in case, let's remove it */
/* TODO: it would be nice to pack all commands in one or more packets */
size_t last = strlen (line) - 1;
if (line[last] == '\n')
line[last] = 0;
send_packet (RCON_ID, RCON_OUT_EXEC, line);
}
}
}
/* there's no way to know if a response is a single packet or several.
so we send an extra packetwith different ID so we can know
where response ends. */
send_packet (RCON_END_ID, RCON_OUT_EXEC, "");
}
static void
process_response ()
{
int ret, id, type;
char text[4096];
ret = recv_packet (&id, &type, text);
if (ret == 0) {
print (quiet, 1, "Connection closed\n");
running = 0;
return;
} else if (ret == -1) {
print (quiet, 1, "Error while recieveing: %s\n", strerror (errno));
running = 0;
return;
}
if (type == RCON_IN_AUTH) {
if (id == -1) {
print (quiet, 1, "Authentication attempt failed\n");
running = 0;
} else {
print (quiet, 1, "Successfully authenticated.\n");
on_ready ();
}
} else if (type == RCON_IN_RESPONSE) {
if (!interactive && id == RCON_END_ID) {
running = 0;
return;
}
if (*text) {
print (0, 1, text);
size_t len = strlen (text);
if (text[len - 1] != '\n')
print (0, 1, "\n");
}
}
}
static void
handle_line (char* line)
{
/* FIXME: line == NULL means the end of stdin stream.
this happens when pipe is used in interactive mode. */
if (!line) {
running = 0;
return;
}
if (*line) {
if (strcmp (line, "logout") == 0) {
running = 0;
/* disabled it. had a few troubles
} else if (strcmp (line, "exit") == 0 || strcmp (line, "quit") == 0) {
char *answer = readline ("This command will shut down the server. Are you sure you want it? [yes/NO]: ");
if (strcmp (answer, "yes") == 0)
send_packet (RCON_OUT_EXEC, line);
else if (*answer == 'y')
printf ("Why 'why'? This is a serious matter. Please type 'yes' if you are sure.\n");
free (answer);
*/
} else {
add_history (line);
send_packet (RCON_ID, RCON_OUT_EXEC, line);
}
}
free (line);
}
static void
form_prompt (const char *host, const char *port, const char *color)
{
asprintf (&prompt, "\033[%smrcon@\033[0m%s:%s \033[%sm>\033[0m ",
color, host, port, color);
}
static void
interactive_mode ()
{
char *history_file = NULL;
/* history file path */
char *homedir = getenv ("HOME");
if (homedir && *homedir) {
const char *fname = "/.srcon_history";
size_t len = strlen (homedir);
history_file = malloc (len + strlen (fname) + 1);
memcpy (history_file, homedir, len + 1);
strcat (history_file, fname);
}
/* initialize readline */
using_history ();
if (history_file)
read_history (history_file);
rl_callback_handler_install (prompt, handle_line);
struct pollfd fds[2] = {
{STDIN_FILENO, POLLIN, 0},
{sock, POLLIN, 0}
};
while (running) {
int ret = poll (fds, 2, -1);
if (ret > 0) {
if (fds[1].revents & POLLIN)
process_response ();
if (fds[1].revents & POLLERR) {
print (quiet, 1, "POLLERR: An error has occured on the device or stream.\n");
running = 0;
}
if (fds[1].revents & POLLHUP) {
print (quiet, 1, "POLLHUP: The device has been disconnected.\n");
running = 0;
}
if (fds[0].revents & POLLIN)
rl_callback_read_char ();
else if (fds[0].revents & POLLHUP)
running = 0;
} else if (ret == -1) {
if (ret != EINTR)
print (quiet, 1, "A poll error has accured: %s\n", strerror (errno));
}
}
rl_clean ();
rl_callback_handler_remove ();
if (history_file) {
write_history (history_file);
free (history_file);
}
}
static void
query_mode ()
{
struct pollfd fd = {sock, POLLIN, 0};
while (running) {
int ret = poll (&fd, 1, -1);
if (ret > 0) {
if (fd.revents & POLLIN)
process_response ();
if (fd.revents & POLLERR) {
print (quiet, 1, "POLLERR: An error has occured on the device or stream.\n");
running = 0;
}
if (fd.revents & POLLHUP) {
print (quiet, 1, "POLLHUP: The device has been disconnected.\n");
running = 0;
}
} else if (ret == -1) {
if (ret != EINTR)
print (quiet, 1, "A poll error has accured: %s\n", strerror (errno));
}
}
}
static void
print_help ()
{
puts ("Usage: srcon [OPTIONS] HOST[:PORT]\n"
"Connect to a Valve Source Server via RCon protocol.\n\n"
"Options:\n"
" -p <password> rcon password\n"
" -h show this help message and exit\n"
" -v show version information and exit\n"
" -i interactive shell mode\n"
" -c command(s) to send on startup\n"
" -t '<color>' shell prompt color\n"
" -q only show response");
}
static void
print_version ()
{
puts ("Source Server Remote Console v1.0.5\n"
"by Alexander AB (lxndr87i@gmail.com)");
}
int
main (int argc, char **argv)
{
int opt;
char *address, *password = NULL, *color = NULL;
/* command line setting */
while ((opt = getopt (argc, argv, "hvp:c:iqt:")) != -1) {
switch (opt) {
case 'h':
print_help ();
return EXIT_SUCCESS;
case 'v':
print_version ();
return EXIT_SUCCESS;
case 'p':
password = optarg;
break;
case 'c':
command = optarg;
break;
case 'i':
interactive = 1;
break;
case 'q':
quiet = 1;
break;
case 't':
color = optarg;
break;
default:
print_help ();
return EXIT_FAILURE;
}
}
address = argv[optind];
if (!address) {
print_help ();
return EXIT_SUCCESS;
}
if (!password) {
print (quiet, 0, "Password option (-p) requiered\n");
return EXIT_AUTHFAIL;
}
/* connection */
char host[128];
char *port;
parse_address (address, host, 128, &port);
if (!establish_connection (host, port))
return EXIT_FAILURE;
/* password packet */
send_packet (RCON_ID, RCON_OUT_AUTH, password);
if (interactive) {
if (!color)
color = "0;31";
form_prompt (host, port, color);
interactive_mode ();
if (prompt)
free (prompt);
} else {
query_mode ();
}
print (quiet, 0, "Disconnecting... ");
fflush (stdout);
close (sock);
print (quiet, 0, "done.\n");
return EXIT_SUCCESS;
}