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