From: Jason Stewart Date: Sun, 5 May 2024 06:49:28 +0000 (-0400) Subject: last/lastb field truncation indicator X-Git-Tag: v2.42-start~309^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=937f05098b997e2c56b95189ef1af630c160161d;p=thirdparty%2Futil-linux.git last/lastb field truncation indicator - indicate truncated fields with an asterisk character - updated man page and unit tests --- diff --git a/login-utils/last.1.adoc b/login-utils/last.1.adoc index a3b8afe039..099b281ccb 100644 --- a/login-utils/last.1.adoc +++ b/login-utils/last.1.adoc @@ -82,7 +82,7 @@ Use ASCII *tab* characters to separate the columns in the output instead of spac Define the output timestamp _format_ to be one of _notime_, _short_, _full_, or _iso_. The _notime_ variant will not print any timestamps at all, _short_ is the default, and _full_ is the same as the *--fulltimes* option. The _iso_ variant will display the timestamp in ISO-8601 format. The ISO format contains timezone information, making it preferable when printouts are investigated outside of the system. *-w*, *--fullnames*:: -Display full user names and domain names in the output. +Display full user names and domain names / IP addresses in the output. Domain names and IP addresses are truncated to 16 characters, and user names are truncated to 8 characters when this flag is not specified. An asterisk is set as the last character of truncated fields. *-x*, *--system*:: Display the system shutdown entries and run level changes. diff --git a/login-utils/last.c b/login-utils/last.c index f5a9fec080..0f92fef353 100644 --- a/login-utils/last.c +++ b/login-utils/last.c @@ -96,6 +96,8 @@ struct last_control { time_t present; /* who where present at time_t */ unsigned int time_fmt; /* time format */ char separator; /* output separator */ + + bool fullnames_mode; }; /* Double linked list of struct utmp's */ @@ -521,8 +523,19 @@ static int list(const struct last_control *ctl, struct utmpx *p, time_t logout_t if (r < 0) mem2strcpy(domain, p->ut_host, sizeof(p->ut_host), sizeof(domain)); + /* + * set last displayed character to an asterisk when + * user/domain/ip fields are to be truncated in non-fullnames mode + */ + if (!ctl->fullnames_mode && (strnlen(p->ut_user, sizeof(p->ut_user)) > ctl->name_len)) + p->ut_user[ctl->name_len-1] = '*'; + if (ctl->showhost) { if (!ctl->altlist) { + + if (!ctl->fullnames_mode && (strnlen(domain, sizeof(domain)) > ctl->domain_len)) + domain[ctl->domain_len-1] = '*'; + len = snprintf(final, sizeof(final), "%-8.*s%c%-12.12s%c%-16.*s%c%-*.*s%c%-*.*s%c%s\n", ctl->name_len, p->ut_user, ctl->separator, utline, ctl->separator, @@ -958,7 +971,8 @@ int main(int argc, char **argv) .showhost = TRUE, .name_len = LAST_LOGIN_LEN, .time_fmt = LAST_TIMEFTM_SHORT, - .domain_len = LAST_DOMAIN_LEN + .domain_len = LAST_DOMAIN_LEN, + .fullnames_mode = false, }; char **files = NULL; size_t i, nfiles = 0; @@ -1055,6 +1069,7 @@ int main(int argc, char **argv) ctl.until = (time_t) (p / 1000000); break; case 'w': + ctl.fullnames_mode = true; if (ctl.name_len < sizeof_member(struct utmpx, ut_user)) ctl.name_len = sizeof_member(struct utmpx, ut_user); if (ctl.domain_len < sizeof_member(struct utmpx, ut_host)) diff --git a/tests/expected/utmp/last-dns b/tests/expected/utmp/last-dns index 34012c7f12..6a0ce54d7e 100644 --- a/tests/expected/utmp/last-dns +++ b/tests/expected/utmp/last-dns @@ -1,5 +1,5 @@ ~~~ dns short ~~~ -IPv4 root a.root-servers.n Wed Aug 28 13:00 - 14:00 (01:00) +IPv4 root a.root-servers.* Wed Aug 28 13:00 - 14:00 (01:00) wtmp-a begins Wed Aug 28 03:00:00 2013 ~~~ dns long ~~~ diff --git a/tests/expected/utmp/last-ipv6-dns b/tests/expected/utmp/last-ipv6-dns index aa53a6451d..9687b3f5a0 100644 --- a/tests/expected/utmp/last-ipv6-dns +++ b/tests/expected/utmp/last-ipv6-dns @@ -1,5 +1,5 @@ ~~~ dns short ~~~ -IPv6 root a.root-servers.n Wed Aug 28 20:30 - 20:40 (00:10) +IPv6 root a.root-servers.* Wed Aug 28 20:30 - 20:40 (00:10) wtmp-ipv6 begins Wed Aug 28 20:30:40 2013 ~~~ dns long ~~~ diff --git a/tests/expected/utmp/last-ipv6-nodns b/tests/expected/utmp/last-ipv6-nodns index 65d422a452..5909d028c2 100644 --- a/tests/expected/utmp/last-ipv6-nodns +++ b/tests/expected/utmp/last-ipv6-nodns @@ -1,5 +1,5 @@ ~~~ show ip ~~~ -IPv6 root 2001:503:ba3e::2 Wed Aug 28 20:30 - 20:40 (00:10) +IPv6 root 2001:503:ba3e::* Wed Aug 28 20:30 - 20:40 (00:10) wtmp-ipv6 begins Wed Aug 28 20:30:40 2013 ~~~ show ip last ~~~ diff --git a/tests/expected/utmp/last-nodns b/tests/expected/utmp/last-nodns index 5b9171de31..9f874f726d 100644 --- a/tests/expected/utmp/last-nodns +++ b/tests/expected/utmp/last-nodns @@ -1,5 +1,5 @@ ~~~ basic output ~~~ -rick long never-gonna-logo Thu Jan 1 00:00 - 03:14 (24855+03:14) +rick long never-gonna-log* Thu Jan 1 00:00 - 03:14 (24855+03:14) torvalds linux hobby Mon Aug 26 00:57 gone - no logout reboot system boot system-name Wed Aug 28 18:00 still running reboot system boot system-name Wed Aug 28 16:00 - 17:00 (01:00) @@ -11,12 +11,12 @@ newtime foo three Wed Aug 28 09:00 - 10:00 (01:00) oldtime foo four Wed Aug 28 08:00 - 09:00 (01:00) init foo five Wed Aug 28 07:00 - 08:00 (01:00) login foo six Wed Aug 28 06:00 - 07:00 (01:00) -user_pro foo seven Wed Aug 28 05:00 - 06:00 (01:00) -accounti foo nine Wed Aug 28 03:00 - 04:00 (01:00) +user_pr* foo seven Wed Aug 28 05:00 - 06:00 (01:00) +account* foo nine Wed Aug 28 03:00 - 04:00 (01:00) wtmp-a begins Wed Aug 28 03:00:00 2013 ~~~ include system ~~~ -rick long never-gonna-logo Thu Jan 1 00:00 - 03:14 (24855+03:14) +rick long never-gonna-log* Thu Jan 1 00:00 - 03:14 (24855+03:14) torvalds linux hobby Mon Aug 26 00:57 gone - no logout reboot system boot system-name Wed Aug 28 18:00 still running shutdown system down system-name Wed Aug 28 17:00 - 18:00 (01:00) @@ -30,8 +30,8 @@ newtime foo three Wed Aug 28 09:00 - 10:00 (01:00) oldtime foo four Wed Aug 28 08:00 - 09:00 (01:00) init foo five Wed Aug 28 07:00 - 08:00 (01:00) login foo six Wed Aug 28 06:00 - 07:00 (01:00) -user_pro foo seven Wed Aug 28 05:00 - 06:00 (01:00) -accounti foo nine Wed Aug 28 03:00 - 04:00 (01:00) +user_pr* foo seven Wed Aug 28 05:00 - 06:00 (01:00) +account* foo nine Wed Aug 28 03:00 - 04:00 (01:00) wtmp-a begins Wed Aug 28 03:00:00 2013 ~~~ show ip ~~~ @@ -43,7 +43,7 @@ torvalds linux Mon Aug 26 00:57 gone - no logout wtmp-a begins Wed Aug 28 03:00:00 2013 ~~~ full times shortopt ~~~ -rick long never-gonna-logo Thu Jan 1 00:00:00 1970 - Tue Jan 19 03:14:07 2038 (24855+03:14) +rick long never-gonna-log* Thu Jan 1 00:00:00 1970 - Tue Jan 19 03:14:07 2038 (24855+03:14) torvalds linux hobby Mon Aug 26 00:57:08 1991 gone - no logout reboot system boot system-name Wed Aug 28 18:00:00 2013 still running reboot system boot system-name Wed Aug 28 16:00:00 2013 - Wed Aug 28 17:00:00 2013 (01:00) @@ -55,12 +55,12 @@ newtime foo three Wed Aug 28 09:00:00 2013 - Wed Aug 28 10: oldtime foo four Wed Aug 28 08:00:00 2013 - Wed Aug 28 09:00:00 2013 (01:00) init foo five Wed Aug 28 07:00:00 2013 - Wed Aug 28 08:00:00 2013 (01:00) login foo six Wed Aug 28 06:00:00 2013 - Wed Aug 28 07:00:00 2013 (01:00) -user_pro foo seven Wed Aug 28 05:00:00 2013 - Wed Aug 28 06:00:00 2013 (01:00) -accounti foo nine Wed Aug 28 03:00:00 2013 - Wed Aug 28 04:00:00 2013 (01:00) +user_pr* foo seven Wed Aug 28 05:00:00 2013 - Wed Aug 28 06:00:00 2013 (01:00) +account* foo nine Wed Aug 28 03:00:00 2013 - Wed Aug 28 04:00:00 2013 (01:00) wtmp-a begins Wed Aug 28 03:00:00 2013 ~~~ full times longopt ~~~ -rick long never-gonna-logo Thu Jan 1 00:00:00 1970 - Tue Jan 19 03:14:07 2038 (24855+03:14) +rick long never-gonna-log* Thu Jan 1 00:00:00 1970 - Tue Jan 19 03:14:07 2038 (24855+03:14) torvalds linux hobby Mon Aug 26 00:57:08 1991 gone - no logout reboot system boot system-name Wed Aug 28 18:00:00 2013 still running reboot system boot system-name Wed Aug 28 16:00:00 2013 - Wed Aug 28 17:00:00 2013 (01:00) @@ -72,12 +72,12 @@ newtime foo three Wed Aug 28 09:00:00 2013 - Wed Aug 28 10: oldtime foo four Wed Aug 28 08:00:00 2013 - Wed Aug 28 09:00:00 2013 (01:00) init foo five Wed Aug 28 07:00:00 2013 - Wed Aug 28 08:00:00 2013 (01:00) login foo six Wed Aug 28 06:00:00 2013 - Wed Aug 28 07:00:00 2013 (01:00) -user_pro foo seven Wed Aug 28 05:00:00 2013 - Wed Aug 28 06:00:00 2013 (01:00) -accounti foo nine Wed Aug 28 03:00:00 2013 - Wed Aug 28 04:00:00 2013 (01:00) +user_pr* foo seven Wed Aug 28 05:00:00 2013 - Wed Aug 28 06:00:00 2013 (01:00) +account* foo nine Wed Aug 28 03:00:00 2013 - Wed Aug 28 04:00:00 2013 (01:00) wtmp-a begins Wed Aug 28 03:00:00 2013 ~~~ no time ~~~ -rick long never-gonna-logo (24855+03:14) +rick long never-gonna-log* (24855+03:14) torvalds linux hobby no logout reboot system boot system-name running reboot system boot system-name (01:00) @@ -89,8 +89,8 @@ newtime foo three (01:00) oldtime foo four (01:00) init foo five (01:00) login foo six (01:00) -user_pro foo seven (01:00) -accounti foo nine (01:00) +user_pr* foo seven (01:00) +account* foo nine (01:00) ~~~ iso-8601 time ~~~ rick long never-gonna-logout 1970-01-01T00:00:00+00:00 - 2038-01-19T03:14:07+00:00 (24855+03:14) torvalds linux hobby 1991-08-26T00:57:08+00:00 gone - no logout @@ -112,12 +112,12 @@ wtmp-a begins 2013-08-28T03:00:00+00:00 oldtime foo four Wed Aug 28 08:00 gone - no logout init foo five Wed Aug 28 07:00 - 08:00 (01:00) login foo six Wed Aug 28 06:00 - 07:00 (01:00) -user_pro foo seven Wed Aug 28 05:00 - 06:00 (01:00) -accounti foo nine Wed Aug 28 03:00 - 04:00 (01:00) +user_pr* foo seven Wed Aug 28 05:00 - 06:00 (01:00) +account* foo nine Wed Aug 28 03:00 - 04:00 (01:00) wtmp-a begins Wed Aug 28 03:00:00 2013 ~~~ present ~~~ -rick long never-gonna-logo Thu Jan 1 00:00 - 03:14 (24855+03:14) +rick long never-gonna-log* Thu Jan 1 00:00 - 03:14 (24855+03:14) torvalds linux hobby Mon Aug 26 00:57 gone - no logout runlevel foo one Wed Aug 28 11:00 - 12:00 (01:00)