forked from dun/conman
/
client-sock.c
451 lines (391 loc) · 13.2 KB
/
client-sock.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
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
/*****************************************************************************
* Written by Chris Dunlap <cdunlap@llnl.gov>.
* Copyright (C) 2007-2016 Lawrence Livermore National Security, LLC.
* Copyright (C) 2001-2007 The Regents of the University of California.
* UCRL-CODE-2002-009.
*
* This file is part of ConMan: The Console Manager.
* For details, see <https://dun.github.io/conman/>.
*
* ConMan is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* ConMan is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with ConMan. If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/
#if HAVE_CONFIG_H
# include <config.h>
#endif /* HAVE_CONFIG_H */
#include <assert.h>
#include <errno.h>
#include <sys/types.h> /* include before in.h for bsd */
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include "client.h"
#include "common.h"
#include "lex.h"
#include "log.h"
#include "util-file.h"
#include "util-net.h"
#include "util-str.h"
static void parse_rsp_ok(Lex l, client_conf_t *conf);
static void parse_rsp_err(Lex l, client_conf_t *conf);
int connect_to_server(client_conf_t *conf)
{
int sd;
struct sockaddr_in saddr;
char buf[MAX_LINE];
char *p;
assert(conf->req->host != NULL);
assert(conf->req->port > 0);
if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
log_err(errno, "Unable to create socket");
memset(&saddr, 0, sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_port = htons(conf->req->port);
if (host_name_to_addr4(conf->req->host, &saddr.sin_addr) < 0) {
conf->errnum = CONMAN_ERR_LOCAL;
conf->errmsg = create_format_string(
"Unable to resolve host <%s>", conf->req->host);
return(-1);
}
if (host_name_to_cname(conf->req->host, buf, sizeof(buf)) == NULL) {
conf->req->fqdn = create_string(conf->req->host);
}
else {
conf->req->fqdn = create_string(buf);
free(conf->req->host);
if ((p = strchr(buf, '.')))
*p = '\0';
conf->req->host = create_string(buf);
}
if (connect(sd, (struct sockaddr *) &saddr, sizeof(saddr)) < 0) {
conf->errnum = CONMAN_ERR_LOCAL;
conf->errmsg = create_format_string(
"Unable to connect to <%s:%d>: %s",
conf->req->fqdn, conf->req->port, strerror(errno));
return(-1);
}
conf->req->sd = sd;
return(0);
}
int send_greeting(client_conf_t *conf)
{
char buf[MAX_SOCK_LINE] = ""; /* init buf for appending with NUL */
int n;
assert(conf->req->sd >= 0);
assert(conf->req->user != NULL);
n = append_format_string(buf, sizeof(buf), "%s %s='%s'",
proto_strs[LEX_UNTOK(CONMAN_TOK_HELLO)],
proto_strs[LEX_UNTOK(CONMAN_TOK_USER)], lex_encode(conf->req->user));
if (conf->req->tty) {
n = append_format_string(buf, sizeof(buf), " %s='%s'",
proto_strs[LEX_UNTOK(CONMAN_TOK_TTY)], lex_encode(conf->req->tty));
}
n = append_format_string(buf, sizeof(buf), "\n");
if (n < 0) {
conf->errnum = CONMAN_ERR_LOCAL;
conf->errmsg = create_string(
"Overran request buffer for sending greeting");
return(-1);
}
if (write_n(conf->req->sd, buf, strlen(buf)) < 0) {
conf->errnum = CONMAN_ERR_LOCAL;
conf->errmsg = create_format_string(
"Unable to send greeting to <%s:%d>: %s",
conf->req->host, conf->req->port, strerror(errno));
return(-1);
}
if (recv_rsp(conf) < 0) {
if (conf->errnum == CONMAN_ERR_AUTHENTICATE) {
/*
* FIXME: NOT_IMPLEMENTED_YET
*/
if (close(conf->req->sd) < 0)
log_err(errno, "Unable to close connection to <%s:%d>",
conf->req->host, conf->req->port);
conf->req->sd = -1;
}
return(-1);
}
return(0);
}
int send_req(client_conf_t *conf)
{
char buf[MAX_SOCK_LINE] = ""; /* init buf for appending with NUL */
int n;
char *cmd = NULL;
char *str;
assert(conf->req->sd >= 0);
switch(conf->req->command) {
case CONMAN_CMD_QUERY:
cmd = proto_strs[LEX_UNTOK(CONMAN_TOK_QUERY)];
break;
case CONMAN_CMD_MONITOR:
cmd = proto_strs[LEX_UNTOK(CONMAN_TOK_MONITOR)];
break;
case CONMAN_CMD_CONNECT:
cmd = proto_strs[LEX_UNTOK(CONMAN_TOK_CONNECT)];
break;
default:
log_err(0, "INTERNAL: Invalid command=%d", conf->req->command);
break;
}
n = append_format_string(buf, sizeof(buf), "%s", cmd);
if (conf->req->enableQuiet) {
n = append_format_string(buf, sizeof(buf), " %s=%s",
proto_strs[LEX_UNTOK(CONMAN_TOK_OPTION)],
proto_strs[LEX_UNTOK(CONMAN_TOK_QUIET)]);
}
if (conf->req->enableRegex) {
n = append_format_string(buf, sizeof(buf), " %s=%s",
proto_strs[LEX_UNTOK(CONMAN_TOK_OPTION)],
proto_strs[LEX_UNTOK(CONMAN_TOK_REGEX)]);
}
if (conf->req->command == CONMAN_CMD_CONNECT) {
if (conf->req->enableForce) {
n = append_format_string(buf, sizeof(buf), " %s=%s",
proto_strs[LEX_UNTOK(CONMAN_TOK_OPTION)],
proto_strs[LEX_UNTOK(CONMAN_TOK_FORCE)]);
}
if (conf->req->enableJoin) {
n = append_format_string(buf, sizeof(buf), " %s=%s",
proto_strs[LEX_UNTOK(CONMAN_TOK_OPTION)],
proto_strs[LEX_UNTOK(CONMAN_TOK_JOIN)]);
}
if (conf->req->enableBroadcast) {
n = append_format_string(buf, sizeof(buf), " %s=%s",
proto_strs[LEX_UNTOK(CONMAN_TOK_OPTION)],
proto_strs[LEX_UNTOK(CONMAN_TOK_BROADCAST)]);
}
}
/* Empty the consoles list here because it will be filled in
* with the actual console names in recv_rsp().
*/
while ((str = list_pop(conf->req->consoles))) {
n = append_format_string(buf, sizeof(buf), " %s='%s'",
proto_strs[LEX_UNTOK(CONMAN_TOK_CONSOLE)], lex_encode(str));
free(str);
}
n = append_format_string(buf, sizeof(buf), "\n");
if (n < 0) {
conf->errnum = CONMAN_ERR_LOCAL;
conf->errmsg = create_string("Overran request buffer");
return(-1);
}
if (write_n(conf->req->sd, buf, strlen(buf)) < 0) {
conf->errnum = CONMAN_ERR_LOCAL;
conf->errmsg = create_format_string(
"Unable to send greeting to <%s:%d>: %s",
conf->req->host, conf->req->port, strerror(errno));
return(-1);
}
/* For QUERY commands, the write-half of the socket
* connection can be closed once the request is sent.
*/
if (conf->req->command == CONMAN_CMD_QUERY) {
if (shutdown(conf->req->sd, SHUT_WR) < 0) {
conf->errnum = CONMAN_ERR_LOCAL;
conf->errmsg = create_format_string(
"Unable to close write-half of connection to <%s:%d>: %s",
conf->req->host, conf->req->port, strerror(errno));
return(-1);
}
}
return(0);
}
int recv_rsp(client_conf_t *conf)
{
char buf[MAX_SOCK_LINE];
int n;
Lex l;
int done = 0;
int tok;
assert(conf->req->sd >= 0);
if ((n = read_line(conf->req->sd, buf, sizeof(buf))) < 0) {
conf->errnum = CONMAN_ERR_LOCAL;
conf->errmsg = create_format_string("Unable to read response"
" from <%s:%d>:\n %s (blocked by TCP-Wrappers?)",
conf->req->host, conf->req->port, strerror(errno));
return(-1);
}
else if (n == 0) {
conf->errnum = CONMAN_ERR_LOCAL;
conf->errmsg = create_format_string("Connection terminated by <%s:%d>",
conf->req->host, conf->req->port);
return(-1);
}
l = lex_create(buf, proto_strs);
while (!done) {
tok = lex_next(l);
switch(tok) {
case CONMAN_TOK_OK: /* OK, so ignore rest of line */
parse_rsp_ok(l, conf);
done = 1;
break;
case CONMAN_TOK_ERROR:
parse_rsp_err(l, conf);
done = -1;
break;
case LEX_EOF:
case LEX_EOL:
done = -1;
break;
default: /* ignore unrecognized tokens */
break;
}
}
lex_destroy(l);
if (done == 1)
return(0);
if (conf->errnum == CONMAN_ERR_NONE) {
conf->errnum = CONMAN_ERR_LOCAL;
conf->errmsg = create_format_string("Received invalid reponse from"
" <%s:%d>", conf->req->host, conf->req->port);
}
return(-1);
}
static void parse_rsp_ok(Lex l, client_conf_t *conf)
{
int tok;
int done = 0;
char *str;
while (!done) {
tok = lex_next(l);
switch (tok) {
case CONMAN_TOK_CONSOLE:
if ((lex_next(l) == '=') && (lex_next(l) == LEX_STR)) {
if ((str = lex_decode(create_string(lex_text(l)))))
list_append(conf->req->consoles, str);
}
break;
case CONMAN_TOK_OPTION:
if (lex_next(l) == '=') {
if (lex_next(l) == CONMAN_TOK_RESET)
conf->req->enableReset = 1;
}
break;
case LEX_EOF:
case LEX_EOL:
done = 1;
break;
default:
break; /* ignore unrecognized tokens */
}
}
return;
}
static void parse_rsp_err(Lex l, client_conf_t *conf)
{
int tok;
int done = 0;
int err = 0;
char buf[MAX_LINE] = "";
while (!done) {
tok = lex_next(l);
switch (tok) {
case CONMAN_TOK_CODE:
if ((lex_next(l) == '=') && (lex_next(l) == LEX_INT))
err = atoi(lex_text(l));
break;
case CONMAN_TOK_MESSAGE:
if ((lex_next(l) == '=') && (lex_next(l) == LEX_STR))
strlcpy(buf, lex_text(l), sizeof(buf));
break;
case LEX_EOF:
case LEX_EOL:
done = 1;
break;
default:
break; /* ignore unrecognized tokens */
}
}
conf->errnum = err;
if (*buf)
conf->errmsg = lex_decode(create_string(buf));
return;
}
void display_error(client_conf_t *conf)
{
char *p;
assert(conf->errnum > 0);
p = create_format_string("ERROR: %s.\n\n",
(conf->errmsg ? conf->errmsg : "Unspecified"));
if (write_n(STDERR_FILENO, p, strlen(p)) < 0)
log_err(errno, "Unable to write to stderr");
if (conf->logd >= 0)
if (write_n(conf->logd, p, strlen(p)) < 0)
log_err(errno, "Unable to write to \"%s\"", conf->log);
free(p);
if (conf->errnum != CONMAN_ERR_LOCAL)
display_data(conf, STDERR_FILENO);
if ((conf->errnum == CONMAN_ERR_TOO_MANY_CONSOLES)
&& (!conf->req->enableBroadcast))
p = "\nDo you want to broadcast (-b) to multiple consoles?\n\n";
else if ((conf->errnum == CONMAN_ERR_BUSY_CONSOLES)
&& ((!conf->req->enableForce) && (!conf->req->enableJoin)))
p = "\nDo you want to force (-f) or join (-j) the connection?\n\n";
else
p = NULL;
if (p) {
if (write_n(STDERR_FILENO, p, strlen(p)) < 0)
log_err(errno, "Unable to write to stderr");
if (conf->logd >= 0)
if (write_n(conf->logd, p, strlen(p)) < 0)
log_err(errno, "Unable to write to \"%s\"", conf->log);
}
exit(2);
}
void display_data(client_conf_t *conf, int fd)
{
char buf[MAX_BUF_SIZE];
int n;
assert(fd >= 0);
if (conf->req->sd < 0)
return;
for (;;) {
n = read(conf->req->sd, buf, sizeof(buf));
if (n < 0)
log_err(errno, "Unable to read from <%s:%d>",
conf->req->host, conf->req->port);
if (n == 0)
break;
if (write_n(fd, buf, n) < 0)
log_err(errno, "Unable to write to fd=%d", fd);
if (conf->logd >= 0)
if (write_n(conf->logd, buf, n) < 0)
log_err(errno, "Unable to write to \"%s\"", conf->log);
}
return;
}
void display_consoles(client_conf_t *conf, int fd)
{
ListIterator i;
char *p;
char buf[MAX_LINE];
int n;
i = list_iterator_create(conf->req->consoles);
while ((p = list_next(i))) {
n = snprintf(buf, sizeof(buf), "%s\n", p);
if (n < 0 || n >= sizeof(buf))
log_err(0, "Got console list buffer overrun");
if (write_n(fd, buf, n) < 0)
log_err(errno, "Unable to write to fd=%d", fd);
if (conf->logd >= 0)
if (write_n(conf->logd, buf, n) < 0)
log_err(errno, "Unable to write to \"%s\"", conf->log);
}
list_iterator_destroy(i);
return;
}