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(:all);
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 my $md5 = shift; # do we want a MD5 salt?
38 my $validstr = './0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
39 my @valid = split(//,$validstr);
42 my $cryptsaltlen = ($md5 ? 8 : 2);
44 open (F, "</dev/urandom") || die &HTMLError("No /dev/urandom found!");
45 foreach (1..$cryptsaltlen) {
47 $out .= $valid[ord($in) % ($#valid + 1)];
50 return ($md5 ? "\$1\$$out\$" : $out);
54 # blowfish only encrypts things in blocks of 8 bytes, so we
55 # need a custom routine that handles longer strings....
60 # prepend a length byte */
61 $input = chr(length($input)).$input;
62 $input .= "\001" x ($blocksize - (length($input) % $blocksize)) if (length($input % $blocksize));
64 for ($pos = 0; $pos < length($input); $pos += $blocksize) {
65 $output .= unpack("H16", $cipher->encrypt(substr($input, $pos, $blocksize))) if ($hascryptix);
71 # like encrypt, needs to deal with big blocks. Note that we assume
72 # trailing spaces are unimportant.
75 my ($pos, $portion, $output, $len);
77 ((length($input) % $blocksize) == 0) || &HTMLError("Password corrupted"); # should always be true...
79 for ($pos = 0; $pos < length($input); $pos += $blocksize*2) {
80 $portion = pack("H16", substr($input, $pos, $blocksize*2));
81 $output .= $cipher->decrypt($portion) if ($hascryptix);
84 # check length byte, discard junk
85 $len = substr($output, 0, 1);
86 $output = substr($output, 1, ord($len));
90 sub SavePasswordToFile {
95 my $cryptuser = crypt($userid, &CreateCryptSalt);
96 my $secret = Encrypt($cipher, $password);
97 $cryptuser =~ y/\//_/; # translate slashes to underscores...
99 my $fn = "$config{authtokenpath}/$cryptuser";
100 open (F, ">$fn") || &HTMLError("$fn: $!");
108 sub ReadPasswordFromFile {
114 $userid =~ y/\//_/; # translate slashes to underscores...
116 # if we couldn't read the password file, assume user is unauthenticated. is this ok?
117 open (F, "<$config{authtokenpath}/$userid") || return undef;
118 chomp($passwd = <F>);
122 # check to make sure we read something
123 return undef if (!$passwd || !$time);
125 # check to make sure the time is positive, and that the auth token
127 my $tdiff = (time - $time);
128 &HTMLError("Your authentication token has expired. Please <a href=\"https://$ENV{SERVER_NAME}/$config{webloginhtml}\">relogin</a>") if (($tdiff < 0) || ($tdiff > $config{authexpires}));
130 return Decrypt($cipher, $passwd);
134 my ($id, $hrkey) = split(/,/, shift, 2);
135 return undef if (!$id || !$hrkey);
136 my $key = pack("H".(length($hrkey)), $hrkey);
137 my $cipher = new Crypt::Blowfish $key;
138 my $r = ReadPasswordFromFile($id, $cipher);
140 UpdateAuthToken("$id,$hrkey", $r);
142 ClearAuthToken("$id,$hrkey")
148 my ($id, $hrkey) = split(/,/, shift, 2);
149 $id =~ y/\//_/; # switch / to _
150 unlink "$config{authtokenpath}/$id" || &HTMLError("Error removing authtoken: $!");
153 sub UpdateAuthToken {
154 my ($id, $hrkey) = split(/,/, shift, 2);
155 my $password = shift;
156 my $key = pack("H".(length($hrkey)), $hrkey);
157 $id =~ y/\//_/; # switch / to _
158 my $cipher = new Crypt::Blowfish $key;
159 my $secret = Encrypt($cipher, $password);
161 my $fn = "$config{authtokenpath}/$id";
162 open (F, ">$fn") || &HTMLError("$fn: $!");
166 chmod 0600, "$fn" || &HTMLError("$fn: $!");
170 sub FormatFingerPrint {
174 if (length($in) == 32) {
176 $out .= substr($in, $_*2, 2)." ";
177 $out .= " " if ($_ == 7);
180 foreach (0..int(length($in)/4)) {
181 $out .= substr($in, $_*4, 4)." ";
189 my $fingerprint = shift;
190 my $signatures = shift;
191 my ($out, $keyringparam) = undef;
193 foreach (split(/:/, $config{keyrings})) {
194 $keyringparam .= "--keyring $_ ";
197 $fingerprint =~ s/\s//g;
198 $fingerprint = "0x".$fingerprint;
200 local $ENV{PATH} = '';
201 $/ = undef; # just suck it up ....
203 open(FP, "$config{gpg} --no-options --no-default-keyring $keyringparam --list-sigs --fingerprint $fingerprint|");
207 open(FP, "$config{gpg} --no-options --no-default-keyring $keyringparam --export -a $fingerprint|");
215 sub FormatTimestamp {
217 $in =~ /^(....)(..)(..)(..)(..)(..)/;
219 return sprintf("%04d/%02d/%02d %02d:%02d:%02d UTC", $1,$2,$3,$4,$5,$6);
224 # [Tue, 11 Jan 2000 02:37:18] "Joey Hess <joeyh@debian.org>" "<debian-boot@lists.debian.org> archive/latest/7130" "<20000110181924.H19910@kitenet.net>"
225 # [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>"
226 my $lastseenpgp = shift;
227 my $lastseenfrom = shift;
228 my ($d1, $d2, $lastseen);
230 return "<b>No activity detected</b>" if (!$lastseenpgp && !$lastseenfrom);
231 $lastseen = $lastseenpgp;
232 $lastseen = $lastseenfrom if (!$lastseenpgp);
234 if ($lastseenfrom && $lastseenpgp) {
235 ($d1) = ($lastseenpgp =~ /^\[(.+?)\]/); $d1 = ParseDate($d1);
236 ($d2) = ($lastseenfrom =~ /^\[(.+?)\]/); $d2 = ParseDate($d2);
237 $lastseen = (($d1 gt $d2) ? $lastseenpgp : $lastseenfrom);
240 my ($date,$user,$list,$msgid) = ($lastseen =~ /^\[(.+?)\]\s+"(.+?)"\s+"(?:<(.+?)>.*?|\-)"\s+"(<.+?>)"/);
241 $list = "on $list" if ($list);
242 $date = CGI::escapeHTML($date);
243 $user = CGI::escapeHTML($user);
244 $list = CGI::escapeHTML($list);
245 $msgid = CGI::escapeHTML($msgid);
246 return "$date $list<br> Message ID: $msgid";
251 my ($abbrev, $country);
252 open (F, $config{countrylist}) || return uc($in);
255 ($abbrev, $country) = split(/\s+/, $_, 2);
256 if ($abbrev eq $in) {
271 print "Content-type: text/html; charset=utf-8\n\n" if (!$htmlhdrsent);
276 &HTMLSendHeader if (!$htmlhdrsent);
286 my ($lat, $long) = @_;
288 $lat =~ s/[^-+\.\d]//g; $long =~ s/[^-+\.\d]//g;
290 if (($lat =~ /^(\-|\+?)\d+(\.\d+)?/) && ($long =~ /^(\-|\+?)\d+(\.\d+)?/)) {
291 return ($lat, $long);
300 my @names = $query->param;
302 foreach $key (@names) { # web security is a joke ... <sigh>
303 $_ = $query->param($key);
308 $query->param($key, $_);
321 $mesg = $ldap->modify($dn, delete => { $attr => [] });
323 $val = [ $val ] if (!ref($val));
324 $mesg = $ldap->modify($dn, replace => { $attr => $val });
325 $mesg->code && &Util::HTMLError("error updating $attr: ".$mesg->error);
332 # reads a config file and results a hashref with the results
333 my (%config, $attr, $setting);
334 open (F, "<$configfile") || &HTMLError("Cannot open $configfile: $!");
337 if ((!/^\s*#/) && ($_ ne "")) {
338 # Chop off any trailing comments
341 ($attr, $setting) = ($1, $2);
342 $setting =~ s/"//g; #"
344 $attr =~ s/^\s+//; $attr =~ s/\s+$//;
345 $setting =~ s/^\s+//; $setting =~ s/\s+$//;
346 $config{$attr} = $setting;
353 sub UpgradeConnection($) {
355 my $mesg = $ldap->start_tls(
357 cafile => '/etc/ssl/certs/spi-cacert-2008.pem'
360 if ($mesg->code != LDAP_SUCCESS) {
361 print "Content-type: text/html; charset=utf-8\n\n";
362 print "<html><body><h1>STARTTLS failed: "..$mesg->error."</h1></body></html>\n";
367 sub readwrite3($$$$) {
368 my ($in, $inputfd, $stdoutfd, $stderrfd) = @_;
370 #Echolot::Log::trace("Entering readwrite_gpg.");
372 local $INPUT_RECORD_SEPARATOR = undef;
373 my $sout = IO::Select->new();
374 my $sin = IO::Select->new();
377 #Echolot::Log::trace("input is $inputfd; output is $stdoutfd; err is $stderrfd; status is ".(defined $statusfd ? $statusfd : 'undef').".");
379 $inputfd->blocking(0);
380 $stdoutfd->blocking(0);
381 $stderrfd->blocking(0);
382 $sout->add($stdoutfd);
383 $sout->add($stderrfd);
386 my ($stdout, $stderr) = ("", "", "");
388 my ($readyr, $readyw);
389 while ($sout->count() > 0 || (defined($sin) && ($sin->count() > 0))) {
390 #Echolot::Log::trace("select waiting for ".($sout->count())." fds.");
391 ($readyr, $readyw, undef) = IO::Select::select($sout, $sin, undef, 42);
392 #Echolot::Log::trace("ready: write: ".(defined $readyw ? scalar @$readyw : 'none')."; read: ".(defined $readyr ? scalar @$readyr : 'none'));
393 for my $wfd (@$readyw) {
394 #Echolot::Log::trace("writing to $wfd.");
396 if ($offset != length($in)) {
397 $written = $wfd->syswrite($in, length($in) - $offset, $offset);
399 unless (defined ($written)) {
400 #Echolot::Log::warn("Error while writing to GnuPG: $!");
406 if ($offset == length($in)) {
407 #Echolot::Log::trace("writing to $wfd done.");
415 next unless (defined(@$readyr)); # Wait some more.
417 for my $rfd (@$readyr) {
419 #Echolot::Log::trace("reading from $rfd done.");
424 #Echolot::Log::trace("reading from $rfd.");
425 if ($rfd == $stdoutfd) {
429 if ($rfd == $stderrfd) {
435 #Echolot::Log::trace("readwrite_gpg done.");
436 return ($stdout, $stderr);
439 sub checkPasswordQuality($$$) {
440 my ($pw, $oldpw, $ldapelements) = @_;
441 my ($stdinR, $stdinW) = (IO::Handle->new(), IO::Handle->new());
442 my ($stdoutR, $stdoutW) = (IO::Handle->new(), IO::Handle->new());
443 my ($stderrR, $stderrW) = (IO::Handle->new(), IO::Handle->new());
444 pipe $stdinR, $stdinW;
445 pipe $stdoutR, $stdoutW;
446 pipe $stderrR, $stderrW;
449 unless (defined $pid) {
450 return (2, "Could not fork: $!");
452 unless ($pid) { # child
459 open (STDIN, "<&".$stdinR->fileno) or warn ("Cannot dup stdinR (fd ".$stdinR->fileno.") as STDIN: $!");
460 open (STDOUT, ">&".$stdoutW->fileno) or warn ("Cannot dup stdoutW (fd ".$stdoutW->fileno.") as STDOUT: $!");
461 open (STDERR, ">&".$stderrW->fileno) or warn ("Cannot dup stderrW (fd ".$stderrW->fileno.") as STDERR: $!");
462 { exec('/usr/lib/userdir-ldap-cgi/password-qualify-check'); }
463 $stderrW->print("Could not exec password-qualify-check: $!\n");
470 $oldpw = '' unless defined $oldpw;
471 my $out = join("\n", $pw, $oldpw, @$ldapelements)."\n";
472 my ($stdout, $stderr) = readwrite3($out, $stdinW, $stdoutR, $stderrR);
475 my $exitcode = $? >> 8;
476 if ($exitcode == 0 && $stdout eq '' && $stderr eq '') {
478 } elsif ($exitcode == 1 && $stderr eq '') {
481 return (2, "check exited with exit code $exitcode, said '$stdout' on stdout, and '$stderr' on stderr.");