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