Pure AWK Base64 decoder for LDIF
What if your LDIF content contains BASE64 encoded values and you want it more human readable?
It can be done using pure AWK. Feel free to use the following AWK script.
/^\w+:.+/ || /^$/ { if(b64) { obc=0 printf attr" " for(i=1;i<=length(b64);i++) { c=index( \ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", \ substr(b64,i,1) \ ) if(c--) { for(b=0;b<6;b++) { o=o*2+int(c/32) c=(c*2)%64 if(++obc==8) { if (o>31 || o==9) { printf "%c",o } else { printf "." } obc=0 o=0 } } } } b64="" print b46 } } /^\w+:: .+/ { attr=$1; b64=$NF } /^ .+/ { if (b64){ b64=b64$NF } } { if(!b64) { print } }
I have shell alias:
alias un64='awk "/^\w+:.+/ || /^$/ { if(b64) { obc=0; printf attr\" \"; for(i=1;i31 || o==9 {printf \"%c\",o} else {printf \".\"} obc=0; o=0; }} }}; b64=\"\"; \\ print b46} } /^\w+:: .+/ {attr=\$1; b64=\$NF} /^ .+/ {if (b64){b64=b64\$NF} } \\ { if(!b64) {print} }"'
and LDIF with encoded attributes:
# extended LDIF # # LDAPv3 # base with scope subtree # filter: (...) # requesting: displayName # # postgres, Services, UNIX, test dn: CN=postgres,OU=Services,OU=UNIX,DC=test displayName:: 0J/RgNC+0LLQtdGA0LrQsA== # search result search: 2 result: 0 Success # numResponses: 2 # numEntries: 1
Now you can decode on the fly:
$ ldapsearch ... displayName | un64 # extended LDIF # # LDAPv3 # base with scope subtree # filter: (...) # requesting: displayName # # postgres, Services, UNIX, test dn: CN=postgres,OU=Services,OU=UNIX,DC=test displayName:: Проверка # search result search: 2 result: 0 Success # numResponses: 2 # numEntries: 1











