HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/home/homectl.c
Git blob: 0fcf210f9340db07221cb542aaf5fc1759a817bb
Lines 1–1000 of 5,181
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <unistd.h>
#include "sd-bus.h"
#include "sd-varlink.h"
#include "ansi-color.h"
#include "ask-password-api.h"
#include "bitfield.h"
#include "build.h"
#include "bus-common-errors.h"
#include "bus-error.h"
#include "bus-locator.h"
#include "bus-util.h"
#include "capability-list.h"
#include "capability-util.h"
#include "cgroup-util.h"
#include "creds-util.h"
#include "crypto-util.h"
#include "dirent-util.h"
#include "dlopen-note.h"
#include "dns-domain.h"
#include "env-util.h"
#include "errno-util.h"
#include "extract-word.h"
#include "fd-util.h"
#include "fileio.h"
#include "firstboot-util.h"
#include "format-table.h"
#include "format-util.h"
#include "fs-util.h"
#include "glyph-util.h"
#include "hashmap.h"
#include "hexdecoct.h"
#include "home-util.h"
#include "homectl-fido2.h"
#include "homectl-pkcs11.h"
#include "homectl-prompts.h"
#include "homectl-recovery-key.h"
#include "json-util.h"
#include "libfido2-util.h"
#include "locale-util.h"
#include "main-func.h"
#include "options.h"
#include "pager.h"
#include "parse-argument.h"
#include "parse-util.h"
#include "password-quality-util.h"
#include "path-util.h"
#include "percent-util.h"
#include "pidref.h"
#include "pkcs11-util.h"
#include "plymouth-util.h"
#include "polkit-agent.h"
#include "process-util.h"
#include "prompt-util.h"
#include "recurse-dir.h"
#include "rlimit-util.h"
#include "runtime-scope.h"
#include "stat-util.h"
#include "string-table.h"
#include "string-util.h"
#include "strv.h"
#include "terminal-util.h"
#include "time-util.h"
#include "uid-classification.h"
#include "user-record.h"
#include "user-record-password-quality.h"
#include "user-record-show.h"
#include "user-record-util.h"
#include "user-util.h"
#include "userdb.h"
#include "verbs.h"
typedef enum {
EXPORT_FORMAT_FULL, /* export the full record */
EXPORT_FORMAT_STRIPPED, /* strip "state" + "binding", but leave signature in place */
EXPORT_FORMAT_MINIMAL, /* also strip signature */
_EXPORT_FORMAT_MAX,
_EXPORT_FORMAT_INVALID = -EINVAL,
} ExportFormat;
static PagerFlags arg_pager_flags = 0;
static bool arg_legend = true;
static bool arg_ask_password = true;
static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
static const char *arg_host = NULL;
static bool arg_offline = false;
static const char *arg_identity = NULL;
static sd_json_variant *arg_identity_extra = NULL;
static sd_json_variant *arg_identity_extra_privileged = NULL;
static sd_json_variant *arg_identity_extra_this_machine = NULL;
static sd_json_variant *arg_identity_extra_other_machines = NULL;
static sd_json_variant *arg_identity_extra_rlimits = NULL;
static char **arg_identity_filter = NULL; /* this one is also applied to 'privileged' and 'thisMachine' subobjects */
static char **arg_identity_filter_rlimits = NULL;
static uint64_t arg_disk_size = UINT64_MAX;
static uint64_t arg_disk_size_relative = UINT64_MAX;
static char **arg_pkcs11_token_uri = NULL;
static char **arg_fido2_device = NULL;
static Fido2EnrollFlags arg_fido2_lock_with = FIDO2ENROLL_PIN | FIDO2ENROLL_UP;
#if HAVE_LIBFIDO2
static int arg_fido2_cred_alg = COSE_ES256;
#else
static int arg_fido2_cred_alg = 0;
#endif
static bool arg_recovery_key = false;
static char *arg_recovery_key_file = NULL;
static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF;
static ExportFormat arg_export_format = EXPORT_FORMAT_FULL;
static uint64_t arg_capability_bounding_set = CAP_MASK_UNSET;
static uint64_t arg_capability_ambient_set = CAP_MASK_UNSET;
static char *arg_blob_dir = NULL;
static bool arg_blob_clear = false;
static Hashmap *arg_blob_files = NULL;
static char *arg_key_name = NULL;
static bool arg_dry_run = false;
static bool arg_seize = true;
static bool arg_prompt_new_user = false;
static bool arg_prompt_shell = true;
static bool arg_prompt_groups = true;
static bool arg_chrome = true;
static bool arg_mute_console = false;
STATIC_DESTRUCTOR_REGISTER(arg_identity_extra, sd_json_variant_unrefp);
STATIC_DESTRUCTOR_REGISTER(arg_identity_extra_this_machine, sd_json_variant_unrefp);
STATIC_DESTRUCTOR_REGISTER(arg_identity_extra_other_machines, sd_json_variant_unrefp);
STATIC_DESTRUCTOR_REGISTER(arg_identity_extra_privileged, sd_json_variant_unrefp);
STATIC_DESTRUCTOR_REGISTER(arg_identity_extra_rlimits, sd_json_variant_unrefp);
STATIC_DESTRUCTOR_REGISTER(arg_identity_filter, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_identity_filter_rlimits, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_pkcs11_token_uri, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_fido2_device, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_recovery_key_file, freep);
STATIC_DESTRUCTOR_REGISTER(arg_blob_dir, freep);
STATIC_DESTRUCTOR_REGISTER(arg_blob_files, hashmap_freep);
STATIC_DESTRUCTOR_REGISTER(arg_key_name, freep);
static const BusLocator *bus_mgr;
static const char *export_format_table[_EXPORT_FORMAT_MAX] = {
[EXPORT_FORMAT_FULL] = "full",
[EXPORT_FORMAT_STRIPPED] = "stripped",
[EXPORT_FORMAT_MINIMAL] = "minimal",
};
DEFINE_PRIVATE_STRING_TABLE_LOOKUP(export_format, ExportFormat);
static bool identity_properties_specified(void) {
return
arg_identity ||
!sd_json_variant_is_blank_object(arg_identity_extra) ||
!sd_json_variant_is_blank_object(arg_identity_extra_privileged) ||
!sd_json_variant_is_blank_object(arg_identity_extra_this_machine) ||
!sd_json_variant_is_blank_object(arg_identity_extra_other_machines) ||
!sd_json_variant_is_blank_object(arg_identity_extra_rlimits) ||
!strv_isempty(arg_identity_filter) ||
!strv_isempty(arg_identity_filter_rlimits) ||
!strv_isempty(arg_pkcs11_token_uri) ||
!strv_isempty(arg_fido2_device) ||
arg_recovery_key ||
arg_blob_dir ||
arg_blob_clear ||
!hashmap_isempty(arg_blob_files);
}
static int acquire_bus(sd_bus **bus) {
int r;
assert(bus);
if (*bus)
return 0;
r = bus_connect_transport(arg_transport, arg_host, RUNTIME_SCOPE_SYSTEM, bus);
if (r < 0)
return bus_log_connect_error(r, arg_transport, RUNTIME_SCOPE_SYSTEM);
(void) sd_bus_set_allow_interactive_authorization(*bus, arg_ask_password);
return 0;
}
COMMAND(
"homectl\0",
"Create, manipulate or inspect home directories.",
.man_pages = "homectl(1)\0",
.pager_flags = &arg_pager_flags,
.flags = COMMAND_HELP_SEPARATE, /* the option table is very wide */
);
VERB_GROUP("Basic User Manipulation Commands");
VERB(verb_list_homes, "list", /* argspec= */ NULL, VERB_ANY, 1, VERB_DEFAULT,
"List home areas");
static int verb_list_homes(int argc, char *argv[], uintptr_t _data, void *userdata) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
_cleanup_(table_unrefp) Table *table = NULL;
int r;
r = acquire_bus(&bus);
if (r < 0)
return r;
r = bus_call_method(bus, bus_mgr, "ListHomes", &error, &reply, NULL);
if (r < 0)
return log_error_errno(r, "Failed to list homes: %s", bus_error_message(&error, r));
table = table_new("name", "uid", "gid", "state", "realname", "home", "shell");
if (!table)
return log_oom();
r = sd_bus_message_enter_container(reply, 'a', "(susussso)");
if (r < 0)
return bus_log_parse_error(r);
for (;;) {
const char *name, *state, *realname, *home, *shell, *color;
TableCell *cell;
uint32_t uid, gid;
r = sd_bus_message_read(reply, "(susussso)", &name, &uid, &state, &gid, &realname, &home, &shell, NULL);
if (r < 0)
return bus_log_parse_error(r);
if (r == 0)
break;
r = table_add_many(table,
TABLE_STRING, name,
TABLE_UID, uid,
TABLE_GID, gid);
if (r < 0)
return table_log_add_error(r);
r = table_add_cell(table, &cell, TABLE_STRING, state);
if (r < 0)
return table_log_add_error(r);
color = user_record_state_color(state);
if (color)
(void) table_set_color(table, cell, color);
r = table_add_many(table,
TABLE_STRING, strna(empty_to_null(realname)),
TABLE_STRING, home,
TABLE_STRING, strna(empty_to_null(shell)));
if (r < 0)
return table_log_add_error(r);
}
r = sd_bus_message_exit_container(reply);
if (r < 0)
return bus_log_parse_error(r);
if (!table_isempty(table) || sd_json_format_enabled(arg_json_format_flags)) {
r = table_set_sort(table, (size_t) 0);
if (r < 0)
return table_log_sort_error(r);
r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
if (r < 0)
return r;
}
if (arg_legend && !sd_json_format_enabled(arg_json_format_flags)) {
if (table_isempty(table))
printf("No home areas.\n");
else
printf("\n%zu home areas listed.\n", table_get_rows(table) - 1);
}
return 0;
}
static int acquire_existing_password(
const char *user_name,
UserRecord *hr,
bool emphasize_current_password,
AskPasswordFlags flags) {
_cleanup_strv_free_erase_ char **password = NULL;
_cleanup_(erase_and_freep) char *envpw = NULL;
_cleanup_free_ char *question = NULL;
int r;
assert(user_name);
assert(hr);
r = getenv_steal_erase("PASSWORD", &envpw);
if (r < 0)
return log_error_errno(r, "Failed to acquire password from environment: %m");
if (r > 0) {
/* People really shouldn't use environment variables for passing passwords. We support this
* only for testing purposes, and do not document the behaviour, so that people won't
* actually use this outside of testing. */
r = user_record_set_password(hr, STRV_MAKE(envpw), true);
if (r < 0)
return log_error_errno(r, "Failed to store password: %m");
return 1;
}
/* If this is not our own user, then don't use the password cache */
if (is_this_me(user_name) <= 0)
SET_FLAG(flags, ASK_PASSWORD_ACCEPT_CACHED|ASK_PASSWORD_PUSH_CACHE, false);
if (asprintf(&question, emphasize_current_password ?
"Please enter current password for user %s:" :
"Please enter password for user %s:",
user_name) < 0)
return log_oom();
AskPasswordRequest req = {
.tty_fd = -EBADF,
.message = question,
.icon = "user-home",
.keyring = "home-password",
.credential = "home.password",
.until = USEC_INFINITY,
.hup_fd = -EBADF,
};
r = ask_password_auto(&req, flags, &password);
if (r == -EUNATCH) { /* EUNATCH is returned if no password was found and asking interactively was
* disabled via the flags. Not an error for us. */
log_debug_errno(r, "No passwords acquired.");
return 0;
}
if (r < 0)
return log_error_errno(r, "Failed to acquire password: %m");
r = user_record_set_password(hr, password, true);
if (r < 0)
return log_error_errno(r, "Failed to store password: %m");
return 1;
}
static int acquire_recovery_key(
const char *user_name,
UserRecord *hr,
AskPasswordFlags flags) {
_cleanup_strv_free_erase_ char **recovery_key = NULL;
_cleanup_(erase_and_freep) char *envpw = NULL;
_cleanup_free_ char *question = NULL;
int r;
assert(user_name);
assert(hr);
r = getenv_steal_erase("PASSWORD", &envpw);
if (r < 0)
return log_error_errno(r, "Failed to acquire password from environment: %m");
if (r > 0) {
/* People really shouldn't use environment variables for passing secrets. We support this
* only for testing purposes, and do not document the behaviour, so that people won't
* actually use this outside of testing. */
r = user_record_set_password(hr, STRV_MAKE(envpw), true); /* recovery keys are stored in the record exactly like regular passwords! */
if (r < 0)
return log_error_errno(r, "Failed to store recovery key: %m");
return 1;
}
/* If this is not our own user, then don't use the password cache */
if (is_this_me(user_name) <= 0)
SET_FLAG(flags, ASK_PASSWORD_ACCEPT_CACHED|ASK_PASSWORD_PUSH_CACHE, false);
if (asprintf(&question, "Please enter recovery key for user %s:", user_name) < 0)
return log_oom();
AskPasswordRequest req = {
.tty_fd = -EBADF,
.message = question,
.icon = "user-home",
.keyring = "home-recovery-key",
.credential = "home.recovery-key",
.until = USEC_INFINITY,
.hup_fd = -EBADF,
};
r = ask_password_auto(&req, flags, &recovery_key);
if (r == -EUNATCH) { /* EUNATCH is returned if no recovery key was found and asking interactively was
* disabled via the flags. Not an error for us. */
log_debug_errno(r, "No recovery keys acquired.");
return 0;
}
if (r < 0)
return log_error_errno(r, "Failed to acquire recovery keys: %m");
r = user_record_set_password(hr, recovery_key, true);
if (r < 0)
return log_error_errno(r, "Failed to store recovery keys: %m");
return 1;
}
static int acquire_token_pin(
const char *user_name,
UserRecord *hr,
AskPasswordFlags flags) {
_cleanup_strv_free_erase_ char **pin = NULL;
_cleanup_(erase_and_freep) char *envpin = NULL;
_cleanup_free_ char *question = NULL;
int r;
assert(user_name);
assert(hr);
r = getenv_steal_erase("PIN", &envpin);
if (r < 0)
return log_error_errno(r, "Failed to acquire PIN from environment: %m");
if (r > 0) {
r = user_record_set_token_pin(hr, STRV_MAKE(envpin), false);
if (r < 0)
return log_error_errno(r, "Failed to store token PIN: %m");
return 1;
}
/* If this is not our own user, then don't use the password cache */
if (is_this_me(user_name) <= 0)
SET_FLAG(flags, ASK_PASSWORD_ACCEPT_CACHED|ASK_PASSWORD_PUSH_CACHE, false);
if (asprintf(&question, "Please enter security token PIN for user %s:", user_name) < 0)
return log_oom();
AskPasswordRequest req = {
.tty_fd = -EBADF,
.message = question,
.icon = "user-home",
.keyring = "token-pin",
.credential = "home.token-pin",
.until = USEC_INFINITY,
.hup_fd = -EBADF,
};
r = ask_password_auto(&req, flags, &pin);
if (r == -EUNATCH) { /* EUNATCH is returned if no PIN was found and asking interactively was disabled
* via the flags. Not an error for us. */
log_debug_errno(r, "No security token PINs acquired.");
return 0;
}
if (r < 0)
return log_error_errno(r, "Failed to acquire security token PIN: %m");
r = user_record_set_token_pin(hr, pin, false);
if (r < 0)
return log_error_errno(r, "Failed to store security token PIN: %m");
return 1;
}
static int handle_generic_user_record_error(
const char *user_name,
UserRecord *hr,
const sd_bus_error *error,
int ret,
bool emphasize_current_password) {
int r;
assert(user_name);
assert(hr);
if (sd_bus_error_has_name(error, BUS_ERROR_HOME_ABSENT))
return log_error_errno(SYNTHETIC_ERRNO(EREMOTE),
"Home of user %s is currently absent, please plug in the necessary storage device or backing file system.", user_name);
else if (sd_bus_error_has_name(error, BUS_ERROR_AUTHENTICATION_LIMIT_HIT))
return log_error_errno(SYNTHETIC_ERRNO(ETOOMANYREFS),
"Too frequent login attempts for user %s, try again later.", user_name);
else if (sd_bus_error_has_name(error, BUS_ERROR_BAD_PASSWORD)) {
if (!strv_isempty(hr->password))
log_notice("Password incorrect or not sufficient, please try again.");
/* Don't consume cache entries or credentials here, we already tried that unsuccessfully. But
* let's push what we acquire here into the cache */
r = acquire_existing_password(
user_name,
hr,
emphasize_current_password,
ASK_PASSWORD_PUSH_CACHE | ASK_PASSWORD_NO_CREDENTIAL);
if (r < 0)
return r;
} else if (sd_bus_error_has_name(error, BUS_ERROR_BAD_RECOVERY_KEY)) {
if (!strv_isempty(hr->password))
log_notice("Recovery key incorrect or not sufficient, please try again.");
/* Don't consume cache entries or credentials here, we already tried that unsuccessfully. But
* let's push what we acquire here into the cache */
r = acquire_recovery_key(
user_name,
hr,
ASK_PASSWORD_PUSH_CACHE | ASK_PASSWORD_NO_CREDENTIAL);
if (r < 0)
return r;
} else if (sd_bus_error_has_name(error, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN)) {
if (strv_isempty(hr->password))
log_notice("Security token not inserted, please enter password.");
else
log_notice("Password incorrect or not sufficient, and configured security token not inserted, please try again.");
r = acquire_existing_password(
user_name,
hr,
emphasize_current_password,
ASK_PASSWORD_PUSH_CACHE | ASK_PASSWORD_NO_CREDENTIAL);
if (r < 0)
return r;
} else if (sd_bus_error_has_name(error, BUS_ERROR_TOKEN_PIN_NEEDED)) {
/* First time the PIN is requested, let's accept cached data, and allow using credential store */
r = acquire_token_pin(
user_name,
hr,
ASK_PASSWORD_ACCEPT_CACHED | ASK_PASSWORD_PUSH_CACHE);
if (r < 0)
return r;
} else if (sd_bus_error_has_name(error, BUS_ERROR_TOKEN_PROTECTED_AUTHENTICATION_PATH_NEEDED)) {
log_notice("%s%sPlease authenticate physically on security token.",
emoji_enabled() ? glyph(GLYPH_TOUCH) : "",
emoji_enabled() ? " " : "");
r = user_record_set_pkcs11_protected_authentication_path_permitted(hr, true);
if (r < 0)
return log_error_errno(r, "Failed to set PKCS#11 protected authentication path permitted flag: %m");
} else if (sd_bus_error_has_name(error, BUS_ERROR_TOKEN_USER_PRESENCE_NEEDED)) {
log_notice("%s%sPlease confirm presence on security token.",
emoji_enabled() ? glyph(GLYPH_TOUCH) : "",
emoji_enabled() ? " " : "");
r = user_record_set_fido2_user_presence_permitted(hr, true);
if (r < 0)
return log_error_errno(r, "Failed to set FIDO2 user presence permitted flag: %m");
} else if (sd_bus_error_has_name(error, BUS_ERROR_TOKEN_USER_VERIFICATION_NEEDED)) {
log_notice("%s%sPlease verify user on security token.",
emoji_enabled() ? glyph(GLYPH_TOUCH) : "",
emoji_enabled() ? " " : "");
r = user_record_set_fido2_user_verification_permitted(hr, true);
if (r < 0)
return log_error_errno(r, "Failed to set FIDO2 user verification permitted flag: %m");
} else if (sd_bus_error_has_name(error, BUS_ERROR_TOKEN_PIN_LOCKED))
return log_error_errno(SYNTHETIC_ERRNO(EPERM), "Security token PIN is locked, please unlock it first. (Hint: Removal and re-insertion might suffice.)");
else if (sd_bus_error_has_name(error, BUS_ERROR_TOKEN_BAD_PIN)) {
log_notice("Security token PIN incorrect, please try again.");
/* If the previous PIN was wrong don't accept cached info anymore, but add to cache. Also, don't use the credential data */
r = acquire_token_pin(
user_name,
hr,
ASK_PASSWORD_PUSH_CACHE | ASK_PASSWORD_NO_CREDENTIAL);
if (r < 0)
return r;
} else if (sd_bus_error_has_name(error, BUS_ERROR_TOKEN_BAD_PIN_FEW_TRIES_LEFT)) {
log_notice("Security token PIN incorrect, please try again (only a few tries left!).");
r = acquire_token_pin(
user_name,
hr,
ASK_PASSWORD_PUSH_CACHE | ASK_PASSWORD_NO_CREDENTIAL);
if (r < 0)
return r;
} else if (sd_bus_error_has_name(error, BUS_ERROR_TOKEN_BAD_PIN_ONE_TRY_LEFT)) {
log_notice("Security token PIN incorrect, please try again (only one try left!).");
r = acquire_token_pin(
user_name,
hr,
ASK_PASSWORD_PUSH_CACHE | ASK_PASSWORD_NO_CREDENTIAL);
if (r < 0)
return r;
} else
return log_error_errno(ret, "Operation on home %s failed: %s", user_name, bus_error_message(error, ret));
return 0;
}
static int acquire_passed_secrets(const char *user_name, UserRecord **ret) {
_cleanup_(user_record_unrefp) UserRecord *secret = NULL;
int r;
assert(ret);
/* Generates an initial secret objects that contains passwords supplied via $PASSWORD, the password
* cache or the credentials subsystem, but excluding any interactive stuff. If nothing is passed,
* returns an empty secret object. */
secret = user_record_new();
if (!secret)
return log_oom();
r = acquire_existing_password(
user_name,
secret,
/* emphasize_current_password= */ false,
ASK_PASSWORD_ACCEPT_CACHED | ASK_PASSWORD_NO_TTY | ASK_PASSWORD_NO_AGENT);
if (r < 0)
return r;
r = acquire_token_pin(
user_name,
secret,
ASK_PASSWORD_ACCEPT_CACHED | ASK_PASSWORD_NO_TTY | ASK_PASSWORD_NO_AGENT);
if (r < 0)
return r;
r = acquire_recovery_key(
user_name,
secret,
ASK_PASSWORD_ACCEPT_CACHED | ASK_PASSWORD_NO_TTY | ASK_PASSWORD_NO_AGENT);
if (r < 0)
return r;
*ret = TAKE_PTR(secret);
return 0;
}
static void dump_home_record(UserRecord *hr) {
int r;
assert(hr);
if (hr->incomplete) {
fflush(stdout);
log_warning("Warning: lacking rights to acquire privileged fields of user record of '%s', output incomplete.", hr->user_name);
}
if (!sd_json_format_enabled(arg_json_format_flags))
user_record_show(hr, true);
else {
_cleanup_(user_record_unrefp) UserRecord *stripped = NULL;
if (arg_export_format == EXPORT_FORMAT_STRIPPED)
r = user_record_clone(hr, USER_RECORD_EXTRACT_EMBEDDED|USER_RECORD_PERMISSIVE, &stripped);
else if (arg_export_format == EXPORT_FORMAT_MINIMAL)
r = user_record_clone(hr, USER_RECORD_EXTRACT_SIGNABLE|USER_RECORD_PERMISSIVE, &stripped);
else
r = 0;
if (r < 0)
log_warning_errno(r, "Failed to strip user record, ignoring: %m");
if (stripped)
hr = stripped;
sd_json_variant_dump(hr->json, arg_json_format_flags, stdout, NULL);
}
}
static int inspect_home(sd_bus *bus, const char *name) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
_cleanup_(user_record_unrefp) UserRecord *hr = NULL;
const char *json;
int incomplete;
uid_t uid;
int r;
r = parse_uid(name, &uid);
if (r < 0)
r = bus_call_method(bus, bus_mgr, "GetUserRecordByName", &error, &reply, "s", name);
else
r = bus_call_method(bus, bus_mgr, "GetUserRecordByUID", &error, &reply, "u", (uint32_t) uid);
if (r < 0)
return log_error_errno(r, "Failed to inspect home: %s", bus_error_message(&error, r));
r = sd_bus_message_read(reply, "sbo", &json, &incomplete, NULL);
if (r < 0)
return bus_log_parse_error(r);
r = sd_json_parse(json, SD_JSON_PARSE_SENSITIVE|SD_JSON_PARSE_MUST_BE_OBJECT, &v, /* reterr_line= */ NULL, /* reterr_column= */ NULL);
if (r < 0)
return log_error_errno(r, "Failed to parse JSON identity: %m");
hr = user_record_new();
if (!hr)
return log_oom();
r = user_record_load(hr, v, USER_RECORD_LOAD_REFUSE_SECRET|USER_RECORD_LOG|USER_RECORD_PERMISSIVE);
if (r < 0)
return r;
hr->incomplete = incomplete;
dump_home_record(hr);
return 0;
}
VERB(verb_inspect_homes, "inspect", "USER…\0", VERB_ANY, VERB_ANY, 0,
"Inspect a home area");
static int verb_inspect_homes(int argc, char *argv[], uintptr_t _data, void *userdata) {
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
int r;
r = acquire_bus(&bus);
if (r < 0)
return r;
pager_open(arg_pager_flags);
char **args = strv_skip(argv, 1);
if (args) {
STRV_FOREACH(arg, args)
RET_GATHER(r, inspect_home(bus, *arg));
return r;
} else {
_cleanup_free_ char *myself = getusername_malloc();
if (!myself)
return log_oom();
return inspect_home(bus, myself);
}
}
static int update_last_change(sd_json_variant **v, bool with_password, bool override) {
sd_json_variant *c;
usec_t n;
int r;
assert(v);
n = now(CLOCK_REALTIME);
c = sd_json_variant_by_key(*v, "lastChangeUSec");
if (c) {
uint64_t u;
if (!override)
goto update_password;
if (!sd_json_variant_is_unsigned(c))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "lastChangeUSec field is not an unsigned integer, refusing.");
u = sd_json_variant_unsigned(c);
if (u >= n)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "lastChangeUSec is from the future, can't update.");
}
r = sd_json_variant_set_field_unsigned(v, "lastChangeUSec", n);
if (r < 0)
return log_error_errno(r, "Failed to update lastChangeUSec: %m");
update_password:
if (!with_password)
return 0;
c = sd_json_variant_by_key(*v, "lastPasswordChangeUSec");
if (c) {
uint64_t u;
if (!override)
return 0;
if (!sd_json_variant_is_unsigned(c))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "lastPasswordChangeUSec field is not an unsigned integer, refusing.");
u = sd_json_variant_unsigned(c);
if (u >= n)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "lastPasswordChangeUSec is from the future, can't update.");
}
r = sd_json_variant_set_field_unsigned(v, "lastPasswordChangeUSec", n);
if (r < 0)
return log_error_errno(r, "Failed to update lastPasswordChangeUSec: %m");
return 1;
}
static int apply_identity_changes(sd_json_variant **_v) {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
int r;
assert(_v);
v = sd_json_variant_ref(*_v);
r = sd_json_variant_filter(&v, arg_identity_filter);
if (r < 0)
return log_error_errno(r, "Failed to filter identity: %m");
r = sd_json_variant_merge_object(&v, arg_identity_extra);
if (r < 0)
return log_error_errno(r, "Failed to merge identities: %m");
if (arg_identity_extra_this_machine || arg_identity_extra_other_machines || !strv_isempty(arg_identity_filter)) {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *per_machine = NULL, *mmid = NULL;
sd_id128_t mid;
r = sd_id128_get_machine(&mid);
if (r < 0)
return log_error_errno(r, "Failed to acquire machine ID: %m");
r = sd_json_variant_new_string(&mmid, SD_ID128_TO_STRING(mid));
if (r < 0)
return log_error_errno(r, "Failed to allocate matchMachineId object: %m");
per_machine = sd_json_variant_ref(sd_json_variant_by_key(v, "perMachine"));
if (per_machine) {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *npm = NULL, *positive = NULL, *negative = NULL;
_cleanup_free_ sd_json_variant **array = NULL;
sd_json_variant *z;
size_t i = 0;
if (!sd_json_variant_is_array(per_machine))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "perMachine field is not an array, refusing.");
array = new(sd_json_variant*, sd_json_variant_elements(per_machine) + 2);
if (!array)
return log_oom();
JSON_VARIANT_ARRAY_FOREACH(z, per_machine) {
sd_json_variant *u;
if (!sd_json_variant_is_object(z))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "perMachine entry is not an object, refusing.");
array[i++] = z;
u = sd_json_variant_by_key(z, "matchMachineId");
if (u && sd_json_variant_equal(u, mmid))
r = sd_json_variant_merge_object(&positive, z);
else {
u = sd_json_variant_by_key(z, "matchNotMachineId");
if (!u || !sd_json_variant_equal(u, mmid))
continue;
r = sd_json_variant_merge_object(&negative, z);
}
if (r < 0)
return log_error_errno(r, "Failed to merge perMachine entry: %m");
i--;
}
r = sd_json_variant_filter(&positive, arg_identity_filter);
if (r < 0)
return log_error_errno(r, "Failed to filter perMachine: %m");
r = sd_json_variant_filter(&negative, arg_identity_filter);
if (r < 0)
return log_error_errno(r, "Failed to filter perMachine: %m");
r = sd_json_variant_merge_object(&positive, arg_identity_extra_this_machine);
if (r < 0)
return log_error_errno(r, "Failed to merge in perMachine fields: %m");
r = sd_json_variant_merge_object(&negative, arg_identity_extra_other_machines);
if (r < 0)
return log_error_errno(r, "Failed to merge in perMachine fields: %m");
if (arg_identity_filter_rlimits || arg_identity_extra_rlimits) {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *rlv = NULL;
rlv = sd_json_variant_ref(sd_json_variant_by_key(positive, "resourceLimits"));
r = sd_json_variant_filter(&rlv, arg_identity_filter_rlimits);
if (r < 0)
return log_error_errno(r, "Failed to filter resource limits: %m");
r = sd_json_variant_merge_object(&rlv, arg_identity_extra_rlimits);
if (r < 0)
return log_error_errno(r, "Failed to set resource limits: %m");
if (sd_json_variant_is_blank_object(rlv)) {
r = sd_json_variant_filter(&positive, STRV_MAKE("resourceLimits"));
if (r < 0)
return log_error_errno(r, "Failed to drop resource limits field from identity: %m");
} else {
r = sd_json_variant_set_field(&positive, "resourceLimits", rlv);
if (r < 0)
return log_error_errno(r, "Failed to update resource limits of identity: %m");
}
}
if (!sd_json_variant_is_blank_object(positive)) {
r = sd_json_variant_set_field(&positive, "matchMachineId", mmid);
if (r < 0)
return log_error_errno(r, "Failed to set matchMachineId field: %m");
array[i++] = positive;
}
if (!sd_json_variant_is_blank_object(negative)) {
r = sd_json_variant_set_field(&negative, "matchNotMachineId", mmid);
if (r < 0)
return log_error_errno(r, "Failed to set matchNotMachineId field: %m");
array[i++] = negative;
}
r = sd_json_variant_new_array(&npm, array, i);
if (r < 0)
return log_error_errno(r, "Failed to allocate new perMachine array: %m");
json_variant_unref_and_replace(per_machine, npm);
} else {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *positive = sd_json_variant_ref(arg_identity_extra_this_machine),
*negative = sd_json_variant_ref(arg_identity_extra_other_machines);
if (arg_identity_extra_rlimits) {
r = sd_json_variant_set_field(&positive, "resourceLimits", arg_identity_extra_rlimits);
if (r < 0)
return log_error_errno(r, "Failed to update resource limits of identity: %m");
}
if (positive) {
r = sd_json_variant_set_field(&positive, "matchMachineId", mmid);
if (r < 0)
return log_error_errno(r, "Failed to set matchMachineId field: %m");
r = sd_json_variant_append_array(&per_machine, positive);
if (r < 0)
return log_error_errno(r, "Failed to append to perMachine array: %m");
}
if (negative) {
r = sd_json_variant_set_field(&negative, "matchNotMachineId", mmid);
if (r < 0)
return log_error_errno(r, "Failed to set matchNotMachineId field: %m");
r = sd_json_variant_append_array(&per_machine, negative);
if (r < 0)
return log_error_errno(r, "Failed to append to perMachine array: %m");
}
}
r = sd_json_variant_set_field(&v, "perMachine", per_machine);
if (r < 0)
return log_error_errno(r, "Failed to update per machine record: %m");
}
if (arg_identity_extra_privileged || arg_identity_filter) {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *privileged = NULL;
privileged = sd_json_variant_ref(sd_json_variant_by_key(v, "privileged"));
r = sd_json_variant_filter(&privileged, arg_identity_filter);
if (r < 0)
return log_error_errno(r, "Failed to filter identity (privileged part): %m");
r = sd_json_variant_merge_object(&privileged, arg_identity_extra_privileged);
if (r < 0)
return log_error_errno(r, "Failed to merge identities (privileged part): %m");
if (sd_json_variant_is_blank_object(privileged)) {
r = sd_json_variant_filter(&v, STRV_MAKE("privileged"));
if (r < 0)
return log_error_errno(r, "Failed to drop privileged part from identity: %m");
} else {
r = sd_json_variant_set_field(&v, "privileged", privileged);
if (r < 0)
return log_error_errno(r, "Failed to update privileged part of identity: %m");
}
}
if (arg_identity_filter_rlimits) {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *rlv = NULL;
rlv = sd_json_variant_ref(sd_json_variant_by_key(v, "resourceLimits"));
r = sd_json_variant_filter(&rlv, arg_identity_filter_rlimits);
if (r < 0)
return log_error_errno(r, "Failed to filter resource limits: %m");
/* Note that we only filter resource limits here, but don't apply them. We do that in the perMachine section */
if (sd_json_variant_is_blank_object(rlv)) {
r = sd_json_variant_filter(&v, STRV_MAKE("resourceLimits"));
if (r < 0)
return log_error_errno(r, "Failed to drop resource limits field from identity: %m");
} else {
r = sd_json_variant_set_field(&v, "resourceLimits", rlv);
if (r < 0)
return log_error_errno(r, "Failed to update resource limits of identity: %m");
}