3 # CommModule - CAcert Communication Module
4 # Copyright (C) 2006-2009 CAcert Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; version 2 of the License.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 # Production Client / CommModule
23 use Device
::SerialPort
qw( :PARAM
:STAT
0.07 );
26 use Time
::HiRes
q(usleep
);
27 use File
::CounterFile
;
34 use Digest
::SHA1
qw(sha1_hex
);
43 #my $serialport="/dev/ttyS0";
44 my $serialport="/dev/ttyUSB0";
46 my $gpgbin="/usr/bin/gpg";
48 my $opensslbin="/usr/bin/openssl";
51 my $mysqlphp="/home/cacert/www/includes/mysql.php";
53 my %revokefile=(2=>"../www/class3-revoke.crl",1=>"../www/revoke.crl");
57 #End of configurations
59 ########################################################
62 my %monarr = ("Jan" => 1, "Feb" => 2, "Mar" => 3, "Apr" => 4, "May" => 5, "Jun" => 6, "Jul" => 7, "Aug" => 8, "Sep" => 9, "Oct" => 10, "Nov" => 11, "Dec" => 12);
66 if(open IN
,"<$mysqlphp")
71 $password=$1 if($content=~m/mysql_connect\s*\("[^"]+",\s*"\w+",\s*"(\w+)"/);
78 die "Could not read file: $!\n";
82 my $dbh = DBI
->connect("DBI:mysql:cacert:localhost","cacert",$password, { RaiseError
=> 1, AutoCommit
=> 1 }) || die ("Error with the database connection.\n");
102 return if(not defined($_[0]));
103 my $timestamp = strftime
("%Y-%m-%d %H:%M:%S", localtime);
104 my $currdate = substr($timestamp, 0, 10);
105 if ($lastdate ne $currdate) {
106 close LOG
if ($lastdate ne "");
107 $lastdate = $currdate;
108 open LOG
,">>logfile$lastdate.txt";
110 print LOG
"$timestamp $_[0]";
124 my $timestamp=strftime
("%Y-%m-%d %H:%M:%S",localtime);
126 #mkdir "revokehashes";
127 foreach (keys %revokefile)
129 next unless (-f
$revokefile{$_});
130 my $revokehash=sha1_hex
(readfile
($revokefile{$_}));
131 SysLog
"Root $_: Hash $revokefile{$_} = $revokehash\n";
151 $new=~s/['"\\]/\\$1/g;
162 SysLog
("Opening Serial interface:\n");
163 sub SerialSettings
($)
166 if(!defined($PortObj))
168 Error
"Could not open Serial Port!\n" ;
172 $PortObj->baudrate(115200);
173 $PortObj->parity("none");
174 $PortObj->databits(8);
175 $PortObj->stopbits(1);
179 #We have to open the SerialPort and close it again, so that we can bind it to a Handle
180 if(! -f
"serial.conf")
182 my $PortObj = new Device
::SerialPort
($serialport);
183 SerialSettings
($PortObj);
184 $PortObj->save("serial.conf");
188 my $PortObj = tie
(*SER
, 'Device::SerialPort', "serial.conf") || Error
"Can't tie using Configuration_File_Name: $!\n";
190 Error
"Could not open Serial Interface!\n" if(not defined($PortObj));
191 SerialSettings
($PortObj);
192 #open SER,">$serialport";
194 SysLog
("Serial interface opened: $PortObj\n");
196 my $sel = new IO
::Select
( \
*SER
);
200 #Hexdump function: Returns the hexdump representation of a string
203 return "" if(not defined($_[0]));
205 $content.=sprintf("%02X ",unpack("C",substr($_[0],$_,1))) foreach (0 .. length($_[0])-1);
209 #pack3 packs together the length of the data in 3 bytes and the data itself, size limited to 16MB. In case the data is more than 16 MB, it is ignored, and a 0 Byte block is transferred
212 return "\x00\x00\x00" if(!defined($_[0]));
213 my $data=(length($_[0]) >= 2**24)?
"":$_[0];
214 my $len=pack("N",length($data));
215 SysLog
"len: ".length($data)."\n" if($debug);
216 return substr($len,1,3).$data;
220 #unpack3 unpacks packed data.
223 return undef if((not defined($_[0])) or length($_[0])<3);
224 #SysLog "hexdump: ".hexdump("\x00".substr($_[0],0,3))."\n";
225 my $len=unpack("N","\x00".substr($_[0],0,3));
226 #SysLog "len3: $len length(): ".length($_[0])." length()-3: ".(length($_[0])-3)."\n";
227 return undef if(length($_[0])-3 != $len);
228 return substr($_[0],3);
232 #unpack3array extracts a whole array of concatented pack3ed data.
236 if((not defined($_[0])) or length($_[0])<3)
238 SysLog
"Begin of structure corrupt\n";
242 while(length($dataleft)>=3)
244 #SysLog "hexdump: ".hexdump("\x00".substr($dataleft,0,3))."\n";
245 my $len=unpack("N","\x00".substr($dataleft,0,3));
246 #SysLog "len3: $len length(): ".length($dataleft)." length()-3: ".(length($dataleft)-3)."\n";
247 if(length($dataleft)-3 < $len)
249 SysLog
"Structure cut off\n";
252 push @retarr, substr($dataleft,3,$len);
253 $dataleft=substr($dataleft,3+$len);
255 if(length($dataleft)!=0)
257 SysLog
"End of structure cut off\n";
264 #Raw send function over the Serial Interface (+debugging)
267 return unless defined($_[0]);
268 SysLog
"Sending ".length($_[0])."\n"; #hexdump($_[0])."\n" if($debug);
275 my $iwrote=scalar($PortObj->write(substr($data,0,$mtu)))||0;
276 #usleep(270*$iwrote+9000); # On Linux, we have to wait to make sure it is being sent, and we dont loose any data.
278 $data=substr($data,$iwrote);
280 print "i wrote: $iwrote total: $total left: ".length($data)."\n" if(!($runcount++ %10));
283 SysLog
"Sent message.\n" if($debug);
284 # print "Sending ".length($_[0])."\n"; #hexdump($_[0])."\n";
285 # foreach(0 .. length($_[0]))
287 # $PortObj->write(substr($_[0],$_,1));
297 #Send data over the Serial Interface with handshaking:
298 sub SendHandshaked
($)
300 SysLog
"Shaking hands ...\n" if($debug);
303 Error
"Handshake uncompleted. Connection lost2! $!\n" if(!scalar($sel->can_read(20)));
305 my $length=read SER
,$data,1;
306 if($length && $data eq "\x10")
310 foreach(0 .. length($_[0])-1)
312 #print "xor mit ".unpack("C",substr($_[0],$_,1))."\n";
313 $xor ^= unpack("C",substr($_[0],$_,1));
315 #print "XOR: $xor\n";
320 SendIt
($_[0].pack("C",$xor)."rie4Ech7");
322 Error
"Packet receipt was not confirmed in 5 seconds. Connection lost!\n" if(!scalar($sel->can_read(5)));
325 $length=read SER
,$data,1;
327 if($length && $data eq "\x10")
329 SysLog
"Sent successfully!...\n";
332 elsif($length && $data eq "\x11")
338 Error
"I cannot send! $length ".unpack("C",$data)."\n";
345 print "!Cannot send! $length \n";
346 Error
"!Stopped sending.\n";
355 my @ready = $sel->can_read(120);
357 my $length=read SER
,$data,1,0;
359 #SysLog "Data: ".hexdump($data)."\n";
364 SysLog
"Start received, sending OK\n" if($debug);
371 while(!$blockfinished)
373 Error
("Tried reading too often\n") if(($tries--)<=0);
374 # SysLog ("tries: $tries") if(!($tries%10));
377 if(!scalar($sel->can_read(5)))
379 Error
"Handshake uncompleted. Connection lost variant3! $!\n" ;
382 $length=read SER
,$data,100,0;
387 #SysLog("Received: $length ".length($block)."\n");
388 $blockfinished=defined(unpack3
(substr($block,0,-9)))?
1:0;
390 if(!$blockfinished and substr($block,-8,8) eq "rie4Ech7")
392 SysLog
"BROKEN Block detected!\n";
400 SysLog
"Block done: ".hexdump
($block)."\n" if($debug);
406 Error
("Error: No Answer received, Timeout.\n") if(length($data)==0);
407 Error
("Error: Wrong Startbyte: ".hexdump
($data)." !\n");
410 SysLog
"Waiting on next request ...\n";
416 # @result(Version,Action,Errorcode,Response)=Request(Version=1,Action=1,System=1,Root=1,Configuration="...",Parameter="...",Request="...");
417 sub Request
($$$$$$$$$$$)
419 SysLog
"Version: $_[0] Action: $_[1] System: $_[2] Root: $_[3] Config: $_[4]\n";
421 SendHandshaked
(pack3
(pack3
(pack("C*",$_[0],$_[1],$_[2],$_[3],$_[4],$_[5],$_[6]>>8,$_[6]&255,$_[7])).pack3
($_[8]).pack3
($_[9]).pack3
($_[10])));
423 my @fields=unpack3array
(substr($data,3,-9));
425 SysLog
"Answer from Server: ".hexdump
($data)."\n" if($debug);
427 #if(open OUT,">result.dat")
434 # SysLog "Could not write result: $!\n";
444 my @sum = $dbh->selectrow_array("select sum(`points`) as `total` from `notary` where `to`='".$_[0]."' group by `to`");
445 SysLog
("Summe: $sum[0]\n") if($debug);
447 return ($sum[0]>=50)?
730:180;
452 sub X509extractSAN
($)
454 my @bits = split("/", $_[0]);
457 foreach my $val(@bits)
459 my @bit=split("=",$val);
460 if($bit[0] eq "subjectAltName")
462 $SAN.="," if($SAN ne "");
463 $SAN.= trim
($bit[1]);
467 $newsubject .= "/".$val;
470 $newsubject=~s{^//}{/};
471 $newsubject=~s/[\n\r\t\x00"\\']//g;
472 $SAN=~s/[ \n\r\t\x00"\\']//g;
473 return($SAN,$newsubject);
476 sub X509extractExpiryDate
($)
479 my $data=`$opensslbin x509 -in "$_[0]" -noout -enddate`;
481 #notAfter=Aug 8 10:26:34 2007 GMT
482 if($data=~m/notAfter=(\w{2,4}) *(\d{1,2}) *(\d{1,2}:\d{1,2}:\d{1,2}) (\d{4}) GMT/)
484 my $date="$4-".$monarr{$1}."-$2 $3";
485 SysLog
"Expiry Date found: $date\n" if($debug);
490 SysLog
"Expiry Date not found: $data\n";
497 return 0 unless(-f
$_[0]);
498 my $data=`$opensslbin crl -in "$_[0]" -noout -lastupdate -inform der`;
499 SysLog
"CRL: $data\n";
500 #lastUpdate=Aug 8 10:26:34 2007 GMT
501 # Is the timezone handled properly?
502 if($data=~m/lastUpdate=(\w{2,4}) *(\d{1,2}) *(\d{1,2}:\d{1,2}:\d{1,2}) (\d{4}) GMT/)
504 my $date=sprintf("%04d-%02d-%02d",$4,$monarr{$1},$2);
505 SysLog
"CRL Issueing Date found: $date\n" if($debug);
506 my $compare = strftime
("%Y-%m-%d", localtime);
507 SysLog
"Comparing $date with $compare\n" if($debug);
508 return $date eq $compare;
512 SysLog
"Expiry Date not found. Perhaps DER format is necessary? Hint: $data\n";
518 sub X509extractSerialNumber
($)
521 my $data=`$opensslbin x509 -in "$_[0]" -noout -serial`;
522 if($data=~m/serial=([0-9A-F]+)/)
529 sub OpenPGPextractExpiryDate
($)
535 open(RGPG
, $gpgbin.' -vv '.$_[0].' 2>&1 |') or Error
('Can\'t start GnuPG($gpgbin): '.$!."\n");
536 open(OUT
, '> infogpg.txt' ) or Error
('Can\'t open output file: infogpg.txt: '.$!);
543 if ( /^\s*version \d+, created (\d+), md5len 0, sigclass (?:0x[0-9a-fA-F]+|\d+)\s*$/ )
545 SysLog
"Detected CTS: $1\n";
547 } elsif ( /^\s*critical hashed subpkt \d+ len \d+ \(sig expires after ((\d+)y)?((\d+)d)?((\d+)h)?(\d+)m\)\s*$/ )
549 SysLog
"Detected FRAME $2 $4 $6 $8\n";
550 $cts += $2 * 31536000; # secs per year (60 * 60 * 24 * 365)
551 $cts += $4 * 86400; # secs per day (60 * 60 * 24)
552 $cts += $6 * 3600; # secs per hour (60 * 60)
553 $cts += $8 * 60; # secs per min (60)
558 SysLog
"Detected VERSION\n";
566 SysLog
"CTS: $cts R: $r\n";
571 $r = sprintf('%.4i-%.2i-%.2i %.2i:%.2i:%.2i', # date format
572 $date[5] + 1900, $date[4] + 1, $date[3], # day
573 $date[2], $date[1], $date[0], # time
581 #sub OpenPGPextractExpiryDate($)
583 # my $data=`$gpgbin -v $_[0]`;
584 # open OUT,">infogpg.txt";
587 # if($data=~m/^sig\s+[0-9A-F]{8} (\d{4}-\d\d-\d\d) [^\[]/)
589 # return "$1 00:00:00";
595 # Sets the locale according to the users preferred language
596 sub setUsersLanguage
($)
599 print "Searching for the language of the user $_[0]\n";
600 my @a=$dbh->selectrow_array("select language from users where id='".int($_[0])."'");
601 $lang = $1 if($a[0]=~m/(\w+_[\w.@]+)/);
603 SysLog
"The users preferred language: $lang\n";
608 setlocale
(LC_ALL
, $lang);
610 $ENV{"LANG"}="en_AU";
611 setlocale
(LC_ALL
, "en_AU");
618 return() unless($_[0]=~m/^\d+$/);
619 my $sth = $dbh->prepare("select * from users where id='$_[0]'");
621 #SysLog "USER DUMP:\n";
622 while ( my $rowdata = $sth->fetchrow_hashref() )
625 #foreach (sort keys %tmp)
627 #SysLog " $_ -> $tmp{$_}\n";
637 return gettext
($_[0]);
640 sub sendmail
($$$$$$$)
642 my ($to, $subject, $message, $from, $replyto, $toname, $fromname)=@_;
643 my $errorsto="returns\@cacert.org";
647 # sendmail($user{email}, "[CAcert.org] Your GPG/PGP Key", $body, "support\@cacert.org", "", "", "CAcert Support");
648 my @lines=split("\n",$message);
650 foreach my $line (@lines)
658 $message .= $line."\n";
662 $fromname = $from if($fromname eq "");
664 my @bits = split(",", $from);
665 $from = addslashes
($bits['0']);
666 $fromname = addslashes
($fromname);
668 my $smtp = IO
::Socket
::INET
->new(PeerAddr
=> 'localhost:25');
670 SysLog
"SMTP: ".<$smtp>;
671 print $smtp "HELO hlin.cacert.org\r\n";
672 SysLog
"SMTP: ".<$smtp>;
673 print $smtp "MAIL FROM:<returns\@cacert.org>\r\n";
674 SysLog
"MAIL FROM: ".<$smtp>;
676 @bits = split(",", $to);
677 foreach my $user (@bits)
679 print $smtp "RCPT TO:<".trim
($user).">\r\n";
680 SysLog
"RCPT TO: ".<$smtp>;
682 print $smtp "DATA\r\n";
683 SysLog
"DATA: ".<$smtp>;
685 print $smtp "X-Mailer: CAcert.org Website\r\n";
686 print $smtp "X-OriginatingIP: ".$ENV{"REMOTE_ADDR"}."\r\n";
687 print $smtp "Sender: $errorsto\r\n";
688 print $smtp "Errors-To: $errorsto\r\n";
691 print $smtp "Reply-To: $replyto\r\n";
695 print $smtp "Reply-To: $from\r\n";
697 print $smtp "From: $from ($fromname)\r\n";
698 print $smtp "To: $to\r\n";
699 my $newsubj=encode_base64
(recode
("html..utf-8", trim
($subject)));
700 #SysLog("NewSubj: --".$newsubj."--\n") if($debug);
702 #SysLog("NewSubj: --".$newsubj."--\n") if($debug);
703 print $smtp trim
($subject)=~m/[^a-zA-Z0-9 ,.\[\]\/-]/?
"Subject: =?utf-8?B?$newsubj?=\r\n":"Subject: $subject\r\n";
704 print $smtp "Mime-Version: 1.0\r\n";
707 print $smtp "Content-Type: text/plain; charset=\"utf-8\"\r\n";
708 print $smtp "Content-Transfer-Encoding: 8bit\r\n";
712 print $smtp "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
713 print $smtp "Content-Transfer-Encoding: quoted-printable\r\n";
714 print $smtp "Content-Disposition: inline\r\n";
716 # print $smtp "Content-Transfer-Encoding: BASE64\r\n";
718 # print $smtp chunk_split(encode_base64(recode("html..utf-8", $message)))."\r\n.\r\n";
719 print $smtp recode
("html..utf-8", $message)."\r\n.\r\n";
720 SysLog
"ENDOFTEXT: ".<$smtp>;
721 print $smtp "QUIT\n";
722 SysLog
"QUIT: ".<$smtp>;
729 my $org=$_[0]?
"org":"";
733 my $table=$org.($server?
"domaincerts":"emailcerts");
735 SysLog
"HandleCerts $table\n";
737 my $sth = $dbh->prepare("select * from $table where crt_name='' and csr_name!='' and warning<3");
740 while ( my $rowdata = $sth->fetchrow_hashref() )
743 my $prefix=$org.($server?
"server":"client");
744 my $short=int($row{'id'}/1000);
745 my $csrname = "../csr/$prefix-".$row{'id'}.".csr";
746 $csrname = "../csr/$prefix/$short/$prefix-".$row{'id'}.".csr" if($newlayout);
747 SysLog
("New Layout: "."../csr/$prefix/$short/$prefix-".$row{'id'}.".csr\n");
749 #my $crtname = "../crt/$prefix-".$row{'id'}.".crt";
750 my $crtname=$csrname; $crtname=~s/^\.\.\/csr/..\
/crt/; $crtname=~s/\.csr$/.crt/;
751 my $dirname=$crtname; $dirname=~s/\/[^\/]*\
.crt
//;
753 SysLog
("New Layout: $crtname\n");
757 #Weird SQL structure ...
758 my @sqlres=$dbh->selectrow_array("select memid from domains where id='".int($row{'domid'})."'");
759 $row{'memid'}=$sqlres[0];
760 SysLog
("Fetched memid: $row{'memid'}\n") if($debug);
763 SysLog
"Opening $csrname\n";
770 # "1"=>"client-org.cnf",
771 # "2"=>"client-codesign.cnf",
772 # "3"=>"client-machine.cnf",
773 # "4"=>"client-ads.cnf",
775 # "6"=>"server-org.cnf",
776 # "7"=>"server-jabber.cnf",
777 # "8"=>"server-ocsp.cnf",
778 # "9"=>"server-timestamp.cnf",
783 if($row{"type"} =~ m/^(8|9)$/)
785 $profile=$row{"type"};
822 if(open(IN
,"<$csrname"))
827 SysLog
"Read $csrname.\n" if($debug);
828 SysLog
"Subject: --$row{'subject'}--\n" if($debug);
830 my ($SAN,$subject)=X509extractSAN
($row{'subject'});
831 SysLog
"Subject: --$subject--\n" if($debug);
832 SysLog
"SAN: --$SAN--\n" if($debug);
833 SysLog
"memid: $row{'memid'}\n" if($debug);
835 my $days=$org?
($server?
(365*2):365):calculateDays
($row{"memid"});
838 $crt=Request
($ver,1,1,$row{'rootcert'}-1,$profile,$row{'md'}eq"sha1"?
2:0,$days,$row{'keytype'}eq"NS"?
1:0,$content,$SAN,$subject);
841 if($crt=~m/^-----BEGIN CERTIFICATE-----/)
843 open OUT
,">$crtname";
849 open OUT
,">$crtname.der";
852 system "$opensslbin x509 -in $crtname.der -inform der -out $crtname";
857 SysLog
"ZERO Length certificate received.\n";
862 print "Error: $! Konnte $csrname nicht laden\n";
869 SysLog
"Opening $crtname\n";
871 my $date=X509extractExpiryDate
($crtname);
872 my $serial=X509extractSerialNumber
($crtname);
874 setUsersLanguage
($row{memid
});
876 my %user=getUserData
($row{memid
});
878 foreach (sort keys %user)
880 SysLog
" $_ -> $user{$_}\n" if($debug);
883 SysLog
("update `$table` set `crt_name`='$crtname', modified=now(), serial='$serial', `expire`='$date' where `id`='".$row{'id'}."'\n");
885 $dbh->do("update `$table` set `crt_name`='$crtname', modified=now(), serial='$serial', `expire`='$date' where `id`='".$row{'id'}."'");
887 my $body = _
("Hi")." $user{fname},\n\n";
888 $body .= sprintf(_
("You can collect your certificate for %s by going to the following location:")."\n\n", $row{'email'}.$row{'CN'});
889 $body .= "https://www.cacert.org/account.php?id=".($server?
"15":"6")."&cert=$row{id}\n\n";
890 $body .= _
("If you have not imported CAcert´s root certificate, please go to:")."\n";
891 $body .= "https://www.cacert.org/index.php?id=3\n";
892 $body .= "Root cert fingerprint = A6:1B:37:5E:39:0D:9C:36:54:EE:BD:20:31:46:1F:6B\n";
893 $body .= "Root cert fingerprint = 135C EC36 F49C B8E9 3B1A B270 CD80 8846 76CE 8F33\n\n";
894 $body .= _
("Best regards")."\n"._
("CAcert.org Support!")."\n\n";
895 sendmail
($user{email
}, "[CAcert.org] "._
("Your certificate"), $body, "support\@cacert.org", "", "", "CAcert Support");
899 SysLog
("Could not find the issued certificate. $crtname ".$row{"id"}."\n");
900 $dbh->do("update `$table` set warning=warning+1 where `id`='".$row{'id'}."'");
913 if($crl=~m/^-----BEGIN X509 CRL-----/)
915 open OUT
,">$crlname.pem";
918 system "$opensslbin crl -in $crlname.pem -outform der -out $crlname.tmp";
922 open OUT
,">$crlname.patch";
925 my $res=system "xdelta patch $crlname.patch $crlname $crlname.tmp";
926 #print "xdelta res: $res\n";
929 open OUT
,">$crlname.tmp";
935 my $res=`openssl crl -verify -in $crlname.tmp -inform der -noout 2>&1`;
936 SysLog
"verify: $res\n";
937 if($res=~m/verify OK/)
939 rename "$crlname.tmp","$crlname";
943 SysLog
"VERIFICATION OF NEW CRL DID NOT SUCCEED! PLEASE REPAIR!\n";
944 SysLog
"Broken CRL is available as $crlname.tmp\n";
945 #Override for testing:
946 rename "$crlname.tmp","$crlname";
952 SysLog
("RECEIVED AN EMPTY CRL!\n");
960 foreach my $rootcert (keys %revokefile)
962 if(!CRLuptodate
($revokefile{$rootcert}))
964 SysLog
"Update of the CRL $rootcert is necessary!\n";
965 my $crlname = $revokefile{$rootcert};
966 my $revokehash=sha1_hex
(readfile
($crlname));
967 my $crl=Request
($ver,2,1,$rootcert-1,0,0,365,0,"","",$revokehash);
968 #print "Received ".length($crl)." ".hexdump($crl)."\n";
969 DoCRL
($crl,$crlname);
977 my $org=$_[0]?
"org":"";
980 my $table=$org.($server?
"domaincerts":"emailcerts");
982 my $sth = $dbh->prepare("select * from $table where revoked='1970-01-01 10:00:01'"); # WHICH TIMEZONE?
985 while ( my $rowdata = $sth->fetchrow_hashref() )
989 my $prefix=$org.($server?
"server":"client");
990 my $short=int($row{'id'}/1000);
992 my $csrname = "../csr/$prefix-".$row{'id'}.".csr";
993 $csrname = "../csr/$prefix/$short/$prefix-".$row{'id'}.".csr" if($newlayout);
994 SysLog
("New Layout: "."../csr/$prefix/$short/$prefix-".$row{'id'}.".csr\n");
996 #my $crtname = "../crt/$prefix-".$row{'id'}.".crt";
997 my $crtname=$csrname; $crtname=~s/^\.\.\/csr/..\
/crt/; $crtname=~s/\.csr$/.crt/;
998 SysLog
("New Layout: $crtname\n");
1000 #my $csrname = "../csr/".$org.($server?"server-":"client-").$row{'id'}.".csr";
1001 #my $crtname = "../crt/".$org.($server?"server-":"client-").$row{'id'}.".crt";
1002 my $crlname = $revokefile{$row{'rootcert'}};
1007 if(open(IN
,"<$crtname"))
1012 my $revokehash=sha1_hex
(readfile
($crlname));
1014 my $crl=Request
($ver,2,1,$row{'rootcert'}-1,0,0,365,0,$content,"",$revokehash);
1015 my $result=DoCRL
($crl,$crlname);
1019 setUsersLanguage
($row{memid
});
1021 my %user=getUserData
($row{memid
});
1023 $dbh->do("update `$table` set `revoked`=now() where `id`='".$row{'id'}."'");
1025 my $body = _
("Hi")." $user{fname},\n\n";
1026 $body .= sprintf(_
("Your certificate for %s has been revoked, as per request.")."\n\n", $row{'CN'});
1027 $body .= _
("Best regards")."\n"._
("CAcert.org Support!")."\n\n";
1028 SysLog
("Sending email to ".$user{"email"}."\n") if($debug);
1029 sendmail
($user{email
}, "[CAcert.org] "._
("Your certificate"), $body, "support\@cacert.org", "", "", "CAcert Support");
1035 SysLog
("Error in RevokeCerts: $crtname $!\n") if($debug);
1047 my $sth = $dbh->prepare("select * from gpg where crt='' and csr!='' ");
1050 while ( $rowdata = $sth->fetchrow_hashref() )
1052 my %row=%{$rowdata};
1055 my $short=int($row{'id'}/1000);
1056 my $csrname = "../csr/$prefix-".$row{'id'}.".csr";
1057 $csrname = "../csr/$prefix/$short/$prefix-".$row{'id'}.".csr" if($newlayout);
1058 SysLog
("New Layout: "."../csr/$prefix/$short/$prefix-".$row{'id'}.".csr\n");
1060 #my $crtname = "../crt/$prefix-".$row{'id'}.".crt";
1061 my $crtname=$csrname; $crtname=~s/^\.\.\/csr/..\
/crt/; $crtname=~s/\.csr$/.crt/;
1062 SysLog
("New Layout: $crtname\n");
1065 #my $csrname = "../csr/gpg-".$row{'id'}.".csr";
1066 #my $crtname = "../crt/gpg-".$row{'id'}.".crt";
1068 SysLog
"Opening $csrname\n";
1072 if(-s
$csrname && open(IN
,"<$csrname"))
1077 SysLog
"Read $csrname.\n";
1078 $crt=Request
($ver,1,2,0,0,2,366,0,$content,"","");
1081 open OUT
,">$crtname";
1089 #Error("Error: $!\n");
1095 SysLog
"Opening $crtname\n";
1096 setUsersLanguage
($row{memid
});
1098 my $date=OpenPGPextractExpiryDate
($crtname);
1099 my %user=getUserData
($row{memid
});
1101 $dbh->do("update `gpg` set `crt`='$crtname', issued=now(), `expire`='$date' where `id`='".$row{'id'}."'");
1103 my $body = _
("Hi")." $user{fname},\n\n";
1104 $body .= sprintf(_
("Your CAcert signed key for %s is available online at:")."\n\n", $row{'email'});
1105 $body .= "https://www.cacert.org/gpg.php?id=3&cert=$row{id}\n\n";
1106 $body .= _
("To help improve the trust of CAcert in general, it's appreciated if you could also sign our key and upload it to a key server. Below is a copy of our primary key details:")."\n\n";
1107 $body .= "pub 1024D/65D0FD58 2003-07-11 CA Cert Signing Authority (Root CA) <gpg\@cacert.org>\n";
1108 $body .= "Key fingerprint = A31D 4F81 EF4E BD07 B456 FA04 D2BB 0D01 65D0 FD58\n\n";
1109 $body .= _
("Best regards")."\n"._
("CAcert.org Support!")."\n\n";
1110 sendmail
($user{email
}, "[CAcert.org] Your GPG/PGP Key", $body, "support\@cacert.org", "", "", "CAcert Support");
1112 SysLog
("Could not find the issued gpg key. ".$row{"id"}."\n");
1113 #$dbh->do("delete from `gpg` where `id`='".$row{'id'}."'");
1123 while ( -f
"./client.pl-active" )
1125 SysLog
("Handling GPG database ...\n");
1127 SysLog
("Issueing certs ...\n");
1128 HandleCerts
(0,0); #personal client certs
1129 HandleCerts
(0,1); #personal server certs
1130 HandleCerts
(1,0); #org client certs
1131 HandleCerts
(1,1); #org server certs
1132 SysLog
("Revoking certs ...\n");
1133 RevokeCerts
(0,0); #personal client certs
1134 RevokeCerts
(0,1); #personal server certs
1135 RevokeCerts
(1,0); #org client certs
1136 RevokeCerts
(1,1); #org server certs
1139 RefreshCRLs
() if(($crlcheck%100) == 1);
1141 #print "Sign Request X.509, Root0\n";
1143 #Request($ver,1,1,0,5,2,365,0,$reqcontent,"","/CN=supertest.cacert.at");
1145 SysLog
("NUL Request:\n");
1146 my $timestamp=strftime
("%m%d%H%M%Y.%S",gmtime);
1147 Request
($ver,0,0,0,0,0,0,0,$timestamp,"","");