Blame SOURCES/0001-Implement-adcli-testjoin.patch

ee8dc1
From 6fd99ff6c5dd6ef0be8d942989b1c6dcee3102d9 Mon Sep 17 00:00:00 2001
ee8dc1
From: Sumit Bose <sbose@redhat.com>
ee8dc1
Date: Fri, 22 Mar 2019 12:37:39 +0100
ee8dc1
Subject: [PATCH] Implement 'adcli testjoin'
ee8dc1
ee8dc1
By calling adcli testjoin it will be checked if the host credentials
ee8dc1
stored in the keytab are still valid.
ee8dc1
ee8dc1
Related to https://bugzilla.redhat.com/show_bug.cgi?id=1622583
ee8dc1
---
ee8dc1
 doc/adcli.xml    | 34 +++++++++++++++++++++++
ee8dc1
 tools/computer.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++
ee8dc1
 tools/tools.c    |  1 +
ee8dc1
 tools/tools.h    |  4 +++
ee8dc1
 4 files changed, 111 insertions(+)
ee8dc1
ee8dc1
diff --git a/doc/adcli.xml b/doc/adcli.xml
ee8dc1
index af73433..9605b4a 100644
ee8dc1
--- a/doc/adcli.xml
ee8dc1
+++ b/doc/adcli.xml
ee8dc1
@@ -43,6 +43,9 @@
ee8dc1
 	<cmdsynopsis>
ee8dc1
 		<command>adcli update</command>
ee8dc1
 	</cmdsynopsis>
ee8dc1
+	<cmdsynopsis>
ee8dc1
+		<command>adcli testjoin</command>
ee8dc1
+	</cmdsynopsis>
ee8dc1
 	<cmdsynopsis>
ee8dc1
 		<command>adcli create-user</command>
ee8dc1
 		<arg choice="opt">--domain=domain.example.com</arg>
ee8dc1
@@ -474,6 +477,37 @@ $ adcli update --login-ccache=/tmp/krbcc_123
ee8dc1
 
ee8dc1
 </refsect1>
ee8dc1
 
ee8dc1
+<refsect1 id='testjoin'>
ee8dc1
+	<title>Testing if the machine account password is valid</title>
ee8dc1
+
ee8dc1
+	<para><command>adcli testjoin</command> uses the current credentials in
ee8dc1
+	the keytab and tries to authenticate with the machine account to the AD
ee8dc1
+	domain. If this works the machine account password and the join are
ee8dc1
+	still valid. If it fails the machine account password or the whole
ee8dc1
+	machine account have to be refreshed with
ee8dc1
+	<command>adcli join</command> or <command>adcli update</command>.
ee8dc1
+	</para>
ee8dc1
+
ee8dc1
+<programlisting>
ee8dc1
+$ adcli testjoin
ee8dc1
+</programlisting>
ee8dc1
+
ee8dc1
+	<para>Only the global options not related to authentication are
ee8dc1
+	available, additionally you can specify the following options to
ee8dc1
+	control how this operation is done.</para>
ee8dc1
+
ee8dc1
+	<variablelist>
ee8dc1
+		<varlistentry>
ee8dc1
+			<term><option>-K, --host-keytab=<parameter>/path/to/keytab</parameter></option></term>
ee8dc1
+			<listitem><para>Specify the path to the host keytab where
ee8dc1
+			current host credentials are stored and the new ones
ee8dc1
+			will be written to.  If not specified, the default
ee8dc1
+			location will be used, usually
ee8dc1
+			<filename>/etc/krb5.keytab</filename>.</para></listitem>
ee8dc1
+		</varlistentry>
ee8dc1
+	</variablelist>
ee8dc1
+</refsect1>
ee8dc1
+
ee8dc1
 <refsect1 id='create_user'>
ee8dc1
 	<title>Creating a User</title>
ee8dc1
 
ee8dc1
diff --git a/tools/computer.c b/tools/computer.c
ee8dc1
index 112340e..610ed2b 100644
ee8dc1
--- a/tools/computer.c
ee8dc1
+++ b/tools/computer.c
ee8dc1
@@ -566,6 +566,78 @@ adcli_tool_computer_update (adcli_conn *conn,
ee8dc1
 	return 0;
ee8dc1
 }
ee8dc1
 
ee8dc1
+int
ee8dc1
+adcli_tool_computer_testjoin (adcli_conn *conn,
ee8dc1
+                              int argc,
ee8dc1
+                              char *argv[])
ee8dc1
+{
ee8dc1
+	adcli_enroll *enroll;
ee8dc1
+	adcli_result res;
ee8dc1
+	const char *ktname;
ee8dc1
+	int opt;
ee8dc1
+
ee8dc1
+	struct option options[] = {
ee8dc1
+		{ "domain", required_argument, NULL, opt_domain },
ee8dc1
+		{ "domain-controller", required_argument, NULL, opt_domain_controller },
ee8dc1
+		{ "host-keytab", required_argument, 0, opt_host_keytab },
ee8dc1
+		{ "verbose", no_argument, NULL, opt_verbose },
ee8dc1
+		{ "help", no_argument, NULL, 'h' },
ee8dc1
+		{ 0 },
ee8dc1
+	};
ee8dc1
+
ee8dc1
+	static adcli_tool_desc usages[] = {
ee8dc1
+		{ 0, "usage: adcli testjoin" },
ee8dc1
+		{ 0 },
ee8dc1
+	};
ee8dc1
+
ee8dc1
+	enroll = adcli_enroll_new (conn);
ee8dc1
+	if (enroll == NULL)
ee8dc1
+		errx (-1, "unexpected memory problems");
ee8dc1
+
ee8dc1
+	while ((opt = adcli_tool_getopt (argc, argv, options)) != -1) {
ee8dc1
+		switch (opt) {
ee8dc1
+		case 'h':
ee8dc1
+		case '?':
ee8dc1
+		case ':':
ee8dc1
+			adcli_tool_usage (options, usages);
ee8dc1
+			adcli_tool_usage (options, common_usages);
ee8dc1
+			adcli_enroll_unref (enroll);
ee8dc1
+			return opt == 'h' ? 0 : 2;
ee8dc1
+		default:
ee8dc1
+			parse_option ((Option)opt, optarg, conn, enroll);
ee8dc1
+			break;
ee8dc1
+		}
ee8dc1
+	}
ee8dc1
+
ee8dc1
+	/* Force use of a keytab to test the join/machine account password */
ee8dc1
+	adcli_conn_set_allowed_login_types (conn, ADCLI_LOGIN_COMPUTER_ACCOUNT);
ee8dc1
+	ktname = adcli_enroll_get_keytab_name (enroll);
ee8dc1
+	adcli_conn_set_login_keytab_name (conn, ktname ? ktname : "");
ee8dc1
+
ee8dc1
+	res = adcli_enroll_load (enroll);
ee8dc1
+	if (res != ADCLI_SUCCESS) {
ee8dc1
+		adcli_enroll_unref (enroll);
ee8dc1
+		adcli_conn_unref (conn);
ee8dc1
+		errx (-res, "couldn't lookup domain info from keytab: %s",
ee8dc1
+		      adcli_get_last_error ());
ee8dc1
+	}
ee8dc1
+
ee8dc1
+	res = adcli_conn_connect (conn);
ee8dc1
+	if (res != ADCLI_SUCCESS) {
ee8dc1
+		adcli_enroll_unref (enroll);
ee8dc1
+		adcli_conn_unref (conn);
ee8dc1
+		errx (-res, "couldn't connect to %s domain: %s",
ee8dc1
+		      adcli_conn_get_domain_name (conn),
ee8dc1
+		      adcli_get_last_error ());
ee8dc1
+	}
ee8dc1
+
ee8dc1
+	printf ("Sucessfully validated join to domain %s\n",
ee8dc1
+	        adcli_conn_get_domain_name (conn));
ee8dc1
+
ee8dc1
+	adcli_enroll_unref (enroll);
ee8dc1
+
ee8dc1
+	return 0;
ee8dc1
+}
ee8dc1
 
ee8dc1
 int
ee8dc1
 adcli_tool_computer_preset (adcli_conn *conn,
ee8dc1
diff --git a/tools/tools.c b/tools/tools.c
ee8dc1
index 915130e..c4e2851 100644
ee8dc1
--- a/tools/tools.c
ee8dc1
+++ b/tools/tools.c
ee8dc1
@@ -55,6 +55,7 @@ struct {
ee8dc1
 	{ "info", adcli_tool_info, "Print information about a domain", CONNECTION_LESS },
ee8dc1
 	{ "join", adcli_tool_computer_join, "Join this machine to a domain", },
ee8dc1
 	{ "update", adcli_tool_computer_update, "Update machine membership in a domain", },
ee8dc1
+	{ "testjoin", adcli_tool_computer_testjoin, "Test if machine account password is valid", },
ee8dc1
 	{ "preset-computer", adcli_tool_computer_preset, "Pre setup computers accounts", },
ee8dc1
 	{ "reset-computer", adcli_tool_computer_reset, "Reset a computer account", },
ee8dc1
 	{ "delete-computer", adcli_tool_computer_delete, "Delete a computer account", },
ee8dc1
diff --git a/tools/tools.h b/tools/tools.h
ee8dc1
index 6c97ccf..8cebbf9 100644
ee8dc1
--- a/tools/tools.h
ee8dc1
+++ b/tools/tools.h
ee8dc1
@@ -70,6 +70,10 @@ int       adcli_tool_computer_update   (adcli_conn *conn,
ee8dc1
                                         int argc,
ee8dc1
                                         char *argv[]);
ee8dc1
 
ee8dc1
+int       adcli_tool_computer_testjoin (adcli_conn *conn,
ee8dc1
+                                        int argc,
ee8dc1
+                                        char *argv[]);
ee8dc1
+
ee8dc1
 int       adcli_tool_computer_delete   (adcli_conn *conn,
ee8dc1
                                         int argc,
ee8dc1
                                         char *argv[]);
ee8dc1
-- 
ee8dc1
2.20.1
ee8dc1