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

systemd / systemd / 14766779411

30 Apr 2025 04:55PM UTC coverage: 72.225% (-0.06%) from 72.282%
14766779411

push

github

web-flow
wait-online: handle varlink connection errors while waiting for DNS (#37283)

Currently, if systemd-networkd-wait-online is started with --dns, and
systemd-resolved is not running, it will exit with an error right away.
Similarly, if systemd-resolved is restarted while waiting for DNS
configuration, systemd-networkd-wait-online will not attempt to
re-connect, and will potentially never see subsequent DNS
configurations.

Improve this by adding socket units for the systemd-resolved varlink
servers, and re-establish the connection in systemd-networkd-wait-online
when we receive `SD_VARLINK_ERROR_DISCONNECTED`.

8 of 16 new or added lines in 2 files covered. (50.0%)

5825 existing lines in 217 files now uncovered.

297168 of 411450 relevant lines covered (72.22%)

695892.62 hits per line

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

72.26
/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 "copy.h"
8
#include "creds-util.h"
9
#include "dirent-util.h"
10
#include "errno-list.h"
11
#include "escape.h"
12
#include "fd-util.h"
13
#include "fileio.h"
14
#include "format-table.h"
15
#include "format-util.h"
16
#include "log.h"
17
#include "main-func.h"
18
#include "mkdir-label.h"
19
#include "pager.h"
20
#include "parse-argument.h"
21
#include "parse-util.h"
22
#include "pretty-print.h"
23
#include "recurse-dir.h"
24
#include "socket-util.h"
25
#include "strv.h"
26
#include "terminal-util.h"
27
#include "uid-classification.h"
28
#include "uid-range.h"
29
#include "umask-util.h"
30
#include "user-record-show.h"
31
#include "user-util.h"
32
#include "userdb.h"
33
#include "verbs.h"
34
#include "virt.h"
35

36
static enum {
37
        OUTPUT_CLASSIC,
38
        OUTPUT_TABLE,
39
        OUTPUT_FRIENDLY,
40
        OUTPUT_JSON,
41
        _OUTPUT_INVALID = -EINVAL,
42
} arg_output = _OUTPUT_INVALID;
43

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

57
STATIC_DESTRUCTOR_REGISTER(arg_services, strv_freep);
307✔
58
STATIC_DESTRUCTOR_REGISTER(arg_from_file, sd_json_variant_unrefp);
307✔
59

60
static const char *user_disposition_to_color(UserDisposition d) {
444✔
61
        assert(d >= 0);
444✔
62
        assert(d < _USER_DISPOSITION_MAX);
444✔
63

64
        switch (d) {
444✔
65
        case USER_INTRINSIC:
66
                return ansi_red();
28✔
67

68
        case USER_SYSTEM:
69
        case USER_DYNAMIC:
70
                return ansi_green();
392✔
71

72
        case USER_CONTAINER:
73
        case USER_FOREIGN:
74
                return ansi_cyan();
10✔
75

76
        case USER_RESERVED:
77
                return ansi_red();
×
78

79
        default:
80
                return NULL;
81
        }
82
}
83

84
static const char* shell_to_color(const char *shell) {
210✔
85
        return !shell || is_nologin_shell(shell) ? ansi_grey() : NULL;
210✔
86
}
87

88
static int show_user(UserRecord *ur, Table *table) {
416✔
89
        int r;
416✔
90

91
        assert(ur);
416✔
92

93
        switch (arg_output) {
416✔
94

95
        case OUTPUT_CLASSIC:
26✔
96
                if (!uid_is_valid(ur->uid))
26✔
97
                        break;
98

99
                printf("%s:x:" UID_FMT ":" GID_FMT ":%s:%s:%s\n",
52✔
100
                       ur->user_name,
101
                       ur->uid,
102
                       user_record_gid(ur),
103
                       strempty(user_record_real_name(ur)),
26✔
104
                       user_record_home_directory(ur),
105
                       user_record_shell(ur));
106

107
                break;
26✔
108

109
        case OUTPUT_JSON:
96✔
110
                sd_json_variant_dump(ur->json, arg_json_format_flags, NULL, NULL);
96✔
111
                break;
96✔
112

113
        case OUTPUT_FRIENDLY:
84✔
114
                user_record_show(ur, true);
84✔
115

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

121
                break;
122

123
        case OUTPUT_TABLE: {
210✔
124
                assert(table);
210✔
125
                UserDisposition d = user_record_disposition(ur);
210✔
126
                const char *sh = user_record_shell(ur);
210✔
127

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

144
                break;
145
        }
146

147
        default:
×
148
                assert_not_reached();
×
149
        }
150

151
        return 0;
152
}
153

154
static bool test_show_mapped(void) {
16✔
155
        /* Show mapped user range only in environments where user mapping is a thing. */
156
        return running_in_userns() > 0;
16✔
157
}
158

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

206
static int table_add_uid_boundaries(Table *table, const UIDRange *p) {
12✔
207
        int r, n_added = 0;
12✔
208

209
        assert(table);
12✔
210

211
        FOREACH_ELEMENT(i, uid_range_table) {
84✔
212
                _cleanup_free_ char *name = NULL, *comment = NULL;
44✔
213

214
                if (!BIT_SET(arg_disposition_mask, i->disposition))
72✔
215
                        continue;
18✔
216

217
                if (!uid_range_covers(p, i->first, i->last - i->first + 1))
54✔
218
                        continue;
×
219

220
                if (i->test && !i->test())
54✔
221
                        continue;
10✔
222

223
                name = strjoin(glyph(GLYPH_ARROW_DOWN),
44✔
224
                               " begin ", i->name, " users ",
225
                               glyph(GLYPH_ARROW_DOWN));
226
                if (!name)
44✔
227
                        return log_oom();
×
228

229
                comment = strjoin("First ", i->name, " user");
44✔
230
                if (!comment)
44✔
231
                        return log_oom();
×
232

233
                r = table_add_many(
44✔
234
                                table,
235
                                TABLE_STRING, glyph(GLYPH_TREE_TOP),
236
                                TABLE_STRING, name,
237
                                TABLE_SET_COLOR, ansi_grey(),
238
                                TABLE_STRING, user_disposition_to_string(i->disposition),
239
                                TABLE_SET_COLOR, ansi_grey(),
240
                                TABLE_UID, i->first,
241
                                TABLE_SET_COLOR, ansi_grey(),
242
                                TABLE_EMPTY,
243
                                TABLE_STRING, comment,
244
                                TABLE_SET_COLOR, ansi_grey(),
245
                                TABLE_EMPTY,
246
                                TABLE_EMPTY,
247
                                TABLE_INT, -1); /* sort before any other entry with the same UID */
248
                if (r < 0)
44✔
249
                        return table_log_add_error(r);
×
250

251
                free(name);
44✔
252
                name = strjoin(glyph(GLYPH_ARROW_UP),
44✔
253
                               " end ", i->name, " users ",
254
                               glyph(GLYPH_ARROW_UP));
255
                if (!name)
44✔
256
                        return log_oom();
×
257

258
                free(comment);
44✔
259
                comment = strjoin("Last ", i->name, " user");
44✔
260
                if (!comment)
44✔
261
                        return log_oom();
×
262

263
                r = table_add_many(
44✔
264
                                table,
265
                                TABLE_STRING, glyph(GLYPH_TREE_RIGHT),
266
                                TABLE_STRING, name,
267
                                TABLE_SET_COLOR, ansi_grey(),
268
                                TABLE_STRING, user_disposition_to_string(i->disposition),
269
                                TABLE_SET_COLOR, ansi_grey(),
270
                                TABLE_UID, i->last,
271
                                TABLE_SET_COLOR, ansi_grey(),
272
                                TABLE_EMPTY,
273
                                TABLE_STRING, comment,
274
                                TABLE_SET_COLOR, ansi_grey(),
275
                                TABLE_EMPTY,
276
                                TABLE_EMPTY,
277
                                TABLE_INT, 1); /* sort after any other entry with the same UID */
278
                if (r < 0)
44✔
279
                        return table_log_add_error(r);
×
280

281
                n_added += 2;
44✔
282
        }
283

284
        return n_added;
285
}
286

287
static int add_unavailable_uid(Table *table, uid_t start, uid_t end) {
×
288
        _cleanup_free_ char *name = NULL;
×
289
        int r;
×
290

291
        assert(table);
×
292
        assert(start <= end);
×
293

294
        name = strjoin(glyph(GLYPH_ARROW_DOWN),
×
295
                       " begin unavailable users ",
296
                       glyph(GLYPH_ARROW_DOWN));
297
        if (!name)
×
298
                return log_oom();
×
299

300
        r = table_add_many(
×
301
                        table,
302
                        TABLE_STRING, glyph(GLYPH_TREE_TOP),
303
                        TABLE_STRING, name,
304
                        TABLE_SET_COLOR, ansi_grey(),
305
                        TABLE_EMPTY,
306
                        TABLE_UID, start,
307
                        TABLE_SET_COLOR, ansi_grey(),
308
                        TABLE_EMPTY,
309
                        TABLE_STRING, "First unavailable user",
310
                        TABLE_SET_COLOR, ansi_grey(),
311
                        TABLE_EMPTY,
312
                        TABLE_EMPTY,
313
                        TABLE_INT, -1); /* sort before an other entry with the same UID */
314
        if (r < 0)
×
315
                return table_log_add_error(r);
×
316

317
        free(name);
×
318
        name = strjoin(glyph(GLYPH_ARROW_UP),
×
319
                       " end unavailable users ",
320
                       glyph(GLYPH_ARROW_UP));
321
        if (!name)
×
322
                return log_oom();
×
323

324
        r = table_add_many(
×
325
                        table,
326
                        TABLE_STRING, glyph(GLYPH_TREE_RIGHT),
327
                        TABLE_STRING, name,
328
                        TABLE_SET_COLOR, ansi_grey(),
329
                        TABLE_EMPTY,
330
                        TABLE_UID, end,
331
                        TABLE_SET_COLOR, ansi_grey(),
332
                        TABLE_EMPTY,
333
                        TABLE_STRING, "Last unavailable user",
334
                        TABLE_SET_COLOR, ansi_grey(),
335
                        TABLE_EMPTY,
336
                        TABLE_EMPTY,
337
                        TABLE_INT, 1); /* sort after any other entry with the same UID */
338
        if (r < 0)
×
339
                return table_log_add_error(r);
×
340

341
        return 2;
342
}
343

344
static int table_add_uid_map(
20✔
345
                Table *table,
346
                const UIDRange *p,
347
                int (*add_unavailable)(Table *t, uid_t start, uid_t end)) {
348

349
        uid_t focus = 0;
20✔
350
        int n_added = 0, r;
20✔
351

352
        assert(table);
20✔
353
        assert(add_unavailable);
20✔
354

355
        if (!p)
20✔
356
                return 0;
357

358
        FOREACH_ARRAY(x, p->entries, p->n_entries) {
40✔
359
                if (focus < x->start) {
20✔
360
                        r = add_unavailable(table, focus, x->start-1);
×
361
                        if (r < 0)
×
362
                                return r;
363

364
                        n_added += r;
×
365
                }
366

367
                if (x->start > UINT32_MAX - x->nr) { /* overflow check */
20✔
368
                        focus = UINT32_MAX;
369
                        break;
370
                }
371

372
                focus = x->start + x->nr;
20✔
373
        }
374

375
        if (focus < UINT32_MAX-1) {
20✔
376
                r = add_unavailable(table, focus, UINT32_MAX-1);
×
377
                if (r < 0)
×
378
                        return r;
379

380
                n_added += r;
×
381
        }
382

383
        return n_added;
384
}
385

386
static int display_user(int argc, char *argv[], void *userdata) {
103✔
387
        _cleanup_(table_unrefp) Table *table = NULL;
×
388
        bool draw_separator = false;
103✔
389
        int ret = 0, r;
103✔
390

391
        if (arg_output < 0)
103✔
392
                arg_output = arg_from_file || (argc > 1 && !arg_fuzzy) ? OUTPUT_FRIENDLY : OUTPUT_TABLE;
92✔
393

394
        if (arg_output == OUTPUT_TABLE) {
103✔
395
                table = table_new(" ", "name", "disposition", "uid", "gid", "realname", "home", "shell", "order");
13✔
396
                if (!table)
13✔
397
                        return log_oom();
×
398

399
                (void) table_set_align_percent(table, table_get_cell(table, 0, 3), 100);
13✔
400
                (void) table_set_align_percent(table, table_get_cell(table, 0, 4), 100);
13✔
401
                table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
13✔
402
                (void) table_set_sort(table, (size_t) 3, (size_t) 8);
13✔
403
                (void) table_hide_column_from_display(table, (size_t) 8);
13✔
404
                if (!arg_boundaries)
13✔
405
                        (void) table_hide_column_from_display(table, (size_t) 0);
1✔
406
        }
407

408
        _cleanup_(userdb_match_done) UserDBMatch match = {
103✔
409
                .disposition_mask = arg_disposition_mask,
410
                .uid_min = arg_uid_min,
411
                .uid_max = arg_uid_max,
412
        };
413

414
        if (arg_from_file) {
103✔
415
                if (argc > 1)
3✔
416
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No argument expected when invoked with --from-file=, refusing.");
×
417

418
                _cleanup_(user_record_unrefp) UserRecord *ur = user_record_new();
6✔
419
                if (!ur)
3✔
420
                        return log_oom();
×
421

422
                r = user_record_load(ur, arg_from_file, USER_RECORD_LOAD_MASK_SECRET|USER_RECORD_LOG);
3✔
423
                if (r < 0)
3✔
424
                        return r;
425

426
                r = show_user(ur, table);
3✔
427
                if (r < 0)
3✔
428
                        return r;
429

430
        } else if (argc > 1 && !arg_fuzzy)
100✔
431
                STRV_FOREACH(i, argv + 1) {
175✔
432
                        _cleanup_(user_record_unrefp) UserRecord *ur = NULL;
93✔
433

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

445
                                RET_GATHER(ret, r);
20✔
446
                        } else {
447
                                if (draw_separator && arg_output == OUTPUT_FRIENDLY)
73✔
448
                                        putchar('\n');
4✔
449

450
                                r = show_user(ur, table);
73✔
451
                                if (r < 0)
73✔
452
                                        return r;
×
453

454
                                draw_separator = true;
455
                        }
456
                }
457
        else {
458
                if (argc > 1) {
1✔
459
                        /* If there are further arguments, they are the fuzzy match strings. */
460
                        match.fuzzy_names = strv_copy(strv_skip(argv, 1));
1✔
461
                        if (!match.fuzzy_names)
1✔
462
                                return log_oom();
×
463
                }
464

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

477
                                r = userdb_iterator_get(iterator, &match, &ur);
358✔
478
                                if (r == -ESRCH)
358✔
479
                                        break;
480
                                if (r == -EHOSTDOWN)
340✔
481
                                        return log_error_errno(r, "Selected user database service is not available for this request.");
×
482
                                if (r < 0)
340✔
483
                                        return log_error_errno(r, "Failed acquire next user: %m");
×
484

485
                                if (draw_separator && arg_output == OUTPUT_FRIENDLY)
340✔
486
                                        putchar('\n');
25✔
487

488
                                r = show_user(ur, table);
340✔
489
                                if (r < 0)
340✔
490
                                        return r;
491

492
                                draw_separator = true;
340✔
493
                        }
494
                }
495
        }
496

497
        if (table) {
103✔
498
                int boundary_lines = 0, uid_map_lines = 0;
13✔
499

500
                if (arg_boundaries) {
13✔
501
                        _cleanup_(uid_range_freep) UIDRange *uid_range = NULL;
12✔
502

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

507
                        boundary_lines = table_add_uid_boundaries(table, uid_range);
12✔
508
                        if (boundary_lines < 0)
12✔
509
                                return boundary_lines;
510

511
                        uid_map_lines = table_add_uid_map(table, uid_range, add_unavailable_uid);
12✔
512
                        if (uid_map_lines < 0)
12✔
513
                                return uid_map_lines;
514
                }
515

516
                if (!table_isempty(table)) {
26✔
517
                        r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
13✔
518
                        if (r < 0)
13✔
519
                                return table_log_print_error(r);
×
520
                }
521

522
                if (arg_legend) {
13✔
523
                        size_t k;
12✔
524

525
                        k = table_get_rows(table) - 1 - boundary_lines - uid_map_lines;
12✔
526
                        if (k > 0)
12✔
527
                                printf("\n%zu users listed.\n", k);
10✔
528
                        else
529
                                printf("No users.\n");
2✔
530
                }
531
        }
532

533
        return ret;
534
}
535

536
static int show_group(GroupRecord *gr, Table *table) {
258✔
537
        int r;
258✔
538

539
        assert(gr);
258✔
540

541
        switch (arg_output) {
258✔
542

543
        case OUTPUT_CLASSIC: {
×
544
                _cleanup_free_ char *m = NULL;
×
545

546
                if (!gid_is_valid(gr->gid))
×
547
                        break;
548

549
                m = strv_join(gr->members, ",");
×
550
                if (!m)
×
551
                        return log_oom();
×
552

553
                printf("%s:x:" GID_FMT ":%s\n",
×
554
                       gr->group_name,
555
                       gr->gid,
556
                       m);
557
                break;
558
        }
559

560
        case OUTPUT_JSON:
5✔
561
                sd_json_variant_dump(gr->json, arg_json_format_flags, NULL, NULL);
5✔
562
                break;
5✔
563

564
        case OUTPUT_FRIENDLY:
19✔
565
                group_record_show(gr, true);
19✔
566

567
                if (gr->incomplete) {
19✔
568
                        fflush(stdout);
×
UNCOV
569
                        log_warning("Warning: lacking rights to acquire privileged fields of group record of '%s', output incomplete.", gr->group_name);
×
570
                }
571

572
                break;
573

574
        case OUTPUT_TABLE: {
234✔
575
                UserDisposition d;
234✔
576

577
                assert(table);
234✔
578
                d = group_record_disposition(gr);
234✔
579

580
                r = table_add_many(
234✔
581
                                table,
582
                                TABLE_STRING, "",
583
                                TABLE_STRING, gr->group_name,
584
                                TABLE_SET_COLOR, user_disposition_to_color(d),
585
                                TABLE_STRING, user_disposition_to_string(d),
586
                                TABLE_GID, gr->gid,
587
                                TABLE_STRING, gr->description,
588
                                TABLE_INT, 0);
589
                if (r < 0)
234✔
590
                        return table_log_add_error(r);
×
591

592
                break;
593
        }
594

595
        default:
×
596
                assert_not_reached();
×
597
        }
598

599
        return 0;
600
}
601

