]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
qemu: add set_password and expire_password monitor commands
authorMarc-André Lureau <marcandre.lureau@redhat.com>
Mon, 10 Jan 2011 11:12:32 +0000 (12:12 +0100)
committerEric Blake <eblake@redhat.com>
Fri, 14 Jan 2011 19:35:42 +0000 (12:35 -0700)
AUTHORS
src/qemu/qemu_monitor.c
src/qemu/qemu_monitor.h
src/qemu/qemu_monitor_json.c
src/qemu/qemu_monitor_json.h
src/qemu/qemu_monitor_text.c
src/qemu/qemu_monitor_text.h

diff --git a/AUTHORS b/AUTHORS
index 721b8f827c370e824ca16383cd4f9f428e7301de..c704a416264b8fe8ad2a9c2b8077e1aa57d37068 100644 (file)
--- a/AUTHORS
+++ b/AUTHORS
@@ -143,6 +143,7 @@ Patches have also been contributed by:
   Roopa Prabhu         <roprabhu@cisco.com>
   Paweł Krześniak      <pawel.krzesniak@gmail.com>
   Kay Schubert         <kayegypt@web.de>
+  Marc-André Lureau    <marcandre.lureau@redhat.com>
 
   [....send patches to get your name here....]
 
index 055e7cee70f83cad4b3350a0b922a35c47d07aa7..302673320b972dfa73d9aa76e5ff9cf92767a764 100644 (file)
@@ -1097,6 +1097,83 @@ int qemuMonitorSetVNCPassword(qemuMonitorPtr mon,
     return ret;
 }
 
+static const char* qemuMonitorTypeToProtocol(int type)
+{
+    switch (type) {
+    case VIR_DOMAIN_GRAPHICS_TYPE_VNC:
+        return "vnc";
+    case VIR_DOMAIN_GRAPHICS_TYPE_SPICE:
+        return "spice";
+    default:
+        qemuReportError(VIR_ERR_INVALID_ARG,
+                        _("unsupported protocol type %s"),
+                        virDomainGraphicsTypeToString(type));
+        return NULL;
+    }
+}
+
+/* Returns -2 if not supported with this monitor connection */
+int qemuMonitorSetPassword(qemuMonitorPtr mon,
+                           int type,
+                           const char *password,
+                           const char *action_if_connected)
+{
+    const char *protocol = qemuMonitorTypeToProtocol(type);
+    int ret;
+
+    if (!protocol)
+        return -1;
+
+    DEBUG("mon=%p, protocol=%s, password=%p, action_if_connected=%s",
+          mon, protocol, password, action_if_connected);
+
+    if (!mon) {
+        qemuReportError(VIR_ERR_INVALID_ARG, "%s",
+                        _("monitor must not be NULL"));
+        return -1;
+    }
+
+    if (!password)
+        password = "";
+
+    if (!action_if_connected)
+        action_if_connected = "keep";
+
+    if (mon->json)
+        ret = qemuMonitorJSONSetPassword(mon, protocol, password, action_if_connected);
+    else
+        ret = qemuMonitorTextSetPassword(mon, protocol, password, action_if_connected);
+    return ret;
+}
+
+int qemuMonitorExpirePassword(qemuMonitorPtr mon,
+                              int type,
+                              const char *expire_time)
+{
+    const char *protocol = qemuMonitorTypeToProtocol(type);
+    int ret;
+
+    if (!protocol)
+        return -1;
+
+    DEBUG("mon=%p, protocol=%s, expire_time=%s",
+          mon, protocol, expire_time);
+
+    if (!mon) {
+        qemuReportError(VIR_ERR_INVALID_ARG, "%s",
+                        _("monitor must not be NULL"));
+        return -1;
+    }
+
+    if (!expire_time)
+        expire_time = "now";
+
+    if (mon->json)
+        ret = qemuMonitorJSONExpirePassword(mon, protocol, expire_time);
+    else
+        ret = qemuMonitorTextExpirePassword(mon, protocol, expire_time);
+    return ret;
+}
 
 int qemuMonitorSetBalloon(qemuMonitorPtr mon,
                           unsigned long newmem)
