- (djm) Fixes to lastlog code for Irix

- (djm) Use atomicio in loginrec
This commit is contained in:
Damien Miller 2000-06-28 00:50:50 +10:00
parent dd47aa21fa
commit 53c5d467c3
2 changed files with 28 additions and 32 deletions

View File

@ -1,3 +1,7 @@
20000628
- (djm) Fixes to lastlog code for Irix
- (djm) Use atomicio in loginrec
20000627 20000627
- (djm) Fixes to login code - not setting li->uid, cleanups - (djm) Fixes to login code - not setting li->uid, cleanups
- (djm) Formatting - (djm) Formatting

View File

@ -170,7 +170,7 @@
#include "xmalloc.h" #include "xmalloc.h"
#include "loginrec.h" #include "loginrec.h"
RCSID("$Id: loginrec.c,v 1.11 2000/06/27 01:18:27 djm Exp $"); RCSID("$Id: loginrec.c,v 1.12 2000/06/27 14:50:50 djm Exp $");
/** /**
** prototypes for helper functions in this file ** prototypes for helper functions in this file
@ -281,17 +281,16 @@ login_get_lastlog_time(const int uid)
struct logininfo * struct logininfo *
login_get_lastlog(struct logininfo *li, const int uid) login_get_lastlog(struct logininfo *li, const int uid)
{ {
#ifndef USE_LASTLOG
struct passwd *pw; struct passwd *pw;
#endif
memset(li, '\0', sizeof(struct logininfo)); memset(li, '\0', sizeof(struct logininfo));
li->uid = uid; li->uid = uid;
#ifndef USE_LASTLOG /*
/* If we don't have a 'real' lastlog, we need the username to * If we don't have a 'real' lastlog, we need the username to
* reliably search wtmp(x) for the last login (see * reliably search wtmp(x) for the last login (see
* wtmp_get_entry().) */ * wtmp_get_entry().)
*/
pw = getpwuid(uid); pw = getpwuid(uid);
if (pw == NULL) if (pw == NULL)
fatal("login_get_lastlog: Cannot find account for uid %i", uid); fatal("login_get_lastlog: Cannot find account for uid %i", uid);
@ -299,7 +298,6 @@ login_get_lastlog(struct logininfo *li, const int uid)
/* No MIN_SIZEOF here - we absolutely *must not* truncate the /* No MIN_SIZEOF here - we absolutely *must not* truncate the
* username */ * username */
strlcpy(li->username, pw->pw_name, sizeof(li->username)); strlcpy(li->username, pw->pw_name, sizeof(li->username));
#endif
if (getlast_entry(li)) if (getlast_entry(li))
return li; return li;
@ -452,10 +450,7 @@ int
getlast_entry(struct logininfo *li) getlast_entry(struct logininfo *li)
{ {
#ifdef USE_LASTLOG #ifdef USE_LASTLOG
if (lastlog_get_entry(li)) return(lastlog_get_entry(li));
return 1;
else
return 0;
#else /* !USE_LASTLOG */ #else /* !USE_LASTLOG */
#ifdef DISABLE_LASTLOG #ifdef DISABLE_LASTLOG
@ -738,23 +733,24 @@ utmp_write_direct(struct logininfo *li, struct utmp *ut)
* If the new ut_line is empty but the old one is not * 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. * and ut_line and ut_name match, preserve the old ut_line.
*/ */
if ( read(fd, &old_ut, sizeof(struct utmp)) == sizeof(struct utmp) if (atomicio(read, fd, &old_ut, sizeof(old_ut)) == sizeof(old_ut) &&
&& ut->ut_host[0] == '\0' (ut->ut_host[0] == '\0') && (old_ut.ut_host[0] != '\0') &&
&& old_ut.ut_host[0] != '\0' (strncmp(old_ut.ut_line, ut->ut_line, sizeof(ut->ut_line)) == 0) &&
&& strncmp(old_ut.ut_line, ut->ut_line, sizeof(ut->ut_line)) == 0 (strncmp(old_ut.ut_name, ut->ut_name, sizeof(ut->ut_name)) == 0)) {
&& strncmp(old_ut.ut_name, ut->ut_name, sizeof(ut->ut_name)) == 0 )
(void)memcpy(ut->ut_host, old_ut.ut_host, sizeof(ut->ut_host)); (void)memcpy(ut->ut_host, old_ut.ut_host, sizeof(ut->ut_host));
}
(void)lseek(fd, (off_t)(tty * sizeof(struct utmp)), SEEK_SET); (void)lseek(fd, (off_t)(tty * sizeof(struct utmp)), SEEK_SET);
if (write(fd, ut, sizeof(struct utmp))==-1) if (atomicio(write, fd, ut, sizeof(ut)) != sizeof(ut))
log("utmp_write_direct: error writing %s: %s", log("utmp_write_direct: error writing %s: %s",
UTMP_FILE, strerror(errno)); UTMP_FILE, strerror(errno));
(void)close(fd); (void)close(fd);
return 1; return 1;
} else } else {
return 0; return 0;
} }
}
# endif /* UTMP_USE_LIBRARY */ # endif /* UTMP_USE_LIBRARY */
static int static int
@ -936,8 +932,7 @@ wtmp_write(struct logininfo *li, struct utmp *ut)
return 0; return 0;
} }
if (fstat(fd, &buf) == 0) if (fstat(fd, &buf) == 0)
if (write(fd, (char *)ut, sizeof(struct utmp)) != if (atomicio(write, fd, ut, sizeof(*ut)) != sizeof(*ut)) {
sizeof(struct utmp)) {
ftruncate(fd, buf.st_size); ftruncate(fd, buf.st_size);
log("wtmp_write: problem writing %s: %s", log("wtmp_write: problem writing %s: %s",
WTMP_FILE, strerror(errno)); WTMP_FILE, strerror(errno));
@ -1044,7 +1039,7 @@ wtmp_get_entry(struct logininfo *li)
} }
while (!found) { while (!found) {
if (read(fd, &ut, sizeof(ut)) != sizeof(ut)) { if (atomicio(read, fd, &ut, sizeof(ut)) != sizeof(ut)) {
log("wtmp_get_entry: read of %s failed: %s", log("wtmp_get_entry: read of %s failed: %s",
WTMP_FILE, strerror(errno)); WTMP_FILE, strerror(errno));
close (fd); close (fd);
@ -1104,8 +1099,7 @@ wtmpx_write(struct logininfo *li, struct utmpx *utx)
} }
if (fstat(fd, &buf) == 0) if (fstat(fd, &buf) == 0)
if (write(fd, (char *)utx, sizeof(struct utmpx)) != if (atomicio(write, fd, utx, sizeof(*utx)) != sizeof(*utx)) {
sizeof(struct utmpx)) {
ftruncate(fd, buf.st_size); ftruncate(fd, buf.st_size);
log("wtmpx_write: problem writing %s: %s", log("wtmpx_write: problem writing %s: %s",
WTMPX_FILE, strerror(errno)); WTMPX_FILE, strerror(errno));
@ -1201,7 +1195,7 @@ wtmpx_get_entry(struct logininfo *li)
} }
while (!found) { while (!found) {
if (read(fd, &utx, sizeof(utx)) != sizeof(utx)) { if (atomicio(read, fd, &utx, sizeof(utx)) != sizeof(utx)) {
log("wtmpx_get_entry: read of %s failed: %s", log("wtmpx_get_entry: read of %s failed: %s",
WTMPX_FILE, strerror(errno)); WTMPX_FILE, strerror(errno));
close (fd); close (fd);
@ -1360,7 +1354,7 @@ lastlog_openseek(struct logininfo *li, int *fd, int filemode)
*fd = open(lastlog_file, filemode); *fd = open(lastlog_file, filemode);
if ( *fd < 0) { if ( *fd < 0) {
log("lastlog_openseek: Couldn't open %s: %s", debug("lastlog_openseek: Couldn't open %s: %s",
lastlog_file, strerror(errno)); lastlog_file, strerror(errno));
return 0; return 0;
} }
@ -1386,9 +1380,8 @@ lastlog_perform_login(struct logininfo *li)
lastlog_construct(li, &last); lastlog_construct(li, &last);
/* write the entry */ /* write the entry */
if (lastlog_openseek(li, &fd, O_RDWR)) { if (lastlog_openseek(li, &fd, O_RDWR|O_CREAT)) {
if (write(fd, &last, sizeof(struct lastlog)) != if (atomicio(write, fd, &last, sizeof(last)) != sizeof(last)) {
sizeof(struct lastlog)) {
log("lastlog_write_filemode: Error writing to %s: %s", log("lastlog_write_filemode: Error writing to %s: %s",
LASTLOG_FILE, strerror(errno)); LASTLOG_FILE, strerror(errno));
return 0; return 0;
@ -1427,9 +1420,8 @@ lastlog_get_entry(struct logininfo *li)
int fd; int fd;
if (lastlog_openseek(li, &fd, O_RDONLY)) { if (lastlog_openseek(li, &fd, O_RDONLY)) {
if ( read(fd, &last, sizeof(struct lastlog)) != if (atomicio(read, fd, &last, sizeof(last)) != sizeof(last)) {
sizeof(struct lastlog) ) { log("lastlog_get_entry: Error reading from %s: %s",
log("lastlog_write_filemode: Error reading from %s: %s",
LASTLOG_FILE, strerror(errno)); LASTLOG_FILE, strerror(errno));
return 0; return 0;
} else { } else {