602
static int table_add_gid_boundaries(Table *table, const UIDRange *p) {
8✔
603
        int r, n_added = 0;
8✔
604

605
        assert(table);
8✔
606

607
        FOREACH_ELEMENT(i, uid_range_table) {
56✔
608
                _cleanup_free_ char *name = NULL, *comment = NULL;
24✔
609

610
                if (!BIT_SET(arg_disposition_mask, i->disposition))
48✔
611
                        continue;
18✔
612

613
                if (!uid_range_covers(p, i->first, i->last - i->first + 1))
30✔
614
                        continue;
×
615

616
                if (i->test && !i->test())
30✔
617
                        continue;
6✔
618

619
                name = strjoin(glyph(GLYPH_ARROW_DOWN),
24✔
620
                               " begin ", i->name, " groups ",
621
                               glyph(GLYPH_ARROW_DOWN));
622
                if (!name)
24✔
623
                        return log_oom();
×
624

625
                comment = strjoin("First ", i->name, " group");
24✔
626
                if (!comment)
24✔
627
                        return log_oom();
×
628

629
                r = table_add_many(
24✔
630
                                table,
631
                                TABLE_STRING, glyph(GLYPH_TREE_TOP),
632
                                TABLE_STRING, name,
633
                                TABLE_SET_COLOR, ansi_grey(),
634
                                TABLE_STRING, user_disposition_to_string(i->disposition),
635
                                TABLE_SET_COLOR, ansi_grey(),
636
                                TABLE_GID, i->first,
637
                                TABLE_SET_COLOR, ansi_grey(),
638
                                TABLE_STRING, comment,
639
                                TABLE_SET_COLOR, ansi_grey(),
640
                                TABLE_INT, -1); /* sort before any other entry with the same GID */
641
                if (r < 0)
24✔
642
                        return table_log_add_error(r);
×
643

644
                free(name);
24✔
645
                name = strjoin(glyph(GLYPH_ARROW_UP),
24✔
646
                               " end ", i->name, " groups ",
647
                               glyph(GLYPH_ARROW_UP));
648
                if (!name)
24✔
649
                        return log_oom();
×
650

651
                free(comment);
24✔
652
                comment = strjoin("Last ", i->name, " group");
24✔
653
                if (!comment)
24✔
654
                        return log_oom();
×
655

656
                r = table_add_many(
24✔
657
                                table,
658
                                TABLE_STRING, glyph(GLYPH_TREE_RIGHT),
659
                                TABLE_STRING, name,
660
                                TABLE_SET_COLOR, ansi_grey(),
661
                                TABLE_STRING, user_disposition_to_string(i->disposition),
662
                                TABLE_SET_COLOR, ansi_grey(),
663
                                TABLE_GID, i->last,
664
                                TABLE_SET_COLOR, ansi_grey(),
665
                                TABLE_STRING, comment,
666
                                TABLE_SET_COLOR, ansi_grey(),
667
                                TABLE_INT, 1); /* sort after any other entry with the same GID */
668
                if (r < 0)
24✔
669
                        return table_log_add_error(r);
×
670

671
                n_added += 2;
24✔
672
        }
673

674
        return n_added;
675
}
676

677
static int add_unavailable_gid(Table *table, uid_t start, uid_t end) {
×
678
        _cleanup_free_ char *name = NULL;
×
679
        int r;
×
680

681
        assert(table);
×
682
        assert(start <= end);
×
683

684
        name = strjoin(glyph(GLYPH_ARROW_DOWN),
×
685
                       " begin unavailable groups ",
686
                       glyph(GLYPH_ARROW_DOWN));
687
        if (!name)
×
688
                return log_oom();
×
689

690
        r = table_add_many(
×
691
                        table,
692
                        TABLE_STRING, glyph(GLYPH_TREE_TOP),
693
                        TABLE_STRING, name,
694
                        TABLE_SET_COLOR, ansi_grey(),
695
                        TABLE_EMPTY,
696
                        TABLE_GID, start,
697
                        TABLE_SET_COLOR, ansi_grey(),
698
                        TABLE_STRING, "First unavailable group",
699
                        TABLE_SET_COLOR, ansi_grey(),
700
                        TABLE_INT, -1); /* sort before any other entry with the same GID */
701
        if (r < 0)
×
702
                return table_log_add_error(r);
×
703

704
        free(name);
×
705
        name = strjoin(glyph(GLYPH_ARROW_UP),
×
706
                       " end unavailable groups ",
707
                       glyph(GLYPH_ARROW_UP));
708
        if (!name)
×
709
                return log_oom();
×
710

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

725
        return 2;
726
}
727

728
static int display_group(int argc, char *argv[], void *userdata) {
35✔
729
        _cleanup_(table_unrefp) Table *table = NULL;
×
730
        bool draw_separator = false;
35✔
731
        int ret = 0, r;
35✔
732

733
        if (arg_output < 0)
35✔
734
                arg_output = arg_from_file || (argc > 1 && !arg_fuzzy) ? OUTPUT_FRIENDLY : OUTPUT_TABLE;
40✔
735

736
        if (arg_output == OUTPUT_TABLE) {
35✔
737
                table = table_new(" ", "name", "disposition", "gid", "description", "order");
9✔
738
                if (!table)
9✔
739
                        return log_oom();
×
740

741
                (void) table_set_align_percent(table, table_get_cell(table, 0, 3), 100);
9✔
742
                table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
9✔
743
                (void) table_set_sort(table, (size_t) 3, (size_t) 5);
9✔
744
                (void) table_hide_column_from_display(table, (size_t) 5);
9✔
745
                if (!arg_boundaries)
9✔
746
                        (void) table_hide_column_from_display(table, (size_t) 0);
1✔
747
        }
748

749
        _cleanup_(userdb_match_done) UserDBMatch match = {
35✔
750
                .disposition_mask = arg_disposition_mask,
751
                .gid_min = arg_uid_min,
752
                .gid_max = arg_uid_max,
753
        };
754

755
        if (arg_from_file) {
35✔
756
                if (argc > 1)
3✔
757
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No argument expected when invoked with --from-file=, refusing.");
×
758

759
                _cleanup_(group_record_unrefp) GroupRecord *gr = group_record_new();
6✔
760
                if (!gr)
3✔
761
                        return log_oom();
×
762

763
                r = group_record_load(gr, arg_from_file, USER_RECORD_LOAD_MASK_SECRET|USER_RECORD_LOG);
3✔
764
                if (r < 0)
3✔
765
                        return r;
766

767
                r = show_group(gr, table);
3✔
768
                if (r < 0)
3✔
769
                        return r;
770

771
        } else if (argc > 1 && !arg_fuzzy)
32✔
772
                STRV_FOREACH(i, argv + 1) {
56✔
773
                        _cleanup_(group_record_unrefp) GroupRecord *gr = NULL;
33✔
774

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

786
                                RET_GATHER(ret, r);
12✔
787
                        } else {
788
                                if (draw_separator && arg_output == OUTPUT_FRIENDLY)
21✔
789
                                        putchar('\n');
4✔
790

791
                                r = show_group(gr, table);
21✔
792
                                if (r < 0)
21✔
793
                                        return r;
×
794

795
                                draw_separator = true;
796
                        }
797
                }
