• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 18251268710

04 Oct 2025 09:35PM UTC coverage: 72.225% (-0.06%) from 72.28%
18251268710

push

github

web-flow
shared/bootspec: don't warn for new `loader.conf` options and correctly parse new `uki` and `profile` boot entry options (#39165)

Commit e2a3d5621 added the `uki` option
to sd-boot, and 1e9c9773b added
`profile`, but because these were not added in src/shared/bootspec,
bootctl still shows warnings like `Unknown line 'uki', ignoring.` when
parsing the config. This PR allows parsing and displaying them correctly
in `bootctl` output. It also stops it from printing a warning for any of
the new `loader.conf` options (`log-level`, `reboot-on-error`, etc.).
Note that `uki-url` is still not handled as I can't easily test it.

4 of 12 new or added lines in 2 files covered. (33.33%)

3065 existing lines in 68 files now uncovered.

303282 of 419915 relevant lines covered (72.22%)

1059441.35 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

73.33
/src/userdb/userdbctl.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <getopt.h>
4
#include <stdlib.h>
5
#include <unistd.h>
6

7
#include "alloc-util.h"
8
#include "bitfield.h"
9
#include "build.h"
10
#include "copy.h"
11
#include "creds-util.h"
12
#include "dirent-util.h"
13
#include "errno-list.h"
14
#include "errno-util.h"
15
#include "escape.h"
16
#include "fd-util.h"
17
#include "fileio.h"
18
#include "format-table.h"
19
#include "format-util.h"
20
#include "fs-util.h"
21
#include "log.h"
22
#include "main-func.h"
23
#include "mkdir.h"
24
#include "pager.h"
25
#include "parse-argument.h"
26
#include "pretty-print.h"
27
#include "recurse-dir.h"
28
#include "socket-util.h"
29
#include "string-table.h"
30
#include "string-util.h"
31
#include "strv.h"
32
#include "uid-classification.h"
33
#include "uid-range.h"
34
#include "umask-util.h"
35
#include "user-record-show.h"
36
#include "user-util.h"
37
#include "userdb.h"
38
#include "verbs.h"
39
#include "virt.h"
40

41
typedef enum {
42
        OUTPUT_CLASSIC,
43
        OUTPUT_TABLE,
44
        OUTPUT_FRIENDLY,
45
        OUTPUT_JSON,
46
        _OUTPUT_MAX,
47
        _OUTPUT_INVALID = -EINVAL,
48
} Output;
49

50
static Output arg_output = _OUTPUT_INVALID;
51
static PagerFlags arg_pager_flags = 0;
52
static bool arg_legend = true;
53
static char** arg_services = NULL;
54
static UserDBFlags arg_userdb_flags = 0;
55
static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF;
56
static bool arg_chain = false;
57
static uint64_t arg_disposition_mask = UINT64_MAX;
58
static uid_t arg_uid_min = 0;
59
static uid_t arg_uid_max = UID_INVALID-1;
60
static bool arg_fuzzy = false;
61
static bool arg_boundaries = true;
62
static sd_json_variant *arg_from_file = NULL;
63

64
STATIC_DESTRUCTOR_REGISTER(arg_services, strv_freep);
313✔
65
STATIC_DESTRUCTOR_REGISTER(arg_from_file, sd_json_variant_unrefp);
313✔
66

67
static const char *output_table[_OUTPUT_MAX] = {
68
        [OUTPUT_CLASSIC]  = "classic",
69
        [OUTPUT_TABLE]    = "table",
70
        [OUTPUT_FRIENDLY] = "friendly",
71
        [OUTPUT_JSON]     = "json",
72
};
73

74
DEFINE_PRIVATE_STRING_TABLE_LOOKUP(output, Output);
8✔
75

76
static const char *user_disposition_to_color(UserDisposition d) {
520✔
77
        assert(d >= 0);
520✔
78
        assert(d < _USER_DISPOSITION_MAX);
520✔
79

80
        switch (d) {
520✔
81
        case USER_INTRINSIC:
82
                return ansi_red();
28✔
83

84
        case USER_SYSTEM:
85
        case USER_DYNAMIC:
86
                return ansi_green();
468✔
87

88
        case USER_CONTAINER:
89
        case USER_FOREIGN:
90
                return ansi_cyan();
10✔
91

92
        case USER_RESERVED:
UNCOV
93
                return ansi_red();
×
94

95
        default:
96
                return NULL;
97
        }
98
}
99

100
static const char* shell_to_color(const char *shell) {
232✔
101
        return !shell || is_nologin_shell(shell) ? ansi_grey() : NULL;
232✔
102
}
103

104
static int show_user(UserRecord *ur, Table *table) {
438✔
105
        int r;
438✔
106

107
        assert(ur);
438✔
108

109
        switch (arg_output) {
438✔
110

111
        case OUTPUT_CLASSIC:
26✔
112
                if (!uid_is_valid(ur->uid))
26✔
113
                        break;
114

115
                printf("%s:x:" UID_FMT ":" GID_FMT ":%s:%s:%s\n",
52✔
116
                       ur->user_name,
117
                       ur->uid,
118
                       user_record_gid(ur),
119
                       strempty(user_record_real_name(ur)),
26✔
120
                       user_record_home_directory(ur),
121
                       user_record_shell(ur));
122

123
                break;
26✔
124

125
        case OUTPUT_JSON:
96✔
126
                sd_json_variant_dump(ur->json, arg_json_format_flags, NULL, NULL);
96✔
127
                break;
96✔
128

129
        case OUTPUT_FRIENDLY:
84✔
130
                user_record_show(ur, true);
84✔
131

132
                if (ur->incomplete) {
84✔
UNCOV
133
                        fflush(stdout);
×
UNCOV
134
                        log_warning("Warning: lacking rights to acquire privileged fields of user record of '%s', output incomplete.", ur->user_name);
×
135
                }
136

137
                break;
138

139
        case OUTPUT_TABLE: {
232✔
140
                assert(table);
232✔
141
                UserDisposition d = user_record_disposition(ur);
232✔
142
                const char *sh = user_record_shell(ur);
232✔
143

144
                r = table_add_many(
400✔
145
                                table,
146
                                TABLE_STRING, "",
147
                                TABLE_STRING, ur->user_name,
148
                                TABLE_SET_COLOR, user_disposition_to_color(d),
149
                                TABLE_STRING, user_disposition_to_string(d),
150
                                TABLE_UID, ur->uid,
151
                                TABLE_GID, user_record_gid(ur),
152
                                TABLE_STRING, empty_to_null(ur->real_name),
153
                                TABLE_PATH, user_record_home_directory(ur),
154
                                TABLE_PATH, sh,
155
                                TABLE_SET_COLOR, shell_to_color(sh),
156
                                TABLE_INT, 0);
157
                if (r < 0)
232✔
UNCOV
158
                        return table_log_add_error(r);
×
159

160
                break;
161
        }
162

UNCOV
163
        default:
×
UNCOV
164
                assert_not_reached();
×
165
        }
166

167
        return 0;
168
}
169

170
static bool test_show_mapped(void) {
16✔
171
        /* Show mapped user range only in environments where user mapping is a thing. */
172
        return running_in_userns() > 0;
16✔
173
}
174

175
static const struct {
176
        uid_t first, last;
177
        const char *name;
178
        UserDisposition disposition;
179
        bool (*test)(void);
180
} uid_range_table[] = {
181
        {
182
                .first = 1,
183
                .last = SYSTEM_UID_MAX,
184
                .name = "system",
185
                .disposition = USER_SYSTEM,
186
        },
187
        {
188
                .first = GREETER_UID_MIN,
189
                .last = GREETER_UID_MAX,
190
                .name = "dynamic greeter",
191
                .disposition = USER_DYNAMIC,
192
        },
193
        {
194
                .first = DYNAMIC_UID_MIN,
195
                .last = DYNAMIC_UID_MAX,
196
                .name = "dynamic system",
197
                .disposition = USER_DYNAMIC,
198
        },
199
        {
200
                .first = CONTAINER_UID_MIN,
201
                .last = CONTAINER_UID_MAX,
202
                .name = "container",
203
                .disposition = USER_CONTAINER,
204
        },
205
        {
206
                .first = FOREIGN_UID_MIN,
207
                .last = FOREIGN_UID_MAX,
208
                .name = "foreign",
209
                .disposition = USER_FOREIGN,
210
        },
211
#if ENABLE_HOMED
212
        {
213
                .first = HOME_UID_MIN,
214
                .last = HOME_UID_MAX,
215
                .name = "systemd-homed",
216
                .disposition = USER_REGULAR,
217
        },
218
#endif
219
        {
220
                .first = MAP_UID_MIN,
221
                .last = MAP_UID_MAX,
222
                .name = "mapped",
223
                .disposition = USER_REGULAR,
224
                .test = test_show_mapped,
225
        },
226
};
227

228
static int table_add_uid_boundaries(Table *table, const UIDRange *p) {
13✔
229
        int r, n_added = 0;
13✔
230

231
        assert(table);
13✔
232

233
        FOREACH_ELEMENT(i, uid_range_table) {
104✔
234
                _cleanup_free_ char *name = NULL, *comment = NULL;
53✔
235

236
                if (!BIT_SET(arg_disposition_mask, i->disposition))
91✔
237
                        continue;
28✔
238

239
                if (!uid_range_covers(p, i->first, i->last - i->first + 1))
63✔
UNCOV
240
                        continue;
×
241

242
                if (i->test && !i->test())
63✔
243
                        continue;
10✔
244

245
                name = strjoin(glyph(GLYPH_ARROW_DOWN),
53✔
246
                               " begin ", i->name, " users ",
247
                               glyph(GLYPH_ARROW_DOWN));
248
                if (!name)
53✔
UNCOV
249
                        return log_oom();
×
250

251
                comment = strjoin("First ", i->name, " user");
53✔
252
                if (!comment)
53✔
UNCOV
253
                        return log_oom();
×
254

255
                r = table_add_many(
53✔
256
                                table,
257
                                TABLE_STRING, glyph(GLYPH_TREE_TOP),
258
                                TABLE_STRING, name,
259
                                TABLE_SET_COLOR, ansi_grey(),
260
                                TABLE_STRING, user_disposition_to_string(i->disposition),
261
                                TABLE_SET_COLOR, ansi_grey(),
262
                                TABLE_UID, i->first,
263
                                TABLE_SET_COLOR, ansi_grey(),
264
                                TABLE_EMPTY,
265
                                TABLE_STRING, comment,
266
                                TABLE_SET_COLOR, ansi_grey(),
267
                                TABLE_EMPTY,
268
                                TABLE_EMPTY,
269
                                TABLE_INT, -1); /* sort before any other entry with the same UID */
270
                if (r < 0)
53✔
271
                        return table_log_add_error(r);
×
272

273
                free(name);
53✔
274
                name = strjoin(glyph(GLYPH_ARROW_UP),
53✔
275
                               " end ", i->name, " users ",
276
                               glyph(GLYPH_ARROW_UP));
277
                if (!name)
53✔
UNCOV
278
                        return log_oom();
×
279

280
                free(comment);
53✔
281
                comment = strjoin("Last ", i->name, " user");
53✔
282
                if (!comment)
53✔
UNCOV
283
                        return log_oom();
×
284

285
                r = table_add_many(
53✔
286
                                table,
287
                                TABLE_STRING, glyph(GLYPH_TREE_RIGHT),
288
                                TABLE_STRING, name,
289
                                TABLE_SET_COLOR, ansi_grey(),
290
                                TABLE_STRING, user_disposition_to_string(i->disposition),
291
                                TABLE_SET_COLOR, ansi_grey(),
292
                                TABLE_UID, i->last,
293
                                TABLE_SET_COLOR, ansi_grey(),
294
                                TABLE_EMPTY,
295
                                TABLE_STRING, comment,
296
                                TABLE_SET_COLOR, ansi_grey(),
297
                                TABLE_EMPTY,
298
                                TABLE_EMPTY,
299
                                TABLE_INT, 1); /* sort after any other entry with the same UID */
300
                if (r < 0)
53✔
301
                        return table_log_add_error(r);
×
302

303
                n_added += 2;
53✔
304
        }
305

306
        return n_added;
307
}
308

