Skip to content

Instantly share code, notes, and snippets.

@triti
Created April 28, 2014 14:25
Show Gist options
  • Save triti/11373719 to your computer and use it in GitHub Desktop.
Save triti/11373719 to your computer and use it in GitHub Desktop.
Set AD options via DeployStudio parameters or custom properties.
#!/bin/bash
# https://gist.github.com/11373719
readonly SCRIPT_NAME=${0##*/}
readonly SCRIPT_VERSION=3.1
printf '%s - %s (%s)\n' "${SCRIPT_NAME}" "${SCRIPT_VERSION}" "$(/bin/date)"
readonly AD_PLIST="/etc/deploystudio/bin/ds_active_directory_binding.plist"
readonly AD_PLIST_PATH="/Volumes/${DS_LAST_SELECTED_TARGET}${AD_PLIST}"
function die () {
local st=$?
printf "%b\n" "$*" >&2
exit $st
}
function assign () {
if [[ "$2" == "directly" ]]; then
# Assign the value of $2 to the variable name referenced in $1
printf -v "$1" '%s' "$3"
printf '%s variable %s set directly to the value "%s"\n' "$4" "$1" "$3"
elif [[ "$2" == "indirectly" ]]; then
# Assign the value of the variable name referenced by $3 to the variable name referenced in $1
printf -v "$1" '%s' "${!3}"
printf '%s variable %s set indirectly from variable %s to the value "%s"\n' "$4" "$1" "$3" "${!1}"
fi
}
function configure_ad_plist () {
printf 'Deleting key %s in plist file %s\n' "$1" "${AD_PLIST_PATH}"
local cmd
printf -v cmd 'Delete :%s' "$1"
/usr/libexec/PlistBuddy -c "$cmd" "${AD_PLIST_PATH}" > /dev/null 2>&1
printf 'Setting key %s to quoted value "%s" in plist file %s\n' "$1" "$2" "${AD_PLIST_PATH}"
printf -v cmd 'Add :%s string "%q"' "$1" "$2"
/usr/libexec/PlistBuddy -c "$cmd" "${AD_PLIST_PATH}" || die "PlistBuddy command '$cmd' failed!"
}
function is_valid_shell () {
/usr/bin/grep -qs "$1" /etc/shells > /dev/null 2>&1
}
[[ -e "${AD_PLIST_PATH}" ]] || die "Could not find ${AD_PLIST_PATH}. Quitting."
# DSCP_AD_DOMAIN (domain.example.com)
ad_domain=""
ad_domain_desc="AD Domain"
ad_domain_key="domain"
# DSCP_AD_GROUPS (group 1,group 2,group 3)
ad_groups=""
ad_groups_desc="AD Admin Groups"
ad_groups_key="admingroups"
# DSCP_AD_MOBILE (enable/disable)
ad_mobile=""
ad_mobile_desc="AD Mobile Account"
ad_mobile_key="mobile"
# DSCP_AD_OU (OU=Foo Bar,OU=Baz,DC=domain,DC=example,DC=com)
ad_ou=""
ad_ou_desc="AD Organizational Unit"
ad_ou_key="ou"
# DSCP_AD_PASSINTERVAL (number)
ad_passinterval=""
ad_passinterval_desc="AD Password Change Interval"
ad_passinterval_key="passinterval"
# DSCP_AD_SHELL (/path/to/shell)
ad_shell=""
ad_shell_desc="AD User Shell"
ad_shell_key="shell"
OPTIND=1
while getopts ":d:D:g:G:m:M:o:O:p:P:s:S:" opt; do
case "$opt" in
d ) assign ad_domain directly "$OPTARG" "$ad_domain_desc" ;;
D ) assign ad_domain indirectly "$OPTARG" "$ad_domain_desc" ;;
g ) assign ad_groups directly "$OPTARG" "$ad_groups_desc" ;;
G ) assign ad_groups indirectly "$OPTARG" "$ad_groups_desc" ;;
m ) assign ad_mobile directly "$OPTARG" "$ad_mobile_desc" ;;
M ) assign ad_mobile indirectly "$OPTARG" "$ad_mobile_desc" ;;
o ) assign ad_ou directly "$OPTARG" "$ad_ou_desc" ;;
O ) assign ad_ou indirectly "$OPTARG" "$ad_ou_desc" ;;
p ) assign ad_passinterval directly "$OPTARG" "$ad_passinterval_desc" ;;
P ) assign ad_passinterval indirectly "$OPTARG" "$ad_passinterval_desc" ;;
s ) assign ad_shell directly "$OPTARG" "$ad_shell_desc" ;;
S ) assign ad_shell indirectly "$OPTARG" "$ad_shell_desc" ;;
esac
done
shift $((OPTIND-1))
if [[ -n "$ad_domain" ]]; then
configure_ad_plist "$ad_domain_key" "$ad_domain"
fi
if [[ -n "$ad_groups" ]]; then
configure_ad_plist "$ad_groups_key" "$ad_groups"
fi
if [[ -n "$ad_mobile" ]]; then
if [[ $ad_mobile = "enable" || $ad_mobile = "disable" ]]; then
configure_ad_plist "$ad_mobile_key" "$ad_mobile"
else
printf '%s must specify either "enable" or "disable". Skipping.\n' "$ad_mobile_desc"
fi
fi
if [[ -n "$ad_ou" ]]; then
configure_ad_plist "$ad_ou_key" "$ad_ou"
fi
if [[ -n "$ad_passinterval" ]]; then
if [[ $ad_passinterval != *[!0-9]* ]]; then
configure_ad_plist "$ad_passinterval_key" "$ad_passinterval"
else
printf '%s must be strictly numeric. Skipping.\n' "$ad_passinterval_desc"
fi
fi
if [[ -n "$ad_shell" ]]; then
if is_valid_shell "$ad_shell"; then
configure_ad_plist "$ad_shell_key" "$ad_shell"
else
printf '%s is not a valid shell. Skipping.\n' "$ad_shell_desc"
fi
fi
printf 'Success!\n'
printf '%s - end\n' "${SCRIPT_NAME}"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment