cdown / rpms / util-linux

Forked from rpms/util-linux 2 years ago
Clone
6717ab
diff -up util-linux-2.23.2/configure.ac.kzak util-linux-2.23.2/configure.ac
6717ab
--- util-linux-2.23.2/configure.ac.kzak	2014-09-25 14:47:46.944259193 +0200
6717ab
+++ util-linux-2.23.2/configure.ac	2014-09-25 14:48:20.087575417 +0200
6717ab
@@ -1027,6 +1027,11 @@ UL_REQUIRES_HAVE([lscpu], [cpu_set_t], [
6717ab
 AM_CONDITIONAL(BUILD_LSCPU, test "x$build_lscpu" = xyes)
6717ab
 
6717ab
 
6717ab
+UL_BUILD_INIT([lslogins], [check])
6717ab
+UL_REQUIRES_BUILD([lslogins], [libsmartcols])
6717ab
+AM_CONDITIONAL([BUILD_LSLOGINS], [test "x$build_lslogins" = xyes])
6717ab
+
6717ab
+
6717ab
 UL_BUILD_INIT([chcpu], [check])
6717ab
 UL_REQUIRES_LINUX([chcpu])
6717ab
 UL_REQUIRES_HAVE([chcpu], [cpu_set_t], [cpu_set_t type])
6717ab
@@ -1404,6 +1409,37 @@ fi
6717ab
 AM_CONDITIONAL(HAVE_SYSTEMD, [test -n "$with_systemdsystemunitdir" -a "x$with_systemdsystemunitdir" != "xno" ])
6717ab
 
6717ab
 
6717ab
+#
6717ab
+# Backport from upstrem to RHEL7.1
6717ab
+#
6717ab
+AC_ARG_WITH([systemd],
6717ab
+  AS_HELP_STRING([--with-systemd], [build with support for systemd]),
6717ab
+  [], [with_systemd=check]
6717ab
+)
6717ab
+
6717ab
+have_systemd=no
6717ab
+AS_IF([test "x$with_systemd" != xno], [
6717ab
+  # new version -- all libsystemd-* libs merged into libsystemd
6717ab
+  PKG_CHECK_MODULES([SYSTEMD], [libsystemd], [have_systemd=yes], [have_systemd=no])
6717ab
+  # old versions
6717ab
+  AS_IF([test "x$have_systemd" != "xyes"], [
6717ab
+    PKG_CHECK_MODULES([SYSTEMD_DAEMON], [libsystemd-daemon],
6717ab
+		      [have_systemd_daemon=yes], [have_systemd_daemon=no])
6717ab
+    PKG_CHECK_MODULES([SYSTEMD_JOURNAL], [libsystemd-journal],
6717ab
+		      [have_systemd_journal=yes], [have_systemd_journal=no])
6717ab
+    AS_IF([test "x$have_systemd_daemon" = "xyes" -a "x$have_systemd_journal" = "xyes" ],[
6717ab
+	   have_systemd=yes])
6717ab
+  ])
6717ab
+  AS_CASE([$with_systemd:$have_systemd],
6717ab
+    [yes:no],
6717ab
+      [AC_MSG_ERROR([systemd expected but libsystemd not found])],
6717ab
+    [*:yes],
6717ab
+       AC_DEFINE([HAVE_LIBSYSTEMD], [1], [Define if libsystemd is available])
6717ab
+  )
6717ab
+])
6717ab
+
6717ab
+
6717ab
+
6717ab
 AC_ARG_WITH([bashcompletiondir],
6717ab
   AS_HELP_STRING([--with-bashcompletiondir=DIR], [Bash completions directory]),
6717ab
   [],
6717ab
diff -up util-linux-2.23.2/include/Makemodule.am.kzak util-linux-2.23.2/include/Makemodule.am
6717ab
--- util-linux-2.23.2/include/Makemodule.am.kzak	2013-07-15 10:25:46.277049008 +0200
6717ab
+++ util-linux-2.23.2/include/Makemodule.am	2014-09-25 14:48:20.087575417 +0200
6717ab
@@ -35,6 +35,7 @@ dist_noinst_HEADERS += \
6717ab
 	include/procutils.h \
6717ab
 	include/randutils.h \
6717ab
 	include/rpmatch.h \
6717ab
+	include/readutmp.h \
6717ab
 	include/setproctitle.h \
6717ab
 	include/strutils.h \
6717ab
 	include/swapheader.h \
6717ab
diff -up util-linux-2.23.2/include/readutmp.h.kzak util-linux-2.23.2/include/readutmp.h
6717ab
--- util-linux-2.23.2/include/readutmp.h.kzak	2014-09-25 14:48:20.088575426 +0200
6717ab
+++ util-linux-2.23.2/include/readutmp.h	2014-09-25 14:48:20.087575417 +0200
6717ab
@@ -0,0 +1,28 @@
6717ab
+/* Declarations for GNU's read utmp module.
6717ab
+
6717ab
+   Copyright (C) 1992-2007, 2009-2014 Free Software Foundation, Inc.
6717ab
+
6717ab
+   This program is free software: you can redistribute it and/or modify
6717ab
+   it under the terms of the GNU General Public License as published by
6717ab
+   the Free Software Foundation; either version 3 of the License, or
6717ab
+   (at your option) any later version.
6717ab
+
6717ab
+   This program is distributed in the hope that it will be useful,
6717ab
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
6717ab
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6717ab
+   GNU General Public License for more details.
6717ab
+
6717ab
+   You should have received a copy of the GNU General Public License
6717ab
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
6717ab
+
6717ab
+/* Written by jla; revised by djm */
6717ab
+
6717ab
+#ifndef READUTMP_H
6717ab
+#define READUTMP_H
6717ab
+
6717ab
+#include <sys/types.h>
6717ab
+#include <utmp.h>
6717ab
+
6717ab
+int read_utmp (char const *file, size_t *n_entries, struct utmp **utmp_buf);
6717ab
+
6717ab
+#endif /* READUTMP_H */
6717ab
diff -up util-linux-2.23.2/lib/Makemodule.am.kzak util-linux-2.23.2/lib/Makemodule.am
6717ab
--- util-linux-2.23.2/lib/Makemodule.am.kzak	2013-07-30 10:39:26.202738200 +0200
6717ab
+++ util-linux-2.23.2/lib/Makemodule.am	2014-09-25 14:48:20.088575426 +0200
6717ab
@@ -25,7 +25,8 @@ libcommon_la_SOURCES = \
6717ab
 	lib/wholedisk.c \
6717ab
 	lib/ttyutils.c \
6717ab
 	lib/xgetpass.c \
6717ab
-	lib/exec_shell.c
6717ab
+	lib/exec_shell.c \
6717ab
+	lib/readutmp.c
6717ab
 
6717ab
 if LINUX
6717ab
 libcommon_la_SOURCES += \
6717ab
diff -up util-linux-2.23.2/lib/readutmp.c.kzak util-linux-2.23.2/lib/readutmp.c
6717ab
--- util-linux-2.23.2/lib/readutmp.c.kzak	2014-09-25 14:48:20.088575426 +0200
6717ab
+++ util-linux-2.23.2/lib/readutmp.c	2014-09-25 14:48:20.088575426 +0200
6717ab
@@ -0,0 +1,76 @@
6717ab
+/* GNU's read utmp module.
6717ab
+
6717ab
+	 Copyright (C) 1992-2001, 2003-2006, 2009-2014 Free Software Foundation, Inc.
6717ab
+
6717ab
+	 This program is free software: you can redistribute it and/or modify
6717ab
+	 it under the terms of the GNU General Public License as published by
6717ab
+	 the Free Software Foundation; either version 3 of the License, or
6717ab
+	 (at your option) any later version.
6717ab
+
6717ab
+	 This program is distributed in the hope that it will be useful,
6717ab
+	 but WITHOUT ANY WARRANTY; without even the implied warranty of
6717ab
+	 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
6717ab
+	 GNU General Public License for more details.
6717ab
+
6717ab
+	 You should have received a copy of the GNU General Public License
6717ab
+	 along with this program.	If not, see <http://www.gnu.org/licenses/>.	*/
6717ab
+
6717ab
+/* Written by jla; revised by djm */
6717ab
+/* extracted for util-linux by ooprala */
6717ab
+
6717ab
+#include <errno.h>
6717ab
+#include <stdio.h>
6717ab
+
6717ab
+#include <sys/types.h>
6717ab
+#include <sys/stat.h>
6717ab
+#include <signal.h>
6717ab
+#include <stdbool.h>
6717ab
+#include <string.h>
6717ab
+#include <stdlib.h>
6717ab
+#include <stdint.h>
6717ab
+
6717ab
+#include "xalloc.h"
6717ab
+#include "readutmp.h"
6717ab
+
6717ab
+/* Read the utmp entries corresponding to file FILE into freshly-
6717ab
+	 malloc'd storage, set *UTMP_BUF to that pointer, set *N_ENTRIES to
6717ab
+	 the number of entries, and return zero.	If there is any error,
6717ab
+	 return -1, setting errno, and don't modify the parameters.
6717ab
+	 If OPTIONS & READ_UTMP_CHECK_PIDS is nonzero, omit entries whose
6717ab
+	 process-IDs do not currently exist.	*/
6717ab
+int
6717ab
+read_utmp (char const *file, size_t *n_entries, struct utmp **utmp_buf)
6717ab
+{
6717ab
+	size_t n_read = 0;
6717ab
+	size_t n_alloc = 0;
6717ab
+	struct utmp *utmp = NULL;
6717ab
+	struct utmp *u;
6717ab
+
6717ab
+	/* Ignore the return value for now.
6717ab
+		 Solaris' utmpname returns 1 upon success -- which is contrary
6717ab
+		 to what the GNU libc version does.	In addition, older GNU libc
6717ab
+		 versions are actually void.	 */
6717ab
+	utmpname(file);
6717ab
+
6717ab
+	setutent();
6717ab
+
6717ab
+	errno = 0;
6717ab
+	while ((u = getutent()) != NULL) {
6717ab
+		if (n_read == n_alloc) {
6717ab
+			n_alloc += 32;
6717ab
+			utmp = xrealloc(utmp, n_alloc * sizeof (struct utmp));
6717ab
+			if (!utmp)
6717ab
+				return -1;
6717ab
+		}
6717ab
+		utmp[n_read++] = *u;
6717ab
+	}
6717ab
+	if (!u && errno)
6717ab
+		return -1;
6717ab
+
6717ab
+	endutent();
6717ab
+
6717ab
+	*n_entries = n_read;
6717ab
+	*utmp_buf = utmp;
6717ab
+
6717ab
+	return 0;
6717ab
+}
6717ab
diff -up util-linux-2.23.2/login-utils/login.c.kzak util-linux-2.23.2/login-utils/login.c
6717ab
--- util-linux-2.23.2/login-utils/login.c.kzak	2014-09-25 14:47:46.870258487 +0200
6717ab
+++ util-linux-2.23.2/login-utils/login.c	2014-09-25 14:48:50.503865622 +0200
6717ab
@@ -923,124 +923,6 @@ static void loginpam_session(struct logi
6717ab
 }
6717ab
 
6717ab
 /*
6717ab
- * We need to check effective UID/GID. For example $HOME could be on root
6717ab
- * squashed NFS or on NFS with UID mapping and access(2) uses real UID/GID.
6717ab
- * The open(2) seems as the surest solution.
6717ab
- * -- kzak@redhat.com (10-Apr-2009)
6717ab
- */
6717ab
-static int effective_access(const char *path, int mode)
6717ab
-{
6717ab
-	int fd = open(path, mode);
6717ab
-	if (fd != -1)
6717ab
-		close(fd);
6717ab
-	return fd == -1 ? -1 : 0;
6717ab
-}
6717ab
-
6717ab
-/*
6717ab
- * Check per accout or global hush-login setting.
6717ab
- *
6717ab
- * Hushed mode is enabled:
6717ab
- *
6717ab
- * a) if global (e.g. /etc/hushlogins) hush file exists:
6717ab
- *     1) for ALL ACCOUNTS if the file is empty
6717ab
- *     2) for the current user if the username or shell are found in the file
6717ab
- *
6717ab
- * b) if ~/.hushlogin file exists
6717ab
- *
6717ab
- * The ~/.hushlogin is ignored if the global hush file exists.
6717ab
- *
6717ab
- * The HUSHLOGIN_FILE login.def variable overwrites the default hush filename.
6717ab
- *
6717ab
- * Note that shadow-utils login(1) does not support "a1)". The "a1)" is
6717ab
- * necessary if you want to use PAM for "Last login" message.
6717ab
- *
6717ab
- * -- Karel Zak <kzak@redhat.com> (26-Aug-2011)
6717ab
- *
6717ab
- *
6717ab
- * Per-account check requires some explanation: As root we may not be able to
6717ab
- * read the directory of the user if it is on an NFS mounted filesystem. We
6717ab
- * temporarily set our effective uid to the user-uid making sure that we keep
6717ab
- * root privs. in the real uid.
6717ab
- *
6717ab
- * A portable solution would require a fork(), but we rely on Linux having the
6717ab
- * BSD setreuid()
6717ab
- */
6717ab
-static int get_hushlogin_status(struct passwd *pwd)
6717ab
-{
6717ab
-	const char *files[] = { _PATH_HUSHLOGINS, _PATH_HUSHLOGIN, NULL };
6717ab
-	const char *file;
6717ab
-	char buf[BUFSIZ];
6717ab
-	int i;
6717ab
-
6717ab
-	file = getlogindefs_str("HUSHLOGIN_FILE", NULL);
6717ab
-	if (file) {
6717ab
-		if (!*file)
6717ab
-			return 0;	/* empty HUSHLOGIN_FILE defined */
6717ab
-
6717ab
-		files[0] = file;
6717ab
-		files[1] = NULL;
6717ab
-	}
6717ab
-
6717ab
-	for (i = 0; files[i]; i++) {
6717ab
-		int ok = 0;
6717ab
-
6717ab
-		file = files[i];
6717ab
-
6717ab
-		/* Global hush-file*/
6717ab
-		if (*file == '/') {
6717ab
-			struct stat st;
6717ab
-			FILE *f;
6717ab
-
6717ab
-			if (stat(file, &st) != 0)
6717ab
-				continue;	/* file does not exist */
6717ab
-
6717ab
-			if (st.st_size == 0)
6717ab
-				return 1;	/* for all accounts */
6717ab
-
6717ab
-			f = fopen(file, "r");
6717ab
-			if (!f)
6717ab
-				continue;	/* ignore errors... */
6717ab
-
6717ab
-			while (ok == 0 && fgets(buf, sizeof(buf), f)) {
6717ab
-				buf[strlen(buf) - 1] = '\0';
6717ab
-				ok = !strcmp(buf, *buf == '/' ? pwd->pw_shell :
6717ab
-								pwd->pw_name);
6717ab
-			}
6717ab
-			fclose(f);
6717ab
-			if (ok)
6717ab
-				return 1;	/* found username/shell */
6717ab
-
6717ab
-			return 0;		/* ignore per-account files */
6717ab
-		}
6717ab
-
6717ab
-		/* Per-account setting */
6717ab
-		if (strlen(pwd->pw_dir) + sizeof(file) + 2 > sizeof(buf))
6717ab
-			continue;
6717ab
-		else {
6717ab
-			uid_t ruid = getuid();
6717ab
-			gid_t egid = getegid();
6717ab
-
6717ab
-			sprintf(buf, "%s/%s", pwd->pw_dir, file);
6717ab
-
6717ab
-			if (setregid(-1, pwd->pw_gid) == 0 &&
6717ab
-			    setreuid(0, pwd->pw_uid) == 0)
6717ab
-				ok = effective_access(buf, O_RDONLY) == 0;
6717ab
-
6717ab
-			if (setuid(0) != 0 ||
6717ab
-			    setreuid(ruid, 0) != 0 ||
6717ab
-			    setregid(-1, egid) != 0) {
6717ab
-				syslog(LOG_ALERT, _("hush login status: restore original IDs failed"));
6717ab
-				exit(EXIT_FAILURE);
6717ab
-			}
6717ab
-			if (ok)
6717ab
-				return 1;	/* enabled by user */
6717ab
-		}
6717ab
-	}
6717ab
-
6717ab
-	return 0;
6717ab
-}
6717ab
-
6717ab
-/*
6717ab
  * Detach the controlling terminal, fork, restore syslog stuff and create a new
6717ab
  * session.
6717ab
  */
6717ab
@@ -1372,7 +1254,7 @@ int main(int argc, char **argv)
6717ab
 
6717ab
 	endpwent();
6717ab
 
6717ab
-	cxt.quiet = get_hushlogin_status(pwd);
6717ab
+	cxt.quiet = get_hushlogin_status(pwd, 1);
6717ab
 
6717ab
 	log_utmp(&cxt);
6717ab
 	log_audit(&cxt, 1);
6717ab
diff -up util-linux-2.23.2/login-utils/logindefs.c.kzak util-linux-2.23.2/login-utils/logindefs.c
6717ab
--- util-linux-2.23.2/login-utils/logindefs.c.kzak	2013-06-13 09:46:10.442650810 +0200
6717ab
+++ util-linux-2.23.2/login-utils/logindefs.c	2014-09-25 14:48:20.088575426 +0200
6717ab
@@ -27,6 +27,9 @@
6717ab
 #include <stdlib.h>
6717ab
 #include <string.h>
6717ab
 #include <sys/syslog.h>
6717ab
+#include <sys/stat.h>
6717ab
+#include <sys/types.h>
6717ab
+#include <pwd.h>
6717ab
 
6717ab
 #include "c.h"
6717ab
 #include "closestream.h"
6717ab
@@ -259,6 +262,135 @@ int logindefs_setenv(const char *name, c
6717ab
 	return val ? setenv(name, val, 1) : -1;
6717ab
 }
6717ab
 
6717ab
+/*
6717ab
+ * We need to check the effective UID/GID. For example, $HOME could be on a
6717ab
+ * root-squashed NFS or on an NFS with UID mapping, and access(2) uses the
6717ab
+ * real UID/GID.  Then open(2) seems as the surest solution.
6717ab
+ * -- kzak@redhat.com (10-Apr-2009)
6717ab
+ */
6717ab
+int effective_access(const char *path, int mode)
6717ab
+{
6717ab
+	int fd = open(path, mode);
6717ab
+	if (fd != -1)
6717ab
+		close(fd);
6717ab
+	return fd == -1 ? -1 : 0;
6717ab
+}
6717ab
+
6717ab
+
6717ab
+/*
6717ab
+ * Check the per-account or the global hush-login setting.
6717ab
+ *
6717ab
+ * Hushed mode is enabled:
6717ab
+ *
6717ab
+ * a) if a global (e.g. /etc/hushlogins) hush file exists:
6717ab
+ *     1) for ALL ACCOUNTS if the file is empty
6717ab
+ *     2) for the current user if the username or shell is found in the file
6717ab
+ *
6717ab
+ * b) if a ~/.hushlogin file exists
6717ab
+ *
6717ab
+ * The ~/.hushlogin file is ignored if the global hush file exists.
6717ab
+ *
6717ab
+ * The HUSHLOGIN_FILE login.def variable overrides the default hush filename.
6717ab
+ *
6717ab
+ * Note that shadow-utils login(1) does not support "a1)". The "a1)" is
6717ab
+ * necessary if you want to use PAM for "Last login" message.
6717ab
+ *
6717ab
+ * -- Karel Zak <kzak@redhat.com> (26-Aug-2011)
6717ab
+ *
6717ab
+ *
6717ab
+ * The per-account check requires some explanation: As root we may not be able
6717ab
+ * to read the directory of the user if it is on an NFS-mounted filesystem. We
6717ab
+ * temporarily set our effective uid to the user-uid, making sure that we keep
6717ab
+ * root privileges in the real uid.
6717ab
+ *
6717ab
+ * A portable solution would require a fork(), but we rely on Linux having the
6717ab
+ * BSD setreuid().
6717ab
+ */
6717ab
+
6717ab
+int get_hushlogin_status(struct passwd *pwd, int force_check)
6717ab
+{
6717ab
+	const char *files[] = { _PATH_HUSHLOGINS, _PATH_HUSHLOGIN, NULL };
6717ab
+	const char *file;
6717ab
+	char buf[BUFSIZ];
6717ab
+	int i;
6717ab
+
6717ab
+	file = getlogindefs_str("HUSHLOGIN_FILE", NULL);
6717ab
+	if (file) {
6717ab
+		if (!*file)
6717ab
+			return 0;	/* empty HUSHLOGIN_FILE defined */
6717ab
+
6717ab
+		files[0] = file;
6717ab
+		files[1] = NULL;
6717ab
+	}
6717ab
+
6717ab
+	for (i = 0; files[i]; i++) {
6717ab
+		int ok = 0;
6717ab
+
6717ab
+		file = files[i];
6717ab
+
6717ab
+		/* global hush-file */
6717ab
+		if (*file == '/') {
6717ab
+			struct stat st;
6717ab
+			FILE *f;
6717ab
+
6717ab
+			if (stat(file, &st) != 0)
6717ab
+				continue;	/* file does not exist */
6717ab
+
6717ab
+			if (st.st_size == 0)
6717ab
+				return 1;	/* for all accounts */
6717ab
+
6717ab
+			f = fopen(file, "r");
6717ab
+			if (!f)
6717ab
+				continue;	/* ignore errors... */
6717ab
+
6717ab
+			while (ok == 0 && fgets(buf, sizeof(buf), f)) {
6717ab
+				buf[strlen(buf) - 1] = '\0';
6717ab
+				ok = !strcmp(buf, *buf == '/' ? pwd->pw_shell :
6717ab
+								pwd->pw_name);
6717ab
+			}
6717ab
+			fclose(f);
6717ab
+			if (ok)
6717ab
+				return 1;	/* found username/shell */
6717ab
+
6717ab
+			return 0;		/* ignore per-account files */
6717ab
+		}
6717ab
+
6717ab
+		/* per-account setting */
6717ab
+		if (strlen(pwd->pw_dir) + sizeof(file) + 2 > sizeof(buf))
6717ab
+			continue;
6717ab
+
6717ab
+		sprintf(buf, "%s/%s", pwd->pw_dir, file);
6717ab
+
6717ab
+		if (force_check) {
6717ab
+			uid_t ruid = getuid();
6717ab
+			gid_t egid = getegid();
6717ab
+
6717ab
+			if (setregid(-1, pwd->pw_gid) == 0 &&
6717ab
+			    setreuid(0, pwd->pw_uid) == 0)
6717ab
+				ok = effective_access(buf, O_RDONLY) == 0;
6717ab
+
6717ab
+			if (setuid(0) != 0 ||
6717ab
+			    setreuid(ruid, 0) != 0 ||
6717ab
+			    setregid(-1, egid) != 0) {
6717ab
+				syslog(LOG_ALERT, _("hush login status: restore original IDs failed"));
6717ab
+				exit(EXIT_FAILURE);
6717ab
+			}
6717ab
+			if (ok)
6717ab
+				return 1;	/* enabled by user */
6717ab
+		}
6717ab
+		else {
6717ab
+			int rc;
6717ab
+			rc = effective_access(buf, O_RDONLY);
6717ab
+			if (rc == 0)
6717ab
+				return 1;
6717ab
+			else if (rc == -1 && errno == EACCES)
6717ab
+					return -1;
6717ab
+		}
6717ab
+
6717ab
+	}
6717ab
+
6717ab
+	return 0;
6717ab
+}
6717ab
 #ifdef TEST_PROGRAM
6717ab
 int main(int argc, char *argv[])
6717ab
 {
6717ab
diff -up util-linux-2.23.2/login-utils/logindefs.h.kzak util-linux-2.23.2/login-utils/logindefs.h
6717ab
--- util-linux-2.23.2/login-utils/logindefs.h.kzak	2013-02-27 17:46:29.887020770 +0100
6717ab
+++ util-linux-2.23.2/login-utils/logindefs.h	2014-09-25 14:48:20.088575426 +0200
6717ab
@@ -8,5 +8,7 @@ extern unsigned long getlogindefs_num(co
6717ab
 extern const char *getlogindefs_str(const char *name, const char *dflt);
6717ab
 extern void free_getlogindefs_data(void);
6717ab
 extern int logindefs_setenv(const char *name, const char *conf, const char *dflt);
6717ab
+extern int effective_access(const char *path, int mode);
6717ab
+extern int get_hushlogin_status(struct passwd *pwd, int force_check);
6717ab
 
6717ab
 #endif /* UTIL_LINUX_LOGINDEFS_H */
6717ab
diff -up util-linux-2.23.2/login-utils/lslogins.1.kzak util-linux-2.23.2/login-utils/lslogins.1
6717ab
--- util-linux-2.23.2/login-utils/lslogins.1.kzak	2014-09-25 14:48:20.088575426 +0200
6717ab
+++ util-linux-2.23.2/login-utils/lslogins.1	2014-09-25 14:48:20.088575426 +0200
6717ab
@@ -0,0 +1,132 @@
6717ab
+.\" Copyright 2014 Ondrej Oprala (ondrej.oprala@gmail.com)
6717ab
+.\" May be distributed under the GNU General Public License
6717ab
+.TH LSLOGINS "1" "April 2014" "util-linux" "User Commands"
6717ab
+.SH NAME
6717ab
+lslogins \- display information about known users in the system
6717ab
+.SH SYNOPSIS
6717ab
+.B lslogins
6717ab
+[\fIoptions\fR] [\fB-s\fR|\fB-u\fR[=\fIUID\fR]] [\fB-g \fIgroups\fR] [\fB-l \fIlogins\fR]
6717ab
+.SH DESCRIPTION
6717ab
+.PP
6717ab
+Examine the wtmp and btmp logs, /etc/shadow (if necessary) and /etc/passwd
6717ab
+and output the desired data.
6717ab
+.PP
6717ab
+The default action is to list info about all the users in the system.
6717ab
+.SH OPTIONS
6717ab
+Mandatory arguments to long options are mandatory for short options too.
6717ab
+.TP
6717ab
+\fB\-a\fR, \fB\-\-acc\-expiration\fR
6717ab
+Display data about the date of last password change and the account expiration
6717ab
+date (see \fBshadow\fR(5) for more info).  (Requires root priviliges.)
6717ab
+.TP
6717ab
+\fB\-\-btmp\-file \fIpath\fP
6717ab
+Alternate path for btmp.
6717ab
+.TP
6717ab
+\fB\-c\fR, \fB\-\-colon\-separate\fR
6717ab
+Separate info about each user with a colon instead of a newline.
6717ab
+.TP
6717ab
+\fB\-e\fR, \fB\-\-export\fR
6717ab
+Output data in the format of NAME=VALUE.
6717ab
+.TP
6717ab
+\fB\-f\fR, \fB\-\-failed\fR
6717ab
+Display data about the users' last failed login attempts.
6717ab
+.TP
6717ab
+\fB\-G\fR, \fB\-\-groups\-info\fR
6717ab
+Show information about groups.
6717ab
+.TP
6717ab
+\fB\-g\fR, \fB\-\-groups\fR=\fIgroups\fR
6717ab
+Only show data of users belonging to \fIgroups\fR.  More than one group
6717ab
+may be specified; the list has to be comma-separated.
6717ab
+.TP
6717ab
+\fB\-h\fR, \fB\-\-help\fR
6717ab
+Display help information and exit.
6717ab
+.TP
6717ab
+\fB\-L\fR, \fB\-\-last\fR
6717ab
+Display data containing information about the users' last login sessions.
6717ab
+.TP
6717ab
+\fB\-l\fR, \fB\-\-logins\fR=\fIlogins\fR
6717ab
+Only show data of users with a login specified in \fIlogins\fR (user names or user
6717ab
+IDS).  More than one login may be specified; the list has to be comma-separated.
6717ab
+.TP
6717ab
+\fB\-m\fR, \fB\-\-supp\-groups\fR
6717ab
+Show supplementary groups.
6717ab
+.TP
6717ab
+\fB\-n\fR, \fB\-\-newline\fR
6717ab
+Display each piece of information on a separate line.
6717ab
+.TP
6717ab
+\fB\-\-noheadings\fR
6717ab
+Do not print a header line.
6717ab
+.TP
6717ab
+\fB\-\-notruncate\fR
6717ab
+Don't truncate output.
6717ab
+.TP
6717ab
+\fB\-o\fR, \fB\-\-output \fIlist\fP
6717ab
+Specify which output columns to print.  Use
6717ab
+.B \-\-help
6717ab
+to get a list of all supported columns.
6717ab
+.TP
6717ab
+\fB\-p\fR, \fB\-\-pwd\fR
6717ab
+Display information related to login by password (see also \fB\-afL).
6717ab
+.TP
6717ab
+\fB\-r\fR, \fB\-\-raw\fR
6717ab
+Raw output (no columnation).
6717ab
+.TP
6717ab
+\fB\-s\fR, \fB\-\-system\-accs\fR[=\fIthreshold\fR]
6717ab
+Show system accounts.  These are by default all accounts with a UID below 1000
6717ab
+(non-inclusive), with the exception of either nobody or nfsnobody (UID 65534).  The UID
6717ab
+threshold can also be specified explicitly (necessary for some distributions that
6717ab
+allocate UIDs starting from 100, 500 - or an entirely different value - rather than 1000).
6717ab
+.TP
6717ab
+\fB\-\-time-format\fR \fItype\fP
6717ab
+Display dates in short, full or iso format.  The default is short, this time
6717ab
+format is designed to be space efficient and human readable.
6717ab
+.TP
6717ab
+\fB\-u\fR, \fB\-\-user\-accs\fR[=\fIthreshold\fR]
6717ab
+Show user accounts.  These are by default all accounts with UID above 1000
6717ab
+(inclusive), with the exception of either nobody or nfsnobody (UID 65534).  The UID
6717ab
+threshold can also be specified explicitly (necessary for some distributions that
6717ab
+allocate UIDs starting from 100, 500 - or an entirely different value - rather than 1000).
6717ab
+.TP
6717ab
+\fB\-V\fR, \fB\-\-version\fR
6717ab
+Display version information and exit.
6717ab
+.TP
6717ab
+\fB\-\-wtmp\-file \fIpath\fP
6717ab
+Alternate path for wtmp.
6717ab
+.TP
6717ab
+\fB\-Z\fR, \fB\-\-context\fR
6717ab
+Display the users' security context.
6717ab
+.TP
6717ab
+\fB\-z\fR, \fB\-\-print0\fR
6717ab
+Delimit user entries with a nul character, instead of a newline.
6717ab
+
6717ab
+.SH NOTES
6717ab
+The default UID thresholds are read from /etc/login.defs.
6717ab
+
6717ab
+.SH EXIT STATUS
6717ab
+.TP
6717ab
+0
6717ab
+if OK,
6717ab
+.TP
6717ab
+1
6717ab
+if incorrect arguments specified,
6717ab
+.TP
6717ab
+2
6717ab
+if a serious error occurs (e.g. a corrupt log).
6717ab
+.SH SEE ALSO
6717ab
+\fBgroup\fP(5), \fBpasswd\fP(5), \fBshadow\fP(5), \fButmp\fP(5)
6717ab
+.SH HISTORY
6717ab
+The \fBlslogins\fP utility is inspired by the \fBlogins\fP utility, which first appeared in FreeBSD 4.10.
6717ab
+.SH AUTHORS
6717ab
+.MT ooprala@redhat.com
6717ab
+Ondrej Oprala
6717ab
+.ME
6717ab
+.br
6717ab
+.MT kzak@redhat.com
6717ab
+Karel Zak
6717ab
+.ME
6717ab
+
6717ab
+.SH AVAILABILITY
6717ab
+The lslogins command is part of the util-linux package and is available from
6717ab
+.UR ftp://\:ftp.kernel.org\:/pub\:/linux\:/utils\:/util-linux/
6717ab
+Linux Kernel Archive
6717ab
+.UE .
6717ab
diff -up util-linux-2.23.2/login-utils/lslogins.c.kzak util-linux-2.23.2/login-utils/lslogins.c
6717ab
--- util-linux-2.23.2/login-utils/lslogins.c.kzak	2014-09-25 14:48:20.089575436 +0200
6717ab
+++ util-linux-2.23.2/login-utils/lslogins.c	2014-09-25 14:48:20.089575436 +0200
6717ab
@@ -0,0 +1,1466 @@
6717ab
+/*
6717ab
+ * lslogins - List information about users on the system
6717ab
+ *
6717ab
+ * Copyright (C) 2014 Ondrej Oprala <ooprala@redhat.com>
6717ab
+ * Copyright (C) 2014 Karel Zak <kzak@redhat.com>
6717ab
+ *
6717ab
+ * This program is free software; you can redistribute it and/or modify
6717ab
+ * it under the terms of the GNU General Public License as published by
6717ab
+ * the Free Software Foundation; either version 2 of the License, or
6717ab
+ * (at your option) any later version.
6717ab
+ *
6717ab
+ * This program is distributed in the hope that it would be useful,
6717ab
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6717ab
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6717ab
+ * GNU General Public License for more details.
6717ab
+ *
6717ab
+ * You should have received a copy of the GNU General Public License along
6717ab
+ * with this program; if not, write to the Free Software Foundation, Inc.,
6717ab
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
6717ab
+ */
6717ab
+
6717ab
+#include <stdio.h>
6717ab
+#include <stdlib.h>
6717ab
+#include <unistd.h>
6717ab
+#include <getopt.h>
6717ab
+#include <sys/types.h>
6717ab
+#include <sys/stat.h>
6717ab
+#include <sys/syslog.h>
6717ab
+#include <pwd.h>
6717ab
+#include <grp.h>
6717ab
+#include <shadow.h>
6717ab
+#include <paths.h>
6717ab
+#include <time.h>
6717ab
+#include <utmp.h>
6717ab
+#include <signal.h>
6717ab
+#include <err.h>
6717ab
+#include <limits.h>
6717ab
+#include <ctype.h>
6717ab
+
6717ab
+#include <search.h>
6717ab
+
6717ab
+#include <libsmartcols.h>
6717ab
+#ifdef HAVE_LIBSELINUX
6717ab
+# include <selinux/selinux.h>
6717ab
+#endif
6717ab
+
6717ab
+#ifdef HAVE_LIBSYSTEMD
6717ab
+# include <systemd/sd-journal.h>
6717ab
+#endif
6717ab
+
6717ab
+#include "c.h"
6717ab
+#include "nls.h"
6717ab
+#include "closestream.h"
6717ab
+#include "xalloc.h"
6717ab
+#include "list.h"
6717ab
+#include "strutils.h"
6717ab
+#include "optutils.h"
6717ab
+#include "pathnames.h"
6717ab
+#include "logindefs.h"
6717ab
+#include "readutmp.h"
6717ab
+#include "procutils.h"
6717ab
+
6717ab
+/*
6717ab
+ * column description
6717ab
+ */
6717ab
+struct lslogins_coldesc {
6717ab
+	const char *name;
6717ab
+	const char *help;
6717ab
+	const char *pretty_name;
6717ab
+
6717ab
+	double whint;	/* width hint */
6717ab
+	long flag;
6717ab
+};
6717ab
+
6717ab
+static int lslogins_flag;
6717ab
+
6717ab
+#define UL_UID_MIN 1000
6717ab
+#define UL_UID_MAX 60000
6717ab
+#define UL_SYS_UID_MIN 201
6717ab
+#define UL_SYS_UID_MAX 999
6717ab
+
6717ab
+/* we use the value of outmode to determine
6717ab
+ * appropriate flags for the libsmartcols table
6717ab
+ * (e.g., a value of out_newline would imply a raw
6717ab
+ * table with the column separator set to '\n').
6717ab
+ */
6717ab
+static int outmode;
6717ab
+/*
6717ab
+ * output modes
6717ab
+ */
6717ab
+enum {
6717ab
+	OUT_COLON = 1,
6717ab
+	OUT_EXPORT,
6717ab
+	OUT_NEWLINE,
6717ab
+	OUT_RAW,
6717ab
+	OUT_NUL,
6717ab
+	OUT_PRETTY
6717ab
+};
6717ab
+
6717ab
+struct lslogins_user {
6717ab
+	char *login;
6717ab
+	uid_t uid;
6717ab
+	char *group;
6717ab
+	gid_t gid;
6717ab
+	char *gecos;
6717ab
+
6717ab
+	int pwd_empty;
6717ab
+	int nologin;
6717ab
+	int pwd_lock;
6717ab
+	int pwd_deny;
6717ab
+
6717ab
+	gid_t *sgroups;
6717ab
+	size_t nsgroups;
6717ab
+
6717ab
+	char *pwd_ctime;
6717ab
+	char *pwd_warn;
6717ab
+	char *pwd_expire;
6717ab
+	char *pwd_ctime_min;
6717ab
+	char *pwd_ctime_max;
6717ab
+
6717ab
+	char *last_login;
6717ab
+	char *last_tty;
6717ab
+	char *last_hostname;
6717ab
+
6717ab
+	char *failed_login;
6717ab
+	char *failed_tty;
6717ab
+
6717ab
+#ifdef HAVE_LIBSELINUX
6717ab
+	security_context_t context;
6717ab
+#endif
6717ab
+	char *homedir;
6717ab
+	char *shell;
6717ab
+	char *pwd_status;
6717ab
+	int   hushed;
6717ab
+	char *nprocs;
6717ab
+
6717ab
+};
6717ab
+
6717ab
+/*
6717ab
+ * time modes
6717ab
+ * */
6717ab
+enum {
6717ab
+	TIME_INVALID = 0,
6717ab
+	TIME_SHORT,
6717ab
+	TIME_FULL,
6717ab
+	TIME_ISO,
6717ab
+};
6717ab
+
6717ab
+/*
6717ab
+ * flags
6717ab
+ */
6717ab
+enum {
6717ab
+	F_SYSAC	= (1 << 3),
6717ab
+	F_USRAC	= (1 << 4),
6717ab
+};
6717ab
+
6717ab
+/*
6717ab
+ * IDs
6717ab
+ */
6717ab
+enum {
6717ab
+	COL_USER = 0,
6717ab
+	COL_UID,
6717ab
+	COL_GECOS,
6717ab
+	COL_HOME,
6717ab
+	COL_SHELL,
6717ab
+	COL_NOLOGIN,
6717ab
+	COL_PWDLOCK,
6717ab
+	COL_PWDEMPTY,
6717ab
+	COL_PWDDENY,
6717ab
+	COL_GROUP,
6717ab
+	COL_GID,
6717ab
+	COL_SGROUPS,
6717ab
+	COL_SGIDS,
6717ab
+	COL_LAST_LOGIN,
6717ab
+	COL_LAST_TTY,
6717ab
+	COL_LAST_HOSTNAME,
6717ab
+	COL_FAILED_LOGIN,
6717ab
+	COL_FAILED_TTY,
6717ab
+	COL_HUSH_STATUS,
6717ab
+	COL_PWD_WARN,
6717ab
+	COL_PWD_CTIME,
6717ab
+	COL_PWD_CTIME_MIN,
6717ab
+	COL_PWD_CTIME_MAX,
6717ab
+	COL_PWD_EXPIR,
6717ab
+	COL_SELINUX,
6717ab
+	COL_NPROCS,
6717ab
+};
6717ab
+
6717ab
+#define is_wtmp_col(x)	((x) == COL_LAST_LOGIN     || \
6717ab
+			 (x) == COL_LAST_TTY       || \
6717ab
+			 (x) == COL_LAST_HOSTNAME)
6717ab
+
6717ab
+#define is_btmp_col(x)	((x) == COL_FAILED_LOGIN   || \
6717ab
+			 (x) == COL_FAILED_TTY)
6717ab
+
6717ab
+enum {
6717ab
+	STATUS_FALSE = 0,
6717ab
+	STATUS_TRUE,
6717ab
+	STATUS_UNKNOWN
6717ab
+};
6717ab
+
6717ab
+static const char *const status[] = {
6717ab
+	[STATUS_FALSE]	= "0",
6717ab
+	[STATUS_TRUE]	= "1",
6717ab
+	[STATUS_UNKNOWN]= NULL
6717ab
+};
6717ab
+
6717ab
+static const char *const pretty_status[] = {
6717ab
+	[STATUS_FALSE]	= N_("no"),
6717ab
+	[STATUS_TRUE]	= N_("yes"),
6717ab
+	[STATUS_UNKNOWN]= NULL
6717ab
+};
6717ab
+
6717ab
+#define get_status(x)	(outmode == OUT_PRETTY ? pretty_status[(x)] : status[(x)])
6717ab
+
6717ab
+static const struct lslogins_coldesc coldescs[] =
6717ab
+{
6717ab
+	[COL_USER]          = { "USER",		N_("user name"), N_("Username"), 0.1, SCOLS_FL_NOEXTREMES },
6717ab
+	[COL_UID]           = { "UID",		N_("user ID"), "UID", 1, SCOLS_FL_RIGHT},
6717ab
+	[COL_PWDEMPTY]      = { "PWD-EMPTY",	N_("password not required"), N_("Password not required"), 1, SCOLS_FL_RIGHT },
6717ab
+	[COL_PWDDENY]       = { "PWD-DENY",	N_("login by password disabled"), N_("Login by password disabled"), 1, SCOLS_FL_RIGHT },
6717ab
+	[COL_PWDLOCK]       = { "PWD-LOCK",	N_("password defined, but locked"), N_("Password is locked"), 1, SCOLS_FL_RIGHT },
6717ab
+	[COL_NOLOGIN]       = { "NOLOGIN",	N_("log in disabled by nologin(8) or pam_nologin(8)"), N_("No login"), 1, SCOLS_FL_RIGHT },
6717ab
+	[COL_GROUP]         = { "GROUP",	N_("primary group name"), N_("Primary group"), 0.1 },
6717ab
+	[COL_GID]           = { "GID",		N_("primary group ID"), "GID", 1, SCOLS_FL_RIGHT },
6717ab
+	[COL_SGROUPS]       = { "SUPP-GROUPS",	N_("supplementary group names"), N_("Supplementary groups"), 0.1 },
6717ab
+	[COL_SGIDS]         = { "SUPP-GIDS",    N_("supplementary group IDs"), N_("Supplementary group IDs"), 0.1 },
6717ab
+	[COL_HOME]          = { "HOMEDIR",	N_("home directory"), N_("Home directory"), 0.1 },
6717ab
+	[COL_SHELL]         = { "SHELL",	N_("login shell"), N_("Shell"), 0.1 },
6717ab
+	[COL_GECOS]         = { "GECOS",	N_("full user name"), N_("Gecos field"), 0.1, SCOLS_FL_TRUNC },
6717ab
+	[COL_LAST_LOGIN]    = { "LAST-LOGIN",	N_("date of last login"), N_("Last login"), 0.1, SCOLS_FL_RIGHT },
6717ab
+	[COL_LAST_TTY]      = { "LAST-TTY",	N_("last tty used"), N_("Last terminal"), 0.05 },
6717ab
+	[COL_LAST_HOSTNAME] = { "LAST-HOSTNAME",N_("hostname during the last session"), N_("Last hostname"),  0.1},
6717ab
+	[COL_FAILED_LOGIN]  = { "FAILED-LOGIN",	N_("date of last failed login"), N_("Failed login"), 0.1 },
6717ab
+	[COL_FAILED_TTY]    = { "FAILED-TTY",	N_("where did the login fail?"), N_("Failed login terminal"), 0.05 },
6717ab
+	[COL_HUSH_STATUS]   = { "HUSHED",	N_("user's hush settings"), N_("Hushed"), 1, SCOLS_FL_RIGHT },
6717ab
+	[COL_PWD_WARN]      = { "PWD-WARN",	N_("days user is warned of password expiration"), N_("Password expiration warn interval"), 0.1, SCOLS_FL_RIGHT },
6717ab
+	[COL_PWD_EXPIR]     = { "PWD-EXPIR",	N_("password expiration date"), N_("Password expiration"), 0.1, SCOLS_FL_RIGHT },
6717ab
+	[COL_PWD_CTIME]     = { "PWD-CHANGE",	N_("date of last password change"), N_("Password changed"), 0.1, SCOLS_FL_RIGHT},
6717ab
+	[COL_PWD_CTIME_MIN] = { "PWD-MIN",	N_("number of days required between changes"), N_("Minimum change time"), 0.1, SCOLS_FL_RIGHT },
6717ab
+	[COL_PWD_CTIME_MAX] = { "PWD-MAX",	N_("max number of days a password may remain unchanged"), N_("Maximum change time"), 0.1, SCOLS_FL_RIGHT },
6717ab
+	[COL_SELINUX]       = { "CONTEXT",	N_("the user's security context"), N_("Selinux context"), 0.1 },
6717ab
+	[COL_NPROCS]        = { "PROC",         N_("number of processes run by the user"), N_("Running processes"), 1, SCOLS_FL_RIGHT },
6717ab
+};
6717ab
+
6717ab
+struct lslogins_control {
6717ab
+	struct utmp *wtmp;
6717ab
+	size_t wtmp_size;
6717ab
+
6717ab
+	struct utmp *btmp;
6717ab
+	size_t btmp_size;
6717ab
+
6717ab
+	void *usertree;
6717ab
+
6717ab
+	uid_t uid;
6717ab
+	uid_t UID_MIN;
6717ab
+	uid_t UID_MAX;
6717ab
+
6717ab
+	uid_t SYS_UID_MIN;
6717ab
+	uid_t SYS_UID_MAX;
6717ab
+
6717ab
+	char **ulist;
6717ab
+	size_t ulsiz;
6717ab
+
6717ab
+	unsigned int time_mode;
6717ab
+
6717ab
+	const char *journal_path;
6717ab
+
6717ab
+	unsigned int selinux_enabled : 1,
6717ab
+		     noheadings : 1,
6717ab
+		     notrunc : 1;
6717ab
+};
6717ab
+
6717ab
+/* these have to remain global since there's no other reasonable way to pass
6717ab
+ * them for each call of fill_table() via twalk() */
6717ab
+static struct libscols_table *tb;
6717ab
+
6717ab
+/* columns[] array specifies all currently wanted output column. The columns
6717ab
+ * are defined by coldescs[] array and you can specify (on command line) each
6717ab
+ * column twice. That's enough, dynamically allocated array of the columns is
6717ab
+ * unnecessary overkill and over-engineering in this case */
6717ab
+static int columns[ARRAY_SIZE(coldescs) * 2];
6717ab
+static int ncolumns;
6717ab
+
6717ab
+static inline size_t err_columns_index(size_t arysz, size_t idx)
6717ab
+{
6717ab
+	if (idx >= arysz)
6717ab
+		errx(EXIT_FAILURE, _("too many columns specified, "
6717ab
+				     "the limit is %zu columns"),
6717ab
+				arysz - 1);
6717ab
+	return idx;
6717ab
+}
6717ab
+
6717ab
+#define add_column(ary, n, id)	\
6717ab
+		((ary)[ err_columns_index(ARRAY_SIZE(ary), (n)) ] = (id))
6717ab
+
6717ab
+static struct timeval now;
6717ab
+
6717ab
+static int date_is_today(time_t t)
6717ab
+{
6717ab
+	if (now.tv_sec == 0)
6717ab
+		gettimeofday(&now, NULL);
6717ab
+	return t / (3600 * 24) == now.tv_sec / (3600 * 24);
6717ab
+}
6717ab
+
6717ab
+static int date_is_thisyear(time_t t)
6717ab
+{
6717ab
+	if (now.tv_sec == 0)
6717ab
+		gettimeofday(&now, NULL);
6717ab
+	return t / (3600 * 24 * 365) == now.tv_sec / (3600 * 24 * 365);
6717ab
+}
6717ab
+
6717ab
+static int column_name_to_id(const char *name, size_t namesz)
6717ab
+{
6717ab
+	size_t i;
6717ab
+
6717ab
+	for (i = 0; i < ARRAY_SIZE(coldescs); i++) {
6717ab
+		const char *cn = coldescs[i].name;
6717ab
+
6717ab
+		if (!strncasecmp(name, cn, namesz) && !*(cn + namesz))
6717ab
+			return i;
6717ab
+	}
6717ab
+	warnx(_("unknown column: %s"), name);
6717ab
+	return -1;
6717ab
+}
6717ab
+
6717ab
+static char *make_time(int mode, time_t time)
6717ab
+{
6717ab
+	char *s;
6717ab
+	struct tm tm;
6717ab
+	char buf[64] = {0};
6717ab
+
6717ab
+	localtime_r(&time, &tm;;
6717ab
+
6717ab
+	switch(mode) {
6717ab
+	case TIME_FULL:
6717ab
+		asctime_r(&tm, buf);
6717ab
+		if (*(s = buf + strlen(buf) - 1) == '\n')
6717ab
+			*s = '\0';
6717ab
+		break;
6717ab
+	case TIME_SHORT:
6717ab
+		if (date_is_today(time))
6717ab
+			strftime(buf, sizeof(buf), "%H:%M:%S", &tm;;
6717ab
+		else if (date_is_thisyear(time))
6717ab
+			strftime(buf, sizeof(buf), "%b%d/%H:%M", &tm;;
6717ab
+		else
6717ab
+			strftime(buf, sizeof(buf), "%Y-%b%d", &tm;;
6717ab
+		break;
6717ab
+	case TIME_ISO:
6717ab
+		strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S%z", &tm;;
6717ab
+		break;
6717ab
+	default:
6717ab
+		errx(EXIT_FAILURE, _("unsupported time type"));
6717ab
+	}
6717ab
+	return xstrdup(buf);
6717ab
+}
6717ab
+
6717ab
+
6717ab
+static char *uidtostr(uid_t uid)
6717ab
+{
6717ab
+	char *str_uid = NULL;
6717ab
+	xasprintf(&str_uid, "%u", uid);
6717ab
+	return str_uid;
6717ab
+}
6717ab
+
6717ab
+static char *gidtostr(gid_t gid)
6717ab
+{
6717ab
+	char *str_gid = NULL;
6717ab
+	xasprintf(&str_gid, "%u", gid);
6717ab
+	return str_gid;
6717ab
+}
6717ab
+
6717ab
+static char *build_sgroups_string(gid_t *sgroups, size_t nsgroups, int want_names)
6717ab
+{
6717ab
+	size_t n = 0, maxlen, len;
6717ab
+	char *res, *p;
6717ab
+
6717ab
+	if (!nsgroups)
6717ab
+		return NULL;
6717ab
+
6717ab
+	len = maxlen = nsgroups * 10;
6717ab
+	res = p = xmalloc(maxlen);
6717ab
+
6717ab
+	while (n < nsgroups) {
6717ab
+		int x;
6717ab
+again:
6717ab
+		if (!want_names)
6717ab
+			x = snprintf(p, len, "%u,", sgroups[n]);
6717ab
+		else {
6717ab
+			struct group *grp = getgrgid(sgroups[n]);
6717ab
+			if (!grp) {
6717ab
+				free(res);
6717ab
+				return NULL;
6717ab
+			}
6717ab
+			x = snprintf(p, len, "%s,", grp->gr_name);
6717ab
+		}
6717ab
+
6717ab
+		if (x < 0 || (size_t) x + 1 > len) {
6717ab
+			size_t cur = p - res;
6717ab
+
6717ab
+			maxlen *= 2;
6717ab
+			res = xrealloc(res, maxlen);
6717ab
+			p = res + cur;
6717ab
+			len = maxlen - cur;
6717ab
+			goto again;
6717ab
+		}
6717ab
+
6717ab
+		len -= x;
6717ab
+		p += x;
6717ab
+		++n;
6717ab
+	}
6717ab
+
6717ab
+	if (p > res)
6717ab
+		*(p - 1) = '\0';
6717ab
+
6717ab
+	return res;
6717ab
+}
6717ab
+
6717ab
+static struct utmp *get_last_wtmp(struct lslogins_control *ctl, const char *username)
6717ab
+{
6717ab
+	size_t n = 0;
6717ab
+	size_t len;
6717ab
+
6717ab
+	if (!username)
6717ab
+		return NULL;
6717ab
+
6717ab
+	len = strlen(username);
6717ab
+	n = ctl->wtmp_size - 1;
6717ab
+	do {
6717ab
+		if (!strncmp(username, ctl->wtmp[n].ut_user,
6717ab
+		    len < UT_NAMESIZE ? len : UT_NAMESIZE))
6717ab
+			return ctl->wtmp + n;
6717ab
+	} while (n--);
6717ab
+	return NULL;
6717ab
+
6717ab
+}
6717ab
+
6717ab
+static int require_wtmp(void)
6717ab
+{
6717ab
+	size_t i;
6717ab
+	for (i = 0; i < (size_t) ncolumns; i++)
6717ab
+		if (is_wtmp_col(columns[i]))
6717ab
+			return 1;
6717ab
+	return 0;
6717ab
+}
6717ab
+
6717ab
+static int require_btmp(void)
6717ab
+{
6717ab
+	size_t i;
6717ab
+	for (i = 0; i < (size_t) ncolumns; i++)
6717ab
+		if (is_btmp_col(columns[i]))
6717ab
+			return 1;
6717ab
+	return 0;
6717ab
+}
6717ab
+
6717ab
+static struct utmp *get_last_btmp(struct lslogins_control *ctl, const char *username)
6717ab
+{
6717ab
+	size_t n = 0;
6717ab
+	size_t len;
6717ab
+
6717ab
+	if (!username)
6717ab
+		return NULL;
6717ab
+
6717ab
+	len = strlen(username);
6717ab
+	n = ctl->btmp_size - 1;
6717ab
+	do {
6717ab
+		if (!strncmp(username, ctl->btmp[n].ut_user,
6717ab
+		    len < UT_NAMESIZE ? len : UT_NAMESIZE))
6717ab
+			return ctl->btmp + n;
6717ab
+	}while (n--);
6717ab
+	return NULL;
6717ab
+
6717ab
+}
6717ab
+
6717ab
+static int parse_wtmp(struct lslogins_control *ctl, char *path)
6717ab
+{
6717ab
+	int rc = 0;
6717ab
+
6717ab
+	rc = read_utmp(path, &ctl->wtmp_size, &ctl->wtmp);
6717ab
+	if (rc < 0 && errno != EACCES)
6717ab
+		err(EXIT_FAILURE, "%s", path);
6717ab
+	return rc;
6717ab
+}
6717ab
+
6717ab
+static int parse_btmp(struct lslogins_control *ctl, char *path)
6717ab
+{
6717ab
+	int rc = 0;
6717ab
+
6717ab
+	rc = read_utmp(path, &ctl->btmp_size, &ctl->btmp);
6717ab
+	if (rc < 0 && errno != EACCES)
6717ab
+		err(EXIT_FAILURE, "%s", path);
6717ab
+	return rc;
6717ab
+}
6717ab
+
6717ab
+static int get_sgroups(gid_t **list, size_t *len, struct passwd *pwd)
6717ab
+{
6717ab
+	size_t n = 0;
6717ab
+
6717ab
+	*len = 0;
6717ab
+	*list = NULL;
6717ab
+
6717ab
+	/* first let's get a supp. group count */
6717ab
+	getgrouplist(pwd->pw_name, pwd->pw_gid, *list, (int *) len);
6717ab
+	if (!*len)
6717ab
+		return -1;
6717ab
+
6717ab
+	*list = xcalloc(1, *len * sizeof(gid_t));
6717ab
+
6717ab
+	/* now for the actual list of GIDs */
6717ab
+	if (-1 == getgrouplist(pwd->pw_name, pwd->pw_gid, *list, (int *) len))
6717ab
+		return -1;
6717ab
+
6717ab
+	/* getgroups also returns the user's primary GID - dispose of it */
6717ab
+	while (n < *len) {
6717ab
+		if ((*list)[n] == pwd->pw_gid)
6717ab
+			break;
6717ab
+		++n;
6717ab
+	}
6717ab
+
6717ab
+	if (*len)
6717ab
+		(*list)[n] = (*list)[--(*len)];
6717ab
+	return 0;
6717ab
+}
6717ab
+
6717ab
+static int get_nprocs(const uid_t uid)
6717ab
+{
6717ab
+	int nprocs = 0;
6717ab
+	pid_t pid;
6717ab
+	struct proc_processes *proc = proc_open_processes();
6717ab
+
6717ab
+	proc_processes_filter_by_uid(proc, uid);
6717ab
+
6717ab
+	while (!proc_next_pid(proc, &pid))
6717ab
+		++nprocs;
6717ab
+
6717ab
+	proc_close_processes(proc);
6717ab
+	return nprocs;
6717ab
+}
6717ab
+
6717ab
+static int valid_pwd(const char *str)
6717ab
+{
6717ab
+	const char *p;
6717ab
+
6717ab
+	for (p = str; p && *p; p++)
6717ab
+		if (!isalnum((unsigned int) *p))
6717ab
+			return 0;
6717ab
+	return p > str ? 1 : 0;
6717ab
+}
6717ab
+
6717ab
+static struct lslogins_user *get_user_info(struct lslogins_control *ctl, const char *username)
6717ab
+{
6717ab
+	struct lslogins_user *user;
6717ab
+	struct passwd *pwd;
6717ab
+	struct group *grp;
6717ab
+	struct spwd *shadow;
6717ab
+	struct utmp *user_wtmp = NULL, *user_btmp = NULL;
6717ab
+	int n = 0;
6717ab
+	time_t time;
6717ab
+	uid_t uid;
6717ab
+	errno = 0;
6717ab
+
6717ab
+	pwd = username ? getpwnam(username) : getpwent();
6717ab
+	if (!pwd)
6717ab
+		return NULL;
6717ab
+
6717ab
+	ctl->uid = uid = pwd->pw_uid;
6717ab
+
6717ab
+	/* nfsnobody is an exception to the UID_MAX limit.  This is "nobody" on
6717ab
+	 * some systems; the decisive point is the UID - 65534 */
6717ab
+	if ((lslogins_flag & F_USRAC) &&
6717ab
+	    strcmp("nfsnobody", pwd->pw_name) != 0 &&
6717ab
+	    uid != 0) {
6717ab
+		if (uid < ctl->UID_MIN || uid > ctl->UID_MAX) {
6717ab
+			errno = EAGAIN;
6717ab
+			return NULL;
6717ab
+		}
6717ab
+
6717ab
+	} else if ((lslogins_flag & F_SYSAC) &&
6717ab
+		   (uid < ctl->SYS_UID_MIN || uid > ctl->SYS_UID_MAX)) {
6717ab
+		errno = EAGAIN;
6717ab
+		return NULL;
6717ab
+	}
6717ab
+
6717ab
+	user = xcalloc(1, sizeof(struct lslogins_user));
6717ab
+
6717ab
+	grp = getgrgid(pwd->pw_gid);
6717ab
+	if (!grp)
6717ab
+		return NULL;
6717ab
+
6717ab
+	if (ctl->wtmp)
6717ab
+		user_wtmp = get_last_wtmp(ctl, pwd->pw_name);
6717ab
+	if (ctl->btmp)
6717ab
+		user_btmp = get_last_btmp(ctl, pwd->pw_name);
6717ab
+
6717ab
+	lckpwdf();
6717ab
+	shadow = getspnam(pwd->pw_name);
6717ab
+	ulckpwdf();
6717ab
+
6717ab
+	/* required  by tseach() stuff */
6717ab
+	user->uid = pwd->pw_uid;
6717ab
+
6717ab
+	while (n < ncolumns) {
6717ab
+		switch (columns[n++]) {
6717ab
+		case COL_USER:
6717ab
+			user->login = xstrdup(pwd->pw_name);
6717ab
+			break;
6717ab
+		case COL_UID:
6717ab
+			user->uid = pwd->pw_uid;
6717ab
+			break;
6717ab
+		case COL_GROUP:
6717ab
+			user->group = xstrdup(grp->gr_name);
6717ab
+			break;
6717ab
+		case COL_GID:
6717ab
+			user->gid = pwd->pw_gid;
6717ab
+			break;
6717ab
+		case COL_SGROUPS:
6717ab
+		case COL_SGIDS:
6717ab
+			if (get_sgroups(&user->sgroups, &user->nsgroups, pwd))
6717ab
+				err(EXIT_FAILURE, _("failed to get supplementary groups"));
6717ab
+			break;
6717ab
+		case COL_HOME:
6717ab
+			user->homedir = xstrdup(pwd->pw_dir);
6717ab
+			break;
6717ab
+		case COL_SHELL:
6717ab
+			user->shell = xstrdup(pwd->pw_shell);
6717ab
+			break;
6717ab
+		case COL_GECOS:
6717ab
+			user->gecos = xstrdup(pwd->pw_gecos);
6717ab
+			break;
6717ab
+		case COL_LAST_LOGIN:
6717ab
+			if (user_wtmp) {
6717ab
+				time = user_wtmp->ut_tv.tv_sec;
6717ab
+				user->last_login = make_time(ctl->time_mode, time);
6717ab
+			}
6717ab
+			break;
6717ab
+		case COL_LAST_TTY:
6717ab
+			if (user_wtmp)
6717ab
+				user->last_tty = xstrdup(user_wtmp->ut_line);
6717ab
+			break;
6717ab
+		case COL_LAST_HOSTNAME:
6717ab
+			if (user_wtmp)
6717ab
+				user->last_hostname = xstrdup(user_wtmp->ut_host);
6717ab
+			break;
6717ab
+		case COL_FAILED_LOGIN:
6717ab
+			if (user_btmp) {
6717ab
+				time = user_btmp->ut_tv.tv_sec;
6717ab
+				user->failed_login = make_time(ctl->time_mode, time);
6717ab
+			}
6717ab
+			break;
6717ab
+		case COL_FAILED_TTY:
6717ab
+			if (user_btmp)
6717ab
+				user->failed_tty = xstrdup(user_btmp->ut_line);
6717ab
+			break;
6717ab
+		case COL_HUSH_STATUS:
6717ab
+			user->hushed = get_hushlogin_status(pwd, 0);
6717ab
+			if (user->hushed == -1)
6717ab
+				user->hushed = STATUS_UNKNOWN;
6717ab
+			break;
6717ab
+		case COL_PWDEMPTY:
6717ab
+			if (shadow) {
6717ab
+				if (!*shadow->sp_pwdp) /* '\0' */
6717ab
+					user->pwd_empty = STATUS_TRUE;
6717ab
+			} else
6717ab
+				user->pwd_empty = STATUS_UNKNOWN;
6717ab
+			break;
6717ab
+		case COL_PWDDENY:
6717ab
+			if (shadow) {
6717ab
+				if ((*shadow->sp_pwdp == '!' ||
6717ab
+				     *shadow->sp_pwdp == '*') &&
6717ab
+				    !valid_pwd(shadow->sp_pwdp + 1))
6717ab
+					user->pwd_deny = STATUS_TRUE;
6717ab
+			} else
6717ab
+				user->pwd_deny = STATUS_UNKNOWN;
6717ab
+			break;
6717ab
+
6717ab
+		case COL_PWDLOCK:
6717ab
+			if (shadow) {
6717ab
+				if (*shadow->sp_pwdp == '!' && valid_pwd(shadow->sp_pwdp + 1))
6717ab
+					user->pwd_lock = STATUS_TRUE;
6717ab
+			} else
6717ab
+				user->pwd_lock = STATUS_UNKNOWN;
6717ab
+			break;
6717ab
+		case COL_NOLOGIN:
6717ab
+			if (strstr(pwd->pw_shell, "nologin"))
6717ab
+				user->nologin = 1;
6717ab
+			else if (pwd->pw_uid)
6717ab
+				user->nologin = access("/etc/nologin", F_OK) == 0 ||
6717ab
+						access("/var/run/nologin", F_OK) == 0;
6717ab
+			break;
6717ab
+		case COL_PWD_WARN:
6717ab
+			if (shadow && shadow->sp_warn >= 0)
6717ab
+				xasprintf(&user->pwd_warn, "%ld", shadow->sp_warn);
6717ab
+			break;
6717ab
+		case COL_PWD_EXPIR:
6717ab
+			if (shadow && shadow->sp_expire >= 0)
6717ab
+				user->pwd_expire = make_time(TIME_SHORT,
6717ab
+						shadow->sp_expire * 86400);
6717ab
+			break;
6717ab
+		case COL_PWD_CTIME:
6717ab
+			/* sp_lstchg is specified in days, showing hours
6717ab
+			 * (especially in non-GMT timezones) would only serve
6717ab
+			 * to confuse */
6717ab
+			if (shadow)
6717ab
+				user->pwd_ctime = make_time(TIME_SHORT,
6717ab
+						shadow->sp_lstchg * 86400);
6717ab
+			break;
6717ab
+		case COL_PWD_CTIME_MIN:
6717ab
+			if (shadow && shadow->sp_min > 0)
6717ab
+				xasprintf(&user->pwd_ctime_min, "%ld", shadow->sp_min);
6717ab
+			break;
6717ab
+		case COL_PWD_CTIME_MAX:
6717ab
+			if (shadow && shadow->sp_max > 0)
6717ab
+				xasprintf(&user->pwd_ctime_max, "%ld", shadow->sp_max);
6717ab
+			break;
6717ab
+		case COL_SELINUX:
6717ab
+#ifdef HAVE_LIBSELINUX
6717ab
+			if (ctl->selinux_enabled) {
6717ab
+				/* typedefs and pointers are pure evil */
6717ab
+				security_context_t con = NULL;
6717ab
+				if (getcon(&con) == 0)
6717ab
+					user->context = con;
6717ab
+			}
6717ab
+#endif
6717ab
+			break;
6717ab
+		case COL_NPROCS:
6717ab
+			xasprintf(&user->nprocs, "%d", get_nprocs(pwd->pw_uid));
6717ab
+			break;
6717ab
+		default:
6717ab
+			/* something went very wrong here */
6717ab
+			err(EXIT_FAILURE, "fatal: unknown error");
6717ab
+			break;
6717ab
+		}
6717ab
+	}
6717ab
+
6717ab
+	return user;
6717ab
+}
6717ab
+
6717ab
+/* some UNIX implementations set errno iff a passwd/grp/...
6717ab
+ * entry was not found. The original UNIX logins(1) utility always
6717ab
+ * ignores invalid login/group names, so we're going to as well.*/
6717ab
+#define IS_REAL_ERRNO(e) !((e) == ENOENT || (e) == ESRCH || \
6717ab
+		(e) == EBADF || (e) == EPERM || (e) == EAGAIN)
6717ab
+
6717ab
+/* get a definitive list of users we want info about... */
6717ab
+
6717ab
+static int str_to_uint(char *s, unsigned int *ul)
6717ab
+{
6717ab
+	char *end;
6717ab
+	if (!s || !*s)
6717ab
+		return -1;
6717ab
+	*ul = strtoul(s, &end, 0);
6717ab
+	if (!*end)
6717ab
+		return 0;
6717ab
+	return 1;
6717ab
+}
6717ab
+
6717ab
+static int get_ulist(struct lslogins_control *ctl, char *logins, char *groups)
6717ab
+{
6717ab
+	char *u, *g;
6717ab
+	size_t i = 0, n = 0, *arsiz;
6717ab
+	struct group *grp;
6717ab
+	struct passwd *pwd;
6717ab
+	char ***ar;
6717ab
+	uid_t uid;
6717ab
+	gid_t gid;
6717ab
+
6717ab
+	ar = &ctl->ulist;
6717ab
+	arsiz = &ctl->ulsiz;
6717ab
+
6717ab
+	/* an arbitrary starting value */
6717ab
+	*arsiz = 32;
6717ab
+	*ar = xcalloc(1, sizeof(char *) * (*arsiz));
6717ab
+
6717ab
+	while ((u = strtok(logins, ","))) {
6717ab
+		logins = NULL;
6717ab
+
6717ab
+		/* user specified by UID? */
6717ab
+		if (!str_to_uint(u, &uid)) {
6717ab
+			pwd = getpwuid(uid);
6717ab
+			if (!pwd)
6717ab
+				continue;
6717ab
+			u = pwd->pw_name;
6717ab
+		}
6717ab
+		(*ar)[i++] = xstrdup(u);
6717ab
+
6717ab
+		if (i == *arsiz)
6717ab
+			*ar = xrealloc(*ar, sizeof(char *) * (*arsiz += 32));
6717ab
+	}
6717ab
+	/* FIXME: this might lead to duplicit entries, although not visible
6717ab
+	 * in output, crunching a user's info multiple times is very redundant */
6717ab
+	while ((g = strtok(groups, ","))) {
6717ab
+		groups = NULL;
6717ab
+
6717ab
+		/* user specified by GID? */
6717ab
+		if (!str_to_uint(g, &gid))
6717ab
+			grp = getgrgid(gid);
6717ab
+		else
6717ab
+			grp = getgrnam(g);
6717ab
+
6717ab
+		if (!grp)
6717ab
+			continue;
6717ab
+
6717ab
+		while ((u = grp->gr_mem[n++])) {
6717ab
+			(*ar)[i++] = xstrdup(u);
6717ab
+
6717ab
+			if (i == *arsiz)
6717ab
+				*ar = xrealloc(*ar, sizeof(char *) * (*arsiz += 32));
6717ab
+		}
6717ab
+	}
6717ab
+	*arsiz = i;
6717ab
+	return 0;
6717ab
+}
6717ab
+
6717ab
+static void free_ctl(struct lslogins_control *ctl)
6717ab
+{
6717ab
+	size_t n = 0;
6717ab
+
6717ab
+	free(ctl->wtmp);
6717ab
+	free(ctl->btmp);
6717ab
+
6717ab
+	while (n < ctl->ulsiz)
6717ab
+		free(ctl->ulist[n++]);
6717ab
+
6717ab
+	free(ctl->ulist);
6717ab
+	free(ctl);
6717ab
+}
6717ab
+
6717ab
+static struct lslogins_user *get_next_user(struct lslogins_control *ctl)
6717ab
+{
6717ab
+	struct lslogins_user *u;
6717ab
+	errno = 0;
6717ab
+	while (!(u = get_user_info(ctl, NULL))) {
6717ab
+		/* no "false" errno-s here, iff we're unable to
6717ab
+		 * get a valid user entry for any reason, quit */
6717ab
+		if (errno == EAGAIN)
6717ab
+			continue;
6717ab
+		return NULL;
6717ab
+	}
6717ab
+	return u;
6717ab
+}
6717ab
+
6717ab
+static int get_user(struct lslogins_control *ctl, struct lslogins_user **user,
6717ab
+		    const char *username)
6717ab
+{
6717ab
+	*user = get_user_info(ctl, username);
6717ab
+	if (!*user && errno)
6717ab
+		if (IS_REAL_ERRNO(errno))
6717ab
+			return -1;
6717ab
+	return 0;
6717ab
+}
6717ab
+
6717ab
+static int cmp_uid(const void *a, const void *b)
6717ab
+{
6717ab
+	uid_t x = ((struct lslogins_user *)a)->uid;
6717ab
+	uid_t z = ((struct lslogins_user *)b)->uid;
6717ab
+	return x > z ? 1 : (x < z ? -1 : 0);
6717ab
+}
6717ab
+
6717ab
+static int create_usertree(struct lslogins_control *ctl)
6717ab
+{
6717ab
+	struct lslogins_user *user = NULL;
6717ab
+	size_t n = 0;
6717ab
+
6717ab
+	if (*ctl->ulist) {
6717ab
+		while (n < ctl->ulsiz) {
6717ab
+			if (get_user(ctl, &user, ctl->ulist[n]))
6717ab
+				return -1;
6717ab
+			if (user) /* otherwise an invalid user name has probably been given */
6717ab
+				tsearch(user, &ctl->usertree, cmp_uid);
6717ab
+			++n;
6717ab
+		}
6717ab
+	} else {
6717ab
+		while ((user = get_next_user(ctl)))
6717ab
+			tsearch(user, &ctl->usertree, cmp_uid);
6717ab
+	}
6717ab
+	return 0;
6717ab
+}
6717ab
+
6717ab
+static struct libscols_table *setup_table(struct lslogins_control *ctl)
6717ab
+{
6717ab
+	struct libscols_table *tb = scols_new_table();
6717ab
+	int n = 0;
6717ab
+
6717ab
+	if (!tb)
6717ab
+		errx(EXIT_FAILURE, _("failed to initialize output table"));
6717ab
+	if (ctl->noheadings)
6717ab
+		scols_table_enable_noheadings(tb, 1);
6717ab
+
6717ab
+	switch(outmode) {
6717ab
+	case OUT_COLON:
6717ab
+		scols_table_enable_raw(tb, 1);
6717ab
+		scols_table_set_column_separator(tb, ":");
6717ab
+		break;
6717ab
+	case OUT_NEWLINE:
6717ab
+		scols_table_set_column_separator(tb, "\n");
6717ab
+		/* fallthrough */
6717ab
+	case OUT_EXPORT:
6717ab
+		scols_table_enable_export(tb, 1);
6717ab
+		break;
6717ab
+	case OUT_NUL:
6717ab
+		scols_table_set_line_separator(tb, "\0");
6717ab
+		/* fallthrough */
6717ab
+	case OUT_RAW:
6717ab
+		scols_table_enable_raw(tb, 1);
6717ab
+		break;
6717ab
+	case OUT_PRETTY:
6717ab
+		scols_table_enable_noheadings(tb, 1);
6717ab
+	default:
6717ab
+		break;
6717ab
+	}
6717ab
+
6717ab
+	while (n < ncolumns) {
6717ab
+		int flags = coldescs[columns[n]].flag;
6717ab
+
6717ab
+		if (ctl->notrunc)
6717ab
+			flags &= ~SCOLS_FL_TRUNC;
6717ab
+
6717ab
+		if (!scols_table_new_column(tb,
6717ab
+				coldescs[columns[n]].name,
6717ab
+				coldescs[columns[n]].whint,
6717ab
+				flags))
6717ab
+			goto fail;
6717ab
+		++n;
6717ab
+	}
6717ab
+
6717ab
+	return tb;
6717ab
+fail:
6717ab
+	scols_unref_table(tb);
6717ab
+	return NULL;
6717ab
+}
6717ab
+
6717ab
+static void fill_table(const void *u, const VISIT which, const int depth __attribute__((unused)))
6717ab
+{
6717ab
+	struct libscols_line *ln;
6717ab
+	struct lslogins_user *user = *(struct lslogins_user **)u;
6717ab
+	int n = 0;
6717ab
+
6717ab
+	if (which == preorder || which == endorder)
6717ab
+		return;
6717ab
+
6717ab
+	ln = scols_table_new_line(tb, NULL);
6717ab
+	while (n < ncolumns) {
6717ab
+		int rc = 0;
6717ab
+
6717ab
+		switch (columns[n]) {
6717ab
+		case COL_USER:
6717ab
+			rc = scols_line_set_data(ln, n, user->login);
6717ab
+			break;
6717ab
+		case COL_UID:
6717ab
+			rc = scols_line_refer_data(ln, n, uidtostr(user->uid));
6717ab
+			break;
6717ab
+		case COL_PWDEMPTY:
6717ab
+			rc = scols_line_set_data(ln, n, get_status(user->pwd_empty));
6717ab
+			break;
6717ab
+		case COL_NOLOGIN:
6717ab
+			rc = scols_line_set_data(ln, n, get_status(user->nologin));
6717ab
+			break;
6717ab
+		case COL_PWDLOCK:
6717ab
+			rc = scols_line_set_data(ln, n, get_status(user->pwd_lock));
6717ab
+			break;
6717ab
+		case COL_PWDDENY:
6717ab
+			rc = scols_line_set_data(ln, n, get_status(user->pwd_deny));
6717ab
+			break;
6717ab
+		case COL_GROUP:
6717ab
+			rc = scols_line_set_data(ln, n, user->group);
6717ab
+			break;
6717ab
+		case COL_GID:
6717ab
+			rc = scols_line_refer_data(ln, n, gidtostr(user->gid));
6717ab
+			break;
6717ab
+		case COL_SGROUPS:
6717ab
+			rc = scols_line_refer_data(ln, n,
6717ab
+				build_sgroups_string(user->sgroups,
6717ab
+						     user->nsgroups,
6717ab
+						     TRUE));
6717ab
+			break;
6717ab
+		case COL_SGIDS:
6717ab
+			rc = scols_line_refer_data(ln, n,
6717ab
+				build_sgroups_string(user->sgroups,
6717ab
+						     user->nsgroups,
6717ab
+						     FALSE));
6717ab
+			break;
6717ab
+		case COL_HOME:
6717ab
+			rc = scols_line_set_data(ln, n, user->homedir);
6717ab
+			break;
6717ab
+		case COL_SHELL:
6717ab
+			rc = scols_line_set_data(ln, n, user->shell);
6717ab
+			break;
6717ab
+		case COL_GECOS:
6717ab
+			rc = scols_line_set_data(ln, n, user->gecos);
6717ab
+			break;
6717ab
+		case COL_LAST_LOGIN:
6717ab
+			rc = scols_line_set_data(ln, n, user->last_login);
6717ab
+			break;
6717ab
+		case COL_LAST_TTY:
6717ab
+			rc = scols_line_set_data(ln, n, user->last_tty);
6717ab
+			break;
6717ab
+		case COL_LAST_HOSTNAME:
6717ab
+			rc = scols_line_set_data(ln, n, user->last_hostname);
6717ab
+			break;
6717ab
+		case COL_FAILED_LOGIN:
6717ab
+			rc = scols_line_set_data(ln, n, user->failed_login);
6717ab
+			break;
6717ab
+		case COL_FAILED_TTY:
6717ab
+			rc = scols_line_set_data(ln, n, user->failed_tty);
6717ab
+			break;
6717ab
+		case COL_HUSH_STATUS:
6717ab
+			rc = scols_line_set_data(ln, n, get_status(user->hushed));
6717ab
+			break;
6717ab
+		case COL_PWD_WARN:
6717ab
+			rc = scols_line_set_data(ln, n, user->pwd_warn);
6717ab
+			break;
6717ab
+		case COL_PWD_EXPIR:
6717ab
+			rc = scols_line_set_data(ln, n, user->pwd_expire);
6717ab
+			break;
6717ab
+		case COL_PWD_CTIME:
6717ab
+			rc = scols_line_set_data(ln, n, user->pwd_ctime);
6717ab
+			break;
6717ab
+		case COL_PWD_CTIME_MIN:
6717ab
+			rc = scols_line_set_data(ln, n, user->pwd_ctime_min);
6717ab
+			break;
6717ab
+		case COL_PWD_CTIME_MAX:
6717ab
+			rc = scols_line_set_data(ln, n, user->pwd_ctime_max);
6717ab
+			break;
6717ab
+		case COL_SELINUX:
6717ab
+#ifdef HAVE_LIBSELINUX
6717ab
+			rc = scols_line_set_data(ln, n, user->context);
6717ab
+#endif
6717ab
+			break;
6717ab
+		case COL_NPROCS:
6717ab
+			rc = scols_line_set_data(ln, n, user->nprocs);
6717ab
+			break;
6717ab
+		default:
6717ab
+			/* something went very wrong here */
6717ab
+			err(EXIT_FAILURE, _("internal error: unknown column"));
6717ab
+		}
6717ab
+
6717ab
+		if (rc != 0)
6717ab
+			err(EXIT_FAILURE, _("failed to set data"));
6717ab
+		++n;
6717ab
+	}
6717ab
+	return;
6717ab
+}
6717ab
+#ifdef HAVE_LIBSYSTEMD
6717ab
+static void print_journal_tail(const char *journal_path, uid_t uid, size_t len)
6717ab
+{
6717ab
+	sd_journal *j;
6717ab
+	char *match, *buf;
6717ab
+	uint64_t x;
6717ab
+	time_t t;
6717ab
+	const char *identifier, *pid, *message;
6717ab
+	size_t identifier_len, pid_len, message_len;
6717ab
+
6717ab
+	if (journal_path)
6717ab
+		sd_journal_open_directory(&j, journal_path, 0);
6717ab
+	else
6717ab
+		sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
6717ab
+
6717ab
+	buf = xmalloc(sizeof(char) * 16);
6717ab
+	xasprintf(&match, "_UID=%d", uid);
6717ab
+
6717ab
+	sd_journal_add_match(j, match, 0);
6717ab
+	sd_journal_seek_tail(j);
6717ab
+	sd_journal_previous_skip(j, len);
6717ab
+
6717ab
+	do {
6717ab
+		if (0 > sd_journal_get_data(j, "SYSLOG_IDENTIFIER",
6717ab
+				(const void **) &identifier, &identifier_len))
6717ab
+			return;
6717ab
+		if (0 > sd_journal_get_data(j, "_PID",
6717ab
+				(const void **) &pid, &pid_len))
6717ab
+			return;
6717ab
+		if (0 > sd_journal_get_data(j, "MESSAGE",
6717ab
+				(const void **) &message, &message_len))
6717ab
+			return;
6717ab
+
6717ab
+		sd_journal_get_realtime_usec(j, &x);
6717ab
+		t = x / 1000000;
6717ab
+		strftime(buf, 16, "%b %d %H:%M:%S", localtime(&t);;
6717ab
+
6717ab
+		fprintf(stdout, "%s", buf);
6717ab
+
6717ab
+		identifier = strchr(identifier, '=') + 1;
6717ab
+		pid = strchr(pid, '=') + 1		;
6717ab
+		message = strchr(message, '=') + 1;
6717ab
+
6717ab
+		fprintf(stdout, " %s", identifier);
6717ab
+		fprintf(stdout, "[%s]:", pid);
6717ab
+		fprintf(stdout, "%s\n", message);
6717ab
+	} while (sd_journal_next(j));
6717ab
+
6717ab
+	free(buf);
6717ab
+	free(match);
6717ab
+	sd_journal_flush_matches(j);
6717ab
+	sd_journal_close(j);
6717ab
+}
6717ab
+#endif
6717ab
+
6717ab
+static int print_pretty(struct libscols_table *tb)
6717ab
+{
6717ab
+	struct libscols_iter *itr = scols_new_iter(SCOLS_ITER_FORWARD);
6717ab
+	struct libscols_column *col;
6717ab
+	struct libscols_cell *data;
6717ab
+	struct libscols_line *ln;
6717ab
+	const char *hstr, *dstr;
6717ab
+	int n = 0;
6717ab
+
6717ab
+	ln = scols_table_get_line(tb, 0);
6717ab
+	while (!scols_table_next_column(tb, itr, &col)) {
6717ab
+
6717ab
+		data = scols_line_get_cell(ln, n);
6717ab
+
6717ab
+		hstr = _(coldescs[columns[n]].pretty_name);
6717ab
+		dstr = scols_cell_get_data(data);
6717ab
+
6717ab
+		if (dstr)
6717ab
+			printf("%s:%*c%-36s\n", hstr, 35 - (int)strlen(hstr), ' ', dstr);
6717ab
+		++n;
6717ab
+	}
6717ab
+
6717ab
+	scols_free_iter(itr);
6717ab
+	return 0;
6717ab
+
6717ab
+}
6717ab
+
6717ab
+static int print_user_table(struct lslogins_control *ctl)
6717ab
+{
6717ab
+	tb = setup_table(ctl);
6717ab
+	if (!tb)
6717ab
+		return -1;
6717ab
+
6717ab
+	twalk(ctl->usertree, fill_table);
6717ab
+	if (outmode == OUT_PRETTY) {
6717ab
+		print_pretty(tb);
6717ab
+#ifdef HAVE_LIBSYSTEMD
6717ab
+		fprintf(stdout, _("\nLast logs:\n"));
6717ab
+		print_journal_tail(ctl->journal_path, ctl->uid, 3);
6717ab
+		fputc('\n', stdout);
6717ab
+#endif
6717ab
+	} else
6717ab
+		scols_print_table(tb);
6717ab
+	return 0;
6717ab
+}
6717ab
+
6717ab
+static void free_user(void *f)
6717ab
+{
6717ab
+	struct lslogins_user *u = f;
6717ab
+	free(u->login);
6717ab
+	free(u->group);
6717ab
+	free(u->gecos);
6717ab
+	free(u->sgroups);
6717ab
+	free(u->pwd_ctime);
6717ab
+	free(u->pwd_warn);
6717ab
+	free(u->pwd_ctime_min);
6717ab
+	free(u->pwd_ctime_max);
6717ab
+	free(u->last_login);
6717ab
+	free(u->last_tty);
6717ab
+	free(u->last_hostname);
6717ab
+	free(u->failed_login);
6717ab
+	free(u->failed_tty);
6717ab
+	free(u->homedir);
6717ab
+	free(u->shell);
6717ab
+	free(u->pwd_status);
6717ab
+#ifdef HAVE_LIBSELINUX
6717ab
+	freecon(u->context);
6717ab
+#endif
6717ab
+	free(u);
6717ab
+}
6717ab
+
6717ab
+struct lslogins_timefmt {
6717ab
+	const char *name;
6717ab
+	int val;
6717ab
+};
6717ab
+
6717ab
+static struct lslogins_timefmt timefmts[] = {
6717ab
+	{ "short", TIME_SHORT },
6717ab
+	{ "full", TIME_FULL },
6717ab
+	{ "iso", TIME_ISO },
6717ab
+};
6717ab
+
6717ab
+static void __attribute__((__noreturn__)) usage(FILE *out)
6717ab
+{
6717ab
+	size_t i;
6717ab
+
6717ab
+	fputs(USAGE_HEADER, out);
6717ab
+	fprintf(out, _(" %s [options]\n"), program_invocation_short_name);
6717ab
+
6717ab
+	fputs(USAGE_OPTIONS, out);
6717ab
+	fputs(_(" -a, --acc-expiration     display info about passwords expiration\n"), out);
6717ab
+	fputs(_(" -c, --colon-separate     display data in a format similar to /etc/passwd\n"), out);
6717ab
+	fputs(_(" -e, --export             display in an export-able output format\n"), out);
6717ab
+	fputs(_(" -f, --failed             display data about the users' last failed logins\n"), out);
6717ab
+	fputs(_(" -G, --groups-info        display information about groups\n"), out);
6717ab
+	fputs(_(" -g, --groups=<groups>    display users belonging to a group in <groups>\n"), out);
6717ab
+	fputs(_(" -L, --last               show info about the users' last login sessions\n"), out);
6717ab
+	fputs(_(" -l, --logins=<logins>    display only users from <logins>\n"), out);
6717ab
+	fputs(_(" -m, --supp-groups        display supplementary groups as well\n"), out);
6717ab
+	fputs(_(" -n, --newline            display each piece of information on a new line\n"), out);
6717ab
+	fputs(_("     --noheadings         don't print headings\n"), out);
6717ab
+	fputs(_("     --notruncate         don't truncate output\n"), out);
6717ab
+	fputs(_(" -o, --output[=<list>]    define the columns to output\n"), out);
6717ab
+	fputs(_(" -p, --pwd                display information related to login by password.\n"), out);
6717ab
+	fputs(_(" -r, --raw                display in raw mode\n"), out);
6717ab
+	fputs(_(" -s, --system-accs        display system accounts\n"), out);
6717ab
+	fputs(_("     --time-format=<type> display dates in short, full or iso format\n"), out);
6717ab
+	fputs(_(" -u, --user-accs          display user accounts\n"), out);
6717ab
+	fputs(_(" -Z, --context            display SELinux contexts\n"), out);
6717ab
+	fputs(_(" -z, --print0             delimit user entries with a nul character\n"), out);
6717ab
+	fputs(_("     --wtmp-file <path>   set an alternate path for wtmp\n"), out);
6717ab
+	fputs(_("     --btmp-file <path>   set an alternate path for btmp\n"), out);
6717ab
+	fputs(USAGE_SEPARATOR, out);
6717ab
+	fputs(USAGE_HELP, out);
6717ab
+	fputs(USAGE_VERSION, out);
6717ab
+
6717ab
+	fprintf(out, _("\nAvailable columns:\n"));
6717ab
+
6717ab
+	for (i = 0; i < ARRAY_SIZE(coldescs); i++)
6717ab
+		fprintf(out, " %14s  %s\n", coldescs[i].name,
6717ab
+				_(coldescs[i].help));
6717ab
+
6717ab
+	fprintf(out, _("\nFor more details see lslogins(1).\n"));
6717ab
+
6717ab
+	exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
6717ab
+}
6717ab
+
6717ab
+int main(int argc, char *argv[])
6717ab
+{
6717ab
+	int c, opt_o = 0;
6717ab
+	char *logins = NULL, *groups = NULL;
6717ab
+	char *path_wtmp = _PATH_WTMP, *path_btmp = _PATH_BTMP;
6717ab
+	struct lslogins_control *ctl = xcalloc(1, sizeof(struct lslogins_control));
6717ab
+	size_t i;
6717ab
+
6717ab
+	/* long only options. */
6717ab
+	enum {
6717ab
+		OPT_VER = CHAR_MAX + 1,
6717ab
+		OPT_WTMP,
6717ab
+		OPT_BTMP,
6717ab
+		OPT_NOTRUNC,
6717ab
+		OPT_NOHEAD,
6717ab
+		OPT_TIME_FMT,
6717ab
+	};
6717ab
+
6717ab
+	static const struct option longopts[] = {
6717ab
+		{ "acc-expiration", no_argument,	0, 'a' },
6717ab
+		{ "colon-separate", no_argument,	0, 'c' },
6717ab
+		{ "export",         no_argument,	0, 'e' },
6717ab
+		{ "failed",         no_argument,	0, 'f' },
6717ab
+		{ "groups",         required_argument,	0, 'g' },
6717ab
+		{ "help",           no_argument,	0, 'h' },
6717ab
+		{ "logins",         required_argument,	0, 'l' },
6717ab
+		{ "supp-groups",    no_argument,	0, 'G' },
6717ab
+		{ "newline",        no_argument,	0, 'n' },
6717ab
+		{ "notruncate",     no_argument,	0, OPT_NOTRUNC },
6717ab
+		{ "noheadings",     no_argument,	0, OPT_NOHEAD },
6717ab
+		{ "output",         required_argument,	0, 'o' },
6717ab
+		{ "last",           no_argument,	0, 'L', },
6717ab
+		{ "raw",            no_argument,	0, 'r' },
6717ab
+		{ "system-accs",    no_argument,	0, 's' },
6717ab
+		{ "time-format",    required_argument,	0, OPT_TIME_FMT },
6717ab
+		{ "user-accs",      no_argument,	0, 'u' },
6717ab
+		{ "version",        no_argument,	0, 'V' },
6717ab
+		{ "pwd",            no_argument,	0, 'p' },
6717ab
+		{ "print0",         no_argument,	0, 'z' },
6717ab
+		{ "wtmp-file",      required_argument,	0, OPT_WTMP },
6717ab
+		{ "btmp-file",      required_argument,	0, OPT_BTMP },
6717ab
+#ifdef HAVE_LIBSELINUX
6717ab
+		{ "context",        no_argument,	0, 'Z' },
6717ab
+#endif
6717ab
+		{ NULL,             0, 			0,  0  }
6717ab
+	};
6717ab
+
6717ab
+	static const ul_excl_t excl[] = {	/* rows and cols in ASCII order */
6717ab
+		{ 'G', 'o' },
6717ab
+		{ 'L', 'o' },
6717ab
+		{ 'Z', 'o' },
6717ab
+		{ 'a', 'o' },
6717ab
+		{ 'c','n','r','z' },
6717ab
+		{ 'o', 'p' },
6717ab
+		{ 0 }
6717ab
+	};
6717ab
+	int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
6717ab
+
6717ab
+	setlocale(LC_ALL, "");
6717ab
+	bindtextdomain(PACKAGE, LOCALEDIR);
6717ab
+	textdomain(PACKAGE);
6717ab
+	atexit(close_stdout);
6717ab
+
6717ab
+	ctl->time_mode = TIME_SHORT;
6717ab
+
6717ab
+	/* very basic default */
6717ab
+	add_column(columns, ncolumns++, COL_UID);
6717ab
+	add_column(columns, ncolumns++, COL_USER);
6717ab
+
6717ab
+	while ((c = getopt_long(argc, argv, "acfGg:hLl:no:prsuVxzZ",
6717ab
+				longopts, NULL)) != -1) {
6717ab
+
6717ab
+		err_exclusive_options(c, longopts, excl, excl_st);
6717ab
+
6717ab
+		switch (c) {
6717ab
+		case 'a':
6717ab
+			add_column(columns, ncolumns++, COL_PWD_WARN);
6717ab
+			add_column(columns, ncolumns++, COL_PWD_CTIME_MIN);
6717ab
+			add_column(columns, ncolumns++, COL_PWD_CTIME_MAX);
6717ab
+			add_column(columns, ncolumns++, COL_PWD_CTIME);
6717ab
+			add_column(columns, ncolumns++, COL_PWD_EXPIR);
6717ab
+			break;
6717ab
+		case 'c':
6717ab
+			outmode = OUT_COLON;
6717ab
+			break;
6717ab
+		case 'e':
6717ab
+			outmode = OUT_EXPORT;
6717ab
+			break;
6717ab
+		case 'f':
6717ab
+			add_column(columns, ncolumns++, COL_FAILED_LOGIN);
6717ab
+			add_column(columns, ncolumns++, COL_FAILED_TTY);
6717ab
+			break;
6717ab
+		case 'G':
6717ab
+			add_column(columns, ncolumns++, COL_GID);
6717ab
+			add_column(columns, ncolumns++, COL_GROUP);
6717ab
+			add_column(columns, ncolumns++, COL_SGIDS);
6717ab
+			add_column(columns, ncolumns++, COL_SGROUPS);
6717ab
+			break;
6717ab
+		case 'g':
6717ab
+			groups = optarg;
6717ab
+			break;
6717ab
+		case 'h':
6717ab
+			usage(stdout);
6717ab
+			break;
6717ab
+		case 'L':
6717ab
+			add_column(columns, ncolumns++, COL_LAST_TTY);
6717ab
+			add_column(columns, ncolumns++, COL_LAST_HOSTNAME);
6717ab
+			add_column(columns, ncolumns++, COL_LAST_LOGIN);
6717ab
+			break;
6717ab
+		case 'l':
6717ab
+			logins = optarg;
6717ab
+			break;
6717ab
+		case 'n':
6717ab
+			outmode = OUT_NEWLINE;
6717ab
+			break;
6717ab
+		case 'o':
6717ab
+			if (optarg) {
6717ab
+				if (*optarg == '=')
6717ab
+					optarg++;
6717ab
+				ncolumns = string_to_idarray(optarg,
6717ab
+						columns, ARRAY_SIZE(columns),
6717ab
+						column_name_to_id);
6717ab
+				if (ncolumns < 0)
6717ab
+					return EXIT_FAILURE;
6717ab
+			}
6717ab
+			opt_o = 1;
6717ab
+			break;
6717ab
+		case 'r':
6717ab
+			outmode = OUT_RAW;
6717ab
+			break;
6717ab
+		case 's':
6717ab
+			ctl->SYS_UID_MIN = getlogindefs_num("SYS_UID_MIN", UL_SYS_UID_MIN);
6717ab
+			ctl->SYS_UID_MAX = getlogindefs_num("SYS_UID_MAX", UL_SYS_UID_MAX);
6717ab
+			lslogins_flag |= F_SYSAC;
6717ab
+			break;
6717ab
+		case 'u':
6717ab
+			ctl->UID_MIN = getlogindefs_num("UID_MIN", UL_UID_MIN);
6717ab
+			ctl->UID_MAX = getlogindefs_num("UID_MAX", UL_UID_MAX);
6717ab
+			lslogins_flag |= F_USRAC;
6717ab
+			break;
6717ab
+		case 'p':
6717ab
+			add_column(columns, ncolumns++, COL_PWDEMPTY);
6717ab
+			add_column(columns, ncolumns++, COL_PWDLOCK);
6717ab
+			add_column(columns, ncolumns++, COL_PWDDENY);
6717ab
+			add_column(columns, ncolumns++, COL_NOLOGIN);
6717ab
+			add_column(columns, ncolumns++, COL_HUSH_STATUS);
6717ab
+			break;
6717ab
+		case 'z':
6717ab
+			outmode = OUT_NUL;
6717ab
+			break;
6717ab
+		case OPT_WTMP:
6717ab
+			path_wtmp = optarg;
6717ab
+			break;
6717ab
+		case OPT_BTMP:
6717ab
+			path_btmp = optarg;
6717ab
+			break;
6717ab
+		case OPT_NOTRUNC:
6717ab
+			ctl->notrunc = 1;
6717ab
+			break;
6717ab
+		case OPT_NOHEAD:
6717ab
+			ctl->noheadings = 1;
6717ab
+			break;
6717ab
+		case OPT_TIME_FMT:
6717ab
+			{
6717ab
+				size_t i;
6717ab
+
6717ab
+				for (i = 0; i < ARRAY_SIZE(timefmts); i++) {
6717ab
+					if (strcmp(timefmts[i].name, optarg) == 0) {
6717ab
+						ctl->time_mode = timefmts[i].val;
6717ab
+						break;
6717ab
+					}
6717ab
+				}
6717ab
+				if (ctl->time_mode == TIME_INVALID)
6717ab
+					usage(stderr);
6717ab
+			}
6717ab
+			break;
6717ab
+		case 'V':
6717ab
+			printf(UTIL_LINUX_VERSION);
6717ab
+			return EXIT_SUCCESS;
6717ab
+		case 'Z':
6717ab
+		{
6717ab
+#ifdef HAVE_LIBSELINUX
6717ab
+			int sl = is_selinux_enabled();
6717ab
+			if (sl < 0)
6717ab
+				warn(_("failed to request selinux state"));
6717ab
+			else
6717ab
+				ctl->selinux_enabled = sl == 1;
6717ab
+#endif
6717ab
+			add_column(columns, ncolumns++, COL_SELINUX);
6717ab
+			break;
6717ab
+		}
6717ab
+		default:
6717ab
+			usage(stderr);
6717ab
+		}
6717ab
+	}
6717ab
+
6717ab
+	if (argc - optind == 1) {
6717ab
+		if (strchr(argv[optind], ','))
6717ab
+			errx(EXIT_FAILURE, _("Only one user may be specified. Use -l for multiple users."));
6717ab
+		logins = argv[optind];
6717ab
+		outmode = OUT_PRETTY;
6717ab
+	} else if (argc != optind)
6717ab
+		usage(stderr);
6717ab
+
6717ab
+	scols_init_debug(0);
6717ab
+
6717ab
+	/* lslogins -u -s == lslogins */
6717ab
+	if (lslogins_flag & F_USRAC && lslogins_flag & F_SYSAC)
6717ab
+		lslogins_flag &= ~(F_USRAC | F_SYSAC);
6717ab
+
6717ab
+	if (outmode == OUT_PRETTY && !opt_o) {
6717ab
+		/* all columns for lslogins <username> */
6717ab
+		for (ncolumns = 0, i = 0; i < ARRAY_SIZE(coldescs); i++)
6717ab
+			 columns[ncolumns++] = i;
6717ab
+
6717ab
+	} else if (ncolumns == 2 && !opt_o) {
6717ab
+		/* default colummns */
6717ab
+		add_column(columns, ncolumns++, COL_NPROCS);
6717ab
+		add_column(columns, ncolumns++, COL_PWDLOCK);
6717ab
+		add_column(columns, ncolumns++, COL_PWDDENY);
6717ab
+		add_column(columns, ncolumns++, COL_LAST_LOGIN);
6717ab
+		add_column(columns, ncolumns++, COL_GECOS);
6717ab
+	}
6717ab
+
6717ab
+	if (require_wtmp())
6717ab
+		parse_wtmp(ctl, path_wtmp);
6717ab
+	if (require_btmp())
6717ab
+		parse_btmp(ctl, path_btmp);
6717ab
+
6717ab
+	get_ulist(ctl, logins, groups);
6717ab
+
6717ab
+	if (create_usertree(ctl))
6717ab
+		return EXIT_FAILURE;
6717ab
+
6717ab
+	print_user_table(ctl);
6717ab
+
6717ab
+	scols_unref_table(tb);
6717ab
+	tdestroy(ctl->usertree, free_user);
6717ab
+	free_ctl(ctl);
6717ab
+
6717ab
+	return EXIT_SUCCESS;
6717ab
+}
6717ab
diff -up util-linux-2.23.2/login-utils/Makemodule.am.kzak util-linux-2.23.2/login-utils/Makemodule.am
6717ab
--- util-linux-2.23.2/login-utils/Makemodule.am.kzak	2013-06-13 09:46:10.441650801 +0200
6717ab
+++ util-linux-2.23.2/login-utils/Makemodule.am	2014-09-25 14:48:20.088575426 +0200
6717ab
@@ -145,6 +145,25 @@ endif
6717ab
 endif # BUILD_NEWGRP
6717ab
 
6717ab
 
6717ab
+if BUILD_LSLOGINS
6717ab
+usrbin_exec_PROGRAMS += lslogins
6717ab
+dist_man_MANS += login-utils/lslogins.1
6717ab
+lslogins_SOURCES = \
6717ab
+	login-utils/lslogins.c \
6717ab
+	login-utils/logindefs.c \
6717ab
+	login-utils/logindefs.h
6717ab
+lslogins_LDADD = $(LDADD) libcommon.la libsmartcols.la
6717ab
+lslogins_CFLAGS = $(AM_CFLAGS) -I$(ul_libsmartcols_incdir)
6717ab
+if HAVE_SELINUX
6717ab
+lslogins_LDADD += -lselinux
6717ab
+endif
6717ab
+if HAVE_SYSTEMD
6717ab
+lslogins_LDADD += $(SYSTEMD_LIBS) $(SYSTEMD_JOURNAL_LIBS)
6717ab
+lslogins_CFLAGS += $(SYSTEMD_CFLAGS) $(SYSTEMD_JOURNAL_CFLAGS)
6717ab
+endif
6717ab
+endif # BUILD_LSLOGINS
6717ab
+
6717ab
+
6717ab
 if BUILD_VIPW
6717ab
 usrsbin_exec_PROGRAMS += vipw
6717ab
 dist_man_MANS += \