[PATCH 14/14] virsh-completer: introduce virshPasswordCompleter

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Suggest some passwords to the user.

Signed-off-by: Ján Tomko <jtomko@xxxxxxxxxx>
---
 tools/virsh-completer.c | 58 +++++++++++++++++++++++++++++++++++++++++
 tools/virsh-completer.h |  4 +++
 tools/virsh-domain.c    |  1 +
 3 files changed, 63 insertions(+)

diff --git a/tools/virsh-completer.c b/tools/virsh-completer.c
index 5985f09272..0687670d37 100644
--- a/tools/virsh-completer.c
+++ b/tools/virsh-completer.c
@@ -32,6 +32,7 @@
 #include "virutil.h"
 #include "viralloc.h"
 #include "virmacaddr.h"
+#include "virrandom.h"
 #include "virstring.h"
 #include "virxml.h"
 
@@ -936,3 +937,60 @@ virshDomainDeviceAliasCompleter(vshControl *ctl,
     VIR_STEAL_PTR(ret, tmp);
     return ret;
 }
+
+
+const char *builtin_passwords[] = {
+    "hunter2", /* ******* */
+    "nbusr123", /* Keď nevieš, tak nefušuj */
+    "4ezgi4",
+};
+
+
+char **
+virshPasswordCompleter(vshControl *ctl ATTRIBUTE_UNUSED,
+                       const vshCmd *cmd ATTRIBUTE_UNUSED,
+                       unsigned int flags)
+{
+    VIR_AUTOFREE(char *) base64 = NULL;
+    VIR_AUTOFREE(unsigned char *) rand = NULL;
+    VIR_AUTOSTRINGLIST tmp = NULL;
+    const size_t optimal_passlen = 8; /* ought to be enough */
+    const char *prefix = NULL;
+    const size_t num = 1;
+    char **ret = NULL;
+    size_t missing;
+    size_t i;
+
+    virCheckFlags(0, NULL);
+
+    if (VIR_ALLOC_N(tmp, num + ARRAY_CARDINALITY(builtin_passwords) + 1) < 0)
+        return NULL;
+
+    ignore_value(vshCommandOptStringQuiet(ctl, cmd, "password", &prefix));
+    if (STREQ_NULLABLE(prefix, " "))
+        prefix = NULL;
+
+    missing = optimal_passlen - MIN(strlen(NULLSTR_EMPTY(prefix)), optimal_passlen);
+
+    if (VIR_ALLOC_N(rand, 7) < 0)
+        return NULL;
+
+    if (virRandomBytes(rand, 6) < 0)
+        return NULL;
+
+    if (!(base64 = virStringEncodeBase64(rand, 6)))
+        return NULL;
+
+    base64[missing] = '\0';
+
+    if (virAsprintf(&tmp[0], "%s%s", NULLSTR_EMPTY(prefix), base64) < 0)
+        return NULL;
+
+    for (i = 0; i < ARRAY_CARDINALITY(builtin_passwords); i++) {
+        if (VIR_STRDUP(tmp[i + 1], builtin_passwords[i]) < 0)
+            return NULL;
+    }
+
+    VIR_STEAL_PTR(ret, tmp);
+    return ret;
+}
diff --git a/tools/virsh-completer.h b/tools/virsh-completer.h
index 2e2e1edafb..d47a5f4da6 100644
--- a/tools/virsh-completer.h
+++ b/tools/virsh-completer.h
@@ -110,4 +110,8 @@ char ** virshDomainDeviceAliasCompleter(vshControl *ctl,
 char ** virshCellnoCompleter(vshControl *ctl,
                              const vshCmd *cmd,
                              unsigned int flags);
+
+char ** virshPasswordCompleter(vshControl *ctl,
+                               const vshCmd *cmd,
+                               unsigned int flags);
 #endif /* LIBVIRT_VIRSH_COMPLETER_H */
diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index e8d5404acf..d8978f5bd1 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -5732,6 +5732,7 @@ static const vshCmdOptDef opts_set_user_password[] = {
     {.name = "password",
      .type = VSH_OT_DATA,
      .flags = VSH_OFLAG_REQ,
+     .completer = virshPasswordCompleter,
      .help = N_("the new password")
     },
     {.name = "encrypted",
-- 
2.20.1

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux