AD for Debian with SaltStack – Part 5 – AD Join Script

Joining Active Directory is currently a manual operation. There is no reason why it couldn’t be done by SaltStack – that is left as an exercise for the reader!

We copy the join script to the machine so the administrator can run it.

    - source: salt://common/ad/
    - user: root
    - group: root
    - mode: '0755'
    - makedirs: True

The AD join script looks like this:


# Script to handle the process of joining the domain.
# Run with username where:
# username is the SAM name for the domain admins account.
# The script produces lots of output to help with debugging any issues. See
# for a full description of the process (albeit RedHat rather than Debian).
# Note that it is very important that the hostname of the machine is set up correctly!
# The hostname command must return the FQDN of the host. 

if [ "$#" != 1 ]
  echo "Usage: username"
  exit 1

domain=$(hostname --domain)
echo "Using account ${username} to join domain ${domain}..."
echo ""

# Quit on error
set -e

# Uncomment to debug
#set -x

# Upper-case version of the domain name
upper_domain=$(echo ${domain} | tr [a-z] [A-Z])

# Find the short hostname and get an upper-case version of it
short_hostname=$(hostname --short)
upper_short_hostname=$(echo ${short_hostname} | tr [a-z] [A-Z])

# Log into the domain as the administrator, asking user for password
# The domain part must be in upper-case
echo "Logging into domain as the administrator"
/usr/bin/kinit "${username}@${upper_domain}"
echo ""

# List what kerberos sent back
echo "Listing kerberos tickets for the domain administrator:"
echo "------------------------------------------------------------------------"
echo ""

# Join AD and put the machine credentials in the krb5.keytab
echo "Requesting domain join using administrator kerberos ticket"
net ads join -k

# List the machine credentials
echo "Listing kerberos tickets for the machine:"
echo "------------------------------------------------------------------------"
klist -k
echo ""

# Wait for 5s to allow everything to catch up
echo "Waiting for everything to catch up..."
sleep 5
echo ""

# Sign in using the machine credentials
echo "Signing in using machine credentials ${upper_short_hostname}$"
kinit -k ${upper_short_hostname}$
echo ""

# Did it work?

if [ $joinedAd -ne 0 ]
    echo "Error: could not join the domain with machine credentials ${upper_short_hostname}$"
    exit 1
    echo "Joined the domain using machine credentials ${upper_short_hostname}$"
    echo ""
    echo "Listing kerberos machine ticket:"
    echo "------------------------------------------------------------------------"
    echo ""

    # Now restart SSSD and everything should be happy :-)
    echo "Enabling and restarting sssd"
    systemctl enable sssd
    systemctl restart sssd
    if [ $? -ne 0 ]
      echo "Error: could not start the System Security Services Daemon (SSSD)"
      exit 1
      echo "System Security Services Daemon (SSSD) restarted and enabled."
      echo "AD should now be working!"

exit 0

Given the delays inherent in AD the sleep 5 should be replaced by polling the kinit -k ${upper_short_hostname}$ line until the join works or 10 minutes has expired. Again, this is left as an exercise for the reader!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.