Blame SOURCES/0003-entry-add-passwd-user-sub-command.patch

54f29a
From 6a673b236dfdfdf9c73cc3d2ccf3949eb1a5ddd0 Mon Sep 17 00:00:00 2001
54f29a
From: Sumit Bose <sbose@redhat.com>
54f29a
Date: Fri, 11 Jun 2021 12:47:37 +0200
54f29a
Subject: [PATCH 3/5] entry: add passwd-user sub-command
54f29a
54f29a
The new command allows to set or reset a user password with the help of
54f29a
an account privileged to set the password.
54f29a
54f29a
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1952828
54f29a
---
54f29a
 doc/adcli.xml     |  20 +++++++
54f29a
 library/adentry.c | 138 ++++++++++++++++++++++++++++++++++++++++++++++
54f29a
 library/adentry.h |   3 +
54f29a
 tools/entry.c     |  99 +++++++++++++++++++++++++++++++++
54f29a
 tools/tools.c     |   1 +
54f29a
 tools/tools.h     |   4 ++
54f29a
 6 files changed, 265 insertions(+)
54f29a
54f29a
diff --git a/doc/adcli.xml b/doc/adcli.xml
54f29a
index 1ed5d3f..6c36297 100644
54f29a
--- a/doc/adcli.xml
54f29a
+++ b/doc/adcli.xml
54f29a
@@ -56,6 +56,11 @@
54f29a
 		<arg choice="opt">--domain=domain.example.com</arg>
54f29a
 		<arg choice="plain">user</arg>
54f29a
 	</cmdsynopsis>
54f29a
+	<cmdsynopsis>
54f29a
+		<command>adcli passwd-user</command>
54f29a
+		<arg choice="opt">--domain=domain.example.com</arg>
54f29a
+		<arg choice="plain">user</arg>
54f29a
+	</cmdsynopsis>
54f29a
 	<cmdsynopsis>
54f29a
 		<command>adcli create-group</command>
54f29a
 		<arg choice="opt">--domain=domain.example.com</arg>
54f29a
@@ -696,6 +701,21 @@ $ adcli delete-user Fry --domain=domain.example.com
54f29a
 
54f29a
 </refsect1>
54f29a
 
54f29a
+<refsect1 id='passwd_user'>
54f29a
+	<title>(Re)setting the password of a User with an Administrative Account</title>
54f29a
+
54f29a
+	<para><command>adcli passwd-user</command> sets or resets the password
54f29a
+	of user account. The administrative account used for this operation
54f29a
+	must have privileges to set a password.</para>
54f29a
+
54f29a
+<programlisting>
54f29a
+$ adcli passwd-user Fry --domain=domain.example.com
54f29a
+</programlisting>
54f29a
+
54f29a
+	<para>The various global options can be used.</para>
54f29a
+
54f29a
+</refsect1>
54f29a
+
54f29a
 
54f29a
 <refsect1 id='create_group'>
54f29a
 	<title>Creating a Group</title>
54f29a
diff --git a/library/adentry.c b/library/adentry.c
54f29a
index 13dcaf8..0d9b9af 100644
54f29a
--- a/library/adentry.c
54f29a
+++ b/library/adentry.c
54f29a
@@ -409,6 +409,144 @@ adcli_entry_delete (adcli_entry *entry)
54f29a
 	return ADCLI_SUCCESS;
54f29a
 }
54f29a
 
54f29a
+static adcli_result
54f29a
+adcli_entry_ensure_enabled (adcli_entry *entry)
54f29a
+{
54f29a
+	adcli_result res;
54f29a
+	LDAP *ldap;
54f29a
+	adcli_attrs *attrs;
54f29a
+	uint32_t uac = 0;
54f29a
+	char *uac_str;
54f29a
+	unsigned long attr_val;
54f29a
+	char *end;
54f29a
+
54f29a
+	return_unexpected_if_fail (entry->entry_attrs != NULL);
54f29a
+
54f29a
+	ldap = adcli_conn_get_ldap_connection (entry->conn);
54f29a
+	return_unexpected_if_fail (ldap != NULL);
54f29a
+
54f29a
+	uac_str = _adcli_ldap_parse_value (ldap, entry->entry_attrs,
54f29a
+	                                   "userAccountControl");
54f29a
+	if (uac_str != NULL) {
54f29a
+		attr_val = strtoul (uac_str, &end, 10);
54f29a
+		if (*end != '\0' || attr_val > UINT32_MAX) {
54f29a
+			_adcli_warn ("Invalid userAccountControl '%s' for %s account in directory: %s, assuming 0",
54f29a
+			            uac_str, entry->object_class, entry->entry_dn);
54f29a
+		} else {
54f29a
+			uac = attr_val;
54f29a
+		}
54f29a
+		free (uac_str);
54f29a
+	}
54f29a
+	if (uac & UAC_ACCOUNTDISABLE) {
54f29a
+		uac &= ~(UAC_ACCOUNTDISABLE);
54f29a
+
54f29a
+		if (asprintf (&uac_str, "%d", uac) < 0) {
54f29a
+			_adcli_warn ("Cannot enable %s entry %s after password (re)set",
54f29a
+			             entry->object_class, entry->entry_dn);
54f29a
+			return ADCLI_ERR_UNEXPECTED;
54f29a
+		}
54f29a
+
54f29a
+		attrs = adcli_attrs_new ();
54f29a
+		adcli_attrs_replace (attrs, "userAccountControl", uac_str,
54f29a
+		                     NULL);
54f29a
+		res = adcli_entry_modify (entry, attrs);
54f29a
+		if (res == ADCLI_SUCCESS) {
54f29a
+			_adcli_info ("Enabled %s entry %s after password (re)set",
54f29a
+			             entry->object_class, entry->entry_dn);
54f29a
+		} else {
54f29a
+			_adcli_warn ("Failed to enable %s entry %s after password (re)set",
54f29a
+			             entry->object_class, entry->entry_dn);
54f29a
+		}
54f29a
+		free (uac_str);
54f29a
+		adcli_attrs_free (attrs);
54f29a
+	} else {
54f29a
+		res = ADCLI_SUCCESS;
54f29a
+	}
54f29a
+
54f29a
+	return res;
54f29a
+}
54f29a
+
54f29a
+adcli_result
54f29a
+adcli_entry_set_passwd (adcli_entry *entry, const char *user_pwd)
54f29a
+{
54f29a
+	adcli_result res;
54f29a
+	LDAP *ldap;
54f29a
+	krb5_error_code code;
54f29a
+	krb5_context k5;
54f29a
+	krb5_ccache ccache;
54f29a
+	krb5_data result_string = { 0, };
54f29a
+	krb5_data result_code_string = { 0, };
54f29a
+	int result_code;
54f29a
+	char *message;
54f29a
+	krb5_principal user_principal;
54f29a
+
54f29a
+	ldap = adcli_conn_get_ldap_connection (entry->conn);
54f29a
+	return_unexpected_if_fail (ldap != NULL);
54f29a
+
54f29a
+	/* Find the user */
54f29a
+	res = update_entry_from_domain (entry, ldap);
54f29a
+	if (res != ADCLI_SUCCESS)
54f29a
+		return res;
54f29a
+
54f29a
+	if (!entry->entry_dn) {
54f29a
+		_adcli_err ("Cannot find the %s entry %s in the domain",
54f29a
+		            entry->object_class, entry->sam_name);
54f29a
+		return ADCLI_ERR_CONFIG;
54f29a
+	}
54f29a
+
54f29a
+	k5 = adcli_conn_get_krb5_context (entry->conn);
54f29a
+	return_unexpected_if_fail (k5 != NULL);
54f29a
+
54f29a
+	code = _adcli_krb5_build_principal (k5, entry->sam_name,
54f29a
+	                                    adcli_conn_get_domain_realm (entry->conn),
54f29a
+	                                    &user_principal);
54f29a
+	return_unexpected_if_fail (code == 0);
54f29a
+
54f29a
+	ccache = adcli_conn_get_login_ccache (entry->conn);
54f29a
+	return_unexpected_if_fail (ccache != NULL);
54f29a
+
54f29a
+	memset (&result_string, 0, sizeof (result_string));
54f29a
+	memset (&result_code_string, 0, sizeof (result_code_string));
54f29a
+
54f29a
+	code = krb5_set_password_using_ccache (k5, ccache, user_pwd,
54f29a
+	                                       user_principal, &result_code,
54f29a
+	                                       &result_code_string, &result_string);
54f29a
+
54f29a
+	if (code != 0) {
54f29a
+		_adcli_err ("Couldn't set password for %s account: %s: %s",
54f29a
+		            entry->object_class,
54f29a
+		            entry->sam_name, krb5_get_error_message (k5, code));
54f29a
+		/* TODO: Parse out these values */
54f29a
+		res = ADCLI_ERR_DIRECTORY;
54f29a
+
54f29a
+	} else if (result_code != 0) {
54f29a
+#ifdef HAVE_KRB5_CHPW_MESSAGE
54f29a
+		if (krb5_chpw_message (k5, &result_string, &message) != 0)
54f29a
+			message = NULL;
54f29a
+#else
54f29a
+		message = NULL;
54f29a
+		if (result_string.length)
54f29a
+			message = _adcli_str_dupn (result_string.data, result_string.length);
54f29a
+#endif
54f29a
+		_adcli_err ("Cannot set %s password: %.*s%s%s",
54f29a
+		            entry->object_class,
54f29a
+		            (int)result_code_string.length, result_code_string.data,
54f29a
+		            message ? ": " : "", message ? message : "");
54f29a
+		res = ADCLI_ERR_CREDENTIALS;
54f29a
+#ifdef HAVE_KRB5_CHPW_MESSAGE
54f29a
+		krb5_free_string (k5, message);
54f29a
+#else
54f29a
+		free (message);
54f29a
+#endif
54f29a
+	} else {
54f29a
+		_adcli_info ("Password (re)setted for %s: %s", entry->object_class, entry->entry_dn);
54f29a
+
54f29a
+		res = adcli_entry_ensure_enabled (entry);
54f29a
+	}
54f29a
+
54f29a
+	return res;
54f29a
+}
54f29a
+
54f29a
 const char *
