1 /* SPDX-License-Identifier: LGPL-2.1+ */
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 "locale-util.h"
22 #include "main-func.h"
23 #include "memory-util.h"
25 #include "parse-util.h"
26 #include "path-util.h"
27 #include "pkcs11-util.h"
28 #include "pretty-print.h"
29 #include "process-util.h"
30 #include "pwquality-util.h"
31 #include "rlimit-util.h"
32 #include "spawn-polkit-agent.h"
33 #include "terminal-util.h"
34 #include "user-record-pwquality.h"
35 #include "user-record-show.h"
36 #include "user-record-util.h"
37 #include "user-record.h"
38 #include "user-util.h"
41 static PagerFlags arg_pager_flags
= 0;
42 static bool arg_legend
= true;
43 static bool arg_ask_password
= true;
44 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
45 static const char *arg_host
= NULL
;
46 static const char *arg_identity
= NULL
;
47 static JsonVariant
*arg_identity_extra
= NULL
;
48 static JsonVariant
*arg_identity_extra_privileged
= NULL
;
49 static JsonVariant
*arg_identity_extra_this_machine
= NULL
;
50 static JsonVariant
*arg_identity_extra_rlimits
= NULL
;
51 static char **arg_identity_filter
= NULL
; /* this one is also applied to 'privileged' and 'thisMachine' subobjects */
52 static char **arg_identity_filter_rlimits
= NULL
;
53 static uint64_t arg_disk_size
= UINT64_MAX
;
54 static uint64_t arg_disk_size_relative
= UINT64_MAX
;
55 static char **arg_pkcs11_token_uri
= NULL
;
56 static char **arg_fido2_device
= NULL
;
57 static bool arg_recovery_key
= false;
58 static bool arg_json
= false;
59 static JsonFormatFlags arg_json_format_flags
= 0;
60 static bool arg_and_resize
= false;
61 static bool arg_and_change_password
= false;
63 EXPORT_FORMAT_FULL
, /* export the full record */
64 EXPORT_FORMAT_STRIPPED
, /* strip "state" + "binding", but leave signature in place */
65 EXPORT_FORMAT_MINIMAL
, /* also strip signature */
66 } arg_export_format
= EXPORT_FORMAT_FULL
;
68 STATIC_DESTRUCTOR_REGISTER(arg_identity_extra
, json_variant_unrefp
);
69 STATIC_DESTRUCTOR_REGISTER(arg_identity_extra_this_machine
, json_variant_unrefp
);
70 STATIC_DESTRUCTOR_REGISTER(arg_identity_extra_privileged
, json_variant_unrefp
);
71 STATIC_DESTRUCTOR_REGISTER(arg_identity_extra_rlimits
, json_variant_unrefp
);
72 STATIC_DESTRUCTOR_REGISTER(arg_identity_filter
, strv_freep
);
73 STATIC_DESTRUCTOR_REGISTER(arg_identity_filter_rlimits
, strv_freep
);
74 STATIC_DESTRUCTOR_REGISTER(arg_pkcs11_token_uri
, strv_freep
);
75 STATIC_DESTRUCTOR_REGISTER(arg_fido2_device
, strv_freep
);
77 static const BusLocator
*bus_mgr
;
79 static bool identity_properties_specified(void) {
82 !json_variant_is_blank_object(arg_identity_extra
) ||
83 !json_variant_is_blank_object(arg_identity_extra_privileged
) ||
84 !json_variant_is_blank_object(arg_identity_extra_this_machine
) ||
85 !json_variant_is_blank_object(arg_identity_extra_rlimits
) ||
86 !strv_isempty(arg_identity_filter
) ||
87 !strv_isempty(arg_identity_filter_rlimits
) ||
88 !strv_isempty(arg_pkcs11_token_uri
) ||
89 !strv_isempty(arg_fido2_device
);
92 static int acquire_bus(sd_bus
**bus
) {
100 r
= bus_connect_transport(arg_transport
, arg_host
, false, bus
);
102 return log_error_errno(r
, "Failed to connect to bus: %m");
104 (void) sd_bus_set_allow_interactive_authorization(*bus
, arg_ask_password
);
109 static int list_homes(int argc
, char *argv
[], void *userdata
) {
110 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
111 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
112 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
113 _cleanup_(table_unrefp
) Table
*table
= NULL
;
116 (void) pager_open(arg_pager_flags
);
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 || arg_json
) {
174 r
= table_set_sort(table
, (size_t) 0, (size_t) -1);
176 return table_log_sort_error(r
);
178 table_set_header(table
, arg_legend
);
181 r
= table_print_json(table
, stdout
, arg_json_format_flags
);
183 r
= table_print(table
, NULL
);
185 return table_log_print_error(r
);
188 if (arg_legend
&& !arg_json
) {
189 if (table_get_rows(table
) > 1)
190 printf("\n%zu home areas listed.\n", table_get_rows(table
) - 1);
192 printf("No home areas.\n");
198 static int acquire_existing_password(const char *user_name
, UserRecord
*hr
, bool emphasize_current
) {
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");
219 if (unsetenv("PASSWORD") < 0)
220 return log_error_errno(errno
, "Failed to unset $PASSWORD: %m");
225 if (asprintf(&question
, emphasize_current
?
226 "Please enter current password for user %s:" :
227 "Please enter password for user %s:",
231 r
= ask_password_auto(question
, "user-home", NULL
, "home-password", USEC_INFINITY
, ASK_PASSWORD_ACCEPT_CACHED
|ASK_PASSWORD_PUSH_CACHE
, &password
);
233 return log_error_errno(r
, "Failed to acquire password: %m");
235 r
= user_record_set_password(hr
, password
, true);
237 return log_error_errno(r
, "Failed to store password: %m");
242 static int acquire_token_pin(const char *user_name
, UserRecord
*hr
) {
243 _cleanup_(strv_free_erasep
) char **pin
= NULL
;
244 _cleanup_free_
char *question
= NULL
;
253 r
= user_record_set_token_pin(hr
, STRV_MAKE(e
), false);
255 return log_error_errno(r
, "Failed to store token PIN: %m");
259 if (unsetenv("PIN") < 0)
260 return log_error_errno(errno
, "Failed to unset $PIN: %m");
265 if (asprintf(&question
, "Please enter security token PIN for user %s:", user_name
) < 0)
268 /* We never cache or use cached PINs, since usually there are only very few attempts allowed before the PIN is blocked */
269 r
= ask_password_auto(question
, "user-home", NULL
, "token-pin", USEC_INFINITY
, 0, &pin
);
271 return log_error_errno(r
, "Failed to acquire security token PIN: %m");
273 r
= user_record_set_token_pin(hr
, pin
, false);
275 return log_error_errno(r
, "Failed to store security token PIN: %m");
280 static int handle_generic_user_record_error(
281 const char *user_name
,
283 const sd_bus_error
*error
,
285 bool emphasize_current_password
) {
291 if (sd_bus_error_has_name(error
, BUS_ERROR_HOME_ABSENT
))
292 return log_error_errno(SYNTHETIC_ERRNO(EREMOTE
),
293 "Home of user %s is currently absent, please plug in the necessary storage device or backing file system.", user_name
);
295 else if (sd_bus_error_has_name(error
, BUS_ERROR_AUTHENTICATION_LIMIT_HIT
))
296 return log_error_errno(SYNTHETIC_ERRNO(ETOOMANYREFS
),
297 "Too frequent unsuccessful login attempts for user %s, try again later.", user_name
);
299 else if (sd_bus_error_has_name(error
, BUS_ERROR_BAD_PASSWORD
)) {
301 if (!strv_isempty(hr
->password
))
302 log_notice("Password incorrect or not sufficient, please try again.");
304 r
= acquire_existing_password(user_name
, hr
, emphasize_current_password
);
308 } else if (sd_bus_error_has_name(error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
)) {
310 if (strv_isempty(hr
->password
))
311 log_notice("Security token not inserted, please enter password.");
313 log_notice("Password incorrect or not sufficient, and configured security token not inserted, please try again.");
315 r
= acquire_existing_password(user_name
, hr
, emphasize_current_password
);
319 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_PIN_NEEDED
)) {
321 r
= acquire_token_pin(user_name
, hr
);
325 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_PROTECTED_AUTHENTICATION_PATH_NEEDED
)) {
327 log_notice("%s%sPlease authenticate physically on security token.",
328 emoji_enabled() ? special_glyph(SPECIAL_GLYPH_TOUCH
) : "",
329 emoji_enabled() ? " " : "");
331 r
= user_record_set_pkcs11_protected_authentication_path_permitted(hr
, true);
333 return log_error_errno(r
, "Failed to set PKCS#11 protected authentication path permitted flag: %m");
335 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_USER_PRESENCE_NEEDED
)) {
337 log_notice("%s%sAuthentication requires presence verification on security token.",
338 emoji_enabled() ? special_glyph(SPECIAL_GLYPH_TOUCH
) : "",
339 emoji_enabled() ? " " : "");
341 r
= user_record_set_fido2_user_presence_permitted(hr
, true);
343 return log_error_errno(r
, "Failed to set FIDO2 user presence permitted flag: %m");
345 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_PIN_LOCKED
))
346 return log_error_errno(SYNTHETIC_ERRNO(EPERM
), "Security token PIN is locked, please unlock it first. (Hint: Removal and re-insertion might suffice.)");
348 else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_BAD_PIN
)) {
350 log_notice("Security token PIN incorrect, please try again.");
352 r
= acquire_token_pin(user_name
, hr
);
356 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_BAD_PIN_FEW_TRIES_LEFT
)) {
358 log_notice("Security token PIN incorrect, please try again (only a few tries left!).");
360 r
= acquire_token_pin(user_name
, hr
);
364 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_BAD_PIN_ONE_TRY_LEFT
)) {
366 log_notice("Security token PIN incorrect, please try again (only one try left!).");
368 r
= acquire_token_pin(user_name
, hr
);
372 return log_error_errno(ret
, "Operation on home %s failed: %s", user_name
, bus_error_message(error
, ret
));
377 static int activate_home(int argc
, char *argv
[], void *userdata
) {
378 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
382 r
= acquire_bus(&bus
);
386 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
387 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
389 secret
= user_record_new();
394 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
395 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
397 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ActivateHome");
399 return bus_log_create_error(r
);
401 r
= sd_bus_message_append(m
, "s", *i
);
403 return bus_log_create_error(r
);
405 r
= bus_message_append_secret(m
, secret
);
407 return bus_log_create_error(r
);
409 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
411 r
= handle_generic_user_record_error(*i
, secret
, &error
, r
, false);
426 static int deactivate_home(int argc
, char *argv
[], void *userdata
) {
427 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
431 r
= acquire_bus(&bus
);
435 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
436 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
437 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
439 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "DeactivateHome");
441 return bus_log_create_error(r
);
443 r
= sd_bus_message_append(m
, "s", *i
);
445 return bus_log_create_error(r
);
447 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
449 log_error_errno(r
, "Failed to deactivate user home: %s", bus_error_message(&error
, r
));
458 static void dump_home_record(UserRecord
*hr
) {
463 if (hr
->incomplete
) {
465 log_warning("Warning: lacking rights to acquire privileged fields of user record of '%s', output incomplete.", hr
->user_name
);
469 _cleanup_(user_record_unrefp
) UserRecord
*stripped
= NULL
;
471 if (arg_export_format
== EXPORT_FORMAT_STRIPPED
)
472 r
= user_record_clone(hr
, USER_RECORD_EXTRACT_EMBEDDED
, &stripped
);
473 else if (arg_export_format
== EXPORT_FORMAT_MINIMAL
)
474 r
= user_record_clone(hr
, USER_RECORD_EXTRACT_SIGNABLE
, &stripped
);
478 log_warning_errno(r
, "Failed to strip user record, ignoring: %m");
482 json_variant_dump(hr
->json
, arg_json_format_flags
, stdout
, NULL
);
484 user_record_show(hr
, true);
487 static char **mangle_user_list(char **list
, char ***ret_allocated
) {
488 _cleanup_free_
char *myself
= NULL
;
491 if (!strv_isempty(list
)) {
492 *ret_allocated
= NULL
;
496 myself
= getusername_malloc();
504 l
[0] = TAKE_PTR(myself
);
511 static int inspect_home(int argc
, char *argv
[], void *userdata
) {
512 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
513 _cleanup_(strv_freep
) char **mangled_list
= NULL
;
517 (void) pager_open(arg_pager_flags
);
519 r
= acquire_bus(&bus
);
523 items
= mangle_user_list(strv_skip(argv
, 1), &mangled_list
);
527 STRV_FOREACH(i
, items
) {
528 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
529 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
530 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
531 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
536 r
= parse_uid(*i
, &uid
);
538 if (!valid_user_group_name(*i
, 0)) {
539 log_error("Invalid user name '%s'.", *i
);
546 r
= bus_call_method(bus
, bus_mgr
, "GetUserRecordByName", &error
, &reply
, "s", *i
);
548 r
= bus_call_method(bus
, bus_mgr
, "GetUserRecordByUID", &error
, &reply
, "u", (uint32_t) uid
);
551 log_error_errno(r
, "Failed to inspect home: %s", bus_error_message(&error
, r
));
558 r
= sd_bus_message_read(reply
, "sbo", &json
, &incomplete
, NULL
);
560 bus_log_parse_error(r
);
567 r
= json_parse(json
, JSON_PARSE_SENSITIVE
, &v
, NULL
, NULL
);
569 log_error_errno(r
, "Failed to parse JSON identity: %m");
576 hr
= user_record_new();
580 r
= user_record_load(hr
, v
, USER_RECORD_LOAD_REFUSE_SECRET
|USER_RECORD_LOG
);
588 hr
->incomplete
= incomplete
;
589 dump_home_record(hr
);
595 static int authenticate_home(int argc
, char *argv
[], void *userdata
) {
596 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
597 _cleanup_(strv_freep
) char **mangled_list
= NULL
;
601 items
= mangle_user_list(strv_skip(argv
, 1), &mangled_list
);
605 r
= acquire_bus(&bus
);
609 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
611 STRV_FOREACH(i
, items
) {
612 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
614 secret
= user_record_new();
619 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
620 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
622 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "AuthenticateHome");
624 return bus_log_create_error(r
);
626 r
= sd_bus_message_append(m
, "s", *i
);
628 return bus_log_create_error(r
);
630 r
= bus_message_append_secret(m
, secret
);
632 return bus_log_create_error(r
);
634 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
636 r
= handle_generic_user_record_error(*i
, secret
, &error
, r
, false);
651 static int update_last_change(JsonVariant
**v
, bool with_password
, bool override
) {
658 n
= now(CLOCK_REALTIME
);
660 c
= json_variant_by_key(*v
, "lastChangeUSec");
665 goto update_password
;
667 if (!json_variant_is_unsigned(c
))
668 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastChangeUSec field is not an unsigned integer, refusing.");
670 u
= json_variant_unsigned(c
);
672 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastChangeUSec is from the future, can't update.");
675 r
= json_variant_set_field_unsigned(v
, "lastChangeUSec", n
);
677 return log_error_errno(r
, "Failed to update lastChangeUSec: %m");
683 c
= json_variant_by_key(*v
, "lastPasswordChangeUSec");
690 if (!json_variant_is_unsigned(c
))
691 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastPasswordChangeUSec field is not an unsigned integer, refusing.");
693 u
= json_variant_unsigned(c
);
695 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastPasswordChangeUSec is from the future, can't update.");
698 r
= json_variant_set_field_unsigned(v
, "lastPasswordChangeUSec", n
);
700 return log_error_errno(r
, "Failed to update lastPasswordChangeUSec: %m");
705 static int apply_identity_changes(JsonVariant
**_v
) {
706 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
711 v
= json_variant_ref(*_v
);
713 r
= json_variant_filter(&v
, arg_identity_filter
);
715 return log_error_errno(r
, "Failed to filter identity: %m");
717 r
= json_variant_merge(&v
, arg_identity_extra
);
719 return log_error_errno(r
, "Failed to merge identities: %m");
721 if (arg_identity_extra_this_machine
|| !strv_isempty(arg_identity_filter
)) {
722 _cleanup_(json_variant_unrefp
) JsonVariant
*per_machine
= NULL
, *mmid
= NULL
;
723 char mids
[SD_ID128_STRING_MAX
];
726 r
= sd_id128_get_machine(&mid
);
728 return log_error_errno(r
, "Failed to acquire machine ID: %m");
730 r
= json_variant_new_string(&mmid
, sd_id128_to_string(mid
, mids
));
732 return log_error_errno(r
, "Failed to allocate matchMachineId object: %m");
734 per_machine
= json_variant_ref(json_variant_by_key(v
, "perMachine"));
736 _cleanup_(json_variant_unrefp
) JsonVariant
*npm
= NULL
, *add
= NULL
;
737 _cleanup_free_ JsonVariant
**array
= NULL
;
741 if (!json_variant_is_array(per_machine
))
742 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "perMachine field is not an array, refusing.");
744 array
= new(JsonVariant
*, json_variant_elements(per_machine
) + 1);
748 JSON_VARIANT_ARRAY_FOREACH(z
, per_machine
) {
751 if (!json_variant_is_object(z
))
752 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "perMachine entry is not an object, refusing.");
756 u
= json_variant_by_key(z
, "matchMachineId");
760 if (!json_variant_equal(u
, mmid
))
763 r
= json_variant_merge(&add
, z
);
765 return log_error_errno(r
, "Failed to merge perMachine entry: %m");
770 r
= json_variant_filter(&add
, arg_identity_filter
);
772 return log_error_errno(r
, "Failed to filter perMachine: %m");
774 r
= json_variant_merge(&add
, arg_identity_extra_this_machine
);
776 return log_error_errno(r
, "Failed to merge in perMachine fields: %m");
778 if (arg_identity_filter_rlimits
|| arg_identity_extra_rlimits
) {
779 _cleanup_(json_variant_unrefp
) JsonVariant
*rlv
= NULL
;
781 rlv
= json_variant_ref(json_variant_by_key(add
, "resourceLimits"));
783 r
= json_variant_filter(&rlv
, arg_identity_filter_rlimits
);
785 return log_error_errno(r
, "Failed to filter resource limits: %m");
787 r
= json_variant_merge(&rlv
, arg_identity_extra_rlimits
);
789 return log_error_errno(r
, "Failed to set resource limits: %m");
791 if (json_variant_is_blank_object(rlv
)) {
792 r
= json_variant_filter(&add
, STRV_MAKE("resourceLimits"));
794 return log_error_errno(r
, "Failed to drop resource limits field from identity: %m");
796 r
= json_variant_set_field(&add
, "resourceLimits", rlv
);
798 return log_error_errno(r
, "Failed to update resource limits of identity: %m");
802 if (!json_variant_is_blank_object(add
)) {
803 r
= json_variant_set_field(&add
, "matchMachineId", mmid
);
805 return log_error_errno(r
, "Failed to set matchMachineId field: %m");
810 r
= json_variant_new_array(&npm
, array
, i
);
812 return log_error_errno(r
, "Failed to allocate new perMachine array: %m");
814 json_variant_unref(per_machine
);
815 per_machine
= TAKE_PTR(npm
);
817 _cleanup_(json_variant_unrefp
) JsonVariant
*item
= json_variant_ref(arg_identity_extra_this_machine
);
819 if (arg_identity_extra_rlimits
) {
820 r
= json_variant_set_field(&item
, "resourceLimits", arg_identity_extra_rlimits
);
822 return log_error_errno(r
, "Failed to update resource limits of identity: %m");
825 r
= json_variant_set_field(&item
, "matchMachineId", mmid
);
827 return log_error_errno(r
, "Failed to set matchMachineId field: %m");
829 r
= json_variant_append_array(&per_machine
, item
);
831 return log_error_errno(r
, "Failed to append to perMachine array: %m");
834 r
= json_variant_set_field(&v
, "perMachine", per_machine
);
836 return log_error_errno(r
, "Failed to update per machine record: %m");
839 if (arg_identity_extra_privileged
|| arg_identity_filter
) {
840 _cleanup_(json_variant_unrefp
) JsonVariant
*privileged
= NULL
;
842 privileged
= json_variant_ref(json_variant_by_key(v
, "privileged"));
844 r
= json_variant_filter(&privileged
, arg_identity_filter
);
846 return log_error_errno(r
, "Failed to filter identity (privileged part): %m");
848 r
= json_variant_merge(&privileged
, arg_identity_extra_privileged
);
850 return log_error_errno(r
, "Failed to merge identities (privileged part): %m");
852 if (json_variant_is_blank_object(privileged
)) {
853 r
= json_variant_filter(&v
, STRV_MAKE("privileged"));
855 return log_error_errno(r
, "Failed to drop privileged part from identity: %m");
857 r
= json_variant_set_field(&v
, "privileged", privileged
);
859 return log_error_errno(r
, "Failed to update privileged part of identity: %m");
863 if (arg_identity_filter_rlimits
) {
864 _cleanup_(json_variant_unrefp
) JsonVariant
*rlv
= NULL
;
866 rlv
= json_variant_ref(json_variant_by_key(v
, "resourceLimits"));
868 r
= json_variant_filter(&rlv
, arg_identity_filter_rlimits
);
870 return log_error_errno(r
, "Failed to filter resource limits: %m");
872 /* Note that we only filter resource limits here, but don't apply them. We do that in the perMachine section */
874 if (json_variant_is_blank_object(rlv
)) {
875 r
= json_variant_filter(&v
, STRV_MAKE("resourceLimits"));
877 return log_error_errno(r
, "Failed to drop resource limits field from identity: %m");
879 r
= json_variant_set_field(&v
, "resourceLimits", rlv
);
881 return log_error_errno(r
, "Failed to update resource limits of identity: %m");
885 json_variant_unref(*_v
);
891 static int add_disposition(JsonVariant
**v
) {
896 if (json_variant_by_key(*v
, "disposition"))
899 /* Set the disposition to regular, if not configured explicitly */
900 r
= json_variant_set_field_string(v
, "disposition", "regular");
902 return log_error_errno(r
, "Failed to set disposition field: %m");
907 static int acquire_new_home_record(UserRecord
**ret
) {
908 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
909 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
916 unsigned line
, column
;
919 streq(arg_identity
, "-") ? stdin
: NULL
,
920 streq(arg_identity
, "-") ? "<stdin>" : arg_identity
, JSON_PARSE_SENSITIVE
, &v
, &line
, &column
);
922 return log_error_errno(r
, "Failed to parse identity at %u:%u: %m", line
, column
);
925 r
= apply_identity_changes(&v
);
929 r
= add_disposition(&v
);
933 STRV_FOREACH(i
, arg_pkcs11_token_uri
) {
934 r
= identity_add_pkcs11_key_data(&v
, *i
);
939 STRV_FOREACH(i
, arg_fido2_device
) {
940 r
= identity_add_fido2_parameters(&v
, *i
);
945 if (arg_recovery_key
) {
946 r
= identity_add_recovery_key(&v
);
951 r
= update_last_change(&v
, true, false);
956 json_variant_dump(v
, JSON_FORMAT_PRETTY
, NULL
, NULL
);
958 hr
= user_record_new();
962 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
);
970 static int acquire_new_password(
971 const char *user_name
,
983 e
= getenv("NEWPASSWORD");
985 _cleanup_(erase_and_freep
) char *copy
= NULL
;
987 /* As above, this is not for use, just for testing */
995 r
= user_record_set_password(hr
, STRV_MAKE(e
), /* prepend = */ true);
997 return log_error_errno(r
, "Failed to store password: %m");
1001 if (unsetenv("NEWPASSWORD") < 0)
1002 return log_error_errno(errno
, "Failed to unset $NEWPASSWORD: %m");
1005 *ret
= TAKE_PTR(copy
);
1011 (void) suggest_passwords();
1014 _cleanup_(strv_free_erasep
) char **first
= NULL
, **second
= NULL
;
1015 _cleanup_free_
char *question
= NULL
;
1018 return log_error_errno(SYNTHETIC_ERRNO(ENOKEY
), "Too many attempts, giving up:");
1020 if (asprintf(&question
, "Please enter new password for user %s:", user_name
) < 0)
1023 r
= ask_password_auto(question
, "user-home", NULL
, "home-password", USEC_INFINITY
, 0, &first
);
1025 return log_error_errno(r
, "Failed to acquire password: %m");
1027 question
= mfree(question
);
1028 if (asprintf(&question
, "Please enter new password for user %s (repeat):", user_name
) < 0)
1031 r
= ask_password_auto(question
, "user-home", NULL
, "home-password", USEC_INFINITY
, 0, &second
);
1033 return log_error_errno(r
, "Failed to acquire password: %m");
1035 if (strv_equal(first
, second
)) {
1036 _cleanup_(erase_and_freep
) char *copy
= NULL
;
1039 copy
= strdup(first
[0]);
1044 r
= user_record_set_password(hr
, first
, /* prepend = */ true);
1046 return log_error_errno(r
, "Failed to store password: %m");
1049 *ret
= TAKE_PTR(copy
);
1054 log_error("Password didn't match, try again.");
1058 static int create_home(int argc
, char *argv
[], void *userdata
) {
1059 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1060 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
1063 r
= acquire_bus(&bus
);
1067 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1070 /* If a username was specified, use it */
1072 if (valid_user_group_name(argv
[1], 0))
1073 r
= json_variant_set_field_string(&arg_identity_extra
, "userName", argv
[1]);
1075 _cleanup_free_
char *un
= NULL
, *rr
= NULL
;
1077 /* Before we consider the user name invalid, let's check if we can split it? */
1078 r
= split_user_name_realm(argv
[1], &un
, &rr
);
1080 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User name '%s' is not valid: %m", argv
[1]);
1083 r
= json_variant_set_field_string(&arg_identity_extra
, "realm", rr
);
1085 return log_error_errno(r
, "Failed to set realm field: %m");
1088 r
= json_variant_set_field_string(&arg_identity_extra
, "userName", un
);
1091 return log_error_errno(r
, "Failed to set userName field: %m");
1093 /* If neither a username nor an identity have been specified we cannot operate. */
1095 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User name required.");
1098 r
= acquire_new_home_record(&hr
);
1102 /* If the JSON record carries no plain text password (besides the recovery key), then let's query it
1104 if (strv_length(hr
->password
) <= arg_recovery_key
) {
1106 if (strv_isempty(hr
->hashed_password
)) {
1107 _cleanup_(erase_and_freep
) char *new_password
= NULL
;
1109 /* No regular (i.e. non-PKCS#11) hashed passwords set in the record, let's fix that. */
1110 r
= acquire_new_password(hr
->user_name
, hr
, /* suggest = */ true, &new_password
);
1114 r
= user_record_make_hashed_password(hr
, STRV_MAKE(new_password
), /* extend = */ false);
1116 return log_error_errno(r
, "Failed to hash password: %m");
1118 /* There's a hash password set in the record, acquire the unhashed version of it. */
1119 r
= acquire_existing_password(hr
->user_name
, hr
, /* emphasize_current= */ false);
1125 if (hr
->enforce_password_policy
== 0) {
1126 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1128 /* If password quality enforcement is disabled, let's at least warn client side */
1130 r
= user_record_quality_check_password(hr
, hr
, &error
);
1132 log_warning_errno(r
, "Specified password does not pass quality checks (%s), proceeding anyway.", bus_error_message(&error
, r
));
1136 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1137 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1138 _cleanup_(erase_and_freep
) char *formatted
= NULL
;
1140 r
= json_variant_format(hr
->json
, 0, &formatted
);
1142 return log_error_errno(r
, "Failed to format user record: %m");
1144 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "CreateHome");
1146 return bus_log_create_error(r
);
1148 (void) sd_bus_message_sensitive(m
);
1150 r
= sd_bus_message_append(m
, "s", formatted
);
1152 return bus_log_create_error(r
);
1154 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1156 if (sd_bus_error_has_name(&error
, BUS_ERROR_LOW_PASSWORD_QUALITY
)) {
1157 _cleanup_(erase_and_freep
) char *new_password
= NULL
;
1159 log_error_errno(r
, "%s", bus_error_message(&error
, r
));
1160 log_info("(Use --enforce-password-policy=no to turn off password quality checks for this account.)");
1162 r
= acquire_new_password(hr
->user_name
, hr
, /* suggest = */ false, &new_password
);
1166 r
= user_record_make_hashed_password(hr
, STRV_MAKE(new_password
), /* extend = */ false);
1168 return log_error_errno(r
, "Failed to hash passwords: %m");
1170 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1181 static int remove_home(int argc
, char *argv
[], void *userdata
) {
1182 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1186 r
= acquire_bus(&bus
);
1190 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1192 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
1193 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1194 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1196 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "RemoveHome");
1198 return bus_log_create_error(r
);
1200 r
= sd_bus_message_append(m
, "s", *i
);
1202 return bus_log_create_error(r
);
1204 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1206 log_error_errno(r
, "Failed to remove home: %s", bus_error_message(&error
, r
));
1215 static int acquire_updated_home_record(
1217 const char *username
,
1220 _cleanup_(json_variant_unrefp
) JsonVariant
*json
= NULL
;
1221 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
1228 unsigned line
, column
;
1231 r
= json_parse_file(
1232 streq(arg_identity
, "-") ? stdin
: NULL
,
1233 streq(arg_identity
, "-") ? "<stdin>" : arg_identity
, JSON_PARSE_SENSITIVE
, &json
, &line
, &column
);
1235 return log_error_errno(r
, "Failed to parse identity at %u:%u: %m", line
, column
);
1237 un
= json_variant_by_key(json
, "userName");
1239 if (!json_variant_is_string(un
) || (username
&& !streq(json_variant_string(un
), username
)))
1240 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User name specified on command line and in JSON record do not match.");
1243 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "No username specified.");
1245 r
= json_variant_set_field_string(&arg_identity_extra
, "userName", username
);
1247 return log_error_errno(r
, "Failed to set userName field: %m");
1251 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1252 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1256 if (!identity_properties_specified())
1257 return log_error_errno(SYNTHETIC_ERRNO(EALREADY
), "No field to change specified.");
1259 r
= bus_call_method(bus
, bus_mgr
, "GetUserRecordByName", &error
, &reply
, "s", username
);
1261 return log_error_errno(r
, "Failed to acquire user home record: %s", bus_error_message(&error
, r
));
1263 r
= sd_bus_message_read(reply
, "sbo", &text
, &incomplete
, NULL
);
1265 return bus_log_parse_error(r
);
1268 return log_error_errno(SYNTHETIC_ERRNO(EACCES
), "Lacking rights to acquire user record including privileged metadata, can't update record.");
1270 r
= json_parse(text
, JSON_PARSE_SENSITIVE
, &json
, NULL
, NULL
);
1272 return log_error_errno(r
, "Failed to parse JSON identity: %m");
1274 reply
= sd_bus_message_unref(reply
);
1276 r
= json_variant_filter(&json
, STRV_MAKE("binding", "status", "signature"));
1278 return log_error_errno(r
, "Failed to strip binding and status from record to update: %m");
1281 r
= apply_identity_changes(&json
);
1285 STRV_FOREACH(i
, arg_pkcs11_token_uri
) {
1286 r
= identity_add_pkcs11_key_data(&json
, *i
);
1291 STRV_FOREACH(i
, arg_fido2_device
) {
1292 r
= identity_add_fido2_parameters(&json
, *i
);
1297 /* If the user supplied a full record, then add in lastChange, but do not override. Otherwise always
1299 r
= update_last_change(&json
, arg_pkcs11_token_uri
|| arg_fido2_device
, !arg_identity
);
1304 json_variant_dump(json
, JSON_FORMAT_PRETTY
, NULL
, NULL
);
1306 hr
= user_record_new();
1310 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
);
1314 *ret
= TAKE_PTR(hr
);
1318 static int home_record_reset_human_interaction_permission(UserRecord
*hr
) {
1323 /* When we execute multiple operations one after the other, let's reset the permission to ask the
1324 * user each time, so that if interaction is necessary we will be told so again and thus can print a
1325 * nice message to the user, telling the user so. */
1327 r
= user_record_set_pkcs11_protected_authentication_path_permitted(hr
, -1);
1329 return log_error_errno(r
, "Failed to reset PKCS#11 protected authentication path permission flag: %m");
1331 r
= user_record_set_fido2_user_presence_permitted(hr
, -1);
1333 return log_error_errno(r
, "Failed to reset FIDO2 user presence permission flag: %m");
1338 static int update_home(int argc
, char *argv
[], void *userdata
) {
1339 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1340 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
1341 _cleanup_free_
char *buffer
= NULL
;
1342 const char *username
;
1347 else if (!arg_identity
) {
1348 buffer
= getusername_malloc();
1356 r
= acquire_bus(&bus
);
1360 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1362 r
= acquire_updated_home_record(bus
, username
, &hr
);
1366 /* If we do multiple operations, let's output things more verbosely, since otherwise the repeated
1367 * authentication might be confusing. */
1369 if (arg_and_resize
|| arg_and_change_password
)
1370 log_info("Updating home directory.");
1373 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1374 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1375 _cleanup_free_
char *formatted
= NULL
;
1377 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "UpdateHome");
1379 return bus_log_create_error(r
);
1381 r
= json_variant_format(hr
->json
, 0, &formatted
);
1383 return log_error_errno(r
, "Failed to format user record: %m");
1385 (void) sd_bus_message_sensitive(m
);
1387 r
= sd_bus_message_append(m
, "s", formatted
);
1389 return bus_log_create_error(r
);
1391 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1393 if (arg_and_change_password
&&
1394 sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1395 /* In the generic handler we'd ask for a password in this case, but when
1396 * changing passwords that's not sufficient, as we need to acquire all keys
1398 return log_error_errno(r
, "Security token not inserted, refusing.");
1400 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1408 log_info("Resizing home.");
1410 (void) home_record_reset_human_interaction_permission(hr
);
1412 /* Also sync down disk size to underlying LUKS/fscrypt/quota */
1413 while (arg_and_resize
) {
1414 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1415 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1417 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ResizeHome");
1419 return bus_log_create_error(r
);
1421 /* Specify UINT64_MAX as size, in which case the underlying disk size will just be synced */
1422 r
= sd_bus_message_append(m
, "st", hr
->user_name
, UINT64_MAX
);
1424 return bus_log_create_error(r
);
1426 r
= bus_message_append_secret(m
, hr
);
1428 return bus_log_create_error(r
);
1430 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1432 if (arg_and_change_password
&&
1433 sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1434 return log_error_errno(r
, "Security token not inserted, refusing.");
1436 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1443 if (arg_and_change_password
)
1444 log_info("Synchronizing passwords and encryption keys.");
1446 (void) home_record_reset_human_interaction_permission(hr
);
1448 /* Also sync down passwords to underlying LUKS/fscrypt */
1449 while (arg_and_change_password
) {
1450 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1451 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1453 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ChangePasswordHome");
1455 return bus_log_create_error(r
);
1457 /* Specify an empty new secret, in which case the underlying LUKS/fscrypt password will just be synced */
1458 r
= sd_bus_message_append(m
, "ss", hr
->user_name
, "{}");
1460 return bus_log_create_error(r
);
1462 r
= bus_message_append_secret(m
, hr
);
1464 return bus_log_create_error(r
);
1466 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1468 if (sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1469 return log_error_errno(r
, "Security token not inserted, refusing.");
1471 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1481 static int passwd_home(int argc
, char *argv
[], void *userdata
) {
1482 _cleanup_(user_record_unrefp
) UserRecord
*old_secret
= NULL
, *new_secret
= NULL
;
1483 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1484 _cleanup_free_
char *buffer
= NULL
;
1485 const char *username
;
1488 if (arg_pkcs11_token_uri
)
1489 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "To change the PKCS#11 security token use 'homectl update --pkcs11-token-uri=…'.");
1490 if (arg_fido2_device
)
1491 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "To change the FIDO2 security token use 'homectl update --fido2-device=…'.");
1492 if (identity_properties_specified())
1493 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "The 'passwd' verb does not permit changing other record properties at the same time.");
1498 buffer
= getusername_malloc();
1505 r
= acquire_bus(&bus
);
1509 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1511 old_secret
= user_record_new();
1515 new_secret
= user_record_new();
1519 r
= acquire_new_password(username
, new_secret
, /* suggest = */ true, NULL
);
1524 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1525 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1527 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ChangePasswordHome");
1529 return bus_log_create_error(r
);
1531 r
= sd_bus_message_append(m
, "s", username
);
1533 return bus_log_create_error(r
);
1535 r
= bus_message_append_secret(m
, new_secret
);
1537 return bus_log_create_error(r
);
1539 r
= bus_message_append_secret(m
, old_secret
);
1541 return bus_log_create_error(r
);
1543 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1545 if (sd_bus_error_has_name(&error
, BUS_ERROR_LOW_PASSWORD_QUALITY
)) {
1547 log_error_errno(r
, "%s", bus_error_message(&error
, r
));
1549 r
= acquire_new_password(username
, new_secret
, /* suggest = */ false, NULL
);
1551 } else if (sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1553 /* In the generic handler we'd ask for a password in this case, but when
1554 * changing passwords that's not sufficeint, as we need to acquire all keys
1556 return log_error_errno(r
, "Security token not inserted, refusing.");
1558 r
= handle_generic_user_record_error(username
, old_secret
, &error
, r
, true);
1568 static int resize_home(int argc
, char *argv
[], void *userdata
) {
1569 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1570 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
1571 uint64_t ds
= UINT64_MAX
;
1574 r
= acquire_bus(&bus
);
1578 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1580 if (arg_disk_size_relative
!= UINT64_MAX
||
1581 (argc
> 2 && parse_percent(argv
[2]) >= 0))
1582 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
1583 "Relative disk size specification currently not supported when resizing.");
1586 r
= parse_size(argv
[2], 1024, &ds
);
1588 return log_error_errno(r
, "Failed to parse disk size parameter: %s", argv
[2]);
1591 if (arg_disk_size
!= UINT64_MAX
) {
1592 if (ds
!= UINT64_MAX
&& ds
!= arg_disk_size
)
1593 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Disk size specified twice and doesn't match, refusing.");
1598 secret
= user_record_new();
1603 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1604 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1606 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ResizeHome");
1608 return bus_log_create_error(r
);
1610 r
= sd_bus_message_append(m
, "st", argv
[1], ds
);
1612 return bus_log_create_error(r
);
1614 r
= bus_message_append_secret(m
, secret
);
1616 return bus_log_create_error(r
);
1618 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1620 r
= handle_generic_user_record_error(argv
[1], secret
, &error
, r
, false);
1630 static int lock_home(int argc
, char *argv
[], void *userdata
) {
1631 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1635 r
= acquire_bus(&bus
);
1639 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
1640 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1641 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1643 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "LockHome");
1645 return bus_log_create_error(r
);
1647 r
= sd_bus_message_append(m
, "s", *i
);
1649 return bus_log_create_error(r
);
1651 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1653 log_error_errno(r
, "Failed to lock home: %s", bus_error_message(&error
, r
));
1662 static int unlock_home(int argc
, char *argv
[], void *userdata
) {
1663 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1667 r
= acquire_bus(&bus
);
1671 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
1672 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
1674 secret
= user_record_new();
1679 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1680 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1682 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "UnlockHome");
1684 return bus_log_create_error(r
);
1686 r
= sd_bus_message_append(m
, "s", *i
);
1688 return bus_log_create_error(r
);
1690 r
= bus_message_append_secret(m
, secret
);
1692 return bus_log_create_error(r
);
1694 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1696 r
= handle_generic_user_record_error(argv
[1], secret
, &error
, r
, false);
1711 static int with_home(int argc
, char *argv
[], void *userdata
) {
1712 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1713 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1714 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1715 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
1716 _cleanup_close_
int acquired_fd
= -1;
1717 _cleanup_strv_free_
char **cmdline
= NULL
;
1722 r
= acquire_bus(&bus
);
1727 _cleanup_free_
char *shell
= NULL
;
1729 /* If no command is specified, spawn a shell */
1730 r
= get_shell(&shell
);
1732 return log_error_errno(r
, "Failed to acquire shell: %m");
1734 cmdline
= strv_new(shell
);
1736 cmdline
= strv_copy(argv
+ 2);
1740 secret
= user_record_new();
1745 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "AcquireHome");
1747 return bus_log_create_error(r
);
1749 r
= sd_bus_message_append(m
, "s", argv
[1]);
1751 return bus_log_create_error(r
);
1753 r
= bus_message_append_secret(m
, secret
);
1755 return bus_log_create_error(r
);
1757 r
= sd_bus_message_append(m
, "b", /* please_suspend = */ getenv_bool("SYSTEMD_PLEASE_SUSPEND_HOME") > 0);
1759 return bus_log_create_error(r
);
1761 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, &reply
);
1762 m
= sd_bus_message_unref(m
);
1764 r
= handle_generic_user_record_error(argv
[1], secret
, &error
, r
, false);
1768 sd_bus_error_free(&error
);
1772 r
= sd_bus_message_read(reply
, "h", &fd
);
1774 return bus_log_parse_error(r
);
1776 acquired_fd
= fcntl(fd
, F_DUPFD_CLOEXEC
, 3);
1777 if (acquired_fd
< 0)
1778 return log_error_errno(errno
, "Failed to duplicate acquired fd: %m");
1780 reply
= sd_bus_message_unref(reply
);
1785 r
= bus_call_method(bus
, bus_mgr
, "GetHomeByName", &error
, &reply
, "s", argv
[1]);
1787 return log_error_errno(r
, "Failed to inspect home: %s", bus_error_message(&error
, r
));
1789 r
= sd_bus_message_read(reply
, "usussso", NULL
, NULL
, NULL
, NULL
, &home
, NULL
, NULL
);
1791 return bus_log_parse_error(r
);
1793 r
= safe_fork("(with)", FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_DEATHSIG
|FORK_LOG
|FORK_RLIMIT_NOFILE_SAFE
|FORK_REOPEN_LOG
, &pid
);
1797 if (chdir(home
) < 0) {
1798 log_error_errno(errno
, "Failed to change to directory %s: %m", home
);
1802 execvp(cmdline
[0], cmdline
);
1803 log_error_errno(errno
, "Failed to execute %s: %m", cmdline
[0]);
1807 ret
= wait_for_terminate_and_check(cmdline
[0], pid
, WAIT_LOG_ABNORMAL
);
1809 /* Close the fd that pings the home now. */
1810 acquired_fd
= safe_close(acquired_fd
);
1812 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "ReleaseHome");
1814 return bus_log_create_error(r
);
1816 r
= sd_bus_message_append(m
, "s", argv
[1]);
1818 return bus_log_create_error(r
);
1820 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1822 if (sd_bus_error_has_name(&error
, BUS_ERROR_HOME_BUSY
))
1823 log_notice("Not deactivating home directory of %s, as it is still used.", argv
[1]);
1825 return log_error_errno(r
, "Failed to release user home: %s", bus_error_message(&error
, r
));
1831 static int lock_all_homes(int argc
, char *argv
[], void *userdata
) {
1832 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1833 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1834 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1837 r
= acquire_bus(&bus
);
1841 r
= bus_message_new_method_call(bus
, &m
, bus_mgr
, "LockAllHomes");
1843 return bus_log_create_error(r
);
1845 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1847 return log_error_errno(r
, "Failed to lock home: %s", bus_error_message(&error
, r
));
1852 static int drop_from_identity(const char *field
) {
1857 /* If we are called to update an identity record and drop some field, let's keep track of what to
1858 * remove from the old record */
1859 r
= strv_extend(&arg_identity_filter
, field
);
1863 /* Let's also drop the field if it was previously set to a new value on the same command line */
1864 r
= json_variant_filter(&arg_identity_extra
, STRV_MAKE(field
));
1866 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
1868 r
= json_variant_filter(&arg_identity_extra_this_machine
, STRV_MAKE(field
));
1870 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
1872 r
= json_variant_filter(&arg_identity_extra_privileged
, STRV_MAKE(field
));
1874 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
1879 static int help(int argc
, char *argv
[], void *userdata
) {
1880 _cleanup_free_
char *link
= NULL
;
1883 (void) pager_open(arg_pager_flags
);
1885 r
= terminal_urlify_man("homectl", "1", &link
);
1889 printf("%1$s [OPTIONS...] COMMAND ...\n\n"
1890 "%2$sCreate, manipulate or inspect home directories.%3$s\n"
1891 "\n%4$sCommands:%5$s\n"
1892 " list List home areas\n"
1893 " activate USER… Activate a home area\n"
1894 " deactivate USER… Deactivate a home area\n"
1895 " inspect USER… Inspect a home area\n"
1896 " authenticate USER… Authenticate a home area\n"
1897 " create USER Create a home area\n"
1898 " remove USER… Remove a home area\n"
1899 " update USER Update a home area\n"
1900 " passwd USER Change password of a home area\n"
1901 " resize USER SIZE Resize a home area\n"
1902 " lock USER… Temporarily lock an active home area\n"
1903 " unlock USER… Unlock a temporarily locked home area\n"
1904 " lock-all Lock all suitable home areas\n"
1905 " with USER [COMMAND…] Run shell or command with access to a home area\n"
1906 "\n%4$sOptions:%5$s\n"
1907 " -h --help Show this help\n"
1908 " --version Show package version\n"
1909 " --no-pager Do not pipe output into a pager\n"
1910 " --no-legend Do not show the headers and footers\n"
1911 " --no-ask-password Do not ask for system passwords\n"
1912 " -H --host=[USER@]HOST Operate on remote host\n"
1913 " -M --machine=CONTAINER Operate on local container\n"
1914 " --identity=PATH Read JSON identity from file\n"
1915 " --json=FORMAT Output inspection data in JSON (takes one of\n"
1916 " pretty, short, off)\n"
1917 " -j Equivalent to --json=pretty (on TTY) or\n"
1918 " --json=short (otherwise)\n"
1919 " --export-format= Strip JSON inspection data (full, stripped,\n"
1921 " -E When specified once equals -j --export-format=\n"
1922 " stripped, when specified twice equals\n"
1923 " -j --export-format=minimal\n"
1924 "\n%4$sGeneral User Record Properties:%5$s\n"
1925 " -c --real-name=REALNAME Real name for user\n"
1926 " --realm=REALM Realm to create user in\n"
1927 " --email-address=EMAIL Email address for user\n"
1928 " --location=LOCATION Set location of user on earth\n"
1929 " --icon-name=NAME Icon name for user\n"
1930 " -d --home-dir=PATH Home directory\n"
1931 " -u --uid=UID Numeric UID for user\n"
1932 " -G --member-of=GROUP Add user to group\n"
1933 " --skel=PATH Skeleton directory to use\n"
1934 " --shell=PATH Shell for account\n"
1935 " --setenv=VARIABLE=VALUE Set an environment variable at log-in\n"
1936 " --timezone=TIMEZONE Set a time-zone\n"
1937 " --language=LOCALE Set preferred language\n"
1938 " --ssh-authorized-keys=KEYS\n"
1939 " Specify SSH public keys\n"
1940 " --pkcs11-token-uri=URI URI to PKCS#11 security token containing\n"
1941 " private key and matching X.509 certificate\n"
1942 " --fido2-device=PATH Path to FIDO2 hidraw device with hmac-secret\n"
1944 " --recovery-key=BOOL Add a recovery key\n"
1945 "\n%4$sAccount Management User Record Properties:%5$s\n"
1946 " --locked=BOOL Set locked account state\n"
1947 " --not-before=TIMESTAMP Do not allow logins before\n"
1948 " --not-after=TIMESTAMP Do not allow logins after\n"
1949 " --rate-limit-interval=SECS\n"
1950 " Login rate-limit interval in seconds\n"
1951 " --rate-limit-burst=NUMBER\n"
1952 " Login rate-limit attempts per interval\n"
1953 "\n%4$sPassword Policy User Record Properties:%5$s\n"
1954 " --password-hint=HINT Set Password hint\n"
1955 " --enforce-password-policy=BOOL\n"
1956 " Control whether to enforce system's password\n"
1957 " policy for this user\n"
1958 " -P Equivalent to --enforce-password-password=no\n"
1959 " --password-change-now=BOOL\n"
1960 " Require the password to be changed on next login\n"
1961 " --password-change-min=TIME\n"
1962 " Require minimum time between password changes\n"
1963 " --password-change-max=TIME\n"
1964 " Require maximum time between password changes\n"
1965 " --password-change-warn=TIME\n"
1966 " How much time to warn before password expiry\n"
1967 " --password-change-inactive=TIME\n"
1968 " How much time to block password after expiry\n"
1969 "\n%4$sResource Management User Record Properties:%5$s\n"
1970 " --disk-size=BYTES Size to assign the user on disk\n"
1971 " --access-mode=MODE User home directory access mode\n"
1972 " --umask=MODE Umask for user when logging in\n"
1973 " --nice=NICE Nice level for user\n"
1974 " --rlimit=LIMIT=VALUE[:VALUE]\n"
1975 " Set resource limits\n"
1976 " --tasks-max=MAX Set maximum number of per-user tasks\n"
1977 " --memory-high=BYTES Set high memory threshold in bytes\n"
1978 " --memory-max=BYTES Set maximum memory limit\n"
1979 " --cpu-weight=WEIGHT Set CPU weight\n"
1980 " --io-weight=WEIGHT Set IO weight\n"
1981 "\n%4$sStorage User Record Properties:%5$s\n"
1982 " --storage=STORAGE Storage type to use (luks, fscrypt, directory,\n"
1983 " subvolume, cifs)\n"
1984 " --image-path=PATH Path to image file/directory\n"
1985 "\n%4$sLUKS Storage User Record Properties:%5$s\n"
1986 " --fs-type=TYPE File system type to use in case of luks\n"
1987 " storage (btrfs, ext4, xfs)\n"
1988 " --luks-discard=BOOL Whether to use 'discard' feature of file system\n"
1989 " when activated (mounted)\n"
1990 " --luks-offline-discard=BOOL\n"
1991 " Whether to trim file on logout\n"
1992 " --luks-cipher=CIPHER Cipher to use for LUKS encryption\n"
1993 " --luks-cipher-mode=MODE Cipher mode to use for LUKS encryption\n"
1994 " --luks-volume-key-size=BITS\n"
1995 " Volume key size to use for LUKS encryption\n"
1996 " --luks-pbkdf-type=TYPE Password-based Key Derivation Function to use\n"
1997 " --luks-pbkdf-hash-algorithm=ALGORITHM\n"
1998 " PBKDF hash algorithm to use\n"
1999 " --luks-pbkdf-time-cost=SECS\n"
2000 " Time cost for PBKDF in seconds\n"
2001 " --luks-pbkdf-memory-cost=BYTES\n"
2002 " Memory cost for PBKDF in bytes\n"
2003 " --luks-pbkdf-parallel-threads=NUMBER\n"
2004 " Number of parallel threads for PKBDF\n"
2005 "\n%4$sMounting User Record Properties:%5$s\n"
2006 " --nosuid=BOOL Control the 'nosuid' flag of the home mount\n"
2007 " --nodev=BOOL Control the 'nodev' flag of the home mount\n"
2008 " --noexec=BOOL Control the 'noexec' flag of the home mount\n"
2009 "\n%4$sCIFS User Record Properties:%5$s\n"
2010 " --cifs-domain=DOMAIN CIFS (Windows) domain\n"
2011 " --cifs-user-name=USER CIFS (Windows) user name\n"
2012 " --cifs-service=SERVICE CIFS (Windows) service to mount as home area\n"
2013 "\n%4$sLogin Behaviour User Record Properties:%5$s\n"
2014 " --stop-delay=SECS How long to leave user services running after\n"
2016 " --kill-processes=BOOL Whether to kill user processes when sessions\n"
2018 " --auto-login=BOOL Try to log this user in automatically\n"
2019 "\nSee the %6$s for details.\n"
2020 , program_invocation_short_name
2021 , ansi_highlight(), ansi_normal()
2022 , ansi_underline(), ansi_normal()
2029 static int parse_argv(int argc
, char *argv
[]) {
2032 ARG_VERSION
= 0x100,
2035 ARG_NO_ASK_PASSWORD
,
2045 ARG_LUKS_OFFLINE_DISCARD
,
2051 ARG_SSH_AUTHORIZED_KEYS
,
2060 ARG_LUKS_CIPHER_MODE
,
2061 ARG_LUKS_VOLUME_KEY_SIZE
,
2073 ARG_LUKS_PBKDF_TYPE
,
2074 ARG_LUKS_PBKDF_HASH_ALGORITHM
,
2075 ARG_LUKS_PBKDF_TIME_COST
,
2076 ARG_LUKS_PBKDF_MEMORY_COST
,
2077 ARG_LUKS_PBKDF_PARALLEL_THREADS
,
2078 ARG_RATE_LIMIT_INTERVAL
,
2079 ARG_RATE_LIMIT_BURST
,
2082 ARG_ENFORCE_PASSWORD_POLICY
,
2083 ARG_PASSWORD_CHANGE_NOW
,
2084 ARG_PASSWORD_CHANGE_MIN
,
2085 ARG_PASSWORD_CHANGE_MAX
,
2086 ARG_PASSWORD_CHANGE_WARN
,
2087 ARG_PASSWORD_CHANGE_INACTIVE
,
2090 ARG_PKCS11_TOKEN_URI
,
2094 ARG_AND_CHANGE_PASSWORD
,
2097 static const struct option options
[] = {
2098 { "help", no_argument
, NULL
, 'h' },
2099 { "version", no_argument
, NULL
, ARG_VERSION
},
2100 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
2101 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
2102 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
2103 { "host", required_argument
, NULL
, 'H' },
2104 { "machine", required_argument
, NULL
, 'M' },
2105 { "identity", required_argument
, NULL
, 'I' },
2106 { "real-name", required_argument
, NULL
, 'c' },
2107 { "comment", required_argument
, NULL
, 'c' }, /* Compat alias to keep thing in sync with useradd(8) */
2108 { "realm", required_argument
, NULL
, ARG_REALM
},
2109 { "email-address", required_argument
, NULL
, ARG_EMAIL_ADDRESS
},
2110 { "location", required_argument
, NULL
, ARG_LOCATION
},
2111 { "password-hint", required_argument
, NULL
, ARG_PASSWORD_HINT
},
2112 { "icon-name", required_argument
, NULL
, ARG_ICON_NAME
},
2113 { "home-dir", required_argument
, NULL
, 'd' }, /* Compatible with useradd(8) */
2114 { "uid", required_argument
, NULL
, 'u' }, /* Compatible with useradd(8) */
2115 { "member-of", required_argument
, NULL
, 'G' },
2116 { "groups", required_argument
, NULL
, 'G' }, /* Compat alias to keep thing in sync with useradd(8) */
2117 { "skel", required_argument
, NULL
, 'k' }, /* Compatible with useradd(8) */
2118 { "shell", required_argument
, NULL
, 's' }, /* Compatible with useradd(8) */
2119 { "setenv", required_argument
, NULL
, ARG_SETENV
},
2120 { "timezone", required_argument
, NULL
, ARG_TIMEZONE
},
2121 { "language", required_argument
, NULL
, ARG_LANGUAGE
},
2122 { "locked", required_argument
, NULL
, ARG_LOCKED
},
2123 { "not-before", required_argument
, NULL
, ARG_NOT_BEFORE
},
2124 { "not-after", required_argument
, NULL
, ARG_NOT_AFTER
},
2125 { "expiredate", required_argument
, NULL
, 'e' }, /* Compat alias to keep thing in sync with useradd(8) */
2126 { "ssh-authorized-keys", required_argument
, NULL
, ARG_SSH_AUTHORIZED_KEYS
},
2127 { "disk-size", required_argument
, NULL
, ARG_DISK_SIZE
},
2128 { "access-mode", required_argument
, NULL
, ARG_ACCESS_MODE
},
2129 { "umask", required_argument
, NULL
, ARG_UMASK
},
2130 { "nice", required_argument
, NULL
, ARG_NICE
},
2131 { "rlimit", required_argument
, NULL
, ARG_RLIMIT
},
2132 { "tasks-max", required_argument
, NULL
, ARG_TASKS_MAX
},
2133 { "memory-high", required_argument
, NULL
, ARG_MEMORY_HIGH
},
2134 { "memory-max", required_argument
, NULL
, ARG_MEMORY_MAX
},
2135 { "cpu-weight", required_argument
, NULL
, ARG_CPU_WEIGHT
},
2136 { "io-weight", required_argument
, NULL
, ARG_IO_WEIGHT
},
2137 { "storage", required_argument
, NULL
, ARG_STORAGE
},
2138 { "image-path", required_argument
, NULL
, ARG_IMAGE_PATH
},
2139 { "fs-type", required_argument
, NULL
, ARG_FS_TYPE
},
2140 { "luks-discard", required_argument
, NULL
, ARG_LUKS_DISCARD
},
2141 { "luks-offline-discard", required_argument
, NULL
, ARG_LUKS_OFFLINE_DISCARD
},
2142 { "luks-cipher", required_argument
, NULL
, ARG_LUKS_CIPHER
},
2143 { "luks-cipher-mode", required_argument
, NULL
, ARG_LUKS_CIPHER_MODE
},
2144 { "luks-volume-key-size", required_argument
, NULL
, ARG_LUKS_VOLUME_KEY_SIZE
},
2145 { "luks-pbkdf-type", required_argument
, NULL
, ARG_LUKS_PBKDF_TYPE
},
2146 { "luks-pbkdf-hash-algorithm", required_argument
, NULL
, ARG_LUKS_PBKDF_HASH_ALGORITHM
},
2147 { "luks-pbkdf-time-cost", required_argument
, NULL
, ARG_LUKS_PBKDF_TIME_COST
},
2148 { "luks-pbkdf-memory-cost", required_argument
, NULL
, ARG_LUKS_PBKDF_MEMORY_COST
},
2149 { "luks-pbkdf-parallel-threads", required_argument
, NULL
, ARG_LUKS_PBKDF_PARALLEL_THREADS
},
2150 { "nosuid", required_argument
, NULL
, ARG_NOSUID
},
2151 { "nodev", required_argument
, NULL
, ARG_NODEV
},
2152 { "noexec", required_argument
, NULL
, ARG_NOEXEC
},
2153 { "cifs-user-name", required_argument
, NULL
, ARG_CIFS_USER_NAME
},
2154 { "cifs-domain", required_argument
, NULL
, ARG_CIFS_DOMAIN
},
2155 { "cifs-service", required_argument
, NULL
, ARG_CIFS_SERVICE
},
2156 { "rate-limit-interval", required_argument
, NULL
, ARG_RATE_LIMIT_INTERVAL
},
2157 { "rate-limit-burst", required_argument
, NULL
, ARG_RATE_LIMIT_BURST
},
2158 { "stop-delay", required_argument
, NULL
, ARG_STOP_DELAY
},
2159 { "kill-processes", required_argument
, NULL
, ARG_KILL_PROCESSES
},
2160 { "enforce-password-policy", required_argument
, NULL
, ARG_ENFORCE_PASSWORD_POLICY
},
2161 { "password-change-now", required_argument
, NULL
, ARG_PASSWORD_CHANGE_NOW
},
2162 { "password-change-min", required_argument
, NULL
, ARG_PASSWORD_CHANGE_MIN
},
2163 { "password-change-max", required_argument
, NULL
, ARG_PASSWORD_CHANGE_MAX
},
2164 { "password-change-warn", required_argument
, NULL
, ARG_PASSWORD_CHANGE_WARN
},
2165 { "password-change-inactive", required_argument
, NULL
, ARG_PASSWORD_CHANGE_INACTIVE
},
2166 { "auto-login", required_argument
, NULL
, ARG_AUTO_LOGIN
},
2167 { "json", required_argument
, NULL
, ARG_JSON
},
2168 { "export-format", required_argument
, NULL
, ARG_EXPORT_FORMAT
},
2169 { "pkcs11-token-uri", required_argument
, NULL
, ARG_PKCS11_TOKEN_URI
},
2170 { "fido2-device", required_argument
, NULL
, ARG_FIDO2_DEVICE
},
2171 { "recovery-key", required_argument
, NULL
, ARG_RECOVERY_KEY
},
2172 { "and-resize", required_argument
, NULL
, ARG_AND_RESIZE
},
2173 { "and-change-password", required_argument
, NULL
, ARG_AND_CHANGE_PASSWORD
},
2185 c
= getopt_long(argc
, argv
, "hH:M:I:c:d:u:k:s:e:G:jPE", options
, NULL
);
2192 return help(0, NULL
, NULL
);
2198 arg_pager_flags
|= PAGER_DISABLE
;
2205 case ARG_NO_ASK_PASSWORD
:
2206 arg_ask_password
= false;
2210 arg_transport
= BUS_TRANSPORT_REMOTE
;
2215 arg_transport
= BUS_TRANSPORT_MACHINE
;
2220 arg_identity
= optarg
;
2224 if (isempty(optarg
)) {
2225 r
= drop_from_identity("realName");
2232 if (!valid_gecos(optarg
))
2233 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Real name '%s' not a valid GECOS field.", optarg
);
2235 r
= json_variant_set_field_string(&arg_identity_extra
, "realName", optarg
);
2237 return log_error_errno(r
, "Failed to set realName field: %m");
2242 _cleanup_free_
char *hd
= NULL
;
2244 if (isempty(optarg
)) {
2245 r
= drop_from_identity("homeDirectory");
2252 r
= parse_path_argument_and_warn(optarg
, false, &hd
);
2256 if (!valid_home(hd
))
2257 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Home directory '%s' not valid.", hd
);
2259 r
= json_variant_set_field_string(&arg_identity_extra
, "homeDirectory", hd
);
2261 return log_error_errno(r
, "Failed to set homeDirectory field: %m");
2267 if (isempty(optarg
)) {
2268 r
= drop_from_identity("realm");
2275 r
= dns_name_is_valid(optarg
);
2277 return log_error_errno(r
, "Failed to determine whether realm '%s' is a valid DNS domain: %m", optarg
);
2279 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Realm '%s' is not a valid DNS domain: %m", optarg
);
2281 r
= json_variant_set_field_string(&arg_identity_extra
, "realm", optarg
);
2283 return log_error_errno(r
, "Failed to set realm field: %m");
2286 case ARG_EMAIL_ADDRESS
:
2289 case ARG_CIFS_USER_NAME
:
2290 case ARG_CIFS_DOMAIN
:
2291 case ARG_CIFS_SERVICE
: {
2294 c
== ARG_EMAIL_ADDRESS
? "emailAddress" :
2295 c
== ARG_LOCATION
? "location" :
2296 c
== ARG_ICON_NAME
? "iconName" :
2297 c
== ARG_CIFS_USER_NAME
? "cifsUserName" :
2298 c
== ARG_CIFS_DOMAIN
? "cifsDomain" :
2299 c
== ARG_CIFS_SERVICE
? "cifsService" :
2304 if (isempty(optarg
)) {
2305 r
= drop_from_identity(field
);
2312 r
= json_variant_set_field_string(&arg_identity_extra
, field
, optarg
);
2314 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2319 case ARG_PASSWORD_HINT
:
2320 if (isempty(optarg
)) {
2321 r
= drop_from_identity("passwordHint");
2328 r
= json_variant_set_field_string(&arg_identity_extra_privileged
, "passwordHint", optarg
);
2330 return log_error_errno(r
, "Failed to set passwordHint field: %m");
2332 string_erase(optarg
);
2338 if (isempty(optarg
)) {
2339 r
= drop_from_identity("niceLevel");
2345 r
= parse_nice(optarg
, &nc
);
2347 return log_error_errno(r
, "Failed to parse nice level: %s", optarg
);
2349 r
= json_variant_set_field_integer(&arg_identity_extra
, "niceLevel", nc
);
2351 return log_error_errno(r
, "Failed to set niceLevel field: %m");
2357 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
, *jcur
= NULL
, *jmax
= NULL
;
2358 _cleanup_free_
char *field
= NULL
, *t
= NULL
;
2363 if (isempty(optarg
)) {
2364 /* Remove all resource limits */
2366 r
= drop_from_identity("resourceLimits");
2370 arg_identity_filter_rlimits
= strv_free(arg_identity_filter_rlimits
);
2371 arg_identity_extra_rlimits
= json_variant_unref(arg_identity_extra_rlimits
);
2375 eq
= strchr(optarg
, '=');
2377 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Can't parse resource limit assignment: %s", optarg
);
2379 field
= strndup(optarg
, eq
- optarg
);
2383 l
= rlimit_from_string_harder(field
);
2385 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Unknown resource limit type: %s", field
);
2387 if (isempty(eq
+ 1)) {
2388 /* Remove only the specific rlimit */
2390 r
= strv_extend(&arg_identity_filter_rlimits
, rlimit_to_string(l
));
2394 r
= json_variant_filter(&arg_identity_extra_rlimits
, STRV_MAKE(field
));
2396 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
2401 r
= rlimit_parse(l
, eq
+ 1, &rl
);
2403 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to parse resource limit value: %s", eq
+ 1);
2405 r
= rl
.rlim_cur
== RLIM_INFINITY
? json_variant_new_null(&jcur
) : json_variant_new_unsigned(&jcur
, rl
.rlim_cur
);
2407 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to allocate current integer: %m");
2409 r
= rl
.rlim_max
== RLIM_INFINITY
? json_variant_new_null(&jmax
) : json_variant_new_unsigned(&jmax
, rl
.rlim_max
);
2411 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to allocate maximum integer: %m");
2415 JSON_BUILD_PAIR("cur", JSON_BUILD_VARIANT(jcur
)),
2416 JSON_BUILD_PAIR("max", JSON_BUILD_VARIANT(jmax
))));
2418 return log_error_errno(r
, "Failed to build resource limit: %m");
2420 t
= strjoin("RLIMIT_", rlimit_to_string(l
));
2424 r
= json_variant_set_field(&arg_identity_extra_rlimits
, t
, v
);
2426 return log_error_errno(r
, "Failed to set %s field: %m", rlimit_to_string(l
));
2434 if (isempty(optarg
)) {
2435 r
= drop_from_identity("uid");
2442 r
= parse_uid(optarg
, &uid
);
2444 return log_error_errno(r
, "Failed to parse UID '%s'.", optarg
);
2446 if (uid_is_system(uid
))
2447 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "UID " UID_FMT
" is in system range, refusing.", uid
);
2448 if (uid_is_dynamic(uid
))
2449 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "UID " UID_FMT
" is in dynamic range, refusing.", uid
);
2450 if (uid
== UID_NOBODY
)
2451 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "UID " UID_FMT
" is nobody UID, refusing.", uid
);
2453 r
= json_variant_set_field_unsigned(&arg_identity_extra
, "uid", uid
);
2455 return log_error_errno(r
, "Failed to set realm field: %m");
2461 case ARG_IMAGE_PATH
: {
2462 const char *field
= c
== 'k' ? "skeletonDirectory" : "imagePath";
2463 _cleanup_free_
char *v
= NULL
;
2465 if (isempty(optarg
)) {
2466 r
= drop_from_identity(field
);
2473 r
= parse_path_argument_and_warn(optarg
, false, &v
);
2477 r
= json_variant_set_field_string(&arg_identity_extra_this_machine
, field
, v
);
2479 return log_error_errno(r
, "Failed to set %s field: %m", v
);
2485 if (isempty(optarg
)) {
2486 r
= drop_from_identity("shell");
2493 if (!valid_shell(optarg
))
2494 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Shell '%s' not valid.", optarg
);
2496 r
= json_variant_set_field_string(&arg_identity_extra
, "shell", optarg
);
2498 return log_error_errno(r
, "Failed to set shell field: %m");
2503 _cleanup_free_
char **l
= NULL
, **k
= NULL
;
2504 _cleanup_(json_variant_unrefp
) JsonVariant
*ne
= NULL
;
2507 if (isempty(optarg
)) {
2508 r
= drop_from_identity("environment");
2515 if (!env_assignment_is_valid(optarg
))
2516 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Environment assignment '%s' not valid.", optarg
);
2518 e
= json_variant_by_key(arg_identity_extra
, "environment");
2520 r
= json_variant_strv(e
, &l
);
2522 return log_error_errno(r
, "Failed to parse JSON environment field: %m");
2525 k
= strv_env_set(l
, optarg
);
2531 r
= json_variant_new_array_strv(&ne
, k
);
2533 return log_error_errno(r
, "Failed to allocate environment list JSON: %m");
2535 r
= json_variant_set_field(&arg_identity_extra
, "environment", ne
);
2537 return log_error_errno(r
, "Failed to set environment list: %m");
2544 if (isempty(optarg
)) {
2545 r
= drop_from_identity("timeZone");
2552 if (!timezone_is_valid(optarg
, LOG_DEBUG
))
2553 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Timezone '%s' is not valid.", optarg
);
2555 r
= json_variant_set_field_string(&arg_identity_extra
, "timeZone", optarg
);
2557 return log_error_errno(r
, "Failed to set timezone field: %m");
2562 if (isempty(optarg
)) {
2563 r
= drop_from_identity("language");
2570 if (!locale_is_valid(optarg
))
2571 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Locale '%s' is not valid.", optarg
);
2573 if (locale_is_installed(optarg
) <= 0)
2574 log_warning("Locale '%s' is not installed, accepting anyway.", optarg
);
2576 r
= json_variant_set_field_string(&arg_identity_extra
, "preferredLanguage", optarg
);
2578 return log_error_errno(r
, "Failed to set preferredLanguage field: %m");
2586 case ARG_KILL_PROCESSES
:
2587 case ARG_ENFORCE_PASSWORD_POLICY
:
2588 case ARG_AUTO_LOGIN
:
2589 case ARG_PASSWORD_CHANGE_NOW
: {
2591 c
== ARG_LOCKED
? "locked" :
2592 c
== ARG_NOSUID
? "mountNoSuid" :
2593 c
== ARG_NODEV
? "mountNoDevices" :
2594 c
== ARG_NOEXEC
? "mountNoExecute" :
2595 c
== ARG_KILL_PROCESSES
? "killProcesses" :
2596 c
== ARG_ENFORCE_PASSWORD_POLICY
? "enforcePasswordPolicy" :
2597 c
== ARG_AUTO_LOGIN
? "autoLogin" :
2598 c
== ARG_PASSWORD_CHANGE_NOW
? "passwordChangeNow" :
2603 if (isempty(optarg
)) {
2604 r
= drop_from_identity(field
);
2611 r
= parse_boolean(optarg
);
2613 return log_error_errno(r
, "Failed to parse %s boolean: %m", field
);
2615 r
= json_variant_set_field_boolean(&arg_identity_extra
, field
, r
> 0);
2617 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2623 r
= json_variant_set_field_boolean(&arg_identity_extra
, "enforcePasswordPolicy", false);
2625 return log_error_errno(r
, "Failed to set enforcePasswordPolicy field: %m");
2630 if (isempty(optarg
)) {
2631 r
= drop_from_identity("diskSize");
2635 r
= drop_from_identity("diskSizeRelative");
2639 arg_disk_size
= arg_disk_size_relative
= UINT64_MAX
;
2643 r
= parse_permille(optarg
);
2645 r
= parse_size(optarg
, 1024, &arg_disk_size
);
2647 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Disk size '%s' not valid.", optarg
);
2649 r
= drop_from_identity("diskSizeRelative");
2653 r
= json_variant_set_field_unsigned(&arg_identity_extra_this_machine
, "diskSize", arg_disk_size
);
2655 return log_error_errno(r
, "Failed to set diskSize field: %m");
2657 arg_disk_size_relative
= UINT64_MAX
;
2659 /* Normalize to UINT32_MAX == 100% */
2660 arg_disk_size_relative
= (uint64_t) r
* UINT32_MAX
/ 1000U;
2662 r
= drop_from_identity("diskSize");
2666 r
= json_variant_set_field_unsigned(&arg_identity_extra_this_machine
, "diskSizeRelative", arg_disk_size_relative
);
2668 return log_error_errno(r
, "Failed to set diskSizeRelative field: %m");
2670 arg_disk_size
= UINT64_MAX
;
2675 case ARG_ACCESS_MODE
: {
2678 if (isempty(optarg
)) {
2679 r
= drop_from_identity("accessMode");
2686 r
= parse_mode(optarg
, &mode
);
2688 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Access mode '%s' not valid.", optarg
);
2690 r
= json_variant_set_field_unsigned(&arg_identity_extra
, "accessMode", mode
);
2692 return log_error_errno(r
, "Failed to set access mode field: %m");
2697 case ARG_LUKS_DISCARD
:
2698 if (isempty(optarg
)) {
2699 r
= drop_from_identity("luksDiscard");
2706 r
= parse_boolean(optarg
);
2708 return log_error_errno(r
, "Failed to parse --luks-discard= parameter: %s", optarg
);
2710 r
= json_variant_set_field_boolean(&arg_identity_extra
, "luksDiscard", r
);
2712 return log_error_errno(r
, "Failed to set discard field: %m");
2716 case ARG_LUKS_OFFLINE_DISCARD
:
2717 if (isempty(optarg
)) {
2718 r
= drop_from_identity("luksOfflineDiscard");
2725 r
= parse_boolean(optarg
);
2727 return log_error_errno(r
, "Failed to parse --luks-offline-discard= parameter: %s", optarg
);
2729 r
= json_variant_set_field_boolean(&arg_identity_extra
, "luksOfflineDiscard", r
);
2731 return log_error_errno(r
, "Failed to set offline discard field: %m");
2735 case ARG_LUKS_VOLUME_KEY_SIZE
:
2736 case ARG_LUKS_PBKDF_PARALLEL_THREADS
:
2737 case ARG_RATE_LIMIT_BURST
: {
2739 c
== ARG_LUKS_VOLUME_KEY_SIZE
? "luksVolumeKeySize" :
2740 c
== ARG_LUKS_PBKDF_PARALLEL_THREADS
? "luksPbkdfParallelThreads" :
2741 c
== ARG_RATE_LIMIT_BURST
? "rateLimitBurst" : NULL
;
2746 if (isempty(optarg
)) {
2747 r
= drop_from_identity(field
);
2752 r
= safe_atou(optarg
, &n
);
2754 return log_error_errno(r
, "Failed to parse %s parameter: %s", field
, optarg
);
2756 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, n
);
2758 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2766 if (isempty(optarg
)) {
2767 r
= drop_from_identity("umask");
2774 r
= parse_mode(optarg
, &m
);
2776 return log_error_errno(r
, "Failed to parse umask: %m");
2778 r
= json_variant_set_field_integer(&arg_identity_extra
, "umask", m
);
2780 return log_error_errno(r
, "Failed to set umask field: %m");
2785 case ARG_SSH_AUTHORIZED_KEYS
: {
2786 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
2787 _cleanup_(strv_freep
) char **l
= NULL
, **add
= NULL
;
2789 if (isempty(optarg
)) {
2790 r
= drop_from_identity("sshAuthorizedKeys");
2797 if (optarg
[0] == '@') {
2798 _cleanup_fclose_
FILE *f
= NULL
;
2800 /* If prefixed with '@' read from a file */
2802 f
= fopen(optarg
+1, "re");
2804 return log_error_errno(errno
, "Failed to open '%s': %m", optarg
+1);
2807 _cleanup_free_
char *line
= NULL
;
2809 r
= read_line(f
, LONG_LINE_MAX
, &line
);
2811 return log_error_errno(r
, "Failed to read from '%s': %m", optarg
+1);
2821 r
= strv_consume(&add
, TAKE_PTR(line
));
2826 /* Otherwise, assume it's a literal key. Let's do some superficial checks
2827 * before accept it though. */
2829 if (string_has_cc(optarg
, NULL
))
2830 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Authorized key contains control characters, refusing.");
2831 if (optarg
[0] == '#')
2832 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Specified key is a comment?");
2834 add
= strv_new(optarg
);
2839 v
= json_variant_ref(json_variant_by_key(arg_identity_extra_privileged
, "sshAuthorizedKeys"));
2841 r
= json_variant_strv(v
, &l
);
2843 return log_error_errno(r
, "Failed to parse SSH authorized keys list: %m");
2846 r
= strv_extend_strv(&l
, add
, true);
2850 v
= json_variant_unref(v
);
2852 r
= json_variant_new_array_strv(&v
, l
);
2856 r
= json_variant_set_field(&arg_identity_extra_privileged
, "sshAuthorizedKeys", v
);
2858 return log_error_errno(r
, "Failed to set authorized keys: %m");
2863 case ARG_NOT_BEFORE
:
2869 field
= c
== ARG_NOT_BEFORE
? "notBeforeUSec" :
2870 IN_SET(c
, ARG_NOT_AFTER
, 'e') ? "notAfterUSec" : NULL
;
2874 if (isempty(optarg
)) {
2875 r
= drop_from_identity(field
);
2882 /* Note the minor discrepancy regarding -e parsing here: we support that for compat
2883 * reasons, and in the original useradd(8) implementation it accepts dates in the
2884 * format YYYY-MM-DD. Coincidentally, we accept dates formatted like that too, but
2885 * with greater precision. */
2886 r
= parse_timestamp(optarg
, &n
);
2888 return log_error_errno(r
, "Failed to parse %s parameter: %m", field
);
2890 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, n
);
2892 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2896 case ARG_PASSWORD_CHANGE_MIN
:
2897 case ARG_PASSWORD_CHANGE_MAX
:
2898 case ARG_PASSWORD_CHANGE_WARN
:
2899 case ARG_PASSWORD_CHANGE_INACTIVE
: {
2903 field
= c
== ARG_PASSWORD_CHANGE_MIN
? "passwordChangeMinUSec" :
2904 c
== ARG_PASSWORD_CHANGE_MAX
? "passwordChangeMaxUSec" :
2905 c
== ARG_PASSWORD_CHANGE_WARN
? "passwordChangeWarnUSec" :
2906 c
== ARG_PASSWORD_CHANGE_INACTIVE
? "passwordChangeInactiveUSec" :
2911 if (isempty(optarg
)) {
2912 r
= drop_from_identity(field
);
2919 r
= parse_sec(optarg
, &n
);
2921 return log_error_errno(r
, "Failed to parse %s parameter: %m", field
);
2923 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, n
);
2925 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2931 case ARG_LUKS_CIPHER
:
2932 case ARG_LUKS_CIPHER_MODE
:
2933 case ARG_LUKS_PBKDF_TYPE
:
2934 case ARG_LUKS_PBKDF_HASH_ALGORITHM
: {
2937 c
== ARG_STORAGE
? "storage" :
2938 c
== ARG_FS_TYPE
? "fileSystemType" :
2939 c
== ARG_LUKS_CIPHER
? "luksCipher" :
2940 c
== ARG_LUKS_CIPHER_MODE
? "luksCipherMode" :
2941 c
== ARG_LUKS_PBKDF_TYPE
? "luksPbkdfType" :
2942 c
== ARG_LUKS_PBKDF_HASH_ALGORITHM
? "luksPbkdfHashAlgorithm" : NULL
;
2946 if (isempty(optarg
)) {
2947 r
= drop_from_identity(field
);
2954 if (!string_is_safe(optarg
))
2955 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Parameter for %s field not valid: %s", field
, optarg
);
2957 r
= json_variant_set_field_string(
2958 IN_SET(c
, ARG_STORAGE
, ARG_FS_TYPE
) ?
2959 &arg_identity_extra_this_machine
:
2960 &arg_identity_extra
, field
, optarg
);
2962 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2967 case ARG_LUKS_PBKDF_TIME_COST
:
2968 case ARG_RATE_LIMIT_INTERVAL
:
2969 case ARG_STOP_DELAY
: {
2971 c
== ARG_LUKS_PBKDF_TIME_COST
? "luksPbkdfTimeCostUSec" :
2972 c
== ARG_RATE_LIMIT_INTERVAL
? "rateLimitIntervalUSec" :
2973 c
== ARG_STOP_DELAY
? "stopDelayUSec" :
2979 if (isempty(optarg
)) {
2980 r
= drop_from_identity(field
);
2987 r
= parse_sec(optarg
, &t
);
2989 return log_error_errno(r
, "Failed to parse %s field: %s", field
, optarg
);
2991 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, t
);
2993 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2999 const char *p
= optarg
;
3002 r
= drop_from_identity("memberOf");
3010 _cleanup_(json_variant_unrefp
) JsonVariant
*mo
= NULL
;
3011 _cleanup_strv_free_
char **list
= NULL
;
3012 _cleanup_free_
char *word
= NULL
;
3014 r
= extract_first_word(&p
, &word
, ",", 0);
3016 return log_error_errno(r
, "Failed to parse group list: %m");
3020 if (!valid_user_group_name(word
, 0))
3021 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Invalid group name %s.", word
);
3023 mo
= json_variant_ref(json_variant_by_key(arg_identity_extra
, "memberOf"));
3025 r
= json_variant_strv(mo
, &list
);
3027 return log_error_errno(r
, "Failed to parse group list: %m");
3029 r
= strv_extend(&list
, word
);
3036 mo
= json_variant_unref(mo
);
3037 r
= json_variant_new_array_strv(&mo
, list
);
3039 return log_error_errno(r
, "Failed to create group list JSON: %m");
3041 r
= json_variant_set_field(&arg_identity_extra
, "memberOf", mo
);
3043 return log_error_errno(r
, "Failed to update group list: %m");
3049 case ARG_TASKS_MAX
: {
3052 if (isempty(optarg
)) {
3053 r
= drop_from_identity("tasksMax");
3059 r
= safe_atou64(optarg
, &u
);
3061 return log_error_errno(r
, "Failed to parse --tasks-max= parameter: %s", optarg
);
3063 r
= json_variant_set_field_unsigned(&arg_identity_extra
, "tasksMax", u
);
3065 return log_error_errno(r
, "Failed to set tasksMax field: %m");
3070 case ARG_MEMORY_MAX
:
3071 case ARG_MEMORY_HIGH
:
3072 case ARG_LUKS_PBKDF_MEMORY_COST
: {
3074 c
== ARG_MEMORY_MAX
? "memoryMax" :
3075 c
== ARG_MEMORY_HIGH
? "memoryHigh" :
3076 c
== ARG_LUKS_PBKDF_MEMORY_COST
? "luksPbkdfMemoryCost" : NULL
;
3082 if (isempty(optarg
)) {
3083 r
= drop_from_identity(field
);
3089 r
= parse_size(optarg
, 1024, &u
);
3091 return log_error_errno(r
, "Failed to parse %s parameter: %s", field
, optarg
);
3093 r
= json_variant_set_field_unsigned(&arg_identity_extra_this_machine
, field
, u
);
3095 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3100 case ARG_CPU_WEIGHT
:
3101 case ARG_IO_WEIGHT
: {
3102 const char *field
= c
== ARG_CPU_WEIGHT
? "cpuWeight" :
3103 c
== ARG_IO_WEIGHT
? "ioWeight" : NULL
;
3108 if (isempty(optarg
)) {
3109 r
= drop_from_identity(field
);
3115 r
= safe_atou64(optarg
, &u
);
3117 return log_error_errno(r
, "Failed to parse --cpu-weight=/--io-weight= parameter: %s", optarg
);
3119 if (!CGROUP_WEIGHT_IS_OK(u
))
3120 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Weight %" PRIu64
" is out of valid weight range.", u
);
3122 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, u
);
3124 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3129 case ARG_PKCS11_TOKEN_URI
: {
3132 if (streq(optarg
, "list"))
3133 return list_pkcs11_tokens();
3135 /* If --pkcs11-token-uri= is specified we always drop everything old */
3136 FOREACH_STRING(p
, "pkcs11TokenUri", "pkcs11EncryptedKey") {
3137 r
= drop_from_identity(p
);
3142 if (isempty(optarg
)) {
3143 arg_pkcs11_token_uri
= strv_free(arg_pkcs11_token_uri
);
3147 if (streq(optarg
, "auto")) {
3148 _cleanup_free_
char *found
= NULL
;
3150 r
= find_pkcs11_token_auto(&found
);
3153 r
= strv_consume(&arg_pkcs11_token_uri
, TAKE_PTR(found
));
3155 if (!pkcs11_uri_valid(optarg
))
3156 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Not a valid PKCS#11 URI: %s", optarg
);
3158 r
= strv_extend(&arg_pkcs11_token_uri
, optarg
);
3163 strv_uniq(arg_pkcs11_token_uri
);
3167 case ARG_FIDO2_DEVICE
: {
3170 if (streq(optarg
, "list"))
3171 return list_fido2_devices();
3173 FOREACH_STRING(p
, "fido2HmacCredential", "fido2HmacSalt") {
3174 r
= drop_from_identity(p
);
3179 if (isempty(optarg
)) {
3180 arg_fido2_device
= strv_free(arg_fido2_device
);
3184 if (streq(optarg
, "auto")) {
3185 _cleanup_free_
char *found
= NULL
;
3187 r
= find_fido2_auto(&found
);
3191 r
= strv_consume(&arg_fido2_device
, TAKE_PTR(found
));
3193 r
= strv_extend(&arg_fido2_device
, optarg
);
3198 strv_uniq(arg_fido2_device
);
3202 case ARG_RECOVERY_KEY
: {
3205 r
= parse_boolean(optarg
);
3207 return log_error_errno(r
, "Failed to parse --recovery-key= argument: %s", optarg
);
3209 arg_recovery_key
= r
;
3211 FOREACH_STRING(p
, "recoveryKey", "recoveryKeyType") {
3212 r
= drop_from_identity(p
);
3222 arg_json_format_flags
= JSON_FORMAT_PRETTY_AUTO
|JSON_FORMAT_COLOR_AUTO
;
3226 if (streq(optarg
, "pretty")) {
3228 arg_json_format_flags
= JSON_FORMAT_PRETTY
|JSON_FORMAT_COLOR_AUTO
;
3229 } else if (streq(optarg
, "short")) {
3231 arg_json_format_flags
= JSON_FORMAT_NEWLINE
;
3232 } else if (streq(optarg
, "off")) {
3234 arg_json_format_flags
= 0;
3235 } else if (streq(optarg
, "help")) {
3241 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Unknown argument to --json=: %s", optarg
);
3246 if (arg_export_format
== EXPORT_FORMAT_FULL
)
3247 arg_export_format
= EXPORT_FORMAT_STRIPPED
;
3248 else if (arg_export_format
== EXPORT_FORMAT_STRIPPED
)
3249 arg_export_format
= EXPORT_FORMAT_MINIMAL
;
3251 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Specifying -E more than twice is not supported.");
3254 if (arg_json_format_flags
== 0)
3255 arg_json_format_flags
= JSON_FORMAT_PRETTY_AUTO
|JSON_FORMAT_COLOR_AUTO
;
3258 case ARG_EXPORT_FORMAT
:
3259 if (streq(optarg
, "full"))
3260 arg_export_format
= EXPORT_FORMAT_FULL
;
3261 else if (streq(optarg
, "stripped"))
3262 arg_export_format
= EXPORT_FORMAT_STRIPPED
;
3263 else if (streq(optarg
, "minimal"))
3264 arg_export_format
= EXPORT_FORMAT_MINIMAL
;
3265 else if (streq(optarg
, "help")) {
3274 case ARG_AND_RESIZE
:
3275 arg_and_resize
= true;
3278 case ARG_AND_CHANGE_PASSWORD
:
3279 arg_and_change_password
= true;
3286 assert_not_reached("Unhandled option");
3290 if (!strv_isempty(arg_pkcs11_token_uri
) || !strv_isempty(arg_fido2_device
))
3291 arg_and_change_password
= true;
3293 if (arg_disk_size
!= UINT64_MAX
|| arg_disk_size_relative
!= UINT64_MAX
)
3294 arg_and_resize
= true;
3299 static int redirect_bus_mgr(void) {
3302 /* Talk to a different service if that's requested. (The same env var is also understood by homed, so
3303 * that it is relatively easily possible to invoke a second instance of homed for debug purposes and
3304 * have homectl talk to it, without colliding with the host version. This is handy when operating
3305 * from a homed-managed account.) */
3307 suffix
= getenv("SYSTEMD_HOME_DEBUG_SUFFIX");
3309 static BusLocator locator
= {
3310 .path
= "/org/freedesktop/home1",
3311 .interface
= "org.freedesktop.home1.Manager",
3314 /* Yes, we leak this memory, but there's little point to collect this, given that we only do
3315 * this in a debug environment, do it only once, and the string shall live for out entire
3316 * process runtime. */
3318 locator
.destination
= strjoin("org.freedesktop.home1.", suffix
);
3319 if (!locator
.destination
)
3324 bus_mgr
= bus_home_mgr
;
3329 static int run(int argc
, char *argv
[]) {
3330 static const Verb verbs
[] = {
3331 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
3332 { "list", VERB_ANY
, 1, VERB_DEFAULT
, list_homes
},
3333 { "activate", 2, VERB_ANY
, 0, activate_home
},
3334 { "deactivate", 2, VERB_ANY
, 0, deactivate_home
},
3335 { "inspect", VERB_ANY
, VERB_ANY
, 0, inspect_home
},
3336 { "authenticate", VERB_ANY
, VERB_ANY
, 0, authenticate_home
},
3337 { "create", VERB_ANY
, 2, 0, create_home
},
3338 { "remove", 2, VERB_ANY
, 0, remove_home
},
3339 { "update", VERB_ANY
, 2, 0, update_home
},
3340 { "passwd", VERB_ANY
, 2, 0, passwd_home
},
3341 { "resize", 2, 3, 0, resize_home
},
3342 { "lock", 2, VERB_ANY
, 0, lock_home
},
3343 { "unlock", 2, VERB_ANY
, 0, unlock_home
},
3344 { "with", 2, VERB_ANY
, 0, with_home
},
3345 { "lock-all", VERB_ANY
, 1, 0, lock_all_homes
},
3353 r
= redirect_bus_mgr();
3357 r
= parse_argv(argc
, argv
);
3361 return dispatch_verb(argc
, argv
, verbs
, NULL
);
3364 DEFINE_MAIN_FUNCTION(run
);