Merge branch 'bug-1394' into testserver-stable
[cacert-devel.git] / includes / account.php
1 <? /*
2 LibreSSL - CAcert web application
3 Copyright (C) 2004-2008 CAcert Inc.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; version 2 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 require_once("../includes/loggedin.php");
19 require_once("../includes/lib/l10n.php");
20 require_once("../includes/lib/check_weak_key.php");
21 require_once("../includes/notary.inc.php");
22
23 loadem("account");
24
25 /**
26 * Build a subject string as needed by the signer
27 *
28 * @param array(string) $domains
29 * First domain is used as CN and repeated in subjectAltName. Duplicates
30 * should already been removed
31 *
32 * @param bool $include_xmpp_addr
33 * [default: true] Whether to include the XmppAddr in the subjectAltName.
34 * This is needed if the Jabber server is jabber.example.com but a Jabber ID
35 * on that server would be alice@example.com
36 *
37 * @return string
38 */
39 function buildSubject(array $domains, $include_xmpp_addr = true) {
40 $subject = "/CN=${domains[0]}";
41
42 foreach ($domains as $domain) {
43 $subject .= "/subjectAltName=DNS:$domain";
44
45 if ($include_xmpp_addr) {
46 $subject .= "/subjectAltName=otherName:1.3.6.1.5.5.7.8.5;UTF8:$domain";
47 }
48 }
49
50 return $subject;
51 }
52
53 /**
54 * Builds the subject string from the session variables
55 * $_SESSION['_config']['rows'] and $_SESSION['_config']['altrows']
56 *
57 * @return string
58 */
59 function buildSubjectFromSession() {
60 $domains = array();
61
62 if (is_array($_SESSION['_config']['rows'])) {
63 $domains = array_merge($domains, $_SESSION['_config']['rows']);
64 }
65
66 if (is_array($_SESSION['_config']['altrows']))
67 foreach ($_SESSION['_config']['altrows'] as $row) {
68 if (substr($row, 0, 4) === "DNS:") {
69 $domains[] = substr($row, 4);
70 }
71 }
72
73 return buildSubject(array_unique($domains));
74 }
75
76 $id = array_key_exists("id",$_REQUEST) ? intval($_REQUEST['id']) : 0;
77 $oldid = array_key_exists("oldid",$_REQUEST) ? intval($_REQUEST['oldid']) : 0;
78 $process = array_key_exists("process",$_REQUEST) ? $_REQUEST['process'] : "";
79 // $showdetalis refers to Secret Question and Answers from account/13.php
80 $showdetails = array_key_exists("showdetails",$_REQUEST) ? intval($_REQUEST['showdetails']) : 0;
81
82 $cert = array_key_exists('cert',$_REQUEST) ? intval($_REQUEST['cert']) : 0;
83 $orgid = array_key_exists('orgid',$_REQUEST) ? intval($_REQUEST['orgid']) : 0;
84 $memid = array_key_exists('memid',$_REQUEST) ? intval($_REQUEST['memid']) : 0;
85 $domid = array_key_exists('domid',$_REQUEST) ? intval($_REQUEST['domid']) : 0;
86
87 $actionrequest = array_key_exists('action',$_REQUEST) ? $_REQUEST['action'] : "";
88
89 $ticketno = array_key_exists('ticketno',$_REQUEST) ? $_REQUEST['ticketno'] : "";
90 $ticketvalidation = FALSE;
91
92
93 if(!$_SESSION['mconn'])
94 {
95 echo _("Several CAcert Services are currently unavailable. Please try again later.");
96 exit;
97 }
98
99 if ($process == _("Cancel"))
100 {
101 // General reset CANCEL process requests
102 $process = "";
103 }
104
105
106 if($id == 45 || $id == 46 || $oldid == 45 || $oldid == 46)
107 {
108 $id = 1;
109 $oldid=0;
110 }
111
112 if($process != "" && $oldid == 1)
113 {
114 $id = 1;
115 csrf_check('addemail');
116 if(strstr($_REQUEST['newemail'], "xn--") && $_SESSION['profile']['codesign'] <= 0)
117 {
118 showheader(_("My CAcert.org Account!"));
119 echo _("Due to the possibility for punycode domain exploits we currently only offer the use of IDN domains if your account has the code signing flag.") . "\n";
120 printf(_("More information can be found %sin our wiki%s."), '<a href="//wiki.cacert.org/FAQ/Privilege">', '</a>');
121 showfooter();
122 exit;
123 }
124 if(trim(mysql_real_escape_string(stripslashes($_REQUEST['newemail']))) == "")
125 {
126 showheader(_("My CAcert.org Account!"));
127 printf(_("Not a valid email address. Can't continue."));
128 showfooter();
129 exit;
130 }
131 $oldid=0;
132 $_REQUEST['email'] = trim(mysql_real_escape_string(stripslashes($_REQUEST['newemail'])));
133 if(check_email_exists($_REQUEST['email'])==true)
134 {
135 showheader(_("My CAcert.org Account!"));
136 printf(_("The email address '%s' is already in a different account. Can't continue."), sanitizeHTML($_REQUEST['email']));
137 showfooter();
138 exit;
139 }
140 $checkemail = checkEmail($_REQUEST['newemail']);
141 if($checkemail != "OK")
142 {
143 showheader(_("My CAcert.org Account!"));
144 if (substr($checkemail, 0, 1) == "4")
145 {
146 echo "<p>"._("The mail server responsible for your domain indicated a temporary failure. This may be due to anti-SPAM measures, such as greylisting. Please try again in a few minutes.")."</p>\n";
147 } else {
148 echo "<p>"._("Email Address given was invalid, or a test connection couldn't be made to your server, or the server rejected the email address as invalid")."</p>\n";
149 }
150 echo "<p>$checkemail</p>\n";
151 showfooter();
152 exit;
153 }
154 $hash = make_hash();
155 $query = "insert into `email` set `email`='".$_REQUEST['email']."',`memid`='".intval($_SESSION['profile']['id'])."',`created`=NOW(),`hash`='$hash'";
156 mysql_query($query);
157 $emailid = mysql_insert_id();
158
159 $body = _("Below is the link you need to open to verify your email address. Once your address is verified you will be able to start issuing certificates to your heart's content!")."\n\n";
160 $body .= "http://".$_SESSION['_config']['normalhostname']."/verify.php?type=email&emailid=$emailid&hash=$hash\n\n";
161 $body .= _("Best regards")."\n"._("CAcert.org Support!");
162
163 sendmail($_REQUEST['email'], "[CAcert.org] "._("Email Probe"), $body, "support@cacert.org", "", "", "CAcert Support");
164
165 showheader(_("My CAcert.org Account!"));
166 printf(_("The email address '%s' has been added to the system, however before any certificates for this can be issued you need to open the link in a browser that has been sent to your email address."), sanitizeHTML($_REQUEST['email']));
167 showfooter();
168 exit;
169 }
170
171 if(array_key_exists("makedefault",$_REQUEST) && $_REQUEST['makedefault'] != "" && $oldid == 2)
172 {
173 $id = 2;
174 $emailid = intval($_REQUEST['emailid']);
175 $query = "select * from `email` where `id`='$emailid' and `memid`='".intval($_SESSION['profile']['id'])."' and `hash` = '' and `deleted`=0";
176 $res = mysql_query($query);
177 if(mysql_num_rows($res) <= 0)
178 {
179 showheader(_("Error!"));
180 echo _("You currently don't have access to the email address you selected, or you haven't verified it yet.");
181 showfooter();
182 exit;
183 }
184 $row = mysql_fetch_assoc($res);
185 $body = sprintf(_("Hi %s,"),$_SESSION['profile']['fname'])."\n\n";
186 $body .= _("You are receiving this email because you or someone else ".
187 "has changed the default email on your account.")."\n\n";
188
189 $body .= _("Best regards")."\n"._("CAcert.org Support!");
190
191 sendmail($_SESSION['profile']['email'], "[CAcert.org] "._("Default Account Changed"), $body,
192 "support@cacert.org", "", "", "CAcert Support");
193
194 $_SESSION['profile']['email'] = $row['email'];
195 $query = "update `users` set `email`='".mysql_real_escape_string($row['email'])."' where `id`='".intval($_SESSION['profile']['id'])."'";
196 mysql_query($query);
197 showheader(_("My CAcert.org Account!"));
198 printf(_("Your default email address has been updated to '%s'."), sanitizeHTML($row['email']));
199 showfooter();
200 exit;
201 }
202
203 if($process != "" && $oldid == 2)
204 {
205 $id = 2;
206 csrf_check("chgdef");
207 showheader(_("My CAcert.org Account!"));
208 $delcount = 0;
209 if(array_key_exists('delid',$_REQUEST) && is_array($_REQUEST['delid']))
210 {
211 $deltitle=false;
212 foreach($_REQUEST['delid'] as $id)
213 {
214 if (!$deltitle) {
215 echo _('The following email addresses have been removed:')."<br>\n";
216 $deltitle=true;
217 }
218 $id = intval($id);
219 $query = "select * from `email` where `id`='$id' and `memid`='".intval($_SESSION['profile']['id'])."' and
220 `email`!='".mysql_real_escape_string($_SESSION['profile']['email'])."'";
221 $res = mysql_query($query);
222 if(mysql_num_rows($res) > 0)
223 {
224 $row = mysql_fetch_assoc($res);
225 echo $row['email']."<br>\n";
226 account_email_delete($row['id']);
227 $delcount++;
228 }
229 }
230 }
231 else
232 {
233 echo _("You did not select any email accounts for removal.");
234 }
235 if(0 == $delcount)
236 {
237 echo _("You did not select any accounts to be removed, or you attempted to remove the default account. No action was taken.");
238 }
239
240 showfooter();
241 exit;
242 }
243
244 if($process != "" && $oldid == 3)
245 {
246 if(!array_key_exists('CCA',$_REQUEST))
247 {
248 showheader(_("My CAcert.org Account!"));
249 echo _("You did not accept the CAcert Community Agreement (CCA), hit the back button and try again.");
250 showfooter();
251 exit;
252 }
253
254 if(!(array_key_exists('addid',$_REQUEST) && is_array($_REQUEST['addid'])) && $_REQUEST['SSO'] != '1')
255 {
256 showheader(_("My CAcert.org Account!"));
257 echo _("I didn't receive a valid Certificate Request, hit the back button and try again.");
258 showfooter();
259 exit;
260 }
261
262 $_SESSION['_config']['SSO'] = intval($_REQUEST['SSO']);
263
264 $_SESSION['_config']['addid'] = $_REQUEST['addid'];
265 if($_SESSION['profile']['points'] >= 50)
266 $_SESSION['_config']['incname'] = intval($_REQUEST['incname']);
267 if(array_key_exists('codesign',$_REQUEST) && $_REQUEST['codesign'] != 0 && ($_SESSION['profile']['codesign'] == 0 || $_SESSION['profile']['points'] < 100))
268 {
269 $_REQUEST['codesign'] = 0;
270 }
271 if($_SESSION['profile']['points'] >= 100 && $_SESSION['profile']['codesign'] > 0 && array_key_exists('codesign',$_REQUEST) && $_REQUEST['codesign'] == 1)
272 {
273 if($_SESSION['_config']['incname'] < 1 || $_SESSION['_config']['incname'] > 4)
274 $_SESSION['_config']['incname'] = 1;
275 }
276 if(array_key_exists('codesign',$_REQUEST) && $_REQUEST['codesign'] == 1 && $_SESSION['profile']['points'] >= 100)
277 $_SESSION['_config']['codesign'] = 1;
278 else
279 $_SESSION['_config']['codesign'] = 0;
280
281 if(array_key_exists('login',$_REQUEST) && $_REQUEST['login'] == 1)
282 $_SESSION['_config']['disablelogin'] = 0;
283 else
284 $_SESSION['_config']['disablelogin'] = 1;
285
286 $_SESSION['_config']['rootcert'] = 1;
287 if($_SESSION['profile']['points'] >= 50)
288 {
289 $_SESSION['_config']['rootcert'] = intval($_REQUEST['rootcert']);
290 if($_SESSION['_config']['rootcert'] < 1 || $_SESSION['_config']['rootcert'] > 2)
291 $_SESSION['_config']['rootcert'] = 1;
292 }
293
294 $_SESSION['_config']['hash_alg'] = HashAlgorithms::clean($_REQUEST['hash_alg']);
295
296 $csr = "";
297 if(trim($_REQUEST['optionalCSR']) == "")
298 {
299 $id = 4;
300 } else {
301 $oldid = 4;
302 $_REQUEST['keytype'] = "MS";
303 $csr = clean_csr($_REQUEST['optionalCSR']);
304 }
305
306 $_SESSION['_config']['description']= trim(stripslashes($_REQUEST['description']));
307 }
308
309 if($oldid == 4)
310 {
311 if($_REQUEST['keytype'] == "NS")
312 {
313 $spkac=""; if(array_key_exists('SPKAC',$_REQUEST) && preg_match("/^[a-zA-Z0-9+=\/]+$/", trim(str_replace("\n", "", str_replace("\r", "",$_REQUEST['SPKAC']))))) $spkac=trim(str_replace("\n", "", str_replace("\r", "",$_REQUEST['SPKAC'])));
314
315 if($spkac=="" || $spkac == "deadbeef")
316 {
317 $id = 4;
318 showheader(_("My CAcert.org Account!"));
319 echo _("I didn't receive a valid Certificate Request, please try a different browser.");
320 showfooter();
321 exit;
322 }
323 $count = 0;
324 $emails = "";
325 $addys = array();
326 $defaultemail="";
327 if(is_array($_SESSION['_config']['addid']))
328 foreach($_SESSION['_config']['addid'] as $id)
329 {
330 $res = mysql_query("select * from `email` where `memid`='".intval($_SESSION['profile']['id'])."' and `id`='".intval($id)."'");
331 if(mysql_num_rows($res) > 0)
332 {
333 $row = mysql_fetch_assoc($res);
334 if(!$emails)
335 $defaultemail = $row['email'];
336 $emails .= "$count.emailAddress = ".$row['email']."\n";
337 $count++;
338 $addys[] = intval($row['id']);
339 }
340 }
341 if($count <= 0 && $_SESSION['_config']['SSO'] != 1)
342 {
343 $id = 4;
344 showheader(_("My CAcert.org Account!"));
345 echo _("You submitted invalid email addresses, or email address you no longer have control of. Can't continue with certificate request.");
346 showfooter();
347 exit;
348 }
349 $user = mysql_fetch_assoc(mysql_query("select * from `users` where `id`='".intval($_SESSION['profile']['id'])."'"));
350 if($_SESSION['_config']['SSO'] == 1)
351 $emails .= "$count.emailAddress = ".$user['uniqueID']."\n";
352
353 if(strlen($user['mname']) == 1)
354 $user['mname'] .= '.';
355 if(!array_key_exists('incname',$_SESSION['_config']) || $_SESSION['_config']['incname'] <= 0 || $_SESSION['_config']['incname'] > 4)
356 {
357 $emails .= "commonName = CAcert WoT User\n";
358 }
359 else
360 {
361 if($_SESSION['_config']['incname'] == 1)
362 $emails .= "commonName = ".$user['fname']." ".$user['lname']."\n";
363 if($_SESSION['_config']['incname'] == 2)
364 $emails .= "commonName = ".$user['fname']." ".$user['mname']." ".$user['lname']."\n";
365 if($_SESSION['_config']['incname'] == 3)
366 $emails .= "commonName = ".$user['fname']." ".$user['lname']." ".$user['suffix']."\n";
367 if($_SESSION['_config']['incname'] == 4)
368 $emails .= "commonName = ".$user['fname']." ".$user['mname']." ".$user['lname']." ".$user['suffix']."\n";
369 }
370 if($_SESSION['_config']['rootcert'] < 1 || $_SESSION['_config']['rootcert'] > 2)
371 $_SESSION['_config']['rootcert'] = 1;
372
373 $emails .= "SPKAC = $spkac";
374 if (($weakKey = checkWeakKeySPKAC($emails)) !== "")
375 {
376 $id = 4;
377 showheader(_("My CAcert.org Account!"));
378 echo $weakKey;
379 showfooter();
380 exit;
381 }
382
383 write_user_agreement(intval($_SESSION['profile']['id']), "CCA", "certificate creation", "", 1);
384
385 $query = "insert into emailcerts set
386 `CN`='$defaultemail',
387 `keytype`='NS',
388 `memid`='".intval($_SESSION['profile']['id'])."',
389 `created`=FROM_UNIXTIME(UNIX_TIMESTAMP()),
390 `codesign`='".intval($_SESSION['_config']['codesign'])."',
391 `disablelogin`='".($_SESSION['_config']['disablelogin']?1:0)."',
392 `rootcert`='".intval($_SESSION['_config']['rootcert'])."',
393 `md`='".mysql_real_escape_string($_SESSION['_config']['hash_alg'])."',
394 `description`='".mysql_real_escape_string($_SESSION['_config']['description'])."'";
395 mysql_query($query);
396 $emailid = mysql_insert_id();
397 if(is_array($addys))
398 foreach($addys as $addy)
399 mysql_query("insert into `emaillink` set `emailcertsid`='$emailid', `emailid`='$addy'");
400 $CSRname=generatecertpath("csr","client",$emailid);
401 $fp = fopen($CSRname, "w");
402 fputs($fp, $emails);
403 fclose($fp);
404 $challenge=$_SESSION['spkac_hash'];
405 $CSRname_esc = escapeshellarg($CSRname);
406 $res=shell_exec("openssl spkac -verify -in $CSRname_esc");
407 if(!strstr($res,"Challenge String: ".$challenge))
408 {
409 $id = $oldid;
410 showheader(_("My CAcert.org Account!"));
411 echo _("The challenge-response code of your certificate request did not match. Can't continue with certificaterequest.");
412 showfooter();
413 exit;
414 }
415 mysql_query("update `emailcerts` set `csr_name`='$CSRname' where `id`='".intval($emailid)."'");
416 } else if($_REQUEST['keytype'] == "MS" || $_REQUEST['keytype'] == "VI") {
417 if($csr == "")
418 $csr = "-----BEGIN CERTIFICATE REQUEST-----\n".clean_csr($_REQUEST['CSR'])."\n-----END CERTIFICATE REQUEST-----\n";
419
420 if (($weakKey = checkWeakKeyCSR($csr)) !== "")
421 {
422 $id = 4;
423 showheader(_("My CAcert.org Account!"));
424 echo $weakKey;
425 showfooter();
426 exit;
427 }
428
429 $tmpfname = tempnam("/tmp", "id4CSR");
430 $fp = fopen($tmpfname, "w");
431 fputs($fp, $csr);
432 fclose($fp);
433
434 $addys = array();
435 $defaultemail = "";
436 $csrsubject="";
437
438 $user = mysql_fetch_assoc(mysql_query("select * from `users` where `id`='".intval($_SESSION['profile']['id'])."'"));
439 if(strlen($user['mname']) == 1)
440 $user['mname'] .= '.';
441 if($_SESSION['_config']['incname'] <= 0 || $_SESSION['_config']['incname'] > 4)
442 $csrsubject = "/CN=CAcert WoT User";
443 if($_SESSION['_config']['incname'] == 1)
444 $csrsubject = "/CN=".$user['fname']." ".$user['lname'];
445 if($_SESSION['_config']['incname'] == 2)
446 $csrsubject = "/CN=".$user['fname']." ".$user['mname']." ".$user['lname'];
447 if($_SESSION['_config']['incname'] == 3)
448 $csrsubject = "/CN=".$user['fname']." ".$user['lname']." ".$user['suffix'];
449 if($_SESSION['_config']['incname'] == 4)
450 $csrsubject = "/CN=".$user['fname']." ".$user['mname']." ".$user['lname']." ".$user['suffix'];
451 if(is_array($_SESSION['_config']['addid']))
452 foreach($_SESSION['_config']['addid'] as $id)
453 {
454 $res = mysql_query("select * from `email` where `memid`='".intval($_SESSION['profile']['id'])."' and `id`='".intval($id)."'");
455 if(mysql_num_rows($res) > 0)
456 {
457 $row = mysql_fetch_assoc($res);
458 if($defaultemail == "")
459 $defaultemail = $row['email'];
460 $csrsubject .= "/emailAddress=".$row['email'];
461 $addys[] = $row['id'];
462 }
463 }
464 if($_SESSION['_config']['SSO'] == 1)
465 $csrsubject .= "/emailAddress = ".$user['uniqueID'];
466
467 $tmpname = tempnam("/tmp", "id4csr");
468 $tmpfname_esc = escapeshellarg($tmpfname);
469 $tmpname_esc = escapeshellarg($tmpname);
470 $do = shell_exec("/usr/bin/openssl req -in $tmpfname_esc -out $tmpname_esc"); // -subj "$csr";
471 @unlink($tmpfname);
472 $csr = "";
473 $fp = fopen($tmpname, "r");
474 while($data = fgets($fp, 4096))
475 $csr .= $data;
476 fclose($fp);
477 @unlink($tmpname);
478 if($_SESSION['_config']['rootcert'] < 1 || $_SESSION['_config']['rootcert'] > 2)
479 $_SESSION['_config']['rootcert'] = 1;
480
481 if($csr == "")
482 {
483 $id = 4;
484 showheader(_("My CAcert.org Account!"));
485 echo _("I didn't receive a valid Certificate Request, hit the back button and try again.");
486 showfooter();
487 exit;
488 }
489 $query = "insert into emailcerts set
490 `CN`='$defaultemail',
491 `keytype`='".sanitizeHTML($_REQUEST['keytype'])."',
492 `memid`='".intval($_SESSION['profile']['id'])."',
493 `created`=FROM_UNIXTIME(UNIX_TIMESTAMP()),
494 `subject`='".mysql_real_escape_string($csrsubject)."',
495 `codesign`='".intval($_SESSION['_config']['codesign'])."',
496 `disablelogin`='".($_SESSION['_config']['disablelogin']?1:0)."',
497 `rootcert`='".intval($_SESSION['_config']['rootcert'])."',
498 `md`='".mysql_real_escape_string($_SESSION['_config']['hash_alg'])."',
499 `description`='".mysql_real_escape_string($_SESSION['_config']['description'])."'";
500 mysql_query($query);
501 $emailid = mysql_insert_id();
502 if(is_array($addys))
503 foreach($addys as $addy)
504 mysql_query("insert into `emaillink` set `emailcertsid`='$emailid', `emailid`='".mysql_real_escape_string($addy)."'");
505 $CSRname=generatecertpath("csr","client",$emailid);
506 $fp = fopen($CSRname, "w");
507 fputs($fp, $csr);
508 fclose($fp);
509 mysql_query("update `emailcerts` set `csr_name`='$CSRname' where `id`='$emailid'");
510 }
511 waitForResult("emailcerts", $emailid, 4);
512 $query = "select * from `emailcerts` where `id`='$emailid' and `crt_name` != ''";
513 $res = mysql_query($query);
514 if(mysql_num_rows($res) <= 0)
515 {
516 $id = 4;
517 showheader(_("My CAcert.org Account!"));
518 printf(_("Your certificate request has failed to be processed correctly, see %sthe WIKI page%s for reasons and solutions."), "<a href='http://wiki.cacert.org/wiki/FAQ/CertificateRenewal'>", "</a>");
519 showfooter();
520 exit;
521 } else {
522 $id = 6;
523 $cert = $emailid;
524 $_REQUEST['cert']=$emailid;
525 }
526 }
527
528 if($oldid == 7)
529 {
530 csrf_check("adddomain");
531 if(strstr($_REQUEST['newdomain'],"\x00"))
532 {
533 showheader(_("My CAcert.org Account!"));
534 echo _("Due to the possibility for nullbyte domain exploits we currently do not allow any domain names with nullbytes.");
535 showfooter();
536 exit;
537 }
538
539 list($newdomain) = explode(" ", $_REQUEST['newdomain'], 2); // Ignore the rest
540 while($newdomain['0'] == '-')
541 $newdomain = substr($newdomain, 1);
542 if(strstr($newdomain, "xn--") && $_SESSION['profile']['codesign'] <= 0)
543 {
544 showheader(_("My CAcert.org Account!"));
545 echo _("Due to the possibility for punycode domain exploits we currently only offer the use of IDN domains if your account has the code signing flag.") . "\n";
546 printf(_("More information can be found %sin our wiki%s."),'<a href="//wiki.cacert.org/FAQ/Privilege">', '</a>');
547 showfooter();
548 exit;
549 }
550
551 $newdom = trim(escapeshellarg($newdomain));
552 $newdomain = mysql_real_escape_string(trim($newdomain));
553
554 $res1 = mysql_query("select * from `orgdomains` where `domain`='$newdomain'");
555 $query = "select * from `domains` where `domain`='$newdomain' and `deleted`=0";
556 $res2 = mysql_query($query);
557 if(mysql_num_rows($res1) > 0 || mysql_num_rows($res2))
558 {
559 $oldid=0;
560 $id = 7;
561 showheader(_("My CAcert.org Account!"));
562 printf(_("The domain '%s' is already in a different account and is listed as valid. Can't continue."), sanitizeHTML($newdomain));
563 showfooter();
564 exit;
565 }
566 }
567
568 if($oldid == 7)
569 {
570 $oldid=0;
571 $id = 8;
572 $addy = array();
573 $adds = array();
574 if(strtolower(substr($newdom, -4, 3)) != ".jp")
575 $adds = explode("\n", trim(shell_exec("/usr/bin/whois $newdom|grep \"@\"")));
576 if(substr($newdomain, -4) == ".org" || substr($newdomain, -5) == ".info")
577 {
578 if(is_array($adds))
579 foreach($adds as $line)
580 {
581 $bits = explode(":", $line, 2);
582 $line = trim($bits[1]);
583 if(!in_array($line, $addy) && $line != "")
584 $addy[] = trim(mysql_real_escape_string(stripslashes($line)));
585 }
586 } else {
587 if(is_array($adds))
588 foreach($adds as $line)
589 {
590 $line = trim(str_replace("\t", " ", $line));
591 $line = trim(str_replace("(", "", $line));
592 $line = trim(str_replace(")", " ", $line));
593 $line = trim(str_replace(":", " ", $line));
594
595 $bits = explode(" ", $line);
596 foreach($bits as $bit)
597 {
598 if(strstr($bit, "@"))
599 $line = $bit;
600 }
601 if(!in_array($line, $addy) && $line != "")
602 $addy[] = trim(mysql_real_escape_string(stripslashes($line)));
603 }
604 }
605
606 $rfc = array("root@$newdomain", "hostmaster@$newdomain", "postmaster@$newdomain", "admin@$newdomain", "webmaster@$newdomain");
607 foreach($rfc as $sub)
608 if(!in_array($sub, $addy))
609 $addy[] = $sub;
610 $_SESSION['_config']['addy'] = $addy;
611 $_SESSION['_config']['domain'] = mysql_real_escape_string($newdomain);
612 }
613
614 if($process != "" && $oldid == 8)
615 {
616 csrf_check('ctcinfo');
617 $oldid=0;
618 $id = 8;
619
620 $authaddy = trim(mysql_real_escape_string(stripslashes($_REQUEST['authaddy'])));
621
622 if($authaddy == "" || !is_array($_SESSION['_config']['addy']))
623 {
624 showheader(_("My CAcert.org Account!"));
625 echo _("The address you submitted isn't a valid authority address for the domain.");
626 showfooter();
627 exit;
628 }
629
630 if(!in_array($authaddy, $_SESSION['_config']['addy']))
631 {
632 showheader(_("My CAcert.org Account!"));
633 echo _("The address you submitted isn't a valid authority address for the domain.");
634 showfooter();
635 exit;
636 }
637
638 $query = "select * from `domains` where `domain`='".mysql_real_escape_string($_SESSION['_config']['domain'])."' and `deleted`=0";
639 $res = mysql_query($query);
640 if(mysql_num_rows($res) > 0)
641 {
642 showheader(_("My CAcert.org Account!"));
643 printf(_("The domain '%s' is already in a different account and is listed as valid. Can't continue."), sanitizeHTML($_SESSION['_config']['domain']));
644 showfooter();
645 exit;
646 }
647 $checkemail = checkEmail($authaddy);
648 if($checkemail != "OK")
649 {
650 showheader(_("My CAcert.org Account!"));
651 //echo "<p>"._("Email Address given was invalid, or a test connection couldn't be made to your server, or the server rejected the email address as invalid")."</p>\n";
652 if (substr($checkemail, 0, 1) == "4")
653 {
654 echo "<p>"._("The mail server responsible for your domain indicated a temporary failure. This may be due to anti-SPAM measures, such as greylisting. Please try again in a few minutes.")."</p>\n";
655 } else {
656 echo "<p>"._("Email Address given was invalid, or a test connection couldn't be made to your server, or the server rejected the email address as invalid")."</p>\n";
657 }
658 echo "<p>$checkemail</p>\n";
659 showfooter();
660 exit;
661 }
662
663 $hash = make_hash();
664 $query = "insert into `domains` set `domain`='".mysql_real_escape_string($_SESSION['_config']['domain'])."',
665 `memid`='".intval($_SESSION['profile']['id'])."',`created`=NOW(),`hash`='$hash'";
666 mysql_query($query);
667 $domainid = mysql_insert_id();
668
669 $body = sprintf(_("Below is the link you need to open to verify your domain '%s'. Once your address is verified you will be able to start issuing certificates to your heart's content!"),$_SESSION['_config']['domain'])."\n\n";
670 $body .= "http://".$_SESSION['_config']['normalhostname']."/verify.php?type=domain&domainid=$domainid&hash=$hash\n\n";
671 $body .= _("Best regards")."\n"._("CAcert.org Support!");
672
673 sendmail($authaddy, "[CAcert.org] "._("Email Probe"), $body, "support@cacert.org", "", "", "CAcert Support");
674
675 showheader(_("My CAcert.org Account!"));
676 printf(_("The domain '%s' has been added to the system, however before any certificates for this can be issued you need to open the link in a browser that has been sent to your email address."), $_SESSION['_config']['domain']);
677 showfooter();
678 exit;
679 }
680
681 if($process != "" && $oldid == 9)
682 {
683 $id = 9;
684 showheader(_("My CAcert.org Account!"));
685 if(array_key_exists('delid',$_REQUEST) && is_array($_REQUEST['delid']))
686 {
687 echo _("The following domains have been removed:")."<br>
688 ("._("Any valid certificates will be revoked as well").")<br>\n";
689
690 foreach($_REQUEST['delid'] as $id)
691 {
692 $id = intval($id);
693 $query = "select * from `domains` where `id`='$id' and `memid`='".intval($_SESSION['profile']['id'])."'";
694 $res = mysql_query($query);
695 if(mysql_num_rows($res) > 0)
696 {
697 $row = mysql_fetch_assoc($res);
698 echo $row['domain']."<br>\n";
699 account_domain_delete($row['id']);
700 }
701
702 }
703 }
704 else
705 {
706 echo _("You did not select any domains for removal.");
707 }
708
709 showfooter();
710 exit;
711 }
712
713 if($process != "" && $oldid == 10)
714 {
715 if(!array_key_exists('CCA',$_REQUEST))
716 {
717 showheader(_("My CAcert.org Account!"));
718 echo _("You did not accept the CAcert Community Agreement (CCA), hit the back button and try again.");
719 showfooter();
720 exit;
721 }
722
723 $CSR = clean_csr($_REQUEST['CSR']);
724 if(strpos($CSR,"---BEGIN")===FALSE)
725 {
726 // In case the CSR is missing the ---BEGIN lines, add them automatically:
727 $CSR = "-----BEGIN CERTIFICATE REQUEST-----\n".$CSR."\n-----END CERTIFICATE REQUEST-----\n";
728 }
729
730 if (($weakKey = checkWeakKeyCSR($CSR)) !== "")
731 {
732 showheader(_("My CAcert.org Account!"));
733 echo $weakKey;
734 showfooter();
735 exit;
736 }
737
738 $_SESSION['_config']['description']= trim(stripslashes($_REQUEST['description']));
739
740 $_SESSION['_config']['tmpfname'] = tempnam("/tmp", "id10CSR");
741 $fp = fopen($_SESSION['_config']['tmpfname'], "w");
742 fputs($fp, $CSR);
743 fclose($fp);
744 $CSR = escapeshellarg($_SESSION['_config']['tmpfname']);
745 $_SESSION['_config']['subject'] = trim(shell_exec("/usr/bin/openssl req -text -noout -in $CSR |tr -d \"\\0\"|grep \"Subject:\""));
746 $bits = explode(",", trim(shell_exec("/usr/bin/openssl req -text -noout -in $CSR |tr -d \"\\0\"|grep -A1 'X509v3 Subject Alternative Name:'|grep DNS:")));
747 foreach($bits as $val)
748 {
749 $_SESSION['_config']['subject'] .= "/subjectAltName=".trim($val);
750 }
751 $id = 11;
752
753 $_SESSION['_config']['0.CN'] = $_SESSION['_config']['0.subjectAltName'] = "";
754 extractit();
755 getcn();
756 getalt();
757
758 if($_SESSION['_config']['0.CN'] == "" && $_SESSION['_config']['0.subjectAltName'] == "")
759 {
760 showheader(_("My CAcert.org Account!"));
761 echo _("CommonName field was blank. This is usually caused by entering your own name when openssl prompt's you for 'YOUR NAME', or if you try to issue certificates for domains you haven't already verified, as such this process can't continue.");
762 showfooter();
763 exit;
764 }
765
766 $_SESSION['_config']['rootcert'] = 1;
767 if($_SESSION['profile']['points'] >= 50)
768 {
769 $_SESSION['_config']['rootcert'] = intval($_REQUEST['rootcert']);
770 if($_SESSION['_config']['rootcert'] < 1 || $_SESSION['_config']['rootcert'] > 2)
771 $_SESSION['_config']['rootcert'] = 1;
772 }
773
774 $_SESSION['_config']['hash_alg'] = HashAlgorithms::clean($_REQUEST['hash_alg']);
775 }
776
777 if($process != "" && $oldid == 11)
778 {
779 if(!file_exists($_SESSION['_config']['tmpfname']))
780 {
781 showheader(_("My CAcert.org Account!"));
782 printf(_("Your certificate request has failed to be processed correctly, see %sthe WIKI page%s for reasons and solutions."), "<a href='http://wiki.cacert.org/wiki/FAQ/CertificateRenewal'>", "</a>");
783 showfooter();
784 exit;
785 }
786
787 if (($weakKey = checkWeakKeyCSR(file_get_contents(
788 $_SESSION['_config']['tmpfname']))) !== "")
789 {
790 showheader(_("My CAcert.org Account!"));
791 echo $weakKey;
792 showfooter();
793 exit;
794 }
795
796 $id = 11;
797 if($_SESSION['_config']['0.CN'] == "" && $_SESSION['_config']['0.subjectAltName'] == "")
798 {
799 showheader(_("My CAcert.org Account!"));
800 echo _("CommonName field was blank. This is usually caused by entering your own name when openssl prompt's you for 'YOUR NAME', or if you try to issue certificates for domains you haven't already verified, as such this process can't continue.");
801 showfooter();
802 exit;
803 }
804
805 $subject = buildSubjectFromSession();
806
807 if($_SESSION['_config']['rootcert'] < 1 || $_SESSION['_config']['rootcert'] > 2)
808 $_SESSION['_config']['rootcert'] = 1;
809
810 write_user_agreement(intval($_SESSION['profile']['id']), "CCA", "certificate creation", "", 1);
811
812 if(array_key_exists('0',$_SESSION['_config']['rowid']) && $_SESSION['_config']['rowid']['0'] > 0)
813 {
814 $query = "insert into `domaincerts` set
815 `CN`='".mysql_real_escape_string($_SESSION['_config']['rows']['0'])."',
816 `domid`='".mysql_real_escape_string($_SESSION['_config']['rowid']['0'])."',
817 `created`=NOW(),`subject`='".mysql_real_escape_string($subject)."',
818 `rootcert`='".mysql_real_escape_string($_SESSION['_config']['rootcert'])."',
819 `md`='".mysql_real_escape_string($_SESSION['_config']['hash_alg'])."',
820 `description`='".mysql_real_escape_string($_SESSION['_config']['description'])."'";
821 } elseif(array_key_exists('0',$_SESSION['_config']['altid']) && $_SESSION['_config']['altid']['0'] > 0) {
822 $query = "insert into `domaincerts` set
823 `CN`='".mysql_real_escape_string($_SESSION['_config']['altrows']['0'])."',
824 `domid`='".mysql_real_escape_string($_SESSION['_config']['altid']['0'])."',
825 `created`=NOW(),`subject`='".mysql_real_escape_string($subject)."',
826 `rootcert`='".mysql_real_escape_string($_SESSION['_config']['rootcert'])."',
827 `md`='".mysql_real_escape_string($_SESSION['_config']['hash_alg'])."',
828 `description`='".mysql_real_escape_string($_SESSION['_config']['description'])."'";
829 } else {
830 showheader(_("My CAcert.org Account!"));
831 echo _("Domain not verified.");
832 showfooter();
833 exit;
834 }
835
836 mysql_query($query);
837 $CSRid = mysql_insert_id();
838
839 if(is_array($_SESSION['_config']['rowid']))
840 foreach($_SESSION['_config']['rowid'] as $dom)
841 mysql_query("insert into `domlink` set `certid`='$CSRid', `domid`='$dom'");
842 if(is_array($_SESSION['_config']['altid']))
843 foreach($_SESSION['_config']['altid'] as $dom)
844 mysql_query("insert into `domlink` set `certid`='$CSRid', `domid`='$dom'");
845
846 $CSRname=generatecertpath("csr","server",$CSRid);
847 rename($_SESSION['_config']['tmpfname'], $CSRname);
848 chmod($CSRname,0644);
849 mysql_query("update `domaincerts` set `CSR_name`='$CSRname' where `id`='$CSRid'");
850 waitForResult("domaincerts", $CSRid, 11);
851 $query = "select * from `domaincerts` where `id`='$CSRid' and `crt_name` != ''";
852 $res = mysql_query($query);
853 if(mysql_num_rows($res) <= 0)
854 {
855 $id = 11;
856 showheader(_("My CAcert.org Account!"));
857 printf(_("Your certificate request has failed to be processed correctly, see %sthe WIKI page%s for reasons and solutions."), "<a href='http://wiki.cacert.org/wiki/FAQ/CertificateRenewal'>", "</a>");
858 showfooter();
859 exit;
860 } else {
861 $id = 15;
862 $cert = $CSRid;
863 $_REQUEST['cert']=$CSRid;
864 }
865 }
866
867 if($oldid == 12 && array_key_exists('renew',$_REQUEST) && $_REQUEST['renew'] != "")
868 {
869 csrf_check('srvcerchange');
870 $id = 12;
871 showheader(_("My CAcert.org Account!"));
872 if(is_array($_REQUEST['revokeid']))
873 {
874 echo _("Now renewing the following certificates:")."<br>\n";
875 foreach($_REQUEST['revokeid'] as $id)
876 {
877 $id = intval($id);
878 echo _("Processing request")." $id:<br/>";
879 $query = "select *,UNIX_TIMESTAMP(`domaincerts`.`revoked`) as `revoke` from `domaincerts`,`domains`
880 where `domaincerts`.`id`='$id' and
881 `domaincerts`.`domid`=`domains`.`id` and
882 `domains`.`memid`='".intval($_SESSION['profile']['id'])."'";
883 $res = mysql_query($query);
884 if(mysql_num_rows($res) <= 0)
885 {
886 printf(_("Invalid ID '%s' presented, can't do anything with it.")."<br/>\n", $id);
887 continue;
888 }
889
890 $row = mysql_fetch_assoc($res);
891
892 if (($weakKey = checkWeakKeyX509(file_get_contents(
893 $row['crt_name']))) !== "")
894 {
895 echo $weakKey, "<br/>\n";
896 continue;
897 }
898
899 mysql_query("update `domaincerts` set `renewed`='1' where `id`='$id'");
900 $query = "insert into `domaincerts` set
901 `domid`='".intval($row['domid'])."',
902 `CN`='".mysql_real_escape_string($row['CN'])."',
903 `subject`='".mysql_real_escape_string($row['subject'])."',".
904 //`csr_name`='".$row['csr_name']."', // RACE CONDITION
905 "`created`='".mysql_real_escape_string($row['created'])."',
906 `modified`=NOW(),
907 `rootcert`='".intval($row['rootcert'])."',
908 `type`='".intval($row['type'])."',
909 `pkhash`='".mysql_real_escape_string($row['pkhash'])."',
910 `description`='".mysql_real_escape_string($row['description'])."',
911 `md`='".HashAlgorithms::clean($row['md'])."'";
912 mysql_query($query);
913 $newid = mysql_insert_id();
914 $newfile=generatecertpath("csr","server",$newid);
915 copy($row['csr_name'], $newfile);
916 $newfile_esc = escapeshellarg($newfile);
917 $_SESSION['_config']['subject'] = trim(shell_exec("/usr/bin/openssl req -text -noout -in $newfile_esc |tr -d \"\\0\"|grep \"Subject:\""));
918 $bits = explode(",", trim(shell_exec("/usr/bin/openssl req -text -noout -in $newfile_esc |tr -d \"\\0\"|grep -A1 'X509v3 Subject Alternative Name:'|grep DNS:")));
919 foreach($bits as $val)
920 {
921 $_SESSION['_config']['subject'] .= "/subjectAltName=".trim($val);
922 }
923 $_SESSION['_config']['0.CN'] = $_SESSION['_config']['0.subjectAltName'] = "";
924 extractit();
925 getcn();
926 getalt();
927
928 if($_SESSION['_config']['0.CN'] == "" && $_SESSION['_config']['0.subjectAltName'] == "")
929 {
930 echo _("CommonName field was blank. This is usually caused by entering your own name when openssl prompt's you for 'YOUR NAME', or if you try to issue certificates for domains you haven't already verified, as such this process can't continue.");
931 continue;
932 }
933
934 $subject = buildSubjectFromSession();
935 $subject = mysql_real_escape_string($subject);
936 mysql_query("update `domaincerts` set `subject`='$subject',`csr_name`='$newfile' where `id`='$newid'");
937
938 echo _("Renewing").": ".sanitizeHTML($_SESSION['_config']['0.CN'])."<br>\n";
939 waitForResult("domaincerts", $newid,$oldid,0);
940 $query = "select * from `domaincerts` where `id`='$newid' and `crt_name` != ''";
941 $res = mysql_query($query);
942 if(mysql_num_rows($res) <= 0)
943 {
944 printf(_("Your certificate request has failed to be processed correctly, see %sthe WIKI page%s for reasons and solutions."), "<a href='http://wiki.cacert.org/wiki/FAQ/CertificateRenewal'>", "</a>");
945 } else {
946 $drow = mysql_fetch_assoc($res);
947 $crt_name = escapeshellarg($drow['crt_name']);
948 $cert = shell_exec("/usr/bin/openssl x509 -in $crt_name");
949 echo "<pre>\n$cert\n</pre>\n";
950 }
951 }
952 }
953 else
954 {
955 echo _("You did not select any certificates for renewal.");
956 }
957
958 showfooter();
959 exit;
960 }
961
962 if($oldid == 12 && array_key_exists('revoke',$_REQUEST) && $_REQUEST['revoke'] != "")
963 {
964 csrf_check('srvcerchange');
965 $id = 12;
966 showheader(_("My CAcert.org Account!"));
967 if(is_array($_REQUEST['revokeid']))
968 {
969 echo _("Now revoking the following certificates:")."<br>\n";
970 foreach($_REQUEST['revokeid'] as $id)
971 {
972 $id = intval($id);
973 $query = "select *,UNIX_TIMESTAMP(`domaincerts`.`revoked`) as `revoke` from `domaincerts`,`domains`
974 where `domaincerts`.`id`='$id' and
975 `domaincerts`.`domid`=`domains`.`id` and
976 `domains`.`memid`='".intval($_SESSION['profile']['id'])."'";
977 $res = mysql_query($query);
978 if(mysql_num_rows($res) <= 0)
979 {
980 printf(_("Invalid ID '%s' presented, can't do anything with it.")."<br>\n", $id);
981 continue;
982 }
983 $row = mysql_fetch_assoc($res);
984 if($row['revoke'] > 0)
985 {
986 printf(_("It would seem '%s' has already been revoked. I'll skip this for now.")."<br>\n", $row['CN']);
987 continue;
988 }
989 mysql_query("update `domaincerts` set `revoked`='1970-01-01 10:00:01' where `id`='$id'");
990 printf(_("Certificate for '%s' with the serial no '%s' has been revoked.").'<br/>', htmlspecialchars($row['CN']), htmlspecialchars($row['serial']));
991 }
992
993 // TRANSLATORS: Please don't translate "Certificate Revocation List (CRL)", it's a technical term
994 echo '<br/>'._('All listed certificates will be added to the Certificate Revocation List (CRL) soon.').'<br/>';
995
996 }
997 else
998 {
999 echo _("You did not select any certificates for revocation.");
1000 }
1001
1002 if(array_key_exists('delid',$_REQUEST) && is_array($_REQUEST['delid']))
1003 {
1004 echo _("Now deleting the following pending requests:")."<br>\n";
1005 foreach($_REQUEST['delid'] as $id)
1006 {
1007 $id = intval($id);
1008 $query = "select *,UNIX_TIMESTAMP(`domaincerts`.`expire`) as `expired` from `domaincerts`,`domains`
1009 where `domaincerts`.`id`='$id' and
1010 `domaincerts`.`domid`=`domains`.`id` and
1011 `domains`.`memid`='".intval($_SESSION['profile']['id'])."'";
1012 $res = mysql_query($query);
1013 if(mysql_num_rows($res) <= 0)
1014 {
1015 printf(_("Invalid ID '%s' presented, can't do anything with it.")."<br>\n", $id);
1016 continue;
1017 }
1018 $row = mysql_fetch_assoc($res);
1019 if($row['expired'] > 0)
1020 {
1021 printf(_("Couldn't remove the request for `%s`, request had already been processed.")."<br>\n", $row['CN']);
1022 continue;
1023 }
1024 mysql_query("delete from `domaincerts` where `id`='$id'");
1025 @unlink($row['csr_name']);
1026 @unlink($row['crt_name']);
1027 printf(_("Removed a pending request for '%s'")."<br>\n", $row['CN']);
1028 }
1029 }
1030 showfooter();
1031 exit;
1032 }
1033
1034 if($oldid == 12 && array_key_exists('change',$_REQUEST) && $_REQUEST['change'] != "")
1035 {
1036 showheader(_("My CAcert.org Account!"));
1037 foreach($_REQUEST as $id => $val)
1038 {
1039 if(substr($id,0,14)=="check_comment_")
1040 {
1041 $cid = intval(substr($id,14));
1042 $comment=trim(mysql_real_escape_string(stripslashes($_REQUEST['comment_'.$cid])));
1043 mysql_query("update `domaincerts` set `description`='$comment' where `id`='$cid'");
1044 }
1045 }
1046 echo(_("Certificate settings have been changed.")."<br/>\n");
1047 showfooter();
1048 exit;
1049 }
1050
1051
1052 if($oldid == 5 && array_key_exists('renew',$_REQUEST) && $_REQUEST['renew'] != "")
1053 {
1054 showheader(_("My CAcert.org Account!"));
1055 if(is_array($_REQUEST['revokeid']))
1056 {
1057 echo _("Now renewing the following certificates:")."<br>\n";
1058 foreach($_REQUEST['revokeid'] as $id)
1059 {
1060 $id = intval($id);
1061 $query = "select *,UNIX_TIMESTAMP(`revoked`) as `revoke` from `emailcerts`
1062 where `id`='$id' and `memid`='".intval($_SESSION['profile']['id'])."'";
1063 $res = mysql_query($query);
1064 if(mysql_num_rows($res) <= 0)
1065 {
1066 printf(_("Invalid ID '%s' presented, can't do anything with it.")."<br>\n", $id);
1067 continue;
1068 }
1069
1070 $row = mysql_fetch_assoc($res);
1071
1072 if (($weakKey = checkWeakKeyX509(file_get_contents(
1073 $row['crt_name']))) !== "")
1074 {
1075 echo $weakKey, "<br/>\n";
1076 continue;
1077 }
1078
1079 mysql_query("update `emailcerts` set `renewed`='1' where `id`='$id'");
1080 $query = "insert into emailcerts set
1081 `memid`='".intval($row['memid'])."',
1082 `CN`='".mysql_real_escape_string($row['CN'])."',
1083 `subject`='".mysql_real_escape_string($row['subject'])."',
1084 `keytype`='".mysql_real_escape_string($row['keytype'])."',
1085 `csr_name`='".mysql_real_escape_string($row['csr_name'])."',
1086 `created`='".mysql_real_escape_string($row['created'])."',
1087 `modified`=NOW(),
1088 `disablelogin`='".intval($row['disablelogin'])."',
1089 `codesign`='".intval($row['codesign'])."',
1090 `rootcert`='".intval($row['rootcert'])."',
1091 `description`='".mysql_real_escape_string($row['description'])."',
1092 `md`='".HashAlgorithms::clean($row['md'])."'";
1093 mysql_query($query);
1094 $newid = mysql_insert_id();
1095 $newfile=generatecertpath("csr","client",$newid);
1096 copy($row['csr_name'], $newfile);
1097 mysql_query("update `emailcerts` set `csr_name`='$newfile' where `id`='$newid'");
1098 $res = mysql_query("select * from `emaillink` where `emailcertsid`='".$row['id']."'");
1099 while($r2 = mysql_fetch_assoc($res))
1100 {
1101 mysql_query("insert into `emaillink` set `emailid`='".$r2['emailid']."',
1102 `emailcertsid`='$newid'");
1103 }
1104 waitForResult("emailcerts", $newid,$oldid,0);
1105 $query = "select * from `emailcerts` where `id`='$newid' and `crt_name` != ''";
1106 $res = mysql_query($query);
1107 if(mysql_num_rows($res) <= 0)
1108 {
1109 printf(_("Your certificate request has failed to be processed correctly, see %sthe WIKI page%s for reasons and solutions."), "<a href='http://wiki.cacert.org/wiki/FAQ/CertificateRenewal'>", "</a>");
1110 } else {
1111 printf(_("Certificate for '%s' has been renewed."), $row['CN']);
1112 echo "<br/>\n<a href='account.php?id=6&cert=$newid' target='_new'>".
1113 _("Click here")."</a> "._("to install your certificate.")."<br/><br/>\n";
1114 }
1115 }
1116 }
1117 else
1118 {
1119 echo _("You did not select any certificates for renewal.")."<br/>";
1120 }
1121
1122 showfooter();
1123 exit;
1124 }
1125
1126 if($oldid == 5 && array_key_exists('revoke',$_REQUEST) && $_REQUEST['revoke'] != "")
1127 {
1128 $id = 5;
1129 showheader(_("My CAcert.org Account!"));
1130 if(array_key_exists('revokeid',$_REQUEST) && is_array($_REQUEST['revokeid']))
1131 {
1132 echo _("Now revoking the following certificates:")."<br>\n";
1133 foreach($_REQUEST['revokeid'] as $id)
1134 {
1135 $id = intval($id);
1136 $query = "select *,UNIX_TIMESTAMP(`revoked`) as `revoke` from `emailcerts`
1137 where `id`='$id' and `memid`='".intval($_SESSION['profile']['id'])."'";
1138 $res = mysql_query($query);
1139 if(mysql_num_rows($res) <= 0)
1140 {
1141 printf(_("Invalid ID '%s' presented, can't do anything with it.")."<br>\n", $id);
1142 continue;
1143 }
1144 $row = mysql_fetch_assoc($res);
1145 if($row['revoke'] > 0)
1146 {
1147 printf(_("It would seem '%s' has already been revoked. I'll skip this for now.")."<br>\n", $row['CN']);
1148 continue;
1149 }
1150 mysql_query("update `emailcerts` set `revoked`='1970-01-01 10:00:01' where `id`='$id'");
1151 printf(_("Certificate for '%s' with the serial no '%s' has been revoked.").'<br/>', htmlspecialchars($row['CN']), htmlspecialchars($row['serial']));
1152 }
1153
1154 // TRANSLATORS: Please don't translate "Certificate Revocation List (CRL)", it's a technical term
1155 echo '<br/>'._('All listed certificates will be added to the Certificate Revocation List (CRL) soon.').'<br/>';
1156 }
1157 else
1158 {
1159 echo _("You did not select any certificates for revocation.");
1160 }
1161
1162 if(array_key_exists('delid',$_REQUEST) && is_array($_REQUEST['delid']))
1163 {
1164 echo _("Now deleting the following pending requests:")."<br>\n";
1165 foreach($_REQUEST['delid'] as $id)
1166 {
1167 $id = intval($id);
1168 $query = "select *,UNIX_TIMESTAMP(`expire`) as `expired` from `emailcerts`
1169 where `id`='$id' and `memid`='".intval($_SESSION['profile']['id'])."'";
1170 $res = mysql_query($query);
1171 if(mysql_num_rows($res) <= 0)
1172 {
1173 printf(_("Invalid ID '%s' presented, can't do anything with it.")."<br>\n", $id);
1174 continue;
1175 }
1176 $row = mysql_fetch_assoc($res);
1177 if($row['expired'] > 0)
1178 {
1179 printf(_("Couldn't remove the request for `%s`, request had already been processed.")."<br>\n", $row['CN']);
1180 continue;
1181 }
1182 mysql_query("delete from `emailcerts` where `id`='$id'");
1183 @unlink($row['csr_name']);
1184 @unlink($row['crt_name']);
1185 printf(_("Removed a pending request for '%s'")."<br>\n", $row['CN']);
1186 }
1187 }
1188 showfooter();
1189 exit;
1190 }
1191
1192 if($oldid == 5 && array_key_exists('change',$_REQUEST) && $_REQUEST['change'] != "")
1193 {
1194 showheader(_("My CAcert.org Account!"));
1195 foreach($_REQUEST as $id => $val)
1196 {
1197 if(substr($id,0,5)=="cert_")
1198 {
1199 $cid = intval(substr($id,5));
1200 $dis=(array_key_exists('disablelogin_'.$cid,$_REQUEST) && $_REQUEST['disablelogin_'.$cid]=="1")?"0":"1";
1201 mysql_query("update `emailcerts` set `disablelogin`='$dis' where `id`='$cid' and `memid`='".intval($_SESSION['profile']['id'])."'");
1202 }
1203 if(substr($id,0,14)=="check_comment_")
1204 {
1205 $cid = intval(substr($id,14));
1206 if(!empty($_REQUEST['check_comment_'.$cid])) {
1207 $comment=trim(mysql_real_escape_string(stripslashes($_REQUEST['comment_'.$cid])));
1208 mysql_query("update `emailcerts` set `description`='$comment' where `id`='$cid' and `memid`='".intval($_SESSION['profile']['id'])."'");
1209 }
1210 }
1211 }
1212 echo(_("Certificate settings have been changed.")."<br/>\n");
1213 showfooter();
1214 exit;
1215 }
1216
1217 if($oldid == 6 && $_REQUEST['certid'] != "")
1218 {
1219 if(trim($_REQUEST['description']) != ""){
1220 $description= trim(mysql_real_escape_string(stripslashes($_REQUEST['description'])));
1221 }else{
1222 $description= "";
1223 }
1224
1225 if(trim($_REQUEST['disablelogin']) == "1"){
1226 $disablelogin = 1;
1227 }else{
1228 $disablelogin = 0;
1229 }
1230
1231 mysql_query("update `emailcerts` set `disablelogin`='$disablelogin', `description`='$description' where `id`='".$_REQUEST['certid']."' and `memid`='".$_SESSION['profile']['id']."'");
1232 }
1233
1234 if($oldid == 13 && $process != "" && $showdetails!="")
1235 {
1236 csrf_check("perschange");
1237 $_SESSION['_config']['user'] = $_SESSION['profile'];
1238
1239 $_SESSION['_config']['user']['Q1'] = trim(mysql_real_escape_string(stripslashes(strip_tags($_REQUEST['Q1']))));
1240 $_SESSION['_config']['user']['Q2'] = trim(mysql_real_escape_string(stripslashes(strip_tags($_REQUEST['Q2']))));
1241 $_SESSION['_config']['user']['Q3'] = trim(mysql_real_escape_string(stripslashes(strip_tags($_REQUEST['Q3']))));
1242 $_SESSION['_config']['user']['Q4'] = trim(mysql_real_escape_string(stripslashes(strip_tags($_REQUEST['Q4']))));
1243 $_SESSION['_config']['user']['Q5'] = trim(mysql_real_escape_string(stripslashes(strip_tags($_REQUEST['Q5']))));
1244 $_SESSION['_config']['user']['A1'] = trim(mysql_real_escape_string(stripslashes(strip_tags($_REQUEST['A1']))));
1245 $_SESSION['_config']['user']['A2'] = trim(mysql_real_escape_string(stripslashes(strip_tags($_REQUEST['A2']))));
1246 $_SESSION['_config']['user']['A3'] = trim(mysql_real_escape_string(stripslashes(strip_tags($_REQUEST['A3']))));
1247 $_SESSION['_config']['user']['A4'] = trim(mysql_real_escape_string(stripslashes(strip_tags($_REQUEST['A4']))));
1248 $_SESSION['_config']['user']['A5'] = trim(mysql_real_escape_string(stripslashes(strip_tags($_REQUEST['A5']))));
1249
1250 if($_SESSION['_config']['user']['Q1'] == $_SESSION['_config']['user']['Q2'] ||
1251 $_SESSION['_config']['user']['Q1'] == $_SESSION['_config']['user']['Q3'] ||
1252 $_SESSION['_config']['user']['Q1'] == $_SESSION['_config']['user']['Q4'] ||
1253 $_SESSION['_config']['user']['Q1'] == $_SESSION['_config']['user']['Q5'] ||
1254 $_SESSION['_config']['user']['Q2'] == $_SESSION['_config']['user']['Q3'] ||
1255 $_SESSION['_config']['user']['Q2'] == $_SESSION['_config']['user']['Q4'] ||
1256 $_SESSION['_config']['user']['Q2'] == $_SESSION['_config']['user']['Q5'] ||
1257 $_SESSION['_config']['user']['Q3'] == $_SESSION['_config']['user']['Q4'] ||
1258 $_SESSION['_config']['user']['Q3'] == $_SESSION['_config']['user']['Q5'] ||
1259 $_SESSION['_config']['user']['Q4'] == $_SESSION['_config']['user']['Q5'] ||
1260 $_SESSION['_config']['user']['A1'] == $_SESSION['_config']['user']['Q1'] ||
1261 $_SESSION['_config']['user']['A1'] == $_SESSION['_config']['user']['Q2'] ||
1262 $_SESSION['_config']['user']['A1'] == $_SESSION['_config']['user']['Q3'] ||
1263 $_SESSION['_config']['user']['A1'] == $_SESSION['_config']['user']['Q4'] ||
1264 $_SESSION['_config']['user']['A1'] == $_SESSION['_config']['user']['Q5'] ||
1265 $_SESSION['_config']['user']['A2'] == $_SESSION['_config']['user']['Q3'] ||
1266 $_SESSION['_config']['user']['A2'] == $_SESSION['_config']['user']['Q4'] ||
1267 $_SESSION['_config']['user']['A2'] == $_SESSION['_config']['user']['Q5'] ||
1268 $_SESSION['_config']['user']['A3'] == $_SESSION['_config']['user']['Q4'] ||
1269 $_SESSION['_config']['user']['A3'] == $_SESSION['_config']['user']['Q5'] ||
1270 $_SESSION['_config']['user']['A4'] == $_SESSION['_config']['user']['Q5'] ||
1271 $_SESSION['_config']['user']['A1'] == $_SESSION['_config']['user']['A2'] ||
1272 $_SESSION['_config']['user']['A1'] == $_SESSION['_config']['user']['A3'] ||
1273 $_SESSION['_config']['user']['A1'] == $_SESSION['_config']['user']['A4'] ||
1274 $_SESSION['_config']['user']['A1'] == $_SESSION['_config']['user']['A5'] ||
1275 $_SESSION['_config']['user']['A2'] == $_SESSION['_config']['user']['A3'] ||
1276 $_SESSION['_config']['user']['A2'] == $_SESSION['_config']['user']['A4'] ||
1277 $_SESSION['_config']['user']['A2'] == $_SESSION['_config']['user']['A5'] ||
1278 $_SESSION['_config']['user']['A3'] == $_SESSION['_config']['user']['A4'] ||
1279 $_SESSION['_config']['user']['A3'] == $_SESSION['_config']['user']['A5'] ||
1280 $_SESSION['_config']['user']['A4'] == $_SESSION['_config']['user']['A5'])
1281 {
1282 $_SESSION['_config']['errmsg'] .= _("For your own security you must enter 5 different password questions and answers. You aren't allowed to duplicate questions, set questions as answers or use the question as the answer.")."<br>\n";
1283 $id = $oldid;
1284 $oldid=0;
1285 }
1286
1287 if($_SESSION['_config']['user']['Q1'] == "" || $_SESSION['_config']['user']['Q2'] == "" ||
1288 $_SESSION['_config']['user']['Q3'] == "" || $_SESSION['_config']['user']['Q4'] == "" ||
1289 $_SESSION['_config']['user']['Q5'] == "")
1290 {
1291 $_SESSION['_config']['errmsg'] .= _("For your own security you must enter 5 lost password questions and answers.")."<br>";
1292 $id = $oldid;
1293 $oldid=0;
1294 }
1295 }
1296
1297 if($oldid == 13 && $process != "")
1298 {
1299 update_points_in_profile();
1300
1301 if($_SESSION['profile']['points'] == 0)
1302 {
1303 $_SESSION['_config']['user']['fname'] = trim(mysql_real_escape_string(stripslashes(strip_tags($_REQUEST['fname']))));
1304 $_SESSION['_config']['user']['mname'] = trim(mysql_real_escape_string(stripslashes(strip_tags($_REQUEST['mname']))));
1305 $_SESSION['_config']['user']['lname'] = trim(mysql_real_escape_string(stripslashes(strip_tags($_REQUEST['lname']))));
1306 $_SESSION['_config']['user']['suffix'] = trim(mysql_real_escape_string(stripslashes(strip_tags($_REQUEST['suffix']))));
1307 $_SESSION['_config']['user']['day'] = intval($_REQUEST['day']);
1308 $_SESSION['_config']['user']['month'] = intval($_REQUEST['month']);
1309 $_SESSION['_config']['user']['year'] = intval($_REQUEST['year']);
1310
1311 if($_SESSION['_config']['user']['fname'] == "" || $_SESSION['_config']['user']['lname'] == "")
1312 {
1313 $_SESSION['_config']['errmsg'] .= _("First and Last name fields can not be blank.")."<br>";
1314 $id = $oldid;
1315 $oldid=0;
1316 }
1317 if($_SESSION['_config']['user']['year'] < 1900 || $_SESSION['_config']['user']['month'] < 1 || $_SESSION['_config']['user']['month'] > 12 ||
1318 $_SESSION['_config']['user']['day'] < 1 || $_SESSION['_config']['user']['day'] > 31)
1319 {
1320 $_SESSION['_config']['errmsg'] .= _("Invalid date of birth")."<br>\n";
1321 $id = $oldid;
1322 $oldid=0;
1323 }
1324 }
1325 }
1326
1327 if($oldid == 13 && $process != "")
1328 {
1329 if($_SESSION['profile']['points'] == 0)
1330 {
1331 $query = "update `users` set `fname`='".$_SESSION['_config']['user']['fname']."',
1332 `mname`='".$_SESSION['_config']['user']['mname']."',
1333 `lname`='".$_SESSION['_config']['user']['lname']."',
1334 `suffix`='".$_SESSION['_config']['user']['suffix']."',
1335 `dob`='".$_SESSION['_config']['user']['year']."-".$_SESSION['_config']['user']['month']."-".$_SESSION['_config']['user']['day']."'
1336 where `id`='".intval($_SESSION['profile']['id'])."'";
1337 mysql_query($query);
1338 }
1339 if ($showdetails!="") {
1340 $query = "update `users` set `Q1`='".$_SESSION['_config']['user']['Q1']."',
1341 `Q2`='".$_SESSION['_config']['user']['Q2']."',
1342 `Q3`='".$_SESSION['_config']['user']['Q3']."',
1343 `Q4`='".$_SESSION['_config']['user']['Q4']."',
1344 `Q5`='".$_SESSION['_config']['user']['Q5']."',
1345 `A1`='".$_SESSION['_config']['user']['A1']."',
1346 `A2`='".$_SESSION['_config']['user']['A2']."',
1347 `A3`='".$_SESSION['_config']['user']['A3']."',
1348 `A4`='".$_SESSION['_config']['user']['A4']."',
1349 `A5`='".$_SESSION['_config']['user']['A5']."'
1350 where `id`='".intval($_SESSION['profile']['id'])."'";
1351 mysql_query($query);
1352 }
1353
1354 $_SESSION['_config']['user']['set'] = 0;
1355 $_SESSION['profile'] = mysql_fetch_assoc(mysql_query("select * from `users` where `id`='".intval($_SESSION['profile']['id'])."'"));
1356 $_SESSION['profile']['loggedin'] = 1;
1357
1358 update_points_in_profile();
1359
1360
1361 $id = 13;
1362 showheader(_("My CAcert.org Account!"));
1363 echo _("Your details have been updated with the database.");
1364 showfooter();
1365 exit;
1366 }
1367
1368 if($oldid == 14 && $process != "")
1369 {
1370 $_SESSION['_config']['user']['oldpass'] = trim(mysql_real_escape_string(stripslashes($_REQUEST['oldpassword'])));
1371 $_SESSION['_config']['user']['pword1'] = trim(mysql_real_escape_string(stripslashes($_REQUEST['pword1'])));
1372 $_SESSION['_config']['user']['pword2'] = trim(mysql_real_escape_string(stripslashes($_REQUEST['pword2'])));
1373
1374 $id = 14;
1375 csrf_check("pwchange");
1376
1377 showheader(_("My CAcert.org Account!"));
1378 if($_SESSION['_config']['user']['pword1'] == "" || $_SESSION['_config']['user']['pword1'] != $_SESSION['_config']['user']['pword2'])
1379 {
1380 echo '<h3 class="error_fatal">', _("Failure: Pass Phrase not Changed"),
1381 '</h3>', "\n";
1382 echo _("New Pass Phrases specified don't match or were blank.");
1383 } else {
1384 $score = checkpw($_SESSION['_config']['user']['pword1'], $_SESSION['profile']['email'], $_SESSION['profile']['fname'],
1385 $_SESSION['profile']['mname'], $_SESSION['profile']['lname'], $_SESSION['profile']['suffix']);
1386
1387 if($_SESSION['_config']['hostname'] != $_SESSION['_config']['securehostname'])
1388 {
1389 $match = mysql_query("select * from `users` where `id`='".intval($_SESSION['profile']['id'])."' and
1390 (`password`=old_password('".$_SESSION['_config']['user']['oldpass']."') or
1391 `password`=sha1('".$_SESSION['_config']['user']['oldpass']."'))");
1392 $rc = mysql_num_rows($match);
1393 } else {
1394 $rc = 1;
1395 }
1396
1397 if(strlen($_SESSION['_config']['user']['pword1']) < 6) {
1398 echo '<h3 class="error_fatal">',
1399 _("Failure: Pass Phrase not Changed"), '</h3>', "\n";
1400 echo _("The Pass Phrase you submitted was too short.");
1401 } else if($score < 3) {
1402 echo '<h3 class="error_fatal">',
1403 _("Failure: Pass Phrase not Changed"), '</h3>', "\n";
1404 printf(_("The Pass Phrase you submitted failed to contain enough differing characters and/or contained words from your name and/or email address. Only scored %s points out of 6."), $score);
1405 } else if($rc <= 0) {
1406 echo '<h3 class="error_fatal">',
1407 _("Failure: Pass Phrase not Changed"), '</h3>', "\n";
1408 echo _("You failed to correctly enter your current Pass Phrase.");
1409 } else {
1410 mysql_query("update `users` set `password`=sha1('".$_SESSION['_config']['user']['pword1']."')
1411 where `id`='".intval($_SESSION['profile']['id'])."'");
1412 echo '<h3>', _("Pass Phrase Changed Successfully"), '</h3>', "\n";
1413 echo _("Your Pass Phrase has been updated and your primary email account has been notified of the change.");
1414 $body = sprintf(_("Hi %s,"),$_SESSION['profile']['fname'])."\n\n";
1415 $body .= _("You are receiving this email because you or someone else ".
1416 "has changed the password on your account.")."\n\n";
1417
1418 $body .= _("Best regards")."\n"._("CAcert.org Support!");
1419
1420 sendmail($_SESSION['profile']['email'], "[CAcert.org] "._("Password Update Notification"), $body,
1421 "support@cacert.org", "", "", "CAcert Support");
1422 }
1423 }
1424 showfooter();
1425 exit;
1426 }
1427
1428 if($oldid == 16)
1429 {
1430 $id = 16;
1431 $_SESSION['_config']['emails'] = array();
1432
1433 foreach($_REQUEST['emails'] as $val)
1434 {
1435 $val = mysql_real_escape_string(stripslashes(trim($val)));
1436 $bits = explode("@", $val);
1437 $count = count($bits);
1438 if($count != 2)
1439 continue;
1440
1441 if(checkownership($bits[1]) == false)
1442 continue;
1443
1444 if(!is_array($_SESSION['_config']['row']))
1445 continue;
1446 else if($_SESSION['_config']['row']['id'] > 0)
1447 $_SESSION['_config']['domids'][] = $_SESSION['_config']['row']['id'];
1448
1449 if($val != "")
1450 $_SESSION['_config']['emails'][] = $val;
1451 }
1452 $_SESSION['_config']['name'] = mysql_real_escape_string(stripslashes(trim($_REQUEST['name'])));
1453 $_SESSION['_config']['OU'] = stripslashes(trim($_REQUEST['OU']));
1454
1455 $_SESSION['_config']['description']= trim(stripslashes($_REQUEST['description']));
1456 }
1457
1458 if($oldid == 16 && (intval(count($_SESSION['_config']['emails'])) + 0) <= 0)
1459 {
1460 $id = 16;
1461 showheader(_("My CAcert.org Account!"));
1462 echo _("I couldn't match any emails against your organisational account.");
1463 showfooter();
1464 exit;
1465 }
1466
1467 if($oldid == 16 && $process != "")
1468 {
1469 if(array_key_exists('codesign',$_REQUEST) && $_REQUEST['codesign'] && $_SESSION['profile']['codesign'] && ($_SESSION['profile']['points'] >= 100))
1470 {
1471 $_REQUEST['codesign'] = 1;
1472 $_SESSION['_config']['codesign'] = 1;
1473 }
1474 else
1475 {
1476 $_REQUEST['codesign'] = 0;
1477 $_SESSION['_config']['codesign'] = 0;
1478 }
1479
1480 $_SESSION['_config']['rootcert'] = intval($_REQUEST['rootcert']);
1481 if($_SESSION['_config']['rootcert'] < 1 || $_SESSION['_config']['rootcert'] > 2)
1482 $_SESSION['_config']['rootcert'] = 1;
1483
1484 $_SESSION['_config']['hash_alg'] = HashAlgorithms::clean($_REQUEST['hash_alg']);
1485
1486 $_SESSION['_config']['description']= trim(stripslashes($_REQUEST['description']));
1487
1488 if(@count($_SESSION['_config']['emails']) > 0)
1489 $id = 17;
1490 }
1491
1492 if($oldid == 17)
1493 {
1494 $org = $_SESSION['_config']['row'];
1495 if($_REQUEST['keytype'] == "NS")
1496 {
1497 $spkac=""; if(preg_match("/^[a-zA-Z0-9+=\/]+$/", trim(str_replace("\n", "", str_replace("\r", "",$_REQUEST['SPKAC']))))) $spkac=trim(str_replace("\n", "", str_replace("\r", "",$_REQUEST['SPKAC'])));
1498
1499 if($spkac == "" || strlen($spkac) < 128)
1500 {
1501 $id = 17;
1502 showheader(_("My CAcert.org Account!"));
1503 echo _("I didn't receive a valid Certificate Request, hit the back button and try again.");
1504 showfooter();
1505 exit;
1506 }
1507
1508 $count = 0;
1509 $emails = "";
1510 $addys = array();
1511 if(is_array($_SESSION['_config']['emails']))
1512 foreach($_SESSION['_config']['emails'] as $_REQUEST['email'])
1513 {
1514 if(!$emails)
1515 $defaultemail = $_REQUEST['email'];
1516 $emails .= "$count.emailAddress = $_REQUEST[email]\n";
1517 $count++;
1518 }
1519 if($_SESSION['_config']['name'] != "")
1520 $emails .= "commonName = ".$_SESSION['_config']['name']."\n";
1521 if($_SESSION['_config']['OU'])
1522 $emails .= "organizationalUnitName = ".mysql_real_escape_string($_SESSION['_config']['OU'])."\n";
1523 if($org['O'])
1524 $emails .= "organizationName = ".$org['O']."\n";
1525 if($org['L'])
1526 $emails .= "localityName = ".$org['L']."\n";
1527 if($org['ST'])
1528 $emails .= "stateOrProvinceName = ".$org['ST']."\n";
1529 if($org['C'])
1530 $emails .= "countryName = ".$org['C']."\n";
1531 if($_SESSION['_config']['rootcert'] < 1 || $_SESSION['_config']['rootcert'] > 2)
1532 $_SESSION['_config']['rootcert'] = 1;
1533
1534
1535 $emails .= "SPKAC = $spkac";
1536 if (($weakKey = checkWeakKeySPKAC($emails)) !== "")
1537 {
1538 $id = 17;
1539 showheader(_("My CAcert.org Account!"));
1540 echo $weakKey;
1541 showfooter();
1542 exit;
1543 }
1544
1545 $query = "insert into `orgemailcerts` set
1546 `CN`='$defaultemail',
1547 `ou`='".mysql_real_escape_string($_SESSION['_config']['OU'])."',
1548 `keytype`='NS',
1549 `orgid`='".intval($org['orgid'])."',
1550 `created`=FROM_UNIXTIME(UNIX_TIMESTAMP()),
1551 `codesign`='".intval($_SESSION['_config']['codesign'])."',
1552 `rootcert`='".intval($_SESSION['_config']['rootcert'])."',
1553 `md`='".mysql_real_escape_string($_SESSION['_config']['hash_alg'])."',
1554 `description`='".mysql_real_escape_string($_SESSION['_config']['description'])."'";
1555 mysql_query($query);
1556 $emailid = mysql_insert_id();
1557
1558 foreach($_SESSION['_config']['domids'] as $addy)
1559 mysql_query("insert into `domemaillink` set `emailcertsid`='$emailid', `emailid`='$addy'");
1560
1561 $CSRname=generatecertpath("csr","orgclient",$emailid);
1562 $fp = fopen($CSRname, "w");
1563 fputs($fp, $emails);
1564 fclose($fp);
1565 $challenge=$_SESSION['spkac_hash'];
1566 $CSRname_esc = escapeshellarg($CSRname);
1567 $res=shell_exec("openssl spkac -verify -in $CSRname_esc");
1568 if(!strstr($res,"Challenge String: ".$challenge))
1569 {
1570 $id = $oldid;
1571 showheader(_("My CAcert.org Account!"));
1572 echo _("The challenge-response code of your certificate request did not match. Can't continue with certificaterequest.");
1573 showfooter();
1574 exit;
1575 }
1576 mysql_query("update `orgemailcerts` set `csr_name`='$CSRname' where `id`='$emailid'");
1577 } else if($_REQUEST['keytype'] == "MS" || $_REQUEST['keytype']=="VI") {
1578 $csr = clean_csr($_REQUEST['CSR']);
1579 if(strpos($csr,"---BEGIN") === FALSE)
1580 {
1581 // In case the CSR is missing the ---BEGIN lines, add them automatically:
1582 $csr = "-----BEGIN CERTIFICATE REQUEST-----\n".$csr."\n-----END CERTIFICATE REQUEST-----\n";
1583 }
1584
1585 if (($weakKey = checkWeakKeyCSR($csr)) !== "")
1586 {
1587 $id = 17;
1588 showheader(_("My CAcert.org Account!"));
1589 echo $weakKey;
1590 showfooter();
1591 exit;
1592 }
1593
1594 $tmpfname = tempnam("/tmp", "id17CSR");
1595 $fp = fopen($tmpfname, "w");
1596 fputs($fp, $csr);
1597 fclose($fp);
1598
1599 $addys = array();
1600 $defaultemail = "";
1601 $csrsubject="";
1602
1603 if($_SESSION['_config']['name'] != "")
1604 $csrsubject = "/CN=".$_SESSION['_config']['name'];
1605 if(is_array($_SESSION['_config']['emails']))
1606 foreach($_SESSION['_config']['emails'] as $_REQUEST['email'])
1607 {
1608 if($defaultemail == "")
1609 $defaultemail = $_REQUEST['email'];
1610 $csrsubject .= "/emailAddress=$_REQUEST[email]";
1611 }
1612 if($_SESSION['_config']['OU'])
1613 $csrsubject .= "/organizationalUnitName=".$_SESSION['_config']['OU'];
1614 if($org['O'])
1615 $csrsubject .= "/organizationName=".$org['O'];
1616 if($org['L'])
1617 $csrsubject .= "/localityName=".$org['L'];
1618 if($org['ST'])
1619 $csrsubject .= "/stateOrProvinceName=".$org['ST'];
1620 if($org['C'])
1621 $csrsubject .= "/countryName=".$org['C'];
1622
1623 $tmpname = tempnam("/tmp", "id17csr");
1624 $tmpfname_esc = escapeshellarg($tmpfname);
1625 $tmpname_esc = escapeshellarg($tmpname);
1626 $do = shell_exec("/usr/bin/openssl req -in $tmpfname_esc -out $tmpname_esc");
1627 @unlink($tmpfname);
1628 $csr = "";
1629 $fp = fopen($tmpname, "r");
1630 while($data = fgets($fp, 4096))
1631 $csr .= $data;
1632 fclose($fp);
1633 @unlink($tmpname);
1634
1635 if($csr == "")
1636 {
1637 showheader(_("My CAcert.org Account!"));
1638 echo _("I didn't receive a valid Certificate Request, hit the back button and try again.");
1639 showfooter();
1640 exit;
1641 }
1642 if($_SESSION['_config']['rootcert'] < 1 || $_SESSION['_config']['rootcert'] > 2)
1643 $_SESSION['_config']['rootcert'] = 1;
1644
1645 $query = "insert into `orgemailcerts` set
1646 `CN`='$defaultemail',
1647 `ou`='".mysql_real_escape_string($_SESSION['_config']['OU'])."',
1648 `keytype`='" . sanitizeHTML($_REQUEST['keytype']) . "',
1649 `orgid`='".intval($org['orgid'])."',
1650 `created`=FROM_UNIXTIME(UNIX_TIMESTAMP()),
1651 `subject`='".mysql_real_escape_string($csrsubject)."',
1652 `codesign`='".intval($_SESSION['_config']['codesign'])."',
1653 `rootcert`='".intval($_SESSION['_config']['rootcert'])."',
1654 `md`='".mysql_real_escape_string($_SESSION['_config']['hash_alg'])."',
1655 `description`='".mysql_real_escape_string($_SESSION['_config']['description'])."'";
1656 mysql_query($query);
1657 $emailid = mysql_insert_id();
1658
1659 foreach($_SESSION['_config']['domids'] as $addy)
1660 mysql_query("insert into `domemaillink` set `emailcertsid`='$emailid', `emailid`='$addy'");
1661
1662 $CSRname=generatecertpath("csr","orgclient",$emailid);
1663 $fp = fopen($CSRname, "w");
1664 fputs($fp, $csr);
1665 fclose($fp);
1666 mysql_query("update `orgemailcerts` set `csr_name`='$CSRname' where `id`='$emailid'");
1667 }
1668 waitForResult("orgemailcerts", $emailid,$oldid);
1669 $query = "select * from `orgemailcerts` where `id`='$emailid' and `crt_name` != ''";
1670 $res = mysql_query($query);
1671 if(mysql_num_rows($res) <= 0)
1672 {
1673 showheader(_("My CAcert.org Account!"));
1674 printf(_("Your certificate request has failed to be processed correctly, see %sthe WIKI page%s for reasons and solutions."), "<a href='http://wiki.cacert.org/wiki/FAQ/CertificateRenewal'>", "</a>");
1675 showfooter();
1676 exit;
1677 } else {
1678 $id = 19;
1679 $cert = $emailid;
1680 $_REQUEST['cert']=$emailid;
1681 }
1682 }
1683
1684 if($oldid == 18 && array_key_exists('renew',$_REQUEST) && $_REQUEST['renew'] != "")
1685 {
1686 csrf_check('clicerchange');
1687 showheader(_("My CAcert.org Account!"));
1688 if(is_array($_REQUEST['revokeid']))
1689 {
1690 $id = 18;
1691 echo _("Now renewing the following certificates:")."<br>\n";
1692 foreach($_REQUEST['revokeid'] as $id)
1693 {
1694 echo "Renewing certificate #$id ...\n<br/>";
1695 $id = intval($id);
1696 $query = "select *,UNIX_TIMESTAMP(`revoked`) as `revoke` from `orgemailcerts`, `org`
1697 where `orgemailcerts`.`id`='$id' and `org`.`memid`='".intval($_SESSION['profile']['id'])."' and
1698 `org`.`orgid`=`orgemailcerts`.`orgid`";
1699 $res = mysql_query($query);
1700 if(mysql_num_rows($res) <= 0)
1701 {
1702 printf(_("Invalid ID '%s' presented, can't do anything with it.")."<br>\n", $id);
1703 continue;
1704 }
1705
1706 $row = mysql_fetch_assoc($res);
1707
1708 if (($weakKey = checkWeakKeyX509(file_get_contents(
1709 $row['crt_name']))) !== "")
1710 {
1711 echo $weakKey, "<br/>\n";
1712 continue;
1713 }
1714
1715 mysql_query("update `orgemailcerts` set `renewed`='1' where `id`='$id'");
1716 if($row['revoke'] > 0)
1717 {
1718 printf(_("It would seem '%s' has already been revoked. I'll skip this for now.")."<br>\n", $row['CN']);
1719 continue;
1720 }
1721 $query = "insert into `orgemailcerts` set
1722 `orgid`='".intval($row['orgid'])."',
1723 `CN`='".mysql_real_escape_string($row['CN'])."',
1724 `ou`='".mysql_real_escape_string($row['ou'])."',
1725 `subject`='".mysql_real_escape_string($row['subject'])."',
1726 `keytype`='".mysql_real_escape_string($row['keytype'])."',
1727 `csr_name`='".mysql_real_escape_string($row['csr_name'])."',
1728 `created`='".mysql_real_escape_string($row['created'])."',
1729 `modified`=NOW(),
1730 `codesign`='".intval($row['codesign'])."',
1731 `rootcert`='".intval($row['rootcert'])."',
1732 `description`='".mysql_real_escape_string($row['description'])."',
1733 `md`='".HashAlgorithms::clean($row['md'])."'";
1734 mysql_query($query);
1735 $newid = mysql_insert_id();
1736 $newfile=generatecertpath("csr","orgclient",$newid);
1737 copy($row['csr_name'], $newfile);
1738 mysql_query("update `orgemailcerts` set `csr_name`='$newfile' where `id`='$newid'");
1739 waitForResult("orgemailcerts", $newid,$oldid,0);
1740 $query = "select * from `orgemailcerts` where `id`='$newid' and `crt_name` != ''";
1741 $res = mysql_query($query);
1742 if(mysql_num_rows($res) > 0)
1743 {
1744 printf(_("Certificate for '%s' has been renewed."), $row['CN']);
1745 echo "<a href='account.php?id=19&cert=$newid' target='_new'>".
1746 _("Click here")."</a> "._("to install your certificate.");
1747 }
1748 echo("<br/>");
1749 }
1750 }
1751 else
1752 {
1753 echo _("You did not select any certificates for renewal.");
1754 }
1755 showfooter();
1756 exit;
1757 }
1758
1759 if($oldid == 18 && array_key_exists('revoke',$_REQUEST) && $_REQUEST['revoke'] != "")
1760 {
1761 csrf_check('clicerchange');
1762 $id = 18;
1763 showheader(_("My CAcert.org Account!"));
1764 if(is_array($_REQUEST['revokeid']))
1765 {
1766 echo _("Now revoking the following certificates:")."<br>\n";
1767 foreach($_REQUEST['revokeid'] as $id)
1768 {
1769 $id = intval($id);
1770 $query = "select *,UNIX_TIMESTAMP(`revoked`) as `revoke` from `orgemailcerts`, `org`
1771 where `orgemailcerts`.`id`='".intval($id)."' and `org`.`memid`='".intval($_SESSION['profile']['id'])."' and
1772 `org`.`orgid`=`orgemailcerts`.`orgid`";
1773 $res = mysql_query($query);
1774 if(mysql_num_rows($res) <= 0)
1775 {
1776 printf(_("Invalid ID '%s' presented, can't do anything with it.")."<br>\n", $id);
1777 continue;
1778 }
1779 $row = mysql_fetch_assoc($res);
1780 if($row['revoke'] > 0)
1781 {
1782 printf(_("It would seem '%s' has already been revoked. I'll skip this for now.")."<br>\n", $row['CN']);
1783 continue;
1784 }
1785 mysql_query("update `orgemailcerts` set `revoked`='1970-01-01 10:00:01' where `id`='$id'");
1786 printf(_("Certificate for '%s' with the serial no '%s' has been revoked.").'<br/>', htmlspecialchars($row['CN']), htmlspecialchars($row['serial']));
1787 }
1788
1789 // TRANSLATORS: Please don't translate "Certificate Revocation List (CRL)", it's a technical term
1790 echo '<br/>'._('All listed certificates will be added to the Certificate Revocation List (CRL) soon.').'<br/>';
1791 }
1792 else
1793 {
1794 echo _("You did not select any certificates for revocation.");
1795 }
1796
1797 if(array_key_exists('delid',$_REQUEST) && is_array($_REQUEST['delid']))
1798 {
1799 echo _("Now deleting the following pending requests:")."<br>\n";
1800 foreach($_REQUEST['delid'] as $id)
1801 {
1802 $id = intval($id);
1803 $query = "select *,UNIX_TIMESTAMP(`expire`) as `expired` from `orgemailcerts`, `org`
1804 where `orgemailcerts`.`id`='".intval($id)."' and `org`.`memid`='".intval($_SESSION['profile']['id'])."' and
1805 `org`.`orgid`=`orgemailcerts`.`orgid`";
1806 $res = mysql_query($query);
1807 if(mysql_num_rows($res) <= 0)
1808 {
1809 printf(_("Invalid ID '%s' presented, can't do anything with it.")."<br>\n", $id);
1810 continue;
1811 }
1812 $row = mysql_fetch_assoc($res);
1813 if($row['expired'] > 0)
1814 {
1815 printf(_("Couldn't remove the request for `%s`, request had already been processed.")."<br>\n", $row['CN']);
1816 continue;
1817 }
1818 mysql_query("delete from `orgemailcerts` where `id`='$id'");
1819 @unlink($row['csr_name']);
1820 @unlink($row['crt_name']);
1821 printf(_("Removed a pending request for '%s'")."<br>\n", $row['CN']);
1822 }
1823 }
1824 showfooter();
1825 exit;
1826 }
1827
1828 if($oldid == 18 && array_key_exists('change',$_REQUEST) && $_REQUEST['change'] != "")
1829 {
1830 showheader(_("My CAcert.org Account!"));
1831 foreach($_REQUEST as $id => $val)
1832 {
1833 if(substr($id,0,14)=="check_comment_")
1834 {
1835 $cid = intval(substr($id,14));
1836 $comment=trim(mysql_real_escape_string(stripslashes($_REQUEST['comment_'.$cid])));
1837 mysql_query("update `orgemailcerts` set `description`='$comment' where `id`='$cid'");
1838 }
1839 }
1840 echo(_("Certificate settings have been changed.")."<br/>\n");
1841 showfooter();
1842 exit;
1843 }
1844
1845 if($oldid == 18 && array_key_exists('filter',$_REQUEST) && $_REQUEST['filter']!= "")
1846 {
1847 $id=18;
1848 $_SESSION['_config']['orgfilterid']=$_REQUEST['orgfilterid'];
1849 $_SESSION['_config']['sorting']=$_REQUEST['sorting'];
1850 $_SESSION['_config']['status']=$_REQUEST['status'];
1851 }
1852
1853 if($oldid == 18 && array_key_exists('reset',$_REQUEST) && $_REQUEST['reset']!= "")
1854 {
1855 $id=18;
1856 $_SESSION['_config']['orgfilterid']=0;
1857 $_SESSION['_config']['sorting']=0;
1858 $_SESSION['_config']['status']=0;
1859 }
1860
1861 if($process != "" && $oldid == 20)
1862 {
1863 $CSR = clean_csr($_REQUEST['CSR']);
1864
1865 if (($weakKey = checkWeakKeyCSR($CSR)) !== "")
1866 {
1867 $id = 20;
1868 showheader(_("My CAcert.org Account!"));
1869 echo $weakKey;
1870 showfooter();
1871 exit;
1872 }
1873
1874 $_SESSION['_config']['description']= trim(stripslashes($_REQUEST['description']));
1875
1876 $_SESSION['_config']['tmpfname'] = tempnam("/tmp", "id20CSR");
1877 $fp = fopen($_SESSION['_config']['tmpfname'], "w");
1878 fputs($fp, $CSR);
1879 fclose($fp);
1880 $CSR = escapeshellarg($_SESSION['_config']['tmpfname']);
1881 $_SESSION['_config']['subject'] = trim(shell_exec("/usr/bin/openssl req -text -noout -in $CSR |tr -d \"\\0\"|grep \"Subject:\""));
1882 $bits = explode(",", trim(shell_exec("/usr/bin/openssl req -text -noout -in $CSR |tr -d \"\\0\"|grep -A1 'X509v3 Subject Alternative Name:'|grep DNS:")));
1883 foreach($bits as $val)
1884 {
1885 $_SESSION['_config']['subject'] .= "/subjectAltName=".trim($val);
1886 }
1887 $id = 21;
1888
1889 $_SESSION['_config']['0.CN'] = $_SESSION['_config']['0.subjectAltName'] = "";
1890 extractit();
1891 getcn2();
1892 getalt2();
1893
1894 $query = "select * from `orginfo`,`org`,`orgdomains` where
1895 `org`.`memid`='".intval($_SESSION['profile']['id'])."' and
1896 `org`.`orgid`=`orginfo`.`id` and
1897 `org`.`orgid`=`orgdomains`.`orgid` and
1898 `orgdomains`.`domain`='".mysql_real_escape_string($_SESSION['_config']['0.CN'])."'";
1899 $_SESSION['_config']['CNorg'] = mysql_fetch_assoc(mysql_query($query));
1900 $query = "select * from `orginfo`,`org`,`orgdomains` where
1901 `org`.`memid`='".intval($_SESSION['profile']['id'])."' and
1902 `org`.`orgid`=`orginfo`.`id` and
1903 `org`.`orgid`=`orgdomains`.`orgid` and
1904 `orgdomains`.`domain`='".mysql_real_escape_string($_SESSION['_config']['0.subjectAltName'])."'";
1905 $_SESSION['_config']['SANorg'] = mysql_fetch_assoc(mysql_query($query));
1906 //echo "<pre>"; print_r($_SESSION['_config']); die;
1907
1908 if($_SESSION['_config']['0.CN'] == "" && $_SESSION['_config']['0.subjectAltName'] == "")
1909 {
1910 $id = 20;
1911 showheader(_("My CAcert.org Account!"));
1912 echo _("CommonName field was blank. This is usually caused by entering your own name when openssl prompt's you for 'YOUR NAME', or if you try to issue certificates for domains you haven't already verified, as such this process can't continue.");
1913 showfooter();
1914 exit;
1915 }
1916
1917 $_SESSION['_config']['rootcert'] = intval($_REQUEST['rootcert']);
1918 if($_SESSION['_config']['rootcert'] < 1 || $_SESSION['_config']['rootcert'] > 2)
1919 $_SESSION['_config']['rootcert'] = 1;
1920
1921 $_SESSION['_config']['hash_alg'] = HashAlgorithms::clean($_REQUEST['hash_alg']);
1922 }
1923
1924 if($process != "" && $oldid == 21)
1925 {
1926 $id = 21;
1927
1928 if(!file_exists($_SESSION['_config']['tmpfname']))
1929 {
1930 showheader(_("My CAcert.org Account!"));
1931 printf(_("Your certificate request has failed to be processed correctly, see %sthe WIKI page%s for reasons and solutions."), "<a href='http://wiki.cacert.org/wiki/FAQ/CertificateRenewal'>", "</a>");
1932 showfooter();
1933 exit;
1934 }
1935
1936 if (($weakKey = checkWeakKeyCSR(file_get_contents(
1937 $_SESSION['_config']['tmpfname']))) !== "")
1938 {
1939 showheader(_("My CAcert.org Account!"));
1940 echo $weakKey;
1941 showfooter();
1942 exit;
1943 }
1944
1945 if($_SESSION['_config']['0.CN'] == "" && $_SESSION['_config']['0.subjectAltName'] == "")
1946 {
1947 showheader(_("My CAcert.org Account!"));
1948 echo _("CommonName field was blank. This is usually caused by entering your own name when openssl prompt's you for 'YOUR NAME', or if you try to issue certificates for domains you haven't already verified, as such this process can't continue.");
1949 showfooter();
1950 exit;
1951 }
1952
1953 if($_SESSION['_config']['rowid']['0'] > 0)
1954 {
1955 $query = "select * from `org`,`orginfo` where
1956 `orginfo`.`id`='".intval($_SESSION['_config']['rowid']['0'])."' and
1957 `orginfo`.`id`=`org`.`orgid` and
1958 `org`.`memid`='".intval($_SESSION['profile']['id'])."'";
1959 } else {
1960 $query = "select * from `org`,`orginfo` where
1961 `orginfo`.`id`='".intval($_SESSION['_config']['altid']['0'])."' and
1962 `orginfo`.`id`=`org`.`orgid` and
1963 `org`.`memid`='".intval($_SESSION['profile']['id'])."'";
1964 }
1965 $org = mysql_fetch_assoc(mysql_query($query));
1966 $csrsubject = "";
1967
1968 if($_SESSION['_config']['OU'])
1969 $csrsubject .= "/organizationalUnitName=".$_SESSION['_config']['OU'];
1970 if($org['O'])
1971 $csrsubject .= "/organizationName=".$org['O'];
1972 if($org['L'])
1973 $csrsubject .= "/localityName=".$org['L'];
1974 if($org['ST'])
1975 $csrsubject .= "/stateOrProvinceName=".$org['ST'];
1976 if($org['C'])
1977 $csrsubject .= "/countryName=".$org['C'];
1978 //if($org['contact'])
1979 // $csrsubject .= "/emailAddress=".trim($org['contact']);
1980
1981 $csrsubject .= buildSubjectFromSession();
1982
1983 $type="";
1984 if($_REQUEST["ocspcert"]!="" && $_SESSION['profile']['admin'] == 1) $type="8";
1985 if($_SESSION['_config']['rootcert'] < 1 || $_SESSION['_config']['rootcert'] > 2)
1986 $_SESSION['_config']['rootcert'] = 1;
1987
1988 if($_SESSION['_config']['rowid']['0'] > 0)
1989 {
1990 $query = "insert into `orgdomaincerts` set
1991 `CN`='".mysql_real_escape_string($_SESSION['_config']['rows']['0'])."',
1992 `orgid`='".intval($org['id'])."',
1993 `created`=NOW(),
1994 `subject`='".mysql_real_escape_string($csrsubject)."',
1995 `rootcert`='".intval($_SESSION['_config']['rootcert'])."',
1996 `md`='".mysql_real_escape_string($_SESSION['_config']['hash_alg'])."',
1997 `type`='".$type."',
1998 `description`='".mysql_real_escape_string($_SESSION['_config']['description'])."'";
1999 } else {
2000 $query = "insert into `orgdomaincerts` set
2001 `CN`='".mysql_real_escape_string($_SESSION['_config']['altrows']['0'])."',
2002 `orgid`='".intval($org['id'])."',
2003 `created`=NOW(),
2004 `subject`='".mysql_real_escape_string($csrsubject)."',
2005 `rootcert`='".intval($_SESSION['_config']['rootcert'])."',
2006 `md`='".mysql_real_escape_string($_SESSION['_config']['hash_alg'])."',
2007 `type`='".$type."',
2008 `description`='".mysql_real_escape_string($_SESSION['_config']['description'])."'";
2009 }
2010 mysql_query($query);
2011 $CSRid = mysql_insert_id();
2012
2013 $CSRname=generatecertpath("csr","orgserver",$CSRid);
2014 rename($_SESSION['_config']['tmpfname'], $CSRname);
2015 chmod($CSRname,0644);
2016 mysql_query("update `orgdomaincerts` set `CSR_name`='$CSRname' where `id`='$CSRid'");
2017 if(is_array($_SESSION['_config']['rowid']))
2018 foreach($_SESSION['_config']['rowid'] as $id)
2019 mysql_query("insert into `orgdomlink` set `orgdomid`='".intval($id)."', `orgcertid`='$CSRid'");
2020 if(is_array($_SESSION['_config']['altid']))
2021 foreach($_SESSION['_config']['altid'] as $id)
2022 mysql_query("insert into `orgdomlink` set `orgdomid`='".intval($id)."', `orgcertid`='$CSRid'");
2023 waitForResult("orgdomaincerts", $CSRid,$oldid);
2024 $query = "select * from `orgdomaincerts` where `id`='$CSRid' and `crt_name` != ''";
2025 $res = mysql_query($query);
2026 if(mysql_num_rows($res) <= 0)
2027 {
2028 showheader(_("My CAcert.org Account!"));
2029 printf(_("Your certificate request has failed to be processed correctly, see %sthe WIKI page%s for reasons and solutions.")." CSRid: $CSRid", "<a href='http://wiki.cacert.org/wiki/FAQ/CertificateRenewal'>", "</a>");
2030 showfooter();
2031 exit;
2032 } else {
2033 $id = 23;
2034 $cert = $CSRid;
2035 $_REQUEST['cert']=$CSRid;
2036 }
2037 }
2038
2039 if($oldid == 22 && array_key_exists('renew',$_REQUEST) && $_REQUEST['renew'] != "")
2040 {
2041 csrf_check('orgsrvcerchange');
2042 showheader(_("My CAcert.org Account!"));
2043 if(is_array($_REQUEST['revokeid']))
2044 {
2045 echo _("Now renewing the following certificates:")."<br>\n";
2046 foreach($_REQUEST['revokeid'] as $id)
2047 {
2048 $id = intval($id);
2049 $query = "select *,UNIX_TIMESTAMP(`orgdomaincerts`.`revoked`) as `revoke` from
2050 `orgdomaincerts`,`org`
2051 where `orgdomaincerts`.`id`='$id' and
2052 `orgdomaincerts`.`orgid`=`org`.`orgid` and
2053 `org`.`memid`='".intval($_SESSION['profile']['id'])."'";
2054 $res = mysql_query($query);
2055 if(mysql_num_rows($res) <= 0)
2056 {
2057 printf(_("Invalid ID '%s' presented, can't do anything with it.")."<br>\n", $id);
2058 continue;
2059 }
2060
2061 $row = mysql_fetch_assoc($res);
2062
2063 if (($weakKey = checkWeakKeyX509(file_get_contents(
2064 $row['crt_name']))) !== "")
2065 {
2066 echo $weakKey, "<br/>\n";
2067 continue;
2068 }
2069
2070 mysql_query("update `orgdomaincerts` set `renewed`='1' where `id`='$id'");
2071 if($row['revoke'] > 0)
2072 {
2073 printf(_("It would seem '%s' has already been revoked. I'll skip this for now.")."<br>\n", $row['CN']);
2074 continue;
2075 }
2076 $query = "insert into `orgdomaincerts` set
2077 `orgid`='".intval($row['orgid'])."',
2078 `CN`='".mysql_real_escape_string($row['CN'])."',
2079 `csr_name`='".mysql_real_escape_string($row['csr_name'])."',
2080 `created`='".mysql_real_escape_string($row['created'])."',
2081 `modified`=NOW(),
2082 `subject`='".mysql_real_escape_string($row['subject'])."',
2083 `type`='".intval($row['type'])."',
2084 `rootcert`='".intval($row['rootcert'])."',
2085 `description`='".mysql_real_escape_string($row['description'])."',
2086 `md`='".HashAlgorithms::clean($row['md'])."'";
2087 mysql_query($query);
2088 $newid = mysql_insert_id();
2089 //echo "NewID: $newid<br/>\n";
2090 $newfile=generatecertpath("csr","orgserver",$newid);
2091 copy($row['csr_name'], $newfile);
2092 mysql_query("update `orgdomaincerts` set `csr_name`='$newfile' where `id`='$newid'");
2093 echo _("Renewing").": ".$row['CN']."<br>\n";
2094 $res = mysql_query("select * from `orgdomlink` where `orgcertid`='".$row['id']."'");
2095 while($r2 = mysql_fetch_assoc($res))
2096 mysql_query("insert into `orgdomlink` set `orgdomid`='".intval($r2['orgdomid'])."', `orgcertid`='$newid'");
2097 waitForResult("orgdomaincerts", $newid,$oldid,0);
2098 $query = "select * from `orgdomaincerts` where `id`='$newid' and `crt_name` != ''";
2099 $res = mysql_query($query);
2100 if(mysql_num_rows($res) <= 0)
2101 {
2102 printf(_("Your certificate request has failed to be processed correctly, see %sthe WIKI page%s for reasons and solutions.")." newid: $newid", "<a href='http://wiki.cacert.org/wiki/FAQ/CertificateRenewal'>", "</a>");
2103 } else {
2104 $drow = mysql_fetch_assoc($res);
2105 $crtname = escapeshellarg($drow['crt_name']);
2106 $cert = shell_exec("/usr/bin/openssl x509 -in $crtname");
2107 echo "<pre>\n$cert\n</pre>\n";
2108 }
2109 }
2110 }
2111 else
2112 {
2113 echo _("You did not select any certificates for renewal.");
2114 }
2115 showfooter();
2116 exit;
2117 }
2118
2119 if($oldid == 22 && array_key_exists('revoke',$_REQUEST) && $_REQUEST['revoke'] != "")
2120 {
2121 csrf_check('orgsrvcerchange');
2122 showheader(_("My CAcert.org Account!"));
2123 if(is_array($_REQUEST['revokeid']))
2124 {
2125 echo _("Now revoking the following certificates:")."<br>\n";
2126 foreach($_REQUEST['revokeid'] as $id)
2127 {
2128 $id = intval($id);
2129 $query = "select *,UNIX_TIMESTAMP(`orgdomaincerts`.`revoked`) as `revoke` from
2130 `orgdomaincerts`,`org`
2131 where `orgdomaincerts`.`id`='$id' and
2132 `orgdomaincerts`.`orgid`=`org`.`orgid` and
2133 `org`.`memid`='".intval($_SESSION['profile']['id'])."'";
2134 $res = mysql_query($query);
2135 if(mysql_num_rows($res) <= 0)
2136 {
2137 printf(_("Invalid ID '%s' presented, can't do anything with it.")."<br>\n", $id);
2138 continue;
2139 }
2140 $row = mysql_fetch_assoc($res);
2141 if($row['revoke'] > 0)
2142 {
2143 printf(_("It would seem '%s' has already been revoked. I'll skip this for now.")."<br>\n", $row['CN']);
2144 continue;
2145 }
2146 mysql_query("update `orgdomaincerts` set `revoked`='1970-01-01 10:00:01' where `id`='$id'");
2147 printf(_("Certificate for '%s' with the serial no '%s' has been revoked.").'<br/>', htmlspecialchars($row['CN']), htmlspecialchars($row['serial']));
2148 }
2149
2150 // TRANSLATORS: Please don't translate "Certificate Revocation List (CRL)", it's a technical term
2151 echo '<br/>'._('All listed certificates will be added to the Certificate Revocation List (CRL) soon.').'<br/>';
2152 }
2153 else
2154 {
2155 echo _("You did not select any certificates for revocation.");
2156 }
2157
2158 if(array_key_exists('delid',$_REQUEST) && is_array($_REQUEST['delid']))
2159 {
2160 echo _("Now deleting the following pending requests:")."<br>\n";
2161 foreach($_REQUEST['delid'] as $id)
2162 {
2163 $id = intval($id);
2164 $query = "select *,UNIX_TIMESTAMP(`orgdomaincerts`.`expire`) as `expired` from
2165 `orgdomaincerts`,`org`
2166 where `orgdomaincerts`.`id`='$id' and
2167 `orgdomaincerts`.`orgid`=`org`.`orgid` and
2168 `org`.`memid`='".intval($_SESSION['profile']['id'])."'";
2169 $res = mysql_query($query);
2170 if(mysql_num_rows($res) <= 0)
2171 {
2172 printf(_("Invalid ID '%s' presented, can't do anything with it.")."<br>\n", $id);
2173 continue;
2174 }
2175 $row = mysql_fetch_assoc($res);
2176 if($row['expired'] > 0)
2177 {
2178 printf(_("Couldn't remove the request for `%s`, request had already been processed.")."<br>\n", $row['CN']);
2179 continue;
2180 }
2181 mysql_query("delete from `orgdomaincerts` where `id`='$id'");
2182 @unlink($row['csr_name']);
2183 @unlink($row['crt_name']);
2184 printf(_("Removed a pending request for '%s'")."<br>\n", $row['CN']);
2185 }
2186 }
2187 showfooter();
2188 exit;
2189 }
2190
2191 if($oldid == 22 && array_key_exists('change',$_REQUEST) && $_REQUEST['change'] != "")
2192 {
2193 showheader(_("My CAcert.org Account!"));
2194 foreach($_REQUEST as $id => $val)
2195 {
2196 if(substr($id,0,14)=="check_comment_")
2197 {
2198 $cid = intval(substr($id,14));
2199 $comment=trim(mysql_real_escape_string(stripslashes($_REQUEST['comment_'.$cid])));
2200 mysql_query("update `orgdomaincerts` set `description`='$comment' where `id`='$cid'");
2201 }
2202 }
2203 echo(_("Certificate settings have been changed.")."<br/>\n");
2204 showfooter();
2205 exit;
2206 }
2207
2208 if($oldid == 22 && array_key_exists('filter',$_REQUEST) && $_REQUEST['filter']!= "")
2209 {
2210 $id=22;
2211 $_SESSION['_config']['dorgfilterid']=$_REQUEST['dorgfilterid'];
2212 $_SESSION['_config']['dsorting']=$_REQUEST['dsorting'];
2213 $_SESSION['_config']['dstatus']=$_REQUEST['dstatus'];
2214 }
2215
2216 if($oldid == 22 && array_key_exists('reset',$_REQUEST) && $_REQUEST['reset']!= "")
2217 {
2218 $id=22;
2219 $_SESSION['_config']['dorgfilterid']=0;
2220 $_SESSION['_config']['dsorting']=0;
2221 $_SESSION['_config']['dstatus']=0;
2222 }
2223
2224
2225 if(($id == 24 || $oldid == 24 || $id == 25 || $oldid == 25 || $id == 26 || $oldid == 26 ||
2226 $id == 27 || $oldid == 27 || $id == 28 || $oldid == 28 || $id == 29 || $oldid == 29 ||
2227 $id == 30 || $oldid == 30 || $id == 31 || $oldid == 31) &&
2228 $_SESSION['profile']['orgadmin'] != 1)
2229 {
2230 showheader(_("My CAcert.org Account!"));
2231 echo _("You don't have access to this area.");
2232 showfooter();
2233 exit;
2234 }
2235
2236 if($oldid == 24 && $process != "")
2237 {
2238 $id = intval($oldid);
2239 $_SESSION['_config']['O'] = trim(mysql_real_escape_string(stripslashes($_REQUEST['O'])));
2240 $_SESSION['_config']['contact'] = trim(mysql_real_escape_string(stripslashes($_REQUEST['contact'])));
2241 $_SESSION['_config']['L'] = trim(mysql_real_escape_string(stripslashes($_REQUEST['L'])));
2242 $_SESSION['_config']['ST'] = trim(mysql_real_escape_string(stripslashes($_REQUEST['ST'])));
2243 $_SESSION['_config']['C'] = trim(mysql_real_escape_string(stripslashes($_REQUEST['C'])));
2244 $_SESSION['_config']['comments'] = trim(mysql_real_escape_string(stripslashes($_REQUEST['comments'])));
2245
2246 if($_SESSION['_config']['O'] == "" || $_SESSION['_config']['contact'] == "")
2247 {
2248 $_SESSION['_config']['errmsg'] = _("Organisation Name and Contact Email are required fields.");
2249 } else {
2250 mysql_query("insert into `orginfo` set `O`='".$_SESSION['_config']['O']."',
2251 `contact`='".$_SESSION['_config']['contact']."',
2252 `L`='".$_SESSION['_config']['L']."',
2253 `ST`='".$_SESSION['_config']['ST']."',
2254 `C`='".strtoupper($_SESSION['_config']['C'])."',
2255 `comments`='".$_SESSION['_config']['comments']."'");
2256 showheader(_("My CAcert.org Account!"));
2257 printf(_("'%s' has just been successfully added as an organisation to the database."), sanitizeHTML($_SESSION['_config']['O']));
2258 showfooter();
2259 exit;
2260 }
2261 }
2262
2263 if($oldid == 27 && $process != "")
2264 {
2265 csrf_check('orgdetchange');
2266 $id = intval($oldid);
2267 $_SESSION['_config']['O'] = trim(mysql_real_escape_string(stripslashes($_REQUEST['O'])));
2268 $_SESSION['_config']['contact'] = trim(mysql_real_escape_string(stripslashes($_REQUEST['contact'])));
2269 $_SESSION['_config']['L'] = trim(mysql_real_escape_string(stripslashes($_REQUEST['L'])));
2270 $_SESSION['_config']['ST'] = trim(mysql_real_escape_string(stripslashes($_REQUEST['ST'])));
2271 $_SESSION['_config']['C'] = trim(mysql_real_escape_string(stripslashes($_REQUEST['C'])));
2272 $_SESSION['_config']['comments'] = trim(mysql_real_escape_string(stripslashes($_REQUEST['comments'])));
2273
2274 if($_SESSION['_config']['O'] == "" || $_SESSION['_config']['contact'] == "")
2275 {
2276 $_SESSION['_config']['errmsg'] = _("Organisation Name and Contact Email are required fields.");
2277 } else {
2278 mysql_query("update `orginfo` set `O`='".$_SESSION['_config']['O']."',
2279 `contact`='".$_SESSION['_config']['contact']."',
2280 `L`='".$_SESSION['_config']['L']."',
2281 `ST`='".$_SESSION['_config']['ST']."',
2282 `C`='".strtoupper($_SESSION['_config']['C'])."',
2283 `comments`='".$_SESSION['_config']['comments']."'
2284 where `id`='".intval($_SESSION['_config']['orgid'])."'");
2285 showheader(_("My CAcert.org Account!"));
2286 printf(_("'%s' has just been successfully updated in the database."), sanitizeHTML($_SESSION['_config']['O']));
2287 showfooter();
2288 exit;
2289 }
2290 }
2291
2292 if($oldid == 28 && $process != "" && array_key_exists("domainname",$_REQUEST))
2293 {
2294 $domain = $_SESSION['_config']['domain'] = trim(mysql_real_escape_string(stripslashes($_REQUEST['domainname'])));
2295 $res1 = mysql_query("select * from `orgdomains` where `domain`='$domain'");
2296 if(mysql_num_rows($res1) > 0)
2297 {
2298 $_SESSION['_config']['errmsg'] = sprintf(_("The domain '%s' is already in a different account and is listed as valid. Can't continue."), sanitizeHTML($domain));
2299 $id = $oldid;
2300 $oldid=0;
2301 }
2302 }
2303
2304 if($oldid == 28 && $_SESSION['_config']['orgid'] <= 0)
2305 {
2306 $oldid=0;
2307 $id = 25;
2308 }
2309
2310 if($oldid == 28 && $process != "" && array_key_exists("orgid",$_SESSION["_config"]))
2311 {
2312 mysql_query("insert into `orgdomains` set `orgid`='".intval($_SESSION['_config']['orgid'])."', `domain`='$domain'"