[BACK]Return to login.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Diff for /src/usr.bin/ssh/Attic/login.c between version 1.7 and 1.8

version 1.7, 1999/09/30 16:55:06 version 1.8, 1999/11/23 22:25:54
Line 24 
Line 24 
 #include <utmp.h>  #include <utmp.h>
 #include "ssh.h"  #include "ssh.h"
   
 /* Returns the time when the user last logged in.  Returns 0 if the  /* Returns the time when the user last logged in.  Returns 0 if the
    information is not available.  This must be called before record_login.     information is not available.  This must be called before record_login.
    The host the user logged in from will be returned in buf. */     The host the user logged in from will be returned in buf. */
   
 /* Returns the time when the user last logged in (or 0 if no previous login  /* Returns the time when the user last logged in (or 0 if no previous login
    is found).  The name of the host used last time is returned in buf. */     is found).  The name of the host used last time is returned in buf. */
   
 unsigned long get_last_login_time(uid_t uid, const char *logname,  unsigned long
                                   char *buf, unsigned int bufsize)  get_last_login_time(uid_t uid, const char *logname,
                       char *buf, unsigned int bufsize)
 {  {
   struct lastlog ll;          struct lastlog ll;
   char *lastlog;          char *lastlog;
   int fd;          int fd;
   
   lastlog = _PATH_LASTLOG;          lastlog = _PATH_LASTLOG;
           buf[0] = '\0';
   
   buf[0] = '\0';          fd = open(lastlog, O_RDONLY);
           if (fd < 0)
   fd = open(lastlog, O_RDONLY);                  return 0;
   if (fd < 0)          lseek(fd, (off_t) ((long) uid * sizeof(ll)), SEEK_SET);
     return 0;          if (read(fd, &ll, sizeof(ll)) != sizeof(ll)) {
   lseek(fd, (off_t)((long)uid * sizeof(ll)), SEEK_SET);                  close(fd);
   if (read(fd, &ll, sizeof(ll)) != sizeof(ll))                  return 0;
     {          }
       close(fd);          close(fd);
       return 0;          if (bufsize > sizeof(ll.ll_host) + 1)
     }                  bufsize = sizeof(ll.ll_host) + 1;
   close(fd);          strncpy(buf, ll.ll_host, bufsize - 1);
   if (bufsize > sizeof(ll.ll_host) + 1)          buf[bufsize - 1] = 0;
     bufsize = sizeof(ll.ll_host) + 1;          return ll.ll_time;
   strncpy(buf, ll.ll_host, bufsize - 1);  
   buf[bufsize - 1] = 0;  
   return ll.ll_time;  
 }  }
   
 /* Records that the user has logged in.  I these parts of operating systems  /* Records that the user has logged in.  I these parts of operating systems
    were more standardized. */     were more standardized. */
   
 void record_login(int pid, const char *ttyname, const char *user, uid_t uid,  void
                   const char *host, struct sockaddr_in *addr)  record_login(int pid, const char *ttyname, const char *user, uid_t uid,
                const char *host, struct sockaddr_in * addr)
 {  {
   int fd;          int fd;
   struct lastlog ll;          struct lastlog ll;
   char *lastlog;          char *lastlog;
           struct utmp u;
           const char *utmp, *wtmp;
   
   struct utmp u;          /* Construct an utmp/wtmp entry. */
   const char *utmp, *wtmp;          memset(&u, 0, sizeof(u));
           strncpy(u.ut_line, ttyname + 5, sizeof(u.ut_line));
           u.ut_time = time(NULL);
           strncpy(u.ut_name, user, sizeof(u.ut_name));
           strncpy(u.ut_host, host, sizeof(u.ut_host));
   
   /* Construct an utmp/wtmp entry. */          /* Figure out the file names. */
   memset(&u, 0, sizeof(u));          utmp = _PATH_UTMP;
   strncpy(u.ut_line, ttyname + 5, sizeof(u.ut_line));          wtmp = _PATH_WTMP;
   u.ut_time = time(NULL);  
   strncpy(u.ut_name, user, sizeof(u.ut_name));  
   strncpy(u.ut_host, host, sizeof(u.ut_host));  
   
   /* Figure out the file names. */          login(&u);
   utmp = _PATH_UTMP;          lastlog = _PATH_LASTLOG;
   wtmp = _PATH_WTMP;  
   
   login(&u);  
   
   lastlog = _PATH_LASTLOG;          /* Update lastlog unless actually recording a logout. */
           if (strcmp(user, "") != 0) {
                   /* It is safer to bzero the lastlog structure first
                      because some systems might have some extra fields in it
                      (e.g. SGI) */
                   memset(&ll, 0, sizeof(ll));
   
   /* Update lastlog unless actually recording a logout. */                  /* Update lastlog. */
   if (strcmp(user, "") != 0)                  ll.ll_time = time(NULL);
     {                  strncpy(ll.ll_line, ttyname + 5, sizeof(ll.ll_line));
       /* It is safer to bzero the lastlog structure first because some                  strncpy(ll.ll_host, host, sizeof(ll.ll_host));
          systems might have some extra fields in it (e.g. SGI) */                  fd = open(lastlog, O_RDWR);
       memset(&ll, 0, sizeof(ll));                  if (fd >= 0) {
                           lseek(fd, (off_t) ((long) uid * sizeof(ll)), SEEK_SET);
       /* Update lastlog. */                          if (write(fd, &ll, sizeof(ll)) != sizeof(ll))
       ll.ll_time = time(NULL);                                  log("Could not write %.100s: %.100s", lastlog, strerror(errno));
       strncpy(ll.ll_line, ttyname + 5, sizeof(ll.ll_line));                          close(fd);
       strncpy(ll.ll_host, host, sizeof(ll.ll_host));                  }
       fd = open(lastlog, O_RDWR);  
       if (fd >= 0)  
         {  
           lseek(fd, (off_t)((long)uid * sizeof(ll)), SEEK_SET);  
           if (write(fd, &ll, sizeof(ll)) != sizeof(ll))  
             log("Could not write %.100s: %.100s", lastlog, strerror(errno));  
           close(fd);  
         }          }
     }  
 }  }
   
 /* Records that the user has logged out. */  /* Records that the user has logged out. */
   
 void record_logout(int pid, const char *ttyname)  void
   record_logout(int pid, const char *ttyname)
 {  {
   const char *line = ttyname + 5; /* /dev/ttyq8 -> ttyq8 */          const char *line = ttyname + 5; /* /dev/ttyq8 -> ttyq8 */
   if (logout(line))          if (logout(line))
     logwtmp(line, "", "");                  logwtmp(line, "", "");
 }  }

Legend:
Removed from v.1.7  
changed lines
  Added in v.1.8