[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: sed or awk: decode base64 string in passwd-like file



On Wed, Jun 29, 2011 at 4:42 PM, Denny Schierz <linuxmail@4lin.net> wrote:
> hi,
>
> I have a file with strings like:
>
> testds@domain.foo:e0NSWVBUfVUx=:500:12002::/imap/spool/domain.foo/%1n/%
> n:storage=50
>
> I need to decode the second field (password field), with something like:
>
> echo e0NSWVBUfVUx= | openssl base64 -d
>
> How can I do this with all other lines?
>
> I have already a small awk script, that converts my ldapsearch output to
> a Dovecot readable passwd file.
>
> ===============
> /^uid: / {uid=$2}
> /^uid: / {uid=$2; u=tolower(substr(uid,1,1));}
> /^postalAddress:/ {maildomain=$2}
> /^myMailQuota/ {mailquota=$2}
> /^userPassword/ {userpassword=$2}
> /^dn/ {printf("%s@%s:%s:500:12002::/imap/spool/%s/%s/%s:storage=%s
> \n",uid,maildomain,userpassword,maildomain,u,uid,mailquota)}
> END {printf("%s@%s:%s:500:12002::/imap/spool/%s/%s/%s:storage=%s
> \n",uid,maildomain,userpassword,maildomain,u,uid,mailquota)}
> ================
>
> the third %s must be changed, means decoded from base64.
>
> any suggestions?
>
use perl ;

perl -F: -a -ne ' $F1=`echo $F[1] | openssl base64 -d`; print join
(":",$F[0],$F1,@F[2 .. $#F])' file

or

use pure perl;

perl -MMIME::Base64 -F: -a -ne '@a=@F; $a[1]=decode_base64($a[1]);
print join (":",@a)' file

Regards,


Reply to: