Base 64 és un sistema de numeració posicional que utilitza 64 com a base. 64 és la major potència que es pot utilitzar únicament utilitzant els caràcters imprimibles del codi ASCII
A Linux tenim l'ordre base64 proporcionada pel paquet coreutils:
$ which base64 /usr/bin/base64 $ dpkg -S /usr/bin/base64 coreutils: /usr/bin/base64
Segons el manual:
$ man base64
l'ordre permet codificar i descodificar base64. Es pot codificar el text contingut en un fitxer:
$ echo "Hola\!" > text_a_codificar.txt $ base64 text_a_codificar.txt SG9sYVwhCg==
O es pot utilitzar directament l'ordre echo:
$ echo "Hola\!" | base64 SG9sYVwhCg==
Per decodificar cal utilitzar -d:
$ echo "SG9sYVwhCg==" | base64 -d Hola\!
Al format LDIF el format s'utilitza per codificar camps amb caràcters estranys:
$ echo "Y249VHVyIEJhZGVuYXMgU2VyZ2ksb3U9cGVvcGxlLG91PUluZm9ybcOgdGljYSxvdT1Qcm9mZXMsb3U9QWxsLGRjPWllc2VicmUsZGM9Y29t" | base64 -d cn=Tur Badenas Sergi,ou=people,ou=Informàtica,ou=Profes,ou=All,dc=iesebre,dc=com
MD5:
$ echo "e01ENX0rb2tOcTl3WUdTT1gwb2c1ZEpUclJRPT0=" | base64 --decode {MD5}+okNq9wYGSOX0og5dJTrRQ==
SHA:
$ echo "e1NIQX1Zbm1JYjk0SkN6QTQ4bWNKaTh5bmNhYnZxVVk9" | base64 -d {SHA}YnmIb94JCzA48mcJi8yncabvqUY=
SSHA:
$ echo "e1NTSEF9WEtZV05VM3Y2eE4wSE5wOEp6Skh0L2FySE5OSVEwTnQ=" | base64 -d {SSHA}XKYWNU3v6xN0HNp8JzJHt/arHNNIQ0Nt
Crypt:
$ echo "e0NSWVBUfWwwS0tGSTFoa1NnN0E=" | base64 -d {CRYPT}l0KKFI1hkSg7A
Si voleu comparar directament els primers caràcters binaris:
$ echo "e1NIQX1" | base64 -d {SHA}base64
$ echo "e1NTSEF9W" | base64 -d {SSHA}
$ echo "e0NSWVBUfW" | base64 -d {CRYPT}
El següent script:
$ cat un64.sh
awk '\ BEGIN {\ FS=":: ";\ c="base64 -d"\ }\ {\ if(/^dn:: /) { \ print $2 |& c; close(c,"to"); c |& getline $2; close(c); printf("%s: %s\n", $1, $2); next\ }\ print $0\ }'
Permet decodificar NOMÉS les entrades dn::. Es recomanable abans aplicar unldif:
$ cat copiaLdap7Agost2012_montsia | ./unldif.sed | ./un64 > fitxerresultat.ldif
També es pot crear un alias per descodificar base64:
$ alias un64='awk '\''BEGIN{FS=":: ";c="base64 -d"}{if(/\w+:: /) {print $2 |& c; close(c,"to"); c |& getline $2; close(c); printf("%s:: \"%s\"\n", $1, $2); next} print $0 }'\'''
IMPORTANT: Compte que l'exemple posa entre cometes "" el resultat
Això permet el següent:
$ ldapsearch -h localhost -D "cn=admin,dc=iesebre,dc=com" -w ie76pNCgxC3Ig -x -b dc=iesebre,dc=com -u -t -LLL '(irisPersonalUniqueID=14278003K)' jpegPhoto | un64 base64: l’entrada no és vàlida dn:: "cn=Tur Badenas Sergi,ou=people,ou=Informàtica,ou=Prof" XMsb3U9QWxsLGRjPWllc2VicmUsZGM9Y29t ufn: Tur Badenas Sergi, people, Inform\C3\A0tica, Profes, All, iesebre.com dn: cn=webfaltes,ou=people,ou=acls,dc=iesebre,dc=com ufn: webfaltes, people, acls, iesebre.com dn: cn=Simpson Springfield Homer,ou=people,ou=Alumnes,ou=All,dc=iesebre,dc=com ufn: Simpson Springfield Homer, people, Alumnes, All, iesebre.com dn: cn=Tur Badenas Borrar,ou=people,dc=iesebre,dc=com ufn: Tur Badenas Borrar, people, iesebre.com
Fixeu-vos però amb el problema que hi ha quan el resultat és multilínia. Consulteu #unldif.
Amb sed:
With this sed script, if the line starts with a blank space, eliminate the space and append it to the previous line:
sed -e :a -e '$!N;s/\n //;ta' -e 'P;D' datafile
It does NOT check if the previous line starts with dn.
This is a mod of a script from Eric Pement's sed one-liners:
http://www.pement.org/sed/sed1line.txt
Per tal de tornar a plegar el fitxer ldif podeu utilitzar:
$ joe fold.awk #!/usr/bin/awk -f # Disclaimer and Terms: You may use these scripts for commercial or # non-commercial use at your own risk, as long as you retain the # copyright statements in the source code. These scripts are provided # "AS IS" with no warranty whatsoever and are FREE for as long as you # want to use them. You can edit and adapt them to your requirements # without seeking permission from me. I only ask that you retain the # credits where they are due. # # Author: Vishal Goenka <[email protected]> # # Fold MIME Content at the given line length # Version 1.0 # # Usage: fold.awk [width=<length to fold at>] <ldif file> # or # cat <ldif file> | fold.awk [width=<length to fold at>] # and if /usr/bin/awk is not available, # awk [width=<length to fold at>] -f fold.awk <ldif file> # # This AWK script implements line delimiting and folding as described in # "5.8.1 Line delimiting and folding" RFC2425. # # The parameter "width" can be used to change the length of the resulting # lines. Folding defaults to 75 characters per line. # # On Solaris and other Unix environments, try nawk/gawk if you get an # error with awk. { if (!width) width=75; line = $0; do { print substr(line, 0, width); line = " " substr(line, width+1); } while (length(line) > 1); }
Recursos:
unldif és un script de sed que evita que ldif salti de línia quan el valor és molt llarg. Creeu el fitxer amb:
$ joe unldif.sed
#!/bin/sed -nf # Disclaimer and Terms: You may use these scripts for commercial or # non-commercial use at your own risk, as long as you retain the # copyright statements in the source code. These scripts are provided # "AS IS" with no warranty whatsoever and are FREE for as long as you # want to use them. You can edit and adapt them to your requirements # without seeking permission from me. I only ask that you retain the # credits where they are due. # # Author: Vishal Goenka <[email protected]> # # Unfold LDIF (LDAP Data Interchange Format) lines # Version 1.0 # # Usage: unldif.sed <ldif file> # or # cat <ldif file> | unldif.sed # and if /usr/bin/sed is not available, # sed -nf unldif.sed <ldif file> # # Most LDIF generators will fold a long field on multiple lines by # inserting a line separator (either a linefeed or carriage # return/linefeed pair) followed by a space. Processing such ldif # files through another script becomes much easier if such lines were # unfolded. That is exactly what this script does. It unfolds ldif # entries that are folded (broken) across lines and writes them on a # single line. # { 1{ h; n; } /^ /!{ H; g; s,\n.*,,p; g; s,.*\n,,; h; } /^ /{ H; g; s,\n ,,; h; } ${ g; s,\n ,,; p; } }
Feu-lo executable:
$ sudo chmod +x unldif.sed
Abans:
$ ldapsearch -h localhost -D "cn=admin,dc=iesebre,dc=com" -w secret -A -x -b dc=iesebre,dc=com -u -t -LLL '(irisPersonalUniqueID=14268056K)' jpegPhoto dn:: Y249VHVyIEJhZGVuYXMgU2VyZ2ksb3U9cGVvcGxlLG91PUluZm9ybcOgdGljYSxvdT1Qcm9mZ XMsb3U9QWxsLGRjPWllc2VicmUsZGM9Y29t ufn: Tur Badenas Sergi, people, Inform\C3\A0tica, Profes, All, iesebre.com dn: cn=webfaltes,ou=people,ou=acls,dc=iesebre,dc=com ufn: webfaltes, people, acls, iesebre.com dn: cn=Simpson Springfield Homer,ou=people,ou=Alumnes,ou=All,dc=iesebre,dc=com ufn: Simpson Springfield Homer, people, Alumnes, All, iesebre.com dn: cn=Tur Badenas Borrar,ou=people,dc=iesebre,dc=com ufn: Tur Badenas Borrar, people, iesebre.com
Després amb unldif:
$ ldapsearch -h localhost -D "cn=admin,dc=iesebre,dc=com" -w secret -A -x -b dc=iesebre,dc=com -u -t -LLL '(irisPersonalUniqueID=14268034K)' jpegPhoto | ./unldif.sed dn:: Y249VHVyIEJhZGVuYXMgU2VyZ2ksb3U9cGVvcGxlLG91PUluZm9ybcOgdGljYSxvdT1Qcm9mZXMsb3U9QWxsLGRjPWllc2VicmUsZGM9Y29t ufn: Tur Badenas Sergi, people, Inform\C3\A0tica, Profes, All, iesebre.com dn: cn=webfaltes,ou=people,ou=acls,dc=iesebre,dc=com ufn: webfaltes, people, acls, iesebre.com dn: cn=Simpson Springfield Homer,ou=people,ou=Alumnes,ou=All,dc=iesebre,dc=com ufn: Simpson Springfield Homer, people, Alumnes, All, iesebre.com dn: cn=Tur Badenas Borrar,ou=people,dc=iesebre,dc=com ufn: Tur Badenas Borrar, people, iesebre.com
Si ho combinem amb #un64:
$ ldapsearch -h localhost -D "cn=admin,dc=iesebre,dc=com" -w secret -A -x -b dc=iesebre,dc=com -u -t -LLL '(irisPersonalUniqueID=14268045K)' jpegPhoto | ./unldif.sed | un64 dn:: "cn=Tur Badenas Sergi,ou=people,ou=Informàtica,ou=Profes,ou=All,dc=iesebre,dc=com" ufn: Tur Badenas Sergi, people, Inform\C3\A0tica, Profes, All, iesebre.com dn: cn=webfaltes,ou=people,ou=acls,dc=iesebre,dc=com ufn: webfaltes, people, acls, iesebre.com dn: cn=Simpson Springfield Homer,ou=people,ou=Alumnes,ou=All,dc=iesebre,dc=com ufn: Simpson Springfield Homer, people, Alumnes, All, iesebre.com dn: cn=Tur Badenas Borrar,ou=people,dc=iesebre,dc=com ufn: Tur Badenas Borrar, people, iesebre.com
Per defecte les sortides de text codificat en base64 de l'ordre base64 tenen una mida màxima de columnes per línia de 76:
$ man base64 ... -w, --wrap=COLS wrap encoded lines after COLS character (default 76). Use 0 to disable line wrapping
Podeu doncs posar
base64 -w 0
Per encabir tota la sortida en una línia.
També podeu utilitzar perl:
$ perl -00 -pe 's/\n (\S)/$1/g'
NOTA: Hi ha vegades que l'últim exemple no funciona i altres sí i no se el perquè...