Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- gnome-keyring-change-passwords 'user|username_value=^gilles$' 'action_url|server=acme.example.com'
- #!/usr/bin/env python
- import getpass, re, sys
- import gnomekeyring
- def getpass2(prompt):
- input1 = getpass.getpass(prompt)
- input2 = getpass.getpass('Repeat ' + prompt)
- if input1 != input2:
- raise ValueError, 'password mismatch'
- return input1
- def check_conditions(conditions, attributes):
- for (names, regexp) in conditions:
- value = ''
- for name in names:
- if attributes.has_key(name):
- value = attributes[name]
- break
- if not re.search(regexp, value): return False
- return True
- def parse_condition_string(arg):
- eq = arg.index('=')
- return arg[:eq].split('|'), re.compile(arg[eq+1:])
- def change_passwords(keyring_name, conditions, old_password, new_password):
- '''Change the password in many Gnome Keyring entries to new_password.
- Iterate over the keyring keyring_name. Only items matching conditions and where
- the current password is old_password are considered. The argument conditions
- is a list of elements of the form (names, regexp) where names is a list of
- attribute names. An item matches the condition if the value of the first
- attribute in names that is present on the item contains a match for regexp.
- '''
- all_items = gnomekeyring.list_item_ids_sync(keyring_name)
- for item in all_items:
- attributes = gnomekeyring.item_get_attributes_sync(keyring_name, item)
- if not check_conditions(conditions, attributes): continue
- info = gnomekeyring.item_get_info_sync(keyring_name, item)
- display_name = info.get_display_name()
- if info.get_secret() == old_password:
- print 'changing:', display_name
- info.set_secret(new_password)
- gnomekeyring.item_set_info_sync(keyring_name, item, info)
- else:
- print 'has different password, skipping:', display_name
- def change_password_ui(condition_strings):
- conditions = map(parse_condition_string, condition_strings)
- old_password = getpass.getpass('Old password:')
- new_password = getpass2('New password:')
- change_passwords('login', conditions, old_password, new_password)
- if __name__ == '__main__':
- if '--help' in sys.argv:
- sys.stdout.write('''Usage: ' + sys.argv[0] + ' [CONDITION...]
- Change multiple entries in the Gnome Keyring login keyring.
- Prompt for the old and new password. Only entries for which the old password
- matches are modified.
- ''')
- else:
- change_password_ui(sys.argv[1:])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement