Skip to content

Instantly share code, notes, and snippets.

@icelander
Created May 12, 2021 19:36
Show Gist options
  • Save icelander/91feec37cea08e507747060c27fb7875 to your computer and use it in GitHub Desktop.
Save icelander/91feec37cea08e507747060c27fb7875 to your computer and use it in GitHub Desktop.
Queries an LDAP server like Mattermost. Be sure to set the configuration values to match your Mattermost config
#!/bin/bash
LdapServer="LdapServer"
LdapPort="LdapPort"
BindUsername="BindUsername"
BindPassword="BindPassword"
BaseDN="BaseDN"
UserFilter="UserFilter"
GroupFilter="GroupFilter"
IdAttribute="IdAttribute"
EmailAttribute="EmailAttribute"
LoginIdAttribute="LoginIdAttribute"
UsernameAttribute="UsernameAttribute"
FirstNameAttribute="FirstNameAttribute"
LastNameAttribute="LastNameAttribute"
PositionAttribute="PositionAttribute"
GroupDisplayNameAttribute=="GroupDisplayNameAttribute="
GroupIdAttribute="GroupIdAttribute"
ldapsearch_cmd=ldapsearch
[[ $(type -P "$ldapsearch_cmd") ]] || {
echo "'$ldapsearch_cmd' shell accessible interface to ldap not found";
echo "Please install on linux with 'sudo apt-get install ldap-utils'"
exit 1;
}
if [[ -z ${1} ]]; then
echo "We could not find a username";
echo "usage: ./ldap-check.sh -u/-i/-g [username/ID attribute/groupname]"
echo "example: ./ldap-check.sh -u john"
echo "example: ./ldap-check.sh -i 77a65590-16b9-103b-86e5-bf7e5461059d"
echo "example: ./ldap-check.sh -g admin-staff"
exit 1;
fi
SearchAttribute="$IdAttribute"
if [[ $1 == '-u' ]]; then
SearchAttribute="$UsernameAttribute"
fi
if [[ -z ${UserFilter} ]]; then
UserFilter="($SearchAttribute=$2)"
else
UserFilter="(&($SearchAttribute=$2)$UserFilter)"
fi
if [[ -z ${GroupFilter} ]]; then
GroupFilter="($GroupIdAttribute=$2)"
else
GroupFilter="(&($GroupIdAttribute=$2)$GroupFilter)"
fi
if [[ $1 == '-u' ]]; then
cmd_to_run="$ldapsearch_cmd -LLL -x -h $LdapServer -p $LdapPort -D \"$BindUsername\" -w \"$BindPassword\" -b \"$BaseDN\" \"$UserFilter\" $IdAttribute $UsernameAttribute $EmailAttribute $LoginIdAttribute $FirstNameAttribtue $LastNameAttribute $PositionAttribute"
echo $cmd_to_run
echo "-------------------------"
eval $cmd_to_run
elif [[ $1 == '-g' ]]; then
cmd_to_run="$ldapsearch_cmd -LLL -x -h $LdapServer -p $LdapPort -D \"$BindUsername\" -w \"$BindPassword\" -b \"$BaseDN\" \"$GroupFilter\""
echo $cmd_to_run
echo "-------------------------"
eval $cmd_to_run
else
echo "User or Group not specified"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment