6 my $blocksize = 8; # A blowfish block is 8 bytes
7 my $configfile = "/etc/userdir-ldap/userdir-ldap.conf";
8 #my $configfile = "/home/randolph/projects/userdir-ldap/userdir-ldap.conf";
10 my %config = &ReadConfigFile;
13 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 $input .= " " x ($blocksize - (length($input) % $blocksize)) if (length($input % $blocksize));
56 for ($pos = 0; $pos < length($input); $pos += $blocksize) {
57 $output .= unpack("H16", $cipher->encrypt(substr($input, $pos, $blocksize))) if ($hascryptix);
63 # like encrypt, needs to deal with big blocks. Note that we assume
64 # trailing spaces are unimportant.
67 my ($pos, $portion, $output);
69 ((length($input) % $blocksize) == 0) || &HTMLError("Password corrupted"); # should always be true...
71 for ($pos = 0; $pos < length($input); $pos += $blocksize*2) {
72 $portion = pack("H16", substr($input, $pos, $blocksize*2));
73 $output .= $cipher->decrypt($portion) if ($hascryptix);
80 sub SavePasswordToFile {
85 my $cryptuser = crypt($userid, &CreateCryptSalt);
86 my $secret = Encrypt($cipher, $password);
87 $cryptuser =~ y/\//_/; # translate slashes to underscores...
89 my $fn = "$config{authtokenpath}/$cryptuser";
90 open (F, ">$fn") || &HTMLError("$fn: $!");
98 sub ReadPasswordFromFile {
104 $userid =~ y/\//_/; # translate slashes to underscores...
106 # if we couldn't read the password file, assume user is unauthenticated. is this ok?
107 open (F, "<$config{authtokenpath}/$userid") || return undef;
108 chomp($passwd = <F>);
112 # check to make sure we read something
113 return undef if (!$passwd || !$time);
115 # check to make sure the time is positive, and that the auth token
117 my $tdiff = (time - $time);
118 &HTMLError("Your authentication token has expired. Please <a href=\"$config{webloginhtml}\">relogin</a>") if (($tdiff < 0) || ($tdiff > $config{authexpires}));
120 return Decrypt($cipher, $passwd);
124 my ($id, $hrkey) = split(/:/, shift, 2);
125 return undef if (!$id || !$hrkey);
126 my $key = pack("H".(length($hrkey)), $hrkey);
127 my $cipher = new Crypt::Blowfish $key;
128 my $r = ReadPasswordFromFile($id, $cipher);
130 UpdateAuthToken("$id:$hrkey", $r);
132 ClearAuthToken("$id:$hrkey")
138 my ($id, $hrkey) = split(/:/, shift, 2);
139 $id =~ y/\//_/; # switch / to _
140 unlink "$config{authtokenpath}/$id" || &HTMLError("Error removing authtoken: $!");
143 sub UpdateAuthToken {
144 my ($id, $hrkey) = split(/:/, shift, 2);
145 my $password = shift;
146 my $key = pack("H".(length($hrkey)), $hrkey);
147 $id =~ y/\//_/; # switch / to _
148 my $cipher = new Crypt::Blowfish $key;
149 my $secret = Encrypt($cipher, $password);
151 my $fn = "$config{authtokenpath}/$id";
152 open (F, ">$fn") || &HTMLError("$fn: $!");
156 chmod 0600, "$fn" || &HTMLError("$fn: $!");
160 sub FormatFingerPrint {
164 if (length($in) == 32) {
166 $out .= substr($in, $_*2, 2)." ";
167 $out .= " " if ($_ == 7);
170 foreach (0..int(length($in)/2)) {
171 $out .= substr($in, $_*4, 4)." ";
178 my $fingerprint = shift;
179 my ($out, $keyringparam) = undef;
181 foreach (split(/:/, $config{keyrings})) {
182 $keyringparam .= "--keyring $_ ";
185 $fingerprint =~ s/\s//g;
186 $fingerprint = "0x".$fingerprint;
188 $/ = undef; # just suck it up ....
189 open(FP, "$config{gpg} --no-options --no-default-keyring $keyringparam --list-sigs --fingerprint $fingerprint|");
192 open(FP, "$config{gpg} --no-options --no-default-keyring $keyringparam --export -a $fingerprint|");
200 sub FormatTimestamp {
202 $in =~ /^(....)(..)(..)(..)(..)(..)/;
204 return sprintf("%04d/%02d/%02d %02d:%02d:%02d UTC", $1,$2,$3,$4,$5,$6);
209 my ($abbrev, $country);
210 open (F, $config{countrylist}) || return uc($in);
213 ($abbrev, $country) = split(/\s+/, $_, 2);
214 if ($abbrev eq $in) {
229 print "Content-type: text/html\n\n" if (!$htmlhdrsent);
234 &HTMLSendHeader if (!$htmlhdrsent);
244 my ($lat, $long) = @_;
246 $lat =~ s/[^-+\.\d]//g; $long =~ s/[^-+\.\d]//g;
248 if (($lat =~ /^(\-|\+?)\d+(\.\d+)?/) && ($long =~ /^(\-|\+?)\d+(\.\d+)?/)) {
249 return ($lat, $long);
258 # reads a config file and results a hashref with the results
259 my (%config, $attr, $setting);
260 open (F, "<$configfile") || &HTMLError("Cannot open $configfile: $!");
263 if ((!/^\s*#/) && ($_ ne "")) {
264 # Chop off any trailing comments
266 ($attr, $setting) = split(/=/, $_, 2);
267 $setting =~ s/"//g; #"
269 $attr =~ s/^ +//; $attr =~ s/ +$//;
270 $setting =~ s/^ +//; $setting =~ s/ +$//;
271 $config{$attr} = $setting;