Blame SOURCES/0001-tools-add-show-computer-command.patch

79648a
From 0a169bd9b2687293f74bb57694eb82f9769610c9 Mon Sep 17 00:00:00 2001
79648a
From: Sumit Bose <sbose@redhat.com>
79648a
Date: Wed, 27 Nov 2019 12:34:45 +0100
79648a
Subject: [PATCH 1/2] tools: add show-computer command
79648a
79648a
The show-computer command prints the LDAP attributes of the related
79648a
computer object from AD.
79648a
79648a
Related to https://bugzilla.redhat.com/show_bug.cgi?id=1737342
79648a
---
79648a
 doc/adcli.xml      | 28 ++++++++++++++
79648a
 library/adenroll.c | 78 +++++++++++++++++++++++++++++---------
79648a
 library/adenroll.h |  5 +++
79648a
 tools/computer.c   | 93 ++++++++++++++++++++++++++++++++++++++++++++++
79648a
 tools/tools.c      |  1 +
79648a
 tools/tools.h      |  4 ++
79648a
 6 files changed, 191 insertions(+), 18 deletions(-)
79648a
79648a
diff --git a/doc/adcli.xml b/doc/adcli.xml
79648a
index 9faf96a..1f93186 100644
79648a
--- a/doc/adcli.xml
79648a
+++ b/doc/adcli.xml
79648a
@@ -93,6 +93,11 @@
79648a
 		<arg choice="opt">--domain=domain.example.com</arg>
79648a
 		<arg choice="plain">computer</arg>
79648a
 	</cmdsynopsis>
79648a
+	<cmdsynopsis>
79648a
+		<command>adcli show-computer</command>
79648a
+		<arg choice="opt">--domain=domain.example.com</arg>
79648a
+		<arg choice="plain">computer</arg>
79648a
+	</cmdsynopsis>
79648a
 </refsynopsisdiv>
79648a
 
79648a
 <refsect1 id='general_overview'>
79648a
@@ -811,6 +816,29 @@ Password for Administrator:
79648a
 
79648a
 </refsect1>
79648a
 
79648a
+<refsect1 id='show_computer_account'>
79648a
+	<title>Show Computer Account Attributes</title>
79648a
+
79648a
+	<para><command>adcli show-computer</command> show the computer account
79648a
+	attributes stored in AD. The account must already exist.</para>
79648a
+
79648a
+<programlisting>
79648a
+$ adcli show-computer --domain=domain.example.com host2
79648a
+Password for Administrator:
79648a
+</programlisting>
79648a
+
79648a
+	<para>If the computer name contains a dot, then it is
79648a
+	treated as fully qualified host name, otherwise it is treated
79648a
+	as short computer name.</para>
79648a
+
79648a
+	<para>If no computer name is specified, then the host name of the
79648a
+	computer adcli is running on is used, as returned by
79648a
+	<literal>gethostname()</literal>.</para>
79648a
+
79648a
+	<para>The various global options can be used.</para>
79648a
+
79648a
+</refsect1>
79648a
+
79648a
 <refsect1 id='bugs'>
79648a
 	<title>Bugs</title>
79648a
 	<para>
79648a
diff --git a/library/adenroll.c b/library/adenroll.c
79648a
index 524663a..8d2adeb 100644
79648a
--- a/library/adenroll.c
79648a
+++ b/library/adenroll.c
79648a
@@ -71,6 +71,21 @@ static krb5_enctype v51_earlier_enctypes[] = {
79648a
 	0
79648a
 };
79648a
 
79648a
+static char *default_ad_ldap_attrs[] =  {
79648a
+	"sAMAccountName",
79648a
+	"userPrincipalName",
79648a
+	"msDS-KeyVersionNumber",
79648a
+	"msDS-supportedEncryptionTypes",
79648a
+	"dNSHostName",
79648a
+	"servicePrincipalName",
79648a
+	"operatingSystem",
79648a
+	"operatingSystemVersion",
79648a
+	"operatingSystemServicePack",
79648a
+	"pwdLastSet",
79648a
+	"userAccountControl",
79648a
+	NULL,
79648a
+};
79648a
+
79648a
 /* Some constants for the userAccountControl AD LDAP attribute, see e.g.
79648a
  * https://support.microsoft.com/en-us/help/305144/how-to-use-the-useraccountcontrol-flags-to-manipulate-user-account-pro
79648a
  * for details. */
