#!/bin/bash # # A simple script to add users and their group to ldap, as well as a kerberos principal. # set -eu usage(){ cat < [] [] [] $(basename $0) deluser $(basename $0) delhost $(basename $0) ldapvi $(basename $0) : User ID (login name) : Password , : LDAP attributes 'givenName' and 'sn'. If omitted, is used. : If given, the user is added to this posix group, which must already exist. : File containing lines of the form: adduser [] [] adduser [] [] … deluser deluser … Every line is processed like a single call to the $(basename $0) program. EOF } BASEDN="{{ basedn }}" LDAPADMIN="cn=admin,$BASEDN" ADPASSWD="$(cat {{ ldap_admin_pwd_file }})" if [ $# -lt 2 ] ; then if [ $# = 0 ] ; then usage exit 1 elif [ "$1" = ldapvi ] ; then exec ldapvi -m -h ldapi:/// -D "$LDAPADMIN" -b "$BASEDN" -w "$ADPASSWD" elif [ -r "$1" ]; then ## recursively call this program: while read -r LINE ; do $0 $LINE done < "$1" ## reset cache after mass import/deletion: sss_cache -U -G exit 0 else usage exit 1 fi elif [ $1 = adduser -a $# -lt 3 ] ; then echo "Error: Password missing." usage exit 1 fi MINID={{ min_id }} MAXID={{ max_id }} HOMES="{{ lan_homes }}" COMMAND="$1" id="$2" pw="${3:-""}" gn="${4:-$2}" sn="${5:-$2}" grp="${6:-""}" domain="$(hostname -d)" if [ -x /usr/sbin/kadmin.local ] ; then KRB5=true pwEntry="" else KRB5=false pwEntry="userPassword: $pw" fi ############# nextnum(){ local num num="$(( $(ldapsearch -Y EXTERNAL -H ldapi:/// -LLL -b "ou=people,$BASEDN" -S $1 $1 2>/dev/null \ | tail -n -2 | grep -oE "[[:digit:]]+$") + 1 ))" if [ $num -lt $MINID ] ; then echo $MINID else echo "$num" fi } add-user(){ local id="$1" local pwEntry="$2" local gn="$3" local sn="$4" local uidNumber=$(nextnum uidNumber) local gidNumber=$(nextnum gidNumber) if [ $uidNumber -ge $MAXID -o $gidNumber -ge $MAXID ] ; then echo "Error: $uidNumber and/or $gidNumber exceed max ID number ${MAXID}." exit 1 fi cat <&1 \ | sed '/ldap_initialize/d' for grp in $(ldapsearch -Y EXTERNAL -H ldapi:/// -LLL -b "ou=groups,$BASEDN" "(&(objectClass=posixGroup)(memberUid=${id}))" cn 2>/dev/null \ | grep cn: | cut -d ' ' -f2) ; do cat <