* krlogind.c
authorRichard Basch <probe@mit.edu>
Tue, 27 Dec 1994 19:46:44 +0000 (19:46 +0000)
committerRichard Basch <probe@mit.edu>
Tue, 27 Dec 1994 19:46:44 +0000 (19:46 +0000)
* logutil.c
* login.c
   Revamped the utmp/wtmp handling routines

git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@4758 dc483132-0cff-0310-8789-dd5450dbe970

src/appl/bsd/krlogind.c
src/appl/bsd/login.c
src/appl/bsd/logutil.c

index 36fdc33ed1468ed30087139ad0b32a0eb72c8f3d..ae1f01f29bced12927bd2be135cfa0e27969ce9e 100644 (file)
@@ -160,10 +160,6 @@ char copyright[] =
 #include <sys/param.h>
 #include <utmp.h>
 
-#ifdef HAVE_UTXENT
-#include <utmpx.h>
-#endif
-     
 #ifdef HAVE_SYS_TTY_H
 #include <sys/tty.h>
 #endif
@@ -772,41 +768,17 @@ void doit(f, fromp)
        }
 #endif
 
-#ifdef HAVE_UTENT
+#ifdef LOGIN_PROCESS
        {
            int tmpx;
            char utmp_id[5];
            struct utmp ent;
-#ifdef HAVE_UTXENT
-           struct utmpx entx;
-#endif
-           strcpy(ent.ut_user, "rlogin");
-           strcpy(ent.ut_line, line+sizeof("/dev/")-1);
 
-           sscanf(line, "/dev/pts/%d", &tmpx);
-           sprintf(utmp_id, "kl%02d", tmpx);
-           strncpy(ent.ut_id, utmp_id, sizeof(ent.ut_id));
-
-           ent.ut_pid = getpid;
+#ifndef NO_UT_PID
+           ent.ut_pid = getpid();
+#endif
            ent.ut_type = LOGIN_PROCESS;
-           ent.ut_time = time(0);
-
-#ifdef HAVE_UTXENT
-           getutmpx(&ent, &entx);
-           setutxent();
-           pututxline(&entx);
-           endutxent();
-           updwtmpx(WTMPX_FILE, &ent);
-#endif
-           utmpname(UTMP_FILE);
-           setutent();
-           pututline(&ent);
-           endutent();
-
-           utmpname(WTMP_FILE);
-           setutent();
-           pututline(&ent);
-           endutent();
+           update_utmp(&ent, "rlogin", line, (char *)0);
        }
 #endif
        
