release.sh: allow REPO and GITBRANCH env override
[minix.git] / commands / telnetd / main.c
blobe0bdf4c94b81dca3927a9f99c45b289e9774df8f
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 #if 0
41 static char *Version = "@(#) telnetd 1.00 (07/26/92)";
42 #endif
44 int opt_d = 0; /* debugging output flag */
46 void usage(void);
47 int main(int argc, char *argv[]);
48 void wtmp(int type, int linenr, char *line, pid_t pid, char *host);
50 void usage()
52 fprintf(stderr, "Usage: telnetd [-dv]\n");
54 exit(-1);
57 int main(argc, argv)
58 int argc;
59 char *argv[];
61 char buff[128];
62 register int c;
63 int pty_fd;
64 int tty_fd;
65 pid_t pid;
66 int lineno;
67 char *tty_name;
68 struct ttyent *ttyp;
69 nwio_tcpconf_t tcpconf;
70 struct hostent *hostent;
71 char *hostname;
73 opterr = 0;
74 while ((c = getopt(argc, argv, "dv")) != EOF) switch(c) {
75 case 'd':
76 case 'v':
77 opt_d = 1;
78 break;
79 default:
80 usage();
83 /* No more arguments allowed. */
84 if (optind != argc) usage();
86 /* Obtain the name of the remote host. */
87 if (ioctl(0, NWIOGTCPCONF, &tcpconf) < 0) {
88 sprintf(buff, "Unable to obtain your IP address\r\n");
89 (void) write(1, buff, strlen(buff));
90 return(-1);
92 if ((hostent = gethostbyaddr((char *) &tcpconf.nwtc_remaddr,
93 sizeof(tcpconf.nwtc_remaddr), AF_INET)) != NULL) {
94 hostname = hostent->h_name;
95 } else {
96 hostname = inet_ntoa(tcpconf.nwtc_remaddr);
99 /* Try allocating a PTY. */
100 if (get_pty(&pty_fd, &tty_name) < 0) {
101 sprintf(buff, "I am sorry, but there is no free PTY left!\r\n");
102 (void) write(1, buff, strlen(buff));
103 return(-1);
106 /* Find the tty in the tty table. */
107 lineno = 0;
108 for (;;) {
109 if ((ttyp = getttyent()) == NULL) {
110 sprintf(buff, "Can't find the tty entry in the tty table\r\n");
111 (void) write(1, buff, strlen(buff));
113 if (strcmp(ttyp->ty_name, tty_name+5) == 0) break;
114 lineno++;
116 endttyent();
118 /* Initialize the connection to an 8 bit clean channel. */
119 term_init();
121 /* Fork off a child process and have it execute a getty(8). */
122 if ((pid = fork()) == 0) {
123 /* Set up a new session. */
124 setsid();
125 if ((tty_fd = open(tty_name, O_RDWR)) < 0) {
126 sprintf(buff, "Can't open %s\r\n", tty_name);
127 (void) write(1, buff, strlen(buff));
128 return(-1);
131 close(pty_fd);
132 dup2(tty_fd, 0);
133 dup2(tty_fd, 1);
134 dup2(tty_fd, 2);
135 close(tty_fd);
136 (void) execl("/usr/sbin/getty", "getty", (char *)NULL);
137 (void) execl("/usr/bin/getty", "getty", (char *)NULL);
138 (void) execl("/usr/bin/login", "login", (char *)NULL);
139 (void) write(1, "EXEC failed!\r\n", 14);
140 } else if (pid < 0) {
141 sprintf(buff, "I am sorry, but the fork(2) call failed!\r\n");
142 (void) write(1, buff, strlen(buff));
143 (void) close(pty_fd);
144 return(-1);
147 wtmp(LOGIN_PROCESS, lineno, tty_name+5, pid, hostname);
149 term_inout(pty_fd);
151 (void) close(pty_fd);
153 wtmp(DEAD_PROCESS, lineno, tty_name+5, pid, hostname);
155 chown(tty_name, 0, 0);
156 chmod(tty_name, 0666);
158 return(0);