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