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