| 1 | package Apache::SiteControl::UserFactory; | ||
| 2 | |||
| 3 | use 5.008; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use Carp; | ||
| 7 | use Data::Dumper; | ||
| 8 | use Apache::SiteControl::User; | ||
| 9 | use Crypt::CBC; | ||
| 10 | |||
| 11 | our $VERSION = "1.0"; | ||
| 12 | |||
| 13 | our $engine; | ||
| 14 | our $encryption_key; | ||
| 15 | |||
| 16 | sub init_engine | ||
| 17 | { | ||
| 18 | my $cipher = shift; | ||
| 19 | my $key = shift; | ||
| 20 | |||
| 21 | if(!defined($engine)) { | ||
| 22 | $engine = Crypt::CBC->new({ key => $key, cipher => $cipher }); | ||
| 23 | } | ||
| 24 | } | ||
| 25 | |||
| 26 | # Params: Apache request, username, password, other credentials... | ||
| 27 | sub makeUser | ||
| 28 | { | ||
| 29 | my $this = shift; | ||
| 30 | my $r = shift; | ||
| 31 | my $username = shift; | ||
| 32 | my $password = shift; | ||
| 33 | my @other_cred = @_; | ||
| 34 | my $sessiondir = $r->dir_config("SiteControlSessions") || "/tmp"; | ||
| 35 | my $lockdir = $r->dir_config("SiteControlLocks") || "/tmp"; | ||
| 36 | my $debug = $r->dir_config("SiteControlDebug") || 0; | ||
| 37 | my $savePassword = $r->dir_config("UserObjectSavePassword") || 0; | ||
| 38 | my $cipher = $r->dir_config("UserObjectPasswordCipher") || "CAST5"; | ||
| 39 | my $key = $r->dir_config("UserObjectPasswordKey") || $encryption_key || "A not very secure key because the admin forgot to set it."; | ||
| 40 | my $saveOther = $r->dir_config("UserObjectSaveOtherCredentials") || 0; | ||
| 41 | my $factory = $r->dir_config("SiteControlUserFactory") || "Apache::SiteControl::UserFactory"; | ||
| 42 | my $user = undef; | ||
| 43 | my %session; | ||
| 44 | my $usermap; | ||
| 45 | my $session_removed = 0; | ||
| 46 | |||
| 47 | $r->log_error("encryption engine using key: $key") if $debug; | ||
| 48 | init_engine($cipher, $key) if($savePassword); | ||
| 49 | |||
| 50 | # Proper steps: | ||
| 51 | # 1. Check to see if session already exists for user. If so, delete it. | ||
| 52 | # 2. Create new session for user and populate it. | ||
| 53 | # 3. Return the new user object. | ||
| 54 | $r->log_error("Making user object for $username.") if $debug; | ||
| 55 | eval { | ||
| 56 | $usermap = $this->_getUsermap($r); | ||
| 57 | $r->log_error("Login process got user map: " . Dumper($usermap)) if $debug; | ||
| 58 | if(defined($usermap) && defined($usermap->{$username})) { | ||
| 59 | $r->log_error("$username is logging in, and already had a session $usermap->{$username}{_session_id}. Removing old session."); | ||
| 60 | $session_removed = 1; | ||
| 61 | eval { | ||
| 62 | tie %session, 'Apache::Session::File', | ||
| 63 | $usermap->{$username}{_session_id}, { | ||
| 64 | Directory => $sessiondir, | ||
| 65 | LockDirectory => $lockdir | ||
| 66 | }; | ||
| 67 | tied(%session)->delete; | ||
| 68 | }; | ||
| 69 | if($@) { | ||
| 70 | $r->log_error("Could not delete old session: $@"); | ||
| 71 | } | ||
| 72 | } | ||
| 73 | tie %session, 'Apache::Session::File', undef, | ||
| 74 | { | ||
| 75 | Directory => $sessiondir, | ||
| 76 | LockDirectory => $lockdir | ||
| 77 | }; | ||
| 78 | $user = new Apache::SiteControl::User($username, $session{_session_id}, $factory); | ||
| 79 | $session{username} = $username; | ||
| 80 | $session{manager} = $factory; | ||
| 81 | $session{attr_password} = $engine->encrypt($password) if($savePassword); | ||
| 82 | $session{attr_session_removed} = $session_removed; | ||
| 83 | if(@other_cred && $saveOther) { | ||
| 84 | my $i = 2; | ||
| 85 | for my $c (@other_cred) { | ||
| 86 | $r->log_error("Saving extra credential_$i with value $c") if $debug; | ||
| 87 | $session{"attr_credential_$i"} = $c; | ||
| 88 | $i++; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | $r->log_error("Created user: " . Dumper($user)) if $debug; | ||
| 92 | }; | ||
| 93 | if($@) { | ||
| 94 | $r->log_error("Problem making new user object: $@"); | ||
| 95 | return undef; | ||
| 96 | } | ||
| 97 | |||
| 98 | # Note: this is a half-baked user, but the controller only needs the session | ||
| 99 | # id. It might be better to return the result of findUser instead. | ||
| 100 | return $user; | ||
| 101 | } | ||
| 102 | |||
| 103 | # Params: apache request, session_key | ||
| 104 | sub findUser | ||
| 105 | { | ||
| 106 | my $this = shift; | ||
| 107 | my $r = shift; | ||
| 108 | my $ses_key = shift; | ||
| 109 | my $sessiondir = $r->dir_config("SiteControlSessions") || "/tmp"; | ||
| 110 | my $lockdir = $r->dir_config("SiteControlLocks") || "/tmp"; | ||
| 111 | my $debug = $r->dir_config("SiteControlDebug") || 0; | ||
| 112 | my $savePassword = $r->dir_config("UserObjectSavePassword") || 0; | ||
| 113 | my $cipher = $r->dir_config("UserObjectPasswordCipher") || "CAST5"; | ||
| 114 | my $key = $r->dir_config("UserObjectPasswordKey") || $encryption_key || "A not very secure key because the admin forgot to set it."; | ||
| 115 | my %session; | ||
| 116 | my $user; | ||
| 117 | |||
| 118 | $r->log_error("encryption engine using key: $key") if $debug; | ||
| 119 | init_engine($cipher, $key) if($savePassword); | ||
| 120 | |||
| 121 | eval { | ||
| 122 | tie %session, 'Apache::Session::File', $ses_key, { | ||
| 123 | Directory => $sessiondir, | ||
| 124 | LockDirectory => $lockdir | ||
| 125 | }; | ||
| 126 | # FIXME: Document the possible problems with changing user factories when | ||
| 127 | # persistent sessions already exist. | ||
| 128 | $user = new Apache::SiteControl::User($session{username}, $ses_key, $session{manager}); | ||
| 129 | for my $key (keys %session) { | ||
| 130 | next if $key !~ /^attr_/; | ||
| 131 | my $k2 = $key; | ||
| 132 | $k2 =~ s/^attr_//; | ||
| 133 | if($k2 eq 'password') { | ||
| 134 | $user->{attributes}{$k2} = $engine->decrypt($session{$key}); | ||
| 135 | } else { | ||
| 136 | $user->{attributes}{$k2} = $session{$key}; | ||
| 137 | } | ||
| 138 | } | ||
| 139 | $r->log_error("Restored user: " . Dumper($user)) if $debug; | ||
| 140 | }; | ||
| 141 | if($@) { | ||
| 142 | # This method should fail for new logins (or login after logout), so | ||
| 143 | # failing to find the user is not considered a "real" error | ||
| 144 | $r->log_error("Failed to find a user with cookie $ses_key.") if $debug; | ||
| 145 | return undef; | ||
| 146 | } | ||
| 147 | |||
| 148 | return $user; | ||
| 149 | } | ||
| 150 | |||
| 151 | # Apache request, user object (not name) | ||
| 152 | sub invalidate | ||
| 153 | { | ||
| 154 | my $this = shift; | ||
| 155 | my $r = shift; | ||
| 156 | my $userobj = shift; | ||
| 157 | my $debug = $r->dir_config("SiteControlDebug") || 0; | ||
| 158 | my $sessiondir = $r->dir_config("SiteControlSessions") || "/tmp"; | ||
| 159 | my $lockdir = $r->dir_config("SiteControlLocks") || "/tmp"; | ||
| 160 | my %session; | ||
| 161 | |||
| 162 | if(!$userobj->isa("Apache::SiteControl::User") || !defined($userobj->{sessionid})) { | ||
| 163 | $r->log_error("Invalid user object passed to saveAttribute. Cannot remove user."); | ||
| 164 | return 0; | ||
| 165 | } | ||
| 166 | |||
| 167 | $r->log_error("Logging out user: " . $userobj->getUsername) if $debug; | ||
| 168 | eval { | ||
| 169 | tie %session, 'Apache::Session::File', $userobj->{sessionid}, { | ||
| 170 | Directory => $sessiondir, | ||
| 171 | LockDirectory => $lockdir | ||
| 172 | }; | ||
| 173 | |||
| 174 | tied(%session)->delete; | ||
| 175 | $r->log_error("Done with logout.") if $debug; | ||
| 176 | }; | ||
| 177 | if($@) { | ||
| 178 | $r->log_error("Could not delete user (logout): $@"); | ||
| 179 | } | ||
| 180 | } | ||
| 181 | |||
| 182 | # Apache request, user object, attribute name | ||
| 183 | sub saveAttribute | ||
| 184 | { | ||
| 185 | my $this = shift; | ||
| 186 | my $r = shift; | ||
| 187 | my $userobj = shift; | ||
| 188 | my $name = shift; | ||
| 189 | my $debug = $r->dir_config("SiteControlDebug") || 0; | ||
| 190 | my $sessiondir = $r->dir_config("SiteControlSessions") || "/tmp"; | ||
| 191 | my $lockdir = $r->dir_config("SiteControlLocks") || "/tmp"; | ||
| 192 | my %session; | ||
| 193 | |||
| 194 | if(!$userobj->isa("Apache::SiteControl::User") || !defined($userobj->{sessionid})) { | ||
| 195 | $r->log_error("Invalid user object passed to saveAttribute. Attribute not saved."); | ||
| 196 | return 0; | ||
| 197 | } | ||
| 198 | |||
| 199 | eval { | ||
| 200 | tie %session, 'Apache::Session::File', $userobj->{sessionid}, { | ||
| 201 | Directory => $sessiondir, | ||
| 202 | LockDirectory => $lockdir | ||
| 203 | }; | ||
| 204 | |||
| 205 | $r->log_error("Saving attribute $name = " . | ||
| 206 | $userobj->getAttribute($name) . | ||
| 207 | "using Apache::Session::File.") if $debug; | ||
| 208 | $session{"attr_$name"} = $userobj->getAttribute($name); | ||
| 209 | untie %session; | ||
| 210 | }; | ||
| 211 | if($@) { | ||
| 212 | $r->log_error("Failed to save user attribute: $@"); | ||
| 213 | return 0; | ||
| 214 | } | ||
| 215 | } | ||
| 216 | |||
| 217 | # Internal method for this implementation (using session files) | ||
| 218 | # in: Apache request object | ||
| 219 | sub _getUsermap | ||
| 220 | { | ||
| 221 | my $this = shift; | ||
| 222 | my $r = shift; | ||
| 223 | my $sessiondir = $r->dir_config("SiteControlSessions") || "/tmp"; | ||
| 224 | my $lockdir = $r->dir_config("SiteControlLocks") || "/tmp"; | ||
| 225 | my $debug = $r->dir_config("SiteControlDebug") || 0; | ||
| 226 | my %usermap; | ||
| 227 | |||
| 228 | eval { | ||
| 229 | my %session; | ||
| 230 | |||
| 231 | my @files = <$sessiondir/[0-9a-f]*>; | ||
| 232 | my @sessions = grep { s#^.*/([^/]*)$#$1# } @files; | ||
| 233 | my $username; | ||
| 234 | |||
| 235 | $r->log_error("Current sessions: @sessions") if $debug; | ||
| 236 | for my $id (@sessions) | ||
| 237 | { | ||
| 238 | next if $id !~ /^[0-9a-f]{20,}/; | ||
| 239 | tie %session, 'Apache::Session::File', $id, { | ||
| 240 | Directory => $sessiondir, | ||
| 241 | LockDirectory => $lockdir | ||
| 242 | }; | ||
| 243 | $username = $session{username}; | ||
| 244 | if(!defined($username)) { | ||
| 245 | $r->log_error("Session $session{_session_id} does not have a username...deleting"); | ||
| 246 | tied(%session)->delete; | ||
| 247 | next; | ||
| 248 | } | ||
| 249 | if(defined($usermap{$username})) { | ||
| 250 | # last modify time of session we saw | ||
| 251 | my $timea = (stat("$sessiondir/$usermap{$username}{_session_id}"))[9]; | ||
| 252 | # last modify time of this session | ||
| 253 | my $timeb = (stat("$sessiondir/$id"))[9]; | ||
| 254 | $r->log_error("User $username has duplicate session! Expiring old session"); | ||
| 255 | if($timea < $timeb) { | ||
| 256 | # The one we saw earlier is older. Delete it. | ||
| 257 | untie %session; | ||
| 258 | tie %session, 'Apache::Session::File', | ||
| 259 | $usermap{$username}{_session_id}, { | ||
| 260 | Directory => $sessiondir, | ||
| 261 | LockDirectory => $lockdir | ||
| 262 | }; | ||
| 263 | tied(%session)->delete; | ||
| 264 | redo; # redo this loop so we record the more better one ;) | ||
| 265 | } else { | ||
| 266 | # The one we are looking at is older...delete it and go on. | ||
| 267 | tied(%session)->delete; | ||
| 268 | next; | ||
| 269 | } | ||
| 270 | } | ||
| 271 | $usermap{$username} = {}; | ||
| 272 | # Copy the session into our usermap | ||
| 273 | for my $key (keys %session) { | ||
| 274 | $usermap{$username}{$key} = $session{$key}; | ||
| 275 | } | ||
| 276 | untie %session; | ||
| 277 | } | ||
| 278 | $r->log_error("Current user map : " . Dumper(\%usermap)) if $debug; | ||
| 279 | |||
| 280 | }; | ||
| 281 | if($@) { | ||
| 282 | $r->log_error("Failure in _getUsermap: $@"); | ||
| 283 | return undef; | ||
| 284 | } | ||
| 285 | |||
| 286 | return { %usermap }; | ||
| 287 | } | ||
| 288 | |||
| 289 | 1; | ||
| 290 | |||
| 291 | __END__ |