UNCOV
309
static int add_unavailable_uid(Table *table, uid_t start, uid_t end) {
×
310
        _cleanup_free_ char *name = NULL;
×
UNCOV
311
        int r;
×
312

UNCOV
313
        assert(table);
×
UNCOV
314
        assert(start <= end);
×
315

UNCOV
316
        name = strjoin(glyph(GLYPH_ARROW_DOWN),
×
317
                       " begin unavailable users ",
318
                       glyph(GLYPH_ARROW_DOWN));
UNCOV
319
        if (!name)
×
UNCOV
320
                return log_oom();
×
321

UNCOV
322
        r = table_add_many(
×
323
                        table,
324
                        TABLE_STRING, glyph(GLYPH_TREE_TOP),
325
                        TABLE_STRING, name,
326
                        TABLE_SET_COLOR, ansi_grey(),
327
                        TABLE_EMPTY,
328
                        TABLE_UID, start,
329
                        TABLE_SET_COLOR, ansi_grey(),
330
                        TABLE_EMPTY,
331
                        TABLE_STRING, "First unavailable user",
332
                        TABLE_SET_COLOR, ansi_grey(),
333
                        TABLE_EMPTY,
334
                        TABLE_EMPTY,
335
                        TABLE_INT, -1); /* sort before an other entry with the same UID */
UNCOV
336
        if (r < 0)
×
UNCOV
337
                return table_log_add_error(r);
×
338

UNCOV
339
        free(name);
×
UNCOV
340
        name = strjoin(glyph(GLYPH_ARROW_UP),
×
341
                       " end unavailable users ",
342
                       glyph(GLYPH_ARROW_UP));
UNCOV
343
        if (!name)
×
UNCOV
344
                return log_oom();
×
345

UNCOV
346
        r = table_add_many(
×
347
                        table,
348
                        TABLE_STRING, glyph(GLYPH_TREE_RIGHT),
349
                        TABLE_STRING, name,
350
                        TABLE_SET_COLOR, ansi_grey(),
351
                        TABLE_EMPTY,
352
                        TABLE_UID, end,
353
                        TABLE_SET_COLOR, ansi_grey(),
354
                        TABLE_EMPTY,
355
                        TABLE_STRING, "Last unavailable user",
356
                        TABLE_SET_COLOR, ansi_grey(),
357
                        TABLE_EMPTY,
358
                        TABLE_EMPTY,
359
                        TABLE_INT, 1); /* sort after any other entry with the same UID */
UNCOV
360
        if (r < 0)
×
UNCOV
361
                return table_log_add_error(r);
×
362

363
        return 2;
364
}
365

366
static int table_add_uid_map(
22✔
367
                Table *table,
368
                const UIDRange *p,
369
                int (*add_unavailable)(Table *t, uid_t start, uid_t end)) {
370

371
        uid_t focus = 0;
22✔
372
        int n_added = 0, r;
22✔
373

374
        assert(table);
22✔
375
        assert(add_unavailable);
22✔
376

377
        if (!p)
22✔
378
                return 0;
379

380
        FOREACH_ARRAY(x, p->entries, p->n_entries) {
44✔
381
                if (focus < x->start) {
22✔
UNCOV
382
                        r = add_unavailable(table, focus, x->start-1);
×
UNCOV
383
                        if (r < 0)
×
384
                                return r;
385

386
                        n_added += r;
×
387
                }
388

389
                if (x->start > UINT32_MAX - x->nr) { /* overflow check */
22✔
390
                        focus = UINT32_MAX;
391
                        break;
392
                }
393

394
                focus = x->start + x->nr;
22✔
395
        }
396

397
        if (focus < UINT32_MAX-1) {
22✔
UNCOV
398
                r = add_unavailable(table, focus, UINT32_MAX-1);
×
UNCOV
399
                if (r < 0)
×
400
                        return r;
401

UNCOV
402
                n_added += r;
×
403
        }
404

405
        return n_added;
406
}
407

408
static int display_user(int argc, char *argv[], void *userdata) {
104✔
409
        _cleanup_(table_unrefp) Table *table = NULL;
104✔
410
        bool draw_separator = false;
104✔
411
        int ret = 0, r;
104✔
412

413
        if (arg_output < 0)
104✔
414
                arg_output = arg_from_file || (argc > 1 && !arg_fuzzy) ? OUTPUT_FRIENDLY : OUTPUT_TABLE;
94✔
415

416
        if (arg_output == OUTPUT_TABLE) {
104✔
417
                table = table_new(" ", "name", "disposition", "uid", "gid", "realname", "home", "shell", "order");
14✔
418
                if (!table)
14✔
UNCOV
419
                        return log_oom();
×
420

421
                (void) table_set_align_percent(table, table_get_cell(table, 0, 3), 100);
14✔
422
                (void) table_set_align_percent(table, table_get_cell(table, 0, 4), 100);
14✔
423
                table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
14✔
424
                (void) table_set_sort(table, (size_t) 3, (size_t) 8);
14✔
425
                (void) table_hide_column_from_display(table, (size_t) 8);
14✔
426
                if (!arg_boundaries)
14✔
427
                        (void) table_hide_column_from_display(table, (size_t) 0);
1✔
428
        }
429

430
        _cleanup_(userdb_match_done) UserDBMatch match = {
104✔
431
                .disposition_mask = arg_disposition_mask,
432
                .uid_min = arg_uid_min,
433
                .uid_max = arg_uid_max,
434
        };
435

436
        if (arg_from_file) {
104✔
437
                if (argc > 1)
3✔
UNCOV
438
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No argument expected when invoked with --from-file=, refusing.");
×
439

440
                _cleanup_(user_record_unrefp) UserRecord *ur = user_record_new();
6✔
441
                if (!ur)
3✔
UNCOV
442
                        return log_oom();
×
443

444
                r = user_record_load(ur, arg_from_file, USER_RECORD_LOAD_MASK_SECRET|USER_RECORD_LOG);
3✔
445
                if (r < 0)
3✔
446
                        return r;
447

448
                r = show_user(ur, table);
3✔
449
                if (r < 0)
3✔
450
                        return r;
451

452
        } else if (argc > 1 && !arg_fuzzy)
101✔
453
                STRV_FOREACH(i, argv + 1) {
175✔
454
                        _cleanup_(user_record_unrefp) UserRecord *ur = NULL;
93✔
455

456
                        r = userdb_by_name(*i, &match, arg_userdb_flags|USERDB_PARSE_NUMERIC, &ur);
93✔
457
                        if (r < 0) {
93✔
458
                                if (r == -ESRCH)
20✔
459
                                        log_error_errno(r, "User %s does not exist.", *i);
16✔
460
                                else if (r == -EHOSTDOWN)
4✔
UNCOV
461
                                        log_error_errno(r, "Selected user database service is not available for this request.");
×
462
                                else if (r == -ENOEXEC)
4✔
UNCOV
463
                                        log_error_errno(r, "User '%s' exists but does not match specified filter.", *i);
×
464
                                else
465
                                        log_error_errno(r, "Failed to find user %s: %m", *i);
4✔
466

467
                                RET_GATHER(ret, r);
20✔
468
                        } else {
469
                                if (draw_separator && arg_output == OUTPUT_FRIENDLY)
73✔
470
                                        putchar('\n');
4✔
471

472
                                r = show_user(ur, table);
73✔
473
                                if (r < 0)
73✔
UNCOV
474
                                        return r;
×
475

476
                                draw_separator = true;
477
                        }
478
                }
479
        else {
480
                if (argc > 1) {
1✔
481
                        /* If there are further arguments, they are the fuzzy match strings. */
482
                        match.fuzzy_names = strv_copy(strv_skip(argv, 1));
1✔
483
                        if (!match.fuzzy_names)
1✔
UNCOV
484
                                return log_oom();
×
485
                }
486

UNCOV
487
                _cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL;
×
488
                r = userdb_all(&match, arg_userdb_flags, &iterator);
19✔
489
                if (r == -ENOLINK) /* ENOLINK → Didn't find answer without Varlink, and didn't try Varlink because was configured to off. */
19✔
UNCOV
490
                        log_debug_errno(r, "No entries found. (Didn't check via Varlink.)");
×
491
                else if (r == -ESRCH) /* ESRCH → Couldn't find any suitable entry, but we checked all sources */
19✔
492
                        log_debug_errno(r, "No entries found.");
19✔
493
                else if (r < 0)
19✔
UNCOV
494
                        return log_error_errno(r, "Failed to enumerate users: %m");
×
495
                else {
496
                        for (;;) {
362✔
497
                                _cleanup_(user_record_unrefp) UserRecord *ur = NULL;
19✔
498

499
                                r = userdb_iterator_get(iterator, &match, &ur);
381✔
500
                                if (r == -ESRCH)
381✔
501
                                        break;
502
                                if (r == -EHOSTDOWN)
362✔
UNCOV
503
                                        return log_error_errno(r, "Selected user database service is not available for this request.");
×
504
                                if (r < 0)
362✔
UNCOV
505
                                        return log_error_errno(r, "Failed to acquire next user: %m");
×
506

507
                                if (draw_separator && arg_output == OUTPUT_FRIENDLY)
362✔
508
                                        putchar('\n');
25✔
509

510
                                r = show_user(ur, table);
362✔
511
                                if (r < 0)
362✔
512
                                        return r;
513

514
                                draw_separator = true;
362✔
515
                        }
516
                }
517
        }
518

519
        if (table) {
104✔
520
                int boundary_lines = 0, uid_map_lines = 0;
14✔
521

522
                if (arg_boundaries) {
14✔
523
                        _cleanup_(uid_range_freep) UIDRange *uid_range = NULL;
13✔
524

525
                        r = uid_range_load_userns(/* path = */ NULL, UID_RANGE_USERNS_INSIDE, &uid_range);
13✔
526
                        if (r < 0)
13✔
UNCOV
527
                                log_debug_errno(r, "Failed to load /proc/self/uid_map, ignoring: %m");
×
528

529
                        boundary_lines = table_add_uid_boundaries(table, uid_range);
13✔
530
                        if (boundary_lines < 0)
13✔
531
                                return boundary_lines;
532

533
                        uid_map_lines = table_add_uid_map(table, uid_range, add_unavailable_uid);
13✔
534
                        if (uid_map_lines < 0)
13✔
535
                                return uid_map_lines;
536
                }
537

538
                if (!table_isempty(table)) {
28✔
539
                        r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
14✔
540
                        if (r < 0)
14✔
541
                                return r;
542
                }
543

544
                if (arg_legend) {
14✔
545
                        size_t k;
12✔
546

547
                        k = table_get_rows(table) - 1 - boundary_lines - uid_map_lines;
12✔
548
                        if (k > 0)
12✔
549
                                printf("\n%zu users listed.\n", k);
10✔
550
                        else
551
                                printf("No users.\n");
2✔
552
                }
553
        }
554

555
        return ret;
556
}
557

558
static int show_group(GroupRecord *gr, Table *table) {
312✔
559
        int r;
312✔
560

561
        assert(gr);
312✔
562

563
        switch (arg_output) {
312✔
564

UNCOV
565
        case OUTPUT_CLASSIC: {
×
UNCOV
566
                _cleanup_free_ char *m = NULL;
×
567

UNCOV
568
                if (!gid_is_valid(gr->gid))
×
569
                        break;
570

UNCOV
571
                m = strv_join(gr->members, ",");
×
UNCOV
572
                if (!m)
×
UNCOV
573
                        return log_oom();
×
574

UNCOV
575
                printf("%s:x:" GID_FMT ":%s\n",
×
576
                       gr->group_name,
577
                       gr->gid,
578
                       m);
579
                break;
580
        }
581

582
        case OUTPUT_JSON:
5✔
583
                sd_json_variant_dump(gr->json, arg_json_format_flags, NULL, NULL);
5✔
584
                break;
5✔
585

586
        case OUTPUT_FRIENDLY:
19✔
587
                group_record_show(gr, true);
19✔
588

589
                if (gr->incomplete) {
19✔
UNCOV
590
                        fflush(stdout);
×
UNCOV
591
                        log_warning("Warning: lacking rights to acquire privileged fields of group record of '%s', output incomplete.", gr->group_name);
×
592
                }
593

594
                break;
595

596
        case OUTPUT_TABLE: {
288✔
597
                UserDisposition d;
288✔
598

599
                assert(table);
288✔
600
                d = group_record_disposition(gr);
288✔
601

602
                r = table_add_many(
288✔
603
                                table,
604
                                TABLE_STRING, "",
605
                                TABLE_STRING, gr->group_name,
606
                                TABLE_SET_COLOR, user_disposition_to_color(d),
607
                                TABLE_STRING, user_disposition_to_string(d),
608
                                TABLE_GID, gr->gid,
609
                                TABLE_STRING, gr->description,
610
                                TABLE_INT, 0);
611
                if (r < 0)
288✔
UNCOV
612
                        return table_log_add_error(r);
×
613

614
                break;
615
        }
616

UNCOV
617
        default:
×
UNCOV
618
                assert_not_reached();
×
619
        }
620

621
        return 0;
622
}
623

624
static int table_add_gid_boundaries(Table *table, const UIDRange *p) {
9✔
625
        int r, n_added = 0;
9✔
626

627
        assert(table);
9✔
628

629
        FOREACH_ELEMENT(i, uid_range_table) {
72✔
630
                _cleanup_free_ char *name = NULL, *comment = NULL;
29✔
631

632
                if (!BIT_SET(arg_disposition_mask, i->disposition))
63✔
633
                        continue;
28✔
634

635
                if (!uid_range_covers(p, i->first, i->last - i->first + 1))
35✔
UNCOV
636
                        continue;
×
637

638
                if (i->test && !i->test())
35✔
639
                        continue;
6✔
640

641
                name = strjoin(glyph(GLYPH_ARROW_DOWN),
29✔
642
                               " begin ", i->name, " groups ",
643
                               glyph(GLYPH_ARROW_DOWN));
644
                if (!name)
29✔
UNCOV
645
                        return log_oom();
×
646

647
                comment = strjoin("First ", i->name, " group");
29✔
648
                if (!comment)
29✔
UNCOV
649
                        return log_oom();
×
650

651
                r = table_add_many(
29✔
652
                                table,
653
                                TABLE_STRING, glyph(GLYPH_TREE_TOP),
654
                                TABLE_STRING, name,
655
                                TABLE_SET_COLOR, ansi_grey(),
656
                                TABLE_STRING, user_disposition_to_string(i->disposition),
657
                                TABLE_SET_COLOR, ansi_grey(),
658
                                TABLE_GID, i->first,
659
                                TABLE_SET_COLOR, ansi_grey(),
660
                                TABLE_STRING, comment,
661
                                TABLE_SET_COLOR, ansi_grey(),
662
                                TABLE_INT, -1); /* sort before any other entry with the same GID */
663
                if (r < 0)
29✔
664
                        return table_log_add_error(r);
×
665

666
                free(name);
29✔
667
                name = strjoin(glyph(GLYPH_ARROW_UP),
29✔
668
                               " end ", i->name, " groups ",
669
                               glyph(GLYPH_ARROW_UP));
670
                if (!name)
29✔
UNCOV
671
                        return log_oom();
×
672

673
                free(comment);
29✔
674
                comment = strjoin("Last ", i->name, " group");
29✔
675
                if (!comment)
29✔
UNCOV
676
                        return log_oom();
×
677

678
                r = table_add_many(
29✔
679
                                table,
680
                                TABLE_STRING, glyph(GLYPH_TREE_RIGHT),
681
                                TABLE_STRING, name,
682
                                TABLE_SET_COLOR, ansi_grey(),
683
                                TABLE_STRING, user_disposition_to_string(i->disposition),
684
                                TABLE_SET_COLOR, ansi_grey(),
685
                                TABLE_GID, i->last,
686
                                TABLE_SET_COLOR, ansi_grey(),
687
                                TABLE_STRING, comment,
688
                                TABLE_SET_COLOR, ansi_grey(),
689
                                TABLE_INT, 1); /* sort after any other entry with the same GID */
690
                if (r < 0)
29✔
691
                        return table_log_add_error(r);
×
692

693
                n_added += 2;
29✔
694
        }
695

696
        return n_added;
697
}
698

UNCOV
699
static int add_unavailable_gid(Table *table, uid_t start, uid_t end) {
×
700
        _cleanup_free_ char *name = NULL;
×
UNCOV
701
        int r;
×
702

UNCOV
703
        assert(table);
×
UNCOV
704
        assert(start <= end);
×
705

UNCOV
706
        name = strjoin(glyph(GLYPH_ARROW_DOWN),
×
707
                       " begin unavailable groups ",
708
                       glyph(GLYPH_ARROW_DOWN));
UNCOV
709
        if (!name)
×
UNCOV
710
                return log_oom();
×
711

712
        r = table_add_many(
×
713
                        table,
714
                        TABLE_STRING, glyph(GLYPH_TREE_TOP),
715
                        TABLE_STRING, name,
716
                        TABLE_SET_COLOR, ansi_grey(),
717
                        TABLE_EMPTY,
718
                        TABLE_GID, start,
719
                        TABLE_SET_COLOR, ansi_grey(),
720
                        TABLE_STRING, "First unavailable group",
721
                        TABLE_SET_COLOR, ansi_grey(),
722
                        TABLE_INT, -1); /* sort before any other entry with the same GID */
UNCOV
723
        if (r < 0)
×
UNCOV
724
                return table_log_add_error(r);
×
725

UNCOV
726
        free(name);
×
UNCOV
727
        name = strjoin(glyph(GLYPH_ARROW_UP),
×
728
                       " end unavailable groups ",
729
                       glyph(GLYPH_ARROW_UP));
UNCOV
730
        if (!name)
×
UNCOV
731
                return log_oom();
×
732

733
        r = table_add_many(
×
734
                        table,
735
                        TABLE_STRING, glyph(GLYPH_TREE_RIGHT),
736
                        TABLE_STRING, name,
737
                        TABLE_SET_COLOR, ansi_grey(),
738
                        TABLE_EMPTY,
739
                        TABLE_GID, end,
740
                        TABLE_SET_COLOR, ansi_grey(),
741
                        TABLE_STRING, "Last unavailable group",
742
                        TABLE_SET_COLOR, ansi_grey(),
743
                        TABLE_INT, 1); /* sort after any other entry with the same GID */
UNCOV
744
        if (r < 0)
×
UNCOV
745
                return table_log_add_error(r);
×
746

747
        return 2;
748
}
749

750
static int display_group(int argc, char *argv[], void *userdata) {
36✔
751
        _cleanup_(table_unrefp) Table *table = NULL;
36✔
752
        bool draw_separator = false;
36✔
753
        int ret = 0, r;
36✔
754

755
        if (arg_output < 0)
36✔
756
                arg_output = arg_from_file || (argc > 1 && !arg_fuzzy) ? OUTPUT_FRIENDLY : OUTPUT_TABLE;
42✔
757

758
        if (arg_output == OUTPUT_TABLE) {
36✔
759
                table = table_new(" ", "name", "disposition", "gid", "description", "order");
10✔
760
                if (!table)
10✔
UNCOV
761
                        return log_oom();
×
762

763
                (void) table_set_align_percent(table, table_get_cell(table, 0, 3), 100);
10✔
764
                table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
10✔
765
                (void) table_set_sort(table, (size_t) 3, (size_t) 5);
10✔
766
                (void) table_hide_column_from_display(table, (size_t) 5);
10✔
767
                if (!arg_boundaries)
10✔
768
                        (void) table_hide_column_from_display(table, (size_t) 0);
1✔
769
        }
770

771
        _cleanup_(userdb_match_done) UserDBMatch match = {
36✔
772
                .disposition_mask = arg_disposition_mask,
773
                .gid_min = arg_uid_min,
774
                .gid_max = arg_uid_max,
775
        };
776

777
        if (arg_from_file) {
36✔
778
                if (argc > 1)
3✔
UNCOV
779
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No argument expected when invoked with --from-file=, refusing.");
×
780

781
                _cleanup_(group_record_unrefp) GroupRecord *gr = group_record_new();
6✔
782
                if (!gr)
3✔
UNCOV
783
                        return log_oom();
×
784

785
                r = group_record_load(gr, arg_from_file, USER_RECORD_LOAD_MASK_SECRET|USER_RECORD_LOG);
3✔
786
                if (r < 0)
3✔
787
                        return r;
788

789
                r = show_group(gr, table);
3✔
790
                if (r < 0)
3✔
791
                        return r;
792

793
        } else if (argc > 1 && !arg_fuzzy)
33✔
794
                STRV_FOREACH(i, argv + 1) {
56✔
795
                        _cleanup_(group_record_unrefp) GroupRecord *gr = NULL;
33✔
796

797
                        r = groupdb_by_name(*i, &match, arg_userdb_flags|USERDB_PARSE_NUMERIC, &gr);
33✔
798
                        if (r < 0) {
33✔
799
                                if (r == -ESRCH)
12✔
800
                                        log_error_errno(r, "Group %s does not exist.", *i);
10✔
801
                                else if (r == -EHOSTDOWN)
2✔
UNCOV
802
                                        log_error_errno(r, "Selected group database service is not available for this request.");
×
803
                                else if (r == -ENOEXEC)
2✔
UNCOV
804
                                        log_error_errno(r, "Group '%s' exists but does not match specified filter.", *i);
×
805
                                else
806
                                        log_error_errno(r, "Failed to find group %s: %m", *i);
2✔
807

808
                                RET_GATHER(ret, r);
12✔
809
                        } else {
810
                                if (draw_separator && arg_output == OUTPUT_FRIENDLY)
21✔
811
                                        putchar('\n');
4✔
812

813
                                r = show_group(gr, table);
21✔
814
                                if (r < 0)
21✔
815
                                        return r;
×
816

817
                                draw_separator = true;
818
                        }
819
                }
820
        else {
821
                if (argc > 1) {
1✔
822
                        match.fuzzy_names = strv_copy(strv_skip(argv, 1));
1✔
823
                        if (!match.fuzzy_names)
1✔
UNCOV
824
                                return log_oom();
×
825
                }
826

UNCOV
827
                _cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL;
×
828
                r = groupdb_all(&match, arg_userdb_flags, &iterator);
10✔
829
                if (r == -ENOLINK)
10✔
UNCOV
830
                        log_debug_errno(r, "No entries found. (Didn't check via Varlink.)");
×
831
                else if (r == -ESRCH)
10✔
832
                        log_debug_errno(r, "No entries found.");
10✔
833
                else if (r < 0)
10✔
UNCOV
834
                        return log_error_errno(r, "Failed to enumerate groups: %m");
×
835
                else {
836
                        for (;;) {
288✔
837
                                _cleanup_(group_record_unrefp) GroupRecord *gr = NULL;
10✔
838

839
                                r = groupdb_iterator_get(iterator, &match, &gr);
298✔
840
                                if (r == -ESRCH)
298✔
841
                                        break;
842
                                if (r == -EHOSTDOWN)
288✔
UNCOV
843
                                        return log_error_errno(r, "Selected group database service is not available for this request.");
×
844
                                if (r < 0)
288✔
UNCOV
845
                                        return log_error_errno(r, "Failed to acquire next group: %m");
×
846

847
                                if (draw_separator && arg_output == OUTPUT_FRIENDLY)
288✔
UNCOV
848
                                        putchar('\n');
×
849

850
                                r = show_group(gr, table);
288✔
851
                                if (r < 0)
288✔
852
                                        return r;
853

854
                                draw_separator = true;
288✔
855
                        }
856
                }
857
        }
858

859
        if (table) {
36✔
860
                int boundary_lines = 0, gid_map_lines = 0;
10✔
861

862
                if (arg_boundaries) {
10✔
863
                        _cleanup_(uid_range_freep) UIDRange *gid_range = NULL;
9✔
864
                        r = uid_range_load_userns(/* path = */ NULL, GID_RANGE_USERNS_INSIDE, &gid_range);
9✔
865
                        if (r < 0)
9✔
UNCOV
866
                                log_debug_errno(r, "Failed to load /proc/self/gid_map, ignoring: %m");
×
867

868
                        boundary_lines = table_add_gid_boundaries(table, gid_range);
9✔
869
                        if (boundary_lines < 0)
9✔
870
                                return boundary_lines;
871

872
                        gid_map_lines = table_add_uid_map(table, gid_range, add_unavailable_gid);
9✔
873
                        if (gid_map_lines < 0)
9✔
874
                                return gid_map_lines;
875
                }
876

877
                if (!table_isempty(table)) {
20✔
878
                        r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
10✔
879
                        if (r < 0)
10✔
880
                                return r;
881
                }
882

883
                if (arg_legend) {
10✔
884
                        size_t k;
9✔
885

886
                        k = table_get_rows(table) - 1 - boundary_lines - gid_map_lines;
9✔
887
                        if (k > 0)
9✔
888
                                printf("\n%zu groups listed.\n", k);
7✔
889
                        else
890
                                printf("No groups.\n");
2✔
891
                }
892
        }
893

894
        return ret;
895
}
896

897
static int show_membership(const char *user, const char *group, Table *table) {
14✔
898
        int r;
14✔
899

900
        assert(user);
14✔
901
        assert(group);
14✔
902

903
        switch (arg_output) {
14✔
904

UNCOV
905
        case OUTPUT_CLASSIC:
×
906
                /* Strictly speaking there's no 'classic' output for this concept, but let's output it in
907
                 * similar style to the classic output for user/group info */
908

UNCOV
909
                printf("%s:%s\n", user, group);
×
UNCOV
910
                break;
×
911

912
        case OUTPUT_JSON: {
2✔
913
                _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
2✔
914

915
                r = sd_json_buildo(
2✔
916
                                &v,
917
                                SD_JSON_BUILD_PAIR("user", SD_JSON_BUILD_STRING(user)),
918
                                SD_JSON_BUILD_PAIR("group", SD_JSON_BUILD_STRING(group)));
919
                if (r < 0)
2✔
UNCOV
920
                        return log_error_errno(r, "Failed to build JSON object: %m");
×
921

922
                sd_json_variant_dump(v, arg_json_format_flags, NULL, NULL);
2✔
923
                break;
2✔
924
        }
925

UNCOV
926
        case OUTPUT_FRIENDLY:
×
927
                /* Hmm, this is not particularly friendly, but not sure how we could do this better */
UNCOV
928
                printf("%s: %s\n", group, user);
×
UNCOV
929
                break;
×
930

931
        case OUTPUT_TABLE:
12✔
932
                assert(table);
12✔
933

934
                r = table_add_many(
12✔
935
                                table,
936
                                TABLE_STRING, user,
937
                                TABLE_STRING, group);
938
                if (r < 0)
12✔
UNCOV
939
                        return table_log_add_error(r);
×
940

941
                break;
942

943
        default:
×
UNCOV
944
                assert_not_reached();
×
945
        }
946

947
        return 0;
948
}
949

950
static int display_memberships(int argc, char *argv[], void *userdata) {
14✔
951
        _cleanup_(table_unrefp) Table *table = NULL;
14✔
952
        int ret = 0, r;
14✔
953

954
        if (arg_from_file)
14✔
UNCOV
955
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--from-file= not supported when showing memberships, refusing.");
×
956

957
        if (arg_output < 0)
14✔
958
                arg_output = OUTPUT_TABLE;
12✔
959

960
        if (arg_output == OUTPUT_TABLE) {
14✔
961
                table = table_new("user", "group");
12✔
962
                if (!table)
12✔
UNCOV
963
                        return log_oom();
×
964

965
                (void) table_set_sort(table, (size_t) 0, (size_t) 1);
12✔
966
        }
967

968
        if (argc > 1)
14✔
969
                STRV_FOREACH(i, argv + 1) {
26✔
970
                        _cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL;
4✔
971

972
                        if (streq(argv[0], "users-in-group")) {
18✔
973
                                r = membershipdb_by_group(*i, arg_userdb_flags, &iterator);
9✔
974
                                if (r < 0)
9✔
975
                                        return log_error_errno(r, "Failed to enumerate users in group: %m");
2✔
976
                        } else if (streq(argv[0], "groups-of-user")) {
9✔
977
                                r = membershipdb_by_user(*i, arg_userdb_flags, &iterator);
9✔
978
                                if (r < 0)
9✔
979
                                        return log_error_errno(r, "Failed to enumerate groups of user: %m");
2✔
980
                        } else
UNCOV
981
                                assert_not_reached();
×
982

983
                        for (;;) {
22✔
984
                                _cleanup_free_ char *user = NULL, *group = NULL;
18✔
985

986
                                r = membershipdb_iterator_get(iterator, &user, &group);
18✔
987
                                if (r == -ESRCH)
18✔
988
                                        break;
989
                                if (r == -EHOSTDOWN)
4✔
UNCOV
990
                                        return log_error_errno(r, "Selected membership database service is not available for this request.");
×
991
                                if (r < 0)
4✔
992
                                        return log_error_errno(r, "Failed to acquire next membership: %m");
×
993

994
                                r = show_membership(user, group, table);
4✔
995
                                if (r < 0)
4✔
996
                                        return r;
997
                        }
998
                }
999
        else {
UNCOV
1000
                _cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL;
×
1001

1002
                r = membershipdb_all(arg_userdb_flags, &iterator);
2✔
1003
                if (r == -ENOLINK)
2✔
UNCOV
1004
                        log_debug_errno(r, "No entries found. (Didn't check via Varlink.)");
×
1005
                else if (r == -ESRCH)
2✔
1006
                        log_debug_errno(r, "No entries found.");
2✔
1007
                else if (r < 0)
2✔
UNCOV
1008
                        return log_error_errno(r, "Failed to enumerate memberships: %m");
×
1009
                else {
1010
                        for (;;) {
22✔
1011
                                _cleanup_free_ char *user = NULL, *group = NULL;
12✔
1012

1013
                                r = membershipdb_iterator_get(iterator, &user, &group);
12✔
1014
                                if (r == -ESRCH)
12✔
1015
                                        break;
1016
                                if (r == -EHOSTDOWN)
10✔
UNCOV
1017
                                        return log_error_errno(r, "Selected membership database service is not available for this request.");
×
1018
                                if (r < 0)
10✔
UNCOV
1019
                                        return log_error_errno(r, "Failed to acquire next membership: %m");
×
1020

1021
                                r = show_membership(user, group, table);
10✔
1022
                                if (r < 0)
10✔
1023
                                        return r;
1024
                        }
1025
                }
1026
        }
1027

1028
        if (table) {
10✔
1029
                if (!table_isempty(table)) {
8✔
1030
                        r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
4✔
1031
                        if (r < 0)
4✔
1032
                                return r;
1033
                }
1034

1035
                if (arg_legend) {
8✔
1036
                        if (table_isempty(table))
16✔
1037
                                printf("No memberships.\n");
4✔
1038
                        else
1039
                                printf("\n%zu memberships listed.\n", table_get_rows(table) - 1);
4✔
1040
                }
1041
        }
1042

1043
        return ret;
1044
}
1045

