1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
7 #include "ask-password-api.h"
8 #include "bus-common-errors.h"
10 #include "bus-locator.h"
11 #include "cgroup-util.h"
12 #include "dns-domain.h"
16 #include "format-table.h"
17 #include "home-util.h"
18 #include "homectl-fido2.h"
19 #include "homectl-pkcs11.h"
20 #include "homectl-recovery-key.h"
21 #include "libfido2-util.h"
22 #include "locale-util.h"
23 #include "main-func.h"
24 #include "memory-util.h"
26 #include "parse-argument.h"
27 #include "parse-util.h"
28 #include "path-util.h"
29 #include "pkcs11-util.h"
30 #include "pretty-print.h"
31 #include "process-util.h"
32 #include "pwquality-util.h"
33 #include "rlimit-util.h"
34 #include "spawn-polkit-agent.h"
35 #include "terminal-util.h"
36 #include "user-record-pwquality.h"
37 #include "user-record-show.h"
38 #include "user-record-util.h"
39 #include "user-record.h"
40 #include "user-util.h"
43 static PagerFlags arg_pager_flags
= 0;
44 static bool arg_legend
= true;
45 static bool arg_ask_password
= true;
46 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
47 static const char *arg_host
= NULL
;
48 static const char *arg_identity
= NULL
;
49 static JsonVariant
*arg_identity_extra
= NULL
;
50 static JsonVariant
*arg_identity_extra_privileged
= NULL
;
51 static JsonVariant
*arg_identity_extra_this_machine
= NULL
;
52 static JsonVariant
*arg_identity_extra_rlimits
= NULL
;
53 static char **arg_identity_filter
= NULL
; /* this one is also applied to 'privileged' and 'thisMachine' subobjects */
54 static char **arg_identity_filter_rlimits
= NULL
;
55 static uint64_t arg_disk_size
= UINT64_MAX
;
56 static uint64_t arg_disk_size_relative
= UINT64_MAX
;
57 static char **arg_pkcs11_token_uri
= NULL
;
58 static char **arg_fido2_device
= NULL
;
59 static bool arg_recovery_key
= false;
60 static JsonFormatFlags arg_json_format_flags
= JSON_FORMAT_OFF
;
61 static bool arg_and_resize
= false;
62 static bool arg_and_change_password
= false;
64 EXPORT_FORMAT_FULL
, /* export the full record */
65 EXPORT_FORMAT_STRIPPED
, /* strip "state" + "binding", but leave signature in place */
66 EXPORT_FORMAT_MINIMAL
, /* also strip signature */
67 } arg_export_format
= EXPORT_FORMAT_FULL
;
69 STATIC_DESTRUCTOR_REGISTER(arg_identity_extra
, json_variant_unrefp
);
70 STATIC_DESTRUCTOR_REGISTER(arg_identity_extra_this_machine
, json_variant_unrefp
);
71 STATIC_DESTRUCTOR_REGISTER(arg_identity_extra_privileged
, json_variant_unrefp
);
72 STATIC_DESTRUCTOR_REGISTER(arg_identity_extra_rlimits
, json_variant_unrefp
);
73 STATIC_DESTRUCTOR_REGISTER(arg_identity_filter
, strv_freep
);
74 STATIC_DESTRUCTOR_REGISTER(arg_identity_filter_rlimits
, strv_freep
);
75 STATIC_DESTRUCTOR_REGISTER(arg_pkcs11_token_uri
, strv_freep
);
76 STATIC_DESTRUCTOR_REGISTER(arg_fido2_device
, strv_freep
);
78 static const BusLocator
*bus_mgr
;
80 static bool identity_properties_specified(void) {
83 !json_variant_is_blank_object(arg_identity_extra
) ||
84 !json_variant_is_blank_object(arg_identity_extra_privileged
) ||
85 !json_variant_is_blank_object(arg_identity_extra_this_machine
) ||
86 !json_variant_is_blank_object(arg_identity_extra_rlimits
) ||
87 !strv_isempty(arg_identity_filter
) ||
88 !strv_isempty(arg_identity_filter_rlimits
) ||
89 !strv_isempty(arg_pkcs11_token_uri
) ||
90 !strv_isempty(arg_fido2_device
);
93 static int acquire_bus(sd_bus
**bus
) {
101 r
= bus_connect_transport(arg_transport
, arg_host
, false, bus
);
103 return bus_log_connect_error(r
);
105 (void) sd_bus_set_allow_interactive_authorization(*bus
, arg_ask_password
);
110 static int list_homes(int argc
, char *argv
[], void *userdata
) {
111 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
112 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
113 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
114 _cleanup_(table_unrefp
) Table
*table
= NULL
;
117 r
= acquire_bus(&bus
);
121 r
= bus_call_method(bus
, bus_mgr
, "ListHomes", &error
, &reply
, NULL
);
123 return log_error_errno(r
, "Failed to list homes: %s", bus_error_message(&error
, r
));
125 table
= table_new("name", "uid", "gid", "state", "realname", "home", "shell");
129 r
= sd_bus_message_enter_container(reply
, 'a', "(susussso)");
131 return bus_log_parse_error(r
);
134 const char *name
, *state
, *realname
, *home
, *shell
, *color
;
138 r
= sd_bus_message_read(reply
, "(susussso)", &name
, &uid
, &state
, &gid
, &realname
, &home
, &shell
, NULL
);
140 return bus_log_parse_error(r
);
144 r
= table_add_many(table
,
149 return table_log_add_error(r
);
152 r
= table_add_cell(table
, &cell
, TABLE_STRING
, state
);
154 return table_log_add_error(r
);
156 color
= user_record_state_color(state
);
158 (void) table_set_color(table
, cell
, color
);
160 r
= table_add_many(table
,
161 TABLE_STRING
, strna(empty_to_null(realname
)),
163 TABLE_STRING
, strna(empty_to_null(shell
)));
165 return table_log_add_error(r
);
168 r
= sd_bus_message_exit_container(reply
);
170 return bus_log_parse_error(r
);
172 if (table_get_rows(table
) > 1 || !FLAGS_SET(arg_json_format_flags
, JSON_FORMAT_OFF
)) {
173 r
= table_set_sort(table
, (size_t) 0, (size_t) -1);
175 return table_log_sort_error(r
);
177 r
= table_print_with_pager(table
, arg_json_format_flags
, arg_pager_flags
, arg_legend
);
182 if (arg_legend
&& (arg_json_format_flags
& JSON_FORMAT_OFF
)) {
183 if (table_get_rows(table
) > 1)
184 printf("\n%zu home areas listed.\n", table_get_rows(table
) - 1);
186 printf("No home areas.\n");
192 static int acquire_existing_password(const char *user_name
, UserRecord
*hr
, bool emphasize_current
) {
193 _cleanup_(strv_free_erasep
) char **password
= NULL
;
194 _cleanup_free_
char *question
= NULL
;
201 e
= getenv("PASSWORD");
203 /* People really shouldn't use environment variables for passing passwords. We support this
204 * only for testing purposes, and do not document the behaviour, so that people won't
205 * actually use this outside of testing. */
207 r
= user_record_set_password(hr
, STRV_MAKE(e
), true);
209 return log_error_errno(r
, "Failed to store password: %m");
212 assert_se(unsetenv("PASSWORD") == 0);
217 if (asprintf(&question
, emphasize_current
?
218 "Please enter current password for user %s:" :
219 "Please enter password for user %s:",
223 r
= ask_password_auto(question
, "user-home", NULL
, "home-password", USEC_INFINITY
, ASK_PASSWORD_ACCEPT_CACHED
|ASK_PASSWORD_PUSH_CACHE
, &password
);
225 return log_error_errno(r
, "Failed to acquire password: %m");
227 r
= user_record_set_password(hr
, password
, true);
229 return log_error_errno(r
, "Failed to store password: %m");
234 static int acquire_token_pin(const char *user_name
, UserRecord
*hr
) {
235 _cleanup_(strv_free_erasep
) char **pin
= NULL
;
236 _cleanup_free_
char *question
= NULL
;
245 r
= user_record_set_token_pin(hr
, STRV_MAKE(e
), false);
247 return log_error_errno(r
, "Failed to store token PIN: %m");
250 assert_se(unsetenv("PIN") == 0);
255 if (asprintf(&question
, "Please enter security token PIN for user %s:", user_name
) < 0)
258 /* We never cache or use cached PINs, since usually there are only very few attempts allowed before the PIN is blocked */
259 r
= ask_password_auto(question
, "user-home", NULL
, "token-pin", USEC_INFINITY
, 0, &pin
);
261 return log_error_errno(r
, "Failed to acquire security token PIN: %m");
263 r
= user_record_set_token_pin(hr
, pin
, false);
265 return log_error_errno(r
, "Failed to store security token PIN: %m");
270 static int handle_generic_user_record_error(
271 const char *user_name
,
273 const sd_bus_error
*error
,
275 bool emphasize_current_password
) {
281 if (sd_bus_error_has_name(error
, BUS_ERROR_HOME_ABSENT
))
282 return log_error_errno(SYNTHETIC_ERRNO(EREMOTE
),
283 "Home of user %s is currently absent, please plug in the necessary storage device or backing file system.", user_name
);
285 else if (sd_bus_error_has_name(error
, BUS_ERROR_AUTHENTICATION_LIMIT_HIT
))
286 return log_error_errno(SYNTHETIC_ERRNO(ETOOMANYREFS
),
287 "Too frequent unsuccessful login attempts for user %s, try again later.", user_name
);
289 else if (sd_bus_error_has_name(error
, BUS_ERROR_BAD_PASSWORD
)) {
291 if (!strv_isempty(hr
->password
))
292 log_notice("Password incorrect or not sufficient, please try again.");
294 r
= acquire_existing_password(user_name
, hr
, emphasize_current_password
);
298 } else if (sd_bus_error_has_name(error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
)) {
300 if (strv_isempty(hr
->password
))
301 log_notice("Security token not inserted, please enter password.");
303 log_notice("Password incorrect or not sufficient, and configured security token not inserted, please try again.");
305 r
= acquire_existing_password(user_name
, hr
, emphasize_current_password
);
309 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_PIN_NEEDED
)) {
311 r
= acquire_token_pin(user_name
, hr
);
315 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_PROTECTED_AUTHENTICATION_PATH_NEEDED
)) {
317 log_notice("%s%sPlease authenticate physically on security token.",
318 emoji_enabled() ? special_glyph(SPECIAL_GLYPH_TOUCH
) : "",
319 emoji_enabled() ? " " : "");
321 r
= user_record_set_pkcs11_protected_authentication_path_permitted(hr
, true);
323 return log_error_errno(r
, "Failed to set PKCS#11 protected authentication path permitted flag: %m");
325 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_USER_PRESENCE_NEEDED
)) {
327 log_notice("%s%sAuthentication requires presence verification on security token.",
328 emoji_enabled() ? special_glyph(SPECIAL_GLYPH_TOUCH
) : "",
329 emoji_enabled() ? " " : "");
331 r
= user_record_set_fido2_user_presence_permitted(hr
, true);
333 return log_error_errno(r
, "Failed to set FIDO2 user presence permitted flag: %m");
335 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_PIN_LOCKED
))
336 return log_error_errno(SYNTHETIC_ERRNO(EPERM
), "Security token PIN is locked, please unlock it first. (Hint: Removal and re-insertion might suffice.)");
338 else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_BAD_PIN
)) {
340 log_notice("Security token PIN incorrect, please try again.");
342 r
= acquire_token_pin(user_name
, hr
);
346 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_BAD_PIN_FEW_TRIES_LEFT
)) {
348 log_notice("Security token PIN incorrect, please try again (only a few tries left!).");
350 r
= acquire_token_pin(user_name
, hr
);
354 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_BAD_PIN_ONE_TRY_LEFT
)) {
356 log_notice("Security token PIN incorrect, please try again (only one try left!).");
358 r
= acquire_token_pin(user_name
, hr
);
362 return log_error_errno(ret
, "Operation on home %s failed: %s", user_name
, bus_error_message(error
, ret
));
367 static int activate_home(int argc
, char *argv
[], void *userdata
) {
368 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
372 r
= acquire_bus(&bus
);
376 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
377 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
379 secret
= user_record_new();
384 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
385 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
387 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ActivateHome");
389 return bus_log_create_error(r
);
391 r
= sd_bus_message_append(m
, "s", *i
);
393 return bus_log_create_error(r
);
395 r
= bus_message_append_secret(m
, secret
);
397 return bus_log_create_error(r
);
399 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
401 r
= handle_generic_user_record_error(*i
, secret
, &error
, r
, false);
416 static int deactivate_home(int argc
, char *argv
[], void *userdata
) {
417 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
421 r
= acquire_bus(&bus
);
425 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
426 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
427 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
429 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "DeactivateHome");
431 return bus_log_create_error(r
);
433 r
= sd_bus_message_append(m
, "s", *i
);
435 return bus_log_create_error(r
);
437 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
439 log_error_errno(r
, "Failed to deactivate user home: %s", bus_error_message(&error
, r
));
448 static void dump_home_record(UserRecord
*hr
) {
453 if (hr
->incomplete
) {
455 log_warning("Warning: lacking rights to acquire privileged fields of user record of '%s', output incomplete.", hr
->user_name
);
458 if (arg_json_format_flags
& JSON_FORMAT_OFF
)
459 user_record_show(hr
, true);
461 _cleanup_(user_record_unrefp
) UserRecord
*stripped
= NULL
;
463 if (arg_export_format
== EXPORT_FORMAT_STRIPPED
)
464 r
= user_record_clone(hr
, USER_RECORD_EXTRACT_EMBEDDED
, &stripped
);
465 else if (arg_export_format
== EXPORT_FORMAT_MINIMAL
)
466 r
= user_record_clone(hr
, USER_RECORD_EXTRACT_SIGNABLE
, &stripped
);
470 log_warning_errno(r
, "Failed to strip user record, ignoring: %m");
474 json_variant_dump(hr
->json
, arg_json_format_flags
, stdout
, NULL
);
478 static char **mangle_user_list(char **list
, char ***ret_allocated
) {
479 _cleanup_free_
char *myself
= NULL
;
482 if (!strv_isempty(list
)) {
483 *ret_allocated
= NULL
;
487 myself
= getusername_malloc();
495 l
[0] = TAKE_PTR(myself
);
502 static int inspect_home(int argc
, char *argv
[], void *userdata
) {
503 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
504 _cleanup_(strv_freep
) char **mangled_list
= NULL
;
508 (void) pager_open(arg_pager_flags
);
510 r
= acquire_bus(&bus
);
514 items
= mangle_user_list(strv_skip(argv
, 1), &mangled_list
);
518 STRV_FOREACH(i
, items
) {
519 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
520 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
521 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
522 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
527 r
= parse_uid(*i
, &uid
);
529 if (!valid_user_group_name(*i
, 0)) {
530 log_error("Invalid user name '%s'.", *i
);
537 r
= bus_call_method(bus
, bus_mgr
, "GetUserRecordByName", &error
, &reply
, "s", *i
);
539 r
= bus_call_method(bus
, bus_mgr
, "GetUserRecordByUID", &error
, &reply
, "u", (uint32_t) uid
);
542 log_error_errno(r
, "Failed to inspect home: %s", bus_error_message(&error
, r
));
549 r
= sd_bus_message_read(reply
, "sbo", &json
, &incomplete
, NULL
);
551 bus_log_parse_error(r
);
558 r
= json_parse(json
, JSON_PARSE_SENSITIVE
, &v
, NULL
, NULL
);
560 log_error_errno(r
, "Failed to parse JSON identity: %m");
567 hr
= user_record_new();
571 r
= user_record_load(hr
, v
, USER_RECORD_LOAD_REFUSE_SECRET
|USER_RECORD_LOG
);
579 hr
->incomplete
= incomplete
;
580 dump_home_record(hr
);
586 static int authenticate_home(int argc
, char *argv
[], void *userdata
) {
587 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
588 _cleanup_(strv_freep
) char **mangled_list
= NULL
;
592 items
= mangle_user_list(strv_skip(argv
, 1), &mangled_list
);
596 r
= acquire_bus(&bus
);
600 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
602 STRV_FOREACH(i
, items
) {
603 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
605 secret
= user_record_new();
610 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
611 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
613 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "AuthenticateHome");
615 return bus_log_create_error(r
);
617 r
= sd_bus_message_append(m
, "s", *i
);
619 return bus_log_create_error(r
);
621 r
= bus_message_append_secret(m
, secret
);
623 return bus_log_create_error(r
);
625 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
627 r
= handle_generic_user_record_error(*i
, secret
, &error
, r
, false);
642 static int update_last_change(JsonVariant
**v
, bool with_password
, bool override
) {
649 n
= now(CLOCK_REALTIME
);
651 c
= json_variant_by_key(*v
, "lastChangeUSec");
656 goto update_password
;
658 if (!json_variant_is_unsigned(c
))
659 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastChangeUSec field is not an unsigned integer, refusing.");
661 u
= json_variant_unsigned(c
);
663 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastChangeUSec is from the future, can't update.");
666 r
= json_variant_set_field_unsigned(v
, "lastChangeUSec", n
);
668 return log_error_errno(r
, "Failed to update lastChangeUSec: %m");
674 c
= json_variant_by_key(*v
, "lastPasswordChangeUSec");
681 if (!json_variant_is_unsigned(c
))
682 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastPasswordChangeUSec field is not an unsigned integer, refusing.");
684 u
= json_variant_unsigned(c
);
686 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastPasswordChangeUSec is from the future, can't update.");
689 r
= json_variant_set_field_unsigned(v
, "lastPasswordChangeUSec", n
);
691 return log_error_errno(r
, "Failed to update lastPasswordChangeUSec: %m");
696 static int apply_identity_changes(JsonVariant
**_v
) {
697 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
702 v
= json_variant_ref(*_v
);
704 r
= json_variant_filter(&v
, arg_identity_filter
);
706 return log_error_errno(r
, "Failed to filter identity: %m");
708 r
= json_variant_merge(&v
, arg_identity_extra
);
710 return log_error_errno(r
, "Failed to merge identities: %m");
712 if (arg_identity_extra_this_machine
|| !strv_isempty(arg_identity_filter
)) {
713 _cleanup_(json_variant_unrefp
) JsonVariant
*per_machine
= NULL
, *mmid
= NULL
;
714 char mids
[SD_ID128_STRING_MAX
];
717 r
= sd_id128_get_machine(&mid
);
719 return log_error_errno(r
, "Failed to acquire machine ID: %m");
721 r
= json_variant_new_string(&mmid
, sd_id128_to_string(mid
, mids
));
723 return log_error_errno(r
, "Failed to allocate matchMachineId object: %m");
725 per_machine
= json_variant_ref(json_variant_by_key(v
, "perMachine"));
727 _cleanup_(json_variant_unrefp
) JsonVariant
*npm
= NULL
, *add
= NULL
;
728 _cleanup_free_ JsonVariant
**array
= NULL
;
732 if (!json_variant_is_array(per_machine
))
733 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "perMachine field is not an array, refusing.");
735 array
= new(JsonVariant
*, json_variant_elements(per_machine
) + 1);
739 JSON_VARIANT_ARRAY_FOREACH(z
, per_machine
) {
742 if (!json_variant_is_object(z
))
743 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "perMachine entry is not an object, refusing.");
747 u
= json_variant_by_key(z
, "matchMachineId");
751 if (!json_variant_equal(u
, mmid
))
754 r
= json_variant_merge(&add
, z
);
756 return log_error_errno(r
, "Failed to merge perMachine entry: %m");
761 r
= json_variant_filter(&add
, arg_identity_filter
);
763 return log_error_errno(r
, "Failed to filter perMachine: %m");
765 r
= json_variant_merge(&add
, arg_identity_extra_this_machine
);
767 return log_error_errno(r
, "Failed to merge in perMachine fields: %m");
769 if (arg_identity_filter_rlimits
|| arg_identity_extra_rlimits
) {
770 _cleanup_(json_variant_unrefp
) JsonVariant
*rlv
= NULL
;
772 rlv
= json_variant_ref(json_variant_by_key(add
, "resourceLimits"));
774 r
= json_variant_filter(&rlv
, arg_identity_filter_rlimits
);
776 return log_error_errno(r
, "Failed to filter resource limits: %m");
778 r
= json_variant_merge(&rlv
, arg_identity_extra_rlimits
);
780 return log_error_errno(r
, "Failed to set resource limits: %m");
782 if (json_variant_is_blank_object(rlv
)) {
783 r
= json_variant_filter(&add
, STRV_MAKE("resourceLimits"));
785 return log_error_errno(r
, "Failed to drop resource limits field from identity: %m");
787 r
= json_variant_set_field(&add
, "resourceLimits", rlv
);
789 return log_error_errno(r
, "Failed to update resource limits of identity: %m");
793 if (!json_variant_is_blank_object(add
)) {
794 r
= json_variant_set_field(&add
, "matchMachineId", mmid
);
796 return log_error_errno(r
, "Failed to set matchMachineId field: %m");
801 r
= json_variant_new_array(&npm
, array
, i
);
803 return log_error_errno(r
, "Failed to allocate new perMachine array: %m");
805 json_variant_unref(per_machine
);
806 per_machine
= TAKE_PTR(npm
);
808 _cleanup_(json_variant_unrefp
) JsonVariant
*item
= json_variant_ref(arg_identity_extra_this_machine
);
810 if (arg_identity_extra_rlimits
) {
811 r
= json_variant_set_field(&item
, "resourceLimits", arg_identity_extra_rlimits
);
813 return log_error_errno(r
, "Failed to update resource limits of identity: %m");
816 r
= json_variant_set_field(&item
, "matchMachineId", mmid
);
818 return log_error_errno(r
, "Failed to set matchMachineId field: %m");
820 r
= json_variant_append_array(&per_machine
, item
);
822 return log_error_errno(r
, "Failed to append to perMachine array: %m");
825 r
= json_variant_set_field(&v
, "perMachine", per_machine
);
827 return log_error_errno(r
, "Failed to update per machine record: %m");
830 if (arg_identity_extra_privileged
|| arg_identity_filter
) {
831 _cleanup_(json_variant_unrefp
) JsonVariant
*privileged
= NULL
;
833 privileged
= json_variant_ref(json_variant_by_key(v
, "privileged"));
835 r
= json_variant_filter(&privileged
, arg_identity_filter
);
837 return log_error_errno(r
, "Failed to filter identity (privileged part): %m");
839 r
= json_variant_merge(&privileged
, arg_identity_extra_privileged
);
841 return log_error_errno(r
, "Failed to merge identities (privileged part): %m");
843 if (json_variant_is_blank_object(privileged
)) {
844 r
= json_variant_filter(&v
, STRV_MAKE("privileged"));
846 return log_error_errno(r
, "Failed to drop privileged part from identity: %m");
848 r
= json_variant_set_field(&v
, "privileged", privileged
);
850 return log_error_errno(r
, "Failed to update privileged part of identity: %m");
854 if (arg_identity_filter_rlimits
) {
855 _cleanup_(json_variant_unrefp
) JsonVariant
*rlv
= NULL
;
857 rlv
= json_variant_ref(json_variant_by_key(v
, "resourceLimits"));
859 r
= json_variant_filter(&rlv
, arg_identity_filter_rlimits
);
861 return log_error_errno(r
, "Failed to filter resource limits: %m");
863 /* Note that we only filter resource limits here, but don't apply them. We do that in the perMachine section */
865 if (json_variant_is_blank_object(rlv
)) {
866 r
= json_variant_filter(&v
, STRV_MAKE("resourceLimits"));
868 return log_error_errno(r
, "Failed to drop resource limits field from identity: %m");
870 r
= json_variant_set_field(&v
, "resourceLimits", rlv
);
872 return log_error_errno(r
, "Failed to update resource limits of identity: %m");
876 json_variant_unref(*_v
);
882 static int add_disposition(JsonVariant
**v
) {
887 if (json_variant_by_key(*v
, "disposition"))
890 /* Set the disposition to regular, if not configured explicitly */
891 r
= json_variant_set_field_string(v
, "disposition", "regular");
893 return log_error_errno(r
, "Failed to set disposition field: %m");
898 static int acquire_new_home_record(UserRecord
**ret
) {
899 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
900 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
907 unsigned line
, column
;
910 streq(arg_identity
, "-") ? stdin
: NULL
,
911 streq(arg_identity
, "-") ? "<stdin>" : arg_identity
, JSON_PARSE_SENSITIVE
, &v
, &line
, &column
);
913 return log_error_errno(r
, "Failed to parse identity at %u:%u: %m", line
, column
);
916 r
= apply_identity_changes(&v
);
920 r
= add_disposition(&v
);
924 STRV_FOREACH(i
, arg_pkcs11_token_uri
) {
925 r
= identity_add_pkcs11_key_data(&v
, *i
);
930 STRV_FOREACH(i
, arg_fido2_device
) {
931 r
= identity_add_fido2_parameters(&v
, *i
);
936 if (arg_recovery_key
) {
937 r
= identity_add_recovery_key(&v
);
942 r
= update_last_change(&v
, true, false);
947 json_variant_dump(v
, JSON_FORMAT_PRETTY
, NULL
, NULL
);
949 hr
= user_record_new();
953 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
);
961 static int acquire_new_password(
962 const char *user_name
,
974 e
= getenv("NEWPASSWORD");
976 _cleanup_(erase_and_freep
) char *copy
= NULL
;
978 /* As above, this is not for use, just for testing */
986 r
= user_record_set_password(hr
, STRV_MAKE(e
), /* prepend = */ true);
988 return log_error_errno(r
, "Failed to store password: %m");
991 assert_se(unsetenv("NEWPASSWORD") == 0);
994 *ret
= TAKE_PTR(copy
);
1000 (void) suggest_passwords();
1003 _cleanup_(strv_free_erasep
) char **first
= NULL
, **second
= NULL
;
1004 _cleanup_free_
char *question
= NULL
;
1007 return log_error_errno(SYNTHETIC_ERRNO(ENOKEY
), "Too many attempts, giving up:");
1009 if (asprintf(&question
, "Please enter new password for user %s:", user_name
) < 0)
1012 r
= ask_password_auto(question
, "user-home", NULL
, "home-password", USEC_INFINITY
, 0, &first
);
1014 return log_error_errno(r
, "Failed to acquire password: %m");
1016 question
= mfree(question
);
1017 if (asprintf(&question
, "Please enter new password for user %s (repeat):", user_name
) < 0)
1020 r
= ask_password_auto(question
, "user-home", NULL
, "home-password", USEC_INFINITY
, 0, &second
);
1022 return log_error_errno(r
, "Failed to acquire password: %m");
1024 if (strv_equal(first
, second
)) {
1025 _cleanup_(erase_and_freep
) char *copy
= NULL
;
1028 copy
= strdup(first
[0]);
1033 r
= user_record_set_password(hr
, first
, /* prepend = */ true);
1035 return log_error_errno(r
, "Failed to store password: %m");
1038 *ret
= TAKE_PTR(copy
);
1043 log_error("Password didn't match, try again.");
1047 static int create_home(int argc
, char *argv
[], void *userdata
) {
1048 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1049 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
1052 r
= acquire_bus(&bus
);
1056 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1059 /* If a username was specified, use it */
1061 if (valid_user_group_name(argv
[1], 0))
1062 r
= json_variant_set_field_string(&arg_identity_extra
, "userName", argv
[1]);
1064 _cleanup_free_
char *un
= NULL
, *rr
= NULL
;
1066 /* Before we consider the user name invalid, let's check if we can split it? */
1067 r
= split_user_name_realm(argv
[1], &un
, &rr
);
1069 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User name '%s' is not valid: %m", argv
[1]);
1072 r
= json_variant_set_field_string(&arg_identity_extra
, "realm", rr
);
1074 return log_error_errno(r
, "Failed to set realm field: %m");
1077 r
= json_variant_set_field_string(&arg_identity_extra
, "userName", un
);
1080 return log_error_errno(r
, "Failed to set userName field: %m");
1082 /* If neither a username nor an identity have been specified we cannot operate. */
1084 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User name required.");
1087 r
= acquire_new_home_record(&hr
);
1091 /* If the JSON record carries no plain text password (besides the recovery key), then let's query it
1093 if (strv_length(hr
->password
) <= arg_recovery_key
) {
1095 if (strv_isempty(hr
->hashed_password
)) {
1096 _cleanup_(erase_and_freep
) char *new_password
= NULL
;
1098 /* No regular (i.e. non-PKCS#11) hashed passwords set in the record, let's fix that. */
1099 r
= acquire_new_password(hr
->user_name
, hr
, /* suggest = */ true, &new_password
);
1103 r
= user_record_make_hashed_password(hr
, STRV_MAKE(new_password
), /* extend = */ false);
1105 return log_error_errno(r
, "Failed to hash password: %m");
1107 /* There's a hash password set in the record, acquire the unhashed version of it. */
1108 r
= acquire_existing_password(hr
->user_name
, hr
, /* emphasize_current= */ false);
1114 if (hr
->enforce_password_policy
== 0) {
1115 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1117 /* If password quality enforcement is disabled, let's at least warn client side */
1119 r
= user_record_quality_check_password(hr
, hr
, &error
);
1121 log_warning_errno(r
, "Specified password does not pass quality checks (%s), proceeding anyway.", bus_error_message(&error
, r
));
1125 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1126 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1127 _cleanup_(erase_and_freep
) char *formatted
= NULL
;
1129 r
= json_variant_format(hr
->json
, 0, &formatted
);
1131 return log_error_errno(r
, "Failed to format user record: %m");
1133 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "CreateHome");
1135 return bus_log_create_error(r
);
1137 (void) sd_bus_message_sensitive(m
);
1139 r
= sd_bus_message_append(m
, "s", formatted
);
1141 return bus_log_create_error(r
);
1143 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1145 if (sd_bus_error_has_name(&error
, BUS_ERROR_LOW_PASSWORD_QUALITY
)) {
1146 _cleanup_(erase_and_freep
) char *new_password
= NULL
;
1148 log_error_errno(r
, "%s", bus_error_message(&error
, r
));
1149 log_info("(Use --enforce-password-policy=no to turn off password quality checks for this account.)");
1151 r
= acquire_new_password(hr
->user_name
, hr
, /* suggest = */ false, &new_password
);
1155 r
= user_record_make_hashed_password(hr
, STRV_MAKE(new_password
), /* extend = */ false);
1157 return log_error_errno(r
, "Failed to hash passwords: %m");
1159 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1170 static int remove_home(int argc
, char *argv
[], void *userdata
) {
1171 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1175 r
= acquire_bus(&bus
);
1179 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1181 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
1182 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1183 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1185 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "RemoveHome");
1187 return bus_log_create_error(r
);
1189 r
= sd_bus_message_append(m
, "s", *i
);
1191 return bus_log_create_error(r
);
1193 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1195 log_error_errno(r
, "Failed to remove home: %s", bus_error_message(&error
, r
));
1204 static int acquire_updated_home_record(
1206 const char *username
,
1209 _cleanup_(json_variant_unrefp
) JsonVariant
*json
= NULL
;
1210 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
1217 unsigned line
, column
;
1220 r
= json_parse_file(
1221 streq(arg_identity
, "-") ? stdin
: NULL
,
1222 streq(arg_identity
, "-") ? "<stdin>" : arg_identity
, JSON_PARSE_SENSITIVE
, &json
, &line
, &column
);
1224 return log_error_errno(r
, "Failed to parse identity at %u:%u: %m", line
, column
);
1226 un
= json_variant_by_key(json
, "userName");
1228 if (!json_variant_is_string(un
) || (username
&& !streq(json_variant_string(un
), username
)))
1229 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User name specified on command line and in JSON record do not match.");
1232 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "No username specified.");
1234 r
= json_variant_set_field_string(&arg_identity_extra
, "userName", username
);
1236 return log_error_errno(r
, "Failed to set userName field: %m");
1240 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1241 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1245 if (!identity_properties_specified())
1246 return log_error_errno(SYNTHETIC_ERRNO(EALREADY
), "No field to change specified.");
1248 r
= bus_call_method(bus
, bus_mgr
, "GetUserRecordByName", &error
, &reply
, "s", username
);
1250 return log_error_errno(r
, "Failed to acquire user home record: %s", bus_error_message(&error
, r
));
1252 r
= sd_bus_message_read(reply
, "sbo", &text
, &incomplete
, NULL
);
1254 return bus_log_parse_error(r
);
1257 return log_error_errno(SYNTHETIC_ERRNO(EACCES
), "Lacking rights to acquire user record including privileged metadata, can't update record.");
1259 r
= json_parse(text
, JSON_PARSE_SENSITIVE
, &json
, NULL
, NULL
);
1261 return log_error_errno(r
, "Failed to parse JSON identity: %m");
1263 reply
= sd_bus_message_unref(reply
);
1265 r
= json_variant_filter(&json
, STRV_MAKE("binding", "status", "signature"));
1267 return log_error_errno(r
, "Failed to strip binding and status from record to update: %m");
1270 r
= apply_identity_changes(&json
);
1274 STRV_FOREACH(i
, arg_pkcs11_token_uri
) {
1275 r
= identity_add_pkcs11_key_data(&json
, *i
);
1280 STRV_FOREACH(i
, arg_fido2_device
) {
1281 r
= identity_add_fido2_parameters(&json
, *i
);
1286 /* If the user supplied a full record, then add in lastChange, but do not override. Otherwise always
1288 r
= update_last_change(&json
, arg_pkcs11_token_uri
|| arg_fido2_device
, !arg_identity
);
1293 json_variant_dump(json
, JSON_FORMAT_PRETTY
, NULL
, NULL
);
1295 hr
= user_record_new();
1299 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
);
1303 *ret
= TAKE_PTR(hr
);
1307 static int home_record_reset_human_interaction_permission(UserRecord
*hr
) {
1312 /* When we execute multiple operations one after the other, let's reset the permission to ask the
1313 * user each time, so that if interaction is necessary we will be told so again and thus can print a
1314 * nice message to the user, telling the user so. */
1316 r
= user_record_set_pkcs11_protected_authentication_path_permitted(hr
, -1);
1318 return log_error_errno(r
, "Failed to reset PKCS#11 protected authentication path permission flag: %m");
1320 r
= user_record_set_fido2_user_presence_permitted(hr
, -1);
1322 return log_error_errno(r
, "Failed to reset FIDO2 user presence permission flag: %m");
1327 static int update_home(int argc
, char *argv
[], void *userdata
) {
1328 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1329 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
1330 _cleanup_free_
char *buffer
= NULL
;
1331 const char *username
;
1336 else if (!arg_identity
) {
1337 buffer
= getusername_malloc();
1345 r
= acquire_bus(&bus
);
1349 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1351 r
= acquire_updated_home_record(bus
, username
, &hr
);
1355 /* If we do multiple operations, let's output things more verbosely, since otherwise the repeated
1356 * authentication might be confusing. */
1358 if (arg_and_resize
|| arg_and_change_password
)
1359 log_info("Updating home directory.");
1362 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1363 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1364 _cleanup_free_
char *formatted
= NULL
;
1366 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "UpdateHome");
1368 return bus_log_create_error(r
);
1370 r
= json_variant_format(hr
->json
, 0, &formatted
);
1372 return log_error_errno(r
, "Failed to format user record: %m");
1374 (void) sd_bus_message_sensitive(m
);
1376 r
= sd_bus_message_append(m
, "s", formatted
);
1378 return bus_log_create_error(r
);
1380 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1382 if (arg_and_change_password
&&
1383 sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1384 /* In the generic handler we'd ask for a password in this case, but when
1385 * changing passwords that's not sufficient, as we need to acquire all keys
1387 return log_error_errno(r
, "Security token not inserted, refusing.");
1389 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1397 log_info("Resizing home.");
1399 (void) home_record_reset_human_interaction_permission(hr
);
1401 /* Also sync down disk size to underlying LUKS/fscrypt/quota */
1402 while (arg_and_resize
) {
1403 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1404 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1406 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ResizeHome");
1408 return bus_log_create_error(r
);
1410 /* Specify UINT64_MAX as size, in which case the underlying disk size will just be synced */
1411 r
= sd_bus_message_append(m
, "st", hr
->user_name
, UINT64_MAX
);
1413 return bus_log_create_error(r
);
1415 r
= bus_message_append_secret(m
, hr
);
1417 return bus_log_create_error(r
);
1419 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1421 if (arg_and_change_password
&&
1422 sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1423 return log_error_errno(r
, "Security token not inserted, refusing.");
1425 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1432 if (arg_and_change_password
)
1433 log_info("Synchronizing passwords and encryption keys.");
1435 (void) home_record_reset_human_interaction_permission(hr
);
1437 /* Also sync down passwords to underlying LUKS/fscrypt */
1438 while (arg_and_change_password
) {
1439 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1440 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1442 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ChangePasswordHome");
1444 return bus_log_create_error(r
);
1446 /* Specify an empty new secret, in which case the underlying LUKS/fscrypt password will just be synced */
1447 r
= sd_bus_message_append(m
, "ss", hr
->user_name
, "{}");
1449 return bus_log_create_error(r
);
1451 r
= bus_message_append_secret(m
, hr
);
1453 return bus_log_create_error(r
);
1455 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1457 if (sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1458 return log_error_errno(r
, "Security token not inserted, refusing.");
1460 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1470 static int passwd_home(int argc
, char *argv
[], void *userdata
) {
1471 _cleanup_(user_record_unrefp
) UserRecord
*old_secret
= NULL
, *new_secret
= NULL
;
1472 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1473 _cleanup_free_
char *buffer
= NULL
;
1474 const char *username
;
1477 if (arg_pkcs11_token_uri
)
1478 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "To change the PKCS#11 security token use 'homectl update --pkcs11-token-uri=…'.");
1479 if (arg_fido2_device
)
1480 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "To change the FIDO2 security token use 'homectl update --fido2-device=…'.");
1481 if (identity_properties_specified())
1482 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "The 'passwd' verb does not permit changing other record properties at the same time.");
1487 buffer
= getusername_malloc();
1494 r
= acquire_bus(&bus
);
1498 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1500 old_secret
= user_record_new();
1504 new_secret
= user_record_new();
1508 r
= acquire_new_password(username
, new_secret
, /* suggest = */ true, NULL
);
1513 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1514 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1516 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ChangePasswordHome");
1518 return bus_log_create_error(r
);
1520 r
= sd_bus_message_append(m
, "s", username
);
1522 return bus_log_create_error(r
);
1524 r
= bus_message_append_secret(m
, new_secret
);
1526 return bus_log_create_error(r
);
1528 r
= bus_message_append_secret(m
, old_secret
);
1530 return bus_log_create_error(r
);
1532 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1534 if (sd_bus_error_has_name(&error
, BUS_ERROR_LOW_PASSWORD_QUALITY
)) {
1536 log_error_errno(r
, "%s", bus_error_message(&error
, r
));
1538 r
= acquire_new_password(username
, new_secret
, /* suggest = */ false, NULL
);
1540 } else if (sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1542 /* In the generic handler we'd ask for a password in this case, but when
1543 * changing passwords that's not sufficeint, as we need to acquire all keys
1545 return log_error_errno(r
, "Security token not inserted, refusing.");
1547 r
= handle_generic_user_record_error(username
, old_secret
, &error
, r
, true);
1557 static int resize_home(int argc
, char *argv
[], void *userdata
) {
1558 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1559 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
1560 uint64_t ds
= UINT64_MAX
;
1563 r
= acquire_bus(&bus
);
1567 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1569 if (arg_disk_size_relative
!= UINT64_MAX
||
1570 (argc
> 2 && parse_percent(argv
[2]) >= 0))
1571 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
1572 "Relative disk size specification currently not supported when resizing.");
1575 r
= parse_size(argv
[2], 1024, &ds
);
1577 return log_error_errno(r
, "Failed to parse disk size parameter: %s", argv
[2]);
1580 if (arg_disk_size
!= UINT64_MAX
) {
1581 if (ds
!= UINT64_MAX
&& ds
!= arg_disk_size
)
1582 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Disk size specified twice and doesn't match, refusing.");
1587 secret
= user_record_new();
1592 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1593 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1595 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ResizeHome");
1597 return bus_log_create_error(r
);
1599 r
= sd_bus_message_append(m
, "st", argv
[1], ds
);
1601 return bus_log_create_error(r
);
1603 r
= bus_message_append_secret(m
, secret
);
1605 return bus_log_create_error(r
);
1607 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1609 r
= handle_generic_user_record_error(argv
[1], secret
, &error
, r
, false);
1619 static int lock_home(int argc
, char *argv
[], void *userdata
) {
1620 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1624 r
= acquire_bus(&bus
);
1628 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
1629 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1630 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1632 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "LockHome");
1634 return bus_log_create_error(r
);
1636 r
= sd_bus_message_append(m
, "s", *i
);
1638 return bus_log_create_error(r
);
1640 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1642 log_error_errno(r
, "Failed to lock home: %s", bus_error_message(&error
, r
));
1651 static int unlock_home(int argc
, char *argv
[], void *userdata
) {
1652 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1656 r
= acquire_bus(&bus
);
1660 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
1661 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
1663 secret
= user_record_new();
1668 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1669 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1671 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "UnlockHome");
1673 return bus_log_create_error(r
);
1675 r
= sd_bus_message_append(m
, "s", *i
);
1677 return bus_log_create_error(r
);
1679 r
= bus_message_append_secret(m
, secret
);
1681 return bus_log_create_error(r
);
1683 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1685 r
= handle_generic_user_record_error(argv
[1], secret
, &error
, r
, false);
1700 static int with_home(int argc
, char *argv
[], void *userdata
) {
1701 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1702 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1703 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1704 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
1705 _cleanup_close_
int acquired_fd
= -1;
1706 _cleanup_strv_free_
char **cmdline
= NULL
;
1711 r
= acquire_bus(&bus
);
1716 _cleanup_free_
char *shell
= NULL
;
1718 /* If no command is specified, spawn a shell */
1719 r
= get_shell(&shell
);
1721 return log_error_errno(r
, "Failed to acquire shell: %m");
1723 cmdline
= strv_new(shell
);
1725 cmdline
= strv_copy(argv
+ 2);
1729 secret
= user_record_new();
1734 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "AcquireHome");
1736 return bus_log_create_error(r
);
1738 r
= sd_bus_message_append(m
, "s", argv
[1]);
1740 return bus_log_create_error(r
);
1742 r
= bus_message_append_secret(m
, secret
);
1744 return bus_log_create_error(r
);
1746 r
= sd_bus_message_append(m
, "b", /* please_suspend = */ getenv_bool("SYSTEMD_PLEASE_SUSPEND_HOME") > 0);
1748 return bus_log_create_error(r
);
1750 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, &reply
);
1751 m
= sd_bus_message_unref(m
);
1753 r
= handle_generic_user_record_error(argv
[1], secret
, &error
, r
, false);
1757 sd_bus_error_free(&error
);
1761 r
= sd_bus_message_read(reply
, "h", &fd
);
1763 return bus_log_parse_error(r
);
1765 acquired_fd
= fcntl(fd
, F_DUPFD_CLOEXEC
, 3);
1766 if (acquired_fd
< 0)
1767 return log_error_errno(errno
, "Failed to duplicate acquired fd: %m");
1769 reply
= sd_bus_message_unref(reply
);
1774 r
= bus_call_method(bus
, bus_mgr
, "GetHomeByName", &error
, &reply
, "s", argv
[1]);
1776 return log_error_errno(r
, "Failed to inspect home: %s", bus_error_message(&error
, r
));
1778 r
= sd_bus_message_read(reply
, "usussso", NULL
, NULL
, NULL
, NULL
, &home
, NULL
, NULL
);
1780 return bus_log_parse_error(r
);
1782 r
= safe_fork("(with)", FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_DEATHSIG
|FORK_LOG
|FORK_RLIMIT_NOFILE_SAFE
|FORK_REOPEN_LOG
, &pid
);
1786 if (chdir(home
) < 0) {
1787 log_error_errno(errno
, "Failed to change to directory %s: %m", home
);
1791 execvp(cmdline
[0], cmdline
);
1792 log_error_errno(errno
, "Failed to execute %s: %m", cmdline
[0]);
1796 ret
= wait_for_terminate_and_check(cmdline
[0], pid
, WAIT_LOG_ABNORMAL
);
1798 /* Close the fd that pings the home now. */
1799 acquired_fd
= safe_close(acquired_fd
);
1801 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ReleaseHome");
1803 return bus_log_create_error(r
);
1805 r
= sd_bus_message_append(m
, "s", argv
[1]);
1807 return bus_log_create_error(r
);
1809 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1811 if (sd_bus_error_has_name(&error
, BUS_ERROR_HOME_BUSY
))
1812 log_notice("Not deactivating home directory of %s, as it is still used.", argv
[1]);
1814 return log_error_errno(r
, "Failed to release user home: %s", bus_error_message(&error
, r
));
1820 static int lock_all_homes(int argc
, char *argv
[], void *userdata
) {
1821 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1822 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1823 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1826 r
= acquire_bus(&bus
);
1830 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "LockAllHomes");
1832 return bus_log_create_error(r
);
1834 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1836 return log_error_errno(r
, "Failed to lock all homes: %s", bus_error_message(&error
, r
));
1841 static int deactivate_all_homes(int argc
, char *argv
[], void *userdata
) {
1842 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1843 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1844 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1847 r
= acquire_bus(&bus
);
1851 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "DeactivateAllHomes");
1853 return bus_log_create_error(r
);
1855 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1857 return log_error_errno(r
, "Failed to deactivate all homes: %s", bus_error_message(&error
, r
));
1862 static int drop_from_identity(const char *field
) {
1867 /* If we are called to update an identity record and drop some field, let's keep track of what to
1868 * remove from the old record */
1869 r
= strv_extend(&arg_identity_filter
, field
);
1873 /* Let's also drop the field if it was previously set to a new value on the same command line */
1874 r
= json_variant_filter(&arg_identity_extra
, STRV_MAKE(field
));
1876 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
1878 r
= json_variant_filter(&arg_identity_extra_this_machine
, STRV_MAKE(field
));
1880 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
1882 r
= json_variant_filter(&arg_identity_extra_privileged
, STRV_MAKE(field
));
1884 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
1889 static int help(int argc
, char *argv
[], void *userdata
) {
1890 _cleanup_free_
char *link
= NULL
;
1893 (void) pager_open(arg_pager_flags
);
1895 r
= terminal_urlify_man("homectl", "1", &link
);
1899 printf("%1$s [OPTIONS...] COMMAND ...\n\n"
1900 "%2$sCreate, manipulate or inspect home directories.%3$s\n"
1901 "\n%4$sCommands:%5$s\n"
1902 " list List home areas\n"
1903 " activate USER… Activate a home area\n"
1904 " deactivate USER… Deactivate a home area\n"
1905 " inspect USER… Inspect a home area\n"
1906 " authenticate USER… Authenticate a home area\n"
1907 " create USER Create a home area\n"
1908 " remove USER… Remove a home area\n"
1909 " update USER Update a home area\n"
1910 " passwd USER Change password of a home area\n"
1911 " resize USER SIZE Resize a home area\n"
1912 " lock USER… Temporarily lock an active home area\n"
1913 " unlock USER… Unlock a temporarily locked home area\n"
1914 " lock-all Lock all suitable home areas\n"
1915 " deactivate-all Deactivate all active home areas\n"
1916 " with USER [COMMAND…] Run shell or command with access to a home area\n"
1917 "\n%4$sOptions:%5$s\n"
1918 " -h --help Show this help\n"
1919 " --version Show package version\n"
1920 " --no-pager Do not pipe output into a pager\n"
1921 " --no-legend Do not show the headers and footers\n"
1922 " --no-ask-password Do not ask for system passwords\n"
1923 " -H --host=[USER@]HOST Operate on remote host\n"
1924 " -M --machine=CONTAINER Operate on local container\n"
1925 " --identity=PATH Read JSON identity from file\n"
1926 " --json=FORMAT Output inspection data in JSON (takes one of\n"
1927 " pretty, short, off)\n"
1928 " -j Equivalent to --json=pretty (on TTY) or\n"
1929 " --json=short (otherwise)\n"
1930 " --export-format= Strip JSON inspection data (full, stripped,\n"
1932 " -E When specified once equals -j --export-format=\n"
1933 " stripped, when specified twice equals\n"
1934 " -j --export-format=minimal\n"
1935 "\n%4$sGeneral User Record Properties:%5$s\n"
1936 " -c --real-name=REALNAME Real name for user\n"
1937 " --realm=REALM Realm to create user in\n"
1938 " --email-address=EMAIL Email address for user\n"
1939 " --location=LOCATION Set location of user on earth\n"
1940 " --icon-name=NAME Icon name for user\n"
1941 " -d --home-dir=PATH Home directory\n"
1942 " -u --uid=UID Numeric UID for user\n"
1943 " -G --member-of=GROUP Add user to group\n"
1944 " --skel=PATH Skeleton directory to use\n"
1945 " --shell=PATH Shell for account\n"
1946 " --setenv=VARIABLE=VALUE Set an environment variable at log-in\n"
1947 " --timezone=TIMEZONE Set a time-zone\n"
1948 " --language=LOCALE Set preferred language\n"
1949 " --ssh-authorized-keys=KEYS\n"
1950 " Specify SSH public keys\n"
1951 " --pkcs11-token-uri=URI URI to PKCS#11 security token containing\n"
1952 " private key and matching X.509 certificate\n"
1953 " --fido2-device=PATH Path to FIDO2 hidraw device with hmac-secret\n"
1955 " --recovery-key=BOOL Add a recovery key\n"
1956 "\n%4$sAccount Management User Record Properties:%5$s\n"
1957 " --locked=BOOL Set locked account state\n"
1958 " --not-before=TIMESTAMP Do not allow logins before\n"
1959 " --not-after=TIMESTAMP Do not allow logins after\n"
1960 " --rate-limit-interval=SECS\n"
1961 " Login rate-limit interval in seconds\n"
1962 " --rate-limit-burst=NUMBER\n"
1963 " Login rate-limit attempts per interval\n"
1964 "\n%4$sPassword Policy User Record Properties:%5$s\n"
1965 " --password-hint=HINT Set Password hint\n"
1966 " --enforce-password-policy=BOOL\n"
1967 " Control whether to enforce system's password\n"
1968 " policy for this user\n"
1969 " -P Equivalent to --enforce-password-password=no\n"
1970 " --password-change-now=BOOL\n"
1971 " Require the password to be changed on next login\n"
1972 " --password-change-min=TIME\n"
1973 " Require minimum time between password changes\n"
1974 " --password-change-max=TIME\n"
1975 " Require maximum time between password changes\n"
1976 " --password-change-warn=TIME\n"
1977 " How much time to warn before password expiry\n"
1978 " --password-change-inactive=TIME\n"
1979 " How much time to block password after expiry\n"
1980 "\n%4$sResource Management User Record Properties:%5$s\n"
1981 " --disk-size=BYTES Size to assign the user on disk\n"
1982 " --access-mode=MODE User home directory access mode\n"
1983 " --umask=MODE Umask for user when logging in\n"
1984 " --nice=NICE Nice level for user\n"
1985 " --rlimit=LIMIT=VALUE[:VALUE]\n"
1986 " Set resource limits\n"
1987 " --tasks-max=MAX Set maximum number of per-user tasks\n"
1988 " --memory-high=BYTES Set high memory threshold in bytes\n"
1989 " --memory-max=BYTES Set maximum memory limit\n"
1990 " --cpu-weight=WEIGHT Set CPU weight\n"
1991 " --io-weight=WEIGHT Set IO weight\n"
1992 "\n%4$sStorage User Record Properties:%5$s\n"
1993 " --storage=STORAGE Storage type to use (luks, fscrypt, directory,\n"
1994 " subvolume, cifs)\n"
1995 " --image-path=PATH Path to image file/directory\n"
1996 "\n%4$sLUKS Storage User Record Properties:%5$s\n"
1997 " --fs-type=TYPE File system type to use in case of luks\n"
1998 " storage (btrfs, ext4, xfs)\n"
1999 " --luks-discard=BOOL Whether to use 'discard' feature of file system\n"
2000 " when activated (mounted)\n"
2001 " --luks-offline-discard=BOOL\n"
2002 " Whether to trim file on logout\n"
2003 " --luks-cipher=CIPHER Cipher to use for LUKS encryption\n"
2004 " --luks-cipher-mode=MODE Cipher mode to use for LUKS encryption\n"
2005 " --luks-volume-key-size=BITS\n"
2006 " Volume key size to use for LUKS encryption\n"
2007 " --luks-pbkdf-type=TYPE Password-based Key Derivation Function to use\n"
2008 " --luks-pbkdf-hash-algorithm=ALGORITHM\n"
2009 " PBKDF hash algorithm to use\n"
2010 " --luks-pbkdf-time-cost=SECS\n"
2011 " Time cost for PBKDF in seconds\n"
2012 " --luks-pbkdf-memory-cost=BYTES\n"
2013 " Memory cost for PBKDF in bytes\n"
2014 " --luks-pbkdf-parallel-threads=NUMBER\n"
2015 " Number of parallel threads for PKBDF\n"
2016 "\n%4$sMounting User Record Properties:%5$s\n"
2017 " --nosuid=BOOL Control the 'nosuid' flag of the home mount\n"
2018 " --nodev=BOOL Control the 'nodev' flag of the home mount\n"
2019 " --noexec=BOOL Control the 'noexec' flag of the home mount\n"
2020 "\n%4$sCIFS User Record Properties:%5$s\n"
2021 " --cifs-domain=DOMAIN CIFS (Windows) domain\n"
2022 " --cifs-user-name=USER CIFS (Windows) user name\n"
2023 " --cifs-service=SERVICE CIFS (Windows) service to mount as home area\n"
2024 "\n%4$sLogin Behaviour User Record Properties:%5$s\n"
2025 " --stop-delay=SECS How long to leave user services running after\n"
2027 " --kill-processes=BOOL Whether to kill user processes when sessions\n"
2029 " --auto-login=BOOL Try to log this user in automatically\n"
2030 "\nSee the %6$s for details.\n",
2031 program_invocation_short_name
,
2041 static int parse_argv(int argc
, char *argv
[]) {
2044 ARG_VERSION
= 0x100,
2047 ARG_NO_ASK_PASSWORD
,
2057 ARG_LUKS_OFFLINE_DISCARD
,
2063 ARG_SSH_AUTHORIZED_KEYS
,
2072 ARG_LUKS_CIPHER_MODE
,
2073 ARG_LUKS_VOLUME_KEY_SIZE
,
2085 ARG_LUKS_PBKDF_TYPE
,
2086 ARG_LUKS_PBKDF_HASH_ALGORITHM
,
2087 ARG_LUKS_PBKDF_TIME_COST
,
2088 ARG_LUKS_PBKDF_MEMORY_COST
,
2089 ARG_LUKS_PBKDF_PARALLEL_THREADS
,
2090 ARG_RATE_LIMIT_INTERVAL
,
2091 ARG_RATE_LIMIT_BURST
,
2094 ARG_ENFORCE_PASSWORD_POLICY
,
2095 ARG_PASSWORD_CHANGE_NOW
,
2096 ARG_PASSWORD_CHANGE_MIN
,
2097 ARG_PASSWORD_CHANGE_MAX
,
2098 ARG_PASSWORD_CHANGE_WARN
,
2099 ARG_PASSWORD_CHANGE_INACTIVE
,
2102 ARG_PKCS11_TOKEN_URI
,
2106 ARG_AND_CHANGE_PASSWORD
,
2109 static const struct option options
[] = {
2110 { "help", no_argument
, NULL
, 'h' },
2111 { "version", no_argument
, NULL
, ARG_VERSION
},
2112 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
2113 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
2114 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
2115 { "host", required_argument
, NULL
, 'H' },
2116 { "machine", required_argument
, NULL
, 'M' },
2117 { "identity", required_argument
, NULL
, 'I' },
2118 { "real-name", required_argument
, NULL
, 'c' },
2119 { "comment", required_argument
, NULL
, 'c' }, /* Compat alias to keep thing in sync with useradd(8) */
2120 { "realm", required_argument
, NULL
, ARG_REALM
},
2121 { "email-address", required_argument
, NULL
, ARG_EMAIL_ADDRESS
},
2122 { "location", required_argument
, NULL
, ARG_LOCATION
},
2123 { "password-hint", required_argument
, NULL
, ARG_PASSWORD_HINT
},
2124 { "icon-name", required_argument
, NULL
, ARG_ICON_NAME
},
2125 { "home-dir", required_argument
, NULL
, 'd' }, /* Compatible with useradd(8) */
2126 { "uid", required_argument
, NULL
, 'u' }, /* Compatible with useradd(8) */
2127 { "member-of", required_argument
, NULL
, 'G' },
2128 { "groups", required_argument
, NULL
, 'G' }, /* Compat alias to keep thing in sync with useradd(8) */
2129 { "skel", required_argument
, NULL
, 'k' }, /* Compatible with useradd(8) */
2130 { "shell", required_argument
, NULL
, 's' }, /* Compatible with useradd(8) */
2131 { "setenv", required_argument
, NULL
, ARG_SETENV
},
2132 { "timezone", required_argument
, NULL
, ARG_TIMEZONE
},
2133 { "language", required_argument
, NULL
, ARG_LANGUAGE
},
2134 { "locked", required_argument
, NULL
, ARG_LOCKED
},
2135 { "not-before", required_argument
, NULL
, ARG_NOT_BEFORE
},
2136 { "not-after", required_argument
, NULL
, ARG_NOT_AFTER
},
2137 { "expiredate", required_argument
, NULL
, 'e' }, /* Compat alias to keep thing in sync with useradd(8) */
2138 { "ssh-authorized-keys", required_argument
, NULL
, ARG_SSH_AUTHORIZED_KEYS
},
2139 { "disk-size", required_argument
, NULL
, ARG_DISK_SIZE
},
2140 { "access-mode", required_argument
, NULL
, ARG_ACCESS_MODE
},
2141 { "umask", required_argument
, NULL
, ARG_UMASK
},
2142 { "nice", required_argument
, NULL
, ARG_NICE
},
2143 { "rlimit", required_argument
, NULL
, ARG_RLIMIT
},
2144 { "tasks-max", required_argument
, NULL
, ARG_TASKS_MAX
},
2145 { "memory-high", required_argument
, NULL
, ARG_MEMORY_HIGH
},
2146 { "memory-max", required_argument
, NULL
, ARG_MEMORY_MAX
},
2147 { "cpu-weight", required_argument
, NULL
, ARG_CPU_WEIGHT
},
2148 { "io-weight", required_argument
, NULL
, ARG_IO_WEIGHT
},
2149 { "storage", required_argument
, NULL
, ARG_STORAGE
},
2150 { "image-path", required_argument
, NULL
, ARG_IMAGE_PATH
},
2151 { "fs-type", required_argument
, NULL
, ARG_FS_TYPE
},
2152 { "luks-discard", required_argument
, NULL
, ARG_LUKS_DISCARD
},
2153 { "luks-offline-discard", required_argument
, NULL
, ARG_LUKS_OFFLINE_DISCARD
},
2154 { "luks-cipher", required_argument
, NULL
, ARG_LUKS_CIPHER
},
2155 { "luks-cipher-mode", required_argument
, NULL
, ARG_LUKS_CIPHER_MODE
},
2156 { "luks-volume-key-size", required_argument
, NULL
, ARG_LUKS_VOLUME_KEY_SIZE
},
2157 { "luks-pbkdf-type", required_argument
, NULL
, ARG_LUKS_PBKDF_TYPE
},
2158 { "luks-pbkdf-hash-algorithm", required_argument
, NULL
, ARG_LUKS_PBKDF_HASH_ALGORITHM
},
2159 { "luks-pbkdf-time-cost", required_argument
, NULL
, ARG_LUKS_PBKDF_TIME_COST
},
2160 { "luks-pbkdf-memory-cost", required_argument
, NULL
, ARG_LUKS_PBKDF_MEMORY_COST
},
2161 { "luks-pbkdf-parallel-threads", required_argument
, NULL
, ARG_LUKS_PBKDF_PARALLEL_THREADS
},
2162 { "nosuid", required_argument
, NULL
, ARG_NOSUID
},
2163 { "nodev", required_argument
, NULL
, ARG_NODEV
},
2164 { "noexec", required_argument
, NULL
, ARG_NOEXEC
},
2165 { "cifs-user-name", required_argument
, NULL
, ARG_CIFS_USER_NAME
},
2166 { "cifs-domain", required_argument
, NULL
, ARG_CIFS_DOMAIN
},
2167 { "cifs-service", required_argument
, NULL
, ARG_CIFS_SERVICE
},
2168 { "rate-limit-interval", required_argument
, NULL
, ARG_RATE_LIMIT_INTERVAL
},
2169 { "rate-limit-burst", required_argument
, NULL
, ARG_RATE_LIMIT_BURST
},
2170 { "stop-delay", required_argument
, NULL
, ARG_STOP_DELAY
},
2171 { "kill-processes", required_argument
, NULL
, ARG_KILL_PROCESSES
},
2172 { "enforce-password-policy", required_argument
, NULL
, ARG_ENFORCE_PASSWORD_POLICY
},
2173 { "password-change-now", required_argument
, NULL
, ARG_PASSWORD_CHANGE_NOW
},
2174 { "password-change-min", required_argument
, NULL
, ARG_PASSWORD_CHANGE_MIN
},
2175 { "password-change-max", required_argument
, NULL
, ARG_PASSWORD_CHANGE_MAX
},
2176 { "password-change-warn", required_argument
, NULL
, ARG_PASSWORD_CHANGE_WARN
},
2177 { "password-change-inactive", required_argument
, NULL
, ARG_PASSWORD_CHANGE_INACTIVE
},
2178 { "auto-login", required_argument
, NULL
, ARG_AUTO_LOGIN
},
2179 { "json", required_argument
, NULL
, ARG_JSON
},
2180 { "export-format", required_argument
, NULL
, ARG_EXPORT_FORMAT
},
2181 { "pkcs11-token-uri", required_argument
, NULL
, ARG_PKCS11_TOKEN_URI
},
2182 { "fido2-device", required_argument
, NULL
, ARG_FIDO2_DEVICE
},
2183 { "recovery-key", required_argument
, NULL
, ARG_RECOVERY_KEY
},
2184 { "and-resize", required_argument
, NULL
, ARG_AND_RESIZE
},
2185 { "and-change-password", required_argument
, NULL
, ARG_AND_CHANGE_PASSWORD
},
2197 c
= getopt_long(argc
, argv
, "hH:M:I:c:d:u:k:s:e:G:jPE", options
, NULL
);
2204 return help(0, NULL
, NULL
);
2210 arg_pager_flags
|= PAGER_DISABLE
;
2217 case ARG_NO_ASK_PASSWORD
:
2218 arg_ask_password
= false;
2222 arg_transport
= BUS_TRANSPORT_REMOTE
;
2227 arg_transport
= BUS_TRANSPORT_MACHINE
;
2232 arg_identity
= optarg
;
2236 if (isempty(optarg
)) {
2237 r
= drop_from_identity("realName");
2244 if (!valid_gecos(optarg
))
2245 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Real name '%s' not a valid GECOS field.", optarg
);
2247 r
= json_variant_set_field_string(&arg_identity_extra
, "realName", optarg
);
2249 return log_error_errno(r
, "Failed to set realName field: %m");
2254 _cleanup_free_
char *hd
= NULL
;
2256 if (isempty(optarg
)) {
2257 r
= drop_from_identity("homeDirectory");
2264 r
= parse_path_argument(optarg
, false, &hd
);
2268 if (!valid_home(hd
))
2269 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Home directory '%s' not valid.", hd
);
2271 r
= json_variant_set_field_string(&arg_identity_extra
, "homeDirectory", hd
);
2273 return log_error_errno(r
, "Failed to set homeDirectory field: %m");
2279 if (isempty(optarg
)) {
2280 r
= drop_from_identity("realm");
2287 r
= dns_name_is_valid(optarg
);
2289 return log_error_errno(r
, "Failed to determine whether realm '%s' is a valid DNS domain: %m", optarg
);
2291 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Realm '%s' is not a valid DNS domain: %m", optarg
);
2293 r
= json_variant_set_field_string(&arg_identity_extra
, "realm", optarg
);
2295 return log_error_errno(r
, "Failed to set realm field: %m");
2298 case ARG_EMAIL_ADDRESS
:
2301 case ARG_CIFS_USER_NAME
:
2302 case ARG_CIFS_DOMAIN
:
2303 case ARG_CIFS_SERVICE
: {
2306 c
== ARG_EMAIL_ADDRESS
? "emailAddress" :
2307 c
== ARG_LOCATION
? "location" :
2308 c
== ARG_ICON_NAME
? "iconName" :
2309 c
== ARG_CIFS_USER_NAME
? "cifsUserName" :
2310 c
== ARG_CIFS_DOMAIN
? "cifsDomain" :
2311 c
== ARG_CIFS_SERVICE
? "cifsService" :
2316 if (isempty(optarg
)) {
2317 r
= drop_from_identity(field
);
2324 r
= json_variant_set_field_string(&arg_identity_extra
, field
, optarg
);
2326 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2331 case ARG_PASSWORD_HINT
:
2332 if (isempty(optarg
)) {
2333 r
= drop_from_identity("passwordHint");
2340 r
= json_variant_set_field_string(&arg_identity_extra_privileged
, "passwordHint", optarg
);
2342 return log_error_errno(r
, "Failed to set passwordHint field: %m");
2344 string_erase(optarg
);
2350 if (isempty(optarg
)) {
2351 r
= drop_from_identity("niceLevel");
2357 r
= parse_nice(optarg
, &nc
);
2359 return log_error_errno(r
, "Failed to parse nice level: %s", optarg
);
2361 r
= json_variant_set_field_integer(&arg_identity_extra
, "niceLevel", nc
);
2363 return log_error_errno(r
, "Failed to set niceLevel field: %m");
2369 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
, *jcur
= NULL
, *jmax
= NULL
;
2370 _cleanup_free_
char *field
= NULL
, *t
= NULL
;
2375 if (isempty(optarg
)) {
2376 /* Remove all resource limits */
2378 r
= drop_from_identity("resourceLimits");
2382 arg_identity_filter_rlimits
= strv_free(arg_identity_filter_rlimits
);
2383 arg_identity_extra_rlimits
= json_variant_unref(arg_identity_extra_rlimits
);
2387 eq
= strchr(optarg
, '=');
2389 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Can't parse resource limit assignment: %s", optarg
);
2391 field
= strndup(optarg
, eq
- optarg
);
2395 l
= rlimit_from_string_harder(field
);
2397 return log_error_errno(l
, "Unknown resource limit type: %s", field
);
2399 if (isempty(eq
+ 1)) {
2400 /* Remove only the specific rlimit */
2402 r
= strv_extend(&arg_identity_filter_rlimits
, rlimit_to_string(l
));
2406 r
= json_variant_filter(&arg_identity_extra_rlimits
, STRV_MAKE(field
));
2408 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
2413 r
= rlimit_parse(l
, eq
+ 1, &rl
);
2415 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to parse resource limit value: %s", eq
+ 1);
2417 r
= rl
.rlim_cur
== RLIM_INFINITY
? json_variant_new_null(&jcur
) : json_variant_new_unsigned(&jcur
, rl
.rlim_cur
);
2419 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to allocate current integer: %m");
2421 r
= rl
.rlim_max
== RLIM_INFINITY
? json_variant_new_null(&jmax
) : json_variant_new_unsigned(&jmax
, rl
.rlim_max
);
2423 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to allocate maximum integer: %m");
2427 JSON_BUILD_PAIR("cur", JSON_BUILD_VARIANT(jcur
)),
2428 JSON_BUILD_PAIR("max", JSON_BUILD_VARIANT(jmax
))));
2430 return log_error_errno(r
, "Failed to build resource limit: %m");
2432 t
= strjoin("RLIMIT_", rlimit_to_string(l
));
2436 r
= json_variant_set_field(&arg_identity_extra_rlimits
, t
, v
);
2438 return log_error_errno(r
, "Failed to set %s field: %m", rlimit_to_string(l
));
2446 if (isempty(optarg
)) {
2447 r
= drop_from_identity("uid");
2454 r
= parse_uid(optarg
, &uid
);
2456 return log_error_errno(r
, "Failed to parse UID '%s'.", optarg
);
2458 if (uid_is_system(uid
))
2459 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "UID " UID_FMT
" is in system range, refusing.", uid
);
2460 if (uid_is_dynamic(uid
))
2461 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "UID " UID_FMT
" is in dynamic range, refusing.", uid
);
2462 if (uid
== UID_NOBODY
)
2463 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "UID " UID_FMT
" is nobody UID, refusing.", uid
);
2465 r
= json_variant_set_field_unsigned(&arg_identity_extra
, "uid", uid
);
2467 return log_error_errno(r
, "Failed to set realm field: %m");
2473 case ARG_IMAGE_PATH
: {
2474 const char *field
= c
== 'k' ? "skeletonDirectory" : "imagePath";
2475 _cleanup_free_
char *v
= NULL
;
2477 if (isempty(optarg
)) {
2478 r
= drop_from_identity(field
);
2485 r
= parse_path_argument(optarg
, false, &v
);
2489 r
= json_variant_set_field_string(&arg_identity_extra_this_machine
, field
, v
);
2491 return log_error_errno(r
, "Failed to set %s field: %m", v
);
2497 if (isempty(optarg
)) {
2498 r
= drop_from_identity("shell");
2505 if (!valid_shell(optarg
))
2506 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Shell '%s' not valid.", optarg
);
2508 r
= json_variant_set_field_string(&arg_identity_extra
, "shell", optarg
);
2510 return log_error_errno(r
, "Failed to set shell field: %m");
2515 _cleanup_free_
char **l
= NULL
;
2516 _cleanup_(json_variant_unrefp
) JsonVariant
*ne
= NULL
;
2519 if (isempty(optarg
)) {
2520 r
= drop_from_identity("environment");
2527 if (!env_assignment_is_valid(optarg
))
2528 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2529 "Environment assignment '%s' not valid.", optarg
);
2531 e
= json_variant_by_key(arg_identity_extra
, "environment");
2533 r
= json_variant_strv(e
, &l
);
2535 return log_error_errno(r
, "Failed to parse JSON environment field: %m");
2538 r
= strv_env_replace_strdup(&l
, optarg
);
2540 return log_error_errno(r
, "Failed to replace JSON environment field: %m");
2544 r
= json_variant_new_array_strv(&ne
, l
);
2546 return log_error_errno(r
, "Failed to allocate environment list JSON: %m");
2548 r
= json_variant_set_field(&arg_identity_extra
, "environment", ne
);
2550 return log_error_errno(r
, "Failed to set environment list: %m");
2557 if (isempty(optarg
)) {
2558 r
= drop_from_identity("timeZone");
2565 if (!timezone_is_valid(optarg
, LOG_DEBUG
))
2566 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Timezone '%s' is not valid.", optarg
);
2568 r
= json_variant_set_field_string(&arg_identity_extra
, "timeZone", optarg
);
2570 return log_error_errno(r
, "Failed to set timezone field: %m");
2575 if (isempty(optarg
)) {
2576 r
= drop_from_identity("language");
2583 if (!locale_is_valid(optarg
))
2584 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Locale '%s' is not valid.", optarg
);
2586 if (locale_is_installed(optarg
) <= 0)
2587 log_warning("Locale '%s' is not installed, accepting anyway.", optarg
);
2589 r
= json_variant_set_field_string(&arg_identity_extra
, "preferredLanguage", optarg
);
2591 return log_error_errno(r
, "Failed to set preferredLanguage field: %m");
2599 case ARG_KILL_PROCESSES
:
2600 case ARG_ENFORCE_PASSWORD_POLICY
:
2601 case ARG_AUTO_LOGIN
:
2602 case ARG_PASSWORD_CHANGE_NOW
: {
2604 c
== ARG_LOCKED
? "locked" :
2605 c
== ARG_NOSUID
? "mountNoSuid" :
2606 c
== ARG_NODEV
? "mountNoDevices" :
2607 c
== ARG_NOEXEC
? "mountNoExecute" :
2608 c
== ARG_KILL_PROCESSES
? "killProcesses" :
2609 c
== ARG_ENFORCE_PASSWORD_POLICY
? "enforcePasswordPolicy" :
2610 c
== ARG_AUTO_LOGIN
? "autoLogin" :
2611 c
== ARG_PASSWORD_CHANGE_NOW
? "passwordChangeNow" :
2616 if (isempty(optarg
)) {
2617 r
= drop_from_identity(field
);
2624 r
= parse_boolean(optarg
);
2626 return log_error_errno(r
, "Failed to parse %s boolean: %m", field
);
2628 r
= json_variant_set_field_boolean(&arg_identity_extra
, field
, r
> 0);
2630 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2636 r
= json_variant_set_field_boolean(&arg_identity_extra
, "enforcePasswordPolicy", false);
2638 return log_error_errno(r
, "Failed to set enforcePasswordPolicy field: %m");
2643 if (isempty(optarg
)) {
2644 r
= drop_from_identity("diskSize");
2648 r
= drop_from_identity("diskSizeRelative");
2652 arg_disk_size
= arg_disk_size_relative
= UINT64_MAX
;
2656 r
= parse_permille(optarg
);
2658 r
= parse_size(optarg
, 1024, &arg_disk_size
);
2660 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Disk size '%s' not valid.", optarg
);
2662 r
= drop_from_identity("diskSizeRelative");
2666 r
= json_variant_set_field_unsigned(&arg_identity_extra_this_machine
, "diskSize", arg_disk_size
);
2668 return log_error_errno(r
, "Failed to set diskSize field: %m");
2670 arg_disk_size_relative
= UINT64_MAX
;
2672 /* Normalize to UINT32_MAX == 100% */
2673 arg_disk_size_relative
= (uint64_t) r
* UINT32_MAX
/ 1000U;
2675 r
= drop_from_identity("diskSize");
2679 r
= json_variant_set_field_unsigned(&arg_identity_extra_this_machine
, "diskSizeRelative", arg_disk_size_relative
);
2681 return log_error_errno(r
, "Failed to set diskSizeRelative field: %m");
2683 arg_disk_size
= UINT64_MAX
;
2688 case ARG_ACCESS_MODE
: {
2691 if (isempty(optarg
)) {
2692 r
= drop_from_identity("accessMode");
2699 r
= parse_mode(optarg
, &mode
);
2701 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Access mode '%s' not valid.", optarg
);
2703 r
= json_variant_set_field_unsigned(&arg_identity_extra
, "accessMode", mode
);
2705 return log_error_errno(r
, "Failed to set access mode field: %m");
2710 case ARG_LUKS_DISCARD
:
2711 if (isempty(optarg
)) {
2712 r
= drop_from_identity("luksDiscard");
2719 r
= parse_boolean(optarg
);
2721 return log_error_errno(r
, "Failed to parse --luks-discard= parameter: %s", optarg
);
2723 r
= json_variant_set_field_boolean(&arg_identity_extra
, "luksDiscard", r
);
2725 return log_error_errno(r
, "Failed to set discard field: %m");
2729 case ARG_LUKS_OFFLINE_DISCARD
:
2730 if (isempty(optarg
)) {
2731 r
= drop_from_identity("luksOfflineDiscard");
2738 r
= parse_boolean(optarg
);
2740 return log_error_errno(r
, "Failed to parse --luks-offline-discard= parameter: %s", optarg
);
2742 r
= json_variant_set_field_boolean(&arg_identity_extra
, "luksOfflineDiscard", r
);
2744 return log_error_errno(r
, "Failed to set offline discard field: %m");
2748 case ARG_LUKS_VOLUME_KEY_SIZE
:
2749 case ARG_LUKS_PBKDF_PARALLEL_THREADS
:
2750 case ARG_RATE_LIMIT_BURST
: {
2752 c
== ARG_LUKS_VOLUME_KEY_SIZE
? "luksVolumeKeySize" :
2753 c
== ARG_LUKS_PBKDF_PARALLEL_THREADS
? "luksPbkdfParallelThreads" :
2754 c
== ARG_RATE_LIMIT_BURST
? "rateLimitBurst" : NULL
;
2759 if (isempty(optarg
)) {
2760 r
= drop_from_identity(field
);
2765 r
= safe_atou(optarg
, &n
);
2767 return log_error_errno(r
, "Failed to parse %s parameter: %s", field
, optarg
);
2769 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, n
);
2771 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2779 if (isempty(optarg
)) {
2780 r
= drop_from_identity("umask");
2787 r
= parse_mode(optarg
, &m
);
2789 return log_error_errno(r
, "Failed to parse umask: %m");
2791 r
= json_variant_set_field_integer(&arg_identity_extra
, "umask", m
);
2793 return log_error_errno(r
, "Failed to set umask field: %m");
2798 case ARG_SSH_AUTHORIZED_KEYS
: {
2799 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
2800 _cleanup_(strv_freep
) char **l
= NULL
, **add
= NULL
;
2802 if (isempty(optarg
)) {
2803 r
= drop_from_identity("sshAuthorizedKeys");
2810 if (optarg
[0] == '@') {
2811 _cleanup_fclose_
FILE *f
= NULL
;
2813 /* If prefixed with '@' read from a file */
2815 f
= fopen(optarg
+1, "re");
2817 return log_error_errno(errno
, "Failed to open '%s': %m", optarg
+1);
2820 _cleanup_free_
char *line
= NULL
;
2822 r
= read_line(f
, LONG_LINE_MAX
, &line
);
2824 return log_error_errno(r
, "Failed to read from '%s': %m", optarg
+1);
2834 r
= strv_consume(&add
, TAKE_PTR(line
));
2839 /* Otherwise, assume it's a literal key. Let's do some superficial checks
2840 * before accept it though. */
2842 if (string_has_cc(optarg
, NULL
))
2843 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Authorized key contains control characters, refusing.");
2844 if (optarg
[0] == '#')
2845 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Specified key is a comment?");
2847 add
= strv_new(optarg
);
2852 v
= json_variant_ref(json_variant_by_key(arg_identity_extra_privileged
, "sshAuthorizedKeys"));
2854 r
= json_variant_strv(v
, &l
);
2856 return log_error_errno(r
, "Failed to parse SSH authorized keys list: %m");
2859 r
= strv_extend_strv(&l
, add
, true);
2863 v
= json_variant_unref(v
);
2865 r
= json_variant_new_array_strv(&v
, l
);
2869 r
= json_variant_set_field(&arg_identity_extra_privileged
, "sshAuthorizedKeys", v
);
2871 return log_error_errno(r
, "Failed to set authorized keys: %m");
2876 case ARG_NOT_BEFORE
:
2882 field
= c
== ARG_NOT_BEFORE
? "notBeforeUSec" :
2883 IN_SET(c
, ARG_NOT_AFTER
, 'e') ? "notAfterUSec" : NULL
;
2887 if (isempty(optarg
)) {
2888 r
= drop_from_identity(field
);
2895 /* Note the minor discrepancy regarding -e parsing here: we support that for compat
2896 * reasons, and in the original useradd(8) implementation it accepts dates in the
2897 * format YYYY-MM-DD. Coincidentally, we accept dates formatted like that too, but
2898 * with greater precision. */
2899 r
= parse_timestamp(optarg
, &n
);
2901 return log_error_errno(r
, "Failed to parse %s parameter: %m", field
);
2903 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, n
);
2905 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2909 case ARG_PASSWORD_CHANGE_MIN
:
2910 case ARG_PASSWORD_CHANGE_MAX
:
2911 case ARG_PASSWORD_CHANGE_WARN
:
2912 case ARG_PASSWORD_CHANGE_INACTIVE
: {
2916 field
= c
== ARG_PASSWORD_CHANGE_MIN
? "passwordChangeMinUSec" :
2917 c
== ARG_PASSWORD_CHANGE_MAX
? "passwordChangeMaxUSec" :
2918 c
== ARG_PASSWORD_CHANGE_WARN
? "passwordChangeWarnUSec" :
2919 c
== ARG_PASSWORD_CHANGE_INACTIVE
? "passwordChangeInactiveUSec" :
2924 if (isempty(optarg
)) {
2925 r
= drop_from_identity(field
);
2932 r
= parse_sec(optarg
, &n
);
2934 return log_error_errno(r
, "Failed to parse %s parameter: %m", field
);
2936 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, n
);
2938 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2944 case ARG_LUKS_CIPHER
:
2945 case ARG_LUKS_CIPHER_MODE
:
2946 case ARG_LUKS_PBKDF_TYPE
:
2947 case ARG_LUKS_PBKDF_HASH_ALGORITHM
: {
2950 c
== ARG_STORAGE
? "storage" :
2951 c
== ARG_FS_TYPE
? "fileSystemType" :
2952 c
== ARG_LUKS_CIPHER
? "luksCipher" :
2953 c
== ARG_LUKS_CIPHER_MODE
? "luksCipherMode" :
2954 c
== ARG_LUKS_PBKDF_TYPE
? "luksPbkdfType" :
2955 c
== ARG_LUKS_PBKDF_HASH_ALGORITHM
? "luksPbkdfHashAlgorithm" : NULL
;
2959 if (isempty(optarg
)) {
2960 r
= drop_from_identity(field
);
2967 if (!string_is_safe(optarg
))
2968 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Parameter for %s field not valid: %s", field
, optarg
);
2970 r
= json_variant_set_field_string(
2971 IN_SET(c
, ARG_STORAGE
, ARG_FS_TYPE
) ?
2972 &arg_identity_extra_this_machine
:
2973 &arg_identity_extra
, field
, optarg
);
2975 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2980 case ARG_LUKS_PBKDF_TIME_COST
:
2981 case ARG_RATE_LIMIT_INTERVAL
:
2982 case ARG_STOP_DELAY
: {
2984 c
== ARG_LUKS_PBKDF_TIME_COST
? "luksPbkdfTimeCostUSec" :
2985 c
== ARG_RATE_LIMIT_INTERVAL
? "rateLimitIntervalUSec" :
2986 c
== ARG_STOP_DELAY
? "stopDelayUSec" :
2992 if (isempty(optarg
)) {
2993 r
= drop_from_identity(field
);
3000 r
= parse_sec(optarg
, &t
);
3002 return log_error_errno(r
, "Failed to parse %s field: %s", field
, optarg
);
3004 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, t
);
3006 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3012 const char *p
= optarg
;
3015 r
= drop_from_identity("memberOf");
3023 _cleanup_(json_variant_unrefp
) JsonVariant
*mo
= NULL
;
3024 _cleanup_strv_free_
char **list
= NULL
;
3025 _cleanup_free_
char *word
= NULL
;
3027 r
= extract_first_word(&p
, &word
, ",", 0);
3029 return log_error_errno(r
, "Failed to parse group list: %m");
3033 if (!valid_user_group_name(word
, 0))
3034 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Invalid group name %s.", word
);
3036 mo
= json_variant_ref(json_variant_by_key(arg_identity_extra
, "memberOf"));
3038 r
= json_variant_strv(mo
, &list
);
3040 return log_error_errno(r
, "Failed to parse group list: %m");
3042 r
= strv_extend(&list
, word
);
3049 mo
= json_variant_unref(mo
);
3050 r
= json_variant_new_array_strv(&mo
, list
);
3052 return log_error_errno(r
, "Failed to create group list JSON: %m");
3054 r
= json_variant_set_field(&arg_identity_extra
, "memberOf", mo
);
3056 return log_error_errno(r
, "Failed to update group list: %m");
3062 case ARG_TASKS_MAX
: {
3065 if (isempty(optarg
)) {
3066 r
= drop_from_identity("tasksMax");
3072 r
= safe_atou64(optarg
, &u
);
3074 return log_error_errno(r
, "Failed to parse --tasks-max= parameter: %s", optarg
);
3076 r
= json_variant_set_field_unsigned(&arg_identity_extra
, "tasksMax", u
);
3078 return log_error_errno(r
, "Failed to set tasksMax field: %m");
3083 case ARG_MEMORY_MAX
:
3084 case ARG_MEMORY_HIGH
:
3085 case ARG_LUKS_PBKDF_MEMORY_COST
: {
3087 c
== ARG_MEMORY_MAX
? "memoryMax" :
3088 c
== ARG_MEMORY_HIGH
? "memoryHigh" :
3089 c
== ARG_LUKS_PBKDF_MEMORY_COST
? "luksPbkdfMemoryCost" : NULL
;
3095 if (isempty(optarg
)) {
3096 r
= drop_from_identity(field
);
3102 r
= parse_size(optarg
, 1024, &u
);
3104 return log_error_errno(r
, "Failed to parse %s parameter: %s", field
, optarg
);
3106 r
= json_variant_set_field_unsigned(&arg_identity_extra_this_machine
, field
, u
);
3108 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3113 case ARG_CPU_WEIGHT
:
3114 case ARG_IO_WEIGHT
: {
3115 const char *field
= c
== ARG_CPU_WEIGHT
? "cpuWeight" :
3116 c
== ARG_IO_WEIGHT
? "ioWeight" : NULL
;
3121 if (isempty(optarg
)) {
3122 r
= drop_from_identity(field
);
3128 r
= safe_atou64(optarg
, &u
);
3130 return log_error_errno(r
, "Failed to parse --cpu-weight=/--io-weight= parameter: %s", optarg
);
3132 if (!CGROUP_WEIGHT_IS_OK(u
))
3133 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Weight %" PRIu64
" is out of valid weight range.", u
);
3135 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, u
);
3137 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3142 case ARG_PKCS11_TOKEN_URI
: {
3145 if (streq(optarg
, "list"))
3146 return pkcs11_list_tokens();
3148 /* If --pkcs11-token-uri= is specified we always drop everything old */
3149 FOREACH_STRING(p
, "pkcs11TokenUri", "pkcs11EncryptedKey") {
3150 r
= drop_from_identity(p
);
3155 if (isempty(optarg
)) {
3156 arg_pkcs11_token_uri
= strv_free(arg_pkcs11_token_uri
);
3160 if (streq(optarg
, "auto")) {
3161 _cleanup_free_
char *found
= NULL
;
3163 r
= pkcs11_find_token_auto(&found
);
3166 r
= strv_consume(&arg_pkcs11_token_uri
, TAKE_PTR(found
));
3168 if (!pkcs11_uri_valid(optarg
))
3169 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Not a valid PKCS#11 URI: %s", optarg
);
3171 r
= strv_extend(&arg_pkcs11_token_uri
, optarg
);
3176 strv_uniq(arg_pkcs11_token_uri
);
3180 case ARG_FIDO2_DEVICE
: {
3183 if (streq(optarg
, "list"))
3184 return fido2_list_devices();
3186 FOREACH_STRING(p
, "fido2HmacCredential", "fido2HmacSalt") {
3187 r
= drop_from_identity(p
);
3192 if (isempty(optarg
)) {
3193 arg_fido2_device
= strv_free(arg_fido2_device
);
3197 if (streq(optarg
, "auto")) {
3198 _cleanup_free_
char *found
= NULL
;
3200 r
= fido2_find_device_auto(&found
);
3204 r
= strv_consume(&arg_fido2_device
, TAKE_PTR(found
));
3206 r
= strv_extend(&arg_fido2_device
, optarg
);
3211 strv_uniq(arg_fido2_device
);
3215 case ARG_RECOVERY_KEY
: {
3218 r
= parse_boolean(optarg
);
3220 return log_error_errno(r
, "Failed to parse --recovery-key= argument: %s", optarg
);
3222 arg_recovery_key
= r
;
3224 FOREACH_STRING(p
, "recoveryKey", "recoveryKeyType") {
3225 r
= drop_from_identity(p
);
3234 arg_json_format_flags
= JSON_FORMAT_PRETTY_AUTO
|JSON_FORMAT_COLOR_AUTO
;
3238 r
= parse_json_argument(optarg
, &arg_json_format_flags
);
3245 if (arg_export_format
== EXPORT_FORMAT_FULL
)
3246 arg_export_format
= EXPORT_FORMAT_STRIPPED
;
3247 else if (arg_export_format
== EXPORT_FORMAT_STRIPPED
)
3248 arg_export_format
= EXPORT_FORMAT_MINIMAL
;
3250 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Specifying -E more than twice is not supported.");
3252 arg_json_format_flags
&= ~JSON_FORMAT_OFF
;
3253 if (arg_json_format_flags
== 0)
3254 arg_json_format_flags
= JSON_FORMAT_PRETTY_AUTO
|JSON_FORMAT_COLOR_AUTO
;
3257 case ARG_EXPORT_FORMAT
:
3258 if (streq(optarg
, "full"))
3259 arg_export_format
= EXPORT_FORMAT_FULL
;
3260 else if (streq(optarg
, "stripped"))
3261 arg_export_format
= EXPORT_FORMAT_STRIPPED
;
3262 else if (streq(optarg
, "minimal"))
3263 arg_export_format
= EXPORT_FORMAT_MINIMAL
;
3264 else if (streq(optarg
, "help")) {
3273 case ARG_AND_RESIZE
:
3274 arg_and_resize
= true;
3277 case ARG_AND_CHANGE_PASSWORD
:
3278 arg_and_change_password
= true;
3285 assert_not_reached("Unhandled option");
3289 if (!strv_isempty(arg_pkcs11_token_uri
) || !strv_isempty(arg_fido2_device
))
3290 arg_and_change_password
= true;
3292 if (arg_disk_size
!= UINT64_MAX
|| arg_disk_size_relative
!= UINT64_MAX
)
3293 arg_and_resize
= true;
3298 static int redirect_bus_mgr(void) {
3301 /* Talk to a different service if that's requested. (The same env var is also understood by homed, so
3302 * that it is relatively easily possible to invoke a second instance of homed for debug purposes and
3303 * have homectl talk to it, without colliding with the host version. This is handy when operating
3304 * from a homed-managed account.) */
3306 suffix
= getenv("SYSTEMD_HOME_DEBUG_SUFFIX");
3308 static BusLocator locator
= {
3309 .path
= "/org/freedesktop/home1",
3310 .interface
= "org.freedesktop.home1.Manager",
3313 /* Yes, we leak this memory, but there's little point to collect this, given that we only do
3314 * this in a debug environment, do it only once, and the string shall live for out entire
3315 * process runtime. */
3317 locator
.destination
= strjoin("org.freedesktop.home1.", suffix
);
3318 if (!locator
.destination
)
3323 bus_mgr
= bus_home_mgr
;
3328 static int run(int argc
, char *argv
[]) {
3329 static const Verb verbs
[] = {
3330 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
3331 { "list", VERB_ANY
, 1, VERB_DEFAULT
, list_homes
},
3332 { "activate", 2, VERB_ANY
, 0, activate_home
},
3333 { "deactivate", 2, VERB_ANY
, 0, deactivate_home
},
3334 { "inspect", VERB_ANY
, VERB_ANY
, 0, inspect_home
},
3335 { "authenticate", VERB_ANY
, VERB_ANY
, 0, authenticate_home
},
3336 { "create", VERB_ANY
, 2, 0, create_home
},
3337 { "remove", 2, VERB_ANY
, 0, remove_home
},
3338 { "update", VERB_ANY
, 2, 0, update_home
},
3339 { "passwd", VERB_ANY
, 2, 0, passwd_home
},
3340 { "resize", 2, 3, 0, resize_home
},
3341 { "lock", 2, VERB_ANY
, 0, lock_home
},
3342 { "unlock", 2, VERB_ANY
, 0, unlock_home
},
3343 { "with", 2, VERB_ANY
, 0, with_home
},
3344 { "lock-all", VERB_ANY
, 1, 0, lock_all_homes
},
3345 { "deactivate-all", VERB_ANY
, 1, 0, deactivate_all_homes
},
3353 r
= redirect_bus_mgr();
3357 r
= parse_argv(argc
, argv
);
3361 return dispatch_verb(argc
, argv
, verbs
, NULL
);
3364 DEFINE_MAIN_FUNCTION(run
);