utmpx.c revision 1.16 1 1.16 christos /* $NetBSD: utmpx.c,v 1.16 2002/11/26 16:52:07 christos Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.1 christos * by Christos Zoulas.
9 1.1 christos *
10 1.1 christos * Redistribution and use in source and binary forms, with or without
11 1.1 christos * modification, are permitted provided that the following conditions
12 1.1 christos * are met:
13 1.1 christos * 1. Redistributions of source code must retain the above copyright
14 1.1 christos * notice, this list of conditions and the following disclaimer.
15 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 christos * notice, this list of conditions and the following disclaimer in the
17 1.1 christos * documentation and/or other materials provided with the distribution.
18 1.1 christos * 3. All advertising materials mentioning features or use of this software
19 1.1 christos * must display the following acknowledgement:
20 1.1 christos * This product includes software developed by the NetBSD
21 1.1 christos * Foundation, Inc. and its contributors.
22 1.1 christos * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 christos * contributors may be used to endorse or promote products derived
24 1.1 christos * from this software without specific prior written permission.
25 1.1 christos *
26 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 christos * POSSIBILITY OF SUCH DAMAGE.
37 1.1 christos */
38 1.1 christos #include <sys/cdefs.h>
39 1.1 christos
40 1.1 christos #if defined(LIBC_SCCS) && !defined(lint)
41 1.16 christos __RCSID("$NetBSD: utmpx.c,v 1.16 2002/11/26 16:52:07 christos Exp $");
42 1.1 christos #endif /* LIBC_SCCS and not lint */
43 1.1 christos
44 1.1 christos #include <sys/types.h>
45 1.1 christos #include <sys/param.h>
46 1.1 christos #include <sys/wait.h>
47 1.1 christos #include <sys/socket.h>
48 1.1 christos #include <sys/time.h>
49 1.1 christos #include <sys/stat.h>
50 1.1 christos
51 1.14 wiz #include <assert.h>
52 1.1 christos #include <stdio.h>
53 1.4 simonb #include <stdlib.h>
54 1.1 christos #include <string.h>
55 1.1 christos #include <vis.h>
56 1.5 christos #include <utmp.h>
57 1.1 christos #include <utmpx.h>
58 1.1 christos #include <unistd.h>
59 1.3 christos #include <fcntl.h>
60 1.9 christos #include <errno.h>
61 1.9 christos #include <db.h>
62 1.1 christos
63 1.1 christos static FILE *fp;
64 1.1 christos static struct utmpx ut;
65 1.1 christos static char utfile[MAXPATHLEN] = _PATH_UTMPX;
66 1.9 christos static char llfile[MAXPATHLEN] = _PATH_LASTLOGX;
67 1.1 christos
68 1.1 christos static struct utmpx *utmp_update(const struct utmpx *);
69 1.1 christos
70 1.1 christos static const char vers[] = "utmpx-1.00";
71 1.1 christos
72 1.1 christos void
73 1.1 christos setutxent()
74 1.1 christos {
75 1.7 simonb
76 1.1 christos (void)memset(&ut, 0, sizeof(ut));
77 1.1 christos if (fp == NULL)
78 1.1 christos return;
79 1.1 christos (void)fseeko(fp, (off_t)sizeof(ut), SEEK_SET);
80 1.1 christos }
81 1.1 christos
82 1.1 christos
83 1.1 christos void
84 1.1 christos endutxent()
85 1.1 christos {
86 1.7 simonb
87 1.1 christos (void)memset(&ut, 0, sizeof(ut));
88 1.8 christos if (fp != NULL) {
89 1.1 christos (void)fclose(fp);
90 1.8 christos fp = NULL;
91 1.8 christos }
92 1.1 christos }
93 1.1 christos
94 1.1 christos
95 1.1 christos struct utmpx *
96 1.1 christos getutxent()
97 1.1 christos {
98 1.7 simonb
99 1.1 christos if (fp == NULL) {
100 1.1 christos struct stat st;
101 1.1 christos
102 1.1 christos if ((fp = fopen(utfile, "r+")) == NULL)
103 1.6 christos if ((fp = fopen(utfile, "w+")) == NULL)
104 1.6 christos if ((fp = fopen(utfile, "r")) == NULL)
105 1.6 christos goto fail;
106 1.1 christos
107 1.1 christos /* get file size in order to check if new file */
108 1.1 christos if (fstat(fileno(fp), &st) == -1)
109 1.1 christos goto failclose;
110 1.1 christos
111 1.1 christos if (st.st_size == 0) {
112 1.1 christos /* new file, add signature record */
113 1.1 christos (void)memset(&ut, 0, sizeof(ut));
114 1.1 christos ut.ut_type = SIGNATURE;
115 1.2 christos (void)memcpy(ut.ut_user, vers, sizeof(vers));
116 1.6 christos if (fwrite(&ut, sizeof(ut), 1, fp) != 1)
117 1.1 christos goto failclose;
118 1.1 christos } else {
119 1.1 christos /* old file, read signature record */
120 1.6 christos if (fread(&ut, sizeof(ut), 1, fp) != 1)
121 1.1 christos goto failclose;
122 1.2 christos if (memcmp(ut.ut_user, vers, sizeof(vers)) != 0 ||
123 1.1 christos ut.ut_type != SIGNATURE)
124 1.1 christos goto failclose;
125 1.1 christos }
126 1.1 christos }
127 1.1 christos
128 1.6 christos if (fread(&ut, sizeof(ut), 1, fp) != 1)
129 1.1 christos goto fail;
130 1.1 christos
131 1.1 christos return &ut;
132 1.1 christos failclose:
133 1.1 christos (void)fclose(fp);
134 1.1 christos fail:
135 1.1 christos (void)memset(&ut, 0, sizeof(ut));
136 1.1 christos return NULL;
137 1.1 christos }
138 1.1 christos
139 1.1 christos
140 1.1 christos struct utmpx *
141 1.1 christos getutxid(const struct utmpx *utx)
142 1.1 christos {
143 1.7 simonb
144 1.14 wiz _DIAGASSERT(utx != NULL);
145 1.14 wiz
146 1.1 christos if (utx->ut_type == EMPTY)
147 1.1 christos return NULL;
148 1.1 christos
149 1.1 christos do {
150 1.1 christos if (ut.ut_type == EMPTY)
151 1.1 christos continue;
152 1.1 christos switch (utx->ut_type) {
153 1.1 christos case EMPTY:
154 1.1 christos return NULL;
155 1.1 christos case RUN_LVL:
156 1.1 christos case BOOT_TIME:
157 1.1 christos case OLD_TIME:
158 1.1 christos case NEW_TIME:
159 1.1 christos if (ut.ut_type == utx->ut_type)
160 1.1 christos return &ut;
161 1.1 christos break;
162 1.1 christos case INIT_PROCESS:
163 1.1 christos case LOGIN_PROCESS:
164 1.1 christos case USER_PROCESS:
165 1.1 christos case DEAD_PROCESS:
166 1.1 christos switch (ut.ut_type) {
167 1.1 christos case INIT_PROCESS:
168 1.1 christos case LOGIN_PROCESS:
169 1.1 christos case USER_PROCESS:
170 1.1 christos case DEAD_PROCESS:
171 1.1 christos if (memcmp(ut.ut_id, utx->ut_id,
172 1.1 christos sizeof(ut.ut_id)) == 0)
173 1.1 christos return &ut;
174 1.1 christos break;
175 1.1 christos default:
176 1.1 christos break;
177 1.1 christos }
178 1.1 christos break;
179 1.1 christos default:
180 1.1 christos return NULL;
181 1.1 christos }
182 1.7 simonb } while (getutxent() != NULL);
183 1.1 christos return NULL;
184 1.1 christos }
185 1.1 christos
186 1.1 christos
187 1.1 christos struct utmpx *
188 1.1 christos getutxline(const struct utmpx *utx)
189 1.1 christos {
190 1.7 simonb
191 1.14 wiz _DIAGASSERT(utx != NULL);
192 1.14 wiz
193 1.1 christos do {
194 1.1 christos switch (ut.ut_type) {
195 1.1 christos case EMPTY:
196 1.1 christos break;
197 1.1 christos case LOGIN_PROCESS:
198 1.1 christos case USER_PROCESS:
199 1.1 christos if (strncmp(ut.ut_line, utx->ut_line,
200 1.1 christos sizeof(ut.ut_line)) == 0)
201 1.1 christos return &ut;
202 1.1 christos break;
203 1.1 christos default:
204 1.1 christos break;
205 1.1 christos }
206 1.7 simonb } while (getutxent() != NULL);
207 1.1 christos return NULL;
208 1.1 christos }
209 1.1 christos
210 1.1 christos
211 1.1 christos struct utmpx *
212 1.1 christos pututxline(const struct utmpx *utx)
213 1.1 christos {
214 1.1 christos struct utmpx temp, *u = NULL;
215 1.1 christos int gotlock = 0;
216 1.1 christos
217 1.14 wiz _DIAGASSERT(utx != NULL);
218 1.14 wiz
219 1.1 christos if (strcmp(_PATH_UTMPX, utfile) == 0 && geteuid() != 0)
220 1.1 christos return utmp_update(utx);
221 1.1 christos
222 1.1 christos if (utx == NULL)
223 1.1 christos return NULL;
224 1.1 christos
225 1.1 christos (void)memcpy(&temp, utx, sizeof(temp));
226 1.1 christos
227 1.1 christos if (fp == NULL) {
228 1.1 christos (void)getutxent();
229 1.1 christos if (fp == NULL)
230 1.1 christos return NULL;
231 1.1 christos }
232 1.1 christos
233 1.1 christos if (getutxid(&temp) == NULL) {
234 1.1 christos setutxent();
235 1.1 christos if (getutxid(&temp) == NULL) {
236 1.1 christos if (lockf(fileno(fp), F_LOCK, (off_t)0) == -1)
237 1.1 christos return NULL;
238 1.1 christos gotlock++;
239 1.1 christos if (fseeko(fp, (off_t)0, SEEK_END) == -1)
240 1.1 christos goto fail;
241 1.1 christos }
242 1.1 christos }
243 1.1 christos
244 1.1 christos if (!gotlock) {
245 1.1 christos /* we are not appending */
246 1.1 christos if (fseeko(fp, -(off_t)sizeof(ut), SEEK_CUR) == -1)
247 1.1 christos return NULL;
248 1.1 christos }
249 1.1 christos
250 1.1 christos if (fwrite(&temp, sizeof (temp), 1, fp) != 1)
251 1.1 christos goto fail;
252 1.1 christos
253 1.1 christos if (fflush(fp) == -1)
254 1.1 christos goto fail;
255 1.1 christos
256 1.1 christos u = memcpy(&ut, &temp, sizeof(ut));
257 1.1 christos fail:
258 1.1 christos if (gotlock) {
259 1.1 christos if (lockf(fileno(fp), F_ULOCK, (off_t)0) == -1)
260 1.1 christos return NULL;
261 1.1 christos }
262 1.1 christos return u;
263 1.1 christos }
264 1.1 christos
265 1.1 christos
266 1.1 christos static struct utmpx *
267 1.1 christos utmp_update(const struct utmpx *utx)
268 1.1 christos {
269 1.1 christos char buf[sizeof(*utx) * 4 + 1];
270 1.1 christos pid_t pid;
271 1.1 christos int status;
272 1.1 christos
273 1.14 wiz _DIAGASSERT(utx != NULL);
274 1.14 wiz
275 1.1 christos (void)strvisx(buf, (const char *)(const void *)utx, sizeof(*utx),
276 1.1 christos VIS_WHITE);
277 1.1 christos switch (pid = fork()) {
278 1.1 christos case 0:
279 1.1 christos (void)execl(_PATH_UTMP_UPDATE,
280 1.16 christos strrchr(_PATH_UTMP_UPDATE, '/') + 1, buf, NULL);
281 1.1 christos exit(1);
282 1.1 christos /*NOTREACHED*/
283 1.1 christos case -1:
284 1.1 christos return NULL;
285 1.1 christos default:
286 1.1 christos if (waitpid(pid, &status, 0) == -1)
287 1.1 christos return NULL;
288 1.1 christos if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
289 1.1 christos return memcpy(&ut, utx, sizeof(ut));
290 1.1 christos return NULL;
291 1.1 christos }
292 1.1 christos
293 1.3 christos }
294 1.3 christos
295 1.11 wiz /*
296 1.11 wiz * The following are extensions and not part of the X/Open spec.
297 1.11 wiz */
298 1.9 christos int
299 1.3 christos updwtmpx(const char *file, const struct utmpx *utx)
300 1.3 christos {
301 1.14 wiz int fd;
302 1.14 wiz
303 1.14 wiz _DIAGASSERT(file != NULL);
304 1.14 wiz _DIAGASSERT(utx != NULL);
305 1.14 wiz
306 1.14 wiz fd = open(file, O_WRONLY|O_APPEND|O_EXLOCK);
307 1.7 simonb
308 1.3 christos if (fd == -1) {
309 1.9 christos if ((fd = open(file, O_CREAT|O_WRONLY|O_EXLOCK, 0644)) == -1)
310 1.9 christos return -1;
311 1.3 christos (void)memset(&ut, 0, sizeof(ut));
312 1.3 christos ut.ut_type = SIGNATURE;
313 1.3 christos (void)memcpy(ut.ut_user, vers, sizeof(vers));
314 1.13 christos if (write(fd, &ut, sizeof(ut)) == -1)
315 1.12 christos return -1;
316 1.3 christos }
317 1.12 christos if (write(fd, utx, sizeof(*utx)) == -1)
318 1.12 christos return -1;
319 1.12 christos if (close(fd) == -1)
320 1.12 christos return -1;
321 1.9 christos return 0;
322 1.1 christos }
323 1.1 christos
324 1.1 christos
325 1.1 christos int
326 1.1 christos utmpxname(const char *fname)
327 1.1 christos {
328 1.14 wiz size_t len;
329 1.14 wiz
330 1.14 wiz _DIAGASSERT(fname != NULL);
331 1.14 wiz
332 1.14 wiz len = strlen(fname);
333 1.1 christos
334 1.1 christos if (len >= sizeof(utfile))
335 1.1 christos return 0;
336 1.1 christos
337 1.1 christos /* must end in x! */
338 1.1 christos if (fname[len - 1] != 'x')
339 1.1 christos return 0;
340 1.1 christos
341 1.15 itojun (void)strlcpy(utfile, fname, sizeof(utfile));
342 1.1 christos endutxent();
343 1.1 christos return 1;
344 1.5 christos }
345 1.5 christos
346 1.5 christos
347 1.5 christos void
348 1.5 christos getutmp(const struct utmpx *ux, struct utmp *u)
349 1.5 christos {
350 1.7 simonb
351 1.14 wiz _DIAGASSERT(ux != NULL);
352 1.14 wiz _DIAGASSERT(u != NULL);
353 1.14 wiz
354 1.5 christos (void)memcpy(u->ut_name, ux->ut_name, sizeof(u->ut_name));
355 1.5 christos (void)memcpy(u->ut_line, ux->ut_line, sizeof(u->ut_line));
356 1.5 christos (void)memcpy(u->ut_host, ux->ut_host, sizeof(u->ut_host));
357 1.5 christos u->ut_time = ux->ut_tv.tv_sec;
358 1.5 christos }
359 1.5 christos
360 1.5 christos void
361 1.5 christos getutmpx(const struct utmp *u, struct utmpx *ux)
362 1.5 christos {
363 1.7 simonb
364 1.14 wiz _DIAGASSERT(ux != NULL);
365 1.14 wiz _DIAGASSERT(u != NULL);
366 1.14 wiz
367 1.5 christos (void)memcpy(ux->ut_name, u->ut_name, sizeof(u->ut_name));
368 1.5 christos (void)memcpy(ux->ut_line, u->ut_line, sizeof(u->ut_line));
369 1.5 christos (void)memcpy(ux->ut_host, u->ut_host, sizeof(u->ut_host));
370 1.5 christos ux->ut_tv.tv_sec = u->ut_time;
371 1.5 christos ux->ut_tv.tv_usec = 0;
372 1.5 christos (void)memset(&ux->ut_ss, 0, sizeof(ux->ut_ss));
373 1.5 christos ux->ut_pid = 0;
374 1.5 christos ux->ut_type = USER_PROCESS;
375 1.5 christos ux->ut_session = 0;
376 1.5 christos ux->ut_exit.e_termination = 0;
377 1.5 christos ux->ut_exit.e_exit = 0;
378 1.9 christos }
379 1.9 christos
380 1.9 christos int
381 1.9 christos lastlogxname(const char *fname)
382 1.9 christos {
383 1.14 wiz size_t len;
384 1.14 wiz
385 1.14 wiz _DIAGASSERT(fname != NULL);
386 1.14 wiz
387 1.14 wiz len = strlen(fname);
388 1.9 christos
389 1.9 christos if (len >= sizeof(llfile))
390 1.9 christos return 0;
391 1.9 christos
392 1.9 christos /* must end in x! */
393 1.9 christos if (fname[len - 1] != 'x')
394 1.9 christos return 0;
395 1.9 christos
396 1.15 itojun (void)strlcpy(llfile, fname, sizeof(llfile));
397 1.9 christos return 1;
398 1.9 christos }
399 1.9 christos
400 1.9 christos struct lastlogx *
401 1.9 christos getlastlogx(uid_t uid, struct lastlogx *ll)
402 1.9 christos {
403 1.9 christos DBT key, data;
404 1.14 wiz DB *db;
405 1.14 wiz
406 1.14 wiz _DIAGASSERT(ll != NULL);
407 1.14 wiz
408 1.14 wiz db = dbopen(llfile, O_RDONLY|O_SHLOCK, 0, DB_HASH, NULL);
409 1.9 christos
410 1.9 christos if (db == NULL)
411 1.9 christos return NULL;
412 1.9 christos
413 1.9 christos key.data = &uid;
414 1.9 christos key.size = sizeof(uid);
415 1.9 christos
416 1.9 christos if ((db->get)(db, &key, &data, 0) != 0)
417 1.9 christos goto error;
418 1.9 christos
419 1.9 christos if (data.size != sizeof(*ll)) {
420 1.9 christos errno = EFTYPE;
421 1.9 christos goto error;
422 1.9 christos }
423 1.9 christos
424 1.9 christos if (ll == NULL)
425 1.9 christos if ((ll = malloc(sizeof(*ll))) == NULL)
426 1.9 christos goto done;
427 1.9 christos
428 1.9 christos (void)memcpy(ll, data.data, sizeof(*ll));
429 1.9 christos goto done;
430 1.9 christos error:
431 1.9 christos ll = NULL;
432 1.9 christos done:
433 1.9 christos (db->close)(db);
434 1.9 christos return ll;
435 1.9 christos }
436 1.9 christos
437 1.9 christos int
438 1.9 christos updlastlogx(const char *fname, uid_t uid, struct lastlogx *ll)
439 1.9 christos {
440 1.9 christos DBT key, data;
441 1.9 christos int error = 0;
442 1.14 wiz DB *db;
443 1.14 wiz
444 1.14 wiz _DIAGASSERT(fname != NULL);
445 1.14 wiz _DIAGASSERT(ll != NULL);
446 1.14 wiz
447 1.14 wiz db = dbopen(fname, O_RDWR|O_CREAT|O_EXLOCK, 0, DB_HASH, NULL);
448 1.9 christos
449 1.9 christos if (db == NULL)
450 1.9 christos return -1;
451 1.9 christos
452 1.9 christos key.data = &uid;
453 1.9 christos key.size = sizeof(uid);
454 1.9 christos data.data = ll;
455 1.9 christos data.size = sizeof(*ll);
456 1.9 christos if ((db->put)(db, &key, &data, 0) != 0)
457 1.9 christos error = -1;
458 1.9 christos
459 1.9 christos (db->close)(db);
460 1.9 christos return error;
461 1.1 christos }
462