index 718ea135bf751c4dba832f9c1c66c39e8b6511b8..92c550bc187491786d65674487b3f40786d3a598 100644 (file)
@@ -195,6 +195,13 @@ int qemuMonitorGetBlockExtent(qemuMonitorPtr mon,
 
 int qemuMonitorSetVNCPassword(qemuMonitorPtr mon,
                               const char *password);
+int qemuMonitorSetPassword(qemuMonitorPtr mon,
+                           int type,
+                           const char *password,
+                           const char *action_if_connected);
+int qemuMonitorExpirePassword(qemuMonitorPtr mon,
+                              int type,
+                              const char *expire_time);
 int qemuMonitorSetBalloon(qemuMonitorPtr mon,
                           unsigned long newmem);
 int qemuMonitorSetCPU(qemuMonitorPtr mon, int cpu, int online);
index 7877731af8dd4a9f6fa3a387fccb391c634fe192..7387089aff4467c1338202f0a094389e4e5fc92f 100644 (file)
@@ -1265,6 +1265,62 @@ int qemuMonitorJSONSetVNCPassword(qemuMonitorPtr mon,
     return ret;
 }
 
+/* Returns -1 on error, -2 if not supported */
+int qemuMonitorJSONSetPassword(qemuMonitorPtr mon,
+                               const char *protocol,
+                               const char *password,
+                               const char *action_if_connected)
+{
+    int ret;
+    virJSONValuePtr cmd = qemuMonitorJSONMakeCommand("set_password",
+                                                     "s:protocol", protocol,
+                                                     "s:password", password,
+                                                     "s:connected", action_if_connected,
+                                                     NULL);
+    virJSONValuePtr reply = NULL;
+    if (!cmd)
+        return -1;
+
+    ret = qemuMonitorJSONCommand(mon, cmd, &reply);
+
+    if (ret == 0) {
+        if (qemuMonitorJSONHasError(reply, "CommandNotFound")) {
+            ret = -2;
+            goto cleanup;
+        }
+
+        ret = qemuMonitorJSONCheckError(cmd, reply);
+    }
+
+cleanup:
+    virJSONValueFree(cmd);
+    virJSONValueFree(reply);
+    return ret;
+}
+
+int qemuMonitorJSONExpirePassword(qemuMonitorPtr mon,
+                                  const char *protocol,
+                                  const char *expire_time)
+{
+    int ret;
+    virJSONValuePtr cmd = qemuMonitorJSONMakeCommand("expire_password",
+                                                     "s:protocol", protocol,
+                                                     "s:time", expire_time,
+                                                     NULL);
+    virJSONValuePtr reply = NULL;
+    if (!cmd)
+        return -1;
+
+    ret = qemuMonitorJSONCommand(mon, cmd, &reply);
+
+    if (ret == 0)
+        ret = qemuMonitorJSONCheckError(cmd, reply);
+
+    virJSONValueFree(cmd);
+    virJSONValueFree(reply);
+    return ret;
+}
+
 /*
  * Returns: 0 if balloon not supported, +1 if balloon adjust worked
  * or -1 on failure
index 8d9614632179c68eb8022009b94f4fdcb7bbfb26..4c47f109db32e5ce77045b9c6315fa9bdb73ec31 100644 (file)
@@ -63,6 +63,13 @@ int qemuMonitorJSONGetBlockExtent(qemuMonitorPtr mon,
 
 int qemuMonitorJSONSetVNCPassword(qemuMonitorPtr mon,
                                   const char *password);
+int qemuMonitorJSONSetPassword(qemuMonitorPtr mon,
+                               const char *protocol,
+                               const char *password,
+                               const char *action_if_connected);
+int qemuMonitorJSONExpirePassword(qemuMonitorPtr mon,
+                                  const char *protocol,
+                                  const char *expire_time);
 int qemuMonitorJSONSetBalloon(qemuMonitorPtr mon,
                               unsigned long newmem);
 int qemuMonitorJSONSetCPU(qemuMonitorPtr mon, int cpu, int online);
index 11a9391510fec56349032bc0f42219e75da3957d..291d958135e361199ecbfddc3723bf55ca410d42 100644 (file)
@@ -768,6 +768,75 @@ int qemuMonitorTextSetVNCPassword(qemuMonitorPtr mon,
     return 0;
 }
 
+/* Returns -1 on error, -2 if not supported */
+int qemuMonitorTextSetPassword(qemuMonitorPtr mon,
+                               const char *protocol,
+                               const char *password,
+                               const char *action_if_connected)
+{
+    char *cmd = NULL;
+    char *reply = NULL;
+    int ret = -1;
+
+    if (virAsprintf(&cmd, "set_password %s \"%s\" %s",
+                    protocol, password, action_if_connected) < 0) {
+        virReportOOMError();
+        goto cleanup;
+    }
+
+    if (qemuMonitorCommand(mon, cmd, &reply) < 0) {
+        qemuReportError(VIR_ERR_OPERATION_FAILED,
+                        "%s", _("setting password failed"));
+        goto cleanup;
+    }
+
+    if (strstr(reply, "unknown command:")) {
+        ret = -2;
+        goto cleanup;
+    }
+
+    ret = 0;
+
+cleanup:
+    VIR_FREE(reply);
+    VIR_FREE(cmd);
+    return ret;
+}
+
+int qemuMonitorTextExpirePassword(qemuMonitorPtr mon,
+                                  const char *protocol,
+                                  const char *expire_time)
+{
+    char *cmd = NULL;
+    char *reply = NULL;
+    int ret = -1;
+
+    if (virAsprintf(&cmd, "expire_password %s %s",
+                    protocol, expire_time) < 0) {
+        virReportOOMError();
+        goto cleanup;
+    }
+
+    if (qemuMonitorCommand(mon, cmd, &reply) < 0) {
+        qemuReportError(VIR_ERR_OPERATION_FAILED,
+                        "%s", _("expiring password failed"));
+        goto cleanup;
+    }
+
+    if (strstr(reply, "unknown command:")) {
+        qemuReportError(VIR_ERR_NO_SUPPORT,
+                        _("expiring password not supported by this qemu: %s"), reply);
+        goto cleanup;
+    }
+
+    ret = 0;
+
+cleanup:
+    VIR_FREE(reply);
+    VIR_FREE(cmd);
+    return ret;
+}
+
 /*
  * Returns: 0 if balloon not supported, +1 if balloon adjust worked
  * or -1 on failure
index 57d6e9b24a57ff6a872e47d35ea1786342f83033..b29dbcc846ef3f2b3d5d64e8c7d766c7d7732bb8 100644 (file)
@@ -61,6 +61,13 @@ int qemuMonitorTextGetBlockExtent(qemuMonitorPtr mon,
 
 int qemuMonitorTextSetVNCPassword(qemuMonitorPtr mon,
                                   const char *password);
+int qemuMonitorTextSetPassword(qemuMonitorPtr mon,
+                               const char *protocol,
+                               const char *password,
+                               const char *action_if_connected);
+int qemuMonitorTextExpirePassword(qemuMonitorPtr mon,
+                                  const char *protocol,
+                                  const char *expire_time);
 int qemuMonitorTextSetBalloon(qemuMonitorPtr mon,
                               unsigned long newmem);
 int qemuMonitorTextSetCPU(qemuMonitorPtr mon, int cpu, int online);