utmpx.c revision 1.17 1 /* $NetBSD: utmpx.c,v 1.17 2003/02/26 19:23:25 christos 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.17 2003/02/26 19:23:25 christos Exp $");
42 #endif /* LIBC_SCCS and not lint */
43
44 #include <sys/types.h>
45 #include <sys/param.h>
46 #include <sys/wait.h>
47 #include <sys/socket.h>
48 #include <sys/time.h>
49 #include <sys/stat.h>
50
51 #include <assert.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <vis.h>
56 #include <utmp.h>
57 #include <utmpx.h>
58 #include <unistd.h>
59 #include <fcntl.h>
60 #include <errno.h>
61 #include <db.h>
62
63 static FILE *fp;
64 static int readonly = 0;
65 static struct utmpx ut;
66 static char utfile[MAXPATHLEN] = _PATH_UTMPX;
67 static char llfile[MAXPATHLEN] = _PATH_LASTLOGX;
68
69 static struct utmpx *utmp_update(const struct utmpx *);
70
71 static const char vers[] = "utmpx-1.00";
72
73 void
74 setutxent()
75 {
76
77 (void)memset(&ut, 0, sizeof(ut));
78 if (fp == NULL)
79 return;
80 (void)fseeko(fp, (off_t)sizeof(ut), SEEK_SET);
81 }
82
83
84 void
85 endutxent()
86 {
87
88 (void)memset(&ut, 0, sizeof(ut));
89 if (fp != NULL) {
90 (void)fclose(fp);
91 fp = NULL;
92 readonly = 0;
93 }
94 }
95
96
97 struct utmpx *
98 getutxent()
99 {
100
101 if (fp == NULL) {
102 struct stat st;
103
104 if ((fp = fopen(utfile, "r+")) == NULL)
105 if ((fp = fopen(utfile, "w+")) == NULL) {
106 if ((fp = fopen(utfile, "r")) == NULL)
107 goto fail;
108 else
109 readonly = 1;
110 }
111
112
113 /* get file size in order to check if new file */
114 if (fstat(fileno(fp), &st) == -1)
115 goto failclose;
116
117 if (st.st_size == 0) {
118 /* new file, add signature record */
119 (void)memset(&ut, 0, sizeof(ut));
120 ut.ut_type = SIGNATURE;
121 (void)memcpy(ut.ut_user, vers, sizeof(vers));
122 if (fwrite(&ut, sizeof(ut), 1, fp) != 1)
123 goto failclose;
124 } else {
125 /* old file, read signature record */
126 if (fread(&ut, sizeof(ut), 1, fp) != 1)
127 goto failclose;
128 if (memcmp(ut.ut_user, vers, sizeof(vers)) != 0 ||
129 ut.ut_type != SIGNATURE)
130 goto failclose;
131 }
132 }
133
134 if (fread(&ut, sizeof(ut), 1, fp) != 1)
135 goto fail;
136
137 return &ut;
138 failclose:
139 (void)fclose(fp);
140 fail:
141 (void)memset(&ut, 0, sizeof(ut));
142 return NULL;
143 }
144
145
146 struct utmpx *
147 getutxid(const struct utmpx *utx)
148 {
149
150 _DIAGASSERT(utx != NULL);
151
152 if (utx->ut_type == EMPTY)
153 return NULL;
154
155 do {
156 if (ut.ut_type == EMPTY)
157 continue;
158 switch (utx->ut_type) {
159 case EMPTY:
160 return NULL;
161 case RUN_LVL:
162 case BOOT_TIME:
163 case OLD_TIME:
164 case NEW_TIME:
165 if (ut.ut_type == utx->ut_type)
166 return &ut;
167 break;
168 case INIT_PROCESS:
169 case LOGIN_PROCESS:
170 case USER_PROCESS:
171 case DEAD_PROCESS:
172 switch (ut.ut_type) {
173 case INIT_PROCESS:
174 case LOGIN_PROCESS:
175 case USER_PROCESS:
176 case DEAD_PROCESS:
177 if (memcmp(ut.ut_id, utx->ut_id,
178 sizeof(ut.ut_id)) == 0)
179 return &ut;
180 break;
181 default:
182 break;
183 }
184 break;
185 default:
186 return NULL;
187 }
188 } while (getutxent() != NULL);
189 return NULL;
190 }
191
192
193 struct utmpx *
194 getutxline(const struct utmpx *utx)
195 {
196
197 _DIAGASSERT(utx != NULL);
198
199 do {
200 switch (ut.ut_type) {
201 case EMPTY:
202 break;
203 case LOGIN_PROCESS:
204 case USER_PROCESS:
205 if (strncmp(ut.ut_line, utx->ut_line,
206 sizeof(ut.ut_line)) == 0)
207 return &ut;
208 break;
209 default:
210 break;
211 }
212 } while (getutxent() != NULL);
213 return NULL;
214 }
215
216
217 struct utmpx *
218 pututxline(const struct utmpx *utx)
219 {
220 struct utmpx temp, *u = NULL;
221 int gotlock = 0;
222
223 _DIAGASSERT(utx != NULL);
224
225 if (utx == NULL)
226 return NULL;
227
228 if (strcmp(_PATH_UTMPX, utfile) == 0)
229 if ((fp != NULL && readonly) || (fp == NULL && geteuid() != 0))
230 return utmp_update(utx);
231
232
233 (void)memcpy(&temp, utx, sizeof(temp));
234
235 if (fp == NULL) {
236 (void)getutxent();
237 if (fp == NULL || readonly)
238 return NULL;
239 }
240
241 if (getutxid(&temp) == NULL) {
242 setutxent();
243 if (getutxid(&temp) == NULL) {
244 if (lockf(fileno(fp), F_LOCK, (off_t)0) == -1)
245 return NULL;
246 gotlock++;
247 if (fseeko(fp, (off_t)0, SEEK_END) == -1)
248 goto fail;
249 }
250 }
251
252 if (!gotlock) {
253 /* we are not appending */
254 if (fseeko(fp, -(off_t)sizeof(ut), SEEK_CUR) == -1)
255 return NULL;
256 }
257
258 if (fwrite(&temp, sizeof (temp), 1, fp) != 1)
259 goto fail;
260
261 if (fflush(fp) == -1)
262 goto fail;
263
264 u = memcpy(&ut, &temp, sizeof(ut));
265 fail:
266 if (gotlock) {
267 if (lockf(fileno(fp), F_ULOCK, (off_t)0) == -1)
268 return NULL;
269 }
270 return u;
271 }
272
273
274 static struct utmpx *
275 utmp_update(const struct utmpx *utx)
276 {
277 char buf[sizeof(*utx) * 4 + 1];
278 pid_t pid;
279 int status;
280
281 _DIAGASSERT(utx != NULL);
282
283 (void)strvisx(buf, (const char *)(const void *)utx, sizeof(*utx),
284 VIS_WHITE);
285 switch (pid = fork()) {
286 case 0:
287 (void)execl(_PATH_UTMP_UPDATE,
288 strrchr(_PATH_UTMP_UPDATE, '/') + 1, buf, NULL);
289 exit(1);
290 /*NOTREACHED*/
291 case -1:
292 return NULL;
293 default:
294 if (waitpid(pid, &status, 0) == -1)
295 return NULL;
296 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
297 return memcpy(&ut, utx, sizeof(ut));
298 return NULL;
299 }
300
301 }
302
303 /*
304 * The following are extensions and not part of the X/Open spec.
305 */
306 int
307 updwtmpx(const char *file, const struct utmpx *utx)
308 {
309 int fd;
310
311 _DIAGASSERT(file != NULL);
312 _DIAGASSERT(utx != NULL);
313
314 fd = open(file, O_WRONLY|O_APPEND|O_EXLOCK);
315
316 if (fd == -1) {
317 if ((fd = open(file, O_CREAT|O_WRONLY|O_EXLOCK, 0644)) == -1)
318 return -1;
319 (void)memset(&ut, 0, sizeof(ut));
320 ut.ut_type = SIGNATURE;
321 (void)memcpy(ut.ut_user, vers, sizeof(vers));
322 if (write(fd, &ut, sizeof(ut)) == -1)
323 return -1;
324 }
325 if (write(fd, utx, sizeof(*utx)) == -1)
326 return -1;
327 if (close(fd) == -1)
328 return -1;
329 return 0;
330 }
331
332
333 int
334 utmpxname(const char *fname)
335 {
336 size_t len;
337
338 _DIAGASSERT(fname != NULL);
339
340 len = strlen(fname);
341
342 if (len >= sizeof(utfile))
343 return 0;
344
345 /* must end in x! */
346 if (fname[len - 1] != 'x')
347 return 0;
348
349 (void)strlcpy(utfile, fname, sizeof(utfile));
350 endutxent();
351 return 1;
352 }
353
354
355 void
356 getutmp(const struct utmpx *ux, struct utmp *u)
357 {
358
359 _DIAGASSERT(ux != NULL);
360 _DIAGASSERT(u != NULL);
361
362 (void)memcpy(u->ut_name, ux->ut_name, sizeof(u->ut_name));
363 (void)memcpy(u->ut_line, ux->ut_line, sizeof(u->ut_line));
364 (void)memcpy(u->ut_host, ux->ut_host, sizeof(u->ut_host));
365 u->ut_time = ux->ut_tv.tv_sec;
366 }
367
368 void
369 getutmpx(const struct utmp *u, struct utmpx *ux)
370 {
371
372 _DIAGASSERT(ux != NULL);
373 _DIAGASSERT(u != NULL);
374
375 (void)memcpy(ux->ut_name, u->ut_name, sizeof(u->ut_name));
376 (void)memcpy(ux->ut_line, u->ut_line, sizeof(u->ut_line));
377 (void)memcpy(ux->ut_host, u->ut_host, sizeof(u->ut_host));
378 ux->ut_tv.tv_sec = u->ut_time;
379 ux->ut_tv.tv_usec = 0;
380 (void)memset(&ux->ut_ss, 0, sizeof(ux->ut_ss));
381 ux->ut_pid = 0;
382 ux->ut_type = USER_PROCESS;
383 ux->ut_session = 0;
384 ux->ut_exit.e_termination = 0;
385 ux->ut_exit.e_exit = 0;
386 }
387
388 int
389 lastlogxname(const char *fname)
390 {
391 size_t len;
392
393 _DIAGASSERT(fname != NULL);
394
395 len = strlen(fname);
396
397 if (len >= sizeof(llfile))
398 return 0;
399
400 /* must end in x! */
401 if (fname[len - 1] != 'x')
402 return 0;
403
404 (void)strlcpy(llfile, fname, sizeof(llfile));
405 return 1;
406 }
407
408 struct lastlogx *
409 getlastlogx(uid_t uid, struct lastlogx *ll)
410 {
411 DBT key, data;
412 DB *db;
413
414 _DIAGASSERT(ll != NULL);
415
416 db = dbopen(llfile, O_RDONLY|O_SHLOCK, 0, DB_HASH, NULL);
417
418 if (db == NULL)
419 return NULL;
420
421 key.data = &uid;
422 key.size = sizeof(uid);
423
424 if ((db->get)(db, &key, &data, 0) != 0)
425 goto error;
426
427 if (data.size != sizeof(*ll)) {
428 errno = EFTYPE;
429 goto error;
430 }
431
432 if (ll == NULL)
433 if ((ll = malloc(sizeof(*ll))) == NULL)
434 goto done;
435
436 (void)memcpy(ll, data.data, sizeof(*ll));
437 goto done;
438 error:
439 ll = NULL;
440 done:
441 (db->close)(db);
442 return ll;
443 }
444
445 int
446 updlastlogx(const char *fname, uid_t uid, struct lastlogx *ll)
447 {
448 DBT key, data;
449 int error = 0;
450 DB *db;
451
452 _DIAGASSERT(fname != NULL);
453 _DIAGASSERT(ll != NULL);
454
455 db = dbopen(fname, O_RDWR|O_CREAT|O_EXLOCK, 0, DB_HASH, NULL);
456
457 if (db == NULL)
458 return -1;
459
460 key.data = &uid;
461 key.size = sizeof(uid);
462 data.data = ll;
463 data.size = sizeof(*ll);
464 if ((db->put)(db, &key, &data, 0) != 0)
465 error = -1;
466
467 (db->close)(db);
468 return error;
469 }
470