summaryrefslogtreecommitdiffstats
path: root/server/api/registration.js
blob: 4897bb50a889b57ad7ab0f46ab57fb9c87796852 (plain) (blame)
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
/* global __appdir */
var path = require('path')
var express = require('express')
const { decorateApp } = require('@awaitjs/express')
var router = decorateApp(express.Router())
var noAuthRouter = decorateApp(express.Router())
var db = require(path.join(__appdir, 'lib', 'sequelize'))
const backendHelper = require(path.join(__appdir, 'lib', 'external-backends', 'backendhelper'))
const ipHelper = require(path.join(__appdir, 'lib', 'iphelper'))
const config = require(path.join(__appdir, 'config', 'config'))
// Ipxe needs the url without the port because ipxe can't handle port requests
const url = config.https.host // + ':' + config.https.port
const log = require(path.join(__appdir, 'lib', 'log'))
const HttpResponse = require(path.join(__appdir, 'lib', 'httpresponse'))
// This is needed for parsing vendor/product codes
const pci = require(path.join(__appdir, 'lib', 'pci'))

// Permission check middleware
router.all(['', '/hooks', '/:y', '/hooks/:x'], async (req, res, next) => {
  switch (req.method) {
    case 'GET':
      if (!await req.user.hasPermission('registration.view')) return res.status(403).send({ error: 'Missing permission', permission: 'registration.view' })
      break

    case 'POST': case 'DELETE':
      if (!await req.user.hasPermission('registration.edit')) return res.status(403).send({ error: 'Missing permission', permission: 'registration.edit' })
      break

    default:
      return res.status(400).send()
  }

  next()
})

// GET requests.

/*
 * Returns all registration hooks sorted by sortValue.
 *
 * @return: List of registration hooks
 */
router.get('/hooks', (req, res) => {
  db.registrationhook.findAll({ order: [ ['sortValue', 'ASC'] ], include: [{ model: db.group, as: 'groups', attributes: ['id', 'name'] }] }).then(hooks => {
    res.send(hooks)
  })
})

// POST requests.

/*
 * Reorders the registration hooks based on an array of hook ids.
 */
router.postAsync('/hookorder', async (req, res) => {
  var idSortvalueMap = {}
  req.body.ids.forEach((id, index) => {
    idSortvalueMap[id] = index
  })
  var hooks = await db.registrationhook.findAll()
  const oldOrder = hooks.sort((a, b) => (a.sortvalue > b.sortvalue))
  var promises = []
  hooks.forEach(hook => {
    promises.push(hook.update({ sortvalue: idSortvalueMap[hook.id] }))
  })
  await Promise.all(promises)
  log({
    category: 'REGISTATIONHOOK_EDIT_ORDER',
    description: 'Registration hook order successfully edited.\n' +
                 'Old-Order: ' + '\n\t' + oldOrder.map(x => { return '[' + x.id + '] ' + x.name }).toString().replace(/,/g, '\n\t') + '\n' +
                 'New-Order: ' + '\n\t' + req.body.ids.map(x => { return '[' + x + '] ' + oldOrder.filter(y => y.id === x)[0].name }).toString().replace(/,/g, '\n\t')
  })

  res.end()
})

router.postAsync(['/hooks', '/hooks/:id'], async (req, res) => {
  var item = {
    name: req.body.name,
    description: req.body.description,
    type: req.body.type,
    script: req.body.script
  }
  var hook = null
  if (req.params.id > 0) {
    hook = await db.registrationhook.findOne({ where: { id: req.params.id } })
    if (hook) {
      await hook.update(item)
      log({
        category: 'REGISTATIONHOOK_EDIT',
        description: '[' + hook.id + '] ' + hook.name + ': Registration hook successfully edited.\n' +
                     'ID: ' + hook.id + '\n' +
                     'Name: ' + hook.name + '\n' +
                     'Type: ' + hook.type + '\n' +
                     'Description: ' + hook.description + '\n' +
                     'Sortvalue: ' + hook.sortvalue + '\n' +
                     'Groups: ' + req.body.groups,
        userId: req.user.id
      })
    }
  } else {
    var maxSortvalue = await db.registrationhook.max('sortvalue')
    item.sortvalue = maxSortvalue ? maxSortvalue + 1 : 1
    hook = await db.registrationhook.create(item)
    log({
      category: 'REGISTATIONHOOK_CREATE',
      description: '[' + hook.id + '] ' + hook.name + ': Registration hook successfully created.\n' +
                   'ID: ' + hook.id + '\n' +
                   'Name: ' + hook.name + '\n' +
                   'Type: ' + hook.type + '\n' +
                   'Description: ' + hook.description + '\n' +
                   'Sortvalue: ' + hook.sortvalue + '\n' +
                   'Groups: ' + req.body.groups,
      userId: req.user.id
    })
  }
  if (hook) {
    hook.setGroups(req.body.groups)
    res.send({ id: hook.id })
  }
  res.end()
})

// ############################################################################
// ##########################  DELETE requests  ###############################

router.deleteAsync('/hooks/:id', async (req, res) => {
  if (!(req.params.id > 0)) return HttpResponse.invalidId().send(res)
  const hook = await db.registrationhook.findOne({ where: { id: req.params.id } })
  const count = await db.registrationhook.destroy({ where: { id: req.params.id } })

  if (count) {
    log({
      category: 'REGISTATIONHOOK_DELETE',
      description: 'Registration hook successfully deleted.\n' +
                   'ID: ' + hook.id + '\n' +
                   'Name: ' + hook.name + '\n' +
                   'Type: ' + hook.type + '\n' +
                   'Sortvalue: ' + hook.sortvalue,
      userId: req.user.id
    })
    HttpResponse.success('deleted', 'hook', req.params.id).send(res)
  } else HttpResponse.notFound(req.params.id).send(res)
})

// ############################################################################
// ############################################################################

module.exports.router = router

// GET requests.

// POST requests.

/*
 * Returns all root parents or all childs of a group.
 *
 * @return: Ipxe menu with a list of groups.
 */
noAuthRouter.post('/group', (req, res) => {
  const id = req.body.id
  var parents = []
  if (req.body.parents) parents = JSON.parse(req.body.parents)
  if (id === '0') {
    db.group.findAll({ where: { '$parents.id$': null }, include: ['parents'] }).then(groups => {
      if (groups) {
        res.send(buildIpxeMenu(id, 'Root', groups, []))
      } else {
        res.status(404).end()
      }
    })
  } else {
    db.group.findOne({ where: { id: id }, include: ['parents', 'subgroups', 'clients'] }).then(group => {
      if (group) {
        res.send(buildIpxeMenu(id, group.name, group.subgroups, parents))
      } else {
        res.status(404).end()
      }
    })
  }
})

/*
 * Add method for adding a client or server.
 */
noAuthRouter.postAsync('/clients', async (req, res) => {
  let client = req.body.client
  if (typeof client === 'string') client = JSON.parse(client)
  let ipxe = req.body.ipxe
  if (typeof ipxe === 'string' && ipxe === 'true') ipxe = true
  let automatic = req.body.automatic
  if (typeof automatic === 'string' && automatic === 'true') automatic = true
  let confirmation = req.body.confirmation
  if (typeof confirmation === 'string' && confirmation === 'true') confirmation = true

  // If the client already exists return the configloader ipxe script.
  const clientDb = await db.client.findOne({ where: { uuid: client.uuid } })
  if (clientDb) {
    if (ipxe) return res.send(`#!ipxe\nchain https://` + url + `/api/configloader/\${uuid}`)
    else return res.send({ error: 'CLIENT_ALREADY_EXISTS', msg: 'A client with the provided UUID does already exist.' })
  }

  if (!client.type) client.type = 'CLIENT'

  // DHCP network stuff:

  // TODO: Multiip / backend problems
  // * Multiple backends possible ? multiple dhcp's? if set ip differentiates which one should we save in the bas db?
  // * Only servers have multiple ips? No multi leased ips possible?

  // If there is no ip, we don't need DHCP checks.
  // Only the first ip address is checked! client.networks[0]
  let dhcp = false
  if (client.networks.length >= 1 && client.type === 'CLIENT') {
    const network = client.networks[0]
    // Get the dhcp backend. Only one dhcp backend can exist else -> conflict.
    dhcp = await backendHelper.getDhcp()
    let ipSelection = false
    let setIpError
    if (dhcp) {
      if (automatic) {
        // Set the name of the client if it's not set
        if (!client.name) {
          // Check the ip state in the dhcp
          const ipCheck = await dhcp.instance.checkIp(dhcp.backend.credentials, network.ip)

          // If it's not leased, set the hostname as clientname
          if (!ipCheck.error && !ipCheck.leased && ipCheck.name !== '') {
            if (ipCheck.name) client.name = ipCheck.name
            if (ipCheck.id) dhcp.ref = ipCheck.id
          } else {
            client.name = client.type + '_' + client.uuid
            const setIp = await dhcp.instance.setIp(dhcp.backend.credentials, network.ip, undefined, network.mac, undefined, true)
            // Check for errors.
            if (!setIp.error) {
              dhcp.ref = setIp.ref
              // Client ip set successfully
              client.networks[0].ip = setIp.ip
            } else {
              log({
                category: 'ERROR_DHCP',
                description: `[${dhcp.backend.id}] Error setting ip ${network.ip} for mac ${network.mac}\nError: ${setIp.msg}`
              })
            }
          }
        }
      } else if (network.dhcp) {
        // If networks.dhcp is set the user already choose the ip and we have to set it now.
        if (!network.dhcp.domain) {
          // Check if there are multiple domains.
          const domainList = await dhcp.instance.checkDomain(dhcp.backend.credentials)
          if (domainList.length > 1) return res.send(buildSelectDomainIpxeMenu(client, domainList))
          else network.dhcp.domain = domainList[0]
        }
        if (!client.name) return res.send(buildNameClientIpxeMenu(client))
        if (confirmation) return res.send(buildOverviewIpxeMenu(client))

        const setIp = await dhcp.instance.setIp(dhcp.backend.credentials, network.dhcp.ip, network.dhcp.domain, network.mac, client.name)
        dhcp.ref = setIp.id
        // Check for errors.
        if (setIp.error) {
          log({
            category: 'ERROR_DHCP',
            description: `[${dhcp.backend.id}] Error setting ip ${network.ip} for mac ${network.mac}\nError: ${setIp.msg}`
          })

          // Setting the client ip failed
          ipSelection = true
          delete client.networks[0].dhcp
          delete client.name
          setIpError = setIp.msg
        } else {
          // Client ip set successfully
          client.networks[0].ip = network.dhcp.ip
          client.networks[0].hostname = client.name
          client.networks[0].domain = setIp.domain
        }
      } else {
        ipSelection = true
      }

      if (ipSelection) {
        // If not: check if the client has a leased ipv4 address.
        const ipCheck = await dhcp.instance.checkIp(dhcp.backend.credentials, network.ip)

        // Build ipxe and return
        if (ipxe && ipCheck.leased && !ipCheck.error) return res.send(buildSelectIpIpxeMenu(client, ipCheck.nextIps, setIpError))
        else if (!ipxe && ipCheck.leased && !ipCheck.error) return res.send({ client: client, ipList: ipCheck.nextIps })

        // Set the hostname as clientname if it exists and is not a leased ip.
        if (!ipCheck.leased && ipCheck.name) {
          if (ipCheck.name) client.name = ipCheck.name
          if (ipCheck.id) dhcp.ref = ipCheck.id
        } else {
          // Leased ip but no hostname? --> Maybe not waited long enough after DHCP deletion
          let date = new Date()
          const tenMin = 1000 * 60 * 10
          const fiveMin = 1000 * 60 * 5
          // Round up to the next 10-min mark for the error msg
          // === Add 5 min to the time and round to the nearest one
          const rounded = new Date(Math.round((date.getTime() + fiveMin) / tenMin) * tenMin)

          return res.send(buildNameClientIpxeMenu(client, ['Client has a fixed IP but NO hostname was found.', 'Infoblox might not be ready after client deletion.', `Wait until ${rounded.toTimeString()} or enter a name and continue anyways ...`]))
        }
      }
    } else { // End of DHCP Stuff
      if (automatic) {
        client.name = client.type + '_' + client.uuid
      }
    }
  }

  // Client does not exist.
  if (!client.parents) client.parents = []
  // TODO: Save all IPs? Maybe only primary ip?
  const createClient = { name: client.name, description: client.type, ip: client.networks[0].ip, mac: client.networks[0].mac, uuid: client.uuid }
  if (client.type === 'CLIENT') createClient.registrationState = await getNextHookScript(client.parents)
  const newClient = await db.client.create(createClient)
  client.id = newClient.id

  // Add dhcp backend mapping
  if (dhcp && dhcp.ref) dhcp.backend.addMappedClients(newClient, { through: { externalId: dhcp.ref } })

  // Add groups to the client.
  if (client.parents.length === 0) client.parents = await ipHelper.getGroups(client.networks[0].ip)
  client.parents.forEach(pid => { newClient.addGroup(pid) })
  log({
    category: 'REGISTRATION',
    description: 'Client added successfully.',
    clientId: newClient.id
  })

  // Add the client to the backends.
  const result = backendHelper.addClient(client)
  if (ipxe) return res.send(`#!ipxe\nsleep 5\nchain https://` + url + `/api/configloader/\${uuid}`)
  else return res.send(await result)
})

noAuthRouter.postAsync('/clients/:uuid', async (req, res) => {
  let client = {}
  if (req.body.version && req.body.version >= 2) {
    /* New hardware collection script */
    client = await parseHardwareInformation(req.body)
  } else {
    /* OLD SCRIPT */
    client = req.body.client
    if (typeof client === 'string') client = JSON.parse(client)

    if (client && client.ram && client.ram.modules) {
      // Add the name to the ram modules.
      for (let ram of client.ram.modules) {
        ram.name = ram.formfactor
        if (client.ram.isEcc === 'Single-bit ECC') ram.name += '-ECC'
      }
    }
  }

  const clientDb = await db.client.findOne({ where: { uuid: req.params.uuid } })
  if (!clientDb) return res.status(404).send({ error: 'CLIENT_NOT_FOUND', message: 'There is no client matching the provided uuid.' })
  if (client.name) clientDb.update({ name: client.name })
  client.id = clientDb.id

  // If domain gets updated without a hostname, set the hostname as name.
  for (let index in client.networks) {
    const network = client.networks[index]
    if (network.domain && !network.hostname) network.hostname = clientDb.name
  }

  if (client && client.system) {
    // System data. Sometime just string with whitespaces only.
    if (!/\S/.test(client.system.manufacturer)) client.system.manufacturer = 'unavailable'
    if (!/\S/.test(client.system.model)) client.system.model = 'unavailable'
    if (!/\S/.test(client.system.serialnumber)) client.system.serialnumber = 'unavailable'
  }

  const result = await backendHelper.updateClient(client)
  res.send(result)
})

/*
 * Mehtod for uploading the tpm key and stuff.
 */
noAuthRouter.putAsync('/:uuid/files', async (req, res) => {
  const client = await db.client.findOne({ where: { uuid: req.params.uuid } })
  const result = await backendHelper.uploadFiles(client.id, req.files)
  res.send(result)
})

/*
 * Open api method for setting the registration state of a given uuid.
 */
noAuthRouter.post('/:uuid/success', (req, res) => {
  const uuid = req.params.uuid
  const id = parseInt(req.body.id)
  db.client.findOne({ where: { uuid: uuid }, include: ['groups'] }).then(client => {
    // Client not found handling
    if (client === null) {
      res.status(404).send({ status: 'INVALID_UUID', error: 'There is no client with the provided UUID.' })
      return
    }

    // Check if the finished script id (id) matches the current state of the client.
    if (client.registrationState !== id) {
      res.status(400).send({ status: 'INVALID_SCRIPT', error: 'This script should not have been executed.' })
      return
    }

    // If it matches, search for the next script and update the clients registrationState.
    // Get all group id's of the client.
    var groupids = []
    client.groups.forEach(g => {
      groupids = [...groupids, g.id]
    })

    // Get the sort value of the current hook.
    db.registrationhook.findOne({ where: { id: client.registrationState } }).then(hook => {
      getNextHookScript(groupids, hook.sortvalue).then(resID => {
        // Update the client's registration state
        client.update({
          registrationState: resID
        })
        res.send({ status: 'SUCCESS' })
      })
    })
  })
})

/*
 * Returns the next bash script for the minilinux. Else empty script. (Empty = bash will make reboot)
 */
noAuthRouter.get('/:uuid/nexthook', (req, res) => {
  const uuid = req.params.uuid
  db.client.findOne({ where: { uuid: uuid } }).then(client => {
    // Return 404 if the client doesn't exist or it has no registration state.
    if (client === null || client.registrationState === null) {
      res.status(404).send()
      return
    }
    db.registrationhook.findOne({ where: { id: client.registrationState } }).then(hook => {
      if (hook.type !== 'BASH') {
        res.send()
      } else {
        res.set('id', client.registrationState)
        res.send(hook.script)
      }
    })
  })
})

module.exports.noAuthRouter = noAuthRouter

/*
 * parentIds:
 * sortvalue:
 *
 */
function getNextHookScript (groupids, sortvalue) {
  // Gets the list of all groupids inclusive the recursive parents.
  return getRecursiveParents(groupids).then(gids => {
    // Get the list of all hooks where the parent is null or those who fullfill the group dependency.
    var options = { where: { '$groups.id$': { [db.Op.or]: [null, gids] } }, include: ['groups'], order: [['sortvalue', 'ASC']] }
    if (sortvalue !== undefined) options.where.sortvalue = { [db.Op.gt]: sortvalue }
    return db.registrationhook.findAll(options).then(result => {
      var resID = null
      if (result.length >= 1) resID = result[0].id
      return resID
    })
  })
}

/*
 * groupids: Array of group ids to get the parents from.
 *
 * Returns a list of the grop ids and all recursive ids of their parents.
 */
function getRecursiveParents (groupIds) {
  var gids = []
  return db.group.findAll({ where: { id: groupIds }, include: ['parents'] }).then(groups => {
    groups.forEach(group => {
      group.parents.forEach(parent => {
        if (!groupIds.includes(parent.id) && !gids.includes(parent.id)) gids = [...gids, parent.id]
      })
    })
    if (gids.length === 0) return groupIds
    else {
      return getRecursiveParents(gids).then(r => {
        return groupIds.concat(r)
      })
    }
  })
}

/*
 * New mehthod for preparing the new json formatted hw information
 */
async function parseHardwareInformation (data) {
  let client = {
    'parents': [], // TODO:
    'type': '', // SERVER OR CLIENT
    'uuid': '',
    'networks': [], // { 'mac': '', 'ip': '' }
    'system': {
      'model': '',
      'manufacturer': '',
      'serialnumber': ''
    },
    'cpus': [],
    'ram': {
      'modules': [],
      'isEcc': false
    },
    'drives': [],
    'gpus': []
  }

  // TODO: Rack and Bay stuff

  /* DmiDecode: CPU, RAM, System Information (Serial, Model, ...) */
  if (data.dmidecode && data.dmidecode.length > 0) {
    const filter = [
      /* "BIOS Information", "OEM Strings"," Base Board Information", "Chassis Information", "System Power Supply" */
      'System Information',
      'Processor Information',
      'Memory Device',
      'Physical Memory Array'
    ]
    const filteredData = data.dmidecode.filter(x => filter.includes(x.name))
    for (let entry of filteredData) {
      switch (entry.name) {
        case 'System Information':
          client.system.model = entry.props['Product Name'].values[0]
          client.system.manufacturer = entry.props['Manufacturer'].values[0]
          client.system.serialnumber = entry.props['Serial Number'].values[0]
          client.uuid = entry.props['UUID'].values[0]
          break

        case 'Processor Information':
          client.cpus.push({
            'model': entry.props['Version'].values[0],
            'manufacturer': entry.props['Manufacturer'].values[0],
            'type': entry.props['Family'].values[0],
            'cores': entry.props['Core Count'].values[0],
            'frequency': entry.props['Current Speed'].values[0].split(' ')[0],
            'unit': entry.props['Current Speed'].values[0].split(' ')[1]
          })
          break

        case 'Memory Device':
          if (entry.props['Size'].values[0] === 'No Module Installed') break
          client.ram.modules.push({
            'capacity': entry.props['Size'].values[0].split(' ')[0],
            'unit': entry.props['Size'].values[0].split(' ')[1],
            'manufacturer': entry.props['Manufacturer'].values[0],
            'model': entry.props['Part Number'].values[0],
            'type': entry.props['Type'].values[0],
            'formfactor': entry.props['Form Factor'].values[0],
            'speed': entry.props['Speed'].values[0],
            'serialnumber': entry.props['Serial Number'].values[0]
          })
          break

        case 'Physical Memory Array':
          client.ram.isEcc = !!entry.props['Error Correction Type'].values[0].endsWith('ECC')
          break
      }
    }
  }

  /* Smartctl */
  for (let key in data.smartctl) {
    let drive = {
      'model': '',
      'family': '', // NEW
      'firmware': '',
      'serial': '', // TODO: Update this to serialnumber to unify (also need to be changed in the idoit backend then)
      'capacity': '',
      'unit': '',
      'type': '',
      'formfactor': '',
      'connection': '', // CHECK?!
      'connection_speed': '' // NEW
    }
    let units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']

    /* Figure out if it's HDD, SSD or CD/DVD-ROM */
    if (data.smartctl[key]['user_capacity']) {
      let capacity = data.smartctl[key]['user_capacity']['bytes']
      let unitcounter = 0
      while (capacity > 1000) {
        if (unitcounter + 1 <= units.length) {
          capacity = capacity / 1000
          unitcounter++
        }
      }
      drive['capacity'] = Math.round(capacity)
      drive['unit'] = units[unitcounter]

      if (data.smartctl[key]['rotation_rate']) {
        if (data.smartctl[key]['rotation_rate'] > 0) {
          drive['type'] = 'HDD'
        } else if (data.smartctl[key]['rotation_rate'] === 0) {
          drive['type'] = 'SSD'
        }
      }
    } else {
      const regexCDDVD = /\/dev\/sr[0-9]+/
      // Seems to be a CD/DVD-ROM
      if (data.smartctl[key]['readlink'].match(regexCDDVD)) drive['type'] = 'CD/DVD-ROM'
      else drive['type'] = 'UNKNOWN'
    }

    if (data.smartctl[key]['form_factor']) drive['formfactor'] = data.smartctl[key]['form_factor'].name
    if (data.smartctl[key]['sata_version']) drive['connection'] = data.smartctl[key]['sata_version'].string
    if (data.smartctl[key]['interface_speed']) {
      if (data.smartctl[key]['interface_speed'].current) drive['connection_speed'] = data.smartctl[key]['interface_speed'].current.string
      else if (data.smartctl[key]['interface_speed'].max) drive['connection_speed'] = data.smartctl[key]['interface_speed'].max.string
    }

    if (data.smartctl[key]['model_name']) drive.model = data.smartctl[key]['model_name']
    if (data.smartctl[key]['model_family']) drive.family = data.smartctl[key]['model_family']
    if (data.smartctl[key]['serial_number']) drive.serial = data.smartctl[key]['serial_number']
    if (data.smartctl[key]['firmware_version']) drive.firmware = data.smartctl[key]['firmware_version']

    client.drives.push(drive)
  }

  /* lspci */
  for (let obj of data.lspci) {
    /* GPU */
    if (obj.class === '0300') {
      const parsedPci = await pci.parseIds(obj.vendor, obj.device)
      client.gpus.push({
        'manufacturer': parsedPci.vendor.name,
        'model': parsedPci.vendor.device.name
      })
    }
  }

  /* ip */
  for (let ip of data.ip) {
    if (ip['link_type'] === 'loopback') continue
    let network = {
      'name': ip.ifname,
      'mac': ip.address,
      'ip': '',
      'ipv6': '',
      'hostname': ''
    }

    for (let addr of ip['addr_info']) {
      if (addr.scope !== 'global') continue
      if (addr.family === 'inet') network.ip = addr.local
      else if (addr.family === 'inet6') network.ipv6 = addr.local
    }

    client.networks.push(network)
  }

  /* net */
  /* Get network information as fallback for ip */
  if (client.networks.length <= 0) {
    for (let key in data.net) {
      let network = {
        'name': key,
        'mac': data.net[key]['mac'],
        'ip': data.net[key]['ipv4'],
        'ipv6': data.net[key]['ipv6'],
        'hostname': ''
      }
      client.networks.push(network)
    }
  }

  /* edid */

  /* lshw */
  if (data.lshw && data.lshw.length > 0) {
    /* Get display information (as fallback) */
    if (client.gpus && client.gpus.length === 0) {
      const gpus = data.lshw[0].children.filter(y => y.id === 'core')[0].children.filter(z => z.id === 'pci')[0].children.filter(w => w.id === 'display')
      for (let gpu of gpus) {
        client.gpus.push({
          'manufacturer': gpu.vendor,
          'model': gpu.product
          /*,
          'memory': ,
          'unit':
          */
        })
      }
    }
  }

  return client
}

/*
 * id: id of the current selected location.
 * name: Name of the current selected location
 * groups: List of group [{ id: <GROUP_ID>, name: <GROUP_NAME> }, ...]
 *
 * Build the ipxe menu out of the list of groups.
 * Used by the manual registration.
 */
