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

systemd / systemd / 15455298557

04 Jun 2025 11:44PM UTC coverage: 72.075% (+0.06%) from 72.013%
15455298557

push

github

yuwata
vmspawn: fix call to GetUnitByPID

This commit also adds a handler for SIGRTMIN+4 which is another signal
used to shutdown systemd.

0 of 34 new or added lines in 1 file covered. (0.0%)

1245 existing lines in 42 files now uncovered.

299811 of 415971 relevant lines covered (72.07%)

707146.64 hits per line

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

72.81
/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-util.h"
30
#include "strv.h"
31
#include "uid-classification.h"
32
#include "uid-range.h"
33
#include "umask-util.h"
34
#include "user-record-show.h"
35
#include "user-util.h"
36
#include "userdb.h"
37
#include "verbs.h"
38
#include "virt.h"
39

40
static enum {
41
        OUTPUT_CLASSIC,
42
        OUTPUT_TABLE,
43
        OUTPUT_FRIENDLY,
44
        OUTPUT_JSON,
45
        _OUTPUT_INVALID = -EINVAL,
46
} arg_output = _OUTPUT_INVALID;
47

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

61
STATIC_DESTRUCTOR_REGISTER(arg_services, strv_freep);
313✔
62
STATIC_DESTRUCTOR_REGISTER(arg_from_file, sd_json_variant_unrefp);
313✔
63

64
static const char *user_disposition_to_color(UserDisposition d) {
570✔
65
        assert(d >= 0);
570✔
66
        assert(d < _USER_DISPOSITION_MAX);
570✔
67

68
        switch (d) {
570✔
69
        case USER_INTRINSIC:
70
                return ansi_red();
32✔
71

72
        case USER_SYSTEM:
73
        case USER_DYNAMIC:
74
                return ansi_green();
512✔
75

76
        case USER_CONTAINER:
77
        case USER_FOREIGN:
78
                return ansi_cyan();
10✔
79

80
        case USER_RESERVED:
81
                return ansi_red();
×
82

83
        default:
84
                return NULL;
85
        }
86
}
87

88
static const char* shell_to_color(const char *shell) {
282✔
89
        return !shell || is_nologin_shell(shell) ? ansi_grey() : NULL;
282✔
90
}
91

92
static int show_user(UserRecord *ur, Table *table) {
488✔
93
        int r;
488✔
94

95
        assert(ur);
488✔
96

97
        switch (arg_output) {
488✔
98

99
        case OUTPUT_CLASSIC:
26✔
100
                if (!uid_is_valid(ur->uid))
26✔
101
                        break;
102

103
                printf("%s:x:" UID_FMT ":" GID_FMT ":%s:%s:%s\n",
52✔
104
                       ur->user_name,
105
                       ur->uid,
106
                       user_record_gid(ur),
107
                       strempty(user_record_real_name(ur)),
26✔
108
                       user_record_home_directory(ur),
109
                       user_record_shell(ur));
110

111
                break;
26✔
112

113
        case OUTPUT_JSON:
96✔
114
                sd_json_variant_dump(ur->json, arg_json_format_flags, NULL, NULL);
96✔
115
                break;
96✔
116

117
        case OUTPUT_FRIENDLY:
84✔
118
                user_record_show(ur, true);
84✔
119

120
                if (ur->incomplete) {
84✔
121
                        fflush(stdout);
×
122
                        log_warning("Warning: lacking rights to acquire privileged fields of user record of '%s', output incomplete.", ur->user_name);
×
123
                }
124

125
                break;
126

127
        case OUTPUT_TABLE: {
282✔
128
                assert(table);
282✔
129
                UserDisposition d = user_record_disposition(ur);
282✔
130
                const char *sh = user_record_shell(ur);
282✔
131

132
                r = table_add_many(
486✔
133
                                table,
134
                                TABLE_STRING, "",
135
                                TABLE_STRING, ur->user_name,
136
                                TABLE_SET_COLOR, user_disposition_to_color(d),
137
                                TABLE_STRING, user_disposition_to_string(d),
138
                                TABLE_UID, ur->uid,
139
                                TABLE_GID, user_record_gid(ur),
140
                                TABLE_STRING, empty_to_null(ur->real_name),
141
                                TABLE_PATH, user_record_home_directory(ur),
142
                                TABLE_PATH, sh,
143
                                TABLE_SET_COLOR, shell_to_color(sh),
144
                                TABLE_INT, 0);
145
                if (r < 0)
282✔
146
                        return table_log_add_error(r);
×
147

148
                break;
149
        }
150

151
        default:
×
152
                assert_not_reached();
×
153
        }
154

155
        return 0;
156
}
157

158
static bool test_show_mapped(void) {
18✔
159
        /* Show mapped user range only in environments where user mapping is a thing. */
160
        return running_in_userns() > 0;
18✔
161
}
162

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

216
static int table_add_uid_boundaries(Table *table, const UIDRange *p) {
15✔
217
        int r, n_added = 0;
15✔
218

219
        assert(table);
15✔
220

221
        FOREACH_ELEMENT(i, uid_range_table) {
120✔
222
                _cleanup_free_ char *name = NULL, *comment = NULL;
65✔
223

224
                if (!BIT_SET(arg_disposition_mask, i->disposition))
105✔
225
                        continue;
28✔
226

227
                if (!uid_range_covers(p, i->first, i->last - i->first + 1))
77✔
UNCOV
228
                        continue;
×
229

230
                if (i->test && !i->test())
77✔
231
                        continue;
12✔
232

233
                name = strjoin(glyph(GLYPH_ARROW_DOWN),
65✔
234
                               " begin ", i->name, " users ",
235
                               glyph(GLYPH_ARROW_DOWN));
236
                if (!name)
65✔
UNCOV
237
                        return log_oom();
×
238

239
                comment = strjoin("First ", i->name, " user");
65✔
240
                if (!comment)
65✔
UNCOV
241
                        return log_oom();
×
242

243
                r = table_add_many(
65✔
244
                                table,
245
                                TABLE_STRING, glyph(GLYPH_TREE_TOP),
246
                                TABLE_STRING, name,
247
                                TABLE_SET_COLOR, ansi_grey(),
248
                                TABLE_STRING, user_disposition_to_string(i->disposition),
249
                                TABLE_SET_COLOR, ansi_grey(),
250
                                TABLE_UID, i->first,
251
                                TABLE_SET_COLOR, ansi_grey(),
252
                                TABLE_EMPTY,
253
                                TABLE_STRING, comment,
254
                                TABLE_SET_COLOR, ansi_grey(),
255
                                TABLE_EMPTY,
256
                                TABLE_EMPTY,
257
                                TABLE_INT, -1); /* sort before any other entry with the same UID */
258
                if (r < 0)
65✔
UNCOV
259
                        return table_log_add_error(r);
×
260

261
                free(name);
65✔
262
                name = strjoin(glyph(GLYPH_ARROW_UP),
65✔
263
                               " end ", i->name, " users ",
264
                               glyph(GLYPH_ARROW_UP));
265
                if (!name)
65✔
UNCOV
266
                        return log_oom();
×
267

268
                free(comment);
65✔
269
                comment = strjoin("Last ", i->name, " user");
65✔
270
                if (!comment)
65✔
UNCOV
271
                        return log_oom();
×
272

273
                r = table_add_many(
65✔
274
                                table,
275
                                TABLE_STRING, glyph(GLYPH_TREE_RIGHT),
276
                                TABLE_STRING, name,
277
                                TABLE_SET_COLOR, ansi_grey(),
278
                                TABLE_STRING, user_disposition_to_string(i->disposition),
279
                                TABLE_SET_COLOR, ansi_grey(),
280
                                TABLE_UID, i->last,
281
                                TABLE_SET_COLOR, ansi_grey(),
282
                                TABLE_EMPTY,
283
                                TABLE_STRING, comment,
284
                                TABLE_SET_COLOR, ansi_grey(),
285
                                TABLE_EMPTY,
286
                                TABLE_EMPTY,
287
                                TABLE_INT, 1); /* sort after any other entry with the same UID */
288
                if (r < 0)
65✔
UNCOV
289
                        return table_log_add_error(r);
×
290

291
                n_added += 2;
65✔
292
        }
293

294
        return n_added;
295
}
296

UNCOV
297
static int add_unavailable_uid(Table *table, uid_t start, uid_t end) {
×
298
        _cleanup_free_ char *name = NULL;
×
UNCOV
299
        int r;
×
300

301
        assert(table);
×
302
        assert(start <= end);
×
303

304
        name = strjoin(glyph(GLYPH_ARROW_DOWN),
×
305
                       " begin unavailable users ",
306
                       glyph(GLYPH_ARROW_DOWN));
UNCOV
307
        if (!name)
×
UNCOV
308
                return log_oom();
×
309

UNCOV
310
        r = table_add_many(
×
311
                        table,
312
                        TABLE_STRING, glyph(GLYPH_TREE_TOP),
313
                        TABLE_STRING, name,
314
                        TABLE_SET_COLOR, ansi_grey(),
315
                        TABLE_EMPTY,
316
                        TABLE_UID, start,
317
                        TABLE_SET_COLOR, ansi_grey(),
318
                        TABLE_EMPTY,
319
                        TABLE_STRING, "First unavailable user",
320
                        TABLE_SET_COLOR, ansi_grey(),
321
                        TABLE_EMPTY,
322
                        TABLE_EMPTY,
323
                        TABLE_INT, -1); /* sort before an other entry with the same UID */
UNCOV
324
        if (r < 0)
×
325
                return table_log_add_error(r);
×
326

UNCOV
327
        free(name);
×
328
        name = strjoin(glyph(GLYPH_ARROW_UP),
×
329
                       " end unavailable users ",
330
                       glyph(GLYPH_ARROW_UP));
UNCOV
331
        if (!name)
×
UNCOV
332
                return log_oom();
×
333

UNCOV
334
        r = table_add_many(
×
335
                        table,
336
                        TABLE_STRING, glyph(GLYPH_TREE_RIGHT),
337
                        TABLE_STRING, name,
338
                        TABLE_SET_COLOR, ansi_grey(),
339
                        TABLE_EMPTY,
340
                        TABLE_UID, end,
341
                        TABLE_SET_COLOR, ansi_grey(),
342
                        TABLE_EMPTY,
343
                        TABLE_STRING, "Last unavailable user",
344
                        TABLE_SET_COLOR, ansi_grey(),
345
                        TABLE_EMPTY,
346
                        TABLE_EMPTY,
347
                        TABLE_INT, 1); /* sort after any other entry with the same UID */
UNCOV
348
        if (r < 0)
×
UNCOV
349
                return table_log_add_error(r);
×
350

351
        return 2;
352
}
353

354
static int table_add_uid_map(
24✔
355
                Table *table,
356
                const UIDRange *p,
357
                int (*add_unavailable)(Table *t, uid_t start, uid_t end)) {
358

359
        uid_t focus = 0;
24✔
360
        int n_added = 0, r;
24✔
361

362
        assert(table);
24✔
363
        assert(add_unavailable);
24✔
364

365
        if (!p)
24✔
366
                return 0;
367

368
        FOREACH_ARRAY(x, p->entries, p->n_entries) {
48✔
369
                if (focus < x->start) {
24✔
UNCOV
370
                        r = add_unavailable(table, focus, x->start-1);
×
UNCOV
371
                        if (r < 0)
×
372
                                return r;
373

UNCOV
374
                        n_added += r;
×
375
                }
376

377
                if (x->start > UINT32_MAX - x->nr) { /* overflow check */
24✔
378
                        focus = UINT32_MAX;
379
                        break;
380
                }
381

382
                focus = x->start + x->nr;
24✔
383
        }
384

385
        if (focus < UINT32_MAX-1) {
24✔
UNCOV
386
                r = add_unavailable(table, focus, UINT32_MAX-1);
×
UNCOV
387
                if (r < 0)
×
388
                        return r;
389

UNCOV
390
                n_added += r;
×
391
        }
392

393
        return n_added;
394
}
395

396
static int display_user(int argc, char *argv[], void *userdata) {
106✔
397
        _cleanup_(table_unrefp) Table *table = NULL;
106✔
398
        bool draw_separator = false;
106✔
399
        int ret = 0, r;
106✔
400

401
        if (arg_output < 0)
106✔
402
                arg_output = arg_from_file || (argc > 1 && !arg_fuzzy) ? OUTPUT_FRIENDLY : OUTPUT_TABLE;
98✔
403

404
        if (arg_output == OUTPUT_TABLE) {
106✔
405
                table = table_new(" ", "name", "disposition", "uid", "gid", "realname", "home", "shell", "order");
16✔
406
                if (!table)
16✔
UNCOV
407
                        return log_oom();
×
408

409
                (void) table_set_align_percent(table, table_get_cell(table, 0, 3), 100);
16✔
410
                (void) table_set_align_percent(table, table_get_cell(table, 0, 4), 100);
16✔
411
                table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
16✔
412
                (void) table_set_sort(table, (size_t) 3, (size_t) 8);
16✔
413
                (void) table_hide_column_from_display(table, (size_t) 8);
16✔
414
                if (!arg_boundaries)
16✔
415
                        (void) table_hide_column_from_display(table, (size_t) 0);
1✔
416
        }
417

418
        _cleanup_(userdb_match_done) UserDBMatch match = {
106✔
419
                .disposition_mask = arg_disposition_mask,
420
                .uid_min = arg_uid_min,
421
                .uid_max = arg_uid_max,
422
        };
423

424
        if (arg_from_file) {
106✔
425
                if (argc > 1)
3✔
UNCOV
426
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No argument expected when invoked with --from-file=, refusing.");
×
427

428
                _cleanup_(user_record_unrefp) UserRecord *ur = user_record_new();
6✔
429
                if (!ur)
3✔
UNCOV
430
                        return log_oom();
×
431

432
                r = user_record_load(ur, arg_from_file, USER_RECORD_LOAD_MASK_SECRET|USER_RECORD_LOG);
3✔
433
                if (r < 0)
3✔
434
                        return r;
435

436
                r = show_user(ur, table);
3✔
437
                if (r < 0)
3✔
438
                        return r;
439

440
        } else if (argc > 1 && !arg_fuzzy)
103✔
441
                STRV_FOREACH(i, argv + 1) {
175✔
442
                        _cleanup_(user_record_unrefp) UserRecord *ur = NULL;
93✔
443

444
                        r = userdb_by_name(*i, &match, arg_userdb_flags|USERDB_PARSE_NUMERIC, &ur);
93✔
445
                        if (r < 0) {
93✔
446
                                if (r == -ESRCH)
20✔
447
                                        log_error_errno(r, "User %s does not exist.", *i);
16✔
448
                                else if (r == -EHOSTDOWN)
4✔
UNCOV
449
                                        log_error_errno(r, "Selected user database service is not available for this request.");
×
450
                                else if (r == -ENOEXEC)
4✔
UNCOV
451
                                        log_error_errno(r, "User '%s' exists but does not match specified filter.", *i);
×
452
                                else
453
                                        log_error_errno(r, "Failed to find user %s: %m", *i);
4✔
454

455
                                RET_GATHER(ret, r);
20✔
456
                        } else {
457
                                if (draw_separator && arg_output == OUTPUT_FRIENDLY)
73✔
458
                                        putchar('\n');
4✔
459

460
                                r = show_user(ur, table);
73✔
461
                                if (r < 0)
73✔
UNCOV
462
                                        return r;
×
463

464
                                draw_separator = true;
465
                        }
466
                }
467
        else {
468
                if (argc > 1) {
1✔
469
                        /* If there are further arguments, they are the fuzzy match strings. */
470
                        match.fuzzy_names = strv_copy(strv_skip(argv, 1));
1✔
471
                        if (!match.fuzzy_names)
1✔
472
                                return log_oom();
×
473
                }
474

UNCOV
475
                _cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL;
×
476
                r = userdb_all(&match, arg_userdb_flags, &iterator);
21✔
477
                if (r == -ENOLINK) /* ENOLINK → Didn't find answer without Varlink, and didn't try Varlink because was configured to off. */
21✔
UNCOV
478
                        log_debug_errno(r, "No entries found. (Didn't check via Varlink.)");
×
479
                else if (r == -ESRCH) /* ESRCH → Couldn't find any suitable entry, but we checked all sources */
21✔
480
                        log_debug_errno(r, "No entries found.");
21✔
481
                else if (r < 0)
21✔
UNCOV
482
                        return log_error_errno(r, "Failed to enumerate users: %m");
×
483
                else {
484
                        for (;;) {
412✔
485
                                _cleanup_(user_record_unrefp) UserRecord *ur = NULL;
21✔
486

487
                                r = userdb_iterator_get(iterator, &match, &ur);
433✔
488
                                if (r == -ESRCH)
433✔
489
                                        break;
490
                                if (r == -EHOSTDOWN)
412✔
UNCOV
491
                                        return log_error_errno(r, "Selected user database service is not available for this request.");
×
492
                                if (r < 0)
412✔
UNCOV
493
                                        return log_error_errno(r, "Failed acquire next user: %m");
×
494

495
                                if (draw_separator && arg_output == OUTPUT_FRIENDLY)
412✔
496
                                        putchar('\n');
25✔
497

498
                                r = show_user(ur, table);
412✔
499
                                if (r < 0)
412✔
500
                                        return r;
501

502
                                draw_separator = true;
412✔
503
                        }
504
                }
505
        }
506

507
        if (table) {
106✔
508
                int boundary_lines = 0, uid_map_lines = 0;
16✔
509

510
                if (arg_boundaries) {
16✔
511
                        _cleanup_(uid_range_freep) UIDRange *uid_range = NULL;
15✔
512

513
                        r = uid_range_load_userns(/* path = */ NULL, UID_RANGE_USERNS_INSIDE, &uid_range);
15✔
514
                        if (r < 0)
15✔
UNCOV
515
                                log_debug_errno(r, "Failed to load /proc/self/uid_map, ignoring: %m");
×
516

517
                        boundary_lines = table_add_uid_boundaries(table, uid_range);
15✔
518
                        if (boundary_lines < 0)
15✔
519
                                return boundary_lines;
520

521
                        uid_map_lines = table_add_uid_map(table, uid_range, add_unavailable_uid);
15✔
522
                        if (uid_map_lines < 0)
15✔
523
                                return uid_map_lines;
524
                }
525

526
                if (!table_isempty(table)) {
32✔
527
                        r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
16✔
528
                        if (r < 0)
16✔
UNCOV
529
                                return table_log_print_error(r);
×
530
                }
531

532
                if (arg_legend) {
16✔
533
                        size_t k;
14✔
534

535
                        k = table_get_rows(table) - 1 - boundary_lines - uid_map_lines;
14✔
536
                        if (k > 0)
14✔
537
                                printf("\n%zu users listed.\n", k);
12✔
538
                        else
539
                                printf("No users.\n");
2✔
540
                }
541
        }
542

543
        return ret;
544
}
545

546
static int show_group(GroupRecord *gr, Table *table) {
312✔
547
        int r;
312✔
548

549
        assert(gr);
312✔
550

551
        switch (arg_output) {
312✔
552

553
        case OUTPUT_CLASSIC: {
×
554
                _cleanup_free_ char *m = NULL;
×
555

UNCOV
556
                if (!gid_is_valid(gr->gid))
×
557
                        break;
558

UNCOV
559
                m = strv_join(gr->members, ",");
×
UNCOV
560
                if (!m)
×
UNCOV
561
                        return log_oom();
×
562

UNCOV
563
                printf("%s:x:" GID_FMT ":%s\n",
×
564
                       gr->group_name,
565
                       gr->gid,
566
                       m);
567
                break;
568
        }
569

570
        case OUTPUT_JSON:
5✔
571
                sd_json_variant_dump(gr->json, arg_json_format_flags, NULL, NULL);
5✔
572
                break;
5✔
573

574
        case OUTPUT_FRIENDLY:
19✔
575
                group_record_show(gr, true);
19✔
576

577
                if (gr->incomplete) {
19✔
UNCOV
578
                        fflush(stdout);
×
UNCOV
579
                        log_warning("Warning: lacking rights to acquire privileged fields of group record of '%s', output incomplete.", gr->group_name);
×
580
                }
581

582
                break;
583

584
        case OUTPUT_TABLE: {
288✔
585
                UserDisposition d;
288✔
586

587
                assert(table);
288✔
588
                d = group_record_disposition(gr);
288✔
589

590
                r = table_add_many(
288✔
591
                                table,
592
                                TABLE_STRING, "",
593
                                TABLE_STRING, gr->group_name,
594
                                TABLE_SET_COLOR, user_disposition_to_color(d),
595
                                TABLE_STRING, user_disposition_to_string(d),
596
                                TABLE_GID, gr->gid,
597
                                TABLE_STRING, gr->description,
598
                                TABLE_INT, 0);
599
                if (r < 0)
288✔
600
                        return table_log_add_error(r);
×
601

602
                break;
603
        }
604

UNCOV
605
        default:
×
UNCOV
606
                assert_not_reached();
×
607
        }
608

609
        return 0;
610
}
611

