]> andersk Git - openssh.git/blobdiff - loginrec.c
- (tim) [regress/README.regress] Document ssh-rand-helper issue. ok bal
[openssh.git] / loginrec.c
index 844e9668f958f9d97f41f2fb0459dd1aa5d47e91..d975443f000c33960c856233b0eaca29b94e935c 100644 (file)
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- *    must display the following acknowledgement:
- *      This product includes software developed by Markus Friedl.
- * 4. The name of the author may not be used to endorse or promote products
- *    derived from this software without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -30,7 +25,7 @@
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-/** 
+/**
  ** loginrec.c:  platform-independent login recording and lastlog retrieval
  **/
 
@@ -63,7 +58,7 @@
   requires very thorough testing so we do not corrupt local auditing
   information. These files and their access methods are very system
   specific indeed.
-  
+
   For utmpx systems, the corresponding library functions are
   setutxent() etc. To the author's knowledge, all utmpx systems have
   these library functions and so no direct write is attempted. If such
 
   Calling the new code
   --------------------
-  
+
   In OpenSSH all login recording and retrieval is performed in
   login.c. Here you'll find working examples. Also, in the logintest.c
   program there are more examples.
 
   Internal handler calling method
   -------------------------------
-  
+
   When a call is made to login_login() or login_logout(), both
   routines set a struct logininfo flag defining which action (log in,
   or log out) is to be taken. They both then call login_write(), which
   symbols for the platform.
 
   Use logintest to check which symbols are defined before modifying
-  configure.in and loginrec.c. (You have to build logintest yourself
+  configure.ac and loginrec.c. (You have to build logintest yourself
   with 'make logintest' as it's not built by default.)
 
   Otherwise, patches to the specific method(s) are very helpful!
-  
+
 */
 
 /**
  ** TODO:
- **   homegrown ttyslot()q
+ **   homegrown ttyslot()
  **   test, test, test
  **
  ** Platform status:
  ** ----------------
  **
  ** Known good:
- **   Linux (Redhat 6.2, need more variants)
+ **   Linux (Redhat 6.2, Debian)
+ **   Solaris
  **   HP-UX 10.20 (gcc only)
  **   IRIX
+ **   NeXT - M68k/HPPA/Sparc (4.2/3.3)
  **
  ** Testing required: Please send reports!
- **   Solaris
  **   NetBSD
  **   HP-UX 11
  **   AIX
  **
  ** Platforms with known problems:
- **   NeXT
+ **   Some variants of Slackware Linux
  **
  **/
 
 #include "includes.h"
 
-#if HAVE_UTMP_H
-# include <utmp.h>
-#endif
-#if HAVE_UTMPX_H
-# include <utmpx.h>
-#endif
-#if HAVE_LASTLOG_H
-# include <lastlog.h>
-#endif
-
 #include "ssh.h"
 #include "xmalloc.h"
 #include "loginrec.h"
+#include "log.h"
+#include "atomicio.h"
 
 RCSID("$Id$");
 
+#ifdef HAVE_UTIL_H
+#  include <util.h>
+#endif
+
+#ifdef HAVE_LIBUTIL_H
+#   include <libutil.h>
+#endif
+
 /**
  ** prototypes for helper functions in this file
  **/
@@ -198,22 +194,15 @@ int lastlog_get_entry(struct logininfo *li);
 int wtmp_get_entry(struct logininfo *li);
 int wtmpx_get_entry(struct logininfo *li);
 
-
-#ifdef MIN
-# undef MIN
-# define MIN(a,b) ( (a)<(b) ? (a) : (b) )
-#endif
-
 /* pick the shortest string */
 #define MIN_SIZEOF(s1,s2) ( sizeof(s1) < sizeof(s2) ? sizeof(s1) : sizeof(s2) )
 
-
 /**
  ** platform-independent login functions
  **/
 
 /* login_login(struct logininfo *)     -Record a login
- * 
+ *
  * Call with a pointer to a struct logininfo initialised with
  * login_init_entry() or login_alloc_entry()
  *
@@ -244,7 +233,6 @@ login_logout(struct logininfo *li)
        return login_write(li);
 }
 
-
 /* login_get_lastlog_time(int)           - Retrieve the last login time
  *
  * Retrieve the last login time for the given uid. Will try to use the
@@ -289,32 +277,34 @@ login_get_lastlog_time(const int uid)
 struct logininfo *
 login_get_lastlog(struct logininfo *li, const int uid)
 {
-#ifndef USE_LASTLOG
        struct passwd *pw;
-#endif
 
-       memset(li, '\0', sizeof(struct logininfo));
+       memset(li, '\0', sizeof(*li));
        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
-        * wtmp_get_entry().) */
+        * wtmp_get_entry().)
+        */
        pw = getpwuid(uid);
+       if (pw == NULL)
+               fatal("login_get_lastlog: Cannot find account for uid %i", uid);
+
        /* No MIN_SIZEOF here - we absolutely *must not* truncate the
-         * username */
-       strlcpy(li->username, pw->pw_name, li->username);
-#endif
+        * username */
+       strlcpy(li->username, pw->pw_name, sizeof(li->username));
+
        if (getlast_entry(li))
                return li;
        else
-               return 0;
+               return NULL;
 }
 
 
 /* login_alloc_entry(int, char*, char*, char*)    - Allocate and initialise
- *                                                  a logininfo structure 
- * 
+ *                                                  a logininfo structure
+ *
  * This function creates a new struct logininfo, a data structure
  * meant to carry the information required to portably record login info.
  *
@@ -327,7 +317,7 @@ logininfo *login_alloc_entry(int pid, const char *username,
 {
        struct logininfo *newli;
 
-       newli = (struct logininfo *) xmalloc (sizeof(struct logininfo));
+       newli = (struct logininfo *) xmalloc (sizeof(*newli));
        (void)login_init_entry(newli, pid, username, hostname, line);
        return newli;
 }
@@ -343,28 +333,37 @@ login_free_entry(struct logininfo *li)
 
 /* login_init_entry(struct logininfo *, int, char*, char*, char*)
  *                                        - initialise a struct logininfo
- * 
+ *
  * Populates a new struct logininfo, a data structure meant to carry
  * the information required to portably record login info.
  *
  * Returns: 1
  */
 int
-login_init_entry(struct logininfo *li, int pid, const char *username, 
+login_init_entry(struct logininfo *li, int pid, const char *username,
                 const char *hostname, const char *line)
 {
-       /* zero the structure */
-       memset(li, 0, sizeof(struct logininfo));
-  
+       struct passwd *pw;
+
+       memset(li, 0, sizeof(*li));
+
        li->pid = pid;
+
        /* set the line information */
        if (line)
                line_fullname(li->line, line, sizeof(li->line));
 
-       if (username)
+       if (username) {
                strlcpy(li->username, username, sizeof(li->username));
+               pw = getpwnam(li->username);
+               if (pw == NULL)
+                       fatal("login_init_entry: Cannot find user \"%s\"", li->username);
+               li->uid = pw->pw_uid;
+       }
+
        if (hostname)
                strlcpy(li->hostname, hostname, sizeof(li->hostname));
+
        return 1;
 }
 
@@ -377,19 +376,14 @@ login_init_entry(struct logininfo *li, int pid, const char *username,
 void
 login_set_current_time(struct logininfo *li)
 {
-#ifdef HAVE_SYS_TIME_H
        struct timeval tv;
 
        gettimeofday(&tv, NULL);
-       li->tv_sec = tv.tv_sec ; li->tv_usec = tv.tv_usec;
-#else
-       time_t tm = time(0);
 
-       li->tv_sec = tm; li->tv_usec = 0;
-#endif
+       li->tv_sec = tv.tv_sec;
+       li->tv_usec = tv.tv_usec;
 }
 
-
 /* copy a sockaddr_* into our logininfo */
 void
 login_set_addr(struct logininfo *li, const struct sockaddr *sa,
@@ -409,14 +403,16 @@ login_set_addr(struct logininfo *li, const struct sockaddr *sa,
  ** login_write: Call low-level recording functions based on autoconf
  ** results
  **/
-
 int
 login_write (struct logininfo *li)
 {
+#ifndef HAVE_CYGWIN
        if ((int)geteuid() != 0) {
-         log("Attempt to write login records by non-root user (aborting)");
+         logit("Attempt to write login records by non-root user (aborting)");
          return 1;
        }
+#endif
+
        /* set the timestamp */
        login_set_current_time(li);
 #ifdef USE_LOGIN
@@ -442,6 +438,27 @@ login_write (struct logininfo *li)
        return 0;
 }
 
+#ifdef LOGIN_NEEDS_UTMPX
+int
+login_utmp_only(struct logininfo *li)
+{
+       li->type = LTYPE_LOGIN;
+       login_set_current_time(li);
+# ifdef USE_UTMP
+       utmp_write_entry(li);
+# endif
+# ifdef USE_WTMP
+       wtmp_write_entry(li);
+# endif
+# ifdef USE_UTMPX
+       utmpx_write_entry(li);
+# endif
+# ifdef USE_WTMPX
+       wtmpx_write_entry(li);
+# endif
+       return 0;
+}
+#endif
 
 /**
  ** getlast_entry: Call low-level functions to retrieve the last login
@@ -453,49 +470,30 @@ int
 getlast_entry(struct logininfo *li)
 {
 #ifdef USE_LASTLOG
-       if (lastlog_get_entry(li))
-               return 1;
-       else
-               return 0;
-#else
-       /* !USE_LASTLOG */
+       return(lastlog_get_entry(li));
+#else /* !USE_LASTLOG */
 
-#  ifdef DISABLE_LASTLOG
-       /* On some systems we shouldn't even try to obtain last login 
+#ifdef DISABLE_LASTLOG
+       /* On some systems we shouldn't even try to obtain last login
         * time, e.g. AIX */
        return 0;
-
-#  else
+# else /* DISABLE_LASTLOG */
        /* Try to retrieve the last login time from wtmp */
-#    if defined(USE_WTMP) && (defined(HAVE_TIME_IN_UTMP) || defined(HAVE_TV_IN_UTMP))
+#  if defined(USE_WTMP) && (defined(HAVE_TIME_IN_UTMP) || defined(HAVE_TV_IN_UTMP))
        /* retrieve last login time from utmp */
-       if (wtmp_get_entry(li))
-               return 1;
-       else
-               return 0;
-#    else
-
+       return (wtmp_get_entry(li));
+#  else /* defined(USE_WTMP) && (defined(HAVE_TIME_IN_UTMP) || defined(HAVE_TV_IN_UTMP)) */
        /* If wtmp isn't available, try wtmpx */
-
-#      if defined(USE_WTMPX) && (defined(HAVE_TIME_IN_UTMPX) || defined(HAVE_TV_IN_UTMPX))
+#   if defined(USE_WTMPX) && (defined(HAVE_TIME_IN_UTMPX) || defined(HAVE_TV_IN_UTMPX))
        /* retrieve last login time from utmpx */
-       if (wtmpx_get_entry(li))
-               return 1;
-       else
-               return 0;
-#      else
-
+       return (wtmpx_get_entry(li));
+#   else
        /* Give up: No means of retrieving last login time */
        return 0;
-#      endif
-       /* USE_WTMPX && (HAVE_TIME_IN_UTMPX || HAVE_TV_IN_UTMPX) */
-
-#    endif
-     /* USE_WTMP && (HAVE_TIME_IN_UTMP || HAVE_TV_IN_UTMP) */
-#  endif
-   /* DISABLE_LASTLOG */ 
-#endif
-/* USE_LASTLOG */
+#   endif /* USE_WTMPX && (HAVE_TIME_IN_UTMPX || HAVE_TV_IN_UTMPX) */
+#  endif /* USE_WTMP && (HAVE_TIME_IN_UTMP || HAVE_TV_IN_UTMP) */
+# endif /* DISABLE_LASTLOG */
+#endif /* USE_LASTLOG */
 }
 
 
@@ -523,29 +521,27 @@ char *
 line_fullname(char *dst, const char *src, int dstsize)
 {
        memset(dst, '\0', dstsize);
-       if ((strncmp(src, "/dev/", 5) == 0) || (dstsize < (strlen(src) + 5)))
+       if ((strncmp(src, "/dev/", 5) == 0) || (dstsize < (strlen(src) + 5))) {
                strlcpy(dst, src, dstsize);
-       else {
+       else {
                strlcpy(dst, "/dev/", dstsize);
                strlcat(dst, src, dstsize);
        }
        return dst;
 }
 
-
 /* line_stripname(): strip the leading '/dev' if it exists, return dst */
 char *
 line_stripname(char *dst, const char *src, int dstsize)
 {
        memset(dst, '\0', dstsize);
        if (strncmp(src, "/dev/", 5) == 0)
-               strlcpy(dst, &src[5], dstsize);
+               strlcpy(dst, src + 5, dstsize);
        else
                strlcpy(dst, src, dstsize);
        return dst;
 }
 
-  
 /* line_abbrevname(): Return the abbreviated (usually four-character)
  * form of the line (Just use the last <dstsize> characters of the
  * full name.)
@@ -553,14 +549,34 @@ line_stripname(char *dst, const char *src, int dstsize)
  * NOTE: use strncpy because we do NOT necessarily want zero
  * termination */
 char *
-line_abbrevname(char *dst, const char *src, int dstsize) {
+line_abbrevname(char *dst, const char *src, int dstsize)
+{
+       size_t len;
+
        memset(dst, '\0', dstsize);
-       src += (strlen(src) - dstsize);
-       strncpy(dst, src, dstsize); /* note: _don't_ change this to strlcpy */
+
+       /* Always skip prefix if present */
+       if (strncmp(src, "/dev/", 5) == 0)
+               src += 5;
+
+#ifdef WITH_ABBREV_NO_TTY
+       if (strncmp(src, "tty", 3) == 0)
+               src += 3;
+#endif
+
+       len = strlen(src);
+
+       if (len > 0) {
+               if (((int)len - dstsize) > 0)
+                       src +=  ((int)len - dstsize);
+
+               /* note: _don't_ change this to strlcpy */
+               strncpy(dst, src, (size_t)dstsize);
+       }
+
        return dst;
 }
 
-
 /**
  ** utmp utility functions
  **
@@ -574,70 +590,90 @@ line_abbrevname(char *dst, const char *src, int dstsize) {
 void
 set_utmp_time(struct logininfo *li, struct utmp *ut)
 {
-#ifdef HAVE_TV_IN_UTMP
+# ifdef HAVE_TV_IN_UTMP
        ut->ut_tv.tv_sec = li->tv_sec;
        ut->ut_tv.tv_usec = li->tv_usec;
-#else
+# else
 #  ifdef HAVE_TIME_IN_UTMP
        ut->ut_time = li->tv_sec;
 #  endif
-#endif
+# endif
 }
 
-
 void
 construct_utmp(struct logininfo *li,
                    struct utmp *ut)
 {
-       memset(ut, '\0', sizeof(struct utmp));
+# ifdef HAVE_ADDR_V6_IN_UTMP
+       struct sockaddr_in6 *sa6;
+#  endif
+       memset(ut, '\0', sizeof(*ut));
 
        /* First fill out fields used for both logins and logouts */
 
-#ifdef HAVE_ID_IN_UTMP
+# ifdef HAVE_ID_IN_UTMP
        line_abbrevname(ut->ut_id, li->line, sizeof(ut->ut_id));
-#endif
+# endif
 
-#ifdef HAVE_TYPE_IN_UTMP
+# ifdef HAVE_TYPE_IN_UTMP
        /* This is done here to keep utmp constants out of struct logininfo */
        switch (li->type) {
        case LTYPE_LOGIN:
                ut->ut_type = USER_PROCESS;
+#ifdef _UNICOS
+               cray_set_tmpdir(ut);
+#endif
                break;
        case LTYPE_LOGOUT:
                ut->ut_type = DEAD_PROCESS;
+#ifdef _UNICOS
+               cray_retain_utmp(ut, li->pid);
+#endif
                break;
        }
-#endif
+# endif
        set_utmp_time(li, ut);
 
        line_stripname(ut->ut_line, li->line, sizeof(ut->ut_line));
-#ifdef HAVE_PID_IN_UTMP
+
+# ifdef HAVE_PID_IN_UTMP
        ut->ut_pid = li->pid;
-#endif
+# endif
 
        /* If we're logging out, leave all other fields blank */
        if (li->type == LTYPE_LOGOUT)
          return;
 
-       /* These fields are only used when logging in, and are blank
-        *  for logouts. */
+       /*
+        * These fields are only used when logging in, and are blank
+        * for logouts.
+        */
 
        /* Use strncpy because we don't necessarily want null termination */
-       strncpy(ut->ut_user, li->username, MIN_SIZEOF(ut->ut_user, li->username));
-#ifdef HAVE_HOST_IN_UTMP
+       strncpy(ut->ut_name, li->username, MIN_SIZEOF(ut->ut_name, li->username));
+# ifdef HAVE_HOST_IN_UTMP
        strncpy(ut->ut_host, li->hostname, MIN_SIZEOF(ut->ut_host, li->hostname));
-#endif
-#ifdef HAVE_ADDR_IN_UTMP
+# endif
+# ifdef HAVE_ADDR_IN_UTMP
        /* this is just a 32-bit IP address */
        if (li->hostaddr.sa.sa_family == AF_INET)
                ut->ut_addr = li->hostaddr.sa_in.sin_addr.s_addr;
-#endif 
+# endif
+# ifdef HAVE_ADDR_V6_IN_UTMP
+       /* this is just a 128-bit IPv6 address */
+       if (li->hostaddr.sa.sa_family == AF_INET6) {
+               sa6 = ((struct sockaddr_in6 *)&li->hostaddr.sa);
+               memcpy(ut->ut_addr_v6, sa6->sin6_addr.s6_addr, 16);
+               if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) {
+                       ut->ut_addr_v6[0] = ut->ut_addr_v6[3];
+                       ut->ut_addr_v6[1] = 0;
+                       ut->ut_addr_v6[2] = 0;
+                       ut->ut_addr_v6[3] = 0;
+               }
+       }
+# endif
 }
-
-#endif
-/* USE_UTMP || USE_WTMP || USE_LOGIN */
-
-
+#endif /* USE_UTMP || USE_WTMP || USE_LOGIN */
 
 /**
  ** utmpx utility functions
@@ -647,27 +683,30 @@ construct_utmp(struct logininfo *li,
  **/
 
 #if defined(USE_UTMPX) || defined (USE_WTMPX)
-
 /* build the utmpx structure */
 void
 set_utmpx_time(struct logininfo *li, struct utmpx *utx)
 {
-#ifdef HAVE_TV_IN_UTMPX
+# ifdef HAVE_TV_IN_UTMPX
        utx->ut_tv.tv_sec = li->tv_sec;
        utx->ut_tv.tv_usec = li->tv_usec;
-#else
+# else /* HAVE_TV_IN_UTMPX */
 #  ifdef HAVE_TIME_IN_UTMPX
        utx->ut_time = li->tv_sec;
-#  endif
-#endif
+#  endif /* HAVE_TIME_IN_UTMPX */
+# endif /* HAVE_TV_IN_UTMPX */
 }
 
-
 void
 construct_utmpx(struct logininfo *li, struct utmpx *utx)
 {
-       memset(utx, '\0', sizeof(struct utmpx));
+# ifdef HAVE_ADDR_V6_IN_UTMP
+       struct sockaddr_in6 *sa6;
+#  endif
+       memset(utx, '\0', sizeof(*utx));
+# ifdef HAVE_ID_IN_UTMPX
        line_abbrevname(utx->ut_id, li->line, sizeof(utx->ut_id));
+# endif
 
        /* this is done here to keep utmp constants out of loginrec.h */
        switch (li->type) {
@@ -681,62 +720,73 @@ construct_utmpx(struct logininfo *li, struct utmpx *utx)
        line_stripname(utx->ut_line, li->line, sizeof(utx->ut_line));
        set_utmpx_time(li, utx);
        utx->ut_pid = li->pid;
+       /* strncpy(): Don't necessarily want null termination */
+       strncpy(utx->ut_name, li->username, MIN_SIZEOF(utx->ut_name, li->username));
 
        if (li->type == LTYPE_LOGOUT)
                return;
 
-       /* These fields are only used when logging in, and are blank
-        *  for logouts. */
+       /*
+        * These fields are only used when logging in, and are blank
+        * for logouts.
+        */
 
-       /* strncpy(): Don't necessarily want null termination */
-       strncpy(utx->ut_name, li->username, MIN_SIZEOF(utx->ut_name, li->username));
-#ifdef HAVE_HOST_IN_UTMPX
+# ifdef HAVE_HOST_IN_UTMPX
        strncpy(utx->ut_host, li->hostname, MIN_SIZEOF(utx->ut_host, li->hostname));
-#endif
-#ifdef HAVE_ADDR_IN_UTMPX
-       /* FIXME: (ATL) not supported yet */
-#endif
-#ifdef HAVE_SYSLEN_IN_UTMPX
+# endif
+# ifdef HAVE_ADDR_IN_UTMPX
+       /* this is just a 32-bit IP address */
+       if (li->hostaddr.sa.sa_family == AF_INET)
+               utx->ut_addr = li->hostaddr.sa_in.sin_addr.s_addr;
+# endif
+# ifdef HAVE_ADDR_V6_IN_UTMP
+       /* this is just a 128-bit IPv6 address */
+       if (li->hostaddr.sa.sa_family == AF_INET6) {
+               sa6 = ((struct sockaddr_in6 *)&li->hostaddr.sa);
+               memcpy(ut->ut_addr_v6, sa6->sin6_addr.s6_addr, 16);
+               if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) {
+                       ut->ut_addr_v6[0] = ut->ut_addr_v6[3];
+                       ut->ut_addr_v6[1] = 0;
+                       ut->ut_addr_v6[2] = 0;
+                       ut->ut_addr_v6[3] = 0;
+               }
+       }
+# endif
+# ifdef HAVE_SYSLEN_IN_UTMPX
        /* ut_syslen is the length of the utx_host string */
        utx->ut_syslen = MIN(strlen(li->hostname), sizeof(utx->ut_host));
-#endif
+# endif
 }
-
-#endif
-/* USE_UTMPX || USE_WTMPX */
-
-
+#endif /* USE_UTMPX || USE_WTMPX */
 
 /**
  ** Low-level utmp functions
  **/
 
 /* FIXME: (ATL) utmp_write_direct needs testing */
-
 #ifdef USE_UTMP
 
 /* if we can, use pututline() etc. */
-#if !defined(DISABLE_PUTUTLINE) && defined(HAVE_SETUTENT) && \
-    defined(HAVE_PUTUTLINE)
+# if !defined(DISABLE_PUTUTLINE) && defined(HAVE_SETUTENT) && \
+       defined(HAVE_PUTUTLINE)
 #  define UTMP_USE_LIBRARY
-#endif
+# endif
 
 
 /* write a utmp entry with the system's help (pututline() and pals) */
-#ifdef UTMP_USE_LIBRARY
+# ifdef UTMP_USE_LIBRARY
 static int
 utmp_write_library(struct logininfo *li, struct utmp *ut)
 {
        setutent();
        pututline(ut);
 
-#ifdef HAVE_ENDUTENT
+#  ifdef HAVE_ENDUTENT
        endutent();
-#endif
+#  endif
        return 1;
 }
-
-#else
+# else /* UTMP_USE_LIBRARY */
 
 /* write a utmp entry direct to the file */
 /* This is a slightly modification of code in OpenBSD's login.c */
@@ -748,8 +798,30 @@ utmp_write_direct(struct logininfo *li, struct utmp *ut)
        int tty;
 
        /* FIXME: (ATL) ttyslot() needs local implementation */
+
+#if defined(HAVE_GETTTYENT)
+       register struct ttyent *ty;
+
+       tty=0;
+
+       setttyent();
+       while ((struct ttyent *)0 != (ty = getttyent())) {
+               tty++;
+               if (!strncmp(ty->ty_name, ut->ut_line, sizeof(ut->ut_line)))
+                       break;
+       }
+       endttyent();
+
+       if((struct ttyent *)0 == ty) {
+               logit("utmp_write_entry: tty not found");
+               return(1);
+       }
+#else /* FIXME */
+
        tty = ttyslot(); /* seems only to work for /dev/ttyp? style names */
 
+#endif /* HAVE_GETTTYENT */
+
        if (tty > 0 && (fd = open(UTMP_FILE, O_RDWR|O_CREAT, 0644)) >= 0) {
                (void)lseek(fd, (off_t)(tty * sizeof(struct utmp)), SEEK_SET);
                /*
@@ -757,25 +829,25 @@ utmp_write_direct(struct logininfo *li, struct utmp *ut)
                 * 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)
-                      && 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_name, ut->ut_name, sizeof(ut->ut_name)) == 0  )
+               if (atomicio(read, fd, &old_ut, sizeof(old_ut)) == sizeof(old_ut) &&
+                       (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_name, ut->ut_name, sizeof(ut->ut_name)) == 0)) {
                        (void)memcpy(ut->ut_host, old_ut.ut_host, sizeof(ut->ut_host));
+               }
 
                (void)lseek(fd, (off_t)(tty * sizeof(struct utmp)), SEEK_SET);
-               if (write(fd, ut, sizeof(struct utmp))==-1)
-                       log("utmp_write_direct: error writing %s: %s",
+               if (atomicio(vwrite, fd, ut, sizeof(*ut)) != sizeof(*ut))
+                       logit("utmp_write_direct: error writing %s: %s",
                            UTMP_FILE, strerror(errno));
-      
+
                (void)close(fd);
                return 1;
-       } else
+       } else {
                return 0;
+       }
 }
-#endif /* UTMP_USE_LIBRARY */
-
+# endif /* UTMP_USE_LIBRARY */
 
 static int
 utmp_perform_login(struct logininfo *li)
@@ -783,17 +855,17 @@ utmp_perform_login(struct logininfo *li)
        struct utmp ut;
 
        construct_utmp(li, &ut);
-#ifdef UTMP_USE_LIBRARY
+# ifdef UTMP_USE_LIBRARY
        if (!utmp_write_library(li, &ut)) {
-               log("utmp_perform_login: utmp_write_library() failed");
+               logit("utmp_perform_login: utmp_write_library() failed");
                return 0;
        }
-#else
+# else
        if (!utmp_write_direct(li, &ut)) {
-               log("utmp_perform_login: utmp_write_direct() failed");
+               logit("utmp_perform_login: utmp_write_direct() failed");
                return 0;
        }
-#endif
+# endif
        return 1;
 }
 
@@ -804,17 +876,17 @@ utmp_perform_logout(struct logininfo *li)
        struct utmp ut;
 
        construct_utmp(li, &ut);
-#ifdef UTMP_USE_LIBRARY
+# ifdef UTMP_USE_LIBRARY
        if (!utmp_write_library(li, &ut)) {
-               log("utmp_perform_logout: utmp_write_library() failed");
+               logit("utmp_perform_logout: utmp_write_library() failed");
                return 0;
        }
-#else
+# else
        if (!utmp_write_direct(li, &ut)) {
-               log("utmp_perform_logout: utmp_write_direct() failed");
+               logit("utmp_perform_logout: utmp_write_direct() failed");
                return 0;
        }
-#endif
+# endif
        return 1;
 }
 
@@ -830,14 +902,11 @@ utmp_write_entry(struct logininfo *li)
                return utmp_perform_logout(li);
 
        default:
-               log("utmp_write_entry: invalid type field");
+               logit("utmp_write_entry: invalid type field");
                return 0;
        }
 }
-
-
-#endif
-/* USE_UTMP */
+#endif /* USE_UTMP */
 
 
 /**
@@ -848,40 +917,36 @@ utmp_write_entry(struct logininfo *li)
 #ifdef USE_UTMPX
 
 /* if we have the wherewithall, use pututxline etc. */
-#if !defined(DISABLE_PUTUTXLINE) && defined(HAVE_SETUTXENT) \
-    && defined(HAVE_PUTUTXLINE)
+# if !defined(DISABLE_PUTUTXLINE) && defined(HAVE_SETUTXENT) && \
+       defined(HAVE_PUTUTXLINE)
 #  define UTMPX_USE_LIBRARY
-#endif
+# endif
 
 
 /* write a utmpx entry with the system's help (pututxline() and pals) */
-#ifdef UTMPX_USE_LIBRARY
+# ifdef UTMPX_USE_LIBRARY
 static int
 utmpx_write_library(struct logininfo *li, struct utmpx *utx)
 {
        setutxent();
        pututxline(utx);
 
-#ifdef HAVE_ENDUTXENT
+#  ifdef HAVE_ENDUTXENT
        endutxent();
-#endif
+#  endif
        return 1;
 }
 
-#else
-/* UTMPX_USE_LIBRARY */
-
+# else /* UTMPX_USE_LIBRARY */
 
 /* write a utmp entry direct to the file */
 static int
 utmpx_write_direct(struct logininfo *li, struct utmpx *utx)
-{  
-       log("utmpx_write_direct: not implemented!");
+{
+       logit("utmpx_write_direct: not implemented!");
        return 0;
 }
-
-#endif
-/* UTMPX_USE_LIBRARY */
+# endif /* UTMPX_USE_LIBRARY */
 
 static int
 utmpx_perform_login(struct logininfo *li)
@@ -889,17 +954,17 @@ utmpx_perform_login(struct logininfo *li)
        struct utmpx utx;
 
        construct_utmpx(li, &utx);
-#ifdef UTMPX_USE_LIBRARY
+# ifdef UTMPX_USE_LIBRARY
        if (!utmpx_write_library(li, &utx)) {
-               log("utmpx_perform_login: utmp_write_library() failed");
+               logit("utmpx_perform_login: utmp_write_library() failed");
                return 0;
        }
-#else
+# else
        if (!utmpx_write_direct(li, &ut)) {
-               log("utmpx_perform_login: utmp_write_direct() failed");
+               logit("utmpx_perform_login: utmp_write_direct() failed");
                return 0;
        }
-#endif
+# endif
        return 1;
 }
 
@@ -909,25 +974,22 @@ utmpx_perform_logout(struct logininfo *li)
 {
        struct utmpx utx;
 
-       memset(&utx, '\0', sizeof(utx));
-       set_utmpx_time(li, &utx);
-       line_stripname(utx.ut_line, li->line, sizeof(utx.ut_line));
-#ifdef HAVE_ID_IN_UTMPX
+       construct_utmpx(li, &utx);
+# ifdef HAVE_ID_IN_UTMPX
        line_abbrevname(utx.ut_id, li->line, sizeof(utx.ut_id));
-#endif
-#ifdef HAVE_TYPE_IN_UTMPX
+# endif
+# ifdef HAVE_TYPE_IN_UTMPX
        utx.ut_type = DEAD_PROCESS;
-#endif
+# endif
 
-#ifdef UTMPX_USE_LIBRARY
+# ifdef UTMPX_USE_LIBRARY
        utmpx_write_library(li, &utx);
-#else
+# else
        utmpx_write_direct(li, &utx);
-#endif
+# endif
        return 1;
 }
 
-
 int
 utmpx_write_entry(struct logininfo *li)
 {
@@ -937,21 +999,18 @@ utmpx_write_entry(struct logininfo *li)
        case LTYPE_LOGOUT:
                return utmpx_perform_logout(li);
        default:
-               log("utmpx_write_entry: invalid type field");
+               logit("utmpx_write_entry: invalid type field");
                return 0;
        }
 }