798
        else {
799
                if (argc > 1) {
1✔
800
                        match.fuzzy_names = strv_copy(strv_skip(argv, 1));
1✔
801
                        if (!match.fuzzy_names)
1✔
802
                                return log_oom();
×
803
                }
804

805
                _cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL;
×
806
                r = groupdb_all(&match, arg_userdb_flags, &iterator);
9✔
807
                if (r == -ENOLINK)
9✔
808
                        log_debug_errno(r, "No entries found. (Didn't check via Varlink.)");
×
809
                else if (r == -ESRCH)
9✔
810
                        log_debug_errno(r, "No entries found.");
9✔
811
                else if (r < 0)
9✔
812
                        return log_error_errno(r, "Failed to enumerate groups: %m");
×
813
                else {
814
                        for (;;) {
234✔
815
                                _cleanup_(group_record_unrefp) GroupRecord *gr = NULL;
9✔
816

817
                                r = groupdb_iterator_get(iterator, &match, &gr);
243✔
818
                                if (r == -ESRCH)
243✔
819
                                        break;
820
                                if (r == -EHOSTDOWN)
234✔
821
                                        return log_error_errno(r, "Selected group database service is not available for this request.");
×
822
                                if (r < 0)
234✔
823
                                        return log_error_errno(r, "Failed acquire next group: %m");
×
824

825
                                if (draw_separator && arg_output == OUTPUT_FRIENDLY)
234✔
826
                                        putchar('\n');
×
827

828
                                r = show_group(gr, table);
234✔
829
                                if (r < 0)
234✔
830
                                        return r;
831

832
                                draw_separator = true;
234✔
833
                        }
834
                }
835
        }
836

837
        if (table) {
35✔
838
                int boundary_lines = 0, gid_map_lines = 0;
9✔
839

840
                if (arg_boundaries) {
9✔
841
                        _cleanup_(uid_range_freep) UIDRange *gid_range = NULL;
8✔
842
                        r = uid_range_load_userns(/* path = */ NULL, GID_RANGE_USERNS_INSIDE, &gid_range);
8✔
843
                        if (r < 0)
8✔
844
                                log_debug_errno(r, "Failed to load /proc/self/gid_map, ignoring: %m");
×
845

846
                        boundary_lines = table_add_gid_boundaries(table, gid_range);
8✔
847
                        if (boundary_lines < 0)
8✔
848
                                return boundary_lines;
849

850
                        gid_map_lines = table_add_uid_map(table, gid_range, add_unavailable_gid);
8✔
851
                        if (gid_map_lines < 0)
8✔
852
                                return gid_map_lines;
853
                }
854

855
                if (!table_isempty(table)) {
18✔
856
                        r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
9✔
857
                        if (r < 0)
9✔
858
                                return table_log_print_error(r);
×
859
                }
860

861
                if (arg_legend) {
9✔
862
                        size_t k;
9✔
863

864
                        k = table_get_rows(table) - 1 - boundary_lines - gid_map_lines;
9✔
865
                        if (k > 0)
9✔
866
                                printf("\n%zu groups listed.\n", k);
7✔
867
                        else
868
                                printf("No groups.\n");
2✔
869
                }
870
        }
871

872
        return ret;
873
}
874

875
static int show_membership(const char *user, const char *group, Table *table) {
14✔
876
        int r;
14✔
877

878
        assert(user);
14✔
879
        assert(group);
14✔
880

881
        switch (arg_output) {
14✔
882

883
        case OUTPUT_CLASSIC:
×
884
                /* Strictly speaking there's no 'classic' output for this concept, but let's output it in
885
                 * similar style to the classic output for user/group info */
886

887
                printf("%s:%s\n", user, group);
×
888
                break;
×
889

890
        case OUTPUT_JSON: {
2✔
891
                _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
2✔
892

893
                r = sd_json_buildo(
2✔
894
                                &v,
895
                                SD_JSON_BUILD_PAIR("user", SD_JSON_BUILD_STRING(user)),
896
                                SD_JSON_BUILD_PAIR("group", SD_JSON_BUILD_STRING(group)));
897
                if (r < 0)
2✔
898
                        return log_error_errno(r, "Failed to build JSON object: %m");
×
899

900
                sd_json_variant_dump(v, arg_json_format_flags, NULL, NULL);
2✔
901
                break;
2✔
902
        }
903

904
        case OUTPUT_FRIENDLY:
×
905
                /* Hmm, this is not particularly friendly, but not sure how we could do this better */
906
                printf("%s: %s\n", group, user);
×
907
                break;
×
908

909
        case OUTPUT_TABLE:
12✔
910
                assert(table);
12✔
911

912
                r = table_add_many(
12✔
913
                                table,
914
                                TABLE_STRING, user,
915
                                TABLE_STRING, group);
916
                if (r < 0)
12✔
917
                        return table_log_add_error(r);
×
918

919
                break;
920

921
        default:
×
922
                assert_not_reached();
×
923
        }
924

925
        return 0;
926
}
927

928
static int display_memberships(int argc, char *argv[], void *userdata) {
14✔
929
        _cleanup_(table_unrefp) Table *table = NULL;
14✔
930
        int ret = 0, r;
14✔
931

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

935
        if (arg_output < 0)
14✔
936
                arg_output = OUTPUT_TABLE;
12✔
937

938
        if (arg_output == OUTPUT_TABLE) {
14✔
939
                table = table_new("user", "group");
12✔
940
                if (!table)
12✔
941
                        return log_oom();
×
942

943
                (void) table_set_sort(table, (size_t) 0, (size_t) 1);
12✔
944
        }
945

946
        if (argc > 1)
14✔
947
                STRV_FOREACH(i, argv + 1) {
26✔
948
                        _cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL;
4✔
949

950
                        if (streq(argv[0], "users-in-group")) {
18✔
951
                                r = membershipdb_by_group(*i, arg_userdb_flags, &iterator);
9✔
952
                                if (r < 0)
9✔
953
                                        return log_error_errno(r, "Failed to enumerate users in group: %m");
2✔
954
                        } else if (streq(argv[0], "groups-of-user")) {
9✔
955
                                r = membershipdb_by_user(*i, arg_userdb_flags, &iterator);
9✔
956
                                if (r < 0)
9✔
957
                                        return log_error_errno(r, "Failed to enumerate groups of user: %m");
2✔
958
                        } else
959
                                assert_not_reached();
×
960

961
                        for (;;) {
22✔
962
                                _cleanup_free_ char *user = NULL, *group = NULL;
18✔
963

964
                                r = membershipdb_iterator_get(iterator, &user, &group);
18✔
965
                                if (r == -ESRCH)
18✔
966
                                        break;
967
                                if (r == -EHOSTDOWN)
4✔
968
                                        return log_error_errno(r, "Selected membership database service is not available for this request.");
×
969
                                if (r < 0)
4✔
970
                                        return log_error_errno(r, "Failed acquire next membership: %m");
×
971

972
                                r = show_membership(user, group, table);
4✔
973
                                if (r < 0)
4✔
974
                                        return r;
975
                        }
976
                }
977
        else {
978
                _cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL;
×
979

980
                r = membershipdb_all(arg_userdb_flags, &iterator);
2✔
981
                if (r == -ENOLINK)
2✔
982
                        log_debug_errno(r, "No entries found. (Didn't check via Varlink.)");
×
983
                else if (r == -ESRCH)
2✔
984
                        log_debug_errno(r, "No entries found.");
2✔
985
                else if (r < 0)
2✔
986
                        return log_error_errno(r, "Failed to enumerate memberships: %m");
×
987
                else {
988
                        for (;;) {
22✔
989
                                _cleanup_free_ char *user = NULL, *group = NULL;
12✔
990

991
                                r = membershipdb_iterator_get(iterator, &user, &group);
12✔
992
                                if (r == -ESRCH)
12✔
993
                                        break;
994
                                if (r == -EHOSTDOWN)
10✔
995
                                        return log_error_errno(r, "Selected membership database service is not available for this request.");
×
996
                                if (r < 0)
10✔
997
                                        return log_error_errno(r, "Failed acquire next membership: %m");
×
998

999
                                r = show_membership(user, group, table);
10✔
1000
                                if (r < 0)
10✔
1001
                                        return r;
1002
                        }
1003
                }
1004
        }
1005

1006
        if (table) {
10✔
1007
                if (!table_isempty(table)) {
8✔
1008
                        r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
4✔
1009
                        if (r < 0)
4✔
1010
                                return table_log_print_error(r);
×
1011
                }
1012

1013
                if (arg_legend) {
8✔
1014
                        if (table_isempty(table))
16✔
1015
                                printf("No memberships.\n");
4✔
1016
                        else
1017
                                printf("\n%zu memberships listed.\n", table_get_rows(table) - 1);
4✔
1018
                }
1019
        }
1020

1021
        return ret;
1022
}
1023

