Project

General

Profile

Download (8.07 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
##|+PRIV
31
##|*IDENT=page-system-usermanager_addcert
32
##|*NAME=System: User Manager: Add Certificate
33
##|*DESCR=Allow access to the 'User Manager: Add Certificate' page.
34
##|*MATCH=system_usermanager_addcert.php*
35
##|-PRIV
36

    
37
require("guiconfig.inc");
38

    
39
$cert_keylens = array( "512", "1024", "2048", "4096");
40

    
41
$pgtitle = array("System", "User Manager: Add Certificate");
42

    
43
$userid = $_GET['userid'];
44
if (isset($_POST['userid']))
45
	$userid = $_POST['userid'];
46

    
47
if (!is_array($config['system']['user']))
48
	$config['system']['user'] = array();
49

    
50
$a_user =& $config['system']['user'];
51

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

    
55
$a_ca =& $config['system']['ca'];
56

    
57
$internal_ca_count = 0;
58
foreach ($a_ca as $ca)
59
	if ($ca['prv'])	
60
		$internal_ca_count++;
61

    
62
if ($_GET) {
63
	$pconfig['keylen'] = "2048";
64
	$pconfig['lifetime'] = "365";
65
}
66

    
67
if ($_POST) {
68
	conf_mount_rw();
69
	
70
	unset($input_errors);
71
	$pconfig = $_POST;
72

    
73
	/* input validation */
74
	if ($pconfig['method'] == "existing") {
75
		$reqdfields = explode(" ",
76
				"name cert key");
77
		$reqdfieldsn = explode(",",
78
				"Desriptive name,Certificate data,Key data");
79
	}
80

    
81
	if ($pconfig['method'] == "internal") {
82
		$reqdfields = explode(" ",
83
				"name caref keylen lifetime");
84
		$reqdfieldsn = explode(",",
85
				"Desriptive name,Certificate authority,Key length,Lifetime");
86
	}
87

    
88
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
89

    
90
	$ca = lookup_ca($pconfig['caref']);
91
	if (!$ca)
92
		$input_errors[] = "Invalid internal Certificate Authority\n";
93

    
94
	/* if this is an AJAX caller then handle via JSON */
95
	if (isAjax() && is_array($input_errors)) {
96
		input_errors2Ajax($input_errors);
97
		conf_mount_ro();
98
		exit;
99
	}
100

    
101
	/* save modifications */
102
	if (!$input_errors) {
103

    
104
		$cert = array();
105
		if (!is_array($a_user[$userid]['cert']))
106
			$a_user[$userid]['cert'] = array();
107

    
108
	    $cert['name'] = $pconfig['name'];
109

    
110
		$subject = cert_get_subject_array($ca['crt']);
111

    
112
		$dn = array(
113
			'countryName' => $subject[0]['v'],
114
			'stateOrProvinceName' => $subject[1]['v'],
115
			'localityName' => $subject[2]['v'],
116
			'organizationName' => $subject[3]['v'],
117
			'emailAddress' => $subject[4]['v'],
118
			'commonName' => $a_user[$userid]['name']);
119

    
120
		cert_create($cert, $pconfig['caref'], $pconfig['keylen'],
121
			$pconfig['lifetime'], $dn);
122

    
123
		$a_user[$userid]['cert'][] = $cert;
124

    
125
		write_config();
126

    
127
		conf_mount_ro();
128
		
129
		pfSenseHeader("system_usermanager.php?act=edit&id={$userid}");
130
	}
131
}
132

    
133
include("head.inc");
134
?>
135

    
136
<body link="#000000" vlink="#000000" alink="#000000" onload="<?= $jsevents["body"]["onload"] ?>">
137
<?php include("fbegin.inc"); ?>
138
<script type="text/javascript">
139
<!--
140

    
141
<?php if ($internal_ca_count): ?>
142
function internalca_change() {
143

    
144
	index = document.iform.caref.selectedIndex;
145
	caref = document.iform.caref[index].value;
146

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

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

    
192
						<?php if (!$internal_ca_count): ?>
193

    
194
						<tr>
195
							<td colspan="2" align="center" class="vtable">
196
								No internal Certificate Authorities have been defined. You must
197
								<a href="system_camanager.php?act=new&method=internal">create</a>
198
								an internal CA before creating an internal certificate.
199
							</td>
200
						</tr>
201

    
202
						<?php else: ?>
203

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

    
251
						<?php endif; ?>
252

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

    
275
internalca_change();
276

    
277
//-->
278
</script>
279

    
280
</body>
(181-181/206)