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
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 parse_utmpx(const char *path
, size_t *nrecords
, struct utmpx
**records
)
480 struct utmpx
*ary
= NULL
;
486 if (utmpxname(path
) < 0)
489 /* optimize allocation according to file size, the realloc() below is
490 * just fallback only */
491 if (stat(path
, &st
) == 0 && (size_t) st
.st_size
> sizeof(struct utmpx
)) {
492 imax
= st
.st_size
/ sizeof(struct utmpx
);
493 ary
= xmalloc(imax
* sizeof(struct utmpx
));
506 ary
= xrealloc(ary
, (imax
*= 2) * sizeof(struct utmpx
));
519 err(EXIT_FAILURE
, "%s", path
);
525 static void get_lastlog(struct lslogins_control
*ctl
, uid_t uid
, void *dst
, int what
)
529 if (ctl
->lastlogin_fd
< 0 ||
530 pread(ctl
->lastlogin_fd
, (void *)&ll
, sizeof(ll
), uid
* sizeof(ll
)) != sizeof(ll
))
535 time_t *t
= (time_t *)dst
;
540 mem2strcpy(dst
, ll
.ll_line
, sizeof(ll
.ll_line
), sizeof(ll
.ll_line
) + 1);
543 mem2strcpy(dst
, ll
.ll_host
, sizeof(ll
.ll_host
), sizeof(ll
.ll_host
) + 1);
550 static int get_sgroups(gid_t
**list
, size_t *len
, struct passwd
*pwd
)
558 /* first let's get a supp. group count */
559 getgrouplist(pwd
->pw_name
, pwd
->pw_gid
, *list
, &ngroups
);
563 *list
= xcalloc(1, ngroups
* sizeof(gid_t
));
565 fprintf(stderr
, "KZAK>>> alloc '%p' for %s\n", *list
, pwd
->pw_name
);
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
)];
588 static int get_nprocs(const uid_t uid
)
592 struct proc_processes
*proc
= proc_open_processes();
594 proc_processes_filter_by_uid(proc
, uid
);
596 while (!proc_next_pid(proc
, &pid
))
599 proc_close_processes(proc
);
604 static const char *get_pwd_method(const char *str
, const char **next
, unsigned int *sz
)
607 const char *res
= NULL
;
609 if (!p
|| *p
++ != '$')
623 if (*p
== 'a' || *p
== 'y')
648 #define is_valid_pwd_char(x) (isalnum((unsigned char) (x)) || (x) == '.' || (x) == '/')
651 * This function do not accept empty passwords or locked accouns.
653 static int valid_pwd(const char *str
)
656 unsigned int sz
= 0, n
;
662 if (get_pwd_method(str
, &p
, &sz
) == NULL
)
673 if (!is_valid_pwd_char(*p
))
680 for (n
= 0; *p
; p
++, n
++) {
681 if (!is_valid_pwd_char(*p
))
690 static struct lslogins_user
*get_user_info(struct lslogins_control
*ctl
, const char *username
)
692 struct lslogins_user
*user
;
696 struct utmpx
*user_wtmp
= NULL
, *user_btmp
= NULL
;
703 pwd
= username
? getpwnam(username
) : getpwent();
707 ctl
->uid
= uid
= pwd
->pw_uid
;
709 /* nfsnobody is an exception to the UID_MAX limit. This is "nobody" on
710 * some systems; the decisive point is the UID - 65534 */
711 if ((lslogins_flag
& F_USRAC
) &&
712 strcmp("nfsnobody", pwd
->pw_name
) != 0 &&
714 if (uid
< ctl
->UID_MIN
|| uid
> ctl
->UID_MAX
) {
719 } else if ((lslogins_flag
& F_SYSAC
) &&
720 (uid
< ctl
->SYS_UID_MIN
|| uid
> ctl
->SYS_UID_MAX
)) {
726 grp
= getgrgid(pwd
->pw_gid
);
730 user
= xcalloc(1, sizeof(struct lslogins_user
));
733 user_wtmp
= get_last_wtmp(ctl
, pwd
->pw_name
);
735 user_btmp
= get_last_btmp(ctl
, pwd
->pw_name
);
738 shadow
= getspnam(pwd
->pw_name
);
741 /* required by tseach() stuff */
742 user
->uid
= pwd
->pw_uid
;
744 while (n
< ncolumns
) {
745 switch (columns
[n
++]) {
747 user
->login
= xstrdup(pwd
->pw_name
);
750 user
->uid
= pwd
->pw_uid
;
753 user
->group
= xstrdup(grp
->gr_name
);
756 user
->gid
= pwd
->pw_gid
;
760 if (!user
->nsgroups
&&
761 get_sgroups(&user
->sgroups
, &user
->nsgroups
, pwd
) < 0)
762 err(EXIT_FAILURE
, _("failed to get supplementary groups"));
765 user
->homedir
= xstrdup(pwd
->pw_dir
);
768 user
->shell
= xstrdup(pwd
->pw_shell
);
771 user
->gecos
= xstrdup(pwd
->pw_gecos
);
775 time
= user_wtmp
->ut_tv
.tv_sec
;
776 user
->last_login
= make_time(ctl
->time_mode
, time
);
779 get_lastlog(ctl
, pwd
->pw_uid
, &time
, LASTLOG_TIME
);
781 user
->last_login
= make_time(ctl
->time_mode
, time
);
785 user
->last_tty
= xcalloc(1, sizeof(user_wtmp
->ut_line
) + 1);
787 mem2strcpy(user
->last_tty
, user_wtmp
->ut_line
,
788 sizeof(user_wtmp
->ut_line
),
789 sizeof(user_wtmp
->ut_line
) + 1);;
791 get_lastlog(ctl
, user
->uid
, user
->last_tty
, LASTLOG_LINE
);
793 case COL_LAST_HOSTNAME
:
794 user
->last_hostname
= xcalloc(1, sizeof(user_wtmp
->ut_host
) + 1);
796 mem2strcpy(user
->last_hostname
, user_wtmp
->ut_host
,
797 sizeof(user_wtmp
->ut_host
),
798 sizeof(user_wtmp
->ut_host
) + 1);;
800 get_lastlog(ctl
, user
->uid
, user
->last_hostname
, LASTLOG_HOST
);
802 case COL_FAILED_LOGIN
:
804 time
= user_btmp
->ut_tv
.tv_sec
;
805 user
->failed_login
= make_time(ctl
->time_mode
, time
);
810 user
->failed_tty
= xmalloc(sizeof(user_btmp
->ut_line
) + 1);
811 mem2strcpy(user
->failed_tty
, user_btmp
->ut_line
,
812 sizeof(user_btmp
->ut_line
),
813 sizeof(user_btmp
->ut_line
) + 1);;
816 case COL_HUSH_STATUS
:
817 user
->hushed
= get_hushlogin_status(pwd
, 0);
818 if (user
->hushed
== -1)
819 user
->hushed
= STATUS_UNKNOWN
;
823 if (!*shadow
->sp_pwdp
) /* '\0' */
824 user
->pwd_empty
= STATUS_TRUE
;
826 user
->pwd_empty
= STATUS_UNKNOWN
;
830 if ((*shadow
->sp_pwdp
== '!' ||
831 *shadow
->sp_pwdp
== '*') &&
832 !valid_pwd(shadow
->sp_pwdp
+ 1))
833 user
->pwd_deny
= STATUS_TRUE
;
835 user
->pwd_deny
= STATUS_UNKNOWN
;
839 if (*shadow
->sp_pwdp
== '!' && valid_pwd(shadow
->sp_pwdp
+ 1))
840 user
->pwd_lock
= STATUS_TRUE
;
842 user
->pwd_lock
= STATUS_UNKNOWN
;
846 const char *p
= shadow
->sp_pwdp
;
848 if (*p
== '!' || *p
== '*')
850 user
->pwd_method
= get_pwd_method(p
, NULL
, NULL
);
852 user
->pwd_method
= NULL
;
855 if (strstr(pwd
->pw_shell
, "nologin"))
857 else if (pwd
->pw_uid
)
858 user
->nologin
= access(_PATH_NOLOGIN
, F_OK
) == 0 ||
859 access(_PATH_VAR_NOLOGIN
, F_OK
) == 0;
862 if (shadow
&& shadow
->sp_warn
>= 0)
863 xasprintf(&user
->pwd_warn
, "%ld", shadow
->sp_warn
);
866 if (shadow
&& shadow
->sp_expire
>= 0)
867 user
->pwd_expire
= make_time(ctl
->time_mode
== TIME_ISO
?
868 TIME_ISO_SHORT
: ctl
->time_mode
,
869 shadow
->sp_expire
* 86400);
872 /* sp_lstchg is specified in days, showing hours
873 * (especially in non-GMT timezones) would only serve
876 user
->pwd_ctime
= make_time(ctl
->time_mode
== TIME_ISO
?
877 TIME_ISO_SHORT
: ctl
->time_mode
,
878 shadow
->sp_lstchg
* 86400);
880 case COL_PWD_CTIME_MIN
:
881 if (shadow
&& shadow
->sp_min
> 0)
882 xasprintf(&user
->pwd_ctime_min
, "%ld", shadow
->sp_min
);
884 case COL_PWD_CTIME_MAX
:
885 if (shadow
&& shadow
->sp_max
> 0)
886 xasprintf(&user
->pwd_ctime_max
, "%ld", shadow
->sp_max
);
889 #ifdef HAVE_LIBSELINUX
890 if (!ctl
->selinux_enabled
|| getcon(&user
->context
) != 0)
891 user
->context
= NULL
;
897 xasprintf(&user
->nprocs
, "%d", get_nprocs(pwd
->pw_uid
));
901 /* something went very wrong here */
902 err(EXIT_FAILURE
, "fatal: unknown error");
910 static int str_to_uint(char *s
, unsigned int *ul
)
918 *ul
= strtoul(s
, &end
, 0);
919 if (errno
== 0 && end
&& !*end
)
924 /* get a definitive list of users we want info about... */
925 static int get_ulist(struct lslogins_control
*ctl
, char *logins
, char *groups
)
928 size_t i
= 0, n
= 0, *arsiz
;
938 /* an arbitrary starting value */
940 *ar
= xcalloc(1, sizeof(char *) * (*arsiz
));
943 while ((u
= strtok(logins
, ","))) {
946 /* user specified by UID? */
947 if (!str_to_uint(u
, &uid
)) {
953 (*ar
)[i
++] = xstrdup(u
);
956 *ar
= xrealloc(*ar
, sizeof(char *) * (*arsiz
+= 32));
962 /* FIXME: this might lead to duplicate entries, although not visible
963 * in output, crunching a user's info multiple times is very redundant */
964 while ((g
= strtok(groups
, ","))) {
968 /* user specified by GID? */
969 if (!str_to_uint(g
, &gid
))
977 while ((u
= grp
->gr_mem
[n
++])) {
978 (*ar
)[i
++] = xstrdup(u
);
981 *ar
= xrealloc(*ar
, sizeof(char *) * (*arsiz
+= 32));
990 static void free_ctl(struct lslogins_control
*ctl
)
997 while (n
< ctl
->ulsiz
)
998 free(ctl
->ulist
[n
++]);
1004 static struct lslogins_user
*get_next_user(struct lslogins_control
*ctl
)
1006 struct lslogins_user
*u
;
1008 while (!(u
= get_user_info(ctl
, NULL
))) {
1009 /* no "false" errno-s here, iff we're unable to
1010 * get a valid user entry for any reason, quit */
1011 if (errno
== EAGAIN
)
1018 /* some UNIX implementations set errno iff a passwd/grp/...
1019 * entry was not found. The original UNIX logins(1) utility always
1020 * ignores invalid login/group names, so we're going to as well.*/
1021 #define IS_REAL_ERRNO(e) !((e) == ENOENT || (e) == ESRCH || \
1022 (e) == EBADF || (e) == EPERM || (e) == EAGAIN)
1024 static int get_user(struct lslogins_control
*ctl
, struct lslogins_user
**user
,
1025 const char *username
)
1027 *user
= get_user_info(ctl
, username
);
1028 if (!*user
&& IS_REAL_ERRNO(errno
))
1033 static int cmp_uid(const void *a
, const void *b
)
1035 uid_t x
= ((const struct lslogins_user
*)a
)->uid
;
1036 uid_t z
= ((const struct lslogins_user
*)b
)->uid
;
1037 return x
> z
? 1 : (x
< z
? -1 : 0);
1040 static int create_usertree(struct lslogins_control
*ctl
)
1042 struct lslogins_user
*user
= NULL
;
1045 if (ctl
->ulist_on
) {
1046 for (n
= 0; n
< ctl
->ulsiz
; n
++) {
1047 int rc
= get_user(ctl
, &user
, ctl
->ulist
[n
]);
1049 if (ctl
->fail_on_unknown
&& !user
) {
1050 warnx(_("cannot found '%s'"), ctl
->ulist
[n
]);
1055 tsearch(user
, &ctl
->usertree
, cmp_uid
);
1058 while ((user
= get_next_user(ctl
)))
1059 tsearch(user
, &ctl
->usertree
, cmp_uid
);
1064 static struct libscols_table
*setup_table(struct lslogins_control
*ctl
)
1066 struct libscols_table
*table
= scols_new_table();
1070 err(EXIT_FAILURE
, _("failed to allocate output table"));
1071 if (ctl
->noheadings
)
1072 scols_table_enable_noheadings(table
, 1);
1076 scols_table_enable_raw(table
, 1);
1077 scols_table_set_column_separator(table
, ":");
1080 scols_table_set_column_separator(table
, "\n");
1083 scols_table_enable_export(table
, 1);
1086 scols_table_set_line_separator(table
, "\0");
1089 scols_table_enable_raw(table
, 1);
1092 scols_table_enable_noheadings(table
, 1);
1097 while (n
< ncolumns
) {
1098 int flags
= coldescs
[columns
[n
]].flag
;
1101 flags
&= ~SCOLS_FL_TRUNC
;
1103 if (!scols_table_new_column(table
,
1104 coldescs
[columns
[n
]].name
,
1105 coldescs
[columns
[n
]].whint
,
1113 scols_unref_table(table
);
1117 static void fill_table(const void *u
, const VISIT which
, const int depth
__attribute__((unused
)))
1119 struct libscols_line
*ln
;
1120 const struct lslogins_user
*user
= *(struct lslogins_user
* const *)u
;
1123 if (which
== preorder
|| which
== endorder
)
1126 ln
= scols_table_new_line(tb
, NULL
);
1128 err(EXIT_FAILURE
, _("failed to allocate output line"));
1130 while (n
< ncolumns
) {
1133 switch (columns
[n
]) {
1135 rc
= scols_line_set_data(ln
, n
, user
->login
);
1138 rc
= scols_line_refer_data(ln
, n
, uidtostr(user
->uid
));
1141 rc
= scols_line_set_data(ln
, n
, get_status(user
->pwd_empty
));
1144 rc
= scols_line_set_data(ln
, n
, get_status(user
->nologin
));
1147 rc
= scols_line_set_data(ln
, n
, get_status(user
->pwd_lock
));
1150 rc
= scols_line_set_data(ln
, n
, get_status(user
->pwd_deny
));
1153 rc
= scols_line_set_data(ln
, n
, user
->pwd_method
);
1156 rc
= scols_line_set_data(ln
, n
, user
->group
);
1159 rc
= scols_line_refer_data(ln
, n
, gidtostr(user
->gid
));
1162 rc
= scols_line_refer_data(ln
, n
,
1163 build_sgroups_string(user
->sgroups
,
1168 rc
= scols_line_refer_data(ln
, n
,
1169 build_sgroups_string(user
->sgroups
,
1174 rc
= scols_line_set_data(ln
, n
, user
->homedir
);
1177 rc
= scols_line_set_data(ln
, n
, user
->shell
);
1180 rc
= scols_line_set_data(ln
, n
, user
->gecos
);
1182 case COL_LAST_LOGIN
:
1183 rc
= scols_line_set_data(ln
, n
, user
->last_login
);
1186 rc
= scols_line_set_data(ln
, n
, user
->last_tty
);
1188 case COL_LAST_HOSTNAME
:
1189 rc
= scols_line_set_data(ln
, n
, user
->last_hostname
);
1191 case COL_FAILED_LOGIN
:
1192 rc
= scols_line_set_data(ln
, n
, user
->failed_login
);
1194 case COL_FAILED_TTY
:
1195 rc
= scols_line_set_data(ln
, n
, user
->failed_tty
);
1197 case COL_HUSH_STATUS
:
1198 rc
= scols_line_set_data(ln
, n
, get_status(user
->hushed
));
1201 rc
= scols_line_set_data(ln
, n
, user
->pwd_warn
);
1204 rc
= scols_line_set_data(ln
, n
, user
->pwd_expire
);
1207 rc
= scols_line_set_data(ln
, n
, user
->pwd_ctime
);
1209 case COL_PWD_CTIME_MIN
:
1210 rc
= scols_line_set_data(ln
, n
, user
->pwd_ctime_min
);
1212 case COL_PWD_CTIME_MAX
:
1213 rc
= scols_line_set_data(ln
, n
, user
->pwd_ctime_max
);
1216 #ifdef HAVE_LIBSELINUX
1217 rc
= scols_line_set_data(ln
, n
, user
->context
);
1223 rc
= scols_line_set_data(ln
, n
, user
->nprocs
);
1227 /* something went very wrong here */
1228 err(EXIT_FAILURE
, _("internal error: unknown column"));
1232 err(EXIT_FAILURE
, _("failed to add output data"));
1236 #ifdef HAVE_LIBSYSTEMD
1237 static char *get_journal_data(sd_journal
*j
, const char *name
)
1239 const char *data
= NULL
, *p
;
1242 if (sd_journal_get_data(j
, name
, (const void **) &data
, &len
) < 0
1246 /* Get rid of journal entry field identifiers */
1247 p
= strnchr(data
, len
, '=');
1248 if (!p
|| !*(p
+ 1))
1252 return xstrndup(p
, len
- (p
- data
));
1255 static void print_journal_tail(const char *journal_path
, uid_t uid
, size_t len
, int time_mode
)
1261 sd_journal_open_directory(&j
, journal_path
, 0);
1263 sd_journal_open(&j
, SD_JOURNAL_LOCAL_ONLY
);
1265 xasprintf(&match
, "_UID=%d", uid
);
1267 sd_journal_add_match(j
, match
, 0);
1268 sd_journal_seek_tail(j
);
1269 sd_journal_previous_skip(j
, len
);
1272 char *id
, *pid
, *msg
, *ts
;
1276 sd_journal_get_realtime_usec(j
, &x
);
1278 ts
= make_time(time_mode
, t
);
1280 id
= get_journal_data(j
, "SYSLOG_IDENTIFIER");
1281 pid
= get_journal_data(j
, "_PID");
1282 msg
= get_journal_data(j
, "MESSAGE");
1284 if (ts
&& id
&& pid
&& msg
)
1285 fprintf(stdout
, "%s %s[%s]: %s\n", ts
, id
, pid
, msg
);
1291 } while (sd_journal_next(j
));
1294 sd_journal_flush_matches(j
);
1295 sd_journal_close(j
);
1299 static int print_pretty(struct libscols_table
*table
)
1301 struct libscols_iter
*itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
1302 struct libscols_column
*col
;
1303 struct libscols_cell
*data
;
1304 struct libscols_line
*ln
;
1305 const char *hstr
, *dstr
;
1308 ln
= scols_table_get_line(table
, 0);
1309 while (!scols_table_next_column(table
, itr
, &col
)) {
1311 data
= scols_line_get_cell(ln
, n
);
1313 hstr
= _(coldescs
[columns
[n
]].pretty_name
);
1314 dstr
= scols_cell_get_data(data
);
1317 printf("%s:%*c%-36s\n", hstr
, 35 - (int)strlen(hstr
), ' ', dstr
);
1321 scols_free_iter(itr
);
1326 static int print_user_table(struct lslogins_control
*ctl
)
1328 tb
= setup_table(ctl
);
1332 twalk(ctl
->usertree
, fill_table
);
1333 if (outmode
== OUT_PRETTY
) {
1335 #ifdef HAVE_LIBSYSTEMD
1336 fprintf(stdout
, _("\nLast logs:\n"));
1337 print_journal_tail(ctl
->journal_path
, ctl
->uid
, 3, ctl
->time_mode
);
1338 fputc('\n', stdout
);
1341 scols_print_table(tb
);
1345 static void free_user(void *f
)
1347 struct lslogins_user
*u
= f
;
1355 free(u
->pwd_ctime_min
);
1356 free(u
->pwd_ctime_max
);
1357 free(u
->last_login
);
1359 free(u
->last_hostname
);
1360 free(u
->failed_login
);
1361 free(u
->failed_tty
);
1364 free(u
->pwd_status
);
1365 #ifdef HAVE_LIBSELINUX
1366 freecon(u
->context
);
1371 static int parse_time_mode(const char *s
)
1373 struct lslogins_timefmt
{
1377 static const struct lslogins_timefmt timefmts
[] = {
1379 {"full", TIME_FULL
},
1380 {"short", TIME_SHORT
},
1384 for (i
= 0; i
< ARRAY_SIZE(timefmts
); i
++) {
1385 if (strcmp(timefmts
[i
].name
, s
) == 0)
1386 return timefmts
[i
].val
;
1388 errx(EXIT_FAILURE
, _("unknown time format: %s"), s
);
1391 static void __attribute__((__noreturn__
)) usage(void)
1396 fputs(USAGE_HEADER
, out
);
1397 fprintf(out
, _(" %s [options] [<username>]\n"), program_invocation_short_name
);
1399 fputs(USAGE_SEPARATOR
, out
);
1400 fputs(_("Display information about known users in the system.\n"), out
);
1402 fputs(USAGE_OPTIONS
, out
);
1403 fputs(_(" -a, --acc-expiration display info about passwords expiration\n"), out
);
1404 fputs(_(" -c, --colon-separate display data in a format similar to /etc/passwd\n"), out
);
1405 fputs(_(" -e, --export display in an export-able output format\n"), out
);
1406 fputs(_(" -f, --failed display data about the users' last failed logins\n"), out
);
1407 fputs(_(" -G, --supp-groups display information about groups\n"), out
);
1408 fputs(_(" -g, --groups=<groups> display users belonging to a group in <groups>\n"), out
);
1409 fputs(_(" -L, --last show info about the users' last login sessions\n"), out
);
1410 fputs(_(" -l, --logins=<logins> display only users from <logins>\n"), out
);
1411 fputs(_(" -n, --newline display each piece of information on a new line\n"), out
);
1412 fputs(_(" --noheadings don't print headings\n"), out
);
1413 fputs(_(" --notruncate don't truncate output\n"), out
);
1414 fputs(_(" -o, --output[=<list>] define the columns to output\n"), out
);
1415 fputs(_(" --output-all output all columns\n"), out
);
1416 fputs(_(" -p, --pwd display information related to login by password.\n"), out
);
1417 fputs(_(" -r, --raw display in raw mode\n"), out
);
1418 fputs(_(" -s, --system-accs display system accounts\n"), out
);
1419 fputs(_(" --time-format=<type> display dates in short, full or iso format\n"), out
);
1420 fputs(_(" -u, --user-accs display user accounts\n"), out
);
1421 fputs(_(" -Z, --context display SELinux contexts\n"), out
);
1422 fputs(_(" -z, --print0 delimit user entries with a nul character\n"), out
);
1423 fputs(_(" --wtmp-file <path> set an alternate path for wtmp\n"), out
);
1424 fputs(_(" --btmp-file <path> set an alternate path for btmp\n"), out
);
1425 fputs(_(" --lastlog <path> set an alternate path for lastlog\n"), out
);
1426 fputs(USAGE_SEPARATOR
, out
);
1427 printf(USAGE_HELP_OPTIONS(26));
1429 fputs(USAGE_COLUMNS
, out
);
1430 for (i
= 0; i
< ARRAY_SIZE(coldescs
); i
++)
1431 fprintf(out
, " %14s %s\n", coldescs
[i
].name
, _(coldescs
[i
].help
));
1433 printf(USAGE_MAN_TAIL("lslogins(1)"));
1438 int main(int argc
, char *argv
[])
1441 char *logins
= NULL
, *groups
= NULL
, *outarg
= NULL
;
1442 char *path_lastlog
= _PATH_LASTLOG
, *path_wtmp
= _PATH_WTMP
, *path_btmp
= _PATH_BTMP
;
1443 struct lslogins_control
*ctl
= xcalloc(1, sizeof(struct lslogins_control
));
1446 /* long only options. */
1448 OPT_WTMP
= CHAR_MAX
+ 1,
1457 static const struct option longopts
[] = {
1458 { "acc-expiration", no_argument
, 0, 'a' },
1459 { "colon-separate", no_argument
, 0, 'c' },
1460 { "export", no_argument
, 0, 'e' },
1461 { "failed", no_argument
, 0, 'f' },
1462 { "groups", required_argument
, 0, 'g' },
1463 { "help", no_argument
, 0, 'h' },
1464 { "logins", required_argument
, 0, 'l' },
1465 { "supp-groups", no_argument
, 0, 'G' },
1466 { "newline", no_argument
, 0, 'n' },
1467 { "notruncate", no_argument
, 0, OPT_NOTRUNC
},
1468 { "noheadings", no_argument
, 0, OPT_NOHEAD
},
1469 { "output", required_argument
, 0, 'o' },
1470 { "output-all", no_argument
, 0, OPT_OUTPUT_ALL
},
1471 { "last", no_argument
, 0, 'L', },
1472 { "raw", no_argument
, 0, 'r' },
1473 { "system-accs", no_argument
, 0, 's' },
1474 { "time-format", required_argument
, 0, OPT_TIME_FMT
},
1475 { "user-accs", no_argument
, 0, 'u' },
1476 { "version", no_argument
, 0, 'V' },
1477 { "pwd", no_argument
, 0, 'p' },
1478 { "print0", no_argument
, 0, 'z' },
1479 { "wtmp-file", required_argument
, 0, OPT_WTMP
},
1480 { "btmp-file", required_argument
, 0, OPT_BTMP
},
1481 { "lastlog-file", required_argument
, 0, OPT_LASTLOG
},
1482 #ifdef HAVE_LIBSELINUX
1483 { "context", no_argument
, 0, 'Z' },
1488 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
1493 { 'c','n','r','z' },
1497 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
1499 setlocale(LC_ALL
, "");
1500 bindtextdomain(PACKAGE
, LOCALEDIR
);
1501 textdomain(PACKAGE
);
1502 close_stdout_atexit();
1504 ctl
->time_mode
= TIME_SHORT
;
1506 /* very basic default */
1507 add_column(columns
, ncolumns
++, COL_UID
);
1508 add_column(columns
, ncolumns
++, COL_USER
);
1510 while ((c
= getopt_long(argc
, argv
, "acefGg:hLl:no:prsuVzZ",
1511 longopts
, NULL
)) != -1) {
1513 err_exclusive_options(c
, longopts
, excl
, excl_st
);
1517 add_column(columns
, ncolumns
++, COL_PWD_WARN
);
1518 add_column(columns
, ncolumns
++, COL_PWD_CTIME_MIN
);
1519 add_column(columns
, ncolumns
++, COL_PWD_CTIME_MAX
);
1520 add_column(columns
, ncolumns
++, COL_PWD_CTIME
);
1521 add_column(columns
, ncolumns
++, COL_PWD_EXPIR
);
1524 outmode
= OUT_COLON
;
1527 outmode
= OUT_EXPORT
;
1530 add_column(columns
, ncolumns
++, COL_FAILED_LOGIN
);
1531 add_column(columns
, ncolumns
++, COL_FAILED_TTY
);
1534 add_column(columns
, ncolumns
++, COL_GID
);
1535 add_column(columns
, ncolumns
++, COL_GROUP
);
1536 add_column(columns
, ncolumns
++, COL_SGIDS
);
1537 add_column(columns
, ncolumns
++, COL_SGROUPS
);
1546 add_column(columns
, ncolumns
++, COL_LAST_TTY
);
1547 add_column(columns
, ncolumns
++, COL_LAST_HOSTNAME
);
1548 add_column(columns
, ncolumns
++, COL_LAST_LOGIN
);
1554 outmode
= OUT_NEWLINE
;
1561 case OPT_OUTPUT_ALL
:
1562 for (ncolumns
= 0; ncolumns
< ARRAY_SIZE(coldescs
); ncolumns
++)
1563 columns
[ncolumns
] = ncolumns
;
1569 ctl
->SYS_UID_MIN
= getlogindefs_num("SYS_UID_MIN", UL_SYS_UID_MIN
);
1570 ctl
->SYS_UID_MAX
= getlogindefs_num("SYS_UID_MAX", UL_SYS_UID_MAX
);
1571 lslogins_flag
|= F_SYSAC
;
1574 ctl
->UID_MIN
= getlogindefs_num("UID_MIN", UL_UID_MIN
);
1575 ctl
->UID_MAX
= getlogindefs_num("UID_MAX", UL_UID_MAX
);
1576 lslogins_flag
|= F_USRAC
;
1579 add_column(columns
, ncolumns
++, COL_PWDEMPTY
);
1580 add_column(columns
, ncolumns
++, COL_PWDLOCK
);
1581 add_column(columns
, ncolumns
++, COL_PWDDENY
);
1582 add_column(columns
, ncolumns
++, COL_NOLOGIN
);
1583 add_column(columns
, ncolumns
++, COL_HUSH_STATUS
);
1584 add_column(columns
, ncolumns
++, COL_PWDMETHOD
);
1590 path_lastlog
= optarg
;
1602 ctl
->noheadings
= 1;
1605 ctl
->time_mode
= parse_time_mode(optarg
);
1608 print_version(EXIT_SUCCESS
);
1611 #ifdef HAVE_LIBSELINUX
1612 int sl
= is_selinux_enabled();
1614 warn(_("failed to request selinux state"));
1616 ctl
->selinux_enabled
= sl
== 1;
1618 add_column(columns
, ncolumns
++, COL_SELINUX
);
1622 errtryhelp(EXIT_FAILURE
);
1626 if (argc
- optind
== 1) {
1627 if (strchr(argv
[optind
], ','))
1628 errx(EXIT_FAILURE
, _("Only one user may be specified. Use -l for multiple users."));
1629 logins
= argv
[optind
];
1630 outmode
= OUT_PRETTY
;
1631 ctl
->fail_on_unknown
= 1;
1632 } else if (argc
!= optind
)
1633 errx(EXIT_FAILURE
, _("Only one user may be specified. Use -l for multiple users."));
1635 scols_init_debug(0);
1637 /* lslogins -u -s == lslogins */
1638 if (lslogins_flag
& F_USRAC
&& lslogins_flag
& F_SYSAC
)
1639 lslogins_flag
&= ~(F_USRAC
| F_SYSAC
);
1641 if (outmode
== OUT_PRETTY
) {
1642 /* all columns for lslogins <username> */
1643 for (ncolumns
= 0, i
= 0; i
< ARRAY_SIZE(coldescs
); i
++)
1644 columns
[ncolumns
++] = i
;
1646 } else if (ncolumns
== 2) {
1647 /* default columns */
1649 add_column(columns
, ncolumns
++, COL_NPROCS
);
1651 add_column(columns
, ncolumns
++, COL_PWDLOCK
);
1652 add_column(columns
, ncolumns
++, COL_PWDDENY
);
1653 add_column(columns
, ncolumns
++, COL_LAST_LOGIN
);
1654 add_column(columns
, ncolumns
++, COL_GECOS
);
1657 if (outarg
&& string_add_to_idarray(outarg
, columns
, ARRAY_SIZE(columns
),
1658 &ncolumns
, column_name_to_id
) < 0)
1659 return EXIT_FAILURE
;
1661 if (require_wtmp()) {
1662 parse_utmpx(path_wtmp
, &ctl
->wtmp_size
, &ctl
->wtmp
);
1663 ctl
->lastlogin_fd
= open(path_lastlog
, O_RDONLY
, 0);
1666 parse_utmpx(path_btmp
, &ctl
->btmp_size
, &ctl
->btmp
);
1668 if (logins
|| groups
)
1669 get_ulist(ctl
, logins
, groups
);
1671 if (create_usertree(ctl
))
1672 return EXIT_FAILURE
;
1674 print_user_table(ctl
);
1676 scols_unref_table(tb
);
1677 tdestroy(ctl
->usertree
, free_user
);
1679 if (ctl
->lastlogin_fd
>= 0)
1680 close(ctl
->lastlogin_fd
);
1683 return EXIT_SUCCESS
;