|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* sysPass |
|
4
|
|
|
* |
|
5
|
|
|
* @author nuxsmin |
|
6
|
|
|
* @link https://syspass.org |
|
7
|
|
|
* @copyright 2012-2019, Rubén Domínguez nuxsmin@$syspass.org |
|
8
|
|
|
* |
|
9
|
|
|
* This file is part of sysPass. |
|
10
|
|
|
* |
|
11
|
|
|
* sysPass is free software: you can redistribute it and/or modify |
|
12
|
|
|
* it under the terms of the GNU General Public License as published by |
|
13
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
14
|
|
|
* (at your option) any later version. |
|
15
|
|
|
* |
|
16
|
|
|
* sysPass is distributed in the hope that it will be useful, |
|
17
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
19
|
|
|
* GNU General Public License for more details. |
|
20
|
|
|
* |
|
21
|
|
|
* You should have received a copy of the GNU General Public License |
|
22
|
|
|
* along with sysPass. If not, see <http://www.gnu.org/licenses/>. |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace SP\Services\Import; |
|
26
|
|
|
|
|
27
|
|
|
use Defuse\Crypto\Exception\CryptoException; |
|
28
|
|
|
use DOMDocument; |
|
29
|
|
|
use DOMElement; |
|
30
|
|
|
use DOMNodeList; |
|
31
|
|
|
use DOMXPath; |
|
32
|
|
|
use Exception; |
|
33
|
|
|
use SP\Core\Crypt\Crypt; |
|
34
|
|
|
use SP\Core\Crypt\Hash; |
|
35
|
|
|
use SP\Core\Crypt\OldCrypt; |
|
36
|
|
|
use SP\Core\Events\Event; |
|
37
|
|
|
use SP\Core\Events\EventMessage; |
|
38
|
|
|
use SP\DataModel\CategoryData; |
|
39
|
|
|
use SP\DataModel\ClientData; |
|
40
|
|
|
use SP\DataModel\TagData; |
|
41
|
|
|
use SP\Services\Account\AccountRequest; |
|
42
|
|
|
use SP\Services\Export\XmlVerifyService; |
|
43
|
|
|
use SP\Util\VersionUtil; |
|
44
|
|
|
|
|
45
|
|
|
defined('APP_ROOT') || die(); |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Esta clase es la encargada de importar cuentas desde sysPass |
|
49
|
|
|
*/ |
|
50
|
|
|
final class SyspassImport extends XmlImportBase implements ImportInterface |
|
51
|
|
|
{ |
|
52
|
|
|
/** |
|
53
|
|
|
* Iniciar la importación desde sysPass. |
|
54
|
|
|
* |
|
55
|
|
|
* @return ImportInterface |
|
56
|
|
|
* @throws ImportException |
|
57
|
|
|
*/ |
|
58
|
|
|
public function doImport() |
|
59
|
|
|
{ |
|
60
|
|
|
try { |
|
61
|
|
|
$this->eventDispatcher->notifyEvent('run.import.syspass', |
|
62
|
|
|
new Event($this, EventMessage::factory() |
|
63
|
|
|
->addDescription(__u('sysPass XML Import'))) |
|
64
|
|
|
); |
|
65
|
|
|
|
|
66
|
|
|
if ($this->importParams->getImportMasterPwd() !== '') { |
|
67
|
|
|
$this->mPassValidHash = Hash::checkHashKey($this->importParams->getImportMasterPwd(), $this->configService->getByParam('masterPwd')); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$this->version = $this->getXmlVersion(); |
|
71
|
|
|
|
|
72
|
|
|
if ($this->detectEncrypted()) { |
|
73
|
|
|
if ($this->importParams->getImportPwd() === '') { |
|
74
|
|
|
throw new ImportException(__u('Encryption password not set'), ImportException::INFO); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$this->processEncrypted(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$this->checkIntegrity(); |
|
81
|
|
|
|
|
82
|
|
|
$this->processCategories(); |
|
83
|
|
|
|
|
84
|
|
|
if ($this->version >= VersionUtil::versionToInteger('3.0.0')) { |
|
85
|
|
|
$this->processClients(); |
|
86
|
|
|
} else { |
|
87
|
|
|
$this->processCustomers(); |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$this->processTags(); |
|
91
|
|
|
$this->processAccounts(); |
|
92
|
|
|
|
|
93
|
|
|
return $this; |
|
94
|
|
|
} catch (ImportException $e) { |
|
95
|
|
|
throw $e; |
|
96
|
|
|
} catch (Exception $e) { |
|
97
|
|
|
throw new ImportException($e->getMessage(), ImportException::CRITICAL); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Obtener la versión del XML |
|
103
|
|
|
*/ |
|
104
|
|
|
protected function getXmlVersion() |
|
105
|
|
|
{ |
|
106
|
|
|
return VersionUtil::versionToInteger((new DOMXPath($this->xmlDOM))->query('/Root/Meta/Version')->item(0)->nodeValue); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Verificar si existen datos encriptados |
|
111
|
|
|
* |
|
112
|
|
|
* @return bool |
|
113
|
|
|
*/ |
|
114
|
|
|
protected function detectEncrypted() |
|
115
|
|
|
{ |
|
116
|
|
|
return ($this->xmlDOM->getElementsByTagName('Encrypted')->length > 0); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Procesar los datos encriptados y añadirlos al árbol DOM desencriptados |
|
121
|
|
|
* |
|
122
|
|
|
* @throws ImportException |
|
123
|
|
|
*/ |
|
124
|
|
|
protected function processEncrypted() |
|
125
|
|
|
{ |
|
126
|
|
|
$hash = $this->xmlDOM->getElementsByTagName('Encrypted')->item(0)->getAttribute('hash'); |
|
127
|
|
|
|
|
128
|
|
|
if (!empty($hash) && !Hash::checkHashKey($this->importParams->getImportPwd(), $hash)) { |
|
129
|
|
|
throw new ImportException(__u('Wrong encryption password')); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
foreach ($this->xmlDOM->getElementsByTagName('Data') as $node) { |
|
133
|
|
|
/** @var $node DOMElement */ |
|
134
|
|
|
$data = base64_decode($node->nodeValue); |
|
135
|
|
|
|
|
136
|
|
|
try { |
|
137
|
|
|
if ($this->version >= 210) { |
|
138
|
|
|
$xmlDecrypted = Crypt::decrypt($data, |
|
139
|
|
|
$node->getAttribute('key'), |
|
140
|
|
|
$this->importParams->getImportPwd()); |
|
141
|
|
|
} else { |
|
142
|
|
|
$xmlDecrypted = OldCrypt::getDecrypt($data, |
|
143
|
|
|
base64_decode($node->getAttribute('iv')), |
|
144
|
|
|
$this->importParams->getImportPwd()); |
|
145
|
|
|
} |
|
146
|
|
|
} catch (CryptoException $e) { |
|
147
|
|
|
processException($e); |
|
148
|
|
|
|
|
149
|
|
|
$this->eventDispatcher->notifyEvent('exception', new Event($e)); |
|
150
|
|
|
|
|
151
|
|
|
continue; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
$newXmlData = new DOMDocument(); |
|
155
|
|
|
|
|
156
|
|
|
if ($newXmlData->loadXML($xmlDecrypted) === false) { |
|
157
|
|
|
throw new ImportException(__u('Wrong encryption password')); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
$this->xmlDOM->documentElement->appendChild($this->xmlDOM->importNode($newXmlData->documentElement, TRUE)); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
// Eliminar los datos encriptados tras desencriptar los mismos |
|
164
|
|
|
if ($this->xmlDOM->getElementsByTagName('Data')->length > 0) { |
|
165
|
|
|
$nodeData = $this->xmlDOM->getElementsByTagName('Encrypted')->item(0); |
|
166
|
|
|
$nodeData->parentNode->removeChild($nodeData); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
$this->eventDispatcher->notifyEvent('run.import.syspass.process.decryption', |
|
170
|
|
|
new Event($this, EventMessage::factory() |
|
171
|
|
|
->addDescription(__u('Data unencrypted'))) |
|
172
|
|
|
); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Checks XML file's data integrity using the signed hash |
|
177
|
|
|
*/ |
|
178
|
|
|
protected function checkIntegrity() |
|
179
|
|
|
{ |
|
180
|
|
|
$key = $this->importParams->getImportPwd() ?: sha1($this->configData->getPasswordSalt()); |
|
181
|
|
|
|
|
182
|
|
|
if (!XmlVerifyService::checkXmlHash($this->xmlDOM, $key)) { |
|
183
|
|
|
$this->eventDispatcher->notifyEvent('run.import.syspass.process.verify', |
|
184
|
|
|
new Event($this, EventMessage::factory() |
|
185
|
|
|
->addDescription(__u('Error while checking integrity hash')) |
|
186
|
|
|
->addDescription(__u('If you are importing an exported file from the same origin, the data could be compromised.'))) |
|
187
|
|
|
); |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Obtener las categorías y añadirlas a sysPass. |
|
193
|
|
|
* |
|
194
|
|
|
* @throws ImportException |
|
195
|
|
|
*/ |
|
196
|
|
|
protected function processCategories() |
|
197
|
|
|
{ |
|
198
|
|
|
$this->getNodesData('Categories', 'Category', |
|
199
|
|
|
function (DOMElement $category) { |
|
200
|
|
|
$categoryData = new CategoryData(); |
|
201
|
|
|
|
|
202
|
|
|
foreach ($category->childNodes as $node) { |
|
203
|
|
|
if (isset($node->tagName)) { |
|
204
|
|
|
switch ($node->tagName) { |
|
205
|
|
|
case 'name': |
|
206
|
|
|
$categoryData->setName($node->nodeValue); |
|
207
|
|
|
break; |
|
208
|
|
|
case 'description': |
|
209
|
|
|
$categoryData->setDescription($node->nodeValue); |
|
210
|
|
|
break; |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
try { |
|
216
|
|
|
$this->addWorkingItem('category', (int)$category->getAttribute('id'), $this->addCategory($categoryData)); |
|
217
|
|
|
|
|
218
|
|
|
$this->eventDispatcher->notifyEvent('run.import.syspass.process.category', |
|
219
|
|
|
new Event($this, EventMessage::factory() |
|
220
|
|
|
->addDetail(__u('Category imported'), $categoryData->getName())) |
|
221
|
|
|
); |
|
222
|
|
|
} catch (Exception $e) { |
|
223
|
|
|
processException($e); |
|
224
|
|
|
|
|
225
|
|
|
$this->eventDispatcher->notifyEvent('exception', new Event($e)); |
|
226
|
|
|
} |
|
227
|
|
|
}); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Obtener los clientes y añadirlos a sysPass. |
|
232
|
|
|
* |
|
233
|
|
|
* @throws ImportException |
|
234
|
|
|
*/ |
|
235
|
|
|
protected function processClients() |
|
236
|
|
|
{ |
|
237
|
|
|
$this->getNodesData('Clients', 'Client', |
|
238
|
|
|
function (DOMElement $client) { |
|
239
|
|
|
$clientData = new ClientData(); |
|
240
|
|
|
|
|
241
|
|
|
foreach ($client->childNodes as $node) { |
|
242
|
|
|
if (isset($node->tagName)) { |
|
243
|
|
|
switch ($node->tagName) { |
|
244
|
|
|
case 'name': |
|
245
|
|
|
$clientData->setName($node->nodeValue); |
|
246
|
|
|
break; |
|
247
|
|
|
case 'description': |
|
248
|
|
|
$clientData->setDescription($node->nodeValue); |
|
249
|
|
|
break; |
|
250
|
|
|
} |
|
251
|
|
|
} |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
try { |
|
255
|
|
|
$this->addWorkingItem('client', (int)$client->getAttribute('id'), $this->addClient($clientData)); |
|
256
|
|
|
|
|
257
|
|
|
$this->eventDispatcher->notifyEvent('run.import.syspass.process.client', |
|
258
|
|
|
new Event($this, EventMessage::factory() |
|
259
|
|
|
->addDetail(__u('Client imported'), $clientData->getName())) |
|
260
|
|
|
); |
|
261
|
|
|
} catch (Exception $e) { |
|
262
|
|
|
processException($e); |
|
263
|
|
|
|
|
264
|
|
|
$this->eventDispatcher->notifyEvent('exception', new Event($e)); |
|
265
|
|
|
} |
|
266
|
|
|
}); |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* Obtener los clientes y añadirlos a sysPass. |
|
271
|
|
|
* |
|
272
|
|
|
* @throws ImportException |
|
273
|
|
|
* @deprecated |
|
274
|
|
|
*/ |
|
275
|
|
|
protected function processCustomers() |
|
276
|
|
|
{ |
|
277
|
|
|
$this->getNodesData('Customers', 'Customer', |
|
278
|
|
|
function (DOMElement $client) { |
|
279
|
|
|
$clientData = new ClientData(); |
|
280
|
|
|
|
|
281
|
|
|
foreach ($client->childNodes as $node) { |
|
282
|
|
|
if (isset($node->tagName)) { |
|
283
|
|
|
switch ($node->tagName) { |
|
284
|
|
|
case 'name': |
|
285
|
|
|
$clientData->setName($node->nodeValue); |
|
286
|
|
|
break; |
|
287
|
|
|
case 'description': |
|
288
|
|
|
$clientData->setDescription($node->nodeValue); |
|
289
|
|
|
break; |
|
290
|
|
|
} |
|
291
|
|
|
} |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
try { |
|
295
|
|
|
$this->addWorkingItem('client', (int)$client->getAttribute('id'), $this->addClient($clientData)); |
|
296
|
|
|
|
|
297
|
|
|
$this->eventDispatcher->notifyEvent('run.import.syspass.process.customer', |
|
298
|
|
|
new Event($this, EventMessage::factory() |
|
299
|
|
|
->addDetail(__u('Client imported'), $clientData->getName())) |
|
300
|
|
|
); |
|
301
|
|
|
} catch (Exception $e) { |
|
302
|
|
|
processException($e); |
|
303
|
|
|
|
|
304
|
|
|
$this->eventDispatcher->notifyEvent('exception', new Event($e)); |
|
305
|
|
|
} |
|
306
|
|
|
}); |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
/** |
|
310
|
|
|
* Obtener las etiquetas y añadirlas a sysPass. |
|
311
|
|
|
* |
|
312
|
|
|
* @throws ImportException |
|
313
|
|
|
*/ |
|
314
|
|
|
protected function processTags() |
|
315
|
|
|
{ |
|
316
|
|
|
$this->getNodesData('Tags', 'Tag', |
|
317
|
|
|
function (DOMElement $tag) { |
|
318
|
|
|
$tagData = new TagData(); |
|
319
|
|
|
|
|
320
|
|
|
foreach ($tag->childNodes as $node) { |
|
321
|
|
|
if (isset($node->tagName)) { |
|
322
|
|
|
switch ($node->tagName) { |
|
323
|
|
|
case 'name': |
|
324
|
|
|
$tagData->setName($node->nodeValue); |
|
325
|
|
|
break; |
|
326
|
|
|
} |
|
327
|
|
|
} |
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
|
|
try { |
|
331
|
|
|
$this->addWorkingItem('tag', (int)$tag->getAttribute('id'), $this->addTag($tagData)); |
|
332
|
|
|
|
|
333
|
|
|
$this->eventDispatcher->notifyEvent('run.import.syspass.process.tag', |
|
334
|
|
|
new Event($this, EventMessage::factory() |
|
335
|
|
|
->addDetail(__u('Tag imported'), $tagData->getName())) |
|
336
|
|
|
); |
|
337
|
|
|
} catch (Exception $e) { |
|
338
|
|
|
processException($e); |
|
339
|
|
|
|
|
340
|
|
|
$this->eventDispatcher->notifyEvent('exception', new Event($e)); |
|
341
|
|
|
} |
|
342
|
|
|
}, false); |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
|
|
/** |
|
346
|
|
|
* Obtener los datos de las cuentas de sysPass y crearlas. |
|
347
|
|
|
* |
|
348
|
|
|
* @throws ImportException |
|
349
|
|
|
*/ |
|
350
|
|
|
protected function processAccounts() |
|
351
|
|
|
{ |
|
352
|
|
|
$this->getNodesData('Accounts', 'Account', |
|
353
|
|
|
function (DOMElement $account) { |
|
354
|
|
|
$accountRequest = new AccountRequest(); |
|
355
|
|
|
|
|
356
|
|
|
/** @var DOMElement $node */ |
|
357
|
|
|
foreach ($account->childNodes as $node) { |
|
358
|
|
|
if (isset($node->tagName)) { |
|
359
|
|
|
switch ($node->tagName) { |
|
360
|
|
|
case 'name'; |
|
361
|
|
|
$accountRequest->name = $node->nodeValue; |
|
362
|
|
|
break; |
|
363
|
|
|
case 'login'; |
|
364
|
|
|
$accountRequest->login = $node->nodeValue; |
|
365
|
|
|
break; |
|
366
|
|
|
case 'categoryId'; |
|
367
|
|
|
$accountRequest->categoryId = $this->getWorkingItem('category', (int)$node->nodeValue); |
|
368
|
|
|
break; |
|
369
|
|
|
case 'clientId'; |
|
370
|
|
|
case 'customerId'; |
|
371
|
|
|
$accountRequest->clientId = $this->getWorkingItem('client', (int)$node->nodeValue); |
|
372
|
|
|
break; |
|
373
|
|
|
case 'url'; |
|
374
|
|
|
$accountRequest->url = $node->nodeValue; |
|
375
|
|
|
break; |
|
376
|
|
|
case 'pass'; |
|
377
|
|
|
$accountRequest->pass = $node->nodeValue; |
|
378
|
|
|
break; |
|
379
|
|
|
case 'key'; |
|
380
|
|
|
$accountRequest->key = $node->nodeValue; |
|
381
|
|
|
break; |
|
382
|
|
|
case 'notes'; |
|
383
|
|
|
$accountRequest->notes = $node->nodeValue; |
|
384
|
|
|
break; |
|
385
|
|
|
case 'tags': |
|
386
|
|
|
$accountRequest->tags = $this->processAccountTags($node->childNodes); |
|
387
|
|
|
} |
|
388
|
|
|
} |
|
389
|
|
|
} |
|
390
|
|
|
|
|
391
|
|
|
try { |
|
392
|
|
|
$this->addAccount($accountRequest); |
|
393
|
|
|
|
|
394
|
|
|
$this->eventDispatcher->notifyEvent('run.import.syspass.process.account', |
|
395
|
|
|
new Event($this, EventMessage::factory() |
|
396
|
|
|
->addDetail(__u('Account imported'), $accountRequest->name)) |
|
397
|
|
|
); |
|
398
|
|
|
} catch (Exception $e) { |
|
399
|
|
|
processException($e); |
|
400
|
|
|
|
|
401
|
|
|
$this->eventDispatcher->notifyEvent('exception', new Event($e)); |
|
402
|
|
|
} |
|
403
|
|
|
}); |
|
404
|
|
|
} |
|
405
|
|
|
|
|
406
|
|
|
/** |
|
407
|
|
|
* Procesar las etiquetas de la cuenta |
|
408
|
|
|
* |
|
409
|
|
|
* @param DOMNodeList $nodes |
|
410
|
|
|
* |
|
411
|
|
|
* @return array |
|
412
|
|
|
*/ |
|
413
|
|
|
protected function processAccountTags(DOMNodeList $nodes) |
|
414
|
|
|
{ |
|
415
|
|
|
$tags = []; |
|
416
|
|
|
|
|
417
|
|
|
if ($nodes->length > 0) { |
|
418
|
|
|
/** @var DOMElement $node */ |
|
419
|
|
|
foreach ($nodes as $node) { |
|
420
|
|
|
if (isset($node->tagName)) { |
|
421
|
|
|
$tags[] = $this->getWorkingItem('tag', (int)$node->getAttribute('id')); |
|
422
|
|
|
} |
|
423
|
|
|
} |
|
424
|
|
|
} |
|
425
|
|
|
|
|
426
|
|
|
return $tags; |
|
427
|
|
|
} |
|
428
|
|
|
} |