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

systemd / systemd / 13877892945

15 Mar 2025 08:56PM UTC coverage: 71.915% (+0.2%) from 71.757%
13877892945

push

github

web-flow
Fix bootctl status to not print strange glyphs in logs (#36745)

146 of 198 new or added lines in 57 files covered. (73.74%)

153 existing lines in 28 files now uncovered.

296065 of 411690 relevant lines covered (71.91%)

715276.25 hits per line

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

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

3
#include <getopt.h>
4

5
#include "bitfield.h"
6
#include "build.h"
7
#include "dirent-util.h"
8
#include "errno-list.h"
9
#include "escape.h"
10
#include "fd-util.h"
11
#include "format-table.h"
12
#include "format-util.h"
13
#include "main-func.h"
14
#include "pager.h"
15
#include "parse-argument.h"
16
#include "parse-util.h"
17
#include "pretty-print.h"
18
#include "socket-util.h"
19
#include "strv.h"
20
#include "terminal-util.h"
21
#include "uid-classification.h"
22
#include "uid-range.h"
23
#include "user-record-show.h"
24
#include "user-util.h"
25
#include "userdb.h"
26
#include "verbs.h"
27
#include "virt.h"
28

29
static enum {
30
        OUTPUT_CLASSIC,
31
        OUTPUT_TABLE,
32
        OUTPUT_FRIENDLY,
33
        OUTPUT_JSON,
34
        _OUTPUT_INVALID = -EINVAL,
35
} arg_output = _OUTPUT_INVALID;
36

37
static PagerFlags arg_pager_flags = 0;
38
static bool arg_legend = true;
39
static char** arg_services = NULL;
40
static UserDBFlags arg_userdb_flags = 0;
41
static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF;
42
static bool arg_chain = false;
43
static uint64_t arg_disposition_mask = UINT64_MAX;
44
static uid_t arg_uid_min = 0;
45
static uid_t arg_uid_max = UID_INVALID-1;
46
static bool arg_fuzzy = false;
47
static bool arg_boundaries = true;
48
static sd_json_variant *arg_from_file = NULL;
49

50
STATIC_DESTRUCTOR_REGISTER(arg_services, strv_freep);
193✔
51
STATIC_DESTRUCTOR_REGISTER(arg_from_file, sd_json_variant_unrefp);
193✔
52

53
static const char *user_disposition_to_color(UserDisposition d) {
442✔
54
        assert(d >= 0);
442✔
55
        assert(d < _USER_DISPOSITION_MAX);
442✔
56

57
        switch (d) {
442✔
58
        case USER_INTRINSIC:
59
                return ansi_red();
28✔
60

61
        case USER_SYSTEM:
62
        case USER_DYNAMIC:
63
                return ansi_green();
392✔
64

65
        case USER_CONTAINER:
66
        case USER_FOREIGN:
67
                return ansi_cyan();
10✔
68

69
        case USER_RESERVED:
70
                return ansi_red();
×
71

72
        default:
73
                return NULL;
74
        }
75
}
76

77
static const char* shell_to_color(const char *shell) {
209✔
78
        return !shell || is_nologin_shell(shell) ? ansi_grey() : NULL;
209✔
79
}
80

81
static int show_user(UserRecord *ur, Table *table) {
415✔
82
        int r;
415✔
83

84
        assert(ur);
415✔
85

86
        switch (arg_output) {
415✔
87

88
        case OUTPUT_CLASSIC:
26✔
89
                if (!uid_is_valid(ur->uid))
26✔
90
                        break;
91

92
                printf("%s:x:" UID_FMT ":" GID_FMT ":%s:%s:%s\n",
52✔
93
                       ur->user_name,
94
                       ur->uid,
95
                       user_record_gid(ur),
96
                       strempty(user_record_real_name(ur)),
26✔
97
                       user_record_home_directory(ur),
98
                       user_record_shell(ur));
99

100
                break;
26✔
101

102
        case OUTPUT_JSON:
96✔
103
                sd_json_variant_dump(ur->json, arg_json_format_flags, NULL, NULL);
96✔
104
                break;
96✔
105

106
        case OUTPUT_FRIENDLY:
84✔
107
                user_record_show(ur, true);
84✔
108

109
                if (ur->incomplete) {
84✔
110
                        fflush(stdout);
×
111
                        log_warning("Warning: lacking rights to acquire privileged fields of user record of '%s', output incomplete.", ur->user_name);
×
112
                }
113

114
                break;
115

116
        case OUTPUT_TABLE: {
209✔
117
                assert(table);
209✔
118
                UserDisposition d = user_record_disposition(ur);
209✔
119
                const char *sh = user_record_shell(ur);
209✔
120

121
                r = table_add_many(
360✔
122
                                table,
123
                                TABLE_STRING, "",
124
                                TABLE_STRING, ur->user_name,
125
                                TABLE_SET_COLOR, user_disposition_to_color(d),
126
                                TABLE_STRING, user_disposition_to_string(d),
127
                                TABLE_UID, ur->uid,
128
                                TABLE_GID, user_record_gid(ur),
129
                                TABLE_STRING, empty_to_null(ur->real_name),
130
                                TABLE_PATH, user_record_home_directory(ur),
131
                                TABLE_PATH, sh,
132
                                TABLE_SET_COLOR, shell_to_color(sh),
133
                                TABLE_INT, 0);
134
                if (r < 0)
209✔
135
                        return table_log_add_error(r);
×
136

137
                break;
138
        }
139

140
        default:
×
141
                assert_not_reached();
×
142
        }
143

144
        return 0;
145
}
146

147
static bool test_show_mapped(void) {
16✔
148
        /* Show mapped user range only in environments where user mapping is a thing. */
149
        return running_in_userns() > 0;
16✔
150
}
151

152
static const struct {
153
        uid_t first, last;
154
        const char *name;
155
        UserDisposition disposition;
156
        bool (*test)(void);
157
} uid_range_table[] = {
158
        {
159
                .first = 1,
160
                .last = SYSTEM_UID_MAX,
161
                .name = "system",
162
                .disposition = USER_SYSTEM,
163
        },
164
        {
165
                .first = DYNAMIC_UID_MIN,
166
                .last = DYNAMIC_UID_MAX,
167
                .name = "dynamic system",
168
                .disposition = USER_DYNAMIC,
169
        },
170
        {
171
                .first = CONTAINER_UID_MIN,
172
                .last = CONTAINER_UID_MAX,
173
                .name = "container",
174
                .disposition = USER_CONTAINER,
175
        },
176
        {
177
                .first = FOREIGN_UID_MIN,
178
                .last = FOREIGN_UID_MAX,
179
                .name = "foreign",
180
                .disposition = USER_FOREIGN,
181
        },
182
#if ENABLE_HOMED
183
        {
184
                .first = HOME_UID_MIN,
185
                .last = HOME_UID_MAX,
186
                .name = "systemd-homed",
187
                .disposition = USER_REGULAR,
188
        },
189
#endif
190
        {
191
                .first = MAP_UID_MIN,
192
                .last = MAP_UID_MAX,
193
                .name = "mapped",
194
                .disposition = USER_REGULAR,
195
                .test = test_show_mapped,
196
        },
197
};
198

199
static int table_add_uid_boundaries(Table *table, const UIDRange *p) {
12✔
200
        int r, n_added = 0;
12✔
201

202
        assert(table);
12✔
203

204
        FOREACH_ELEMENT(i, uid_range_table) {
84✔
205
                _cleanup_free_ char *name = NULL, *comment = NULL;
44✔
206

207
                if (!BIT_SET(arg_disposition_mask, i->disposition))
72✔
208
                        continue;
18✔
209

210
                if (!uid_range_covers(p, i->first, i->last - i->first + 1))
54✔
211
                        continue;
×
212

213
                if (i->test && !i->test())
54✔
214
                        continue;
10✔
215

216
                name = strjoin(glyph(GLYPH_ARROW_DOWN),
44✔
217
                               " begin ", i->name, " users ",
218
                               glyph(GLYPH_ARROW_DOWN));
219
                if (!name)
44✔
220
                        return log_oom();
×
221

222
                comment = strjoin("First ", i->name, " user");
44✔
223
                if (!comment)
44✔
224
                        return log_oom();
×
225

226
                r = table_add_many(
44✔
227
                                table,
228
                                TABLE_STRING, glyph(GLYPH_TREE_TOP),
229
                                TABLE_STRING, name,
230
                                TABLE_SET_COLOR, ansi_grey(),
231
                                TABLE_STRING, user_disposition_to_string(i->disposition),
232
                                TABLE_SET_COLOR, ansi_grey(),
233
                                TABLE_UID, i->first,
234
                                TABLE_SET_COLOR, ansi_grey(),
235
                                TABLE_EMPTY,
236
                                TABLE_STRING, comment,
237
                                TABLE_SET_COLOR, ansi_grey(),
238
                                TABLE_EMPTY,
239
                                TABLE_EMPTY,
240
                                TABLE_INT, -1); /* sort before any other entry with the same UID */
241
                if (r < 0)
44✔
242
                        return table_log_add_error(r);
×
243

244
                free(name);
44✔
245
                name = strjoin(glyph(GLYPH_ARROW_UP),
44✔
246
                               " end ", i->name, " users ",
247
                               glyph(GLYPH_ARROW_UP));
248
                if (!name)
44✔
249
                        return log_oom();
×
250

251
                free(comment);
44✔
252
                comment = strjoin("Last ", i->name, " user");
44✔
253
                if (!comment)
44✔
254
                        return log_oom();
×
255

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

274
                n_added += 2;
44✔
275
        }
276

277
        return n_added;
278
}
279

280
static int add_unavailable_uid(Table *table, uid_t start, uid_t end) {
×
281
        _cleanup_free_ char *name = NULL;
×
282
        int r;
×
283

284
        assert(table);
×
285
        assert(start <= end);
×
286

NEW
287
        name = strjoin(glyph(GLYPH_ARROW_DOWN),
×
288
                       " begin unavailable users ",
289
                       glyph(GLYPH_ARROW_DOWN));
290
        if (!name)
×
291
                return log_oom();
×
292

293
        r = table_add_many(
×
294
                        table,
295
                        TABLE_STRING, glyph(GLYPH_TREE_TOP),
296
                        TABLE_STRING, name,
297
                        TABLE_SET_COLOR, ansi_grey(),
298
                        TABLE_EMPTY,
299
                        TABLE_UID, start,
300
                        TABLE_SET_COLOR, ansi_grey(),
301
                        TABLE_EMPTY,
302
                        TABLE_STRING, "First unavailable user",
303
                        TABLE_SET_COLOR, ansi_grey(),
304
                        TABLE_EMPTY,
305
                        TABLE_EMPTY,
306
                        TABLE_INT, -1); /* sort before an other entry with the same UID */
307
        if (r < 0)
×
308
                return table_log_add_error(r);
×
309

310
        free(name);
×
NEW
311
        name = strjoin(glyph(GLYPH_ARROW_UP),
×
312
                       " end unavailable users ",
313
                       glyph(GLYPH_ARROW_UP));
314
        if (!name)
×
315
                return log_oom();
×
316

317
        r = table_add_many(
×
318
                        table,
319
                        TABLE_STRING, glyph(GLYPH_TREE_RIGHT),
320
                        TABLE_STRING, name,
321
                        TABLE_SET_COLOR, ansi_grey(),
322
                        TABLE_EMPTY,
323
                        TABLE_UID, end,
324
                        TABLE_SET_COLOR, ansi_grey(),
325
                        TABLE_EMPTY,
326
                        TABLE_STRING, "Last unavailable user",
327
                        TABLE_SET_COLOR, ansi_grey(),
328
                        TABLE_EMPTY,
329
                        TABLE_EMPTY,
330
                        TABLE_INT, 1); /* sort after any other entry with the same UID */
331
        if (r < 0)
×
332
                return table_log_add_error(r);
×
333

334
        return 2;
335
}
336

337
static int table_add_uid_map(
20✔
338
                Table *table,
339
                const UIDRange *p,
340
                int (*add_unavailable)(Table *t, uid_t start, uid_t end)) {
341

342
        uid_t focus = 0;
20✔
343
        int n_added = 0, r;
20✔
344

345
        assert(table);
20✔
346
        assert(add_unavailable);
20✔
347

348
        if (!p)
20✔
349
                return 0;
350

351
        FOREACH_ARRAY(x, p->entries, p->n_entries) {
40✔
352
                if (focus < x->start) {
20✔
353
                        r = add_unavailable(table, focus, x->start-1);
×
354
                        if (r < 0)
×
355
                                return r;
356

357
                        n_added += r;
×
358
                }
359

360
                if (x->start > UINT32_MAX - x->nr) { /* overflow check */
20✔
361
                        focus = UINT32_MAX;
362
                        break;
363
                }
364

365
                focus = x->start + x->nr;
20✔
366
        }
367

368
        if (focus < UINT32_MAX-1) {
20✔
369
                r = add_unavailable(table, focus, UINT32_MAX-1);
×
370
                if (r < 0)
×
371
                        return r;
372

373
                n_added += r;
×
374
        }
375

376
        return n_added;
377
}
378

379
static int display_user(int argc, char *argv[], void *userdata) {
103✔
380
        _cleanup_(table_unrefp) Table *table = NULL;
×
381
        bool draw_separator = false;
103✔
382
        int ret = 0, r;
103✔
383

384
        if (arg_output < 0)
103✔
385
                arg_output = arg_from_file || (argc > 1 && !arg_fuzzy) ? OUTPUT_FRIENDLY : OUTPUT_TABLE;
92✔
386

387
        if (arg_output == OUTPUT_TABLE) {
103✔
388
                table = table_new(" ", "name", "disposition", "uid", "gid", "realname", "home", "shell", "order");
13✔
389
                if (!table)
13✔
390
                        return log_oom();
×
391

392
                (void) table_set_align_percent(table, table_get_cell(table, 0, 3), 100);
13✔
393
                (void) table_set_align_percent(table, table_get_cell(table, 0, 4), 100);
13✔
394
                table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
13✔
395
                (void) table_set_sort(table, (size_t) 3, (size_t) 8);
13✔
396
                (void) table_hide_column_from_display(table, (size_t) 8);
13✔
397
                if (!arg_boundaries)
13✔
398
                        (void) table_hide_column_from_display(table, (size_t) 0);
1✔
399
        }
400

401
        _cleanup_(userdb_match_done) UserDBMatch match = {
103✔
402
                .disposition_mask = arg_disposition_mask,
403
                .uid_min = arg_uid_min,
404
                .uid_max = arg_uid_max,
405
        };
406

407
        if (arg_from_file) {
103✔
408
                if (argc > 1)
3✔
409
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No argument expected when invoked with --from-file=, refusing.");
×
410

411
                _cleanup_(user_record_unrefp) UserRecord *ur = user_record_new();
6✔
412
                if (!ur)
3✔
413
                        return log_oom();
×
414

415
                r = user_record_load(ur, arg_from_file, USER_RECORD_LOAD_MASK_SECRET|USER_RECORD_LOG);
3✔
416
                if (r < 0)
3✔
417
                        return r;
418

419
                r = show_user(ur, table);
3✔
420
                if (r < 0)
3✔
421
                        return r;
422

423
        } else if (argc > 1 && !arg_fuzzy)
100✔
424
                STRV_FOREACH(i, argv + 1) {
175✔
425
                        _cleanup_(user_record_unrefp) UserRecord *ur = NULL;
93✔
426

427
                        r = userdb_by_name(*i, &match, arg_userdb_flags|USERDB_PARSE_NUMERIC, &ur);
93✔
428
                        if (r < 0) {
93✔
429
                                if (r == -ESRCH)
20✔
430
                                        log_error_errno(r, "User %s does not exist.", *i);
16✔
431
                                else if (r == -EHOSTDOWN)
4✔
432
                                        log_error_errno(r, "Selected user database service is not available for this request.");
×
433
                                else if (r == -ENOEXEC)
4✔
434
                                        log_error_errno(r, "User '%s' exists but does not match specified filter.", *i);
×
435
                                else
436
                                        log_error_errno(r, "Failed to find user %s: %m", *i);
4✔
437

438
                                RET_GATHER(ret, r);
20✔
439
                        } else {
440
                                if (draw_separator && arg_output == OUTPUT_FRIENDLY)
73✔
441
                                        putchar('\n');
4✔
442

443
                                r = show_user(ur, table);
73✔
444
                                if (r < 0)
73✔
445
                                        return r;
×
446

447
                                draw_separator = true;
448
                        }
449
                }
450
        else {
451
                if (argc > 1) {
1✔
452
                        /* If there are further arguments, they are the fuzzy match strings. */
453
                        match.fuzzy_names = strv_copy(strv_skip(argv, 1));
1✔
454
                        if (!match.fuzzy_names)
1✔
455
                                return log_oom();
×
456
                }
457

458
                _cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL;
×
459
                r = userdb_all(&match, arg_userdb_flags, &iterator);
18✔
460
                if (r == -ENOLINK) /* ENOLINK → Didn't find answer without Varlink, and didn't try Varlink because was configured to off. */
18✔
461
                        log_debug_errno(r, "No entries found. (Didn't check via Varlink.)");
×
462
                else if (r == -ESRCH) /* ESRCH → Couldn't find any suitable entry, but we checked all sources */
18✔
463
                        log_debug_errno(r, "No entries found.");
18✔
464
                else if (r < 0)
18✔
465
                        return log_error_errno(r, "Failed to enumerate users: %m");
×
466
                else {
467
                        for (;;) {
339✔
468
                                _cleanup_(user_record_unrefp) UserRecord *ur = NULL;
18✔
469

470
                                r = userdb_iterator_get(iterator, &match, &ur);
357✔
471
                                if (r == -ESRCH)
357✔
472
                                        break;
473
                                if (r == -EHOSTDOWN)
339✔
474
                                        return log_error_errno(r, "Selected user database service is not available for this request.");
×
475
                                if (r < 0)
339✔
476
                                        return log_error_errno(r, "Failed acquire next user: %m");
×
477

478
                                if (draw_separator && arg_output == OUTPUT_FRIENDLY)
339✔
479
                                        putchar('\n');
25✔
480

481
                                r = show_user(ur, table);
339✔
482
                                if (r < 0)
339✔
483
                                        return r;
484

485
                                draw_separator = true;
339✔
486
                        }
487
                }
488
        }
489

490
        if (table) {
103✔
491
                int boundary_lines = 0, uid_map_lines = 0;
13✔
492

493
                if (arg_boundaries) {
13✔
494
                        _cleanup_(uid_range_freep) UIDRange *uid_range = NULL;
12✔
495

496
                        r = uid_range_load_userns(/* path = */ NULL, UID_RANGE_USERNS_INSIDE, &uid_range);
12✔
497
                        if (r < 0)
12✔
498
                                log_debug_errno(r, "Failed to load /proc/self/uid_map, ignoring: %m");
×
499

500
                        boundary_lines = table_add_uid_boundaries(table, uid_range);
12✔
501
                        if (boundary_lines < 0)
12✔
502
                                return boundary_lines;
503

504
                        uid_map_lines = table_add_uid_map(table, uid_range, add_unavailable_uid);
12✔
505
                        if (uid_map_lines < 0)
12✔
506
                                return uid_map_lines;
507
                }
508

509
                if (!table_isempty(table)) {
26✔
510
                        r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
13✔
511
                        if (r < 0)
13✔
512
                                return table_log_print_error(r);
×
513
                }
514

515
                if (arg_legend) {
13✔
516
                        size_t k;
12✔
517

518
                        k = table_get_rows(table) - 1 - boundary_lines - uid_map_lines;
12✔
519
                        if (k > 0)
12✔
520
                                printf("\n%zu users listed.\n", k);
10✔
521
                        else
522
                                printf("No users.\n");
2✔
523
                }
524
        }
525

526
        return ret;
527
}
528

529
static int show_group(GroupRecord *gr, Table *table) {
257✔
530
        int r;
257✔
531

532
        assert(gr);
257✔
533

534
        switch (arg_output) {
257✔
535

536
        case OUTPUT_CLASSIC: {
×
537
                _cleanup_free_ char *m = NULL;
×
538

539
                if (!gid_is_valid(gr->gid))
×
540
                        break;
541

542
                m = strv_join(gr->members, ",");
×
543
                if (!m)
×
544
                        return log_oom();
×
545

546
                printf("%s:x:" GID_FMT ":%s\n",
×
547
                       gr->group_name,
548
                       gr->gid,
549
                       m);
550
                break;
551
        }
552

553
        case OUTPUT_JSON:
5✔
554
                sd_json_variant_dump(gr->json, arg_json_format_flags, NULL, NULL);
5✔
555
                break;
5✔
556

557
        case OUTPUT_FRIENDLY:
19✔
558
                group_record_show(gr, true);
19✔
559

560
                if (gr->incomplete) {
19✔
561
                        fflush(stdout);
×
562
                        log_warning("Warning: lacking rights to acquire privileged fields of group record of '%s', output incomplete.", gr->group_name);
257✔
563
                }
564

565
                break;
566

567
        case OUTPUT_TABLE: {
233✔
568
                UserDisposition d;
233✔
569

570
                assert(table);
233✔
571
                d = group_record_disposition(gr);
233✔
572

573
                r = table_add_many(
233✔
574
                                table,
575
                                TABLE_STRING, "",
576
                                TABLE_STRING, gr->group_name,
577
                                TABLE_SET_COLOR, user_disposition_to_color(d),
578
                                TABLE_STRING, user_disposition_to_string(d),
579
                                TABLE_GID, gr->gid,
580
                                TABLE_STRING, gr->description,
581
                                TABLE_INT, 0);
582
                if (r < 0)
233✔
583
                        return table_log_add_error(r);
×
584

585
                break;
586
        }
587

588
        default:
×
589
                assert_not_reached();
×
590
        }
591

592
        return 0;
593
}
594

595
static int table_add_gid_boundaries(Table *table, const UIDRange *p) {
8✔
596
        int r, n_added = 0;
8✔
597

598
        assert(table);
8✔
599

600
        FOREACH_ELEMENT(i, uid_range_table) {
56✔
601
                _cleanup_free_ char *name = NULL, *comment = NULL;
24✔
602

603
                if (!BIT_SET(arg_disposition_mask, i->disposition))
48✔
604
                        continue;
18✔
605

606
                if (!uid_range_covers(p, i->first, i->last - i->first + 1))
30✔
607
                        continue;
×
608

609
                if (i->test && !i->test())
30✔
610
                        continue;
6✔
611

612
                name = strjoin(glyph(GLYPH_ARROW_DOWN),
24✔
613
                               " begin ", i->name, " groups ",
614
                               glyph(GLYPH_ARROW_DOWN));
615
                if (!name)
24✔
616
                        return log_oom();
×
617

618
                comment = strjoin("First ", i->name, " group");
24✔
619
                if (!comment)
24✔
620
                        return log_oom();
×
621

622
                r = table_add_many(
24✔
623
                                table,
624
                                TABLE_STRING, glyph(GLYPH_TREE_TOP),
625
                                TABLE_STRING, name,
626
                                TABLE_SET_COLOR, ansi_grey(),
627
                                TABLE_STRING, user_disposition_to_string(i->disposition),
628
                                TABLE_SET_COLOR, ansi_grey(),
629
                                TABLE_GID, i->first,
630
                                TABLE_SET_COLOR, ansi_grey(),
631
                                TABLE_STRING, comment,
632
                                TABLE_SET_COLOR, ansi_grey(),
633
                                TABLE_INT, -1); /* sort before any other entry with the same GID */
634
                if (r < 0)
24✔
635
                        return table_log_add_error(r);
×
636

637
                free(name);
24✔
638
                name = strjoin(glyph(GLYPH_ARROW_UP),
24✔
639
                               " end ", i->name, " groups ",
640
                               glyph(GLYPH_ARROW_UP));
641
                if (!name)
24✔
642
                        return log_oom();
×
643

644
                free(comment);
24✔
645
                comment = strjoin("Last ", i->name, " group");
24✔
646
                if (!comment)
24✔
647
                        return log_oom();
×
648

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

664
                n_added += 2;
24✔
665
        }
666

667
        return n_added;
668
}
669

670
static int add_unavailable_gid(Table *table, uid_t start, uid_t end) {
×
671
        _cleanup_free_ char *name = NULL;
×
672
        int r;
×
673

674
        assert(table);
×
675
        assert(start <= end);
×
676

NEW
677
        name = strjoin(glyph(GLYPH_ARROW_DOWN),
×
678
                       " begin unavailable groups ",
679
                       glyph(GLYPH_ARROW_DOWN));
680
        if (!name)
×
681
                return log_oom();
×
682

683
        r = table_add_many(
×
684
                        table,
685
                        TABLE_STRING, glyph(GLYPH_TREE_TOP),
686
                        TABLE_STRING, name,
687
                        TABLE_SET_COLOR, ansi_grey(),
688
                        TABLE_EMPTY,
689
                        TABLE_GID, start,
690
                        TABLE_SET_COLOR, ansi_grey(),
691
                        TABLE_STRING, "First unavailable group",
692
                        TABLE_SET_COLOR, ansi_grey(),
693
                        TABLE_INT, -1); /* sort before any other entry with the same GID */
694
        if (r < 0)
×
695
                return table_log_add_error(r);
×
696

697
        free(name);
×
NEW
698
        name = strjoin(glyph(GLYPH_ARROW_UP),
×
699
                       " end unavailable groups ",
700
                       glyph(GLYPH_ARROW_UP));
701
        if (!name)
×
702
                return log_oom();
×
703

704
        r = table_add_many(
×
705
                        table,
706
                        TABLE_STRING, glyph(GLYPH_TREE_RIGHT),
707
                        TABLE_STRING, name,
708
                        TABLE_SET_COLOR, ansi_grey(),
709
                        TABLE_EMPTY,
710
                        TABLE_GID, end,
711
                        TABLE_SET_COLOR, ansi_grey(),
712
                        TABLE_STRING, "Last unavailable group",
713
                        TABLE_SET_COLOR, ansi_grey(),
714
                        TABLE_INT, 1); /* sort after any other entry with the same GID */
715
        if (r < 0)
×
716
                return table_log_add_error(r);
×
717

718
        return 2;
719
}
720

721
static int display_group(int argc, char *argv[], void *userdata) {
35✔
722
        _cleanup_(table_unrefp) Table *table = NULL;
×
723
        bool draw_separator = false;
35✔
724
        int ret = 0, r;
35✔
725

726
        if (arg_output < 0)
35✔
727
                arg_output = arg_from_file || (argc > 1 && !arg_fuzzy) ? OUTPUT_FRIENDLY : OUTPUT_TABLE;
40✔
728

729
        if (arg_output == OUTPUT_TABLE) {
35✔
730
                table = table_new(" ", "name", "disposition", "gid", "description", "order");
9✔
731
                if (!table)
9✔
732
                        return log_oom();
×
733

734
                (void) table_set_align_percent(table, table_get_cell(table, 0, 3), 100);
9✔
735
                table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
9✔
736
                (void) table_set_sort(table, (size_t) 3, (size_t) 5);
9✔
737
                (void) table_hide_column_from_display(table, (size_t) 5);
9✔
738
                if (!arg_boundaries)
9✔
739
                        (void) table_hide_column_from_display(table, (size_t) 0);
1✔
740
        }
741

742
        _cleanup_(userdb_match_done) UserDBMatch match = {
35✔
743
                .disposition_mask = arg_disposition_mask,
744
                .gid_min = arg_uid_min,
745
                .gid_max = arg_uid_max,
746
        };
747

748
        if (arg_from_file) {
35✔
749
                if (argc > 1)
3✔
750
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No argument expected when invoked with --from-file=, refusing.");
×
751

752
                _cleanup_(group_record_unrefp) GroupRecord *gr = group_record_new();
6✔
753
                if (!gr)
3✔
754
                        return log_oom();
×
755

756
                r = group_record_load(gr, arg_from_file, USER_RECORD_LOAD_MASK_SECRET|USER_RECORD_LOG);
3✔
757
                if (r < 0)
3✔
758
                        return r;
759

760
                r = show_group(gr, table);
3✔
761
                if (r < 0)
3✔
762
                        return r;
763

764
        } else if (argc > 1 && !arg_fuzzy)
32✔
765
                STRV_FOREACH(i, argv + 1) {
56✔
766
                        _cleanup_(group_record_unrefp) GroupRecord *gr = NULL;
33✔
767

768
                        r = groupdb_by_name(*i, &match, arg_userdb_flags|USERDB_PARSE_NUMERIC, &gr);
33✔
769
                        if (r < 0) {
33✔
770
                                if (r == -ESRCH)
12✔
771
                                        log_error_errno(r, "Group %s does not exist.", *i);
10✔
772
                                else if (r == -EHOSTDOWN)
2✔
773
                                        log_error_errno(r, "Selected group database service is not available for this request.");
×
774
                                else if (r == -ENOEXEC)
2✔
775
                                        log_error_errno(r, "Group '%s' exists but does not match specified filter.", *i);
×
776
                                else
777
                                        log_error_errno(r, "Failed to find group %s: %m", *i);
2✔
778

779
                                RET_GATHER(ret, r);
12✔
780
                        } else {
781
                                if (draw_separator && arg_output == OUTPUT_FRIENDLY)
21✔
782
                                        putchar('\n');
4✔
783

784
                                r = show_group(gr, table);
21✔
785
                                if (r < 0)
21✔
786
                                        return r;
×
787

788
                                draw_separator = true;
789
                        }
790
                }
791
        else {
792
                if (argc > 1) {
1✔
793
                        match.fuzzy_names = strv_copy(strv_skip(argv, 1));
1✔
794
                        if (!match.fuzzy_names)
1✔
795
                                return log_oom();
×
796
                }
797

798
                _cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL;
×
799
                r = groupdb_all(&match, arg_userdb_flags, &iterator);
9✔
800
                if (r == -ENOLINK)
9✔
801
                        log_debug_errno(r, "No entries found. (Didn't check via Varlink.)");
×
802
                else if (r == -ESRCH)
9✔
803
                        log_debug_errno(r, "No entries found.");
9✔
804
                else if (r < 0)
9✔
805
                        return log_error_errno(r, "Failed to enumerate groups: %m");
×
806
                else {
807
                        for (;;) {
233✔
808
                                _cleanup_(group_record_unrefp) GroupRecord *gr = NULL;
9✔
809

810
                                r = groupdb_iterator_get(iterator, &match, &gr);
242✔
811
                                if (r == -ESRCH)
242✔
812
                                        break;
813
                                if (r == -EHOSTDOWN)
233✔
814
                                        return log_error_errno(r, "Selected group database service is not available for this request.");
×
815
                                if (r < 0)
233✔
816
                                        return log_error_errno(r, "Failed acquire next group: %m");
×
817

818
                                if (draw_separator && arg_output == OUTPUT_FRIENDLY)
233✔
819
                                        putchar('\n');
×
820

821
                                r = show_group(gr, table);
233✔
822
                                if (r < 0)
233✔
823
                                        return r;
824

825
                                draw_separator = true;
233✔
826
                        }
827
                }
828
        }
829

830
        if (table) {
35✔
831
                int boundary_lines = 0, gid_map_lines = 0;
9✔
832

833
                if (arg_boundaries) {
9✔
834
                        _cleanup_(uid_range_freep) UIDRange *gid_range = NULL;
8✔
835
                        r = uid_range_load_userns(/* path = */ NULL, GID_RANGE_USERNS_INSIDE, &gid_range);
8✔
836
                        if (r < 0)
8✔
837
                                log_debug_errno(r, "Failed to load /proc/self/gid_map, ignoring: %m");
×
838

839
                        boundary_lines = table_add_gid_boundaries(table, gid_range);
8✔
840
                        if (boundary_lines < 0)
8✔
841
                                return boundary_lines;
842

843
                        gid_map_lines = table_add_uid_map(table, gid_range, add_unavailable_gid);
8✔
844
                        if (gid_map_lines < 0)
8✔
845
                                return gid_map_lines;
846
                }
847

848
                if (!table_isempty(table)) {
18✔
849
                        r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
9✔
850
                        if (r < 0)
9✔
851
                                return table_log_print_error(r);
×
852
                }
853

854
                if (arg_legend) {
9✔
855
                        size_t k;
9✔
856

857
                        k = table_get_rows(table) - 1 - boundary_lines - gid_map_lines;
9✔
858
                        if (k > 0)
9✔
859
                                printf("\n%zu groups listed.\n", k);
7✔
860
                        else
861
                                printf("No groups.\n");
2✔
862
                }
863
        }
864

865
        return ret;
866
}
867

868
static int show_membership(const char *user, const char *group, Table *table) {
14✔
869
        int r;
14✔
870

871
        assert(user);
14✔
872
        assert(group);
14✔
873

874
        switch (arg_output) {
14✔
875

876
        case OUTPUT_CLASSIC:
×
877
                /* Strictly speaking there's no 'classic' output for this concept, but let's output it in
878
                 * similar style to the classic output for user/group info */
879

880
                printf("%s:%s\n", user, group);
×
881
                break;
×
882

883
        case OUTPUT_JSON: {
2✔
884
                _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
2✔
885

886
                r = sd_json_buildo(
2✔
887
                                &v,
888
                                SD_JSON_BUILD_PAIR("user", SD_JSON_BUILD_STRING(user)),
889
                                SD_JSON_BUILD_PAIR("group", SD_JSON_BUILD_STRING(group)));
890
                if (r < 0)
2✔
891
                        return log_error_errno(r, "Failed to build JSON object: %m");
×
892

893
                sd_json_variant_dump(v, arg_json_format_flags, NULL, NULL);
2✔
894
                break;
2✔
895
        }
896

897
        case OUTPUT_FRIENDLY:
×
898
                /* Hmm, this is not particularly friendly, but not sure how we could do this better */
899
                printf("%s: %s\n", group, user);
×
900
                break;
×
901

902
        case OUTPUT_TABLE:
12✔
903
                assert(table);
12✔
904

905
                r = table_add_many(
12✔
906
                                table,
907
                                TABLE_STRING, user,
908
                                TABLE_STRING, group);
909
                if (r < 0)
12✔
910
                        return table_log_add_error(r);
×
911

912
                break;
913

914
        default:
×
915
                assert_not_reached();
×
916
        }
917

918
        return 0;
919
}
920

921
static int display_memberships(int argc, char *argv[], void *userdata) {
14✔
922
        _cleanup_(table_unrefp) Table *table = NULL;
14✔
923
        int ret = 0, r;
14✔
924

925
        if (arg_from_file)
14✔
926
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--from-file= not supported when showing memberships, refusing.");
×
927

928
        if (arg_output < 0)
14✔
929
                arg_output = OUTPUT_TABLE;
12✔
930

931
        if (arg_output == OUTPUT_TABLE) {
14✔
932
                table = table_new("user", "group");
12✔
933
                if (!table)
12✔
934
                        return log_oom();
×
935

936
                (void) table_set_sort(table, (size_t) 0, (size_t) 1);
12✔
937
        }
938

939
        if (argc > 1)
14✔
940
                STRV_FOREACH(i, argv + 1) {
26✔
941
                        _cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL;
4✔
942

943
                        if (streq(argv[0], "users-in-group")) {
18✔
944
                                r = membershipdb_by_group(*i, arg_userdb_flags, &iterator);
9✔
945
                                if (r < 0)
9✔
946
                                        return log_error_errno(r, "Failed to enumerate users in group: %m");
2✔
947
                        } else if (streq(argv[0], "groups-of-user")) {
9✔
948
                                r = membershipdb_by_user(*i, arg_userdb_flags, &iterator);
9✔
949
                                if (r < 0)
9✔
950
                                        return log_error_errno(r, "Failed to enumerate groups of user: %m");
2✔
951
                        } else
952
                                assert_not_reached();
×
953

954
                        for (;;) {
22✔
955
                                _cleanup_free_ char *user = NULL, *group = NULL;
18✔
956

957
                                r = membershipdb_iterator_get(iterator, &user, &group);
18✔
958
                                if (r == -ESRCH)
18✔
959
                                        break;
960
                                if (r == -EHOSTDOWN)
4✔
961
                                        return log_error_errno(r, "Selected membership database service is not available for this request.");
×
962
                                if (r < 0)
4✔
963
                                        return log_error_errno(r, "Failed acquire next membership: %m");
×
964

965
                                r = show_membership(user, group, table);
4✔
966
                                if (r < 0)
4✔
967
                                        return r;
968
                        }
969
                }
970
        else {
971
                _cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL;
×
972

973
                r = membershipdb_all(arg_userdb_flags, &iterator);
2✔
974
                if (r == -ENOLINK)
2✔
975
                        log_debug_errno(r, "No entries found. (Didn't check via Varlink.)");
×
976
                else if (r == -ESRCH)
2✔
977
                        log_debug_errno(r, "No entries found.");
2✔
978
                else if (r < 0)
2✔
979
                        return log_error_errno(r, "Failed to enumerate memberships: %m");
×
980
                else {
981
                        for (;;) {
22✔
982
                                _cleanup_free_ char *user = NULL, *group = NULL;
12✔
983

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

992
                                r = show_membership(user, group, table);
10✔
993
                                if (r < 0)
10✔
994
                                        return r;
995
                        }
996
                }
997
        }
998

999
        if (table) {
10✔
1000
                if (!table_isempty(table)) {
8✔
1001
                        r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
4✔
1002
                        if (r < 0)
4✔
1003
                                return table_log_print_error(r);
×
1004
                }
1005

1006
                if (arg_legend) {
8✔
1007
                        if (table_isempty(table))
16✔
1008
                                printf("No memberships.\n");
4✔
1009
                        else
1010
                                printf("\n%zu memberships listed.\n", table_get_rows(table) - 1);
4✔
1011
                }
1012
        }
1013

1014
        return ret;
1015
}
1016

1017
static int display_services(int argc, char *argv[], void *userdata) {
2✔
1018
        _cleanup_(table_unrefp) Table *t = NULL;
×
1019
        _cleanup_closedir_ DIR *d = NULL;
2✔
1020
        int r;
2✔
1021

1022
        if (arg_from_file)
2✔
1023
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--from-file= not supported when showing services, refusing.");
×
1024

1025
        d = opendir("/run/systemd/userdb/");
2✔
1026
        if (!d) {
2✔
1027
                if (errno == ENOENT) {
×
1028
                        log_info("No services.");
×
1029
                        return 0;
×
1030
                }
1031

1032
                return log_error_errno(errno, "Failed to open /run/systemd/userdb/: %m");
×
1033
        }
1034

1035
        t = table_new("service", "listening");
2✔
1036
        if (!t)
2✔
1037
                return log_oom();
×
1038

1039
        (void) table_set_sort(t, (size_t) 0);
2✔
1040

1041
        FOREACH_DIRENT(de, d, return -errno) {
18✔
1042
                _cleanup_free_ char *j = NULL, *no = NULL;
12✔
1043
                _cleanup_close_ int fd = -EBADF;
12✔
1044

1045
                j = path_join("/run/systemd/userdb/", de->d_name);
12✔
1046
                if (!j)
12✔
1047
                        return log_oom();
×
1048

1049
                fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
12✔
1050
                if (fd < 0)
12✔
1051
                        return log_error_errno(errno, "Failed to allocate AF_UNIX/SOCK_STREAM socket: %m");
×
1052

1053
                r = connect_unix_path(fd, dirfd(d), de->d_name);
12✔
1054
                if (r < 0) {
12✔
1055
                        no = strjoin("No (", errno_to_name(r), ")");
×
1056
                        if (!no)
×
1057
                                return log_oom();
×
1058
                }
1059

1060
                r = table_add_many(t,
24✔
1061
                                   TABLE_STRING, de->d_name,
1062
                                   TABLE_STRING, no ?: "yes",
1063
                                   TABLE_SET_COLOR, ansi_highlight_green_red(!no));
1064
                if (r < 0)
12✔
1065
                        return table_log_add_error(r);
×
1066
        }
1067

1068
        if (!table_isempty(t)) {
4✔
1069
                r = table_print_with_pager(t, arg_json_format_flags, arg_pager_flags, arg_legend);
2✔
1070
                if (r < 0)
2✔
1071
                        return table_log_print_error(r);
×
1072
        }
1073

1074
        if (arg_legend && arg_output != OUTPUT_JSON) {
2✔
1075
                if (table_isempty(t))
2✔
1076
                        printf("No services.\n");
×
1077
                else
1078
                        printf("\n%zu services listed.\n", table_get_rows(t) - 1);
1✔
1079
        }
1080

1081
        return 0;
1082
}
1083

1084
static int ssh_authorized_keys(int argc, char *argv[], void *userdata) {
10✔
1085
        _cleanup_(user_record_unrefp) UserRecord *ur = NULL;
×
1086
        char **chain_invocation;
10✔
1087
        int r;
10✔
1088

1089
        assert(argc >= 2);
10✔
1090

1091
        if (arg_from_file)
10✔
1092
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--from-file= not supported when showing SSH authorized keys, refusing.");
×
1093

1094
        if (arg_chain) {
10✔
1095
                /* If --chain is specified, the rest of the command line is the chain command */
1096

1097
                if (argc < 3)
4✔
1098
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1✔
1099
                                               "No chain command line specified, refusing.");
1100

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

1106
                if (!path_is_normalized(argv[2]))
2✔
1107
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1108
                                               "Chain invocation of ssh-authorized-keys commands requires an normalized binary path argument.");
1109

1110
                chain_invocation = argv + 2;
2✔
1111
        } else {
1112
                /* If --chain is not specified, then refuse any further arguments */
1113

1114
                if (argc > 2)
6✔
1115
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Too many arguments.");
×
1116

1117
                chain_invocation = NULL;
1118
        }
