Blame SOURCES/0002-computer-add-create-msa-sub-command.patch

e06942
From 41379f7ad6a9442dd55cc43d832427911e86db31 Mon Sep 17 00:00:00 2001
e06942
From: Sumit Bose <sbose@redhat.com>
e06942
Date: Fri, 23 Oct 2020 16:53:43 +0200
e06942
Subject: [PATCH 2/7] computer: add create-msa sub-command
e06942
e06942
Add new sub-command to create a managed service account in AD. This can
e06942
be used if LDAP access to AD is needed but the host is already joined to
e06942
a different domain.
e06942
e06942
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1854112
e06942
---
e06942
 doc/adcli.xml      | 140 ++++++++++++++++++++++++++++++++++++++
e06942
 library/adenroll.c | 164 ++++++++++++++++++++++++++++++++++++++-------
e06942
 tools/computer.c   | 125 ++++++++++++++++++++++++++++++++++
e06942
 tools/tools.c      |   1 +
e06942
 tools/tools.h      |   4 ++
e06942
 5 files changed, 409 insertions(+), 25 deletions(-)
e06942
e06942
diff --git a/doc/adcli.xml b/doc/adcli.xml
e06942
index cc44fd8..14921f9 100644
e06942
--- a/doc/adcli.xml
e06942
+++ b/doc/adcli.xml
e06942
@@ -98,6 +98,10 @@
e06942
 		<arg choice="opt">--domain=domain.example.com</arg>
e06942
 		<arg choice="plain">computer</arg>
e06942
 	</cmdsynopsis>
e06942
+	<cmdsynopsis>
e06942
+		<command>adcli create-msa</command>
e06942
+		<arg choice="opt">--domain=domain.example.com</arg>
e06942
+	</cmdsynopsis>
e06942
 </refsynopsisdiv>
e06942
 
e06942
 <refsect1 id='general_overview'>
e06942
@@ -885,6 +889,142 @@ Password for Administrator:
e06942
 
e06942
 </refsect1>
e06942
 
