Project

General

Profile

Download (24.7 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
	pfSense_BUILDER_BINARIES:
62
	pfSense_MODULE: auth
63
*/
64

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
163
	$certdeleted = lookup_cert($a_user[$id]['cert'][$_POST['certid']]);
164
	$certdeleted = $certdeleted['descr'];
165
	unset($a_user[$id]['cert'][$_POST['certid']]);
166
	write_config();
167
	$_POST['act'] = "edit";
168
	$savemsg = gettext("Certificate") . " {$certdeleted} " . gettext("association removed.") . "<br />";
169
}
170

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

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

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

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

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

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

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

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

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

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

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

    
272
	if (!$input_errors) {
273
		// This used to be a separate act=delpriv
274
		if ($a_user[$id] && !empty($_POST['privid'])) {
275
			foreach ($_POST['privid'] as $i)
276
				unset($a_user[$id]['priv'][$i]);
277

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
368
		conf_mount_ro();
369

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

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

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

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

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

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

    
404
	return($privhtml);
405
}
406

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

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

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

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

    
440
	}
441

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

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

    
450
	return($certhtml);
451
}
452

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
765
			$nonPrvCas = array();
766
			foreach( $config['ca'] as $ca) {
767
				if (!$ca['prv'])
768
					continue;
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>
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
					To.append(new Option(option, option));
860
					From.eq(i).remove();
861
				}
862
			}
863
		}
864
	}
865

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

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

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

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

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

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

    
897
	// ---------- On initial page load ------------------------------------------------------------
898

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

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

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