1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #include "alloc-util.h"
8 #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"
18 #include "format-util.h"
20 #include "hexdecoct.h"
21 #include "home-util.h"
22 #include "homectl-fido2.h"
23 #include "libcrypt-util.h"
24 #include "locale-util.h"
25 #include "main-func.h"
26 #include "memory-util.h"
27 #include "openssl-util.h"
29 #include "parse-util.h"
30 #include "path-util.h"
31 #include "pkcs11-util.h"
32 #include "pretty-print.h"
33 #include "process-util.h"
34 #include "pwquality-util.h"
35 #include "random-util.h"
36 #include "rlimit-util.h"
37 #include "spawn-polkit-agent.h"
38 #include "terminal-util.h"
39 #include "user-record-show.h"
40 #include "user-record-util.h"
41 #include "user-record.h"
42 #include "user-util.h"
45 static PagerFlags arg_pager_flags
= 0;
46 static bool arg_legend
= true;
47 static bool arg_ask_password
= true;
48 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
49 static const char *arg_host
= NULL
;
50 static const char *arg_identity
= NULL
;
51 static JsonVariant
*arg_identity_extra
= NULL
;
52 static JsonVariant
*arg_identity_extra_privileged
= NULL
;
53 static JsonVariant
*arg_identity_extra_this_machine
= NULL
;
54 static JsonVariant
*arg_identity_extra_rlimits
= NULL
;
55 static char **arg_identity_filter
= NULL
; /* this one is also applied to 'privileged' and 'thisMachine' subobjects */
56 static char **arg_identity_filter_rlimits
= NULL
;
57 static uint64_t arg_disk_size
= UINT64_MAX
;
58 static uint64_t arg_disk_size_relative
= UINT64_MAX
;
59 static char **arg_pkcs11_token_uri
= NULL
;
60 static char **arg_fido2_device
= NULL
;
61 static bool arg_json
= false;
62 static JsonFormatFlags arg_json_format_flags
= 0;
63 static bool arg_and_resize
= false;
64 static bool arg_and_change_password
= false;
66 EXPORT_FORMAT_FULL
, /* export the full record */
67 EXPORT_FORMAT_STRIPPED
, /* strip "state" + "binding", but leave signature in place */
68 EXPORT_FORMAT_MINIMAL
, /* also strip signature */
69 } arg_export_format
= EXPORT_FORMAT_FULL
;
71 STATIC_DESTRUCTOR_REGISTER(arg_identity_extra
, json_variant_unrefp
);
72 STATIC_DESTRUCTOR_REGISTER(arg_identity_extra_this_machine
, json_variant_unrefp
);
73 STATIC_DESTRUCTOR_REGISTER(arg_identity_extra_privileged
, json_variant_unrefp
);
74 STATIC_DESTRUCTOR_REGISTER(arg_identity_extra_rlimits
, json_variant_unrefp
);
75 STATIC_DESTRUCTOR_REGISTER(arg_identity_filter
, strv_freep
);
76 STATIC_DESTRUCTOR_REGISTER(arg_identity_filter_rlimits
, strv_freep
);
77 STATIC_DESTRUCTOR_REGISTER(arg_pkcs11_token_uri
, strv_freep
);
78 STATIC_DESTRUCTOR_REGISTER(arg_fido2_device
, strv_freep
);
80 static bool identity_properties_specified(void) {
83 !json_variant_is_blank_object(arg_identity_extra
) ||
84 !json_variant_is_blank_object(arg_identity_extra_privileged
) ||
85 !json_variant_is_blank_object(arg_identity_extra_this_machine
) ||
86 !json_variant_is_blank_object(arg_identity_extra_rlimits
) ||
87 !strv_isempty(arg_identity_filter
) ||
88 !strv_isempty(arg_identity_filter_rlimits
) ||
89 !strv_isempty(arg_pkcs11_token_uri
) ||
90 !strv_isempty(arg_fido2_device
);
93 static int acquire_bus(sd_bus
**bus
) {
101 r
= bus_connect_transport(arg_transport
, arg_host
, false, bus
);
103 return log_error_errno(r
, "Failed to connect to bus: %m");
105 (void) sd_bus_set_allow_interactive_authorization(*bus
, arg_ask_password
);
110 static int list_homes(int argc
, char *argv
[], void *userdata
) {
111 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
112 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
113 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
114 _cleanup_(table_unrefp
) Table
*table
= NULL
;
117 (void) pager_open(arg_pager_flags
);
119 r
= acquire_bus(&bus
);
123 r
= bus_call_method(bus
, bus_home_mgr
, "ListHomes", &error
, &reply
, NULL
);
125 return log_error_errno(r
, "Failed to list homes: %s", bus_error_message(&error
, r
));
127 table
= table_new("name", "uid", "gid", "state", "realname", "home", "shell");
131 r
= sd_bus_message_enter_container(reply
, 'a', "(susussso)");
133 return bus_log_parse_error(r
);
136 const char *name
, *state
, *realname
, *home
, *shell
, *color
;
140 r
= sd_bus_message_read(reply
, "(susussso)", &name
, &uid
, &state
, &gid
, &realname
, &home
, &shell
, NULL
);
142 return bus_log_parse_error(r
);
146 r
= table_add_many(table
,
151 return log_error_errno(r
, "Failed to add row to table: %m");
154 r
= table_add_cell(table
, &cell
, TABLE_STRING
, state
);
156 return log_error_errno(r
, "Failed to add field to table: %m");
158 color
= user_record_state_color(state
);
160 (void) table_set_color(table
, cell
, color
);
162 r
= table_add_many(table
,
163 TABLE_STRING
, strna(empty_to_null(realname
)),
165 TABLE_STRING
, strna(empty_to_null(shell
)));
167 return log_error_errno(r
, "Failed to add row to table: %m");
170 r
= sd_bus_message_exit_container(reply
);
172 return bus_log_parse_error(r
);
174 if (table_get_rows(table
) > 1 || arg_json
) {
175 r
= table_set_sort(table
, (size_t) 0, (size_t) -1);
177 return log_error_errno(r
, "Failed to sort table: %m");
179 table_set_header(table
, arg_legend
);
182 r
= table_print_json(table
, stdout
, arg_json_format_flags
);
184 r
= table_print(table
, NULL
);
186 return log_error_errno(r
, "Failed to show table: %m");
189 if (arg_legend
&& !arg_json
) {
190 if (table_get_rows(table
) > 1)
191 printf("\n%zu home areas listed.\n", table_get_rows(table
) - 1);
193 printf("No home areas.\n");
199 static int acquire_existing_password(const char *user_name
, UserRecord
*hr
, bool emphasize_current
) {
200 _cleanup_(strv_free_erasep
) char **password
= NULL
;
201 _cleanup_free_
char *question
= NULL
;
208 e
= getenv("PASSWORD");
210 /* People really shouldn't use environment variables for passing passwords. We support this
211 * only for testing purposes, and do not document the behaviour, so that people won't
212 * actually use this outside of testing. */
214 r
= user_record_set_password(hr
, STRV_MAKE(e
), true);
216 return log_error_errno(r
, "Failed to store password: %m");
220 if (unsetenv("PASSWORD") < 0)
221 return log_error_errno(errno
, "Failed to unset $PASSWORD: %m");
226 if (asprintf(&question
, emphasize_current
?
227 "Please enter current password for user %s:" :
228 "Please enter password for user %s:",
232 r
= ask_password_auto(question
, "user-home", NULL
, "home-password", USEC_INFINITY
, ASK_PASSWORD_ACCEPT_CACHED
|ASK_PASSWORD_PUSH_CACHE
, &password
);
234 return log_error_errno(r
, "Failed to acquire password: %m");
236 r
= user_record_set_password(hr
, password
, true);
238 return log_error_errno(r
, "Failed to store password: %m");
243 static int acquire_token_pin(const char *user_name
, UserRecord
*hr
) {
244 _cleanup_(strv_free_erasep
) char **pin
= NULL
;
245 _cleanup_free_
char *question
= NULL
;
254 r
= user_record_set_token_pin(hr
, STRV_MAKE(e
), false);
256 return log_error_errno(r
, "Failed to store token PIN: %m");
260 if (unsetenv("PIN") < 0)
261 return log_error_errno(errno
, "Failed to unset $PIN: %m");
266 if (asprintf(&question
, "Please enter security token PIN for user %s:", user_name
) < 0)
269 /* We never cache or use cached PINs, since usually there are only very few attempts allowed before the PIN is blocked */
270 r
= ask_password_auto(question
, "user-home", NULL
, "token-pin", USEC_INFINITY
, 0, &pin
);
272 return log_error_errno(r
, "Failed to acquire security token PIN: %m");
274 r
= user_record_set_token_pin(hr
, pin
, false);
276 return log_error_errno(r
, "Failed to store security token PIN: %m");
281 static int handle_generic_user_record_error(
282 const char *user_name
,
284 const sd_bus_error
*error
,
286 bool emphasize_current_password
) {
292 if (sd_bus_error_has_name(error
, BUS_ERROR_HOME_ABSENT
))
293 return log_error_errno(SYNTHETIC_ERRNO(EREMOTE
),
294 "Home of user %s is currently absent, please plug in the necessary storage device or backing file system.", user_name
);
296 else if (sd_bus_error_has_name(error
, BUS_ERROR_AUTHENTICATION_LIMIT_HIT
))
297 return log_error_errno(SYNTHETIC_ERRNO(ETOOMANYREFS
),
298 "Too frequent unsuccessful login attempts for user %s, try again later.", user_name
);
300 else if (sd_bus_error_has_name(error
, BUS_ERROR_BAD_PASSWORD
)) {
302 if (!strv_isempty(hr
->password
))
303 log_notice("Password incorrect or not sufficient, please try again.");
305 r
= acquire_existing_password(user_name
, hr
, emphasize_current_password
);
309 } else if (sd_bus_error_has_name(error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
)) {
311 if (strv_isempty(hr
->password
))
312 log_notice("Security token not inserted, please enter password.");
314 log_notice("Password incorrect or not sufficient, and configured security token not inserted, please try again.");
316 r
= acquire_existing_password(user_name
, hr
, emphasize_current_password
);
320 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_PIN_NEEDED
)) {
322 r
= acquire_token_pin(user_name
, hr
);
326 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_PROTECTED_AUTHENTICATION_PATH_NEEDED
)) {
328 log_notice("%s%sPlease authenticate physically on security token.",
329 emoji_enabled() ? special_glyph(SPECIAL_GLYPH_TOUCH
) : "",
330 emoji_enabled() ? " " : "");
332 r
= user_record_set_pkcs11_protected_authentication_path_permitted(hr
, true);
334 return log_error_errno(r
, "Failed to set PKCS#11 protected authentication path permitted flag: %m");
336 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_USER_PRESENCE_NEEDED
)) {
338 log_notice("%s%sAuthentication requires presence verification on security token.",
339 emoji_enabled() ? special_glyph(SPECIAL_GLYPH_TOUCH
) : "",
340 emoji_enabled() ? " " : "");
342 r
= user_record_set_fido2_user_presence_permitted(hr
, true);
344 return log_error_errno(r
, "Failed to set FIDO2 user presence permitted flag: %m");
346 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_PIN_LOCKED
))
347 return log_error_errno(SYNTHETIC_ERRNO(EPERM
), "Security token PIN is locked, please unlock it first. (Hint: Removal and re-insertion might suffice.)");
349 else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_BAD_PIN
)) {
351 log_notice("Security token PIN incorrect, please try again.");
353 r
= acquire_token_pin(user_name
, hr
);
357 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_BAD_PIN_FEW_TRIES_LEFT
)) {
359 log_notice("Security token PIN incorrect, please try again (only a few tries left!).");
361 r
= acquire_token_pin(user_name
, hr
);
365 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_BAD_PIN_ONE_TRY_LEFT
)) {
367 log_notice("Security token PIN incorrect, please try again (only one try left!).");
369 r
= acquire_token_pin(user_name
, hr
);
373 return log_error_errno(ret
, "Operation on home %s failed: %s", user_name
, bus_error_message(error
, ret
));
378 static int activate_home(int argc
, char *argv
[], void *userdata
) {
379 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
383 r
= acquire_bus(&bus
);
387 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
388 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
390 secret
= user_record_new();
395 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
396 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
398 r
= bus_message_new_method_call(bus
, &m
, bus_home_mgr
, "ActivateHome");
400 return bus_log_create_error(r
);
402 r
= sd_bus_message_append(m
, "s", *i
);
404 return bus_log_create_error(r
);
406 r
= bus_message_append_secret(m
, secret
);
408 return bus_log_create_error(r
);
410 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
412 r
= handle_generic_user_record_error(*i
, secret
, &error
, r
, false);
427 static int deactivate_home(int argc
, char *argv
[], void *userdata
) {
428 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
432 r
= acquire_bus(&bus
);
436 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
437 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
438 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
440 r
= bus_message_new_method_call(bus
, &m
, bus_home_mgr
, "DeactivateHome");
442 return bus_log_create_error(r
);
444 r
= sd_bus_message_append(m
, "s", *i
);
446 return bus_log_create_error(r
);
448 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
450 log_error_errno(r
, "Failed to deactivate user home: %s", bus_error_message(&error
, r
));
459 static void dump_home_record(UserRecord
*hr
) {
464 if (hr
->incomplete
) {
466 log_warning("Warning: lacking rights to acquire privileged fields of user record of '%s', output incomplete.", hr
->user_name
);
470 _cleanup_(user_record_unrefp
) UserRecord
*stripped
= NULL
;
472 if (arg_export_format
== EXPORT_FORMAT_STRIPPED
)
473 r
= user_record_clone(hr
, USER_RECORD_EXTRACT_EMBEDDED
, &stripped
);
474 else if (arg_export_format
== EXPORT_FORMAT_MINIMAL
)
475 r
= user_record_clone(hr
, USER_RECORD_EXTRACT_SIGNABLE
, &stripped
);
479 log_warning_errno(r
, "Failed to strip user record, ignoring: %m");
483 json_variant_dump(hr
->json
, arg_json_format_flags
, stdout
, NULL
);
485 user_record_show(hr
, true);
488 static char **mangle_user_list(char **list
, char ***ret_allocated
) {
489 _cleanup_free_
char *myself
= NULL
;
492 if (!strv_isempty(list
)) {
493 *ret_allocated
= NULL
;
497 myself
= getusername_malloc();
505 l
[0] = TAKE_PTR(myself
);
512 static int inspect_home(int argc
, char *argv
[], void *userdata
) {
513 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
514 _cleanup_(strv_freep
) char **mangled_list
= NULL
;
518 (void) pager_open(arg_pager_flags
);
520 r
= acquire_bus(&bus
);
524 items
= mangle_user_list(strv_skip(argv
, 1), &mangled_list
);
528 STRV_FOREACH(i
, items
) {
529 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
530 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
531 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
532 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
537 r
= parse_uid(*i
, &uid
);
539 if (!valid_user_group_name(*i
, 0)) {
540 log_error("Invalid user name '%s'.", *i
);
547 r
= bus_call_method(bus
, bus_home_mgr
, "GetUserRecordByName", &error
, &reply
, "s", *i
);
549 r
= bus_call_method(bus
, bus_home_mgr
, "GetUserRecordByUID", &error
, &reply
, "u", (uint32_t) uid
);
552 log_error_errno(r
, "Failed to inspect home: %s", bus_error_message(&error
, r
));
559 r
= sd_bus_message_read(reply
, "sbo", &json
, &incomplete
, NULL
);
561 bus_log_parse_error(r
);
568 r
= json_parse(json
, JSON_PARSE_SENSITIVE
, &v
, NULL
, NULL
);
570 log_error_errno(r
, "Failed to parse JSON identity: %m");
577 hr
= user_record_new();
581 r
= user_record_load(hr
, v
, USER_RECORD_LOAD_REFUSE_SECRET
|USER_RECORD_LOG
);
589 hr
->incomplete
= incomplete
;
590 dump_home_record(hr
);
596 static int authenticate_home(int argc
, char *argv
[], void *userdata
) {
597 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
598 _cleanup_(strv_freep
) char **mangled_list
= NULL
;
602 items
= mangle_user_list(strv_skip(argv
, 1), &mangled_list
);
606 r
= acquire_bus(&bus
);
610 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
612 STRV_FOREACH(i
, items
) {
613 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
615 secret
= user_record_new();
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_home_mgr
, "AuthenticateHome");
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
= bus_message_append_secret(m
, secret
);
633 return bus_log_create_error(r
);
635 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
637 r
= handle_generic_user_record_error(*i
, secret
, &error
, r
, false);
652 static int update_last_change(JsonVariant
**v
, bool with_password
, bool override
) {
659 n
= now(CLOCK_REALTIME
);
661 c
= json_variant_by_key(*v
, "lastChangeUSec");
666 goto update_password
;
668 if (!json_variant_is_unsigned(c
))
669 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastChangeUSec field is not an unsigned integer, refusing.");
671 u
= json_variant_unsigned(c
);
673 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastChangeUSec is from the future, can't update.");
676 r
= json_variant_set_field_unsigned(v
, "lastChangeUSec", n
);
678 return log_error_errno(r
, "Failed to update lastChangeUSec: %m");
684 c
= json_variant_by_key(*v
, "lastPasswordChangeUSec");
691 if (!json_variant_is_unsigned(c
))
692 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastPasswordChangeUSec field is not an unsigned integer, refusing.");
694 u
= json_variant_unsigned(c
);
696 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastPasswordChangeUSec is from the future, can't update.");
699 r
= json_variant_set_field_unsigned(v
, "lastPasswordChangeUSec", n
);
701 return log_error_errno(r
, "Failed to update lastPasswordChangeUSec: %m");
706 static int apply_identity_changes(JsonVariant
**_v
) {
707 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
712 v
= json_variant_ref(*_v
);
714 r
= json_variant_filter(&v
, arg_identity_filter
);
716 return log_error_errno(r
, "Failed to filter identity: %m");
718 r
= json_variant_merge(&v
, arg_identity_extra
);
720 return log_error_errno(r
, "Failed to merge identities: %m");
722 if (arg_identity_extra_this_machine
|| !strv_isempty(arg_identity_filter
)) {
723 _cleanup_(json_variant_unrefp
) JsonVariant
*per_machine
= NULL
, *mmid
= NULL
;
724 char mids
[SD_ID128_STRING_MAX
];
727 r
= sd_id128_get_machine(&mid
);
729 return log_error_errno(r
, "Failed to acquire machine ID: %m");
731 r
= json_variant_new_string(&mmid
, sd_id128_to_string(mid
, mids
));
733 return log_error_errno(r
, "Failed to allocate matchMachineId object: %m");
735 per_machine
= json_variant_ref(json_variant_by_key(v
, "perMachine"));
737 _cleanup_(json_variant_unrefp
) JsonVariant
*npm
= NULL
, *add
= NULL
;
738 _cleanup_free_ JsonVariant
**array
= NULL
;
742 if (!json_variant_is_array(per_machine
))
743 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "perMachine field is not an array, refusing.");
745 array
= new(JsonVariant
*, json_variant_elements(per_machine
) + 1);
749 JSON_VARIANT_ARRAY_FOREACH(z
, per_machine
) {
752 if (!json_variant_is_object(z
))
753 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "perMachine entry is not an object, refusing.");
757 u
= json_variant_by_key(z
, "matchMachineId");
761 if (!json_variant_equal(u
, mmid
))
764 r
= json_variant_merge(&add
, z
);
766 return log_error_errno(r
, "Failed to merge perMachine entry: %m");
771 r
= json_variant_filter(&add
, arg_identity_filter
);
773 return log_error_errno(r
, "Failed to filter perMachine: %m");
775 r
= json_variant_merge(&add
, arg_identity_extra_this_machine
);
777 return log_error_errno(r
, "Failed to merge in perMachine fields: %m");
779 if (arg_identity_filter_rlimits
|| arg_identity_extra_rlimits
) {
780 _cleanup_(json_variant_unrefp
) JsonVariant
*rlv
= NULL
;
782 rlv
= json_variant_ref(json_variant_by_key(add
, "resourceLimits"));
784 r
= json_variant_filter(&rlv
, arg_identity_filter_rlimits
);
786 return log_error_errno(r
, "Failed to filter resource limits: %m");
788 r
= json_variant_merge(&rlv
, arg_identity_extra_rlimits
);
790 return log_error_errno(r
, "Failed to set resource limits: %m");
792 if (json_variant_is_blank_object(rlv
)) {
793 r
= json_variant_filter(&add
, STRV_MAKE("resourceLimits"));
795 return log_error_errno(r
, "Failed to drop resource limits field from identity: %m");
797 r
= json_variant_set_field(&add
, "resourceLimits", rlv
);
799 return log_error_errno(r
, "Failed to update resource limits of identity: %m");
803 if (!json_variant_is_blank_object(add
)) {
804 r
= json_variant_set_field(&add
, "matchMachineId", mmid
);
806 return log_error_errno(r
, "Failed to set matchMachineId field: %m");
811 r
= json_variant_new_array(&npm
, array
, i
);
813 return log_error_errno(r
, "Failed to allocate new perMachine array: %m");
815 json_variant_unref(per_machine
);
816 per_machine
= TAKE_PTR(npm
);
818 _cleanup_(json_variant_unrefp
) JsonVariant
*item
= json_variant_ref(arg_identity_extra_this_machine
);
820 if (arg_identity_extra_rlimits
) {
821 r
= json_variant_set_field(&item
, "resourceLimits", arg_identity_extra_rlimits
);
823 return log_error_errno(r
, "Failed to update resource limits of identity: %m");
826 r
= json_variant_set_field(&item
, "matchMachineId", mmid
);
828 return log_error_errno(r
, "Failed to set matchMachineId field: %m");
830 r
= json_variant_append_array(&per_machine
, item
);
832 return log_error_errno(r
, "Failed to append to perMachine array: %m");
835 r
= json_variant_set_field(&v
, "perMachine", per_machine
);
837 return log_error_errno(r
, "Failed to update per machine record: %m");
840 if (arg_identity_extra_privileged
|| arg_identity_filter
) {
841 _cleanup_(json_variant_unrefp
) JsonVariant
*privileged
= NULL
;
843 privileged
= json_variant_ref(json_variant_by_key(v
, "privileged"));
845 r
= json_variant_filter(&privileged
, arg_identity_filter
);
847 return log_error_errno(r
, "Failed to filter identity (privileged part): %m");
849 r
= json_variant_merge(&privileged
, arg_identity_extra_privileged
);
851 return log_error_errno(r
, "Failed to merge identities (privileged part): %m");
853 if (json_variant_is_blank_object(privileged
)) {
854 r
= json_variant_filter(&v
, STRV_MAKE("privileged"));
856 return log_error_errno(r
, "Failed to drop privileged part from identity: %m");
858 r
= json_variant_set_field(&v
, "privileged", privileged
);
860 return log_error_errno(r
, "Failed to update privileged part of identity: %m");
864 if (arg_identity_filter_rlimits
) {
865 _cleanup_(json_variant_unrefp
) JsonVariant
*rlv
= NULL
;
867 rlv
= json_variant_ref(json_variant_by_key(v
, "resourceLimits"));
869 r
= json_variant_filter(&rlv
, arg_identity_filter_rlimits
);
871 return log_error_errno(r
, "Failed to filter resource limits: %m");
873 /* Note that we only filter resource limits here, but don't apply them. We do that in the perMachine section */
875 if (json_variant_is_blank_object(rlv
)) {
876 r
= json_variant_filter(&v
, STRV_MAKE("resourceLimits"));
878 return log_error_errno(r
, "Failed to drop resource limits field from identity: %m");
880 r
= json_variant_set_field(&v
, "resourceLimits", rlv
);
882 return log_error_errno(r
, "Failed to update resource limits of identity: %m");
886 json_variant_unref(*_v
);
892 static int add_disposition(JsonVariant
**v
) {
897 if (json_variant_by_key(*v
, "disposition"))
900 /* Set the disposition to regular, if not configured explicitly */
901 r
= json_variant_set_field_string(v
, "disposition", "regular");
903 return log_error_errno(r
, "Failed to set disposition field: %m");
908 struct pkcs11_callback_data
{
913 static void pkcs11_callback_data_release(struct pkcs11_callback_data
*data
) {
914 erase_and_free(data
->pin_used
);
915 X509_free(data
->cert
);
919 static int pkcs11_callback(
921 CK_SESSION_HANDLE session
,
923 const CK_SLOT_INFO
*slot_info
,
924 const CK_TOKEN_INFO
*token_info
,
928 _cleanup_(erase_and_freep
) char *pin_used
= NULL
;
929 struct pkcs11_callback_data
*data
= userdata
;
930 CK_OBJECT_HANDLE object
;
939 /* Called for every token matching our URI */
941 r
= pkcs11_token_login(m
, session
, slot_id
, token_info
, "home directory operation", "user-home", "pkcs11-pin", UINT64_MAX
, &pin_used
);
945 r
= pkcs11_token_find_x509_certificate(m
, session
, uri
, &object
);
949 r
= pkcs11_token_read_x509_certificate(m
, session
, object
, &data
->cert
);
953 /* Let's read some random data off the token and write it to the kernel pool before we generate our
954 * random key from it. This way we can claim the quality of the RNG is at least as good as the
955 * kernel's and the token's pool */
956 (void) pkcs11_token_acquire_rng(m
, session
);
958 data
->pin_used
= TAKE_PTR(pin_used
);
963 static int acquire_pkcs11_certificate(
966 char **ret_pin_used
) {
969 _cleanup_(pkcs11_callback_data_release
) struct pkcs11_callback_data data
= {};
972 r
= pkcs11_find_token(uri
, pkcs11_callback
, &data
);
973 if (r
== -EAGAIN
) /* pkcs11_find_token() doesn't log about this error, but all others */
974 return log_error_errno(ENXIO
, "Specified PKCS#11 token with URI '%s' not found.", uri
);
978 *ret_cert
= TAKE_PTR(data
.cert
);
979 *ret_pin_used
= TAKE_PTR(data
.pin_used
);
983 return log_error_errno(EOPNOTSUPP
, "PKCS#11 tokens not supported on this build.");
987 static int encrypt_bytes(
989 const void *decrypted_key
,
990 size_t decrypted_key_size
,
991 void **ret_encrypt_key
,
992 size_t *ret_encrypt_key_size
) {
994 _cleanup_(EVP_PKEY_CTX_freep
) EVP_PKEY_CTX
*ctx
= NULL
;
995 _cleanup_free_
void *b
= NULL
;
998 ctx
= EVP_PKEY_CTX_new(pkey
, NULL
);
1000 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Failed to allocate public key context");
1002 if (EVP_PKEY_encrypt_init(ctx
) <= 0)
1003 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Failed to initialize public key context");
1005 if (EVP_PKEY_CTX_set_rsa_padding(ctx
, RSA_PKCS1_PADDING
) <= 0)
1006 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Failed to configure PKCS#1 padding");
1008 if (EVP_PKEY_encrypt(ctx
, NULL
, &l
, decrypted_key
, decrypted_key_size
) <= 0)
1009 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Failed to determine encrypted key size");
1015 if (EVP_PKEY_encrypt(ctx
, b
, &l
, decrypted_key
, decrypted_key_size
) <= 0)
1016 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Failed to determine encrypted key size");
1018 *ret_encrypt_key
= TAKE_PTR(b
);
1019 *ret_encrypt_key_size
= l
;
1024 static int add_token_pin(JsonVariant
**v
, const char *pin
) {
1025 _cleanup_(json_variant_unrefp
) JsonVariant
*w
= NULL
, *l
= NULL
;
1026 _cleanup_(strv_free_erasep
) char **pins
= NULL
;
1034 w
= json_variant_ref(json_variant_by_key(*v
, "secret"));
1035 l
= json_variant_ref(json_variant_by_key(w
, "tokenPin"));
1037 r
= json_variant_strv(l
, &pins
);
1039 return log_error_errno(r
, "Failed to convert PIN array: %m");
1041 if (strv_find(pins
, pin
))
1044 r
= strv_extend(&pins
, pin
);
1050 l
= json_variant_unref(l
);
1052 r
= json_variant_new_array_strv(&l
, pins
);
1054 return log_error_errno(r
, "Failed to allocate new PIN array JSON: %m");
1056 json_variant_sensitive(l
);
1058 r
= json_variant_set_field(&w
, "tokenPin", l
);
1060 return log_error_errno(r
, "Failed to update PIN field: %m");
1062 r
= json_variant_set_field(v
, "secret", w
);
1064 return log_error_errno(r
, "Failed to update secret object: %m");
1069 static int add_pkcs11_encrypted_key(
1072 const void *encrypted_key
, size_t encrypted_key_size
,
1073 const void *decrypted_key
, size_t decrypted_key_size
) {
1075 _cleanup_(json_variant_unrefp
) JsonVariant
*l
= NULL
, *w
= NULL
, *e
= NULL
;
1076 _cleanup_(erase_and_freep
) char *base64_encoded
= NULL
;
1077 _cleanup_free_
char *salt
= NULL
;
1078 struct crypt_data cd
= {};
1084 assert(encrypted_key
);
1085 assert(encrypted_key_size
> 0);
1086 assert(decrypted_key
);
1087 assert(decrypted_key_size
> 0);
1089 r
= make_salt(&salt
);
1091 return log_error_errno(r
, "Failed to generate salt: %m");
1093 /* Before using UNIX hashing on the supplied key we base64 encode it, since crypt_r() and friends
1094 * expect a NUL terminated string, and we use a binary key */
1095 r
= base64mem(decrypted_key
, decrypted_key_size
, &base64_encoded
);
1097 return log_error_errno(r
, "Failed to base64 encode secret key: %m");
1100 k
= crypt_r(base64_encoded
, salt
, &cd
);
1102 return log_error_errno(errno_or_else(EINVAL
), "Failed to UNIX hash secret key: %m");
1104 r
= json_build(&e
, JSON_BUILD_OBJECT(
1105 JSON_BUILD_PAIR("uri", JSON_BUILD_STRING(uri
)),
1106 JSON_BUILD_PAIR("data", JSON_BUILD_BASE64(encrypted_key
, encrypted_key_size
)),
1107 JSON_BUILD_PAIR("hashedPassword", JSON_BUILD_STRING(k
))));
1109 return log_error_errno(r
, "Failed to build encrypted JSON key object: %m");
1111 w
= json_variant_ref(json_variant_by_key(*v
, "privileged"));
1112 l
= json_variant_ref(json_variant_by_key(w
, "pkcs11EncryptedKey"));
1114 r
= json_variant_append_array(&l
, e
);
1116 return log_error_errno(r
, "Failed append PKCS#11 encrypted key: %m");
1118 r
= json_variant_set_field(&w
, "pkcs11EncryptedKey", l
);
1120 return log_error_errno(r
, "Failed to set PKCS#11 encrypted key: %m");
1122 r
= json_variant_set_field(v
, "privileged", w
);
1124 return log_error_errno(r
, "Failed to update privileged field: %m");
1129 static int add_pkcs11_token_uri(JsonVariant
**v
, const char *uri
) {
1130 _cleanup_(json_variant_unrefp
) JsonVariant
*w
= NULL
;
1131 _cleanup_strv_free_
char **l
= NULL
;
1137 w
= json_variant_ref(json_variant_by_key(*v
, "pkcs11TokenUri"));
1139 r
= json_variant_strv(w
, &l
);
1141 return log_error_errno(r
, "Failed to parse PKCS#11 token list: %m");
1143 if (strv_contains(l
, uri
))
1147 r
= strv_extend(&l
, uri
);
1151 w
= json_variant_unref(w
);
1152 r
= json_variant_new_array_strv(&w
, l
);
1154 return log_error_errno(r
, "Failed to create PKCS#11 token URI JSON: %m");
1156 r
= json_variant_set_field(v
, "pkcs11TokenUri", w
);
1158 return log_error_errno(r
, "Failed to update PKCS#11 token URI list: %m");
1163 static int add_pkcs11_key_data(JsonVariant
**v
, const char *uri
) {
1164 _cleanup_(erase_and_freep
) void *decrypted_key
= NULL
, *encrypted_key
= NULL
;
1165 _cleanup_(erase_and_freep
) char *pin
= NULL
;
1166 size_t decrypted_key_size
, encrypted_key_size
;
1167 _cleanup_(X509_freep
) X509
*cert
= NULL
;
1175 r
= acquire_pkcs11_certificate(uri
, &cert
, &pin
);
1179 pkey
= X509_get0_pubkey(cert
);
1181 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Failed to extract public key from X.509 certificate.");
1183 if (EVP_PKEY_base_id(pkey
) != EVP_PKEY_RSA
)
1184 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "X.509 certificate does not refer to RSA key.");
1186 rsa
= EVP_PKEY_get0_RSA(pkey
);
1188 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Failed to acquire RSA public key from X.509 certificate.");
1190 bits
= RSA_bits(rsa
);
1191 log_debug("Bits in RSA key: %i", bits
);
1193 /* We use PKCS#1 padding for the RSA cleartext, hence let's leave some extra space for it, hence only
1194 * generate a random key half the size of the RSA length */
1195 decrypted_key_size
= bits
/ 8 / 2;
1197 if (decrypted_key_size
< 1)
1198 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Uh, RSA key size too short?");
1200 log_debug("Generating %zu bytes random key.", decrypted_key_size
);
1202 decrypted_key
= malloc(decrypted_key_size
);
1206 r
= genuine_random_bytes(decrypted_key
, decrypted_key_size
, RANDOM_BLOCK
);
1208 return log_error_errno(r
, "Failed to generate random key: %m");
1210 r
= encrypt_bytes(pkey
, decrypted_key
, decrypted_key_size
, &encrypted_key
, &encrypted_key_size
);
1212 return log_error_errno(r
, "Failed to encrypt key: %m");
1214 /* Add the token URI to the public part of the record. */
1215 r
= add_pkcs11_token_uri(v
, uri
);
1219 /* Include the encrypted version of the random key we just generated in the privileged part of the record */
1220 r
= add_pkcs11_encrypted_key(
1223 encrypted_key
, encrypted_key_size
,
1224 decrypted_key
, decrypted_key_size
);
1228 /* If we acquired the PIN also include it in the secret section of the record, so that systemd-homed
1229 * can use it if it needs to, given that it likely needs to decrypt the key again to pass to LUKS or
1231 r
= add_token_pin(v
, pin
);
1238 static int acquire_new_home_record(UserRecord
**ret
) {
1239 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
1240 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
1247 unsigned line
, column
;
1249 r
= json_parse_file(
1250 streq(arg_identity
, "-") ? stdin
: NULL
,
1251 streq(arg_identity
, "-") ? "<stdin>" : arg_identity
, JSON_PARSE_SENSITIVE
, &v
, &line
, &column
);
1253 return log_error_errno(r
, "Failed to parse identity at %u:%u: %m", line
, column
);
1256 r
= apply_identity_changes(&v
);
1260 r
= add_disposition(&v
);
1264 STRV_FOREACH(i
, arg_pkcs11_token_uri
) {
1265 r
= add_pkcs11_key_data(&v
, *i
);
1270 STRV_FOREACH(i
, arg_fido2_device
) {
1271 r
= identity_add_fido2_parameters(&v
, *i
);
1276 r
= update_last_change(&v
, true, false);
1281 json_variant_dump(v
, JSON_FORMAT_PRETTY
, NULL
, NULL
);
1283 hr
= user_record_new();
1287 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
);
1291 *ret
= TAKE_PTR(hr
);
1295 static int acquire_new_password(
1296 const char *user_name
,
1307 e
= getenv("NEWPASSWORD");
1309 /* As above, this is not for use, just for testing */
1311 r
= user_record_set_password(hr
, STRV_MAKE(e
), /* prepend = */ false);
1313 return log_error_errno(r
, "Failed to store password: %m");
1317 if (unsetenv("NEWPASSWORD") < 0)
1318 return log_error_errno(errno
, "Failed to unset $NEWPASSWORD: %m");
1324 (void) suggest_passwords();
1327 _cleanup_(strv_free_erasep
) char **first
= NULL
, **second
= NULL
;
1328 _cleanup_free_
char *question
= NULL
;
1331 return log_error_errno(SYNTHETIC_ERRNO(ENOKEY
), "Too many attempts, giving up:");
1333 if (asprintf(&question
, "Please enter new password for user %s:", user_name
) < 0)
1336 r
= ask_password_auto(question
, "user-home", NULL
, "home-password", USEC_INFINITY
, 0, &first
);
1338 return log_error_errno(r
, "Failed to acquire password: %m");
1340 question
= mfree(question
);
1341 if (asprintf(&question
, "Please enter new password for user %s (repeat):", user_name
) < 0)
1344 r
= ask_password_auto(question
, "user-home", NULL
, "home-password", USEC_INFINITY
, 0, &second
);
1346 return log_error_errno(r
, "Failed to acquire password: %m");
1348 if (strv_equal(first
, second
)) {
1349 r
= user_record_set_password(hr
, first
, /* prepend = */ false);
1351 return log_error_errno(r
, "Failed to store password: %m");
1356 log_error("Password didn't match, try again.");
1360 static int create_home(int argc
, char *argv
[], void *userdata
) {
1361 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1362 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
1363 _cleanup_strv_free_
char **original_hashed_passwords
= NULL
;
1366 r
= acquire_bus(&bus
);
1370 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1373 /* If a username was specified, use it */
1375 if (valid_user_group_name(argv
[1], 0))
1376 r
= json_variant_set_field_string(&arg_identity_extra
, "userName", argv
[1]);
1378 _cleanup_free_
char *un
= NULL
, *rr
= NULL
;
1380 /* Before we consider the user name invalid, let's check if we can split it? */
1381 r
= split_user_name_realm(argv
[1], &un
, &rr
);
1383 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User name '%s' is not valid: %m", argv
[1]);
1386 r
= json_variant_set_field_string(&arg_identity_extra
, "realm", rr
);
1388 return log_error_errno(r
, "Failed to set realm field: %m");
1391 r
= json_variant_set_field_string(&arg_identity_extra
, "userName", un
);
1394 return log_error_errno(r
, "Failed to set userName field: %m");
1396 /* If neither a username nor an identity have been specified we cannot operate. */
1398 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User name required.");
1401 r
= acquire_new_home_record(&hr
);
1405 /* Remember the original hashed passwords before we add our own, so that we can return to them later,
1406 * should the entered password turn out not to be acceptable. */
1407 original_hashed_passwords
= strv_copy(hr
->hashed_password
);
1408 if (!original_hashed_passwords
)
1411 /* If the JSON record carries no plain text password, then let's query it manually. */
1412 if (!hr
->password
) {
1414 if (strv_isempty(hr
->hashed_password
)) {
1415 /* No regular (i.e. non-PKCS#11) hashed passwords set in the record, let's fix that. */
1416 r
= acquire_new_password(hr
->user_name
, hr
, /* suggest = */ true);
1420 r
= user_record_make_hashed_password(hr
, hr
->password
, /* extend = */ true);
1422 return log_error_errno(r
, "Failed to hash password: %m");
1424 /* There's a hash password set in the record, acquire the unhashed version of it. */
1425 r
= acquire_existing_password(hr
->user_name
, hr
, false);
1431 if (hr
->enforce_password_policy
== 0) {
1432 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1434 /* If password quality enforcement is disabled, let's at least warn client side */
1436 r
= quality_check_password(hr
, hr
, &error
);
1438 log_warning_errno(r
, "Specified password does not pass quality checks (%s), proceeding anyway.", bus_error_message(&error
, r
));
1442 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1443 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1444 _cleanup_(erase_and_freep
) char *formatted
= NULL
;
1446 r
= json_variant_format(hr
->json
, 0, &formatted
);
1448 return log_error_errno(r
, "Failed to format user record: %m");
1450 r
= bus_message_new_method_call(bus
, &m
, bus_home_mgr
, "CreateHome");
1452 return bus_log_create_error(r
);
1454 (void) sd_bus_message_sensitive(m
);
1456 r
= sd_bus_message_append(m
, "s", formatted
);
1458 return bus_log_create_error(r
);
1460 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1462 if (!sd_bus_error_has_name(&error
, BUS_ERROR_LOW_PASSWORD_QUALITY
))
1463 return log_error_errno(r
, "Failed to create user home: %s", bus_error_message(&error
, r
));
1465 log_error_errno(r
, "%s", bus_error_message(&error
, r
));
1466 log_info("(Use --enforce-password-policy=no to turn off password quality checks for this account.)");
1470 r
= user_record_set_hashed_password(hr
, original_hashed_passwords
);
1474 r
= acquire_new_password(hr
->user_name
, hr
, /* suggest = */ false);
1478 r
= user_record_make_hashed_password(hr
, hr
->password
, /* extend = */ true);
1480 return log_error_errno(r
, "Failed to hash passwords: %m");
1486 static int remove_home(int argc
, char *argv
[], void *userdata
) {
1487 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1491 r
= acquire_bus(&bus
);
1495 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1497 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
1498 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1499 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1501 r
= bus_message_new_method_call(bus
, &m
, bus_home_mgr
, "RemoveHome");
1503 return bus_log_create_error(r
);
1505 r
= sd_bus_message_append(m
, "s", *i
);
1507 return bus_log_create_error(r
);
1509 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1511 log_error_errno(r
, "Failed to remove home: %s", bus_error_message(&error
, r
));
1520 static int acquire_updated_home_record(
1522 const char *username
,
1525 _cleanup_(json_variant_unrefp
) JsonVariant
*json
= NULL
;
1526 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
1533 unsigned line
, column
;
1536 r
= json_parse_file(
1537 streq(arg_identity
, "-") ? stdin
: NULL
,
1538 streq(arg_identity
, "-") ? "<stdin>" : arg_identity
, JSON_PARSE_SENSITIVE
, &json
, &line
, &column
);
1540 return log_error_errno(r
, "Failed to parse identity at %u:%u: %m", line
, column
);
1542 un
= json_variant_by_key(json
, "userName");
1544 if (!json_variant_is_string(un
) || (username
&& !streq(json_variant_string(un
), username
)))
1545 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User name specified on command line and in JSON record do not match.");
1548 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "No username specified.");
1550 r
= json_variant_set_field_string(&arg_identity_extra
, "userName", username
);
1552 return log_error_errno(r
, "Failed to set userName field: %m");
1556 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1557 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1561 if (!identity_properties_specified())
1562 return log_error_errno(SYNTHETIC_ERRNO(EALREADY
), "No field to change specified.");
1564 r
= bus_call_method(bus
, bus_home_mgr
, "GetUserRecordByName", &error
, &reply
, "s", username
);
1566 return log_error_errno(r
, "Failed to acquire user home record: %s", bus_error_message(&error
, r
));
1568 r
= sd_bus_message_read(reply
, "sbo", &text
, &incomplete
, NULL
);
1570 return bus_log_parse_error(r
);
1573 return log_error_errno(SYNTHETIC_ERRNO(EACCES
), "Lacking rights to acquire user record including privileged metadata, can't update record.");
1575 r
= json_parse(text
, JSON_PARSE_SENSITIVE
, &json
, NULL
, NULL
);
1577 return log_error_errno(r
, "Failed to parse JSON identity: %m");
1579 reply
= sd_bus_message_unref(reply
);
1581 r
= json_variant_filter(&json
, STRV_MAKE("binding", "status", "signature"));
1583 return log_error_errno(r
, "Failed to strip binding and status from record to update: %m");
1586 r
= apply_identity_changes(&json
);
1590 STRV_FOREACH(i
, arg_pkcs11_token_uri
) {
1591 r
= add_pkcs11_key_data(&json
, *i
);
1596 STRV_FOREACH(i
, arg_fido2_device
) {
1597 r
= identity_add_fido2_parameters(&json
, *i
);
1602 /* If the user supplied a full record, then add in lastChange, but do not override. Otherwise always
1604 r
= update_last_change(&json
, arg_pkcs11_token_uri
|| arg_fido2_device
, !arg_identity
);
1609 json_variant_dump(json
, JSON_FORMAT_PRETTY
, NULL
, NULL
);
1611 hr
= user_record_new();
1615 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
);
1619 *ret
= TAKE_PTR(hr
);
1623 static int update_home(int argc
, char *argv
[], void *userdata
) {
1624 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1625 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
1626 _cleanup_free_
char *buffer
= NULL
;
1627 const char *username
;
1632 else if (!arg_identity
) {
1633 buffer
= getusername_malloc();
1641 r
= acquire_bus(&bus
);
1645 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1647 r
= acquire_updated_home_record(bus
, username
, &hr
);
1652 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1653 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1654 _cleanup_free_
char *formatted
= NULL
;
1656 r
= bus_message_new_method_call(bus
, &m
, bus_home_mgr
, "UpdateHome");
1658 return bus_log_create_error(r
);
1660 r
= json_variant_format(hr
->json
, 0, &formatted
);
1662 return log_error_errno(r
, "Failed to format user record: %m");
1664 (void) sd_bus_message_sensitive(m
);
1666 r
= sd_bus_message_append(m
, "s", formatted
);
1668 return bus_log_create_error(r
);
1670 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1672 if (arg_and_change_password
&&
1673 sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1674 /* In the generic handler we'd ask for a password in this case, but when
1675 * changing passwords that's not sufficient, as we need to acquire all keys
1677 return log_error_errno(r
, "Security token not inserted, refusing.");
1679 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1686 /* Also sync down disk size to underlying LUKS/fscrypt/quota */
1687 while (arg_and_resize
) {
1688 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1689 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1691 log_debug("Resizing");
1693 r
= bus_message_new_method_call(bus
, &m
, bus_home_mgr
, "ResizeHome");
1695 return bus_log_create_error(r
);
1697 /* Specify UINT64_MAX as size, in which case the underlying disk size will just be synced */
1698 r
= sd_bus_message_append(m
, "st", hr
->user_name
, UINT64_MAX
);
1700 return bus_log_create_error(r
);
1702 r
= bus_message_append_secret(m
, hr
);
1704 return bus_log_create_error(r
);
1706 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1708 if (arg_and_change_password
&&
1709 sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1710 return log_error_errno(r
, "Security token not inserted, refusing.");
1712 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1719 /* Also sync down passwords to underlying LUKS/fscrypt */
1720 while (arg_and_change_password
) {
1721 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1722 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1724 log_debug("Propagating password");
1726 r
= bus_message_new_method_call(bus
, &m
, bus_home_mgr
, "ChangePasswordHome");
1728 return bus_log_create_error(r
);
1730 /* Specify an empty new secret, in which case the underlying LUKS/fscrypt password will just be synced */
1731 r
= sd_bus_message_append(m
, "ss", hr
->user_name
, "{}");
1733 return bus_log_create_error(r
);
1735 r
= bus_message_append_secret(m
, hr
);
1737 return bus_log_create_error(r
);
1739 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1741 if (sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1742 return log_error_errno(r
, "Security token not inserted, refusing.");
1744 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1754 static int passwd_home(int argc
, char *argv
[], void *userdata
) {
1755 _cleanup_(user_record_unrefp
) UserRecord
*old_secret
= NULL
, *new_secret
= NULL
;
1756 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1757 _cleanup_free_
char *buffer
= NULL
;
1758 const char *username
;
1761 if (arg_pkcs11_token_uri
)
1762 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "To change the PKCS#11 security token use 'homectl update --pkcs11-token-uri=…'.");
1763 if (arg_fido2_device
)
1764 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "To change the FIDO2 security token use 'homectl update --fido2-device=…'.");
1765 if (identity_properties_specified())
1766 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "The 'passwd' verb does not permit changing other record properties at the same time.");
1771 buffer
= getusername_malloc();
1778 r
= acquire_bus(&bus
);
1782 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1784 old_secret
= user_record_new();
1788 new_secret
= user_record_new();
1792 r
= acquire_new_password(username
, new_secret
, /* suggest = */ true);
1797 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1798 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1800 r
= bus_message_new_method_call(bus
, &m
, bus_home_mgr
, "ChangePasswordHome");
1802 return bus_log_create_error(r
);
1804 r
= sd_bus_message_append(m
, "s", username
);
1806 return bus_log_create_error(r
);
1808 r
= bus_message_append_secret(m
, new_secret
);
1810 return bus_log_create_error(r
);
1812 r
= bus_message_append_secret(m
, old_secret
);
1814 return bus_log_create_error(r
);
1816 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1818 if (sd_bus_error_has_name(&error
, BUS_ERROR_LOW_PASSWORD_QUALITY
)) {
1820 log_error_errno(r
, "%s", bus_error_message(&error
, r
));
1822 r
= acquire_new_password(username
, new_secret
, /* suggest = */ false);
1824 } else if (sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1826 /* In the generic handler we'd ask for a password in this case, but when
1827 * changing passwords that's not sufficeint, as we need to acquire all keys
1829 return log_error_errno(r
, "Security token not inserted, refusing.");
1831 r
= handle_generic_user_record_error(username
, old_secret
, &error
, r
, true);
1841 static int resize_home(int argc
, char *argv
[], void *userdata
) {
1842 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1843 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
1844 uint64_t ds
= UINT64_MAX
;
1847 r
= acquire_bus(&bus
);
1851 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1853 if (arg_disk_size_relative
!= UINT64_MAX
||
1854 (argc
> 2 && parse_percent(argv
[2]) >= 0))
1855 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
1856 "Relative disk size specification currently not supported when resizing.");
1859 r
= parse_size(argv
[2], 1024, &ds
);
1861 return log_error_errno(r
, "Failed to parse disk size parameter: %s", argv
[2]);
1864 if (arg_disk_size
!= UINT64_MAX
) {
1865 if (ds
!= UINT64_MAX
&& ds
!= arg_disk_size
)
1866 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Disk size specified twice and doesn't match, refusing.");
1871 secret
= user_record_new();
1876 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1877 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1879 r
= bus_message_new_method_call(bus
, &m
, bus_home_mgr
, "ResizeHome");
1881 return bus_log_create_error(r
);
1883 r
= sd_bus_message_append(m
, "st", argv
[1], ds
);
1885 return bus_log_create_error(r
);
1887 r
= bus_message_append_secret(m
, secret
);
1889 return bus_log_create_error(r
);
1891 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1893 r
= handle_generic_user_record_error(argv
[1], secret
, &error
, r
, false);
1903 static int lock_home(int argc
, char *argv
[], void *userdata
) {
1904 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1908 r
= acquire_bus(&bus
);
1912 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
1913 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1914 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1916 r
= bus_message_new_method_call(bus
, &m
, bus_home_mgr
, "LockHome");
1918 return bus_log_create_error(r
);
1920 r
= sd_bus_message_append(m
, "s", *i
);
1922 return bus_log_create_error(r
);
1924 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1926 log_error_errno(r
, "Failed to lock home: %s", bus_error_message(&error
, r
));
1935 static int unlock_home(int argc
, char *argv
[], void *userdata
) {
1936 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1940 r
= acquire_bus(&bus
);
1944 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
1945 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
1947 secret
= user_record_new();
1952 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1953 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1955 r
= bus_message_new_method_call(bus
, &m
, bus_home_mgr
, "UnlockHome");
1957 return bus_log_create_error(r
);
1959 r
= sd_bus_message_append(m
, "s", *i
);
1961 return bus_log_create_error(r
);
1963 r
= bus_message_append_secret(m
, secret
);
1965 return bus_log_create_error(r
);
1967 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1969 r
= handle_generic_user_record_error(argv
[1], secret
, &error
, r
, false);
1984 static int with_home(int argc
, char *argv
[], void *userdata
) {
1985 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1986 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1987 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1988 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
1989 _cleanup_close_
int acquired_fd
= -1;
1990 _cleanup_strv_free_
char **cmdline
= NULL
;
1995 r
= acquire_bus(&bus
);
2000 _cleanup_free_
char *shell
= NULL
;
2002 /* If no command is specified, spawn a shell */
2003 r
= get_shell(&shell
);
2005 return log_error_errno(r
, "Failed to acquire shell: %m");
2007 cmdline
= strv_new(shell
);
2009 cmdline
= strv_copy(argv
+ 2);
2013 secret
= user_record_new();
2018 r
= bus_message_new_method_call(bus
, &m
, bus_home_mgr
, "AcquireHome");
2020 return bus_log_create_error(r
);
2022 r
= sd_bus_message_append(m
, "s", argv
[1]);
2024 return bus_log_create_error(r
);
2026 r
= bus_message_append_secret(m
, secret
);
2028 return bus_log_create_error(r
);
2030 r
= sd_bus_message_append(m
, "b", /* please_suspend = */ getenv_bool("SYSTEMD_PLEASE_SUSPEND_HOME") > 0);
2032 return bus_log_create_error(r
);
2034 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, &reply
);
2035 m
= sd_bus_message_unref(m
);
2037 r
= handle_generic_user_record_error(argv
[1], secret
, &error
, r
, false);
2041 sd_bus_error_free(&error
);
2045 r
= sd_bus_message_read(reply
, "h", &fd
);
2047 return bus_log_parse_error(r
);
2049 acquired_fd
= fcntl(fd
, F_DUPFD_CLOEXEC
, 3);
2050 if (acquired_fd
< 0)
2051 return log_error_errno(errno
, "Failed to duplicate acquired fd: %m");
2053 reply
= sd_bus_message_unref(reply
);
2058 r
= bus_call_method(bus
, bus_home_mgr
, "GetHomeByName", &error
, &reply
, "s", argv
[1]);
2060 return log_error_errno(r
, "Failed to inspect home: %s", bus_error_message(&error
, r
));
2062 r
= sd_bus_message_read(reply
, "usussso", NULL
, NULL
, NULL
, NULL
, &home
, NULL
, NULL
);
2064 return bus_log_parse_error(r
);
2066 r
= safe_fork("(with)", FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_DEATHSIG
|FORK_LOG
|FORK_RLIMIT_NOFILE_SAFE
|FORK_REOPEN_LOG
, &pid
);
2070 if (chdir(home
) < 0) {
2071 log_error_errno(errno
, "Failed to change to directory %s: %m", home
);
2075 execvp(cmdline
[0], cmdline
);
2076 log_error_errno(errno
, "Failed to execute %s: %m", cmdline
[0]);
2080 ret
= wait_for_terminate_and_check(cmdline
[0], pid
, WAIT_LOG_ABNORMAL
);
2082 /* Close the fd that pings the home now. */
2083 acquired_fd
= safe_close(acquired_fd
);
2085 r
= bus_message_new_method_call(bus
, &m
, bus_home_mgr
, "ReleaseHome");
2087 return bus_log_create_error(r
);
2089 r
= sd_bus_message_append(m
, "s", argv
[1]);
2091 return bus_log_create_error(r
);
2093 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
2095 if (sd_bus_error_has_name(&error
, BUS_ERROR_HOME_BUSY
))
2096 log_notice("Not deactivating home directory of %s, as it is still used.", argv
[1]);
2098 return log_error_errno(r
, "Failed to release user home: %s", bus_error_message(&error
, r
));
2104 static int lock_all_homes(int argc
, char *argv
[], void *userdata
) {
2105 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2106 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2107 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
2110 r
= acquire_bus(&bus
);
2114 r
= bus_message_new_method_call(bus
, &m
, bus_home_mgr
, "LockAllHomes");
2116 return bus_log_create_error(r
);
2118 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
2120 return log_error_errno(r
, "Failed to lock home: %s", bus_error_message(&error
, r
));
2125 static int drop_from_identity(const char *field
) {
2130 /* If we are called to update an identity record and drop some field, let's keep track of what to
2131 * remove from the old record */
2132 r
= strv_extend(&arg_identity_filter
, field
);
2136 /* Let's also drop the field if it was previously set to a new value on the same command line */
2137 r
= json_variant_filter(&arg_identity_extra
, STRV_MAKE(field
));
2139 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
2141 r
= json_variant_filter(&arg_identity_extra_this_machine
, STRV_MAKE(field
));
2143 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
2145 r
= json_variant_filter(&arg_identity_extra_privileged
, STRV_MAKE(field
));
2147 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
2152 static int help(int argc
, char *argv
[], void *userdata
) {
2153 _cleanup_free_
char *link
= NULL
;
2156 (void) pager_open(arg_pager_flags
);
2158 r
= terminal_urlify_man("homectl", "1", &link
);
2162 printf("%1$s [OPTIONS...] COMMAND ...\n\n"
2163 "%2$sCreate, manipulate or inspect home directories.%3$s\n"
2164 "\n%4$sCommands:%5$s\n"
2165 " list List home areas\n"
2166 " activate USER… Activate a home area\n"
2167 " deactivate USER… Deactivate a home area\n"
2168 " inspect USER… Inspect a home area\n"
2169 " authenticate USER… Authenticate a home area\n"
2170 " create USER Create a home area\n"
2171 " remove USER… Remove a home area\n"
2172 " update USER Update a home area\n"
2173 " passwd USER Change password of a home area\n"
2174 " resize USER SIZE Resize a home area\n"
2175 " lock USER… Temporarily lock an active home area\n"
2176 " unlock USER… Unlock a temporarily locked home area\n"
2177 " lock-all Lock all suitable home areas\n"
2178 " with USER [COMMAND…] Run shell or command with access to a home area\n"
2179 "\n%4$sOptions:%5$s\n"
2180 " -h --help Show this help\n"
2181 " --version Show package version\n"
2182 " --no-pager Do not pipe output into a pager\n"
2183 " --no-legend Do not show the headers and footers\n"
2184 " --no-ask-password Do not ask for system passwords\n"
2185 " -H --host=[USER@]HOST Operate on remote host\n"
2186 " -M --machine=CONTAINER Operate on local container\n"
2187 " --identity=PATH Read JSON identity from file\n"
2188 " --json=FORMAT Output inspection data in JSON (takes one of\n"
2189 " pretty, short, off)\n"
2190 " -j Equivalent to --json=pretty (on TTY) or\n"
2191 " --json=short (otherwise)\n"
2192 " --export-format= Strip JSON inspection data (full, stripped,\n"
2194 " -E When specified once equals -j --export-format=\n"
2195 " stripped, when specified twice equals\n"
2196 " -j --export-format=minimal\n"
2197 "\n%4$sGeneral User Record Properties:%5$s\n"
2198 " -c --real-name=REALNAME Real name for user\n"
2199 " --realm=REALM Realm to create user in\n"
2200 " --email-address=EMAIL Email address for user\n"
2201 " --location=LOCATION Set location of user on earth\n"
2202 " --icon-name=NAME Icon name for user\n"
2203 " -d --home-dir=PATH Home directory\n"
2204 " -u --uid=UID Numeric UID for user\n"
2205 " -G --member-of=GROUP Add user to group\n"
2206 " --skel=PATH Skeleton directory to use\n"
2207 " --shell=PATH Shell for account\n"
2208 " --setenv=VARIABLE=VALUE Set an environment variable at log-in\n"
2209 " --timezone=TIMEZONE Set a time-zone\n"
2210 " --language=LOCALE Set preferred language\n"
2211 " --ssh-authorized-keys=KEYS\n"
2212 " Specify SSH public keys\n"
2213 " --pkcs11-token-uri=URI URI to PKCS#11 security token containing\n"
2214 " private key and matching X.509 certificate\n"
2215 " --fido2-device=PATH Path to FIDO2 hidraw device with hmac-secret\n"
2217 "\n%4$sAccount Management User Record Properties:%5$s\n"
2218 " --locked=BOOL Set locked account state\n"
2219 " --not-before=TIMESTAMP Do not allow logins before\n"
2220 " --not-after=TIMESTAMP Do not allow logins after\n"
2221 " --rate-limit-interval=SECS\n"
2222 " Login rate-limit interval in seconds\n"
2223 " --rate-limit-burst=NUMBER\n"
2224 " Login rate-limit attempts per interval\n"
2225 "\n%4$sPassword Policy User Record Properties:%5$s\n"
2226 " --password-hint=HINT Set Password hint\n"
2227 " --enforce-password-policy=BOOL\n"
2228 " Control whether to enforce system's password\n"
2229 " policy for this user\n"
2230 " -P Equivalent to --enforce-password-password=no\n"
2231 " --password-change-now=BOOL\n"
2232 " Require the password to be changed on next login\n"
2233 " --password-change-min=TIME\n"
2234 " Require minimum time between password changes\n"
2235 " --password-change-max=TIME\n"
2236 " Require maximum time between password changes\n"
2237 " --password-change-warn=TIME\n"
2238 " How much time to warn before password expiry\n"
2239 " --password-change-inactive=TIME\n"
2240 " How much time to block password after expiry\n"
2241 "\n%4$sResource Management User Record Properties:%5$s\n"
2242 " --disk-size=BYTES Size to assign the user on disk\n"
2243 " --access-mode=MODE User home directory access mode\n"
2244 " --umask=MODE Umask for user when logging in\n"
2245 " --nice=NICE Nice level for user\n"
2246 " --rlimit=LIMIT=VALUE[:VALUE]\n"
2247 " Set resource limits\n"
2248 " --tasks-max=MAX Set maximum number of per-user tasks\n"
2249 " --memory-high=BYTES Set high memory threshold in bytes\n"
2250 " --memory-max=BYTES Set maximum memory limit\n"
2251 " --cpu-weight=WEIGHT Set CPU weight\n"
2252 " --io-weight=WEIGHT Set IO weight\n"
2253 "\n%4$sStorage User Record Properties:%5$s\n"
2254 " --storage=STORAGE Storage type to use (luks, fscrypt, directory,\n"
2255 " subvolume, cifs)\n"
2256 " --image-path=PATH Path to image file/directory\n"
2257 "\n%4$sLUKS Storage User Record Properties:%5$s\n"
2258 " --fs-type=TYPE File system type to use in case of luks\n"
2259 " storage (ext4, xfs, btrfs)\n"
2260 " --luks-discard=BOOL Whether to use 'discard' feature of file system\n"
2261 " when activated (mounted)\n"
2262 " --luks-offline-discard=BOOL\n"
2263 " Whether to trim file on logout\n"
2264 " --luks-cipher=CIPHER Cipher to use for LUKS encryption\n"
2265 " --luks-cipher-mode=MODE Cipher mode to use for LUKS encryption\n"
2266 " --luks-volume-key-size=BITS\n"
2267 " Volume key size to use for LUKS encryption\n"
2268 " --luks-pbkdf-type=TYPE Password-based Key Derivation Function to use\n"
2269 " --luks-pbkdf-hash-algorithm=ALGORITHM\n"
2270 " PBKDF hash algorithm to use\n"
2271 " --luks-pbkdf-time-cost=SECS\n"
2272 " Time cost for PBKDF in seconds\n"
2273 " --luks-pbkdf-memory-cost=BYTES\n"
2274 " Memory cost for PBKDF in bytes\n"
2275 " --luks-pbkdf-parallel-threads=NUMBER\n"
2276 " Number of parallel threads for PKBDF\n"
2277 "\n%4$sMounting User Record Properties:%5$s\n"
2278 " --nosuid=BOOL Control the 'nosuid' flag of the home mount\n"
2279 " --nodev=BOOL Control the 'nodev' flag of the home mount\n"
2280 " --noexec=BOOL Control the 'noexec' flag of the home mount\n"
2281 "\n%4$sCIFS User Record Properties:%5$s\n"
2282 " --cifs-domain=DOMAIN CIFS (Windows) domain\n"
2283 " --cifs-user-name=USER CIFS (Windows) user name\n"
2284 " --cifs-service=SERVICE CIFS (Windows) service to mount as home area\n"
2285 "\n%4$sLogin Behaviour User Record Properties:%5$s\n"
2286 " --stop-delay=SECS How long to leave user services running after\n"
2288 " --kill-processes=BOOL Whether to kill user processes when sessions\n"
2290 " --auto-login=BOOL Try to log this user in automatically\n"
2291 "\nSee the %6$s for details.\n"
2292 , program_invocation_short_name
2293 , ansi_highlight(), ansi_normal()
2294 , ansi_underline(), ansi_normal()
2301 static int parse_argv(int argc
, char *argv
[]) {
2304 ARG_VERSION
= 0x100,
2307 ARG_NO_ASK_PASSWORD
,
2317 ARG_LUKS_OFFLINE_DISCARD
,
2323 ARG_SSH_AUTHORIZED_KEYS
,
2332 ARG_LUKS_CIPHER_MODE
,
2333 ARG_LUKS_VOLUME_KEY_SIZE
,
2345 ARG_LUKS_PBKDF_TYPE
,
2346 ARG_LUKS_PBKDF_HASH_ALGORITHM
,
2347 ARG_LUKS_PBKDF_TIME_COST
,
2348 ARG_LUKS_PBKDF_MEMORY_COST
,
2349 ARG_LUKS_PBKDF_PARALLEL_THREADS
,
2350 ARG_RATE_LIMIT_INTERVAL
,
2351 ARG_RATE_LIMIT_BURST
,
2354 ARG_ENFORCE_PASSWORD_POLICY
,
2355 ARG_PASSWORD_CHANGE_NOW
,
2356 ARG_PASSWORD_CHANGE_MIN
,
2357 ARG_PASSWORD_CHANGE_MAX
,
2358 ARG_PASSWORD_CHANGE_WARN
,
2359 ARG_PASSWORD_CHANGE_INACTIVE
,
2362 ARG_PKCS11_TOKEN_URI
,
2365 ARG_AND_CHANGE_PASSWORD
,
2368 static const struct option options
[] = {
2369 { "help", no_argument
, NULL
, 'h' },
2370 { "version", no_argument
, NULL
, ARG_VERSION
},
2371 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
2372 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
2373 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
2374 { "host", required_argument
, NULL
, 'H' },
2375 { "machine", required_argument
, NULL
, 'M' },
2376 { "identity", required_argument
, NULL
, 'I' },
2377 { "real-name", required_argument
, NULL
, 'c' },
2378 { "comment", required_argument
, NULL
, 'c' }, /* Compat alias to keep thing in sync with useradd(8) */
2379 { "realm", required_argument
, NULL
, ARG_REALM
},
2380 { "email-address", required_argument
, NULL
, ARG_EMAIL_ADDRESS
},
2381 { "location", required_argument
, NULL
, ARG_LOCATION
},
2382 { "password-hint", required_argument
, NULL
, ARG_PASSWORD_HINT
},
2383 { "icon-name", required_argument
, NULL
, ARG_ICON_NAME
},
2384 { "home-dir", required_argument
, NULL
, 'd' }, /* Compatible with useradd(8) */
2385 { "uid", required_argument
, NULL
, 'u' }, /* Compatible with useradd(8) */
2386 { "member-of", required_argument
, NULL
, 'G' },
2387 { "groups", required_argument
, NULL
, 'G' }, /* Compat alias to keep thing in sync with useradd(8) */
2388 { "skel", required_argument
, NULL
, 'k' }, /* Compatible with useradd(8) */
2389 { "shell", required_argument
, NULL
, 's' }, /* Compatible with useradd(8) */
2390 { "setenv", required_argument
, NULL
, ARG_SETENV
},
2391 { "timezone", required_argument
, NULL
, ARG_TIMEZONE
},
2392 { "language", required_argument
, NULL
, ARG_LANGUAGE
},
2393 { "locked", required_argument
, NULL
, ARG_LOCKED
},
2394 { "not-before", required_argument
, NULL
, ARG_NOT_BEFORE
},
2395 { "not-after", required_argument
, NULL
, ARG_NOT_AFTER
},
2396 { "expiredate", required_argument
, NULL
, 'e' }, /* Compat alias to keep thing in sync with useradd(8) */
2397 { "ssh-authorized-keys", required_argument
, NULL
, ARG_SSH_AUTHORIZED_KEYS
},
2398 { "disk-size", required_argument
, NULL
, ARG_DISK_SIZE
},
2399 { "access-mode", required_argument
, NULL
, ARG_ACCESS_MODE
},
2400 { "umask", required_argument
, NULL
, ARG_UMASK
},
2401 { "nice", required_argument
, NULL
, ARG_NICE
},
2402 { "rlimit", required_argument
, NULL
, ARG_RLIMIT
},
2403 { "tasks-max", required_argument
, NULL
, ARG_TASKS_MAX
},
2404 { "memory-high", required_argument
, NULL
, ARG_MEMORY_HIGH
},
2405 { "memory-max", required_argument
, NULL
, ARG_MEMORY_MAX
},
2406 { "cpu-weight", required_argument
, NULL
, ARG_CPU_WEIGHT
},
2407 { "io-weight", required_argument
, NULL
, ARG_IO_WEIGHT
},
2408 { "storage", required_argument
, NULL
, ARG_STORAGE
},
2409 { "image-path", required_argument
, NULL
, ARG_IMAGE_PATH
},
2410 { "fs-type", required_argument
, NULL
, ARG_FS_TYPE
},
2411 { "luks-discard", required_argument
, NULL
, ARG_LUKS_DISCARD
},
2412 { "luks-offline-discard", required_argument
, NULL
, ARG_LUKS_OFFLINE_DISCARD
},
2413 { "luks-cipher", required_argument
, NULL
, ARG_LUKS_CIPHER
},
2414 { "luks-cipher-mode", required_argument
, NULL
, ARG_LUKS_CIPHER_MODE
},
2415 { "luks-volume-key-size", required_argument
, NULL
, ARG_LUKS_VOLUME_KEY_SIZE
},
2416 { "luks-pbkdf-type", required_argument
, NULL
, ARG_LUKS_PBKDF_TYPE
},
2417 { "luks-pbkdf-hash-algorithm", required_argument
, NULL
, ARG_LUKS_PBKDF_HASH_ALGORITHM
},
2418 { "luks-pbkdf-time-cost", required_argument
, NULL
, ARG_LUKS_PBKDF_TIME_COST
},
2419 { "luks-pbkdf-memory-cost", required_argument
, NULL
, ARG_LUKS_PBKDF_MEMORY_COST
},
2420 { "luks-pbkdf-parallel-threads", required_argument
, NULL
, ARG_LUKS_PBKDF_PARALLEL_THREADS
},
2421 { "nosuid", required_argument
, NULL
, ARG_NOSUID
},
2422 { "nodev", required_argument
, NULL
, ARG_NODEV
},
2423 { "noexec", required_argument
, NULL
, ARG_NOEXEC
},
2424 { "cifs-user-name", required_argument
, NULL
, ARG_CIFS_USER_NAME
},
2425 { "cifs-domain", required_argument
, NULL
, ARG_CIFS_DOMAIN
},
2426 { "cifs-service", required_argument
, NULL
, ARG_CIFS_SERVICE
},
2427 { "rate-limit-interval", required_argument
, NULL
, ARG_RATE_LIMIT_INTERVAL
},
2428 { "rate-limit-burst", required_argument
, NULL
, ARG_RATE_LIMIT_BURST
},
2429 { "stop-delay", required_argument
, NULL
, ARG_STOP_DELAY
},
2430 { "kill-processes", required_argument
, NULL
, ARG_KILL_PROCESSES
},
2431 { "enforce-password-policy", required_argument
, NULL
, ARG_ENFORCE_PASSWORD_POLICY
},
2432 { "password-change-now", required_argument
, NULL
, ARG_PASSWORD_CHANGE_NOW
},
2433 { "password-change-min", required_argument
, NULL
, ARG_PASSWORD_CHANGE_MIN
},
2434 { "password-change-max", required_argument
, NULL
, ARG_PASSWORD_CHANGE_MAX
},
2435 { "password-change-warn", required_argument
, NULL
, ARG_PASSWORD_CHANGE_WARN
},
2436 { "password-change-inactive", required_argument
, NULL
, ARG_PASSWORD_CHANGE_INACTIVE
},
2437 { "auto-login", required_argument
, NULL
, ARG_AUTO_LOGIN
},
2438 { "json", required_argument
, NULL
, ARG_JSON
},
2439 { "export-format", required_argument
, NULL
, ARG_EXPORT_FORMAT
},
2440 { "pkcs11-token-uri", required_argument
, NULL
, ARG_PKCS11_TOKEN_URI
},
2441 { "fido2-device", required_argument
, NULL
, ARG_FIDO2_DEVICE
},
2442 { "and-resize", required_argument
, NULL
, ARG_AND_RESIZE
},
2443 { "and-change-password", required_argument
, NULL
, ARG_AND_CHANGE_PASSWORD
},
2455 c
= getopt_long(argc
, argv
, "hH:M:I:c:d:u:k:s:e:G:jPE", options
, NULL
);
2462 return help(0, NULL
, NULL
);
2468 arg_pager_flags
|= PAGER_DISABLE
;
2475 case ARG_NO_ASK_PASSWORD
:
2476 arg_ask_password
= false;
2480 arg_transport
= BUS_TRANSPORT_REMOTE
;
2485 arg_transport
= BUS_TRANSPORT_MACHINE
;
2490 arg_identity
= optarg
;
2494 if (isempty(optarg
)) {
2495 r
= drop_from_identity("realName");
2502 if (!valid_gecos(optarg
))
2503 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Real name '%s' not a valid GECOS field.", optarg
);
2505 r
= json_variant_set_field_string(&arg_identity_extra
, "realName", optarg
);
2507 return log_error_errno(r
, "Failed to set realName field: %m");
2512 _cleanup_free_
char *hd
= NULL
;
2514 if (isempty(optarg
)) {
2515 r
= drop_from_identity("homeDirectory");
2522 r
= parse_path_argument_and_warn(optarg
, false, &hd
);
2526 if (!valid_home(hd
))
2527 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Home directory '%s' not valid.", hd
);
2529 r
= json_variant_set_field_string(&arg_identity_extra
, "homeDirectory", hd
);
2531 return log_error_errno(r
, "Failed to set homeDirectory field: %m");
2537 if (isempty(optarg
)) {
2538 r
= drop_from_identity("realm");
2545 r
= dns_name_is_valid(optarg
);
2547 return log_error_errno(r
, "Failed to determine whether realm '%s' is a valid DNS domain: %m", optarg
);
2549 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Realm '%s' is not a valid DNS domain: %m", optarg
);
2551 r
= json_variant_set_field_string(&arg_identity_extra
, "realm", optarg
);
2553 return log_error_errno(r
, "Failed to set realm field: %m");
2556 case ARG_EMAIL_ADDRESS
:
2559 case ARG_CIFS_USER_NAME
:
2560 case ARG_CIFS_DOMAIN
:
2561 case ARG_CIFS_SERVICE
: {
2564 c
== ARG_EMAIL_ADDRESS
? "emailAddress" :
2565 c
== ARG_LOCATION
? "location" :
2566 c
== ARG_ICON_NAME
? "iconName" :
2567 c
== ARG_CIFS_USER_NAME
? "cifsUserName" :
2568 c
== ARG_CIFS_DOMAIN
? "cifsDomain" :
2569 c
== ARG_CIFS_SERVICE
? "cifsService" :
2574 if (isempty(optarg
)) {
2575 r
= drop_from_identity(field
);
2582 r
= json_variant_set_field_string(&arg_identity_extra
, field
, optarg
);
2584 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2589 case ARG_PASSWORD_HINT
:
2590 if (isempty(optarg
)) {
2591 r
= drop_from_identity("passwordHint");
2598 r
= json_variant_set_field_string(&arg_identity_extra_privileged
, "passwordHint", optarg
);
2600 return log_error_errno(r
, "Failed to set passwordHint field: %m");
2602 string_erase(optarg
);
2608 if (isempty(optarg
)) {
2609 r
= drop_from_identity("niceLevel");
2615 r
= parse_nice(optarg
, &nc
);
2617 return log_error_errno(r
, "Failed to parse nice level: %s", optarg
);
2619 r
= json_variant_set_field_integer(&arg_identity_extra
, "niceLevel", nc
);
2621 return log_error_errno(r
, "Failed to set niceLevel field: %m");
2627 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
, *jcur
= NULL
, *jmax
= NULL
;
2628 _cleanup_free_
char *field
= NULL
, *t
= NULL
;
2633 if (isempty(optarg
)) {
2634 /* Remove all resource limits */
2636 r
= drop_from_identity("resourceLimits");
2640 arg_identity_filter_rlimits
= strv_free(arg_identity_filter_rlimits
);
2641 arg_identity_extra_rlimits
= json_variant_unref(arg_identity_extra_rlimits
);
2645 eq
= strchr(optarg
, '=');
2647 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Can't parse resource limit assignment: %s", optarg
);
2649 field
= strndup(optarg
, eq
- optarg
);
2653 l
= rlimit_from_string_harder(field
);
2655 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Unknown resource limit type: %s", field
);
2657 if (isempty(eq
+ 1)) {
2658 /* Remove only the specific rlimit */
2660 r
= strv_extend(&arg_identity_filter_rlimits
, rlimit_to_string(l
));
2664 r
= json_variant_filter(&arg_identity_extra_rlimits
, STRV_MAKE(field
));
2666 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
2671 r
= rlimit_parse(l
, eq
+ 1, &rl
);
2673 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to parse resource limit value: %s", eq
+ 1);
2675 r
= rl
.rlim_cur
== RLIM_INFINITY
? json_variant_new_null(&jcur
) : json_variant_new_unsigned(&jcur
, rl
.rlim_cur
);
2677 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to allocate current integer: %m");
2679 r
= rl
.rlim_max
== RLIM_INFINITY
? json_variant_new_null(&jmax
) : json_variant_new_unsigned(&jmax
, rl
.rlim_max
);
2681 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to allocate maximum integer: %m");
2685 JSON_BUILD_PAIR("cur", JSON_BUILD_VARIANT(jcur
)),
2686 JSON_BUILD_PAIR("max", JSON_BUILD_VARIANT(jmax
))));
2688 return log_error_errno(r
, "Failed to build resource limit: %m");
2690 t
= strjoin("RLIMIT_", rlimit_to_string(l
));
2694 r
= json_variant_set_field(&arg_identity_extra_rlimits
, t
, v
);
2696 return log_error_errno(r
, "Failed to set %s field: %m", rlimit_to_string(l
));
2704 if (isempty(optarg
)) {
2705 r
= drop_from_identity("uid");
2712 r
= parse_uid(optarg
, &uid
);
2714 return log_error_errno(r
, "Failed to parse UID '%s'.", optarg
);
2716 if (uid_is_system(uid
))
2717 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "UID " UID_FMT
" is in system range, refusing.", uid
);
2718 if (uid_is_dynamic(uid
))
2719 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "UID " UID_FMT
" is in dynamic range, refusing.", uid
);
2720 if (uid
== UID_NOBODY
)
2721 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "UID " UID_FMT
" is nobody UID, refusing.", uid
);
2723 r
= json_variant_set_field_unsigned(&arg_identity_extra
, "uid", uid
);
2725 return log_error_errno(r
, "Failed to set realm field: %m");
2731 case ARG_IMAGE_PATH
: {
2732 const char *field
= c
== 'k' ? "skeletonDirectory" : "imagePath";
2733 _cleanup_free_
char *v
= NULL
;
2735 if (isempty(optarg
)) {
2736 r
= drop_from_identity(field
);
2743 r
= parse_path_argument_and_warn(optarg
, false, &v
);
2747 r
= json_variant_set_field_string(&arg_identity_extra_this_machine
, field
, v
);
2749 return log_error_errno(r
, "Failed to set %s field: %m", v
);
2755 if (isempty(optarg
)) {
2756 r
= drop_from_identity("shell");
2763 if (!valid_shell(optarg
))
2764 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Shell '%s' not valid.", optarg
);
2766 r
= json_variant_set_field_string(&arg_identity_extra
, "shell", optarg
);
2768 return log_error_errno(r
, "Failed to set shell field: %m");
2773 _cleanup_free_
char **l
= NULL
, **k
= NULL
;
2774 _cleanup_(json_variant_unrefp
) JsonVariant
*ne
= NULL
;
2777 if (isempty(optarg
)) {
2778 r
= drop_from_identity("environment");
2785 if (!env_assignment_is_valid(optarg
))
2786 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Environment assignment '%s' not valid.", optarg
);
2788 e
= json_variant_by_key(arg_identity_extra
, "environment");
2790 r
= json_variant_strv(e
, &l
);
2792 return log_error_errno(r
, "Failed to parse JSON environment field: %m");
2795 k
= strv_env_set(l
, optarg
);
2801 r
= json_variant_new_array_strv(&ne
, k
);
2803 return log_error_errno(r
, "Failed to allocate environment list JSON: %m");
2805 r
= json_variant_set_field(&arg_identity_extra
, "environment", ne
);
2807 return log_error_errno(r
, "Failed to set environment list: %m");
2814 if (isempty(optarg
)) {
2815 r
= drop_from_identity("timeZone");
2822 if (!timezone_is_valid(optarg
, LOG_DEBUG
))
2823 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Timezone '%s' is not valid.", optarg
);
2825 r
= json_variant_set_field_string(&arg_identity_extra
, "timeZone", optarg
);
2827 return log_error_errno(r
, "Failed to set timezone field: %m");
2832 if (isempty(optarg
)) {
2833 r
= drop_from_identity("language");
2840 if (!locale_is_valid(optarg
))
2841 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Locale '%s' is not valid.", optarg
);
2843 if (locale_is_installed(optarg
) <= 0)
2844 log_warning("Locale '%s' is not installed, accepting anyway.", optarg
);
2846 r
= json_variant_set_field_string(&arg_identity_extra
, "preferredLanguage", optarg
);
2848 return log_error_errno(r
, "Failed to set preferredLanguage field: %m");
2856 case ARG_KILL_PROCESSES
:
2857 case ARG_ENFORCE_PASSWORD_POLICY
:
2858 case ARG_AUTO_LOGIN
:
2859 case ARG_PASSWORD_CHANGE_NOW
: {
2861 c
== ARG_LOCKED
? "locked" :
2862 c
== ARG_NOSUID
? "mountNoSuid" :
2863 c
== ARG_NODEV
? "mountNoDevices" :
2864 c
== ARG_NOEXEC
? "mountNoExecute" :
2865 c
== ARG_KILL_PROCESSES
? "killProcesses" :
2866 c
== ARG_ENFORCE_PASSWORD_POLICY
? "enforcePasswordPolicy" :
2867 c
== ARG_AUTO_LOGIN
? "autoLogin" :
2868 c
== ARG_PASSWORD_CHANGE_NOW
? "passwordChangeNow" :
2873 if (isempty(optarg
)) {
2874 r
= drop_from_identity(field
);
2881 r
= parse_boolean(optarg
);
2883 return log_error_errno(r
, "Failed to parse %s boolean: %m", field
);
2885 r
= json_variant_set_field_boolean(&arg_identity_extra
, field
, r
> 0);
2887 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2893 r
= json_variant_set_field_boolean(&arg_identity_extra
, "enforcePasswordPolicy", false);
2895 return log_error_errno(r
, "Failed to set enforcePasswordPolicy field: %m");
2900 if (isempty(optarg
)) {
2901 r
= drop_from_identity("diskSize");
2905 r
= drop_from_identity("diskSizeRelative");
2909 arg_disk_size
= arg_disk_size_relative
= UINT64_MAX
;
2913 r
= parse_permille(optarg
);
2915 r
= parse_size(optarg
, 1024, &arg_disk_size
);
2917 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Disk size '%s' not valid.", optarg
);
2919 r
= drop_from_identity("diskSizeRelative");
2923 r
= json_variant_set_field_unsigned(&arg_identity_extra_this_machine
, "diskSize", arg_disk_size
);
2925 return log_error_errno(r
, "Failed to set diskSize field: %m");
2927 arg_disk_size_relative
= UINT64_MAX
;
2929 /* Normalize to UINT32_MAX == 100% */
2930 arg_disk_size_relative
= (uint64_t) r
* UINT32_MAX
/ 1000U;
2932 r
= drop_from_identity("diskSize");
2936 r
= json_variant_set_field_unsigned(&arg_identity_extra_this_machine
, "diskSizeRelative", arg_disk_size_relative
);
2938 return log_error_errno(r
, "Failed to set diskSizeRelative field: %m");
2940 arg_disk_size
= UINT64_MAX
;
2945 case ARG_ACCESS_MODE
: {
2948 if (isempty(optarg
)) {
2949 r
= drop_from_identity("accessMode");
2956 r
= parse_mode(optarg
, &mode
);
2958 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Access mode '%s' not valid.", optarg
);
2960 r
= json_variant_set_field_unsigned(&arg_identity_extra
, "accessMode", mode
);
2962 return log_error_errno(r
, "Failed to set access mode field: %m");
2967 case ARG_LUKS_DISCARD
:
2968 if (isempty(optarg
)) {
2969 r
= drop_from_identity("luksDiscard");
2976 r
= parse_boolean(optarg
);
2978 return log_error_errno(r
, "Failed to parse --luks-discard= parameter: %s", optarg
);
2980 r
= json_variant_set_field_boolean(&arg_identity_extra
, "luksDiscard", r
);
2982 return log_error_errno(r
, "Failed to set discard field: %m");
2986 case ARG_LUKS_OFFLINE_DISCARD
:
2987 if (isempty(optarg
)) {
2988 r
= drop_from_identity("luksOfflineDiscard");
2995 r
= parse_boolean(optarg
);
2997 return log_error_errno(r
, "Failed to parse --luks-offline-discard= parameter: %s", optarg
);
2999 r
= json_variant_set_field_boolean(&arg_identity_extra
, "luksOfflineDiscard", r
);
3001 return log_error_errno(r
, "Failed to set offline discard field: %m");
3005 case ARG_LUKS_VOLUME_KEY_SIZE
:
3006 case ARG_LUKS_PBKDF_PARALLEL_THREADS
:
3007 case ARG_RATE_LIMIT_BURST
: {
3009 c
== ARG_LUKS_VOLUME_KEY_SIZE
? "luksVolumeKeySize" :
3010 c
== ARG_LUKS_PBKDF_PARALLEL_THREADS
? "luksPbkdfParallelThreads" :
3011 c
== ARG_RATE_LIMIT_BURST
? "rateLimitBurst" : NULL
;
3016 if (isempty(optarg
)) {
3017 r
= drop_from_identity(field
);
3022 r
= safe_atou(optarg
, &n
);
3024 return log_error_errno(r
, "Failed to parse %s parameter: %s", field
, optarg
);
3026 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, n
);
3028 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3036 if (isempty(optarg
)) {
3037 r
= drop_from_identity("umask");
3044 r
= parse_mode(optarg
, &m
);
3046 return log_error_errno(r
, "Failed to parse umask: %m");
3048 r
= json_variant_set_field_integer(&arg_identity_extra
, "umask", m
);
3050 return log_error_errno(r
, "Failed to set umask field: %m");
3055 case ARG_SSH_AUTHORIZED_KEYS
: {
3056 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
3057 _cleanup_(strv_freep
) char **l
= NULL
, **add
= NULL
;
3059 if (isempty(optarg
)) {
3060 r
= drop_from_identity("sshAuthorizedKeys");
3067 if (optarg
[0] == '@') {
3068 _cleanup_fclose_
FILE *f
= NULL
;
3070 /* If prefixed with '@' read from a file */
3072 f
= fopen(optarg
+1, "re");
3074 return log_error_errno(errno
, "Failed to open '%s': %m", optarg
+1);
3077 _cleanup_free_
char *line
= NULL
;
3079 r
= read_line(f
, LONG_LINE_MAX
, &line
);
3081 return log_error_errno(r
, "Failed to read from '%s': %m", optarg
+1);
3091 r
= strv_consume(&add
, TAKE_PTR(line
));
3096 /* Otherwise, assume it's a literal key. Let's do some superficial checks
3097 * before accept it though. */
3099 if (string_has_cc(optarg
, NULL
))
3100 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Authorized key contains control characters, refusing.");
3101 if (optarg
[0] == '#')
3102 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Specified key is a comment?");
3104 add
= strv_new(optarg
);
3109 v
= json_variant_ref(json_variant_by_key(arg_identity_extra_privileged
, "sshAuthorizedKeys"));
3111 r
= json_variant_strv(v
, &l
);
3113 return log_error_errno(r
, "Failed to parse SSH authorized keys list: %m");
3116 r
= strv_extend_strv(&l
, add
, true);
3120 v
= json_variant_unref(v
);
3122 r
= json_variant_new_array_strv(&v
, l
);
3126 r
= json_variant_set_field(&arg_identity_extra_privileged
, "sshAuthorizedKeys", v
);
3128 return log_error_errno(r
, "Failed to set authorized keys: %m");
3133 case ARG_NOT_BEFORE
:
3139 field
= c
== ARG_NOT_BEFORE
? "notBeforeUSec" :
3140 IN_SET(c
, ARG_NOT_AFTER
, 'e') ? "notAfterUSec" : NULL
;
3144 if (isempty(optarg
)) {
3145 r
= drop_from_identity(field
);
3152 /* Note the minor discrepancy regarding -e parsing here: we support that for compat
3153 * reasons, and in the original useradd(8) implementation it accepts dates in the
3154 * format YYYY-MM-DD. Coincidentally, we accept dates formatted like that too, but
3155 * with greater precision. */
3156 r
= parse_timestamp(optarg
, &n
);
3158 return log_error_errno(r
, "Failed to parse %s parameter: %m", field
);
3160 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, n
);
3162 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3166 case ARG_PASSWORD_CHANGE_MIN
:
3167 case ARG_PASSWORD_CHANGE_MAX
:
3168 case ARG_PASSWORD_CHANGE_WARN
:
3169 case ARG_PASSWORD_CHANGE_INACTIVE
: {
3173 field
= c
== ARG_PASSWORD_CHANGE_MIN
? "passwordChangeMinUSec" :
3174 c
== ARG_PASSWORD_CHANGE_MAX
? "passwordChangeMaxUSec" :
3175 c
== ARG_PASSWORD_CHANGE_WARN
? "passwordChangeWarnUSec" :
3176 c
== ARG_PASSWORD_CHANGE_INACTIVE
? "passwordChangeInactiveUSec" :
3181 if (isempty(optarg
)) {
3182 r
= drop_from_identity(field
);
3189 r
= parse_sec(optarg
, &n
);
3191 return log_error_errno(r
, "Failed to parse %s parameter: %m", field
);
3193 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, n
);
3195 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3201 case ARG_LUKS_CIPHER
:
3202 case ARG_LUKS_CIPHER_MODE
:
3203 case ARG_LUKS_PBKDF_TYPE
:
3204 case ARG_LUKS_PBKDF_HASH_ALGORITHM
: {
3207 c
== ARG_STORAGE
? "storage" :
3208 c
== ARG_FS_TYPE
? "fileSystemType" :
3209 c
== ARG_LUKS_CIPHER
? "luksCipher" :
3210 c
== ARG_LUKS_CIPHER_MODE
? "luksCipherMode" :
3211 c
== ARG_LUKS_PBKDF_TYPE
? "luksPbkdfType" :
3212 c
== ARG_LUKS_PBKDF_HASH_ALGORITHM
? "luksPbkdfHashAlgorithm" : NULL
;
3216 if (isempty(optarg
)) {
3217 r
= drop_from_identity(field
);
3224 if (!string_is_safe(optarg
))
3225 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Parameter for %s field not valid: %s", field
, optarg
);
3227 r
= json_variant_set_field_string(
3228 IN_SET(c
, ARG_STORAGE
, ARG_FS_TYPE
) ?
3229 &arg_identity_extra_this_machine
:
3230 &arg_identity_extra
, field
, optarg
);
3232 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3237 case ARG_LUKS_PBKDF_TIME_COST
:
3238 case ARG_RATE_LIMIT_INTERVAL
:
3239 case ARG_STOP_DELAY
: {
3241 c
== ARG_LUKS_PBKDF_TIME_COST
? "luksPbkdfTimeCostUSec" :
3242 c
== ARG_RATE_LIMIT_INTERVAL
? "rateLimitIntervalUSec" :
3243 c
== ARG_STOP_DELAY
? "stopDelayUSec" :
3249 if (isempty(optarg
)) {
3250 r
= drop_from_identity(field
);
3257 r
= parse_sec(optarg
, &t
);
3259 return log_error_errno(r
, "Failed to parse %s field: %s", field
, optarg
);
3261 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, t
);
3263 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3269 const char *p
= optarg
;
3272 r
= drop_from_identity("memberOf");
3280 _cleanup_(json_variant_unrefp
) JsonVariant
*mo
= NULL
;
3281 _cleanup_strv_free_
char **list
= NULL
;
3282 _cleanup_free_
char *word
= NULL
;
3284 r
= extract_first_word(&p
, &word
, ",", 0);
3286 return log_error_errno(r
, "Failed to parse group list: %m");
3290 if (!valid_user_group_name(word
, 0))
3291 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Invalid group name %s.", word
);
3293 mo
= json_variant_ref(json_variant_by_key(arg_identity_extra
, "memberOf"));
3295 r
= json_variant_strv(mo
, &list
);
3297 return log_error_errno(r
, "Failed to parse group list: %m");
3299 r
= strv_extend(&list
, word
);
3306 mo
= json_variant_unref(mo
);
3307 r
= json_variant_new_array_strv(&mo
, list
);
3309 return log_error_errno(r
, "Failed to create group list JSON: %m");
3311 r
= json_variant_set_field(&arg_identity_extra
, "memberOf", mo
);
3313 return log_error_errno(r
, "Failed to update group list: %m");
3319 case ARG_TASKS_MAX
: {
3322 if (isempty(optarg
)) {
3323 r
= drop_from_identity("tasksMax");
3329 r
= safe_atou64(optarg
, &u
);
3331 return log_error_errno(r
, "Failed to parse --tasks-max= parameter: %s", optarg
);
3333 r
= json_variant_set_field_unsigned(&arg_identity_extra
, "tasksMax", u
);
3335 return log_error_errno(r
, "Failed to set tasksMax field: %m");
3340 case ARG_MEMORY_MAX
:
3341 case ARG_MEMORY_HIGH
:
3342 case ARG_LUKS_PBKDF_MEMORY_COST
: {
3344 c
== ARG_MEMORY_MAX
? "memoryMax" :
3345 c
== ARG_MEMORY_HIGH
? "memoryHigh" :
3346 c
== ARG_LUKS_PBKDF_MEMORY_COST
? "luksPbkdfMemoryCost" : NULL
;
3352 if (isempty(optarg
)) {
3353 r
= drop_from_identity(field
);
3359 r
= parse_size(optarg
, 1024, &u
);
3361 return log_error_errno(r
, "Failed to parse %s parameter: %s", field
, optarg
);
3363 r
= json_variant_set_field_unsigned(&arg_identity_extra_this_machine
, field
, u
);
3365 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3370 case ARG_CPU_WEIGHT
:
3371 case ARG_IO_WEIGHT
: {
3372 const char *field
= c
== ARG_CPU_WEIGHT
? "cpuWeight" :
3373 c
== ARG_IO_WEIGHT
? "ioWeight" : NULL
;
3378 if (isempty(optarg
)) {
3379 r
= drop_from_identity(field
);
3385 r
= safe_atou64(optarg
, &u
);
3387 return log_error_errno(r
, "Failed to parse --cpu-weight=/--io-weight= parameter: %s", optarg
);
3389 if (!CGROUP_WEIGHT_IS_OK(u
))
3390 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Weight %" PRIu64
" is out of valid weight range.", u
);
3392 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, u
);
3394 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3399 case ARG_PKCS11_TOKEN_URI
: {
3402 /* If --pkcs11-token-uri= is specified we always drop everything old */
3403 FOREACH_STRING(p
, "pkcs11TokenUri", "pkcs11EncryptedKey") {
3404 r
= drop_from_identity(p
);
3409 if (isempty(optarg
)) {
3410 arg_pkcs11_token_uri
= strv_free(arg_pkcs11_token_uri
);
3414 if (!pkcs11_uri_valid(optarg
))
3415 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Not a valid PKCS#11 URI: %s", optarg
);
3417 r
= strv_extend(&arg_pkcs11_token_uri
, optarg
);
3421 strv_uniq(arg_pkcs11_token_uri
);
3425 case ARG_FIDO2_DEVICE
: {
3428 if (streq(optarg
, "list"))
3429 return list_fido2_devices();
3431 FOREACH_STRING(p
, "fido2HmacCredential", "fido2HmacSalt") {
3432 r
= drop_from_identity(p
);
3437 if (isempty(optarg
)) {
3438 arg_fido2_device
= strv_free(arg_fido2_device
);
3442 if (streq(optarg
, "auto")) {
3443 _cleanup_free_
char *found
= NULL
;
3445 r
= find_fido2_auto(&found
);
3449 r
= strv_consume(&arg_fido2_device
, TAKE_PTR(found
));
3451 r
= strv_extend(&arg_fido2_device
, optarg
);
3456 strv_uniq(arg_fido2_device
);
3462 arg_json_format_flags
= JSON_FORMAT_PRETTY_AUTO
|JSON_FORMAT_COLOR_AUTO
;
3466 if (streq(optarg
, "pretty")) {
3468 arg_json_format_flags
= JSON_FORMAT_PRETTY
|JSON_FORMAT_COLOR_AUTO
;
3469 } else if (streq(optarg
, "short")) {
3471 arg_json_format_flags
= JSON_FORMAT_NEWLINE
;
3472 } else if (streq(optarg
, "off")) {
3474 arg_json_format_flags
= 0;
3475 } else if (streq(optarg
, "help")) {
3481 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Unknown argument to --json=: %s", optarg
);
3486 if (arg_export_format
== EXPORT_FORMAT_FULL
)
3487 arg_export_format
= EXPORT_FORMAT_STRIPPED
;
3488 else if (arg_export_format
== EXPORT_FORMAT_STRIPPED
)
3489 arg_export_format
= EXPORT_FORMAT_MINIMAL
;
3491 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Specifying -E more than twice is not supported.");
3494 if (arg_json_format_flags
== 0)
3495 arg_json_format_flags
= JSON_FORMAT_PRETTY_AUTO
|JSON_FORMAT_COLOR_AUTO
;
3498 case ARG_EXPORT_FORMAT
:
3499 if (streq(optarg
, "full"))
3500 arg_export_format
= EXPORT_FORMAT_FULL
;
3501 else if (streq(optarg
, "stripped"))
3502 arg_export_format
= EXPORT_FORMAT_STRIPPED
;
3503 else if (streq(optarg
, "minimal"))
3504 arg_export_format
= EXPORT_FORMAT_MINIMAL
;
3505 else if (streq(optarg
, "help")) {
3514 case ARG_AND_RESIZE
:
3515 arg_and_resize
= true;
3518 case ARG_AND_CHANGE_PASSWORD
:
3519 arg_and_change_password
= true;
3526 assert_not_reached("Unhandled option");
3530 if (!strv_isempty(arg_pkcs11_token_uri
) || !strv_isempty(arg_fido2_device
))
3531 arg_and_change_password
= true;
3533 if (arg_disk_size
!= UINT64_MAX
|| arg_disk_size_relative
!= UINT64_MAX
)
3534 arg_and_resize
= true;
3539 static int run(int argc
, char *argv
[]) {
3540 static const Verb verbs
[] = {
3541 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
3542 { "list", VERB_ANY
, 1, VERB_DEFAULT
, list_homes
},
3543 { "activate", 2, VERB_ANY
, 0, activate_home
},
3544 { "deactivate", 2, VERB_ANY
, 0, deactivate_home
},
3545 { "inspect", VERB_ANY
, VERB_ANY
, 0, inspect_home
},
3546 { "authenticate", VERB_ANY
, VERB_ANY
, 0, authenticate_home
},
3547 { "create", VERB_ANY
, 2, 0, create_home
},
3548 { "remove", 2, VERB_ANY
, 0, remove_home
},
3549 { "update", VERB_ANY
, 2, 0, update_home
},
3550 { "passwd", VERB_ANY
, 2, 0, passwd_home
},
3551 { "resize", 2, 3, 0, resize_home
},
3552 { "lock", 2, VERB_ANY
, 0, lock_home
},
3553 { "unlock", 2, VERB_ANY
, 0, unlock_home
},
3554 { "with", 2, VERB_ANY
, 0, with_home
},
3555 { "lock-all", VERB_ANY
, 1, 0, lock_all_homes
},
3563 r
= parse_argv(argc
, argv
);
3567 return dispatch_verb(argc
, argv
, verbs
, NULL
);
3570 DEFINE_MAIN_FUNCTION(run
);