Project

General

Profile

Download (24.9 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	system_usermanager.php
4
*/
5
/* ====================================================================
6
 *	Copyright (c)  2004-2015  Electric Sheep Fencing, LLC. All rights reserved.
7
 *	Copyright (c)  2008 Shrew Soft Inc.
8
 *	Copyright (c)  2005 Paul Taylor <paultaylor@winn-dixie.com>
9
 *
10
 *	Some or all of this file is based on the m0n0wall project which is
11
 *	Copyright (c)  2004 Manuel Kasper (BSD 2 clause)
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
##|+PRIV
62
##|*IDENT=page-system-usermanager
63
##|*NAME=System: User Manager
64
##|*DESCR=Allow access to the 'System: User Manager' page.
65
##|*MATCH=system_usermanager.php*
66
##|-PRIV
67

    
68
require("certs.inc");
69
require("guiconfig.inc");
70

    
71
// start admin user code
72
$pgtitle = array(gettext("System"), gettext("User Manager"), gettext("Users"));
73

    
74
if (isset($_POST['userid']) && is_numericint($_POST['userid'])) {
75
	$id = $_POST['userid'];
76
}
77

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

    
82
if (!isset($config['system']['user']) || !is_array($config['system']['user'])) {
83
	$config['system']['user'] = array();
84
}
85

    
86
$a_user = &$config['system']['user'];
87
$act = $_GET['act'];
88

    
89
if (isset($_SERVER['HTTP_REFERER'])) {
90
	$referer = $_SERVER['HTTP_REFERER'];
91
} else {
92
	$referer = '/system_usermanager.php';
93
}
94

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

    
108
if ($_GET['act'] == "deluser") {
109

    
110
	if (!isset($_GET['username']) || !isset($a_user[$id]) || ($_GET['username'] != $a_user[$id]['name'])) {
111
		pfSenseHeader("system_usermanager.php");
112
		exit;
113
	}
114

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

    
133
if (isset($_POST['dellall'])) {
134

    
135
	$del_users = $_POST['delete_check'];
136

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

    
151
if ($_POST['act'] == "delcert") {
152

    
153
	if (!$a_user[$id]) {
154
		pfSenseHeader("system_usermanager.php");
155
		exit;
156
	}
157

    
158
	$certdeleted = lookup_cert($a_user[$id]['cert'][$_POST['certid']]);
159
	$certdeleted = $certdeleted['descr'];
160
	unset($a_user[$id]['cert'][$_POST['certid']]);
161
	write_config();
162
	$_POST['act'] = "edit";
163
	$savemsg = gettext("Certificate") . " {$certdeleted} " . gettext("association removed.") . "<br />";
164
}
165

    
166
if ($_POST['act'] == "delprivid") {
167
	$privdeleted = $priv_list[$a_user[$id]['priv'][$_POST['privid']]]['name'];
168
	unset($a_user[$id]['priv'][$_POST['privid']]);
169
	local_user_set($a_user[$id]);
170
	write_config();
171
	$_POST['act'] = "edit";
172
	$savemsg = gettext("Privilege ") . $privdeleted . gettext(" removed") . "<br />";
173
}
174

    
175
if ($_POST['save']) {
176
	unset($input_errors);
177
	$pconfig = $_POST;
178

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

    
201
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
202

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

    
207
	if (strlen($_POST['usernamefld']) > 16) {
208
		$input_errors[] = gettext("The username is longer than 16 characters.");
209
	}
210

    
211
	if (($_POST['passwordfld1']) && ($_POST['passwordfld1'] != $_POST['passwordfld2'])) {
212
		$input_errors[] = gettext("The passwords do not match.");
213
	}
214

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

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

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

    
263
	if (!empty($_POST['name'])) {
264
		$ca = lookup_ca($_POST['caref']);
265
		if (!$ca) {
266
			$input_errors[] = gettext("Invalid internal Certificate Authority") . "\n";
267
		}
268
	}
269

    
270
	/* if this is an AJAX caller then handle via JSON */
271
	if (isAjax() && is_array($input_errors)) {
272
		input_errors2Ajax($input_errors);
273
		exit;
274
	}
275

    
276
	if (!$input_errors) {
277

    
278

    
279
		conf_mount_rw();
280
		$userent = array();
281
		if (isset($id) && $a_user[$id]) {
282
			$userent = $a_user[$id];
283
		}
284

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

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

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

    
298
		$userent['name'] = $_POST['usernamefld'];
299
		$userent['descr'] = $_POST['descr'];
300
		$userent['expires'] = $_POST['expires'];
301
		$userent['authorizedkeys'] = base64_encode($_POST['authorizedkeys']);
302
		$userent['ipsecpsk'] = $_POST['ipsecpsk'];
303

    
304
		if ($_POST['disabled']) {
305
			$userent['disabled'] = true;
306
		} else {
307
			unset($userent['disabled']);
308
		}
309

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

    
318
				$cert['descr'] = $_POST['name'];
319

    
320
				$subject = cert_get_subject_array($ca['crt']);
321

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

    
330
				cert_create($cert, $_POST['caref'], $_POST['keylen'],
331
					(int)$_POST['lifetime'], $dn);
332

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

    
351
			$a_user[] = $userent;
352
		}
353

    
354
		/* Add user to groups so PHP can see the memberships properly or else the user's shell account does not get proper permissions (if applicable) See #5152. */
355
		local_user_set_groups($userent, $_POST['groups']);
356
		local_user_set($userent);
357
		/* Add user to groups again to ensure they are set everywhere, otherwise the user may not appear to be a member of the group. See commit:5372d26d9d25d751d16865ed9d46869d3b0ec5e1. */
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
	$i = 0;
386

    
387
	foreach (get_user_privdesc($a_user[$id]) as $priv) {
388
		$group = false;
389
		if ($priv['group']) {
390
			$group = $priv['group'];
391
		}
392

    
393
		$privhtml .=		'<tr>';
394
		$privhtml .=			'<td>' . htmlspecialchars($priv['group']) . '</td>';
395
		$privhtml .=			'<td>' . htmlspecialchars($priv['name']) . '</td>';
396
		$privhtml .=			'<td>' . htmlspecialchars($priv['descr']) . '</td>';
397
		$privhtml .=			'<td>';
398
		if (!$group) {
399
			$privhtml .=			'<a class="fa fa-trash no-confirm icon-pointer" title="'.gettext('Delete Privilege').'" id="delprivid' .$i. '"></a></td>';
400
		}
401

    
402
		$privhtml .=			'</td>';
403
		$privhtml .=		'</tr>';
404

    
405
		if (!$group) {
406
			$i++;
407
		}
408
	}
409

    
410
	$privhtml .=		'</tbody>';
411
	$privhtml .=	'</table>';
412
	$privhtml .= '</div>';
413

    
414
	$privhtml .= '<nav class="action-buttons">';
415
	$privhtml .=	'<a href="system_usermanager_addprivs.php?userid=' . $id . '" class="btn btn-success">' . gettext("Add") . '</a>';
416
	$privhtml .= '</nav>';
417

    
418
	return($privhtml);
419
}
420

    
421
function build_cert_table() {
422
	global $a_user, $id;
423

    
424
	$certhtml = '<div class="table-responsive">';
425
	$certhtml .=	'<table class="table table-striped table-hover table-condensed">';
426
	$certhtml .=		'<thead>';
427
	$certhtml .=			'<tr>';
428
	$certhtml .=				'<th>' . gettext('Name') . '</th>';
429
	$certhtml .=				'<th>' . gettext('CA') . '</th>';
430
	$certhtml .=				'<th></th>';
431
	$certhtml .=			'</tr>';
432
	$certhtml .=		'</thead>';
433
	$certhtml .=		'<tbody>';
434

    
435
	$a_cert = $a_user[$id]['cert'];
436
	if (is_array($a_cert)) {
437
		$i = 0;
438
		foreach ($a_cert as $certref) {
439
			$cert = lookup_cert($certref);
440
			$ca = lookup_ca($cert['caref']);
441
			$revokedstr =	is_cert_revoked($cert) ? '<b> Revoked</b>':'';
442

    
443
			$certhtml .=	'<tr>';
444
			$certhtml .=		'<td>' . htmlspecialchars($cert['descr']) . $revokedstr . '</td>';
445
			$certhtml .=		'<td>' . htmlspecialchars($ca['descr']) . '</td>';
446
			$certhtml .=		'<td>';
447
			$certhtml .=			'<a id="delcert' . $i .'" class="fa fa-trash no-confirm icon-pointer" title="';
448
			$certhtml .=			gettext('Remove this certificate association? (Certificate will not be deleted)') . '"></a>';
449
			$certhtml .=		'</td>';
450
			$certhtml .=	'</tr>';
451
			$i++;
452
		}
453

    
454
	}
455

    
456
	$certhtml .=		'</tbody>';
457
	$certhtml .=	'</table>';
458
	$certhtml .= '</div>';
459

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

    
464
	return($certhtml);
465
}
466

    
467
include("head.inc");
468

    
469
if ($input_errors) {
470
	print_input_errors($input_errors);
471
}
472

    
473
if ($savemsg) {
474
	print_info_box($savemsg, 'success');
475
}
476

    
477
$tab_array = array();
478
$tab_array[] = array(gettext("Users"), true, "system_usermanager.php");
479
$tab_array[] = array(gettext("Groups"), false, "system_groupmanager.php");
480
$tab_array[] = array(gettext("Settings"), false, "system_usermanager_settings.php");
481
$tab_array[] = array(gettext("Servers"), false, "system_authservers.php");
482
display_top_tabs($tab_array);
483

    
484
if (!($act == "new" || $act == "edit" || $input_errors)) {
485
?>
486
<form method="post">
487
<div class="table-responsive">
488
	<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap" data-sortable>
489
		<thead>
490
			<tr>
491
				<th>&nbsp;</th>
492
				<th><?=gettext("Username")?></th>
493
				<th><?=gettext("Full name")?></th>
494
				<th><?=gettext("Disabled")?></th>
495
				<th><?=gettext("Groups")?></th>
496
			</tr>
497
		</thead>
498
		<tbody>
499
<?php
500
foreach ($a_user as $i => $userent):
501
	?>
502
			<tr>
503
				<td>
504
					<input type="checkbox" id="frc<?=$i?>" name="delete_check[]" value="<?=$i?>" <?=($userent['scope'] == "system" ? 'disabled' : '')?>/>
505
				</td>
506
				<td>
507
<?php
508
	if ($userent['scope'] != "user") {
509
		$usrimg = 'eye-open';
510
	} else {
511
		$usrimg = 'user';
512
	}
513
?>
514
					<i class="fa fa-<?=$usrimg?>"></i>
515
					<?=htmlspecialchars($userent['name'])?>
516
				</td>
517
				<td><?=htmlspecialchars($userent['descr'])?></td>
518
				<td><?php if (isset($userent['disabled'])) echo "*"?></td>
519
				<td><?=implode(",", local_user_get_groups($userent))?></td>
520
				<td>
521
					<a class="fa fa-pencil" title="<?=gettext("Edit user"); ?>" href="?act=edit&amp;userid=<?=$i?>"></a>
522
<?php if ($userent['scope'] != "system"): ?>
523
					<a class="fa fa-trash"	title="<?=gettext("Delete user")?>" href="?act=deluser&amp;userid=<?=$i?>&amp;username=<?=$userent['name']?>"></a>
524
<?php endif; ?>
525
				</td>
526
			</tr>
527
<?php endforeach; ?>
528
		</tbody>
529
	</table>
530
</div>
531
<nav class="action-buttons">
532
	<a href="?act=new" class="btn btn-sm btn-success">
533
		<i class="fa fa-plus icon-embed-btn"></i>
534
		<?=gettext("Add")?>
535
	</a>
536

    
537
	<button type="submit" class="btn btn-sm btn-danger" name="dellall" value="dellall" title="<?=gettext('Delete selected users')?>">
538
		<i class="fa fa-trash icon-embed-btn"></i>
539
		<?=gettext("Delete")?>
540
	</button>
541
</nav>
542

    
543
<div id="infoblock">
544
	<?=print_info_box(gettext("Additional users can be added here. User permissions for accessing " .
545
	"the webConfigurator can be assigned directly or inherited from group memberships. " .
546
	"An icon that appears grey indicates that it is a system defined object. " .
547
	"Some system object properties can be modified but they cannot be deleted.") .
548
	'<br /><br />' .
549
	gettext("Accounts added here are also used for other parts of the system " .
550
	"such as OpenVPN, IPsec, and Captive Portal."), info)?>
551
</div>
552

    
553
<?php
554
	include("foot.inc");
555
	exit;
556
}
557

    
558
$form = new Form;
559

    
560
if ($act == "new" || $act == "edit" || $input_errors):
561

    
562
	$form->addGlobal(new Form_Input(
563
		'act',
564
		null,
565
		'hidden',
566
		''
567
	));
