Skip to content

Instantly share code, notes, and snippets.

@icelander
Created May 11, 2021 22:08
Show Gist options
  • Save icelander/084dd468b53c4380d3cc17177ff8c7ce to your computer and use it in GitHub Desktop.
Save icelander/084dd468b53c4380d3cc17177ff8c7ce to your computer and use it in GitHub Desktop.
And improved script to check Mattermost LDAP config
#!/bin/bash
jq_cmd=jq
[[ $(type -P "$jq_cmd") ]] || {
echo "'$jq_cmd' command line JSON processor not found";
echo "Please install on linux with 'sudo apt-get install jq'"
echo "Please install on mac with 'brew install jq'"
exit 1;
}
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
echo "Looking for config.json"
config_file=
if [[ -e "./config.json" ]]; then
config_file="./config.json"
echo "Found config at $config_file";
fi
if [[ -z ${config_file} && -e "./config/config.json" ]]; then
config_file="./config/config.json"
echo "Found config at $config_file";
fi
if [[ -z ${config_file} && -e "../config/config.json" ]]; then
config_file="../config/config.json"
echo "Found config at $config_file";
fi
if [[ -z ${config_file} ]]; then
echo "We could not find config.json";
exit 1;
fi
LdapServer=`cat $config_file | jq -r .LdapSettings.LdapServer`
LdapPort=`cat $config_file | jq -r .LdapSettings.LdapPort`
BindUsername=`cat $config_file | jq -r .LdapSettings.BindUsername`
BindPassword=`cat $config_file | jq -r .LdapSettings.BindPassword`
BaseDN=`cat $config_file | jq -r .LdapSettings.BaseDN`
UserFilter=`cat $config_file | jq -r .LdapSettings.UserFilter`
GroupFilter=`cat $config_file | jq -r .LdapSettings.GroupFilter`
IdAttribute=`cat $config_file | jq -r .LdapSettings.IdAttribute`
EmailAttribute=`cat $config_file | jq -r .LdapSettings.EmailAttribute`
LoginIdAttribute=`cat $config_file | jq -r .LdapSettings.LoginIdAttribute`
UsernameAttribute=`cat $config_file | jq -r .LdapSettings.UsernameAttribute`
FirstNameAttribute=`cat $config_file | jq -r .LdapSettings.FirstNameAttribute`
LastNameAttribute=`cat $config_file | jq -r .LdapSettings.LastNameAttribute`
PositionAttribute=`cat $config_file | jq -r .LdapSettings.PositionAttribute`
GroupDisplayNameAttribute==`cat $config_file | jq -r .LdapSettings.GroupDisplayNameAttribute`
GroupIdAttribute=`cat $config_file | jq -r .LdapSettings.GroupIdAttribute`
cat << HERE
Configuration Report:
Server:
- LDAP Connection: $LdapServer:$LdapPort
- LDAP Username: $BindUsername
- LDAP Password: $BindPassword
LDAP:
- Base DN: $BaseDN
- User Filter: $UserFilter
- Group Filter: $GroupFilter
User Attributes:
- User ID: $IdAttribute
- Email: $EmailAttribute
- Username: $UsernameAttribute
- Login: $LoginIdAttribute
- First Name: $FirstNameAttribute
- Last Name: $LastNameAttribute
- Position: $PositionAttribute
Group Attributes:
- Group ID: $GroupIdAttribute
- Group Name: $GroupDisplayNameAttribute
HERE
echo "-------------------------"
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