#!/bin/sh #------------------------------------------------------------------------------ # /var/install/bin/ldapserver-convert-thunderbird2ldap - convert Thunderbird # ldif file to OpenLDAP ldif file format so that it can be imported into # the LDAP database # # Copyright (c) 2012-2023 The Eisfair Team, team(at)eisfair(dot)org # # Creation: 2012-02-19 jed # Last Update: $Id$ # # Usage: ldapserver-convert-thunderbird2ldap.sh [--debug] # [--in tb-export-ldif-file --out ldapserver-import-ldif-file # --abook address-book-ldappath] # # 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. #------------------------------------------------------------------------------ #include eislib etc. . /var/install/include/eislib . /var/install/include/jedlib #exec 2>/tmp/ldapserver-thunderbird2ldap-trace-$$.log #set -x #------------------------------------------------------------------------------ # debug print # $1 - debug output #------------------------------------------------------------------------------ debug_print() { if [ ${debug} -eq 1 ] then case $1 in *-info|*-warn|*-error ) _dp_label="$1" shift mecho ${_dp_label} "$*" ;; * ) echo "$*" ;; esac fi } #------------------------------------------------------------------------------ # decode base64 encoded string # input : $1 - text string # output: text string #------------------------------------------------------------------------------ decode_base64() { _db64_in="$1" # decode string... # 1. decode BASE64 # 2. decode UTF-8 to ISO-8859-1 _db64_out="`echo "${_db64_in}" | perl -MMIME::Base64 -ne 'print decode_base64($_)' | iconv -f UTF-8 -t ISO-8859-1`" echo "${_db64_out}" } #------------------------------------------------------------------------------ # decode base64 encoded string # input : $1 - text string # output: text string #------------------------------------------------------------------------------ encode_base64() { _eb64_in="$1" # encode string # encode ISO-8859-1 to UTF-8 _eb64_out="`echo "${_eb64_in}" | iconv -f ISO-8859-1 -t UTF-8 | perl -MMIME::Base64 -ne 'print encode_base64($_)'`" # _eb64_out="`echo "${_eb64_in}" | perl -MMIME::Base64 -ne 'print encode_base64($_)'`" echo "${_eb64_out}" } #------------------------------------------------------------------------------ # lookup country code - see http://de.wikipedia.org/wiki/ISO-3166-1-Kodierliste # # input : $1 - country name # output: country code #------------------------------------------------------------------------------ lookup_ccode() { case $1 in 'Deutschland') _lccode='DE';; 'England') _lccode='GB';; 'Frankreich') _lccode='FR';; 'Liechtenstein') _lccode='LI';; 'Niederlande') _lccode='NL';; 'Österreich') _lccode='AT';; 'Schweiz') _lccode='CH';; *) _lccode="$1" ;; esac echo "${_lccode}" } #============================================================================== # main #============================================================================== # set defaults ldap_datapath=/var/lib/openldap mode='batch' ldapclient_configfile=/etc/config.d/ldapclient ldapserver_configfile=/etc/config.d/ldapserver ldapserver_logfile=${ldap_datapath}/ldapserver-thunderbird2ldap-import.log ldapserver_pwfile=${ldap_datapath}/ldapserver.pw infile='' outfile='' abook_ldappath='' # check availability of external programs if [ ! -f /usr/bin/iconv ] then mecho --warn "The required program '/usr/bin/iconv' couldn't be found!" exit 1 fi if [ ! -f /usr/bin/perl ] then mecho --warn "The required program '/usr/bin/perl' couldn't be found!" exit 1 fi # read configuration file if [ -f ${ldapserver_configfile} ] then . ${ldapserver_configfile} else mecho --error "Configuration file '${ldapserver_configfile}' doesn't exist!" exit 1 fi if [ -f ${ldapclient_configfile} ] then . ${ldapclient_configfile} else mecho --error "Configuration file '${ldapclient_configfile}' doesn't exist!" exit 1 fi # command line parameter debug=0 import=0 quiet=0 if [ $# -gt 0 ] then # read parameter(s) while [ $# -gt 0 ] do case $1 in '--abook'|'-a') abook_ldappath="$2" shift; shift ;; '--debug') debug=1 shift ;; '--import') import=1 ;; '--in'|'-i') infile="$2" shift; shift ;; '--out'|'-o') outfile="$2" shift; shift ;; '--quiet') quiet=1 shift ;; *) break ;; esac done fi #infile=./tb_export.ldif #outfile=./ldap_import.ldif #abook_ldappath='ou=public,ou=Addresses' if [ -z "${infile}" -o -z "${outfile}" ] then mode='interactive' clrhome mecho --info "Convert Thunderbird ldif file to OpenLDAP ldif file" mecho infile=$(/var/install/bin/ask "Import from Thunderbird LDIF file (full path) [q]" "./tb_export.ldif" "+") if [ "${infile}" = "q" -o "${infile}" = "Q" ] then exit 0 fi outfile=$(/var/install/bin/ask "Export to OpenLDAP LDIF file (full path) [q]" "./ldap_import.ldif" "+") if [ "${outfile}" = "q" -o "${outfile}" = "Q" ] then exit 0 fi mecho abook_ldappath=$(/var/install/bin/ask "LDAP address book path (e.g. ou=public,ou=Addresses) [q]" "ou=public,ou=Addresses" "+") if [ "${abook_ldappath}" = "q" -o "${abook_ldappath}" = "Q" ] then exit 0 fi mecho fi if [ ! -f ${infile} ] then mecho --error "Thunderbird LDIF file '${infile}' doesn't exist, abort conversion." exit 1 fi if [ -f ${outfile} ] then mecho --warn "OpenLDAP LDIF file '${outfile}' already exists." mecho if /var/install/bin/ask "Do you want to overwrite it" "no" then rm -f ${outfile} else mecho --error "LDIF file conversion aborted." exit 1 fi fi if [ -n "${abook_ldappath}" ] then # remove spaces and LDAP BaseDN abook_ldappath=`echo "${abook_ldappath}" | sed -e 's/ *//g' -e "s/${LDAPSERVER_LDAP_BASEDN}//g"` else mecho --error "LDAP address book path not given, abort conversion." exit 1 fi # make sure it's in a unix file format dtou ${infile} chmod -x ${infile} if [ "${mode}" = "interactive" ] then mecho mecho "infile : ${outfile}" mecho "outfile : ${outfile}" mecho "LDAP path : ${abook_ldappath}" mecho "LDAP BaseDN: ${LDAPSERVER_LDAP_BASEDN}" mecho mecho "starting conversion of file ..." fi debug_print "-start---------------------------------------------------------------" debug_print "infile : ${outfile}" debug_print "outfile : ${outfile}" debug_print "LDAP path : ${abook_ldappath}" debug_print "LDAP BaseDN: ${LDAPSERVER_LDAP_BASEDN}" debug_print "-base64-decoding/encoding--------------------------------------------" # read number of lines imax=`wc -l ${infile} | cut -d' ' -f1` idx=${imax} while read line do ilen=`expr length "${idx}"` # get length of number idxstr=`expr substr " " 1 ${ilen}` # create empty string debug_print "${idx} IN >${line}<" # check if string is base64 encoded echo "${line}" | grep -q "::" if [ $? -eq 0 ] then # base64 coded string found in_prefix=`echo "${line}" | sed 's/:: .*$/: /g'` in_value=`echo "${line}" | sed 's/^.*:: //g'` tmp_string="`decode_base64 \"${in_value}\"`" tmp_string="`echo ${in_prefix}${tmp_string}`" debug_print "${idxstr} TMP>${tmp_string}<" else tmp_string="${line}" fi # check if 2-char ISO-3166 country code is used echo "${tmp_string}" | grep -q "^c:" if [ $? -eq 0 ] then cname=`echo "${tmp_string}" | cut -d: -f2-` cname=`trim_spaces "${cname}"` slen=`expr length "${cname}"` if [ ${slen} -gt 2 ] then ccode=`lookup_ccode "${cname}"` tmp_string="c: ${ccode}" fi fi out_string="`echo ${tmp_string} | sed -e "/^dn:/s/,mail=.*/,${abook_ldappath},${LDAPSERVER_LDAP_BASEDN}/g" \ -e "/^dn:[:]* .*,/! s/^\(dn: .*\)$/\1,${abook_ldappath},${LDAPSERVER_LDAP_BASEDN}/" \ -e 's/^company:/o:/g' -e 's/^objectclass:/objectClass:/g' -e 's/^facsimiletelephonenumber:/facsimileTelephoneNumber:/g' \ -e 's/^homeStreet:/mozillaHomeStreet:/g' -e '/^mozillaUseHtmlMail:/s/: *\(.*\)$/: \U\1/' \ -e 's/^birthyear:/birthYear:/' -e '/^birthmonth:/s/month: *0*\(.*\)$/Month: \1/' \ -e 's/^modifytimestamp/#modifytimestamp/g' \ -e '/^birthday:/s/day: *0*\(.*\)$/Day: \1/'`" # check if string contains german umlauts echo "${out_string}" | grep -E -q "Ä|Ö|Ü|ä|ö|ü|ß" if [ $? -eq 0 ] then # german umlauts found, force base64 encoding debug_print "${idxstr} TMP>${out_string}<" in_prefix=`echo "${out_string}" | sed 's/: .*$/:: /g'` in_value=`echo "${out_string}" | sed 's/^.*: //g'` tmp_string="`encode_base64 \"${in_value}\"`" out_string="${in_prefix}`echo ${tmp_string} | tr -d '\n '`" fi if [ "${line}" != "${out_string}" ] then debug_print --warn "${idxstr} OUT>${out_string}<" fi if [ -z "${out_string}" ] then debug_print "${idxstr} -------------------------------------------------------------------" fi echo "${out_string}" >> ${outfile} idx=`expr ${idx} - 1` done < ${infile} if [ "${mode}" = "interactive" ] then mecho if /var/install/bin/ask "Do you want to import it to the LDAP database" "no" then import=1 fi fi if [ ${import} -eq 1 ] then if [ "${START_LDAPCLIENT}" = "yes" -a "${LDAPCLIENT_LDAP_AUTH}" = "yes" ] then # try to use ldapclient parameters debug_print "-ldapclient-import---------------------------------------------------" ldap_auth='ldapclient' ldap_hosturi="${LDAPCLIENT_LDAP_HOSTURI}" ldap_binddn="cn=${LDAPCLIENT_LDAP_ADMIN_NAME},${LDAPCLIENT_LDAP_BASEDN}" # write password to file without LF (0a) echo -n "${LDAPCLIENT_LDAP_ADMIN_PASS}" > ${ldapserver_pwfile} else debug_print "-ldapserver-import---------------------------------------------------" ldap_auth='ldapserver' ldap_hosturi="ldap://`hostname --fqdn`:389" ldap_binddn="cn=${LDAPSERVER_LDAP_ADMIN_NAME},${LDAPSERVER_LDAP_BASEDN}" # write password to file without LF (0a) echo -n "${LDAPSERVER_LDAP_ADMIN_PASS}" > ${ldapserver_pwfile} fi chmod 0600 ${ldapserver_pwfile} if [ "${mode}" = "interactive" ] then mecho mecho "Authentication: ${ldap_auth}" mecho "LDAP HostURI : ${ldap_hosturi}" mecho "LDAP BindDN : ${ldap_binddn}" mecho sleep 3 fi debug_print "LDAP HostURI : ${ldap_hosturi}" debug_print "LDAP BindDN : ${ldap_binddn}" ldap_debug='' if [ ${debug} -eq 1 ] then ldap_debug='-d 3' fi rm -f ${ldapserver_logfile} /usr/bin/ldapadd ${ldap_debug} -c -y ${ldapserver_pwfile} -H ${ldap_hosturi} -D "${ldap_binddn}" -f ${outfile} >> ${ldapserver_logfile} 2>> ${ldapserver_logfile} retval=$? rm -f ${ldapserver_pwfile} debug_print "LDAPadd Result: ${retval}" if [ "${mode}" = "interactive" ] then # check if show-doc.cui supports colors color='' frame='' if $(grep -qE "^MENU=['\"]/var/install/bin/show-menu['\"]" /etc/config.d/setup) then color='--nocolor' frame='--noframe' fi sleep 1 /var/install/bin/show-doc.cui ${color} ${frame} --follow --title ${ldapserver_logfile} ${ldapserver_logfile} fi fi if [ "${mode}" = "interactive" ] then mecho "done." fi debug_print "-end-----------------------------------------------------------------" exit 0