function buildIpxeMenu (id, name, groups, parents) {
  var basUrl = 'https://' + url
  var script = '#!ipxe\r\n'
  // script = script.concat(`console --picture \${img} --x 800 --y 600 || shell\r\n`)

  // Add parent to keep track of the path we clicked through.
  var parentId = 0
  // Cheap way to duplicate the array:
  var oldParents = parents.slice(0)
  if (parents.length > 0) {
    parentId = oldParents[oldParents.length - 1].id
    oldParents.length = oldParents.length - 1
  }
  parents.push({ id: id, name: toAscii(name) })
  script += `set space:hex 20:20\r\n`
  script += `set space \${space:string}\r\n`
  script += `set parents ` + JSON.stringify(parents) + '\r\n\r\n'

  // Menu
  var menuscript = ''
  script += ':start\r\n'
  script += 'menu Choose the group you want the client to be saved in\r\n'

  // Parent menu entries.
  var spacer = ''
  parents.forEach(parent => {
    script += 'item --gap ' + spacer + '[' + parent.id + '] ' + parent.name + '\r\n'
    spacer += `\${space}`
  })

  // Back button
  script += 'item --key b back ' + spacer + '..\r\n'
  menuscript += ':back\r\nparams\r\nparam id ' + parentId + '\r\nparam parents ' + JSON.stringify(oldParents) + '\r\n'
  menuscript += 'chain --replace ' + basUrl + '/api/registration/group##params\r\n\r\n'

  // Group menu entries. First 1-9 are pressable via key?
  var counter = '1'
  groups.forEach(group => {
    script += 'item --key ' + counter + ' ' + counter + ' ' + spacer + '[' + group.id + '] ' + toAscii(group.name) + '\r\n'
    menuscript += ':' + counter + '\r\n' + 'params\r\nparam id ' + group.id + `\r\nparam parents \${parents}\r\n`
    menuscript += 'chain --replace ' + basUrl + '/api/registration/group##params\r\n\r\n'
    counter++
  })

  // Menu seperator
  script += 'item --gap\r\n'

  // Add client menu
  script += 'item select Add client to ' + toAscii(name) + '\r\n'
  menuscript += `:select\r\necho Enter client name\r\nread clientname\r\nparams\r\n`
  menuscript += `param client { "name": "\${clientname}", "type": "CLIENT", "uuid": "\${uuid}", "purpose": "Pool PC", "parents": [` + parents[parents.length - 1].id + `], "networks": [{ "ip": "\${net0/ip}", "mac": "\${net0/mac}" }] }\r\n`
  menuscript += 'param ipxe true\r\n'
  menuscript += 'chain --replace ' + basUrl + '/api/registration/clients##params\r\n\r\n'

  // Goto start menu
  if (id !== '0') {
    script += 'item reset Go to start\r\n'
    menuscript += ':reset\r\nparams\r\nparam id ' + 0 + '\r\nchain --replace ' + basUrl + '/api/registration/group##params\r\n\r\n'
  }

  // Exit menu
  script += 'item exit Exit manual registration\r\n'
  menuscript += ':exit\r\nexit 1\r\n\r\n'

  // Concat script + menuscript and return it.
  script += `choose target && goto \${target}\r\n\r\n`
  script += menuscript
  return script
}

function buildSelectIpIpxeMenu (client, ipList, error = undefined) {
  const basUrl = 'https://' + url
  let script = '#!ipxe\r\n'
  if (error) script += 'echo\r\necho ' + error + '\r\necho\r\nprompt Press any key to select a new ip address\r\n'

  let menuscript = ''
  script += ':start\r\n'
  script += 'menu Select the ip for this client: \r\n'
  for (let ip of ipList) {
    client.networks[0].dhcp = { ip: ip }
    script += 'item ' + ip + ' ' + ip + '\r\n'
    menuscript += ':' + ip + '\r\n' + 'params\r\nparam client ' + JSON.stringify(client) + '\r\n'
    menuscript += 'param ipxe true\r\n'
    menuscript += 'chain --replace ' + basUrl + '/api/registration/clients##params\r\n\r\n'
  }
  script += `choose target && goto \${target}\r\n\r\n`
  script += menuscript
  return script
}

function buildSelectDomainIpxeMenu (client, domainList) {
  const basUrl = 'https://' + url
  let script = '#!ipxe\r\n'

  let menuscript = ''
  script += ':start\r\n'
  script += 'menu Select the domain for this client: \r\n'
  for (let index in domainList) {
    const domain = domainList[index]
    client.networks[0].dhcp.domain = domain
    script += 'item ' + domain + ' ' + domain + '\r\n'
    menuscript += ':' + domain + '\r\n' + 'params\r\nparam client ' + JSON.stringify(client) + '\r\n'
    menuscript += 'param ipxe true\r\n'
    menuscript += 'chain --replace ' + basUrl + '/api/registration/clients##params\r\n\r\n'
  }
  script += `choose target && goto \${target}\r\n\r\n`
  script += menuscript
  return script
}