e06942
+<refsect1 id='managed_service_account'>
e06942
+	<title>Create a managed service account</title>
e06942
+
e06942
+	<para><command>adcli create-msa</command> creates a managed service
e06942
+	account (MSA) in the given Active Directory domain. This is useful if a
e06942
+	computer should not fully join the Active Directory domain but LDAP
e06942
+	access is needed. A typical use case is that the computer is already
e06942
+	joined an Active Directory domain and needs access to another Active
e06942
+	Directory domain in the same or a trusted forest where the host
e06942
+	credentials from the joined Active Directory domain are
e06942
+	not valid, e.g. there is only a one-way trust.</para>
e06942
+
e06942
+<programlisting>
e06942
+$ adcli create-msa --domain=domain.example.com
e06942
+Password for Administrator:
e06942
+</programlisting>
e06942
+
e06942
+	<para>The managed service account, as maintained by adcli, cannot have
e06942
+	additional service principals names (SPNs) associated with it. An SPN
e06942
+	is defined within the context of a Kerberos service which is tied to a
e06942
+	machine account in Active Directory. Since a machine can be joined to a
e06942
+	single Active Directory domain, managed service account in a different
e06942
+	Active Directory domain will not have the SPNs that otherwise are part
e06942
+	of another Active Directory domain's machine.</para>
e06942
+
e06942
+	<para>Since it is expected that a client will most probably join to the
e06942
+	Active Directory domain matching its DNS domain the managed service
e06942
+	account will be needed for a different Active directory domain and as a
e06942
+	result the Active Directory domain name is a mandatory option. If
e06942
+	called with no other options <command>adcli create-msa</command>
e06942
+	will use the short hostname with an additional random suffix as
e06942
+	computer name to avoid name collisions.</para>
e06942
+
e06942
+	<para>LDAP attribute sAMAccountName has a limit of 20 characters.
e06942
+	However, machine account's NetBIOS name must be at most 16 characters
e06942
+	long, including a trailing '$' sign. Since it is not expected that the
e06942
+	managed service accounts created by adcli will be used on the NetBIOS
e06942
+	level the remaining 4 characters can be used to add uniqueness. Managed
e06942
+	service account names will have a suffix of 3 random characters from
e06942
+	number and upper- and lowercase ASCII ranges appended to the chosen
e06942
+	short host name, using '!' as a separator. For a host with the
e06942
+	shortname 'myhost', a managed service account will have a common name
e06942
+	(CN attribute) 'myhost!A2c' and a NetBIOS name
e06942
+	(sAMAccountName attribute) will be 'myhost!A2c$'. A corresponding
e06942
+	Kerberos principal in the Active Directory domain where the managed
e06942
+	service account was created would be
e06942
+	'myhost!A2c$@DOMAIN.EXAMPLE.COM'.</para>
e06942
+
e06942
+	<para>A keytab for the managed service account is stored into a file
e06942
+	specified with -K option. If it is not specified, the file is named
e06942
+	after the default keytab file, with lowercase Active Directory domain
e06942
+	of the managed service account as a suffix. On most systems it would be
e06942
+	<filename>/etc/krb5.keytab</filename> with a suffix of
e06942
+	'domain.example.com', e.g.
e06942
+	<filename>/etc/krb5.keytad.domain.example.com</filename>.</para>
e06942
+
e06942
+	<para><command>adcli create-msa</command> can be called multiple
e06942
+	times to reset the password of the managed service account. To identify
e06942
+	the right account with the random component in the name the
e06942
+	corresponding principal is read from the keytab. If the keytab got
e06942
+	deleted <command>adcli</command> will try to identify an existing
e06942
+	managed service account with the help of the fully-qualified name, if
e06942
+	this fails a new managed service account will be created.</para>
e06942
+
e06942
+	<para>The managed service account password can be updated with
e06942
+<programlisting>
e06942
+$ adcli update --domain=domain.example.com --host-keytab=/etc/krb5.keytad.domain.example.com
e06942
+</programlisting>
e06942
+	and the managed service account can be deleted with
e06942
+<programlisting>
e06942
+$ adcli delete-computer --domain=domain.example.com 'myhost!A2c'
e06942
+</programlisting>
e06942
+	</para>
e06942
+
e06942
+	<para>In addition to the global options, you can specify the following
e06942
+	options to control how this operation is done.</para>
e06942
+
e06942
+	<variablelist>
e06942
+		<varlistentry>
e06942
+			<term><option>-N, --computer-name=<parameter>computer</parameter></option></term>
e06942
+			<listitem><para>The short non-dotted name of the managed
e06942
+			service account that will be created in the Active
e06942
+			Directory domain. The long option name
e06942
+			<option>--computer-name</option> is
e06942
+			kept to underline the similarity with the same option
e06942
+			of the other sub-commands. If not specified,
e06942
+			then the first portion of the <option>--host-fqdn</option>
e06942
+			or its default is used with a random suffix.</para></listitem>
e06942
+		</varlistentry>
e06942
+		<varlistentry>
e06942
+			<term><option>-O, --domain-ou=<parameter>OU=xxx</parameter></option></term>
e06942
+			<listitem><para>The full distinguished name of the OU in
e06942
+			which to create the managed service account. If not
e06942
+			specified, then the managed service account will be
e06942
+			created in a default location.</para></listitem>
e06942
+		</varlistentry>
e06942
+		<varlistentry>
e06942
+			<term><option>-H, --host-fqdn=<parameter>host</parameter></option></term>
e06942
+			<listitem><para>Override the local machine's fully
e06942
+			qualified DNS domain name. If not specified, the local
e06942
+			machine's hostname will be retrieved via
e06942
+			<function>gethostname()</function>.
e06942
+			If <function>gethostname()</function> only returns a short name
e06942
+			<function>getaddrinfo()</function> with the AI_CANONNAME hint
e06942
+			is called to expand the name to a fully qualified DNS
e06942
+			domain name.</para></listitem>
e06942
+		</varlistentry>
e06942
+		<varlistentry>
e06942
+			<term><option>-K, --host-keytab=<parameter>/path/to/keytab</parameter></option></term>
e06942
+			<listitem><para>Specify the path to the host keytab where
e06942
+			credentials of the managed service account will be
e06942
+			written after a successful creation. If not specified,
e06942
+			the default location will be used, usually
e06942
+			<filename>/etc/krb5.keytab</filename> with
e06942
+			the lower-cased Active Directory domain name added as a
e06942
+			suffix e.g.
e06942
+			<filename>/etc/krb5.keytab.domain.example.com</filename>.
e06942
+			</para></listitem>
e06942
+		</varlistentry>
e06942
+		<varlistentry>
e06942
+			<term><option>--show-details</option></term>
e06942
+			<listitem><para>After a successful creation print out
e06942
+			information about the created object. This is output in
e06942
+			a format that should be both human and machine
e06942
+			readable.</para></listitem>
e06942
+		</varlistentry>
e06942
+		<varlistentry>
e06942
+			<term><option>--show-password</option></term>
e06942
+			<listitem><para>After a successful creation print out
e06942
+			the managed service account password. This is output in
e06942
+			a format that should be both human and machine
e06942
+			readable.</para></listitem>
e06942
+		</varlistentry>
e06942
+	</variablelist>
e06942
+</refsect1>
e06942
+
e06942
 <refsect1 id='delegation'>