79648a
@@ -1213,19 +1228,6 @@ retrieve_computer_account (adcli_enroll *enroll)
79648a
 	char *end;
79648a
 	int ret;
79648a
 
79648a
-	char *attrs[] =  {
79648a
-		"msDS-KeyVersionNumber",
79648a
-		"msDS-supportedEncryptionTypes",
79648a
-		"dNSHostName",
79648a
-		"servicePrincipalName",
79648a
-		"operatingSystem",
79648a
-		"operatingSystemVersion",
79648a
-		"operatingSystemServicePack",
79648a
-		"pwdLastSet",
79648a
-		"userAccountControl",
79648a
-		NULL,
79648a
-	};
79648a
-
79648a
 	assert (enroll->computer_dn != NULL);
79648a
 	assert (enroll->computer_attributes == NULL);
79648a
 
79648a
@@ -1233,7 +1235,8 @@ retrieve_computer_account (adcli_enroll *enroll)
79648a
 	assert (ldap != NULL);
79648a
 
79648a
 	ret = ldap_search_ext_s (ldap, enroll->computer_dn, LDAP_SCOPE_BASE,
79648a
-	                         "(objectClass=*)", attrs, 0, NULL, NULL, NULL, -1,
79648a
+	                         "(objectClass=*)", default_ad_ldap_attrs,
79648a
+	                         0, NULL, NULL, NULL, -1,
79648a
 	                         &enroll->computer_attributes);
79648a
 
79648a
 	if (ret != LDAP_SUCCESS) {
79648a
@@ -2179,12 +2182,11 @@ adcli_enroll_load (adcli_enroll *enroll)
79648a
 }
79648a
 
79648a
 adcli_result
