]> andersk Git - openssh.git/blobdiff - loginrec.c
Hopefully things did not get mixed around too much. It compiles under
[openssh.git] / loginrec.c
index 6e9c4ce46ce46d34473dc5400a89ba56f13ebe6c..b664a9cfec86ef329223376359a546be38b7ecce 100644 (file)
 
 /**
  ** 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 "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
  **/
@@ -273,7 +284,7 @@ login_get_lastlog(struct logininfo *li, const int uid)
 {
        struct passwd *pw;
 
-       memset(li, '\0', sizeof(struct logininfo));
+       memset(li, '\0', sizeof(*li));
        li->uid = uid;
 
        /* 
@@ -311,7 +322,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;
 }
@@ -339,7 +350,7 @@ login_init_entry(struct logininfo *li, int pid, const char *username,
 {
        struct passwd *pw;
        
-       memset(li, 0, sizeof(struct logininfo));
+       memset(li, 0, sizeof(*li));
   
        li->pid = pid;
 
@@ -400,10 +411,12 @@ login_set_addr(struct logininfo *li, const struct sockaddr *sa,
 int
 login_write (struct logininfo *li)
 {
+#ifndef HAVE_CYGWIN
        if ((int)geteuid() != 0) {
          log("Attempt to write login records by non-root user (aborting)");
          return 1;
        }
+#endif
 
        /* set the timestamp */
        login_set_current_time(li);
@@ -491,9 +504,9 @@ 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);
        }
@@ -506,7 +519,7 @@ 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;
@@ -526,8 +539,13 @@ line_abbrevname(char *dst, const char *src, int dstsize)
        memset(dst, '\0', dstsize);
        
        /* Always skip prefix if present */
+#ifdef sgi
+       if (strncmp(src, "/dev/tty", 8) == 0)
+               src += 8;
+#else
        if (strncmp(src, "/dev/", 5) == 0)
                src += 5;
+#endif
                
        len = strlen(src);
 
@@ -569,7 +587,7 @@ void
 construct_utmp(struct logininfo *li,
                    struct utmp *ut)
 {
-       memset(ut, '\0', sizeof(struct utmp));
+       memset(ut, '\0', sizeof(*ut));
 
        /* First fill out fields used for both logins and logouts */
 
@@ -643,7 +661,7 @@ set_utmpx_time(struct logininfo *li, struct utmpx *utx)
 void
 construct_utmpx(struct logininfo *li, struct utmpx *utx)
 {
-       memset(utx, '\0', sizeof(struct utmpx));
+       memset(utx, '\0', sizeof(*utx));
 # ifdef HAVE_ID_IN_UTMPX
        line_abbrevname(utx->ut_id, li->line, sizeof(utx->ut_id));
 # endif
@@ -675,7 +693,9 @@ construct_utmpx(struct logininfo *li, struct utmpx *utx)
        strncpy(utx->ut_host, li->hostname, MIN_SIZEOF(utx->ut_host, li->hostname));
 # endif
 # ifdef HAVE_ADDR_IN_UTMPX
-       /* FIXME: (ATL) not supported yet */
+       /* 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_SYSLEN_IN_UTMPX
        /* ut_syslen is the length of the utx_host string */
@@ -723,8 +743,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) {
+               log("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);
                /*
@@ -740,7 +782,7 @@ utmp_write_direct(struct logininfo *li, struct utmp *ut)
                }
                
                (void)lseek(fd, (off_t)(tty * sizeof(struct utmp)), SEEK_SET);
-               if (atomicio(write, fd, ut, sizeof(ut)) != sizeof(ut))
+               if (atomicio(write, fd, ut, sizeof(*ut)) != sizeof(*ut))
                        log("utmp_write_direct: error writing %s: %s",
                            UTMP_FILE, strerror(errno));
       
@@ -1031,7 +1073,7 @@ wtmp_get_entry(struct logininfo *li)
        }
 
        /* 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)(0 - sizeof(struct utmp)), SEEK_END) == -1) {
                /* Looks like we've got a fresh wtmp file */
                close(fd);
                return 0;
@@ -1238,7 +1280,7 @@ syslogin_perform_login(struct logininfo *li)
 {
        struct utmp *ut;
 
-       if (! (ut = (struct utmp *)malloc(sizeof(struct utmp)))) {
+       if (! (ut = (struct utmp *)malloc(sizeof(*ut)))) {
                log("syslogin_perform_login: couldn't malloc()");
                return 0;
        }
@@ -1301,7 +1343,7 @@ static void
 lastlog_construct(struct logininfo *li, struct lastlog *last)
 {
        /* clear the structure */
-       memset(last, '\0', sizeof(struct lastlog));
+       memset(last, '\0', sizeof(*last));
   
        (void)line_stripname(last->ll_line, li->line, sizeof(last->ll_line));
        strlcpy(last->ll_host, li->hostname,
@@ -1358,14 +1400,17 @@ lastlog_openseek(struct logininfo *li, int *fd, int filemode)
                return 0;
        }
        
-       /* find this uid's offset in the lastlog file */
-       offset = (off_t) ( (long)li->uid * sizeof(struct lastlog));
+       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 ) {
-               log("lastlog_openseek: %s->lseek(): %s",
-                   lastlog_file, strerror(errno));
-               return 0;
+               if ( lseek(*fd, offset, SEEK_SET) != offset ) {
+                       log("lastlog_openseek: %s->lseek(): %s",
+                        lastlog_file, strerror(errno));
+                       return 0;
+               }
        }
+       
        return 1;
 }
 
@@ -1378,17 +1423,19 @@ 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|O_CREAT)) {
-               if (atomicio(write, fd, &last, sizeof(last)) != sizeof(last)) {
-                       log("lastlog_write_filemode: Error writing to %s: %s",
-                           LASTLOG_FILE, strerror(errno));
-                       return 0;
-               }
-               return 1;
-       } else {
+       if (atomicio(write, fd, &last, sizeof(last)) != sizeof(last)) {
+               close(fd);
+               log("lastlog_write_filemode: Error writing to %s: %s",
+                   LASTLOG_FILE, strerror(errno));
                return 0;
        }
+
+       close(fd);
+       return 1;
 }
 
 int
This page took 0.049898 seconds and 4 git commands to generate.