summaryrefslogtreecommitdiffstats
path: root/inc/configmodule/adauth.inc.php
blob: c0d4860c1ddd8ca7de5a7f2c755065d4de7099aa (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
<?php

ConfigModules::registerModule(
	ConfigModule_AdAuth::MODID, // ID
	Dictionary::translate('config-module', 'adAuth_title'), // Title
	Dictionary::translate('config-module', 'adAuth_description'), // Description
	Dictionary::translate('config-module', 'group_authentication'), // Group
	true // Only one per config?
);

class ConfigModule_AdAuth extends ConfigModule
{
	const MODID = 'AdAuth';

	public static function insert($title, $server, $searchbase, $binddn, $bindpw, $home)
	{
		Database::exec("LOCK TABLE configtgz_module WRITE");
		Database::exec("INSERT INTO configtgz_module (title, moduletype, filepath, contents) "
			. " VALUES (:title, :modid, '', '')", array('title' => $title, 'modid' => self::MODID));
		$id = Database::lastInsertId();
		if (!is_numeric($id)) Util::traceError('Inserting new AD config to DB did not yield a numeric insert id');
		// Entry created, now try to get a free port for the proxy
		$res = Database::simpleQuery("SELECT moduleid, contents FROM configtgz_module WHERE moduletype = :modid", array(
			'modid' => self::MODID
		));
		$ports = array();
		while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
			if ($row['moduleid'] == $id) {
				// ...
			} else {
				$data = json_decode($row['contents'], true);
				if (isset($data['proxyport'])) $ports[] = $data['proxyport'];
			}
		}
		$port = 3300;
		while (in_array($port, $ports)) {
			$port++;
		}
		// Port determined, carry on...
		$ownEntry = array(
			'server' => $server,
			'searchbase' => $searchbase,
			'binddn' => $binddn,
			'bindpw' => $bindpw,
			'home' => $home,
			'proxyport' => $port
		);
		$data = json_encode($ownEntry);
		if ($data === false) Util::traceError('Serializing the AD data failed.');
		$moduleTgz = CONFIG_TGZ_LIST_DIR . '/modules/AD_AUTH_id_' . $id . '.' . mt_rand() . '.tgz';
		Database::exec("UPDATE configtgz_module SET filepath = :filename, contents = :contents WHERE moduleid = :id LIMIT 1", array(
			'id' => $id,
			'filename' => $moduleTgz,
			'contents' => $data
		));
		Database::exec("UNLOCK TABLES");
		// Add archive file name to array before returning it
		$ownEntry['moduleid'] = $id;
		$ownEntry['filename'] = $moduleTgz;
		return $ownEntry;
	}

	/**
	 * To be called if the server ip changes, as it's embedded in the AD module configs.
	 * This will then recreate all AD tgz modules.
	 */
	private static function rebuildAll($parent = NULL)
	{
		// Stop all running instances of ldadp
		$task = Taskmanager::submit('LdadpLauncher', array(
				'parentTask' => $parent,
				'failOnParentFail' => false,
				'ids' => array()
		));
		$ads = self::getAll();
		if (empty($ads)) // Nothing to do
			return false;

		if (isset($task['id']))
			$parent = $task['id'];
		foreach ($ads as $ad) {
			$ad['parentTask'] = $parent;
			$ad['failOnParentFail'] = false;
			$ad['proxyip'] = Property::getServerIp();
			$task = Taskmanager::submit('CreateAdConfig', $ad);
			if (isset($task['id']))
				$parent = $task['id'];
		}
		Trigger::ldadp($parent);
		return $parent;
	}
	
	/**
	 * Get all existing AD proxy configs.
	 * 
	 * @return array array of ad configs in DB with fields:
	 *		moduleid, filename, server, searchbase, binddn, bindpw, home, proxyport
	 */
	public static function getAll()
	{
		$res = Database::simpleQuery("SELECT moduleid, filepath, contents FROM configtgz_module WHERE moduletype = :modid", array(
			'modid' => self::MODID
		));
		$mods = array();
		while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
			$data = json_decode($row['contents'], true);
			$data['moduleid'] = $row['moduleid'];
			$data['filename'] = $row['filepath'];
			$mods[] = $data;
		}
		return $mods;
	}
	
	// ############## Callbacks #############################
	
	/**
	 * Server IP changed - rebuild all AD modules.
	 */
	public function event_serverIpChanged()
	{
		self::rebuildAll();
	}
	
}