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 int column_name_to_id(const char *name
, size_t namesz
)
301 for (i
= 0; i
< ARRAY_SIZE(coldescs
); i
++) {
302 const char *cn
= coldescs
[i
].name
;
304 if (!strncasecmp(name
, cn
, namesz
) && !*(cn
+ namesz
))
307 warnx(_("unknown column: %s"), name
);
311 static struct timeval now
;
313 static char *make_time(int mode
, time_t time
)
323 localtime_r(&time
, &tm
);
326 if (*(s
= buf
+ strlen(buf
) - 1) == '\n')
332 rc
= strtime_short(&time
, &now
, UL_SHORTTIME_THISYEAR_HHMM
,
336 rc
= strtime_iso(&time
, ISO_8601_DATE
|ISO_8601_TIME
|ISO_8601_TIMEZONE
,
340 rc
= strtime_iso(&time
, ISO_8601_DATE
, buf
, sizeof(buf
));
343 errx(EXIT_FAILURE
, _("unsupported time type"));
347 errx(EXIT_FAILURE
, _("failed to compose time string"));
353 static char *uidtostr(uid_t uid
)
355 char *str_uid
= NULL
;
356 xasprintf(&str_uid
, "%u", uid
);
360 static char *gidtostr(gid_t gid
)
362 char *str_gid
= NULL
;
363 xasprintf(&str_gid
, "%u", gid
);
367 static char *build_sgroups_string(gid_t
*sgroups
, size_t nsgroups
, int want_names
)
369 size_t n
= 0, maxlen
, len
;
375 len
= maxlen
= nsgroups
* 10;
376 res
= p
= xmalloc(maxlen
);
378 while (n
< nsgroups
) {
382 x
= snprintf(p
, len
, "%u,", sgroups
[n
]);
384 struct group
*grp
= getgrgid(sgroups
[n
]);
389 x
= snprintf(p
, len
, "%s,", grp
->gr_name
);
392 if (x
< 0 || (size_t) x
>= len
) {
393 size_t cur
= p
- res
;
396 res
= xrealloc(res
, maxlen
);
413 static struct utmp
*get_last_wtmp(struct lslogins_control
*ctl
, const char *username
)
421 len
= strlen(username
);
422 n
= ctl
->wtmp_size
- 1;
424 if (!strncmp(username
, ctl
->wtmp
[n
].ut_user
,
425 len
< UT_NAMESIZE
? len
: UT_NAMESIZE
))
426 return ctl
->wtmp
+ n
;
432 static int require_wtmp(void)
435 for (i
= 0; i
< (size_t) ncolumns
; i
++)
436 if (is_wtmp_col(columns
[i
]))
441 static int require_btmp(void)
444 for (i
= 0; i
< (size_t) ncolumns
; i
++)
445 if (is_btmp_col(columns
[i
]))
450 static struct utmp
*get_last_btmp(struct lslogins_control
*ctl
, const char *username
)
458 len
= strlen(username
);
459 n
= ctl
->btmp_size
- 1;
461 if (!strncmp(username
, ctl
->btmp
[n
].ut_user
,
462 len
< UT_NAMESIZE
? len
: UT_NAMESIZE
))
463 return ctl
->btmp
+ n
;
469 static int read_utmp(char const *file
, size_t *nents
, struct utmp
**res
)
471 size_t n_read
= 0, n_alloc
= 0;
472 struct utmp
*utmp
= NULL
, *u
;
474 if (utmpname(file
) < 0)
480 while ((u
= getutent()) != NULL
) {
481 if (n_read
== n_alloc
) {
483 utmp
= xrealloc(utmp
, n_alloc
* sizeof (struct utmp
));
500 static int parse_wtmp(struct lslogins_control
*ctl
, char *path
)
504 rc
= read_utmp(path
, &ctl
->wtmp_size
, &ctl
->wtmp
);
505 if (rc
< 0 && errno
!= EACCES
)
506 err(EXIT_FAILURE
, "%s", path
);
510 static int parse_btmp(struct lslogins_control
*ctl
, char *path
)
514 rc
= read_utmp(path
, &ctl
->btmp_size
, &ctl
->btmp
);
515 if (rc
< 0 && errno
!= EACCES
)
516 err(EXIT_FAILURE
, "%s", path
);
520 static int get_sgroups(gid_t
**list
, size_t *len
, struct passwd
*pwd
)
528 /* first let's get a supp. group count */
529 getgrouplist(pwd
->pw_name
, pwd
->pw_gid
, *list
, &ngroups
);
533 *list
= xcalloc(1, ngroups
* sizeof(gid_t
));
535 /* now for the actual list of GIDs */
536 if (-1 == getgrouplist(pwd
->pw_name
, pwd
->pw_gid
, *list
, &ngroups
))
539 *len
= (size_t) ngroups
;
541 /* getgroups also returns the user's primary GID - dispose of it */
543 if ((*list
)[n
] == pwd
->pw_gid
)
549 (*list
)[n
] = (*list
)[--(*len
)];
554 static int get_nprocs(const uid_t uid
)
558 struct proc_processes
*proc
= proc_open_processes();
560 proc_processes_filter_by_uid(proc
, uid
);
562 while (!proc_next_pid(proc
, &pid
))
565 proc_close_processes(proc
);
569 static int valid_pwd(const char *str
)
573 for (p
= str
; p
&& *p
; p
++)
574 if (!isalnum((unsigned int) *p
))
576 return p
> str
? 1 : 0;
579 static struct lslogins_user
*get_user_info(struct lslogins_control
*ctl
, const char *username
)
581 struct lslogins_user
*user
;
585 struct utmp
*user_wtmp
= NULL
, *user_btmp
= NULL
;
591 pwd
= username
? getpwnam(username
) : getpwent();
595 ctl
->uid
= uid
= pwd
->pw_uid
;
597 /* nfsnobody is an exception to the UID_MAX limit. This is "nobody" on
598 * some systems; the decisive point is the UID - 65534 */
599 if ((lslogins_flag
& F_USRAC
) &&
600 strcmp("nfsnobody", pwd
->pw_name
) != 0 &&
602 if (uid
< ctl
->UID_MIN
|| uid
> ctl
->UID_MAX
) {
607 } else if ((lslogins_flag
& F_SYSAC
) &&
608 (uid
< ctl
->SYS_UID_MIN
|| uid
> ctl
->SYS_UID_MAX
)) {
613 user
= xcalloc(1, sizeof(struct lslogins_user
));
615 grp
= getgrgid(pwd
->pw_gid
);
620 user_wtmp
= get_last_wtmp(ctl
, pwd
->pw_name
);
622 user_btmp
= get_last_btmp(ctl
, pwd
->pw_name
);
625 shadow
= getspnam(pwd
->pw_name
);
628 /* required by tseach() stuff */
629 user
->uid
= pwd
->pw_uid
;
631 while (n
< ncolumns
) {
632 switch (columns
[n
++]) {
634 user
->login
= xstrdup(pwd
->pw_name
);
637 user
->uid
= pwd
->pw_uid
;
640 user
->group
= xstrdup(grp
->gr_name
);
643 user
->gid
= pwd
->pw_gid
;
647 if (get_sgroups(&user
->sgroups
, &user
->nsgroups
, pwd
))
648 err(EXIT_FAILURE
, _("failed to get supplementary groups"));
651 user
->homedir
= xstrdup(pwd
->pw_dir
);
654 user
->shell
= xstrdup(pwd
->pw_shell
);
657 user
->gecos
= xstrdup(pwd
->pw_gecos
);
661 time
= user_wtmp
->ut_tv
.tv_sec
;
662 user
->last_login
= make_time(ctl
->time_mode
, time
);
667 user
->last_tty
= xstrdup(user_wtmp
->ut_line
);
669 case COL_LAST_HOSTNAME
:
671 user
->last_hostname
= xstrdup(user_wtmp
->ut_host
);
673 case COL_FAILED_LOGIN
:
675 time
= user_btmp
->ut_tv
.tv_sec
;
676 user
->failed_login
= make_time(ctl
->time_mode
, time
);
681 user
->failed_tty
= xstrdup(user_btmp
->ut_line
);
683 case COL_HUSH_STATUS
:
684 user
->hushed
= get_hushlogin_status(pwd
, 0);
685 if (user
->hushed
== -1)
686 user
->hushed
= STATUS_UNKNOWN
;
690 if (!*shadow
->sp_pwdp
) /* '\0' */
691 user
->pwd_empty
= STATUS_TRUE
;
693 user
->pwd_empty
= STATUS_UNKNOWN
;
697 if ((*shadow
->sp_pwdp
== '!' ||
698 *shadow
->sp_pwdp
== '*') &&
699 !valid_pwd(shadow
->sp_pwdp
+ 1))
700 user
->pwd_deny
= STATUS_TRUE
;
702 user
->pwd_deny
= STATUS_UNKNOWN
;
707 if (*shadow
->sp_pwdp
== '!' && valid_pwd(shadow
->sp_pwdp
+ 1))
708 user
->pwd_lock
= STATUS_TRUE
;
710 user
->pwd_lock
= STATUS_UNKNOWN
;
713 if (strstr(pwd
->pw_shell
, "nologin"))
715 else if (pwd
->pw_uid
)
716 user
->nologin
= access(_PATH_NOLOGIN
, F_OK
) == 0 ||
717 access(_PATH_VAR_NOLOGIN
, F_OK
) == 0;
720 if (shadow
&& shadow
->sp_warn
>= 0)
721 xasprintf(&user
->pwd_warn
, "%ld", shadow
->sp_warn
);
724 if (shadow
&& shadow
->sp_expire
>= 0)
725 user
->pwd_expire
= make_time(ctl
->time_mode
== TIME_ISO
?
726 TIME_ISO_SHORT
: ctl
->time_mode
,
727 shadow
->sp_expire
* 86400);
730 /* sp_lstchg is specified in days, showing hours
731 * (especially in non-GMT timezones) would only serve
734 user
->pwd_ctime
= make_time(ctl
->time_mode
== TIME_ISO
?
735 TIME_ISO_SHORT
: ctl
->time_mode
,
736 shadow
->sp_lstchg
* 86400);
738 case COL_PWD_CTIME_MIN
:
739 if (shadow
&& shadow
->sp_min
> 0)
740 xasprintf(&user
->pwd_ctime_min
, "%ld", shadow
->sp_min
);
742 case COL_PWD_CTIME_MAX
:
743 if (shadow
&& shadow
->sp_max
> 0)
744 xasprintf(&user
->pwd_ctime_max
, "%ld", shadow
->sp_max
);
747 #ifdef HAVE_LIBSELINUX
748 if (ctl
->selinux_enabled
) {
749 /* typedefs and pointers are pure evil */
750 security_context_t con
= NULL
;
751 if (getcon(&con
) == 0)
757 xasprintf(&user
->nprocs
, "%d", get_nprocs(pwd
->pw_uid
));
760 /* something went very wrong here */
761 err(EXIT_FAILURE
, "fatal: unknown error");
769 /* some UNIX implementations set errno iff a passwd/grp/...
770 * entry was not found. The original UNIX logins(1) utility always
771 * ignores invalid login/group names, so we're going to as well.*/
772 #define IS_REAL_ERRNO(e) !((e) == ENOENT || (e) == ESRCH || \
773 (e) == EBADF || (e) == EPERM || (e) == EAGAIN)
775 /* get a definitive list of users we want info about... */
777 static int str_to_uint(char *s
, unsigned int *ul
)
782 *ul
= strtoul(s
, &end
, 0);
788 static int get_ulist(struct lslogins_control
*ctl
, char *logins
, char *groups
)
791 size_t i
= 0, n
= 0, *arsiz
;
801 /* an arbitrary starting value */
803 *ar
= xcalloc(1, sizeof(char *) * (*arsiz
));
806 while ((u
= strtok(logins
, ","))) {
809 /* user specified by UID? */
810 if (!str_to_uint(u
, &uid
)) {
816 (*ar
)[i
++] = xstrdup(u
);
819 *ar
= xrealloc(*ar
, sizeof(char *) * (*arsiz
+= 32));
825 /* FIXME: this might lead to duplicate entries, although not visible
826 * in output, crunching a user's info multiple times is very redundant */
827 while ((g
= strtok(groups
, ","))) {
831 /* user specified by GID? */
832 if (!str_to_uint(g
, &gid
))
840 while ((u
= grp
->gr_mem
[n
++])) {
841 (*ar
)[i
++] = xstrdup(u
);
844 *ar
= xrealloc(*ar
, sizeof(char *) * (*arsiz
+= 32));
853 static void free_ctl(struct lslogins_control
*ctl
)
860 while (n
< ctl
->ulsiz
)
861 free(ctl
->ulist
[n
++]);
867 static struct lslogins_user
*get_next_user(struct lslogins_control
*ctl
)
869 struct lslogins_user
*u
;
871 while (!(u
= get_user_info(ctl
, NULL
))) {
872 /* no "false" errno-s here, iff we're unable to
873 * get a valid user entry for any reason, quit */
881 static int get_user(struct lslogins_control
*ctl
, struct lslogins_user
**user
,
882 const char *username
)
884 *user
= get_user_info(ctl
, username
);
886 if (IS_REAL_ERRNO(errno
))
891 static int cmp_uid(const void *a
, const void *b
)
893 uid_t x
= ((struct lslogins_user
*)a
)->uid
;
894 uid_t z
= ((struct lslogins_user
*)b
)->uid
;
895 return x
> z
? 1 : (x
< z
? -1 : 0);
898 static int create_usertree(struct lslogins_control
*ctl
)
900 struct lslogins_user
*user
= NULL
;
904 while (n
< ctl
->ulsiz
) {
905 if (get_user(ctl
, &user
, ctl
->ulist
[n
]))
907 if (user
) /* otherwise an invalid user name has probably been given */
908 tsearch(user
, &ctl
->usertree
, cmp_uid
);
912 while ((user
= get_next_user(ctl
)))
913 tsearch(user
, &ctl
->usertree
, cmp_uid
);
918 static struct libscols_table
*setup_table(struct lslogins_control
*ctl
)
920 struct libscols_table
*table
= scols_new_table();
924 errx(EXIT_FAILURE
, _("failed to initialize output table"));
926 scols_table_enable_noheadings(table
, 1);
930 scols_table_enable_raw(table
, 1);
931 scols_table_set_column_separator(table
, ":");
934 scols_table_set_column_separator(table
, "\n");
937 scols_table_enable_export(table
, 1);
940 scols_table_set_line_separator(table
, "\0");
943 scols_table_enable_raw(table
, 1);
946 scols_table_enable_noheadings(table
, 1);
951 while (n
< ncolumns
) {
952 int flags
= coldescs
[columns
[n
]].flag
;
955 flags
&= ~SCOLS_FL_TRUNC
;
957 if (!scols_table_new_column(table
,
958 coldescs
[columns
[n
]].name
,
959 coldescs
[columns
[n
]].whint
,
967 scols_unref_table(table
);
971 static void fill_table(const void *u
, const VISIT which
, const int depth
__attribute__((unused
)))
973 struct libscols_line
*ln
;
974 struct lslogins_user
*user
= *(struct lslogins_user
**)u
;
977 if (which
== preorder
|| which
== endorder
)
980 ln
= scols_table_new_line(tb
, NULL
);
981 while (n
< ncolumns
) {
984 switch (columns
[n
]) {
986 rc
= scols_line_set_data(ln
, n
, user
->login
);
989 rc
= scols_line_refer_data(ln
, n
, uidtostr(user
->uid
));
992 rc
= scols_line_set_data(ln
, n
, get_status(user
->pwd_empty
));
995 rc
= scols_line_set_data(ln
, n
, get_status(user
->nologin
));
998 rc
= scols_line_set_data(ln
, n
, get_status(user
->pwd_lock
));
1001 rc
= scols_line_set_data(ln
, n
, get_status(user
->pwd_deny
));
1004 rc
= scols_line_set_data(ln
, n
, user
->group
);
1007 rc
= scols_line_refer_data(ln
, n
, gidtostr(user
->gid
));
1010 rc
= scols_line_refer_data(ln
, n
,
1011 build_sgroups_string(user
->sgroups
,
1016 rc
= scols_line_refer_data(ln
, n
,
1017 build_sgroups_string(user
->sgroups
,
1022 rc
= scols_line_set_data(ln
, n
, user
->homedir
);
1025 rc
= scols_line_set_data(ln
, n
, user
->shell
);
1028 rc
= scols_line_set_data(ln
, n
, user
->gecos
);
1030 case COL_LAST_LOGIN
:
1031 rc
= scols_line_set_data(ln
, n
, user
->last_login
);
1034 rc
= scols_line_set_data(ln
, n
, user
->last_tty
);
1036 case COL_LAST_HOSTNAME
:
1037 rc
= scols_line_set_data(ln
, n
, user
->last_hostname
);
1039 case COL_FAILED_LOGIN
:
1040 rc
= scols_line_set_data(ln
, n
, user
->failed_login
);
1042 case COL_FAILED_TTY
:
1043 rc
= scols_line_set_data(ln
, n
, user
->failed_tty
);
1045 case COL_HUSH_STATUS
:
1046 rc
= scols_line_set_data(ln
, n
, get_status(user
->hushed
));
1049 rc
= scols_line_set_data(ln
, n
, user
->pwd_warn
);
1052 rc
= scols_line_set_data(ln
, n
, user
->pwd_expire
);
1055 rc
= scols_line_set_data(ln
, n
, user
->pwd_ctime
);
1057 case COL_PWD_CTIME_MIN
:
1058 rc
= scols_line_set_data(ln
, n
, user
->pwd_ctime_min
);
1060 case COL_PWD_CTIME_MAX
:
1061 rc
= scols_line_set_data(ln
, n
, user
->pwd_ctime_max
);
1064 #ifdef HAVE_LIBSELINUX
1065 rc
= scols_line_set_data(ln
, n
, user
->context
);
1069 rc
= scols_line_set_data(ln
, n
, user
->nprocs
);
1072 /* something went very wrong here */
1073 err(EXIT_FAILURE
, _("internal error: unknown column"));
1077 err(EXIT_FAILURE
, _("failed to set data"));
1082 #ifdef HAVE_LIBSYSTEMD
1083 static void print_journal_tail(const char *journal_path
, uid_t uid
, size_t len
, int time_mode
)
1086 char *match
, *timestamp
;
1089 const char *identifier
, *pid
, *message
;
1090 size_t identifier_len
, pid_len
, message_len
;
1093 sd_journal_open_directory(&j
, journal_path
, 0);
1095 sd_journal_open(&j
, SD_JOURNAL_LOCAL_ONLY
);
1097 xasprintf(&match
, "_UID=%d", uid
);
1099 sd_journal_add_match(j
, match
, 0);
1100 sd_journal_seek_tail(j
);
1101 sd_journal_previous_skip(j
, len
);
1104 if (0 > sd_journal_get_data(j
, "SYSLOG_IDENTIFIER",
1105 (const void **) &identifier
, &identifier_len
))
1107 if (0 > sd_journal_get_data(j
, "_PID",
1108 (const void **) &pid
, &pid_len
))
1110 if (0 > sd_journal_get_data(j
, "MESSAGE",
1111 (const void **) &message
, &message_len
))
1114 sd_journal_get_realtime_usec(j
, &x
);
1116 timestamp
= make_time(time_mode
, t
);
1117 /* Get rid of journal entry field identifiers */
1118 identifier
= strchr(identifier
, '=') + 1;
1119 pid
= strchr(pid
, '=') + 1;
1120 message
= strchr(message
, '=') + 1;
1122 fprintf(stdout
, "%s %s[%s]: %s\n", timestamp
, identifier
, pid
,
1125 } while (sd_journal_next(j
));
1129 sd_journal_flush_matches(j
);
1130 sd_journal_close(j
);
1134 static int print_pretty(struct libscols_table
*table
)
1136 struct libscols_iter
*itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
1137 struct libscols_column
*col
;
1138 struct libscols_cell
*data
;
1139 struct libscols_line
*ln
;
1140 const char *hstr
, *dstr
;
1143 ln
= scols_table_get_line(table
, 0);
1144 while (!scols_table_next_column(table
, itr
, &col
)) {
1146 data
= scols_line_get_cell(ln
, n
);
1148 hstr
= _(coldescs
[columns
[n
]].pretty_name
);
1149 dstr
= scols_cell_get_data(data
);
1152 printf("%s:%*c%-36s\n", hstr
, 35 - (int)strlen(hstr
), ' ', dstr
);
1156 scols_free_iter(itr
);
1161 static int print_user_table(struct lslogins_control
*ctl
)
1163 tb
= setup_table(ctl
);
1167 twalk(ctl
->usertree
, fill_table
);
1168 if (outmode
== OUT_PRETTY
) {
1170 #ifdef HAVE_LIBSYSTEMD
1171 fprintf(stdout
, _("\nLast logs:\n"));
1172 print_journal_tail(ctl
->journal_path
, ctl
->uid
, 3, ctl
->time_mode
);
1173 fputc('\n', stdout
);
1176 scols_print_table(tb
);
1180 static void free_user(void *f
)
1182 struct lslogins_user
*u
= f
;
1189 free(u
->pwd_ctime_min
);
1190 free(u
->pwd_ctime_max
);
1191 free(u
->last_login
);
1193 free(u
->last_hostname
);
1194 free(u
->failed_login
);
1195 free(u
->failed_tty
);
1198 free(u
->pwd_status
);
1199 #ifdef HAVE_LIBSELINUX
1200 freecon(u
->context
);
1205 static int parse_time_mode(const char *optarg
)
1207 struct lslogins_timefmt
{
1211 static const struct lslogins_timefmt timefmts
[] = {
1213 {"full", TIME_FULL
},
1214 {"short", TIME_SHORT
},
1218 for (i
= 0; i
< ARRAY_SIZE(timefmts
); i
++) {
1219 if (strcmp(timefmts
[i
].name
, optarg
) == 0)
1220 return timefmts
[i
].val
;
1222 errx(EXIT_FAILURE
, _("unknown time format: %s"), optarg
);
1225 static void __attribute__((__noreturn__
)) usage(FILE *out
)
1229 fputs(USAGE_HEADER
, out
);
1230 fprintf(out
, _(" %s [options]\n"), program_invocation_short_name
);
1232 fputs(USAGE_SEPARATOR
, out
);
1233 fputs(_("Display information about known users in the system.\n"), out
);
1235 fputs(USAGE_OPTIONS
, out
);
1236 fputs(_(" -a, --acc-expiration display info about passwords expiration\n"), out
);
1237 fputs(_(" -c, --colon-separate display data in a format similar to /etc/passwd\n"), out
);
1238 fputs(_(" -e, --export display in an export-able output format\n"), out
);
1239 fputs(_(" -f, --failed display data about the users' last failed logins\n"), out
);
1240 fputs(_(" -G, --supp-groups display information about groups\n"), out
);
1241 fputs(_(" -g, --groups=<groups> display users belonging to a group in <groups>\n"), out
);
1242 fputs(_(" -L, --last show info about the users' last login sessions\n"), out
);
1243 fputs(_(" -l, --logins=<logins> display only users from <logins>\n"), out
);
1244 fputs(_(" -n, --newline display each piece of information on a new line\n"), out
);
1245 fputs(_(" --noheadings don't print headings\n"), out
);
1246 fputs(_(" --notruncate don't truncate output\n"), out
);
1247 fputs(_(" -o, --output[=<list>] define the columns to output\n"), out
);
1248 fputs(_(" -p, --pwd display information related to login by password.\n"), out
);
1249 fputs(_(" -r, --raw display in raw mode\n"), out
);
1250 fputs(_(" -s, --system-accs display system accounts\n"), out
);
1251 fputs(_(" --time-format=<type> display dates in short, full or iso format\n"), out
);
1252 fputs(_(" -u, --user-accs display user accounts\n"), out
);
1253 fputs(_(" -Z, --context display SELinux contexts\n"), out
);
1254 fputs(_(" -z, --print0 delimit user entries with a nul character\n"), out
);
1255 fputs(_(" --wtmp-file <path> set an alternate path for wtmp\n"), out
);
1256 fputs(_(" --btmp-file <path> set an alternate path for btmp\n"), out
);
1257 fputs(USAGE_SEPARATOR
, out
);
1258 fputs(USAGE_HELP
, out
);
1259 fputs(USAGE_VERSION
, out
);
1261 fprintf(out
, _("\nAvailable columns:\n"));
1263 for (i
= 0; i
< ARRAY_SIZE(coldescs
); i
++)
1264 fprintf(out
, " %14s %s\n", coldescs
[i
].name
,
1265 _(coldescs
[i
].help
));
1267 fprintf(out
, USAGE_MAN_TAIL("lslogins(1)"));
1269 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
1272 int main(int argc
, char *argv
[])
1275 char *logins
= NULL
, *groups
= NULL
;
1276 char *path_wtmp
= _PATH_WTMP
, *path_btmp
= _PATH_BTMP
;
1277 struct lslogins_control
*ctl
= xcalloc(1, sizeof(struct lslogins_control
));
1280 /* long only options. */
1282 OPT_WTMP
= CHAR_MAX
+ 1,
1289 static const struct option longopts
[] = {
1290 { "acc-expiration", no_argument
, 0, 'a' },
1291 { "colon-separate", no_argument
, 0, 'c' },
1292 { "export", no_argument
, 0, 'e' },
1293 { "failed", no_argument
, 0, 'f' },
1294 { "groups", required_argument
, 0, 'g' },
1295 { "help", no_argument
, 0, 'h' },
1296 { "logins", required_argument
, 0, 'l' },
1297 { "supp-groups", no_argument
, 0, 'G' },
1298 { "newline", no_argument
, 0, 'n' },
1299 { "notruncate", no_argument
, 0, OPT_NOTRUNC
},
1300 { "noheadings", no_argument
, 0, OPT_NOHEAD
},
1301 { "output", required_argument
, 0, 'o' },
1302 { "last", no_argument
, 0, 'L', },
1303 { "raw", no_argument
, 0, 'r' },
1304 { "system-accs", no_argument
, 0, 's' },
1305 { "time-format", required_argument
, 0, OPT_TIME_FMT
},
1306 { "user-accs", no_argument
, 0, 'u' },
1307 { "version", no_argument
, 0, 'V' },
1308 { "pwd", no_argument
, 0, 'p' },
1309 { "print0", no_argument
, 0, 'z' },
1310 { "wtmp-file", required_argument
, 0, OPT_WTMP
},
1311 { "btmp-file", required_argument
, 0, OPT_BTMP
},
1312 #ifdef HAVE_LIBSELINUX
1313 { "context", no_argument
, 0, 'Z' },
1318 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
1323 { 'c','n','r','z' },
1327 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
1329 setlocale(LC_ALL
, "");
1330 bindtextdomain(PACKAGE
, LOCALEDIR
);
1331 textdomain(PACKAGE
);
1332 atexit(close_stdout
);
1334 ctl
->time_mode
= TIME_SHORT
;
1336 /* very basic default */
1337 add_column(columns
, ncolumns
++, COL_UID
);
1338 add_column(columns
, ncolumns
++, COL_USER
);
1340 while ((c
= getopt_long(argc
, argv
, "acefGg:hLl:no:prsuVzZ",
1341 longopts
, NULL
)) != -1) {
1343 err_exclusive_options(c
, longopts
, excl
, excl_st
);
1347 add_column(columns
, ncolumns
++, COL_PWD_WARN
);
1348 add_column(columns
, ncolumns
++, COL_PWD_CTIME_MIN
);
1349 add_column(columns
, ncolumns
++, COL_PWD_CTIME_MAX
);
1350 add_column(columns
, ncolumns
++, COL_PWD_CTIME
);
1351 add_column(columns
, ncolumns
++, COL_PWD_EXPIR
);
1354 outmode
= OUT_COLON
;
1357 outmode
= OUT_EXPORT
;
1360 add_column(columns
, ncolumns
++, COL_FAILED_LOGIN
);
1361 add_column(columns
, ncolumns
++, COL_FAILED_TTY
);
1364 add_column(columns
, ncolumns
++, COL_GID
);
1365 add_column(columns
, ncolumns
++, COL_GROUP
);
1366 add_column(columns
, ncolumns
++, COL_SGIDS
);
1367 add_column(columns
, ncolumns
++, COL_SGROUPS
);
1376 add_column(columns
, ncolumns
++, COL_LAST_TTY
);
1377 add_column(columns
, ncolumns
++, COL_LAST_HOSTNAME
);
1378 add_column(columns
, ncolumns
++, COL_LAST_LOGIN
);
1384 outmode
= OUT_NEWLINE
;
1390 ncolumns
= string_to_idarray(optarg
,
1391 columns
, ARRAY_SIZE(columns
),
1394 return EXIT_FAILURE
;
1402 ctl
->SYS_UID_MIN
= getlogindefs_num("SYS_UID_MIN", UL_SYS_UID_MIN
);
1403 ctl
->SYS_UID_MAX
= getlogindefs_num("SYS_UID_MAX", UL_SYS_UID_MAX
);
1404 lslogins_flag
|= F_SYSAC
;
1407 ctl
->UID_MIN
= getlogindefs_num("UID_MIN", UL_UID_MIN
);
1408 ctl
->UID_MAX
= getlogindefs_num("UID_MAX", UL_UID_MAX
);
1409 lslogins_flag
|= F_USRAC
;
1412 add_column(columns
, ncolumns
++, COL_PWDEMPTY
);
1413 add_column(columns
, ncolumns
++, COL_PWDLOCK
);
1414 add_column(columns
, ncolumns
++, COL_PWDDENY
);
1415 add_column(columns
, ncolumns
++, COL_NOLOGIN
);
1416 add_column(columns
, ncolumns
++, COL_HUSH_STATUS
);
1431 ctl
->noheadings
= 1;
1434 ctl
->time_mode
= parse_time_mode(optarg
);
1437 printf(UTIL_LINUX_VERSION
);
1438 return EXIT_SUCCESS
;
1441 #ifdef HAVE_LIBSELINUX
1442 int sl
= is_selinux_enabled();
1444 warn(_("failed to request selinux state"));
1446 ctl
->selinux_enabled
= sl
== 1;
1448 add_column(columns
, ncolumns
++, COL_SELINUX
);
1456 if (argc
- optind
== 1) {
1457 if (strchr(argv
[optind
], ','))
1458 errx(EXIT_FAILURE
, _("Only one user may be specified. Use -l for multiple users."));
1459 logins
= argv
[optind
];
1460 outmode
= OUT_PRETTY
;
1461 } else if (argc
!= optind
)
1462 errx(EXIT_FAILURE
, _("Only one user may be specified. Use -l for multiple users."));
1464 scols_init_debug(0);
1466 /* lslogins -u -s == lslogins */
1467 if (lslogins_flag
& F_USRAC
&& lslogins_flag
& F_SYSAC
)
1468 lslogins_flag
&= ~(F_USRAC
| F_SYSAC
);
1470 if (outmode
== OUT_PRETTY
&& !opt_o
) {
1471 /* all columns for lslogins <username> */
1472 for (ncolumns
= 0, i
= 0; i
< ARRAY_SIZE(coldescs
); i
++)
1473 columns
[ncolumns
++] = i
;
1475 } else if (ncolumns
== 2 && !opt_o
) {
1476 /* default colummns */
1477 add_column(columns
, ncolumns
++, COL_NPROCS
);
1478 add_column(columns
, ncolumns
++, COL_PWDLOCK
);
1479 add_column(columns
, ncolumns
++, COL_PWDDENY
);
1480 add_column(columns
, ncolumns
++, COL_LAST_LOGIN
);
1481 add_column(columns
, ncolumns
++, COL_GECOS
);
1485 parse_wtmp(ctl
, path_wtmp
);
1487 parse_btmp(ctl
, path_btmp
);
1489 if (logins
|| groups
)
1490 get_ulist(ctl
, logins
, groups
);
1492 if (create_usertree(ctl
))
1493 return EXIT_FAILURE
;
1495 print_user_table(ctl
);
1497 scols_unref_table(tb
);
1498 tdestroy(ctl
->usertree
, free_user
);
1501 return EXIT_SUCCESS
;