#!/usr/bin/env python
#-*- coding:utf-8 -*-
#Filename: manage_passwd.py
'''this file can help you manage all of your acounts and passwds that are hard to remember. with this tool, you just need to memorize the name of your website or app account. Introducton: 1.manpwd is a softlink to this file which has beed made a system command in /usr/bin/manpwd 2.the file which contains your password contents are encrypted, so if it somehow obtained by anyone, it's hard for them to decrypt it. And before that, you can change them. 3.in the following text, [] stands for the content you need to input Usage: 1.manpwd new --- newly create a record file. 2.manpwd add [name] --- add a record. [name] is the website's or app's name. if the record files are not created, add action is failed. name can't be same with recorded ones, or action os failed. "manpwd del" and "manpwd mod" are both suitable 3.manpwd del [name] --- del a record. if [name] is not in record, action failed. 4.manpwd mod [name] --- modify a record. 5.manpwd qry [name] --- query a specified name's information Record Structure itemname: ... username: ... email: ... password: ... '''
import sys
import pickle
import os
import platform
import base64
RECORD_FILE = '/home/xieyuan/.passwd_record/record.new'
TEMPFILE = RECORD_FILE + '.tmp'
class PasswdManager:
def __init__(self):
self.user_file = RECORD_FILE
self.record = {}
self.usage = ''' Usage: 1.manpwd new --- newly create a record file. 2.manpwd add [name] --- add a record. when names conflict, fail 3.manpwd del [name] --- del a record. name must be in record 4.manpwd mod [name] --- modify a record. empty items be noticed 5.manpwd qry [name] --- display a set of information of [name] 6.manpwd qry --- display all the names, in case you forget them'''
def Dispatch(self):
if len(sys.argv) == 2:
if sys.argv[1] == 'new':
self.CreateFile()
elif sys.argv[1] == 'qry':
self.QueryReocrd('')
else:
print(self.usage)
elif len(sys.argv) == 3:
if sys.argv[1] == 'add':
self.AddRecord(sys.argv[2])
elif sys.argv[1] == 'del':
self.DelRecord(sys.argv[2])
elif sys.argv[1] == 'mod':
self.ModRecord(sys.argv[2])
elif sys.argv[1] == 'qry':
self.QueryReocrd(sys.argv[2])
else:
print(self.usage)
else:
print(self.usage)
def isRecordExists(self):
return os.path.exists(self.user_file)
def DecodeFile(self):
f = open(self.user_file, 'r+')
filestring = f.read()
newstring = base64.decodestring(filestring)
ftmp = open(TEMPFILE, 'w+')
ftmp.write(newstring)
f.close()
ftmp.close()
with open(TEMPFILE, 'rb') as f:
self.record = pickle.load(f)
os.remove(TEMPFILE)
def EncodeFile(self):
with open(self.user_file, 'wb') as f:
pickle.dump(self.record, f)
f = open(self.user_file, 'r+')
filestring = f.read()
newstring = base64.encodestring(filestring)
f = open(self.user_file, 'w+')
f.write(newstring)
f.close()
def CreateFile(self):
#first see if the file already exists
if os.path.exists(self.user_file):
self.FeedBack('record file', 'already exists')
else:
os.mknod(self.user_file)
self.FeedBack('create record', 'ok!')
def AddRecord(self, name):
if self.isRecordExists():
#if file not empty, load first
if os.path.getsize(RECORD_FILE) > 0:
self.DecodeFile()
#if file empty, write directly
if name in self.record:
self.FeedBack('names', 'conflict...')
else:
user = raw_input('username: ')
email = raw_input('email: ')
passwd = raw_input('password: ')
self.record[name] = (user, email, passwd)
self.EncodeFile()
self.FeedBack('add record', 'ok!')
else:
self.FeedBack('record', 'not found...')
def DelRecord(self, name):
if self.isRecordExists() and os.path.getsize(RECORD_FILE) > 0:
self.DecodeFile()
if name in self.record:
del self.record[name]
self.EncodeFile()
self.FeedBack('Delete record', 'ok')
else:
self.FeedBack('record', 'not exists')
else:
self.FeedBack('record', 'not exists')
def ModRecord(self, name):
if self.isRecordExists() and os.path.getsize(RECORD_FILE) > 0:
self.DecodeFile()
if name in self.record:
user = raw_input('username: ')
email = raw_input('email: ')
passwd = raw_input('password: ')
if user == '' or email == '' or passwd == '':
print('there is empty item(s), sure to modify?[y/n]')
sure = raw_input()
if not sure == 'y':
self.FeedBack('Modify record', 'abort')
return
self.record[name] = (user, email, passwd)
self.EncodeFile()
self.FeedBack('Modify record', 'ok')
else:
self.FeedBack('record', 'not exists')
else:
self.FeedBack('record', 'not exists')
def QueryReocrd(self, name):
if self.isRecordExists() and os.path.getsize(RECORD_FILE) > 0:
self.DecodeFile()
if not name == '':
if name in self.record:
self.ShowRecord(self.record[name])
else:
self.FeedBack('record', 'not exists')
else:
for key in self.record.keys():
# ShowRecord(self.record[key])
print(key + ' ')
else:
self.FeedBack('record', 'not exists')
def FeedBack(self, typeme='', infostr=''):
print(typeme + ' ' + infostr)
def ShowRecord(self, tup):
print('username: {0}\nemail: {1}\npassword: {2}\n'.format(tup[0], tup[1], tup[2]))
if __name__ == '__main__':
app = PasswdManager()
app.Dispatch()