Project

General

Profile

Download (25.3 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
if (isset($_POST['userid']) && is_numericint($_POST['userid'])) {
73
	$id = $_POST['userid'];
74
}
75

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

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

    
84
$a_user = &$config['system']['user'];
85
$act = $_GET['act'];
86

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

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

    
106
if ($_GET['act'] == "deluser") {
107

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

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

    
130
if (isset($_POST['dellall'])) {
131

    
132
	$del_users = $_POST['delete_check'];
133

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

    
148
if ($_POST['act'] == "delcert") {
149

    
150
	if (!$a_user[$id]) {
151
		pfSenseHeader("system_usermanager.php");
152
		exit;
153
	}
154

    
155
	$certdeleted = lookup_cert($a_user[$id]['cert'][$_POST['certid']]);
156
	$certdeleted = $certdeleted['descr'];
157
	unset($a_user[$id]['cert'][$_POST['certid']]);
158
	write_config();
159
	$_POST['act'] = "edit";
160
	$savemsg = sprintf(gettext("Certificate %s association removed."), $certdeleted);
161
}
162

    
163
if ($_POST['act'] == "delprivid") {
164
	$privdeleted = $priv_list[$a_user[$id]['priv'][$_POST['privid']]]['name'];
165
	unset($a_user[$id]['priv'][$_POST['privid']]);
166
	local_user_set($a_user[$id]);
167
	write_config();
168
	$_POST['act'] = "edit";
169
	$savemsg = sprintf(gettext("Privilege %s removed."), $privdeleted);
170
}
171

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

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

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

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

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

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

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

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

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

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

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

    
273
	if (!$input_errors) {
274

    
275
		conf_mount_rw();
276
		$userent = array();
277
		if (isset($id) && $a_user[$id]) {
278
			$userent = $a_user[$id];
279
		}
280

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

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

    
289
		/* the user password was modified */
290
		if ($_POST['passwordfld1']) {
291
			local_user_set_password($userent, $_POST['passwordfld1']);
292
		}
293

    
294
		/* only change description if sent */
295
		if (isset($_POST['descr'])) {
296
			$userent['descr'] = $_POST['descr'];
297
		}
298

    
299
		$userent['name'] = $_POST['usernamefld'];
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
$pgtitle = array(gettext("System"), gettext("User Manager"), gettext("Users"));
468

    
469
if ($act == "new" || $act == "edit" || $input_errors) {
470
	$pgtitle[] = gettext('Edit');
471
}
472
include("head.inc");
473

    
474
if ($input_errors) {
475
	print_input_errors($input_errors);
476
}
477

    
478
if ($savemsg) {
479
	print_info_box($savemsg, 'success');
480
}
481

    
482
$tab_array = array();
483
$tab_array[] = array(gettext("Users"), true, "system_usermanager.php");
484
$tab_array[] = array(gettext("Groups"), false, "system_groupmanager.php");
485
$tab_array[] = array(gettext("Settings"), false, "system_usermanager_settings.php");
486
$tab_array[] = array(gettext("Authentication Servers"), false, "system_authservers.php");
487
display_top_tabs($tab_array);
488

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

    
548
	<button type="submit" class="btn btn-sm btn-danger" name="dellall" value="dellall" title="<?=gettext('Delete selected users')?>">
549
		<i class="fa fa-trash icon-embed-btn"></i>
550
		<?=gettext("Delete")?>
551
	</button>
552
</nav>
553
</form>
554

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

    
565
<?php
566
	include("foot.inc");
567
	exit;
568
}
569

    
570
$form = new Form;
571

    
572
if ($act == "new" || $act == "edit" || $input_errors):
573

    
574
	$form->addGlobal(new Form_Input(
575
		'act',
576
		null,
577
		'hidden',
578
		''
579
	));
580

    
581
	$form->addGlobal(new Form_Input(
582
		'userid',
583
		null,
584
		'hidden',
585
		isset($id) ? $id:''
586
	));
587

    
588
	$form->addGlobal(new Form_Input(
589
		'privid',
590
		null,
591
		'hidden',
592
		''
593
	));
594

    
595
	$form->addGlobal(new Form_Input(
596
		'certid',
597
		null,
598
		'hidden',
599
		''
600
	));
601

    
602
	$ro = "";
603
	if ($pconfig['utype'] == "system") {
604
		$ro = "readonly";
605
	}
606

    
607
	$section = new Form_Section('User Properties');
608

    
609
	$section->addInput(new Form_StaticText(
610
		'Defined by',
611
		strtoupper($pconfig['utype'])
612
	));
613

    
614
	$form->addGlobal(new Form_Input(
615
		'utype',
616
		null,
617
		'hidden',
618
		$pconfig['utype']
619
	));
620

    
621
	$section->addInput(new Form_Checkbox(
622
		'disabled',
623
		'Disabled',
624
		'This user cannot login',
625
		$pconfig['disabled']
626
	));
627

    
628
	$section->addInput($input = new Form_Input(
629
		'usernamefld',
630
		'Username',
631
		'text',
632
		$pconfig['usernamefld']
633
	));
634

    
635
	if ($ro) {
636
		$input->setReadonly();
637
	}
638

    
639
	$form->addGlobal(new Form_Input(
640
		'oldusername',
641
		null,
642
		'hidden',
643
		$pconfig['usernamefld']
644
	));
645

    
646
	$group = new Form_Group('Password');
647
	$group->add(new Form_Input(
648
		'passwordfld1',
649
		'Password',
650
		'password'
651
	));
652
	$group->add(new Form_Input(
653
		'passwordfld2',
654
		'Confirm Password',
655
		'password'
656
	));
657

    
658
	$section->add($group);
659

    
660
	$section->addInput($input = new Form_Input(
661
		'descr',
662
		'Full name',
663
		'text',
664
		htmlspecialchars($pconfig['descr'])
665
	))->setHelp('User\'s full name, for your own information only');
666

    
667
	if ($ro) {
668
		$input->setDisabled();
669
	}
670

    
671
	$section->addInput(new Form_Input(
672
		'expires',
673
		'Expiration date',
674
		'date',
675
		$pconfig['expires']
676
	))->setHelp('Leave blank if the account shouldn\'t expire, otherwise enter '.
677
		'the expiration date');
678

    
679
	// ==== Group membership ==================================================
680
	$group = new Form_Group('Group membership');
681

    
682
	// Make a list of all the groups configured on the system, and a list of
683
	// those which this user is a member of
684
	$systemGroups = array();
685
	$usersGroups = array();
686

    
687
	$usergid = [$pconfig['usernamefld']];
688

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

    
699
	$group->add(new Form_Select(
700
		'sysgroups',
701
		null,
702
		array_combine((array)$pconfig['groups'], (array)$pconfig['groups']),
703
		$systemGroups,
704
		true
705
	))->setHelp('Not member of');
706

    
707
	$group->add(new Form_Select(
708
		'groups',
709
		null,
710
		array_combine((array)$pconfig['groups'], (array)$pconfig['groups']),
711
		$usersGroups,
712
		true
713
	))->setHelp('Member of');
714

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

    
717
	$group = new Form_Group('');
718

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

    
724
	$group->add(new Form_Button(
725
		'movetodisabled',
726
		'< Move to "Not member of" list'
727
	))->removeClass('btn-primary')->addClass('btn-default btn-sm');
728

    
729
	$group->setHelp('Hold down CTRL (pc)/COMMAND (mac) key to select multiple items');
730
	$section->add($group);
731

    
732
	// ==== Button for adding user certificate ================================
733
	if ($act == 'new') {
734
		$section->addInput(new Form_Checkbox(
735
			'showcert',
736
			'Certificate',
737
			'Click to create a user certificate',
738
			false
739
		));
740
	}
741

    
742
	$form->add($section);
743

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

    
749
		$section = new Form_Section('Effective Privileges');
750

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

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

    
758
		// ==== Certificate table section =====================================
759
		$section = new Form_Section('User Certificates');
760

    
761
		$section->addInput(new Form_StaticText(
762
			null,
763
			build_cert_table()
764
		));
765

    
766
		$form->add($section);
767
	}
768

    
769
	// ==== Add user certificate for a new user
770
	if (is_array($config['ca']) && count($config['ca']) > 0) {
771
		$section = new Form_Section('Create Certificate for User');
772
		$section->addClass('cert-options');
773

    
774
		$nonPrvCas = array();
775
		foreach($config['ca'] as $ca) {
776
			if (!$ca['prv']) {
777
				continue;
778
			}
779

    
780
			$nonPrvCas[ $ca['refid'] ] = $ca['descr'];
781
		}
782

    
783
		if (!empty($nonPrvCas)) {
784
			$section->addInput(new Form_Input(
785
				'name',
786
				'Descriptive name',
787
				'text',
788
				$pconfig['name']
789
			));
790

    
791
			$section->addInput(new Form_Select(
792
				'caref',
793
				'Certificate authority',
794
				null,
795
				$nonPrvCas
796
			));
797

    
798
			$section->addInput(new Form_Select(
799
				'keylen',
800
				'Key length',
801
				2048,
802
				array(
803
					512 => '512 bits',
804
					1024 => '1024 bits',
805
					2048 => '2049 bits',
806
					4096 => '4096 bits',
807
				)
808
			));
809

    
810
			$section->addInput(new Form_Input(
811
				'lifetime',
812
				'Lifetime',
813
				'number',
814
				$pconfig['lifetime']
815
			));
816
		}
817

    
818
		$form->add($section);
819
	}
820

    
821
endif;
822
// ==== Paste a key for the new user
823
$section = new Form_Section('Keys');
824

    
825
$section->addInput(new Form_Checkbox(
826
	'showkey',
827
	'Authorized keys',
828
	'Click to paste an authorized key',
829
	false
830
));
831

    
832
$section->addInput(new Form_Textarea(
833
	'authorizedkeys',
834
	'Authorized SSH Keys',
835
	$pconfig['authorizedkeys']
836
))->setHelp('Enter authorized SSH keys for this user');
837

    
838
$section->addInput(new Form_Input(
839
	'ipsecpsk',
840
	'IPsec Pre-Shared Key',
841
	'text',
842
	$pconfig['ipsecpsk']
843
));
844

    
845
$form->add($section);
846

    
847
print $form;
848
?>
849
<script type="text/javascript">
850
//<![CDATA[
851
events.push(function() {
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 > 0) {
866
			for (i=0; i<len; i++) {
867
				if (From.eq(i).is(':selected')) {
868
					option = From.eq(i).val();
869
					value  = From.eq(i).text();
870
					To.append(new Option(value, option));
871
					From.eq(i).remove();
872
				}
873
			}
874
		}
875
	}
876

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

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

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

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

    
894
	$("#showkey").click(function() {
895
		hideInput('authorizedkeys', false);
896
		hideCheckbox('showkey', true);
897
	});
898

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

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

    
917

    
918
	// ---------- On initial page load ------------------------------------------------------------
919

    
920
	hideClass('cert-options', true);
921
	//hideInput('authorizedkeys', true);
922
	hideCheckbox('showkey', true);
923

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