1024
static int display_services(int argc, char *argv[], void *userdata) {
2✔
1025
        _cleanup_(table_unrefp) Table *t = NULL;
×
1026
        _cleanup_closedir_ DIR *d = NULL;
2✔
1027
        int r;
2✔
1028

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

1032
        d = opendir("/run/systemd/userdb/");
2✔
1033
        if (!d) {
2✔
1034
                if (errno == ENOENT) {
×
1035
                        log_info("No services.");
×
1036
                        return 0;
×
1037
                }
1038

1039
                return log_error_errno(errno, "Failed to open /run/systemd/userdb/: %m");
×
1040
        }
1041

1042
        t = table_new("service", "listening");
2✔
1043
        if (!t)
2✔
1044
                return log_oom();
×
1045

1046
        (void) table_set_sort(t, (size_t) 0);
2✔
1047

1048
        FOREACH_DIRENT(de, d, return -errno) {
18✔
1049
                _cleanup_free_ char *j = NULL, *no = NULL;
12✔
1050
                _cleanup_close_ int fd = -EBADF;
12✔
1051

1052
                j = path_join("/run/systemd/userdb/", de->d_name);
12✔
1053
                if (!j)
12✔
1054
                        return log_oom();
×
1055

1056
                fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
12✔
1057
                if (fd < 0)
12✔
1058
                        return log_error_errno(errno, "Failed to allocate AF_UNIX/SOCK_STREAM socket: %m");
×
1059

1060
                r = connect_unix_path(fd, dirfd(d), de->d_name);
12✔
1061
                if (r < 0) {
12✔
1062
                        no = strjoin("No (", errno_to_name(r), ")");
×
1063
                        if (!no)
×
1064
                                return log_oom();
×
1065
                }
1066

1067
                r = table_add_many(t,
24✔
1068
                                   TABLE_STRING, de->d_name,
1069
                                   TABLE_STRING, no ?: "yes",
1070
                                   TABLE_SET_COLOR, ansi_highlight_green_red(!no));
1071
                if (r < 0)
12✔
1072
                        return table_log_add_error(r);
×
1073
        }
1074

1075
        if (!table_isempty(t)) {
4✔
1076
                r = table_print_with_pager(t, arg_json_format_flags, arg_pager_flags, arg_legend);
2✔
1077
                if (r < 0)
2✔
1078
                        return table_log_print_error(r);
×
1079
        }
1080

1081
        if (arg_legend && arg_output != OUTPUT_JSON) {
2✔
1082
                if (table_isempty(t))
2✔
1083
                        printf("No services.\n");
×
1084
                else
1085
                        printf("\n%zu services listed.\n", table_get_rows(t) - 1);
1✔
1086
        }
1087

1088
        return 0;
1089
}
1090

1091
static int ssh_authorized_keys(int argc, char *argv[], void *userdata) {
10✔
1092
        _cleanup_(user_record_unrefp) UserRecord *ur = NULL;
×
1093
        char **chain_invocation;
10✔
1094
        int r;
10✔
1095

1096
        assert(argc >= 2);
10✔
1097

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

1101
        if (arg_chain) {
10✔
1102
                /* If --chain is specified, the rest of the command line is the chain command */
1103

1104
                if (argc < 3)
4✔
1105
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1✔
1106
                                               "No chain command line specified, refusing.");
1107

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

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

1117
                chain_invocation = argv + 2;
2✔
1118
        } else {
1119
                /* If --chain is not specified, then refuse any further arguments */
1120

1121
                if (argc > 2)
6✔
1122
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Too many arguments.");
×
1123

1124
                chain_invocation = NULL;
1125
        }
1126

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

1143
                if (ur->incomplete) {
6✔
1144
                        fflush(stdout);
×
1145
                        log_warning("Warning: lacking rights to acquire privileged fields of user record of '%s', output incomplete.", ur->user_name);
×
1146
                }
1147
        }
1148

1149
        if (chain_invocation) {
8✔
1150
                if (DEBUG_LOGGING) {
2✔
1151
                        _cleanup_free_ char *s = NULL;
1✔
1152

1153
                        s = quote_command_line(chain_invocation, SHELL_ESCAPE_EMPTY);
1✔
1154
                        if (!s)
1✔
1155
                                return log_oom();
×
1156

1157
                        log_debug("Chain invoking: %s", s);
1✔
1158
                }
1159

1160
                fflush(stdout);
2✔
1161
                execv(chain_invocation[0], chain_invocation);
×
1162
                if (errno == ENOENT) /* Let's handle ENOENT gracefully */
×
1163
                        log_warning_errno(errno, "Chain executable '%s' does not exist, ignoring chain invocation.", chain_invocation[0]);
8✔
1164
                else {
1165
                        log_error_errno(errno, "Failed to invoke chain executable '%s': %m", chain_invocation[0]);
×
1166
                        if (r >= 0)
×
1167
                                r = -errno;
×
1168
                }
1169
        }
1170

1171
        return r;
1172
}
1173

1174
static int load_credential_one(int credential_dir_fd, const char *name, int userdb_dir_fd) {
228✔
1175
        int r;
228✔
1176

1177
        assert(credential_dir_fd >= 0);
228✔
1178
        assert(name);
228✔
1179
        assert(userdb_dir_fd >= 0);
228✔
1180

1181
        const char *user = startswith(name, "userdb.user.");
228✔
1182
        const char *group = startswith(name, "userdb.group.");
228✔
1183
        if (!user && !group)
228✔
1184
                return 0;
228✔
1185

1186
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
228✔
1187
        unsigned line = 0, column = 0;
228✔
1188
        r = sd_json_parse_file_at(NULL, credential_dir_fd, name, SD_JSON_PARSE_SENSITIVE, &v, &line, &column);
228✔
1189
        if (r < 0)
228✔
1190
                return log_error_errno(r, "Failed to parse credential '%s' as JSON at %u:%u: %m", name, line, column);
×
1191

1192
        _cleanup_(user_record_unrefp) UserRecord *ur = NULL, *ur_stripped = NULL, *ur_privileged = NULL;
228✔
1193
        _cleanup_(group_record_unrefp) GroupRecord *gr = NULL, *gr_stripped = NULL, *gr_privileged = NULL;
228✔
1194
        _cleanup_free_ char *fn = NULL, *link = NULL;
228✔
1195

1196
        if (user) {
228✔
1197
                ur = user_record_new();
114✔
1198
                if (!ur)
114✔
1199
                        return log_oom();
14✔
1200

1201
                r = user_record_load(ur, v, USER_RECORD_LOAD_MASK_SECRET|USER_RECORD_LOG);
114✔
1202
                if (r < 0)
114✔
1203
                        return r;
1204

1205
                if (user_record_is_root(ur))
114✔
1206
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Creating 'root' user from credentials is not supported.");
×
1207
                if (user_record_is_nobody(ur))
114✔
1208
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Creating 'nobody' user from credentials is not supported.");
×
1209

1210
                if (!streq_ptr(user, ur->user_name))
114✔
1211
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1212
                                               "Credential suffix '%s' does not match user record name '%s'",
1213
                                               user, strna(ur->user_name));
1214

1215
                if (!uid_is_valid(ur->uid))
114✔
1216
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "JSON user record missing uid field");
×
1217

1218
                if (!gid_is_valid(user_record_gid(ur)))
114✔
1219
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "JSON user record missing gid field");
×
1220

1221
                _cleanup_(user_record_unrefp) UserRecord *m = NULL;
114✔
1222
                r = userdb_by_name(ur->user_name, /* match= */ NULL, USERDB_SUPPRESS_SHADOW, &m);
114✔
1223
                if (r >= 0) {
114✔
1224
                        if (m->uid != ur->uid)
14✔
1225
                                return log_error_errno(SYNTHETIC_ERRNO(EEXIST),
×
1226
                                                       "Cannot create user %s from credential %s as it already exists with UID " UID_FMT " instead of " UID_FMT,
1227
                                                       ur->user_name, name, m->uid, ur->uid);
1228

1229
                        log_info("User with name %s and UID " UID_FMT " already exists, not creating user from credential %s", ur->user_name, ur->uid, name);
14✔
1230
                        return 0;
14✔
1231
                }
1232
                if (r != -ESRCH)
100✔
1233
                        return log_error_errno(r, "Failed to check if user with name %s already exists: %m", ur->user_name);
×
1234

1235
                m = user_record_unref(m);
100✔
1236
                r = userdb_by_uid(ur->uid, /* match= */ NULL, USERDB_SUPPRESS_SHADOW, &m);
