-
Notifications
You must be signed in to change notification settings - Fork 23
/
keepassphp-cli.php
229 lines (194 loc) · 5.79 KB
/
keepassphp-cli.php
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
function usageAndDie()
{
echo "Usage: php keepassphp-cli.php <command> [args...]",
"\n\nPossible commands are:",
"\n add <id> <kdbx file> <password> [key file] [kphpdb password] Adds a database",
"\n get <id> <password> [kphpdb password] Shows the content of a database",
"\n pwd <id> <n> <password> [kphpdb password] Gets a password",
"\n rem <id> <kphpdb password> Removes a database",
"\n kdbx <kdbx file> <password> [key file] Decrypts a database file",
"\n kdbx-pwd <entry uuid> <kdbx file> <password> [key file] Gets a password from a database file",
"\n encrypt <file> <password> [key file] Encrypts a file with the kdbx format",
"\n decrypt <file> <password> [key file] Decrypts a file encrypted with encrypt";
die();
}
function KPHPDebugAndDie($msg)
{
echo "\nError: $msg\n", "Debug data:\n",
\KeePassPHP\KeePassPHP::$debugData, "\n";
die();
}
function errorAndDie($msg)
{
echo "\nError: $msg\n";
die();
}
function visitDatabase(\KeePassPHP\Database $db)
{
echo "Database '", $db->getName(), "'\n";
$groups = $db->getGroups();
if($groups == null)
echo " (no groups)";
else
{
foreach($groups as &$g)
visitGroup($g, 4);
}
}
function visitGroup(\KeePassPHP\Group $group, $indent)
{
echo str_pad("", $indent, " "), "Group '", $group->name, "'\n";
if($group->groups != null)
{
foreach($group->groups as &$g)
visitGroup($g, $indent + 4);
}
if($group->entries == null)
echo str_pad("", $indent + 4, " "), "(no entries)\n";
else
{
foreach($group->entries as &$e)
visitEntry($e, $indent + 4);
}
}
function visitEntry(\KeePassPHP\Entry $entry, $indent)
{
echo str_pad("", $indent, " "),
$entry->uuid, "\t => ", $entry->getStringField(\KeePassPHP\Database::KEY_TITLE),
"\t", $entry->getStringField(\KeePassPHP\Database::KEY_USERNAME),
"\t", $entry->getStringField(\KeePassPHP\Database::KEY_URL), "\n";
}
$count = isset($argc) ? intval($argc) : 0;
if($count < 2)
usageAndDie();
// load classes
require_once "keepassphp/keepassphp.php";
use \KeePassPHP\KeePassPHP as KeePassPHP;
// configuration
$debugMode = true;
// execute command
$command = $argv[1];
if($command == "add")
{
if($count < 5)
usageAndDie();
// initialize KeePassPHP
if(!KeePassPHP::init(null, $debugMode))
KPHPDebugAndDie("Initialization failed.");
$dbid = $argv[2];
$file = $argv[3];
$pwd = $argv[4];
$keyfile = $count >= 6 ? $argv[5] : null;
$kphpdbPwd = $count >= 7 ? $argv[6] : null;
if(empty($kphpdbPwd))
$kphpdbPwd = KeePassPHP::extractHalfPassword($pwd);
if(KeePassPHP::existsKphpDB($dbid))
{
if(!KeePassPHP::removeDatabase($dbid, $kphpdbPwd))
KPHPDebugAndDie("Database '" . $dbid .
"' already exists and cannot be deleted.");
}
if(!KeePassPHP::addDatabaseFromFiles($dbid, $file, $pwd, $keyfile,
$kphpdbPwd, true))
KPHPDebugAndDie("Cannot add database '" . $dbid . "'.");
echo "Database '", $dbid, "' added successfully.";
}
else if($command == "get" || $command == "pwd")
{
$offset = $command == "pwd" ? 1 : 0;
if($count < 4 + $offset)
usageAndDie();
// initialize KeePassPHP
if(!KeePassPHP::init(null, $debugMode))
KPHPDebugAndDie("Initialization failed.");
$dbid = $argv[2];
$pwd = $argv[3 + $offset];
$kphpdbPwd = $count >= 5 + $offset ? $argv[4 + $offset] : null;
if(empty($kphpdbPwd))
$kphpdbPwd = KeePassPHP::extractHalfPassword($pwd);
$db = KeePassPHP::getDatabase($dbid, $kphpdbPwd, $pwd, $command == "pwd");
if($db == null)
KPHPDebugAndDie("Cannot get database '" . $dbid . "'.");
if($command == "pwd")
{
$r = $db->getPassword($argv[3]);
if($r == null)
echo "entry '", $argv[3], "' not found!";
else
echo $r;
}
else
visitDatabase($db);
}
else if($command == "rem")
{
if($count < 4)
usageAndDie();
// initialize KeePassPHP
if(!KeePassPHP::init(null, $debugMode))
KPHPDebugAndDie("Initialization failed.");
$dbid = $argv[2];
$pwd = $argv[3];
if(KeePassPHP::removeDatabase($dbid, $pwd))
echo "Database '", $dbid, "' successfully removed.";
else
KPHPDebugAndDie("Cannot remove database '" . $dbid . "'.");
}
else if($command == "kdbx" || $command == "kdbx-pwd")
{
// no need to initialize KeePassPHP here, since
// we're only using low-level API.
$offset = $command == "kdbx-pwd" ? 1 : 0;
if($count < 4 + $offset)
usageAndDie();
$file = $argv[2 + $offset];
$pwd = $argv[3 + $offset];
$keyfile = $count >= 5 + $offset ? $argv[4 + $offset] : null;
$ckey = KeePassPHP::masterKey();
KeePassPHP::addPassword($ckey, $pwd);
if(!KeePassPHP::addKeyFile($ckey, $keyfile))
errorAndDie("file key parsing error.");
// The loading may take some time if your database is hard to decrypt.
$error = null;
$db = KeePassPHP::openDatabaseFile($file, $ckey, $error);
if($db == null)
errorAndDie($error);
if($command == "kdbx-pwd")
{
$r = $db->getPassword($argv[2]);
if($r == null)
echo "entry '", $argv[2], "' not found!";
else
echo $r;
}
else
visitDatabase($db);
}
else if($command == "encrypt" || $command == "decrypt")
{
if($count < 4)
usageAndDie();
$fileContent = file_get_contents($argv[2]);
if(!$fileContent)
errrorAndDie("Cannot open file '" . $argv[2] . "'");
$pwd = $argv[3];
$keyfile = $count >= 5 ? $argv[4] : null;
$ckey = KeePassPHP::masterKey();
KeePassPHP::addPassword($ckey, $pwd);
if(!KeePassPHP::addKeyFile($ckey, $keyfile))
errorAndDie("file key parsing error.");
$error = null;
$result = $command == "encrypt"
? KeePassPHP::encryptInKdbx($fileContent, $ckey, 6000, $error)
: KeePassPHP::decryptFromKdbx($fileContent, $ckey, true, $error);
if($result === null)
KPHPDebugAndDie($error);
echo $result;
}
else
{
echo "\nUnkown command '", $command, "'.\n";
usageAndDie();
}
?>