-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
284 lines (237 loc) · 5.57 KB
/
main.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
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
/*
* Remote Console (RCON)
*
* This application facilitates communication with servers adhering to the
* Source RCON Protocol as defined on the Valve Developer website at
* https://developer.valvesoftware.com/wiki/Source_RCON_Protocol.
*
* This application expects to find a configuration file named
* ~/.config/rcon/rcon.conf. The configuration file should contain an entry
* for each RCON capable server you want to access with this utility, using the
* format:
*
* name,IP address,port[,password]
*
* The user will be prompted for the server's RCON password at runtime if
* a password is not provided in the configuration file.
*
* Call the application from the command-line with two arguments:
*
* target The name of the server to contact. This is the
* 'name' field in the configuration example above.
*
* command Command to send to that server.
*
* For example:
*
* $ rcon my_server status
*
* will send the command "status" to the server named "my_server" in the
* ~/.rcon configuration file. The configuration file entry for this example
* might be:
*
* my_server,127.0.0.1,21515
*
* or
*
* my_server,127.0.0.1,21515,P4S5W0RD
*
* where "P4S5W0RD" is the server's RCON password.
*/
#include "main.h"
#include "rcon.h"
int main(int argc, char **argv)
{
size_t command_len = 0;
char *command; /* Command to send */
char *response; /* Server's response */
int rval;
char *target; /* Server's alias in the config file */
/*
* Process command line.
*/
if (argc < 3) {
puts("usage: rcon <target> <command>");
return EINVAL;
}
target = argv[1];
for (int i = 2; i < argc; ++i)
command_len += strlen(argv[i]) + 1;
command = (char *) calloc(command_len, sizeof(char));
if (command == NULL) {
perror("calloc");
return errno;
}
/* Concatenate args as a single string. */
for (int i=2; i < argc; ++i) {
strcat(command, argv[i]);
if (i + 1 < argc)
strcat(command, " ");
}
/*
* Process configuration file.
*/
rval = load_config(target);
if (rval) {
free(command);
fprintf(stderr, "load_config: %s\n", strerror(rval));
return rval;
}
/*
* Transaction.
*/
rval = rcon_send(command, SERVERDATA_EXECCOMMAND);
free(command);
if (rval) {
errno = rval;
perror("rcon_send");
return rval;
}
response = rcon_recv();
if (response == NULL) {
rval = errno;
free(response);
perror("rcon_recv()");
return rval;
}
puts(response);
free(response);
return 0;
}
/*
* Searches the provided file for the named target. On success,
* a pointer to the configuration line is returned, which must be
* freed later. On failure, NULL is returned.
*/
char * find_config(FILE *file, const char *target)
{
bool eof = false;
char *line = NULL;
size_t line_len = 0;
int rval;
while (eof == false) {
/* Let getline() allocate the buffer. */
rval = getline(&line, &line_len, file);
if (rval == -1)
eof = true;
if (line_len) {
if (strncmp(line, target, strlen(target)) == 0) {
break;
}
}
free(line);
line = NULL;
line_len = 0;
}
return line;
}
/*
* Prompt the user for a password, and return it. The caller must free the
* provided string.
*/
char * get_password(void)
{
char input;
size_t max_length = 128;
char *password = calloc(max_length, sizeof(char));
struct termios term;
printf("Enter password: ");
/* Disable terminal echo */
tcgetattr(fileno(stdin), &term);
term.c_lflag &= ~ECHO;
tcsetattr(fileno(stdin), 0, &term);
for (int i = 0; i < max_length; ++i) {
input = getchar();
if (input == '\n') {
printf("\n");
break;
}
password[i] = input;
}
/* Enable terminal echo */
term.c_lflag |= ECHO;
tcsetattr(fileno(stdin), 0, &term);
return password;
}
/*
* Obtain the entry for the provided target from the configuration file, and
* call rcon_init() with the details. Returns zero on success, and errno on
* failure.
*
* The configuration file is expected as ~/.config/rcon/rcon.conf and contains one line
* for each server entry using this format:
*
* target_name,IP address,port,password
*/
int load_config(const char *target)
{
FILE *file;
char *line;
size_t rval;
file = open_config();
if (file == NULL)
return errno;
line = find_config(file, target);
if (line == NULL)
return errno;
rval = parse_config(line);
free(line);
return rval;
}
/*
* Provides a file pointer to ~/.config/rcon/rcon.conf
*
* Returns NULL on failure.
*/
FILE * open_config(void)
{
FILE *file;
char *home_dir;
char *path;
errno = 0; /* Per getpwuid man page */
home_dir = getpwuid(getuid())->pw_dir;
path = calloc(strlen(home_dir) + 24, sizeof(char));
if (path == NULL)
return NULL;
strcpy(path, home_dir);
strcat(path, "/.config/rcon/rcon.conf");
file = fopen(path, "r");
free(path);
return file;
}
/*
* Parse the provided configuration entry and call rcon_init(). The user is
* prompted for a password if one is not provided in the configuration line.
*
* Returns zero on success and errno on failure.
*/
int parse_config(char *config_entry)
{
char *address;
uint16_t port;
char *password;
int last;
int rval;
strtok(config_entry, ","); /* Ignore the name entry */
address = strtok(NULL, ",");
port = atoi(strtok(NULL, ","));
password = strtok(NULL, ",");
/* strip newline from password, if we have one. */
if (password) {
last = strlen(password) - 1;
if (password[last] == '\n')
password[last] = '\0';
}
rval = rcon_init(address, port);
if (rval) {
return rval;
}
if (password == NULL) {
password = get_password();
rval = rcon_auth(password);
free(password);
} else {
rval = rcon_auth(password);
}
return rval;
}