#!/bin/bash
#
# Author: Jochen Schäfer <jschaef@novell.com>, 2001-2014
# 
# copyright (c) Novell Deutschland GmbH, 2014. All rights reserved.
# GNU Public License
#
# ncif-lib.sh	26 Nov 2014

###################################################################
# get_pass:
#	Password input echoing stars
###################################################################

function get_pass
{
	local edirUser=$1

	echo -n "Please enter the password for $edirUser: "

	function get_input()
	{
		passvar=$1
		unset PASSWORD
		unset CHARCOUNT
		unset PROMPT
		stty -echo

		CHARCOUNT=0
		while IFS= read -p "$PROMPT" -r -s -n 1 CHAR
		do
		    # Enter - accept password
		    if [[ $CHAR == $'\0' ]] ; then
			break
		    fi
		    # Backspace
		    if [[ $CHAR == $'\177' ]] ; then
			if [ $CHARCOUNT -gt 0 ] ; then
			    CHARCOUNT=$((CHARCOUNT-1))
			    PROMPT=$'\b \b'
			    PASSWORD="${PASSWORD%?}"
			else
			    PROMPT=''
			fi
		    else
			CHARCOUNT=$((CHARCOUNT+1))
			PROMPT='*'
			PASSWORD+="$CHAR"
		    fi
		done

		eval ${passvar}=$PASSWORD
		stty echo
		echo
	}
	
	get_input password1

	echo -n "Please reenter the password for $edirUser: "

	get_input password2

	if [ "$password1" != "$password2" -o -z "$password1" ]; then
		echo -ne "\n!!! Password missmatch or password empty, stop execution of $0 here!!!\n"
		exit 7
	else
		password=$password1	
	fi
}

function check_ldap()
{
	local dn=$1
	local passwd=$2

	if [ -n "$dn" -o -n "$passwd" ];  then
		local LDAP_OPTS="-x -ZZ -D $dn -w $passwd -LLL" 
	else	
		local LDAP_OPTS="-x -ZZ -LLL" 
	fi	

	local search_attr=directoryTreeName

	ldapsearch $LDAP_OPTS -s base -b "" $search_attr |grep -qi $search_attr
	
	if [ $? -ne 0 ];then
		if [ -n "$dn" ];then
			echo -ne "\n!!! LDAP Connect with user $dn failed, exiting here!!!\n"
		else
			echo -ne "\n!!! LDAP Connect failed, exiting here!!!\n"
		fi
		exit 8
	fi	
}	


function get_tree()
{
	local dn=$1
	local passwd=$2
	local LDAP_OPTS="-x -ZZ -D $dn -w $passwd -LLL" 
	local search_attr=directoryTreeName
	local tree=$(ldapsearch $LDAP_OPTS -s base -b "" $search_attr |awk  -F': ' '$1~"TreeName" {print $2} ')
	
	if [ $? -ne 0 ];then
		echo -ne "\n!!! LDAP Connect with user $dn failed, exiting here!!!\n"
		exit 9
	else
		echo $tree
	fi	
}	

function get_org()
{
	 grep n4u.nds.server-context  /etc/opt/novell/eDirectory/conf/nds.conf | awk -F"=" '{print $NF}'	
}

function get_comp()
{
	local LOCATION=$1
	echo ${LOCATION:0:4}
}

# parameter 1 - function, parameter 2 - userdn, parameter 3 - groupdn
function add_group_membership()
{
	local my_function=$1
	local user_dn=$2
	local group_dn=$3

	$my_function $user_dn modify add groupMembership $group_dn
	if [ $? -ne 0 ];then
		user_errors+=("$user_dn add groupMembership\nfailed for $group_dn\n")
	fi

	$my_function $user_dn modify add securityEquals $group_dn
	if [ $? -ne 0 ];then
		user_errors+=("$user_dn add securityEquals\nfailed for $group_dn\n")
	fi

	$my_function $group_dn modify add member $user_dn
	if [ $? -ne 0 ];then
		group_errors+=("$group_dn add member\nfailed for $user_dn\n")
	fi
	
	$my_function $group_dn modify add equivalentToMe $user_dn
	if [ $? -ne 0 ];then
		group_errors+=("$group_dn add equivalentToMe\nfailed for $user_dn\n")
	fi
}

function compare_user_group_containers()
{
	local user_dn=$1
	local group_dn=$2
	local c1=$(sed -rne 's#^[^,]*,(.*$)#\1#p' <<<$user_dn)
	local c2=$(sed -rne 's#^[^,]*,(.*$)#\1#p' <<<$group_dn)
	if [ "$c1" == "$c2" ]; then
		echo TRUE
	else
		echo FALSE
	fi
}

function split_lines()
{
   awk 'length > 76 { while ( length($0) > 76 ) {
   printf "%s\n ", substr($0,1,76)
   $0 = substr($0,77)
   }
   if (length) print
        next
   }
  {print}'
}
