685b53a5a83ce321845b978513b2d596e713a253
[cacert.git] / includes / account.php
1 <? /*
2 LibreSSL - CAcert web application
3 Copyright (C) 2004-2008 CAcert Inc.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; version 2 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 require_once("../includes/loggedin.php");
19
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 $line = trim(str_replace(":", " ", $line));
492
493 $bits = explode(" ", $line);
494 foreach($bits as $bit)
495 {
496 if(strstr($bit, "@"))
497 $line = $bit;
498 }
499 if(!in_array($line, $addy) && $line != "")
500 $addy[] = trim(mysql_real_escape_string(stripslashes($line)));
501 }
502 }
503
504 $rfc = array("root@$newdomain", "hostmaster@$newdomain", "postmaster@$newdomain", "admin@$newdomain", "webmaster@$newdomain");
505 foreach($rfc as $sub)
506 if(!in_array($sub, $addy))
507 $addy[] = $sub;
508 $_SESSION['_config']['addy'] = $addy;
509 $_SESSION['_config']['domain'] = mysql_real_escape_string($newdomain);
510 }
511
512 if($process != "" && $oldid == 8)
513 {
514 csrf_check('ctcinfo');
515 $oldid=0;
516 $id = 8;
517
518 $authaddy = trim(mysql_real_escape_string(stripslashes($_REQUEST['authaddy'])));
519
520 if($authaddy == "" || !is_array($_SESSION['_config']['addy']))
521 {
522 showheader(_("My CAcert.org Account!"));
523 echo _("The address you submitted isn't a valid authority address for the domain.");
524 showfooter();
525 exit;
526 }
527
528 if(!in_array($authaddy, $_SESSION['_config']['addy']))
529 {
530 showheader(_("My CAcert.org Account!"));
531 echo _("The address you submitted isn't a valid authority address for the domain.");
532 showfooter();
533 exit;
534 }
535
536 $query = "select * from `domains` where `domain`='".mysql_real_escape_string($_SESSION['_config']['domain'])."' and `deleted`=0";
537 $res = mysql_query($query);
538 if(mysql_num_rows($res) > 0)
539 {
540 showheader(_("My CAcert.org Account!"));
541 printf(_("The domain '%s' is already in a different account and is listed as valid. Can't continue."), sanitizeHTML($_SESSION['_config']['domain']));
542 showfooter();
543 exit;
544 }
545 $checkemail = checkEmail($authaddy);
546 if($checkemail != "OK")
547 {
548 showheader(_("My CAcert.org Account!"));
549 //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";
550 if (substr($checkemail, 0, 1) == "4")
551 {
552 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";
553 } else {
554 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";
555 }
556 echo "<p>$checkemail</p>\n";
557 showfooter();
558 exit;
559 }
560
561 $hash = make_hash();
562 $query = "insert into `domains` set `domain`='".mysql_real_escape_string($_SESSION['_config']['domain'])."',
563 `memid`='".$_SESSION['profile']['id']."',`created`=NOW(),`hash`='$hash'";
564 mysql_query($query);
565 $domainid = mysql_insert_id();
566
567 $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";
568 $body .= "http://".$_SESSION['_config']['normalhostname']."/verify.php?type=domain&domainid=$domainid&hash=$hash\n\n";
569 $body .= _("Best regards")."\n"._("CAcert.org Support!");
570
571 sendmail($authaddy, "[CAcert.org] "._("Email Probe"), $body, "support@cacert.org", "", "", "CAcert Support");
572
573 showheader(_("My CAcert.org Account!"));
574 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']);
575 showfooter();
576 exit;
577 }
578
579 if($process != "" && $oldid == 9)
580 {
581 $id = 9;
582 showheader(_("My CAcert.org Account!"));
583 if(array_key_exists('delid',$_REQUEST) && is_array($_REQUEST['delid']))
584 {
585 echo _("The following domains have been removed:")."<br>
586 ("._("Any valid certificates will be revoked as well").")<br>\n";
587
588 foreach($_REQUEST['delid'] as $id)
589 {
590 $id = intval($id);
591 $query = "select * from `domains` where `id`='$id' and `memid`='".$_SESSION['profile']['id']."'";
592 $res = mysql_query($query);
593 if(mysql_num_rows($res) > 0)
594 {
595 $row = mysql_fetch_assoc($res);
596 echo $row['domain']."<br>\n";
597 mysql_query("update `domains` set `deleted`=NOW() where `id`='$id'");
598 $dres = mysql_query("select * from `domlink` where `domid`='$id'");
599 while($drow = mysql_fetch_assoc($dres))
600 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");
601 }
602 }
603 }
604 else
605 {
606 echo _("You did not select any domains for removal.");
607 }
608
609 showfooter();
610 exit;
611 }
612
613 if($process != "" && $oldid == 10)
614 {
615 $CSR = clean_csr($_REQUEST['CSR']);
616 $_SESSION['_config']['tmpfname'] = tempnam("/tmp", "id10CSR");
617 $fp = fopen($_SESSION['_config']['tmpfname'], "w");
618 if(strpos($CSR,"---BEGIN")===FALSE)
619 {
620 // In case the CSR is missing the ---BEGIN lines, add them automatically:
621 fputs($fp,"-----BEGIN CERTIFICATE REQUEST-----\n".$CSR."\n-----END CERTIFICATE REQUEST-----\n");
622 }
623 else
624 {
625 fputs($fp, $CSR);
626 }
627 fclose($fp);
628 $CSR = $_SESSION['_config']['tmpfname'];
629 $_SESSION['_config']['subject'] = trim(`/usr/bin/openssl req -text -noout -in "$CSR"|tr -d "\\0"|grep "Subject:"`);
630 $bits = explode(",", trim(`/usr/bin/openssl req -text -noout -in "$CSR"|tr -d "\\0"|grep -A1 'X509v3 Subject Alternative Name:'|grep DNS:`));
631 foreach($bits as $val)
632 {
633 $_SESSION['_config']['subject'] .= "/subjectAltName=".trim($val);
634 }
635 $id = 11;
636
637 $_SESSION['_config']['0.CN'] = $_SESSION['_config']['0.subjectAltName'] = "";
638 extractit();
639 getcn();
640 getalt();
641
642 if($_SESSION['_config']['0.CN'] == "" && $_SESSION['_config']['0.subjectAltName'] == "")
643 {
644 showheader(_("My CAcert.org Account!"));
645 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.");
646 showfooter();
647 exit;
648 }
649
650 $_SESSION['_config']['rootcert'] = 1;
651 if($_SESSION['profile']['points'] >= 50)
652 {
653 $_SESSION['_config']['rootcert'] = intval($_REQUEST['rootcert']);
654 if($_SESSION['_config']['rootcert'] < 1 || $_SESSION['_config']['rootcert'] > 2)
655 $_SESSION['_config']['rootcert'] = 1;
656 }
657 }
658
659 if($process != "" && $oldid == 11)
660 {
661 $id = 11;
662 if($_SESSION['_config']['0.CN'] == "" && $_SESSION['_config']['0.subjectAltName'] == "")
663 {
664 showheader(_("My CAcert.org Account!"));
665 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.");
666 showfooter();
667 exit;
668 }
669
670 $subject = "";
671 $count = 0;
672 $supressSAN=0;
673 if($_SESSION["profile"]["id"] == 104074) $supressSAN=1;
674
675 if(is_array($_SESSION['_config']['rows']))
676 foreach($_SESSION['_config']['rows'] as $row)
677 {
678 $count++;
679 if($count <= 1)
680 {
681 $subject .= "/CN=$row";
682 if(!$supressSAN) $subject .= "/subjectAltName=DNS:$row";
683 if(!$supressSAN) $subject .= "/subjectAltName=otherName:1.3.6.1.5.5.7.8.5;UTF8:$row";
684 } else {
685 if(!$supressSAN) $subject .= "/subjectAltName=DNS:$row";
686 if(!$supressSAN) $subject .= "/subjectAltName=otherName:1.3.6.1.5.5.7.8.5;UTF8:$row";
687 }
688 }
689 if(is_array($_SESSION['_config']['altrows']))
690 foreach($_SESSION['_config']['altrows'] as $row)
691 {
692 if(substr($row, 0, 4) == "DNS:")
693 {
694 $row = substr($row, 4);
695 if(!$supressSAN) $subject .= "/subjectAltName=DNS:$row";
696 if(!$supressSAN) $subject .= "/subjectAltName=otherName:1.3.6.1.5.5.7.8.5;UTF8:$row";
697 }
698 }
699 if($_SESSION['_config']['rootcert'] < 1 || $_SESSION['_config']['rootcert'] > 2)
700 $_SESSION['_config']['rootcert'] = 1;
701
702 if(array_key_exists('0',$_SESSION['_config']['rowid']) && $_SESSION['_config']['rowid']['0'] > 0)
703 {
704 $query = "insert into `domaincerts` set
705 `CN`='".mysql_real_escape_string($_SESSION['_config']['rows']['0'])."',
706 `domid`='".mysql_real_escape_string($_SESSION['_config']['rowid']['0'])."',
707 `created`=NOW(),`subject`='".mysql_real_escape_string($subject)."',
708 `rootcert`='".mysql_real_escape_string($_SESSION['_config']['rootcert'])."'";
709 } elseif(array_key_exists('0',$_SESSION['_config']['altid']) && $_SESSION['_config']['altid']['0'] > 0) {
710 $query = "insert into `domaincerts` set
711 `CN`='".mysql_real_escape_string($_SESSION['_config']['altrows']['0'])."',
712 `domid`='".mysql_real_escape_string($_SESSION['_config']['altid']['0'])."',
713 `created`=NOW(),`subject`='".mysql_real_escape_string($subject)."',
714 `rootcert`='".mysql_real_escape_string($_SESSION['_config']['rootcert'])."'";
715 } else {
716 showheader(_("My CAcert.org Account!"));
717 echo _("Domain not verified.");
718 showfooter();
719 exit;
720
721 }
722
723 mysql_query($query);
724 $CSRid = mysql_insert_id();
725
726 if(is_array($_SESSION['_config']['rowid']))
727 foreach($_SESSION['_config']['rowid'] as $dom)
728 mysql_query("insert into `domlink` set `certid`='$CSRid', `domid`='$dom'");
729 if(is_array($_SESSION['_config']['altid']))
730 foreach($_SESSION['_config']['altid'] as $dom)
731 mysql_query("insert into `domlink` set `certid`='$CSRid', `domid`='$dom'");
732
733 $CSRname=generatecertpath("csr","server",$CSRid);
734 if(!file_exists($_SESSION['_config']['tmpfname']))
735 {
736 showheader(_("My CAcert.org Account!"));
737 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>");
738 showfooter();
739 exit;
740 }
741 rename($_SESSION['_config']['tmpfname'], $CSRname);
742 chmod($CSRname,0644);
743 mysql_query("update `domaincerts` set `CSR_name`='$CSRname' where `id`='$CSRid'");
744 waitForResult("domaincerts", $CSRid, 11);
745 $query = "select * from `domaincerts` where `id`='$CSRid' and `crt_name` != ''";
746 $res = mysql_query($query);
747 if(mysql_num_rows($res) <= 0)
748 {
749 $id = 11;
750 showheader(_("My CAcert.org Account!"));
751 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>");
752 showfooter();
753 exit;
754 } else {
755 $id = 15;
756 $cert = $CSRid;
757 $_REQUEST['cert']=$CSRid;
758 }
759 }
760
761 if($oldid == 12 && array_key_exists('renew',$_REQUEST) && $_REQUEST['renew'] != "")
762 {
763 csrf_check('srvcerchange');
764 $id = 12;
765 showheader(_("My CAcert.org Account!"));
766 if(is_array($_REQUEST['revokeid']))
767 {
768 echo _("Now renewing the following certificates:")."<br>\n";
769 foreach($_REQUEST['revokeid'] as $id)
770 {
771 $id = intval($id);
772 echo _("Processing request")." $id:<br/>";
773 $query = "select *,UNIX_TIMESTAMP(`domaincerts`.`revoked`) as `revoke` from `domaincerts`,`domains`
774 where `domaincerts`.`id`='$id' and
775 `domaincerts`.`domid`=`domains`.`id` and
776 `domains`.`memid`='".$_SESSION['profile']['id']."'";
777 $res = mysql_query($query);
778 if(mysql_num_rows($res) <= 0)
779 {
780 printf(_("Invalid ID '%s' presented, can't do anything with it.")."<br/>\n", $id);
781 continue;
782 }
783 mysql_query("update `domaincerts` set `renewed`='1' where `id`='$id'");
784 $row = mysql_fetch_assoc($res);
785 $query = "insert into `domaincerts` set
786 `domid`='".$row['domid']."',
787 `CN`='".mysql_real_escape_string($row['CN'])."',
788 `subject`='".mysql_real_escape_string($row['subject'])."',".
789 //`csr_name`='".$row['csr_name']."', // RACE CONDITION
790 "`created`='".$row['created']."',
791 `modified`=NOW(),
792 `rootcert`='".$row['rootcert']."',
793 `type`='".$row['type']."',
794 `pkhash`='".$row['pkhash']."'";
795 mysql_query($query);
796 $newid = mysql_insert_id();
797 $newfile=generatecertpath("csr","server",$newid);
798 copy($row['csr_name'], $newfile);
799 $_SESSION['_config']['subject'] = trim(`/usr/bin/openssl req -text -noout -in "$newfile"|tr -d "\\0"|grep "Subject:"`);
800 $bits = explode(",", trim(`/usr/bin/openssl req -text -noout -in "$newfile"|tr -d "\\0"|grep -A1 'X509v3 Subject Alternative Name:'|grep DNS:`));
801 foreach($bits as $val)
802 {
803 $_SESSION['_config']['subject'] .= "/subjectAltName=".trim($val);
804 }
805 $_SESSION['_config']['0.CN'] = $_SESSION['_config']['0.subjectAltName'] = "";
806 extractit();
807 getcn();
808 getalt();
809
810 if($_SESSION['_config']['0.CN'] == "" && $_SESSION['_config']['0.subjectAltName'] == "")
811 {
812 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.");
813 continue;
814 }
815
816 $subject = "";
817 $count = 0;
818 if(is_array($_SESSION['_config']['rows']))
819 foreach($_SESSION['_config']['rows'] as $row)
820 {
821 $count++;
822 if($count <= 1)
823 {
824 $subject .= "/CN=$row";
825 if(!strstr($subject, "=$row/") &&
826 substr($subject, -strlen("=$row")) != "=$row")
827 $subject .= "/subjectAltName=$row";
828 } else {
829 if(!strstr($subject, "=$row/") &&
830 substr($subject, -strlen("=$row")) != "=$row")
831 $subject .= "/subjectAltName=$row";
832 }
833 }
834 if(is_array($_SESSION['_config']['altrows']))
835 foreach($_SESSION['_config']['altrows'] as $row)
836 if(!strstr($subject, "=$row/") &&
837 substr($subject, -strlen("=$row")) != "=$row")
838 $subject .= "/subjectAltName=$row";
839 $subject = mysql_real_escape_string($subject);
840 mysql_query("update `domaincerts` set `subject`='$subject',`csr_name`='$newfile' where `id`='$newid'");
841
842 echo _("Renewing").": ".sanitizeHTML($_SESSION['_config']['0.CN'])."<br>\n";
843 waitForResult("domaincerts", $newid,$oldid,0);
844 $query = "select * from `domaincerts` where `id`='$newid' and `crt_name` != ''";
845 $res = mysql_query($query);
846 if(mysql_num_rows($res) <= 0)
847 {
848 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>");
849 } else {
850 $drow = mysql_fetch_assoc($res);
851 $cert = `/usr/bin/openssl x509 -in $drow[crt_name]`;
852 echo "<pre>\n$cert\n</pre>\n";
853 }
854 }
855 }
856 else
857 {
858 echo _("You did not select any certificates for renewal.");
859 }
860 showfooter();
861 exit;
862 }
863
864 if($oldid == 12 && array_key_exists('revoke',$_REQUEST) && $_REQUEST['revoke'] != "")
865 {
866 csrf_check('srvcerchange');
867 $id = 12;
868 showheader(_("My CAcert.org Account!"));
869 if(is_array($_REQUEST['revokeid']))
870 {
871 echo _("Now revoking the following certificates:")."<br>\n";
872 foreach($_REQUEST['revokeid'] as $id)
873 {
874 $id = intval($id);
875 $query = "select *,UNIX_TIMESTAMP(`domaincerts`.`revoked`) as `revoke` from `domaincerts`,`domains`
876 where `domaincerts`.`id`='$id' and
877 `domaincerts`.`domid`=`domains`.`id` and
878 `domains`.`memid`='".$_SESSION['profile']['id']."'";
879 $res = mysql_query($query);
880 if(mysql_num_rows($res) <= 0)
881 {
882 printf(_("Invalid ID '%s' presented, can't do anything with it.")."<br>\n", $id);
883 continue;
884 }
885 $row = mysql_fetch_assoc($res);
886 if($row['revoke'] > 0)
887 {
888 printf(_("It would seem '%s' has already been revoked. I'll skip this for now.")."<br>\n", $row['CN']);
889 continue;
890 }
891 mysql_query("update `domaincerts` set `revoked`='1970-01-01 10:00:01' where `id`='$id'");
892 printf(_("Certificate for '%s' has been revoked.")."<br>\n", $row['CN']);
893 }
894 }
895 else
896 {
897 echo _("You did not select any certificates for revocation.");
898 }
899
900 if(array_key_exists('delid',$_REQUEST) && is_array($_REQUEST['delid']))
901 {
902 echo _("Now deleting the following pending requests:")."<br>\n";
903 foreach($_REQUEST['delid'] as $id)
904 {
905 $id = intval($id);
906 $query = "select *,UNIX_TIMESTAMP(`domaincerts`.`expire`) as `expired` from `domaincerts`,`domains`
907 where `domaincerts`.`id`='$id' and
908 `domaincerts`.`domid`=`domains`.`id` and
909 `domains`.`memid`='".$_SESSION['profile']['id']."'";
910 $res = mysql_query($query);
911 if(mysql_num_rows($res) <= 0)
912 {
913 printf(_("Invalid ID '%s' presented, can't do anything with it.")."<br>\n", $id);
914 continue;
915 }
916 $row = mysql_fetch_assoc($res);
917 if($row['expired'] > 0)
918 {
919 printf(_("Couldn't remove the request for `%s`, request had already been processed.")."<br>\n", $row['CN']);
920 continue;
921 }
922 mysql_query("delete from `domaincerts` where `id`='$id'");
923 @unlink($row['csr_name']);
924 @unlink($row['crt_name']);
925 printf(_("Removed a pending request for '%s'")."<br>\n", $row['CN']);
926 }
927 }
928 showfooter();
929 exit;
930 }
931
932 if($oldid == 5 && array_key_exists('renew',$_REQUEST) && $_REQUEST['renew'] != "")
933 {
934 showheader(_("My CAcert.org Account!"));
935 if(is_array($_REQUEST['revokeid']))
936 {
937 echo _("Now renewing the following certificates:")."<br>\n";
938 foreach($_REQUEST['revokeid'] as $id)
939 {
940 $id = intval($id);
941 $query = "select *,UNIX_TIMESTAMP(`revoked`) as `revoke` from `emailcerts`
942 where `id`='$id' and `memid`='".$_SESSION['profile']['id']."'";
943 $res = mysql_query($query);
944 if(mysql_num_rows($res) <= 0)
945 {
946 printf(_("Invalid ID '%s' presented, can't do anything with it.")."<br>\n", $id);
947 continue;
948 }
949 mysql_query("update `emailcerts` set `renewed`='1' where `id`='$id'");
950 $row = mysql_fetch_assoc($res);
951 $query = "insert into emailcerts set
952 `memid`='".$row['memid']."',
953 `CN`='".mysql_real_escape_string($row['CN'])."',
954 `subject`='".mysql_real_escape_string($row['subject'])."',
955 `keytype`='".$row['keytype']."',
956 `csr_name`='".$row['csr_name']."',
957 `created`='".$row['created']."',
958 `modified`=NOW(),
959 `disablelogin`='".$row['disablelogin']."',
960 `codesign`='".$row['codesign']."',
961 `rootcert`='".$row['rootcert']."'";
962 mysql_query($query);
963 $newid = mysql_insert_id();
964 $newfile=generatecertpath("csr","client",$newid);
965 copy($row['csr_name'], $newfile);
966 mysql_query("update `emailcerts` set `csr_name`='$newfile' where `id`='$newid'");
967 $res = mysql_query("select * from `emaillink` where `emailcertsid`='".$row['id']."'");
968 while($r2 = mysql_fetch_assoc($res))
969 {
970 mysql_query("insert into `emaillink` set `emailid`='".$r2['emailid']."',
971 `emailcertsid`='$newid'");
972 }
973 waitForResult("emailcerts", $newid,$oldid,0);
974 $query = "select * from `emailcerts` where `id`='$newid' and `crt_name` != ''";
975 $res = mysql_query($query);
976 if(mysql_num_rows($res) <= 0)
977 {
978 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>");
979 } else {
980 printf(_("Certificate for '%s' has been renewed."), $row['CN']);
981 echo "<br/>\n<a href='account.php?id=6&cert=$newid' target='_new'>".
982 _("Click here")."</a> "._("to install your certificate.")."<br/><br/>\n";
983 }
984 }
985 }
986 else
987 {
988 echo _("You did not select any certificates for renewal.")."<br/>";
989 }
990
991 showfooter();
992 exit;
993 }
994
995 if($oldid == 5 && array_key_exists('revoke',$_REQUEST) && $_REQUEST['revoke'] != "")
996 {
997 $id = 5;
998 showheader(_("My CAcert.org Account!"));
999 if(array_key_exists('revokeid',$_REQUEST) && is_array($_REQUEST['revokeid']))
1000 {
1001 echo _("Now revoking the following certificates:")."<br>\n";
1002 foreach($_REQUEST['revokeid'] as $id)
1003 {
1004 $id = intval($id);
1005 $query = "select *,UNIX_TIMESTAMP(`revoked`) as `revoke` from `emailcerts`
1006 where `id`='$id' and `memid`='".$_SESSION['profile']['id']."'";
1007 $res = mysql_query($query);
1008 if(mysql_num_rows($res) <= 0)
1009 {
1010 printf(_("Invalid ID '%s' presented, can't do anything with it.")."<br>\n", $id);
1011 continue;
1012 }
1013 $row = mysql_fetch_assoc($res);
1014 if($row['revoke'] > 0)
1015 {
1016 printf(_("It would seem '%s' has already been revoked. I'll skip this for now.")."<br>\n", $row['CN']);
1017 continue;
1018 }
1019 mysql_query("update `emailcerts` set `revoked`='1970-01-01 10:00:01' where `id`='$id'");
1020 printf(_("Certificate for '%s' has been revoked.")."<br>\n", $row['CN']);
1021 }
1022 }
1023 else
1024 {
1025 echo _("You did not select any certificates for revocation.");
1026 }
1027
1028 if(array_key_exists('delid',$_REQUEST) && is_array($_REQUEST['delid']))
1029 {
1030 echo _("Now deleting the following pending requests:")."<br>\n";
1031 foreach($_REQUEST['delid'] as $id)
1032 {
1033 $id = intval($id);
1034 $query = "select *,UNIX_TIMESTAMP(`expire`) as `expired` from `emailcerts`
1035 where `id`='$id' and `memid`='".$_SESSION['profile']['id']."'";
1036 $res = mysql_query($query);
1037 if(mysql_num_rows($res) <= 0)
1038 {
1039 printf(_("Invalid ID '%s' presented, can't do anything with it.")."<br>\n", $id);
1040 continue;
1041 }
1042 $row = mysql_fetch_assoc($res);
1043 if($row['expired'] > 0)
1044 {
1045 printf(_("Couldn't remove the request for `%s`, request had already been processed.")."<br>\n", $row['CN']);
1046 continue;
1047 }
1048 mysql_query("delete from `emailcerts` where `id`='$id'");
1049 @unlink($row['csr_name']);
1050 @unlink($row['crt_name']);
1051 printf(_("Removed a pending request for '%s'")."<br>\n", $row['CN']);
1052 }
1053 }
1054 showfooter();
1055 exit;
1056 }
1057
1058 if($oldid == 5 && array_key_exists('change',$_REQUEST) && $_REQUEST['change'] != "")
1059 {
1060 showheader(_("My CAcert.org Account!"));
1061 //echo _("Now changing the settings for the following certificates:")."<br>\n";
1062 foreach($_REQUEST as $id => $val)
1063 {
1064 //echo $id."<br/>";
1065 if(substr($id,0,5)=="cert_")
1066 {
1067 $id = intval(substr($id,5));
1068 $dis=(array_key_exists('disablelogin_'.$id,$_REQUEST) && $_REQUEST['disablelogin_'.$id]=="1")?"0":"1";
1069 //echo "$id -> ".$_REQUEST['disablelogin_'.$id]."<br/>\n";
1070 mysql_query("update `emailcerts` set `disablelogin`='$dis' where `id`='$id' and `memid`='".$_SESSION['profile']['id']."'");
1071 //$row = mysql_fetch_assoc($res);
1072 }
1073 }
1074 echo(_("Certificate settings have been changed.")."<br/>\n");
1075 showfooter();
1076 exit;
1077 }
1078
1079
1080 if($oldid == 13 && $process != "")
1081 {
1082 csrf_check("perschange");
1083 $_SESSION['_config']['user'] = $_SESSION['profile'];
1084
1085 $_SESSION['_config']['user']['Q1'] = trim(mysql_real_escape_string(stripslashes(strip_tags($_REQUEST['Q1']))));
1086 $_SESSION['_config']['user']['Q2'] = trim(mysql_real_escape_string(stripslashes(strip_tags($_REQUEST['Q2']))));
1087 $_SESSION['_config']['user']['Q3'] = trim(mysql_real_escape_string(stripslashes(strip_tags($_REQUEST['Q3']))));
1088 $_SESSION['_config']['user']['Q4'] = trim(mysql_real_escape_string(stripslashes(strip_tags($_REQUEST['Q4']))));
1089 $_SESSION['_config']['user']['Q5'] = trim(mysql_real_escape_string(stripslashes(strip_tags($_REQUEST['Q5']))));
1090 $_SESSION['_config']['user']['A1'] = trim(mysql_real_escape_string(stripslashes(strip_tags($_REQUEST['A1']))));
1091 $_SESSION['_config']['user']['A2'] = trim(mysql_real_escape_string(stripslashes(strip_tags($_REQUEST['A2']))));
1092 $_SESSION['_config']['user']['A3'] = trim(mysql_real_escape_string(stripslashes(strip_tags($_REQUEST['A3']))));
1093 $_SESSION['_config']['user']['A4'] = trim(mysql_real_escape_string(stripslashes(strip_tags($_REQUEST['A4']))));
1094 $_SESSION['_config']['user']['A5'] = trim(mysql_real_escape_string(stripslashes(strip_tags($_REQUEST['A5']))));
1095
1096 if($_SESSION['_config']['user']['Q1'] == $_SESSION['_config']['user']['Q2'] ||
1097 $_SESSION['_config']['user']['Q1'] == $_SESSION['_config']['user']['Q3'] ||
1098 $_SESSION['_config']['user']['Q1'] == $_SESSION['_config']['user']['Q4'] ||
1099 $_SESSION['_config']['user']['Q1'] == $_SESSION['_config']['user']['Q5'] ||
1100 $_SESSION['_config']['user']['Q2'] == $_SESSION['_config']['user']['Q3'] ||
1101 $_SESSION['_config']['user']['Q2'] == $_SESSION['_config']['user']['Q4'] ||
1102 $_SESSION['_config']['user']['Q2'] == $_SESSION['_config']['user']['Q5'] ||
1103 $_SESSION['_config']['user']['Q3'] == $_SESSION['_config']['user']['Q4'] ||
1104 $_SESSION['_config']['user']['Q3'] == $_SESSION['_config']['user']['Q5'] ||
1105 $_SESSION['_config']['user']['Q4'] == $_SESSION['_config']['user']['Q5'] ||
1106 $_SESSION['_config']['user']['A1'] == $_SESSION['_config']['user']['Q1'] ||
1107 $_SESSION['_config']['user']['A1'] == $_SESSION['_config']['user']['Q2'] ||
1108 $_SESSION['_config']['user']['A1'] == $_SESSION['_config']['user']['Q3'] ||
1109 $_SESSION['_config']['user']['A1'] == $_SESSION['_config']['user']['Q4'] ||
1110 $_SESSION['_config']['user']['A1'] == $_SESSION['_config']['user']['Q5'] ||
1111 $_SESSION['_config']['user']['A2'] == $_SESSION['_config']['user']['Q3'] ||
1112 $_SESSION['_config']['user']['A2'] == $_SESSION['_config']['user']['Q4'] ||
1113 $_SESSION['_config']['user']['A2'] == $_SESSION['_config']['user']['Q5'] ||
1114 $_SESSION['_config']['user']['A3'] == $_SESSION['_config']['user']['Q4'] ||
1115 $_SESSION['_config']['user']['A3'] == $_SESSION['_config']['user']['Q5'] ||
1116 $_SESSION['_config']['user']['A4'] == $_SESSION['_config']['user']['Q5'] ||
1117 $_SESSION['_config']['user']['A1'] == $_SESSION['_config']['user']['A2'] ||
1118 $_SESSION['_config']['user']['A1'] == $_SESSION['_config']['user']['A3'] ||
1119 $_SESSION['_config']['user']['A1'] == $_SESSION['_config']['user']['A4'] ||
1120 $_SESSION['_config']['user']['A1'] == $_SESSION['_config']['user']['A5'] ||
1121 $_SESSION['_config']['user']['A2'] == $_SESSION['_config']['user']['A3'] ||
1122 $_SESSION['_config']['user']['A2'] == $_SESSION['_config']['user']['A4'] ||
1123 $_SESSION['_config']['user']['A2'] == $_SESSION['_config']['user']['A5'] ||
1124 $_SESSION['_config']['user']['A3'] == $_SESSION['_config']['user']['A4'] ||
1125 $_SESSION['_config']['user']['A3'] == $_SESSION['_config']['user']['A5'] ||
1126 $_SESSION['_config']['user']['A4'] == $_SESSION['_config']['user']['A5'])
1127 {
1128 $_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";
1129 $id = $oldid;
1130 $oldid=0;
1131 }
1132
1133 if($_SESSION['_config']['user']['Q1'] == "" || $_SESSION['_config']['user']['Q2'] == "" ||
1134 $_SESSION['_config']['user']['Q3'] == "" || $_SESSION['_config']['user']['Q4'] == "" ||
1135 $_SESSION['_config']['user']['Q5'] == "")
1136 {
1137 $_SESSION['_config']['errmsg'] .= _("For your own security you must enter 5 lost password questions and answers.")."<br>";
1138 $id = $oldid;
1139 $oldid=0;
1140 }
1141 }
1142
1143 if($oldid == 13 && $process != "")
1144 {
1145 $ddquery = "select sum(`points`) as `total` from `notary` where `to`='".$_SESSION['profile']['id']."' group by `to`";
1146 $ddres = mysql_query($ddquery);
1147 $ddrow = mysql_fetch_assoc($ddres);
1148 $_SESSION['profile']['points'] = $ddrow['total'];
1149
1150 if($_SESSION['profile']['points'] == 0)
1151 {
1152 $_SESSION['_config']['user']['fname'] = trim(mysql_real_escape_string(stripslashes(strip_tags($_REQUEST['fname']))));
1153 $_SESSION['_config']['user']['mname'] = trim(mysql_real_escape_string(stripslashes(strip_tags($_REQUEST['mname']))));
1154 $_SESSION['_config']['user']['lname'] = trim(mysql_real_escape_string(stripslashes(strip_tags($_REQUEST['lname']))));
1155 $_SESSION['_config']['user']['suffix'] = trim(mysql_real_escape_string(stripslashes(strip_tags($_REQUEST['suffix']))));
1156 $_SESSION['_config']['user']['day'] = intval($_REQUEST['day']);
1157 $_SESSION['_config']['user']['month'] = intval($_REQUEST['month']);
1158 $_SESSION['_config']['user']['year'] = intval($_REQUEST['year']);
1159
1160 if($_SESSION['_config']['user']['fname'] == "" || $_SESSION['_config']['user']['lname'] == "")
1161 {
1162 $_SESSION['_config']['errmsg'] .= _("First and Last name fields can not be blank.")."<br>";
1163 $id = $oldid;
1164 $oldid=0;
1165 }
1166 if($_SESSION['_config']['user']['year'] < 1900 || $_SESSION['_config']['user']['month'] < 1 || $_SESSION['_config']['user']['month'] > 12 ||
1167 $_SESSION['_config']['user']['day'] < 1 || $_SESSION['_config']['user']['day'] > 31)
1168 {
1169 $_SESSION['_config']['errmsg'] .= _("Invalid date of birth")."<br>\n";
1170 $id = $oldid;
1171 $oldid=0;
1172 }
1173 }
1174 }
1175
1176 if($oldid == 13 && $process != "")
1177 {
1178 if($_SESSION['profile']['points'] == 0)
1179 {
1180 $query = "update `users` set `fname`='".$_SESSION['_config']['user']['fname']."',
1181 `mname`='".$_SESSION['_config']['user']['mname']."',
1182 `lname`='".$_SESSION['_config']['user']['lname']."',
1183 `suffix`='".$_SESSION['_config']['user']['suffix']."',
1184 `dob`='".$_SESSION['_config']['user']['year']."-".$_SESSION['_config']['user']['month']."-".$_SESSION['_config']['user']['day']."'
1185 where `id`='".$_SESSION['profile']['id']."'";
1186 mysql_query($query);
1187 }
1188 $query = "update `users` set `Q1`='".$_SESSION['_config']['user']['Q1']."',
1189 `Q2`='".$_SESSION['_config']['user']['Q2']."',
1190 `Q3`='".$_SESSION['_config']['user']['Q3']."',
1191 `Q4`='".$_SESSION['_config']['user']['Q4']."',
1192 `Q5`='".$_SESSION['_config']['user']['Q5']."',
1193 `A1`='".$_SESSION['_config']['user']['A1']."',
1194 `A2`='".$_SESSION['_config']['user']['A2']."',
1195 `A3`='".$_SESSION['_config']['user']['A3']."',
1196 `A4`='".$_SESSION['_config']['user']['A4']."',
1197 `A5`='".$_SESSION['_config']['user']['A5']."'
1198 where `id`='".$_SESSION['profile']['id']."'";
1199 mysql_query($query);
1200
1201 //!!!Should be rewritten
1202 $_SESSION['_config']['user']['otphash'] = trim(mysql_real_escape_string(stripslashes(strip_tags($_REQUEST['otphash']))));
1203 $_SESSION['_config']['user']['otppin'] = trim(mysql_real_escape_string(stripslashes(strip_tags($_REQUEST['otppin']))));
1204 if($_SESSION['_config']['user']['otphash'] != "" && $_SESSION['_config']['user']['otppin'] != "")
1205 {
1206 $query = "update `users` set `otphash`='".$_SESSION['_config']['user']['otphash']."',
1207 `otppin`='".$_SESSION['_config']['user']['otppin']."' where `id`='".$_SESSION['profile']['id']."'";
1208 mysql_query($query);
1209 }
1210
1211 $_SESSION['_config']['user']['set'] = 0;
1212 $_SESSION['profile'] = mysql_fetch_assoc(mysql_query("select * from `users` where `id`='".$_SESSION['profile']['id']."'"));
1213 $_SESSION['profile']['loggedin'] = 1;
1214
1215 $ddquery = "select sum(`points`) as `total` from `notary` where `to`='".$_SESSION['profile']['id']."' group by `to`";
1216 $ddres = mysql_query($ddquery);
1217 $ddrow = mysql_fetch_assoc($ddres);
1218 $_SESSION['profile']['points'] = $ddrow['total'];
1219
1220
1221 $id = 13;
1222 showheader(_("My CAcert.org Account!"));
1223 echo _("Your details have been updated with the database.");
1224 showfooter();
1225 exit;
1226 }
1227
1228 if($oldid == 14 && $process != "")
1229 {
1230 $_SESSION['_config']['user']['oldpass'] = trim(mysql_real_escape_string(stripslashes($_REQUEST['oldpassword'])));
1231 $_SESSION['_config']['user']['pword1'] = trim(mysql_real_escape_string(stripslashes($_REQUEST['pword1'])));
1232 $_SESSION['_config']['user']['pword2'] = trim(mysql_real_escape_string(stripslashes($_REQUEST['pword2'])));
1233
1234 $id = 14;
1235 csrf_check("pwchange");
1236
1237 showheader(_("My CAcert.org Account!"));
1238 if($_SESSION['_config']['user']['pword1'] == "" || $_SESSION['_config']['user']['pword1'] != $_SESSION['_config']['user']['pword2'])
1239 {
1240 echo _("New Pass Phrases specified don't match or were blank.");
1241 } else {
1242 $score = checkpw($_SESSION['_config']['user']['pword1'], $_SESSION['profile']['email'], $_SESSION['profile']['fname'],
1243 $_SESSION['profile']['mname'], $_SESSION['profile']['lname'], $_SESSION['profile']['suffix']);
1244
1245 if($_SESSION['_config']['hostname'] != $_SESSION['_config']['securehostname'])
1246 {
1247 $match = mysql_query("select * from `users` where `id`='".$_SESSION['profile']['id']."' and
1248 (`password`=old_password('".$_SESSION['_config']['user']['oldpass']."') or
1249 `password`=sha1('".$_SESSION['_config']['user']['oldpass']."'))");
1250 $rc = mysql_num_rows($match);
1251 } else {
1252 $rc = 1;
1253 }
1254
1255 if(strlen($_SESSION['_config']['user']['pword1']) < 6) {
1256 echo _("The Pass Phrase you submitted was too short.");
1257 } else if($score < 3) {
1258 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);
1259 } else if($rc <= 0) {
1260 echo _("You failed to correctly enter your current Pass Phrase.");
1261 } else {
1262 mysql_query("update `users` set `password`=sha1('".$_SESSION['_config']['user']['pword1']."')
1263 where `id`='".$_SESSION['profile']['id']."'");
1264 echo _("Your Pass Phrase has been updated and your primary email account has been notified of the change.");
1265 $body = sprintf(_("Hi %s,"),$_SESSION['profile']['fname'])."\n";
1266 $body .= _("You are receiving this email because you or someone else")."\n";
1267 $body .= _("has changed the password on your account.")."\n";
1268
1269 $body .= _("Best regards")."\n"._("CAcert.org Support!");
1270
1271 sendmail($_SESSION['profile']['email'], "[CAcert.org] "._("Password Update Notification"), $body,
1272 "support@cacert.org", "", "", "CAcert Support");
1273 }
1274 }
1275 showfooter();
1276 exit;
1277 }
1278
1279 if($oldid == 16)
1280 {
1281 $id = 16;
1282 $_SESSION['_config']['emails'] = array();
1283
1284 foreach($_REQUEST['emails'] as $val)
1285 {
1286 $val = mysql_real_escape_string(stripslashes(trim($val)));
1287 $bits = explode("@", $val);
1288 $count = count($bits);
1289 if($count != 2)
1290 continue;
1291
1292 if(checkownership($bits[1]) == false)
1293 continue;
1294
1295 if(!is_array($_SESSION['_config']['row']))
1296 continue;
1297 else if($_SESSION['_config']['row']['id'] > 0)
1298 $_SESSION['_config']['domids'][] = $_SESSION['_config']['row']['id'];
1299
1300 if($val != "")
1301 $_SESSION['_config']['emails'][] = $val;
1302 }
1303 $_SESSION['_config']['name'] = mysql_real_escape_string(stripslashes(trim($_REQUEST['name'])));
1304 $_SESSION['_config']['OU'] = mysql_real_escape_string(stripslashes(trim($_REQUEST['OU'])));
1305 }
1306
1307 if($oldid == 16 && (intval(count($_SESSION['_config']['emails'])) + 0) <= 0)
1308 {
1309 $id = 16;
1310 showheader(_("My CAcert.org Account!"));
1311 echo _("I couldn't match any emails against your organisational account.");
1312 showfooter();
1313 exit;
1314 }
1315
1316 if($oldid == 16 && $process != "")
1317 {
1318
1319 if(array_key_exists('codesign',$_REQUEST) && $_REQUEST['codesign'] && $_SESSION['profile']['codesign'] && ($_SESSION['profile']['points'] >= 100))
1320 {
1321 $_REQUEST['codesign'] = 1;
1322 $_SESSION['_config']['codesign'] = 1;
1323 }
1324 else
1325 {
1326 $_REQUEST['codesign'] = 0;
1327 $_SESSION['_config']['codesign'] = 0;
1328 }
1329
1330 $_SESSION['_config']['rootcert'] = intval($_REQUEST['rootcert']);
1331 if($_SESSION['_config']['rootcert'] < 1 || $_SESSION['_config']['rootcert'] > 2)
1332 $_SESSION['_config']['rootcert'] = 1;
1333
1334 if(@count($_SESSION['_config']['emails']) > 0)
1335 $id = 17;
1336 }
1337
1338 if($oldid == 17)
1339 {
1340 $org = $_SESSION['_config']['row'];
1341 if($_REQUEST['keytype'] == "NS")
1342 {
1343 $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'])));
1344
1345 if($spkac == "" || strlen($spkac) < 128)
1346 {
1347 $id = 17;
1348 showheader(_("My CAcert.org Account!"));
1349 echo _("I didn't receive a valid Certificate Request, hit the back button and try again.");
1350 showfooter();
1351 exit;
1352 }
1353
1354 $count = 0;
1355 $emails = "";
1356 $addys = array();
1357 if(is_array($_SESSION['_config']['emails']))
1358 foreach($_SESSION['_config']['emails'] as $_REQUEST['email'])
1359 {
1360 if(!$emails)
1361 $defaultemail = $_REQUEST['email'];
1362 $emails .= "$count.emailAddress = $_REQUEST[email]\n";
1363 $count++;
1364 }
1365 if($_SESSION['_config']['name'] != "")
1366 $emails .= "commonName = ".$_SESSION['_config']['name']."\n";
1367 if($_SESSION['_config']['OU'])
1368 $emails .= "organizationalUnitName = ".$_SESSION['_config']['OU']."\n";
1369 if($org['O'])
1370 $emails .= "organizationName = ".$org['O']."\n";
1371 if($org['L'])
1372 $emails .= "localityName = ".$org['L']."\n";
1373 if($org['ST'])
1374 $emails .= "stateOrProvinceName = ".$org['ST']."\n";
1375 if($org['C'])
1376 $emails .= "countryName = ".$org['C']."\n";
1377 if($_SESSION['_config']['rootcert'] < 1 || $_SESSION['_config']['rootcert'] > 2)
1378 $_SESSION['_config']['rootcert'] = 1;
1379
1380 $emails .= "SPKAC = $spkac";
1381 $query = "insert into `orgemailcerts` set
1382 `CN`='$defaultemail',
1383 `keytype`='NS',
1384 `orgid`='".$org['orgid']."',
1385 `created`=FROM_UNIXTIME(UNIX_TIMESTAMP()),
1386 `codesign`='".$_SESSION['_config']['codesign']."',
1387 `rootcert`='".$_SESSION['_config']['rootcert']."'";
1388 mysql_query($query);
1389 $emailid = mysql_insert_id();
1390
1391 foreach($_SESSION['_config']['domids'] as $addy)
1392 mysql_query("insert into `domemaillink` set `emailcertsid`='$emailid', `emailid`='$addy'");
1393
1394 $CSRname=generatecertpath("csr","orgclient",$emailid);
1395 $fp = fopen($CSRname, "w");
1396 fputs($fp, $emails);
1397 fclose($fp);
1398 $challenge=$_SESSION['spkac_hash'];
1399 $res=`openssl spkac -verify -in $CSRname`;
1400 if(!strstr($res,"Challenge String: ".$challenge))
1401 {
1402 $id = $oldid;
1403 showheader(_("My CAcert.org Account!"));
1404 echo _("The challenge-response code of your certificate request did not match. Can't continue with certificaterequest.");
1405 showfooter();
1406 exit;
1407 }
1408 mysql_query("update `orgemailcerts` set `csr_name`='$CSRname' where `id`='$emailid'");
1409 } else if($_REQUEST['keytype'] == "MS" || $_REQUEST['keytype']=="VI") {
1410 $csr = "-----BEGIN CERTIFICATE REQUEST-----\n".clean_csr($_REQUEST['CSR'])."-----END CERTIFICATE REQUEST-----\n";
1411 $tmpfname = tempnam("/tmp", "id17CSR");
1412 $fp = fopen($tmpfname, "w");
1413 fputs($fp, $csr);
1414 fclose($fp);
1415
1416 $addys = array();
1417 $defaultemail = "";
1418 $csrsubject="";
1419
1420 if($_SESSION['_config']['name'] != "")
1421 $csrsubject = "/CN=".$_SESSION['_config']['name'];
1422 if(is_array($_SESSION['_config']['emails']))
1423 foreach($_SESSION['_config']['emails'] as $_REQUEST['email'])
1424 {
1425 if($defaultemail == "")
1426 $defaultemail = $_REQUEST['email'];
1427 $csrsubject .= "/emailAddress=$_REQUEST[email]";
1428 }
1429 if($_SESSION['_config']['OU'])
1430 $csrsubject .= "/organizationalUnitName=".$_SESSION['_config']['OU'];
1431 if($org['O'])
1432 $csrsubject .= "/organizationName=".$org['O'];
1433 if($org['L'])
1434 $csrsubject .= "/localityName=".$org['L'];
1435 if($org['ST'])
1436 $csrsubject .= "/stateOrProvinceName=".$org['ST'];
1437 if($org['C'])
1438 $csrsubject .= "/countryName=".$org['C'];
1439
1440 $tmpname = tempnam("/tmp", "id17csr");
1441 $do = `/usr/bin/openssl req -in $tmpfname -out $tmpname`;
1442 @unlink($tmpfname);
1443 $csr = "";
1444 $fp = fopen($tmpname, "r");
1445 while($data = fgets($fp, 4096))
1446 $csr .= $data;
1447 fclose($fp);
1448 @unlink($tmpname);
1449
1450 if($csr == "")
1451 {
1452 showheader(_("My CAcert.org Account!"));
1453 echo _("I didn't receive a valid Certificate Request, hit the back button and try again.");
1454 showfooter();
1455 exit;
1456 }
1457 if($_SESSION['_config']['rootcert'] < 1 || $_SESSION['_config']['rootcert'] > 2)
1458 $_SESSION['_config']['rootcert'] = 1;
1459
1460 $query = "insert into `orgemailcerts` set
1461 `CN`='$defaultemail',
1462 `keytype`='" . sanitizeHTML($_REQUEST['keytype']) . "',
1463 `orgid`='".$org['orgid']."',
1464 `created`=FROM_UNIXTIME(UNIX_TIMESTAMP()),
1465 `subject`='$csrsubject',
1466 `codesign`='".$_SESSION['_config']['codesign']."',
1467 `rootcert`='".$_SESSION['_config']['rootcert']."'";
1468 mysql_query($query);
1469 $emailid = mysql_insert_id();
1470
1471 foreach($_SESSION['_config']['domids'] as $addy)
1472 mysql_query("insert into `domemaillink` set `emailcertsid`='$emailid', `emailid`='$addy'");
1473
1474 $CSRname=generatecertpath("csr","orgclient",$emailid);
1475 $fp = fopen($CSRname, "w");
1476 fputs($fp, $csr);
1477 fclose($fp);
1478 mysql_query("update `orgemailcerts` set `csr_name`='$CSRname' where `id`='$emailid'");
1479 }
1480 waitForResult("orgemailcerts", $emailid,$oldid);
1481 $query = "select * from `orgemailcerts` where `id`='$emailid' and `crt_name` != ''";
1482 $res = mysql_query($query);
1483 if(mysql_num_rows($res) <= 0)
1484 {
1485 showheader(_("My CAcert.org Account!"));
1486 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>");
1487 showfooter();
1488 exit;
1489 } else {
1490 $id = 19;
1491 $cert = $emailid;
1492 $_REQUEST['cert']=$emailid;
1493 }
1494 }
1495
1496 if($oldid == 18 && array_key_exists('renew',$_REQUEST) && $_REQUEST['renew'] != "")
1497 {
1498 csrf_check('clicerchange');
1499 showheader(_("My CAcert.org Account!"));
1500 if(is_array($_REQUEST['revokeid']))
1501 {
1502 $id = 18;
1503 echo _("Now renewing the following certificates:")."<br>\n";
1504 foreach($_REQUEST['revokeid'] as $id)
1505 {
1506 echo "Renewing certificate #$id ...\n<br/>";
1507 $id = intval($id);
1508 $query = "select *,UNIX_TIMESTAMP(`revoked`) as `revoke` from `orgemailcerts`, `org`
1509 where `orgemailcerts`.`id`='$id' and `org`.`memid`='".$_SESSION['profile']['id']."' and
1510 `org`.`orgid`=`orgemailcerts`.`orgid`";
1511 $res = mysql_query($query);
1512 if(mysql_num_rows($res) <= 0)
1513 {
1514 printf(_("Invalid ID '%s' presented, can't do anything with it.")."<br>\n", $id);
1515 continue;
1516 }
1517 mysql_query("update `orgemailcerts` set `renewed`='1' where `id`='$id'");
1518 $row = mysql_fetch_assoc($res);
1519 if($row['revoke'] > 0)
1520 {
1521 printf(_("It would seem '%s' has already been revoked. I'll skip this for now.")."<br>\n", $row['CN']);
1522 continue;
1523 }
1524 $query = "insert into `orgemailcerts` set
1525 `orgid`='".$row['orgid']."',
1526 `CN`='".$row['CN']."',
1527 `subject`='".$row['subject']."',
1528 `keytype`='".$row['keytype']."',
1529 `csr_name`='".$row['csr_name']."',
1530 `created`='".$row['created']."',
1531 `modified`=NOW(),
1532 `codesign`='".$row['codesign']."',
1533 `rootcert`='".$row['rootcert']."'";
1534 mysql_query($query);
1535 $newid = mysql_insert_id();
1536 $newfile=generatecertpath("csr","orgclient",$newid);
1537 copy($row['csr_name'], $newfile);
1538 mysql_query("update `orgemailcerts` set `csr_name`='$newfile' where `id`='$newid'");
1539 waitForResult("orgemailcerts", $newid,$oldid,0);
1540 $query = "select * from `orgemailcerts` where `id`='$newid' and `crt_name` != ''";
1541 $res = mysql_query($query);
1542 if(mysql_num_rows($res) > 0)
1543 {
1544 printf(_("Certificate for '%s' has been renewed."), $row['CN']);
1545 echo "<a href='account.php?id=19&cert=$newid' target='_new'>".
1546 _("Click here")."</a> "._("to install your certificate.");
1547 }
1548 echo("<br/>");
1549 }
1550 }
1551 else
1552 {
1553 echo _("You did not select any certificates for renewal.");
1554 }
1555 showfooter();
1556 exit;
1557 }
1558
1559 if($oldid == 18 && array_key_exists('revoke',$_REQUEST) && $_REQUEST['revoke'] != "")
1560 {
1561 csrf_check('clicerchange');
1562 $id = 18;
1563 showheader(_("My CAcert.org Account!"));
1564 if(is_array($_REQUEST['revokeid']))
1565 {
1566 echo _("Now revoking the following certificates:")."<br>\n";
1567 foreach($_REQUEST['revokeid'] as $id)
1568 {
1569 $id = intval($id);
1570 $query = "select *,UNIX_TIMESTAMP(`revoked`) as `revoke` from `orgemailcerts`, `org`
1571 where `orgemailcerts`.`id`='$id' and `org`.`memid`='".$_SESSION['profile']['id']."' and
1572 `org`.`orgid`=`orgemailcerts`.`orgid`";
1573 $res = mysql_query($query);
1574 if(mysql_num_rows($res) <= 0)
1575 {
1576 printf(_("Invalid ID '%s' presented, can't do anything with it.")."<br>\n", $id);
1577 continue;
1578 }
1579 $row = mysql_fetch_assoc($res);
1580 if($row['revoke'] > 0)
1581 {
1582 printf(_("It would seem '%s' has already been revoked. I'll skip this for now.")."<br>\n", $row['CN']);
1583 continue;
1584 }
1585 mysql_query("update `orgemailcerts` set `revoked`='1970-01-01 10:00:01' where `id`='$id'");
1586 printf(_("Certificate for '%s' has been revoked.")."<br>\n", $row['CN']);
1587 }
1588 }
1589 else
1590 {
1591 echo _("You did not select any certificates for revocation.");
1592 }
1593
1594 if(array_key_exists('delid',$_REQUEST) && is_array($_REQUEST['delid']))
1595 {
1596 echo _("Now deleting the following pending requests:")."<br>\n";
1597 foreach($_REQUEST['delid'] as $id)
1598 {
1599 $id = intval($id);
1600 $query = "select *,UNIX_TIMESTAMP(`expire`) as `expired` from `orgemailcerts`, `org`
1601 where `orgemailcerts`.`id`='$id' and `org`.`memid`='".$_SESSION['profile']['id']."' and
1602 `org`.`orgid`=`orgemailcerts`.`orgid`";
1603 $res = mysql_query($query);
1604 if(mysql_num_rows($res) <= 0)
1605 {
1606 printf(_("Invalid ID '%s' presented, can't do anything with it.")."<br>\n", $id);
1607 continue;
1608 }
1609 $row = mysql_fetch_assoc($res);
1610 if($row['expired'] > 0)
1611 {
1612 printf(_("Couldn't remove the request for `%s`, request had already been processed.")."<br>\n", $row['CN']);
1613 continue;
1614 }
1615 mysql_query("delete from `orgemailcerts` where `id`='$id'");
1616 @unlink($row['csr_name']);
1617 @unlink($row['crt_name']);
1618 printf(_("Removed a pending request for '%s'")."<br>\n", $row['CN']);
1619 }
1620 }
1621 showfooter();
1622 exit;
1623 }
1624
1625 if($process != "" && $oldid == 20)
1626 {
1627 $CSR = clean_csr($_REQUEST['CSR']);
1628 $_SESSION['_config']['tmpfname'] = tempnam("/tmp", "id20CSR");
1629 $fp = fopen($_SESSION['_config']['tmpfname'], "w");
1630 fputs($fp, $CSR);
1631 fclose($fp);
1632 $CSR = $_SESSION['_config']['tmpfname'];
1633 $_SESSION['_config']['subject'] = trim(`/usr/bin/openssl req -text -noout -in "$CSR"|tr -d "\\0"|grep "Subject:"`);
1634 $bits = explode(",", trim(`/usr/bin/openssl req -text -noout -in "$CSR"|tr -d "\\0"|grep -A1 'X509v3 Subject Alternative Name:'|grep DNS:`));
1635 foreach($bits as $val)
1636 {
1637 $_SESSION['_config']['subject'] .= "/subjectAltName=".trim($val);
1638 }
1639 $id = 21;
1640
1641 $_SESSION['_config']['0.CN'] = $_SESSION['_config']['0.subjectAltName'] = "";
1642 extractit();
1643 getcn2();
1644 getalt2();
1645
1646 $query = "select * from `orginfo`,`org`,`orgdomains` where
1647 `org`.`memid`='".$_SESSION['profile']['id']."' and
1648 `org`.`orgid`=`orginfo`.`id` and
1649 `org`.`orgid`=`orgdomains`.`orgid` and
1650 `orgdomains`.`domain`='".$_SESSION['_config']['0.CN']."'";
1651 $_SESSION['_config']['CNorg'] = mysql_fetch_assoc(mysql_query($query));
1652 $query = "select * from `orginfo`,`org`,`orgdomains` where
1653 `org`.`memid`='".$_SESSION['profile']['id']."' and
1654 `org`.`orgid`=`orginfo`.`id` and
1655 `org`.`orgid`=`orgdomains`.`orgid` and
1656 `orgdomains`.`domain`='".$_SESSION['_config']['0.subjectAltName']."'";
1657 $_SESSION['_config']['SANorg'] = mysql_fetch_assoc(mysql_query($query));
1658 //echo "<pre>"; print_r($_SESSION['_config']); die;
1659
1660 if($_SESSION['_config']['0.CN'] == "" && $_SESSION['_config']['0.subjectAltName'] == "")
1661 {
1662 $id = 20;
1663 showheader(_("My CAcert.org Account!"));
1664 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.");
1665 showfooter();
1666 exit;
1667 }
1668
1669 $_SESSION['_config']['rootcert'] = intval($_REQUEST['rootcert']);
1670 if($_SESSION['_config']['rootcert'] < 1 || $_SESSION['_config']['rootcert'] > 2)
1671 $_SESSION['_config']['rootcert'] = 1;
1672 }
1673
1674 if($process != "" && $oldid == 21)
1675 {
1676 $id = 21;
1677
1678 if($_SESSION['_config']['0.CN'] == "" && $_SESSION['_config']['0.subjectAltName'] == "")
1679 {
1680 showheader(_("My CAcert.org Account!"));
1681 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.");
1682 showfooter();
1683 exit;
1684 }
1685
1686 if($_SESSION['_config']['rowid']['0'] > 0)
1687 {
1688 $query = "select * from `org`,`orginfo` where
1689 `orginfo`.`id`='".$_SESSION['_config']['rowid']['0']."' and
1690 `orginfo`.`id`=`org`.`orgid` and
1691 `org`.`memid`='".$_SESSION['profile']['id']."'";
1692 } else {
1693 $query = "select * from `org`,`orginfo` where
1694 `orginfo`.`id`='".$_SESSION['_config']['altid']['0']."' and
1695 `orginfo`.`id`=`org`.`orgid` and
1696 `org`.`memid`='".$_SESSION['profile']['id']."'";
1697 }
1698 $org = mysql_fetch_assoc(mysql_query($query));
1699 $csrsubject = "";
1700
1701 if($_SESSION['_config']['OU'])
1702 $csrsubject .= "/organizationalUnitName=".$_SESSION['_config']['OU'];
1703 if($org['O'])
1704 $csrsubject .= "/organizationName=".$org['O'];
1705 if($org['L'])
1706 $csrsubject .= "/localityName=".$org['L'];
1707 if($org['ST'])
1708 $csrsubject .= "/stateOrProvinceName=".$org['ST'];
1709 if($org['C'])
1710 $csrsubject .= "/countryName=".$org['C'];
1711 //if($org['contact'])
1712 // $csrsubject .= "/emailAddress=".trim($org['contact']);
1713
1714 if(is_array($_SESSION['_config']['rows']))
1715 foreach($_SESSION['_config']['rows'] as $row)
1716 $csrsubject .= "/commonName=$row";
1717 $SAN="";
1718 if(is_array($_SESSION['_config']['altrows']))
1719 foreach($_SESSION['_config']['altrows'] as $subalt)
1720 {
1721 if($SAN != "")
1722 $SAN .= ",";
1723 $SAN .= "$subalt";
1724 }
1725
1726 if($SAN != "")
1727 $csrsubject .= "/subjectAltName=".$SAN;
1728
1729 $type="";
1730 if($_REQUEST["ocspcert"]!="" && $_SESSION['profile']['admin'] == 1) $type="8";
1731 if($_SESSION['_config']['rootcert'] < 1 || $_SESSION['_config']['rootcert'] > 2)
1732 $_SESSION['_config']['rootcert'] = 1;
1733
1734 if($_SESSION['_config']['rowid']['0'] > 0)
1735 {
1736 $query = "insert into `orgdomaincerts` set
1737 `CN`='".$_SESSION['_config']['rows']['0']."',
1738 `orgid`='".$org['id']."',
1739 `created`=NOW(),
1740 `subject`='$csrsubject',
1741 `rootcert`='".$_SESSION['_config']['rootcert']."',
1742 `type`='$type'";
1743 } else {
1744 $query = "insert into `orgdomaincerts` set
1745 `CN`='".$_SESSION['_config']['altrows']['0']."',
1746 `orgid`='".$org['id']."',
1747 `created`=NOW(),
1748 `subject`='$csrsubject',
1749 `rootcert`='".$_SESSION['_config']['rootcert']."',
1750 `type`='$type'";
1751 }
1752 mysql_query($query);
1753 $CSRid = mysql_insert_id();
1754
1755 $CSRname=generatecertpath("csr","orgserver",$CSRid);
1756 rename($_SESSION['_config']['tmpfname'], $CSRname);
1757 chmod($CSRname,0644);
1758 mysql_query("update `orgdomaincerts` set `CSR_name`='$CSRname' where `id`='$CSRid'");
1759 if(is_array($_SESSION['_config']['rowid']))
1760 foreach($_SESSION['_config']['rowid'] as $id)
1761 mysql_query("insert into `orgdomlink` set `orgdomid`='$id', `orgcertid`='$CSRid'");
1762 if(is_array($_SESSION['_config']['altid']))
1763 foreach($_SESSION['_config']['altid'] as $id)
1764 mysql_query("insert into `orgdomlink` set `orgdomid`='$id', `orgcertid`='$CSRid'");
1765 waitForResult("orgdomaincerts", $CSRid,$oldid);
1766 $query = "select * from `orgdomaincerts` where `id`='$CSRid' and `crt_name` != ''";
1767 $res = mysql_query($query);
1768 if(mysql_num_rows($res) <= 0)
1769 {
1770 showheader(_("My CAcert.org Account!"));
1771 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>");
1772 showfooter();
1773 exit;
1774 } else {
1775 $id = 23;
1776 $cert = $CSRid;
1777 $_REQUEST['cert']=$CSRid;
1778 }
1779 }
1780
1781 if($oldid == 22 && array_key_exists('renew',$_REQUEST) && $_REQUEST['renew'] != "")
1782 {
1783 csrf_check('orgsrvcerchange');
1784 showheader(_("My CAcert.org Account!"));
1785 if(is_array($_REQUEST['revokeid']))
1786 {
1787 echo _("Now renewing the following certificates:")."<br>\n";
1788 foreach($_REQUEST['revokeid'] as $id)
1789 {
1790 $id = intval($id);
1791 $query = "select *,UNIX_TIMESTAMP(`orgdomaincerts`.`revoked`) as `revoke` from
1792 `orgdomaincerts`,`org`
1793 where `orgdomaincerts`.`id`='$id' and
1794 `orgdomaincerts`.`orgid`=`org`.`orgid` and
1795 `org`.`memid`='".$_SESSION['profile']['id']."'";
1796 $res = mysql_query($query);
1797 if(mysql_num_rows($res) <= 0)
1798 {
1799 printf(_("Invalid ID '%s' presented, can't do anything with it.")."<br>\n", $id);
1800 continue;
1801 }
1802 mysql_query("update `orgdomaincerts` set `renewed`='1' where `id`='$id'");
1803 $row = mysql_fetch_assoc($res);
1804 if($row['revoke'] > 0)
1805 {
1806 printf(_("It would seem '%s' has already been revoked. I'll skip this for now.")."<br>\n", $row['CN']);
1807 continue;
1808 }
1809 $query = "insert into `orgdomaincerts` set
1810 `orgid`='".$row['orgid']."',
1811 `CN`='".$row['CN']."',
1812 `csr_name`='".$row['csr_name']."',
1813 `created`='".$row['created']."',
1814 `modified`=NOW(),
1815 `subject`='".$row['subject']."',
1816 `type`='".$row['type']."',
1817 `rootcert`='".$row['rootcert']."'";
1818 mysql_query($query);
1819 $newid = mysql_insert_id();
1820 //echo "NewID: $newid<br/>\n";
1821 $newfile=generatecertpath("csr","orgserver",$newid);
1822 copy($row['csr_name'], $newfile);
1823 mysql_query("update `orgdomaincerts` set `csr_name`='$newfile' where `id`='$newid'");
1824 echo _("Renewing").": ".$row['CN']."<br>\n";
1825 $res = mysql_query("select * from `orgdomlink` where `orgcertid`='".$row['id']."'");
1826 while($r2 = mysql_fetch_assoc($res))
1827 mysql_query("insert into `orgdomlink` set `orgdomid`='".$r2['id']."', `orgcertid`='$newid'");
1828 waitForResult("orgdomaincerts", $newid,$oldid,0);
1829 $query = "select * from `orgdomaincerts` where `id`='$newid' and `crt_name` != ''";
1830 $res = mysql_query($query);
1831 if(mysql_num_rows($res) <= 0)
1832 {
1833 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>");
1834 } else {
1835 $drow = mysql_fetch_assoc($res);
1836 $cert = `/usr/bin/openssl x509 -in $drow[crt_name]`;
1837 echo "<pre>\n$cert\n</pre>\n";
1838 }
1839 }
1840 }
1841 else
1842 {
1843 echo _("You did not select any certificates for renewal.");
1844 }
1845 showfooter();
1846 exit;
1847 }
1848
1849 if($oldid == 22 && array_key_exists('revoke',$_REQUEST) && $_REQUEST['revoke'] != "")
1850 {
1851 csrf_check('orgsrvcerchange');
1852 showheader(_("My CAcert.org Account!"));
1853 if(is_array($_REQUEST['revokeid']))
1854 {
1855 echo _("Now revoking the following certificates:")."<br>\n";
1856 foreach($_REQUEST['revokeid'] as $id)
1857 {
1858 $id = intval($id);
1859 $query = "select *,UNIX_TIMESTAMP(`orgdomaincerts`.`revoked`) as `revoke` from
1860 `orgdomaincerts`,`org`
1861 where `orgdomaincerts`.`id`='$id' and
1862 `orgdomaincerts`.`orgid`=`org`.`orgid` and
1863 `org`.`memid`='".$_SESSION['profile']['id']."'";
1864 $res = mysql_query($query);
1865 if(mysql_num_rows($res) <= 0)
1866 {
1867 printf(_("Invalid ID '%s' presented, can't do anything with it.")."<br>\n", $id);
1868 continue;
1869 }
1870 $row = mysql_fetch_assoc($res);
1871 if($row['revoke'] > 0)
1872 {
1873 printf(_("It would seem '%s' has already been revoked. I'll skip this for now.")."<br>\n", $row['CN']);
1874 continue;
1875 }
1876 mysql_query("update `orgdomaincerts` set `revoked`='1970-01-01 10:00:01' where `id`='$id'");
1877 printf(_("Certificate for '%s' has been revoked.")."<br>\n", $row['CN']);
1878 }
1879 }
1880 else
1881 {
1882 echo _("You did not select any certificates for revocation.");
1883 }
1884
1885 if(array_key_exists('delid',$_REQUEST) && is_array($_REQUEST['delid']))
1886 {
1887 echo _("Now deleting the following pending requests:")."<br>\n";
1888 foreach($_REQUEST['delid'] as $id)
1889 {
1890 $id = intval($id);
1891 $query = "select *,UNIX_TIMESTAMP(`orgdomaincerts`.`expire`) as `expired` from
1892 `orgdomaincerts`,`org`
1893 where `orgdomaincerts`.`id`='$id' and
1894 `orgdomaincerts`.`orgid`=`org`.`orgid` and
1895 `org`.`memid`='".$_SESSION['profile']['id']."'";
1896 $res = mysql_query($query);
1897 if(mysql_num_rows($res) <= 0)
1898 {
1899 printf(_("Invalid ID '%s' presented, can't do anything with it.")."<br>\n", $id);
1900 continue;
1901 }
1902 $row = mysql_fetch_assoc($res);
1903 if($row['expired'] > 0)
1904 {
1905 printf(_("Couldn't remove the request for `%s`, request had already been processed.")."<br>\n", $row['CN']);
1906 continue;
1907 }
1908 mysql_query("delete from `orgdomaincerts` where `id`='$id'");
1909 @unlink($row['csr_name']);
1910 @unlink($row['crt_name']);
1911 printf(_("Removed a pending request for '%s'")."<br>\n", $row['CN']);
1912 }
1913 }
1914 showfooter();
1915 exit;
1916 }
1917
1918 if(($id == 24 || $oldid == 24 || $id == 25 || $oldid == 25 || $id == 26 || $oldid == 26 ||
1919 $id == 27 || $oldid == 27 || $id == 28 || $oldid == 28 || $id == 29 || $oldid == 29 ||
1920 $id == 30 || $oldid == 30 || $id == 31 || $oldid == 31) &&
1921 $_SESSION['profile']['orgadmin'] != 1)
1922 {
1923 showheader(_("My CAcert.org Account!"));
1924 echo _("You don't have access to this area.");
1925 showfooter();
1926 exit;
1927 }
1928
1929 if($oldid == 24 && $process != "")
1930 {
1931 $id = intval($oldid);
1932 $_SESSION['_config']['O'] = trim(mysql_real_escape_string(stripslashes($_REQUEST['O'])));
1933 $_SESSION['_config']['contact'] = trim(mysql_real_escape_string(stripslashes($_REQUEST['contact'])));
1934 $_SESSION['_config']['L'] = trim(mysql_real_escape_string(stripslashes($_REQUEST['L'])));
1935 $_SESSION['_config']['ST'] = trim(mysql_real_escape_string(stripslashes($_REQUEST['ST'])));
1936 $_SESSION['_config']['C'] = trim(mysql_real_escape_string(stripslashes($_REQUEST['C'])));
1937 $_SESSION['_config']['comments'] = trim(mysql_real_escape_string(stripslashes($_REQUEST['comments'])));
1938
1939 if($_SESSION['_config']['O'] == "" || $_SESSION['_config']['contact'] == "")
1940 {
1941 $_SESSION['_config']['errmsg'] = _("Organisation Name and Contact Email are required fields.");
1942 } else {
1943 mysql_query("insert into `orginfo` set `O`='".$_SESSION['_config']['O']."',
1944 `contact`='".$_SESSION['_config']['contact']."',
1945 `L`='".$_SESSION['_config']['L']."',
1946 `ST`='".$_SESSION['_config']['ST']."',
1947 `C`='".$_SESSION['_config']['C']."',
1948 `comments`='".$_SESSION['_config']['comments']."'");
1949 showheader(_("My CAcert.org Account!"));
1950 printf(_("'%s' has just been successfully added as an organisation to the database."), sanitizeHTML($_SESSION['_config']['O']));
1951 showfooter();
1952 exit;
1953 }
1954 }
1955
1956 if($oldid == 27 && $process != "")
1957 {
1958 csrf_check('orgdetchange');
1959 $id = intval($oldid);
1960 $_SESSION['_config']['O'] = trim(mysql_real_escape_string(stripslashes($_REQUEST['O'])));
1961 $_SESSION['_config']['contact'] = trim(mysql_real_escape_string(stripslashes($_REQUEST['contact'])));
1962 $_SESSION['_config']['L'] = trim(mysql_real_escape_string(stripslashes($_REQUEST['L'])));
1963 $_SESSION['_config']['ST'] = trim(mysql_real_escape_string(stripslashes($_REQUEST['ST'])));
1964 $_SESSION['_config']['C'] = trim(mysql_real_escape_string(stripslashes($_REQUEST['C'])));
1965 $_SESSION['_config']['comments'] = trim(mysql_real_escape_string(stripslashes($_REQUEST['comments'])));
1966
1967 if($_SESSION['_config']['O'] == "" || $_SESSION['_config']['contact'] == "")
1968 {
1969 $_SESSION['_config']['errmsg'] = _("Organisation Name and Contact Email are required fields.");
1970 } else {
1971 mysql_query("update `orginfo` set `O`='".$_SESSION['_config']['O']."',
1972 `contact`='".$_SESSION['_config']['contact']."',
1973 `L`='".$_SESSION['_config']['L']."',
1974 `ST`='".$_SESSION['_config']['ST']."',
1975 `C`='".$_SESSION['_config']['C']."',
1976 `comments`='".$_SESSION['_config']['comments']."'
1977 where `id`='".$_SESSION['_config']['orgid']."'");
1978 showheader(_("My CAcert.org Account!"));
1979 printf(_("'%s' has just been successfully updated in the database."), sanitizeHTML($_SESSION['_config']['O']));
1980 showfooter();
1981 exit;
1982 }
1983 }
1984
1985 if($oldid == 28 && $process != "" && array_key_exists("domainname",$_REQUEST))
1986 {
1987 $domain = $_SESSION['_config']['domain'] = trim(mysql_real_escape_string(stripslashes($_REQUEST['domainname'])));
1988 $res1 = mysql_query("select * from `orgdomains` where `domain`='$domain'");
1989 if(mysql_num_rows($res1) > 0)
1990 {
1991 $_SESSION['_config']['errmsg'] = sprintf(_("The domain '%s' is already in a different account and is listed as valid. Can't continue."), sanitizeHTML($domain));
1992 $id = $oldid;
1993 $oldid=0;
1994 }
1995 }
1996
1997 if($oldid == 28 && $_SESSION['_config']['orgid'] <= 0)
1998 {
1999 $oldid=0;
2000 $id = 25;
2001 }
2002
2003 if($oldid == 28 && $process != "" && array_key_exists("orgid",$_SESSION["_config"]))
2004 {
2005 mysql_query("insert into `orgdomains` set `orgid`='".intval($_SESSION['_config']['orgid'])."', `domain`='$domain'");
2006 showheader(_("My CAcert.org Account!"));
2007 printf(_("'%s' has just been successfully added to the database."), sanitizeHTML($domain));
2008 echo "<br><br><a href='account.php?id=26&orgid=".intval($_SESSION['_config']['orgid'])."'>"._("Click here")."</a> "._("to continue.");
2009 showfooter();
2010 exit;
2011 }
2012
2013 if($oldid == 29 && $process != "")
2014 {
2015 $domain = mysql_real_escape_string(stripslashes(trim($domainname)));
2016
2017 $res1 = mysql_query("select * from `orgdomains` where `domain` like '$domain' and `id`!='".intval($_SESSION['_config']['domid'])."'");
2018 $res2 = mysql_query("select * from `domains` where `domain` like '$domain' and `deleted`=0");
2019 if(mysql_num_rows($res1) > 0 || mysql_num_rows($res2) > 0)
2020 {
2021 $_SESSION['_config']['errmsg'] = sprintf(_("The domain '%s' is already in a different account and is listed as valid. Can't continue."), sanitizeHTML($domain));
2022 $id = $oldid;
2023 $oldid=0;
2024 }
2025 }
2026
2027 if(($oldid == 29 || $oldid == 30) && $process != _("Cancel"))
2028 {
2029 $query = "select `orgdomaincerts`.`id` as `id` from `orgdomlink`, `orgdomaincerts`, `orgdomains` where
2030 `orgdomlink`.`orgdomid`=`orgdomains`.`id` and
2031 `orgdomaincerts`.`id`=`orgdomlink`.`orgcertid` and
2032 `orgdomains`.`id`='".intval($_SESSION['_config']['domid'])."'";
2033 $res = mysql_query($query);
2034 while($row = mysql_fetch_assoc($res))
2035 mysql_query("update `orgdomaincerts` set `revoked`='1970-01-01 10:00:01' where `id`='".$row['id']."'");
2036
2037 $query = "select `orgemailcerts`.`id` as `id` from `orgemailcerts`, `orgemaillink`, `orgdomains` where
2038 `orgemaillink`.`domid`=`orgdomains`.`id` and
2039 `orgemailcerts`.`id`=`orgemaillink`.`emailcertsid` and
2040 `orgdomains`.`id`='".intval($_SESSION['_config']['domid'])."'";
2041 $res = mysql_query($query);
2042 while($row = mysql_fetch_assoc($res))
2043 mysql_query("update `orgemailcerts` set `revoked`='1970-01-01 10:00:01' where `id`='".intval($row['id'])."'");
2044 }
2045
2046 if($oldid == 29 && $process != "")
2047 {
2048 $row = mysql_fetch_assoc(mysql_query("select * from `orgdomains` where `id`='".intval($_SESSION['_config']['domid'])."'"));
2049 mysql_query("update `orgdomains` set `domain`='$domain' where `id`='".intval($_SESSION['_config']['domid'])."'");
2050 showheader(_("My CAcert.org Account!"));
2051 printf(_("'%s' has just been successfully updated in the database."), sanitizeHTML($domain));
2052 echo "<br><br><a href='account.php?id=26&orgid=".intval($_SESSION['_config']['orgid'])."'>"._("Click here")."</a> "._("to continue.");
2053 showfooter();
2054 exit;
2055 }
2056
2057 if($oldid == 30 && $process != "")
2058 {
2059 $row = mysql_fetch_assoc(mysql_query("select * from `orgdomains` where `id`='".intval($_SESSION['_config']['domid'])."'"));
2060 $domain = $row['domain'];
2061 mysql_query("delete from `orgdomains` where `id`='".intval($_SESSION['_config']['domid'])."'");
2062 showheader(_("My CAcert.org Account!"));
2063 printf(_("'%s' has just been successfully deleted from the database."), sanitizeHTML($domain));
2064 echo "<br><br><a href='account.php?id=26&orgid=".intval($_SESSION['_config']['orgid'])."'>"._("Click here")."</a> "._("to continue.");
2065 showfooter();
2066 exit;
2067 }
2068
2069 if($oldid == 30)
2070 {
2071 $id = 26;
2072 $orgid = 0;
2073 }
2074
2075 if($oldid == 31 && $process != _("Cancel"))
2076 {
2077 $query = "select * from `orgdomains` where `orgid`='".intval($_SESSION['_config']['orgid'])."'";
2078 $dres = mysql_query($query);
2079 while($drow = mysql_fetch_assoc($dres))
2080 {
2081 $query = "select `orgdomaincerts`.`id` as `id` from `orgdomlink`, `orgdomaincerts`, `orgdomains` where
2082 `orgdomlink`.`orgdomid`=`orgdomains`.`id` and
2083 `orgdomaincerts`.`id`=`orgdomlink`.`orgcertid` and
2084 `orgdomains`.`id`='".intval($drow['id'])."'";
2085 $res = mysql_query($query);
2086 while($row = mysql_fetch_assoc($res))
2087 {
2088 mysql_query("update `orgdomaincerts` set `revoked`='1970-01-01 10:00:01' where `id`='".intval($row['id'])."'");
2089 mysql_query("delete from `orgdomaincerts` where `orgid`='".intval($row['id'])."'");
2090 mysql_query("delete from `orgdomlink` where `domid`='".intval($row['id'])."'");
2091 }
2092
2093 $query = "select `orgemailcerts`.`id` as `id` from `orgemailcerts`, `orgemaillink`, `orgdomains` where
2094 `orgemaillink`.`domid`=`orgdomains`.`id` and
2095 `orgemailcerts`.`id`=`orgemaillink`.`emailcertsid` and
2096 `orgdomains`.`id`='".intval($drow['id'])."'";
2097 $res = mysql_query($query);
2098 while($row = mysql_fetch_assoc($res))
2099 {
2100 mysql_query("update `orgemailcerts` set `revoked`='1970-01-01 10:00:01' where `id`='".intval($row['id'])."'");
2101 mysql_query("delete from `orgemailcerts` where `id`='".intval($row['id'])."'");
2102 mysql_query("delete from `orgemaillink` where `domid`='".intval($row['id'])."'");
2103 }
2104 }
2105 mysql_query("delete from `org` where `orgid`='".intval($_SESSION['_config']['orgid'])."'");
2106 mysql_query("delete from `orgdomains` where `orgid`='".intval($_SESSION['_config']['orgid'])."'");
2107 mysql_query("delete from `orginfo` where `id`='".intval($_SESSION['_config']['orgid'])."'");
2108 }
2109
2110 if($oldid == 31)
2111 {
2112 $id = 25;
2113 $orgid = 0;
2114 }
2115
2116 if($id == 32 || $oldid == 32 || $id == 33 || $oldid == 33 || $id == 34 || $oldid == 34 ||
2117 $id == 35 || $oldid == 35)
2118 {
2119 $query = "select * from `org` where `memid`='".intval($_SESSION['profile']['id'])."' and `masteracc`='1'";
2120 $_macc = mysql_num_rows(mysql_query($query));
2121 if($_SESSION['profile']['orgadmin'] != 1 && $_macc <= 0)
2122 {
2123 showheader(_("My CAcert.org Account!"));
2124 echo _("You don't have access to this area.");
2125 showfooter();
2126 exit;
2127 }
2128 }
2129
2130 if($id == 33 && $_SESSION['profile']['orgadmin'] != 1)
2131 {
2132 $orgid = intval($_SESSION['_config']['orgid']);
2133 $query = "select * from `org` where `orgid`='$orgid' and `memid`='".intval($_SESSION['profile']['id'])."' and `masteracc`='1'";
2134 $res = mysql_query($query);
2135 if(mysql_num_rows($res) <= 0)
2136 {
2137 $id = 35;
2138 }
2139 }
2140
2141 if($oldid == 33 && $process != "")
2142 {
2143 csrf_check('orgadmadd');
2144 if($_SESSION['profile']['orgadmin'] == 1)
2145 $masteracc = $_SESSION['_config'][masteracc] = intval($_REQUEST['masteracc']);
2146 else
2147 $masteracc = $_SESSION['_config'][masteracc] = 0;
2148 $_REQUEST['email'] = $_SESSION['_config']['email'] = mysql_real_escape_string(stripslashes(trim($_REQUEST['email'])));
2149 $OU = $_SESSION['_config']['OU'] = mysql_real_escape_string(stripslashes(trim($_REQUEST['OU'])));
2150 $comments = $_SESSION['_config']['comments'] = mysql_real_escape_string(stripslashes(trim($_REQUEST['comments'])));
2151 $res = mysql_query("select * from `users` where `email`='".$_REQUEST['email']."' and `deleted`=0");
2152 if(mysql_num_rows($res) <= 0)
2153 {
2154 $id = $oldid;
2155 $oldid=0;
2156 $_SESSION['_config']['errmsg'] = sprintf(_("Wasn't able to match '%s' against any user in the system"), sanitizeHTML($_REQUEST['email']));
2157 } else {
2158 $row = mysql_fetch_assoc($res);
2159 mysql_query("insert into `org` set `memid`='".intval($row['id'])."', `orgid`='".intval($_SESSION['_config']['orgid'])."',
2160 `masteracc`='$masteracc', `OU`='$OU', `comments`='$comments'");
2161 }
2162 }
2163
2164 if(($oldid == 34 || $id == 34) && $_SESSION['profile']['orgadmin'] != 1)
2165 {
2166 $orgid = intval($_SESSION['_config']['orgid']);
2167 $res = mysql_query("select * from `org` where `orgid`='$orgid' and `memid`='".$_SESSION['profile']['id']."' and `masteracc`='1'");
2168 if(mysql_num_rows($res) <= 0)
2169 $id = 32;
2170 }
2171
2172 if($oldid == 34 && $process != "")
2173 {
2174 $orgid = intval($_SESSION['_config']['orgid']);
2175 $memid = intval($_REQUEST['memid']);
2176 $query = "delete from `org` where `orgid`='$orgid' and `memid`='$memid'";
2177 mysql_query($query);
2178 }
2179
2180 if($oldid == 34 || $oldid == 33)
2181 {
2182 $oldid=0;
2183 $id = 32;
2184 $orgid = 0;
2185 }
2186
2187 if($id == 36)
2188 {
2189 $row = mysql_fetch_assoc(mysql_query("select * from `alerts` where `memid`='".intval($_SESSION['profile']['id'])."'"));
2190 $_REQUEST['general'] = $row['general'];
2191 $_REQUEST['country'] = $row['country'];
2192 $_REQUEST['regional'] = $row['regional'];
2193 $_REQUEST['radius'] = $row['radius'];
2194 }
2195
2196 if($oldid == 36)
2197 {
2198 $rc = mysql_num_rows(mysql_query("select * from `alerts` where `memid`='".intval($_SESSION['profile']['id'])."'"));
2199 if($rc > 0)
2200 {
2201 $query = "update `alerts` set `general`='".intval(array_key_exists('general',$_REQUEST)?$_REQUEST['general']:0)."',
2202 `country`='".intval(array_key_exists('country',$_REQUEST)?$_REQUEST['country']:0)."',
2203 `regional`='".intval(array_key_exists('regional',$_REQUEST)?$_REQUEST['regional']:0)."',
2204 `radius`='".intval(array_key_exists('radius',$_REQUEST)?$_REQUEST['radius']:0)."'
2205 where `memid`='".intval($_SESSION['profile']['id'])."'";
2206 } else {
2207 $query = "insert into `alerts` set `general`='".intval(array_key_exists('general',$_REQUEST)?$_REQUEST['general']:0)."',
2208 `country`='".intval(array_key_exists('country',$_REQUEST)?$_REQUEST['country']:0)."',
2209 `regional`='".intval(array_key_exists('regional',$_REQUEST)?$_REQUEST['regional']:0)."',
2210 `radius`='".intval(array_key_exists('radius',$_REQUEST)?$_REQUEST['radius']:0)."',
2211 `memid`='".intval($_SESSION['profile']['id'])."'";
2212 }
2213 mysql_query($query);
2214 $id = $oldid;
2215 $oldid=0;
2216 }
2217
2218 if($oldid == 41 && $_REQUEST['action'] == 'default')
2219 {
2220 csrf_check("mainlang");
2221 $lang = mysql_real_escape_string($_REQUEST['lang']);
2222 foreach($_SESSION['_config']['translations'] as $key => $val)
2223 {
2224 if($key == $lang)
2225 {
2226 mysql_query("update `users` set `language`='$lang' where `id`='".$_SESSION['profile']['id']."'");
2227 $_SESSION['profile']['language'] = $lang;
2228 showheader(_("My CAcert.org Account!"));
2229 echo _("Your language setting has been updated.");
2230 showfooter();
2231 exit;
2232 }
2233 }
2234
2235 showheader(_("My CAcert.org Account!"));
2236 echo _("You tried to use an invalid language.");
2237 showfooter();
2238 exit;
2239 }
2240
2241 if($oldid == 41 && $_REQUEST['action'] == 'addsec')
2242 {
2243 csrf_check("seclang");
2244 $addlang = mysql_real_escape_string($_REQUEST['addlang']);
2245 // Does the language exist?
2246 mysql_query("insert into `addlang` set `userid`='".intval($_SESSION['profile']['id'])."', `lang`='$addlang'");
2247 showheader(_("My CAcert.org Account!"));
2248 echo _("Your language setting has been updated.");
2249 showfooter();
2250 exit;
2251 }
2252
2253 if($oldid == 41 && $_REQUEST['action'] == 'dellang')
2254 {
2255 csrf_check("seclang");
2256 $remove = mysql_real_escape_string($_REQUEST['remove']);
2257 mysql_query("delete from `addlang` where `userid`='".intval($_SESSION['profile']['id'])."' and `lang`='$remove'");
2258 showheader(_("My CAcert.org Account!"));
2259 echo _("Your language setting has been updated.");
2260 showfooter();
2261 exit;
2262 }
2263
2264 if(($id == 42 || $id == 43 || $id == 44 || $id == 48 || $id == 49 || $id == 50 ||
2265 $oldid == 42 || $oldid == 43 || $oldid == 44 || $oldid == 48 || $oldid == 49 || $oldid == 50) &&
2266 $_SESSION['profile']['admin'] != 1)
2267 {
2268 showheader(_("My CAcert.org Account!"));
2269 echo _("You don't have access to this area.");
2270 showfooter();
2271 exit;
2272 }
2273
2274 if(($id == 53 || $id == 54 || $oldid == 53 || $oldid == 54) &&
2275 $_SESSION['profile']['locadmin'] != 1)
2276 {
2277 showheader(_("My CAcert.org Account!"));
2278 echo _("You don't have access to this area.");
2279 showfooter();
2280 exit;
2281 }
2282
2283 if($oldid == 54 || ($id == 53 && array_key_exists('action',$_REQUEST) && $_REQUEST['action'] != "") ||
2284 ($id == 54 && array_key_exists('action',$_REQUEST) && $_REQUEST['action'] != "" &&
2285 $_REQUEST['action'] != "aliases" && $_REQUEST['action'] != "edit" && $_REQUEST['action'] != "add"))
2286 {
2287 $id = 53;
2288 $ccid = intval(array_key_exists('ccid',$_REQUEST)?$_REQUEST['ccid']:0);
2289 $regid = intval(array_key_exists('regid',$_REQUEST)?$_REQUEST['regid']:0);
2290 $newreg = intval(array_key_exists('newreg',$_REQUEST)?$_REQUEST['newreg']:0);
2291 $locid = intval(array_key_exists('locid',$_REQUEST)?$_REQUEST['locid']:0);
2292 $name = array_key_exists('name',$_REQUEST)?mysql_real_escape_string(strip_tags($_REQUEST['name'])):"";
2293 $long = array_key_exists('longitude',$_REQUEST)?ereg_replace("[^-0-9\.]","",$_REQUEST['longitude']):"";
2294 $lat = array_key_exists('latitude', $_REQUEST)?ereg_replace("[^-0-9\.]","",$_REQUEST['latitude']):"";
2295 $action = array_key_exists('action',$_REQUEST)?$_REQUEST['action']:"";
2296
2297 if($locid > 0 && $action == "edit")
2298 {
2299 $query = "update `locations` set `name`='$name', `lat`='$lat', `long`='$long' where `id`='$locid'";
2300 mysql_query($query);
2301 $row = mysql_fetch_assoc(mysql_query("select * from `locations` where `id`='$locid'"));
2302 $_REQUEST['regid'] = $row['regid'];
2303 unset($_REQUEST['ccid']);
2304 unset($_REQUEST['locid']);
2305 unset($_REQUEST['action']);
2306 } else if($regid > 0 && $action == "edit") {
2307 $query = "update `regions` set `name`='$name' where `id`='$regid'";
2308 mysql_query($query);
2309 $row = mysql_fetch_assoc(mysql_query("select * from `regions` where `id`='$regid'"));
2310 $_REQUEST['ccid'] = $row['ccid'];
2311 unset($_REQUEST['regid']);
2312 unset($_REQUEST['locid']);
2313 unset($_REQUEST['action']);
2314 } else if($regid > 0 && $action == "add") {
2315 $row = mysql_fetch_assoc(mysql_query("select `ccid` from `regions` where `id`='$regid'"));
2316 $ccid = $row['ccid'];
2317 $query = "insert into `locations` set `ccid`='$ccid', `regid`='$regid', `name`='$name', `lat`='$lat', `long`='$long'";
2318 mysql_query($query);
2319 unset($_REQUEST['ccid']);
2320 unset($_REQUEST['locid']);
2321 unset($_REQUEST['action']);
2322 } else if($ccid > 0 && $action == "add" && $name != "") {
2323 $query = "insert into `regions` set `ccid`='$ccid', `name`='$name'";
2324 mysql_query($query);
2325 $row = mysql_fetch_assoc(mysql_query("select * from `locations` where `id`='$locid'"));
2326 unset($_REQUEST['regid']);
2327 unset($_REQUEST['locid']);
2328 unset($_REQUEST['action']);
2329 } else if($locid > 0 && $action == "delete") {
2330 $row = mysql_fetch_assoc(mysql_query("select * from `locations` where `id`='$locid'"));
2331 $_REQUEST['regid'] = $row['regid'];
2332 mysql_query("delete from `localias` where `locid`='$locid'");
2333 mysql_query("delete from `locations` where `id`='$locid'");
2334 unset($_REQUEST['ccid']);
2335 unset($_REQUEST['locid']);
2336 unset($_REQUEST['action']);
2337 } else if($locid > 0 && $action == "move") {
2338 $row = mysql_fetch_assoc(mysql_query("select * from `locations` where `id`='$locid'"));
2339 $oldregid = $row['regid'];
2340 mysql_query("update `locations` set `regid`='$newreg' where `id`='$locid'");
2341 mysql_query("update `users` set `regid`='$newreg' where `regid`='$oldregid'");
2342 $row = mysql_fetch_assoc(mysql_query("select * from `locations` where `id`='$locid'"));
2343 $_REQUEST['regid'] = $row['regid'];
2344 unset($_REQUEST['ccid']);
2345 unset($_REQUEST['locid']);
2346 unset($_REQUEST['action']);
2347 } else if($regid > 0 && $action == "delete") {
2348 $row = mysql_fetch_assoc(mysql_query("select * from `regions` where `id`='$regid'"));
2349 $_REQUEST['ccid'] = $row['ccid'];
2350 mysql_query("delete from `locations` where `regid`='$regid'");
2351 mysql_query("delete from `regions` where `id`='$regid'");
2352 unset($_REQUEST['regid']);
2353 unset($_REQUEST['locid']);
2354 unset($_REQUEST['action']);
2355 } else if($locid > 0 && $action == "alias") {
2356 $id = 54;
2357 $_REQUEST['action'] = "aliases";
2358 $_REQUEST['locid'] = $locid;
2359 $name = htmlentities($name);
2360 $row = mysql_query("insert into `localias` set `locid`='$locid',`name`='$name'");
2361 } else if($locid > 0 && $action == "delalias") {
2362 $id = 54;
2363 $_REQUEST['action'] = "aliases";
2364 $_REQUEST['locid'] = $locid;
2365 $row = mysql_query("delete from `localias` where `locid`='$locid' and `name`='$name'");
2366 }
2367 }
2368
2369 if($oldid == 42 && $_REQUEST['email'] == "")
2370 {
2371 $id = $oldid;
2372 $oldid=0;
2373 }
2374
2375 if($oldid == 42)
2376 {
2377 $id = 43;
2378 $oldid=0;
2379 }
2380
2381 if($oldid == 43 && $_REQUEST['action'] == "updatedob")
2382 {
2383 $id = 43;
2384 $oldid=0;
2385 $fname = mysql_real_escape_string($_REQUEST['fname']);
2386 $mname = mysql_real_escape_string($_REQUEST['mname']);
2387 $lname = mysql_real_escape_string($_REQUEST['lname']);
2388 $suffix = mysql_real_escape_string($_REQUEST['suffix']);
2389 $day = intval($_REQUEST['day']);
2390 $month = intval($_REQUEST['month']);
2391 $year = intval($_REQUEST['year']);
2392 $userid = intval($_REQUEST['userid']);
2393 $query = "select `fname`,`mname`,`lname`,`suffix`,`dob` from `users` where `id`='$userid'";
2394 $details = mysql_fetch_assoc(mysql_query($query));
2395 $query = "insert into `adminlog` set `when`=NOW(),`old-lname`='${details['lname']}',`old-dob`='${details['dob']}',
2396 `new-lname`='$lname',`new-dob`='$year-$month-$day',`uid`='$userid',`adminid`='".$_SESSION['profile']['id']."'";
2397 mysql_query($query);
2398 $query = "update `users` set `fname`='$fname',`mname`='$mname',`lname`='$lname',`suffix`='$suffix',`dob`='$year-$month-$day' where `id`='$userid'";
2399 mysql_query($query);
2400 }
2401
2402 if($oldid == 48 && $_REQUEST['domain'] == "")
2403 {
2404 $id = $oldid;
2405 $oldid=0;
2406 }
2407
2408 if($oldid == 48)
2409 {
2410 $id = 49;
2411 $oldid=0;
2412 }
2413
2414 if($id == 44)
2415 {
2416 if($_REQUEST['userid'] != "")
2417 $_REQUEST['userid'] = intval($_REQUEST['userid']);
2418 $row = mysql_fetch_assoc(mysql_query("select * from `users` where `id`='".intval($_REQUEST['userid'])."'"));
2419 if($row['email'] == "")
2420 $id = 42;
2421 else
2422 $_REQUEST['email'] = $row['email'];
2423 }
2424
2425 if($oldid == 44)
2426 {
2427 showheader(_("My CAcert.org Account!"));
2428 if(intval($_REQUEST['userid']) <= 0)
2429 {
2430 echo _("No such user found.");
2431 } else {
2432 mysql_query("update `users` set `password`=sha1('".mysql_real_escape_string(stripslashes($_REQUEST['newpass']))."') where `id`='".intval($_REQUEST['userid'])."'");
2433 $row = mysql_fetch_assoc(mysql_query("select * from `users` where `id`='".intval($_REQUEST['userid'])."'"));
2434 printf(_("The password for %s has been updated successfully in the system."), sanitizeHTML($row['email']));
2435
2436
2437 $body = sprintf(_("Hi %s,"),$row['fname'])."\n";
2438 $body .= _("You are receiving this email because a CAcert administrator")."\n";
2439 $body .= _("has changed the password on your account.")."\n";
2440
2441 $body .= _("Best regards")."\n"._("CAcert.org Support!");
2442
2443 sendmail($row['email'], "[CAcert.org] "._("Password Update Notification"), $body,
2444 "support@cacert.org", "", "", "CAcert Support");
2445
2446 }
2447 showfooter();
2448 exit;
2449 }
2450
2451 if($process != "" && $oldid == 45)
2452 {
2453 $CSR = clean_csr($CSR);
2454 $_SESSION['_config']['CSR'] = $CSR;
2455 $_SESSION['_config']['subject'] = trim(`echo "$CSR"|/usr/bin/openssl req -text -noout|tr -d "\\0"|grep "Subject:"`);
2456 $bits = explode(",", trim(`echo "$CSR"|/usr/bin/openssl req -text -noout|tr -d "\\0"|grep -A1 'X509v3 Subject Alternative Name:'|grep DNS:`));
2457 foreach($bits as $val)
2458 {
2459 $_SESSION['_config']['subject'] .= "/subjectAltName=".trim($val);
2460 }
2461 $id = 46;
2462
2463 $_SESSION['_config']['0.CN'] = $_SESSION['_config']['0.subjectAltName'] = "";
2464 extractit();
2465 getcn();
2466 getalt();
2467
2468 if($_SESSION['_config']['0.CN'] == "" && $_SESSION['_config']['0.subjectAltName'] == "")
2469 {
2470 showheader(_("My CAcert.org Account!"));
2471 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.");
2472 showfooter();
2473 exit;
2474 }
2475 }
2476
2477 if($process != "" && $oldid == 46)
2478 {
2479 $CSR = clean_csr($_SESSION['_config']['CSR']);
2480 $_SESSION['_config']['subject'] = trim(`echo "$CSR"|/usr/bin/openssl req -text -noout|tr -d "\\0"|grep "Subject:"`);
2481 $bits = explode(",", trim(`echo "$CSR"|/usr/bin/openssl req -text -noout|tr -d "\\0"|grep -A1 'X509v3 Subject Alternative Name:'|grep DNS:`));
2482 foreach($bits as $val)
2483 {
2484 $_SESSION['_config']['subject'] .= "/subjectAltName=".trim($val);
2485 }
2486 $id = 11;
2487
2488 $_SESSION['_config']['0.CN'] = $_SESSION['_config']['0.subjectAltName'] = "";
2489 extractit();
2490 getcn();
2491 getalt();
2492
2493 if($_SESSION['_config']['0.CN'] == "" && $_SESSION['_config']['0.subjectAltName'] == "")
2494 {
2495 showheader(_("My CAcert.org Account!"));
2496 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.");
2497 showfooter();
2498 exit;
2499 }
2500
2501 $query = "insert into `domaincerts` set
2502 `CN`='".$_SESSION['_config']['0.CN']."',
2503 `domid`='".$_SESSION['_config']['row']['id']."',
2504 `created`=NOW()";
2505 mysql_query($query);
2506 $CSRid = mysql_insert_id();
2507
2508 foreach($_SESSION['_config']['rowid'] as $dom)
2509 mysql_query("insert into `domlink` set `certid`='$CSRid', `domid`='$dom'");
2510 if(is_array($_SESSION['_config']['altid']))
2511 foreach($_SESSION['_config']['altid'] as $dom)
2512 mysql_query("insert into `domlink` set `certid`='$CSRid', `domid`='$dom'");
2513
2514 $CSRname=generatecertpath("csr","server",$CSRid);
2515 $fp = fopen($CSRname, "w");
2516 fputs($fp, $_SESSION['_config']['CSR']);
2517 fclose($fp);
2518 mysql_query("update `domaincerts` set `CSR_name`='$CSRname' where `id`='$CSRid'");
2519 waitForResult("domaincerts", $CSRid,$oldid);
2520 $query = "select * from `domaincerts` where `id`='$CSRid' and `crt_name` != ''";
2521 $res = mysql_query($query);
2522 if(mysql_num_rows($res) <= 0)
2523 {
2524 showheader(_("My CAcert.org Account!"));
2525 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>");
2526 showfooter();
2527 exit;
2528 } else {
2529 $id = 47;
2530 $cert = $CSRid;
2531 $_REQUEST['cert']=$CSRid;
2532 }
2533 }
2534
2535 if($id == 43 && array_key_exists('tverify',$_REQUEST) && $_REQUEST['tverify'] > 0)
2536 {
2537 $memid = $_REQUEST['userid'] = intval($_REQUEST['tverify']);
2538 $query = "select * from `users` where `id`='$memid'";
2539 $row = mysql_fetch_assoc(mysql_query($query));
2540 $ver = !$row['tverify'];
2541 mysql_query("update `users` set `tverify`='$ver' where `id`='$memid'");
2542 }
2543
2544 if($id == 43 && array_key_exists('assurer',$_REQUEST) && $_REQUEST['assurer'] > 0)
2545 {
2546 csrf_check('admsetassuret');
2547 $memid = $_REQUEST['userid'] = intval($_REQUEST['assurer']);
2548 $query = "select * from `users` where `id`='$memid'";
2549 $row = mysql_fetch_assoc(mysql_query($query));
2550 $ver = !$row['assurer'];
2551 mysql_query("update `users` set `assurer`='$ver' where `id`='$memid'");
2552 }
2553
2554 if($id == 43 && array_key_exists('assurer_blocked',$_REQUEST) && $_REQUEST['assurer_blocked'] > 0)
2555 {
2556 $memid = $_REQUEST['userid'] = intval($_REQUEST['assurer_blocked']);
2557 $query = "select * from `users` where `id`='$memid'";
2558 $row = mysql_fetch_assoc(mysql_query($query));
2559 $ver = !$row['assurer_blocked'</