]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/userdb/userdbctl.c
Merge pull request #16491 from keszybz/udev-logging
[thirdparty/systemd.git] / src / userdb / userdbctl.c
CommitLineData
1604937f
LP
1/* SPDX-License-Identifier: LGPL-2.1+ */
2
3#include <getopt.h>
4#include <utmp.h>
5
6#include "dirent-util.h"
7#include "errno-list.h"
8#include "fd-util.h"
9#include "format-table.h"
10#include "format-util.h"
11#include "group-record-show.h"
12#include "main-func.h"
13#include "pager.h"
14#include "parse-util.h"
15#include "pretty-print.h"
16#include "socket-util.h"
17#include "strv.h"
18#include "terminal-util.h"
19#include "user-record-show.h"
20#include "user-util.h"
21#include "userdb.h"
22#include "verbs.h"
23
24static enum {
25 OUTPUT_CLASSIC,
26 OUTPUT_TABLE,
27 OUTPUT_FRIENDLY,
28 OUTPUT_JSON,
29 _OUTPUT_INVALID = -1
30} arg_output = _OUTPUT_INVALID;
31
32static PagerFlags arg_pager_flags = 0;
33static bool arg_legend = true;
34static char** arg_services = NULL;
35static UserDBFlags arg_userdb_flags = 0;
36
37STATIC_DESTRUCTOR_REGISTER(arg_services, strv_freep);
38
39static int show_user(UserRecord *ur, Table *table) {
40 int r;
41
42 assert(ur);
43
44 switch (arg_output) {
45
46 case OUTPUT_CLASSIC:
47 if (!uid_is_valid(ur->uid))
48 break;
49
50 printf("%s:x:" UID_FMT ":" GID_FMT ":%s:%s:%s\n",
51 ur->user_name,
52 ur->uid,
53 user_record_gid(ur),
54 strempty(user_record_real_name(ur)),
55 user_record_home_directory(ur),
56 user_record_shell(ur));
57
58 break;
59
60 case OUTPUT_JSON:
61 json_variant_dump(ur->json, JSON_FORMAT_COLOR_AUTO|JSON_FORMAT_PRETTY, NULL, 0);
62 break;
63
64 case OUTPUT_FRIENDLY:
65 user_record_show(ur, true);
66
67 if (ur->incomplete) {
68 fflush(stdout);
69 log_warning("Warning: lacking rights to acquire privileged fields of user record of '%s', output incomplete.", ur->user_name);
70 }
71
72 break;
73
74 case OUTPUT_TABLE:
75 assert(table);
76
77 r = table_add_many(
78 table,
79 TABLE_STRING, ur->user_name,
80 TABLE_STRING, user_disposition_to_string(user_record_disposition(ur)),
81 TABLE_UID, ur->uid,
82 TABLE_GID, user_record_gid(ur),
83 TABLE_STRING, empty_to_null(ur->real_name),
84 TABLE_STRING, user_record_home_directory(ur),
85 TABLE_STRING, user_record_shell(ur),
86 TABLE_INT, (int) user_record_disposition(ur));
87 if (r < 0)
f987a261 88 return table_log_add_error(r);
1604937f
LP
89
90 break;
91
92 default:
93 assert_not_reached("Unexpected output mode");
94 }
95
96 return 0;
97}
98
99static int display_user(int argc, char *argv[], void *userdata) {
100 _cleanup_(table_unrefp) Table *table = NULL;
101 bool draw_separator = false;
102 int ret = 0, r;
103
104 if (arg_output < 0)
105 arg_output = argc > 1 ? OUTPUT_FRIENDLY : OUTPUT_TABLE;
106
107 if (arg_output == OUTPUT_TABLE) {
108 table = table_new("name", "disposition", "uid", "gid", "realname", "home", "shell", "disposition-numeric");
109 if (!table)
110 return log_oom();
111
112 (void) table_set_align_percent(table, table_get_cell(table, 0, 2), 100);
113 (void) table_set_align_percent(table, table_get_cell(table, 0, 3), 100);
114 (void) table_set_empty_string(table, "-");
ad5555b4
AP
115 (void) table_set_sort(table, (size_t) 7, (size_t) 2, (size_t) -1);
116 (void) table_set_display(table, (size_t) 0, (size_t) 1, (size_t) 2, (size_t) 3, (size_t) 4, (size_t) 5, (size_t) 6, (size_t) -1);
1604937f
LP
117 }
118
119 if (argc > 1) {
120 char **i;
121
122 STRV_FOREACH(i, argv + 1) {
123 _cleanup_(user_record_unrefp) UserRecord *ur = NULL;
124 uid_t uid;
125
126 if (parse_uid(*i, &uid) >= 0)
127 r = userdb_by_uid(uid, arg_userdb_flags, &ur);
128 else
129 r = userdb_by_name(*i, arg_userdb_flags, &ur);
130 if (r < 0) {
131 if (r == -ESRCH)
132 log_error_errno(r, "User %s does not exist.", *i);
133 else if (r == -EHOSTDOWN)
134 log_error_errno(r, "Selected user database service is not available for this request.");
135 else
136 log_error_errno(r, "Failed to find user %s: %m", *i);
137
138 if (ret >= 0)
139 ret = r;
140 } else {
141 if (draw_separator && arg_output == OUTPUT_FRIENDLY)
142 putchar('\n');
143
144 r = show_user(ur, table);
145 if (r < 0)
146 return r;
147
148 draw_separator = true;
149 }
150 }
151 } else {
152 _cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL;
153
154 r = userdb_all(arg_userdb_flags, &iterator);
155 if (r < 0)
156 return log_error_errno(r, "Failed to enumerate users: %m");
157
158 for (;;) {
159 _cleanup_(user_record_unrefp) UserRecord *ur = NULL;
160
161 r = userdb_iterator_get(iterator, &ur);
162 if (r == -ESRCH)
163 break;
164 if (r == -EHOSTDOWN)
165 return log_error_errno(r, "Selected user database service is not available for this request.");
166 if (r < 0)
167 return log_error_errno(r, "Failed acquire next user: %m");
168
169 if (draw_separator && arg_output == OUTPUT_FRIENDLY)
170 putchar('\n');
171
172 r = show_user(ur, table);
173 if (r < 0)
174 return r;
175
176 draw_separator = true;
177 }
178 }
179
180 if (table) {
181 r = table_print(table, NULL);
182 if (r < 0)
4b6607d9 183 return table_log_print_error(r);
1604937f
LP
184 }
185
186 return ret;
187}
188
189static int show_group(GroupRecord *gr, Table *table) {
190 int r;
191
192 assert(gr);
193
194 switch (arg_output) {
195
196 case OUTPUT_CLASSIC: {
197 _cleanup_free_ char *m = NULL;
198
199 if (!gid_is_valid(gr->gid))
200 break;
201
202 m = strv_join(gr->members, ",");
203 if (!m)
204 return log_oom();
205
206 printf("%s:x:" GID_FMT ":%s\n",
207 gr->group_name,
208 gr->gid,
209 m);
210 break;
211 }
212
213 case OUTPUT_JSON:
214 json_variant_dump(gr->json, JSON_FORMAT_COLOR_AUTO|JSON_FORMAT_PRETTY, NULL, 0);
215 break;
216
217 case OUTPUT_FRIENDLY:
218 group_record_show(gr, true);
219
220 if (gr->incomplete) {
221 fflush(stdout);
222 log_warning("Warning: lacking rights to acquire privileged fields of group record of '%s', output incomplete.", gr->group_name);
223 }
224
225 break;
226
227 case OUTPUT_TABLE:
228 assert(table);
229
230 r = table_add_many(
231 table,
232 TABLE_STRING, gr->group_name,
233 TABLE_STRING, user_disposition_to_string(group_record_disposition(gr)),
234 TABLE_GID, gr->gid,
235 TABLE_INT, (int) group_record_disposition(gr));
236 if (r < 0)
f987a261 237 return table_log_add_error(r);
1604937f
LP
238
239 break;
240
241 default:
162392b7 242 assert_not_reached("Unexpected display mode");
1604937f
LP
243 }
244
245 return 0;
246}
247
248
249static int display_group(int argc, char *argv[], void *userdata) {
250 _cleanup_(table_unrefp) Table *table = NULL;
251 bool draw_separator = false;
252 int ret = 0, r;
253
254 if (arg_output < 0)
255 arg_output = argc > 1 ? OUTPUT_FRIENDLY : OUTPUT_TABLE;
256
257 if (arg_output == OUTPUT_TABLE) {
258 table = table_new("name", "disposition", "gid", "disposition-numeric");
259 if (!table)
260 return log_oom();
261
262 (void) table_set_align_percent(table, table_get_cell(table, 0, 2), 100);
ad5555b4
AP
263 (void) table_set_sort(table, (size_t) 3, (size_t) 2, (size_t) -1);
264 (void) table_set_display(table, (size_t) 0, (size_t) 1, (size_t) 2, (size_t) -1);
1604937f
LP
265 }
266
267 if (argc > 1) {
268 char **i;
269
270 STRV_FOREACH(i, argv + 1) {
271 _cleanup_(group_record_unrefp) GroupRecord *gr = NULL;
272 gid_t gid;
273
274 if (parse_gid(*i, &gid) >= 0)
275 r = groupdb_by_gid(gid, arg_userdb_flags, &gr);
276 else
277 r = groupdb_by_name(*i, arg_userdb_flags, &gr);
278 if (r < 0) {
279 if (r == -ESRCH)
280 log_error_errno(r, "Group %s does not exist.", *i);
281 else if (r == -EHOSTDOWN)
282 log_error_errno(r, "Selected group database service is not available for this request.");
283 else
284 log_error_errno(r, "Failed to find group %s: %m", *i);
285
286 if (ret >= 0)
287 ret = r;
288 } else {
289 if (draw_separator && arg_output == OUTPUT_FRIENDLY)
290 putchar('\n');
291
292 r = show_group(gr, table);
293 if (r < 0)
294 return r;
295
296 draw_separator = true;
297 }
298 }
299
300 } else {
301 _cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL;
302
303 r = groupdb_all(arg_userdb_flags, &iterator);
304 if (r < 0)
305 return log_error_errno(r, "Failed to enumerate groups: %m");
306
307 for (;;) {
308 _cleanup_(group_record_unrefp) GroupRecord *gr = NULL;
309
310 r = groupdb_iterator_get(iterator, &gr);
311 if (r == -ESRCH)
312 break;
313 if (r == -EHOSTDOWN)
314 return log_error_errno(r, "Selected group database service is not available for this request.");
315 if (r < 0)
316 return log_error_errno(r, "Failed acquire next group: %m");
317
318 if (draw_separator && arg_output == OUTPUT_FRIENDLY)
319 putchar('\n');
320
321 r = show_group(gr, table);
322 if (r < 0)
323 return r;
324
325 draw_separator = true;
326 }
327
328 }
329
330 if (table) {
331 r = table_print(table, NULL);
332 if (r < 0)
4b6607d9 333 return table_log_print_error(r);
1604937f
LP
334 }
335
336 return ret;
337}
338
339static int show_membership(const char *user, const char *group, Table *table) {
340 int r;
341
342 assert(user);
343 assert(group);
344
345 switch (arg_output) {
346
347 case OUTPUT_CLASSIC:
348 /* Strictly speaking there's no 'classic' output for this concept, but let's output it in
349 * similar style to the classic output for user/group info */
350
351 printf("%s:%s\n", user, group);
352 break;
353
354 case OUTPUT_JSON: {
355 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
356
357 r = json_build(&v, JSON_BUILD_OBJECT(
358 JSON_BUILD_PAIR("user", JSON_BUILD_STRING(user)),
359 JSON_BUILD_PAIR("group", JSON_BUILD_STRING(group))));
360 if (r < 0)
361 return log_error_errno(r, "Failed to build JSON object: %m");
362
363 json_variant_dump(v, JSON_FORMAT_PRETTY|JSON_FORMAT_COLOR_AUTO, NULL, NULL);
364 break;
365 }
366
367 case OUTPUT_FRIENDLY:
368 /* Hmm, this is not particularly friendly, but not sure how we could do this better */
369 printf("%s: %s\n", group, user);
370 break;
371
372 case OUTPUT_TABLE:
373 assert(table);
374
375 r = table_add_many(
376 table,
377 TABLE_STRING, user,
378 TABLE_STRING, group);
379 if (r < 0)
f987a261 380 return table_log_add_error(r);
1604937f
LP
381
382 break;
383
384 default:
385 assert_not_reached("Unexpected output mode");
386 }
387
388 return 0;
389}
390
391static int display_memberships(int argc, char *argv[], void *userdata) {
392 _cleanup_(table_unrefp) Table *table = NULL;
393 int ret = 0, r;
394
395 if (arg_output < 0)
396 arg_output = OUTPUT_TABLE;
397
398 if (arg_output == OUTPUT_TABLE) {
399 table = table_new("user", "group");
400 if (!table)
401 return log_oom();
402
ad5555b4 403 (void) table_set_sort(table, (size_t) 0, (size_t) 1, (size_t) -1);
1604937f
LP
404 }
405
406 if (argc > 1) {
407 char **i;
408
409 STRV_FOREACH(i, argv + 1) {
410 _cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL;
411
412 if (streq(argv[0], "users-in-group")) {
413 r = membershipdb_by_group(*i, arg_userdb_flags, &iterator);
414 if (r < 0)
415 return log_error_errno(r, "Failed to enumerate users in group: %m");
416 } else if (streq(argv[0], "groups-of-user")) {
417 r = membershipdb_by_user(*i, arg_userdb_flags, &iterator);
418 if (r < 0)
419 return log_error_errno(r, "Failed to enumerate groups of user: %m");
420 } else
421 assert_not_reached("Unexpected verb");
422
423 for (;;) {
424 _cleanup_free_ char *user = NULL, *group = NULL;
425
426 r = membershipdb_iterator_get(iterator, &user, &group);
427 if (r == -ESRCH)
428 break;
429 if (r == -EHOSTDOWN)
430 return log_error_errno(r, "Selected membership database service is not available for this request.");
431 if (r < 0)
432 return log_error_errno(r, "Failed acquire next membership: %m");
433
434 r = show_membership(user, group, table);
435 if (r < 0)
436 return r;
437 }
438 }
439 } else {
440 _cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL;
441
442 r = membershipdb_all(arg_userdb_flags, &iterator);
443 if (r < 0)
444 return log_error_errno(r, "Failed to enumerate memberships: %m");
445
446 for (;;) {
447 _cleanup_free_ char *user = NULL, *group = NULL;
448
449 r = membershipdb_iterator_get(iterator, &user, &group);
450 if (r == -ESRCH)
451 break;
452 if (r == -EHOSTDOWN)
453 return log_error_errno(r, "Selected membership database service is not available for this request.");
454 if (r < 0)
455 return log_error_errno(r, "Failed acquire next membership: %m");
456
457 r = show_membership(user, group, table);
458 if (r < 0)
459 return r;
460 }
461 }
462
463 if (table) {
464 r = table_print(table, NULL);
465 if (r < 0)
4b6607d9 466 return table_log_print_error(r);
1604937f
LP
467 }
468
469 return ret;
470}
471
472static int display_services(int argc, char *argv[], void *userdata) {
473 _cleanup_(table_unrefp) Table *t = NULL;
474 _cleanup_(closedirp) DIR *d = NULL;
475 struct dirent *de;
476 int r;
477
478 d = opendir("/run/systemd/userdb/");
479 if (!d) {
480 if (errno == ENOENT) {
481 log_info("No services.");
482 return 0;
483 }
484
485 return log_error_errno(errno, "Failed to open /run/systemd/userdb/: %m");
486 }
487
488 t = table_new("service", "listening");
489 if (!t)
490 return log_oom();
491
ad5555b4 492 (void) table_set_sort(t, (size_t) 0, (size_t) -1);
1604937f
LP
493
494 FOREACH_DIRENT(de, d, return -errno) {
495 _cleanup_free_ char *j = NULL, *no = NULL;
496 union sockaddr_union sockaddr;
f36a9d59 497 socklen_t sockaddr_len;
1604937f
LP
498 _cleanup_close_ int fd = -1;
499
500 j = path_join("/run/systemd/userdb/", de->d_name);
501 if (!j)
502 return log_oom();
503
504 r = sockaddr_un_set_path(&sockaddr.un, j);
505 if (r < 0)
506 return log_error_errno(r, "Path %s does not fit in AF_UNIX socket address: %m", j);
f36a9d59 507 sockaddr_len = r;
1604937f
LP
508
509 fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
510 if (fd < 0)
511 return log_error_errno(r, "Failed to allocate AF_UNIX/SOCK_STREAM socket: %m");
512
f36a9d59 513 if (connect(fd, &sockaddr.un, sockaddr_len) < 0) {
1604937f
LP
514 no = strjoin("No (", errno_to_name(errno), ")");
515 if (!no)
516 return log_oom();
517 }
518
519 r = table_add_many(t,
520 TABLE_STRING, de->d_name,
521 TABLE_STRING, no ?: "yes",
522 TABLE_SET_COLOR, no ? ansi_highlight_red() : ansi_highlight_green());
523 if (r < 0)
f987a261 524 return table_log_add_error(r);
1604937f
LP
525 }
526
527 if (table_get_rows(t) <= 0) {
528 log_info("No services.");
529 return 0;
530 }
531
532 if (arg_output == OUTPUT_JSON)
533 table_print_json(t, NULL, JSON_FORMAT_PRETTY|JSON_FORMAT_COLOR_AUTO);
534 else
535 table_print(t, NULL);
536
537 return 0;
538}
539
540static int ssh_authorized_keys(int argc, char *argv[], void *userdata) {
541 _cleanup_(user_record_unrefp) UserRecord *ur = NULL;
542 int r;
543
1604937f
LP
544 r = userdb_by_name(argv[1], arg_userdb_flags, &ur);
545 if (r == -ESRCH)
2aea5883 546 return log_error_errno(r, "User %s does not exist.", argv[1]);
1604937f 547 else if (r == -EHOSTDOWN)
2aea5883
LP
548 return log_error_errno(r, "Selected user database service is not available for this request.");
549 else if (r == -EINVAL)
550 return log_error_errno(r, "Failed to find user %s: %m (Invalid user name?)", argv[1]);
1604937f 551 else if (r < 0)
2aea5883 552 return log_error_errno(r, "Failed to find user %s: %m", argv[1]);
1604937f
LP
553
554 if (strv_isempty(ur->ssh_authorized_keys))
555 log_debug("User record for %s has no public SSH keys.", argv[1]);
556 else {
557 char **i;
558
559 STRV_FOREACH(i, ur->ssh_authorized_keys)
560 printf("%s\n", *i);
561 }
562
563 if (ur->incomplete) {
564 fflush(stdout);
565 log_warning("Warning: lacking rights to acquire privileged fields of user record of '%s', output incomplete.", ur->user_name);
566 }
567
568 return EXIT_SUCCESS;
569}
570
571static int help(int argc, char *argv[], void *userdata) {
572 _cleanup_free_ char *link = NULL;
573 int r;
574
575 (void) pager_open(arg_pager_flags);
576
577 r = terminal_urlify_man("userdbctl", "1", &link);
578 if (r < 0)
579 return log_oom();
580
581 printf("%s [OPTIONS...] COMMAND ...\n\n"
582 "%sShow user and group information.%s\n"
583 "\nCommands:\n"
7009610f
ZJS
584 " user [USER…] Inspect user\n"
585 " group [GROUP…] Inspect group\n"
586 " users-in-group [GROUP…] Show users that are members of specified group(s)\n"
587 " groups-of-user [USER…] Show groups the specified user(s) is a member of\n"
588 " services Show enabled database services\n"
1604937f 589 "\nOptions:\n"
7009610f
ZJS
590 " -h --help Show this help\n"
591 " --version Show package version\n"
592 " --no-pager Do not pipe output into a pager\n"
593 " --no-legend Do not show the headers and footers\n"
594 " --output=MODE Select output mode (classic, friendly, table, json)\n"
595 " -j Equivalent to --output=json\n"
1604937f 596 " -s --service=SERVICE[:SERVICE…]\n"
7009610f
ZJS
597 " Query the specified service\n"
598 " --with-nss=BOOL Control whether to include glibc NSS data\n"
599 " -N Do not synthesize or include glibc NSS data\n"
600 " (Same as --synthesize=no --with-nss=no)\n"
601 " --synthesize=BOOL Synthesize root/nobody user\n"
1604937f
LP
602 "\nSee the %s for details.\n"
603 , program_invocation_short_name
604 , ansi_highlight(), ansi_normal()
605 , link
606 );
607
608 return 0;
609}
610
611static int parse_argv(int argc, char *argv[]) {
612
613 enum {
614 ARG_VERSION = 0x100,
615 ARG_NO_PAGER,
616 ARG_NO_LEGEND,
617 ARG_OUTPUT,
618 ARG_WITH_NSS,
619 ARG_SYNTHESIZE,
620 };
621
622 static const struct option options[] = {
623 { "help", no_argument, NULL, 'h' },
624 { "version", no_argument, NULL, ARG_VERSION },
625 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
626 { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
627 { "output", required_argument, NULL, ARG_OUTPUT },
628 { "service", required_argument, NULL, 's' },
629 { "with-nss", required_argument, NULL, ARG_WITH_NSS },
630 { "synthesize", required_argument, NULL, ARG_SYNTHESIZE },
631 {}
632 };
633
634 const char *e;
635 int r;
636
637 assert(argc >= 0);
638 assert(argv);
639
640 /* We are going to update this environment variable with our own, hence let's first read what is already set */
641 e = getenv("SYSTEMD_ONLY_USERDB");
642 if (e) {
643 char **l;
644
645 l = strv_split(e, ":");
646 if (!l)
647 return log_oom();
648
649 strv_free(arg_services);
650 arg_services = l;
651 }
652
653 for (;;) {
654 int c;
655
656 c = getopt_long(argc, argv, "hjs:N", options, NULL);
657 if (c < 0)
658 break;
659
660 switch (c) {
661
662 case 'h':
663 return help(0, NULL, NULL);
664
665 case ARG_VERSION:
666 return version();
667
668 case ARG_NO_PAGER:
669 arg_pager_flags |= PAGER_DISABLE;
670 break;
671
672 case ARG_NO_LEGEND:
673 arg_legend = false;
674 break;
675
676 case ARG_OUTPUT:
677 if (streq(optarg, "classic"))
678 arg_output = OUTPUT_CLASSIC;
679 else if (streq(optarg, "friendly"))
680 arg_output = OUTPUT_FRIENDLY;
681 else if (streq(optarg, "json"))
682 arg_output = OUTPUT_JSON;
683 else if (streq(optarg, "table"))
684 arg_output = OUTPUT_TABLE;
685 else if (streq(optarg, "help")) {
686 puts("classic\n"
687 "friendly\n"
688 "json");
689 return 0;
690 } else
691 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid --output= mode: %s", optarg);
692
693 break;
694
695 case 'j':
696 arg_output = OUTPUT_JSON;
697 break;
698
699 case 's':
700 if (isempty(optarg))
701 arg_services = strv_free(arg_services);
702 else {
7c7c4485 703 _cleanup_strv_free_ char **l = NULL;
1604937f
LP
704
705 l = strv_split(optarg, ":");
706 if (!l)
707 return log_oom();
708
709 r = strv_extend_strv(&arg_services, l, true);
710 if (r < 0)
711 return log_oom();
712 }
713
714 break;
715
716 case 'N':
717 arg_userdb_flags |= USERDB_AVOID_NSS|USERDB_DONT_SYNTHESIZE;
718 break;
719
720 case ARG_WITH_NSS:
721 r = parse_boolean(optarg);
722 if (r < 0)
723 return log_error_errno(r, "Failed to parse --with-nss= parameter: %s", optarg);
724
725 SET_FLAG(arg_userdb_flags, USERDB_AVOID_NSS, !r);
726 break;
727
728 case ARG_SYNTHESIZE:
729 r = parse_boolean(optarg);
730 if (r < 0)
731 return log_error_errno(r, "Failed to parse --synthesize= parameter: %s", optarg);
732
733 SET_FLAG(arg_userdb_flags, USERDB_DONT_SYNTHESIZE, !r);
734 break;
735
736 case '?':
737 return -EINVAL;
738
739 default:
740 assert_not_reached("Unhandled option");
741 }
742 }
743
744 return 1;
745}
746
747static int run(int argc, char *argv[]) {
748 static const Verb verbs[] = {
749 { "help", VERB_ANY, VERB_ANY, 0, help },
750 { "user", VERB_ANY, VERB_ANY, VERB_DEFAULT, display_user },
751 { "group", VERB_ANY, VERB_ANY, 0, display_group },
752 { "users-in-group", VERB_ANY, VERB_ANY, 0, display_memberships },
753 { "groups-of-user", VERB_ANY, VERB_ANY, 0, display_memberships },
754 { "services", VERB_ANY, 1, 0, display_services },
755
756 /* This one is a helper for sshd_config's AuthorizedKeysCommand= setting, it's not a
757 * user-facing verb and thus should not appear in man pages or --help texts. */
758 { "ssh-authorized-keys", 2, 2, 0, ssh_authorized_keys },
759 {}
760 };
761
762 int r;
763
41d1f469 764 log_setup_cli();
1604937f
LP
765
766 r = parse_argv(argc, argv);
767 if (r <= 0)
768 return r;
769
770 if (arg_services) {
771 _cleanup_free_ char *e = NULL;
772
773 e = strv_join(arg_services, ":");
774 if (!e)
775 return log_oom();
776
777 if (setenv("SYSTEMD_ONLY_USERDB", e, true) < 0)
778 return log_error_errno(r, "Failed to set $SYSTEMD_ONLY_USERDB: %m");
779
780 log_info("Enabled services: %s", e);
781 } else {
782 if (unsetenv("SYSTEMD_ONLY_USERDB") < 0)
783 return log_error_errno(r, "Failed to unset $SYSTEMD_ONLY_USERDB: %m");
784 }
785
786 return dispatch_verb(argc, argv, verbs, NULL);
787}
788
789DEFINE_MAIN_FUNCTION(run);