1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
7 #include "ask-password-api.h"
8 #include "bus-common-errors.h"
10 #include "bus-locator.h"
11 #include "cgroup-util.h"
12 #include "dns-domain.h"
16 #include "format-table.h"
17 #include "home-util.h"
18 #include "homectl-fido2.h"
19 #include "homectl-pkcs11.h"
20 #include "homectl-recovery-key.h"
21 #include "libfido2-util.h"
22 #include "locale-util.h"
23 #include "main-func.h"
24 #include "memory-util.h"
26 #include "parse-argument.h"
27 #include "parse-util.h"
28 #include "path-util.h"
29 #include "percent-util.h"
30 #include "pkcs11-util.h"
31 #include "pretty-print.h"
32 #include "process-util.h"
33 #include "pwquality-util.h"
34 #include "rlimit-util.h"
35 #include "spawn-polkit-agent.h"
36 #include "terminal-util.h"
37 #include "user-record-pwquality.h"
38 #include "user-record-show.h"
39 #include "user-record-util.h"
40 #include "user-record.h"
41 #include "user-util.h"
44 static PagerFlags arg_pager_flags
= 0;
45 static bool arg_legend
= true;
46 static bool arg_ask_password
= true;
47 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
48 static const char *arg_host
= NULL
;
49 static const char *arg_identity
= NULL
;
50 static JsonVariant
*arg_identity_extra
= NULL
;
51 static JsonVariant
*arg_identity_extra_privileged
= NULL
;
52 static JsonVariant
*arg_identity_extra_this_machine
= NULL
;
53 static JsonVariant
*arg_identity_extra_rlimits
= NULL
;
54 static char **arg_identity_filter
= NULL
; /* this one is also applied to 'privileged' and 'thisMachine' subobjects */
55 static char **arg_identity_filter_rlimits
= NULL
;
56 static uint64_t arg_disk_size
= UINT64_MAX
;
57 static uint64_t arg_disk_size_relative
= UINT64_MAX
;
58 static char **arg_pkcs11_token_uri
= NULL
;
59 static char **arg_fido2_device
= NULL
;
60 static bool arg_recovery_key
= false;
61 static JsonFormatFlags arg_json_format_flags
= JSON_FORMAT_OFF
;
62 static bool arg_and_resize
= false;
63 static bool arg_and_change_password
= false;
65 EXPORT_FORMAT_FULL
, /* export the full record */
66 EXPORT_FORMAT_STRIPPED
, /* strip "state" + "binding", but leave signature in place */
67 EXPORT_FORMAT_MINIMAL
, /* also strip signature */
68 } arg_export_format
= EXPORT_FORMAT_FULL
;
70 STATIC_DESTRUCTOR_REGISTER(arg_identity_extra
, json_variant_unrefp
);
71 STATIC_DESTRUCTOR_REGISTER(arg_identity_extra_this_machine
, json_variant_unrefp
);
72 STATIC_DESTRUCTOR_REGISTER(arg_identity_extra_privileged
, json_variant_unrefp
);
73 STATIC_DESTRUCTOR_REGISTER(arg_identity_extra_rlimits
, json_variant_unrefp
);
74 STATIC_DESTRUCTOR_REGISTER(arg_identity_filter
, strv_freep
);
75 STATIC_DESTRUCTOR_REGISTER(arg_identity_filter_rlimits
, strv_freep
);
76 STATIC_DESTRUCTOR_REGISTER(arg_pkcs11_token_uri
, strv_freep
);
77 STATIC_DESTRUCTOR_REGISTER(arg_fido2_device
, strv_freep
);
79 static const BusLocator
*bus_mgr
;
81 static bool identity_properties_specified(void) {
84 !json_variant_is_blank_object(arg_identity_extra
) ||
85 !json_variant_is_blank_object(arg_identity_extra_privileged
) ||
86 !json_variant_is_blank_object(arg_identity_extra_this_machine
) ||
87 !json_variant_is_blank_object(arg_identity_extra_rlimits
) ||
88 !strv_isempty(arg_identity_filter
) ||
89 !strv_isempty(arg_identity_filter_rlimits
) ||
90 !strv_isempty(arg_pkcs11_token_uri
) ||
91 !strv_isempty(arg_fido2_device
);
94 static int acquire_bus(sd_bus
**bus
) {
102 r
= bus_connect_transport(arg_transport
, arg_host
, false, bus
);
104 return bus_log_connect_error(r
);
106 (void) sd_bus_set_allow_interactive_authorization(*bus
, arg_ask_password
);
111 static int list_homes(int argc
, char *argv
[], void *userdata
) {
112 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
113 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
114 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
115 _cleanup_(table_unrefp
) Table
*table
= NULL
;
118 r
= acquire_bus(&bus
);
122 r
= bus_call_method(bus
, bus_mgr
, "ListHomes", &error
, &reply
, NULL
);
124 return log_error_errno(r
, "Failed to list homes: %s", bus_error_message(&error
, r
));
126 table
= table_new("name", "uid", "gid", "state", "realname", "home", "shell");
130 r
= sd_bus_message_enter_container(reply
, 'a', "(susussso)");
132 return bus_log_parse_error(r
);
135 const char *name
, *state
, *realname
, *home
, *shell
, *color
;
139 r
= sd_bus_message_read(reply
, "(susussso)", &name
, &uid
, &state
, &gid
, &realname
, &home
, &shell
, NULL
);
141 return bus_log_parse_error(r
);
145 r
= table_add_many(table
,
150 return table_log_add_error(r
);
153 r
= table_add_cell(table
, &cell
, TABLE_STRING
, state
);
155 return table_log_add_error(r
);
157 color
= user_record_state_color(state
);
159 (void) table_set_color(table
, cell
, color
);
161 r
= table_add_many(table
,
162 TABLE_STRING
, strna(empty_to_null(realname
)),
164 TABLE_STRING
, strna(empty_to_null(shell
)));
166 return table_log_add_error(r
);
169 r
= sd_bus_message_exit_container(reply
);
171 return bus_log_parse_error(r
);
173 if (table_get_rows(table
) > 1 || !FLAGS_SET(arg_json_format_flags
, JSON_FORMAT_OFF
)) {
174 r
= table_set_sort(table
, (size_t) 0);
176 return table_log_sort_error(r
);
178 r
= table_print_with_pager(table
, arg_json_format_flags
, arg_pager_flags
, arg_legend
);
183 if (arg_legend
&& (arg_json_format_flags
& JSON_FORMAT_OFF
)) {
184 if (table_get_rows(table
) > 1)
185 printf("\n%zu home areas listed.\n", table_get_rows(table
) - 1);
187 printf("No home areas.\n");
193 static int acquire_existing_password(
194 const char *user_name
,
196 bool emphasize_current
,
197 AskPasswordFlags flags
) {
199 _cleanup_(strv_free_erasep
) char **password
= NULL
;
200 _cleanup_free_
char *question
= NULL
;
207 e
= getenv("PASSWORD");
209 /* People really shouldn't use environment variables for passing passwords. We support this
210 * only for testing purposes, and do not document the behaviour, so that people won't
211 * actually use this outside of testing. */
213 r
= user_record_set_password(hr
, STRV_MAKE(e
), true);
215 return log_error_errno(r
, "Failed to store password: %m");
218 assert_se(unsetenv("PASSWORD") == 0);
223 /* If this is not our own user, then don't use the password cache */
224 if (is_this_me(user_name
) <= 0)
225 SET_FLAG(flags
, ASK_PASSWORD_ACCEPT_CACHED
|ASK_PASSWORD_PUSH_CACHE
, false);
227 if (asprintf(&question
, emphasize_current
?
228 "Please enter current password for user %s:" :
229 "Please enter password for user %s:",
233 r
= ask_password_auto(question
,
234 /* icon= */ "user-home",
236 /* key_name= */ "home-password",
237 /* credential_name= */ "home.password",
241 if (r
== -EUNATCH
) { /* EUNATCH is returned if no password was found and asking interactively was
242 * disabled via the flags. Not an error for us. */
243 log_debug_errno(r
, "No passwords acquired.");
247 return log_error_errno(r
, "Failed to acquire password: %m");
249 r
= user_record_set_password(hr
, password
, true);
251 return log_error_errno(r
, "Failed to store password: %m");
256 static int acquire_token_pin(
257 const char *user_name
,
259 AskPasswordFlags flags
) {
261 _cleanup_(strv_free_erasep
) char **pin
= NULL
;
262 _cleanup_free_
char *question
= NULL
;
271 r
= user_record_set_token_pin(hr
, STRV_MAKE(e
), false);
273 return log_error_errno(r
, "Failed to store token PIN: %m");
276 assert_se(unsetenv("PIN") == 0);
281 /* If this is not our own user, then don't use the password cache */
282 if (is_this_me(user_name
) <= 0)
283 SET_FLAG(flags
, ASK_PASSWORD_ACCEPT_CACHED
|ASK_PASSWORD_PUSH_CACHE
, false);
285 if (asprintf(&question
, "Please enter security token PIN for user %s:", user_name
) < 0)
288 r
= ask_password_auto(
290 /* icon= */ "user-home",
292 /* key_name= */ "token-pin",
293 /* credential_name= */ "home.token-pin",
297 if (r
== -EUNATCH
) { /* EUNATCH is returned if no PIN was found and asking interactively was disabled
298 * via the flags. Not an error for us. */
299 log_debug_errno(r
, "No security token PINs acquired.");
303 return log_error_errno(r
, "Failed to acquire security token PIN: %m");
305 r
= user_record_set_token_pin(hr
, pin
, false);
307 return log_error_errno(r
, "Failed to store security token PIN: %m");
312 static int handle_generic_user_record_error(
313 const char *user_name
,
315 const sd_bus_error
*error
,
317 bool emphasize_current_password
) {
323 if (sd_bus_error_has_name(error
, BUS_ERROR_HOME_ABSENT
))
324 return log_error_errno(SYNTHETIC_ERRNO(EREMOTE
),
325 "Home of user %s is currently absent, please plug in the necessary storage device or backing file system.", user_name
);
327 else if (sd_bus_error_has_name(error
, BUS_ERROR_AUTHENTICATION_LIMIT_HIT
))
328 return log_error_errno(SYNTHETIC_ERRNO(ETOOMANYREFS
),
329 "Too frequent unsuccessful login attempts for user %s, try again later.", user_name
);
331 else if (sd_bus_error_has_name(error
, BUS_ERROR_BAD_PASSWORD
)) {
333 if (!strv_isempty(hr
->password
))
334 log_notice("Password incorrect or not sufficient, please try again.");
336 /* Don't consume cache entries or credentials here, we already tried that unsuccessfully. But
337 * let's push what we acquire here into the cache */
338 r
= acquire_existing_password(
341 emphasize_current_password
,
342 ASK_PASSWORD_PUSH_CACHE
| ASK_PASSWORD_NO_CREDENTIAL
);
346 } else if (sd_bus_error_has_name(error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
)) {
348 if (strv_isempty(hr
->password
))
349 log_notice("Security token not inserted, please enter password.");
351 log_notice("Password incorrect or not sufficient, and configured security token not inserted, please try again.");
353 r
= acquire_existing_password(
356 emphasize_current_password
,
357 ASK_PASSWORD_PUSH_CACHE
| ASK_PASSWORD_NO_CREDENTIAL
);
361 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_PIN_NEEDED
)) {
363 /* First time the PIN is requested, let's accept cached data, and allow using credential store */
364 r
= acquire_token_pin(
367 ASK_PASSWORD_ACCEPT_CACHED
| ASK_PASSWORD_PUSH_CACHE
);
371 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_PROTECTED_AUTHENTICATION_PATH_NEEDED
)) {
373 log_notice("%s%sPlease authenticate physically on security token.",
374 emoji_enabled() ? special_glyph(SPECIAL_GLYPH_TOUCH
) : "",
375 emoji_enabled() ? " " : "");
377 r
= user_record_set_pkcs11_protected_authentication_path_permitted(hr
, true);
379 return log_error_errno(r
, "Failed to set PKCS#11 protected authentication path permitted flag: %m");
381 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_USER_PRESENCE_NEEDED
)) {
383 log_notice("%s%sAuthentication requires presence verification on security token.",
384 emoji_enabled() ? special_glyph(SPECIAL_GLYPH_TOUCH
) : "",
385 emoji_enabled() ? " " : "");
387 r
= user_record_set_fido2_user_presence_permitted(hr
, true);
389 return log_error_errno(r
, "Failed to set FIDO2 user presence permitted flag: %m");
391 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_PIN_LOCKED
))
392 return log_error_errno(SYNTHETIC_ERRNO(EPERM
), "Security token PIN is locked, please unlock it first. (Hint: Removal and re-insertion might suffice.)");
394 else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_BAD_PIN
)) {
396 log_notice("Security token PIN incorrect, please try again.");
398 /* If the previous PIN was wrong don't accept cached info anymore, but add to cache. Also, don't use the credential data */
399 r
= acquire_token_pin(
402 ASK_PASSWORD_PUSH_CACHE
| ASK_PASSWORD_NO_CREDENTIAL
);
406 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_BAD_PIN_FEW_TRIES_LEFT
)) {
408 log_notice("Security token PIN incorrect, please try again (only a few tries left!).");
410 r
= acquire_token_pin(
413 ASK_PASSWORD_PUSH_CACHE
| ASK_PASSWORD_NO_CREDENTIAL
);
417 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_BAD_PIN_ONE_TRY_LEFT
)) {
419 log_notice("Security token PIN incorrect, please try again (only one try left!).");
421 r
= acquire_token_pin(
424 ASK_PASSWORD_PUSH_CACHE
| ASK_PASSWORD_NO_CREDENTIAL
);
428 return log_error_errno(ret
, "Operation on home %s failed: %s", user_name
, bus_error_message(error
, ret
));
433 static int acquire_passed_secrets(const char *user_name
, UserRecord
**ret
) {
434 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
439 /* Generates an initial secret objects that contains passwords supplied via $PASSWORD, the password
440 * cache or the credentials subsystem, but excluding any interactive stuff. If nothing is passed,
441 * returns an empty secret object. */
443 secret
= user_record_new();
447 r
= acquire_existing_password(
450 /* emphasize_current_password = */ false,
451 ASK_PASSWORD_ACCEPT_CACHED
| ASK_PASSWORD_NO_TTY
| ASK_PASSWORD_NO_AGENT
);
455 r
= acquire_token_pin(
458 ASK_PASSWORD_ACCEPT_CACHED
| ASK_PASSWORD_NO_TTY
| ASK_PASSWORD_NO_AGENT
);
462 *ret
= TAKE_PTR(secret
);
466 static int activate_home(int argc
, char *argv
[], void *userdata
) {
467 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
471 r
= acquire_bus(&bus
);
475 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
476 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
478 r
= acquire_passed_secrets(*i
, &secret
);
483 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
484 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
486 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ActivateHome");
488 return bus_log_create_error(r
);
490 r
= sd_bus_message_append(m
, "s", *i
);
492 return bus_log_create_error(r
);
494 r
= bus_message_append_secret(m
, secret
);
496 return bus_log_create_error(r
);
498 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
500 r
= handle_generic_user_record_error(*i
, secret
, &error
, r
, /* emphasize_current_password= */ false);
515 static int deactivate_home(int argc
, char *argv
[], void *userdata
) {
516 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
520 r
= acquire_bus(&bus
);
524 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
525 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
526 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
528 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "DeactivateHome");
530 return bus_log_create_error(r
);
532 r
= sd_bus_message_append(m
, "s", *i
);
534 return bus_log_create_error(r
);
536 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
538 log_error_errno(r
, "Failed to deactivate user home: %s", bus_error_message(&error
, r
));
547 static void dump_home_record(UserRecord
*hr
) {
552 if (hr
->incomplete
) {
554 log_warning("Warning: lacking rights to acquire privileged fields of user record of '%s', output incomplete.", hr
->user_name
);
557 if (arg_json_format_flags
& JSON_FORMAT_OFF
)
558 user_record_show(hr
, true);
560 _cleanup_(user_record_unrefp
) UserRecord
*stripped
= NULL
;
562 if (arg_export_format
== EXPORT_FORMAT_STRIPPED
)
563 r
= user_record_clone(hr
, USER_RECORD_EXTRACT_EMBEDDED
, &stripped
);
564 else if (arg_export_format
== EXPORT_FORMAT_MINIMAL
)
565 r
= user_record_clone(hr
, USER_RECORD_EXTRACT_SIGNABLE
, &stripped
);
569 log_warning_errno(r
, "Failed to strip user record, ignoring: %m");
573 json_variant_dump(hr
->json
, arg_json_format_flags
, stdout
, NULL
);
577 static char **mangle_user_list(char **list
, char ***ret_allocated
) {
578 _cleanup_free_
char *myself
= NULL
;
581 if (!strv_isempty(list
)) {
582 *ret_allocated
= NULL
;
586 myself
= getusername_malloc();
594 l
[0] = TAKE_PTR(myself
);
601 static int inspect_home(int argc
, char *argv
[], void *userdata
) {
602 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
603 _cleanup_(strv_freep
) char **mangled_list
= NULL
;
607 (void) pager_open(arg_pager_flags
);
609 r
= acquire_bus(&bus
);
613 items
= mangle_user_list(strv_skip(argv
, 1), &mangled_list
);
617 STRV_FOREACH(i
, items
) {
618 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
619 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
620 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
621 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
626 r
= parse_uid(*i
, &uid
);
628 if (!valid_user_group_name(*i
, 0)) {
629 log_error("Invalid user name '%s'.", *i
);
636 r
= bus_call_method(bus
, bus_mgr
, "GetUserRecordByName", &error
, &reply
, "s", *i
);
638 r
= bus_call_method(bus
, bus_mgr
, "GetUserRecordByUID", &error
, &reply
, "u", (uint32_t) uid
);
641 log_error_errno(r
, "Failed to inspect home: %s", bus_error_message(&error
, r
));
648 r
= sd_bus_message_read(reply
, "sbo", &json
, &incomplete
, NULL
);
650 bus_log_parse_error(r
);
657 r
= json_parse(json
, JSON_PARSE_SENSITIVE
, &v
, NULL
, NULL
);
659 log_error_errno(r
, "Failed to parse JSON identity: %m");
666 hr
= user_record_new();
670 r
= user_record_load(hr
, v
, USER_RECORD_LOAD_REFUSE_SECRET
|USER_RECORD_LOG
);
678 hr
->incomplete
= incomplete
;
679 dump_home_record(hr
);
685 static int authenticate_home(int argc
, char *argv
[], void *userdata
) {
686 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
687 _cleanup_(strv_freep
) char **mangled_list
= NULL
;
691 items
= mangle_user_list(strv_skip(argv
, 1), &mangled_list
);
695 r
= acquire_bus(&bus
);
699 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
701 STRV_FOREACH(i
, items
) {
702 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
704 r
= acquire_passed_secrets(*i
, &secret
);
709 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
710 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
712 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "AuthenticateHome");
714 return bus_log_create_error(r
);
716 r
= sd_bus_message_append(m
, "s", *i
);
718 return bus_log_create_error(r
);
720 r
= bus_message_append_secret(m
, secret
);
722 return bus_log_create_error(r
);
724 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
726 r
= handle_generic_user_record_error(*i
, secret
, &error
, r
, false);
741 static int update_last_change(JsonVariant
**v
, bool with_password
, bool override
) {
748 n
= now(CLOCK_REALTIME
);
750 c
= json_variant_by_key(*v
, "lastChangeUSec");
755 goto update_password
;
757 if (!json_variant_is_unsigned(c
))
758 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastChangeUSec field is not an unsigned integer, refusing.");
760 u
= json_variant_unsigned(c
);
762 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastChangeUSec is from the future, can't update.");
765 r
= json_variant_set_field_unsigned(v
, "lastChangeUSec", n
);
767 return log_error_errno(r
, "Failed to update lastChangeUSec: %m");
773 c
= json_variant_by_key(*v
, "lastPasswordChangeUSec");
780 if (!json_variant_is_unsigned(c
))
781 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastPasswordChangeUSec field is not an unsigned integer, refusing.");
783 u
= json_variant_unsigned(c
);
785 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastPasswordChangeUSec is from the future, can't update.");
788 r
= json_variant_set_field_unsigned(v
, "lastPasswordChangeUSec", n
);
790 return log_error_errno(r
, "Failed to update lastPasswordChangeUSec: %m");
795 static int apply_identity_changes(JsonVariant
**_v
) {
796 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
801 v
= json_variant_ref(*_v
);
803 r
= json_variant_filter(&v
, arg_identity_filter
);
805 return log_error_errno(r
, "Failed to filter identity: %m");
807 r
= json_variant_merge(&v
, arg_identity_extra
);
809 return log_error_errno(r
, "Failed to merge identities: %m");
811 if (arg_identity_extra_this_machine
|| !strv_isempty(arg_identity_filter
)) {
812 _cleanup_(json_variant_unrefp
) JsonVariant
*per_machine
= NULL
, *mmid
= NULL
;
813 char mids
[SD_ID128_STRING_MAX
];
816 r
= sd_id128_get_machine(&mid
);
818 return log_error_errno(r
, "Failed to acquire machine ID: %m");
820 r
= json_variant_new_string(&mmid
, sd_id128_to_string(mid
, mids
));
822 return log_error_errno(r
, "Failed to allocate matchMachineId object: %m");
824 per_machine
= json_variant_ref(json_variant_by_key(v
, "perMachine"));
826 _cleanup_(json_variant_unrefp
) JsonVariant
*npm
= NULL
, *add
= NULL
;
827 _cleanup_free_ JsonVariant
**array
= NULL
;
831 if (!json_variant_is_array(per_machine
))
832 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "perMachine field is not an array, refusing.");
834 array
= new(JsonVariant
*, json_variant_elements(per_machine
) + 1);
838 JSON_VARIANT_ARRAY_FOREACH(z
, per_machine
) {
841 if (!json_variant_is_object(z
))
842 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "perMachine entry is not an object, refusing.");
846 u
= json_variant_by_key(z
, "matchMachineId");
850 if (!json_variant_equal(u
, mmid
))
853 r
= json_variant_merge(&add
, z
);
855 return log_error_errno(r
, "Failed to merge perMachine entry: %m");
860 r
= json_variant_filter(&add
, arg_identity_filter
);
862 return log_error_errno(r
, "Failed to filter perMachine: %m");
864 r
= json_variant_merge(&add
, arg_identity_extra_this_machine
);
866 return log_error_errno(r
, "Failed to merge in perMachine fields: %m");
868 if (arg_identity_filter_rlimits
|| arg_identity_extra_rlimits
) {
869 _cleanup_(json_variant_unrefp
) JsonVariant
*rlv
= NULL
;
871 rlv
= json_variant_ref(json_variant_by_key(add
, "resourceLimits"));
873 r
= json_variant_filter(&rlv
, arg_identity_filter_rlimits
);
875 return log_error_errno(r
, "Failed to filter resource limits: %m");
877 r
= json_variant_merge(&rlv
, arg_identity_extra_rlimits
);
879 return log_error_errno(r
, "Failed to set resource limits: %m");
881 if (json_variant_is_blank_object(rlv
)) {
882 r
= json_variant_filter(&add
, STRV_MAKE("resourceLimits"));
884 return log_error_errno(r
, "Failed to drop resource limits field from identity: %m");
886 r
= json_variant_set_field(&add
, "resourceLimits", rlv
);
888 return log_error_errno(r
, "Failed to update resource limits of identity: %m");
892 if (!json_variant_is_blank_object(add
)) {
893 r
= json_variant_set_field(&add
, "matchMachineId", mmid
);
895 return log_error_errno(r
, "Failed to set matchMachineId field: %m");
900 r
= json_variant_new_array(&npm
, array
, i
);
902 return log_error_errno(r
, "Failed to allocate new perMachine array: %m");
904 json_variant_unref(per_machine
);
905 per_machine
= TAKE_PTR(npm
);
907 _cleanup_(json_variant_unrefp
) JsonVariant
*item
= json_variant_ref(arg_identity_extra_this_machine
);
909 if (arg_identity_extra_rlimits
) {
910 r
= json_variant_set_field(&item
, "resourceLimits", arg_identity_extra_rlimits
);
912 return log_error_errno(r
, "Failed to update resource limits of identity: %m");
915 r
= json_variant_set_field(&item
, "matchMachineId", mmid
);
917 return log_error_errno(r
, "Failed to set matchMachineId field: %m");
919 r
= json_variant_append_array(&per_machine
, item
);
921 return log_error_errno(r
, "Failed to append to perMachine array: %m");
924 r
= json_variant_set_field(&v
, "perMachine", per_machine
);
926 return log_error_errno(r
, "Failed to update per machine record: %m");
929 if (arg_identity_extra_privileged
|| arg_identity_filter
) {
930 _cleanup_(json_variant_unrefp
) JsonVariant
*privileged
= NULL
;
932 privileged
= json_variant_ref(json_variant_by_key(v
, "privileged"));
934 r
= json_variant_filter(&privileged
, arg_identity_filter
);
936 return log_error_errno(r
, "Failed to filter identity (privileged part): %m");
938 r
= json_variant_merge(&privileged
, arg_identity_extra_privileged
);
940 return log_error_errno(r
, "Failed to merge identities (privileged part): %m");
942 if (json_variant_is_blank_object(privileged
)) {
943 r
= json_variant_filter(&v
, STRV_MAKE("privileged"));
945 return log_error_errno(r
, "Failed to drop privileged part from identity: %m");
947 r
= json_variant_set_field(&v
, "privileged", privileged
);
949 return log_error_errno(r
, "Failed to update privileged part of identity: %m");
953 if (arg_identity_filter_rlimits
) {
954 _cleanup_(json_variant_unrefp
) JsonVariant
*rlv
= NULL
;
956 rlv
= json_variant_ref(json_variant_by_key(v
, "resourceLimits"));
958 r
= json_variant_filter(&rlv
, arg_identity_filter_rlimits
);
960 return log_error_errno(r
, "Failed to filter resource limits: %m");
962 /* Note that we only filter resource limits here, but don't apply them. We do that in the perMachine section */
964 if (json_variant_is_blank_object(rlv
)) {
965 r
= json_variant_filter(&v
, STRV_MAKE("resourceLimits"));
967 return log_error_errno(r
, "Failed to drop resource limits field from identity: %m");
969 r
= json_variant_set_field(&v
, "resourceLimits", rlv
);
971 return log_error_errno(r
, "Failed to update resource limits of identity: %m");
975 json_variant_unref(*_v
);
981 static int add_disposition(JsonVariant
**v
) {
986 if (json_variant_by_key(*v
, "disposition"))
989 /* Set the disposition to regular, if not configured explicitly */
990 r
= json_variant_set_field_string(v
, "disposition", "regular");
992 return log_error_errno(r
, "Failed to set disposition field: %m");
997 static int acquire_new_home_record(UserRecord
**ret
) {
998 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
999 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
1006 unsigned line
, column
;
1008 r
= json_parse_file(
1009 streq(arg_identity
, "-") ? stdin
: NULL
,
1010 streq(arg_identity
, "-") ? "<stdin>" : arg_identity
, JSON_PARSE_SENSITIVE
, &v
, &line
, &column
);
1012 return log_error_errno(r
, "Failed to parse identity at %u:%u: %m", line
, column
);
1015 r
= apply_identity_changes(&v
);
1019 r
= add_disposition(&v
);
1023 STRV_FOREACH(i
, arg_pkcs11_token_uri
) {
1024 r
= identity_add_pkcs11_key_data(&v
, *i
);
1029 STRV_FOREACH(i
, arg_fido2_device
) {
1030 r
= identity_add_fido2_parameters(&v
, *i
);
1035 if (arg_recovery_key
) {
1036 r
= identity_add_recovery_key(&v
);
1041 r
= update_last_change(&v
, true, false);
1046 json_variant_dump(v
, JSON_FORMAT_PRETTY
, NULL
, NULL
);
1048 hr
= user_record_new();
1052 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
);
1056 *ret
= TAKE_PTR(hr
);
1060 static int acquire_new_password(
1061 const char *user_name
,
1073 e
= getenv("NEWPASSWORD");
1075 _cleanup_(erase_and_freep
) char *copy
= NULL
;
1077 /* As above, this is not for use, just for testing */
1085 r
= user_record_set_password(hr
, STRV_MAKE(e
), /* prepend = */ true);
1087 return log_error_errno(r
, "Failed to store password: %m");
1090 assert_se(unsetenv("NEWPASSWORD") == 0);
1093 *ret
= TAKE_PTR(copy
);
1099 (void) suggest_passwords();
1102 _cleanup_(strv_free_erasep
) char **first
= NULL
, **second
= NULL
;
1103 _cleanup_free_
char *question
= NULL
;
1106 return log_error_errno(SYNTHETIC_ERRNO(ENOKEY
), "Too many attempts, giving up:");
1108 if (asprintf(&question
, "Please enter new password for user %s:", user_name
) < 0)
1111 r
= ask_password_auto(
1113 /* icon= */ "user-home",
1115 /* key_name= */ "home-password",
1116 /* credential_name= */ "home.new-password",
1118 0, /* no caching, we want to collect a new password here after all */
1121 return log_error_errno(r
, "Failed to acquire password: %m");
1123 question
= mfree(question
);
1124 if (asprintf(&question
, "Please enter new password for user %s (repeat):", user_name
) < 0)
1127 r
= ask_password_auto(
1129 /* icon= */ "user-home",
1131 /* key_name= */ "home-password",
1132 /* credential_name= */ "home.new-password",
1137 return log_error_errno(r
, "Failed to acquire password: %m");
1139 if (strv_equal(first
, second
)) {
1140 _cleanup_(erase_and_freep
) char *copy
= NULL
;
1143 copy
= strdup(first
[0]);
1148 r
= user_record_set_password(hr
, first
, /* prepend = */ true);
1150 return log_error_errno(r
, "Failed to store password: %m");
1153 *ret
= TAKE_PTR(copy
);
1158 log_error("Password didn't match, try again.");
1162 static int create_home(int argc
, char *argv
[], void *userdata
) {
1163 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1164 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
1167 r
= acquire_bus(&bus
);
1171 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1174 /* If a username was specified, use it */
1176 if (valid_user_group_name(argv
[1], 0))
1177 r
= json_variant_set_field_string(&arg_identity_extra
, "userName", argv
[1]);
1179 _cleanup_free_
char *un
= NULL
, *rr
= NULL
;
1181 /* Before we consider the user name invalid, let's check if we can split it? */
1182 r
= split_user_name_realm(argv
[1], &un
, &rr
);
1184 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User name '%s' is not valid: %m", argv
[1]);
1187 r
= json_variant_set_field_string(&arg_identity_extra
, "realm", rr
);
1189 return log_error_errno(r
, "Failed to set realm field: %m");
1192 r
= json_variant_set_field_string(&arg_identity_extra
, "userName", un
);
1195 return log_error_errno(r
, "Failed to set userName field: %m");
1197 /* If neither a username nor an identity have been specified we cannot operate. */
1199 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User name required.");
1202 r
= acquire_new_home_record(&hr
);
1206 /* If the JSON record carries no plain text password (besides the recovery key), then let's query it
1208 if (strv_length(hr
->password
) <= arg_recovery_key
) {
1210 if (strv_isempty(hr
->hashed_password
)) {
1211 _cleanup_(erase_and_freep
) char *new_password
= NULL
;
1213 /* No regular (i.e. non-PKCS#11) hashed passwords set in the record, let's fix that. */
1214 r
= acquire_new_password(hr
->user_name
, hr
, /* suggest = */ true, &new_password
);
1218 r
= user_record_make_hashed_password(hr
, STRV_MAKE(new_password
), /* extend = */ false);
1220 return log_error_errno(r
, "Failed to hash password: %m");
1222 /* There's a hash password set in the record, acquire the unhashed version of it. */
1223 r
= acquire_existing_password(
1226 /* emphasize_current= */ false,
1227 ASK_PASSWORD_ACCEPT_CACHED
| ASK_PASSWORD_PUSH_CACHE
);
1233 if (hr
->enforce_password_policy
== 0) {
1234 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1236 /* If password quality enforcement is disabled, let's at least warn client side */
1238 r
= user_record_quality_check_password(hr
, hr
, &error
);
1240 log_warning_errno(r
, "Specified password does not pass quality checks (%s), proceeding anyway.", bus_error_message(&error
, r
));
1244 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1245 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1246 _cleanup_(erase_and_freep
) char *formatted
= NULL
;
1248 r
= json_variant_format(hr
->json
, 0, &formatted
);
1250 return log_error_errno(r
, "Failed to format user record: %m");
1252 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "CreateHome");
1254 return bus_log_create_error(r
);
1256 (void) sd_bus_message_sensitive(m
);
1258 r
= sd_bus_message_append(m
, "s", formatted
);
1260 return bus_log_create_error(r
);
1262 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1264 if (sd_bus_error_has_name(&error
, BUS_ERROR_LOW_PASSWORD_QUALITY
)) {
1265 _cleanup_(erase_and_freep
) char *new_password
= NULL
;
1267 log_error_errno(r
, "%s", bus_error_message(&error
, r
));
1268 log_info("(Use --enforce-password-policy=no to turn off password quality checks for this account.)");
1270 r
= acquire_new_password(hr
->user_name
, hr
, /* suggest = */ false, &new_password
);
1274 r
= user_record_make_hashed_password(hr
, STRV_MAKE(new_password
), /* extend = */ false);
1276 return log_error_errno(r
, "Failed to hash passwords: %m");
1278 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1289 static int remove_home(int argc
, char *argv
[], void *userdata
) {
1290 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1294 r
= acquire_bus(&bus
);
1298 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1300 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
1301 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1302 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1304 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "RemoveHome");
1306 return bus_log_create_error(r
);
1308 r
= sd_bus_message_append(m
, "s", *i
);
1310 return bus_log_create_error(r
);
1312 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1314 log_error_errno(r
, "Failed to remove home: %s", bus_error_message(&error
, r
));
1323 static int acquire_updated_home_record(
1325 const char *username
,
1328 _cleanup_(json_variant_unrefp
) JsonVariant
*json
= NULL
;
1329 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
1336 unsigned line
, column
;
1339 r
= json_parse_file(
1340 streq(arg_identity
, "-") ? stdin
: NULL
,
1341 streq(arg_identity
, "-") ? "<stdin>" : arg_identity
, JSON_PARSE_SENSITIVE
, &json
, &line
, &column
);
1343 return log_error_errno(r
, "Failed to parse identity at %u:%u: %m", line
, column
);
1345 un
= json_variant_by_key(json
, "userName");
1347 if (!json_variant_is_string(un
) || (username
&& !streq(json_variant_string(un
), username
)))
1348 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User name specified on command line and in JSON record do not match.");
1351 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "No username specified.");
1353 r
= json_variant_set_field_string(&arg_identity_extra
, "userName", username
);
1355 return log_error_errno(r
, "Failed to set userName field: %m");
1359 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1360 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1364 if (!identity_properties_specified())
1365 return log_error_errno(SYNTHETIC_ERRNO(EALREADY
), "No field to change specified.");
1367 r
= bus_call_method(bus
, bus_mgr
, "GetUserRecordByName", &error
, &reply
, "s", username
);
1369 return log_error_errno(r
, "Failed to acquire user home record: %s", bus_error_message(&error
, r
));
1371 r
= sd_bus_message_read(reply
, "sbo", &text
, &incomplete
, NULL
);
1373 return bus_log_parse_error(r
);
1376 return log_error_errno(SYNTHETIC_ERRNO(EACCES
), "Lacking rights to acquire user record including privileged metadata, can't update record.");
1378 r
= json_parse(text
, JSON_PARSE_SENSITIVE
, &json
, NULL
, NULL
);
1380 return log_error_errno(r
, "Failed to parse JSON identity: %m");
1382 reply
= sd_bus_message_unref(reply
);
1384 r
= json_variant_filter(&json
, STRV_MAKE("binding", "status", "signature"));
1386 return log_error_errno(r
, "Failed to strip binding and status from record to update: %m");
1389 r
= apply_identity_changes(&json
);
1393 STRV_FOREACH(i
, arg_pkcs11_token_uri
) {
1394 r
= identity_add_pkcs11_key_data(&json
, *i
);
1399 STRV_FOREACH(i
, arg_fido2_device
) {
1400 r
= identity_add_fido2_parameters(&json
, *i
);
1405 /* If the user supplied a full record, then add in lastChange, but do not override. Otherwise always
1407 r
= update_last_change(&json
, arg_pkcs11_token_uri
|| arg_fido2_device
, !arg_identity
);
1412 json_variant_dump(json
, JSON_FORMAT_PRETTY
, NULL
, NULL
);
1414 hr
= user_record_new();
1418 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
);
1422 *ret
= TAKE_PTR(hr
);
1426 static int home_record_reset_human_interaction_permission(UserRecord
*hr
) {
1431 /* When we execute multiple operations one after the other, let's reset the permission to ask the
1432 * user each time, so that if interaction is necessary we will be told so again and thus can print a
1433 * nice message to the user, telling the user so. */
1435 r
= user_record_set_pkcs11_protected_authentication_path_permitted(hr
, -1);
1437 return log_error_errno(r
, "Failed to reset PKCS#11 protected authentication path permission flag: %m");
1439 r
= user_record_set_fido2_user_presence_permitted(hr
, -1);
1441 return log_error_errno(r
, "Failed to reset FIDO2 user presence permission flag: %m");
1446 static int update_home(int argc
, char *argv
[], void *userdata
) {
1447 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1448 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
1449 _cleanup_free_
char *buffer
= NULL
;
1450 const char *username
;
1455 else if (!arg_identity
) {
1456 buffer
= getusername_malloc();
1464 r
= acquire_bus(&bus
);
1468 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1470 r
= acquire_updated_home_record(bus
, username
, &hr
);
1474 /* If we do multiple operations, let's output things more verbosely, since otherwise the repeated
1475 * authentication might be confusing. */
1477 if (arg_and_resize
|| arg_and_change_password
)
1478 log_info("Updating home directory.");
1481 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1482 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1483 _cleanup_free_
char *formatted
= NULL
;
1485 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "UpdateHome");
1487 return bus_log_create_error(r
);
1489 r
= json_variant_format(hr
->json
, 0, &formatted
);
1491 return log_error_errno(r
, "Failed to format user record: %m");
1493 (void) sd_bus_message_sensitive(m
);
1495 r
= sd_bus_message_append(m
, "s", formatted
);
1497 return bus_log_create_error(r
);
1499 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1501 if (arg_and_change_password
&&
1502 sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1503 /* In the generic handler we'd ask for a password in this case, but when
1504 * changing passwords that's not sufficient, as we need to acquire all keys
1506 return log_error_errno(r
, "Security token not inserted, refusing.");
1508 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1516 log_info("Resizing home.");
1518 (void) home_record_reset_human_interaction_permission(hr
);
1520 /* Also sync down disk size to underlying LUKS/fscrypt/quota */
1521 while (arg_and_resize
) {
1522 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1523 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1525 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ResizeHome");
1527 return bus_log_create_error(r
);
1529 /* Specify UINT64_MAX as size, in which case the underlying disk size will just be synced */
1530 r
= sd_bus_message_append(m
, "st", hr
->user_name
, UINT64_MAX
);
1532 return bus_log_create_error(r
);
1534 r
= bus_message_append_secret(m
, hr
);
1536 return bus_log_create_error(r
);
1538 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1540 if (arg_and_change_password
&&
1541 sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1542 return log_error_errno(r
, "Security token not inserted, refusing.");
1544 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1551 if (arg_and_change_password
)
1552 log_info("Synchronizing passwords and encryption keys.");
1554 (void) home_record_reset_human_interaction_permission(hr
);
1556 /* Also sync down passwords to underlying LUKS/fscrypt */
1557 while (arg_and_change_password
) {
1558 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1559 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1561 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ChangePasswordHome");
1563 return bus_log_create_error(r
);
1565 /* Specify an empty new secret, in which case the underlying LUKS/fscrypt password will just be synced */
1566 r
= sd_bus_message_append(m
, "ss", hr
->user_name
, "{}");
1568 return bus_log_create_error(r
);
1570 r
= bus_message_append_secret(m
, hr
);
1572 return bus_log_create_error(r
);
1574 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1576 if (sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1577 return log_error_errno(r
, "Security token not inserted, refusing.");
1579 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1589 static int passwd_home(int argc
, char *argv
[], void *userdata
) {
1590 _cleanup_(user_record_unrefp
) UserRecord
*old_secret
= NULL
, *new_secret
= NULL
;
1591 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1592 _cleanup_free_
char *buffer
= NULL
;
1593 const char *username
;
1596 if (arg_pkcs11_token_uri
)
1597 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "To change the PKCS#11 security token use 'homectl update --pkcs11-token-uri=…'.");
1598 if (arg_fido2_device
)
1599 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "To change the FIDO2 security token use 'homectl update --fido2-device=…'.");
1600 if (identity_properties_specified())
1601 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "The 'passwd' verb does not permit changing other record properties at the same time.");
1606 buffer
= getusername_malloc();
1613 r
= acquire_bus(&bus
);
1617 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1619 old_secret
= user_record_new();
1623 new_secret
= user_record_new();
1627 r
= acquire_new_password(username
, new_secret
, /* suggest = */ true, NULL
);
1632 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1633 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1635 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ChangePasswordHome");
1637 return bus_log_create_error(r
);
1639 r
= sd_bus_message_append(m
, "s", username
);
1641 return bus_log_create_error(r
);
1643 r
= bus_message_append_secret(m
, new_secret
);
1645 return bus_log_create_error(r
);
1647 r
= bus_message_append_secret(m
, old_secret
);
1649 return bus_log_create_error(r
);
1651 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1653 if (sd_bus_error_has_name(&error
, BUS_ERROR_LOW_PASSWORD_QUALITY
)) {
1655 log_error_errno(r
, "%s", bus_error_message(&error
, r
));
1657 r
= acquire_new_password(username
, new_secret
, /* suggest = */ false, NULL
);
1659 } else if (sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1661 /* In the generic handler we'd ask for a password in this case, but when
1662 * changing passwords that's not sufficeint, as we need to acquire all keys
1664 return log_error_errno(r
, "Security token not inserted, refusing.");
1666 r
= handle_generic_user_record_error(username
, old_secret
, &error
, r
, true);
1676 static int resize_home(int argc
, char *argv
[], void *userdata
) {
1677 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1678 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
1679 uint64_t ds
= UINT64_MAX
;
1682 r
= acquire_bus(&bus
);
1686 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1688 if (arg_disk_size_relative
!= UINT64_MAX
||
1689 (argc
> 2 && parse_permyriad(argv
[2]) >= 0))
1690 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
1691 "Relative disk size specification currently not supported when resizing.");
1694 r
= parse_size(argv
[2], 1024, &ds
);
1696 return log_error_errno(r
, "Failed to parse disk size parameter: %s", argv
[2]);
1699 if (arg_disk_size
!= UINT64_MAX
) {
1700 if (ds
!= UINT64_MAX
&& ds
!= arg_disk_size
)
1701 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Disk size specified twice and doesn't match, refusing.");
1706 r
= acquire_passed_secrets(argv
[1], &secret
);
1711 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1712 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1714 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ResizeHome");
1716 return bus_log_create_error(r
);
1718 r
= sd_bus_message_append(m
, "st", argv
[1], ds
);
1720 return bus_log_create_error(r
);
1722 r
= bus_message_append_secret(m
, secret
);
1724 return bus_log_create_error(r
);
1726 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1728 r
= handle_generic_user_record_error(argv
[1], secret
, &error
, r
, false);
1738 static int lock_home(int argc
, char *argv
[], void *userdata
) {
1739 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1743 r
= acquire_bus(&bus
);
1747 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
1748 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1749 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1751 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "LockHome");
1753 return bus_log_create_error(r
);
1755 r
= sd_bus_message_append(m
, "s", *i
);
1757 return bus_log_create_error(r
);
1759 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1761 log_error_errno(r
, "Failed to lock home: %s", bus_error_message(&error
, r
));
1770 static int unlock_home(int argc
, char *argv
[], void *userdata
) {
1771 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1775 r
= acquire_bus(&bus
);
1779 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
1780 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
1782 r
= acquire_passed_secrets(*i
, &secret
);
1787 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1788 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1790 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "UnlockHome");
1792 return bus_log_create_error(r
);
1794 r
= sd_bus_message_append(m
, "s", *i
);
1796 return bus_log_create_error(r
);
1798 r
= bus_message_append_secret(m
, secret
);
1800 return bus_log_create_error(r
);
1802 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1804 r
= handle_generic_user_record_error(argv
[1], secret
, &error
, r
, false);
1819 static int with_home(int argc
, char *argv
[], void *userdata
) {
1820 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1821 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1822 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1823 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
1824 _cleanup_close_
int acquired_fd
= -1;
1825 _cleanup_strv_free_
char **cmdline
= NULL
;
1830 r
= acquire_bus(&bus
);
1835 _cleanup_free_
char *shell
= NULL
;
1837 /* If no command is specified, spawn a shell */
1838 r
= get_shell(&shell
);
1840 return log_error_errno(r
, "Failed to acquire shell: %m");
1842 cmdline
= strv_new(shell
);
1844 cmdline
= strv_copy(argv
+ 2);
1848 r
= acquire_passed_secrets(argv
[1], &secret
);
1853 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "AcquireHome");
1855 return bus_log_create_error(r
);
1857 r
= sd_bus_message_append(m
, "s", argv
[1]);
1859 return bus_log_create_error(r
);
1861 r
= bus_message_append_secret(m
, secret
);
1863 return bus_log_create_error(r
);
1865 r
= sd_bus_message_append(m
, "b", /* please_suspend = */ getenv_bool("SYSTEMD_PLEASE_SUSPEND_HOME") > 0);
1867 return bus_log_create_error(r
);
1869 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, &reply
);
1870 m
= sd_bus_message_unref(m
);
1872 r
= handle_generic_user_record_error(argv
[1], secret
, &error
, r
, false);
1876 sd_bus_error_free(&error
);
1880 r
= sd_bus_message_read(reply
, "h", &fd
);
1882 return bus_log_parse_error(r
);
1884 acquired_fd
= fcntl(fd
, F_DUPFD_CLOEXEC
, 3);
1885 if (acquired_fd
< 0)
1886 return log_error_errno(errno
, "Failed to duplicate acquired fd: %m");
1888 reply
= sd_bus_message_unref(reply
);
1893 r
= bus_call_method(bus
, bus_mgr
, "GetHomeByName", &error
, &reply
, "s", argv
[1]);
1895 return log_error_errno(r
, "Failed to inspect home: %s", bus_error_message(&error
, r
));
1897 r
= sd_bus_message_read(reply
, "usussso", NULL
, NULL
, NULL
, NULL
, &home
, NULL
, NULL
);
1899 return bus_log_parse_error(r
);
1901 r
= safe_fork("(with)", FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_DEATHSIG
|FORK_LOG
|FORK_RLIMIT_NOFILE_SAFE
|FORK_REOPEN_LOG
, &pid
);
1905 if (chdir(home
) < 0) {
1906 log_error_errno(errno
, "Failed to change to directory %s: %m", home
);
1910 execvp(cmdline
[0], cmdline
);
1911 log_error_errno(errno
, "Failed to execute %s: %m", cmdline
[0]);
1915 ret
= wait_for_terminate_and_check(cmdline
[0], pid
, WAIT_LOG_ABNORMAL
);
1917 /* Close the fd that pings the home now. */
1918 acquired_fd
= safe_close(acquired_fd
);
1920 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ReleaseHome");
1922 return bus_log_create_error(r
);
1924 r
= sd_bus_message_append(m
, "s", argv
[1]);
1926 return bus_log_create_error(r
);
1928 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1930 if (sd_bus_error_has_name(&error
, BUS_ERROR_HOME_BUSY
))
1931 log_notice("Not deactivating home directory of %s, as it is still used.", argv
[1]);
1933 return log_error_errno(r
, "Failed to release user home: %s", bus_error_message(&error
, r
));
1939 static int lock_all_homes(int argc
, char *argv
[], void *userdata
) {
1940 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1941 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1942 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1945 r
= acquire_bus(&bus
);
1949 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "LockAllHomes");
1951 return bus_log_create_error(r
);
1953 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1955 return log_error_errno(r
, "Failed to lock all homes: %s", bus_error_message(&error
, r
));
1960 static int deactivate_all_homes(int argc
, char *argv
[], void *userdata
) {
1961 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1962 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1963 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1966 r
= acquire_bus(&bus
);
1970 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "DeactivateAllHomes");
1972 return bus_log_create_error(r
);
1974 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1976 return log_error_errno(r
, "Failed to deactivate all homes: %s", bus_error_message(&error
, r
));
1981 static int drop_from_identity(const char *field
) {
1986 /* If we are called to update an identity record and drop some field, let's keep track of what to
1987 * remove from the old record */
1988 r
= strv_extend(&arg_identity_filter
, field
);
1992 /* Let's also drop the field if it was previously set to a new value on the same command line */
1993 r
= json_variant_filter(&arg_identity_extra
, STRV_MAKE(field
));
1995 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
1997 r
= json_variant_filter(&arg_identity_extra_this_machine
, STRV_MAKE(field
));
1999 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
2001 r
= json_variant_filter(&arg_identity_extra_privileged
, STRV_MAKE(field
));
2003 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
2008 static int help(int argc
, char *argv
[], void *userdata
) {
2009 _cleanup_free_
char *link
= NULL
;
2012 (void) pager_open(arg_pager_flags
);
2014 r
= terminal_urlify_man("homectl", "1", &link
);
2018 printf("%1$s [OPTIONS...] COMMAND ...\n\n"
2019 "%2$sCreate, manipulate or inspect home directories.%3$s\n"
2020 "\n%4$sCommands:%5$s\n"
2021 " list List home areas\n"
2022 " activate USER… Activate a home area\n"
2023 " deactivate USER… Deactivate a home area\n"
2024 " inspect USER… Inspect a home area\n"
2025 " authenticate USER… Authenticate a home area\n"
2026 " create USER Create a home area\n"
2027 " remove USER… Remove a home area\n"
2028 " update USER Update a home area\n"
2029 " passwd USER Change password of a home area\n"
2030 " resize USER SIZE Resize a home area\n"
2031 " lock USER… Temporarily lock an active home area\n"
2032 " unlock USER… Unlock a temporarily locked home area\n"
2033 " lock-all Lock all suitable home areas\n"
2034 " deactivate-all Deactivate all active home areas\n"
2035 " with USER [COMMAND…] Run shell or command with access to a home area\n"
2036 "\n%4$sOptions:%5$s\n"
2037 " -h --help Show this help\n"
2038 " --version Show package version\n"
2039 " --no-pager Do not pipe output into a pager\n"
2040 " --no-legend Do not show the headers and footers\n"
2041 " --no-ask-password Do not ask for system passwords\n"
2042 " -H --host=[USER@]HOST Operate on remote host\n"
2043 " -M --machine=CONTAINER Operate on local container\n"
2044 " --identity=PATH Read JSON identity from file\n"
2045 " --json=FORMAT Output inspection data in JSON (takes one of\n"
2046 " pretty, short, off)\n"
2047 " -j Equivalent to --json=pretty (on TTY) or\n"
2048 " --json=short (otherwise)\n"
2049 " --export-format= Strip JSON inspection data (full, stripped,\n"
2051 " -E When specified once equals -j --export-format=\n"
2052 " stripped, when specified twice equals\n"
2053 " -j --export-format=minimal\n"
2054 "\n%4$sGeneral User Record Properties:%5$s\n"
2055 " -c --real-name=REALNAME Real name for user\n"
2056 " --realm=REALM Realm to create user in\n"
2057 " --email-address=EMAIL Email address for user\n"
2058 " --location=LOCATION Set location of user on earth\n"
2059 " --icon-name=NAME Icon name for user\n"
2060 " -d --home-dir=PATH Home directory\n"
2061 " -u --uid=UID Numeric UID for user\n"
2062 " -G --member-of=GROUP Add user to group\n"
2063 " --skel=PATH Skeleton directory to use\n"
2064 " --shell=PATH Shell for account\n"
2065 " --setenv=VARIABLE=VALUE Set an environment variable at log-in\n"
2066 " --timezone=TIMEZONE Set a time-zone\n"
2067 " --language=LOCALE Set preferred language\n"
2068 " --ssh-authorized-keys=KEYS\n"
2069 " Specify SSH public keys\n"
2070 " --pkcs11-token-uri=URI URI to PKCS#11 security token containing\n"
2071 " private key and matching X.509 certificate\n"
2072 " --fido2-device=PATH Path to FIDO2 hidraw device with hmac-secret\n"
2074 " --recovery-key=BOOL Add a recovery key\n"
2075 "\n%4$sAccount Management User Record Properties:%5$s\n"
2076 " --locked=BOOL Set locked account state\n"
2077 " --not-before=TIMESTAMP Do not allow logins before\n"
2078 " --not-after=TIMESTAMP Do not allow logins after\n"
2079 " --rate-limit-interval=SECS\n"
2080 " Login rate-limit interval in seconds\n"
2081 " --rate-limit-burst=NUMBER\n"
2082 " Login rate-limit attempts per interval\n"
2083 "\n%4$sPassword Policy User Record Properties:%5$s\n"
2084 " --password-hint=HINT Set Password hint\n"
2085 " --enforce-password-policy=BOOL\n"
2086 " Control whether to enforce system's password\n"
2087 " policy for this user\n"
2088 " -P Equivalent to --enforce-password-password=no\n"
2089 " --password-change-now=BOOL\n"
2090 " Require the password to be changed on next login\n"
2091 " --password-change-min=TIME\n"
2092 " Require minimum time between password changes\n"
2093 " --password-change-max=TIME\n"
2094 " Require maximum time between password changes\n"
2095 " --password-change-warn=TIME\n"
2096 " How much time to warn before password expiry\n"
2097 " --password-change-inactive=TIME\n"
2098 " How much time to block password after expiry\n"
2099 "\n%4$sResource Management User Record Properties:%5$s\n"
2100 " --disk-size=BYTES Size to assign the user on disk\n"
2101 " --access-mode=MODE User home directory access mode\n"
2102 " --umask=MODE Umask for user when logging in\n"
2103 " --nice=NICE Nice level for user\n"
2104 " --rlimit=LIMIT=VALUE[:VALUE]\n"
2105 " Set resource limits\n"
2106 " --tasks-max=MAX Set maximum number of per-user tasks\n"
2107 " --memory-high=BYTES Set high memory threshold in bytes\n"
2108 " --memory-max=BYTES Set maximum memory limit\n"
2109 " --cpu-weight=WEIGHT Set CPU weight\n"
2110 " --io-weight=WEIGHT Set IO weight\n"
2111 "\n%4$sStorage User Record Properties:%5$s\n"
2112 " --storage=STORAGE Storage type to use (luks, fscrypt, directory,\n"
2113 " subvolume, cifs)\n"
2114 " --image-path=PATH Path to image file/directory\n"
2115 "\n%4$sLUKS Storage User Record Properties:%5$s\n"
2116 " --fs-type=TYPE File system type to use in case of luks\n"
2117 " storage (btrfs, ext4, xfs)\n"
2118 " --luks-discard=BOOL Whether to use 'discard' feature of file system\n"
2119 " when activated (mounted)\n"
2120 " --luks-offline-discard=BOOL\n"
2121 " Whether to trim file on logout\n"
2122 " --luks-cipher=CIPHER Cipher to use for LUKS encryption\n"
2123 " --luks-cipher-mode=MODE Cipher mode to use for LUKS encryption\n"
2124 " --luks-volume-key-size=BITS\n"
2125 " Volume key size to use for LUKS encryption\n"
2126 " --luks-pbkdf-type=TYPE Password-based Key Derivation Function to use\n"
2127 " --luks-pbkdf-hash-algorithm=ALGORITHM\n"
2128 " PBKDF hash algorithm to use\n"
2129 " --luks-pbkdf-time-cost=SECS\n"
2130 " Time cost for PBKDF in seconds\n"
2131 " --luks-pbkdf-memory-cost=BYTES\n"
2132 " Memory cost for PBKDF in bytes\n"
2133 " --luks-pbkdf-parallel-threads=NUMBER\n"
2134 " Number of parallel threads for PKBDF\n"
2135 "\n%4$sMounting User Record Properties:%5$s\n"
2136 " --nosuid=BOOL Control the 'nosuid' flag of the home mount\n"
2137 " --nodev=BOOL Control the 'nodev' flag of the home mount\n"
2138 " --noexec=BOOL Control the 'noexec' flag of the home mount\n"
2139 "\n%4$sCIFS User Record Properties:%5$s\n"
2140 " --cifs-domain=DOMAIN CIFS (Windows) domain\n"
2141 " --cifs-user-name=USER CIFS (Windows) user name\n"
2142 " --cifs-service=SERVICE CIFS (Windows) service to mount as home area\n"
2143 "\n%4$sLogin Behaviour User Record Properties:%5$s\n"
2144 " --stop-delay=SECS How long to leave user services running after\n"
2146 " --kill-processes=BOOL Whether to kill user processes when sessions\n"
2148 " --auto-login=BOOL Try to log this user in automatically\n"
2149 "\nSee the %6$s for details.\n",
2150 program_invocation_short_name
,
2160 static int parse_argv(int argc
, char *argv
[]) {
2163 ARG_VERSION
= 0x100,
2166 ARG_NO_ASK_PASSWORD
,
2176 ARG_LUKS_OFFLINE_DISCARD
,
2182 ARG_SSH_AUTHORIZED_KEYS
,
2191 ARG_LUKS_CIPHER_MODE
,
2192 ARG_LUKS_VOLUME_KEY_SIZE
,
2204 ARG_LUKS_PBKDF_TYPE
,
2205 ARG_LUKS_PBKDF_HASH_ALGORITHM
,
2206 ARG_LUKS_PBKDF_TIME_COST
,
2207 ARG_LUKS_PBKDF_MEMORY_COST
,
2208 ARG_LUKS_PBKDF_PARALLEL_THREADS
,
2209 ARG_RATE_LIMIT_INTERVAL
,
2210 ARG_RATE_LIMIT_BURST
,
2213 ARG_ENFORCE_PASSWORD_POLICY
,
2214 ARG_PASSWORD_CHANGE_NOW
,
2215 ARG_PASSWORD_CHANGE_MIN
,
2216 ARG_PASSWORD_CHANGE_MAX
,
2217 ARG_PASSWORD_CHANGE_WARN
,
2218 ARG_PASSWORD_CHANGE_INACTIVE
,
2221 ARG_PKCS11_TOKEN_URI
,
2225 ARG_AND_CHANGE_PASSWORD
,
2228 static const struct option options
[] = {
2229 { "help", no_argument
, NULL
, 'h' },
2230 { "version", no_argument
, NULL
, ARG_VERSION
},
2231 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
2232 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
2233 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
2234 { "host", required_argument
, NULL
, 'H' },
2235 { "machine", required_argument
, NULL
, 'M' },
2236 { "identity", required_argument
, NULL
, 'I' },
2237 { "real-name", required_argument
, NULL
, 'c' },
2238 { "comment", required_argument
, NULL
, 'c' }, /* Compat alias to keep thing in sync with useradd(8) */
2239 { "realm", required_argument
, NULL
, ARG_REALM
},
2240 { "email-address", required_argument
, NULL
, ARG_EMAIL_ADDRESS
},
2241 { "location", required_argument
, NULL
, ARG_LOCATION
},
2242 { "password-hint", required_argument
, NULL
, ARG_PASSWORD_HINT
},
2243 { "icon-name", required_argument
, NULL
, ARG_ICON_NAME
},
2244 { "home-dir", required_argument
, NULL
, 'd' }, /* Compatible with useradd(8) */
2245 { "uid", required_argument
, NULL
, 'u' }, /* Compatible with useradd(8) */
2246 { "member-of", required_argument
, NULL
, 'G' },
2247 { "groups", required_argument
, NULL
, 'G' }, /* Compat alias to keep thing in sync with useradd(8) */
2248 { "skel", required_argument
, NULL
, 'k' }, /* Compatible with useradd(8) */
2249 { "shell", required_argument
, NULL
, 's' }, /* Compatible with useradd(8) */
2250 { "setenv", required_argument
, NULL
, ARG_SETENV
},
2251 { "timezone", required_argument
, NULL
, ARG_TIMEZONE
},
2252 { "language", required_argument
, NULL
, ARG_LANGUAGE
},
2253 { "locked", required_argument
, NULL
, ARG_LOCKED
},
2254 { "not-before", required_argument
, NULL
, ARG_NOT_BEFORE
},
2255 { "not-after", required_argument
, NULL
, ARG_NOT_AFTER
},
2256 { "expiredate", required_argument
, NULL
, 'e' }, /* Compat alias to keep thing in sync with useradd(8) */
2257 { "ssh-authorized-keys", required_argument
, NULL
, ARG_SSH_AUTHORIZED_KEYS
},
2258 { "disk-size", required_argument
, NULL
, ARG_DISK_SIZE
},
2259 { "access-mode", required_argument
, NULL
, ARG_ACCESS_MODE
},
2260 { "umask", required_argument
, NULL
, ARG_UMASK
},
2261 { "nice", required_argument
, NULL
, ARG_NICE
},
2262 { "rlimit", required_argument
, NULL
, ARG_RLIMIT
},
2263 { "tasks-max", required_argument
, NULL
, ARG_TASKS_MAX
},
2264 { "memory-high", required_argument
, NULL
, ARG_MEMORY_HIGH
},
2265 { "memory-max", required_argument
, NULL
, ARG_MEMORY_MAX
},
2266 { "cpu-weight", required_argument
, NULL
, ARG_CPU_WEIGHT
},
2267 { "io-weight", required_argument
, NULL
, ARG_IO_WEIGHT
},
2268 { "storage", required_argument
, NULL
, ARG_STORAGE
},
2269 { "image-path", required_argument
, NULL
, ARG_IMAGE_PATH
},
2270 { "fs-type", required_argument
, NULL
, ARG_FS_TYPE
},
2271 { "luks-discard", required_argument
, NULL
, ARG_LUKS_DISCARD
},
2272 { "luks-offline-discard", required_argument
, NULL
, ARG_LUKS_OFFLINE_DISCARD
},
2273 { "luks-cipher", required_argument
, NULL
, ARG_LUKS_CIPHER
},
2274 { "luks-cipher-mode", required_argument
, NULL
, ARG_LUKS_CIPHER_MODE
},
2275 { "luks-volume-key-size", required_argument
, NULL
, ARG_LUKS_VOLUME_KEY_SIZE
},
2276 { "luks-pbkdf-type", required_argument
, NULL
, ARG_LUKS_PBKDF_TYPE
},
2277 { "luks-pbkdf-hash-algorithm", required_argument
, NULL
, ARG_LUKS_PBKDF_HASH_ALGORITHM
},
2278 { "luks-pbkdf-time-cost", required_argument
, NULL
, ARG_LUKS_PBKDF_TIME_COST
},
2279 { "luks-pbkdf-memory-cost", required_argument
, NULL
, ARG_LUKS_PBKDF_MEMORY_COST
},
2280 { "luks-pbkdf-parallel-threads", required_argument
, NULL
, ARG_LUKS_PBKDF_PARALLEL_THREADS
},
2281 { "nosuid", required_argument
, NULL
, ARG_NOSUID
},
2282 { "nodev", required_argument
, NULL
, ARG_NODEV
},
2283 { "noexec", required_argument
, NULL
, ARG_NOEXEC
},
2284 { "cifs-user-name", required_argument
, NULL
, ARG_CIFS_USER_NAME
},
2285 { "cifs-domain", required_argument
, NULL
, ARG_CIFS_DOMAIN
},
2286 { "cifs-service", required_argument
, NULL
, ARG_CIFS_SERVICE
},
2287 { "rate-limit-interval", required_argument
, NULL
, ARG_RATE_LIMIT_INTERVAL
},
2288 { "rate-limit-burst", required_argument
, NULL
, ARG_RATE_LIMIT_BURST
},
2289 { "stop-delay", required_argument
, NULL
, ARG_STOP_DELAY
},
2290 { "kill-processes", required_argument
, NULL
, ARG_KILL_PROCESSES
},
2291 { "enforce-password-policy", required_argument
, NULL
, ARG_ENFORCE_PASSWORD_POLICY
},
2292 { "password-change-now", required_argument
, NULL
, ARG_PASSWORD_CHANGE_NOW
},
2293 { "password-change-min", required_argument
, NULL
, ARG_PASSWORD_CHANGE_MIN
},
2294 { "password-change-max", required_argument
, NULL
, ARG_PASSWORD_CHANGE_MAX
},
2295 { "password-change-warn", required_argument
, NULL
, ARG_PASSWORD_CHANGE_WARN
},
2296 { "password-change-inactive", required_argument
, NULL
, ARG_PASSWORD_CHANGE_INACTIVE
},
2297 { "auto-login", required_argument
, NULL
, ARG_AUTO_LOGIN
},
2298 { "json", required_argument
, NULL
, ARG_JSON
},
2299 { "export-format", required_argument
, NULL
, ARG_EXPORT_FORMAT
},
2300 { "pkcs11-token-uri", required_argument
, NULL
, ARG_PKCS11_TOKEN_URI
},
2301 { "fido2-device", required_argument
, NULL
, ARG_FIDO2_DEVICE
},
2302 { "recovery-key", required_argument
, NULL
, ARG_RECOVERY_KEY
},
2303 { "and-resize", required_argument
, NULL
, ARG_AND_RESIZE
},
2304 { "and-change-password", required_argument
, NULL
, ARG_AND_CHANGE_PASSWORD
},
2316 c
= getopt_long(argc
, argv
, "hH:M:I:c:d:u:k:s:e:G:jPE", options
, NULL
);
2323 return help(0, NULL
, NULL
);
2329 arg_pager_flags
|= PAGER_DISABLE
;
2336 case ARG_NO_ASK_PASSWORD
:
2337 arg_ask_password
= false;
2341 arg_transport
= BUS_TRANSPORT_REMOTE
;
2346 arg_transport
= BUS_TRANSPORT_MACHINE
;
2351 arg_identity
= optarg
;
2355 if (isempty(optarg
)) {
2356 r
= drop_from_identity("realName");
2363 if (!valid_gecos(optarg
))
2364 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Real name '%s' not a valid GECOS field.", optarg
);
2366 r
= json_variant_set_field_string(&arg_identity_extra
, "realName", optarg
);
2368 return log_error_errno(r
, "Failed to set realName field: %m");
2373 _cleanup_free_
char *hd
= NULL
;
2375 if (isempty(optarg
)) {
2376 r
= drop_from_identity("homeDirectory");
2383 r
= parse_path_argument(optarg
, false, &hd
);
2387 if (!valid_home(hd
))
2388 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Home directory '%s' not valid.", hd
);
2390 r
= json_variant_set_field_string(&arg_identity_extra
, "homeDirectory", hd
);
2392 return log_error_errno(r
, "Failed to set homeDirectory field: %m");
2398 if (isempty(optarg
)) {
2399 r
= drop_from_identity("realm");
2406 r
= dns_name_is_valid(optarg
);
2408 return log_error_errno(r
, "Failed to determine whether realm '%s' is a valid DNS domain: %m", optarg
);
2410 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Realm '%s' is not a valid DNS domain: %m", optarg
);
2412 r
= json_variant_set_field_string(&arg_identity_extra
, "realm", optarg
);
2414 return log_error_errno(r
, "Failed to set realm field: %m");
2417 case ARG_EMAIL_ADDRESS
:
2420 case ARG_CIFS_USER_NAME
:
2421 case ARG_CIFS_DOMAIN
:
2422 case ARG_CIFS_SERVICE
: {
2425 c
== ARG_EMAIL_ADDRESS
? "emailAddress" :
2426 c
== ARG_LOCATION
? "location" :
2427 c
== ARG_ICON_NAME
? "iconName" :
2428 c
== ARG_CIFS_USER_NAME
? "cifsUserName" :
2429 c
== ARG_CIFS_DOMAIN
? "cifsDomain" :
2430 c
== ARG_CIFS_SERVICE
? "cifsService" :
2435 if (isempty(optarg
)) {
2436 r
= drop_from_identity(field
);
2443 r
= json_variant_set_field_string(&arg_identity_extra
, field
, optarg
);
2445 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2450 case ARG_PASSWORD_HINT
:
2451 if (isempty(optarg
)) {
2452 r
= drop_from_identity("passwordHint");
2459 r
= json_variant_set_field_string(&arg_identity_extra_privileged
, "passwordHint", optarg
);
2461 return log_error_errno(r
, "Failed to set passwordHint field: %m");
2463 string_erase(optarg
);
2469 if (isempty(optarg
)) {
2470 r
= drop_from_identity("niceLevel");
2476 r
= parse_nice(optarg
, &nc
);
2478 return log_error_errno(r
, "Failed to parse nice level: %s", optarg
);
2480 r
= json_variant_set_field_integer(&arg_identity_extra
, "niceLevel", nc
);
2482 return log_error_errno(r
, "Failed to set niceLevel field: %m");
2488 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
, *jcur
= NULL
, *jmax
= NULL
;
2489 _cleanup_free_
char *field
= NULL
, *t
= NULL
;
2494 if (isempty(optarg
)) {
2495 /* Remove all resource limits */
2497 r
= drop_from_identity("resourceLimits");
2501 arg_identity_filter_rlimits
= strv_free(arg_identity_filter_rlimits
);
2502 arg_identity_extra_rlimits
= json_variant_unref(arg_identity_extra_rlimits
);
2506 eq
= strchr(optarg
, '=');
2508 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Can't parse resource limit assignment: %s", optarg
);
2510 field
= strndup(optarg
, eq
- optarg
);
2514 l
= rlimit_from_string_harder(field
);
2516 return log_error_errno(l
, "Unknown resource limit type: %s", field
);
2518 if (isempty(eq
+ 1)) {
2519 /* Remove only the specific rlimit */
2521 r
= strv_extend(&arg_identity_filter_rlimits
, rlimit_to_string(l
));
2525 r
= json_variant_filter(&arg_identity_extra_rlimits
, STRV_MAKE(field
));
2527 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
2532 r
= rlimit_parse(l
, eq
+ 1, &rl
);
2534 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to parse resource limit value: %s", eq
+ 1);
2536 r
= rl
.rlim_cur
== RLIM_INFINITY
? json_variant_new_null(&jcur
) : json_variant_new_unsigned(&jcur
, rl
.rlim_cur
);
2538 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to allocate current integer: %m");
2540 r
= rl
.rlim_max
== RLIM_INFINITY
? json_variant_new_null(&jmax
) : json_variant_new_unsigned(&jmax
, rl
.rlim_max
);
2542 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to allocate maximum integer: %m");
2546 JSON_BUILD_PAIR("cur", JSON_BUILD_VARIANT(jcur
)),
2547 JSON_BUILD_PAIR("max", JSON_BUILD_VARIANT(jmax
))));
2549 return log_error_errno(r
, "Failed to build resource limit: %m");
2551 t
= strjoin("RLIMIT_", rlimit_to_string(l
));
2555 r
= json_variant_set_field(&arg_identity_extra_rlimits
, t
, v
);
2557 return log_error_errno(r
, "Failed to set %s field: %m", rlimit_to_string(l
));
2565 if (isempty(optarg
)) {
2566 r
= drop_from_identity("uid");
2573 r
= parse_uid(optarg
, &uid
);
2575 return log_error_errno(r
, "Failed to parse UID '%s'.", optarg
);
2577 if (uid_is_system(uid
))
2578 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "UID " UID_FMT
" is in system range, refusing.", uid
);
2579 if (uid_is_dynamic(uid
))
2580 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "UID " UID_FMT
" is in dynamic range, refusing.", uid
);
2581 if (uid
== UID_NOBODY
)
2582 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "UID " UID_FMT
" is nobody UID, refusing.", uid
);
2584 r
= json_variant_set_field_unsigned(&arg_identity_extra
, "uid", uid
);
2586 return log_error_errno(r
, "Failed to set realm field: %m");
2592 case ARG_IMAGE_PATH
: {
2593 const char *field
= c
== 'k' ? "skeletonDirectory" : "imagePath";
2594 _cleanup_free_
char *v
= NULL
;
2596 if (isempty(optarg
)) {
2597 r
= drop_from_identity(field
);
2604 r
= parse_path_argument(optarg
, false, &v
);
2608 r
= json_variant_set_field_string(&arg_identity_extra_this_machine
, field
, v
);
2610 return log_error_errno(r
, "Failed to set %s field: %m", v
);
2616 if (isempty(optarg
)) {
2617 r
= drop_from_identity("shell");
2624 if (!valid_shell(optarg
))
2625 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Shell '%s' not valid.", optarg
);
2627 r
= json_variant_set_field_string(&arg_identity_extra
, "shell", optarg
);
2629 return log_error_errno(r
, "Failed to set shell field: %m");
2634 _cleanup_free_
char **l
= NULL
;
2635 _cleanup_(json_variant_unrefp
) JsonVariant
*ne
= NULL
;
2638 if (isempty(optarg
)) {
2639 r
= drop_from_identity("environment");
2646 if (!env_assignment_is_valid(optarg
))
2647 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2648 "Environment assignment '%s' not valid.", optarg
);
2650 e
= json_variant_by_key(arg_identity_extra
, "environment");
2652 r
= json_variant_strv(e
, &l
);
2654 return log_error_errno(r
, "Failed to parse JSON environment field: %m");
2657 r
= strv_env_replace_strdup(&l
, optarg
);
2659 return log_error_errno(r
, "Failed to replace JSON environment field: %m");
2663 r
= json_variant_new_array_strv(&ne
, l
);
2665 return log_error_errno(r
, "Failed to allocate environment list JSON: %m");
2667 r
= json_variant_set_field(&arg_identity_extra
, "environment", ne
);
2669 return log_error_errno(r
, "Failed to set environment list: %m");
2676 if (isempty(optarg
)) {
2677 r
= drop_from_identity("timeZone");
2684 if (!timezone_is_valid(optarg
, LOG_DEBUG
))
2685 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Timezone '%s' is not valid.", optarg
);
2687 r
= json_variant_set_field_string(&arg_identity_extra
, "timeZone", optarg
);
2689 return log_error_errno(r
, "Failed to set timezone field: %m");
2694 if (isempty(optarg
)) {
2695 r
= drop_from_identity("language");
2702 if (!locale_is_valid(optarg
))
2703 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Locale '%s' is not valid.", optarg
);
2705 if (locale_is_installed(optarg
) <= 0)
2706 log_warning("Locale '%s' is not installed, accepting anyway.", optarg
);
2708 r
= json_variant_set_field_string(&arg_identity_extra
, "preferredLanguage", optarg
);
2710 return log_error_errno(r
, "Failed to set preferredLanguage field: %m");
2718 case ARG_KILL_PROCESSES
:
2719 case ARG_ENFORCE_PASSWORD_POLICY
:
2720 case ARG_AUTO_LOGIN
:
2721 case ARG_PASSWORD_CHANGE_NOW
: {
2723 c
== ARG_LOCKED
? "locked" :
2724 c
== ARG_NOSUID
? "mountNoSuid" :
2725 c
== ARG_NODEV
? "mountNoDevices" :
2726 c
== ARG_NOEXEC
? "mountNoExecute" :
2727 c
== ARG_KILL_PROCESSES
? "killProcesses" :
2728 c
== ARG_ENFORCE_PASSWORD_POLICY
? "enforcePasswordPolicy" :
2729 c
== ARG_AUTO_LOGIN
? "autoLogin" :
2730 c
== ARG_PASSWORD_CHANGE_NOW
? "passwordChangeNow" :
2735 if (isempty(optarg
)) {
2736 r
= drop_from_identity(field
);
2743 r
= parse_boolean(optarg
);
2745 return log_error_errno(r
, "Failed to parse %s boolean: %m", field
);
2747 r
= json_variant_set_field_boolean(&arg_identity_extra
, field
, r
> 0);
2749 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2755 r
= json_variant_set_field_boolean(&arg_identity_extra
, "enforcePasswordPolicy", false);
2757 return log_error_errno(r
, "Failed to set enforcePasswordPolicy field: %m");
2762 if (isempty(optarg
)) {
2763 r
= drop_from_identity("diskSize");
2767 r
= drop_from_identity("diskSizeRelative");
2771 arg_disk_size
= arg_disk_size_relative
= UINT64_MAX
;
2775 r
= parse_permyriad(optarg
);
2777 r
= parse_size(optarg
, 1024, &arg_disk_size
);
2779 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Disk size '%s' not valid.", optarg
);
2781 r
= drop_from_identity("diskSizeRelative");
2785 r
= json_variant_set_field_unsigned(&arg_identity_extra_this_machine
, "diskSize", arg_disk_size
);
2787 return log_error_errno(r
, "Failed to set diskSize field: %m");
2789 arg_disk_size_relative
= UINT64_MAX
;
2791 /* Normalize to UINT32_MAX == 100% */
2792 arg_disk_size_relative
= UINT32_SCALE_FROM_PERMYRIAD(r
);
2794 r
= drop_from_identity("diskSize");
2798 r
= json_variant_set_field_unsigned(&arg_identity_extra_this_machine
, "diskSizeRelative", arg_disk_size_relative
);
2800 return log_error_errno(r
, "Failed to set diskSizeRelative field: %m");
2802 arg_disk_size
= UINT64_MAX
;
2807 case ARG_ACCESS_MODE
: {
2810 if (isempty(optarg
)) {
2811 r
= drop_from_identity("accessMode");
2818 r
= parse_mode(optarg
, &mode
);
2820 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Access mode '%s' not valid.", optarg
);
2822 r
= json_variant_set_field_unsigned(&arg_identity_extra
, "accessMode", mode
);
2824 return log_error_errno(r
, "Failed to set access mode field: %m");
2829 case ARG_LUKS_DISCARD
:
2830 if (isempty(optarg
)) {
2831 r
= drop_from_identity("luksDiscard");
2838 r
= parse_boolean(optarg
);
2840 return log_error_errno(r
, "Failed to parse --luks-discard= parameter: %s", optarg
);
2842 r
= json_variant_set_field_boolean(&arg_identity_extra
, "luksDiscard", r
);
2844 return log_error_errno(r
, "Failed to set discard field: %m");
2848 case ARG_LUKS_OFFLINE_DISCARD
:
2849 if (isempty(optarg
)) {
2850 r
= drop_from_identity("luksOfflineDiscard");
2857 r
= parse_boolean(optarg
);
2859 return log_error_errno(r
, "Failed to parse --luks-offline-discard= parameter: %s", optarg
);
2861 r
= json_variant_set_field_boolean(&arg_identity_extra
, "luksOfflineDiscard", r
);
2863 return log_error_errno(r
, "Failed to set offline discard field: %m");
2867 case ARG_LUKS_VOLUME_KEY_SIZE
:
2868 case ARG_LUKS_PBKDF_PARALLEL_THREADS
:
2869 case ARG_RATE_LIMIT_BURST
: {
2871 c
== ARG_LUKS_VOLUME_KEY_SIZE
? "luksVolumeKeySize" :
2872 c
== ARG_LUKS_PBKDF_PARALLEL_THREADS
? "luksPbkdfParallelThreads" :
2873 c
== ARG_RATE_LIMIT_BURST
? "rateLimitBurst" : NULL
;
2878 if (isempty(optarg
)) {
2879 r
= drop_from_identity(field
);
2884 r
= safe_atou(optarg
, &n
);
2886 return log_error_errno(r
, "Failed to parse %s parameter: %s", field
, optarg
);
2888 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, n
);
2890 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2898 if (isempty(optarg
)) {
2899 r
= drop_from_identity("umask");
2906 r
= parse_mode(optarg
, &m
);
2908 return log_error_errno(r
, "Failed to parse umask: %m");
2910 r
= json_variant_set_field_integer(&arg_identity_extra
, "umask", m
);
2912 return log_error_errno(r
, "Failed to set umask field: %m");
2917 case ARG_SSH_AUTHORIZED_KEYS
: {
2918 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
2919 _cleanup_(strv_freep
) char **l
= NULL
, **add
= NULL
;
2921 if (isempty(optarg
)) {
2922 r
= drop_from_identity("sshAuthorizedKeys");
2929 if (optarg
[0] == '@') {
2930 _cleanup_fclose_
FILE *f
= NULL
;
2932 /* If prefixed with '@' read from a file */
2934 f
= fopen(optarg
+1, "re");
2936 return log_error_errno(errno
, "Failed to open '%s': %m", optarg
+1);
2939 _cleanup_free_
char *line
= NULL
;
2941 r
= read_line(f
, LONG_LINE_MAX
, &line
);
2943 return log_error_errno(r
, "Failed to read from '%s': %m", optarg
+1);
2953 r
= strv_consume(&add
, TAKE_PTR(line
));
2958 /* Otherwise, assume it's a literal key. Let's do some superficial checks
2959 * before accept it though. */
2961 if (string_has_cc(optarg
, NULL
))
2962 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Authorized key contains control characters, refusing.");
2963 if (optarg
[0] == '#')
2964 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Specified key is a comment?");
2966 add
= strv_new(optarg
);
2971 v
= json_variant_ref(json_variant_by_key(arg_identity_extra_privileged
, "sshAuthorizedKeys"));
2973 r
= json_variant_strv(v
, &l
);
2975 return log_error_errno(r
, "Failed to parse SSH authorized keys list: %m");
2978 r
= strv_extend_strv(&l
, add
, true);
2982 v
= json_variant_unref(v
);
2984 r
= json_variant_new_array_strv(&v
, l
);
2988 r
= json_variant_set_field(&arg_identity_extra_privileged
, "sshAuthorizedKeys", v
);
2990 return log_error_errno(r
, "Failed to set authorized keys: %m");
2995 case ARG_NOT_BEFORE
:
3001 field
= c
== ARG_NOT_BEFORE
? "notBeforeUSec" :
3002 IN_SET(c
, ARG_NOT_AFTER
, 'e') ? "notAfterUSec" : NULL
;
3006 if (isempty(optarg
)) {
3007 r
= drop_from_identity(field
);
3014 /* Note the minor discrepancy regarding -e parsing here: we support that for compat
3015 * reasons, and in the original useradd(8) implementation it accepts dates in the
3016 * format YYYY-MM-DD. Coincidentally, we accept dates formatted like that too, but
3017 * with greater precision. */
3018 r
= parse_timestamp(optarg
, &n
);
3020 return log_error_errno(r
, "Failed to parse %s parameter: %m", field
);
3022 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, n
);
3024 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3028 case ARG_PASSWORD_CHANGE_MIN
:
3029 case ARG_PASSWORD_CHANGE_MAX
:
3030 case ARG_PASSWORD_CHANGE_WARN
:
3031 case ARG_PASSWORD_CHANGE_INACTIVE
: {
3035 field
= c
== ARG_PASSWORD_CHANGE_MIN
? "passwordChangeMinUSec" :
3036 c
== ARG_PASSWORD_CHANGE_MAX
? "passwordChangeMaxUSec" :
3037 c
== ARG_PASSWORD_CHANGE_WARN
? "passwordChangeWarnUSec" :
3038 c
== ARG_PASSWORD_CHANGE_INACTIVE
? "passwordChangeInactiveUSec" :
3043 if (isempty(optarg
)) {
3044 r
= drop_from_identity(field
);
3051 r
= parse_sec(optarg
, &n
);
3053 return log_error_errno(r
, "Failed to parse %s parameter: %m", field
);
3055 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, n
);
3057 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3063 case ARG_LUKS_CIPHER
:
3064 case ARG_LUKS_CIPHER_MODE
:
3065 case ARG_LUKS_PBKDF_TYPE
:
3066 case ARG_LUKS_PBKDF_HASH_ALGORITHM
: {
3069 c
== ARG_STORAGE
? "storage" :
3070 c
== ARG_FS_TYPE
? "fileSystemType" :
3071 c
== ARG_LUKS_CIPHER
? "luksCipher" :
3072 c
== ARG_LUKS_CIPHER_MODE
? "luksCipherMode" :
3073 c
== ARG_LUKS_PBKDF_TYPE
? "luksPbkdfType" :
3074 c
== ARG_LUKS_PBKDF_HASH_ALGORITHM
? "luksPbkdfHashAlgorithm" : NULL
;
3078 if (isempty(optarg
)) {
3079 r
= drop_from_identity(field
);
3086 if (!string_is_safe(optarg
))
3087 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Parameter for %s field not valid: %s", field
, optarg
);
3089 r
= json_variant_set_field_string(
3090 IN_SET(c
, ARG_STORAGE
, ARG_FS_TYPE
) ?
3091 &arg_identity_extra_this_machine
:
3092 &arg_identity_extra
, field
, optarg
);
3094 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3099 case ARG_LUKS_PBKDF_TIME_COST
:
3100 case ARG_RATE_LIMIT_INTERVAL
:
3101 case ARG_STOP_DELAY
: {
3103 c
== ARG_LUKS_PBKDF_TIME_COST
? "luksPbkdfTimeCostUSec" :
3104 c
== ARG_RATE_LIMIT_INTERVAL
? "rateLimitIntervalUSec" :
3105 c
== ARG_STOP_DELAY
? "stopDelayUSec" :
3111 if (isempty(optarg
)) {
3112 r
= drop_from_identity(field
);
3119 r
= parse_sec(optarg
, &t
);
3121 return log_error_errno(r
, "Failed to parse %s field: %s", field
, optarg
);
3123 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, t
);
3125 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3131 const char *p
= optarg
;
3134 r
= drop_from_identity("memberOf");
3142 _cleanup_(json_variant_unrefp
) JsonVariant
*mo
= NULL
;
3143 _cleanup_strv_free_
char **list
= NULL
;
3144 _cleanup_free_
char *word
= NULL
;
3146 r
= extract_first_word(&p
, &word
, ",", 0);
3148 return log_error_errno(r
, "Failed to parse group list: %m");
3152 if (!valid_user_group_name(word
, 0))
3153 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Invalid group name %s.", word
);
3155 mo
= json_variant_ref(json_variant_by_key(arg_identity_extra
, "memberOf"));
3157 r
= json_variant_strv(mo
, &list
);
3159 return log_error_errno(r
, "Failed to parse group list: %m");
3161 r
= strv_extend(&list
, word
);
3168 mo
= json_variant_unref(mo
);
3169 r
= json_variant_new_array_strv(&mo
, list
);
3171 return log_error_errno(r
, "Failed to create group list JSON: %m");
3173 r
= json_variant_set_field(&arg_identity_extra
, "memberOf", mo
);
3175 return log_error_errno(r
, "Failed to update group list: %m");
3181 case ARG_TASKS_MAX
: {
3184 if (isempty(optarg
)) {
3185 r
= drop_from_identity("tasksMax");
3191 r
= safe_atou64(optarg
, &u
);
3193 return log_error_errno(r
, "Failed to parse --tasks-max= parameter: %s", optarg
);
3195 r
= json_variant_set_field_unsigned(&arg_identity_extra
, "tasksMax", u
);
3197 return log_error_errno(r
, "Failed to set tasksMax field: %m");
3202 case ARG_MEMORY_MAX
:
3203 case ARG_MEMORY_HIGH
:
3204 case ARG_LUKS_PBKDF_MEMORY_COST
: {
3206 c
== ARG_MEMORY_MAX
? "memoryMax" :
3207 c
== ARG_MEMORY_HIGH
? "memoryHigh" :
3208 c
== ARG_LUKS_PBKDF_MEMORY_COST
? "luksPbkdfMemoryCost" : NULL
;
3214 if (isempty(optarg
)) {
3215 r
= drop_from_identity(field
);
3221 r
= parse_size(optarg
, 1024, &u
);
3223 return log_error_errno(r
, "Failed to parse %s parameter: %s", field
, optarg
);
3225 r
= json_variant_set_field_unsigned(&arg_identity_extra_this_machine
, field
, u
);
3227 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3232 case ARG_CPU_WEIGHT
:
3233 case ARG_IO_WEIGHT
: {
3234 const char *field
= c
== ARG_CPU_WEIGHT
? "cpuWeight" :
3235 c
== ARG_IO_WEIGHT
? "ioWeight" : NULL
;
3240 if (isempty(optarg
)) {
3241 r
= drop_from_identity(field
);
3247 r
= safe_atou64(optarg
, &u
);
3249 return log_error_errno(r
, "Failed to parse --cpu-weight=/--io-weight= parameter: %s", optarg
);
3251 if (!CGROUP_WEIGHT_IS_OK(u
))
3252 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Weight %" PRIu64
" is out of valid weight range.", u
);
3254 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, u
);
3256 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3261 case ARG_PKCS11_TOKEN_URI
: {
3264 if (streq(optarg
, "list"))
3265 return pkcs11_list_tokens();
3267 /* If --pkcs11-token-uri= is specified we always drop everything old */
3268 FOREACH_STRING(p
, "pkcs11TokenUri", "pkcs11EncryptedKey") {
3269 r
= drop_from_identity(p
);
3274 if (isempty(optarg
)) {
3275 arg_pkcs11_token_uri
= strv_free(arg_pkcs11_token_uri
);
3279 if (streq(optarg
, "auto")) {
3280 _cleanup_free_
char *found
= NULL
;
3282 r
= pkcs11_find_token_auto(&found
);
3285 r
= strv_consume(&arg_pkcs11_token_uri
, TAKE_PTR(found
));
3287 if (!pkcs11_uri_valid(optarg
))
3288 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Not a valid PKCS#11 URI: %s", optarg
);
3290 r
= strv_extend(&arg_pkcs11_token_uri
, optarg
);
3295 strv_uniq(arg_pkcs11_token_uri
);
3299 case ARG_FIDO2_DEVICE
: {
3302 if (streq(optarg
, "list"))
3303 return fido2_list_devices();
3305 FOREACH_STRING(p
, "fido2HmacCredential", "fido2HmacSalt") {
3306 r
= drop_from_identity(p
);
3311 if (isempty(optarg
)) {
3312 arg_fido2_device
= strv_free(arg_fido2_device
);
3316 if (streq(optarg
, "auto")) {
3317 _cleanup_free_
char *found
= NULL
;
3319 r
= fido2_find_device_auto(&found
);
3323 r
= strv_consume(&arg_fido2_device
, TAKE_PTR(found
));
3325 r
= strv_extend(&arg_fido2_device
, optarg
);
3330 strv_uniq(arg_fido2_device
);
3334 case ARG_RECOVERY_KEY
: {
3337 r
= parse_boolean(optarg
);
3339 return log_error_errno(r
, "Failed to parse --recovery-key= argument: %s", optarg
);
3341 arg_recovery_key
= r
;
3343 FOREACH_STRING(p
, "recoveryKey", "recoveryKeyType") {
3344 r
= drop_from_identity(p
);
3353 arg_json_format_flags
= JSON_FORMAT_PRETTY_AUTO
|JSON_FORMAT_COLOR_AUTO
;
3357 r
= parse_json_argument(optarg
, &arg_json_format_flags
);
3364 if (arg_export_format
== EXPORT_FORMAT_FULL
)
3365 arg_export_format
= EXPORT_FORMAT_STRIPPED
;
3366 else if (arg_export_format
== EXPORT_FORMAT_STRIPPED
)
3367 arg_export_format
= EXPORT_FORMAT_MINIMAL
;
3369 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Specifying -E more than twice is not supported.");
3371 arg_json_format_flags
&= ~JSON_FORMAT_OFF
;
3372 if (arg_json_format_flags
== 0)
3373 arg_json_format_flags
= JSON_FORMAT_PRETTY_AUTO
|JSON_FORMAT_COLOR_AUTO
;
3376 case ARG_EXPORT_FORMAT
:
3377 if (streq(optarg
, "full"))
3378 arg_export_format
= EXPORT_FORMAT_FULL
;
3379 else if (streq(optarg
, "stripped"))
3380 arg_export_format
= EXPORT_FORMAT_STRIPPED
;
3381 else if (streq(optarg
, "minimal"))
3382 arg_export_format
= EXPORT_FORMAT_MINIMAL
;
3383 else if (streq(optarg
, "help")) {
3392 case ARG_AND_RESIZE
:
3393 arg_and_resize
= true;
3396 case ARG_AND_CHANGE_PASSWORD
:
3397 arg_and_change_password
= true;
3404 assert_not_reached("Unhandled option");
3408 if (!strv_isempty(arg_pkcs11_token_uri
) || !strv_isempty(arg_fido2_device
))
3409 arg_and_change_password
= true;
3411 if (arg_disk_size
!= UINT64_MAX
|| arg_disk_size_relative
!= UINT64_MAX
)
3412 arg_and_resize
= true;
3417 static int redirect_bus_mgr(void) {
3420 /* Talk to a different service if that's requested. (The same env var is also understood by homed, so
3421 * that it is relatively easily possible to invoke a second instance of homed for debug purposes and
3422 * have homectl talk to it, without colliding with the host version. This is handy when operating
3423 * from a homed-managed account.) */
3425 suffix
= getenv("SYSTEMD_HOME_DEBUG_SUFFIX");
3427 static BusLocator locator
= {
3428 .path
= "/org/freedesktop/home1",
3429 .interface
= "org.freedesktop.home1.Manager",
3432 /* Yes, we leak this memory, but there's little point to collect this, given that we only do
3433 * this in a debug environment, do it only once, and the string shall live for out entire
3434 * process runtime. */
3436 locator
.destination
= strjoin("org.freedesktop.home1.", suffix
);
3437 if (!locator
.destination
)
3442 bus_mgr
= bus_home_mgr
;
3447 static int run(int argc
, char *argv
[]) {
3448 static const Verb verbs
[] = {
3449 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
3450 { "list", VERB_ANY
, 1, VERB_DEFAULT
, list_homes
},
3451 { "activate", 2, VERB_ANY
, 0, activate_home
},
3452 { "deactivate", 2, VERB_ANY
, 0, deactivate_home
},
3453 { "inspect", VERB_ANY
, VERB_ANY
, 0, inspect_home
},
3454 { "authenticate", VERB_ANY
, VERB_ANY
, 0, authenticate_home
},
3455 { "create", VERB_ANY
, 2, 0, create_home
},
3456 { "remove", 2, VERB_ANY
, 0, remove_home
},
3457 { "update", VERB_ANY
, 2, 0, update_home
},
3458 { "passwd", VERB_ANY
, 2, 0, passwd_home
},
3459 { "resize", 2, 3, 0, resize_home
},
3460 { "lock", 2, VERB_ANY
, 0, lock_home
},
3461 { "unlock", 2, VERB_ANY
, 0, unlock_home
},
3462 { "with", 2, VERB_ANY
, 0, with_home
},
3463 { "lock-all", VERB_ANY
, 1, 0, lock_all_homes
},
3464 { "deactivate-all", VERB_ANY
, 1, 0, deactivate_all_homes
},
3472 r
= redirect_bus_mgr();
3476 r
= parse_argv(argc
, argv
);
3480 return dispatch_verb(argc
, argv
, verbs
, NULL
);
3483 DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run
);