Skip to content

Instantly share code, notes, and snippets.

@basinilya
Last active January 11, 2024 07:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save basinilya/fa24f4a36e8d32fbdae1b6c67ce5181b to your computer and use it in GitHub Desktop.
Save basinilya/fa24f4a36e8d32fbdae1b6c67ce5181b to your computer and use it in GitHub Desktop.
#!/bin/bash
# print base64 UUIDs on command line as LDAP search filter of the form:
# (|(ObjectGuid=\xx\xx\xx\xx\xx\xx\xx\xx\xx\xx\xx\xx\xx\xx\xx\xx)...)
fn_base64_uuid_to_hexarray() {
# hexdump base64 valie in $2 to a new indexed array of hex values and name the array $1
# declare global array, not assigning a value for 2 reasons:
# - command substitution exit code would be discarded
# - declaring arrays with variable name require quoting the right side: $n='(1 2 3)'
declare -a -g ${1:?}
# declare a local nameref with a static name so no quoting is needed
local -n a=${1:?}
a=( $(echo "${2:?}" | base64 -d | fn_hexdump_stdin) )
}
fn_hexdump_stdin() {
# hexdump stdin as values separated by space
hexdump -v -e '1/1 " %02X"'
}
fn_format_hexarray_uuid_as_isostr() {
#ex: fn_format_hexarray_uuid_as_iso hexarray
local -n G=${1:?}
OBJECTGUID="${G[3]}${G[2]}${G[1]}${G[0]}-${G[5]}${G[4]}-${G[7]}${G[6]}-${G[8]}${G[9]}-${G[10]}${G[11]}${G[12]}${G[13]}${G[14]}${G[15]:?}"
}
fn_format_hexarray_as_ldap_filter_value() {
# escape hexarray named $1 as ldap filter value and store in variable named $2
declare -g ${2:?}=
local -n G=${1:?}
local -n s=${2:?}
for h in "${G[@]}"; do
s=$s'\'$h
done
}
printf '(|'
for OBJECT_ID in "$@"; do
fn_base64_uuid_to_hexarray hexarray "$OBJECT_ID"
fn_format_hexarray_as_ldap_filter_value hexarray ldap_filter_value
printf '(ObjectGuid=%s)' "$ldap_filter_value"
done
printf ')\n'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment