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

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