Project

General

Profile

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

    
543
	<button type="submit" class="btn btn-sm btn-danger" name="dellall" value="dellall" title="<?=gettext('Delete selected users')?>">
544
		<i class="fa fa-trash icon-embed-btn"></i>
545
		<?=gettext("Delete")?>
546
	</button>
547
</nav>
548
</form>
549

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

    
560
<?php
561
	include("foot.inc");
562
	exit;
563
}
564

    
565
$form = new Form;
566

    
567
if ($act == "new" || $act == "edit" || $input_errors):
568

    
569
	$form->addGlobal(new Form_Input(
570
		'act',
571
		null,
572
		'hidden',
573
		''
574
	));
575

    
576
	$form->addGlobal(new Form_Input(
577
		'userid',
578
		null,
579
		'hidden',
580
		isset($id) ? $id:''
581
	));
582

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

    
590
	$form->addGlobal(new Form_Input(
591
		'certid',
592
		null,
593
		'hidden',
594
		''
595
	));
596

    
597
	$ro = "";
598
	if ($pconfig['utype'] == "system") {
599
		$ro = "readonly";
600
	}
601

    
602
	$section = new Form_Section('User Properties');
603

    
604
	$section->addInput(new Form_StaticText(
605
		'Defined by',
606
		strtoupper($pconfig['utype'])
607
	));
608

    
609
	$form->addGlobal(new Form_Input(
610
		'utype',
611
		null,
612
		'hidden',
613
		$pconfig['utype']
614
	));
615

    
616
	$section->addInput(new Form_Checkbox(
617
		'disabled',
618
		'Disabled',
619
		'This user cannot login',
620
		$pconfig['disabled']
621
	));
622

    
623
	$section->addInput($input = new Form_Input(
624
		'usernamefld',
625
		'Username',
626
		'text',
627
		$pconfig['usernamefld']
628
	));
629

    
630
	if ($ro) {
631
		$input->setReadonly();
632
	}
633

    
634
	$form->addGlobal(new Form_Input(
635
		'oldusername',
636
		null,
637
		'hidden',
638
		$pconfig['usernamefld']
639
	));
640

    
641
	$group = new Form_Group('Password');
642
	$group->add(new Form_Input(
643
		'passwordfld1',
644
		'Password',
645
		'password'
646
	));
647
	$group->add(new Form_Input(
648
		'passwordfld2',
649
		'Confirm Password',
650
		'password'
651
	));
652

    
653
	$section->add($group);
654

    
655
	$section->addInput($input = new Form_Input(
656
		'descr',
657
		'Full name',
658
		'text',
659
		htmlspecialchars($pconfig['descr'])
660
	))->setHelp('User\'s full name, for your own information only');
661

    
662
	if ($ro) {
663
		$input->setDisabled();
664
	}
665

    
666
	$section->addInput(new Form_Input(
667
		'expires',
668
		'Expiration date',
669
		'date',
670
		$pconfig['expires']
671
	))->setHelp('Leave blank if the account shouldn\'t expire, otherwise enter '.
672
		'the expiration date');
673

    
674
	// ==== Group membership ==================================================
675
	$group = new Form_Group('Group membership');
676

    
677
	// Make a list of all the groups configured on the system, and a list of
678
	// those which this user is a member of
679
	$systemGroups = array();
680
	$usersGroups = array();
681

    
682
	$usergid = [$pconfig['usernamefld']];
683

    
684
	foreach ($config['system']['group'] as $Ggroup) {
685
		if ($Ggroup['name'] != "all") {
686
			if (($act == 'edit') && $Ggroup['member'] && in_array($pconfig['uid'], $Ggroup['member'])) {
687
				$usersGroups[ $Ggroup['name'] ] = $Ggroup['name'];	// Add it to the user's list
688
			} else {
689
				$systemGroups[ $Ggroup['name'] ] = $Ggroup['name']; // Add it to the 'not a member of' list
690
			}
691
		}
692
	}
693

    
694
	$group->add(new Form_Select(
695
		'sysgroups',
696
		null,
697
		array_combine((array)$pconfig['groups'], (array)$pconfig['groups']),
698
		$systemGroups,
699
		true
700
	))->setHelp('Not member of');
701

    
702
	$group->add(new Form_Select(
703
		'groups',
704
		null,
705
		array_combine((array)$pconfig['groups'], (array)$pconfig['groups']),
706
		$usersGroups,
707
		true
708
	))->setHelp('Member of');
709

    
710
	$section->add($group);
711

    
712
	$group = new Form_Group('');
713

    
714
	$group->add(new Form_Button(
715
		'movetoenabled',
716
		'Move to "Member of" list >'
717
	))->removeClass('btn-primary')->addClass('btn-default btn-sm');
718

    
719
	$group->add(new Form_Button(
720
		'movetodisabled',
721
		'< Move to "Not member of" list'
722
	))->removeClass('btn-primary')->addClass('btn-default btn-sm');
723

    
724
	$group->setHelp('Hold down CTRL (pc)/COMMAND (mac) key to select multiple items');
725
	$section->add($group);
726

    
727
	// ==== Button for adding user certificate ================================
