Project

General

Profile

Download (8.01 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

    
69
	unset($input_errors);
70
	$pconfig = $_POST;
71

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

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

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

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

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

    
99
	/* save modifications */
100
	if (!$input_errors) {
101

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

    
106
	    $cert['name'] = $pconfig['name'];
107

    
108
		$subject = cert_get_subject_array($ca['crt']);
109

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

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

    
121
		$a_user[$userid]['cert'][] = $cert;
122

    
123
		write_config();
124

    
125
		pfSenseHeader("system_usermanager.php?act=edit&id={$userid}");
126
	}
127
}
128

    
129
include("head.inc");
130
?>
131

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

    
137
<?php if ($internal_ca_count): ?>
138
function internalca_change() {
139

    
140
	index = document.iform.caref.selectedIndex;
141
	caref = document.iform.caref[index].value;
142

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

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

    
188
						<?php if (!$internal_ca_count): ?>
189

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

    
198
						<?php else: ?>
199

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

    
247
						<?php endif; ?>
248

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

    
271
internalca_change();
272

    
273
//-->
274
</script>
275

    
276
</body>
(184-184/209)