-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathnmr_unix.c
157 lines (141 loc) · 4.4 KB
/
nmr_unix.c
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
/* NMR.C V2.7-mea
| Copyright (c) 1988,1989,1990 by
| The Hebrew University of Jerusalem, Computation Center.
|
| This software is distributed under a license from the Hebrew University
| of Jerusalem. It may be copied only under the terms listed in the license
| agreement. This copyright message should never be changed or removed.
| This software is gievn without any warranty, and the Hebrew University
| of Jerusalem assumes no responsibility for any damage that might be caused
| by use or misuse of this software.
| UNIX environment specific versions of some routines.
*/
/*
| Loop over all users in the system. Look for those that are interactive.
| from those either list all of them (if UserName points to Null), or look for
| a specific username (if UserName points to some string).
| If a list of users is requested, we try to block 6 usernames in a single
| line.
*/
#include "consts.h"
#include "prototypes.h"
#include <utmpx.h>
#include <time.h>
#ifndef UTMPX_FILE
#define UTMPX_FILE "/var/run/utmp"
#endif
void
list_users(Faddress, Taddress, cmd, UserName)
const char *Faddress, *Taddress;
char *UserName;
const char cmd;
{
char line[LINESIZE];
int fd, lines;
struct utmpx Utmp;
struct passwd *pwd;
struct stat stats;
time_t now;
char tty[16];
char idle[8];
int idles;
char *s;
int users = 0;
time(&now);
if ((fd = open(UTMPX_FILE, O_RDONLY, 0600)) < 0) {
sprintf(line,
"Can't open `%s' for reading. No CPQ USER commands now.",
UTMPX_FILE);
send_nmr(Faddress, Taddress, line, strlen(line), ASCII, CMD_MSG);
} else {
lines = 0;
while (read(fd, (void*)&Utmp, sizeof Utmp) == sizeof Utmp) {
char uname[9];
if (Utmp.ut_type != USER_PROCESS) continue;
if (*Utmp.ut_user == 0) continue; /* Try next */
strncpy(uname, Utmp.ut_user, 8);
uname[8] = 0;
pwd = getpwnam(uname);
if (pwd == NULL) continue; /* Hmm ??? */
++users;
if (cmd == 'U' && (*UserName == 0 ||
strcasecmp(UserName, uname) != 0))
continue; /* It's CPQ U <name> -command */
sprintf(tty, "/dev/%s", Utmp.ut_line);
stats.st_mode = 0;
stat(tty, &stats);
++lines;
if (1 == lines) {
strcpy(line, "Login Idle Terminal Name");
send_nmr(Faddress, Taddress, line, strlen(line), ASCII, CMD_MSG);
}
*idle = 0;
idles = now - stats.st_atime;
if (get_gone_user(uname, &s) >= 0) {
strcpy(idle, "GONE");
} else if (idles > 86400) {
sprintf(idle, "%dd%dh", idles/86400, (idles % 86400) / 3600);
} else if (idles > 3599) {
sprintf(idle, "%d:%02d", idles/3600, (idles/60)%60);
} else if (idles > 59) {
sprintf(idle, "%d", idles/60);
}
sprintf(line, "%-8s %6s %c%-12.12s %s",
uname, idle,
(stats.st_mode & 020) == 0 ? '*':' ', /* mesg n ? */
Utmp.ut_line, pwd->pw_gecos);
send_nmr(Faddress, Taddress, line, strlen(line), ASCII, CMD_MSG);
}
if (cmd == 'U' && *UserName == 0) {
sprintf(line, "CPQ: %4d USERS LOGGED ON", users);
send_nmr(Faddress, Taddress, line, strlen(line), ASCII, CMD_MSG);
} else if (lines == 0) {
if (*UserName == 0)
strcpy(line, "No one is logged on.");
else
sprintf(line, "User %s not logged on.", UserName);
send_nmr(Faddress, Taddress, line, strlen(line), ASCII, CMD_MSG);
}
close(fd);
}
}
/*
| Send the /etc/motd file to the requestor.
*/
void
send_welcome_message(Faddress, Taddress)
const char *Faddress, *Taddress;
{
char *p, line[LINESIZE];
FILE *fd;
if ((fd = fopen("/etc/motd", "r")) == NULL) {
logger(1, "NMR, Can't open `/etc/motd' for CPQ LOG command\n");
sprintf(line, "LOG file not available");
send_nmr(Faddress, Taddress, line, strlen(line), ASCII, CMD_MSG);
return;
}
while (fgets(line, sizeof line, fd) != NULL) {
if ((p = strchr(line, '\n')) != NULL) *p = '\0';
send_nmr(Faddress, Taddress, line, strlen(line), ASCII, CMD_MSG);
}
fclose(fd);
}
/*
| Send current time. (local time zone)
*/
void
send_cpu_time(Faddress, Taddress)
const char *Faddress, *Taddress;
{
char line[LINESIZE];
struct tm *tm, *localtime();
time_t clock;
time(&clock); /* Get the current time */
tm = localtime(&clock);
*line = 0;
strftime(line,sizeof line,"CPQ: TIME IS %T %Z %A %D",tm);
if (*line == 0) {
strcpy(line,"Can't format date string for CPQ TIME reply!");
}
send_nmr(Faddress, Taddress, line, strlen(line), ASCII, CMD_MSG);
}