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
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>
30 static char PATH_UTMP
[] = "/etc/utmp";
31 static char PATH_WTMP
[] = "/usr/adm/wtmp";
33 _PROTOTYPE(void wtmp
, (int type
, int linenr
, char *line
, pid_t pid
,
35 _PROTOTYPE(void report
, (char *label
));
37 void wtmp(type
, linenr
, line
, pid
, host
)
38 int type
; /* type of entry */
39 int linenr
; /* line number in ttytab */
40 char *line
; /* tty name (only good on login) */
41 pid_t pid
; /* pid of process */
42 char *host
; /* name of the remote host */
44 /* Log an event into the UTMP and WTMP files. */
46 struct utmp utmp
; /* UTMP/WTMP User Accounting */
49 /* Clear the utmp record. */
50 memset((void *) &utmp
, 0, sizeof(utmp
));
55 /* A new login, fill in line and host name. */
56 strncpy(utmp
.ut_line
, line
, sizeof(utmp
.ut_line
));
57 strncpy(utmp
.ut_host
, host
, sizeof(utmp
.ut_host
));
61 /* A logout. Use the current utmp entry, but make sure it is a
62 * user process exiting, and not getty or login giving up.
64 if ((fd
= open(PATH_UTMP
, O_RDONLY
)) < 0) {
65 if (errno
!= ENOENT
) report(PATH_UTMP
);
68 if (lseek(fd
, (off_t
) (linenr
+1) * sizeof(utmp
), SEEK_SET
) == -1
69 || read(fd
, &utmp
, sizeof(utmp
)) == -1
76 if (utmp
.ut_type
!= USER_PROCESS
) return;
77 strncpy(utmp
.ut_name
, "", sizeof(utmp
.ut_name
));
81 /* Finish new utmp entry. */
84 utmp
.ut_time
= time((time_t *) 0);
86 /* Write new entry to utmp. */
87 if ((fd
= open(PATH_UTMP
, O_WRONLY
)) < 0
88 || lseek(fd
, (off_t
) (linenr
+1) * sizeof(utmp
), SEEK_SET
) == -1
89 || write(fd
, &utmp
, sizeof(utmp
)) == -1
91 if (errno
!= ENOENT
) report(PATH_UTMP
);
93 if (fd
!= -1) close(fd
);
95 if (type
== DEAD_PROCESS
) {
96 /* Add new wtmp entry. */
97 if ((fd
= open(PATH_WTMP
, O_WRONLY
| O_APPEND
)) < 0
98 || write(fd
, &utmp
, sizeof(utmp
)) == -1
100 if (errno
!= ENOENT
) report(PATH_WTMP
);
102 if (fd
!= -1) close(fd
);
111 sprintf(message
, "telnetd: %s: %s\r\n", strerror(errno
));
112 (void) write(1, message
, strlen(message
));