| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <stdlib.h> |
| 4 |
#include <unistd.h> |
| 5 |
|
| 6 |
#include "alloc-util.h" |
| 7 |
#include "ansi-color.h" |
| 8 |
#include "bitfield.h" |
| 9 |
#include "build.h" |
| 10 |
#include "copy.h" |
| 11 |
#include "creds-util.h" |
| 12 |
#include "dirent-util.h" |
| 13 |
#include "errno-list.h" |
| 14 |
#include "errno-util.h" |
| 15 |
#include "escape.h" |
| 16 |
#include "fd-util.h" |
| 17 |
#include "fileio.h" |
| 18 |
#include "format-table.h" |
| 19 |
#include "format-util.h" |
| 20 |
#include "fs-util.h" |
| 21 |
#include "glyph-util.h" |
| 22 |
#include "help-util.h" |
| 23 |
#include "io-util.h" |
| 24 |
#include "json-util.h" |
| 25 |
#include "log.h" |
| 26 |
#include "main-func.h" |
| 27 |
#include "mkdir.h" |
| 28 |
#include "options.h" |
| 29 |
#include "pager.h" |
| 30 |
#include "parse-argument.h" |
| 31 |
#include "recurse-dir.h" |
| 32 |
#include "socket-util.h" |
| 33 |
#include "string-table.h" |
| 34 |
#include "string-util.h" |
| 35 |
#include "strv.h" |
| 36 |
#include "uid-classification.h" |
| 37 |
#include "uid-range.h" |
| 38 |
#include "umask-util.h" |
| 39 |
#include "user-record-show.h" |
| 40 |
#include "user-util.h" |
| 41 |
#include "userdb.h" |
| 42 |
#include "verbs.h" |
| 43 |
#include "virt.h" |
| 44 |
|
| 45 |
typedef enum { |
| 46 |
OUTPUT_CLASSIC, |
| 47 |
OUTPUT_TABLE, |
| 48 |
OUTPUT_FRIENDLY, |
| 49 |
OUTPUT_JSON, |
| 50 |
_OUTPUT_MAX, |
| 51 |
_OUTPUT_INVALID = -EINVAL, |
| 52 |
} Output; |
| 53 |
|
| 54 |
static Output arg_output = _OUTPUT_INVALID; |
| 55 |
static PagerFlags arg_pager_flags = 0; |
| 56 |
static bool arg_legend = true; |
| 57 |
static char** arg_services = NULL; |
| 58 |
static UserDBFlags arg_userdb_flags = 0; |
| 59 |
static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF; |
| 60 |
static bool arg_chain = false; |
| 61 |
static uint64_t arg_disposition_mask = UINT64_MAX; |
| 62 |
static uid_t arg_uid_min = 0; |
| 63 |
static uid_t arg_uid_max = UID_INVALID-1; |
| 64 |
static sd_id128_t arg_uuid = SD_ID128_NULL; |
| 65 |
static bool arg_fuzzy = false; |
| 66 |
static bool arg_boundaries = true; |
| 67 |
static sd_json_variant *arg_from_file = NULL; |
| 68 |
|
| 69 |
STATIC_DESTRUCTOR_REGISTER(arg_services, strv_freep); |
| 70 |
STATIC_DESTRUCTOR_REGISTER(arg_from_file, sd_json_variant_unrefp); |
| 71 |
|
| 72 |
static const char *output_table[_OUTPUT_MAX] = { |
| 73 |
[OUTPUT_CLASSIC] = "classic", |
| 74 |
[OUTPUT_TABLE] = "table", |
| 75 |
[OUTPUT_FRIENDLY] = "friendly", |
| 76 |
[OUTPUT_JSON] = "json", |
| 77 |
}; |
| 78 |
|
| 79 |
DEFINE_PRIVATE_STRING_TABLE_LOOKUP(output, Output); |
| 80 |
|
| 81 |
static const char *user_disposition_to_color(UserDisposition d) { |
| 82 |
assert(d >= 0); |
| 83 |
assert(d < _USER_DISPOSITION_MAX); |
| 84 |
|
| 85 |
switch (d) { |
| 86 |
case USER_INTRINSIC: |
| 87 |
return ansi_red(); |
| 88 |
|
| 89 |
case USER_SYSTEM: |
| 90 |
case USER_DYNAMIC: |
| 91 |
return ansi_green(); |
| 92 |
|
| 93 |
case USER_CONTAINER: |
| 94 |
case USER_FOREIGN: |
| 95 |
return ansi_cyan(); |
| 96 |
|
| 97 |
case USER_RESERVED: |
| 98 |
return ansi_red(); |
| 99 |
|
| 100 |
default: |
| 101 |
return NULL; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
static const char* shell_to_color(const char *shell) { |
| 106 |
return !shell || is_nologin_shell(shell) ? ansi_grey() : NULL; |
| 107 |
} |
| 108 |
|
| 109 |
static int show_user(UserRecord *ur, Table *table) { |
| 110 |
int r; |
| 111 |
|
| 112 |
assert(ur); |
| 113 |
|
| 114 |
switch (arg_output) { |
| 115 |
|
| 116 |
case OUTPUT_CLASSIC: |
| 117 |
if (!uid_is_valid(ur->uid)) |
| 118 |
break; |
| 119 |
|
| 120 |
printf("%s:x:" UID_FMT ":" GID_FMT ":%s:%s:%s\n", |
| 121 |
ur->user_name, |
| 122 |
ur->uid, |
| 123 |
user_record_gid(ur), |
| 124 |
strempty(user_record_real_name(ur)), |
| 125 |
user_record_home_directory(ur), |
| 126 |
user_record_shell(ur)); |
| 127 |
|
| 128 |
break; |
| 129 |
|
| 130 |
case OUTPUT_JSON: |
| 131 |
sd_json_variant_dump(ur->json, arg_json_format_flags, NULL, NULL); |
| 132 |
break; |
| 133 |
|
| 134 |
case OUTPUT_FRIENDLY: |
| 135 |
user_record_show(ur, true); |
| 136 |
|
| 137 |
if (ur->incomplete) { |
| 138 |
fflush(stdout); |
| 139 |
log_warning("Warning: lacking rights to acquire privileged fields of user record of '%s', output incomplete.", ur->user_name); |
| 140 |
} |
| 141 |
|
| 142 |
break; |
| 143 |
|
| 144 |
case OUTPUT_TABLE: { |
| 145 |
assert(table); |
| 146 |
UserDisposition d = user_record_disposition(ur); |
| 147 |
const char *sh = user_record_shell(ur); |
| 148 |
|
| 149 |
r = table_add_many( |
| 150 |
table, |
| 151 |
TABLE_STRING, "", |
| 152 |
TABLE_STRING, ur->user_name, |
| 153 |
TABLE_SET_COLOR, user_disposition_to_color(d), |
| 154 |
TABLE_STRING, user_disposition_to_string(d), |
| 155 |
TABLE_UID, ur->uid, |
| 156 |
TABLE_GID, user_record_gid(ur), |
| 157 |
TABLE_STRING, empty_to_null(ur->real_name), |
| 158 |
TABLE_PATH, user_record_home_directory(ur), |
| 159 |
TABLE_PATH, sh, |
| 160 |
TABLE_SET_COLOR, shell_to_color(sh), |
| 161 |
TABLE_INT, 0); |
| 162 |
if (r < 0) |
| 163 |
return table_log_add_error(r); |
| 164 |
|
| 165 |
break; |
| 166 |
} |
| 167 |
|
| 168 |
default: |
| 169 |
assert_not_reached(); |
| 170 |
} |
| 171 |
|
| 172 |
return 0; |
| 173 |
} |
| 174 |
|
| 175 |
static bool test_show_mapped(void) { |
| 176 |
/* Show mapped user range only in environments where user mapping is a thing. */ |
| 177 |
return running_in_userns() > 0; |
| 178 |
} |
| 179 |
|
| 180 |
static const struct { |
| 181 |
uid_t first, last; |
| 182 |
const char *name; |
| 183 |
UserDisposition disposition; |
| 184 |
bool (*test)(void); |
| 185 |
} uid_range_table[] = { |
| 186 |
{ |
| 187 |
.first = 1, |
| 188 |
.last = SYSTEM_UID_MAX, |
| 189 |
.name = "system", |
| 190 |
.disposition = USER_SYSTEM, |
| 191 |
}, |
| 192 |
{ |
| 193 |
.first = GREETER_UID_MIN, |
| 194 |
.last = GREETER_UID_MAX, |
| 195 |
.name = "dynamic greeter", |
| 196 |
.disposition = USER_DYNAMIC, |
| 197 |
}, |
| 198 |
{ |
| 199 |
.first = DYNAMIC_UID_MIN, |
| 200 |
.last = DYNAMIC_UID_MAX, |
| 201 |
.name = "dynamic system", |
| 202 |
.disposition = USER_DYNAMIC, |
| 203 |
}, |
| 204 |
{ |
| 205 |
.first = CONTAINER_UID_MIN, |
| 206 |
.last = CONTAINER_UID_MAX, |
| 207 |
.name = "container", |
| 208 |
.disposition = USER_CONTAINER, |
| 209 |
}, |
| 210 |
{ |
| 211 |
.first = FOREIGN_UID_MIN, |
| 212 |
.last = FOREIGN_UID_MAX, |
| 213 |
.name = "foreign", |
| 214 |
.disposition = USER_FOREIGN, |
| 215 |
}, |
| 216 |
#if ENABLE_HOMED |
| 217 |
{ |
| 218 |
.first = HOME_UID_MIN, |
| 219 |
.last = HOME_UID_MAX, |
| 220 |
.name = "systemd-homed", |
| 221 |
.disposition = USER_REGULAR, |
| 222 |
}, |
| 223 |
#endif |
| 224 |
{ |
| 225 |
.first = MAP_UID_MIN, |
| 226 |
.last = MAP_UID_MAX, |
| 227 |
.name = "mapped", |
| 228 |
.disposition = USER_REGULAR, |
| 229 |
.test = test_show_mapped, |
| 230 |
}, |
| 231 |
}; |
| 232 |
|
| 233 |
static int table_add_uid_boundaries(Table *table, const UIDRange *p) { |
| 234 |
int r, n_added = 0; |
| 235 |
|
| 236 |
assert(table); |
| 237 |
|
| 238 |
FOREACH_ELEMENT(i, uid_range_table) { |
| 239 |
_cleanup_free_ char *name = NULL, *comment = NULL; |
| 240 |
|
| 241 |
if (!BIT_SET(arg_disposition_mask, i->disposition)) |
| 242 |
continue; |
| 243 |
|
| 244 |
if (!uid_range_covers(p, i->first, i->last - i->first + 1)) |
| 245 |
continue; |
| 246 |
|
| 247 |
if (i->test && !i->test()) |
| 248 |
continue; |
| 249 |
|
| 250 |
name = strjoin(glyph(GLYPH_ARROW_DOWN), |
| 251 |
" begin ", i->name, " users ", |
| 252 |
glyph(GLYPH_ARROW_DOWN)); |
| 253 |
if (!name) |
| 254 |
return log_oom(); |
| 255 |
|
| 256 |
comment = strjoin("First ", i->name, " user"); |
| 257 |
if (!comment) |
| 258 |
return log_oom(); |
| 259 |
|
| 260 |
r = table_add_many( |
| 261 |
table, |
| 262 |
TABLE_STRING, glyph(GLYPH_TREE_TOP), |
| 263 |
TABLE_STRING, name, |
| 264 |
TABLE_SET_COLOR, ansi_grey(), |
| 265 |
TABLE_STRING, user_disposition_to_string(i->disposition), |
| 266 |
TABLE_SET_COLOR, ansi_grey(), |
| 267 |
TABLE_UID, i->first, |
| 268 |
TABLE_SET_COLOR, ansi_grey(), |
| 269 |
TABLE_EMPTY, |
| 270 |
TABLE_STRING, comment, |
| 271 |
TABLE_SET_COLOR, ansi_grey(), |
| 272 |
TABLE_EMPTY, |
| 273 |
TABLE_EMPTY, |
| 274 |
TABLE_INT, -1); /* sort before any other entry with the same UID */ |
| 275 |
if (r < 0) |
| 276 |
return table_log_add_error(r); |
| 277 |
|
| 278 |
free(name); |
| 279 |
name = strjoin(glyph(GLYPH_ARROW_UP), |
| 280 |
" end ", i->name, " users ", |
| 281 |
glyph(GLYPH_ARROW_UP)); |
| 282 |
if (!name) |
| 283 |
return log_oom(); |
| 284 |
|
| 285 |
free(comment); |
| 286 |
comment = strjoin("Last ", i->name, " user"); |
| 287 |
if (!comment) |
| 288 |
return log_oom(); |
| 289 |
|
| 290 |
r = table_add_many( |
| 291 |
table, |
| 292 |
TABLE_STRING, glyph(GLYPH_TREE_RIGHT), |
| 293 |
TABLE_STRING, name, |
| 294 |
TABLE_SET_COLOR, ansi_grey(), |
| 295 |
TABLE_STRING, user_disposition_to_string(i->disposition), |
| 296 |
TABLE_SET_COLOR, ansi_grey(), |
| 297 |
TABLE_UID, i->last, |
| 298 |
TABLE_SET_COLOR, ansi_grey(), |
| 299 |
TABLE_EMPTY, |
| 300 |
TABLE_STRING, comment, |
| 301 |
TABLE_SET_COLOR, ansi_grey(), |
| 302 |
TABLE_EMPTY, |
| 303 |
TABLE_EMPTY, |
| 304 |
TABLE_INT, 1); /* sort after any other entry with the same UID */ |
| 305 |
if (r < 0) |
| 306 |
return table_log_add_error(r); |
| 307 |
|
| 308 |
n_added += 2; |
| 309 |
} |
| 310 |
|
| 311 |
return n_added; |
| 312 |
} |
| 313 |
|
| 314 |
static int add_unavailable_uid(Table *table, uid_t start, uid_t end) { |
| 315 |
_cleanup_free_ char *name = NULL; |
| 316 |
int r; |
| 317 |
|
| 318 |
assert(table); |
| 319 |
assert(start <= end); |
| 320 |
|
| 321 |
name = strjoin(glyph(GLYPH_ARROW_DOWN), |
| 322 |
" begin unavailable users ", |
| 323 |
glyph(GLYPH_ARROW_DOWN)); |
| 324 |
if (!name) |
| 325 |
return log_oom(); |
| 326 |
|
| 327 |
r = table_add_many( |
| 328 |
table, |
| 329 |
TABLE_STRING, glyph(GLYPH_TREE_TOP), |
| 330 |
TABLE_STRING, name, |
| 331 |
TABLE_SET_COLOR, ansi_grey(), |
| 332 |
TABLE_EMPTY, |
| 333 |
TABLE_UID, start, |
| 334 |
TABLE_SET_COLOR, ansi_grey(), |
| 335 |
TABLE_EMPTY, |
| 336 |
TABLE_STRING, "First unavailable user", |
| 337 |
TABLE_SET_COLOR, ansi_grey(), |
| 338 |
TABLE_EMPTY, |
| 339 |
TABLE_EMPTY, |
| 340 |
TABLE_INT, -1); /* sort before an other entry with the same UID */ |
| 341 |
if (r < 0) |
| 342 |
return table_log_add_error(r); |
| 343 |
|
| 344 |
free(name); |
| 345 |
name = strjoin(glyph(GLYPH_ARROW_UP), |
| 346 |
" end unavailable users ", |
| 347 |
glyph(GLYPH_ARROW_UP)); |
| 348 |
if (!name) |
| 349 |
return log_oom(); |
| 350 |
|
| 351 |
r = table_add_many( |
| 352 |
table, |
| 353 |
TABLE_STRING, glyph(GLYPH_TREE_RIGHT), |
| 354 |
TABLE_STRING, name, |
| 355 |
TABLE_SET_COLOR, ansi_grey(), |
| 356 |
TABLE_EMPTY, |
| 357 |
TABLE_UID, end, |
| 358 |
TABLE_SET_COLOR, ansi_grey(), |
| 359 |
TABLE_EMPTY, |
| 360 |
TABLE_STRING, "Last unavailable user", |
| 361 |
TABLE_SET_COLOR, ansi_grey(), |
| 362 |
TABLE_EMPTY, |
| 363 |
TABLE_EMPTY, |
| 364 |
TABLE_INT, 1); /* sort after any other entry with the same UID */ |
| 365 |
if (r < 0) |
| 366 |
return table_log_add_error(r); |
| 367 |
|
| 368 |
return 2; |
| 369 |
} |
| 370 |
|
| 371 |
static int table_add_uid_map( |
| 372 |
Table *table, |
| 373 |
const UIDRange *p, |
| 374 |
int (*add_unavailable)(Table *t, uid_t start, uid_t end)) { |
| 375 |
|
| 376 |
uid_t focus = 0; |
| 377 |
int n_added = 0, r; |
| 378 |
|
| 379 |
assert(table); |
| 380 |
assert(add_unavailable); |
| 381 |
|
| 382 |
if (!p) |
| 383 |
return 0; |
| 384 |
|
| 385 |
FOREACH_ARRAY(x, p->entries, p->n_entries) { |
| 386 |
if (focus < x->start) { |
| 387 |
r = add_unavailable(table, focus, x->start-1); |
| 388 |
if (r < 0) |
| 389 |
return r; |
| 390 |
|
| 391 |
n_added += r; |
| 392 |
} |
| 393 |
|
| 394 |
if (x->start > UINT32_MAX - x->nr) { /* overflow check */ |
| 395 |
focus = UINT32_MAX; |
| 396 |
break; |
| 397 |
} |
| 398 |
|
| 399 |
focus = x->start + x->nr; |
| 400 |
} |
| 401 |
|
| 402 |
if (focus < UINT32_MAX-1) { |
| 403 |
r = add_unavailable(table, focus, UINT32_MAX-1); |
| 404 |
if (r < 0) |
| 405 |
return r; |
| 406 |
|
| 407 |
n_added += r; |
| 408 |
} |
| 409 |
|
| 410 |
return n_added; |
| 411 |
} |
| 412 |
|
| 413 |
VERB(verb_display_user, "user", "[USER…]", VERB_ANY, VERB_ANY, VERB_DEFAULT, |
| 414 |
"Inspect user"); |
| 415 |
static int verb_display_user(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 416 |
_cleanup_(table_unrefp) Table *table = NULL; |
| 417 |
bool draw_separator = false; |
| 418 |
int ret = 0, r; |
| 419 |
|
| 420 |
if (arg_output < 0) |
| 421 |
arg_output = arg_from_file || (argc > 1 && !arg_fuzzy) || !sd_id128_is_null(arg_uuid) ? OUTPUT_FRIENDLY : OUTPUT_TABLE; |
| 422 |
|
| 423 |
if (arg_output == OUTPUT_TABLE) { |
| 424 |
table = table_new(" ", "name", "disposition", "uid", "gid", "realname", "home", "shell", "order"); |
| 425 |
if (!table) |
| 426 |
return log_oom(); |
| 427 |
|
| 428 |
(void) table_set_align_percent(table, table_get_cell(table, 0, 3), 100); |
| 429 |
(void) table_set_align_percent(table, table_get_cell(table, 0, 4), 100); |
| 430 |
table_set_ersatz_string(table, TABLE_ERSATZ_DASH); |
| 431 |
(void) table_set_sort(table, (size_t) 3, (size_t) 8); |
| 432 |
(void) table_hide_column_from_display(table, (size_t) 8); |
| 433 |
if (!arg_boundaries) |
| 434 |
(void) table_hide_column_from_display(table, (size_t) 0); |
| 435 |
} |
| 436 |
|
| 437 |
_cleanup_(userdb_match_done) UserDBMatch match = { |
| 438 |
.disposition_mask = arg_disposition_mask, |
| 439 |
.uid_min = arg_uid_min, |
| 440 |
.uid_max = arg_uid_max, |
| 441 |
.uuid = arg_uuid, |
| 442 |
}; |
| 443 |
|
| 444 |
if (arg_from_file) { |
| 445 |
if (argc > 1) |
| 446 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No argument expected when invoked with --from-file=, refusing."); |
| 447 |
|
| 448 |
_cleanup_(user_record_unrefp) UserRecord *ur = user_record_new(); |
| 449 |
if (!ur) |
| 450 |
return log_oom(); |
| 451 |
|
| 452 |
r = user_record_load(ur, arg_from_file, USER_RECORD_LOAD_MASK_SECRET|USER_RECORD_LOG); |
| 453 |
if (r < 0) |
| 454 |
return r; |
| 455 |
|
| 456 |
r = show_user(ur, table); |
| 457 |
if (r < 0) |
| 458 |
return r; |
| 459 |
|
| 460 |
} else if (argc > 1 && !arg_fuzzy) |
| 461 |
STRV_FOREACH(i, argv + 1) { |
| 462 |
_cleanup_(user_record_unrefp) UserRecord *ur = NULL; |
| 463 |
|
| 464 |
r = userdb_by_name(*i, &match, arg_userdb_flags|USERDB_PARSE_NUMERIC, &ur); |
| 465 |
if (r < 0) { |
| 466 |
if (r == -ESRCH) |
| 467 |
log_error_errno(r, "User %s does not exist.", *i); |
| 468 |
else if (r == -EHOSTDOWN) |
| 469 |
log_error_errno(r, "Selected user database service is not available for this request."); |
| 470 |
else if (r == -ENOEXEC) |
| 471 |
log_error_errno(r, "User '%s' exists but does not match specified filter.", *i); |
| 472 |
else |
| 473 |
log_error_errno(r, "Failed to find user %s: %m", *i); |
| 474 |
|
| 475 |
RET_GATHER(ret, r); |
| 476 |
} else { |
| 477 |
if (draw_separator && arg_output == OUTPUT_FRIENDLY) |
| 478 |
putchar('\n'); |
| 479 |
|
| 480 |
r = show_user(ur, table); |
| 481 |
if (r < 0) |
| 482 |
return r; |
| 483 |
|
| 484 |
draw_separator = true; |
| 485 |
} |
| 486 |
} |
| 487 |
else { |
| 488 |
if (argc > 1) { |
| 489 |
/* If there are further arguments, they are the fuzzy match strings. */ |
| 490 |
match.fuzzy_names = strv_copy(strv_skip(argv, 1)); |
| 491 |
if (!match.fuzzy_names) |
| 492 |
return log_oom(); |
| 493 |
} |
| 494 |
|
| 495 |
_cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL; |
| 496 |
r = userdb_all(&match, arg_userdb_flags, &iterator); |
| 497 |
if (r == -ENOLINK) /* ENOLINK → Didn't find answer without Varlink, and didn't try Varlink because was configured to off. */ |
| 498 |
log_debug_errno(r, "No entries found. (Didn't check via Varlink.)"); |
| 499 |
else if (r == -ESRCH) /* ESRCH → Couldn't find any suitable entry, but we checked all sources */ |
| 500 |
log_debug_errno(r, "No entries found."); |
| 501 |
else if (r < 0) |
| 502 |
return log_error_errno(r, "Failed to enumerate users: %m"); |
| 503 |
else { |
| 504 |
for (;;) { |
| 505 |
_cleanup_(user_record_unrefp) UserRecord *ur = NULL; |
| 506 |
|
| 507 |
r = userdb_iterator_get(iterator, &match, &ur); |
| 508 |
if (r == -ESRCH) |
| 509 |
break; |
| 510 |
if (r == -EHOSTDOWN) |
| 511 |
return log_error_errno(r, "Selected user database service is not available for this request."); |
| 512 |
if (r < 0) |
| 513 |
return log_error_errno(r, "Failed to acquire next user: %m"); |
| 514 |
|
| 515 |
if (draw_separator && arg_output == OUTPUT_FRIENDLY) |
| 516 |
putchar('\n'); |
| 517 |
|
| 518 |
r = show_user(ur, table); |
| 519 |
if (r < 0) |
| 520 |
return r; |
| 521 |
|
| 522 |
draw_separator = true; |
| 523 |
} |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
if (table) { |
| 528 |
int boundary_lines = 0, uid_map_lines = 0; |
| 529 |
|
| 530 |
if (arg_boundaries) { |
| 531 |
_cleanup_(uid_range_freep) UIDRange *uid_range = NULL; |
| 532 |
|
| 533 |
r = uid_range_load_userns(/* path= */ NULL, UID_RANGE_USERNS_INSIDE, &uid_range); |
| 534 |
if (r < 0) |
| 535 |
log_debug_errno(r, "Failed to load /proc/self/uid_map, ignoring: %m"); |
| 536 |
|
| 537 |
boundary_lines = table_add_uid_boundaries(table, uid_range); |
| 538 |
if (boundary_lines < 0) |
| 539 |
return boundary_lines; |
| 540 |
|
| 541 |
uid_map_lines = table_add_uid_map(table, uid_range, add_unavailable_uid); |
| 542 |
if (uid_map_lines < 0) |
| 543 |
return uid_map_lines; |
| 544 |
} |
| 545 |
|
| 546 |
if (!table_isempty(table)) { |
| 547 |
r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend); |
| 548 |
if (r < 0) |
| 549 |
return r; |
| 550 |
} |
| 551 |
|
| 552 |
if (arg_legend) { |
| 553 |
size_t k; |
| 554 |
|
| 555 |
k = table_get_rows(table) - 1 - boundary_lines - uid_map_lines; |
| 556 |
if (k > 0) |
| 557 |
printf("\n%zu users listed.\n", k); |
| 558 |
else |
| 559 |
printf("No users.\n"); |
| 560 |
} |
| 561 |
} |
| 562 |
|
| 563 |
return ret; |
| 564 |
} |
| 565 |
|
| 566 |
static int show_group(GroupRecord *gr, Table *table) { |
| 567 |
int r; |
| 568 |
|
| 569 |
assert(gr); |
| 570 |
|
| 571 |
switch (arg_output) { |
| 572 |
|
| 573 |
case OUTPUT_CLASSIC: { |
| 574 |
_cleanup_free_ char *m = NULL; |
| 575 |
|
| 576 |
if (!gid_is_valid(gr->gid)) |
| 577 |
break; |
| 578 |
|
| 579 |
m = strv_join(gr->members, ","); |
| 580 |
if (!m) |
| 581 |
return log_oom(); |
| 582 |
|
| 583 |
printf("%s:x:" GID_FMT ":%s\n", |
| 584 |
gr->group_name, |
| 585 |
gr->gid, |
| 586 |
m); |
| 587 |
break; |
| 588 |
} |
| 589 |
|
| 590 |
case OUTPUT_JSON: |
| 591 |
sd_json_variant_dump(gr->json, arg_json_format_flags, NULL, NULL); |
| 592 |
break; |
| 593 |
|
| 594 |
case OUTPUT_FRIENDLY: |
| 595 |
group_record_show(gr, true); |
| 596 |
|
| 597 |
if (gr->incomplete) { |
| 598 |
fflush(stdout); |
| 599 |
log_warning("Warning: lacking rights to acquire privileged fields of group record of '%s', output incomplete.", gr->group_name); |
| 600 |
} |
| 601 |
|
| 602 |
break; |
| 603 |
|
| 604 |
case OUTPUT_TABLE: { |
| 605 |
UserDisposition d; |
| 606 |
|
| 607 |
assert(table); |
| 608 |
d = group_record_disposition(gr); |
| 609 |
|
| 610 |
r = table_add_many( |
| 611 |
table, |
| 612 |
TABLE_STRING, "", |
| 613 |
TABLE_STRING, gr->group_name, |
| 614 |
TABLE_SET_COLOR, user_disposition_to_color(d), |
| 615 |
TABLE_STRING, user_disposition_to_string(d), |
| 616 |
TABLE_GID, gr->gid, |
| 617 |
TABLE_STRING, gr->description, |
| 618 |
TABLE_INT, 0); |
| 619 |
if (r < 0) |
| 620 |
return table_log_add_error(r); |
| 621 |
|
| 622 |
break; |
| 623 |
} |
| 624 |
|
| 625 |
default: |
| 626 |
assert_not_reached(); |
| 627 |
} |
| 628 |
|
| 629 |
return 0; |
| 630 |
} |
| 631 |
|
| 632 |
static int table_add_gid_boundaries(Table *table, const UIDRange *p) { |
| 633 |
int r, n_added = 0; |
| 634 |
|
| 635 |
assert(table); |
| 636 |
|
| 637 |
FOREACH_ELEMENT(i, uid_range_table) { |
| 638 |
_cleanup_free_ char *name = NULL, *comment = NULL; |
| 639 |
|
| 640 |
if (!BIT_SET(arg_disposition_mask, i->disposition)) |
| 641 |
continue; |
| 642 |
|
| 643 |
if (!uid_range_covers(p, i->first, i->last - i->first + 1)) |
| 644 |
continue; |
| 645 |
|
| 646 |
if (i->test && !i->test()) |
| 647 |
continue; |
| 648 |
|
| 649 |
name = strjoin(glyph(GLYPH_ARROW_DOWN), |
| 650 |
" begin ", i->name, " groups ", |
| 651 |
glyph(GLYPH_ARROW_DOWN)); |
| 652 |
if (!name) |
| 653 |
return log_oom(); |
| 654 |
|
| 655 |
comment = strjoin("First ", i->name, " group"); |
| 656 |
if (!comment) |
| 657 |
return log_oom(); |
| 658 |
|
| 659 |
r = table_add_many( |
| 660 |
table, |
| 661 |
TABLE_STRING, glyph(GLYPH_TREE_TOP), |
| 662 |
TABLE_STRING, name, |
| 663 |
TABLE_SET_COLOR, ansi_grey(), |
| 664 |
TABLE_STRING, user_disposition_to_string(i->disposition), |
| 665 |
TABLE_SET_COLOR, ansi_grey(), |
| 666 |
TABLE_GID, i->first, |
| 667 |
TABLE_SET_COLOR, ansi_grey(), |
| 668 |
TABLE_STRING, comment, |
| 669 |
TABLE_SET_COLOR, ansi_grey(), |
| 670 |
TABLE_INT, -1); /* sort before any other entry with the same GID */ |
| 671 |
if (r < 0) |
| 672 |
return table_log_add_error(r); |
| 673 |
|
| 674 |
free(name); |
| 675 |
name = strjoin(glyph(GLYPH_ARROW_UP), |
| 676 |
" end ", i->name, " groups ", |
| 677 |
glyph(GLYPH_ARROW_UP)); |
| 678 |
if (!name) |
| 679 |
return log_oom(); |
| 680 |
|
| 681 |
free(comment); |
| 682 |
comment = strjoin("Last ", i->name, " group"); |
| 683 |
if (!comment) |
| 684 |
return log_oom(); |
| 685 |
|
| 686 |
r = table_add_many( |
| 687 |
table, |
| 688 |
TABLE_STRING, glyph(GLYPH_TREE_RIGHT), |
| 689 |
TABLE_STRING, name, |
| 690 |
TABLE_SET_COLOR, ansi_grey(), |
| 691 |
TABLE_STRING, user_disposition_to_string(i->disposition), |
| 692 |
TABLE_SET_COLOR, ansi_grey(), |
| 693 |
TABLE_GID, i->last, |
| 694 |
TABLE_SET_COLOR, ansi_grey(), |
| 695 |
TABLE_STRING, comment, |
| 696 |
TABLE_SET_COLOR, ansi_grey(), |
| 697 |
TABLE_INT, 1); /* sort after any other entry with the same GID */ |
| 698 |
if (r < 0) |
| 699 |
return table_log_add_error(r); |
| 700 |
|
| 701 |
n_added += 2; |
| 702 |
} |
| 703 |
|
| 704 |
return n_added; |
| 705 |
} |
| 706 |
|
| 707 |
static int add_unavailable_gid(Table *table, uid_t start, uid_t end) { |
| 708 |
_cleanup_free_ char *name = NULL; |
| 709 |
int r; |
| 710 |
|
| 711 |
assert(table); |
| 712 |
assert(start <= end); |
| 713 |
|
| 714 |
name = strjoin(glyph(GLYPH_ARROW_DOWN), |
| 715 |
" begin unavailable groups ", |
| 716 |
glyph(GLYPH_ARROW_DOWN)); |
| 717 |
if (!name) |
| 718 |
return log_oom(); |
| 719 |
|
| 720 |
r = table_add_many( |
| 721 |
table, |
| 722 |
TABLE_STRING, glyph(GLYPH_TREE_TOP), |
| 723 |
TABLE_STRING, name, |
| 724 |
TABLE_SET_COLOR, ansi_grey(), |
| 725 |
TABLE_EMPTY, |
| 726 |
TABLE_GID, start, |
| 727 |
TABLE_SET_COLOR, ansi_grey(), |
| 728 |
TABLE_STRING, "First unavailable group", |
| 729 |
TABLE_SET_COLOR, ansi_grey(), |
| 730 |
TABLE_INT, -1); /* sort before any other entry with the same GID */ |
| 731 |
if (r < 0) |
| 732 |
return table_log_add_error(r); |
| 733 |
|
| 734 |
free(name); |
| 735 |
name = strjoin(glyph(GLYPH_ARROW_UP), |
| 736 |
" end unavailable groups ", |
| 737 |
glyph(GLYPH_ARROW_UP)); |
| 738 |
if (!name) |
| 739 |
return log_oom(); |
| 740 |
|
| 741 |
r = table_add_many( |
| 742 |
table, |
| 743 |
TABLE_STRING, glyph(GLYPH_TREE_RIGHT), |
| 744 |
TABLE_STRING, name, |
| 745 |
TABLE_SET_COLOR, ansi_grey(), |
| 746 |
TABLE_EMPTY, |
| 747 |
TABLE_GID, end, |
| 748 |
TABLE_SET_COLOR, ansi_grey(), |
| 749 |
TABLE_STRING, "Last unavailable group", |
| 750 |
TABLE_SET_COLOR, ansi_grey(), |
| 751 |
TABLE_INT, 1); /* sort after any other entry with the same GID */ |
| 752 |
if (r < 0) |
| 753 |
return table_log_add_error(r); |
| 754 |
|
| 755 |
return 2; |
| 756 |
} |
| 757 |
|
| 758 |
VERB(verb_display_group, "group", "[GROUP…]", VERB_ANY, VERB_ANY, 0, |
| 759 |
"Inspect group"); |
| 760 |
static int verb_display_group(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 761 |
_cleanup_(table_unrefp) Table *table = NULL; |
| 762 |
bool draw_separator = false; |
| 763 |
int ret = 0, r; |
| 764 |
|
| 765 |
if (arg_output < 0) |
| 766 |
arg_output = arg_from_file || (argc > 1 && !arg_fuzzy) || !sd_id128_is_null(arg_uuid) ? OUTPUT_FRIENDLY : OUTPUT_TABLE; |
| 767 |
|
| 768 |
if (arg_output == OUTPUT_TABLE) { |
| 769 |
table = table_new(" ", "name", "disposition", "gid", "description", "order"); |
| 770 |
if (!table) |
| 771 |
return log_oom(); |
| 772 |
|
| 773 |
(void) table_set_align_percent(table, table_get_cell(table, 0, 3), 100); |
| 774 |
table_set_ersatz_string(table, TABLE_ERSATZ_DASH); |
| 775 |
(void) table_set_sort(table, (size_t) 3, (size_t) 5); |
| 776 |
(void) table_hide_column_from_display(table, (size_t) 5); |
| 777 |
if (!arg_boundaries) |
| 778 |
(void) table_hide_column_from_display(table, (size_t) 0); |
| 779 |
} |
| 780 |
|
| 781 |
_cleanup_(userdb_match_done) UserDBMatch match = { |
| 782 |
.disposition_mask = arg_disposition_mask, |
| 783 |
.gid_min = arg_uid_min, |
| 784 |
.gid_max = arg_uid_max, |
| 785 |
.uuid = arg_uuid, |
| 786 |
}; |
| 787 |
|
| 788 |
if (arg_from_file) { |
| 789 |
if (argc > 1) |
| 790 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No argument expected when invoked with --from-file=, refusing."); |
| 791 |
|
| 792 |
_cleanup_(group_record_unrefp) GroupRecord *gr = group_record_new(); |
| 793 |
if (!gr) |
| 794 |
return log_oom(); |
| 795 |
|
| 796 |
r = group_record_load(gr, arg_from_file, USER_RECORD_LOAD_MASK_SECRET|USER_RECORD_LOG); |
| 797 |
if (r < 0) |
| 798 |
return r; |
| 799 |
|
| 800 |
r = show_group(gr, table); |
| 801 |
if (r < 0) |
| 802 |
return r; |
| 803 |
|
| 804 |
} else if (argc > 1 && !arg_fuzzy) |
| 805 |
STRV_FOREACH(i, argv + 1) { |
| 806 |
_cleanup_(group_record_unrefp) GroupRecord *gr = NULL; |
| 807 |
|
| 808 |
r = groupdb_by_name(*i, &match, arg_userdb_flags|USERDB_PARSE_NUMERIC, &gr); |
| 809 |
if (r < 0) { |
| 810 |
if (r == -ESRCH) |
| 811 |
log_error_errno(r, "Group %s does not exist.", *i); |
| 812 |
else if (r == -EHOSTDOWN) |
| 813 |
log_error_errno(r, "Selected group database service is not available for this request."); |
| 814 |
else if (r == -ENOEXEC) |
| 815 |
log_error_errno(r, "Group '%s' exists but does not match specified filter.", *i); |
| 816 |
else |
| 817 |
log_error_errno(r, "Failed to find group %s: %m", *i); |
| 818 |
|
| 819 |
RET_GATHER(ret, r); |
| 820 |
} else { |
| 821 |
if (draw_separator && arg_output == OUTPUT_FRIENDLY) |
| 822 |
putchar('\n'); |
| 823 |
|
| 824 |
r = show_group(gr, table); |
| 825 |
if (r < 0) |
| 826 |
return r; |
| 827 |
|
| 828 |
draw_separator = true; |
| 829 |
} |
| 830 |
} |
| 831 |
else { |
| 832 |
if (argc > 1) { |
| 833 |
match.fuzzy_names = strv_copy(strv_skip(argv, 1)); |
| 834 |
if (!match.fuzzy_names) |
| 835 |
return log_oom(); |
| 836 |
} |
| 837 |
|
| 838 |
_cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL; |
| 839 |
r = groupdb_all(&match, arg_userdb_flags, &iterator); |
| 840 |
if (r == -ENOLINK) |
| 841 |
log_debug_errno(r, "No entries found. (Didn't check via Varlink.)"); |
| 842 |
else if (r == -ESRCH) |
| 843 |
log_debug_errno(r, "No entries found."); |
| 844 |
else if (r < 0) |
| 845 |
return log_error_errno(r, "Failed to enumerate groups: %m"); |
| 846 |
else { |
| 847 |
for (;;) { |
| 848 |
_cleanup_(group_record_unrefp) GroupRecord *gr = NULL; |
| 849 |
|
| 850 |
r = groupdb_iterator_get(iterator, &match, &gr); |
| 851 |
if (r == -ESRCH) |
| 852 |
break; |
| 853 |
if (r == -EHOSTDOWN) |
| 854 |
return log_error_errno(r, "Selected group database service is not available for this request."); |
| 855 |
if (r < 0) |
| 856 |
return log_error_errno(r, "Failed to acquire next group: %m"); |
| 857 |
|
| 858 |
if (draw_separator && arg_output == OUTPUT_FRIENDLY) |
| 859 |
putchar('\n'); |
| 860 |
|
| 861 |
r = show_group(gr, table); |
| 862 |
if (r < 0) |
| 863 |
return r; |
| 864 |
|
| 865 |
draw_separator = true; |
| 866 |
} |
| 867 |
} |
| 868 |
} |
| 869 |
|
| 870 |
if (table) { |
| 871 |
int boundary_lines = 0, gid_map_lines = 0; |
| 872 |
|
| 873 |
if (arg_boundaries) { |
| 874 |
_cleanup_(uid_range_freep) UIDRange *gid_range = NULL; |
| 875 |
r = uid_range_load_userns(/* path= */ NULL, GID_RANGE_USERNS_INSIDE, &gid_range); |
| 876 |
if (r < 0) |
| 877 |
log_debug_errno(r, "Failed to load /proc/self/gid_map, ignoring: %m"); |
| 878 |
|
| 879 |
boundary_lines = table_add_gid_boundaries(table, gid_range); |
| 880 |
if (boundary_lines < 0) |
| 881 |
return boundary_lines; |
| 882 |
|
| 883 |
gid_map_lines = table_add_uid_map(table, gid_range, add_unavailable_gid); |
| 884 |
if (gid_map_lines < 0) |
| 885 |
return gid_map_lines; |
| 886 |
} |
| 887 |
|
| 888 |
if (!table_isempty(table)) { |
| 889 |
r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend); |
| 890 |
if (r < 0) |
| 891 |
return r; |
| 892 |
} |
| 893 |
|
| 894 |
if (arg_legend) { |
| 895 |
size_t k; |
| 896 |
|
| 897 |
k = table_get_rows(table) - 1 - boundary_lines - gid_map_lines; |
| 898 |
if (k > 0) |
| 899 |
printf("\n%zu groups listed.\n", k); |
| 900 |
else |
| 901 |
printf("No groups.\n"); |
| 902 |
} |
| 903 |
} |
| 904 |
|
| 905 |
return ret; |
| 906 |
} |
| 907 |
|
| 908 |
static int show_membership(const char *user, const char *group, Table *table) { |
| 909 |
int r; |
| 910 |
|
| 911 |
assert(user); |
| 912 |
assert(group); |
| 913 |
|
| 914 |
switch (arg_output) { |
| 915 |
|
| 916 |
case OUTPUT_CLASSIC: |
| 917 |
/* Strictly speaking there's no 'classic' output for this concept, but let's output it in |
| 918 |
* similar style to the classic output for user/group info */ |
| 919 |
|
| 920 |
printf("%s:%s\n", user, group); |
| 921 |
break; |
| 922 |
|
| 923 |
case OUTPUT_JSON: { |
| 924 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 925 |
|
| 926 |
r = sd_json_buildo( |
| 927 |
&v, |
| 928 |
SD_JSON_BUILD_PAIR_STRING("user", user), |
| 929 |
SD_JSON_BUILD_PAIR_STRING("group", group)); |
| 930 |
if (r < 0) |
| 931 |
return log_error_errno(r, "Failed to build JSON object: %m"); |
| 932 |
|
| 933 |
sd_json_variant_dump(v, arg_json_format_flags, NULL, NULL); |
| 934 |
break; |
| 935 |
} |
| 936 |
|
| 937 |
case OUTPUT_FRIENDLY: |
| 938 |
/* Hmm, this is not particularly friendly, but not sure how we could do this better */ |
| 939 |
printf("%s: %s\n", group, user); |
| 940 |
break; |
| 941 |
|
| 942 |
case OUTPUT_TABLE: |
| 943 |
assert(table); |
| 944 |
|
| 945 |
r = table_add_many( |
| 946 |
table, |
| 947 |
TABLE_STRING, user, |
| 948 |
TABLE_STRING, group); |
| 949 |
if (r < 0) |
| 950 |
return table_log_add_error(r); |
| 951 |
|
| 952 |
break; |
| 953 |
|
| 954 |
default: |
| 955 |
assert_not_reached(); |
| 956 |
} |
| 957 |
|
| 958 |
return 0; |
| 959 |
} |
| 960 |
|
| 961 |
VERB(verb_display_memberships, "users-in-group", "[GROUP…]", VERB_ANY, VERB_ANY, 0, |
| 962 |
"Show users that are members of specified groups"); |
| 963 |
VERB(verb_display_memberships, "groups-of-user", "[USER…]", VERB_ANY, VERB_ANY, 0, |
| 964 |
"Show groups the specified users are members of"); |
| 965 |
static int verb_display_memberships(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 966 |
_cleanup_(table_unrefp) Table *table = NULL; |
| 967 |
int ret = 0, r; |
| 968 |
|
| 969 |
if (arg_from_file) |
| 970 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--from-file= not supported when showing memberships, refusing."); |
| 971 |
|
| 972 |
if (arg_output < 0) |
| 973 |
arg_output = OUTPUT_TABLE; |
| 974 |
|
| 975 |
if (arg_output == OUTPUT_TABLE) { |
| 976 |
table = table_new("user", "group"); |
| 977 |
if (!table) |
| 978 |
return log_oom(); |
| 979 |
|
| 980 |
(void) table_set_sort(table, (size_t) 0, (size_t) 1); |
| 981 |
} |
| 982 |
|
| 983 |
if (argc > 1) |
| 984 |
STRV_FOREACH(i, argv + 1) { |
| 985 |
_cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL; |
| 986 |
|
| 987 |
if (streq(argv[0], "users-in-group")) { |
| 988 |
r = membershipdb_by_group(*i, arg_userdb_flags, &iterator); |
| 989 |
if (r < 0) |
| 990 |
return log_error_errno(r, "Failed to enumerate users in group: %m"); |
| 991 |
} else if (streq(argv[0], "groups-of-user")) { |
| 992 |
r = membershipdb_by_user(*i, arg_userdb_flags, &iterator); |
| 993 |
if (r < 0) |
| 994 |
return log_error_errno(r, "Failed to enumerate groups of user: %m"); |
| 995 |
} else |
| 996 |
assert_not_reached(); |
| 997 |
|
| 998 |
for (;;) { |
| 999 |
_cleanup_free_ char *user = NULL, *group = NULL; |
| 1000 |
|
| 1001 |
r = membershipdb_iterator_get(iterator, &user, &group); |
| 1002 |
if (r == -ESRCH) |
| 1003 |
break; |
| 1004 |
if (r == -EHOSTDOWN) |
| 1005 |
return log_error_errno(r, "Selected membership database service is not available for this request."); |
| 1006 |
if (r < 0) |
| 1007 |
return log_error_errno(r, "Failed to acquire next membership: %m"); |
| 1008 |
|
| 1009 |
r = show_membership(user, group, table); |
| 1010 |
if (r < 0) |
| 1011 |
return r; |
| 1012 |
} |
| 1013 |
} |
| 1014 |
else { |
| 1015 |
_cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL; |
| 1016 |
|
| 1017 |
r = membershipdb_all(arg_userdb_flags, &iterator); |
| 1018 |
if (r == -ENOLINK) |
| 1019 |
log_debug_errno(r, "No entries found. (Didn't check via Varlink.)"); |
| 1020 |
else if (r == -ESRCH) |
| 1021 |
log_debug_errno(r, "No entries found."); |
| 1022 |
else if (r < 0) |
| 1023 |
return log_error_errno(r, "Failed to enumerate memberships: %m"); |
| 1024 |
else { |
| 1025 |
for (;;) { |
| 1026 |
_cleanup_free_ char *user = NULL, *group = NULL; |
| 1027 |
|
| 1028 |
r = membershipdb_iterator_get(iterator, &user, &group); |
| 1029 |
if (r == -ESRCH) |
| 1030 |
break; |
| 1031 |
if (r == -EHOSTDOWN) |
| 1032 |
return log_error_errno(r, "Selected membership database service is not available for this request."); |
| 1033 |
if (r < 0) |
| 1034 |
return log_error_errno(r, "Failed to acquire next membership: %m"); |
| 1035 |
|
| 1036 |
r = show_membership(user, group, table); |
| 1037 |
if (r < 0) |
| 1038 |
return r; |
| 1039 |
} |
| 1040 |
} |
| 1041 |
} |
| 1042 |
|
| 1043 |
if (table) { |
| 1044 |
if (!table_isempty(table)) { |
| 1045 |
r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend); |
| 1046 |
if (r < 0) |
| 1047 |
return r; |
| 1048 |
} |
| 1049 |
|
| 1050 |
if (arg_legend) { |
| 1051 |
if (table_isempty(table)) |
| 1052 |
printf("No memberships.\n"); |
| 1053 |
else |
| 1054 |
printf("\n%zu memberships listed.\n", table_get_rows(table) - 1); |
| 1055 |
} |
| 1056 |
} |
| 1057 |
|
| 1058 |
return ret; |
| 1059 |
} |
| 1060 |
|
| 1061 |
VERB(verb_display_services, "services", NULL, VERB_ANY, 1, 0, |
| 1062 |
"Show enabled database services"); |
| 1063 |
static int verb_display_services(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 1064 |
_cleanup_(table_unrefp) Table *t = NULL; |
| 1065 |
_cleanup_closedir_ DIR *d = NULL; |
| 1066 |
int r; |
| 1067 |
|
| 1068 |
if (arg_from_file) |
| 1069 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--from-file= not supported when showing services, refusing."); |
| 1070 |
|
| 1071 |
d = opendir("/run/systemd/userdb/"); |
| 1072 |
if (!d) { |
| 1073 |
if (errno == ENOENT) { |
| 1074 |
log_info("No services."); |
| 1075 |
return 0; |
| 1076 |
} |
| 1077 |
|
| 1078 |
return log_error_errno(errno, "Failed to open %s: %m", "/run/systemd/userdb/"); |
| 1079 |
} |
| 1080 |
|
| 1081 |
t = table_new("service", "listening"); |
| 1082 |
if (!t) |
| 1083 |
return log_oom(); |
| 1084 |
|
| 1085 |
(void) table_set_sort(t, (size_t) 0); |
| 1086 |
|
| 1087 |
FOREACH_DIRENT(de, d, return -errno) { |
| 1088 |
_cleanup_free_ char *no = NULL; |
| 1089 |
_cleanup_close_ int fd = -EBADF; |
| 1090 |
|
| 1091 |
fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); |
| 1092 |
if (fd < 0) |
| 1093 |
return log_error_errno(errno, "Failed to allocate AF_UNIX/SOCK_STREAM socket: %m"); |
| 1094 |
|
| 1095 |
r = connect_unix_path(fd, dirfd(d), de->d_name); |
| 1096 |
if (r < 0) { |
| 1097 |
no = strjoin("No (", ERRNO_NAME(r), ")"); |
| 1098 |
if (!no) |
| 1099 |
return log_oom(); |
| 1100 |
} |
| 1101 |
|
| 1102 |
r = table_add_many(t, |
| 1103 |
TABLE_STRING, de->d_name, |
| 1104 |
TABLE_STRING, no ?: "yes", |
| 1105 |
TABLE_SET_COLOR, ansi_highlight_green_red(!no)); |
| 1106 |
if (r < 0) |
| 1107 |
return table_log_add_error(r); |
| 1108 |
} |
| 1109 |
|
| 1110 |
if (!table_isempty(t)) { |
| 1111 |
r = table_print_with_pager(t, arg_json_format_flags, arg_pager_flags, arg_legend); |
| 1112 |
if (r < 0) |
| 1113 |
return r; |
| 1114 |
} |
| 1115 |
|
| 1116 |
if (arg_legend && arg_output != OUTPUT_JSON) { |
| 1117 |
if (table_isempty(t)) |
| 1118 |
printf("No services.\n"); |
| 1119 |
else |
| 1120 |
printf("\n%zu services listed.\n", table_get_rows(t) - 1); |
| 1121 |
} |
| 1122 |
|
| 1123 |
return 0; |
| 1124 |
} |
| 1125 |
|
| 1126 |
VERB(verb_ssh_authorized_keys, "ssh-authorized-keys", "USER", 2, VERB_ANY, 0, |
| 1127 |
"Show SSH authorized keys for user"); |
| 1128 |
static int verb_ssh_authorized_keys(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 1129 |
_cleanup_(user_record_unrefp) UserRecord *ur = NULL; |
| 1130 |
const char *username; |
| 1131 |
char **chain_invocation; |
| 1132 |
int r; |
| 1133 |
|
| 1134 |
assert(argc >= 2); |
| 1135 |
|
| 1136 |
if (arg_from_file) |
| 1137 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--from-file= not supported when showing SSH authorized keys, refusing."); |
| 1138 |
|
| 1139 |
username = argv[1]; |
| 1140 |
|
| 1141 |
if (arg_chain) { |
| 1142 |
/* If --chain is specified, the rest of the command line is the chain command */ |
| 1143 |
|
| 1144 |
if (argc < 3) |
| 1145 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 1146 |
"No chain command line specified, refusing."); |
| 1147 |
|
| 1148 |
/* Make similar restrictions on the chain command as OpenSSH itself makes on the primary command. */ |
| 1149 |
if (!path_is_absolute(argv[2])) |
| 1150 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 1151 |
"Chain invocation of ssh-authorized-keys commands requires an absolute program path (got '%s').", |
| 1152 |
argv[2]); |
| 1153 |
|
| 1154 |
if (!path_is_normalized(argv[2])) |
| 1155 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 1156 |
"Chain invocation of ssh-authorized-keys commands requires a normalized program path (got '%s').", |
| 1157 |
argv[2]); |
| 1158 |
|
| 1159 |
chain_invocation = argv + 2; |
| 1160 |
} else { |
| 1161 |
/* If --chain is not specified, then refuse any further arguments */ |
| 1162 |
|
| 1163 |
if (argc > 2) |
| 1164 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Too many arguments."); |
| 1165 |
|
| 1166 |
chain_invocation = NULL; |
| 1167 |
} |
| 1168 |
|
| 1169 |
r = userdb_by_name(username, /* match= */ NULL, arg_userdb_flags, &ur); |
| 1170 |
if (r == -ESRCH) |
| 1171 |
log_error_errno(r, "User %s does not exist.", username); |
| 1172 |
else if (r == -EHOSTDOWN) |
| 1173 |
log_error_errno(r, "Selected user database service is not available for this request."); |
| 1174 |
else if (r == -EINVAL) |
| 1175 |
log_error_errno(r, "Failed to find user %s: %m (Invalid user name?)", username); |
| 1176 |
else if (r < 0) |
| 1177 |
log_error_errno(r, "Failed to find user %s: %m", username); |
| 1178 |
else { |
| 1179 |
if (strv_isempty(ur->ssh_authorized_keys)) |
| 1180 |
log_debug("User record for %s has no public SSH keys.", username); |
| 1181 |
else |
| 1182 |
STRV_FOREACH(i, ur->ssh_authorized_keys) |
| 1183 |
printf("%s\n", *i); |
| 1184 |
|
| 1185 |
if (ur->incomplete) { |
| 1186 |
fflush(stdout); |
| 1187 |
log_warning("Warning: lacking rights to acquire privileged fields of user record of '%s', output incomplete.", ur->user_name); |
| 1188 |
} |
| 1189 |
} |
| 1190 |
|
| 1191 |
if (chain_invocation) { |
| 1192 |
if (DEBUG_LOGGING) { |
| 1193 |
_cleanup_free_ char *s = NULL; |
| 1194 |
|
| 1195 |
s = quote_command_line(chain_invocation, SHELL_ESCAPE_EMPTY); |
| 1196 |
if (!s) |
| 1197 |
return log_oom(); |
| 1198 |
|
| 1199 |
log_debug("Chain invoking: %s", s); |
| 1200 |
} |
| 1201 |
|
| 1202 |
fflush(stdout); |
| 1203 |
execv(chain_invocation[0], chain_invocation); |
| 1204 |
if (errno == ENOENT) /* Let's handle ENOENT gracefully */ |
| 1205 |
log_warning_errno(errno, "Chain executable '%s' does not exist, ignoring chain invocation.", chain_invocation[0]); |
| 1206 |
else { |
| 1207 |
log_error_errno(errno, "Failed to invoke chain executable '%s': %m", chain_invocation[0]); |
| 1208 |
if (r >= 0) |
| 1209 |
r = -errno; |
| 1210 |
} |
| 1211 |
} |
| 1212 |
|
| 1213 |
return r; |
| 1214 |
} |
| 1215 |
|
| 1216 |
static int write_membership(int dir_fd, const char *dir, const char *user, const char *group) { |
| 1217 |
int r; |
| 1218 |
|
| 1219 |
assert(dir_fd >= 0 || dir_fd == AT_FDCWD); |
| 1220 |
assert(dir); |
| 1221 |
assert(user); |
| 1222 |
assert(group); |
| 1223 |
|
| 1224 |
_cleanup_free_ char *membership = strjoin(user, ":", group, ".membership"); |
| 1225 |
if (!membership) |
| 1226 |
return log_oom(); |
| 1227 |
|
| 1228 |
_cleanup_close_ int fd = openat(dir_fd, membership, O_WRONLY|O_CREAT|O_CLOEXEC, 0644); |
| 1229 |
if (fd < 0) |
| 1230 |
return log_error_errno(errno, "Failed to create %s/%s: %m", dir, membership); |
| 1231 |
|
| 1232 |
r = loop_write(fd, "{}\n", SIZE_MAX); |
| 1233 |
if (r < 0) |
| 1234 |
return log_error_errno(r, "Failed to write empty JSON object into %s/%s: %m", dir, membership); |
| 1235 |
|
| 1236 |
log_info("Installed %s/%s from credential.", dir, membership); |
| 1237 |
|
| 1238 |
return 0; |
| 1239 |
} |
| 1240 |
|
| 1241 |
static int load_credential_one( |
| 1242 |
int credential_dir_fd, |
| 1243 |
const char *name, |
| 1244 |
int *userdb_dir_persist_fd, |
| 1245 |
int *userdb_dir_transient_fd) { |
| 1246 |
|
| 1247 |
int r; |
| 1248 |
|
| 1249 |
assert(credential_dir_fd >= 0); |
| 1250 |
assert(name); |
| 1251 |
assert(userdb_dir_persist_fd); |
| 1252 |
assert(userdb_dir_transient_fd); |
| 1253 |
|
| 1254 |
const char *suffix = startswith(name, "userdb."); |
| 1255 |
if (!suffix) |
| 1256 |
return 0; |
| 1257 |
|
| 1258 |
const char *transient = startswith(suffix, "transient."), |
| 1259 |
*user = startswith(transient ?: suffix, "user."), |
| 1260 |
*group = startswith(transient ?: suffix, "group."); |
| 1261 |
if (!user && !group) |
| 1262 |
return 0; |
| 1263 |
|
| 1264 |
const char *userdb_dir = transient ? "/run/userdb" : "/etc/userdb"; |
| 1265 |
|
| 1266 |
int *userdb_dir_fd = transient ? userdb_dir_transient_fd : userdb_dir_persist_fd; |
| 1267 |
if (*userdb_dir_fd == -EBADF) { |
| 1268 |
*userdb_dir_fd = xopenat_full(AT_FDCWD, userdb_dir, |
| 1269 |
/* open_flags= */ O_DIRECTORY|O_CREAT|O_CLOEXEC, |
| 1270 |
/* xopen_flags= */ XO_LABEL, |
| 1271 |
/* mode= */ 0755); |
| 1272 |
if (*userdb_dir_fd < 0) |
| 1273 |
return log_error_errno(*userdb_dir_fd, "Failed to open '%s/': %m", userdb_dir); |
| 1274 |
} else if (*userdb_dir_fd < 0) |
| 1275 |
return log_debug_errno(*userdb_dir_fd, "Previous attempt to open '%s/' failed, skipping.", userdb_dir); |
| 1276 |
|
| 1277 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 1278 |
unsigned line = 0, column = 0; |
| 1279 |
r = sd_json_parse_file_at(/* f= */ NULL, credential_dir_fd, name, SD_JSON_PARSE_MUST_BE_OBJECT|SD_JSON_PARSE_SENSITIVE, &v, &line, &column); |
| 1280 |
if (r < 0) |
| 1281 |
return log_error_errno(r, "Failed to parse credential '%s' as JSON at %u:%u: %m", name, line, column); |
| 1282 |
|
| 1283 |
_cleanup_(user_record_unrefp) UserRecord *ur = NULL, *ur_stripped = NULL, *ur_privileged = NULL; |
| 1284 |
_cleanup_(group_record_unrefp) GroupRecord *gr = NULL, *gr_stripped = NULL, *gr_privileged = NULL; |
| 1285 |
_cleanup_free_ char *fn = NULL, *link = NULL; |
| 1286 |
|
| 1287 |
if (user) { |
| 1288 |
ur = user_record_new(); |
| 1289 |
if (!ur) |
| 1290 |
return log_oom(); |
| 1291 |
|
| 1292 |
r = user_record_load(ur, v, USER_RECORD_LOAD_MASK_SECRET|USER_RECORD_LOG); |
| 1293 |
if (r < 0) |
| 1294 |
return r; |
| 1295 |
|
| 1296 |
if (user_record_is_root(ur)) |
| 1297 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Creating 'root' user from credentials is not supported."); |
| 1298 |
if (user_record_is_nobody(ur)) |
| 1299 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Creating 'nobody' user from credentials is not supported."); |
| 1300 |
|
| 1301 |
if (!streq_ptr(user, ur->user_name)) |
| 1302 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 1303 |
"Credential suffix '%s' does not match user record name '%s'", |
| 1304 |
user, strna(ur->user_name)); |
| 1305 |
|
| 1306 |
if (!uid_is_valid(ur->uid)) |
| 1307 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "JSON user record missing uid field"); |
| 1308 |
|
| 1309 |
if (!gid_is_valid(user_record_gid(ur))) |
| 1310 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "JSON user record missing gid field"); |
| 1311 |
|
| 1312 |
_cleanup_(user_record_unrefp) UserRecord *m = NULL; |
| 1313 |
r = userdb_by_name(ur->user_name, /* match= */ NULL, USERDB_SUPPRESS_SHADOW, &m); |
| 1314 |
if (r >= 0) { |
| 1315 |
if (m->uid != ur->uid) |
| 1316 |
return log_error_errno(SYNTHETIC_ERRNO(EEXIST), |
| 1317 |
"Cannot create user %s from credential %s as it already exists with UID " UID_FMT " instead of " UID_FMT, |
| 1318 |
ur->user_name, name, m->uid, ur->uid); |
| 1319 |
|
| 1320 |
log_info("User with name %s and UID " UID_FMT " already exists, not creating user from credential %s", ur->user_name, ur->uid, name); |
| 1321 |
return 0; |
| 1322 |
} |
| 1323 |
if (r != -ESRCH) |
| 1324 |
return log_error_errno(r, "Failed to check if user with name %s already exists: %m", ur->user_name); |
| 1325 |
|
| 1326 |
m = user_record_unref(m); |
| 1327 |
r = userdb_by_uid(ur->uid, /* match= */ NULL, USERDB_SUPPRESS_SHADOW, &m); |
| 1328 |
if (r >= 0) { |
| 1329 |
if (!streq_ptr(ur->user_name, m->user_name)) |
| 1330 |
return log_error_errno(SYNTHETIC_ERRNO(EEXIST), |
| 1331 |
"Cannot create user %s from credential %s as UID " UID_FMT " is already assigned to user %s", |
| 1332 |
ur->user_name, name, ur->uid, m->user_name); |
| 1333 |
|
| 1334 |
log_info("User with name %s and UID " UID_FMT " already exists, not creating user from credential %s", ur->user_name, ur->uid, name); |
| 1335 |
return 0; |
| 1336 |
} |
| 1337 |
if (r != -ESRCH) |
| 1338 |
return log_error_errno(r, "Failed to check if user with UID " UID_FMT " already exists: %m", ur->uid); |
| 1339 |
|
| 1340 |
r = user_record_clone(ur, USER_RECORD_LOAD_MASK_PRIVILEGED|USER_RECORD_LOG, &ur_stripped); |
| 1341 |
if (r < 0) |
| 1342 |
return r; |
| 1343 |
|
| 1344 |
r = user_record_clone(ur, USER_RECORD_EXTRACT_PRIVILEGED|USER_RECORD_EMPTY_OK|USER_RECORD_LOG, &ur_privileged); |
| 1345 |
if (r < 0) |
| 1346 |
return r; |
| 1347 |
|
| 1348 |
fn = strjoin(ur->user_name, ".user"); |
| 1349 |
if (!fn) |
| 1350 |
return log_oom(); |
| 1351 |
|
| 1352 |
if (asprintf(&link, UID_FMT ".user", ur->uid) < 0) |
| 1353 |
return log_oom(); |
| 1354 |
} else { |
| 1355 |
assert(group); |
| 1356 |
|
| 1357 |
gr = group_record_new(); |
| 1358 |
if (!gr) |
| 1359 |
return log_oom(); |
| 1360 |
|
| 1361 |
r = group_record_load(gr, v, USER_RECORD_LOAD_MASK_SECRET|USER_RECORD_LOG); |
| 1362 |
if (r < 0) |
| 1363 |
return r; |
| 1364 |
|
| 1365 |
if (group_record_is_root(gr)) |
| 1366 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Creating 'root' group from credentials is not supported."); |
| 1367 |
if (group_record_is_nobody(gr)) |
| 1368 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Creating 'nobody' group from credentials is not supported."); |
| 1369 |
|
| 1370 |
if (!streq_ptr(group, gr->group_name)) |
| 1371 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 1372 |
"Credential suffix '%s' does not match group record name '%s'", |
| 1373 |
group, strna(gr->group_name)); |
| 1374 |
|
| 1375 |
if (!gid_is_valid(gr->gid)) |
| 1376 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "JSON group record missing gid field"); |
| 1377 |
|
| 1378 |
_cleanup_(group_record_unrefp) GroupRecord *m = NULL; |
| 1379 |
r = groupdb_by_name(gr->group_name, /* match= */ NULL, USERDB_SUPPRESS_SHADOW, &m); |
| 1380 |
if (r >= 0) { |
| 1381 |
if (m->gid != gr->gid) |
| 1382 |
return log_error_errno(SYNTHETIC_ERRNO(EEXIST), |
| 1383 |
"Cannot create group %s from credential %s as it already exists with GID " GID_FMT " instead of " GID_FMT, |
| 1384 |
gr->group_name, name, m->gid, gr->gid); |
| 1385 |
|
| 1386 |
log_info("Group with name %s and GID " GID_FMT " already exists, not creating group from credential %s", gr->group_name, gr->gid, name); |
| 1387 |
return 0; |
| 1388 |
} |
| 1389 |
if (r != -ESRCH) |
| 1390 |
return log_error_errno(r, "Failed to check if group with name %s already exists: %m", gr->group_name); |
| 1391 |
|
| 1392 |
m = group_record_unref(m); |
| 1393 |
r = groupdb_by_gid(gr->gid, /* match= */ NULL, USERDB_SUPPRESS_SHADOW, &m); |
| 1394 |
if (r >= 0) { |
| 1395 |
if (!streq_ptr(gr->group_name, m->group_name)) |
| 1396 |
return log_error_errno(SYNTHETIC_ERRNO(EEXIST), |
| 1397 |
"Cannot create group %s from credential %s as GID " GID_FMT " is already assigned to group %s", |
| 1398 |
gr->group_name, name, gr->gid, m->group_name); |
| 1399 |
|
| 1400 |
log_info("Group with name %s and GID " GID_FMT " already exists, not creating group from credential %s", gr->group_name, gr->gid, name); |
| 1401 |
return 0; |
| 1402 |
} |
| 1403 |
if (r != -ESRCH) |
| 1404 |
return log_error_errno(r, "Failed to check if group with GID " GID_FMT " already exists: %m", gr->gid); |
| 1405 |
|
| 1406 |
r = group_record_clone(gr, USER_RECORD_LOAD_MASK_PRIVILEGED|USER_RECORD_LOG, &gr_stripped); |
| 1407 |
if (r < 0) |
| 1408 |
return r; |
| 1409 |
|
| 1410 |
r = group_record_clone(gr, USER_RECORD_EXTRACT_PRIVILEGED|USER_RECORD_EMPTY_OK|USER_RECORD_LOG, &gr_privileged); |
| 1411 |
if (r < 0) |
| 1412 |
return r; |
| 1413 |
|
| 1414 |
fn = strjoin(gr->group_name, ".group"); |
| 1415 |
if (!fn) |
| 1416 |
return log_oom(); |
| 1417 |
|
| 1418 |
if (asprintf(&link, GID_FMT ".group", gr->gid) < 0) |
| 1419 |
return log_oom(); |
| 1420 |
} |
| 1421 |
|
| 1422 |
if (!filename_is_valid(fn)) |
| 1423 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 1424 |
"Passed credential '%s' would result in invalid filename '%s'.", |
| 1425 |
name, fn); |
| 1426 |
|
| 1427 |
_cleanup_free_ char *formatted = NULL; |
| 1428 |
r = sd_json_variant_format(ur ? ur_stripped->json : gr_stripped->json, SD_JSON_FORMAT_NEWLINE, &formatted); |
| 1429 |
if (r < 0) |
| 1430 |
return log_error_errno(r, "Failed to format JSON record: %m"); |
| 1431 |
|
| 1432 |
r = write_string_file_at(*userdb_dir_fd, fn, formatted, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC); |
| 1433 |
if (r < 0) |
| 1434 |
return log_error_errno(r, "Failed to write JSON record to %s/%s: %m", userdb_dir, fn); |
| 1435 |
|
| 1436 |
if (symlinkat(fn, *userdb_dir_fd, link) < 0) |
| 1437 |
return log_error_errno(errno, "Failed to create symlink from %s to %s: %m", link, fn); |
| 1438 |
|
| 1439 |
log_info("Installed %s/%s from credential.", userdb_dir, fn); |
| 1440 |
|
| 1441 |
if ((ur && !sd_json_variant_is_blank_object(ur_privileged->json)) || |
| 1442 |
(gr && !sd_json_variant_is_blank_object(gr_privileged->json))) { |
| 1443 |
fn = mfree(fn); |
| 1444 |
fn = strjoin(ur ? ur->user_name : gr->group_name, ur ? ".user-privileged" : ".group-privileged"); |
| 1445 |
if (!fn) |
| 1446 |
return log_oom(); |
| 1447 |
|
| 1448 |
formatted = mfree(formatted); |
| 1449 |
r = sd_json_variant_format(ur ? ur_privileged->json : gr_privileged->json, SD_JSON_FORMAT_NEWLINE, &formatted); |
| 1450 |
if (r < 0) |
| 1451 |
return log_error_errno(r, "Failed to format JSON record: %m"); |
| 1452 |
|
| 1453 |
r = write_string_file_at(*userdb_dir_fd, fn, formatted, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_MODE_0600); |
| 1454 |
if (r < 0) |
| 1455 |
return log_error_errno(r, "Failed to write JSON record to %s/%s: %m", userdb_dir, fn); |
| 1456 |
|
| 1457 |
link = mfree(link); |
| 1458 |
|
| 1459 |
if (ur) { |
| 1460 |
if (asprintf(&link, UID_FMT ".user-privileged", ur->uid) < 0) |
| 1461 |
return log_oom(); |
| 1462 |
} else { |
| 1463 |
if (asprintf(&link, GID_FMT ".group-privileged", gr->gid) < 0) |
| 1464 |
return log_oom(); |
| 1465 |
} |
| 1466 |
|
| 1467 |
if (symlinkat(fn, *userdb_dir_fd, link) < 0) |
| 1468 |
return log_error_errno(errno, "Failed to create symlink from %s to %s: %m", link, fn); |
| 1469 |
|
| 1470 |
log_info("Installed %s/%s from credential.", userdb_dir, fn); |
| 1471 |
} |
| 1472 |
|
| 1473 |
if (ur) |
| 1474 |
STRV_FOREACH(g, ur->member_of) { |
| 1475 |
r = write_membership(*userdb_dir_fd, userdb_dir, ur->user_name, *g); |
| 1476 |
if (r < 0) |
| 1477 |
return r; |
| 1478 |
} |
| 1479 |
else |
| 1480 |
STRV_FOREACH(u, gr->members) { |
| 1481 |
r = write_membership(*userdb_dir_fd, userdb_dir, *u, gr->group_name); |
| 1482 |
if (r < 0) |
| 1483 |
return r; |
| 1484 |
} |
| 1485 |
|
| 1486 |
if (ur && user_record_disposition(ur) == USER_REGULAR) { |
| 1487 |
const char *hd = user_record_home_directory(ur); |
| 1488 |
|
| 1489 |
r = RET_NERRNO(access(hd, F_OK)); |
| 1490 |
if (r < 0) { |
| 1491 |
if (r != -ENOENT) |
| 1492 |
return log_error_errno(r, "Failed to check if %s exists: %m", hd); |
| 1493 |
|
| 1494 |
WITH_UMASK(0000) { |
| 1495 |
r = mkdir_parents(hd, 0755); |
| 1496 |
if (r < 0) |
| 1497 |
return log_error_errno(r, "Failed to create parent directories of %s: %m", hd); |
| 1498 |
|
| 1499 |
if (mkdir(hd, 0700) < 0 && errno != EEXIST) |
| 1500 |
return log_error_errno(errno, "Failed to create %s: %m", hd); |
| 1501 |
} |
| 1502 |
|
| 1503 |
if (chown(hd, ur->uid, user_record_gid(ur)) < 0) |
| 1504 |
return log_error_errno(errno, "Failed to chown %s: %m", hd); |
| 1505 |
|
| 1506 |
r = copy_tree(user_record_skeleton_directory(ur), hd, ur->uid, user_record_gid(ur), |
| 1507 |
COPY_REFLINK|COPY_MERGE, /* denylist= */ NULL, /* subvolumes= */ NULL); |
| 1508 |
if (r < 0 && r != -ENOENT) |
| 1509 |
return log_error_errno(r, "Failed to copy skeleton directory to %s: %m", hd); |
| 1510 |
} |
| 1511 |
} |
| 1512 |
|
| 1513 |
return 0; |
| 1514 |
} |
| 1515 |
|
| 1516 |
VERB(verb_load_credentials, "load-credentials", NULL, VERB_ANY, 1, 0, |
| 1517 |
"Write static user/group records from credentials"); |
| 1518 |
static int verb_load_credentials(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 1519 |
int r; |
| 1520 |
|
| 1521 |
_cleanup_close_ int credential_dir_fd = open_credentials_dir(); |
| 1522 |
if (IN_SET(credential_dir_fd, -ENXIO, -ENOENT)) { |
| 1523 |
/* Credential env var not set, or dir doesn't exist. */ |
| 1524 |
log_debug("No credentials found."); |
| 1525 |
return 0; |
| 1526 |
} |
| 1527 |
if (credential_dir_fd < 0) |
| 1528 |
return log_error_errno(credential_dir_fd, "Failed to open credentials directory: %m"); |
| 1529 |
|
| 1530 |
_cleanup_free_ DirectoryEntries *des = NULL; |
| 1531 |
r = readdir_all(credential_dir_fd, RECURSE_DIR_SORT|RECURSE_DIR_IGNORE_DOT|RECURSE_DIR_ENSURE_TYPE, &des); |
| 1532 |
if (r < 0) |
| 1533 |
return log_error_errno(r, "Failed to enumerate credentials: %m"); |
| 1534 |
|
| 1535 |
_cleanup_close_ int userdb_persist_dir_fd = -EBADF, userdb_transient_dir_fd = -EBADF; |
| 1536 |
|
| 1537 |
FOREACH_ARRAY(i, des->entries, des->n_entries) { |
| 1538 |
struct dirent *de = *i; |
| 1539 |
|
| 1540 |
if (de->d_type != DT_REG) |
| 1541 |
continue; |
| 1542 |
|
| 1543 |
RET_GATHER(r, load_credential_one( |
| 1544 |
credential_dir_fd, |
| 1545 |
de->d_name, |
| 1546 |
&userdb_persist_dir_fd, |
| 1547 |
&userdb_transient_dir_fd)); |
| 1548 |
} |
| 1549 |
|
| 1550 |
return r; |
| 1551 |
} |
| 1552 |
|
| 1553 |
static int help(void) { |
| 1554 |
_cleanup_(table_unrefp) Table *verbs = NULL, *options = NULL; |
| 1555 |
int r; |
| 1556 |
|
| 1557 |
r = verbs_get_help_table(&verbs); |
| 1558 |
if (r < 0) |
| 1559 |
return r; |
| 1560 |
|
| 1561 |
r = option_parser_get_help_table(&options); |
| 1562 |
if (r < 0) |
| 1563 |
return r; |
| 1564 |
|
| 1565 |
(void) table_sync_column_widths(0, verbs, options); |
| 1566 |
|
| 1567 |
pager_open(arg_pager_flags); |
| 1568 |
|
| 1569 |
help_cmdline("[OPTIONS…] COMMAND …"); |
| 1570 |
help_abstract("Show user and group information."); |
| 1571 |
|
| 1572 |
help_section("Commands"); |
| 1573 |
r = table_print_or_warn(verbs); |
| 1574 |
if (r < 0) |
| 1575 |
return r; |
| 1576 |
|
| 1577 |
help_section("Options"); |
| 1578 |
r = table_print_or_warn(options); |
| 1579 |
if (r < 0) |
| 1580 |
return r; |
| 1581 |
|
| 1582 |
help_man_page_reference("userdbctl", "1"); |
| 1583 |
return 0; |
| 1584 |
} |
| 1585 |
|
| 1586 |
VERB_COMMON_HELP_HIDDEN(help); |
| 1587 |
|
| 1588 |
static int parse_from_file(const char *arg, sd_json_variant **ret) { |
| 1589 |
sd_json_variant *v = NULL; |
| 1590 |
int r; |
| 1591 |
|
| 1592 |
assert(ret); |
| 1593 |
|
| 1594 |
if (!isempty(arg)) { |
| 1595 |
const char *fn = streq(arg, "-") ? NULL : arg; |
| 1596 |
unsigned line = 0; |
| 1597 |
r = sd_json_parse_file( |
| 1598 |
fn ? NULL : stdin, |
| 1599 |
fn ?: "<stdin>", |
| 1600 |
SD_JSON_PARSE_MUST_BE_OBJECT | SD_JSON_PARSE_SENSITIVE, |
| 1601 |
&v, |
| 1602 |
&line, |
| 1603 |
/* reterr_column= */ NULL); |
| 1604 |
if (r < 0) |
| 1605 |
return log_syntax(/* unit= */ NULL, LOG_ERR, fn ?: "<stdin>", line, r, "JSON parse failure."); |
| 1606 |
} |
| 1607 |
|
| 1608 |
*ret = v; |
| 1609 |
return 0; |
| 1610 |
} |
| 1611 |
|
| 1612 |
static int parse_argv(int argc, char *argv[], char ***remaining_args) { |
| 1613 |
const char *e; |
| 1614 |
int r; |
| 1615 |
|
| 1616 |
assert(argc >= 0); |
| 1617 |
assert(argv); |
| 1618 |
assert(remaining_args); |
| 1619 |
|
| 1620 |
/* We are going to update this environment variable with our own, hence let's first read what is already set */ |
| 1621 |
e = getenv("SYSTEMD_ONLY_USERDB"); |
| 1622 |
if (e) { |
| 1623 |
char **l; |
| 1624 |
|
| 1625 |
l = strv_split(e, ":"); |
| 1626 |
if (!l) |
| 1627 |
return log_oom(); |
| 1628 |
|
| 1629 |
strv_free(arg_services); |
| 1630 |
arg_services = l; |
| 1631 |
} |
| 1632 |
|
| 1633 |
OptionParser opts = { argc, argv, OPTION_PARSER_RETURN_POSITIONAL_ARGS }; |
| 1634 |
_cleanup_strv_free_ char **args = NULL; |
| 1635 |
|
| 1636 |
FOREACH_OPTION_OR_RETURN(c, &opts) { |
| 1637 |
switch (c) { |
| 1638 |
|
| 1639 |
OPTION_COMMON_HELP: |
| 1640 |
return help(); |
| 1641 |
|
| 1642 |
OPTION_COMMON_VERSION: |
| 1643 |
return version(); |
| 1644 |
|
| 1645 |
OPTION_COMMON_NO_PAGER: |
| 1646 |
arg_pager_flags |= PAGER_DISABLE; |
| 1647 |
break; |
| 1648 |
|
| 1649 |
OPTION_COMMON_NO_LEGEND: |
| 1650 |
arg_legend = false; |
| 1651 |
break; |
| 1652 |
|
| 1653 |
OPTION_LONG("output", "MODE", |
| 1654 |
"Select output mode (classic, friendly, table, json)"): |
| 1655 |
if (streq(opts.arg, "help")) |
| 1656 |
return DUMP_STRING_TABLE(output, Output, _OUTPUT_MAX); |
| 1657 |
|
| 1658 |
arg_output = output_from_string(opts.arg); |
| 1659 |
if (arg_output < 0) |
| 1660 |
return log_error_errno(arg_output, "Invalid --output= mode: %s", opts.arg); |
| 1661 |
|
| 1662 |
arg_json_format_flags = arg_output == OUTPUT_JSON ? SD_JSON_FORMAT_PRETTY|SD_JSON_FORMAT_COLOR_AUTO : SD_JSON_FORMAT_OFF; |
| 1663 |
break; |
| 1664 |
|
| 1665 |
OPTION_COMMON_JSON: |
| 1666 |
r = parse_json_argument(opts.arg, &arg_json_format_flags); |
| 1667 |
if (r <= 0) |
| 1668 |
return r; |
| 1669 |
|
| 1670 |
arg_output = sd_json_format_enabled(arg_json_format_flags) ? OUTPUT_JSON : _OUTPUT_INVALID; |
| 1671 |
break; |
| 1672 |
|
| 1673 |
OPTION_SHORT('j', NULL, "Equivalent to --output=json"): |
| 1674 |
arg_json_format_flags = SD_JSON_FORMAT_PRETTY|SD_JSON_FORMAT_COLOR_AUTO; |
| 1675 |
arg_output = OUTPUT_JSON; |
| 1676 |
break; |
| 1677 |
|
| 1678 |
OPTION('s', "service", "SERVICE[:SERVICE…]", "Query the specified service"): |
| 1679 |
if (isempty(opts.arg)) |
| 1680 |
arg_services = strv_free(arg_services); |
| 1681 |
else { |
| 1682 |
r = strv_split_and_extend(&arg_services, opts.arg, ":", /* filter_duplicates= */ true); |
| 1683 |
if (r < 0) |
| 1684 |
return log_error_errno(r, "Failed to parse -s/--service= argument: %m"); |
| 1685 |
} |
| 1686 |
|
| 1687 |
break; |
| 1688 |
|
| 1689 |
OPTION_LONG("with-nss", "BOOL", "Control whether to include glibc NSS data"): |
| 1690 |
r = parse_boolean_argument("--with-nss=", opts.arg, NULL); |
| 1691 |
if (r < 0) |
| 1692 |
return r; |
| 1693 |
|
| 1694 |
SET_FLAG(arg_userdb_flags, USERDB_EXCLUDE_NSS, !r); |
| 1695 |
break; |
| 1696 |
|
| 1697 |
OPTION_SHORT('N', NULL, |
| 1698 |
"Do not synthesize or include glibc NSS data " |
| 1699 |
"(Same as --synthesize=no --with-nss=no)"): |
| 1700 |
arg_userdb_flags |= USERDB_EXCLUDE_NSS|USERDB_DONT_SYNTHESIZE_INTRINSIC|USERDB_DONT_SYNTHESIZE_FOREIGN; |
| 1701 |
break; |
| 1702 |
|
| 1703 |
OPTION_LONG("synthesize", "BOOL", "Synthesize root/nobody user"): |
| 1704 |
r = parse_boolean_argument("--synthesize=", opts.arg, NULL); |
| 1705 |
if (r < 0) |
| 1706 |
return r; |
| 1707 |
|
| 1708 |
SET_FLAG(arg_userdb_flags, USERDB_DONT_SYNTHESIZE_INTRINSIC|USERDB_DONT_SYNTHESIZE_FOREIGN, !r); |
| 1709 |
break; |
| 1710 |
|
| 1711 |
OPTION_LONG("with-dropin", "BOOL", "Control whether to include drop-in records"): |
| 1712 |
r = parse_boolean_argument("--with-dropin=", opts.arg, NULL); |
| 1713 |
if (r < 0) |
| 1714 |
return r; |
| 1715 |
|
| 1716 |
SET_FLAG(arg_userdb_flags, USERDB_EXCLUDE_DROPIN, !r); |
| 1717 |
break; |
| 1718 |
|
| 1719 |
OPTION_LONG("with-varlink", "BOOL", "Control whether to talk to services at all"): |
| 1720 |
r = parse_boolean_argument("--with-varlink=", opts.arg, NULL); |
| 1721 |
if (r < 0) |
| 1722 |
return r; |
| 1723 |
|
| 1724 |
SET_FLAG(arg_userdb_flags, USERDB_EXCLUDE_VARLINK, !r); |
| 1725 |
break; |
| 1726 |
|
| 1727 |
OPTION_LONG("multiplexer", "BOOL", "Control whether to use the multiplexer"): |
| 1728 |
r = parse_boolean_argument("--multiplexer=", opts.arg, NULL); |
| 1729 |
if (r < 0) |
| 1730 |
return r; |
| 1731 |
|
| 1732 |
SET_FLAG(arg_userdb_flags, USERDB_AVOID_MULTIPLEXER, !r); |
| 1733 |
break; |
| 1734 |
|
| 1735 |
OPTION_LONG("chain", NULL, "Chain another command"): |
| 1736 |
arg_chain = true; |
| 1737 |
break; |
| 1738 |
|
| 1739 |
OPTION_POSITIONAL: |
| 1740 |
r = strv_extend(&args, opts.arg); |
| 1741 |
if (r < 0) |
| 1742 |
return log_oom(); |
| 1743 |
break; |
| 1744 |
|
| 1745 |
OPTION_LONG("uid-min", "ID", "Filter by minimum UID/GID (default 0)"): |
| 1746 |
r = parse_uid(opts.arg, &arg_uid_min); |
| 1747 |
if (r < 0) |
| 1748 |
return log_error_errno(r, "Failed to parse --uid-min= value: %s", opts.arg); |
| 1749 |
break; |
| 1750 |
|
| 1751 |
OPTION_LONG("uid-max", "ID", "Filter by maximum UID/GID (default 4294967294)"): |
| 1752 |
r = parse_uid(opts.arg, &arg_uid_max); |
| 1753 |
if (r < 0) |
| 1754 |
return log_error_errno(r, "Failed to parse --uid-max= value: %s", opts.arg); |
| 1755 |
break; |
| 1756 |
|
| 1757 |
OPTION_LONG("uuid", "UUID", "Filter by UUID"): |
| 1758 |
r = sd_id128_from_string(opts.arg, &arg_uuid); |
| 1759 |
if (r < 0) |
| 1760 |
return log_error_errno(r, "Failed to parse --uuid= value: %s", opts.arg); |
| 1761 |
break; |
| 1762 |
|
| 1763 |
OPTION('z', "fuzzy", NULL, "Do a fuzzy name search"): |
| 1764 |
arg_fuzzy = true; |
| 1765 |
break; |
| 1766 |
|
| 1767 |
OPTION_LONG("disposition", "VALUE", "Filter by disposition"): { |
| 1768 |
UserDisposition d = user_disposition_from_string(opts.arg); |
| 1769 |
if (d < 0) |
| 1770 |
return log_error_errno(d, "Unknown user disposition: %s", opts.arg); |
| 1771 |
|
| 1772 |
if (arg_disposition_mask == UINT64_MAX) |
| 1773 |
arg_disposition_mask = 0; |
| 1774 |
|
| 1775 |
arg_disposition_mask |= UINT64_C(1) << d; |
| 1776 |
break; |
| 1777 |
} |
| 1778 |
|
| 1779 |
OPTION_SHORT('I', NULL, "Equivalent to --disposition=intrinsic"): |
| 1780 |
if (arg_disposition_mask == UINT64_MAX) |
| 1781 |
arg_disposition_mask = 0; |
| 1782 |
|
| 1783 |
arg_disposition_mask |= UINT64_C(1) << USER_INTRINSIC; |
| 1784 |
break; |
| 1785 |
|
| 1786 |
OPTION_SHORT('S', NULL, "Equivalent to --disposition=system"): |
| 1787 |
if (arg_disposition_mask == UINT64_MAX) |
| 1788 |
arg_disposition_mask = 0; |
| 1789 |
|
| 1790 |
arg_disposition_mask |= UINT64_C(1) << USER_SYSTEM; |
| 1791 |
break; |
| 1792 |
|
| 1793 |
OPTION_SHORT('R', NULL, "Equivalent to --disposition=regular"): |
| 1794 |
if (arg_disposition_mask == UINT64_MAX) |
| 1795 |
arg_disposition_mask = 0; |
| 1796 |
|
| 1797 |
arg_disposition_mask |= UINT64_C(1) << USER_REGULAR; |
| 1798 |
break; |
| 1799 |
|
| 1800 |
OPTION_LONG("boundaries", "BOOL", |
| 1801 |
"Show/hide UID/GID range boundaries in output"): |
| 1802 |
r = parse_boolean_argument("boundaries", opts.arg, &arg_boundaries); |
| 1803 |
if (r < 0) |
| 1804 |
return r; |
| 1805 |
break; |
| 1806 |
|
| 1807 |
OPTION_SHORT('B', NULL, "Equivalent to --boundaries=no"): |
| 1808 |
arg_boundaries = false; |
| 1809 |
break; |
| 1810 |
|
| 1811 |
OPTION('F', "from-file", "PATH", "Read JSON record from file"): { |
| 1812 |
sd_json_variant *v = NULL; /* initialization to appease gcc-14 */ |
| 1813 |
|
| 1814 |
r = parse_from_file(opts.arg, &v); |
| 1815 |
if (r < 0) |
| 1816 |
return r; |
| 1817 |
|
| 1818 |
json_variant_unref_and_replace(arg_from_file, v); |
| 1819 |
break; |
| 1820 |
} |
| 1821 |
} |
| 1822 |
|
| 1823 |
/* When --chain was seen, stop parsing switches after the second positional argument: |
| 1824 |
* [OPTS0…, VERB, OPTS1…, USERNAME, OPTS2…, COMMAND, OPTS3…] |
| 1825 |
* We shall parse OPTS0, OPTS1, OPTS2, but OPTS3 are for COMMAND. |
| 1826 |
* --chain can be anywhere in OPTS0, OPTS1, OPTS2, or first in OPTS3. |
| 1827 |
*/ |
| 1828 |
if (arg_chain && strv_length(args) >= 3) |
| 1829 |
opts.state = OPTION_PARSER_DONE; |
| 1830 |
} |
| 1831 |
|
| 1832 |
if (arg_uid_min > arg_uid_max) |
| 1833 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 1834 |
"Minimum UID/GID " UID_FMT " is above maximum UID/GID " UID_FMT ", refusing.", |
| 1835 |
arg_uid_min, arg_uid_max); |
| 1836 |
|
| 1837 |
/* If not mask was specified, use the all bits on mask */ |
| 1838 |
if (arg_disposition_mask == UINT64_MAX) |
| 1839 |
arg_disposition_mask = USER_DISPOSITION_MASK_ALL; |
| 1840 |
|
| 1841 |
if (arg_from_file) |
| 1842 |
arg_boundaries = false; |
| 1843 |
|
| 1844 |
/* We gathered some positional args in 'args' ourselves. Append the remaining ones. */ |
| 1845 |
if (strv_extend_strv(&args, option_parser_get_args(&opts), /* filter_duplicates= */ false) < 0) |
| 1846 |
return log_oom(); |
| 1847 |
|
| 1848 |
*remaining_args = TAKE_PTR(args); |
| 1849 |
return 1; |
| 1850 |
} |
| 1851 |
|
| 1852 |
static int run(int argc, char *argv[]) { |
| 1853 |
_cleanup_strv_free_ char **args = NULL; |
| 1854 |
int r; |
| 1855 |
|
| 1856 |
log_setup(); |
| 1857 |
|
| 1858 |
r = parse_argv(argc, argv, &args); |
| 1859 |
if (r <= 0) |
| 1860 |
return r; |
| 1861 |
|
| 1862 |
if (arg_services) { |
| 1863 |
_cleanup_free_ char *e = NULL; |
| 1864 |
|
| 1865 |
e = strv_join(arg_services, ":"); |
| 1866 |
if (!e) |
| 1867 |
return log_oom(); |
| 1868 |
|
| 1869 |
r = RET_NERRNO(setenv("SYSTEMD_ONLY_USERDB", e, true)); |
| 1870 |
if (r < 0) |
| 1871 |
return log_error_errno(r, "Failed to set $SYSTEMD_ONLY_USERDB: %m"); |
| 1872 |
|
| 1873 |
log_info("Enabled services: %s", e); |
| 1874 |
} else |
| 1875 |
assert_se(unsetenv("SYSTEMD_ONLY_USERDB") == 0); |
| 1876 |
|
| 1877 |
return dispatch_verb(args, NULL); |
| 1878 |
} |
| 1879 |
|
| 1880 |
DEFINE_MAIN_FUNCTION(run); |