1046
static int display_services(int argc, char *argv[], void *userdata) {
2✔
UNCOV
1047
        _cleanup_(table_unrefp) Table *t = NULL;
×
1048
        _cleanup_closedir_ DIR *d = NULL;
2✔
1049
        int r;
2✔
1050

1051
        if (arg_from_file)
2✔
UNCOV
1052
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--from-file= not supported when showing services, refusing.");
×
1053

1054
        d = opendir("/run/systemd/userdb/");
2✔
1055
        if (!d) {
2✔
UNCOV
1056
                if (errno == ENOENT) {
×
UNCOV
1057
                        log_info("No services.");
×
UNCOV
1058
                        return 0;
×
1059
                }
1060

UNCOV
1061
                return log_error_errno(errno, "Failed to open %s: %m", "/run/systemd/userdb/");
×
1062
        }
1063

1064
        t = table_new("service", "listening");
2✔
1065
        if (!t)
2✔
UNCOV
1066
                return log_oom();
×
1067

1068
        (void) table_set_sort(t, (size_t) 0);
2✔
1069

1070
        FOREACH_DIRENT(de, d, return -errno) {
18✔
1071
                _cleanup_free_ char *j = NULL, *no = NULL;
12✔
1072
                _cleanup_close_ int fd = -EBADF;
12✔
1073

1074
                j = path_join("/run/systemd/userdb/", de->d_name);
12✔
1075
                if (!j)
12✔
UNCOV
1076
                        return log_oom();
×
1077

1078
                fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
12✔
1079
                if (fd < 0)
12✔
UNCOV
1080
                        return log_error_errno(errno, "Failed to allocate AF_UNIX/SOCK_STREAM socket: %m");
×
1081

1082
                r = connect_unix_path(fd, dirfd(d), de->d_name);
12✔
1083
                if (r < 0) {
12✔
UNCOV
1084
                        no = strjoin("No (", ERRNO_NAME(r), ")");
×
UNCOV
1085
                        if (!no)
×
UNCOV
1086
                                return log_oom();
×
1087
                }
1088

1089
                r = table_add_many(t,
24✔
1090
                                   TABLE_STRING, de->d_name,
1091
                                   TABLE_STRING, no ?: "yes",
1092
                                   TABLE_SET_COLOR, ansi_highlight_green_red(!no));
1093
                if (r < 0)
12✔
UNCOV
1094
                        return table_log_add_error(r);
×
1095
        }
1096

1097
        if (!table_isempty(t)) {
4✔
1098
                r = table_print_with_pager(t, arg_json_format_flags, arg_pager_flags, arg_legend);
2✔
1099
                if (r < 0)
2✔
1100
                        return r;
1101
        }
1102

1103
        if (arg_legend && arg_output != OUTPUT_JSON) {
2✔
1104
                if (table_isempty(t))
2✔
UNCOV
1105
                        printf("No services.\n");
×
1106
                else
1107
                        printf("\n%zu services listed.\n", table_get_rows(t) - 1);
1✔
1108
        }
1109

1110
        return 0;
1111
}
1112

1113
static int ssh_authorized_keys(int argc, char *argv[], void *userdata) {
10✔
UNCOV
1114
        _cleanup_(user_record_unrefp) UserRecord *ur = NULL;
×
1115
        char **chain_invocation;
10✔
1116
        int r;
10✔
1117

1118
        assert(argc >= 2);
10✔
1119

1120
        if (arg_from_file)
10✔
UNCOV
1121
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--from-file= not supported when showing SSH authorized keys, refusing.");
×
1122

1123
        if (arg_chain) {
10✔
1124
                /* If --chain is specified, the rest of the command line is the chain command */
1125

1126
                if (argc < 3)
4✔
1127
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1✔
1128
                                               "No chain command line specified, refusing.");
1129

1130
                /* Make similar restrictions on the chain command as OpenSSH itself makes on the primary command. */
1131
                if (!path_is_absolute(argv[2]))
3✔
1132
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1✔
1133
                                               "Chain invocation of ssh-authorized-keys commands requires an absolute binary path argument.");
1134

1135
                if (!path_is_normalized(argv[2]))
2✔
UNCOV
1136
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1137
                                               "Chain invocation of ssh-authorized-keys commands requires an normalized binary path argument.");
1138

1139
                chain_invocation = argv + 2;
2✔
1140
        } else {
1141
                /* If --chain is not specified, then refuse any further arguments */
1142

1143
                if (argc > 2)
6✔
UNCOV
1144
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Too many arguments.");
×
1145

1146
                chain_invocation = NULL;
1147
        }
1148

1149
        r = userdb_by_name(argv[1], /* match= */ NULL, arg_userdb_flags, &ur);
8✔
1150
        if (r == -ESRCH)
8✔
1151
                log_error_errno(r, "User %s does not exist.", argv[1]);
2✔
1152
        else if (r == -EHOSTDOWN)
6✔
UNCOV
1153
                log_error_errno(r, "Selected user database service is not available for this request.");
×
1154
        else if (r == -EINVAL)
6✔
1155
                log_error_errno(r, "Failed to find user %s: %m (Invalid user name?)", argv[1]);
×
1156
        else if (r < 0)
6✔
UNCOV
1157
                log_error_errno(r, "Failed to find user %s: %m", argv[1]);
×
1158
        else {
1159
                if (strv_isempty(ur->ssh_authorized_keys))
6✔
UNCOV
1160
                        log_debug("User record for %s has no public SSH keys.", argv[1]);
×
1161
                else
1162
                        STRV_FOREACH(i, ur->ssh_authorized_keys)
14✔
1163
                                printf("%s\n", *i);
8✔
1164

1165
                if (ur->incomplete) {
6✔
UNCOV
1166
                        fflush(stdout);
×
UNCOV
1167
                        log_warning("Warning: lacking rights to acquire privileged fields of user record of '%s', output incomplete.", ur->user_name);
×
1168
                }
1169
        }
1170

1171
        if (chain_invocation) {
8✔
1172
                if (DEBUG_LOGGING) {
2✔
1173
                        _cleanup_free_ char *s = NULL;
1✔
1174

1175
                        s = quote_command_line(chain_invocation, SHELL_ESCAPE_EMPTY);
1✔
1176
                        if (!s)
1✔
1177
                                return log_oom();
×
1178

1179
                        log_debug("Chain invoking: %s", s);
1✔
1180
                }
1181

1182
                fflush(stdout);
2✔
UNCOV
1183
                execv(chain_invocation[0], chain_invocation);
×
UNCOV
1184
                if (errno == ENOENT) /* Let's handle ENOENT gracefully */
×
1185
                        log_warning_errno(errno, "Chain executable '%s' does not exist, ignoring chain invocation.", chain_invocation[0]);
8✔
1186
                else {
UNCOV
1187
                        log_error_errno(errno, "Failed to invoke chain executable '%s': %m", chain_invocation[0]);
×
UNCOV
1188
                        if (r >= 0)
×
UNCOV
1189
                                r = -errno;
×
1190
                }
1191
        }
1192

1193
        return r;
1194
}
1195

1196
static int load_credential_one(
236✔
1197
                int credential_dir_fd,
1198
                const char *name,
1199
                int *userdb_dir_persist_fd,
1200
                int *userdb_dir_transient_fd) {
1201

1202
        int r;
236✔
1203

1204
        assert(credential_dir_fd >= 0);
236✔
1205
        assert(name);
236✔
1206
        assert(userdb_dir_persist_fd);
236✔
1207
        assert(userdb_dir_transient_fd);
236✔
1208

1209
        const char *suffix = startswith(name, "userdb.");
236✔
1210
        if (!suffix)
236✔
1211
                return 0;
236✔
1212

1213
        const char *transient = startswith(suffix, "transient."),
236✔
1214
                *user = startswith(transient ?: suffix, "user."),
472✔
1215
                *group = startswith(transient ?: suffix, "group.");
236✔
1216
        if (!user && !group)
236✔
1217
                return 0;
1218

1219
        const char *userdb_dir = transient ? "/run/userdb" : "/etc/userdb";
236✔
1220

1221
        int *userdb_dir_fd = transient ? userdb_dir_transient_fd : userdb_dir_persist_fd;
236✔
1222
        if (*userdb_dir_fd == -EBADF) {
236✔
1223
                *userdb_dir_fd = xopenat_full(AT_FDCWD, userdb_dir,
118✔
1224
                                              /* open_flags= */ O_DIRECTORY|O_CREAT|O_CLOEXEC,
1225
                                              /* xopen_flags= */ XO_LABEL,
1226
                                              /* mode= */ 0755);
1227
                if (*userdb_dir_fd < 0)
118✔
UNCOV
1228
                        return log_error_errno(*userdb_dir_fd, "Failed to open '%s/': %m", userdb_dir);
×
1229
        } else if (*userdb_dir_fd < 0)
118✔
UNCOV
1230
                return log_debug_errno(*userdb_dir_fd, "Previous attempt to open '%s/' failed, skipping.", userdb_dir);
×
1231

1232
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
236✔
1233
        unsigned line = 0, column = 0;
236✔
1234
        r = sd_json_parse_file_at(NULL, credential_dir_fd, name, SD_JSON_PARSE_SENSITIVE, &v, &line, &column);
236✔
1235
        if (r < 0)
236✔
UNCOV
1236
                return log_error_errno(r, "Failed to parse credential '%s' as JSON at %u:%u: %m", name, line, column);
×
1237

1238
        _cleanup_(user_record_unrefp) UserRecord *ur = NULL, *ur_stripped = NULL, *ur_privileged = NULL;
236✔
1239
        _cleanup_(group_record_unrefp) GroupRecord *gr = NULL, *gr_stripped = NULL, *gr_privileged = NULL;
236✔
1240
        _cleanup_free_ char *fn = NULL, *link = NULL;
236✔
1241

1242
        if (user) {
236✔
1243
                ur = user_record_new();
118✔
1244
                if (!ur)
118✔
1245
                        return log_oom();
14✔
1246

1247
                r = user_record_load(ur, v, USER_RECORD_LOAD_MASK_SECRET|USER_RECORD_LOG);
118✔
1248
                if (r < 0)
118✔
1249
                        return r;
1250

1251
                if (user_record_is_root(ur))
118✔
UNCOV
1252
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Creating 'root' user from credentials is not supported.");
×
1253
                if (user_record_is_nobody(ur))
118✔
UNCOV
1254
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Creating 'nobody' user from credentials is not supported.");
×
1255

1256
                if (!streq_ptr(user, ur->user_name))
118✔
UNCOV
1257
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1258
                                               "Credential suffix '%s' does not match user record name '%s'",
1259
                                               user, strna(ur->user_name));
1260

1261
                if (!uid_is_valid(ur->uid))
118✔
UNCOV
1262
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "JSON user record missing uid field");
×
1263

1264
                if (!gid_is_valid(user_record_gid(ur)))