@@ -1074,23 +1046,15 @@ protocol(f, p)
 krb5_sigtype cleanup()
 {
     char *p;
+    struct utmp ut;
     
-    /* 
-      I dont know why P starts with the character '/', but apparently it
-      has to do with the way login set line when the initial entry for this
-      line is made.
-      */
-    p = line + sizeof("/dev/") -1 ;
-    if (!logout(p)) {
-#ifdef SYSV
-       logwtmp(p, "", "", 0, 0);
-#else
-       logwtmp(p, "", "", 0);
+#ifndef NO_UT_PID
+    ut.ut_pid = 0;
 #endif
-    }
-    else 
-      syslog(LOG_ERR ,
-            "Cannot delete entry from utmp for %s\n",p);
+#ifdef HAVE_SETUTENT
+    ut.ut_type = DEAD_PROCESS;
+#endif
+    update_utmp(&ut, "", line, (char *)0);
     
     (void)chmod(line, 0666);
     (void)chown(line, 0, 0);
index b612ed2eb8459cfa9f4aa749bc0e133954f20f68..5d7d3fe0bb1ef7005380faa5146eb39a785126ba 100644 (file)
@@ -240,7 +240,7 @@ main(argc, argv)
        int kflag, Kflag, eflag;
        int quietlog, passwd_req, ioctlval;
        sigtype timedout();
-       char *domain, *salt, **envinit, *ttyn, *tty, *ktty;
+       char *domain, *salt, **envinit, *ttyn, *tty;
        char tbuf[MAXPATHLEN + 2];
        char *ttyname(), *stypeof(), *crypt(), *getpass();
        time_t login_time;
@@ -490,12 +490,6 @@ main(argc, argv)
        else
                tty = ttyn;
 
-       /* For kerberos tickets, extract only the last part of the ttyname */
-       if (ktty = strrchr(tty, '/'))
-               ++ktty;
-       else
-               ktty = tty;
-
 #ifndef LOG_ODELAY /* 4.2 syslog ... */                      
        openlog("login", 0);
 #else
@@ -597,14 +591,6 @@ main(argc, argv)
                       log in.  Can you say abstraction violation? */
                    _res.retrans = 1;
 #endif /* BIND_HACK */
-#if 0  /* XXX krb5 has defaults; don't do this. */
-                   /* Set up the ticket file environment variable */
-                   strncpy(tkfile, KRB_TK_DIR, sizeof(tkfile));
-                   strncat(tkfile, ktty,
-                           sizeof(tkfile) - strlen(tkfile) - 1);
-                   (void) setenv(KRB_ENVIRON, tkfile, 1);
-                   krb_set_tkt_string(tkfile);
-#endif
 
 #ifdef _IBMR2
                    krbval = setuidx(ID_REAL|ID_EFFECTIVE, pwd->pw_uid);
@@ -766,17 +752,13 @@ bad_login:
 
                memset((char *)&utmp, 0, sizeof(utmp));
                login_time = time(&utmp.ut_time);
-               (void) strncpy(utmp.ut_name, username, sizeof(utmp.ut_name));
-#ifndef NO_UT_HOST
-               if (hostname)
-                   (void) strncpy(utmp.ut_host, hostname,
-                                  sizeof(utmp.ut_host));
-               else
-                   memset(utmp.ut_host, 0, sizeof(utmp.ut_host));
+#ifdef USER_PROCESS
+               utmp.ut_type = USER_PROCESS;
+#endif
+#ifndef NO_UT_PID
+               utmp.ut_pid = getppid();
 #endif
-               /* Solaris 2.0, 2.1 used ttyn here. Never Again... */
-               (void) strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line));
-               login(&utmp);
+               update_utmp(&utmp, username, ttyn, hostname);
        }
 
        quietlog = access(HUSHLOGIN, F_OK) == 0;
index 9ee9b243302d95519d1ea26bc26993f9919c8e61..2be2c0db0b8643562f8866027feee4b0148a8f4e 100644 (file)
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
 #include <sys/types.h>
 #include <sys/file.h>
-#include <utmp.h>
 #include <stdio.h>
 #include <string.h>
 #include <sys/time.h>
 #include <sys/stat.h>
-#ifdef NEED_SYS_FCNTL_H
-#include <sys/fcntl.h>
+#include <fcntl.h>
+#include <utmp.h>
+#ifdef HAVE_SETUTXENT
+#include <utmpx.h>
 #endif
-     
+
 #ifndef UTMP_FILE
 #define        UTMP_FILE       "/etc/utmp"
 #endif
 #ifndef WTMP_FILE
-#ifdef SYSV
-#define WTMPFILE    "/etc/wtmp"
-#else
 #define        WTMP_FILE       "/usr/adm/wtmp"
 #endif
-#endif
      
 #ifndef EMPTY
 /* linux has UT_UNKNOWN but not EMPTY */
 #define EMPTY UT_UNKNOWN
 #endif
 
-void login(ut)
-     struct utmp *ut;
+void update_utmp();
+void update_wtmp();
+void logwtmp();
+
+void update_utmp(ent, username, line, host)
+    struct utmp *ent;
+    char *username, *line, *host;
 {
-    register int fd;
-    struct utmp utmp;
-    int tty;
+    char *tmpx;
+#ifdef HAVE_SETUTENT
+    struct utmp ut;
+    char utmp_id[5];
+#else
+    struct stat statb;
+#endif
+#ifdef HAVE_SETUTXENT
+    struct utmpx utx;
+#endif
     
-#if defined(HAVE_GETUTENT) && !defined(NO_UT_PID)
-    if (!ut->ut_pid)
-       ut->ut_pid = getppid();
-    ut->ut_type = USER_PROCESS;
-    (void) strncpy(ut->ut_id, ut->ut_line, sizeof(ut->ut_id));
-
-    (void) setutent();
-    (void) memset((char *)&utmp, 0, sizeof(utmp));
-    (void) strncpy(utmp.ut_id, ut->ut_id, sizeof(utmp.ut_id));
-    utmp.ut_type = DEAD_PROCESS;
-    (void) getutid(&utmp);
-
-    (void) pututline(ut);
-    (void) endutent();
+    strncpy(ent->ut_line, line+sizeof("/dev/")-1, sizeof(ent->ut_line));
+    ent->ut_time = time(0);
+
+#ifndef NO_UT_HOST
+    if (host)
+       strncpy(ent->ut_host, host, sizeof(ent->ut_host));
+    else
+       ent->ut_host[0] = '\0';
+#endif
+
+#ifdef HAVE_SETUTENT
+    tmpx = line + strlen(line)-1;
+    if (*(tmpx-1) != '/') tmpx--; /* last two characters, unless it's a / */
+    sprintf(utmp_id, "kl%s", tmpx);
+    strncpy(ent->ut_id, utmp_id, sizeof(ent->ut_id));
+    strncpy(ent->ut_user, username, sizeof(ent->ut_user));
+
+    switch(ent->ut_type) {
+    case LOGIN_PROCESS:
+    case USER_PROCESS:
+       memset((char *)&ut, 0, sizeof(ut));
+       strncpy(ut.ut_id, ent->ut_id, sizeof(ut.ut_id));
+       ut.ut_type = DEAD_PROCESS;
+       getutid(&ut);
+       break;
+    case DEAD_PROCESS:
+       memset((char *)&ut, 0, sizeof(ut));
+       strncpy(ut.ut_id, ent->ut_id, sizeof(ut.ut_id));
+       ut.ut_type = USER_PROCESS;
+       getutid(&ut);
+       break;
+    }
 #else
+    strncpy(ent->ut_name, username, sizeof(ent->ut_name));
+#endif
+    
+#ifdef HAVE_SETUTXENT
+    getutmpx(ent, &utx);
+    setutxent();
+    pututxline(&utx);
+    endutxent();
+#endif
+#ifdef HAVE_SETUTENT
+    utmpname(UTMP_FILE);
+    setutent();
+    pututline(ent);
+    endutent();
+#else /* HAVE_SETUTENT */
     tty = ttyslot();
     if (tty > 0 && (fd = open(UTMP_FILE, O_WRONLY, 0)) >= 0) {
        (void)lseek(fd, (off_t)(tty * sizeof(struct utmp)), SEEK_SET);
-       (void)write(fd, (char *)ut, sizeof(struct utmp));
+       (void)write(fd, (char *)ent, sizeof(struct utmp));
        (void)close(fd);
     }
+#endif /* HAVE_SETUTENT */
+
+    update_wtmp(ent);
+}
+
+void update_wtmp(ent)
+    struct utmp *ent;
+{
+#ifdef HAVE_SETUTXENT
+    struct utmpx utx;
+
+    getutmpx(ent, &utx);
+    updwtmpx(&utx);
 #endif
+
+#ifdef HAVE_UPDWTMP
+    updwtmp(WTMP_FILE, ent);
+#else /* HAVE_UPDWTMP */
+#ifdef HAVE_SETUTENT
+    utmpname(WTMP_FILE);
+    setutent();
+    pututline(ent);
+    endutent();
+#else /* HAVE_SETUTENT */
+
     if ((fd = open(WTMP_FILE, O_WRONLY|O_APPEND, 0)) >= 0) {
-       (void)write(fd, (char *)ut, sizeof(struct utmp));
+       if (!fstat(fd, &statb)) {
+           if (write(fd, (char *)&ut, sizeof(struct utmp)) !=
+               sizeof(struct utmp))
+             (void)ftruncate(fd, statb.st_size);
+       }
        (void)close(fd);
     }
+#endif /* HAVE_SETUTENT */
+#endif /* HAVE_UPDWTMP */
 }
 
-logout(line)
-    register char *line;
+void logwtmp(tty, locuser, host, loggingin)
+    char *tty;
+    char *locuser;
+    char *host;
+    int loggingin;
 {
-    register FILE *fp;
     struct utmp ut;
-    int rval;
-    
-    if (!(fp = fopen(UTMP_FILE, "r+")))
-       return(0);
-    rval = 1;
-    while (fread((char *)&ut, sizeof(struct utmp), 1, fp) == 1) {
-       if (!ut.ut_name[0] ||
-           strncmp(ut.ut_line, line, sizeof(ut.ut_line)))
-           continue;
-       memset(ut.ut_name,0, sizeof(ut.ut_name));
-#ifndef  NO_UT_HOST
-       memset(ut.ut_host,0, sizeof(ut.ut_host));
-#endif
-       (void)time(&ut.ut_time);
-#if defined(HAVE_GETUTENT) && !defined(NO_UT_PID)
-       memset(ut.ut_id, 0, sizeof(ut.ut_id));
-       ut.ut_pid = 0;
-#ifndef NO_UT_EXIT
-       ut.ut_exit.e_exit = 0;
-#endif
-       ut.ut_type = EMPTY;
-#endif
-       (void)fseek(fp, (off_t)-sizeof(struct utmp), SEEK_CUR);
-       (void)fwrite((char *)&ut, sizeof(struct utmp), 1, fp);
-       (void)fseek(fp, (off_t)0, SEEK_CUR);
-       rval = 0;
-    }
-    (void)fclose(fp);
-    return(rval);
-}
+    char *tmpx;
+    char utmp_id[5];
 
-static int fd = -1;
-
-#ifndef SYSV
-logwtmp(line, name, host, keep_open)
-#else
-logwtmp(line, name, host, keep_open, logingin)
-#endif
-     char *line, *name, *host;
-     int keep_open;
-#ifdef  SYSV
-     int logingin;
+#ifndef NO_UT_HOST
+    strncpy(ut.ut_host, host, sizeof(ut.ut_host));
 #endif
-{
-    struct utmp ut;
-    struct stat buf;
+
+    strncpy(ut.ut_line, tty, sizeof(ut.ut_line));
+    ut.ut_time = time(0);
     
-    if (fd < 0 && (fd = open(WTMP_FILE, O_WRONLY|O_APPEND, 0)) < 0)
-      return;
-    if (!fstat(fd, &buf)) {
-       (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
-       (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
-#ifndef  NO_UT_HOST
-       (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
-#endif 
-#ifdef  SYSV
-       (void)strncpy(ut.ut_id, (char *)ut.ut_line + 4,
-                     sizeof(ut.ut_id));
-       ut.ut_type = logingin ? USER_PROCESS : DEAD_PROCESS;
-       ut.ut_pid = getpid();
+#ifdef HAVE_SETUTENT
+    strncpy(ut.ut_user, locuser, sizeof(ut.ut_user));
+
+    tmpx = tty + strlen(tty) - 2;
+    sprintf(utmp_id, "kr%s", tmpx);
+    strncpy(ut.ut_id, utmp_id, sizeof(ut.ut_id));
+
+    ut.ut_type = (loggingin ? USER_PROCESS : DEAD_PROCESS);
+#ifndef NO_UT_PID
+    ut.ut_pid = (loggingin ? getpid() : 0);
 #endif
-       (void)time(&ut.ut_time);
-#if defined(HAVE_GETUTENT) && !defined(NO_UT_PID)
-       if (*name) {
-           if (!ut.ut_pid)
-               ut.ut_pid = getpid();
-           ut.ut_type = USER_PROCESS;
-       } else {
-           ut.ut_type = EMPTY;
-       }
 #endif
-       if (write(fd, (char *)&ut, sizeof(struct utmp)) !=
-           sizeof(struct utmp))
-         (void)ftruncate(fd, buf.st_size);
-    }
-    if ( !keep_open)
-      (void)close(fd);
+
+    update_wtmp(&ut);
 }