vm: fix region reporting bug
[minix.git] / commands / telnetd / main.c
blobf0620c6c9ee7847e737558bfb60a2aa56970301f
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.
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.
12 * Usage: telnetd [-dv]
14 * Version: @(#)telnetd.c 1.00 07/26/92
16 * Author: Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
17 * Michael Temari, <temari@temari.ae.ge.com>
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"
40 static char *Version = "@(#) telnetd 1.00 (07/26/92)";
42 int opt_d = 0; /* debugging output flag */
44 void usage(void);
45 int main(int argc, char *argv[]);
46 void wtmp(int type, int linenr, char *line, pid_t pid, char *host);
48 void usage()
50 fprintf(stderr, "Usage: telnetd [-dv]\n");
52 exit(-1);
55 int main(argc, argv)
56 int argc;
57 char *argv[];
59 char buff[128];
60 register int c;
61 int pty_fd;
62 int tty_fd;
63 pid_t pid;
64 int lineno;
65 char *tty_name;
66 struct ttyent *ttyp;
67 nwio_tcpconf_t tcpconf;
68 struct hostent *hostent;
69 char *hostname;
71 opterr = 0;
72 while ((c = getopt(argc, argv, "dv")) != EOF) switch(c) {
73 case 'd':
74 case 'v':
75 opt_d = 1;
76 break;
77 default:
78 usage();
81 /* No more arguments allowed. */
82 if (optind != argc) usage();
84 /* Obtain the name of the remote host. */
85 if (ioctl(0, NWIOGTCPCONF, &tcpconf) < 0) {
86 sprintf(buff, "Unable to obtain your IP address\r\n");
87 (void) write(1, buff, strlen(buff));
88 return(-1);
90 if ((hostent = gethostbyaddr((char *) &tcpconf.nwtc_remaddr,
91 sizeof(tcpconf.nwtc_remaddr), AF_INET)) != NULL) {
92 hostname = hostent->h_name;
93 } else {
94 hostname = inet_ntoa(tcpconf.nwtc_remaddr);
97 /* Try allocating a PTY. */
98 if (get_pty(&pty_fd, &tty_name) < 0) {
99 sprintf(buff, "I am sorry, but there is no free PTY left!\r\n");
100 (void) write(1, buff, strlen(buff));
101 return(-1);
104 /* Find the tty in the tty table. */
105 lineno = 0;
106 for (;;) {
107 if ((ttyp = getttyent()) == NULL) {
108 sprintf(buff, "Can't find %s in the tty table\r\n");
109 (void) write(1, buff, strlen(buff));
111 if (strcmp(ttyp->ty_name, tty_name+5) == 0) break;
112 lineno++;
114 endttyent();
116 /* Initialize the connection to an 8 bit clean channel. */
117 term_init();
119 /* Fork off a child process and have it execute a getty(8). */
120 if ((pid = fork()) == 0) {
121 /* Set up a new session. */
122 setsid();
123 if ((tty_fd = open(tty_name, O_RDWR)) < 0) {
124 sprintf(buff, "Can't open %s\r\n", tty_name);
125 (void) write(1, buff, strlen(buff));
126 return(-1);
129 close(pty_fd);
130 dup2(tty_fd, 0);
131 dup2(tty_fd, 1);
132 dup2(tty_fd, 2);
133 close(tty_fd);
134 (void) execl("/usr/sbin/getty", "getty", (char *)NULL);
135 (void) execl("/usr/bin/getty", "getty", (char *)NULL);
136 (void) execl("/usr/bin/login", "login", (char *)NULL);
137 (void) write(1, "EXEC failed!\r\n", 14);
138 } else if (pid < 0) {
139 sprintf(buff, "I am sorry, but the fork(2) call failed!\r\n");
140 (void) write(1, buff, strlen(buff));
141 (void) close(pty_fd);
142 return(-1);
145 wtmp(LOGIN_PROCESS, lineno, tty_name+5, pid, hostname);
147 term_inout(pty_fd);
149 (void) close(pty_fd);
151 wtmp(DEAD_PROCESS, lineno, tty_name+5, pid, hostname);
153 chown(tty_name, 0, 0);
154 chmod(tty_name, 0666);
156 return(0);