100✔
1237
                if (r >= 0) {
100✔
1238
                        if (!streq_ptr(ur->user_name, m->user_name))
×
1239
                                return log_error_errno(SYNTHETIC_ERRNO(EEXIST),
×
1240
                                                       "Cannot create user %s from credential %s as UID " UID_FMT " is already assigned to user %s",
1241
                                                       ur->user_name, name, ur->uid, m->user_name);
1242

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

1249
                r = user_record_clone(ur, USER_RECORD_LOAD_MASK_PRIVILEGED|USER_RECORD_LOG, &ur_stripped);
100✔
1250
                if (r < 0)
100✔
1251
                        return r;
1252

1253
                r = user_record_clone(ur, USER_RECORD_EXTRACT_PRIVILEGED|USER_RECORD_EMPTY_OK|USER_RECORD_LOG, &ur_privileged);
100✔
1254
                if (r < 0)
100✔
1255
                        return r;
1256

1257
                fn = strjoin(ur->user_name, ".user");
100✔
1258
                if (!fn)
100✔
1259
                        return log_oom();
×
1260

1261
                if (asprintf(&link, UID_FMT ".user", ur->uid) < 0)
100✔
1262
                        return log_oom();
×
1263
        } else {
1264
                assert(group);
114✔
1265

1266
                gr = group_record_new();
114✔
1267
                if (!gr)
114✔
1268
                        return log_oom();
14✔
1269

1270
                r = group_record_load(gr, v, USER_RECORD_LOAD_MASK_SECRET|USER_RECORD_LOG);
114✔
1271
                if (r < 0)
114✔
1272
                        return r;
1273

1274
                if (group_record_is_root(gr))
114✔
1275
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Creating 'root' group from credentials is not supported.");
×
1276
                if (group_record_is_nobody(gr))
114✔
1277
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Creating 'nobody' group from credentials is not supported.");
×
1278

1279
                if (!streq_ptr(group, gr->group_name))
114✔
1280
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1281
                                               "Credential suffix '%s' does not match group record name '%s'",
1282
                                               group, strna(gr->group_name));
1283

1284
                if (!gid_is_valid(gr->gid))
114✔
1285
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "JSON group record missing gid field");
×
1286

1287
                _cleanup_(group_record_unrefp) GroupRecord *m = NULL;
114✔
1288
                r = groupdb_by_name(gr->group_name, /* match= */ NULL, USERDB_SUPPRESS_SHADOW, &m);
114✔
1289
                if (r >= 0) {
114✔
1290
                        if (m->gid != gr->gid)
14✔
1291
                                return log_error_errno(SYNTHETIC_ERRNO(EEXIST),
×
1292
                                                       "Cannot create group %s from credential %s as it already exists with GID " GID_FMT " instead of " GID_FMT,
1293
                                                       gr->group_name, name, m->gid, gr->gid);
1294

1295
                        log_info("Group with name %s and GID " GID_FMT " already exists, not creating group from credential %s", gr->group_name, gr->gid, name);
14✔
1296
                        return 0;
14✔
1297
                }
1298
                if (r != -ESRCH)
100✔
1299
                        return log_error_errno(r, "Failed to check if group with name %s already exists: %m", gr->group_name);
×
1300

1301
                m = group_record_unref(m);
100✔
1302
                r = groupdb_by_gid(gr->gid, /* match= */ NULL, USERDB_SUPPRESS_SHADOW, &m);
100✔
1303
                if (r >= 0) {
100✔
1304
                        if (!streq_ptr(gr->group_name, m->group_name))
×
1305
                                return log_error_errno(SYNTHETIC_ERRNO(EEXIST),
×
1306
                                                       "Cannot create group %s from credential %s as GID " GID_FMT " is already assigned to group %s",
1307
                                                       gr->group_name, name, gr->gid, m->group_name);
1308

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

1315
                r = group_record_clone(gr, USER_RECORD_LOAD_MASK_PRIVILEGED|USER_RECORD_LOG, &gr_stripped);
100✔
1316
                if (r < 0)
100✔
1317
                        return r;
1318

1319
                r = group_record_clone(gr, USER_RECORD_EXTRACT_PRIVILEGED|USER_RECORD_EMPTY_OK|USER_RECORD_LOG, &gr_privileged);
100✔
1320
                if (r < 0)
100✔
1321
                        return r;
1322

1323
                fn = strjoin(gr->group_name, ".group");
100✔
1324
                if (!fn)
100✔
1325
                        return log_oom();
×
1326

1327
                if (asprintf(&link, GID_FMT ".group", gr->gid) < 0)
100✔
1328
                        return log_oom();
×
1329
        }
1330

1331
        if (!filename_is_valid(fn))
200✔
1332
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1333
                                       "Passed credential '%s' would result in invalid filename '%s'.",
1334
                                       name, fn);
1335

1336
        _cleanup_free_ char *formatted = NULL;
200✔
1337
        r = sd_json_variant_format(ur ? ur_stripped->json : gr_stripped->json, SD_JSON_FORMAT_NEWLINE, &formatted);
200✔
1338
        if (r < 0)
200✔
1339
                return log_error_errno(r, "Failed to format JSON record: %m");
×
1340

1341
        r = write_string_file_at(userdb_dir_fd, fn, formatted, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC);
200✔
1342
        if (r < 0)
200✔
1343
                return log_error_errno(r, "Failed to write JSON record to /etc/userdb/%s: %m", fn);
×
1344

1345
        if (symlinkat(fn, userdb_dir_fd, link) < 0)
200✔
1346
                return log_error_errno(errno, "Failed to create symlink from %s to %s", link, fn);
×
1347

1348
        log_info("Installed /etc/userdb/%s from credential.", fn);
200✔
1349

1350
        if ((ur && !sd_json_variant_is_blank_object(ur_privileged->json)) ||
200✔
1351
            (gr && !sd_json_variant_is_blank_object(gr_privileged->json))) {
100✔
1352
                fn = mfree(fn);
100✔
1353
                fn = strjoin(ur ? ur->user_name : gr->group_name, ur ? ".user-privileged" : ".group-privileged");
100✔
1354
                if (!fn)
100✔
1355
                        return log_oom();
×
1356

1357
                formatted = mfree(formatted);
100✔
1358
                r = sd_json_variant_format(ur ? ur_privileged->json : gr_privileged->json, SD_JSON_FORMAT_NEWLINE, &formatted);
100✔
1359
                if (r < 0)
100✔
1360
                        return log_error_errno(r, "Failed to format JSON record: %m");
×
1361

1362
                r = write_string_file_at(userdb_dir_fd, fn, formatted, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_MODE_0600);
100✔
1363
                if (r < 0)
100✔
1364
                        return log_error_errno(r, "Failed to write JSON record to /etc/userdb/%s: %m", fn);
×
1365

1366
                link = mfree(link);
100✔
1367

1368
                if (ur) {
100✔
1369
                        if (asprintf(&link, UID_FMT ".user-privileged", ur->uid) < 0)
100✔
1370
                                return log_oom();
×
1371
                } else {
1372
                        if (asprintf(&link, GID_FMT ".group-privileged", gr->gid) < 0)
×
1373
                                return log_oom();
×
1374
                }
1375

1376
                if (symlinkat(fn, userdb_dir_fd, link) < 0)
100✔
1377
                        return log_error_errno(errno, "Failed to create symlink from %s to %s", link, fn);
×
1378

1379
                log_info("Installed /etc/userdb/%s from credential.", fn);
100✔
1380
        }
1381

1382
        if (ur)
200✔
1383
                STRV_FOREACH(g, ur->member_of) {
300✔
1384
                        _cleanup_free_ char *membership = strjoin(ur->user_name, ":", *g);
400✔
1385
                        if (!membership)
200✔
1386
                                return log_oom();
×
1387

1388
                        _cleanup_close_ int fd = openat(userdb_dir_fd, membership, O_WRONLY|O_CREAT|O_CLOEXEC, 0644);
400✔
1389
                        if (fd < 0)
200✔
1390
                                return log_error_errno(errno, "Failed to create %s: %m", membership);
×
1391

1392
                        log_info("Installed /etc/userdb/%s from credential.", membership);
200✔
1393
                }
1394
        else
1395
                STRV_FOREACH(u, gr->members) {
100✔
1396
                        _cleanup_free_ char *membership = strjoin(*u, ":", gr->group_name);
×
1397
                        if (!membership)
×
1398
                                return log_oom();
×
1399

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

1404
                        log_info("Installed /etc/userdb/%s from credential.", membership);
×
1405
                }
1406

1407
        if (ur && user_record_disposition(ur) == USER_REGULAR) {
200✔
1408
                const char *hd = user_record_home_directory(ur);
100✔
1409

1410
                r = RET_NERRNO(access(hd, F_OK));
300✔
1411
                if (r < 0) {
100✔
1412
                        if (r != -ENOENT)
100✔
1413
                                return log_error_errno(r, "Failed to check if %s exists: %m", hd);
×
1414

1415
                        WITH_UMASK(0000) {
200✔
1416
                                r = mkdir_parents(hd, 0755);
100✔
1417
                                if (r < 0)
100✔
1418
                                        return log_error_errno(r, "Failed to create parent directories of %s: %m", hd);
×
1419

1420
                                if (mkdir(hd, 0700) < 0 && errno != EEXIST)
100✔
1421
                                        return log_error_errno(errno, "Failed to create %s: %m", hd);
×
1422
                        }
1423

1424
                        if (chown(hd, ur->uid, user_record_gid(ur)) < 0)
100✔
1425
                                return log_error_errno(errno, "Failed to chown %s: %m", hd);
×
1426

1427
                        r = copy_tree(user_record_skeleton_directory(ur), hd, ur->uid, user_record_gid(ur),
100✔
1428
                                      COPY_REFLINK|COPY_MERGE, /* denylist= */ NULL, /* subvolumes= */NULL);
1429
                        if (r < 0 && r != -ENOENT)
100✔
1430
                                return log_error_errno(r, "Failed to copy skeleton directory to %s: %m", hd);
×
1431
                }
1432
        }
