offlineimap_auth.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/python3
  2. #
  3. # Offlineimap authentication using gpg and emacs authinfo.
  4. #
  5. # Copyright (C) 2023 Distopico <distopico@riseup.net>
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. import re
  20. import os
  21. import subprocess
  22. def set_credentials(repo, user, pw):
  23. KEYRING_NAME = 'offlineimap'
  24. attrs = {'repo': repo, 'user': user}
  25. # TODO: implement save credentials
  26. def get_credentials(account, port):
  27. s = 'machine %s ([^ ]*) (.*) port %s password ([^ ]*)\n' \
  28. % (account, port)
  29. p = re.compile(s)
  30. authinfo = os.popen('gpg -q --no-tty -d ~/.authinfo.gpg').read()
  31. return p.search(authinfo).group(1)
  32. def get_username(account, port):
  33. return get_credentials(account, port)
  34. def get_password(machine, login, port):
  35. s = 'machine %s login %s port %s password ([^ ]*)\n' \
  36. % (machine, login, port)
  37. p = re.compile(s)
  38. authinfo = os.popen('gpg -q --no-tty -d ~/.authinfo.gpg').read()
  39. return p.search(authinfo).group(1)
  40. def get_password_emacs(machine, account, port):
  41. return get_password(machine, account, port)
  42. if __name__ == '__main__':
  43. import sys
  44. import getpass
  45. if len(sys.argv) != 3:
  46. print('Usage: %s <repository> <username>' % (os.path.basename(sys.argv[0])))
  47. sys.exit(0)
  48. repo, username = sys.argv[1:]
  49. password = getpass.getpass('Enter password for user "%s": ' % username)
  50. password_confirmation = getpass.getpass('Confirm password: ')
  51. if password != password_confirmation:
  52. print('Error: password confirmation does not match')
  53. sys.exit(1)
  54. set_credentials(repo, username, password)