Project

General

Profile

Download (25.4 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'])) {
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
if ($_POST['act'] == "delprivid") {
171

    
172
		if ($a_user[$id] && !empty($_POST['privid'])) {
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 removed.") . "<br />";
178
		}
179
}
180

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

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

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

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

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

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

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

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

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

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

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

    
282
	if (!$input_errors) {
283

    
284

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
371
		conf_mount_ro();
372

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

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

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

    
391
	foreach (get_user_privdesc($a_user[$id]) as $i => $priv) {
392
		$privhtml .=		'<tr>';
393
		$privhtml .=			'<td>' . htmlspecialchars($priv['group']) . '</td>';
394
		$privhtml .=			'<td>' . htmlspecialchars($priv['name']) . '</td>';
395
		$privhtml .=			'<td>' . htmlspecialchars($priv['descr']) . '</td>';
396
		$privhtml .=			'<td><a class="fa fa-trash no-confirm" title="'.gettext('Delete Privilege').'" id="delprivid' .$i. '"></a></td>';
397
		$privhtml .=		'</tr>';
398
	}
399

    
400
	$privhtml .=		'</tbody>';
401
	$privhtml .=	'</table>';
402
	$privhtml .= '</div>';
403

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

    
408
	return($privhtml);
409
}
410

    
411
function build_cert_table() {
412
	global $a_user, $id;
413

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

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

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

    
444
	}
445

    
446
	$certhtml .=		'</tbody>';
447
	$certhtml .=	'</table>';
448
	$certhtml .= '</div>';
449

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

    
454
	return($certhtml);
455
}
456

    
457
$closehead = false;
458
include("head.inc");
459

    
460
if ($input_errors)
461
	print_input_errors($input_errors);
462

    
463
if ($savemsg)
464
	print_info_box($savemsg, 'success');
465

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

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

    
525
	<button type="submit" class="btn btn-sm btn-danger" name="dellall" value="dellall" title="<?=gettext('Delete selected users')?>">
526
		<i class="fa fa-trash icon-embed-btn"></i>
527
		<?=gettext("Delete")?>
528
	</button>
529
</nav>
530

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

    
541
<?php
542
	include("foot.inc");
543
	exit;
544
}
545

    
546
require_once('classes/Form.class.php');
547
$form = new Form;
548

    
549
if ($act == "new" || $act == "edit" || $input_errors):
550

    
551
	$form->addGlobal(new Form_Input(
552
		'act',
553
		null,
554
		'hidden',
555
		''
556
	));
557

    
558
	$form->addGlobal(new Form_Input(
559
		'userid',
560
		null,
561
		'hidden',
562
		isset($id) ? $id:''
563
	));
564

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

    
572
	$form->addGlobal(new Form_Input(
573
		'certid',
574
		null,
575
		'hidden',
576
		''
577
	));
578

    
579
	$ro = "";
580
	if ($pconfig['utype'] == "system") {
581
		$ro = "readonly=\"readonly\"";
582
	}
583

    
584
	$section = new Form_Section('User Properties');
585

    
586
	$section->addInput(new Form_StaticText(
587
		'Defined by',
588
		strtoupper($pconfig['utype'])
589
	));
590

    
591
	$form->addGlobal(new Form_Input(
592
		'utype',
593
		null,
594
		'hidden',
595
		$pconfig['utype']
596
	));
597

    
598
	$section->addInput(new Form_Checkbox(
599
		'disabled',
600
		'Disabled',
601
		'This user cannot login',
602
		$pconfig['disabled']
603
	));
604

    
605
	$section->addInput($input = new Form_Input(
606
		'usernamefld',
607
		'Username',
608
		'text',
609
		$pconfig['usernamefld']
610
	));
611

    
612
	if ($ro)
613
		$input->setReadonly();
614

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

    
622
	$group = new Form_Group('Password');
623
	$group->add(new Form_Input(
624
		'passwordfld1',
625
		'Password',
626
		'password'
627
	));
628
	$group->add(new Form_Input(
629
		'passwordfld2',
630
		'Confirm Password',
631
		'password'
632
	));
633

    
634
	$section->add($group);
635

    
636
	$section->addInput($input = new Form_Input(
637
		'descr',
638
		'Full name',
639
		'text',
640
		htmlspecialchars($pconfig['descr'])
641
	))->setHelp('User\'s full name, for your own information only');
642

    
643
	if ($ro)
644
		$input->setDisabled();
645

    
646
	$section->addInput(new Form_Input(
647
		'expires',
648
		'Expiration date',
649
		'date',
650
		$pconfig['expires']
651
	))->setHelp('Leave blank if the account shouldn\'t expire, otherwise enter '.
652
		'the expiration date');
653

    
654
	// ==== Group membership ==================================================
655
	$group = new Form_Group('Group membership');
656

    
657
	// Make a list of all the groups configured on the system, and a list of
658
	// those which this user is a member of
659
	$systemGroups = array();
660
	$usersGroups = array();
661

    
662
	$usergid = [$pconfig['usernamefld']];
663

    
664
	foreach ($config['system']['group'] as $Ggroup) {
665
		if($Ggroup['name'] != "all") {
666
			if(($act == 'edit') && $Ggroup['member'] && in_array($pconfig['uid'], $Ggroup['member']))
667
				$usersGroups[ $Ggroup['name'] ] = $Ggroup['name'];	// Add it to the user's list
668
			else
669
				$systemGroups[ $Ggroup['name'] ] = $Ggroup['name']; // Add it to the 'not a member of' list
670
		}
671
	}
672

    
673
	$group->add(new Form_Select(
674
		'sysgroups',
675
		null,
676
		array_combine((array)$pconfig['groups'], (array)$pconfig['groups']),
677
		$systemGroups,
678
		true
679
	))->setHelp('Not member of');
680

    
681
	$group->add(new Form_Select(
682
		'groups',
683
		null,
684
		array_combine((array)$pconfig['groups'], (array)$pconfig['groups']),
685
		$usersGroups,
686
		true
687
	))->setHelp('Member of');
688

    
689
	$section->add($group);
690

    
691
	$group = new Form_Group('');
692

    
693
	$group->add(new Form_Button(
694
		'movetoenabled',
695
		'Move to "Member of" list >'
696
	))->removeClass('btn-primary')->addClass('btn-default btn-sm');
697

    
698
	$group->add(new Form_Button(
699
		'movetodisabled',
700
		'< Move to "Not member of" list'
701
	))->removeClass('btn-primary')->addClass('btn-default btn-sm');
702

    
703
	$group->setHelp('Hold down CTRL (pc)/COMMAND (mac) key to select multiple items');
704
	$section->add($group);
705

    
706
	// ==== Button for adding user certificate ================================
707
	if($act == 'new') {
708
		$section->addInput(new Form_Checkbox(
709
			'showcert',
710
			'Certificate',
711
			'Click to create a user certificate',
712
			false
713
		));
714
	}
715

    
716
	$form->add($section);
717

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

    
723
		$section = new Form_Section('Effective Privileges');
724

    
725
		$section->addInput(new Form_StaticText(
726
			null,
727
			build_priv_table()
728
		));
729

    
730
		$form->add($section);
731

    
732
		// ==== Certificate table section =====================================
733
		$section = new Form_Section('User certificates');
734

    
735
		$section->addInput(new Form_StaticText(
736
			null,
737
			build_cert_table()
738
		));
739

    
740
		$form->add($section);
741
	}
742
else;
743
		$section = new Form_Section('User Certificates');
744

    
745
		foreach ((array)$a_user[$id]['cert'] as $i => $certref) {
746
			$cert = lookup_cert($certref);
747
			$ca = lookup_ca($cert['caref']);
748

    
749
			// We reverse name and action for readability of longer names
750
			$section->addInput(new Form_Checkbox(
751
				'certid[]',
752
				'Delete certificate',
753
				$cert['descr']. (is_cert_revoked($cert) ? ' <b>revoked</b>' : ''),
754
				false,
755
				$i
756
			));
757
		}
758

    
759
		#FIXME; old ui supplied direct export links to each certificate
760

    
761
		$section->addInput(new Form_StaticText(
762
			null,
763
			new Form_Button(null, 'add certificate', 'system_certmanager.php?act=new&userid='. $id).
764
			new Form_Button(null, 'export certificates', 'system_certmanager.php')
765
		));
766

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

    
772
			$nonPrvCas = array();
773
			foreach( $config['ca'] as $ca) {
774
				if (!$ca['prv'])
775
					continue;
776

    
777
				$nonPrvCas[ $ca['refid'] ] = $ca['descr'];
778
			}
779

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

    
788
				$section->addInput(new Form_Select(
789
					'caref',
790
					'Certificate authority',
791
					null,
792
					$nonPrvCas
793
				));
794

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

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

    
815
			$form->add($section);
816
		}
817

    
818
endif;
819
// ==== Paste a key for the new user
820
$section = new Form_Section('Keys');
821

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

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

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

    
842
$form->add($section);
843

    
844
print $form;
845
?>
846
<script>
847
//<![CDATA[
848
events.push(function(){
849

    
850
	// Select every option in the specified multiselect
851
	function AllServers(id, selectAll) {
852
	   for (i = 0; i < id.length; i++)	   {
853
		   id.eq(i).prop('selected', selectAll);
854
	   }
855
	}
856

    
857
	// Move all selected options from one multiselect to another
858
	function moveOptions(From, To)	{
859
		var len = From.length;
860
		var option;
861

    
862
		if(len > 0) {
863
			for(i=0; i<len; i++) {
864
				if(From.eq(i).is(':selected')) {
865
					option = From.eq(i).val();
866
					To.append(new Option(option, option));
867
					From.eq(i).remove();
868
				}
869
			}
870
		}
871
	}
872

    
873
	// Make buttons plain buttons, not submit
874
	$("#movetodisabled").prop('type','button');
875
	$("#movetoenabled").prop('type','button');
876

    
877
	// On click . .
878
	$("#movetodisabled").click(function() {
879
		moveOptions($('[name="groups[]"] option'), $('[name="sysgroups[]"]'));
880
	});
881

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

    
886
	$("#showcert").click(function() {
887
		hideClass('cert-options', !this.checked);
888
	});
889

    
890
	$("#showkey").click(function() {
891
		hideInput('authorizedkeys', false);
892
		hideCheckbox('showkey', true);
893
	});
894

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

    
912

    
913
	// ---------- On initial page load ------------------------------------------------------------
914

    
915
	hideClass('cert-options', true);
916
	//hideInput('authorizedkeys', true);
917
	hideCheckbox('showkey', true);
918

    
919
	// On submit mark all the user's groups as "selected"
920
	$('form').submit(function(){
921
		AllServers($('[name="groups[]"] option'), true);
922
	});
923
});
924
//]]>
925
</script>
926
<?php
927

    
928
include('foot.inc');
(207-207/230)