Project

General

Profile

Download (25.1 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
	echo "privid: ";
272
	echo $_POST['privid'];
273
	if (!$input_errors) {
274
		// This used to be a separate act=delpriv
275
		
276
		if ($a_user[$id] && !empty($_POST['privid'])) {
277
			foreach ($_POST['privid'] as $i)
278
				unset($a_user[$id]['priv'][$i]);
279

    
280
			local_user_set($a_user[$id]);
281
			write_config();
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
	foreach (get_user_privdesc($a_user[$id]) as $i => $priv) {
391
		$privhtml .=		'<tr>';
392
		$privhtml .=			'<td>' . htmlspecialchars($priv['group']) . '</td>';
393
		$privhtml .=			'<td>' . htmlspecialchars($priv['name']) . '</td>';
394
		$privhtml .=			'<td>' . htmlspecialchars($priv['descr']) . '</td>';
395
		$privhtml .=			'<td><a class="fa fa-trash" title="'.gettext('Delete Privilege').'" id="delprivid' .$i. '"></a></td>';
396
		$privhtml .=		'</tr>';
397
	}
398

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

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

    
407
	return($privhtml);
408
}
409

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

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

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

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

    
443
	}
444

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

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

    
453
	return($certhtml);
454
}
455

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

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

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

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

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

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

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

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

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

    
545
if ($act == "new" || $act == "edit" || $input_errors):
546

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

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

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

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

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

    
580
	$section = new Form_Section('User Properties');
581

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

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

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

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

    
608
	if ($ro)
609
		$input->setReadonly();
610

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

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

    
630
	$section->add($group);
631

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

    
639
	if ($ro)
640
		$input->setDisabled();
641

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

    
650
	// ==== Group membership ==================================================
651
	$group = new Form_Group('Group membership');
652

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

    
658
	$usergid = [$pconfig['usernamefld']];
659

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

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

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

    
685
	$section->add($group);
686

    
687
	$group = new Form_Group('');
688

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

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

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

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

    
712
	$form->add($section);
713

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

    
719
		$section = new Form_Section('Effective Privileges');
720

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

    
726
		$form->add($section);
727

    
728
		// ==== Certificate table section =====================================
729
		$section = new Form_Section('User certificates');
730

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

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

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

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

    
755
		#FIXME; old ui supplied direct export links to each certificate
756

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

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

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

    
773
				$nonPrvCas[ $ca['refid'] ] = $ca['descr'];
774
			}
775

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

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

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

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

    
811
			$form->add($section);
812
		}
813

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

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

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

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

    
838
$form->add($section);
839

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

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

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

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

    
869
	// Make buttons plain buttons, not submit
870
	$("#movetodisabled").prop('type','button');
871
	$("#movetoenabled").prop('type','button');
872

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

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

    
882
	$("#showcert").click(function() {
883
		hideClass('cert-options', !this.checked);
884
	});
885

    
886
	$("#showkey").click(function() {
887
		hideInput('authorizedkeys', false);
888
		hideCheckbox('showkey', true);
889
	});
890

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

    
909
	// ---------- On initial page load ------------------------------------------------------------
910

    
911
	hideClass('cert-options', true);
912
	//hideInput('authorizedkeys', true);
913
	hideCheckbox('showkey', true);
914

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

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