imap admin access with python's imaplib

Looking to migrate to ZCS? Ask here. Got a great tip or script that helped you migrate? Post it here.
Post Reply
sunqiang
Posts: 4
Joined: Sat Sep 13, 2014 2:38 am

imap admin access with python's imaplib

Post by sunqiang »

google search 'imap admin access' returns this post.

at first, I think python's imaplib.proxyauth is the intuitive way.

so here is the demo code:

hostname, username, password = get_config()

conn = imaplib.IMAP4_SSL(hostname)

typ, cap = conn.login(username, password) # username is a admin

conn.proxyauth("user1")



however, proxyauth raise a error "imaplib.error: PROXYAUTH command error: BAD ['parse error: command not implemented']
after some debuging, it seems the line "conn.login(username, password)" will translated to send the tcp string 'PALI1 LOGIN username "password"
', this works. and the line "conn.proxyauth("user1")" will translated to 'PALI2 PROXYAUTH user1
', and it failed.
so I wonder how imapsync, as this post said, knows to do this delegated authentication.
imapsync's "imapsync --host1 source.server.com --user1 username --authuser1 adminusername --password1 adminpassword " will translated to what TCP protocal string?
eduard.guloiu
Posts: 3
Joined: Sat Sep 13, 2014 2:52 am

imap admin access with python's imaplib

Post by eduard.guloiu »

Hello,
First of all you should enable Plain text login in zimbra (anyway that's the method that works with zimbra proxy).

If you use zimbra proxy (or not) you can auth as admin via ssl with the imap AUTH command.

This is what it works:

imap_conn = imaplib.IMAP4_SSL(imaphost,imapport)

authcb = lambda resp: "{0}x00{1}x00{2}".format(user,adminuser,adminpass)

imap_conn.authenticate("PLAIN", authcb)

I am working on a script that dumps the entire mailbox and import only differences in case of restore. I will post it as soon as it works.

Bare with me.

Tip: fetching one message a time takes let's say about 2 hours to backup, if you get bulk (let's say 200 msgs at once) it will last 6 minutes.
sunqiang
Posts: 4
Joined: Sat Sep 13, 2014 2:38 am

imap admin access with python's imaplib

Post by sunqiang »

[quote user="eduard.guloiu"]Hello,
First of all you should enable Plain text login in zimbra (anyway that's the method that works with zimbra proxy).

If you use zimbra proxy (or not) you can auth as admin via ssl with the imap AUTH command.

This is what it works:

imap_conn = imaplib.IMAP4_SSL(imaphost,imapport)

authcb = lambda resp: "{0}x00{1}x00{2}".format(user,adminuser,adminpass)

imap_conn.authenticate("PLAIN", authcb)

I am working on a script that dumps the entire mailbox and import only differences in case of restore. I will post it as soon as it works.

Bare with me.

Tip: fetching one message a time takes let's say about 2 hours to backup, if you get bulk (let's say 200 msgs at once) it will last 6 minutes.[/QUOTE]

thanks! you made my day! with your help, I search "python imap plain authentication", and it returns http://www.tylerlesmann.com/2009/apr/15 ... on-python/" (almost the same as yours direct answer)

can't waiting your dump and restore script~

thanks again.
eduard.guloiu
Posts: 3
Joined: Sat Sep 13, 2014 2:52 am

imap admin access with python's imaplib

Post by eduard.guloiu »

Finaly,
After a lot of work, here it is:

https://docs.google.com/open?id=0B5jJFD ... 1J1Q0p4UFk

Alternate location:

http://ed.contconsult.ro/?a=d&i=26bFu5VWVe
I have tested it wit gmail and imported back to zimbra. Some flags are awkward on gmail but I check all of them from script.
I am dumping all the messages in a shelve object with the key made of md5 from message header and folder. (you will not have duplicates).

Import is done using threads as max as batch_size, as python imap allows only one message at a time (which is very slow).
Use case example

dump gmail

gmail uses login over ssl so this should work:

