File: SysAuthLDAPPlugin.class.php

package info (click to toggle)
fusionforge 5.3.2%2B20141104-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 60,472 kB
  • sloc: php: 271,846; sql: 36,817; python: 14,575; perl: 6,406; sh: 5,980; xml: 4,294; pascal: 1,411; makefile: 911; cpp: 52; awk: 27
file content (364 lines) | stat: -rw-r--r-- 10,187 bytes parent folder | download | duplicates (3)
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
<?php
/**
 * FusionForge sysauthldap plugin
 *
 * Copyright 2012, Roland Mas
 *
 * This file is part of FusionForge.
 *
 * FusionForge is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 2 of the License,
 * or (at your option) any later version.
 *
 * FusionForge is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

forge_define_config_item('enabled', 'sysauthldap', 'no');
forge_set_config_item_bool('enabled', 'sysauthldap');

forge_define_config_item('ldap_host', 'sysauthldap', '$core/web_host');
forge_define_config_item('ldap_port', 'sysauthldap', '389');
forge_define_config_item('ldap_version', 'sysauthldap', '3');
forge_define_config_item('base_dn', 'sysauthldap', 'fromhost:$core/web_host');
forge_define_config_item('bind_dn', 'sysauthldap', 'cn=admin,$sysauthldap/base_dn');
forge_define_config_item('password', 'sysauthldap', '');

setconfigfromenv ('sysauthldap', 'ldap_password',
			 'GForgePluginSysAuthLdapPasswd', NULL) ;

class SysAuthLDAPPlugin extends SysAuthPlugin {
	function SysAuthLDAPPlugin () {
		$this->SysAuthPlugin() ;
		$this->name = "sysauthldap" ;
		$this->text = _("System authentication via LDAP");
		$this->pkg_desc =
_("This plugin maintains data about users, groups and memberships in an
LDAP directory that can be used for NSS/PAM system authentication (or
for other uses).");
		$this->ldap_conn = NULL;
		$this->user_suffix = "Users";
		$this->group_suffix = "Projects";
	}

	function setError($msg, $code=1) {
		error_log($msg);
		parent::setError($msg,$code);
	}

	function _connect() {
		if ($this->ldap_conn) {
			return true;
		}

		$this->clearError();
		$ldap_conn = @ldap_connect(forge_get_config('ldap_host', $this->name),forge_get_config('ldap_port', $this->name));
		if (!$ldap_conn) {
			$this->setError('ERROR: Cannot connect to LDAP server<br />');
			return false;
		}
		if (forge_get_config('ldap_version', $this->name)) {
			if (!ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, forge_get_config('ldap_version', $this->name))) {
				return false;
			}
		}

		$this->basedn = $this->_parse_fromhost(forge_get_config('base_dn', $this->name));
		$this->binddn = $this->_parse_fromhost(forge_get_config('bind_dn', $this->name));

		if (!ldap_bind($ldap_conn,$this->binddn,forge_get_config('ldap_password', $this->name))) {
			return false;
		}
		$this->ldap_conn = $ldap_conn;
		return true;
	}

	function _parse_fromhost($s) {
		$result = $s;
		if (preg_match ('/\b(fromhost:[^,]+)/', $s, $matches)) {
			$m = preg_replace ('/^fromhost:/','',$matches[0]);

			$r = array();
			foreach (explode ('.', $m) as $i) {
				$r[] = 'dc='.$i;
			}
			$result = preg_replace ('/\b(fromhost:[^,]+)/',
						      implode(',',$r),
						      $s);
		}
		return $result;
	}

	function user_update($params) {
		if (!forge_get_config('enabled',$this->name)) return true;
		if (!$this->_connect()) { exit_error("Error connecting to LDAP"); }

		$user = $params['user'];

		if (! $user->isActive()) {
			return $this->_user_delete($user);
		}

		$entry = $this->_get_user_entry($user);
		$dn = $this->_get_user_dn($user);

		if (!$this->_exists($dn)) {
			if (@ldap_add($this->ldap_conn,$dn,$entry)) {
				return true;
			}

			$this->_create_user_suffix();
			if (ldap_add($this->ldap_conn,$dn,$entry)) {
				return true;
			}
			$this->setError("ERROR: cannot add LDAP user entry '".
					$user->getUnixName()."' ($dn): ".ldap_error($this->ldap_conn));
			return false;
		} else {
			if (@ldap_modify($this->ldap_conn,$dn,$entry)) {
				return true;
			}
			$this->setError("ERROR: cannot modify LDAP user entry '".
					$user->getUnixName()."' ($dn): ".ldap_error($this->ldap_conn));
			return false;
		}
	}

	function _user_delete($user) {
		$dn = $this->_get_user_dn($user);

		if (!$this->_exists($dn)) {
			return true;
		}

		if (ldap_delete($this->ldap_conn,$dn)) {
			return true;
		}
		$this->setError("ERROR: cannot delete LDAP user entry '".
				$user->getUnixName()."' ($dn): ".ldap_error($this->ldap_conn));
		return false;
	}

	function user_delete($params) {
		if (!forge_get_config('enabled',$this->name)) return true;
		if (!$this->_connect()) { exit_error("Error connecting to LDAP"); }

		$user = $params['user'];

		return $this->_user_delete($user);
	}

	function _group_update_standard($group) {
		$entry = $this->_get_group_entry($group);
		$dn = $this->_get_group_dn($group);

		foreach ($group->getUsers(false) as $u) {
			$entry['memberUid'][] = $u->getUnixName();
		}

		if (!$this->_exists($dn)) {
			if (@ldap_add($this->ldap_conn,$dn,$entry)) {
				return true;
			}

			$this->_create_group_suffix();
			if (ldap_add($this->ldap_conn,$dn,$entry)) {
				return true;
			}

			$this->setError("ERROR: cannot add LDAP group entry '".
					$group->getUnixName()."' ($dn): ".ldap_error($this->ldap_conn));
			return false;
		} else {
			if (@ldap_modify($this->ldap_conn,$dn,$entry)) {
				return true;
			}

			$this->setError("ERROR: cannot modify LDAP group entry '".
					$group->getUnixName()."' ($dn): ".ldap_error($this->ldap_conn));
			return false;
		}
	}

	function _group_update_scm($group) {
		if (! $group->usesSCM()) {
			return $this->_group_delete_prefix($group,'scm_');
		}

		$entry = $this->_get_group_entry($group, 'scm_');
		$dn = $this->_get_group_dn($group, 'scm_');

		foreach ($group->getUsers(false) as $u) {
			if (forge_check_perm_for_user($u,'scm',$group->getID(),'write')) {
				$entry['memberUid'][] = $u->getUnixName();
			}
		}

		if (!$this->_exists($dn)) {
			if (@ldap_add($this->ldap_conn,$dn,$entry)) {
				return true;
			}

			$this->_create_group_suffix();
			if (ldap_add($this->ldap_conn,$dn,$entry)) {
				return true;
			}

			$this->setError("ERROR: cannot add LDAP group entry '".
					$group->getUnixName()."' ($dn): ".ldap_error($this->ldap_conn));
			return false;
		} else {
			if (@ldap_modify($this->ldap_conn,$dn,$entry)) {
				return true;
			}

			$this->setError("ERROR: cannot modify LDAP group entry '".
					$group->getUnixName()."' ($dn): ".ldap_error($this->ldap_conn));
			return false;
		}
	}

	function _group_delete_prefix($group,$prefix='') {
		$dn = $this->_get_group_dn($group,$prefix);

		if (!$this->_exists($dn)) {
			return true;
		}

		if (ldap_delete($this->ldap_conn,$dn)) {
			return true;
		}

		$this->setError("ERROR: cannot delete LDAP group entry '".
				$group->getUnixName()."' ($dn): ".ldap_error($this->ldap_conn));
		return false;
	}

	function group_update($params) {
		if (!forge_get_config('enabled',$this->name)) return true;
		if (!$this->_connect()) { exit_error("Error connecting to LDAP"); }

		$group = $params['group'];

		if (! $group->isActive()) {
			return $this->_group_delete_prefix($group) &&
				$this->_group_delete_prefix($group,'scm_');
		}

		return $this->_group_update_standard($group)
			&& $this->_group_update_scm($group);
	}

	function group_delete($params) {
		if (!forge_get_config('enabled',$this->name)) return true;
		if (!$this->_connect()) { exit_error("Error connecting to LDAP"); }

		$group = $params['group'];

		return $this->_group_delete_prefix($group,'') &&
			$this->_group_delete_prefix($group,'scm_');
	}

	function _get_user_dn_suffix() {
		return 'ou='.$this->user_suffix.','.$this->basedn;
	}

	function _create_user_suffix() {
		$dn = $this->_get_user_dn_suffix();
		if (!$this->_exists($dn)) {
			$entry = array();
			$entry['objectClass'] = 'organizationalUnit';
			$entry['ou'] = $this->user_suffix;
			ldap_add($this->ldap_conn,$dn,$entry);
		}
	}

	function _get_user_dn($user) {
		return 'uid='.$user->getUnixName().','.$this->_get_user_dn_suffix();
	}

	function _get_user_entry($user) {
		$entry = array();

		$entry['objectClass'][0]='top';
		$entry['objectClass'][1]='account';
		$entry['objectClass'][2]='posixAccount';
		$entry['objectClass'][3]='shadowAccount';
		$entry['objectClass'][4]='debGforgeAccount';
		$entry['uid']=$user->getUnixName();
		$entry['cn']=$user->getRealName();
		$entry['gecos']=preg_replace("/[\x80-\xff]/","?",$user->getRealName());
		$entry['userPassword']='{crypt}'.$user->getUnixPasswd();
		$entry['homeDirectory'] = account_user_homedir($user->getUnixName());
		$entry['loginShell']=$user->getShell();
		$entry['debGforgeCvsShell']="/bin/cvssh";
		$entry['debGforgeForwardEmail']=$user->getEmail();
		$entry['uidNumber']=$user->getUnixUID();
		$entry['gidNumber']=$user->getUnixGID();
		$entry['shadowLastChange']=1;
		$entry['shadowMax']=99999;
		$entry['shadowWarning']=7;

		return $entry;
	}

	function _get_group_dn_suffix() {
		return 'ou='.$this->group_suffix.','.$this->basedn;
	}

	function _create_group_suffix() {
		$dn = $this->_get_group_dn_suffix();
		if (!$this->_exists($dn)) {
			$entry = array();
			$entry['objectClass'] = 'organizationalUnit';
			$entry['ou'] = $this->group_suffix;
			ldap_add($this->ldap_conn,$dn,$entry);
		}
	}

	function _get_group_dn($group, $prefix='') {
		return 'cn='.$prefix.$group->getUnixName().','.$this->_get_group_dn_suffix();
	}

	function _get_group_entry($group, $prefix='') {
		$entry = array();

		$entry['objectClass'][0]='top';
		$entry['objectClass'][1]='posixGroup';
		$entry['cn']=$prefix.$group->getUnixName();
		$entry['userPassword']='{crypt}x';
		$entry['gidNumber']=$group->getID()+10000;

		return $entry;
	}

	function _exists($dn) {
		$sdn = $this->basedn;
		$t = ldap_explode_dn($dn,0);
		$t = $t[0];
		$filter = "($t)";
		$sr = ldap_search($this->ldap_conn, $sdn, $filter);
		if (!$sr) {
			return false;
		}
		$e = ldap_get_entries($this->ldap_conn, $sr);
		for ($i = 0; $i < $e['count']; $i++) {
			if ($e[$i]['dn'] == $dn) {
				return true;
			}
		}
		return false;
	}
}

// Local Variables:
// mode: php
// c-file-style: "bsd"
// End: