summaryrefslogtreecommitdiffstats
path: root/pwman
blob: 85e2f9554915c0ff711d517422b7f48451a188fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python
"""
# Simple password manager
# Copyright (c) 2011 Michael Buesch <m@bues.ch>
# Licensed under the GNU/GPL version 2 or later.
"""

import sys
import getopt
import libpwman as pwman


def testEscape():
	t0 = t1 = '1-2x-y-a\tb c\\\\_ \\\\ x_-y\r\n_\tz\v__\\\\__\\-'
	nrIter = 3
	print "ORIG :", t0
	for i in range(0, nrIter):
		t1 = pwman.escapeCmd(t1)
		print "ESC%d : %s" % (i, t1)
	for i in range(0, nrIter):
		t1 = pwman.unescapeCmd(t1)
	print "UNESC:", t1
	assert(t1 == t0)

def usage():
	print "pwman v%d - lightweight password manager" % pwman.VERSION
	print ""
	print "Usage: %s [OPTIONS] [COMMANDS]" % sys.argv[0]
	print ""
	print "If COMMANDS are given, run these commands."
	print "Starts in interactive mode, otherwise."
	print ""
	print "Options:"
	print " -d|--database PATH      Use PATH as database file."
	print "                         If not given, %s is used." %\
						pwman.getDefaultDatabase()
	print " -U|--commit-clear-undo  The commit command clears undo queue."
	print " -t|--timeout SECONDS    Sets the session timeout. Default is 10 minutes."

def main():
	dbFile = None
	commitClearsUndo = False
	timeout = 600
	try:
		(opts, args) = getopt.getopt(sys.argv[1:],
			"hd:Ut:",
			[ "help", "database=", "test", "commit-clear-undo",
			  "timeout=", ])
		for (o, v) in opts:
			if o in ("-h", "--help"):
				usage()
				return 0
			if o in ("-d", "--database"):
				dbFile = v
			if o == "--test":
				testEscape()
				return 0
			if o in ("-U", "--commit-clear-undo"):
				commitClearsUndo = True
			if o in ("-t", "--timeout"):
				timeout = int(v)
	except (getopt.GetoptError, ValueError), e:
		usage()
		return 1
	commands = args
	if not dbFile:
		dbFile = pwman.getDefaultDatabase()
	if not dbFile:
		print "No database file specified"
		return 1
	print "Opening database '%s'..." % dbFile
	passphrase = pwman.readPassphrase("Master passphrase",
					  not pwman.fileExists(dbFile))
	if passphrase is None:
		return 1
	try:
		p = pwman.PWMan(dbFile, passphrase,
				commitClearsUndo=commitClearsUndo,
				timeout=timeout)
		if commands:
			for command in commands:
				p.runOneCommand(command)
		else:
			p.interactive()
	except (pwman.PWMan.Error), e:
		print "Error: " + str(e)
		return 1
	except (pwman.PWManTimeout), e:
		pwman.clearScreen()
		print "pwman session timeout after %d seconds of inactivity." % e.seconds
		p.flunkDirty()
		print "exiting..."
		return 1
	return 0

if __name__ == "__main__":
	sys.exit(main())
bues.ch cgit interface