Project

General

Profile

Download (8.36 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
		if (!is_array($a_user[$userid]['cert']))
115
			$a_user[$userid]['cert'] = array();
116

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

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

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

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

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

    
134
		write_config();
135

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

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

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

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

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

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

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

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

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

    
211
						<?php else: ?>
212

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

    
260
						<?php endif; ?>
261

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

    
284
internalca_change();
285

    
286
//-->
287
</script>
288

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