From patchwork Tue Jun 30 14:37:13 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Marc-Andr=C3=A9_Lureau?= X-Patchwork-Id: 489743 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 24F181402CC for ; Wed, 1 Jul 2015 00:38:06 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b=OJvUWo6Q; dkim-atps=neutral Received: from localhost ([::1]:47351 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z9wfk-0008Tn-Dg for incoming@patchwork.ozlabs.org; Tue, 30 Jun 2015 10:38:04 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49602) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z9wfA-0007b6-9w for qemu-devel@nongnu.org; Tue, 30 Jun 2015 10:37:29 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Z9wf5-0006TI-9x for qemu-devel@nongnu.org; Tue, 30 Jun 2015 10:37:28 -0400 Received: from mail-qg0-x233.google.com ([2607:f8b0:400d:c04::233]:32961) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z9wf5-0006TE-55 for qemu-devel@nongnu.org; Tue, 30 Jun 2015 10:37:23 -0400 Received: by qgem68 with SMTP id m68so4747646qge.0 for ; Tue, 30 Jun 2015 07:37:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:mime-version:content-type :content-transfer-encoding; bh=liXxBydryjdxCe2a8LlZ71CmaTEifG2vlza2XvllDlo=; b=OJvUWo6Q56dwrWTeObXn/kJ9aIkBzbX0el9mB8ouuhSRU2xIWlxWyp7FgtJMNxvDg7 fNFDaO9xX5S1AeUReEv7YYIRchJq/USMS9HyQOULC4eFYl+7HxWSLb9gjL12D7qHR9ly t/YkBvSIsdVzUuyYCS9p+guOI28dceipEzO9N//qbw8syzuQX/qDQN1tYoulzE8weMvq z5YcVAiz1cNZyZfrsOuUTUnjvNqDCSPOfLcsQIqwTb6jAgCWk/TO6zWJYIpLfsl1VU5j 4TTkIde/gKsMJo0aR+NUt+Q0zDkMdZeiEsGL50gMfb/vF7/18foq9PGlo7t21w141PQL CXbg== X-Received: by 10.140.152.72 with SMTP id 69mr27707721qhy.85.1435675042760; Tue, 30 Jun 2015 07:37:22 -0700 (PDT) Received: from localhost (bne75-h02-31-39-163-232.dsl.sta.abo.bbox.fr. [31.39.163.232]) by mx.google.com with ESMTPSA id j62sm13091619qhc.33.2015.06.30.07.37.21 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 30 Jun 2015 07:37:21 -0700 (PDT) From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= To: qemu-devel@nongnu.org Date: Tue, 30 Jun 2015 16:37:13 +0200 Message-Id: <1435675033-9926-1-git-send-email-marcandre.lureau@gmail.com> X-Mailer: git-send-email 2.4.3 MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2607:f8b0:400d:c04::233 Cc: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , mdroth@linux.vnet.ibm.com Subject: [Qemu-devel] [PATCH] qemu-ga: implement win32 guest-set-user-password X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Use NetUserSetInfo() to set the user password. This function is notoriously known to be problematic for users with EFS encrypted files. But the alternative, NetUserChangePassword() requires the old password. Nevertheless, The EFS file should be recovered by changing back to the old password. Signed-off-by: Marc-André Lureau Reviewed-by: Daniel P. Berrange --- configure | 2 +- qga/commands-win32.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 76 insertions(+), 3 deletions(-) diff --git a/configure b/configure index 3063739..400de0d 100755 --- a/configure +++ b/configure @@ -732,7 +732,7 @@ if test "$mingw32" = "yes" ; then sysconfdir="\${prefix}" local_statedir= confsuffix="" - libs_qga="-lws2_32 -lwinmm -lpowrprof $libs_qga" + libs_qga="-lws2_32 -lwinmm -lpowrprof -lnetapi32 $libs_qga" fi werror="" diff --git a/qga/commands-win32.c b/qga/commands-win32.c index fbddc8b..d31530c 100644 --- a/qga/commands-win32.c +++ b/qga/commands-win32.c @@ -16,6 +16,8 @@ #include #include #include +#include + #include "qga/guest-agent-core.h" #include "qga/vss-win32.h" #include "qga-qmp-commands.h" @@ -676,12 +678,84 @@ int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp) return -1; } +static gchar * +get_net_error_message(gint error) +{ + HMODULE module = NULL; + gchar *retval = NULL; + wchar_t *msg = NULL; + int flags, nchars; + + flags = FORMAT_MESSAGE_ALLOCATE_BUFFER + |FORMAT_MESSAGE_IGNORE_INSERTS + |FORMAT_MESSAGE_FROM_SYSTEM; + + if (error >= NERR_BASE && error <= MAX_NERR) { + module = LoadLibraryExW(L"netmsg.dll", NULL, LOAD_LIBRARY_AS_DATAFILE); + + if (module != NULL) { + flags |= FORMAT_MESSAGE_FROM_HMODULE; + } + } + + FormatMessageW(flags, module, error, 0, (LPWSTR)&msg, 0, NULL); + + if (msg != NULL) { + nchars = wcslen(msg); + + if (nchars > 2 && msg[nchars-1] == '\n' && msg[nchars-2] == '\r') { + msg[nchars-2] = '\0'; + } + + retval = g_utf16_to_utf8(msg, -1, NULL, NULL, NULL); + + LocalFree(msg); + } + + if (module != NULL) { + FreeLibrary(module); + } + + return retval; +} + void qmp_guest_set_user_password(const char *username, const char *password, bool crypted, Error **errp) { - error_setg(errp, QERR_UNSUPPORTED); + NET_API_STATUS nas; + char *rawpasswddata = NULL; + size_t rawpasswdlen; + wchar_t *user, *wpass; + USER_INFO_1003 pi1003 = { 0, }; + + if (crypted) { + error_setg(errp, QERR_UNSUPPORTED); + return; + } + + rawpasswddata = (char *)g_base64_decode(password, &rawpasswdlen); + rawpasswddata = g_renew(char, rawpasswddata, rawpasswdlen + 1); + rawpasswddata[rawpasswdlen] = '\0'; + + user = g_utf8_to_utf16(username, -1, NULL, NULL, NULL); + wpass = g_utf8_to_utf16(rawpasswddata, -1, NULL, NULL, NULL); + + pi1003.usri1003_password = wpass; + nas = NetUserSetInfo(NULL, user, + 1003, (LPBYTE)&pi1003, + NULL); + + if (nas != NERR_Success) { + gchar *msg = get_net_error_message(nas); + error_setg(errp, "failed to set password: %s", msg); + g_free(msg); + } + + g_free(user); + g_free(wpass); + g_free(rawpasswddata); } GuestMemoryBlockList *qmp_guest_get_memory_blocks(Error **errp) @@ -709,7 +783,6 @@ GList *ga_command_blacklist_init(GList *blacklist) const char *list_unsupported[] = { "guest-suspend-hybrid", "guest-network-get-interfaces", "guest-get-vcpus", "guest-set-vcpus", - "guest-set-user-password", "guest-get-memory-blocks", "guest-set-memory-blocks", "guest-get-memory-block-size", "guest-fsfreeze-freeze-list", "guest-get-fsinfo",