How to Run ldap-check.sh on Mattermost Server

Purpose

Setting up the ldap-check script to allow System Administrator to run an ldapsearch to verify the availability of AD/LDAP users as an alternative to the standard ldapsearch command.

Steps

#!/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/-g [username/groupname]"
	echo "example: ./ldap-check.sh -u john"
	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`
EmailAttribute=`cat $config_file | jq -r .LdapSettings.EmailAttribute`
UsernameAttribute=`cat $config_file | jq -r .LdapSettings.UsernameAttribute`
IdAttribute=`cat $config_file | jq -r .LdapSettings.IdAttribute`
GroupFilter=`cat $config_file | jq -r .LdapSettings.GroupFilter`
GroupIdAttribute=`cat $config_file | jq -r .LdapSettings.GroupIdAttribute`

if [[ -z ${UserFilter} ]]; then
	UserFilter="($IdAttribute=$2)"
else
	UserFilter="(&($IdAttribute=$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"
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
  • Set the script to be executable by the user:
sudo chmod +x ldap-check.sh
  • Run the ./ldap-check.sh -<u_or_g> <username_or_groupname>. For example:
ahmaddanial@mattermost:/opt/mattermost/bin$ ./ldap-check.sh -u ahmad.danial
Looking for config.json
Found config at ../config/config.json
ldapsearch -LLL -x -h XXX.X.XX.XXX -p 389 -D "cn=admin,dc=mattermost,dc=com" -w "Testing" -b "ou=people,dc=mattermost,dc=com" "(&(uid=ahmad.danial)(objectClass=inetOrgPerson))" uid displayName mail
-------------------------
dn:: Y249QmVuZGVyIEJlbmRpbmcgUm9kcsOtZ3VleixvdT1wZW9wbGUsZGM9cGxhbmV0ZXhwcmVzc
 yxkYz1jb20=
displayName: AhmadDanial
mail: ahmad.danial@mattermost.com
uid: ahmad.danial
ahmaddanial@mattermost:/opt/mattermost/bin$ ./ldap-check.sh -g mattermost_team
Looking for config.json
Found config at ../config/config.json
ldapsearch -LLL -x -h XXX.X.XX.XXX -p 389 -D "cn=admin,dc=mattermost,dc=com" -w "Testing" -b "ou=people,dc=mattermost,dc=com" "(&(cn=mattermost_team)(objectClass=Group))"
-------------------------
dn: cn=mattermost_team,ou=people,dc=mattermost,dc=com
objectClass: Group
objectClass: top
groupType: 2147483650
cn: mattermost_team
member: cn=Ahmad Danial,ou=people,dc=planetexpress,dc=com
member: cn=Danny Mohammad,ou=people,dc=planetexpress,dc=com
member:: Y249QmVuZGVyIEJlbmRpbmcgUm9kcsOtZ3VleixvdT1wZW9wbGUsZGM9cGxhbmV0ZXhwc
 mVzcyxkYz1jb20=