summaryrefslogtreecommitdiffstats
path: root/server/api/registration.js
blob: a5c1b6f3544280e6d1c25e79ecd5bb1760ec66bf (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
/* global __appdir */
var path = require('path')
const Infoblox = require('infoblox')
var express = require('express')
var router = express.Router()
var noAuthRouter = express.Router()
var db = require(path.join(__appdir, 'lib', 'sequelize'))
var dhcp = require(path.join(__appdir, 'config', 'config')).dhcp

// GET requests.

/*
 * @return:
 */
router.get('/', (req, res) => {
  var ipam = new Infoblox({
    ip: 'dhcp.uni-freiburg.de',
    apiVersion: '1.7.1'
  })
  ipam.login(dhcp.user, dhcp.password).then(function (r) {
    if (r) {
      // Get the network from the ip.
      // ipam.getHost('10.21.9.78').then(function (response) {
      ipam.getNetworkFromIp('10.21.9.78').then(function (response) {
      // ipam.getIpsFromSubnet('10.21.9.0/24').then(function (response) {
      // ipam.list('record:host').then(function (response) {
      // ipam.getDomain().then(function (response) {
        response = JSON.parse(response)
        // ipam.getNext(response[0]._ref, 1).then(r => {
        res.send(response)
        // })
        // res.send(JSON.parse(response))
      })
    }
    // res.status(200).send({ status: 'work in progress ...' })
  })
})

router.get('/setdhcptest', (req, res) => {
  var ipam = new Infoblox({
    ip: 'dhcp.uni-freiburg.de',
    apiVersion: '1.7.1'
  })
  ipam.login('js603', 'Iwjp!DFhk4').then(function (r) {
    if (r) {
      ipam.create('record:host?_return_fields%2B=na13me,ipv4addrs&_return_as_object=1', {'name': 'wapiTest.lp.privat', 'ipv4addrs': [{'ipv4addr': '10.21.9.141', 'mac': 'aa:bb:cc:11:22:21'}]}).then(function (response) {
        res.send(response)
      })
    }
    // res.status(200).send({ status: 'work in progress ...' })
  })
})

module.exports.router = router

// GET requests.

// POST requests.

/*
 *
 * @return:
 */
noAuthRouter.post('/client', (req, res) => {
  // const mac = req.body.mac
  // const uuid = req.body.uuid
  // const ip = req.body.ip
  // const name = req.body.name
  // console.log(name.concat(' ', ip, ' ', mac, ' ', uuid))

  var script = '#!ipxe\r\n'
  script.concat(`console --picture \${img} --x 800 --y 600 || shell\r\n`)
  db.group.findAll({ where: { '$parents.id$': null }, include: ['parents'] }).then(groups => {
    groups.forEach(g => {
      script = script.concat('echo', ' [', g.id, '] ', g.name, '\r\n')
    })

    script = script.concat('read group\r\n')
    script = script.concat('params\r\n')
    script = script.concat(`param id \${group}\r\n`)
    script = script.concat('chain https://bas.stfu-kthx.net:8888/api/registration/group##params\r\n')
    res.status(200).send(script)
  })
})

noAuthRouter.post('/group', (req, res) => {
  const id = req.body.id
  console.log(id)
  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))
      } else {
        res.status(404).end()
      }
    })
  }
})

noAuthRouter.post('/add', (req, res) => {
  const mac = req.body.mac
  const uuid = req.body.uuid
  const ip = req.body.ip
  const name = req.body.name
  const parentId = req.body.id
  db.client.create({ name: name, ip: ip, mac: mac, uuid: uuid }).then(client => {
    client.addGroup(parentId)
    res.send('#ipxe\r\nshell')
  })
  console.log(name.concat(' ', ip, ' ', mac, ' ', uuid))
})

module.exports.noAuthRouter = noAuthRouter

function buildIpxeMenu (id, name, groups) {
  var script = '#!ipxe\r\n'
  script.concat(`console --picture \${img} --x 800 --y 600 || shell\r\n`)
  var menuscript = ''
  script = script.concat(':start\r\n')
  script = script.concat('menu Choose the group you want the client to be saved in\r\n')
  var counter = '1'
  groups.forEach(group => {
    // script = script.concat('echo', ' [', subgroup.id, '] ', subgroup.name, '\r\n')
    script = script.concat('item --key ', counter, ' ', counter, ' [', group.id, '] ', group.name, '\r\n')
    menuscript = menuscript.concat(':', counter, '\r\n', 'params\r\nparam id ', group.id, '\r\nchain https://bas.stfu-kthx.net:8888/api/registration/group##params\r\n\r\n')
    counter++
  })
  script = script.concat('item select Add client to ', name, '\r\n')
  menuscript = menuscript.concat(`:select\r\necho Enter client name\r\nread clientname\r\nparams\r\nparam name \${clientname}\r\n`)
  menuscript = menuscript.concat('param id ', id, `\r\nparam mac \${net0/mac}\r\nparam uuid \${uuid}\r\nparam ip \${net0/ip}\r\n`)
  menuscript = menuscript.concat('chain https://bas.stfu-kthx.net:8888/api/registration/add##params\r\n\r\n')
  if (id !== '0') {
    script = script.concat('item reset Go to start\r\n')
    menuscript = menuscript.concat(':reset\r\nparams\r\nparam id ', 0, '\r\nchain --replace https://bas.stfu-kthx.net:8888/api/registration/group##params\r\n\r\n')
  }
  script = script.concat(`choose target && goto \${target}\r\n\r\n`)
  script = script.concat(menuscript)
  return script
}