qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Vinzenz 'evilissimo' Feenstra <vfeenstr@redhat.com>
To: qemu-devel@nongnu.org
Cc: mdroth@linux.vnet.ibm.com, Vinzenz Feenstra <vfeenstr@redhat.com>
Subject: [Qemu-devel] [PATCH v1] qga: Add 'guest-get-users' command
Date: Tue, 28 Mar 2017 09:21:30 +0200	[thread overview]
Message-ID: <20170328072130.17142-1-vfeenstr@redhat.com> (raw)

From: Vinzenz Feenstra <vfeenstr@redhat.com>

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 <vfeenstr@redhat.com>
---
 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 <sys/ioctl.h>
 #include <sys/wait.h>
 #include <dirent.h>
+#include <utmpx.h>
 #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, GACommandState *cs)
     ga_command_state_add(cs, NULL, guest_fsfreeze_cleanup);
 #endif
 }
+
+GuestUserList *qmp_guest_get_users(Error **err)
+{
+    GHashTable *cache = g_hash_table_new_full(g_str_hash, g_str_equal,
+                                              NULL, NULL);
+    GuestUserList *head = NULL, *cur_item = NULL;
+    setutxent();
+    for (;;) {
+        struct utmpx *user_info = getutxent();
+        if (user_info == NULL) {
+            break;
+        } else if (user_info->ut_type != 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 = g_new0(GuestUserList, 1);
+        item->value = g_new0(GuestUser, 1);
+        item->value->user = g_strdup(user_info->ut_user);
+
+        if (!cur_item) {
+            head = cur_item = item;
+        } else {
+            cur_item->next = item;
+            cur_item = 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, GACommandState *cs)
         ga_command_state_add(cs, NULL, guest_fsfreeze_cleanup);
     }
 }
+
+GuestUserList *qmp_guest_get_users(Error **err)
+{
+    GHashTable *cache = g_hash_table_new_full(g_str_hash, g_str_equal,
+                                              NULL, NULL);
+    GuestUserList *head = NULL, *cur_item = NULL;
+
+    LPWKSTA_USER_INFO_1 buffer = NULL, iter_buffer = 0;
+    DWORD level = 1; /* Level 1 has more information */
+    DWORD prefered_max_length = MAX_PREFERRED_LENGTH;
+    DWORD entries_read = 0;
+    DWORD total_entries = 0;
+    DWORD resume_handle = 0;
+    LPWSTR server_name = NULL;
+    NET_API_STATUS result = ERROR_MORE_DATA;
+
+    while (result == ERROR_MORE_DATA) {
+        result = NetWkstaUserEnum(
+            server_name,
+            level,
+            (LPBYTE *)&buffer,
+            prefered_max_length,
+            &entries_read,
+            &total_entries,
+            &resume_handle);
+
+        if (result != ERROR_MORE_DATA && result != ERROR_SUCCESS) {
+            error_setg_win32(err, result, "Failed to enumerate active users");
+            goto error;
+        }
+
+        iter_buffer = buffer;
+        DWORD i = 0;
+        for (i = 0; i < entries_read; ++i, ++iter_buffer) {
+            gchar *name = g_utf16_to_utf8(
+                iter_buffer->wkui1_username,
+                -1, NULL, NULL, NULL
+            );
+
+            if (name == NULL) {
+                continue;
+            }
+
+            if (g_hash_table_contains(cache, name)) {
+                g_free(name);
+                continue;
+            }
+
+            gchar *domain = g_utf16_to_utf8(
+                iter_buffer->wkui1_logon_domain,
+                -1, NULL, NULL, NULL
+            );
+
+            g_hash_table_insert(cache, name, NULL);
+            GuestUserList *item = g_new0(GuestUserList, 1);
+            item->value = g_new0(GuestUser, 1);
+            item->value->user = name;
+            item->value->domain = domain;
+            item->value->has_domain = true;
+
+            if (!cur_item) {
+                head = cur_item = item;
+            } else {
+                cur_item->next = item;
+                cur_item = item;
+            }
+        }
+    }
+    NetApiBufferFree(buffer);
+    g_hash_table_unref(cache);
+    return head;
+
+error:
+    if (buffer != 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'] }
-- 
2.9.3

             reply	other threads:[~2017-03-28  7:21 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-28  7:21 Vinzenz 'evilissimo' Feenstra [this message]
2017-03-28  9:34 ` [Qemu-devel] [PATCH v1] qga: Add 'guest-get-users' command Marc-André Lureau
2017-03-28 13:12   ` Eric Blake

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170328072130.17142-1-vfeenstr@redhat.com \
    --to=vfeenstr@redhat.com \
    --cc=mdroth@linux.vnet.ibm.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).