Blame 0002-tools-add-show-computer-command.patch

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