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