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