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