3 # Copyright (c) 1999-2006 Debian Admin Team Members and Developers (taken from debian/copyright in 2008 by weasel)
4 # Copyright (c) 2002, 2003, 2004, 2008 Peter Palfrader
9 use Date::Manip qw(ParseDate);
10 use Net::LDAP qw(LDAP_SUCCESS LDAP_PROTOCOL_ERROR);
13 my $blocksize = 8; # A blowfish block is 8 bytes
14 my $configfile = "/etc/userdir-ldap/userdir-ldap.conf";
15 #my $configfile = "/home/randolph/html/debian/perl/userdir-ldap.conf";
17 my %config = &ReadConfigFile;
20 eval 'use Crypt::Blowfish';
28 open (F, "</dev/urandom") || die &HTMLError("No /dev/urandom found!");
29 read(F, $input, $keysize); # key length is 8 bytes
36 # this can create either a DES type salt or a MD5 salt
37 # 0 for DES, 1 for MD5 salt and 2 for apache MD5 salt
39 my $validstr = './0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
40 my @valid = split(//,$validstr);
44 if (($type == 1)||($type == 2)) {
48 open (F, "</dev/urandom") || die &HTMLError("No /dev/urandom found!");
49 foreach (1..$cryptsaltlen) {
51 $out .= $valid[ord($in) % ($#valid + 1)];
55 if ($type == 1) { $md5 = "\$1\$$out\$"; }
60 # blowfish only encrypts things in blocks of 8 bytes, so we
61 # need a custom routine that handles longer strings....
66 # prepend a length byte */
67 $input = chr(length($input)).$input;
68 $input .= "\001" x ($blocksize - (length($input) % $blocksize)) if (length($input % $blocksize));
70 for ($pos = 0; $pos < length($input); $pos += $blocksize) {
71 $output .= unpack("H16", $cipher->encrypt(substr($input, $pos, $blocksize))) if ($hascryptix);
77 # like encrypt, needs to deal with big blocks. Note that we assume
78 # trailing spaces are unimportant.
81 my ($pos, $portion, $output, $len);
83 ((length($input) % $blocksize) == 0) || &HTMLError("Password corrupted"); # should always be true...
85 for ($pos = 0; $pos < length($input); $pos += $blocksize*2) {
86 $portion = pack("H16", substr($input, $pos, $blocksize*2));
87 $output .= $cipher->decrypt($portion) if ($hascryptix);
90 # check length byte, discard junk
91 $len = substr($output, 0, 1);
92 $output = substr($output, 1, ord($len));
96 sub SavePasswordToFile {
101 my $cryptuser = crypt($userid, &CreateCryptSalt);
102 my $secret = Encrypt($cipher, $password);
103 $cryptuser =~ y/\//_/; # translate slashes to underscores...
105 my $fn = "$config{authtokenpath}/$cryptuser";
106 open (F, ">$fn") || &HTMLError("$fn: $!");
114 sub ReadPasswordFromFile {
120 $userid =~ y/\//_/; # translate slashes to underscores...
122 # if we couldn't read the password file, assume user is unauthenticated. is this ok?
123 open (F, "<$config{authtokenpath}/$userid") || return undef;
124 chomp($passwd = <F>);
128 # check to make sure we read something
129 return undef if (!$passwd || !$time);
131 # check to make sure the time is positive, and that the auth token
133 my $tdiff = (time - $time);
134 &HTMLError("Your authentication token has expired. Please <a href=\"https://$ENV{SERVER_NAME}/$config{webloginhtml}\">relogin</a>") if (($tdiff < 0) || ($tdiff > $config{authexpires}));
136 return Decrypt($cipher, $passwd);
140 my ($id, $hrkey) = split(/,/, shift, 2);
141 return undef if (!$id || !$hrkey);
142 my $key = pack("H".(length($hrkey)), $hrkey);
143 my $cipher = new Crypt::Blowfish $key;
144 my $r = ReadPasswordFromFile($id, $cipher);
146 UpdateAuthToken("$id,$hrkey", $r);
148 ClearAuthToken("$id,$hrkey")
154 my ($id, $hrkey) = split(/,/, shift, 2);
155 $id =~ y/\//_/; # switch / to _
156 unlink "$config{authtokenpath}/$id" || &HTMLError("Error removing authtoken: $!");
159 sub UpdateAuthToken {
160 my ($id, $hrkey) = split(/,/, shift, 2);
161 my $password = shift;
162 my $key = pack("H".(length($hrkey)), $hrkey);
163 $id =~ y/\//_/; # switch / to _
164 my $cipher = new Crypt::Blowfish $key;
165 my $secret = Encrypt($cipher, $password);
167 my $fn = "$config{authtokenpath}/$id";
168 open (F, ">$fn") || &HTMLError("$fn: $!");
172 chmod 0600, "$fn" || &HTMLError("$fn: $!");
176 sub FormatFingerPrint {
180 if (length($in) == 32) {
182 $out .= substr($in, $_*2, 2)." ";
183 $out .= " " if ($_ == 7);
186 foreach (0..int(length($in)/4)) {
187 $out .= substr($in, $_*4, 4)." ";
195 my $fingerprint = shift;
196 my $signatures = shift;
197 my ($out, $keyringparam) = undef;
199 foreach (split(/:/, $config{keyrings})) {
200 $keyringparam .= "--keyring $_ ";
203 $fingerprint =~ s/\s//g;
204 $fingerprint = "0x".$fingerprint;
206 local $ENV{PATH} = '';
207 $/ = undef; # just suck it up ....
209 open(FP, "$config{gpg} --no-options --no-default-keyring $keyringparam --list-sigs --fingerprint $fingerprint|");
213 open(FP, "$config{gpg} --no-options --no-default-keyring $keyringparam --export -a $fingerprint|");
221 sub FormatTimestamp {
223 $in =~ /^(....)(..)(..)(..)(..)(..)/;
225 return sprintf("%04d/%02d/%02d %02d:%02d:%02d UTC", $1,$2,$3,$4,$5,$6);
230 # [Tue, 11 Jan 2000 02:37:18] "Joey Hess <joeyh@debian.org>" "<debian-boot@lists.debian.org> archive/latest/7130" "<20000110181924.H19910@kitenet.net>"
231 # [Mon, 10 Jan 2000 21:48:19] "9E1E 1052 F8BB A351 0606 5527 50BB 2974 2D59 A7D2" "<debian-devel-changes@lists.debian.org> archive/latest/58632" "<20000110200506.13257.qmail@master.debian.org>"
232 my $lastseenpgp = shift;
233 my $lastseenfrom = shift;
234 my ($d1, $d2, $lastseen);
236 return "<b>No activity detected</b>" if (!$lastseenpgp && !$lastseenfrom);
237 $lastseen = $lastseenpgp;
238 $lastseen = $lastseenfrom if (!$lastseenpgp);
240 if ($lastseenfrom && $lastseenpgp) {
241 ($d1) = ($lastseenpgp =~ /^\[(.+?)\]/); $d1 = ParseDate($d1);
242 ($d2) = ($lastseenfrom =~ /^\[(.+?)\]/); $d2 = ParseDate($d2);
243 $lastseen = (($d1 gt $d2) ? $lastseenpgp : $lastseenfrom);
246 my ($date,$user,$list,$msgid) = ($lastseen =~ /^\[(.+?)\]\s+"(.+?)"\s+"(?:<(.+?)>.*?|\-)"\s+"(<.+?>)"/);
247 $list = "on $list" if ($list);
248 $date = CGI::escapeHTML($date);
249 $user = CGI::escapeHTML($user);
250 $list = CGI::escapeHTML($list);
251 $msgid = CGI::escapeHTML($msgid);
252 return "$date $list<br> Message ID: $msgid";
257 my ($abbrev, $country);
258 open (F, $config{countrylist}) || return uc($in);
261 ($abbrev, $country) = split(/\s+/, $_, 2);
262 if ($abbrev eq $in) {
277 print "Content-type: text/html; charset=utf-8\n\n" if (!$htmlhdrsent);
282 &HTMLSendHeader if (!$htmlhdrsent);
292 my ($lat, $long) = @_;
294 $lat =~ s/[^-+\.\d]//g; $long =~ s/[^-+\.\d]//g;
296 if (($lat =~ /^(\-|\+?)\d+(\.\d+)?/) && ($long =~ /^(\-|\+?)\d+(\.\d+)?/)) {
297 return ($lat, $long);
306 my @names = $query->param;
308 foreach $key (@names) { # web security is a joke ... <sigh>
309 $_ = $query->param($key);
314 $query->param($key, $_);
327 $mesg = $ldap->modify($dn, delete => { $attr => [] });
329 $val = [ $val ] if (!ref($val));
330 $mesg = $ldap->modify($dn, replace => { $attr => $val });
331 $mesg->code && &Util::HTMLError("error updating $attr: ".$mesg->error);
338 # reads a config file and results a hashref with the results
339 my (%config, $attr, $setting);
340 open (F, "<$configfile") || &HTMLError("Cannot open $configfile: $!");
343 if ((!/^\s*#/) && ($_ ne "")) {
344 # Chop off any trailing comments
347 ($attr, $setting) = ($1, $2);
348 $setting =~ s/"//g; #"
350 $attr =~ s/^\s+//; $attr =~ s/\s+$//;
351 $setting =~ s/^\s+//; $setting =~ s/\s+$//;
352 $config{$attr} = $setting;
359 sub UpgradeConnection($) {
361 my $mesg = $ldap->start_tls(
363 cafile => '/etc/ssl/certs/spi-cacert-2008.pem'
366 if ($mesg->code != LDAP_SUCCESS) {
367 print "Content-type: text/html; charset=utf-8\n\n";
368 print "<html><body><h1>STARTTLS failed: ".$mesg->error."</h1></body></html>\n";
373 sub readwrite3($$$$) {
374 my ($in, $inputfd, $stdoutfd, $stderrfd) = @_;
376 #Echolot::Log::trace("Entering readwrite_gpg.");
378 local $INPUT_RECORD_SEPARATOR = undef;
379 my $sout = IO::Select->new();
380 my $sin = IO::Select->new();
383 #Echolot::Log::trace("input is $inputfd; output is $stdoutfd; err is $stderrfd; status is ".(defined $statusfd ? $statusfd : 'undef').".");
385 $inputfd->blocking(0);
386 $stdoutfd->blocking(0);
387 $stderrfd->blocking(0);
388 $sout->add($stdoutfd);
389 $sout->add($stderrfd);
392 my ($stdout, $stderr) = ("", "", "");
394 my ($readyr, $readyw);
395 while ($sout->count() > 0 || (defined($sin) && ($sin->count() > 0))) {
396 #Echolot::Log::trace("select waiting for ".($sout->count())." fds.");
397 ($readyr, $readyw, undef) = IO::Select::select($sout, $sin, undef, 42);
398 #Echolot::Log::trace("ready: write: ".(defined $readyw ? scalar @$readyw : 'none')."; read: ".(defined $readyr ? scalar @$readyr : 'none'));
399 for my $wfd (@$readyw) {
400 #Echolot::Log::trace("writing to $wfd.");
402 if ($offset != length($in)) {
403 $written = $wfd->syswrite($in, length($in) - $offset, $offset);
405 unless (defined ($written)) {
406 #Echolot::Log::warn("Error while writing to GnuPG: $!");
412 if ($offset == length($in)) {
413 #Echolot::Log::trace("writing to $wfd done.");
421 next unless (defined(@$readyr)); # Wait some more.
423 for my $rfd (@$readyr) {
425 #Echolot::Log::trace("reading from $rfd done.");
430 #Echolot::Log::trace("reading from $rfd.");
431 if ($rfd == $stdoutfd) {
435 if ($rfd == $stderrfd) {
441 #Echolot::Log::trace("readwrite_gpg done.");
442 return ($stdout, $stderr);
445 sub checkPasswordQuality($$$) {
446 my ($pw, $oldpw, $ldapelements) = @_;
447 my ($stdinR, $stdinW) = (IO::Handle->new(), IO::Handle->new());
448 my ($stdoutR, $stdoutW) = (IO::Handle->new(), IO::Handle->new());
449 my ($stderrR, $stderrW) = (IO::Handle->new(), IO::Handle->new());
450 pipe $stdinR, $stdinW;
451 pipe $stdoutR, $stdoutW;
452 pipe $stderrR, $stderrW;
455 unless (defined $pid) {
456 return (2, "Could not fork: $!");
458 unless ($pid) { # child
465 open (STDIN, "<&".$stdinR->fileno) or warn ("Cannot dup stdinR (fd ".$stdinR->fileno.") as STDIN: $!");
466 open (STDOUT, ">&".$stdoutW->fileno) or warn ("Cannot dup stdoutW (fd ".$stdoutW->fileno.") as STDOUT: $!");
467 open (STDERR, ">&".$stderrW->fileno) or warn ("Cannot dup stderrW (fd ".$stderrW->fileno.") as STDERR: $!");
468 { exec('/usr/lib/userdir-ldap-cgi/password-qualify-check'); }
469 $stderrW->print("Could not exec password-qualify-check: $!\n");
476 $oldpw = '' unless defined $oldpw;
477 my $out = join("\n", $pw, $oldpw, @$ldapelements)."\n";
478 my ($stdout, $stderr) = readwrite3($out, $stdinW, $stdoutR, $stderrR);
481 my $exitcode = $? >> 8;
482 if ($exitcode == 0 && $stdout eq '' && $stderr eq '') {
484 } elsif ($exitcode == 1 && $stderr eq '') {
487 return (2, "check exited with exit code $exitcode, said '$stdout' on stdout, and '$stderr' on stderr.");