<xi:include href="version-info.xml" xpointer="v257"/></listitem>
</varlistentry>
+ <varlistentry>
+ <term><option>--from-file=PATH</option></term>
+ <term><option>-f</option></term>
+
+ <listitem><para>When used with the <command>user</command> or <command>group</command> command, read
+ the user definition in JSON format from the specified file, instead of querying it from the
+ system. If the path is specified as <literal>-</literal>, reads the JSON data from standard
+ input. This is useful to validate and introspect JSON user or group records quickly, and check how
+ they would be interpreted on the local system.</para>
+
+ <xi:include href="version-info.xml" xpointer="v258"/></listitem>
+ </varlistentry>
+
<xi:include href="standard-options.xml" xpointer="no-pager" />
<xi:include href="standard-options.xml" xpointer="no-legend" />
<xi:include href="standard-options.xml" xpointer="help" />
<listitem><para>List all known users records or show details of one or more specified user
records. Use <option>--output=</option> to tweak output mode.</para>
+ <para>If used in conjuntion with <option>--from-file=</option> the user record data is read in JSON
+ format from the specified file instead of querying it from the system. For details see above.</para>
+
<xi:include href="version-info.xml" xpointer="v245"/></listitem>
</varlistentry>
<listitem><para>List all known group records or show details of one or more specified group
records. Use <option>--output=</option> to tweak the output mode.</para>
+ <para>If used in conjuntion with <option>--from-file=</option> the group record data is read in JSON
+ format from the specified file instead of querying it from the system. For details see above.</para>
+
<xi:include href="version-info.xml" xpointer="v245"/></listitem>
</varlistentry>
static uid_t arg_uid_max = UID_INVALID-1;
static bool arg_fuzzy = false;
static bool arg_boundaries = true;
+static sd_json_variant *arg_from_file = NULL;
STATIC_DESTRUCTOR_REGISTER(arg_services, strv_freep);
+STATIC_DESTRUCTOR_REGISTER(arg_from_file, sd_json_variant_unrefp);
static const char *user_disposition_to_color(UserDisposition d) {
assert(d >= 0);
int ret = 0, r;
if (arg_output < 0)
- arg_output = argc > 1 && !arg_fuzzy ? OUTPUT_FRIENDLY : OUTPUT_TABLE;
+ arg_output = arg_from_file || (argc > 1 && !arg_fuzzy) ? OUTPUT_FRIENDLY : OUTPUT_TABLE;
if (arg_output == OUTPUT_TABLE) {
table = table_new(" ", "name", "disposition", "uid", "gid", "realname", "home", "shell", "order");
.uid_max = arg_uid_max,
};
- if (argc > 1 && !arg_fuzzy)
+ if (arg_from_file) {
+ if (argc > 1)
+ return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No argument expected when invoked with --from-file=, refusing.");
+
+ _cleanup_(user_record_unrefp) UserRecord *ur = user_record_new();
+ if (!ur)
+ return log_oom();
+
+ r = user_record_load(ur, arg_from_file, USER_RECORD_LOAD_MASK_SECRET|USER_RECORD_LOG);
+ if (r < 0)
+ return r;
+
+ r = show_user(ur, table);
+ if (r < 0)
+ return r;
+
+ } else if (argc > 1 && !arg_fuzzy)
STRV_FOREACH(i, argv + 1) {
_cleanup_(user_record_unrefp) UserRecord *ur = NULL;
int ret = 0, r;
if (arg_output < 0)
- arg_output = argc > 1 && !arg_fuzzy ? OUTPUT_FRIENDLY : OUTPUT_TABLE;
+ arg_output = arg_from_file || (argc > 1 && !arg_fuzzy) ? OUTPUT_FRIENDLY : OUTPUT_TABLE;
if (arg_output == OUTPUT_TABLE) {
table = table_new(" ", "name", "disposition", "gid", "description", "order");
.gid_max = arg_uid_max,
};
- if (argc > 1 && !arg_fuzzy)
+ if (arg_from_file) {
+ if (argc > 1)
+ return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No argument expected when invoked with --from-file=, refusing.");
+
+ _cleanup_(group_record_unrefp) GroupRecord *gr = group_record_new();
+ if (!gr)
+ return log_oom();
+
+ r = group_record_load(gr, arg_from_file, USER_RECORD_LOAD_MASK_SECRET|USER_RECORD_LOG);
+ if (r < 0)
+ return r;
+
+ r = show_group(gr, table);
+ if (r < 0)
+ return r;
+
+ } else if (argc > 1 && !arg_fuzzy)
STRV_FOREACH(i, argv + 1) {
_cleanup_(group_record_unrefp) GroupRecord *gr = NULL;
_cleanup_(table_unrefp) Table *table = NULL;
int ret = 0, r;
+ if (arg_from_file)
+ return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--from-file= not supported when showing memberships, refusing.");
+
if (arg_output < 0)
arg_output = OUTPUT_TABLE;
_cleanup_closedir_ DIR *d = NULL;
int r;
+ if (arg_from_file)
+ return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--from-file= not supported when showing services, refusing.");
+
d = opendir("/run/systemd/userdb/");
if (!d) {
if (errno == ENOENT) {
assert(argc >= 2);
+ if (arg_from_file)
+ return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--from-file= not supported when showing SSH authorized keys, refusing.");
+
if (arg_chain) {
/* If --chain is specified, the rest of the command line is the chain command */
" -R Equivalent to --disposition=regular\n"
" --boundaries=BOOL Show/hide UID/GID range boundaries in output\n"
" -B Equivalent to --boundaries=no\n"
+ " -F --from-file=PATH Read JSON record from file\n"
"\nSee the %s for details.\n",
program_invocation_short_name,
ansi_highlight(),
{ "fuzzy", no_argument, NULL, 'z' },
{ "disposition", required_argument, NULL, ARG_DISPOSITION },
{ "boundaries", required_argument, NULL, ARG_BOUNDARIES },
+ { "from-file", required_argument, NULL, 'F' },
{}
};
int c;
c = getopt_long(argc, argv,
- arg_chain ? "+hjs:NISRzB" : "hjs:NISRzB", /* When --chain was used disable parsing of further switches */
+ arg_chain ? "+hjs:NISRzBF:" : "hjs:NISRzBF:", /* When --chain was used disable parsing of further switches */
options, NULL);
if (c < 0)
break;
arg_boundaries = false;
break;
+ case 'F': {
+ if (isempty(optarg)) {
+ arg_from_file = sd_json_variant_unref(arg_from_file);
+ break;
+ }
+
+ _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
+ const char *fn = streq(optarg, "-") ? NULL : optarg;
+ unsigned line = 0;
+ r = sd_json_parse_file(fn ? NULL : stdin, fn ?: "<stdin>", SD_JSON_PARSE_SENSITIVE, &v, &line, /* reterr_column= */ NULL);
+ if (r < 0)
+ return log_syntax(/* unit= */ NULL, LOG_ERR, fn ?: "<stdin>", line, r, "JSON parse failure.");
+
+ sd_json_variant_unref(arg_from_file);
+ arg_from_file = TAKE_PTR(v);
+ break;
+ }
+
case '?':
return -EINVAL;
if (arg_disposition_mask == UINT64_MAX)
arg_disposition_mask = USER_DISPOSITION_MASK_ALL;
+ if (arg_from_file)
+ arg_boundaries = false;
+
return 1;
}