Search code examples
pythonmacosimaplaunchd

Offlineimap run with launchd can't find python modules used to hide password


I am trying to get offlineimap to run in the background on OSX El Capitan using launchd.

Here is my plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>com.andypierz.offlineimap.plist</string>
  <key>ProgramArguments</key>
  <array>
    <string>/usr/local/bin/offlineimap</string>
    <string>-u</string>
    <string>quiet</string>
   </array>
   <key>StartInterval</key>
   <integer>60</integer>
   <key>StandardErrorPath</key>
     <string>/Users/Andy/.Mailder/offlineimap_err.log</string>
    <key>StandardOutPath</key>
      <string>/Users/Andy/.Mailder/offlineimap.log</string>
</dict>
</plist>

This loads and runs, however my logs show offlineimap is running into an error:

OfflineIMAP 6.7.0
  Licensed under the GNU GPL v2 or any later version (with an OpenSSL exception)
ERROR: No module named keyring
ERROR: Exceptions occurred during the run!
ERROR: No module named keyring

To avoid have my passwords in plaintext in my .offlineimaprc I am using the python keyring method described here.

When I run offlineimap from the terminal it works fine and I am able to use the python keyring to import my passwords with no problem. Similarly, when I run offlineimap as a cronjob this also seems to work without incident. However, cron is deprecated on OSX so I would prefer to use launchd.

Relevant parts of my .offlineimaprc:

[general]
accounts = personal, work
maxsyncaccounts = 3
pythonfile = /Users/Andy/offlineimap.py

[Repository personalRemote]
type = IMAP
remotehost = myhost.com
remoteuser = [email protected]
remotepasseval = keyring.get_password('email', 'personal')

[Repository workRemote]
type = IMAP
remotehost = myhost.com
remoteuser = [email protected]
remotepasseval = keyring.get_password('email', 'work')

My offlineimap.py file is just

#!/usr/bin/python
import keyring

Solution

  • to answer the error:

    $sudo pip install keyring
    

    and if that does not work for you,

    0) create password in keychain like done here in Retrieving Passwords section

    1) create file

    $touch ~/offlineimap.py
    

    offlineimap.py

    #!/usr/bin/python
    import re, subprocess
    def get_keychain_pass(account=None, server=None):
        params = {
            'security': '/usr/bin/security',
            'command': 'find-internet-password',
            'account': account,
            'server': server,
            'keychain': '/Users/${USER}/Library/Keychains/login.keychain',
        }
        command = "sudo -u ${USER} %(security)s -v %(command)s -g -a %(account)s -s %(server)s %(keychain)s" % params
        output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT)
        outtext = [l for l in output.splitlines()
                   if l.startswith('password: ')][0]
    
        return re.match(r'password: "(.*)"', outtext).group(1)
    

    2) edit line in your

    .offlineimaprc

    pythonfile = ~/offlineimap.py
    

    3) and update remote sections

    remotepasseval = get_keychain_pass(account="[email protected]", server="sub.domain.com")
    

    4) if still having problems, try to change the command parameter in offlineimap.py file to

    find-generic-password
    

    5) correct your Label string and run it just once during start

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
        <key>Label</key>
        <string>com.andypierz.offlineimap</string>
        <key>LaunchOnlyOnce</key>
        <true/>
        <key>ProgramArguments</key>
        <array>
        <string>sh</string>
        <string>-c</string>
        <string>/usr/local/bin/offlineimap -u quiet</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
    </dict>
    </plist>
    

    6) load it

    launchctl load com.andypierz.offlineimap.plist