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"
59 #include "procutils.h"
60 #include "timeutils.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 101
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').
105 struct lslogins_user
{
125 const char *pwd_method
;
134 #ifdef HAVE_LIBSELINUX
135 security_context_t context
;
197 #define is_wtmp_col(x) ((x) == COL_LAST_LOGIN || \
198 (x) == COL_LAST_TTY || \
199 (x) == COL_LAST_HOSTNAME)
201 #define is_btmp_col(x) ((x) == COL_FAILED_LOGIN || \
202 (x) == COL_FAILED_TTY)
210 static const char *const status
[] = {
211 [STATUS_FALSE
] = "0",
213 [STATUS_UNKNOWN
]= NULL
216 static const char *const pretty_status
[] = {
217 [STATUS_FALSE
] = N_("no"),
218 [STATUS_TRUE
] = N_("yes"),
219 [STATUS_UNKNOWN
]= NULL
222 #define get_status(x) (outmode == OUT_PRETTY ? pretty_status[(x)] : status[(x)])
224 static const struct lslogins_coldesc coldescs
[] =
226 [COL_USER
] = { "USER", N_("user name"), N_("Username"), 0.1, SCOLS_FL_NOEXTREMES
},
227 [COL_UID
] = { "UID", N_("user ID"), "UID", 1, SCOLS_FL_RIGHT
},
228 [COL_PWDEMPTY
] = { "PWD-EMPTY", N_("password not required"), N_("Password not required"), 1, SCOLS_FL_RIGHT
},
229 [COL_PWDDENY
] = { "PWD-DENY", N_("login by password disabled"), N_("Login by password disabled"), 1, SCOLS_FL_RIGHT
},
230 [COL_PWDLOCK
] = { "PWD-LOCK", N_("password defined, but locked"), N_("Password is locked"), 1, SCOLS_FL_RIGHT
},
231 [COL_PWDMETHOD
] = { "PWD-METHOD", N_("password encryption method"), N_("Password encryption method"), 0.1 },
232 [COL_NOLOGIN
] = { "NOLOGIN", N_("log in disabled by nologin(8) or pam_nologin(8)"), N_("No login"), 1, SCOLS_FL_RIGHT
},
233 [COL_GROUP
] = { "GROUP", N_("primary group name"), N_("Primary group"), 0.1 },
234 [COL_GID
] = { "GID", N_("primary group ID"), "GID", 1, SCOLS_FL_RIGHT
},
235 [COL_SGROUPS
] = { "SUPP-GROUPS", N_("supplementary group names"), N_("Supplementary groups"), 0.1 },
236 [COL_SGIDS
] = { "SUPP-GIDS", N_("supplementary group IDs"), N_("Supplementary group IDs"), 0.1 },
237 [COL_HOME
] = { "HOMEDIR", N_("home directory"), N_("Home directory"), 0.1 },
238 [COL_SHELL
] = { "SHELL", N_("login shell"), N_("Shell"), 0.1 },
239 [COL_GECOS
] = { "GECOS", N_("full user name"), N_("Gecos field"), 0.1, SCOLS_FL_TRUNC
},
240 [COL_LAST_LOGIN
] = { "LAST-LOGIN", N_("date of last login"), N_("Last login"), 0.1, SCOLS_FL_RIGHT
},
241 [COL_LAST_TTY
] = { "LAST-TTY", N_("last tty used"), N_("Last terminal"), 0.05 },
242 [COL_LAST_HOSTNAME
] = { "LAST-HOSTNAME",N_("hostname during the last session"), N_("Last hostname"), 0.1},
243 [COL_FAILED_LOGIN
] = { "FAILED-LOGIN", N_("date of last failed login"), N_("Failed login"), 0.1 },
244 [COL_FAILED_TTY
] = { "FAILED-TTY", N_("where did the login fail?"), N_("Failed login terminal"), 0.05 },
245 [COL_HUSH_STATUS
] = { "HUSHED", N_("user's hush settings"), N_("Hushed"), 1, SCOLS_FL_RIGHT
},
246 [COL_PWD_WARN
] = { "PWD-WARN", N_("days user is warned of password expiration"), N_("Password expiration warn interval"), 0.1, SCOLS_FL_RIGHT
},
247 [COL_PWD_EXPIR
] = { "PWD-EXPIR", N_("password expiration date"), N_("Password expiration"), 0.1, SCOLS_FL_RIGHT
},
248 [COL_PWD_CTIME
] = { "PWD-CHANGE", N_("date of last password change"), N_("Password changed"), 0.1, SCOLS_FL_RIGHT
},
249 [COL_PWD_CTIME_MIN
] = { "PWD-MIN", N_("number of days required between changes"), N_("Minimum change time"), 0.1, SCOLS_FL_RIGHT
},
250 [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
},
251 [COL_SELINUX
] = { "CONTEXT", N_("the user's security context"), N_("Selinux context"), 0.1 },
252 [COL_NPROCS
] = { "PROC", N_("number of processes run by the user"), N_("Running processes"), 1, SCOLS_FL_RIGHT
},
255 struct lslogins_control
{
276 unsigned int time_mode
;
278 const char *journal_path
;
280 unsigned int selinux_enabled
: 1,
281 fail_on_unknown
: 1, /* fail if user does not exist */
287 /* these have to remain global since there's no other reasonable way to pass
288 * them for each call of fill_table() via twalk() */
289 static struct libscols_table
*tb
;
291 /* columns[] array specifies all currently wanted output column. The columns
292 * are defined by coldescs[] array and you can specify (on command line) each
293 * column twice. That's enough, dynamically allocated array of the columns is
294 * unnecessary overkill and over-engineering in this case */
295 static int columns
[ARRAY_SIZE(coldescs
) * 2];
296 static size_t ncolumns
;
298 static inline size_t err_columns_index(size_t arysz
, size_t idx
)
301 errx(EXIT_FAILURE
, _("too many columns specified, "
302 "the limit is %zu columns"),
307 #define add_column(ary, n, id) \
308 ((ary)[ err_columns_index(ARRAY_SIZE(ary), (n)) ] = (id))
310 static int column_name_to_id(const char *name
, size_t namesz
)
314 for (i
= 0; i
< ARRAY_SIZE(coldescs
); i
++) {
315 const char *cn
= coldescs
[i
].name
;
317 if (!strncasecmp(name
, cn
, namesz
) && !*(cn
+ namesz
))
320 warnx(_("unknown column: %s"), name
);
324 static struct timeval now
;
326 static char *make_time(int mode
, time_t time
)
336 localtime_r(&time
, &tm
);
339 if (*(s
= buf
+ strlen(buf
) - 1) == '\n')
345 rc
= strtime_short(&time
, &now
, UL_SHORTTIME_THISYEAR_HHMM
,
349 rc
= strtime_iso(&time
, ISO_TIMESTAMP_T
, buf
, sizeof(buf
));
352 rc
= strtime_iso(&time
, ISO_DATE
, buf
, sizeof(buf
));
355 errx(EXIT_FAILURE
, _("unsupported time type"));
359 errx(EXIT_FAILURE
, _("failed to compose time string"));
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 utmpx
*get_last_wtmp(struct lslogins_control
*ctl
, const char *username
)
432 n
= ctl
->wtmp_size
- 1;
434 if (!strncmp(username
, ctl
->wtmp
[n
].ut_user
,
435 sizeof(ctl
->wtmp
[0].ut_user
)))
436 return ctl
->wtmp
+ n
;
442 static int require_wtmp(void)
445 for (i
= 0; i
< ncolumns
; i
++)
446 if (is_wtmp_col(columns
[i
]))
451 static int require_btmp(void)
454 for (i
= 0; i
< ncolumns
; i
++)
455 if (is_btmp_col(columns
[i
]))
460 static struct utmpx
*get_last_btmp(struct lslogins_control
*ctl
, const char *username
)
467 n
= ctl
->btmp_size
- 1;
469 if (!strncmp(username
, ctl
->btmp
[n
].ut_user
,
470 sizeof(ctl
->wtmp
[0].ut_user
)))
471 return ctl
->btmp
+ n
;
477 static int read_utmp(char const *file
, size_t *nents
, struct utmpx
**res
)
479 size_t n_read
= 0, n_alloc
= 0;
480 struct utmpx
*utmp
= NULL
, *u
;
482 if (utmpxname(file
) < 0)
488 while ((u
= getutxent()) != NULL
) {
489 if (n_read
== n_alloc
) {
491 utmp
= xrealloc(utmp
, n_alloc
* sizeof (struct utmpx
));
508 static int parse_wtmp(struct lslogins_control
*ctl
, char *path
)
512 rc
= read_utmp(path
, &ctl
->wtmp_size
, &ctl
->wtmp
);
513 if (rc
< 0 && errno
!= EACCES
)
514 err(EXIT_FAILURE
, "%s", path
);
518 static int parse_btmp(struct lslogins_control
*ctl
, char *path
)
522 rc
= read_utmp(path
, &ctl
->btmp_size
, &ctl
->btmp
);
523 if (rc
< 0 && errno
!= EACCES
)
524 err(EXIT_FAILURE
, "%s", path
);
528 static void get_lastlog(struct lslogins_control
*ctl
, uid_t uid
, void *dst
, int what
)
532 if (ctl
->lastlogin_fd
< 0 ||
533 pread(ctl
->lastlogin_fd
, (void *)&ll
, sizeof(ll
), uid
* sizeof(ll
)) != sizeof(ll
))
538 time_t *t
= (time_t *)dst
;
543 mem2strcpy(dst
, ll
.ll_line
, sizeof(ll
.ll_line
), sizeof(ll
.ll_line
) + 1);
546 mem2strcpy(dst
, ll
.ll_host
, sizeof(ll
.ll_host
), sizeof(ll
.ll_host
) + 1);
553 static int get_sgroups(gid_t
**list
, size_t *len
, struct passwd
*pwd
)
561 /* first let's get a supp. group count */
562 getgrouplist(pwd
->pw_name
, pwd
->pw_gid
, *list
, &ngroups
);
566 *list
= xcalloc(1, ngroups
* sizeof(gid_t
));
568 /* now for the actual list of GIDs */
569 if (-1 == getgrouplist(pwd
->pw_name
, pwd
->pw_gid
, *list
, &ngroups
))
572 *len
= (size_t) ngroups
;
574 /* getgroups also returns the user's primary GID - dispose of it */
576 if ((*list
)[n
] == pwd
->pw_gid
)
582 (*list
)[n
] = (*list
)[--(*len
)];
587 static int get_nprocs(const uid_t uid
)
591 struct proc_processes
*proc
= proc_open_processes();
593 proc_processes_filter_by_uid(proc
, uid
);
595 while (!proc_next_pid(proc
, &pid
))
598 proc_close_processes(proc
);
602 static const char *get_pwd_method(const char *str
, const char **next
, unsigned int *sz
)
605 const char *res
= NULL
;
607 if (!p
|| *p
++ != '$')
621 if (*p
== 'a' || *p
== 'y')
646 #define is_valid_pwd_char(x) (isalnum((unsigned char) (x)) || (x) == '.' || (x) == '/')
649 * This function do not accept empty passwords or locked accouns.
651 static int valid_pwd(const char *str
)
654 unsigned int sz
= 0, n
;
660 if (get_pwd_method(str
, &p
, &sz
) == NULL
)
671 if (!is_valid_pwd_char(*p
))
678 for (n
= 0; *p
; p
++, n
++) {
679 if (!is_valid_pwd_char(*p
))
688 static struct lslogins_user
*get_user_info(struct lslogins_control
*ctl
, const char *username
)
690 struct lslogins_user
*user
;
694 struct utmpx
*user_wtmp
= NULL
, *user_btmp
= NULL
;
701 pwd
= username
? getpwnam(username
) : getpwent();
705 ctl
->uid
= uid
= pwd
->pw_uid
;
707 /* nfsnobody is an exception to the UID_MAX limit. This is "nobody" on
708 * some systems; the decisive point is the UID - 65534 */
709 if ((lslogins_flag
& F_USRAC
) &&
710 strcmp("nfsnobody", pwd
->pw_name
) != 0 &&
712 if (uid
< ctl
->UID_MIN
|| uid
> ctl
->UID_MAX
) {
717 } else if ((lslogins_flag
& F_SYSAC
) &&
718 (uid
< ctl
->SYS_UID_MIN
|| uid
> ctl
->SYS_UID_MAX
)) {
724 grp
= getgrgid(pwd
->pw_gid
);
728 user
= xcalloc(1, sizeof(struct lslogins_user
));
731 user_wtmp
= get_last_wtmp(ctl
, pwd
->pw_name
);
733 user_btmp
= get_last_btmp(ctl
, pwd
->pw_name
);
736 shadow
= getspnam(pwd
->pw_name
);
739 /* required by tseach() stuff */
740 user
->uid
= pwd
->pw_uid
;
742 while (n
< ncolumns
) {
743 switch (columns
[n
++]) {
745 user
->login
= xstrdup(pwd
->pw_name
);
748 user
->uid
= pwd
->pw_uid
;
751 user
->group
= xstrdup(grp
->gr_name
);
754 user
->gid
= pwd
->pw_gid
;
758 if (get_sgroups(&user
->sgroups
, &user
->nsgroups
, pwd
))
759 err(EXIT_FAILURE
, _("failed to get supplementary groups"));
762 user
->homedir
= xstrdup(pwd
->pw_dir
);
765 user
->shell
= xstrdup(pwd
->pw_shell
);
768 user
->gecos
= xstrdup(pwd
->pw_gecos
);
772 time
= user_wtmp
->ut_tv
.tv_sec
;
773 user
->last_login
= make_time(ctl
->time_mode
, time
);
776 get_lastlog(ctl
, pwd
->pw_uid
, &time
, LASTLOG_TIME
);
778 user
->last_login
= make_time(ctl
->time_mode
, time
);
782 user
->last_tty
= xcalloc(1, sizeof(user_wtmp
->ut_line
) + 1);
784 mem2strcpy(user
->last_tty
, user_wtmp
->ut_line
,
785 sizeof(user_wtmp
->ut_line
),
786 sizeof(user_wtmp
->ut_line
) + 1);;
788 get_lastlog(ctl
, user
->uid
, user
->last_tty
, LASTLOG_LINE
);
790 case COL_LAST_HOSTNAME
:
791 user
->last_hostname
= xcalloc(1, sizeof(user_wtmp
->ut_host
) + 1);
793 mem2strcpy(user
->last_hostname
, user_wtmp
->ut_host
,
794 sizeof(user_wtmp
->ut_host
),
795 sizeof(user_wtmp
->ut_host
) + 1);;
797 get_lastlog(ctl
, user
->uid
, user
->last_hostname
, LASTLOG_HOST
);
799 case COL_FAILED_LOGIN
:
801 time
= user_btmp
->ut_tv
.tv_sec
;
802 user
->failed_login
= make_time(ctl
->time_mode
, time
);
807 user
->failed_tty
= xmalloc(sizeof(user_btmp
->ut_line
) + 1);
808 mem2strcpy(user
->failed_tty
, user_btmp
->ut_line
,
809 sizeof(user_btmp
->ut_line
),
810 sizeof(user_btmp
->ut_line
) + 1);;
813 case COL_HUSH_STATUS
:
814 user
->hushed
= get_hushlogin_status(pwd
, 0);
815 if (user
->hushed
== -1)
816 user
->hushed
= STATUS_UNKNOWN
;
820 if (!*shadow
->sp_pwdp
) /* '\0' */
821 user
->pwd_empty
= STATUS_TRUE
;
823 user
->pwd_empty
= STATUS_UNKNOWN
;
827 if ((*shadow
->sp_pwdp
== '!' ||
828 *shadow
->sp_pwdp
== '*') &&
829 !valid_pwd(shadow
->sp_pwdp
+ 1))
830 user
->pwd_deny
= STATUS_TRUE
;
832 user
->pwd_deny
= STATUS_UNKNOWN
;
836 if (*shadow
->sp_pwdp
== '!' && valid_pwd(shadow
->sp_pwdp
+ 1))
837 user
->pwd_lock
= STATUS_TRUE
;
839 user
->pwd_lock
= STATUS_UNKNOWN
;
843 const char *p
= shadow
->sp_pwdp
;
845 if (*p
== '!' || *p
== '*')
847 user
->pwd_method
= get_pwd_method(p
, NULL
, NULL
);
849 user
->pwd_method
= NULL
;
852 if (strstr(pwd
->pw_shell
, "nologin"))
854 else if (pwd
->pw_uid
)
855 user
->nologin
= access(_PATH_NOLOGIN
, F_OK
) == 0 ||
856 access(_PATH_VAR_NOLOGIN
, F_OK
) == 0;
859 if (shadow
&& shadow
->sp_warn
>= 0)
860 xasprintf(&user
->pwd_warn
, "%ld", shadow
->sp_warn
);
863 if (shadow
&& shadow
->sp_expire
>= 0)
864 user
->pwd_expire
= make_time(ctl
->time_mode
== TIME_ISO
?
865 TIME_ISO_SHORT
: ctl
->time_mode
,
866 shadow
->sp_expire
* 86400);
869 /* sp_lstchg is specified in days, showing hours
870 * (especially in non-GMT timezones) would only serve
873 user
->pwd_ctime
= make_time(ctl
->time_mode
== TIME_ISO
?
874 TIME_ISO_SHORT
: ctl
->time_mode
,
875 shadow
->sp_lstchg
* 86400);
877 case COL_PWD_CTIME_MIN
:
878 if (shadow
&& shadow
->sp_min
> 0)
879 xasprintf(&user
->pwd_ctime_min
, "%ld", shadow
->sp_min
);
881 case COL_PWD_CTIME_MAX
:
882 if (shadow
&& shadow
->sp_max
> 0)
883 xasprintf(&user
->pwd_ctime_max
, "%ld", shadow
->sp_max
);
886 #ifdef HAVE_LIBSELINUX
887 if (ctl
->selinux_enabled
) {
888 /* typedefs and pointers are pure evil */
889 security_context_t con
= NULL
;
890 if (getcon(&con
) == 0)
896 xasprintf(&user
->nprocs
, "%d", get_nprocs(pwd
->pw_uid
));
899 /* something went very wrong here */
900 err(EXIT_FAILURE
, "fatal: unknown error");
908 static int str_to_uint(char *s
, unsigned int *ul
)
913 *ul
= strtoul(s
, &end
, 0);
919 /* get a definitive list of users we want info about... */
920 static int get_ulist(struct lslogins_control
*ctl
, char *logins
, char *groups
)
923 size_t i
= 0, n
= 0, *arsiz
;
933 /* an arbitrary starting value */
935 *ar
= xcalloc(1, sizeof(char *) * (*arsiz
));
938 while ((u
= strtok(logins
, ","))) {
941 /* user specified by UID? */
942 if (!str_to_uint(u
, &uid
)) {
948 (*ar
)[i
++] = xstrdup(u
);
951 *ar
= xrealloc(*ar
, sizeof(char *) * (*arsiz
+= 32));
957 /* FIXME: this might lead to duplicate entries, although not visible
958 * in output, crunching a user's info multiple times is very redundant */
959 while ((g
= strtok(groups
, ","))) {
963 /* user specified by GID? */
964 if (!str_to_uint(g
, &gid
))
972 while ((u
= grp
->gr_mem
[n
++])) {
973 (*ar
)[i
++] = xstrdup(u
);
976 *ar
= xrealloc(*ar
, sizeof(char *) * (*arsiz
+= 32));
985 static void free_ctl(struct lslogins_control
*ctl
)
992 while (n
< ctl
->ulsiz
)
993 free(ctl
->ulist
[n
++]);
999 static struct lslogins_user
*get_next_user(struct lslogins_control
*ctl
)
1001 struct lslogins_user
*u
;
1003 while (!(u
= get_user_info(ctl
, NULL
))) {
1004 /* no "false" errno-s here, iff we're unable to
1005 * get a valid user entry for any reason, quit */
1006 if (errno
== EAGAIN
)
1013 /* some UNIX implementations set errno iff a passwd/grp/...
1014 * entry was not found. The original UNIX logins(1) utility always
1015 * ignores invalid login/group names, so we're going to as well.*/
1016 #define IS_REAL_ERRNO(e) !((e) == ENOENT || (e) == ESRCH || \
1017 (e) == EBADF || (e) == EPERM || (e) == EAGAIN)
1019 static int get_user(struct lslogins_control
*ctl
, struct lslogins_user
**user
,
1020 const char *username
)
1022 *user
= get_user_info(ctl
, username
);
1023 if (!*user
&& IS_REAL_ERRNO(errno
))
1028 static int cmp_uid(const void *a
, const void *b
)
1030 uid_t x
= ((const struct lslogins_user
*)a
)->uid
;
1031 uid_t z
= ((const struct lslogins_user
*)b
)->uid
;
1032 return x
> z
? 1 : (x
< z
? -1 : 0);
1035 static int create_usertree(struct lslogins_control
*ctl
)
1037 struct lslogins_user
*user
= NULL
;
1040 if (ctl
->ulist_on
) {
1041 for (n
= 0; n
< ctl
->ulsiz
; n
++) {
1042 int rc
= get_user(ctl
, &user
, ctl
->ulist
[n
]);
1044 if (ctl
->fail_on_unknown
&& !user
) {
1045 warnx(_("cannot found '%s'"), ctl
->ulist
[n
]);
1051 tsearch(user
, &ctl
->usertree
, cmp_uid
);
1054 while ((user
= get_next_user(ctl
)))
1055 tsearch(user
, &ctl
->usertree
, cmp_uid
);
1060 static struct libscols_table
*setup_table(struct lslogins_control
*ctl
)
1062 struct libscols_table
*table
= scols_new_table();
1066 err(EXIT_FAILURE
, _("failed to allocate output table"));
1067 if (ctl
->noheadings
)
1068 scols_table_enable_noheadings(table
, 1);
1072 scols_table_enable_raw(table
, 1);
1073 scols_table_set_column_separator(table
, ":");
1076 scols_table_set_column_separator(table
, "\n");
1079 scols_table_enable_export(table
, 1);
1082 scols_table_set_line_separator(table
, "\0");
1085 scols_table_enable_raw(table
, 1);
1088 scols_table_enable_noheadings(table
, 1);
1093 while (n
< ncolumns
) {
1094 int flags
= coldescs
[columns
[n
]].flag
;
1097 flags
&= ~SCOLS_FL_TRUNC
;
1099 if (!scols_table_new_column(table
,
1100 coldescs
[columns
[n
]].name
,
1101 coldescs
[columns
[n
]].whint
,
1109 scols_unref_table(table
);
1113 static void fill_table(const void *u
, const VISIT which
, const int depth
__attribute__((unused
)))
1115 struct libscols_line
*ln
;
1116 const struct lslogins_user
*user
= *(struct lslogins_user
* const *)u
;
1119 if (which
== preorder
|| which
== endorder
)
1122 ln
= scols_table_new_line(tb
, NULL
);
1124 err(EXIT_FAILURE
, _("failed to allocate output line"));
1126 while (n
< ncolumns
) {
1129 switch (columns
[n
]) {
1131 rc
= scols_line_set_data(ln
, n
, user
->login
);
1134 rc
= scols_line_refer_data(ln
, n
, uidtostr(user
->uid
));
1137 rc
= scols_line_set_data(ln
, n
, get_status(user
->pwd_empty
));
1140 rc
= scols_line_set_data(ln
, n
, get_status(user
->nologin
));
1143 rc
= scols_line_set_data(ln
, n
, get_status(user
->pwd_lock
));
1146 rc
= scols_line_set_data(ln
, n
, get_status(user
->pwd_deny
));
1149 rc
= scols_line_set_data(ln
, n
, user
->pwd_method
);
1152 rc
= scols_line_set_data(ln
, n
, user
->group
);
1155 rc
= scols_line_refer_data(ln
, n
, gidtostr(user
->gid
));
1158 rc
= scols_line_refer_data(ln
, n
,
1159 build_sgroups_string(user
->sgroups
,
1164 rc
= scols_line_refer_data(ln
, n
,
1165 build_sgroups_string(user
->sgroups
,
1170 rc
= scols_line_set_data(ln
, n
, user
->homedir
);
1173 rc
= scols_line_set_data(ln
, n
, user
->shell
);
1176 rc
= scols_line_set_data(ln
, n
, user
->gecos
);
1178 case COL_LAST_LOGIN
:
1179 rc
= scols_line_set_data(ln
, n
, user
->last_login
);
1182 rc
= scols_line_set_data(ln
, n
, user
->last_tty
);
1184 case COL_LAST_HOSTNAME
:
1185 rc
= scols_line_set_data(ln
, n
, user
->last_hostname
);
1187 case COL_FAILED_LOGIN
:
1188 rc
= scols_line_set_data(ln
, n
, user
->failed_login
);
1190 case COL_FAILED_TTY
:
1191 rc
= scols_line_set_data(ln
, n
, user
->failed_tty
);
1193 case COL_HUSH_STATUS
:
1194 rc
= scols_line_set_data(ln
, n
, get_status(user
->hushed
));
1197 rc
= scols_line_set_data(ln
, n
, user
->pwd_warn
);
1200 rc
= scols_line_set_data(ln
, n
, user
->pwd_expire
);
1203 rc
= scols_line_set_data(ln
, n
, user
->pwd_ctime
);
1205 case COL_PWD_CTIME_MIN
:
1206 rc
= scols_line_set_data(ln
, n
, user
->pwd_ctime_min
);
1208 case COL_PWD_CTIME_MAX
:
1209 rc
= scols_line_set_data(ln
, n
, user
->pwd_ctime_max
);
1212 #ifdef HAVE_LIBSELINUX
1213 rc
= scols_line_set_data(ln
, n
, user
->context
);
1217 rc
= scols_line_set_data(ln
, n
, user
->nprocs
);
1220 /* something went very wrong here */
1221 err(EXIT_FAILURE
, _("internal error: unknown column"));
1225 err(EXIT_FAILURE
, _("failed to add output data"));
1229 #ifdef HAVE_LIBSYSTEMD
1230 static void print_journal_tail(const char *journal_path
, uid_t uid
, size_t len
, int time_mode
)
1233 char *match
, *timestamp
;
1236 const char *identifier
, *pid
, *message
;
1237 size_t identifier_len
, pid_len
, message_len
;
1240 sd_journal_open_directory(&j
, journal_path
, 0);
1242 sd_journal_open(&j
, SD_JOURNAL_LOCAL_ONLY
);
1244 xasprintf(&match
, "_UID=%d", uid
);
1246 sd_journal_add_match(j
, match
, 0);
1247 sd_journal_seek_tail(j
);
1248 sd_journal_previous_skip(j
, len
);
1251 if (0 > sd_journal_get_data(j
, "SYSLOG_IDENTIFIER",
1252 (const void **) &identifier
, &identifier_len
))
1254 if (0 > sd_journal_get_data(j
, "_PID",
1255 (const void **) &pid
, &pid_len
))
1257 if (0 > sd_journal_get_data(j
, "MESSAGE",
1258 (const void **) &message
, &message_len
))
1261 sd_journal_get_realtime_usec(j
, &x
);
1263 timestamp
= make_time(time_mode
, t
);
1264 /* Get rid of journal entry field identifiers */
1265 identifier
= strchr(identifier
, '=') + 1;
1266 pid
= strchr(pid
, '=') + 1;
1267 message
= strchr(message
, '=') + 1;
1269 fprintf(stdout
, "%s %s[%s]: %s\n", timestamp
, identifier
, pid
,
1272 } while (sd_journal_next(j
));
1276 sd_journal_flush_matches(j
);
1277 sd_journal_close(j
);
1281 static int print_pretty(struct libscols_table
*table
)
1283 struct libscols_iter
*itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
1284 struct libscols_column
*col
;
1285 struct libscols_cell
*data
;
1286 struct libscols_line
*ln
;
1287 const char *hstr
, *dstr
;
1290 ln
= scols_table_get_line(table
, 0);
1291 while (!scols_table_next_column(table
, itr
, &col
)) {
1293 data
= scols_line_get_cell(ln
, n
);
1295 hstr
= _(coldescs
[columns
[n
]].pretty_name
);
1296 dstr
= scols_cell_get_data(data
);
1299 printf("%s:%*c%-36s\n", hstr
, 35 - (int)strlen(hstr
), ' ', dstr
);
1303 scols_free_iter(itr
);
1308 static int print_user_table(struct lslogins_control
*ctl
)
1310 tb
= setup_table(ctl
);
1314 twalk(ctl
->usertree
, fill_table
);
1315 if (outmode
== OUT_PRETTY
) {
1317 #ifdef HAVE_LIBSYSTEMD
1318 fprintf(stdout
, _("\nLast logs:\n"));
1319 print_journal_tail(ctl
->journal_path
, ctl
->uid
, 3, ctl
->time_mode
);
1320 fputc('\n', stdout
);
1323 scols_print_table(tb
);
1327 static void free_user(void *f
)
1329 struct lslogins_user
*u
= f
;
1336 free(u
->pwd_ctime_min
);
1337 free(u
->pwd_ctime_max
);
1338 free(u
->last_login
);
1340 free(u
->last_hostname
);
1341 free(u
->failed_login
);
1342 free(u
->failed_tty
);
1345 free(u
->pwd_status
);
1346 #ifdef HAVE_LIBSELINUX
1347 freecon(u
->context
);
1352 static int parse_time_mode(const char *s
)
1354 struct lslogins_timefmt
{
1358 static const struct lslogins_timefmt timefmts
[] = {
1360 {"full", TIME_FULL
},
1361 {"short", TIME_SHORT
},
1365 for (i
= 0; i
< ARRAY_SIZE(timefmts
); i
++) {
1366 if (strcmp(timefmts
[i
].name
, s
) == 0)
1367 return timefmts
[i
].val
;
1369 errx(EXIT_FAILURE
, _("unknown time format: %s"), s
);
1372 static void __attribute__((__noreturn__
)) usage(void)
1377 fputs(USAGE_HEADER
, out
);
1378 fprintf(out
, _(" %s [options] [<username>]\n"), program_invocation_short_name
);
1380 fputs(USAGE_SEPARATOR
, out
);
1381 fputs(_("Display information about known users in the system.\n"), out
);
1383 fputs(USAGE_OPTIONS
, out
);
1384 fputs(_(" -a, --acc-expiration display info about passwords expiration\n"), out
);
1385 fputs(_(" -c, --colon-separate display data in a format similar to /etc/passwd\n"), out
);
1386 fputs(_(" -e, --export display in an export-able output format\n"), out
);
1387 fputs(_(" -f, --failed display data about the users' last failed logins\n"), out
);
1388 fputs(_(" -G, --supp-groups display information about groups\n"), out
);
1389 fputs(_(" -g, --groups=<groups> display users belonging to a group in <groups>\n"), out
);
1390 fputs(_(" -L, --last show info about the users' last login sessions\n"), out
);
1391 fputs(_(" -l, --logins=<logins> display only users from <logins>\n"), out
);
1392 fputs(_(" -n, --newline display each piece of information on a new line\n"), out
);
1393 fputs(_(" --noheadings don't print headings\n"), out
);
1394 fputs(_(" --notruncate don't truncate output\n"), out
);
1395 fputs(_(" -o, --output[=<list>] define the columns to output\n"), out
);
1396 fputs(_(" --output-all output all columns\n"), out
);
1397 fputs(_(" -p, --pwd display information related to login by password.\n"), out
);
1398 fputs(_(" -r, --raw display in raw mode\n"), out
);
1399 fputs(_(" -s, --system-accs display system accounts\n"), out
);
1400 fputs(_(" --time-format=<type> display dates in short, full or iso format\n"), out
);
1401 fputs(_(" -u, --user-accs display user accounts\n"), out
);
1402 fputs(_(" -Z, --context display SELinux contexts\n"), out
);
1403 fputs(_(" -z, --print0 delimit user entries with a nul character\n"), out
);
1404 fputs(_(" --wtmp-file <path> set an alternate path for wtmp\n"), out
);
1405 fputs(_(" --btmp-file <path> set an alternate path for btmp\n"), out
);
1406 fputs(_(" --lastlog <path> set an alternate path for lastlog\n"), out
);
1407 fputs(USAGE_SEPARATOR
, out
);
1408 printf(USAGE_HELP_OPTIONS(26));
1410 fputs(USAGE_COLUMNS
, out
);
1411 for (i
= 0; i
< ARRAY_SIZE(coldescs
); i
++)
1412 fprintf(out
, " %14s %s\n", coldescs
[i
].name
, _(coldescs
[i
].help
));
1414 printf(USAGE_MAN_TAIL("lslogins(1)"));
1419 int main(int argc
, char *argv
[])
1422 char *logins
= NULL
, *groups
= NULL
, *outarg
= NULL
;
1423 char *path_lastlog
= _PATH_LASTLOG
, *path_wtmp
= _PATH_WTMP
, *path_btmp
= _PATH_BTMP
;
1424 struct lslogins_control
*ctl
= xcalloc(1, sizeof(struct lslogins_control
));
1427 /* long only options. */
1429 OPT_WTMP
= CHAR_MAX
+ 1,
1438 static const struct option longopts
[] = {
1439 { "acc-expiration", no_argument
, 0, 'a' },
1440 { "colon-separate", no_argument
, 0, 'c' },
1441 { "export", no_argument
, 0, 'e' },
1442 { "failed", no_argument
, 0, 'f' },
1443 { "groups", required_argument
, 0, 'g' },
1444 { "help", no_argument
, 0, 'h' },
1445 { "logins", required_argument
, 0, 'l' },
1446 { "supp-groups", no_argument
, 0, 'G' },
1447 { "newline", no_argument
, 0, 'n' },
1448 { "notruncate", no_argument
, 0, OPT_NOTRUNC
},
1449 { "noheadings", no_argument
, 0, OPT_NOHEAD
},
1450 { "output", required_argument
, 0, 'o' },
1451 { "output-all", no_argument
, 0, OPT_OUTPUT_ALL
},
1452 { "last", no_argument
, 0, 'L', },
1453 { "raw", no_argument
, 0, 'r' },
1454 { "system-accs", no_argument
, 0, 's' },
1455 { "time-format", required_argument
, 0, OPT_TIME_FMT
},
1456 { "user-accs", no_argument
, 0, 'u' },
1457 { "version", no_argument
, 0, 'V' },
1458 { "pwd", no_argument
, 0, 'p' },
1459 { "print0", no_argument
, 0, 'z' },
1460 { "wtmp-file", required_argument
, 0, OPT_WTMP
},
1461 { "btmp-file", required_argument
, 0, OPT_BTMP
},
1462 { "lastlog-file", required_argument
, 0, OPT_LASTLOG
},
1463 #ifdef HAVE_LIBSELINUX
1464 { "context", no_argument
, 0, 'Z' },
1469 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
1474 { 'c','n','r','z' },
1478 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
1480 setlocale(LC_ALL
, "");
1481 bindtextdomain(PACKAGE
, LOCALEDIR
);
1482 textdomain(PACKAGE
);
1483 close_stdout_atexit();
1485 ctl
->time_mode
= TIME_SHORT
;
1487 /* very basic default */
1488 add_column(columns
, ncolumns
++, COL_UID
);
1489 add_column(columns
, ncolumns
++, COL_USER
);
1491 while ((c
= getopt_long(argc
, argv
, "acefGg:hLl:no:prsuVzZ",
1492 longopts
, NULL
)) != -1) {
1494 err_exclusive_options(c
, longopts
, excl
, excl_st
);
1498 add_column(columns
, ncolumns
++, COL_PWD_WARN
);
1499 add_column(columns
, ncolumns
++, COL_PWD_CTIME_MIN
);
1500 add_column(columns
, ncolumns
++, COL_PWD_CTIME_MAX
);
1501 add_column(columns
, ncolumns
++, COL_PWD_CTIME
);
1502 add_column(columns
, ncolumns
++, COL_PWD_EXPIR
);
1505 outmode
= OUT_COLON
;
1508 outmode
= OUT_EXPORT
;
1511 add_column(columns
, ncolumns
++, COL_FAILED_LOGIN
);
1512 add_column(columns
, ncolumns
++, COL_FAILED_TTY
);
1515 add_column(columns
, ncolumns
++, COL_GID
);
1516 add_column(columns
, ncolumns
++, COL_GROUP
);
1517 add_column(columns
, ncolumns
++, COL_SGIDS
);
1518 add_column(columns
, ncolumns
++, COL_SGROUPS
);
1527 add_column(columns
, ncolumns
++, COL_LAST_TTY
);
1528 add_column(columns
, ncolumns
++, COL_LAST_HOSTNAME
);
1529 add_column(columns
, ncolumns
++, COL_LAST_LOGIN
);
1535 outmode
= OUT_NEWLINE
;
1542 case OPT_OUTPUT_ALL
:
1543 for (ncolumns
= 0; ncolumns
< ARRAY_SIZE(coldescs
); ncolumns
++)
1544 columns
[ncolumns
] = ncolumns
;
1550 ctl
->SYS_UID_MIN
= getlogindefs_num("SYS_UID_MIN", UL_SYS_UID_MIN
);
1551 ctl
->SYS_UID_MAX
= getlogindefs_num("SYS_UID_MAX", UL_SYS_UID_MAX
);
1552 lslogins_flag
|= F_SYSAC
;
1555 ctl
->UID_MIN
= getlogindefs_num("UID_MIN", UL_UID_MIN
);
1556 ctl
->UID_MAX
= getlogindefs_num("UID_MAX", UL_UID_MAX
);
1557 lslogins_flag
|= F_USRAC
;
1560 add_column(columns
, ncolumns
++, COL_PWDEMPTY
);
1561 add_column(columns
, ncolumns
++, COL_PWDLOCK
);
1562 add_column(columns
, ncolumns
++, COL_PWDDENY
);
1563 add_column(columns
, ncolumns
++, COL_NOLOGIN
);
1564 add_column(columns
, ncolumns
++, COL_HUSH_STATUS
);
1565 add_column(columns
, ncolumns
++, COL_PWDMETHOD
);
1571 path_lastlog
= optarg
;
1583 ctl
->noheadings
= 1;
1586 ctl
->time_mode
= parse_time_mode(optarg
);
1589 print_version(EXIT_SUCCESS
);
1592 #ifdef HAVE_LIBSELINUX
1593 int sl
= is_selinux_enabled();
1595 warn(_("failed to request selinux state"));
1597 ctl
->selinux_enabled
= sl
== 1;
1599 add_column(columns
, ncolumns
++, COL_SELINUX
);
1603 errtryhelp(EXIT_FAILURE
);
1607 if (argc
- optind
== 1) {
1608 if (strchr(argv
[optind
], ','))
1609 errx(EXIT_FAILURE
, _("Only one user may be specified. Use -l for multiple users."));
1610 logins
= argv
[optind
];
1611 outmode
= OUT_PRETTY
;
1612 ctl
->fail_on_unknown
= 1;
1613 } else if (argc
!= optind
)
1614 errx(EXIT_FAILURE
, _("Only one user may be specified. Use -l for multiple users."));
1616 scols_init_debug(0);
1618 /* lslogins -u -s == lslogins */
1619 if (lslogins_flag
& F_USRAC
&& lslogins_flag
& F_SYSAC
)
1620 lslogins_flag
&= ~(F_USRAC
| F_SYSAC
);
1622 if (outmode
== OUT_PRETTY
) {
1623 /* all columns for lslogins <username> */
1624 for (ncolumns
= 0, i
= 0; i
< ARRAY_SIZE(coldescs
); i
++)
1625 columns
[ncolumns
++] = i
;
1627 } else if (ncolumns
== 2) {
1628 /* default columns */
1629 add_column(columns
, ncolumns
++, COL_NPROCS
);
1630 add_column(columns
, ncolumns
++, COL_PWDLOCK
);
1631 add_column(columns
, ncolumns
++, COL_PWDDENY
);
1632 add_column(columns
, ncolumns
++, COL_LAST_LOGIN
);
1633 add_column(columns
, ncolumns
++, COL_GECOS
);
1636 if (outarg
&& string_add_to_idarray(outarg
, columns
, ARRAY_SIZE(columns
),
1637 &ncolumns
, column_name_to_id
) < 0)
1638 return EXIT_FAILURE
;
1640 if (require_wtmp()) {
1641 parse_wtmp(ctl
, path_wtmp
);
1642 ctl
->lastlogin_fd
= open(path_lastlog
, O_RDONLY
, 0);
1645 parse_btmp(ctl
, path_btmp
);
1647 if (logins
|| groups
)
1648 get_ulist(ctl
, logins
, groups
);
1650 if (create_usertree(ctl
))
1651 return EXIT_FAILURE
;
1653 print_user_table(ctl
);
1655 scols_unref_table(tb
);
1656 tdestroy(ctl
->usertree
, free_user
);
1657 close(ctl
->lastlogin_fd
);
1660 return EXIT_SUCCESS
;