From c85b41d3f82a01ec8bea48b67e1220e81116f456 Mon Sep 17 00:00:00 2001 From: Ray Strode Date: Wed, 29 Jun 2016 10:50:37 -0400 Subject: [PATCH] user: check if user is in wheel more efficiently We currently get all the groups a user belongs to in one pass, then check each one to see if it's wheel. It's much more efficient to just get the wheel group and check if any of its members are the user. https://bugs.freedesktop.org/show_bug.cgi?id=48177 --- src/user.c | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/src/user.c b/src/user.c index 0fb1a17..2cf4450 100644 --- a/src/user.c +++ b/src/user.c @@ -95,88 +95,79 @@ struct User { gchar *language; gchar *x_session; gchar *location; guint64 login_frequency; gint64 login_time; GVariant *login_history; gchar *icon_file; gchar *default_icon_file; gboolean locked; gboolean automatic_login; gboolean system_account; gboolean local_account; guint *extension_ids; guint n_extension_ids; }; typedef struct UserClass { AccountsUserSkeletonClass parent_class; } UserClass; static void user_accounts_user_iface_init (AccountsUserIface *iface); G_DEFINE_TYPE_WITH_CODE (User, user, ACCOUNTS_TYPE_USER_SKELETON, G_IMPLEMENT_INTERFACE (ACCOUNTS_TYPE_USER, user_accounts_user_iface_init)); static gint account_type_from_pwent (struct passwd *pwent) { struct group *grp; - gid_t wheel; - gid_t *groups; - gint ngroups; gint i; if (pwent->pw_uid == 0) { g_debug ("user is root so account type is administrator"); return ACCOUNT_TYPE_ADMINISTRATOR; } grp = getgrnam (ADMIN_GROUP); if (grp == NULL) { g_debug (ADMIN_GROUP " group not found"); return ACCOUNT_TYPE_STANDARD; } - wheel = grp->gr_gid; - ngroups = get_user_groups (pwent->pw_name, pwent->pw_gid, &groups); - - for (i = 0; i < ngroups; i++) { - if (groups[i] == wheel) { - g_free (groups); + for (i = 0; grp->gr_mem[i] != NULL; i++) { + if (g_strcmp0 (grp->gr_mem[i], pwent->pw_name) == 0) { return ACCOUNT_TYPE_ADMINISTRATOR; } } - g_free (groups); - return ACCOUNT_TYPE_STANDARD; } void user_update_from_pwent (User *user, struct passwd *pwent) { #ifdef HAVE_SHADOW_H struct spwd *spent; #endif gchar *real_name; gboolean changed; const gchar *passwd; gboolean locked; PasswordMode mode; AccountType account_type; g_object_freeze_notify (G_OBJECT (user)); changed = FALSE; if (pwent->pw_gecos && pwent->pw_gecos[0] != '\0') { gchar *first_comma = NULL; gchar *valid_utf8_name = NULL; if (g_utf8_validate (pwent->pw_gecos, -1, NULL)) { valid_utf8_name = pwent->pw_gecos; first_comma = g_utf8_strchr (valid_utf8_name, -1, ','); } else { -- 2.7.4