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"
12 #include "cgroup-util.h"
13 #include "dns-domain.h"
17 #include "format-table.h"
19 #include "glyph-util.h"
20 #include "home-util.h"
21 #include "homectl-fido2.h"
22 #include "homectl-pkcs11.h"
23 #include "homectl-recovery-key.h"
24 #include "libfido2-util.h"
25 #include "locale-util.h"
26 #include "main-func.h"
27 #include "memory-util.h"
29 #include "parse-argument.h"
30 #include "parse-util.h"
31 #include "path-util.h"
32 #include "percent-util.h"
33 #include "pkcs11-util.h"
34 #include "pretty-print.h"
35 #include "process-util.h"
36 #include "pwquality-util.h"
37 #include "rlimit-util.h"
38 #include "spawn-polkit-agent.h"
39 #include "terminal-util.h"
40 #include "uid-alloc-range.h"
41 #include "user-record-pwquality.h"
42 #include "user-record-show.h"
43 #include "user-record-util.h"
44 #include "user-record.h"
45 #include "user-util.h"
48 static PagerFlags arg_pager_flags
= 0;
49 static bool arg_legend
= true;
50 static bool arg_ask_password
= true;
51 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
52 static const char *arg_host
= NULL
;
53 static const char *arg_identity
= NULL
;
54 static JsonVariant
*arg_identity_extra
= NULL
;
55 static JsonVariant
*arg_identity_extra_privileged
= NULL
;
56 static JsonVariant
*arg_identity_extra_this_machine
= NULL
;
57 static JsonVariant
*arg_identity_extra_rlimits
= NULL
;
58 static char **arg_identity_filter
= NULL
; /* this one is also applied to 'privileged' and 'thisMachine' subobjects */
59 static char **arg_identity_filter_rlimits
= NULL
;
60 static uint64_t arg_disk_size
= UINT64_MAX
;
61 static uint64_t arg_disk_size_relative
= UINT64_MAX
;
62 static char **arg_pkcs11_token_uri
= NULL
;
63 static char **arg_fido2_device
= NULL
;
64 static Fido2EnrollFlags arg_fido2_lock_with
= FIDO2ENROLL_PIN
| FIDO2ENROLL_UP
;
66 static int arg_fido2_cred_alg
= COSE_ES256
;
68 static int arg_fido2_cred_alg
= 0;
70 static bool arg_recovery_key
= false;
71 static JsonFormatFlags arg_json_format_flags
= JSON_FORMAT_OFF
;
72 static bool arg_and_resize
= false;
73 static bool arg_and_change_password
= false;
75 EXPORT_FORMAT_FULL
, /* export the full record */
76 EXPORT_FORMAT_STRIPPED
, /* strip "state" + "binding", but leave signature in place */
77 EXPORT_FORMAT_MINIMAL
, /* also strip signature */
78 } arg_export_format
= EXPORT_FORMAT_FULL
;
80 STATIC_DESTRUCTOR_REGISTER(arg_identity_extra
, json_variant_unrefp
);
81 STATIC_DESTRUCTOR_REGISTER(arg_identity_extra_this_machine
, json_variant_unrefp
);
82 STATIC_DESTRUCTOR_REGISTER(arg_identity_extra_privileged
, json_variant_unrefp
);
83 STATIC_DESTRUCTOR_REGISTER(arg_identity_extra_rlimits
, json_variant_unrefp
);
84 STATIC_DESTRUCTOR_REGISTER(arg_identity_filter
, strv_freep
);
85 STATIC_DESTRUCTOR_REGISTER(arg_identity_filter_rlimits
, strv_freep
);
86 STATIC_DESTRUCTOR_REGISTER(arg_pkcs11_token_uri
, strv_freep
);
87 STATIC_DESTRUCTOR_REGISTER(arg_fido2_device
, strv_freep
);
89 static const BusLocator
*bus_mgr
;
91 static bool identity_properties_specified(void) {
94 !json_variant_is_blank_object(arg_identity_extra
) ||
95 !json_variant_is_blank_object(arg_identity_extra_privileged
) ||
96 !json_variant_is_blank_object(arg_identity_extra_this_machine
) ||
97 !json_variant_is_blank_object(arg_identity_extra_rlimits
) ||
98 !strv_isempty(arg_identity_filter
) ||
99 !strv_isempty(arg_identity_filter_rlimits
) ||
100 !strv_isempty(arg_pkcs11_token_uri
) ||
101 !strv_isempty(arg_fido2_device
);
104 static int acquire_bus(sd_bus
**bus
) {
112 r
= bus_connect_transport(arg_transport
, arg_host
, false, bus
);
114 return bus_log_connect_error(r
, arg_transport
);
116 (void) sd_bus_set_allow_interactive_authorization(*bus
, arg_ask_password
);
121 static int list_homes(int argc
, char *argv
[], void *userdata
) {
122 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
123 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
124 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
125 _cleanup_(table_unrefp
) Table
*table
= NULL
;
128 r
= acquire_bus(&bus
);
132 r
= bus_call_method(bus
, bus_mgr
, "ListHomes", &error
, &reply
, NULL
);
134 return log_error_errno(r
, "Failed to list homes: %s", bus_error_message(&error
, r
));
136 table
= table_new("name", "uid", "gid", "state", "realname", "home", "shell");
140 r
= sd_bus_message_enter_container(reply
, 'a', "(susussso)");
142 return bus_log_parse_error(r
);
145 const char *name
, *state
, *realname
, *home
, *shell
, *color
;
149 r
= sd_bus_message_read(reply
, "(susussso)", &name
, &uid
, &state
, &gid
, &realname
, &home
, &shell
, NULL
);
151 return bus_log_parse_error(r
);
155 r
= table_add_many(table
,
160 return table_log_add_error(r
);
163 r
= table_add_cell(table
, &cell
, TABLE_STRING
, state
);
165 return table_log_add_error(r
);
167 color
= user_record_state_color(state
);
169 (void) table_set_color(table
, cell
, color
);
171 r
= table_add_many(table
,
172 TABLE_STRING
, strna(empty_to_null(realname
)),
174 TABLE_STRING
, strna(empty_to_null(shell
)));
176 return table_log_add_error(r
);
179 r
= sd_bus_message_exit_container(reply
);
181 return bus_log_parse_error(r
);
183 if (table_get_rows(table
) > 1 || !FLAGS_SET(arg_json_format_flags
, JSON_FORMAT_OFF
)) {
184 r
= table_set_sort(table
, (size_t) 0);
186 return table_log_sort_error(r
);
188 r
= table_print_with_pager(table
, arg_json_format_flags
, arg_pager_flags
, arg_legend
);
193 if (arg_legend
&& (arg_json_format_flags
& JSON_FORMAT_OFF
)) {
194 if (table_get_rows(table
) > 1)
195 printf("\n%zu home areas listed.\n", table_get_rows(table
) - 1);
197 printf("No home areas.\n");
203 static int acquire_existing_password(
204 const char *user_name
,
206 bool emphasize_current
,
207 AskPasswordFlags flags
) {
209 _cleanup_(strv_free_erasep
) char **password
= NULL
;
210 _cleanup_(erase_and_freep
) char *envpw
= NULL
;
211 _cleanup_free_
char *question
= NULL
;
217 r
= getenv_steal_erase("PASSWORD", &envpw
);
219 return log_error_errno(r
, "Failed to acquire password from environment: %m");
221 /* People really shouldn't use environment variables for passing passwords. We support this
222 * only for testing purposes, and do not document the behaviour, so that people won't
223 * actually use this outside of testing. */
225 r
= user_record_set_password(hr
, STRV_MAKE(envpw
), true);
227 return log_error_errno(r
, "Failed to store password: %m");
232 /* If this is not our own user, then don't use the password cache */
233 if (is_this_me(user_name
) <= 0)
234 SET_FLAG(flags
, ASK_PASSWORD_ACCEPT_CACHED
|ASK_PASSWORD_PUSH_CACHE
, false);
236 if (asprintf(&question
, emphasize_current
?
237 "Please enter current password for user %s:" :
238 "Please enter password for user %s:",
242 r
= ask_password_auto(question
,
243 /* icon= */ "user-home",
245 /* key_name= */ "home-password",
246 /* credential_name= */ "home.password",
250 if (r
== -EUNATCH
) { /* EUNATCH is returned if no password was found and asking interactively was
251 * disabled via the flags. Not an error for us. */
252 log_debug_errno(r
, "No passwords acquired.");
256 return log_error_errno(r
, "Failed to acquire password: %m");
258 r
= user_record_set_password(hr
, password
, true);
260 return log_error_errno(r
, "Failed to store password: %m");
265 static int acquire_recovery_key(
266 const char *user_name
,
268 AskPasswordFlags flags
) {
270 _cleanup_(strv_free_erasep
) char **recovery_key
= NULL
;
271 _cleanup_(erase_and_freep
) char *envpw
= NULL
;
272 _cleanup_free_
char *question
= NULL
;
278 r
= getenv_steal_erase("PASSWORD", &envpw
);
280 return log_error_errno(r
, "Failed to acquire password from environment: %m");
282 /* People really shouldn't use environment variables for passing secrets. We support this
283 * only for testing purposes, and do not document the behaviour, so that people won't
284 * actually use this outside of testing. */
286 r
= user_record_set_password(hr
, STRV_MAKE(envpw
), true); /* recovery keys are stored in the record exactly like regular passwords! */
288 return log_error_errno(r
, "Failed to store recovery key: %m");
293 /* If this is not our own user, then don't use the password cache */
294 if (is_this_me(user_name
) <= 0)
295 SET_FLAG(flags
, ASK_PASSWORD_ACCEPT_CACHED
|ASK_PASSWORD_PUSH_CACHE
, false);
297 if (asprintf(&question
, "Please enter recovery key for user %s:", user_name
) < 0)
300 r
= ask_password_auto(question
,
301 /* icon= */ "user-home",
303 /* key_name= */ "home-recovery-key",
304 /* credential_name= */ "home.recovery-key",
308 if (r
== -EUNATCH
) { /* EUNATCH is returned if no recovery key was found and asking interactively was
309 * disabled via the flags. Not an error for us. */
310 log_debug_errno(r
, "No recovery keys acquired.");
314 return log_error_errno(r
, "Failed to acquire recovery keys: %m");
316 r
= user_record_set_password(hr
, recovery_key
, true);
318 return log_error_errno(r
, "Failed to store recovery keys: %m");
323 static int acquire_token_pin(
324 const char *user_name
,
326 AskPasswordFlags flags
) {
328 _cleanup_(strv_free_erasep
) char **pin
= NULL
;
329 _cleanup_(erase_and_freep
) char *envpin
= NULL
;
330 _cleanup_free_
char *question
= NULL
;
336 r
= getenv_steal_erase("PIN", &envpin
);
338 return log_error_errno(r
, "Failed to acquire PIN from environment: %m");
340 r
= user_record_set_token_pin(hr
, STRV_MAKE(envpin
), false);
342 return log_error_errno(r
, "Failed to store token PIN: %m");
347 /* If this is not our own user, then don't use the password cache */
348 if (is_this_me(user_name
) <= 0)
349 SET_FLAG(flags
, ASK_PASSWORD_ACCEPT_CACHED
|ASK_PASSWORD_PUSH_CACHE
, false);
351 if (asprintf(&question
, "Please enter security token PIN for user %s:", user_name
) < 0)
354 r
= ask_password_auto(
356 /* icon= */ "user-home",
358 /* key_name= */ "token-pin",
359 /* credential_name= */ "home.token-pin",
363 if (r
== -EUNATCH
) { /* EUNATCH is returned if no PIN was found and asking interactively was disabled
364 * via the flags. Not an error for us. */
365 log_debug_errno(r
, "No security token PINs acquired.");
369 return log_error_errno(r
, "Failed to acquire security token PIN: %m");
371 r
= user_record_set_token_pin(hr
, pin
, false);
373 return log_error_errno(r
, "Failed to store security token PIN: %m");
378 static int handle_generic_user_record_error(
379 const char *user_name
,
381 const sd_bus_error
*error
,
383 bool emphasize_current_password
) {
389 if (sd_bus_error_has_name(error
, BUS_ERROR_HOME_ABSENT
))
390 return log_error_errno(SYNTHETIC_ERRNO(EREMOTE
),
391 "Home of user %s is currently absent, please plug in the necessary storage device or backing file system.", user_name
);
393 else if (sd_bus_error_has_name(error
, BUS_ERROR_AUTHENTICATION_LIMIT_HIT
))
394 return log_error_errno(SYNTHETIC_ERRNO(ETOOMANYREFS
),
395 "Too frequent login attempts for user %s, try again later.", user_name
);
397 else if (sd_bus_error_has_name(error
, BUS_ERROR_BAD_PASSWORD
)) {
399 if (!strv_isempty(hr
->password
))
400 log_notice("Password incorrect or not sufficient, please try again.");
402 /* Don't consume cache entries or credentials here, we already tried that unsuccessfully. But
403 * let's push what we acquire here into the cache */
404 r
= acquire_existing_password(
407 emphasize_current_password
,
408 ASK_PASSWORD_PUSH_CACHE
| ASK_PASSWORD_NO_CREDENTIAL
);
412 } else if (sd_bus_error_has_name(error
, BUS_ERROR_BAD_RECOVERY_KEY
)) {
414 if (!strv_isempty(hr
->password
))
415 log_notice("Recovery key incorrect or not sufficient, please try again.");
417 /* Don't consume cache entries or credentials here, we already tried that unsuccessfully. But
418 * let's push what we acquire here into the cache */
419 r
= acquire_recovery_key(
422 ASK_PASSWORD_PUSH_CACHE
| ASK_PASSWORD_NO_CREDENTIAL
);
426 } else if (sd_bus_error_has_name(error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
)) {
428 if (strv_isempty(hr
->password
))
429 log_notice("Security token not inserted, please enter password.");
431 log_notice("Password incorrect or not sufficient, and configured security token not inserted, please try again.");
433 r
= acquire_existing_password(
436 emphasize_current_password
,
437 ASK_PASSWORD_PUSH_CACHE
| ASK_PASSWORD_NO_CREDENTIAL
);
441 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_PIN_NEEDED
)) {
443 /* First time the PIN is requested, let's accept cached data, and allow using credential store */
444 r
= acquire_token_pin(
447 ASK_PASSWORD_ACCEPT_CACHED
| ASK_PASSWORD_PUSH_CACHE
);
451 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_PROTECTED_AUTHENTICATION_PATH_NEEDED
)) {
453 log_notice("%s%sPlease authenticate physically on security token.",
454 emoji_enabled() ? special_glyph(SPECIAL_GLYPH_TOUCH
) : "",
455 emoji_enabled() ? " " : "");
457 r
= user_record_set_pkcs11_protected_authentication_path_permitted(hr
, true);
459 return log_error_errno(r
, "Failed to set PKCS#11 protected authentication path permitted flag: %m");
461 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_USER_PRESENCE_NEEDED
)) {
463 log_notice("%s%sPlease confirm presence on security token.",
464 emoji_enabled() ? special_glyph(SPECIAL_GLYPH_TOUCH
) : "",
465 emoji_enabled() ? " " : "");
467 r
= user_record_set_fido2_user_presence_permitted(hr
, true);
469 return log_error_errno(r
, "Failed to set FIDO2 user presence permitted flag: %m");
471 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_USER_VERIFICATION_NEEDED
)) {
473 log_notice("%s%sPlease verify user on security token.",
474 emoji_enabled() ? special_glyph(SPECIAL_GLYPH_TOUCH
) : "",
475 emoji_enabled() ? " " : "");
477 r
= user_record_set_fido2_user_verification_permitted(hr
, true);
479 return log_error_errno(r
, "Failed to set FIDO2 user verification permitted flag: %m");
481 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_PIN_LOCKED
))
482 return log_error_errno(SYNTHETIC_ERRNO(EPERM
), "Security token PIN is locked, please unlock it first. (Hint: Removal and re-insertion might suffice.)");
484 else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_BAD_PIN
)) {
486 log_notice("Security token PIN incorrect, please try again.");
488 /* If the previous PIN was wrong don't accept cached info anymore, but add to cache. Also, don't use the credential data */
489 r
= acquire_token_pin(
492 ASK_PASSWORD_PUSH_CACHE
| ASK_PASSWORD_NO_CREDENTIAL
);
496 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_BAD_PIN_FEW_TRIES_LEFT
)) {
498 log_notice("Security token PIN incorrect, please try again (only a few tries left!).");
500 r
= acquire_token_pin(
503 ASK_PASSWORD_PUSH_CACHE
| ASK_PASSWORD_NO_CREDENTIAL
);
507 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_BAD_PIN_ONE_TRY_LEFT
)) {
509 log_notice("Security token PIN incorrect, please try again (only one try left!).");
511 r
= acquire_token_pin(
514 ASK_PASSWORD_PUSH_CACHE
| ASK_PASSWORD_NO_CREDENTIAL
);
518 return log_error_errno(ret
, "Operation on home %s failed: %s", user_name
, bus_error_message(error
, ret
));
523 static int acquire_passed_secrets(const char *user_name
, UserRecord
**ret
) {
524 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
529 /* Generates an initial secret objects that contains passwords supplied via $PASSWORD, the password
530 * cache or the credentials subsystem, but excluding any interactive stuff. If nothing is passed,
531 * returns an empty secret object. */
533 secret
= user_record_new();
537 r
= acquire_existing_password(
540 /* emphasize_current_password = */ false,
541 ASK_PASSWORD_ACCEPT_CACHED
| ASK_PASSWORD_NO_TTY
| ASK_PASSWORD_NO_AGENT
);
545 r
= acquire_token_pin(
548 ASK_PASSWORD_ACCEPT_CACHED
| ASK_PASSWORD_NO_TTY
| ASK_PASSWORD_NO_AGENT
);
552 r
= acquire_recovery_key(
555 ASK_PASSWORD_ACCEPT_CACHED
| ASK_PASSWORD_NO_TTY
| ASK_PASSWORD_NO_AGENT
);
559 *ret
= TAKE_PTR(secret
);
563 static int activate_home(int argc
, char *argv
[], void *userdata
) {
564 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
567 r
= acquire_bus(&bus
);
571 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
572 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
574 r
= acquire_passed_secrets(*i
, &secret
);
579 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
580 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
582 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ActivateHome");
584 return bus_log_create_error(r
);
586 r
= sd_bus_message_append(m
, "s", *i
);
588 return bus_log_create_error(r
);
590 r
= bus_message_append_secret(m
, secret
);
592 return bus_log_create_error(r
);
594 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
596 r
= handle_generic_user_record_error(*i
, secret
, &error
, r
, /* emphasize_current_password= */ false);
611 static int deactivate_home(int argc
, char *argv
[], void *userdata
) {
612 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
615 r
= acquire_bus(&bus
);
619 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
620 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
621 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
623 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "DeactivateHome");
625 return bus_log_create_error(r
);
627 r
= sd_bus_message_append(m
, "s", *i
);
629 return bus_log_create_error(r
);
631 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
633 log_error_errno(r
, "Failed to deactivate user home: %s", bus_error_message(&error
, r
));
642 static void dump_home_record(UserRecord
*hr
) {
647 if (hr
->incomplete
) {
649 log_warning("Warning: lacking rights to acquire privileged fields of user record of '%s', output incomplete.", hr
->user_name
);
652 if (arg_json_format_flags
& JSON_FORMAT_OFF
)
653 user_record_show(hr
, true);
655 _cleanup_(user_record_unrefp
) UserRecord
*stripped
= NULL
;
657 if (arg_export_format
== EXPORT_FORMAT_STRIPPED
)
658 r
= user_record_clone(hr
, USER_RECORD_EXTRACT_EMBEDDED
|USER_RECORD_PERMISSIVE
, &stripped
);
659 else if (arg_export_format
== EXPORT_FORMAT_MINIMAL
)
660 r
= user_record_clone(hr
, USER_RECORD_EXTRACT_SIGNABLE
|USER_RECORD_PERMISSIVE
, &stripped
);
664 log_warning_errno(r
, "Failed to strip user record, ignoring: %m");
668 json_variant_dump(hr
->json
, arg_json_format_flags
, stdout
, NULL
);
672 static char **mangle_user_list(char **list
, char ***ret_allocated
) {
673 _cleanup_free_
char *myself
= NULL
;
676 if (!strv_isempty(list
)) {
677 *ret_allocated
= NULL
;
681 myself
= getusername_malloc();
689 l
[0] = TAKE_PTR(myself
);
696 static int inspect_home(int argc
, char *argv
[], void *userdata
) {
697 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
698 _cleanup_(strv_freep
) char **mangled_list
= NULL
;
702 pager_open(arg_pager_flags
);
704 r
= acquire_bus(&bus
);
708 items
= mangle_user_list(strv_skip(argv
, 1), &mangled_list
);
712 STRV_FOREACH(i
, items
) {
713 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
714 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
715 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
716 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
721 r
= parse_uid(*i
, &uid
);
723 if (!valid_user_group_name(*i
, 0)) {
724 log_error("Invalid user name '%s'.", *i
);
731 r
= bus_call_method(bus
, bus_mgr
, "GetUserRecordByName", &error
, &reply
, "s", *i
);
733 r
= bus_call_method(bus
, bus_mgr
, "GetUserRecordByUID", &error
, &reply
, "u", (uint32_t) uid
);
736 log_error_errno(r
, "Failed to inspect home: %s", bus_error_message(&error
, r
));
743 r
= sd_bus_message_read(reply
, "sbo", &json
, &incomplete
, NULL
);
745 bus_log_parse_error(r
);
752 r
= json_parse(json
, JSON_PARSE_SENSITIVE
, &v
, NULL
, NULL
);
754 log_error_errno(r
, "Failed to parse JSON identity: %m");
761 hr
= user_record_new();
765 r
= user_record_load(hr
, v
, USER_RECORD_LOAD_REFUSE_SECRET
|USER_RECORD_LOG
|USER_RECORD_PERMISSIVE
);
773 hr
->incomplete
= incomplete
;
774 dump_home_record(hr
);
780 static int authenticate_home(int argc
, char *argv
[], void *userdata
) {
781 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
782 _cleanup_(strv_freep
) char **mangled_list
= NULL
;
786 items
= mangle_user_list(strv_skip(argv
, 1), &mangled_list
);
790 r
= acquire_bus(&bus
);
794 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
796 STRV_FOREACH(i
, items
) {
797 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
799 r
= acquire_passed_secrets(*i
, &secret
);
804 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
805 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
807 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "AuthenticateHome");
809 return bus_log_create_error(r
);
811 r
= sd_bus_message_append(m
, "s", *i
);
813 return bus_log_create_error(r
);
815 r
= bus_message_append_secret(m
, secret
);
817 return bus_log_create_error(r
);
819 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
821 r
= handle_generic_user_record_error(*i
, secret
, &error
, r
, false);
836 static int update_last_change(JsonVariant
**v
, bool with_password
, bool override
) {
843 n
= now(CLOCK_REALTIME
);
845 c
= json_variant_by_key(*v
, "lastChangeUSec");
850 goto update_password
;
852 if (!json_variant_is_unsigned(c
))
853 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastChangeUSec field is not an unsigned integer, refusing.");
855 u
= json_variant_unsigned(c
);
857 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastChangeUSec is from the future, can't update.");
860 r
= json_variant_set_field_unsigned(v
, "lastChangeUSec", n
);
862 return log_error_errno(r
, "Failed to update lastChangeUSec: %m");
868 c
= json_variant_by_key(*v
, "lastPasswordChangeUSec");
875 if (!json_variant_is_unsigned(c
))
876 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastPasswordChangeUSec field is not an unsigned integer, refusing.");
878 u
= json_variant_unsigned(c
);
880 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastPasswordChangeUSec is from the future, can't update.");
883 r
= json_variant_set_field_unsigned(v
, "lastPasswordChangeUSec", n
);
885 return log_error_errno(r
, "Failed to update lastPasswordChangeUSec: %m");
890 static int apply_identity_changes(JsonVariant
**_v
) {
891 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
896 v
= json_variant_ref(*_v
);
898 r
= json_variant_filter(&v
, arg_identity_filter
);
900 return log_error_errno(r
, "Failed to filter identity: %m");
902 r
= json_variant_merge(&v
, arg_identity_extra
);
904 return log_error_errno(r
, "Failed to merge identities: %m");
906 if (arg_identity_extra_this_machine
|| !strv_isempty(arg_identity_filter
)) {
907 _cleanup_(json_variant_unrefp
) JsonVariant
*per_machine
= NULL
, *mmid
= NULL
;
910 r
= sd_id128_get_machine(&mid
);
912 return log_error_errno(r
, "Failed to acquire machine ID: %m");
914 r
= json_variant_new_string(&mmid
, SD_ID128_TO_STRING(mid
));
916 return log_error_errno(r
, "Failed to allocate matchMachineId object: %m");
918 per_machine
= json_variant_ref(json_variant_by_key(v
, "perMachine"));
920 _cleanup_(json_variant_unrefp
) JsonVariant
*npm
= NULL
, *add
= NULL
;
921 _cleanup_free_ JsonVariant
**array
= NULL
;
925 if (!json_variant_is_array(per_machine
))
926 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "perMachine field is not an array, refusing.");
928 array
= new(JsonVariant
*, json_variant_elements(per_machine
) + 1);
932 JSON_VARIANT_ARRAY_FOREACH(z
, per_machine
) {
935 if (!json_variant_is_object(z
))
936 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "perMachine entry is not an object, refusing.");
940 u
= json_variant_by_key(z
, "matchMachineId");
944 if (!json_variant_equal(u
, mmid
))
947 r
= json_variant_merge(&add
, z
);
949 return log_error_errno(r
, "Failed to merge perMachine entry: %m");
954 r
= json_variant_filter(&add
, arg_identity_filter
);
956 return log_error_errno(r
, "Failed to filter perMachine: %m");
958 r
= json_variant_merge(&add
, arg_identity_extra_this_machine
);
960 return log_error_errno(r
, "Failed to merge in perMachine fields: %m");
962 if (arg_identity_filter_rlimits
|| arg_identity_extra_rlimits
) {
963 _cleanup_(json_variant_unrefp
) JsonVariant
*rlv
= NULL
;
965 rlv
= json_variant_ref(json_variant_by_key(add
, "resourceLimits"));
967 r
= json_variant_filter(&rlv
, arg_identity_filter_rlimits
);
969 return log_error_errno(r
, "Failed to filter resource limits: %m");
971 r
= json_variant_merge(&rlv
, arg_identity_extra_rlimits
);
973 return log_error_errno(r
, "Failed to set resource limits: %m");
975 if (json_variant_is_blank_object(rlv
)) {
976 r
= json_variant_filter(&add
, STRV_MAKE("resourceLimits"));
978 return log_error_errno(r
, "Failed to drop resource limits field from identity: %m");
980 r
= json_variant_set_field(&add
, "resourceLimits", rlv
);
982 return log_error_errno(r
, "Failed to update resource limits of identity: %m");
986 if (!json_variant_is_blank_object(add
)) {
987 r
= json_variant_set_field(&add
, "matchMachineId", mmid
);
989 return log_error_errno(r
, "Failed to set matchMachineId field: %m");
994 r
= json_variant_new_array(&npm
, array
, i
);
996 return log_error_errno(r
, "Failed to allocate new perMachine array: %m");
998 json_variant_unref(per_machine
);
999 per_machine
= TAKE_PTR(npm
);
1001 _cleanup_(json_variant_unrefp
) JsonVariant
*item
= json_variant_ref(arg_identity_extra_this_machine
);
1003 if (arg_identity_extra_rlimits
) {
1004 r
= json_variant_set_field(&item
, "resourceLimits", arg_identity_extra_rlimits
);
1006 return log_error_errno(r
, "Failed to update resource limits of identity: %m");
1009 r
= json_variant_set_field(&item
, "matchMachineId", mmid
);
1011 return log_error_errno(r
, "Failed to set matchMachineId field: %m");
1013 r
= json_variant_append_array(&per_machine
, item
);
1015 return log_error_errno(r
, "Failed to append to perMachine array: %m");
1018 r
= json_variant_set_field(&v
, "perMachine", per_machine
);
1020 return log_error_errno(r
, "Failed to update per machine record: %m");
1023 if (arg_identity_extra_privileged
|| arg_identity_filter
) {
1024 _cleanup_(json_variant_unrefp
) JsonVariant
*privileged
= NULL
;
1026 privileged
= json_variant_ref(json_variant_by_key(v
, "privileged"));
1028 r
= json_variant_filter(&privileged
, arg_identity_filter
);
1030 return log_error_errno(r
, "Failed to filter identity (privileged part): %m");
1032 r
= json_variant_merge(&privileged
, arg_identity_extra_privileged
);
1034 return log_error_errno(r
, "Failed to merge identities (privileged part): %m");
1036 if (json_variant_is_blank_object(privileged
)) {
1037 r
= json_variant_filter(&v
, STRV_MAKE("privileged"));
1039 return log_error_errno(r
, "Failed to drop privileged part from identity: %m");
1041 r
= json_variant_set_field(&v
, "privileged", privileged
);
1043 return log_error_errno(r
, "Failed to update privileged part of identity: %m");
1047 if (arg_identity_filter_rlimits
) {
1048 _cleanup_(json_variant_unrefp
) JsonVariant
*rlv
= NULL
;
1050 rlv
= json_variant_ref(json_variant_by_key(v
, "resourceLimits"));
1052 r
= json_variant_filter(&rlv
, arg_identity_filter_rlimits
);
1054 return log_error_errno(r
, "Failed to filter resource limits: %m");
1056 /* Note that we only filter resource limits here, but don't apply them. We do that in the perMachine section */
1058 if (json_variant_is_blank_object(rlv
)) {
1059 r
= json_variant_filter(&v
, STRV_MAKE("resourceLimits"));
1061 return log_error_errno(r
, "Failed to drop resource limits field from identity: %m");
1063 r
= json_variant_set_field(&v
, "resourceLimits", rlv
);
1065 return log_error_errno(r
, "Failed to update resource limits of identity: %m");
1069 json_variant_unref(*_v
);
1075 static int add_disposition(JsonVariant
**v
) {
1080 if (json_variant_by_key(*v
, "disposition"))
1083 /* Set the disposition to regular, if not configured explicitly */
1084 r
= json_variant_set_field_string(v
, "disposition", "regular");
1086 return log_error_errno(r
, "Failed to set disposition field: %m");
1091 static int acquire_new_home_record(UserRecord
**ret
) {
1092 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
1093 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
1099 unsigned line
, column
;
1101 r
= json_parse_file(
1102 streq(arg_identity
, "-") ? stdin
: NULL
,
1103 streq(arg_identity
, "-") ? "<stdin>" : arg_identity
, JSON_PARSE_SENSITIVE
, &v
, &line
, &column
);
1105 return log_error_errno(r
, "Failed to parse identity at %u:%u: %m", line
, column
);
1108 r
= apply_identity_changes(&v
);
1112 r
= add_disposition(&v
);
1116 STRV_FOREACH(i
, arg_pkcs11_token_uri
) {
1117 r
= identity_add_pkcs11_key_data(&v
, *i
);
1122 STRV_FOREACH(i
, arg_fido2_device
) {
1123 r
= identity_add_fido2_parameters(&v
, *i
, arg_fido2_lock_with
, arg_fido2_cred_alg
);
1128 if (arg_recovery_key
) {
1129 r
= identity_add_recovery_key(&v
);
1134 r
= update_last_change(&v
, true, false);
1139 json_variant_dump(v
, JSON_FORMAT_PRETTY
, NULL
, NULL
);
1141 hr
= user_record_new();
1145 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
);
1149 *ret
= TAKE_PTR(hr
);
1153 static int acquire_new_password(
1154 const char *user_name
,
1159 _cleanup_(erase_and_freep
) char *envpw
= NULL
;
1166 r
= getenv_steal_erase("NEWPASSWORD", &envpw
);
1168 return log_error_errno(r
, "Failed to acquire password from environment: %m");
1170 /* As above, this is not for use, just for testing */
1172 r
= user_record_set_password(hr
, STRV_MAKE(envpw
), /* prepend = */ true);
1174 return log_error_errno(r
, "Failed to store password: %m");
1177 *ret
= TAKE_PTR(envpw
);
1183 (void) suggest_passwords();
1186 _cleanup_(strv_free_erasep
) char **first
= NULL
, **second
= NULL
;
1187 _cleanup_free_
char *question
= NULL
;
1190 return log_error_errno(SYNTHETIC_ERRNO(ENOKEY
), "Too many attempts, giving up:");
1192 if (asprintf(&question
, "Please enter new password for user %s:", user_name
) < 0)
1195 r
= ask_password_auto(
1197 /* icon= */ "user-home",
1199 /* key_name= */ "home-password",
1200 /* credential_name= */ "home.new-password",
1202 0, /* no caching, we want to collect a new password here after all */
1205 return log_error_errno(r
, "Failed to acquire password: %m");
1207 question
= mfree(question
);
1208 if (asprintf(&question
, "Please enter new password for user %s (repeat):", user_name
) < 0)
1211 r
= ask_password_auto(
1213 /* icon= */ "user-home",
1215 /* key_name= */ "home-password",
1216 /* credential_name= */ "home.new-password",
1221 return log_error_errno(r
, "Failed to acquire password: %m");
1223 if (strv_equal(first
, second
)) {
1224 _cleanup_(erase_and_freep
) char *copy
= NULL
;
1227 copy
= strdup(first
[0]);
1232 r
= user_record_set_password(hr
, first
, /* prepend = */ true);
1234 return log_error_errno(r
, "Failed to store password: %m");
1237 *ret
= TAKE_PTR(copy
);
1242 log_error("Password didn't match, try again.");
1246 static int create_home(int argc
, char *argv
[], void *userdata
) {
1247 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1248 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
1251 r
= acquire_bus(&bus
);
1255 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1258 /* If a username was specified, use it */
1260 if (valid_user_group_name(argv
[1], 0))
1261 r
= json_variant_set_field_string(&arg_identity_extra
, "userName", argv
[1]);
1263 _cleanup_free_
char *un
= NULL
, *rr
= NULL
;
1265 /* Before we consider the user name invalid, let's check if we can split it? */
1266 r
= split_user_name_realm(argv
[1], &un
, &rr
);
1268 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User name '%s' is not valid: %m", argv
[1]);
1271 r
= json_variant_set_field_string(&arg_identity_extra
, "realm", rr
);
1273 return log_error_errno(r
, "Failed to set realm field: %m");
1276 r
= json_variant_set_field_string(&arg_identity_extra
, "userName", un
);
1279 return log_error_errno(r
, "Failed to set userName field: %m");
1281 /* If neither a username nor an identity have been specified we cannot operate. */
1283 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User name required.");
1286 r
= acquire_new_home_record(&hr
);
1290 /* If the JSON record carries no plain text password (besides the recovery key), then let's query it
1292 if (strv_length(hr
->password
) <= arg_recovery_key
) {
1294 if (strv_isempty(hr
->hashed_password
)) {
1295 _cleanup_(erase_and_freep
) char *new_password
= NULL
;
1297 /* No regular (i.e. non-PKCS#11) hashed passwords set in the record, let's fix that. */
1298 r
= acquire_new_password(hr
->user_name
, hr
, /* suggest = */ true, &new_password
);
1302 r
= user_record_make_hashed_password(hr
, STRV_MAKE(new_password
), /* extend = */ false);
1304 return log_error_errno(r
, "Failed to hash password: %m");
1306 /* There's a hash password set in the record, acquire the unhashed version of it. */
1307 r
= acquire_existing_password(
1310 /* emphasize_current= */ false,
1311 ASK_PASSWORD_ACCEPT_CACHED
| ASK_PASSWORD_PUSH_CACHE
);
1317 if (hr
->enforce_password_policy
== 0) {
1318 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1320 /* If password quality enforcement is disabled, let's at least warn client side */
1322 r
= user_record_quality_check_password(hr
, hr
, &error
);
1324 log_warning_errno(r
, "Specified password does not pass quality checks (%s), proceeding anyway.", bus_error_message(&error
, r
));
1328 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1329 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1330 _cleanup_(erase_and_freep
) char *formatted
= NULL
;
1332 r
= json_variant_format(hr
->json
, 0, &formatted
);
1334 return log_error_errno(r
, "Failed to format user record: %m");
1336 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "CreateHome");
1338 return bus_log_create_error(r
);
1340 (void) sd_bus_message_sensitive(m
);
1342 r
= sd_bus_message_append(m
, "s", formatted
);
1344 return bus_log_create_error(r
);
1346 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1348 if (sd_bus_error_has_name(&error
, BUS_ERROR_LOW_PASSWORD_QUALITY
)) {
1349 _cleanup_(erase_and_freep
) char *new_password
= NULL
;
1351 log_error_errno(r
, "%s", bus_error_message(&error
, r
));
1352 log_info("(Use --enforce-password-policy=no to turn off password quality checks for this account.)");
1354 r
= acquire_new_password(hr
->user_name
, hr
, /* suggest = */ false, &new_password
);
1358 r
= user_record_make_hashed_password(hr
, STRV_MAKE(new_password
), /* extend = */ false);
1360 return log_error_errno(r
, "Failed to hash passwords: %m");
1362 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1373 static int remove_home(int argc
, char *argv
[], void *userdata
) {
1374 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1377 r
= acquire_bus(&bus
);
1381 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1383 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
1384 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1385 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1387 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "RemoveHome");
1389 return bus_log_create_error(r
);
1391 r
= sd_bus_message_append(m
, "s", *i
);
1393 return bus_log_create_error(r
);
1395 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1397 log_error_errno(r
, "Failed to remove home: %s", bus_error_message(&error
, r
));
1406 static int acquire_updated_home_record(
1408 const char *username
,
1411 _cleanup_(json_variant_unrefp
) JsonVariant
*json
= NULL
;
1412 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
1418 unsigned line
, column
;
1421 r
= json_parse_file(
1422 streq(arg_identity
, "-") ? stdin
: NULL
,
1423 streq(arg_identity
, "-") ? "<stdin>" : arg_identity
, JSON_PARSE_SENSITIVE
, &json
, &line
, &column
);
1425 return log_error_errno(r
, "Failed to parse identity at %u:%u: %m", line
, column
);
1427 un
= json_variant_by_key(json
, "userName");
1429 if (!json_variant_is_string(un
) || (username
&& !streq(json_variant_string(un
), username
)))
1430 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User name specified on command line and in JSON record do not match.");
1433 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "No username specified.");
1435 r
= json_variant_set_field_string(&arg_identity_extra
, "userName", username
);
1437 return log_error_errno(r
, "Failed to set userName field: %m");
1441 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1442 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1446 if (!identity_properties_specified())
1447 return log_error_errno(SYNTHETIC_ERRNO(EALREADY
), "No field to change specified.");
1449 r
= bus_call_method(bus
, bus_mgr
, "GetUserRecordByName", &error
, &reply
, "s", username
);
1451 return log_error_errno(r
, "Failed to acquire user home record: %s", bus_error_message(&error
, r
));
1453 r
= sd_bus_message_read(reply
, "sbo", &text
, &incomplete
, NULL
);
1455 return bus_log_parse_error(r
);
1458 return log_error_errno(SYNTHETIC_ERRNO(EACCES
), "Lacking rights to acquire user record including privileged metadata, can't update record.");
1460 r
= json_parse(text
, JSON_PARSE_SENSITIVE
, &json
, NULL
, NULL
);
1462 return log_error_errno(r
, "Failed to parse JSON identity: %m");
1464 reply
= sd_bus_message_unref(reply
);
1466 r
= json_variant_filter(&json
, STRV_MAKE("binding", "status", "signature"));
1468 return log_error_errno(r
, "Failed to strip binding and status from record to update: %m");
1471 r
= apply_identity_changes(&json
);
1475 STRV_FOREACH(i
, arg_pkcs11_token_uri
) {
1476 r
= identity_add_pkcs11_key_data(&json
, *i
);
1481 STRV_FOREACH(i
, arg_fido2_device
) {
1482 r
= identity_add_fido2_parameters(&json
, *i
, arg_fido2_lock_with
, arg_fido2_cred_alg
);
1487 /* If the user supplied a full record, then add in lastChange, but do not override. Otherwise always
1489 r
= update_last_change(&json
, arg_pkcs11_token_uri
|| arg_fido2_device
, !arg_identity
);
1494 json_variant_dump(json
, JSON_FORMAT_PRETTY
, NULL
, NULL
);
1496 hr
= user_record_new();
1500 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
);
1504 *ret
= TAKE_PTR(hr
);
1508 static int home_record_reset_human_interaction_permission(UserRecord
*hr
) {
1513 /* When we execute multiple operations one after the other, let's reset the permission to ask the
1514 * user each time, so that if interaction is necessary we will be told so again and thus can print a
1515 * nice message to the user, telling the user so. */
1517 r
= user_record_set_pkcs11_protected_authentication_path_permitted(hr
, -1);
1519 return log_error_errno(r
, "Failed to reset PKCS#11 protected authentication path permission flag: %m");
1521 r
= user_record_set_fido2_user_presence_permitted(hr
, -1);
1523 return log_error_errno(r
, "Failed to reset FIDO2 user presence permission flag: %m");
1525 r
= user_record_set_fido2_user_verification_permitted(hr
, -1);
1527 return log_error_errno(r
, "Failed to reset FIDO2 user verification permission flag: %m");
1532 static int update_home(int argc
, char *argv
[], void *userdata
) {
1533 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1534 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
, *secret
= NULL
;
1535 _cleanup_free_
char *buffer
= NULL
;
1536 const char *username
;
1541 else if (!arg_identity
) {
1542 buffer
= getusername_malloc();
1550 r
= acquire_bus(&bus
);
1554 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1556 r
= acquire_updated_home_record(bus
, username
, &hr
);
1560 /* Add in all secrets we can acquire cheaply */
1561 r
= acquire_passed_secrets(username
, &secret
);
1565 r
= user_record_merge_secret(hr
, secret
);
1569 /* If we do multiple operations, let's output things more verbosely, since otherwise the repeated
1570 * authentication might be confusing. */
1572 if (arg_and_resize
|| arg_and_change_password
)
1573 log_info("Updating home directory.");
1576 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1577 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1578 _cleanup_free_
char *formatted
= NULL
;
1580 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "UpdateHome");
1582 return bus_log_create_error(r
);
1584 r
= json_variant_format(hr
->json
, 0, &formatted
);
1586 return log_error_errno(r
, "Failed to format user record: %m");
1588 (void) sd_bus_message_sensitive(m
);
1590 r
= sd_bus_message_append(m
, "s", formatted
);
1592 return bus_log_create_error(r
);
1594 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1596 if (arg_and_change_password
&&
1597 sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1598 /* In the generic handler we'd ask for a password in this case, but when
1599 * changing passwords that's not sufficient, as we need to acquire all keys
1601 return log_error_errno(r
, "Security token not inserted, refusing.");
1603 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1611 log_info("Resizing home.");
1613 (void) home_record_reset_human_interaction_permission(hr
);
1615 /* Also sync down disk size to underlying LUKS/fscrypt/quota */
1616 while (arg_and_resize
) {
1617 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1618 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1620 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ResizeHome");
1622 return bus_log_create_error(r
);
1624 /* Specify UINT64_MAX as size, in which case the underlying disk size will just be synced */
1625 r
= sd_bus_message_append(m
, "st", hr
->user_name
, UINT64_MAX
);
1627 return bus_log_create_error(r
);
1629 r
= bus_message_append_secret(m
, hr
);
1631 return bus_log_create_error(r
);
1633 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1635 if (arg_and_change_password
&&
1636 sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1637 return log_error_errno(r
, "Security token not inserted, refusing.");
1639 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1646 if (arg_and_change_password
)
1647 log_info("Synchronizing passwords and encryption keys.");
1649 (void) home_record_reset_human_interaction_permission(hr
);
1651 /* Also sync down passwords to underlying LUKS/fscrypt */
1652 while (arg_and_change_password
) {
1653 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1654 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1656 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ChangePasswordHome");
1658 return bus_log_create_error(r
);
1660 /* Specify an empty new secret, in which case the underlying LUKS/fscrypt password will just be synced */
1661 r
= sd_bus_message_append(m
, "ss", hr
->user_name
, "{}");
1663 return bus_log_create_error(r
);
1665 r
= bus_message_append_secret(m
, hr
);
1667 return bus_log_create_error(r
);
1669 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1671 if (sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1672 return log_error_errno(r
, "Security token not inserted, refusing.");
1674 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1684 static int passwd_home(int argc
, char *argv
[], void *userdata
) {
1685 _cleanup_(user_record_unrefp
) UserRecord
*old_secret
= NULL
, *new_secret
= NULL
;
1686 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1687 _cleanup_free_
char *buffer
= NULL
;
1688 const char *username
;
1691 if (arg_pkcs11_token_uri
)
1692 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1693 "To change the PKCS#11 security token use 'homectl update --pkcs11-token-uri=%s'.",
1694 special_glyph(SPECIAL_GLYPH_ELLIPSIS
));
1695 if (arg_fido2_device
)
1696 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1697 "To change the FIDO2 security token use 'homectl update --fido2-device=%s'.",
1698 special_glyph(SPECIAL_GLYPH_ELLIPSIS
));
1699 if (identity_properties_specified())
1700 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "The 'passwd' verb does not permit changing other record properties at the same time.");
1705 buffer
= getusername_malloc();
1712 r
= acquire_bus(&bus
);
1716 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1718 r
= acquire_passed_secrets(username
, &old_secret
);
1722 new_secret
= user_record_new();
1726 r
= acquire_new_password(username
, new_secret
, /* suggest = */ true, NULL
);
1731 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1732 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1734 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ChangePasswordHome");
1736 return bus_log_create_error(r
);
1738 r
= sd_bus_message_append(m
, "s", username
);
1740 return bus_log_create_error(r
);
1742 r
= bus_message_append_secret(m
, new_secret
);
1744 return bus_log_create_error(r
);
1746 r
= bus_message_append_secret(m
, old_secret
);
1748 return bus_log_create_error(r
);
1750 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1752 if (sd_bus_error_has_name(&error
, BUS_ERROR_LOW_PASSWORD_QUALITY
)) {
1754 log_error_errno(r
, "%s", bus_error_message(&error
, r
));
1756 r
= acquire_new_password(username
, new_secret
, /* suggest = */ false, NULL
);
1758 } else if (sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1760 /* In the generic handler we'd ask for a password in this case, but when
1761 * changing passwords that's not sufficeint, as we need to acquire all keys
1763 return log_error_errno(r
, "Security token not inserted, refusing.");
1765 r
= handle_generic_user_record_error(username
, old_secret
, &error
, r
, true);
1775 static int parse_disk_size(const char *t
, uint64_t *ret
) {
1781 if (streq(t
, "min"))
1783 else if (streq(t
, "max"))
1784 *ret
= UINT64_MAX
-1; /* Largest size that isn't UINT64_MAX special marker */
1788 r
= parse_size(t
, 1024, &ds
);
1790 return log_error_errno(r
, "Failed to parse disk size parameter: %s", t
);
1792 if (ds
>= UINT64_MAX
) /* UINT64_MAX has special meaning for us ("dont change"), refuse */
1793 return log_error_errno(SYNTHETIC_ERRNO(ERANGE
), "Disk size out of range: %s", t
);
1801 static int parse_sector_size(const char *t
, uint64_t *ret
) {
1809 r
= safe_atou64(t
, &ss
);
1811 return log_error_errno(r
, "Failed to parse sector size parameter %s", t
);
1812 if (ss
< 512 || ss
> 4096) /* Allow up to 4K due to dm-crypt support and 4K alignment by the homed LUKS backend */
1813 return log_error_errno(SYNTHETIC_ERRNO(ERANGE
), "Sector size not between 512 and 4096: %s", t
);
1814 if (!ISPOWEROF2(ss
))
1815 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Sector size not power of 2: %s", t
);
1821 static int resize_home(int argc
, char *argv
[], void *userdata
) {
1822 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1823 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
1824 uint64_t ds
= UINT64_MAX
;
1827 r
= acquire_bus(&bus
);
1831 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1833 if (arg_disk_size_relative
!= UINT64_MAX
||
1834 (argc
> 2 && parse_permyriad(argv
[2]) >= 0))
1835 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
1836 "Relative disk size specification currently not supported when resizing.");
1839 r
= parse_disk_size(argv
[2], &ds
);
1844 if (arg_disk_size
!= UINT64_MAX
) {
1845 if (ds
!= UINT64_MAX
&& ds
!= arg_disk_size
)
1846 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Disk size specified twice and doesn't match, refusing.");
1851 r
= acquire_passed_secrets(argv
[1], &secret
);
1856 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1857 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1859 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ResizeHome");
1861 return bus_log_create_error(r
);
1863 r
= sd_bus_message_append(m
, "st", argv
[1], ds
);
1865 return bus_log_create_error(r
);
1867 r
= bus_message_append_secret(m
, secret
);
1869 return bus_log_create_error(r
);
1871 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1873 r
= handle_generic_user_record_error(argv
[1], secret
, &error
, r
, false);
1883 static int lock_home(int argc
, char *argv
[], void *userdata
) {
1884 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1887 r
= acquire_bus(&bus
);
1891 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
1892 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1893 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1895 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "LockHome");
1897 return bus_log_create_error(r
);
1899 r
= sd_bus_message_append(m
, "s", *i
);
1901 return bus_log_create_error(r
);
1903 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1905 log_error_errno(r
, "Failed to lock home: %s", bus_error_message(&error
, r
));
1914 static int unlock_home(int argc
, char *argv
[], void *userdata
) {
1915 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1918 r
= acquire_bus(&bus
);
1922 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
1923 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
1925 r
= acquire_passed_secrets(*i
, &secret
);
1930 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1931 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1933 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "UnlockHome");
1935 return bus_log_create_error(r
);
1937 r
= sd_bus_message_append(m
, "s", *i
);
1939 return bus_log_create_error(r
);
1941 r
= bus_message_append_secret(m
, secret
);
1943 return bus_log_create_error(r
);
1945 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1947 r
= handle_generic_user_record_error(argv
[1], secret
, &error
, r
, false);
1962 static int with_home(int argc
, char *argv
[], void *userdata
) {
1963 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1964 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1965 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1966 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
1967 _cleanup_close_
int acquired_fd
= -1;
1968 _cleanup_strv_free_
char **cmdline
= NULL
;
1973 r
= acquire_bus(&bus
);
1978 _cleanup_free_
char *shell
= NULL
;
1980 /* If no command is specified, spawn a shell */
1981 r
= get_shell(&shell
);
1983 return log_error_errno(r
, "Failed to acquire shell: %m");
1985 cmdline
= strv_new(shell
);
1987 cmdline
= strv_copy(argv
+ 2);
1991 r
= acquire_passed_secrets(argv
[1], &secret
);
1996 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "AcquireHome");
1998 return bus_log_create_error(r
);
2000 r
= sd_bus_message_append(m
, "s", argv
[1]);
2002 return bus_log_create_error(r
);
2004 r
= bus_message_append_secret(m
, secret
);
2006 return bus_log_create_error(r
);
2008 r
= sd_bus_message_append(m
, "b", /* please_suspend = */ getenv_bool("SYSTEMD_PLEASE_SUSPEND_HOME") > 0);
2010 return bus_log_create_error(r
);
2012 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, &reply
);
2013 m
= sd_bus_message_unref(m
);
2015 r
= handle_generic_user_record_error(argv
[1], secret
, &error
, r
, false);
2019 sd_bus_error_free(&error
);
2023 r
= sd_bus_message_read(reply
, "h", &fd
);
2025 return bus_log_parse_error(r
);
2027 acquired_fd
= fcntl(fd
, F_DUPFD_CLOEXEC
, 3);
2028 if (acquired_fd
< 0)
2029 return log_error_errno(errno
, "Failed to duplicate acquired fd: %m");
2031 reply
= sd_bus_message_unref(reply
);
2036 r
= bus_call_method(bus
, bus_mgr
, "GetHomeByName", &error
, &reply
, "s", argv
[1]);
2038 return log_error_errno(r
, "Failed to inspect home: %s", bus_error_message(&error
, r
));
2040 r
= sd_bus_message_read(reply
, "usussso", NULL
, NULL
, NULL
, NULL
, &home
, NULL
, NULL
);
2042 return bus_log_parse_error(r
);
2044 r
= safe_fork("(with)", FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_DEATHSIG
|FORK_LOG
|FORK_RLIMIT_NOFILE_SAFE
|FORK_REOPEN_LOG
, &pid
);
2048 if (chdir(home
) < 0) {
2049 log_error_errno(errno
, "Failed to change to directory %s: %m", home
);
2053 execvp(cmdline
[0], cmdline
);
2054 log_error_errno(errno
, "Failed to execute %s: %m", cmdline
[0]);
2058 ret
= wait_for_terminate_and_check(cmdline
[0], pid
, WAIT_LOG_ABNORMAL
);
2060 /* Close the fd that pings the home now. */
2061 acquired_fd
= safe_close(acquired_fd
);
2063 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ReleaseHome");
2065 return bus_log_create_error(r
);
2067 r
= sd_bus_message_append(m
, "s", argv
[1]);
2069 return bus_log_create_error(r
);
2071 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
2073 if (sd_bus_error_has_name(&error
, BUS_ERROR_HOME_BUSY
))
2074 log_notice("Not deactivating home directory of %s, as it is still used.", argv
[1]);
2076 return log_error_errno(r
, "Failed to release user home: %s", bus_error_message(&error
, r
));
2082 static int lock_all_homes(int argc
, char *argv
[], void *userdata
) {
2083 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2084 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2085 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
2088 r
= acquire_bus(&bus
);
2092 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "LockAllHomes");
2094 return bus_log_create_error(r
);
2096 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
2098 return log_error_errno(r
, "Failed to lock all homes: %s", bus_error_message(&error
, r
));
2103 static int deactivate_all_homes(int argc
, char *argv
[], void *userdata
) {
2104 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2105 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2106 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
2109 r
= acquire_bus(&bus
);
2113 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "DeactivateAllHomes");
2115 return bus_log_create_error(r
);
2117 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
2119 return log_error_errno(r
, "Failed to deactivate all homes: %s", bus_error_message(&error
, r
));
2124 static int rebalance(int argc
, char *argv
[], void *userdata
) {
2125 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2126 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2127 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
2130 r
= acquire_bus(&bus
);
2134 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "Rebalance");
2136 return bus_log_create_error(r
);
2138 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
2140 if (sd_bus_error_has_name(&error
, BUS_ERROR_REBALANCE_NOT_NEEDED
))
2141 log_info("No homes needed rebalancing.");
2143 return log_error_errno(r
, "Failed to rebalance: %s", bus_error_message(&error
, r
));
2145 log_info("Completed rebalancing.");
2150 static int drop_from_identity(const char *field
) {
2155 /* If we are called to update an identity record and drop some field, let's keep track of what to
2156 * remove from the old record */
2157 r
= strv_extend(&arg_identity_filter
, field
);
2161 /* Let's also drop the field if it was previously set to a new value on the same command line */
2162 r
= json_variant_filter(&arg_identity_extra
, STRV_MAKE(field
));
2164 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
2166 r
= json_variant_filter(&arg_identity_extra_this_machine
, STRV_MAKE(field
));
2168 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
2170 r
= json_variant_filter(&arg_identity_extra_privileged
, STRV_MAKE(field
));
2172 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
2177 static int help(int argc
, char *argv
[], void *userdata
) {
2178 _cleanup_free_
char *link
= NULL
;
2181 pager_open(arg_pager_flags
);
2183 r
= terminal_urlify_man("homectl", "1", &link
);
2187 printf("%1$s [OPTIONS...] COMMAND ...\n\n"
2188 "%2$sCreate, manipulate or inspect home directories.%3$s\n"
2189 "\n%4$sCommands:%5$s\n"
2190 " list List home areas\n"
2191 " activate USER… Activate a home area\n"
2192 " deactivate USER… Deactivate a home area\n"
2193 " inspect USER… Inspect a home area\n"
2194 " authenticate USER… Authenticate a home area\n"
2195 " create USER Create a home area\n"
2196 " remove USER… Remove a home area\n"
2197 " update USER Update a home area\n"
2198 " passwd USER Change password of a home area\n"
2199 " resize USER SIZE Resize a home area\n"
2200 " lock USER… Temporarily lock an active home area\n"
2201 " unlock USER… Unlock a temporarily locked home area\n"
2202 " lock-all Lock all suitable home areas\n"
2203 " deactivate-all Deactivate all active home areas\n"
2204 " rebalance Rebalance free space between home areas\n"
2205 " with USER [COMMAND…] Run shell or command with access to a home area\n"
2206 "\n%4$sOptions:%5$s\n"
2207 " -h --help Show this help\n"
2208 " --version Show package version\n"
2209 " --no-pager Do not pipe output into a pager\n"
2210 " --no-legend Do not show the headers and footers\n"
2211 " --no-ask-password Do not ask for system passwords\n"
2212 " -H --host=[USER@]HOST Operate on remote host\n"
2213 " -M --machine=CONTAINER Operate on local container\n"
2214 " --identity=PATH Read JSON identity from file\n"
2215 " --json=FORMAT Output inspection data in JSON (takes one of\n"
2216 " pretty, short, off)\n"
2217 " -j Equivalent to --json=pretty (on TTY) or\n"
2218 " --json=short (otherwise)\n"
2219 " --export-format= Strip JSON inspection data (full, stripped,\n"
2221 " -E When specified once equals -j --export-format=\n"
2222 " stripped, when specified twice equals\n"
2223 " -j --export-format=minimal\n"
2224 "\n%4$sGeneral User Record Properties:%5$s\n"
2225 " -c --real-name=REALNAME Real name for user\n"
2226 " --realm=REALM Realm to create user in\n"
2227 " --email-address=EMAIL Email address for user\n"
2228 " --location=LOCATION Set location of user on earth\n"
2229 " --icon-name=NAME Icon name for user\n"
2230 " -d --home-dir=PATH Home directory\n"
2231 " -u --uid=UID Numeric UID for user\n"
2232 " -G --member-of=GROUP Add user to group\n"
2233 " --skel=PATH Skeleton directory to use\n"
2234 " --shell=PATH Shell for account\n"
2235 " --setenv=VARIABLE[=VALUE] Set an environment variable at log-in\n"
2236 " --timezone=TIMEZONE Set a time-zone\n"
2237 " --language=LOCALE Set preferred language\n"
2238 " --ssh-authorized-keys=KEYS\n"
2239 " Specify SSH public keys\n"
2240 " --pkcs11-token-uri=URI URI to PKCS#11 security token containing\n"
2241 " private key and matching X.509 certificate\n"
2242 " --fido2-device=PATH Path to FIDO2 hidraw device with hmac-secret\n"
2244 " --fido2-with-client-pin=BOOL\n"
2245 " Whether to require entering a PIN to unlock the\n"
2247 " --fido2-with-user-presence=BOOL\n"
2248 " Whether to require user presence to unlock the\n"
2250 " --fido2-with-user-verification=BOOL\n"
2251 " Whether to require user verification to unlock\n"
2253 " --recovery-key=BOOL Add a recovery key\n"
2254 "\n%4$sAccount Management User Record Properties:%5$s\n"
2255 " --locked=BOOL Set locked account state\n"
2256 " --not-before=TIMESTAMP Do not allow logins before\n"
2257 " --not-after=TIMESTAMP Do not allow logins after\n"
2258 " --rate-limit-interval=SECS\n"
2259 " Login rate-limit interval in seconds\n"
2260 " --rate-limit-burst=NUMBER\n"
2261 " Login rate-limit attempts per interval\n"
2262 "\n%4$sPassword Policy User Record Properties:%5$s\n"
2263 " --password-hint=HINT Set Password hint\n"
2264 " --enforce-password-policy=BOOL\n"
2265 " Control whether to enforce system's password\n"
2266 " policy for this user\n"
2267 " -P Same as --enforce-password-password=no\n"
2268 " --password-change-now=BOOL\n"
2269 " Require the password to be changed on next login\n"
2270 " --password-change-min=TIME\n"
2271 " Require minimum time between password changes\n"
2272 " --password-change-max=TIME\n"
2273 " Require maximum time between password changes\n"
2274 " --password-change-warn=TIME\n"
2275 " How much time to warn before password expiry\n"
2276 " --password-change-inactive=TIME\n"
2277 " How much time to block password after expiry\n"
2278 "\n%4$sResource Management User Record Properties:%5$s\n"
2279 " --disk-size=BYTES Size to assign the user on disk\n"
2280 " --access-mode=MODE User home directory access mode\n"
2281 " --umask=MODE Umask for user when logging in\n"
2282 " --nice=NICE Nice level for user\n"
2283 " --rlimit=LIMIT=VALUE[:VALUE]\n"
2284 " Set resource limits\n"
2285 " --tasks-max=MAX Set maximum number of per-user tasks\n"
2286 " --memory-high=BYTES Set high memory threshold in bytes\n"
2287 " --memory-max=BYTES Set maximum memory limit\n"
2288 " --cpu-weight=WEIGHT Set CPU weight\n"
2289 " --io-weight=WEIGHT Set IO weight\n"
2290 "\n%4$sStorage User Record Properties:%5$s\n"
2291 " --storage=STORAGE Storage type to use (luks, fscrypt, directory,\n"
2292 " subvolume, cifs)\n"
2293 " --image-path=PATH Path to image file/directory\n"
2294 " --drop-caches=BOOL Whether to automatically drop caches on logout\n"
2295 "\n%4$sLUKS Storage User Record Properties:%5$s\n"
2296 " --fs-type=TYPE File system type to use in case of luks\n"
2297 " storage (btrfs, ext4, xfs)\n"
2298 " --luks-discard=BOOL Whether to use 'discard' feature of file system\n"
2299 " when activated (mounted)\n"
2300 " --luks-offline-discard=BOOL\n"
2301 " Whether to trim file on logout\n"
2302 " --luks-cipher=CIPHER Cipher to use for LUKS encryption\n"
2303 " --luks-cipher-mode=MODE Cipher mode to use for LUKS encryption\n"
2304 " --luks-volume-key-size=BITS\n"
2305 " Volume key size to use for LUKS encryption\n"
2306 " --luks-pbkdf-type=TYPE Password-based Key Derivation Function to use\n"
2307 " --luks-pbkdf-hash-algorithm=ALGORITHM\n"
2308 " PBKDF hash algorithm to use\n"
2309 " --luks-pbkdf-time-cost=SECS\n"
2310 " Time cost for PBKDF in seconds\n"
2311 " --luks-pbkdf-memory-cost=BYTES\n"
2312 " Memory cost for PBKDF in bytes\n"
2313 " --luks-pbkdf-parallel-threads=NUMBER\n"
2314 " Number of parallel threads for PKBDF\n"
2315 " --luks-sector-size=BYTES\n"
2316 " Sector size for LUKS encryption in bytes\n"
2317 " --luks-extra-mount-options=OPTIONS\n"
2318 " LUKS extra mount options\n"
2319 " --auto-resize-mode=MODE Automatically grow/shrink home on login/logout\n"
2320 " --rebalance-weight=WEIGHT Weight while rebalancing\n"
2321 "\n%4$sMounting User Record Properties:%5$s\n"
2322 " --nosuid=BOOL Control the 'nosuid' flag of the home mount\n"
2323 " --nodev=BOOL Control the 'nodev' flag of the home mount\n"
2324 " --noexec=BOOL Control the 'noexec' flag of the home mount\n"
2325 "\n%4$sCIFS User Record Properties:%5$s\n"
2326 " --cifs-domain=DOMAIN CIFS (Windows) domain\n"
2327 " --cifs-user-name=USER CIFS (Windows) user name\n"
2328 " --cifs-service=SERVICE CIFS (Windows) service to mount as home area\n"
2329 " --cifs-extra-mount-options=OPTIONS\n"
2330 " CIFS (Windows) extra mount options\n"
2331 "\n%4$sLogin Behaviour User Record Properties:%5$s\n"
2332 " --stop-delay=SECS How long to leave user services running after\n"
2334 " --kill-processes=BOOL Whether to kill user processes when sessions\n"
2336 " --auto-login=BOOL Try to log this user in automatically\n"
2337 "\nSee the %6$s for details.\n",
2338 program_invocation_short_name
,
2348 static int parse_argv(int argc
, char *argv
[]) {
2351 ARG_VERSION
= 0x100,
2354 ARG_NO_ASK_PASSWORD
,
2364 ARG_LUKS_OFFLINE_DISCARD
,
2370 ARG_SSH_AUTHORIZED_KEYS
,
2379 ARG_LUKS_CIPHER_MODE
,
2380 ARG_LUKS_VOLUME_KEY_SIZE
,
2387 ARG_CIFS_EXTRA_MOUNT_OPTIONS
,
2393 ARG_LUKS_PBKDF_TYPE
,
2394 ARG_LUKS_PBKDF_HASH_ALGORITHM
,
2395 ARG_LUKS_PBKDF_FORCE_ITERATIONS
,
2396 ARG_LUKS_PBKDF_TIME_COST
,
2397 ARG_LUKS_PBKDF_MEMORY_COST
,
2398 ARG_LUKS_PBKDF_PARALLEL_THREADS
,
2399 ARG_LUKS_SECTOR_SIZE
,
2400 ARG_RATE_LIMIT_INTERVAL
,
2401 ARG_RATE_LIMIT_BURST
,
2404 ARG_ENFORCE_PASSWORD_POLICY
,
2405 ARG_PASSWORD_CHANGE_NOW
,
2406 ARG_PASSWORD_CHANGE_MIN
,
2407 ARG_PASSWORD_CHANGE_MAX
,
2408 ARG_PASSWORD_CHANGE_WARN
,
2409 ARG_PASSWORD_CHANGE_INACTIVE
,
2412 ARG_PKCS11_TOKEN_URI
,
2419 ARG_AND_CHANGE_PASSWORD
,
2421 ARG_LUKS_EXTRA_MOUNT_OPTIONS
,
2422 ARG_AUTO_RESIZE_MODE
,
2423 ARG_REBALANCE_WEIGHT
,
2427 static const struct option options
[] = {
2428 { "help", no_argument
, NULL
, 'h' },
2429 { "version", no_argument
, NULL
, ARG_VERSION
},
2430 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
2431 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
2432 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
2433 { "host", required_argument
, NULL
, 'H' },
2434 { "machine", required_argument
, NULL
, 'M' },
2435 { "identity", required_argument
, NULL
, 'I' },
2436 { "real-name", required_argument
, NULL
, 'c' },
2437 { "comment", required_argument
, NULL
, 'c' }, /* Compat alias to keep thing in sync with useradd(8) */
2438 { "realm", required_argument
, NULL
, ARG_REALM
},
2439 { "email-address", required_argument
, NULL
, ARG_EMAIL_ADDRESS
},
2440 { "location", required_argument
, NULL
, ARG_LOCATION
},
2441 { "password-hint", required_argument
, NULL
, ARG_PASSWORD_HINT
},
2442 { "icon-name", required_argument
, NULL
, ARG_ICON_NAME
},
2443 { "home-dir", required_argument
, NULL
, 'd' }, /* Compatible with useradd(8) */
2444 { "uid", required_argument
, NULL
, 'u' }, /* Compatible with useradd(8) */
2445 { "member-of", required_argument
, NULL
, 'G' },
2446 { "groups", required_argument
, NULL
, 'G' }, /* Compat alias to keep thing in sync with useradd(8) */
2447 { "skel", required_argument
, NULL
, 'k' }, /* Compatible with useradd(8) */
2448 { "shell", required_argument
, NULL
, 's' }, /* Compatible with useradd(8) */
2449 { "setenv", required_argument
, NULL
, ARG_SETENV
},
2450 { "timezone", required_argument
, NULL
, ARG_TIMEZONE
},
2451 { "language", required_argument
, NULL
, ARG_LANGUAGE
},
2452 { "locked", required_argument
, NULL
, ARG_LOCKED
},
2453 { "not-before", required_argument
, NULL
, ARG_NOT_BEFORE
},
2454 { "not-after", required_argument
, NULL
, ARG_NOT_AFTER
},
2455 { "expiredate", required_argument
, NULL
, 'e' }, /* Compat alias to keep thing in sync with useradd(8) */
2456 { "ssh-authorized-keys", required_argument
, NULL
, ARG_SSH_AUTHORIZED_KEYS
},
2457 { "disk-size", required_argument
, NULL
, ARG_DISK_SIZE
},
2458 { "access-mode", required_argument
, NULL
, ARG_ACCESS_MODE
},
2459 { "umask", required_argument
, NULL
, ARG_UMASK
},
2460 { "nice", required_argument
, NULL
, ARG_NICE
},
2461 { "rlimit", required_argument
, NULL
, ARG_RLIMIT
},
2462 { "tasks-max", required_argument
, NULL
, ARG_TASKS_MAX
},
2463 { "memory-high", required_argument
, NULL
, ARG_MEMORY_HIGH
},
2464 { "memory-max", required_argument
, NULL
, ARG_MEMORY_MAX
},
2465 { "cpu-weight", required_argument
, NULL
, ARG_CPU_WEIGHT
},
2466 { "io-weight", required_argument
, NULL
, ARG_IO_WEIGHT
},
2467 { "storage", required_argument
, NULL
, ARG_STORAGE
},
2468 { "image-path", required_argument
, NULL
, ARG_IMAGE_PATH
},
2469 { "fs-type", required_argument
, NULL
, ARG_FS_TYPE
},
2470 { "luks-discard", required_argument
, NULL
, ARG_LUKS_DISCARD
},
2471 { "luks-offline-discard", required_argument
, NULL
, ARG_LUKS_OFFLINE_DISCARD
},
2472 { "luks-cipher", required_argument
, NULL
, ARG_LUKS_CIPHER
},
2473 { "luks-cipher-mode", required_argument
, NULL
, ARG_LUKS_CIPHER_MODE
},
2474 { "luks-volume-key-size", required_argument
, NULL
, ARG_LUKS_VOLUME_KEY_SIZE
},
2475 { "luks-pbkdf-type", required_argument
, NULL
, ARG_LUKS_PBKDF_TYPE
},
2476 { "luks-pbkdf-hash-algorithm", required_argument
, NULL
, ARG_LUKS_PBKDF_HASH_ALGORITHM
},
2477 { "luks-pbkdf-force-iterations", required_argument
, NULL
, ARG_LUKS_PBKDF_FORCE_ITERATIONS
},
2478 { "luks-pbkdf-time-cost", required_argument
, NULL
, ARG_LUKS_PBKDF_TIME_COST
},
2479 { "luks-pbkdf-memory-cost", required_argument
, NULL
, ARG_LUKS_PBKDF_MEMORY_COST
},
2480 { "luks-pbkdf-parallel-threads", required_argument
, NULL
, ARG_LUKS_PBKDF_PARALLEL_THREADS
},
2481 { "luks-sector-size", required_argument
, NULL
, ARG_LUKS_SECTOR_SIZE
},
2482 { "nosuid", required_argument
, NULL
, ARG_NOSUID
},
2483 { "nodev", required_argument
, NULL
, ARG_NODEV
},
2484 { "noexec", required_argument
, NULL
, ARG_NOEXEC
},
2485 { "cifs-user-name", required_argument
, NULL
, ARG_CIFS_USER_NAME
},
2486 { "cifs-domain", required_argument
, NULL
, ARG_CIFS_DOMAIN
},
2487 { "cifs-service", required_argument
, NULL
, ARG_CIFS_SERVICE
},
2488 { "cifs-extra-mount-options", required_argument
, NULL
, ARG_CIFS_EXTRA_MOUNT_OPTIONS
},
2489 { "rate-limit-interval", required_argument
, NULL
, ARG_RATE_LIMIT_INTERVAL
},
2490 { "rate-limit-burst", required_argument
, NULL
, ARG_RATE_LIMIT_BURST
},
2491 { "stop-delay", required_argument
, NULL
, ARG_STOP_DELAY
},
2492 { "kill-processes", required_argument
, NULL
, ARG_KILL_PROCESSES
},
2493 { "enforce-password-policy", required_argument
, NULL
, ARG_ENFORCE_PASSWORD_POLICY
},
2494 { "password-change-now", required_argument
, NULL
, ARG_PASSWORD_CHANGE_NOW
},
2495 { "password-change-min", required_argument
, NULL
, ARG_PASSWORD_CHANGE_MIN
},
2496 { "password-change-max", required_argument
, NULL
, ARG_PASSWORD_CHANGE_MAX
},
2497 { "password-change-warn", required_argument
, NULL
, ARG_PASSWORD_CHANGE_WARN
},
2498 { "password-change-inactive", required_argument
, NULL
, ARG_PASSWORD_CHANGE_INACTIVE
},
2499 { "auto-login", required_argument
, NULL
, ARG_AUTO_LOGIN
},
2500 { "json", required_argument
, NULL
, ARG_JSON
},
2501 { "export-format", required_argument
, NULL
, ARG_EXPORT_FORMAT
},
2502 { "pkcs11-token-uri", required_argument
, NULL
, ARG_PKCS11_TOKEN_URI
},
2503 { "fido2-credential-algorithm", required_argument
, NULL
, ARG_FIDO2_CRED_ALG
},
2504 { "fido2-device", required_argument
, NULL
, ARG_FIDO2_DEVICE
},
2505 { "fido2-with-client-pin", required_argument
, NULL
, ARG_FIDO2_WITH_PIN
},
2506 { "fido2-with-user-presence", required_argument
, NULL
, ARG_FIDO2_WITH_UP
},
2507 { "fido2-with-user-verification",required_argument
, NULL
, ARG_FIDO2_WITH_UV
},
2508 { "recovery-key", required_argument
, NULL
, ARG_RECOVERY_KEY
},
2509 { "and-resize", required_argument
, NULL
, ARG_AND_RESIZE
},
2510 { "and-change-password", required_argument
, NULL
, ARG_AND_CHANGE_PASSWORD
},
2511 { "drop-caches", required_argument
, NULL
, ARG_DROP_CACHES
},
2512 { "luks-extra-mount-options", required_argument
, NULL
, ARG_LUKS_EXTRA_MOUNT_OPTIONS
},
2513 { "auto-resize-mode", required_argument
, NULL
, ARG_AUTO_RESIZE_MODE
},
2514 { "rebalance-weight", required_argument
, NULL
, ARG_REBALANCE_WEIGHT
},
2526 c
= getopt_long(argc
, argv
, "hH:M:I:c:d:u:k:s:e:G:jPE", options
, NULL
);
2533 return help(0, NULL
, NULL
);
2539 arg_pager_flags
|= PAGER_DISABLE
;
2546 case ARG_NO_ASK_PASSWORD
:
2547 arg_ask_password
= false;
2551 arg_transport
= BUS_TRANSPORT_REMOTE
;
2556 arg_transport
= BUS_TRANSPORT_MACHINE
;
2561 arg_identity
= optarg
;
2565 if (isempty(optarg
)) {
2566 r
= drop_from_identity("realName");
2573 if (!valid_gecos(optarg
))
2574 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Real name '%s' not a valid GECOS field.", optarg
);
2576 r
= json_variant_set_field_string(&arg_identity_extra
, "realName", optarg
);
2578 return log_error_errno(r
, "Failed to set realName field: %m");
2583 _cleanup_free_
char *hd
= NULL
;
2585 if (isempty(optarg
)) {
2586 r
= drop_from_identity("homeDirectory");
2593 r
= parse_path_argument(optarg
, false, &hd
);
2597 if (!valid_home(hd
))
2598 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Home directory '%s' not valid.", hd
);
2600 r
= json_variant_set_field_string(&arg_identity_extra
, "homeDirectory", hd
);
2602 return log_error_errno(r
, "Failed to set homeDirectory field: %m");
2608 if (isempty(optarg
)) {
2609 r
= drop_from_identity("realm");
2616 r
= dns_name_is_valid(optarg
);
2618 return log_error_errno(r
, "Failed to determine whether realm '%s' is a valid DNS domain: %m", optarg
);
2620 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Realm '%s' is not a valid DNS domain: %m", optarg
);
2622 r
= json_variant_set_field_string(&arg_identity_extra
, "realm", optarg
);
2624 return log_error_errno(r
, "Failed to set realm field: %m");
2627 case ARG_EMAIL_ADDRESS
:
2630 case ARG_CIFS_USER_NAME
:
2631 case ARG_CIFS_DOMAIN
:
2632 case ARG_CIFS_EXTRA_MOUNT_OPTIONS
:
2633 case ARG_LUKS_EXTRA_MOUNT_OPTIONS
: {
2636 c
== ARG_EMAIL_ADDRESS
? "emailAddress" :
2637 c
== ARG_LOCATION
? "location" :
2638 c
== ARG_ICON_NAME
? "iconName" :
2639 c
== ARG_CIFS_USER_NAME
? "cifsUserName" :
2640 c
== ARG_CIFS_DOMAIN
? "cifsDomain" :
2641 c
== ARG_CIFS_EXTRA_MOUNT_OPTIONS
? "cifsExtraMountOptions" :
2642 c
== ARG_LUKS_EXTRA_MOUNT_OPTIONS
? "luksExtraMountOptions" :
2647 if (isempty(optarg
)) {
2648 r
= drop_from_identity(field
);
2655 r
= json_variant_set_field_string(&arg_identity_extra
, field
, optarg
);
2657 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2662 case ARG_CIFS_SERVICE
:
2663 if (isempty(optarg
)) {
2664 r
= drop_from_identity("cifsService");
2671 r
= parse_cifs_service(optarg
, NULL
, NULL
, NULL
);
2673 return log_error_errno(r
, "Failed to validate CIFS service name: %s", optarg
);
2675 r
= json_variant_set_field_string(&arg_identity_extra
, "cifsService", optarg
);
2677 return log_error_errno(r
, "Failed to set cifsService field: %m");
2681 case ARG_PASSWORD_HINT
:
2682 if (isempty(optarg
)) {
2683 r
= drop_from_identity("passwordHint");
2690 r
= json_variant_set_field_string(&arg_identity_extra_privileged
, "passwordHint", optarg
);
2692 return log_error_errno(r
, "Failed to set passwordHint field: %m");
2694 string_erase(optarg
);
2700 if (isempty(optarg
)) {
2701 r
= drop_from_identity("niceLevel");
2707 r
= parse_nice(optarg
, &nc
);
2709 return log_error_errno(r
, "Failed to parse nice level: %s", optarg
);
2711 r
= json_variant_set_field_integer(&arg_identity_extra
, "niceLevel", nc
);
2713 return log_error_errno(r
, "Failed to set niceLevel field: %m");
2719 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
, *jcur
= NULL
, *jmax
= NULL
;
2720 _cleanup_free_
char *field
= NULL
, *t
= NULL
;
2725 if (isempty(optarg
)) {
2726 /* Remove all resource limits */
2728 r
= drop_from_identity("resourceLimits");
2732 arg_identity_filter_rlimits
= strv_free(arg_identity_filter_rlimits
);
2733 arg_identity_extra_rlimits
= json_variant_unref(arg_identity_extra_rlimits
);
2737 eq
= strchr(optarg
, '=');
2739 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Can't parse resource limit assignment: %s", optarg
);
2741 field
= strndup(optarg
, eq
- optarg
);
2745 l
= rlimit_from_string_harder(field
);
2747 return log_error_errno(l
, "Unknown resource limit type: %s", field
);
2749 if (isempty(eq
+ 1)) {
2750 /* Remove only the specific rlimit */
2752 r
= strv_extend(&arg_identity_filter_rlimits
, rlimit_to_string(l
));
2756 r
= json_variant_filter(&arg_identity_extra_rlimits
, STRV_MAKE(field
));
2758 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
2763 r
= rlimit_parse(l
, eq
+ 1, &rl
);
2765 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to parse resource limit value: %s", eq
+ 1);
2767 r
= rl
.rlim_cur
== RLIM_INFINITY
? json_variant_new_null(&jcur
) : json_variant_new_unsigned(&jcur
, rl
.rlim_cur
);
2769 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to allocate current integer: %m");
2771 r
= rl
.rlim_max
== RLIM_INFINITY
? json_variant_new_null(&jmax
) : json_variant_new_unsigned(&jmax
, rl
.rlim_max
);
2773 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to allocate maximum integer: %m");
2777 JSON_BUILD_PAIR("cur", JSON_BUILD_VARIANT(jcur
)),
2778 JSON_BUILD_PAIR("max", JSON_BUILD_VARIANT(jmax
))));
2780 return log_error_errno(r
, "Failed to build resource limit: %m");
2782 t
= strjoin("RLIMIT_", rlimit_to_string(l
));
2786 r
= json_variant_set_field(&arg_identity_extra_rlimits
, t
, v
);
2788 return log_error_errno(r
, "Failed to set %s field: %m", rlimit_to_string(l
));
2796 if (isempty(optarg
)) {
2797 r
= drop_from_identity("uid");
2804 r
= parse_uid(optarg
, &uid
);
2806 return log_error_errno(r
, "Failed to parse UID '%s'.", optarg
);
2808 if (uid_is_system(uid
))
2809 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "UID " UID_FMT
" is in system range, refusing.", uid
);
2810 if (uid_is_dynamic(uid
))
2811 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "UID " UID_FMT
" is in dynamic range, refusing.", uid
);
2812 if (uid
== UID_NOBODY
)
2813 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "UID " UID_FMT
" is nobody UID, refusing.", uid
);
2815 r
= json_variant_set_field_unsigned(&arg_identity_extra
, "uid", uid
);
2817 return log_error_errno(r
, "Failed to set realm field: %m");
2823 case ARG_IMAGE_PATH
: {
2824 const char *field
= c
== 'k' ? "skeletonDirectory" : "imagePath";
2825 _cleanup_free_
char *v
= NULL
;
2827 if (isempty(optarg
)) {
2828 r
= drop_from_identity(field
);
2835 r
= parse_path_argument(optarg
, false, &v
);
2839 r
= json_variant_set_field_string(&arg_identity_extra_this_machine
, field
, v
);
2841 return log_error_errno(r
, "Failed to set %s field: %m", v
);
2847 if (isempty(optarg
)) {
2848 r
= drop_from_identity("shell");
2855 if (!valid_shell(optarg
))
2856 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Shell '%s' not valid.", optarg
);
2858 r
= json_variant_set_field_string(&arg_identity_extra
, "shell", optarg
);
2860 return log_error_errno(r
, "Failed to set shell field: %m");
2865 _cleanup_free_
char **l
= NULL
;
2866 _cleanup_(json_variant_unrefp
) JsonVariant
*ne
= NULL
;
2869 if (isempty(optarg
)) {
2870 r
= drop_from_identity("environment");
2877 e
= json_variant_by_key(arg_identity_extra
, "environment");
2879 r
= json_variant_strv(e
, &l
);
2881 return log_error_errno(r
, "Failed to parse JSON environment field: %m");
2884 r
= strv_env_replace_strdup_passthrough(&l
, optarg
);
2886 return log_error_errno(r
, "Cannot assign environment variable %s: %m", optarg
);
2890 r
= json_variant_new_array_strv(&ne
, l
);
2892 return log_error_errno(r
, "Failed to allocate environment list JSON: %m");
2894 r
= json_variant_set_field(&arg_identity_extra
, "environment", ne
);
2896 return log_error_errno(r
, "Failed to set environment list: %m");
2903 if (isempty(optarg
)) {
2904 r
= drop_from_identity("timeZone");
2911 if (!timezone_is_valid(optarg
, LOG_DEBUG
))
2912 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Timezone '%s' is not valid.", optarg
);
2914 r
= json_variant_set_field_string(&arg_identity_extra
, "timeZone", optarg
);
2916 return log_error_errno(r
, "Failed to set timezone field: %m");
2921 if (isempty(optarg
)) {
2922 r
= drop_from_identity("language");
2929 if (!locale_is_valid(optarg
))
2930 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Locale '%s' is not valid.", optarg
);
2932 if (locale_is_installed(optarg
) <= 0)
2933 log_warning("Locale '%s' is not installed, accepting anyway.", optarg
);
2935 r
= json_variant_set_field_string(&arg_identity_extra
, "preferredLanguage", optarg
);
2937 return log_error_errno(r
, "Failed to set preferredLanguage field: %m");
2945 case ARG_KILL_PROCESSES
:
2946 case ARG_ENFORCE_PASSWORD_POLICY
:
2947 case ARG_AUTO_LOGIN
:
2948 case ARG_PASSWORD_CHANGE_NOW
: {
2950 c
== ARG_LOCKED
? "locked" :
2951 c
== ARG_NOSUID
? "mountNoSuid" :
2952 c
== ARG_NODEV
? "mountNoDevices" :
2953 c
== ARG_NOEXEC
? "mountNoExecute" :
2954 c
== ARG_KILL_PROCESSES
? "killProcesses" :
2955 c
== ARG_ENFORCE_PASSWORD_POLICY
? "enforcePasswordPolicy" :
2956 c
== ARG_AUTO_LOGIN
? "autoLogin" :
2957 c
== ARG_PASSWORD_CHANGE_NOW
? "passwordChangeNow" :
2962 if (isempty(optarg
)) {
2963 r
= drop_from_identity(field
);
2970 r
= parse_boolean(optarg
);
2972 return log_error_errno(r
, "Failed to parse %s boolean: %m", field
);
2974 r
= json_variant_set_field_boolean(&arg_identity_extra
, field
, r
> 0);
2976 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2982 r
= json_variant_set_field_boolean(&arg_identity_extra
, "enforcePasswordPolicy", false);
2984 return log_error_errno(r
, "Failed to set enforcePasswordPolicy field: %m");
2989 if (isempty(optarg
)) {
2990 FOREACH_STRING(prop
, "diskSize", "diskSizeRelative", "rebalanceWeight") {
2991 r
= drop_from_identity(prop
);
2996 arg_disk_size
= arg_disk_size_relative
= UINT64_MAX
;
3000 r
= parse_permyriad(optarg
);
3002 r
= parse_disk_size(optarg
, &arg_disk_size
);
3006 r
= drop_from_identity("diskSizeRelative");
3010 r
= json_variant_set_field_unsigned(&arg_identity_extra_this_machine
, "diskSize", arg_disk_size
);
3012 return log_error_errno(r
, "Failed to set diskSize field: %m");
3014 arg_disk_size_relative
= UINT64_MAX
;
3016 /* Normalize to UINT32_MAX == 100% */
3017 arg_disk_size_relative
= UINT32_SCALE_FROM_PERMYRIAD(r
);
3019 r
= drop_from_identity("diskSize");
3023 r
= json_variant_set_field_unsigned(&arg_identity_extra_this_machine
, "diskSizeRelative", arg_disk_size_relative
);
3025 return log_error_errno(r
, "Failed to set diskSizeRelative field: %m");
3027 arg_disk_size
= UINT64_MAX
;
3030 /* Automatically turn off the rebalance logic if user configured a size explicitly */
3031 r
= json_variant_set_field_unsigned(&arg_identity_extra_this_machine
, "rebalanceWeight", REBALANCE_WEIGHT_OFF
);
3033 return log_error_errno(r
, "Failed to set rebalanceWeight field: %m");
3037 case ARG_ACCESS_MODE
: {
3040 if (isempty(optarg
)) {
3041 r
= drop_from_identity("accessMode");
3048 r
= parse_mode(optarg
, &mode
);
3050 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Access mode '%s' not valid.", optarg
);
3052 r
= json_variant_set_field_unsigned(&arg_identity_extra
, "accessMode", mode
);
3054 return log_error_errno(r
, "Failed to set access mode field: %m");
3059 case ARG_LUKS_DISCARD
:
3060 if (isempty(optarg
)) {
3061 r
= drop_from_identity("luksDiscard");
3068 r
= parse_boolean(optarg
);
3070 return log_error_errno(r
, "Failed to parse --luks-discard= parameter: %s", optarg
);
3072 r
= json_variant_set_field_boolean(&arg_identity_extra
, "luksDiscard", r
);
3074 return log_error_errno(r
, "Failed to set discard field: %m");
3078 case ARG_LUKS_OFFLINE_DISCARD
:
3079 if (isempty(optarg
)) {
3080 r
= drop_from_identity("luksOfflineDiscard");
3087 r
= parse_boolean(optarg
);
3089 return log_error_errno(r
, "Failed to parse --luks-offline-discard= parameter: %s", optarg
);
3091 r
= json_variant_set_field_boolean(&arg_identity_extra
, "luksOfflineDiscard", r
);
3093 return log_error_errno(r
, "Failed to set offline discard field: %m");
3097 case ARG_LUKS_VOLUME_KEY_SIZE
:
3098 case ARG_LUKS_PBKDF_FORCE_ITERATIONS
:
3099 case ARG_LUKS_PBKDF_PARALLEL_THREADS
:
3100 case ARG_RATE_LIMIT_BURST
: {
3102 c
== ARG_LUKS_VOLUME_KEY_SIZE
? "luksVolumeKeySize" :
3103 c
== ARG_LUKS_PBKDF_FORCE_ITERATIONS
? "luksPbkdfForceIterations" :
3104 c
== ARG_LUKS_PBKDF_PARALLEL_THREADS
? "luksPbkdfParallelThreads" :
3105 c
== ARG_RATE_LIMIT_BURST
? "rateLimitBurst" : NULL
;
3110 if (isempty(optarg
)) {
3111 r
= drop_from_identity(field
);
3116 r
= safe_atou(optarg
, &n
);
3118 return log_error_errno(r
, "Failed to parse %s parameter: %s", field
, optarg
);
3120 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, n
);
3122 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3127 case ARG_LUKS_SECTOR_SIZE
: {
3130 if (isempty(optarg
)) {
3131 r
= drop_from_identity("luksSectorSize");
3138 r
= parse_sector_size(optarg
, &ss
);
3142 r
= json_variant_set_field_unsigned(&arg_identity_extra
, "luksSectorSize", ss
);
3144 return log_error_errno(r
, "Failed to set sector size field: %m");
3152 if (isempty(optarg
)) {
3153 r
= drop_from_identity("umask");
3160 r
= parse_mode(optarg
, &m
);
3162 return log_error_errno(r
, "Failed to parse umask: %m");
3164 r
= json_variant_set_field_integer(&arg_identity_extra
, "umask", m
);
3166 return log_error_errno(r
, "Failed to set umask field: %m");
3171 case ARG_SSH_AUTHORIZED_KEYS
: {
3172 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
3173 _cleanup_(strv_freep
) char **l
= NULL
, **add
= NULL
;
3175 if (isempty(optarg
)) {
3176 r
= drop_from_identity("sshAuthorizedKeys");
3183 if (optarg
[0] == '@') {
3184 _cleanup_fclose_
FILE *f
= NULL
;
3186 /* If prefixed with '@' read from a file */
3188 f
= fopen(optarg
+1, "re");
3190 return log_error_errno(errno
, "Failed to open '%s': %m", optarg
+1);
3193 _cleanup_free_
char *line
= NULL
;
3195 r
= read_line(f
, LONG_LINE_MAX
, &line
);
3197 return log_error_errno(r
, "Failed to read from '%s': %m", optarg
+1);
3207 r
= strv_consume(&add
, TAKE_PTR(line
));
3212 /* Otherwise, assume it's a literal key. Let's do some superficial checks
3213 * before accept it though. */
3215 if (string_has_cc(optarg
, NULL
))
3216 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Authorized key contains control characters, refusing.");
3217 if (optarg
[0] == '#')
3218 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Specified key is a comment?");
3220 add
= strv_new(optarg
);
3225 v
= json_variant_ref(json_variant_by_key(arg_identity_extra_privileged
, "sshAuthorizedKeys"));
3227 r
= json_variant_strv(v
, &l
);
3229 return log_error_errno(r
, "Failed to parse SSH authorized keys list: %m");
3232 r
= strv_extend_strv(&l
, add
, true);
3236 v
= json_variant_unref(v
);
3238 r
= json_variant_new_array_strv(&v
, l
);
3242 r
= json_variant_set_field(&arg_identity_extra_privileged
, "sshAuthorizedKeys", v
);
3244 return log_error_errno(r
, "Failed to set authorized keys: %m");
3249 case ARG_NOT_BEFORE
:
3255 field
= c
== ARG_NOT_BEFORE
? "notBeforeUSec" :
3256 IN_SET(c
, ARG_NOT_AFTER
, 'e') ? "notAfterUSec" : NULL
;
3260 if (isempty(optarg
)) {
3261 r
= drop_from_identity(field
);
3268 /* Note the minor discrepancy regarding -e parsing here: we support that for compat
3269 * reasons, and in the original useradd(8) implementation it accepts dates in the
3270 * format YYYY-MM-DD. Coincidentally, we accept dates formatted like that too, but
3271 * with greater precision. */
3272 r
= parse_timestamp(optarg
, &n
);
3274 return log_error_errno(r
, "Failed to parse %s parameter: %m", field
);
3276 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, n
);
3278 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3282 case ARG_PASSWORD_CHANGE_MIN
:
3283 case ARG_PASSWORD_CHANGE_MAX
:
3284 case ARG_PASSWORD_CHANGE_WARN
:
3285 case ARG_PASSWORD_CHANGE_INACTIVE
: {
3289 field
= c
== ARG_PASSWORD_CHANGE_MIN
? "passwordChangeMinUSec" :
3290 c
== ARG_PASSWORD_CHANGE_MAX
? "passwordChangeMaxUSec" :
3291 c
== ARG_PASSWORD_CHANGE_WARN
? "passwordChangeWarnUSec" :
3292 c
== ARG_PASSWORD_CHANGE_INACTIVE
? "passwordChangeInactiveUSec" :
3297 if (isempty(optarg
)) {
3298 r
= drop_from_identity(field
);
3305 r
= parse_sec(optarg
, &n
);
3307 return log_error_errno(r
, "Failed to parse %s parameter: %m", field
);
3309 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, n
);
3311 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3317 case ARG_LUKS_CIPHER
:
3318 case ARG_LUKS_CIPHER_MODE
:
3319 case ARG_LUKS_PBKDF_TYPE
:
3320 case ARG_LUKS_PBKDF_HASH_ALGORITHM
: {
3323 c
== ARG_STORAGE
? "storage" :
3324 c
== ARG_FS_TYPE
? "fileSystemType" :
3325 c
== ARG_LUKS_CIPHER
? "luksCipher" :
3326 c
== ARG_LUKS_CIPHER_MODE
? "luksCipherMode" :
3327 c
== ARG_LUKS_PBKDF_TYPE
? "luksPbkdfType" :
3328 c
== ARG_LUKS_PBKDF_HASH_ALGORITHM
? "luksPbkdfHashAlgorithm" : NULL
;
3332 if (isempty(optarg
)) {
3333 r
= drop_from_identity(field
);
3340 if (!string_is_safe(optarg
))
3341 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Parameter for %s field not valid: %s", field
, optarg
);
3343 r
= json_variant_set_field_string(
3344 IN_SET(c
, ARG_STORAGE
, ARG_FS_TYPE
) ?
3345 &arg_identity_extra_this_machine
:
3346 &arg_identity_extra
, field
, optarg
);
3348 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3353 case ARG_LUKS_PBKDF_TIME_COST
:
3354 case ARG_RATE_LIMIT_INTERVAL
:
3355 case ARG_STOP_DELAY
: {
3357 c
== ARG_LUKS_PBKDF_TIME_COST
? "luksPbkdfTimeCostUSec" :
3358 c
== ARG_RATE_LIMIT_INTERVAL
? "rateLimitIntervalUSec" :
3359 c
== ARG_STOP_DELAY
? "stopDelayUSec" :
3365 if (isempty(optarg
)) {
3366 r
= drop_from_identity(field
);
3373 r
= parse_sec(optarg
, &t
);
3375 return log_error_errno(r
, "Failed to parse %s field: %s", field
, optarg
);
3377 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, t
);
3379 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3385 const char *p
= optarg
;
3388 r
= drop_from_identity("memberOf");
3396 _cleanup_(json_variant_unrefp
) JsonVariant
*mo
= NULL
;
3397 _cleanup_strv_free_
char **list
= NULL
;
3398 _cleanup_free_
char *word
= NULL
;
3400 r
= extract_first_word(&p
, &word
, ",", 0);
3402 return log_error_errno(r
, "Failed to parse group list: %m");
3406 if (!valid_user_group_name(word
, 0))
3407 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Invalid group name %s.", word
);
3409 mo
= json_variant_ref(json_variant_by_key(arg_identity_extra
, "memberOf"));
3411 r
= json_variant_strv(mo
, &list
);
3413 return log_error_errno(r
, "Failed to parse group list: %m");
3415 r
= strv_extend(&list
, word
);
3422 mo
= json_variant_unref(mo
);
3423 r
= json_variant_new_array_strv(&mo
, list
);
3425 return log_error_errno(r
, "Failed to create group list JSON: %m");
3427 r
= json_variant_set_field(&arg_identity_extra
, "memberOf", mo
);
3429 return log_error_errno(r
, "Failed to update group list: %m");
3435 case ARG_TASKS_MAX
: {
3438 if (isempty(optarg
)) {
3439 r
= drop_from_identity("tasksMax");
3445 r
= safe_atou64(optarg
, &u
);
3447 return log_error_errno(r
, "Failed to parse --tasks-max= parameter: %s", optarg
);
3449 r
= json_variant_set_field_unsigned(&arg_identity_extra
, "tasksMax", u
);
3451 return log_error_errno(r
, "Failed to set tasksMax field: %m");
3456 case ARG_MEMORY_MAX
:
3457 case ARG_MEMORY_HIGH
:
3458 case ARG_LUKS_PBKDF_MEMORY_COST
: {
3460 c
== ARG_MEMORY_MAX
? "memoryMax" :
3461 c
== ARG_MEMORY_HIGH
? "memoryHigh" :
3462 c
== ARG_LUKS_PBKDF_MEMORY_COST
? "luksPbkdfMemoryCost" : NULL
;
3468 if (isempty(optarg
)) {
3469 r
= drop_from_identity(field
);
3475 r
= parse_size(optarg
, 1024, &u
);
3477 return log_error_errno(r
, "Failed to parse %s parameter: %s", field
, optarg
);
3479 r
= json_variant_set_field_unsigned(&arg_identity_extra_this_machine
, field
, u
);
3481 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3486 case ARG_CPU_WEIGHT
:
3487 case ARG_IO_WEIGHT
: {
3488 const char *field
= c
== ARG_CPU_WEIGHT
? "cpuWeight" :
3489 c
== ARG_IO_WEIGHT
? "ioWeight" : NULL
;
3494 if (isempty(optarg
)) {
3495 r
= drop_from_identity(field
);
3501 r
= safe_atou64(optarg
, &u
);
3503 return log_error_errno(r
, "Failed to parse --cpu-weight=/--io-weight= parameter: %s", optarg
);
3505 if (!CGROUP_WEIGHT_IS_OK(u
))
3506 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Weight %" PRIu64
" is out of valid weight range.", u
);
3508 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, u
);
3510 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3515 case ARG_PKCS11_TOKEN_URI
:
3516 if (streq(optarg
, "list"))
3517 return pkcs11_list_tokens();
3519 /* If --pkcs11-token-uri= is specified we always drop everything old */
3520 FOREACH_STRING(p
, "pkcs11TokenUri", "pkcs11EncryptedKey") {
3521 r
= drop_from_identity(p
);
3526 if (isempty(optarg
)) {
3527 arg_pkcs11_token_uri
= strv_free(arg_pkcs11_token_uri
);
3531 if (streq(optarg
, "auto")) {
3532 _cleanup_free_
char *found
= NULL
;
3534 r
= pkcs11_find_token_auto(&found
);
3537 r
= strv_consume(&arg_pkcs11_token_uri
, TAKE_PTR(found
));
3539 if (!pkcs11_uri_valid(optarg
))
3540 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Not a valid PKCS#11 URI: %s", optarg
);
3542 r
= strv_extend(&arg_pkcs11_token_uri
, optarg
);
3547 strv_uniq(arg_pkcs11_token_uri
);
3550 case ARG_FIDO2_CRED_ALG
:
3551 r
= parse_fido2_algorithm(optarg
, &arg_fido2_cred_alg
);
3553 return log_error_errno(r
, "Failed to parse COSE algorithm: %s", optarg
);
3556 case ARG_FIDO2_DEVICE
:
3557 if (streq(optarg
, "list"))
3558 return fido2_list_devices();
3560 FOREACH_STRING(p
, "fido2HmacCredential", "fido2HmacSalt") {
3561 r
= drop_from_identity(p
);
3566 if (isempty(optarg
)) {
3567 arg_fido2_device
= strv_free(arg_fido2_device
);
3571 if (streq(optarg
, "auto")) {
3572 _cleanup_free_
char *found
= NULL
;
3574 r
= fido2_find_device_auto(&found
);
3578 r
= strv_consume(&arg_fido2_device
, TAKE_PTR(found
));
3580 r
= strv_extend(&arg_fido2_device
, optarg
);
3584 strv_uniq(arg_fido2_device
);
3587 case ARG_FIDO2_WITH_PIN
: {
3590 r
= parse_boolean_argument("--fido2-with-client-pin=", optarg
, &lock_with_pin
);
3594 SET_FLAG(arg_fido2_lock_with
, FIDO2ENROLL_PIN
, lock_with_pin
);
3598 case ARG_FIDO2_WITH_UP
: {
3601 r
= parse_boolean_argument("--fido2-with-user-presence=", optarg
, &lock_with_up
);
3605 SET_FLAG(arg_fido2_lock_with
, FIDO2ENROLL_UP
, lock_with_up
);
3609 case ARG_FIDO2_WITH_UV
: {
3612 r
= parse_boolean_argument("--fido2-with-user-verification=", optarg
, &lock_with_uv
);
3616 SET_FLAG(arg_fido2_lock_with
, FIDO2ENROLL_UV
, lock_with_uv
);
3620 case ARG_RECOVERY_KEY
:
3621 r
= parse_boolean(optarg
);
3623 return log_error_errno(r
, "Failed to parse --recovery-key= argument: %s", optarg
);
3625 arg_recovery_key
= r
;
3627 FOREACH_STRING(p
, "recoveryKey", "recoveryKeyType") {
3628 r
= drop_from_identity(p
);
3635 case ARG_AUTO_RESIZE_MODE
:
3636 if (isempty(optarg
)) {
3637 r
= drop_from_identity("autoResizeMode");
3644 r
= auto_resize_mode_from_string(optarg
);
3646 return log_error_errno(r
, "Failed to parse --auto-resize-mode= argument: %s", optarg
);
3648 r
= json_variant_set_field_string(&arg_identity_extra
, "autoResizeMode", auto_resize_mode_to_string(r
));
3650 return log_error_errno(r
, "Failed to set autoResizeMode field: %m");
3654 case ARG_REBALANCE_WEIGHT
: {
3657 if (isempty(optarg
)) {
3658 r
= drop_from_identity("rebalanceWeight");
3663 if (streq(optarg
, "off"))
3664 u
= REBALANCE_WEIGHT_OFF
;
3666 r
= safe_atou64(optarg
, &u
);
3668 return log_error_errno(r
, "Failed to parse --rebalance-weight= argument: %s", optarg
);
3670 if (u
< REBALANCE_WEIGHT_MIN
|| u
> REBALANCE_WEIGHT_MAX
)
3671 return log_error_errno(SYNTHETIC_ERRNO(ERANGE
), "Rebalancing weight out of valid range %" PRIu64
"%s%" PRIu64
": %s",
3672 REBALANCE_WEIGHT_MIN
, special_glyph(SPECIAL_GLYPH_ELLIPSIS
), REBALANCE_WEIGHT_MAX
, optarg
);
3675 /* Drop from per machine stuff and everywhere */
3676 r
= drop_from_identity("rebalanceWeight");
3680 /* Add to main identity */
3681 r
= json_variant_set_field_unsigned(&arg_identity_extra
, "rebalanceWeight", u
);
3683 return log_error_errno(r
, "Failed to set rebalanceWeight field: %m");
3689 arg_json_format_flags
= JSON_FORMAT_PRETTY_AUTO
|JSON_FORMAT_COLOR_AUTO
;
3693 r
= parse_json_argument(optarg
, &arg_json_format_flags
);
3700 if (arg_export_format
== EXPORT_FORMAT_FULL
)
3701 arg_export_format
= EXPORT_FORMAT_STRIPPED
;
3702 else if (arg_export_format
== EXPORT_FORMAT_STRIPPED
)
3703 arg_export_format
= EXPORT_FORMAT_MINIMAL
;
3705 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Specifying -E more than twice is not supported.");
3707 arg_json_format_flags
&= ~JSON_FORMAT_OFF
;
3708 if (arg_json_format_flags
== 0)
3709 arg_json_format_flags
= JSON_FORMAT_PRETTY_AUTO
|JSON_FORMAT_COLOR_AUTO
;
3712 case ARG_EXPORT_FORMAT
:
3713 if (streq(optarg
, "full"))
3714 arg_export_format
= EXPORT_FORMAT_FULL
;
3715 else if (streq(optarg
, "stripped"))
3716 arg_export_format
= EXPORT_FORMAT_STRIPPED
;
3717 else if (streq(optarg
, "minimal"))
3718 arg_export_format
= EXPORT_FORMAT_MINIMAL
;
3719 else if (streq(optarg
, "help")) {
3728 case ARG_AND_RESIZE
:
3729 arg_and_resize
= true;
3732 case ARG_AND_CHANGE_PASSWORD
:
3733 arg_and_change_password
= true;
3736 case ARG_DROP_CACHES
: {
3739 if (isempty(optarg
)) {
3740 r
= drop_from_identity("dropCaches");
3745 r
= parse_boolean_argument("--drop-caches=", optarg
, &drop_caches
);
3749 r
= json_variant_set_field_boolean(&arg_identity_extra
, "dropCaches", r
);
3751 return log_error_errno(r
, "Failed to set drop caches field: %m");
3760 assert_not_reached();
3764 if (!strv_isempty(arg_pkcs11_token_uri
) || !strv_isempty(arg_fido2_device
))
3765 arg_and_change_password
= true;
3767 if (arg_disk_size
!= UINT64_MAX
|| arg_disk_size_relative
!= UINT64_MAX
)
3768 arg_and_resize
= true;
3773 static int redirect_bus_mgr(void) {
3776 /* Talk to a different service if that's requested. (The same env var is also understood by homed, so
3777 * that it is relatively easily possible to invoke a second instance of homed for debug purposes and
3778 * have homectl talk to it, without colliding with the host version. This is handy when operating
3779 * from a homed-managed account.) */
3781 suffix
= getenv("SYSTEMD_HOME_DEBUG_SUFFIX");
3783 static BusLocator locator
= {
3784 .path
= "/org/freedesktop/home1",
3785 .interface
= "org.freedesktop.home1.Manager",
3788 /* Yes, we leak this memory, but there's little point to collect this, given that we only do
3789 * this in a debug environment, do it only once, and the string shall live for out entire
3790 * process runtime. */
3792 locator
.destination
= strjoin("org.freedesktop.home1.", suffix
);
3793 if (!locator
.destination
)
3798 bus_mgr
= bus_home_mgr
;
3803 static int run(int argc
, char *argv
[]) {
3804 static const Verb verbs
[] = {
3805 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
3806 { "list", VERB_ANY
, 1, VERB_DEFAULT
, list_homes
},
3807 { "activate", 2, VERB_ANY
, 0, activate_home
},
3808 { "deactivate", 2, VERB_ANY
, 0, deactivate_home
},
3809 { "inspect", VERB_ANY
, VERB_ANY
, 0, inspect_home
},
3810 { "authenticate", VERB_ANY
, VERB_ANY
, 0, authenticate_home
},
3811 { "create", VERB_ANY
, 2, 0, create_home
},
3812 { "remove", 2, VERB_ANY
, 0, remove_home
},
3813 { "update", VERB_ANY
, 2, 0, update_home
},
3814 { "passwd", VERB_ANY
, 2, 0, passwd_home
},
3815 { "resize", 2, 3, 0, resize_home
},
3816 { "lock", 2, VERB_ANY
, 0, lock_home
},
3817 { "unlock", 2, VERB_ANY
, 0, unlock_home
},
3818 { "with", 2, VERB_ANY
, 0, with_home
},
3819 { "lock-all", VERB_ANY
, 1, 0, lock_all_homes
},
3820 { "deactivate-all", VERB_ANY
, 1, 0, deactivate_all_homes
},
3821 { "rebalance", VERB_ANY
, 1, 0, rebalance
},
3829 r
= redirect_bus_mgr();
3833 r
= parse_argv(argc
, argv
);
3837 return dispatch_verb(argc
, argv
, verbs
, NULL
);
3840 DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run
);