612
static int table_add_gid_boundaries(Table *table, const UIDRange *p) {
9✔
613
        int r, n_added = 0;
9✔
614

615
        assert(table);
9✔
616

617
        FOREACH_ELEMENT(i, uid_range_table) {
72✔
618
                _cleanup_free_ char *name = NULL, *comment = NULL;
29✔
619

620
                if (!BIT_SET(arg_disposition_mask, i->disposition))
63✔
621
                        continue;
28✔
622

623
                if (!uid_range_covers(p, i->first, i->last - i->first + 1))
35✔
UNCOV
624
                        continue;
×
625

626
                if (i->test && !i->test())
35✔
627
                        continue;
6✔
628

629
                name = strjoin(glyph(GLYPH_ARROW_DOWN),
29✔
630
                               " begin ", i->name, " groups ",
631
                               glyph(GLYPH_ARROW_DOWN));
632
                if (!name)
29✔
UNCOV
633
                        return log_oom();
×
634

635
                comment = strjoin("First ", i->name, " group");
29✔
636
                if (!comment)
29✔
UNCOV
637
                        return log_oom();
×
638

639
                r = table_add_many(
29✔
640
                                table,
641
                                TABLE_STRING, glyph(GLYPH_TREE_TOP),
642
                                TABLE_STRING, name,
643
                                TABLE_SET_COLOR, ansi_grey(),
644
                                TABLE_STRING, user_disposition_to_string(i->disposition),
645
                                TABLE_SET_COLOR, ansi_grey(),
646
                                TABLE_GID, i->first,
647
                                TABLE_SET_COLOR, ansi_grey(),
648
                                TABLE_STRING, comment,
649
                                TABLE_SET_COLOR, ansi_grey(),
650
                                TABLE_INT, -1); /* sort before any other entry with the same GID */
651
                if (r < 0)
29✔
UNCOV
652
                        return table_log_add_error(r);
×
653

654
                free(name);
29✔
655
                name = strjoin(glyph(GLYPH_ARROW_UP),
29✔
656
                               " end ", i->name, " groups ",
657
                               glyph(GLYPH_ARROW_UP));
658
                if (!name)
29✔
UNCOV
659
                        return log_oom();
×
660

661
                free(comment);
29✔
662
                comment = strjoin("Last ", i->name, " group");
29✔
663
                if (!comment)
29✔
UNCOV
664
                        return log_oom();
×
665

666
                r = table_add_many(
29✔
667
                                table,
668
                                TABLE_STRING, glyph(GLYPH_TREE_RIGHT),
669
                                TABLE_STRING, name,
670
                                TABLE_SET_COLOR, ansi_grey(),
671
                                TABLE_STRING, user_disposition_to_string(i->disposition),
672
                                TABLE_SET_COLOR, ansi_grey(),
673
                                TABLE_GID, i->last,
674
                                TABLE_SET_COLOR, ansi_grey(),
675
                                TABLE_STRING, comment,
676
                                TABLE_SET_COLOR, ansi_grey(),
677
                                TABLE_INT, 1); /* sort after any other entry with the same GID */
678
                if (r < 0)
29✔
UNCOV
679
                        return table_log_add_error(r);
×
680

681
                n_added += 2;
29✔
682
        }
683

684
        return n_added;
685
}
686

UNCOV
687
static int add_unavailable_gid(Table *table, uid_t start, uid_t end) {
×
688
        _cleanup_free_ char *name = NULL;
×
UNCOV
689
        int r;
×
690

691
        assert(table);
×
692
        assert(start <= end);
×
693

694
        name = strjoin(glyph(GLYPH_ARROW_DOWN),
×
695
                       " begin unavailable groups ",
696
                       glyph(GLYPH_ARROW_DOWN));
UNCOV
697
        if (!name)
×
UNCOV
698
                return log_oom();
×
699

UNCOV
700
        r = table_add_many(
×
701
                        table,
702
                        TABLE_STRING, glyph(GLYPH_TREE_TOP),
703
                        TABLE_STRING, name,
704
                        TABLE_SET_COLOR, ansi_grey(),
705
                        TABLE_EMPTY,
706
                        TABLE_GID, start,
707
                        TABLE_SET_COLOR, ansi_grey(),
708
                        TABLE_STRING, "First unavailable group",
709
                        TABLE_SET_COLOR, ansi_grey(),
710
                        TABLE_INT, -1); /* sort before any other entry with the same GID */
UNCOV
711
        if (r < 0)
×
712
                return table_log_add_error(r);
×
713

UNCOV
714
        free(name);
×
715
        name = strjoin(glyph(GLYPH_ARROW_UP),
×
716
                       " end unavailable groups ",
717
                       glyph(GLYPH_ARROW_UP));
UNCOV
718
        if (!name)
×
UNCOV
719
                return log_oom();
×
720

UNCOV
721
        r = table_add_many(
×
722
                        table,
723
                        TABLE_STRING, glyph(GLYPH_TREE_RIGHT),
724
                        TABLE_STRING, name,
725
                        TABLE_SET_COLOR, ansi_grey(),
726
                        TABLE_EMPTY,
727
                        TABLE_GID, end,
728
                        TABLE_SET_COLOR, ansi_grey(),
729
                        TABLE_STRING, "Last unavailable group",
730
                        TABLE_SET_COLOR, ansi_grey(),
731
                        TABLE_INT, 1); /* sort after any other entry with the same GID */
UNCOV
732
        if (r < 0)
×
UNCOV
733
                return table_log_add_error(r);
×
734

735
        return 2;
736
}
737

738
static int display_group(int argc, char *argv[], void *userdata) {
36✔
739
        _cleanup_(table_unrefp) Table *table = NULL;
36✔
740
        bool draw_separator = false;
36✔
741
        int ret = 0, r;
36✔
742

743
        if (arg_output < 0)
36✔
744
                arg_output = arg_from_file || (argc > 1 && !arg_fuzzy) ? OUTPUT_FRIENDLY : OUTPUT_TABLE;
42✔
745

746
        if (arg_output == OUTPUT_TABLE) {
36✔
747
                table = table_new(" ", "name", "disposition", "gid", "description", "order");
10✔
748
                if (!table)
10✔
UNCOV
749
                        return log_oom();
×
750

751
                (void) table_set_align_percent(table, table_get_cell(table, 0, 3), 100);
10✔
752
                table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
10✔
753
                (void) table_set_sort(table, (size_t) 3, (size_t) 5);
10✔
754
                (void) table_hide_column_from_display(table, (size_t) 5);
10✔
755
                if (!arg_boundaries)
10✔
756
                        (void) table_hide_column_from_display(table, (size_t) 0);
1✔
757
        }
758

759
        _cleanup_(userdb_match_done) UserDBMatch match = {
36✔
760
                .disposition_mask = arg_disposition_mask,
761
                .gid_min = arg_uid_min,
762
                .gid_max = arg_uid_max,
763
        };
764

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

769
                _cleanup_(group_record_unrefp) GroupRecord *gr = group_record_new();
6✔
770
                if (!gr)
3✔
UNCOV
771
                        return log_oom();
×
772

773
                r = group_record_load(gr, arg_from_file, USER_RECORD_LOAD_MASK_SECRET|USER_RECORD_LOG);
3✔
774
                if (r < 0)
3✔
775
                        return r;
776

777
                r = show_group(gr, table);
3✔
778
                if (r < 0)
3✔
779
                        return r;
780

781
        } else if (argc > 1 && !arg_fuzzy)
33✔
782
                STRV_FOREACH(i, argv + 1) {
56✔
783
                        _cleanup_(group_record_unrefp) GroupRecord *gr = NULL;
33✔
784

785
                        r = groupdb_by_name(*i, &match, arg_userdb_flags|USERDB_PARSE_NUMERIC, &gr);
33✔
786
                        if (r < 0) {
33✔
787
                                if (r == -ESRCH)
12✔
788
                                        log_error_errno(r, "Group %s does not exist.", *i);
10✔
789
                                else if (r == -EHOSTDOWN)
2✔
UNCOV
790
                                        log_error_errno(r, "Selected group database service is not available for this request.");
×
791
                                else if (r == -ENOEXEC)
2✔
UNCOV
792
                                        log_error_errno(r, "Group '%s' exists but does not match specified filter.", *i);
×
793
                                else
794
                                        log_error_errno(r, "Failed to find group %s: %m", *i);
2✔
795

796
                                RET_GATHER(ret, r);
12✔
797
                        } else {
798
                                if (draw_separator && arg_output == OUTPUT_FRIENDLY)
21✔
799
                                        putchar('\n');
4✔
800

801
                                r = show_group(gr, table);
21✔
802
                                if (r < 0)
21✔
UNCOV
803
                                        return r;
×
804

805
                                draw_separator = true;
806
                        }
807
                }