118✔
UNCOV
1265
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "JSON user record missing gid field");
×
1266

1267
                _cleanup_(user_record_unrefp) UserRecord *m = NULL;
118✔
1268
                r = userdb_by_name(ur->user_name, /* match= */ NULL, USERDB_SUPPRESS_SHADOW, &m);
118✔
1269
                if (r >= 0) {
118✔
1270
                        if (m->uid != ur->uid)
14✔
UNCOV
1271
                                return log_error_errno(SYNTHETIC_ERRNO(EEXIST),
×
1272
                                                       "Cannot create user %s from credential %s as it already exists with UID " UID_FMT " instead of " UID_FMT,
1273
                                                       ur->user_name, name, m->uid, ur->uid);
1274

1275
                        log_info("User with name %s and UID " UID_FMT " already exists, not creating user from credential %s", ur->user_name, ur->uid, name);
14✔
1276
                        return 0;
14✔
1277
                }
1278
                if (r != -ESRCH)
104✔
UNCOV
1279
                        return log_error_errno(r, "Failed to check if user with name %s already exists: %m", ur->user_name);
×
1280

1281
                m = user_record_unref(m);
104✔
1282
                r = userdb_by_uid(ur->uid, /* match= */ NULL, USERDB_SUPPRESS_SHADOW, &m);
104✔
1283
                if (r >= 0) {
104✔
UNCOV
1284
                        if (!streq_ptr(ur->user_name, m->user_name))
×
UNCOV
1285
                                return log_error_errno(SYNTHETIC_ERRNO(EEXIST),
×
1286
                                                       "Cannot create user %s from credential %s as UID " UID_FMT " is already assigned to user %s",
1287
                                                       ur->user_name, name, ur->uid, m->user_name);
1288

UNCOV
1289
                        log_info("User with name %s and UID " UID_FMT " already exists, not creating user from credential %s", ur->user_name, ur->uid, name);
×
UNCOV
1290
                        return 0;
×
1291
                }
1292
                if (r != -ESRCH)
104✔
1293
                        return log_error_errno(r, "Failed to check if user with UID " UID_FMT " already exists: %m", ur->uid);
×
1294

1295
                r = user_record_clone(ur, USER_RECORD_LOAD_MASK_PRIVILEGED|USER_RECORD_LOG, &ur_stripped);
104✔
1296
                if (r < 0)
104✔
1297
                        return r;
1298

1299
                r = user_record_clone(ur, USER_RECORD_EXTRACT_PRIVILEGED|USER_RECORD_EMPTY_OK|USER_RECORD_LOG, &ur_privileged);
104✔
1300
                if (r < 0)
104✔
1301
                        return r;
1302

1303
                fn = strjoin(ur->user_name, ".user");
104✔
1304
                if (!fn)
104✔
UNCOV
1305
                        return log_oom();
×
1306

1307
                if (asprintf(&link, UID_FMT ".user", ur->uid) < 0)
104✔
UNCOV
1308
                        return log_oom();
×
1309
        } else {
1310
                assert(group);
118✔
1311

1312
                gr = group_record_new();
118✔
1313
                if (!gr)
118✔
1314
                        return log_oom();
14✔
1315

1316
                r = group_record_load(gr, v, USER_RECORD_LOAD_MASK_SECRET|USER_RECORD_LOG);
118✔
1317
                if (r < 0)
118✔
1318
                        return r;
1319

1320
                if (group_record_is_root(gr))
118✔
UNCOV
1321
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Creating 'root' group from credentials is not supported.");
×
1322
                if (group_record_is_nobody(gr))
118✔
UNCOV
1323
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Creating 'nobody' group from credentials is not supported.");
×
1324

1325
                if (!streq_ptr(group, gr->group_name))
118✔
UNCOV
1326
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1327
                                               "Credential suffix '%s' does not match group record name '%s'",
1328
                                               group, strna(gr->group_name));
1329

1330
                if (!gid_is_valid(gr->gid))
118✔
UNCOV
1331
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "JSON group record missing gid field");
×
1332

1333
                _cleanup_(group_record_unrefp) GroupRecord *m = NULL;
118✔
1334
                r = groupdb_by_name(gr->group_name, /* match= */ NULL, USERDB_SUPPRESS_SHADOW, &m);
118✔
1335
                if (r >= 0) {
118✔
1336
                        if (m->gid != gr->gid)
14✔
UNCOV
1337
                                return log_error_errno(SYNTHETIC_ERRNO(EEXIST),
×
1338
                                                       "Cannot create group %s from credential %s as it already exists with GID " GID_FMT " instead of " GID_FMT,
1339
                                                       gr->group_name, name, m->gid, gr->gid);
1340

1341
                        log_info("Group with name %s and GID " GID_FMT " already exists, not creating group from credential %s", gr->group_name, gr->gid, name);
14✔
1342
                        return 0;
14✔
1343
                }
1344
                if (r != -ESRCH)
104✔
UNCOV
1345
                        return log_error_errno(r, "Failed to check if group with name %s already exists: %m", gr->group_name);
×
1346

1347
                m = group_record_unref(m);
104✔
1348
                r = groupdb_by_gid(gr->gid, /* match= */ NULL, USERDB_SUPPRESS_SHADOW, &m);
104✔
1349
                if (r >= 0) {
104✔
UNCOV
1350
                        if (!streq_ptr(gr->group_name, m->group_name))
×
UNCOV
1351
                                return log_error_errno(SYNTHETIC_ERRNO(EEXIST),
×
1352
                                                       "Cannot create group %s from credential %s as GID " GID_FMT " is already assigned to group %s",
1353
                                                       gr->group_name, name, gr->gid, m->group_name);
1354

UNCOV
1355
                        log_info("Group with name %s and GID " GID_FMT " already exists, not creating group from credential %s", gr->group_name, gr->gid, name);
×
UNCOV
1356
                        return 0;
×
1357
                }
1358
                if (r != -ESRCH)
104✔
1359
                        return log_error_errno(r, "Failed to check if group with GID " GID_FMT " already exists: %m", gr->gid);
×
1360

1361
                r = group_record_clone(gr, USER_RECORD_LOAD_MASK_PRIVILEGED|USER_RECORD_LOG, &gr_stripped);
104✔
1362
                if (r < 0)
104✔
1363
                        return r;
1364

1365
                r = group_record_clone(gr, USER_RECORD_EXTRACT_PRIVILEGED|USER_RECORD_EMPTY_OK|USER_RECORD_LOG, &gr_privileged);
104✔
1366
                if (r < 0)
104✔
1367
                        return r;
1368

1369
                fn = strjoin(gr->group_name, ".group");
104✔
1370
                if (!fn)
104✔
UNCOV
1371
                        return log_oom();
×
1372

1373
                if (asprintf(&link, GID_FMT ".group", gr->gid) < 0)
104✔
UNCOV
1374
                        return log_oom();
×
1375
        }
1376

1377
        if (!filename_is_valid(fn))
208✔
UNCOV
1378
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1379
                                       "Passed credential '%s' would result in invalid filename '%s'.",
1380
                                       name, fn);
1381

1382
        _cleanup_free_ char *formatted = NULL;
208✔
1383
        r = sd_json_variant_format(ur ? ur_stripped->json : gr_stripped->json, SD_JSON_FORMAT_NEWLINE, &formatted);
208✔
1384
        if (r < 0)
208✔
UNCOV
1385
                return log_error_errno(r, "Failed to format JSON record: %m");
×
1386

1387
        r = write_string_file_at(*userdb_dir_fd, fn, formatted, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC);
208✔
1388
        if (r < 0)
208✔
1389
                return log_error_errno(r, "Failed to write JSON record to %s/%s: %m", userdb_dir, fn);
×
1390

1391
        if (symlinkat(fn, *userdb_dir_fd, link) < 0)
208✔
UNCOV
1392
                return log_error_errno(errno, "Failed to create symlink from %s to %s: %m", link, fn);
×
1393

1394
        log_info("Installed %s/%s from credential.", userdb_dir, fn);
208✔
1395

1396
        if ((ur && !sd_json_variant_is_blank_object(ur_privileged->json)) ||
208✔
1397
            (gr && !sd_json_variant_is_blank_object(gr_privileged->json))) {
104✔
1398
                fn = mfree(fn);
104✔
1399
                fn = strjoin(ur ? ur->user_name : gr->group_name, ur ? ".user-privileged" : ".group-privileged");
104✔
1400
                if (!fn)
104✔
UNCOV
1401
                        return log_oom();
×
1402

1403
                formatted = mfree(formatted);
104✔
1404
                r = sd_json_variant_format(ur ? ur_privileged->json : gr_privileged->json, SD_JSON_FORMAT_NEWLINE, &formatted);
104✔
1405
                if (r < 0)
104✔
1406
                        return log_error_errno(r, "Failed to format JSON record: %m");
×
1407

1408
                r = write_string_file_at(*userdb_dir_fd, fn, formatted, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_MODE_0600);
104✔
1409
                if (r < 0)
104✔
UNCOV
1410
                        return log_error_errno(r, "Failed to write JSON record to %s/%s: %m", userdb_dir, fn);
×
1411

1412
                link = mfree(link);
104✔
1413

1414
                if (ur) {
104✔
1415
                        if (asprintf(&link, UID_FMT ".user-privileged", ur->uid) < 0)
104✔
UNCOV
1416
                                return log_oom();
×
1417
                } else {
UNCOV
1418
                        if (asprintf(&link, GID_FMT ".group-privileged", gr->gid) < 0)
×
UNCOV
1419
                                return log_oom();
×
1420
                }
1421

1422
                if (symlinkat(fn, *userdb_dir_fd, link) < 0)
104✔
UNCOV
1423
                        return log_error_errno(errno, "Failed to create symlink from %s to %s: %m", link, fn);
×
1424

1425
                log_info("Installed %s/%s from credential.", userdb_dir, fn);
104✔
1426
        }
1427

1428
        if (ur)
208✔
1429
                STRV_FOREACH(g, ur->member_of) {
312✔
1430
                        _cleanup_free_ char *membership = strjoin(ur->user_name, ":", *g);
416✔
1431
                        if (!membership)
208✔
1432
                                return log_oom();
×
1433

1434
                        _cleanup_close_ int fd = openat(*userdb_dir_fd, membership, O_WRONLY|O_CREAT|O_CLOEXEC, 0644);
416✔
1435
                        if (fd < 0)
208✔
1436
                                return log_error_errno(errno, "Failed to create %s: %m", membership);
×
1437

1438
                        log_info("Installed %s/%s from credential.", userdb_dir, membership);
208✔
1439
                }
1440
        else
