]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
login: lastlog code refactoring
authorKarel Zak <kzak@redhat.com>
Thu, 25 Aug 2011 16:05:33 +0000 (18:05 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 26 Oct 2011 21:17:16 +0000 (23:17 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
login-utils/login.c

index e29fb43ec459d4bb3a65451dbbdfa6a0e7f50176..91f5b815cfbe1897c0c6e5a2245b93da209dd3fe 100644 (file)
@@ -67,6 +67,7 @@
 #include "strutils.h"
 #include "nls.h"
 #include "xalloc.h"
+#include "writeall.h"
 
 #define is_pam_failure(_rc)    ((_rc) != PAM_SUCCESS)
 
@@ -102,6 +103,7 @@ struct login_context {
        char            hostaddress[16];
 
        pid_t           pid;
+       int             quiet;          /* 1 is hush file exists */
 };
 
 /*
@@ -117,7 +119,6 @@ static struct passwd pwdcopy;
 static void timedout(int);
 static void sigint(int);
 static void motd(void);
-static void dolastlog(struct login_context *cxt, int quiet);
 
 /*
  * Nice and simple code provided by Linus Torvalds 16-Feb-93
@@ -342,6 +343,53 @@ static void log_audit(struct login_context *cxt, struct passwd *pwd, int status)
 #define log_audit(cxt, pwd, status)
 #endif                         /* HAVE_LIBAUDIT */
 
+static void log_lastlog(struct login_context *cxt)
+{
+       struct lastlog ll;
+       time_t t;
+       int fd;
+
+       fd = open(_PATH_LASTLOG, O_RDWR, 0);
+       if (fd < 0)
+               return;
+
+       lseek(fd, (off_t) pwd->pw_uid * sizeof(ll), SEEK_SET);
+
+       /*
+        * Print last log message
+        */
+       if (!cxt->quiet) {
+               if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) &&
+                                                       ll.ll_time != 0) {
+                       time_t ll_time = (time_t) ll.ll_time;
+
+                       printf(_("Last login: %.*s "), 24 - 5, ctime(&ll_time));
+                       if (*ll.ll_host != '\0')
+                               printf(_("from %.*s\n"),
+                                      (int)sizeof(ll.ll_host), ll.ll_host);
+                       else
+                               printf(_("on %.*s\n"),
+                                      (int)sizeof(ll.ll_line), ll.ll_line);
+               }
+               lseek(fd, (off_t) pwd->pw_uid * sizeof(ll), SEEK_SET);
+       }
+
+       memset((char *)&ll, 0, sizeof(ll));
+
+       time(&t);
+       ll.ll_time = t;         /* ll_time is always 32bit */
+
+       if (cxt->tty_name)
+               xstrncpy(ll.ll_line, cxt->tty_name, sizeof(ll.ll_line));
+       if (cxt->hostname)
+               xstrncpy(ll.ll_host, cxt->hostname, sizeof(ll.ll_host));
+
+       if (write_all(fd, (char *)&ll, sizeof(ll)))
+               warn(_("write lastlog failed"));
+
+       close(fd);
+}
+
 /* encapsulate stupid "void **" pam_get_item() API */
 static int loginpam_get_username(pam_handle_t * pamh, char **name)
 {
@@ -387,7 +435,7 @@ int main(int argc, char **argv)
        register int ch;
        register char *p;
        int fflag, hflag, pflag, cnt;
-       int quietlog, passwd_req;
+       int passwd_req;
        char *domain;
        char tbuf[PATH_MAX + 2];
        char *termenv;
@@ -712,12 +760,12 @@ int main(int argc, char **argv)
                   have a libc in which snprintf is the same as sprintf */
                if (strlen(pwd->pw_dir) + sizeof(_PATH_HUSHLOGIN) + 2 >
                    PATH_MAX)
-                       quietlog = 0;
+                       cxt.quiet = 0;
                else {
                        sprintf(tmpstr, "%s/%s", pwd->pw_dir, _PATH_HUSHLOGIN);
                        setregid(-1, pwd->pw_gid);
                        setreuid(0, pwd->pw_uid);
-                       quietlog = (effective_access(tmpstr, O_RDONLY) == 0);
+                       cxt.quiet = (effective_access(tmpstr, O_RDONLY) == 0);
                        setuid(0);      /* setreuid doesn't do it alone! */
                        setreuid(ruid, 0);
                        setregid(-1, egid);
@@ -816,7 +864,7 @@ int main(int argc, char **argv)
        }
 
        log_audit(&cxt, pwd, 1);
-       dolastlog(&cxt, quietlog);
+       log_lastlog(&cxt);
 
        if (fchown(0, pwd->pw_uid,
                   (gr = getgrnam(TTYGRPNAME)) ? gr->gr_gid : pwd->pw_gid))
@@ -921,7 +969,7 @@ int main(int argc, char **argv)
                               pwd->pw_name);
        }
 
-       if (!quietlog) {
+       if (!cxt.quiet) {
                motd();
 
 #ifdef LOGIN_STAT_MAIL
@@ -1136,49 +1184,6 @@ void sigint(int sig __attribute__ ((__unused__)))
        longjmp(motdinterrupt, 1);
 }
 
-void dolastlog(struct login_context *cxt, int quiet)
-{
-       struct lastlog ll;
-       int fd;
-
-       if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
-               lseek(fd, (off_t) pwd->pw_uid * sizeof(ll), SEEK_SET);
-               if (!quiet) {
-                       if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) &&
-                           ll.ll_time != 0) {
-                               time_t ll_time = (time_t) ll.ll_time;
-
-                               printf(_("Last login: %.*s "),
-                                      24 - 5, ctime(&ll_time));
-
-                               if (*ll.ll_host != '\0')
-                                       printf(_("from %.*s\n"),
-                                              (int)sizeof(ll.ll_host),
-                                              ll.ll_host);
-                               else
-                                       printf(_("on %.*s\n"),
-                                              (int)sizeof(ll.ll_line),
-                                              ll.ll_line);
-                       }
-                       lseek(fd, (off_t) pwd->pw_uid * sizeof(ll), SEEK_SET);
-               }
-               memset((char *)&ll, 0, sizeof(ll));
-
-               {
-                       time_t t;
-                       time(&t);
-                       ll.ll_time = t; /* ll_time is always 32bit */
-               }
-
-               xstrncpy(ll.ll_line, cxt->tty_name, sizeof(ll.ll_line));
-               if (cxt->hostname)
-                       xstrncpy(ll.ll_host, cxt->hostname, sizeof(ll.ll_host));
-
-               if (write(fd, (char *)&ll, sizeof(ll)) < 0)
-                       warn(_("write lastlog failed"));
-               close(fd);
-       }
-}
 
 /* Should not be called from PAM code... */
 void sleepexit(int eval)