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"
18 #include "glyph-util.h"
19 #include "home-util.h"
20 #include "homectl-fido2.h"
21 #include "homectl-pkcs11.h"
22 #include "homectl-recovery-key.h"
23 #include "libfido2-util.h"
24 #include "locale-util.h"
25 #include "main-func.h"
26 #include "memory-util.h"
28 #include "parse-argument.h"
29 #include "parse-util.h"
30 #include "path-util.h"
31 #include "percent-util.h"
32 #include "pkcs11-util.h"
33 #include "pretty-print.h"
34 #include "process-util.h"
35 #include "pwquality-util.h"
36 #include "rlimit-util.h"
37 #include "spawn-polkit-agent.h"
38 #include "terminal-util.h"
39 #include "user-record-pwquality.h"
40 #include "user-record-show.h"
41 #include "user-record-util.h"
42 #include "user-record.h"
43 #include "user-util.h"
46 static PagerFlags arg_pager_flags
= 0;
47 static bool arg_legend
= true;
48 static bool arg_ask_password
= true;
49 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
50 static const char *arg_host
= NULL
;
51 static const char *arg_identity
= NULL
;
52 static JsonVariant
*arg_identity_extra
= NULL
;
53 static JsonVariant
*arg_identity_extra_privileged
= NULL
;
54 static JsonVariant
*arg_identity_extra_this_machine
= NULL
;
55 static JsonVariant
*arg_identity_extra_rlimits
= NULL
;
56 static char **arg_identity_filter
= NULL
; /* this one is also applied to 'privileged' and 'thisMachine' subobjects */
57 static char **arg_identity_filter_rlimits
= NULL
;
58 static uint64_t arg_disk_size
= UINT64_MAX
;
59 static uint64_t arg_disk_size_relative
= UINT64_MAX
;
60 static char **arg_pkcs11_token_uri
= NULL
;
61 static char **arg_fido2_device
= NULL
;
62 static Fido2EnrollFlags arg_fido2_lock_with
= FIDO2ENROLL_PIN
| FIDO2ENROLL_UP
;
63 static bool arg_recovery_key
= false;
64 static JsonFormatFlags arg_json_format_flags
= JSON_FORMAT_OFF
;
65 static bool arg_and_resize
= false;
66 static bool arg_and_change_password
= false;
68 EXPORT_FORMAT_FULL
, /* export the full record */
69 EXPORT_FORMAT_STRIPPED
, /* strip "state" + "binding", but leave signature in place */
70 EXPORT_FORMAT_MINIMAL
, /* also strip signature */
71 } arg_export_format
= EXPORT_FORMAT_FULL
;
73 STATIC_DESTRUCTOR_REGISTER(arg_identity_extra
, json_variant_unrefp
);
74 STATIC_DESTRUCTOR_REGISTER(arg_identity_extra_this_machine
, json_variant_unrefp
);
75 STATIC_DESTRUCTOR_REGISTER(arg_identity_extra_privileged
, json_variant_unrefp
);
76 STATIC_DESTRUCTOR_REGISTER(arg_identity_extra_rlimits
, json_variant_unrefp
);
77 STATIC_DESTRUCTOR_REGISTER(arg_identity_filter
, strv_freep
);
78 STATIC_DESTRUCTOR_REGISTER(arg_identity_filter_rlimits
, strv_freep
);
79 STATIC_DESTRUCTOR_REGISTER(arg_pkcs11_token_uri
, strv_freep
);
80 STATIC_DESTRUCTOR_REGISTER(arg_fido2_device
, strv_freep
);
82 static const BusLocator
*bus_mgr
;
84 static bool identity_properties_specified(void) {
87 !json_variant_is_blank_object(arg_identity_extra
) ||
88 !json_variant_is_blank_object(arg_identity_extra_privileged
) ||
89 !json_variant_is_blank_object(arg_identity_extra_this_machine
) ||
90 !json_variant_is_blank_object(arg_identity_extra_rlimits
) ||
91 !strv_isempty(arg_identity_filter
) ||
92 !strv_isempty(arg_identity_filter_rlimits
) ||
93 !strv_isempty(arg_pkcs11_token_uri
) ||
94 !strv_isempty(arg_fido2_device
);
97 static int acquire_bus(sd_bus
**bus
) {
105 r
= bus_connect_transport(arg_transport
, arg_host
, false, bus
);
107 return bus_log_connect_error(r
);
109 (void) sd_bus_set_allow_interactive_authorization(*bus
, arg_ask_password
);
114 static int list_homes(int argc
, char *argv
[], void *userdata
) {
115 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
116 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
117 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
118 _cleanup_(table_unrefp
) Table
*table
= NULL
;
121 r
= acquire_bus(&bus
);
125 r
= bus_call_method(bus
, bus_mgr
, "ListHomes", &error
, &reply
, NULL
);
127 return log_error_errno(r
, "Failed to list homes: %s", bus_error_message(&error
, r
));
129 table
= table_new("name", "uid", "gid", "state", "realname", "home", "shell");
133 r
= sd_bus_message_enter_container(reply
, 'a', "(susussso)");
135 return bus_log_parse_error(r
);
138 const char *name
, *state
, *realname
, *home
, *shell
, *color
;
142 r
= sd_bus_message_read(reply
, "(susussso)", &name
, &uid
, &state
, &gid
, &realname
, &home
, &shell
, NULL
);
144 return bus_log_parse_error(r
);
148 r
= table_add_many(table
,
153 return table_log_add_error(r
);
156 r
= table_add_cell(table
, &cell
, TABLE_STRING
, state
);
158 return table_log_add_error(r
);
160 color
= user_record_state_color(state
);
162 (void) table_set_color(table
, cell
, color
);
164 r
= table_add_many(table
,
165 TABLE_STRING
, strna(empty_to_null(realname
)),
167 TABLE_STRING
, strna(empty_to_null(shell
)));
169 return table_log_add_error(r
);
172 r
= sd_bus_message_exit_container(reply
);
174 return bus_log_parse_error(r
);
176 if (table_get_rows(table
) > 1 || !FLAGS_SET(arg_json_format_flags
, JSON_FORMAT_OFF
)) {
177 r
= table_set_sort(table
, (size_t) 0);
179 return table_log_sort_error(r
);
181 r
= table_print_with_pager(table
, arg_json_format_flags
, arg_pager_flags
, arg_legend
);
186 if (arg_legend
&& (arg_json_format_flags
& JSON_FORMAT_OFF
)) {
187 if (table_get_rows(table
) > 1)
188 printf("\n%zu home areas listed.\n", table_get_rows(table
) - 1);
190 printf("No home areas.\n");
196 static int acquire_existing_password(
197 const char *user_name
,
199 bool emphasize_current
,
200 AskPasswordFlags flags
) {
202 _cleanup_(strv_free_erasep
) char **password
= NULL
;
203 _cleanup_free_
char *question
= NULL
;
210 e
= getenv("PASSWORD");
212 /* People really shouldn't use environment variables for passing passwords. We support this
213 * only for testing purposes, and do not document the behaviour, so that people won't
214 * actually use this outside of testing. */
216 r
= user_record_set_password(hr
, STRV_MAKE(e
), true);
218 return log_error_errno(r
, "Failed to store password: %m");
220 assert_se(unsetenv_erase("PASSWORD") >= 0);
224 /* If this is not our own user, then don't use the password cache */
225 if (is_this_me(user_name
) <= 0)
226 SET_FLAG(flags
, ASK_PASSWORD_ACCEPT_CACHED
|ASK_PASSWORD_PUSH_CACHE
, false);
228 if (asprintf(&question
, emphasize_current
?
229 "Please enter current password for user %s:" :
230 "Please enter password for user %s:",
234 r
= ask_password_auto(question
,
235 /* icon= */ "user-home",
237 /* key_name= */ "home-password",
238 /* credential_name= */ "home.password",
242 if (r
== -EUNATCH
) { /* EUNATCH is returned if no password was found and asking interactively was
243 * disabled via the flags. Not an error for us. */
244 log_debug_errno(r
, "No passwords acquired.");
248 return log_error_errno(r
, "Failed to acquire password: %m");
250 r
= user_record_set_password(hr
, password
, true);
252 return log_error_errno(r
, "Failed to store password: %m");
257 static int acquire_token_pin(
258 const char *user_name
,
260 AskPasswordFlags flags
) {
262 _cleanup_(strv_free_erasep
) char **pin
= NULL
;
263 _cleanup_free_
char *question
= NULL
;
272 r
= user_record_set_token_pin(hr
, STRV_MAKE(e
), false);
274 return log_error_errno(r
, "Failed to store token PIN: %m");
276 assert_se(unsetenv_erase("PIN") >= 0);
280 /* If this is not our own user, then don't use the password cache */
281 if (is_this_me(user_name
) <= 0)
282 SET_FLAG(flags
, ASK_PASSWORD_ACCEPT_CACHED
|ASK_PASSWORD_PUSH_CACHE
, false);
284 if (asprintf(&question
, "Please enter security token PIN for user %s:", user_name
) < 0)
287 r
= ask_password_auto(
289 /* icon= */ "user-home",
291 /* key_name= */ "token-pin",
292 /* credential_name= */ "home.token-pin",
296 if (r
== -EUNATCH
) { /* EUNATCH is returned if no PIN was found and asking interactively was disabled
297 * via the flags. Not an error for us. */
298 log_debug_errno(r
, "No security token PINs acquired.");
302 return log_error_errno(r
, "Failed to acquire security token PIN: %m");
304 r
= user_record_set_token_pin(hr
, pin
, false);
306 return log_error_errno(r
, "Failed to store security token PIN: %m");
311 static int handle_generic_user_record_error(
312 const char *user_name
,
314 const sd_bus_error
*error
,
316 bool emphasize_current_password
) {
322 if (sd_bus_error_has_name(error
, BUS_ERROR_HOME_ABSENT
))
323 return log_error_errno(SYNTHETIC_ERRNO(EREMOTE
),
324 "Home of user %s is currently absent, please plug in the necessary storage device or backing file system.", user_name
);
326 else if (sd_bus_error_has_name(error
, BUS_ERROR_AUTHENTICATION_LIMIT_HIT
))
327 return log_error_errno(SYNTHETIC_ERRNO(ETOOMANYREFS
),
328 "Too frequent unsuccessful login attempts for user %s, try again later.", user_name
);
330 else if (sd_bus_error_has_name(error
, BUS_ERROR_BAD_PASSWORD
)) {
332 if (!strv_isempty(hr
->password
))
333 log_notice("Password incorrect or not sufficient, please try again.");
335 /* Don't consume cache entries or credentials here, we already tried that unsuccessfully. But
336 * let's push what we acquire here into the cache */
337 r
= acquire_existing_password(
340 emphasize_current_password
,
341 ASK_PASSWORD_PUSH_CACHE
| ASK_PASSWORD_NO_CREDENTIAL
);
345 } else if (sd_bus_error_has_name(error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
)) {
347 if (strv_isempty(hr
->password
))
348 log_notice("Security token not inserted, please enter password.");
350 log_notice("Password incorrect or not sufficient, and configured security token not inserted, please try again.");
352 r
= acquire_existing_password(
355 emphasize_current_password
,
356 ASK_PASSWORD_PUSH_CACHE
| ASK_PASSWORD_NO_CREDENTIAL
);
360 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_PIN_NEEDED
)) {
362 /* First time the PIN is requested, let's accept cached data, and allow using credential store */
363 r
= acquire_token_pin(
366 ASK_PASSWORD_ACCEPT_CACHED
| ASK_PASSWORD_PUSH_CACHE
);
370 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_PROTECTED_AUTHENTICATION_PATH_NEEDED
)) {
372 log_notice("%s%sPlease authenticate physically on security token.",
373 emoji_enabled() ? special_glyph(SPECIAL_GLYPH_TOUCH
) : "",
374 emoji_enabled() ? " " : "");
376 r
= user_record_set_pkcs11_protected_authentication_path_permitted(hr
, true);
378 return log_error_errno(r
, "Failed to set PKCS#11 protected authentication path permitted flag: %m");
380 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_USER_PRESENCE_NEEDED
)) {
382 log_notice("%s%sPlease confirm presence on security token.",
383 emoji_enabled() ? special_glyph(SPECIAL_GLYPH_TOUCH
) : "",
384 emoji_enabled() ? " " : "");
386 r
= user_record_set_fido2_user_presence_permitted(hr
, true);
388 return log_error_errno(r
, "Failed to set FIDO2 user presence permitted flag: %m");
390 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_USER_VERIFICATION_NEEDED
)) {
392 log_notice("%s%sPlease verify user on security token.",
393 emoji_enabled() ? special_glyph(SPECIAL_GLYPH_TOUCH
) : "",
394 emoji_enabled() ? " " : "");
396 r
= user_record_set_fido2_user_verification_permitted(hr
, true);
398 return log_error_errno(r
, "Failed to set FIDO2 user verification permitted flag: %m");
400 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_PIN_LOCKED
))
401 return log_error_errno(SYNTHETIC_ERRNO(EPERM
), "Security token PIN is locked, please unlock it first. (Hint: Removal and re-insertion might suffice.)");
403 else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_BAD_PIN
)) {
405 log_notice("Security token PIN incorrect, please try again.");
407 /* If the previous PIN was wrong don't accept cached info anymore, but add to cache. Also, don't use the credential data */
408 r
= acquire_token_pin(
411 ASK_PASSWORD_PUSH_CACHE
| ASK_PASSWORD_NO_CREDENTIAL
);
415 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_BAD_PIN_FEW_TRIES_LEFT
)) {
417 log_notice("Security token PIN incorrect, please try again (only a few tries left!).");
419 r
= acquire_token_pin(
422 ASK_PASSWORD_PUSH_CACHE
| ASK_PASSWORD_NO_CREDENTIAL
);
426 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_BAD_PIN_ONE_TRY_LEFT
)) {
428 log_notice("Security token PIN incorrect, please try again (only one try left!).");
430 r
= acquire_token_pin(
433 ASK_PASSWORD_PUSH_CACHE
| ASK_PASSWORD_NO_CREDENTIAL
);
437 return log_error_errno(ret
, "Operation on home %s failed: %s", user_name
, bus_error_message(error
, ret
));
442 static int acquire_passed_secrets(const char *user_name
, UserRecord
**ret
) {
443 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
448 /* Generates an initial secret objects that contains passwords supplied via $PASSWORD, the password
449 * cache or the credentials subsystem, but excluding any interactive stuff. If nothing is passed,
450 * returns an empty secret object. */
452 secret
= user_record_new();
456 r
= acquire_existing_password(
459 /* emphasize_current_password = */ false,
460 ASK_PASSWORD_ACCEPT_CACHED
| ASK_PASSWORD_NO_TTY
| ASK_PASSWORD_NO_AGENT
);
464 r
= acquire_token_pin(
467 ASK_PASSWORD_ACCEPT_CACHED
| ASK_PASSWORD_NO_TTY
| ASK_PASSWORD_NO_AGENT
);
471 *ret
= TAKE_PTR(secret
);
475 static int activate_home(int argc
, char *argv
[], void *userdata
) {
476 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
480 r
= acquire_bus(&bus
);
484 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
485 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
487 r
= acquire_passed_secrets(*i
, &secret
);
492 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
493 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
495 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ActivateHome");
497 return bus_log_create_error(r
);
499 r
= sd_bus_message_append(m
, "s", *i
);
501 return bus_log_create_error(r
);
503 r
= bus_message_append_secret(m
, secret
);
505 return bus_log_create_error(r
);
507 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
509 r
= handle_generic_user_record_error(*i
, secret
, &error
, r
, /* emphasize_current_password= */ false);
524 static int deactivate_home(int argc
, char *argv
[], void *userdata
) {
525 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
529 r
= acquire_bus(&bus
);
533 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
534 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
535 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
537 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "DeactivateHome");
539 return bus_log_create_error(r
);
541 r
= sd_bus_message_append(m
, "s", *i
);
543 return bus_log_create_error(r
);
545 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
547 log_error_errno(r
, "Failed to deactivate user home: %s", bus_error_message(&error
, r
));
556 static void dump_home_record(UserRecord
*hr
) {
561 if (hr
->incomplete
) {
563 log_warning("Warning: lacking rights to acquire privileged fields of user record of '%s', output incomplete.", hr
->user_name
);
566 if (arg_json_format_flags
& JSON_FORMAT_OFF
)
567 user_record_show(hr
, true);
569 _cleanup_(user_record_unrefp
) UserRecord
*stripped
= NULL
;
571 if (arg_export_format
== EXPORT_FORMAT_STRIPPED
)
572 r
= user_record_clone(hr
, USER_RECORD_EXTRACT_EMBEDDED
|USER_RECORD_PERMISSIVE
, &stripped
);
573 else if (arg_export_format
== EXPORT_FORMAT_MINIMAL
)
574 r
= user_record_clone(hr
, USER_RECORD_EXTRACT_SIGNABLE
|USER_RECORD_PERMISSIVE
, &stripped
);
578 log_warning_errno(r
, "Failed to strip user record, ignoring: %m");
582 json_variant_dump(hr
->json
, arg_json_format_flags
, stdout
, NULL
);
586 static char **mangle_user_list(char **list
, char ***ret_allocated
) {
587 _cleanup_free_
char *myself
= NULL
;
590 if (!strv_isempty(list
)) {
591 *ret_allocated
= NULL
;
595 myself
= getusername_malloc();
603 l
[0] = TAKE_PTR(myself
);
610 static int inspect_home(int argc
, char *argv
[], void *userdata
) {
611 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
612 _cleanup_(strv_freep
) char **mangled_list
= NULL
;
616 pager_open(arg_pager_flags
);
618 r
= acquire_bus(&bus
);
622 items
= mangle_user_list(strv_skip(argv
, 1), &mangled_list
);
626 STRV_FOREACH(i
, items
) {
627 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
628 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
629 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
630 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
635 r
= parse_uid(*i
, &uid
);
637 if (!valid_user_group_name(*i
, 0)) {
638 log_error("Invalid user name '%s'.", *i
);
645 r
= bus_call_method(bus
, bus_mgr
, "GetUserRecordByName", &error
, &reply
, "s", *i
);
647 r
= bus_call_method(bus
, bus_mgr
, "GetUserRecordByUID", &error
, &reply
, "u", (uint32_t) uid
);
650 log_error_errno(r
, "Failed to inspect home: %s", bus_error_message(&error
, r
));
657 r
= sd_bus_message_read(reply
, "sbo", &json
, &incomplete
, NULL
);
659 bus_log_parse_error(r
);
666 r
= json_parse(json
, JSON_PARSE_SENSITIVE
, &v
, NULL
, NULL
);
668 log_error_errno(r
, "Failed to parse JSON identity: %m");
675 hr
= user_record_new();
679 r
= user_record_load(hr
, v
, USER_RECORD_LOAD_REFUSE_SECRET
|USER_RECORD_LOG
|USER_RECORD_PERMISSIVE
);
687 hr
->incomplete
= incomplete
;
688 dump_home_record(hr
);
694 static int authenticate_home(int argc
, char *argv
[], void *userdata
) {
695 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
696 _cleanup_(strv_freep
) char **mangled_list
= NULL
;
700 items
= mangle_user_list(strv_skip(argv
, 1), &mangled_list
);
704 r
= acquire_bus(&bus
);
708 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
710 STRV_FOREACH(i
, items
) {
711 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
713 r
= acquire_passed_secrets(*i
, &secret
);
718 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
719 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
721 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "AuthenticateHome");
723 return bus_log_create_error(r
);
725 r
= sd_bus_message_append(m
, "s", *i
);
727 return bus_log_create_error(r
);
729 r
= bus_message_append_secret(m
, secret
);
731 return bus_log_create_error(r
);
733 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
735 r
= handle_generic_user_record_error(*i
, secret
, &error
, r
, false);
750 static int update_last_change(JsonVariant
**v
, bool with_password
, bool override
) {
757 n
= now(CLOCK_REALTIME
);
759 c
= json_variant_by_key(*v
, "lastChangeUSec");
764 goto update_password
;
766 if (!json_variant_is_unsigned(c
))
767 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastChangeUSec field is not an unsigned integer, refusing.");
769 u
= json_variant_unsigned(c
);
771 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastChangeUSec is from the future, can't update.");
774 r
= json_variant_set_field_unsigned(v
, "lastChangeUSec", n
);
776 return log_error_errno(r
, "Failed to update lastChangeUSec: %m");
782 c
= json_variant_by_key(*v
, "lastPasswordChangeUSec");
789 if (!json_variant_is_unsigned(c
))
790 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastPasswordChangeUSec field is not an unsigned integer, refusing.");
792 u
= json_variant_unsigned(c
);
794 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastPasswordChangeUSec is from the future, can't update.");
797 r
= json_variant_set_field_unsigned(v
, "lastPasswordChangeUSec", n
);
799 return log_error_errno(r
, "Failed to update lastPasswordChangeUSec: %m");
804 static int apply_identity_changes(JsonVariant
**_v
) {
805 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
810 v
= json_variant_ref(*_v
);
812 r
= json_variant_filter(&v
, arg_identity_filter
);
814 return log_error_errno(r
, "Failed to filter identity: %m");
816 r
= json_variant_merge(&v
, arg_identity_extra
);
818 return log_error_errno(r
, "Failed to merge identities: %m");
820 if (arg_identity_extra_this_machine
|| !strv_isempty(arg_identity_filter
)) {
821 _cleanup_(json_variant_unrefp
) JsonVariant
*per_machine
= NULL
, *mmid
= NULL
;
824 r
= sd_id128_get_machine(&mid
);
826 return log_error_errno(r
, "Failed to acquire machine ID: %m");
828 r
= json_variant_new_string(&mmid
, SD_ID128_TO_STRING(mid
));
830 return log_error_errno(r
, "Failed to allocate matchMachineId object: %m");
832 per_machine
= json_variant_ref(json_variant_by_key(v
, "perMachine"));
834 _cleanup_(json_variant_unrefp
) JsonVariant
*npm
= NULL
, *add
= NULL
;
835 _cleanup_free_ JsonVariant
**array
= NULL
;
839 if (!json_variant_is_array(per_machine
))
840 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "perMachine field is not an array, refusing.");
842 array
= new(JsonVariant
*, json_variant_elements(per_machine
) + 1);
846 JSON_VARIANT_ARRAY_FOREACH(z
, per_machine
) {
849 if (!json_variant_is_object(z
))
850 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "perMachine entry is not an object, refusing.");
854 u
= json_variant_by_key(z
, "matchMachineId");
858 if (!json_variant_equal(u
, mmid
))
861 r
= json_variant_merge(&add
, z
);
863 return log_error_errno(r
, "Failed to merge perMachine entry: %m");
868 r
= json_variant_filter(&add
, arg_identity_filter
);
870 return log_error_errno(r
, "Failed to filter perMachine: %m");
872 r
= json_variant_merge(&add
, arg_identity_extra_this_machine
);
874 return log_error_errno(r
, "Failed to merge in perMachine fields: %m");
876 if (arg_identity_filter_rlimits
|| arg_identity_extra_rlimits
) {
877 _cleanup_(json_variant_unrefp
) JsonVariant
*rlv
= NULL
;
879 rlv
= json_variant_ref(json_variant_by_key(add
, "resourceLimits"));
881 r
= json_variant_filter(&rlv
, arg_identity_filter_rlimits
);
883 return log_error_errno(r
, "Failed to filter resource limits: %m");
885 r
= json_variant_merge(&rlv
, arg_identity_extra_rlimits
);
887 return log_error_errno(r
, "Failed to set resource limits: %m");
889 if (json_variant_is_blank_object(rlv
)) {
890 r
= json_variant_filter(&add
, STRV_MAKE("resourceLimits"));
892 return log_error_errno(r
, "Failed to drop resource limits field from identity: %m");
894 r
= json_variant_set_field(&add
, "resourceLimits", rlv
);
896 return log_error_errno(r
, "Failed to update resource limits of identity: %m");
900 if (!json_variant_is_blank_object(add
)) {
901 r
= json_variant_set_field(&add
, "matchMachineId", mmid
);
903 return log_error_errno(r
, "Failed to set matchMachineId field: %m");
908 r
= json_variant_new_array(&npm
, array
, i
);
910 return log_error_errno(r
, "Failed to allocate new perMachine array: %m");
912 json_variant_unref(per_machine
);
913 per_machine
= TAKE_PTR(npm
);
915 _cleanup_(json_variant_unrefp
) JsonVariant
*item
= json_variant_ref(arg_identity_extra_this_machine
);
917 if (arg_identity_extra_rlimits
) {
918 r
= json_variant_set_field(&item
, "resourceLimits", arg_identity_extra_rlimits
);
920 return log_error_errno(r
, "Failed to update resource limits of identity: %m");
923 r
= json_variant_set_field(&item
, "matchMachineId", mmid
);
925 return log_error_errno(r
, "Failed to set matchMachineId field: %m");
927 r
= json_variant_append_array(&per_machine
, item
);
929 return log_error_errno(r
, "Failed to append to perMachine array: %m");
932 r
= json_variant_set_field(&v
, "perMachine", per_machine
);
934 return log_error_errno(r
, "Failed to update per machine record: %m");
937 if (arg_identity_extra_privileged
|| arg_identity_filter
) {
938 _cleanup_(json_variant_unrefp
) JsonVariant
*privileged
= NULL
;
940 privileged
= json_variant_ref(json_variant_by_key(v
, "privileged"));
942 r
= json_variant_filter(&privileged
, arg_identity_filter
);
944 return log_error_errno(r
, "Failed to filter identity (privileged part): %m");
946 r
= json_variant_merge(&privileged
, arg_identity_extra_privileged
);
948 return log_error_errno(r
, "Failed to merge identities (privileged part): %m");
950 if (json_variant_is_blank_object(privileged
)) {
951 r
= json_variant_filter(&v
, STRV_MAKE("privileged"));
953 return log_error_errno(r
, "Failed to drop privileged part from identity: %m");
955 r
= json_variant_set_field(&v
, "privileged", privileged
);
957 return log_error_errno(r
, "Failed to update privileged part of identity: %m");
961 if (arg_identity_filter_rlimits
) {
962 _cleanup_(json_variant_unrefp
) JsonVariant
*rlv
= NULL
;
964 rlv
= json_variant_ref(json_variant_by_key(v
, "resourceLimits"));
966 r
= json_variant_filter(&rlv
, arg_identity_filter_rlimits
);
968 return log_error_errno(r
, "Failed to filter resource limits: %m");
970 /* Note that we only filter resource limits here, but don't apply them. We do that in the perMachine section */
972 if (json_variant_is_blank_object(rlv
)) {
973 r
= json_variant_filter(&v
, STRV_MAKE("resourceLimits"));
975 return log_error_errno(r
, "Failed to drop resource limits field from identity: %m");
977 r
= json_variant_set_field(&v
, "resourceLimits", rlv
);
979 return log_error_errno(r
, "Failed to update resource limits of identity: %m");
983 json_variant_unref(*_v
);
989 static int add_disposition(JsonVariant
**v
) {
994 if (json_variant_by_key(*v
, "disposition"))
997 /* Set the disposition to regular, if not configured explicitly */
998 r
= json_variant_set_field_string(v
, "disposition", "regular");
1000 return log_error_errno(r
, "Failed to set disposition field: %m");
1005 static int acquire_new_home_record(UserRecord
**ret
) {
1006 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
1007 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
1014 unsigned line
, column
;
1016 r
= json_parse_file(
1017 streq(arg_identity
, "-") ? stdin
: NULL
,
1018 streq(arg_identity
, "-") ? "<stdin>" : arg_identity
, JSON_PARSE_SENSITIVE
, &v
, &line
, &column
);
1020 return log_error_errno(r
, "Failed to parse identity at %u:%u: %m", line
, column
);
1023 r
= apply_identity_changes(&v
);
1027 r
= add_disposition(&v
);
1031 STRV_FOREACH(i
, arg_pkcs11_token_uri
) {
1032 r
= identity_add_pkcs11_key_data(&v
, *i
);
1037 STRV_FOREACH(i
, arg_fido2_device
) {
1038 r
= identity_add_fido2_parameters(&v
, *i
, arg_fido2_lock_with
);
1043 if (arg_recovery_key
) {
1044 r
= identity_add_recovery_key(&v
);
1049 r
= update_last_change(&v
, true, false);
1054 json_variant_dump(v
, JSON_FORMAT_PRETTY
, NULL
, NULL
);
1056 hr
= user_record_new();
1060 r
= user_record_load(hr
, v
, USER_RECORD_REQUIRE_REGULAR
|USER_RECORD_ALLOW_SECRET
|USER_RECORD_ALLOW_PRIVILEGED
|USER_RECORD_ALLOW_PER_MACHINE
|USER_RECORD_ALLOW_SIGNATURE
|USER_RECORD_LOG
|USER_RECORD_PERMISSIVE
);
1064 *ret
= TAKE_PTR(hr
);
1068 static int acquire_new_password(
1069 const char *user_name
,
1081 e
= getenv("NEWPASSWORD");
1083 _cleanup_(erase_and_freep
) char *copy
= NULL
;
1085 /* As above, this is not for use, just for testing */
1093 r
= user_record_set_password(hr
, STRV_MAKE(e
), /* prepend = */ true);
1095 return log_error_errno(r
, "Failed to store password: %m");
1097 assert_se(unsetenv_erase("NEWPASSWORD") >= 0);
1100 *ret
= TAKE_PTR(copy
);
1106 (void) suggest_passwords();
1109 _cleanup_(strv_free_erasep
) char **first
= NULL
, **second
= NULL
;
1110 _cleanup_free_
char *question
= NULL
;
1113 return log_error_errno(SYNTHETIC_ERRNO(ENOKEY
), "Too many attempts, giving up:");
1115 if (asprintf(&question
, "Please enter new password for user %s:", user_name
) < 0)
1118 r
= ask_password_auto(
1120 /* icon= */ "user-home",
1122 /* key_name= */ "home-password",
1123 /* credential_name= */ "home.new-password",
1125 0, /* no caching, we want to collect a new password here after all */
1128 return log_error_errno(r
, "Failed to acquire password: %m");
1130 question
= mfree(question
);
1131 if (asprintf(&question
, "Please enter new password for user %s (repeat):", user_name
) < 0)
1134 r
= ask_password_auto(
1136 /* icon= */ "user-home",
1138 /* key_name= */ "home-password",
1139 /* credential_name= */ "home.new-password",
1144 return log_error_errno(r
, "Failed to acquire password: %m");
1146 if (strv_equal(first
, second
)) {
1147 _cleanup_(erase_and_freep
) char *copy
= NULL
;
1150 copy
= strdup(first
[0]);
1155 r
= user_record_set_password(hr
, first
, /* prepend = */ true);
1157 return log_error_errno(r
, "Failed to store password: %m");
1160 *ret
= TAKE_PTR(copy
);
1165 log_error("Password didn't match, try again.");
1169 static int create_home(int argc
, char *argv
[], void *userdata
) {
1170 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1171 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
1174 r
= acquire_bus(&bus
);
1178 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1181 /* If a username was specified, use it */
1183 if (valid_user_group_name(argv
[1], 0))
1184 r
= json_variant_set_field_string(&arg_identity_extra
, "userName", argv
[1]);
1186 _cleanup_free_
char *un
= NULL
, *rr
= NULL
;
1188 /* Before we consider the user name invalid, let's check if we can split it? */
1189 r
= split_user_name_realm(argv
[1], &un
, &rr
);
1191 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User name '%s' is not valid: %m", argv
[1]);
1194 r
= json_variant_set_field_string(&arg_identity_extra
, "realm", rr
);
1196 return log_error_errno(r
, "Failed to set realm field: %m");
1199 r
= json_variant_set_field_string(&arg_identity_extra
, "userName", un
);
1202 return log_error_errno(r
, "Failed to set userName field: %m");
1204 /* If neither a username nor an identity have been specified we cannot operate. */
1206 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User name required.");
1209 r
= acquire_new_home_record(&hr
);
1213 /* If the JSON record carries no plain text password (besides the recovery key), then let's query it
1215 if (strv_length(hr
->password
) <= arg_recovery_key
) {
1217 if (strv_isempty(hr
->hashed_password
)) {
1218 _cleanup_(erase_and_freep
) char *new_password
= NULL
;
1220 /* No regular (i.e. non-PKCS#11) hashed passwords set in the record, let's fix that. */
1221 r
= acquire_new_password(hr
->user_name
, hr
, /* suggest = */ true, &new_password
);
1225 r
= user_record_make_hashed_password(hr
, STRV_MAKE(new_password
), /* extend = */ false);
1227 return log_error_errno(r
, "Failed to hash password: %m");
1229 /* There's a hash password set in the record, acquire the unhashed version of it. */
1230 r
= acquire_existing_password(
1233 /* emphasize_current= */ false,
1234 ASK_PASSWORD_ACCEPT_CACHED
| ASK_PASSWORD_PUSH_CACHE
);
1240 if (hr
->enforce_password_policy
== 0) {
1241 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1243 /* If password quality enforcement is disabled, let's at least warn client side */
1245 r
= user_record_quality_check_password(hr
, hr
, &error
);
1247 log_warning_errno(r
, "Specified password does not pass quality checks (%s), proceeding anyway.", bus_error_message(&error
, r
));
1251 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1252 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1253 _cleanup_(erase_and_freep
) char *formatted
= NULL
;
1255 r
= json_variant_format(hr
->json
, 0, &formatted
);
1257 return log_error_errno(r
, "Failed to format user record: %m");
1259 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "CreateHome");
1261 return bus_log_create_error(r
);
1263 (void) sd_bus_message_sensitive(m
);
1265 r
= sd_bus_message_append(m
, "s", formatted
);
1267 return bus_log_create_error(r
);
1269 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1271 if (sd_bus_error_has_name(&error
, BUS_ERROR_LOW_PASSWORD_QUALITY
)) {
1272 _cleanup_(erase_and_freep
) char *new_password
= NULL
;
1274 log_error_errno(r
, "%s", bus_error_message(&error
, r
));
1275 log_info("(Use --enforce-password-policy=no to turn off password quality checks for this account.)");
1277 r
= acquire_new_password(hr
->user_name
, hr
, /* suggest = */ false, &new_password
);
1281 r
= user_record_make_hashed_password(hr
, STRV_MAKE(new_password
), /* extend = */ false);
1283 return log_error_errno(r
, "Failed to hash passwords: %m");
1285 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1296 static int remove_home(int argc
, char *argv
[], void *userdata
) {
1297 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1301 r
= acquire_bus(&bus
);
1305 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1307 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
1308 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1309 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1311 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "RemoveHome");
1313 return bus_log_create_error(r
);
1315 r
= sd_bus_message_append(m
, "s", *i
);
1317 return bus_log_create_error(r
);
1319 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1321 log_error_errno(r
, "Failed to remove home: %s", bus_error_message(&error
, r
));
1330 static int acquire_updated_home_record(
1332 const char *username
,
1335 _cleanup_(json_variant_unrefp
) JsonVariant
*json
= NULL
;
1336 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
1343 unsigned line
, column
;
1346 r
= json_parse_file(
1347 streq(arg_identity
, "-") ? stdin
: NULL
,
1348 streq(arg_identity
, "-") ? "<stdin>" : arg_identity
, JSON_PARSE_SENSITIVE
, &json
, &line
, &column
);
1350 return log_error_errno(r
, "Failed to parse identity at %u:%u: %m", line
, column
);
1352 un
= json_variant_by_key(json
, "userName");
1354 if (!json_variant_is_string(un
) || (username
&& !streq(json_variant_string(un
), username
)))
1355 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User name specified on command line and in JSON record do not match.");
1358 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "No username specified.");
1360 r
= json_variant_set_field_string(&arg_identity_extra
, "userName", username
);
1362 return log_error_errno(r
, "Failed to set userName field: %m");
1366 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1367 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1371 if (!identity_properties_specified())
1372 return log_error_errno(SYNTHETIC_ERRNO(EALREADY
), "No field to change specified.");
1374 r
= bus_call_method(bus
, bus_mgr
, "GetUserRecordByName", &error
, &reply
, "s", username
);
1376 return log_error_errno(r
, "Failed to acquire user home record: %s", bus_error_message(&error
, r
));
1378 r
= sd_bus_message_read(reply
, "sbo", &text
, &incomplete
, NULL
);
1380 return bus_log_parse_error(r
);
1383 return log_error_errno(SYNTHETIC_ERRNO(EACCES
), "Lacking rights to acquire user record including privileged metadata, can't update record.");
1385 r
= json_parse(text
, JSON_PARSE_SENSITIVE
, &json
, NULL
, NULL
);
1387 return log_error_errno(r
, "Failed to parse JSON identity: %m");
1389 reply
= sd_bus_message_unref(reply
);
1391 r
= json_variant_filter(&json
, STRV_MAKE("binding", "status", "signature"));
1393 return log_error_errno(r
, "Failed to strip binding and status from record to update: %m");
1396 r
= apply_identity_changes(&json
);
1400 STRV_FOREACH(i
, arg_pkcs11_token_uri
) {
1401 r
= identity_add_pkcs11_key_data(&json
, *i
);
1406 STRV_FOREACH(i
, arg_fido2_device
) {
1407 r
= identity_add_fido2_parameters(&json
, *i
, arg_fido2_lock_with
);
1412 /* If the user supplied a full record, then add in lastChange, but do not override. Otherwise always
1414 r
= update_last_change(&json
, arg_pkcs11_token_uri
|| arg_fido2_device
, !arg_identity
);
1419 json_variant_dump(json
, JSON_FORMAT_PRETTY
, NULL
, NULL
);
1421 hr
= user_record_new();
1425 r
= user_record_load(hr
, json
, USER_RECORD_REQUIRE_REGULAR
|USER_RECORD_ALLOW_PRIVILEGED
|USER_RECORD_ALLOW_PER_MACHINE
|USER_RECORD_ALLOW_SECRET
|USER_RECORD_ALLOW_SIGNATURE
|USER_RECORD_LOG
|USER_RECORD_PERMISSIVE
);
1429 *ret
= TAKE_PTR(hr
);
1433 static int home_record_reset_human_interaction_permission(UserRecord
*hr
) {
1438 /* When we execute multiple operations one after the other, let's reset the permission to ask the
1439 * user each time, so that if interaction is necessary we will be told so again and thus can print a
1440 * nice message to the user, telling the user so. */
1442 r
= user_record_set_pkcs11_protected_authentication_path_permitted(hr
, -1);
1444 return log_error_errno(r
, "Failed to reset PKCS#11 protected authentication path permission flag: %m");
1446 r
= user_record_set_fido2_user_presence_permitted(hr
, -1);
1448 return log_error_errno(r
, "Failed to reset FIDO2 user presence permission flag: %m");
1450 r
= user_record_set_fido2_user_verification_permitted(hr
, -1);
1452 return log_error_errno(r
, "Failed to reset FIDO2 user verification permission flag: %m");
1457 static int update_home(int argc
, char *argv
[], void *userdata
) {
1458 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1459 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
1460 _cleanup_free_
char *buffer
= NULL
;
1461 const char *username
;
1466 else if (!arg_identity
) {
1467 buffer
= getusername_malloc();
1475 r
= acquire_bus(&bus
);
1479 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1481 r
= acquire_updated_home_record(bus
, username
, &hr
);
1485 /* If we do multiple operations, let's output things more verbosely, since otherwise the repeated
1486 * authentication might be confusing. */
1488 if (arg_and_resize
|| arg_and_change_password
)
1489 log_info("Updating home directory.");
1492 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1493 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1494 _cleanup_free_
char *formatted
= NULL
;
1496 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "UpdateHome");
1498 return bus_log_create_error(r
);
1500 r
= json_variant_format(hr
->json
, 0, &formatted
);
1502 return log_error_errno(r
, "Failed to format user record: %m");
1504 (void) sd_bus_message_sensitive(m
);
1506 r
= sd_bus_message_append(m
, "s", formatted
);
1508 return bus_log_create_error(r
);
1510 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1512 if (arg_and_change_password
&&
1513 sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1514 /* In the generic handler we'd ask for a password in this case, but when
1515 * changing passwords that's not sufficient, as we need to acquire all keys
1517 return log_error_errno(r
, "Security token not inserted, refusing.");
1519 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1527 log_info("Resizing home.");
1529 (void) home_record_reset_human_interaction_permission(hr
);
1531 /* Also sync down disk size to underlying LUKS/fscrypt/quota */
1532 while (arg_and_resize
) {
1533 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1534 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1536 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ResizeHome");
1538 return bus_log_create_error(r
);
1540 /* Specify UINT64_MAX as size, in which case the underlying disk size will just be synced */
1541 r
= sd_bus_message_append(m
, "st", hr
->user_name
, UINT64_MAX
);
1543 return bus_log_create_error(r
);
1545 r
= bus_message_append_secret(m
, hr
);
1547 return bus_log_create_error(r
);
1549 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1551 if (arg_and_change_password
&&
1552 sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1553 return log_error_errno(r
, "Security token not inserted, refusing.");
1555 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1562 if (arg_and_change_password
)
1563 log_info("Synchronizing passwords and encryption keys.");
1565 (void) home_record_reset_human_interaction_permission(hr
);
1567 /* Also sync down passwords to underlying LUKS/fscrypt */
1568 while (arg_and_change_password
) {
1569 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1570 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1572 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ChangePasswordHome");
1574 return bus_log_create_error(r
);
1576 /* Specify an empty new secret, in which case the underlying LUKS/fscrypt password will just be synced */
1577 r
= sd_bus_message_append(m
, "ss", hr
->user_name
, "{}");
1579 return bus_log_create_error(r
);
1581 r
= bus_message_append_secret(m
, hr
);
1583 return bus_log_create_error(r
);
1585 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1587 if (sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1588 return log_error_errno(r
, "Security token not inserted, refusing.");
1590 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1600 static int passwd_home(int argc
, char *argv
[], void *userdata
) {
1601 _cleanup_(user_record_unrefp
) UserRecord
*old_secret
= NULL
, *new_secret
= NULL
;
1602 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1603 _cleanup_free_
char *buffer
= NULL
;
1604 const char *username
;
1607 if (arg_pkcs11_token_uri
)
1608 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "To change the PKCS#11 security token use 'homectl update --pkcs11-token-uri=…'.");
1609 if (arg_fido2_device
)
1610 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "To change the FIDO2 security token use 'homectl update --fido2-device=…'.");
1611 if (identity_properties_specified())
1612 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "The 'passwd' verb does not permit changing other record properties at the same time.");
1617 buffer
= getusername_malloc();
1624 r
= acquire_bus(&bus
);
1628 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1630 old_secret
= user_record_new();
1634 new_secret
= user_record_new();
1638 r
= acquire_new_password(username
, new_secret
, /* suggest = */ true, NULL
);
1643 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1644 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1646 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ChangePasswordHome");
1648 return bus_log_create_error(r
);
1650 r
= sd_bus_message_append(m
, "s", username
);
1652 return bus_log_create_error(r
);
1654 r
= bus_message_append_secret(m
, new_secret
);
1656 return bus_log_create_error(r
);
1658 r
= bus_message_append_secret(m
, old_secret
);
1660 return bus_log_create_error(r
);
1662 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1664 if (sd_bus_error_has_name(&error
, BUS_ERROR_LOW_PASSWORD_QUALITY
)) {
1666 log_error_errno(r
, "%s", bus_error_message(&error
, r
));
1668 r
= acquire_new_password(username
, new_secret
, /* suggest = */ false, NULL
);
1670 } else if (sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1672 /* In the generic handler we'd ask for a password in this case, but when
1673 * changing passwords that's not sufficeint, as we need to acquire all keys
1675 return log_error_errno(r
, "Security token not inserted, refusing.");
1677 r
= handle_generic_user_record_error(username
, old_secret
, &error
, r
, true);
1687 static int resize_home(int argc
, char *argv
[], void *userdata
) {
1688 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1689 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
1690 uint64_t ds
= UINT64_MAX
;
1693 r
= acquire_bus(&bus
);
1697 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1699 if (arg_disk_size_relative
!= UINT64_MAX
||
1700 (argc
> 2 && parse_permyriad(argv
[2]) >= 0))
1701 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
1702 "Relative disk size specification currently not supported when resizing.");
1705 r
= parse_size(argv
[2], 1024, &ds
);
1707 return log_error_errno(r
, "Failed to parse disk size parameter: %s", argv
[2]);
1710 if (arg_disk_size
!= UINT64_MAX
) {
1711 if (ds
!= UINT64_MAX
&& ds
!= arg_disk_size
)
1712 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Disk size specified twice and doesn't match, refusing.");
1717 r
= acquire_passed_secrets(argv
[1], &secret
);
1722 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1723 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1725 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ResizeHome");
1727 return bus_log_create_error(r
);
1729 r
= sd_bus_message_append(m
, "st", argv
[1], ds
);
1731 return bus_log_create_error(r
);
1733 r
= bus_message_append_secret(m
, secret
);
1735 return bus_log_create_error(r
);
1737 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1739 r
= handle_generic_user_record_error(argv
[1], secret
, &error
, r
, false);
1749 static int lock_home(int argc
, char *argv
[], void *userdata
) {
1750 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1754 r
= acquire_bus(&bus
);
1758 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
1759 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1760 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1762 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "LockHome");
1764 return bus_log_create_error(r
);
1766 r
= sd_bus_message_append(m
, "s", *i
);
1768 return bus_log_create_error(r
);
1770 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1772 log_error_errno(r
, "Failed to lock home: %s", bus_error_message(&error
, r
));
1781 static int unlock_home(int argc
, char *argv
[], void *userdata
) {
1782 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1786 r
= acquire_bus(&bus
);
1790 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
1791 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
1793 r
= acquire_passed_secrets(*i
, &secret
);
1798 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1799 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1801 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "UnlockHome");
1803 return bus_log_create_error(r
);
1805 r
= sd_bus_message_append(m
, "s", *i
);
1807 return bus_log_create_error(r
);
1809 r
= bus_message_append_secret(m
, secret
);
1811 return bus_log_create_error(r
);
1813 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1815 r
= handle_generic_user_record_error(argv
[1], secret
, &error
, r
, false);
1830 static int with_home(int argc
, char *argv
[], void *userdata
) {
1831 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1832 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1833 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1834 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
1835 _cleanup_close_
int acquired_fd
= -1;
1836 _cleanup_strv_free_
char **cmdline
= NULL
;
1841 r
= acquire_bus(&bus
);
1846 _cleanup_free_
char *shell
= NULL
;
1848 /* If no command is specified, spawn a shell */
1849 r
= get_shell(&shell
);
1851 return log_error_errno(r
, "Failed to acquire shell: %m");
1853 cmdline
= strv_new(shell
);
1855 cmdline
= strv_copy(argv
+ 2);
1859 r
= acquire_passed_secrets(argv
[1], &secret
);
1864 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "AcquireHome");
1866 return bus_log_create_error(r
);
1868 r
= sd_bus_message_append(m
, "s", argv
[1]);
1870 return bus_log_create_error(r
);
1872 r
= bus_message_append_secret(m
, secret
);
1874 return bus_log_create_error(r
);
1876 r
= sd_bus_message_append(m
, "b", /* please_suspend = */ getenv_bool("SYSTEMD_PLEASE_SUSPEND_HOME") > 0);
1878 return bus_log_create_error(r
);
1880 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, &reply
);
1881 m
= sd_bus_message_unref(m
);
1883 r
= handle_generic_user_record_error(argv
[1], secret
, &error
, r
, false);
1887 sd_bus_error_free(&error
);
1891 r
= sd_bus_message_read(reply
, "h", &fd
);
1893 return bus_log_parse_error(r
);
1895 acquired_fd
= fcntl(fd
, F_DUPFD_CLOEXEC
, 3);
1896 if (acquired_fd
< 0)
1897 return log_error_errno(errno
, "Failed to duplicate acquired fd: %m");
1899 reply
= sd_bus_message_unref(reply
);
1904 r
= bus_call_method(bus
, bus_mgr
, "GetHomeByName", &error
, &reply
, "s", argv
[1]);
1906 return log_error_errno(r
, "Failed to inspect home: %s", bus_error_message(&error
, r
));
1908 r
= sd_bus_message_read(reply
, "usussso", NULL
, NULL
, NULL
, NULL
, &home
, NULL
, NULL
);
1910 return bus_log_parse_error(r
);
1912 r
= safe_fork("(with)", FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_DEATHSIG
|FORK_LOG
|FORK_RLIMIT_NOFILE_SAFE
|FORK_REOPEN_LOG
, &pid
);
1916 if (chdir(home
) < 0) {
1917 log_error_errno(errno
, "Failed to change to directory %s: %m", home
);
1921 execvp(cmdline
[0], cmdline
);
1922 log_error_errno(errno
, "Failed to execute %s: %m", cmdline
[0]);
1926 ret
= wait_for_terminate_and_check(cmdline
[0], pid
, WAIT_LOG_ABNORMAL
);
1928 /* Close the fd that pings the home now. */
1929 acquired_fd
= safe_close(acquired_fd
);
1931 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ReleaseHome");
1933 return bus_log_create_error(r
);
1935 r
= sd_bus_message_append(m
, "s", argv
[1]);
1937 return bus_log_create_error(r
);
1939 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1941 if (sd_bus_error_has_name(&error
, BUS_ERROR_HOME_BUSY
))
1942 log_notice("Not deactivating home directory of %s, as it is still used.", argv
[1]);
1944 return log_error_errno(r
, "Failed to release user home: %s", bus_error_message(&error
, r
));
1950 static int lock_all_homes(int argc
, char *argv
[], void *userdata
) {
1951 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1952 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1953 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1956 r
= acquire_bus(&bus
);
1960 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "LockAllHomes");
1962 return bus_log_create_error(r
);
1964 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1966 return log_error_errno(r
, "Failed to lock all homes: %s", bus_error_message(&error
, r
));
1971 static int deactivate_all_homes(int argc
, char *argv
[], void *userdata
) {
1972 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1973 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1974 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1977 r
= acquire_bus(&bus
);
1981 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "DeactivateAllHomes");
1983 return bus_log_create_error(r
);
1985 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1987 return log_error_errno(r
, "Failed to deactivate all homes: %s", bus_error_message(&error
, r
));
1992 static int drop_from_identity(const char *field
) {
1997 /* If we are called to update an identity record and drop some field, let's keep track of what to
1998 * remove from the old record */
1999 r
= strv_extend(&arg_identity_filter
, field
);
2003 /* Let's also drop the field if it was previously set to a new value on the same command line */
2004 r
= json_variant_filter(&arg_identity_extra
, STRV_MAKE(field
));
2006 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
2008 r
= json_variant_filter(&arg_identity_extra_this_machine
, STRV_MAKE(field
));
2010 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
2012 r
= json_variant_filter(&arg_identity_extra_privileged
, STRV_MAKE(field
));
2014 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
2019 static int help(int argc
, char *argv
[], void *userdata
) {
2020 _cleanup_free_
char *link
= NULL
;
2023 pager_open(arg_pager_flags
);
2025 r
= terminal_urlify_man("homectl", "1", &link
);
2029 printf("%1$s [OPTIONS...] COMMAND ...\n\n"
2030 "%2$sCreate, manipulate or inspect home directories.%3$s\n"
2031 "\n%4$sCommands:%5$s\n"
2032 " list List home areas\n"
2033 " activate USER… Activate a home area\n"
2034 " deactivate USER… Deactivate a home area\n"
2035 " inspect USER… Inspect a home area\n"
2036 " authenticate USER… Authenticate a home area\n"
2037 " create USER Create a home area\n"
2038 " remove USER… Remove a home area\n"
2039 " update USER Update a home area\n"
2040 " passwd USER Change password of a home area\n"
2041 " resize USER SIZE Resize a home area\n"
2042 " lock USER… Temporarily lock an active home area\n"
2043 " unlock USER… Unlock a temporarily locked home area\n"
2044 " lock-all Lock all suitable home areas\n"
2045 " deactivate-all Deactivate all active home areas\n"
2046 " with USER [COMMAND…] Run shell or command with access to a home area\n"
2047 "\n%4$sOptions:%5$s\n"
2048 " -h --help Show this help\n"
2049 " --version Show package version\n"
2050 " --no-pager Do not pipe output into a pager\n"
2051 " --no-legend Do not show the headers and footers\n"
2052 " --no-ask-password Do not ask for system passwords\n"
2053 " -H --host=[USER@]HOST Operate on remote host\n"
2054 " -M --machine=CONTAINER Operate on local container\n"
2055 " --identity=PATH Read JSON identity from file\n"
2056 " --json=FORMAT Output inspection data in JSON (takes one of\n"
2057 " pretty, short, off)\n"
2058 " -j Equivalent to --json=pretty (on TTY) or\n"
2059 " --json=short (otherwise)\n"
2060 " --export-format= Strip JSON inspection data (full, stripped,\n"
2062 " -E When specified once equals -j --export-format=\n"
2063 " stripped, when specified twice equals\n"
2064 " -j --export-format=minimal\n"
2065 "\n%4$sGeneral User Record Properties:%5$s\n"
2066 " -c --real-name=REALNAME Real name for user\n"
2067 " --realm=REALM Realm to create user in\n"
2068 " --email-address=EMAIL Email address for user\n"
2069 " --location=LOCATION Set location of user on earth\n"
2070 " --icon-name=NAME Icon name for user\n"
2071 " -d --home-dir=PATH Home directory\n"
2072 " -u --uid=UID Numeric UID for user\n"
2073 " -G --member-of=GROUP Add user to group\n"
2074 " --skel=PATH Skeleton directory to use\n"
2075 " --shell=PATH Shell for account\n"
2076 " --setenv=VARIABLE[=VALUE] Set an environment variable at log-in\n"
2077 " --timezone=TIMEZONE Set a time-zone\n"
2078 " --language=LOCALE Set preferred language\n"
2079 " --ssh-authorized-keys=KEYS\n"
2080 " Specify SSH public keys\n"
2081 " --pkcs11-token-uri=URI URI to PKCS#11 security token containing\n"
2082 " private key and matching X.509 certificate\n"
2083 " --fido2-device=PATH Path to FIDO2 hidraw device with hmac-secret\n"
2085 " --fido2-with-client-pin=BOOL\n"
2086 " Whether to require entering a PIN to unlock the\n"
2088 " --fido2-with-user-presence=BOOL\n"
2089 " Whether to require user presence to unlock the\n"
2091 " --fido2-with-user-verification=BOOL\n"
2092 " Whether to require user verification to unlock\n"
2094 " --recovery-key=BOOL Add a recovery key\n"
2095 "\n%4$sAccount Management User Record Properties:%5$s\n"
2096 " --locked=BOOL Set locked account state\n"
2097 " --not-before=TIMESTAMP Do not allow logins before\n"
2098 " --not-after=TIMESTAMP Do not allow logins after\n"
2099 " --rate-limit-interval=SECS\n"
2100 " Login rate-limit interval in seconds\n"
2101 " --rate-limit-burst=NUMBER\n"
2102 " Login rate-limit attempts per interval\n"
2103 "\n%4$sPassword Policy User Record Properties:%5$s\n"
2104 " --password-hint=HINT Set Password hint\n"
2105 " --enforce-password-policy=BOOL\n"
2106 " Control whether to enforce system's password\n"
2107 " policy for this user\n"
2108 " -P Same as --enforce-password-password=no\n"
2109 " --password-change-now=BOOL\n"
2110 " Require the password to be changed on next login\n"
2111 " --password-change-min=TIME\n"
2112 " Require minimum time between password changes\n"
2113 " --password-change-max=TIME\n"
2114 " Require maximum time between password changes\n"
2115 " --password-change-warn=TIME\n"
2116 " How much time to warn before password expiry\n"
2117 " --password-change-inactive=TIME\n"
2118 " How much time to block password after expiry\n"
2119 "\n%4$sResource Management User Record Properties:%5$s\n"
2120 " --disk-size=BYTES Size to assign the user on disk\n"
2121 " --access-mode=MODE User home directory access mode\n"
2122 " --umask=MODE Umask for user when logging in\n"
2123 " --nice=NICE Nice level for user\n"
2124 " --rlimit=LIMIT=VALUE[:VALUE]\n"
2125 " Set resource limits\n"
2126 " --tasks-max=MAX Set maximum number of per-user tasks\n"
2127 " --memory-high=BYTES Set high memory threshold in bytes\n"
2128 " --memory-max=BYTES Set maximum memory limit\n"
2129 " --cpu-weight=WEIGHT Set CPU weight\n"
2130 " --io-weight=WEIGHT Set IO weight\n"
2131 "\n%4$sStorage User Record Properties:%5$s\n"
2132 " --storage=STORAGE Storage type to use (luks, fscrypt, directory,\n"
2133 " subvolume, cifs)\n"
2134 " --image-path=PATH Path to image file/directory\n"
2135 " --drop-caches=BOOL Whether to automatically drop caches on logout\n"
2136 "\n%4$sLUKS Storage User Record Properties:%5$s\n"
2137 " --fs-type=TYPE File system type to use in case of luks\n"
2138 " storage (btrfs, ext4, xfs)\n"
2139 " --luks-discard=BOOL Whether to use 'discard' feature of file system\n"
2140 " when activated (mounted)\n"
2141 " --luks-offline-discard=BOOL\n"
2142 " Whether to trim file on logout\n"
2143 " --luks-cipher=CIPHER Cipher to use for LUKS encryption\n"
2144 " --luks-cipher-mode=MODE Cipher mode to use for LUKS encryption\n"
2145 " --luks-volume-key-size=BITS\n"
2146 " Volume key size to use for LUKS encryption\n"
2147 " --luks-pbkdf-type=TYPE Password-based Key Derivation Function to use\n"
2148 " --luks-pbkdf-hash-algorithm=ALGORITHM\n"
2149 " PBKDF hash algorithm to use\n"
2150 " --luks-pbkdf-time-cost=SECS\n"
2151 " Time cost for PBKDF in seconds\n"
2152 " --luks-pbkdf-memory-cost=BYTES\n"
2153 " Memory cost for PBKDF in bytes\n"
2154 " --luks-pbkdf-parallel-threads=NUMBER\n"
2155 " Number of parallel threads for PKBDF\n"
2156 "\n%4$sMounting User Record Properties:%5$s\n"
2157 " --nosuid=BOOL Control the 'nosuid' flag of the home mount\n"
2158 " --nodev=BOOL Control the 'nodev' flag of the home mount\n"
2159 " --noexec=BOOL Control the 'noexec' flag of the home mount\n"
2160 "\n%4$sCIFS User Record Properties:%5$s\n"
2161 " --cifs-domain=DOMAIN CIFS (Windows) domain\n"
2162 " --cifs-user-name=USER CIFS (Windows) user name\n"
2163 " --cifs-service=SERVICE CIFS (Windows) service to mount as home area\n"
2164 " --cifs-extra-mount-options=OPTIONS\n"
2165 " CIFS (Windows) extra mount options\n"
2166 "\n%4$sLogin Behaviour User Record Properties:%5$s\n"
2167 " --stop-delay=SECS How long to leave user services running after\n"
2169 " --kill-processes=BOOL Whether to kill user processes when sessions\n"
2171 " --auto-login=BOOL Try to log this user in automatically\n"
2172 "\nSee the %6$s for details.\n",
2173 program_invocation_short_name
,
2183 static int parse_argv(int argc
, char *argv
[]) {
2186 ARG_VERSION
= 0x100,
2189 ARG_NO_ASK_PASSWORD
,
2199 ARG_LUKS_OFFLINE_DISCARD
,
2205 ARG_SSH_AUTHORIZED_KEYS
,
2214 ARG_LUKS_CIPHER_MODE
,
2215 ARG_LUKS_VOLUME_KEY_SIZE
,
2222 ARG_CIFS_EXTRA_MOUNT_OPTIONS
,
2228 ARG_LUKS_PBKDF_TYPE
,
2229 ARG_LUKS_PBKDF_HASH_ALGORITHM
,
2230 ARG_LUKS_PBKDF_TIME_COST
,
2231 ARG_LUKS_PBKDF_MEMORY_COST
,
2232 ARG_LUKS_PBKDF_PARALLEL_THREADS
,
2233 ARG_RATE_LIMIT_INTERVAL
,
2234 ARG_RATE_LIMIT_BURST
,
2237 ARG_ENFORCE_PASSWORD_POLICY
,
2238 ARG_PASSWORD_CHANGE_NOW
,
2239 ARG_PASSWORD_CHANGE_MIN
,
2240 ARG_PASSWORD_CHANGE_MAX
,
2241 ARG_PASSWORD_CHANGE_WARN
,
2242 ARG_PASSWORD_CHANGE_INACTIVE
,
2245 ARG_PKCS11_TOKEN_URI
,
2252 ARG_AND_CHANGE_PASSWORD
,
2256 static const struct option options
[] = {
2257 { "help", no_argument
, NULL
, 'h' },
2258 { "version", no_argument
, NULL
, ARG_VERSION
},
2259 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
2260 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
2261 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
2262 { "host", required_argument
, NULL
, 'H' },
2263 { "machine", required_argument
, NULL
, 'M' },
2264 { "identity", required_argument
, NULL
, 'I' },
2265 { "real-name", required_argument
, NULL
, 'c' },
2266 { "comment", required_argument
, NULL
, 'c' }, /* Compat alias to keep thing in sync with useradd(8) */
2267 { "realm", required_argument
, NULL
, ARG_REALM
},
2268 { "email-address", required_argument
, NULL
, ARG_EMAIL_ADDRESS
},
2269 { "location", required_argument
, NULL
, ARG_LOCATION
},
2270 { "password-hint", required_argument
, NULL
, ARG_PASSWORD_HINT
},
2271 { "icon-name", required_argument
, NULL
, ARG_ICON_NAME
},
2272 { "home-dir", required_argument
, NULL
, 'd' }, /* Compatible with useradd(8) */
2273 { "uid", required_argument
, NULL
, 'u' }, /* Compatible with useradd(8) */
2274 { "member-of", required_argument
, NULL
, 'G' },
2275 { "groups", required_argument
, NULL
, 'G' }, /* Compat alias to keep thing in sync with useradd(8) */
2276 { "skel", required_argument
, NULL
, 'k' }, /* Compatible with useradd(8) */
2277 { "shell", required_argument
, NULL
, 's' }, /* Compatible with useradd(8) */
2278 { "setenv", required_argument
, NULL
, ARG_SETENV
},
2279 { "timezone", required_argument
, NULL
, ARG_TIMEZONE
},
2280 { "language", required_argument
, NULL
, ARG_LANGUAGE
},
2281 { "locked", required_argument
, NULL
, ARG_LOCKED
},
2282 { "not-before", required_argument
, NULL
, ARG_NOT_BEFORE
},
2283 { "not-after", required_argument
, NULL
, ARG_NOT_AFTER
},
2284 { "expiredate", required_argument
, NULL
, 'e' }, /* Compat alias to keep thing in sync with useradd(8) */
2285 { "ssh-authorized-keys", required_argument
, NULL
, ARG_SSH_AUTHORIZED_KEYS
},
2286 { "disk-size", required_argument
, NULL
, ARG_DISK_SIZE
},
2287 { "access-mode", required_argument
, NULL
, ARG_ACCESS_MODE
},
2288 { "umask", required_argument
, NULL
, ARG_UMASK
},
2289 { "nice", required_argument
, NULL
, ARG_NICE
},
2290 { "rlimit", required_argument
, NULL
, ARG_RLIMIT
},
2291 { "tasks-max", required_argument
, NULL
, ARG_TASKS_MAX
},
2292 { "memory-high", required_argument
, NULL
, ARG_MEMORY_HIGH
},
2293 { "memory-max", required_argument
, NULL
, ARG_MEMORY_MAX
},
2294 { "cpu-weight", required_argument
, NULL
, ARG_CPU_WEIGHT
},
2295 { "io-weight", required_argument
, NULL
, ARG_IO_WEIGHT
},
2296 { "storage", required_argument
, NULL
, ARG_STORAGE
},
2297 { "image-path", required_argument
, NULL
, ARG_IMAGE_PATH
},
2298 { "fs-type", required_argument
, NULL
, ARG_FS_TYPE
},
2299 { "luks-discard", required_argument
, NULL
, ARG_LUKS_DISCARD
},
2300 { "luks-offline-discard", required_argument
, NULL
, ARG_LUKS_OFFLINE_DISCARD
},
2301 { "luks-cipher", required_argument
, NULL
, ARG_LUKS_CIPHER
},
2302 { "luks-cipher-mode", required_argument
, NULL
, ARG_LUKS_CIPHER_MODE
},
2303 { "luks-volume-key-size", required_argument
, NULL
, ARG_LUKS_VOLUME_KEY_SIZE
},
2304 { "luks-pbkdf-type", required_argument
, NULL
, ARG_LUKS_PBKDF_TYPE
},
2305 { "luks-pbkdf-hash-algorithm", required_argument
, NULL
, ARG_LUKS_PBKDF_HASH_ALGORITHM
},
2306 { "luks-pbkdf-time-cost", required_argument
, NULL
, ARG_LUKS_PBKDF_TIME_COST
},
2307 { "luks-pbkdf-memory-cost", required_argument
, NULL
, ARG_LUKS_PBKDF_MEMORY_COST
},
2308 { "luks-pbkdf-parallel-threads", required_argument
, NULL
, ARG_LUKS_PBKDF_PARALLEL_THREADS
},
2309 { "nosuid", required_argument
, NULL
, ARG_NOSUID
},
2310 { "nodev", required_argument
, NULL
, ARG_NODEV
},
2311 { "noexec", required_argument
, NULL
, ARG_NOEXEC
},
2312 { "cifs-user-name", required_argument
, NULL
, ARG_CIFS_USER_NAME
},
2313 { "cifs-domain", required_argument
, NULL
, ARG_CIFS_DOMAIN
},
2314 { "cifs-service", required_argument
, NULL
, ARG_CIFS_SERVICE
},
2315 { "cifs-extra-mount-options", required_argument
, NULL
, ARG_CIFS_EXTRA_MOUNT_OPTIONS
},
2316 { "rate-limit-interval", required_argument
, NULL
, ARG_RATE_LIMIT_INTERVAL
},
2317 { "rate-limit-burst", required_argument
, NULL
, ARG_RATE_LIMIT_BURST
},
2318 { "stop-delay", required_argument
, NULL
, ARG_STOP_DELAY
},
2319 { "kill-processes", required_argument
, NULL
, ARG_KILL_PROCESSES
},
2320 { "enforce-password-policy", required_argument
, NULL
, ARG_ENFORCE_PASSWORD_POLICY
},
2321 { "password-change-now", required_argument
, NULL
, ARG_PASSWORD_CHANGE_NOW
},
2322 { "password-change-min", required_argument
, NULL
, ARG_PASSWORD_CHANGE_MIN
},
2323 { "password-change-max", required_argument
, NULL
, ARG_PASSWORD_CHANGE_MAX
},
2324 { "password-change-warn", required_argument
, NULL
, ARG_PASSWORD_CHANGE_WARN
},
2325 { "password-change-inactive", required_argument
, NULL
, ARG_PASSWORD_CHANGE_INACTIVE
},
2326 { "auto-login", required_argument
, NULL
, ARG_AUTO_LOGIN
},
2327 { "json", required_argument
, NULL
, ARG_JSON
},
2328 { "export-format", required_argument
, NULL
, ARG_EXPORT_FORMAT
},
2329 { "pkcs11-token-uri", required_argument
, NULL
, ARG_PKCS11_TOKEN_URI
},
2330 { "fido2-device", required_argument
, NULL
, ARG_FIDO2_DEVICE
},
2331 { "fido2-with-client-pin", required_argument
, NULL
, ARG_FIDO2_WITH_PIN
},
2332 { "fido2-with-user-presence", required_argument
, NULL
, ARG_FIDO2_WITH_UP
},
2333 { "fido2-with-user-verification",required_argument
, NULL
, ARG_FIDO2_WITH_UV
},
2334 { "recovery-key", required_argument
, NULL
, ARG_RECOVERY_KEY
},
2335 { "and-resize", required_argument
, NULL
, ARG_AND_RESIZE
},
2336 { "and-change-password", required_argument
, NULL
, ARG_AND_CHANGE_PASSWORD
},
2337 { "drop-caches", required_argument
, NULL
, ARG_DROP_CACHES
},
2349 c
= getopt_long(argc
, argv
, "hH:M:I:c:d:u:k:s:e:G:jPE", options
, NULL
);
2356 return help(0, NULL
, NULL
);
2362 arg_pager_flags
|= PAGER_DISABLE
;
2369 case ARG_NO_ASK_PASSWORD
:
2370 arg_ask_password
= false;
2374 arg_transport
= BUS_TRANSPORT_REMOTE
;
2379 arg_transport
= BUS_TRANSPORT_MACHINE
;
2384 arg_identity
= optarg
;
2388 if (isempty(optarg
)) {
2389 r
= drop_from_identity("realName");
2396 if (!valid_gecos(optarg
))
2397 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Real name '%s' not a valid GECOS field.", optarg
);
2399 r
= json_variant_set_field_string(&arg_identity_extra
, "realName", optarg
);
2401 return log_error_errno(r
, "Failed to set realName field: %m");
2406 _cleanup_free_
char *hd
= NULL
;
2408 if (isempty(optarg
)) {
2409 r
= drop_from_identity("homeDirectory");
2416 r
= parse_path_argument(optarg
, false, &hd
);
2420 if (!valid_home(hd
))
2421 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Home directory '%s' not valid.", hd
);
2423 r
= json_variant_set_field_string(&arg_identity_extra
, "homeDirectory", hd
);
2425 return log_error_errno(r
, "Failed to set homeDirectory field: %m");
2431 if (isempty(optarg
)) {
2432 r
= drop_from_identity("realm");
2439 r
= dns_name_is_valid(optarg
);
2441 return log_error_errno(r
, "Failed to determine whether realm '%s' is a valid DNS domain: %m", optarg
);
2443 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Realm '%s' is not a valid DNS domain: %m", optarg
);
2445 r
= json_variant_set_field_string(&arg_identity_extra
, "realm", optarg
);
2447 return log_error_errno(r
, "Failed to set realm field: %m");
2450 case ARG_EMAIL_ADDRESS
:
2453 case ARG_CIFS_USER_NAME
:
2454 case ARG_CIFS_DOMAIN
:
2455 case ARG_CIFS_EXTRA_MOUNT_OPTIONS
: {
2458 c
== ARG_EMAIL_ADDRESS
? "emailAddress" :
2459 c
== ARG_LOCATION
? "location" :
2460 c
== ARG_ICON_NAME
? "iconName" :
2461 c
== ARG_CIFS_USER_NAME
? "cifsUserName" :
2462 c
== ARG_CIFS_DOMAIN
? "cifsDomain" :
2463 c
== ARG_CIFS_EXTRA_MOUNT_OPTIONS
? "cifsExtraMountOptions" :
2468 if (isempty(optarg
)) {
2469 r
= drop_from_identity(field
);
2476 r
= json_variant_set_field_string(&arg_identity_extra
, field
, optarg
);
2478 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2483 case ARG_CIFS_SERVICE
:
2484 if (isempty(optarg
)) {
2485 r
= drop_from_identity("cifsService");
2492 r
= parse_cifs_service(optarg
, NULL
, NULL
, NULL
);
2494 return log_error_errno(r
, "Failed to validate CIFS service name: %s", optarg
);
2496 r
= json_variant_set_field_string(&arg_identity_extra
, "cifsService", optarg
);
2498 return log_error_errno(r
, "Failed to set cifsService field: %m");
2502 case ARG_PASSWORD_HINT
:
2503 if (isempty(optarg
)) {
2504 r
= drop_from_identity("passwordHint");
2511 r
= json_variant_set_field_string(&arg_identity_extra_privileged
, "passwordHint", optarg
);
2513 return log_error_errno(r
, "Failed to set passwordHint field: %m");
2515 string_erase(optarg
);
2521 if (isempty(optarg
)) {
2522 r
= drop_from_identity("niceLevel");
2528 r
= parse_nice(optarg
, &nc
);
2530 return log_error_errno(r
, "Failed to parse nice level: %s", optarg
);
2532 r
= json_variant_set_field_integer(&arg_identity_extra
, "niceLevel", nc
);
2534 return log_error_errno(r
, "Failed to set niceLevel field: %m");
2540 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
, *jcur
= NULL
, *jmax
= NULL
;
2541 _cleanup_free_
char *field
= NULL
, *t
= NULL
;
2546 if (isempty(optarg
)) {
2547 /* Remove all resource limits */
2549 r
= drop_from_identity("resourceLimits");
2553 arg_identity_filter_rlimits
= strv_free(arg_identity_filter_rlimits
);
2554 arg_identity_extra_rlimits
= json_variant_unref(arg_identity_extra_rlimits
);
2558 eq
= strchr(optarg
, '=');
2560 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Can't parse resource limit assignment: %s", optarg
);
2562 field
= strndup(optarg
, eq
- optarg
);
2566 l
= rlimit_from_string_harder(field
);
2568 return log_error_errno(l
, "Unknown resource limit type: %s", field
);
2570 if (isempty(eq
+ 1)) {
2571 /* Remove only the specific rlimit */
2573 r
= strv_extend(&arg_identity_filter_rlimits
, rlimit_to_string(l
));
2577 r
= json_variant_filter(&arg_identity_extra_rlimits
, STRV_MAKE(field
));
2579 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
2584 r
= rlimit_parse(l
, eq
+ 1, &rl
);
2586 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to parse resource limit value: %s", eq
+ 1);
2588 r
= rl
.rlim_cur
== RLIM_INFINITY
? json_variant_new_null(&jcur
) : json_variant_new_unsigned(&jcur
, rl
.rlim_cur
);
2590 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to allocate current integer: %m");
2592 r
= rl
.rlim_max
== RLIM_INFINITY
? json_variant_new_null(&jmax
) : json_variant_new_unsigned(&jmax
, rl
.rlim_max
);
2594 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to allocate maximum integer: %m");
2598 JSON_BUILD_PAIR("cur", JSON_BUILD_VARIANT(jcur
)),
2599 JSON_BUILD_PAIR("max", JSON_BUILD_VARIANT(jmax
))));
2601 return log_error_errno(r
, "Failed to build resource limit: %m");
2603 t
= strjoin("RLIMIT_", rlimit_to_string(l
));
2607 r
= json_variant_set_field(&arg_identity_extra_rlimits
, t
, v
);
2609 return log_error_errno(r
, "Failed to set %s field: %m", rlimit_to_string(l
));
2617 if (isempty(optarg
)) {
2618 r
= drop_from_identity("uid");
2625 r
= parse_uid(optarg
, &uid
);
2627 return log_error_errno(r
, "Failed to parse UID '%s'.", optarg
);
2629 if (uid_is_system(uid
))
2630 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "UID " UID_FMT
" is in system range, refusing.", uid
);
2631 if (uid_is_dynamic(uid
))
2632 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "UID " UID_FMT
" is in dynamic range, refusing.", uid
);
2633 if (uid
== UID_NOBODY
)
2634 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "UID " UID_FMT
" is nobody UID, refusing.", uid
);
2636 r
= json_variant_set_field_unsigned(&arg_identity_extra
, "uid", uid
);
2638 return log_error_errno(r
, "Failed to set realm field: %m");
2644 case ARG_IMAGE_PATH
: {
2645 const char *field
= c
== 'k' ? "skeletonDirectory" : "imagePath";
2646 _cleanup_free_
char *v
= NULL
;
2648 if (isempty(optarg
)) {
2649 r
= drop_from_identity(field
);
2656 r
= parse_path_argument(optarg
, false, &v
);
2660 r
= json_variant_set_field_string(&arg_identity_extra_this_machine
, field
, v
);
2662 return log_error_errno(r
, "Failed to set %s field: %m", v
);
2668 if (isempty(optarg
)) {
2669 r
= drop_from_identity("shell");
2676 if (!valid_shell(optarg
))
2677 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Shell '%s' not valid.", optarg
);
2679 r
= json_variant_set_field_string(&arg_identity_extra
, "shell", optarg
);
2681 return log_error_errno(r
, "Failed to set shell field: %m");
2686 _cleanup_free_
char **l
= NULL
;
2687 _cleanup_(json_variant_unrefp
) JsonVariant
*ne
= NULL
;
2690 if (isempty(optarg
)) {
2691 r
= drop_from_identity("environment");
2698 e
= json_variant_by_key(arg_identity_extra
, "environment");
2700 r
= json_variant_strv(e
, &l
);
2702 return log_error_errno(r
, "Failed to parse JSON environment field: %m");
2705 r
= strv_env_replace_strdup_passthrough(&l
, optarg
);
2707 return log_error_errno(r
, "Cannot assign environment variable %s: %m", optarg
);
2711 r
= json_variant_new_array_strv(&ne
, l
);
2713 return log_error_errno(r
, "Failed to allocate environment list JSON: %m");
2715 r
= json_variant_set_field(&arg_identity_extra
, "environment", ne
);
2717 return log_error_errno(r
, "Failed to set environment list: %m");
2724 if (isempty(optarg
)) {
2725 r
= drop_from_identity("timeZone");
2732 if (!timezone_is_valid(optarg
, LOG_DEBUG
))
2733 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Timezone '%s' is not valid.", optarg
);
2735 r
= json_variant_set_field_string(&arg_identity_extra
, "timeZone", optarg
);
2737 return log_error_errno(r
, "Failed to set timezone field: %m");
2742 if (isempty(optarg
)) {
2743 r
= drop_from_identity("language");
2750 if (!locale_is_valid(optarg
))
2751 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Locale '%s' is not valid.", optarg
);
2753 if (locale_is_installed(optarg
) <= 0)
2754 log_warning("Locale '%s' is not installed, accepting anyway.", optarg
);
2756 r
= json_variant_set_field_string(&arg_identity_extra
, "preferredLanguage", optarg
);
2758 return log_error_errno(r
, "Failed to set preferredLanguage field: %m");
2766 case ARG_KILL_PROCESSES
:
2767 case ARG_ENFORCE_PASSWORD_POLICY
:
2768 case ARG_AUTO_LOGIN
:
2769 case ARG_PASSWORD_CHANGE_NOW
: {
2771 c
== ARG_LOCKED
? "locked" :
2772 c
== ARG_NOSUID
? "mountNoSuid" :
2773 c
== ARG_NODEV
? "mountNoDevices" :
2774 c
== ARG_NOEXEC
? "mountNoExecute" :
2775 c
== ARG_KILL_PROCESSES
? "killProcesses" :
2776 c
== ARG_ENFORCE_PASSWORD_POLICY
? "enforcePasswordPolicy" :
2777 c
== ARG_AUTO_LOGIN
? "autoLogin" :
2778 c
== ARG_PASSWORD_CHANGE_NOW
? "passwordChangeNow" :
2783 if (isempty(optarg
)) {
2784 r
= drop_from_identity(field
);
2791 r
= parse_boolean(optarg
);
2793 return log_error_errno(r
, "Failed to parse %s boolean: %m", field
);
2795 r
= json_variant_set_field_boolean(&arg_identity_extra
, field
, r
> 0);
2797 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2803 r
= json_variant_set_field_boolean(&arg_identity_extra
, "enforcePasswordPolicy", false);
2805 return log_error_errno(r
, "Failed to set enforcePasswordPolicy field: %m");
2810 if (isempty(optarg
)) {
2811 r
= drop_from_identity("diskSize");
2815 r
= drop_from_identity("diskSizeRelative");
2819 arg_disk_size
= arg_disk_size_relative
= UINT64_MAX
;
2823 r
= parse_permyriad(optarg
);
2825 r
= parse_size(optarg
, 1024, &arg_disk_size
);
2827 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Disk size '%s' not valid.", optarg
);
2829 r
= drop_from_identity("diskSizeRelative");
2833 r
= json_variant_set_field_unsigned(&arg_identity_extra_this_machine
, "diskSize", arg_disk_size
);
2835 return log_error_errno(r
, "Failed to set diskSize field: %m");
2837 arg_disk_size_relative
= UINT64_MAX
;
2839 /* Normalize to UINT32_MAX == 100% */
2840 arg_disk_size_relative
= UINT32_SCALE_FROM_PERMYRIAD(r
);
2842 r
= drop_from_identity("diskSize");
2846 r
= json_variant_set_field_unsigned(&arg_identity_extra_this_machine
, "diskSizeRelative", arg_disk_size_relative
);
2848 return log_error_errno(r
, "Failed to set diskSizeRelative field: %m");
2850 arg_disk_size
= UINT64_MAX
;
2855 case ARG_ACCESS_MODE
: {
2858 if (isempty(optarg
)) {
2859 r
= drop_from_identity("accessMode");
2866 r
= parse_mode(optarg
, &mode
);
2868 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Access mode '%s' not valid.", optarg
);
2870 r
= json_variant_set_field_unsigned(&arg_identity_extra
, "accessMode", mode
);
2872 return log_error_errno(r
, "Failed to set access mode field: %m");
2877 case ARG_LUKS_DISCARD
:
2878 if (isempty(optarg
)) {
2879 r
= drop_from_identity("luksDiscard");
2886 r
= parse_boolean(optarg
);
2888 return log_error_errno(r
, "Failed to parse --luks-discard= parameter: %s", optarg
);
2890 r
= json_variant_set_field_boolean(&arg_identity_extra
, "luksDiscard", r
);
2892 return log_error_errno(r
, "Failed to set discard field: %m");
2896 case ARG_LUKS_OFFLINE_DISCARD
:
2897 if (isempty(optarg
)) {
2898 r
= drop_from_identity("luksOfflineDiscard");
2905 r
= parse_boolean(optarg
);
2907 return log_error_errno(r
, "Failed to parse --luks-offline-discard= parameter: %s", optarg
);
2909 r
= json_variant_set_field_boolean(&arg_identity_extra
, "luksOfflineDiscard", r
);
2911 return log_error_errno(r
, "Failed to set offline discard field: %m");
2915 case ARG_LUKS_VOLUME_KEY_SIZE
:
2916 case ARG_LUKS_PBKDF_PARALLEL_THREADS
:
2917 case ARG_RATE_LIMIT_BURST
: {
2919 c
== ARG_LUKS_VOLUME_KEY_SIZE
? "luksVolumeKeySize" :
2920 c
== ARG_LUKS_PBKDF_PARALLEL_THREADS
? "luksPbkdfParallelThreads" :
2921 c
== ARG_RATE_LIMIT_BURST
? "rateLimitBurst" : NULL
;
2926 if (isempty(optarg
)) {
2927 r
= drop_from_identity(field
);
2932 r
= safe_atou(optarg
, &n
);
2934 return log_error_errno(r
, "Failed to parse %s parameter: %s", field
, optarg
);
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
);
2946 if (isempty(optarg
)) {
2947 r
= drop_from_identity("umask");
2954 r
= parse_mode(optarg
, &m
);
2956 return log_error_errno(r
, "Failed to parse umask: %m");
2958 r
= json_variant_set_field_integer(&arg_identity_extra
, "umask", m
);
2960 return log_error_errno(r
, "Failed to set umask field: %m");
2965 case ARG_SSH_AUTHORIZED_KEYS
: {
2966 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
2967 _cleanup_(strv_freep
) char **l
= NULL
, **add
= NULL
;
2969 if (isempty(optarg
)) {
2970 r
= drop_from_identity("sshAuthorizedKeys");
2977 if (optarg
[0] == '@') {
2978 _cleanup_fclose_
FILE *f
= NULL
;
2980 /* If prefixed with '@' read from a file */
2982 f
= fopen(optarg
+1, "re");
2984 return log_error_errno(errno
, "Failed to open '%s': %m", optarg
+1);
2987 _cleanup_free_
char *line
= NULL
;
2989 r
= read_line(f
, LONG_LINE_MAX
, &line
);
2991 return log_error_errno(r
, "Failed to read from '%s': %m", optarg
+1);
3001 r
= strv_consume(&add
, TAKE_PTR(line
));
3006 /* Otherwise, assume it's a literal key. Let's do some superficial checks
3007 * before accept it though. */
3009 if (string_has_cc(optarg
, NULL
))
3010 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Authorized key contains control characters, refusing.");
3011 if (optarg
[0] == '#')
3012 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Specified key is a comment?");
3014 add
= strv_new(optarg
);
3019 v
= json_variant_ref(json_variant_by_key(arg_identity_extra_privileged
, "sshAuthorizedKeys"));
3021 r
= json_variant_strv(v
, &l
);
3023 return log_error_errno(r
, "Failed to parse SSH authorized keys list: %m");
3026 r
= strv_extend_strv(&l
, add
, true);
3030 v
= json_variant_unref(v
);
3032 r
= json_variant_new_array_strv(&v
, l
);
3036 r
= json_variant_set_field(&arg_identity_extra_privileged
, "sshAuthorizedKeys", v
);
3038 return log_error_errno(r
, "Failed to set authorized keys: %m");
3043 case ARG_NOT_BEFORE
:
3049 field
= c
== ARG_NOT_BEFORE
? "notBeforeUSec" :
3050 IN_SET(c
, ARG_NOT_AFTER
, 'e') ? "notAfterUSec" : NULL
;
3054 if (isempty(optarg
)) {
3055 r
= drop_from_identity(field
);
3062 /* Note the minor discrepancy regarding -e parsing here: we support that for compat
3063 * reasons, and in the original useradd(8) implementation it accepts dates in the
3064 * format YYYY-MM-DD. Coincidentally, we accept dates formatted like that too, but
3065 * with greater precision. */
3066 r
= parse_timestamp(optarg
, &n
);
3068 return log_error_errno(r
, "Failed to parse %s parameter: %m", field
);
3070 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, n
);
3072 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3076 case ARG_PASSWORD_CHANGE_MIN
:
3077 case ARG_PASSWORD_CHANGE_MAX
:
3078 case ARG_PASSWORD_CHANGE_WARN
:
3079 case ARG_PASSWORD_CHANGE_INACTIVE
: {
3083 field
= c
== ARG_PASSWORD_CHANGE_MIN
? "passwordChangeMinUSec" :
3084 c
== ARG_PASSWORD_CHANGE_MAX
? "passwordChangeMaxUSec" :
3085 c
== ARG_PASSWORD_CHANGE_WARN
? "passwordChangeWarnUSec" :
3086 c
== ARG_PASSWORD_CHANGE_INACTIVE
? "passwordChangeInactiveUSec" :
3091 if (isempty(optarg
)) {
3092 r
= drop_from_identity(field
);
3099 r
= parse_sec(optarg
, &n
);
3101 return log_error_errno(r
, "Failed to parse %s parameter: %m", field
);
3103 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, n
);
3105 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3111 case ARG_LUKS_CIPHER
:
3112 case ARG_LUKS_CIPHER_MODE
:
3113 case ARG_LUKS_PBKDF_TYPE
:
3114 case ARG_LUKS_PBKDF_HASH_ALGORITHM
: {
3117 c
== ARG_STORAGE
? "storage" :
3118 c
== ARG_FS_TYPE
? "fileSystemType" :
3119 c
== ARG_LUKS_CIPHER
? "luksCipher" :
3120 c
== ARG_LUKS_CIPHER_MODE
? "luksCipherMode" :
3121 c
== ARG_LUKS_PBKDF_TYPE
? "luksPbkdfType" :
3122 c
== ARG_LUKS_PBKDF_HASH_ALGORITHM
? "luksPbkdfHashAlgorithm" : NULL
;
3126 if (isempty(optarg
)) {
3127 r
= drop_from_identity(field
);
3134 if (!string_is_safe(optarg
))
3135 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Parameter for %s field not valid: %s", field
, optarg
);
3137 r
= json_variant_set_field_string(
3138 IN_SET(c
, ARG_STORAGE
, ARG_FS_TYPE
) ?
3139 &arg_identity_extra_this_machine
:
3140 &arg_identity_extra
, field
, optarg
);
3142 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3147 case ARG_LUKS_PBKDF_TIME_COST
:
3148 case ARG_RATE_LIMIT_INTERVAL
:
3149 case ARG_STOP_DELAY
: {
3151 c
== ARG_LUKS_PBKDF_TIME_COST
? "luksPbkdfTimeCostUSec" :
3152 c
== ARG_RATE_LIMIT_INTERVAL
? "rateLimitIntervalUSec" :
3153 c
== ARG_STOP_DELAY
? "stopDelayUSec" :
3159 if (isempty(optarg
)) {
3160 r
= drop_from_identity(field
);
3167 r
= parse_sec(optarg
, &t
);
3169 return log_error_errno(r
, "Failed to parse %s field: %s", field
, optarg
);
3171 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, t
);
3173 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3179 const char *p
= optarg
;
3182 r
= drop_from_identity("memberOf");
3190 _cleanup_(json_variant_unrefp
) JsonVariant
*mo
= NULL
;
3191 _cleanup_strv_free_
char **list
= NULL
;
3192 _cleanup_free_
char *word
= NULL
;
3194 r
= extract_first_word(&p
, &word
, ",", 0);
3196 return log_error_errno(r
, "Failed to parse group list: %m");
3200 if (!valid_user_group_name(word
, 0))
3201 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Invalid group name %s.", word
);
3203 mo
= json_variant_ref(json_variant_by_key(arg_identity_extra
, "memberOf"));
3205 r
= json_variant_strv(mo
, &list
);
3207 return log_error_errno(r
, "Failed to parse group list: %m");
3209 r
= strv_extend(&list
, word
);
3216 mo
= json_variant_unref(mo
);
3217 r
= json_variant_new_array_strv(&mo
, list
);
3219 return log_error_errno(r
, "Failed to create group list JSON: %m");
3221 r
= json_variant_set_field(&arg_identity_extra
, "memberOf", mo
);
3223 return log_error_errno(r
, "Failed to update group list: %m");
3229 case ARG_TASKS_MAX
: {
3232 if (isempty(optarg
)) {
3233 r
= drop_from_identity("tasksMax");
3239 r
= safe_atou64(optarg
, &u
);
3241 return log_error_errno(r
, "Failed to parse --tasks-max= parameter: %s", optarg
);
3243 r
= json_variant_set_field_unsigned(&arg_identity_extra
, "tasksMax", u
);
3245 return log_error_errno(r
, "Failed to set tasksMax field: %m");
3250 case ARG_MEMORY_MAX
:
3251 case ARG_MEMORY_HIGH
:
3252 case ARG_LUKS_PBKDF_MEMORY_COST
: {
3254 c
== ARG_MEMORY_MAX
? "memoryMax" :
3255 c
== ARG_MEMORY_HIGH
? "memoryHigh" :
3256 c
== ARG_LUKS_PBKDF_MEMORY_COST
? "luksPbkdfMemoryCost" : NULL
;
3262 if (isempty(optarg
)) {
3263 r
= drop_from_identity(field
);
3269 r
= parse_size(optarg
, 1024, &u
);
3271 return log_error_errno(r
, "Failed to parse %s parameter: %s", field
, optarg
);
3273 r
= json_variant_set_field_unsigned(&arg_identity_extra_this_machine
, field
, u
);
3275 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3280 case ARG_CPU_WEIGHT
:
3281 case ARG_IO_WEIGHT
: {
3282 const char *field
= c
== ARG_CPU_WEIGHT
? "cpuWeight" :
3283 c
== ARG_IO_WEIGHT
? "ioWeight" : NULL
;
3288 if (isempty(optarg
)) {
3289 r
= drop_from_identity(field
);
3295 r
= safe_atou64(optarg
, &u
);
3297 return log_error_errno(r
, "Failed to parse --cpu-weight=/--io-weight= parameter: %s", optarg
);
3299 if (!CGROUP_WEIGHT_IS_OK(u
))
3300 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Weight %" PRIu64
" is out of valid weight range.", u
);
3302 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, u
);
3304 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3309 case ARG_PKCS11_TOKEN_URI
: {
3312 if (streq(optarg
, "list"))
3313 return pkcs11_list_tokens();
3315 /* If --pkcs11-token-uri= is specified we always drop everything old */
3316 FOREACH_STRING(p
, "pkcs11TokenUri", "pkcs11EncryptedKey") {
3317 r
= drop_from_identity(p
);
3322 if (isempty(optarg
)) {
3323 arg_pkcs11_token_uri
= strv_free(arg_pkcs11_token_uri
);
3327 if (streq(optarg
, "auto")) {
3328 _cleanup_free_
char *found
= NULL
;
3330 r
= pkcs11_find_token_auto(&found
);
3333 r
= strv_consume(&arg_pkcs11_token_uri
, TAKE_PTR(found
));
3335 if (!pkcs11_uri_valid(optarg
))
3336 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Not a valid PKCS#11 URI: %s", optarg
);
3338 r
= strv_extend(&arg_pkcs11_token_uri
, optarg
);
3343 strv_uniq(arg_pkcs11_token_uri
);
3347 case ARG_FIDO2_DEVICE
: {
3350 if (streq(optarg
, "list"))
3351 return fido2_list_devices();
3353 FOREACH_STRING(p
, "fido2HmacCredential", "fido2HmacSalt") {
3354 r
= drop_from_identity(p
);
3359 if (isempty(optarg
)) {
3360 arg_fido2_device
= strv_free(arg_fido2_device
);
3364 if (streq(optarg
, "auto")) {
3365 _cleanup_free_
char *found
= NULL
;
3367 r
= fido2_find_device_auto(&found
);
3371 r
= strv_consume(&arg_fido2_device
, TAKE_PTR(found
));
3373 r
= strv_extend(&arg_fido2_device
, optarg
);
3377 strv_uniq(arg_fido2_device
);
3381 case ARG_FIDO2_WITH_PIN
: {
3384 r
= parse_boolean_argument("--fido2-with-client-pin=", optarg
, &lock_with_pin
);
3388 SET_FLAG(arg_fido2_lock_with
, FIDO2ENROLL_PIN
, lock_with_pin
);
3392 case ARG_FIDO2_WITH_UP
: {
3395 r
= parse_boolean_argument("--fido2-with-user-presence=", optarg
, &lock_with_up
);
3399 SET_FLAG(arg_fido2_lock_with
, FIDO2ENROLL_UP
, lock_with_up
);
3403 case ARG_FIDO2_WITH_UV
: {
3406 r
= parse_boolean_argument("--fido2-with-user-verification=", optarg
, &lock_with_uv
);
3410 SET_FLAG(arg_fido2_lock_with
, FIDO2ENROLL_UV
, lock_with_uv
);
3414 case ARG_RECOVERY_KEY
: {
3417 r
= parse_boolean(optarg
);
3419 return log_error_errno(r
, "Failed to parse --recovery-key= argument: %s", optarg
);
3421 arg_recovery_key
= r
;
3423 FOREACH_STRING(p
, "recoveryKey", "recoveryKeyType") {
3424 r
= drop_from_identity(p
);
3433 arg_json_format_flags
= JSON_FORMAT_PRETTY_AUTO
|JSON_FORMAT_COLOR_AUTO
;
3437 r
= parse_json_argument(optarg
, &arg_json_format_flags
);
3444 if (arg_export_format
== EXPORT_FORMAT_FULL
)
3445 arg_export_format
= EXPORT_FORMAT_STRIPPED
;
3446 else if (arg_export_format
== EXPORT_FORMAT_STRIPPED
)
3447 arg_export_format
= EXPORT_FORMAT_MINIMAL
;
3449 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Specifying -E more than twice is not supported.");
3451 arg_json_format_flags
&= ~JSON_FORMAT_OFF
;
3452 if (arg_json_format_flags
== 0)
3453 arg_json_format_flags
= JSON_FORMAT_PRETTY_AUTO
|JSON_FORMAT_COLOR_AUTO
;
3456 case ARG_EXPORT_FORMAT
:
3457 if (streq(optarg
, "full"))
3458 arg_export_format
= EXPORT_FORMAT_FULL
;
3459 else if (streq(optarg
, "stripped"))
3460 arg_export_format
= EXPORT_FORMAT_STRIPPED
;
3461 else if (streq(optarg
, "minimal"))
3462 arg_export_format
= EXPORT_FORMAT_MINIMAL
;
3463 else if (streq(optarg
, "help")) {
3472 case ARG_AND_RESIZE
:
3473 arg_and_resize
= true;
3476 case ARG_AND_CHANGE_PASSWORD
:
3477 arg_and_change_password
= true;
3480 case ARG_DROP_CACHES
: {
3483 if (isempty(optarg
)) {
3484 r
= drop_from_identity("dropCaches");
3489 r
= parse_boolean_argument("--drop-caches=", optarg
, &drop_caches
);
3493 r
= json_variant_set_field_boolean(&arg_identity_extra
, "dropCaches", r
);
3495 return log_error_errno(r
, "Failed to set drop caches field: %m");
3504 assert_not_reached();
3508 if (!strv_isempty(arg_pkcs11_token_uri
) || !strv_isempty(arg_fido2_device
))
3509 arg_and_change_password
= true;
3511 if (arg_disk_size
!= UINT64_MAX
|| arg_disk_size_relative
!= UINT64_MAX
)
3512 arg_and_resize
= true;
3517 static int redirect_bus_mgr(void) {
3520 /* Talk to a different service if that's requested. (The same env var is also understood by homed, so
3521 * that it is relatively easily possible to invoke a second instance of homed for debug purposes and
3522 * have homectl talk to it, without colliding with the host version. This is handy when operating
3523 * from a homed-managed account.) */
3525 suffix
= getenv("SYSTEMD_HOME_DEBUG_SUFFIX");
3527 static BusLocator locator
= {
3528 .path
= "/org/freedesktop/home1",
3529 .interface
= "org.freedesktop.home1.Manager",
3532 /* Yes, we leak this memory, but there's little point to collect this, given that we only do
3533 * this in a debug environment, do it only once, and the string shall live for out entire
3534 * process runtime. */
3536 locator
.destination
= strjoin("org.freedesktop.home1.", suffix
);
3537 if (!locator
.destination
)
3542 bus_mgr
= bus_home_mgr
;
3547 static int run(int argc
, char *argv
[]) {
3548 static const Verb verbs
[] = {
3549 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
3550 { "list", VERB_ANY
, 1, VERB_DEFAULT
, list_homes
},
3551 { "activate", 2, VERB_ANY
, 0, activate_home
},
3552 { "deactivate", 2, VERB_ANY
, 0, deactivate_home
},
3553 { "inspect", VERB_ANY
, VERB_ANY
, 0, inspect_home
},
3554 { "authenticate", VERB_ANY
, VERB_ANY
, 0, authenticate_home
},
3555 { "create", VERB_ANY
, 2, 0, create_home
},
3556 { "remove", 2, VERB_ANY
, 0, remove_home
},
3557 { "update", VERB_ANY
, 2, 0, update_home
},
3558 { "passwd", VERB_ANY
, 2, 0, passwd_home
},
3559 { "resize", 2, 3, 0, resize_home
},
3560 { "lock", 2, VERB_ANY
, 0, lock_home
},
3561 { "unlock", 2, VERB_ANY
, 0, unlock_home
},
3562 { "with", 2, VERB_ANY
, 0, with_home
},
3563 { "lock-all", VERB_ANY
, 1, 0, lock_all_homes
},
3564 { "deactivate-all", VERB_ANY
, 1, 0, deactivate_all_homes
},
3572 r
= redirect_bus_mgr();
3576 r
= parse_argv(argc
, argv
);
3580 return dispatch_verb(argc
, argv
, verbs
, NULL
);
3583 DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run
);