I need to decode base64 embedded in ldif (openldap) backups.
I found here a way to join lines starting with a blank.
Then, based on this question about "How to decode base64 text in xml file in Linux?" I want to decode the base64 strings, but I'm not being able to get it to work.
My Script is:
#Join lines starting with space
sed -n 'H; ${ x; s/\n//; s/\n //g; p}' "$FILE" > "$FILE_JOINED"
#Decode lines containing base64 (those with double colon)
sed -r 's/(:: )([[:graph:]]+)/\1 '"`grep -oP ':: [[:graph:]]+' "$FILE_JOINED" |cut -c 4- | base64 -d`"'/g' "$FILE_JOINED"
When I execute this, I get the following error:
sed: -e expression #1, char 297: unknown option to `s'
Here I add an example of the "$FILE_JOINED" contents:
dn: olcDatabase={1}mdb,cn=config
objectClass: olcDatabaseConfig
objectClass: olcMdbConfig
olcDatabase: {1}mdb
olcDbDirectory: /var/lib/ldap
olcSuffix: dc=proxy,dc=ldap
olcAccess:: b25lIHZhbHVlCg==
olcAccess: {1}to filter=(&(objectClass=securityPrincipal)(!(pwdAccountLockedTime=*))) attrs=userPassword,shadowLastChange by dn="cn=Man1,ou=local,dc=proxy,dc=ldap" write by anonymous auth by self write by * none
olcAccess: {2} to * by * read
olcAddContentAcl: FALSE
olcLastMod: TRUE
olcMaxDerefDepth: 15
olcReadOnly: FALSE
olcRootDN: cn=Man1,ou=local,dc=proxy,dc=ldap
olcRootPW:: dmFsdWUgdHdvCg==
olcSyncUseSubentry: FALSE
olcSyncrepl:: dmFsdWUgdGhyZWUK
olcMirrorMode: TRUE
dn: olcOverlay={0}unique,olcDatabase={1}mdb,cn=config
objectClass: olcOverlayConfig
objectClass: olcUniqueConfig
(NOTE that the second command leaves the double colon (::
) instead of leaving only one. I did it on purpose to be able to easily grep the output. I'll fix that later)
The second command has a grep in it: How does it "select" the correct line to decode in all the file contents?
Here is the result of the grep
command alone:
# grep -oP ':: [[:graph:]]+' x |cut -c 4- | base64 -d
one value
value two
value three
Could anybody please give me any pointers on how to decode the base64 values contained in a ldif file?
base64 -d
may include a/
and so is terminating thesed
statement too early. You may need to force any/
to be quoted to protect it.grep
command, I directly echoed the value to be base64 decoded