1433

1434
        return 0;
1435
}
1436

1437
static int load_credentials(int argc, char *argv[], void *userdata) {
114✔
1438
        int r;
114✔
1439

1440
        _cleanup_close_ int credential_dir_fd = open_credentials_dir();
228✔
1441
        if (IN_SET(credential_dir_fd, -ENXIO, -ENOENT)) {
114✔
1442
                /* Credential env var not set, or dir doesn't exist. */
1443
                log_debug("No credentials found.");
×
1444
                return 0;
×
1445
        }
1446
        if (credential_dir_fd < 0)
114✔
1447
                return log_error_errno(credential_dir_fd, "Failed to open credentials directory: %m");
×
1448

1449
        _cleanup_free_ DirectoryEntries *des = NULL;
114✔
1450
        r = readdir_all(credential_dir_fd, RECURSE_DIR_SORT|RECURSE_DIR_IGNORE_DOT|RECURSE_DIR_ENSURE_TYPE, &des);
114✔
1451
        if (r < 0)
114✔
1452
                return log_error_errno(r, "Failed to enumerate credentials: %m");
×
1453

1454
        _cleanup_close_ int userdb_dir_fd = xopenat_full(
228✔
1455
                AT_FDCWD, "/etc/userdb",
1456
                /* open_flags= */ O_DIRECTORY|O_CREAT|O_CLOEXEC,
1457
                /* xopen_flags= */ XO_LABEL,
1458
                /* mode= */ 0755);
1459
        if (userdb_dir_fd < 0)
114✔
1460
                return log_error_errno(userdb_dir_fd, "Failed to open '/etc/userdb/': %m");
×
1461

1462
        FOREACH_ARRAY(i, des->entries, des->n_entries) {
342✔
1463
                struct dirent *de = *i;
228✔
1464

1465
                if (de->d_type != DT_REG)
228✔
1466
                        continue;
×
1467

1468
                RET_GATHER(r, load_credential_one(credential_dir_fd, de->d_name, userdb_dir_fd));
228✔
1469
        }
1470

1471
        return r;
1472
}
1473

1474
static int help(int argc, char *argv[], void *userdata) {
1✔
1475
        _cleanup_free_ char *link = NULL;
1✔
1476
        int r;
1✔
1477

1478
        pager_open(arg_pager_flags);
1✔
1479

1480
        r = terminal_urlify_man("userdbctl", "1", &link);
1✔
1481
        if (r < 0)
1✔
1482
                return log_oom();
×
1483

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

1528
        return 0;
1529
}
1530

