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
|
/*
** Zabbix
** Copyright (C) 2001-2019 Zabbix SIA
**
** This program 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 2 of the License, or
** (at your option) any later version.
**
** This program 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 this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
#include "common.h"
#include "comms.h"
#include "telnet.h"
#include "log.h"
static char prompt_char = '\0';
static int telnet_waitsocket(ZBX_SOCKET socket_fd, int mode)
{
const char *__function_name = "telnet_waitsocket";
struct timeval tv;
int rc;
fd_set fd, *readfd = NULL, *writefd = NULL;
zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
tv.tv_sec = 0;
tv.tv_usec = 100000; /* 1/10 sec */
FD_ZERO(&fd);
FD_SET(socket_fd, &fd);
if (WAIT_READ == mode)
readfd = &fd;
else
writefd = &fd;
rc = select(ZBX_SOCKET_TO_INT(socket_fd) + 1, readfd, writefd, NULL, &tv);
if (ZBX_PROTO_ERROR == rc)
{
zabbix_log(LOG_LEVEL_DEBUG, "%s() rc:%d errno:%d error:[%s]", __function_name, rc,
zbx_socket_last_error(), strerror_from_system(zbx_socket_last_error()));
}
zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%d", __function_name, rc);
return rc;
}
static ssize_t telnet_socket_read(ZBX_SOCKET socket_fd, void *buf, size_t count)
{
const char *__function_name = "telnet_socket_read";
ssize_t rc;
int error;
zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
while (ZBX_PROTO_ERROR == (rc = ZBX_TCP_READ(socket_fd, buf, count)))
{
error = zbx_socket_last_error(); /* zabbix_log() resets the error code */
zabbix_log(LOG_LEVEL_DEBUG, "%s() rc:%ld errno:%d error:[%s]",
__function_name, (long int)rc, error, strerror_from_system(error));
#ifdef _WINDOWS
if (WSAEWOULDBLOCK == error)
#else
if (EAGAIN == error)
#endif
{
/* wait and if there is still an error or no input available */
/* we assume the other side has nothing more to say */
if (1 > (rc = telnet_waitsocket(socket_fd, WAIT_READ)))
goto ret;
continue;
}
break;
}
/* when ZBX_TCP_READ returns 0, it means EOF - let's consider it a permanent error */
/* note that if telnet_waitsocket() is zero, it is not a permanent condition */
if (0 == rc)
rc = ZBX_PROTO_ERROR;
ret:
zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%ld", __function_name, (long int)rc);
return rc;
}
static ssize_t telnet_socket_write(ZBX_SOCKET socket_fd, const void *buf, size_t count)
{
const char *__function_name = "telnet_socket_write";
ssize_t rc;
int error;
zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
while (ZBX_PROTO_ERROR == (rc = ZBX_TCP_WRITE(socket_fd, buf, count)))
{
error = zbx_socket_last_error(); /* zabbix_log() resets the error code */
zabbix_log(LOG_LEVEL_DEBUG, "%s() rc:%ld errno:%d error:[%s]",
__function_name, (long int)rc, error, strerror_from_system(error));
#ifdef _WINDOWS
if (WSAEWOULDBLOCK == error)
#else
if (EAGAIN == error)
#endif
{
telnet_waitsocket(socket_fd, WAIT_WRITE);
continue;
}
break;
}
zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%ld", __function_name, (long int)rc);
return rc;
}
static ssize_t telnet_read(ZBX_SOCKET socket_fd, char *buf, size_t *buf_left, size_t *buf_offset)
{
const char *__function_name = "telnet_read";
unsigned char c, c1, c2, c3;
ssize_t rc = ZBX_PROTO_ERROR;
zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
for (;;)
{
if (1 > (rc = telnet_socket_read(socket_fd, &c1, 1)))
break;
zabbix_log(LOG_LEVEL_DEBUG, "%s() c1:[%x=%c]", __function_name, c1, isprint(c1) ? c1 : ' ');
switch (c1)
{
case CMD_IAC:
while (0 == (rc = telnet_socket_read(socket_fd, &c2, 1)))
;
if (ZBX_PROTO_ERROR == rc)
goto end;
zabbix_log(LOG_LEVEL_DEBUG, "%s() c2:%x", __function_name, c2);
switch (c2)
{
case CMD_IAC: /* only IAC needs to be doubled to be sent as data */
if (0 < *buf_left)
{
buf[(*buf_offset)++] = (char)c2;
(*buf_left)--;
}
break;
case CMD_WILL:
case CMD_WONT:
case CMD_DO:
case CMD_DONT:
while (0 == (rc = telnet_socket_read(socket_fd, &c3, 1)))
;
if (ZBX_PROTO_ERROR == rc)
goto end;
zabbix_log(LOG_LEVEL_DEBUG, "%s() c3:%x", __function_name, c3);
/* reply to all options with "WONT" or "DONT", */
/* unless it is Suppress Go Ahead (SGA) */
c = CMD_IAC;
telnet_socket_write(socket_fd, &c, 1);
if (CMD_WONT == c2)
c = CMD_DONT; /* the only valid response */
else if (CMD_DONT == c2)
c = CMD_WONT; /* the only valid response */
else if (OPT_SGA == c3)
c = (c2 == CMD_DO ? CMD_WILL : CMD_DO);
else
c = (c2 == CMD_DO ? CMD_WONT : CMD_DONT);
telnet_socket_write(socket_fd, &c, 1);
telnet_socket_write(socket_fd, &c3, 1);
break;
default:
break;
}
break;
default:
if (0 < *buf_left)
{
buf[(*buf_offset)++] = (char)c1;
(*buf_left)--;
}
break;
}
}
end:
zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%d", __function_name, (int)rc);
return rc;
}
/******************************************************************************
* *
* Comments: converts CR+LF to Unix LF and clears CR+NUL *
* *
******************************************************************************/
static void convert_telnet_to_unix_eol(char *buf, size_t *offset)
{
size_t i, sz = *offset, new_offset;
new_offset = 0;
for (i = 0; i < sz; i++)
{
if (i + 1 < sz && '\r' == buf[i] && '\n' == buf[i + 1]) /* CR+LF (Windows) */
{
buf[new_offset++] = '\n';
i++;
}
else if (i + 1 < sz && '\r' == buf[i] && '\0' == buf[i + 1]) /* CR+NUL */
{
i++;
}
else if (i + 1 < sz && '\n' == buf[i] && '\r' == buf[i + 1]) /* LF+CR */
{
buf[new_offset++] = '\n';
i++;
}
else if ('\r' == buf[i]) /* CR */
{
buf[new_offset++] = '\n';
}
else
buf[new_offset++] = buf[i];
}
*offset = new_offset;
}
static void convert_unix_to_telnet_eol(const char *buf, size_t offset, char *out_buf, size_t *out_offset)
{
size_t i;
*out_offset = 0;
for (i = 0; i < offset; i++)
{
if ('\n' != buf[i])
{
out_buf[(*out_offset)++] = buf[i];
}
else
{
out_buf[(*out_offset)++] = '\r';
out_buf[(*out_offset)++] = '\n';
}
}
}
static char telnet_lastchar(const char *buf, size_t offset)
{
while (0 < offset)
{
offset--;
if (' ' != buf[offset])
return buf[offset];
}
return '\0';
}
static int telnet_rm_echo(char *buf, size_t *offset, const char *echo, size_t len)
{
if (0 == memcmp(buf, echo, len))
{
*offset -= len;
memmove(&buf[0], &buf[len], *offset * sizeof(char));
return SUCCEED;
}
return FAIL;
}
static void telnet_rm_prompt(const char *buf, size_t *offset)
{
unsigned char state = 0; /* 0 - init, 1 - prompt */
while (0 < *offset)
{
(*offset)--;
if (0 == state && buf[*offset] == prompt_char)
state = 1;
if (1 == state && buf[*offset] == '\n')
break;
}
}
int telnet_test_login(ZBX_SOCKET socket_fd)
{
const char *__function_name = "telnet_test_login";
char buf[MAX_BUFFER_LEN];
size_t sz, offset;
int rc, ret = FAIL;
zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
sz = sizeof(buf);
offset = 0;
while (ZBX_PROTO_ERROR != (rc = telnet_read(socket_fd, buf, &sz, &offset)))
{
if (':' == telnet_lastchar(buf, offset))
break;
}
convert_telnet_to_unix_eol(buf, &offset);
zabbix_log(LOG_LEVEL_DEBUG, "%s() login prompt:'%.*s'", __function_name, (int)offset, buf);
if (ZBX_PROTO_ERROR != rc)
ret = SUCCEED;
zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret));
return ret;
}
int telnet_login(ZBX_SOCKET socket_fd, const char *username, const char *password, AGENT_RESULT *result)
{
const char *__function_name = "telnet_login";
char buf[MAX_BUFFER_LEN], c;
size_t sz, offset;
int rc, ret = FAIL;
zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
sz = sizeof(buf);
offset = 0;
while (ZBX_PROTO_ERROR != (rc = telnet_read(socket_fd, buf, &sz, &offset)))
{
if (':' == telnet_lastchar(buf, offset))
break;
}
convert_telnet_to_unix_eol(buf, &offset);
zabbix_log(LOG_LEVEL_DEBUG, "%s() login prompt:'%.*s'", __function_name, (int)offset, buf);
if (ZBX_PROTO_ERROR == rc)
{
SET_MSG_RESULT(result, zbx_strdup(NULL, "No login prompt."));
goto fail;
}
telnet_socket_write(socket_fd, username, strlen(username));
telnet_socket_write(socket_fd, "\r\n", 2);
sz = sizeof(buf);
offset = 0;
while (ZBX_PROTO_ERROR != (rc = telnet_read(socket_fd, buf, &sz, &offset)))
{
if (':' == telnet_lastchar(buf, offset))
break;
}
convert_telnet_to_unix_eol(buf, &offset);
zabbix_log(LOG_LEVEL_DEBUG, "%s() password prompt:'%.*s'", __function_name, (int)offset, buf);
if (ZBX_PROTO_ERROR == rc)
{
SET_MSG_RESULT(result, zbx_strdup(NULL, "No password prompt."));
goto fail;
}
telnet_socket_write(socket_fd, password, strlen(password));
telnet_socket_write(socket_fd, "\r\n", 2);
sz = sizeof(buf);
offset = 0;
while (ZBX_PROTO_ERROR != (rc = telnet_read(socket_fd, buf, &sz, &offset)))
{
if ('$' == (c = telnet_lastchar(buf, offset)) || '#' == c || '>' == c || '%' == c)
{
prompt_char = c;
break;
}
}
convert_telnet_to_unix_eol(buf, &offset);
zabbix_log(LOG_LEVEL_DEBUG, "%s() prompt:'%.*s'", __function_name, (int)offset, buf);
if (ZBX_PROTO_ERROR == rc)
{
SET_MSG_RESULT(result, zbx_strdup(NULL, "Login failed."));
goto fail;
}
ret = SUCCEED;
fail:
zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret));
return ret;
}
int telnet_execute(ZBX_SOCKET socket_fd, const char *command, AGENT_RESULT *result, const char *encoding)
{
const char *__function_name = "telnet_execute";
char buf[MAX_BUFFER_LEN];
size_t sz, offset;
int rc, ret = FAIL;
char *command_lf = NULL, *command_crlf = NULL;
size_t i, offset_lf, offset_crlf;
zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
/* `command' with multiple lines may contain CR+LF from the browser; */
/* it should be converted to plain LF to remove echo later on properly */
offset_lf = strlen(command);
command_lf = (char *)zbx_malloc(command_lf, offset_lf + 1);
zbx_strlcpy(command_lf, command, offset_lf + 1);
convert_telnet_to_unix_eol(command_lf, &offset_lf);
/* telnet protocol requires that end-of-line is transferred as CR+LF */
command_crlf = (char *)zbx_malloc(command_crlf, offset_lf * 2 + 1);
convert_unix_to_telnet_eol(command_lf, offset_lf, command_crlf, &offset_crlf);
telnet_socket_write(socket_fd, command_crlf, offset_crlf);
telnet_socket_write(socket_fd, "\r\n", 2);
sz = sizeof(buf);
offset = 0;
while (ZBX_PROTO_ERROR != (rc = telnet_read(socket_fd, buf, &sz, &offset)))
{
if (prompt_char == telnet_lastchar(buf, offset))
break;
}
convert_telnet_to_unix_eol(buf, &offset);
zabbix_log(LOG_LEVEL_DEBUG, "%s() command output:'%.*s'", __function_name, (int)offset, buf);
if (ZBX_PROTO_ERROR == rc)
{
SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot find prompt after command execution: %s",
strerror_from_system(zbx_socket_last_error())));
goto fail;
}
telnet_rm_echo(buf, &offset, command_lf, offset_lf);
/* multi-line commands may have returned additional prompts; */
/* this is not a perfect solution, because in case of multiple */
/* multi-line shell statements these prompts might appear in */
/* the middle of the output, but we still try to be helpful by */
/* removing additional prompts at least from the beginning */
for (i = 0; i < offset_lf; i++)
{
if ('\n' == command_lf[i])
{
if (SUCCEED != telnet_rm_echo(buf, &offset, "$ ", 2) &&
SUCCEED != telnet_rm_echo(buf, &offset, "# ", 2) &&
SUCCEED != telnet_rm_echo(buf, &offset, "> ", 2) &&
SUCCEED != telnet_rm_echo(buf, &offset, "% ", 2))
{
break;
}
}
}
telnet_rm_echo(buf, &offset, "\n", 1);
telnet_rm_prompt(buf, &offset);
zabbix_log(LOG_LEVEL_DEBUG, "%s() stripped command output:'%.*s'", __function_name, (int)offset, buf);
if (MAX_BUFFER_LEN == offset)
offset--;
buf[offset] = '\0';
SET_STR_RESULT(result, convert_to_utf8(buf, offset, encoding));
ret = SUCCEED;
fail:
zbx_free(command_lf);
zbx_free(command_crlf);
zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret));
return ret;
}
|