-
-
-#endif
-/* USE_UTMPX */
+#endif /* USE_UTMPX */
 
 
 /**
  ** Low-level wtmp functions
  **/
 
-#ifdef USE_WTMP 
+#ifdef USE_WTMP
 
 /* write a wtmp entry direct to the end of the file */
 /* This is a slight modification of code in OpenBSD's logwtmp.c */
@@ -962,15 +1021,14 @@ wtmp_write(struct logininfo *li, struct utmp *ut)
        int fd, ret = 1;
 
        if ((fd = open(WTMP_FILE, O_WRONLY|O_APPEND, 0)) < 0) {
-               log("wtmp_write: problem writing %s: %s",
+               logit("wtmp_write: problem writing %s: %s",
                    WTMP_FILE, strerror(errno));
                return 0;
        }
-       if (fstat(fd, &buf) == 0) 
-               if (write(fd, (char *)ut, sizeof(struct utmp)) != 
-                   sizeof(struct utmp)) {
+       if (fstat(fd, &buf) == 0)
+               if (atomicio(vwrite, fd, ut, sizeof(*ut)) != sizeof(*ut)) {
                        ftruncate(fd, buf.st_size);
-                       log("wtmp_write: problem writing %s: %s",
+                       logit("wtmp_write: problem writing %s: %s",
                            WTMP_FILE, strerror(errno));
                        ret = 0;
                }
@@ -978,9 +1036,9 @@ wtmp_write(struct logininfo *li, struct utmp *ut)
        return ret;
 }
 
-
 static int
-wtmp_perform_login(struct logininfo *li){
+wtmp_perform_login(struct logininfo *li)
+{
        struct utmp ut;
 
        construct_utmp(li, &ut);
@@ -1007,14 +1065,14 @@ wtmp_write_entry(struct logininfo *li)
        case LTYPE_LOGOUT:
                return wtmp_perform_logout(li);
        default:
-               log("wtmp_write_entry: invalid type field");
+               logit("wtmp_write_entry: invalid type field");
                return 0;
        }
 }
 
 
 /* Notes on fetching login data from wtmp/wtmpx
- * 
+ *
  * Logouts are usually recorded with (amongst other things) a blank
  * username on a given tty line.  However, some systems (HP-UX is one)
  * leave all fields set, but change the ut_type field to DEAD_PROCESS.
@@ -1029,24 +1087,22 @@ wtmp_write_entry(struct logininfo *li)
  * place and not have ut_type.
  */
 
-
 /* return true if this wtmp entry indicates a login */
 static int
 wtmp_islogin(struct logininfo *li, struct utmp *ut)
 {
-       if ( strncmp(li->username, ut->ut_user,
-                    MIN_SIZEOF(li->username, ut->ut_user)) == 0 ) {
-#ifdef HAVE_TYPE_IN_UTMP
+       if (strncmp(li->username, ut->ut_name,
+               MIN_SIZEOF(li->username, ut->ut_name)) == 0) {
+# ifdef HAVE_TYPE_IN_UTMP
                if (ut->ut_type & USER_PROCESS)
                        return 1;
-#else
+# else
                return 1;
-#endif
+# endif
        }
        return 0;
 }
 
-
 int
 wtmp_get_entry(struct logininfo *li)
 {
@@ -1058,27 +1114,27 @@ wtmp_get_entry(struct logininfo *li)
        li->tv_sec = li->tv_usec = 0;
 
        if ((fd = open(WTMP_FILE, O_RDONLY)) < 0) {
-               log("wtmp_get_entry: problem opening %s: %s",
+               logit("wtmp_get_entry: problem opening %s: %s",
                    WTMP_FILE, strerror(errno));
                return 0;
        }
-       if (fstat(fd, &st) != 0) {
-               log("wtmp_get_entry: couldn't stat %s: %s",
+       if (fstat(fd, &st) != 0) {
+               logit("wtmp_get_entry: couldn't stat %s: %s",
                    WTMP_FILE, strerror(errno));
                close(fd);
                return 0;
        }
 
        /* Seek to the start of the last struct utmp */
-       if (lseek(fd, (off_t)(0-sizeof(struct utmp)), SEEK_END) == -1) {
+       if (lseek(fd, -(off_t)sizeof(struct utmp), SEEK_END) == -1) {
                /* Looks like we've got a fresh wtmp file */
                close(fd);
                return 0;
        }
 
        while (!found) {
-               if (read(fd, &ut, sizeof(ut)) != sizeof(ut)) {
-                       log("wtmp_get_entry: read of %s failed: %s",
+               if (atomicio(read, fd, &ut, sizeof(ut)) != sizeof(ut)) {
+                       logit("wtmp_get_entry: read of %s failed: %s",
                            WTMP_FILE, strerror(errno));
                        close (fd);
                        return 0;
@@ -1087,23 +1143,23 @@ wtmp_get_entry(struct logininfo *li)
                        found = 1;
                        /* We've already checked for a time in struct
                         * utmp, in login_getlast(). */
-#ifdef HAVE_TIME_IN_UTMP
+# ifdef HAVE_TIME_IN_UTMP
                        li->tv_sec = ut.ut_time;
-#else
+# else
 #  if HAVE_TV_IN_UTMP
                        li->tv_sec = ut.ut_tv.tv_sec;
 #  endif
-#endif
+# endif
                        line_fullname(li->line, ut.ut_line,
                                      MIN_SIZEOF(li->line, ut.ut_line));
-#ifdef HAVE_HOST_IN_UTMP
+# ifdef HAVE_HOST_IN_UTMP
                        strlcpy(li->hostname, ut.ut_host,
                                MIN_SIZEOF(li->hostname, ut.ut_host));
-#endif
+# endif
                        continue;
                }
                /* Seek back 2 x struct utmp */
-               if (lseek(fd, (off_t)(0-2*sizeof(struct utmp)), SEEK_CUR) == -1) {
+               if (lseek(fd, -(off_t)(2 * sizeof(struct utmp)), SEEK_CUR) == -1) {
                        /* We've found the start of the file, so quit */
                        close (fd);
                        return 0;
@@ -1114,10 +1170,7 @@ wtmp_get_entry(struct logininfo *li)
        close(fd);
        return 1;
 }
-
-
-#endif
-/* USE_WTMP */
+# endif /* USE_WTMP */
 
 
 /**
@@ -1125,32 +1178,35 @@ wtmp_get_entry(struct logininfo *li)
  **/
 
 #ifdef USE_WTMPX
-
 /* write a wtmpx entry direct to the end of the file */
 /* This is a slight modification of code in OpenBSD's logwtmp.c */
 static int
 wtmpx_write(struct logininfo *li, struct utmpx *utx)
 {
+#ifndef HAVE_UPDWTMPX
        struct stat buf;
        int fd, ret = 1;
 
        if ((fd = open(WTMPX_FILE, O_WRONLY|O_APPEND, 0)) < 0) {
-               log("wtmpx_write: problem opening %s: %s",
+               logit("wtmpx_write: problem opening %s: %s",
                    WTMPX_FILE, strerror(errno));
                return 0;
        }
 
-       if (fstat(fd, &buf) == 0) 
-               if (write(fd, (char *)utx, sizeof(struct utmpx)) != 
-                   sizeof(struct utmpx)) {
+       if (fstat(fd, &buf) == 0)
+               if (atomicio(vwrite, fd, utx, sizeof(*utx)) != sizeof(*utx)) {
                        ftruncate(fd, buf.st_size);
-                       log("wtmpx_write: problem writing %s: %s",
+                       logit("wtmpx_write: problem writing %s: %s",
                            WTMPX_FILE, strerror(errno));
                        ret = 0;
                }
        (void)close(fd);
 
        return ret;
+#else
+       updwtmpx(WTMPX_FILE, utx);
+       return 1;
+#endif
 }
 
 
@@ -1183,7 +1239,7 @@ wtmpx_write_entry(struct logininfo *li)
        case LTYPE_LOGOUT:
                return wtmpx_perform_logout(li);
        default:
-               log("wtmpx_write_entry: invalid type field");
+               logit("wtmpx_write_entry: invalid type field");
                return 0;
        }
 }
@@ -1195,14 +1251,14 @@ wtmpx_write_entry(struct logininfo *li)
 static int
 wtmpx_islogin(struct logininfo *li, struct utmpx *utx)
 {
-       if ( strncmp(li->username, utx->ut_user,
-                    MIN_SIZEOF(li->username, utx->us_user)) == 0 ) {
-#ifdef HAVE_TYPE_IN_UTMPX
+       if ( strncmp(li->username, utx->ut_name,
+               MIN_SIZEOF(li->username, utx->ut_name)) == 0 ) {
+# ifdef HAVE_TYPE_IN_UTMPX
                if (utx->ut_type == USER_PROCESS)
                        return 1;
-#else
+# else
                return 1;
-#endif
+# endif
        }
        return 0;
 }
@@ -1219,27 +1275,27 @@ wtmpx_get_entry(struct logininfo *li)
        li->tv_sec = li->tv_usec = 0;
 
        if ((fd = open(WTMPX_FILE, O_RDONLY)) < 0) {
-               log("wtmpx_get_entry: problem opening %s: %s",
+               logit("wtmpx_get_entry: problem opening %s: %s",
                    WTMPX_FILE, strerror(errno));
                return 0;
        }
-       if (fstat(fd, &st) != 0) {
-               log("wtmpx_get_entry: couldn't stat %s: %s",
-                   WTMP_FILE, strerror(errno));
+       if (fstat(fd, &st) != 0) {
+               logit("wtmpx_get_entry: couldn't stat %s: %s",
+                   WTMPX_FILE, strerror(errno));
                close(fd);
                return 0;
        }
-       
+
        /* Seek to the start of the last struct utmpx */
-       if (lseek(fd, (off_t)(0-sizeof(struct utmpx)), SEEK_END) == -1 ) {
+       if (lseek(fd, -(off_t)sizeof(struct utmpx), SEEK_END) == -1 ) {
                /* probably a newly rotated wtmpx file */
                close(fd);
                return 0;
        }
 
        while (!found) {
-               if (read(fd, &utx, sizeof(utx)) != sizeof(utx)) {
-                       log("wtmpx_get_entry: read of %s failed: %s",
+               if (atomicio(read, fd, &utx, sizeof(utx)) != sizeof(utx)) {
+                       logit("wtmpx_get_entry: read of %s failed: %s",
                            WTMPX_FILE, strerror(errno));
                        close (fd);
                        return 0;
@@ -1247,21 +1303,22 @@ wtmpx_get_entry(struct logininfo *li)
                /* Logouts are recorded as a blank username on a particular line.
                 * So, we just need to find the username in struct utmpx */
                if ( wtmpx_islogin(li, &utx) ) {
-#ifdef HAVE_TV_IN_UTMPX
+                       found = 1;
+# ifdef HAVE_TV_IN_UTMPX
                        li->tv_sec = utx.ut_tv.tv_sec;
-#else
+# else
 #  ifdef HAVE_TIME_IN_UTMPX
                        li->tv_sec = utx.ut_time;
 #  endif
-#endif
+# endif
                        line_fullname(li->line, utx.ut_line, sizeof(li->line));
-#ifdef HAVE_HOST_IN_UTMPX
+# ifdef HAVE_HOST_IN_UTMPX
                        strlcpy(li->hostname, utx.ut_host,
                                MIN_SIZEOF(li->hostname, utx.ut_host));
-#endif
+# endif
                        continue;
                }
-               if (lseek(fd, (off_t)(0-2*sizeof(struct utmpx)), SEEK_CUR) == -1) {
+               if (lseek(fd, -(off_t)(2 * sizeof(struct utmpx)), SEEK_CUR) == -1) {
                        close (fd);
                        return 0;
                }
@@ -1270,57 +1327,52 @@ wtmpx_get_entry(struct logininfo *li)
        close(fd);
        return 1;
 }
-
-
 #endif /* USE_WTMPX */
 
-
 /**
  ** Low-level libutil login() functions
  **/
 
 #ifdef USE_LOGIN
-
 static int
 syslogin_perform_login(struct logininfo *li)
 {
        struct utmp *ut;
 
-       if (! (ut = (struct utmp *)malloc(sizeof(struct utmp)))) {
-               log("syslogin_perform_login: couldn't malloc()");
+       if (! (ut = (struct utmp *)malloc(sizeof(*ut)))) {
+               logit("syslogin_perform_login: couldn't malloc()");
                return 0;
        }
        construct_utmp(li, ut);
        login(ut);
+       free(ut);
 
        return 1;
 }
 
-
 static int
 syslogin_perform_logout(struct logininfo *li)
 {
-#ifdef HAVE_LOGOUT
+# ifdef HAVE_LOGOUT
        char line[8];
-  
+
        (void)line_stripname(line, li->line, sizeof(line));
 
        if (!logout(line)) {
-               log("syslogin_perform_logout: logout() returned an error");
-# ifdef HAVE_LOGWTMP
+               logit("syslogin_perform_logout: logout() returned an error");
+#  ifdef HAVE_LOGWTMP
        } else {
                logwtmp(line, "", "");
+#  endif
        }
-# endif
        /* FIXME: (ATL - if the need arises) What to do if we have
         * login, but no logout?  what if logout but no logwtmp? All
         * routines are in libutil so they should all be there,
         * but... */
-#endif
+# endif
        return 1;
 }
 
-
 int
 syslogin_write_entry(struct logininfo *li)
 {
@@ -1330,49 +1382,43 @@ syslogin_write_entry(struct logininfo *li)
        case LTYPE_LOGOUT:
                return syslogin_perform_logout(li);
        default:
-               log("syslogin_write_entry: Invalid type field");
+               logit("syslogin_write_entry: Invalid type field");
                return 0;
        }
 }
-
-
 #endif /* USE_LOGIN */
 
 /* end of file log-syslogin.c */
 
-
 /**
  ** Low-level lastlog functions
  **/
 
 #ifdef USE_LASTLOG
+#define LL_FILE 1
+#define LL_DIR 2
+#define LL_OTHER 3
 
 static void
 lastlog_construct(struct logininfo *li, struct lastlog *last)
 {
        /* clear the structure */
-       memset(last, '\0', sizeof(struct lastlog));
-  
-       (void)line_stripname(last->ll_line, li->line,
-                            sizeof(last->ll_line));
+       memset(last, '\0', sizeof(*last));
+
+       (void)line_stripname(last->ll_line, li->line, sizeof(last->ll_line));
        strlcpy(last->ll_host, li->hostname,
                MIN_SIZEOF(last->ll_host, li->hostname));
        last->ll_time = li->tv_sec;
 }
 
-
-#define LL_FILE 1
-#define LL_DIR 2
-#define LL_OTHER 3
-
 static int
 lastlog_filetype(char *filename)
 {
        struct stat st;
 
-       if ( stat(LASTLOG_FILE, &st) != 0) {
-               log("lastlog_perform_login: Couldn't stat %s: %s",
-                   LASTLOG_FILE, strerror(errno));
+       if (stat(LASTLOG_FILE, &st) != 0) {
+               logit("lastlog_perform_login: Couldn't stat %s: %s", LASTLOG_FILE,
+                       strerror(errno));
                return 0;
        }
        if (S_ISDIR(st.st_mode))
@@ -1394,33 +1440,37 @@ lastlog_openseek(struct logininfo *li, int *fd, int filemode)
 
        type = lastlog_filetype(LASTLOG_FILE);
        switch (type) {
-       case LL_FILE:
-               strlcpy(lastlog_file, LASTLOG_FILE, sizeof(lastlog_file));
-               break;
-       case LL_DIR:
-               snprintf(lastlog_file, sizeof(lastlog_file), "%s/%s",
-                        LASTLOG_FILE, li->username);
-               break;
-       default:
-               log("lastlog_openseek: %.100s is not a file or directory!",
-                   LASTLOG_FILE);
-               return 0;
-       } /* switch */
+               case LL_FILE:
+                       strlcpy(lastlog_file, LASTLOG_FILE, sizeof(lastlog_file));
+                       break;
+               case LL_DIR:
+                       snprintf(lastlog_file, sizeof(lastlog_file), "%s/%s",
+                                LASTLOG_FILE, li->username);
+                       break;
+               default:
+                       logit("lastlog_openseek: %.100s is not a file or directory!",
+                           LASTLOG_FILE);
+                       return 0;
+       }
 
-       *fd = open(lastlog_file, filemode);
+       *fd = open(lastlog_file, filemode, 0600);
        if ( *fd < 0) {
-               log("lastlog_openseek: Couldn't open %s: %s",
+               debug("lastlog_openseek: Couldn't open %s: %s",
                    lastlog_file, strerror(errno));
                return 0;
        }
-       /* find this uid's offset in the lastlog file */
-       offset = (off_t) ( (long)li->uid * sizeof(struct lastlog));
 
-       if ( lseek(*fd, offset, SEEK_SET) != offset ) {
-               log("lastlog_openseek: %s->lseek(): %s",
-                   lastlog_file, strerror(errno));
-               return 0;
+       if (type == LL_FILE) {
+               /* find this uid's offset in the lastlog file */
+               offset = (off_t) ((long)li->uid * sizeof(struct lastlog));
+
+               if ( lseek(*fd, offset, SEEK_SET) != offset ) {
+                       logit("lastlog_openseek: %s->lseek(): %s",
+                        lastlog_file, strerror(errno));
+                       return 0;
+               }
        }
+
        return 1;
 }
 
@@ -1433,19 +1483,20 @@ lastlog_perform_login(struct logininfo *li)
        /* create our struct lastlog */
        lastlog_construct(li, &last);
 
+       if (!lastlog_openseek(li, &fd, O_RDWR|O_CREAT))
+               return(0);
+
        /* write the entry */
-       if (lastlog_openseek(li, &fd, O_RDWR)) {
-               if ( write(fd, &last, sizeof(struct lastlog)) 
-                    != sizeof(struct lastlog) ) {
-                       log("lastlog_write_filemode: Error writing to %s: %s",
-                           LASTLOG_FILE, strerror(errno));
-                       return 0;
-               }
-               return 1;
-       } else
+       if (atomicio(vwrite, fd, &last, sizeof(last)) != sizeof(last)) {
+               close(fd);
+               logit("lastlog_write_filemode: Error writing to %s: %s",
+                   LASTLOG_FILE, strerror(errno));
                return 0;
-}
+       }
 
+       close(fd);
+       return 1;
+}
 
 int
 lastlog_write_entry(struct logininfo *li)
@@ -1454,12 +1505,11 @@ lastlog_write_entry(struct logininfo *li)
        case LTYPE_LOGIN:
                return lastlog_perform_login(li);
        default:
-               log("lastlog_write_entry: Invalid type field");
+               logit("lastlog_write_entry: Invalid type field");
                return 0;
        }
 }
 
-
 static void
 lastlog_populate_entry(struct logininfo *li, struct lastlog *last)
 {
@@ -1469,26 +1519,36 @@ lastlog_populate_entry(struct logininfo *li, struct lastlog *last)
        li->tv_sec = last->ll_time;
 }
 
-
 int
 lastlog_get_entry(struct logininfo *li)
 {
        struct lastlog last;
-       int fd;
+       int fd, ret;
 
-       if (lastlog_openseek(li, &fd, O_RDONLY)) {
-               if ( read(fd, &last, sizeof(struct lastlog)) 
-                    != sizeof(struct lastlog) ) {
-                       log("lastlog_write_filemode: Error reading from %s: %s",
-                           LASTLOG_FILE, strerror(errno));
-                       return 0;
-               } else {
-                       lastlog_populate_entry(li, &last);
-                       return 1;
-               }
-       } else
-               return 0;    
-}
+       if (!lastlog_openseek(li, &fd, O_RDONLY))
+               return (0);
+
+       ret = atomicio(read, fd, &last, sizeof(last));
+       close(fd);
 
+       switch (ret) {
+       case 0:
+               memset(&last, '\0', sizeof(last));
+               /* FALLTHRU */
+       case sizeof(last):
+               lastlog_populate_entry(li, &last);
+               return (1);
+       case -1:
+               error("%s: Error reading from %s: %s", __func__,
+                   LASTLOG_FILE, strerror(errno));
+               return (0);
+       default:
+               error("%s: Error reading from %s: Expecting %d, got %d",
+                   __func__, LASTLOG_FILE, sizeof(last), ret);
+               return (0);
+       }
 
+       /* NOTREACHED */
+       return (0);
+}
 #endif /* USE_LASTLOG */
This page took 0.103247 seconds and 4 git commands to generate.