808
        else {
809
                if (argc > 1) {
1✔
810
                        match.fuzzy_names = strv_copy(strv_skip(argv, 1));
1✔
811
                        if (!match.fuzzy_names)
1✔
812
                                return log_oom();
×
813
                }
814

UNCOV
815
                _cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL;
×
816
                r = groupdb_all(&match, arg_userdb_flags, &iterator);
10✔
817
                if (r == -ENOLINK)
10✔
UNCOV
818
                        log_debug_errno(r, "No entries found. (Didn't check via Varlink.)");
×
819
                else if (r == -ESRCH)
10✔
820
                        log_debug_errno(r, "No entries found.");
10✔
821
                else if (r < 0)
10✔
UNCOV
822
                        return log_error_errno(r, "Failed to enumerate groups: %m");
×
823
                else {
824
                        for (;;) {
288✔
825
                                _cleanup_(group_record_unrefp) GroupRecord *gr = NULL;
10✔
826

827
                                r = groupdb_iterator_get(iterator, &match, &gr);
298✔
828
                                if (r == -ESRCH)
298✔
829
                                        break;
830
                                if (r == -EHOSTDOWN)
288✔
UNCOV
831
                                        return log_error_errno(r, "Selected group database service is not available for this request.");
×
832
                                if (r < 0)
288✔
UNCOV
833
                                        return log_error_errno(r, "Failed acquire next group: %m");
×
834

835
                                if (draw_separator && arg_output == OUTPUT_FRIENDLY)
288✔
UNCOV
836
                                        putchar('\n');
×
837

838
                                r = show_group(gr, table);
288✔
839
                                if (r < 0)
288✔
840
                                        return r;
841

842
                                draw_separator = true;
288✔
843
                        }
844
                }
845
        }
846

847
        if (table) {
36✔
848
                int boundary_lines = 0, gid_map_lines = 0;
10✔
849

850
                if (arg_boundaries) {
10✔
851
                        _cleanup_(uid_range_freep) UIDRange *gid_range = NULL;
9✔
852
                        r = uid_range_load_userns(/* path = */ NULL, GID_RANGE_USERNS_INSIDE, &gid_range);
9✔
853
                        if (r < 0)
9✔
UNCOV
854
                                log_debug_errno(r, "Failed to load /proc/self/gid_map, ignoring: %m");
×
855

856
                        boundary_lines = table_add_gid_boundaries(table, gid_range);
9✔
857
                        if (boundary_lines < 0)
9✔
858
                                return boundary_lines;
859

860
                        gid_map_lines = table_add_uid_map(table, gid_range, add_unavailable_gid);
9✔
861
                        if (gid_map_lines < 0)
9✔
862
                                return gid_map_lines;
863
                }
864

865
                if (!table_isempty(table)) {
20✔
866
                        r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
10✔
867
                        if (r < 0)
10✔
UNCOV
868
                                return table_log_print_error(r);
×
869
                }
870

871
                if (arg_legend) {
10✔
872
                        size_t k;
9✔
873

874
                        k = table_get_rows(table) - 1 - boundary_lines - gid_map_lines;
9✔
875
                        if (k > 0)
9✔
876
                                printf("\n%zu groups listed.\n", k);
7✔
877
                        else
878
                                printf("No groups.\n");
2✔
879
                }
880
        }
881

882
        return ret;
883
}
884

885
static int show_membership(const char *user, const char *group, Table *table) {
14✔
886
        int r;
14✔
887

888
        assert(user);
14✔
889
        assert(group);
14✔
890

891
        switch (arg_output) {
14✔
892

UNCOV
893
        case OUTPUT_CLASSIC:
×
894
                /* Strictly speaking there's no 'classic' output for this concept, but let's output it in
895
                 * similar style to the classic output for user/group info */
896

UNCOV
897
                printf("%s:%s\n", user, group);
×
UNCOV
898
                break;
×
899

900
        case OUTPUT_JSON: {
2✔
901
                _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
2✔
902

903
                r = sd_json_buildo(
2✔
904
                                &v,
905
                                SD_JSON_BUILD_PAIR("user", SD_JSON_BUILD_STRING(user)),
906
                                SD_JSON_BUILD_PAIR("group", SD_JSON_BUILD_STRING(group)));
907
                if (r < 0)
2✔
908
                        return log_error_errno(r, "Failed to build JSON object: %m");
×
909

910
                sd_json_variant_dump(v, arg_json_format_flags, NULL, NULL);
2✔
911
                break;
2✔
912
        }
913

UNCOV
914
        case OUTPUT_FRIENDLY:
×
915
                /* Hmm, this is not particularly friendly, but not sure how we could do this better */
UNCOV
916
                printf("%s: %s\n", group, user);
×
UNCOV
917
                break;
×
918

919
        case OUTPUT_TABLE:
12✔
920
                assert(table);
12✔
921

922
                r = table_add_many(
12✔
923
                                table,
924
                                TABLE_STRING, user,
925
                                TABLE_STRING, group);
926
                if (r < 0)
12✔
UNCOV
927
                        return table_log_add_error(r);
×
928

929
                break;
930

UNCOV
931
        default:
×
UNCOV
932
                assert_not_reached();
×
933
        }
934

935
        return 0;
936
}
937

938
static int display_memberships(int argc, char *argv[], void *userdata) {
14✔
939
        _cleanup_(table_unrefp) Table *table = NULL;
14✔
940
        int ret = 0, r;
14✔
941

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

945
        if (arg_output < 0)
14✔
946
                arg_output = OUTPUT_TABLE;
12✔
947

948
        if (arg_output == OUTPUT_TABLE) {
14✔
949
                table = table_new("user", "group");
12✔
950
                if (!table)
12✔
UNCOV
951
                        return log_oom();
×
952

953
                (void) table_set_sort(table, (size_t) 0, (size_t) 1);
12✔
954
        }
955

956
        if (argc > 1)
14✔
957
                STRV_FOREACH(i, argv + 1) {
26✔
958
                        _cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL;
4✔
959

960
                        if (streq(argv[0], "users-in-group")) {
18✔
961
                                r = membershipdb_by_group(*i, arg_userdb_flags, &iterator);
9✔
962
                                if (r < 0)
9✔
963
                                        return log_error_errno(r, "Failed to enumerate users in group: %m");
2✔
964
                        } else if (streq(argv[0], "groups-of-user")) {
9✔
965
                                r = membershipdb_by_user(*i, arg_userdb_flags, &iterator);
9✔
966
                                if (r < 0)
9✔
967
                                        return log_error_errno(r, "Failed to enumerate groups of user: %m");
2✔
968
                        } else
UNCOV
969
                                assert_not_reached();
×
970

971
                        for (;;) {
22✔
972
                                _cleanup_free_ char *user = NULL, *group = NULL;
18✔
973

974
                                r = membershipdb_iterator_get(iterator, &user, &group);
18✔
975
                                if (r == -ESRCH)
18✔
976
                                        break;
977
                                if (r == -EHOSTDOWN)
4✔
UNCOV
978
                                        return log_error_errno(r, "Selected membership database service is not available for this request.");
×
979
                                if (r < 0)
4✔
UNCOV
980
                                        return log_error_errno(r, "Failed acquire next membership: %m");
×
981

982
                                r = show_membership(user, group, table);
4✔
983
                                if (r < 0)
4✔
984
                                        return r;
985
                        }
986
                }
987
        else {
UNCOV
988
                _cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL;
×
989

990
                r = membershipdb_all(arg_userdb_flags, &iterator);
2✔
991
                if (r == -ENOLINK)
2✔
UNCOV
992
                        log_debug_errno(r, "No entries found. (Didn't check via Varlink.)");
×
993
                else if (r == -ESRCH)
2✔
994
                        log_debug_errno(r, "No entries found.");
2✔
995
                else if (r < 0)
2✔
UNCOV
996
                        return log_error_errno(r, "Failed to enumerate memberships: %m");
×
997
                else {
998
                        for (;;) {
22✔
999
                                _cleanup_free_ char *user = NULL, *group = NULL;
12✔
1000

1001
                                r = membershipdb_iterator_get(iterator, &user, &group);
12✔
1002
                                if (r == -ESRCH)
12✔
1003
                                        break;
1004
                                if (r == -EHOSTDOWN)
10✔
UNCOV
1005
                                        return log_error_errno(r, "Selected membership database service is not available for this request.");
×
1006
                                if (r < 0)
10✔
UNCOV
1007
                                        return log_error_errno(r, "Failed acquire next membership: %m");
×
1008

1009
                                r = show_membership(user, group, table);
10✔
1010
                                if (r < 0)
10✔
1011
                                        return r;
1012
                        }
1013
                }
1014
        }
1015

1016
        if (table) {
10✔
1017
                if (!table_isempty(table)) {
8✔
1018
                        r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
4✔
1019
                        if (r < 0)
4✔
UNCOV
1020
                                return table_log_print_error(r);
×
1021
                }
1022

1023
                if (arg_legend) {
8✔
1024
                        if (table_isempty(table))
16✔
1025
                                printf("No memberships.\n");
4✔
1026
                        else
1027
                                printf("\n%zu memberships listed.\n", table_get_rows(table) - 1);
4✔
1028
                }
1029
        }
1030

1031
        return ret;
1032
}
1033

