source: trunk/minix/commands/telnetd/wtmp.c@ 20

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

Minix 3.1.2a

File size: 2.9 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 <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"
29
30static char PATH_UTMP[] = "/etc/utmp";
31static char PATH_WTMP[] = "/usr/adm/wtmp";
32
33_PROTOTYPE(void wtmp, (int type, int linenr, char *line, pid_t pid,
34 char *host));
35_PROTOTYPE(void report, (char *label));
36
37void wtmp(type, linenr, line, pid, host)
38int type; /* type of entry */
39int linenr; /* line number in ttytab */
40char *line; /* tty name (only good on login) */
41pid_t pid; /* pid of process */
42char *host; /* name of the remote host */
43{
44/* Log an event into the UTMP and WTMP files. */
45
46 struct utmp utmp; /* UTMP/WTMP User Accounting */
47 int fd;
48
49 /* Clear the utmp record. */
50 memset((void *) &utmp, 0, sizeof(utmp));
51
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;
59
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.
63 */
64 if ((fd = open(PATH_UTMP, O_RDONLY)) < 0) {
65 if (errno != ENOENT) report(PATH_UTMP);
66 return;
67 }
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;
74 }
75 close(fd);
76 if (utmp.ut_type != USER_PROCESS) return;
77 strncpy(utmp.ut_name, "", sizeof(utmp.ut_name));
78 break;
79 }
80
81 /* Finish new utmp entry. */
82 utmp.ut_pid = pid;
83 utmp.ut_type = type;
84 utmp.ut_time = time((time_t *) 0);
85
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);
92 }
93 if (fd != -1) close(fd);
94
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);
101 }
102 if (fd != -1) close(fd);
103 }
104}
105
106void report(label)
107char *label;
108{
109 char message[128];
110
111 sprintf(message, "telnetd: %s: %s\r\n", strerror(errno));
112 (void) write(1, message, strlen(message));
113}
Note: See TracBrowser for help on using the repository browser.