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
				<th>&nbsp;</th>
497
			</tr>
498
		</thead>
499
		<tbody>
500
<?php
501
foreach ($a_user as $i => $userent):
502
	?>
503
			<tr>
504
				<td>
505
					<input type="checkbox" id="frc<?=$i?>" name="delete_check[]" value="<?=$i?>" <?=($userent['scope'] == "system" ? 'disabled' : '')?>/>
506
				</td>
507
				<td>
508
<?php
509
	if ($userent['scope'] != "user") {
510
		$usrimg = 'eye-open';
511
	} else {
512
		$usrimg = 'user';
513
	}
514
?>
515
					<i class="fa fa-<?=$usrimg?>"></i>
516
					<?=htmlspecialchars($userent['name'])?>
517
				</td>
518
				<td><?=htmlspecialchars($userent['descr'])?></td>
519
				<td><?php if (isset($userent['disabled'])) echo "*"?></td>
520
				<td><?=implode(",", local_user_get_groups($userent))?></td>
521
				<td>
522
					<a class="fa fa-pencil" title="<?=gettext("Edit user"); ?>" href="?act=edit&amp;userid=<?=$i?>"></a>
523
<?php if ($userent['scope'] != "system"): ?>
524
					<a class="fa fa-trash"	title="<?=gettext("Delete user")?>" href="?act=deluser&amp;userid=<?=$i?>&amp;username=<?=$userent['name']?>"></a>
525
<?php endif; ?>
526
				</td>
527
			</tr>
528
<?php endforeach; ?>
529
		</tbody>
530
	</table>
531
</div>
532
<nav class="action-buttons">
533
	<a href="?act=new" class="btn btn-sm btn-success">
534
		<i class="fa fa-plus icon-embed-btn"></i>
535
		<?=gettext("Add")?>
536
	</a>
537

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

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

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

    
560
$form = new Form;
561

    
562
if ($act == "new" || $act == "edit" || $input_errors):
563

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

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

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

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

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

    
597
	$section = new Form_Section('User Properties');
598

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

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

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

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

    
625
	if ($ro) {
626
		$input->setReadonly();
627
	}
628

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

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

    
648
	$section->add($group);
649

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

    
657
	if ($ro) {
658
		$input->setDisabled();
659
	}
660

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

    
669
	// ==== Group membership ==================================================
670
	$group = new Form_Group('Group membership');
671

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

    
677
	$usergid = [$pconfig['usernamefld']];
678

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

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

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

    
705
	$section->add($group);
706

    
707
	$group = new Form_Group('');
708

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

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

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

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

    
732
	$form->add($section);
733

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

    
739
		$section = new Form_Section('Effective Privileges');
740

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

    
746
		$form->add($section);
747

    
748
		// ==== Certificate table section =====================================
749
		$section = new Form_Section('User certificates');
750

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

    
756
		$form->add($section);
757
	}
758

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

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

    
770
			$nonPrvCas[ $ca['refid'] ] = $ca['descr'];
771
		}
772

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

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

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

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

    
808
		$form->add($section);
809
	}
810

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

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

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

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

    
835
$form->add($section);
836

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

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

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

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

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

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

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

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

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

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

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

    
907

    
908
	// ---------- On initial page load ------------------------------------------------------------
909

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

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