2 * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 * This code is derived from software contributed to The NetBSD Foundation
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
30 #include "namespace.h"
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <sys/socket.h>
51 static FILE *fp
= NULL
;
52 static int readonly
= 0;
53 static struct utmpx ut
;
54 static char utfile
[MAXPATHLEN
] = _PATH_UTMPX
;
56 static struct utmpx
*utmp_update(const struct utmpx
*);
58 static const char vers
[] = "utmpx-2.00";
59 static utx_db_t dbtype
= UTX_DB_UTMPX
;
60 DB
*lastlogx_db
= NULL
;
67 if ((fp
= fopen(fname
, "r+")) == NULL
)
68 if ((fp
= fopen(fname
, "w+")) == NULL
) {
69 if ((fp
= fopen(fname
, "r")) == NULL
)
75 /* get file size in order to check if new file */
76 if (fstat(fileno(fp
), &st
) == -1)
79 if (st
.st_size
== 0) {
80 /* new file, add signature record */
81 (void)memset(&ut
, 0, sizeof(ut
));
82 ut
.ut_type
= SIGNATURE
;
83 (void)memcpy(ut
.ut_user
, vers
, sizeof(vers
));
84 if (fwrite(&ut
, sizeof(ut
), 1, fp
) != 1)
87 /* old file, read signature record */
88 if (fread(&ut
, sizeof(ut
), 1, fp
) != 1)
90 if (memcmp(ut
.ut_user
, vers
, 5) != 0 ||
91 ut
.ut_type
!= SIGNATURE
) {
101 (void)memset(&ut
, 0, sizeof(ut
));
106 setutxdb(utx_db_t db_type
, char *fname
)
124 /* A previous db file is still open */
129 if ((_open_db(fname
)) == -1)
139 (void)memset(&ut
, 0, sizeof(ut
));
144 if (dbtype
!= UTX_DB_UTMPX
)
145 setutxdb(UTX_DB_UTMPX
, utfile
);
147 (void)fseeko(fp
, (off_t
)sizeof(ut
), SEEK_SET
);
153 (void)memset(&ut
, 0, sizeof(ut
));
166 if ((_open_db(utfile
)) == -1)
170 if (fread(&ut
, sizeof(ut
), 1, fp
) != 1)
175 (void)memset(&ut
, 0, sizeof(ut
));
180 getutxid(const struct utmpx
*utx
)
183 _DIAGASSERT(utx
!= NULL
);
185 if (utx
->ut_type
== EMPTY
)
189 if (ut
.ut_type
== EMPTY
)
191 switch (utx
->ut_type
) {
198 if (ut
.ut_type
== utx
->ut_type
)
205 switch (ut
.ut_type
) {
210 if (memcmp(ut
.ut_id
, utx
->ut_id
,
211 sizeof(ut
.ut_id
)) == 0)
221 } while (getutxent() != NULL
);
226 getutxline(const struct utmpx
*utx
)
229 _DIAGASSERT(utx
!= NULL
);
232 switch (ut
.ut_type
) {
237 if (strncmp(ut
.ut_line
, utx
->ut_line
,
238 sizeof(ut
.ut_line
)) == 0)
244 } while (getutxent() != NULL
);
249 pututxline(const struct utmpx
*utx
)
253 struct utmpx temp
, *u
= NULL
;
256 _DIAGASSERT(utx
!= NULL
);
261 if (utx
->ut_type
== USER_PROCESS
) {
262 ll
.ll_tv
= utx
->ut_tv
;
263 strcpy(ll
.ll_host
, utx
->ut_host
);
264 strcpy(ll
.ll_line
, utx
->ut_line
);
265 pw
= getpwnam(utx
->ut_name
);
267 updlastlogx(_PATH_LASTLOGX
, pw
->pw_uid
, &ll
);
270 if (strcmp(_PATH_UTMPX
, utfile
) == 0)
271 if ((fp
!= NULL
&& readonly
) || (fp
== NULL
&& geteuid() != 0))
272 return utmp_update(utx
);
275 (void)memcpy(&temp
, utx
, sizeof(temp
));
279 if (fp
== NULL
|| readonly
)
283 if (getutxid(&temp
) == NULL
) {
285 if (getutxid(&temp
) == NULL
) {
286 if (lockf(fileno(fp
), F_LOCK
, (off_t
)0) == -1)
289 if (fseeko(fp
, (off_t
)0, SEEK_END
) == -1)
295 /* we are not appending */
296 if (fseeko(fp
, -(off_t
)sizeof(ut
), SEEK_CUR
) == -1)
300 if (fwrite(&temp
, sizeof (temp
), 1, fp
) != 1)
303 if (fflush(fp
) == -1)
306 u
= memcpy(&ut
, &temp
, sizeof(ut
));
309 if (lockf(fileno(fp
), F_ULOCK
, (off_t
)0) == -1)
315 static struct utmpx
*
316 utmp_update(const struct utmpx
*utx
)
318 char buf
[sizeof(*utx
) * 4 + 1];
322 _DIAGASSERT(utx
!= NULL
);
324 (void)strvisx(buf
, (const char *)(const void *)utx
, sizeof(*utx
),
326 switch (pid
= fork()) {
328 (void)execl(_PATH_UTMP_UPDATE
,
329 strrchr(_PATH_UTMP_UPDATE
, '/') + 1, buf
, NULL
);
335 if (waitpid(pid
, &status
, 0) == -1)
337 if (WIFEXITED(status
) && WEXITSTATUS(status
) == 0)
338 return memcpy(&ut
, utx
, sizeof(ut
));
345 * The following are extensions and not part of the X/Open spec.
348 updwtmpx(const char *file
, const struct utmpx
*utx
)
350 (void)_updwtmpx(file
, utx
);
354 _updwtmpx(const char *file
, const struct utmpx
*utx
)
359 _DIAGASSERT(file
!= NULL
);
360 _DIAGASSERT(utx
!= NULL
);
362 fd
= open(file
, O_WRONLY
|O_APPEND
|O_SHLOCK
);
365 if ((fd
= open(file
, O_CREAT
|O_WRONLY
|O_EXLOCK
, 0644)) == -1)
367 (void)memset(&ut
, 0, sizeof(ut
));
368 ut
.ut_type
= SIGNATURE
;
369 (void)memcpy(ut
.ut_user
, vers
, sizeof(vers
));
370 if (write(fd
, &ut
, sizeof(ut
)) == -1)
373 if (write(fd
, utx
, sizeof(*utx
)) == -1)
387 utmpxname(const char *fname
)
391 _DIAGASSERT(fname
!= NULL
);
395 if (len
>= sizeof(utfile
))
399 if (fname
[len
- 1] != 'x')
402 (void)strlcpy(utfile
, fname
, sizeof(utfile
));
408 getutmp(const struct utmpx
*ux
, struct utmp
*u
)
411 _DIAGASSERT(ux
!= NULL
);
412 _DIAGASSERT(u
!= NULL
);
414 (void)memcpy(u
->ut_name
, ux
->ut_name
, sizeof(u
->ut_name
));
415 (void)memcpy(u
->ut_line
, ux
->ut_line
, sizeof(u
->ut_line
));
416 (void)memcpy(u
->ut_host
, ux
->ut_host
, sizeof(u
->ut_host
));
417 u
->ut_time
= ux
->ut_tv
.tv_sec
;
421 getutmpx(const struct utmp
*u
, struct utmpx
*ux
)
424 _DIAGASSERT(ux
!= NULL
);
425 _DIAGASSERT(u
!= NULL
);
427 (void)memcpy(ux
->ut_name
, u
->ut_name
, sizeof(u
->ut_name
));
428 (void)memcpy(ux
->ut_line
, u
->ut_line
, sizeof(u
->ut_line
));
429 (void)memcpy(ux
->ut_host
, u
->ut_host
, sizeof(u
->ut_host
));
430 ux
->ut_tv
.tv_sec
= u
->ut_time
;
431 ux
->ut_tv
.tv_usec
= 0;
432 (void)memset(&ux
->ut_ss
, 0, sizeof(ux
->ut_ss
));
434 ux
->ut_type
= USER_PROCESS
;
436 ux
->ut_exit
.e_termination
= 0;
437 ux
->ut_exit
.e_exit
= 0;
441 getlastlogx(const char *fname
, uid_t uid
, struct lastlogx
*ll
)
446 _DIAGASSERT(fname
!= NULL
);
447 _DIAGASSERT(ll
!= NULL
);
449 db
= dbopen(fname
, O_RDONLY
|O_SHLOCK
, 0, DB_HASH
, NULL
);
455 key
.size
= sizeof(uid
);
457 if ((db
->get
)(db
, &key
, &data
, 0) != 0)
460 if (data
.size
!= sizeof(*ll
)) {
466 if ((ll
= malloc(sizeof(*ll
))) == NULL
)
469 (void)memcpy(ll
, data
.data
, sizeof(*ll
));
479 updlastlogx(const char *fname
, uid_t uid
, struct lastlogx
*ll
)
485 _DIAGASSERT(fname
!= NULL
);
486 _DIAGASSERT(ll
!= NULL
);
488 db
= dbopen(fname
, O_RDWR
|O_CREAT
|O_EXLOCK
, 0644, DB_HASH
, NULL
);
494 key
.size
= sizeof(uid
);
496 data
.size
= sizeof(*ll
);
497 if ((db
->put
)(db
, &key
, &data
, 0) != 0)