568

    
569
	$form->addGlobal(new Form_Input(
570
		'userid',
571
		null,
572
		'hidden',
573
		isset($id) ? $id:''
574
	));
575

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

    
583
	$form->addGlobal(new Form_Input(
584
		'certid',
585
		null,
586
		'hidden',
587
		''
588
	));
589

    
590
	$ro = "";
591
	if ($pconfig['utype'] == "system") {
592
		$ro = "readonly";
593
	}
594

    
595
	$section = new Form_Section('User Properties');
596

    
597
	$section->addInput(new Form_StaticText(
598
		'Defined by',
599
		strtoupper($pconfig['utype'])
600
	));
601

    
602
	$form->addGlobal(new Form_Input(
603
		'utype',
604
		null,
605
		'hidden',
606
		$pconfig['utype']
607
	));
608

    
609
	$section->addInput(new Form_Checkbox(
610
		'disabled',
611
		'Disabled',
612
		'This user cannot login',
613
		$pconfig['disabled']
614
	));
615

    
616
	$section->addInput($input = new Form_Input(
617
		'usernamefld',
618
		'Username',
619
		'text',
620
		$pconfig['usernamefld']
621
	));
622

    
623
	if ($ro) {
624
		$input->setReadonly();
625
	}
626

    
627
	$form->addGlobal(new Form_Input(
628
		'oldusername',
629
		null,
630
		'hidden',
631
		$pconfig['usernamefld']
632
	));
