]>
Commit | Line | Data |
---|---|---|
a032b68d | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
46cdbd49 BR |
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" | |
46cdbd49 BR |
11 | #include "main-func.h" |
12 | #include "pager.h" | |
3a6ce677 | 13 | #include "parse-argument.h" |
46cdbd49 BR |
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 | ||
24 | static enum { | |
25 | OUTPUT_CLASSIC, | |
26 | OUTPUT_TABLE, | |
27 | OUTPUT_FRIENDLY, | |
28 | OUTPUT_JSON, | |
3a6ce677 | 29 | _OUTPUT_INVALID = -EINVAL, |
46cdbd49 BR |
30 | } arg_output = _OUTPUT_INVALID; |
31 | ||
32 | static PagerFlags arg_pager_flags = 0; | |
33 | static bool arg_legend = true; | |
34 | static char** arg_services = NULL; | |
35 | static UserDBFlags arg_userdb_flags = 0; | |
36 | ||
37 | STATIC_DESTRUCTOR_REGISTER(arg_services, strv_freep); | |
38 | ||
39 | static 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) | |
a10f5d05 | 88 | return table_log_add_error(r); |
46cdbd49 BR |
89 | |
90 | break; | |
91 | ||
92 | default: | |
93 | assert_not_reached("Unexpected output mode"); | |
94 | } | |
95 | ||
96 | return 0; | |
97 | } | |
98 | ||
99 | static 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, "-"); | |
3a6ce677 BR |
115 | (void) table_set_sort(table, (size_t) 7, (size_t) 2); |
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); | |
46cdbd49 BR |
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) | |
a10f5d05 | 183 | return table_log_print_error(r); |
46cdbd49 BR |
184 | } |
185 | ||
186 | return ret; | |
187 | } | |
188 | ||
189 | static 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, | |
a032b68d | 235 | TABLE_STRING, gr->description, |
46cdbd49 BR |
236 | TABLE_INT, (int) group_record_disposition(gr)); |
237 | if (r < 0) | |
a10f5d05 | 238 | return table_log_add_error(r); |
46cdbd49 BR |
239 | |
240 | break; | |
241 | ||
242 | default: | |
a10f5d05 | 243 | assert_not_reached("Unexpected display mode"); |
46cdbd49 BR |
244 | } |
245 | ||
246 | return 0; | |
247 | } | |
248 | ||
249 | ||
250 | static int display_group(int argc, char *argv[], void *userdata) { | |
251 | _cleanup_(table_unrefp) Table *table = NULL; | |
252 | bool draw_separator = false; | |
253 | int ret = 0, r; | |
254 | ||
255 | if (arg_output < 0) | |
256 | arg_output = argc > 1 ? OUTPUT_FRIENDLY : OUTPUT_TABLE; | |
257 | ||
258 | if (arg_output == OUTPUT_TABLE) { | |
a032b68d | 259 | table = table_new("name", "disposition", "gid", "description", "disposition-numeric"); |
46cdbd49 BR |
260 | if (!table) |
261 | return log_oom(); | |
262 | ||
263 | (void) table_set_align_percent(table, table_get_cell(table, 0, 2), 100); | |
a032b68d | 264 | (void) table_set_empty_string(table, "-"); |
3a6ce677 BR |
265 | (void) table_set_sort(table, (size_t) 3, (size_t) 2); |
266 | (void) table_set_display(table, (size_t) 0, (size_t) 1, (size_t) 2, (size_t) 3); | |
46cdbd49 BR |
267 | } |
268 | ||
269 | if (argc > 1) { | |
270 | char **i; | |
271 | ||
272 | STRV_FOREACH(i, argv + 1) { | |
273 | _cleanup_(group_record_unrefp) GroupRecord *gr = NULL; | |
274 | gid_t gid; | |
275 | ||
276 | if (parse_gid(*i, &gid) >= 0) | |
277 | r = groupdb_by_gid(gid, arg_userdb_flags, &gr); | |
278 | else | |
279 | r = groupdb_by_name(*i, arg_userdb_flags, &gr); | |
280 | if (r < 0) { | |
281 | if (r == -ESRCH) | |
282 | log_error_errno(r, "Group %s does not exist.", *i); | |
283 | else if (r == -EHOSTDOWN) | |
284 | log_error_errno(r, "Selected group database service is not available for this request."); | |
285 | else | |
286 | log_error_errno(r, "Failed to find group %s: %m", *i); | |
287 | ||
288 | if (ret >= 0) | |
289 | ret = r; | |
290 | } else { | |
291 | if (draw_separator && arg_output == OUTPUT_FRIENDLY) | |
292 | putchar('\n'); | |
293 | ||
294 | r = show_group(gr, table); | |
295 | if (r < 0) | |
296 | return r; | |
297 | ||
298 | draw_separator = true; | |
299 | } | |
300 | } | |
301 | ||
302 | } else { | |
303 | _cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL; | |
304 | ||
305 | r = groupdb_all(arg_userdb_flags, &iterator); | |
306 | if (r < 0) | |
307 | return log_error_errno(r, "Failed to enumerate groups: %m"); | |
308 | ||
309 | for (;;) { | |
310 | _cleanup_(group_record_unrefp) GroupRecord *gr = NULL; | |
311 | ||
312 | r = groupdb_iterator_get(iterator, &gr); | |
313 | if (r == -ESRCH) | |
314 | break; | |
315 | if (r == -EHOSTDOWN) | |
316 | return log_error_errno(r, "Selected group database service is not available for this request."); | |
317 | if (r < 0) | |
318 | return log_error_errno(r, "Failed acquire next group: %m"); | |
319 | ||
320 | if (draw_separator && arg_output == OUTPUT_FRIENDLY) | |
321 | putchar('\n'); | |
322 | ||
323 | r = show_group(gr, table); | |
324 | if (r < 0) | |
325 | return r; | |
326 | ||
327 | draw_separator = true; | |
328 | } | |
329 | ||
330 | } | |
331 | ||
332 | if (table) { | |
333 | r = table_print(table, NULL); | |
334 | if (r < 0) | |
a10f5d05 | 335 | return table_log_print_error(r); |
46cdbd49 BR |
336 | } |
337 | ||
338 | return ret; | |
339 | } | |
340 | ||
341 | static int show_membership(const char *user, const char *group, Table *table) { | |
342 | int r; | |
343 | ||
344 | assert(user); | |
345 | assert(group); | |
346 | ||
347 | switch (arg_output) { | |
348 | ||
349 | case OUTPUT_CLASSIC: | |
350 | /* Strictly speaking there's no 'classic' output for this concept, but let's output it in | |
351 | * similar style to the classic output for user/group info */ | |
352 | ||
353 | printf("%s:%s\n", user, group); | |
354 | break; | |
355 | ||
356 | case OUTPUT_JSON: { | |
357 | _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; | |
358 | ||
359 | r = json_build(&v, JSON_BUILD_OBJECT( | |
360 | JSON_BUILD_PAIR("user", JSON_BUILD_STRING(user)), | |
361 | JSON_BUILD_PAIR("group", JSON_BUILD_STRING(group)))); | |
362 | if (r < 0) | |
363 | return log_error_errno(r, "Failed to build JSON object: %m"); | |
364 | ||
365 | json_variant_dump(v, JSON_FORMAT_PRETTY|JSON_FORMAT_COLOR_AUTO, NULL, NULL); | |
366 | break; | |
367 | } | |
368 | ||
369 | case OUTPUT_FRIENDLY: | |
370 | /* Hmm, this is not particularly friendly, but not sure how we could do this better */ | |
371 | printf("%s: %s\n", group, user); | |
372 | break; | |
373 | ||
374 | case OUTPUT_TABLE: | |
375 | assert(table); | |
376 | ||
377 | r = table_add_many( | |
378 | table, | |
379 | TABLE_STRING, user, | |
380 | TABLE_STRING, group); | |
381 | if (r < 0) | |
a10f5d05 | 382 | return table_log_add_error(r); |
46cdbd49 BR |
383 | |
384 | break; | |
385 | ||
386 | default: | |
387 | assert_not_reached("Unexpected output mode"); | |
388 | } | |
389 | ||
390 | return 0; | |
391 | } | |
392 | ||
393 | static int display_memberships(int argc, char *argv[], void *userdata) { | |
394 | _cleanup_(table_unrefp) Table *table = NULL; | |
395 | int ret = 0, r; | |
396 | ||
397 | if (arg_output < 0) | |
398 | arg_output = OUTPUT_TABLE; | |
399 | ||
400 | if (arg_output == OUTPUT_TABLE) { | |
401 | table = table_new("user", "group"); | |
402 | if (!table) | |
403 | return log_oom(); | |
404 | ||
3a6ce677 | 405 | (void) table_set_sort(table, (size_t) 0, (size_t) 1); |
46cdbd49 BR |
406 | } |
407 | ||
408 | if (argc > 1) { | |
409 | char **i; | |
410 | ||
411 | STRV_FOREACH(i, argv + 1) { | |
412 | _cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL; | |
413 | ||
414 | if (streq(argv[0], "users-in-group")) { | |
415 | r = membershipdb_by_group(*i, arg_userdb_flags, &iterator); | |
416 | if (r < 0) | |
417 | return log_error_errno(r, "Failed to enumerate users in group: %m"); | |
418 | } else if (streq(argv[0], "groups-of-user")) { | |
419 | r = membershipdb_by_user(*i, arg_userdb_flags, &iterator); | |
420 | if (r < 0) | |
421 | return log_error_errno(r, "Failed to enumerate groups of user: %m"); | |
422 | } else | |
423 | assert_not_reached("Unexpected verb"); | |
424 | ||
425 | for (;;) { | |
426 | _cleanup_free_ char *user = NULL, *group = NULL; | |
427 | ||
428 | r = membershipdb_iterator_get(iterator, &user, &group); | |
429 | if (r == -ESRCH) | |
430 | break; | |
431 | if (r == -EHOSTDOWN) | |
432 | return log_error_errno(r, "Selected membership database service is not available for this request."); | |
433 | if (r < 0) | |
434 | return log_error_errno(r, "Failed acquire next membership: %m"); | |
435 | ||
436 | r = show_membership(user, group, table); | |
437 | if (r < 0) | |
438 | return r; | |
439 | } | |
440 | } | |
441 | } else { | |
442 | _cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL; | |
443 | ||
444 | r = membershipdb_all(arg_userdb_flags, &iterator); | |
445 | if (r < 0) | |
446 | return log_error_errno(r, "Failed to enumerate memberships: %m"); | |
447 | ||
448 | for (;;) { | |
449 | _cleanup_free_ char *user = NULL, *group = NULL; | |
450 | ||
451 | r = membershipdb_iterator_get(iterator, &user, &group); | |
452 | if (r == -ESRCH) | |
453 | break; | |
454 | if (r == -EHOSTDOWN) | |
455 | return log_error_errno(r, "Selected membership database service is not available for this request."); | |
456 | if (r < 0) | |
457 | return log_error_errno(r, "Failed acquire next membership: %m"); | |
458 | ||
459 | r = show_membership(user, group, table); | |
460 | if (r < 0) | |
461 | return r; | |
462 | } | |
463 | } | |
464 | ||
465 | if (table) { | |
466 | r = table_print(table, NULL); | |
467 | if (r < 0) | |
a10f5d05 | 468 | return table_log_print_error(r); |
46cdbd49 BR |
469 | } |
470 | ||
471 | return ret; | |
472 | } | |
473 | ||
474 | static int display_services(int argc, char *argv[], void *userdata) { | |
475 | _cleanup_(table_unrefp) Table *t = NULL; | |
476 | _cleanup_(closedirp) DIR *d = NULL; | |
477 | struct dirent *de; | |
478 | int r; | |
479 | ||
480 | d = opendir("/run/systemd/userdb/"); | |
481 | if (!d) { | |
482 | if (errno == ENOENT) { | |
483 | log_info("No services."); | |
484 | return 0; | |
485 | } | |
486 | ||
487 | return log_error_errno(errno, "Failed to open /run/systemd/userdb/: %m"); | |
488 | } | |
489 | ||
490 | t = table_new("service", "listening"); | |
491 | if (!t) | |
492 | return log_oom(); | |
493 | ||
3a6ce677 | 494 | (void) table_set_sort(t, (size_t) 0); |
46cdbd49 BR |
495 | |
496 | FOREACH_DIRENT(de, d, return -errno) { | |
497 | _cleanup_free_ char *j = NULL, *no = NULL; | |
498 | union sockaddr_union sockaddr; | |
499 | socklen_t sockaddr_len; | |
500 | _cleanup_close_ int fd = -1; | |
501 | ||
502 | j = path_join("/run/systemd/userdb/", de->d_name); | |
503 | if (!j) | |
504 | return log_oom(); | |
505 | ||
506 | r = sockaddr_un_set_path(&sockaddr.un, j); | |
507 | if (r < 0) | |
508 | return log_error_errno(r, "Path %s does not fit in AF_UNIX socket address: %m", j); | |
509 | sockaddr_len = r; | |
510 | ||
511 | fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); | |
512 | if (fd < 0) | |
513 | return log_error_errno(r, "Failed to allocate AF_UNIX/SOCK_STREAM socket: %m"); | |
514 | ||
515 | if (connect(fd, &sockaddr.un, sockaddr_len) < 0) { | |
516 | no = strjoin("No (", errno_to_name(errno), ")"); | |
517 | if (!no) | |
518 | return log_oom(); | |
519 | } | |
520 | ||
521 | r = table_add_many(t, | |
522 | TABLE_STRING, de->d_name, | |
523 | TABLE_STRING, no ?: "yes", | |
524 | TABLE_SET_COLOR, no ? ansi_highlight_red() : ansi_highlight_green()); | |
525 | if (r < 0) | |
a10f5d05 | 526 | return table_log_add_error(r); |
46cdbd49 BR |
527 | } |
528 | ||
529 | if (table_get_rows(t) <= 0) { | |
530 | log_info("No services."); | |
531 | return 0; | |
532 | } | |
533 | ||
534 | if (arg_output == OUTPUT_JSON) | |
535 | table_print_json(t, NULL, JSON_FORMAT_PRETTY|JSON_FORMAT_COLOR_AUTO); | |
536 | else | |
537 | table_print(t, NULL); | |
538 | ||
539 | return 0; | |
540 | } | |
541 | ||
542 | static int ssh_authorized_keys(int argc, char *argv[], void *userdata) { | |
543 | _cleanup_(user_record_unrefp) UserRecord *ur = NULL; | |
544 | int r; | |
545 | ||
46cdbd49 BR |
546 | r = userdb_by_name(argv[1], arg_userdb_flags, &ur); |
547 | if (r == -ESRCH) | |
d0648cfe | 548 | return log_error_errno(r, "User %s does not exist.", argv[1]); |
46cdbd49 | 549 | else if (r == -EHOSTDOWN) |
d0648cfe MB |
550 | return log_error_errno(r, "Selected user database service is not available for this request."); |
551 | else if (r == -EINVAL) | |
552 | return log_error_errno(r, "Failed to find user %s: %m (Invalid user name?)", argv[1]); | |
46cdbd49 | 553 | else if (r < 0) |
d0648cfe | 554 | return log_error_errno(r, "Failed to find user %s: %m", argv[1]); |
46cdbd49 BR |
555 | |
556 | if (strv_isempty(ur->ssh_authorized_keys)) | |
557 | log_debug("User record for %s has no public SSH keys.", argv[1]); | |
558 | else { | |
559 | char **i; | |
560 | ||
561 | STRV_FOREACH(i, ur->ssh_authorized_keys) | |
562 | printf("%s\n", *i); | |
563 | } | |
564 | ||
565 | if (ur->incomplete) { | |
566 | fflush(stdout); | |
567 | log_warning("Warning: lacking rights to acquire privileged fields of user record of '%s', output incomplete.", ur->user_name); | |
568 | } | |
569 | ||
570 | return EXIT_SUCCESS; | |
571 | } | |
572 | ||
573 | static int help(int argc, char *argv[], void *userdata) { | |
574 | _cleanup_free_ char *link = NULL; | |
575 | int r; | |
576 | ||
577 | (void) pager_open(arg_pager_flags); | |
578 | ||
579 | r = terminal_urlify_man("userdbctl", "1", &link); | |
580 | if (r < 0) | |
581 | return log_oom(); | |
582 | ||
583 | printf("%s [OPTIONS...] COMMAND ...\n\n" | |
584 | "%sShow user and group information.%s\n" | |
585 | "\nCommands:\n" | |
20a6e51f MB |
586 | " user [USER…] Inspect user\n" |
587 | " group [GROUP…] Inspect group\n" | |
588 | " users-in-group [GROUP…] Show users that are members of specified group(s)\n" | |
589 | " groups-of-user [USER…] Show groups the specified user(s) is a member of\n" | |
590 | " services Show enabled database services\n" | |
46cdbd49 | 591 | "\nOptions:\n" |
20a6e51f MB |
592 | " -h --help Show this help\n" |
593 | " --version Show package version\n" | |
594 | " --no-pager Do not pipe output into a pager\n" | |
595 | " --no-legend Do not show the headers and footers\n" | |
596 | " --output=MODE Select output mode (classic, friendly, table, json)\n" | |
597 | " -j Equivalent to --output=json\n" | |
46cdbd49 | 598 | " -s --service=SERVICE[:SERVICE…]\n" |
20a6e51f MB |
599 | " Query the specified service\n" |
600 | " --with-nss=BOOL Control whether to include glibc NSS data\n" | |
601 | " -N Do not synthesize or include glibc NSS data\n" | |
602 | " (Same as --synthesize=no --with-nss=no)\n" | |
603 | " --synthesize=BOOL Synthesize root/nobody user\n" | |
8b3d4ff0 MB |
604 | " --with-dropin=BOOL Control whether to include drop-in records\n" |
605 | " --with-varlink=BOOL Control whether to talk to services at all\n" | |
3a6ce677 BR |
606 | "\nSee the %s for details.\n", |
607 | program_invocation_short_name, | |
608 | ansi_highlight(), | |
609 | ansi_normal(), | |
610 | link); | |
46cdbd49 BR |
611 | |
612 | return 0; | |
613 | } | |
614 | ||
615 | static int parse_argv(int argc, char *argv[]) { | |
616 | ||
617 | enum { | |
618 | ARG_VERSION = 0x100, | |
619 | ARG_NO_PAGER, | |
620 | ARG_NO_LEGEND, | |
621 | ARG_OUTPUT, | |
622 | ARG_WITH_NSS, | |
8b3d4ff0 MB |
623 | ARG_WITH_DROPIN, |
624 | ARG_WITH_VARLINK, | |
46cdbd49 BR |
625 | ARG_SYNTHESIZE, |
626 | }; | |
627 | ||
628 | static const struct option options[] = { | |
8b3d4ff0 MB |
629 | { "help", no_argument, NULL, 'h' }, |
630 | { "version", no_argument, NULL, ARG_VERSION }, | |
631 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, | |
632 | { "no-legend", no_argument, NULL, ARG_NO_LEGEND }, | |
633 | { "output", required_argument, NULL, ARG_OUTPUT }, | |
634 | { "service", required_argument, NULL, 's' }, | |
635 | { "with-nss", required_argument, NULL, ARG_WITH_NSS }, | |
636 | { "with-dropin", required_argument, NULL, ARG_WITH_DROPIN }, | |
637 | { "with-varlink", required_argument, NULL, ARG_WITH_VARLINK }, | |
638 | { "synthesize", required_argument, NULL, ARG_SYNTHESIZE }, | |
46cdbd49 BR |
639 | {} |
640 | }; | |
641 | ||
642 | const char *e; | |
643 | int r; | |
644 | ||
645 | assert(argc >= 0); | |
646 | assert(argv); | |
647 | ||
648 | /* We are going to update this environment variable with our own, hence let's first read what is already set */ | |
649 | e = getenv("SYSTEMD_ONLY_USERDB"); | |
650 | if (e) { | |
651 | char **l; | |
652 | ||
653 | l = strv_split(e, ":"); | |
654 | if (!l) | |
655 | return log_oom(); | |
656 | ||
657 | strv_free(arg_services); | |
658 | arg_services = l; | |
659 | } | |
660 | ||
661 | for (;;) { | |
662 | int c; | |
663 | ||
664 | c = getopt_long(argc, argv, "hjs:N", options, NULL); | |
665 | if (c < 0) | |
666 | break; | |
667 | ||
668 | switch (c) { | |
669 | ||
670 | case 'h': | |
671 | return help(0, NULL, NULL); | |
672 | ||
673 | case ARG_VERSION: | |
674 | return version(); | |
675 | ||
676 | case ARG_NO_PAGER: | |
677 | arg_pager_flags |= PAGER_DISABLE; | |
678 | break; | |
679 | ||
680 | case ARG_NO_LEGEND: | |
681 | arg_legend = false; | |
682 | break; | |
683 | ||
684 | case ARG_OUTPUT: | |
685 | if (streq(optarg, "classic")) | |
686 | arg_output = OUTPUT_CLASSIC; | |
687 | else if (streq(optarg, "friendly")) | |
688 | arg_output = OUTPUT_FRIENDLY; | |
689 | else if (streq(optarg, "json")) | |
690 | arg_output = OUTPUT_JSON; | |
691 | else if (streq(optarg, "table")) | |
692 | arg_output = OUTPUT_TABLE; | |
693 | else if (streq(optarg, "help")) { | |
694 | puts("classic\n" | |
695 | "friendly\n" | |
129ef395 MB |
696 | "json\n" |
697 | "table"); | |
46cdbd49 BR |
698 | return 0; |
699 | } else | |
700 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid --output= mode: %s", optarg); | |
701 | ||
702 | break; | |
703 | ||
704 | case 'j': | |
705 | arg_output = OUTPUT_JSON; | |
706 | break; | |
707 | ||
708 | case 's': | |
709 | if (isempty(optarg)) | |
710 | arg_services = strv_free(arg_services); | |
711 | else { | |
712 | _cleanup_strv_free_ char **l = NULL; | |
713 | ||
714 | l = strv_split(optarg, ":"); | |
715 | if (!l) | |
716 | return log_oom(); | |
717 | ||
718 | r = strv_extend_strv(&arg_services, l, true); | |
719 | if (r < 0) | |
720 | return log_oom(); | |
721 | } | |
722 | ||
723 | break; | |
724 | ||
725 | case 'N': | |
8b3d4ff0 | 726 | arg_userdb_flags |= USERDB_EXCLUDE_NSS|USERDB_DONT_SYNTHESIZE; |
46cdbd49 BR |
727 | break; |
728 | ||
729 | case ARG_WITH_NSS: | |
3a6ce677 | 730 | r = parse_boolean_argument("--with-nss=", optarg, NULL); |
46cdbd49 | 731 | if (r < 0) |
3a6ce677 | 732 | return r; |
46cdbd49 | 733 | |
8b3d4ff0 MB |
734 | SET_FLAG(arg_userdb_flags, USERDB_EXCLUDE_NSS, !r); |
735 | break; | |
736 | ||
737 | case ARG_WITH_DROPIN: | |
738 | r = parse_boolean_argument("--with-dropin=", optarg, NULL); | |
739 | if (r < 0) | |
740 | return r; | |
741 | ||
742 | SET_FLAG(arg_userdb_flags, USERDB_EXCLUDE_DROPIN, !r); | |
743 | break; | |
744 | ||
745 | case ARG_WITH_VARLINK: | |
746 | r = parse_boolean_argument("--with-varlink=", optarg, NULL); | |
747 | if (r < 0) | |
748 | return r; | |
749 | ||
750 | SET_FLAG(arg_userdb_flags, USERDB_EXCLUDE_VARLINK, !r); | |
46cdbd49 BR |
751 | break; |
752 | ||
753 | case ARG_SYNTHESIZE: | |
3a6ce677 | 754 | r = parse_boolean_argument("--synthesize=", optarg, NULL); |
46cdbd49 | 755 | if (r < 0) |
3a6ce677 | 756 | return r; |
46cdbd49 BR |
757 | |
758 | SET_FLAG(arg_userdb_flags, USERDB_DONT_SYNTHESIZE, !r); | |
759 | break; | |
760 | ||
761 | case '?': | |
762 | return -EINVAL; | |
763 | ||
764 | default: | |
765 | assert_not_reached("Unhandled option"); | |
766 | } | |
767 | } | |
768 | ||
769 | return 1; | |
770 | } | |
771 | ||
772 | static int run(int argc, char *argv[]) { | |
773 | static const Verb verbs[] = { | |
774 | { "help", VERB_ANY, VERB_ANY, 0, help }, | |
775 | { "user", VERB_ANY, VERB_ANY, VERB_DEFAULT, display_user }, | |
776 | { "group", VERB_ANY, VERB_ANY, 0, display_group }, | |
777 | { "users-in-group", VERB_ANY, VERB_ANY, 0, display_memberships }, | |
778 | { "groups-of-user", VERB_ANY, VERB_ANY, 0, display_memberships }, | |
779 | { "services", VERB_ANY, 1, 0, display_services }, | |
780 | ||
781 | /* This one is a helper for sshd_config's AuthorizedKeysCommand= setting, it's not a | |
782 | * user-facing verb and thus should not appear in man pages or --help texts. */ | |
783 | { "ssh-authorized-keys", 2, 2, 0, ssh_authorized_keys }, | |
784 | {} | |
785 | }; | |
786 | ||
787 | int r; | |
788 | ||
3a6ce677 | 789 | log_setup(); |
46cdbd49 BR |
790 | |
791 | r = parse_argv(argc, argv); | |
792 | if (r <= 0) | |
793 | return r; | |
794 | ||
795 | if (arg_services) { | |
796 | _cleanup_free_ char *e = NULL; | |
797 | ||
798 | e = strv_join(arg_services, ":"); | |
799 | if (!e) | |
800 | return log_oom(); | |
801 | ||
802 | if (setenv("SYSTEMD_ONLY_USERDB", e, true) < 0) | |
803 | return log_error_errno(r, "Failed to set $SYSTEMD_ONLY_USERDB: %m"); | |
804 | ||
805 | log_info("Enabled services: %s", e); | |
a032b68d MB |
806 | } else |
807 | assert_se(unsetenv("SYSTEMD_ONLY_USERDB") == 0); | |
46cdbd49 BR |
808 | |
809 | return dispatch_verb(argc, argv, verbs, NULL); | |
810 | } | |
811 | ||
812 | DEFINE_MAIN_FUNCTION(run); |