Advertisement
Guest User

Untitled

a guest
Jul 6th, 2015
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. gnome-keyring-change-passwords 'user|username_value=^gilles$' 'action_url|server=acme.example.com'
  2.  
  3. #!/usr/bin/env python
  4. import getpass, re, sys
  5. import gnomekeyring
  6.  
  7. def getpass2(prompt):
  8. input1 = getpass.getpass(prompt)
  9. input2 = getpass.getpass('Repeat ' + prompt)
  10. if input1 != input2:
  11. raise ValueError, 'password mismatch'
  12. return input1
  13.  
  14. def check_conditions(conditions, attributes):
  15. for (names, regexp) in conditions:
  16. value = ''
  17. for name in names:
  18. if attributes.has_key(name):
  19. value = attributes[name]
  20. break
  21. if not re.search(regexp, value): return False
  22. return True
  23.  
  24. def parse_condition_string(arg):
  25. eq = arg.index('=')
  26. return arg[:eq].split('|'), re.compile(arg[eq+1:])
  27.  
  28. def change_passwords(keyring_name, conditions, old_password, new_password):
  29. '''Change the password in many Gnome Keyring entries to new_password.
  30.  
  31. Iterate over the keyring keyring_name. Only items matching conditions and where
  32. the current password is old_password are considered. The argument conditions
  33. is a list of elements of the form (names, regexp) where names is a list of
  34. attribute names. An item matches the condition if the value of the first
  35. attribute in names that is present on the item contains a match for regexp.
  36. '''
  37. all_items = gnomekeyring.list_item_ids_sync(keyring_name)
  38. for item in all_items:
  39. attributes = gnomekeyring.item_get_attributes_sync(keyring_name, item)
  40. if not check_conditions(conditions, attributes): continue
  41. info = gnomekeyring.item_get_info_sync(keyring_name, item)
  42. display_name = info.get_display_name()
  43. if info.get_secret() == old_password:
  44. print 'changing:', display_name
  45. info.set_secret(new_password)
  46. gnomekeyring.item_set_info_sync(keyring_name, item, info)
  47. else:
  48. print 'has different password, skipping:', display_name
  49.  
  50. def change_password_ui(condition_strings):
  51. conditions = map(parse_condition_string, condition_strings)
  52. old_password = getpass.getpass('Old password:')
  53. new_password = getpass2('New password:')
  54. change_passwords('login', conditions, old_password, new_password)
  55.  
  56. if __name__ == '__main__':
  57. if '--help' in sys.argv:
  58. sys.stdout.write('''Usage: ' + sys.argv[0] + ' [CONDITION...]
  59. Change multiple entries in the Gnome Keyring login keyring.
  60. Prompt for the old and new password. Only entries for which the old password
  61. matches are modified.
  62.  
  63. ''')
  64. else:
  65. change_password_ui(sys.argv[1:])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement