1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
7 #include "ask-password-api.h"
8 #include "bus-common-errors.h"
10 #include "bus-locator.h"
11 #include "cgroup-util.h"
12 #include "dns-domain.h"
16 #include "format-table.h"
17 #include "home-util.h"
18 #include "homectl-fido2.h"
19 #include "homectl-pkcs11.h"
20 #include "homectl-recovery-key.h"
21 #include "libfido2-util.h"
22 #include "locale-util.h"
23 #include "main-func.h"
24 #include "memory-util.h"
26 #include "parse-argument.h"
27 #include "parse-util.h"
28 #include "path-util.h"
29 #include "percent-util.h"
30 #include "pkcs11-util.h"
31 #include "pretty-print.h"
32 #include "process-util.h"
33 #include "pwquality-util.h"
34 #include "rlimit-util.h"
35 #include "spawn-polkit-agent.h"
36 #include "terminal-util.h"
37 #include "user-record-pwquality.h"
38 #include "user-record-show.h"
39 #include "user-record-util.h"
40 #include "user-record.h"
41 #include "user-util.h"
44 static PagerFlags arg_pager_flags
= 0;
45 static bool arg_legend
= true;
46 static bool arg_ask_password
= true;
47 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
48 static const char *arg_host
= NULL
;
49 static const char *arg_identity
= NULL
;
50 static JsonVariant
*arg_identity_extra
= NULL
;
51 static JsonVariant
*arg_identity_extra_privileged
= NULL
;
52 static JsonVariant
*arg_identity_extra_this_machine
= NULL
;
53 static JsonVariant
*arg_identity_extra_rlimits
= NULL
;
54 static char **arg_identity_filter
= NULL
; /* this one is also applied to 'privileged' and 'thisMachine' subobjects */
55 static char **arg_identity_filter_rlimits
= NULL
;
56 static uint64_t arg_disk_size
= UINT64_MAX
;
57 static uint64_t arg_disk_size_relative
= UINT64_MAX
;
58 static char **arg_pkcs11_token_uri
= NULL
;
59 static char **arg_fido2_device
= NULL
;
60 static bool arg_recovery_key
= false;
61 static JsonFormatFlags arg_json_format_flags
= JSON_FORMAT_OFF
;
62 static bool arg_and_resize
= false;
63 static bool arg_and_change_password
= false;
65 EXPORT_FORMAT_FULL
, /* export the full record */
66 EXPORT_FORMAT_STRIPPED
, /* strip "state" + "binding", but leave signature in place */
67 EXPORT_FORMAT_MINIMAL
, /* also strip signature */
68 } arg_export_format
= EXPORT_FORMAT_FULL
;
70 STATIC_DESTRUCTOR_REGISTER(arg_identity_extra
, json_variant_unrefp
);
71 STATIC_DESTRUCTOR_REGISTER(arg_identity_extra_this_machine
, json_variant_unrefp
);
72 STATIC_DESTRUCTOR_REGISTER(arg_identity_extra_privileged
, json_variant_unrefp
);
73 STATIC_DESTRUCTOR_REGISTER(arg_identity_extra_rlimits
, json_variant_unrefp
);
74 STATIC_DESTRUCTOR_REGISTER(arg_identity_filter
, strv_freep
);
75 STATIC_DESTRUCTOR_REGISTER(arg_identity_filter_rlimits
, strv_freep
);
76 STATIC_DESTRUCTOR_REGISTER(arg_pkcs11_token_uri
, strv_freep
);
77 STATIC_DESTRUCTOR_REGISTER(arg_fido2_device
, strv_freep
);
79 static const BusLocator
*bus_mgr
;
81 static bool identity_properties_specified(void) {
84 !json_variant_is_blank_object(arg_identity_extra
) ||
85 !json_variant_is_blank_object(arg_identity_extra_privileged
) ||
86 !json_variant_is_blank_object(arg_identity_extra_this_machine
) ||
87 !json_variant_is_blank_object(arg_identity_extra_rlimits
) ||
88 !strv_isempty(arg_identity_filter
) ||
89 !strv_isempty(arg_identity_filter_rlimits
) ||
90 !strv_isempty(arg_pkcs11_token_uri
) ||
91 !strv_isempty(arg_fido2_device
);
94 static int acquire_bus(sd_bus
**bus
) {
102 r
= bus_connect_transport(arg_transport
, arg_host
, false, bus
);
104 return bus_log_connect_error(r
);
106 (void) sd_bus_set_allow_interactive_authorization(*bus
, arg_ask_password
);
111 static int list_homes(int argc
, char *argv
[], void *userdata
) {
112 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
113 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
114 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
115 _cleanup_(table_unrefp
) Table
*table
= NULL
;
118 r
= acquire_bus(&bus
);
122 r
= bus_call_method(bus
, bus_mgr
, "ListHomes", &error
, &reply
, NULL
);
124 return log_error_errno(r
, "Failed to list homes: %s", bus_error_message(&error
, r
));
126 table
= table_new("name", "uid", "gid", "state", "realname", "home", "shell");
130 r
= sd_bus_message_enter_container(reply
, 'a', "(susussso)");
132 return bus_log_parse_error(r
);
135 const char *name
, *state
, *realname
, *home
, *shell
, *color
;
139 r
= sd_bus_message_read(reply
, "(susussso)", &name
, &uid
, &state
, &gid
, &realname
, &home
, &shell
, NULL
);
141 return bus_log_parse_error(r
);
145 r
= table_add_many(table
,
150 return table_log_add_error(r
);
153 r
= table_add_cell(table
, &cell
, TABLE_STRING
, state
);
155 return table_log_add_error(r
);
157 color
= user_record_state_color(state
);
159 (void) table_set_color(table
, cell
, color
);
161 r
= table_add_many(table
,
162 TABLE_STRING
, strna(empty_to_null(realname
)),
164 TABLE_STRING
, strna(empty_to_null(shell
)));
166 return table_log_add_error(r
);
169 r
= sd_bus_message_exit_container(reply
);
171 return bus_log_parse_error(r
);
173 if (table_get_rows(table
) > 1 || !FLAGS_SET(arg_json_format_flags
, JSON_FORMAT_OFF
)) {
174 r
= table_set_sort(table
, (size_t) 0);
176 return table_log_sort_error(r
);
178 r
= table_print_with_pager(table
, arg_json_format_flags
, arg_pager_flags
, arg_legend
);
183 if (arg_legend
&& (arg_json_format_flags
& JSON_FORMAT_OFF
)) {
184 if (table_get_rows(table
) > 1)
185 printf("\n%zu home areas listed.\n", table_get_rows(table
) - 1);
187 printf("No home areas.\n");
193 static int acquire_existing_password(const char *user_name
, UserRecord
*hr
, bool emphasize_current
) {
194 _cleanup_(strv_free_erasep
) char **password
= NULL
;
195 _cleanup_free_
char *question
= NULL
;
202 e
= getenv("PASSWORD");
204 /* People really shouldn't use environment variables for passing passwords. We support this
205 * only for testing purposes, and do not document the behaviour, so that people won't
206 * actually use this outside of testing. */
208 r
= user_record_set_password(hr
, STRV_MAKE(e
), true);
210 return log_error_errno(r
, "Failed to store password: %m");
213 assert_se(unsetenv("PASSWORD") == 0);
218 if (asprintf(&question
, emphasize_current
?
219 "Please enter current password for user %s:" :
220 "Please enter password for user %s:",
224 r
= ask_password_auto(question
, "user-home", NULL
, "home-password", "home.password", USEC_INFINITY
, ASK_PASSWORD_ACCEPT_CACHED
|ASK_PASSWORD_PUSH_CACHE
, &password
);
226 return log_error_errno(r
, "Failed to acquire password: %m");
228 r
= user_record_set_password(hr
, password
, true);
230 return log_error_errno(r
, "Failed to store password: %m");
235 static int acquire_token_pin(const char *user_name
, UserRecord
*hr
) {
236 _cleanup_(strv_free_erasep
) char **pin
= NULL
;
237 _cleanup_free_
char *question
= NULL
;
246 r
= user_record_set_token_pin(hr
, STRV_MAKE(e
), false);
248 return log_error_errno(r
, "Failed to store token PIN: %m");
251 assert_se(unsetenv("PIN") == 0);
256 if (asprintf(&question
, "Please enter security token PIN for user %s:", user_name
) < 0)
259 /* We never cache or use cached PINs, since usually there are only very few attempts allowed before the PIN is blocked */
260 r
= ask_password_auto(question
, "user-home", NULL
, "token-pin", "home.token-pin", USEC_INFINITY
, 0, &pin
);
262 return log_error_errno(r
, "Failed to acquire security token PIN: %m");
264 r
= user_record_set_token_pin(hr
, pin
, false);
266 return log_error_errno(r
, "Failed to store security token PIN: %m");
271 static int handle_generic_user_record_error(
272 const char *user_name
,
274 const sd_bus_error
*error
,
276 bool emphasize_current_password
) {
282 if (sd_bus_error_has_name(error
, BUS_ERROR_HOME_ABSENT
))
283 return log_error_errno(SYNTHETIC_ERRNO(EREMOTE
),
284 "Home of user %s is currently absent, please plug in the necessary storage device or backing file system.", user_name
);
286 else if (sd_bus_error_has_name(error
, BUS_ERROR_AUTHENTICATION_LIMIT_HIT
))
287 return log_error_errno(SYNTHETIC_ERRNO(ETOOMANYREFS
),
288 "Too frequent unsuccessful login attempts for user %s, try again later.", user_name
);
290 else if (sd_bus_error_has_name(error
, BUS_ERROR_BAD_PASSWORD
)) {
292 if (!strv_isempty(hr
->password
))
293 log_notice("Password incorrect or not sufficient, please try again.");
295 r
= acquire_existing_password(user_name
, hr
, emphasize_current_password
);
299 } else if (sd_bus_error_has_name(error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
)) {
301 if (strv_isempty(hr
->password
))
302 log_notice("Security token not inserted, please enter password.");
304 log_notice("Password incorrect or not sufficient, and configured security token not inserted, please try again.");
306 r
= acquire_existing_password(user_name
, hr
, emphasize_current_password
);
310 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_PIN_NEEDED
)) {
312 r
= acquire_token_pin(user_name
, hr
);
316 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_PROTECTED_AUTHENTICATION_PATH_NEEDED
)) {
318 log_notice("%s%sPlease authenticate physically on security token.",
319 emoji_enabled() ? special_glyph(SPECIAL_GLYPH_TOUCH
) : "",
320 emoji_enabled() ? " " : "");
322 r
= user_record_set_pkcs11_protected_authentication_path_permitted(hr
, true);
324 return log_error_errno(r
, "Failed to set PKCS#11 protected authentication path permitted flag: %m");
326 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_USER_PRESENCE_NEEDED
)) {
328 log_notice("%s%sAuthentication requires presence verification on security token.",
329 emoji_enabled() ? special_glyph(SPECIAL_GLYPH_TOUCH
) : "",
330 emoji_enabled() ? " " : "");
332 r
= user_record_set_fido2_user_presence_permitted(hr
, true);
334 return log_error_errno(r
, "Failed to set FIDO2 user presence permitted flag: %m");
336 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_PIN_LOCKED
))
337 return log_error_errno(SYNTHETIC_ERRNO(EPERM
), "Security token PIN is locked, please unlock it first. (Hint: Removal and re-insertion might suffice.)");
339 else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_BAD_PIN
)) {
341 log_notice("Security token PIN incorrect, please try again.");
343 r
= acquire_token_pin(user_name
, hr
);
347 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_BAD_PIN_FEW_TRIES_LEFT
)) {
349 log_notice("Security token PIN incorrect, please try again (only a few tries left!).");
351 r
= acquire_token_pin(user_name
, hr
);
355 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_BAD_PIN_ONE_TRY_LEFT
)) {
357 log_notice("Security token PIN incorrect, please try again (only one try left!).");
359 r
= acquire_token_pin(user_name
, hr
);
363 return log_error_errno(ret
, "Operation on home %s failed: %s", user_name
, bus_error_message(error
, ret
));
368 static int activate_home(int argc
, char *argv
[], void *userdata
) {
369 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
373 r
= acquire_bus(&bus
);
377 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
378 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
380 secret
= user_record_new();
385 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
386 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
388 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ActivateHome");
390 return bus_log_create_error(r
);
392 r
= sd_bus_message_append(m
, "s", *i
);
394 return bus_log_create_error(r
);
396 r
= bus_message_append_secret(m
, secret
);
398 return bus_log_create_error(r
);
400 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
402 r
= handle_generic_user_record_error(*i
, secret
, &error
, r
, false);
417 static int deactivate_home(int argc
, char *argv
[], void *userdata
) {
418 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
422 r
= acquire_bus(&bus
);
426 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
427 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
428 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
430 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "DeactivateHome");
432 return bus_log_create_error(r
);
434 r
= sd_bus_message_append(m
, "s", *i
);
436 return bus_log_create_error(r
);
438 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
440 log_error_errno(r
, "Failed to deactivate user home: %s", bus_error_message(&error
, r
));
449 static void dump_home_record(UserRecord
*hr
) {
454 if (hr
->incomplete
) {
456 log_warning("Warning: lacking rights to acquire privileged fields of user record of '%s', output incomplete.", hr
->user_name
);
459 if (arg_json_format_flags
& JSON_FORMAT_OFF
)
460 user_record_show(hr
, true);
462 _cleanup_(user_record_unrefp
) UserRecord
*stripped
= NULL
;
464 if (arg_export_format
== EXPORT_FORMAT_STRIPPED
)
465 r
= user_record_clone(hr
, USER_RECORD_EXTRACT_EMBEDDED
, &stripped
);
466 else if (arg_export_format
== EXPORT_FORMAT_MINIMAL
)
467 r
= user_record_clone(hr
, USER_RECORD_EXTRACT_SIGNABLE
, &stripped
);
471 log_warning_errno(r
, "Failed to strip user record, ignoring: %m");
475 json_variant_dump(hr
->json
, arg_json_format_flags
, stdout
, NULL
);
479 static char **mangle_user_list(char **list
, char ***ret_allocated
) {
480 _cleanup_free_
char *myself
= NULL
;
483 if (!strv_isempty(list
)) {
484 *ret_allocated
= NULL
;
488 myself
= getusername_malloc();
496 l
[0] = TAKE_PTR(myself
);
503 static int inspect_home(int argc
, char *argv
[], void *userdata
) {
504 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
505 _cleanup_(strv_freep
) char **mangled_list
= NULL
;
509 (void) pager_open(arg_pager_flags
);
511 r
= acquire_bus(&bus
);
515 items
= mangle_user_list(strv_skip(argv
, 1), &mangled_list
);
519 STRV_FOREACH(i
, items
) {
520 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
521 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
522 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
523 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
528 r
= parse_uid(*i
, &uid
);
530 if (!valid_user_group_name(*i
, 0)) {
531 log_error("Invalid user name '%s'.", *i
);
538 r
= bus_call_method(bus
, bus_mgr
, "GetUserRecordByName", &error
, &reply
, "s", *i
);
540 r
= bus_call_method(bus
, bus_mgr
, "GetUserRecordByUID", &error
, &reply
, "u", (uint32_t) uid
);
543 log_error_errno(r
, "Failed to inspect home: %s", bus_error_message(&error
, r
));
550 r
= sd_bus_message_read(reply
, "sbo", &json
, &incomplete
, NULL
);
552 bus_log_parse_error(r
);
559 r
= json_parse(json
, JSON_PARSE_SENSITIVE
, &v
, NULL
, NULL
);
561 log_error_errno(r
, "Failed to parse JSON identity: %m");
568 hr
= user_record_new();
572 r
= user_record_load(hr
, v
, USER_RECORD_LOAD_REFUSE_SECRET
|USER_RECORD_LOG
);
580 hr
->incomplete
= incomplete
;
581 dump_home_record(hr
);
587 static int authenticate_home(int argc
, char *argv
[], void *userdata
) {
588 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
589 _cleanup_(strv_freep
) char **mangled_list
= NULL
;
593 items
= mangle_user_list(strv_skip(argv
, 1), &mangled_list
);
597 r
= acquire_bus(&bus
);
601 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
603 STRV_FOREACH(i
, items
) {
604 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
606 secret
= user_record_new();
611 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
612 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
614 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "AuthenticateHome");
616 return bus_log_create_error(r
);
618 r
= sd_bus_message_append(m
, "s", *i
);
620 return bus_log_create_error(r
);
622 r
= bus_message_append_secret(m
, secret
);
624 return bus_log_create_error(r
);
626 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
628 r
= handle_generic_user_record_error(*i
, secret
, &error
, r
, false);
643 static int update_last_change(JsonVariant
**v
, bool with_password
, bool override
) {
650 n
= now(CLOCK_REALTIME
);
652 c
= json_variant_by_key(*v
, "lastChangeUSec");
657 goto update_password
;
659 if (!json_variant_is_unsigned(c
))
660 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastChangeUSec field is not an unsigned integer, refusing.");
662 u
= json_variant_unsigned(c
);
664 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastChangeUSec is from the future, can't update.");
667 r
= json_variant_set_field_unsigned(v
, "lastChangeUSec", n
);
669 return log_error_errno(r
, "Failed to update lastChangeUSec: %m");
675 c
= json_variant_by_key(*v
, "lastPasswordChangeUSec");
682 if (!json_variant_is_unsigned(c
))
683 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastPasswordChangeUSec field is not an unsigned integer, refusing.");
685 u
= json_variant_unsigned(c
);
687 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastPasswordChangeUSec is from the future, can't update.");
690 r
= json_variant_set_field_unsigned(v
, "lastPasswordChangeUSec", n
);
692 return log_error_errno(r
, "Failed to update lastPasswordChangeUSec: %m");
697 static int apply_identity_changes(JsonVariant
**_v
) {
698 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
703 v
= json_variant_ref(*_v
);
705 r
= json_variant_filter(&v
, arg_identity_filter
);
707 return log_error_errno(r
, "Failed to filter identity: %m");
709 r
= json_variant_merge(&v
, arg_identity_extra
);
711 return log_error_errno(r
, "Failed to merge identities: %m");
713 if (arg_identity_extra_this_machine
|| !strv_isempty(arg_identity_filter
)) {
714 _cleanup_(json_variant_unrefp
) JsonVariant
*per_machine
= NULL
, *mmid
= NULL
;
715 char mids
[SD_ID128_STRING_MAX
];
718 r
= sd_id128_get_machine(&mid
);
720 return log_error_errno(r
, "Failed to acquire machine ID: %m");
722 r
= json_variant_new_string(&mmid
, sd_id128_to_string(mid
, mids
));
724 return log_error_errno(r
, "Failed to allocate matchMachineId object: %m");
726 per_machine
= json_variant_ref(json_variant_by_key(v
, "perMachine"));
728 _cleanup_(json_variant_unrefp
) JsonVariant
*npm
= NULL
, *add
= NULL
;
729 _cleanup_free_ JsonVariant
**array
= NULL
;
733 if (!json_variant_is_array(per_machine
))
734 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "perMachine field is not an array, refusing.");
736 array
= new(JsonVariant
*, json_variant_elements(per_machine
) + 1);
740 JSON_VARIANT_ARRAY_FOREACH(z
, per_machine
) {
743 if (!json_variant_is_object(z
))
744 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "perMachine entry is not an object, refusing.");
748 u
= json_variant_by_key(z
, "matchMachineId");
752 if (!json_variant_equal(u
, mmid
))
755 r
= json_variant_merge(&add
, z
);
757 return log_error_errno(r
, "Failed to merge perMachine entry: %m");
762 r
= json_variant_filter(&add
, arg_identity_filter
);
764 return log_error_errno(r
, "Failed to filter perMachine: %m");
766 r
= json_variant_merge(&add
, arg_identity_extra_this_machine
);
768 return log_error_errno(r
, "Failed to merge in perMachine fields: %m");
770 if (arg_identity_filter_rlimits
|| arg_identity_extra_rlimits
) {
771 _cleanup_(json_variant_unrefp
) JsonVariant
*rlv
= NULL
;
773 rlv
= json_variant_ref(json_variant_by_key(add
, "resourceLimits"));
775 r
= json_variant_filter(&rlv
, arg_identity_filter_rlimits
);
777 return log_error_errno(r
, "Failed to filter resource limits: %m");
779 r
= json_variant_merge(&rlv
, arg_identity_extra_rlimits
);
781 return log_error_errno(r
, "Failed to set resource limits: %m");
783 if (json_variant_is_blank_object(rlv
)) {
784 r
= json_variant_filter(&add
, STRV_MAKE("resourceLimits"));
786 return log_error_errno(r
, "Failed to drop resource limits field from identity: %m");
788 r
= json_variant_set_field(&add
, "resourceLimits", rlv
);
790 return log_error_errno(r
, "Failed to update resource limits of identity: %m");
794 if (!json_variant_is_blank_object(add
)) {
795 r
= json_variant_set_field(&add
, "matchMachineId", mmid
);
797 return log_error_errno(r
, "Failed to set matchMachineId field: %m");
802 r
= json_variant_new_array(&npm
, array
, i
);
804 return log_error_errno(r
, "Failed to allocate new perMachine array: %m");
806 json_variant_unref(per_machine
);
807 per_machine
= TAKE_PTR(npm
);
809 _cleanup_(json_variant_unrefp
) JsonVariant
*item
= json_variant_ref(arg_identity_extra_this_machine
);
811 if (arg_identity_extra_rlimits
) {
812 r
= json_variant_set_field(&item
, "resourceLimits", arg_identity_extra_rlimits
);
814 return log_error_errno(r
, "Failed to update resource limits of identity: %m");
817 r
= json_variant_set_field(&item
, "matchMachineId", mmid
);
819 return log_error_errno(r
, "Failed to set matchMachineId field: %m");
821 r
= json_variant_append_array(&per_machine
, item
);
823 return log_error_errno(r
, "Failed to append to perMachine array: %m");
826 r
= json_variant_set_field(&v
, "perMachine", per_machine
);
828 return log_error_errno(r
, "Failed to update per machine record: %m");
831 if (arg_identity_extra_privileged
|| arg_identity_filter
) {
832 _cleanup_(json_variant_unrefp
) JsonVariant
*privileged
= NULL
;
834 privileged
= json_variant_ref(json_variant_by_key(v
, "privileged"));
836 r
= json_variant_filter(&privileged
, arg_identity_filter
);
838 return log_error_errno(r
, "Failed to filter identity (privileged part): %m");
840 r
= json_variant_merge(&privileged
, arg_identity_extra_privileged
);
842 return log_error_errno(r
, "Failed to merge identities (privileged part): %m");
844 if (json_variant_is_blank_object(privileged
)) {
845 r
= json_variant_filter(&v
, STRV_MAKE("privileged"));
847 return log_error_errno(r
, "Failed to drop privileged part from identity: %m");
849 r
= json_variant_set_field(&v
, "privileged", privileged
);
851 return log_error_errno(r
, "Failed to update privileged part of identity: %m");
855 if (arg_identity_filter_rlimits
) {
856 _cleanup_(json_variant_unrefp
) JsonVariant
*rlv
= NULL
;
858 rlv
= json_variant_ref(json_variant_by_key(v
, "resourceLimits"));
860 r
= json_variant_filter(&rlv
, arg_identity_filter_rlimits
);
862 return log_error_errno(r
, "Failed to filter resource limits: %m");
864 /* Note that we only filter resource limits here, but don't apply them. We do that in the perMachine section */
866 if (json_variant_is_blank_object(rlv
)) {
867 r
= json_variant_filter(&v
, STRV_MAKE("resourceLimits"));
869 return log_error_errno(r
, "Failed to drop resource limits field from identity: %m");
871 r
= json_variant_set_field(&v
, "resourceLimits", rlv
);
873 return log_error_errno(r
, "Failed to update resource limits of identity: %m");
877 json_variant_unref(*_v
);
883 static int add_disposition(JsonVariant
**v
) {
888 if (json_variant_by_key(*v
, "disposition"))
891 /* Set the disposition to regular, if not configured explicitly */
892 r
= json_variant_set_field_string(v
, "disposition", "regular");
894 return log_error_errno(r
, "Failed to set disposition field: %m");
899 static int acquire_new_home_record(UserRecord
**ret
) {
900 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
901 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
908 unsigned line
, column
;
911 streq(arg_identity
, "-") ? stdin
: NULL
,
912 streq(arg_identity
, "-") ? "<stdin>" : arg_identity
, JSON_PARSE_SENSITIVE
, &v
, &line
, &column
);
914 return log_error_errno(r
, "Failed to parse identity at %u:%u: %m", line
, column
);
917 r
= apply_identity_changes(&v
);
921 r
= add_disposition(&v
);
925 STRV_FOREACH(i
, arg_pkcs11_token_uri
) {
926 r
= identity_add_pkcs11_key_data(&v
, *i
);
931 STRV_FOREACH(i
, arg_fido2_device
) {
932 r
= identity_add_fido2_parameters(&v
, *i
);
937 if (arg_recovery_key
) {
938 r
= identity_add_recovery_key(&v
);
943 r
= update_last_change(&v
, true, false);
948 json_variant_dump(v
, JSON_FORMAT_PRETTY
, NULL
, NULL
);
950 hr
= user_record_new();
954 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
);
962 static int acquire_new_password(
963 const char *user_name
,
975 e
= getenv("NEWPASSWORD");
977 _cleanup_(erase_and_freep
) char *copy
= NULL
;
979 /* As above, this is not for use, just for testing */
987 r
= user_record_set_password(hr
, STRV_MAKE(e
), /* prepend = */ true);
989 return log_error_errno(r
, "Failed to store password: %m");
992 assert_se(unsetenv("NEWPASSWORD") == 0);
995 *ret
= TAKE_PTR(copy
);
1001 (void) suggest_passwords();
1004 _cleanup_(strv_free_erasep
) char **first
= NULL
, **second
= NULL
;
1005 _cleanup_free_
char *question
= NULL
;
1008 return log_error_errno(SYNTHETIC_ERRNO(ENOKEY
), "Too many attempts, giving up:");
1010 if (asprintf(&question
, "Please enter new password for user %s:", user_name
) < 0)
1013 r
= ask_password_auto(question
, "user-home", NULL
, "home-password", "home.new-password", USEC_INFINITY
, 0, &first
);
1015 return log_error_errno(r
, "Failed to acquire password: %m");
1017 question
= mfree(question
);
1018 if (asprintf(&question
, "Please enter new password for user %s (repeat):", user_name
) < 0)
1021 r
= ask_password_auto(question
, "user-home", NULL
, "home-password", "home.new-password", USEC_INFINITY
, 0, &second
);
1023 return log_error_errno(r
, "Failed to acquire password: %m");
1025 if (strv_equal(first
, second
)) {
1026 _cleanup_(erase_and_freep
) char *copy
= NULL
;
1029 copy
= strdup(first
[0]);
1034 r
= user_record_set_password(hr
, first
, /* prepend = */ true);
1036 return log_error_errno(r
, "Failed to store password: %m");
1039 *ret
= TAKE_PTR(copy
);
1044 log_error("Password didn't match, try again.");
1048 static int create_home(int argc
, char *argv
[], void *userdata
) {
1049 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1050 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
1053 r
= acquire_bus(&bus
);
1057 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1060 /* If a username was specified, use it */
1062 if (valid_user_group_name(argv
[1], 0))
1063 r
= json_variant_set_field_string(&arg_identity_extra
, "userName", argv
[1]);
1065 _cleanup_free_
char *un
= NULL
, *rr
= NULL
;
1067 /* Before we consider the user name invalid, let's check if we can split it? */
1068 r
= split_user_name_realm(argv
[1], &un
, &rr
);
1070 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User name '%s' is not valid: %m", argv
[1]);
1073 r
= json_variant_set_field_string(&arg_identity_extra
, "realm", rr
);
1075 return log_error_errno(r
, "Failed to set realm field: %m");
1078 r
= json_variant_set_field_string(&arg_identity_extra
, "userName", un
);
1081 return log_error_errno(r
, "Failed to set userName field: %m");
1083 /* If neither a username nor an identity have been specified we cannot operate. */
1085 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User name required.");
1088 r
= acquire_new_home_record(&hr
);
1092 /* If the JSON record carries no plain text password (besides the recovery key), then let's query it
1094 if (strv_length(hr
->password
) <= arg_recovery_key
) {
1096 if (strv_isempty(hr
->hashed_password
)) {
1097 _cleanup_(erase_and_freep
) char *new_password
= NULL
;
1099 /* No regular (i.e. non-PKCS#11) hashed passwords set in the record, let's fix that. */
1100 r
= acquire_new_password(hr
->user_name
, hr
, /* suggest = */ true, &new_password
);
1104 r
= user_record_make_hashed_password(hr
, STRV_MAKE(new_password
), /* extend = */ false);
1106 return log_error_errno(r
, "Failed to hash password: %m");
1108 /* There's a hash password set in the record, acquire the unhashed version of it. */
1109 r
= acquire_existing_password(hr
->user_name
, hr
, /* emphasize_current= */ false);
1115 if (hr
->enforce_password_policy
== 0) {
1116 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1118 /* If password quality enforcement is disabled, let's at least warn client side */
1120 r
= user_record_quality_check_password(hr
, hr
, &error
);
1122 log_warning_errno(r
, "Specified password does not pass quality checks (%s), proceeding anyway.", bus_error_message(&error
, r
));
1126 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1127 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1128 _cleanup_(erase_and_freep
) char *formatted
= NULL
;
1130 r
= json_variant_format(hr
->json
, 0, &formatted
);
1132 return log_error_errno(r
, "Failed to format user record: %m");
1134 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "CreateHome");
1136 return bus_log_create_error(r
);
1138 (void) sd_bus_message_sensitive(m
);
1140 r
= sd_bus_message_append(m
, "s", formatted
);
1142 return bus_log_create_error(r
);
1144 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1146 if (sd_bus_error_has_name(&error
, BUS_ERROR_LOW_PASSWORD_QUALITY
)) {
1147 _cleanup_(erase_and_freep
) char *new_password
= NULL
;
1149 log_error_errno(r
, "%s", bus_error_message(&error
, r
));
1150 log_info("(Use --enforce-password-policy=no to turn off password quality checks for this account.)");
1152 r
= acquire_new_password(hr
->user_name
, hr
, /* suggest = */ false, &new_password
);
1156 r
= user_record_make_hashed_password(hr
, STRV_MAKE(new_password
), /* extend = */ false);
1158 return log_error_errno(r
, "Failed to hash passwords: %m");
1160 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1171 static int remove_home(int argc
, char *argv
[], void *userdata
) {
1172 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1176 r
= acquire_bus(&bus
);
1180 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1182 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
1183 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1184 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1186 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "RemoveHome");
1188 return bus_log_create_error(r
);
1190 r
= sd_bus_message_append(m
, "s", *i
);
1192 return bus_log_create_error(r
);
1194 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1196 log_error_errno(r
, "Failed to remove home: %s", bus_error_message(&error
, r
));
1205 static int acquire_updated_home_record(
1207 const char *username
,
1210 _cleanup_(json_variant_unrefp
) JsonVariant
*json
= NULL
;
1211 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
1218 unsigned line
, column
;
1221 r
= json_parse_file(
1222 streq(arg_identity
, "-") ? stdin
: NULL
,
1223 streq(arg_identity
, "-") ? "<stdin>" : arg_identity
, JSON_PARSE_SENSITIVE
, &json
, &line
, &column
);
1225 return log_error_errno(r
, "Failed to parse identity at %u:%u: %m", line
, column
);
1227 un
= json_variant_by_key(json
, "userName");
1229 if (!json_variant_is_string(un
) || (username
&& !streq(json_variant_string(un
), username
)))
1230 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User name specified on command line and in JSON record do not match.");
1233 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "No username specified.");
1235 r
= json_variant_set_field_string(&arg_identity_extra
, "userName", username
);
1237 return log_error_errno(r
, "Failed to set userName field: %m");
1241 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1242 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1246 if (!identity_properties_specified())
1247 return log_error_errno(SYNTHETIC_ERRNO(EALREADY
), "No field to change specified.");
1249 r
= bus_call_method(bus
, bus_mgr
, "GetUserRecordByName", &error
, &reply
, "s", username
);
1251 return log_error_errno(r
, "Failed to acquire user home record: %s", bus_error_message(&error
, r
));
1253 r
= sd_bus_message_read(reply
, "sbo", &text
, &incomplete
, NULL
);
1255 return bus_log_parse_error(r
);
1258 return log_error_errno(SYNTHETIC_ERRNO(EACCES
), "Lacking rights to acquire user record including privileged metadata, can't update record.");
1260 r
= json_parse(text
, JSON_PARSE_SENSITIVE
, &json
, NULL
, NULL
);
1262 return log_error_errno(r
, "Failed to parse JSON identity: %m");
1264 reply
= sd_bus_message_unref(reply
);
1266 r
= json_variant_filter(&json
, STRV_MAKE("binding", "status", "signature"));
1268 return log_error_errno(r
, "Failed to strip binding and status from record to update: %m");
1271 r
= apply_identity_changes(&json
);
1275 STRV_FOREACH(i
, arg_pkcs11_token_uri
) {
1276 r
= identity_add_pkcs11_key_data(&json
, *i
);
1281 STRV_FOREACH(i
, arg_fido2_device
) {
1282 r
= identity_add_fido2_parameters(&json
, *i
);
1287 /* If the user supplied a full record, then add in lastChange, but do not override. Otherwise always
1289 r
= update_last_change(&json
, arg_pkcs11_token_uri
|| arg_fido2_device
, !arg_identity
);
1294 json_variant_dump(json
, JSON_FORMAT_PRETTY
, NULL
, NULL
);
1296 hr
= user_record_new();
1300 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
);
1304 *ret
= TAKE_PTR(hr
);
1308 static int home_record_reset_human_interaction_permission(UserRecord
*hr
) {
1313 /* When we execute multiple operations one after the other, let's reset the permission to ask the
1314 * user each time, so that if interaction is necessary we will be told so again and thus can print a
1315 * nice message to the user, telling the user so. */
1317 r
= user_record_set_pkcs11_protected_authentication_path_permitted(hr
, -1);
1319 return log_error_errno(r
, "Failed to reset PKCS#11 protected authentication path permission flag: %m");
1321 r
= user_record_set_fido2_user_presence_permitted(hr
, -1);
1323 return log_error_errno(r
, "Failed to reset FIDO2 user presence permission flag: %m");
1328 static int update_home(int argc
, char *argv
[], void *userdata
) {
1329 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1330 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
1331 _cleanup_free_
char *buffer
= NULL
;
1332 const char *username
;
1337 else if (!arg_identity
) {
1338 buffer
= getusername_malloc();
1346 r
= acquire_bus(&bus
);
1350 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1352 r
= acquire_updated_home_record(bus
, username
, &hr
);
1356 /* If we do multiple operations, let's output things more verbosely, since otherwise the repeated
1357 * authentication might be confusing. */
1359 if (arg_and_resize
|| arg_and_change_password
)
1360 log_info("Updating home directory.");
1363 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1364 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1365 _cleanup_free_
char *formatted
= NULL
;
1367 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "UpdateHome");
1369 return bus_log_create_error(r
);
1371 r
= json_variant_format(hr
->json
, 0, &formatted
);
1373 return log_error_errno(r
, "Failed to format user record: %m");
1375 (void) sd_bus_message_sensitive(m
);
1377 r
= sd_bus_message_append(m
, "s", formatted
);
1379 return bus_log_create_error(r
);
1381 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1383 if (arg_and_change_password
&&
1384 sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1385 /* In the generic handler we'd ask for a password in this case, but when
1386 * changing passwords that's not sufficient, as we need to acquire all keys
1388 return log_error_errno(r
, "Security token not inserted, refusing.");
1390 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1398 log_info("Resizing home.");
1400 (void) home_record_reset_human_interaction_permission(hr
);
1402 /* Also sync down disk size to underlying LUKS/fscrypt/quota */
1403 while (arg_and_resize
) {
1404 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1405 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1407 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ResizeHome");
1409 return bus_log_create_error(r
);
1411 /* Specify UINT64_MAX as size, in which case the underlying disk size will just be synced */
1412 r
= sd_bus_message_append(m
, "st", hr
->user_name
, UINT64_MAX
);
1414 return bus_log_create_error(r
);
1416 r
= bus_message_append_secret(m
, hr
);
1418 return bus_log_create_error(r
);
1420 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1422 if (arg_and_change_password
&&
1423 sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1424 return log_error_errno(r
, "Security token not inserted, refusing.");
1426 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1433 if (arg_and_change_password
)
1434 log_info("Synchronizing passwords and encryption keys.");
1436 (void) home_record_reset_human_interaction_permission(hr
);
1438 /* Also sync down passwords to underlying LUKS/fscrypt */
1439 while (arg_and_change_password
) {
1440 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1441 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1443 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ChangePasswordHome");
1445 return bus_log_create_error(r
);
1447 /* Specify an empty new secret, in which case the underlying LUKS/fscrypt password will just be synced */
1448 r
= sd_bus_message_append(m
, "ss", hr
->user_name
, "{}");
1450 return bus_log_create_error(r
);
1452 r
= bus_message_append_secret(m
, hr
);
1454 return bus_log_create_error(r
);
1456 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1458 if (sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1459 return log_error_errno(r
, "Security token not inserted, refusing.");
1461 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1471 static int passwd_home(int argc
, char *argv
[], void *userdata
) {
1472 _cleanup_(user_record_unrefp
) UserRecord
*old_secret
= NULL
, *new_secret
= NULL
;
1473 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1474 _cleanup_free_
char *buffer
= NULL
;
1475 const char *username
;
1478 if (arg_pkcs11_token_uri
)
1479 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "To change the PKCS#11 security token use 'homectl update --pkcs11-token-uri=…'.");
1480 if (arg_fido2_device
)
1481 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "To change the FIDO2 security token use 'homectl update --fido2-device=…'.");
1482 if (identity_properties_specified())
1483 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "The 'passwd' verb does not permit changing other record properties at the same time.");
1488 buffer
= getusername_malloc();
1495 r
= acquire_bus(&bus
);
1499 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1501 old_secret
= user_record_new();
1505 new_secret
= user_record_new();
1509 r
= acquire_new_password(username
, new_secret
, /* suggest = */ true, NULL
);
1514 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1515 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1517 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ChangePasswordHome");
1519 return bus_log_create_error(r
);
1521 r
= sd_bus_message_append(m
, "s", username
);
1523 return bus_log_create_error(r
);
1525 r
= bus_message_append_secret(m
, new_secret
);
1527 return bus_log_create_error(r
);
1529 r
= bus_message_append_secret(m
, old_secret
);
1531 return bus_log_create_error(r
);
1533 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1535 if (sd_bus_error_has_name(&error
, BUS_ERROR_LOW_PASSWORD_QUALITY
)) {
1537 log_error_errno(r
, "%s", bus_error_message(&error
, r
));
1539 r
= acquire_new_password(username
, new_secret
, /* suggest = */ false, NULL
);
1541 } else if (sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1543 /* In the generic handler we'd ask for a password in this case, but when
1544 * changing passwords that's not sufficeint, as we need to acquire all keys
1546 return log_error_errno(r
, "Security token not inserted, refusing.");
1548 r
= handle_generic_user_record_error(username
, old_secret
, &error
, r
, true);
1558 static int resize_home(int argc
, char *argv
[], void *userdata
) {
1559 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1560 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
1561 uint64_t ds
= UINT64_MAX
;
1564 r
= acquire_bus(&bus
);
1568 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1570 if (arg_disk_size_relative
!= UINT64_MAX
||
1571 (argc
> 2 && parse_permyriad(argv
[2]) >= 0))
1572 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
1573 "Relative disk size specification currently not supported when resizing.");
1576 r
= parse_size(argv
[2], 1024, &ds
);
1578 return log_error_errno(r
, "Failed to parse disk size parameter: %s", argv
[2]);
1581 if (arg_disk_size
!= UINT64_MAX
) {
1582 if (ds
!= UINT64_MAX
&& ds
!= arg_disk_size
)
1583 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Disk size specified twice and doesn't match, refusing.");
1588 secret
= user_record_new();
1593 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1594 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1596 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ResizeHome");
1598 return bus_log_create_error(r
);
1600 r
= sd_bus_message_append(m
, "st", argv
[1], ds
);
1602 return bus_log_create_error(r
);
1604 r
= bus_message_append_secret(m
, secret
);
1606 return bus_log_create_error(r
);
1608 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1610 r
= handle_generic_user_record_error(argv
[1], secret
, &error
, r
, false);
1620 static int lock_home(int argc
, char *argv
[], void *userdata
) {
1621 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1625 r
= acquire_bus(&bus
);
1629 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
1630 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1631 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1633 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "LockHome");
1635 return bus_log_create_error(r
);
1637 r
= sd_bus_message_append(m
, "s", *i
);
1639 return bus_log_create_error(r
);
1641 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1643 log_error_errno(r
, "Failed to lock home: %s", bus_error_message(&error
, r
));
1652 static int unlock_home(int argc
, char *argv
[], void *userdata
) {
1653 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1657 r
= acquire_bus(&bus
);
1661 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
1662 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
1664 secret
= user_record_new();
1669 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1670 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1672 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "UnlockHome");
1674 return bus_log_create_error(r
);
1676 r
= sd_bus_message_append(m
, "s", *i
);
1678 return bus_log_create_error(r
);
1680 r
= bus_message_append_secret(m
, secret
);
1682 return bus_log_create_error(r
);
1684 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1686 r
= handle_generic_user_record_error(argv
[1], secret
, &error
, r
, false);
1701 static int with_home(int argc
, char *argv
[], void *userdata
) {
1702 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1703 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1704 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1705 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
1706 _cleanup_close_
int acquired_fd
= -1;
1707 _cleanup_strv_free_
char **cmdline
= NULL
;
1712 r
= acquire_bus(&bus
);
1717 _cleanup_free_
char *shell
= NULL
;
1719 /* If no command is specified, spawn a shell */
1720 r
= get_shell(&shell
);
1722 return log_error_errno(r
, "Failed to acquire shell: %m");
1724 cmdline
= strv_new(shell
);
1726 cmdline
= strv_copy(argv
+ 2);
1730 secret
= user_record_new();
1735 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "AcquireHome");
1737 return bus_log_create_error(r
);
1739 r
= sd_bus_message_append(m
, "s", argv
[1]);
1741 return bus_log_create_error(r
);
1743 r
= bus_message_append_secret(m
, secret
);
1745 return bus_log_create_error(r
);
1747 r
= sd_bus_message_append(m
, "b", /* please_suspend = */ getenv_bool("SYSTEMD_PLEASE_SUSPEND_HOME") > 0);
1749 return bus_log_create_error(r
);
1751 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, &reply
);
1752 m
= sd_bus_message_unref(m
);
1754 r
= handle_generic_user_record_error(argv
[1], secret
, &error
, r
, false);
1758 sd_bus_error_free(&error
);
1762 r
= sd_bus_message_read(reply
, "h", &fd
);
1764 return bus_log_parse_error(r
);
1766 acquired_fd
= fcntl(fd
, F_DUPFD_CLOEXEC
, 3);
1767 if (acquired_fd
< 0)
1768 return log_error_errno(errno
, "Failed to duplicate acquired fd: %m");
1770 reply
= sd_bus_message_unref(reply
);
1775 r
= bus_call_method(bus
, bus_mgr
, "GetHomeByName", &error
, &reply
, "s", argv
[1]);
1777 return log_error_errno(r
, "Failed to inspect home: %s", bus_error_message(&error
, r
));
1779 r
= sd_bus_message_read(reply
, "usussso", NULL
, NULL
, NULL
, NULL
, &home
, NULL
, NULL
);
1781 return bus_log_parse_error(r
);
1783 r
= safe_fork("(with)", FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_DEATHSIG
|FORK_LOG
|FORK_RLIMIT_NOFILE_SAFE
|FORK_REOPEN_LOG
, &pid
);
1787 if (chdir(home
) < 0) {
1788 log_error_errno(errno
, "Failed to change to directory %s: %m", home
);
1792 execvp(cmdline
[0], cmdline
);
1793 log_error_errno(errno
, "Failed to execute %s: %m", cmdline
[0]);
1797 ret
= wait_for_terminate_and_check(cmdline
[0], pid
, WAIT_LOG_ABNORMAL
);
1799 /* Close the fd that pings the home now. */
1800 acquired_fd
= safe_close(acquired_fd
);
1802 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ReleaseHome");
1804 return bus_log_create_error(r
);
1806 r
= sd_bus_message_append(m
, "s", argv
[1]);
1808 return bus_log_create_error(r
);
1810 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1812 if (sd_bus_error_has_name(&error
, BUS_ERROR_HOME_BUSY
))
1813 log_notice("Not deactivating home directory of %s, as it is still used.", argv
[1]);
1815 return log_error_errno(r
, "Failed to release user home: %s", bus_error_message(&error
, r
));
1821 static int lock_all_homes(int argc
, char *argv
[], void *userdata
) {
1822 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1823 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1824 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1827 r
= acquire_bus(&bus
);
1831 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "LockAllHomes");
1833 return bus_log_create_error(r
);
1835 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1837 return log_error_errno(r
, "Failed to lock all homes: %s", bus_error_message(&error
, r
));
1842 static int deactivate_all_homes(int argc
, char *argv
[], void *userdata
) {
1843 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1844 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1845 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1848 r
= acquire_bus(&bus
);
1852 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "DeactivateAllHomes");
1854 return bus_log_create_error(r
);
1856 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1858 return log_error_errno(r
, "Failed to deactivate all homes: %s", bus_error_message(&error
, r
));
1863 static int drop_from_identity(const char *field
) {
1868 /* If we are called to update an identity record and drop some field, let's keep track of what to
1869 * remove from the old record */
1870 r
= strv_extend(&arg_identity_filter
, field
);
1874 /* Let's also drop the field if it was previously set to a new value on the same command line */
1875 r
= json_variant_filter(&arg_identity_extra
, STRV_MAKE(field
));
1877 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
1879 r
= json_variant_filter(&arg_identity_extra_this_machine
, STRV_MAKE(field
));
1881 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
1883 r
= json_variant_filter(&arg_identity_extra_privileged
, STRV_MAKE(field
));
1885 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
1890 static int help(int argc
, char *argv
[], void *userdata
) {
1891 _cleanup_free_
char *link
= NULL
;
1894 (void) pager_open(arg_pager_flags
);
1896 r
= terminal_urlify_man("homectl", "1", &link
);
1900 printf("%1$s [OPTIONS...] COMMAND ...\n\n"
1901 "%2$sCreate, manipulate or inspect home directories.%3$s\n"
1902 "\n%4$sCommands:%5$s\n"
1903 " list List home areas\n"
1904 " activate USER… Activate a home area\n"
1905 " deactivate USER… Deactivate a home area\n"
1906 " inspect USER… Inspect a home area\n"
1907 " authenticate USER… Authenticate a home area\n"
1908 " create USER Create a home area\n"
1909 " remove USER… Remove a home area\n"
1910 " update USER Update a home area\n"
1911 " passwd USER Change password of a home area\n"
1912 " resize USER SIZE Resize a home area\n"
1913 " lock USER… Temporarily lock an active home area\n"
1914 " unlock USER… Unlock a temporarily locked home area\n"
1915 " lock-all Lock all suitable home areas\n"
1916 " deactivate-all Deactivate all active home areas\n"
1917 " with USER [COMMAND…] Run shell or command with access to a home area\n"
1918 "\n%4$sOptions:%5$s\n"
1919 " -h --help Show this help\n"
1920 " --version Show package version\n"
1921 " --no-pager Do not pipe output into a pager\n"
1922 " --no-legend Do not show the headers and footers\n"
1923 " --no-ask-password Do not ask for system passwords\n"
1924 " -H --host=[USER@]HOST Operate on remote host\n"
1925 " -M --machine=CONTAINER Operate on local container\n"
1926 " --identity=PATH Read JSON identity from file\n"
1927 " --json=FORMAT Output inspection data in JSON (takes one of\n"
1928 " pretty, short, off)\n"
1929 " -j Equivalent to --json=pretty (on TTY) or\n"
1930 " --json=short (otherwise)\n"
1931 " --export-format= Strip JSON inspection data (full, stripped,\n"
1933 " -E When specified once equals -j --export-format=\n"
1934 " stripped, when specified twice equals\n"
1935 " -j --export-format=minimal\n"
1936 "\n%4$sGeneral User Record Properties:%5$s\n"
1937 " -c --real-name=REALNAME Real name for user\n"
1938 " --realm=REALM Realm to create user in\n"
1939 " --email-address=EMAIL Email address for user\n"
1940 " --location=LOCATION Set location of user on earth\n"
1941 " --icon-name=NAME Icon name for user\n"
1942 " -d --home-dir=PATH Home directory\n"
1943 " -u --uid=UID Numeric UID for user\n"
1944 " -G --member-of=GROUP Add user to group\n"
1945 " --skel=PATH Skeleton directory to use\n"
1946 " --shell=PATH Shell for account\n"
1947 " --setenv=VARIABLE=VALUE Set an environment variable at log-in\n"
1948 " --timezone=TIMEZONE Set a time-zone\n"
1949 " --language=LOCALE Set preferred language\n"
1950 " --ssh-authorized-keys=KEYS\n"
1951 " Specify SSH public keys\n"
1952 " --pkcs11-token-uri=URI URI to PKCS#11 security token containing\n"
1953 " private key and matching X.509 certificate\n"
1954 " --fido2-device=PATH Path to FIDO2 hidraw device with hmac-secret\n"
1956 " --recovery-key=BOOL Add a recovery key\n"
1957 "\n%4$sAccount Management User Record Properties:%5$s\n"
1958 " --locked=BOOL Set locked account state\n"
1959 " --not-before=TIMESTAMP Do not allow logins before\n"
1960 " --not-after=TIMESTAMP Do not allow logins after\n"
1961 " --rate-limit-interval=SECS\n"
1962 " Login rate-limit interval in seconds\n"
1963 " --rate-limit-burst=NUMBER\n"
1964 " Login rate-limit attempts per interval\n"
1965 "\n%4$sPassword Policy User Record Properties:%5$s\n"
1966 " --password-hint=HINT Set Password hint\n"
1967 " --enforce-password-policy=BOOL\n"
1968 " Control whether to enforce system's password\n"
1969 " policy for this user\n"
1970 " -P Equivalent to --enforce-password-password=no\n"
1971 " --password-change-now=BOOL\n"
1972 " Require the password to be changed on next login\n"
1973 " --password-change-min=TIME\n"
1974 " Require minimum time between password changes\n"
1975 " --password-change-max=TIME\n"
1976 " Require maximum time between password changes\n"
1977 " --password-change-warn=TIME\n"
1978 " How much time to warn before password expiry\n"
1979 " --password-change-inactive=TIME\n"
1980 " How much time to block password after expiry\n"
1981 "\n%4$sResource Management User Record Properties:%5$s\n"
1982 " --disk-size=BYTES Size to assign the user on disk\n"
1983 " --access-mode=MODE User home directory access mode\n"
1984 " --umask=MODE Umask for user when logging in\n"
1985 " --nice=NICE Nice level for user\n"
1986 " --rlimit=LIMIT=VALUE[:VALUE]\n"
1987 " Set resource limits\n"
1988 " --tasks-max=MAX Set maximum number of per-user tasks\n"
1989 " --memory-high=BYTES Set high memory threshold in bytes\n"
1990 " --memory-max=BYTES Set maximum memory limit\n"
1991 " --cpu-weight=WEIGHT Set CPU weight\n"
1992 " --io-weight=WEIGHT Set IO weight\n"
1993 "\n%4$sStorage User Record Properties:%5$s\n"
1994 " --storage=STORAGE Storage type to use (luks, fscrypt, directory,\n"
1995 " subvolume, cifs)\n"
1996 " --image-path=PATH Path to image file/directory\n"
1997 "\n%4$sLUKS Storage User Record Properties:%5$s\n"
1998 " --fs-type=TYPE File system type to use in case of luks\n"
1999 " storage (btrfs, ext4, xfs)\n"
2000 " --luks-discard=BOOL Whether to use 'discard' feature of file system\n"
2001 " when activated (mounted)\n"
2002 " --luks-offline-discard=BOOL\n"
2003 " Whether to trim file on logout\n"
2004 " --luks-cipher=CIPHER Cipher to use for LUKS encryption\n"
2005 " --luks-cipher-mode=MODE Cipher mode to use for LUKS encryption\n"
2006 " --luks-volume-key-size=BITS\n"
2007 " Volume key size to use for LUKS encryption\n"
2008 " --luks-pbkdf-type=TYPE Password-based Key Derivation Function to use\n"
2009 " --luks-pbkdf-hash-algorithm=ALGORITHM\n"
2010 " PBKDF hash algorithm to use\n"
2011 " --luks-pbkdf-time-cost=SECS\n"
2012 " Time cost for PBKDF in seconds\n"
2013 " --luks-pbkdf-memory-cost=BYTES\n"
2014 " Memory cost for PBKDF in bytes\n"
2015 " --luks-pbkdf-parallel-threads=NUMBER\n"
2016 " Number of parallel threads for PKBDF\n"
2017 "\n%4$sMounting User Record Properties:%5$s\n"
2018 " --nosuid=BOOL Control the 'nosuid' flag of the home mount\n"
2019 " --nodev=BOOL Control the 'nodev' flag of the home mount\n"
2020 " --noexec=BOOL Control the 'noexec' flag of the home mount\n"
2021 "\n%4$sCIFS User Record Properties:%5$s\n"
2022 " --cifs-domain=DOMAIN CIFS (Windows) domain\n"
2023 " --cifs-user-name=USER CIFS (Windows) user name\n"
2024 " --cifs-service=SERVICE CIFS (Windows) service to mount as home area\n"
2025 "\n%4$sLogin Behaviour User Record Properties:%5$s\n"
2026 " --stop-delay=SECS How long to leave user services running after\n"
2028 " --kill-processes=BOOL Whether to kill user processes when sessions\n"
2030 " --auto-login=BOOL Try to log this user in automatically\n"
2031 "\nSee the %6$s for details.\n",
2032 program_invocation_short_name
,
2042 static int parse_argv(int argc
, char *argv
[]) {
2045 ARG_VERSION
= 0x100,
2048 ARG_NO_ASK_PASSWORD
,
2058 ARG_LUKS_OFFLINE_DISCARD
,
2064 ARG_SSH_AUTHORIZED_KEYS
,
2073 ARG_LUKS_CIPHER_MODE
,
2074 ARG_LUKS_VOLUME_KEY_SIZE
,
2086 ARG_LUKS_PBKDF_TYPE
,
2087 ARG_LUKS_PBKDF_HASH_ALGORITHM
,
2088 ARG_LUKS_PBKDF_TIME_COST
,
2089 ARG_LUKS_PBKDF_MEMORY_COST
,
2090 ARG_LUKS_PBKDF_PARALLEL_THREADS
,
2091 ARG_RATE_LIMIT_INTERVAL
,
2092 ARG_RATE_LIMIT_BURST
,
2095 ARG_ENFORCE_PASSWORD_POLICY
,
2096 ARG_PASSWORD_CHANGE_NOW
,
2097 ARG_PASSWORD_CHANGE_MIN
,
2098 ARG_PASSWORD_CHANGE_MAX
,
2099 ARG_PASSWORD_CHANGE_WARN
,
2100 ARG_PASSWORD_CHANGE_INACTIVE
,
2103 ARG_PKCS11_TOKEN_URI
,
2107 ARG_AND_CHANGE_PASSWORD
,
2110 static const struct option options
[] = {
2111 { "help", no_argument
, NULL
, 'h' },
2112 { "version", no_argument
, NULL
, ARG_VERSION
},
2113 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
2114 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
2115 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
2116 { "host", required_argument
, NULL
, 'H' },
2117 { "machine", required_argument
, NULL
, 'M' },
2118 { "identity", required_argument
, NULL
, 'I' },
2119 { "real-name", required_argument
, NULL
, 'c' },
2120 { "comment", required_argument
, NULL
, 'c' }, /* Compat alias to keep thing in sync with useradd(8) */
2121 { "realm", required_argument
, NULL
, ARG_REALM
},
2122 { "email-address", required_argument
, NULL
, ARG_EMAIL_ADDRESS
},
2123 { "location", required_argument
, NULL
, ARG_LOCATION
},
2124 { "password-hint", required_argument
, NULL
, ARG_PASSWORD_HINT
},
2125 { "icon-name", required_argument
, NULL
, ARG_ICON_NAME
},
2126 { "home-dir", required_argument
, NULL
, 'd' }, /* Compatible with useradd(8) */
2127 { "uid", required_argument
, NULL
, 'u' }, /* Compatible with useradd(8) */
2128 { "member-of", required_argument
, NULL
, 'G' },
2129 { "groups", required_argument
, NULL
, 'G' }, /* Compat alias to keep thing in sync with useradd(8) */
2130 { "skel", required_argument
, NULL
, 'k' }, /* Compatible with useradd(8) */
2131 { "shell", required_argument
, NULL
, 's' }, /* Compatible with useradd(8) */
2132 { "setenv", required_argument
, NULL
, ARG_SETENV
},
2133 { "timezone", required_argument
, NULL
, ARG_TIMEZONE
},
2134 { "language", required_argument
, NULL
, ARG_LANGUAGE
},
2135 { "locked", required_argument
, NULL
, ARG_LOCKED
},
2136 { "not-before", required_argument
, NULL
, ARG_NOT_BEFORE
},
2137 { "not-after", required_argument
, NULL
, ARG_NOT_AFTER
},
2138 { "expiredate", required_argument
, NULL
, 'e' }, /* Compat alias to keep thing in sync with useradd(8) */
2139 { "ssh-authorized-keys", required_argument
, NULL
, ARG_SSH_AUTHORIZED_KEYS
},
2140 { "disk-size", required_argument
, NULL
, ARG_DISK_SIZE
},
2141 { "access-mode", required_argument
, NULL
, ARG_ACCESS_MODE
},
2142 { "umask", required_argument
, NULL
, ARG_UMASK
},
2143 { "nice", required_argument
, NULL
, ARG_NICE
},
2144 { "rlimit", required_argument
, NULL
, ARG_RLIMIT
},
2145 { "tasks-max", required_argument
, NULL
, ARG_TASKS_MAX
},
2146 { "memory-high", required_argument
, NULL
, ARG_MEMORY_HIGH
},
2147 { "memory-max", required_argument
, NULL
, ARG_MEMORY_MAX
},
2148 { "cpu-weight", required_argument
, NULL
, ARG_CPU_WEIGHT
},
2149 { "io-weight", required_argument
, NULL
, ARG_IO_WEIGHT
},
2150 { "storage", required_argument
, NULL
, ARG_STORAGE
},
2151 { "image-path", required_argument
, NULL
, ARG_IMAGE_PATH
},
2152 { "fs-type", required_argument
, NULL
, ARG_FS_TYPE
},
2153 { "luks-discard", required_argument
, NULL
, ARG_LUKS_DISCARD
},
2154 { "luks-offline-discard", required_argument
, NULL
, ARG_LUKS_OFFLINE_DISCARD
},
2155 { "luks-cipher", required_argument
, NULL
, ARG_LUKS_CIPHER
},
2156 { "luks-cipher-mode", required_argument
, NULL
, ARG_LUKS_CIPHER_MODE
},
2157 { "luks-volume-key-size", required_argument
, NULL
, ARG_LUKS_VOLUME_KEY_SIZE
},
2158 { "luks-pbkdf-type", required_argument
, NULL
, ARG_LUKS_PBKDF_TYPE
},
2159 { "luks-pbkdf-hash-algorithm", required_argument
, NULL
, ARG_LUKS_PBKDF_HASH_ALGORITHM
},
2160 { "luks-pbkdf-time-cost", required_argument
, NULL
, ARG_LUKS_PBKDF_TIME_COST
},
2161 { "luks-pbkdf-memory-cost", required_argument
, NULL
, ARG_LUKS_PBKDF_MEMORY_COST
},
2162 { "luks-pbkdf-parallel-threads", required_argument
, NULL
, ARG_LUKS_PBKDF_PARALLEL_THREADS
},
2163 { "nosuid", required_argument
, NULL
, ARG_NOSUID
},
2164 { "nodev", required_argument
, NULL
, ARG_NODEV
},
2165 { "noexec", required_argument
, NULL
, ARG_NOEXEC
},
2166 { "cifs-user-name", required_argument
, NULL
, ARG_CIFS_USER_NAME
},
2167 { "cifs-domain", required_argument
, NULL
, ARG_CIFS_DOMAIN
},
2168 { "cifs-service", required_argument
, NULL
, ARG_CIFS_SERVICE
},
2169 { "rate-limit-interval", required_argument
, NULL
, ARG_RATE_LIMIT_INTERVAL
},
2170 { "rate-limit-burst", required_argument
, NULL
, ARG_RATE_LIMIT_BURST
},
2171 { "stop-delay", required_argument
, NULL
, ARG_STOP_DELAY
},
2172 { "kill-processes", required_argument
, NULL
, ARG_KILL_PROCESSES
},
2173 { "enforce-password-policy", required_argument
, NULL
, ARG_ENFORCE_PASSWORD_POLICY
},
2174 { "password-change-now", required_argument
, NULL
, ARG_PASSWORD_CHANGE_NOW
},
2175 { "password-change-min", required_argument
, NULL
, ARG_PASSWORD_CHANGE_MIN
},
2176 { "password-change-max", required_argument
, NULL
, ARG_PASSWORD_CHANGE_MAX
},
2177 { "password-change-warn", required_argument
, NULL
, ARG_PASSWORD_CHANGE_WARN
},
2178 { "password-change-inactive", required_argument
, NULL
, ARG_PASSWORD_CHANGE_INACTIVE
},
2179 { "auto-login", required_argument
, NULL
, ARG_AUTO_LOGIN
},
2180 { "json", required_argument
, NULL
, ARG_JSON
},
2181 { "export-format", required_argument
, NULL
, ARG_EXPORT_FORMAT
},
2182 { "pkcs11-token-uri", required_argument
, NULL
, ARG_PKCS11_TOKEN_URI
},
2183 { "fido2-device", required_argument
, NULL
, ARG_FIDO2_DEVICE
},
2184 { "recovery-key", required_argument
, NULL
, ARG_RECOVERY_KEY
},
2185 { "and-resize", required_argument
, NULL
, ARG_AND_RESIZE
},
2186 { "and-change-password", required_argument
, NULL
, ARG_AND_CHANGE_PASSWORD
},
2198 c
= getopt_long(argc
, argv
, "hH:M:I:c:d:u:k:s:e:G:jPE", options
, NULL
);
2205 return help(0, NULL
, NULL
);
2211 arg_pager_flags
|= PAGER_DISABLE
;
2218 case ARG_NO_ASK_PASSWORD
:
2219 arg_ask_password
= false;
2223 arg_transport
= BUS_TRANSPORT_REMOTE
;
2228 arg_transport
= BUS_TRANSPORT_MACHINE
;
2233 arg_identity
= optarg
;
2237 if (isempty(optarg
)) {
2238 r
= drop_from_identity("realName");
2245 if (!valid_gecos(optarg
))
2246 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Real name '%s' not a valid GECOS field.", optarg
);
2248 r
= json_variant_set_field_string(&arg_identity_extra
, "realName", optarg
);
2250 return log_error_errno(r
, "Failed to set realName field: %m");
2255 _cleanup_free_
char *hd
= NULL
;
2257 if (isempty(optarg
)) {
2258 r
= drop_from_identity("homeDirectory");
2265 r
= parse_path_argument(optarg
, false, &hd
);
2269 if (!valid_home(hd
))
2270 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Home directory '%s' not valid.", hd
);
2272 r
= json_variant_set_field_string(&arg_identity_extra
, "homeDirectory", hd
);
2274 return log_error_errno(r
, "Failed to set homeDirectory field: %m");
2280 if (isempty(optarg
)) {
2281 r
= drop_from_identity("realm");
2288 r
= dns_name_is_valid(optarg
);
2290 return log_error_errno(r
, "Failed to determine whether realm '%s' is a valid DNS domain: %m", optarg
);
2292 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Realm '%s' is not a valid DNS domain: %m", optarg
);
2294 r
= json_variant_set_field_string(&arg_identity_extra
, "realm", optarg
);
2296 return log_error_errno(r
, "Failed to set realm field: %m");
2299 case ARG_EMAIL_ADDRESS
:
2302 case ARG_CIFS_USER_NAME
:
2303 case ARG_CIFS_DOMAIN
:
2304 case ARG_CIFS_SERVICE
: {
2307 c
== ARG_EMAIL_ADDRESS
? "emailAddress" :
2308 c
== ARG_LOCATION
? "location" :
2309 c
== ARG_ICON_NAME
? "iconName" :
2310 c
== ARG_CIFS_USER_NAME
? "cifsUserName" :
2311 c
== ARG_CIFS_DOMAIN
? "cifsDomain" :
2312 c
== ARG_CIFS_SERVICE
? "cifsService" :
2317 if (isempty(optarg
)) {
2318 r
= drop_from_identity(field
);
2325 r
= json_variant_set_field_string(&arg_identity_extra
, field
, optarg
);
2327 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2332 case ARG_PASSWORD_HINT
:
2333 if (isempty(optarg
)) {
2334 r
= drop_from_identity("passwordHint");
2341 r
= json_variant_set_field_string(&arg_identity_extra_privileged
, "passwordHint", optarg
);
2343 return log_error_errno(r
, "Failed to set passwordHint field: %m");
2345 string_erase(optarg
);
2351 if (isempty(optarg
)) {
2352 r
= drop_from_identity("niceLevel");
2358 r
= parse_nice(optarg
, &nc
);
2360 return log_error_errno(r
, "Failed to parse nice level: %s", optarg
);
2362 r
= json_variant_set_field_integer(&arg_identity_extra
, "niceLevel", nc
);
2364 return log_error_errno(r
, "Failed to set niceLevel field: %m");
2370 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
, *jcur
= NULL
, *jmax
= NULL
;
2371 _cleanup_free_
char *field
= NULL
, *t
= NULL
;
2376 if (isempty(optarg
)) {
2377 /* Remove all resource limits */
2379 r
= drop_from_identity("resourceLimits");
2383 arg_identity_filter_rlimits
= strv_free(arg_identity_filter_rlimits
);
2384 arg_identity_extra_rlimits
= json_variant_unref(arg_identity_extra_rlimits
);
2388 eq
= strchr(optarg
, '=');
2390 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Can't parse resource limit assignment: %s", optarg
);
2392 field
= strndup(optarg
, eq
- optarg
);
2396 l
= rlimit_from_string_harder(field
);
2398 return log_error_errno(l
, "Unknown resource limit type: %s", field
);
2400 if (isempty(eq
+ 1)) {
2401 /* Remove only the specific rlimit */
2403 r
= strv_extend(&arg_identity_filter_rlimits
, rlimit_to_string(l
));
2407 r
= json_variant_filter(&arg_identity_extra_rlimits
, STRV_MAKE(field
));
2409 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
2414 r
= rlimit_parse(l
, eq
+ 1, &rl
);
2416 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to parse resource limit value: %s", eq
+ 1);
2418 r
= rl
.rlim_cur
== RLIM_INFINITY
? json_variant_new_null(&jcur
) : json_variant_new_unsigned(&jcur
, rl
.rlim_cur
);
2420 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to allocate current integer: %m");
2422 r
= rl
.rlim_max
== RLIM_INFINITY
? json_variant_new_null(&jmax
) : json_variant_new_unsigned(&jmax
, rl
.rlim_max
);
2424 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to allocate maximum integer: %m");
2428 JSON_BUILD_PAIR("cur", JSON_BUILD_VARIANT(jcur
)),
2429 JSON_BUILD_PAIR("max", JSON_BUILD_VARIANT(jmax
))));
2431 return log_error_errno(r
, "Failed to build resource limit: %m");
2433 t
= strjoin("RLIMIT_", rlimit_to_string(l
));
2437 r
= json_variant_set_field(&arg_identity_extra_rlimits
, t
, v
);
2439 return log_error_errno(r
, "Failed to set %s field: %m", rlimit_to_string(l
));
2447 if (isempty(optarg
)) {
2448 r
= drop_from_identity("uid");
2455 r
= parse_uid(optarg
, &uid
);
2457 return log_error_errno(r
, "Failed to parse UID '%s'.", optarg
);
2459 if (uid_is_system(uid
))
2460 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "UID " UID_FMT
" is in system range, refusing.", uid
);
2461 if (uid_is_dynamic(uid
))
2462 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "UID " UID_FMT
" is in dynamic range, refusing.", uid
);
2463 if (uid
== UID_NOBODY
)
2464 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "UID " UID_FMT
" is nobody UID, refusing.", uid
);
2466 r
= json_variant_set_field_unsigned(&arg_identity_extra
, "uid", uid
);
2468 return log_error_errno(r
, "Failed to set realm field: %m");
2474 case ARG_IMAGE_PATH
: {
2475 const char *field
= c
== 'k' ? "skeletonDirectory" : "imagePath";
2476 _cleanup_free_
char *v
= NULL
;
2478 if (isempty(optarg
)) {
2479 r
= drop_from_identity(field
);
2486 r
= parse_path_argument(optarg
, false, &v
);
2490 r
= json_variant_set_field_string(&arg_identity_extra_this_machine
, field
, v
);
2492 return log_error_errno(r
, "Failed to set %s field: %m", v
);
2498 if (isempty(optarg
)) {
2499 r
= drop_from_identity("shell");
2506 if (!valid_shell(optarg
))
2507 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Shell '%s' not valid.", optarg
);
2509 r
= json_variant_set_field_string(&arg_identity_extra
, "shell", optarg
);
2511 return log_error_errno(r
, "Failed to set shell field: %m");
2516 _cleanup_free_
char **l
= NULL
;
2517 _cleanup_(json_variant_unrefp
) JsonVariant
*ne
= NULL
;
2520 if (isempty(optarg
)) {
2521 r
= drop_from_identity("environment");
2528 if (!env_assignment_is_valid(optarg
))
2529 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2530 "Environment assignment '%s' not valid.", optarg
);
2532 e
= json_variant_by_key(arg_identity_extra
, "environment");
2534 r
= json_variant_strv(e
, &l
);
2536 return log_error_errno(r
, "Failed to parse JSON environment field: %m");
2539 r
= strv_env_replace_strdup(&l
, optarg
);
2541 return log_error_errno(r
, "Failed to replace JSON environment field: %m");
2545 r
= json_variant_new_array_strv(&ne
, l
);
2547 return log_error_errno(r
, "Failed to allocate environment list JSON: %m");
2549 r
= json_variant_set_field(&arg_identity_extra
, "environment", ne
);
2551 return log_error_errno(r
, "Failed to set environment list: %m");
2558 if (isempty(optarg
)) {
2559 r
= drop_from_identity("timeZone");
2566 if (!timezone_is_valid(optarg
, LOG_DEBUG
))
2567 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Timezone '%s' is not valid.", optarg
);
2569 r
= json_variant_set_field_string(&arg_identity_extra
, "timeZone", optarg
);
2571 return log_error_errno(r
, "Failed to set timezone field: %m");
2576 if (isempty(optarg
)) {
2577 r
= drop_from_identity("language");
2584 if (!locale_is_valid(optarg
))
2585 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Locale '%s' is not valid.", optarg
);
2587 if (locale_is_installed(optarg
) <= 0)
2588 log_warning("Locale '%s' is not installed, accepting anyway.", optarg
);
2590 r
= json_variant_set_field_string(&arg_identity_extra
, "preferredLanguage", optarg
);
2592 return log_error_errno(r
, "Failed to set preferredLanguage field: %m");
2600 case ARG_KILL_PROCESSES
:
2601 case ARG_ENFORCE_PASSWORD_POLICY
:
2602 case ARG_AUTO_LOGIN
:
2603 case ARG_PASSWORD_CHANGE_NOW
: {
2605 c
== ARG_LOCKED
? "locked" :
2606 c
== ARG_NOSUID
? "mountNoSuid" :
2607 c
== ARG_NODEV
? "mountNoDevices" :
2608 c
== ARG_NOEXEC
? "mountNoExecute" :
2609 c
== ARG_KILL_PROCESSES
? "killProcesses" :
2610 c
== ARG_ENFORCE_PASSWORD_POLICY
? "enforcePasswordPolicy" :
2611 c
== ARG_AUTO_LOGIN
? "autoLogin" :
2612 c
== ARG_PASSWORD_CHANGE_NOW
? "passwordChangeNow" :
2617 if (isempty(optarg
)) {
2618 r
= drop_from_identity(field
);
2625 r
= parse_boolean(optarg
);
2627 return log_error_errno(r
, "Failed to parse %s boolean: %m", field
);
2629 r
= json_variant_set_field_boolean(&arg_identity_extra
, field
, r
> 0);
2631 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2637 r
= json_variant_set_field_boolean(&arg_identity_extra
, "enforcePasswordPolicy", false);
2639 return log_error_errno(r
, "Failed to set enforcePasswordPolicy field: %m");
2644 if (isempty(optarg
)) {
2645 r
= drop_from_identity("diskSize");
2649 r
= drop_from_identity("diskSizeRelative");
2653 arg_disk_size
= arg_disk_size_relative
= UINT64_MAX
;
2657 r
= parse_permyriad(optarg
);
2659 r
= parse_size(optarg
, 1024, &arg_disk_size
);
2661 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Disk size '%s' not valid.", optarg
);
2663 r
= drop_from_identity("diskSizeRelative");
2667 r
= json_variant_set_field_unsigned(&arg_identity_extra_this_machine
, "diskSize", arg_disk_size
);
2669 return log_error_errno(r
, "Failed to set diskSize field: %m");
2671 arg_disk_size_relative
= UINT64_MAX
;
2673 /* Normalize to UINT32_MAX == 100% */
2674 arg_disk_size_relative
= UINT32_SCALE_FROM_PERMYRIAD(r
);
2676 r
= drop_from_identity("diskSize");
2680 r
= json_variant_set_field_unsigned(&arg_identity_extra_this_machine
, "diskSizeRelative", arg_disk_size_relative
);
2682 return log_error_errno(r
, "Failed to set diskSizeRelative field: %m");
2684 arg_disk_size
= UINT64_MAX
;
2689 case ARG_ACCESS_MODE
: {
2692 if (isempty(optarg
)) {
2693 r
= drop_from_identity("accessMode");
2700 r
= parse_mode(optarg
, &mode
);
2702 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Access mode '%s' not valid.", optarg
);
2704 r
= json_variant_set_field_unsigned(&arg_identity_extra
, "accessMode", mode
);
2706 return log_error_errno(r
, "Failed to set access mode field: %m");
2711 case ARG_LUKS_DISCARD
:
2712 if (isempty(optarg
)) {
2713 r
= drop_from_identity("luksDiscard");
2720 r
= parse_boolean(optarg
);
2722 return log_error_errno(r
, "Failed to parse --luks-discard= parameter: %s", optarg
);
2724 r
= json_variant_set_field_boolean(&arg_identity_extra
, "luksDiscard", r
);
2726 return log_error_errno(r
, "Failed to set discard field: %m");
2730 case ARG_LUKS_OFFLINE_DISCARD
:
2731 if (isempty(optarg
)) {
2732 r
= drop_from_identity("luksOfflineDiscard");
2739 r
= parse_boolean(optarg
);
2741 return log_error_errno(r
, "Failed to parse --luks-offline-discard= parameter: %s", optarg
);
2743 r
= json_variant_set_field_boolean(&arg_identity_extra
, "luksOfflineDiscard", r
);
2745 return log_error_errno(r
, "Failed to set offline discard field: %m");
2749 case ARG_LUKS_VOLUME_KEY_SIZE
:
2750 case ARG_LUKS_PBKDF_PARALLEL_THREADS
:
2751 case ARG_RATE_LIMIT_BURST
: {
2753 c
== ARG_LUKS_VOLUME_KEY_SIZE
? "luksVolumeKeySize" :
2754 c
== ARG_LUKS_PBKDF_PARALLEL_THREADS
? "luksPbkdfParallelThreads" :
2755 c
== ARG_RATE_LIMIT_BURST
? "rateLimitBurst" : NULL
;
2760 if (isempty(optarg
)) {
2761 r
= drop_from_identity(field
);
2766 r
= safe_atou(optarg
, &n
);
2768 return log_error_errno(r
, "Failed to parse %s parameter: %s", field
, optarg
);
2770 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, n
);
2772 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2780 if (isempty(optarg
)) {
2781 r
= drop_from_identity("umask");
2788 r
= parse_mode(optarg
, &m
);
2790 return log_error_errno(r
, "Failed to parse umask: %m");
2792 r
= json_variant_set_field_integer(&arg_identity_extra
, "umask", m
);
2794 return log_error_errno(r
, "Failed to set umask field: %m");
2799 case ARG_SSH_AUTHORIZED_KEYS
: {
2800 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
2801 _cleanup_(strv_freep
) char **l
= NULL
, **add
= NULL
;
2803 if (isempty(optarg
)) {
2804 r
= drop_from_identity("sshAuthorizedKeys");
2811 if (optarg
[0] == '@') {
2812 _cleanup_fclose_
FILE *f
= NULL
;
2814 /* If prefixed with '@' read from a file */
2816 f
= fopen(optarg
+1, "re");
2818 return log_error_errno(errno
, "Failed to open '%s': %m", optarg
+1);
2821 _cleanup_free_
char *line
= NULL
;
2823 r
= read_line(f
, LONG_LINE_MAX
, &line
);
2825 return log_error_errno(r
, "Failed to read from '%s': %m", optarg
+1);
2835 r
= strv_consume(&add
, TAKE_PTR(line
));
2840 /* Otherwise, assume it's a literal key. Let's do some superficial checks
2841 * before accept it though. */
2843 if (string_has_cc(optarg
, NULL
))
2844 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Authorized key contains control characters, refusing.");
2845 if (optarg
[0] == '#')
2846 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Specified key is a comment?");
2848 add
= strv_new(optarg
);
2853 v
= json_variant_ref(json_variant_by_key(arg_identity_extra_privileged
, "sshAuthorizedKeys"));
2855 r
= json_variant_strv(v
, &l
);
2857 return log_error_errno(r
, "Failed to parse SSH authorized keys list: %m");
2860 r
= strv_extend_strv(&l
, add
, true);
2864 v
= json_variant_unref(v
);
2866 r
= json_variant_new_array_strv(&v
, l
);
2870 r
= json_variant_set_field(&arg_identity_extra_privileged
, "sshAuthorizedKeys", v
);
2872 return log_error_errno(r
, "Failed to set authorized keys: %m");
2877 case ARG_NOT_BEFORE
:
2883 field
= c
== ARG_NOT_BEFORE
? "notBeforeUSec" :
2884 IN_SET(c
, ARG_NOT_AFTER
, 'e') ? "notAfterUSec" : NULL
;
2888 if (isempty(optarg
)) {
2889 r
= drop_from_identity(field
);
2896 /* Note the minor discrepancy regarding -e parsing here: we support that for compat
2897 * reasons, and in the original useradd(8) implementation it accepts dates in the
2898 * format YYYY-MM-DD. Coincidentally, we accept dates formatted like that too, but
2899 * with greater precision. */
2900 r
= parse_timestamp(optarg
, &n
);
2902 return log_error_errno(r
, "Failed to parse %s parameter: %m", field
);
2904 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, n
);
2906 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2910 case ARG_PASSWORD_CHANGE_MIN
:
2911 case ARG_PASSWORD_CHANGE_MAX
:
2912 case ARG_PASSWORD_CHANGE_WARN
:
2913 case ARG_PASSWORD_CHANGE_INACTIVE
: {
2917 field
= c
== ARG_PASSWORD_CHANGE_MIN
? "passwordChangeMinUSec" :
2918 c
== ARG_PASSWORD_CHANGE_MAX
? "passwordChangeMaxUSec" :
2919 c
== ARG_PASSWORD_CHANGE_WARN
? "passwordChangeWarnUSec" :
2920 c
== ARG_PASSWORD_CHANGE_INACTIVE
? "passwordChangeInactiveUSec" :
2925 if (isempty(optarg
)) {
2926 r
= drop_from_identity(field
);
2933 r
= parse_sec(optarg
, &n
);
2935 return log_error_errno(r
, "Failed to parse %s parameter: %m", field
);
2937 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, n
);
2939 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2945 case ARG_LUKS_CIPHER
:
2946 case ARG_LUKS_CIPHER_MODE
:
2947 case ARG_LUKS_PBKDF_TYPE
:
2948 case ARG_LUKS_PBKDF_HASH_ALGORITHM
: {
2951 c
== ARG_STORAGE
? "storage" :
2952 c
== ARG_FS_TYPE
? "fileSystemType" :
2953 c
== ARG_LUKS_CIPHER
? "luksCipher" :
2954 c
== ARG_LUKS_CIPHER_MODE
? "luksCipherMode" :
2955 c
== ARG_LUKS_PBKDF_TYPE
? "luksPbkdfType" :
2956 c
== ARG_LUKS_PBKDF_HASH_ALGORITHM
? "luksPbkdfHashAlgorithm" : NULL
;
2960 if (isempty(optarg
)) {
2961 r
= drop_from_identity(field
);
2968 if (!string_is_safe(optarg
))
2969 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Parameter for %s field not valid: %s", field
, optarg
);
2971 r
= json_variant_set_field_string(
2972 IN_SET(c
, ARG_STORAGE
, ARG_FS_TYPE
) ?
2973 &arg_identity_extra_this_machine
:
2974 &arg_identity_extra
, field
, optarg
);
2976 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2981 case ARG_LUKS_PBKDF_TIME_COST
:
2982 case ARG_RATE_LIMIT_INTERVAL
:
2983 case ARG_STOP_DELAY
: {
2985 c
== ARG_LUKS_PBKDF_TIME_COST
? "luksPbkdfTimeCostUSec" :
2986 c
== ARG_RATE_LIMIT_INTERVAL
? "rateLimitIntervalUSec" :
2987 c
== ARG_STOP_DELAY
? "stopDelayUSec" :
2993 if (isempty(optarg
)) {
2994 r
= drop_from_identity(field
);
3001 r
= parse_sec(optarg
, &t
);
3003 return log_error_errno(r
, "Failed to parse %s field: %s", field
, optarg
);
3005 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, t
);
3007 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3013 const char *p
= optarg
;
3016 r
= drop_from_identity("memberOf");
3024 _cleanup_(json_variant_unrefp
) JsonVariant
*mo
= NULL
;
3025 _cleanup_strv_free_
char **list
= NULL
;
3026 _cleanup_free_
char *word
= NULL
;
3028 r
= extract_first_word(&p
, &word
, ",", 0);
3030 return log_error_errno(r
, "Failed to parse group list: %m");
3034 if (!valid_user_group_name(word
, 0))
3035 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Invalid group name %s.", word
);
3037 mo
= json_variant_ref(json_variant_by_key(arg_identity_extra
, "memberOf"));
3039 r
= json_variant_strv(mo
, &list
);
3041 return log_error_errno(r
, "Failed to parse group list: %m");
3043 r
= strv_extend(&list
, word
);
3050 mo
= json_variant_unref(mo
);
3051 r
= json_variant_new_array_strv(&mo
, list
);
3053 return log_error_errno(r
, "Failed to create group list JSON: %m");
3055 r
= json_variant_set_field(&arg_identity_extra
, "memberOf", mo
);
3057 return log_error_errno(r
, "Failed to update group list: %m");
3063 case ARG_TASKS_MAX
: {
3066 if (isempty(optarg
)) {
3067 r
= drop_from_identity("tasksMax");
3073 r
= safe_atou64(optarg
, &u
);
3075 return log_error_errno(r
, "Failed to parse --tasks-max= parameter: %s", optarg
);
3077 r
= json_variant_set_field_unsigned(&arg_identity_extra
, "tasksMax", u
);
3079 return log_error_errno(r
, "Failed to set tasksMax field: %m");
3084 case ARG_MEMORY_MAX
:
3085 case ARG_MEMORY_HIGH
:
3086 case ARG_LUKS_PBKDF_MEMORY_COST
: {
3088 c
== ARG_MEMORY_MAX
? "memoryMax" :
3089 c
== ARG_MEMORY_HIGH
? "memoryHigh" :
3090 c
== ARG_LUKS_PBKDF_MEMORY_COST
? "luksPbkdfMemoryCost" : NULL
;
3096 if (isempty(optarg
)) {
3097 r
= drop_from_identity(field
);
3103 r
= parse_size(optarg
, 1024, &u
);
3105 return log_error_errno(r
, "Failed to parse %s parameter: %s", field
, optarg
);
3107 r
= json_variant_set_field_unsigned(&arg_identity_extra_this_machine
, field
, u
);
3109 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3114 case ARG_CPU_WEIGHT
:
3115 case ARG_IO_WEIGHT
: {
3116 const char *field
= c
== ARG_CPU_WEIGHT
? "cpuWeight" :
3117 c
== ARG_IO_WEIGHT
? "ioWeight" : NULL
;
3122 if (isempty(optarg
)) {
3123 r
= drop_from_identity(field
);
3129 r
= safe_atou64(optarg
, &u
);
3131 return log_error_errno(r
, "Failed to parse --cpu-weight=/--io-weight= parameter: %s", optarg
);
3133 if (!CGROUP_WEIGHT_IS_OK(u
))
3134 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Weight %" PRIu64
" is out of valid weight range.", u
);
3136 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, u
);
3138 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3143 case ARG_PKCS11_TOKEN_URI
: {
3146 if (streq(optarg
, "list"))
3147 return pkcs11_list_tokens();
3149 /* If --pkcs11-token-uri= is specified we always drop everything old */
3150 FOREACH_STRING(p
, "pkcs11TokenUri", "pkcs11EncryptedKey") {
3151 r
= drop_from_identity(p
);
3156 if (isempty(optarg
)) {
3157 arg_pkcs11_token_uri
= strv_free(arg_pkcs11_token_uri
);
3161 if (streq(optarg
, "auto")) {
3162 _cleanup_free_
char *found
= NULL
;
3164 r
= pkcs11_find_token_auto(&found
);
3167 r
= strv_consume(&arg_pkcs11_token_uri
, TAKE_PTR(found
));
3169 if (!pkcs11_uri_valid(optarg
))
3170 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Not a valid PKCS#11 URI: %s", optarg
);
3172 r
= strv_extend(&arg_pkcs11_token_uri
, optarg
);
3177 strv_uniq(arg_pkcs11_token_uri
);
3181 case ARG_FIDO2_DEVICE
: {
3184 if (streq(optarg
, "list"))
3185 return fido2_list_devices();
3187 FOREACH_STRING(p
, "fido2HmacCredential", "fido2HmacSalt") {
3188 r
= drop_from_identity(p
);
3193 if (isempty(optarg
)) {
3194 arg_fido2_device
= strv_free(arg_fido2_device
);
3198 if (streq(optarg
, "auto")) {
3199 _cleanup_free_
char *found
= NULL
;
3201 r
= fido2_find_device_auto(&found
);
3205 r
= strv_consume(&arg_fido2_device
, TAKE_PTR(found
));
3207 r
= strv_extend(&arg_fido2_device
, optarg
);
3212 strv_uniq(arg_fido2_device
);
3216 case ARG_RECOVERY_KEY
: {
3219 r
= parse_boolean(optarg
);
3221 return log_error_errno(r
, "Failed to parse --recovery-key= argument: %s", optarg
);
3223 arg_recovery_key
= r
;
3225 FOREACH_STRING(p
, "recoveryKey", "recoveryKeyType") {
3226 r
= drop_from_identity(p
);
3235 arg_json_format_flags
= JSON_FORMAT_PRETTY_AUTO
|JSON_FORMAT_COLOR_AUTO
;
3239 r
= parse_json_argument(optarg
, &arg_json_format_flags
);
3246 if (arg_export_format
== EXPORT_FORMAT_FULL
)
3247 arg_export_format
= EXPORT_FORMAT_STRIPPED
;
3248 else if (arg_export_format
== EXPORT_FORMAT_STRIPPED
)
3249 arg_export_format
= EXPORT_FORMAT_MINIMAL
;
3251 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Specifying -E more than twice is not supported.");
3253 arg_json_format_flags
&= ~JSON_FORMAT_OFF
;
3254 if (arg_json_format_flags
== 0)
3255 arg_json_format_flags
= JSON_FORMAT_PRETTY_AUTO
|JSON_FORMAT_COLOR_AUTO
;
3258 case ARG_EXPORT_FORMAT
:
3259 if (streq(optarg
, "full"))
3260 arg_export_format
= EXPORT_FORMAT_FULL
;
3261 else if (streq(optarg
, "stripped"))
3262 arg_export_format
= EXPORT_FORMAT_STRIPPED
;
3263 else if (streq(optarg
, "minimal"))
3264 arg_export_format
= EXPORT_FORMAT_MINIMAL
;
3265 else if (streq(optarg
, "help")) {
3274 case ARG_AND_RESIZE
:
3275 arg_and_resize
= true;
3278 case ARG_AND_CHANGE_PASSWORD
:
3279 arg_and_change_password
= true;
3286 assert_not_reached("Unhandled option");
3290 if (!strv_isempty(arg_pkcs11_token_uri
) || !strv_isempty(arg_fido2_device
))
3291 arg_and_change_password
= true;
3293 if (arg_disk_size
!= UINT64_MAX
|| arg_disk_size_relative
!= UINT64_MAX
)
3294 arg_and_resize
= true;
3299 static int redirect_bus_mgr(void) {
3302 /* Talk to a different service if that's requested. (The same env var is also understood by homed, so
3303 * that it is relatively easily possible to invoke a second instance of homed for debug purposes and
3304 * have homectl talk to it, without colliding with the host version. This is handy when operating
3305 * from a homed-managed account.) */
3307 suffix
= getenv("SYSTEMD_HOME_DEBUG_SUFFIX");
3309 static BusLocator locator
= {
3310 .path
= "/org/freedesktop/home1",
3311 .interface
= "org.freedesktop.home1.Manager",
3314 /* Yes, we leak this memory, but there's little point to collect this, given that we only do
3315 * this in a debug environment, do it only once, and the string shall live for out entire
3316 * process runtime. */
3318 locator
.destination
= strjoin("org.freedesktop.home1.", suffix
);
3319 if (!locator
.destination
)
3324 bus_mgr
= bus_home_mgr
;
3329 static int run(int argc
, char *argv
[]) {
3330 static const Verb verbs
[] = {
3331 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
3332 { "list", VERB_ANY
, 1, VERB_DEFAULT
, list_homes
},
3333 { "activate", 2, VERB_ANY
, 0, activate_home
},
3334 { "deactivate", 2, VERB_ANY
, 0, deactivate_home
},
3335 { "inspect", VERB_ANY
, VERB_ANY
, 0, inspect_home
},
3336 { "authenticate", VERB_ANY
, VERB_ANY
, 0, authenticate_home
},
3337 { "create", VERB_ANY
, 2, 0, create_home
},
3338 { "remove", 2, VERB_ANY
, 0, remove_home
},
3339 { "update", VERB_ANY
, 2, 0, update_home
},
3340 { "passwd", VERB_ANY
, 2, 0, passwd_home
},
3341 { "resize", 2, 3, 0, resize_home
},
3342 { "lock", 2, VERB_ANY
, 0, lock_home
},
3343 { "unlock", 2, VERB_ANY
, 0, unlock_home
},
3344 { "with", 2, VERB_ANY
, 0, with_home
},
3345 { "lock-all", VERB_ANY
, 1, 0, lock_all_homes
},
3346 { "deactivate-all", VERB_ANY
, 1, 0, deactivate_all_homes
},
3354 r
= redirect_bus_mgr();
3358 r
= parse_argv(argc
, argv
);
3362 return dispatch_verb(argc
, argv
, verbs
, NULL
);
3365 DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run
);