function buildNameClientIpxeMenu (client, showErrorLines = []) {
  const basUrl = 'https://' + url
  let script = '#!ipxe\r\n'

  for (let line of showErrorLines) {
    script += `echo ${line}\r\n`
  }

  script += '\r\necho Enter client name\r\nread clientname\r\nparams\r\n'
  client.name = `\${clientname}`
  script += 'param client ' + JSON.stringify(client) + '\r\n'
  script += 'param ipxe true\r\n'
  // Trigger the overview ipxe menu
  script += 'param confirmation true\r\n'
  script += 'chain --replace ' + basUrl + '/api/registration/clients##params\r\n\r\n'
  return script
}

function buildOverviewIpxeMenu (client) {
  const basUrl = 'https://' + url
  const c = JSON.stringify(client)
  let script = '#!ipxe\r\n'
  script += ':start\r\n'
  script += 'menu Overview Register Client\r\n'
  script += 'item --gap Name: ' + client.name + '\r\n'
  delete client.name
  if (client.networks[0].dhcp) {
    if (client.networks[0].dhcp.ip) script += 'item --gap New IP: ' + client.networks[0].dhcp.ip + '\r\n'
    if (client.networks[0].dhcp.domain) script += 'item --gap Domain: ' + client.networks[0].dhcp.domain + '\r\n'
    delete client.networks[0].dhcp
  }

  script += 'item --gap\r\n'
  script += 'item --gap Current IP: ' + client.networks[0].ip + '\r\n'
  script += 'item --gap MAC: ' + client.networks[0].mac + '\r\n'
  script += 'item --gap UUID: ' + client.uuid + '\r\n'

  for (let index = 1; index < client.networks.length; index++) {
    // Only handle the networks with index >= 1
    const network = client.networks[index]
    script += 'item --gap\r\n'
    script += 'item --gap Current IP: ' + network.ip + '\r\n'
    if (network.dhcp && network.dhcp.ip) script += 'item --gap New IP: ' + network.dhcp.ip + '\r\n'
    if (network.dhcp && network.dhcp.domain) script += 'item --gap Domain: ' + network.dhcp.domain + '\r\n'
    script += 'item --gap MAC: ' + network.mac + '\r\n'
    if (network.dhcp) delete network.dhcp
  }

  script += 'item --gap\r\n'
  script += 'item default\r\n'
  script += 'item --key y confirm Confirm [y]es\r\n'
  script += 'item --key n cancel Cancel [n]o\r\n'
  script += `choose --default default target && goto \${target}\r\n\r\n`

  script += ':default\r\n'
  script += 'goto start\r\n\r\n'

  script += ':confirm\r\n'
  script += 'params\r\nparam client ' + c + '\r\n'
  script += 'param ipxe true\r\n'
  script += 'chain --replace ' + basUrl + '/api/registration/clients##params\r\n\r\n'

  script += ':cancel\r\n'
  script += 'params\r\nparam client ' + JSON.stringify(client) + '\r\n'
  script += 'param ipxe true\r\n'
  script += 'chain --replace ' + basUrl + '/api/registration/clients##params\r\n\r\n'
  return script
}

function toAscii (string) {
  string = string.replace('ü', 'ue')
  string = string.replace('ö', 'oe')
  string = string.replace('ä', 'ae')
  return ascii(string)
}

/* eslint-disable */
var escapable = /[\\"\x00-\x1f\x7f-\uffff]/g
/* eslint-enable */
var meta = { // table of character substitutions
  '\b': '\\b',
  '\t': '\\t',
  '\n': '\\n',
  '\f': '\\f',
  '\r': '\\r',
  '"': '\\"',
  '\\': '\\\\'
}

function ascii (string) {
// If the string contains no control characters, no quote characters, and no
// backslash characters, then we can safely slap some quotes around it.
// Otherwise we must also replace the offending characters with safe escape
// sequences.

  escapable.lastIndex = 0
  return escapable.test(string)
    ? string.replace(escapable, function (a) {
      var c = meta[a]
      return typeof c === 'string' ? c
        : '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4)
    }) : string
}