Project

General

Profile

Download (8.1 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
require_once("certs.inc");
39

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
126
		write_config();
127

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

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

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

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

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

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

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

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

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

    
203
						<?php else: ?>
204

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

    
252
						<?php endif; ?>
253

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

    
276
internalca_change();
277

    
278
//-->
279
</script>
280

    
281
</body>
(193-193/218)