1034
static int display_services(int argc, char *argv[], void *userdata) {
2✔
UNCOV
1035
        _cleanup_(table_unrefp) Table *t = NULL;
×
1036
        _cleanup_closedir_ DIR *d = NULL;
2✔
1037
        int r;
2✔
1038

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

1042
        d = opendir("/run/systemd/userdb/");
2✔
1043
        if (!d) {
2✔
UNCOV
1044
                if (errno == ENOENT) {
×
UNCOV
1045
                        log_info("No services.");
×
UNCOV
1046
                        return 0;
×
1047
                }
1048

UNCOV
1049
                return log_error_errno(errno, "Failed to open %s: %m", "/run/systemd/userdb/");
×
1050
        }
1051

1052
        t = table_new("service", "listening");
2✔
1053
        if (!t)
2✔
UNCOV
1054
                return log_oom();
×
1055

1056
        (void) table_set_sort(t, (size_t) 0);
2✔
1057

1058
        FOREACH_DIRENT(de, d, return -errno) {
18✔
1059
                _cleanup_free_ char *j = NULL, *no = NULL;
12✔
1060
                _cleanup_close_ int fd = -EBADF;
12✔
1061

1062
                j = path_join("/run/systemd/userdb/", de->d_name);
12✔
1063
                if (!j)
12✔
UNCOV
1064
                        return log_oom();
×
1065

1066
                fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
12✔
1067
                if (fd < 0)
12✔
1068
                        return log_error_errno(errno, "Failed to allocate AF_UNIX/SOCK_STREAM socket: %m");
×
1069

1070
                r = connect_unix_path(fd, dirfd(d), de->d_name);
12✔
1071
                if (r < 0) {
12✔
UNCOV
1072
                        no = strjoin("No (", errno_to_name(r), ")");
×
UNCOV
1073
                        if (!no)
×
UNCOV
1074
                                return log_oom();
×
1075
                }
1076

1077
                r = table_add_many(t,
24✔
1078
                                   TABLE_STRING, de->d_name,
1079
                                   TABLE_STRING, no ?: "yes",
1080
                                   TABLE_SET_COLOR, ansi_highlight_green_red(!no));
1081
                if (r < 0)
12✔
1082
                        return table_log_add_error(r);
×
1083
        }
1084

1085
        if (!table_isempty(t)) {
4✔
1086
                r = table_print_with_pager(t, arg_json_format_flags, arg_pager_flags, arg_legend);
2✔
1087
                if (r < 0)
2✔
UNCOV
1088
                        return table_log_print_error(r);
×
1089
        }
1090

1091
        if (arg_legend && arg_output != OUTPUT_JSON) {
2✔
1092
                if (table_isempty(t))
2✔
UNCOV
1093
                        printf("No services.\n");
×
1094
                else
1095
                        printf("\n%zu services listed.\n", table_get_rows(t) - 1);
1✔
1096
        }
1097

1098
        return 0;
1099
}
1100

1101
static int ssh_authorized_keys(int argc, char *argv[], void *userdata) {
10✔
UNCOV
1102
        _cleanup_(user_record_unrefp) UserRecord *ur = NULL;
×
1103
        char **chain_invocation;
10✔
1104
        int r;
10✔
1105

1106
        assert(argc >= 2);
10✔
1107

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

1111
        if (arg_chain) {
10✔
1112
                /* If --chain is specified, the rest of the command line is the chain command */
1113

1114
                if (argc < 3)
4✔
1115
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1✔
1116
                                               "No chain command line specified, refusing.");
1117

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

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

1127
                chain_invocation = argv + 2;
2✔
1128
        } else {
1129
                /* If --chain is not specified, then refuse any further arguments */
1130

1131
                if (argc > 2)
6✔
UNCOV
1132
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Too many arguments.");
×
1133

1134
                chain_invocation = NULL;
1135
        }
1136

1137
        r = userdb_by_name(argv[1], /* match= */ NULL, arg_userdb_flags, &ur);
8✔
1138
        if (r == -ESRCH)
8✔
1139
                log_error_errno(r, "User %s does not exist.", argv[1]);
2✔
1140
        else if (r == -EHOSTDOWN)
6✔
UNCOV
1141
                log_error_errno(r, "Selected user database service is not available for this request.");
×
1142
        else if (r == -EINVAL)
6✔
UNCOV
1143
                log_error_errno(r, "Failed to find user %s: %m (Invalid user name?)", argv[1]);
×
1144
        else if (r < 0)
6✔
UNCOV
1145
                log_error_errno(r, "Failed to find user %s: %m", argv[1]);
×
1146
        else {
1147
                if (strv_isempty(ur->ssh_authorized_keys))
6✔
1148
                        log_debug("User record for %s has no public SSH keys.", argv[1]);
×
1149
                else
1150
                        STRV_FOREACH(i, ur->ssh_authorized_keys)
14✔
1151
                                printf("%s\n", *i);
8✔
1152

1153
                if (ur->incomplete) {
6✔
UNCOV
1154
                        fflush(stdout);
×
UNCOV
1155
                        log_warning("Warning: lacking rights to acquire privileged fields of user record of '%s', output incomplete.", ur->user_name);
×
1156
                }
1157
        }
1158

1159
        if (chain_invocation) {
8✔
1160
                if (DEBUG_LOGGING) {
2✔
1161
                        _cleanup_free_ char *s = NULL;
1✔
1162

1163
                        s = quote_command_line(chain_invocation, SHELL_ESCAPE_EMPTY);
1✔
1164
                        if (!s)
1✔
1165
                                return log_oom();
×
1166

1167
                        log_debug("Chain invoking: %s", s);
1✔
1168
                }
1169

1170
                fflush(stdout);
2✔
1171
                execv(chain_invocation[0], chain_invocation);
×
UNCOV
1172
                if (errno == ENOENT) /* Let's handle ENOENT gracefully */
×
1173
                        log_warning_errno(errno, "Chain executable '%s' does not exist, ignoring chain invocation.", chain_invocation[0]);
8✔
1174
                else {
UNCOV
1175
                        log_error_errno(errno, "Failed to invoke chain executable '%s': %m", chain_invocation[0]);
×
UNCOV
1176
                        if (r >= 0)
×
UNCOV
1177
                                r = -errno;
×
1178
                }
1179
        }
1180

1181
        return r;
1182
}
1183

1184
static int load_credential_one(int credential_dir_fd, const char *name, int userdb_dir_fd) {
232✔
1185
        int r;
232✔
1186

1187
        assert(credential_dir_fd >= 0);
232✔
1188
        assert(name);
232✔
1189
        assert(userdb_dir_fd >= 0);
232✔
1190

1191
        const char *user = startswith(name, "userdb.user.");
232✔
1192
        const char *group = startswith(name, "userdb.group.");
232✔
1193
        if (!user && !group)
232✔
1194
                return 0;
232✔
1195

1196
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
232✔
1197
        unsigned line = 0, column = 0;
232✔
1198
        r = sd_json_parse_file_at(NULL, credential_dir_fd, name, SD_JSON_PARSE_SENSITIVE, &v, &line, &column);
232✔
1199
        if (r < 0)
232✔
UNCOV
1200
                return log_error_errno(r, "Failed to parse credential '%s' as JSON at %u:%u: %m", name, line, column);
×
1201

1202
        _cleanup_(user_record_unrefp) UserRecord *ur = NULL, *ur_stripped = NULL, *ur_privileged = NULL;
232✔
1203
        _cleanup_(group_record_unrefp) GroupRecord *gr = NULL, *gr_stripped = NULL, *gr_privileged = NULL;
232✔
1204
        _cleanup_free_ char *fn = NULL, *link = NULL;
232✔
1205

1206
        if (user) {
232✔
1207
                ur = user_record_new();
116✔
1208
                if (!ur)
116✔
1209
                        return log_oom();
15✔
1210

1211
                r = user_record_load(ur, v, USER_RECORD_LOAD_MASK_SECRET|USER_RECORD_LOG);
116✔
1212
                if (r < 0)
116✔
1213
                        return r;
1214

1215
                if (user_record_is_root(ur))
116✔
UNCOV
1216
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Creating 'root' user from credentials is not supported.");
×
1217
                if (user_record_is_nobody(ur))
116✔
UNCOV
1218
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Creating 'nobody' user from credentials is not supported.");
×
1219

1220
                if (!streq_ptr(user, ur->user_name))
116✔
UNCOV
1221
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1222
                                               "Credential suffix '%s' does not match user record name '%s'",
1223
                                               user, strna(ur->user_name));
1224

1225
                if (!uid_is_valid(ur->uid))
116✔
UNCOV
1226
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "JSON user record missing uid field");
×
1227

1228
                if (!gid_is_valid(user_record_gid(ur)))
116✔
1229
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "JSON user record missing gid field");
×
1230

1231
                _cleanup_(user_record_unrefp) UserRecord *m = NULL;
116✔
1232
                r = userdb_by_name(ur->user_name, /* match= */ NULL, USERDB_SUPPRESS_SHADOW, &m);
116✔
1233
                if (r >= 0) {
116✔
1234
                        if (m->uid != ur->uid)
15✔
UNCOV
1235
                                return log_error_errno(SYNTHETIC_ERRNO(EEXIST),
×
1236
                                                       "Cannot create user %s from credential %s as it already exists with UID " UID_FMT " instead of " UID_FMT,
1237
                                                       ur->user_name, name, m->uid, ur->uid);
1238

1239
                        log_info("User with name %s and UID " UID_FMT " already exists, not creating user from credential %s", ur->user_name, ur->uid, name);
15✔
1240
                        return 0;
15✔
1241
                }
1242
                if (r != -ESRCH)
101✔
1243
                        return log_error_errno(r, "Failed to check if user with name %s already exists: %m", ur->user_name);
×
1244

1245
                m = user_record_unref(m);
101✔
1246
                r = userdb_by_uid(ur->uid, /* match= */ NULL, USERDB_SUPPRESS_SHADOW, &m);
101✔
1247
                if (r >= 0) {
101✔
1248
                        if (!streq_ptr(ur->user_name, m->user_name))
×
UNCOV
1249
                                return log_error_errno(SYNTHETIC_ERRNO(EEXIST),
×
1250
                                                       "Cannot create user %s from credential %s as UID " UID_FMT " is already assigned to user %s",
1251
                                                       ur->user_name, name, ur->uid, m->user_name);
1252

UNCOV
1253
                        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
1254
                        return 0;
×
1255
                }
1256
                if (r != -ESRCH)
101✔
UNCOV
1257
                        return log_error_errno(r, "Failed to check if user with UID " UID_FMT " already exists: %m", ur->uid);
×
1258

1259
                r = user_record_clone(ur, USER_RECORD_LOAD_MASK_PRIVILEGED|USER_RECORD_LOG, &ur_stripped);
101✔
1260
                if (r < 0)
101✔
1261
                        return r;
1262

1263
                r = user_record_clone(ur, USER_RECORD_EXTRACT_PRIVILEGED|USER_RECORD_EMPTY_OK|USER_RECORD_LOG, &ur_privileged);
101✔
1264
                if (r < 0)
101✔
1265
                        return r;
1266

1267
                fn = strjoin(ur->user_name, ".user");
101✔
1268
                if (!fn)
101✔
UNCOV
1269
                        return log_oom();
×
1270

1271
                if (asprintf(&link, UID_FMT ".user", ur->uid) < 0)
