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

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