From: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
To: qemu-devel@nongnu.org
Cc: den@virtuozzo.com, michael.roth@amd.com, kkostiuk@redhat.com,
marcandre.lureau@gmail.com
Subject: [PATCH 4/6] qga: Add user creation functionality
Date: Wed, 25 Oct 2023 16:00:56 +0200 [thread overview]
Message-ID: <20231025140058.113376-5-alexander.ivanov@virtuozzo.com> (raw)
In-Reply-To: <20231025140058.113376-1-alexander.ivanov@virtuozzo.com>
Add an optional argument "create" to guest-set-user-password command to
create a user with provided username and password.
Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
---
qga/commands-posix.c | 19 +++++++++++++++++++
qga/commands-win32.c | 22 ++++++++++++++++++++++
qga/qapi-schema.json | 5 ++++-
3 files changed, 45 insertions(+), 1 deletion(-)
diff --git a/qga/commands-posix.c b/qga/commands-posix.c
index 461b4d7bb6..26711a1a72 100644
--- a/qga/commands-posix.c
+++ b/qga/commands-posix.c
@@ -2189,6 +2189,7 @@ out:
void qmp_guest_set_user_password(const char *username,
const char *password,
bool crypted,
+ bool has_create, bool create,
Error **errp)
{
char *passwd_path = NULL;
@@ -2227,6 +2228,24 @@ void qmp_guest_set_user_password(const char *username,
goto out;
}
+ /* create new user if requested */
+ if (has_create && create) {
+ char *str = g_shell_quote(username);
+ char *cmd = g_strdup_printf(
+ /* we want output only from useradd command */
+ "id -u %s >/dev/null 2>&1 || useradd -m %s",
+ str, str);
+ const char *argv[] = {
+ "/bin/sh", "-c", cmd, NULL
+ };
+ run_command(argv, NULL, errp);
+ g_free(str);
+ g_free(cmd);
+ if (*errp) {
+ goto out;
+ }
+ }
+
const char *argv[] = {
#ifdef __FreeBSD__
passwd_path, "pw", "usermod", "-n", username,
diff --git a/qga/commands-win32.c b/qga/commands-win32.c
index 5aa43a9ed7..618d862c00 100644
--- a/qga/commands-win32.c
+++ b/qga/commands-win32.c
@@ -1921,6 +1921,7 @@ get_net_error_message(gint error)
void qmp_guest_set_user_password(const char *username,
const char *password,
bool crypted,
+ bool has_create, bool create,
Error **errp)
{
NET_API_STATUS nas;
@@ -1952,6 +1953,27 @@ void qmp_guest_set_user_password(const char *username,
goto done;
}
+ if (has_create && create) {
+ USER_INFO_1 ui = { 0 };
+
+ ui.usri1_name = user;
+ ui.usri1_password = wpass;
+ ui.usri1_priv = USER_PRIV_USER;
+ ui.usri1_flags = UF_SCRIPT | UF_DONT_EXPIRE_PASSWD;
+ nas = NetUserAdd(NULL, 1, (LPBYTE) & ui, NULL);
+
+ if (nas == NERR_Success) {
+ goto done;
+ }
+
+ if (nas != NERR_UserExists) {
+ gchar *msg = get_net_error_message(nas);
+ error_setg(errp, "failed to add user: %s", msg);
+ g_free(msg);
+ goto done;
+ }
+ }
+
pi1003.usri1003_password = wpass;
nas = NetUserSetInfo(NULL, user,
1003, (LPBYTE)&pi1003,
diff --git a/qga/qapi-schema.json b/qga/qapi-schema.json
index b39be4cdc2..e96d463639 100644
--- a/qga/qapi-schema.json
+++ b/qga/qapi-schema.json
@@ -1059,6 +1059,8 @@
# @password: the new password entry string, base64 encoded
#
# @crypted: true if password is already crypt()d, false if raw
+# @create: #optinal user will be created if it does not exist yet.
+# The default value is false. (since 8.2)
#
# If the @crypted flag is true, it is the caller's responsibility to
# ensure the correct crypt() encryption scheme is used. This command
@@ -1078,7 +1080,8 @@
# Since: 2.3
##
{ 'command': 'guest-set-user-password',
- 'data': { 'username': 'str', 'password': 'str', 'crypted': 'bool' } }
+ 'data': { 'username': 'str', 'password': 'str', 'crypted': 'bool',
+ '*create': 'bool' } }
##
# @GuestMemoryBlock:
--
2.34.1
next prev parent reply other threads:[~2023-10-25 14:02 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-25 14:00 [PATCH 0/6] qga: Assorted patches, let us discuss Alexander Ivanov
2023-10-25 14:00 ` [PATCH 1/6] qga: Add process termination functionality Alexander Ivanov
2023-10-26 8:24 ` Konstantin Kostiuk
2023-10-25 14:00 ` [PATCH 2/6] qga: Move command execution code to a separate function Alexander Ivanov
2023-10-26 8:28 ` Konstantin Kostiuk
2023-10-25 14:00 ` [PATCH 3/6] qga: Let run_command() work without input data Alexander Ivanov
2023-10-25 14:00 ` Alexander Ivanov [this message]
2023-10-25 14:00 ` [PATCH 5/6] qga: Add timeout for fsfreeze Alexander Ivanov
2023-10-26 9:16 ` Konstantin Kostiuk
2023-10-30 16:32 ` Alexander Ivanov
2023-10-30 17:37 ` Konstantin Kostiuk
2023-10-25 14:00 ` [PATCH 6/6] qga: Cancel async snapshot before abort Alexander Ivanov
2023-10-26 8:47 ` Konstantin Kostiuk
2023-10-26 9:17 ` Konstantin Kostiuk
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=20231025140058.113376-5-alexander.ivanov@virtuozzo.com \
--to=alexander.ivanov@virtuozzo.com \
--cc=den@virtuozzo.com \
--cc=kkostiuk@redhat.com \
--cc=marcandre.lureau@gmail.com \
--cc=michael.roth@amd.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).