Add commands: Option to remove machine principals, start ldapvi.

This commit is contained in:
Andreas B. Mundt 2019-11-30 09:56:40 +01:00
parent 61e4b1d852
commit 6b3c2f0e0f

View file

@ -10,6 +10,8 @@ usage(){
Usage: Usage:
$(basename $0) adduser <uid> <password> [<cn>] [<sn>] $(basename $0) adduser <uid> <password> [<cn>] [<sn>]
$(basename $0) deluser <uid> $(basename $0) deluser <uid>
$(basename $0) delhost <hostname>
$(basename $0) ldapvi
<uid>: User ID (login name) <uid>: User ID (login name)
<password>: Password <password>: Password
@ -20,9 +22,17 @@ EOF
#sss_cache -U -G ## should not be necessary #sss_cache -U -G ## should not be necessary
BASEDN="{{ basedn }}"
LDAPADMIN="cn=admin,$BASEDN"
ADPASSWD="$(cat {{ ldap_admin_pwd_file }})"
if [ $# -lt 2 ] ; then if [ $# -lt 2 ] ; then
usage if [ "$1" = ldapvi ] ; then
exit 1 exec ldapvi -h ldapi:/// -D "$LDAPADMIN" -b "$BASEDN" -w "$ADPASSWD"
else
usage
exit 1
fi
elif [ $1 = adduser -a $# -lt 3 ] ; then elif [ $1 = adduser -a $# -lt 3 ] ; then
echo "Error: Password missing." echo "Error: Password missing."
usage usage
@ -31,16 +41,14 @@ fi
MINID=10000 MINID=10000
MAXID=20000 MAXID=20000
BASEDN="{{ basedn }}"
HOMES="{{ lan_homes }}" HOMES="{{ lan_homes }}"
LDAPADMIN="cn=admin,$BASEDN"
ADPASSWD="$(cat {{ ldap_admin_pwd_file }})"
COMMAND="$1" COMMAND="$1"
uid="$2" id="$2"
pw="${3:-""}" pw="${3:-""}"
cn="${4:-$2}" cn="${4:-$2}"
sn="${5:-$2}" sn="${5:-$2}"
domain="$(hostname -d)"
if [ -x /usr/sbin/kadmin.local ] ; then if [ -x /usr/sbin/kadmin.local ] ; then
KRB5=true KRB5=true
@ -75,18 +83,18 @@ add-user(){
cat <<EOF | ldapadd -H ldapi:/// -D "$LDAPADMIN" -w "$ADPASSWD" | sed '/^$/d' cat <<EOF | ldapadd -H ldapi:/// -D "$LDAPADMIN" -w "$ADPASSWD" | sed '/^$/d'
############## LDIF ############## ############## LDIF ##############
dn: uid=${uid},ou=people,$BASEDN dn: uid=${id},ou=people,$BASEDN
objectClass: inetOrgPerson objectClass: inetOrgPerson
objectClass: posixAccount objectClass: posixAccount
uidNumber: ${uidNumber} uidNumber: ${uidNumber}
gidNumber: ${gidNumber} gidNumber: ${gidNumber}
homeDirectory: ${HOMES}/${uid} homeDirectory: ${HOMES}/${id}
loginShell: /bin/bash loginShell: /bin/bash
cn: ${cn} cn: ${cn}
sn: ${sn} sn: ${sn}
${pwEntry} ${pwEntry}
dn: cn=${uid},ou=groups,$BASEDN dn: cn=${id},ou=groups,$BASEDN
objectClass: posixGroup objectClass: posixGroup
gidNumber: ${gidNumber} gidNumber: ${gidNumber}
################################## ##################################
@ -95,11 +103,11 @@ EOF
echo "uidNumber: ${uidNumber} gidNumber: ${gidNumber}" echo "uidNumber: ${uidNumber} gidNumber: ${gidNumber}"
if [ $KRB5 ] ; then if [ $KRB5 ] ; then
kadmin.local -q "add_principal -policy default -pw \"$pw\" -x dn=\"uid=${uid},ou=people,$BASEDN\" ${uid}" \ kadmin.local -q "add_principal -policy default -pw \"$pw\" -x dn=\"uid=${id},ou=people,$BASEDN\" ${id}" \
| sed '/Authenticating as principal/d' | sed '/Authenticating as principal/d'
cp -r /etc/skel ${HOMES}/${uid} cp -r /etc/skel ${HOMES}/${id}
chown -R ${uidNumber}:${gidNumber} ${HOMES}/${uid} chown -R ${uidNumber}:${gidNumber} ${HOMES}/${id}
ls -nld ${HOMES}/${uid} ls -nld ${HOMES}/${id}
fi fi
} }
@ -108,21 +116,32 @@ del-user(){
local KEEPDIR local KEEPDIR
if [ $KRB5 ] ; then if [ $KRB5 ] ; then
## Remove all kerberos attributes from LDAP, then the whole DN below. The latter should be sufficient. ## Remove all kerberos attributes from LDAP, then the whole DN below. The latter should be sufficient.
kadmin.local -q "delete_principal -force ${uid}" \ kadmin.local -q "delete_principal -force ${id}" \
| sed -e '/Authenticating as principal/d' -e '/Make sure that you have removed/d' | sed -e '/Authenticating as principal/d' -e '/Make sure that you have removed/d'
fi fi
ldapdelete -v -H ldapi:/// -D "$LDAPADMIN" -w "$ADPASSWD" "uid=${uid},ou=people,$BASEDN" "cn=${uid},ou=groups,$BASEDN" 2>&1 \ ldapdelete -v -H ldapi:/// -D "$LDAPADMIN" -w "$ADPASSWD" "uid=${id},ou=people,$BASEDN" "cn=${id},ou=groups,$BASEDN" 2>&1 \
| sed '/ldap_initialize/d' | sed '/ldap_initialize/d'
if [ -d ${HOMES}/${uid} ] ; then if [ -d ${HOMES}/${id} ] ; then
KEEPDIR="${HOMES}/rm_$(date '+%Y%m%d')_${uid}" KEEPDIR="${HOMES}/rm_$(date '+%Y%m%d')_${id}"
mv ${HOMES}/${uid} "${KEEPDIR}" mv ${HOMES}/${id} "${KEEPDIR}"
chown -R root:root "${KEEPDIR}" chown -R root:root "${KEEPDIR}"
ls -ld "$KEEPDIR" ls -ld "$KEEPDIR"
fi fi
} }
del-host(){
if [ $KRB5 ] ; then
## Remove kerberos principals from LDAP.
kadmin.local -q "delete_principal -force host/${id}.${domain}" \
| sed -e '/Authenticating as principal/d' -e '/Make sure that you have removed/d'
kadmin.local -q "delete_principal -force nfs/${id}.${domain}" \
| sed -e '/Authenticating as principal/d' -e '/Make sure that you have removed/d'
fi
}
############################## ##############################
########### main ############# ########### main #############
############################## ##############################
@ -134,6 +153,9 @@ case $COMMAND in
deluser) deluser)
del-user del-user
;; ;;
delhost)
del-host
;;
*) *)
usage usage
;; ;;