79648a
-adcli_enroll_update (adcli_enroll *enroll,
79648a
-		     adcli_enroll_flags flags)
79648a
+adcli_enroll_read_computer_account (adcli_enroll *enroll,
79648a
+		                    adcli_enroll_flags flags)
79648a
 {
79648a
 	adcli_result res = ADCLI_SUCCESS;
79648a
 	LDAP *ldap;
79648a
-	char *value;
79648a
 
79648a
 	return_unexpected_if_fail (enroll != NULL);
79648a
 
79648a
@@ -2214,7 +2216,18 @@ adcli_enroll_update (adcli_enroll *enroll,
79648a
 	}
79648a
 
79648a
 	/* Get information about the computer account */
79648a
-	res = retrieve_computer_account (enroll);
79648a
+	return retrieve_computer_account (enroll);
79648a
+}
79648a
+
79648a
+adcli_result
79648a
+adcli_enroll_update (adcli_enroll *enroll,
79648a
+		     adcli_enroll_flags flags)
79648a
+{
79648a
+	adcli_result res = ADCLI_SUCCESS;
79648a
+	LDAP *ldap;
79648a
+	char *value;
79648a
+
79648a
+	res = adcli_enroll_read_computer_account (enroll, flags);
79648a
 	if (res != ADCLI_SUCCESS)
79648a
 		return res;
79648a
 
79648a
@@ -2242,6 +2255,35 @@ adcli_enroll_update (adcli_enroll *enroll,
79648a
 	return enroll_join_or_update_tasks (enroll, flags);
79648a
 }
79648a
 
79648a
+adcli_result
79648a
+adcli_enroll_show_computer_attribute (adcli_enroll *enroll)
79648a
+{
79648a
+	LDAP *ldap;
79648a
+	size_t c;
79648a
+	char **vals;
79648a
+	size_t v;
79648a
+
79648a
+	ldap = adcli_conn_get_ldap_connection (enroll->conn);
79648a
+	assert (ldap != NULL);
79648a
+
79648a
+	for (c = 0; default_ad_ldap_attrs[c] != NULL; c++) {
79648a
+		vals = _adcli_ldap_parse_values (ldap,
79648a
+		                                 enroll->computer_attributes,
79648a
+		                                 default_ad_ldap_attrs[c]);
79648a
+		printf ("%s:\n", default_ad_ldap_attrs[c]);
79648a
+		if (vals == NULL) {
79648a
+			printf (" - not set -\n");
79648a
+		} else {
79648a
+			for (v = 0; vals[v] != NULL; v++) {
79648a
+				printf (" %s\n", vals[v]);
79648a
+			}
79648a
+		}
79648a
+		_adcli_strv_free (vals);
79648a
+	}
79648a
+
79648a
+	return ADCLI_SUCCESS;
79648a
+}
79648a
+
79648a
 adcli_result
79648a
 adcli_enroll_delete (adcli_enroll *enroll,
79648a
                      adcli_enroll_flags delete_flags)
79648a
diff --git a/library/adenroll.h b/library/adenroll.h
79648a
index 1d5d00d..11eb517 100644
79648a
--- a/library/adenroll.h
79648a
+++ b/library/adenroll.h
79648a
@@ -46,6 +46,11 @@ adcli_result       adcli_enroll_join                    (adcli_enroll *enroll,
79648a
 adcli_result       adcli_enroll_update                  (adcli_enroll *enroll,
79648a
 		                                         adcli_enroll_flags flags);
79648a
 
79648a
+adcli_result       adcli_enroll_read_computer_account   (adcli_enroll *enroll,
79648a
+                                                         adcli_enroll_flags flags);
79648a
+
79648a
+adcli_result       adcli_enroll_show_computer_attribute (adcli_enroll *enroll);
79648a
+
79648a
 adcli_result       adcli_enroll_delete                  (adcli_enroll *enroll,
79648a
                                                          adcli_enroll_flags delete_flags);
79648a
 
79648a
diff --git a/tools/computer.c b/tools/computer.c
79648a
index ac8a203..c8b96a4 100644
79648a
--- a/tools/computer.c
79648a
+++ b/tools/computer.c
79648a
@@ -964,3 +964,96 @@ adcli_tool_computer_delete (adcli_conn *conn,
79648a
 	adcli_enroll_unref (enroll);
79648a
 	return 0;
79648a
 }
79648a
+
79648a
+int
79648a
+adcli_tool_computer_show (adcli_conn *conn,
79648a
+                          int argc,
79648a
+                          char *argv[])
79648a
+{
79648a
+	adcli_enroll *enroll;
79648a
+	adcli_result res;
79648a
+	int opt;
79648a
+
79648a
+	struct option options[] = {
79648a
+		{ "domain", required_argument, NULL, opt_domain },
79648a
+		{ "domain-realm", required_argument, NULL, opt_domain_realm },
79648a
+		{ "domain-controller", required_argument, NULL, opt_domain_controller },
79648a
+		{ "login-user", required_argument, NULL, opt_login_user },
79648a
+		{ "login-ccache", optional_argument, NULL, opt_login_ccache },
79648a
+		{ "login-type", required_argument, NULL, opt_login_type },
79648a
+		{ "no-password", no_argument, 0, opt_no_password },
79648a
+		{ "stdin-password", no_argument, 0, opt_stdin_password },
79648a
+		{ "prompt-password", no_argument, 0, opt_prompt_password },
79648a
+		{ "verbose", no_argument, NULL, opt_verbose },
79648a
+		{ "help", no_argument, NULL, 'h' },
79648a
+		{ 0 },
79648a
+	};
79648a
+
79648a
+	static adcli_tool_desc usages[] = {
79648a
+		{ 0, "usage: adcli show-computer --domain=xxxx host1.example.com" },
79648a
+		{ 0 },
79648a
+	};
79648a
+
79648a
+	enroll = adcli_enroll_new (conn);
79648a
+	if (enroll == NULL) {
79648a
+		warnx ("unexpected memory problems");
79648a
+		return -1;
79648a
+	}
79648a
+
79648a
+	while ((opt = adcli_tool_getopt (argc, argv, options)) != -1) {
79648a
+		switch (opt) {
79648a
+		case 'h':
79648a
+		case '?':
79648a
+		case ':':
79648a
+			adcli_tool_usage (options, usages);
79648a
+			adcli_tool_usage (options, common_usages);
79648a
+			adcli_enroll_unref (enroll);
79648a
+			return opt == 'h' ? 0 : 2;
79648a
+		default:
79648a
+			res = parse_option ((Option)opt, optarg, conn, enroll);
79648a
+			if (res != ADCLI_SUCCESS) {
79648a
+				adcli_enroll_unref (enroll);
79648a
+				return res;
79648a
+			}
79648a
+			break;
79648a
+		}
79648a
+	}
79648a
+
79648a
+	argc -= optind;
79648a
+	argv += optind;
79648a
+
79648a
+	res = adcli_conn_connect (conn);
79648a
+	if (res != ADCLI_SUCCESS) {
79648a
+		warnx ("couldn't connect to %s domain: %s",
79648a
+		       adcli_conn_get_domain_name (conn),
79648a
+		       adcli_get_last_error ());
79648a
+		adcli_enroll_unref (enroll);
79648a
+		return -res;
79648a
+	}
79648a
+
79648a
+	if (argc == 1) {
79648a
+		parse_fqdn_or_name (enroll, argv[0]);
79648a
+	}
79648a
+
79648a
+	res = adcli_enroll_read_computer_account (enroll, 0);
79648a
+	if (res != ADCLI_SUCCESS) {
79648a
+		warnx ("couldn't read data for %s: %s",
79648a
+		       adcli_enroll_get_host_fqdn (enroll) != NULL
79648a
+		           ? adcli_enroll_get_host_fqdn (enroll)
79648a
+		           : adcli_enroll_get_computer_name (enroll),
79648a
+		       adcli_get_last_error ());
79648a
+		adcli_enroll_unref (enroll);
79648a
+		return -res;
79648a
+	}
79648a
+
79648a
+	res = adcli_enroll_show_computer_attribute (enroll);
79648a
+	if (res != ADCLI_SUCCESS) {
79648a
+		warnx ("couldn't print data for %s: %s",
79648a
+		       argv[0], adcli_get_last_error ());
79648a
+		adcli_enroll_unref (enroll);
79648a
+		return -res;
79648a
+	}
79648a
+
79648a
+	adcli_enroll_unref (enroll);
79648a
+	return 0;
79648a
+}
79648a
diff --git a/tools/tools.c b/tools/tools.c
79648a
index fc9fa9a..9d422f2 100644
79648a
--- a/tools/tools.c
79648a
+++ b/tools/tools.c
79648a
@@ -59,6 +59,7 @@ struct {
79648a
 	{ "preset-computer", adcli_tool_computer_preset, "Pre setup computers accounts", },
79648a
 	{ "reset-computer", adcli_tool_computer_reset, "Reset a computer account", },
79648a
 	{ "delete-computer", adcli_tool_computer_delete, "Delete a computer account", },
79648a
+	{ "show-computer", adcli_tool_computer_show, "Show computer account attributes stored in AD", },
79648a
 	{ "create-user", adcli_tool_user_create, "Create a user account", },
79648a
 	{ "delete-user", adcli_tool_user_delete, "Delete a user account", },
79648a
 	{ "create-group", adcli_tool_group_create, "Create a group", },
79648a
diff --git a/tools/tools.h b/tools/tools.h
79648a
index 8cebbf9..3702875 100644
79648a
--- a/tools/tools.h
79648a
+++ b/tools/tools.h
79648a
@@ -78,6 +78,10 @@ int       adcli_tool_computer_delete   (adcli_conn *conn,
79648a
                                         int argc,
79648a
                                         char *argv[]);
79648a
 
79648a
+int       adcli_tool_computer_show     (adcli_conn *conn,
79648a
+                                        int argc,
79648a
+                                        char *argv[]);
79648a
+
79648a
 int       adcli_tool_user_create       (adcli_conn *conn,
79648a
                                         int argc,
79648a
                                         char *argv[]);
79648a
-- 
79648a
2.21.0
79648a