From e81910097884e05a58da38e277b581ea3a073da9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esa=20M=C3=A4=C3=A4tt=C3=A4?= Date: Mon, 7 May 2012 20:02:23 +0300 Subject: [PATCH 1/1] Added passwordeval option and documentation. Removes need for plain text password in the getmailrc file. Instead of "password" option "passwordeval" option can be used. For example: passwordeval = gpg -d gmail.gpg The value of the "passwordeval" option is run and the output is used as the password. If password cannot be found from the output, then password is prompted from the user. --- docs/configuration.html | 22 ++++++++++++++++++++-- docs/getmailrc-examples | 34 ++++++++++++++++++++++++---------- getmailcore/retrievers.py | 11 +++++++++++ getmailcore/utilities.py | 24 +++++++++++++++++++++++- 4 files changed, 78 insertions(+), 13 deletions(-) diff --git a/docs/configuration.html b/docs/configuration.html index 0508ddf..f48ab27 100644 --- a/docs/configuration.html +++ b/docs/configuration.html @@ -548,8 +548,9 @@ type = typename password (string) — password to use when logging in to the mail server. If not - using Kerberos authentication -- see below -- getmail gets the password - credential for the POP/IMAP server in one of the following ways: + using Kerberos authentication or passwordeval option -- see below -- + getmail gets the password credential for the POP/IMAP server in one of + the following ways:
  1. from the password configuration item in the getmailrc file
  2. on Mac OS X only, from the OS X keychain
  3. @@ -569,6 +570,12 @@ type = typename via the keychain. See this posting to the getmail users' mailing list by Alan Schmitt for details. +
  4. + passwordeval + (string) + — command which output is used as the password. This can be used + instead of the password option. +
  5. All IMAP retriever types also take the following optional parameters: @@ -1248,6 +1255,17 @@ password = my_mail_password

    + A typical POP3 mail account with password in gpg encrypted file. +

    +
    +[retriever]
    +type = SimplePOP3Retriever
    +server = popmail.isp.example.net
    +username = account_name
    +passwordeval = gpg -d ~/.popmail.isp.example.net.gpg
    +
    + +

    If your ISP provides POP3 access on a non-standard port number, you would need to include the port parameter:

    diff --git a/docs/getmailrc-examples b/docs/getmailrc-examples index dc8da2d..3d98ce8 100644 --- a/docs/getmailrc-examples +++ b/docs/getmailrc-examples @@ -22,8 +22,22 @@ password = mailpassword type = Maildir path = ~jeffp/Maildir/ +# Example 2: same as (1), but evaluate password using external command. In this +# case gpg. # -# Example 2: same as (1), but operate quietly, delete messages from + +[retriever] +type = SimplePOP3Retriever +server = pop.example.net +username = jeff.plotzky +passwordeval = gpg -d ~/.pop.example.net.gpg + +[destination] +type = Maildir +path = ~jeffp/Maildir/ + +# +# Example 3: same as (1), but operate quietly, delete messages from # the server after retrieving them, and log getmail's actions (in detail) # to a file. # @@ -45,7 +59,7 @@ type = Maildir path = ~jeffp/Maildir/ # -# Example 3: same as (1), but the mail account is accessed via IMAP4 instead +# Example 4: same as (1), but the mail account is accessed via IMAP4 instead # of POP3. # @@ -60,7 +74,7 @@ type = Maildir path = ~jeffp/Maildir/ # -# Example 4: same as (3), but retrieve mail from the INBOX, INBOX.spam, and +# Example 5: same as (4), but retrieve mail from the INBOX, INBOX.spam, and # mailing-lists.getmail-users mail folders. # @@ -76,7 +90,7 @@ type = Maildir path = ~jeffp/Maildir/ # -# Example 5: same as (3), but move messages to the mail folder "sent-mail" +# Example 6: same as (4), but move messages to the mail folder "sent-mail" # after retrieving them. Note that you do this by setting delete and # move_on_delete options. # @@ -96,7 +110,7 @@ type = Maildir path = ~jeffp/Maildir/ # -# Example 6: same as (1), but deliver the messages to an mboxrd-format mbox +# Example 7: same as (1), but deliver the messages to an mboxrd-format mbox # file as user "jeffp". # @@ -112,7 +126,7 @@ path = ~jeffp/Mail/inbox user = jeffp # -# Example 7: same as (1), but deliver the messages through an external MDA +# Example 8: same as (1), but deliver the messages through an external MDA # which takes several arguments. # @@ -129,7 +143,7 @@ arguments = ("--message-from-stdin", "--scan-message", "--to-maildir", "~jeffp/Maildir/") # -# Example 8: retrieve mail from a corporate POP3-SSL domain mailbox, +# Example 9: retrieve mail from a corporate POP3-SSL domain mailbox, # sort messages for several local users and deliver to maildirs in their # home directories (except Sam, who likes mbox files, and Christina, who # uses procmail for further sorting), and deliver all other mail to @@ -187,7 +201,7 @@ arguments = ("-f", "%(sender)", "-m", "/home/christina/.procmailrc") user = christina # -# Example 9: same as (3), but use SpamAssassin to filter out spam, +# Example 10: same as (4), but use SpamAssassin to filter out spam, # and ClamAV to filter out MS worms. # @@ -213,7 +227,7 @@ type = Maildir path = ~jeffp/Maildir/ # -# Example 10: same as (3), but deliver all mail to two different local +# Example 11: same as (4), but deliver all mail to two different local # mailboxes. # @@ -231,7 +245,7 @@ destinations = ( ) # -# Example 11: retrieve mail from a simple (non-multidrop) POP3 mailbox. +# Example 12: retrieve mail from a simple (non-multidrop) POP3 mailbox. # Then extract addresses from the message header (see documentation for which # fields are examined), and deliver mail containing the address # to ~/Mail/lists/list1/, mail containing the diff --git a/getmailcore/retrievers.py b/getmailcore/retrievers.py index 6e82df7..351dade 100755 --- a/getmailcore/retrievers.py +++ b/getmailcore/retrievers.py @@ -62,6 +62,7 @@ class SimplePOP3Retriever(POP3RetrieverBase, POP3initMixIn): ConfInt(name='port', required=False, default=110), ConfString(name='username'), ConfPassword(name='password', required=False, default=None), + ConfString(name='passwordeval', required=False, default=None), ConfBool(name='use_apop', required=False, default=False), ConfBool(name='delete_dup_msgids', required=False, default=False), ) @@ -95,6 +96,7 @@ class SimplePOP3SSLRetriever(POP3RetrieverBase, POP3SSLinitMixIn): ConfInt(name='port', required=False, default=POP3_ssl_port), ConfString(name='username'), ConfPassword(name='password', required=False, default=None), + ConfString(name='passwordeval', required=False, default=None), ConfBool(name='use_apop', required=False, default=False), ConfBool(name='delete_dup_msgids', required=False, default=False), ConfFile(name='keyfile', required=False, default=None), @@ -171,6 +173,7 @@ class BrokenUIDLPOP3Retriever(BrokenUIDLPOP3RetrieverBase, POP3initMixIn): ConfInt(name='port', required=False, default=110), ConfString(name='username'), ConfPassword(name='password', required=False, default=None), + ConfString(name='passwordeval', required=False, default=None), ConfBool(name='use_apop', required=False, default=False), ) received_with = 'POP3' @@ -201,6 +204,7 @@ class BrokenUIDLPOP3SSLRetriever(BrokenUIDLPOP3RetrieverBase, POP3SSLinitMixIn): ConfInt(name='port', required=False, default=POP3_ssl_port), ConfString(name='username'), ConfPassword(name='password', required=False, default=None), + ConfString(name='passwordeval', required=False, default=None), ConfBool(name='use_apop', required=False, default=False), ConfFile(name='keyfile', required=False, default=None), ConfFile(name='certfile', required=False, default=None), @@ -237,6 +241,7 @@ class MultidropPOP3Retriever(MultidropPOP3RetrieverBase, POP3initMixIn): ConfInt(name='port', required=False, default=110), ConfString(name='username'), ConfPassword(name='password', required=False, default=None), + ConfString(name='passwordeval', required=False, default=None), ConfBool(name='use_apop', required=False, default=False), ConfString(name='envelope_recipient'), ) @@ -270,6 +275,7 @@ class MultidropPOP3SSLRetriever(MultidropPOP3RetrieverBase, POP3SSLinitMixIn): ConfInt(name='port', required=False, default=POP3_ssl_port), ConfString(name='username'), ConfPassword(name='password', required=False, default=None), + ConfString(name='passwordeval', required=False, default=None), ConfBool(name='use_apop', required=False, default=False), ConfString(name='envelope_recipient'), ConfFile(name='keyfile', required=False, default=None), @@ -316,6 +322,7 @@ class MultidropSDPSRetriever(SimplePOP3Retriever, POP3initMixIn): ConfInt(name='port', required=False, default=110), ConfString(name='username'), ConfPassword(name='password', required=False, default=None), + ConfString(name='passwordeval', required=False, default=None), # Demon apparently doesn't support APOP ConfBool(name='use_apop', required=False, default=False), ) @@ -368,6 +375,7 @@ class SimpleIMAPRetriever(IMAPRetrieverBase, IMAPinitMixIn): ConfInt(name='port', required=False, default=imaplib.IMAP4_PORT), ConfString(name='username'), ConfPassword(name='password', required=False, default=None), + ConfString(name='passwordeval', required=False, default=None), ConfTupleOfUnicode(name='mailboxes', required=False, default="('INBOX', )", allow_specials=('ALL',)), ConfBool(name='use_peek', required=False, default=True), @@ -409,6 +417,7 @@ class SimpleIMAPSSLRetriever(IMAPRetrieverBase, IMAPSSLinitMixIn): ConfInt(name='port', required=False, default=imaplib.IMAP4_SSL_PORT), ConfString(name='username'), ConfPassword(name='password', required=False, default=None), + ConfString(name='passwordeval', required=False, default=None), ConfTupleOfUnicode(name='mailboxes', required=False, default="('INBOX', )", allow_specials=('ALL',)), ConfBool(name='use_peek', required=False, default=True), @@ -454,6 +463,7 @@ class MultidropIMAPRetriever(MultidropIMAPRetrieverBase, IMAPinitMixIn): ConfInt(name='port', required=False, default=imaplib.IMAP4_PORT), ConfString(name='username'), ConfPassword(name='password', required=False, default=None), + ConfString(name='passwordeval', required=False, default=None), ConfTupleOfUnicode(name='mailboxes', required=False, default="('INBOX', )", allow_specials=('ALL',)), ConfBool(name='use_peek', required=False, default=True), @@ -496,6 +506,7 @@ class MultidropIMAPSSLRetriever(MultidropIMAPRetrieverBase, IMAPSSLinitMixIn): ConfInt(name='port', required=False, default=imaplib.IMAP4_SSL_PORT), ConfString(name='username'), ConfPassword(name='password', required=False, default=None), + ConfString(name='passwordeval', required=False, default=None), ConfTupleOfUnicode(name='mailboxes', required=False, default="('INBOX', )", allow_specials=('ALL',)), ConfBool(name='use_peek', required=False, default=True), diff --git a/getmailcore/utilities.py b/getmailcore/utilities.py index 9ed6643..572be61 100755 --- a/getmailcore/utilities.py +++ b/getmailcore/utilities.py @@ -688,7 +688,29 @@ if keychain_password is None: ####################################### def get_password(label, user, server, protocol, logger): - # try keychain/keyrings first, where available + # try passwordeval first + if label.conf['passwordeval']: + cmd = label.conf['passwordeval'] + (status, output) = commands.getstatusoutput(cmd) + password = None + if status != os.EX_OK or not output: + emsg = 'passwordeval command "{}" failed: {} {}'.format( + cmd, status, output) + # Add line separator if missing so that prompting for password is + # clearer + if emsg[-1] != os.linesep: + emsg += os.linesep + logger.error(emsg) + else: + # select last line as password from the output and remove any line + # separators from the end. + password = output.rstrip(os.linesep).rpartition(os.linesep)[2] + # if could not be evaluated, prompt in the usual way + if not password: + password = getpass.getpass('Enter password for %s: ' % label) + return password + + # try keychain/keyrings second, where available password = keychain_password(user, server, protocol, logger) if password: logger.debug('using password from keychain/keyring') -- 2.11.0