728
	if ($act == 'new') {
729
		$section->addInput(new Form_Checkbox(
730
			'showcert',
731
			'Certificate',
732
			'Click to create a user certificate',
733
			false
734
		));
735
	}
736

    
737
	$form->add($section);
738

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

    
744
		$section = new Form_Section('Effective Privileges');
745

    
746
		$section->addInput(new Form_StaticText(
747
			null,
748
			build_priv_table()
749
		));
750

    
751
		$form->add($section);
752

    
753
		// ==== Certificate table section =====================================
754
		$section = new Form_Section('User certificates');
755

    
756
		$section->addInput(new Form_StaticText(
757
			null,
758
			build_cert_table()
759
		));
760

    
761
		$form->add($section);
762
	}
763

    
764
	// ==== Add user certificate for a new user
765
	if (is_array($config['ca']) && count($config['ca']) > 0) {
766
		$section = new Form_Section('Create certificate for user');
767
		$section->addClass('cert-options');
768

    
769
		$nonPrvCas = array();
770
		foreach($config['ca'] as $ca) {
771
			if (!$ca['prv']) {
772
				continue;
773
			}
774

    
775
			$nonPrvCas[ $ca['refid'] ] = $ca['descr'];
776
		}
777

    
778
		if (!empty($nonPrvCas)) {
779
			$section->addInput(new Form_Input(
780
				'name',
781
				'Descriptive name',
782
				'text',
783
				$pconfig['name']
784
			));
785

    
786
			$section->addInput(new Form_Select(
787
				'caref',
788
				'Certificate authority',
789
				null,
790
				$nonPrvCas
791
			));
792

    
793
			$section->addInput(new Form_Select(
794
				'keylen',
795
				'Key length',
796
				2048,
797
				array(
798
					512 => '512 bits',
799
					1024 => '1024 bits',
800
					2048 => '2049 bits',
801
					4096 => '4096 bits',
802
				)
803
			));
804

    
805
			$section->addInput(new Form_Input(
806
				'lifetime',
807
				'Lifetime',
808
				'number',
809
				$pconfig['lifetime']
810
			));
811
		}
812

    
813
		$form->add($section);
814
	}
815

    
816
endif;
817
// ==== Paste a key for the new user
818
$section = new Form_Section('Keys');
819

    
820
$section->addInput(new Form_Checkbox(
821
	'showkey',
822
	'Authorized keys',
823
	'Click to paste an authorized key',
824
	false
825
));
826

    
827
$section->addInput(new Form_Textarea(
828
	'authorizedkeys',
829
	'Authorized SSH Keys',
830
	$pconfig['authorizedkeys']
831
))->setHelp('Enter authorized SSH keys for this user');
832

    
833
$section->addInput(new Form_Input(
834
	'ipsecpsk',
835
	'IPsec Pre-Shared Key',
836
	'text',
837
	$pconfig['ipsecpsk']
838
));
839

    
840
$form->add($section);
841

    
842
print $form;
843
?>
844
<script type="text/javascript">
845
//<![CDATA[
846
events.push(function() {
847

    
848
	// Select every option in the specified multiselect
849
	function AllServers(id, selectAll) {
850
	   for (i = 0; i < id.length; i++)	   {
851
		   id.eq(i).prop('selected', selectAll);
852
	   }
853
	}
854

    
855
	// Move all selected options from one multiselect to another
856
	function moveOptions(From, To)	{
857
		var len = From.length;
858
		var option;
859

    
860
		if (len > 0) {
861
			for (i=0; i<len; i++) {
862
				if (From.eq(i).is(':selected')) {
863
					option = From.eq(i).val();
864
					value  = From.eq(i).text();
865
					To.append(new Option(value, option));
866
					From.eq(i).remove();
867
				}
868
			}
869
		}
870
	}
871

    
872
	// Make buttons plain buttons, not submit
873
	$("#movetodisabled").prop('type','button');
874
	$("#movetoenabled").prop('type','button');
875

    
876
	// On click . .
877
	$("#movetodisabled").click(function() {
878
		moveOptions($('[name="groups[]"] option'), $('[name="sysgroups[]"]'));
879
	});
880

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

    
885
	$("#showcert").click(function() {
886
		hideClass('cert-options', !this.checked);
887
	});
888

    
889
	$("#showkey").click(function() {
890
		hideInput('authorizedkeys', false);
891
		hideCheckbox('showkey', true);
892
	});
893

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

    
903
	$('[id^=delprivid]').click(function(event) {
904
		if (confirm(event.target.title)) {
905
			$('#privid').val(event.target.id.match(/\d+$/)[0]);
906
			$('#userid').val('<?=$id;?>');
907
			$('#act').val('delprivid');
908
			$('form').submit();
909
		}
910
	});
911

    
912

    
913
	// ---------- On initial page load ------------------------------------------------------------
914

    
915
	hideClass('cert-options', true);
916
	//hideInput('authorizedkeys', true);
917
	hideCheckbox('showkey', true);
918

    
919
	// On submit mark all the user's groups as "selected"
920
	$('form').submit(function() {
921
		AllServers($('[name="groups[]"] option'), true);
922
	});
923
});
924
//]]>
925
</script>
926
<?php
927
include('foot.inc');
928
?>
(208-208/229)