5 use Date::Manip qw(ParseDate);
7 my $blocksize = 8; # A blowfish block is 8 bytes
8 my $configfile = "/etc/userdir-ldap/userdir-ldap.conf";
9 #my $configfile = "/home/randolph/html/debian/perl/userdir-ldap.conf";
11 my %config = &ReadConfigFile;
14 eval 'use Crypt::Blowfish';
22 open (F, "</dev/urandom") || die &HTMLError("No /dev/urandom found!");
23 read(F, $input, $keysize); # key length is 8 bytes
30 # this can create either a DES type salt or a MD5 salt
31 my $md5 = shift; # do we want a MD5 salt?
32 my $validstr = './0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
33 my @valid = split(//,$validstr);
36 my $cryptsaltlen = ($md5 ? 8 : 2);
38 open (F, "</dev/urandom") || die &HTMLError("No /dev/urandom found!");
39 foreach (1..$cryptsaltlen) {
41 $out .= $valid[ord($in) % ($#valid + 1)];
44 return ($md5 ? "\$1\$$out\$" : $out);
48 # blowfish only encrypts things in blocks of 8 bytes, so we
49 # need a custom routine that handles longer strings....
54 # prepend a length byte */
55 $input = chr(length($input)).$input;
56 $input .= "\001" x ($blocksize - (length($input) % $blocksize)) if (length($input % $blocksize));
58 for ($pos = 0; $pos < length($input); $pos += $blocksize) {
59 $output .= unpack("H16", $cipher->encrypt(substr($input, $pos, $blocksize))) if ($hascryptix);
65 # like encrypt, needs to deal with big blocks. Note that we assume
66 # trailing spaces are unimportant.
69 my ($pos, $portion, $output, $len);
71 ((length($input) % $blocksize) == 0) || &HTMLError("Password corrupted"); # should always be true...
73 for ($pos = 0; $pos < length($input); $pos += $blocksize*2) {
74 $portion = pack("H16", substr($input, $pos, $blocksize*2));
75 $output .= $cipher->decrypt($portion) if ($hascryptix);
78 # check length byte, discard junk
79 $len = substr($output, 0, 1);
80 $output = substr($output, 1, ord($len));
84 sub SavePasswordToFile {
89 my $cryptuser = crypt($userid, &CreateCryptSalt);
90 my $secret = Encrypt($cipher, $password);
91 $cryptuser =~ y/\//_/; # translate slashes to underscores...
93 my $fn = "$config{authtokenpath}/$cryptuser";
94 open (F, ">$fn") || &HTMLError("$fn: $!");
102 sub ReadPasswordFromFile {
108 $userid =~ y/\//_/; # translate slashes to underscores...
110 # if we couldn't read the password file, assume user is unauthenticated. is this ok?
111 open (F, "<$config{authtokenpath}/$userid") || return undef;
112 chomp($passwd = <F>);
116 # check to make sure we read something
117 return undef if (!$passwd || !$time);
119 # check to make sure the time is positive, and that the auth token
121 my $tdiff = (time - $time);
122 &HTMLError("Your authentication token has expired. Please <a href=\"https://$ENV{SERVER_NAME}/$config{webloginhtml}\">relogin</a>") if (($tdiff < 0) || ($tdiff > $config{authexpires}));
124 return Decrypt($cipher, $passwd);
128 my ($id, $hrkey) = split(/,/, shift, 2);
129 return undef if (!$id || !$hrkey);
130 my $key = pack("H".(length($hrkey)), $hrkey);
131 my $cipher = new Crypt::Blowfish $key;
132 my $r = ReadPasswordFromFile($id, $cipher);
134 UpdateAuthToken("$id,$hrkey", $r);
136 ClearAuthToken("$id,$hrkey")
142 my ($id, $hrkey) = split(/,/, shift, 2);
143 $id =~ y/\//_/; # switch / to _
144 unlink "$config{authtokenpath}/$id" || &HTMLError("Error removing authtoken: $!");
147 sub UpdateAuthToken {
148 my ($id, $hrkey) = split(/,/, shift, 2);
149 my $password = shift;
150 my $key = pack("H".(length($hrkey)), $hrkey);
151 $id =~ y/\//_/; # switch / to _
152 my $cipher = new Crypt::Blowfish $key;
153 my $secret = Encrypt($cipher, $password);
155 my $fn = "$config{authtokenpath}/$id";
156 open (F, ">$fn") || &HTMLError("$fn: $!");
160 chmod 0600, "$fn" || &HTMLError("$fn: $!");
164 sub FormatFingerPrint {
168 if (length($in) == 32) {
170 $out .= substr($in, $_*2, 2)." ";
171 $out .= " " if ($_ == 7);
174 foreach (0..int(length($in)/4)) {
175 $out .= substr($in, $_*4, 4)." ";
183 my $fingerprint = shift;
184 my $signatures = shift;
185 my ($out, $keyringparam) = undef;
187 foreach (split(/:/, $config{keyrings})) {
188 $keyringparam .= "--keyring $_ ";
191 $fingerprint =~ s/\s//g;
192 $fingerprint = "0x".$fingerprint;
194 local $ENV{PATH} = '';
195 $/ = undef; # just suck it up ....
197 open(FP, "$config{gpg} --no-options --no-default-keyring $keyringparam --list-sigs --fingerprint $fingerprint|");
201 open(FP, "$config{gpg} --no-options --no-default-keyring $keyringparam --export -a $fingerprint|");
209 sub FormatTimestamp {
211 $in =~ /^(....)(..)(..)(..)(..)(..)/;
213 return sprintf("%04d/%02d/%02d %02d:%02d:%02d UTC", $1,$2,$3,$4,$5,$6);
218 # [Tue, 11 Jan 2000 02:37:18] "Joey Hess <joeyh@debian.org>" "<debian-boot@lists.debian.org> archive/latest/7130" "<20000110181924.H19910@kitenet.net>"
219 # [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>"
220 my $lastseenpgp = shift;
221 my $lastseenfrom = shift;
222 my ($d1, $d2, $lastseen);
224 return "<b>No activity detected</b>" if (!$lastseenpgp && !$lastseenfrom);
225 $lastseen = $lastseenfrom if (!$lastseenpgp);
227 if ($lastseenfrom && $lastseenpgp) {
228 ($d1) = ($lastseenpgp =~ /^\[(.+?)\]/); $d1 = ParseDate($d1);
229 ($d2) = ($lastseenfrom =~ /^\[(.+?)\]/); $d2 = ParseDate($d2);
230 $lastseen = (($d1 gt $d2) ? $lastseenpgp : $lastseenfrom);
233 my ($date,$user,$list,$msgid) = ($lastseen =~ /^\[(.+?)\]\s+"(.+?)"\s+"(?:<(.+?)>.*?|\-)"\s+"<(.+?)>"/);
234 $list = "on $list" if ($list);
235 return "$date $list<br> Message ID: $msgid";
240 my ($abbrev, $country);
241 open (F, $config{countrylist}) || return uc($in);
244 ($abbrev, $country) = split(/\s+/, $_, 2);
245 if ($abbrev eq $in) {
260 print "Content-type: text/html; charset=utf-8\n\n" if (!$htmlhdrsent);
265 &HTMLSendHeader if (!$htmlhdrsent);
275 my ($lat, $long) = @_;
277 $lat =~ s/[^-+\.\d]//g; $long =~ s/[^-+\.\d]//g;
279 if (($lat =~ /^(\-|\+?)\d+(\.\d+)?/) && ($long =~ /^(\-|\+?)\d+(\.\d+)?/)) {
280 return ($lat, $long);
289 my @names = $query->param;
291 foreach $key (@names) { # web security is a joke ... <sigh>
292 $_ = $query->param($key);
297 $query->param($key, $_);
310 $mesg = $ldap->modify($dn, delete => { $attr => [] });
312 $val = [ $val ] if (!ref($val));
313 $mesg = $ldap->modify($dn, replace => { $attr => $val });
314 $mesg->code && &Util::HTMLError("error updating $attr: ".$mesg->error);
321 # reads a config file and results a hashref with the results
322 my (%config, $attr, $setting);
323 open (F, "<$configfile") || &HTMLError("Cannot open $configfile: $!");
326 if ((!/^\s*#/) && ($_ ne "")) {
327 # Chop off any trailing comments
330 ($attr, $setting) = ($1, $2);
331 $setting =~ s/"//g; #"
333 $attr =~ s/^\s+//; $attr =~ s/\s+$//;
334 $setting =~ s/^\s+//; $setting =~ s/\s+$//;
335 $config{$attr} = $setting;