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
|
/* ISC license. */
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#include <pwd.h>
#include <skalibs/types.h>
#include <skalibs/error.h>
#include <skalibs/allreadwrite.h>
#include <skalibs/buffer.h>
#include <skalibs/strerr2.h>
#include <skalibs/env.h>
#include <skalibs/tai.h>
#include <skalibs/djbunix.h>
#include <skalibs/unix-timed.h>
#include <utmps/utmpx.h>
#include "utmps-internal.h"
static void answer (int e)
{
unsigned char c = e ;
write(1, (char *)&c, 1) ;
}
int main (void)
{
struct utmpx b ;
char const *x ;
tain_t deadline ;
size_t w ;
uid_t uid ;
int fd ;
char buf[sizeof(struct utmpx)] ;
PROG = "utmps-wtmpd" ;
x = ucspi_get("REMOTEEUID") ;
if (!x) strerr_diefu1x(100, "get $IPCREMOTEEUID from environment") ;
if (!uid0_scan(x, &uid)) strerr_dieinvalid(100, "IPCREMOTEEUID") ;
if (ndelay_on(0) < 0) strerr_diefu1sys(111, "set stdin non-blocking") ;
tain_now_g() ;
tain_ulong(&deadline, 30) ;
tain_add_g(&deadline, &deadline) ;
w = buffer_timed_get_g(buffer_0small, buf, 1, &deadline) ;
if (!w) strerr_diefu1sys(111, "read from stdin") ;
if (buf[0] != '+') { errno = EPROTO ; strerr_diefu1sys(111, "read command") ; }
w = buffer_timed_get_g(buffer_0small, buf, sizeof(struct utmpx), &deadline) ;
if (w < sizeof(struct utmpx)) strerr_diefu1sys(111, "read from stdin") ;
utmps_utmpx_unpack(buf, &b) ;
if (uid)
{
struct passwd *pw ;
errno = 0 ;
pw = getpwnam(b.ut_user) ;
if (!pw)
{
if (errno)
{
answer(errno) ;
strerr_diefu1sys(111, "read user database") ;
}
else
{
answer(EPERM) ;
strerr_diefu2x(1, "verify ut_user identity", ": no such user") ;
}
}
if (pw->pw_uid != uid)
{
answer(EPERM) ;
strerr_diefu2x(1, "verify ut_user identity", ": uid mismatch") ;
}
}
fd = open_append("wtmp") ;
if (fd < 0)
{
answer(errno) ;
strerr_diefu1sys(111, "open wtmp") ;
}
if (lock_ex(fd) < 0)
{
answer(errno) ;
strerr_diefu1sys(111, "open wtmp") ;
}
if (lseek(fd, 0, SEEK_END) < 0)
{
answer(errno) ;
strerr_diefu1sys(111, "lseek on wtmp") ;
}
w = allwrite(fd, buf, sizeof(struct utmpx)) ;
if (w < sizeof(struct utmpx))
{
int e = errno ;
if (w)
{
struct stat st ;
if (!fstat(fd, &st)) ftruncate(fd, st.st_size - w) ;
}
answer(e) ;
strerr_diefu1sys(111, "append to wtmp") ;
}
fsync(fd) ;
answer(0) ;
return 0 ;
}
|