54f29a
 adcli_entry_get_sam_name (adcli_entry *entry)
54f29a
 {
54f29a
diff --git a/library/adentry.h b/library/adentry.h
54f29a
index ae90689..f2382b1 100644
54f29a
--- a/library/adentry.h
54f29a
+++ b/library/adentry.h
54f29a
@@ -49,6 +49,9 @@ adcli_result       adcli_entry_modify                   (adcli_entry *entry,
54f29a
 
54f29a
 adcli_result       adcli_entry_delete                   (adcli_entry *entry);
54f29a
 
54f29a
+adcli_result       adcli_entry_set_passwd               (adcli_entry *entry,
54f29a
+                                                         const char *user_pwd);
54f29a
+
54f29a
 const char *       adcli_entry_get_domain_ou            (adcli_entry *entry);
54f29a
 
54f29a
 void               adcli_entry_set_domain_ou            (adcli_entry *entry,
54f29a
diff --git a/tools/entry.c b/tools/entry.c
54f29a
index 05e4313..52d2546 100644
54f29a
--- a/tools/entry.c
54f29a
+++ b/tools/entry.c
54f29a
@@ -24,6 +24,7 @@
54f29a
 #include "config.h"
54f29a
 
54f29a
 #include "adcli.h"
54f29a
+#include "adprivate.h"
54f29a
 #include "adattrs.h"
54f29a
 #include "tools.h"
54f29a
 
54f29a
@@ -385,6 +386,104 @@ adcli_tool_user_delete (adcli_conn *conn,
54f29a
 	return 0;
54f29a
 }
54f29a
 
54f29a
+int
54f29a
+adcli_tool_user_passwd (adcli_conn *conn,
54f29a
+                        int argc,
54f29a
+                        char *argv[])
54f29a
+{
54f29a
+	adcli_result res;
54f29a
+	adcli_entry *entry;
54f29a
+	int opt;
54f29a
+	char *user_pwd = NULL;
54f29a
+
54f29a
+	struct option options[] = {
54f29a
+		{ "domain", required_argument, NULL, opt_domain },
54f29a
+		{ "domain-realm", required_argument, NULL, opt_domain_realm },
54f29a
+		{ "domain-controller", required_argument, NULL, opt_domain_controller },
54f29a
+		{ "use-ldaps", no_argument, 0, opt_use_ldaps },
54f29a
+		{ "login-user", required_argument, NULL, opt_login_user },
54f29a
+		{ "login-ccache", optional_argument, NULL, opt_login_ccache },
54f29a
+		{ "no-password", no_argument, 0, opt_no_password },
54f29a
+		{ "stdin-password", no_argument, 0, opt_stdin_password },
54f29a
+		{ "prompt-password", no_argument, 0, opt_prompt_password },
54f29a
+		{ "verbose", no_argument, NULL, opt_verbose },
54f29a
+		{ "help", no_argument, NULL, 'h' },
54f29a
+		{ 0 },
54f29a
+	};
54f29a
+
54f29a
+	static adcli_tool_desc usages[] = {
54f29a
+		{ 0, "usage: adcli passwd-user --domain=xxxx user" },
54f29a
+		{ 0 },
54f29a
+	};
54f29a
+
54f29a
+	while ((opt = adcli_tool_getopt (argc, argv, options)) != -1) {
54f29a
+		switch (opt) {
54f29a
+		case 'h':
54f29a
+		case '?':
54f29a
+		case ':':
54f29a
+			adcli_tool_usage (options, usages);
54f29a
+			adcli_tool_usage (options, common_usages);
54f29a
+			return opt == 'h' ? 0 : 2;
54f29a
+		default:
54f29a
+			res = parse_option ((Option)opt, optarg, conn);
54f29a
+			if (res != ADCLI_SUCCESS) {
54f29a
+				return res;
54f29a
+			}
54f29a
+			break;
54f29a
+		}
54f29a
+	}
54f29a
+
54f29a
+	argc -= optind;
54f29a
+	argv += optind;
54f29a
+
54f29a
+	if (argc != 1) {
54f29a
+		warnx ("specify one user name to (re)set password");
54f29a
+		return 2;
54f29a
+	}
54f29a
+
54f29a
+	entry = adcli_entry_new_user (conn, argv[0]);
54f29a
+	if (entry == NULL) {
54f29a
+		warnx ("unexpected memory problems");
54f29a
+		return -1;
54f29a
+	}
54f29a
+
54f29a
+	adcli_conn_set_allowed_login_types (conn, ADCLI_LOGIN_USER_ACCOUNT);
54f29a
+
54f29a
+	res = adcli_conn_connect (conn);
54f29a
+	if (res != ADCLI_SUCCESS) {
54f29a
+		warnx ("couldn't connect to %s domain: %s",
54f29a
+		       adcli_conn_get_domain_name (conn),
54f29a
+		       adcli_get_last_error ());
54f29a
+		adcli_entry_unref (entry);
54f29a
+		return -res;
54f29a
+	}
54f29a
+
54f29a
+	user_pwd = adcli_prompt_password_func (ADCLI_LOGIN_USER_ACCOUNT,
54f29a
+	                                       adcli_entry_get_sam_name(entry),
54f29a
+	                                       0, NULL);
54f29a
+	if (user_pwd == NULL || *user_pwd == '\0') {
54f29a
+		warnx ("missing password");
54f29a
+		_adcli_password_free (user_pwd);
54f29a
+		adcli_entry_unref (entry);
54f29a
+		return 2;
54f29a
+	}
54f29a
+
54f29a
+	res = adcli_entry_set_passwd (entry, user_pwd);
54f29a
+	_adcli_password_free (user_pwd);
54f29a
+	if (res != ADCLI_SUCCESS) {
54f29a
+		warnx ("(re)setting password for user %s in domain %s failed: %s",
54f29a
+		       adcli_entry_get_sam_name (entry),
54f29a
+		       adcli_conn_get_domain_name (conn),
54f29a
+		       adcli_get_last_error ());
54f29a
+		adcli_entry_unref (entry);
54f29a
+		return -res;
54f29a
+	}
54f29a
+
54f29a
+	adcli_entry_unref (entry);
54f29a
+
54f29a
+	return 0;
54f29a
+}
54f29a
+
54f29a
 int
54f29a
 adcli_tool_group_create (adcli_conn *conn,
54f29a
                          int argc,
54f29a
diff --git a/tools/tools.c b/tools/tools.c
54f29a
index 84bbba9..a14b9ca 100644
54f29a
--- a/tools/tools.c
54f29a
+++ b/tools/tools.c
54f29a
@@ -63,6 +63,7 @@ struct {
54f29a
 	{ "create-msa", adcli_tool_computer_managed_service_account, "Create a managed service account in the given AD domain", },
54f29a
 	{ "create-user", adcli_tool_user_create, "Create a user account", },
54f29a
 	{ "delete-user", adcli_tool_user_delete, "Delete a user account", },
54f29a
+	{ "passwd-user", adcli_tool_user_passwd, "(Re)set a user password", },
54f29a
 	{ "create-group", adcli_tool_group_create, "Create a group", },
54f29a
 	{ "delete-group", adcli_tool_group_delete, "Delete a group", },
54f29a
 	{ "add-member", adcli_tool_member_add, "Add users to a group", },
54f29a
diff --git a/tools/tools.h b/tools/tools.h
54f29a
index 82d5e4e..d38aa32 100644
54f29a
--- a/tools/tools.h
54f29a
+++ b/tools/tools.h
54f29a
@@ -94,6 +94,10 @@ int       adcli_tool_user_delete       (adcli_conn *conn,
54f29a
                                         int argc,
54f29a
                                         char *argv[]);
54f29a
 
54f29a
+int       adcli_tool_user_passwd       (adcli_conn *conn,
54f29a
+                                        int argc,
54f29a
+                                        char *argv[]);
54f29a
+
54f29a
 int       adcli_tool_group_create      (adcli_conn *conn,
54f29a
                                         int argc,
54f29a
                                         char *argv[]);
54f29a
-- 
54f29a
2.31.1
54f29a