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 "locale-util.h"
22 #include "main-func.h"
23 #include "memory-util.h"
25 #include "parse-util.h"
26 #include "path-util.h"
27 #include "pkcs11-util.h"
28 #include "pretty-print.h"
29 #include "process-util.h"
30 #include "pwquality-util.h"
31 #include "rlimit-util.h"
32 #include "spawn-polkit-agent.h"
33 #include "terminal-util.h"
34 #include "user-record-pwquality.h"
35 #include "user-record-show.h"
36 #include "user-record-util.h"
37 #include "user-record.h"
38 #include "user-util.h"
41 static PagerFlags arg_pager_flags
= 0;
42 static bool arg_legend
= true;
43 static bool arg_ask_password
= true;
44 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
45 static const char *arg_host
= NULL
;
46 static const char *arg_identity
= NULL
;
47 static JsonVariant
*arg_identity_extra
= NULL
;
48 static JsonVariant
*arg_identity_extra_privileged
= NULL
;
49 static JsonVariant
*arg_identity_extra_this_machine
= NULL
;
50 static JsonVariant
*arg_identity_extra_rlimits
= NULL
;
51 static char **arg_identity_filter
= NULL
; /* this one is also applied to 'privileged' and 'thisMachine' subobjects */
52 static char **arg_identity_filter_rlimits
= NULL
;
53 static uint64_t arg_disk_size
= UINT64_MAX
;
54 static uint64_t arg_disk_size_relative
= UINT64_MAX
;
55 static char **arg_pkcs11_token_uri
= NULL
;
56 static char **arg_fido2_device
= NULL
;
57 static bool arg_recovery_key
= false;
58 static bool arg_json
= false;
59 static JsonFormatFlags arg_json_format_flags
= 0;
60 static bool arg_and_resize
= false;
61 static bool arg_and_change_password
= false;
63 EXPORT_FORMAT_FULL
, /* export the full record */
64 EXPORT_FORMAT_STRIPPED
, /* strip "state" + "binding", but leave signature in place */
65 EXPORT_FORMAT_MINIMAL
, /* also strip signature */
66 } arg_export_format
= EXPORT_FORMAT_FULL
;
68 STATIC_DESTRUCTOR_REGISTER(arg_identity_extra
, json_variant_unrefp
);
69 STATIC_DESTRUCTOR_REGISTER(arg_identity_extra_this_machine
, json_variant_unrefp
);
70 STATIC_DESTRUCTOR_REGISTER(arg_identity_extra_privileged
, json_variant_unrefp
);
71 STATIC_DESTRUCTOR_REGISTER(arg_identity_extra_rlimits
, json_variant_unrefp
);
72 STATIC_DESTRUCTOR_REGISTER(arg_identity_filter
, strv_freep
);
73 STATIC_DESTRUCTOR_REGISTER(arg_identity_filter_rlimits
, strv_freep
);
74 STATIC_DESTRUCTOR_REGISTER(arg_pkcs11_token_uri
, strv_freep
);
75 STATIC_DESTRUCTOR_REGISTER(arg_fido2_device
, strv_freep
);
77 static const BusLocator
*bus_mgr
;
79 static bool identity_properties_specified(void) {
82 !json_variant_is_blank_object(arg_identity_extra
) ||
83 !json_variant_is_blank_object(arg_identity_extra_privileged
) ||
84 !json_variant_is_blank_object(arg_identity_extra_this_machine
) ||
85 !json_variant_is_blank_object(arg_identity_extra_rlimits
) ||
86 !strv_isempty(arg_identity_filter
) ||
87 !strv_isempty(arg_identity_filter_rlimits
) ||
88 !strv_isempty(arg_pkcs11_token_uri
) ||
89 !strv_isempty(arg_fido2_device
);
92 static int acquire_bus(sd_bus
**bus
) {
100 r
= bus_connect_transport(arg_transport
, arg_host
, false, bus
);
102 return bus_log_connect_error(r
);
104 (void) sd_bus_set_allow_interactive_authorization(*bus
, arg_ask_password
);
109 static int list_homes(int argc
, char *argv
[], void *userdata
) {
110 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
111 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
112 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
113 _cleanup_(table_unrefp
) Table
*table
= NULL
;
116 (void) pager_open(arg_pager_flags
);
118 r
= acquire_bus(&bus
);
122 r
= bus_call_method(bus
, bus_mgr
, "ListHomes", &error
, &reply
, NULL
);
124 return log_error_errno(r
, "Failed to list homes: %s", bus_error_message(&error
, r
));
126 table
= table_new("name", "uid", "gid", "state", "realname", "home", "shell");
130 r
= sd_bus_message_enter_container(reply
, 'a', "(susussso)");
132 return bus_log_parse_error(r
);
135 const char *name
, *state
, *realname
, *home
, *shell
, *color
;
139 r
= sd_bus_message_read(reply
, "(susussso)", &name
, &uid
, &state
, &gid
, &realname
, &home
, &shell
, NULL
);
141 return bus_log_parse_error(r
);
145 r
= table_add_many(table
,
150 return table_log_add_error(r
);
153 r
= table_add_cell(table
, &cell
, TABLE_STRING
, state
);
155 return table_log_add_error(r
);
157 color
= user_record_state_color(state
);
159 (void) table_set_color(table
, cell
, color
);
161 r
= table_add_many(table
,
162 TABLE_STRING
, strna(empty_to_null(realname
)),
164 TABLE_STRING
, strna(empty_to_null(shell
)));
166 return table_log_add_error(r
);
169 r
= sd_bus_message_exit_container(reply
);
171 return bus_log_parse_error(r
);
173 if (table_get_rows(table
) > 1 || arg_json
) {
174 r
= table_set_sort(table
, (size_t) 0, (size_t) -1);
176 return table_log_sort_error(r
);
178 table_set_header(table
, arg_legend
);
181 r
= table_print_json(table
, stdout
, arg_json_format_flags
);
183 r
= table_print(table
, NULL
);
185 return table_log_print_error(r
);
188 if (arg_legend
&& !arg_json
) {
189 if (table_get_rows(table
) > 1)
190 printf("\n%zu home areas listed.\n", table_get_rows(table
) - 1);
192 printf("No home areas.\n");
198 static int acquire_existing_password(const char *user_name
, UserRecord
*hr
, bool emphasize_current
) {
199 _cleanup_(strv_free_erasep
) char **password
= NULL
;
200 _cleanup_free_
char *question
= NULL
;
207 e
= getenv("PASSWORD");
209 /* People really shouldn't use environment variables for passing passwords. We support this
210 * only for testing purposes, and do not document the behaviour, so that people won't
211 * actually use this outside of testing. */
213 r
= user_record_set_password(hr
, STRV_MAKE(e
), true);
215 return log_error_errno(r
, "Failed to store password: %m");
218 assert_se(unsetenv("PASSWORD") == 0);
223 if (asprintf(&question
, emphasize_current
?
224 "Please enter current password for user %s:" :
225 "Please enter password for user %s:",
229 r
= ask_password_auto(question
, "user-home", NULL
, "home-password", USEC_INFINITY
, ASK_PASSWORD_ACCEPT_CACHED
|ASK_PASSWORD_PUSH_CACHE
, &password
);
231 return log_error_errno(r
, "Failed to acquire password: %m");
233 r
= user_record_set_password(hr
, password
, true);
235 return log_error_errno(r
, "Failed to store password: %m");
240 static int acquire_token_pin(const char *user_name
, UserRecord
*hr
) {
241 _cleanup_(strv_free_erasep
) char **pin
= NULL
;
242 _cleanup_free_
char *question
= NULL
;
251 r
= user_record_set_token_pin(hr
, STRV_MAKE(e
), false);
253 return log_error_errno(r
, "Failed to store token PIN: %m");
256 assert_se(unsetenv("PIN") == 0);
261 if (asprintf(&question
, "Please enter security token PIN for user %s:", user_name
) < 0)
264 /* We never cache or use cached PINs, since usually there are only very few attempts allowed before the PIN is blocked */
265 r
= ask_password_auto(question
, "user-home", NULL
, "token-pin", USEC_INFINITY
, 0, &pin
);
267 return log_error_errno(r
, "Failed to acquire security token PIN: %m");
269 r
= user_record_set_token_pin(hr
, pin
, false);
271 return log_error_errno(r
, "Failed to store security token PIN: %m");
276 static int handle_generic_user_record_error(
277 const char *user_name
,
279 const sd_bus_error
*error
,
281 bool emphasize_current_password
) {
287 if (sd_bus_error_has_name(error
, BUS_ERROR_HOME_ABSENT
))
288 return log_error_errno(SYNTHETIC_ERRNO(EREMOTE
),
289 "Home of user %s is currently absent, please plug in the necessary storage device or backing file system.", user_name
);
291 else if (sd_bus_error_has_name(error
, BUS_ERROR_AUTHENTICATION_LIMIT_HIT
))
292 return log_error_errno(SYNTHETIC_ERRNO(ETOOMANYREFS
),
293 "Too frequent unsuccessful login attempts for user %s, try again later.", user_name
);
295 else if (sd_bus_error_has_name(error
, BUS_ERROR_BAD_PASSWORD
)) {
297 if (!strv_isempty(hr
->password
))
298 log_notice("Password incorrect or not sufficient, please try again.");
300 r
= acquire_existing_password(user_name
, hr
, emphasize_current_password
);
304 } else if (sd_bus_error_has_name(error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
)) {
306 if (strv_isempty(hr
->password
))
307 log_notice("Security token not inserted, please enter password.");
309 log_notice("Password incorrect or not sufficient, and configured security token not inserted, please try again.");
311 r
= acquire_existing_password(user_name
, hr
, emphasize_current_password
);
315 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_PIN_NEEDED
)) {
317 r
= acquire_token_pin(user_name
, hr
);
321 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_PROTECTED_AUTHENTICATION_PATH_NEEDED
)) {
323 log_notice("%s%sPlease authenticate physically on security token.",
324 emoji_enabled() ? special_glyph(SPECIAL_GLYPH_TOUCH
) : "",
325 emoji_enabled() ? " " : "");
327 r
= user_record_set_pkcs11_protected_authentication_path_permitted(hr
, true);
329 return log_error_errno(r
, "Failed to set PKCS#11 protected authentication path permitted flag: %m");
331 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_USER_PRESENCE_NEEDED
)) {
333 log_notice("%s%sAuthentication requires presence verification on security token.",
334 emoji_enabled() ? special_glyph(SPECIAL_GLYPH_TOUCH
) : "",
335 emoji_enabled() ? " " : "");
337 r
= user_record_set_fido2_user_presence_permitted(hr
, true);
339 return log_error_errno(r
, "Failed to set FIDO2 user presence permitted flag: %m");
341 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_PIN_LOCKED
))
342 return log_error_errno(SYNTHETIC_ERRNO(EPERM
), "Security token PIN is locked, please unlock it first. (Hint: Removal and re-insertion might suffice.)");
344 else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_BAD_PIN
)) {
346 log_notice("Security token PIN incorrect, please try again.");
348 r
= acquire_token_pin(user_name
, hr
);
352 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_BAD_PIN_FEW_TRIES_LEFT
)) {
354 log_notice("Security token PIN incorrect, please try again (only a few tries left!).");
356 r
= acquire_token_pin(user_name
, hr
);
360 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_BAD_PIN_ONE_TRY_LEFT
)) {
362 log_notice("Security token PIN incorrect, please try again (only one try left!).");
364 r
= acquire_token_pin(user_name
, hr
);
368 return log_error_errno(ret
, "Operation on home %s failed: %s", user_name
, bus_error_message(error
, ret
));
373 static int activate_home(int argc
, char *argv
[], void *userdata
) {
374 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
378 r
= acquire_bus(&bus
);
382 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
383 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
385 secret
= user_record_new();
390 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
391 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
393 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ActivateHome");
395 return bus_log_create_error(r
);
397 r
= sd_bus_message_append(m
, "s", *i
);
399 return bus_log_create_error(r
);
401 r
= bus_message_append_secret(m
, secret
);
403 return bus_log_create_error(r
);
405 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
407 r
= handle_generic_user_record_error(*i
, secret
, &error
, r
, false);
422 static int deactivate_home(int argc
, char *argv
[], void *userdata
) {
423 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
427 r
= acquire_bus(&bus
);
431 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
432 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
433 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
435 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "DeactivateHome");
437 return bus_log_create_error(r
);
439 r
= sd_bus_message_append(m
, "s", *i
);
441 return bus_log_create_error(r
);
443 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
445 log_error_errno(r
, "Failed to deactivate user home: %s", bus_error_message(&error
, r
));
454 static void dump_home_record(UserRecord
*hr
) {
459 if (hr
->incomplete
) {
461 log_warning("Warning: lacking rights to acquire privileged fields of user record of '%s', output incomplete.", hr
->user_name
);
465 _cleanup_(user_record_unrefp
) UserRecord
*stripped
= NULL
;
467 if (arg_export_format
== EXPORT_FORMAT_STRIPPED
)
468 r
= user_record_clone(hr
, USER_RECORD_EXTRACT_EMBEDDED
, &stripped
);
469 else if (arg_export_format
== EXPORT_FORMAT_MINIMAL
)
470 r
= user_record_clone(hr
, USER_RECORD_EXTRACT_SIGNABLE
, &stripped
);
474 log_warning_errno(r
, "Failed to strip user record, ignoring: %m");
478 json_variant_dump(hr
->json
, arg_json_format_flags
, stdout
, NULL
);
480 user_record_show(hr
, true);
483 static char **mangle_user_list(char **list
, char ***ret_allocated
) {
484 _cleanup_free_
char *myself
= NULL
;
487 if (!strv_isempty(list
)) {
488 *ret_allocated
= NULL
;
492 myself
= getusername_malloc();
500 l
[0] = TAKE_PTR(myself
);
507 static int inspect_home(int argc
, char *argv
[], void *userdata
) {
508 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
509 _cleanup_(strv_freep
) char **mangled_list
= NULL
;
513 (void) pager_open(arg_pager_flags
);
515 r
= acquire_bus(&bus
);
519 items
= mangle_user_list(strv_skip(argv
, 1), &mangled_list
);
523 STRV_FOREACH(i
, items
) {
524 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
525 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
526 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
527 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
532 r
= parse_uid(*i
, &uid
);
534 if (!valid_user_group_name(*i
, 0)) {
535 log_error("Invalid user name '%s'.", *i
);
542 r
= bus_call_method(bus
, bus_mgr
, "GetUserRecordByName", &error
, &reply
, "s", *i
);
544 r
= bus_call_method(bus
, bus_mgr
, "GetUserRecordByUID", &error
, &reply
, "u", (uint32_t) uid
);
547 log_error_errno(r
, "Failed to inspect home: %s", bus_error_message(&error
, r
));
554 r
= sd_bus_message_read(reply
, "sbo", &json
, &incomplete
, NULL
);
556 bus_log_parse_error(r
);
563 r
= json_parse(json
, JSON_PARSE_SENSITIVE
, &v
, NULL
, NULL
);
565 log_error_errno(r
, "Failed to parse JSON identity: %m");
572 hr
= user_record_new();
576 r
= user_record_load(hr
, v
, USER_RECORD_LOAD_REFUSE_SECRET
|USER_RECORD_LOG
);
584 hr
->incomplete
= incomplete
;
585 dump_home_record(hr
);
591 static int authenticate_home(int argc
, char *argv
[], void *userdata
) {
592 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
593 _cleanup_(strv_freep
) char **mangled_list
= NULL
;
597 items
= mangle_user_list(strv_skip(argv
, 1), &mangled_list
);
601 r
= acquire_bus(&bus
);
605 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
607 STRV_FOREACH(i
, items
) {
608 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
610 secret
= user_record_new();
615 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
616 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
618 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "AuthenticateHome");
620 return bus_log_create_error(r
);
622 r
= sd_bus_message_append(m
, "s", *i
);
624 return bus_log_create_error(r
);
626 r
= bus_message_append_secret(m
, secret
);
628 return bus_log_create_error(r
);
630 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
632 r
= handle_generic_user_record_error(*i
, secret
, &error
, r
, false);
647 static int update_last_change(JsonVariant
**v
, bool with_password
, bool override
) {
654 n
= now(CLOCK_REALTIME
);
656 c
= json_variant_by_key(*v
, "lastChangeUSec");
661 goto update_password
;
663 if (!json_variant_is_unsigned(c
))
664 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastChangeUSec field is not an unsigned integer, refusing.");
666 u
= json_variant_unsigned(c
);
668 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastChangeUSec is from the future, can't update.");
671 r
= json_variant_set_field_unsigned(v
, "lastChangeUSec", n
);
673 return log_error_errno(r
, "Failed to update lastChangeUSec: %m");
679 c
= json_variant_by_key(*v
, "lastPasswordChangeUSec");
686 if (!json_variant_is_unsigned(c
))
687 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastPasswordChangeUSec field is not an unsigned integer, refusing.");
689 u
= json_variant_unsigned(c
);
691 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastPasswordChangeUSec is from the future, can't update.");
694 r
= json_variant_set_field_unsigned(v
, "lastPasswordChangeUSec", n
);
696 return log_error_errno(r
, "Failed to update lastPasswordChangeUSec: %m");
701 static int apply_identity_changes(JsonVariant
**_v
) {
702 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
707 v
= json_variant_ref(*_v
);
709 r
= json_variant_filter(&v
, arg_identity_filter
);
711 return log_error_errno(r
, "Failed to filter identity: %m");
713 r
= json_variant_merge(&v
, arg_identity_extra
);
715 return log_error_errno(r
, "Failed to merge identities: %m");
717 if (arg_identity_extra_this_machine
|| !strv_isempty(arg_identity_filter
)) {
718 _cleanup_(json_variant_unrefp
) JsonVariant
*per_machine
= NULL
, *mmid
= NULL
;
719 char mids
[SD_ID128_STRING_MAX
];
722 r
= sd_id128_get_machine(&mid
);
724 return log_error_errno(r
, "Failed to acquire machine ID: %m");
726 r
= json_variant_new_string(&mmid
, sd_id128_to_string(mid
, mids
));
728 return log_error_errno(r
, "Failed to allocate matchMachineId object: %m");
730 per_machine
= json_variant_ref(json_variant_by_key(v
, "perMachine"));
732 _cleanup_(json_variant_unrefp
) JsonVariant
*npm
= NULL
, *add
= NULL
;
733 _cleanup_free_ JsonVariant
**array
= NULL
;
737 if (!json_variant_is_array(per_machine
))
738 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "perMachine field is not an array, refusing.");
740 array
= new(JsonVariant
*, json_variant_elements(per_machine
) + 1);
744 JSON_VARIANT_ARRAY_FOREACH(z
, per_machine
) {
747 if (!json_variant_is_object(z
))
748 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "perMachine entry is not an object, refusing.");
752 u
= json_variant_by_key(z
, "matchMachineId");
756 if (!json_variant_equal(u
, mmid
))
759 r
= json_variant_merge(&add
, z
);
761 return log_error_errno(r
, "Failed to merge perMachine entry: %m");
766 r
= json_variant_filter(&add
, arg_identity_filter
);
768 return log_error_errno(r
, "Failed to filter perMachine: %m");
770 r
= json_variant_merge(&add
, arg_identity_extra_this_machine
);
772 return log_error_errno(r
, "Failed to merge in perMachine fields: %m");
774 if (arg_identity_filter_rlimits
|| arg_identity_extra_rlimits
) {
775 _cleanup_(json_variant_unrefp
) JsonVariant
*rlv
= NULL
;
777 rlv
= json_variant_ref(json_variant_by_key(add
, "resourceLimits"));
779 r
= json_variant_filter(&rlv
, arg_identity_filter_rlimits
);
781 return log_error_errno(r
, "Failed to filter resource limits: %m");
783 r
= json_variant_merge(&rlv
, arg_identity_extra_rlimits
);
785 return log_error_errno(r
, "Failed to set resource limits: %m");
787 if (json_variant_is_blank_object(rlv
)) {
788 r
= json_variant_filter(&add
, STRV_MAKE("resourceLimits"));
790 return log_error_errno(r
, "Failed to drop resource limits field from identity: %m");
792 r
= json_variant_set_field(&add
, "resourceLimits", rlv
);
794 return log_error_errno(r
, "Failed to update resource limits of identity: %m");
798 if (!json_variant_is_blank_object(add
)) {
799 r
= json_variant_set_field(&add
, "matchMachineId", mmid
);
801 return log_error_errno(r
, "Failed to set matchMachineId field: %m");
806 r
= json_variant_new_array(&npm
, array
, i
);
808 return log_error_errno(r
, "Failed to allocate new perMachine array: %m");
810 json_variant_unref(per_machine
);
811 per_machine
= TAKE_PTR(npm
);
813 _cleanup_(json_variant_unrefp
) JsonVariant
*item
= json_variant_ref(arg_identity_extra_this_machine
);
815 if (arg_identity_extra_rlimits
) {
816 r
= json_variant_set_field(&item
, "resourceLimits", arg_identity_extra_rlimits
);
818 return log_error_errno(r
, "Failed to update resource limits of identity: %m");
821 r
= json_variant_set_field(&item
, "matchMachineId", mmid
);
823 return log_error_errno(r
, "Failed to set matchMachineId field: %m");
825 r
= json_variant_append_array(&per_machine
, item
);
827 return log_error_errno(r
, "Failed to append to perMachine array: %m");
830 r
= json_variant_set_field(&v
, "perMachine", per_machine
);
832 return log_error_errno(r
, "Failed to update per machine record: %m");
835 if (arg_identity_extra_privileged
|| arg_identity_filter
) {
836 _cleanup_(json_variant_unrefp
) JsonVariant
*privileged
= NULL
;
838 privileged
= json_variant_ref(json_variant_by_key(v
, "privileged"));
840 r
= json_variant_filter(&privileged
, arg_identity_filter
);
842 return log_error_errno(r
, "Failed to filter identity (privileged part): %m");
844 r
= json_variant_merge(&privileged
, arg_identity_extra_privileged
);
846 return log_error_errno(r
, "Failed to merge identities (privileged part): %m");
848 if (json_variant_is_blank_object(privileged
)) {
849 r
= json_variant_filter(&v
, STRV_MAKE("privileged"));
851 return log_error_errno(r
, "Failed to drop privileged part from identity: %m");
853 r
= json_variant_set_field(&v
, "privileged", privileged
);
855 return log_error_errno(r
, "Failed to update privileged part of identity: %m");
859 if (arg_identity_filter_rlimits
) {
860 _cleanup_(json_variant_unrefp
) JsonVariant
*rlv
= NULL
;
862 rlv
= json_variant_ref(json_variant_by_key(v
, "resourceLimits"));
864 r
= json_variant_filter(&rlv
, arg_identity_filter_rlimits
);
866 return log_error_errno(r
, "Failed to filter resource limits: %m");
868 /* Note that we only filter resource limits here, but don't apply them. We do that in the perMachine section */
870 if (json_variant_is_blank_object(rlv
)) {
871 r
= json_variant_filter(&v
, STRV_MAKE("resourceLimits"));
873 return log_error_errno(r
, "Failed to drop resource limits field from identity: %m");
875 r
= json_variant_set_field(&v
, "resourceLimits", rlv
);
877 return log_error_errno(r
, "Failed to update resource limits of identity: %m");
881 json_variant_unref(*_v
);
887 static int add_disposition(JsonVariant
**v
) {
892 if (json_variant_by_key(*v
, "disposition"))
895 /* Set the disposition to regular, if not configured explicitly */
896 r
= json_variant_set_field_string(v
, "disposition", "regular");
898 return log_error_errno(r
, "Failed to set disposition field: %m");
903 static int acquire_new_home_record(UserRecord
**ret
) {
904 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
905 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
912 unsigned line
, column
;
915 streq(arg_identity
, "-") ? stdin
: NULL
,
916 streq(arg_identity
, "-") ? "<stdin>" : arg_identity
, JSON_PARSE_SENSITIVE
, &v
, &line
, &column
);
918 return log_error_errno(r
, "Failed to parse identity at %u:%u: %m", line
, column
);
921 r
= apply_identity_changes(&v
);
925 r
= add_disposition(&v
);
929 STRV_FOREACH(i
, arg_pkcs11_token_uri
) {
930 r
= identity_add_pkcs11_key_data(&v
, *i
);
935 STRV_FOREACH(i
, arg_fido2_device
) {
936 r
= identity_add_fido2_parameters(&v
, *i
);
941 if (arg_recovery_key
) {
942 r
= identity_add_recovery_key(&v
);
947 r
= update_last_change(&v
, true, false);
952 json_variant_dump(v
, JSON_FORMAT_PRETTY
, NULL
, NULL
);
954 hr
= user_record_new();
958 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
);
966 static int acquire_new_password(
967 const char *user_name
,
979 e
= getenv("NEWPASSWORD");
981 _cleanup_(erase_and_freep
) char *copy
= NULL
;
983 /* As above, this is not for use, just for testing */
991 r
= user_record_set_password(hr
, STRV_MAKE(e
), /* prepend = */ true);
993 return log_error_errno(r
, "Failed to store password: %m");
996 assert_se(unsetenv("NEWPASSWORD") == 0);
999 *ret
= TAKE_PTR(copy
);
1005 (void) suggest_passwords();
1008 _cleanup_(strv_free_erasep
) char **first
= NULL
, **second
= NULL
;
1009 _cleanup_free_
char *question
= NULL
;
1012 return log_error_errno(SYNTHETIC_ERRNO(ENOKEY
), "Too many attempts, giving up:");
1014 if (asprintf(&question
, "Please enter new password for user %s:", user_name
) < 0)
1017 r
= ask_password_auto(question
, "user-home", NULL
, "home-password", USEC_INFINITY
, 0, &first
);
1019 return log_error_errno(r
, "Failed to acquire password: %m");
1021 question
= mfree(question
);
1022 if (asprintf(&question
, "Please enter new password for user %s (repeat):", user_name
) < 0)
1025 r
= ask_password_auto(question
, "user-home", NULL
, "home-password", USEC_INFINITY
, 0, &second
);
1027 return log_error_errno(r
, "Failed to acquire password: %m");
1029 if (strv_equal(first
, second
)) {
1030 _cleanup_(erase_and_freep
) char *copy
= NULL
;
1033 copy
= strdup(first
[0]);
1038 r
= user_record_set_password(hr
, first
, /* prepend = */ true);
1040 return log_error_errno(r
, "Failed to store password: %m");
1043 *ret
= TAKE_PTR(copy
);
1048 log_error("Password didn't match, try again.");
1052 static int create_home(int argc
, char *argv
[], void *userdata
) {
1053 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1054 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
1057 r
= acquire_bus(&bus
);
1061 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1064 /* If a username was specified, use it */
1066 if (valid_user_group_name(argv
[1], 0))
1067 r
= json_variant_set_field_string(&arg_identity_extra
, "userName", argv
[1]);
1069 _cleanup_free_
char *un
= NULL
, *rr
= NULL
;
1071 /* Before we consider the user name invalid, let's check if we can split it? */
1072 r
= split_user_name_realm(argv
[1], &un
, &rr
);
1074 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User name '%s' is not valid: %m", argv
[1]);
1077 r
= json_variant_set_field_string(&arg_identity_extra
, "realm", rr
);
1079 return log_error_errno(r
, "Failed to set realm field: %m");
1082 r
= json_variant_set_field_string(&arg_identity_extra
, "userName", un
);
1085 return log_error_errno(r
, "Failed to set userName field: %m");
1087 /* If neither a username nor an identity have been specified we cannot operate. */
1089 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User name required.");
1092 r
= acquire_new_home_record(&hr
);
1096 /* If the JSON record carries no plain text password (besides the recovery key), then let's query it
1098 if (strv_length(hr
->password
) <= arg_recovery_key
) {
1100 if (strv_isempty(hr
->hashed_password
)) {
1101 _cleanup_(erase_and_freep
) char *new_password
= NULL
;
1103 /* No regular (i.e. non-PKCS#11) hashed passwords set in the record, let's fix that. */
1104 r
= acquire_new_password(hr
->user_name
, hr
, /* suggest = */ true, &new_password
);
1108 r
= user_record_make_hashed_password(hr
, STRV_MAKE(new_password
), /* extend = */ false);
1110 return log_error_errno(r
, "Failed to hash password: %m");
1112 /* There's a hash password set in the record, acquire the unhashed version of it. */
1113 r
= acquire_existing_password(hr
->user_name
, hr
, /* emphasize_current= */ false);
1119 if (hr
->enforce_password_policy
== 0) {
1120 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1122 /* If password quality enforcement is disabled, let's at least warn client side */
1124 r
= user_record_quality_check_password(hr
, hr
, &error
);
1126 log_warning_errno(r
, "Specified password does not pass quality checks (%s), proceeding anyway.", bus_error_message(&error
, r
));
1130 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1131 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1132 _cleanup_(erase_and_freep
) char *formatted
= NULL
;
1134 r
= json_variant_format(hr
->json
, 0, &formatted
);
1136 return log_error_errno(r
, "Failed to format user record: %m");
1138 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "CreateHome");
1140 return bus_log_create_error(r
);
1142 (void) sd_bus_message_sensitive(m
);
1144 r
= sd_bus_message_append(m
, "s", formatted
);
1146 return bus_log_create_error(r
);
1148 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1150 if (sd_bus_error_has_name(&error
, BUS_ERROR_LOW_PASSWORD_QUALITY
)) {
1151 _cleanup_(erase_and_freep
) char *new_password
= NULL
;
1153 log_error_errno(r
, "%s", bus_error_message(&error
, r
));
1154 log_info("(Use --enforce-password-policy=no to turn off password quality checks for this account.)");
1156 r
= acquire_new_password(hr
->user_name
, hr
, /* suggest = */ false, &new_password
);
1160 r
= user_record_make_hashed_password(hr
, STRV_MAKE(new_password
), /* extend = */ false);
1162 return log_error_errno(r
, "Failed to hash passwords: %m");
1164 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1175 static int remove_home(int argc
, char *argv
[], void *userdata
) {
1176 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1180 r
= acquire_bus(&bus
);
1184 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1186 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
1187 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1188 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1190 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "RemoveHome");
1192 return bus_log_create_error(r
);
1194 r
= sd_bus_message_append(m
, "s", *i
);
1196 return bus_log_create_error(r
);
1198 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1200 log_error_errno(r
, "Failed to remove home: %s", bus_error_message(&error
, r
));
1209 static int acquire_updated_home_record(
1211 const char *username
,
1214 _cleanup_(json_variant_unrefp
) JsonVariant
*json
= NULL
;
1215 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
1222 unsigned line
, column
;
1225 r
= json_parse_file(
1226 streq(arg_identity
, "-") ? stdin
: NULL
,
1227 streq(arg_identity
, "-") ? "<stdin>" : arg_identity
, JSON_PARSE_SENSITIVE
, &json
, &line
, &column
);
1229 return log_error_errno(r
, "Failed to parse identity at %u:%u: %m", line
, column
);
1231 un
= json_variant_by_key(json
, "userName");
1233 if (!json_variant_is_string(un
) || (username
&& !streq(json_variant_string(un
), username
)))
1234 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User name specified on command line and in JSON record do not match.");
1237 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "No username specified.");
1239 r
= json_variant_set_field_string(&arg_identity_extra
, "userName", username
);
1241 return log_error_errno(r
, "Failed to set userName field: %m");
1245 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1246 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1250 if (!identity_properties_specified())
1251 return log_error_errno(SYNTHETIC_ERRNO(EALREADY
), "No field to change specified.");
1253 r
= bus_call_method(bus
, bus_mgr
, "GetUserRecordByName", &error
, &reply
, "s", username
);
1255 return log_error_errno(r
, "Failed to acquire user home record: %s", bus_error_message(&error
, r
));
1257 r
= sd_bus_message_read(reply
, "sbo", &text
, &incomplete
, NULL
);
1259 return bus_log_parse_error(r
);
1262 return log_error_errno(SYNTHETIC_ERRNO(EACCES
), "Lacking rights to acquire user record including privileged metadata, can't update record.");
1264 r
= json_parse(text
, JSON_PARSE_SENSITIVE
, &json
, NULL
, NULL
);
1266 return log_error_errno(r
, "Failed to parse JSON identity: %m");
1268 reply
= sd_bus_message_unref(reply
);
1270 r
= json_variant_filter(&json
, STRV_MAKE("binding", "status", "signature"));
1272 return log_error_errno(r
, "Failed to strip binding and status from record to update: %m");
1275 r
= apply_identity_changes(&json
);
1279 STRV_FOREACH(i
, arg_pkcs11_token_uri
) {
1280 r
= identity_add_pkcs11_key_data(&json
, *i
);
1285 STRV_FOREACH(i
, arg_fido2_device
) {
1286 r
= identity_add_fido2_parameters(&json
, *i
);
1291 /* If the user supplied a full record, then add in lastChange, but do not override. Otherwise always
1293 r
= update_last_change(&json
, arg_pkcs11_token_uri
|| arg_fido2_device
, !arg_identity
);
1298 json_variant_dump(json
, JSON_FORMAT_PRETTY
, NULL
, NULL
);
1300 hr
= user_record_new();
1304 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
);
1308 *ret
= TAKE_PTR(hr
);
1312 static int home_record_reset_human_interaction_permission(UserRecord
*hr
) {
1317 /* When we execute multiple operations one after the other, let's reset the permission to ask the
1318 * user each time, so that if interaction is necessary we will be told so again and thus can print a
1319 * nice message to the user, telling the user so. */
1321 r
= user_record_set_pkcs11_protected_authentication_path_permitted(hr
, -1);
1323 return log_error_errno(r
, "Failed to reset PKCS#11 protected authentication path permission flag: %m");
1325 r
= user_record_set_fido2_user_presence_permitted(hr
, -1);
1327 return log_error_errno(r
, "Failed to reset FIDO2 user presence permission flag: %m");
1332 static int update_home(int argc
, char *argv
[], void *userdata
) {
1333 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1334 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
1335 _cleanup_free_
char *buffer
= NULL
;
1336 const char *username
;
1341 else if (!arg_identity
) {
1342 buffer
= getusername_malloc();
1350 r
= acquire_bus(&bus
);
1354 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1356 r
= acquire_updated_home_record(bus
, username
, &hr
);
1360 /* If we do multiple operations, let's output things more verbosely, since otherwise the repeated
1361 * authentication might be confusing. */
1363 if (arg_and_resize
|| arg_and_change_password
)
1364 log_info("Updating home directory.");
1367 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1368 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1369 _cleanup_free_
char *formatted
= NULL
;
1371 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "UpdateHome");
1373 return bus_log_create_error(r
);
1375 r
= json_variant_format(hr
->json
, 0, &formatted
);
1377 return log_error_errno(r
, "Failed to format user record: %m");
1379 (void) sd_bus_message_sensitive(m
);
1381 r
= sd_bus_message_append(m
, "s", formatted
);
1383 return bus_log_create_error(r
);
1385 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1387 if (arg_and_change_password
&&
1388 sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1389 /* In the generic handler we'd ask for a password in this case, but when
1390 * changing passwords that's not sufficient, as we need to acquire all keys
1392 return log_error_errno(r
, "Security token not inserted, refusing.");
1394 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1402 log_info("Resizing home.");
1404 (void) home_record_reset_human_interaction_permission(hr
);
1406 /* Also sync down disk size to underlying LUKS/fscrypt/quota */
1407 while (arg_and_resize
) {
1408 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1409 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1411 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ResizeHome");
1413 return bus_log_create_error(r
);
1415 /* Specify UINT64_MAX as size, in which case the underlying disk size will just be synced */
1416 r
= sd_bus_message_append(m
, "st", hr
->user_name
, UINT64_MAX
);
1418 return bus_log_create_error(r
);
1420 r
= bus_message_append_secret(m
, hr
);
1422 return bus_log_create_error(r
);
1424 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1426 if (arg_and_change_password
&&
1427 sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1428 return log_error_errno(r
, "Security token not inserted, refusing.");
1430 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1437 if (arg_and_change_password
)
1438 log_info("Synchronizing passwords and encryption keys.");
1440 (void) home_record_reset_human_interaction_permission(hr
);
1442 /* Also sync down passwords to underlying LUKS/fscrypt */
1443 while (arg_and_change_password
) {
1444 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1445 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1447 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ChangePasswordHome");
1449 return bus_log_create_error(r
);
1451 /* Specify an empty new secret, in which case the underlying LUKS/fscrypt password will just be synced */
1452 r
= sd_bus_message_append(m
, "ss", hr
->user_name
, "{}");
1454 return bus_log_create_error(r
);
1456 r
= bus_message_append_secret(m
, hr
);
1458 return bus_log_create_error(r
);
1460 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1462 if (sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1463 return log_error_errno(r
, "Security token not inserted, refusing.");
1465 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1475 static int passwd_home(int argc
, char *argv
[], void *userdata
) {
1476 _cleanup_(user_record_unrefp
) UserRecord
*old_secret
= NULL
, *new_secret
= NULL
;
1477 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1478 _cleanup_free_
char *buffer
= NULL
;
1479 const char *username
;
1482 if (arg_pkcs11_token_uri
)
1483 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "To change the PKCS#11 security token use 'homectl update --pkcs11-token-uri=…'.");
1484 if (arg_fido2_device
)
1485 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "To change the FIDO2 security token use 'homectl update --fido2-device=…'.");
1486 if (identity_properties_specified())
1487 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "The 'passwd' verb does not permit changing other record properties at the same time.");
1492 buffer
= getusername_malloc();
1499 r
= acquire_bus(&bus
);
1503 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1505 old_secret
= user_record_new();
1509 new_secret
= user_record_new();
1513 r
= acquire_new_password(username
, new_secret
, /* suggest = */ true, NULL
);
1518 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1519 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1521 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ChangePasswordHome");
1523 return bus_log_create_error(r
);
1525 r
= sd_bus_message_append(m
, "s", username
);
1527 return bus_log_create_error(r
);
1529 r
= bus_message_append_secret(m
, new_secret
);
1531 return bus_log_create_error(r
);
1533 r
= bus_message_append_secret(m
, old_secret
);
1535 return bus_log_create_error(r
);
1537 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1539 if (sd_bus_error_has_name(&error
, BUS_ERROR_LOW_PASSWORD_QUALITY
)) {
1541 log_error_errno(r
, "%s", bus_error_message(&error
, r
));
1543 r
= acquire_new_password(username
, new_secret
, /* suggest = */ false, NULL
);
1545 } else if (sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1547 /* In the generic handler we'd ask for a password in this case, but when
1548 * changing passwords that's not sufficeint, as we need to acquire all keys
1550 return log_error_errno(r
, "Security token not inserted, refusing.");
1552 r
= handle_generic_user_record_error(username
, old_secret
, &error
, r
, true);
1562 static int resize_home(int argc
, char *argv
[], void *userdata
) {
1563 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1564 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
1565 uint64_t ds
= UINT64_MAX
;
1568 r
= acquire_bus(&bus
);
1572 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1574 if (arg_disk_size_relative
!= UINT64_MAX
||
1575 (argc
> 2 && parse_percent(argv
[2]) >= 0))
1576 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
1577 "Relative disk size specification currently not supported when resizing.");
1580 r
= parse_size(argv
[2], 1024, &ds
);
1582 return log_error_errno(r
, "Failed to parse disk size parameter: %s", argv
[2]);
1585 if (arg_disk_size
!= UINT64_MAX
) {
1586 if (ds
!= UINT64_MAX
&& ds
!= arg_disk_size
)
1587 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Disk size specified twice and doesn't match, refusing.");
1592 secret
= user_record_new();
1597 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1598 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1600 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ResizeHome");
1602 return bus_log_create_error(r
);
1604 r
= sd_bus_message_append(m
, "st", argv
[1], ds
);
1606 return bus_log_create_error(r
);
1608 r
= bus_message_append_secret(m
, secret
);
1610 return bus_log_create_error(r
);
1612 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1614 r
= handle_generic_user_record_error(argv
[1], secret
, &error
, r
, false);
1624 static int lock_home(int argc
, char *argv
[], void *userdata
) {
1625 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1629 r
= acquire_bus(&bus
);
1633 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
1634 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1635 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1637 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "LockHome");
1639 return bus_log_create_error(r
);
1641 r
= sd_bus_message_append(m
, "s", *i
);
1643 return bus_log_create_error(r
);
1645 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1647 log_error_errno(r
, "Failed to lock home: %s", bus_error_message(&error
, r
));
1656 static int unlock_home(int argc
, char *argv
[], void *userdata
) {
1657 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1661 r
= acquire_bus(&bus
);
1665 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
1666 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
1668 secret
= user_record_new();
1673 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1674 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1676 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "UnlockHome");
1678 return bus_log_create_error(r
);
1680 r
= sd_bus_message_append(m
, "s", *i
);
1682 return bus_log_create_error(r
);
1684 r
= bus_message_append_secret(m
, secret
);
1686 return bus_log_create_error(r
);
1688 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1690 r
= handle_generic_user_record_error(argv
[1], secret
, &error
, r
, false);
1705 static int with_home(int argc
, char *argv
[], void *userdata
) {
1706 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1707 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1708 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1709 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
1710 _cleanup_close_
int acquired_fd
= -1;
1711 _cleanup_strv_free_
char **cmdline
= NULL
;
1716 r
= acquire_bus(&bus
);
1721 _cleanup_free_
char *shell
= NULL
;
1723 /* If no command is specified, spawn a shell */
1724 r
= get_shell(&shell
);
1726 return log_error_errno(r
, "Failed to acquire shell: %m");
1728 cmdline
= strv_new(shell
);
1730 cmdline
= strv_copy(argv
+ 2);
1734 secret
= user_record_new();
1739 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "AcquireHome");
1741 return bus_log_create_error(r
);
1743 r
= sd_bus_message_append(m
, "s", argv
[1]);
1745 return bus_log_create_error(r
);
1747 r
= bus_message_append_secret(m
, secret
);
1749 return bus_log_create_error(r
);
1751 r
= sd_bus_message_append(m
, "b", /* please_suspend = */ getenv_bool("SYSTEMD_PLEASE_SUSPEND_HOME") > 0);
1753 return bus_log_create_error(r
);
1755 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, &reply
);
1756 m
= sd_bus_message_unref(m
);
1758 r
= handle_generic_user_record_error(argv
[1], secret
, &error
, r
, false);
1762 sd_bus_error_free(&error
);
1766 r
= sd_bus_message_read(reply
, "h", &fd
);
1768 return bus_log_parse_error(r
);
1770 acquired_fd
= fcntl(fd
, F_DUPFD_CLOEXEC
, 3);
1771 if (acquired_fd
< 0)
1772 return log_error_errno(errno
, "Failed to duplicate acquired fd: %m");
1774 reply
= sd_bus_message_unref(reply
);
1779 r
= bus_call_method(bus
, bus_mgr
, "GetHomeByName", &error
, &reply
, "s", argv
[1]);
1781 return log_error_errno(r
, "Failed to inspect home: %s", bus_error_message(&error
, r
));
1783 r
= sd_bus_message_read(reply
, "usussso", NULL
, NULL
, NULL
, NULL
, &home
, NULL
, NULL
);
1785 return bus_log_parse_error(r
);
1787 r
= safe_fork("(with)", FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_DEATHSIG
|FORK_LOG
|FORK_RLIMIT_NOFILE_SAFE
|FORK_REOPEN_LOG
, &pid
);
1791 if (chdir(home
) < 0) {
1792 log_error_errno(errno
, "Failed to change to directory %s: %m", home
);
1796 execvp(cmdline
[0], cmdline
);
1797 log_error_errno(errno
, "Failed to execute %s: %m", cmdline
[0]);
1801 ret
= wait_for_terminate_and_check(cmdline
[0], pid
, WAIT_LOG_ABNORMAL
);
1803 /* Close the fd that pings the home now. */
1804 acquired_fd
= safe_close(acquired_fd
);
1806 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ReleaseHome");
1808 return bus_log_create_error(r
);
1810 r
= sd_bus_message_append(m
, "s", argv
[1]);
1812 return bus_log_create_error(r
);
1814 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1816 if (sd_bus_error_has_name(&error
, BUS_ERROR_HOME_BUSY
))
1817 log_notice("Not deactivating home directory of %s, as it is still used.", argv
[1]);
1819 return log_error_errno(r
, "Failed to release user home: %s", bus_error_message(&error
, r
));
1825 static int lock_all_homes(int argc
, char *argv
[], void *userdata
) {
1826 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1827 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1828 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1831 r
= acquire_bus(&bus
);
1835 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "LockAllHomes");
1837 return bus_log_create_error(r
);
1839 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1841 return log_error_errno(r
, "Failed to lock all homes: %s", bus_error_message(&error
, r
));
1846 static int deactivate_all_homes(int argc
, char *argv
[], void *userdata
) {
1847 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1848 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1849 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1852 r
= acquire_bus(&bus
);
1856 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "DeactivateAllHomes");
1858 return bus_log_create_error(r
);
1860 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1862 return log_error_errno(r
, "Failed to deactivate all homes: %s", bus_error_message(&error
, r
));
1867 static int drop_from_identity(const char *field
) {
1872 /* If we are called to update an identity record and drop some field, let's keep track of what to
1873 * remove from the old record */
1874 r
= strv_extend(&arg_identity_filter
, field
);
1878 /* Let's also drop the field if it was previously set to a new value on the same command line */
1879 r
= json_variant_filter(&arg_identity_extra
, STRV_MAKE(field
));
1881 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
1883 r
= json_variant_filter(&arg_identity_extra_this_machine
, STRV_MAKE(field
));
1885 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
1887 r
= json_variant_filter(&arg_identity_extra_privileged
, STRV_MAKE(field
));
1889 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
1894 static int help(int argc
, char *argv
[], void *userdata
) {
1895 _cleanup_free_
char *link
= NULL
;
1898 (void) pager_open(arg_pager_flags
);
1900 r
= terminal_urlify_man("homectl", "1", &link
);
1904 printf("%1$s [OPTIONS...] COMMAND ...\n\n"
1905 "%2$sCreate, manipulate or inspect home directories.%3$s\n"
1906 "\n%4$sCommands:%5$s\n"
1907 " list List home areas\n"
1908 " activate USER… Activate a home area\n"
1909 " deactivate USER… Deactivate a home area\n"
1910 " inspect USER… Inspect a home area\n"
1911 " authenticate USER… Authenticate a home area\n"
1912 " create USER Create a home area\n"
1913 " remove USER… Remove a home area\n"
1914 " update USER Update a home area\n"
1915 " passwd USER Change password of a home area\n"
1916 " resize USER SIZE Resize a home area\n"
1917 " lock USER… Temporarily lock an active home area\n"
1918 " unlock USER… Unlock a temporarily locked home area\n"
1919 " lock-all Lock all suitable home areas\n"
1920 " deactivate-all Deactivate all active home areas\n"
1921 " with USER [COMMAND…] Run shell or command with access to a home area\n"
1922 "\n%4$sOptions:%5$s\n"
1923 " -h --help Show this help\n"
1924 " --version Show package version\n"
1925 " --no-pager Do not pipe output into a pager\n"
1926 " --no-legend Do not show the headers and footers\n"
1927 " --no-ask-password Do not ask for system passwords\n"
1928 " -H --host=[USER@]HOST Operate on remote host\n"
1929 " -M --machine=CONTAINER Operate on local container\n"
1930 " --identity=PATH Read JSON identity from file\n"
1931 " --json=FORMAT Output inspection data in JSON (takes one of\n"
1932 " pretty, short, off)\n"
1933 " -j Equivalent to --json=pretty (on TTY) or\n"
1934 " --json=short (otherwise)\n"
1935 " --export-format= Strip JSON inspection data (full, stripped,\n"
1937 " -E When specified once equals -j --export-format=\n"
1938 " stripped, when specified twice equals\n"
1939 " -j --export-format=minimal\n"
1940 "\n%4$sGeneral User Record Properties:%5$s\n"
1941 " -c --real-name=REALNAME Real name for user\n"
1942 " --realm=REALM Realm to create user in\n"
1943 " --email-address=EMAIL Email address for user\n"
1944 " --location=LOCATION Set location of user on earth\n"
1945 " --icon-name=NAME Icon name for user\n"
1946 " -d --home-dir=PATH Home directory\n"
1947 " -u --uid=UID Numeric UID for user\n"
1948 " -G --member-of=GROUP Add user to group\n"
1949 " --skel=PATH Skeleton directory to use\n"
1950 " --shell=PATH Shell for account\n"
1951 " --setenv=VARIABLE=VALUE Set an environment variable at log-in\n"
1952 " --timezone=TIMEZONE Set a time-zone\n"
1953 " --language=LOCALE Set preferred language\n"
1954 " --ssh-authorized-keys=KEYS\n"
1955 " Specify SSH public keys\n"
1956 " --pkcs11-token-uri=URI URI to PKCS#11 security token containing\n"
1957 " private key and matching X.509 certificate\n"
1958 " --fido2-device=PATH Path to FIDO2 hidraw device with hmac-secret\n"
1960 " --recovery-key=BOOL Add a recovery key\n"
1961 "\n%4$sAccount Management User Record Properties:%5$s\n"
1962 " --locked=BOOL Set locked account state\n"
1963 " --not-before=TIMESTAMP Do not allow logins before\n"
1964 " --not-after=TIMESTAMP Do not allow logins after\n"
1965 " --rate-limit-interval=SECS\n"
1966 " Login rate-limit interval in seconds\n"
1967 " --rate-limit-burst=NUMBER\n"
1968 " Login rate-limit attempts per interval\n"
1969 "\n%4$sPassword Policy User Record Properties:%5$s\n"
1970 " --password-hint=HINT Set Password hint\n"
1971 " --enforce-password-policy=BOOL\n"
1972 " Control whether to enforce system's password\n"
1973 " policy for this user\n"
1974 " -P Equivalent to --enforce-password-password=no\n"
1975 " --password-change-now=BOOL\n"
1976 " Require the password to be changed on next login\n"
1977 " --password-change-min=TIME\n"
1978 " Require minimum time between password changes\n"
1979 " --password-change-max=TIME\n"
1980 " Require maximum time between password changes\n"
1981 " --password-change-warn=TIME\n"
1982 " How much time to warn before password expiry\n"
1983 " --password-change-inactive=TIME\n"
1984 " How much time to block password after expiry\n"
1985 "\n%4$sResource Management User Record Properties:%5$s\n"
1986 " --disk-size=BYTES Size to assign the user on disk\n"
1987 " --access-mode=MODE User home directory access mode\n"
1988 " --umask=MODE Umask for user when logging in\n"
1989 " --nice=NICE Nice level for user\n"
1990 " --rlimit=LIMIT=VALUE[:VALUE]\n"
1991 " Set resource limits\n"
1992 " --tasks-max=MAX Set maximum number of per-user tasks\n"
1993 " --memory-high=BYTES Set high memory threshold in bytes\n"
1994 " --memory-max=BYTES Set maximum memory limit\n"
1995 " --cpu-weight=WEIGHT Set CPU weight\n"
1996 " --io-weight=WEIGHT Set IO weight\n"
1997 "\n%4$sStorage User Record Properties:%5$s\n"
1998 " --storage=STORAGE Storage type to use (luks, fscrypt, directory,\n"
1999 " subvolume, cifs)\n"
2000 " --image-path=PATH Path to image file/directory\n"
2001 "\n%4$sLUKS Storage User Record Properties:%5$s\n"
2002 " --fs-type=TYPE File system type to use in case of luks\n"
2003 " storage (btrfs, ext4, xfs)\n"
2004 " --luks-discard=BOOL Whether to use 'discard' feature of file system\n"
2005 " when activated (mounted)\n"
2006 " --luks-offline-discard=BOOL\n"
2007 " Whether to trim file on logout\n"
2008 " --luks-cipher=CIPHER Cipher to use for LUKS encryption\n"
2009 " --luks-cipher-mode=MODE Cipher mode to use for LUKS encryption\n"
2010 " --luks-volume-key-size=BITS\n"
2011 " Volume key size to use for LUKS encryption\n"
2012 " --luks-pbkdf-type=TYPE Password-based Key Derivation Function to use\n"
2013 " --luks-pbkdf-hash-algorithm=ALGORITHM\n"
2014 " PBKDF hash algorithm to use\n"
2015 " --luks-pbkdf-time-cost=SECS\n"
2016 " Time cost for PBKDF in seconds\n"
2017 " --luks-pbkdf-memory-cost=BYTES\n"
2018 " Memory cost for PBKDF in bytes\n"
2019 " --luks-pbkdf-parallel-threads=NUMBER\n"
2020 " Number of parallel threads for PKBDF\n"
2021 "\n%4$sMounting User Record Properties:%5$s\n"
2022 " --nosuid=BOOL Control the 'nosuid' flag of the home mount\n"
2023 " --nodev=BOOL Control the 'nodev' flag of the home mount\n"
2024 " --noexec=BOOL Control the 'noexec' flag of the home mount\n"
2025 "\n%4$sCIFS User Record Properties:%5$s\n"
2026 " --cifs-domain=DOMAIN CIFS (Windows) domain\n"
2027 " --cifs-user-name=USER CIFS (Windows) user name\n"
2028 " --cifs-service=SERVICE CIFS (Windows) service to mount as home area\n"
2029 "\n%4$sLogin Behaviour User Record Properties:%5$s\n"
2030 " --stop-delay=SECS How long to leave user services running after\n"
2032 " --kill-processes=BOOL Whether to kill user processes when sessions\n"
2034 " --auto-login=BOOL Try to log this user in automatically\n"
2035 "\nSee the %6$s for details.\n"
2036 , program_invocation_short_name
2037 , ansi_highlight(), ansi_normal()
2038 , ansi_underline(), ansi_normal()
2045 static int parse_argv(int argc
, char *argv
[]) {
2048 ARG_VERSION
= 0x100,
2051 ARG_NO_ASK_PASSWORD
,
2061 ARG_LUKS_OFFLINE_DISCARD
,
2067 ARG_SSH_AUTHORIZED_KEYS
,
2076 ARG_LUKS_CIPHER_MODE
,
2077 ARG_LUKS_VOLUME_KEY_SIZE
,
2089 ARG_LUKS_PBKDF_TYPE
,
2090 ARG_LUKS_PBKDF_HASH_ALGORITHM
,
2091 ARG_LUKS_PBKDF_TIME_COST
,
2092 ARG_LUKS_PBKDF_MEMORY_COST
,
2093 ARG_LUKS_PBKDF_PARALLEL_THREADS
,
2094 ARG_RATE_LIMIT_INTERVAL
,
2095 ARG_RATE_LIMIT_BURST
,
2098 ARG_ENFORCE_PASSWORD_POLICY
,
2099 ARG_PASSWORD_CHANGE_NOW
,
2100 ARG_PASSWORD_CHANGE_MIN
,
2101 ARG_PASSWORD_CHANGE_MAX
,
2102 ARG_PASSWORD_CHANGE_WARN
,
2103 ARG_PASSWORD_CHANGE_INACTIVE
,
2106 ARG_PKCS11_TOKEN_URI
,
2110 ARG_AND_CHANGE_PASSWORD
,
2113 static const struct option options
[] = {
2114 { "help", no_argument
, NULL
, 'h' },
2115 { "version", no_argument
, NULL
, ARG_VERSION
},
2116 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
2117 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
2118 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
2119 { "host", required_argument
, NULL
, 'H' },
2120 { "machine", required_argument
, NULL
, 'M' },
2121 { "identity", required_argument
, NULL
, 'I' },
2122 { "real-name", required_argument
, NULL
, 'c' },
2123 { "comment", required_argument
, NULL
, 'c' }, /* Compat alias to keep thing in sync with useradd(8) */
2124 { "realm", required_argument
, NULL
, ARG_REALM
},
2125 { "email-address", required_argument
, NULL
, ARG_EMAIL_ADDRESS
},
2126 { "location", required_argument
, NULL
, ARG_LOCATION
},
2127 { "password-hint", required_argument
, NULL
, ARG_PASSWORD_HINT
},
2128 { "icon-name", required_argument
, NULL
, ARG_ICON_NAME
},
2129 { "home-dir", required_argument
, NULL
, 'd' }, /* Compatible with useradd(8) */
2130 { "uid", required_argument
, NULL
, 'u' }, /* Compatible with useradd(8) */
2131 { "member-of", required_argument
, NULL
, 'G' },
2132 { "groups", required_argument
, NULL
, 'G' }, /* Compat alias to keep thing in sync with useradd(8) */
2133 { "skel", required_argument
, NULL
, 'k' }, /* Compatible with useradd(8) */
2134 { "shell", required_argument
, NULL
, 's' }, /* Compatible with useradd(8) */
2135 { "setenv", required_argument
, NULL
, ARG_SETENV
},
2136 { "timezone", required_argument
, NULL
, ARG_TIMEZONE
},
2137 { "language", required_argument
, NULL
, ARG_LANGUAGE
},
2138 { "locked", required_argument
, NULL
, ARG_LOCKED
},
2139 { "not-before", required_argument
, NULL
, ARG_NOT_BEFORE
},
2140 { "not-after", required_argument
, NULL
, ARG_NOT_AFTER
},
2141 { "expiredate", required_argument
, NULL
, 'e' }, /* Compat alias to keep thing in sync with useradd(8) */
2142 { "ssh-authorized-keys", required_argument
, NULL
, ARG_SSH_AUTHORIZED_KEYS
},
2143 { "disk-size", required_argument
, NULL
, ARG_DISK_SIZE
},
2144 { "access-mode", required_argument
, NULL
, ARG_ACCESS_MODE
},
2145 { "umask", required_argument
, NULL
, ARG_UMASK
},
2146 { "nice", required_argument
, NULL
, ARG_NICE
},
2147 { "rlimit", required_argument
, NULL
, ARG_RLIMIT
},
2148 { "tasks-max", required_argument
, NULL
, ARG_TASKS_MAX
},
2149 { "memory-high", required_argument
, NULL
, ARG_MEMORY_HIGH
},
2150 { "memory-max", required_argument
, NULL
, ARG_MEMORY_MAX
},
2151 { "cpu-weight", required_argument
, NULL
, ARG_CPU_WEIGHT
},
2152 { "io-weight", required_argument
, NULL
, ARG_IO_WEIGHT
},
2153 { "storage", required_argument
, NULL
, ARG_STORAGE
},
2154 { "image-path", required_argument
, NULL
, ARG_IMAGE_PATH
},
2155 { "fs-type", required_argument
, NULL
, ARG_FS_TYPE
},
2156 { "luks-discard", required_argument
, NULL
, ARG_LUKS_DISCARD
},
2157 { "luks-offline-discard", required_argument
, NULL
, ARG_LUKS_OFFLINE_DISCARD
},
2158 { "luks-cipher", required_argument
, NULL
, ARG_LUKS_CIPHER
},
2159 { "luks-cipher-mode", required_argument
, NULL
, ARG_LUKS_CIPHER_MODE
},
2160 { "luks-volume-key-size", required_argument
, NULL
, ARG_LUKS_VOLUME_KEY_SIZE
},
2161 { "luks-pbkdf-type", required_argument
, NULL
, ARG_LUKS_PBKDF_TYPE
},
2162 { "luks-pbkdf-hash-algorithm", required_argument
, NULL
, ARG_LUKS_PBKDF_HASH_ALGORITHM
},
2163 { "luks-pbkdf-time-cost", required_argument
, NULL
, ARG_LUKS_PBKDF_TIME_COST
},
2164 { "luks-pbkdf-memory-cost", required_argument
, NULL
, ARG_LUKS_PBKDF_MEMORY_COST
},
2165 { "luks-pbkdf-parallel-threads", required_argument
, NULL
, ARG_LUKS_PBKDF_PARALLEL_THREADS
},
2166 { "nosuid", required_argument
, NULL
, ARG_NOSUID
},
2167 { "nodev", required_argument
, NULL
, ARG_NODEV
},
2168 { "noexec", required_argument
, NULL
, ARG_NOEXEC
},
2169 { "cifs-user-name", required_argument
, NULL
, ARG_CIFS_USER_NAME
},
2170 { "cifs-domain", required_argument
, NULL
, ARG_CIFS_DOMAIN
},
2171 { "cifs-service", required_argument
, NULL
, ARG_CIFS_SERVICE
},
2172 { "rate-limit-interval", required_argument
, NULL
, ARG_RATE_LIMIT_INTERVAL
},
2173 { "rate-limit-burst", required_argument
, NULL
, ARG_RATE_LIMIT_BURST
},
2174 { "stop-delay", required_argument
, NULL
, ARG_STOP_DELAY
},
2175 { "kill-processes", required_argument
, NULL
, ARG_KILL_PROCESSES
},
2176 { "enforce-password-policy", required_argument
, NULL
, ARG_ENFORCE_PASSWORD_POLICY
},
2177 { "password-change-now", required_argument
, NULL
, ARG_PASSWORD_CHANGE_NOW
},
2178 { "password-change-min", required_argument
, NULL
, ARG_PASSWORD_CHANGE_MIN
},
2179 { "password-change-max", required_argument
, NULL
, ARG_PASSWORD_CHANGE_MAX
},
2180 { "password-change-warn", required_argument
, NULL
, ARG_PASSWORD_CHANGE_WARN
},
2181 { "password-change-inactive", required_argument
, NULL
, ARG_PASSWORD_CHANGE_INACTIVE
},
2182 { "auto-login", required_argument
, NULL
, ARG_AUTO_LOGIN
},
2183 { "json", required_argument
, NULL
, ARG_JSON
},
2184 { "export-format", required_argument
, NULL
, ARG_EXPORT_FORMAT
},
2185 { "pkcs11-token-uri", required_argument
, NULL
, ARG_PKCS11_TOKEN_URI
},
2186 { "fido2-device", required_argument
, NULL
, ARG_FIDO2_DEVICE
},
2187 { "recovery-key", required_argument
, NULL
, ARG_RECOVERY_KEY
},
2188 { "and-resize", required_argument
, NULL
, ARG_AND_RESIZE
},
2189 { "and-change-password", required_argument
, NULL
, ARG_AND_CHANGE_PASSWORD
},
2201 c
= getopt_long(argc
, argv
, "hH:M:I:c:d:u:k:s:e:G:jPE", options
, NULL
);
2208 return help(0, NULL
, NULL
);
2214 arg_pager_flags
|= PAGER_DISABLE
;
2221 case ARG_NO_ASK_PASSWORD
:
2222 arg_ask_password
= false;
2226 arg_transport
= BUS_TRANSPORT_REMOTE
;
2231 arg_transport
= BUS_TRANSPORT_MACHINE
;
2236 arg_identity
= optarg
;
2240 if (isempty(optarg
)) {
2241 r
= drop_from_identity("realName");
2248 if (!valid_gecos(optarg
))
2249 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Real name '%s' not a valid GECOS field.", optarg
);
2251 r
= json_variant_set_field_string(&arg_identity_extra
, "realName", optarg
);
2253 return log_error_errno(r
, "Failed to set realName field: %m");
2258 _cleanup_free_
char *hd
= NULL
;
2260 if (isempty(optarg
)) {
2261 r
= drop_from_identity("homeDirectory");
2268 r
= parse_path_argument_and_warn(optarg
, false, &hd
);
2272 if (!valid_home(hd
))
2273 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Home directory '%s' not valid.", hd
);
2275 r
= json_variant_set_field_string(&arg_identity_extra
, "homeDirectory", hd
);
2277 return log_error_errno(r
, "Failed to set homeDirectory field: %m");
2283 if (isempty(optarg
)) {
2284 r
= drop_from_identity("realm");
2291 r
= dns_name_is_valid(optarg
);
2293 return log_error_errno(r
, "Failed to determine whether realm '%s' is a valid DNS domain: %m", optarg
);
2295 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Realm '%s' is not a valid DNS domain: %m", optarg
);
2297 r
= json_variant_set_field_string(&arg_identity_extra
, "realm", optarg
);
2299 return log_error_errno(r
, "Failed to set realm field: %m");
2302 case ARG_EMAIL_ADDRESS
:
2305 case ARG_CIFS_USER_NAME
:
2306 case ARG_CIFS_DOMAIN
:
2307 case ARG_CIFS_SERVICE
: {
2310 c
== ARG_EMAIL_ADDRESS
? "emailAddress" :
2311 c
== ARG_LOCATION
? "location" :
2312 c
== ARG_ICON_NAME
? "iconName" :
2313 c
== ARG_CIFS_USER_NAME
? "cifsUserName" :
2314 c
== ARG_CIFS_DOMAIN
? "cifsDomain" :
2315 c
== ARG_CIFS_SERVICE
? "cifsService" :
2320 if (isempty(optarg
)) {
2321 r
= drop_from_identity(field
);
2328 r
= json_variant_set_field_string(&arg_identity_extra
, field
, optarg
);
2330 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2335 case ARG_PASSWORD_HINT
:
2336 if (isempty(optarg
)) {
2337 r
= drop_from_identity("passwordHint");
2344 r
= json_variant_set_field_string(&arg_identity_extra_privileged
, "passwordHint", optarg
);
2346 return log_error_errno(r
, "Failed to set passwordHint field: %m");
2348 string_erase(optarg
);
2354 if (isempty(optarg
)) {
2355 r
= drop_from_identity("niceLevel");
2361 r
= parse_nice(optarg
, &nc
);
2363 return log_error_errno(r
, "Failed to parse nice level: %s", optarg
);
2365 r
= json_variant_set_field_integer(&arg_identity_extra
, "niceLevel", nc
);
2367 return log_error_errno(r
, "Failed to set niceLevel field: %m");
2373 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
, *jcur
= NULL
, *jmax
= NULL
;
2374 _cleanup_free_
char *field
= NULL
, *t
= NULL
;
2379 if (isempty(optarg
)) {
2380 /* Remove all resource limits */
2382 r
= drop_from_identity("resourceLimits");
2386 arg_identity_filter_rlimits
= strv_free(arg_identity_filter_rlimits
);
2387 arg_identity_extra_rlimits
= json_variant_unref(arg_identity_extra_rlimits
);
2391 eq
= strchr(optarg
, '=');
2393 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Can't parse resource limit assignment: %s", optarg
);
2395 field
= strndup(optarg
, eq
- optarg
);
2399 l
= rlimit_from_string_harder(field
);
2401 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Unknown resource limit type: %s", field
);
2403 if (isempty(eq
+ 1)) {
2404 /* Remove only the specific rlimit */
2406 r
= strv_extend(&arg_identity_filter_rlimits
, rlimit_to_string(l
));
2410 r
= json_variant_filter(&arg_identity_extra_rlimits
, STRV_MAKE(field
));
2412 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
2417 r
= rlimit_parse(l
, eq
+ 1, &rl
);
2419 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to parse resource limit value: %s", eq
+ 1);
2421 r
= rl
.rlim_cur
== RLIM_INFINITY
? json_variant_new_null(&jcur
) : json_variant_new_unsigned(&jcur
, rl
.rlim_cur
);
2423 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to allocate current integer: %m");
2425 r
= rl
.rlim_max
== RLIM_INFINITY
? json_variant_new_null(&jmax
) : json_variant_new_unsigned(&jmax
, rl
.rlim_max
);
2427 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to allocate maximum integer: %m");
2431 JSON_BUILD_PAIR("cur", JSON_BUILD_VARIANT(jcur
)),
2432 JSON_BUILD_PAIR("max", JSON_BUILD_VARIANT(jmax
))));
2434 return log_error_errno(r
, "Failed to build resource limit: %m");
2436 t
= strjoin("RLIMIT_", rlimit_to_string(l
));
2440 r
= json_variant_set_field(&arg_identity_extra_rlimits
, t
, v
);
2442 return log_error_errno(r
, "Failed to set %s field: %m", rlimit_to_string(l
));
2450 if (isempty(optarg
)) {
2451 r
= drop_from_identity("uid");
2458 r
= parse_uid(optarg
, &uid
);
2460 return log_error_errno(r
, "Failed to parse UID '%s'.", optarg
);
2462 if (uid_is_system(uid
))
2463 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "UID " UID_FMT
" is in system range, refusing.", uid
);
2464 if (uid_is_dynamic(uid
))
2465 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "UID " UID_FMT
" is in dynamic range, refusing.", uid
);
2466 if (uid
== UID_NOBODY
)
2467 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "UID " UID_FMT
" is nobody UID, refusing.", uid
);
2469 r
= json_variant_set_field_unsigned(&arg_identity_extra
, "uid", uid
);
2471 return log_error_errno(r
, "Failed to set realm field: %m");
2477 case ARG_IMAGE_PATH
: {
2478 const char *field
= c
== 'k' ? "skeletonDirectory" : "imagePath";
2479 _cleanup_free_
char *v
= NULL
;
2481 if (isempty(optarg
)) {
2482 r
= drop_from_identity(field
);
2489 r
= parse_path_argument_and_warn(optarg
, false, &v
);
2493 r
= json_variant_set_field_string(&arg_identity_extra_this_machine
, field
, v
);
2495 return log_error_errno(r
, "Failed to set %s field: %m", v
);
2501 if (isempty(optarg
)) {
2502 r
= drop_from_identity("shell");
2509 if (!valid_shell(optarg
))
2510 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Shell '%s' not valid.", optarg
);
2512 r
= json_variant_set_field_string(&arg_identity_extra
, "shell", optarg
);
2514 return log_error_errno(r
, "Failed to set shell field: %m");
2519 _cleanup_free_
char **l
= NULL
, **k
= NULL
;
2520 _cleanup_(json_variant_unrefp
) JsonVariant
*ne
= NULL
;
2523 if (isempty(optarg
)) {
2524 r
= drop_from_identity("environment");
2531 if (!env_assignment_is_valid(optarg
))
2532 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Environment assignment '%s' not valid.", optarg
);
2534 e
= json_variant_by_key(arg_identity_extra
, "environment");
2536 r
= json_variant_strv(e
, &l
);
2538 return log_error_errno(r
, "Failed to parse JSON environment field: %m");
2541 k
= strv_env_set(l
, optarg
);
2547 r
= json_variant_new_array_strv(&ne
, k
);
2549 return log_error_errno(r
, "Failed to allocate environment list JSON: %m");
2551 r
= json_variant_set_field(&arg_identity_extra
, "environment", ne
);
2553 return log_error_errno(r
, "Failed to set environment list: %m");
2560 if (isempty(optarg
)) {
2561 r
= drop_from_identity("timeZone");
2568 if (!timezone_is_valid(optarg
, LOG_DEBUG
))
2569 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Timezone '%s' is not valid.", optarg
);
2571 r
= json_variant_set_field_string(&arg_identity_extra
, "timeZone", optarg
);
2573 return log_error_errno(r
, "Failed to set timezone field: %m");
2578 if (isempty(optarg
)) {
2579 r
= drop_from_identity("language");
2586 if (!locale_is_valid(optarg
))
2587 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Locale '%s' is not valid.", optarg
);
2589 if (locale_is_installed(optarg
) <= 0)
2590 log_warning("Locale '%s' is not installed, accepting anyway.", optarg
);
2592 r
= json_variant_set_field_string(&arg_identity_extra
, "preferredLanguage", optarg
);
2594 return log_error_errno(r
, "Failed to set preferredLanguage field: %m");
2602 case ARG_KILL_PROCESSES
:
2603 case ARG_ENFORCE_PASSWORD_POLICY
:
2604 case ARG_AUTO_LOGIN
:
2605 case ARG_PASSWORD_CHANGE_NOW
: {
2607 c
== ARG_LOCKED
? "locked" :
2608 c
== ARG_NOSUID
? "mountNoSuid" :
2609 c
== ARG_NODEV
? "mountNoDevices" :
2610 c
== ARG_NOEXEC
? "mountNoExecute" :
2611 c
== ARG_KILL_PROCESSES
? "killProcesses" :
2612 c
== ARG_ENFORCE_PASSWORD_POLICY
? "enforcePasswordPolicy" :
2613 c
== ARG_AUTO_LOGIN
? "autoLogin" :
2614 c
== ARG_PASSWORD_CHANGE_NOW
? "passwordChangeNow" :
2619 if (isempty(optarg
)) {
2620 r
= drop_from_identity(field
);
2627 r
= parse_boolean(optarg
);
2629 return log_error_errno(r
, "Failed to parse %s boolean: %m", field
);
2631 r
= json_variant_set_field_boolean(&arg_identity_extra
, field
, r
> 0);
2633 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2639 r
= json_variant_set_field_boolean(&arg_identity_extra
, "enforcePasswordPolicy", false);
2641 return log_error_errno(r
, "Failed to set enforcePasswordPolicy field: %m");
2646 if (isempty(optarg
)) {
2647 r
= drop_from_identity("diskSize");
2651 r
= drop_from_identity("diskSizeRelative");
2655 arg_disk_size
= arg_disk_size_relative
= UINT64_MAX
;
2659 r
= parse_permille(optarg
);
2661 r
= parse_size(optarg
, 1024, &arg_disk_size
);
2663 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Disk size '%s' not valid.", optarg
);
2665 r
= drop_from_identity("diskSizeRelative");
2669 r
= json_variant_set_field_unsigned(&arg_identity_extra_this_machine
, "diskSize", arg_disk_size
);
2671 return log_error_errno(r
, "Failed to set diskSize field: %m");
2673 arg_disk_size_relative
= UINT64_MAX
;
2675 /* Normalize to UINT32_MAX == 100% */
2676 arg_disk_size_relative
= (uint64_t) r
* UINT32_MAX
/ 1000U;
2678 r
= drop_from_identity("diskSize");
2682 r
= json_variant_set_field_unsigned(&arg_identity_extra_this_machine
, "diskSizeRelative", arg_disk_size_relative
);
2684 return log_error_errno(r
, "Failed to set diskSizeRelative field: %m");
2686 arg_disk_size
= UINT64_MAX
;
2691 case ARG_ACCESS_MODE
: {
2694 if (isempty(optarg
)) {
2695 r
= drop_from_identity("accessMode");
2702 r
= parse_mode(optarg
, &mode
);
2704 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Access mode '%s' not valid.", optarg
);
2706 r
= json_variant_set_field_unsigned(&arg_identity_extra
, "accessMode", mode
);
2708 return log_error_errno(r
, "Failed to set access mode field: %m");
2713 case ARG_LUKS_DISCARD
:
2714 if (isempty(optarg
)) {
2715 r
= drop_from_identity("luksDiscard");
2722 r
= parse_boolean(optarg
);
2724 return log_error_errno(r
, "Failed to parse --luks-discard= parameter: %s", optarg
);
2726 r
= json_variant_set_field_boolean(&arg_identity_extra
, "luksDiscard", r
);
2728 return log_error_errno(r
, "Failed to set discard field: %m");
2732 case ARG_LUKS_OFFLINE_DISCARD
:
2733 if (isempty(optarg
)) {
2734 r
= drop_from_identity("luksOfflineDiscard");
2741 r
= parse_boolean(optarg
);
2743 return log_error_errno(r
, "Failed to parse --luks-offline-discard= parameter: %s", optarg
);
2745 r
= json_variant_set_field_boolean(&arg_identity_extra
, "luksOfflineDiscard", r
);
2747 return log_error_errno(r
, "Failed to set offline discard field: %m");
2751 case ARG_LUKS_VOLUME_KEY_SIZE
:
2752 case ARG_LUKS_PBKDF_PARALLEL_THREADS
:
2753 case ARG_RATE_LIMIT_BURST
: {
2755 c
== ARG_LUKS_VOLUME_KEY_SIZE
? "luksVolumeKeySize" :
2756 c
== ARG_LUKS_PBKDF_PARALLEL_THREADS
? "luksPbkdfParallelThreads" :
2757 c
== ARG_RATE_LIMIT_BURST
? "rateLimitBurst" : NULL
;
2762 if (isempty(optarg
)) {
2763 r
= drop_from_identity(field
);
2768 r
= safe_atou(optarg
, &n
);
2770 return log_error_errno(r
, "Failed to parse %s parameter: %s", field
, optarg
);
2772 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, n
);
2774 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2782 if (isempty(optarg
)) {
2783 r
= drop_from_identity("umask");
2790 r
= parse_mode(optarg
, &m
);
2792 return log_error_errno(r
, "Failed to parse umask: %m");
2794 r
= json_variant_set_field_integer(&arg_identity_extra
, "umask", m
);
2796 return log_error_errno(r
, "Failed to set umask field: %m");
2801 case ARG_SSH_AUTHORIZED_KEYS
: {
2802 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
2803 _cleanup_(strv_freep
) char **l
= NULL
, **add
= NULL
;
2805 if (isempty(optarg
)) {
2806 r
= drop_from_identity("sshAuthorizedKeys");
2813 if (optarg
[0] == '@') {
2814 _cleanup_fclose_
FILE *f
= NULL
;
2816 /* If prefixed with '@' read from a file */
2818 f
= fopen(optarg
+1, "re");
2820 return log_error_errno(errno
, "Failed to open '%s': %m", optarg
+1);
2823 _cleanup_free_
char *line
= NULL
;
2825 r
= read_line(f
, LONG_LINE_MAX
, &line
);
2827 return log_error_errno(r
, "Failed to read from '%s': %m", optarg
+1);
2837 r
= strv_consume(&add
, TAKE_PTR(line
));
2842 /* Otherwise, assume it's a literal key. Let's do some superficial checks
2843 * before accept it though. */
2845 if (string_has_cc(optarg
, NULL
))
2846 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Authorized key contains control characters, refusing.");
2847 if (optarg
[0] == '#')
2848 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Specified key is a comment?");
2850 add
= strv_new(optarg
);
2855 v
= json_variant_ref(json_variant_by_key(arg_identity_extra_privileged
, "sshAuthorizedKeys"));
2857 r
= json_variant_strv(v
, &l
);
2859 return log_error_errno(r
, "Failed to parse SSH authorized keys list: %m");
2862 r
= strv_extend_strv(&l
, add
, true);
2866 v
= json_variant_unref(v
);
2868 r
= json_variant_new_array_strv(&v
, l
);
2872 r
= json_variant_set_field(&arg_identity_extra_privileged
, "sshAuthorizedKeys", v
);
2874 return log_error_errno(r
, "Failed to set authorized keys: %m");
2879 case ARG_NOT_BEFORE
:
2885 field
= c
== ARG_NOT_BEFORE
? "notBeforeUSec" :
2886 IN_SET(c
, ARG_NOT_AFTER
, 'e') ? "notAfterUSec" : NULL
;
2890 if (isempty(optarg
)) {
2891 r
= drop_from_identity(field
);
2898 /* Note the minor discrepancy regarding -e parsing here: we support that for compat
2899 * reasons, and in the original useradd(8) implementation it accepts dates in the
2900 * format YYYY-MM-DD. Coincidentally, we accept dates formatted like that too, but
2901 * with greater precision. */
2902 r
= parse_timestamp(optarg
, &n
);
2904 return log_error_errno(r
, "Failed to parse %s parameter: %m", field
);
2906 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, n
);
2908 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2912 case ARG_PASSWORD_CHANGE_MIN
:
2913 case ARG_PASSWORD_CHANGE_MAX
:
2914 case ARG_PASSWORD_CHANGE_WARN
:
2915 case ARG_PASSWORD_CHANGE_INACTIVE
: {
2919 field
= c
== ARG_PASSWORD_CHANGE_MIN
? "passwordChangeMinUSec" :
2920 c
== ARG_PASSWORD_CHANGE_MAX
? "passwordChangeMaxUSec" :
2921 c
== ARG_PASSWORD_CHANGE_WARN
? "passwordChangeWarnUSec" :
2922 c
== ARG_PASSWORD_CHANGE_INACTIVE
? "passwordChangeInactiveUSec" :
2927 if (isempty(optarg
)) {
2928 r
= drop_from_identity(field
);
2935 r
= parse_sec(optarg
, &n
);
2937 return log_error_errno(r
, "Failed to parse %s parameter: %m", field
);
2939 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, n
);
2941 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2947 case ARG_LUKS_CIPHER
:
2948 case ARG_LUKS_CIPHER_MODE
:
2949 case ARG_LUKS_PBKDF_TYPE
:
2950 case ARG_LUKS_PBKDF_HASH_ALGORITHM
: {
2953 c
== ARG_STORAGE
? "storage" :
2954 c
== ARG_FS_TYPE
? "fileSystemType" :
2955 c
== ARG_LUKS_CIPHER
? "luksCipher" :
2956 c
== ARG_LUKS_CIPHER_MODE
? "luksCipherMode" :
2957 c
== ARG_LUKS_PBKDF_TYPE
? "luksPbkdfType" :
2958 c
== ARG_LUKS_PBKDF_HASH_ALGORITHM
? "luksPbkdfHashAlgorithm" : NULL
;
2962 if (isempty(optarg
)) {
2963 r
= drop_from_identity(field
);
2970 if (!string_is_safe(optarg
))
2971 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Parameter for %s field not valid: %s", field
, optarg
);
2973 r
= json_variant_set_field_string(
2974 IN_SET(c
, ARG_STORAGE
, ARG_FS_TYPE
) ?
2975 &arg_identity_extra_this_machine
:
2976 &arg_identity_extra
, field
, optarg
);
2978 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2983 case ARG_LUKS_PBKDF_TIME_COST
:
2984 case ARG_RATE_LIMIT_INTERVAL
:
2985 case ARG_STOP_DELAY
: {
2987 c
== ARG_LUKS_PBKDF_TIME_COST
? "luksPbkdfTimeCostUSec" :
2988 c
== ARG_RATE_LIMIT_INTERVAL
? "rateLimitIntervalUSec" :
2989 c
== ARG_STOP_DELAY
? "stopDelayUSec" :
2995 if (isempty(optarg
)) {
2996 r
= drop_from_identity(field
);
3003 r
= parse_sec(optarg
, &t
);
3005 return log_error_errno(r
, "Failed to parse %s field: %s", field
, optarg
);
3007 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, t
);
3009 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3015 const char *p
= optarg
;
3018 r
= drop_from_identity("memberOf");
3026 _cleanup_(json_variant_unrefp
) JsonVariant
*mo
= NULL
;
3027 _cleanup_strv_free_
char **list
= NULL
;
3028 _cleanup_free_
char *word
= NULL
;
3030 r
= extract_first_word(&p
, &word
, ",", 0);
3032 return log_error_errno(r
, "Failed to parse group list: %m");
3036 if (!valid_user_group_name(word
, 0))
3037 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Invalid group name %s.", word
);
3039 mo
= json_variant_ref(json_variant_by_key(arg_identity_extra
, "memberOf"));
3041 r
= json_variant_strv(mo
, &list
);
3043 return log_error_errno(r
, "Failed to parse group list: %m");
3045 r
= strv_extend(&list
, word
);
3052 mo
= json_variant_unref(mo
);
3053 r
= json_variant_new_array_strv(&mo
, list
);
3055 return log_error_errno(r
, "Failed to create group list JSON: %m");
3057 r
= json_variant_set_field(&arg_identity_extra
, "memberOf", mo
);
3059 return log_error_errno(r
, "Failed to update group list: %m");
3065 case ARG_TASKS_MAX
: {
3068 if (isempty(optarg
)) {
3069 r
= drop_from_identity("tasksMax");
3075 r
= safe_atou64(optarg
, &u
);
3077 return log_error_errno(r
, "Failed to parse --tasks-max= parameter: %s", optarg
);
3079 r
= json_variant_set_field_unsigned(&arg_identity_extra
, "tasksMax", u
);
3081 return log_error_errno(r
, "Failed to set tasksMax field: %m");
3086 case ARG_MEMORY_MAX
:
3087 case ARG_MEMORY_HIGH
:
3088 case ARG_LUKS_PBKDF_MEMORY_COST
: {
3090 c
== ARG_MEMORY_MAX
? "memoryMax" :
3091 c
== ARG_MEMORY_HIGH
? "memoryHigh" :
3092 c
== ARG_LUKS_PBKDF_MEMORY_COST
? "luksPbkdfMemoryCost" : NULL
;
3098 if (isempty(optarg
)) {
3099 r
= drop_from_identity(field
);
3105 r
= parse_size(optarg
, 1024, &u
);
3107 return log_error_errno(r
, "Failed to parse %s parameter: %s", field
, optarg
);
3109 r
= json_variant_set_field_unsigned(&arg_identity_extra_this_machine
, field
, u
);
3111 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3116 case ARG_CPU_WEIGHT
:
3117 case ARG_IO_WEIGHT
: {
3118 const char *field
= c
== ARG_CPU_WEIGHT
? "cpuWeight" :
3119 c
== ARG_IO_WEIGHT
? "ioWeight" : NULL
;
3124 if (isempty(optarg
)) {
3125 r
= drop_from_identity(field
);
3131 r
= safe_atou64(optarg
, &u
);
3133 return log_error_errno(r
, "Failed to parse --cpu-weight=/--io-weight= parameter: %s", optarg
);
3135 if (!CGROUP_WEIGHT_IS_OK(u
))
3136 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Weight %" PRIu64
" is out of valid weight range.", u
);
3138 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, u
);
3140 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3145 case ARG_PKCS11_TOKEN_URI
: {
3148 if (streq(optarg
, "list"))
3149 return list_pkcs11_tokens();
3151 /* If --pkcs11-token-uri= is specified we always drop everything old */
3152 FOREACH_STRING(p
, "pkcs11TokenUri", "pkcs11EncryptedKey") {
3153 r
= drop_from_identity(p
);
3158 if (isempty(optarg
)) {
3159 arg_pkcs11_token_uri
= strv_free(arg_pkcs11_token_uri
);
3163 if (streq(optarg
, "auto")) {
3164 _cleanup_free_
char *found
= NULL
;
3166 r
= find_pkcs11_token_auto(&found
);
3169 r
= strv_consume(&arg_pkcs11_token_uri
, TAKE_PTR(found
));
3171 if (!pkcs11_uri_valid(optarg
))
3172 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Not a valid PKCS#11 URI: %s", optarg
);
3174 r
= strv_extend(&arg_pkcs11_token_uri
, optarg
);
3179 strv_uniq(arg_pkcs11_token_uri
);
3183 case ARG_FIDO2_DEVICE
: {
3186 if (streq(optarg
, "list"))
3187 return list_fido2_devices();
3189 FOREACH_STRING(p
, "fido2HmacCredential", "fido2HmacSalt") {
3190 r
= drop_from_identity(p
);
3195 if (isempty(optarg
)) {
3196 arg_fido2_device
= strv_free(arg_fido2_device
);
3200 if (streq(optarg
, "auto")) {
3201 _cleanup_free_
char *found
= NULL
;
3203 r
= find_fido2_auto(&found
);
3207 r
= strv_consume(&arg_fido2_device
, TAKE_PTR(found
));
3209 r
= strv_extend(&arg_fido2_device
, optarg
);
3214 strv_uniq(arg_fido2_device
);
3218 case ARG_RECOVERY_KEY
: {
3221 r
= parse_boolean(optarg
);
3223 return log_error_errno(r
, "Failed to parse --recovery-key= argument: %s", optarg
);
3225 arg_recovery_key
= r
;
3227 FOREACH_STRING(p
, "recoveryKey", "recoveryKeyType") {
3228 r
= drop_from_identity(p
);
3238 arg_json_format_flags
= JSON_FORMAT_PRETTY_AUTO
|JSON_FORMAT_COLOR_AUTO
;
3242 if (streq(optarg
, "pretty")) {
3244 arg_json_format_flags
= JSON_FORMAT_PRETTY
|JSON_FORMAT_COLOR_AUTO
;
3245 } else if (streq(optarg
, "short")) {
3247 arg_json_format_flags
= JSON_FORMAT_NEWLINE
;
3248 } else if (streq(optarg
, "off")) {
3250 arg_json_format_flags
= 0;
3251 } else if (streq(optarg
, "help")) {
3257 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Unknown argument to --json=: %s", optarg
);
3262 if (arg_export_format
== EXPORT_FORMAT_FULL
)
3263 arg_export_format
= EXPORT_FORMAT_STRIPPED
;
3264 else if (arg_export_format
== EXPORT_FORMAT_STRIPPED
)
3265 arg_export_format
= EXPORT_FORMAT_MINIMAL
;
3267 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Specifying -E more than twice is not supported.");
3270 if (arg_json_format_flags
== 0)
3271 arg_json_format_flags
= JSON_FORMAT_PRETTY_AUTO
|JSON_FORMAT_COLOR_AUTO
;
3274 case ARG_EXPORT_FORMAT
:
3275 if (streq(optarg
, "full"))
3276 arg_export_format
= EXPORT_FORMAT_FULL
;
3277 else if (streq(optarg
, "stripped"))
3278 arg_export_format
= EXPORT_FORMAT_STRIPPED
;
3279 else if (streq(optarg
, "minimal"))
3280 arg_export_format
= EXPORT_FORMAT_MINIMAL
;
3281 else if (streq(optarg
, "help")) {
3290 case ARG_AND_RESIZE
:
3291 arg_and_resize
= true;
3294 case ARG_AND_CHANGE_PASSWORD
:
3295 arg_and_change_password
= true;
3302 assert_not_reached("Unhandled option");
3306 if (!strv_isempty(arg_pkcs11_token_uri
) || !strv_isempty(arg_fido2_device
))
3307 arg_and_change_password
= true;
3309 if (arg_disk_size
!= UINT64_MAX
|| arg_disk_size_relative
!= UINT64_MAX
)
3310 arg_and_resize
= true;
3315 static int redirect_bus_mgr(void) {
3318 /* Talk to a different service if that's requested. (The same env var is also understood by homed, so
3319 * that it is relatively easily possible to invoke a second instance of homed for debug purposes and
3320 * have homectl talk to it, without colliding with the host version. This is handy when operating
3321 * from a homed-managed account.) */
3323 suffix
= getenv("SYSTEMD_HOME_DEBUG_SUFFIX");
3325 static BusLocator locator
= {
3326 .path
= "/org/freedesktop/home1",
3327 .interface
= "org.freedesktop.home1.Manager",
3330 /* Yes, we leak this memory, but there's little point to collect this, given that we only do
3331 * this in a debug environment, do it only once, and the string shall live for out entire
3332 * process runtime. */
3334 locator
.destination
= strjoin("org.freedesktop.home1.", suffix
);
3335 if (!locator
.destination
)
3340 bus_mgr
= bus_home_mgr
;
3345 static int run(int argc
, char *argv
[]) {
3346 static const Verb verbs
[] = {
3347 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
3348 { "list", VERB_ANY
, 1, VERB_DEFAULT
, list_homes
},
3349 { "activate", 2, VERB_ANY
, 0, activate_home
},
3350 { "deactivate", 2, VERB_ANY
, 0, deactivate_home
},
3351 { "inspect", VERB_ANY
, VERB_ANY
, 0, inspect_home
},
3352 { "authenticate", VERB_ANY
, VERB_ANY
, 0, authenticate_home
},
3353 { "create", VERB_ANY
, 2, 0, create_home
},
3354 { "remove", 2, VERB_ANY
, 0, remove_home
},
3355 { "update", VERB_ANY
, 2, 0, update_home
},
3356 { "passwd", VERB_ANY
, 2, 0, passwd_home
},
3357 { "resize", 2, 3, 0, resize_home
},
3358 { "lock", 2, VERB_ANY
, 0, lock_home
},
3359 { "unlock", 2, VERB_ANY
, 0, unlock_home
},
3360 { "with", 2, VERB_ANY
, 0, with_home
},
3361 { "lock-all", VERB_ANY
, 1, 0, lock_all_homes
},
3362 { "deactivate-all", VERB_ANY
, 1, 0, deactivate_all_homes
},
3370 r
= redirect_bus_mgr();
3374 r
= parse_argv(argc
, argv
);
3378 return dispatch_verb(argc
, argv
, verbs
, NULL
);
3381 DEFINE_MAIN_FUNCTION(run
);