101✔
UNCOV
1272
                        return log_oom();
×
1273
        } else {
1274
                assert(group);
116✔
1275

1276
                gr = group_record_new();
116✔
1277
                if (!gr)
116✔
1278
                        return log_oom();
15✔
1279

1280
                r = group_record_load(gr, v, USER_RECORD_LOAD_MASK_SECRET|USER_RECORD_LOG);
116✔
1281
                if (r < 0)
116✔
1282
                        return r;
1283

1284
                if (group_record_is_root(gr))
116✔
UNCOV
1285
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Creating 'root' group from credentials is not supported.");
×
1286
                if (group_record_is_nobody(gr))
116✔
UNCOV
1287
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Creating 'nobody' group from credentials is not supported.");
×
1288

1289
                if (!streq_ptr(group, gr->group_name))
116✔
UNCOV
1290
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1291
                                               "Credential suffix '%s' does not match group record name '%s'",
1292
                                               group, strna(gr->group_name));
1293

1294
                if (!gid_is_valid(gr->gid))
116✔
1295
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "JSON group record missing gid field");
×
1296

1297
                _cleanup_(group_record_unrefp) GroupRecord *m = NULL;
116✔
1298
                r = groupdb_by_name(gr->group_name, /* match= */ NULL, USERDB_SUPPRESS_SHADOW, &m);
116✔
1299
                if (r >= 0) {
116✔
1300
                        if (m->gid != gr->gid)
15✔
UNCOV
1301
                                return log_error_errno(SYNTHETIC_ERRNO(EEXIST),
×
1302
                                                       "Cannot create group %s from credential %s as it already exists with GID " GID_FMT " instead of " GID_FMT,
1303
                                                       gr->group_name, name, m->gid, gr->gid);
1304

1305
                        log_info("Group with name %s and GID " GID_FMT " already exists, not creating group from credential %s", gr->group_name, gr->gid, name);
15✔
1306
                        return 0;
15✔
1307
                }
1308
                if (r != -ESRCH)
101✔
1309
                        return log_error_errno(r, "Failed to check if group with name %s already exists: %m", gr->group_name);
×
1310

1311
                m = group_record_unref(m);
101✔
1312
                r = groupdb_by_gid(gr->gid, /* match= */ NULL, USERDB_SUPPRESS_SHADOW, &m);
101✔
1313
                if (r >= 0) {
101✔
1314
                        if (!streq_ptr(gr->group_name, m->group_name))
×
UNCOV
1315
                                return log_error_errno(SYNTHETIC_ERRNO(EEXIST),
×
1316
                                                       "Cannot create group %s from credential %s as GID " GID_FMT " is already assigned to group %s",
1317
                                                       gr->group_name, name, gr->gid, m->group_name);
1318

UNCOV
1319
                        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
1320
                        return 0;
×
1321
                }
1322
                if (r != -ESRCH)
101✔
UNCOV
1323
                        return log_error_errno(r, "Failed to check if group with GID " GID_FMT " already exists: %m", gr->gid);
×
1324

1325
                r = group_record_clone(gr, USER_RECORD_LOAD_MASK_PRIVILEGED|USER_RECORD_LOG, &gr_stripped);
101✔
1326
                if (r < 0)
101✔
1327
                        return r;
1328

1329
                r = group_record_clone(gr, USER_RECORD_EXTRACT_PRIVILEGED|USER_RECORD_EMPTY_OK|USER_RECORD_LOG, &gr_privileged);
101✔
1330
                if (r < 0)
101✔
1331
                        return r;
1332

1333
                fn = strjoin(gr->group_name, ".group");
101✔
1334
                if (!fn)
101✔
UNCOV
1335
                        return log_oom();
×
1336

1337
                if (asprintf(&link, GID_FMT ".group", gr->gid) < 0)
101✔
UNCOV
1338
                        return log_oom();
×
1339
        }
1340

1341
        if (!filename_is_valid(fn))
202✔
UNCOV
1342
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1343
                                       "Passed credential '%s' would result in invalid filename '%s'.",
1344
                                       name, fn);
1345

1346
        _cleanup_free_ char *formatted = NULL;
202✔
1347
        r = sd_json_variant_format(ur ? ur_stripped->json : gr_stripped->json, SD_JSON_FORMAT_NEWLINE, &formatted);
202✔
1348
        if (r < 0)
202✔
UNCOV
1349
                return log_error_errno(r, "Failed to format JSON record: %m");
×
1350

1351
        r = write_string_file_at(userdb_dir_fd, fn, formatted, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC);
202✔
1352
        if (r < 0)
202✔
UNCOV
1353
                return log_error_errno(r, "Failed to write JSON record to /etc/userdb/%s: %m", fn);
×
1354

1355
        if (symlinkat(fn, userdb_dir_fd, link) < 0)
202✔
UNCOV
1356
                return log_error_errno(errno, "Failed to create symlink from %s to %s", link, fn);
×
1357

1358
        log_info("Installed /etc/userdb/%s from credential.", fn);
202✔
1359

1360
        if ((ur && !sd_json_variant_is_blank_object(ur_privileged->json)) ||
202✔
1361
            (gr && !sd_json_variant_is_blank_object(gr_privileged->json))) {
101✔
1362
                fn = mfree(fn);
101✔
1363
                fn = strjoin(ur ? ur->user_name : gr->group_name, ur ? ".user-privileged" : ".group-privileged");
101✔
1364
                if (!fn)
101✔
UNCOV
1365
                        return log_oom();
×
1366

1367
                formatted = mfree(formatted);
101✔
1368
                r = sd_json_variant_format(ur ? ur_privileged->json : gr_privileged->json, SD_JSON_FORMAT_NEWLINE, &formatted);
101✔
1369
                if (r < 0)
101✔
UNCOV
1370
                        return log_error_errno(r, "Failed to format JSON record: %m");
×
1371

1372
                r = write_string_file_at(userdb_dir_fd, fn, formatted, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_MODE_0600);
101✔
1373
                if (r < 0)
101✔
1374
                        return log_error_errno(r, "Failed to write JSON record to /etc/userdb/%s: %m", fn);
×
1375

1376
                link = mfree(link);
101✔
1377

1378
                if (ur) {
101✔
1379
                        if (asprintf(&link, UID_FMT ".user-privileged", ur->uid) < 0)
101✔
UNCOV
1380
                                return log_oom();
×
1381
                } else {
UNCOV
1382
                        if (asprintf(&link, GID_FMT ".group-privileged", gr->gid) < 0)
×
UNCOV
1383
                                return log_oom();
×
1384
                }
1385

1386
                if (symlinkat(fn, userdb_dir_fd, link) < 0)
101✔
UNCOV
1387
                        return log_error_errno(errno, "Failed to create symlink from %s to %s", link, fn);
×
1388

1389
                log_info("Installed /etc/userdb/%s from credential.", fn);
101✔
1390
        }
1391

1392
        if (ur)
202✔
1393
                STRV_FOREACH(g, ur->member_of) {
303✔
1394
                        _cleanup_free_ char *membership = strjoin(ur->user_name, ":", *g);
404✔
1395
                        if (!membership)
202✔
UNCOV
1396
                                return log_oom();
×
1397

1398
                        _cleanup_close_ int fd = openat(userdb_dir_fd, membership, O_WRONLY|O_CREAT|O_CLOEXEC, 0644);
404✔
1399
                        if (fd < 0)
202✔
1400
                                return log_error_errno(errno, "Failed to create %s: %m", membership);
×
1401

1402
                        log_info("Installed /etc/userdb/%s from credential.", membership);
202✔
1403
                }
1404
        else
1405
                STRV_FOREACH(u, gr->members) {
101✔
1406
                        _cleanup_free_ char *membership = strjoin(*u, ":", gr->group_name);
×
UNCOV
1407
                        if (!membership)
×
1408
                                return log_oom();
×
1409

UNCOV
1410
                        _cleanup_close_ int fd = openat(userdb_dir_fd, membership, O_WRONLY|O_CREAT|O_CLOEXEC, 0644);
×
UNCOV
1411
                        if (fd < 0)
×
UNCOV
1412
                                return log_error_errno(errno, "Failed to create %s: %m", membership);
×
1413

UNCOV
1414
                        log_info("Installed /etc/userdb/%s from credential.", membership);
×
1415
                }
1416

1417
        if (ur && user_record_disposition(ur) == USER_REGULAR) {
202✔
1418
                const char *hd = user_record_home_directory(ur);
101✔
1419

1420
                r = RET_NERRNO(access(hd, F_OK));
303✔
1421
                if (r < 0) {
101✔
1422
                        if (r != -ENOENT)
101✔
UNCOV
1423
                                return log_error_errno(r, "Failed to check if %s exists: %m", hd);
×
1424

1425
                        WITH_UMASK(0000) {
202✔
1426
                                r = mkdir_parents(hd, 0755);
101✔
1427
                                if (r < 0)
101✔
UNCOV
1428
                                        return log_error_errno(r, "Failed to create parent directories of %s: %m", hd);
×
1429

1430
                                if (mkdir(hd, 0700) < 0 && errno != EEXIST)
101✔
UNCOV
1431
                                        return log_error_errno(errno, "Failed to create %s: %m", hd);
×
1432
                        }
1433

1434
                        if (chown(hd, ur->uid, user_record_gid(ur)) < 0)
101✔
UNCOV
1435
                                return log_error_errno(errno, "Failed to chown %s: %m", hd);
×
1436

1437
                        r = copy_tree(user_record_skeleton_directory(ur), hd, ur->uid, user_record_gid(ur),
101✔
1438
                                      COPY_REFLINK|COPY_MERGE, /* denylist= */ NULL, /* subvolumes= */NULL);
1439
                        if (r < 0 && r != -ENOENT)
101✔
UNCOV
1440
                                return log_error_errno(r, "Failed to copy skeleton directory to %s: %m", hd);
×
1441
                }
1442
        }
1443

1444
        return 0;
1445
}
1446

