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

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