2 * lslogins - List information about users on the system
4 * Copyright (C) 2014 Ondrej Oprala <ooprala@redhat.com>
5 * Copyright (C) 2014 Karel Zak <kzak@redhat.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it would be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <sys/types.h>
28 #include <sys/syslog.h>
40 #include <libsmartcols.h>
41 #ifdef HAVE_LIBSELINUX
42 # include <selinux/selinux.h>
45 #ifdef HAVE_LIBSYSTEMD
46 # include <systemd/sd-journal.h>
51 #include "closestream.h"
56 #include "pathnames.h"
57 #include "logindefs.h"
58 #include "procutils.h"
59 #include "timeutils.h"
64 struct lslogins_coldesc
{
67 const char *pretty_name
;
69 double whint
; /* width hint */
73 static int lslogins_flag
;
75 #define UL_UID_MIN 1000
76 #define UL_UID_MAX 60000
77 #define UL_SYS_UID_MIN 201
78 #define UL_SYS_UID_MAX 999
80 /* we use the value of outmode to determine
81 * appropriate flags for the libsmartcols table
82 * (e.g., a value of out_newline would imply a raw
83 * table with the column separator set to '\n').
98 struct lslogins_user
{
126 #ifdef HAVE_LIBSELINUX
127 security_context_t context
;
188 #define is_wtmp_col(x) ((x) == COL_LAST_LOGIN || \
189 (x) == COL_LAST_TTY || \
190 (x) == COL_LAST_HOSTNAME)
192 #define is_btmp_col(x) ((x) == COL_FAILED_LOGIN || \
193 (x) == COL_FAILED_TTY)
201 static const char *const status
[] = {
202 [STATUS_FALSE
] = "0",
204 [STATUS_UNKNOWN
]= NULL
207 static const char *const pretty_status
[] = {
208 [STATUS_FALSE
] = N_("no"),
209 [STATUS_TRUE
] = N_("yes"),
210 [STATUS_UNKNOWN
]= NULL
213 #define get_status(x) (outmode == OUT_PRETTY ? pretty_status[(x)] : status[(x)])
215 static const struct lslogins_coldesc coldescs
[] =
217 [COL_USER
] = { "USER", N_("user name"), N_("Username"), 0.1, SCOLS_FL_NOEXTREMES
},
218 [COL_UID
] = { "UID", N_("user ID"), "UID", 1, SCOLS_FL_RIGHT
},
219 [COL_PWDEMPTY
] = { "PWD-EMPTY", N_("password not required"), N_("Password not required"), 1, SCOLS_FL_RIGHT
},
220 [COL_PWDDENY
] = { "PWD-DENY", N_("login by password disabled"), N_("Login by password disabled"), 1, SCOLS_FL_RIGHT
},
221 [COL_PWDLOCK
] = { "PWD-LOCK", N_("password defined, but locked"), N_("Password is locked"), 1, SCOLS_FL_RIGHT
},
222 [COL_NOLOGIN
] = { "NOLOGIN", N_("log in disabled by nologin(8) or pam_nologin(8)"), N_("No login"), 1, SCOLS_FL_RIGHT
},
223 [COL_GROUP
] = { "GROUP", N_("primary group name"), N_("Primary group"), 0.1 },
224 [COL_GID
] = { "GID", N_("primary group ID"), "GID", 1, SCOLS_FL_RIGHT
},
225 [COL_SGROUPS
] = { "SUPP-GROUPS", N_("supplementary group names"), N_("Supplementary groups"), 0.1 },
226 [COL_SGIDS
] = { "SUPP-GIDS", N_("supplementary group IDs"), N_("Supplementary group IDs"), 0.1 },
227 [COL_HOME
] = { "HOMEDIR", N_("home directory"), N_("Home directory"), 0.1 },
228 [COL_SHELL
] = { "SHELL", N_("login shell"), N_("Shell"), 0.1 },
229 [COL_GECOS
] = { "GECOS", N_("full user name"), N_("Gecos field"), 0.1, SCOLS_FL_TRUNC
},
230 [COL_LAST_LOGIN
] = { "LAST-LOGIN", N_("date of last login"), N_("Last login"), 0.1, SCOLS_FL_RIGHT
},
231 [COL_LAST_TTY
] = { "LAST-TTY", N_("last tty used"), N_("Last terminal"), 0.05 },
232 [COL_LAST_HOSTNAME
] = { "LAST-HOSTNAME",N_("hostname during the last session"), N_("Last hostname"), 0.1},
233 [COL_FAILED_LOGIN
] = { "FAILED-LOGIN", N_("date of last failed login"), N_("Failed login"), 0.1 },
234 [COL_FAILED_TTY
] = { "FAILED-TTY", N_("where did the login fail?"), N_("Failed login terminal"), 0.05 },
235 [COL_HUSH_STATUS
] = { "HUSHED", N_("user's hush settings"), N_("Hushed"), 1, SCOLS_FL_RIGHT
},
236 [COL_PWD_WARN
] = { "PWD-WARN", N_("days user is warned of password expiration"), N_("Password expiration warn interval"), 0.1, SCOLS_FL_RIGHT
},
237 [COL_PWD_EXPIR
] = { "PWD-EXPIR", N_("password expiration date"), N_("Password expiration"), 0.1, SCOLS_FL_RIGHT
},
238 [COL_PWD_CTIME
] = { "PWD-CHANGE", N_("date of last password change"), N_("Password changed"), 0.1, SCOLS_FL_RIGHT
},
239 [COL_PWD_CTIME_MIN
] = { "PWD-MIN", N_("number of days required between changes"), N_("Minimum change time"), 0.1, SCOLS_FL_RIGHT
},
240 [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
},
241 [COL_SELINUX
] = { "CONTEXT", N_("the user's security context"), N_("Selinux context"), 0.1 },
242 [COL_NPROCS
] = { "PROC", N_("number of processes run by the user"), N_("Running processes"), 1, SCOLS_FL_RIGHT
},
245 struct lslogins_control
{
264 unsigned int time_mode
;
266 const char *journal_path
;
268 unsigned int selinux_enabled
: 1,
274 /* these have to remain global since there's no other reasonable way to pass
275 * them for each call of fill_table() via twalk() */
276 static struct libscols_table
*tb
;
278 /* columns[] array specifies all currently wanted output column. The columns
279 * are defined by coldescs[] array and you can specify (on command line) each
280 * column twice. That's enough, dynamically allocated array of the columns is
281 * unnecessary overkill and over-engineering in this case */
282 static int columns
[ARRAY_SIZE(coldescs
) * 2];
285 static inline size_t err_columns_index(size_t arysz
, size_t idx
)
288 errx(EXIT_FAILURE
, _("too many columns specified, "
289 "the limit is %zu columns"),
294 #define add_column(ary, n, id) \
295 ((ary)[ err_columns_index(ARRAY_SIZE(ary), (n)) ] = (id))
297 static struct timeval now
;
299 static int date_is_today(time_t t
)
302 gettimeofday(&now
, NULL
);
303 return t
/ (3600 * 24) == now
.tv_sec
/ (3600 * 24);
306 static int date_is_thisyear(time_t t
)
309 gettimeofday(&now
, NULL
);
310 return t
/ (3600 * 24 * 365) == now
.tv_sec
/ (3600 * 24 * 365);
313 static int column_name_to_id(const char *name
, size_t namesz
)
317 for (i
= 0; i
< ARRAY_SIZE(coldescs
); i
++) {
318 const char *cn
= coldescs
[i
].name
;
320 if (!strncasecmp(name
, cn
, namesz
) && !*(cn
+ namesz
))
323 warnx(_("unknown column: %s"), name
);
327 static char *make_time(int mode
, time_t time
)
333 localtime_r(&time
, &tm
);
338 if (*(s
= buf
+ strlen(buf
) - 1) == '\n')
342 if (date_is_today(time
))
343 strftime(buf
, sizeof(buf
), "%H:%M:%S", &tm
);
344 else if (date_is_thisyear(time
))
345 strftime(buf
, sizeof(buf
), "%b%d/%H:%M", &tm
);
347 strftime(buf
, sizeof(buf
), "%Y-%b%d", &tm
);
350 if (strtm_iso(&tm
, ISO_8601_DATE
|ISO_8601_TIME
|ISO_8601_TIMEZONE
,
351 buf
, sizeof(buf
)) != 0)
352 err(EXIT_FAILURE
, _("failed to formate ISO time"));
356 strftime(buf
, sizeof(buf
), "%Y-%m-%d", &tm
);
359 errx(EXIT_FAILURE
, _("unsupported time type"));
365 static char *uidtostr(uid_t uid
)
367 char *str_uid
= NULL
;
368 xasprintf(&str_uid
, "%u", uid
);
372 static char *gidtostr(gid_t gid
)
374 char *str_gid
= NULL
;
375 xasprintf(&str_gid
, "%u", gid
);
379 static char *build_sgroups_string(gid_t
*sgroups
, size_t nsgroups
, int want_names
)
381 size_t n
= 0, maxlen
, len
;
387 len
= maxlen
= nsgroups
* 10;
388 res
= p
= xmalloc(maxlen
);
390 while (n
< nsgroups
) {
394 x
= snprintf(p
, len
, "%u,", sgroups
[n
]);
396 struct group
*grp
= getgrgid(sgroups
[n
]);
401 x
= snprintf(p
, len
, "%s,", grp
->gr_name
);
404 if (x
< 0 || (size_t) x
>= len
) {
405 size_t cur
= p
- res
;
408 res
= xrealloc(res
, maxlen
);
425 static struct utmp
*get_last_wtmp(struct lslogins_control
*ctl
, const char *username
)
433 len
= strlen(username
);
434 n
= ctl
->wtmp_size
- 1;
436 if (!strncmp(username
, ctl
->wtmp
[n
].ut_user
,
437 len
< UT_NAMESIZE
? len
: UT_NAMESIZE
))
438 return ctl
->wtmp
+ n
;
444 static int require_wtmp(void)
447 for (i
= 0; i
< (size_t) ncolumns
; i
++)
448 if (is_wtmp_col(columns
[i
]))
453 static int require_btmp(void)
456 for (i
= 0; i
< (size_t) ncolumns
; i
++)
457 if (is_btmp_col(columns
[i
]))
462 static struct utmp
*get_last_btmp(struct lslogins_control
*ctl
, const char *username
)
470 len
= strlen(username
);
471 n
= ctl
->btmp_size
- 1;
473 if (!strncmp(username
, ctl
->btmp
[n
].ut_user
,
474 len
< UT_NAMESIZE
? len
: UT_NAMESIZE
))
475 return ctl
->btmp
+ n
;
481 static int read_utmp(char const *file
, size_t *nents
, struct utmp
**res
)
483 size_t n_read
= 0, n_alloc
= 0;
484 struct utmp
*utmp
= NULL
, *u
;
486 if (utmpname(file
) < 0)
492 while ((u
= getutent()) != NULL
) {
493 if (n_read
== n_alloc
) {
495 utmp
= xrealloc(utmp
, n_alloc
* sizeof (struct utmp
));
512 static int parse_wtmp(struct lslogins_control
*ctl
, char *path
)
516 rc
= read_utmp(path
, &ctl
->wtmp_size
, &ctl
->wtmp
);
517 if (rc
< 0 && errno
!= EACCES
)
518 err(EXIT_FAILURE
, "%s", path
);
522 static int parse_btmp(struct lslogins_control
*ctl
, char *path
)
526 rc
= read_utmp(path
, &ctl
->btmp_size
, &ctl
->btmp
);
527 if (rc
< 0 && errno
!= EACCES
)
528 err(EXIT_FAILURE
, "%s", path
);
532 static int get_sgroups(gid_t
**list
, size_t *len
, struct passwd
*pwd
)
540 /* first let's get a supp. group count */
541 getgrouplist(pwd
->pw_name
, pwd
->pw_gid
, *list
, &ngroups
);
545 *list
= xcalloc(1, ngroups
* sizeof(gid_t
));
547 /* now for the actual list of GIDs */
548 if (-1 == getgrouplist(pwd
->pw_name
, pwd
->pw_gid
, *list
, &ngroups
))
551 *len
= (size_t) ngroups
;
553 /* getgroups also returns the user's primary GID - dispose of it */
555 if ((*list
)[n
] == pwd
->pw_gid
)
561 (*list
)[n
] = (*list
)[--(*len
)];
566 static int get_nprocs(const uid_t uid
)
570 struct proc_processes
*proc
= proc_open_processes();
572 proc_processes_filter_by_uid(proc
, uid
);
574 while (!proc_next_pid(proc
, &pid
))
577 proc_close_processes(proc
);
581 static int valid_pwd(const char *str
)
585 for (p
= str
; p
&& *p
; p
++)
586 if (!isalnum((unsigned int) *p
))
588 return p
> str
? 1 : 0;
591 static struct lslogins_user
*get_user_info(struct lslogins_control
*ctl
, const char *username
)
593 struct lslogins_user
*user
;
597 struct utmp
*user_wtmp
= NULL
, *user_btmp
= NULL
;
603 pwd
= username
? getpwnam(username
) : getpwent();
607 ctl
->uid
= uid
= pwd
->pw_uid
;
609 /* nfsnobody is an exception to the UID_MAX limit. This is "nobody" on
610 * some systems; the decisive point is the UID - 65534 */
611 if ((lslogins_flag
& F_USRAC
) &&
612 strcmp("nfsnobody", pwd
->pw_name
) != 0 &&
614 if (uid
< ctl
->UID_MIN
|| uid
> ctl
->UID_MAX
) {
619 } else if ((lslogins_flag
& F_SYSAC
) &&
620 (uid
< ctl
->SYS_UID_MIN
|| uid
> ctl
->SYS_UID_MAX
)) {
625 user
= xcalloc(1, sizeof(struct lslogins_user
));
627 grp
= getgrgid(pwd
->pw_gid
);
632 user_wtmp
= get_last_wtmp(ctl
, pwd
->pw_name
);
634 user_btmp
= get_last_btmp(ctl
, pwd
->pw_name
);
637 shadow
= getspnam(pwd
->pw_name
);
640 /* required by tseach() stuff */
641 user
->uid
= pwd
->pw_uid
;
643 while (n
< ncolumns
) {
644 switch (columns
[n
++]) {
646 user
->login
= xstrdup(pwd
->pw_name
);
649 user
->uid
= pwd
->pw_uid
;
652 user
->group
= xstrdup(grp
->gr_name
);
655 user
->gid
= pwd
->pw_gid
;
659 if (get_sgroups(&user
->sgroups
, &user
->nsgroups
, pwd
))
660 err(EXIT_FAILURE
, _("failed to get supplementary groups"));
663 user
->homedir
= xstrdup(pwd
->pw_dir
);
666 user
->shell
= xstrdup(pwd
->pw_shell
);
669 user
->gecos
= xstrdup(pwd
->pw_gecos
);
673 time
= user_wtmp
->ut_tv
.tv_sec
;
674 user
->last_login
= make_time(ctl
->time_mode
, time
);
679 user
->last_tty
= xstrdup(user_wtmp
->ut_line
);
681 case COL_LAST_HOSTNAME
:
683 user
->last_hostname
= xstrdup(user_wtmp
->ut_host
);
685 case COL_FAILED_LOGIN
:
687 time
= user_btmp
->ut_tv
.tv_sec
;
688 user
->failed_login
= make_time(ctl
->time_mode
, time
);
693 user
->failed_tty
= xstrdup(user_btmp
->ut_line
);
695 case COL_HUSH_STATUS
:
696 user
->hushed
= get_hushlogin_status(pwd
, 0);
697 if (user
->hushed
== -1)
698 user
->hushed
= STATUS_UNKNOWN
;
702 if (!*shadow
->sp_pwdp
) /* '\0' */
703 user
->pwd_empty
= STATUS_TRUE
;
705 user
->pwd_empty
= STATUS_UNKNOWN
;
709 if ((*shadow
->sp_pwdp
== '!' ||
710 *shadow
->sp_pwdp
== '*') &&
711 !valid_pwd(shadow
->sp_pwdp
+ 1))
712 user
->pwd_deny
= STATUS_TRUE
;
714 user
->pwd_deny
= STATUS_UNKNOWN
;
719 if (*shadow
->sp_pwdp
== '!' && valid_pwd(shadow
->sp_pwdp
+ 1))
720 user
->pwd_lock
= STATUS_TRUE
;
722 user
->pwd_lock
= STATUS_UNKNOWN
;
725 if (strstr(pwd
->pw_shell
, "nologin"))
727 else if (pwd
->pw_uid
)
728 user
->nologin
= access(_PATH_NOLOGIN
, F_OK
) == 0 ||
729 access(_PATH_VAR_NOLOGIN
, F_OK
) == 0;
732 if (shadow
&& shadow
->sp_warn
>= 0)
733 xasprintf(&user
->pwd_warn
, "%ld", shadow
->sp_warn
);
736 if (shadow
&& shadow
->sp_expire
>= 0)
737 user
->pwd_expire
= make_time(ctl
->time_mode
== TIME_ISO
?
738 TIME_ISO_SHORT
: ctl
->time_mode
,
739 shadow
->sp_expire
* 86400);
742 /* sp_lstchg is specified in days, showing hours
743 * (especially in non-GMT timezones) would only serve
746 user
->pwd_ctime
= make_time(ctl
->time_mode
== TIME_ISO
?
747 TIME_ISO_SHORT
: ctl
->time_mode
,
748 shadow
->sp_lstchg
* 86400);
750 case COL_PWD_CTIME_MIN
:
751 if (shadow
&& shadow
->sp_min
> 0)
752 xasprintf(&user
->pwd_ctime_min
, "%ld", shadow
->sp_min
);
754 case COL_PWD_CTIME_MAX
:
755 if (shadow
&& shadow
->sp_max
> 0)
756 xasprintf(&user
->pwd_ctime_max
, "%ld", shadow
->sp_max
);
759 #ifdef HAVE_LIBSELINUX
760 if (ctl
->selinux_enabled
) {
761 /* typedefs and pointers are pure evil */
762 security_context_t con
= NULL
;
763 if (getcon(&con
) == 0)
769 xasprintf(&user
->nprocs
, "%d", get_nprocs(pwd
->pw_uid
));
772 /* something went very wrong here */
773 err(EXIT_FAILURE
, "fatal: unknown error");
781 /* some UNIX implementations set errno iff a passwd/grp/...
782 * entry was not found. The original UNIX logins(1) utility always
783 * ignores invalid login/group names, so we're going to as well.*/
784 #define IS_REAL_ERRNO(e) !((e) == ENOENT || (e) == ESRCH || \
785 (e) == EBADF || (e) == EPERM || (e) == EAGAIN)
787 /* get a definitive list of users we want info about... */
789 static int str_to_uint(char *s
, unsigned int *ul
)
794 *ul
= strtoul(s
, &end
, 0);
800 static int get_ulist(struct lslogins_control
*ctl
, char *logins
, char *groups
)
803 size_t i
= 0, n
= 0, *arsiz
;
813 /* an arbitrary starting value */
815 *ar
= xcalloc(1, sizeof(char *) * (*arsiz
));
818 while ((u
= strtok(logins
, ","))) {
821 /* user specified by UID? */
822 if (!str_to_uint(u
, &uid
)) {
828 (*ar
)[i
++] = xstrdup(u
);
831 *ar
= xrealloc(*ar
, sizeof(char *) * (*arsiz
+= 32));
837 /* FIXME: this might lead to duplicit entries, although not visible
838 * in output, crunching a user's info multiple times is very redundant */
839 while ((g
= strtok(groups
, ","))) {
843 /* user specified by GID? */
844 if (!str_to_uint(g
, &gid
))
852 while ((u
= grp
->gr_mem
[n
++])) {
853 (*ar
)[i
++] = xstrdup(u
);
856 *ar
= xrealloc(*ar
, sizeof(char *) * (*arsiz
+= 32));
865 static void free_ctl(struct lslogins_control
*ctl
)
872 while (n
< ctl
->ulsiz
)
873 free(ctl
->ulist
[n
++]);
879 static struct lslogins_user
*get_next_user(struct lslogins_control
*ctl
)
881 struct lslogins_user
*u
;
883 while (!(u
= get_user_info(ctl
, NULL
))) {
884 /* no "false" errno-s here, iff we're unable to
885 * get a valid user entry for any reason, quit */
893 static int get_user(struct lslogins_control
*ctl
, struct lslogins_user
**user
,
894 const char *username
)
896 *user
= get_user_info(ctl
, username
);
898 if (IS_REAL_ERRNO(errno
))
903 static int cmp_uid(const void *a
, const void *b
)
905 uid_t x
= ((struct lslogins_user
*)a
)->uid
;
906 uid_t z
= ((struct lslogins_user
*)b
)->uid
;
907 return x
> z
? 1 : (x
< z
? -1 : 0);
910 static int create_usertree(struct lslogins_control
*ctl
)
912 struct lslogins_user
*user
= NULL
;
916 while (n
< ctl
->ulsiz
) {
917 if (get_user(ctl
, &user
, ctl
->ulist
[n
]))
919 if (user
) /* otherwise an invalid user name has probably been given */
920 tsearch(user
, &ctl
->usertree
, cmp_uid
);
924 while ((user
= get_next_user(ctl
)))
925 tsearch(user
, &ctl
->usertree
, cmp_uid
);
930 static struct libscols_table
*setup_table(struct lslogins_control
*ctl
)
932 struct libscols_table
*table
= scols_new_table();
936 errx(EXIT_FAILURE
, _("failed to initialize output table"));
938 scols_table_enable_noheadings(table
, 1);
942 scols_table_enable_raw(table
, 1);
943 scols_table_set_column_separator(table
, ":");
946 scols_table_set_column_separator(table
, "\n");
949 scols_table_enable_export(table
, 1);
952 scols_table_set_line_separator(table
, "\0");
955 scols_table_enable_raw(table
, 1);
958 scols_table_enable_noheadings(table
, 1);
963 while (n
< ncolumns
) {
964 int flags
= coldescs
[columns
[n
]].flag
;
967 flags
&= ~SCOLS_FL_TRUNC
;
969 if (!scols_table_new_column(table
,
970 coldescs
[columns
[n
]].name
,
971 coldescs
[columns
[n
]].whint
,
979 scols_unref_table(table
);
983 static void fill_table(const void *u
, const VISIT which
, const int depth
__attribute__((unused
)))
985 struct libscols_line
*ln
;
986 struct lslogins_user
*user
= *(struct lslogins_user
**)u
;
989 if (which
== preorder
|| which
== endorder
)
992 ln
= scols_table_new_line(tb
, NULL
);
993 while (n
< ncolumns
) {
996 switch (columns
[n
]) {
998 rc
= scols_line_set_data(ln
, n
, user
->login
);
1001 rc
= scols_line_refer_data(ln
, n
, uidtostr(user
->uid
));
1004 rc
= scols_line_set_data(ln
, n
, get_status(user
->pwd_empty
));
1007 rc
= scols_line_set_data(ln
, n
, get_status(user
->nologin
));
1010 rc
= scols_line_set_data(ln
, n
, get_status(user
->pwd_lock
));
1013 rc
= scols_line_set_data(ln
, n
, get_status(user
->pwd_deny
));
1016 rc
= scols_line_set_data(ln
, n
, user
->group
);
1019 rc
= scols_line_refer_data(ln
, n
, gidtostr(user
->gid
));
1022 rc
= scols_line_refer_data(ln
, n
,
1023 build_sgroups_string(user
->sgroups
,
1028 rc
= scols_line_refer_data(ln
, n
,
1029 build_sgroups_string(user
->sgroups
,
1034 rc
= scols_line_set_data(ln
, n
, user
->homedir
);
1037 rc
= scols_line_set_data(ln
, n
, user
->shell
);
1040 rc
= scols_line_set_data(ln
, n
, user
->gecos
);
1042 case COL_LAST_LOGIN
:
1043 rc
= scols_line_set_data(ln
, n
, user
->last_login
);
1046 rc
= scols_line_set_data(ln
, n
, user
->last_tty
);
1048 case COL_LAST_HOSTNAME
:
1049 rc
= scols_line_set_data(ln
, n
, user
->last_hostname
);
1051 case COL_FAILED_LOGIN
:
1052 rc
= scols_line_set_data(ln
, n
, user
->failed_login
);
1054 case COL_FAILED_TTY
:
1055 rc
= scols_line_set_data(ln
, n
, user
->failed_tty
);
1057 case COL_HUSH_STATUS
:
1058 rc
= scols_line_set_data(ln
, n
, get_status(user
->hushed
));
1061 rc
= scols_line_set_data(ln
, n
, user
->pwd_warn
);
1064 rc
= scols_line_set_data(ln
, n
, user
->pwd_expire
);
1067 rc
= scols_line_set_data(ln
, n
, user
->pwd_ctime
);
1069 case COL_PWD_CTIME_MIN
:
1070 rc
= scols_line_set_data(ln
, n
, user
->pwd_ctime_min
);
1072 case COL_PWD_CTIME_MAX
:
1073 rc
= scols_line_set_data(ln
, n
, user
->pwd_ctime_max
);
1076 #ifdef HAVE_LIBSELINUX
1077 rc
= scols_line_set_data(ln
, n
, user
->context
);
1081 rc
= scols_line_set_data(ln
, n
, user
->nprocs
);
1084 /* something went very wrong here */
1085 err(EXIT_FAILURE
, _("internal error: unknown column"));
1089 err(EXIT_FAILURE
, _("failed to set data"));
1094 #ifdef HAVE_LIBSYSTEMD
1095 static void print_journal_tail(const char *journal_path
, uid_t uid
, size_t len
, int time_mode
)
1098 char *match
, *timestamp
;
1101 const char *identifier
, *pid
, *message
;
1102 size_t identifier_len
, pid_len
, message_len
;
1105 sd_journal_open_directory(&j
, journal_path
, 0);
1107 sd_journal_open(&j
, SD_JOURNAL_LOCAL_ONLY
);
1109 xasprintf(&match
, "_UID=%d", uid
);
1111 sd_journal_add_match(j
, match
, 0);
1112 sd_journal_seek_tail(j
);
1113 sd_journal_previous_skip(j
, len
);
1116 if (0 > sd_journal_get_data(j
, "SYSLOG_IDENTIFIER",
1117 (const void **) &identifier
, &identifier_len
))
1119 if (0 > sd_journal_get_data(j
, "_PID",
1120 (const void **) &pid
, &pid_len
))
1122 if (0 > sd_journal_get_data(j
, "MESSAGE",
1123 (const void **) &message
, &message_len
))
1126 sd_journal_get_realtime_usec(j
, &x
);
1128 timestamp
= make_time(time_mode
, t
);
1129 /* Get rid of journal entry field identifiers */
1130 identifier
= strchr(identifier
, '=') + 1;
1131 pid
= strchr(pid
, '=') + 1;
1132 message
= strchr(message
, '=') + 1;
1134 fprintf(stdout
, "%s %s[%s]: %s\n", timestamp
, identifier
, pid
,
1137 } while (sd_journal_next(j
));
1141 sd_journal_flush_matches(j
);
1142 sd_journal_close(j
);
1146 static int print_pretty(struct libscols_table
*table
)
1148 struct libscols_iter
*itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
1149 struct libscols_column
*col
;
1150 struct libscols_cell
*data
;
1151 struct libscols_line
*ln
;
1152 const char *hstr
, *dstr
;
1155 ln
= scols_table_get_line(table
, 0);
1156 while (!scols_table_next_column(table
, itr
, &col
)) {
1158 data
= scols_line_get_cell(ln
, n
);
1160 hstr
= _(coldescs
[columns
[n
]].pretty_name
);
1161 dstr
= scols_cell_get_data(data
);
1164 printf("%s:%*c%-36s\n", hstr
, 35 - (int)strlen(hstr
), ' ', dstr
);
1168 scols_free_iter(itr
);
1173 static int print_user_table(struct lslogins_control
*ctl
)
1175 tb
= setup_table(ctl
);
1179 twalk(ctl
->usertree
, fill_table
);
1180 if (outmode
== OUT_PRETTY
) {
1182 #ifdef HAVE_LIBSYSTEMD
1183 fprintf(stdout
, _("\nLast logs:\n"));
1184 print_journal_tail(ctl
->journal_path
, ctl
->uid
, 3, ctl
->time_mode
);
1185 fputc('\n', stdout
);
1188 scols_print_table(tb
);
1192 static void free_user(void *f
)
1194 struct lslogins_user
*u
= f
;
1201 free(u
->pwd_ctime_min
);
1202 free(u
->pwd_ctime_max
);
1203 free(u
->last_login
);
1205 free(u
->last_hostname
);
1206 free(u
->failed_login
);
1207 free(u
->failed_tty
);
1210 free(u
->pwd_status
);
1211 #ifdef HAVE_LIBSELINUX
1212 freecon(u
->context
);
1217 static int parse_time_mode(const char *optarg
)
1219 struct lslogins_timefmt
{
1223 static const struct lslogins_timefmt timefmts
[] = {
1225 {"full", TIME_FULL
},
1226 {"short", TIME_SHORT
},
1230 for (i
= 0; i
< ARRAY_SIZE(timefmts
); i
++) {
1231 if (strcmp(timefmts
[i
].name
, optarg
) == 0)
1232 return timefmts
[i
].val
;
1234 errx(EXIT_FAILURE
, _("unknown time format: %s"), optarg
);
1237 static void __attribute__((__noreturn__
)) usage(FILE *out
)
1241 fputs(USAGE_HEADER
, out
);
1242 fprintf(out
, _(" %s [options]\n"), program_invocation_short_name
);
1244 fputs(USAGE_SEPARATOR
, out
);
1245 fputs(_("Display information about known users in the system.\n"), out
);
1247 fputs(USAGE_OPTIONS
, out
);
1248 fputs(_(" -a, --acc-expiration display info about passwords expiration\n"), out
);
1249 fputs(_(" -c, --colon-separate display data in a format similar to /etc/passwd\n"), out
);
1250 fputs(_(" -e, --export display in an export-able output format\n"), out
);
1251 fputs(_(" -f, --failed display data about the users' last failed logins\n"), out
);
1252 fputs(_(" -G, --supp-groups display information about groups\n"), out
);
1253 fputs(_(" -g, --groups=<groups> display users belonging to a group in <groups>\n"), out
);
1254 fputs(_(" -L, --last show info about the users' last login sessions\n"), out
);
1255 fputs(_(" -l, --logins=<logins> display only users from <logins>\n"), out
);
1256 fputs(_(" -n, --newline display each piece of information on a new line\n"), out
);
1257 fputs(_(" --noheadings don't print headings\n"), out
);
1258 fputs(_(" --notruncate don't truncate output\n"), out
);
1259 fputs(_(" -o, --output[=<list>] define the columns to output\n"), out
);
1260 fputs(_(" -p, --pwd display information related to login by password.\n"), out
);
1261 fputs(_(" -r, --raw display in raw mode\n"), out
);
1262 fputs(_(" -s, --system-accs display system accounts\n"), out
);
1263 fputs(_(" --time-format=<type> display dates in short, full or iso format\n"), out
);
1264 fputs(_(" -u, --user-accs display user accounts\n"), out
);
1265 fputs(_(" -Z, --context display SELinux contexts\n"), out
);
1266 fputs(_(" -z, --print0 delimit user entries with a nul character\n"), out
);
1267 fputs(_(" --wtmp-file <path> set an alternate path for wtmp\n"), out
);
1268 fputs(_(" --btmp-file <path> set an alternate path for btmp\n"), out
);
1269 fputs(USAGE_SEPARATOR
, out
);
1270 fputs(USAGE_HELP
, out
);
1271 fputs(USAGE_VERSION
, out
);
1273 fprintf(out
, _("\nAvailable columns:\n"));
1275 for (i
= 0; i
< ARRAY_SIZE(coldescs
); i
++)
1276 fprintf(out
, " %14s %s\n", coldescs
[i
].name
,
1277 _(coldescs
[i
].help
));
1279 fprintf(out
, USAGE_MAN_TAIL("lslogins(1)"));
1281 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
1284 int main(int argc
, char *argv
[])
1287 char *logins
= NULL
, *groups
= NULL
;
1288 char *path_wtmp
= _PATH_WTMP
, *path_btmp
= _PATH_BTMP
;
1289 struct lslogins_control
*ctl
= xcalloc(1, sizeof(struct lslogins_control
));
1292 /* long only options. */
1294 OPT_WTMP
= CHAR_MAX
+ 1,
1301 static const struct option longopts
[] = {
1302 { "acc-expiration", no_argument
, 0, 'a' },
1303 { "colon-separate", no_argument
, 0, 'c' },
1304 { "export", no_argument
, 0, 'e' },
1305 { "failed", no_argument
, 0, 'f' },
1306 { "groups", required_argument
, 0, 'g' },
1307 { "help", no_argument
, 0, 'h' },
1308 { "logins", required_argument
, 0, 'l' },
1309 { "supp-groups", no_argument
, 0, 'G' },
1310 { "newline", no_argument
, 0, 'n' },
1311 { "notruncate", no_argument
, 0, OPT_NOTRUNC
},
1312 { "noheadings", no_argument
, 0, OPT_NOHEAD
},
1313 { "output", required_argument
, 0, 'o' },
1314 { "last", no_argument
, 0, 'L', },
1315 { "raw", no_argument
, 0, 'r' },
1316 { "system-accs", no_argument
, 0, 's' },
1317 { "time-format", required_argument
, 0, OPT_TIME_FMT
},
1318 { "user-accs", no_argument
, 0, 'u' },
1319 { "version", no_argument
, 0, 'V' },
1320 { "pwd", no_argument
, 0, 'p' },
1321 { "print0", no_argument
, 0, 'z' },
1322 { "wtmp-file", required_argument
, 0, OPT_WTMP
},
1323 { "btmp-file", required_argument
, 0, OPT_BTMP
},
1324 #ifdef HAVE_LIBSELINUX
1325 { "context", no_argument
, 0, 'Z' },
1330 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
1335 { 'c','n','r','z' },
1339 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
1341 setlocale(LC_ALL
, "");
1342 bindtextdomain(PACKAGE
, LOCALEDIR
);
1343 textdomain(PACKAGE
);
1344 atexit(close_stdout
);
1346 ctl
->time_mode
= TIME_SHORT
;
1348 /* very basic default */
1349 add_column(columns
, ncolumns
++, COL_UID
);
1350 add_column(columns
, ncolumns
++, COL_USER
);
1352 while ((c
= getopt_long(argc
, argv
, "acefGg:hLl:no:prsuVzZ",
1353 longopts
, NULL
)) != -1) {
1355 err_exclusive_options(c
, longopts
, excl
, excl_st
);
1359 add_column(columns
, ncolumns
++, COL_PWD_WARN
);
1360 add_column(columns
, ncolumns
++, COL_PWD_CTIME_MIN
);
1361 add_column(columns
, ncolumns
++, COL_PWD_CTIME_MAX
);
1362 add_column(columns
, ncolumns
++, COL_PWD_CTIME
);
1363 add_column(columns
, ncolumns
++, COL_PWD_EXPIR
);
1366 outmode
= OUT_COLON
;
1369 outmode
= OUT_EXPORT
;
1372 add_column(columns
, ncolumns
++, COL_FAILED_LOGIN
);
1373 add_column(columns
, ncolumns
++, COL_FAILED_TTY
);
1376 add_column(columns
, ncolumns
++, COL_GID
);
1377 add_column(columns
, ncolumns
++, COL_GROUP
);
1378 add_column(columns
, ncolumns
++, COL_SGIDS
);
1379 add_column(columns
, ncolumns
++, COL_SGROUPS
);
1388 add_column(columns
, ncolumns
++, COL_LAST_TTY
);
1389 add_column(columns
, ncolumns
++, COL_LAST_HOSTNAME
);
1390 add_column(columns
, ncolumns
++, COL_LAST_LOGIN
);
1396 outmode
= OUT_NEWLINE
;
1402 ncolumns
= string_to_idarray(optarg
,
1403 columns
, ARRAY_SIZE(columns
),
1406 return EXIT_FAILURE
;
1414 ctl
->SYS_UID_MIN
= getlogindefs_num("SYS_UID_MIN", UL_SYS_UID_MIN
);
1415 ctl
->SYS_UID_MAX
= getlogindefs_num("SYS_UID_MAX", UL_SYS_UID_MAX
);
1416 lslogins_flag
|= F_SYSAC
;
1419 ctl
->UID_MIN
= getlogindefs_num("UID_MIN", UL_UID_MIN
);
1420 ctl
->UID_MAX
= getlogindefs_num("UID_MAX", UL_UID_MAX
);
1421 lslogins_flag
|= F_USRAC
;
1424 add_column(columns
, ncolumns
++, COL_PWDEMPTY
);
1425 add_column(columns
, ncolumns
++, COL_PWDLOCK
);
1426 add_column(columns
, ncolumns
++, COL_PWDDENY
);
1427 add_column(columns
, ncolumns
++, COL_NOLOGIN
);
1428 add_column(columns
, ncolumns
++, COL_HUSH_STATUS
);
1443 ctl
->noheadings
= 1;
1446 ctl
->time_mode
= parse_time_mode(optarg
);
1449 printf(UTIL_LINUX_VERSION
);
1450 return EXIT_SUCCESS
;
1453 #ifdef HAVE_LIBSELINUX
1454 int sl
= is_selinux_enabled();
1456 warn(_("failed to request selinux state"));
1458 ctl
->selinux_enabled
= sl
== 1;
1460 add_column(columns
, ncolumns
++, COL_SELINUX
);
1468 if (argc
- optind
== 1) {
1469 if (strchr(argv
[optind
], ','))
1470 errx(EXIT_FAILURE
, _("Only one user may be specified. Use -l for multiple users."));
1471 logins
= argv
[optind
];
1472 outmode
= OUT_PRETTY
;
1473 } else if (argc
!= optind
)
1474 errx(EXIT_FAILURE
, _("Only one user may be specified. Use -l for multiple users."));
1476 scols_init_debug(0);
1478 /* lslogins -u -s == lslogins */
1479 if (lslogins_flag
& F_USRAC
&& lslogins_flag
& F_SYSAC
)
1480 lslogins_flag
&= ~(F_USRAC
| F_SYSAC
);
1482 if (outmode
== OUT_PRETTY
&& !opt_o
) {
1483 /* all columns for lslogins <username> */
1484 for (ncolumns
= 0, i
= 0; i
< ARRAY_SIZE(coldescs
); i
++)
1485 columns
[ncolumns
++] = i
;
1487 } else if (ncolumns
== 2 && !opt_o
) {
1488 /* default colummns */
1489 add_column(columns
, ncolumns
++, COL_NPROCS
);
1490 add_column(columns
, ncolumns
++, COL_PWDLOCK
);
1491 add_column(columns
, ncolumns
++, COL_PWDDENY
);
1492 add_column(columns
, ncolumns
++, COL_LAST_LOGIN
);
1493 add_column(columns
, ncolumns
++, COL_GECOS
);
1497 parse_wtmp(ctl
, path_wtmp
);
1499 parse_btmp(ctl
, path_btmp
);
1501 if (logins
|| groups
)
1502 get_ulist(ctl
, logins
, groups
);
1504 if (create_usertree(ctl
))
1505 return EXIT_FAILURE
;
1507 print_user_table(ctl
);
1509 scols_unref_table(tb
);
1510 tdestroy(ctl
->usertree
, free_user
);
1513 return EXIT_SUCCESS
;