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 "libcrypt-util.h"
23 #include "locale-util.h"
24 #include "main-func.h"
25 #include "memory-util.h"
26 #include "openssl-util.h"
28 #include "parse-util.h"
29 #include "path-util.h"
30 #include "pkcs11-util.h"
31 #include "pretty-print.h"
32 #include "process-util.h"
33 #include "pwquality-util.h"
34 #include "random-util.h"
35 #include "rlimit-util.h"
36 #include "spawn-polkit-agent.h"
37 #include "terminal-util.h"
38 #include "user-record-show.h"
39 #include "user-record-util.h"
40 #include "user-record.h"
41 #include "user-util.h"
44 static PagerFlags arg_pager_flags
= 0;
45 static bool arg_legend
= true;
46 static bool arg_ask_password
= true;
47 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
48 static const char *arg_host
= NULL
;
49 static const char *arg_identity
= NULL
;
50 static JsonVariant
*arg_identity_extra
= NULL
;
51 static JsonVariant
*arg_identity_extra_privileged
= NULL
;
52 static JsonVariant
*arg_identity_extra_this_machine
= NULL
;
53 static JsonVariant
*arg_identity_extra_rlimits
= NULL
;
54 static char **arg_identity_filter
= NULL
; /* this one is also applied to 'privileged' and 'thisMachine' subobjects */
55 static char **arg_identity_filter_rlimits
= NULL
;
56 static uint64_t arg_disk_size
= UINT64_MAX
;
57 static uint64_t arg_disk_size_relative
= UINT64_MAX
;
58 static char **arg_pkcs11_token_uri
= NULL
;
59 static bool arg_json
= false;
60 static JsonFormatFlags arg_json_format_flags
= 0;
61 static bool arg_and_resize
= false;
62 static bool arg_and_change_password
= false;
64 EXPORT_FORMAT_FULL
, /* export the full record */
65 EXPORT_FORMAT_STRIPPED
, /* strip "state" + "binding", but leave signature in place */
66 EXPORT_FORMAT_MINIMAL
, /* also strip signature */
67 } arg_export_format
= EXPORT_FORMAT_FULL
;
69 STATIC_DESTRUCTOR_REGISTER(arg_identity_extra
, json_variant_unrefp
);
70 STATIC_DESTRUCTOR_REGISTER(arg_identity_extra_this_machine
, json_variant_unrefp
);
71 STATIC_DESTRUCTOR_REGISTER(arg_identity_extra_privileged
, json_variant_unrefp
);
72 STATIC_DESTRUCTOR_REGISTER(arg_identity_extra_rlimits
, json_variant_unrefp
);
73 STATIC_DESTRUCTOR_REGISTER(arg_identity_filter
, strv_freep
);
74 STATIC_DESTRUCTOR_REGISTER(arg_identity_filter_rlimits
, strv_freep
);
75 STATIC_DESTRUCTOR_REGISTER(arg_pkcs11_token_uri
, strv_freep
);
77 static bool identity_properties_specified(void) {
80 !json_variant_is_blank_object(arg_identity_extra
) ||
81 !json_variant_is_blank_object(arg_identity_extra_privileged
) ||
82 !json_variant_is_blank_object(arg_identity_extra_this_machine
) ||
83 !json_variant_is_blank_object(arg_identity_extra_rlimits
) ||
84 !strv_isempty(arg_identity_filter
) ||
85 !strv_isempty(arg_identity_filter_rlimits
) ||
86 !strv_isempty(arg_pkcs11_token_uri
);
89 static int acquire_bus(sd_bus
**bus
) {
97 r
= bus_connect_transport(arg_transport
, arg_host
, false, bus
);
99 return log_error_errno(r
, "Failed to connect to bus: %m");
101 (void) sd_bus_set_allow_interactive_authorization(*bus
, arg_ask_password
);
106 static int list_homes(int argc
, char *argv
[], void *userdata
) {
107 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
108 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
109 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
110 _cleanup_(table_unrefp
) Table
*table
= NULL
;
113 (void) pager_open(arg_pager_flags
);
115 r
= acquire_bus(&bus
);
119 r
= bus_call_method(bus
, bus_home_mgr
, "ListHomes", &error
, &reply
, NULL
);
121 return log_error_errno(r
, "Failed to list homes: %s", bus_error_message(&error
, r
));
123 table
= table_new("name", "uid", "gid", "state", "realname", "home", "shell");
127 r
= sd_bus_message_enter_container(reply
, 'a', "(susussso)");
129 return bus_log_parse_error(r
);
132 const char *name
, *state
, *realname
, *home
, *shell
, *color
;
136 r
= sd_bus_message_read(reply
, "(susussso)", &name
, &uid
, &state
, &gid
, &realname
, &home
, &shell
, NULL
);
138 return bus_log_parse_error(r
);
142 r
= table_add_many(table
,
147 return log_error_errno(r
, "Failed to add row to table: %m");
150 r
= table_add_cell(table
, &cell
, TABLE_STRING
, state
);
152 return log_error_errno(r
, "Failed to add field to table: %m");
154 color
= user_record_state_color(state
);
156 (void) table_set_color(table
, cell
, color
);
158 r
= table_add_many(table
,
159 TABLE_STRING
, strna(empty_to_null(realname
)),
161 TABLE_STRING
, strna(empty_to_null(shell
)));
163 return log_error_errno(r
, "Failed to add row to table: %m");
166 r
= sd_bus_message_exit_container(reply
);
168 return bus_log_parse_error(r
);
170 if (table_get_rows(table
) > 1 || arg_json
) {
171 r
= table_set_sort(table
, (size_t) 0, (size_t) -1);
173 return log_error_errno(r
, "Failed to sort table: %m");
175 table_set_header(table
, arg_legend
);
178 r
= table_print_json(table
, stdout
, arg_json_format_flags
);
180 r
= table_print(table
, NULL
);
182 return log_error_errno(r
, "Failed to show table: %m");
185 if (arg_legend
&& !arg_json
) {
186 if (table_get_rows(table
) > 1)
187 printf("\n%zu home areas listed.\n", table_get_rows(table
) - 1);
189 printf("No home areas.\n");
195 static int acquire_existing_password(const char *user_name
, UserRecord
*hr
, bool emphasize_current
) {
196 _cleanup_(strv_free_erasep
) char **password
= NULL
;
197 _cleanup_free_
char *question
= NULL
;
204 e
= getenv("PASSWORD");
206 /* People really shouldn't use environment variables for passing passwords. We support this
207 * only for testing purposes, and do not document the behaviour, so that people won't
208 * actually use this outside of testing. */
210 r
= user_record_set_password(hr
, STRV_MAKE(e
), true);
212 return log_error_errno(r
, "Failed to store password: %m");
216 if (unsetenv("PASSWORD") < 0)
217 return log_error_errno(errno
, "Failed to unset $PASSWORD: %m");
222 if (asprintf(&question
, emphasize_current
?
223 "Please enter current password for user %s:" :
224 "Please enter password for user %s:",
228 r
= ask_password_auto(question
, "user-home", NULL
, "home-password", USEC_INFINITY
, ASK_PASSWORD_ACCEPT_CACHED
|ASK_PASSWORD_PUSH_CACHE
, &password
);
230 return log_error_errno(r
, "Failed to acquire password: %m");
232 r
= user_record_set_password(hr
, password
, true);
234 return log_error_errno(r
, "Failed to store password: %m");
239 static int acquire_pkcs11_pin(const char *user_name
, UserRecord
*hr
) {
240 _cleanup_(strv_free_erasep
) char **pin
= NULL
;
241 _cleanup_free_
char *question
= NULL
;
250 r
= user_record_set_pkcs11_pin(hr
, STRV_MAKE(e
), false);
252 return log_error_errno(r
, "Failed to store PKCS#11 PIN: %m");
256 if (unsetenv("PIN") < 0)
257 return log_error_errno(errno
, "Failed to unset $PIN: %m");
262 if (asprintf(&question
, "Please enter security token PIN for user %s:", user_name
) < 0)
265 /* We never cache or use cached PINs, since usually there are only very few attempts allowed before the PIN is blocked */
266 r
= ask_password_auto(question
, "user-home", NULL
, "pkcs11-pin", USEC_INFINITY
, 0, &pin
);
268 return log_error_errno(r
, "Failed to acquire security token PIN: %m");
270 r
= user_record_set_pkcs11_pin(hr
, pin
, false);
272 return log_error_errno(r
, "Failed to store security token PIN: %m");
277 static int handle_generic_user_record_error(
278 const char *user_name
,
280 const sd_bus_error
*error
,
282 bool emphasize_current_password
) {
288 if (sd_bus_error_has_name(error
, BUS_ERROR_HOME_ABSENT
))
289 return log_error_errno(SYNTHETIC_ERRNO(EREMOTE
),
290 "Home of user %s is currently absent, please plug in the necessary storage device or backing file system.", user_name
);
292 else if (sd_bus_error_has_name(error
, BUS_ERROR_AUTHENTICATION_LIMIT_HIT
))
293 return log_error_errno(SYNTHETIC_ERRNO(ETOOMANYREFS
),
294 "Too frequent unsuccessful login attempts for user %s, try again later.", user_name
);
296 else if (sd_bus_error_has_name(error
, BUS_ERROR_BAD_PASSWORD
)) {
298 if (!strv_isempty(hr
->password
))
299 log_notice("Password incorrect or not sufficient, please try again.");
301 r
= acquire_existing_password(user_name
, hr
, emphasize_current_password
);
305 } else if (sd_bus_error_has_name(error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
)) {
307 if (strv_isempty(hr
->password
))
308 log_notice("Security token not inserted, please enter password.");
310 log_notice("Password incorrect or not sufficient, and configured security token not inserted, please try again.");
312 r
= acquire_existing_password(user_name
, hr
, emphasize_current_password
);
316 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_PIN_NEEDED
)) {
318 r
= acquire_pkcs11_pin(user_name
, hr
);
322 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_PROTECTED_AUTHENTICATION_PATH_NEEDED
)) {
324 log_notice("Please authenticate physically on security token.");
326 r
= user_record_set_pkcs11_protected_authentication_path_permitted(hr
, true);
328 return log_error_errno(r
, "Failed to set PKCS#11 protected authentication path permitted flag: %m");
330 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_PIN_LOCKED
))
331 return log_error_errno(SYNTHETIC_ERRNO(EPERM
), "Security token PIN is locked, please unlock security token PIN first.");
333 else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_BAD_PIN
)) {
335 log_notice("Security token PIN incorrect, please try again.");
337 r
= acquire_pkcs11_pin(user_name
, hr
);
341 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_BAD_PIN_FEW_TRIES_LEFT
)) {
343 log_notice("Security token PIN incorrect, please try again (only a few tries left!).");
345 r
= acquire_pkcs11_pin(user_name
, hr
);
349 } else if (sd_bus_error_has_name(error
, BUS_ERROR_TOKEN_BAD_PIN_ONE_TRY_LEFT
)) {
351 log_notice("Security token PIN incorrect, please try again (only one try left!).");
353 r
= acquire_pkcs11_pin(user_name
, hr
);
357 return log_error_errno(ret
, "Operation on home %s failed: %s", user_name
, bus_error_message(error
, ret
));
362 static int activate_home(int argc
, char *argv
[], void *userdata
) {
363 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
367 r
= acquire_bus(&bus
);
371 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
372 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
374 secret
= user_record_new();
379 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
380 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
382 r
= bus_message_new_method_call(bus
, &m
, bus_home_mgr
, "ActivateHome");
384 return bus_log_create_error(r
);
386 r
= sd_bus_message_append(m
, "s", *i
);
388 return bus_log_create_error(r
);
390 r
= bus_message_append_secret(m
, secret
);
392 return bus_log_create_error(r
);
394 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
396 r
= handle_generic_user_record_error(*i
, secret
, &error
, r
, false);
411 static int deactivate_home(int argc
, char *argv
[], void *userdata
) {
412 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
416 r
= acquire_bus(&bus
);
420 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
421 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
422 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
424 r
= bus_message_new_method_call(bus
, &m
, bus_home_mgr
, "DeactivateHome");
426 return bus_log_create_error(r
);
428 r
= sd_bus_message_append(m
, "s", *i
);
430 return bus_log_create_error(r
);
432 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
434 log_error_errno(r
, "Failed to deactivate user home: %s", bus_error_message(&error
, r
));
443 static void dump_home_record(UserRecord
*hr
) {
448 if (hr
->incomplete
) {
450 log_warning("Warning: lacking rights to acquire privileged fields of user record of '%s', output incomplete.", hr
->user_name
);
454 _cleanup_(user_record_unrefp
) UserRecord
*stripped
= NULL
;
456 if (arg_export_format
== EXPORT_FORMAT_STRIPPED
)
457 r
= user_record_clone(hr
, USER_RECORD_EXTRACT_EMBEDDED
, &stripped
);
458 else if (arg_export_format
== EXPORT_FORMAT_MINIMAL
)
459 r
= user_record_clone(hr
, USER_RECORD_EXTRACT_SIGNABLE
, &stripped
);
463 log_warning_errno(r
, "Failed to strip user record, ignoring: %m");
467 json_variant_dump(hr
->json
, arg_json_format_flags
, stdout
, NULL
);
469 user_record_show(hr
, true);
472 static char **mangle_user_list(char **list
, char ***ret_allocated
) {
473 _cleanup_free_
char *myself
= NULL
;
476 if (!strv_isempty(list
)) {
477 *ret_allocated
= NULL
;
481 myself
= getusername_malloc();
489 l
[0] = TAKE_PTR(myself
);
496 static int inspect_home(int argc
, char *argv
[], void *userdata
) {
497 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
498 _cleanup_(strv_freep
) char **mangled_list
= NULL
;
502 (void) pager_open(arg_pager_flags
);
504 r
= acquire_bus(&bus
);
508 items
= mangle_user_list(strv_skip(argv
, 1), &mangled_list
);
512 STRV_FOREACH(i
, items
) {
513 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
514 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
515 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
516 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
521 r
= parse_uid(*i
, &uid
);
523 if (!valid_user_group_name(*i
, 0)) {
524 log_error("Invalid user name '%s'.", *i
);
531 r
= bus_call_method(bus
, bus_home_mgr
, "GetUserRecordByName", &error
, &reply
, "s", *i
);
533 r
= bus_call_method(bus
, bus_home_mgr
, "GetUserRecordByUID", &error
, &reply
, "u", (uint32_t) uid
);
536 log_error_errno(r
, "Failed to inspect home: %s", bus_error_message(&error
, r
));
543 r
= sd_bus_message_read(reply
, "sbo", &json
, &incomplete
, NULL
);
545 bus_log_parse_error(r
);
552 r
= json_parse(json
, JSON_PARSE_SENSITIVE
, &v
, NULL
, NULL
);
554 log_error_errno(r
, "Failed to parse JSON identity: %m");
561 hr
= user_record_new();
565 r
= user_record_load(hr
, v
, USER_RECORD_LOAD_REFUSE_SECRET
|USER_RECORD_LOG
);
573 hr
->incomplete
= incomplete
;
574 dump_home_record(hr
);
580 static int authenticate_home(int argc
, char *argv
[], void *userdata
) {
581 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
582 _cleanup_(strv_freep
) char **mangled_list
= NULL
;
586 items
= mangle_user_list(strv_skip(argv
, 1), &mangled_list
);
590 r
= acquire_bus(&bus
);
594 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
596 STRV_FOREACH(i
, items
) {
597 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
599 secret
= user_record_new();
604 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
605 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
607 r
= bus_message_new_method_call(bus
, &m
, bus_home_mgr
, "AuthenticateHome");
609 return bus_log_create_error(r
);
611 r
= sd_bus_message_append(m
, "s", *i
);
613 return bus_log_create_error(r
);
615 r
= bus_message_append_secret(m
, secret
);
617 return bus_log_create_error(r
);
619 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
621 r
= handle_generic_user_record_error(*i
, secret
, &error
, r
, false);
636 static int update_last_change(JsonVariant
**v
, bool with_password
, bool override
) {
643 n
= now(CLOCK_REALTIME
);
645 c
= json_variant_by_key(*v
, "lastChangeUSec");
650 goto update_password
;
652 if (!json_variant_is_unsigned(c
))
653 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastChangeUSec field is not an unsigned integer, refusing.");
655 u
= json_variant_unsigned(c
);
657 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastChangeUSec is from the future, can't update.");
660 r
= json_variant_set_field_unsigned(v
, "lastChangeUSec", n
);
662 return log_error_errno(r
, "Failed to update lastChangeUSec: %m");
668 c
= json_variant_by_key(*v
, "lastPasswordChangeUSec");
675 if (!json_variant_is_unsigned(c
))
676 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastPasswordChangeUSec field is not an unsigned integer, refusing.");
678 u
= json_variant_unsigned(c
);
680 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "lastPasswordChangeUSec is from the future, can't update.");
683 r
= json_variant_set_field_unsigned(v
, "lastPasswordChangeUSec", n
);
685 return log_error_errno(r
, "Failed to update lastPasswordChangeUSec: %m");
690 static int apply_identity_changes(JsonVariant
**_v
) {
691 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
696 v
= json_variant_ref(*_v
);
698 r
= json_variant_filter(&v
, arg_identity_filter
);
700 return log_error_errno(r
, "Failed to filter identity: %m");
702 r
= json_variant_merge(&v
, arg_identity_extra
);
704 return log_error_errno(r
, "Failed to merge identities: %m");
706 if (arg_identity_extra_this_machine
|| !strv_isempty(arg_identity_filter
)) {
707 _cleanup_(json_variant_unrefp
) JsonVariant
*per_machine
= NULL
, *mmid
= NULL
;
708 char mids
[SD_ID128_STRING_MAX
];
711 r
= sd_id128_get_machine(&mid
);
713 return log_error_errno(r
, "Failed to acquire machine ID: %m");
715 r
= json_variant_new_string(&mmid
, sd_id128_to_string(mid
, mids
));
717 return log_error_errno(r
, "Failed to allocate matchMachineId object: %m");
719 per_machine
= json_variant_ref(json_variant_by_key(v
, "perMachine"));
721 _cleanup_(json_variant_unrefp
) JsonVariant
*npm
= NULL
, *add
= NULL
;
722 _cleanup_free_ JsonVariant
**array
= NULL
;
726 if (!json_variant_is_array(per_machine
))
727 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "perMachine field is not an array, refusing.");
729 array
= new(JsonVariant
*, json_variant_elements(per_machine
) + 1);
733 JSON_VARIANT_ARRAY_FOREACH(z
, per_machine
) {
736 if (!json_variant_is_object(z
))
737 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "perMachine entry is not an object, refusing.");
741 u
= json_variant_by_key(z
, "matchMachineId");
745 if (!json_variant_equal(u
, mmid
))
748 r
= json_variant_merge(&add
, z
);
750 return log_error_errno(r
, "Failed to merge perMachine entry: %m");
755 r
= json_variant_filter(&add
, arg_identity_filter
);
757 return log_error_errno(r
, "Failed to filter perMachine: %m");
759 r
= json_variant_merge(&add
, arg_identity_extra_this_machine
);
761 return log_error_errno(r
, "Failed to merge in perMachine fields: %m");
763 if (arg_identity_filter_rlimits
|| arg_identity_extra_rlimits
) {
764 _cleanup_(json_variant_unrefp
) JsonVariant
*rlv
= NULL
;
766 rlv
= json_variant_ref(json_variant_by_key(add
, "resourceLimits"));
768 r
= json_variant_filter(&rlv
, arg_identity_filter_rlimits
);
770 return log_error_errno(r
, "Failed to filter resource limits: %m");
772 r
= json_variant_merge(&rlv
, arg_identity_extra_rlimits
);
774 return log_error_errno(r
, "Failed to set resource limits: %m");
776 if (json_variant_is_blank_object(rlv
)) {
777 r
= json_variant_filter(&add
, STRV_MAKE("resourceLimits"));
779 return log_error_errno(r
, "Failed to drop resource limits field from identity: %m");
781 r
= json_variant_set_field(&add
, "resourceLimits", rlv
);
783 return log_error_errno(r
, "Failed to update resource limits of identity: %m");
787 if (!json_variant_is_blank_object(add
)) {
788 r
= json_variant_set_field(&add
, "matchMachineId", mmid
);
790 return log_error_errno(r
, "Failed to set matchMachineId field: %m");
795 r
= json_variant_new_array(&npm
, array
, i
);
797 return log_error_errno(r
, "Failed to allocate new perMachine array: %m");
799 json_variant_unref(per_machine
);
800 per_machine
= TAKE_PTR(npm
);
802 _cleanup_(json_variant_unrefp
) JsonVariant
*item
= json_variant_ref(arg_identity_extra_this_machine
);
804 if (arg_identity_extra_rlimits
) {
805 r
= json_variant_set_field(&item
, "resourceLimits", arg_identity_extra_rlimits
);
807 return log_error_errno(r
, "Failed to update resource limits of identity: %m");
810 r
= json_variant_set_field(&item
, "matchMachineId", mmid
);
812 return log_error_errno(r
, "Failed to set matchMachineId field: %m");
814 r
= json_variant_append_array(&per_machine
, item
);
816 return log_error_errno(r
, "Failed to append to perMachine array: %m");
819 r
= json_variant_set_field(&v
, "perMachine", per_machine
);
821 return log_error_errno(r
, "Failed to update per machine record: %m");
824 if (arg_identity_extra_privileged
|| arg_identity_filter
) {
825 _cleanup_(json_variant_unrefp
) JsonVariant
*privileged
= NULL
;
827 privileged
= json_variant_ref(json_variant_by_key(v
, "privileged"));
829 r
= json_variant_filter(&privileged
, arg_identity_filter
);
831 return log_error_errno(r
, "Failed to filter identity (privileged part): %m");
833 r
= json_variant_merge(&privileged
, arg_identity_extra_privileged
);
835 return log_error_errno(r
, "Failed to merge identities (privileged part): %m");
837 if (json_variant_is_blank_object(privileged
)) {
838 r
= json_variant_filter(&v
, STRV_MAKE("privileged"));
840 return log_error_errno(r
, "Failed to drop privileged part from identity: %m");
842 r
= json_variant_set_field(&v
, "privileged", privileged
);
844 return log_error_errno(r
, "Failed to update privileged part of identity: %m");
848 if (arg_identity_filter_rlimits
) {
849 _cleanup_(json_variant_unrefp
) JsonVariant
*rlv
= NULL
;
851 rlv
= json_variant_ref(json_variant_by_key(v
, "resourceLimits"));
853 r
= json_variant_filter(&rlv
, arg_identity_filter_rlimits
);
855 return log_error_errno(r
, "Failed to filter resource limits: %m");
857 /* Note that we only filter resource limits here, but don't apply them. We do that in the perMachine section */
859 if (json_variant_is_blank_object(rlv
)) {
860 r
= json_variant_filter(&v
, STRV_MAKE("resourceLimits"));
862 return log_error_errno(r
, "Failed to drop resource limits field from identity: %m");
864 r
= json_variant_set_field(&v
, "resourceLimits", rlv
);
866 return log_error_errno(r
, "Failed to update resource limits of identity: %m");
870 json_variant_unref(*_v
);
876 static int add_disposition(JsonVariant
**v
) {
881 if (json_variant_by_key(*v
, "disposition"))
884 /* Set the disposition to regular, if not configured explicitly */
885 r
= json_variant_set_field_string(v
, "disposition", "regular");
887 return log_error_errno(r
, "Failed to set disposition field: %m");
892 struct pkcs11_callback_data
{
897 static void pkcs11_callback_data_release(struct pkcs11_callback_data
*data
) {
898 erase_and_free(data
->pin_used
);
899 X509_free(data
->cert
);
903 static int pkcs11_callback(
905 CK_SESSION_HANDLE session
,
907 const CK_SLOT_INFO
*slot_info
,
908 const CK_TOKEN_INFO
*token_info
,
912 _cleanup_(erase_and_freep
) char *pin_used
= NULL
;
913 struct pkcs11_callback_data
*data
= userdata
;
914 CK_OBJECT_HANDLE object
;
923 /* Called for every token matching our URI */
925 r
= pkcs11_token_login(m
, session
, slot_id
, token_info
, "home directory operation", "user-home", "pkcs11-pin", UINT64_MAX
, &pin_used
);
929 r
= pkcs11_token_find_x509_certificate(m
, session
, uri
, &object
);
933 r
= pkcs11_token_read_x509_certificate(m
, session
, object
, &data
->cert
);
937 /* Let's read some random data off the token and write it to the kernel pool before we generate our
938 * random key from it. This way we can claim the quality of the RNG is at least as good as the
939 * kernel's and the token's pool */
940 (void) pkcs11_token_acquire_rng(m
, session
);
942 data
->pin_used
= TAKE_PTR(pin_used
);
947 static int acquire_pkcs11_certificate(
950 char **ret_pin_used
) {
953 _cleanup_(pkcs11_callback_data_release
) struct pkcs11_callback_data data
= {};
956 r
= pkcs11_find_token(uri
, pkcs11_callback
, &data
);
957 if (r
== -EAGAIN
) /* pkcs11_find_token() doesn't log about this error, but all others */
958 return log_error_errno(ENXIO
, "Specified PKCS#11 token with URI '%s' not found.", uri
);
962 *ret_cert
= TAKE_PTR(data
.cert
);
963 *ret_pin_used
= TAKE_PTR(data
.pin_used
);
967 return log_error_errno(EOPNOTSUPP
, "PKCS#11 tokens not supported on this build.");
971 static int encrypt_bytes(
973 const void *decrypted_key
,
974 size_t decrypted_key_size
,
975 void **ret_encrypt_key
,
976 size_t *ret_encrypt_key_size
) {
978 _cleanup_(EVP_PKEY_CTX_freep
) EVP_PKEY_CTX
*ctx
= NULL
;
979 _cleanup_free_
void *b
= NULL
;
982 ctx
= EVP_PKEY_CTX_new(pkey
, NULL
);
984 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Failed to allocate public key context");
986 if (EVP_PKEY_encrypt_init(ctx
) <= 0)
987 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Failed to initialize public key context");
989 if (EVP_PKEY_CTX_set_rsa_padding(ctx
, RSA_PKCS1_PADDING
) <= 0)
990 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Failed to configure PKCS#1 padding");
992 if (EVP_PKEY_encrypt(ctx
, NULL
, &l
, decrypted_key
, decrypted_key_size
) <= 0)
993 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Failed to determine encrypted key size");
999 if (EVP_PKEY_encrypt(ctx
, b
, &l
, decrypted_key
, decrypted_key_size
) <= 0)
1000 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Failed to determine encrypted key size");
1002 *ret_encrypt_key
= TAKE_PTR(b
);
1003 *ret_encrypt_key_size
= l
;
1008 static int add_pkcs11_pin(JsonVariant
**v
, const char *pin
) {
1009 _cleanup_(json_variant_unrefp
) JsonVariant
*w
= NULL
, *l
= NULL
;
1010 _cleanup_(strv_free_erasep
) char **pins
= NULL
;
1018 w
= json_variant_ref(json_variant_by_key(*v
, "secret"));
1019 l
= json_variant_ref(json_variant_by_key(w
, "pkcs11Pin"));
1021 r
= json_variant_strv(l
, &pins
);
1023 return log_error_errno(r
, "Failed to convert PIN array: %m");
1025 if (strv_find(pins
, pin
))
1028 r
= strv_extend(&pins
, pin
);
1034 l
= json_variant_unref(l
);
1036 r
= json_variant_new_array_strv(&l
, pins
);
1038 return log_error_errno(r
, "Failed to allocate new PIN array JSON: %m");
1040 json_variant_sensitive(l
);
1042 r
= json_variant_set_field(&w
, "pkcs11Pin", l
);
1044 return log_error_errno(r
, "Failed to update PIN field: %m");
1046 r
= json_variant_set_field(v
, "secret", w
);
1048 return log_error_errno(r
, "Failed to update secret object: %m");
1053 static int add_pkcs11_encrypted_key(
1056 const void *encrypted_key
, size_t encrypted_key_size
,
1057 const void *decrypted_key
, size_t decrypted_key_size
) {
1059 _cleanup_(json_variant_unrefp
) JsonVariant
*l
= NULL
, *w
= NULL
, *e
= NULL
;
1060 _cleanup_(erase_and_freep
) char *base64_encoded
= NULL
;
1061 _cleanup_free_
char *salt
= NULL
;
1062 struct crypt_data cd
= {};
1068 assert(encrypted_key
);
1069 assert(encrypted_key_size
> 0);
1070 assert(decrypted_key
);
1071 assert(decrypted_key_size
> 0);
1073 r
= make_salt(&salt
);
1075 return log_error_errno(r
, "Failed to generate salt: %m");
1077 /* Before using UNIX hashing on the supplied key we base64 encode it, since crypt_r() and friends
1078 * expect a NUL terminated string, and we use a binary key */
1079 r
= base64mem(decrypted_key
, decrypted_key_size
, &base64_encoded
);
1081 return log_error_errno(r
, "Failed to base64 encode secret key: %m");
1084 k
= crypt_r(base64_encoded
, salt
, &cd
);
1086 return log_error_errno(errno_or_else(EINVAL
), "Failed to UNIX hash secret key: %m");
1088 r
= json_build(&e
, JSON_BUILD_OBJECT(
1089 JSON_BUILD_PAIR("uri", JSON_BUILD_STRING(uri
)),
1090 JSON_BUILD_PAIR("data", JSON_BUILD_BASE64(encrypted_key
, encrypted_key_size
)),
1091 JSON_BUILD_PAIR("hashedPassword", JSON_BUILD_STRING(k
))));
1093 return log_error_errno(r
, "Failed to build encrypted JSON key object: %m");
1095 w
= json_variant_ref(json_variant_by_key(*v
, "privileged"));
1096 l
= json_variant_ref(json_variant_by_key(w
, "pkcs11EncryptedKey"));
1098 r
= json_variant_append_array(&l
, e
);
1100 return log_error_errno(r
, "Failed append PKCS#11 encrypted key: %m");
1102 r
= json_variant_set_field(&w
, "pkcs11EncryptedKey", l
);
1104 return log_error_errno(r
, "Failed to set PKCS#11 encrypted key: %m");
1106 r
= json_variant_set_field(v
, "privileged", w
);
1108 return log_error_errno(r
, "Failed to update privileged field: %m");
1113 static int add_pkcs11_token_uri(JsonVariant
**v
, const char *uri
) {
1114 _cleanup_(json_variant_unrefp
) JsonVariant
*w
= NULL
;
1115 _cleanup_strv_free_
char **l
= NULL
;
1121 w
= json_variant_ref(json_variant_by_key(*v
, "pkcs11TokenUri"));
1123 r
= json_variant_strv(w
, &l
);
1125 return log_error_errno(r
, "Failed to parse PKCS#11 token list: %m");
1127 if (strv_contains(l
, uri
))
1131 r
= strv_extend(&l
, uri
);
1135 w
= json_variant_unref(w
);
1136 r
= json_variant_new_array_strv(&w
, l
);
1138 return log_error_errno(r
, "Failed to create PKCS#11 token URI JSON: %m");
1140 r
= json_variant_set_field(v
, "pkcs11TokenUri", w
);
1142 return log_error_errno(r
, "Failed to update PKCS#11 token URI list: %m");
1147 static int add_pkcs11_key_data(JsonVariant
**v
, const char *uri
) {
1148 _cleanup_(erase_and_freep
) void *decrypted_key
= NULL
, *encrypted_key
= NULL
;
1149 _cleanup_(erase_and_freep
) char *pin
= NULL
;
1150 size_t decrypted_key_size
, encrypted_key_size
;
1151 _cleanup_(X509_freep
) X509
*cert
= NULL
;
1159 r
= acquire_pkcs11_certificate(uri
, &cert
, &pin
);
1163 pkey
= X509_get0_pubkey(cert
);
1165 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Failed to extract public key from X.509 certificate.");
1167 if (EVP_PKEY_base_id(pkey
) != EVP_PKEY_RSA
)
1168 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "X.509 certificate does not refer to RSA key.");
1170 rsa
= EVP_PKEY_get0_RSA(pkey
);
1172 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Failed to acquire RSA public key from X.509 certificate.");
1174 bits
= RSA_bits(rsa
);
1175 log_debug("Bits in RSA key: %i", bits
);
1177 /* We use PKCS#1 padding for the RSA cleartext, hence let's leave some extra space for it, hence only
1178 * generate a random key half the size of the RSA length */
1179 decrypted_key_size
= bits
/ 8 / 2;
1181 if (decrypted_key_size
< 1)
1182 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Uh, RSA key size too short?");
1184 log_debug("Generating %zu bytes random key.", decrypted_key_size
);
1186 decrypted_key
= malloc(decrypted_key_size
);
1190 r
= genuine_random_bytes(decrypted_key
, decrypted_key_size
, RANDOM_BLOCK
);
1192 return log_error_errno(r
, "Failed to generate random key: %m");
1194 r
= encrypt_bytes(pkey
, decrypted_key
, decrypted_key_size
, &encrypted_key
, &encrypted_key_size
);
1196 return log_error_errno(r
, "Failed to encrypt key: %m");
1198 /* Add the token URI to the public part of the record. */
1199 r
= add_pkcs11_token_uri(v
, uri
);
1203 /* Include the encrypted version of the random key we just generated in the privileged part of the record */
1204 r
= add_pkcs11_encrypted_key(
1207 encrypted_key
, encrypted_key_size
,
1208 decrypted_key
, decrypted_key_size
);
1212 /* If we acquired the PIN also include it in the secret section of the record, so that systemd-homed
1213 * can use it if it needs to, given that it likely needs to decrypt the key again to pass to LUKS or
1215 r
= add_pkcs11_pin(v
, pin
);
1222 static int acquire_new_home_record(UserRecord
**ret
) {
1223 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
1224 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
1231 unsigned line
, column
;
1233 r
= json_parse_file(
1234 streq(arg_identity
, "-") ? stdin
: NULL
,
1235 streq(arg_identity
, "-") ? "<stdin>" : arg_identity
, JSON_PARSE_SENSITIVE
, &v
, &line
, &column
);
1237 return log_error_errno(r
, "Failed to parse identity at %u:%u: %m", line
, column
);
1240 r
= apply_identity_changes(&v
);
1244 r
= add_disposition(&v
);
1248 STRV_FOREACH(i
, arg_pkcs11_token_uri
) {
1249 r
= add_pkcs11_key_data(&v
, *i
);
1254 r
= update_last_change(&v
, true, false);
1259 json_variant_dump(v
, JSON_FORMAT_PRETTY
, NULL
, NULL
);
1261 hr
= user_record_new();
1265 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
);
1269 *ret
= TAKE_PTR(hr
);
1273 static int acquire_new_password(
1274 const char *user_name
,
1285 e
= getenv("NEWPASSWORD");
1287 /* As above, this is not for use, just for testing */
1289 r
= user_record_set_password(hr
, STRV_MAKE(e
), /* prepend = */ false);
1291 return log_error_errno(r
, "Failed to store password: %m");
1295 if (unsetenv("NEWPASSWORD") < 0)
1296 return log_error_errno(errno
, "Failed to unset $NEWPASSWORD: %m");
1302 (void) suggest_passwords();
1305 _cleanup_(strv_free_erasep
) char **first
= NULL
, **second
= NULL
;
1306 _cleanup_free_
char *question
= NULL
;
1309 return log_error_errno(SYNTHETIC_ERRNO(ENOKEY
), "Too many attempts, giving up:");
1311 if (asprintf(&question
, "Please enter new password for user %s:", user_name
) < 0)
1314 r
= ask_password_auto(question
, "user-home", NULL
, "home-password", USEC_INFINITY
, 0, &first
);
1316 return log_error_errno(r
, "Failed to acquire password: %m");
1318 question
= mfree(question
);
1319 if (asprintf(&question
, "Please enter new password for user %s (repeat):", user_name
) < 0)
1322 r
= ask_password_auto(question
, "user-home", NULL
, "home-password", USEC_INFINITY
, 0, &second
);
1324 return log_error_errno(r
, "Failed to acquire password: %m");
1326 if (strv_equal(first
, second
)) {
1327 r
= user_record_set_password(hr
, first
, /* prepend = */ false);
1329 return log_error_errno(r
, "Failed to store password: %m");
1334 log_error("Password didn't match, try again.");
1338 static int create_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_strv_free_
char **original_hashed_passwords
= NULL
;
1344 r
= acquire_bus(&bus
);
1348 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1351 /* If a username was specified, use it */
1353 if (valid_user_group_name(argv
[1], 0))
1354 r
= json_variant_set_field_string(&arg_identity_extra
, "userName", argv
[1]);
1356 _cleanup_free_
char *un
= NULL
, *rr
= NULL
;
1358 /* Before we consider the user name invalid, let's check if we can split it? */
1359 r
= split_user_name_realm(argv
[1], &un
, &rr
);
1361 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User name '%s' is not valid: %m", argv
[1]);
1364 r
= json_variant_set_field_string(&arg_identity_extra
, "realm", rr
);
1366 return log_error_errno(r
, "Failed to set realm field: %m");
1369 r
= json_variant_set_field_string(&arg_identity_extra
, "userName", un
);
1372 return log_error_errno(r
, "Failed to set userName field: %m");
1374 /* If neither a username nor an identity have been specified we cannot operate. */
1376 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User name required.");
1379 r
= acquire_new_home_record(&hr
);
1383 /* Remember the original hashed passwords before we add our own, so that we can return to them later,
1384 * should the entered password turn out not to be acceptable. */
1385 original_hashed_passwords
= strv_copy(hr
->hashed_password
);
1386 if (!original_hashed_passwords
)
1389 /* If the JSON record carries no plain text password, then let's query it manually. */
1390 if (!hr
->password
) {
1392 if (strv_isempty(hr
->hashed_password
)) {
1393 /* No regular (i.e. non-PKCS#11) hashed passwords set in the record, let's fix that. */
1394 r
= acquire_new_password(hr
->user_name
, hr
, /* suggest = */ true);
1398 r
= user_record_make_hashed_password(hr
, hr
->password
, /* extend = */ true);
1400 return log_error_errno(r
, "Failed to hash password: %m");
1402 /* There's a hash password set in the record, acquire the unhashed version of it. */
1403 r
= acquire_existing_password(hr
->user_name
, hr
, false);
1409 if (hr
->enforce_password_policy
== 0) {
1410 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1412 /* If password quality enforcement is disabled, let's at least warn client side */
1414 r
= quality_check_password(hr
, hr
, &error
);
1416 log_warning_errno(r
, "Specified password does not pass quality checks (%s), proceeding anyway.", bus_error_message(&error
, r
));
1420 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1421 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1422 _cleanup_(erase_and_freep
) char *formatted
= NULL
;
1424 r
= json_variant_format(hr
->json
, 0, &formatted
);
1428 r
= bus_message_new_method_call(bus
, &m
, bus_home_mgr
, "CreateHome");
1430 return bus_log_create_error(r
);
1432 (void) sd_bus_message_sensitive(m
);
1434 r
= sd_bus_message_append(m
, "s", formatted
);
1436 return bus_log_create_error(r
);
1438 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1440 if (!sd_bus_error_has_name(&error
, BUS_ERROR_LOW_PASSWORD_QUALITY
))
1441 return log_error_errno(r
, "Failed to create user home: %s", bus_error_message(&error
, r
));
1443 log_error_errno(r
, "%s", bus_error_message(&error
, r
));
1444 log_info("(Use --enforce-password-policy=no to turn off password quality checks for this account.)");
1448 r
= user_record_set_hashed_password(hr
, original_hashed_passwords
);
1452 r
= acquire_new_password(hr
->user_name
, hr
, /* suggest = */ false);
1456 r
= user_record_make_hashed_password(hr
, hr
->password
, /* extend = */ true);
1458 return log_error_errno(r
, "Failed to hash passwords: %m");
1464 static int remove_home(int argc
, char *argv
[], void *userdata
) {
1465 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1469 r
= acquire_bus(&bus
);
1473 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1475 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
1476 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1477 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1479 r
= bus_message_new_method_call(bus
, &m
, bus_home_mgr
, "RemoveHome");
1481 return bus_log_create_error(r
);
1483 r
= sd_bus_message_append(m
, "s", *i
);
1485 return bus_log_create_error(r
);
1487 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1489 log_error_errno(r
, "Failed to remove home: %s", bus_error_message(&error
, r
));
1498 static int acquire_updated_home_record(
1500 const char *username
,
1503 _cleanup_(json_variant_unrefp
) JsonVariant
*json
= NULL
;
1504 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
1511 unsigned line
, column
;
1514 r
= json_parse_file(
1515 streq(arg_identity
, "-") ? stdin
: NULL
,
1516 streq(arg_identity
, "-") ? "<stdin>" : arg_identity
, JSON_PARSE_SENSITIVE
, &json
, &line
, &column
);
1518 return log_error_errno(r
, "Failed to parse identity at %u:%u: %m", line
, column
);
1520 un
= json_variant_by_key(json
, "userName");
1522 if (!json_variant_is_string(un
) || (username
&& !streq(json_variant_string(un
), username
)))
1523 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User name specified on command line and in JSON record do not match.");
1526 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "No username specified.");
1528 r
= json_variant_set_field_string(&arg_identity_extra
, "userName", username
);
1530 return log_error_errno(r
, "Failed to set userName field: %m");
1534 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1535 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1539 if (!identity_properties_specified())
1540 return log_error_errno(SYNTHETIC_ERRNO(EALREADY
), "No field to change specified.");
1542 r
= bus_call_method(bus
, bus_home_mgr
, "GetUserRecordByName", &error
, &reply
, "s", username
);
1544 return log_error_errno(r
, "Failed to acquire user home record: %s", bus_error_message(&error
, r
));
1546 r
= sd_bus_message_read(reply
, "sbo", &text
, &incomplete
, NULL
);
1548 return bus_log_parse_error(r
);
1551 return log_error_errno(SYNTHETIC_ERRNO(EACCES
), "Lacking rights to acquire user record including privileged metadata, can't update record.");
1553 r
= json_parse(text
, JSON_PARSE_SENSITIVE
, &json
, NULL
, NULL
);
1555 return log_error_errno(r
, "Failed to parse JSON identity: %m");
1557 reply
= sd_bus_message_unref(reply
);
1559 r
= json_variant_filter(&json
, STRV_MAKE("binding", "status", "signature"));
1561 return log_error_errno(r
, "Failed to strip binding and status from record to update: %m");
1564 r
= apply_identity_changes(&json
);
1568 STRV_FOREACH(i
, arg_pkcs11_token_uri
) {
1569 r
= add_pkcs11_key_data(&json
, *i
);
1574 /* If the user supplied a full record, then add in lastChange, but do not override. Otherwise always
1576 r
= update_last_change(&json
, !!arg_pkcs11_token_uri
, !arg_identity
);
1581 json_variant_dump(json
, JSON_FORMAT_PRETTY
, NULL
, NULL
);
1583 hr
= user_record_new();
1587 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
);
1591 *ret
= TAKE_PTR(hr
);
1595 static int update_home(int argc
, char *argv
[], void *userdata
) {
1596 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1597 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
1598 _cleanup_free_
char *buffer
= NULL
;
1599 const char *username
;
1604 else if (!arg_identity
) {
1605 buffer
= getusername_malloc();
1613 r
= acquire_bus(&bus
);
1617 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1619 r
= acquire_updated_home_record(bus
, username
, &hr
);
1624 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1625 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1626 _cleanup_free_
char *formatted
= NULL
;
1628 r
= bus_message_new_method_call(bus
, &m
, bus_home_mgr
, "UpdateHome");
1630 return bus_log_create_error(r
);
1632 r
= json_variant_format(hr
->json
, 0, &formatted
);
1636 (void) sd_bus_message_sensitive(m
);
1638 r
= sd_bus_message_append(m
, "s", formatted
);
1640 return bus_log_create_error(r
);
1642 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1644 if (arg_and_change_password
&&
1645 sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1646 /* In the generic handler we'd ask for a password in this case, but when
1647 * changing passwords that's not sufficient, as we need to acquire all keys
1649 return log_error_errno(r
, "Security token not inserted, refusing.");
1651 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1658 /* Also sync down disk size to underlying LUKS/fscrypt/quota */
1659 while (arg_and_resize
) {
1660 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1661 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1663 log_debug("Resizing");
1665 r
= bus_message_new_method_call(bus
, &m
, bus_home_mgr
, "ResizeHome");
1667 return bus_log_create_error(r
);
1669 /* Specify UINT64_MAX as size, in which case the underlying disk size will just be synced */
1670 r
= sd_bus_message_append(m
, "st", hr
->user_name
, UINT64_MAX
);
1672 return bus_log_create_error(r
);
1674 r
= bus_message_append_secret(m
, hr
);
1676 return bus_log_create_error(r
);
1678 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1680 if (arg_and_change_password
&&
1681 sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1682 return log_error_errno(r
, "Security token not inserted, refusing.");
1684 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1691 /* Also sync down passwords to underlying LUKS/fscrypt */
1692 while (arg_and_change_password
) {
1693 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1694 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1696 log_debug("Propagating password");
1698 r
= bus_message_new_method_call(bus
, &m
, bus_home_mgr
, "ChangePasswordHome");
1700 return bus_log_create_error(r
);
1702 /* Specify an empty new secret, in which case the underlying LUKS/fscrypt password will just be synced */
1703 r
= sd_bus_message_append(m
, "ss", hr
->user_name
, "{}");
1705 return bus_log_create_error(r
);
1707 r
= bus_message_append_secret(m
, hr
);
1709 return bus_log_create_error(r
);
1711 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1713 if (sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1714 return log_error_errno(r
, "Security token not inserted, refusing.");
1716 r
= handle_generic_user_record_error(hr
->user_name
, hr
, &error
, r
, false);
1726 static int passwd_home(int argc
, char *argv
[], void *userdata
) {
1727 _cleanup_(user_record_unrefp
) UserRecord
*old_secret
= NULL
, *new_secret
= NULL
;
1728 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1729 _cleanup_free_
char *buffer
= NULL
;
1730 const char *username
;
1733 if (arg_pkcs11_token_uri
)
1734 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "To change the PKCS#11 security token use 'homectl update --pkcs11-token-uri=…'.");
1735 if (identity_properties_specified())
1736 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "The 'passwd' verb does not permit changing other record properties at the same time.");
1741 buffer
= getusername_malloc();
1748 r
= acquire_bus(&bus
);
1752 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1754 old_secret
= user_record_new();
1758 new_secret
= user_record_new();
1762 r
= acquire_new_password(username
, new_secret
, /* suggest = */ true);
1767 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1768 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1770 r
= bus_message_new_method_call(bus
, &m
, bus_home_mgr
, "ChangePasswordHome");
1772 return bus_log_create_error(r
);
1774 r
= sd_bus_message_append(m
, "s", username
);
1776 return bus_log_create_error(r
);
1778 r
= bus_message_append_secret(m
, new_secret
);
1780 return bus_log_create_error(r
);
1782 r
= bus_message_append_secret(m
, old_secret
);
1784 return bus_log_create_error(r
);
1786 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1788 if (sd_bus_error_has_name(&error
, BUS_ERROR_LOW_PASSWORD_QUALITY
)) {
1790 log_error_errno(r
, "%s", bus_error_message(&error
, r
));
1792 r
= acquire_new_password(username
, new_secret
, /* suggest = */ false);
1794 } else if (sd_bus_error_has_name(&error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
))
1796 /* In the generic handler we'd ask for a password in this case, but when
1797 * changing passwords that's not sufficeint, as we need to acquire all keys
1799 return log_error_errno(r
, "Security token not inserted, refusing.");
1801 r
= handle_generic_user_record_error(username
, old_secret
, &error
, r
, true);
1811 static int resize_home(int argc
, char *argv
[], void *userdata
) {
1812 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1813 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
1814 uint64_t ds
= UINT64_MAX
;
1817 r
= acquire_bus(&bus
);
1821 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1823 if (arg_disk_size_relative
!= UINT64_MAX
||
1824 (argc
> 2 && parse_percent(argv
[2]) >= 0))
1825 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
1826 "Relative disk size specification currently not supported when resizing.");
1829 r
= parse_size(argv
[2], 1024, &ds
);
1831 return log_error_errno(r
, "Failed to parse disk size parameter: %s", argv
[2]);
1834 if (arg_disk_size
!= UINT64_MAX
) {
1835 if (ds
!= UINT64_MAX
&& ds
!= arg_disk_size
)
1836 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Disk size specified twice and doesn't match, refusing.");
1841 secret
= user_record_new();
1846 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1847 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1849 r
= bus_message_new_method_call(bus
, &m
, bus_home_mgr
, "ResizeHome");
1851 return bus_log_create_error(r
);
1853 r
= sd_bus_message_append(m
, "st", argv
[1], ds
);
1855 return bus_log_create_error(r
);
1857 r
= bus_message_append_secret(m
, secret
);
1859 return bus_log_create_error(r
);
1861 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1863 r
= handle_generic_user_record_error(argv
[1], secret
, &error
, r
, false);
1873 static int lock_home(int argc
, char *argv
[], void *userdata
) {
1874 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1878 r
= acquire_bus(&bus
);
1882 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
1883 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1884 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1886 r
= bus_message_new_method_call(bus
, &m
, bus_home_mgr
, "LockHome");
1888 return bus_log_create_error(r
);
1890 r
= sd_bus_message_append(m
, "s", *i
);
1892 return bus_log_create_error(r
);
1894 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1896 log_error_errno(r
, "Failed to lock home: %s", bus_error_message(&error
, r
));
1905 static int unlock_home(int argc
, char *argv
[], void *userdata
) {
1906 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1910 r
= acquire_bus(&bus
);
1914 STRV_FOREACH(i
, strv_skip(argv
, 1)) {
1915 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
1917 secret
= user_record_new();
1922 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1923 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1925 r
= bus_message_new_method_call(bus
, &m
, bus_home_mgr
, "UnlockHome");
1927 return bus_log_create_error(r
);
1929 r
= sd_bus_message_append(m
, "s", *i
);
1931 return bus_log_create_error(r
);
1933 r
= bus_message_append_secret(m
, secret
);
1935 return bus_log_create_error(r
);
1937 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
1939 r
= handle_generic_user_record_error(argv
[1], secret
, &error
, r
, false);
1954 static int with_home(int argc
, char *argv
[], void *userdata
) {
1955 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1956 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1957 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1958 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
1959 _cleanup_close_
int acquired_fd
= -1;
1960 _cleanup_strv_free_
char **cmdline
= NULL
;
1965 r
= acquire_bus(&bus
);
1970 _cleanup_free_
char *shell
= NULL
;
1972 /* If no command is specified, spawn a shell */
1973 r
= get_shell(&shell
);
1975 return log_error_errno(r
, "Failed to acquire shell: %m");
1977 cmdline
= strv_new(shell
);
1979 cmdline
= strv_copy(argv
+ 2);
1983 secret
= user_record_new();
1988 r
= bus_message_new_method_call(bus
, &m
, bus_home_mgr
, "AcquireHome");
1990 return bus_log_create_error(r
);
1992 r
= sd_bus_message_append(m
, "s", argv
[1]);
1994 return bus_log_create_error(r
);
1996 r
= bus_message_append_secret(m
, secret
);
1998 return bus_log_create_error(r
);
2000 r
= sd_bus_message_append(m
, "b", /* please_suspend = */ getenv_bool("SYSTEMD_PLEASE_SUSPEND_HOME") > 0);
2002 return bus_log_create_error(r
);
2004 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, &reply
);
2005 m
= sd_bus_message_unref(m
);
2007 r
= handle_generic_user_record_error(argv
[1], secret
, &error
, r
, false);
2011 sd_bus_error_free(&error
);
2015 r
= sd_bus_message_read(reply
, "h", &fd
);
2017 return bus_log_parse_error(r
);
2019 acquired_fd
= fcntl(fd
, F_DUPFD_CLOEXEC
, 3);
2020 if (acquired_fd
< 0)
2021 return log_error_errno(errno
, "Failed to duplicate acquired fd: %m");
2023 reply
= sd_bus_message_unref(reply
);
2028 r
= bus_call_method(bus
, bus_home_mgr
, "GetHomeByName", &error
, &reply
, "s", argv
[1]);
2030 return log_error_errno(r
, "Failed to inspect home: %s", bus_error_message(&error
, r
));
2032 r
= sd_bus_message_read(reply
, "usussso", NULL
, NULL
, NULL
, NULL
, &home
, NULL
, NULL
);
2034 return bus_log_parse_error(r
);
2036 r
= safe_fork("(with)", FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_DEATHSIG
|FORK_LOG
|FORK_RLIMIT_NOFILE_SAFE
|FORK_REOPEN_LOG
, &pid
);
2040 if (chdir(home
) < 0) {
2041 log_error_errno(errno
, "Failed to change to directory %s: %m", home
);
2045 execvp(cmdline
[0], cmdline
);
2046 log_error_errno(errno
, "Failed to execute %s: %m", cmdline
[0]);
2050 ret
= wait_for_terminate_and_check(cmdline
[0], pid
, WAIT_LOG_ABNORMAL
);
2052 /* Close the fd that pings the home now. */
2053 acquired_fd
= safe_close(acquired_fd
);
2055 r
= bus_message_new_method_call(bus
, &m
, bus_home_mgr
, "ReleaseHome");
2057 return bus_log_create_error(r
);
2059 r
= sd_bus_message_append(m
, "s", argv
[1]);
2061 return bus_log_create_error(r
);
2063 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
2065 if (sd_bus_error_has_name(&error
, BUS_ERROR_HOME_BUSY
))
2066 log_notice("Not deactivating home directory of %s, as it is still used.", argv
[1]);
2068 return log_error_errno(r
, "Failed to release user home: %s", bus_error_message(&error
, r
));
2074 static int lock_all_homes(int argc
, char *argv
[], void *userdata
) {
2075 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2076 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2077 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
2080 r
= acquire_bus(&bus
);
2084 r
= bus_message_new_method_call(bus
, &m
, bus_home_mgr
, "LockAllHomes");
2086 return bus_log_create_error(r
);
2088 r
= sd_bus_call(bus
, m
, HOME_SLOW_BUS_CALL_TIMEOUT_USEC
, &error
, NULL
);
2090 return log_error_errno(r
, "Failed to lock home: %s", bus_error_message(&error
, r
));
2095 static int drop_from_identity(const char *field
) {
2100 /* If we are called to update an identity record and drop some field, let's keep track of what to
2101 * remove from the old record */
2102 r
= strv_extend(&arg_identity_filter
, field
);
2106 /* Let's also drop the field if it was previously set to a new value on the same command line */
2107 r
= json_variant_filter(&arg_identity_extra
, STRV_MAKE(field
));
2109 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
2111 r
= json_variant_filter(&arg_identity_extra_this_machine
, STRV_MAKE(field
));
2113 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
2115 r
= json_variant_filter(&arg_identity_extra_privileged
, STRV_MAKE(field
));
2117 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
2122 static int help(int argc
, char *argv
[], void *userdata
) {
2123 _cleanup_free_
char *link
= NULL
;
2126 (void) pager_open(arg_pager_flags
);
2128 r
= terminal_urlify_man("homectl", "1", &link
);
2132 printf("%1$s [OPTIONS...] COMMAND ...\n\n"
2133 "%2$sCreate, manipulate or inspect home directories.%3$s\n"
2134 "\n%4$sCommands:%5$s\n"
2135 " list List home areas\n"
2136 " activate USER… Activate a home area\n"
2137 " deactivate USER… Deactivate a home area\n"
2138 " inspect USER… Inspect a home area\n"
2139 " authenticate USER… Authenticate a home area\n"
2140 " create USER Create a home area\n"
2141 " remove USER… Remove a home area\n"
2142 " update USER Update a home area\n"
2143 " passwd USER Change password of a home area\n"
2144 " resize USER SIZE Resize a home area\n"
2145 " lock USER… Temporarily lock an active home area\n"
2146 " unlock USER… Unlock a temporarily locked home area\n"
2147 " lock-all Lock all suitable home areas\n"
2148 " with USER [COMMAND…] Run shell or command with access to a home area\n"
2149 "\n%4$sOptions:%5$s\n"
2150 " -h --help Show this help\n"
2151 " --version Show package version\n"
2152 " --no-pager Do not pipe output into a pager\n"
2153 " --no-legend Do not show the headers and footers\n"
2154 " --no-ask-password Do not ask for system passwords\n"
2155 " -H --host=[USER@]HOST Operate on remote host\n"
2156 " -M --machine=CONTAINER Operate on local container\n"
2157 " --identity=PATH Read JSON identity from file\n"
2158 " --json=FORMAT Output inspection data in JSON (takes one of\n"
2159 " pretty, short, off)\n"
2160 " -j Equivalent to --json=pretty (on TTY) or\n"
2161 " --json=short (otherwise)\n"
2162 " --export-format= Strip JSON inspection data (full, stripped,\n"
2164 " -E When specified once equals -j --export-format=\n"
2165 " stripped, when specified twice equals\n"
2166 " -j --export-format=minimal\n"
2167 "\n%4$sGeneral User Record Properties:%5$s\n"
2168 " -c --real-name=REALNAME Real name for user\n"
2169 " --realm=REALM Realm to create user in\n"
2170 " --email-address=EMAIL Email address for user\n"
2171 " --location=LOCATION Set location of user on earth\n"
2172 " --icon-name=NAME Icon name for user\n"
2173 " -d --home-dir=PATH Home directory\n"
2174 " -u --uid=UID Numeric UID for user\n"
2175 " -G --member-of=GROUP Add user to group\n"
2176 " --skel=PATH Skeleton directory to use\n"
2177 " --shell=PATH Shell for account\n"
2178 " --setenv=VARIABLE=VALUE Set an environment variable at log-in\n"
2179 " --timezone=TIMEZONE Set a time-zone\n"
2180 " --language=LOCALE Set preferred language\n"
2181 " --ssh-authorized-keys=KEYS\n"
2182 " Specify SSH public keys\n"
2183 " --pkcs11-token-uri=URI URI to PKCS#11 security token containing\n"
2184 " private key and matching X.509 certificate\n"
2185 "\n%4$sAccount Management User Record Properties:%5$s\n"
2186 " --locked=BOOL Set locked account state\n"
2187 " --not-before=TIMESTAMP Do not allow logins before\n"
2188 " --not-after=TIMESTAMP Do not allow logins after\n"
2189 " --rate-limit-interval=SECS\n"
2190 " Login rate-limit interval in seconds\n"
2191 " --rate-limit-burst=NUMBER\n"
2192 " Login rate-limit attempts per interval\n"
2193 "\n%4$sPassword Policy User Record Properties:%5$s\n"
2194 " --password-hint=HINT Set Password hint\n"
2195 " --enforce-password-policy=BOOL\n"
2196 " Control whether to enforce system's password\n"
2197 " policy for this user\n"
2198 " -P Equivalent to --enforce-password-password=no\n"
2199 " --password-change-now=BOOL\n"
2200 " Require the password to be changed on next login\n"
2201 " --password-change-min=TIME\n"
2202 " Require minimum time between password changes\n"
2203 " --password-change-max=TIME\n"
2204 " Require maximum time between password changes\n"
2205 " --password-change-warn=TIME\n"
2206 " How much time to warn before password expiry\n"
2207 " --password-change-inactive=TIME\n"
2208 " How much time to block password after expiry\n"
2209 "\n%4$sResource Management User Record Properties:%5$s\n"
2210 " --disk-size=BYTES Size to assign the user on disk\n"
2211 " --access-mode=MODE User home directory access mode\n"
2212 " --umask=MODE Umask for user when logging in\n"
2213 " --nice=NICE Nice level for user\n"
2214 " --rlimit=LIMIT=VALUE[:VALUE]\n"
2215 " Set resource limits\n"
2216 " --tasks-max=MAX Set maximum number of per-user tasks\n"
2217 " --memory-high=BYTES Set high memory threshold in bytes\n"
2218 " --memory-max=BYTES Set maximum memory limit\n"
2219 " --cpu-weight=WEIGHT Set CPU weight\n"
2220 " --io-weight=WEIGHT Set IO weight\n"
2221 "\n%4$sStorage User Record Properties:%5$s\n"
2222 " --storage=STORAGE Storage type to use (luks, fscrypt, directory,\n"
2223 " subvolume, cifs)\n"
2224 " --image-path=PATH Path to image file/directory\n"
2225 "\n%4$sLUKS Storage User Record Properties:%5$s\n"
2226 " --fs-type=TYPE File system type to use in case of luks\n"
2227 " storage (ext4, xfs, btrfs)\n"
2228 " --luks-discard=BOOL Whether to use 'discard' feature of file system\n"
2229 " when activated (mounted)\n"
2230 " --luks-offline-discard=BOOL\n"
2231 " Whether to trim file on logout\n"
2232 " --luks-cipher=CIPHER Cipher to use for LUKS encryption\n"
2233 " --luks-cipher-mode=MODE Cipher mode to use for LUKS encryption\n"
2234 " --luks-volume-key-size=BITS\n"
2235 " Volume key size to use for LUKS encryption\n"
2236 " --luks-pbkdf-type=TYPE Password-based Key Derivation Function to use\n"
2237 " --luks-pbkdf-hash-algorithm=ALGORITHM\n"
2238 " PBKDF hash algorithm to use\n"
2239 " --luks-pbkdf-time-cost=SECS\n"
2240 " Time cost for PBKDF in seconds\n"
2241 " --luks-pbkdf-memory-cost=BYTES\n"
2242 " Memory cost for PBKDF in bytes\n"
2243 " --luks-pbkdf-parallel-threads=NUMBER\n"
2244 " Number of parallel threads for PKBDF\n"
2245 "\n%4$sMounting User Record Properties:%5$s\n"
2246 " --nosuid=BOOL Control the 'nosuid' flag of the home mount\n"
2247 " --nodev=BOOL Control the 'nodev' flag of the home mount\n"
2248 " --noexec=BOOL Control the 'noexec' flag of the home mount\n"
2249 "\n%4$sCIFS User Record Properties:%5$s\n"
2250 " --cifs-domain=DOMAIN CIFS (Windows) domain\n"
2251 " --cifs-user-name=USER CIFS (Windows) user name\n"
2252 " --cifs-service=SERVICE CIFS (Windows) service to mount as home area\n"
2253 "\n%4$sLogin Behaviour User Record Properties:%5$s\n"
2254 " --stop-delay=SECS How long to leave user services running after\n"
2256 " --kill-processes=BOOL Whether to kill user processes when sessions\n"
2258 " --auto-login=BOOL Try to log this user in automatically\n"
2259 "\nSee the %6$s for details.\n"
2260 , program_invocation_short_name
2261 , ansi_highlight(), ansi_normal()
2262 , ansi_underline(), ansi_normal()
2269 static int parse_argv(int argc
, char *argv
[]) {
2272 ARG_VERSION
= 0x100,
2275 ARG_NO_ASK_PASSWORD
,
2285 ARG_LUKS_OFFLINE_DISCARD
,
2291 ARG_SSH_AUTHORIZED_KEYS
,
2300 ARG_LUKS_CIPHER_MODE
,
2301 ARG_LUKS_VOLUME_KEY_SIZE
,
2313 ARG_LUKS_PBKDF_TYPE
,
2314 ARG_LUKS_PBKDF_HASH_ALGORITHM
,
2315 ARG_LUKS_PBKDF_TIME_COST
,
2316 ARG_LUKS_PBKDF_MEMORY_COST
,
2317 ARG_LUKS_PBKDF_PARALLEL_THREADS
,
2318 ARG_RATE_LIMIT_INTERVAL
,
2319 ARG_RATE_LIMIT_BURST
,
2322 ARG_ENFORCE_PASSWORD_POLICY
,
2323 ARG_PASSWORD_CHANGE_NOW
,
2324 ARG_PASSWORD_CHANGE_MIN
,
2325 ARG_PASSWORD_CHANGE_MAX
,
2326 ARG_PASSWORD_CHANGE_WARN
,
2327 ARG_PASSWORD_CHANGE_INACTIVE
,
2330 ARG_PKCS11_TOKEN_URI
,
2332 ARG_AND_CHANGE_PASSWORD
,
2335 static const struct option options
[] = {
2336 { "help", no_argument
, NULL
, 'h' },
2337 { "version", no_argument
, NULL
, ARG_VERSION
},
2338 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
2339 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
2340 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
2341 { "host", required_argument
, NULL
, 'H' },
2342 { "machine", required_argument
, NULL
, 'M' },
2343 { "identity", required_argument
, NULL
, 'I' },
2344 { "real-name", required_argument
, NULL
, 'c' },
2345 { "comment", required_argument
, NULL
, 'c' }, /* Compat alias to keep thing in sync with useradd(8) */
2346 { "realm", required_argument
, NULL
, ARG_REALM
},
2347 { "email-address", required_argument
, NULL
, ARG_EMAIL_ADDRESS
},
2348 { "location", required_argument
, NULL
, ARG_LOCATION
},
2349 { "password-hint", required_argument
, NULL
, ARG_PASSWORD_HINT
},
2350 { "icon-name", required_argument
, NULL
, ARG_ICON_NAME
},
2351 { "home-dir", required_argument
, NULL
, 'd' }, /* Compatible with useradd(8) */
2352 { "uid", required_argument
, NULL
, 'u' }, /* Compatible with useradd(8) */
2353 { "member-of", required_argument
, NULL
, 'G' },
2354 { "groups", required_argument
, NULL
, 'G' }, /* Compat alias to keep thing in sync with useradd(8) */
2355 { "skel", required_argument
, NULL
, 'k' }, /* Compatible with useradd(8) */
2356 { "shell", required_argument
, NULL
, 's' }, /* Compatible with useradd(8) */
2357 { "setenv", required_argument
, NULL
, ARG_SETENV
},
2358 { "timezone", required_argument
, NULL
, ARG_TIMEZONE
},
2359 { "language", required_argument
, NULL
, ARG_LANGUAGE
},
2360 { "locked", required_argument
, NULL
, ARG_LOCKED
},
2361 { "not-before", required_argument
, NULL
, ARG_NOT_BEFORE
},
2362 { "not-after", required_argument
, NULL
, ARG_NOT_AFTER
},
2363 { "expiredate", required_argument
, NULL
, 'e' }, /* Compat alias to keep thing in sync with useradd(8) */
2364 { "ssh-authorized-keys", required_argument
, NULL
, ARG_SSH_AUTHORIZED_KEYS
},
2365 { "disk-size", required_argument
, NULL
, ARG_DISK_SIZE
},
2366 { "access-mode", required_argument
, NULL
, ARG_ACCESS_MODE
},
2367 { "umask", required_argument
, NULL
, ARG_UMASK
},
2368 { "nice", required_argument
, NULL
, ARG_NICE
},
2369 { "rlimit", required_argument
, NULL
, ARG_RLIMIT
},
2370 { "tasks-max", required_argument
, NULL
, ARG_TASKS_MAX
},
2371 { "memory-high", required_argument
, NULL
, ARG_MEMORY_HIGH
},
2372 { "memory-max", required_argument
, NULL
, ARG_MEMORY_MAX
},
2373 { "cpu-weight", required_argument
, NULL
, ARG_CPU_WEIGHT
},
2374 { "io-weight", required_argument
, NULL
, ARG_IO_WEIGHT
},
2375 { "storage", required_argument
, NULL
, ARG_STORAGE
},
2376 { "image-path", required_argument
, NULL
, ARG_IMAGE_PATH
},
2377 { "fs-type", required_argument
, NULL
, ARG_FS_TYPE
},
2378 { "luks-discard", required_argument
, NULL
, ARG_LUKS_DISCARD
},
2379 { "luks-offline-discard", required_argument
, NULL
, ARG_LUKS_OFFLINE_DISCARD
},
2380 { "luks-cipher", required_argument
, NULL
, ARG_LUKS_CIPHER
},
2381 { "luks-cipher-mode", required_argument
, NULL
, ARG_LUKS_CIPHER_MODE
},
2382 { "luks-volume-key-size", required_argument
, NULL
, ARG_LUKS_VOLUME_KEY_SIZE
},
2383 { "luks-pbkdf-type", required_argument
, NULL
, ARG_LUKS_PBKDF_TYPE
},
2384 { "luks-pbkdf-hash-algorithm", required_argument
, NULL
, ARG_LUKS_PBKDF_HASH_ALGORITHM
},
2385 { "luks-pbkdf-time-cost", required_argument
, NULL
, ARG_LUKS_PBKDF_TIME_COST
},
2386 { "luks-pbkdf-memory-cost", required_argument
, NULL
, ARG_LUKS_PBKDF_MEMORY_COST
},
2387 { "luks-pbkdf-parallel-threads", required_argument
, NULL
, ARG_LUKS_PBKDF_PARALLEL_THREADS
},
2388 { "nosuid", required_argument
, NULL
, ARG_NOSUID
},
2389 { "nodev", required_argument
, NULL
, ARG_NODEV
},
2390 { "noexec", required_argument
, NULL
, ARG_NOEXEC
},
2391 { "cifs-user-name", required_argument
, NULL
, ARG_CIFS_USER_NAME
},
2392 { "cifs-domain", required_argument
, NULL
, ARG_CIFS_DOMAIN
},
2393 { "cifs-service", required_argument
, NULL
, ARG_CIFS_SERVICE
},
2394 { "rate-limit-interval", required_argument
, NULL
, ARG_RATE_LIMIT_INTERVAL
},
2395 { "rate-limit-burst", required_argument
, NULL
, ARG_RATE_LIMIT_BURST
},
2396 { "stop-delay", required_argument
, NULL
, ARG_STOP_DELAY
},
2397 { "kill-processes", required_argument
, NULL
, ARG_KILL_PROCESSES
},
2398 { "enforce-password-policy", required_argument
, NULL
, ARG_ENFORCE_PASSWORD_POLICY
},
2399 { "password-change-now", required_argument
, NULL
, ARG_PASSWORD_CHANGE_NOW
},
2400 { "password-change-min", required_argument
, NULL
, ARG_PASSWORD_CHANGE_MIN
},
2401 { "password-change-max", required_argument
, NULL
, ARG_PASSWORD_CHANGE_MAX
},
2402 { "password-change-warn", required_argument
, NULL
, ARG_PASSWORD_CHANGE_WARN
},
2403 { "password-change-inactive", required_argument
, NULL
, ARG_PASSWORD_CHANGE_INACTIVE
},
2404 { "auto-login", required_argument
, NULL
, ARG_AUTO_LOGIN
},
2405 { "json", required_argument
, NULL
, ARG_JSON
},
2406 { "export-format", required_argument
, NULL
, ARG_EXPORT_FORMAT
},
2407 { "pkcs11-token-uri", required_argument
, NULL
, ARG_PKCS11_TOKEN_URI
},
2408 { "and-resize", required_argument
, NULL
, ARG_AND_RESIZE
},
2409 { "and-change-password", required_argument
, NULL
, ARG_AND_CHANGE_PASSWORD
},
2421 c
= getopt_long(argc
, argv
, "hH:M:I:c:d:u:k:s:e:G:jPE", options
, NULL
);
2428 return help(0, NULL
, NULL
);
2434 arg_pager_flags
|= PAGER_DISABLE
;
2441 case ARG_NO_ASK_PASSWORD
:
2442 arg_ask_password
= false;
2446 arg_transport
= BUS_TRANSPORT_REMOTE
;
2451 arg_transport
= BUS_TRANSPORT_MACHINE
;
2456 arg_identity
= optarg
;
2460 if (isempty(optarg
)) {
2461 r
= drop_from_identity("realName");
2468 if (!valid_gecos(optarg
))
2469 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Real name '%s' not a valid GECOS field.", optarg
);
2471 r
= json_variant_set_field_string(&arg_identity_extra
, "realName", optarg
);
2473 return log_error_errno(r
, "Failed to set realName field: %m");
2478 _cleanup_free_
char *hd
= NULL
;
2480 if (isempty(optarg
)) {
2481 r
= drop_from_identity("homeDirectory");
2488 r
= parse_path_argument_and_warn(optarg
, false, &hd
);
2492 if (!valid_home(hd
))
2493 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Home directory '%s' not valid.", hd
);
2495 r
= json_variant_set_field_string(&arg_identity_extra
, "homeDirectory", hd
);
2497 return log_error_errno(r
, "Failed to set homeDirectory field: %m");
2503 if (isempty(optarg
)) {
2504 r
= drop_from_identity("realm");
2511 r
= dns_name_is_valid(optarg
);
2513 return log_error_errno(r
, "Failed to determine whether realm '%s' is a valid DNS domain: %m", optarg
);
2515 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Realm '%s' is not a valid DNS domain: %m", optarg
);
2517 r
= json_variant_set_field_string(&arg_identity_extra
, "realm", optarg
);
2519 return log_error_errno(r
, "Failed to set realm field: %m");
2522 case ARG_EMAIL_ADDRESS
:
2525 case ARG_CIFS_USER_NAME
:
2526 case ARG_CIFS_DOMAIN
:
2527 case ARG_CIFS_SERVICE
: {
2530 c
== ARG_EMAIL_ADDRESS
? "emailAddress" :
2531 c
== ARG_LOCATION
? "location" :
2532 c
== ARG_ICON_NAME
? "iconName" :
2533 c
== ARG_CIFS_USER_NAME
? "cifsUserName" :
2534 c
== ARG_CIFS_DOMAIN
? "cifsDomain" :
2535 c
== ARG_CIFS_SERVICE
? "cifsService" :
2540 if (isempty(optarg
)) {
2541 r
= drop_from_identity(field
);
2548 r
= json_variant_set_field_string(&arg_identity_extra
, field
, optarg
);
2550 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2555 case ARG_PASSWORD_HINT
:
2556 if (isempty(optarg
)) {
2557 r
= drop_from_identity("passwordHint");
2564 r
= json_variant_set_field_string(&arg_identity_extra_privileged
, "passwordHint", optarg
);
2566 return log_error_errno(r
, "Failed to set passwordHint field: %m");
2568 string_erase(optarg
);
2574 if (isempty(optarg
)) {
2575 r
= drop_from_identity("niceLevel");
2581 r
= parse_nice(optarg
, &nc
);
2583 return log_error_errno(r
, "Failed to parse nice level: %s", optarg
);
2585 r
= json_variant_set_field_integer(&arg_identity_extra
, "niceLevel", nc
);
2587 return log_error_errno(r
, "Failed to set niceLevel field: %m");
2593 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
, *jcur
= NULL
, *jmax
= NULL
;
2594 _cleanup_free_
char *field
= NULL
, *t
= NULL
;
2599 if (isempty(optarg
)) {
2600 /* Remove all resource limits */
2602 r
= drop_from_identity("resourceLimits");
2606 arg_identity_filter_rlimits
= strv_free(arg_identity_filter_rlimits
);
2607 arg_identity_extra_rlimits
= json_variant_unref(arg_identity_extra_rlimits
);
2611 eq
= strchr(optarg
, '=');
2613 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Can't parse resource limit assignment: %s", optarg
);
2615 field
= strndup(optarg
, eq
- optarg
);
2619 l
= rlimit_from_string_harder(field
);
2621 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Unknown resource limit type: %s", field
);
2623 if (isempty(eq
+ 1)) {
2624 /* Remove only the specific rlimit */
2626 r
= strv_extend(&arg_identity_filter_rlimits
, rlimit_to_string(l
));
2630 r
= json_variant_filter(&arg_identity_extra_rlimits
, STRV_MAKE(field
));
2632 return log_error_errno(r
, "Failed to filter JSON identity data: %m");
2637 r
= rlimit_parse(l
, eq
+ 1, &rl
);
2639 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to parse resource limit value: %s", eq
+ 1);
2641 r
= rl
.rlim_cur
== RLIM_INFINITY
? json_variant_new_null(&jcur
) : json_variant_new_unsigned(&jcur
, rl
.rlim_cur
);
2643 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to allocate current integer: %m");
2645 r
= rl
.rlim_max
== RLIM_INFINITY
? json_variant_new_null(&jmax
) : json_variant_new_unsigned(&jmax
, rl
.rlim_max
);
2647 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to allocate maximum integer: %m");
2651 JSON_BUILD_PAIR("cur", JSON_BUILD_VARIANT(jcur
)),
2652 JSON_BUILD_PAIR("max", JSON_BUILD_VARIANT(jmax
))));
2654 return log_error_errno(r
, "Failed to build resource limit: %m");
2656 t
= strjoin("RLIMIT_", rlimit_to_string(l
));
2660 r
= json_variant_set_field(&arg_identity_extra_rlimits
, t
, v
);
2662 return log_error_errno(r
, "Failed to set %s field: %m", rlimit_to_string(l
));
2670 if (isempty(optarg
)) {
2671 r
= drop_from_identity("uid");
2678 r
= parse_uid(optarg
, &uid
);
2680 return log_error_errno(r
, "Failed to parse UID '%s'.", optarg
);
2682 if (uid_is_system(uid
))
2683 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "UID " UID_FMT
" is in system range, refusing.", uid
);
2684 if (uid_is_dynamic(uid
))
2685 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "UID " UID_FMT
" is in dynamic range, refusing.", uid
);
2686 if (uid
== UID_NOBODY
)
2687 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "UID " UID_FMT
" is nobody UID, refusing.", uid
);
2689 r
= json_variant_set_field_unsigned(&arg_identity_extra
, "uid", uid
);
2691 return log_error_errno(r
, "Failed to set realm field: %m");
2697 case ARG_IMAGE_PATH
: {
2698 const char *field
= c
== 'k' ? "skeletonDirectory" : "imagePath";
2699 _cleanup_free_
char *v
= NULL
;
2701 if (isempty(optarg
)) {
2702 r
= drop_from_identity(field
);
2709 r
= parse_path_argument_and_warn(optarg
, false, &v
);
2713 r
= json_variant_set_field_string(&arg_identity_extra_this_machine
, field
, v
);
2715 return log_error_errno(r
, "Failed to set %s field: %m", v
);
2721 if (isempty(optarg
)) {
2722 r
= drop_from_identity("shell");
2729 if (!valid_shell(optarg
))
2730 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Shell '%s' not valid.", optarg
);
2732 r
= json_variant_set_field_string(&arg_identity_extra
, "shell", optarg
);
2734 return log_error_errno(r
, "Failed to set shell field: %m");
2739 _cleanup_free_
char **l
= NULL
, **k
= NULL
;
2740 _cleanup_(json_variant_unrefp
) JsonVariant
*ne
= NULL
;
2743 if (isempty(optarg
)) {
2744 r
= drop_from_identity("environment");
2751 if (!env_assignment_is_valid(optarg
))
2752 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Environment assignment '%s' not valid.", optarg
);
2754 e
= json_variant_by_key(arg_identity_extra
, "environment");
2756 r
= json_variant_strv(e
, &l
);
2758 return log_error_errno(r
, "Failed to parse JSON environment field: %m");
2761 k
= strv_env_set(l
, optarg
);
2767 r
= json_variant_new_array_strv(&ne
, k
);
2769 return log_error_errno(r
, "Failed to allocate environment list JSON: %m");
2771 r
= json_variant_set_field(&arg_identity_extra
, "environment", ne
);
2773 return log_error_errno(r
, "Failed to set environment list: %m");
2780 if (isempty(optarg
)) {
2781 r
= drop_from_identity("timeZone");
2788 if (!timezone_is_valid(optarg
, LOG_DEBUG
))
2789 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Timezone '%s' is not valid.", optarg
);
2791 r
= json_variant_set_field_string(&arg_identity_extra
, "timeZone", optarg
);
2793 return log_error_errno(r
, "Failed to set timezone field: %m");
2798 if (isempty(optarg
)) {
2799 r
= drop_from_identity("language");
2806 if (!locale_is_valid(optarg
))
2807 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Locale '%s' is not valid.", optarg
);
2809 if (locale_is_installed(optarg
) <= 0)
2810 log_warning("Locale '%s' is not installed, accepting anyway.", optarg
);
2812 r
= json_variant_set_field_string(&arg_identity_extra
, "preferredLanguage", optarg
);
2814 return log_error_errno(r
, "Failed to set preferredLanguage field: %m");
2822 case ARG_KILL_PROCESSES
:
2823 case ARG_ENFORCE_PASSWORD_POLICY
:
2824 case ARG_AUTO_LOGIN
:
2825 case ARG_PASSWORD_CHANGE_NOW
: {
2827 c
== ARG_LOCKED
? "locked" :
2828 c
== ARG_NOSUID
? "mountNoSuid" :
2829 c
== ARG_NODEV
? "mountNoDevices" :
2830 c
== ARG_NOEXEC
? "mountNoExecute" :
2831 c
== ARG_KILL_PROCESSES
? "killProcesses" :
2832 c
== ARG_ENFORCE_PASSWORD_POLICY
? "enforcePasswordPolicy" :
2833 c
== ARG_AUTO_LOGIN
? "autoLogin" :
2834 c
== ARG_PASSWORD_CHANGE_NOW
? "passwordChangeNow" :
2839 if (isempty(optarg
)) {
2840 r
= drop_from_identity(field
);
2847 r
= parse_boolean(optarg
);
2849 return log_error_errno(r
, "Failed to parse %s boolean: %m", field
);
2851 r
= json_variant_set_field_boolean(&arg_identity_extra
, field
, r
> 0);
2853 return log_error_errno(r
, "Failed to set %s field: %m", field
);
2859 r
= json_variant_set_field_boolean(&arg_identity_extra
, "enforcePasswordPolicy", false);
2861 return log_error_errno(r
, "Failed to set enforcePasswordPolicy field: %m");
2866 if (isempty(optarg
)) {
2867 r
= drop_from_identity("diskSize");
2871 r
= drop_from_identity("diskSizeRelative");
2875 arg_disk_size
= arg_disk_size_relative
= UINT64_MAX
;
2879 r
= parse_permille(optarg
);
2881 r
= parse_size(optarg
, 1024, &arg_disk_size
);
2883 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Disk size '%s' not valid.", optarg
);
2885 r
= drop_from_identity("diskSizeRelative");
2889 r
= json_variant_set_field_unsigned(&arg_identity_extra_this_machine
, "diskSize", arg_disk_size
);
2891 return log_error_errno(r
, "Failed to set diskSize field: %m");
2893 arg_disk_size_relative
= UINT64_MAX
;
2895 /* Normalize to UINT32_MAX == 100% */
2896 arg_disk_size_relative
= (uint64_t) r
* UINT32_MAX
/ 1000U;
2898 r
= drop_from_identity("diskSize");
2902 r
= json_variant_set_field_unsigned(&arg_identity_extra_this_machine
, "diskSizeRelative", arg_disk_size_relative
);
2904 return log_error_errno(r
, "Failed to set diskSizeRelative field: %m");
2906 arg_disk_size
= UINT64_MAX
;
2911 case ARG_ACCESS_MODE
: {
2914 if (isempty(optarg
)) {
2915 r
= drop_from_identity("accessMode");
2922 r
= parse_mode(optarg
, &mode
);
2924 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Access mode '%s' not valid.", optarg
);
2926 r
= json_variant_set_field_unsigned(&arg_identity_extra
, "accessMode", mode
);
2928 return log_error_errno(r
, "Failed to set access mode field: %m");
2933 case ARG_LUKS_DISCARD
:
2934 if (isempty(optarg
)) {
2935 r
= drop_from_identity("luksDiscard");
2942 r
= parse_boolean(optarg
);
2944 return log_error_errno(r
, "Failed to parse --luks-discard= parameter: %s", optarg
);
2946 r
= json_variant_set_field_boolean(&arg_identity_extra
, "luksDiscard", r
);
2948 return log_error_errno(r
, "Failed to set discard field: %m");
2952 case ARG_LUKS_OFFLINE_DISCARD
:
2953 if (isempty(optarg
)) {
2954 r
= drop_from_identity("luksOfflineDiscard");
2961 r
= parse_boolean(optarg
);
2963 return log_error_errno(r
, "Failed to parse --luks-offline-discard= parameter: %s", optarg
);
2965 r
= json_variant_set_field_boolean(&arg_identity_extra
, "luksOfflineDiscard", r
);
2967 return log_error_errno(r
, "Failed to set offline discard field: %m");
2971 case ARG_LUKS_VOLUME_KEY_SIZE
:
2972 case ARG_LUKS_PBKDF_PARALLEL_THREADS
:
2973 case ARG_RATE_LIMIT_BURST
: {
2975 c
== ARG_LUKS_VOLUME_KEY_SIZE
? "luksVolumeKeySize" :
2976 c
== ARG_LUKS_PBKDF_PARALLEL_THREADS
? "luksPbkdfParallelThreads" :
2977 c
== ARG_RATE_LIMIT_BURST
? "rateLimitBurst" : NULL
;
2982 if (isempty(optarg
)) {
2983 r
= drop_from_identity(field
);
2988 r
= safe_atou(optarg
, &n
);
2990 return log_error_errno(r
, "Failed to parse %s parameter: %s", field
, optarg
);
2992 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, n
);
2994 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3002 if (isempty(optarg
)) {
3003 r
= drop_from_identity("umask");
3010 r
= parse_mode(optarg
, &m
);
3012 return log_error_errno(r
, "Failed to parse umask: %m");
3014 r
= json_variant_set_field_integer(&arg_identity_extra
, "umask", m
);
3016 return log_error_errno(r
, "Failed to set umask field: %m");
3021 case ARG_SSH_AUTHORIZED_KEYS
: {
3022 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
3023 _cleanup_(strv_freep
) char **l
= NULL
, **add
= NULL
;
3025 if (isempty(optarg
)) {
3026 r
= drop_from_identity("sshAuthorizedKeys");
3033 if (optarg
[0] == '@') {
3034 _cleanup_fclose_
FILE *f
= NULL
;
3036 /* If prefixed with '@' read from a file */
3038 f
= fopen(optarg
+1, "re");
3040 return log_error_errno(errno
, "Failed to open '%s': %m", optarg
+1);
3043 _cleanup_free_
char *line
= NULL
;
3045 r
= read_line(f
, LONG_LINE_MAX
, &line
);
3047 return log_error_errno(r
, "Failed to read from '%s': %m", optarg
+1);
3057 r
= strv_consume(&add
, TAKE_PTR(line
));
3062 /* Otherwise, assume it's a literal key. Let's do some superficial checks
3063 * before accept it though. */
3065 if (string_has_cc(optarg
, NULL
))
3066 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Authorized key contains control characters, refusing.");
3067 if (optarg
[0] == '#')
3068 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Specified key is a comment?");
3070 add
= strv_new(optarg
);
3075 v
= json_variant_ref(json_variant_by_key(arg_identity_extra_privileged
, "sshAuthorizedKeys"));
3077 r
= json_variant_strv(v
, &l
);
3079 return log_error_errno(r
, "Failed to parse SSH authorized keys list: %m");
3082 r
= strv_extend_strv(&l
, add
, true);
3086 v
= json_variant_unref(v
);
3088 r
= json_variant_new_array_strv(&v
, l
);
3092 r
= json_variant_set_field(&arg_identity_extra_privileged
, "sshAuthorizedKeys", v
);
3094 return log_error_errno(r
, "Failed to set authorized keys: %m");
3099 case ARG_NOT_BEFORE
:
3105 field
= c
== ARG_NOT_BEFORE
? "notBeforeUSec" :
3106 IN_SET(c
, ARG_NOT_AFTER
, 'e') ? "notAfterUSec" : NULL
;
3110 if (isempty(optarg
)) {
3111 r
= drop_from_identity(field
);
3118 /* Note the minor discrepancy regarding -e parsing here: we support that for compat
3119 * reasons, and in the original useradd(8) implementation it accepts dates in the
3120 * format YYYY-MM-DD. Coincidentally, we accept dates formatted like that too, but
3121 * with greater precision. */
3122 r
= parse_timestamp(optarg
, &n
);
3124 return log_error_errno(r
, "Failed to parse %s parameter: %m", field
);
3126 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, n
);
3128 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3132 case ARG_PASSWORD_CHANGE_MIN
:
3133 case ARG_PASSWORD_CHANGE_MAX
:
3134 case ARG_PASSWORD_CHANGE_WARN
:
3135 case ARG_PASSWORD_CHANGE_INACTIVE
: {
3139 field
= c
== ARG_PASSWORD_CHANGE_MIN
? "passwordChangeMinUSec" :
3140 c
== ARG_PASSWORD_CHANGE_MAX
? "passwordChangeMaxUSec" :
3141 c
== ARG_PASSWORD_CHANGE_WARN
? "passwordChangeWarnUSec" :
3142 c
== ARG_PASSWORD_CHANGE_INACTIVE
? "passwordChangeInactiveUSec" :
3147 if (isempty(optarg
)) {
3148 r
= drop_from_identity(field
);
3155 r
= parse_sec(optarg
, &n
);
3157 return log_error_errno(r
, "Failed to parse %s parameter: %m", field
);
3159 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, n
);
3161 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3167 case ARG_LUKS_CIPHER
:
3168 case ARG_LUKS_CIPHER_MODE
:
3169 case ARG_LUKS_PBKDF_TYPE
:
3170 case ARG_LUKS_PBKDF_HASH_ALGORITHM
: {
3173 c
== ARG_STORAGE
? "storage" :
3174 c
== ARG_FS_TYPE
? "fileSystemType" :
3175 c
== ARG_LUKS_CIPHER
? "luksCipher" :
3176 c
== ARG_LUKS_CIPHER_MODE
? "luksCipherMode" :
3177 c
== ARG_LUKS_PBKDF_TYPE
? "luksPbkdfType" :
3178 c
== ARG_LUKS_PBKDF_HASH_ALGORITHM
? "luksPbkdfHashAlgorithm" : NULL
;
3182 if (isempty(optarg
)) {
3183 r
= drop_from_identity(field
);
3190 if (!string_is_safe(optarg
))
3191 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Parameter for %s field not valid: %s", field
, optarg
);
3193 r
= json_variant_set_field_string(
3194 IN_SET(c
, ARG_STORAGE
, ARG_FS_TYPE
) ?
3195 &arg_identity_extra_this_machine
:
3196 &arg_identity_extra
, field
, optarg
);
3198 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3203 case ARG_LUKS_PBKDF_TIME_COST
:
3204 case ARG_RATE_LIMIT_INTERVAL
:
3205 case ARG_STOP_DELAY
: {
3207 c
== ARG_LUKS_PBKDF_TIME_COST
? "luksPbkdfTimeCostUSec" :
3208 c
== ARG_RATE_LIMIT_INTERVAL
? "rateLimitIntervalUSec" :
3209 c
== ARG_STOP_DELAY
? "stopDelayUSec" :
3215 if (isempty(optarg
)) {
3216 r
= drop_from_identity(field
);
3223 r
= parse_sec(optarg
, &t
);
3225 return log_error_errno(r
, "Failed to parse %s field: %s", field
, optarg
);
3227 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, t
);
3229 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3235 const char *p
= optarg
;
3238 r
= drop_from_identity("memberOf");
3246 _cleanup_(json_variant_unrefp
) JsonVariant
*mo
= NULL
;
3247 _cleanup_strv_free_
char **list
= NULL
;
3248 _cleanup_free_
char *word
= NULL
;
3250 r
= extract_first_word(&p
, &word
, ",", 0);
3252 return log_error_errno(r
, "Failed to parse group list: %m");
3256 if (!valid_user_group_name(word
, 0))
3257 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Invalid group name %s.", word
);
3259 mo
= json_variant_ref(json_variant_by_key(arg_identity_extra
, "memberOf"));
3261 r
= json_variant_strv(mo
, &list
);
3263 return log_error_errno(r
, "Failed to parse group list: %m");
3265 r
= strv_extend(&list
, word
);
3272 mo
= json_variant_unref(mo
);
3273 r
= json_variant_new_array_strv(&mo
, list
);
3275 return log_error_errno(r
, "Failed to create group list JSON: %m");
3277 r
= json_variant_set_field(&arg_identity_extra
, "memberOf", mo
);
3279 return log_error_errno(r
, "Failed to update group list: %m");
3285 case ARG_TASKS_MAX
: {
3288 if (isempty(optarg
)) {
3289 r
= drop_from_identity("tasksMax");
3295 r
= safe_atou64(optarg
, &u
);
3297 return log_error_errno(r
, "Failed to parse --tasks-max= parameter: %s", optarg
);
3299 r
= json_variant_set_field_unsigned(&arg_identity_extra
, "tasksMax", u
);
3301 return log_error_errno(r
, "Failed to set tasksMax field: %m");
3306 case ARG_MEMORY_MAX
:
3307 case ARG_MEMORY_HIGH
:
3308 case ARG_LUKS_PBKDF_MEMORY_COST
: {
3310 c
== ARG_MEMORY_MAX
? "memoryMax" :
3311 c
== ARG_MEMORY_HIGH
? "memoryHigh" :
3312 c
== ARG_LUKS_PBKDF_MEMORY_COST
? "luksPbkdfMemoryCost" : NULL
;
3318 if (isempty(optarg
)) {
3319 r
= drop_from_identity(field
);
3325 r
= parse_size(optarg
, 1024, &u
);
3327 return log_error_errno(r
, "Failed to parse %s parameter: %s", field
, optarg
);
3329 r
= json_variant_set_field_unsigned(&arg_identity_extra_this_machine
, field
, u
);
3331 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3336 case ARG_CPU_WEIGHT
:
3337 case ARG_IO_WEIGHT
: {
3338 const char *field
= c
== ARG_CPU_WEIGHT
? "cpuWeight" :
3339 c
== ARG_IO_WEIGHT
? "ioWeight" : NULL
;
3344 if (isempty(optarg
)) {
3345 r
= drop_from_identity(field
);
3351 r
= safe_atou64(optarg
, &u
);
3353 return log_error_errno(r
, "Failed to parse --cpu-weight=/--io-weight= parameter: %s", optarg
);
3355 if (!CGROUP_WEIGHT_IS_OK(u
))
3356 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Weight %" PRIu64
" is out of valid weight range.", u
);
3358 r
= json_variant_set_field_unsigned(&arg_identity_extra
, field
, u
);
3360 return log_error_errno(r
, "Failed to set %s field: %m", field
);
3365 case ARG_PKCS11_TOKEN_URI
: {
3368 /* If --pkcs11-token-uri= is specified we always drop everything old */
3369 FOREACH_STRING(p
, "pkcs11TokenUri", "pkcs11EncryptedKey") {
3370 r
= drop_from_identity(p
);
3375 if (isempty(optarg
)) {
3376 arg_pkcs11_token_uri
= strv_free(arg_pkcs11_token_uri
);
3380 if (!pkcs11_uri_valid(optarg
))
3381 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Not a valid PKCS#11 URI: %s", optarg
);
3383 r
= strv_extend(&arg_pkcs11_token_uri
, optarg
);
3387 strv_uniq(arg_pkcs11_token_uri
);
3393 arg_json_format_flags
= JSON_FORMAT_PRETTY_AUTO
|JSON_FORMAT_COLOR_AUTO
;
3397 if (streq(optarg
, "pretty")) {
3399 arg_json_format_flags
= JSON_FORMAT_PRETTY
|JSON_FORMAT_COLOR_AUTO
;
3400 } else if (streq(optarg
, "short")) {
3402 arg_json_format_flags
= JSON_FORMAT_NEWLINE
;
3403 } else if (streq(optarg
, "off")) {
3405 arg_json_format_flags
= 0;
3406 } else if (streq(optarg
, "help")) {
3412 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Unknown argument to --json=: %s", optarg
);
3417 if (arg_export_format
== EXPORT_FORMAT_FULL
)
3418 arg_export_format
= EXPORT_FORMAT_STRIPPED
;
3419 else if (arg_export_format
== EXPORT_FORMAT_STRIPPED
)
3420 arg_export_format
= EXPORT_FORMAT_MINIMAL
;
3422 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Specifying -E more than twice is not supported.");
3425 if (arg_json_format_flags
== 0)
3426 arg_json_format_flags
= JSON_FORMAT_PRETTY_AUTO
|JSON_FORMAT_COLOR_AUTO
;
3429 case ARG_EXPORT_FORMAT
:
3430 if (streq(optarg
, "full"))
3431 arg_export_format
= EXPORT_FORMAT_FULL
;
3432 else if (streq(optarg
, "stripped"))
3433 arg_export_format
= EXPORT_FORMAT_STRIPPED
;
3434 else if (streq(optarg
, "minimal"))
3435 arg_export_format
= EXPORT_FORMAT_MINIMAL
;
3436 else if (streq(optarg
, "help")) {
3445 case ARG_AND_RESIZE
:
3446 arg_and_resize
= true;
3449 case ARG_AND_CHANGE_PASSWORD
:
3450 arg_and_change_password
= true;
3457 assert_not_reached("Unhandled option");
3461 if (!strv_isempty(arg_pkcs11_token_uri
))
3462 arg_and_change_password
= true;
3464 if (arg_disk_size
!= UINT64_MAX
|| arg_disk_size_relative
!= UINT64_MAX
)
3465 arg_and_resize
= true;
3470 static int run(int argc
, char *argv
[]) {
3471 static const Verb verbs
[] = {
3472 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
3473 { "list", VERB_ANY
, 1, VERB_DEFAULT
, list_homes
},
3474 { "activate", 2, VERB_ANY
, 0, activate_home
},
3475 { "deactivate", 2, VERB_ANY
, 0, deactivate_home
},
3476 { "inspect", VERB_ANY
, VERB_ANY
, 0, inspect_home
},
3477 { "authenticate", VERB_ANY
, VERB_ANY
, 0, authenticate_home
},
3478 { "create", VERB_ANY
, 2, 0, create_home
},
3479 { "remove", 2, VERB_ANY
, 0, remove_home
},
3480 { "update", VERB_ANY
, 2, 0, update_home
},
3481 { "passwd", VERB_ANY
, 2, 0, passwd_home
},
3482 { "resize", 2, 3, 0, resize_home
},
3483 { "lock", 2, VERB_ANY
, 0, lock_home
},
3484 { "unlock", 2, VERB_ANY
, 0, unlock_home
},
3485 { "with", 2, VERB_ANY
, 0, with_home
},
3486 { "lock-all", VERB_ANY
, 1, 0, lock_all_homes
},
3494 r
= parse_argv(argc
, argv
);
3498 return dispatch_verb(argc
, argv
, verbs
, NULL
);
3501 DEFINE_MAIN_FUNCTION(run
);