Project

General

Profile

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

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

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

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

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

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

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

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

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

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

    
113
if ($_GET['act'] == "deluser") {
114

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

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

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

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

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

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

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

    
164
	$certdeleted = lookup_cert($a_user[$id]['cert'][$_POST['certid']]);
165
	$certdeleted = $certdeleted['descr'];
166
	unset($a_user[$id]['cert'][$_POST['certid']]);
167
	write_config();
168
	$_POST['act'] = "edit";
169
	$savemsg = gettext("Certificate") . " {$certdeleted} " . gettext("association removed.") . "<br />";
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
		// This used to be a separate act=delpriv
275
		if ($a_user[$id] && !empty($_POST['privid'])) {
276
			foreach ($_POST['privid'] as $i)
277
				unset($a_user[$id]['priv'][$i]);
278

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
358
		/* 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. */
359
		local_user_set_groups($userent,$_POST['groups']);
360
		local_user_set($userent);
361
		/* 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. */
362
		local_user_set_groups($userent, $_POST['groups']);
363
		write_config();
364

    
365
		if (is_dir("/etc/inc/privhooks")) {
366
			run_plugins("/etc/inc/privhooks");
367
		}
368

    
369
		conf_mount_ro();
370

    
371
		pfSenseHeader("system_usermanager.php");
372
	}
373
}
374

    
375
function build_priv_table() {
376
	global $a_user, $id;
377

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

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

    
397
	$privhtml .=		'</tbody>';
398
	$privhtml .=	'</table>';
399
	$privhtml .= '</div>';
400

    
401
	$privhtml .= '<nav class="action-buttons">';
402
	$privhtml .=	'<a href="system_usermanager_addprivs.php?userid=' . $id . '" class="btn btn-success">' . gettext("Add") . '</a>';
403
	$privhtml .= '</nav>';
404

    
405
	return($privhtml);
406
}
407

    
408
function build_cert_table() {
409
	global $a_user, $id;
410

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

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

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

    
441
	}
442

    
443
	$certhtml .=		'</tbody>';
444
	$certhtml .=	'</table>';
445
	$certhtml .= '</div>';
446

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

    
451
	return($certhtml);
452
}
453

    
454
$closehead = false;
455
include("head.inc");
456

    
457
if ($input_errors)
458
	print_input_errors($input_errors);
459

    
460
if ($savemsg)
461
	print_info_box($savemsg, 'success');
462

    
463
$tab_array = array();
464
$tab_array[] = array(gettext("Users"), true, "system_usermanager.php");
465
$tab_array[] = array(gettext("Groups"), false, "system_groupmanager.php");
466
$tab_array[] = array(gettext("Settings"), false, "system_usermanager_settings.php");
467
$tab_array[] = array(gettext("Servers"), false, "system_authservers.php");
468
display_top_tabs($tab_array);
469

    
470
if (!($act == "new" || $act == "edit" || $input_errors)) {
471
?>
472

    
473
<div class="table-responsive">
474
	<table class="table table-striped table-hover">
475
		<thead>
476
			<tr>
477
				<th>&nbsp;</th>
478
				<th><?=gettext("Username")?></th>
479
				<th><?=gettext("Full name")?></th>
480
				<th><?=gettext("Disabled")?></th>
481
				<th><?=gettext("Groups")?></th>
482
			</tr>
483
		</thead>
484
		<tbody>
485
		</tbody>
486
		<tbody>
487
<?php
488
foreach($a_user as $i => $userent):
489
	?>
490
			<tr>
491
				<td>
492
					<input type="checkbox" id="frc<?=$i?>" name="delete_check[]" value="<?=$i?>" <?=($userent['scope'] == "system" ? 'disabled="disabled"' : '')?>/>
493
				</td>
494
				<td>
495
<?php
496
	if($userent['scope'] != "user")
497
		$usrimg = 'eye-open';
498
	else
499
		$usrimg = 'user';
500
?>
501
					<i class="icon icon-<?=$usrimg?>"></i>
502
					<?=htmlspecialchars($userent['name'])?>
503
				</td>
504
				<td><?=htmlspecialchars($userent['descr'])?></td>
505
				<td><?php if(isset($userent['disabled'])) echo "*"?></td>
506
				<td><?=implode(",",local_user_get_groups($userent))?></td>
507
				<td>
508
					<a class="fa fa-pencil" title="<?=gettext("Edit user"); ?>" href="?act=edit&amp;userid=<?=$i?>"></a>
509
<?php if($userent['scope'] != "system"): ?>
510
					<a class="fa fa-trash"	title="<?=gettext("Delete user")?>" href="?act=deluser&amp;userid=<?=$i?>&amp;username=<?=$userent['name']?>"></a>
511
<?php endif; ?>
512
				</td>
513
			</tr>
514
<?php endforeach; ?>
515
		</tbody>
516
	</table>
517
</div>
518
<nav class="action-buttons">
519
	<a href="?act=new" class="btn btn-success">
520
		<i class="fa fa-plus icon-embed-btn"></i>
521
		<?=gettext("Add")?>
522
	</a>
523
</nav>
524

    
525
<div id="infoblock">
526
	<?=print_info_box(gettext("Additional users can be added here. User permissions for accessing " .
527
	"the webConfigurator can be assigned directly or inherited from group memberships. " .
528
	"An icon that appears grey indicates that it is a system defined object. " .
529
	"Some system object properties can be modified but they cannot be deleted.") .
530
	'<br /><br />' .
531
	gettext("Accounts created here are also used for other parts of the system " .
532
	"such as OpenVPN, IPsec, and Captive Portal."), info)?>
533
</div>
534

    
535
<?php
536
	include("foot.inc");
537
	exit;
538
}
539

    
540
require_once('classes/Form.class.php');
541
$form = new Form;
542

    
543
if ($act == "new" || $act == "edit" || $input_errors):
544

    
545
	$form->addGlobal(new Form_Input(
546
		'act',
547
		null,
548
		'hidden',
549
		''
550
	));
551

    
552
	$form->addGlobal(new Form_Input(
553
		'userid',
554
		null,
555
		'hidden',
556
		isset($id) ? $id:''
557
	));
558

    
559
	$form->addGlobal(new Form_Input(
560
		'privid',
561
		null,
562
		'hidden',
563
		''
564
	));
565

    
566
	$form->addGlobal(new Form_Input(
567
		'certid',
568
		null,
569
		'hidden',
570
		''
571
	));
572

    
573
	$ro = "";
574
	if ($pconfig['utype'] == "system") {
575
		$ro = "readonly=\"readonly\"";
576
	}
577

    
578
	$section = new Form_Section('User Properties');
579

    
580
	$section->addInput(new Form_StaticText(
581
		'Defined by',
582
		strtoupper($pconfig['utype'])
583
	));
584

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

    
592
	$section->addInput(new Form_Checkbox(
593
		'disabled',
594
		'Disabled',
595
		'This user cannot login',
596
		$pconfig['disabled']
597
	));
598

    
599
	$section->addInput($input = new Form_Input(
600
		'usernamefld',
601
		'Username',
602
		'text',
603
		$pconfig['usernamefld']
604
	));
605

    
606
	if ($ro)
607
		$input->setReadonly();
608

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

    
616
	$group = new Form_Group('Password');
617
	$group->add(new Form_Input(
618
		'passwordfld1',
619
		'Password',
620
		'password'
621
	));
622
	$group->add(new Form_Input(
623
		'passwordfld2',
624
		'Confirm Password',
625
		'password'
626
	));
627

    
628
	$section->add($group);
629

    
630
	$section->addInput($input = new Form_Input(
631
		'descr',
632
		'Full name',
633
		'text',
634
		htmlspecialchars($pconfig['descr'])
635
	))->setHelp('User\'s full name, for your own information only');
636

    
637
	if ($ro)
638
		$input->setDisabled();
639

    
640
	$section->addInput(new Form_Input(
641
		'expires',
642
		'Expiration date',
643
		'date',
644
		$pconfig['expires']
645
	))->setHelp('Leave blank if the account shouldn\'t expire, otherwise enter '.
646
		'the expiration date');
647

    
648
	// ==== Group membership ==================================================
649
	$group = new Form_Group('Group membership');
650

    
651
	// Make a list of all the groups configured on the system, and a list of
652
	// those which this user is a member of
653
	$systemGroups = array();
654
	$usersGroups = array();
655

    
656
	$usergid = [$pconfig['usernamefld']];
657

    
658
	foreach ($config['system']['group'] as $Ggroup) {
659
		if($Ggroup['name'] != "all") {
660
			if(($act == 'edit') && $Ggroup['member'] && in_array($pconfig['uid'], $Ggroup['member']))
661
				$usersGroups[ $Ggroup['name'] ] = $Ggroup['name'];	// Add it to the user's list
662
			else
663
				$systemGroups[ $Ggroup['name'] ] = $Ggroup['name']; // Add it to the 'not a member of' list
664
		}
665
	}
666

    
667
	$group->add(new Form_Select(
668
		'sysgroups',
669
		null,
670
		array_combine((array)$pconfig['groups'], (array)$pconfig['groups']),
671
		$systemGroups,
672
		true
673
	))->setHelp('Not member of');
674

    
675
	$group->add(new Form_Select(
676
		'groups',
677
		null,
678
		array_combine((array)$pconfig['groups'], (array)$pconfig['groups']),
679
		$usersGroups,
680
		true
681
	))->setHelp('Member of');
682

    
683
	$section->add($group);
684

    
685
	$group = new Form_Group('');
686

    
687
	$group->add(new Form_Button(
688
		'movetoenabled',
689
		'Move to "Member of" list >'
690
	))->removeClass('btn-primary')->addClass('btn-default btn-sm');
691

    
692
	$group->add(new Form_Button(
693
		'movetodisabled',
694
		'< Move to "Not member of" list'
695
	))->removeClass('btn-primary')->addClass('btn-default btn-sm');
696

    
697
	$group->setHelp('Hold down CTRL (pc)/COMMAND (mac) key to select multiple items');
698
	$section->add($group);
699

    
700
	// ==== Button for adding user certificate ================================
701
	if($act == 'new') {
702
		$section->addInput(new Form_Checkbox(
703
			'showcert',
704
			'Certificate',
705
			'Click to create a user certificate',
706
			false
707
		));
708
	}
709

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

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

    
717
		$section = new Form_Section('Effective Privileges');
718

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

    
724
		$form->add($section);
725

    
726
		// ==== Certificate table section =====================================
727
		$section = new Form_Section('User certificates');
728

    
729
		$section->addInput(new Form_StaticText(
730
			null,
731
			build_cert_table()
732
		));
733

    
734
		$form->add($section);
735
	}
736
else;
737
		$section = new Form_Section('User Certificates');
738

    
739
		foreach ((array)$a_user[$id]['cert'] as $i => $certref) {
740
			$cert = lookup_cert($certref);
741
			$ca = lookup_ca($cert['caref']);
742

    
743
			// We reverse name and action for readability of longer names
744
			$section->addInput(new Form_Checkbox(
745
				'certid[]',
746
				'Delete certificate',
747
				$cert['descr']. (is_cert_revoked($cert) ? ' <b>revoked</b>' : ''),
748
				false,
749
				$i
750
			));
751
		}
752

    
753
		#FIXME; old ui supplied direct export links to each certificate
754

    
755
		$section->addInput(new Form_StaticText(
756
			null,
757
			new Form_Button(null, 'add certificate', 'system_certmanager.php?act=new&userid='. $id).
758
			new Form_Button(null, 'export certificates', 'system_certmanager.php')
759
		));
760

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

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

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

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

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

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

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

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

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

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

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

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

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

    
838
print $form;
839
?>
840
<script>
841
//<![CDATA[
842
events.push(function(){
843

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

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

    
856
		if(len > 0) {
857
			for(i=0; i<len; i++) {
858
				if(From.eq(i).is(':selected')) {
859
					option = From.eq(i).val();
860
					To.append(new Option(option, 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
	// ---------- On initial page load ------------------------------------------------------------
899

    
900
	hideClass('cert-options', true);
901
	//hideInput('authorizedkeys', true);
902
	hideCheckbox('showkey', true);
903

    
904
	// On submit mark all the user's groups as "selected"
905
	$('form').submit(function(){
906
		AllServers($('[name="groups[]"] option'), true);
907
	});
908
});
909
//]]>
910
</script>
911
<?php
912

    
913
include('foot.inc');
(211-211/234)