[13] | 1 | #!/usr/bin/python |
---|
| 2 | """This script will take the plaintext export file from MyPasswordSafe (named |
---|
| 3 | 'mypasswordsafe.txt' in the current directory) and convert it to the CSV import |
---|
| 4 | format expected by OI Safe (for the Android phone platform) and write it to |
---|
| 5 | 'oisafe.csv'. |
---|
[14] | 6 | |
---|
| 7 | Copyright 2009, Eli Carter |
---|
| 8 | GPLv2 or later |
---|
[13] | 9 | """ |
---|
| 10 | |
---|
| 11 | class PasswordData(object): |
---|
| 12 | """Holds all information about the passwords. |
---|
| 13 | |
---|
| 14 | Can load from MyPasswordSafe exports, and save to a form OISafe can import. |
---|
| 15 | """ |
---|
| 16 | def __init__(self): |
---|
| 17 | self.master_password = None |
---|
| 18 | self.passwords = None |
---|
| 19 | |
---|
| 20 | def load_mps(self, filename): |
---|
| 21 | """Expects a plain text export from mypasswordsafe as input |
---|
| 22 | That file is kind of a tab-delimitted file, with the master password as |
---|
| 23 | the only content of the first line. |
---|
| 24 | The fields are: |
---|
| 25 | name, user, password, created, modified, accessed, lifetime, uuid |
---|
| 26 | notes |
---|
| 27 | |
---|
| 28 | Where the notes field is the content of the second line, with \n indicating newlines. |
---|
| 29 | """ |
---|
| 30 | lines = open(filename).readlines() |
---|
| 31 | self.master_password = lines[:-1] |
---|
| 32 | self.passwords = [] |
---|
| 33 | field_names = ['name', 'user', 'password', 'group', 'created', |
---|
| 34 | 'modified', 'accessed', 'lifetime', 'uuid'] |
---|
| 35 | for i in range(1, len(lines), 2): |
---|
| 36 | entry = {} |
---|
| 37 | fields = lines[i].split('\t') |
---|
| 38 | entry['notes'] = lines[i+1].replace('\\n', '\n') |
---|
| 39 | for fname, fvalue in zip(field_names, fields): |
---|
| 40 | #print fname, fvalue |
---|
| 41 | entry[fname] = fvalue |
---|
| 42 | self.passwords.append(entry) |
---|
| 43 | |
---|
| 44 | def save_oisafe(self, filename): |
---|
| 45 | """Writes out the password information stored in this object to the |
---|
| 46 | named file in a format that OI Safe can import. |
---|
| 47 | """ |
---|
| 48 | out = open(filename, 'w') |
---|
| 49 | out.write('"Category","Description","Website","Username","Password",' |
---|
| 50 | '"Notes"\n') |
---|
| 51 | for entry in self.passwords: |
---|
| 52 | category = csv_escape_quotes(entry['group'].lstrip('/')) |
---|
| 53 | description = csv_escape_quotes(entry['name']) |
---|
| 54 | website = '' |
---|
| 55 | username = csv_escape_quotes(entry['user']) |
---|
| 56 | password = csv_escape_quotes(entry['password']) |
---|
| 57 | full_notes = entry['notes'].strip('\n') + '\n\n' |
---|
| 58 | for extra_field in ['created', 'modified', 'accessed', 'lifetime', |
---|
| 59 | 'uuid']: |
---|
| 60 | full_notes += '%s: %s\n' % (extra_field, entry[extra_field]) |
---|
| 61 | notes = csv_escape_quotes(full_notes.rstrip('\n')) |
---|
| 62 | |
---|
| 63 | line = '","'.join([category, description, website, username, |
---|
| 64 | password, notes]) |
---|
| 65 | out.write('"%s"\n' % line) |
---|
| 66 | |
---|
| 67 | |
---|
| 68 | def csv_escape_quotes(text): |
---|
| 69 | """Quote the " characters in the provided text by doubling them. |
---|
| 70 | """ |
---|
| 71 | return text.replace('"', '""') |
---|
| 72 | |
---|
| 73 | |
---|
| 74 | def main(): |
---|
| 75 | """Converts a MyPasswordSafe export to an OI Safe import. |
---|
| 76 | """ |
---|
| 77 | database = PasswordData() |
---|
| 78 | database.load_mps('mypasswordsafe.txt') |
---|
| 79 | database.save_oisafe('oisafe.csv') |
---|
| 80 | |
---|
| 81 | |
---|
| 82 | if __name__ == '__main__': |
---|
| 83 | main() |
---|
| 84 | |
---|