port of netbsd's tr
[minix.git] / commands / telnetd / wtmp.c
blob47769ce18c1c3556885a31b6a5ab29171cafd4ec
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 <errno.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <utmp.h>
26 #include <time.h>
27 #include <stdio.h>
28 #include "telnetd.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,
34 char *host));
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 */
47 int fd;
49 /* Clear the utmp record. */
50 memset((void *) &utmp, 0, sizeof(utmp));
52 /* Fill in utmp. */
53 switch (type) {
54 case LOGIN_PROCESS:
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));
58 break;
60 case DEAD_PROCESS:
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);
66 return;
68 if (lseek(fd, (off_t) (linenr+1) * sizeof(utmp), SEEK_SET) == -1
69 || read(fd, &utmp, sizeof(utmp)) == -1
70 ) {
71 report(PATH_UTMP);
72 close(fd);
73 return;
75 close(fd);
76 if (utmp.ut_type != USER_PROCESS) return;
77 strncpy(utmp.ut_name, "", sizeof(utmp.ut_name));
78 break;
81 /* Finish new utmp entry. */
82 utmp.ut_pid = pid;
83 utmp.ut_type = type;
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
90 ) {
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
99 ) {
100 if (errno != ENOENT) report(PATH_WTMP);
102 if (fd != -1) close(fd);
106 void report(label)
107 char *label;
109 char message[128];
111 sprintf(message, "telnetd: %s: %s\r\n", strerror(errno));
112 (void) write(1, message, strlen(message));