7 my $blocksize = 8; # A blowfish block is 8 bytes
8 my $configfile = "/etc/userdir-ldap/userdir-ldap.conf";
10 my %config = &ReadConfigFile;
15 open (F, "</dev/urandom") || die &HTMLError("No /dev/urandom found!");
16 read(F, $input, $keysize); # key length is 8 bytes
23 # this can create either a DES type salt or a MD5 salt
24 my $md5 = shift; # do we want a MD5 salt?
25 my $validstr = './0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
26 my @valid = split(//,$validstr);
29 my $cryptsaltlen = ($md5 ? 8 : 2);
31 open (F, "</dev/urandom") || die &HTMLError("No /dev/urandom found!");
32 foreach (1..$cryptsaltlen) {
34 $out .= $valid[ord($in) % ($#valid + 1)];
37 return ($md5 ? "\$1\$$out\$" : $out);
41 # blowfish only encrypts things in blocks of 8 bytes, so we
42 # need a custom routine that handles longer strings....
47 $input .= " " x ($blocksize - (length($input) % $blocksize)) if (length($input % $blocksize));
49 for ($pos = 0; $pos < length($input); $pos += $blocksize) {
50 $output .= unpack("H16", $cipher->encrypt(substr($input, $pos, $blocksize)));
56 # like encrypt, needs to deal with big blocks. Note that we assume
57 # trailing spaces are unimportant.
60 my ($pos, $portion, $output);
62 ((length($input) % $blocksize) == 0) || &HTMLError("Password corrupted"); # should always be true...
64 for ($pos = 0; $pos < length($input); $pos += $blocksize*2) {
65 $portion = pack("H16", substr($input, $pos, $blocksize*2));
66 $output .= $cipher->decrypt($portion);
73 sub SavePasswordToFile {
78 my $cryptuser = crypt($userid, &CreateCryptSalt);
79 my $secret = Encrypt($cipher, $password);
80 $cryptuser =~ y/\//_/; # translate slashes to underscores...
82 my $fn = "$config{authtokenpath}/$cryptuser";
83 open (F, ">$fn") || &HTMLError("$fn: $!");
91 sub ReadPasswordFromFile {
97 $userid =~ y/\//_/; # translate slashes to underscores...
99 # if we couldn't read the password file, assume user is unauthenticated. is this ok?
100 open (F, "<$config{authtokenpath}/$userid") || return undef;
101 chomp($passwd = <F>);
105 # check to make sure we read something
106 return undef if (!$passwd || !$time);
108 # check to make sure the time is positive, and that the auth token
110 my $tdiff = (time - $time);
111 &HTMLError("Your authentication token has expired. Please <a href=\"$config{webloginhtml}\">relogin</a>") if (($tdiff < 0) || ($tdiff > $config{authexpires}));
113 return Decrypt($cipher, $passwd);
117 my ($id, $hrkey) = split(/:/, shift, 2);
118 return undef if (!$id || !$hrkey);
119 my $key = pack("H".(length($hrkey)), $hrkey);
120 my $cipher = new Crypt::Blowfish $key;
121 my $r = ReadPasswordFromFile($id, $cipher);
123 UpdateAuthToken("$id:$hrkey", $r);
125 ClearAuthToken("$id:$hrkey")
131 my ($id, $hrkey) = split(/:/, shift, 2);
132 $id =~ y/\//_/; # switch / to _
133 unlink "$config{authtokenpath}/$id" || &HTMLError("Error removing authtoken: $!");
136 sub UpdateAuthToken {
137 my ($id, $hrkey) = split(/:/, shift, 2);
138 my $password = shift;
139 my $key = pack("H".(length($hrkey)), $hrkey);
140 $id =~ y/\//_/; # switch / to _
141 my $cipher = new Crypt::Blowfish $key;
142 my $secret = Encrypt($cipher, $password);
144 my $fn = "$config{authtokenpath}/$id";
145 open (F, ">$fn") || &HTMLError("$fn: $!");
149 chmod 0600, "$fn" || &HTMLError("$fn: $!");
153 sub FormatFingerPrint {
157 if (length($in) == 32) {
159 $out .= substr($in, $_*2, 2)." ";
160 $out .= " " if ($_ == 7);
163 foreach (0..int(length($in)/2)) {
164 $out .= substr($in, $_*4, 4)." ";
171 my $fingerprint = shift;
172 my ($out, $keyringparam);
174 foreach (split(/:/, $config{keyrings})) {
175 $keyringparam .= "--keyring $_ ";
178 $fingerprint =~ s/\s//g;
179 $fingerprint = "0x".$fingerprint;
181 $/ = undef; # just suck it up ....
182 open(FP, "$config{gpg} $keyringparam --list-sigs --fingerprint $fingerprint|");
185 open(FP, "$config{gpg} $keyringparam --export -a $fingerprint|");
193 sub FormatTimestamp {
195 $in =~ /^(....)(..)(..)(..)(..)(..)/;
197 return sprintf("%04d/%02d/%02d %02d:%02d:%02d UTC", $1,$2,$3,$4,$5,$6);
202 my ($abbrev, $country);
203 open (F, $config{countrylist}) || return uc($in);
206 ($abbrev, $country) = split(/\s+/, $_, 2);
207 if ($abbrev eq $in) {
222 print "Content-type: text/html\n\n" if (!$htmlhdrsent);
227 &HTMLSendHeader if (!$htmlhdrsent);
237 my ($lat, $long) = @_;
239 $lat =~ s/[^-+\.\d]//g; $long =~ s/[^-+\.\d]//g;
241 if (($lat =~ /^(\-|\+?)\d+(\.\d+)?/) && ($long =~ /^(\-|\+?)\d+(\.\d+)?/)) {
242 return ($lat, $long);
251 # reads a config file and results a hashref with the results
252 my (%config, $attr, $setting);
253 open (F, "<$configfile") || &HTMLError("Cannot open $configfile: $!");
256 if ((!/^\s*#/) && ($_ ne "")) {
257 # Chop off any trailing comments
259 ($attr, $setting) = split(/=/, $_, 2);
262 $attr =~ s/^ +//; $attr =~ s/ +$//;
263 $setting =~ s/^ +//; $setting =~ s/ +$//;
264 $config{$attr} = $setting;