From nobody Mon Apr 29 15:20:25 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1490685778786338.46291045447026; Tue, 28 Mar 2017 00:22:58 -0700 (PDT) Received: from localhost ([::1]:50361 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cslSz-0000Y7-DY for importer@patchew.org; Tue, 28 Mar 2017 03:22:57 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56832) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cslRl-0008D2-GK for qemu-devel@nongnu.org; Tue, 28 Mar 2017 03:21:42 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cslRg-0003Xn-H6 for qemu-devel@nongnu.org; Tue, 28 Mar 2017 03:21:41 -0400 Received: from mx1.redhat.com ([209.132.183.28]:51186) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cslRg-0003Xa-8R for qemu-devel@nongnu.org; Tue, 28 Mar 2017 03:21:36 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 3730D7AE85; Tue, 28 Mar 2017 07:21:35 +0000 (UTC) Received: from evilvm.brq.redhat.com (dhcp131-51.brq.redhat.com [10.34.131.51]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5DF547F398; Tue, 28 Mar 2017 07:21:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 3730D7AE85 Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=vfeenstr@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 3730D7AE85 From: Vinzenz 'evilissimo' Feenstra To: qemu-devel@nongnu.org Date: Tue, 28 Mar 2017 09:21:30 +0200 Message-Id: <20170328072130.17142-1-vfeenstr@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Tue, 28 Mar 2017 07:21:35 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v1] qga: Add 'guest-get-users' command X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: mdroth@linux.vnet.ibm.com, Vinzenz Feenstra Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Vinzenz Feenstra A command that will list all currenctly logged in users having running processes. Examples: virsh # qemu-agent-command F25 '{ "execute": "guest-get-users" }' {"return":[{"user":"root"}]} virsh # qemu-agent-command Win2k12r2 '{ "execute": "guest-get-users" }' {"return":[{"domain":"LADIDA","user":"Administrator"}]} Signed-off-by: Vinzenz Feenstra --- qga/commands-posix.c | 35 +++++++++++++++++++++++ qga/commands-win32.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++= ++++ qga/qapi-schema.json | 21 ++++++++++++++ 3 files changed, 136 insertions(+) diff --git a/qga/commands-posix.c b/qga/commands-posix.c index 73d93eb..8cb2094 100644 --- a/qga/commands-posix.c +++ b/qga/commands-posix.c @@ -15,6 +15,7 @@ #include #include #include +#include #include "qga/guest-agent-core.h" #include "qga-qmp-commands.h" #include "qapi/qmp/qerror.h" @@ -2515,3 +2516,37 @@ void ga_command_state_init(GAState *s, GACommandStat= e *cs) ga_command_state_add(cs, NULL, guest_fsfreeze_cleanup); #endif } + +GuestUserList *qmp_guest_get_users(Error **err) +{ + GHashTable *cache =3D g_hash_table_new_full(g_str_hash, g_str_equal, + NULL, NULL); + GuestUserList *head =3D NULL, *cur_item =3D NULL; + setutxent(); + for (;;) { + struct utmpx *user_info =3D getutxent(); + if (user_info =3D=3D NULL) { + break; + } else if (user_info->ut_type !=3D USER_PROCESS) { + continue; + } else if (g_hash_table_contains(cache, user_info->ut_user)) { + continue; + } + + g_hash_table_insert(cache, user_info->ut_user, NULL); + + GuestUserList *item =3D g_new0(GuestUserList, 1); + item->value =3D g_new0(GuestUser, 1); + item->value->user =3D g_strdup(user_info->ut_user); + + if (!cur_item) { + head =3D cur_item =3D item; + } else { + cur_item->next =3D item; + cur_item =3D item; + } + } + endutxent(); + g_hash_table_unref(cache); + return head; +} diff --git a/qga/commands-win32.c b/qga/commands-win32.c index 19d72b2..46c038b 100644 --- a/qga/commands-win32.c +++ b/qga/commands-win32.c @@ -1536,3 +1536,83 @@ void ga_command_state_init(GAState *s, GACommandStat= e *cs) ga_command_state_add(cs, NULL, guest_fsfreeze_cleanup); } } + +GuestUserList *qmp_guest_get_users(Error **err) +{ + GHashTable *cache =3D g_hash_table_new_full(g_str_hash, g_str_equal, + NULL, NULL); + GuestUserList *head =3D NULL, *cur_item =3D NULL; + + LPWKSTA_USER_INFO_1 buffer =3D NULL, iter_buffer =3D 0; + DWORD level =3D 1; /* Level 1 has more information */ + DWORD prefered_max_length =3D MAX_PREFERRED_LENGTH; + DWORD entries_read =3D 0; + DWORD total_entries =3D 0; + DWORD resume_handle =3D 0; + LPWSTR server_name =3D NULL; + NET_API_STATUS result =3D ERROR_MORE_DATA; + + while (result =3D=3D ERROR_MORE_DATA) { + result =3D NetWkstaUserEnum( + server_name, + level, + (LPBYTE *)&buffer, + prefered_max_length, + &entries_read, + &total_entries, + &resume_handle); + + if (result !=3D ERROR_MORE_DATA && result !=3D ERROR_SUCCESS) { + error_setg_win32(err, result, "Failed to enumerate active user= s"); + goto error; + } + + iter_buffer =3D buffer; + DWORD i =3D 0; + for (i =3D 0; i < entries_read; ++i, ++iter_buffer) { + gchar *name =3D g_utf16_to_utf8( + iter_buffer->wkui1_username, + -1, NULL, NULL, NULL + ); + + if (name =3D=3D NULL) { + continue; + } + + if (g_hash_table_contains(cache, name)) { + g_free(name); + continue; + } + + gchar *domain =3D g_utf16_to_utf8( + iter_buffer->wkui1_logon_domain, + -1, NULL, NULL, NULL + ); + + g_hash_table_insert(cache, name, NULL); + GuestUserList *item =3D g_new0(GuestUserList, 1); + item->value =3D g_new0(GuestUser, 1); + item->value->user =3D name; + item->value->domain =3D domain; + item->value->has_domain =3D true; + + if (!cur_item) { + head =3D cur_item =3D item; + } else { + cur_item->next =3D item; + cur_item =3D item; + } + } + } + NetApiBufferFree(buffer); + g_hash_table_unref(cache); + return head; + +error: + if (buffer !=3D NULL) { + NetApiBufferFree(buffer); + } + g_hash_table_unref(cache); + qapi_free_GuestUserList(head); + return NULL; +} diff --git a/qga/qapi-schema.json b/qga/qapi-schema.json index a02dbf2..190e8b4 100644 --- a/qga/qapi-schema.json +++ b/qga/qapi-schema.json @@ -1042,3 +1042,24 @@ 'data': { 'path': 'str', '*arg': ['str'], '*env': ['str'], '*input-data': 'str', '*capture-output': 'bool' }, 'returns': 'GuestExec' } + +## +# @GuestUser: +# @user: Username +# @domain: Logon domain (windows only) +# +# Since: 2.10 +## +{ 'struct': 'GuestUser', + 'data': { 'user': 'str', '*domain': 'str' } } + +## +# @guest-get-users: +# Retrieves a list of currently active user accounts on the VM. +# +# Returns: A unique list of users. +# +# Since: 2.10 +## +{ 'command': 'guest-get-users', + 'returns': ['GuestUser'] } --=20 2.9.3