/home/eguloiu/python-imap-util.py -i imap.gmail.com -u user@gmail.com -a user@gmail.com -p 'secret pass' -t 993 -s -b -l



import into zimbra:

/home/eguloiu/python-imap-util.py -i -u user@domain -a admin@domain -p -t 993 -s -r -f
See help for more options, or take a look into the scritp.
I need your feedback and anyone's who uses the script.


This is some output from my zimbra server:

------Backup start time 2012-09-20 18:57:58.012247

['"Chats"', '"Contacts"', '"Drafts"', '"INBOX"', '"INBOX/Drafts"', '"INBOX/Sent"', '"INBOX/Spam"', '"Junk"', '"Sent"']

Selected folder: "Chats"
Selected folder: "Contacts"
Selected folder: "Drafts"
Selected folder: "INBOX"

Writting message 75993 of 75993

Selected folder: "INBOX/Drafts"
Selected folder: "INBOX/Sent"
Selected folder: "INBOX/Spam"
Selected folder: "Junk"

Writting message 155 of 155

Selected folder: "Sent"
------Backup end time: 2012-09-20 19:03:49.420811, Duration: 0:05:51.408580


I will really appreciate any feedback.
Thanks
sunqiang
Posts: 4
Joined: Sat Sep 13, 2014 2:38 am

imap admin access with python's imaplib

Post by sunqiang »

well done, I'll check the it asap, and let you know if I had any issues.

thanks again

[quote user="eduard.guloiu"]Finaly,
After a lot of work, here it is:

https://docs.google.com/open?id=0B5jJFD ... 1J1Q0p4UFk

Alternate location:

[url=Open Upload - File download[/url]
I have tested it wit gmail and imported back to zimbra. Some flags are awkward on gmail but I check all of them from script.
I am dumping all the messages in a shelve object with the key made of md5 from message header and folder. (you will not have duplicates).

Import is done using threads as max as batch_size, as python imap allows only one message at a time (which is very slow).
Use case example

dump gmail

gmail uses login over ssl so this should work:

/home/eguloiu/python-imap-util.py -i imap.gmail.com -u user@gmail.com -a user@gmail.com -p 'secret pass' -t 993 -s -b -l



import into zimbra:

/home/eguloiu/python-imap-util.py -i -u user@domain -a admin@domain -p -t 993 -s -r -f
See help for more options, or take a look into the scritp.
I need your feedback and anyone's who uses the script.


This is some output from my zimbra server:

------Backup start time 2012-09-20 18:57:58.012247

['"Chats"', '"Contacts"', '"Drafts"', '"INBOX"', '"INBOX/Drafts"', '"INBOX/Sent"', '"INBOX/Spam"', '"Junk"', '"Sent"']

Selected folder: "Chats"
Selected folder: "Contacts"
Selected folder: "Drafts"
Selected folder: "INBOX"

Writting message 75993 of 75993

Selected folder: "INBOX/Drafts"
Selected folder: "INBOX/Sent"
Selected folder: "INBOX/Spam"
Selected folder: "Junk"

Writting message 155 of 155

Selected folder: "Sent"
------Backup end time: 2012-09-20 19:03:49.420811, Duration: 0:05:51.408580


I will really appreciate any feedback.
Thanks[/QUOTE]
sgiunchi
Posts: 21
Joined: Sat Sep 13, 2014 1:04 am

Re: imap admin access with python's imaplib

Post by sgiunchi »

imap_conn = imaplib.IMAP4_SSL(imaphost,imapport)
authcb = lambda resp: "{0}x00{1}x00{2}".format(user,adminuser,adminpass)
imap_conn.authenticate("PLAIN", authcb)
I tried for hours to make it work.

At last, I understood I had to change this:
authcb = lambda resp: "{0}x00{1}x00{2}".format(user,adminuser,adminpass)
with this:
authcb = lambda resp: "{0}\00{1}\00{2}".format(user,adminuser,adminpass)

Hope it helps.
Stefano
Post Reply