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