source: trunk/minix/commands/telnetd/main.c@ 10

Last change on this file since 10 was 9, checked in by Mattia Monga, 13 years ago

Minix 3.1.2a

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