From 0bb4308014d587354d4dbd1cc3a122f0751974b2 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 6 Aug 2020 17:35:34 +0200 Subject: [PATCH] userdb: add "description" field to group records User records have the realname/gecos fields, groups never had that, but it would really be useful to have it, hence let's add it with similar semantics. We enforce the same syntax as for GECOS, since it's better to start with strict rules and losen them later instead of the opposite. --- src/shared/group-record-show.c | 3 +++ src/shared/group-record.c | 2 ++ src/shared/group-record.h | 2 ++ src/shared/user-record.c | 2 +- src/shared/user-record.h | 1 + src/userdb/userdbctl.c | 6 ++++-- 6 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/shared/group-record-show.c b/src/shared/group-record-show.c index d0300e483c..8b59f919fa 100644 --- a/src/shared/group-record-show.c +++ b/src/shared/group-record-show.c @@ -68,6 +68,9 @@ void group_record_show(GroupRecord *gr, bool show_full_user_info) { } } + if (gr->description && !streq(gr->description, gr->group_name)) + printf(" Description: %s\n", gr->description); + if (!strv_isempty(gr->hashed_password)) printf(" Passwords: %zu\n", strv_length(gr->hashed_password)); diff --git a/src/shared/group-record.c b/src/shared/group-record.c index 3c9520693d..d999ff95f8 100644 --- a/src/shared/group-record.c +++ b/src/shared/group-record.c @@ -28,6 +28,7 @@ static GroupRecord *group_record_free(GroupRecord *g) { free(g->group_name); free(g->realm); free(g->group_name_and_realm_auto); + free(g->description); strv_free(g->members); free(g->service); @@ -192,6 +193,7 @@ int group_record_load( static const JsonDispatch group_dispatch_table[] = { { "groupName", JSON_VARIANT_STRING, json_dispatch_user_group_name, offsetof(GroupRecord, group_name), JSON_RELAX}, { "realm", JSON_VARIANT_STRING, json_dispatch_realm, offsetof(GroupRecord, realm), 0 }, + { "description", JSON_VARIANT_STRING, json_dispatch_gecos, offsetof(GroupRecord, description), 0 }, { "disposition", JSON_VARIANT_STRING, json_dispatch_user_disposition, offsetof(GroupRecord, disposition), 0 }, { "service", JSON_VARIANT_STRING, json_dispatch_string, offsetof(GroupRecord, service), JSON_SAFE }, { "lastChangeUSec", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(GroupRecord, last_change_usec), 0 }, diff --git a/src/shared/group-record.h b/src/shared/group-record.h index b72a43e50d..85c91eb1f5 100644 --- a/src/shared/group-record.h +++ b/src/shared/group-record.h @@ -13,6 +13,8 @@ typedef struct GroupRecord { char *realm; char *group_name_and_realm_auto; + char *description; + UserDisposition disposition; uint64_t last_change_usec; diff --git a/src/shared/user-record.c b/src/shared/user-record.c index 16edaa45fa..801de19774 100644 --- a/src/shared/user-record.c +++ b/src/shared/user-record.c @@ -203,7 +203,7 @@ int json_dispatch_realm(const char *name, JsonVariant *variant, JsonDispatchFlag return 0; } -static int json_dispatch_gecos(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +int json_dispatch_gecos(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { char **s = userdata; const char *n; int r; diff --git a/src/shared/user-record.h b/src/shared/user-record.h index 1bfd095d27..39580b6b76 100644 --- a/src/shared/user-record.h +++ b/src/shared/user-record.h @@ -388,6 +388,7 @@ int user_record_test_password_change_required(UserRecord *h); /* The following six are user by group-record.c, that's why we export them here */ int json_dispatch_realm(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata); +int json_dispatch_gecos(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata); int json_dispatch_user_group_list(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata); int json_dispatch_user_disposition(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata); diff --git a/src/userdb/userdbctl.c b/src/userdb/userdbctl.c index c973ee9c01..12c6943ebd 100644 --- a/src/userdb/userdbctl.c +++ b/src/userdb/userdbctl.c @@ -232,6 +232,7 @@ static int show_group(GroupRecord *gr, Table *table) { TABLE_STRING, gr->group_name, TABLE_STRING, user_disposition_to_string(group_record_disposition(gr)), TABLE_GID, gr->gid, + TABLE_STRING, gr->description, TABLE_INT, (int) group_record_disposition(gr)); if (r < 0) return table_log_add_error(r); @@ -255,13 +256,14 @@ static int display_group(int argc, char *argv[], void *userdata) { arg_output = argc > 1 ? OUTPUT_FRIENDLY : OUTPUT_TABLE; if (arg_output == OUTPUT_TABLE) { - table = table_new("name", "disposition", "gid", "disposition-numeric"); + table = table_new("name", "disposition", "gid", "description", "disposition-numeric"); if (!table) return log_oom(); (void) table_set_align_percent(table, table_get_cell(table, 0, 2), 100); + (void) table_set_empty_string(table, "-"); (void) table_set_sort(table, (size_t) 3, (size_t) 2, (size_t) -1); - (void) table_set_display(table, (size_t) 0, (size_t) 1, (size_t) 2, (size_t) -1); + (void) table_set_display(table, (size_t) 0, (size_t) 1, (size_t) 2, (size_t) 3, (size_t) -1); } if (argc > 1) {