예제 #1
0
파일: account.c 프로젝트: magcius/cockpit
static gboolean
handle_change_groups (CockpitAccount *object,
                      GDBusMethodInvocation *invocation,
                      const gchar *const *arg_add,
                      const gchar *const *arg_remove)
{
  Account *acc = ACCOUNT (object);

  if (!auth_check_sender_role (invocation, COCKPIT_ROLE_USER_ADMIN))
    return TRUE;

  if (acc->u)
    act_user_change_groups (acc->u, arg_add, arg_remove);

  cockpit_account_complete_change_groups (object, invocation);
  return TRUE;
}
예제 #2
0
파일: account.c 프로젝트: nphilipp/cockpit
static gboolean
handle_change_groups (CockpitAccount *object,
                      GDBusMethodInvocation *invocation,
                      const gchar *const *arg_add,
                      const gchar *const *arg_remove)
{
    Account *acc = ACCOUNT (object);
    GError *error;
    int i, j, ngroups;
    gid_t primary;
    gid_t *groups;
    GString *str;
    struct group *gr;
    const gchar *argv[6];
    gint status;

    if (!auth_check_sender_role (invocation, COCKPIT_ROLE_USER_ADMIN))
        return TRUE;

    ngroups = get_user_groups (acc->u, &primary, &groups);

    str = g_string_new ("");
    for (i = 0; i < ngroups; i++)
    {
        if (groups[i] == primary)
            continue;

        gr = getgrgid_alloc (groups[i]);
        if (gr != NULL)
        {
            for (j = 0; arg_remove[j]; j++)
            {
                if (strcmp (gr->gr_name, arg_remove[j]) == 0)
                    break;
            }
            if (!arg_remove[j])
                g_string_append_printf (str, "%s,", gr->gr_name);
            g_free (gr);
        }
    }
    for (j = 0; arg_add[j]; j++)
        g_string_append_printf (str, "%s,", arg_add[j]);

    /* remove excess comma */
    g_string_truncate (str, str->len - 1);

    g_free (groups);

    argv[0] = "/usr/sbin/usermod";
    argv[1] = "-G";
    argv[2] = str->str;
    argv[3] = "--";
    argv[4] = act_user_get_user_name (acc->u);
    argv[5] = NULL;

    error = NULL;
    if (g_spawn_sync (NULL, (gchar**)argv, NULL, 0, NULL, NULL, NULL, NULL, &status, &error))
        g_spawn_check_exit_status (status, &error);

    if (error)
    {
        g_dbus_method_invocation_return_error (invocation,
                                               COCKPIT_ERROR, COCKPIT_ERROR_FAILED,
                                               "Failed to change user groups via %s: %s", argv[0], error->message);
        g_error_free (error);
    }
    else
    {
        account_update (acc, acc->u);
        cockpit_account_complete_change_groups (object, invocation);
    }

    return TRUE;
}