633

    
634
	$group = new Form_Group('Password');
635
	$group->add(new Form_Input(
636
		'passwordfld1',
637
		'Password',
638
		'password'
639
	));
640
	$group->add(new Form_Input(
641
		'passwordfld2',
642
		'Confirm Password',
643
		'password'
644
	));
645

    
646
	$section->add($group);
647

    
648
	$section->addInput($input = new Form_Input(
649
		'descr',
650
		'Full name',
651
		'text',
652
		htmlspecialchars($pconfig['descr'])
653
	))->setHelp('User\'s full name, for your own information only');
654

    
655
	if ($ro) {
656
		$input->setDisabled();
657
	}
658

    
659
	$section->addInput(new Form_Input(
660
		'expires',
661
		'Expiration date',
662
		'date',
663
		$pconfig['expires']
664
	))->setHelp('Leave blank if the account shouldn\'t expire, otherwise enter '.
665
		'the expiration date');
666

    
667
	// ==== Group membership ==================================================
668
	$group = new Form_Group('Group membership');
669

    
670
	// Make a list of all the groups configured on the system, and a list of
671
	// those which this user is a member of
672
	$systemGroups = array();
673
	$usersGroups = array();
674

    
675
	$usergid = [$pconfig['usernamefld']];
676

    
677
	foreach ($config['system']['group'] as $Ggroup) {
678
		if ($Ggroup['name'] != "all") {
679
			if (($act == 'edit') && $Ggroup['member'] && in_array($pconfig['uid'], $Ggroup['member'])) {
680
				$usersGroups[ $Ggroup['name'] ] = $Ggroup['name'];	// Add it to the user's list
681
			} else {
682
				$systemGroups[ $Ggroup['name'] ] = $Ggroup['name']; // Add it to the 'not a member of' list
683
			}
684
		}
685
	}
686

    
687
	$group->add(new Form_Select(
688
		'sysgroups',
689
		null,
690
		array_combine((array)$pconfig['groups'], (array)$pconfig['groups']),
691
		$systemGroups,
692
		true
693
	))->setHelp('Not member of');
694

    
695
	$group->add(new Form_Select(
696
		'groups',
697
		null,
698
		array_combine((array)$pconfig['groups'], (array)$pconfig['groups']),
699
		$usersGroups,
700
		true
701
	))->setHelp('Member of');
702

    
703
	$section->add($group);
704

    
705
	$group = new Form_Group('');
706

    
707
	$group->add(new Form_Button(
708
		'movetoenabled',
709
		'Move to "Member of" list >'
710
	))->removeClass('btn-primary')->addClass('btn-default btn-sm');
711

    
712
	$group->add(new Form_Button(
713
		'movetodisabled',
714
		'< Move to "Not member of" list'
715
	))->removeClass('btn-primary')->addClass('btn-default btn-sm');
716

    
717
	$group->setHelp('Hold down CTRL (pc)/COMMAND (mac) key to select multiple items');
718
	$section->add($group);
719

    
720
	// ==== Button for adding user certificate ================================
721
	if ($act == 'new') {
722
		$section->addInput(new Form_Checkbox(
723
			'showcert',
724
			'Certificate',
725
			'Click to create a user certificate',
726
			false
727
		));
728
	}
729

    
730
	$form->add($section);
731

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

    
737
		$section = new Form_Section('Effective Privileges');
738

    
739
		$section->addInput(new Form_StaticText(
740
			null,
741
			build_priv_table()
742
		));
743

    
744
		$form->add($section);
745

    
746
		// ==== Certificate table section =====================================
747
		$section = new Form_Section('User certificates');
748

    
749
		$section->addInput(new Form_StaticText(
750
			null,
751
			build_cert_table()
752
		));
