Browse Source

Switch login(3) from lseek+read/write to pread/pwrite and only do the pread()

if the data is needed.  Use O_CLOEXEC on the internal fd as MT paranoia.
Fix cast in offset calculation; delete register keyword;
prefer memset() over bzero()
ok millert@
OPENBSD_5_9
guenther 9 years ago
parent
commit
b34242015e
2 changed files with 15 additions and 14 deletions
  1. +11
    -10
      src/lib/libutil/login.c
  2. +4
    -4
      src/lib/libutil/logout.c

+ 11
- 10
src/lib/libutil/login.c View File

@ -1,4 +1,4 @@
/* $OpenBSD: login.c,v 1.10 2005/08/02 21:46:23 espie Exp $ */
/* $OpenBSD: login.c,v 1.11 2015/12/28 20:11:36 guenther Exp $ */
/*
* Copyright (c) 1988, 1993
* The Regents of the University of California. All rights reserved.
@ -43,28 +43,29 @@ void
login(struct utmp *utp)
{
struct utmp old_ut;
register int fd;
int tty;
int fd, tty;
off_t pos;
tty = ttyslot();
if (tty > 0 && (fd = open(_PATH_UTMP, O_RDWR|O_CREAT, 0644)) >= 0) {
(void)lseek(fd, (off_t)(tty * sizeof(struct utmp)), SEEK_SET);
if (tty > 0 && (fd = open(_PATH_UTMP, O_RDWR|O_CREAT|O_CLOEXEC, 0644))
>= 0) {
/*
* Prevent luser from zero'ing out ut_host.
* If the new ut_line is empty but the old one is not
* and ut_line and ut_name match, preserve the old ut_line.
*/
if (read(fd, &old_ut, sizeof(struct utmp)) ==
sizeof(struct utmp) && utp->ut_host[0] == '\0' &&
pos = (off_t)tty * sizeof(struct utmp);
if (utp->ut_host[0] == '\0' &&
pread(fd, &old_ut, sizeof(struct utmp), pos) ==
sizeof(struct utmp) &&
old_ut.ut_host[0] != '\0' &&
strncmp(old_ut.ut_line, utp->ut_line, UT_LINESIZE) == 0 &&
strncmp(old_ut.ut_name, utp->ut_name, UT_NAMESIZE) == 0)
(void)memcpy(utp->ut_host, old_ut.ut_host, UT_HOSTSIZE);
(void)lseek(fd, (off_t)(tty * sizeof(struct utmp)), SEEK_SET);
(void)write(fd, utp, sizeof(struct utmp));
(void)pwrite(fd, utp, sizeof(struct utmp), pos);
(void)close(fd);
}
if ((fd = open(_PATH_WTMP, O_WRONLY|O_APPEND, 0)) >= 0) {
if ((fd = open(_PATH_WTMP, O_WRONLY|O_APPEND|O_CLOEXEC)) >= 0) {
(void)write(fd, utp, sizeof(struct utmp));
(void)close(fd);
}


+ 4
- 4
src/lib/libutil/logout.c View File

@ -1,4 +1,4 @@
/* $OpenBSD: logout.c,v 1.8 2005/08/02 21:46:23 espie Exp $ */
/* $OpenBSD: logout.c,v 1.9 2015/12/28 20:11:36 guenther Exp $ */
/*
* Copyright (c) 1988, 1993
* The Regents of the University of California. All rights reserved.
@ -47,14 +47,14 @@ logout(const char *line)
int fd, rval;
UTMP ut;
if ((fd = open(_PATH_UTMP, O_RDWR, 0)) < 0)
if ((fd = open(_PATH_UTMP, O_RDWR|O_CLOEXEC)) < 0)
return(0);
rval = 0;
while (read(fd, &ut, sizeof(UTMP)) == sizeof(UTMP)) {
if (!ut.ut_name[0] || strncmp(ut.ut_line, line, UT_LINESIZE))
continue;
bzero(ut.ut_name, UT_NAMESIZE);
bzero(ut.ut_host, UT_HOSTSIZE);
memset(ut.ut_name, 0, UT_NAMESIZE);
memset(ut.ut_host, 0, UT_HOSTSIZE);
(void)time(&ut.ut_time);
(void)lseek(fd, -(off_t)sizeof(UTMP), SEEK_CUR);
(void)write(fd, &ut, sizeof(UTMP));


Loading…
Cancel
Save