1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
7 #include "ask-password-api.h"
9 #include "bus-common-errors.h"
10 #include "bus-error.h"
11 #include "bus-locator.h"
13 #include "capability-util.h"
14 #include "cgroup-util.h"
15 #include "dns-domain.h"
19 #include "format-table.h"
21 #include "glyph-util.h"
22 #include "home-util.h"
23 #include "homectl-fido2.h"
24 #include "homectl-pkcs11.h"
25 #include "homectl-recovery-key.h"
26 #include "libfido2-util.h"
27 #include "locale-util.h"
28 #include "main-func.h"
29 #include "memory-util.h"
31 #include "parse-argument.h"
32 #include "parse-util.h"
33 #include "path-util.h"
34 #include "percent-util.h"
35 #include "pkcs11-util.h"
36 #include "pretty-print.h"
37 #include "process-util.h"
38 #include "pwquality-util.h"
39 #include "rlimit-util.h"
40 #include "spawn-polkit-agent.h"
41 #include "terminal-util.h"
42 #include "uid-alloc-range.h"
43 #include "user-record.h"
44 #include "user-record-pwquality.h"
45 #include "user-record-show.h"
46 #include "user-record-util.h"
47 #include "user-util.h"
50 static PagerFlags arg_pager_flags
= 0;
51 static bool arg_legend
= true;
52 static bool arg_ask_password
= true;
53 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
54 static const char *arg_host
= NULL
;
55 static const char *arg_identity
= NULL
;
56 static JsonVariant
*arg_identity_extra
= NULL
;
57 static JsonVariant
*arg_identity_extra_privileged
= NULL
;
58 static JsonVariant
*arg_identity_extra_this_machine
= NULL
;
59 static JsonVariant
*arg_identity_extra_rlimits
= NULL
;
60 static char **arg_identity_filter
= NULL
; /* this one is also applied to 'privileged' and 'thisMachine' subobjects */
61 static char **arg_identity_filter_rlimits
= NULL
;
62 static uint64_t arg_disk_size
= UINT64_MAX
;
63 static uint64_t arg_disk_size_relative
= UINT64_MAX
;
64 static char **arg_pkcs11_token_uri
= NULL
;
65 static char **arg_fido2_device
= NULL
;
66 static Fido2EnrollFlags arg_fido2_lock_with
= FIDO2ENROLL_PIN
| FIDO2ENROLL_UP
;
68 static int arg_fido2_cred_alg
= COSE_ES256
;
70 static int arg_fido2_cred_alg
= 0;
72 static bool arg_recovery_key
= false;
73 static JsonFormatFlags arg_json_format_flags
= JSON_FORMAT_OFF
;
74 static bool arg_and_resize
= false;
75 static bool arg_and_change_password
= false;
77 EXPORT_FORMAT_FULL
, /* export the full record */
78 EXPORT_FORMAT_STRIPPED
, /* strip "state" + "binding", but leave signature in place */
79 EXPORT_FORMAT_MINIMAL
, /* also strip signature */
80 } arg_export_format
= EXPORT_FORMAT_FULL
;
81 static uint64_t arg_capability_bounding_set
= UINT64_MAX
;
82 static uint64_t arg_capability_ambient_set
= UINT64_MAX
;
84 STATIC_DESTRUCTOR_REGISTER(arg_identity_extra
, json_variant_unrefp
);
85 STATIC_DESTRUCTOR_REGISTER(arg_identity_extra_this_machine
, json_variant_unrefp
);
86 STATIC_DESTRUCTOR_REGISTER(arg_identity_extra_privileged
, json_variant_unrefp
);
87 STATIC_DESTRUCTOR_REGISTER(arg_identity_extra_rlimits
, json_variant_unrefp
);
88 STATIC_DESTRUCTOR_REGISTER(arg_identity_filter
, strv_freep
);
89 STATIC_DESTRUCTOR_REGISTER(arg_identity_filter_rlimits
, strv_freep
);
90 STATIC_DESTRUCTOR_REGISTER(arg_pkcs11_token_uri
, strv_freep
);
91 STATIC_DESTRUCTOR_REGISTER(arg_fido2_device
, strv_freep
);
93 static const BusLocator
*bus_mgr
;
95 static bool identity_properties_specified(void) {
98 !json_variant_is_blank_object(arg_identity_extra
) ||
99 !json_variant_is_blank_object(arg_identity_extra_privileged
) ||
100 !json_variant_is_blank_object(arg_identity_extra_this_machine
) ||
101 !json_variant_is_blank_object(arg_identity_extra_rlimits
) ||
102 !strv_isempty(arg_identity_filter
) ||
103 !strv_isempty(arg_identity_filter_rlimits
) ||
104 !strv_isempty(arg_pkcs11_token_uri
) ||
105 !strv_isempty(arg_fido2_device
);
108 static int acquire_bus(sd_bus
**bus
) {
116 r
= bus_connect_transport(arg_transport
, arg_host
, RUNTIME_SCOPE_SYSTEM
, bus
);
118 return bus_log_connect_error(r
, arg_transport
);
120 (void) sd_bus_set_allow_interactive_authorization(*bus
, arg_ask_password
);
125 static int list_homes(int argc
, char *argv
[], void *userdata
) {
126 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
127 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
128 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
129 _cleanup_(table_unrefp
) Table
*table
= NULL
;
132 r
= acquire_bus(&bus
);
136 r
= bus_call_method(bus
, bus_mgr
, "ListHomes", &error
, &reply
, NULL
);
138 return log_error_errno(r
, "Failed to list homes: %s", bus_error_message(&error
, r
));
140 table
= table_new("name", "uid", "gid", "state", "realname", "home", "shell");
144 r
= sd_bus_message_enter_container(reply
, 'a', "(susussso)");
146 return bus_log_parse_error(r
);
149 const char *name
, *state
, *realname
, *home
, *shell
, *color
;
153 r
= sd_bus_message_read(reply
, "(susussso)", &name
, &uid
, &state
, &gid
, &realname
, &home
, &shell
, NULL
);
155 return bus_log_parse_error(r
);
159 r
= table_add_many(table
,
164 return table_log_add_error(r
);
167 r
= table_add_cell(table
, &cell
, TABLE_STRING
, state
);
169 return table_log_add_error(r
);
171 color
= user_record_state_color(state
);
173 (void) table_set_color(table
, cell
, color
);
175 r
= table_add_many(table
,
176 TABLE_STRING
, strna(empty_to_null(realname
)),
178 TABLE_STRING
, strna(empty_to_null(shell
)));
180 return table_log_add_error(r
);
183 r
= sd_bus_message_exit_container(reply
);
185 return bus_log_parse_error(r
);
187 if (table_get_rows(table
) > 1 || !FLAGS_SET(arg_json_format_flags
, JSON_FORMAT_OFF
)) {
188 r
= table_set_sort(table
, (size_t) 0);
190 return table_log_sort_error(r
);
192 r
= table_print_with_pager(table
, arg_json_format_flags
, arg_pager_flags
, arg_legend
);
197 if (arg_legend
&& (arg_json_format_flags
& JSON_FORMAT_OFF
)) {
198 if (table_get_rows(table
) > 1)
199 printf("\n%zu home areas listed.\n", table_get_rows(table
) - 1);
201 printf("No home areas.\n");
207 static int acquire_existing_password(
208 const char *user_name
,
210 bool emphasize_current
,
211 AskPasswordFlags flags
) {
213 _cleanup_(strv_free_erasep
) char **password
= NULL
;
214 _cleanup_(erase_and_freep
) char *envpw
= NULL
;
215 _cleanup_free_
char *question
= NULL
;
221 r
= getenv_steal_erase("PASSWORD", &envpw
);
223 return log_error_errno(r
, "Failed to acquire password from environment: %m");
225 /* People really shouldn't use environment variables for passing passwords. We support this
226 * only for testing purposes, and do not document the behaviour, so that people won't
227 * actually use this outside of testing. */
229 r
= user_record_set_password(hr
, STRV_MAKE(envpw
), true);
231 return log_error_errno(r
, "Failed to store password: %m");
236 /* If this is not our own user, then don't use the password cache */
237 if (is_this_me(user_name
) <= 0)
238 SET_FLAG(flags
, ASK_PASSWORD_ACCEPT_CACHED
|ASK_PASSWORD_PUSH_CACHE
, false);
240 if (asprintf(&question
, emphasize_current
?
241 "Please enter current password for user %s:" :
242 "Please enter password for user %s:",
246 r
= ask_password_auto(question
,
247 /* icon= */ "user-home",
249 /* key_name= */ "home-password",
250 /* credential_name= */ "home.password",
254 if (r
== -EUNATCH
) { /* EUNATCH is returned if no password was found and asking interactively was
255 * disabled via the flags. Not an error for us. */
256 log_debug_errno(r
, "No passwords acquired.");
260 return log_error_errno(r
, "Failed to acquire password: %m");
262 r
= user_record_set_password(hr
, password
, true);
264 return log_error_errno(r
, "Failed to store password: %m");
269 static int acquire_recovery_key(
270 const char *user_name
,
272 AskPasswordFlags flags
) {
274 _cleanup_(strv_free_erasep
) char **recovery_key
= NULL
;
275 _cleanup_(erase_and_freep
) char *envpw
= NULL
;
276 _cleanup_free_
char *question
= NULL
;
282 r
= getenv_steal_erase("PASSWORD", &envpw
);
284 return log_error_errno(r
, "Failed to acquire password from environment: %m");
286 /* People really shouldn't use environment variables for passing secrets. We support this
287 * only for testing purposes, and do not document the behaviour, so that people won't
288 * actually use this outside of testing. */
290 r
= user_record_set_password(hr
, STRV_MAKE(envpw
), true); /* recovery keys are stored in the record exactly like regular passwords! */
292 return log_error_errno(r
, "Failed to store recovery key: %m");
297 /* If this is not our own user, then don't use the password cache */
298 if (is_this_me(user_name
) <= 0)
299 SET_FLAG(flags
, ASK_PASSWORD_ACCEPT_CACHED
|ASK_PASSWORD_PUSH_CACHE
, false);
301 if (asprintf(&question
, "Please enter recovery key for user %s:", user_name
) < 0)
304 r
= ask_password_auto(question
,
305 /* icon= */ "user-home",
307 /* key_name= */ "home-recovery-key",
308 /* credential_name= */ "home.recovery-key",
312 if (r
== -EUNATCH
) { /* EUNATCH is returned if no recovery key was found and asking interactively was
313 * disabled via the flags. Not an error for us. */
314 log_debug_errno(r
, "No recovery keys acquired.");
318 return log_error_errno(r
, "Failed to acquire recovery keys: %m");
320 r
= user_record_set_password(hr
, recovery_key
, true);
322 return log_error_errno(r
, "Failed to store recovery keys: %m");
327 static int acquire_token_pin(
328 const char *user_name
,
330 AskPasswordFlags flags
) {
332 _cleanup_(strv_free_erasep
) char **pin
= NULL
;
333 _cleanup_(erase_and_freep
) char *envpin
= NULL
;
334 _cleanup_free_
char *question
= NULL
;
340 r
= getenv_steal_erase("PIN", &envpin
);
342 return log_error_errno(r
, "Failed to acquire PIN from environment: %m");
344 r
= user_record_set_token_pin(hr
, STRV_MAKE(envpin
), false);
346 return log_error_errno(r
, "Failed to store token PIN: %m");
351 /* If this is not our own user, then don't use the password cache */
352 if (is_this_me(user_name
) <= 0)
353 SET_FLAG(flags
, ASK_PASSWORD_ACCEPT_CACHED
|ASK_PASSWORD_PUSH_CACHE
, false);
355 if (asprintf(&question
, "Please enter security token PIN for user %s:", user_name
) < 0)
358 r
= ask_password_auto(
360 /* icon= */ "user-home",
362 /* key_name= */ "token-pin",
363 /* credential_name= */ "home.token-pin",
367 if (r
== -EUNATCH
) { /* EUNATCH is returned if no PIN was found and asking interactively was disabled
368 * via the flags. Not an error for us. */
369 log_debug_errno(r
, "No security token PINs acquired.");
373 return log_error_errno(r
, "Failed to acquire security token PIN: %m");
375 r
= user_record_set_token_pin(hr
, pin
, false);
377 return log_error_errno(r
, "Failed to store security token PIN: %m");
382 static int handle_generic_user_record_error(
383 const char *user_name
,
385 const sd_bus_error
*error
,
387 bool emphasize_current_password
) {
393 if (sd_bus_error_has_name(error
, BUS_ERROR_HOME_ABSENT
))
394 return log_error_errno(SYNTHETIC_ERRNO(EREMOTE
),
395 "Home of user %s is currently absent, please plug in the necessary storage device or backing file system.", user_name
);
397 else if (sd_bus_error_has_name(error
, BUS_ERROR_AUTHENTICATION_LIMIT_HIT
))
398 return log_error_errno(SYNTHETIC_ERRNO(ETOOMANYREFS
),
399 "Too frequent login attempts for user %s, try again later.", user_name
);
401 else if (sd_bus_error_has_name(error
, BUS_ERROR_BAD_PASSWORD
)) {
403 if (!strv_isempty(hr
->password
))
404 log_notice("Password incorrect or not sufficient, please try again.");
406 /* Don't consume cache entries or credentials here, we already tried that unsuccessfully. But
407 * let's push what we acquire here into the cache */
408 r
= acquire_existing_password(
411 emphasize_current_password
,
412 ASK_PASSWORD_PUSH_CACHE
| ASK_PASSWORD_NO_CREDENTIAL
);
416 } else if (sd_bus_error_has_name(error
, BUS_ERROR_BAD_RECOVERY_KEY
)) {
418 if (!strv_isempty(hr
->password
))
419 log_notice("Recovery key incorrect or not sufficient, please try again.");
421 /* Don't consume cache entries or credentials here, we already tried that unsuccessfully. But
422 * let's push what we acquire here into the cache */
423 r
= acquire_recovery_key(
426 ASK_PASSWORD_PUSH_CACHE
| ASK_PASSWORD_NO_CREDENTIAL
);
430 } else if (sd_bus_error_has_name(error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
)) {
432 if (strv_isempty(hr
->password
))
433 log_notice("Security token not inserted, please enter password.");
435 log_notice("Password incorrect or not sufficient, and configured security token not inserted, please try again.");
437 r
= acquire_existing_password(
440 emphasize_current_password
,
441 ASK_PASSWORD_PUSH_CACHE
| ASK_PASSWORD_NO_CREDENTIAL
);
445 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_PIN_NEEDED
)) {
447 /* First time the PIN is requested, let's accept cached data, and allow using credential store */
448 r
= acquire_token_pin(
451 ASK_PASSWORD_ACCEPT_CACHED
| ASK_PASSWORD_PUSH_CACHE
);
455 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_PROTECTED_AUTHENTICATION_PATH_NEEDED
)) {
457 log_notice("%s%sPlease authenticate physically on security token.",
458 emoji_enabled() ? special_glyph(SPECIAL_GLYPH_TOUCH
) : "",
459 emoji_enabled() ? " " : "");
461 r
= user_record_set_pkcs11_protected_authentication_path_permitted(hr
, true);
463 return log_error_errno(r
, "Failed to set PKCS#11 protected authentication path permitted flag: %m");
465 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_USER_PRESENCE_NEEDED
)) {
467 log_notice("%s%sPlease confirm presence on security token.",
468 emoji_enabled() ? special_glyph(SPECIAL_GLYPH_TOUCH
) : "",
469 emoji_enabled() ? " " : "");
471 r
= user_record_set_fido2_user_presence_permitted(hr
, true);
473 return log_error_errno(r
, "Failed to set FIDO2 user presence permitted flag: %m");
475 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_USER_VERIFICATION_NEEDED
)) {
477 log_notice("%s%sPlease verify user on security token.",
478 emoji_enabled() ? special_glyph(SPECIAL_GLYPH_TOUCH
) : "",
479 emoji_enabled() ? " " : "");
481 r
= user_record_set_fido2_user_verification_permitted(hr
, true);
483 return log_error_errno(r
, "Failed to set FIDO2 user verification permitted flag: %m");
485 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_PIN_LOCKED
))
486 return log_error_errno(SYNTHETIC_ERRNO(EPERM
), "Security token PIN is locked, please unlock it first. (Hint: Removal and re-insertion might suffice.)");
488 else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_BAD_PIN
)) {
490 log_notice("Security token PIN incorrect, please try again.");
492 /* If the previous PIN was wrong don't accept cached info anymore, but add to cache. Also, don't use the credential data */
493 r
= acquire_token_pin(
496 ASK_PASSWORD_PUSH_CACHE
| ASK_PASSWORD_NO_CREDENTIAL
);
500 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_BAD_PIN_FEW_TRIES_LEFT
)) {
502 log_notice("Security token PIN incorrect, please try again (only a few tries left!).");
504 r
= acquire_token_pin(
507 ASK_PASSWORD_PUSH_CACHE
| ASK_PASSWORD_NO_CREDENTIAL
);
511 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_BAD_PIN_ONE_TRY_LEFT
)) {
513 log_notice("Security token PIN incorrect, please try again (only one try left!).");
515 r
= acquire_token_pin(
518 ASK_PASSWORD_PUSH_CACHE
| ASK_PASSWORD_NO_CREDENTIAL
);
522 return log_error_errno(ret
, "Operation on home %s failed: %s", user_name
, bus_error_message(error
, ret
));
527 static int acquire_passed_secrets(const char *user_name
, UserRecord
**ret
) {
528 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
533 /* Generates an initial secret objects that contains passwords supplied via $PASSWORD, the password
534 * cache or the credentials subsystem, but excluding any interactive stuff. If nothing is passed,
535 * returns an empty secret object. */
537 secret
= user_record_new();
541 r
= acquire_existing_password(
544 /* emphasize_current_password = */ false,
545 ASK_PASSWORD_ACCEPT_CACHED
| ASK_PASSWORD_NO_TTY
| ASK_PASSWORD_NO_AGENT
);
549 r
= acquire_token_pin(
552 ASK_PASSWORD_ACCEPT_CACHED
| ASK_PASSWORD_NO_TTY
| ASK_PASSWORD_NO_AGENT
);
556 r
= acquire_recovery_key(
559 ASK_PASSWORD_ACCEPT_CACHED
| ASK_PASSWORD_NO_TTY
| ASK_PASSWORD_NO_AGENT
);
563 *ret
= TAKE_PTR(secret
);
567 static int activate_home(int argc
, char *argv
[], void *userdata
) {
568 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
571 r
= acquire_bus(&bus
);
575 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
576 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
578 r
= acquire_passed_secrets(*i
, &secret
);
583 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
584 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
586 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ActivateHome");
588 return bus_log_create_error(r
);
590 r
= sd_bus_message_append(m
, "s", *i
);
592 return bus_log_create_error(r
);
594 r
= bus_message_append_secret(m
, secret
);
596 return bus_log_create_error(r
);
598 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
600 r
= handle_generic_user_record_error(*i
, secret
, &error
, r
, /* emphasize_current_password= */ false);
615 static int deactivate_home(int argc
, char *argv
[], void *userdata
) {
616 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
619 r
= acquire_bus(&bus
);
623 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
624 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
625 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
627 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "DeactivateHome");
629 return bus_log_create_error(r
);
631 r
= sd_bus_message_append(m
, "s", *i
);
633 return bus_log_create_error(r
);
635 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
637 log_error_errno(r
, "Failed to deactivate user home: %s", bus_error_message(&error
, r
));
646 static void dump_home_record(UserRecord
*hr
) {
651 if (hr
->incomplete
) {
653 log_warning("Warning: lacking rights to acquire privileged fields of user record of '%s', output incomplete.", hr
->user_name
);
656 if (arg_json_format_flags
& JSON_FORMAT_OFF
)
657 user_record_show(hr
, true);
659 _cleanup_(user_record_unrefp
) UserRecord
*stripped
= NULL
;
661 if (arg_export_format
== EXPORT_FORMAT_STRIPPED
)
662 r
= user_record_clone(hr
, USER_RECORD_EXTRACT_EMBEDDED
|USER_RECORD_PERMISSIVE
, &stripped
);
663 else if (arg_export_format
== EXPORT_FORMAT_MINIMAL
)
664 r
= user_record_clone(hr
, USER_RECORD_EXTRACT_SIGNABLE
|USER_RECORD_PERMISSIVE
, &stripped
);
668 log_warning_errno(r
, "Failed to strip user record, ignoring: %m");
672 json_variant_dump(hr
->json
, arg_json_format_flags
, stdout
, NULL
);
676 static char **mangle_user_list(char **list
, char ***ret_allocated
) {
677 _cleanup_free_
char *myself
= NULL
;
680 if (!strv_isempty(list
)) {
681 *ret_allocated
= NULL
;
685 myself
= getusername_malloc();
693 l
[0] = TAKE_PTR(myself
);
700 static int inspect_home(int argc
, char *argv
[], void *userdata
) {
701 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
702 _cleanup_(strv_freep
) char **mangled_list
= NULL
;
706 pager_open(arg_pager_flags
);
708 r
= acquire_bus(&bus
);
712 items
= mangle_user_list(strv_skip(argv
, 1), &mangled_list
);
716 STRV_FOREACH(i
, items
) {
717 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
718 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
719 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
720 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
725 r
= parse_uid(*i
, &uid
);
727 if (!valid_user_group_name(*i
, 0)) {
728 log_error("Invalid user name '%s'.", *i
);
735 r
= bus_call_method(bus
, bus_mgr
, "GetUserRecordByName", &error
, &reply
, "s", *i
);
737 r
= bus_call_method(bus
, bus_mgr
, "GetUserRecordByUID", &error
, &reply
, "u", (uint32_t) uid
);
740 log_error_errno(r
, "Failed to inspect home: %s", bus_error_message(&error
, r
));
747 r
= sd_bus_message_read(reply
, "sbo", &json
, &incomplete
, NULL
);
749 bus_log_parse_error(r
);
756 r
= json_parse(json
, JSON_PARSE_SENSITIVE
, &v
, NULL
, NULL
);
758 log_error_errno(r
, "Failed to parse JSON identity: %m");
765 hr
= user_record_new();
769 r
= user_record_load(hr
, v
, USER_RECORD_LOAD_REFUSE_SECRET
|USER_RECORD_LOG
|USER_RECORD_PERMISSIVE
);
777 hr
->incomplete
= incomplete
;
778 dump_home_record(hr
);
784 static int authenticate_home(int argc
, char *argv
[], void *userdata
) {
785 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
786 _cleanup_(strv_freep
) char **mangled_list
= NULL
;
790 items
= mangle_user_list(strv_skip(argv
, 1), &mangled_list
);
794 r
= acquire_bus(&bus
);
798 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
800 STRV_FOREACH(i
, items
) {
801 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
803 r
= acquire_passed_secrets(*i
, &secret
);
808 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
809 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
811 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "AuthenticateHome");
813 return bus_log_create_error(r
);
815 r
= sd_bus_message_append(m
, "s", *i
);
817 return bus_log_create_error(r
);
819 r
= bus_message_append_secret(m
, secret
);
821 return bus_log_create_error(r
);
823 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
825 r
= handle_generic_user_record_error(*i
, secret
, &error
, r
, false);
840 static int update_last_change(JsonVariant
**v
, bool with_password
, bool override
) {
847 n
= now(CLOCK_REALTIME
);
849 c
= json_variant_by_key(*v
, "lastChangeUSec");
854 goto update_password
;
856 if (!json_variant_is_unsigned(c
))
857 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastChangeUSec field is not an unsigned integer, refusing.");
859 u
= json_variant_unsigned(c
);
861 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastChangeUSec is from the future, can't update.");
864 r
= json_variant_set_field_unsigned(v
, "lastChangeUSec", n
);
866 return log_error_errno(r
, "Failed to update lastChangeUSec: %m");
872 c
= json_variant_by_key(*v
, "lastPasswordChangeUSec");
879 if (!json_variant_is_unsigned(c
))
880 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastPasswordChangeUSec field is not an unsigned integer, refusing.");
882 u
= json_variant_unsigned(c
);
884 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastPasswordChangeUSec is from the future, can't update.");
887 r
= json_variant_set_field_unsigned(v
, "lastPasswordChangeUSec", n
);
889 return log_error_errno(r
, "Failed to update lastPasswordChangeUSec: %m");
894 static int apply_identity_changes(JsonVariant
**_v
) {
895 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
900 v
= json_variant_ref(*_v
);
902 r
= json_variant_filter(&v
, arg_identity_filter
);
904 return log_error_errno(r
, "Failed to filter identity: %m");
906 r
= json_variant_merge(&v
, arg_identity_extra
);
908 return log_error_errno(r
, "Failed to merge identities: %m");
910 if (arg_identity_extra_this_machine
|| !strv_isempty(arg_identity_filter
)) {
911 _cleanup_(json_variant_unrefp
) JsonVariant
*per_machine
= NULL
, *mmid
= NULL
;
914 r
= sd_id128_get_machine(&mid
);
916 return log_error_errno(r
, "Failed to acquire machine ID: %m");
918 r
= json_variant_new_string(&mmid
, SD_ID128_TO_STRING(mid
));
920 return log_error_errno(r
, "Failed to allocate matchMachineId object: %m");
922 per_machine
= json_variant_ref(json_variant_by_key(v
, "perMachine"));
924 _cleanup_(json_variant_unrefp
) JsonVariant
*npm
= NULL
, *add
= NULL
;
925 _cleanup_free_ JsonVariant
**array
= NULL
;
929 if (!json_variant_is_array(per_machine
))
930 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "perMachine field is not an array, refusing.");
932 array
= new(JsonVariant
*, json_variant_elements(per_machine
) + 1);
936 JSON_VARIANT_ARRAY_FOREACH(z
, per_machine
) {
939 if (!json_variant_is_object(z
))
940 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "perMachine entry is not an object, refusing.");
944 u
= json_variant_by_key(z
, "matchMachineId");
948 if (!json_variant_equal(u
, mmid
))
951 r
= json_variant_merge(&add
, z
);
953 return log_error_errno(r
, "Failed to merge perMachine entry: %m");
958 r
= json_variant_filter(&add
, arg_identity_filter
);
960 return log_error_errno(r
, "Failed to filter perMachine: %m");
962 r
= json_variant_merge(&add
, arg_identity_extra_this_machine
);
964 return log_error_errno(r
, "Failed to merge in perMachine fields: %m");
966 if (arg_identity_filter_rlimits
|| arg_identity_extra_rlimits
) {
967 _cleanup_(json_variant_unrefp
) JsonVariant
*rlv
= NULL
;
969 rlv
= json_variant_ref(json_variant_by_key(add
, "resourceLimits"));
971 r
= json_variant_filter(&rlv
, arg_identity_filter_rlimits
);
973 return log_error_errno(r
, "Failed to filter resource limits: %m");
975 r
= json_variant_merge(&rlv
, arg_identity_extra_rlimits
);
977 return log_error_errno(r
, "Failed to set resource limits: %m");
979 if (json_variant_is_blank_object(rlv
)) {
980 r
= json_variant_filter(&add
, STRV_MAKE("resourceLimits"));
982 return log_error_errno(r
, "Failed to drop resource limits field from identity: %m");
984 r
= json_variant_set_field(&add
, "resourceLimits", rlv
);
986 return log_error_errno(r
, "Failed to update resource limits of identity: %m");
990 if (!json_variant_is_blank_object(add
)) {
991 r
= json_variant_set_field(&add
, "matchMachineId", mmid
);
993 return log_error_errno(r
, "Failed to set matchMachineId field: %m");
998 r
= json_variant_new_array(&npm
, array
, i
);
1000 return log_error_errno(r
, "Failed to allocate new perMachine array: %m");
1002 json_variant_unref(per_machine
);
1003 per_machine
= TAKE_PTR(npm
);
1005 _cleanup_(json_variant_unrefp
) JsonVariant
*item
= json_variant_ref(arg_identity_extra_this_machine
);
1007 if (arg_identity_extra_rlimits
) {
1008 r
= json_variant_set_field(&item
, "resourceLimits", arg_identity_extra_rlimits
);
1010 return log_error_errno(r
, "Failed to update resource limits of identity: %m");
1013 r
= json_variant_set_field(&item
, "matchMachineId", mmid
);
1015 return log_error_errno(r
, "Failed to set matchMachineId field: %m");
1017 r
= json_variant_append_array(&per_machine
, item
);
1019 return log_error_errno(r
, "Failed to append to perMachine array: %m");
1022 r
= json_variant_set_field(&v
, "perMachine", per_machine
);
1024 return log_error_errno(r
, "Failed to update per machine record: %m");
1027 if (arg_identity_extra_privileged
|| arg_identity_filter
) {
1028 _cleanup_(json_variant_unrefp
) JsonVariant
*privileged
= NULL
;
1030 privileged
= json_variant_ref(json_variant_by_key(v
, "privileged"));
1032 r
= json_variant_filter(&privileged
, arg_identity_filter
);
1034 return log_error_errno(r
, "Failed to filter identity (privileged part): %m");
1036 r
= json_variant_merge(&privileged
, arg_identity_extra_privileged
);
1038 return log_error_errno(r
, "Failed to merge identities (privileged part): %m");
1040 if (json_variant_is_blank_object(privileged
)) {
1041 r
= json_variant_filter(&v
, STRV_MAKE("privileged"));
1043 return log_error_errno(r
, "Failed to drop privileged part from identity: %m");
1045 r
= json_variant_set_field(&v
, "privileged", privileged
);
1047 return log_error_errno(r
, "Failed to update privileged part of identity: %m");
1051 if (arg_identity_filter_rlimits
) {
1052 _cleanup_(json_variant_unrefp
) JsonVariant
*rlv
= NULL
;
1054 rlv
= json_variant_ref(json_variant_by_key(v
, "resourceLimits"));
1056 r
= json_variant_filter(&rlv
, arg_identity_filter_rlimits
);
1058 return log_error_errno(r
, "Failed to filter resource limits: %m");
1060 /* Note that we only filter resource limits here, but don't apply them. We do that in the perMachine section */
1062 if (json_variant_is_blank_object(rlv
)) {
1063 r
= json_variant_filter(&v
, STRV_MAKE("resourceLimits"));
1065 return log_error_errno(r
, "Failed to drop resource limits field from identity: %m");
1067 r
= json_variant_set_field(&v
, "resourceLimits", rlv
);
1069 return log_error_errno(r
, "Failed to update resource limits of identity: %m");
1073 json_variant_unref(*_v
);
1079 static int add_disposition(JsonVariant
**v
) {
1084 if (json_variant_by_key(*v
, "disposition"))
1087 /* Set the disposition to regular, if not configured explicitly */
1088 r
= json_variant_set_field_string(v
, "disposition", "regular");
1090 return log_error_errno(r
, "Failed to set disposition field: %m");
1095 static int acquire_new_home_record(UserRecord
**ret
) {
1096 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
1097 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
1103 unsigned line
, column
;
1105 r
= json_parse_file(
1106 streq(arg_identity
, "-") ? stdin
: NULL
,
1107 streq(arg_identity
, "-") ? "<stdin>" : arg_identity
, JSON_PARSE_SENSITIVE
, &v
, &line
, &column
);
1109 return log_error_errno(r
, "Failed to parse identity at %u:%u: %m", line
, column
);
1112 r
= apply_identity_changes(&v
);
1116 r
= add_disposition(&v
);
1120 STRV_FOREACH(i
, arg_pkcs11_token_uri
) {
1121 r
= identity_add_pkcs11_key_data(&v
, *i
);
1126 STRV_FOREACH(i
, arg_fido2_device
) {
1127 r
= identity_add_fido2_parameters(&v
, *i
, arg_fido2_lock_with
, arg_fido2_cred_alg
);
1132 if (arg_recovery_key
) {
1133 r
= identity_add_recovery_key(&v
);
1138 r
= update_last_change(&v
, true, false);
1143 json_variant_dump(v
, JSON_FORMAT_PRETTY
, NULL
, NULL
);
1145 hr
= user_record_new();
1149 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
);
1153 *ret
= TAKE_PTR(hr
);
1157 static int acquire_new_password(
1158 const char *user_name
,
1163 _cleanup_(erase_and_freep
) char *envpw
= NULL
;
1170 r
= getenv_steal_erase("NEWPASSWORD", &envpw
);
1172 return log_error_errno(r
, "Failed to acquire password from environment: %m");
1174 /* As above, this is not for use, just for testing */
1176 r
= user_record_set_password(hr
, STRV_MAKE(envpw
), /* prepend = */ true);
1178 return log_error_errno(r
, "Failed to store password: %m");
1181 *ret
= TAKE_PTR(envpw
);
1187 (void) suggest_passwords();
1190 _cleanup_(strv_free_erasep
) char **first
= NULL
, **second
= NULL
;
1191 _cleanup_free_
char *question
= NULL
;
1194 return log_error_errno(SYNTHETIC_ERRNO(ENOKEY
), "Too many attempts, giving up:");
1196 if (asprintf(&question
, "Please enter new password for user %s:", user_name
) < 0)
1199 r
= ask_password_auto(
1201 /* icon= */ "user-home",
1203 /* key_name= */ "home-password",
1204 /* credential_name= */ "home.new-password",
1206 0, /* no caching, we want to collect a new password here after all */
1209 return log_error_errno(r
, "Failed to acquire password: %m");
1211 question
= mfree(question
);
1212 if (asprintf(&question
, "Please enter new password for user %s (repeat):", user_name
) < 0)
1215 r
= ask_password_auto(
1217 /* icon= */ "user-home",
1219 /* key_name= */ "home-password",
1220 /* credential_name= */ "home.new-password",
1225 return log_error_errno(r
, "Failed to acquire password: %m");
1227 if (strv_equal(first
, second
)) {
1228 _cleanup_(erase_and_freep
) char *copy
= NULL
;
1231 copy
= strdup(first
[0]);
1236 r
= user_record_set_password(hr
, first
, /* prepend = */ true);
1238 return log_error_errno(r
, "Failed to store password: %m");
1241 *ret
= TAKE_PTR(copy
);
1246 log_error("Password didn't match, try again.");
1250 static int create_home(int argc
, char *argv
[], void *userdata
) {
1251 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1252 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
1255 r
= acquire_bus(&bus
);
1259 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1262 /* If a username was specified, use it */
1264 if (valid_user_group_name(argv
[1], 0))
1265 r
= json_variant_set_field_string(&arg_identity_extra
, "userName", argv
[1]);
1267 _cleanup_free_
char *un
= NULL
, *rr
= NULL
;
1269 /* Before we consider the user name invalid, let's check if we can split it? */
1270 r
= split_user_name_realm(argv
[1], &un
, &rr
);
1272 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User name '%s' is not valid: %m", argv
[1]);
1275 r
= json_variant_set_field_string(&arg_identity_extra
, "realm", rr
);
1277 return log_error_errno(r
, "Failed to set realm field: %m");
1280 r
= json_variant_set_field_string(&arg_identity_extra
, "userName", un
);
1283 return log_error_errno(r
, "Failed to set userName field: %m");
1285 /* If neither a username nor an identity have been specified we cannot operate. */
1287 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User name required.");
1290 r
= acquire_new_home_record(&hr
);
1294 /* If the JSON record carries no plain text password (besides the recovery key), then let's query it
1296 if (strv_length(hr
->password
) <= arg_recovery_key
) {
1298 if (strv_isempty(hr
->hashed_password
)) {
1299 _cleanup_(erase_and_freep
) char *new_password
= NULL
;
1301 /* No regular (i.e. non-PKCS#11) hashed passwords set in the record, let's fix that. */
1302 r
= acquire_new_password(hr
->user_name
, hr
, /* suggest = */ true, &new_password
);
1306 r
= user_record_make_hashed_password(hr
, STRV_MAKE(new_password
), /* extend = */ false);
1308 return log_error_errno(r
, "Failed to hash password: %m");
1310 /* There's a hash password set in the record, acquire the unhashed version of it. */
1311 r
= acquire_existing_password(
1314 /* emphasize_current= */ false,
1315 ASK_PASSWORD_ACCEPT_CACHED
| ASK_PASSWORD_PUSH_CACHE
);
1321 if (hr
->enforce_password_policy
== 0) {
1322 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1324 /* If password quality enforcement is disabled, let's at least warn client side */
1326 r
= user_record_quality_check_password(hr
, hr
, &error
);
1328 log_warning_errno(r
, "Specified password does not pass quality checks (%s), proceeding anyway.", bus_error_message(&error
, r
));
1332 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1333 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1334 _cleanup_(erase_and_freep
) char *formatted
= NULL
;
1336 r
= json_variant_format(hr
->json
, 0, &formatted
);
1338 return log_error_errno(r
, "Failed to format user record: %m");
1340 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "CreateHome");
1342 return bus_log_create_error(r
);
1344 (void) sd_bus_message_sensitive(m
);
1346 r
= sd_bus_message_append(m
, "s", formatted
);
1348 return bus_log_create_error(r
);
1350 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1352 if (sd_bus_error_has_name(&error
, BUS_ERROR_LOW_PASSWORD_QUALITY
)) {
1353 _cleanup_(erase_and_freep
) char *new_password
= NULL
;
1355 log_error_errno(r
, "%s", bus_error_message(&error
, r
));
1356 log_info("(Use --enforce-password-policy=no to turn off password quality checks for this account.)");
1358 r
= acquire_new_password(hr
->user_name
, hr
, /* suggest = */ false, &new_password
);
1362 r
= user_record_make_hashed_password(hr
, STRV_MAKE(new_password
), /* extend = */ false);
1364 return log_error_errno(r
, "Failed to hash passwords: %m");
1366 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1377 static int remove_home(int argc
, char *argv
[], void *userdata
) {
1378 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1381 r
= acquire_bus(&bus
);
1385 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1387 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
1388 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1389 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1391 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "RemoveHome");
1393 return bus_log_create_error(r
);
1395 r
= sd_bus_message_append(m
, "s", *i
);
1397 return bus_log_create_error(r
);
1399 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1401 log_error_errno(r
, "Failed to remove home: %s", bus_error_message(&error
, r
));
1410 static int acquire_updated_home_record(
1412 const char *username
,
1415 _cleanup_(json_variant_unrefp
) JsonVariant
*json
= NULL
;
1416 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
1422 unsigned line
, column
;
1425 r
= json_parse_file(
1426 streq(arg_identity
, "-") ? stdin
: NULL
,
1427 streq(arg_identity
, "-") ? "<stdin>" : arg_identity
, JSON_PARSE_SENSITIVE
, &json
, &line
, &column
);
1429 return log_error_errno(r
, "Failed to parse identity at %u:%u: %m", line
, column
);
1431 un
= json_variant_by_key(json
, "userName");
1433 if (!json_variant_is_string(un
) || (username
&& !streq(json_variant_string(un
), username
)))
1434 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User name specified on command line and in JSON record do not match.");
1437 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "No username specified.");
1439 r
= json_variant_set_field_string(&arg_identity_extra
, "userName", username
);
1441 return log_error_errno(r
, "Failed to set userName field: %m");
1445 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1446 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1450 if (!identity_properties_specified())
1451 return log_error_errno(SYNTHETIC_ERRNO(EALREADY
), "No field to change specified.");
1453 r
= bus_call_method(bus
, bus_mgr
, "GetUserRecordByName", &error
, &reply
, "s", username
);
1455 return log_error_errno(r
, "Failed to acquire user home record: %s", bus_error_message(&error
, r
));
1457 r
= sd_bus_message_read(reply
, "sbo", &text
, &incomplete
, NULL
);
1459 return bus_log_parse_error(r
);
1462 return log_error_errno(SYNTHETIC_ERRNO(EACCES
), "Lacking rights to acquire user record including privileged metadata, can't update record.");
1464 r
= json_parse(text
, JSON_PARSE_SENSITIVE
, &json
, NULL
, NULL
);
1466 return log_error_errno(r
, "Failed to parse JSON identity: %m");
1468 reply
= sd_bus_message_unref(reply
);
1470 r
= json_variant_filter(&json
, STRV_MAKE("binding", "status", "signature"));
1472 return log_error_errno(r
, "Failed to strip binding and status from record to update: %m");
1475 r
= apply_identity_changes(&json
);
1479 STRV_FOREACH(i
, arg_pkcs11_token_uri
) {
1480 r
= identity_add_pkcs11_key_data(&json
, *i
);
1485 STRV_FOREACH(i
, arg_fido2_device
) {
1486 r
= identity_add_fido2_parameters(&json
, *i
, arg_fido2_lock_with
, arg_fido2_cred_alg
);
1491 /* If the user supplied a full record, then add in lastChange, but do not override. Otherwise always
1493 r
= update_last_change(&json
, arg_pkcs11_token_uri
|| arg_fido2_device
, !arg_identity
);
1498 json_variant_dump(json
, JSON_FORMAT_PRETTY
, NULL
, NULL
);
1500 hr
= user_record_new();
1504 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
);
1508 *ret
= TAKE_PTR(hr
);
1512 static int home_record_reset_human_interaction_permission(UserRecord
*hr
) {
1517 /* When we execute multiple operations one after the other, let's reset the permission to ask the
1518 * user each time, so that if interaction is necessary we will be told so again and thus can print a
1519 * nice message to the user, telling the user so. */
1521 r
= user_record_set_pkcs11_protected_authentication_path_permitted(hr
, -1);
1523 return log_error_errno(r
, "Failed to reset PKCS#11 protected authentication path permission flag: %m");
1525 r
= user_record_set_fido2_user_presence_permitted(hr
, -1);
1527 return log_error_errno(r
, "Failed to reset FIDO2 user presence permission flag: %m");
1529 r
= user_record_set_fido2_user_verification_permitted(hr
, -1);
1531 return log_error_errno(r
, "Failed to reset FIDO2 user verification permission flag: %m");
1536 static int update_home(int argc
, char *argv
[], void *userdata
) {
1537 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1538 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
, *secret
= NULL
;
1539 _cleanup_free_
char *buffer
= NULL
;
1540 const char *username
;
1545 else if (!arg_identity
) {
1546 buffer
= getusername_malloc();
1554 r
= acquire_bus(&bus
);
1558 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1560 r
= acquire_updated_home_record(bus
, username
, &hr
);
1564 /* Add in all secrets we can acquire cheaply */
1565 r
= acquire_passed_secrets(username
, &secret
);
1569 r
= user_record_merge_secret(hr
, secret
);
1573 /* If we do multiple operations, let's output things more verbosely, since otherwise the repeated
1574 * authentication might be confusing. */
1576 if (arg_and_resize
|| arg_and_change_password
)
1577 log_info("Updating home directory.");
1580 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1581 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1582 _cleanup_free_
char *formatted
= NULL
;
1584 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "UpdateHome");
1586 return bus_log_create_error(r
);
1588 r
= json_variant_format(hr
->json
, 0, &formatted
);
1590 return log_error_errno(r
, "Failed to format user record: %m");
1592 (void) sd_bus_message_sensitive(m
);
1594 r
= sd_bus_message_append(m
, "s", formatted
);
1596 return bus_log_create_error(r
);
1598 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1600 if (arg_and_change_password
&&
1601 sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1602 /* In the generic handler we'd ask for a password in this case, but when
1603 * changing passwords that's not sufficient, as we need to acquire all keys
1605 return log_error_errno(r
, "Security token not inserted, refusing.");
1607 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1615 log_info("Resizing home.");
1617 (void) home_record_reset_human_interaction_permission(hr
);
1619 /* Also sync down disk size to underlying LUKS/fscrypt/quota */
1620 while (arg_and_resize
) {
1621 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1622 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1624 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ResizeHome");
1626 return bus_log_create_error(r
);
1628 /* Specify UINT64_MAX as size, in which case the underlying disk size will just be synced */
1629 r
= sd_bus_message_append(m
, "st", hr
->user_name
, UINT64_MAX
);
1631 return bus_log_create_error(r
);
1633 r
= bus_message_append_secret(m
, hr
);
1635 return bus_log_create_error(r
);
1637 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1639 if (arg_and_change_password
&&
1640 sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1641 return log_error_errno(r
, "Security token not inserted, refusing.");
1643 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1650 if (arg_and_change_password
)
1651 log_info("Synchronizing passwords and encryption keys.");
1653 (void) home_record_reset_human_interaction_permission(hr
);
1655 /* Also sync down passwords to underlying LUKS/fscrypt */
1656 while (arg_and_change_password
) {
1657 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1658 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1660 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ChangePasswordHome");
1662 return bus_log_create_error(r
);
1664 /* Specify an empty new secret, in which case the underlying LUKS/fscrypt password will just be synced */
1665 r
= sd_bus_message_append(m
, "ss", hr
->user_name
, "{}");
1667 return bus_log_create_error(r
);
1669 r
= bus_message_append_secret(m
, hr
);
1671 return bus_log_create_error(r
);
1673 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1675 if (sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1676 return log_error_errno(r
, "Security token not inserted, refusing.");
1678 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1688 static int passwd_home(int argc
, char *argv
[], void *userdata
) {
1689 _cleanup_(user_record_unrefp
) UserRecord
*old_secret
= NULL
, *new_secret
= NULL
;
1690 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1691 _cleanup_free_
char *buffer
= NULL
;
1692 const char *username
;
1695 if (arg_pkcs11_token_uri
)
1696 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1697 "To change the PKCS#11 security token use 'homectl update --pkcs11-token-uri=%s'.",
1698 special_glyph(SPECIAL_GLYPH_ELLIPSIS
));
1699 if (arg_fido2_device
)
1700 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1701 "To change the FIDO2 security token use 'homectl update --fido2-device=%s'.",
1702 special_glyph(SPECIAL_GLYPH_ELLIPSIS
));
1703 if (identity_properties_specified())
1704 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "The 'passwd' verb does not permit changing other record properties at the same time.");
1709 buffer
= getusername_malloc();
1716 r
= acquire_bus(&bus
);
1720 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1722 r
= acquire_passed_secrets(username
, &old_secret
);
1726 new_secret
= user_record_new();
1730 r
= acquire_new_password(username
, new_secret
, /* suggest = */ true, NULL
);
1735 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1736 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1738 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ChangePasswordHome");
1740 return bus_log_create_error(r
);
1742 r
= sd_bus_message_append(m
, "s", username
);
1744 return bus_log_create_error(r
);
1746 r
= bus_message_append_secret(m
, new_secret
);
1748 return bus_log_create_error(r
);
1750 r
= bus_message_append_secret(m
, old_secret
);
1752 return bus_log_create_error(r
);
1754 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1756 if (sd_bus_error_has_name(&error
, BUS_ERROR_LOW_PASSWORD_QUALITY
)) {
1758 log_error_errno(r
, "%s", bus_error_message(&error
, r
));
1760 r
= acquire_new_password(username
, new_secret
, /* suggest = */ false, NULL
);
1762 } else if (sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1764 /* In the generic handler we'd ask for a password in this case, but when
1765 * changing passwords that's not sufficeint, as we need to acquire all keys
1767 return log_error_errno(r
, "Security token not inserted, refusing.");
1769 r
= handle_generic_user_record_error(username
, old_secret
, &error
, r
, true);
1779 static int parse_disk_size(const char *t
, uint64_t *ret
) {
1785 if (streq(t
, "min"))
1787 else if (streq(t
, "max"))
1788 *ret
= UINT64_MAX
-1; /* Largest size that isn't UINT64_MAX special marker */
1792 r
= parse_size(t
, 1024, &ds
);
1794 return log_error_errno(r
, "Failed to parse disk size parameter: %s", t
);
1796 if (ds
>= UINT64_MAX
) /* UINT64_MAX has special meaning for us ("dont change"), refuse */
1797 return log_error_errno(SYNTHETIC_ERRNO(ERANGE
), "Disk size out of range: %s", t
);
1805 static int resize_home(int argc
, char *argv
[], void *userdata
) {
1806 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1807 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
1808 uint64_t ds
= UINT64_MAX
;
1811 r
= acquire_bus(&bus
);
1815 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1817 if (arg_disk_size_relative
!= UINT64_MAX
||
1818 (argc
> 2 && parse_permyriad(argv
[2]) >= 0))
1819 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
1820 "Relative disk size specification currently not supported when resizing.");
1823 r
= parse_disk_size(argv
[2], &ds
);
1828 if (arg_disk_size
!= UINT64_MAX
) {
1829 if (ds
!= UINT64_MAX
&& ds
!= arg_disk_size
)
1830 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Disk size specified twice and doesn't match, refusing.");
1835 r
= acquire_passed_secrets(argv
[1], &secret
);
1840 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1841 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1843 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ResizeHome");
1845 return bus_log_create_error(r
);
1847 r
= sd_bus_message_append(m
, "st", argv
[1], ds
);
1849 return bus_log_create_error(r
);
1851 r
= bus_message_append_secret(m
, secret
);
1853 return bus_log_create_error(r
);
1855 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1857 r
= handle_generic_user_record_error(argv
[1], secret
, &error
, r
, false);
1867 static int lock_home(int argc
, char *argv
[], void *userdata
) {
1868 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1871 r
= acquire_bus(&bus
);
1875 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
1876 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1877 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1879 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "LockHome");
1881 return bus_log_create_error(r
);
1883 r
= sd_bus_message_append(m
, "s", *i
);
1885 return bus_log_create_error(r
);
1887 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1889 log_error_errno(r
, "Failed to lock home: %s", bus_error_message(&error
, r
));
1898 static int unlock_home(int argc
, char *argv
[], void *userdata
) {
1899 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1902 r
= acquire_bus(&bus
);
1906 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
1907 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
1909 r
= acquire_passed_secrets(*i
, &secret
);
1914 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1915 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1917 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "UnlockHome");
1919 return bus_log_create_error(r
);
1921 r
= sd_bus_message_append(m
, "s", *i
);
1923 return bus_log_create_error(r
);
1925 r
= bus_message_append_secret(m
, secret
);
1927 return bus_log_create_error(r
);
1929 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1931 r
= handle_generic_user_record_error(argv
[1], secret
, &error
, r
, false);
1946 static int with_home(int argc
, char *argv
[], void *userdata
) {
1947 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1948 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1949 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1950 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
1951 _cleanup_close_
int acquired_fd
= -EBADF
;
1952 _cleanup_strv_free_
char **cmdline
= NULL
;
1957 r
= acquire_bus(&bus
);
1962 _cleanup_free_
char *shell
= NULL
;
1964 /* If no command is specified, spawn a shell */
1965 r
= get_shell(&shell
);
1967 return log_error_errno(r
, "Failed to acquire shell: %m");
1969 cmdline
= strv_new(shell
);
1971 cmdline
= strv_copy(argv
+ 2);
1975 r
= acquire_passed_secrets(argv
[1], &secret
);
1980 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "AcquireHome");
1982 return bus_log_create_error(r
);
1984 r
= sd_bus_message_append(m
, "s", argv
[1]);
1986 return bus_log_create_error(r
);
1988 r
= bus_message_append_secret(m
, secret
);
1990 return bus_log_create_error(r
);
1992 r
= sd_bus_message_append(m
, "b", /* please_suspend = */ getenv_bool("SYSTEMD_PLEASE_SUSPEND_HOME") > 0);
1994 return bus_log_create_error(r
);
1996 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, &reply
);
1997 m
= sd_bus_message_unref(m
);
1999 r
= handle_generic_user_record_error(argv
[1], secret
, &error
, r
, false);
2003 sd_bus_error_free(&error
);
2007 r
= sd_bus_message_read(reply
, "h", &fd
);
2009 return bus_log_parse_error(r
);
2011 acquired_fd
= fcntl(fd
, F_DUPFD_CLOEXEC
, 3);
2012 if (acquired_fd
< 0)
2013 return log_error_errno(errno
, "Failed to duplicate acquired fd: %m");
2015 reply
= sd_bus_message_unref(reply
);
2020 r
= bus_call_method(bus
, bus_mgr
, "GetHomeByName", &error
, &reply
, "s", argv
[1]);
2022 return log_error_errno(r
, "Failed to inspect home: %s", bus_error_message(&error
, r
));
2024 r
= sd_bus_message_read(reply
, "usussso", NULL
, NULL
, NULL
, NULL
, &home
, NULL
, NULL
);
2026 return bus_log_parse_error(r
);
2028 r
= safe_fork("(with)", FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_DEATHSIG
|FORK_LOG
|FORK_RLIMIT_NOFILE_SAFE
|FORK_REOPEN_LOG
, &pid
);
2032 if (chdir(home
) < 0) {
2033 log_error_errno(errno
, "Failed to change to directory %s: %m", home
);
2037 execvp(cmdline
[0], cmdline
);
2038 log_error_errno(errno
, "Failed to execute %s: %m", cmdline
[0]);
2042 ret
= wait_for_terminate_and_check(cmdline
[0], pid
, WAIT_LOG_ABNORMAL
);
2044 /* Close the fd that pings the home now. */
2045 acquired_fd
= safe_close(acquired_fd
);
2047 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ReleaseHome");
2049 return bus_log_create_error(r
);
2051 r
= sd_bus_message_append(m
, "s", argv
[1]);
2053 return bus_log_create_error(r
);
2055 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
2057 if (sd_bus_error_has_name(&error
, BUS_ERROR_HOME_BUSY
))
2058 log_notice("Not deactivating home directory of %s, as it is still used.", argv
[1]);
2060 return log_error_errno(r
, "Failed to release user home: %s", bus_error_message(&error
, r
));
2066 static int lock_all_homes(int argc
, char *argv
[], void *userdata
) {
2067 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2068 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2069 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
2072 r
= acquire_bus(&bus
);
2076 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "LockAllHomes");
2078 return bus_log_create_error(r
);
2080 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
2082 return log_error_errno(r
, "Failed to lock all homes: %s", bus_error_message(&error
, r
));
2087 static int deactivate_all_homes(int argc
, char *argv
[], void *userdata
) {
2088 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2089 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2090 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
2093 r
= acquire_bus(&bus
);
2097 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "DeactivateAllHomes");
2099 return bus_log_create_error(r
);
2101 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
2103 return log_error_errno(r
, "Failed to deactivate all homes: %s", bus_error_message(&error
, r
));
2108 static int rebalance(int argc
, char *argv
[], void *userdata
) {
2109 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2110 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2111 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
2114 r
= acquire_bus(&bus
);
2118 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "Rebalance");
2120 return bus_log_create_error(r
);
2122 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
2124 if (sd_bus_error_has_name(&error
, BUS_ERROR_REBALANCE_NOT_NEEDED
))
2125 log_info("No homes needed rebalancing.");
2127 return log_error_errno(r
, "Failed to rebalance: %s", bus_error_message(&error
, r
));
2129 log_info("Completed rebalancing.");
2134 static int drop_from_identity(const char *field
) {
2139 /* If we are called to update an identity record and drop some field, let's keep track of what to
2140 * remove from the old record */
2141 r
= strv_extend(&arg_identity_filter
, field
);
2145 /* Let's also drop the field if it was previously set to a new value on the same command line */
2146 r
= json_variant_filter(&arg_identity_extra
, STRV_MAKE(field
));
2148 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
2150 r
= json_variant_filter(&arg_identity_extra_this_machine
, STRV_MAKE(field
));
2152 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
2154 r
= json_variant_filter(&arg_identity_extra_privileged
, STRV_MAKE(field
));
2156 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
2161 static int help(int argc
, char *argv
[], void *userdata
) {
2162 _cleanup_free_
char *link
= NULL
;
2165 pager_open(arg_pager_flags
);
2167 r
= terminal_urlify_man("homectl", "1", &link
);
2171 printf("%1$s [OPTIONS...] COMMAND ...\n\n"
2172 "%2$sCreate, manipulate or inspect home directories.%3$s\n"
2173 "\n%4$sCommands:%5$s\n"
2174 " list List home areas\n"
2175 " activate USER… Activate a home area\n"
2176 " deactivate USER… Deactivate a home area\n"
2177 " inspect USER… Inspect a home area\n"
2178 " authenticate USER… Authenticate a home area\n"
2179 " create USER Create a home area\n"
2180 " remove USER… Remove a home area\n"
2181 " update USER Update a home area\n"
2182 " passwd USER Change password of a home area\n"
2183 " resize USER SIZE Resize a home area\n"
2184 " lock USER… Temporarily lock an active home area\n"
2185 " unlock USER… Unlock a temporarily locked home area\n"
2186 " lock-all Lock all suitable home areas\n"
2187 " deactivate-all Deactivate all active home areas\n"
2188 " rebalance Rebalance free space between home areas\n"
2189 " with USER [COMMAND…] Run shell or command with access to a home area\n"
2190 "\n%4$sOptions:%5$s\n"
2191 " -h --help Show this help\n"
2192 " --version Show package version\n"
2193 " --no-pager Do not pipe output into a pager\n"
2194 " --no-legend Do not show the headers and footers\n"
2195 " --no-ask-password Do not ask for system passwords\n"
2196 " -H --host=[USER@]HOST Operate on remote host\n"
2197 " -M --machine=CONTAINER Operate on local container\n"
2198 " --identity=PATH Read JSON identity from file\n"
2199 " --json=FORMAT Output inspection data in JSON (takes one of\n"
2200 " pretty, short, off)\n"
2201 " -j Equivalent to --json=pretty (on TTY) or\n"
2202 " --json=short (otherwise)\n"
2203 " --export-format= Strip JSON inspection data (full, stripped,\n"
2205 " -E When specified once equals -j --export-format=\n"
2206 " stripped, when specified twice equals\n"
2207 " -j --export-format=minimal\n"
2208 "\n%4$sGeneral User Record Properties:%5$s\n"
2209 " -c --real-name=REALNAME Real name for user\n"
2210 " --realm=REALM Realm to create user in\n"
2211 " --email-address=EMAIL Email address for user\n"
2212 " --location=LOCATION Set location of user on earth\n"
2213 " --icon-name=NAME Icon name for user\n"
2214 " -d --home-dir=PATH Home directory\n"
2215 " -u --uid=UID Numeric UID for user\n"
2216 " -G --member-of=GROUP Add user to group\n"
2217 " --capability-bounding-set=CAPS\n"
2218 " Bounding POSIX capability set\n"
2219 " --capability-ambient-set=CAPS\n"
2220 " Ambient POSIX capability set\n"
2221 " --skel=PATH Skeleton directory to use\n"
2222 " --shell=PATH Shell for account\n"
2223 " --setenv=VARIABLE[=VALUE] Set an environment variable at log-in\n"
2224 " --timezone=TIMEZONE Set a time-zone\n"
2225 " --language=LOCALE Set preferred language\n"
2226 " --ssh-authorized-keys=KEYS\n"
2227 " Specify SSH public keys\n"
2228 " --pkcs11-token-uri=URI URI to PKCS#11 security token containing\n"
2229 " private key and matching X.509 certificate\n"
2230 " --fido2-device=PATH Path to FIDO2 hidraw device with hmac-secret\n"
2232 " --fido2-with-client-pin=BOOL\n"
2233 " Whether to require entering a PIN to unlock the\n"
2235 " --fido2-with-user-presence=BOOL\n"
2236 " Whether to require user presence to unlock the\n"
2238 " --fido2-with-user-verification=BOOL\n"
2239 " Whether to require user verification to unlock\n"
2241 " --recovery-key=BOOL Add a recovery key\n"
2242 "\n%4$sAccount Management User Record Properties:%5$s\n"
2243 " --locked=BOOL Set locked account state\n"
2244 " --not-before=TIMESTAMP Do not allow logins before\n"
2245 " --not-after=TIMESTAMP Do not allow logins after\n"
2246 " --rate-limit-interval=SECS\n"
2247 " Login rate-limit interval in seconds\n"
2248 " --rate-limit-burst=NUMBER\n"
2249 " Login rate-limit attempts per interval\n"
2250 "\n%4$sPassword Policy User Record Properties:%5$s\n"
2251 " --password-hint=HINT Set Password hint\n"
2252 " --enforce-password-policy=BOOL\n"
2253 " Control whether to enforce system's password\n"
2254 " policy for this user\n"
2255 " -P Same as --enforce-password-password=no\n"
2256 " --password-change-now=BOOL\n"
2257 " Require the password to be changed on next login\n"
2258 " --password-change-min=TIME\n"
2259 " Require minimum time between password changes\n"
2260 " --password-change-max=TIME\n"
2261 " Require maximum time between password changes\n"
2262 " --password-change-warn=TIME\n"
2263 " How much time to warn before password expiry\n"
2264 " --password-change-inactive=TIME\n"
2265 " How much time to block password after expiry\n"
2266 "\n%4$sResource Management User Record Properties:%5$s\n"
2267 " --disk-size=BYTES Size to assign the user on disk\n"
2268 " --access-mode=MODE User home directory access mode\n"
2269 " --umask=MODE Umask for user when logging in\n"
2270 " --nice=NICE Nice level for user\n"
2271 " --rlimit=LIMIT=VALUE[:VALUE]\n"
2272 " Set resource limits\n"
2273 " --tasks-max=MAX Set maximum number of per-user tasks\n"
2274 " --memory-high=BYTES Set high memory threshold in bytes\n"
2275 " --memory-max=BYTES Set maximum memory limit\n"
2276 " --cpu-weight=WEIGHT Set CPU weight\n"
2277 " --io-weight=WEIGHT Set IO weight\n"
2278 "\n%4$sStorage User Record Properties:%5$s\n"
2279 " --storage=STORAGE Storage type to use (luks, fscrypt, directory,\n"
2280 " subvolume, cifs)\n"
2281 " --image-path=PATH Path to image file/directory\n"
2282 " --drop-caches=BOOL Whether to automatically drop caches on logout\n"
2283 "\n%4$sLUKS Storage User Record Properties:%5$s\n"
2284 " --fs-type=TYPE File system type to use in case of luks\n"
2285 " storage (btrfs, ext4, xfs)\n"
2286 " --luks-discard=BOOL Whether to use 'discard' feature of file system\n"
2287 " when activated (mounted)\n"
2288 " --luks-offline-discard=BOOL\n"
2289 " Whether to trim file on logout\n"
2290 " --luks-cipher=CIPHER Cipher to use for LUKS encryption\n"
2291 " --luks-cipher-mode=MODE Cipher mode to use for LUKS encryption\n"
2292 " --luks-volume-key-size=BITS\n"
2293 " Volume key size to use for LUKS encryption\n"
2294 " --luks-pbkdf-type=TYPE Password-based Key Derivation Function to use\n"
2295 " --luks-pbkdf-hash-algorithm=ALGORITHM\n"
2296 " PBKDF hash algorithm to use\n"
2297 " --luks-pbkdf-time-cost=SECS\n"
2298 " Time cost for PBKDF in seconds\n"
2299 " --luks-pbkdf-memory-cost=BYTES\n"
2300 " Memory cost for PBKDF in bytes\n"
2301 " --luks-pbkdf-parallel-threads=NUMBER\n"
2302 " Number of parallel threads for PKBDF\n"
2303 " --luks-sector-size=BYTES\n"
2304 " Sector size for LUKS encryption in bytes\n"
2305 " --luks-extra-mount-options=OPTIONS\n"
2306 " LUKS extra mount options\n"
2307 " --auto-resize-mode=MODE Automatically grow/shrink home on login/logout\n"
2308 " --rebalance-weight=WEIGHT Weight while rebalancing\n"
2309 "\n%4$sMounting User Record Properties:%5$s\n"
2310 " --nosuid=BOOL Control the 'nosuid' flag of the home mount\n"
2311 " --nodev=BOOL Control the 'nodev' flag of the home mount\n"
2312 " --noexec=BOOL Control the 'noexec' flag of the home mount\n"
2313 "\n%4$sCIFS User Record Properties:%5$s\n"
2314 " --cifs-domain=DOMAIN CIFS (Windows) domain\n"
2315 " --cifs-user-name=USER CIFS (Windows) user name\n"
2316 " --cifs-service=SERVICE CIFS (Windows) service to mount as home area\n"
2317 " --cifs-extra-mount-options=OPTIONS\n"
2318 " CIFS (Windows) extra mount options\n"
2319 "\n%4$sLogin Behaviour User Record Properties:%5$s\n"
2320 " --stop-delay=SECS How long to leave user services running after\n"
2322 " --kill-processes=BOOL Whether to kill user processes when sessions\n"
2324 " --auto-login=BOOL Try to log this user in automatically\n"
2325 "\nSee the %6$s for details.\n",
2326 program_invocation_short_name
,
2336 static int parse_argv(int argc
, char *argv
[]) {
2339 ARG_VERSION
= 0x100,
2342 ARG_NO_ASK_PASSWORD
,
2352 ARG_LUKS_OFFLINE_DISCARD
,
2358 ARG_SSH_AUTHORIZED_KEYS
,
2367 ARG_LUKS_CIPHER_MODE
,
2368 ARG_LUKS_VOLUME_KEY_SIZE
,
2375 ARG_CIFS_EXTRA_MOUNT_OPTIONS
,
2381 ARG_LUKS_PBKDF_TYPE
,
2382 ARG_LUKS_PBKDF_HASH_ALGORITHM
,
2383 ARG_LUKS_PBKDF_FORCE_ITERATIONS
,
2384 ARG_LUKS_PBKDF_TIME_COST
,
2385 ARG_LUKS_PBKDF_MEMORY_COST
,
2386 ARG_LUKS_PBKDF_PARALLEL_THREADS
,
2387 ARG_LUKS_SECTOR_SIZE
,
2388 ARG_RATE_LIMIT_INTERVAL
,
2389 ARG_RATE_LIMIT_BURST
,
2392 ARG_ENFORCE_PASSWORD_POLICY
,
2393 ARG_PASSWORD_CHANGE_NOW
,
2394 ARG_PASSWORD_CHANGE_MIN
,
2395 ARG_PASSWORD_CHANGE_MAX
,
2396 ARG_PASSWORD_CHANGE_WARN
,
2397 ARG_PASSWORD_CHANGE_INACTIVE
,
2400 ARG_PKCS11_TOKEN_URI
,
2407 ARG_AND_CHANGE_PASSWORD
,
2409 ARG_LUKS_EXTRA_MOUNT_OPTIONS
,
2410 ARG_AUTO_RESIZE_MODE
,
2411 ARG_REBALANCE_WEIGHT
,
2413 ARG_CAPABILITY_BOUNDING_SET
,
2414 ARG_CAPABILITY_AMBIENT_SET
,
2417 static const struct option options
[] = {
2418 { "help", no_argument
, NULL
, 'h' },
2419 { "version", no_argument
, NULL
, ARG_VERSION
},
2420 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
2421 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
2422 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
2423 { "host", required_argument
, NULL
, 'H' },
2424 { "machine", required_argument
, NULL
, 'M' },
2425 { "identity", required_argument
, NULL
, 'I' },
2426 { "real-name", required_argument
, NULL
, 'c' },
2427 { "comment", required_argument
, NULL
, 'c' }, /* Compat alias to keep thing in sync with useradd(8) */
2428 { "realm", required_argument
, NULL
, ARG_REALM
},
2429 { "email-address", required_argument
, NULL
, ARG_EMAIL_ADDRESS
},
2430 { "location", required_argument
, NULL
, ARG_LOCATION
},
2431 { "password-hint", required_argument
, NULL
, ARG_PASSWORD_HINT
},
2432 { "icon-name", required_argument
, NULL
, ARG_ICON_NAME
},
2433 { "home-dir", required_argument
, NULL
, 'd' }, /* Compatible with useradd(8) */
2434 { "uid", required_argument
, NULL
, 'u' }, /* Compatible with useradd(8) */
2435 { "member-of", required_argument
, NULL
, 'G' },
2436 { "groups", required_argument
, NULL
, 'G' }, /* Compat alias to keep thing in sync with useradd(8) */
2437 { "skel", required_argument
, NULL
, 'k' }, /* Compatible with useradd(8) */
2438 { "shell", required_argument
, NULL
, 's' }, /* Compatible with useradd(8) */
2439 { "setenv", required_argument
, NULL
, ARG_SETENV
},
2440 { "timezone", required_argument
, NULL
, ARG_TIMEZONE
},
2441 { "language", required_argument
, NULL
, ARG_LANGUAGE
},
2442 { "locked", required_argument
, NULL
, ARG_LOCKED
},
2443 { "not-before", required_argument
, NULL
, ARG_NOT_BEFORE
},
2444 { "not-after", required_argument
, NULL
, ARG_NOT_AFTER
},
2445 { "expiredate", required_argument
, NULL
, 'e' }, /* Compat alias to keep thing in sync with useradd(8) */
2446 { "ssh-authorized-keys", required_argument
, NULL
, ARG_SSH_AUTHORIZED_KEYS
},
2447 { "disk-size", required_argument
, NULL
, ARG_DISK_SIZE
},
2448 { "access-mode", required_argument
, NULL
, ARG_ACCESS_MODE
},
2449 { "umask", required_argument
, NULL
, ARG_UMASK
},
2450 { "nice", required_argument
, NULL
, ARG_NICE
},
2451 { "rlimit", required_argument
, NULL
, ARG_RLIMIT
},
2452 { "tasks-max", required_argument
, NULL
, ARG_TASKS_MAX
},
2453 { "memory-high", required_argument
, NULL
, ARG_MEMORY_HIGH
},
2454 { "memory-max", required_argument
, NULL
, ARG_MEMORY_MAX
},
2455 { "cpu-weight", required_argument
, NULL
, ARG_CPU_WEIGHT
},
2456 { "io-weight", required_argument
, NULL
, ARG_IO_WEIGHT
},
2457 { "storage", required_argument
, NULL
, ARG_STORAGE
},
2458 { "image-path", required_argument
, NULL
, ARG_IMAGE_PATH
},
2459 { "fs-type", required_argument
, NULL
, ARG_FS_TYPE
},
2460 { "luks-discard", required_argument
, NULL
, ARG_LUKS_DISCARD
},
2461 { "luks-offline-discard", required_argument
, NULL
, ARG_LUKS_OFFLINE_DISCARD
},
2462 { "luks-cipher", required_argument
, NULL
, ARG_LUKS_CIPHER
},
2463 { "luks-cipher-mode", required_argument
, NULL
, ARG_LUKS_CIPHER_MODE
},
2464 { "luks-volume-key-size", required_argument
, NULL
, ARG_LUKS_VOLUME_KEY_SIZE
},
2465 { "luks-pbkdf-type", required_argument
, NULL
, ARG_LUKS_PBKDF_TYPE
},
2466 { "luks-pbkdf-hash-algorithm", required_argument
, NULL
, ARG_LUKS_PBKDF_HASH_ALGORITHM
},
2467 { "luks-pbkdf-force-iterations", required_argument
, NULL
, ARG_LUKS_PBKDF_FORCE_ITERATIONS
},
2468 { "luks-pbkdf-time-cost", required_argument
, NULL
, ARG_LUKS_PBKDF_TIME_COST
},
2469 { "luks-pbkdf-memory-cost", required_argument
, NULL
, ARG_LUKS_PBKDF_MEMORY_COST
},
2470 { "luks-pbkdf-parallel-threads", required_argument
, NULL
, ARG_LUKS_PBKDF_PARALLEL_THREADS
},
2471 { "luks-sector-size", required_argument
, NULL
, ARG_LUKS_SECTOR_SIZE
},
2472 { "nosuid", required_argument
, NULL
, ARG_NOSUID
},
2473 { "nodev", required_argument
, NULL
, ARG_NODEV
},
2474 { "noexec", required_argument
, NULL
, ARG_NOEXEC
},
2475 { "cifs-user-name", required_argument
, NULL
, ARG_CIFS_USER_NAME
},
2476 { "cifs-domain", required_argument
, NULL
, ARG_CIFS_DOMAIN
},
2477 { "cifs-service", required_argument
, NULL
, ARG_CIFS_SERVICE
},
2478 { "cifs-extra-mount-options", required_argument
, NULL
, ARG_CIFS_EXTRA_MOUNT_OPTIONS
},
2479 { "rate-limit-interval", required_argument
, NULL
, ARG_RATE_LIMIT_INTERVAL
},
2480 { "rate-limit-burst", required_argument
, NULL
, ARG_RATE_LIMIT_BURST
},
2481 { "stop-delay", required_argument
, NULL
, ARG_STOP_DELAY
},
2482 { "kill-processes", required_argument
, NULL
, ARG_KILL_PROCESSES
},
2483 { "enforce-password-policy", required_argument
, NULL
, ARG_ENFORCE_PASSWORD_POLICY
},
2484 { "password-change-now", required_argument
, NULL
, ARG_PASSWORD_CHANGE_NOW
},
2485 { "password-change-min", required_argument
, NULL
, ARG_PASSWORD_CHANGE_MIN
},
2486 { "password-change-max", required_argument
, NULL
, ARG_PASSWORD_CHANGE_MAX
},
2487 { "password-change-warn", required_argument
, NULL
, ARG_PASSWORD_CHANGE_WARN
},
2488 { "password-change-inactive", required_argument
, NULL
, ARG_PASSWORD_CHANGE_INACTIVE
},
2489 { "auto-login", required_argument
, NULL
, ARG_AUTO_LOGIN
},
2490 { "json", required_argument
, NULL
, ARG_JSON
},
2491 { "export-format", required_argument
, NULL
, ARG_EXPORT_FORMAT
},
2492 { "pkcs11-token-uri", required_argument
, NULL
, ARG_PKCS11_TOKEN_URI
},
2493 { "fido2-credential-algorithm", required_argument
, NULL
, ARG_FIDO2_CRED_ALG
},
2494 { "fido2-device", required_argument
, NULL
, ARG_FIDO2_DEVICE
},
2495 { "fido2-with-client-pin", required_argument
, NULL
, ARG_FIDO2_WITH_PIN
},
2496 { "fido2-with-user-presence", required_argument
, NULL
, ARG_FIDO2_WITH_UP
},
2497 { "fido2-with-user-verification",required_argument
, NULL
, ARG_FIDO2_WITH_UV
},
2498 { "recovery-key", required_argument
, NULL
, ARG_RECOVERY_KEY
},
2499 { "and-resize", required_argument
, NULL
, ARG_AND_RESIZE
},
2500 { "and-change-password", required_argument
, NULL
, ARG_AND_CHANGE_PASSWORD
},
2501 { "drop-caches", required_argument
, NULL
, ARG_DROP_CACHES
},
2502 { "luks-extra-mount-options", required_argument
, NULL
, ARG_LUKS_EXTRA_MOUNT_OPTIONS
},
2503 { "auto-resize-mode", required_argument
, NULL
, ARG_AUTO_RESIZE_MODE
},
2504 { "rebalance-weight", required_argument
, NULL
, ARG_REBALANCE_WEIGHT
},
2505 { "capability-bounding-set", required_argument
, NULL
, ARG_CAPABILITY_BOUNDING_SET
},
2506 { "capability-ambient-set", required_argument
, NULL
, ARG_CAPABILITY_AMBIENT_SET
},
2518 c
= getopt_long(argc
, argv
, "hH:M:I:c:d:u:k:s:e:G:jPE", options
, NULL
);
2525 return help(0, NULL
, NULL
);
2531 arg_pager_flags
|= PAGER_DISABLE
;
2538 case ARG_NO_ASK_PASSWORD
:
2539 arg_ask_password
= false;
2543 arg_transport
= BUS_TRANSPORT_REMOTE
;
2548 arg_transport
= BUS_TRANSPORT_MACHINE
;
2553 arg_identity
= optarg
;
2557 if (isempty(optarg
)) {
2558 r
= drop_from_identity("realName");
2565 if (!valid_gecos(optarg
))
2566 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Real name '%s' not a valid GECOS field.", optarg
);
2568 r
= json_variant_set_field_string(&arg_identity_extra
, "realName", optarg
);
2570 return log_error_errno(r
, "Failed to set realName field: %m");
2575 _cleanup_free_
char *hd
= NULL
;
2577 if (isempty(optarg
)) {
2578 r
= drop_from_identity("homeDirectory");
2585 r
= parse_path_argument(optarg
, false, &hd
);
2589 if (!valid_home(hd
))
2590 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Home directory '%s' not valid.", hd
);
2592 r
= json_variant_set_field_string(&arg_identity_extra
, "homeDirectory", hd
);
2594 return log_error_errno(r
, "Failed to set homeDirectory field: %m");
2600 if (isempty(optarg
)) {
2601 r
= drop_from_identity("realm");
2608 r
= dns_name_is_valid(optarg
);
2610 return log_error_errno(r
, "Failed to determine whether realm '%s' is a valid DNS domain: %m", optarg
);
2612 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Realm '%s' is not a valid DNS domain: %m", optarg
);
2614 r
= json_variant_set_field_string(&arg_identity_extra
, "realm", optarg
);
2616 return log_error_errno(r
, "Failed to set realm field: %m");
2619 case ARG_EMAIL_ADDRESS
:
2622 case ARG_CIFS_USER_NAME
:
2623 case ARG_CIFS_DOMAIN
:
2624 case ARG_CIFS_EXTRA_MOUNT_OPTIONS
:
2625 case ARG_LUKS_EXTRA_MOUNT_OPTIONS
: {
2628 c
== ARG_EMAIL_ADDRESS
? "emailAddress" :
2629 c
== ARG_LOCATION
? "location" :
2630 c
== ARG_ICON_NAME
? "iconName" :
2631 c
== ARG_CIFS_USER_NAME
? "cifsUserName" :
2632 c
== ARG_CIFS_DOMAIN
? "cifsDomain" :
2633 c
== ARG_CIFS_EXTRA_MOUNT_OPTIONS
? "cifsExtraMountOptions" :
2634 c
== ARG_LUKS_EXTRA_MOUNT_OPTIONS
? "luksExtraMountOptions" :
2639 if (isempty(optarg
)) {
2640 r
= drop_from_identity(field
);
2647 r
= json_variant_set_field_string(&arg_identity_extra
, field
, optarg
);
2649 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2654 case ARG_CIFS_SERVICE
:
2655 if (isempty(optarg
)) {
2656 r
= drop_from_identity("cifsService");
2663 r
= parse_cifs_service(optarg
, NULL
, NULL
, NULL
);
2665 return log_error_errno(r
, "Failed to validate CIFS service name: %s", optarg
);
2667 r
= json_variant_set_field_string(&arg_identity_extra
, "cifsService", optarg
);
2669 return log_error_errno(r
, "Failed to set cifsService field: %m");
2673 case ARG_PASSWORD_HINT
:
2674 if (isempty(optarg
)) {
2675 r
= drop_from_identity("passwordHint");
2682 r
= json_variant_set_field_string(&arg_identity_extra_privileged
, "passwordHint", optarg
);
2684 return log_error_errno(r
, "Failed to set passwordHint field: %m");
2686 string_erase(optarg
);
2692 if (isempty(optarg
)) {
2693 r
= drop_from_identity("niceLevel");
2699 r
= parse_nice(optarg
, &nc
);
2701 return log_error_errno(r
, "Failed to parse nice level: %s", optarg
);
2703 r
= json_variant_set_field_integer(&arg_identity_extra
, "niceLevel", nc
);
2705 return log_error_errno(r
, "Failed to set niceLevel field: %m");
2711 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
, *jcur
= NULL
, *jmax
= NULL
;
2712 _cleanup_free_
char *field
= NULL
, *t
= NULL
;
2717 if (isempty(optarg
)) {
2718 /* Remove all resource limits */
2720 r
= drop_from_identity("resourceLimits");
2724 arg_identity_filter_rlimits
= strv_free(arg_identity_filter_rlimits
);
2725 arg_identity_extra_rlimits
= json_variant_unref(arg_identity_extra_rlimits
);
2729 eq
= strchr(optarg
, '=');
2731 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Can't parse resource limit assignment: %s", optarg
);
2733 field
= strndup(optarg
, eq
- optarg
);
2737 l
= rlimit_from_string_harder(field
);
2739 return log_error_errno(l
, "Unknown resource limit type: %s", field
);
2741 if (isempty(eq
+ 1)) {
2742 /* Remove only the specific rlimit */
2744 r
= strv_extend(&arg_identity_filter_rlimits
, rlimit_to_string(l
));
2748 r
= json_variant_filter(&arg_identity_extra_rlimits
, STRV_MAKE(field
));
2750 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
2755 r
= rlimit_parse(l
, eq
+ 1, &rl
);
2757 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to parse resource limit value: %s", eq
+ 1);
2759 r
= rl
.rlim_cur
== RLIM_INFINITY
? json_variant_new_null(&jcur
) : json_variant_new_unsigned(&jcur
, rl
.rlim_cur
);
2761 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to allocate current integer: %m");
2763 r
= rl
.rlim_max
== RLIM_INFINITY
? json_variant_new_null(&jmax
) : json_variant_new_unsigned(&jmax
, rl
.rlim_max
);
2765 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to allocate maximum integer: %m");
2769 JSON_BUILD_PAIR("cur", JSON_BUILD_VARIANT(jcur
)),
2770 JSON_BUILD_PAIR("max", JSON_BUILD_VARIANT(jmax
))));
2772 return log_error_errno(r
, "Failed to build resource limit: %m");
2774 t
= strjoin("RLIMIT_", rlimit_to_string(l
));
2778 r
= json_variant_set_field(&arg_identity_extra_rlimits
, t
, v
);
2780 return log_error_errno(r
, "Failed to set %s field: %m", rlimit_to_string(l
));
2788 if (isempty(optarg
)) {
2789 r
= drop_from_identity("uid");
2796 r
= parse_uid(optarg
, &uid
);
2798 return log_error_errno(r
, "Failed to parse UID '%s'.", optarg
);
2800 if (uid_is_system(uid
))
2801 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "UID " UID_FMT
" is in system range, refusing.", uid
);
2802 if (uid_is_dynamic(uid
))
2803 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "UID " UID_FMT
" is in dynamic range, refusing.", uid
);
2804 if (uid
== UID_NOBODY
)
2805 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "UID " UID_FMT
" is nobody UID, refusing.", uid
);
2807 r
= json_variant_set_field_unsigned(&arg_identity_extra
, "uid", uid
);
2809 return log_error_errno(r
, "Failed to set realm field: %m");
2815 case ARG_IMAGE_PATH
: {
2816 const char *field
= c
== 'k' ? "skeletonDirectory" : "imagePath";
2817 _cleanup_free_
char *v
= NULL
;
2819 if (isempty(optarg
)) {
2820 r
= drop_from_identity(field
);
2827 r
= parse_path_argument(optarg
, false, &v
);
2831 r
= json_variant_set_field_string(&arg_identity_extra_this_machine
, field
, v
);
2833 return log_error_errno(r
, "Failed to set %s field: %m", v
);
2839 if (isempty(optarg
)) {
2840 r
= drop_from_identity("shell");
2847 if (!valid_shell(optarg
))
2848 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Shell '%s' not valid.", optarg
);
2850 r
= json_variant_set_field_string(&arg_identity_extra
, "shell", optarg
);
2852 return log_error_errno(r
, "Failed to set shell field: %m");
2857 _cleanup_free_
char **l
= NULL
;
2858 _cleanup_(json_variant_unrefp
) JsonVariant
*ne
= NULL
;
2861 if (isempty(optarg
)) {
2862 r
= drop_from_identity("environment");
2869 e
= json_variant_by_key(arg_identity_extra
, "environment");
2871 r
= json_variant_strv(e
, &l
);
2873 return log_error_errno(r
, "Failed to parse JSON environment field: %m");
2876 r
= strv_env_replace_strdup_passthrough(&l
, optarg
);
2878 return log_error_errno(r
, "Cannot assign environment variable %s: %m", optarg
);
2882 r
= json_variant_new_array_strv(&ne
, l
);
2884 return log_error_errno(r
, "Failed to allocate environment list JSON: %m");
2886 r
= json_variant_set_field(&arg_identity_extra
, "environment", ne
);
2888 return log_error_errno(r
, "Failed to set environment list: %m");
2895 if (isempty(optarg
)) {
2896 r
= drop_from_identity("timeZone");
2903 if (!timezone_is_valid(optarg
, LOG_DEBUG
))
2904 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Timezone '%s' is not valid.", optarg
);
2906 r
= json_variant_set_field_string(&arg_identity_extra
, "timeZone", optarg
);
2908 return log_error_errno(r
, "Failed to set timezone field: %m");
2913 if (isempty(optarg
)) {
2914 r
= drop_from_identity("language");
2921 if (!locale_is_valid(optarg
))
2922 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Locale '%s' is not valid.", optarg
);
2924 if (locale_is_installed(optarg
) <= 0)
2925 log_warning("Locale '%s' is not installed, accepting anyway.", optarg
);
2927 r
= json_variant_set_field_string(&arg_identity_extra
, "preferredLanguage", optarg
);
2929 return log_error_errno(r
, "Failed to set preferredLanguage field: %m");
2937 case ARG_KILL_PROCESSES
:
2938 case ARG_ENFORCE_PASSWORD_POLICY
:
2939 case ARG_AUTO_LOGIN
:
2940 case ARG_PASSWORD_CHANGE_NOW
: {
2942 c
== ARG_LOCKED
? "locked" :
2943 c
== ARG_NOSUID
? "mountNoSuid" :
2944 c
== ARG_NODEV
? "mountNoDevices" :
2945 c
== ARG_NOEXEC
? "mountNoExecute" :
2946 c
== ARG_KILL_PROCESSES
? "killProcesses" :
2947 c
== ARG_ENFORCE_PASSWORD_POLICY
? "enforcePasswordPolicy" :
2948 c
== ARG_AUTO_LOGIN
? "autoLogin" :
2949 c
== ARG_PASSWORD_CHANGE_NOW
? "passwordChangeNow" :
2954 if (isempty(optarg
)) {
2955 r
= drop_from_identity(field
);
2962 r
= parse_boolean(optarg
);
2964 return log_error_errno(r
, "Failed to parse %s boolean: %m", field
);
2966 r
= json_variant_set_field_boolean(&arg_identity_extra
, field
, r
> 0);
2968 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2974 r
= json_variant_set_field_boolean(&arg_identity_extra
, "enforcePasswordPolicy", false);
2976 return log_error_errno(r
, "Failed to set enforcePasswordPolicy field: %m");
2981 if (isempty(optarg
)) {
2982 FOREACH_STRING(prop
, "diskSize", "diskSizeRelative", "rebalanceWeight") {
2983 r
= drop_from_identity(prop
);
2988 arg_disk_size
= arg_disk_size_relative
= UINT64_MAX
;
2992 r
= parse_permyriad(optarg
);
2994 r
= parse_disk_size(optarg
, &arg_disk_size
);
2998 r
= drop_from_identity("diskSizeRelative");
3002 r
= json_variant_set_field_unsigned(&arg_identity_extra_this_machine
, "diskSize", arg_disk_size
);
3004 return log_error_errno(r
, "Failed to set diskSize field: %m");
3006 arg_disk_size_relative
= UINT64_MAX
;
3008 /* Normalize to UINT32_MAX == 100% */
3009 arg_disk_size_relative
= UINT32_SCALE_FROM_PERMYRIAD(r
);
3011 r
= drop_from_identity("diskSize");
3015 r
= json_variant_set_field_unsigned(&arg_identity_extra_this_machine
, "diskSizeRelative", arg_disk_size_relative
);
3017 return log_error_errno(r
, "Failed to set diskSizeRelative field: %m");
3019 arg_disk_size
= UINT64_MAX
;
3022 /* Automatically turn off the rebalance logic if user configured a size explicitly */
3023 r
= json_variant_set_field_unsigned(&arg_identity_extra_this_machine
, "rebalanceWeight", REBALANCE_WEIGHT_OFF
);
3025 return log_error_errno(r
, "Failed to set rebalanceWeight field: %m");
3029 case ARG_ACCESS_MODE
: {
3032 if (isempty(optarg
)) {
3033 r
= drop_from_identity("accessMode");
3040 r
= parse_mode(optarg
, &mode
);
3042 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Access mode '%s' not valid.", optarg
);
3044 r
= json_variant_set_field_unsigned(&arg_identity_extra
, "accessMode", mode
);
3046 return log_error_errno(r
, "Failed to set access mode field: %m");
3051 case ARG_LUKS_DISCARD
:
3052 if (isempty(optarg
)) {
3053 r
= drop_from_identity("luksDiscard");
3060 r
= parse_boolean(optarg
);
3062 return log_error_errno(r
, "Failed to parse --luks-discard= parameter: %s", optarg
);
3064 r
= json_variant_set_field_boolean(&arg_identity_extra
, "luksDiscard", r
);
3066 return log_error_errno(r
, "Failed to set discard field: %m");
3070 case ARG_LUKS_OFFLINE_DISCARD
:
3071 if (isempty(optarg
)) {
3072 r
= drop_from_identity("luksOfflineDiscard");
3079 r
= parse_boolean(optarg
);
3081 return log_error_errno(r
, "Failed to parse --luks-offline-discard= parameter: %s", optarg
);
3083 r
= json_variant_set_field_boolean(&arg_identity_extra
, "luksOfflineDiscard", r
);
3085 return log_error_errno(r
, "Failed to set offline discard field: %m");
3089 case ARG_LUKS_VOLUME_KEY_SIZE
:
3090 case ARG_LUKS_PBKDF_FORCE_ITERATIONS
:
3091 case ARG_LUKS_PBKDF_PARALLEL_THREADS
:
3092 case ARG_RATE_LIMIT_BURST
: {
3094 c
== ARG_LUKS_VOLUME_KEY_SIZE
? "luksVolumeKeySize" :
3095 c
== ARG_LUKS_PBKDF_FORCE_ITERATIONS
? "luksPbkdfForceIterations" :
3096 c
== ARG_LUKS_PBKDF_PARALLEL_THREADS
? "luksPbkdfParallelThreads" :
3097 c
== ARG_RATE_LIMIT_BURST
? "rateLimitBurst" : NULL
;
3102 if (isempty(optarg
)) {
3103 r
= drop_from_identity(field
);
3108 r
= safe_atou(optarg
, &n
);
3110 return log_error_errno(r
, "Failed to parse %s parameter: %s", field
, optarg
);
3112 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, n
);
3114 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3119 case ARG_LUKS_SECTOR_SIZE
: {
3122 if (isempty(optarg
)) {
3123 r
= drop_from_identity("luksSectorSize");
3130 r
= parse_sector_size(optarg
, &ss
);
3134 r
= json_variant_set_field_unsigned(&arg_identity_extra
, "luksSectorSize", ss
);
3136 return log_error_errno(r
, "Failed to set sector size field: %m");
3144 if (isempty(optarg
)) {
3145 r
= drop_from_identity("umask");
3152 r
= parse_mode(optarg
, &m
);
3154 return log_error_errno(r
, "Failed to parse umask: %m");
3156 r
= json_variant_set_field_integer(&arg_identity_extra
, "umask", m
);
3158 return log_error_errno(r
, "Failed to set umask field: %m");
3163 case ARG_SSH_AUTHORIZED_KEYS
: {
3164 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
3165 _cleanup_(strv_freep
) char **l
= NULL
, **add
= NULL
;
3167 if (isempty(optarg
)) {
3168 r
= drop_from_identity("sshAuthorizedKeys");
3175 if (optarg
[0] == '@') {
3176 _cleanup_fclose_
FILE *f
= NULL
;
3178 /* If prefixed with '@' read from a file */
3180 f
= fopen(optarg
+1, "re");
3182 return log_error_errno(errno
, "Failed to open '%s': %m", optarg
+1);
3185 _cleanup_free_
char *line
= NULL
;
3187 r
= read_line(f
, LONG_LINE_MAX
, &line
);
3189 return log_error_errno(r
, "Failed to read from '%s': %m", optarg
+1);
3199 r
= strv_consume(&add
, TAKE_PTR(line
));
3204 /* Otherwise, assume it's a literal key. Let's do some superficial checks
3205 * before accept it though. */
3207 if (string_has_cc(optarg
, NULL
))
3208 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Authorized key contains control characters, refusing.");
3209 if (optarg
[0] == '#')
3210 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Specified key is a comment?");
3212 add
= strv_new(optarg
);
3217 v
= json_variant_ref(json_variant_by_key(arg_identity_extra_privileged
, "sshAuthorizedKeys"));
3219 r
= json_variant_strv(v
, &l
);
3221 return log_error_errno(r
, "Failed to parse SSH authorized keys list: %m");
3224 r
= strv_extend_strv(&l
, add
, true);
3228 v
= json_variant_unref(v
);
3230 r
= json_variant_new_array_strv(&v
, l
);
3234 r
= json_variant_set_field(&arg_identity_extra_privileged
, "sshAuthorizedKeys", v
);
3236 return log_error_errno(r
, "Failed to set authorized keys: %m");
3241 case ARG_NOT_BEFORE
:
3247 field
= c
== ARG_NOT_BEFORE
? "notBeforeUSec" :
3248 IN_SET(c
, ARG_NOT_AFTER
, 'e') ? "notAfterUSec" : NULL
;
3252 if (isempty(optarg
)) {
3253 r
= drop_from_identity(field
);
3260 /* Note the minor discrepancy regarding -e parsing here: we support that for compat
3261 * reasons, and in the original useradd(8) implementation it accepts dates in the
3262 * format YYYY-MM-DD. Coincidentally, we accept dates formatted like that too, but
3263 * with greater precision. */
3264 r
= parse_timestamp(optarg
, &n
);
3266 return log_error_errno(r
, "Failed to parse %s parameter: %m", field
);
3268 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, n
);
3270 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3274 case ARG_PASSWORD_CHANGE_MIN
:
3275 case ARG_PASSWORD_CHANGE_MAX
:
3276 case ARG_PASSWORD_CHANGE_WARN
:
3277 case ARG_PASSWORD_CHANGE_INACTIVE
: {
3281 field
= c
== ARG_PASSWORD_CHANGE_MIN
? "passwordChangeMinUSec" :
3282 c
== ARG_PASSWORD_CHANGE_MAX
? "passwordChangeMaxUSec" :
3283 c
== ARG_PASSWORD_CHANGE_WARN
? "passwordChangeWarnUSec" :
3284 c
== ARG_PASSWORD_CHANGE_INACTIVE
? "passwordChangeInactiveUSec" :
3289 if (isempty(optarg
)) {
3290 r
= drop_from_identity(field
);
3297 r
= parse_sec(optarg
, &n
);
3299 return log_error_errno(r
, "Failed to parse %s parameter: %m", field
);
3301 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, n
);
3303 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3309 case ARG_LUKS_CIPHER
:
3310 case ARG_LUKS_CIPHER_MODE
:
3311 case ARG_LUKS_PBKDF_TYPE
:
3312 case ARG_LUKS_PBKDF_HASH_ALGORITHM
: {
3315 c
== ARG_STORAGE
? "storage" :
3316 c
== ARG_FS_TYPE
? "fileSystemType" :
3317 c
== ARG_LUKS_CIPHER
? "luksCipher" :
3318 c
== ARG_LUKS_CIPHER_MODE
? "luksCipherMode" :
3319 c
== ARG_LUKS_PBKDF_TYPE
? "luksPbkdfType" :
3320 c
== ARG_LUKS_PBKDF_HASH_ALGORITHM
? "luksPbkdfHashAlgorithm" : NULL
;
3324 if (isempty(optarg
)) {
3325 r
= drop_from_identity(field
);
3332 if (!string_is_safe(optarg
))
3333 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Parameter for %s field not valid: %s", field
, optarg
);
3335 r
= json_variant_set_field_string(
3336 IN_SET(c
, ARG_STORAGE
, ARG_FS_TYPE
) ?
3337 &arg_identity_extra_this_machine
:
3338 &arg_identity_extra
, field
, optarg
);
3340 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3345 case ARG_LUKS_PBKDF_TIME_COST
:
3346 case ARG_RATE_LIMIT_INTERVAL
:
3347 case ARG_STOP_DELAY
: {
3349 c
== ARG_LUKS_PBKDF_TIME_COST
? "luksPbkdfTimeCostUSec" :
3350 c
== ARG_RATE_LIMIT_INTERVAL
? "rateLimitIntervalUSec" :
3351 c
== ARG_STOP_DELAY
? "stopDelayUSec" :
3357 if (isempty(optarg
)) {
3358 r
= drop_from_identity(field
);
3365 r
= parse_sec(optarg
, &t
);
3367 return log_error_errno(r
, "Failed to parse %s field: %s", field
, optarg
);
3369 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, t
);
3371 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3377 const char *p
= optarg
;
3380 r
= drop_from_identity("memberOf");
3388 _cleanup_(json_variant_unrefp
) JsonVariant
*mo
= NULL
;
3389 _cleanup_strv_free_
char **list
= NULL
;
3390 _cleanup_free_
char *word
= NULL
;
3392 r
= extract_first_word(&p
, &word
, ",", 0);
3394 return log_error_errno(r
, "Failed to parse group list: %m");
3398 if (!valid_user_group_name(word
, 0))
3399 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Invalid group name %s.", word
);
3401 mo
= json_variant_ref(json_variant_by_key(arg_identity_extra
, "memberOf"));
3403 r
= json_variant_strv(mo
, &list
);
3405 return log_error_errno(r
, "Failed to parse group list: %m");
3407 r
= strv_extend(&list
, word
);
3414 mo
= json_variant_unref(mo
);
3415 r
= json_variant_new_array_strv(&mo
, list
);
3417 return log_error_errno(r
, "Failed to create group list JSON: %m");
3419 r
= json_variant_set_field(&arg_identity_extra
, "memberOf", mo
);
3421 return log_error_errno(r
, "Failed to update group list: %m");
3427 case ARG_TASKS_MAX
: {
3430 if (isempty(optarg
)) {
3431 r
= drop_from_identity("tasksMax");
3437 r
= safe_atou64(optarg
, &u
);
3439 return log_error_errno(r
, "Failed to parse --tasks-max= parameter: %s", optarg
);
3441 r
= json_variant_set_field_unsigned(&arg_identity_extra
, "tasksMax", u
);
3443 return log_error_errno(r
, "Failed to set tasksMax field: %m");
3448 case ARG_MEMORY_MAX
:
3449 case ARG_MEMORY_HIGH
:
3450 case ARG_LUKS_PBKDF_MEMORY_COST
: {
3452 c
== ARG_MEMORY_MAX
? "memoryMax" :
3453 c
== ARG_MEMORY_HIGH
? "memoryHigh" :
3454 c
== ARG_LUKS_PBKDF_MEMORY_COST
? "luksPbkdfMemoryCost" : NULL
;
3460 if (isempty(optarg
)) {
3461 r
= drop_from_identity(field
);
3467 r
= parse_size(optarg
, 1024, &u
);
3469 return log_error_errno(r
, "Failed to parse %s parameter: %s", field
, optarg
);
3471 r
= json_variant_set_field_unsigned(&arg_identity_extra_this_machine
, field
, u
);
3473 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3478 case ARG_CPU_WEIGHT
:
3479 case ARG_IO_WEIGHT
: {
3480 const char *field
= c
== ARG_CPU_WEIGHT
? "cpuWeight" :
3481 c
== ARG_IO_WEIGHT
? "ioWeight" : NULL
;
3486 if (isempty(optarg
)) {
3487 r
= drop_from_identity(field
);
3493 r
= safe_atou64(optarg
, &u
);
3495 return log_error_errno(r
, "Failed to parse --cpu-weight=/--io-weight= parameter: %s", optarg
);
3497 if (!CGROUP_WEIGHT_IS_OK(u
))
3498 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Weight %" PRIu64
" is out of valid weight range.", u
);
3500 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, u
);
3502 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3507 case ARG_PKCS11_TOKEN_URI
:
3508 if (streq(optarg
, "list"))
3509 return pkcs11_list_tokens();
3511 /* If --pkcs11-token-uri= is specified we always drop everything old */
3512 FOREACH_STRING(p
, "pkcs11TokenUri", "pkcs11EncryptedKey") {
3513 r
= drop_from_identity(p
);
3518 if (isempty(optarg
)) {
3519 arg_pkcs11_token_uri
= strv_free(arg_pkcs11_token_uri
);
3523 if (streq(optarg
, "auto")) {
3524 _cleanup_free_
char *found
= NULL
;
3526 r
= pkcs11_find_token_auto(&found
);
3529 r
= strv_consume(&arg_pkcs11_token_uri
, TAKE_PTR(found
));
3531 if (!pkcs11_uri_valid(optarg
))
3532 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Not a valid PKCS#11 URI: %s", optarg
);
3534 r
= strv_extend(&arg_pkcs11_token_uri
, optarg
);
3539 strv_uniq(arg_pkcs11_token_uri
);
3542 case ARG_FIDO2_CRED_ALG
:
3543 r
= parse_fido2_algorithm(optarg
, &arg_fido2_cred_alg
);
3545 return log_error_errno(r
, "Failed to parse COSE algorithm: %s", optarg
);
3548 case ARG_FIDO2_DEVICE
:
3549 if (streq(optarg
, "list"))
3550 return fido2_list_devices();
3552 FOREACH_STRING(p
, "fido2HmacCredential", "fido2HmacSalt") {
3553 r
= drop_from_identity(p
);
3558 if (isempty(optarg
)) {
3559 arg_fido2_device
= strv_free(arg_fido2_device
);
3563 if (streq(optarg
, "auto")) {
3564 _cleanup_free_
char *found
= NULL
;
3566 r
= fido2_find_device_auto(&found
);
3570 r
= strv_consume(&arg_fido2_device
, TAKE_PTR(found
));
3572 r
= strv_extend(&arg_fido2_device
, optarg
);
3576 strv_uniq(arg_fido2_device
);
3579 case ARG_FIDO2_WITH_PIN
:
3580 r
= parse_boolean_argument("--fido2-with-client-pin=", optarg
, NULL
);
3584 SET_FLAG(arg_fido2_lock_with
, FIDO2ENROLL_PIN
, r
);
3587 case ARG_FIDO2_WITH_UP
:
3588 r
= parse_boolean_argument("--fido2-with-user-presence=", optarg
, NULL
);
3592 SET_FLAG(arg_fido2_lock_with
, FIDO2ENROLL_UP
, r
);
3595 case ARG_FIDO2_WITH_UV
:
3596 r
= parse_boolean_argument("--fido2-with-user-verification=", optarg
, NULL
);
3600 SET_FLAG(arg_fido2_lock_with
, FIDO2ENROLL_UV
, r
);
3603 case ARG_RECOVERY_KEY
:
3604 r
= parse_boolean(optarg
);
3606 return log_error_errno(r
, "Failed to parse --recovery-key= argument: %s", optarg
);
3608 arg_recovery_key
= r
;
3610 FOREACH_STRING(p
, "recoveryKey", "recoveryKeyType") {
3611 r
= drop_from_identity(p
);
3618 case ARG_AUTO_RESIZE_MODE
:
3619 if (isempty(optarg
)) {
3620 r
= drop_from_identity("autoResizeMode");
3627 r
= auto_resize_mode_from_string(optarg
);
3629 return log_error_errno(r
, "Failed to parse --auto-resize-mode= argument: %s", optarg
);
3631 r
= json_variant_set_field_string(&arg_identity_extra
, "autoResizeMode", auto_resize_mode_to_string(r
));
3633 return log_error_errno(r
, "Failed to set autoResizeMode field: %m");
3637 case ARG_REBALANCE_WEIGHT
: {
3640 if (isempty(optarg
)) {
3641 r
= drop_from_identity("rebalanceWeight");
3647 if (streq(optarg
, "off"))
3648 u
= REBALANCE_WEIGHT_OFF
;
3650 r
= safe_atou64(optarg
, &u
);
3652 return log_error_errno(r
, "Failed to parse --rebalance-weight= argument: %s", optarg
);
3654 if (u
< REBALANCE_WEIGHT_MIN
|| u
> REBALANCE_WEIGHT_MAX
)
3655 return log_error_errno(SYNTHETIC_ERRNO(ERANGE
), "Rebalancing weight out of valid range %" PRIu64
"%s%" PRIu64
": %s",
3656 REBALANCE_WEIGHT_MIN
, special_glyph(SPECIAL_GLYPH_ELLIPSIS
), REBALANCE_WEIGHT_MAX
, optarg
);
3659 /* Drop from per machine stuff and everywhere */
3660 r
= drop_from_identity("rebalanceWeight");
3664 /* Add to main identity */
3665 r
= json_variant_set_field_unsigned(&arg_identity_extra
, "rebalanceWeight", u
);
3667 return log_error_errno(r
, "Failed to set rebalanceWeight field: %m");
3673 arg_json_format_flags
= JSON_FORMAT_PRETTY_AUTO
|JSON_FORMAT_COLOR_AUTO
;
3677 r
= parse_json_argument(optarg
, &arg_json_format_flags
);
3684 if (arg_export_format
== EXPORT_FORMAT_FULL
)
3685 arg_export_format
= EXPORT_FORMAT_STRIPPED
;
3686 else if (arg_export_format
== EXPORT_FORMAT_STRIPPED
)
3687 arg_export_format
= EXPORT_FORMAT_MINIMAL
;
3689 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Specifying -E more than twice is not supported.");
3691 arg_json_format_flags
&= ~JSON_FORMAT_OFF
;
3692 if (arg_json_format_flags
== 0)
3693 arg_json_format_flags
= JSON_FORMAT_PRETTY_AUTO
|JSON_FORMAT_COLOR_AUTO
;
3696 case ARG_EXPORT_FORMAT
:
3697 if (streq(optarg
, "full"))
3698 arg_export_format
= EXPORT_FORMAT_FULL
;
3699 else if (streq(optarg
, "stripped"))
3700 arg_export_format
= EXPORT_FORMAT_STRIPPED
;
3701 else if (streq(optarg
, "minimal"))
3702 arg_export_format
= EXPORT_FORMAT_MINIMAL
;
3703 else if (streq(optarg
, "help")) {
3712 case ARG_AND_RESIZE
:
3713 arg_and_resize
= true;
3716 case ARG_AND_CHANGE_PASSWORD
:
3717 arg_and_change_password
= true;
3720 case ARG_DROP_CACHES
: {
3721 if (isempty(optarg
)) {
3722 r
= drop_from_identity("dropCaches");
3728 r
= parse_boolean_argument("--drop-caches=", optarg
, NULL
);
3732 r
= json_variant_set_field_boolean(&arg_identity_extra
, "dropCaches", r
);
3734 return log_error_errno(r
, "Failed to set drop caches field: %m");
3739 case ARG_CAPABILITY_AMBIENT_SET
:
3740 case ARG_CAPABILITY_BOUNDING_SET
: {
3741 _cleanup_strv_free_
char **l
= NULL
;
3742 bool subtract
= false;
3743 uint64_t parsed
, *which
, updated
;
3744 const char *p
, *field
;
3746 if (c
== ARG_CAPABILITY_AMBIENT_SET
) {
3747 which
= &arg_capability_ambient_set
;
3748 field
= "capabilityAmbientSet";
3750 assert(c
== ARG_CAPABILITY_BOUNDING_SET
);
3751 which
= &arg_capability_bounding_set
;
3752 field
= "capabilityBoundingSet";
3755 if (isempty(optarg
)) {
3756 r
= drop_from_identity(field
);
3760 *which
= UINT64_MAX
;
3770 r
= capability_set_from_string(p
, &parsed
);
3772 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Invalid capabilities in capability string '%s'.", p
);
3774 return log_error_errno(r
, "Failed to parse capability string '%s': %m", p
);
3776 if (*which
== UINT64_MAX
)
3777 updated
= subtract
? all_capabilities() & ~parsed
: parsed
;
3779 updated
= *which
& ~parsed
;
3781 updated
= *which
| parsed
;
3783 if (capability_set_to_strv(updated
, &l
) < 0)
3786 r
= json_variant_set_field_strv(&arg_identity_extra
, field
, l
);
3788 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3798 assert_not_reached();
3802 if (!strv_isempty(arg_pkcs11_token_uri
) || !strv_isempty(arg_fido2_device
))
3803 arg_and_change_password
= true;
3805 if (arg_disk_size
!= UINT64_MAX
|| arg_disk_size_relative
!= UINT64_MAX
)
3806 arg_and_resize
= true;
3811 static int redirect_bus_mgr(void) {
3814 /* Talk to a different service if that's requested. (The same env var is also understood by homed, so
3815 * that it is relatively easily possible to invoke a second instance of homed for debug purposes and
3816 * have homectl talk to it, without colliding with the host version. This is handy when operating
3817 * from a homed-managed account.) */
3819 suffix
= getenv("SYSTEMD_HOME_DEBUG_SUFFIX");
3821 static BusLocator locator
= {
3822 .path
= "/org/freedesktop/home1",
3823 .interface
= "org.freedesktop.home1.Manager",
3826 /* Yes, we leak this memory, but there's little point to collect this, given that we only do
3827 * this in a debug environment, do it only once, and the string shall live for out entire
3828 * process runtime. */
3830 locator
.destination
= strjoin("org.freedesktop.home1.", suffix
);
3831 if (!locator
.destination
)
3836 bus_mgr
= bus_home_mgr
;
3841 static int run(int argc
, char *argv
[]) {
3842 static const Verb verbs
[] = {
3843 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
3844 { "list", VERB_ANY
, 1, VERB_DEFAULT
, list_homes
},
3845 { "activate", 2, VERB_ANY
, 0, activate_home
},
3846 { "deactivate", 2, VERB_ANY
, 0, deactivate_home
},
3847 { "inspect", VERB_ANY
, VERB_ANY
, 0, inspect_home
},
3848 { "authenticate", VERB_ANY
, VERB_ANY
, 0, authenticate_home
},
3849 { "create", VERB_ANY
, 2, 0, create_home
},
3850 { "remove", 2, VERB_ANY
, 0, remove_home
},
3851 { "update", VERB_ANY
, 2, 0, update_home
},
3852 { "passwd", VERB_ANY
, 2, 0, passwd_home
},
3853 { "resize", 2, 3, 0, resize_home
},
3854 { "lock", 2, VERB_ANY
, 0, lock_home
},
3855 { "unlock", 2, VERB_ANY
, 0, unlock_home
},
3856 { "with", 2, VERB_ANY
, 0, with_home
},
3857 { "lock-all", VERB_ANY
, 1, 0, lock_all_homes
},
3858 { "deactivate-all", VERB_ANY
, 1, 0, deactivate_all_homes
},
3859 { "rebalance", VERB_ANY
, 1, 0, rebalance
},
3867 r
= redirect_bus_mgr();
3871 r
= parse_argv(argc
, argv
);
3875 return dispatch_verb(argc
, argv
, verbs
, NULL
);
3878 DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run
);