# # omloadfile $Revision: 1.5 $ # # $Log: omloadfile,v $ # Revision 1.5 2005/06/30 21:05:12 dsturdivant # Fixed bug with MI not being parsed correctly # # Revision 1.4 2005/04/07 18:08:04 dsturdivant # no message # # Revision 1.3 2005/02/03 21:18:10 dsturdivant # no message # # Revision 1.2 2004/09/20 15:27:17 dkelly # Added fix to work around cross-device problems with dos2unix # # Revision 1.1 2004/09/06 12:29:48 dkelly # Added omloadfile (a script which converts a CSV file into a series of # om- commands to provision users from a non-Exchange server. # # DEBUG=0 IMPORT_FILE="@@@" MIME_MAILNODE=$(/opt/scalix/bin/omshowux -m 2>/dev/null) COMMON_NAME_FORMAT="S, G" COMMON_NAME_IN_ADDRESS=0 COMMAND_NAME=$(basename $0) OUTPUT_FILE_BASE=/tmp/${COMMAND_NAME}.out fn_usage_error() { if [ $# -ge 1 ] then echo "The following error occurred: " $2 >&2 fi cat >&2 <<@EOF usage: ${COMMAND_NAME} -f filename -m mailnode [-c] [-F format] [-v] @EOF exit 100 } fn_parse_import_file() { echo "${COMMAND_NAME}: Parsing import file : $1" >&2 awk -F"," -vCOMMON_NAME_FORMAT="${COMMON_NAME_FORMAT}" \ -vCOMMON_NAME_IN_ADDRESS="${COMMON_NAME_IN_ADDRESS}" \ -vMIME_MAILNODE="${MIME_MAILNODE}" \ -vOUTPUT_FILE_BASE="${OUTPUT_FILE_BASE}" ' # In a common name format we should ignore the first "." and any subsequent # spaces function advance_to_next_character(pos, string) { // Ignore the first "." if(substr(string, pos, 1) == ".") { pos++; } while(substr(string, pos+1, 1) == " ") { pos++; } return pos-1; } BEGIN { print "Common Name Format: " COMMON_NAME_FORMAT > OUTPUT_FILE_BASE".log"; print "Common Name in Address: " COMMON_NAME_IN_ADDRESS >> OUTPUT_FILE_BASE".log"; print "Mime Mailnode: " MIME_MAILNODE >> OUTPUT_FILE_BASE".log"; OMADDENT_FILE=OUTPUT_FILE_BASE".omaddent"; OMMIGU_FILE=OUTPUT_FILE_BASE".ommigu"; OMMODU_FILE=OUTPUT_FILE_BASE".ommodu"; ERROR_FILE=OUTPUT_FILE_BASE".error"; // Parse the mime mailnode into X.400 OU values num_units = split(MIME_MAILNODE,ou,","); X400_MAILNODE=""; for(i=1;i<=num_units;i++) { X400_MAILNODE=X400_MAILNODE"/OU"i"="ou[i]; } } { // Build the name attributes S=$1; G=$2; I=$3; NAME_ATTRIBUTES=""; if(length(S) > 0) { NAME_ATTRIBUTES=NAME_ATTRIBUTES"/S="S; } if(length(G) > 0) { NAME_ATTRIBUTES=NAME_ATTRIBUTES"/G="G; } if(length(I) > 0) { NAME_ATTRIBUTES=NAME_ATTRIBUTES"/I="I; } AUTHID=$4; // Build the common name COMMON_NAME=""; for(i=1;i<=length(COMMON_NAME_FORMAT);i++) { FORMAT_CHAR=substr(COMMON_NAME_FORMAT, i, 1); NEXT_CHAR=substr(COMMON_NAME_FORMAT, i+1, 1); if(FORMAT_CHAR == "S") { if(length(S) > 0) { COMMON_NAME=COMMON_NAME""S; } else { i = advance_to_next_character(i+1, COMMON_NAME_FORMAT); } } else if(FORMAT_CHAR == "G") { if(length(G) > 0) { COMMON_NAME=COMMON_NAME""G; } else { i = advance_to_next_character(i+1, COMMON_NAME_FORMAT); } } else if(FORMAT_CHAR == "I") { if(length(I) > 0) { COMMON_NAME=COMMON_NAME""I"."; } i = advance_to_next_character(i+1, COMMON_NAME_FORMAT); } else { COMMON_NAME=COMMON_NAME""FORMAT_CHAR; } } COMMON_NAME=COMMON_NAME; // Build the internet address IA=""; if(NF > 4) { for(i=5;i<=NF;i++) { j=i+1; if ((substr($i,1,1) == "\"") && (substr($i,length($i),1) != "\"") && (substr($i,length($i),1) != ">")) { $i=$i", "$j; for(;j 0) { if(COMMON_NAME_IN_ADDRESS==1) { IA=IA"=\""COMMON_NAME"\" <"$i">"; } else { IA=IA"="$i; } } } } // Build the omaddent entry OMADDENT_ENTRY="CN="COMMON_NAME"/IA"IA""X400_MAILNODE"/LDAP-OBJECT-CLASS=user"NAME_ATTRIBUTES; print OMADDENT_ENTRY >> OMADDENT_FILE; if(length(AUTHID) > 0) { print "ommodu -o \"CN="COMMON_NAME"\" --authid \""AUTHID"\"" >> OMMODU_FILE; } print COMMON_NAME >> OMMIGU_FILE; } ' $1 } USER_LONG_OPTS="usecommonname,file:,debug,format:,output:,mailnode:" GETOPT_RESULT=$(getopt -a -o "cf:F:o:m:v" -l "${USER_LONG_OPTS}" -n "" -- "$@") if [ $? -ne 0 ] then fn_usage_error fi eval set -- "$GETOPT_RESULT" while true do case "$1" in -c|--usecommonname) COMMON_NAME_IN_ADDRESS=1; shift;; -m|--mailnode) MIME_MAILNODE=$2; shift 2;; -f|--file ) IMPORT_FILE=$2 ; shift 2;; -F|--format ) COMMON_NAME_FORMAT=$2 ; shift 2;; -o|--output ) OUTPUT_FILE_BASE=$2 ; shift 2;; -v ) DEBUG=1 ; shift ;; -- ) shift; break;; * ) fn_usage_error $1 ;; esac done # Check that the import file has been set if [ "${IMPORT_FILE}" == "@@@" ] then echo "No import file has been specified" >&2 fn_usage_error fi # Check for the existence of the import file if [ ! -f "${IMPORT_FILE}" ] then echo "${IMPORT_FILE} does not exist" >&2 fn_usage_error fi # Check that the mailnode has been determined if [ "${MIME_MAILNODE}" == "" ] then echo "A mailnode has not been specified and the default MIME mailnode cannot be determined" >&2 fn_usage_error fi # Remove any files beginning with the output file base rm -f ${OUTPUT_FILE_BASE}* if [ ${DEBUG} -eq 1 ] then set -x fi # Check to see if the import file is on the same # filesystem as the /tmp directory TMP_FS=$(df /tmp | tail -n 1 | cut -d" " -f 1) IMPORT_FS=$(df ${IMPORT_FILE} | tail -n 1 | cut -d " " -f 1) OLDDIR=$(pwd) if [ "${TMP_FS}" != "${IMPORT_FS}" ] then echo "Warning: import file is not on the same file system as /tmp" echo "Copying import file to /tmp directory" cp -p ${IMPORT_FILE} /tmp/omloadfile.copy export IMPORT_FILE=/tmp/omloadfile.copy cd /tmp fi # Ensure that the file is in Unix format dos2unix -n ${IMPORT_FILE} /tmp/omloadfile.in cd ${OLDDIR} # Create the 2 files fn_parse_import_file /tmp/omloadfile.in rm -f /tmp/omloadfile.in rm -f /tmp/omloadfile.copy exit 0