Project

General

Profile

Download (8.39 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
    system_usermanager_addcert.php
4

    
5
    Copyright (C) 2008 Shrew Soft Inc.
6
    All rights reserved.
7

    
8
    Redistribution and use in source and binary forms, with or without
9
    modification, are permitted provided that the following conditions are met:
10

    
11
    1. Redistributions of source code must retain the above copyright notice,
12
       this list of conditions and the following disclaimer.
13

    
14
    2. Redistributions in binary form must reproduce the above copyright
15
       notice, this list of conditions and the following disclaimer in the
16
       documentation and/or other materials provided with the distribution.
17

    
18
    THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19
    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20
    AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21
    AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
22
    OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
    POSSIBILITY OF SUCH DAMAGE.
28
*/
29
/*
30
	pfSense_MODULE:	certificate_manager
31
*/
32

    
33
##|+PRIV
34
##|*IDENT=page-system-usermanager_addcert
35
##|*NAME=System: User Manager: Add Certificate
36
##|*DESCR=Allow access to the 'User Manager: Add Certificate' page.
37
##|*MATCH=system_usermanager_addcert.php*
38
##|-PRIV
39

    
40
require("guiconfig.inc");
41
require("certs.inc");
42

    
43
$cert_keylens = array( "512", "1024", "2048", "4096");
44

    
45
$pgtitle = array(gettext("System"), gettext("User Manager: Add Certificate"));
46

    
47
$userid = $_GET['userid'];
48
if (isset($_POST['userid']))
49
	$userid = $_POST['userid'];
50

    
51
if (!is_array($config['system']['user']))
52
	$config['system']['user'] = array();
53

    
54
$a_user =& $config['system']['user'];
55

    
56
if (!is_array($config['system']['ca']))
57
	$config['system']['ca'] = array();
58

    
59
$a_ca =& $config['system']['ca'];
60

    
61
$internal_ca_count = 0;
62
foreach ($a_ca as $ca)
63
	if ($ca['prv'])	
64
		$internal_ca_count++;
65

    
66
if ($_GET) {
67
	$pconfig['keylen'] = "2048";
68
	$pconfig['lifetime'] = "3650";
69
}
70

    
71
if ($_POST) {
72
	conf_mount_rw();
73
	
74
	unset($input_errors);
75
	$pconfig = $_POST;
76

    
77
	/* input validation */
78
	if ($pconfig['method'] == "existing") {
79
		$reqdfields = explode(" ",
80
				"name cert key");
81
		$reqdfieldsn = array(
82
				gettext("Descriptive name"),
83
				gettext("Certificate data"),
84
				gettext("Key data"));
85
	}
86

    
87
	if ($pconfig['method'] == "internal") {
88
		$reqdfields = explode(" ",
89
				"name caref keylen lifetime");
90
		$reqdfieldsn = array(
91
				gettext("Descriptive name"),
92
				gettext("Certificate authority"),
93
				gettext("Key length"),
94
				gettext("Lifetime"));
95
	}
96

    
97
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
98

    
99
	$ca = lookup_ca($pconfig['caref']);
100
	if (!$ca)
101
		$input_errors[] = sprintf(gettext("Invalid internal Certificate Authority%s"),"\n");
102

    
103
	/* if this is an AJAX caller then handle via JSON */
104
	if (isAjax() && is_array($input_errors)) {
105
		input_errors2Ajax($input_errors);
106
		conf_mount_ro();
107
		exit;
108
	}
109

    
110
	/* save modifications */
111
	if (!$input_errors) {
112

    
113
		$cert = array();
114
		$cert['refid'] = uniqid();
115
		if (!is_array($a_user[$userid]['cert']))
116
			$a_user[$userid]['cert'] = array();
117

    
118
	    $cert['name'] = $pconfig['name'];
119

    
120
		$subject = cert_get_subject_array($ca['crt']);
121

    
122
		$dn = array(
123
			'countryName' => $subject[0]['v'],
124
			'stateOrProvinceName' => $subject[1]['v'],
125
			'localityName' => $subject[2]['v'],
126
			'organizationName' => $subject[3]['v'],
127
			'emailAddress' => $subject[4]['v'],
128
			'commonName' => $a_user[$userid]['name']);
129

    
130
		cert_create($cert, $pconfig['caref'], $pconfig['keylen'],
131
			$pconfig['lifetime'], $dn);
132

    
133
		$a_user[$userid]['cert'][] = $cert;
134

    
135
		write_config();
136

    
137
		conf_mount_ro();
138
		
139
		pfSenseHeader("system_usermanager.php?act=edit&id={$userid}");
140
	}
141
}
142

    
143
include("head.inc");
144
?>
145

    
146
<body link="#000000" vlink="#000000" alink="#000000" onload="<?= $jsevents["body"]["onload"] ?>">
147
<?php include("fbegin.inc"); ?>
148
<script type="text/javascript">
149
<!--
150

    
151
<?php if ($internal_ca_count): ?>
152
function internalca_change() {
153

    
154
	index = document.iform.caref.selectedIndex;
155
	caref = document.iform.caref[index].value;
156

    
157
	switch (caref) {
158
<?php
159
		foreach ($a_ca as $ca):
160
			if (!$ca['prv'])
161
				continue;
162
			$subject = cert_get_subject_array($ca['crt']);
163
?>
164
		case "<?=$ca['refid'];?>":
165
			document.iform.dn_country.value = "<?=$subject[0]['v'];?>";
166
			document.iform.dn_state.value = "<?=$subject[1]['v'];?>";
167
			document.iform.dn_city.value = "<?=$subject[2]['v'];?>";
168
			document.iform.dn_organization.value = "<?=$subject[3]['v'];?>";
169
			break;
170
<?php	endforeach; ?>
171
	}
172
}
173
<?php endif; ?>
174

    
175
//-->
176
</script>
177
<?php
178
	if ($input_errors)
179
		print_input_errors($input_errors);
180
	if ($savemsg)
181
		print_info_box($savemsg);
182
?>
183
<table width="100%" border="0" cellpadding="0" cellspacing="0">
184
	<tr>
185
		<td>
186
		<?php
187
			$tab_array = array();
188
			$tab_array[] = array(gettext("Users"), true, "system_usermanager.php");
189
			$tab_array[] = array(gettext("Groups"), false, "system_groupmanager.php");
190
			$tab_array[] = array(gettext("Settings"), false, "system_usermanager_settings.php");
191
			$tab_array[] = array(gettext("Servers"), false, "system_authservers.php");
192
			display_top_tabs($tab_array);
193
		?>
194
		</td>
195
	</tr>
196
	<tr>
197
		<td id="mainarea">
198
			<div class="tabcont">
199
				<form action="system_usermanager_addcert.php" method="post" name="iform" id="iform">
200
					<table width="100%" border="0" cellpadding="6" cellspacing="0">
201

    
202
						<?php if (!$internal_ca_count): ?>
203

    
204
						<tr>
205
							<td colspan="2" align="center" class="vtable">
206
								<?=gettext("No internal Certificate Authorities have been defined. You must");?>
207
								<a href="system_camanager.php?act=new&method=internal"><?=gettext("create");?></a>
208
								<?=gettext("an internal CA before creating an internal certificate.");?>
209
							</td>
210
						</tr>
211

    
212
						<?php else: ?>
213

    
214
						<tr>
215
							<td width="22%" valign="top" class="vncellreq"><?=gettext("Descriptive name");?></td>
216
							<td width="78%" class="vtable">
217
								<input name="name" type="text" class="formfld unknown" id="name" size="20" value="<?=htmlspecialchars($pconfig['name']);?>"/>
218
							</td>
219
						</tr>
220
						<tr>
221
							<td width="22%" valign="top" class="vncellreq"><?=gettext("Certificate authority");?></td>
222
							<td width="78%" class="vtable">
223
								<select name='caref' id='caref' class="formselect" onChange='internalca_change()'>
224
								<?php
225
									foreach( $a_ca as $ca):
226
									if (!$ca['prv'])
227
										continue;
228
									$selected = "";
229
									if ($pconfig['caref'] == $ca['refid'])
230
										$selected = "selected";
231
								?>
232
									<option value="<?=$ca['refid'];?>"<?=$selected;?>><?=$ca['name'];?></option>
233
								<?php endforeach; ?>
234
								</select>
235
							</td>
236
						</tr>
237
						<tr>
238
							<td width="22%" valign="top" class="vncellreq"><?=gettext("Key length");?></td>
239
							<td width="78%" class="vtable">
240
								<select name='keylen' class="formselect">
241
								<?php
242
									foreach( $cert_keylens as $len):
243
									$selected = "";
244
									if ($pconfig['keylen'] == $len)
245
										$selected = "selected";
246
								?>
247
									<option value="<?=$len;?>"<?=$selected;?>><?=$len;?></option>
248
								<?php endforeach; ?>
249
								</select>
250
								<?=gettext("bits");?>
251
							</td>
252
						</tr>
253
						<tr>
254
							<td width="22%" valign="top" class="vncellreq"><?=gettext("Lifetime");?></td>
255
							<td width="78%" class="vtable">
256
								<input name="lifetime" type="text" class="formfld unknown" id="lifetime" size="5" value="<?=htmlspecialchars($pconfig['lifetime']);?>"/>
257
								<?=gettext("days");?>
258
							</td>
259
						</tr>
260

    
261
						<?php endif; ?>
262

    
263
						<tr>
264
							<td width="22%" valign="top">&nbsp;</td>
265
							<td width="78%">
266
								<?php if ($internal_ca_count): ?>
267
								<input id="submit" name="save" type="submit" class="formbtn" value="<?=gettext("Save");?>" />
268
								<input id="cancelbutton" class="formbtn" type="button" value="<?=gettext("Cancel");?>" onclick="history.back()" />
269
								<?php endif; ?>
270
								<?php if (isset($userid) && $a_user[$userid]): ?>
271
								<input name="userid" type="hidden" value="<?=$userid;?>" />
272
								<?php endif;?>
273
							</td>
274
						</tr>
275
					</table>
276
				</form>
277
			</div>
278
		</td>
279
	</tr>
280
</table>
281
<?php include("fend.inc");?>
282
<script type="text/javascript">
283
<!--
284

    
285
internalca_change();
286

    
287
//-->
288
</script>
289

    
290
</body>
(195-195/222)