| 1 | package Apache2::SiteControl::UserFactory; | ||
| 2 | |||
| 3 | use 5.008; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use Carp; | ||
| 7 | use Data::Dumper; | ||
| 8 | use Apache2::SiteControl::User; | ||
| 9 | use Crypt::CBC; | ||
| 10 | |||
| 11 | our $engine; | ||
| 12 | our $encryption_key; | ||
| 13 | |||
| 14 | sub init_engine | ||
| 15 | { | ||
| 16 | my $cipher = shift; | ||
| 17 | my $key = shift; | ||
| 18 | |||
| 19 | if(!defined($engine)) { | ||
| 20 | $engine = Crypt::CBC->new({ key => $key, cipher => $cipher }); | ||
| 21 | } | ||
| 22 | } | ||
| 23 | |||
| 24 | # Params: Apache request, username, password, other credentials... | ||
| 25 | sub makeUser | ||
| 26 | { | ||
| 27 | my $this = shift; | ||
| 28 | my $r = shift; | ||
| 29 | my $username = shift; | ||
| 30 | my $password = shift; | ||
| 31 | my @other_cred = @_; | ||
| 32 | my $sessiondir = $r->dir_config("SiteControlSessions") || "/tmp"; | ||
| 33 | my $lockdir = $r->dir_config("SiteControlLocks") || "/tmp"; | ||
| 34 | my $mapdir = $r->dir_config("SiteControlUsermap") || ""; | ||
| 35 | my $debug = $r->dir_config("SiteControlDebug") || 0; | ||
| 36 | my $savePassword = $r->dir_config("UserObjectSavePassword") || 0; | ||
| 37 | my $cipher = $r->dir_config("UserObjectPasswordCipher") || "CAST5"; | ||
| 38 | my $key = $r->dir_config("UserObjectPasswordKey") || $encryption_key || "A not very secure key because the admin forgot to set it."; | ||
| 39 | my $saveOther = $r->dir_config("UserObjectSaveOtherCredentials") || 0; | ||
| 40 | my $factory = $r->dir_config("SiteControlUserFactory") || "Apache2::SiteControl::UserFactory"; | ||
| 41 | my $user = undef; | ||
| 42 | my %session; | ||
| 43 | my $usermap; | ||
| 44 | my $session_removed = 0; | ||
| 45 | |||
| 46 | $r->log_error("encryption engine using key: $key") if $debug; | ||
| 47 | init_engine($cipher, $key) if($savePassword); | ||
| 48 | |||
| 49 | # Proper steps: | ||
| 50 | # 1. Check to see if session already exists for user. If so, delete it. | ||
| 51 | # 2. Create new session for user and populate it. | ||
| 52 | # 3. Return the new user object. | ||
| 53 | $r->log_error("Making user object for $username.") if $debug; | ||
| 54 | eval { | ||
| 55 | if($mapdir && -l "$mapdir/$username") { | ||
| 56 | $r->log_error("$username is logging in, and already had a session. Removing old session."); | ||
| 57 | $session_removed = 1; | ||
| 58 | my $sid = readlink "$mapdir/$username"; | ||
| 59 | unlink "$mapdir/$username"; # Remove the link | ||
| 60 | unlink "$sid"; # Remove the session file | ||
| 61 | } | ||
| 62 | tie %session, 'Apache::Session::File', undef, | ||
| 63 | { | ||
| 64 | Directory => $sessiondir, | ||
| 65 | LockDirectory => $lockdir | ||
| 66 | }; | ||
| 67 | # Remember the username to session mapping. | ||
| 68 | $r->log_error("Making symlink from $sessiondir/$session{_session_id} to $mapdir/$username") if($mapdir); | ||
| 69 | symlink "$sessiondir/" . $session{_session_id}, "$mapdir/$username" if($mapdir); | ||
| 70 | $user = new Apache2::SiteControl::User($username, $session{_session_id}, $factory); | ||
| 71 | $session{username} = $username; | ||
| 72 | $session{manager} = $factory; | ||
| 73 | $session{attr_password} = $engine->encrypt($password) if($savePassword); | ||
| 74 | $session{attr_session_removed} = $session_removed; | ||
| 75 | if(@other_cred && $saveOther) { | ||
| 76 | my $i = 2; | ||
| 77 | for my $c (@other_cred) { | ||
| 78 | $r->log_error("Saving extra credential_$i with value $c") if $debug; | ||
| 79 | $session{"attr_credential_$i"} = $c; | ||
| 80 | $i++; | ||
| 81 | } | ||
| 82 | } | ||
| 83 | $r->log_error("Created user: " . Dumper($user)) if $debug; | ||
| 84 | }; | ||
| 85 | if($@) { | ||
| 86 | $r->log_error("Problem making new user object: $@"); | ||
| 87 | return undef; | ||
| 88 | } | ||
| 89 | |||
| 90 | # Note: this is a half-baked user, but the controller only needs the session | ||
| 91 | # id. It might be better to return the result of findUser instead. | ||
| 92 | return $user; | ||
| 93 | } | ||
| 94 | |||
| 95 | # Params: apache request, session_key | ||
| 96 | sub findUser | ||
| 97 | { | ||
| 98 | my $this = shift; | ||
| 99 | my $r = shift; | ||
| 100 | my $ses_key = shift; | ||
| 101 | my $sessiondir = $r->dir_config("SiteControlSessions") || "/tmp"; | ||
| 102 | my $lockdir = $r->dir_config("SiteControlLocks") || "/tmp"; | ||
| 103 | my $debug = $r->dir_config("SiteControlDebug") || 0; | ||
| 104 | my $savePassword = $r->dir_config("UserObjectSavePassword") || 0; | ||
| 105 | my $cipher = $r->dir_config("UserObjectPasswordCipher") || "CAST5"; | ||
| 106 | my $key = $r->dir_config("UserObjectPasswordKey") || $encryption_key || "A not very secure key because the admin forgot to set it."; | ||
| 107 | my %session; | ||
| 108 | my $user; | ||
| 109 | |||
| 110 | $r->log_error("encryption engine using key: $key") if $debug; | ||
| 111 | init_engine($cipher, $key) if($savePassword); | ||
| 112 | |||
| 113 | eval { | ||
| 114 | tie %session, 'Apache::Session::File', $ses_key, { | ||
| 115 | Directory => $sessiondir, | ||
| 116 | LockDirectory => $lockdir | ||
| 117 | }; | ||
| 118 | # FIXME: Document the possible problems with changing user factories when | ||
| 119 | # persistent sessions already exist. | ||
| 120 | $user = new Apache2::SiteControl::User($session{username}, $ses_key, $session{manager}); | ||
| 121 | for my $key (keys %session) { | ||
| 122 | next if $key !~ /^attr_/; | ||
| 123 | my $k2 = $key; | ||
| 124 | $k2 =~ s/^attr_//; | ||
| 125 | if($k2 eq 'password') { | ||
| 126 | $user->{attributes}{$k2} = $engine->decrypt($session{$key}); | ||
| 127 | } else { | ||
| 128 | $user->{attributes}{$k2} = $session{$key}; | ||
| 129 | } | ||
| 130 | } | ||
| 131 | $r->log_error("Restored user: " . Dumper($user)) if $debug; | ||
| 132 | }; | ||
| 133 | if($@) { | ||
| 134 | # This method should fail for new logins (or login after logout), so | ||
| 135 | # failing to find the user is not considered a "real" error | ||
| 136 | $r->log_error("Failed to find a user with cookie $ses_key.") if $debug; | ||
| 137 | return undef; | ||
| 138 | } | ||
| 139 | |||
| 140 | return $user; | ||
| 141 | } | ||
| 142 | |||
| 143 | # Apache request, user object (not name) | ||
| 144 | sub invalidate | ||
| 145 | { | ||
| 146 | my $this = shift; | ||
| 147 | my $r = shift; | ||
| 148 | my $userobj = shift; | ||
| 149 | my $debug = $r->dir_config("SiteControlDebug") || 0; | ||
| 150 | my $sessiondir = $r->dir_config("SiteControlSessions") || "/tmp"; | ||
| 151 | my $mapdir = $r->dir_config("SiteControlUsermap") || ""; | ||
| 152 | my $lockdir = $r->dir_config("SiteControlLocks") || "/tmp"; | ||
| 153 | my %session; | ||
| 154 | |||
| 155 | if(!$userobj->isa("Apache2::SiteControl::User") || !defined($userobj->{sessionid})) { | ||
| 156 | $r->log_error("Invalid user object passed to saveAttribute. Cannot remove user."); | ||
| 157 | return 0; | ||
| 158 | } | ||
| 159 | |||
| 160 | $r->log_error("Logging out user: " . $userobj->getUsername) if $debug; | ||
| 161 | eval { | ||
| 162 | unlink "$mapdir/" . $userobj->getUsername; # Remove the MSD link | ||
| 163 | tie %session, 'Apache::Session::File', $userobj->{sessionid}, { | ||
| 164 | Directory => $sessiondir, | ||
| 165 | LockDirectory => $lockdir | ||
| 166 | }; | ||
| 167 | |||
| 168 | tied(%session)->delete; | ||
| 169 | $r->log_error("Done with logout.") if $debug; | ||
| 170 | }; | ||
| 171 | if($@) { | ||
| 172 | $r->log_error("Could not delete user (logout): $@"); | ||
| 173 | } | ||
| 174 | } | ||
| 175 | |||
| 176 | # Apache request, user object, attribute name | ||
| 177 | sub saveAttribute | ||
| 178 | { | ||
| 179 | my $this = shift; | ||
| 180 | my $r = shift; | ||
| 181 | my $userobj = shift; | ||
| 182 | my $name = shift; | ||
| 183 | my $debug = $r->dir_config("SiteControlDebug") || 0; | ||
| 184 | my $sessiondir = $r->dir_config("SiteControlSessions") || "/tmp"; | ||
| 185 | my $lockdir = $r->dir_config("SiteControlLocks") || "/tmp"; | ||
| 186 | my %session; | ||
| 187 | |||
| 188 | if(!$userobj->isa("Apache2::SiteControl::User") || !defined($userobj->{sessionid})) { | ||
| 189 | $r->log_error("Invalid user object passed to saveAttribute. Attribute not saved."); | ||
| 190 | return 0; | ||
| 191 | } | ||
| 192 | |||
| 193 | eval { | ||
| 194 | tie %session, 'Apache::Session::File', $userobj->{sessionid}, { | ||
| 195 | Directory => $sessiondir, | ||
| 196 | LockDirectory => $lockdir | ||
| 197 | }; | ||
| 198 | |||
| 199 | $r->log_error("Saving attribute $name = " . | ||
| 200 | $userobj->getAttribute($name) . | ||
| 201 | "using Apache::Session::File.") if $debug; | ||
| 202 | $session{"attr_$name"} = $userobj->getAttribute($name); | ||
| 203 | untie %session; | ||
| 204 | }; | ||
| 205 | if($@) { | ||
| 206 | $r->log_error("Failed to save user attribute: $@"); | ||
| 207 | return 0; | ||
| 208 | } | ||
| 209 | } | ||
| 210 | |||
| 211 | 1; | ||
| 212 | |||
| 213 | __END__ |