e06942
 	<title>Delegated Permissions</title>
e06942
 	<para>It is common practice in AD to not use an account from the Domain
e06942
diff --git a/library/adenroll.c b/library/adenroll.c
e06942
index 5ae1f7b..dbfda36 100644
e06942
--- a/library/adenroll.c
e06942
+++ b/library/adenroll.c
e06942
@@ -155,6 +155,20 @@ struct _adcli_enroll {
e06942
 	char *description;
e06942
 };
e06942
 
e06942
+static void
e06942
+check_if_service (adcli_enroll *enroll,
e06942
+                  LDAP *ldap,
e06942
+                  LDAPMessage *results)
e06942
+{
e06942
+	char **objectclasses = NULL;
e06942
+
e06942
+	objectclasses = _adcli_ldap_parse_values (ldap, results, "objectClass");
e06942
+	enroll->is_service = _adcli_strv_has_ex (objectclasses,
e06942
+	                                         "msDS-ManagedServiceAccount",
e06942
+	                                         strcasecmp) == 1 ? true : false;
e06942
+	_adcli_strv_free (objectclasses);
e06942
+}
e06942
+
e06942
 static adcli_result
e06942
 ensure_host_fqdn (adcli_result res,
e06942
                   adcli_enroll *enroll)
e06942
@@ -471,13 +485,15 @@ ensure_keytab_principals (adcli_result res,
e06942
 {
e06942
 	krb5_context k5;
e06942
 	krb5_error_code code;
e06942
-	int count;
e06942
+	int count = 0;
e06942
 	int at, i;
e06942
 
e06942
 	/* Prepare the principals we're going to add to the keytab */
e06942
 
e06942
-	return_unexpected_if_fail (enroll->service_principals);
e06942
-	count = _adcli_strv_len (enroll->service_principals);
e06942
+	if (!enroll->is_service) {
e06942
+		return_unexpected_if_fail (enroll->service_principals);
e06942
+		count = _adcli_strv_len (enroll->service_principals);
e06942
+	}
e06942
 
e06942
 	k5 = adcli_conn_get_krb5_context (enroll->conn);
e06942
 	return_unexpected_if_fail (k5 != NULL);
e06942
@@ -556,8 +572,12 @@ static adcli_result
e06942
 lookup_computer_container (adcli_enroll *enroll,
e06942
                            LDAP *ldap)
e06942
 {
e06942
-	char *attrs[] = { "wellKnownObjects", NULL };
e06942
-	char *prefix = "B:32:AA312825768811D1ADED00C04FD8D5CD:";
e06942
+	char *attrs[] = { enroll->is_service ? "otherWellKnownObjects"
e06942
+	                                     : "wellKnownObjects", NULL };
e06942
+	const char *prefix = enroll->is_service ? "B:32:1EB93889E40C45DF9F0C64D23BBB6237:"
e06942
+	                                        : "B:32:AA312825768811D1ADED00C04FD8D5CD:";
e06942
+	const char *filter = enroll->is_service ? "(&(objectClass=container)(cn=Managed Service Accounts))"
e06942
+	                                        : "(&(objectClass=container)(cn=Computers))";
e06942
 	int prefix_len;
e06942
 	LDAPMessage *results;
e06942
 	const char *base;
e06942
@@ -586,7 +606,7 @@ lookup_computer_container (adcli_enroll *enroll,
e06942
 		                                   "Couldn't lookup computer container: %s", base);
e06942
 	}
e06942
 
e06942
-	values = _adcli_ldap_parse_values (ldap, results, "wellKnownObjects");
e06942
+	values = _adcli_ldap_parse_values (ldap, results, attrs[0]);
e06942
 	ldap_msgfree (results);
e06942
 
e06942
 	prefix_len = strlen (prefix);
e06942
@@ -604,8 +624,7 @@ lookup_computer_container (adcli_enroll *enroll,
e06942
 
e06942
 	/* Try harder */
e06942
 	if (!enroll->computer_container) {
e06942
-		ret = ldap_search_ext_s (ldap, base, LDAP_SCOPE_BASE,
e06942
-		                         "(&(objectClass=container)(cn=Computers))",
e06942
+		ret = ldap_search_ext_s (ldap, base, LDAP_SCOPE_BASE, filter,
e06942
 		                         attrs, 0, NULL, NULL, NULL, -1, &results);
e06942
 		if (ret == LDAP_SUCCESS) {
e06942
 			enroll->computer_container = _adcli_ldap_parse_dn (ldap, results);
e06942
@@ -747,7 +766,7 @@ static adcli_result
e06942
 create_computer_account (adcli_enroll *enroll,
e06942
                          LDAP *ldap)
e06942
 {
e06942
-	char *vals_objectClass[] = { "computer", NULL };
e06942
+	char *vals_objectClass[] = { enroll->is_service ? "msDS-ManagedServiceAccount" : "computer", NULL };
e06942
 	LDAPMod objectClass = { LDAP_MOD_ADD, "objectClass", { vals_objectClass, } };
e06942
 	char *vals_sAMAccountName[] = { enroll->computer_sam, NULL };
e06942
 	LDAPMod sAMAccountName = { LDAP_MOD_ADD, "sAMAccountName", { vals_sAMAccountName, } };
e06942
@@ -806,7 +825,7 @@ create_computer_account (adcli_enroll *enroll,
e06942
 	m = 0;
e06942
 	for (c = 0; c < mods_count - 1; c++) {
e06942
 		/* Skip empty LDAP sttributes */
e06942
-		if (all_mods[c]->mod_vals.modv_strvals[0] != NULL) {
e06942
+		if (all_mods[c]->mod_vals.modv_strvals != NULL && all_mods[c]->mod_vals.modv_strvals[0] != NULL) {
e06942
 			mods[m++] = all_mods[c];
e06942
 		}
e06942
 	}
e06942
@@ -936,7 +955,7 @@ locate_computer_account (adcli_enroll *enroll,
e06942
                          LDAPMessage **rresults,
e06942
                          LDAPMessage **rentry)
e06942
 {
e06942
-	char *attrs[] = { "1.1", NULL };
e06942
+	char *attrs[] = { "objectClass", NULL };
e06942
 	LDAPMessage *results = NULL;
e06942
 	LDAPMessage *entry = NULL;
e06942
 	const char *base;
e06942
@@ -948,7 +967,9 @@ locate_computer_account (adcli_enroll *enroll,
e06942
 	/* If we don't yet know our computer dn, then try and find it */
e06942
 	value = _adcli_ldap_escape_filter (enroll->computer_sam);
e06942
 	return_unexpected_if_fail (value != NULL);
e06942
-	if (asprintf (&filter, "(&(objectClass=computer)(sAMAccountName=%s))", value) < 0)
e06942
+	if (asprintf (&filter, "(&(objectClass=%s)(sAMAccountName=%s))",
e06942
+	              enroll->is_service ? "msDS-ManagedServiceAccount" : "computer",
e06942
+	              value) < 0)
e06942
 		return_unexpected_if_reached ();
e06942
 	free (value);
e06942
 
e06942
@@ -962,8 +983,11 @@ locate_computer_account (adcli_enroll *enroll,
e06942
 	if (ret == LDAP_SUCCESS) {
e06942
 		entry = ldap_first_entry (ldap, results);
e06942
 
e06942
-		/* If we found a computer account, make note of dn */
e06942
+		/* If we found a computer/service account, make note of dn */
e06942
 		if (entry) {
e06942
+			if (!enroll->is_service_explicit) {
e06942
+				check_if_service ( enroll, ldap, results);
e06942
+			}
e06942
 			dn = ldap_get_dn (ldap, entry);
e06942
 			free (enroll->computer_dn);
e06942
 			enroll->computer_dn = strdup (dn);
e06942
@@ -1003,7 +1027,7 @@ load_computer_account (adcli_enroll *enroll,
e06942
                        LDAPMessage **rresults,
e06942
                        LDAPMessage **rentry)
e06942
 {
e06942
-	char *attrs[] = { "1.1", NULL };
e06942
+	char *attrs[] = { "objectClass", NULL };
e06942
 	LDAPMessage *results = NULL;
e06942
 	LDAPMessage *entry = NULL;
e06942
 	int ret;
e06942
@@ -1081,6 +1105,12 @@ locate_or_create_computer_account (adcli_enroll *enroll,
e06942
 	if (res == ADCLI_SUCCESS && entry == NULL)
e06942
 		res = create_computer_account (enroll, ldap);
e06942
 
e06942
+	/* Service account already exists, just continue and update the
e06942
+	 * password */
e06942
+	if (enroll->is_service && entry != NULL) {
e06942
+		res = ADCLI_SUCCESS;
e06942
+	}
e06942
+
e06942
 	if (results)
e06942
 		ldap_msgfree (results);
e06942
 
e06942
@@ -1413,6 +1443,11 @@ update_computer_account (adcli_enroll *enroll)
e06942
 	LDAP *ldap;
e06942
 	char *value = NULL;
e06942
 
e06942
+	/* No updates for service accounts */
e06942
+	if (enroll->is_service) {
e06942
+		return;
e06942
+	}
e06942
+
e06942
 	ldap = adcli_conn_get_ldap_connection (enroll->conn);
e06942
 	return_if_fail (ldap != NULL);
e06942
 
e06942
@@ -1501,6 +1536,11 @@ update_service_principals (adcli_enroll *enroll)
e06942
 	LDAP *ldap;
e06942
 	int ret;
e06942
 
e06942
+	/* No updates for service accounts */
e06942
+	if (enroll->is_service) {
e06942
+		return ADCLI_SUCCESS;
e06942
+	}
e06942
+
e06942
 	ldap = adcli_conn_get_ldap_connection (enroll->conn);
e06942
 	return_unexpected_if_fail (ldap != NULL);
e06942
 
e06942
@@ -1614,6 +1654,8 @@ load_keytab_entry (krb5_context k5,
e06942
 			enroll->computer_name = name;
e06942
 			name[len - 1] = '\0';
e06942
 			_adcli_info ("Found computer name in keytab: %s", name);
e06942
+			adcli_conn_set_computer_name (enroll->conn,
e06942
+			                              enroll->computer_name);
e06942
 			name = NULL;
e06942
 
e06942
 		} else if (!enroll->host_fqdn && _adcli_str_has_prefix (name, "host/") && strchr (name, '.')) {
e06942
@@ -2002,17 +2044,25 @@ adcli_enroll_prepare (adcli_enroll *enroll,
e06942
 
e06942
 	adcli_clear_last_error ();
e06942
 
e06942
-	/* Basic discovery and figuring out enroll params */
e06942
-	res = ensure_host_fqdn (res, enroll);
e06942
-	res = ensure_computer_name (res, enroll);
e06942
-	res = ensure_computer_sam (res, enroll);
e06942
-	res = ensure_user_principal (res, enroll);
e06942
-	res = ensure_computer_password (res, enroll);
e06942
-	if (!(flags & ADCLI_ENROLL_NO_KEYTAB))
e06942
+	if (enroll->is_service) {
e06942
+		/* Ensure basic params for service accounts */
e06942
+		res = ensure_computer_sam (res, enroll);
e06942
+		res = ensure_computer_password (res, enroll);
e06942
 		res = ensure_host_keytab (res, enroll);
e06942
-	res = ensure_service_names (res, enroll);
e06942
-	res = ensure_service_principals (res, enroll);
e06942
-	res = ensure_keytab_principals (res, enroll);
e06942
+		res = ensure_keytab_principals (res, enroll);
e06942
+	} else {
e06942
+		/* Basic discovery and figuring out enroll params */
e06942
+		res = ensure_host_fqdn (res, enroll);
e06942
+		res = ensure_computer_name (res, enroll);
e06942
+		res = ensure_computer_sam (res, enroll);
e06942
+		res = ensure_user_principal (res, enroll);
e06942
+		res = ensure_computer_password (res, enroll);
e06942
+		if (!(flags & ADCLI_ENROLL_NO_KEYTAB))
e06942
+			res = ensure_host_keytab (res, enroll);
e06942
+		res = ensure_service_names (res, enroll);
e06942
+		res = ensure_service_principals (res, enroll);
e06942
+		res = ensure_keytab_principals (res, enroll);
e06942
+	}
e06942
 
e06942
 	return res;
e06942
 }
e06942
@@ -2157,6 +2207,58 @@ enroll_join_or_update_tasks (adcli_enroll *enroll,
e06942
 	return update_keytab_for_principals (enroll, flags);
e06942
 }
e06942
 
e06942
+static adcli_result
e06942
+adcli_enroll_add_description_for_service_account (adcli_enroll *enroll)
e06942
+{
e06942
+	const char *fqdn;
e06942
+	char *desc;
e06942
+
e06942
+	fqdn = adcli_conn_get_host_fqdn (enroll->conn);
e06942
+	return_unexpected_if_fail (fqdn != NULL);
e06942
+	if (asprintf (&desc, "Please do not edit, Service account for %s, "
e06942
+	                     "managed by adcli.", fqdn) < 0) {
e06942
+		return_unexpected_if_reached ();
e06942
+	}
e06942
+
e06942
+	adcli_enroll_set_description (enroll, desc);
e06942
+	free (desc);
e06942
+
e06942
+	return ADCLI_SUCCESS;
e06942
+}
e06942
+
e06942
+static adcli_result
e06942
+adcli_enroll_add_keytab_for_service_account (adcli_enroll *enroll)
e06942
+{
e06942
+	krb5_context k5;
e06942
+	krb5_error_code code;
e06942
+	char def_keytab_name[MAX_KEYTAB_NAME_LEN];
e06942
+	char *lc_dom_name;
e06942
+	int ret;
e06942
+
e06942
+	if (adcli_enroll_get_keytab_name (enroll) == NULL) {
e06942
+		k5 = adcli_conn_get_krb5_context (enroll->conn);
e06942
+		return_unexpected_if_fail (k5 != NULL);
e06942
+
e06942
+		code = krb5_kt_default_name (k5, def_keytab_name,
e06942
+		                             sizeof (def_keytab_name));
e06942
+		return_unexpected_if_fail (code == 0);
e06942
+
e06942
+		lc_dom_name = strdup (adcli_conn_get_domain_name (enroll->conn));
e06942
+		return_unexpected_if_fail (lc_dom_name != NULL);
e06942
+		_adcli_str_down (lc_dom_name);
e06942
+
e06942
+
e06942
+		ret = asprintf (&enroll->keytab_name, "%s.%s", def_keytab_name,
e06942
+		                                             lc_dom_name);
e06942
+		free (lc_dom_name);
e06942
+		return_unexpected_if_fail (ret > 0);
e06942
+	}
e06942
+
e06942
+	_adcli_info ("Using service account keytab: %s", enroll->keytab_name);
e06942
+
e06942
+	return ADCLI_SUCCESS;
e06942
+}
e06942
+
e06942
 adcli_result
e06942
 adcli_enroll_join (adcli_enroll *enroll,
e06942
                    adcli_enroll_flags flags)
e06942
@@ -2172,7 +2274,14 @@ adcli_enroll_join (adcli_enroll *enroll,
e06942
 	if (res != ADCLI_SUCCESS)
e06942
 		return res;
e06942
 
e06942
-	res = ensure_default_service_names (enroll);
e06942
+	if (enroll->is_service) {
e06942
+		res = adcli_enroll_add_description_for_service_account (enroll);
e06942
+		if (res == ADCLI_SUCCESS) {
e06942
+			res = adcli_enroll_add_keytab_for_service_account (enroll);
e06942
+		}
e06942
+	} else {
e06942
+		res = ensure_default_service_names (enroll);
e06942
+	}
e06942
 	if (res != ADCLI_SUCCESS)
e06942
 		return res;
e06942
 
e06942
@@ -2281,6 +2390,11 @@ adcli_enroll_update (adcli_enroll *enroll,
e06942
 	}
e06942
 	free (value);
e06942
 
e06942
+	/* We only support password changes for service accounts */
e06942
+	if (enroll->is_service && (flags & ADCLI_ENROLL_PASSWORD_VALID)) {
e06942
+		return ADCLI_SUCCESS;
e06942
+	}
e06942
+
e06942
 	return enroll_join_or_update_tasks (enroll, flags);
e06942
 }
e06942
 
e06942
diff --git a/tools/computer.c b/tools/computer.c
e06942
index 5a97d8b..63fd374 100644
e06942
--- a/tools/computer.c
e06942
+++ b/tools/computer.c
e06942
@@ -1074,3 +1074,128 @@ adcli_tool_computer_show (adcli_conn *conn,
e06942
 	adcli_enroll_unref (enroll);
e06942
 	return 0;
e06942
 }
e06942
+
e06942
+int
e06942
+adcli_tool_computer_managed_service_account (adcli_conn *conn,
e06942
+                                             int argc,
e06942
+                                             char *argv[])
e06942
+{
e06942
+	adcli_enroll *enroll;
e06942
+	adcli_result res;
e06942
+	int show_password = 0;
e06942
+	int details = 0;
e06942
+	int opt;
e06942
+
e06942
+	struct option options[] = {
e06942
+		{ "domain", required_argument, NULL, opt_domain },
e06942
+		{ "domain-realm", required_argument, NULL, opt_domain_realm },
e06942
+		{ "domain-controller", required_argument, NULL, opt_domain_controller },
e06942
+		{ "use-ldaps", no_argument, 0, opt_use_ldaps },
e06942
+		{ "login-user", required_argument, NULL, opt_login_user },
e06942
+		{ "login-ccache", optional_argument, NULL, opt_login_ccache },
e06942
+		{ "host-fqdn", required_argument, 0, opt_host_fqdn },
e06942
+		{ "computer-name", required_argument, 0, opt_computer_name },
e06942
+		{ "host-keytab", required_argument, 0, opt_host_keytab },
e06942
+		{ "no-password", no_argument, 0, opt_no_password },
e06942
+		{ "stdin-password", no_argument, 0, opt_stdin_password },
e06942
+		{ "prompt-password", no_argument, 0, opt_prompt_password },
e06942
+		{ "domain-ou", required_argument, NULL, opt_domain_ou },
e06942
+		{ "show-details", no_argument, NULL, opt_show_details },
e06942
+		{ "show-password", no_argument, NULL, opt_show_password },
e06942
+		{ "verbose", no_argument, NULL, opt_verbose },
e06942
+		{ "help", no_argument, NULL, 'h' },
e06942
+		{ 0 },
e06942
+	};
e06942
+
e06942
+	static adcli_tool_desc usages[] = {
e06942
+		{ 0, "usage: adcli create-msa --domain=xxxx" },
e06942
+		{ 0 },
e06942
+	};
e06942
+
e06942
+	enroll = adcli_enroll_new (conn);
e06942
+	if (enroll == NULL) {
e06942
+		warnx ("unexpected memory problems");
e06942
+		return -1;
e06942
+	}
e06942
+
e06942
+	while ((opt = adcli_tool_getopt (argc, argv, options)) != -1) {
e06942
+		switch (opt) {
e06942
+		case opt_one_time_password:
e06942
+			adcli_conn_set_allowed_login_types (conn, ADCLI_LOGIN_COMPUTER_ACCOUNT);
e06942
+			adcli_conn_set_computer_password (conn, optarg);
e06942
+			break;
e06942
+		case opt_show_details:
e06942
+			details = 1;
e06942
+			break;
e06942
+		case opt_show_password:
e06942
+			show_password = 1;
e06942
+			break;
e06942
+		case 'h':
e06942
+		case '?':
e06942
+		case ':':
e06942
+			adcli_tool_usage (options, usages);
e06942
+			adcli_tool_usage (options, common_usages);
e06942
+			adcli_enroll_unref (enroll);
e06942
+			return opt == 'h' ? 0 : 2;
e06942
+		default:
e06942
+			res = parse_option ((Option)opt, optarg, conn, enroll);
e06942
+			if (res != ADCLI_SUCCESS) {
e06942
+				adcli_enroll_unref (enroll);
e06942
+				return res;
e06942
+			}
e06942
+			break;
e06942
+		}
e06942
+	}
e06942
+
e06942
+	argc -= optind;
e06942
+	argv += optind;
e06942
+
e06942
+	if (argc == 1)
e06942
+		adcli_conn_set_domain_name (conn, argv[0]);
e06942
+	else if (argc > 1) {
e06942
+		warnx ("extra arguments specified");
e06942
+		adcli_enroll_unref (enroll);
e06942
+		return 2;
e06942
+	}
e06942
+
e06942
+	if (adcli_conn_get_domain_name (conn) == NULL) {
e06942
+		warnx ("domain name is required");
e06942
+		adcli_enroll_unref (enroll);
e06942
+		return 2;
e06942
+	}
e06942
+
e06942
+	adcli_enroll_set_is_service (enroll, true);
e06942
+	adcli_conn_set_allowed_login_types (conn, ADCLI_LOGIN_USER_ACCOUNT);
e06942
+
e06942
+	res = adcli_enroll_load (enroll);
e06942
+	if (res != ADCLI_SUCCESS) {
e06942
+		/* ignored */
e06942
+	}
e06942
+
e06942
+	res = adcli_conn_connect (conn);
e06942
+	if (res != ADCLI_SUCCESS) {
e06942
+		warnx ("couldn't connect to %s domain: %s",
e06942
+		       adcli_conn_get_domain_name (conn),
e06942
+		       adcli_get_last_error ());
e06942
+		adcli_enroll_unref (enroll);
e06942
+		return -res;
e06942
+	}
e06942
+
e06942
+	res = adcli_enroll_join (enroll, 0);
e06942
+	if (res != ADCLI_SUCCESS) {
e06942
+		warnx ("Adding service account for %s failed: %s",
e06942
+		       adcli_conn_get_domain_name (conn),
e06942
+		       adcli_get_last_error ());
e06942
+		adcli_enroll_unref (enroll);
e06942
+		return -res;
e06942
+	}
e06942
+
e06942
+	if (details)
e06942
+		dump_details (conn, enroll, show_password);
e06942
+	else if (show_password)
e06942
+		dump_password (conn, enroll);
e06942
+
e06942
+	adcli_enroll_unref (enroll);
e06942
+
e06942
+	return 0;
e06942
+}
e06942
diff --git a/tools/tools.c b/tools/tools.c
e06942
index 1b6d879..d0dcf98 100644
e06942
--- a/tools/tools.c
e06942
+++ b/tools/tools.c
e06942
@@ -60,6 +60,7 @@ struct {
e06942
 	{ "reset-computer", adcli_tool_computer_reset, "Reset a computer account", },
e06942
 	{ "delete-computer", adcli_tool_computer_delete, "Delete a computer account", },
e06942
 	{ "show-computer", adcli_tool_computer_show, "Show computer account attributes stored in AD", },
e06942
+	{ "create-msa", adcli_tool_computer_managed_service_account, "Create a managed service account in the given AD domain", },
e06942
 	{ "create-user", adcli_tool_user_create, "Create a user account", },
e06942
 	{ "delete-user", adcli_tool_user_delete, "Delete a user account", },
e06942
 	{ "create-group", adcli_tool_group_create, "Create a group", },
e06942
diff --git a/tools/tools.h b/tools/tools.h
e06942
index 3702875..82d5e4e 100644
e06942
--- a/tools/tools.h
e06942
+++ b/tools/tools.h
e06942
@@ -82,6 +82,10 @@ int       adcli_tool_computer_show     (adcli_conn *conn,
e06942
                                         int argc,
e06942
                                         char *argv[]);
e06942
 
e06942
+int       adcli_tool_computer_managed_service_account (adcli_conn *conn,
e06942
+                                                       int argc,
e06942
+                                                       char *argv[]);
e06942
+
e06942
 int       adcli_tool_user_create       (adcli_conn *conn,
e06942
                                         int argc,
e06942
                                         char *argv[]);
e06942
-- 
e06942
2.28.0
e06942