summaryrefslogtreecommitdiffstats
path: root/modules-available/sysconfig/install.inc.php
blob: 5388288275decae393c7e1d87478e2e91580fa78 (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
<?php

$update = array();

$update[] = tableCreate('configtgz', "
	`configid` int(10) unsigned NOT NULL AUTO_INCREMENT,
	`title` varchar(200) NOT NULL,
	`filepath` varchar(255) NOT NULL,
	`status` enum('OK','OUTDATED','MISSING') NOT NULL DEFAULT 'MISSING',
	`warnings` TEXT NULL DEFAULT NULL,
	`dateline` int(10) unsigned NOT NULL DEFAULT '0',
	PRIMARY KEY (`configid`)
");

$update[] = tableCreate('configtgz_module', "
	`moduleid` int(10) unsigned NOT NULL AUTO_INCREMENT,
	`title` varchar(200) NOT NULL,
	`moduletype` varchar(16) NOT NULL,
	`filepath` varchar(250) NOT NULL,
	`contents` longblob NOT NULL,
	`version` int(10) unsigned NOT NULL DEFAULT '0',
	`status` enum('OK','MISSING','OUTDATED') NOT NULL DEFAULT 'MISSING',
	`dateline` int(10) unsigned NOT NULL DEFAULT '0',
	PRIMARY KEY (`moduleid`),
	KEY `title` (`title`),
	KEY `moduletype` (`moduletype`,`title`)
");

$update[] = tableCreate('configtgz_x_module', "
	`configid` int(10) unsigned NOT NULL,
	`moduleid` int(10) unsigned NOT NULL,
	PRIMARY KEY (`configid`,`moduleid`),
	KEY `moduleid` (`moduleid`)
");

$update[] = tableCreate('configtgz_location', "
	`locationid` int(11) NOT NULL,
	`configid` int(10) unsigned NOT NULL,
	PRIMARY KEY (`locationid`),
	KEY `configid` (`configid`)
");

// Constraints
$update[] = tableAddConstraint('configtgz_x_module', 'configid', 'configtgz', 'configid',
	'ON DELETE CASCADE ON UPDATE CASCADE');
$update[] = tableAddConstraint('configtgz_x_module', 'moduleid', 'configtgz_module', 'moduleid',
	'ON DELETE CASCADE ON UPDATE CASCADE');
$update[] = tableAddConstraint('configtgz_location', 'configid', 'configtgz', 'configid',
	'ON DELETE CASCADE ON UPDATE CASCADE');
// No constraint to location table since we use locationid 0 for global (NULL would require special handling for UPDATE)

// Update path

// #######################
// ##### 2014-12-12
// Rename config modules
Database::exec("UPDATE configtgz_module SET moduletype = 'Branding' WHERE moduletype = 'BRANDING'");
Database::exec("UPDATE configtgz_module SET moduletype = 'AdAuth' WHERE moduletype = 'AD_AUTH'");
Database::exec("UPDATE configtgz_module SET moduletype = 'CustomModule' WHERE moduletype = 'custom'");

// #######################
// ##### 2015-01-16
// Extend config module db tables
tableDropColumn('configtgz_module', 'haschanged');
if (!tableHasColumn('configtgz_module', 'version')) {
	if (Database::exec("ALTER TABLE `configtgz_module` ADD `version` INT( 10 ) UNSIGNED NOT NULL DEFAULT '0'") === false) {
		finalResponse(UPDATE_FAILED, 'Could not add version to configtgz_module: ' . Database::lastError());
	}
	$update[] = UPDATE_DONE;
}
if (!tableHasColumn('configtgz_module', 'status')) {
	if (Database::exec("ALTER TABLE `configtgz_module` ADD `status` ENUM( 'OK', 'MISSING', 'OUTDATED' ) NOT NULL DEFAULT 'MISSING'") === false) {
		finalResponse(UPDATE_FAILED, 'Could not add status to configtgz_module: ' . Database::lastError());
	}
	$update[] = UPDATE_DONE;
}
if (!tableHasColumn('configtgz', 'status')) {
	if (Database::exec("ALTER TABLE `configtgz` ADD `status` ENUM( 'OK', 'OUTDATED', 'MISSING' ) NOT NULL DEFAULT 'MISSING'") === false) {
		finalResponse(UPDATE_FAILED, 'Could not add status to configtgz: ' . Database::lastError());
	}
	$update[] = UPDATE_DONE;
}
if (!tableHasColumn('configtgz_module', 'dateline')) {
	if (Database::exec("ALTER TABLE `configtgz_module` ADD `dateline` int(10) unsigned NOT NULL DEFAULT '0'") === false) {
		finalResponse(UPDATE_FAILED, 'Could not add dateline to configtgz_module: ' . Database::lastError());
	}
	$update[] = UPDATE_DONE;
	// Infer from module's filemtime
	$res = Database::simpleQuery('SELECT moduleid, filepath FROM configtgz_module');
	foreach ($res as $row) {
		Database::exec('UPDATE configtgz_module SET dateline = :mtime WHERE moduleid = :moduleid',
			['moduleid' => $row['moduleid'], 'mtime' => filemtime($row['filepath'])]);
	}
}
if (!tableHasColumn('configtgz', 'dateline')) {
	if (Database::exec("ALTER TABLE `configtgz` ADD `dateline` int(10) unsigned NOT NULL DEFAULT '0'") === false) {
		finalResponse(UPDATE_FAILED, 'Could not add dateline to configtgz: ' . Database::lastError());
	}
	$update[] = UPDATE_DONE;
	// Infer from latest module (since module injection by slx-admin modules would alter the timestamp)
	$res = Database::simpleQuery('SELECT c.configid, Max(m.dateline) AS dateline FROM configtgz c
			INNER JOIN configtgz_x_module cxm USING (configid)
			INNER JOIN configtgz_module m USING (moduleid)
			GROUP BY configid');
	foreach ($res as $row) {
		Database::exec('UPDATE configtgz SET dateline = :mtime WHERE configid = :configid',
			['configid' => $row['configid'], 'mtime' => $row['dateline']]);
	}
}

// 2020-01-16: Change contents column type
Database::exec("ALTER TABLE `configtgz_module` CHANGE `contents` `contents` LONGBLOB NOT NULL");

// 2020-11-02: Add warnings column
if (!tableHasColumn('configtgz', 'warnings')) {
	if (Database::exec("ALTER TABLE `configtgz` ADD `warnings` TEXT NULL DEFAULT NULL") === false) {
		finalResponse(UPDATE_FAILED, 'Could not add warnings to configtgz: ' . Database::lastError());
	}
	$update[] = UPDATE_DONE;
}

// ----- rebuild configs ------
// PERMANENT HACK; Rebuild configs.. move somewhere else?
Module::isAvailable('sysconfig');
$list = ConfigModule::getAll();
$parentTask = null;
$configList = [];
if ($list === null) {
	EventLog::warning('Could not regenerate configs - please do so manually');
} else {
	foreach ($list as $confMod) {
		if ($confMod->moduleType() === 'SshConfig') {
			// 2020-11-12: Split SshConfig into SshConfig and SshKey
			$pubkey = $confMod->getData('publicKey');
			if (!empty($pubkey)) {
				error_log('Legacy module with pubkey ' . $confMod->id());
				$key = ConfigModule::getInstanceOrNull('SshKey');
				if ($key !== null) {
					$key->setData('publicKey', $pubkey);
					if ($key->insert($confMod->title())) {
						// Insert worked, remove key from old module, add this module to the same configs
						$task = $key->generate(false, $parentTask);
						if ($task !== false) {
							$parentTask = $task;
						}
						error_log('Inserted new module with id ' . $key->id());
						$confMod->setData('publicKey', false);
						$confMod->update();
						$configs = ConfigTgz::getAllForModule($confMod->id());
						foreach ($configs as $config) {
							// Add newly created key-only module to all configs
							$new = array_merge($config->getModuleIds(), [$key->id()]);
							error_log(implode(',', $config->getModuleIds()) . ' -> ' . implode(',', $new));
							$config->update('', $new);
							$configList[] = $config;
						}
					}
				}
			}
		}
		if ($confMod->needRebuild()) {
			$update[] = UPDATE_DONE;
			$task = $confMod->generate(false, $parentTask);
			if ($task !== false) {
				$parentTask = $task;
			}
		}
	}
	foreach ($configList as $config) {
		$config->generate(false, 0, $parentTask);
	}
}

// Start any changed services
ConfigModuleBaseLdap::ldadp();

// Create response for browser
responseFromArray($update);