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>
41 #include <libsmartcols.h>
42 #ifdef HAVE_LIBSELINUX
43 # include <selinux/selinux.h>
46 #ifdef HAVE_LIBSYSTEMD
47 # include <systemd/sd-journal.h>
52 #include "closestream.h"
57 #include "pathnames.h"
58 #include "logindefs.h"
60 #include "procutils.h"
65 struct lslogins_coldesc
{
68 const char *pretty_name
;
70 double whint
; /* width hint */
74 static int lslogins_flag
;
76 #define UL_UID_MIN 1000
77 #define UL_UID_MAX 60000
78 #define UL_SYS_UID_MIN 201
79 #define UL_SYS_UID_MAX 999
81 /* we use the value of outmode to determine
82 * appropriate flags for the libsmartcols table
83 * (e.g., a value of out_newline would imply a raw
84 * table with the column separator set to '\n').
99 struct lslogins_user
{
127 #ifdef HAVE_LIBSELINUX
128 security_context_t context
;
189 #define is_wtmp_col(x) ((x) == COL_LAST_LOGIN || \
190 (x) == COL_LAST_TTY || \
191 (x) == COL_LAST_HOSTNAME)
193 #define is_btmp_col(x) ((x) == COL_FAILED_LOGIN || \
194 (x) == COL_FAILED_TTY)
202 static const char *const status
[] = {
203 [STATUS_FALSE
] = "0",
205 [STATUS_UNKNOWN
]= NULL
208 static const char *const pretty_status
[] = {
209 [STATUS_FALSE
] = N_("no"),
210 [STATUS_TRUE
] = N_("yes"),
211 [STATUS_UNKNOWN
]= NULL
214 #define get_status(x) (outmode == OUT_PRETTY ? pretty_status[(x)] : status[(x)])
216 static const struct lslogins_coldesc coldescs
[] =
218 [COL_USER
] = { "USER", N_("user name"), N_("Username"), 0.1, SCOLS_FL_NOEXTREMES
},
219 [COL_UID
] = { "UID", N_("user ID"), "UID", 1, SCOLS_FL_RIGHT
},
220 [COL_PWDEMPTY
] = { "PWD-EMPTY", N_("password not required"), N_("Password not required"), 1, SCOLS_FL_RIGHT
},
221 [COL_PWDDENY
] = { "PWD-DENY", N_("login by password disabled"), N_("Login by password disabled"), 1, SCOLS_FL_RIGHT
},
222 [COL_PWDLOCK
] = { "PWD-LOCK", N_("password defined, but locked"), N_("Password is locked"), 1, SCOLS_FL_RIGHT
},
223 [COL_NOLOGIN
] = { "NOLOGIN", N_("log in disabled by nologin(8) or pam_nologin(8)"), N_("No login"), 1, SCOLS_FL_RIGHT
},
224 [COL_GROUP
] = { "GROUP", N_("primary group name"), N_("Primary group"), 0.1 },
225 [COL_GID
] = { "GID", N_("primary group ID"), "GID", 1, SCOLS_FL_RIGHT
},
226 [COL_SGROUPS
] = { "SUPP-GROUPS", N_("supplementary group names"), N_("Supplementary groups"), 0.1 },
227 [COL_SGIDS
] = { "SUPP-GIDS", N_("supplementary group IDs"), N_("Supplementary group IDs"), 0.1 },
228 [COL_HOME
] = { "HOMEDIR", N_("home directory"), N_("Home directory"), 0.1 },
229 [COL_SHELL
] = { "SHELL", N_("login shell"), N_("Shell"), 0.1 },
230 [COL_GECOS
] = { "GECOS", N_("full user name"), N_("Gecos field"), 0.1, SCOLS_FL_TRUNC
},
231 [COL_LAST_LOGIN
] = { "LAST-LOGIN", N_("date of last login"), N_("Last login"), 0.1, SCOLS_FL_RIGHT
},
232 [COL_LAST_TTY
] = { "LAST-TTY", N_("last tty used"), N_("Last terminal"), 0.05 },
233 [COL_LAST_HOSTNAME
] = { "LAST-HOSTNAME",N_("hostname during the last session"), N_("Last hostname"), 0.1},
234 [COL_FAILED_LOGIN
] = { "FAILED-LOGIN", N_("date of last failed login"), N_("Failed login"), 0.1 },
235 [COL_FAILED_TTY
] = { "FAILED-TTY", N_("where did the login fail?"), N_("Failed login terminal"), 0.05 },
236 [COL_HUSH_STATUS
] = { "HUSHED", N_("user's hush settings"), N_("Hushed"), 1, SCOLS_FL_RIGHT
},
237 [COL_PWD_WARN
] = { "PWD-WARN", N_("days user is warned of password expiration"), N_("Password expiration warn interval"), 0.1, SCOLS_FL_RIGHT
},
238 [COL_PWD_EXPIR
] = { "PWD-EXPIR", N_("password expiration date"), N_("Password expiration"), 0.1, SCOLS_FL_RIGHT
},
239 [COL_PWD_CTIME
] = { "PWD-CHANGE", N_("date of last password change"), N_("Password changed"), 0.1, SCOLS_FL_RIGHT
},
240 [COL_PWD_CTIME_MIN
] = { "PWD-MIN", N_("number of days required between changes"), N_("Minimum change time"), 0.1, SCOLS_FL_RIGHT
},
241 [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
},
242 [COL_SELINUX
] = { "CONTEXT", N_("the user's security context"), N_("Selinux context"), 0.1 },
243 [COL_NPROCS
] = { "PROC", N_("number of processes run by the user"), N_("Running processes"), 1, SCOLS_FL_RIGHT
},
246 struct lslogins_control
{
265 unsigned int time_mode
;
267 const char *journal_path
;
269 unsigned int selinux_enabled
: 1,
275 /* these have to remain global since there's no other reasonable way to pass
276 * them for each call of fill_table() via twalk() */
277 static struct libscols_table
*tb
;
279 /* columns[] array specifies all currently wanted output column. The columns
280 * are defined by coldescs[] array and you can specify (on command line) each
281 * column twice. That's enough, dynamically allocated array of the columns is
282 * unnecessary overkill and over-engineering in this case */
283 static int columns
[ARRAY_SIZE(coldescs
) * 2];
286 static inline size_t err_columns_index(size_t arysz
, size_t idx
)
289 errx(EXIT_FAILURE
, _("too many columns specified, "
290 "the limit is %zu columns"),
295 #define add_column(ary, n, id) \
296 ((ary)[ err_columns_index(ARRAY_SIZE(ary), (n)) ] = (id))
298 static struct timeval now
;
300 static int date_is_today(time_t t
)
303 gettimeofday(&now
, NULL
);
304 return t
/ (3600 * 24) == now
.tv_sec
/ (3600 * 24);
307 static int date_is_thisyear(time_t t
)
310 gettimeofday(&now
, NULL
);
311 return t
/ (3600 * 24 * 365) == now
.tv_sec
/ (3600 * 24 * 365);
314 static int column_name_to_id(const char *name
, size_t namesz
)
318 for (i
= 0; i
< ARRAY_SIZE(coldescs
); i
++) {
319 const char *cn
= coldescs
[i
].name
;
321 if (!strncasecmp(name
, cn
, namesz
) && !*(cn
+ namesz
))
324 warnx(_("unknown column: %s"), name
);
328 static char *make_time(int mode
, time_t time
)
334 localtime_r(&time
, &tm
);
339 if (*(s
= buf
+ strlen(buf
) - 1) == '\n')
343 if (date_is_today(time
))
344 strftime(buf
, sizeof(buf
), "%H:%M:%S", &tm
);
345 else if (date_is_thisyear(time
))
346 strftime(buf
, sizeof(buf
), "%b%d/%H:%M", &tm
);
348 strftime(buf
, sizeof(buf
), "%Y-%b%d", &tm
);
351 strftime(buf
, sizeof(buf
), "%Y-%m-%dT%H:%M:%S%z", &tm
);
354 strftime(buf
, sizeof(buf
), "%Y-%m-%d", &tm
);
357 errx(EXIT_FAILURE
, _("unsupported time type"));
363 static char *uidtostr(uid_t uid
)
365 char *str_uid
= NULL
;
366 xasprintf(&str_uid
, "%u", uid
);
370 static char *gidtostr(gid_t gid
)
372 char *str_gid
= NULL
;
373 xasprintf(&str_gid
, "%u", gid
);
377 static char *build_sgroups_string(gid_t
*sgroups
, size_t nsgroups
, int want_names
)
379 size_t n
= 0, maxlen
, len
;
385 len
= maxlen
= nsgroups
* 10;
386 res
= p
= xmalloc(maxlen
);
388 while (n
< nsgroups
) {
392 x
= snprintf(p
, len
, "%u,", sgroups
[n
]);
394 struct group
*grp
= getgrgid(sgroups
[n
]);
399 x
= snprintf(p
, len
, "%s,", grp
->gr_name
);
402 if (x
< 0 || (size_t) x
+ 1 > len
) {
403 size_t cur
= p
- res
;
406 res
= xrealloc(res
, maxlen
);
423 static struct utmp
*get_last_wtmp(struct lslogins_control
*ctl
, const char *username
)
431 len
= strlen(username
);
432 n
= ctl
->wtmp_size
- 1;
434 if (!strncmp(username
, ctl
->wtmp
[n
].ut_user
,
435 len
< UT_NAMESIZE
? len
: UT_NAMESIZE
))
436 return ctl
->wtmp
+ n
;
442 static int require_wtmp(void)
445 for (i
= 0; i
< (size_t) ncolumns
; i
++)
446 if (is_wtmp_col(columns
[i
]))
451 static int require_btmp(void)
454 for (i
= 0; i
< (size_t) ncolumns
; i
++)
455 if (is_btmp_col(columns
[i
]))
460 static struct utmp
*get_last_btmp(struct lslogins_control
*ctl
, const char *username
)
468 len
= strlen(username
);
469 n
= ctl
->btmp_size
- 1;
471 if (!strncmp(username
, ctl
->btmp
[n
].ut_user
,
472 len
< UT_NAMESIZE
? len
: UT_NAMESIZE
))
473 return ctl
->btmp
+ n
;
479 static int parse_wtmp(struct lslogins_control
*ctl
, char *path
)
483 rc
= read_utmp(path
, &ctl
->wtmp_size
, &ctl
->wtmp
);
484 if (rc
< 0 && errno
!= EACCES
)
485 err(EXIT_FAILURE
, "%s", path
);
489 static int parse_btmp(struct lslogins_control
*ctl
, char *path
)
493 rc
= read_utmp(path
, &ctl
->btmp_size
, &ctl
->btmp
);
494 if (rc
< 0 && errno
!= EACCES
)
495 err(EXIT_FAILURE
, "%s", path
);
499 static int get_sgroups(gid_t
**list
, size_t *len
, struct passwd
*pwd
)
506 /* first let's get a supp. group count */
507 getgrouplist(pwd
->pw_name
, pwd
->pw_gid
, *list
, (int *) len
);
511 *list
= xcalloc(1, *len
* sizeof(gid_t
));
513 /* now for the actual list of GIDs */
514 if (-1 == getgrouplist(pwd
->pw_name
, pwd
->pw_gid
, *list
, (int *) len
))
517 /* getgroups also returns the user's primary GID - dispose of it */
519 if ((*list
)[n
] == pwd
->pw_gid
)
525 (*list
)[n
] = (*list
)[--(*len
)];
529 static int get_nprocs(const uid_t uid
)
533 struct proc_processes
*proc
= proc_open_processes();
535 proc_processes_filter_by_uid(proc
, uid
);
537 while (!proc_next_pid(proc
, &pid
))
540 proc_close_processes(proc
);
544 static int valid_pwd(const char *str
)
548 for (p
= str
; p
&& *p
; p
++)
549 if (!isalnum((unsigned int) *p
))
551 return p
> str
? 1 : 0;
554 static struct lslogins_user
*get_user_info(struct lslogins_control
*ctl
, const char *username
)
556 struct lslogins_user
*user
;
560 struct utmp
*user_wtmp
= NULL
, *user_btmp
= NULL
;
566 pwd
= username
? getpwnam(username
) : getpwent();
570 ctl
->uid
= uid
= pwd
->pw_uid
;
572 /* nfsnobody is an exception to the UID_MAX limit. This is "nobody" on
573 * some systems; the decisive point is the UID - 65534 */
574 if ((lslogins_flag
& F_USRAC
) &&
575 strcmp("nfsnobody", pwd
->pw_name
) != 0 &&
577 if (uid
< ctl
->UID_MIN
|| uid
> ctl
->UID_MAX
) {
582 } else if ((lslogins_flag
& F_SYSAC
) &&
583 (uid
< ctl
->SYS_UID_MIN
|| uid
> ctl
->SYS_UID_MAX
)) {
588 user
= xcalloc(1, sizeof(struct lslogins_user
));
590 grp
= getgrgid(pwd
->pw_gid
);
595 user_wtmp
= get_last_wtmp(ctl
, pwd
->pw_name
);
597 user_btmp
= get_last_btmp(ctl
, pwd
->pw_name
);
600 shadow
= getspnam(pwd
->pw_name
);
603 /* required by tseach() stuff */
604 user
->uid
= pwd
->pw_uid
;
606 while (n
< ncolumns
) {
607 switch (columns
[n
++]) {
609 user
->login
= xstrdup(pwd
->pw_name
);
612 user
->uid
= pwd
->pw_uid
;
615 user
->group
= xstrdup(grp
->gr_name
);
618 user
->gid
= pwd
->pw_gid
;
622 if (get_sgroups(&user
->sgroups
, &user
->nsgroups
, pwd
))
623 err(EXIT_FAILURE
, _("failed to get supplementary groups"));
626 user
->homedir
= xstrdup(pwd
->pw_dir
);
629 user
->shell
= xstrdup(pwd
->pw_shell
);
632 user
->gecos
= xstrdup(pwd
->pw_gecos
);
636 time
= user_wtmp
->ut_tv
.tv_sec
;
637 user
->last_login
= make_time(ctl
->time_mode
, time
);
642 user
->last_tty
= xstrdup(user_wtmp
->ut_line
);
644 case COL_LAST_HOSTNAME
:
646 user
->last_hostname
= xstrdup(user_wtmp
->ut_host
);
648 case COL_FAILED_LOGIN
:
650 time
= user_btmp
->ut_tv
.tv_sec
;
651 user
->failed_login
= make_time(ctl
->time_mode
, time
);
656 user
->failed_tty
= xstrdup(user_btmp
->ut_line
);
658 case COL_HUSH_STATUS
:
659 user
->hushed
= get_hushlogin_status(pwd
, 0);
660 if (user
->hushed
== -1)
661 user
->hushed
= STATUS_UNKNOWN
;
665 if (!*shadow
->sp_pwdp
) /* '\0' */
666 user
->pwd_empty
= STATUS_TRUE
;
668 user
->pwd_empty
= STATUS_UNKNOWN
;
672 if ((*shadow
->sp_pwdp
== '!' ||
673 *shadow
->sp_pwdp
== '*') &&
674 !valid_pwd(shadow
->sp_pwdp
+ 1))
675 user
->pwd_deny
= STATUS_TRUE
;
677 user
->pwd_deny
= STATUS_UNKNOWN
;
682 if (*shadow
->sp_pwdp
== '!' && valid_pwd(shadow
->sp_pwdp
+ 1))
683 user
->pwd_lock
= STATUS_TRUE
;
685 user
->pwd_lock
= STATUS_UNKNOWN
;
688 if (strstr(pwd
->pw_shell
, "nologin"))
690 else if (pwd
->pw_uid
)
691 user
->nologin
= access(_PATH_NOLOGIN
, F_OK
) == 0 ||
692 access(_PATH_VAR_NOLOGIN
, F_OK
) == 0;
695 if (shadow
&& shadow
->sp_warn
>= 0)
696 xasprintf(&user
->pwd_warn
, "%ld", shadow
->sp_warn
);
699 if (shadow
&& shadow
->sp_expire
>= 0)
700 user
->pwd_expire
= make_time(ctl
->time_mode
== TIME_ISO
?
701 TIME_ISO_SHORT
: ctl
->time_mode
,
702 shadow
->sp_expire
* 86400);
705 /* sp_lstchg is specified in days, showing hours
706 * (especially in non-GMT timezones) would only serve
709 user
->pwd_ctime
= make_time(ctl
->time_mode
== TIME_ISO
?
710 TIME_ISO_SHORT
: ctl
->time_mode
,
711 shadow
->sp_lstchg
* 86400);
713 case COL_PWD_CTIME_MIN
:
714 if (shadow
&& shadow
->sp_min
> 0)
715 xasprintf(&user
->pwd_ctime_min
, "%ld", shadow
->sp_min
);
717 case COL_PWD_CTIME_MAX
:
718 if (shadow
&& shadow
->sp_max
> 0)
719 xasprintf(&user
->pwd_ctime_max
, "%ld", shadow
->sp_max
);
722 #ifdef HAVE_LIBSELINUX
723 if (ctl
->selinux_enabled
) {
724 /* typedefs and pointers are pure evil */
725 security_context_t con
= NULL
;
726 if (getcon(&con
) == 0)
732 xasprintf(&user
->nprocs
, "%d", get_nprocs(pwd
->pw_uid
));
735 /* something went very wrong here */
736 err(EXIT_FAILURE
, "fatal: unknown error");
744 /* some UNIX implementations set errno iff a passwd/grp/...
745 * entry was not found. The original UNIX logins(1) utility always
746 * ignores invalid login/group names, so we're going to as well.*/
747 #define IS_REAL_ERRNO(e) !((e) == ENOENT || (e) == ESRCH || \
748 (e) == EBADF || (e) == EPERM || (e) == EAGAIN)
750 /* get a definitive list of users we want info about... */
752 static int str_to_uint(char *s
, unsigned int *ul
)
757 *ul
= strtoul(s
, &end
, 0);
763 static int get_ulist(struct lslogins_control
*ctl
, char *logins
, char *groups
)
766 size_t i
= 0, n
= 0, *arsiz
;
776 /* an arbitrary starting value */
778 *ar
= xcalloc(1, sizeof(char *) * (*arsiz
));
781 while ((u
= strtok(logins
, ","))) {
784 /* user specified by UID? */
785 if (!str_to_uint(u
, &uid
)) {
791 (*ar
)[i
++] = xstrdup(u
);
794 *ar
= xrealloc(*ar
, sizeof(char *) * (*arsiz
+= 32));
800 /* FIXME: this might lead to duplicit entries, although not visible
801 * in output, crunching a user's info multiple times is very redundant */
802 while ((g
= strtok(groups
, ","))) {
806 /* user specified by GID? */
807 if (!str_to_uint(g
, &gid
))
815 while ((u
= grp
->gr_mem
[n
++])) {
816 (*ar
)[i
++] = xstrdup(u
);
819 *ar
= xrealloc(*ar
, sizeof(char *) * (*arsiz
+= 32));
828 static void free_ctl(struct lslogins_control
*ctl
)
835 while (n
< ctl
->ulsiz
)
836 free(ctl
->ulist
[n
++]);
842 static struct lslogins_user
*get_next_user(struct lslogins_control
*ctl
)
844 struct lslogins_user
*u
;
846 while (!(u
= get_user_info(ctl
, NULL
))) {
847 /* no "false" errno-s here, iff we're unable to
848 * get a valid user entry for any reason, quit */
856 static int get_user(struct lslogins_control
*ctl
, struct lslogins_user
**user
,
857 const char *username
)
859 *user
= get_user_info(ctl
, username
);
861 if (IS_REAL_ERRNO(errno
))
866 static int cmp_uid(const void *a
, const void *b
)
868 uid_t x
= ((struct lslogins_user
*)a
)->uid
;
869 uid_t z
= ((struct lslogins_user
*)b
)->uid
;
870 return x
> z
? 1 : (x
< z
? -1 : 0);
873 static int create_usertree(struct lslogins_control
*ctl
)
875 struct lslogins_user
*user
= NULL
;
879 while (n
< ctl
->ulsiz
) {
880 if (get_user(ctl
, &user
, ctl
->ulist
[n
]))
882 if (user
) /* otherwise an invalid user name has probably been given */
883 tsearch(user
, &ctl
->usertree
, cmp_uid
);
887 while ((user
= get_next_user(ctl
)))
888 tsearch(user
, &ctl
->usertree
, cmp_uid
);
893 static struct libscols_table
*setup_table(struct lslogins_control
*ctl
)
895 struct libscols_table
*table
= scols_new_table();
899 errx(EXIT_FAILURE
, _("failed to initialize output table"));
901 scols_table_enable_noheadings(table
, 1);
905 scols_table_enable_raw(table
, 1);
906 scols_table_set_column_separator(table
, ":");
909 scols_table_set_column_separator(table
, "\n");
912 scols_table_enable_export(table
, 1);
915 scols_table_set_line_separator(table
, "\0");
918 scols_table_enable_raw(table
, 1);
921 scols_table_enable_noheadings(table
, 1);
926 while (n
< ncolumns
) {
927 int flags
= coldescs
[columns
[n
]].flag
;
930 flags
&= ~SCOLS_FL_TRUNC
;
932 if (!scols_table_new_column(table
,
933 coldescs
[columns
[n
]].name
,
934 coldescs
[columns
[n
]].whint
,
942 scols_unref_table(table
);
946 static void fill_table(const void *u
, const VISIT which
, const int depth
__attribute__((unused
)))
948 struct libscols_line
*ln
;
949 struct lslogins_user
*user
= *(struct lslogins_user
**)u
;
952 if (which
== preorder
|| which
== endorder
)
955 ln
= scols_table_new_line(tb
, NULL
);
956 while (n
< ncolumns
) {
959 switch (columns
[n
]) {
961 rc
= scols_line_set_data(ln
, n
, user
->login
);
964 rc
= scols_line_refer_data(ln
, n
, uidtostr(user
->uid
));
967 rc
= scols_line_set_data(ln
, n
, get_status(user
->pwd_empty
));
970 rc
= scols_line_set_data(ln
, n
, get_status(user
->nologin
));
973 rc
= scols_line_set_data(ln
, n
, get_status(user
->pwd_lock
));
976 rc
= scols_line_set_data(ln
, n
, get_status(user
->pwd_deny
));
979 rc
= scols_line_set_data(ln
, n
, user
->group
);
982 rc
= scols_line_refer_data(ln
, n
, gidtostr(user
->gid
));
985 rc
= scols_line_refer_data(ln
, n
,
986 build_sgroups_string(user
->sgroups
,
991 rc
= scols_line_refer_data(ln
, n
,
992 build_sgroups_string(user
->sgroups
,
997 rc
= scols_line_set_data(ln
, n
, user
->homedir
);
1000 rc
= scols_line_set_data(ln
, n
, user
->shell
);
1003 rc
= scols_line_set_data(ln
, n
, user
->gecos
);
1005 case COL_LAST_LOGIN
:
1006 rc
= scols_line_set_data(ln
, n
, user
->last_login
);
1009 rc
= scols_line_set_data(ln
, n
, user
->last_tty
);
1011 case COL_LAST_HOSTNAME
:
1012 rc
= scols_line_set_data(ln
, n
, user
->last_hostname
);
1014 case COL_FAILED_LOGIN
:
1015 rc
= scols_line_set_data(ln
, n
, user
->failed_login
);
1017 case COL_FAILED_TTY
:
1018 rc
= scols_line_set_data(ln
, n
, user
->failed_tty
);
1020 case COL_HUSH_STATUS
:
1021 rc
= scols_line_set_data(ln
, n
, get_status(user
->hushed
));
1024 rc
= scols_line_set_data(ln
, n
, user
->pwd_warn
);
1027 rc
= scols_line_set_data(ln
, n
, user
->pwd_expire
);
1030 rc
= scols_line_set_data(ln
, n
, user
->pwd_ctime
);
1032 case COL_PWD_CTIME_MIN
:
1033 rc
= scols_line_set_data(ln
, n
, user
->pwd_ctime_min
);
1035 case COL_PWD_CTIME_MAX
:
1036 rc
= scols_line_set_data(ln
, n
, user
->pwd_ctime_max
);
1039 #ifdef HAVE_LIBSELINUX
1040 rc
= scols_line_set_data(ln
, n
, user
->context
);
1044 rc
= scols_line_set_data(ln
, n
, user
->nprocs
);
1047 /* something went very wrong here */
1048 err(EXIT_FAILURE
, _("internal error: unknown column"));
1052 err(EXIT_FAILURE
, _("failed to set data"));
1057 #ifdef HAVE_LIBSYSTEMD
1058 static void print_journal_tail(const char *journal_path
, uid_t uid
, size_t len
, int time_mode
)
1061 char *match
, *timestamp
;
1064 const char *identifier
, *pid
, *message
;
1065 size_t identifier_len
, pid_len
, message_len
;
1068 sd_journal_open_directory(&j
, journal_path
, 0);
1070 sd_journal_open(&j
, SD_JOURNAL_LOCAL_ONLY
);
1072 xasprintf(&match
, "_UID=%d", uid
);
1074 sd_journal_add_match(j
, match
, 0);
1075 sd_journal_seek_tail(j
);
1076 sd_journal_previous_skip(j
, len
);
1079 if (0 > sd_journal_get_data(j
, "SYSLOG_IDENTIFIER",
1080 (const void **) &identifier
, &identifier_len
))
1082 if (0 > sd_journal_get_data(j
, "_PID",
1083 (const void **) &pid
, &pid_len
))
1085 if (0 > sd_journal_get_data(j
, "MESSAGE",
1086 (const void **) &message
, &message_len
))
1089 sd_journal_get_realtime_usec(j
, &x
);
1091 timestamp
= make_time(time_mode
, t
);
1092 /* Get rid of journal entry field identifiers */
1093 identifier
= strchr(identifier
, '=') + 1;
1094 pid
= strchr(pid
, '=') + 1;
1095 message
= strchr(message
, '=') + 1;
1097 fprintf(stdout
, "%s %s[%s]: %s\n", timestamp
, identifier
, pid
,
1100 } while (sd_journal_next(j
));
1104 sd_journal_flush_matches(j
);
1105 sd_journal_close(j
);
1109 static int print_pretty(struct libscols_table
*table
)
1111 struct libscols_iter
*itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
1112 struct libscols_column
*col
;
1113 struct libscols_cell
*data
;
1114 struct libscols_line
*ln
;
1115 const char *hstr
, *dstr
;
1118 ln
= scols_table_get_line(table
, 0);
1119 while (!scols_table_next_column(table
, itr
, &col
)) {
1121 data
= scols_line_get_cell(ln
, n
);
1123 hstr
= _(coldescs
[columns
[n
]].pretty_name
);
1124 dstr
= scols_cell_get_data(data
);
1127 printf("%s:%*c%-36s\n", hstr
, 35 - (int)strlen(hstr
), ' ', dstr
);
1131 scols_free_iter(itr
);
1136 static int print_user_table(struct lslogins_control
*ctl
)
1138 tb
= setup_table(ctl
);
1142 twalk(ctl
->usertree
, fill_table
);
1143 if (outmode
== OUT_PRETTY
) {
1145 #ifdef HAVE_LIBSYSTEMD
1146 fprintf(stdout
, _("\nLast logs:\n"));
1147 print_journal_tail(ctl
->journal_path
, ctl
->uid
, 3, ctl
->time_mode
);
1148 fputc('\n', stdout
);
1151 scols_print_table(tb
);
1155 static void free_user(void *f
)
1157 struct lslogins_user
*u
= f
;
1164 free(u
->pwd_ctime_min
);
1165 free(u
->pwd_ctime_max
);
1166 free(u
->last_login
);
1168 free(u
->last_hostname
);
1169 free(u
->failed_login
);
1170 free(u
->failed_tty
);
1173 free(u
->pwd_status
);
1174 #ifdef HAVE_LIBSELINUX
1175 freecon(u
->context
);
1180 static int parse_time_mode(const char *optarg
)
1182 struct lslogins_timefmt
{
1186 static const struct lslogins_timefmt timefmts
[] = {
1188 {"full", TIME_FULL
},
1189 {"short", TIME_SHORT
},
1193 for (i
= 0; i
< ARRAY_SIZE(timefmts
); i
++) {
1194 if (strcmp(timefmts
[i
].name
, optarg
) == 0)
1195 return timefmts
[i
].val
;
1197 errx(EXIT_FAILURE
, _("unknown time format: %s"), optarg
);
1200 static void __attribute__((__noreturn__
)) usage(FILE *out
)
1204 fputs(USAGE_HEADER
, out
);
1205 fprintf(out
, _(" %s [options]\n"), program_invocation_short_name
);
1207 fputs(USAGE_SEPARATOR
, out
);
1208 fputs(_("Display information about known users in the system.\n"), out
);
1210 fputs(USAGE_OPTIONS
, out
);
1211 fputs(_(" -a, --acc-expiration display info about passwords expiration\n"), out
);
1212 fputs(_(" -c, --colon-separate display data in a format similar to /etc/passwd\n"), out
);
1213 fputs(_(" -e, --export display in an export-able output format\n"), out
);
1214 fputs(_(" -f, --failed display data about the users' last failed logins\n"), out
);
1215 fputs(_(" -G, --supp-groups display information about groups\n"), out
);
1216 fputs(_(" -g, --groups=<groups> display users belonging to a group in <groups>\n"), out
);
1217 fputs(_(" -L, --last show info about the users' last login sessions\n"), out
);
1218 fputs(_(" -l, --logins=<logins> display only users from <logins>\n"), out
);
1219 fputs(_(" -n, --newline display each piece of information on a new line\n"), out
);
1220 fputs(_(" --noheadings don't print headings\n"), out
);
1221 fputs(_(" --notruncate don't truncate output\n"), out
);
1222 fputs(_(" -o, --output[=<list>] define the columns to output\n"), out
);
1223 fputs(_(" -p, --pwd display information related to login by password.\n"), out
);
1224 fputs(_(" -r, --raw display in raw mode\n"), out
);
1225 fputs(_(" -s, --system-accs display system accounts\n"), out
);
1226 fputs(_(" --time-format=<type> display dates in short, full or iso format\n"), out
);
1227 fputs(_(" -u, --user-accs display user accounts\n"), out
);
1228 fputs(_(" -Z, --context display SELinux contexts\n"), out
);
1229 fputs(_(" -z, --print0 delimit user entries with a nul character\n"), out
);
1230 fputs(_(" --wtmp-file <path> set an alternate path for wtmp\n"), out
);
1231 fputs(_(" --btmp-file <path> set an alternate path for btmp\n"), out
);
1232 fputs(USAGE_SEPARATOR
, out
);
1233 fputs(USAGE_HELP
, out
);
1234 fputs(USAGE_VERSION
, out
);
1236 fprintf(out
, _("\nAvailable columns:\n"));
1238 for (i
= 0; i
< ARRAY_SIZE(coldescs
); i
++)
1239 fprintf(out
, " %14s %s\n", coldescs
[i
].name
,
1240 _(coldescs
[i
].help
));
1242 fprintf(out
, USAGE_MAN_TAIL("lslogins(1)"));
1244 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
1247 int main(int argc
, char *argv
[])
1250 char *logins
= NULL
, *groups
= NULL
;
1251 char *path_wtmp
= _PATH_WTMP
, *path_btmp
= _PATH_BTMP
;
1252 struct lslogins_control
*ctl
= xcalloc(1, sizeof(struct lslogins_control
));
1255 /* long only options. */
1257 OPT_WTMP
= CHAR_MAX
+ 1,
1264 static const struct option longopts
[] = {
1265 { "acc-expiration", no_argument
, 0, 'a' },
1266 { "colon-separate", no_argument
, 0, 'c' },
1267 { "export", no_argument
, 0, 'e' },
1268 { "failed", no_argument
, 0, 'f' },
1269 { "groups", required_argument
, 0, 'g' },
1270 { "help", no_argument
, 0, 'h' },
1271 { "logins", required_argument
, 0, 'l' },
1272 { "supp-groups", no_argument
, 0, 'G' },
1273 { "newline", no_argument
, 0, 'n' },
1274 { "notruncate", no_argument
, 0, OPT_NOTRUNC
},
1275 { "noheadings", no_argument
, 0, OPT_NOHEAD
},
1276 { "output", required_argument
, 0, 'o' },
1277 { "last", no_argument
, 0, 'L', },
1278 { "raw", no_argument
, 0, 'r' },
1279 { "system-accs", no_argument
, 0, 's' },
1280 { "time-format", required_argument
, 0, OPT_TIME_FMT
},
1281 { "user-accs", no_argument
, 0, 'u' },
1282 { "version", no_argument
, 0, 'V' },
1283 { "pwd", no_argument
, 0, 'p' },
1284 { "print0", no_argument
, 0, 'z' },
1285 { "wtmp-file", required_argument
, 0, OPT_WTMP
},
1286 { "btmp-file", required_argument
, 0, OPT_BTMP
},
1287 #ifdef HAVE_LIBSELINUX
1288 { "context", no_argument
, 0, 'Z' },
1293 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
1298 { 'c','n','r','z' },
1302 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
1304 setlocale(LC_ALL
, "");
1305 bindtextdomain(PACKAGE
, LOCALEDIR
);
1306 textdomain(PACKAGE
);
1307 atexit(close_stdout
);
1309 ctl
->time_mode
= TIME_SHORT
;
1311 /* very basic default */
1312 add_column(columns
, ncolumns
++, COL_UID
);
1313 add_column(columns
, ncolumns
++, COL_USER
);
1315 while ((c
= getopt_long(argc
, argv
, "acefGg:hLl:no:prsuVzZ",
1316 longopts
, NULL
)) != -1) {
1318 err_exclusive_options(c
, longopts
, excl
, excl_st
);
1322 add_column(columns
, ncolumns
++, COL_PWD_WARN
);
1323 add_column(columns
, ncolumns
++, COL_PWD_CTIME_MIN
);
1324 add_column(columns
, ncolumns
++, COL_PWD_CTIME_MAX
);
1325 add_column(columns
, ncolumns
++, COL_PWD_CTIME
);
1326 add_column(columns
, ncolumns
++, COL_PWD_EXPIR
);
1329 outmode
= OUT_COLON
;
1332 outmode
= OUT_EXPORT
;
1335 add_column(columns
, ncolumns
++, COL_FAILED_LOGIN
);
1336 add_column(columns
, ncolumns
++, COL_FAILED_TTY
);
1339 add_column(columns
, ncolumns
++, COL_GID
);
1340 add_column(columns
, ncolumns
++, COL_GROUP
);
1341 add_column(columns
, ncolumns
++, COL_SGIDS
);
1342 add_column(columns
, ncolumns
++, COL_SGROUPS
);
1351 add_column(columns
, ncolumns
++, COL_LAST_TTY
);
1352 add_column(columns
, ncolumns
++, COL_LAST_HOSTNAME
);
1353 add_column(columns
, ncolumns
++, COL_LAST_LOGIN
);
1359 outmode
= OUT_NEWLINE
;
1365 ncolumns
= string_to_idarray(optarg
,
1366 columns
, ARRAY_SIZE(columns
),
1369 return EXIT_FAILURE
;
1377 ctl
->SYS_UID_MIN
= getlogindefs_num("SYS_UID_MIN", UL_SYS_UID_MIN
);
1378 ctl
->SYS_UID_MAX
= getlogindefs_num("SYS_UID_MAX", UL_SYS_UID_MAX
);
1379 lslogins_flag
|= F_SYSAC
;
1382 ctl
->UID_MIN
= getlogindefs_num("UID_MIN", UL_UID_MIN
);
1383 ctl
->UID_MAX
= getlogindefs_num("UID_MAX", UL_UID_MAX
);
1384 lslogins_flag
|= F_USRAC
;
1387 add_column(columns
, ncolumns
++, COL_PWDEMPTY
);
1388 add_column(columns
, ncolumns
++, COL_PWDLOCK
);
1389 add_column(columns
, ncolumns
++, COL_PWDDENY
);
1390 add_column(columns
, ncolumns
++, COL_NOLOGIN
);
1391 add_column(columns
, ncolumns
++, COL_HUSH_STATUS
);
1406 ctl
->noheadings
= 1;
1409 ctl
->time_mode
= parse_time_mode(optarg
);
1412 printf(UTIL_LINUX_VERSION
);
1413 return EXIT_SUCCESS
;
1416 #ifdef HAVE_LIBSELINUX
1417 int sl
= is_selinux_enabled();
1419 warn(_("failed to request selinux state"));
1421 ctl
->selinux_enabled
= sl
== 1;
1423 add_column(columns
, ncolumns
++, COL_SELINUX
);
1431 if (argc
- optind
== 1) {
1432 if (strchr(argv
[optind
], ','))
1433 errx(EXIT_FAILURE
, _("Only one user may be specified. Use -l for multiple users."));
1434 logins
= argv
[optind
];
1435 outmode
= OUT_PRETTY
;
1436 } else if (argc
!= optind
)
1437 errx(EXIT_FAILURE
, _("Only one user may be specified. Use -l for multiple users."));
1439 scols_init_debug(0);
1441 /* lslogins -u -s == lslogins */
1442 if (lslogins_flag
& F_USRAC
&& lslogins_flag
& F_SYSAC
)
1443 lslogins_flag
&= ~(F_USRAC
| F_SYSAC
);
1445 if (outmode
== OUT_PRETTY
&& !opt_o
) {
1446 /* all columns for lslogins <username> */
1447 for (ncolumns
= 0, i
= 0; i
< ARRAY_SIZE(coldescs
); i
++)
1448 columns
[ncolumns
++] = i
;
1450 } else if (ncolumns
== 2 && !opt_o
) {
1451 /* default colummns */
1452 add_column(columns
, ncolumns
++, COL_NPROCS
);
1453 add_column(columns
, ncolumns
++, COL_PWDLOCK
);
1454 add_column(columns
, ncolumns
++, COL_PWDDENY
);
1455 add_column(columns
, ncolumns
++, COL_LAST_LOGIN
);
1456 add_column(columns
, ncolumns
++, COL_GECOS
);
1460 parse_wtmp(ctl
, path_wtmp
);
1462 parse_btmp(ctl
, path_btmp
);
1464 if (logins
|| groups
)
1465 get_ulist(ctl
, logins
, groups
);
1467 if (create_usertree(ctl
))
1468 return EXIT_FAILURE
;
1470 print_user_table(ctl
);
1472 scols_unref_table(tb
);
1473 tdestroy(ctl
->usertree
, free_user
);
1476 return EXIT_SUCCESS
;