753

    
754
		$form->add($section);
755
	}
756

    
757
	// ==== Add user certificate for a new user
758
	if (is_array($config['ca']) && count($config['ca']) > 0) {
759
		$section = new Form_Section('Create certificate for user');
760
		$section->addClass('cert-options');
761

    
762
		$nonPrvCas = array();
763
		foreach($config['ca'] as $ca) {
764
			if (!$ca['prv']) {
765
				continue;
766
			}
767

    
768
			$nonPrvCas[ $ca['refid'] ] = $ca['descr'];
769
		}
770

    
771
		if (!empty($nonPrvCas)) {
772
			$section->addInput(new Form_Input(
773
				'name',
774
				'Descriptive name',
775
				'text',
776
				$pconfig['name']
777
			));
778

    
779
			$section->addInput(new Form_Select(
780
				'caref',
781
				'Certificate authority',
782
				null,
783
				$nonPrvCas
784
			));
785

    
786
			$section->addInput(new Form_Select(
787
				'keylen',
788
				'Key length',
789
				2048,
790
				array(
791
					512 => '512 bits',
792
					1024 => '1024 bits',
793
					2048 => '2049 bits',
794
					4096 => '4096 bits',
795
				)
796
			));
797

    
798
			$section->addInput(new Form_Input(
799
				'lifetime',
800
				'Lifetime',
801
				'number',
802
				$pconfig['lifetime']
803
			));
804
		}
805

    
806
		$form->add($section);
807
	}
808

    
809
endif;
810
// ==== Paste a key for the new user
811
$section = new Form_Section('Keys');
812

    
813
$section->addInput(new Form_Checkbox(
814
	'showkey',
815
	'Authorized keys',
816
	'Click to paste an authorized key',
817
	false
818
));
819

    
820
$section->addInput(new Form_Textarea(
821
	'authorizedkeys',
822
	'Authorized SSH Keys',
823
	$pconfig['authorizedkeys']
824
))->setHelp('Enter authorized SSH keys for this user');
825

    
826
$section->addInput(new Form_Input(
827
	'ipsecpsk',
828
	'IPsec Pre-Shared Key',
829
	'text',
830
	$pconfig['ipsecpsk']
831
));
832

    
833
$form->add($section);
834

    
835
print $form;
836
?>
837
<script type="text/javascript">
838
//<![CDATA[
839
events.push(function() {
840

    
841
	// Select every option in the specified multiselect
842
	function AllServers(id, selectAll) {
843
	   for (i = 0; i < id.length; i++)	   {
844
		   id.eq(i).prop('selected', selectAll);
845
	   }
846
	}
847

    
848
	// Move all selected options from one multiselect to another
849
	function moveOptions(From, To)	{
850
		var len = From.length;
851
		var option;
852

    
853
		if (len > 0) {
854
			for (i=0; i<len; i++) {
855
				if (From.eq(i).is(':selected')) {
856
					option = From.eq(i).val();
857
					value  = From.eq(i).text();
858
					To.append(new Option(value, option));
859
					From.eq(i).remove();
860
				}
861
			}
862
		}
863
	}
864

    
865
	// Make buttons plain buttons, not submit
866
	$("#movetodisabled").prop('type','button');
867
	$("#movetoenabled").prop('type','button');
868

    
869
	// On click . .
870
	$("#movetodisabled").click(function() {
871
		moveOptions($('[name="groups[]"] option'), $('[name="sysgroups[]"]'));
872
	});
873

    
874
	$("#movetoenabled").click(function() {
875
		moveOptions($('[name="sysgroups[]"] option'), $('[name="groups[]"]'));
876
	});
877

    
878
	$("#showcert").click(function() {
879
		hideClass('cert-options', !this.checked);
880
	});
881

    
882
	$("#showkey").click(function() {
883
		hideInput('authorizedkeys', false);
884
		hideCheckbox('showkey', true);
885
	});
886

    
887
	$('[id^=delcert]').click(function(event) {
888
		if (confirm(event.target.title)) {
889
			$('#certid').val(event.target.id.match(/\d+$/)[0]);
890
			$('#userid').val('<?=$id;?>');
891
			$('#act').val('delcert');
892
			$('form').submit();
893
		}
894
	});
895

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

    
905

    
906
	// ---------- On initial page load ------------------------------------------------------------
907

    
908
	hideClass('cert-options', true);
909
	//hideInput('authorizedkeys', true);
910
	hideCheckbox('showkey', true);
911

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

    
921
include('foot.inc');
(207-207/228)