summaryrefslogtreecommitdiff
path: root/src/utmps/utmps-utmpd.c
blob: ec82f2ec326d5a3eef39839c5689d294c66fade1 (plain)
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
/* ISC license. */

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

#include <skalibs/posixishard.h>
#include <skalibs/posixplz.h>
#include <skalibs/types.h>
#include <skalibs/env.h>
#include <skalibs/allreadwrite.h>
#include <skalibs/buffer.h>
#include <skalibs/strerr2.h>
#include <skalibs/tai.h>
#include <skalibs/djbunix.h>
#include <skalibs/unix-timed.h>

#include <utmps/utmpx.h>
#include "utmps-internal.h"

static int fd = -1 ;

static void get0 (char *s, size_t n)
{
  tain deadline ;
  tain_ulong(&deadline, 30) ;
  tain_add_g(&deadline, &deadline) ;
  if (buffer_timed_get_g(buffer_0small, s, n, &deadline) < n)
    strerr_diefu1sys(111, "read from stdin") ;
}

static void flush1 (void)
{
  tain deadline ;
  tain_ulong(&deadline, 30) ;
  tain_add_g(&deadline, &deadline) ;
  if (!buffer_timed_flush_g(buffer_1small, &deadline))
    strerr_diefu1sys(111, "write to stdout") ;
}

static void answer (int e)
{
  unsigned char c = e ;
  buffer_putnoflush(buffer_1small, (char *)&c, 1) ;
  flush1() ;
}

static void maybe_open (void)
{
  if (fd < 0)
  {
    mode_t m = umask(0) ;
    fd = open("utmp", O_RDWR | O_CREAT, 0644) ;
    umask(m) ;
    if (fd < 0)
    {
      answer(errno) ;
      strerr_diefu1sys(111, "open utmp file") ;
    }
  }
}

static int read_utmp_entry_unlocked (char *s)
{
  ssize_t r = read(fd, s, sizeof(struct utmpx)) ;
  if (r < 0) goto err ;
  if (!r) return 0 ;
  if (r == sizeof(struct utmpx)) return 1 ;
  errno = EPIPE ;
 err:
  unlink_void("utmp") ;
  answer(errno) ;
  strerr_diefu1sys(111, "read utmp file") ;
}

static void lockit (int w)
{
  if (fd_lock(fd, w, 0) < 1)
  {
    unlink_void("utmp") ;
    answer(errno) ;
    strerr_diefu1sys(111, "read utmp file") ;
  }
}

static inline void unlockit (void)
{
  fd_unlock(fd) ;
}

static int idmatch (unsigned short type, char const *id, struct utmpx const *b)
{
  if (type == BOOT_TIME || type == OLD_TIME || type == NEW_TIME)
  {
    if (type == (unsigned short)b->ut_type) return 1 ;
  }
  else if (type == INIT_PROCESS || type == LOGIN_PROCESS || type == USER_PROCESS || type == DEAD_PROCESS)
  {
    if ((b->ut_type == INIT_PROCESS || b->ut_type == LOGIN_PROCESS || b->ut_type == USER_PROCESS || b->ut_type == DEAD_PROCESS)
      && !strncmp(id, b->ut_id, UTMPS_UT_IDSIZE)) return 1 ;
  }
  return 0 ;
}

static inline int onestepback (void)
{
  return lseek(fd, -(off_t)sizeof(struct utmpx), SEEK_CUR) >= 0 ;
}

static void do_getent (void)
{
  struct utmpx b ;
  char buf[1 + sizeof(struct utmpx)] = "" ;
  maybe_open() ;
  lockit(0) ;
  if (!read_utmp_entry_unlocked(buf+1))
  {
    unlockit() ;
    answer(ESRCH) ;
    return ;
  }
  unlockit() ;
  utmps_utmpx_unpack(buf+1, &b) ;
  utmps_utmpx_pack(buf+1, &b) ;
  buffer_putnoflush(buffer_1small, buf, 1 + sizeof(struct utmpx)) ;
  flush1() ;
}

static void do_getid (void)
{
  unsigned short type ;
  char rbuf[USHORT_PACK + UTMPS_UT_IDSIZE] ;
  char sbuf[1 + sizeof(struct utmpx)] = "" ;
  get0(rbuf, USHORT_PACK + UTMPS_UT_IDSIZE) ;
  ushort_unpack_big(rbuf, &type) ;
  maybe_open() ;
  lockit(0) ;
  for (;;)
  {
    struct utmpx b ;
    if (!read_utmp_entry_unlocked(sbuf+1))
    {
      unlockit() ;
      answer(ESRCH) ;
      return ;
    }
    utmps_utmpx_unpack(sbuf+1, &b) ;
    if (idmatch(type, rbuf + USHORT_PACK, &b)) break ;
  }
  unlockit() ;
  buffer_putnoflush(buffer_1small, sbuf, 1 + sizeof(struct utmpx)) ;
  flush1() ;
}

static void do_getline (void)
{
  char rbuf[UTMPS_UT_LINESIZE] ;
  char sbuf[1 + sizeof(struct utmpx)] = "" ;
  get0(rbuf, UTMPS_UT_LINESIZE) ;
  maybe_open() ;
  lockit(0) ;
  for (;;)
  {
    struct utmpx b ;
    if (!read_utmp_entry_unlocked(sbuf+1))
    {
      unlockit() ;
      answer(ESRCH) ;
      return ;
    }
    utmps_utmpx_unpack(sbuf+1, &b) ;
    if ((b.ut_type == LOGIN_PROCESS || b.ut_type == USER_PROCESS)
      && !strncmp(rbuf, b.ut_line, UTMPS_UT_LINESIZE)) break ;
  }
  unlockit() ;
  buffer_putnoflush(buffer_1small, sbuf, 1 + sizeof(struct utmpx)) ;
  flush1() ;
}

static void do_putline (uid_t uid, gid_t gid)
{
  struct utmpx u ;
  char buf[sizeof(struct utmpx)] ;
  get0(buf, sizeof(struct utmpx)) ;
  if (uid && gid != getegid())
  {
    answer(EPERM) ;
    return ;
  }
  utmps_utmpx_unpack(buf, &u) ;
  maybe_open() ;
  lockit(1) ;
  onestepback() ;  /* compatibility with glibc implementation */
  for (;;)
  {
    struct utmpx b ;
    char tmp[sizeof(struct utmpx)] ;
    if (!read_utmp_entry_unlocked(tmp)) break ;
    utmps_utmpx_unpack(tmp, &b) ;
    if (idmatch(u.ut_type, u.ut_id, &b) && !strncmp(u.ut_line, b.ut_line, UTMPS_UT_LINESIZE))
    {
      if (!onestepback())
      {
        unlockit() ;
        answer(errno) ;
        return ;
      }
      break ;
    }
  }
  utmps_utmpx_pack(buf, &u) ;
  if (allwrite(fd, buf, sizeof(struct utmpx)) < sizeof(struct utmpx))
  {
    unlockit() ;
    answer(errno) ;
    strerr_diefu1sys(111, "write to utmp") ;
  }
  fsync(fd) ;
  unlockit() ;
  answer(0) ;
}

static void do_rewind (void)
{
  maybe_open() ;
  if (lseek(fd, 0, SEEK_SET) < 0) { answer(errno) ; return ; }
  answer(0) ;
}

int main (void)
{
  uid_t uid ;
  gid_t gid ;
  char const *x ;
  PROG = "utmps-utmpd" ;
  x = ucspi_get("REMOTEEUID") ;
  if (!x) strerr_diefu1x(100, "get $IPCREMOTEEUID from environment") ;
  if (!uid0_scan(x, &uid)) strerr_dieinvalid(100, "IPCREMOTEEUID") ;
  x = ucspi_get("REMOTEEGID") ;
  if (!x) strerr_diefu1x(100, "get $IPCREMOTEEGID from environment") ;
  if (!gid0_scan(x, &gid)) strerr_dieinvalid(100, "IPCREMOTEEGID") ;
  if (ndelay_on(0) < 0) strerr_diefu1sys(111, "set stdin non-blocking") ;
  tain_now_set_stopwatch_g() ;

  for (;;)
  {
    tain deadline ;
    char c ;
    tain_add_g(&deadline, &tain_infinite_relative) ;
    if (!buffer_timed_get_g(buffer_0small, &c, 1, &deadline)) break ;
    switch (c)
    {
      case 'e' : do_getent() ; break ;
      case 'i' : do_getid() ; break ;
      case 'l' : do_getline() ; break ;
      case 'E' : do_putline(uid, gid) ; break ;
      case 'r' : do_rewind() ; break ;
      default :
        errno = EPROTO ;
        strerr_diefu1sys(1, "interpret stdin") ;
    }
  }
  return 0 ;
}