1119

1120
        r = userdb_by_name(argv[1], /* match= */ NULL, arg_userdb_flags, &ur);
8✔
1121
        if (r == -ESRCH)
8✔
1122
                log_error_errno(r, "User %s does not exist.", argv[1]);
2✔
1123
        else if (r == -EHOSTDOWN)
6✔
1124
                log_error_errno(r, "Selected user database service is not available for this request.");
×
1125
        else if (r == -EINVAL)
6✔
1126
                log_error_errno(r, "Failed to find user %s: %m (Invalid user name?)", argv[1]);
×
1127
        else if (r < 0)
6✔
1128
                log_error_errno(r, "Failed to find user %s: %m", argv[1]);
×
1129
        else {
1130
                if (strv_isempty(ur->ssh_authorized_keys))
6✔
1131
                        log_debug("User record for %s has no public SSH keys.", argv[1]);
×
1132
                else
1133
                        STRV_FOREACH(i, ur->ssh_authorized_keys)
14✔
1134
                                printf("%s\n", *i);
8✔
1135

1136
                if (ur->incomplete) {
6✔
1137
                        fflush(stdout);
×
1138
                        log_warning("Warning: lacking rights to acquire privileged fields of user record of '%s', output incomplete.", ur->user_name);
×
1139
                }
1140
        }
1141

1142
        if (chain_invocation) {
8✔
1143
                if (DEBUG_LOGGING) {
2✔
1144
                        _cleanup_free_ char *s = NULL;
1✔
1145

1146
                        s = quote_command_line(chain_invocation, SHELL_ESCAPE_EMPTY);
1✔
1147
                        if (!s)
1✔
1148
                                return log_oom();
×
1149

1150
                        log_debug("Chain invoking: %s", s);
1✔
1151
                }
1152

1153
                fflush(stdout);
2✔
1154
                execv(chain_invocation[0], chain_invocation);
×
1155
                if (errno == ENOENT) /* Let's handle ENOENT gracefully */
×
1156
                        log_warning_errno(errno, "Chain executable '%s' does not exist, ignoring chain invocation.", chain_invocation[0]);
8✔
1157
                else {
1158
                        log_error_errno(errno, "Failed to invoke chain executable '%s': %m", chain_invocation[0]);
×
1159
                        if (r >= 0)
×
1160
                                r = -errno;
×
1161
                }
1162
        }
1163

1164
        return r;
1165
}
1166

1167
static int help(int argc, char *argv[], void *userdata) {
1✔
1168
        _cleanup_free_ char *link = NULL;
1✔
1169
        int r;
1✔
1170

1171
        pager_open(arg_pager_flags);
1✔
1172

1173
        r = terminal_urlify_man("userdbctl", "1", &link);
1✔
1174
        if (r < 0)
1✔
1175
                return log_oom();
×
1176

1177
        printf("%s [OPTIONS...] COMMAND ...\n\n"
2✔
1178
               "%sShow user and group information.%s\n"
1179
               "\nCommands:\n"
1180
               "  user [USER…]               Inspect user\n"
1181
               "  group [GROUP…]             Inspect group\n"
1182
               "  users-in-group [GROUP…]    Show users that are members of specified groups\n"
1183
               "  groups-of-user [USER…]     Show groups the specified users are members of\n"
1184
               "  services                   Show enabled database services\n"
1185
               "  ssh-authorized-keys USER   Show SSH authorized keys for user\n"
1186
               "\nOptions:\n"
1187
               "  -h --help                  Show this help\n"
1188
               "     --version               Show package version\n"
1189
               "     --no-pager              Do not pipe output into a pager\n"
1190
               "     --no-legend             Do not show the headers and footers\n"
1191
               "     --output=MODE           Select output mode (classic, friendly, table, json)\n"
1192
               "  -j                         Equivalent to --output=json\n"
1193
               "  -s --service=SERVICE[:SERVICE…]\n"
1194
               "                             Query the specified service\n"
1195
               "     --with-nss=BOOL         Control whether to include glibc NSS data\n"
1196
               "  -N                         Do not synthesize or include glibc NSS data\n"
1197
               "                             (Same as --synthesize=no --with-nss=no)\n"
1198
               "     --synthesize=BOOL       Synthesize root/nobody user\n"
1199
               "     --with-dropin=BOOL      Control whether to include drop-in records\n"
1200
               "     --with-varlink=BOOL     Control whether to talk to services at all\n"
1201
               "     --multiplexer=BOOL      Control whether to use the multiplexer\n"
1202
               "     --json=pretty|short     JSON output mode\n"
1203
               "     --chain                 Chain another command\n"
1204
               "     --uid-min=ID            Filter by minimum UID/GID (default 0)\n"
1205
               "     --uid-max=ID            Filter by maximum UID/GID (default 4294967294)\n"
1206
               "  -z --fuzzy                 Do a fuzzy name search\n"
1207
               "     --disposition=VALUE     Filter by disposition\n"
1208
               "  -I                         Equivalent to --disposition=intrinsic\n"
1209
               "  -S                         Equivalent to --disposition=system\n"
1210
               "  -R                         Equivalent to --disposition=regular\n"
1211
               "     --boundaries=BOOL       Show/hide UID/GID range boundaries in output\n"
1212
               "  -B                         Equivalent to --boundaries=no\n"
1213
               "  -F --from-file=PATH        Read JSON record from file\n"
1214
               "\nSee the %s for details.\n",
1215
               program_invocation_short_name,
1216
               ansi_highlight(),
1217
               ansi_normal(),
1218
               link);
1219

1220
        return 0;
1221
}
1222

1223
static int parse_argv(int argc, char *argv[]) {
195✔
1224

1225
        enum {
195✔
1226
                ARG_VERSION = 0x100,
1227
                ARG_NO_PAGER,
1228
                ARG_NO_LEGEND,
1229
                ARG_OUTPUT,
1230
                ARG_WITH_NSS,
1231
                ARG_WITH_DROPIN,
1232
                ARG_WITH_VARLINK,
1233
                ARG_SYNTHESIZE,
1234
                ARG_MULTIPLEXER,
1235
                ARG_JSON,
1236
                ARG_CHAIN,
1237
                ARG_UID_MIN,
1238
                ARG_UID_MAX,
1239
                ARG_DISPOSITION,
1240
                ARG_BOUNDARIES,
1241
        };
1242

1243
        static const struct option options[] = {
195✔
1244
                { "help",         no_argument,       NULL, 'h'              },
1245
                { "version",      no_argument,       NULL, ARG_VERSION      },
1246
                { "no-pager",     no_argument,       NULL, ARG_NO_PAGER     },
1247
                { "no-legend",    no_argument,       NULL, ARG_NO_LEGEND    },
1248
                { "output",       required_argument, NULL, ARG_OUTPUT       },
1249
                { "service",      required_argument, NULL, 's'              },
1250
                { "with-nss",     required_argument, NULL, ARG_WITH_NSS     },
1251
                { "with-dropin",  required_argument, NULL, ARG_WITH_DROPIN  },
1252
                { "with-varlink", required_argument, NULL, ARG_WITH_VARLINK },
1253
                { "synthesize",   required_argument, NULL, ARG_SYNTHESIZE   },
1254
                { "multiplexer",  required_argument, NULL, ARG_MULTIPLEXER  },
1255
                { "json",         required_argument, NULL, ARG_JSON         },
1256
                { "chain",        no_argument,       NULL, ARG_CHAIN        },
1257
                { "uid-min",      required_argument, NULL, ARG_UID_MIN      },
1258
                { "uid-max",      required_argument, NULL, ARG_UID_MAX      },
1259
                { "fuzzy",        no_argument,       NULL, 'z'              },
1260
                { "disposition",  required_argument, NULL, ARG_DISPOSITION  },
1261
                { "boundaries",   required_argument, NULL, ARG_BOUNDARIES   },
1262
                { "from-file",    required_argument, NULL, 'F'              },
1263
                {}
1264
        };
1265

1266
        const char *e;
195✔
1267
        int r;
195✔
1268

1269
        assert(argc >= 0);
195✔
1270
        assert(argv);
195✔
1271

1272
        /* We are going to update this environment variable with our own, hence let's first read what is already set */
1273
        e = getenv("SYSTEMD_ONLY_USERDB");
195✔
1274
        if (e) {
195✔
1275
                char **l;
×
1276

1277
                l = strv_split(e, ":");
×
1278
                if (!l)
×
1279
                        return log_oom();
×
1280

1281
                strv_free(arg_services);
×
1282
                arg_services = l;
×
1283
        }
1284

1285
        /* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long()
1286
         * that checks for GNU extensions in optstring ('-' or '+' at the beginning). */
1287
        optind = 0;
195✔
1288

1289
        for (;;) {
286✔
1290
                int c;
286✔
1291

1292
                c = getopt_long(argc, argv,
286✔
1293
                                arg_chain ? "+hjs:NISRzBF:" : "hjs:NISRzBF:", /* When --chain was used disable parsing of further switches */
286✔
1294
                                options, NULL);
1295
                if (c < 0)
286✔
1296
                        break;
1297

1298
                switch (c) {
121✔
1299

1300
                case 'h':
1✔
1301
                        return help(0, NULL, NULL);
1✔
1302

1303
                case ARG_VERSION:
1✔
1304
                        return version();
1✔
1305

1306
                case ARG_NO_PAGER:
×
1307
                        arg_pager_flags |= PAGER_DISABLE;
×
1308
                        break;
×
1309

1310
                case ARG_NO_LEGEND:
1✔
1311
                        arg_legend = false;
1✔
1312
                        break;
1✔
1313

1314
                case ARG_OUTPUT:
8✔
1315
                        if (isempty(optarg))
8✔
1316
                                arg_output = _OUTPUT_INVALID;
×
1317
                        else if (streq(optarg, "classic"))
8✔
1318
                                arg_output = OUTPUT_CLASSIC;
1✔
1319
                        else if (streq(optarg, "friendly"))
7✔
1320
                                arg_output = OUTPUT_FRIENDLY;
1✔
1321
                        else if (streq(optarg, "json"))
6✔
1322
                                arg_output = OUTPUT_JSON;
1✔
1323
                        else if (streq(optarg, "table"))
5✔
1324
                                arg_output = OUTPUT_TABLE;
1✔
1325
                        else if (streq(optarg, "help")) {
4✔
1326
                                puts("classic\n"
×
1327
                                     "friendly\n"
1328
                                     "json\n"
1329
                                     "table");
1330
                                return 0;
×
1331
                        } else
1332
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid --output= mode: %s", optarg);
4✔
1333

1334
                        arg_json_format_flags = arg_output == OUTPUT_JSON ? SD_JSON_FORMAT_PRETTY|SD_JSON_FORMAT_COLOR_AUTO : SD_JSON_FORMAT_OFF;
4✔
1335
                        break;
4✔
1336

1337
                case ARG_JSON:
6✔
1338
                        r = parse_json_argument(optarg, &arg_json_format_flags);
6✔
1339
                        if (r <= 0)
6✔
1340
                                return r;
1341

1342
                        arg_output = sd_json_format_enabled(arg_json_format_flags) ? OUTPUT_JSON : _OUTPUT_INVALID;
2✔
1343
                        break;
2✔
1344

1345
                case 'j':
26✔
1346
                        arg_json_format_flags = SD_JSON_FORMAT_PRETTY|SD_JSON_FORMAT_COLOR_AUTO;
26✔
1347
                        arg_output = OUTPUT_JSON;
26✔
1348
                        break;
26✔
1349

1350
                case 's':
×
1351
                        if (isempty(optarg))
×
1352
                                arg_services = strv_free(arg_services);
×
1353
                        else {
1354
                                r = strv_split_and_extend(&arg_services, optarg, ":", /* filter_duplicates = */ true);
×
1355
                                if (r < 0)
×
1356
                                        return log_error_errno(r, "Failed to parse -s/--service= argument: %m");
×
1357
                        }
1358

1359
                        break;
1360

1361
                case 'N':
1✔
1362
                        arg_userdb_flags |= USERDB_EXCLUDE_NSS|USERDB_DONT_SYNTHESIZE_INTRINSIC|USERDB_DONT_SYNTHESIZE_FOREIGN;
1✔
1363
                        break;
1✔
1364

1365
                case ARG_WITH_NSS:
14✔
1366
                        r = parse_boolean_argument("--with-nss=", optarg, NULL);
14✔
1367
                        if (r < 0)
14✔
1368
                                return r;
1369

1370
                        SET_FLAG(arg_userdb_flags, USERDB_EXCLUDE_NSS, !r);
10✔
1371
                        break;
10✔
1372

1373
                case ARG_WITH_DROPIN:
8✔
1374
                        r = parse_boolean_argument("--with-dropin=", optarg, NULL);
8✔
1375
                        if (r < 0)
8✔
1376
                                return r;
1377

1378
                        SET_FLAG(arg_userdb_flags, USERDB_EXCLUDE_DROPIN, !r);
4✔
1379
                        break;
4✔
1380

1381
                case ARG_WITH_VARLINK:
7✔
1382
                        r = parse_boolean_argument("--with-varlink=", optarg, NULL);
7✔
1383
                        if (r < 0)
7✔
1384
                                return r;
1385

1386
                        SET_FLAG(arg_userdb_flags, USERDB_EXCLUDE_VARLINK, !r);
3✔
1387
                        break;
3✔
1388

1389
                case ARG_SYNTHESIZE:
12✔
1390
                        r = parse_boolean_argument("--synthesize=", optarg, NULL);
12✔
1391
                        if (r < 0)
12✔
1392
                                return r;
1393

1394
                        SET_FLAG(arg_userdb_flags, USERDB_DONT_SYNTHESIZE_INTRINSIC|USERDB_DONT_SYNTHESIZE_FOREIGN, !r);
8✔
1395
                        break;
8✔
1396

1397
                case ARG_MULTIPLEXER:
6✔
1398
                        r = parse_boolean_argument("--multiplexer=", optarg, NULL);
6✔
1399
                        if (r < 0)
6✔
1400
                                return r;
1401

1402
                        SET_FLAG(arg_userdb_flags, USERDB_AVOID_MULTIPLEXER, !r);
2✔
1403
                        break;
2✔
1404

1405
                case ARG_CHAIN:
4✔
1406
                        arg_chain = true;
4✔
1407
                        break;
4✔
1408

1409
                case ARG_DISPOSITION: {
4✔
1410
                        UserDisposition d = user_disposition_from_string(optarg);
4✔
1411
                        if (d < 0)
4✔
1412
                                return log_error_errno(d, "Unknown user disposition: %s", optarg);
×
1413

1414
                        if (arg_disposition_mask == UINT64_MAX)
4✔
1415
                                arg_disposition_mask = 0;
2✔
1416

1417
                        arg_disposition_mask |= UINT64_C(1) << d;
4✔
1418
                        break;
4✔
1419
                }
1420

1421
                case 'I':
2✔
1422
                        if (arg_disposition_mask == UINT64_MAX)
2✔
1423
                                arg_disposition_mask = 0;
2✔
1424

1425
                        arg_disposition_mask |= UINT64_C(1) << USER_INTRINSIC;
2✔
1426
                        break;
2✔
1427

1428
                case 'S':
4✔
1429
                        if (arg_disposition_mask == UINT64_MAX)
4✔
1430
                                arg_disposition_mask = 0;
2✔
1431

1432
                        arg_disposition_mask |= UINT64_C(1) << USER_SYSTEM;
4✔
1433
                        break;
4✔
1434

1435
                case 'R':
2✔
1436
                        if (arg_disposition_mask == UINT64_MAX)
2✔
1437
                                arg_disposition_mask = 0;
2✔
1438

1439
                        arg_disposition_mask |= UINT64_C(1) << USER_REGULAR;
2✔
1440
                        break;
2✔
1441

1442
                case ARG_UID_MIN:
2✔
1443
                        r = parse_uid(optarg, &arg_uid_min);
2✔
1444
                        if (r < 0)
2✔
1445
                                return log_error_errno(r, "Failed to parse --uid-min= value: %s", optarg);
×
1446
                        break;
1447

1448
                case ARG_UID_MAX:
2✔
1449
                        r = parse_uid(optarg, &arg_uid_max);
2✔
1450
                        if (r < 0)
2✔
1451
                                return log_error_errno(r, "Failed to parse --uid-max= value: %s", optarg);
×
1452
                        break;
1453

1454
                case 'z':
2✔
1455
                        arg_fuzzy = true;
2✔
1456
                        break;
2✔
1457

1458
                case ARG_BOUNDARIES:
×
1459
                        r = parse_boolean_argument("boundaries", optarg, &arg_boundaries);
×
1460
                        if (r < 0)
×
1461
                                return r;
1462
                        break;
1463

1464
                case 'B':
2✔
1465
                        arg_boundaries = false;
2✔
1466
                        break;
2✔
1467

1468
                case 'F': {
6✔
1469
                        if (isempty(optarg)) {
6✔
1470
                                arg_from_file = sd_json_variant_unref(arg_from_file);
×
1471
                                break;
6✔
1472
                        }
1473

1474
                        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
6✔
1475
                        const char *fn = streq(optarg, "-") ? NULL : optarg;
6✔
1476
                        unsigned line = 0;
6✔
1477
                        r = sd_json_parse_file(fn ? NULL : stdin, fn ?: "<stdin>", SD_JSON_PARSE_SENSITIVE, &v, &line, /* reterr_column= */ NULL);
6✔
1478
                        if (r < 0)
6✔
1479
                                return log_syntax(/* unit= */ NULL, LOG_ERR, fn ?: "<stdin>", line, r, "JSON parse failure.");
×
1480

1481
                        sd_json_variant_unref(arg_from_file);
6✔
1482
                        arg_from_file = TAKE_PTR(v);
6✔
1483
                        break;
6✔
1484
                }
1485

1486
                case '?':
1487
                        return -EINVAL;
1488

1489
                default:
×
1490
                        assert_not_reached();
×
1491
                }
1492
        }
1493

1494
        if (arg_uid_min > arg_uid_max)
165✔
1495
                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);