1441
                STRV_FOREACH(u, gr->members) {
104✔
UNCOV
1442
                        _cleanup_free_ char *membership = strjoin(*u, ":", gr->group_name);
×
UNCOV
1443
                        if (!membership)
×
UNCOV
1444
                                return log_oom();
×
1445

UNCOV
1446
                        _cleanup_close_ int fd = openat(*userdb_dir_fd, membership, O_WRONLY|O_CREAT|O_CLOEXEC, 0644);
×
1447
                        if (fd < 0)
×
UNCOV
1448
                                return log_error_errno(errno, "Failed to create %s: %m", membership);
×
1449

UNCOV
1450
                        log_info("Installed %s/%s from credential.", userdb_dir, membership);
×
1451
                }
1452

1453
        if (ur && user_record_disposition(ur) == USER_REGULAR) {
208✔
1454
                const char *hd = user_record_home_directory(ur);
104✔
1455

1456
                r = RET_NERRNO(access(hd, F_OK));
312✔
1457
                if (r < 0) {
104✔
1458
                        if (r != -ENOENT)
104✔
1459
                                return log_error_errno(r, "Failed to check if %s exists: %m", hd);
×
1460

1461
                        WITH_UMASK(0000) {
208✔
1462
                                r = mkdir_parents(hd, 0755);
104✔
1463
                                if (r < 0)
104✔
1464
                                        return log_error_errno(r, "Failed to create parent directories of %s: %m", hd);
×
1465

1466
                                if (mkdir(hd, 0700) < 0 && errno != EEXIST)
104✔
UNCOV
1467
                                        return log_error_errno(errno, "Failed to create %s: %m", hd);
×
1468
                        }
1469

1470
                        if (chown(hd, ur->uid, user_record_gid(ur)) < 0)
104✔
UNCOV
1471
                                return log_error_errno(errno, "Failed to chown %s: %m", hd);
×
1472

1473
                        r = copy_tree(user_record_skeleton_directory(ur), hd, ur->uid, user_record_gid(ur),
104✔
1474
                                      COPY_REFLINK|COPY_MERGE, /* denylist= */ NULL, /* subvolumes= */NULL);
1475
                        if (r < 0 && r != -ENOENT)
104✔
UNCOV
1476
                                return log_error_errno(r, "Failed to copy skeleton directory to %s: %m", hd);
×
1477
                }
1478
        }
1479

1480
        return 0;
1481
}
1482

1483
static int load_credentials(int argc, char *argv[], void *userdata) {
118✔
1484
        int r;
118✔
1485

1486
        _cleanup_close_ int credential_dir_fd = open_credentials_dir();
236✔
1487
        if (IN_SET(credential_dir_fd, -ENXIO, -ENOENT)) {
118✔
1488
                /* Credential env var not set, or dir doesn't exist. */
UNCOV
1489
                log_debug("No credentials found.");
×
UNCOV
1490
                return 0;
×
1491
        }
1492
        if (credential_dir_fd < 0)
118✔
UNCOV
1493
                return log_error_errno(credential_dir_fd, "Failed to open credentials directory: %m");
×
1494

1495
        _cleanup_free_ DirectoryEntries *des = NULL;
118✔
1496
        r = readdir_all(credential_dir_fd, RECURSE_DIR_SORT|RECURSE_DIR_IGNORE_DOT|RECURSE_DIR_ENSURE_TYPE, &des);
118✔
1497
        if (r < 0)
118✔
UNCOV
1498
                return log_error_errno(r, "Failed to enumerate credentials: %m");
×
1499

1500
        _cleanup_close_ int userdb_persist_dir_fd = -EBADF, userdb_transient_dir_fd = -EBADF;
236✔
1501

1502
        FOREACH_ARRAY(i, des->entries, des->n_entries) {
354✔
1503
                struct dirent *de = *i;
236✔
1504

1505
                if (de->d_type != DT_REG)
236✔
UNCOV
1506
                        continue;
×
1507

1508
                RET_GATHER(r, load_credential_one(
236✔
1509
                                credential_dir_fd,
1510
                                de->d_name,
1511
                                &userdb_persist_dir_fd,
1512
                                &userdb_transient_dir_fd));
1513
        }
1514

1515
        return r;
118✔
1516
}
1517

1518
static int help(int argc, char *argv[], void *userdata) {
1✔
1519
        _cleanup_free_ char *link = NULL;
1✔
1520
        int r;
1✔
1521

1522
        pager_open(arg_pager_flags);
1✔
1523

1524
        r = terminal_urlify_man("userdbctl", "1", &link);
1✔
1525
        if (r < 0)
1✔
UNCOV
1526
                return log_oom();
×
1527

1528
        printf("%s [OPTIONS...] COMMAND ...\n\n"
2✔
1529
               "%sShow user and group information.%s\n"
1530
               "\nCommands:\n"
1531
               "  user [USER…]               Inspect user\n"
1532
               "  group [GROUP…]             Inspect group\n"
1533
               "  users-in-group [GROUP…]    Show users that are members of specified groups\n"
1534
               "  groups-of-user [USER…]     Show groups the specified users are members of\n"
1535
               "  services                   Show enabled database services\n"
1536
               "  ssh-authorized-keys USER   Show SSH authorized keys for user\n"
1537
               "  load-credentials           Write static user/group records from credentials\n"
1538
               "\nOptions:\n"
1539
               "  -h --help                  Show this help\n"
1540
               "     --version               Show package version\n"
1541
               "     --no-pager              Do not pipe output into a pager\n"
1542
               "     --no-legend             Do not show the headers and footers\n"
1543
               "     --output=MODE           Select output mode (classic, friendly, table, json)\n"
1544
               "  -j                         Equivalent to --output=json\n"
1545
               "  -s --service=SERVICE[:SERVICE…]\n"
1546
               "                             Query the specified service\n"
1547
               "     --with-nss=BOOL         Control whether to include glibc NSS data\n"
1548
               "  -N                         Do not synthesize or include glibc NSS data\n"
1549
               "                             (Same as --synthesize=no --with-nss=no)\n"
1550
               "     --synthesize=BOOL       Synthesize root/nobody user\n"
1551
               "     --with-dropin=BOOL      Control whether to include drop-in records\n"
1552
               "     --with-varlink=BOOL     Control whether to talk to services at all\n"
1553
               "     --multiplexer=BOOL      Control whether to use the multiplexer\n"
1554
               "     --json=pretty|short     JSON output mode\n"
1555
               "     --chain                 Chain another command\n"
1556
               "     --uid-min=ID            Filter by minimum UID/GID (default 0)\n"
1557
               "     --uid-max=ID            Filter by maximum UID/GID (default 4294967294)\n"
1558
               "  -z --fuzzy                 Do a fuzzy name search\n"
1559
               "     --disposition=VALUE     Filter by disposition\n"
1560
               "  -I                         Equivalent to --disposition=intrinsic\n"
1561
               "  -S                         Equivalent to --disposition=system\n"
1562
               "  -R                         Equivalent to --disposition=regular\n"
1563
               "     --boundaries=BOOL       Show/hide UID/GID range boundaries in output\n"
1564
               "  -B                         Equivalent to --boundaries=no\n"
1565
               "  -F --from-file=PATH        Read JSON record from file\n"
1566
               "\nSee the %s for details.\n",
1567
               program_invocation_short_name,
1568
               ansi_highlight(),
1569
               ansi_normal(),
1570
               link);
1571

1572
        return 0;
1573
}
1574

1575
static int parse_argv(int argc, char *argv[]) {
315✔
1576

1577
        enum {
315✔
1578
                ARG_VERSION = 0x100,
1579
                ARG_NO_PAGER,
1580
                ARG_NO_LEGEND,
1581
                ARG_OUTPUT,
1582
                ARG_WITH_NSS,
1583
                ARG_WITH_DROPIN,
1584
                ARG_WITH_VARLINK,
1585
                ARG_SYNTHESIZE,
1586
                ARG_MULTIPLEXER,
1587
                ARG_JSON,
1588
                ARG_CHAIN,
1589
                ARG_UID_MIN,
1590
                ARG_UID_MAX,
1591
                ARG_DISPOSITION,
1592
                ARG_BOUNDARIES,
1593
        };
1594

1595
        static const struct option options[] = {
315✔
1596
                { "help",         no_argument,       NULL, 'h'              },
1597
                { "version",      no_argument,       NULL, ARG_VERSION      },
1598
                { "no-pager",     no_argument,       NULL, ARG_NO_PAGER     },
1599
                { "no-legend",    no_argument,       NULL, ARG_NO_LEGEND    },
1600
                { "output",       required_argument, NULL, ARG_OUTPUT       },
1601
                { "service",      required_argument, NULL, 's'              },
1602
                { "with-nss",     required_argument, NULL, ARG_WITH_NSS     },
1603
                { "with-dropin",  required_argument, NULL, ARG_WITH_DROPIN  },
1604
                { "with-varlink", required_argument, NULL, ARG_WITH_VARLINK },
1605
                { "synthesize",   required_argument, NULL, ARG_SYNTHESIZE   },
1606
                { "multiplexer",  required_argument, NULL, ARG_MULTIPLEXER  },
1607
                { "json",         required_argument, NULL, ARG_JSON         },
1608
                { "chain",        no_argument,       NULL, ARG_CHAIN        },
1609
                { "uid-min",      required_argument, NULL, ARG_UID_MIN      },
1610
                { "uid-max",      required_argument, NULL, ARG_UID_MAX      },
1611
                { "fuzzy",        no_argument,       NULL, 'z'              },
1612
                { "disposition",  required_argument, NULL, ARG_DISPOSITION  },
1613
                { "boundaries",   required_argument, NULL, ARG_BOUNDARIES   },
1614
                { "from-file",    required_argument, NULL, 'F'              },
1615
                {}
1616
        };
1617

1618
        const char *e;
315✔
1619
        int r;
315✔
1620

1621
        assert(argc >= 0);
315✔
1622
        assert(argv);
315✔
1623

1624
        /* We are going to update this environment variable with our own, hence let's first read what is already set */
1625
        e = getenv("SYSTEMD_ONLY_USERDB");
315✔
1626
        if (e) {
315✔
UNCOV
1627
                char **l;
×
1628

UNCOV
1629
                l = strv_split(e, ":");
×
UNCOV
1630
                if (!l)
×
UNCOV
1631
                        return log_oom();
×
1632

UNCOV
1633
                strv_free(arg_services);
×
UNCOV
1634
                arg_services = l;
×
1635
        }
1636

1637
        /* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long()
1638
         * that checks for GNU extensions in optstring ('-' or '+' at the beginning). */
1639
        optind = 0;
315✔
1640

1641
        for (;;) {
412✔
1642
                int c;
412✔
1643

1644
                c = getopt_long(argc, argv,
412✔
1645
                                arg_chain ? "+hjs:NISRzBF:" : "hjs:NISRzBF:", /* When --chain was used disable parsing of further switches */
412✔
1646
                                options, NULL);
1647
                if (c < 0)
412✔
1648
                        break;
1649

1650
                switch (c) {
127✔
1651

1652
                case 'h':
1✔
1653
                        return help(0, NULL, NULL);
1✔
1654

1655
                case ARG_VERSION:
1✔
1656
                        return version();
1✔
1657

1658
                case ARG_NO_PAGER:
2✔
1659
                        arg_pager_flags |= PAGER_DISABLE;
2✔
1660
                        break;
2✔
1661

1662
                case ARG_NO_LEGEND:
3✔
1663
                        arg_legend = false;
3✔
1664
                        break;
3✔
1665

1666
                case ARG_OUTPUT:
8✔
1667
                        if (streq(optarg, "help"))
8✔
UNCOV
1668
                                return DUMP_STRING_TABLE(output, Output, _OUTPUT_MAX);
×
1669

1670
                        arg_output = output_from_string(optarg);
8✔
1671
                        if (arg_output < 0)
8✔
1672
                                return log_error_errno(arg_output, "Invalid --output= mode: %s", optarg);
4✔
1673

1674
                        arg_json_format_flags = arg_output == OUTPUT_JSON ? SD_JSON_FORMAT_PRETTY|SD_JSON_FORMAT_COLOR_AUTO : SD_JSON_FORMAT_OFF;
4✔
1675
                        break;
4✔
1676

1677
                case ARG_JSON:
6✔
1678
                        r = parse_json_argument(optarg, &arg_json_format_flags);
6✔
1679
                        if (r <= 0)
6✔
1680
                                return r;
1681

1682
                        arg_output = sd_json_format_enabled(arg_json_format_flags) ? OUTPUT_JSON : _OUTPUT_INVALID;
2✔
1683
                        break;
2✔
1684

1685
                case 'j':
26✔
1686
                        arg_json_format_flags = SD_JSON_FORMAT_PRETTY|SD_JSON_FORMAT_COLOR_AUTO;
26✔
1687
                        arg_output = OUTPUT_JSON;
26✔
1688
                        break;
26✔
1689

1690
                case 's':
×
1691
                        if (isempty(optarg))
×
1692
                                arg_services = strv_free(arg_services);
×
1693
                        else {
1694
                                r = strv_split_and_extend(&arg_services, optarg, ":", /* filter_duplicates = */ true);
×
1695
                                if (r < 0)
×
1696
                                        return log_error_errno(r, "Failed to parse -s/--service= argument: %m");
×
1697
                        }
1698

1699
                        break;
1700

1701
                case 'N':
1✔
1702
                        arg_userdb_flags |= USERDB_EXCLUDE_NSS|USERDB_DONT_SYNTHESIZE_INTRINSIC|USERDB_DONT_SYNTHESIZE_FOREIGN;
1✔
1703
                        break;
1✔
1704

1705
                case ARG_WITH_NSS:
14✔
1706
                        r = parse_boolean_argument("--with-nss=", optarg, NULL);
14✔
1707
                        if (r < 0)
14✔
1708
                                return r;
1709

1710
                        SET_FLAG(arg_userdb_flags, USERDB_EXCLUDE_NSS, !r);
10✔
1711
                        break;
10✔
1712

1713
                case ARG_WITH_DROPIN:
8✔
1714
                        r = parse_boolean_argument("--with-dropin=", optarg, NULL);
8✔
1715
                        if (r < 0)
8✔
1716
                                return r;
1717

1718
                        SET_FLAG(arg_userdb_flags, USERDB_EXCLUDE_DROPIN, !r);
4✔
1719
                        break;
4✔
1720

1721
                case ARG_WITH_VARLINK:
7✔
1722
                        r = parse_boolean_argument("--with-varlink=", optarg, NULL);
7✔
1723
                        if (r < 0)
7✔
1724
                                return r;
1725

1726
                        SET_FLAG(arg_userdb_flags, USERDB_EXCLUDE_VARLINK, !r);
3✔
1727
                        break;
3✔
1728

1729
                case ARG_SYNTHESIZE:
12✔
1730
                        r = parse_boolean_argument("--synthesize=", optarg, NULL);
12✔
1731
                        if (r < 0)
12✔
1732
                                return r;
1733

1734
                        SET_FLAG(arg_userdb_flags, USERDB_DONT_SYNTHESIZE_INTRINSIC|USERDB_DONT_SYNTHESIZE_FOREIGN, !r);
8✔
1735
                        break;
8✔
1736

1737
                case ARG_MULTIPLEXER:
6✔
1738
                        r = parse_boolean_argument("--multiplexer=", optarg, NULL);
6✔
1739
                        if (r < 0)
6✔
1740
                                return r;
1741

1742
                        SET_FLAG(arg_userdb_flags, USERDB_AVOID_MULTIPLEXER, !r);
2✔
1743
                        break;
2✔
1744

1745
                case ARG_CHAIN:
4✔
1746
                        arg_chain = true;
4✔
1747
                        break;
4✔
1748

1749
                case ARG_DISPOSITION: {
4✔
1750
                        UserDisposition d = user_disposition_from_string(optarg);
4✔
1751
                        if (d < 0)
4✔
1752
                                return log_error_errno(d, "Unknown user disposition: %s", optarg);
×
1753

1754
                        if (arg_disposition_mask == UINT64_MAX)
4✔
1755
                                arg_disposition_mask = 0;
2✔
1756

1757
                        arg_disposition_mask |= UINT64_C(1) << d;
4✔
1758
                        break;
4✔
1759
                }
1760

1761
                case 'I':
2✔
1762
                        if (arg_disposition_mask == UINT64_MAX)
2✔
1763
                                arg_disposition_mask = 0;
2✔
1764

1765
                        arg_disposition_mask |= UINT64_C(1) << USER_INTRINSIC;
2✔
1766
                        break;
2✔
1767

1768
                case 'S':
6✔
1769
                        if (arg_disposition_mask == UINT64_MAX)
6✔
1770
                                arg_disposition_mask = 0;
4✔
1771

1772
                        arg_disposition_mask |= UINT64_C(1) << USER_SYSTEM;
6✔
1773
                        break;
6✔
1774

1775
                case 'R':
2✔
1776
                        if (arg_disposition_mask == UINT64_MAX)
2✔
1777
                                arg_disposition_mask = 0;
2✔
1778

1779
                        arg_disposition_mask |= UINT64_C(1) << USER_REGULAR;
2✔
1780
                        break;
2✔
1781

1782
                case ARG_UID_MIN:
2✔
1783
                        r = parse_uid(optarg, &arg_uid_min);
2✔
1784
                        if (r < 0)
2✔
1785
                                return log_error_errno(r, "Failed to parse --uid-min= value: %s", optarg);
×
1786
                        break;
1787

1788
                case ARG_UID_MAX:
2✔
1789
                        r = parse_uid(optarg, &arg_uid_max);
2✔
1790
                        if (r < 0)
2✔
1791
                                return log_error_errno(r, "Failed to parse --uid-max= value: %s", optarg);
×
1792
                        break;
1793

1794
                case 'z':
2✔
1795
                        arg_fuzzy = true;
2✔
1796
                        break;
2✔
1797

1798
                case ARG_BOUNDARIES:
×
1799
                        r = parse_boolean_argument("boundaries", optarg, &arg_boundaries);
×
1800
                        if (r < 0)
×
1801
                                return r;
1802
                        break;
1803

1804
                case 'B':
2✔
1805
                        arg_boundaries = false;
2✔
1806
                        break;
2✔
1807

1808
                case 'F': {
6✔
1809
                        if (isempty(optarg)) {
6✔
1810
                                arg_from_file = sd_json_variant_unref(arg_from_file);
×
1811
                                break;
6✔
1812
                        }
1813

1814
                        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
6✔
1815
                        const char *fn = streq(optarg, "-") ? NULL : optarg;
6✔
1816
                        unsigned line = 0;
6✔
1817
                        r = sd_json_parse_file(fn ? NULL : stdin, fn ?: "<stdin>", SD_JSON_PARSE_SENSITIVE, &v, &line, /* reterr_column= */ NULL);
6✔
1818
                        if (r < 0)
6✔
1819
                                return log_syntax(/* unit= */ NULL, LOG_ERR, fn ?: "<stdin>", line, r, "JSON parse failure.");
×
1820

1821
                        sd_json_variant_unref(arg_from_file);
6✔
1822
                        arg_from_file = TAKE_PTR(v);
6✔
1823
                        break;
6✔
1824
                }
1825

1826
                case '?':
1827
                        return -EINVAL;
1828

1829
                default:
×
1830
                        assert_not_reached();
×
1831
                }
1832
        }
1833

1834
        if (arg_uid_min > arg_uid_max)
285✔
1835
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Minimum UID/GID " UID_FMT " is above maximum UID/GID " UID_FMT ", refusing.", arg_uid_min, arg_uid_max);
×
1836

1837
        /* If not mask was specified, use the all bits on mask */
1838
        if (arg_disposition_mask == UINT64_MAX)
285✔
1839
                arg_disposition_mask = USER_DISPOSITION_MASK_ALL;
275✔
1840

1841
        if (arg_from_file)
285✔
1842
                arg_boundaries = false;
6✔
1843

1844
        return 1;
1845
}
1846

1847
static int run(int argc, char *argv[]) {
315✔
1848
        static const Verb verbs[] = {
315✔
1849
                { "help",                VERB_ANY, VERB_ANY, 0,            help                },
1850
                { "user",                VERB_ANY, VERB_ANY, VERB_DEFAULT, display_user        },
1851
                { "group",               VERB_ANY, VERB_ANY, 0,            display_group       },
1852
                { "users-in-group",      VERB_ANY, VERB_ANY, 0,            display_memberships },
1853
                { "groups-of-user",      VERB_ANY, VERB_ANY, 0,            display_memberships },
1854
                { "services",            VERB_ANY, 1,        0,            display_services    },
1855
                { "ssh-authorized-keys", 2,        VERB_ANY, 0,            ssh_authorized_keys },
1856
                { "load-credentials",    VERB_ANY, 1,        0,            load_credentials    },
1857
                {}
1858
        };
1859

1860
        int r;
315✔
1861

1862
        log_setup();
315✔
1863

1864
        r = parse_argv(argc, argv);
315✔
1865
        if (r <= 0)
315✔
1866
                return r;
1867

1868
        if (arg_services) {
285✔
1869
                _cleanup_free_ char *e = NULL;
×
1870

1871
                e = strv_join(arg_services, ":");
×
1872
                if (!e)
×
1873
                        return log_oom();
×
1874

1875
                if (setenv("SYSTEMD_ONLY_USERDB", e, true) < 0)
×
1876
                        return log_error_errno(r, "Failed to set $SYSTEMD_ONLY_USERDB: %m");
×
1877

1878
                log_info("Enabled services: %s", e);
×
1879
        } else
1880
                assert_se(unsetenv("SYSTEMD_ONLY_USERDB") == 0);
285✔
1881

1882
        return dispatch_verb(argc, argv, verbs, NULL);
285✔
1883
}
1884

1885
DEFINE_MAIN_FUNCTION(run);
315✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc