#!/bin/sh #------------------------------------------------------------------------------ # /var/install/bin/capi2text-test-ldap - test ldap access # # Copyright (c) 2008-2024 The Eisfair Team, team(at)eisfair(dot)org # # Creation : 2008-11-18 jed # Last Update: $Id$ # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. #------------------------------------------------------------------------------ # read eislib . /var/install/include/eislib #debug_install_package=true if ${debug_install_package:-false} then exec 2>/tmp/$(basename ${0})-trace$$.log set -x ask_debug=true export ask_debug fi configfile=/etc/config.d/capi2text ldap_bin=/usr/bin/ldapsearch ldap_passwd=/root/.capi2text-ldap tmpfile=/tmp/capi2text-test-ldap.$$ #CAPI2TEXT_DO_DEBUG="yes" #------------------------------------------------------------------------------ # print debug message # input : $1 - text string # output: debug message #------------------------------------------------------------------------------ print_debug () { if [ "${CAPI2TEXT_DO_DEBUG}" = "yes" ] then echo "$1" fi } #------------------------------------------------------------------------------ # decode base64 encoded string # input : $1 - text string # output: text string #------------------------------------------------------------------------------ decode_base64 () { _db_ldap_in="$1" # decode string... # 1. decode BASE64 # 2. decode UTF-8 to ISO-8859-1 _db_ldap_out="`echo "${_db_ldap_in}" | perl -MMIME::Base64 -ne 'print decode_base64($_)' | iconv -f UTF-8 -t ISO-8859-1`" echo "${_db_ldap_out}" } #------------------------------------------------------------------------------ # get address details from LDAP #------------------------------------------------------------------------------ ldap_extract () { echo "${1}" | grep -q "::" if [ $? -eq 0 ] then # decode base64 string _ldap_tmp=`echo "${1}" | cut -d' ' -f2` _ldap_out="`decode_base64 ${_ldap_tmp}`" else _ldap_out=`echo "${1}" | cut -d':' -f2` fi echo "${_ldap_out}" | sed -e 's/^ *//g' -e 's/ *$//g' } #------------------------------------------------------------------------------ # combine ldap information # # input : $1 - format string # output: ldap result #------------------------------------------------------------------------------ ldap_combine_info () { _ci_ldap_search='' # read format search string while [ $# -gt 0 ] do if [ -z "${_ci_ldap_search}" ] then _ci_ldap_search="${1}" else _ci_ldap_search="${_ci_ldap_search} ${1}" fi shift done _ci_ldap_result='' _ci_maxlen=1 _ci_orflag=0 while [ ${_ci_maxlen} -gt 0 ] do _ci_maxlen=`expr length "${_ci_ldap_search}"` _ci_char="`expr substr \"${_ci_ldap_search}\" 1 1`" case ${_ci_char} in '|') case ${_ci_orflag} in 0 ) # enable or-mode _ci_orflag=1 ;; * ) # disable or-mode _ci_orflag=0 ;; esac _ci_ldap_search="`echo \"${_ci_ldap_search}\" | sed \"s/^.//\"`" ;; '{') # grep ldap field name if [ ${_ci_orflag} -eq 1 ] then # or-mode active, loop until positive result while [ ${_ci_orflag} -eq 1 ] do _ci_ldap_key="`echo \"${_ci_ldap_search}\" | cut -d{ -f2- | cut -d} -f1`" _ci_ldap_search="`echo \"${_ci_ldap_search}\" | sed \"s/.${_ci_ldap_key}}//\"`" _ci_ldap_tmp=`grep "^${_ci_ldap_key}:*" ${tmpfile} | head -n1` _ci_char="`expr substr \"${_ci_ldap_search}\" 1 1`" if [ -n "${_ci_ldap_tmp}" ] then # positive result, enable or-ignore-mode _ci_ldap_result="${_ci_ldap_result}`ldap_extract \"${_ci_ldap_tmp}\"`" _ci_orflag=2 break else if [ "${_ci_char}" = '|' ] then # disable or-mode _ci_orflag=2 break fi fi done else _ci_ldap_key="`echo \"${_ci_ldap_search}\" | cut -d{ -f2- | cut -d} -f1`" _ci_ldap_search="`echo \"${_ci_ldap_search}\" | sed \"s/.${_ci_ldap_key}}//\"`" if [ ${_ci_orflag} -eq 0 ] then # normal mode, grep result _ci_ldap_tmp=`grep "^${_ci_ldap_key}:*" ${tmpfile} | head -n1` if [ -n "${_ci_ldap_tmp}" ] then _ci_ldap_result="${_ci_ldap_result}`ldap_extract \"${_ci_ldap_tmp}\"`" fi fi fi ;; *) if [ ${_ci_orflag} -eq 0 ] then # normal mode, append character _ci_ldap_result="${_ci_ldap_result}${_ci_char}" _ci_ldap_search=`echo "${_ci_ldap_search}" | sed 's/^.//'` fi ;; esac done echo "${_ci_ldap_result}" } #============================================================================== # main #============================================================================== if [ -f ${configfile} ] then . ${configfile} clrhome mecho --info "test ldap access" mecho if [ "${CAPI2TEXT_LDAP_SEARCH}" = "yes" ] then if [ "${CAPI2TEXT_DO_DEBUG}" = "yes" ] then ldap_debug_str="-d 1" else ldap_debug_str='' fi mecho "Input example: 0221 112233" mecho " 0049 221 112233" mecho # PHONENBR=0221334455 # PHONENBR=0049221334455 /var/install/bin/ask "Enter phone number (e.g. format: [country-code] area-code number)" '' '*' > /tmp/ask.$$ rc=$? # PHONENBR=*221*334455 PHONENBR=`cat /tmp/ask.$$ | sed -e 's/[+) (]/ /g' -e 's/^[0 ]*/\*/' | tr -s ' ' \*` rm -f /tmp/ask.$$ if [ ${rc} = 255 ] then exit 1 fi # CAPI2TEXT_LDAP_HOSTURI='ldap://voyager.privatnet.lan:389' # CAPI2TEXT_LDAP_BASEDN='ou=Addresses,dc=privatnet,dc=lan' # CAPI2TEXT_LDAP_AUTH='no' # CAPI2TEXT_LDAP_BINDDN='cn=ldapadmin,dc=domain,dc=lan' # CAPI2TEXT_LDAP_BINDPASS='my-secret' # homePhone telephoneNumber mobile facsimileTelephoneNumber QFIELD='homePhone' ldap_fields='cn givenName sn mozillaHomePostalCode mozillaHomeLocalityName mozillaHomeStreet postalCode l street' ldap_sizelimit=0 echo echo "CAPI2TEXT_DO_DEBUG='${CAPI2TEXT_DO_DEBUG}'" echo "CAPI2TEXT_LDAP_HOSTURI='${CAPI2TEXT_LDAP_HOSTURI}'" echo "CAPI2TEXT_LDAP_BASEDN='${CAPI2TEXT_LDAP_BASEDN}'" echo "CAPI2TEXT_LDAP_AUTH='${CAPI2TEXT_LDAP_AUTH}'" # echo "CAPI2TEXT_LDAP_BINDDN='${CAPI2TEXT_LDAP_BINDDN}'" # echo "CAPI2TEXT_LDAP_BINDPASS='${CAPI2TEXT_LDAP_BINDPASS}'" echo echo "LDAP-FIELDS:${ldap_fields}" # define ldap field list if [ -z "${CAPI2TEXT_LDAP_FIELD_NAME}" ] then # CAPI2TEXT_LDAP_FIELD_NAME='{cn}' CAPI2TEXT_LDAP_FIELD_NAME='{givenName} {sn}, |{mozillaHomeStreet}{street}|, |{mozillaHomePostalCode}{postalCode}| |{mozillaHomeLocalityName}{l}|' fi if [ "${CAPI2TEXT_LDAP_AUTH}" = "yes" ] then # ldap authentication # remove spaces from string CAPI2TEXT_LDAP_BINDDN=`echo ${CAPI2TEXT_LDAP_BINDDN} | sed 's/ //g'` ldap_auth_str="-D ${CAPI2TEXT_LDAP_BINDDN} -y ${ldap_passwd}" else ldap_auth_str='' fi # LDAP phone number fields: # homePhone / telephoneNumber / mobile / facsimileTelephoneNumber / pager for QFIELD in homePhone telephoneNumber mobile facsimileTelephoneNumber pager do ldap_query="${QFIELD}=${PHONENBR}" echo "LDAP-QUERY :${ldap_query}" # b - search_base # d - debug # H - LDAP-URI # L - search results are display in LDAP Data Interchange Format # p - ldap port # v - verbose # x - use simple authentication instead of SASL # z - size limit, return only x results # run ldap search ${ldap_bin} ${ldap_debug_str} -b ${CAPI2TEXT_LDAP_BASEDN} -H ${CAPI2TEXT_LDAP_HOSTURI} ${ldap_auth_str} -LLL -x -z ${ldap_sizelimit} "(${ldap_query})" ${ldap_fields} 'objectclass=*' > ${tmpfile} 2> /dev/null ldap_res=$? if [ ${ldap_res} -eq 0 -a -s ${tmpfile} ] then # fetch ldap name of caller NAME="`ldap_combine_info \"${CAPI2TEXT_LDAP_FIELD_NAME}\" | sed -e 's/^ *//' -e 's/ *$//'`" if [ -n "${NAME}" ] then # result found print_debug "- Request successful (${QFIELD})" echo echo "Result: ${NAME}" echo break else # error print_debug "- Request unsuccessful (${QFIELD})" fi else # error print_debug "- Request unsuccessful (${QFIELD})" fi done if [ "${CAPI2TEXT_DO_DEBUG}" != 'yes' ] then rm -f ${tmpfile} fi else echo "Error: CAPI2TEXT_LDAP_SEARCH='no' has been set!" fi else echo "Error: No configuration file '${configfile}' found!" fi anykey exit 0