1447
static int load_credentials(int argc, char *argv[], void *userdata) {
116✔
1448
        int r;
116✔
1449

1450
        _cleanup_close_ int credential_dir_fd = open_credentials_dir();
232✔
1451
        if (IN_SET(credential_dir_fd, -ENXIO, -ENOENT)) {
116✔
1452
                /* Credential env var not set, or dir doesn't exist. */
UNCOV
1453
                log_debug("No credentials found.");
×
UNCOV
1454
                return 0;
×
1455
        }
1456
        if (credential_dir_fd < 0)
116✔
UNCOV
1457
                return log_error_errno(credential_dir_fd, "Failed to open credentials directory: %m");
×
1458

1459
        _cleanup_free_ DirectoryEntries *des = NULL;
116✔
1460
        r = readdir_all(credential_dir_fd, RECURSE_DIR_SORT|RECURSE_DIR_IGNORE_DOT|RECURSE_DIR_ENSURE_TYPE, &des);
116✔
1461
        if (r < 0)
116✔
UNCOV
1462
                return log_error_errno(r, "Failed to enumerate credentials: %m");
×
1463

1464
        _cleanup_close_ int userdb_dir_fd = xopenat_full(
232✔
1465
                AT_FDCWD, "/etc/userdb",
1466
                /* open_flags= */ O_DIRECTORY|O_CREAT|O_CLOEXEC,
1467
                /* xopen_flags= */ XO_LABEL,
1468
                /* mode= */ 0755);
1469
        if (userdb_dir_fd < 0)
116✔
1470
                return log_error_errno(userdb_dir_fd, "Failed to open %s: %m", "/etc/userdb/");
×
1471

1472
        FOREACH_ARRAY(i, des->entries, des->n_entries) {
348✔
1473
                struct dirent *de = *i;
232✔
1474

1475
                if (de->d_type != DT_REG)
232✔
UNCOV
1476
                        continue;
×
1477

1478
                RET_GATHER(r, load_credential_one(credential_dir_fd, de->d_name, userdb_dir_fd));
232✔
1479
        }
1480

1481
        return r;
1482
}
1483

1484
static int help(int argc, char *argv[], void *userdata) {
1✔
1485
        _cleanup_free_ char *link = NULL;
1✔
1486
        int r;
1✔
1487

1488
        pager_open(arg_pager_flags);
1✔
1489

1490
        r = terminal_urlify_man("userdbctl", "1", &link);
1✔
1491
        if (r < 0)
1✔
UNCOV
1492
                return log_oom();
×
1493

1494
        printf("%s [OPTIONS...] COMMAND ...\n\n"
2✔
1495
               "%sShow user and group information.%s\n"
1496
               "\nCommands:\n"
1497
               "  user [USER…]               Inspect user\n"
1498
               "  group [GROUP…]             Inspect group\n"
1499
               "  users-in-group [GROUP…]    Show users that are members of specified groups\n"
1500
               "  groups-of-user [USER…]     Show groups the specified users are members of\n"
1501
               "  services                   Show enabled database services\n"
1502
               "  ssh-authorized-keys USER   Show SSH authorized keys for user\n"
1503
               "  load-credentials           Write static user/group records from credentials\n"
1504
               "\nOptions:\n"
1505
               "  -h --help                  Show this help\n"
1506
               "     --version               Show package version\n"
1507
               "     --no-pager              Do not pipe output into a pager\n"
1508
               "     --no-legend             Do not show the headers and footers\n"
1509
               "     --output=MODE           Select output mode (classic, friendly, table, json)\n"
1510
               "  -j                         Equivalent to --output=json\n"
1511
               "  -s --service=SERVICE[:SERVICE…]\n"
1512
               "                             Query the specified service\n"
1513
               "     --with-nss=BOOL         Control whether to include glibc NSS data\n"
1514
               "  -N                         Do not synthesize or include glibc NSS data\n"
1515
               "                             (Same as --synthesize=no --with-nss=no)\n"
1516
               "     --synthesize=BOOL       Synthesize root/nobody user\n"
1517
               "     --with-dropin=BOOL      Control whether to include drop-in records\n"
1518
               "     --with-varlink=BOOL     Control whether to talk to services at all\n"
1519
               "     --multiplexer=BOOL      Control whether to use the multiplexer\n"
1520
               "     --json=pretty|short     JSON output mode\n"
1521
               "     --chain                 Chain another command\n"
1522
               "     --uid-min=ID            Filter by minimum UID/GID (default 0)\n"
1523
               "     --uid-max=ID            Filter by maximum UID/GID (default 4294967294)\n"
1524
               "  -z --fuzzy                 Do a fuzzy name search\n"
1525
               "     --disposition=VALUE     Filter by disposition\n"
1526
               "  -I                         Equivalent to --disposition=intrinsic\n"
1527
               "  -S                         Equivalent to --disposition=system\n"
1528
               "  -R                         Equivalent to --disposition=regular\n"
1529
               "     --boundaries=BOOL       Show/hide UID/GID range boundaries in output\n"
1530
               "  -B                         Equivalent to --boundaries=no\n"
1531
               "  -F --from-file=PATH        Read JSON record from file\n"
1532
               "\nSee the %s for details.\n",
1533
               program_invocation_short_name,
1534
               ansi_highlight(),
1535
               ansi_normal(),
1536
               link);
1537

1538
        return 0;
1539
}
1540

