-
Notifications
You must be signed in to change notification settings - Fork 0
/
utmp.c
299 lines (256 loc) · 6.53 KB
/
utmp.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/* utmp.c Shareware Copyright by Sam Lantinga 10/6/93 */
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <utmp.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "splitvt.h"
#ifdef DEBUG_UTMP
#undef UTMP_FILE
#define UTMP_FILE "/tmp/utmp"
#else
#ifndef UTMP_FILE
#define UTMP_FILE "/var/run/utmp"
#endif /* UTMP_FILE */
#endif /* DEBUG_UTMP */
int get_utmp(char *tty, struct utmp *save);
int set_utmp(char *tty, struct utmp *save);
/* Remove us from the utmp file, saving our entry to replace later */
static struct utmp saved_utmp;
static int utmp_saved=0;
static char saved_tty[128];
int remove_me(void)
{
struct utmp ut;
char *tty;
time_t now;
if ( ! isatty(0) )
return(-1);
tty=(char *)ttyname(0);
if ( tty == NULL || strlen(tty)+1 > sizeof(saved_tty) )
return(-1);
/* Retrieve our utmp record */
(void) time(&now);
if ( get_utmp(tty, &ut) == 0 ) {
/* Save the utmp entry and tty pathname */
utmp_saved=1;
d_copy((char *)&ut, (char *)&saved_utmp, sizeof(ut));
strcpy(saved_tty, tty);
/* Clean out the entry and return */
ut.ut_name[0]='\0';
ut.ut_time=now;
#ifdef USER_PROCESS
ut.ut_type = DEAD_PROCESS;
#endif
#ifdef HAVE_UTHOST
ut.ut_host[0]='\0';
#endif
return(set_utmp(tty, &ut));
}
/* Nothing to clean out, good. */
return(0);
}
int replace_me(void)
{
if ( utmp_saved )
return(set_utmp(saved_tty, &saved_utmp));
return(0);
}
int get_utmp(char *tty, struct utmp *save)
{
int fd;
char *ttyptr;
struct utmp ut;
/* See if we can open the utmp file */
if ( (fd=open(UTMP_FILE, O_RDWR)) < 0 )
return(-1);
/* Get the ttyxy form of the tty pathname if possible. */
if ( *tty == '/' ) {
for ( ttyptr=(tty+1); *ttyptr; ++ttyptr ) {
if ( *ttyptr == '/' )
break;
}
if ( *ttyptr == '/' )
++ttyptr;
} else
ttyptr=tty;
while (read(fd,(char *) &ut, sizeof(ut)) == sizeof(ut)) {
if (strncmp(ttyptr, ut.ut_line, sizeof(ut.ut_line)) == 0) {
/* Break out; we've found our entry! */
if ( save )
d_copy((char *)&ut, (char *)&save, sizeof(ut));
close(fd);
return(0);
}
}
/* We never found an entry for our tty */
close(fd);
return(-1);
}
int set_utmp(char *tty, struct utmp *save)
{
int fd, found=0;
char *ttyptr;
struct utmp ut;
/* See if we can open the utmp file */
if ( (fd=open(UTMP_FILE, O_RDWR)) < 0 )
return(-1);
/* Get the ttyxy form of the tty pathname if possible. */
if ( *tty == '/' ) {
for ( ttyptr=(tty+1); *ttyptr; ++ttyptr ) {
if ( *ttyptr == '/' )
break;
}
if ( *ttyptr == '/' )
++ttyptr;
} else
ttyptr=tty;
while (read(fd,(char *) &ut, sizeof(ut)) == sizeof(ut)) {
if (strncmp(ttyptr, ut.ut_line, sizeof(ut.ut_line)) == 0) {
found=1;
lseek(fd, -(long)sizeof(struct utmp), 1);
break;
}
}
/* Add a new entry to the utmp file if we can't find our entry */
if ( ! found )
{ /* Reopen to avoid a race with other end-of-utmp entries. */
(void) close(fd);
if ( (fd=open(UTMP_FILE, (O_RDWR|O_APPEND))) < 0 )
return -1;
}
if (write(fd, (char *)save, sizeof(*save)) != sizeof(*save)) {
(void) close(fd);
return -1;
}
return(close(fd));
}
/* Set up a utmp entry and tty for a user */
int addutmp(char *user, int uid, char *tty)
/*char *user; /* The user to add to the utmp file */
/*int uid; /* The uid corresponding to user */
/*char *tty; /* /dev/ttyxx */
{
#if !defined(SOLARIS) && !defined(IRIX) && !defined(__GLIBC__)
struct stat sb;
#endif
struct utmp ut;
char *ttyptr;
/* Retrieve any existing utmp entry */
d_zero((char *)&ut, sizeof(ut));
#if 0 /* Outdated */
(void) get_utmp(tty, &ut);
#endif /* Outdated */
/* Get the ttyxy form of the tty pathname if possible. */
if ( *tty == '/' ) {
for ( ttyptr=(tty+1); *ttyptr; ++ttyptr ) {
if ( *ttyptr == '/' )
break;
}
if ( *ttyptr == '/' )
++ttyptr;
} else
ttyptr=tty;
/* Customize the utmp entry */
strncpy(ut.ut_name, user, sizeof(ut.ut_name)-1);
ut.ut_name[sizeof(ut.ut_name)-1]='\0';
strncpy(ut.ut_line, ttyptr, sizeof(ut.ut_line)-1);
ut.ut_line[sizeof(ut.ut_line)-1]='\0';
#ifdef USER_PROCESS
ut.ut_type=USER_PROCESS;
ut.ut_pid=getpid();
#endif
#if defined(HAVE_UTHOST)
# if 0 /* Outdated */
/* remove_me() should be called before this function */
if ( utmp_saved ) {
strncpy(ut.ut_host, saved_utmp.ut_host, sizeof(ut.ut_host)-1);
ut.ut_host[sizeof(ut.ut_host)-1]='\0';
}
# else /* Portable and fun. */
strncpy(ut.ut_host, "splitvt", 8);
# endif
#endif
#if __WORDSIZE == 64 && __WORDSIZE_COMPAT32
/* 'time_t' is 64-bit, 'ut.ut_time' is 32-bit. */
{
time_t now;
(void) time(&now);
ut.ut_time = now & 0xffffffff; /* Discard upper bits. */
}
#else /* Equal size time representation. */
(void) time(&ut.ut_time);
#endif
#if !defined(SOLARIS) && !defined(IRIX) && !defined(__GLIBC__)
/* Solaris and Irix and GLIBC machines do this automatically */
/* Change the ownership and mode of the tty */
if ( stat(tty, &sb) == 0 ) {
(void) chmod(tty, 0620); /* crw--w---- */
(void) chown(tty, uid, sb.st_gid);
}
#endif
#if 0 /* Outdated */
return(set_utmp(tty, &ut));
#else /* Only the safe use of /var/log/wtmp. */
logwtmp(ttyptr, user, "splitvt");
return 0;
#endif
}
/* End a utmp entry and tty for a user and a tty */
int delutmp(char *user, char *tty)
/*char *tty; /* /dev/ttyxx */
{
struct stat sb;
struct utmp ut;
int retval=0;
#if 0 /* Outdated manual manipulations. */
/* Retrieve any existing utmp entry */
d_zero((char *)&ut, sizeof(ut));
if ( get_utmp(tty, &ut) == 0 ) {
/* Clear the utmp entry */
ut.ut_name[0]='\0';
#ifdef USER_PROCESS
ut.ut_type=DEAD_PROCESS;
#endif
#if defined(HAVE_UTHOST)
ut.ut_host[0]='\0';
#endif
#if __WORDSIZE == 64 && __WORDSIZE_COMPAT32
/* 'time_t' is 64-bit, 'ut.ut_time' is 32-bit. */
{
time_t now;
(void) time(&now);
ut.ut_time = now & 0xffffffff; /* Discard bits. */
}
#else /* Equal size time representation. */
(void) time(&ut.ut_time);
#endif
retval=set_utmp(tty, &ut);
}
#else /* Portable, half-way contemporary approach. */
char *ttyptr;
/* Get the ttyxy form of the tty pathname if possible. */
if ( *tty == '/' ) {
for ( ttyptr=(tty+1); *ttyptr; ++ttyptr ) {
if ( *ttyptr == '/' )
break;
}
if ( *ttyptr == '/' )
++ttyptr;
} else
ttyptr=tty;
logwtmp(ttyptr, "", "");
#endif
#if !defined(SOLARIS) && !defined(IRIX)
/* Solaris and Irix machines do this automatically */
/* Reset the owner and mode of the tty */
if ( stat(tty, &sb) == 0 ) {
(void) chmod(tty, 0666); /* crw-rw-rw- */
(void) chown(tty, 0, sb.st_gid);
}
#endif
return(retval);
}