Project

General

Profile

Download (8.18 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
require("priv.defs.inc");
43
require("priv.inc");
44

    
45
$cert_keylens = array( "512", "1024", "2048", "4096");
46

    
47
$pgtitle = array("System", "User Manager: Add Certificate");
48

    
49
$userid = $_GET['userid'];
50
if (isset($_POST['userid']))
51
	$userid = $_POST['userid'];
52

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

    
56
$a_user =& $config['system']['user'];
57

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

    
61
$a_ca =& $config['system']['ca'];
62

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

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

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

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

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

    
94
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
95

    
96
	$ca = lookup_ca($pconfig['caref']);
97
	if (!$ca)
98
		$input_errors[] = "Invalid internal Certificate Authority\n";
99

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

    
107
	/* save modifications */
108
	if (!$input_errors) {
109

    
110
		$cert = array();
111
		if (!is_array($a_user[$userid]['cert']))
112
			$a_user[$userid]['cert'] = array();
113

    
114
	    $cert['name'] = $pconfig['name'];
115

    
116
		$subject = cert_get_subject_array($ca['crt']);
117

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

    
126
		cert_create($cert, $pconfig['caref'], $pconfig['keylen'],
127
			$pconfig['lifetime'], $dn);
128

    
129
		$a_user[$userid]['cert'][] = $cert;
130

    
131
		write_config();
132

    
133
		conf_mount_ro();
134
		
135
		pfSenseHeader("system_usermanager.php?act=edit&id={$userid}");
136
	}
137
}
138

    
139
include("head.inc");
140
?>
141

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

    
147
<?php if ($internal_ca_count): ?>
148
function internalca_change() {
149

    
150
	index = document.iform.caref.selectedIndex;
151
	caref = document.iform.caref[index].value;
152

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

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

    
198
						<?php if (!$internal_ca_count): ?>
199

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

    
208
						<?php else: ?>
209

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

    
257
						<?php endif; ?>
258

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

    
281
internalca_change();
282

    
283
//-->
284
</script>
285

    
286
</body>
(190-190/215)