1531
static int parse_argv(int argc, char *argv[]) {
309✔
1532

1533
        enum {
309✔
1534
                ARG_VERSION = 0x100,
1535
                ARG_NO_PAGER,
1536
                ARG_NO_LEGEND,
1537
                ARG_OUTPUT,
1538
                ARG_WITH_NSS,
1539
                ARG_WITH_DROPIN,
1540
                ARG_WITH_VARLINK,
1541
                ARG_SYNTHESIZE,
1542
                ARG_MULTIPLEXER,
1543
                ARG_JSON,
1544
                ARG_CHAIN,
1545
                ARG_UID_MIN,
1546
                ARG_UID_MAX,
1547
                ARG_DISPOSITION,
1548
                ARG_BOUNDARIES,
1549
        };
1550

1551
        static const struct option options[] = {
309✔
1552
                { "help",         no_argument,       NULL, 'h'              },
1553
                { "version",      no_argument,       NULL, ARG_VERSION      },
1554
                { "no-pager",     no_argument,       NULL, ARG_NO_PAGER     },
1555
                { "no-legend",    no_argument,       NULL, ARG_NO_LEGEND    },
1556
                { "output",       required_argument, NULL, ARG_OUTPUT       },
1557
                { "service",      required_argument, NULL, 's'              },
1558
                { "with-nss",     required_argument, NULL, ARG_WITH_NSS     },
1559
                { "with-dropin",  required_argument, NULL, ARG_WITH_DROPIN  },
1560
                { "with-varlink", required_argument, NULL, ARG_WITH_VARLINK },
1561
                { "synthesize",   required_argument, NULL, ARG_SYNTHESIZE   },
1562
                { "multiplexer",  required_argument, NULL, ARG_MULTIPLEXER  },
1563
                { "json",         required_argument, NULL, ARG_JSON         },
1564
                { "chain",        no_argument,       NULL, ARG_CHAIN        },
1565
                { "uid-min",      required_argument, NULL, ARG_UID_MIN      },
1566
                { "uid-max",      required_argument, NULL, ARG_UID_MAX      },
1567
                { "fuzzy",        no_argument,       NULL, 'z'              },
1568
                { "disposition",  required_argument, NULL, ARG_DISPOSITION  },
1569
                { "boundaries",   required_argument, NULL, ARG_BOUNDARIES   },
1570
                { "from-file",    required_argument, NULL, 'F'              },
1571
                {}
1572
        };
1573

1574
        const char *e;
309✔
1575
        int r;
309✔
1576

1577
        assert(argc >= 0);
309✔
1578
        assert(argv);
309✔
1579

1580
        /* We are going to update this environment variable with our own, hence let's first read what is already set */
1581
        e = getenv("SYSTEMD_ONLY_USERDB");
309✔
1582
        if (e) {
309✔
1583
                char **l;
×
1584

1585
                l = strv_split(e, ":");
×
1586
                if (!l)
×
1587
                        return log_oom();
×
1588

1589
                strv_free(arg_services);
×
1590
                arg_services = l;
×
1591
        }
1592

1593
        /* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long()
1594
         * that checks for GNU extensions in optstring ('-' or '+' at the beginning). */
1595
        optind = 0;
309✔
1596

1597
        for (;;) {
400✔
1598
                int c;
400✔
1599

1600
                c = getopt_long(argc, argv,
400✔
1601
                                arg_chain ? "+hjs:NISRzBF:" : "hjs:NISRzBF:", /* When --chain was used disable parsing of further switches */
400✔
1602
                                options, NULL);
1603
                if (c < 0)
400✔
1604
                        break;
1605

1606
                switch (c) {
121✔
1607

1608
                case 'h':
1✔
1609
                        return help(0, NULL, NULL);
1✔
1610

1611
                case ARG_VERSION:
1✔
1612
                        return version();
1✔
1613

1614
                case ARG_NO_PAGER:
×
1615
                        arg_pager_flags |= PAGER_DISABLE;
×
1616
                        break;
×
1617

1618
                case ARG_NO_LEGEND:
1✔
1619
                        arg_legend = false;
1✔
1620
                        break;
1✔
1621

1622
                case ARG_OUTPUT:
8✔
1623
                        if (isempty(optarg))
8✔
1624
                                arg_output = _OUTPUT_INVALID;
×
1625
                        else if (streq(optarg, "classic"))
8✔
1626
                                arg_output = OUTPUT_CLASSIC;
1✔
1627
                        else if (streq(optarg, "friendly"))
7✔
1628
                                arg_output = OUTPUT_FRIENDLY;
1✔
1629
                        else if (streq(optarg, "json"))
6✔
1630
                                arg_output = OUTPUT_JSON;
1✔
1631
                        else if (streq(optarg, "table"))
5✔
1632
                                arg_output = OUTPUT_TABLE;
1✔
1633
                        else if (streq(optarg, "help")) {
4✔
1634
                                puts("classic\n"
×
1635
                                     "friendly\n"
1636
                                     "json\n"
1637
                                     "table");
1638
                                return 0;
×
1639
                        } else
1640
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid --output= mode: %s", optarg);
4✔
1641

1642
                        arg_json_format_flags = arg_output == OUTPUT_JSON ? SD_JSON_FORMAT_PRETTY|SD_JSON_FORMAT_COLOR_AUTO : SD_JSON_FORMAT_OFF;
4✔
1643
                        break;
4✔
1644

1645
                case ARG_JSON:
6✔
1646
                        r = parse_json_argument(optarg, &arg_json_format_flags);
6✔
1647
                        if (r <= 0)
6✔
1648
                                return r;
1649

1650
                        arg_output = sd_json_format_enabled(arg_json_format_flags) ? OUTPUT_JSON : _OUTPUT_INVALID;
2✔
1651
                        break;
2✔
1652

1653
                case 'j':
26✔
1654
                        arg_json_format_flags = SD_JSON_FORMAT_PRETTY|SD_JSON_FORMAT_COLOR_AUTO;
26✔
1655
                        arg_output = OUTPUT_JSON;
26✔
1656
                        break;
26✔
1657

1658
                case 's':
×
1659
                        if (isempty(optarg))
×
1660
                                arg_services = strv_free(arg_services);
×
1661
                        else {
1662
                                r = strv_split_and_extend(&arg_services, optarg, ":", /* filter_duplicates = */ true);
×
1663
                                if (r < 0)
×
1664
                                        return log_error_errno(r, "Failed to parse -s/--service= argument: %m");
×
1665
                        }
1666

1667
                        break;
1668

1669
                case 'N':
1✔
1670
                        arg_userdb_flags |= USERDB_EXCLUDE_NSS|USERDB_DONT_SYNTHESIZE_INTRINSIC|USERDB_DONT_SYNTHESIZE_FOREIGN;
1✔
1671
                        break;
1✔
1672

1673
                case ARG_WITH_NSS:
14✔
1674
                        r = parse_boolean_argument("--with-nss=", optarg, NULL);
14✔
1675
                        if (r < 0)
14✔
1676
                                return r;
1677

1678
                        SET_FLAG(arg_userdb_flags, USERDB_EXCLUDE_NSS, !r);
10✔
1679
                        break;
10✔
1680

1681
                case ARG_WITH_DROPIN:
8✔
1682
                        r = parse_boolean_argument("--with-dropin=", optarg, NULL);
8✔
1683
                        if (r < 0)
8✔
1684
                                return r;
1685

1686
                        SET_FLAG(arg_userdb_flags, USERDB_EXCLUDE_DROPIN, !r);
4✔
1687
                        break;
4✔
1688

1689
                case ARG_WITH_VARLINK:
7✔
1690
                        r = parse_boolean_argument("--with-varlink=", optarg, NULL);
7✔
1691
                        if (r < 0)
7✔
1692
                                return r;
1693

1694
                        SET_FLAG(arg_userdb_flags, USERDB_EXCLUDE_VARLINK, !r);
3✔
1695
                        break;
3✔
1696

1697
                case ARG_SYNTHESIZE:
12✔
1698
                        r = parse_boolean_argument("--synthesize=", optarg, NULL);
12✔
1699
                        if (r < 0)
12✔
1700
                                return r;
1701

1702
                        SET_FLAG(arg_userdb_flags, USERDB_DONT_SYNTHESIZE_INTRINSIC|USERDB_DONT_SYNTHESIZE_FOREIGN, !r);
8✔
1703
                        break;
8✔
1704

1705
                case ARG_MULTIPLEXER:
6✔
1706
                        r = parse_boolean_argument("--multiplexer=", optarg, NULL);
6✔
1707
                        if (r < 0)
6✔
1708
                                return r;
1709

1710
                        SET_FLAG(arg_userdb_flags, USERDB_AVOID_MULTIPLEXER, !r);
2✔
1711
                        break;
2✔
1712

1713
                case ARG_CHAIN:
4✔
1714
                        arg_chain = true;
4✔
1715
                        break;
4✔
1716

1717
                case ARG_DISPOSITION: {
4✔
1718
                        UserDisposition d = user_disposition_from_string(optarg);
4✔
1719
                        if (d < 0)
4✔
1720
                                return log_error_errno(d, "Unknown user disposition: %s", optarg);
×
1721

1722
                        if (arg_disposition_mask == UINT64_MAX)
4✔
1723
                                arg_disposition_mask = 0;
2✔
1724

1725
                        arg_disposition_mask |= UINT64_C(1) << d;
4✔
1726
                        break;
4✔
1727
                }
1728

1729
                case 'I':
2✔
1730
                        if (arg_disposition_mask == UINT64_MAX)
2✔
1731
                                arg_disposition_mask = 0;
2✔
1732

1733
                        arg_disposition_mask |= UINT64_C(1) << USER_INTRINSIC;
2✔
1734
                        break;
2✔
1735

1736
                case 'S':
4✔
1737
                        if (arg_disposition_mask == UINT64_MAX)
4✔
1738
                                arg_disposition_mask = 0;
2✔
1739

1740
                        arg_disposition_mask |= UINT64_C(1) << USER_SYSTEM;
4✔
1741
                        break;
4✔
1742

1743
                case 'R':
2✔
1744
                        if (arg_disposition_mask == UINT64_MAX)
2✔
1745
                                arg_disposition_mask = 0;
2✔
1746

1747
                        arg_disposition_mask |= UINT64_C(1) << USER_REGULAR;
2✔
1748
                        break;
2✔
1749

1750
                case ARG_UID_MIN:
2✔
1751
                        r = parse_uid(optarg, &arg_uid_min);
2✔
1752
                        if (r < 0)
2✔
1753
                                return log_error_errno(r, "Failed to parse --uid-min= value: %s", optarg);
×
1754
                        break;
1755

1756
                case ARG_UID_MAX:
2✔
1757
                        r = parse_uid(optarg, &arg_uid_max);
2✔
1758
                        if (r < 0)
2✔
1759
                                return log_error_errno(r, "Failed to parse --uid-max= value: %s", optarg);
×
1760
                        break;
1761

1762
                case 'z':
2✔
1763
                        arg_fuzzy = true;
2✔
1764
                        break;
2✔
1765

1766
                case ARG_BOUNDARIES:
×
1767
                        r = parse_boolean_argument("boundaries", optarg, &arg_boundaries);
×
1768
                        if (r < 0)
×
1769
                                return r;
1770
                        break;
1771

1772
                case 'B':
2✔
1773
                        arg_boundaries = false;
2✔
1774
                        break;
2✔
1775

1776
                case 'F': {
6✔
1777
                        if (isempty(optarg)) {
6✔
1778
                                arg_from_file = sd_json_variant_unref(arg_from_file);
×
1779
                                break;
6✔
1780
                        }
1781

1782
                        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
6✔
1783
                        const char *fn = streq(optarg, "-") ? NULL : optarg;
6✔
1784
                        unsigned line = 0;
6✔
1785
                        r = sd_json_parse_file(fn ? NULL : stdin, fn ?: "<stdin>", SD_JSON_PARSE_SENSITIVE, &v, &line, /* reterr_column= */ NULL);
6✔
1786
                        if (r < 0)
6✔
1787
                                return log_syntax(/* unit= */ NULL, LOG_ERR, fn ?: "<stdin>", line, r, "JSON parse failure.");
×
1788

1789
                        sd_json_variant_unref(arg_from_file);
6✔
1790
                        arg_from_file = TAKE_PTR(v);
6✔
1791
                        break;
6✔
1792
                }
1793

1794
                case '?':
1795
                        return -EINVAL;
1796

1797
                default:
×
1798
                        assert_not_reached();
×
1799
                }
1800
        }
1801

1802
        if (arg_uid_min > arg_uid_max)
279✔
1803
                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);
×
1804

1805
        /* If not mask was specified, use the all bits on mask */
1806
        if (arg_disposition_mask == UINT64_MAX)
279✔
1807
                arg_disposition_mask = USER_DISPOSITION_MASK_ALL;
271✔
1808

1809
        if (arg_from_file)
279✔
1810
                arg_boundaries = false;
6✔
1811

1812
        return 1;
1813
}
1814

1815
static int run(int argc, char *argv[]) {
309✔
1816
        static const Verb verbs[] = {
309✔
1817
                { "help",                VERB_ANY, VERB_ANY, 0,            help                },
1818
                { "user",                VERB_ANY, VERB_ANY, VERB_DEFAULT, display_user        },
1819
                { "group",               VERB_ANY, VERB_ANY, 0,            display_group       },
1820
                { "users-in-group",      VERB_ANY, VERB_ANY, 0,            display_memberships },
1821
                { "groups-of-user",      VERB_ANY, VERB_ANY, 0,            display_memberships },
1822
                { "services",            VERB_ANY, 1,        0,            display_services    },
1823
                { "ssh-authorized-keys", 2,        VERB_ANY, 0,            ssh_authorized_keys },
1824
                { "load-credentials",    VERB_ANY, 1,        0,            load_credentials    },
1825
                {}
1826
        };
1827

1828
        int r;
309✔
1829

1830
        log_setup();
309✔
1831

1832
        r = parse_argv(argc, argv);
309✔
1833
        if (r <= 0)
309✔
1834
                return r;
1835

1836
        if (arg_services) {
279✔
1837
                _cleanup_free_ char *e = NULL;
×
1838

1839
                e = strv_join(arg_services, ":");
×
1840
                if (!e)
×
1841
                        return log_oom();
×
1842

1843
                if (setenv("SYSTEMD_ONLY_USERDB", e, true) < 0)
×
1844
                        return log_error_errno(r, "Failed to set $SYSTEMD_ONLY_USERDB: %m");
×
1845

1846
                log_info("Enabled services: %s", e);
×
1847
        } else
1848
                assert_se(unsetenv("SYSTEMD_ONLY_USERDB") == 0);
279✔
1849

1850
        return dispatch_verb(argc, argv, verbs, NULL);
279✔
1851
}
1852

1853
DEFINE_MAIN_FUNCTION(run);
618✔
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