Project

General

Profile

Download (24.5 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	system_usermanager.php
5
*/
6
/* ====================================================================
7
 *	Copyright (c)  2004-2015  Electric Sheep Fencing, LLC. All rights reserved.
8
 *	Copyright (c)  2004, 2005 Scott Ullrich
9
 *	Copyright (c)  2003-2005 Manuel Kasper <mk@neon1.net>
10
 *	Copyright (c)  2008 Shrew Soft Inc.
11
 *	Copyright (c)  2005 Paul Taylor <paultaylor@winn-dixie.com>
12
 *
13
 *	Redistribution and use in source and binary forms, with or without modification,
14
 *	are permitted provided that the following conditions are met:
15
 *
16
 *	1. Redistributions of source code must retain the above copyright notice,
17
 *		this list of conditions and the following disclaimer.
18
 *
19
 *	2. Redistributions in binary form must reproduce the above copyright
20
 *		notice, this list of conditions and the following disclaimer in
21
 *		the documentation and/or other materials provided with the
22
 *		distribution.
23
 *
24
 *	3. All advertising materials mentioning features or use of this software
25
 *		must display the following acknowledgment:
26
 *		"This product includes software developed by the pfSense Project
27
 *		 for use in the pfSense software distribution. (http://www.pfsense.org/).
28
 *
29
 *	4. The names "pfSense" and "pfSense Project" must not be used to
30
 *		 endorse or promote products derived from this software without
31
 *		 prior written permission. For written permission, please contact
32
 *		 coreteam@pfsense.org.
33
 *
34
 *	5. Products derived from this software may not be called "pfSense"
35
 *		nor may "pfSense" appear in their names without prior written
36
 *		permission of the Electric Sheep Fencing, LLC.
37
 *
38
 *	6. Redistributions of any form whatsoever must retain the following
39
 *		acknowledgment:
40
 *
41
 *	"This product includes software developed by the pfSense Project
42
 *	for use in the pfSense software distribution (http://www.pfsense.org/).
43
 *
44
 *	THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
45
 *	EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46
 *	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
47
 *	PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
48
 *	ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
49
 *	SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50
 *	NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
51
 *	LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52
 *	HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
53
 *	STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54
 *	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
55
 *	OF THE POSSIBILITY OF SUCH DAMAGE.
56
 *
57
 *	====================================================================
58
 *
59
 */
60
/*
61
	pfSense_BUILDER_BINARIES:
62
	pfSense_MODULE: auth
63
*/
64

    
65
##|+PRIV
66
##|*IDENT=page-system-usermanager
67
##|*NAME=System: User Manager page
68
##|*DESCR=Allow access to the 'System: User Manager' page.
69
##|*MATCH=system_usermanager.php*
70
##|-PRIV
71

    
72
require("certs.inc");
73
require("guiconfig.inc");
74

    
75
// start admin user code
76
$pgtitle = array(gettext("System"), gettext("User Manager"));
77

    
78
if (isset($_POST['userid']) && is_numericint($_POST['userid'])) {
79
	$id = $_POST['userid'];
80
}
81

    
82
if (isset($_GET['userid']) && is_numericint($_GET['userid'])) {
83
	$id = $_GET['userid'];
84
}
85

    
86
if (!isset($config['system']['user']) || !is_array($config['system']['user'])) {
87
	$config['system']['user'] = array();
88
}
89

    
90
$a_user = &$config['system']['user'];
91
$act = $_GET['act'];
92

    
93
if (isset($_SERVER['HTTP_REFERER'])) {
94
	$referer = $_SERVER['HTTP_REFERER'];
95
} else {
96
	$referer = '/system_usermanager.php';
97
}
98

    
99
if (isset($id) && $a_user[$id]) {
100
	$pconfig['usernamefld'] = $a_user[$id]['name'];
101
	$pconfig['descr'] = $a_user[$id]['descr'];
102
	$pconfig['expires'] = $a_user[$id]['expires'];
103
	$pconfig['groups'] = local_user_get_groups($a_user[$id]);
104
	$pconfig['utype'] = $a_user[$id]['scope'];
105
	$pconfig['uid'] = $a_user[$id]['uid'];
106
	$pconfig['authorizedkeys'] = base64_decode($a_user[$id]['authorizedkeys']);
107
	$pconfig['priv'] = $a_user[$id]['priv'];
108
	$pconfig['ipsecpsk'] = $a_user[$id]['ipsecpsk'];
109
	$pconfig['disabled'] = isset($a_user[$id]['disabled']);
110
}
111

    
112
if ($_POST['act'] == "deluser") {
113

    
114
	if (!isset($_POST['username']) || !isset($a_user[$id]) || ($_POST['username'] != $a_user[$id]['name'])) {
115
		pfSenseHeader("system_usermanager.php");
116
		exit;
117
	}
118

    
119
	conf_mount_rw();
120
	local_user_del($a_user[$id]);
121
	conf_mount_ro();
122
	$userdeleted = $a_user[$id]['name'];
123
	unset($a_user[$id]);
124
	write_config();
125
	$savemsg = gettext("User")." {$userdeleted} ".
126
				gettext("successfully deleted")."<br />";
127
}
128
else if ($act == "new") {
129
	/*
130
	 * set this value cause the text field is read only
131
	 * and the user should not be able to mess with this
132
	 * setting.
133
	 */
134
	$pconfig['utype'] = "user";
135
	$pconfig['lifetime'] = 3650;
136
}
137

    
138
if (isset($_POST['dellall_x'])) {
139

    
140
	$del_users = $_POST['delete_check'];
141

    
142
	if (!empty($del_users)) {
143
		foreach ($del_users as $userid) {
144
			if (isset($a_user[$userid]) && $a_user[$userid]['scope'] != "system") {
145
				conf_mount_rw();
146
				local_user_del($a_user[$userid]);
147
				conf_mount_ro();
148
				unset($a_user[$userid]);
149
			}
150
		}
151
		$savemsg = gettext("Selected users removed successfully!");
152
		write_config($savemsg);
153
	}
154
}
155

    
156
if ($_POST['act'] == "delcert") {
157

    
158
	if (!$a_user[$id]) {
159
		pfSenseHeader("system_usermanager.php");
160
		exit;
161
	}
162

    
163
	$certdeleted = lookup_cert($a_user[$id]['cert'][$_POST['certid']]);
164
	$certdeleted = $certdeleted['descr'];
165
	unset($a_user[$id]['cert'][$_POST['certid']]);
166
	write_config();
167
	$_POST['act'] = "edit";
168
	$savemsg = gettext("Certificate") . " {$certdeleted} " . gettext("association removed.") . "<br />";
169
}
170

    
171
if ($_POST['save']) {
172
	unset($input_errors);
173
	$pconfig = $_POST;
174

    
175
	/* input validation */
176
	if (isset($id) && ($a_user[$id])) {
177
		$reqdfields = explode(" ", "usernamefld");
178
		$reqdfieldsn = array(gettext("Username"));
179
	} else {
180
		if (empty($_POST['name'])) {
181
			$reqdfields = explode(" ", "usernamefld passwordfld1");
182
			$reqdfieldsn = array(
183
				gettext("Username"),
184
				gettext("Password"));
185
		} else {
186
			$reqdfields = explode(" ", "usernamefld passwordfld1 name caref keylen lifetime");
187
			$reqdfieldsn = array(
188
				gettext("Username"),
189
				gettext("Password"),
190
				gettext("Descriptive name"),
191
				gettext("Certificate authority"),
192
				gettext("Key length"),
193
				gettext("Lifetime"));
194
		}
195
	}
196

    
197
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
198

    
199
	if (preg_match("/[^a-zA-Z0-9\.\-_]/", $_POST['usernamefld'])) {
200
		$input_errors[] = gettext("The username contains invalid characters.");
201
	}
202

    
203
	if (strlen($_POST['usernamefld']) > 16) {
204
		$input_errors[] = gettext("The username is longer than 16 characters.");
205
	}
206

    
207
	if (($_POST['passwordfld1']) && ($_POST['passwordfld1'] != $_POST['passwordfld2'])) {
208
		$input_errors[] = gettext("The passwords do not match.");
209
	}
210

    
211
	if (isset($_POST['ipsecpsk']) && !preg_match('/^[[:ascii:]]*$/', $_POST['ipsecpsk'])) {
212
		$input_errors[] = gettext("IPsec Pre-Shared Key contains invalid characters.");
213
	}
214

    
215
	if (isset($id) && $a_user[$id]) {
216
		$oldusername = $a_user[$id]['name'];
217
	} else {
218
		$oldusername = "";
219
	}
220
	/* make sure this user name is unique */
221
	if (!$input_errors) {
222
		foreach ($a_user as $userent) {
223
			if ($userent['name'] == $_POST['usernamefld'] && $oldusername != $_POST['usernamefld']) {
224
				$input_errors[] = gettext("Another entry with the same username already exists.");
225
				break;
226
			}
227
		}
228
	}
229
	/* also make sure it is not reserved */
230
	if (!$input_errors) {
231
		$system_users = explode("\n", file_get_contents("/etc/passwd"));
232
		foreach ($system_users as $s_user) {
233
			$ent = explode(":", $s_user);
234
			if ($ent[0] == $_POST['usernamefld'] && $oldusername != $_POST['usernamefld']) {
235
				$input_errors[] = gettext("That username is reserved by the system.");
236
				break;
237
			}
238
		}
239
	}
240

    
241
	/*
242
	 * Check for a valid expiration date if one is set at all (valid means,
243
	 * DateTime puts out a time stamp so any DateTime compatible time
244
	 * format may be used. to keep it simple for the enduser, we only
245
	 * claim to accept MM/DD/YYYY as inputs. Advanced users may use inputs
246
	 * like "+1 day", which will be converted to MM/DD/YYYY based on "now".
247
	 * Otherwise such an entry would lead to an invalid expiration data.
248
	 */
249
	if ($_POST['expires']) {
250
		try {
251
			$expdate = new DateTime($_POST['expires']);
252
			//convert from any DateTime compatible date to MM/DD/YYYY
253
			$_POST['expires'] = $expdate->format("m/d/Y");
254
		} catch (Exception $ex) {
255
			$input_errors[] = gettext("Invalid expiration date format; use MM/DD/YYYY instead.");
256
		}
257
	}
258

    
259
	if (!empty($_POST['name'])) {
260
		$ca = lookup_ca($_POST['caref']);
261
		if (!$ca) {
262
			$input_errors[] = gettext("Invalid internal Certificate Authority") . "\n";
263
		}
264
	}
265

    
266
	/* if this is an AJAX caller then handle via JSON */
267
	if (isAjax() && is_array($input_errors)) {
268
		input_errors2Ajax($input_errors);
269
		exit;
270
	}
271

    
272
	if (!$input_errors) {
273
		// This used to be a separate act=delpriv
274
		if ($a_user[$id] && !empty($_POST['privid'])) {
275
			foreach ($_POST['privid'] as $i)
276
				unset($a_user[$id]['priv'][$i]);
277

    
278
			local_user_set($a_user[$id]);
279
			write_config();
280
		}
281

    
282
		conf_mount_rw();
283
		$userent = array();
284
		if (isset($id) && $a_user[$id]) {
285
			$userent = $a_user[$id];
286
		}
287

    
288
		isset($_POST['utype']) ? $userent['scope'] = $_POST['utype'] : $userent['scope'] = "system";
289

    
290
		/* the user name was modified */
291
		if (!empty($_POST['oldusername']) && ($_POST['usernamefld'] <> $_POST['oldusername'])) {
292
			$_SERVER['REMOTE_USER'] = $_POST['usernamefld'];
293
			local_user_del($userent);
294
		}
295

    
296
		/* the user password was modified */
297
		if ($_POST['passwordfld1']) {
298
			local_user_set_password($userent, $_POST['passwordfld1']);
299
		}
300

    
301
		$userent['name'] = $_POST['usernamefld'];
302
		$userent['descr'] = $_POST['descr'];
303
		$userent['expires'] = $_POST['expires'];
304
		$userent['authorizedkeys'] = base64_encode($_POST['authorizedkeys']);
305
		$userent['ipsecpsk'] = $_POST['ipsecpsk'];
306

    
307
		if ($_POST['disabled']) {
308
			$userent['disabled'] = true;
309
		} else {
310
			unset($userent['disabled']);
311
		}
312

    
313
		if (isset($id) && $a_user[$id]) {
314
			$a_user[$id] = $userent;
315
		} else {
316
			if (!empty($_POST['name'])) {
317
				$cert = array();
318
				$cert['refid'] = uniqid();
319
				$userent['cert'] = array();
320

    
321
				$cert['descr'] = $_POST['name'];
322

    
323
				$subject = cert_get_subject_array($ca['crt']);
324

    
325
				$dn = array(
326
					'countryName' => $subject[0]['v'],
327
					'stateOrProvinceName' => $subject[1]['v'],
328
					'localityName' => $subject[2]['v'],
329
					'organizationName' => $subject[3]['v'],
330
					'emailAddress' => $subject[4]['v'],
331
					'commonName' => $userent['name']);
332

    
333
				cert_create($cert, $_POST['caref'], $_POST['keylen'],
334
					(int)$_POST['lifetime'], $dn);
335

    
336
				if (!is_array($config['cert'])) {
337
					$config['cert'] = array();
338
				}
339
				$config['cert'][] = $cert;
340
				$userent['cert'][] = $cert['refid'];
341
			}
342
			$userent['uid'] = $config['system']['nextuid']++;
343
			/* Add the user to All Users group. */
344
			foreach ($config['system']['group'] as $gidx => $group) {
345
				if ($group['name'] == "all") {
346
					if (!is_array($config['system']['group'][$gidx]['member'])) {
347
						$config['system']['group'][$gidx]['member'] = array();
348
					}
349
					$config['system']['group'][$gidx]['member'][] = $userent['uid'];
350
					break;
351
				}
352
			}
353

    
354
			$a_user[] = $userent;
355
		}
356

    
357
		local_user_set($userent);
358
		local_user_set_groups($userent, $_POST['groups']);
359
		write_config();
360

    
361
		if (is_dir("/etc/inc/privhooks")) {
362
			run_plugins("/etc/inc/privhooks");
363
		}
364

    
365
		conf_mount_ro();
366

    
367
		pfSenseHeader("system_usermanager.php");
368
	}
369
}
370

    
371
function build_priv_table() {
372
	global $a_user, $id;
373

    
374
	$privhtml = '<div class="table-responsive">';
375
	$privhtml .=	'<table class="table table-striped table-hover table-condensed">';
376
	$privhtml .=		'<thead>';
377
	$privhtml .=			'<tr>';
378
	$privhtml .=				'<th>' . gettext('Inherited from') . '</th>';
379
	$privhtml .=				'<th>' . gettext('Name') . '</th>';
380
	$privhtml .=				'<th>' . gettext('Description') . '</th>';
381
	$privhtml .=			'</tr>';
382
	$privhtml .=		'</thead>';
383
	$privhtml .=		'<tbody>';
384

    
385
	foreach (get_user_privdesc($a_user[$id]) as $i => $priv) {
386
		$privhtml .=		'<tr>';
387
		$privhtml .=			'<td>' . htmlspecialchars($priv['group']) . '</td>';
388
		$privhtml .=			'<td>' . htmlspecialchars($priv['name']) . '</td>';
389
		$privhtml .=			'<td>' . htmlspecialchars($priv['descr']) . '</td>';
390
		$privhtml .=		'</tr>';
391
	}
392

    
393
	$privhtml .=		'</tbody>';
394
	$privhtml .=	'</table>';
395
	$privhtml .= '</div>';
396

    
397
	$privhtml .= '<nav class="action-buttons">';
398
	$privhtml .=	'<a href="system_usermanager_addprivs.php?userid=' . $id . '" class="btn btn-success">' . gettext("Add") . '</a>';
399
	$privhtml .= '</nav>';
400

    
401
	return($privhtml);
402
}
403

    
404
function build_cert_table() {
405
	global $a_user, $id;
406

    
407
	$certhtml = '<div class="table-responsive">';
408
	$certhtml .=	'<table class="table table-striped table-hover table-condensed">';
409
	$certhtml .=		'<thead>';
410
	$certhtml .=			'<tr>';
411
	$certhtml .=				'<th>' . gettext('Name') . '</th>';
412
	$certhtml .=				'<th>' . gettext('CA') . '</th>';
413
	$certhtml .=				'<th></th>';
414
	$certhtml .=			'</tr>';
415
	$certhtml .=		'</thead>';
416
	$certhtml .=		'<tbody>';
417

    
418
	$a_cert = $a_user[$id]['cert'];
419
	if (is_array($a_cert)) {
420
		$i = 0;
421
		foreach ($a_cert as $certref) {
422
			$cert = lookup_cert($certref);
423
			$ca = lookup_ca($cert['caref']);
424
			$revokedstr =	is_cert_revoked($cert) ? '<b> Revoked</b>':'';
425

    
426
			$certhtml .=	'<tr>';
427
			$certhtml .=		'<td>' . htmlspecialchars($cert['descr']) . $revokedstr . '</td>';
428
			$certhtml .=		'<td>' . htmlspecialchars($ca['descr']) . '</td>';
429
			$certhtml .=		'<td>';
430
			$certhtml .=			'<a id="delcert' . $i .'" class="btn btn-xs btn-warning" title="';
431
			$certhtml .=			gettext('Remove this certificate association? (Certificate will not be deleted)') . '">Delete</a>';
432
			$certhtml .=		'</td>';
433
			$certhtml .=	'</tr>';
434
			$i++;
435
		}
436

    
437
	}
438

    
439
	$certhtml .=		'</tbody>';
440
	$certhtml .=	'</table>';
441
	$certhtml .= '</div>';
442

    
443
	$certhtml .= '<nav class="action-buttons">';
444
	$certhtml .=	'<a href="system_certmanager.php?act=new&amp;userid=' . $id . '" class="btn btn-success">' . gettext("Add") . '</a>';
445
	$certhtml .= '</nav>';
446

    
447
	return($certhtml);
448
}
449

    
450
$closehead = false;
451
include("head.inc");
452

    
453
if ($input_errors)
454
	print_input_errors($input_errors);
455

    
456
if ($savemsg)
457
	print_info_box($savemsg, 'success');
458

    
459
$tab_array = array();
460
$tab_array[] = array(gettext("Users"), true, "system_usermanager.php");
461
$tab_array[] = array(gettext("Groups"), false, "system_groupmanager.php");
462
$tab_array[] = array(gettext("Settings"), false, "system_usermanager_settings.php");
463
$tab_array[] = array(gettext("Servers"), false, "system_authservers.php");
464
display_top_tabs($tab_array);
465

    
466
if (!($act == "new" || $act == "edit" || $input_errors)) {
467
?>
468

    
469
<div class="table-responsive">
470
	<table class="table table-striped table-hover">
471
		<thead>
472
			<tr>
473
				<th>&nbsp;</th>
474
				<th><?=gettext("Username")?></th>
475
				<th><?=gettext("Full name")?></th>
476
				<th><?=gettext("Disabled")?></th>
477
				<th><?=gettext("Groups")?></th>
478
			</tr>
479
		</thead>
480
		<tbody>
481
		</tbody>
482
		<tbody>
483
<?php
484
foreach($a_user as $i => $userent):
485
	?>
486
			<tr>
487
				<td>
488
					<input type="checkbox" id="frc<?=$i?>" name="delete_check[]" value="<?=$i?>" <?=($userent['scope'] == "system" ? 'disabled="disabled"' : '')?>/>
489
				</td>
490
				<td>
491
<?php
492
	if($userent['scope'] != "user")
493
		$usrimg = 'eye-open';
494
	else
495
		$usrimg = 'user';
496
?>
497
					<i class="icon icon-<?=$usrimg?>"></i>
498
					<?=htmlspecialchars($userent['name'])?>
499
				</td>
500
				<td><?=htmlspecialchars($userent['descr'])?></td>
501
				<td><?php if(isset($userent['disabled'])) echo "*"?></td>
502
				<td><?=implode(",",local_user_get_groups($userent))?></td>
503
				<td>
504
					<a href="?act=edit&amp;userid=<?=$i?>" class="btn btn-xs btn-primary">edit</a>
505
<?php if($userent['scope'] != "system"): ?>
506
					<a href="?act=del&amp;userid=<?=$i?>" class="btn btn-xs btn-danger">delete</a>
507
<?php endif; ?>
508
				</td>
509
			</tr>
510
<?php endforeach; ?>
511
		</tbody>
512
	</table>
513
</div>
514
<nav class="action-buttons">
515
	<a href="?act=new" class="btn btn-success">add new</a>
516
</nav>
517
<p>
518
	<?=gettext("Additional users can be added here. User permissions for accessing " .
519
	"the webConfigurator can be assigned directly or inherited from group memberships. " .
520
	"An icon that appears grey indicates that it is a system defined object. " .
521
	"Some system object properties can be modified but they cannot be deleted.")?>
522
	<br /><br />
523
	<?=gettext("Accounts created here are also used for other parts of the system " .
524
	"such as OpenVPN, IPsec, and Captive Portal.")?>
525
</p>
526
<?php
527
	include("foot.inc");
528
	exit;
529
}
530

    
531
require_once('classes/Form.class.php');
532
$form = new Form;
533

    
534
if ($act == "new" || $act == "edit" || $input_errors):
535

    
536
	$form->addGlobal(new Form_Input(
537
		'act',
538
		null,
539
		'hidden',
540
		''
541
	));
542

    
543
	$form->addGlobal(new Form_Input(
544
		'userid',
545
		null,
546
		'hidden',
547
		isset($id) ? $id:''
548
	));
549

    
550
	$form->addGlobal(new Form_Input(
551
		'privid',
552
		null,
553
		'hidden',
554
		''
555
	));
556

    
557
	$form->addGlobal(new Form_Input(
558
		'certid',
559
		null,
560
		'hidden',
561
		''
562
	));
563

    
564
	$ro = "";
565
	if ($pconfig['utype'] == "system") {
566
		$ro = "readonly=\"readonly\"";
567
	}
568

    
569
	$section = new Form_Section('User Properties');
570

    
571
	$section->addInput(new Form_StaticText(
572
		'Defined by',
573
		strtoupper($pconfig['utype'])
574
	));
575

    
576
	$form->addGlobal(new Form_Input(
577
		'utype',
578
		null,
579
		'hidden',
580
		$pconfig['utype']
581
	));
582

    
583
	$section->addInput(new Form_Checkbox(
584
		'disabled',
585
		'Disabled',
586
		'This user cannot login',
587
		$pconfig['disabled']
588
	));
589

    
590
	$section->addInput($input = new Form_Input(
591
		'usernamefld',
592
		'Username',
593
		'text',
594
		$pconfig['usernamefld']
595
	));
596

    
597
	if ($ro)
598
		$input->setDisabled();
599

    
600
	$form->addGlobal(new Form_Input(
601
		'oldusername',
602
		null,
603
		'hidden',
604
		$pconfig['usernamefld']
605
	));
606

    
607
	$group = new Form_Group('Password');
608
	$group->add(new Form_Input(
609
		'passwordfld1',
610
		'Password',
611
		'password'
612
	));
613
	$group->add(new Form_Input(
614
		'passwordfld2',
615
		'Confirm Password',
616
		'password'
617
	));
618

    
619
	$section->add($group);
620

    
621
	$section->addInput($input = new Form_Input(
622
		'descr',
623
		'Full name',
624
		'text',
625
		htmlspecialchars($pconfig['descr'])
626
	))->setHelp('User\'s full name, for your own information only');
627

    
628
	if ($ro)
629
		$input->setDisabled();
630

    
631
	$section->addInput(new Form_Input(
632
		'expires',
633
		'Expiration date',
634
		'date',
635
		$pconfig['expires']
636
	))->setHelp('Leave blank if the account shouldn\'t expire, otherwise enter '.
637
		'the expiration date');
638

    
639
	// ==== Group membership ==================================================
640
	$group = new Form_Group('Group membership');
641

    
642
	// Make a list of all the groups configured on the system, and a list of
643
	// those which this user is a member of
644
	$systemGroups = array();
645
	$usersGroups = array();
646

    
647
	$usergid = [$pconfig['usernamefld']];
648

    
649
	foreach ($config['system']['group'] as $Ggroup) {
650
		if($Ggroup['name'] != "all") {
651
			if(($act == 'edit') && $Ggroup['member'] && in_array($pconfig['uid'], $Ggroup['member']))
652
				$usersGroups[ $Ggroup['name'] ] = $Ggroup['name'];	// Add it to the user's list
653
			else
654
				$systemGroups[ $Ggroup['name'] ] = $Ggroup['name']; // Add it to the 'not a member of' list
655
		}
656
	}
657

    
658
	$group->add(new Form_Select(
659
		'sysgroups',
660
		null,
661
		array_combine((array)$pconfig['groups'], (array)$pconfig['groups']),
662
		$systemGroups,
663
		true
664
	))->setHelp('Not member of');
665

    
666
	$group->add(new Form_Select(
667
		'groups',
668
		null,
669
		array_combine((array)$pconfig['groups'], (array)$pconfig['groups']),
670
		$usersGroups,
671
		true
672
	))->setHelp('Member of');
673

    
674
	$section->add($group);
675

    
676
	$group = new Form_Group('');
677

    
678
	$group->add(new Form_Button(
679
		'movetoenabled',
680
		'Move to "Member of" list >'
681
	))->removeClass('btn-primary')->addClass('btn-default btn-sm');
682

    
683
	$group->add(new Form_Button(
684
		'movetodisabled',
685
		'< Move to "Not member of" list'
686
	))->removeClass('btn-primary')->addClass('btn-default btn-sm');
687

    
688
	$group->setHelp('Hold down CTRL (pc)/COMMAND (mac) key to select multiple items');
689
	$section->add($group);
690

    
691
	// ==== Button for adding user certificate ================================
692
	if($act == 'new') {
693
		$section->addInput(new Form_Checkbox(
694
			'showcert',
695
			'Certificate',
696
			'Click to create a user certificate',
697
			false
698
		));
699
	}
700

    
701
	$form->add($section);
702

    
703
	// ==== Effective privileges section ======================================
704
	if (isset($pconfig['uid'])) {
705
		// We are going to build an HTML table and add it to an Input_StaticText. It may be ugly, but it
706
		// is the best way to make the display we need.
707

    
708
		$section = new Form_Section('Effective Privileges');
709

    
710
		$section->addInput(new Form_StaticText(
711
			null,
712
			build_priv_table()
713
		));
714

    
715
		$form->add($section);
716

    
717
		// ==== Certificate table section =====================================
718
		$section = new Form_Section('User certificates');
719

    
720
		$section->addInput(new Form_StaticText(
721
			null,
722
			build_cert_table()
723
		));
724

    
725
		$form->add($section);
726
	}
727
else;
728
		$section = new Form_Section('User Certificates');
729

    
730
		foreach ((array)$a_user[$id]['cert'] as $i => $certref) {
731
			$cert = lookup_cert($certref);
732
			$ca = lookup_ca($cert['caref']);
733

    
734
			// We reverse name and action for readability of longer names
735
			$section->addInput(new Form_Checkbox(
736
				'certid[]',
737
				'Delete certificate',
738
				$cert['descr']. (is_cert_revoked($cert) ? ' <b>revoked</b>' : ''),
739
				false,
740
				$i
741
			));
742
		}
743

    
744
		#FIXME; old ui supplied direct export links to each certificate
745

    
746
		$section->addInput(new Form_StaticText(
747
			null,
748
			new Form_Button(null, 'add certificate', 'system_certmanager.php?act=new&userid='. $id).
749
			new Form_Button(null, 'export certificates', 'system_certmanager.php')
750
		));
751

    
752
		// ==== Add user certificate for a new user
753
		if (is_array($config['ca']) && count($config['ca']) > 0) {
754
			$section = new Form_Section('Create certificate for user');
755
			$section->addClass('cert-options');
756

    
757
			$nonPrvCas = array();
758
			foreach( $config['ca'] as $ca) {
759
				if (!$ca['prv'])
760
					continue;
761

    
762
				$nonPrvCas[ $ca['refid'] ] = $ca['descr'];
763
			}
764

    
765
			if (!empty($nonPrvCas)) {
766
				$section->addInput(new Form_Input(
767
					'name',
768
					'Descriptive name',
769
					'text',
770
					$pconfig['name']
771
				));
772

    
773
				$section->addInput(new Form_Select(
774
					'caref',
775
					'Certificate authority',
776
					null,
777
					$nonPrvCas
778
				));
779

    
780
				$section->addInput(new Form_Select(
781
					'keylen',
782
					'Key length',
783
					2048,
784
					array(
785
						512 => '512 bits',
786
						1024 => '1024 bits',
787
						2048 => '2049 bits',
788
						4096 => '4096 bits',
789
					)
790
				));
791

    
792
				$section->addInput(new Form_Input(
793
					'lifetime',
794
					'Lifetime',
795
					'number',
796
					$pconfig['lifetime']
797
				));
798
			}
799

    
800
			$form->add($section);
801
		}
802

    
803
endif;
804
// ==== Paste a key for the new user
805
$section = new Form_Section('Keys');
806

    
807
$section->addInput(new Form_Textarea(
808
	'authorizedkeys',
809
	'Authorized keys',
810
	$pconfig['authorizedkeys']
811
))->setHelp('Paste an authorized keys file here.');
812

    
813
$group = new Form_Group('IPsec Pre-Shared Key');
814

    
815
$group->add(new Form_Input(
816
	'ipsecpsk',
817
	'IPsec Pre-Shared Key',
818
	'text',
819
	$pconfig['ipsecpsk']
820
));
821

    
822
$group->add(new Form_Checkbox(
823
	'showkey',
824
	'Authorized keys',
825
	'Click to paste an authorized key',
826
	false
827
));
828

    
829
$section->add($group);
830
$form->add($section);
831

    
832
print $form;
833
?>
834
<script>
835
//<![CDATA[
836
events.push(function(){
837
	// Hides all elements of the specified class.
838
	function hideClass(s_class, hide) {
839
		if(hide)
840
			$('.' + s_class).hide();
841
		else
842
			$('.' + s_class).show();
843
	}
844

    
845
	// Hides the <div> in which the specified input element lives so that the input, its label and help text are hidden
846
	function hideInput(id, hide) {
847
		if(hide)
848
			$('#' + id).parent().parent('div').addClass('hidden');
849
		else
850
			$('#' + id).parent().parent('div').removeClass('hidden');
851
	}
852

    
853
	// Select every option in the specified multiselect
854
	function AllServers(id, selectAll) {
855
	   for (i = 0; i < id.length; i++)	   {
856
		   id.eq(i).prop('selected', selectAll);
857
	   }
858
	}
859

    
860
	// Move all selected options from one multiselect to another
861
	function moveOptions(From, To)	{
862
		var len = From.length;
863
		var option;
864

    
865
		if(len > 1) {
866
			for(i=0; i<len; i++) {
867
				if(From.eq(i).is(':selected')) {
868
					option = From.eq(i).val();
869
					To.append(new Option(option, option));
870
					From.eq(i).remove();
871
				}
872
			}
873
		}
874
	}
875

    
876
	// Make buttons plain buttons, not submit
877
	$("#movetodisabled").prop('type','button');
878
	$("#movetoenabled").prop('type','button');
879

    
880
	// On click . .
881
	$("#movetodisabled").click(function() {
882
		moveOptions($('[name="groups[]"] option'), $('[name="sysgroups[]"]'));
883
	});
884

    
885
	$("#movetoenabled").click(function() {
886
		moveOptions($('[name="sysgroups[]"] option'), $('[name="groups[]"]'));
887
	});
888

    
889
	$("#showcert").click(function() {
890
		hideClass('cert-options', !this.checked);
891
	});
892

    
893
	$("#showkey").click(function() {
894
		hideInput('authorizedkeys', !this.checked);
895
	});
896

    
897
	$('[id^=delcert]').click(function(event) {
898
		if(confirm(event.target.title)) {
899
			$('#certid').val(event.target.id.match(/\d+$/)[0]);
900
			$('#userid').val('<?=$id;?>');
901
			$('#act').val('delcert');
902
			$('form').submit();
903
		}
904
	});
905

    
906
	// On page load . .
907
   hideClass('cert-options', true);
908
   hideInput('authorizedkeys', true);
909

    
910
	// On submit mark all the user's groups as "selected"
911
	$('form').submit(function(){
912
		AllServers($('[name="groups[]"] option'), true);
913
	});
914
});
915
//]]>
916
</script>
917
<?php
918

    
919
include('foot.inc');
(212-212/238)