Project

General

Profile

Download (25.6 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
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'])) {
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['act'] == "delprivid") {
172
	$privdeleted = $priv_list[$a_user[$id]['priv'][$_POST['privid']]]['name'];
173
	unset($a_user[$id]['priv'][$_POST['privid']]);
174
	local_user_set($a_user[$id]);
175
	write_config();
176
	$_POST['act'] = "edit";
177
	$savemsg = gettext("Privilege ") . $privdeleted . gettext(" removed") . "<br />";
178
}
179

    
180
if ($_POST['save']) {
181
	unset($input_errors);
182
	$pconfig = $_POST;
183

    
184
	/* input validation */
185
	if (isset($id) && ($a_user[$id])) {
186
		$reqdfields = explode(" ", "usernamefld");
187
		$reqdfieldsn = array(gettext("Username"));
188
	} else {
189
		if (empty($_POST['name'])) {
190
			$reqdfields = explode(" ", "usernamefld passwordfld1");
191
			$reqdfieldsn = array(
192
				gettext("Username"),
193
				gettext("Password"));
194
		} else {
195
			$reqdfields = explode(" ", "usernamefld passwordfld1 name caref keylen lifetime");
196
			$reqdfieldsn = array(
197
				gettext("Username"),
198
				gettext("Password"),
199
				gettext("Descriptive name"),
200
				gettext("Certificate authority"),
201
				gettext("Key length"),
202
				gettext("Lifetime"));
203
		}
204
	}
205

    
206
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
207

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

    
212
	if (strlen($_POST['usernamefld']) > 16) {
213
		$input_errors[] = gettext("The username is longer than 16 characters.");
214
	}
215

    
216
	if (($_POST['passwordfld1']) && ($_POST['passwordfld1'] != $_POST['passwordfld2'])) {
217
		$input_errors[] = gettext("The passwords do not match.");
218
	}
219

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

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

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

    
268
	if (!empty($_POST['name'])) {
269
		$ca = lookup_ca($_POST['caref']);
270
		if (!$ca) {
271
			$input_errors[] = gettext("Invalid internal Certificate Authority") . "\n";
272
		}
273
	}
274

    
275
	/* if this is an AJAX caller then handle via JSON */
276
	if (isAjax() && is_array($input_errors)) {
277
		input_errors2Ajax($input_errors);
278
		exit;
279
	}
280

    
281
	if (!$input_errors) {
282

    
283

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
370
		conf_mount_ro();
371

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

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

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

    
390
	$i = 0;
391

    
392
	foreach (get_user_privdesc($a_user[$id]) as $priv) {
393
		$group = false;
394
		if ($priv['group']) {
395
			$group = $priv['group'];
396
		}
397

    
398
		$privhtml .=		'<tr>';
399
		$privhtml .=			'<td>' . htmlspecialchars($priv['group']) . '</td>';
400
		$privhtml .=			'<td>' . htmlspecialchars($priv['name']) . '</td>';
401
		$privhtml .=			'<td>' . htmlspecialchars($priv['descr']) . '</td>';
402
		$privhtml .=			'<td>';
403
		if (!$group)
404
			$privhtml .=			'<a class="fa fa-trash no-confirm icon-pointer" title="'.gettext('Delete Privilege').'" id="delprivid' .$i. '"></a></td>';
405

    
406
		$privhtml .=			'</td>';
407
		$privhtml .=		'</tr>';
408

    
409
		if(!$group)
410
			$i++;
411
	}
412

    
413
	$privhtml .=		'</tbody>';
414
	$privhtml .=	'</table>';
415
	$privhtml .= '</div>';
416

    
417
	$privhtml .= '<nav class="action-buttons">';
418
	$privhtml .=	'<a href="system_usermanager_addprivs.php?userid=' . $id . '" class="btn btn-success">' . gettext("Add") . '</a>';
419
	$privhtml .= '</nav>';
420

    
421
	return($privhtml);
422
}
423

    
424
function build_cert_table() {
425
	global $a_user, $id;
426

    
427
	$certhtml = '<div class="table-responsive">';
428
	$certhtml .=	'<table class="table table-striped table-hover table-condensed">';
429
	$certhtml .=		'<thead>';
430
	$certhtml .=			'<tr>';
431
	$certhtml .=				'<th>' . gettext('Name') . '</th>';
432
	$certhtml .=				'<th>' . gettext('CA') . '</th>';
433
	$certhtml .=				'<th></th>';
434
	$certhtml .=			'</tr>';
435
	$certhtml .=		'</thead>';
436
	$certhtml .=		'<tbody>';
437

    
438
	$a_cert = $a_user[$id]['cert'];
439
	if (is_array($a_cert)) {
440
		$i = 0;
441
		foreach ($a_cert as $certref) {
442
			$cert = lookup_cert($certref);
443
			$ca = lookup_ca($cert['caref']);
444
			$revokedstr =	is_cert_revoked($cert) ? '<b> Revoked</b>':'';
445

    
446
			$certhtml .=	'<tr>';
447
			$certhtml .=		'<td>' . htmlspecialchars($cert['descr']) . $revokedstr . '</td>';
448
			$certhtml .=		'<td>' . htmlspecialchars($ca['descr']) . '</td>';
449
			$certhtml .=		'<td>';
450
			$certhtml .=			'<a id="delcert' . $i .'" class="fa fa-trash no-confirm icon-pointer" title="';
451
			$certhtml .=			gettext('Remove this certificate association? (Certificate will not be deleted)') . '">Delete</a>';
452
			$certhtml .=		'</td>';
453
			$certhtml .=	'</tr>';
454
			$i++;
455
		}
456

    
457
	}
458

    
459
	$certhtml .=		'</tbody>';
460
	$certhtml .=	'</table>';
461
	$certhtml .= '</div>';
462

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

    
467
	return($certhtml);
468
}
469

    
470
$closehead = false;
471
include("head.inc");
472

    
473
if ($input_errors)
474
	print_input_errors($input_errors);
475

    
476
if ($savemsg)
477
	print_info_box($savemsg, 'success');
478

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

    
486
if (!($act == "new" || $act == "edit" || $input_errors)) {
487
?>
488
<form method="post">
489
<div class="table-responsive">
490
	<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap" data-sortable>
491
		<thead>
492
			<tr>
493
				<th>&nbsp;</th>
494
				<th><?=gettext("Username")?></th>
495
				<th><?=gettext("Full name")?></th>
496
				<th><?=gettext("Disabled")?></th>
497
				<th><?=gettext("Groups")?></th>
498
			</tr>
499
		</thead>
500
		<tbody>
501
<?php
502
foreach($a_user as $i => $userent):
503
	?>
504
			<tr>
505
				<td>
506
					<input type="checkbox" id="frc<?=$i?>" name="delete_check[]" value="<?=$i?>" <?=($userent['scope'] == "system" ? 'disabled' : '')?>/>
507
				</td>
508
				<td>
509
<?php
510
	if($userent['scope'] != "user")
511
		$usrimg = 'eye-open';
512
	else
513
		$usrimg = 'user';
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

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

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

    
559
$form = new Form;
560

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

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

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

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

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

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

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

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

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

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

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

    
624
	if ($ro)
625
		$input->setReadonly();
626

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

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

    
646
	$section->add($group);
647

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

    
655
	if ($ro)
656
		$input->setDisabled();
657

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

    
666
	// ==== Group membership ==================================================
667
	$group = new Form_Group('Group membership');
668

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

    
674
	$usergid = [$pconfig['usernamefld']];
675

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

    
685
	$group->add(new Form_Select(
686
		'sysgroups',
687
		null,
688
		array_combine((array)$pconfig['groups'], (array)$pconfig['groups']),
689
		$systemGroups,
690
		true
691
	))->setHelp('Not member of');
692

    
693
	$group->add(new Form_Select(
694
		'groups',
695
		null,
696
		array_combine((array)$pconfig['groups'], (array)$pconfig['groups']),
697
		$usersGroups,
698
		true
699
	))->setHelp('Member of');
700

    
701
	$section->add($group);
702

    
703
	$group = new Form_Group('');
704

    
705
	$group->add(new Form_Button(
706
		'movetoenabled',
707
		'Move to "Member of" list >'
708
	))->removeClass('btn-primary')->addClass('btn-default btn-sm');
709

    
710
	$group->add(new Form_Button(
711
		'movetodisabled',
712
		'< Move to "Not member of" list'
713
	))->removeClass('btn-primary')->addClass('btn-default btn-sm');
714

    
715
	$group->setHelp('Hold down CTRL (pc)/COMMAND (mac) key to select multiple items');
716
	$section->add($group);
717

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

    
728
	$form->add($section);
729

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

    
735
		$section = new Form_Section('Effective Privileges');
736

    
737
		$section->addInput(new Form_StaticText(
738
			null,
739
			build_priv_table()
740
		));
741

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

    
744
		// ==== Certificate table section =====================================
745
		$section = new Form_Section('User certificates');
746

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

    
752
		$form->add($section);
753
	}
754
else;
755
		$section = new Form_Section('User Certificates');
756

    
757
		foreach ((array)$a_user[$id]['cert'] as $i => $certref) {
758
			$cert = lookup_cert($certref);
759
			$ca = lookup_ca($cert['caref']);
760

    
761
			// We reverse name and action for readability of longer names
762
			$section->addInput(new Form_Checkbox(
763
				'certid[]',
764
				'Delete certificate',
765
				$cert['descr']. (is_cert_revoked($cert) ? ' <b>revoked</b>' : ''),
766
				false,
767
				$i
768
			));
769
		}
770

    
771
		#FIXME; old ui supplied direct export links to each certificate
772

    
773
		$section->addInput(new Form_StaticText(
774
			null,
775
			new Form_Button(null, 'add certificate', 'system_certmanager.php?act=new&userid='. $id).
776
			new Form_Button(null, 'export certificates', 'system_certmanager.php')
777
		));
778

    
779
		// ==== Add user certificate for a new user
780
		if (is_array($config['ca']) && count($config['ca']) > 0) {
781
			$section = new Form_Section('Create certificate for user');
782
			$section->addClass('cert-options');
783

    
784
			$nonPrvCas = array();
785
			foreach( $config['ca'] as $ca) {
786
				if (!$ca['prv'])
787
					continue;
788

    
789
				$nonPrvCas[ $ca['refid'] ] = $ca['descr'];
790
			}
791

    
792
			if (!empty($nonPrvCas)) {
793
				$section->addInput(new Form_Input(
794
					'name',
795
					'Descriptive name',
796
					'text',
797
					$pconfig['name']
798
				));
799

    
800
				$section->addInput(new Form_Select(
801
					'caref',
802
					'Certificate authority',
803
					null,
804
					$nonPrvCas
805
				));
806

    
807
				$section->addInput(new Form_Select(
808
					'keylen',
809
					'Key length',
810
					2048,
811
					array(
812
						512 => '512 bits',
813
						1024 => '1024 bits',
814
						2048 => '2049 bits',
815
						4096 => '4096 bits',
816
					)
817
				));
818

    
819
				$section->addInput(new Form_Input(
820
					'lifetime',
821
					'Lifetime',
822
					'number',
823
					$pconfig['lifetime']
824
				));
825
			}
826

    
827
			$form->add($section);
828
		}
829

    
830
endif;
831
// ==== Paste a key for the new user
832
$section = new Form_Section('Keys');
833

    
834
$section->addInput(new Form_Checkbox(
835
	'showkey',
836
	'Authorized keys',
837
	'Click to paste an authorized key',
838
	false
839
));
840

    
841
$section->addInput(new Form_Textarea(
842
	'authorizedkeys',
843
	'Authorized SSH Keys',
844
	$pconfig['authorizedkeys']
845
))->setHelp('Enter authorized SSH keys for this user');
846

    
847
$section->addInput(new Form_Input(
848
	'ipsecpsk',
849
	'IPsec Pre-Shared Key',
850
	'text',
851
	$pconfig['ipsecpsk']
852
));
853

    
854
$form->add($section);
855

    
856
print $form;
857
?>
858
<script type="text/javascript">
859
//<![CDATA[
860
events.push(function(){
861

    
862
	// Select every option in the specified multiselect
863
	function AllServers(id, selectAll) {
864
	   for (i = 0; i < id.length; i++)	   {
865
		   id.eq(i).prop('selected', selectAll);
866
	   }
867
	}
868

    
869
	// Move all selected options from one multiselect to another
870
	function moveOptions(From, To)	{
871
		var len = From.length;
872
		var option;
873

    
874
		if(len > 0) {
875
			for(i=0; i<len; i++) {
876
				if(From.eq(i).is(':selected')) {
877
					option = From.eq(i).val();
878
					value  = From.eq(i).text();
879
					To.append(new Option(value, option));
880
					From.eq(i).remove();
881
				}
882
			}
883
		}
884
	}
885

    
886
	// Make buttons plain buttons, not submit
887
	$("#movetodisabled").prop('type','button');
888
	$("#movetoenabled").prop('type','button');
889

    
890
	// On click . .
891
	$("#movetodisabled").click(function() {
892
		moveOptions($('[name="groups[]"] option'), $('[name="sysgroups[]"]'));
893
	});
894

    
895
	$("#movetoenabled").click(function() {
896
		moveOptions($('[name="sysgroups[]"] option'), $('[name="groups[]"]'));
897
	});
898

    
899
	$("#showcert").click(function() {
900
		hideClass('cert-options', !this.checked);
901
	});
902

    
903
	$("#showkey").click(function() {
904
		hideInput('authorizedkeys', false);
905
		hideCheckbox('showkey', true);
906
	});
907

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

    
917
	$('[id^=delprivid]').click(function(event) {
918
		if(confirm(event.target.title)) {
919
			$('#privid').val(event.target.id.match(/\d+$/)[0]);
920
			$('#userid').val('<?=$id;?>');
921
			$('#act').val('delprivid');
922
			$('form').submit();
923
		}
924
	});
925

    
926

    
927
	// ---------- On initial page load ------------------------------------------------------------
928

    
929
	hideClass('cert-options', true);
930
	//hideInput('authorizedkeys', true);
931
	hideCheckbox('showkey', true);
932

    
933
	// On submit mark all the user's groups as "selected"
934
	$('form').submit(function(){
935
		AllServers($('[name="groups[]"] option'), true);
936
	});
937
});
938
//]]>
939
</script>
940
<?php
941

    
942
include('foot.inc');
(205-205/228)