| 1 |
/* |
| 2 |
Copyright (C) 1993-2026 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GNU Inetutils. |
| 5 |
|
| 6 |
GNU Inetutils is free software: you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation, either version 3 of the License, or (at |
| 9 |
your option) any later version. |
| 10 |
|
| 11 |
GNU Inetutils is distributed in the hope that it will be useful, but |
| 12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with this program. If not, see `http://www.gnu.org/licenses/'. */ |
| 18 |
|
| 19 |
#include <config.h> |
| 20 |
|
| 21 |
#define TELOPTS |
| 22 |
#define TELCMDS |
| 23 |
#define SLC_NAMES |
| 24 |
#include "telnetd.h" |
| 25 |
#include <stdarg.h> |
| 26 |
#ifdef HAVE_TERMIO_H |
| 27 |
# include <termio.h> |
| 28 |
#endif |
| 29 |
#include <time.h> |
| 30 |
|
| 31 |
#if defined AUTHENTICATION || defined ENCRYPTION |
| 32 |
# include <libtelnet/misc.h> |
| 33 |
# define NET_ENCRYPT net_encrypt |
| 34 |
#else |
| 35 |
# define NET_ENCRYPT() |
| 36 |
#endif |
| 37 |
|
| 38 |
#ifdef HAVE_TERMCAP_TGETENT |
| 39 |
# include <termcap.h> |
| 40 |
#elif defined HAVE_CURSES_TGETENT |
| 41 |
# include <curses.h> |
| 42 |
# ifndef _XOPEN_CURSES |
| 43 |
# include <term.h> |
| 44 |
# endif |
| 45 |
#endif |
| 46 |
|
| 47 |
#if defined HAVE_STREAMSPTY && defined HAVE_GETMSG \ |
| 48 |
&& defined HAVE_STROPTS_H |
| 49 |
# include <stropts.h> |
| 50 |
#endif |
| 51 |
|
| 52 |
#include <attribute.h> |
| 53 |
|
| 54 |
static char netobuf[BUFSIZ + NETSLOP], *nfrontp, *nbackp; |
| 55 |
static char *neturg; /* one past last byte of urgent data */ |
| 56 |
#ifdef ENCRYPTION |
| 57 |
static char *nclearto; |
| 58 |
#endif |
| 59 |
|
| 60 |
static char ptyobuf[BUFSIZ + NETSLOP], *pfrontp, *pbackp; |
| 61 |
|
| 62 |
static char netibuf[BUFSIZ], *netip; |
| 63 |
static int ncc; |
| 64 |
|
| 65 |
static char ptyibuf[BUFSIZ], *ptyip; |
| 66 |
static int pcc; |
| 67 |
|
| 68 |
extern int not42; |
| 69 |
|
| 70 |
static int |
| 71 |
readstream (int p, char *ibuf, int bufsize) |
| 72 |
{ |
| 73 |
#ifndef HAVE_STREAMSPTY |
| 74 |
return read (p, ibuf, bufsize); |
| 75 |
#else |
| 76 |
int flags = 0; |
| 77 |
int ret = 0; |
| 78 |
struct termios *tsp; |
| 79 |
# ifdef HAVE_TERMIO_H |
| 80 |
struct termio *tp; |
| 81 |
# endif |
| 82 |
struct iocblk *ip; |
| 83 |
char vstop, vstart; |
| 84 |
int ixon; |
| 85 |
int newflow; |
| 86 |
struct strbuf strbufc, strbufd; |
| 87 |
unsigned char ctlbuf[BUFSIZ]; |
| 88 |
static int flowstate = -1; |
| 89 |
|
| 90 |
strbufc.maxlen = BUFSIZ; |
| 91 |
strbufc.buf = (char *) ctlbuf; |
| 92 |
strbufd.maxlen = bufsize - 1; |
| 93 |
strbufd.len = 0; |
| 94 |
strbufd.buf = ibuf + 1; |
| 95 |
ibuf[0] = 0; |
| 96 |
|
| 97 |
ret = getmsg (p, &strbufc, &strbufd, &flags); |
| 98 |
if (ret < 0) /* error of some sort -- probably EAGAIN */ |
| 99 |
return -1; |
| 100 |
|
| 101 |
if (strbufc.len <= 0 || ctlbuf[0] == M_DATA) |
| 102 |
{ |
| 103 |
/* data message */ |
| 104 |
if (strbufd.len > 0) /* real data */ |
| 105 |
return strbufd.len + 1; /* count header char */ |
| 106 |
else |
| 107 |
{ |
| 108 |
/* nothing there */ |
| 109 |
errno = EAGAIN; |
| 110 |
return -1; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
/* |
| 115 |
* It's a control message. Return 1, to look at the flag we set |
| 116 |
*/ |
| 117 |
|
| 118 |
switch (ctlbuf[0]) |
| 119 |
{ |
| 120 |
case M_FLUSH: |
| 121 |
if (ibuf[1] & FLUSHW) |
| 122 |
ibuf[0] = TIOCPKT_FLUSHWRITE; |
| 123 |
return 1; |
| 124 |
|
| 125 |
case M_IOCTL: |
| 126 |
ip = (struct iocblk *) (ibuf + 1); |
| 127 |
|
| 128 |
switch (ip->ioc_cmd) |
| 129 |
{ |
| 130 |
case TCSETS: |
| 131 |
case TCSETSW: |
| 132 |
case TCSETSF: |
| 133 |
tsp = (struct termios *) (ibuf + 1 + sizeof (struct iocblk)); |
| 134 |
vstop = tsp->c_cc[VSTOP]; |
| 135 |
vstart = tsp->c_cc[VSTART]; |
| 136 |
ixon = tsp->c_iflag & IXON; |
| 137 |
break; |
| 138 |
|
| 139 |
# ifdef HAVE_TERMIO_H |
| 140 |
case TCSETA: |
| 141 |
case TCSETAW: |
| 142 |
case TCSETAF: |
| 143 |
tp = (struct termio *) (ibuf + 1 + sizeof (struct iocblk)); |
| 144 |
vstop = tp->c_cc[VSTOP]; |
| 145 |
vstart = tp->c_cc[VSTART]; |
| 146 |
ixon = tp->c_iflag & IXON; |
| 147 |
break; |
| 148 |
# endif/* HAVE_TERMIO_H */ |
| 149 |
|
| 150 |
default: |
| 151 |
errno = EAGAIN; |
| 152 |
return -1; |
| 153 |
} |
| 154 |
|
| 155 |
newflow = (ixon && (vstart == 021) && (vstop == 023)) ? 1 : 0; |
| 156 |
if (newflow != flowstate) /* it's a change */ |
| 157 |
{ |
| 158 |
flowstate = newflow; |
| 159 |
ibuf[0] = newflow ? TIOCPKT_DOSTOP : TIOCPKT_NOSTOP; |
| 160 |
return 1; |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
/* nothing worth doing anything about */ |
| 165 |
errno = EAGAIN; |
| 166 |
return -1; |
| 167 |
#endif /* HAVE_STREAMSPTY */ |
| 168 |
} |
| 169 |
|
| 170 |
/* ************************************************************************* */ |
| 171 |
/* Net and PTY I/O functions */ |
| 172 |
|
| 173 |
void |
| 174 |
io_setup (void) |
| 175 |
{ |
| 176 |
pfrontp = pbackp = ptyobuf; |
| 177 |
nfrontp = nbackp = netobuf; |
| 178 |
#ifdef ENCRYPTION |
| 179 |
nclearto = 0; |
| 180 |
#endif |
| 181 |
netip = netibuf; |
| 182 |
ptyip = ptyibuf; |
| 183 |
} |
| 184 |
|
| 185 |
void |
| 186 |
set_neturg (void) |
| 187 |
{ |
| 188 |
neturg = nfrontp - 1; |
| 189 |
} |
| 190 |
|
| 191 |
/* net-buffers */ |
| 192 |
|
| 193 |
void |
| 194 |
net_output_byte (int c) |
| 195 |
{ |
| 196 |
*nfrontp++ = c; |
| 197 |
} |
| 198 |
|
| 199 |
int |
| 200 |
net_output_data (const char *format, ...) |
| 201 |
{ |
| 202 |
va_list args; |
| 203 |
size_t remaining, ret; |
| 204 |
|
| 205 |
va_start (args, format); |
| 206 |
remaining = BUFSIZ - (nfrontp - netobuf); |
| 207 |
/* try a netflush() if the room is too low */ |
| 208 |
if (strlen (format) > remaining || BUFSIZ / 4 > remaining) |
| 209 |
{ |
| 210 |
netflush (); |
| 211 |
remaining = BUFSIZ - (nfrontp - netobuf); |
| 212 |
} |
| 213 |
ret = vsnprintf (nfrontp, remaining, format, args); |
| 214 |
nfrontp += ((ret < remaining - 1) ? ret : remaining - 1); |
| 215 |
va_end (args); |
| 216 |
return ret; |
| 217 |
} |
| 218 |
|
| 219 |
int |
| 220 |
net_output_datalen (const void *buf, size_t l) |
| 221 |
{ |
| 222 |
size_t remaining; |
| 223 |
|
| 224 |
remaining = BUFSIZ - (nfrontp - netobuf); |
| 225 |
if (remaining < l) |
| 226 |
{ |
| 227 |
netflush (); |
| 228 |
remaining = BUFSIZ - (nfrontp - netobuf); |
| 229 |
} |
| 230 |
if (remaining < l) |
| 231 |
return -1; |
| 232 |
memmove (nfrontp, buf, l); |
| 233 |
nfrontp += l; |
| 234 |
return (int) l; |
| 235 |
} |
| 236 |
|
| 237 |
int |
| 238 |
net_input_level (void) |
| 239 |
{ |
| 240 |
return ncc; |
| 241 |
} |
| 242 |
|
| 243 |
int |
| 244 |
net_output_level (void) |
| 245 |
{ |
| 246 |
return nfrontp - nbackp; |
| 247 |
} |
| 248 |
|
| 249 |
int |
| 250 |
net_buffer_is_full (void) |
| 251 |
{ |
| 252 |
return (&netobuf[BUFSIZ] - nfrontp) < 2; |
| 253 |
} |
| 254 |
|
| 255 |
int |
| 256 |
net_get_char (int peek) |
| 257 |
{ |
| 258 |
if (peek) |
| 259 |
return *netip; |
| 260 |
else if (ncc > 0) |
| 261 |
{ |
| 262 |
ncc--; |
| 263 |
return *netip++ & 0377; |
| 264 |
} |
| 265 |
|
| 266 |
return 0; |
| 267 |
} |
| 268 |
|
| 269 |
int |
| 270 |
net_read (void) |
| 271 |
{ |
| 272 |
ncc = read (net, netibuf, sizeof (netibuf)); |
| 273 |
if (ncc < 0 && errno == EWOULDBLOCK) |
| 274 |
ncc = 0; |
| 275 |
else if (ncc == 0) |
| 276 |
{ |
| 277 |
syslog (LOG_INFO, "telnetd: peer died"); |
| 278 |
cleanup (0); |
| 279 |
/* NOT REACHED */ |
| 280 |
} |
| 281 |
else if (ncc > 0) |
| 282 |
{ |
| 283 |
netip = netibuf; |
| 284 |
} |
| 285 |
return ncc; |
| 286 |
} |
| 287 |
|
| 288 |
/* PTY buffer functions */ |
| 289 |
|
| 290 |
int |
| 291 |
pty_buffer_is_full (void) |
| 292 |
{ |
| 293 |
return (&ptyobuf[BUFSIZ] - pfrontp) < 2; |
| 294 |
} |
| 295 |
|
| 296 |
void |
| 297 |
pty_output_byte (int c) |
| 298 |
{ |
| 299 |
*pfrontp++ = c; |
| 300 |
} |
| 301 |
|
| 302 |
void |
| 303 |
pty_output_datalen (const void *data, size_t len) |
| 304 |
{ |
| 305 |
if ((size_t) (&ptyobuf[BUFSIZ] - pfrontp) > len) |
| 306 |
ptyflush (); |
| 307 |
memcpy (pfrontp, data, len); |
| 308 |
pfrontp += len; |
| 309 |
} |
| 310 |
|
| 311 |
int |
| 312 |
pty_input_level (void) |
| 313 |
{ |
| 314 |
return pcc; |
| 315 |
} |
| 316 |
|
| 317 |
int |
| 318 |
pty_output_level (void) |
| 319 |
{ |
| 320 |
return pfrontp - pbackp; |
| 321 |
} |
| 322 |
|
| 323 |
void |
| 324 |
ptyflush (void) |
| 325 |
{ |
| 326 |
int n; |
| 327 |
|
| 328 |
if ((n = pfrontp - pbackp) > 0) |
| 329 |
n = write (pty, pbackp, n); |
| 330 |
|
| 331 |
if (n < 0) |
| 332 |
{ |
| 333 |
if (errno == EWOULDBLOCK || errno == EINTR) |
| 334 |
return; |
| 335 |
cleanup (0); |
| 336 |
/* NOT REACHED */ |
| 337 |
} |
| 338 |
|
| 339 |
pbackp += n; |
| 340 |
if (pbackp == pfrontp) |
| 341 |
pbackp = pfrontp = ptyobuf; |
| 342 |
} |
| 343 |
|
| 344 |
int |
| 345 |
pty_get_char (int peek) |
| 346 |
{ |
| 347 |
if (peek) |
| 348 |
return *ptyip; |
| 349 |
else if (pcc > 0) |
| 350 |
{ |
| 351 |
pcc--; |
| 352 |
return *ptyip++ & 0377; |
| 353 |
} |
| 354 |
|
| 355 |
return 0; |
| 356 |
} |
| 357 |
|
| 358 |
int |
| 359 |
pty_input_putback (const char *str, size_t len) |
| 360 |
{ |
| 361 |
if (len > (size_t) (&ptyibuf[BUFSIZ] - ptyip)) |
| 362 |
len = &ptyibuf[BUFSIZ] - ptyip; |
| 363 |
strncpy (ptyip, str, len); |
| 364 |
pcc += len; |
| 365 |
|
| 366 |
return 0; |
| 367 |
} |
| 368 |
|
| 369 |
/* pty_read() |
| 370 |
* |
| 371 |
* Read errors EWOULDBLOCK, EAGAIN, and EIO are |
| 372 |
* tweaked into reporting zero bytes input. |
| 373 |
* In particular, EIO is known to appear when |
| 374 |
* reading off the master side, before having |
| 375 |
* an active slave side. |
| 376 |
*/ |
| 377 |
int |
| 378 |
pty_read (void) |
| 379 |
{ |
| 380 |
pcc = readstream (pty, ptyibuf, BUFSIZ); |
| 381 |
if (pcc < 0 && (errno == EWOULDBLOCK |
| 382 |
#ifdef EAGAIN |
| 383 |
|| errno == EAGAIN |
| 384 |
#endif |
| 385 |
|| errno == EIO)) |
| 386 |
pcc = 0; |
| 387 |
ptyip = ptyibuf; |
| 388 |
|
| 389 |
return pcc; |
| 390 |
} |
| 391 |
|
| 392 |
/* ************************************************************************* */ |
| 393 |
|
| 394 |
|
| 395 |
/* io_drain () |
| 396 |
* |
| 397 |
* |
| 398 |
* A small subroutine to flush the network output buffer, get some data |
| 399 |
* from the network, and pass it through the telnet state machine. We |
| 400 |
* also flush the pty input buffer (by dropping its data) if it becomes |
| 401 |
* too full. |
| 402 |
*/ |
| 403 |
|
| 404 |
void |
| 405 |
io_drain (void) |
| 406 |
{ |
| 407 |
fd_set rfds; |
| 408 |
|
| 409 |
if (nfrontp - nbackp > 0) |
| 410 |
netflush (); |
| 411 |
|
| 412 |
FD_ZERO (&rfds); |
| 413 |
FD_SET (net, &rfds); |
| 414 |
if (1 != select (net + 1, &rfds, NULL, NULL, NULL)) |
| 415 |
{ |
| 416 |
syslog (LOG_INFO, "ttloop: select: %m\n"); |
| 417 |
exit (EXIT_FAILURE); |
| 418 |
} |
| 419 |
|
| 420 |
ncc = read (net, netibuf, sizeof netibuf); |
| 421 |
if (ncc < 0) |
| 422 |
{ |
| 423 |
syslog (LOG_INFO, "ttloop: read: %m\n"); |
| 424 |
exit (EXIT_FAILURE); |
| 425 |
} |
| 426 |
else if (ncc == 0) |
| 427 |
{ |
| 428 |
syslog (LOG_INFO, "ttloop: peer died: %m\n"); |
| 429 |
exit (EXIT_FAILURE); |
| 430 |
} |
| 431 |
netip = netibuf; |
| 432 |
telrcv (); /* state machine */ |
| 433 |
if (ncc > 0) |
| 434 |
{ |
| 435 |
pfrontp = pbackp = ptyobuf; |
| 436 |
telrcv (); |
| 437 |
} |
| 438 |
} /* end of ttloop */ |
| 439 |
|
| 440 |
/* |
| 441 |
* Check a descriptor to see if out of band data exists on it. |
| 442 |
*/ |
| 443 |
/* int s; socket number */ |
| 444 |
int |
| 445 |
stilloob (int s) |
| 446 |
{ |
| 447 |
static struct timeval timeout = { 0, 0 }; |
| 448 |
fd_set excepts; |
| 449 |
int value; |
| 450 |
|
| 451 |
do |
| 452 |
{ |
| 453 |
FD_ZERO (&excepts); |
| 454 |
FD_SET (s, &excepts); |
| 455 |
value = select (s + 1, (fd_set *) 0, (fd_set *) 0, &excepts, &timeout); |
| 456 |
} |
| 457 |
while (value == -1 && errno == EINTR); |
| 458 |
|
| 459 |
if (value < 0) |
| 460 |
fatalperror (pty, "select"); |
| 461 |
|
| 462 |
return FD_ISSET (s, &excepts); |
| 463 |
} |
| 464 |
|
| 465 |
/* |
| 466 |
* nextitem() |
| 467 |
* |
| 468 |
* Return the address of the next "item" in the TELNET data |
| 469 |
* stream. This will be the address of the next character if |
| 470 |
* the current address is a user data character, or it will |
| 471 |
* be the address of the character following the TELNET command |
| 472 |
* if the current address is a TELNET IAC ("I Am a Command") |
| 473 |
* character. |
| 474 |
*/ |
| 475 |
char * |
| 476 |
nextitem (char *current, const char *endp) |
| 477 |
{ |
| 478 |
if (current >= endp) |
| 479 |
return NULL; |
| 480 |
if ((*current & 0xff) != IAC) |
| 481 |
return current + 1; |
| 482 |
if (current + 1 >= endp) |
| 483 |
return NULL; |
| 484 |
|
| 485 |
switch (*(current + 1) & 0xff) |
| 486 |
{ |
| 487 |
case DO: |
| 488 |
case DONT: |
| 489 |
case WILL: |
| 490 |
case WONT: |
| 491 |
return current + 3 <= endp ? current + 3 : NULL; |
| 492 |
|
| 493 |
case SB: /* loop forever looking for the SE */ |
| 494 |
{ |
| 495 |
char *look = current + 2; |
| 496 |
|
| 497 |
while (look < endp) |
| 498 |
if ((*look++ & 0xff) == IAC && look < endp |
| 499 |
&& (*look++ & 0xff) == SE) |
| 500 |
return look; |
| 501 |
|
| 502 |
return NULL; |
| 503 |
} |
| 504 |
default: |
| 505 |
return current + 2 <= endp ? current + 2 : NULL; |
| 506 |
} |
| 507 |
} /* end of nextitem */ |
| 508 |
|
| 509 |
|
| 510 |
/* |
| 511 |
* netclear() |
| 512 |
* |
| 513 |
* We are about to do a TELNET SYNCH operation. Clear |
| 514 |
* the path to the network. |
| 515 |
* |
| 516 |
* Things are a bit tricky since we may have sent the first |
| 517 |
* byte or so of a previous TELNET command into the network. |
| 518 |
* So, we have to scan the network buffer from the beginning |
| 519 |
* until we are up to where we want to be. |
| 520 |
* |
| 521 |
* A side effect of what we do, just to keep things |
| 522 |
* simple, is to clear the urgent data pointer. The principal |
| 523 |
* caller should be setting the urgent data pointer AFTER calling |
| 524 |
* us in any case. |
| 525 |
*/ |
| 526 |
#define wewant(p) \ |
| 527 |
((nfrontp > p) && ((*p & 0xff) == IAC) && \ |
| 528 |
(nfrontp > p + 1 && (((*(p + 1) & 0xff) != EC) && \ |
| 529 |
((*(p + 1) & 0xff) != EL)))) |
| 530 |
|
| 531 |
|
| 532 |
void |
| 533 |
netclear (void) |
| 534 |
{ |
| 535 |
char *thisitem, *next; |
| 536 |
char *good; |
| 537 |
|
| 538 |
#ifdef ENCRYPTION |
| 539 |
thisitem = nclearto > netobuf ? nclearto : netobuf; |
| 540 |
#else /* ENCRYPTION */ |
| 541 |
thisitem = netobuf; |
| 542 |
#endif /* ENCRYPTION */ |
| 543 |
|
| 544 |
while ((next = nextitem (thisitem, nbackp)) != NULL && next <= nbackp) |
| 545 |
thisitem = next; |
| 546 |
|
| 547 |
/* Now, thisitem is first before/at boundary. */ |
| 548 |
|
| 549 |
#ifdef ENCRYPTION |
| 550 |
good = nclearto > netobuf ? nclearto : netobuf; |
| 551 |
#else /* ENCRYPTION */ |
| 552 |
good = netobuf; /* where the good bytes go */ |
| 553 |
#endif /* ENCRYPTION */ |
| 554 |
|
| 555 |
while (thisitem != NULL && nfrontp > thisitem) |
| 556 |
{ |
| 557 |
if (wewant (thisitem)) |
| 558 |
{ |
| 559 |
int length; |
| 560 |
|
| 561 |
for (next = thisitem; |
| 562 |
next != NULL && wewant (next) && nfrontp > next; |
| 563 |
next = nextitem (next, nfrontp)) |
| 564 |
; |
| 565 |
if (next == NULL) |
| 566 |
next = nfrontp; |
| 567 |
|
| 568 |
length = next - thisitem; |
| 569 |
memmove (good, thisitem, length); |
| 570 |
good += length; |
| 571 |
thisitem = next; |
| 572 |
} |
| 573 |
else |
| 574 |
{ |
| 575 |
thisitem = nextitem (thisitem, nfrontp); |
| 576 |
} |
| 577 |
} |
| 578 |
|
| 579 |
nbackp = netobuf; |
| 580 |
nfrontp = good; /* next byte to be sent */ |
| 581 |
neturg = 0; |
| 582 |
} /* end of netclear */ |
| 583 |
|
| 584 |
/* |
| 585 |
* netflush |
| 586 |
* Send as much data as possible to the network, |
| 587 |
* handling requests for urgent data. |
| 588 |
*/ |
| 589 |
void |
| 590 |
netflush (void) |
| 591 |
{ |
| 592 |
int n; |
| 593 |
|
| 594 |
if ((n = nfrontp - nbackp) > 0) |
| 595 |
{ |
| 596 |
NET_ENCRYPT (); |
| 597 |
|
| 598 |
/* |
| 599 |
* if no urgent data, or if the other side appears to be an |
| 600 |
* old 4.2 client (and thus unable to survive TCP urgent data), |
| 601 |
* write the entire buffer in non-OOB mode. |
| 602 |
*/ |
| 603 |
if (!neturg || !not42) |
| 604 |
n = write (net, nbackp, n); /* normal write */ |
| 605 |
else |
| 606 |
{ |
| 607 |
n = neturg - nbackp; |
| 608 |
/* |
| 609 |
* In 4.2 (and 4.3) systems, there is some question about |
| 610 |
* what byte in a sendOOB operation is the "OOB" data. |
| 611 |
* To make ourselves compatible, we only send ONE byte |
| 612 |
* out of band, the one WE THINK should be OOB (though |
| 613 |
* we really have more the TCP philosophy of urgent data |
| 614 |
* rather than the Unix philosophy of OOB data). |
| 615 |
*/ |
| 616 |
if (n > 1) |
| 617 |
n = send (net, nbackp, n - 1, 0); /* send URGENT all by itself */ |
| 618 |
else |
| 619 |
n = send (net, nbackp, n, MSG_OOB); /* URGENT data */ |
| 620 |
} |
| 621 |
} |
| 622 |
if (n < 0) |
| 623 |
{ |
| 624 |
if (errno == EWOULDBLOCK || errno == EINTR) |
| 625 |
return; |
| 626 |
cleanup (0); |
| 627 |
/* NOT REACHED */ |
| 628 |
} |
| 629 |
|
| 630 |
nbackp += n; |
| 631 |
#ifdef ENCRYPTION |
| 632 |
if (nbackp > nclearto) |
| 633 |
nclearto = 0; |
| 634 |
#endif /* ENCRYPTION */ |
| 635 |
if (nbackp >= neturg) |
| 636 |
neturg = 0; |
| 637 |
|
| 638 |
if (nbackp == nfrontp) |
| 639 |
{ |
| 640 |
nbackp = nfrontp = netobuf; |
| 641 |
#ifdef ENCRYPTION |
| 642 |
nclearto = 0; |
| 643 |
#endif /* ENCRYPTION */ |
| 644 |
} |
| 645 |
} /* end of netflush */ |
| 646 |
|
| 647 |
/* |
| 648 |
* miscellaneous functions doing a variety of little jobs follow ... |
| 649 |
*/ |
| 650 |
|
| 651 |
void |
| 652 |
fatal (int f, char *msg) |
| 653 |
{ |
| 654 |
char buf[BUFSIZ]; |
| 655 |
|
| 656 |
snprintf (buf, sizeof buf, "telnetd: %s.\r\n", msg); |
| 657 |
#ifdef ENCRYPTION |
| 658 |
if (encrypt_output) |
| 659 |
{ |
| 660 |
/* |
| 661 |
* Better turn off encryption first.... |
| 662 |
* Hope it flushes... |
| 663 |
*/ |
| 664 |
encrypt_send_end (); |
| 665 |
netflush (); |
| 666 |
} |
| 667 |
#endif /* ENCRYPTION */ |
| 668 |
write (f, buf, (int) strlen (buf)); |
| 669 |
sleep (1); |
| 670 |
/*FIXME*/ exit (EXIT_FAILURE); |
| 671 |
} |
| 672 |
|
| 673 |
void |
| 674 |
fatalperror (int f, char *msg) |
| 675 |
{ |
| 676 |
char buf[BUFSIZ]; |
| 677 |
|
| 678 |
snprintf (buf, sizeof buf, "%s: %s", msg, strerror (errno)); |
| 679 |
fatal (f, buf); |
| 680 |
} |
| 681 |
|
| 682 |
/* ************************************************************************* */ |
| 683 |
/* Terminal determination */ |
| 684 |
|
| 685 |
static unsigned char ttytype_sbbuf[] = { |
| 686 |
IAC, SB, TELOPT_TTYPE, TELQUAL_SEND, IAC, SE |
| 687 |
}; |
| 688 |
|
| 689 |
static void |
| 690 |
_gettermname (void) |
| 691 |
{ |
| 692 |
if (his_state_is_wont (TELOPT_TTYPE)) |
| 693 |
return; |
| 694 |
settimer (baseline); |
| 695 |
net_output_datalen (ttytype_sbbuf, sizeof ttytype_sbbuf); |
| 696 |
ttloop (sequenceIs (ttypesubopt, baseline)); |
| 697 |
} |
| 698 |
|
| 699 |
/* |
| 700 |
* Changes terminaltype. |
| 701 |
*/ |
| 702 |
int |
| 703 |
getterminaltype (char *uname, size_t len) |
| 704 |
{ |
| 705 |
int retval = -1; |
| 706 |
|
| 707 |
settimer (baseline); |
| 708 |
#if defined AUTHENTICATION |
| 709 |
/* |
| 710 |
* Handle the Authentication option before we do anything else. |
| 711 |
* Distinguish the available modes by level: |
| 712 |
* |
| 713 |
* off: Authentication is forbidden. |
| 714 |
* none: Voluntary authentication. |
| 715 |
* user, valid, other: Mandatory authentication only. |
| 716 |
*/ |
| 717 |
if (auth_level < 0) |
| 718 |
send_wont (TELOPT_AUTHENTICATION, 1); |
| 719 |
else |
| 720 |
{ |
| 721 |
if (auth_level > 0) |
| 722 |
send_do (TELOPT_AUTHENTICATION, 1); |
| 723 |
else |
| 724 |
send_will (TELOPT_AUTHENTICATION, 1); |
| 725 |
|
| 726 |
ttloop (his_will_wont_is_changing (TELOPT_AUTHENTICATION)); |
| 727 |
|
| 728 |
if (his_state_is_will (TELOPT_AUTHENTICATION)) |
| 729 |
retval = auth_wait (uname, len); |
| 730 |
} |
| 731 |
#else /* !AUTHENTICATION */ |
| 732 |
(void) uname; /* Silence warning. */ |
| 733 |
(void) len; /* Silence warning. */ |
| 734 |
#endif |
| 735 |
|
| 736 |
#ifdef ENCRYPTION |
| 737 |
send_will (TELOPT_ENCRYPT, 1); |
| 738 |
#endif /* ENCRYPTION */ |
| 739 |
send_do (TELOPT_TTYPE, 1); |
| 740 |
send_do (TELOPT_TSPEED, 1); |
| 741 |
send_do (TELOPT_XDISPLOC, 1); |
| 742 |
send_do (TELOPT_NEW_ENVIRON, 1); |
| 743 |
send_do (TELOPT_OLD_ENVIRON, 1); |
| 744 |
|
| 745 |
#ifdef ENCRYPTION |
| 746 |
ttloop (his_do_dont_is_changing (TELOPT_ENCRYPT) |
| 747 |
|| his_will_wont_is_changing (TELOPT_TTYPE) |
| 748 |
|| his_will_wont_is_changing (TELOPT_TSPEED) |
| 749 |
|| his_will_wont_is_changing (TELOPT_XDISPLOC) |
| 750 |
|| his_will_wont_is_changing (TELOPT_NEW_ENVIRON) |
| 751 |
|| his_will_wont_is_changing (TELOPT_OLD_ENVIRON)); |
| 752 |
#else |
| 753 |
ttloop (his_will_wont_is_changing (TELOPT_TTYPE) |
| 754 |
|| his_will_wont_is_changing (TELOPT_TSPEED) |
| 755 |
|| his_will_wont_is_changing (TELOPT_XDISPLOC) |
| 756 |
|| his_will_wont_is_changing (TELOPT_NEW_ENVIRON) |
| 757 |
|| his_will_wont_is_changing (TELOPT_OLD_ENVIRON)); |
| 758 |
#endif |
| 759 |
|
| 760 |
#ifdef ENCRYPTION |
| 761 |
if (his_state_is_will (TELOPT_ENCRYPT)) |
| 762 |
encrypt_wait (); |
| 763 |
#endif |
| 764 |
|
| 765 |
if (his_state_is_will (TELOPT_TSPEED)) |
| 766 |
{ |
| 767 |
static unsigned char sb[] = |
| 768 |
{ IAC, SB, TELOPT_TSPEED, TELQUAL_SEND, IAC, SE }; |
| 769 |
|
| 770 |
net_output_datalen (sb, sizeof sb); |
| 771 |
} |
| 772 |
if (his_state_is_will (TELOPT_XDISPLOC)) |
| 773 |
{ |
| 774 |
static unsigned char sb[] = |
| 775 |
{ IAC, SB, TELOPT_XDISPLOC, TELQUAL_SEND, IAC, SE }; |
| 776 |
|
| 777 |
net_output_datalen (sb, sizeof sb); |
| 778 |
} |
| 779 |
if (his_state_is_will (TELOPT_NEW_ENVIRON)) |
| 780 |
{ |
| 781 |
static unsigned char sb[] = |
| 782 |
{ IAC, SB, TELOPT_NEW_ENVIRON, TELQUAL_SEND, IAC, SE }; |
| 783 |
|
| 784 |
net_output_datalen (sb, sizeof sb); |
| 785 |
} |
| 786 |
else if (his_state_is_will (TELOPT_OLD_ENVIRON)) |
| 787 |
{ |
| 788 |
static unsigned char sb[] = |
| 789 |
{ IAC, SB, TELOPT_OLD_ENVIRON, TELQUAL_SEND, IAC, SE }; |
| 790 |
|
| 791 |
net_output_datalen (sb, sizeof sb); |
| 792 |
} |
| 793 |
if (his_state_is_will (TELOPT_TTYPE)) |
| 794 |
net_output_datalen (ttytype_sbbuf, sizeof ttytype_sbbuf); |
| 795 |
|
| 796 |
if (his_state_is_will (TELOPT_TSPEED)) |
| 797 |
ttloop (sequenceIs (tspeedsubopt, baseline)); |
| 798 |
if (his_state_is_will (TELOPT_XDISPLOC)) |
| 799 |
ttloop (sequenceIs (xdisplocsubopt, baseline)); |
| 800 |
if (his_state_is_will (TELOPT_NEW_ENVIRON)) |
| 801 |
ttloop (sequenceIs (environsubopt, baseline)); |
| 802 |
if (his_state_is_will (TELOPT_OLD_ENVIRON)) |
| 803 |
ttloop (sequenceIs (oenvironsubopt, baseline)); |
| 804 |
|
| 805 |
if (his_state_is_will (TELOPT_TTYPE)) |
| 806 |
{ |
| 807 |
char *first = NULL, *last = NULL; |
| 808 |
|
| 809 |
ttloop (sequenceIs (ttypesubopt, baseline)); |
| 810 |
/* |
| 811 |
* If the other side has already disabled the option, then |
| 812 |
* we have to just go with what we (might) have already gotten. |
| 813 |
*/ |
| 814 |
if (his_state_is_will (TELOPT_TTYPE) && !terminaltypeok (terminaltype)) |
| 815 |
{ |
| 816 |
free (first); |
| 817 |
first = xstrdup (terminaltype); |
| 818 |
for (;;) |
| 819 |
{ |
| 820 |
/* Save the unknown name, and request the next name. */ |
| 821 |
free (last); |
| 822 |
last = xstrdup (terminaltype); |
| 823 |
_gettermname (); |
| 824 |
if (terminaltypeok (terminaltype)) |
| 825 |
break; |
| 826 |
if ((strcmp (last, terminaltype) == 0) |
| 827 |
|| his_state_is_wont (TELOPT_TTYPE)) |
| 828 |
{ |
| 829 |
/* |
| 830 |
* We've hit the end. If this is the same as |
| 831 |
* the first name, just go with it. |
| 832 |
*/ |
| 833 |
if (strcmp (first, terminaltype) == 0) |
| 834 |
break; |
| 835 |
/* |
| 836 |
* Get the terminal name one more time, so that |
| 837 |
* RFC1091 compliant telnets will cycle back to |
| 838 |
* the start of the list. |
| 839 |
*/ |
| 840 |
_gettermname (); |
| 841 |
if (strcmp (first, terminaltype) != 0) |
| 842 |
{ |
| 843 |
free (terminaltype); |
| 844 |
terminaltype = xstrdup (first); |
| 845 |
} |
| 846 |
break; |
| 847 |
} |
| 848 |
} |
| 849 |
} |
| 850 |
free (first); |
| 851 |
free (last); |
| 852 |
} |
| 853 |
return retval; |
| 854 |
} |
| 855 |
|
| 856 |
/* |
| 857 |
* Exit status: |
| 858 |
* |
| 859 |
* 1 Accepted terminal type, or inconclusive, |
| 860 |
* 0 Explicitly unsupported type. |
| 861 |
*/ |
| 862 |
int |
| 863 |
terminaltypeok (char *s) |
| 864 |
{ |
| 865 |
#ifdef HAVE_TGETENT |
| 866 |
char buf[2048]; |
| 867 |
|
| 868 |
if (terminaltype == NULL) |
| 869 |
return 1; |
| 870 |
|
| 871 |
if (tgetent (buf, s) == 0) |
| 872 |
return 0; |
| 873 |
#endif /* HAVE_TGETENT */ |
| 874 |
|
| 875 |
return 1; |
| 876 |
} |
| 877 |
|
| 878 |
#if defined AUTHENTICATION || defined ENCRYPTION |
| 879 |
|
| 880 |
/* libtelnet needs this function. */ |
| 881 |
|
| 882 |
void |
| 883 |
printsub (int direction MAYBE_UNUSED, unsigned char *pointer MAYBE_UNUSED, |
| 884 |
int length MAYBE_UNUSED) |
| 885 |
{ |
| 886 |
} |
| 887 |
|
| 888 |
int |
| 889 |
net_write (unsigned char *str, int len) |
| 890 |
{ |
| 891 |
return net_output_datalen (str, len); |
| 892 |
} |
| 893 |
|
| 894 |
void |
| 895 |
net_encrypt (void) |
| 896 |
{ |
| 897 |
# ifdef ENCRYPTION |
| 898 |
char *s = (nclearto > nbackp) ? nclearto : nbackp; |
| 899 |
if (s < nfrontp && encrypt_output) |
| 900 |
(*encrypt_output) ((unsigned char *) s, nfrontp - s); |
| 901 |
nclearto = nfrontp; |
| 902 |
# endif/* ENCRYPTION */ |
| 903 |
} |
| 904 |
|
| 905 |
int |
| 906 |
telnet_spin (void) |
| 907 |
{ |
| 908 |
io_drain (); |
| 909 |
return 0; |
| 910 |
} |
| 911 |
|
| 912 |
|
| 913 |
#endif |
| 914 |
|
| 915 |
/* ************************************************************************* */ |
| 916 |
/* String expansion functions */ |
| 917 |
|
| 918 |
#define EXP_STATE_CONTINUE 0 |
| 919 |
#define EXP_STATE_SUCCESS 1 |
| 920 |
#define EXP_STATE_ERROR 2 |
| 921 |
|
| 922 |
struct line_expander |
| 923 |
{ |
| 924 |
int state; /* Current state */ |
| 925 |
int level; /* The nesting level */ |
| 926 |
char *source; /* The source string */ |
| 927 |
char *cp; /* Current position in the source */ |
| 928 |
struct obstack stk; /* Obstack for expanded version */ |
| 929 |
}; |
| 930 |
|
| 931 |
static char *_var_short_name (struct line_expander *exp); |
| 932 |
static char *_var_long_name (struct line_expander *exp, |
| 933 |
char *start, int length); |
| 934 |
static char *_expand_var (struct line_expander *exp); |
| 935 |
static void _expand_cond (struct line_expander *exp); |
| 936 |
static void _skip_block (struct line_expander *exp); |
| 937 |
static void _expand_block (struct line_expander *exp); |
| 938 |
|
| 939 |
static const char * |
| 940 |
sanitize (const char *u) |
| 941 |
{ |
| 942 |
/* Ignore values starting with '-' or containing shell metachars, as |
| 943 |
they can cause trouble. */ |
| 944 |
if (u && *u != '-' && !u[strcspn (u, "\t\n !\"#$&'()*;<=>?[\\^`{|}~")]) |
| 945 |
return u; |
| 946 |
else |
| 947 |
return ""; |
| 948 |
} |
| 949 |
|
| 950 |
/* Expand a variable referenced by its short one-symbol name. |
| 951 |
Input: exp->cp points to the variable name. |
| 952 |
FIXME: not implemented */ |
| 953 |
char * |
| 954 |
_var_short_name (struct line_expander *exp) |
| 955 |
{ |
| 956 |
char *q; |
| 957 |
char timebuf[64]; |
| 958 |
time_t t; |
| 959 |
|
| 960 |
switch (*exp->cp++) |
| 961 |
{ |
| 962 |
case 'a': |
| 963 |
#ifdef AUTHENTICATION |
| 964 |
if (auth_level >= 0 && autologin == AUTH_VALID) |
| 965 |
return xstrdup ("ok"); |
| 966 |
#endif |
| 967 |
return NULL; |
| 968 |
|
| 969 |
case 'd': |
| 970 |
time (&t); |
| 971 |
strftime (timebuf, sizeof (timebuf), |
| 972 |
"%l:%M%p on %A, %d %B %Y", localtime (&t)); |
| 973 |
return xstrdup (timebuf); |
| 974 |
|
| 975 |
case 'h': |
| 976 |
return xstrdup (sanitize (remote_hostname)); |
| 977 |
|
| 978 |
case 'l': |
| 979 |
return xstrdup (sanitize (local_hostname)); |
| 980 |
|
| 981 |
case 'L': |
| 982 |
return xstrdup (sanitize (line)); |
| 983 |
|
| 984 |
case 't': |
| 985 |
q = strchr (line + 1, '/'); |
| 986 |
if (q) |
| 987 |
q++; |
| 988 |
else |
| 989 |
q = line; |
| 990 |
return xstrdup (sanitize (q)); |
| 991 |
|
| 992 |
case 'T': |
| 993 |
return terminaltype ? xstrdup (sanitize (terminaltype)) : NULL; |
| 994 |
|
| 995 |
case 'u': |
| 996 |
return user_name ? xstrdup (sanitize (user_name)) : NULL; |
| 997 |
|
| 998 |
case 'U': |
| 999 |
return xstrdup (sanitize (getenv ("USER"))); |
| 1000 |
|
| 1001 |
default: |
| 1002 |
exp->state = EXP_STATE_ERROR; |
| 1003 |
return NULL; |
| 1004 |
} |
| 1005 |
} |
| 1006 |
|
| 1007 |
/* Expand a variable referenced by its long name. |
| 1008 |
Input: exp->cp points to initial '(' |
| 1009 |
FIXME: not implemented */ |
| 1010 |
char * |
| 1011 |
_var_long_name (struct line_expander *exp, char *start, int length) |
| 1012 |
{ |
| 1013 |
(void) start; /* Silence warnings until implemented. */ |
| 1014 |
(void) length; |
| 1015 |
exp->state = EXP_STATE_ERROR; |
| 1016 |
return NULL; |
| 1017 |
} |
| 1018 |
|
| 1019 |
/* Expand a variable to its value. |
| 1020 |
Input: exp->cp points one character _past_ % (or ?) */ |
| 1021 |
char * |
| 1022 |
_expand_var (struct line_expander *exp) |
| 1023 |
{ |
| 1024 |
char *p; |
| 1025 |
switch (*exp->cp) |
| 1026 |
{ |
| 1027 |
case '{': |
| 1028 |
/* Collect variable name */ |
| 1029 |
for (p = ++exp->cp; *exp->cp && *exp->cp != '}'; exp->cp++) |
| 1030 |
; |
| 1031 |
if (*exp->cp == 0) |
| 1032 |
{ |
| 1033 |
exp->cp = p; |
| 1034 |
exp->state = EXP_STATE_ERROR; |
| 1035 |
break; |
| 1036 |
} |
| 1037 |
p = _var_long_name (exp, p, exp->cp - p); |
| 1038 |
exp->cp++; |
| 1039 |
break; |
| 1040 |
|
| 1041 |
default: |
| 1042 |
p = _var_short_name (exp); |
| 1043 |
break; |
| 1044 |
} |
| 1045 |
return p; |
| 1046 |
} |
| 1047 |
|
| 1048 |
/* Expand a conditional block. A conditional block is: |
| 1049 |
%?<var>{true-stmt}[{false-stmt}] |
| 1050 |
<var> may be either a one-symbol variable name or (string). The latter |
| 1051 |
is not handled yet. |
| 1052 |
On input exp->cp points to % character */ |
| 1053 |
void |
| 1054 |
_expand_cond (struct line_expander *exp) |
| 1055 |
{ |
| 1056 |
char *p; |
| 1057 |
|
| 1058 |
if (*++exp->cp == '?') |
| 1059 |
{ |
| 1060 |
/* condition */ |
| 1061 |
exp->cp++; |
| 1062 |
p = _expand_var (exp); |
| 1063 |
if (p) |
| 1064 |
{ |
| 1065 |
_expand_block (exp); |
| 1066 |
_skip_block (exp); |
| 1067 |
} |
| 1068 |
else |
| 1069 |
{ |
| 1070 |
_skip_block (exp); |
| 1071 |
_expand_block (exp); |
| 1072 |
} |
| 1073 |
free (p); |
| 1074 |
} |
| 1075 |
else |
| 1076 |
{ |
| 1077 |
p = _expand_var (exp); |
| 1078 |
if (p) |
| 1079 |
obstack_grow (&exp->stk, p, strlen (p)); |
| 1080 |
free (p); |
| 1081 |
} |
| 1082 |
} |
| 1083 |
|
| 1084 |
/* Skip the block. If the exp->cp does not point to the beginning of a |
| 1085 |
block ({ character), the function does nothing */ |
| 1086 |
void |
| 1087 |
_skip_block (struct line_expander *exp) |
| 1088 |
{ |
| 1089 |
int level = exp->level; |
| 1090 |
if (*exp->cp != '{') |
| 1091 |
return; |
| 1092 |
for (; *exp->cp; exp->cp++) |
| 1093 |
{ |
| 1094 |
switch (*exp->cp) |
| 1095 |
{ |
| 1096 |
case '{': |
| 1097 |
exp->level++; |
| 1098 |
break; |
| 1099 |
|
| 1100 |
case '}': |
| 1101 |
exp->level--; |
| 1102 |
if (exp->level == level) |
| 1103 |
{ |
| 1104 |
exp->cp++; |
| 1105 |
return; |
| 1106 |
} |
| 1107 |
} |
| 1108 |
} |
| 1109 |
} |
| 1110 |
|
| 1111 |
/* Expand a block within the formatted line. Stops either when end of source |
| 1112 |
line was reached or the nesting reaches the initial value */ |
| 1113 |
void |
| 1114 |
_expand_block (struct line_expander *exp) |
| 1115 |
{ |
| 1116 |
int level = exp->level; |
| 1117 |
if (*exp->cp == '{') |
| 1118 |
{ |
| 1119 |
exp->level++; |
| 1120 |
exp->cp++; /*FIXME? */ |
| 1121 |
} |
| 1122 |
while (exp->state == EXP_STATE_CONTINUE) |
| 1123 |
{ |
| 1124 |
for (; *exp->cp && *exp->cp != '%'; exp->cp++) |
| 1125 |
{ |
| 1126 |
switch (*exp->cp) |
| 1127 |
{ |
| 1128 |
case '{': |
| 1129 |
exp->level++; |
| 1130 |
break; |
| 1131 |
|
| 1132 |
case '}': |
| 1133 |
exp->level--; |
| 1134 |
if (exp->level == level) |
| 1135 |
{ |
| 1136 |
exp->cp++; |
| 1137 |
return; |
| 1138 |
} |
| 1139 |
break; |
| 1140 |
|
| 1141 |
case '\\': |
| 1142 |
exp->cp++; |
| 1143 |
break; |
| 1144 |
} |
| 1145 |
obstack_1grow (&exp->stk, *exp->cp); |
| 1146 |
} |
| 1147 |
|
| 1148 |
if (*exp->cp == 0) |
| 1149 |
{ |
| 1150 |
obstack_1grow (&exp->stk, 0); |
| 1151 |
exp->state = EXP_STATE_SUCCESS; |
| 1152 |
break; |
| 1153 |
} |
| 1154 |
else if (*exp->cp == '%' && exp->cp[1] == '%') |
| 1155 |
{ |
| 1156 |
obstack_1grow (&exp->stk, *exp->cp); |
| 1157 |
exp->cp += 2; |
| 1158 |
continue; |
| 1159 |
} |
| 1160 |
|
| 1161 |
_expand_cond (exp); |
| 1162 |
} |
| 1163 |
} |
| 1164 |
|
| 1165 |
/* Expand a format line */ |
| 1166 |
char * |
| 1167 |
expand_line (const char *line) |
| 1168 |
{ |
| 1169 |
char *p = NULL; |
| 1170 |
struct line_expander exp; |
| 1171 |
|
| 1172 |
exp.state = EXP_STATE_CONTINUE; |
| 1173 |
exp.level = 0; |
| 1174 |
exp.source = (char *) line; |
| 1175 |
exp.cp = (char *) line; |
| 1176 |
obstack_init (&exp.stk); |
| 1177 |
_expand_block (&exp); |
| 1178 |
if (exp.state == EXP_STATE_SUCCESS) |
| 1179 |
p = xstrdup (obstack_finish (&exp.stk)); |
| 1180 |
else |
| 1181 |
{ |
| 1182 |
syslog (LOG_ERR, "can't expand line: %s", line); |
| 1183 |
syslog (LOG_ERR, "stopped near %s", exp.cp ? exp.cp : "(END)"); |
| 1184 |
} |
| 1185 |
obstack_free (&exp.stk, NULL); |
| 1186 |
return p; |
| 1187 |
} |