]> Frank Brehm's Git Trees - pixelpark/pp-admin-tools.git/commitdiff
Completing bin/get-ldap-membership
authorFrank Brehm <frank.brehm@pixelpark.com>
Fri, 15 Oct 2021 09:38:06 +0000 (11:38 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Fri, 15 Oct 2021 09:38:06 +0000 (11:38 +0200)
bin/get-ldap-membership
lib/functions.rc

index de6de3b24dc078a11b9b149b3c8d02859bd65f28..8e687227da1df56ba8ec208b37d3c88c6130ff49 100755 (executable)
@@ -110,11 +110,93 @@ usage() {
 
 }
 
+#------------------------------------------------------------------------------
+get_dn() {
+
+    local user_id="$1"
+    local dn=
+
+    local ldap_filter_oclass="(|"
+    local oclass=
+    local line=
+
+    for oclass in inetLocalMailRecipient inetMailingListUser inetOrgPerson \
+            inetResource mailGroupMember mailRecipient; do
+        ldap_filter_oclass+="(objectClass=${oclass})"
+    done
+    ldap_filter_oclass+=")"
+
+    local filter="(&${ldap_filter_oclass}(|(mail=${user_id})(mailAlternateAddress=${user_id})"
+    filter+="(mailEquivalentAddress=${user_id})(uid=${user_id})))"
+
+    info "Getting DN of LDAP-Object with E-Mail address or UID '${CYAN}${user_id}${NORMAL}' ..."
+
+    cmd="ldapsearch -LLL -o ldif-wrap=no -h \"${LDAP_HOST}\" -p ${LDAP_PORT} -b \"${LDAP_BASE}\""
+    cmd+=" -x -D \"${LDAP_USR}\" -y \"${LDAP_PWD_FILE}\" \"${filter}\" dn"
+    debug "Executing: ${cmd}"
+    for line in $( eval ${cmd} | grep -i '^dn:' | sed -e 's/^dn:[      ]*//i' ) ; do
+        echo "${line}"
+    done
+
+}
+
+#------------------------------------------------------------------------------
+search_memberships() {
+
+    local dn="$1"
+
+    info "Searching for groups with member '${CYAN}${dn}${NORMAL}' ..."
+    local ldap_filter_oclass="(|"
+    local oclass=
+
+    for oclass in groupOfNames groupOfUniqueNames; do
+        ldap_filter_oclass+="(objectClass=${oclass})"
+    done
+    ldap_filter_oclass+=")"
+
+    local filter="(&${ldap_filter_oclass}(|(uniqueMember=${dn})(member=${dn})))"
+    cmd="ldapsearch -LLL -o ldif-wrap=no -h \"${LDAP_HOST}\" -p ${LDAP_PORT} -b \"${LDAP_BASE}\""
+    cmd+=" -x -D \"${LDAP_USR}\" -y \"${LDAP_PWD_FILE}\" \"${filter}\""
+    cmd+=" dn cn description mail mailAlternateAddress mailEquivalentAddress"
+    debug "Executing: ${cmd}"
+    eval ${cmd}
+}
+
+#------------------------------------------------------------------------------
+search_user() {
+
+    local user_id="$1"
+    local dn=
+
+    local oifs="${IFS}"
+    IFS="
+"
+
+    echo
+    if echo "${user_id}" | grep -q -i "${LDAP_BASE}\$" ; then
+        dn="${user_id}"
+        info "Searching for user with DN '${CYAN}${dn}${NORMAL}' ..."
+        search_memberships "${dn}"
+    else
+        for dn in $( get_dn "${user_id}" ); do
+            info "Searching for user '${CYAN}${user_id}${NORMAL}' with DN '${CYAN}${dn}${NORMAL}' ..."
+            search_memberships "${dn}"
+        done
+    fi
+
+    IFS="${oifs}"
+
+}
+
 #------------------------------------------------------------------------------
 main() {
 
     get_options "$@"
 
+    local id
+    for id in "${SEARCH_IDS[@]}" ; do
+        search_user "${id}"
+    done
 
 }
 
index ae808a04d913bcfbf82458ccbc0d41e841a5beb4..99a4ee76af0aeeb44bc6a16fd6aa6b86a16b905e 100644 (file)
@@ -10,7 +10,7 @@ BLUE=""
 CYAN=""
 NORMAL=""
 
-VERSION="0.2.0"
+VERSION="0.2.1"
 
 STD_SHORT_OPTIONS="sdvhV"
 STD_LONG_OPTIONS="simulate,debug,verbose,nocolor,help,version"