×
1496

1497
        /* If not mask was specified, use the all bits on mask */
1498
        if (arg_disposition_mask == UINT64_MAX)
165✔
1499
                arg_disposition_mask = USER_DISPOSITION_MASK_ALL;
157✔
1500

1501
        if (arg_from_file)
165✔
1502
                arg_boundaries = false;
6✔
1503

1504
        return 1;
1505
}
1506

1507
static int run(int argc, char *argv[]) {
195✔
1508
        static const Verb verbs[] = {
195✔
1509
                { "help",                VERB_ANY, VERB_ANY, 0,            help                },
1510
                { "user",                VERB_ANY, VERB_ANY, VERB_DEFAULT, display_user        },
1511
                { "group",               VERB_ANY, VERB_ANY, 0,            display_group       },
1512
                { "users-in-group",      VERB_ANY, VERB_ANY, 0,            display_memberships },
1513
                { "groups-of-user",      VERB_ANY, VERB_ANY, 0,            display_memberships },
1514
                { "services",            VERB_ANY, 1,        0,            display_services    },
1515

1516
                /* This one is a helper for sshd_config's AuthorizedKeysCommand= setting, it's not a
1517
                 * user-facing verb and thus should not appear in man pages or --help texts. */
1518
                { "ssh-authorized-keys", 2,        VERB_ANY, 0,            ssh_authorized_keys },
1519
                {}
1520
        };
1521

1522
        int r;
195✔
1523

1524
        log_setup();
195✔
1525

1526
        r = parse_argv(argc, argv);
195✔
1527
        if (r <= 0)
195✔
1528
                return r;
1529

1530
        if (arg_services) {
165✔
1531
                _cleanup_free_ char *e = NULL;
×
1532

1533
                e = strv_join(arg_services, ":");
×
1534
                if (!e)
×
1535
                        return log_oom();
×
1536

1537
                if (setenv("SYSTEMD_ONLY_USERDB", e, true) < 0)
×
1538
                        return log_error_errno(r, "Failed to set $SYSTEMD_ONLY_USERDB: %m");
×
1539

1540
                log_info("Enabled services: %s", e);
×
1541
        } else
1542
                assert_se(unsetenv("SYSTEMD_ONLY_USERDB") == 0);
165✔
1543

1544
        return dispatch_verb(argc, argv, verbs, NULL);
165✔
1545
}
1546

1547
DEFINE_MAIN_FUNCTION(run);
390✔
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