1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include "format-util.h"
5 #include "process-util.h"
6 #include "rlimit-util.h"
8 #include "terminal-util.h"
9 #include "user-record-show.h"
10 #include "user-util.h"
13 const char *user_record_state_color(const char *state
) {
14 if (STR_IN_SET(state
, "unfixated", "absent"))
16 else if (streq(state
, "active"))
17 return ansi_highlight_green();
18 else if (STR_IN_SET(state
, "locked", "dirty"))
19 return ansi_highlight_yellow();
24 void user_record_show(UserRecord
*hr
, bool show_full_group_info
) {
25 const char *hd
, *ip
, *shell
;
31 printf(" User name: %s\n",
32 user_record_user_name_and_realm(hr
));
37 color
= user_record_state_color(hr
->state
);
39 printf(" State: %s%s%s\n",
40 strempty(color
), hr
->state
, color
? ansi_normal() : "");
43 printf(" Disposition: %s\n", user_disposition_to_string(user_record_disposition(hr
)));
45 if (hr
->last_change_usec
!= USEC_INFINITY
) {
46 printf(" Last Change: %s\n", FORMAT_TIMESTAMP(hr
->last_change_usec
));
48 if (hr
->last_change_usec
> now(CLOCK_REALTIME
))
49 printf(" %sModification time lies in the future, system clock wrong?%s\n",
50 ansi_highlight_yellow(), ansi_normal());
53 if (hr
->last_password_change_usec
!= USEC_INFINITY
&&
54 hr
->last_password_change_usec
!= hr
->last_change_usec
)
55 printf(" Last Passw.: %s\n", FORMAT_TIMESTAMP(hr
->last_password_change_usec
));
57 r
= user_record_test_blocked(hr
);
61 printf(" Login OK: %sno%s (record is locked)\n", ansi_highlight_red(), ansi_normal());
65 printf(" Login OK: %sno%s (record not valid yet))\n", ansi_highlight_red(), ansi_normal());
69 printf(" Login OK: %sno%s (record not valid anymore))\n", ansi_highlight_red(), ansi_normal());
76 if (r
< 0 && r
!= -ESTALE
) {
78 printf(" Login OK: %sno%s (%m)\n", ansi_highlight_red(), ansi_normal());
82 if (is_nologin_shell(user_record_shell(hr
))) {
83 printf(" Login OK: %sno%s (nologin shell)\n", ansi_highlight_red(), ansi_normal());
87 y
= user_record_ratelimit_next_try(hr
);
88 if (y
!= USEC_INFINITY
&& y
> now(CLOCK_REALTIME
)) {
89 printf(" Login OK: %sno%s (ratelimit)\n", ansi_highlight_red(), ansi_normal());
93 printf(" Login OK: %syes%s\n", ansi_highlight_green(), ansi_normal());
97 r
= user_record_test_password_change_required(hr
);
101 printf(" Password OK: %schange now%s\n", ansi_highlight_yellow(), ansi_normal());
105 printf(" Password OK: %sexpired%s (change now!)\n", ansi_highlight_yellow(), ansi_normal());
109 printf(" Password OK: %sexpired%s (for good)\n", ansi_highlight_red(), ansi_normal());
113 printf(" Password OK: %sexpires soon%s\n", ansi_highlight_yellow(), ansi_normal());
117 printf(" Password OK: %sno timestamp%s\n", ansi_highlight_red(), ansi_normal());
121 printf(" Password OK: %schange not permitted%s\n", ansi_highlight_yellow(), ansi_normal());
125 printf(" Password OK: %slast password change in future%s\n", ansi_highlight_yellow(), ansi_normal());
131 printf(" Password OK: %sno%s (%m)\n", ansi_highlight_yellow(), ansi_normal());
135 printf(" Password OK: %syes%s\n", ansi_highlight_green(), ansi_normal());
139 if (uid_is_valid(hr
->uid
))
140 printf(" UID: " UID_FMT
"\n", hr
->uid
);
141 if (gid_is_valid(hr
->gid
)) {
142 if (show_full_group_info
) {
143 _cleanup_(group_record_unrefp
) GroupRecord
*gr
= NULL
;
145 r
= groupdb_by_gid(hr
->gid
, 0, &gr
);
148 printf(" GID: " GID_FMT
" (unresolvable: %m)\n", hr
->gid
);
150 printf(" GID: " GID_FMT
" (%s)\n", hr
->gid
, gr
->group_name
);
152 printf(" GID: " GID_FMT
"\n", hr
->gid
);
153 } else if (uid_is_valid(hr
->uid
)) /* Show UID as GID if not separately configured */
154 printf(" GID: " GID_FMT
"\n", (gid_t
) hr
->uid
);
156 if (show_full_group_info
) {
157 _cleanup_(userdb_iterator_freep
) UserDBIterator
*iterator
= NULL
;
159 r
= membershipdb_by_user(hr
->user_name
, 0, &iterator
);
162 printf(" Aux. Groups: (can't acquire: %m)\n");
164 const char *prefix
= " Aux. Groups:";
167 _cleanup_free_
char *group
= NULL
;
169 r
= membershipdb_iterator_get(iterator
, NULL
, &group
);
174 printf("%s (can't iterate: %m)\n", prefix
);
178 printf("%s %s\n", prefix
, group
);
184 if (hr
->real_name
&& !streq(hr
->real_name
, hr
->user_name
))
185 printf(" Real Name: %s\n", hr
->real_name
);
187 hd
= user_record_home_directory(hr
);
189 printf(" Directory: %s\n", hd
);
191 storage
= user_record_storage(hr
);
192 if (storage
>= 0) /* Let's be political, and clarify which storage we like, and which we don't. About CIFS we don't complain. */
193 printf(" Storage: %s%s\n", user_storage_to_string(storage
),
194 storage
== USER_LUKS
? " (strong encryption)" :
195 storage
== USER_FSCRYPT
? " (weak encryption)" :
196 IN_SET(storage
, USER_DIRECTORY
, USER_SUBVOLUME
) ? " (no encryption)" : "");
198 ip
= user_record_image_path(hr
);
199 if (ip
&& !streq_ptr(ip
, hd
))
200 printf(" Image Path: %s\n", ip
);
202 b
= user_record_removable(hr
);
204 printf(" Removable: %s\n", yes_no(b
));
206 shell
= user_record_shell(hr
);
208 printf(" Shell: %s\n", shell
);
210 if (hr
->email_address
)
211 printf(" Email: %s\n", hr
->email_address
);
213 printf(" Location: %s\n", hr
->location
);
214 if (hr
->password_hint
)
215 printf(" Passw. Hint: %s\n", hr
->password_hint
);
217 printf(" Icon Name: %s\n", hr
->icon_name
);
220 printf(" Time Zone: %s\n", hr
->time_zone
);
222 if (hr
->preferred_language
)
223 printf(" Language: %s\n", hr
->preferred_language
);
225 if (!strv_isempty(hr
->environment
)) {
228 STRV_FOREACH(i
, hr
->environment
) {
229 printf(i
== hr
->environment
?
230 " Environment: %s\n" :
236 printf(" Locked: %s\n", yes_no(hr
->locked
));
238 if (hr
->not_before_usec
!= UINT64_MAX
)
239 printf(" Not Before: %s\n", FORMAT_TIMESTAMP(hr
->not_before_usec
));
241 if (hr
->not_after_usec
!= UINT64_MAX
)
242 printf(" Not After: %s\n", FORMAT_TIMESTAMP(hr
->not_after_usec
));
244 if (hr
->umask
!= MODE_INVALID
)
245 printf(" UMask: 0%03o\n", hr
->umask
);
247 if (nice_is_valid(hr
->nice_level
))
248 printf(" Nice: %i\n", hr
->nice_level
);
250 for (int j
= 0; j
< _RLIMIT_MAX
; j
++) {
252 printf(" Limit: RLIMIT_%s=%" PRIu64
":%" PRIu64
"\n",
253 rlimit_to_string(j
), (uint64_t) hr
->rlimits
[j
]->rlim_cur
, (uint64_t) hr
->rlimits
[j
]->rlim_max
);
256 if (hr
->tasks_max
!= UINT64_MAX
)
257 printf(" Tasks Max: %" PRIu64
"\n", hr
->tasks_max
);
259 if (hr
->memory_high
!= UINT64_MAX
)
260 printf(" Memory High: %s\n", FORMAT_BYTES(hr
->memory_high
));
262 if (hr
->memory_max
!= UINT64_MAX
)
263 printf(" Memory Max: %s\n", FORMAT_BYTES(hr
->memory_max
));
265 if (hr
->cpu_weight
!= UINT64_MAX
)
266 printf(" CPU Weight: %" PRIu64
"\n", hr
->cpu_weight
);
268 if (hr
->io_weight
!= UINT64_MAX
)
269 printf(" IO Weight: %" PRIu64
"\n", hr
->io_weight
);
271 if (hr
->access_mode
!= MODE_INVALID
)
272 printf(" Access Mode: 0%03o\n", user_record_access_mode(hr
));
274 if (storage
== USER_LUKS
) {
275 printf("LUKS Discard: online=%s offline=%s\n", yes_no(user_record_luks_discard(hr
)), yes_no(user_record_luks_offline_discard(hr
)));
277 if (!sd_id128_is_null(hr
->luks_uuid
))
278 printf(" LUKS UUID: " SD_ID128_UUID_FORMAT_STR
"\n", SD_ID128_FORMAT_VAL(hr
->luks_uuid
));
279 if (!sd_id128_is_null(hr
->partition_uuid
))
280 printf(" Part UUID: " SD_ID128_UUID_FORMAT_STR
"\n", SD_ID128_FORMAT_VAL(hr
->partition_uuid
));
281 if (!sd_id128_is_null(hr
->file_system_uuid
))
282 printf(" FS UUID: " SD_ID128_UUID_FORMAT_STR
"\n", SD_ID128_FORMAT_VAL(hr
->file_system_uuid
));
284 if (hr
->file_system_type
)
285 printf(" File System: %s\n", user_record_file_system_type(hr
));
287 if (hr
->luks_extra_mount_options
)
288 printf("LUKS MntOpts: %s\n", hr
->luks_extra_mount_options
);
291 printf(" LUKS Cipher: %s\n", hr
->luks_cipher
);
292 if (hr
->luks_cipher_mode
)
293 printf(" Cipher Mode: %s\n", hr
->luks_cipher_mode
);
294 if (hr
->luks_volume_key_size
!= UINT64_MAX
)
295 printf(" Volume Key: %" PRIu64
"bit\n", hr
->luks_volume_key_size
* 8);
297 if (hr
->luks_pbkdf_type
)
298 printf(" PBKDF Type: %s\n", hr
->luks_pbkdf_type
);
299 if (hr
->luks_pbkdf_hash_algorithm
)
300 printf(" PBKDF Hash: %s\n", hr
->luks_pbkdf_hash_algorithm
);
301 if (hr
->luks_pbkdf_time_cost_usec
!= UINT64_MAX
)
302 printf(" PBKDF Time: %s\n", FORMAT_TIMESPAN(hr
->luks_pbkdf_time_cost_usec
, 0));
303 if (hr
->luks_pbkdf_memory_cost
!= UINT64_MAX
)
304 printf(" PBKDF Bytes: %s\n", FORMAT_BYTES(hr
->luks_pbkdf_memory_cost
));
306 if (hr
->luks_pbkdf_parallel_threads
!= UINT64_MAX
)
307 printf("PBKDF Thread: %" PRIu64
"\n", hr
->luks_pbkdf_parallel_threads
);
309 } else if (storage
== USER_CIFS
) {
311 if (hr
->cifs_service
)
312 printf("CIFS Service: %s\n", hr
->cifs_service
);
314 if (hr
->cifs_extra_mount_options
)
315 printf("CIFS MntOpts: %s\n", hr
->cifs_extra_mount_options
);
318 if (hr
->cifs_user_name
)
319 printf(" CIFS User: %s\n", user_record_cifs_user_name(hr
));
321 printf(" CIFS Domain: %s\n", hr
->cifs_domain
);
323 if (storage
!= USER_CLASSIC
)
324 printf(" Mount Flags: %s %s %s\n",
325 hr
->nosuid
? "nosuid" : "suid",
326 hr
->nodev
? "nodev" : "dev",
327 hr
->noexec
? "noexec" : "exec");
329 if (hr
->skeleton_directory
)
330 printf(" Skel. Dir.: %s\n", user_record_skeleton_directory(hr
));
332 if (hr
->disk_size
!= UINT64_MAX
)
333 printf(" Disk Size: %s\n", FORMAT_BYTES(hr
->disk_size
));
335 if (hr
->disk_usage
!= UINT64_MAX
) {
336 if (hr
->disk_size
!= UINT64_MAX
) {
339 permille
= (unsigned) DIV_ROUND_UP(hr
->disk_usage
* 1000U, hr
->disk_size
); /* Round up! */
340 printf(" Disk Usage: %s (= %u.%01u%%)\n",
341 FORMAT_BYTES(hr
->disk_usage
),
342 permille
/ 10, permille
% 10);
344 printf(" Disk Usage: %s\n", FORMAT_BYTES(hr
->disk_usage
));
347 if (hr
->disk_free
!= UINT64_MAX
) {
348 if (hr
->disk_size
!= UINT64_MAX
) {
349 const char *color_on
, *color_off
;
352 permille
= (unsigned) ((hr
->disk_free
* 1000U) / hr
->disk_size
); /* Round down! */
354 /* Color the output red or yellow if we are below 10% resp. 25% free. Because 10% and
355 * 25% can be a lot of space still, let's additionally make some absolute
356 * restrictions: 1G and 2G */
357 if (permille
<= 100U &&
358 hr
->disk_free
< 1024U*1024U*1024U /* 1G */) {
359 color_on
= ansi_highlight_red();
360 color_off
= ansi_normal();
361 } else if (permille
<= 250U &&
362 hr
->disk_free
< 2U*1024U*1024U*1024U /* 2G */) {
363 color_on
= ansi_highlight_yellow();
364 color_off
= ansi_normal();
366 color_on
= color_off
= "";
368 printf(" Disk Free: %s%s (= %u.%01u%%)%s\n",
370 FORMAT_BYTES(hr
->disk_free
),
371 permille
/ 10, permille
% 10,
374 printf(" Disk Free: %s\n", FORMAT_BYTES(hr
->disk_free
));
377 if (hr
->disk_floor
!= UINT64_MAX
)
378 printf(" Disk Floor: %s\n", FORMAT_BYTES(hr
->disk_floor
));
380 if (hr
->disk_ceiling
!= UINT64_MAX
)
381 printf("Disk Ceiling: %s\n", FORMAT_BYTES(hr
->disk_ceiling
));
383 if (hr
->good_authentication_counter
!= UINT64_MAX
)
384 printf(" Good Auth.: %" PRIu64
"\n", hr
->good_authentication_counter
);
386 if (hr
->last_good_authentication_usec
!= UINT64_MAX
)
387 printf(" Last Good: %s\n", FORMAT_TIMESTAMP(hr
->last_good_authentication_usec
));
389 if (hr
->bad_authentication_counter
!= UINT64_MAX
)
390 printf(" Bad Auth.: %" PRIu64
"\n", hr
->bad_authentication_counter
);
392 if (hr
->last_bad_authentication_usec
!= UINT64_MAX
)
393 printf(" Last Bad: %s\n", FORMAT_TIMESTAMP(hr
->last_bad_authentication_usec
));
395 t
= user_record_ratelimit_next_try(hr
);
396 if (t
!= USEC_INFINITY
) {
397 usec_t n
= now(CLOCK_REALTIME
);
400 printf(" Next Try: anytime\n");
402 printf(" Next Try: %sin %s%s\n",
403 ansi_highlight_red(),
404 FORMAT_TIMESPAN(t
- n
, USEC_PER_SEC
),
408 if (storage
!= USER_CLASSIC
)
409 printf(" Auth. Limit: %" PRIu64
" attempts per %s\n", user_record_ratelimit_burst(hr
),
410 FORMAT_TIMESPAN(user_record_ratelimit_interval_usec(hr
), 0));
412 if (hr
->enforce_password_policy
>= 0)
413 printf(" Passwd Pol.: %s\n", yes_no(hr
->enforce_password_policy
));
415 if (hr
->password_change_min_usec
!= UINT64_MAX
||
416 hr
->password_change_max_usec
!= UINT64_MAX
||
417 hr
->password_change_warn_usec
!= UINT64_MAX
||
418 hr
->password_change_inactive_usec
!= UINT64_MAX
) {
420 printf(" Passwd Chg.:");
422 if (hr
->password_change_min_usec
!= UINT64_MAX
) {
423 printf(" min %s", FORMAT_TIMESPAN(hr
->password_change_min_usec
, 0));
425 if (hr
->password_change_max_usec
!= UINT64_MAX
)
429 if (hr
->password_change_max_usec
!= UINT64_MAX
)
430 printf(" max %s", FORMAT_TIMESPAN(hr
->password_change_max_usec
, 0));
432 if (hr
->password_change_warn_usec
!= UINT64_MAX
)
433 printf("/warn %s", FORMAT_TIMESPAN(hr
->password_change_warn_usec
, 0));
435 if (hr
->password_change_inactive_usec
!= UINT64_MAX
)
436 printf("/inactive %s", FORMAT_TIMESPAN(hr
->password_change_inactive_usec
, 0));
441 if (hr
->password_change_now
>= 0)
442 printf("Pas. Ch. Now: %s\n", yes_no(hr
->password_change_now
));
444 if (hr
->drop_caches
>= 0 || user_record_drop_caches(hr
))
445 printf(" Drop Caches: %s\n", yes_no(user_record_drop_caches(hr
)));
447 if (hr
->auto_resize_mode
>= 0)
448 printf(" Auto Resize: %s\n", auto_resize_mode_to_string(user_record_auto_resize_mode(hr
)));
450 if (hr
->rebalance_weight
!= REBALANCE_WEIGHT_UNSET
) {
453 rb
= user_record_rebalance_weight(hr
);
454 if (rb
== REBALANCE_WEIGHT_OFF
)
455 printf(" Rebalance: off\n");
457 printf(" Rebalance: weight %" PRIu64
"\n", rb
);
460 if (!strv_isempty(hr
->ssh_authorized_keys
))
461 printf("SSH Pub. Key: %zu\n", strv_length(hr
->ssh_authorized_keys
));
463 if (!strv_isempty(hr
->pkcs11_token_uri
)) {
466 STRV_FOREACH(i
, hr
->pkcs11_token_uri
)
467 printf(i
== hr
->pkcs11_token_uri
?
468 "PKCS11 Token: %s\n" :
472 if (hr
->n_fido2_hmac_credential
> 0)
473 printf(" FIDO2 Token: %zu\n", hr
->n_fido2_hmac_credential
);
475 if (!strv_isempty(hr
->recovery_key_type
))
476 printf("Recovery Key: %zu\n", strv_length(hr
->recovery_key_type
));
478 k
= strv_length(hr
->hashed_password
);
480 printf(" Passwords: %snone%s\n",
481 user_record_disposition(hr
) == USER_REGULAR
? ansi_highlight_yellow() : ansi_normal(), ansi_normal());
483 printf(" Passwords: %zu\n", k
);
485 if (hr
->signed_locally
>= 0)
486 printf(" Local Sig.: %s\n", yes_no(hr
->signed_locally
));
488 if (hr
->stop_delay_usec
!= UINT64_MAX
)
489 printf(" Stop Delay: %s\n", FORMAT_TIMESPAN(hr
->stop_delay_usec
, 0));
491 if (hr
->auto_login
>= 0)
492 printf("Autom. Login: %s\n", yes_no(hr
->auto_login
));
494 if (hr
->kill_processes
>= 0)
495 printf(" Kill Proc.: %s\n", yes_no(hr
->kill_processes
));
498 printf(" Service: %s\n", hr
->service
);
501 void group_record_show(GroupRecord
*gr
, bool show_full_user_info
) {
504 printf(" Group name: %s\n",
505 group_record_group_name_and_realm(gr
));
507 printf(" Disposition: %s\n", user_disposition_to_string(group_record_disposition(gr
)));
509 if (gr
->last_change_usec
!= USEC_INFINITY
)
510 printf(" Last Change: %s\n", FORMAT_TIMESTAMP(gr
->last_change_usec
));
512 if (gid_is_valid(gr
->gid
))
513 printf(" GID: " GID_FMT
"\n", gr
->gid
);
515 if (show_full_user_info
) {
516 _cleanup_(userdb_iterator_freep
) UserDBIterator
*iterator
= NULL
;
518 r
= membershipdb_by_group(gr
->group_name
, 0, &iterator
);
521 printf(" Members: (can't acquire: %m)");
523 const char *prefix
= " Members:";
526 _cleanup_free_
char *user
= NULL
;
528 r
= membershipdb_iterator_get(iterator
, &user
, NULL
);
533 printf("%s (can't iterate: %m\n", prefix
);
537 printf("%s %s\n", prefix
, user
);
542 const char *prefix
= " Members:";
545 STRV_FOREACH(i
, gr
->members
) {
546 printf("%s %s\n", prefix
, *i
);
551 if (!strv_isempty(gr
->administrators
)) {
552 const char *prefix
= " Admins:";
555 STRV_FOREACH(i
, gr
->administrators
) {
556 printf("%s %s\n", prefix
, *i
);
561 if (gr
->description
&& !streq(gr
->description
, gr
->group_name
))
562 printf(" Description: %s\n", gr
->description
);
564 if (!strv_isempty(gr
->hashed_password
))
565 printf(" Passwords: %zu\n", strv_length(gr
->hashed_password
));
568 printf(" Service: %s\n", gr
->service
);