1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
7 #include "ask-password-api.h"
8 #include "bus-common-errors.h"
10 #include "bus-locator.h"
11 #include "cgroup-util.h"
12 #include "dns-domain.h"
16 #include "format-table.h"
17 #include "home-util.h"
18 #include "homectl-fido2.h"
19 #include "homectl-pkcs11.h"
20 #include "homectl-recovery-key.h"
21 #include "libfido2-util.h"
22 #include "locale-util.h"
23 #include "main-func.h"
24 #include "memory-util.h"
26 #include "parse-argument.h"
27 #include "parse-util.h"
28 #include "path-util.h"
29 #include "percent-util.h"
30 #include "pkcs11-util.h"
31 #include "pretty-print.h"
32 #include "process-util.h"
33 #include "pwquality-util.h"
34 #include "rlimit-util.h"
35 #include "spawn-polkit-agent.h"
36 #include "terminal-util.h"
37 #include "user-record-pwquality.h"
38 #include "user-record-show.h"
39 #include "user-record-util.h"
40 #include "user-record.h"
41 #include "user-util.h"
44 static PagerFlags arg_pager_flags
= 0;
45 static bool arg_legend
= true;
46 static bool arg_ask_password
= true;
47 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
48 static const char *arg_host
= NULL
;
49 static const char *arg_identity
= NULL
;
50 static JsonVariant
*arg_identity_extra
= NULL
;
51 static JsonVariant
*arg_identity_extra_privileged
= NULL
;
52 static JsonVariant
*arg_identity_extra_this_machine
= NULL
;
53 static JsonVariant
*arg_identity_extra_rlimits
= NULL
;
54 static char **arg_identity_filter
= NULL
; /* this one is also applied to 'privileged' and 'thisMachine' subobjects */
55 static char **arg_identity_filter_rlimits
= NULL
;
56 static uint64_t arg_disk_size
= UINT64_MAX
;
57 static uint64_t arg_disk_size_relative
= UINT64_MAX
;
58 static char **arg_pkcs11_token_uri
= NULL
;
59 static char **arg_fido2_device
= NULL
;
60 static Fido2EnrollFlags arg_fido2_lock_with
= FIDO2ENROLL_PIN
| FIDO2ENROLL_UP
;
61 static bool arg_recovery_key
= false;
62 static JsonFormatFlags arg_json_format_flags
= JSON_FORMAT_OFF
;
63 static bool arg_and_resize
= false;
64 static bool arg_and_change_password
= false;
66 EXPORT_FORMAT_FULL
, /* export the full record */
67 EXPORT_FORMAT_STRIPPED
, /* strip "state" + "binding", but leave signature in place */
68 EXPORT_FORMAT_MINIMAL
, /* also strip signature */
69 } arg_export_format
= EXPORT_FORMAT_FULL
;
71 STATIC_DESTRUCTOR_REGISTER(arg_identity_extra
, json_variant_unrefp
);
72 STATIC_DESTRUCTOR_REGISTER(arg_identity_extra_this_machine
, json_variant_unrefp
);
73 STATIC_DESTRUCTOR_REGISTER(arg_identity_extra_privileged
, json_variant_unrefp
);
74 STATIC_DESTRUCTOR_REGISTER(arg_identity_extra_rlimits
, json_variant_unrefp
);
75 STATIC_DESTRUCTOR_REGISTER(arg_identity_filter
, strv_freep
);
76 STATIC_DESTRUCTOR_REGISTER(arg_identity_filter_rlimits
, strv_freep
);
77 STATIC_DESTRUCTOR_REGISTER(arg_pkcs11_token_uri
, strv_freep
);
78 STATIC_DESTRUCTOR_REGISTER(arg_fido2_device
, strv_freep
);
80 static const BusLocator
*bus_mgr
;
82 static bool identity_properties_specified(void) {
85 !json_variant_is_blank_object(arg_identity_extra
) ||
86 !json_variant_is_blank_object(arg_identity_extra_privileged
) ||
87 !json_variant_is_blank_object(arg_identity_extra_this_machine
) ||
88 !json_variant_is_blank_object(arg_identity_extra_rlimits
) ||
89 !strv_isempty(arg_identity_filter
) ||
90 !strv_isempty(arg_identity_filter_rlimits
) ||
91 !strv_isempty(arg_pkcs11_token_uri
) ||
92 !strv_isempty(arg_fido2_device
);
95 static int acquire_bus(sd_bus
**bus
) {
103 r
= bus_connect_transport(arg_transport
, arg_host
, false, bus
);
105 return bus_log_connect_error(r
);
107 (void) sd_bus_set_allow_interactive_authorization(*bus
, arg_ask_password
);
112 static int list_homes(int argc
, char *argv
[], void *userdata
) {
113 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
114 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
115 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
116 _cleanup_(table_unrefp
) Table
*table
= NULL
;
119 r
= acquire_bus(&bus
);
123 r
= bus_call_method(bus
, bus_mgr
, "ListHomes", &error
, &reply
, NULL
);
125 return log_error_errno(r
, "Failed to list homes: %s", bus_error_message(&error
, r
));
127 table
= table_new("name", "uid", "gid", "state", "realname", "home", "shell");
131 r
= sd_bus_message_enter_container(reply
, 'a', "(susussso)");
133 return bus_log_parse_error(r
);
136 const char *name
, *state
, *realname
, *home
, *shell
, *color
;
140 r
= sd_bus_message_read(reply
, "(susussso)", &name
, &uid
, &state
, &gid
, &realname
, &home
, &shell
, NULL
);
142 return bus_log_parse_error(r
);
146 r
= table_add_many(table
,
151 return table_log_add_error(r
);
154 r
= table_add_cell(table
, &cell
, TABLE_STRING
, state
);
156 return table_log_add_error(r
);
158 color
= user_record_state_color(state
);
160 (void) table_set_color(table
, cell
, color
);
162 r
= table_add_many(table
,
163 TABLE_STRING
, strna(empty_to_null(realname
)),
165 TABLE_STRING
, strna(empty_to_null(shell
)));
167 return table_log_add_error(r
);
170 r
= sd_bus_message_exit_container(reply
);
172 return bus_log_parse_error(r
);
174 if (table_get_rows(table
) > 1 || !FLAGS_SET(arg_json_format_flags
, JSON_FORMAT_OFF
)) {
175 r
= table_set_sort(table
, (size_t) 0);
177 return table_log_sort_error(r
);
179 r
= table_print_with_pager(table
, arg_json_format_flags
, arg_pager_flags
, arg_legend
);
184 if (arg_legend
&& (arg_json_format_flags
& JSON_FORMAT_OFF
)) {
185 if (table_get_rows(table
) > 1)
186 printf("\n%zu home areas listed.\n", table_get_rows(table
) - 1);
188 printf("No home areas.\n");
194 static int acquire_existing_password(
195 const char *user_name
,
197 bool emphasize_current
,
198 AskPasswordFlags flags
) {
200 _cleanup_(strv_free_erasep
) char **password
= NULL
;
201 _cleanup_free_
char *question
= NULL
;
208 e
= getenv("PASSWORD");
210 /* People really shouldn't use environment variables for passing passwords. We support this
211 * only for testing purposes, and do not document the behaviour, so that people won't
212 * actually use this outside of testing. */
214 r
= user_record_set_password(hr
, STRV_MAKE(e
), true);
216 return log_error_errno(r
, "Failed to store password: %m");
219 assert_se(unsetenv("PASSWORD") == 0);
224 /* If this is not our own user, then don't use the password cache */
225 if (is_this_me(user_name
) <= 0)
226 SET_FLAG(flags
, ASK_PASSWORD_ACCEPT_CACHED
|ASK_PASSWORD_PUSH_CACHE
, false);
228 if (asprintf(&question
, emphasize_current
?
229 "Please enter current password for user %s:" :
230 "Please enter password for user %s:",
234 r
= ask_password_auto(question
,
235 /* icon= */ "user-home",
237 /* key_name= */ "home-password",
238 /* credential_name= */ "home.password",
242 if (r
== -EUNATCH
) { /* EUNATCH is returned if no password was found and asking interactively was
243 * disabled via the flags. Not an error for us. */
244 log_debug_errno(r
, "No passwords acquired.");
248 return log_error_errno(r
, "Failed to acquire password: %m");
250 r
= user_record_set_password(hr
, password
, true);
252 return log_error_errno(r
, "Failed to store password: %m");
257 static int acquire_token_pin(
258 const char *user_name
,
260 AskPasswordFlags flags
) {
262 _cleanup_(strv_free_erasep
) char **pin
= NULL
;
263 _cleanup_free_
char *question
= NULL
;
272 r
= user_record_set_token_pin(hr
, STRV_MAKE(e
), false);
274 return log_error_errno(r
, "Failed to store token PIN: %m");
277 assert_se(unsetenv("PIN") == 0);
282 /* If this is not our own user, then don't use the password cache */
283 if (is_this_me(user_name
) <= 0)
284 SET_FLAG(flags
, ASK_PASSWORD_ACCEPT_CACHED
|ASK_PASSWORD_PUSH_CACHE
, false);
286 if (asprintf(&question
, "Please enter security token PIN for user %s:", user_name
) < 0)
289 r
= ask_password_auto(
291 /* icon= */ "user-home",
293 /* key_name= */ "token-pin",
294 /* credential_name= */ "home.token-pin",
298 if (r
== -EUNATCH
) { /* EUNATCH is returned if no PIN was found and asking interactively was disabled
299 * via the flags. Not an error for us. */
300 log_debug_errno(r
, "No security token PINs acquired.");
304 return log_error_errno(r
, "Failed to acquire security token PIN: %m");
306 r
= user_record_set_token_pin(hr
, pin
, false);
308 return log_error_errno(r
, "Failed to store security token PIN: %m");
313 static int handle_generic_user_record_error(
314 const char *user_name
,
316 const sd_bus_error
*error
,
318 bool emphasize_current_password
) {
324 if (sd_bus_error_has_name(error
, BUS_ERROR_HOME_ABSENT
))
325 return log_error_errno(SYNTHETIC_ERRNO(EREMOTE
),
326 "Home of user %s is currently absent, please plug in the necessary storage device or backing file system.", user_name
);
328 else if (sd_bus_error_has_name(error
, BUS_ERROR_AUTHENTICATION_LIMIT_HIT
))
329 return log_error_errno(SYNTHETIC_ERRNO(ETOOMANYREFS
),
330 "Too frequent unsuccessful login attempts for user %s, try again later.", user_name
);
332 else if (sd_bus_error_has_name(error
, BUS_ERROR_BAD_PASSWORD
)) {
334 if (!strv_isempty(hr
->password
))
335 log_notice("Password incorrect or not sufficient, please try again.");
337 /* Don't consume cache entries or credentials here, we already tried that unsuccessfully. But
338 * let's push what we acquire here into the cache */
339 r
= acquire_existing_password(
342 emphasize_current_password
,
343 ASK_PASSWORD_PUSH_CACHE
| ASK_PASSWORD_NO_CREDENTIAL
);
347 } else if (sd_bus_error_has_name(error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
)) {
349 if (strv_isempty(hr
->password
))
350 log_notice("Security token not inserted, please enter password.");
352 log_notice("Password incorrect or not sufficient, and configured security token not inserted, please try again.");
354 r
= acquire_existing_password(
357 emphasize_current_password
,
358 ASK_PASSWORD_PUSH_CACHE
| ASK_PASSWORD_NO_CREDENTIAL
);
362 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_PIN_NEEDED
)) {
364 /* First time the PIN is requested, let's accept cached data, and allow using credential store */
365 r
= acquire_token_pin(
368 ASK_PASSWORD_ACCEPT_CACHED
| ASK_PASSWORD_PUSH_CACHE
);
372 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_PROTECTED_AUTHENTICATION_PATH_NEEDED
)) {
374 log_notice("%s%sPlease authenticate physically on security token.",
375 emoji_enabled() ? special_glyph(SPECIAL_GLYPH_TOUCH
) : "",
376 emoji_enabled() ? " " : "");
378 r
= user_record_set_pkcs11_protected_authentication_path_permitted(hr
, true);
380 return log_error_errno(r
, "Failed to set PKCS#11 protected authentication path permitted flag: %m");
382 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_USER_PRESENCE_NEEDED
)) {
384 log_notice("%s%sPlease confirm presence on security token.",
385 emoji_enabled() ? special_glyph(SPECIAL_GLYPH_TOUCH
) : "",
386 emoji_enabled() ? " " : "");
388 r
= user_record_set_fido2_user_presence_permitted(hr
, true);
390 return log_error_errno(r
, "Failed to set FIDO2 user presence permitted flag: %m");
392 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_USER_VERIFICATION_NEEDED
)) {
394 log_notice("%s%sPlease verify user on security token.",
395 emoji_enabled() ? special_glyph(SPECIAL_GLYPH_TOUCH
) : "",
396 emoji_enabled() ? " " : "");
398 r
= user_record_set_fido2_user_verification_permitted(hr
, true);
400 return log_error_errno(r
, "Failed to set FIDO2 user verification permitted flag: %m");
402 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_PIN_LOCKED
))
403 return log_error_errno(SYNTHETIC_ERRNO(EPERM
), "Security token PIN is locked, please unlock it first. (Hint: Removal and re-insertion might suffice.)");
405 else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_BAD_PIN
)) {
407 log_notice("Security token PIN incorrect, please try again.");
409 /* If the previous PIN was wrong don't accept cached info anymore, but add to cache. Also, don't use the credential data */
410 r
= acquire_token_pin(
413 ASK_PASSWORD_PUSH_CACHE
| ASK_PASSWORD_NO_CREDENTIAL
);
417 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_BAD_PIN_FEW_TRIES_LEFT
)) {
419 log_notice("Security token PIN incorrect, please try again (only a few tries left!).");
421 r
= acquire_token_pin(
424 ASK_PASSWORD_PUSH_CACHE
| ASK_PASSWORD_NO_CREDENTIAL
);
428 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_BAD_PIN_ONE_TRY_LEFT
)) {
430 log_notice("Security token PIN incorrect, please try again (only one try left!).");
432 r
= acquire_token_pin(
435 ASK_PASSWORD_PUSH_CACHE
| ASK_PASSWORD_NO_CREDENTIAL
);
439 return log_error_errno(ret
, "Operation on home %s failed: %s", user_name
, bus_error_message(error
, ret
));
444 static int acquire_passed_secrets(const char *user_name
, UserRecord
**ret
) {
445 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
450 /* Generates an initial secret objects that contains passwords supplied via $PASSWORD, the password
451 * cache or the credentials subsystem, but excluding any interactive stuff. If nothing is passed,
452 * returns an empty secret object. */
454 secret
= user_record_new();
458 r
= acquire_existing_password(
461 /* emphasize_current_password = */ false,
462 ASK_PASSWORD_ACCEPT_CACHED
| ASK_PASSWORD_NO_TTY
| ASK_PASSWORD_NO_AGENT
);
466 r
= acquire_token_pin(
469 ASK_PASSWORD_ACCEPT_CACHED
| ASK_PASSWORD_NO_TTY
| ASK_PASSWORD_NO_AGENT
);
473 *ret
= TAKE_PTR(secret
);
477 static int activate_home(int argc
, char *argv
[], void *userdata
) {
478 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
482 r
= acquire_bus(&bus
);
486 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
487 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
489 r
= acquire_passed_secrets(*i
, &secret
);
494 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
495 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
497 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ActivateHome");
499 return bus_log_create_error(r
);
501 r
= sd_bus_message_append(m
, "s", *i
);
503 return bus_log_create_error(r
);
505 r
= bus_message_append_secret(m
, secret
);
507 return bus_log_create_error(r
);
509 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
511 r
= handle_generic_user_record_error(*i
, secret
, &error
, r
, /* emphasize_current_password= */ false);
526 static int deactivate_home(int argc
, char *argv
[], void *userdata
) {
527 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
531 r
= acquire_bus(&bus
);
535 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
536 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
537 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
539 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "DeactivateHome");
541 return bus_log_create_error(r
);
543 r
= sd_bus_message_append(m
, "s", *i
);
545 return bus_log_create_error(r
);
547 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
549 log_error_errno(r
, "Failed to deactivate user home: %s", bus_error_message(&error
, r
));
558 static void dump_home_record(UserRecord
*hr
) {
563 if (hr
->incomplete
) {
565 log_warning("Warning: lacking rights to acquire privileged fields of user record of '%s', output incomplete.", hr
->user_name
);
568 if (arg_json_format_flags
& JSON_FORMAT_OFF
)
569 user_record_show(hr
, true);
571 _cleanup_(user_record_unrefp
) UserRecord
*stripped
= NULL
;
573 if (arg_export_format
== EXPORT_FORMAT_STRIPPED
)
574 r
= user_record_clone(hr
, USER_RECORD_EXTRACT_EMBEDDED
|USER_RECORD_PERMISSIVE
, &stripped
);
575 else if (arg_export_format
== EXPORT_FORMAT_MINIMAL
)
576 r
= user_record_clone(hr
, USER_RECORD_EXTRACT_SIGNABLE
|USER_RECORD_PERMISSIVE
, &stripped
);
580 log_warning_errno(r
, "Failed to strip user record, ignoring: %m");
584 json_variant_dump(hr
->json
, arg_json_format_flags
, stdout
, NULL
);
588 static char **mangle_user_list(char **list
, char ***ret_allocated
) {
589 _cleanup_free_
char *myself
= NULL
;
592 if (!strv_isempty(list
)) {
593 *ret_allocated
= NULL
;
597 myself
= getusername_malloc();
605 l
[0] = TAKE_PTR(myself
);
612 static int inspect_home(int argc
, char *argv
[], void *userdata
) {
613 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
614 _cleanup_(strv_freep
) char **mangled_list
= NULL
;
618 (void) pager_open(arg_pager_flags
);
620 r
= acquire_bus(&bus
);
624 items
= mangle_user_list(strv_skip(argv
, 1), &mangled_list
);
628 STRV_FOREACH(i
, items
) {
629 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
630 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
631 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
632 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
637 r
= parse_uid(*i
, &uid
);
639 if (!valid_user_group_name(*i
, 0)) {
640 log_error("Invalid user name '%s'.", *i
);
647 r
= bus_call_method(bus
, bus_mgr
, "GetUserRecordByName", &error
, &reply
, "s", *i
);
649 r
= bus_call_method(bus
, bus_mgr
, "GetUserRecordByUID", &error
, &reply
, "u", (uint32_t) uid
);
652 log_error_errno(r
, "Failed to inspect home: %s", bus_error_message(&error
, r
));
659 r
= sd_bus_message_read(reply
, "sbo", &json
, &incomplete
, NULL
);
661 bus_log_parse_error(r
);
668 r
= json_parse(json
, JSON_PARSE_SENSITIVE
, &v
, NULL
, NULL
);
670 log_error_errno(r
, "Failed to parse JSON identity: %m");
677 hr
= user_record_new();
681 r
= user_record_load(hr
, v
, USER_RECORD_LOAD_REFUSE_SECRET
|USER_RECORD_LOG
|USER_RECORD_PERMISSIVE
);
689 hr
->incomplete
= incomplete
;
690 dump_home_record(hr
);
696 static int authenticate_home(int argc
, char *argv
[], void *userdata
) {
697 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
698 _cleanup_(strv_freep
) char **mangled_list
= NULL
;
702 items
= mangle_user_list(strv_skip(argv
, 1), &mangled_list
);
706 r
= acquire_bus(&bus
);
710 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
712 STRV_FOREACH(i
, items
) {
713 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
715 r
= acquire_passed_secrets(*i
, &secret
);
720 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
721 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
723 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "AuthenticateHome");
725 return bus_log_create_error(r
);
727 r
= sd_bus_message_append(m
, "s", *i
);
729 return bus_log_create_error(r
);
731 r
= bus_message_append_secret(m
, secret
);
733 return bus_log_create_error(r
);
735 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
737 r
= handle_generic_user_record_error(*i
, secret
, &error
, r
, false);
752 static int update_last_change(JsonVariant
**v
, bool with_password
, bool override
) {
759 n
= now(CLOCK_REALTIME
);
761 c
= json_variant_by_key(*v
, "lastChangeUSec");
766 goto update_password
;
768 if (!json_variant_is_unsigned(c
))
769 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastChangeUSec field is not an unsigned integer, refusing.");
771 u
= json_variant_unsigned(c
);
773 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastChangeUSec is from the future, can't update.");
776 r
= json_variant_set_field_unsigned(v
, "lastChangeUSec", n
);
778 return log_error_errno(r
, "Failed to update lastChangeUSec: %m");
784 c
= json_variant_by_key(*v
, "lastPasswordChangeUSec");
791 if (!json_variant_is_unsigned(c
))
792 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastPasswordChangeUSec field is not an unsigned integer, refusing.");
794 u
= json_variant_unsigned(c
);
796 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastPasswordChangeUSec is from the future, can't update.");
799 r
= json_variant_set_field_unsigned(v
, "lastPasswordChangeUSec", n
);
801 return log_error_errno(r
, "Failed to update lastPasswordChangeUSec: %m");
806 static int apply_identity_changes(JsonVariant
**_v
) {
807 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
812 v
= json_variant_ref(*_v
);
814 r
= json_variant_filter(&v
, arg_identity_filter
);
816 return log_error_errno(r
, "Failed to filter identity: %m");
818 r
= json_variant_merge(&v
, arg_identity_extra
);
820 return log_error_errno(r
, "Failed to merge identities: %m");
822 if (arg_identity_extra_this_machine
|| !strv_isempty(arg_identity_filter
)) {
823 _cleanup_(json_variant_unrefp
) JsonVariant
*per_machine
= NULL
, *mmid
= NULL
;
824 char mids
[SD_ID128_STRING_MAX
];
827 r
= sd_id128_get_machine(&mid
);
829 return log_error_errno(r
, "Failed to acquire machine ID: %m");
831 r
= json_variant_new_string(&mmid
, sd_id128_to_string(mid
, mids
));
833 return log_error_errno(r
, "Failed to allocate matchMachineId object: %m");
835 per_machine
= json_variant_ref(json_variant_by_key(v
, "perMachine"));
837 _cleanup_(json_variant_unrefp
) JsonVariant
*npm
= NULL
, *add
= NULL
;
838 _cleanup_free_ JsonVariant
**array
= NULL
;
842 if (!json_variant_is_array(per_machine
))
843 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "perMachine field is not an array, refusing.");
845 array
= new(JsonVariant
*, json_variant_elements(per_machine
) + 1);
849 JSON_VARIANT_ARRAY_FOREACH(z
, per_machine
) {
852 if (!json_variant_is_object(z
))
853 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "perMachine entry is not an object, refusing.");
857 u
= json_variant_by_key(z
, "matchMachineId");
861 if (!json_variant_equal(u
, mmid
))
864 r
= json_variant_merge(&add
, z
);
866 return log_error_errno(r
, "Failed to merge perMachine entry: %m");
871 r
= json_variant_filter(&add
, arg_identity_filter
);
873 return log_error_errno(r
, "Failed to filter perMachine: %m");
875 r
= json_variant_merge(&add
, arg_identity_extra_this_machine
);
877 return log_error_errno(r
, "Failed to merge in perMachine fields: %m");
879 if (arg_identity_filter_rlimits
|| arg_identity_extra_rlimits
) {
880 _cleanup_(json_variant_unrefp
) JsonVariant
*rlv
= NULL
;
882 rlv
= json_variant_ref(json_variant_by_key(add
, "resourceLimits"));
884 r
= json_variant_filter(&rlv
, arg_identity_filter_rlimits
);
886 return log_error_errno(r
, "Failed to filter resource limits: %m");
888 r
= json_variant_merge(&rlv
, arg_identity_extra_rlimits
);
890 return log_error_errno(r
, "Failed to set resource limits: %m");
892 if (json_variant_is_blank_object(rlv
)) {
893 r
= json_variant_filter(&add
, STRV_MAKE("resourceLimits"));
895 return log_error_errno(r
, "Failed to drop resource limits field from identity: %m");
897 r
= json_variant_set_field(&add
, "resourceLimits", rlv
);
899 return log_error_errno(r
, "Failed to update resource limits of identity: %m");
903 if (!json_variant_is_blank_object(add
)) {
904 r
= json_variant_set_field(&add
, "matchMachineId", mmid
);
906 return log_error_errno(r
, "Failed to set matchMachineId field: %m");
911 r
= json_variant_new_array(&npm
, array
, i
);
913 return log_error_errno(r
, "Failed to allocate new perMachine array: %m");
915 json_variant_unref(per_machine
);
916 per_machine
= TAKE_PTR(npm
);
918 _cleanup_(json_variant_unrefp
) JsonVariant
*item
= json_variant_ref(arg_identity_extra_this_machine
);
920 if (arg_identity_extra_rlimits
) {
921 r
= json_variant_set_field(&item
, "resourceLimits", arg_identity_extra_rlimits
);
923 return log_error_errno(r
, "Failed to update resource limits of identity: %m");
926 r
= json_variant_set_field(&item
, "matchMachineId", mmid
);
928 return log_error_errno(r
, "Failed to set matchMachineId field: %m");
930 r
= json_variant_append_array(&per_machine
, item
);
932 return log_error_errno(r
, "Failed to append to perMachine array: %m");
935 r
= json_variant_set_field(&v
, "perMachine", per_machine
);
937 return log_error_errno(r
, "Failed to update per machine record: %m");
940 if (arg_identity_extra_privileged
|| arg_identity_filter
) {
941 _cleanup_(json_variant_unrefp
) JsonVariant
*privileged
= NULL
;
943 privileged
= json_variant_ref(json_variant_by_key(v
, "privileged"));
945 r
= json_variant_filter(&privileged
, arg_identity_filter
);
947 return log_error_errno(r
, "Failed to filter identity (privileged part): %m");
949 r
= json_variant_merge(&privileged
, arg_identity_extra_privileged
);
951 return log_error_errno(r
, "Failed to merge identities (privileged part): %m");
953 if (json_variant_is_blank_object(privileged
)) {
954 r
= json_variant_filter(&v
, STRV_MAKE("privileged"));
956 return log_error_errno(r
, "Failed to drop privileged part from identity: %m");
958 r
= json_variant_set_field(&v
, "privileged", privileged
);
960 return log_error_errno(r
, "Failed to update privileged part of identity: %m");
964 if (arg_identity_filter_rlimits
) {
965 _cleanup_(json_variant_unrefp
) JsonVariant
*rlv
= NULL
;
967 rlv
= json_variant_ref(json_variant_by_key(v
, "resourceLimits"));
969 r
= json_variant_filter(&rlv
, arg_identity_filter_rlimits
);
971 return log_error_errno(r
, "Failed to filter resource limits: %m");
973 /* Note that we only filter resource limits here, but don't apply them. We do that in the perMachine section */
975 if (json_variant_is_blank_object(rlv
)) {
976 r
= json_variant_filter(&v
, STRV_MAKE("resourceLimits"));
978 return log_error_errno(r
, "Failed to drop resource limits field from identity: %m");
980 r
= json_variant_set_field(&v
, "resourceLimits", rlv
);
982 return log_error_errno(r
, "Failed to update resource limits of identity: %m");
986 json_variant_unref(*_v
);
992 static int add_disposition(JsonVariant
**v
) {
997 if (json_variant_by_key(*v
, "disposition"))
1000 /* Set the disposition to regular, if not configured explicitly */
1001 r
= json_variant_set_field_string(v
, "disposition", "regular");
1003 return log_error_errno(r
, "Failed to set disposition field: %m");
1008 static int acquire_new_home_record(UserRecord
**ret
) {
1009 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
1010 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
1017 unsigned line
, column
;
1019 r
= json_parse_file(
1020 streq(arg_identity
, "-") ? stdin
: NULL
,
1021 streq(arg_identity
, "-") ? "<stdin>" : arg_identity
, JSON_PARSE_SENSITIVE
, &v
, &line
, &column
);
1023 return log_error_errno(r
, "Failed to parse identity at %u:%u: %m", line
, column
);
1026 r
= apply_identity_changes(&v
);
1030 r
= add_disposition(&v
);
1034 STRV_FOREACH(i
, arg_pkcs11_token_uri
) {
1035 r
= identity_add_pkcs11_key_data(&v
, *i
);
1040 STRV_FOREACH(i
, arg_fido2_device
) {
1041 r
= identity_add_fido2_parameters(&v
, *i
, arg_fido2_lock_with
);
1046 if (arg_recovery_key
) {
1047 r
= identity_add_recovery_key(&v
);
1052 r
= update_last_change(&v
, true, false);
1057 json_variant_dump(v
, JSON_FORMAT_PRETTY
, NULL
, NULL
);
1059 hr
= user_record_new();
1063 r
= user_record_load(hr
, v
, USER_RECORD_REQUIRE_REGULAR
|USER_RECORD_ALLOW_SECRET
|USER_RECORD_ALLOW_PRIVILEGED
|USER_RECORD_ALLOW_PER_MACHINE
|USER_RECORD_ALLOW_SIGNATURE
|USER_RECORD_LOG
|USER_RECORD_PERMISSIVE
);
1067 *ret
= TAKE_PTR(hr
);
1071 static int acquire_new_password(
1072 const char *user_name
,
1084 e
= getenv("NEWPASSWORD");
1086 _cleanup_(erase_and_freep
) char *copy
= NULL
;
1088 /* As above, this is not for use, just for testing */
1096 r
= user_record_set_password(hr
, STRV_MAKE(e
), /* prepend = */ true);
1098 return log_error_errno(r
, "Failed to store password: %m");
1101 assert_se(unsetenv("NEWPASSWORD") == 0);
1104 *ret
= TAKE_PTR(copy
);
1110 (void) suggest_passwords();
1113 _cleanup_(strv_free_erasep
) char **first
= NULL
, **second
= NULL
;
1114 _cleanup_free_
char *question
= NULL
;
1117 return log_error_errno(SYNTHETIC_ERRNO(ENOKEY
), "Too many attempts, giving up:");
1119 if (asprintf(&question
, "Please enter new password for user %s:", user_name
) < 0)
1122 r
= ask_password_auto(
1124 /* icon= */ "user-home",
1126 /* key_name= */ "home-password",
1127 /* credential_name= */ "home.new-password",
1129 0, /* no caching, we want to collect a new password here after all */
1132 return log_error_errno(r
, "Failed to acquire password: %m");
1134 question
= mfree(question
);
1135 if (asprintf(&question
, "Please enter new password for user %s (repeat):", user_name
) < 0)
1138 r
= ask_password_auto(
1140 /* icon= */ "user-home",
1142 /* key_name= */ "home-password",
1143 /* credential_name= */ "home.new-password",
1148 return log_error_errno(r
, "Failed to acquire password: %m");
1150 if (strv_equal(first
, second
)) {
1151 _cleanup_(erase_and_freep
) char *copy
= NULL
;
1154 copy
= strdup(first
[0]);
1159 r
= user_record_set_password(hr
, first
, /* prepend = */ true);
1161 return log_error_errno(r
, "Failed to store password: %m");
1164 *ret
= TAKE_PTR(copy
);
1169 log_error("Password didn't match, try again.");
1173 static int create_home(int argc
, char *argv
[], void *userdata
) {
1174 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1175 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
1178 r
= acquire_bus(&bus
);
1182 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1185 /* If a username was specified, use it */
1187 if (valid_user_group_name(argv
[1], 0))
1188 r
= json_variant_set_field_string(&arg_identity_extra
, "userName", argv
[1]);
1190 _cleanup_free_
char *un
= NULL
, *rr
= NULL
;
1192 /* Before we consider the user name invalid, let's check if we can split it? */
1193 r
= split_user_name_realm(argv
[1], &un
, &rr
);
1195 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User name '%s' is not valid: %m", argv
[1]);
1198 r
= json_variant_set_field_string(&arg_identity_extra
, "realm", rr
);
1200 return log_error_errno(r
, "Failed to set realm field: %m");
1203 r
= json_variant_set_field_string(&arg_identity_extra
, "userName", un
);
1206 return log_error_errno(r
, "Failed to set userName field: %m");
1208 /* If neither a username nor an identity have been specified we cannot operate. */
1210 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User name required.");
1213 r
= acquire_new_home_record(&hr
);
1217 /* If the JSON record carries no plain text password (besides the recovery key), then let's query it
1219 if (strv_length(hr
->password
) <= arg_recovery_key
) {
1221 if (strv_isempty(hr
->hashed_password
)) {
1222 _cleanup_(erase_and_freep
) char *new_password
= NULL
;
1224 /* No regular (i.e. non-PKCS#11) hashed passwords set in the record, let's fix that. */
1225 r
= acquire_new_password(hr
->user_name
, hr
, /* suggest = */ true, &new_password
);
1229 r
= user_record_make_hashed_password(hr
, STRV_MAKE(new_password
), /* extend = */ false);
1231 return log_error_errno(r
, "Failed to hash password: %m");
1233 /* There's a hash password set in the record, acquire the unhashed version of it. */
1234 r
= acquire_existing_password(
1237 /* emphasize_current= */ false,
1238 ASK_PASSWORD_ACCEPT_CACHED
| ASK_PASSWORD_PUSH_CACHE
);
1244 if (hr
->enforce_password_policy
== 0) {
1245 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1247 /* If password quality enforcement is disabled, let's at least warn client side */
1249 r
= user_record_quality_check_password(hr
, hr
, &error
);
1251 log_warning_errno(r
, "Specified password does not pass quality checks (%s), proceeding anyway.", bus_error_message(&error
, r
));
1255 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1256 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1257 _cleanup_(erase_and_freep
) char *formatted
= NULL
;
1259 r
= json_variant_format(hr
->json
, 0, &formatted
);
1261 return log_error_errno(r
, "Failed to format user record: %m");
1263 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "CreateHome");
1265 return bus_log_create_error(r
);
1267 (void) sd_bus_message_sensitive(m
);
1269 r
= sd_bus_message_append(m
, "s", formatted
);
1271 return bus_log_create_error(r
);
1273 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1275 if (sd_bus_error_has_name(&error
, BUS_ERROR_LOW_PASSWORD_QUALITY
)) {
1276 _cleanup_(erase_and_freep
) char *new_password
= NULL
;
1278 log_error_errno(r
, "%s", bus_error_message(&error
, r
));
1279 log_info("(Use --enforce-password-policy=no to turn off password quality checks for this account.)");
1281 r
= acquire_new_password(hr
->user_name
, hr
, /* suggest = */ false, &new_password
);
1285 r
= user_record_make_hashed_password(hr
, STRV_MAKE(new_password
), /* extend = */ false);
1287 return log_error_errno(r
, "Failed to hash passwords: %m");
1289 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1300 static int remove_home(int argc
, char *argv
[], void *userdata
) {
1301 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1305 r
= acquire_bus(&bus
);
1309 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1311 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
1312 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1313 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1315 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "RemoveHome");
1317 return bus_log_create_error(r
);
1319 r
= sd_bus_message_append(m
, "s", *i
);
1321 return bus_log_create_error(r
);
1323 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1325 log_error_errno(r
, "Failed to remove home: %s", bus_error_message(&error
, r
));
1334 static int acquire_updated_home_record(
1336 const char *username
,
1339 _cleanup_(json_variant_unrefp
) JsonVariant
*json
= NULL
;
1340 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
1347 unsigned line
, column
;
1350 r
= json_parse_file(
1351 streq(arg_identity
, "-") ? stdin
: NULL
,
1352 streq(arg_identity
, "-") ? "<stdin>" : arg_identity
, JSON_PARSE_SENSITIVE
, &json
, &line
, &column
);
1354 return log_error_errno(r
, "Failed to parse identity at %u:%u: %m", line
, column
);
1356 un
= json_variant_by_key(json
, "userName");
1358 if (!json_variant_is_string(un
) || (username
&& !streq(json_variant_string(un
), username
)))
1359 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User name specified on command line and in JSON record do not match.");
1362 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "No username specified.");
1364 r
= json_variant_set_field_string(&arg_identity_extra
, "userName", username
);
1366 return log_error_errno(r
, "Failed to set userName field: %m");
1370 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1371 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1375 if (!identity_properties_specified())
1376 return log_error_errno(SYNTHETIC_ERRNO(EALREADY
), "No field to change specified.");
1378 r
= bus_call_method(bus
, bus_mgr
, "GetUserRecordByName", &error
, &reply
, "s", username
);
1380 return log_error_errno(r
, "Failed to acquire user home record: %s", bus_error_message(&error
, r
));
1382 r
= sd_bus_message_read(reply
, "sbo", &text
, &incomplete
, NULL
);
1384 return bus_log_parse_error(r
);
1387 return log_error_errno(SYNTHETIC_ERRNO(EACCES
), "Lacking rights to acquire user record including privileged metadata, can't update record.");
1389 r
= json_parse(text
, JSON_PARSE_SENSITIVE
, &json
, NULL
, NULL
);
1391 return log_error_errno(r
, "Failed to parse JSON identity: %m");
1393 reply
= sd_bus_message_unref(reply
);
1395 r
= json_variant_filter(&json
, STRV_MAKE("binding", "status", "signature"));
1397 return log_error_errno(r
, "Failed to strip binding and status from record to update: %m");
1400 r
= apply_identity_changes(&json
);
1404 STRV_FOREACH(i
, arg_pkcs11_token_uri
) {
1405 r
= identity_add_pkcs11_key_data(&json
, *i
);
1410 STRV_FOREACH(i
, arg_fido2_device
) {
1411 r
= identity_add_fido2_parameters(&json
, *i
, arg_fido2_lock_with
);
1416 /* If the user supplied a full record, then add in lastChange, but do not override. Otherwise always
1418 r
= update_last_change(&json
, arg_pkcs11_token_uri
|| arg_fido2_device
, !arg_identity
);
1423 json_variant_dump(json
, JSON_FORMAT_PRETTY
, NULL
, NULL
);
1425 hr
= user_record_new();
1429 r
= user_record_load(hr
, json
, USER_RECORD_REQUIRE_REGULAR
|USER_RECORD_ALLOW_PRIVILEGED
|USER_RECORD_ALLOW_PER_MACHINE
|USER_RECORD_ALLOW_SECRET
|USER_RECORD_ALLOW_SIGNATURE
|USER_RECORD_LOG
|USER_RECORD_PERMISSIVE
);
1433 *ret
= TAKE_PTR(hr
);
1437 static int home_record_reset_human_interaction_permission(UserRecord
*hr
) {
1442 /* When we execute multiple operations one after the other, let's reset the permission to ask the
1443 * user each time, so that if interaction is necessary we will be told so again and thus can print a
1444 * nice message to the user, telling the user so. */
1446 r
= user_record_set_pkcs11_protected_authentication_path_permitted(hr
, -1);
1448 return log_error_errno(r
, "Failed to reset PKCS#11 protected authentication path permission flag: %m");
1450 r
= user_record_set_fido2_user_presence_permitted(hr
, -1);
1452 return log_error_errno(r
, "Failed to reset FIDO2 user presence permission flag: %m");
1454 r
= user_record_set_fido2_user_verification_permitted(hr
, -1);
1456 return log_error_errno(r
, "Failed to reset FIDO2 user verification permission flag: %m");
1461 static int update_home(int argc
, char *argv
[], void *userdata
) {
1462 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1463 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
1464 _cleanup_free_
char *buffer
= NULL
;
1465 const char *username
;
1470 else if (!arg_identity
) {
1471 buffer
= getusername_malloc();
1479 r
= acquire_bus(&bus
);
1483 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1485 r
= acquire_updated_home_record(bus
, username
, &hr
);
1489 /* If we do multiple operations, let's output things more verbosely, since otherwise the repeated
1490 * authentication might be confusing. */
1492 if (arg_and_resize
|| arg_and_change_password
)
1493 log_info("Updating home directory.");
1496 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1497 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1498 _cleanup_free_
char *formatted
= NULL
;
1500 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "UpdateHome");
1502 return bus_log_create_error(r
);
1504 r
= json_variant_format(hr
->json
, 0, &formatted
);
1506 return log_error_errno(r
, "Failed to format user record: %m");
1508 (void) sd_bus_message_sensitive(m
);
1510 r
= sd_bus_message_append(m
, "s", formatted
);
1512 return bus_log_create_error(r
);
1514 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1516 if (arg_and_change_password
&&
1517 sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1518 /* In the generic handler we'd ask for a password in this case, but when
1519 * changing passwords that's not sufficient, as we need to acquire all keys
1521 return log_error_errno(r
, "Security token not inserted, refusing.");
1523 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1531 log_info("Resizing home.");
1533 (void) home_record_reset_human_interaction_permission(hr
);
1535 /* Also sync down disk size to underlying LUKS/fscrypt/quota */
1536 while (arg_and_resize
) {
1537 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1538 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1540 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ResizeHome");
1542 return bus_log_create_error(r
);
1544 /* Specify UINT64_MAX as size, in which case the underlying disk size will just be synced */
1545 r
= sd_bus_message_append(m
, "st", hr
->user_name
, UINT64_MAX
);
1547 return bus_log_create_error(r
);
1549 r
= bus_message_append_secret(m
, hr
);
1551 return bus_log_create_error(r
);
1553 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1555 if (arg_and_change_password
&&
1556 sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1557 return log_error_errno(r
, "Security token not inserted, refusing.");
1559 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1566 if (arg_and_change_password
)
1567 log_info("Synchronizing passwords and encryption keys.");
1569 (void) home_record_reset_human_interaction_permission(hr
);
1571 /* Also sync down passwords to underlying LUKS/fscrypt */
1572 while (arg_and_change_password
) {
1573 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1574 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1576 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ChangePasswordHome");
1578 return bus_log_create_error(r
);
1580 /* Specify an empty new secret, in which case the underlying LUKS/fscrypt password will just be synced */
1581 r
= sd_bus_message_append(m
, "ss", hr
->user_name
, "{}");
1583 return bus_log_create_error(r
);
1585 r
= bus_message_append_secret(m
, hr
);
1587 return bus_log_create_error(r
);
1589 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1591 if (sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1592 return log_error_errno(r
, "Security token not inserted, refusing.");
1594 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1604 static int passwd_home(int argc
, char *argv
[], void *userdata
) {
1605 _cleanup_(user_record_unrefp
) UserRecord
*old_secret
= NULL
, *new_secret
= NULL
;
1606 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1607 _cleanup_free_
char *buffer
= NULL
;
1608 const char *username
;
1611 if (arg_pkcs11_token_uri
)
1612 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "To change the PKCS#11 security token use 'homectl update --pkcs11-token-uri=…'.");
1613 if (arg_fido2_device
)
1614 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "To change the FIDO2 security token use 'homectl update --fido2-device=…'.");
1615 if (identity_properties_specified())
1616 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "The 'passwd' verb does not permit changing other record properties at the same time.");
1621 buffer
= getusername_malloc();
1628 r
= acquire_bus(&bus
);
1632 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1634 old_secret
= user_record_new();
1638 new_secret
= user_record_new();
1642 r
= acquire_new_password(username
, new_secret
, /* suggest = */ true, NULL
);
1647 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1648 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1650 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ChangePasswordHome");
1652 return bus_log_create_error(r
);
1654 r
= sd_bus_message_append(m
, "s", username
);
1656 return bus_log_create_error(r
);
1658 r
= bus_message_append_secret(m
, new_secret
);
1660 return bus_log_create_error(r
);
1662 r
= bus_message_append_secret(m
, old_secret
);
1664 return bus_log_create_error(r
);
1666 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1668 if (sd_bus_error_has_name(&error
, BUS_ERROR_LOW_PASSWORD_QUALITY
)) {
1670 log_error_errno(r
, "%s", bus_error_message(&error
, r
));
1672 r
= acquire_new_password(username
, new_secret
, /* suggest = */ false, NULL
);
1674 } else if (sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1676 /* In the generic handler we'd ask for a password in this case, but when
1677 * changing passwords that's not sufficeint, as we need to acquire all keys
1679 return log_error_errno(r
, "Security token not inserted, refusing.");
1681 r
= handle_generic_user_record_error(username
, old_secret
, &error
, r
, true);
1691 static int resize_home(int argc
, char *argv
[], void *userdata
) {
1692 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1693 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
1694 uint64_t ds
= UINT64_MAX
;
1697 r
= acquire_bus(&bus
);
1701 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1703 if (arg_disk_size_relative
!= UINT64_MAX
||
1704 (argc
> 2 && parse_permyriad(argv
[2]) >= 0))
1705 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
1706 "Relative disk size specification currently not supported when resizing.");
1709 r
= parse_size(argv
[2], 1024, &ds
);
1711 return log_error_errno(r
, "Failed to parse disk size parameter: %s", argv
[2]);
1714 if (arg_disk_size
!= UINT64_MAX
) {
1715 if (ds
!= UINT64_MAX
&& ds
!= arg_disk_size
)
1716 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Disk size specified twice and doesn't match, refusing.");
1721 r
= acquire_passed_secrets(argv
[1], &secret
);
1726 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1727 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1729 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ResizeHome");
1731 return bus_log_create_error(r
);
1733 r
= sd_bus_message_append(m
, "st", argv
[1], ds
);
1735 return bus_log_create_error(r
);
1737 r
= bus_message_append_secret(m
, secret
);
1739 return bus_log_create_error(r
);
1741 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1743 r
= handle_generic_user_record_error(argv
[1], secret
, &error
, r
, false);
1753 static int lock_home(int argc
, char *argv
[], void *userdata
) {
1754 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1758 r
= acquire_bus(&bus
);
1762 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
1763 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1764 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1766 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "LockHome");
1768 return bus_log_create_error(r
);
1770 r
= sd_bus_message_append(m
, "s", *i
);
1772 return bus_log_create_error(r
);
1774 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1776 log_error_errno(r
, "Failed to lock home: %s", bus_error_message(&error
, r
));
1785 static int unlock_home(int argc
, char *argv
[], void *userdata
) {
1786 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1790 r
= acquire_bus(&bus
);
1794 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
1795 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
1797 r
= acquire_passed_secrets(*i
, &secret
);
1802 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1803 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1805 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "UnlockHome");
1807 return bus_log_create_error(r
);
1809 r
= sd_bus_message_append(m
, "s", *i
);
1811 return bus_log_create_error(r
);
1813 r
= bus_message_append_secret(m
, secret
);
1815 return bus_log_create_error(r
);
1817 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1819 r
= handle_generic_user_record_error(argv
[1], secret
, &error
, r
, false);
1834 static int with_home(int argc
, char *argv
[], void *userdata
) {
1835 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1836 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1837 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1838 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
1839 _cleanup_close_
int acquired_fd
= -1;
1840 _cleanup_strv_free_
char **cmdline
= NULL
;
1845 r
= acquire_bus(&bus
);
1850 _cleanup_free_
char *shell
= NULL
;
1852 /* If no command is specified, spawn a shell */
1853 r
= get_shell(&shell
);
1855 return log_error_errno(r
, "Failed to acquire shell: %m");
1857 cmdline
= strv_new(shell
);
1859 cmdline
= strv_copy(argv
+ 2);
1863 r
= acquire_passed_secrets(argv
[1], &secret
);
1868 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "AcquireHome");
1870 return bus_log_create_error(r
);
1872 r
= sd_bus_message_append(m
, "s", argv
[1]);
1874 return bus_log_create_error(r
);
1876 r
= bus_message_append_secret(m
, secret
);
1878 return bus_log_create_error(r
);
1880 r
= sd_bus_message_append(m
, "b", /* please_suspend = */ getenv_bool("SYSTEMD_PLEASE_SUSPEND_HOME") > 0);
1882 return bus_log_create_error(r
);
1884 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, &reply
);
1885 m
= sd_bus_message_unref(m
);
1887 r
= handle_generic_user_record_error(argv
[1], secret
, &error
, r
, false);
1891 sd_bus_error_free(&error
);
1895 r
= sd_bus_message_read(reply
, "h", &fd
);
1897 return bus_log_parse_error(r
);
1899 acquired_fd
= fcntl(fd
, F_DUPFD_CLOEXEC
, 3);
1900 if (acquired_fd
< 0)
1901 return log_error_errno(errno
, "Failed to duplicate acquired fd: %m");
1903 reply
= sd_bus_message_unref(reply
);
1908 r
= bus_call_method(bus
, bus_mgr
, "GetHomeByName", &error
, &reply
, "s", argv
[1]);
1910 return log_error_errno(r
, "Failed to inspect home: %s", bus_error_message(&error
, r
));
1912 r
= sd_bus_message_read(reply
, "usussso", NULL
, NULL
, NULL
, NULL
, &home
, NULL
, NULL
);
1914 return bus_log_parse_error(r
);
1916 r
= safe_fork("(with)", FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_DEATHSIG
|FORK_LOG
|FORK_RLIMIT_NOFILE_SAFE
|FORK_REOPEN_LOG
, &pid
);
1920 if (chdir(home
) < 0) {
1921 log_error_errno(errno
, "Failed to change to directory %s: %m", home
);
1925 execvp(cmdline
[0], cmdline
);
1926 log_error_errno(errno
, "Failed to execute %s: %m", cmdline
[0]);
1930 ret
= wait_for_terminate_and_check(cmdline
[0], pid
, WAIT_LOG_ABNORMAL
);
1932 /* Close the fd that pings the home now. */
1933 acquired_fd
= safe_close(acquired_fd
);
1935 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ReleaseHome");
1937 return bus_log_create_error(r
);
1939 r
= sd_bus_message_append(m
, "s", argv
[1]);
1941 return bus_log_create_error(r
);
1943 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1945 if (sd_bus_error_has_name(&error
, BUS_ERROR_HOME_BUSY
))
1946 log_notice("Not deactivating home directory of %s, as it is still used.", argv
[1]);
1948 return log_error_errno(r
, "Failed to release user home: %s", bus_error_message(&error
, r
));
1954 static int lock_all_homes(int argc
, char *argv
[], void *userdata
) {
1955 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1956 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1957 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1960 r
= acquire_bus(&bus
);
1964 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "LockAllHomes");
1966 return bus_log_create_error(r
);
1968 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1970 return log_error_errno(r
, "Failed to lock all homes: %s", bus_error_message(&error
, r
));
1975 static int deactivate_all_homes(int argc
, char *argv
[], void *userdata
) {
1976 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1977 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1978 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1981 r
= acquire_bus(&bus
);
1985 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "DeactivateAllHomes");
1987 return bus_log_create_error(r
);
1989 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1991 return log_error_errno(r
, "Failed to deactivate all homes: %s", bus_error_message(&error
, r
));
1996 static int drop_from_identity(const char *field
) {
2001 /* If we are called to update an identity record and drop some field, let's keep track of what to
2002 * remove from the old record */
2003 r
= strv_extend(&arg_identity_filter
, field
);
2007 /* Let's also drop the field if it was previously set to a new value on the same command line */
2008 r
= json_variant_filter(&arg_identity_extra
, STRV_MAKE(field
));
2010 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
2012 r
= json_variant_filter(&arg_identity_extra_this_machine
, STRV_MAKE(field
));
2014 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
2016 r
= json_variant_filter(&arg_identity_extra_privileged
, STRV_MAKE(field
));
2018 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
2023 static int help(int argc
, char *argv
[], void *userdata
) {
2024 _cleanup_free_
char *link
= NULL
;
2027 (void) pager_open(arg_pager_flags
);
2029 r
= terminal_urlify_man("homectl", "1", &link
);
2033 printf("%1$s [OPTIONS...] COMMAND ...\n\n"
2034 "%2$sCreate, manipulate or inspect home directories.%3$s\n"
2035 "\n%4$sCommands:%5$s\n"
2036 " list List home areas\n"
2037 " activate USER… Activate a home area\n"
2038 " deactivate USER… Deactivate a home area\n"
2039 " inspect USER… Inspect a home area\n"
2040 " authenticate USER… Authenticate a home area\n"
2041 " create USER Create a home area\n"
2042 " remove USER… Remove a home area\n"
2043 " update USER Update a home area\n"
2044 " passwd USER Change password of a home area\n"
2045 " resize USER SIZE Resize a home area\n"
2046 " lock USER… Temporarily lock an active home area\n"
2047 " unlock USER… Unlock a temporarily locked home area\n"
2048 " lock-all Lock all suitable home areas\n"
2049 " deactivate-all Deactivate all active home areas\n"
2050 " with USER [COMMAND…] Run shell or command with access to a home area\n"
2051 "\n%4$sOptions:%5$s\n"
2052 " -h --help Show this help\n"
2053 " --version Show package version\n"
2054 " --no-pager Do not pipe output into a pager\n"
2055 " --no-legend Do not show the headers and footers\n"
2056 " --no-ask-password Do not ask for system passwords\n"
2057 " -H --host=[USER@]HOST Operate on remote host\n"
2058 " -M --machine=CONTAINER Operate on local container\n"
2059 " --identity=PATH Read JSON identity from file\n"
2060 " --json=FORMAT Output inspection data in JSON (takes one of\n"
2061 " pretty, short, off)\n"
2062 " -j Equivalent to --json=pretty (on TTY) or\n"
2063 " --json=short (otherwise)\n"
2064 " --export-format= Strip JSON inspection data (full, stripped,\n"
2066 " -E When specified once equals -j --export-format=\n"
2067 " stripped, when specified twice equals\n"
2068 " -j --export-format=minimal\n"
2069 "\n%4$sGeneral User Record Properties:%5$s\n"
2070 " -c --real-name=REALNAME Real name for user\n"
2071 " --realm=REALM Realm to create user in\n"
2072 " --email-address=EMAIL Email address for user\n"
2073 " --location=LOCATION Set location of user on earth\n"
2074 " --icon-name=NAME Icon name for user\n"
2075 " -d --home-dir=PATH Home directory\n"
2076 " -u --uid=UID Numeric UID for user\n"
2077 " -G --member-of=GROUP Add user to group\n"
2078 " --skel=PATH Skeleton directory to use\n"
2079 " --shell=PATH Shell for account\n"
2080 " --setenv=VARIABLE=VALUE Set an environment variable at log-in\n"
2081 " --timezone=TIMEZONE Set a time-zone\n"
2082 " --language=LOCALE Set preferred language\n"
2083 " --ssh-authorized-keys=KEYS\n"
2084 " Specify SSH public keys\n"
2085 " --pkcs11-token-uri=URI URI to PKCS#11 security token containing\n"
2086 " private key and matching X.509 certificate\n"
2087 " --fido2-device=PATH Path to FIDO2 hidraw device with hmac-secret\n"
2089 " --fido2-with-client-pin=BOOL\n"
2090 " Whether to require entering a PIN to unlock the\n"
2092 " --fido2-with-user-presence=BOOL\n"
2093 " Whether to require user presence to unlock the\n"
2095 " --fido2-with-user-verification=BOOL\n"
2096 " Whether to require user verification to unlock the\n"
2098 " --recovery-key=BOOL Add a recovery key\n"
2099 "\n%4$sAccount Management User Record Properties:%5$s\n"
2100 " --locked=BOOL Set locked account state\n"
2101 " --not-before=TIMESTAMP Do not allow logins before\n"
2102 " --not-after=TIMESTAMP Do not allow logins after\n"
2103 " --rate-limit-interval=SECS\n"
2104 " Login rate-limit interval in seconds\n"
2105 " --rate-limit-burst=NUMBER\n"
2106 " Login rate-limit attempts per interval\n"
2107 "\n%4$sPassword Policy User Record Properties:%5$s\n"
2108 " --password-hint=HINT Set Password hint\n"
2109 " --enforce-password-policy=BOOL\n"
2110 " Control whether to enforce system's password\n"
2111 " policy for this user\n"
2112 " -P Equivalent to --enforce-password-password=no\n"
2113 " --password-change-now=BOOL\n"
2114 " Require the password to be changed on next login\n"
2115 " --password-change-min=TIME\n"
2116 " Require minimum time between password changes\n"
2117 " --password-change-max=TIME\n"
2118 " Require maximum time between password changes\n"
2119 " --password-change-warn=TIME\n"
2120 " How much time to warn before password expiry\n"
2121 " --password-change-inactive=TIME\n"
2122 " How much time to block password after expiry\n"
2123 "\n%4$sResource Management User Record Properties:%5$s\n"
2124 " --disk-size=BYTES Size to assign the user on disk\n"
2125 " --access-mode=MODE User home directory access mode\n"
2126 " --umask=MODE Umask for user when logging in\n"
2127 " --nice=NICE Nice level for user\n"
2128 " --rlimit=LIMIT=VALUE[:VALUE]\n"
2129 " Set resource limits\n"
2130 " --tasks-max=MAX Set maximum number of per-user tasks\n"
2131 " --memory-high=BYTES Set high memory threshold in bytes\n"
2132 " --memory-max=BYTES Set maximum memory limit\n"
2133 " --cpu-weight=WEIGHT Set CPU weight\n"
2134 " --io-weight=WEIGHT Set IO weight\n"
2135 "\n%4$sStorage User Record Properties:%5$s\n"
2136 " --storage=STORAGE Storage type to use (luks, fscrypt, directory,\n"
2137 " subvolume, cifs)\n"
2138 " --image-path=PATH Path to image file/directory\n"
2139 "\n%4$sLUKS Storage User Record Properties:%5$s\n"
2140 " --fs-type=TYPE File system type to use in case of luks\n"
2141 " storage (btrfs, ext4, xfs)\n"
2142 " --luks-discard=BOOL Whether to use 'discard' feature of file system\n"
2143 " when activated (mounted)\n"
2144 " --luks-offline-discard=BOOL\n"
2145 " Whether to trim file on logout\n"
2146 " --luks-cipher=CIPHER Cipher to use for LUKS encryption\n"
2147 " --luks-cipher-mode=MODE Cipher mode to use for LUKS encryption\n"
2148 " --luks-volume-key-size=BITS\n"
2149 " Volume key size to use for LUKS encryption\n"
2150 " --luks-pbkdf-type=TYPE Password-based Key Derivation Function to use\n"
2151 " --luks-pbkdf-hash-algorithm=ALGORITHM\n"
2152 " PBKDF hash algorithm to use\n"
2153 " --luks-pbkdf-time-cost=SECS\n"
2154 " Time cost for PBKDF in seconds\n"
2155 " --luks-pbkdf-memory-cost=BYTES\n"
2156 " Memory cost for PBKDF in bytes\n"
2157 " --luks-pbkdf-parallel-threads=NUMBER\n"
2158 " Number of parallel threads for PKBDF\n"
2159 "\n%4$sMounting User Record Properties:%5$s\n"
2160 " --nosuid=BOOL Control the 'nosuid' flag of the home mount\n"
2161 " --nodev=BOOL Control the 'nodev' flag of the home mount\n"
2162 " --noexec=BOOL Control the 'noexec' flag of the home mount\n"
2163 "\n%4$sCIFS User Record Properties:%5$s\n"
2164 " --cifs-domain=DOMAIN CIFS (Windows) domain\n"
2165 " --cifs-user-name=USER CIFS (Windows) user name\n"
2166 " --cifs-service=SERVICE CIFS (Windows) service to mount as home area\n"
2167 "\n%4$sLogin Behaviour User Record Properties:%5$s\n"
2168 " --stop-delay=SECS How long to leave user services running after\n"
2170 " --kill-processes=BOOL Whether to kill user processes when sessions\n"
2172 " --auto-login=BOOL Try to log this user in automatically\n"
2173 "\nSee the %6$s for details.\n",
2174 program_invocation_short_name
,
2184 static int parse_argv(int argc
, char *argv
[]) {
2187 ARG_VERSION
= 0x100,
2190 ARG_NO_ASK_PASSWORD
,
2200 ARG_LUKS_OFFLINE_DISCARD
,
2206 ARG_SSH_AUTHORIZED_KEYS
,
2215 ARG_LUKS_CIPHER_MODE
,
2216 ARG_LUKS_VOLUME_KEY_SIZE
,
2228 ARG_LUKS_PBKDF_TYPE
,
2229 ARG_LUKS_PBKDF_HASH_ALGORITHM
,
2230 ARG_LUKS_PBKDF_TIME_COST
,
2231 ARG_LUKS_PBKDF_MEMORY_COST
,
2232 ARG_LUKS_PBKDF_PARALLEL_THREADS
,
2233 ARG_RATE_LIMIT_INTERVAL
,
2234 ARG_RATE_LIMIT_BURST
,
2237 ARG_ENFORCE_PASSWORD_POLICY
,
2238 ARG_PASSWORD_CHANGE_NOW
,
2239 ARG_PASSWORD_CHANGE_MIN
,
2240 ARG_PASSWORD_CHANGE_MAX
,
2241 ARG_PASSWORD_CHANGE_WARN
,
2242 ARG_PASSWORD_CHANGE_INACTIVE
,
2245 ARG_PKCS11_TOKEN_URI
,
2252 ARG_AND_CHANGE_PASSWORD
,
2255 static const struct option options
[] = {
2256 { "help", no_argument
, NULL
, 'h' },
2257 { "version", no_argument
, NULL
, ARG_VERSION
},
2258 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
2259 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
2260 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
2261 { "host", required_argument
, NULL
, 'H' },
2262 { "machine", required_argument
, NULL
, 'M' },
2263 { "identity", required_argument
, NULL
, 'I' },
2264 { "real-name", required_argument
, NULL
, 'c' },
2265 { "comment", required_argument
, NULL
, 'c' }, /* Compat alias to keep thing in sync with useradd(8) */
2266 { "realm", required_argument
, NULL
, ARG_REALM
},
2267 { "email-address", required_argument
, NULL
, ARG_EMAIL_ADDRESS
},
2268 { "location", required_argument
, NULL
, ARG_LOCATION
},
2269 { "password-hint", required_argument
, NULL
, ARG_PASSWORD_HINT
},
2270 { "icon-name", required_argument
, NULL
, ARG_ICON_NAME
},
2271 { "home-dir", required_argument
, NULL
, 'd' }, /* Compatible with useradd(8) */
2272 { "uid", required_argument
, NULL
, 'u' }, /* Compatible with useradd(8) */
2273 { "member-of", required_argument
, NULL
, 'G' },
2274 { "groups", required_argument
, NULL
, 'G' }, /* Compat alias to keep thing in sync with useradd(8) */
2275 { "skel", required_argument
, NULL
, 'k' }, /* Compatible with useradd(8) */
2276 { "shell", required_argument
, NULL
, 's' }, /* Compatible with useradd(8) */
2277 { "setenv", required_argument
, NULL
, ARG_SETENV
},
2278 { "timezone", required_argument
, NULL
, ARG_TIMEZONE
},
2279 { "language", required_argument
, NULL
, ARG_LANGUAGE
},
2280 { "locked", required_argument
, NULL
, ARG_LOCKED
},
2281 { "not-before", required_argument
, NULL
, ARG_NOT_BEFORE
},
2282 { "not-after", required_argument
, NULL
, ARG_NOT_AFTER
},
2283 { "expiredate", required_argument
, NULL
, 'e' }, /* Compat alias to keep thing in sync with useradd(8) */
2284 { "ssh-authorized-keys", required_argument
, NULL
, ARG_SSH_AUTHORIZED_KEYS
},
2285 { "disk-size", required_argument
, NULL
, ARG_DISK_SIZE
},
2286 { "access-mode", required_argument
, NULL
, ARG_ACCESS_MODE
},
2287 { "umask", required_argument
, NULL
, ARG_UMASK
},
2288 { "nice", required_argument
, NULL
, ARG_NICE
},
2289 { "rlimit", required_argument
, NULL
, ARG_RLIMIT
},
2290 { "tasks-max", required_argument
, NULL
, ARG_TASKS_MAX
},
2291 { "memory-high", required_argument
, NULL
, ARG_MEMORY_HIGH
},
2292 { "memory-max", required_argument
, NULL
, ARG_MEMORY_MAX
},
2293 { "cpu-weight", required_argument
, NULL
, ARG_CPU_WEIGHT
},
2294 { "io-weight", required_argument
, NULL
, ARG_IO_WEIGHT
},
2295 { "storage", required_argument
, NULL
, ARG_STORAGE
},
2296 { "image-path", required_argument
, NULL
, ARG_IMAGE_PATH
},
2297 { "fs-type", required_argument
, NULL
, ARG_FS_TYPE
},
2298 { "luks-discard", required_argument
, NULL
, ARG_LUKS_DISCARD
},
2299 { "luks-offline-discard", required_argument
, NULL
, ARG_LUKS_OFFLINE_DISCARD
},
2300 { "luks-cipher", required_argument
, NULL
, ARG_LUKS_CIPHER
},
2301 { "luks-cipher-mode", required_argument
, NULL
, ARG_LUKS_CIPHER_MODE
},
2302 { "luks-volume-key-size", required_argument
, NULL
, ARG_LUKS_VOLUME_KEY_SIZE
},
2303 { "luks-pbkdf-type", required_argument
, NULL
, ARG_LUKS_PBKDF_TYPE
},
2304 { "luks-pbkdf-hash-algorithm", required_argument
, NULL
, ARG_LUKS_PBKDF_HASH_ALGORITHM
},
2305 { "luks-pbkdf-time-cost", required_argument
, NULL
, ARG_LUKS_PBKDF_TIME_COST
},
2306 { "luks-pbkdf-memory-cost", required_argument
, NULL
, ARG_LUKS_PBKDF_MEMORY_COST
},
2307 { "luks-pbkdf-parallel-threads", required_argument
, NULL
, ARG_LUKS_PBKDF_PARALLEL_THREADS
},
2308 { "nosuid", required_argument
, NULL
, ARG_NOSUID
},
2309 { "nodev", required_argument
, NULL
, ARG_NODEV
},
2310 { "noexec", required_argument
, NULL
, ARG_NOEXEC
},
2311 { "cifs-user-name", required_argument
, NULL
, ARG_CIFS_USER_NAME
},
2312 { "cifs-domain", required_argument
, NULL
, ARG_CIFS_DOMAIN
},
2313 { "cifs-service", required_argument
, NULL
, ARG_CIFS_SERVICE
},
2314 { "rate-limit-interval", required_argument
, NULL
, ARG_RATE_LIMIT_INTERVAL
},
2315 { "rate-limit-burst", required_argument
, NULL
, ARG_RATE_LIMIT_BURST
},
2316 { "stop-delay", required_argument
, NULL
, ARG_STOP_DELAY
},
2317 { "kill-processes", required_argument
, NULL
, ARG_KILL_PROCESSES
},
2318 { "enforce-password-policy", required_argument
, NULL
, ARG_ENFORCE_PASSWORD_POLICY
},
2319 { "password-change-now", required_argument
, NULL
, ARG_PASSWORD_CHANGE_NOW
},
2320 { "password-change-min", required_argument
, NULL
, ARG_PASSWORD_CHANGE_MIN
},
2321 { "password-change-max", required_argument
, NULL
, ARG_PASSWORD_CHANGE_MAX
},
2322 { "password-change-warn", required_argument
, NULL
, ARG_PASSWORD_CHANGE_WARN
},
2323 { "password-change-inactive", required_argument
, NULL
, ARG_PASSWORD_CHANGE_INACTIVE
},
2324 { "auto-login", required_argument
, NULL
, ARG_AUTO_LOGIN
},
2325 { "json", required_argument
, NULL
, ARG_JSON
},
2326 { "export-format", required_argument
, NULL
, ARG_EXPORT_FORMAT
},
2327 { "pkcs11-token-uri", required_argument
, NULL
, ARG_PKCS11_TOKEN_URI
},
2328 { "fido2-device", required_argument
, NULL
, ARG_FIDO2_DEVICE
},
2329 { "fido2-with-client-pin", required_argument
, NULL
, ARG_FIDO2_WITH_PIN
},
2330 { "fido2-with-user-presence", required_argument
, NULL
, ARG_FIDO2_WITH_UP
},
2331 { "fido2-with-user-verification",required_argument
, NULL
, ARG_FIDO2_WITH_UV
},
2332 { "recovery-key", required_argument
, NULL
, ARG_RECOVERY_KEY
},
2333 { "and-resize", required_argument
, NULL
, ARG_AND_RESIZE
},
2334 { "and-change-password", required_argument
, NULL
, ARG_AND_CHANGE_PASSWORD
},
2346 c
= getopt_long(argc
, argv
, "hH:M:I:c:d:u:k:s:e:G:jPE", options
, NULL
);
2353 return help(0, NULL
, NULL
);
2359 arg_pager_flags
|= PAGER_DISABLE
;
2366 case ARG_NO_ASK_PASSWORD
:
2367 arg_ask_password
= false;
2371 arg_transport
= BUS_TRANSPORT_REMOTE
;
2376 arg_transport
= BUS_TRANSPORT_MACHINE
;
2381 arg_identity
= optarg
;
2385 if (isempty(optarg
)) {
2386 r
= drop_from_identity("realName");
2393 if (!valid_gecos(optarg
))
2394 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Real name '%s' not a valid GECOS field.", optarg
);
2396 r
= json_variant_set_field_string(&arg_identity_extra
, "realName", optarg
);
2398 return log_error_errno(r
, "Failed to set realName field: %m");
2403 _cleanup_free_
char *hd
= NULL
;
2405 if (isempty(optarg
)) {
2406 r
= drop_from_identity("homeDirectory");
2413 r
= parse_path_argument(optarg
, false, &hd
);
2417 if (!valid_home(hd
))
2418 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Home directory '%s' not valid.", hd
);
2420 r
= json_variant_set_field_string(&arg_identity_extra
, "homeDirectory", hd
);
2422 return log_error_errno(r
, "Failed to set homeDirectory field: %m");
2428 if (isempty(optarg
)) {
2429 r
= drop_from_identity("realm");
2436 r
= dns_name_is_valid(optarg
);
2438 return log_error_errno(r
, "Failed to determine whether realm '%s' is a valid DNS domain: %m", optarg
);
2440 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Realm '%s' is not a valid DNS domain: %m", optarg
);
2442 r
= json_variant_set_field_string(&arg_identity_extra
, "realm", optarg
);
2444 return log_error_errno(r
, "Failed to set realm field: %m");
2447 case ARG_EMAIL_ADDRESS
:
2450 case ARG_CIFS_USER_NAME
:
2451 case ARG_CIFS_DOMAIN
:
2452 case ARG_CIFS_SERVICE
: {
2455 c
== ARG_EMAIL_ADDRESS
? "emailAddress" :
2456 c
== ARG_LOCATION
? "location" :
2457 c
== ARG_ICON_NAME
? "iconName" :
2458 c
== ARG_CIFS_USER_NAME
? "cifsUserName" :
2459 c
== ARG_CIFS_DOMAIN
? "cifsDomain" :
2460 c
== ARG_CIFS_SERVICE
? "cifsService" :
2465 if (isempty(optarg
)) {
2466 r
= drop_from_identity(field
);
2473 r
= json_variant_set_field_string(&arg_identity_extra
, field
, optarg
);
2475 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2480 case ARG_PASSWORD_HINT
:
2481 if (isempty(optarg
)) {
2482 r
= drop_from_identity("passwordHint");
2489 r
= json_variant_set_field_string(&arg_identity_extra_privileged
, "passwordHint", optarg
);
2491 return log_error_errno(r
, "Failed to set passwordHint field: %m");
2493 string_erase(optarg
);
2499 if (isempty(optarg
)) {
2500 r
= drop_from_identity("niceLevel");
2506 r
= parse_nice(optarg
, &nc
);
2508 return log_error_errno(r
, "Failed to parse nice level: %s", optarg
);
2510 r
= json_variant_set_field_integer(&arg_identity_extra
, "niceLevel", nc
);
2512 return log_error_errno(r
, "Failed to set niceLevel field: %m");
2518 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
, *jcur
= NULL
, *jmax
= NULL
;
2519 _cleanup_free_
char *field
= NULL
, *t
= NULL
;
2524 if (isempty(optarg
)) {
2525 /* Remove all resource limits */
2527 r
= drop_from_identity("resourceLimits");
2531 arg_identity_filter_rlimits
= strv_free(arg_identity_filter_rlimits
);
2532 arg_identity_extra_rlimits
= json_variant_unref(arg_identity_extra_rlimits
);
2536 eq
= strchr(optarg
, '=');
2538 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Can't parse resource limit assignment: %s", optarg
);
2540 field
= strndup(optarg
, eq
- optarg
);
2544 l
= rlimit_from_string_harder(field
);
2546 return log_error_errno(l
, "Unknown resource limit type: %s", field
);
2548 if (isempty(eq
+ 1)) {
2549 /* Remove only the specific rlimit */
2551 r
= strv_extend(&arg_identity_filter_rlimits
, rlimit_to_string(l
));
2555 r
= json_variant_filter(&arg_identity_extra_rlimits
, STRV_MAKE(field
));
2557 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
2562 r
= rlimit_parse(l
, eq
+ 1, &rl
);
2564 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to parse resource limit value: %s", eq
+ 1);
2566 r
= rl
.rlim_cur
== RLIM_INFINITY
? json_variant_new_null(&jcur
) : json_variant_new_unsigned(&jcur
, rl
.rlim_cur
);
2568 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to allocate current integer: %m");
2570 r
= rl
.rlim_max
== RLIM_INFINITY
? json_variant_new_null(&jmax
) : json_variant_new_unsigned(&jmax
, rl
.rlim_max
);
2572 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to allocate maximum integer: %m");
2576 JSON_BUILD_PAIR("cur", JSON_BUILD_VARIANT(jcur
)),
2577 JSON_BUILD_PAIR("max", JSON_BUILD_VARIANT(jmax
))));
2579 return log_error_errno(r
, "Failed to build resource limit: %m");
2581 t
= strjoin("RLIMIT_", rlimit_to_string(l
));
2585 r
= json_variant_set_field(&arg_identity_extra_rlimits
, t
, v
);
2587 return log_error_errno(r
, "Failed to set %s field: %m", rlimit_to_string(l
));
2595 if (isempty(optarg
)) {
2596 r
= drop_from_identity("uid");
2603 r
= parse_uid(optarg
, &uid
);
2605 return log_error_errno(r
, "Failed to parse UID '%s'.", optarg
);
2607 if (uid_is_system(uid
))
2608 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "UID " UID_FMT
" is in system range, refusing.", uid
);
2609 if (uid_is_dynamic(uid
))
2610 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "UID " UID_FMT
" is in dynamic range, refusing.", uid
);
2611 if (uid
== UID_NOBODY
)
2612 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "UID " UID_FMT
" is nobody UID, refusing.", uid
);
2614 r
= json_variant_set_field_unsigned(&arg_identity_extra
, "uid", uid
);
2616 return log_error_errno(r
, "Failed to set realm field: %m");
2622 case ARG_IMAGE_PATH
: {
2623 const char *field
= c
== 'k' ? "skeletonDirectory" : "imagePath";
2624 _cleanup_free_
char *v
= NULL
;
2626 if (isempty(optarg
)) {
2627 r
= drop_from_identity(field
);
2634 r
= parse_path_argument(optarg
, false, &v
);
2638 r
= json_variant_set_field_string(&arg_identity_extra_this_machine
, field
, v
);
2640 return log_error_errno(r
, "Failed to set %s field: %m", v
);
2646 if (isempty(optarg
)) {
2647 r
= drop_from_identity("shell");
2654 if (!valid_shell(optarg
))
2655 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Shell '%s' not valid.", optarg
);
2657 r
= json_variant_set_field_string(&arg_identity_extra
, "shell", optarg
);
2659 return log_error_errno(r
, "Failed to set shell field: %m");
2664 _cleanup_free_
char **l
= NULL
;
2665 _cleanup_(json_variant_unrefp
) JsonVariant
*ne
= NULL
;
2668 if (isempty(optarg
)) {
2669 r
= drop_from_identity("environment");
2676 if (!env_assignment_is_valid(optarg
))
2677 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2678 "Environment assignment '%s' not valid.", optarg
);
2680 e
= json_variant_by_key(arg_identity_extra
, "environment");
2682 r
= json_variant_strv(e
, &l
);
2684 return log_error_errno(r
, "Failed to parse JSON environment field: %m");
2687 r
= strv_env_replace_strdup(&l
, optarg
);
2689 return log_error_errno(r
, "Failed to replace JSON environment field: %m");
2693 r
= json_variant_new_array_strv(&ne
, l
);
2695 return log_error_errno(r
, "Failed to allocate environment list JSON: %m");
2697 r
= json_variant_set_field(&arg_identity_extra
, "environment", ne
);
2699 return log_error_errno(r
, "Failed to set environment list: %m");
2706 if (isempty(optarg
)) {
2707 r
= drop_from_identity("timeZone");
2714 if (!timezone_is_valid(optarg
, LOG_DEBUG
))
2715 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Timezone '%s' is not valid.", optarg
);
2717 r
= json_variant_set_field_string(&arg_identity_extra
, "timeZone", optarg
);
2719 return log_error_errno(r
, "Failed to set timezone field: %m");
2724 if (isempty(optarg
)) {
2725 r
= drop_from_identity("language");
2732 if (!locale_is_valid(optarg
))
2733 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Locale '%s' is not valid.", optarg
);
2735 if (locale_is_installed(optarg
) <= 0)
2736 log_warning("Locale '%s' is not installed, accepting anyway.", optarg
);
2738 r
= json_variant_set_field_string(&arg_identity_extra
, "preferredLanguage", optarg
);
2740 return log_error_errno(r
, "Failed to set preferredLanguage field: %m");
2748 case ARG_KILL_PROCESSES
:
2749 case ARG_ENFORCE_PASSWORD_POLICY
:
2750 case ARG_AUTO_LOGIN
:
2751 case ARG_PASSWORD_CHANGE_NOW
: {
2753 c
== ARG_LOCKED
? "locked" :
2754 c
== ARG_NOSUID
? "mountNoSuid" :
2755 c
== ARG_NODEV
? "mountNoDevices" :
2756 c
== ARG_NOEXEC
? "mountNoExecute" :
2757 c
== ARG_KILL_PROCESSES
? "killProcesses" :
2758 c
== ARG_ENFORCE_PASSWORD_POLICY
? "enforcePasswordPolicy" :
2759 c
== ARG_AUTO_LOGIN
? "autoLogin" :
2760 c
== ARG_PASSWORD_CHANGE_NOW
? "passwordChangeNow" :
2765 if (isempty(optarg
)) {
2766 r
= drop_from_identity(field
);
2773 r
= parse_boolean(optarg
);
2775 return log_error_errno(r
, "Failed to parse %s boolean: %m", field
);
2777 r
= json_variant_set_field_boolean(&arg_identity_extra
, field
, r
> 0);
2779 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2785 r
= json_variant_set_field_boolean(&arg_identity_extra
, "enforcePasswordPolicy", false);
2787 return log_error_errno(r
, "Failed to set enforcePasswordPolicy field: %m");
2792 if (isempty(optarg
)) {
2793 r
= drop_from_identity("diskSize");
2797 r
= drop_from_identity("diskSizeRelative");
2801 arg_disk_size
= arg_disk_size_relative
= UINT64_MAX
;
2805 r
= parse_permyriad(optarg
);
2807 r
= parse_size(optarg
, 1024, &arg_disk_size
);
2809 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Disk size '%s' not valid.", optarg
);
2811 r
= drop_from_identity("diskSizeRelative");
2815 r
= json_variant_set_field_unsigned(&arg_identity_extra_this_machine
, "diskSize", arg_disk_size
);
2817 return log_error_errno(r
, "Failed to set diskSize field: %m");
2819 arg_disk_size_relative
= UINT64_MAX
;
2821 /* Normalize to UINT32_MAX == 100% */
2822 arg_disk_size_relative
= UINT32_SCALE_FROM_PERMYRIAD(r
);
2824 r
= drop_from_identity("diskSize");
2828 r
= json_variant_set_field_unsigned(&arg_identity_extra_this_machine
, "diskSizeRelative", arg_disk_size_relative
);
2830 return log_error_errno(r
, "Failed to set diskSizeRelative field: %m");
2832 arg_disk_size
= UINT64_MAX
;
2837 case ARG_ACCESS_MODE
: {
2840 if (isempty(optarg
)) {
2841 r
= drop_from_identity("accessMode");
2848 r
= parse_mode(optarg
, &mode
);
2850 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Access mode '%s' not valid.", optarg
);
2852 r
= json_variant_set_field_unsigned(&arg_identity_extra
, "accessMode", mode
);
2854 return log_error_errno(r
, "Failed to set access mode field: %m");
2859 case ARG_LUKS_DISCARD
:
2860 if (isempty(optarg
)) {
2861 r
= drop_from_identity("luksDiscard");
2868 r
= parse_boolean(optarg
);
2870 return log_error_errno(r
, "Failed to parse --luks-discard= parameter: %s", optarg
);
2872 r
= json_variant_set_field_boolean(&arg_identity_extra
, "luksDiscard", r
);
2874 return log_error_errno(r
, "Failed to set discard field: %m");
2878 case ARG_LUKS_OFFLINE_DISCARD
:
2879 if (isempty(optarg
)) {
2880 r
= drop_from_identity("luksOfflineDiscard");
2887 r
= parse_boolean(optarg
);
2889 return log_error_errno(r
, "Failed to parse --luks-offline-discard= parameter: %s", optarg
);
2891 r
= json_variant_set_field_boolean(&arg_identity_extra
, "luksOfflineDiscard", r
);
2893 return log_error_errno(r
, "Failed to set offline discard field: %m");
2897 case ARG_LUKS_VOLUME_KEY_SIZE
:
2898 case ARG_LUKS_PBKDF_PARALLEL_THREADS
:
2899 case ARG_RATE_LIMIT_BURST
: {
2901 c
== ARG_LUKS_VOLUME_KEY_SIZE
? "luksVolumeKeySize" :
2902 c
== ARG_LUKS_PBKDF_PARALLEL_THREADS
? "luksPbkdfParallelThreads" :
2903 c
== ARG_RATE_LIMIT_BURST
? "rateLimitBurst" : NULL
;
2908 if (isempty(optarg
)) {
2909 r
= drop_from_identity(field
);
2914 r
= safe_atou(optarg
, &n
);
2916 return log_error_errno(r
, "Failed to parse %s parameter: %s", field
, optarg
);
2918 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, n
);
2920 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2928 if (isempty(optarg
)) {
2929 r
= drop_from_identity("umask");
2936 r
= parse_mode(optarg
, &m
);
2938 return log_error_errno(r
, "Failed to parse umask: %m");
2940 r
= json_variant_set_field_integer(&arg_identity_extra
, "umask", m
);
2942 return log_error_errno(r
, "Failed to set umask field: %m");
2947 case ARG_SSH_AUTHORIZED_KEYS
: {
2948 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
2949 _cleanup_(strv_freep
) char **l
= NULL
, **add
= NULL
;
2951 if (isempty(optarg
)) {
2952 r
= drop_from_identity("sshAuthorizedKeys");
2959 if (optarg
[0] == '@') {
2960 _cleanup_fclose_
FILE *f
= NULL
;
2962 /* If prefixed with '@' read from a file */
2964 f
= fopen(optarg
+1, "re");
2966 return log_error_errno(errno
, "Failed to open '%s': %m", optarg
+1);
2969 _cleanup_free_
char *line
= NULL
;
2971 r
= read_line(f
, LONG_LINE_MAX
, &line
);
2973 return log_error_errno(r
, "Failed to read from '%s': %m", optarg
+1);
2983 r
= strv_consume(&add
, TAKE_PTR(line
));
2988 /* Otherwise, assume it's a literal key. Let's do some superficial checks
2989 * before accept it though. */
2991 if (string_has_cc(optarg
, NULL
))
2992 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Authorized key contains control characters, refusing.");
2993 if (optarg
[0] == '#')
2994 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Specified key is a comment?");
2996 add
= strv_new(optarg
);
3001 v
= json_variant_ref(json_variant_by_key(arg_identity_extra_privileged
, "sshAuthorizedKeys"));
3003 r
= json_variant_strv(v
, &l
);
3005 return log_error_errno(r
, "Failed to parse SSH authorized keys list: %m");
3008 r
= strv_extend_strv(&l
, add
, true);
3012 v
= json_variant_unref(v
);
3014 r
= json_variant_new_array_strv(&v
, l
);
3018 r
= json_variant_set_field(&arg_identity_extra_privileged
, "sshAuthorizedKeys", v
);
3020 return log_error_errno(r
, "Failed to set authorized keys: %m");
3025 case ARG_NOT_BEFORE
:
3031 field
= c
== ARG_NOT_BEFORE
? "notBeforeUSec" :
3032 IN_SET(c
, ARG_NOT_AFTER
, 'e') ? "notAfterUSec" : NULL
;
3036 if (isempty(optarg
)) {
3037 r
= drop_from_identity(field
);
3044 /* Note the minor discrepancy regarding -e parsing here: we support that for compat
3045 * reasons, and in the original useradd(8) implementation it accepts dates in the
3046 * format YYYY-MM-DD. Coincidentally, we accept dates formatted like that too, but
3047 * with greater precision. */
3048 r
= parse_timestamp(optarg
, &n
);
3050 return log_error_errno(r
, "Failed to parse %s parameter: %m", field
);
3052 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, n
);
3054 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3058 case ARG_PASSWORD_CHANGE_MIN
:
3059 case ARG_PASSWORD_CHANGE_MAX
:
3060 case ARG_PASSWORD_CHANGE_WARN
:
3061 case ARG_PASSWORD_CHANGE_INACTIVE
: {
3065 field
= c
== ARG_PASSWORD_CHANGE_MIN
? "passwordChangeMinUSec" :
3066 c
== ARG_PASSWORD_CHANGE_MAX
? "passwordChangeMaxUSec" :
3067 c
== ARG_PASSWORD_CHANGE_WARN
? "passwordChangeWarnUSec" :
3068 c
== ARG_PASSWORD_CHANGE_INACTIVE
? "passwordChangeInactiveUSec" :
3073 if (isempty(optarg
)) {
3074 r
= drop_from_identity(field
);
3081 r
= parse_sec(optarg
, &n
);
3083 return log_error_errno(r
, "Failed to parse %s parameter: %m", field
);
3085 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, n
);
3087 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3093 case ARG_LUKS_CIPHER
:
3094 case ARG_LUKS_CIPHER_MODE
:
3095 case ARG_LUKS_PBKDF_TYPE
:
3096 case ARG_LUKS_PBKDF_HASH_ALGORITHM
: {
3099 c
== ARG_STORAGE
? "storage" :
3100 c
== ARG_FS_TYPE
? "fileSystemType" :
3101 c
== ARG_LUKS_CIPHER
? "luksCipher" :
3102 c
== ARG_LUKS_CIPHER_MODE
? "luksCipherMode" :
3103 c
== ARG_LUKS_PBKDF_TYPE
? "luksPbkdfType" :
3104 c
== ARG_LUKS_PBKDF_HASH_ALGORITHM
? "luksPbkdfHashAlgorithm" : NULL
;
3108 if (isempty(optarg
)) {
3109 r
= drop_from_identity(field
);
3116 if (!string_is_safe(optarg
))
3117 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Parameter for %s field not valid: %s", field
, optarg
);
3119 r
= json_variant_set_field_string(
3120 IN_SET(c
, ARG_STORAGE
, ARG_FS_TYPE
) ?
3121 &arg_identity_extra_this_machine
:
3122 &arg_identity_extra
, field
, optarg
);
3124 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3129 case ARG_LUKS_PBKDF_TIME_COST
:
3130 case ARG_RATE_LIMIT_INTERVAL
:
3131 case ARG_STOP_DELAY
: {
3133 c
== ARG_LUKS_PBKDF_TIME_COST
? "luksPbkdfTimeCostUSec" :
3134 c
== ARG_RATE_LIMIT_INTERVAL
? "rateLimitIntervalUSec" :
3135 c
== ARG_STOP_DELAY
? "stopDelayUSec" :
3141 if (isempty(optarg
)) {
3142 r
= drop_from_identity(field
);
3149 r
= parse_sec(optarg
, &t
);
3151 return log_error_errno(r
, "Failed to parse %s field: %s", field
, optarg
);
3153 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, t
);
3155 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3161 const char *p
= optarg
;
3164 r
= drop_from_identity("memberOf");
3172 _cleanup_(json_variant_unrefp
) JsonVariant
*mo
= NULL
;
3173 _cleanup_strv_free_
char **list
= NULL
;
3174 _cleanup_free_
char *word
= NULL
;
3176 r
= extract_first_word(&p
, &word
, ",", 0);
3178 return log_error_errno(r
, "Failed to parse group list: %m");
3182 if (!valid_user_group_name(word
, 0))
3183 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Invalid group name %s.", word
);
3185 mo
= json_variant_ref(json_variant_by_key(arg_identity_extra
, "memberOf"));
3187 r
= json_variant_strv(mo
, &list
);
3189 return log_error_errno(r
, "Failed to parse group list: %m");
3191 r
= strv_extend(&list
, word
);
3198 mo
= json_variant_unref(mo
);
3199 r
= json_variant_new_array_strv(&mo
, list
);
3201 return log_error_errno(r
, "Failed to create group list JSON: %m");
3203 r
= json_variant_set_field(&arg_identity_extra
, "memberOf", mo
);
3205 return log_error_errno(r
, "Failed to update group list: %m");
3211 case ARG_TASKS_MAX
: {
3214 if (isempty(optarg
)) {
3215 r
= drop_from_identity("tasksMax");
3221 r
= safe_atou64(optarg
, &u
);
3223 return log_error_errno(r
, "Failed to parse --tasks-max= parameter: %s", optarg
);
3225 r
= json_variant_set_field_unsigned(&arg_identity_extra
, "tasksMax", u
);
3227 return log_error_errno(r
, "Failed to set tasksMax field: %m");
3232 case ARG_MEMORY_MAX
:
3233 case ARG_MEMORY_HIGH
:
3234 case ARG_LUKS_PBKDF_MEMORY_COST
: {
3236 c
== ARG_MEMORY_MAX
? "memoryMax" :
3237 c
== ARG_MEMORY_HIGH
? "memoryHigh" :
3238 c
== ARG_LUKS_PBKDF_MEMORY_COST
? "luksPbkdfMemoryCost" : NULL
;
3244 if (isempty(optarg
)) {
3245 r
= drop_from_identity(field
);
3251 r
= parse_size(optarg
, 1024, &u
);
3253 return log_error_errno(r
, "Failed to parse %s parameter: %s", field
, optarg
);
3255 r
= json_variant_set_field_unsigned(&arg_identity_extra_this_machine
, field
, u
);
3257 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3262 case ARG_CPU_WEIGHT
:
3263 case ARG_IO_WEIGHT
: {
3264 const char *field
= c
== ARG_CPU_WEIGHT
? "cpuWeight" :
3265 c
== ARG_IO_WEIGHT
? "ioWeight" : NULL
;
3270 if (isempty(optarg
)) {
3271 r
= drop_from_identity(field
);
3277 r
= safe_atou64(optarg
, &u
);
3279 return log_error_errno(r
, "Failed to parse --cpu-weight=/--io-weight= parameter: %s", optarg
);
3281 if (!CGROUP_WEIGHT_IS_OK(u
))
3282 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Weight %" PRIu64
" is out of valid weight range.", u
);
3284 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, u
);
3286 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3291 case ARG_PKCS11_TOKEN_URI
: {
3294 if (streq(optarg
, "list"))
3295 return pkcs11_list_tokens();
3297 /* If --pkcs11-token-uri= is specified we always drop everything old */
3298 FOREACH_STRING(p
, "pkcs11TokenUri", "pkcs11EncryptedKey") {
3299 r
= drop_from_identity(p
);
3304 if (isempty(optarg
)) {
3305 arg_pkcs11_token_uri
= strv_free(arg_pkcs11_token_uri
);
3309 if (streq(optarg
, "auto")) {
3310 _cleanup_free_
char *found
= NULL
;
3312 r
= pkcs11_find_token_auto(&found
);
3315 r
= strv_consume(&arg_pkcs11_token_uri
, TAKE_PTR(found
));
3317 if (!pkcs11_uri_valid(optarg
))
3318 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Not a valid PKCS#11 URI: %s", optarg
);
3320 r
= strv_extend(&arg_pkcs11_token_uri
, optarg
);
3325 strv_uniq(arg_pkcs11_token_uri
);
3329 case ARG_FIDO2_DEVICE
: {
3332 if (streq(optarg
, "list"))
3333 return fido2_list_devices();
3335 FOREACH_STRING(p
, "fido2HmacCredential", "fido2HmacSalt") {
3336 r
= drop_from_identity(p
);
3341 if (isempty(optarg
)) {
3342 arg_fido2_device
= strv_free(arg_fido2_device
);
3346 if (streq(optarg
, "auto")) {
3347 _cleanup_free_
char *found
= NULL
;
3349 r
= fido2_find_device_auto(&found
);
3353 r
= strv_consume(&arg_fido2_device
, TAKE_PTR(found
));
3355 r
= strv_extend(&arg_fido2_device
, optarg
);
3359 strv_uniq(arg_fido2_device
);
3363 case ARG_FIDO2_WITH_PIN
: {
3366 r
= parse_boolean_argument("--fido2-with-client-pin=", optarg
, &lock_with_pin
);
3370 SET_FLAG(arg_fido2_lock_with
, FIDO2ENROLL_PIN
, lock_with_pin
);
3374 case ARG_FIDO2_WITH_UP
: {
3377 r
= parse_boolean_argument("--fido2-with-user-presence=", optarg
, &lock_with_up
);
3381 SET_FLAG(arg_fido2_lock_with
, FIDO2ENROLL_UP
, lock_with_up
);
3385 case ARG_FIDO2_WITH_UV
: {
3388 r
= parse_boolean_argument("--fido2-with-user-verification=", optarg
, &lock_with_uv
);
3392 SET_FLAG(arg_fido2_lock_with
, FIDO2ENROLL_UV
, lock_with_uv
);
3396 case ARG_RECOVERY_KEY
: {
3399 r
= parse_boolean(optarg
);
3401 return log_error_errno(r
, "Failed to parse --recovery-key= argument: %s", optarg
);
3403 arg_recovery_key
= r
;
3405 FOREACH_STRING(p
, "recoveryKey", "recoveryKeyType") {
3406 r
= drop_from_identity(p
);
3415 arg_json_format_flags
= JSON_FORMAT_PRETTY_AUTO
|JSON_FORMAT_COLOR_AUTO
;
3419 r
= parse_json_argument(optarg
, &arg_json_format_flags
);
3426 if (arg_export_format
== EXPORT_FORMAT_FULL
)
3427 arg_export_format
= EXPORT_FORMAT_STRIPPED
;
3428 else if (arg_export_format
== EXPORT_FORMAT_STRIPPED
)
3429 arg_export_format
= EXPORT_FORMAT_MINIMAL
;
3431 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Specifying -E more than twice is not supported.");
3433 arg_json_format_flags
&= ~JSON_FORMAT_OFF
;
3434 if (arg_json_format_flags
== 0)
3435 arg_json_format_flags
= JSON_FORMAT_PRETTY_AUTO
|JSON_FORMAT_COLOR_AUTO
;
3438 case ARG_EXPORT_FORMAT
:
3439 if (streq(optarg
, "full"))
3440 arg_export_format
= EXPORT_FORMAT_FULL
;
3441 else if (streq(optarg
, "stripped"))
3442 arg_export_format
= EXPORT_FORMAT_STRIPPED
;
3443 else if (streq(optarg
, "minimal"))
3444 arg_export_format
= EXPORT_FORMAT_MINIMAL
;
3445 else if (streq(optarg
, "help")) {
3454 case ARG_AND_RESIZE
:
3455 arg_and_resize
= true;
3458 case ARG_AND_CHANGE_PASSWORD
:
3459 arg_and_change_password
= true;
3466 assert_not_reached();
3470 if (!strv_isempty(arg_pkcs11_token_uri
) || !strv_isempty(arg_fido2_device
))
3471 arg_and_change_password
= true;
3473 if (arg_disk_size
!= UINT64_MAX
|| arg_disk_size_relative
!= UINT64_MAX
)
3474 arg_and_resize
= true;
3479 static int redirect_bus_mgr(void) {
3482 /* Talk to a different service if that's requested. (The same env var is also understood by homed, so
3483 * that it is relatively easily possible to invoke a second instance of homed for debug purposes and
3484 * have homectl talk to it, without colliding with the host version. This is handy when operating
3485 * from a homed-managed account.) */
3487 suffix
= getenv("SYSTEMD_HOME_DEBUG_SUFFIX");
3489 static BusLocator locator
= {
3490 .path
= "/org/freedesktop/home1",
3491 .interface
= "org.freedesktop.home1.Manager",
3494 /* Yes, we leak this memory, but there's little point to collect this, given that we only do
3495 * this in a debug environment, do it only once, and the string shall live for out entire
3496 * process runtime. */
3498 locator
.destination
= strjoin("org.freedesktop.home1.", suffix
);
3499 if (!locator
.destination
)
3504 bus_mgr
= bus_home_mgr
;
3509 static int run(int argc
, char *argv
[]) {
3510 static const Verb verbs
[] = {
3511 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
3512 { "list", VERB_ANY
, 1, VERB_DEFAULT
, list_homes
},
3513 { "activate", 2, VERB_ANY
, 0, activate_home
},
3514 { "deactivate", 2, VERB_ANY
, 0, deactivate_home
},
3515 { "inspect", VERB_ANY
, VERB_ANY
, 0, inspect_home
},
3516 { "authenticate", VERB_ANY
, VERB_ANY
, 0, authenticate_home
},
3517 { "create", VERB_ANY
, 2, 0, create_home
},
3518 { "remove", 2, VERB_ANY
, 0, remove_home
},
3519 { "update", VERB_ANY
, 2, 0, update_home
},
3520 { "passwd", VERB_ANY
, 2, 0, passwd_home
},
3521 { "resize", 2, 3, 0, resize_home
},
3522 { "lock", 2, VERB_ANY
, 0, lock_home
},
3523 { "unlock", 2, VERB_ANY
, 0, unlock_home
},
3524 { "with", 2, VERB_ANY
, 0, with_home
},
3525 { "lock-all", VERB_ANY
, 1, 0, lock_all_homes
},
3526 { "deactivate-all", VERB_ANY
, 1, 0, deactivate_all_homes
},
3534 r
= redirect_bus_mgr();
3538 r
= parse_argv(argc
, argv
);
3542 return dispatch_verb(argc
, argv
, verbs
, NULL
);
3545 DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run
);