PageRenderTime 28ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/commands/telnetd/main.c

http://www.minix3.org/
C | 159 lines | 114 code | 20 blank | 25 comment | 19 complexity | fc550d4e47e9a282c65dab3466d2d621 MD5 | raw file
Possible License(s): MIT, WTFPL, AGPL-1.0, BSD-3-Clause, GPL-3.0, LGPL-2.0, JSON, 0BSD
  1. /*
  2. * TNET A server program for MINIX which implements the TCP/IP
  3. * suite of networking protocols. It is based on the
  4. * TCP/IP code written by Phil Karn et al, as found in
  5. * his NET package for Packet Radio communications.
  6. *
  7. * This file contains an implementation of the "server"
  8. * for the TELNET protocol. This protocol can be used to
  9. * remote-login on other systems, just like a normal TTY
  10. * session.
  11. *
  12. * Usage: telnetd [-dv]
  13. *
  14. * Version: @(#)telnetd.c 1.00 07/26/92
  15. *
  16. * Author: Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
  17. * Michael Temari, <temari@temari.ae.ge.com>
  18. */
  19. #include <sys/types.h>
  20. #include <fcntl.h>
  21. #include <sys/wait.h>
  22. #include <sys/ioctl.h>
  23. #include <sys/stat.h>
  24. #include <errno.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <unistd.h>
  28. #include <time.h>
  29. #include <stdio.h>
  30. #include <ttyent.h>
  31. #include <utmp.h>
  32. #include <net/gen/in.h>
  33. #include <net/gen/tcp.h>
  34. #include <net/gen/tcp_io.h>
  35. #include <net/gen/socket.h>
  36. #include <net/gen/netdb.h>
  37. #include <net/gen/inet.h>
  38. #include "telnetd.h"
  39. #if 0
  40. static char *Version = "@(#) telnetd 1.00 (07/26/92)";
  41. #endif
  42. int opt_d = 0; /* debugging output flag */
  43. void usage(void);
  44. int main(int argc, char *argv[]);
  45. void wtmp(int type, int linenr, char *line, pid_t pid, char *host);
  46. void usage()
  47. {
  48. fprintf(stderr, "Usage: telnetd [-dv]\n");
  49. exit(-1);
  50. }
  51. int main(argc, argv)
  52. int argc;
  53. char *argv[];
  54. {
  55. char buff[128];
  56. register int c;
  57. int pty_fd;
  58. int tty_fd;
  59. pid_t pid;
  60. int lineno;
  61. char *tty_name;
  62. struct ttyent *ttyp;
  63. nwio_tcpconf_t tcpconf;
  64. struct hostent *hostent;
  65. char *hostname;
  66. opterr = 0;
  67. while ((c = getopt(argc, argv, "dv")) != EOF) switch(c) {
  68. case 'd':
  69. case 'v':
  70. opt_d = 1;
  71. break;
  72. default:
  73. usage();
  74. }
  75. /* No more arguments allowed. */
  76. if (optind != argc) usage();
  77. /* Obtain the name of the remote host. */
  78. if (ioctl(0, NWIOGTCPCONF, &tcpconf) < 0) {
  79. sprintf(buff, "Unable to obtain your IP address\r\n");
  80. (void) write(1, buff, strlen(buff));
  81. return(-1);
  82. }
  83. if ((hostent = gethostbyaddr((char *) &tcpconf.nwtc_remaddr,
  84. sizeof(tcpconf.nwtc_remaddr), AF_INET)) != NULL) {
  85. hostname = hostent->h_name;
  86. } else {
  87. hostname = inet_ntoa(tcpconf.nwtc_remaddr);
  88. }
  89. /* Try allocating a PTY. */
  90. if (get_pty(&pty_fd, &tty_name) < 0) {
  91. sprintf(buff, "I am sorry, but there is no free PTY left!\r\n");
  92. (void) write(1, buff, strlen(buff));
  93. return(-1);
  94. }
  95. /* Find the tty in the tty table. */
  96. lineno = 0;
  97. for (;;) {
  98. if ((ttyp = getttyent()) == NULL) {
  99. sprintf(buff, "Can't find the tty entry in the tty table\r\n");
  100. (void) write(1, buff, strlen(buff));
  101. }
  102. if (strcmp(ttyp->ty_name, tty_name+5) == 0) break;
  103. lineno++;
  104. }
  105. endttyent();
  106. /* Initialize the connection to an 8 bit clean channel. */
  107. term_init();
  108. /* Fork off a child process and have it execute a getty(8). */
  109. if ((pid = fork()) == 0) {
  110. /* Set up a new session. */
  111. setsid();
  112. if ((tty_fd = open(tty_name, O_RDWR)) < 0) {
  113. sprintf(buff, "Can't open %s\r\n", tty_name);
  114. (void) write(1, buff, strlen(buff));
  115. return(-1);
  116. }
  117. close(pty_fd);
  118. dup2(tty_fd, 0);
  119. dup2(tty_fd, 1);
  120. dup2(tty_fd, 2);
  121. close(tty_fd);
  122. (void) execl("/usr/sbin/getty", "getty", (char *)NULL);
  123. (void) execl("/usr/bin/getty", "getty", (char *)NULL);
  124. (void) execl("/usr/bin/login", "login", (char *)NULL);
  125. (void) write(1, "EXEC failed!\r\n", 14);
  126. } else if (pid < 0) {
  127. sprintf(buff, "I am sorry, but the fork(2) call failed!\r\n");
  128. (void) write(1, buff, strlen(buff));
  129. (void) close(pty_fd);
  130. return(-1);
  131. }
  132. wtmp(LOGIN_PROCESS, lineno, tty_name+5, pid, hostname);
  133. term_inout(pty_fd);
  134. (void) close(pty_fd);
  135. wtmp(DEAD_PROCESS, lineno, tty_name+5, pid, hostname);
  136. chown(tty_name, 0, 0);
  137. chmod(tty_name, 0666);
  138. return(0);
  139. }