1541
static int parse_argv(int argc, char *argv[]) {
315✔
1542

1543
        enum {
315✔
1544
                ARG_VERSION = 0x100,
1545
                ARG_NO_PAGER,
1546
                ARG_NO_LEGEND,
1547
                ARG_OUTPUT,
1548
                ARG_WITH_NSS,
1549
                ARG_WITH_DROPIN,
1550
                ARG_WITH_VARLINK,
1551
                ARG_SYNTHESIZE,
1552
                ARG_MULTIPLEXER,
1553
                ARG_JSON,
1554
                ARG_CHAIN,
1555
                ARG_UID_MIN,
1556
                ARG_UID_MAX,
1557
                ARG_DISPOSITION,
1558
                ARG_BOUNDARIES,
1559
        };
1560

1561
        static const struct option options[] = {
315✔
1562
                { "help",         no_argument,       NULL, 'h'              },
1563
                { "version",      no_argument,       NULL, ARG_VERSION      },
1564
                { "no-pager",     no_argument,       NULL, ARG_NO_PAGER     },
1565
                { "no-legend",    no_argument,       NULL, ARG_NO_LEGEND    },
1566
                { "output",       required_argument, NULL, ARG_OUTPUT       },
1567
                { "service",      required_argument, NULL, 's'              },
1568
                { "with-nss",     required_argument, NULL, ARG_WITH_NSS     },
1569
                { "with-dropin",  required_argument, NULL, ARG_WITH_DROPIN  },
1570
                { "with-varlink", required_argument, NULL, ARG_WITH_VARLINK },
1571
                { "synthesize",   required_argument, NULL, ARG_SYNTHESIZE   },
1572
                { "multiplexer",  required_argument, NULL, ARG_MULTIPLEXER  },
1573
                { "json",         required_argument, NULL, ARG_JSON         },
1574
                { "chain",        no_argument,       NULL, ARG_CHAIN        },
1575
                { "uid-min",      required_argument, NULL, ARG_UID_MIN      },
1576
                { "uid-max",      required_argument, NULL, ARG_UID_MAX      },
1577
                { "fuzzy",        no_argument,       NULL, 'z'              },
1578
                { "disposition",  required_argument, NULL, ARG_DISPOSITION  },
1579
                { "boundaries",   required_argument, NULL, ARG_BOUNDARIES   },
1580
                { "from-file",    required_argument, NULL, 'F'              },
1581
                {}
1582
        };
1583

1584
        const char *e;
315✔
1585
        int r;
315✔
1586

1587
        assert(argc >= 0);
315✔
1588
        assert(argv);
315✔
1589

1590
        /* We are going to update this environment variable with our own, hence let's first read what is already set */
1591
        e = getenv("SYSTEMD_ONLY_USERDB");
315✔
1592
        if (e) {
315✔
1593
                char **l;
×
1594

UNCOV
1595
                l = strv_split(e, ":");
×
UNCOV
1596
                if (!l)
×
UNCOV
1597
                        return log_oom();
×
1598

UNCOV
1599
                strv_free(arg_services);
×
UNCOV
1600
                arg_services = l;
×
1601
        }
1602

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

1607
        for (;;) {
412✔
1608
                int c;
412✔
1609

1610
                c = getopt_long(argc, argv,
412✔
1611
                                arg_chain ? "+hjs:NISRzBF:" : "hjs:NISRzBF:", /* When --chain was used disable parsing of further switches */
412✔
1612
                                options, NULL);
1613
                if (c < 0)
412✔
1614
                        break;
1615

1616
                switch (c) {
127✔
1617

1618
                case 'h':
1✔
1619
                        return help(0, NULL, NULL);
1✔
1620

1621
                case ARG_VERSION:
1✔
1622
                        return version();
1✔
1623

1624
                case ARG_NO_PAGER:
2✔
1625
                        arg_pager_flags |= PAGER_DISABLE;
2✔
1626
                        break;
2✔
1627

1628
                case ARG_NO_LEGEND:
3✔
1629
                        arg_legend = false;
3✔
1630
                        break;
3✔
1631

1632
                case ARG_OUTPUT:
8✔
1633
                        if (isempty(optarg))
8✔
UNCOV
1634
                                arg_output = _OUTPUT_INVALID;
×
1635
                        else if (streq(optarg, "classic"))
8✔
1636
                                arg_output = OUTPUT_CLASSIC;
1✔
1637
                        else if (streq(optarg, "friendly"))
7✔
1638
                                arg_output = OUTPUT_FRIENDLY;
1✔
1639
                        else if (streq(optarg, "json"))
6✔
1640
                                arg_output = OUTPUT_JSON;
1✔
1641
                        else if (streq(optarg, "table"))
5✔
1642
                                arg_output = OUTPUT_TABLE;
1✔
1643
                        else if (streq(optarg, "help")) {
4✔
UNCOV
1644
                                puts("classic\n"
×
1645
                                     "friendly\n"
1646
                                     "json\n"
1647
                                     "table");
UNCOV
1648
                                return 0;
×
1649
                        } else
1650
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid --output= mode: %s", optarg);
4✔
1651

1652
                        arg_json_format_flags = arg_output == OUTPUT_JSON ? SD_JSON_FORMAT_PRETTY|SD_JSON_FORMAT_COLOR_AUTO : SD_JSON_FORMAT_OFF;
4✔
1653
                        break;
4✔
1654

1655
                case ARG_JSON:
6✔
1656
                        r = parse_json_argument(optarg, &arg_json_format_flags);
6✔
1657
                        if (r <= 0)
6✔
1658
                                return r;
1659

1660
                        arg_output = sd_json_format_enabled(arg_json_format_flags) ? OUTPUT_JSON : _OUTPUT_INVALID;
2✔
1661
                        break;
2✔
1662

1663
                case 'j':
26✔
1664
                        arg_json_format_flags = SD_JSON_FORMAT_PRETTY|SD_JSON_FORMAT_COLOR_AUTO;
26✔
1665
                        arg_output = OUTPUT_JSON;
26✔
1666
                        break;
26✔
1667

1668
                case 's':
×
UNCOV
1669
                        if (isempty(optarg))
×
UNCOV
1670
                                arg_services = strv_free(arg_services);
×
1671
                        else {
UNCOV
1672
                                r = strv_split_and_extend(&arg_services, optarg, ":", /* filter_duplicates = */ true);
×
UNCOV
1673
                                if (r < 0)
×
UNCOV
1674
                                        return log_error_errno(r, "Failed to parse -s/--service= argument: %m");
×
1675
                        }
1676

1677
                        break;
1678

1679
                case 'N':
1✔
1680
                        arg_userdb_flags |= USERDB_EXCLUDE_NSS|USERDB_DONT_SYNTHESIZE_INTRINSIC|USERDB_DONT_SYNTHESIZE_FOREIGN;
1✔
1681
                        break;
1✔
1682

1683
                case ARG_WITH_NSS:
14✔
1684
                        r = parse_boolean_argument("--with-nss=", optarg, NULL);
14✔
1685
                        if (r < 0)
14✔
1686
                                return r;
1687

1688
                        SET_FLAG(arg_userdb_flags, USERDB_EXCLUDE_NSS, !r);
10✔
1689
                        break;
10✔
1690

1691
                case ARG_WITH_DROPIN:
8✔
1692
                        r = parse_boolean_argument("--with-dropin=", optarg, NULL);
8✔
1693
                        if (r < 0)
8✔
1694
                                return r;
1695

1696
                        SET_FLAG(arg_userdb_flags, USERDB_EXCLUDE_DROPIN, !r);
4✔
1697
                        break;
4✔
1698

1699
                case ARG_WITH_VARLINK:
7✔
1700
                        r = parse_boolean_argument("--with-varlink=", optarg, NULL);
7✔
1701
                        if (r < 0)
7✔
1702
                                return r;
1703

1704
                        SET_FLAG(arg_userdb_flags, USERDB_EXCLUDE_VARLINK, !r);
3✔
1705
                        break;
3✔
1706

1707
                case ARG_SYNTHESIZE:
12✔
1708
                        r = parse_boolean_argument("--synthesize=", optarg, NULL);
12✔
1709
                        if (r < 0)
12✔
1710
                                return r;
1711

1712
                        SET_FLAG(arg_userdb_flags, USERDB_DONT_SYNTHESIZE_INTRINSIC|USERDB_DONT_SYNTHESIZE_FOREIGN, !r);
8✔
1713
                        break;
8✔
1714

1715
                case ARG_MULTIPLEXER:
6✔
1716
                        r = parse_boolean_argument("--multiplexer=", optarg, NULL);
6✔
1717
                        if (r < 0)
6✔
1718
                                return r;
1719

1720
                        SET_FLAG(arg_userdb_flags, USERDB_AVOID_MULTIPLEXER, !r);
2✔
1721
                        break;
2✔
1722

1723
                case ARG_CHAIN:
4✔
1724
                        arg_chain = true;
4✔
1725
                        break;
4✔
1726

1727
                case ARG_DISPOSITION: {
4✔
1728
                        UserDisposition d = user_disposition_from_string(optarg);
4✔
1729
                        if (d < 0)
4✔
UNCOV
1730
                                return log_error_errno(d, "Unknown user disposition: %s", optarg);
×
1731

1732
                        if (arg_disposition_mask == UINT64_MAX)
4✔
1733
                                arg_disposition_mask = 0;
2✔
1734

1735
                        arg_disposition_mask |= UINT64_C(1) << d;
4✔
1736
                        break;
4✔
1737
                }
1738

1739
                case 'I':
2✔
1740
                        if (arg_disposition_mask == UINT64_MAX)
2✔
1741
                                arg_disposition_mask = 0;
2✔
1742

1743
                        arg_disposition_mask |= UINT64_C(1) << USER_INTRINSIC;
2✔
1744
                        break;
2✔
1745

1746
                case 'S':
6✔
1747
                        if (arg_disposition_mask == UINT64_MAX)
6✔
1748
                                arg_disposition_mask = 0;
4✔
1749

1750
                        arg_disposition_mask |= UINT64_C(1) << USER_SYSTEM;
6✔
1751
                        break;
6✔
1752

1753
                case 'R':
2✔
1754
                        if (arg_disposition_mask == UINT64_MAX)
2✔
1755
                                arg_disposition_mask = 0;
2✔
1756

1757
                        arg_disposition_mask |= UINT64_C(1) << USER_REGULAR;
2✔
1758
                        break;
2✔
1759

1760
                case ARG_UID_MIN:
2✔
1761
                        r = parse_uid(optarg, &arg_uid_min);
2✔
1762
                        if (r < 0)
2✔
1763
                                return log_error_errno(r, "Failed to parse --uid-min= value: %s", optarg);
×
1764
                        break;
1765

1766
                case ARG_UID_MAX:
2✔
1767
                        r = parse_uid(optarg, &arg_uid_max);
2✔
1768
                        if (r < 0)
2✔
UNCOV
1769
                                return log_error_errno(r, "Failed to parse --uid-max= value: %s", optarg);
×
1770
                        break;
1771

1772
                case 'z':
2✔
1773
                        arg_fuzzy = true;
2✔
1774
                        break;
2✔
1775

UNCOV
1776
                case ARG_BOUNDARIES:
×
UNCOV
1777
                        r = parse_boolean_argument("boundaries", optarg, &arg_boundaries);
×
UNCOV
1778
                        if (r < 0)
×
1779
                                return r;
1780
                        break;
1781

1782
                case 'B':
2✔
1783
                        arg_boundaries = false;
2✔
1784
                        break;
2✔
1785

1786
                case 'F': {
6✔
1787
                        if (isempty(optarg)) {
6✔
UNCOV
1788
                                arg_from_file = sd_json_variant_unref(arg_from_file);
×
1789
                                break;
6✔
1790
                        }
1791

1792
                        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
6✔
1793
                        const char *fn = streq(optarg, "-") ? NULL : optarg;
6✔
1794
                        unsigned line = 0;
6✔
1795
                        r = sd_json_parse_file(fn ? NULL : stdin, fn ?: "<stdin>", SD_JSON_PARSE_SENSITIVE, &v, &line, /* reterr_column= */ NULL);
6✔
1796
                        if (r < 0)
6✔
UNCOV
1797
                                return log_syntax(/* unit= */ NULL, LOG_ERR, fn ?: "<stdin>", line, r, "JSON parse failure.");
×
1798

1799
                        sd_json_variant_unref(arg_from_file);
6✔
1800
                        arg_from_file = TAKE_PTR(v);
6✔
1801
                        break;
6✔
1802
                }
1803

1804
                case '?':
1805
                        return -EINVAL;
1806

1807
                default:
×
UNCOV
1808
                        assert_not_reached();
×
1809
                }
1810
        }
1811

1812
        if (arg_uid_min > arg_uid_max)
285✔
UNCOV
1813
                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);
×
1814

1815
        /* If not mask was specified, use the all bits on mask */
1816
        if (arg_disposition_mask == UINT64_MAX)
285✔
1817
                arg_disposition_mask = USER_DISPOSITION_MASK_ALL;
275✔
1818

1819
        if (arg_from_file)
285✔
1820
                arg_boundaries = false;
6✔
1821

1822
        return 1;
1823
}
1824

1825
static int run(int argc, char *argv[]) {
315✔
1826
        static const Verb verbs[] = {
315✔
1827
                { "help",                VERB_ANY, VERB_ANY, 0,            help                },
1828
                { "user",                VERB_ANY, VERB_ANY, VERB_DEFAULT, display_user        },
1829
                { "group",               VERB_ANY, VERB_ANY, 0,            display_group       },
1830
                { "users-in-group",      VERB_ANY, VERB_ANY, 0,            display_memberships },
1831
                { "groups-of-user",      VERB_ANY, VERB_ANY, 0,            display_memberships },
1832
                { "services",            VERB_ANY, 1,        0,            display_services    },
1833
                { "ssh-authorized-keys", 2,        VERB_ANY, 0,            ssh_authorized_keys },
1834
                { "load-credentials",    VERB_ANY, 1,        0,            load_credentials    },
1835
                {}
1836
        };
1837

1838
        int r;
315✔
1839

1840
        log_setup();
315✔
1841

1842
        r = parse_argv(argc, argv);
315✔
1843
        if (r <= 0)
315✔
1844
                return r;
1845

1846
        if (arg_services) {
285✔
1847
                _cleanup_free_ char *e = NULL;
×
1848

UNCOV
1849
                e = strv_join(arg_services, ":");
×
1850
                if (!e)
×
UNCOV
1851
                        return log_oom();
×
1852

UNCOV
1853
                if (setenv("SYSTEMD_ONLY_USERDB", e, true) < 0)
×
UNCOV
1854
                        return log_error_errno(r, "Failed to set $SYSTEMD_ONLY_USERDB: %m");
×
1855

UNCOV
1856
                log_info("Enabled services: %s", e);
×
1857
        } else
1858
                assert_se(unsetenv("SYSTEMD_ONLY_USERDB") == 0);
285✔
1859

1860
        return dispatch_verb(argc, argv, verbs, NULL);
285✔
1861
}
1862

1863
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