telnetd.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Simple telnet server
  4. * Bjorn Wesen, Axis Communications AB (bjornw@axis.com)
  5. *
  6. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  7. *
  8. * ---------------------------------------------------------------------------
  9. * (C) Copyright 2000, Axis Communications AB, LUND, SWEDEN
  10. ****************************************************************************
  11. *
  12. * The telnetd manpage says it all:
  13. *
  14. * Telnetd operates by allocating a pseudo-terminal device (see pty(4)) for
  15. * a client, then creating a login process which has the slave side of the
  16. * pseudo-terminal as stdin, stdout, and stderr. Telnetd manipulates the
  17. * master side of the pseudo-terminal, implementing the telnet protocol and
  18. * passing characters between the remote client and the login process.
  19. *
  20. * Vladimir Oleynik <dzo@simtreas.ru> 2001
  21. * Set process group corrections, initial busybox port
  22. */
  23. /*#define DEBUG 1 */
  24. #define DEBUG 0
  25. #include "busybox.h"
  26. #if DEBUG
  27. #define TELCMDS
  28. #define TELOPTS
  29. #endif
  30. #include <arpa/telnet.h>
  31. #include <sys/syslog.h>
  32. #define BUFSIZE 4000
  33. #if ENABLE_LOGIN
  34. static const char *loginpath = "/bin/login";
  35. #else
  36. static const char *loginpath = DEFAULT_SHELL;
  37. #endif
  38. static const char *issuefile = "/etc/issue.net";
  39. /* shell name and arguments */
  40. static const char *argv_init[2];
  41. /* structure that describes a session */
  42. struct tsession {
  43. struct tsession *next;
  44. int sockfd_read, sockfd_write, ptyfd;
  45. int shell_pid;
  46. /* two circular buffers */
  47. char *buf1, *buf2;
  48. int rdidx1, wridx1, size1;
  49. int rdidx2, wridx2, size2;
  50. };
  51. /*
  52. This is how the buffers are used. The arrows indicate the movement
  53. of data.
  54. +-------+ wridx1++ +------+ rdidx1++ +----------+
  55. | | <-------------- | buf1 | <-------------- | |
  56. | | size1-- +------+ size1++ | |
  57. | pty | | socket |
  58. | | rdidx2++ +------+ wridx2++ | |
  59. | | --------------> | buf2 | --------------> | |
  60. +-------+ size2++ +------+ size2-- +----------+
  61. Each session has got two buffers.
  62. */
  63. static int maxfd;
  64. static struct tsession *sessions;
  65. /*
  66. Remove all IAC's from the buffer pointed to by bf (received IACs are ignored
  67. and must be removed so as to not be interpreted by the terminal). Make an
  68. uninterrupted string of characters fit for the terminal. Do this by packing
  69. all characters meant for the terminal sequentially towards the end of bf.
  70. Return a pointer to the beginning of the characters meant for the terminal.
  71. and make *num_totty the number of characters that should be sent to
  72. the terminal.
  73. Note - If an IAC (3 byte quantity) starts before (bf + len) but extends
  74. past (bf + len) then that IAC will be left unprocessed and *processed will be
  75. less than len.
  76. FIXME - if we mean to send 0xFF to the terminal then it will be escaped,
  77. what is the escape character? We aren't handling that situation here.
  78. CR-LF ->'s CR mapping is also done here, for convenience
  79. */
  80. static char *
  81. remove_iacs(struct tsession *ts, int *pnum_totty)
  82. {
  83. unsigned char *ptr0 = (unsigned char *)ts->buf1 + ts->wridx1;
  84. unsigned char *ptr = ptr0;
  85. unsigned char *totty = ptr;
  86. unsigned char *end = ptr + MIN(BUFSIZE - ts->wridx1, ts->size1);
  87. int processed;
  88. int num_totty;
  89. while (ptr < end) {
  90. if (*ptr != IAC) {
  91. int c = *ptr;
  92. *totty++ = *ptr++;
  93. /* We now map \r\n ==> \r for pragmatic reasons.
  94. * Many client implementations send \r\n when
  95. * the user hits the CarriageReturn key.
  96. */
  97. if (c == '\r' && (*ptr == '\n' || *ptr == 0) && ptr < end)
  98. ptr++;
  99. } else {
  100. /*
  101. * TELOPT_NAWS support!
  102. */
  103. if ((ptr+2) >= end) {
  104. /* only the beginning of the IAC is in the
  105. buffer we were asked to process, we can't
  106. process this char. */
  107. break;
  108. }
  109. /*
  110. * IAC -> SB -> TELOPT_NAWS -> 4-byte -> IAC -> SE
  111. */
  112. else if (ptr[1] == SB && ptr[2] == TELOPT_NAWS) {
  113. struct winsize ws;
  114. if ((ptr+8) >= end)
  115. break; /* incomplete, can't process */
  116. ws.ws_col = (ptr[3] << 8) | ptr[4];
  117. ws.ws_row = (ptr[5] << 8) | ptr[6];
  118. ioctl(ts->ptyfd, TIOCSWINSZ, (char *)&ws);
  119. ptr += 9;
  120. } else {
  121. /* skip 3-byte IAC non-SB cmd */
  122. #if DEBUG
  123. fprintf(stderr, "Ignoring IAC %s,%s\n",
  124. TELCMD(ptr[1]), TELOPT(ptr[2]));
  125. #endif
  126. ptr += 3;
  127. }
  128. }
  129. }
  130. processed = ptr - ptr0;
  131. num_totty = totty - ptr0;
  132. /* the difference between processed and num_to tty
  133. is all the iacs we removed from the stream.
  134. Adjust buf1 accordingly. */
  135. ts->wridx1 += processed - num_totty;
  136. ts->size1 -= processed - num_totty;
  137. *pnum_totty = num_totty;
  138. /* move the chars meant for the terminal towards the end of the
  139. buffer. */
  140. return memmove(ptr - num_totty, ptr0, num_totty);
  141. }
  142. static int
  143. getpty(char *line, int size)
  144. {
  145. int p;
  146. #if ENABLE_FEATURE_DEVPTS
  147. p = open("/dev/ptmx", O_RDWR);
  148. if (p > 0) {
  149. const char *name;
  150. grantpt(p);
  151. unlockpt(p);
  152. name = ptsname(p);
  153. if (!name) {
  154. bb_perror_msg("ptsname error (is /dev/pts mounted?)");
  155. return -1;
  156. }
  157. safe_strncpy(line, name, size);
  158. return p;
  159. }
  160. #else
  161. struct stat stb;
  162. int i;
  163. int j;
  164. strcpy(line, "/dev/ptyXX");
  165. for (i = 0; i < 16; i++) {
  166. line[8] = "pqrstuvwxyzabcde"[i];
  167. line[9] = '0';
  168. if (stat(line, &stb) < 0) {
  169. continue;
  170. }
  171. for (j = 0; j < 16; j++) {
  172. line[9] = j < 10 ? j + '0' : j - 10 + 'a';
  173. if (DEBUG)
  174. fprintf(stderr, "Trying to open device: %s\n", line);
  175. p = open(line, O_RDWR | O_NOCTTY);
  176. if (p >= 0) {
  177. line[5] = 't';
  178. return p;
  179. }
  180. }
  181. }
  182. #endif /* FEATURE_DEVPTS */
  183. return -1;
  184. }
  185. static void
  186. send_iac(struct tsession *ts, unsigned char command, int option)
  187. {
  188. /* We rely on that there is space in the buffer for now. */
  189. char *b = ts->buf2 + ts->rdidx2;
  190. *b++ = IAC;
  191. *b++ = command;
  192. *b++ = option;
  193. ts->rdidx2 += 3;
  194. ts->size2 += 3;
  195. }
  196. static struct tsession *
  197. make_new_session(
  198. USE_FEATURE_TELNETD_STANDALONE(int sock_r, int sock_w)
  199. SKIP_FEATURE_TELNETD_STANDALONE(void)
  200. ) {
  201. struct termios termbuf;
  202. int fd, pid;
  203. char tty_name[32];
  204. struct tsession *ts = xzalloc(sizeof(struct tsession) + BUFSIZE * 2);
  205. ts->buf1 = (char *)(&ts[1]);
  206. ts->buf2 = ts->buf1 + BUFSIZE;
  207. /* Got a new connection, set up a tty. */
  208. fd = getpty(tty_name, 32);
  209. if (fd < 0) {
  210. bb_error_msg("all terminals in use");
  211. return NULL;
  212. }
  213. if (fd > maxfd) maxfd = fd;
  214. ndelay_on(ts->ptyfd = fd);
  215. #if ENABLE_FEATURE_TELNETD_STANDALONE
  216. if (sock_w > maxfd) maxfd = sock_w;
  217. if (sock_r > maxfd) maxfd = sock_r;
  218. ndelay_on(ts->sockfd_write = sock_w);
  219. ndelay_on(ts->sockfd_read = sock_r);
  220. #else
  221. ts->sockfd_write = 1;
  222. /* xzalloc: ts->sockfd_read = 0; */
  223. ndelay_on(0);
  224. ndelay_on(1);
  225. #endif
  226. /* Make the telnet client understand we will echo characters so it
  227. * should not do it locally. We don't tell the client to run linemode,
  228. * because we want to handle line editing and tab completion and other
  229. * stuff that requires char-by-char support. */
  230. send_iac(ts, DO, TELOPT_ECHO);
  231. send_iac(ts, DO, TELOPT_NAWS);
  232. send_iac(ts, DO, TELOPT_LFLOW);
  233. send_iac(ts, WILL, TELOPT_ECHO);
  234. send_iac(ts, WILL, TELOPT_SGA);
  235. pid = fork();
  236. if (pid < 0) {
  237. free(ts);
  238. close(fd);
  239. bb_perror_msg("fork");
  240. return NULL;
  241. }
  242. if (pid > 0) {
  243. /* parent */
  244. ts->shell_pid = pid;
  245. return ts;
  246. }
  247. /* child */
  248. /* make new process group */
  249. setsid();
  250. tcsetpgrp(0, getpid());
  251. /* ^^^ strace says: "ioctl(0, TIOCSPGRP, [pid]) = -1 ENOTTY" -- ??! */
  252. /* open the child's side of the tty. */
  253. /* NB: setsid() disconnects from any previous ctty's. Therefore
  254. * we must open child's side of the tty AFTER setsid! */
  255. fd = xopen(tty_name, O_RDWR); /* becomes our ctty */
  256. dup2(fd, 0);
  257. dup2(fd, 1);
  258. dup2(fd, 2);
  259. while (fd > 2) close(fd--);
  260. /* The pseudo-terminal allocated to the client is configured to operate in
  261. * cooked mode, and with XTABS CRMOD enabled (see tty(4)). */
  262. tcgetattr(0, &termbuf);
  263. termbuf.c_lflag |= ECHO; /* if we use readline we dont want this */
  264. termbuf.c_oflag |= ONLCR|XTABS;
  265. termbuf.c_iflag |= ICRNL;
  266. termbuf.c_iflag &= ~IXOFF;
  267. /*termbuf.c_lflag &= ~ICANON;*/
  268. tcsetattr(0, TCSANOW, &termbuf);
  269. print_login_issue(issuefile, NULL);
  270. /* exec shell, with correct argv and env */
  271. execv(loginpath, (char *const *)argv_init);
  272. bb_perror_msg_and_die("execv");
  273. }
  274. #if ENABLE_FEATURE_TELNETD_STANDALONE
  275. static void
  276. free_session(struct tsession *ts)
  277. {
  278. struct tsession *t = sessions;
  279. /* unlink this telnet session from the session list */
  280. if (t == ts)
  281. sessions = ts->next;
  282. else {
  283. while (t->next != ts)
  284. t = t->next;
  285. t->next = ts->next;
  286. }
  287. kill(ts->shell_pid, SIGKILL);
  288. wait4(ts->shell_pid, NULL, 0, NULL);
  289. close(ts->ptyfd);
  290. close(ts->sockfd_read);
  291. /* error if ts->sockfd_read == ts->sockfd_write. So what? ;) */
  292. close(ts->sockfd_write);
  293. free(ts);
  294. /* scan all sessions and find new maxfd */
  295. ts = sessions;
  296. maxfd = 0;
  297. while (ts) {
  298. if (maxfd < ts->ptyfd)
  299. maxfd = ts->ptyfd;
  300. if (maxfd < ts->sockfd_read)
  301. maxfd = ts->sockfd_read;
  302. if (maxfd < ts->sockfd_write)
  303. maxfd = ts->sockfd_write;
  304. ts = ts->next;
  305. }
  306. }
  307. #else /* !FEATURE_TELNETD_STANDALONE */
  308. /* Never actually called */
  309. void free_session(struct tsession *ts);
  310. #endif
  311. int
  312. telnetd_main(int argc, char **argv)
  313. {
  314. fd_set rdfdset, wrfdset;
  315. unsigned opt;
  316. int selret, maxlen, w, r;
  317. struct tsession *ts;
  318. #if ENABLE_FEATURE_TELNETD_STANDALONE
  319. #define IS_INETD (opt & OPT_INETD)
  320. int master_fd = -1; /* be happy, gcc */
  321. unsigned portnbr = 23;
  322. char *opt_bindaddr = NULL;
  323. char *opt_portnbr;
  324. #else
  325. enum {
  326. IS_INETD = 1,
  327. master_fd = -1,
  328. portnbr = 23,
  329. };
  330. #endif
  331. enum {
  332. OPT_PORT = 4 * ENABLE_FEATURE_TELNETD_STANDALONE,
  333. OPT_FOREGROUND = 0x10 * ENABLE_FEATURE_TELNETD_STANDALONE,
  334. OPT_INETD = 0x20 * ENABLE_FEATURE_TELNETD_STANDALONE,
  335. };
  336. opt = getopt32(argc, argv, "f:l:" USE_FEATURE_TELNETD_STANDALONE("p:b:Fi"),
  337. &issuefile, &loginpath
  338. USE_FEATURE_TELNETD_STANDALONE(, &opt_portnbr, &opt_bindaddr));
  339. /* Redirect log to syslog early, if needed */
  340. if (IS_INETD || !(opt & OPT_FOREGROUND)) {
  341. openlog(applet_name, 0, LOG_USER);
  342. logmode = LOGMODE_SYSLOG;
  343. }
  344. //if (opt & 1) // -f
  345. //if (opt & 2) // -l
  346. USE_FEATURE_TELNETD_STANDALONE(
  347. if (opt & OPT_PORT) // -p
  348. portnbr = xatou16(opt_portnbr);
  349. //if (opt & 8) // -b
  350. //if (opt & 0x10) // -F
  351. //if (opt & 0x20) // -i
  352. );
  353. /* Used to check access(loginpath, X_OK) here. Pointless.
  354. * exec will do this for us for free later. */
  355. argv_init[0] = loginpath;
  356. #if ENABLE_FEATURE_TELNETD_STANDALONE
  357. if (IS_INETD) {
  358. sessions = make_new_session(0, 1);
  359. } else {
  360. master_fd = create_and_bind_stream_or_die(opt_bindaddr, portnbr);
  361. xlisten(master_fd, 1);
  362. if (!(opt & OPT_FOREGROUND))
  363. xdaemon(0, 0);
  364. }
  365. #else
  366. sessions = make_new_session();
  367. #endif
  368. /* We don't want to die if just one session is broken */
  369. signal(SIGPIPE, SIG_IGN);
  370. again:
  371. FD_ZERO(&rdfdset);
  372. FD_ZERO(&wrfdset);
  373. if (!IS_INETD) {
  374. FD_SET(master_fd, &rdfdset);
  375. /* This is needed because free_session() does not
  376. * take into account master_fd when it finds new
  377. * maxfd among remaining fd's: */
  378. if (master_fd > maxfd)
  379. maxfd = master_fd;
  380. }
  381. /* select on the master socket, all telnet sockets and their
  382. * ptys if there is room in their session buffers. */
  383. ts = sessions;
  384. while (ts) {
  385. /* buf1 is used from socket to pty
  386. * buf2 is used from pty to socket */
  387. if (ts->size1 > 0) /* can write to pty */
  388. FD_SET(ts->ptyfd, &wrfdset);
  389. if (ts->size1 < BUFSIZE) /* can read from socket */
  390. FD_SET(ts->sockfd_read, &rdfdset);
  391. if (ts->size2 > 0) /* can write to socket */
  392. FD_SET(ts->sockfd_write, &wrfdset);
  393. if (ts->size2 < BUFSIZE) /* can read from pty */
  394. FD_SET(ts->ptyfd, &rdfdset);
  395. ts = ts->next;
  396. }
  397. selret = select(maxfd + 1, &rdfdset, &wrfdset, 0, 0);
  398. if (!selret)
  399. return 0;
  400. #if ENABLE_FEATURE_TELNETD_STANDALONE
  401. /* First check for and accept new sessions. */
  402. if (!IS_INETD && FD_ISSET(master_fd, &rdfdset)) {
  403. int fd;
  404. struct tsession *new_ts;
  405. fd = accept(master_fd, NULL, 0);
  406. if (fd < 0)
  407. goto again;
  408. /* Create a new session and link it into our active list */
  409. new_ts = make_new_session(fd, fd);
  410. if (new_ts) {
  411. new_ts->next = sessions;
  412. sessions = new_ts;
  413. } else {
  414. close(fd);
  415. }
  416. }
  417. #endif
  418. /* Then check for data tunneling. */
  419. ts = sessions;
  420. while (ts) { /* For all sessions... */
  421. struct tsession *next = ts->next; /* in case we free ts. */
  422. if (ts->size1 && FD_ISSET(ts->ptyfd, &wrfdset)) {
  423. int num_totty;
  424. char *ptr;
  425. /* Write to pty from buffer 1. */
  426. ptr = remove_iacs(ts, &num_totty);
  427. w = safe_write(ts->ptyfd, ptr, num_totty);
  428. /* needed? if (w < 0 && errno == EAGAIN) continue; */
  429. if (w < 0) {
  430. if (IS_INETD)
  431. return 0;
  432. free_session(ts);
  433. ts = next;
  434. continue;
  435. }
  436. ts->wridx1 += w;
  437. ts->size1 -= w;
  438. if (ts->wridx1 == BUFSIZE)
  439. ts->wridx1 = 0;
  440. }
  441. if (ts->size2 && FD_ISSET(ts->sockfd_write, &wrfdset)) {
  442. /* Write to socket from buffer 2. */
  443. maxlen = MIN(BUFSIZE - ts->wridx2, ts->size2);
  444. w = safe_write(ts->sockfd_write, ts->buf2 + ts->wridx2, maxlen);
  445. /* needed? if (w < 0 && errno == EAGAIN) continue; */
  446. if (w < 0) {
  447. if (IS_INETD)
  448. return 0;
  449. free_session(ts);
  450. ts = next;
  451. continue;
  452. }
  453. ts->wridx2 += w;
  454. ts->size2 -= w;
  455. if (ts->wridx2 == BUFSIZE)
  456. ts->wridx2 = 0;
  457. }
  458. if (ts->size1 < BUFSIZE && FD_ISSET(ts->sockfd_read, &rdfdset)) {
  459. /* Read from socket to buffer 1. */
  460. maxlen = MIN(BUFSIZE - ts->rdidx1, BUFSIZE - ts->size1);
  461. r = safe_read(ts->sockfd_read, ts->buf1 + ts->rdidx1, maxlen);
  462. if (r < 0 && errno == EAGAIN) continue;
  463. if (r <= 0) {
  464. if (IS_INETD)
  465. return 0;
  466. free_session(ts);
  467. ts = next;
  468. continue;
  469. }
  470. if (!ts->buf1[ts->rdidx1 + r - 1])
  471. if (!--r)
  472. continue;
  473. ts->rdidx1 += r;
  474. ts->size1 += r;
  475. if (ts->rdidx1 == BUFSIZE)
  476. ts->rdidx1 = 0;
  477. }
  478. if (ts->size2 < BUFSIZE && FD_ISSET(ts->ptyfd, &rdfdset)) {
  479. /* Read from pty to buffer 2. */
  480. maxlen = MIN(BUFSIZE - ts->rdidx2, BUFSIZE - ts->size2);
  481. r = safe_read(ts->ptyfd, ts->buf2 + ts->rdidx2, maxlen);
  482. if (r < 0 && errno == EAGAIN) continue;
  483. if (r <= 0) {
  484. if (IS_INETD)
  485. return 0;
  486. free_session(ts);
  487. ts = next;
  488. continue;
  489. }
  490. ts->rdidx2 += r;
  491. ts->size2 += r;
  492. if (ts->rdidx2 == BUFSIZE)
  493. ts->rdidx2 = 0;
  494. }
  495. if (ts->size1 == 0) {
  496. ts->rdidx1 = 0;
  497. ts->wridx1 = 0;
  498. }
  499. if (ts->size2 == 0) {
  500. ts->rdidx2 = 0;
  501. ts->wridx2 = 0;
  502. }
  503. ts = next;
  504. }
  505. goto again;
  506. }