-#!/bin/bash
+#!/usr/bin/env bash
+[ "${BASH_VERSINFO:-0}" -lt 5 ] && echo "upgrade to bash version 5" && exit 1
# ldap user create for crm
# v1.4
# ppa mickraus
# comment: check builduser()
-# need proper formating
+# need proper formating
# changelog: prep for better ldif build
+# updated by gerstenberg 29.07.2024
+
+# Check for Mac or Linux Binaries
+OS="$(uname -s)"
+ARCH="$(uname -m)"
+
+case $OS in
+ 'Darwin') case $ARCH in
+ 'arm64') tr="/opt/homebrew/bin/gtr"
+ sed="/opt/homebrew/bin/gsed"
+ ;;
+ 'x86_64') tr="/usr/local/bin/gtr"
+ sed="/usr/local/bin/gsed"
+ ;;
+ *) echo "OS \"$OS\" with \"$ARCH\" is not supported right now.."
+ exit 1
+ ;;
+ esac
+ ;;
+ 'Linux') tr="/usr/bin/tr"
+ sed="/usr/bin/sed"
+ ;;
+ *) echo "OS \"$OS\" is not supported right now.."
+ exit 1
+ ;;
+esac
+
+function read_users() {
+ local file="$1"
+ if [ -f "$file" ] ; then
+ declare -gA names
+ mapfile -t lines < "${file}"
+ for line in ${!lines[@]} ; do
+ [[ ! "${lines[line]:0:1}" == "#" ]] || continue
+ mapfile -d ';' -t parts <<< "${lines[line]}"
+ shopt -s extglob # turn on extended glob
+ parts=( "${parts[@]/#+([[:blank:]])/}" ) # remove leading space/tab from each element
+ parts=( "${parts[@]/%+([[:blank:]])/}" ) # remove trailing space/tab from each element
+ local uid="${parts[0],,}"
+ local name="${uid%%@*}"
+ local first="${parts[1]:-${name%%.*}}"
+ local sure="${parts[2]:-${name##*.}}"
+ local jira="${parts[3]:-n}"
+ local confluence="${parts[4]:-n}"
+ names[${uid},email]="$uid"
+ names[${uid},first]="${first^}"
+ names[${uid},sure]="${sure^}"
+ names[${uid},jira]="${jira}"
+ names[${uid},confluence]="${confluence}"
+ done
+ else
+ echo ""
+ echo "File $file not found.."
+ echo ""
+ exit 1
+ fi
+}
+
+function main_fromfile() {
+ local file="$1"
+ read_users "$file"
+ for i in ${names[*]} ; do
+ [[ "${names[$i,email]}" != "" ]] || continue
+ GIVENNAMES="${names[$i,first]}"
+ SURNAME="${names[$i,sure]}"
+ LOWERCMAIL="${names[$i,email]}"
+ PPAPPLICATIONRIGHTS=''
+ MEMBEROF=''
+ if [[ "${names[$i,jira]}" == "y" ]]; then
+# MEMBEROF=$(printf "\nmemberOf: cn=jira-crm-people,ou=CRM-Groups,ou=Confluence,ou=Applications,o=Pixelpark,o=isp")
+ PPAPPLICATIONRIGHTS=$(printf "\nppapplicationright: jira:access")
+ fi
+ if [[ "${names[$i,confluence]}" == "y" ]]; then
+# MEMBEROF+=$(printf "\nmemberOf: cn=confluence-crm-people,ou=CRM-Groups,ou=Confluence,ou=Applications,o=Pixelpark,o=isp")
+ PPAPPLICATIONRIGHTS+=$(printf "\nppapplicationright: extranet:access")
+ fi
+ genuserpw
+ outldapldif
+ done
+}
#setting global vars
-RUN=0 #counting the loop
+RUN=0 #counting the loop
#function start main standalone
function main_standalone() {
#function for formating/fixing/building cli input
function fixinputcli() {
+GIVENNAMES=$(echo "$GIVENNAMES" | $sed -e 's/\(^\|[ -]\)\([a-z]\)/\1\u\2/g')
+SURNAME=$(echo "$SURNAME" | $sed -e 's/\(^\|[ -]\)\([a-z]\)/\1\u\2/g')
LOWERCMAIL=${LOWERCMAIL,,}
+PPAPPLICATIONRIGHTS=''
+MEMBEROF=''
if [[ $PPAPPJIRA == "y" ]]; then
-JIRAACCESS='ppapplicationright: jira:access'
-else
-JIRAACCESS=''
+# MEMBEROF=$(printf "\nmemberOf: cn=jira-crm-people,ou=CRM-Groups,ou=Confluence,ou=Applications,o=Pixelpark,o=isp")
+PPAPPLICATIONRIGHTS=$(printf "\nppapplicationright: jira:access")
fi
if [[ $PPAPPCONFLUENCE == "y" ]]; then
-CONFLUENCEACCESS='ppapplicationright: extranet:access'
-else
-CONFLUENCEACCESS=''
+# MEMBEROF+=$(printf "\nmemberOf: cn=confluence-crm-people,ou=CRM-Groups,ou=Confluence,ou=Applications,o=Pixelpark,o=isp")
+PPAPPLICATIONRIGHTS+=$(printf "\nppapplicationright: extranet:access")
fi
-
##for furture updates, need to check name for non ascii chars and empty space on VAR end
}
#funtion for creating a rnd user pw
genuserpw() {
-PASSWORD=""
-while true; do
-PASSWORD=$(openssl rand 222 | tr -dc 'a-zA-Z0-9!=?#' | head -c 12)
-if [[ $PASSWORD =~ [0-9] ]] && [[ $PASSWORD =~ [!=?#] ]]; then
-break
-fi
-done
-CLEARPW=$PASSWORD
-CRYPTPW=`printf ${CLEARPW} | base64`
+ PASSWORD=""
+ length=12
+ # charset='A-Za-z0-9!"#$%&'\''()*+,-./:;<=>?@[\]^_`{|}~'
+ charset='a-zA-Z0-9!=?#'
+ while true; do
+ PASSWORD=$($tr -dc $charset </dev/urandom | head -c $length;echo '')
+ if [[ $PASSWORD =~ [0-9] ]] && [[ $PASSWORD =~ [!=?#] ]]; then
+ break
+ fi
+ done
+ CLEARPW=$PASSWORD
+ CRYPTPW=`printf ${CLEARPW} | base64`
}
#function for creating a users ldif
outldapldif() {
let RUN++
-
-if [[ $PPAPPJIRA == "n" && $PPAPPCONFLUENCE == "n" ]]; then
-#shell output
-printf "\033c"
-echo -ne "---\n"
-echo -ne "Username: $LOWERCMAIL\n"
-echo -ne "Password: $CLEARPW\n"
-echo -ne "---\n"
-echo -ne "\n\n"
-echo "dn: uid=$LOWERCMAIL,ou=People,ou=CRM,ou=Applications,o=Pixelpark,o=isp
-cn: $GIVENNAMES $SURNAME
-givenname: $GIVENNAMES
-mail: $LOWERCMAIL
-objectclass: inetOrgPerson
-objectclass: inetSubscriber
-objectclass: inetUser
-objectclass: ipUser
-objectclass: nsManagedPerson
-objectclass: organizationalPerson
-objectclass: person
-objectclass: ppPixelAccount
-objectclass: top
-sn: $SURNAME
-uid: $LOWERCMAIL
-userpassword:: $CRYPTPW " | tee -a "create_${GIVENNAMES,,}-${SURNAME,,}_nr${RUN}.ldif"
-return 1
-fi
-
-if [[ $PPAPPJIRA == "y" && $PPAPPCONFLUENCE == "y" ]]; then
-#shell output
-printf "\033c"
-echo -ne "---\n"
-echo -ne "Username: $LOWERCMAIL\n"
-echo -ne "Password: $CLEARPW\n"
-echo -ne "---\n"
-echo -ne "\n\n"
-echo "dn: uid=$LOWERCMAIL,ou=People,ou=CRM,ou=Applications,o=Pixelpark,o=isp
-cn: $GIVENNAMES $SURNAME
-givenname: $GIVENNAMES
-mail: $LOWERCMAIL
-objectclass: inetOrgPerson
-objectclass: inetSubscriber
-objectclass: inetUser
-objectclass: ipUser
-objectclass: nsManagedPerson
-objectclass: organizationalPerson
-objectclass: person
-objectclass: ppPixelAccount
-objectclass: top
-ppapplicationright: jira:access
-ppapplicationright: extranet:access
-sn: $SURNAME
-uid: $LOWERCMAIL
-userpassword:: $CRYPTPW " | tee -a "create_${GIVENNAMES,,}-${SURNAME,,}_nr${RUN}.ldif"
-return 1
-fi
-
-if [[ $PPAPPJIRA == "y" && $PPAPPCONFLUENCE != "y" ]]; then
#shell output
printf "\033c"
echo -ne "---\n"
objectclass: organizationalPerson
objectclass: person
objectclass: ppPixelAccount
-objectclass: top
-ppapplicationright: jira:access
+objectclass: top${MEMBEROF}${PPAPPLICATIONRIGHTS}
sn: $SURNAME
uid: $LOWERCMAIL
-userpassword:: $CRYPTPW " | tee -a "create_${GIVENNAMES,,}-${SURNAME,,}_nr${RUN}.ldif"
+userpassword:: $CRYPTPW
+" | tee -a "create_${GIVENNAMES// /-}-${SURNAME// /-}_nr${RUN}.ldif"
return 1
-fi
-
-if [[ $PPAPPCONFLUENCE == "y" && $PPAPPJIRA != "y" ]]; then
-#shell output
-printf "\033c"
-echo -ne "---\n"
-echo -ne "Username: $LOWERCMAIL\n"
-echo -ne "Password: $CLEARPW\n"
-echo -ne "---\n"
-echo -ne "\n\n"
-echo "dn: uid=$LOWERCMAIL,ou=People,ou=CRM,ou=Applications,o=Pixelpark,o=isp
-cn: $GIVENNAMES $SURNAME
-givenname: $GIVENNAMES
-mail: $LOWERCMAIL
-objectclass: inetOrgPerson
-objectclass: inetSubscriber
-objectclass: inetUser
-objectclass: ipUser
-objectclass: nsManagedPerson
-objectclass: organizationalPerson
-objectclass: person
-objectclass: ppPixelAccount
-objectclass: top
-ppapplicationright: extranet:access
-sn: $SURNAME
-uid: $LOWERCMAIL
-userpassword:: $CRYPTPW " | tee -a "create_${GIVENNAMES,,}-${SURNAME,,}_nr${RUN}.ldif"
-return 1
-fi
}
-#starting main_standalone() or main_deamon()
-while true; do
-main_standalone || break
-done
+if [ "$1" ] ; then
+ main_fromfile "$1"
+else
+ #starting main_standalone() or main_deamon()
+ while true; do
+ main_standalone || break
+ done
+fi