summaryrefslogtreecommitdiffstats
path: root/modules-available/rebootcontrol/pages/subnet.inc.php
blob: a6d8d837e135acff7bc660018a65a4189c58ef4e (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
<?php

class SubPage
{

	public static function doPreprocess()
	{
		$action = Request::post('action', false, 'string');
		if ($action === 'add') {
			self::addSubnet();
		} elseif ($action === 'edit') {
			self::editSubnet();
		} elseif ($action === 'delete') {
			self::deleteSubnet();
		}
	}

	/*
	 * POST
	 */

	private static function addSubnet()
	{
		User::assertPermission('subnet.edit');
		$cidr = Request::post('cidr', Request::REQUIRED, 'string');
		$range = IpUtil::parseCidr($cidr);
		if ($range === null) {
			Message::addError('invalid-cidr', $cidr);
			return;
		}
		$ret = Database::exec('INSERT INTO reboot_subnet (start, end, fixed, isdirect)
				VALUES (:start, :end, 1, 0)', [
			'start' => $range['start'],
			'end' => $range['end'],
			], true);
		if ($ret === false) {
			Message::addError('subnet-already-exists');
		} else {
			Message::addSuccess('subnet-created');
			Util::redirect('?do=rebootcontrol&show=subnet&what=subnet&id=' . Database::lastInsertId());
		}
	}

	private static function editSubnet()
	{
		User::assertPermission('subnet.flag');
		$id = Request::post('id', Request::REQUIRED, 'int');
		$subnet = Database::queryFirst('SELECT subnetid
				FROM reboot_subnet WHERE subnetid = :id', ['id' => $id]);
		if ($subnet === false) {
			Message::addError('invalid-subnet', $id);
			return;
		}
		$params = [
			'id' => $id,
			'fixed' => !empty(Request::post('fixed', false, 'string')),
			'isdirect' => !empty(Request::post('isdirect', false, 'string')),
		];
		Database::exec('UPDATE reboot_subnet SET fixed = :fixed, isdirect = If(:fixed, :isdirect, isdirect)
				WHERE subnetid = :id', $params);
		if (User::hasPermission('jumphost.assign-subnet')) {
			$hosts = Request::post('jumphost', [], 'array');
			if (empty($hosts)) {
				Database::exec('DELETE FROM reboot_jumphost_x_subnet WHERE subnetid = :id', ['id' => $id]);
			} else {
				$hosts = array_keys($hosts);
				Database::exec('DELETE FROM reboot_jumphost_x_subnet WHERE subnetid = :id AND hostid NOT IN (:hosts)',
					['id' => $id, 'hosts' => $hosts]);
				$hosts = array_map(function($item) use ($id) {
					return [$item, $id];
				}, $hosts);
				Database::exec('INSERT IGNORE INTO reboot_jumphost_x_subnet (hostid, subnetid) VALUES :hosts', ['hosts' => $hosts]);
			}
		}
		Message::addSuccess('subnet-updated');
	}

	private static function deleteSubnet()
	{
		User::assertPermission('subnet.edit');
		User::assertPermission('subnet.flag');
		$id = Request::post('id', Request::REQUIRED, 'int');
		$num = Database::exec('DELETE FROM reboot_subnet WHERE subnetid = :id', ['id' => $id]);
		if ($num < 1) {
			Message::addError('invalid-subnet', $id);
			return;
		}
		Message::addSuccess('subnet-deleted');
	}

	/*
	 * Render
	 */

	public static function doRender()
	{
		$what = Request::get('what', 'list', 'string');
		if ($what === 'list') {
			self::showSubnets();
		} elseif ($what === 'subnet') {
			self::showSubnet();
		}
	}

	private static function showSubnets()
	{
		User::assertPermission('subnet.*');
		$nets = [];
		$c2c = Property::get(RebootControl::KEY_SCAN_CLIENT_TO_CLIENT);
		$res = Database::simpleQuery('SELECT subnetid, start, end, fixed, isdirect,
       		nextdirectcheck, lastseen, seencount, Count(hxs.hostid) AS jumphostcount, Count(sxs.srcid) AS sourcecount
				FROM reboot_subnet s
				LEFT JOIN reboot_jumphost_x_subnet hxs USING (subnetid)
				LEFT JOIN reboot_subnet_x_subnet sxs ON (s.subnetid = sxs.dstid AND sxs.reachable <> 0)
				GROUP BY subnetid, start, end
				ORDER BY start ASC, end DESC');
		$deadline = strtotime('-60 days');
		foreach ($res as $row) {
			$row['cidr'] = IpUtil::rangeToCidr($row['start'], $row['end']);
			$row['lastseen_s'] = Util::prettyTime($row['lastseen']);
			if ($row['lastseen'] && $row['lastseen'] < $deadline) {
				$row['lastseen_class'] = 'text-danger';
			}
			if (!$c2c) {
				$row['sourcecount'] = '-';
			}
			$nets[] = $row;
		}
		$data = ['subnets' => $nets];
		Render::addTemplate('subnet-list', $data);
	}

	private static function showSubnet()
	{
		User::assertPermission('subnet.*');
		$id = Request::get('id', Request::REQUIRED, 'int');
		$subnet = Database::queryFirst('SELECT subnetid, start, end, fixed, isdirect
				FROM reboot_subnet WHERE subnetid = :id', ['id' => $id]);
		if ($subnet === false) {
			Message::addError('invalid-subnet', $id);
			return;
		}
		$subnet['cidr'] = IpUtil::rangeToCidr($subnet['start'], $subnet['end']);
		$subnet['start_s'] = long2ip($subnet['start']);
		$subnet['end_s'] = long2ip($subnet['end']);
		// Get list of jump hosts
		$res = Database::simpleQuery('SELECT h.hostid, h.host, h.port, hxs.subnetid FROM reboot_jumphost h
				LEFT JOIN reboot_jumphost_x_subnet hxs ON (h.hostid = hxs.hostid AND hxs.subnetid = :id)
				ORDER BY h.host ASC', ['id' => $id]);
		// Mark those assigned to the current subnet
		$jh = [];
		foreach ($res as $row) {
			$row['checked'] = $row['subnetid'] === null ? '' : 'checked';
			$jh[] = $row;
		}
		$subnet['jumpHosts'] = $jh;
		$c2c = Property::get(RebootControl::KEY_SCAN_CLIENT_TO_CLIENT);
		if ($c2c) {
			// Get list of all subnets that can broadcast into this one
			$res = Database::simpleQuery('SELECT s.start, s.end FROM reboot_subnet s
				INNER JOIN reboot_subnet_x_subnet sxs ON (s.subnetid = sxs.srcid AND sxs.dstid = :id AND sxs.reachable = 1)
				ORDER BY s.start ASC', ['id' => $id]);
			$sn = [];
			foreach ($res as $row) {
				$sn[] = ['cidr' => IpUtil::rangeToCidr($row['start'], $row['end'])];
			}
			$subnet['sourceNets'] = $sn;
			$subnet['showC2C'] = true;
		}
		Permission::addGlobalTags($subnet['perms'], null, ['subnet.flag', 'jumphost.view', 'jumphost.assign-subnet']);
		Render::addTemplate('subnet-edit', $subnet);
	}

	public static function doAjax()
	{

	}

}