829bbcf180c19a0a626028efb45a8737e0b66e26
2 LibreSSL - CAcert web application
3 Copyright (C) 2004-2008 CAcert Inc.
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.
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.
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
19 require_once("../includes/loggedin.php");
20 require_once("../includes/lib/general.php");
21 require_once('../includes/notary.inc.php');
23 $id = 0; if(array_key_exists('id',$_REQUEST)) $id=intval($_REQUEST['id']);
24 $oldid = $_REQUEST['oldid'] = array_key_exists('oldid',$_REQUEST) ?
intval($_REQUEST['oldid']) : 0;
26 if($_SESSION['profile']['points'] < 50)
28 header("location: /account.php");
36 $CSR=""; if(array_key_exists('CSR',$_REQUEST)) $CSR=stripslashes($_REQUEST['CSR']);
41 if(array_key_exists('process',$_REQUEST) && $_REQUEST['process'] != "" && $CSR == "")
43 $_SESSION['_config']['errmsg'] = _("You failed to paste a valid GPG/PGP key.");
53 if($_SESSION["profile"]["id"] != 5897)
55 showheader(_("Welcome to CAcert.org"));
56 echo "The OpenPGP signing system is currently shutdown due to a maintenance. We hope to get it fixed within the next few hours. We are very sorry for the inconvenience.";
63 function verifyName($name)
65 if($name == "") return 0;
66 if($name == $_SESSION['profile']['fname']." ".$_SESSION['profile']['lname']) return 1;
67 if($name == $_SESSION['profile']['fname']." ".$_SESSION['profile']['mname']." ".$_SESSION['profile']['lname']) return 1;
68 if($name == $_SESSION['profile']['fname']." ".$_SESSION['profile']['lname']." ".$_SESSION['profile']['suffix']) return 1;
69 if($name == $_SESSION['profile']['fname']." ".$_SESSION['profile']['mname']." ".$_SESSION['profile']['lname']." ".$_SESSION['profile']['suffix']) return 1;
74 function verifyEmail($email)
76 if($email == "") return 0;
77 if(mysql_num_rows(mysql_query("select * from `email` where `memid`='".$_SESSION['profile']['id']."' and `email`='".mysql_real_escape_string($email)."' and `deleted`=0 and `hash`=''")) > 0) return 1;
85 if($oldid == "0" && $CSR != "")
87 if(!array_key_exists('CCA',$_REQUEST))
89 showheader(_("My CAcert.org Account!"));
90 echo _("You did not accept the CAcert Community Agreement (CCA), hit the back button and try again.");
95 $err = runCommand('mktemp --directory /tmp/cacert_gpg.XXXXXXXXXX',
105 $err = runCommand("gpg --with-colons --homedir $tmpdir 2>&1",
114 showheader(_("Welcome to CAcert.org"));
116 echo "<p style='color:#ff0000'>"._("There was an error parsing your key.")."</p>";
117 unset($_REQUEST['process']);
124 $gpgarr = explode("\n", trim($gpg));
125 foreach($gpgarr as $line)
127 #echo "Line[]: $line <br/>\n";
128 if(substr($line, 0, 3) == "pub" ||
substr($line, 0, 3) == "uid")
140 $resulttable=_("The following UIDs were found in your key:")."<br/><table border='1'><tr><td>#</td><td>"._("Name")."</td><td>"._("Email")."</td><td>Result</td>";
144 foreach(explode("\n", $gpg) as $line)
146 $bits = explode(":", $line);
147 $resulttable.="<tr><td>".++
$i."</td>";
148 $name = $comment = "";
149 if($bits[0] == "pub")
155 showheader(_("Welcome to CAcert.org"));
156 echo "<font color='#ff0000'>"._("Please upload only one key at a time.")."</font>";
157 unset($_REQUEST['process']);
162 if($bits[0] == "pub" && (!$keyid ||
!$when))
174 if(sizeof($bits)<10) $uidformatwrong=1;
176 if(preg_match("/\@.*\@/",$bits[9]))
178 showheader(_("Welcome to CAcert.org"));
180 echo "<font color='#ff0000'>"._("Multiple Email Adresses per UID are not allowed.")."</font>";
181 unset($_REQUEST['process']);
187 // Name (Comment) <Email>
188 if(preg_match("/^([^\(\)\[@<>]+) \(([^\(\)@<>]*)\) <([\w=\/%.-]*\@[\w.-]*|[\w.-]*\![\w=\/%.-]*)>/",$bits[9],$matches))
190 $name=trim(hex2bin($matches[1]));
192 $comm=trim(hex2bin($matches[2]));
193 $mail=trim(hex2bin($matches[3]));
196 elseif(preg_match("/^([^\(\)\[@<>]+) <([\w=\/%.-]*\@[\w.-]*|[\w.-]*\![\w=\/%.-]*)>/",$bits[9],$matches))
198 $name=trim(hex2bin($matches[1]));
201 $mail=trim(hex2bin($matches[2]));
203 // Unrecognized format
209 $nameok=verifyName($name);
210 $emailok=verifyEmail($mail);
216 $resulttable.="<td bgcolor='#".($nameok?
"c0ffc0":"ffc0c0")."'>".sanitizeHTML($name)."</td>";
217 $resulttable.="<td bgcolor='#".($emailok?
"c0ffc0":"ffc0c0")."'>".sanitizeHTML($mail)."</td>";
222 $rmessage=_("Error: UID is revoked");
224 elseif($uidformatwrong==1)
226 $rmessage=_("The format of the UID was not recognized. Please use 'Name (comment) <email@domain>'");
228 elseif($mail=="" and $name=="")
230 $rmessage=_("Error: Both Name and Email address are empty");
232 elseif($emailok and $nameok)
235 $rmessage=_("Name and Email OK.");
237 elseif(!$emailok and !$nameok)
239 $rmessage=_("Name and Email both cannot be matched with your account.");
241 elseif($emailok and $name=="")
244 $rmessage=_("The email is OK. The name is empty.");
246 elseif($nameok and $mail=="")
249 $rmessage=_("The name is OK. The email is empty.");
253 $rmessage=_("The email address has not been registered and verified in your account. Please add the email address to your account first.");
257 $rmessage=_("The name in the UID does not match the name in your account. Please verify the name.");
262 $rmessage=_("Error");
267 $resulttable.="<td>$rmessage</td>";
268 $lastvalidemail=$mail;
274 //echo "Adding UID $i\n";
275 $resulttable.="<td bgcolor='#ffc0c0'>$rmessage</td>";
277 $resulttable.="</tr>\n";
279 if($emailok) $multiple++
;
281 $resulttable.="</table>";
285 showheader(_("Welcome to CAcert.org"));
288 echo "<font color='#ff0000'>"._("No valid UIDs found on your key")."</font>";
289 unset($_REQUEST['process']);
296 $resulttable.=_("The unverified UIDs have been removed, the verified UIDs have been signed.");
303 if($oldid == "0" && $CSR != "")
305 write_user_agreement(intval($_SESSION['profile']['id']), "CCA", "certificate creation", "", 1);
307 //set variable for comment
308 if(trim($_REQUEST['description']) == ""){
311 $description= trim(mysql_real_escape_string(stripslashes($_REQUEST['description'])));
314 $query = "insert into `gpg` set `memid`='".intval($_SESSION['profile']['id'])."',
315 `email`='".mysql_real_escape_string($lastvalidemail)."',
317 `expires`='".mysql_real_escape_string($expires)."',
318 `multiple`='".mysql_real_escape_string($multiple)."',
319 `keyid`='".mysql_real_escape_string($keyid)."',
320 `description`='".mysql_real_escape_string($description)."'";
322 $insert_id = mysql_insert_id();
325 $cwd = '/tmp/gpgspace'.$insert_id;
328 $fp = fopen("$cwd/gpg.csr", "w");
329 fputs($fp, clean_gpgcsr($CSR));
333 system("gpg --homedir $cwd --import $cwd/gpg.csr");
336 $cmd_keyid = escapeshellarg($keyid);
337 $gpg = trim(`gpg
--homedir
$cwd --with
-colons
--fixed
-list-mode
--list-keys
$cmd_keyid 2>&1`
);
339 $gpgarr = explode("\n", $gpg);
340 foreach($gpgarr as $line)
342 //echo "Line[]: $line <br/>\n";
343 if(substr($line, 0, 4) == "uid:")
345 $name = $comment = "";
346 $bits = explode(":", $line);
348 $pos = strpos($bits[9], "(") - 1;
353 $pos = strpos($bits[9], "<") - 1;
357 $pos = strlen($bits[9]);
360 $name = trim(hex2bin(trim(substr($bits[9], 0, $pos))));
361 $nameok=verifyName($name);
365 $pos2 = strpos($bits[9], ")");
366 $comm = trim(hex2bin(trim(substr($bits[9], $pos, $pos2 - $pos))));
371 $pos = strpos($bits[9], "<") +
1;
375 if (preg_match("/<([\w.-]*\@[\w.-]*)>/", $bits[9],$match)) {
376 //echo "Found: ".$match[1];
377 $mail = trim(hex2bin($match[1]));
381 //echo "Not found!\n";
384 $emailok=verifyEmail($mail);
390 $ToBeDeleted[]=$uidid;
392 elseif($mail=="" and $name=="")
394 //echo "$uidid will be deleted\n";
395 $ToBeDeleted[]=$uidid;
397 elseif($emailok and $nameok)
400 elseif($emailok and $name=="")
403 elseif($nameok and $mail=="")
406 elseif(!$emailok and !$nameok)
408 //echo "$uidid will be deleted\n";
409 $ToBeDeleted[]=$uidid;
413 //echo "$uidid will be deleted\n";
414 $ToBeDeleted[]=$uidid;
418 //echo "$uidid will be deleted\n";
419 $ToBeDeleted[]=$uidid;
425 if(count($ToBeDeleted)>0)
427 $descriptorspec = array(
428 0 => array("pipe", "r"), // stdin is a pipe that the child will read from
429 1 => array("pipe", "w"), // stdout is a pipe that the child will write to
430 2 => array("pipe", "w") // stderr is a file to write to
433 $stderr = fopen('php://stderr', 'w');
435 //echo "Keyid: $keyid\n";
437 $cmd_keyid = escapeshellarg($keyid);
438 $process = proc_open("/usr/bin/gpg --homedir $cwd --no-tty --command-fd 0 --status-fd 1 --logger-fd 2 --edit-key $cmd_keyid", $descriptorspec, $pipes);
440 //echo "Process: $process\n";
441 //fputs($stderr,"Process: $process\n");
443 if (is_resource($process)) {
444 //echo("it is a resource\n");
445 // $pipes now looks like this:
446 // 0 => writeable handle connected to child stdin
447 // 1 => readable handle connected to child stdout
448 // Any error output will be appended to /tmp/error-output.txt
449 while (!feof($pipes[1]))
451 $buffer = fgets($pipes[1], 4096);
454 if($buffer == "[GNUPG:] GET_BOOL keyedit.sign_all.okay\n")
456 fputs($pipes[0],"yes\n");
458 elseif($buffer == "[GNUPG:] GOT_IT\n")
461 elseif(ereg("^\[GNUPG:\] GET_BOOL keyedit\.remove\.uid\.okay\s*",$buffer))
463 fputs($pipes[0],"yes\n");
465 elseif(ereg("^\[GNUPG:\] GET_LINE keyedit\.prompt\s*",$buffer))
467 if(count($ToBeDeleted)>0)
469 $delthisuid=array_pop($ToBeDeleted);
470 //echo "Deleting an UID $delthisuid\n";
471 fputs($pipes[0],"uid ".$delthisuid."\n");
476 fputs($pipes[0],$state?
"save\n":"deluid\n");
480 elseif($buffer == "[GNUPG:] GOOD_PASSPHRASE\n")
483 elseif(ereg("^\[GNUPG:\] KEYEXPIRED ",$buffer))
485 echo "Key expired!\n";
488 elseif($buffer == "")
494 echo "ERROR: UNKNOWN $buffer\n";
502 //echo stream_get_contents($pipes[1]);
505 // It is important that you close any pipes before calling
506 // proc_close in order to avoid a deadlock
507 $return_value = proc_close($process);
509 //echo "command returned $return_value\n";
513 echo "Keine ressource!\n";
520 $csrname=generatecertpath("csr","gpg",$insert_id);
521 $cmd_keyid = escapeshellarg($keyid);
522 $do=`gpg
--homedir
$cwd --batch
--export
-options export
-minimal
--export
$cmd_keyid >$csrname`
;
524 mysql_query("update `gpg` set `csr`='$csrname' where `id`='$insert_id'");
525 waitForResult('gpg', $insert_id);
527 showheader(_("Welcome to CAcert.org"));
529 $query = "select * from `gpg` where `id`='$insert_id' and `crt`!=''";
530 $res = mysql_query($query);
531 if(mysql_num_rows($res) <= 0)
533 echo _("Your certificate request has failed to be processed correctly, please try submitting it again.")."<br>\n";
534 echo _("If this is a re-occuring problem, please send a copy of the key you are trying to signed to support@cacert.org. Thank you.");
537 readfile(generatecertpath("crt","gpg",$insert_id));
545 if($oldid == 2 && array_key_exists('change',$_REQUEST) && $_REQUEST['change'] != "")
547 showheader(_("My CAcert.org Account!"));
548 foreach($_REQUEST as $id => $val)
550 if(substr($id,0,14)=="check_comment_")
552 $cid = intval(substr($id,14));
553 $comment=trim(mysql_real_escape_string(stripslashes($_REQUEST['comment_'.$cid])));
554 mysql_query("update `gpg` set `description`='$comment' where `id`='$cid' and `memid`='".$_SESSION['profile']['id']."'");
557 echo(_("Certificate settings have been changed.")."<br/>\n");
564 showheader(_("Welcome to CAcert.org"));
565 includeit($id, "gpg");