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-util.h"
27 #include "path-util.h"
28 #include "pkcs11-util.h"
29 #include "pretty-print.h"
30 #include "process-util.h"
31 #include "pwquality-util.h"
32 #include "rlimit-util.h"
33 #include "spawn-polkit-agent.h"
34 #include "terminal-util.h"
35 #include "user-record-pwquality.h"
36 #include "user-record-show.h"
37 #include "user-record-util.h"
38 #include "user-record.h"
39 #include "user-util.h"
42 static PagerFlags arg_pager_flags
= 0;
43 static bool arg_legend
= true;
44 static bool arg_ask_password
= true;
45 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
46 static const char *arg_host
= NULL
;
47 static const char *arg_identity
= NULL
;
48 static JsonVariant
*arg_identity_extra
= NULL
;
49 static JsonVariant
*arg_identity_extra_privileged
= NULL
;
50 static JsonVariant
*arg_identity_extra_this_machine
= NULL
;
51 static JsonVariant
*arg_identity_extra_rlimits
= NULL
;
52 static char **arg_identity_filter
= NULL
; /* this one is also applied to 'privileged' and 'thisMachine' subobjects */
53 static char **arg_identity_filter_rlimits
= NULL
;
54 static uint64_t arg_disk_size
= UINT64_MAX
;
55 static uint64_t arg_disk_size_relative
= UINT64_MAX
;
56 static char **arg_pkcs11_token_uri
= NULL
;
57 static char **arg_fido2_device
= NULL
;
58 static bool arg_recovery_key
= false;
59 static JsonFormatFlags arg_json_format_flags
= JSON_FORMAT_OFF
;
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 r
= acquire_bus(&bus
);
120 r
= bus_call_method(bus
, bus_mgr
, "ListHomes", &error
, &reply
, NULL
);
122 return log_error_errno(r
, "Failed to list homes: %s", bus_error_message(&error
, r
));
124 table
= table_new("name", "uid", "gid", "state", "realname", "home", "shell");
128 r
= sd_bus_message_enter_container(reply
, 'a', "(susussso)");
130 return bus_log_parse_error(r
);
133 const char *name
, *state
, *realname
, *home
, *shell
, *color
;
137 r
= sd_bus_message_read(reply
, "(susussso)", &name
, &uid
, &state
, &gid
, &realname
, &home
, &shell
, NULL
);
139 return bus_log_parse_error(r
);
143 r
= table_add_many(table
,
148 return table_log_add_error(r
);
151 r
= table_add_cell(table
, &cell
, TABLE_STRING
, state
);
153 return table_log_add_error(r
);
155 color
= user_record_state_color(state
);
157 (void) table_set_color(table
, cell
, color
);
159 r
= table_add_many(table
,
160 TABLE_STRING
, strna(empty_to_null(realname
)),
162 TABLE_STRING
, strna(empty_to_null(shell
)));
164 return table_log_add_error(r
);
167 r
= sd_bus_message_exit_container(reply
);
169 return bus_log_parse_error(r
);
171 if (table_get_rows(table
) > 1 || !FLAGS_SET(arg_json_format_flags
, JSON_FORMAT_OFF
)) {
172 r
= table_set_sort(table
, (size_t) 0, (size_t) -1);
174 return table_log_sort_error(r
);
176 r
= table_print_with_pager(table
, arg_json_format_flags
, arg_pager_flags
, arg_legend
);
181 if (arg_legend
&& (arg_json_format_flags
& JSON_FORMAT_OFF
)) {
182 if (table_get_rows(table
) > 1)
183 printf("\n%zu home areas listed.\n", table_get_rows(table
) - 1);
185 printf("No home areas.\n");
191 static int acquire_existing_password(const char *user_name
, UserRecord
*hr
, bool emphasize_current
) {
192 _cleanup_(strv_free_erasep
) char **password
= NULL
;
193 _cleanup_free_
char *question
= NULL
;
200 e
= getenv("PASSWORD");
202 /* People really shouldn't use environment variables for passing passwords. We support this
203 * only for testing purposes, and do not document the behaviour, so that people won't
204 * actually use this outside of testing. */
206 r
= user_record_set_password(hr
, STRV_MAKE(e
), true);
208 return log_error_errno(r
, "Failed to store password: %m");
211 assert_se(unsetenv("PASSWORD") == 0);
216 if (asprintf(&question
, emphasize_current
?
217 "Please enter current password for user %s:" :
218 "Please enter password for user %s:",
222 r
= ask_password_auto(question
, "user-home", NULL
, "home-password", USEC_INFINITY
, ASK_PASSWORD_ACCEPT_CACHED
|ASK_PASSWORD_PUSH_CACHE
, &password
);
224 return log_error_errno(r
, "Failed to acquire password: %m");
226 r
= user_record_set_password(hr
, password
, true);
228 return log_error_errno(r
, "Failed to store password: %m");
233 static int acquire_token_pin(const char *user_name
, UserRecord
*hr
) {
234 _cleanup_(strv_free_erasep
) char **pin
= NULL
;
235 _cleanup_free_
char *question
= NULL
;
244 r
= user_record_set_token_pin(hr
, STRV_MAKE(e
), false);
246 return log_error_errno(r
, "Failed to store token PIN: %m");
249 assert_se(unsetenv("PIN") == 0);
254 if (asprintf(&question
, "Please enter security token PIN for user %s:", user_name
) < 0)
257 /* We never cache or use cached PINs, since usually there are only very few attempts allowed before the PIN is blocked */
258 r
= ask_password_auto(question
, "user-home", NULL
, "token-pin", USEC_INFINITY
, 0, &pin
);
260 return log_error_errno(r
, "Failed to acquire security token PIN: %m");
262 r
= user_record_set_token_pin(hr
, pin
, false);
264 return log_error_errno(r
, "Failed to store security token PIN: %m");
269 static int handle_generic_user_record_error(
270 const char *user_name
,
272 const sd_bus_error
*error
,
274 bool emphasize_current_password
) {
280 if (sd_bus_error_has_name(error
, BUS_ERROR_HOME_ABSENT
))
281 return log_error_errno(SYNTHETIC_ERRNO(EREMOTE
),
282 "Home of user %s is currently absent, please plug in the necessary storage device or backing file system.", user_name
);
284 else if (sd_bus_error_has_name(error
, BUS_ERROR_AUTHENTICATION_LIMIT_HIT
))
285 return log_error_errno(SYNTHETIC_ERRNO(ETOOMANYREFS
),
286 "Too frequent unsuccessful login attempts for user %s, try again later.", user_name
);
288 else if (sd_bus_error_has_name(error
, BUS_ERROR_BAD_PASSWORD
)) {
290 if (!strv_isempty(hr
->password
))
291 log_notice("Password incorrect or not sufficient, please try again.");
293 r
= acquire_existing_password(user_name
, hr
, emphasize_current_password
);
297 } else if (sd_bus_error_has_name(error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
)) {
299 if (strv_isempty(hr
->password
))
300 log_notice("Security token not inserted, please enter password.");
302 log_notice("Password incorrect or not sufficient, and configured security token not inserted, please try again.");
304 r
= acquire_existing_password(user_name
, hr
, emphasize_current_password
);
308 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_PIN_NEEDED
)) {
310 r
= acquire_token_pin(user_name
, hr
);
314 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_PROTECTED_AUTHENTICATION_PATH_NEEDED
)) {
316 log_notice("%s%sPlease authenticate physically on security token.",
317 emoji_enabled() ? special_glyph(SPECIAL_GLYPH_TOUCH
) : "",
318 emoji_enabled() ? " " : "");
320 r
= user_record_set_pkcs11_protected_authentication_path_permitted(hr
, true);
322 return log_error_errno(r
, "Failed to set PKCS#11 protected authentication path permitted flag: %m");
324 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_USER_PRESENCE_NEEDED
)) {
326 log_notice("%s%sAuthentication requires presence verification on security token.",
327 emoji_enabled() ? special_glyph(SPECIAL_GLYPH_TOUCH
) : "",
328 emoji_enabled() ? " " : "");
330 r
= user_record_set_fido2_user_presence_permitted(hr
, true);
332 return log_error_errno(r
, "Failed to set FIDO2 user presence permitted flag: %m");
334 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_PIN_LOCKED
))
335 return log_error_errno(SYNTHETIC_ERRNO(EPERM
), "Security token PIN is locked, please unlock it first. (Hint: Removal and re-insertion might suffice.)");
337 else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_BAD_PIN
)) {
339 log_notice("Security token PIN incorrect, please try again.");
341 r
= acquire_token_pin(user_name
, hr
);
345 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_BAD_PIN_FEW_TRIES_LEFT
)) {
347 log_notice("Security token PIN incorrect, please try again (only a few tries left!).");
349 r
= acquire_token_pin(user_name
, hr
);
353 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_BAD_PIN_ONE_TRY_LEFT
)) {
355 log_notice("Security token PIN incorrect, please try again (only one try left!).");
357 r
= acquire_token_pin(user_name
, hr
);
361 return log_error_errno(ret
, "Operation on home %s failed: %s", user_name
, bus_error_message(error
, ret
));
366 static int activate_home(int argc
, char *argv
[], void *userdata
) {
367 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
371 r
= acquire_bus(&bus
);
375 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
376 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
378 secret
= user_record_new();
383 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
384 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
386 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ActivateHome");
388 return bus_log_create_error(r
);
390 r
= sd_bus_message_append(m
, "s", *i
);
392 return bus_log_create_error(r
);
394 r
= bus_message_append_secret(m
, secret
);
396 return bus_log_create_error(r
);
398 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
400 r
= handle_generic_user_record_error(*i
, secret
, &error
, r
, false);
415 static int deactivate_home(int argc
, char *argv
[], void *userdata
) {
416 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
420 r
= acquire_bus(&bus
);
424 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
425 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
426 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
428 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "DeactivateHome");
430 return bus_log_create_error(r
);
432 r
= sd_bus_message_append(m
, "s", *i
);
434 return bus_log_create_error(r
);
436 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
438 log_error_errno(r
, "Failed to deactivate user home: %s", bus_error_message(&error
, r
));
447 static void dump_home_record(UserRecord
*hr
) {
452 if (hr
->incomplete
) {
454 log_warning("Warning: lacking rights to acquire privileged fields of user record of '%s', output incomplete.", hr
->user_name
);
457 if (arg_json_format_flags
& JSON_FORMAT_OFF
)
458 user_record_show(hr
, true);
460 _cleanup_(user_record_unrefp
) UserRecord
*stripped
= NULL
;
462 if (arg_export_format
== EXPORT_FORMAT_STRIPPED
)
463 r
= user_record_clone(hr
, USER_RECORD_EXTRACT_EMBEDDED
, &stripped
);
464 else if (arg_export_format
== EXPORT_FORMAT_MINIMAL
)
465 r
= user_record_clone(hr
, USER_RECORD_EXTRACT_SIGNABLE
, &stripped
);
469 log_warning_errno(r
, "Failed to strip user record, ignoring: %m");
473 json_variant_dump(hr
->json
, arg_json_format_flags
, stdout
, NULL
);
477 static char **mangle_user_list(char **list
, char ***ret_allocated
) {
478 _cleanup_free_
char *myself
= NULL
;
481 if (!strv_isempty(list
)) {
482 *ret_allocated
= NULL
;
486 myself
= getusername_malloc();
494 l
[0] = TAKE_PTR(myself
);
501 static int inspect_home(int argc
, char *argv
[], void *userdata
) {
502 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
503 _cleanup_(strv_freep
) char **mangled_list
= NULL
;
507 (void) pager_open(arg_pager_flags
);
509 r
= acquire_bus(&bus
);
513 items
= mangle_user_list(strv_skip(argv
, 1), &mangled_list
);
517 STRV_FOREACH(i
, items
) {
518 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
519 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
520 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
521 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
526 r
= parse_uid(*i
, &uid
);
528 if (!valid_user_group_name(*i
, 0)) {
529 log_error("Invalid user name '%s'.", *i
);
536 r
= bus_call_method(bus
, bus_mgr
, "GetUserRecordByName", &error
, &reply
, "s", *i
);
538 r
= bus_call_method(bus
, bus_mgr
, "GetUserRecordByUID", &error
, &reply
, "u", (uint32_t) uid
);
541 log_error_errno(r
, "Failed to inspect home: %s", bus_error_message(&error
, r
));
548 r
= sd_bus_message_read(reply
, "sbo", &json
, &incomplete
, NULL
);
550 bus_log_parse_error(r
);
557 r
= json_parse(json
, JSON_PARSE_SENSITIVE
, &v
, NULL
, NULL
);
559 log_error_errno(r
, "Failed to parse JSON identity: %m");
566 hr
= user_record_new();
570 r
= user_record_load(hr
, v
, USER_RECORD_LOAD_REFUSE_SECRET
|USER_RECORD_LOG
);
578 hr
->incomplete
= incomplete
;
579 dump_home_record(hr
);
585 static int authenticate_home(int argc
, char *argv
[], void *userdata
) {
586 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
587 _cleanup_(strv_freep
) char **mangled_list
= NULL
;
591 items
= mangle_user_list(strv_skip(argv
, 1), &mangled_list
);
595 r
= acquire_bus(&bus
);
599 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
601 STRV_FOREACH(i
, items
) {
602 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
604 secret
= user_record_new();
609 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
610 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
612 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "AuthenticateHome");
614 return bus_log_create_error(r
);
616 r
= sd_bus_message_append(m
, "s", *i
);
618 return bus_log_create_error(r
);
620 r
= bus_message_append_secret(m
, secret
);
622 return bus_log_create_error(r
);
624 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
626 r
= handle_generic_user_record_error(*i
, secret
, &error
, r
, false);
641 static int update_last_change(JsonVariant
**v
, bool with_password
, bool override
) {
648 n
= now(CLOCK_REALTIME
);
650 c
= json_variant_by_key(*v
, "lastChangeUSec");
655 goto update_password
;
657 if (!json_variant_is_unsigned(c
))
658 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastChangeUSec field is not an unsigned integer, refusing.");
660 u
= json_variant_unsigned(c
);
662 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastChangeUSec is from the future, can't update.");
665 r
= json_variant_set_field_unsigned(v
, "lastChangeUSec", n
);
667 return log_error_errno(r
, "Failed to update lastChangeUSec: %m");
673 c
= json_variant_by_key(*v
, "lastPasswordChangeUSec");
680 if (!json_variant_is_unsigned(c
))
681 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastPasswordChangeUSec field is not an unsigned integer, refusing.");
683 u
= json_variant_unsigned(c
);
685 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastPasswordChangeUSec is from the future, can't update.");
688 r
= json_variant_set_field_unsigned(v
, "lastPasswordChangeUSec", n
);
690 return log_error_errno(r
, "Failed to update lastPasswordChangeUSec: %m");
695 static int apply_identity_changes(JsonVariant
**_v
) {
696 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
701 v
= json_variant_ref(*_v
);
703 r
= json_variant_filter(&v
, arg_identity_filter
);
705 return log_error_errno(r
, "Failed to filter identity: %m");
707 r
= json_variant_merge(&v
, arg_identity_extra
);
709 return log_error_errno(r
, "Failed to merge identities: %m");
711 if (arg_identity_extra_this_machine
|| !strv_isempty(arg_identity_filter
)) {
712 _cleanup_(json_variant_unrefp
) JsonVariant
*per_machine
= NULL
, *mmid
= NULL
;
713 char mids
[SD_ID128_STRING_MAX
];
716 r
= sd_id128_get_machine(&mid
);
718 return log_error_errno(r
, "Failed to acquire machine ID: %m");
720 r
= json_variant_new_string(&mmid
, sd_id128_to_string(mid
, mids
));
722 return log_error_errno(r
, "Failed to allocate matchMachineId object: %m");
724 per_machine
= json_variant_ref(json_variant_by_key(v
, "perMachine"));
726 _cleanup_(json_variant_unrefp
) JsonVariant
*npm
= NULL
, *add
= NULL
;
727 _cleanup_free_ JsonVariant
**array
= NULL
;
731 if (!json_variant_is_array(per_machine
))
732 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "perMachine field is not an array, refusing.");
734 array
= new(JsonVariant
*, json_variant_elements(per_machine
) + 1);
738 JSON_VARIANT_ARRAY_FOREACH(z
, per_machine
) {
741 if (!json_variant_is_object(z
))
742 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "perMachine entry is not an object, refusing.");
746 u
= json_variant_by_key(z
, "matchMachineId");
750 if (!json_variant_equal(u
, mmid
))
753 r
= json_variant_merge(&add
, z
);
755 return log_error_errno(r
, "Failed to merge perMachine entry: %m");
760 r
= json_variant_filter(&add
, arg_identity_filter
);
762 return log_error_errno(r
, "Failed to filter perMachine: %m");
764 r
= json_variant_merge(&add
, arg_identity_extra_this_machine
);
766 return log_error_errno(r
, "Failed to merge in perMachine fields: %m");
768 if (arg_identity_filter_rlimits
|| arg_identity_extra_rlimits
) {
769 _cleanup_(json_variant_unrefp
) JsonVariant
*rlv
= NULL
;
771 rlv
= json_variant_ref(json_variant_by_key(add
, "resourceLimits"));
773 r
= json_variant_filter(&rlv
, arg_identity_filter_rlimits
);
775 return log_error_errno(r
, "Failed to filter resource limits: %m");
777 r
= json_variant_merge(&rlv
, arg_identity_extra_rlimits
);
779 return log_error_errno(r
, "Failed to set resource limits: %m");
781 if (json_variant_is_blank_object(rlv
)) {
782 r
= json_variant_filter(&add
, STRV_MAKE("resourceLimits"));
784 return log_error_errno(r
, "Failed to drop resource limits field from identity: %m");
786 r
= json_variant_set_field(&add
, "resourceLimits", rlv
);
788 return log_error_errno(r
, "Failed to update resource limits of identity: %m");
792 if (!json_variant_is_blank_object(add
)) {
793 r
= json_variant_set_field(&add
, "matchMachineId", mmid
);
795 return log_error_errno(r
, "Failed to set matchMachineId field: %m");
800 r
= json_variant_new_array(&npm
, array
, i
);
802 return log_error_errno(r
, "Failed to allocate new perMachine array: %m");
804 json_variant_unref(per_machine
);
805 per_machine
= TAKE_PTR(npm
);
807 _cleanup_(json_variant_unrefp
) JsonVariant
*item
= json_variant_ref(arg_identity_extra_this_machine
);
809 if (arg_identity_extra_rlimits
) {
810 r
= json_variant_set_field(&item
, "resourceLimits", arg_identity_extra_rlimits
);
812 return log_error_errno(r
, "Failed to update resource limits of identity: %m");
815 r
= json_variant_set_field(&item
, "matchMachineId", mmid
);
817 return log_error_errno(r
, "Failed to set matchMachineId field: %m");
819 r
= json_variant_append_array(&per_machine
, item
);
821 return log_error_errno(r
, "Failed to append to perMachine array: %m");
824 r
= json_variant_set_field(&v
, "perMachine", per_machine
);
826 return log_error_errno(r
, "Failed to update per machine record: %m");
829 if (arg_identity_extra_privileged
|| arg_identity_filter
) {
830 _cleanup_(json_variant_unrefp
) JsonVariant
*privileged
= NULL
;
832 privileged
= json_variant_ref(json_variant_by_key(v
, "privileged"));
834 r
= json_variant_filter(&privileged
, arg_identity_filter
);
836 return log_error_errno(r
, "Failed to filter identity (privileged part): %m");
838 r
= json_variant_merge(&privileged
, arg_identity_extra_privileged
);
840 return log_error_errno(r
, "Failed to merge identities (privileged part): %m");
842 if (json_variant_is_blank_object(privileged
)) {
843 r
= json_variant_filter(&v
, STRV_MAKE("privileged"));
845 return log_error_errno(r
, "Failed to drop privileged part from identity: %m");
847 r
= json_variant_set_field(&v
, "privileged", privileged
);
849 return log_error_errno(r
, "Failed to update privileged part of identity: %m");
853 if (arg_identity_filter_rlimits
) {
854 _cleanup_(json_variant_unrefp
) JsonVariant
*rlv
= NULL
;
856 rlv
= json_variant_ref(json_variant_by_key(v
, "resourceLimits"));
858 r
= json_variant_filter(&rlv
, arg_identity_filter_rlimits
);
860 return log_error_errno(r
, "Failed to filter resource limits: %m");
862 /* Note that we only filter resource limits here, but don't apply them. We do that in the perMachine section */
864 if (json_variant_is_blank_object(rlv
)) {
865 r
= json_variant_filter(&v
, STRV_MAKE("resourceLimits"));
867 return log_error_errno(r
, "Failed to drop resource limits field from identity: %m");
869 r
= json_variant_set_field(&v
, "resourceLimits", rlv
);
871 return log_error_errno(r
, "Failed to update resource limits of identity: %m");
875 json_variant_unref(*_v
);
881 static int add_disposition(JsonVariant
**v
) {
886 if (json_variant_by_key(*v
, "disposition"))
889 /* Set the disposition to regular, if not configured explicitly */
890 r
= json_variant_set_field_string(v
, "disposition", "regular");
892 return log_error_errno(r
, "Failed to set disposition field: %m");
897 static int acquire_new_home_record(UserRecord
**ret
) {
898 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
899 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
906 unsigned line
, column
;
909 streq(arg_identity
, "-") ? stdin
: NULL
,
910 streq(arg_identity
, "-") ? "<stdin>" : arg_identity
, JSON_PARSE_SENSITIVE
, &v
, &line
, &column
);
912 return log_error_errno(r
, "Failed to parse identity at %u:%u: %m", line
, column
);
915 r
= apply_identity_changes(&v
);
919 r
= add_disposition(&v
);
923 STRV_FOREACH(i
, arg_pkcs11_token_uri
) {
924 r
= identity_add_pkcs11_key_data(&v
, *i
);
929 STRV_FOREACH(i
, arg_fido2_device
) {
930 r
= identity_add_fido2_parameters(&v
, *i
);
935 if (arg_recovery_key
) {
936 r
= identity_add_recovery_key(&v
);
941 r
= update_last_change(&v
, true, false);
946 json_variant_dump(v
, JSON_FORMAT_PRETTY
, NULL
, NULL
);
948 hr
= user_record_new();
952 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
);
960 static int acquire_new_password(
961 const char *user_name
,
973 e
= getenv("NEWPASSWORD");
975 _cleanup_(erase_and_freep
) char *copy
= NULL
;
977 /* As above, this is not for use, just for testing */
985 r
= user_record_set_password(hr
, STRV_MAKE(e
), /* prepend = */ true);
987 return log_error_errno(r
, "Failed to store password: %m");
990 assert_se(unsetenv("NEWPASSWORD") == 0);
993 *ret
= TAKE_PTR(copy
);
999 (void) suggest_passwords();
1002 _cleanup_(strv_free_erasep
) char **first
= NULL
, **second
= NULL
;
1003 _cleanup_free_
char *question
= NULL
;
1006 return log_error_errno(SYNTHETIC_ERRNO(ENOKEY
), "Too many attempts, giving up:");
1008 if (asprintf(&question
, "Please enter new password for user %s:", user_name
) < 0)
1011 r
= ask_password_auto(question
, "user-home", NULL
, "home-password", USEC_INFINITY
, 0, &first
);
1013 return log_error_errno(r
, "Failed to acquire password: %m");
1015 question
= mfree(question
);
1016 if (asprintf(&question
, "Please enter new password for user %s (repeat):", user_name
) < 0)
1019 r
= ask_password_auto(question
, "user-home", NULL
, "home-password", USEC_INFINITY
, 0, &second
);
1021 return log_error_errno(r
, "Failed to acquire password: %m");
1023 if (strv_equal(first
, second
)) {
1024 _cleanup_(erase_and_freep
) char *copy
= NULL
;
1027 copy
= strdup(first
[0]);
1032 r
= user_record_set_password(hr
, first
, /* prepend = */ true);
1034 return log_error_errno(r
, "Failed to store password: %m");
1037 *ret
= TAKE_PTR(copy
);
1042 log_error("Password didn't match, try again.");
1046 static int create_home(int argc
, char *argv
[], void *userdata
) {
1047 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1048 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
1051 r
= acquire_bus(&bus
);
1055 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1058 /* If a username was specified, use it */
1060 if (valid_user_group_name(argv
[1], 0))
1061 r
= json_variant_set_field_string(&arg_identity_extra
, "userName", argv
[1]);
1063 _cleanup_free_
char *un
= NULL
, *rr
= NULL
;
1065 /* Before we consider the user name invalid, let's check if we can split it? */
1066 r
= split_user_name_realm(argv
[1], &un
, &rr
);
1068 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User name '%s' is not valid: %m", argv
[1]);
1071 r
= json_variant_set_field_string(&arg_identity_extra
, "realm", rr
);
1073 return log_error_errno(r
, "Failed to set realm field: %m");
1076 r
= json_variant_set_field_string(&arg_identity_extra
, "userName", un
);
1079 return log_error_errno(r
, "Failed to set userName field: %m");
1081 /* If neither a username nor an identity have been specified we cannot operate. */
1083 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User name required.");
1086 r
= acquire_new_home_record(&hr
);
1090 /* If the JSON record carries no plain text password (besides the recovery key), then let's query it
1092 if (strv_length(hr
->password
) <= arg_recovery_key
) {
1094 if (strv_isempty(hr
->hashed_password
)) {
1095 _cleanup_(erase_and_freep
) char *new_password
= NULL
;
1097 /* No regular (i.e. non-PKCS#11) hashed passwords set in the record, let's fix that. */
1098 r
= acquire_new_password(hr
->user_name
, hr
, /* suggest = */ true, &new_password
);
1102 r
= user_record_make_hashed_password(hr
, STRV_MAKE(new_password
), /* extend = */ false);
1104 return log_error_errno(r
, "Failed to hash password: %m");
1106 /* There's a hash password set in the record, acquire the unhashed version of it. */
1107 r
= acquire_existing_password(hr
->user_name
, hr
, /* emphasize_current= */ false);
1113 if (hr
->enforce_password_policy
== 0) {
1114 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1116 /* If password quality enforcement is disabled, let's at least warn client side */
1118 r
= user_record_quality_check_password(hr
, hr
, &error
);
1120 log_warning_errno(r
, "Specified password does not pass quality checks (%s), proceeding anyway.", bus_error_message(&error
, r
));
1124 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1125 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1126 _cleanup_(erase_and_freep
) char *formatted
= NULL
;
1128 r
= json_variant_format(hr
->json
, 0, &formatted
);
1130 return log_error_errno(r
, "Failed to format user record: %m");
1132 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "CreateHome");
1134 return bus_log_create_error(r
);
1136 (void) sd_bus_message_sensitive(m
);
1138 r
= sd_bus_message_append(m
, "s", formatted
);
1140 return bus_log_create_error(r
);
1142 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1144 if (sd_bus_error_has_name(&error
, BUS_ERROR_LOW_PASSWORD_QUALITY
)) {
1145 _cleanup_(erase_and_freep
) char *new_password
= NULL
;
1147 log_error_errno(r
, "%s", bus_error_message(&error
, r
));
1148 log_info("(Use --enforce-password-policy=no to turn off password quality checks for this account.)");
1150 r
= acquire_new_password(hr
->user_name
, hr
, /* suggest = */ false, &new_password
);
1154 r
= user_record_make_hashed_password(hr
, STRV_MAKE(new_password
), /* extend = */ false);
1156 return log_error_errno(r
, "Failed to hash passwords: %m");
1158 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1169 static int remove_home(int argc
, char *argv
[], void *userdata
) {
1170 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1174 r
= acquire_bus(&bus
);
1178 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1180 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
1181 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1182 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1184 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "RemoveHome");
1186 return bus_log_create_error(r
);
1188 r
= sd_bus_message_append(m
, "s", *i
);
1190 return bus_log_create_error(r
);
1192 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1194 log_error_errno(r
, "Failed to remove home: %s", bus_error_message(&error
, r
));
1203 static int acquire_updated_home_record(
1205 const char *username
,
1208 _cleanup_(json_variant_unrefp
) JsonVariant
*json
= NULL
;
1209 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
1216 unsigned line
, column
;
1219 r
= json_parse_file(
1220 streq(arg_identity
, "-") ? stdin
: NULL
,
1221 streq(arg_identity
, "-") ? "<stdin>" : arg_identity
, JSON_PARSE_SENSITIVE
, &json
, &line
, &column
);
1223 return log_error_errno(r
, "Failed to parse identity at %u:%u: %m", line
, column
);
1225 un
= json_variant_by_key(json
, "userName");
1227 if (!json_variant_is_string(un
) || (username
&& !streq(json_variant_string(un
), username
)))
1228 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User name specified on command line and in JSON record do not match.");
1231 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "No username specified.");
1233 r
= json_variant_set_field_string(&arg_identity_extra
, "userName", username
);
1235 return log_error_errno(r
, "Failed to set userName field: %m");
1239 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1240 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1244 if (!identity_properties_specified())
1245 return log_error_errno(SYNTHETIC_ERRNO(EALREADY
), "No field to change specified.");
1247 r
= bus_call_method(bus
, bus_mgr
, "GetUserRecordByName", &error
, &reply
, "s", username
);
1249 return log_error_errno(r
, "Failed to acquire user home record: %s", bus_error_message(&error
, r
));
1251 r
= sd_bus_message_read(reply
, "sbo", &text
, &incomplete
, NULL
);
1253 return bus_log_parse_error(r
);
1256 return log_error_errno(SYNTHETIC_ERRNO(EACCES
), "Lacking rights to acquire user record including privileged metadata, can't update record.");
1258 r
= json_parse(text
, JSON_PARSE_SENSITIVE
, &json
, NULL
, NULL
);
1260 return log_error_errno(r
, "Failed to parse JSON identity: %m");
1262 reply
= sd_bus_message_unref(reply
);
1264 r
= json_variant_filter(&json
, STRV_MAKE("binding", "status", "signature"));
1266 return log_error_errno(r
, "Failed to strip binding and status from record to update: %m");
1269 r
= apply_identity_changes(&json
);
1273 STRV_FOREACH(i
, arg_pkcs11_token_uri
) {
1274 r
= identity_add_pkcs11_key_data(&json
, *i
);
1279 STRV_FOREACH(i
, arg_fido2_device
) {
1280 r
= identity_add_fido2_parameters(&json
, *i
);
1285 /* If the user supplied a full record, then add in lastChange, but do not override. Otherwise always
1287 r
= update_last_change(&json
, arg_pkcs11_token_uri
|| arg_fido2_device
, !arg_identity
);
1292 json_variant_dump(json
, JSON_FORMAT_PRETTY
, NULL
, NULL
);
1294 hr
= user_record_new();
1298 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
);
1302 *ret
= TAKE_PTR(hr
);
1306 static int home_record_reset_human_interaction_permission(UserRecord
*hr
) {
1311 /* When we execute multiple operations one after the other, let's reset the permission to ask the
1312 * user each time, so that if interaction is necessary we will be told so again and thus can print a
1313 * nice message to the user, telling the user so. */
1315 r
= user_record_set_pkcs11_protected_authentication_path_permitted(hr
, -1);
1317 return log_error_errno(r
, "Failed to reset PKCS#11 protected authentication path permission flag: %m");
1319 r
= user_record_set_fido2_user_presence_permitted(hr
, -1);
1321 return log_error_errno(r
, "Failed to reset FIDO2 user presence permission flag: %m");
1326 static int update_home(int argc
, char *argv
[], void *userdata
) {
1327 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1328 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
1329 _cleanup_free_
char *buffer
= NULL
;
1330 const char *username
;
1335 else if (!arg_identity
) {
1336 buffer
= getusername_malloc();
1344 r
= acquire_bus(&bus
);
1348 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1350 r
= acquire_updated_home_record(bus
, username
, &hr
);
1354 /* If we do multiple operations, let's output things more verbosely, since otherwise the repeated
1355 * authentication might be confusing. */
1357 if (arg_and_resize
|| arg_and_change_password
)
1358 log_info("Updating home directory.");
1361 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1362 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1363 _cleanup_free_
char *formatted
= NULL
;
1365 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "UpdateHome");
1367 return bus_log_create_error(r
);
1369 r
= json_variant_format(hr
->json
, 0, &formatted
);
1371 return log_error_errno(r
, "Failed to format user record: %m");
1373 (void) sd_bus_message_sensitive(m
);
1375 r
= sd_bus_message_append(m
, "s", formatted
);
1377 return bus_log_create_error(r
);
1379 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1381 if (arg_and_change_password
&&
1382 sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1383 /* In the generic handler we'd ask for a password in this case, but when
1384 * changing passwords that's not sufficient, as we need to acquire all keys
1386 return log_error_errno(r
, "Security token not inserted, refusing.");
1388 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1396 log_info("Resizing home.");
1398 (void) home_record_reset_human_interaction_permission(hr
);
1400 /* Also sync down disk size to underlying LUKS/fscrypt/quota */
1401 while (arg_and_resize
) {
1402 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1403 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1405 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ResizeHome");
1407 return bus_log_create_error(r
);
1409 /* Specify UINT64_MAX as size, in which case the underlying disk size will just be synced */
1410 r
= sd_bus_message_append(m
, "st", hr
->user_name
, UINT64_MAX
);
1412 return bus_log_create_error(r
);
1414 r
= bus_message_append_secret(m
, hr
);
1416 return bus_log_create_error(r
);
1418 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1420 if (arg_and_change_password
&&
1421 sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1422 return log_error_errno(r
, "Security token not inserted, refusing.");
1424 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1431 if (arg_and_change_password
)
1432 log_info("Synchronizing passwords and encryption keys.");
1434 (void) home_record_reset_human_interaction_permission(hr
);
1436 /* Also sync down passwords to underlying LUKS/fscrypt */
1437 while (arg_and_change_password
) {
1438 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1439 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1441 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ChangePasswordHome");
1443 return bus_log_create_error(r
);
1445 /* Specify an empty new secret, in which case the underlying LUKS/fscrypt password will just be synced */
1446 r
= sd_bus_message_append(m
, "ss", hr
->user_name
, "{}");
1448 return bus_log_create_error(r
);
1450 r
= bus_message_append_secret(m
, hr
);
1452 return bus_log_create_error(r
);
1454 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1456 if (sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1457 return log_error_errno(r
, "Security token not inserted, refusing.");
1459 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1469 static int passwd_home(int argc
, char *argv
[], void *userdata
) {
1470 _cleanup_(user_record_unrefp
) UserRecord
*old_secret
= NULL
, *new_secret
= NULL
;
1471 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1472 _cleanup_free_
char *buffer
= NULL
;
1473 const char *username
;
1476 if (arg_pkcs11_token_uri
)
1477 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "To change the PKCS#11 security token use 'homectl update --pkcs11-token-uri=…'.");
1478 if (arg_fido2_device
)
1479 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "To change the FIDO2 security token use 'homectl update --fido2-device=…'.");
1480 if (identity_properties_specified())
1481 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "The 'passwd' verb does not permit changing other record properties at the same time.");
1486 buffer
= getusername_malloc();
1493 r
= acquire_bus(&bus
);
1497 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1499 old_secret
= user_record_new();
1503 new_secret
= user_record_new();
1507 r
= acquire_new_password(username
, new_secret
, /* suggest = */ true, NULL
);
1512 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1513 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1515 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ChangePasswordHome");
1517 return bus_log_create_error(r
);
1519 r
= sd_bus_message_append(m
, "s", username
);
1521 return bus_log_create_error(r
);
1523 r
= bus_message_append_secret(m
, new_secret
);
1525 return bus_log_create_error(r
);
1527 r
= bus_message_append_secret(m
, old_secret
);
1529 return bus_log_create_error(r
);
1531 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1533 if (sd_bus_error_has_name(&error
, BUS_ERROR_LOW_PASSWORD_QUALITY
)) {
1535 log_error_errno(r
, "%s", bus_error_message(&error
, r
));
1537 r
= acquire_new_password(username
, new_secret
, /* suggest = */ false, NULL
);
1539 } else if (sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1541 /* In the generic handler we'd ask for a password in this case, but when
1542 * changing passwords that's not sufficeint, as we need to acquire all keys
1544 return log_error_errno(r
, "Security token not inserted, refusing.");
1546 r
= handle_generic_user_record_error(username
, old_secret
, &error
, r
, true);
1556 static int resize_home(int argc
, char *argv
[], void *userdata
) {
1557 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1558 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
1559 uint64_t ds
= UINT64_MAX
;
1562 r
= acquire_bus(&bus
);
1566 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1568 if (arg_disk_size_relative
!= UINT64_MAX
||
1569 (argc
> 2 && parse_percent(argv
[2]) >= 0))
1570 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
1571 "Relative disk size specification currently not supported when resizing.");
1574 r
= parse_size(argv
[2], 1024, &ds
);
1576 return log_error_errno(r
, "Failed to parse disk size parameter: %s", argv
[2]);
1579 if (arg_disk_size
!= UINT64_MAX
) {
1580 if (ds
!= UINT64_MAX
&& ds
!= arg_disk_size
)
1581 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Disk size specified twice and doesn't match, refusing.");
1586 secret
= user_record_new();
1591 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1592 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1594 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ResizeHome");
1596 return bus_log_create_error(r
);
1598 r
= sd_bus_message_append(m
, "st", argv
[1], ds
);
1600 return bus_log_create_error(r
);
1602 r
= bus_message_append_secret(m
, secret
);
1604 return bus_log_create_error(r
);
1606 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1608 r
= handle_generic_user_record_error(argv
[1], secret
, &error
, r
, false);
1618 static int lock_home(int argc
, char *argv
[], void *userdata
) {
1619 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1623 r
= acquire_bus(&bus
);
1627 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
1628 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1629 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1631 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "LockHome");
1633 return bus_log_create_error(r
);
1635 r
= sd_bus_message_append(m
, "s", *i
);
1637 return bus_log_create_error(r
);
1639 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1641 log_error_errno(r
, "Failed to lock home: %s", bus_error_message(&error
, r
));
1650 static int unlock_home(int argc
, char *argv
[], void *userdata
) {
1651 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1655 r
= acquire_bus(&bus
);
1659 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
1660 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
1662 secret
= user_record_new();
1667 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1668 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1670 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "UnlockHome");
1672 return bus_log_create_error(r
);
1674 r
= sd_bus_message_append(m
, "s", *i
);
1676 return bus_log_create_error(r
);
1678 r
= bus_message_append_secret(m
, secret
);
1680 return bus_log_create_error(r
);
1682 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1684 r
= handle_generic_user_record_error(argv
[1], secret
, &error
, r
, false);
1699 static int with_home(int argc
, char *argv
[], void *userdata
) {
1700 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1701 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1702 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1703 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
1704 _cleanup_close_
int acquired_fd
= -1;
1705 _cleanup_strv_free_
char **cmdline
= NULL
;
1710 r
= acquire_bus(&bus
);
1715 _cleanup_free_
char *shell
= NULL
;
1717 /* If no command is specified, spawn a shell */
1718 r
= get_shell(&shell
);
1720 return log_error_errno(r
, "Failed to acquire shell: %m");
1722 cmdline
= strv_new(shell
);
1724 cmdline
= strv_copy(argv
+ 2);
1728 secret
= user_record_new();
1733 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "AcquireHome");
1735 return bus_log_create_error(r
);
1737 r
= sd_bus_message_append(m
, "s", argv
[1]);
1739 return bus_log_create_error(r
);
1741 r
= bus_message_append_secret(m
, secret
);
1743 return bus_log_create_error(r
);
1745 r
= sd_bus_message_append(m
, "b", /* please_suspend = */ getenv_bool("SYSTEMD_PLEASE_SUSPEND_HOME") > 0);
1747 return bus_log_create_error(r
);
1749 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, &reply
);
1750 m
= sd_bus_message_unref(m
);
1752 r
= handle_generic_user_record_error(argv
[1], secret
, &error
, r
, false);
1756 sd_bus_error_free(&error
);
1760 r
= sd_bus_message_read(reply
, "h", &fd
);
1762 return bus_log_parse_error(r
);
1764 acquired_fd
= fcntl(fd
, F_DUPFD_CLOEXEC
, 3);
1765 if (acquired_fd
< 0)
1766 return log_error_errno(errno
, "Failed to duplicate acquired fd: %m");
1768 reply
= sd_bus_message_unref(reply
);
1773 r
= bus_call_method(bus
, bus_mgr
, "GetHomeByName", &error
, &reply
, "s", argv
[1]);
1775 return log_error_errno(r
, "Failed to inspect home: %s", bus_error_message(&error
, r
));
1777 r
= sd_bus_message_read(reply
, "usussso", NULL
, NULL
, NULL
, NULL
, &home
, NULL
, NULL
);
1779 return bus_log_parse_error(r
);
1781 r
= safe_fork("(with)", FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_DEATHSIG
|FORK_LOG
|FORK_RLIMIT_NOFILE_SAFE
|FORK_REOPEN_LOG
, &pid
);
1785 if (chdir(home
) < 0) {
1786 log_error_errno(errno
, "Failed to change to directory %s: %m", home
);
1790 execvp(cmdline
[0], cmdline
);
1791 log_error_errno(errno
, "Failed to execute %s: %m", cmdline
[0]);
1795 ret
= wait_for_terminate_and_check(cmdline
[0], pid
, WAIT_LOG_ABNORMAL
);
1797 /* Close the fd that pings the home now. */
1798 acquired_fd
= safe_close(acquired_fd
);
1800 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ReleaseHome");
1802 return bus_log_create_error(r
);
1804 r
= sd_bus_message_append(m
, "s", argv
[1]);
1806 return bus_log_create_error(r
);
1808 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1810 if (sd_bus_error_has_name(&error
, BUS_ERROR_HOME_BUSY
))
1811 log_notice("Not deactivating home directory of %s, as it is still used.", argv
[1]);
1813 return log_error_errno(r
, "Failed to release user home: %s", bus_error_message(&error
, r
));
1819 static int lock_all_homes(int argc
, char *argv
[], void *userdata
) {
1820 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1821 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1822 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1825 r
= acquire_bus(&bus
);
1829 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "LockAllHomes");
1831 return bus_log_create_error(r
);
1833 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1835 return log_error_errno(r
, "Failed to lock all homes: %s", bus_error_message(&error
, r
));
1840 static int deactivate_all_homes(int argc
, char *argv
[], void *userdata
) {
1841 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1842 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1843 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1846 r
= acquire_bus(&bus
);
1850 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "DeactivateAllHomes");
1852 return bus_log_create_error(r
);
1854 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1856 return log_error_errno(r
, "Failed to deactivate all homes: %s", bus_error_message(&error
, r
));
1861 static int drop_from_identity(const char *field
) {
1866 /* If we are called to update an identity record and drop some field, let's keep track of what to
1867 * remove from the old record */
1868 r
= strv_extend(&arg_identity_filter
, field
);
1872 /* Let's also drop the field if it was previously set to a new value on the same command line */
1873 r
= json_variant_filter(&arg_identity_extra
, STRV_MAKE(field
));
1875 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
1877 r
= json_variant_filter(&arg_identity_extra_this_machine
, STRV_MAKE(field
));
1879 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
1881 r
= json_variant_filter(&arg_identity_extra_privileged
, STRV_MAKE(field
));
1883 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
1888 static int help(int argc
, char *argv
[], void *userdata
) {
1889 _cleanup_free_
char *link
= NULL
;
1892 (void) pager_open(arg_pager_flags
);
1894 r
= terminal_urlify_man("homectl", "1", &link
);
1898 printf("%1$s [OPTIONS...] COMMAND ...\n\n"
1899 "%2$sCreate, manipulate or inspect home directories.%3$s\n"
1900 "\n%4$sCommands:%5$s\n"
1901 " list List home areas\n"
1902 " activate USER… Activate a home area\n"
1903 " deactivate USER… Deactivate a home area\n"
1904 " inspect USER… Inspect a home area\n"
1905 " authenticate USER… Authenticate a home area\n"
1906 " create USER Create a home area\n"
1907 " remove USER… Remove a home area\n"
1908 " update USER Update a home area\n"
1909 " passwd USER Change password of a home area\n"
1910 " resize USER SIZE Resize a home area\n"
1911 " lock USER… Temporarily lock an active home area\n"
1912 " unlock USER… Unlock a temporarily locked home area\n"
1913 " lock-all Lock all suitable home areas\n"
1914 " deactivate-all Deactivate all active home areas\n"
1915 " with USER [COMMAND…] Run shell or command with access to a home area\n"
1916 "\n%4$sOptions:%5$s\n"
1917 " -h --help Show this help\n"
1918 " --version Show package version\n"
1919 " --no-pager Do not pipe output into a pager\n"
1920 " --no-legend Do not show the headers and footers\n"
1921 " --no-ask-password Do not ask for system passwords\n"
1922 " -H --host=[USER@]HOST Operate on remote host\n"
1923 " -M --machine=CONTAINER Operate on local container\n"
1924 " --identity=PATH Read JSON identity from file\n"
1925 " --json=FORMAT Output inspection data in JSON (takes one of\n"
1926 " pretty, short, off)\n"
1927 " -j Equivalent to --json=pretty (on TTY) or\n"
1928 " --json=short (otherwise)\n"
1929 " --export-format= Strip JSON inspection data (full, stripped,\n"
1931 " -E When specified once equals -j --export-format=\n"
1932 " stripped, when specified twice equals\n"
1933 " -j --export-format=minimal\n"
1934 "\n%4$sGeneral User Record Properties:%5$s\n"
1935 " -c --real-name=REALNAME Real name for user\n"
1936 " --realm=REALM Realm to create user in\n"
1937 " --email-address=EMAIL Email address for user\n"
1938 " --location=LOCATION Set location of user on earth\n"
1939 " --icon-name=NAME Icon name for user\n"
1940 " -d --home-dir=PATH Home directory\n"
1941 " -u --uid=UID Numeric UID for user\n"
1942 " -G --member-of=GROUP Add user to group\n"
1943 " --skel=PATH Skeleton directory to use\n"
1944 " --shell=PATH Shell for account\n"
1945 " --setenv=VARIABLE=VALUE Set an environment variable at log-in\n"
1946 " --timezone=TIMEZONE Set a time-zone\n"
1947 " --language=LOCALE Set preferred language\n"
1948 " --ssh-authorized-keys=KEYS\n"
1949 " Specify SSH public keys\n"
1950 " --pkcs11-token-uri=URI URI to PKCS#11 security token containing\n"
1951 " private key and matching X.509 certificate\n"
1952 " --fido2-device=PATH Path to FIDO2 hidraw device with hmac-secret\n"
1954 " --recovery-key=BOOL Add a recovery key\n"
1955 "\n%4$sAccount Management User Record Properties:%5$s\n"
1956 " --locked=BOOL Set locked account state\n"
1957 " --not-before=TIMESTAMP Do not allow logins before\n"
1958 " --not-after=TIMESTAMP Do not allow logins after\n"
1959 " --rate-limit-interval=SECS\n"
1960 " Login rate-limit interval in seconds\n"
1961 " --rate-limit-burst=NUMBER\n"
1962 " Login rate-limit attempts per interval\n"
1963 "\n%4$sPassword Policy User Record Properties:%5$s\n"
1964 " --password-hint=HINT Set Password hint\n"
1965 " --enforce-password-policy=BOOL\n"
1966 " Control whether to enforce system's password\n"
1967 " policy for this user\n"
1968 " -P Equivalent to --enforce-password-password=no\n"
1969 " --password-change-now=BOOL\n"
1970 " Require the password to be changed on next login\n"
1971 " --password-change-min=TIME\n"
1972 " Require minimum time between password changes\n"
1973 " --password-change-max=TIME\n"
1974 " Require maximum time between password changes\n"
1975 " --password-change-warn=TIME\n"
1976 " How much time to warn before password expiry\n"
1977 " --password-change-inactive=TIME\n"
1978 " How much time to block password after expiry\n"
1979 "\n%4$sResource Management User Record Properties:%5$s\n"
1980 " --disk-size=BYTES Size to assign the user on disk\n"
1981 " --access-mode=MODE User home directory access mode\n"
1982 " --umask=MODE Umask for user when logging in\n"
1983 " --nice=NICE Nice level for user\n"
1984 " --rlimit=LIMIT=VALUE[:VALUE]\n"
1985 " Set resource limits\n"
1986 " --tasks-max=MAX Set maximum number of per-user tasks\n"
1987 " --memory-high=BYTES Set high memory threshold in bytes\n"
1988 " --memory-max=BYTES Set maximum memory limit\n"
1989 " --cpu-weight=WEIGHT Set CPU weight\n"
1990 " --io-weight=WEIGHT Set IO weight\n"
1991 "\n%4$sStorage User Record Properties:%5$s\n"
1992 " --storage=STORAGE Storage type to use (luks, fscrypt, directory,\n"
1993 " subvolume, cifs)\n"
1994 " --image-path=PATH Path to image file/directory\n"
1995 "\n%4$sLUKS Storage User Record Properties:%5$s\n"
1996 " --fs-type=TYPE File system type to use in case of luks\n"
1997 " storage (btrfs, ext4, xfs)\n"
1998 " --luks-discard=BOOL Whether to use 'discard' feature of file system\n"
1999 " when activated (mounted)\n"
2000 " --luks-offline-discard=BOOL\n"
2001 " Whether to trim file on logout\n"
2002 " --luks-cipher=CIPHER Cipher to use for LUKS encryption\n"
2003 " --luks-cipher-mode=MODE Cipher mode to use for LUKS encryption\n"
2004 " --luks-volume-key-size=BITS\n"
2005 " Volume key size to use for LUKS encryption\n"
2006 " --luks-pbkdf-type=TYPE Password-based Key Derivation Function to use\n"
2007 " --luks-pbkdf-hash-algorithm=ALGORITHM\n"
2008 " PBKDF hash algorithm to use\n"
2009 " --luks-pbkdf-time-cost=SECS\n"
2010 " Time cost for PBKDF in seconds\n"
2011 " --luks-pbkdf-memory-cost=BYTES\n"
2012 " Memory cost for PBKDF in bytes\n"
2013 " --luks-pbkdf-parallel-threads=NUMBER\n"
2014 " Number of parallel threads for PKBDF\n"
2015 "\n%4$sMounting User Record Properties:%5$s\n"
2016 " --nosuid=BOOL Control the 'nosuid' flag of the home mount\n"
2017 " --nodev=BOOL Control the 'nodev' flag of the home mount\n"
2018 " --noexec=BOOL Control the 'noexec' flag of the home mount\n"
2019 "\n%4$sCIFS User Record Properties:%5$s\n"
2020 " --cifs-domain=DOMAIN CIFS (Windows) domain\n"
2021 " --cifs-user-name=USER CIFS (Windows) user name\n"
2022 " --cifs-service=SERVICE CIFS (Windows) service to mount as home area\n"
2023 "\n%4$sLogin Behaviour User Record Properties:%5$s\n"
2024 " --stop-delay=SECS How long to leave user services running after\n"
2026 " --kill-processes=BOOL Whether to kill user processes when sessions\n"
2028 " --auto-login=BOOL Try to log this user in automatically\n"
2029 "\nSee the %6$s for details.\n",
2030 program_invocation_short_name
,
2040 static int parse_argv(int argc
, char *argv
[]) {
2043 ARG_VERSION
= 0x100,
2046 ARG_NO_ASK_PASSWORD
,
2056 ARG_LUKS_OFFLINE_DISCARD
,
2062 ARG_SSH_AUTHORIZED_KEYS
,
2071 ARG_LUKS_CIPHER_MODE
,
2072 ARG_LUKS_VOLUME_KEY_SIZE
,
2084 ARG_LUKS_PBKDF_TYPE
,
2085 ARG_LUKS_PBKDF_HASH_ALGORITHM
,
2086 ARG_LUKS_PBKDF_TIME_COST
,
2087 ARG_LUKS_PBKDF_MEMORY_COST
,
2088 ARG_LUKS_PBKDF_PARALLEL_THREADS
,
2089 ARG_RATE_LIMIT_INTERVAL
,
2090 ARG_RATE_LIMIT_BURST
,
2093 ARG_ENFORCE_PASSWORD_POLICY
,
2094 ARG_PASSWORD_CHANGE_NOW
,
2095 ARG_PASSWORD_CHANGE_MIN
,
2096 ARG_PASSWORD_CHANGE_MAX
,
2097 ARG_PASSWORD_CHANGE_WARN
,
2098 ARG_PASSWORD_CHANGE_INACTIVE
,
2101 ARG_PKCS11_TOKEN_URI
,
2105 ARG_AND_CHANGE_PASSWORD
,
2108 static const struct option options
[] = {
2109 { "help", no_argument
, NULL
, 'h' },
2110 { "version", no_argument
, NULL
, ARG_VERSION
},
2111 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
2112 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
2113 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
2114 { "host", required_argument
, NULL
, 'H' },
2115 { "machine", required_argument
, NULL
, 'M' },
2116 { "identity", required_argument
, NULL
, 'I' },
2117 { "real-name", required_argument
, NULL
, 'c' },
2118 { "comment", required_argument
, NULL
, 'c' }, /* Compat alias to keep thing in sync with useradd(8) */
2119 { "realm", required_argument
, NULL
, ARG_REALM
},
2120 { "email-address", required_argument
, NULL
, ARG_EMAIL_ADDRESS
},
2121 { "location", required_argument
, NULL
, ARG_LOCATION
},
2122 { "password-hint", required_argument
, NULL
, ARG_PASSWORD_HINT
},
2123 { "icon-name", required_argument
, NULL
, ARG_ICON_NAME
},
2124 { "home-dir", required_argument
, NULL
, 'd' }, /* Compatible with useradd(8) */
2125 { "uid", required_argument
, NULL
, 'u' }, /* Compatible with useradd(8) */
2126 { "member-of", required_argument
, NULL
, 'G' },
2127 { "groups", required_argument
, NULL
, 'G' }, /* Compat alias to keep thing in sync with useradd(8) */
2128 { "skel", required_argument
, NULL
, 'k' }, /* Compatible with useradd(8) */
2129 { "shell", required_argument
, NULL
, 's' }, /* Compatible with useradd(8) */
2130 { "setenv", required_argument
, NULL
, ARG_SETENV
},
2131 { "timezone", required_argument
, NULL
, ARG_TIMEZONE
},
2132 { "language", required_argument
, NULL
, ARG_LANGUAGE
},
2133 { "locked", required_argument
, NULL
, ARG_LOCKED
},
2134 { "not-before", required_argument
, NULL
, ARG_NOT_BEFORE
},
2135 { "not-after", required_argument
, NULL
, ARG_NOT_AFTER
},
2136 { "expiredate", required_argument
, NULL
, 'e' }, /* Compat alias to keep thing in sync with useradd(8) */
2137 { "ssh-authorized-keys", required_argument
, NULL
, ARG_SSH_AUTHORIZED_KEYS
},
2138 { "disk-size", required_argument
, NULL
, ARG_DISK_SIZE
},
2139 { "access-mode", required_argument
, NULL
, ARG_ACCESS_MODE
},
2140 { "umask", required_argument
, NULL
, ARG_UMASK
},
2141 { "nice", required_argument
, NULL
, ARG_NICE
},
2142 { "rlimit", required_argument
, NULL
, ARG_RLIMIT
},
2143 { "tasks-max", required_argument
, NULL
, ARG_TASKS_MAX
},
2144 { "memory-high", required_argument
, NULL
, ARG_MEMORY_HIGH
},
2145 { "memory-max", required_argument
, NULL
, ARG_MEMORY_MAX
},
2146 { "cpu-weight", required_argument
, NULL
, ARG_CPU_WEIGHT
},
2147 { "io-weight", required_argument
, NULL
, ARG_IO_WEIGHT
},
2148 { "storage", required_argument
, NULL
, ARG_STORAGE
},
2149 { "image-path", required_argument
, NULL
, ARG_IMAGE_PATH
},
2150 { "fs-type", required_argument
, NULL
, ARG_FS_TYPE
},
2151 { "luks-discard", required_argument
, NULL
, ARG_LUKS_DISCARD
},
2152 { "luks-offline-discard", required_argument
, NULL
, ARG_LUKS_OFFLINE_DISCARD
},
2153 { "luks-cipher", required_argument
, NULL
, ARG_LUKS_CIPHER
},
2154 { "luks-cipher-mode", required_argument
, NULL
, ARG_LUKS_CIPHER_MODE
},
2155 { "luks-volume-key-size", required_argument
, NULL
, ARG_LUKS_VOLUME_KEY_SIZE
},
2156 { "luks-pbkdf-type", required_argument
, NULL
, ARG_LUKS_PBKDF_TYPE
},
2157 { "luks-pbkdf-hash-algorithm", required_argument
, NULL
, ARG_LUKS_PBKDF_HASH_ALGORITHM
},
2158 { "luks-pbkdf-time-cost", required_argument
, NULL
, ARG_LUKS_PBKDF_TIME_COST
},
2159 { "luks-pbkdf-memory-cost", required_argument
, NULL
, ARG_LUKS_PBKDF_MEMORY_COST
},
2160 { "luks-pbkdf-parallel-threads", required_argument
, NULL
, ARG_LUKS_PBKDF_PARALLEL_THREADS
},
2161 { "nosuid", required_argument
, NULL
, ARG_NOSUID
},
2162 { "nodev", required_argument
, NULL
, ARG_NODEV
},
2163 { "noexec", required_argument
, NULL
, ARG_NOEXEC
},
2164 { "cifs-user-name", required_argument
, NULL
, ARG_CIFS_USER_NAME
},
2165 { "cifs-domain", required_argument
, NULL
, ARG_CIFS_DOMAIN
},
2166 { "cifs-service", required_argument
, NULL
, ARG_CIFS_SERVICE
},
2167 { "rate-limit-interval", required_argument
, NULL
, ARG_RATE_LIMIT_INTERVAL
},
2168 { "rate-limit-burst", required_argument
, NULL
, ARG_RATE_LIMIT_BURST
},
2169 { "stop-delay", required_argument
, NULL
, ARG_STOP_DELAY
},
2170 { "kill-processes", required_argument
, NULL
, ARG_KILL_PROCESSES
},
2171 { "enforce-password-policy", required_argument
, NULL
, ARG_ENFORCE_PASSWORD_POLICY
},
2172 { "password-change-now", required_argument
, NULL
, ARG_PASSWORD_CHANGE_NOW
},
2173 { "password-change-min", required_argument
, NULL
, ARG_PASSWORD_CHANGE_MIN
},
2174 { "password-change-max", required_argument
, NULL
, ARG_PASSWORD_CHANGE_MAX
},
2175 { "password-change-warn", required_argument
, NULL
, ARG_PASSWORD_CHANGE_WARN
},
2176 { "password-change-inactive", required_argument
, NULL
, ARG_PASSWORD_CHANGE_INACTIVE
},
2177 { "auto-login", required_argument
, NULL
, ARG_AUTO_LOGIN
},
2178 { "json", required_argument
, NULL
, ARG_JSON
},
2179 { "export-format", required_argument
, NULL
, ARG_EXPORT_FORMAT
},
2180 { "pkcs11-token-uri", required_argument
, NULL
, ARG_PKCS11_TOKEN_URI
},
2181 { "fido2-device", required_argument
, NULL
, ARG_FIDO2_DEVICE
},
2182 { "recovery-key", required_argument
, NULL
, ARG_RECOVERY_KEY
},
2183 { "and-resize", required_argument
, NULL
, ARG_AND_RESIZE
},
2184 { "and-change-password", required_argument
, NULL
, ARG_AND_CHANGE_PASSWORD
},
2196 c
= getopt_long(argc
, argv
, "hH:M:I:c:d:u:k:s:e:G:jPE", options
, NULL
);
2203 return help(0, NULL
, NULL
);
2209 arg_pager_flags
|= PAGER_DISABLE
;
2216 case ARG_NO_ASK_PASSWORD
:
2217 arg_ask_password
= false;
2221 arg_transport
= BUS_TRANSPORT_REMOTE
;
2226 arg_transport
= BUS_TRANSPORT_MACHINE
;
2231 arg_identity
= optarg
;
2235 if (isempty(optarg
)) {
2236 r
= drop_from_identity("realName");
2243 if (!valid_gecos(optarg
))
2244 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Real name '%s' not a valid GECOS field.", optarg
);
2246 r
= json_variant_set_field_string(&arg_identity_extra
, "realName", optarg
);
2248 return log_error_errno(r
, "Failed to set realName field: %m");
2253 _cleanup_free_
char *hd
= NULL
;
2255 if (isempty(optarg
)) {
2256 r
= drop_from_identity("homeDirectory");
2263 r
= parse_path_argument_and_warn(optarg
, false, &hd
);
2267 if (!valid_home(hd
))
2268 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Home directory '%s' not valid.", hd
);
2270 r
= json_variant_set_field_string(&arg_identity_extra
, "homeDirectory", hd
);
2272 return log_error_errno(r
, "Failed to set homeDirectory field: %m");
2278 if (isempty(optarg
)) {
2279 r
= drop_from_identity("realm");
2286 r
= dns_name_is_valid(optarg
);
2288 return log_error_errno(r
, "Failed to determine whether realm '%s' is a valid DNS domain: %m", optarg
);
2290 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Realm '%s' is not a valid DNS domain: %m", optarg
);
2292 r
= json_variant_set_field_string(&arg_identity_extra
, "realm", optarg
);
2294 return log_error_errno(r
, "Failed to set realm field: %m");
2297 case ARG_EMAIL_ADDRESS
:
2300 case ARG_CIFS_USER_NAME
:
2301 case ARG_CIFS_DOMAIN
:
2302 case ARG_CIFS_SERVICE
: {
2305 c
== ARG_EMAIL_ADDRESS
? "emailAddress" :
2306 c
== ARG_LOCATION
? "location" :
2307 c
== ARG_ICON_NAME
? "iconName" :
2308 c
== ARG_CIFS_USER_NAME
? "cifsUserName" :
2309 c
== ARG_CIFS_DOMAIN
? "cifsDomain" :
2310 c
== ARG_CIFS_SERVICE
? "cifsService" :
2315 if (isempty(optarg
)) {
2316 r
= drop_from_identity(field
);
2323 r
= json_variant_set_field_string(&arg_identity_extra
, field
, optarg
);
2325 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2330 case ARG_PASSWORD_HINT
:
2331 if (isempty(optarg
)) {
2332 r
= drop_from_identity("passwordHint");
2339 r
= json_variant_set_field_string(&arg_identity_extra_privileged
, "passwordHint", optarg
);
2341 return log_error_errno(r
, "Failed to set passwordHint field: %m");
2343 string_erase(optarg
);
2349 if (isempty(optarg
)) {
2350 r
= drop_from_identity("niceLevel");
2356 r
= parse_nice(optarg
, &nc
);
2358 return log_error_errno(r
, "Failed to parse nice level: %s", optarg
);
2360 r
= json_variant_set_field_integer(&arg_identity_extra
, "niceLevel", nc
);
2362 return log_error_errno(r
, "Failed to set niceLevel field: %m");
2368 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
, *jcur
= NULL
, *jmax
= NULL
;
2369 _cleanup_free_
char *field
= NULL
, *t
= NULL
;
2374 if (isempty(optarg
)) {
2375 /* Remove all resource limits */
2377 r
= drop_from_identity("resourceLimits");
2381 arg_identity_filter_rlimits
= strv_free(arg_identity_filter_rlimits
);
2382 arg_identity_extra_rlimits
= json_variant_unref(arg_identity_extra_rlimits
);
2386 eq
= strchr(optarg
, '=');
2388 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Can't parse resource limit assignment: %s", optarg
);
2390 field
= strndup(optarg
, eq
- optarg
);
2394 l
= rlimit_from_string_harder(field
);
2396 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Unknown resource limit type: %s", field
);
2398 if (isempty(eq
+ 1)) {
2399 /* Remove only the specific rlimit */
2401 r
= strv_extend(&arg_identity_filter_rlimits
, rlimit_to_string(l
));
2405 r
= json_variant_filter(&arg_identity_extra_rlimits
, STRV_MAKE(field
));
2407 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
2412 r
= rlimit_parse(l
, eq
+ 1, &rl
);
2414 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to parse resource limit value: %s", eq
+ 1);
2416 r
= rl
.rlim_cur
== RLIM_INFINITY
? json_variant_new_null(&jcur
) : json_variant_new_unsigned(&jcur
, rl
.rlim_cur
);
2418 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to allocate current integer: %m");
2420 r
= rl
.rlim_max
== RLIM_INFINITY
? json_variant_new_null(&jmax
) : json_variant_new_unsigned(&jmax
, rl
.rlim_max
);
2422 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to allocate maximum integer: %m");
2426 JSON_BUILD_PAIR("cur", JSON_BUILD_VARIANT(jcur
)),
2427 JSON_BUILD_PAIR("max", JSON_BUILD_VARIANT(jmax
))));
2429 return log_error_errno(r
, "Failed to build resource limit: %m");
2431 t
= strjoin("RLIMIT_", rlimit_to_string(l
));
2435 r
= json_variant_set_field(&arg_identity_extra_rlimits
, t
, v
);
2437 return log_error_errno(r
, "Failed to set %s field: %m", rlimit_to_string(l
));
2445 if (isempty(optarg
)) {
2446 r
= drop_from_identity("uid");
2453 r
= parse_uid(optarg
, &uid
);
2455 return log_error_errno(r
, "Failed to parse UID '%s'.", optarg
);
2457 if (uid_is_system(uid
))
2458 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "UID " UID_FMT
" is in system range, refusing.", uid
);
2459 if (uid_is_dynamic(uid
))
2460 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "UID " UID_FMT
" is in dynamic range, refusing.", uid
);
2461 if (uid
== UID_NOBODY
)
2462 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "UID " UID_FMT
" is nobody UID, refusing.", uid
);
2464 r
= json_variant_set_field_unsigned(&arg_identity_extra
, "uid", uid
);
2466 return log_error_errno(r
, "Failed to set realm field: %m");
2472 case ARG_IMAGE_PATH
: {
2473 const char *field
= c
== 'k' ? "skeletonDirectory" : "imagePath";
2474 _cleanup_free_
char *v
= NULL
;
2476 if (isempty(optarg
)) {
2477 r
= drop_from_identity(field
);
2484 r
= parse_path_argument_and_warn(optarg
, false, &v
);
2488 r
= json_variant_set_field_string(&arg_identity_extra_this_machine
, field
, v
);
2490 return log_error_errno(r
, "Failed to set %s field: %m", v
);
2496 if (isempty(optarg
)) {
2497 r
= drop_from_identity("shell");
2504 if (!valid_shell(optarg
))
2505 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Shell '%s' not valid.", optarg
);
2507 r
= json_variant_set_field_string(&arg_identity_extra
, "shell", optarg
);
2509 return log_error_errno(r
, "Failed to set shell field: %m");
2514 _cleanup_free_
char **l
= NULL
, **k
= NULL
;
2515 _cleanup_(json_variant_unrefp
) JsonVariant
*ne
= NULL
;
2518 if (isempty(optarg
)) {
2519 r
= drop_from_identity("environment");
2526 if (!env_assignment_is_valid(optarg
))
2527 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Environment assignment '%s' not valid.", optarg
);
2529 e
= json_variant_by_key(arg_identity_extra
, "environment");
2531 r
= json_variant_strv(e
, &l
);
2533 return log_error_errno(r
, "Failed to parse JSON environment field: %m");
2536 k
= strv_env_set(l
, optarg
);
2542 r
= json_variant_new_array_strv(&ne
, k
);
2544 return log_error_errno(r
, "Failed to allocate environment list JSON: %m");
2546 r
= json_variant_set_field(&arg_identity_extra
, "environment", ne
);
2548 return log_error_errno(r
, "Failed to set environment list: %m");
2555 if (isempty(optarg
)) {
2556 r
= drop_from_identity("timeZone");
2563 if (!timezone_is_valid(optarg
, LOG_DEBUG
))
2564 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Timezone '%s' is not valid.", optarg
);
2566 r
= json_variant_set_field_string(&arg_identity_extra
, "timeZone", optarg
);
2568 return log_error_errno(r
, "Failed to set timezone field: %m");
2573 if (isempty(optarg
)) {
2574 r
= drop_from_identity("language");
2581 if (!locale_is_valid(optarg
))
2582 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Locale '%s' is not valid.", optarg
);
2584 if (locale_is_installed(optarg
) <= 0)
2585 log_warning("Locale '%s' is not installed, accepting anyway.", optarg
);
2587 r
= json_variant_set_field_string(&arg_identity_extra
, "preferredLanguage", optarg
);
2589 return log_error_errno(r
, "Failed to set preferredLanguage field: %m");
2597 case ARG_KILL_PROCESSES
:
2598 case ARG_ENFORCE_PASSWORD_POLICY
:
2599 case ARG_AUTO_LOGIN
:
2600 case ARG_PASSWORD_CHANGE_NOW
: {
2602 c
== ARG_LOCKED
? "locked" :
2603 c
== ARG_NOSUID
? "mountNoSuid" :
2604 c
== ARG_NODEV
? "mountNoDevices" :
2605 c
== ARG_NOEXEC
? "mountNoExecute" :
2606 c
== ARG_KILL_PROCESSES
? "killProcesses" :
2607 c
== ARG_ENFORCE_PASSWORD_POLICY
? "enforcePasswordPolicy" :
2608 c
== ARG_AUTO_LOGIN
? "autoLogin" :
2609 c
== ARG_PASSWORD_CHANGE_NOW
? "passwordChangeNow" :
2614 if (isempty(optarg
)) {
2615 r
= drop_from_identity(field
);
2622 r
= parse_boolean(optarg
);
2624 return log_error_errno(r
, "Failed to parse %s boolean: %m", field
);
2626 r
= json_variant_set_field_boolean(&arg_identity_extra
, field
, r
> 0);
2628 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2634 r
= json_variant_set_field_boolean(&arg_identity_extra
, "enforcePasswordPolicy", false);
2636 return log_error_errno(r
, "Failed to set enforcePasswordPolicy field: %m");
2641 if (isempty(optarg
)) {
2642 r
= drop_from_identity("diskSize");
2646 r
= drop_from_identity("diskSizeRelative");
2650 arg_disk_size
= arg_disk_size_relative
= UINT64_MAX
;
2654 r
= parse_permille(optarg
);
2656 r
= parse_size(optarg
, 1024, &arg_disk_size
);
2658 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Disk size '%s' not valid.", optarg
);
2660 r
= drop_from_identity("diskSizeRelative");
2664 r
= json_variant_set_field_unsigned(&arg_identity_extra_this_machine
, "diskSize", arg_disk_size
);
2666 return log_error_errno(r
, "Failed to set diskSize field: %m");
2668 arg_disk_size_relative
= UINT64_MAX
;
2670 /* Normalize to UINT32_MAX == 100% */
2671 arg_disk_size_relative
= (uint64_t) r
* UINT32_MAX
/ 1000U;
2673 r
= drop_from_identity("diskSize");
2677 r
= json_variant_set_field_unsigned(&arg_identity_extra_this_machine
, "diskSizeRelative", arg_disk_size_relative
);
2679 return log_error_errno(r
, "Failed to set diskSizeRelative field: %m");
2681 arg_disk_size
= UINT64_MAX
;
2686 case ARG_ACCESS_MODE
: {
2689 if (isempty(optarg
)) {
2690 r
= drop_from_identity("accessMode");
2697 r
= parse_mode(optarg
, &mode
);
2699 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Access mode '%s' not valid.", optarg
);
2701 r
= json_variant_set_field_unsigned(&arg_identity_extra
, "accessMode", mode
);
2703 return log_error_errno(r
, "Failed to set access mode field: %m");
2708 case ARG_LUKS_DISCARD
:
2709 if (isempty(optarg
)) {
2710 r
= drop_from_identity("luksDiscard");
2717 r
= parse_boolean(optarg
);
2719 return log_error_errno(r
, "Failed to parse --luks-discard= parameter: %s", optarg
);
2721 r
= json_variant_set_field_boolean(&arg_identity_extra
, "luksDiscard", r
);
2723 return log_error_errno(r
, "Failed to set discard field: %m");
2727 case ARG_LUKS_OFFLINE_DISCARD
:
2728 if (isempty(optarg
)) {
2729 r
= drop_from_identity("luksOfflineDiscard");
2736 r
= parse_boolean(optarg
);
2738 return log_error_errno(r
, "Failed to parse --luks-offline-discard= parameter: %s", optarg
);
2740 r
= json_variant_set_field_boolean(&arg_identity_extra
, "luksOfflineDiscard", r
);
2742 return log_error_errno(r
, "Failed to set offline discard field: %m");
2746 case ARG_LUKS_VOLUME_KEY_SIZE
:
2747 case ARG_LUKS_PBKDF_PARALLEL_THREADS
:
2748 case ARG_RATE_LIMIT_BURST
: {
2750 c
== ARG_LUKS_VOLUME_KEY_SIZE
? "luksVolumeKeySize" :
2751 c
== ARG_LUKS_PBKDF_PARALLEL_THREADS
? "luksPbkdfParallelThreads" :
2752 c
== ARG_RATE_LIMIT_BURST
? "rateLimitBurst" : NULL
;
2757 if (isempty(optarg
)) {
2758 r
= drop_from_identity(field
);
2763 r
= safe_atou(optarg
, &n
);
2765 return log_error_errno(r
, "Failed to parse %s parameter: %s", field
, optarg
);
2767 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, n
);
2769 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2777 if (isempty(optarg
)) {
2778 r
= drop_from_identity("umask");
2785 r
= parse_mode(optarg
, &m
);
2787 return log_error_errno(r
, "Failed to parse umask: %m");
2789 r
= json_variant_set_field_integer(&arg_identity_extra
, "umask", m
);
2791 return log_error_errno(r
, "Failed to set umask field: %m");
2796 case ARG_SSH_AUTHORIZED_KEYS
: {
2797 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
2798 _cleanup_(strv_freep
) char **l
= NULL
, **add
= NULL
;
2800 if (isempty(optarg
)) {
2801 r
= drop_from_identity("sshAuthorizedKeys");
2808 if (optarg
[0] == '@') {
2809 _cleanup_fclose_
FILE *f
= NULL
;
2811 /* If prefixed with '@' read from a file */
2813 f
= fopen(optarg
+1, "re");
2815 return log_error_errno(errno
, "Failed to open '%s': %m", optarg
+1);
2818 _cleanup_free_
char *line
= NULL
;
2820 r
= read_line(f
, LONG_LINE_MAX
, &line
);
2822 return log_error_errno(r
, "Failed to read from '%s': %m", optarg
+1);
2832 r
= strv_consume(&add
, TAKE_PTR(line
));
2837 /* Otherwise, assume it's a literal key. Let's do some superficial checks
2838 * before accept it though. */
2840 if (string_has_cc(optarg
, NULL
))
2841 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Authorized key contains control characters, refusing.");
2842 if (optarg
[0] == '#')
2843 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Specified key is a comment?");
2845 add
= strv_new(optarg
);
2850 v
= json_variant_ref(json_variant_by_key(arg_identity_extra_privileged
, "sshAuthorizedKeys"));
2852 r
= json_variant_strv(v
, &l
);
2854 return log_error_errno(r
, "Failed to parse SSH authorized keys list: %m");
2857 r
= strv_extend_strv(&l
, add
, true);
2861 v
= json_variant_unref(v
);
2863 r
= json_variant_new_array_strv(&v
, l
);
2867 r
= json_variant_set_field(&arg_identity_extra_privileged
, "sshAuthorizedKeys", v
);
2869 return log_error_errno(r
, "Failed to set authorized keys: %m");
2874 case ARG_NOT_BEFORE
:
2880 field
= c
== ARG_NOT_BEFORE
? "notBeforeUSec" :
2881 IN_SET(c
, ARG_NOT_AFTER
, 'e') ? "notAfterUSec" : NULL
;
2885 if (isempty(optarg
)) {
2886 r
= drop_from_identity(field
);
2893 /* Note the minor discrepancy regarding -e parsing here: we support that for compat
2894 * reasons, and in the original useradd(8) implementation it accepts dates in the
2895 * format YYYY-MM-DD. Coincidentally, we accept dates formatted like that too, but
2896 * with greater precision. */
2897 r
= parse_timestamp(optarg
, &n
);
2899 return log_error_errno(r
, "Failed to parse %s parameter: %m", field
);
2901 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, n
);
2903 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2907 case ARG_PASSWORD_CHANGE_MIN
:
2908 case ARG_PASSWORD_CHANGE_MAX
:
2909 case ARG_PASSWORD_CHANGE_WARN
:
2910 case ARG_PASSWORD_CHANGE_INACTIVE
: {
2914 field
= c
== ARG_PASSWORD_CHANGE_MIN
? "passwordChangeMinUSec" :
2915 c
== ARG_PASSWORD_CHANGE_MAX
? "passwordChangeMaxUSec" :
2916 c
== ARG_PASSWORD_CHANGE_WARN
? "passwordChangeWarnUSec" :
2917 c
== ARG_PASSWORD_CHANGE_INACTIVE
? "passwordChangeInactiveUSec" :
2922 if (isempty(optarg
)) {
2923 r
= drop_from_identity(field
);
2930 r
= parse_sec(optarg
, &n
);
2932 return log_error_errno(r
, "Failed to parse %s parameter: %m", field
);
2934 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, n
);
2936 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2942 case ARG_LUKS_CIPHER
:
2943 case ARG_LUKS_CIPHER_MODE
:
2944 case ARG_LUKS_PBKDF_TYPE
:
2945 case ARG_LUKS_PBKDF_HASH_ALGORITHM
: {
2948 c
== ARG_STORAGE
? "storage" :
2949 c
== ARG_FS_TYPE
? "fileSystemType" :
2950 c
== ARG_LUKS_CIPHER
? "luksCipher" :
2951 c
== ARG_LUKS_CIPHER_MODE
? "luksCipherMode" :
2952 c
== ARG_LUKS_PBKDF_TYPE
? "luksPbkdfType" :
2953 c
== ARG_LUKS_PBKDF_HASH_ALGORITHM
? "luksPbkdfHashAlgorithm" : NULL
;
2957 if (isempty(optarg
)) {
2958 r
= drop_from_identity(field
);
2965 if (!string_is_safe(optarg
))
2966 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Parameter for %s field not valid: %s", field
, optarg
);
2968 r
= json_variant_set_field_string(
2969 IN_SET(c
, ARG_STORAGE
, ARG_FS_TYPE
) ?
2970 &arg_identity_extra_this_machine
:
2971 &arg_identity_extra
, field
, optarg
);
2973 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2978 case ARG_LUKS_PBKDF_TIME_COST
:
2979 case ARG_RATE_LIMIT_INTERVAL
:
2980 case ARG_STOP_DELAY
: {
2982 c
== ARG_LUKS_PBKDF_TIME_COST
? "luksPbkdfTimeCostUSec" :
2983 c
== ARG_RATE_LIMIT_INTERVAL
? "rateLimitIntervalUSec" :
2984 c
== ARG_STOP_DELAY
? "stopDelayUSec" :
2990 if (isempty(optarg
)) {
2991 r
= drop_from_identity(field
);
2998 r
= parse_sec(optarg
, &t
);
3000 return log_error_errno(r
, "Failed to parse %s field: %s", field
, optarg
);
3002 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, t
);
3004 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3010 const char *p
= optarg
;
3013 r
= drop_from_identity("memberOf");
3021 _cleanup_(json_variant_unrefp
) JsonVariant
*mo
= NULL
;
3022 _cleanup_strv_free_
char **list
= NULL
;
3023 _cleanup_free_
char *word
= NULL
;
3025 r
= extract_first_word(&p
, &word
, ",", 0);
3027 return log_error_errno(r
, "Failed to parse group list: %m");
3031 if (!valid_user_group_name(word
, 0))
3032 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Invalid group name %s.", word
);
3034 mo
= json_variant_ref(json_variant_by_key(arg_identity_extra
, "memberOf"));
3036 r
= json_variant_strv(mo
, &list
);
3038 return log_error_errno(r
, "Failed to parse group list: %m");
3040 r
= strv_extend(&list
, word
);
3047 mo
= json_variant_unref(mo
);
3048 r
= json_variant_new_array_strv(&mo
, list
);
3050 return log_error_errno(r
, "Failed to create group list JSON: %m");
3052 r
= json_variant_set_field(&arg_identity_extra
, "memberOf", mo
);
3054 return log_error_errno(r
, "Failed to update group list: %m");
3060 case ARG_TASKS_MAX
: {
3063 if (isempty(optarg
)) {
3064 r
= drop_from_identity("tasksMax");
3070 r
= safe_atou64(optarg
, &u
);
3072 return log_error_errno(r
, "Failed to parse --tasks-max= parameter: %s", optarg
);
3074 r
= json_variant_set_field_unsigned(&arg_identity_extra
, "tasksMax", u
);
3076 return log_error_errno(r
, "Failed to set tasksMax field: %m");
3081 case ARG_MEMORY_MAX
:
3082 case ARG_MEMORY_HIGH
:
3083 case ARG_LUKS_PBKDF_MEMORY_COST
: {
3085 c
== ARG_MEMORY_MAX
? "memoryMax" :
3086 c
== ARG_MEMORY_HIGH
? "memoryHigh" :
3087 c
== ARG_LUKS_PBKDF_MEMORY_COST
? "luksPbkdfMemoryCost" : NULL
;
3093 if (isempty(optarg
)) {
3094 r
= drop_from_identity(field
);
3100 r
= parse_size(optarg
, 1024, &u
);
3102 return log_error_errno(r
, "Failed to parse %s parameter: %s", field
, optarg
);
3104 r
= json_variant_set_field_unsigned(&arg_identity_extra_this_machine
, field
, u
);
3106 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3111 case ARG_CPU_WEIGHT
:
3112 case ARG_IO_WEIGHT
: {
3113 const char *field
= c
== ARG_CPU_WEIGHT
? "cpuWeight" :
3114 c
== ARG_IO_WEIGHT
? "ioWeight" : NULL
;
3119 if (isempty(optarg
)) {
3120 r
= drop_from_identity(field
);
3126 r
= safe_atou64(optarg
, &u
);
3128 return log_error_errno(r
, "Failed to parse --cpu-weight=/--io-weight= parameter: %s", optarg
);
3130 if (!CGROUP_WEIGHT_IS_OK(u
))
3131 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Weight %" PRIu64
" is out of valid weight range.", u
);
3133 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, u
);
3135 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3140 case ARG_PKCS11_TOKEN_URI
: {
3143 if (streq(optarg
, "list"))
3144 return pkcs11_list_tokens();
3146 /* If --pkcs11-token-uri= is specified we always drop everything old */
3147 FOREACH_STRING(p
, "pkcs11TokenUri", "pkcs11EncryptedKey") {
3148 r
= drop_from_identity(p
);
3153 if (isempty(optarg
)) {
3154 arg_pkcs11_token_uri
= strv_free(arg_pkcs11_token_uri
);
3158 if (streq(optarg
, "auto")) {
3159 _cleanup_free_
char *found
= NULL
;
3161 r
= pkcs11_find_token_auto(&found
);
3164 r
= strv_consume(&arg_pkcs11_token_uri
, TAKE_PTR(found
));
3166 if (!pkcs11_uri_valid(optarg
))
3167 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Not a valid PKCS#11 URI: %s", optarg
);
3169 r
= strv_extend(&arg_pkcs11_token_uri
, optarg
);
3174 strv_uniq(arg_pkcs11_token_uri
);
3178 case ARG_FIDO2_DEVICE
: {
3181 if (streq(optarg
, "list"))
3182 return fido2_list_devices();
3184 FOREACH_STRING(p
, "fido2HmacCredential", "fido2HmacSalt") {
3185 r
= drop_from_identity(p
);
3190 if (isempty(optarg
)) {
3191 arg_fido2_device
= strv_free(arg_fido2_device
);
3195 if (streq(optarg
, "auto")) {
3196 _cleanup_free_
char *found
= NULL
;
3198 r
= fido2_find_device_auto(&found
);
3202 r
= strv_consume(&arg_fido2_device
, TAKE_PTR(found
));
3204 r
= strv_extend(&arg_fido2_device
, optarg
);
3209 strv_uniq(arg_fido2_device
);
3213 case ARG_RECOVERY_KEY
: {
3216 r
= parse_boolean(optarg
);
3218 return log_error_errno(r
, "Failed to parse --recovery-key= argument: %s", optarg
);
3220 arg_recovery_key
= r
;
3222 FOREACH_STRING(p
, "recoveryKey", "recoveryKeyType") {
3223 r
= drop_from_identity(p
);
3232 arg_json_format_flags
= JSON_FORMAT_PRETTY_AUTO
|JSON_FORMAT_COLOR_AUTO
;
3236 r
= json_parse_cmdline_parameter_and_warn(optarg
, &arg_json_format_flags
);
3243 if (arg_export_format
== EXPORT_FORMAT_FULL
)
3244 arg_export_format
= EXPORT_FORMAT_STRIPPED
;
3245 else if (arg_export_format
== EXPORT_FORMAT_STRIPPED
)
3246 arg_export_format
= EXPORT_FORMAT_MINIMAL
;
3248 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Specifying -E more than twice is not supported.");
3250 arg_json_format_flags
&= ~JSON_FORMAT_OFF
;
3251 if (arg_json_format_flags
== 0)
3252 arg_json_format_flags
= JSON_FORMAT_PRETTY_AUTO
|JSON_FORMAT_COLOR_AUTO
;
3255 case ARG_EXPORT_FORMAT
:
3256 if (streq(optarg
, "full"))
3257 arg_export_format
= EXPORT_FORMAT_FULL
;
3258 else if (streq(optarg
, "stripped"))
3259 arg_export_format
= EXPORT_FORMAT_STRIPPED
;
3260 else if (streq(optarg
, "minimal"))
3261 arg_export_format
= EXPORT_FORMAT_MINIMAL
;
3262 else if (streq(optarg
, "help")) {
3271 case ARG_AND_RESIZE
:
3272 arg_and_resize
= true;
3275 case ARG_AND_CHANGE_PASSWORD
:
3276 arg_and_change_password
= true;
3283 assert_not_reached("Unhandled option");
3287 if (!strv_isempty(arg_pkcs11_token_uri
) || !strv_isempty(arg_fido2_device
))
3288 arg_and_change_password
= true;
3290 if (arg_disk_size
!= UINT64_MAX
|| arg_disk_size_relative
!= UINT64_MAX
)
3291 arg_and_resize
= true;
3296 static int redirect_bus_mgr(void) {
3299 /* Talk to a different service if that's requested. (The same env var is also understood by homed, so
3300 * that it is relatively easily possible to invoke a second instance of homed for debug purposes and
3301 * have homectl talk to it, without colliding with the host version. This is handy when operating
3302 * from a homed-managed account.) */
3304 suffix
= getenv("SYSTEMD_HOME_DEBUG_SUFFIX");
3306 static BusLocator locator
= {
3307 .path
= "/org/freedesktop/home1",
3308 .interface
= "org.freedesktop.home1.Manager",
3311 /* Yes, we leak this memory, but there's little point to collect this, given that we only do
3312 * this in a debug environment, do it only once, and the string shall live for out entire
3313 * process runtime. */
3315 locator
.destination
= strjoin("org.freedesktop.home1.", suffix
);
3316 if (!locator
.destination
)
3321 bus_mgr
= bus_home_mgr
;
3326 static int run(int argc
, char *argv
[]) {
3327 static const Verb verbs
[] = {
3328 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
3329 { "list", VERB_ANY
, 1, VERB_DEFAULT
, list_homes
},
3330 { "activate", 2, VERB_ANY
, 0, activate_home
},
3331 { "deactivate", 2, VERB_ANY
, 0, deactivate_home
},
3332 { "inspect", VERB_ANY
, VERB_ANY
, 0, inspect_home
},
3333 { "authenticate", VERB_ANY
, VERB_ANY
, 0, authenticate_home
},
3334 { "create", VERB_ANY
, 2, 0, create_home
},
3335 { "remove", 2, VERB_ANY
, 0, remove_home
},
3336 { "update", VERB_ANY
, 2, 0, update_home
},
3337 { "passwd", VERB_ANY
, 2, 0, passwd_home
},
3338 { "resize", 2, 3, 0, resize_home
},
3339 { "lock", 2, VERB_ANY
, 0, lock_home
},
3340 { "unlock", 2, VERB_ANY
, 0, unlock_home
},
3341 { "with", 2, VERB_ANY
, 0, with_home
},
3342 { "lock-all", VERB_ANY
, 1, 0, lock_all_homes
},
3343 { "deactivate-all", VERB_ANY
, 1, 0, deactivate_all_homes
},
3351 r
= redirect_bus_mgr();
3355 r
= parse_argv(argc
, argv
);
3359 return dispatch_verb(argc
, argv
, verbs
, NULL
);
3362 DEFINE_MAIN_FUNCTION(run
);