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

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