6 my $blocksize = 8; # A blowfish block is 8 bytes
7 my $configfile = "/etc/userdir-ldap/userdir-ldap.conf";
8 #my $configfile = "/home/randolph/html/debian/perl/userdir-ldap.conf";
10 my %config = &ReadConfigFile;
13 eval 'use Crypt::Blowfish';
21 open (F, "</dev/urandom") || die &HTMLError("No /dev/urandom found!");
22 read(F, $input, $keysize); # key length is 8 bytes
29 # this can create either a DES type salt or a MD5 salt
30 my $md5 = shift; # do we want a MD5 salt?
31 my $validstr = './0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
32 my @valid = split(//,$validstr);
35 my $cryptsaltlen = ($md5 ? 8 : 2);
37 open (F, "</dev/urandom") || die &HTMLError("No /dev/urandom found!");
38 foreach (1..$cryptsaltlen) {
40 $out .= $valid[ord($in) % ($#valid + 1)];
43 return ($md5 ? "\$1\$$out\$" : $out);
47 # blowfish only encrypts things in blocks of 8 bytes, so we
48 # need a custom routine that handles longer strings....
53 $input .= " " x ($blocksize - (length($input) % $blocksize)) if (length($input % $blocksize));
55 for ($pos = 0; $pos < length($input); $pos += $blocksize) {
56 $output .= unpack("H16", $cipher->encrypt(substr($input, $pos, $blocksize))) if ($hascryptix);
62 # like encrypt, needs to deal with big blocks. Note that we assume
63 # trailing spaces are unimportant.
66 my ($pos, $portion, $output);
68 ((length($input) % $blocksize) == 0) || &HTMLError("Password corrupted"); # should always be true...
70 for ($pos = 0; $pos < length($input); $pos += $blocksize*2) {
71 $portion = pack("H16", substr($input, $pos, $blocksize*2));
72 $output .= $cipher->decrypt($portion) if ($hascryptix);
79 sub SavePasswordToFile {
84 my $cryptuser = crypt($userid, &CreateCryptSalt);
85 my $secret = Encrypt($cipher, $password);
86 $cryptuser =~ y/\//_/; # translate slashes to underscores...
88 my $fn = "$config{authtokenpath}/$cryptuser";
89 open (F, ">$fn") || &HTMLError("$fn: $!");
97 sub ReadPasswordFromFile {
103 $userid =~ y/\//_/; # translate slashes to underscores...
105 # if we couldn't read the password file, assume user is unauthenticated. is this ok?
106 open (F, "<$config{authtokenpath}/$userid") || return undef;
107 chomp($passwd = <F>);
111 # check to make sure we read something
112 return undef if (!$passwd || !$time);
114 # check to make sure the time is positive, and that the auth token
116 my $tdiff = (time - $time);
117 &HTMLError("Your authentication token has expired. Please <a href=\"$config{webloginhtml}\">relogin</a>") if (($tdiff < 0) || ($tdiff > $config{authexpires}));
119 return Decrypt($cipher, $passwd);
123 my ($id, $hrkey) = split(/,/, shift, 2);
124 return undef if (!$id || !$hrkey);
125 my $key = pack("H".(length($hrkey)), $hrkey);
126 my $cipher = new Crypt::Blowfish $key;
127 my $r = ReadPasswordFromFile($id, $cipher);
129 UpdateAuthToken("$id,$hrkey", $r);
131 ClearAuthToken("$id,$hrkey")
137 my ($id, $hrkey) = split(/,/, shift, 2);
138 $id =~ y/\//_/; # switch / to _
139 unlink "$config{authtokenpath}/$id" || &HTMLError("Error removing authtoken: $!");
142 sub UpdateAuthToken {
143 my ($id, $hrkey) = split(/,/, shift, 2);
144 my $password = shift;
145 my $key = pack("H".(length($hrkey)), $hrkey);
146 $id =~ y/\//_/; # switch / to _
147 my $cipher = new Crypt::Blowfish $key;
148 my $secret = Encrypt($cipher, $password);
150 my $fn = "$config{authtokenpath}/$id";
151 open (F, ">$fn") || &HTMLError("$fn: $!");
155 chmod 0600, "$fn" || &HTMLError("$fn: $!");
159 sub FormatFingerPrint {
163 if (length($in) == 32) {
165 $out .= substr($in, $_*2, 2)." ";
166 $out .= " " if ($_ == 7);
169 foreach (0..int(length($in)/2)) {
170 $out .= substr($in, $_*4, 4)." ";
177 my $fingerprint = shift;
178 my ($out, $keyringparam) = undef;
180 foreach (split(/:/, $config{keyrings})) {
181 $keyringparam .= "--keyring $_ ";
184 $fingerprint =~ s/\s//g;
185 $fingerprint = "0x".$fingerprint;
187 $/ = undef; # just suck it up ....
188 open(FP, "$config{gpg} --no-options --no-default-keyring $keyringparam --list-sigs --fingerprint $fingerprint|");
191 open(FP, "$config{gpg} --no-options --no-default-keyring $keyringparam --export -a $fingerprint|");
199 sub FormatTimestamp {
201 $in =~ /^(....)(..)(..)(..)(..)(..)/;
203 return sprintf("%04d/%02d/%02d %02d:%02d:%02d UTC", $1,$2,$3,$4,$5,$6);
208 my ($abbrev, $country);
209 open (F, $config{countrylist}) || return uc($in);
212 ($abbrev, $country) = split(/\s+/, $_, 2);
213 if ($abbrev eq $in) {
228 print "Content-type: text/html\n\n" if (!$htmlhdrsent);
233 &HTMLSendHeader if (!$htmlhdrsent);
243 my ($lat, $long) = @_;
245 $lat =~ s/[^-+\.\d]//g; $long =~ s/[^-+\.\d]//g;
247 if (($lat =~ /^(\-|\+?)\d+(\.\d+)?/) && ($long =~ /^(\-|\+?)\d+(\.\d+)?/)) {
248 return ($lat, $long);
257 # reads a config file and results a hashref with the results
258 my (%config, $attr, $setting);
259 open (F, "<$configfile") || &HTMLError("Cannot open $configfile: $!");
262 if ((!/^\s*#/) && ($_ ne "")) {
263 # Chop off any trailing comments
265 ($attr, $setting) = split(/=/, $_, 2);
266 $setting =~ s/"//g; #"
268 $attr =~ s/^\s+//; $attr =~ s/\s+$//;
269 $setting =~ s/^\s+//; $setting =~ s/\s+$//;
270 $config{$attr} = $setting;