| 1 | # $Id: AuthzCache.pm,v 1.17 2003/06/19 14:40:46 cgilmore Exp $ | ||
| 2 | # | ||
| 3 | # Author : Christian Gilmore | ||
| 4 | # Created On : Fri Jun 23 10:15:36 CDT 2000 | ||
| 5 | # Status : Functional | ||
| 6 | # | ||
| 7 | # PURPOSE | ||
| 8 | # Group Authorization Cache | ||
| 9 | # | ||
| 10 | ############################################################################### | ||
| 11 | # | ||
| 12 | # IBM Public License Version 1.0 | ||
| 13 | # | ||
| 14 | # THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS IBM | ||
| 15 | # PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR | ||
| 16 | # DISTRIBUTION OF THE PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF | ||
| 17 | # THIS AGREEMENT. | ||
| 18 | # | ||
| 19 | # 1. DEFINITIONS | ||
| 20 | # | ||
| 21 | # "Contribution" means: | ||
| 22 | # | ||
| 23 | # a) in the case of International Business Machines Corporation | ||
| 24 | # ("IBM"), the Original Program, and | ||
| 25 | # | ||
| 26 | # b) in the case of each Contributor, | ||
| 27 | # | ||
| 28 | # i) changes to the Program, and | ||
| 29 | # | ||
| 30 | # ii) additions to the Program; | ||
| 31 | # | ||
| 32 | # where such changes and/or additions to the Program originate from | ||
| 33 | # and are distributed by that particular Contributor. A Contribution | ||
| 34 | # 'originates' from a Contributor if it was added to the Program by | ||
| 35 | # such Contributor itself or anyone acting on such Contributor's | ||
| 36 | # behalf. Contributions do not include additions to the Program | ||
| 37 | # which: (i) are separate modules of software distributed in | ||
| 38 | # conjunction with the Program under their own license agreement, | ||
| 39 | # and (ii) are not derivative works of the Program. | ||
| 40 | # | ||
| 41 | # "Contributor" means IBM and any other entity that distributes the | ||
| 42 | # Program. | ||
| 43 | # | ||
| 44 | # "Licensed Patents " mean patent claims licensable by a Contributor | ||
| 45 | # which are necessarily infringed by the use or sale of its | ||
| 46 | # Contribution alone or when combined with the Program. | ||
| 47 | # | ||
| 48 | # "Original Program" means the original version of the software | ||
| 49 | # accompanying this Agreement as released by IBM, including source | ||
| 50 | # code, object code and documentation, if any. | ||
| 51 | # | ||
| 52 | # "Program" means the Original Program and Contributions. | ||
| 53 | # | ||
| 54 | # "Recipient" means anyone who receives the Program under this | ||
| 55 | # Agreement, including all Contributors. | ||
| 56 | # | ||
| 57 | # 2. GRANT OF RIGHTS | ||
| 58 | # | ||
| 59 | # a) Subject to the terms of this Agreement, each Contributor hereby | ||
| 60 | # grants Recipient a non-exclusive, worldwide, royalty-free | ||
| 61 | # copyright license to reproduce, prepare derivative works of, | ||
| 62 | # publicly display, publicly perform, distribute and sublicense the | ||
| 63 | # Contribution of such Contributor, if any, and such derivative | ||
| 64 | # works, in source code and object code form. | ||
| 65 | # | ||
| 66 | # b) Subject to the terms of this Agreement, each Contributor hereby | ||
| 67 | # grants Recipient a non-exclusive, worldwide, royalty-free patent | ||
| 68 | # license under Licensed Patents to make, use, sell, offer to sell, | ||
| 69 | # import and otherwise transfer the Contribution of such | ||
| 70 | # Contributor, if any, in source code and object code form. This | ||
| 71 | # patent license shall apply to the combination of the Contribution | ||
| 72 | # and the Program if, at the time the Contribution is added by the | ||
| 73 | # Contributor, such addition of the Contribution causes such | ||
| 74 | # combination to be covered by the Licensed Patents. The patent | ||
| 75 | # license shall not apply to any other combinations which include | ||
| 76 | # the Contribution. No hardware per se is licensed hereunder. | ||
| 77 | # | ||
| 78 | # c) Recipient understands that although each Contributor grants the | ||
| 79 | # licenses to its Contributions set forth herein, no assurances are | ||
| 80 | # provided by any Contributor that the Program does not infringe the | ||
| 81 | # patent or other intellectual property rights of any other entity. | ||
| 82 | # Each Contributor disclaims any liability to Recipient for claims | ||
| 83 | # brought by any other entity based on infringement of intellectual | ||
| 84 | # property rights or otherwise. As a condition to exercising the | ||
| 85 | # rights and licenses granted hereunder, each Recipient hereby | ||
| 86 | # assumes sole responsibility to secure any other intellectual | ||
| 87 | # property rights needed, if any. For example, if a third party | ||
| 88 | # patent license is required to allow Recipient to distribute the | ||
| 89 | # Program, it is Recipient's responsibility to acquire that license | ||
| 90 | # before distributing the Program. | ||
| 91 | # | ||
| 92 | # d) Each Contributor represents that to its knowledge it has | ||
| 93 | # sufficient copyright rights in its Contribution, if any, to grant | ||
| 94 | # the copyright license set forth in this Agreement. | ||
| 95 | # | ||
| 96 | # 3. REQUIREMENTS | ||
| 97 | # | ||
| 98 | # A Contributor may choose to distribute the Program in object code | ||
| 99 | # form under its own license agreement, provided that: | ||
| 100 | # | ||
| 101 | # a) it complies with the terms and conditions of this Agreement; | ||
| 102 | # | ||
| 103 | # and | ||
| 104 | # | ||
| 105 | # b) its license agreement: | ||
| 106 | # | ||
| 107 | # i) effectively disclaims on behalf of all Contributors all | ||
| 108 | # warranties and conditions, express and implied, including | ||
| 109 | # warranties or conditions of title and non-infringement, and | ||
| 110 | # implied warranties or conditions of merchantability and fitness | ||
| 111 | # for a particular purpose; | ||
| 112 | # | ||
| 113 | # ii) effectively excludes on behalf of all Contributors all | ||
| 114 | # liability for damages, including direct, indirect, special, | ||
| 115 | # incidental and consequential damages, such as lost profits; | ||
| 116 | # iii) states that any provisions which differ from this Agreement | ||
| 117 | # are offered by that Contributor alone and not by any other party; | ||
| 118 | # and | ||
| 119 | # | ||
| 120 | # iv) states that source code for the Program is available from such | ||
| 121 | # Contributor, and informs licensees how to obtain it in a | ||
| 122 | # reasonable manner on or through a medium customarily used for | ||
| 123 | # software exchange. | ||
| 124 | # | ||
| 125 | # When the Program is made available in source code form: | ||
| 126 | # | ||
| 127 | # a) it must be made available under this Agreement; and | ||
| 128 | # | ||
| 129 | # b) a copy of this Agreement must be included with each copy of the | ||
| 130 | # Program. | ||
| 131 | # | ||
| 132 | # Each Contributor must include the following in a conspicuous | ||
| 133 | # location in the Program: | ||
| 134 | # | ||
| 135 | # Copyright © {date here}, International Business Machines | ||
| 136 | # Corporation and others. All Rights Reserved. | ||
| 137 | # | ||
| 138 | # In addition, each Contributor must identify itself as the originator | ||
| 139 | # of its Contribution, if any, in a manner that reasonably allows | ||
| 140 | # subsequent Recipients to identify the originator of the | ||
| 141 | # Contribution. | ||
| 142 | # | ||
| 143 | # 4. COMMERCIAL DISTRIBUTION | ||
| 144 | # | ||
| 145 | # Commercial distributors of software may accept certain | ||
| 146 | # responsibilities with respect to end users, business partners and | ||
| 147 | # the like. While this license is intended to facilitate the | ||
| 148 | # commercial use of the Program, the Contributor who includes the | ||
| 149 | # Program in a commercial product offering should do so in a manner | ||
| 150 | # which does not create potential liability for other Contributors. | ||
| 151 | # Therefore, if a Contributor includes the Program in a commercial | ||
| 152 | # product offering, such Contributor ("Commercial Contributor") hereby | ||
| 153 | # agrees to defend and indemnify every other Contributor ("Indemnified | ||
| 154 | # Contributor") against any losses, damages and costs (collectively | ||
| 155 | # "Losses") arising from claims, lawsuits and other legal actions | ||
| 156 | # brought by a third party against the Indemnified Contributor to the | ||
| 157 | # extent caused by the acts or omissions of such Commercial | ||
| 158 | # Contributor in connection with its distribution of the Program in a | ||
| 159 | # commercial product offering. The obligations in this section do not | ||
| 160 | # apply to any claims or Losses relating to any actual or alleged | ||
| 161 | # intellectual property infringement. In order to qualify, an | ||
| 162 | # Indemnified Contributor must: a) promptly notify the Commercial | ||
| 163 | # Contributor in writing of such claim, and b) allow the Commercial | ||
| 164 | # Contributor to control, and cooperate with the Commercial | ||
| 165 | # Contributor in, the defense and any related settlement negotiations. | ||
| 166 | # The Indemnified Contributor may participate in any such claim at its | ||
| 167 | # own expense. | ||
| 168 | # | ||
| 169 | # For example, a Contributor might include the Program in a commercial | ||
| 170 | # product offering, Product X. That Contributor is then a Commercial | ||
| 171 | # Contributor. If that Commercial Contributor then makes performance | ||
| 172 | # claims, or offers warranties related to Product X, those performance | ||
| 173 | # claims and warranties are such Commercial Contributor's | ||
| 174 | # responsibility alone. Under this section, the Commercial Contributor | ||
| 175 | # would have to defend claims against the other Contributors related | ||
| 176 | # to those performance claims and warranties, and if a court requires | ||
| 177 | # any other Contributor to pay any damages as a result, the Commercial | ||
| 178 | # Contributor must pay those damages. | ||
| 179 | # | ||
| 180 | # 5. NO WARRANTY | ||
| 181 | # | ||
| 182 | # EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS | ||
| 183 | # PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF | ||
| 184 | # ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, | ||
| 185 | # ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, | ||
| 186 | # MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient | ||
| 187 | # is solely responsible for determining the appropriateness of using | ||
| 188 | # and distributing the Program and assumes all risks associated with | ||
| 189 | # its exercise of rights under this Agreement, including but not | ||
| 190 | # limited to the risks and costs of program errors, compliance with | ||
| 191 | # applicable laws, damage to or loss of data, programs or equipment, | ||
| 192 | # and unavailability or interruption of operations. | ||
| 193 | # | ||
| 194 | # 6. DISCLAIMER OF LIABILITY | ||
| 195 | # | ||
| 196 | # EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT | ||
| 197 | # NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, | ||
| 198 | # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
| 199 | # (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND ON | ||
| 200 | # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR | ||
| 201 | # TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF | ||
| 202 | # THE USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS | ||
| 203 | # GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH | ||
| 204 | # DAMAGES. | ||
| 205 | # | ||
| 206 | # 7. GENERAL | ||
| 207 | # | ||
| 208 | # If any provision of this Agreement is invalid or unenforceable under | ||
| 209 | # applicable law, it shall not affect the validity or enforceability | ||
| 210 | # of the remainder of the terms of this Agreement, and without further | ||
| 211 | # action by the parties hereto, such provision shall be reformed to | ||
| 212 | # the minimum extent necessary to make such provision valid and | ||
| 213 | # enforceable. | ||
| 214 | # | ||
| 215 | # If Recipient institutes patent litigation against a Contributor with | ||
| 216 | # respect to a patent applicable to software (including a cross-claim | ||
| 217 | # or counterclaim in a lawsuit), then any patent licenses granted by | ||
| 218 | # that Contributor to such Recipient under this Agreement shall | ||
| 219 | # terminate as of the date such litigation is filed. In addition, If | ||
| 220 | # Recipient institutes patent litigation against any entity (including | ||
| 221 | # a cross-claim or counterclaim in a lawsuit) alleging that the | ||
| 222 | # Program itself (excluding combinations of the Program with other | ||
| 223 | # software or hardware) infringes such Recipient's patent(s), then | ||
| 224 | # such Recipient's rights granted under Section 2(b) shall terminate | ||
| 225 | # as of the date such litigation is filed. | ||
| 226 | # | ||
| 227 | # All Recipient's rights under this Agreement shall terminate if it | ||
| 228 | # fails to comply with any of the material terms or conditions of this | ||
| 229 | # Agreement and does not cure such failure in a reasonable period of | ||
| 230 | # time after becoming aware of such noncompliance. If all Recipient's | ||
| 231 | # rights under this Agreement terminate, Recipient agrees to cease use | ||
| 232 | # and distribution of the Program as soon as reasonably practicable. | ||
| 233 | # However, Recipient's obligations under this Agreement and any | ||
| 234 | # licenses granted by Recipient relating to the Program shall continue | ||
| 235 | # and survive. | ||
| 236 | # | ||
| 237 | # IBM may publish new versions (including revisions) of this Agreement | ||
| 238 | # from time to time. Each new version of the Agreement will be given a | ||
| 239 | # distinguishing version number. The Program (including Contributions) | ||
| 240 | # may always be distributed subject to the version of the Agreement | ||
| 241 | # under which it was received. In addition, after a new version of the | ||
| 242 | # Agreement is published, Contributor may elect to distribute the | ||
| 243 | # Program (including its Contributions) under the new version. No one | ||
| 244 | # other than IBM has the right to modify this Agreement. Except as | ||
| 245 | # expressly stated in Sections 2(a) and 2(b) above, Recipient receives | ||
| 246 | # no rights or licenses to the intellectual property of any | ||
| 247 | # Contributor under this Agreement, whether expressly, by implication, | ||
| 248 | # estoppel or otherwise. All rights in the Program not expressly | ||
| 249 | # granted under this Agreement are reserved. | ||
| 250 | # | ||
| 251 | # This Agreement is governed by the laws of the State of New York and | ||
| 252 | # the intellectual property laws of the United States of America. No | ||
| 253 | # party to this Agreement will bring a legal action under this | ||
| 254 | # Agreement more than one year after the cause of action arose. Each | ||
| 255 | # party waives its rights to a jury trial in any resulting litigation. | ||
| 256 | # | ||
| 257 | ############################################################################### | ||
| 258 | |||
| 259 | |||
| 260 | # Package name | ||
| 261 | package Apache::AuthzCache; | ||
| 262 | |||
| 263 | |||
| 264 | # Required libraries | ||
| 265 | use strict; | ||
| 266 | use mod_perl (); | ||
| 267 | use Apache::Constants qw(OK AUTH_REQUIRED DECLINED DONE); | ||
| 268 | use Apache::Log; | ||
| 269 | use Cache::FileCache; | ||
| 270 | use Text::ParseWords; | ||
| 271 | use Time::Object; | ||
| 272 | |||
| 273 | |||
| 274 | # Global constants | ||
| 275 | use constant REQUIRE_OPTS => { 'inagroup' => 1, | ||
| 276 | 'inmanygroups' => 2, | ||
| 277 | 'inallgroups' => 3 }; | ||
| 278 | |||
| 279 | |||
| 280 | # Global variables | ||
| 281 | $Apache::AuthzCache::VERSION = '1.01'; | ||
| 282 | |||
| 283 | |||
| 284 | ############################################################################### | ||
| 285 | ############################################################################### | ||
| 286 | # handler: hook into Apache/mod_perl API | ||
| 287 | ############################################################################### | ||
| 288 | ############################################################################### | ||
| 289 | sub handler { | ||
| 290 | my $r = shift; | ||
| 291 | return OK unless $r->is_initial_req; # only the first internal request | ||
| 292 | my $requires = $r->requires; | ||
| 293 | return OK unless $requires; | ||
| 294 | my $require_groups = undef; | ||
| 295 | |||
| 296 | # Get configuration | ||
| 297 | my $casesensitive = $r->dir_config('AuthzCache_CaseSensitive') || 'on'; | ||
| 298 | my $cache_time_limit = $r->dir_config('AuthzCache_CacheTime') || | ||
| 299 | $r->dir_config('AuthzCache_Timeout') || $Cache::Cache::EXPIRES_NEVER; | ||
| 300 | my $cache_dir = $r->dir_config('AuthzCache_Directory') || '/tmp'; | ||
| 301 | my $cache_umask = $r->dir_config('AuthzCache_Umask') || '007'; | ||
| 302 | my $auth_name = $r->auth_name; | ||
| 303 | my $requirement = $r->dir_config('AuthzRequire') || 'inAGroup'; | ||
| 304 | $r->log->debug("manage_cache: cache_time_limit=$cache_time_limit, ", | ||
| 305 | "cache_dir=$cache_dir, cache_umask=$cache_umask, ", | ||
| 306 | "auth_name=$auth_name"); | ||
| 307 | $requirement = REQUIRE_OPTS->{lc($requirement)} || 1; | ||
| 308 | |||
| 309 | # Get username | ||
| 310 | my $user_sent = $r->connection->user; | ||
| 311 | $r->log->debug("handler: username=$user_sent"); | ||
| 312 | |||
| 313 | # Clear for paranoid security precautions | ||
| 314 | $r->subprocess_env(REMOTE_GROUP => undef); | ||
| 315 | undef($r->headers_in->{'REMOTE_GROUP'}); | ||
| 316 | $r->notes('AuthzCache' => undef); | ||
| 317 | |||
| 318 | # Get required groups | ||
| 319 | for my $req (@$requires) { | ||
| 320 | my ($require, $rest) = split /\s+/, $req->{requirement}, 2; | ||
| 321 | if ($require eq "user") { return OK | ||
| 322 | if grep $user_sent eq $_, split /\s+/, $rest } | ||
| 323 | elsif ($require eq "valid-user") { return OK } | ||
| 324 | elsif ($require eq 'group') { | ||
| 325 | @$require_groups = Text::ParseWords::parse_line('\s+', 0, $rest); | ||
| 326 | } | ||
| 327 | } | ||
| 328 | |||
| 329 | # Do we want Windows-like case-insensitivity? | ||
| 330 | if ($casesensitive eq 'off') { | ||
| 331 | $user_sent = lc($user_sent); | ||
| 332 | } | ||
| 333 | |||
| 334 | # Create the cache if needed | ||
| 335 | my $cache = Cache::FileCache->new({ namespace => $auth_name, | ||
| 336 | default_expires_in => $cache_time_limit, | ||
| 337 | cache_root => $cache_dir, | ||
| 338 | directory_umask => $cache_umask }); | ||
| 339 | my $user_groups = $cache->get($user_sent); | ||
| 340 | |||
| 341 | # Is the user in the cache | ||
| 342 | if ($user_groups) { | ||
| 343 | $r->log->debug("handler: using cached groups for $user_sent"); | ||
| 344 | |||
| 345 | my $success_groups = ''; | ||
| 346 | OUTTER: foreach my $req_group (@$require_groups) { | ||
| 347 | my $succeeded = 0; | ||
| 348 | INNER: foreach my $user_group (@$user_groups) { | ||
| 349 | $r->log->debug("handler: comparing $req_group to $user_group"); | ||
| 350 | if ($casesensitive eq 'off' && | ||
| 351 | lc($req_group) eq lc($user_group)) { | ||
| 352 | # Password matches so end stage | ||
| 353 | # The required patch was not introduced in 1.26. It is no longer | ||
| 354 | # promised to be included in any timeframe. Commenting out. | ||
| 355 | # if ($mod_perl::VERSION < 1.26) { | ||
| 356 | # Since set_handlers() doesn't work properly until | ||
| 357 | # 1.26 (according to Doug MacEachern) I have to work | ||
| 358 | # around it by cobbling together cheat sheets for the | ||
| 359 | # subsequent handlers in this phase. I get the | ||
| 360 | # willies about the security implications in a | ||
| 361 | # general environment where you might be using | ||
| 362 | # someone else's handlers upstream or downstream... | ||
| 363 | $r->log->debug("handler: user in cache and case-insensitive ", | ||
| 364 | "groups $req_group and $user_group match; ", | ||
| 365 | "appending to success list"); | ||
| 366 | if ($requirement == 1) { | ||
| 367 | $r->subprocess_env(REMOTE_GROUP => $user_group); | ||
| 368 | $r->headers_in->{'REMOTE_GROUP'} = $user_group; | ||
| 369 | $r->notes('AuthzCache' => 'hit'); | ||
| 370 | return OK; | ||
| 371 | } else { | ||
| 372 | $success_groups .= "\"$user_group\" "; | ||
| 373 | $succeeded = 1; | ||
| 374 | last INNER; | ||
| 375 | } | ||
| 376 | # } else { | ||
| 377 | # $r->log->debug("handler: user in cache and case-insensitive ", | ||
| 378 | # "groups $req_group and $user_group match; ", | ||
| 379 | # "returning OK and clearing PerlAuthzHandler"); | ||
| 380 | # $r->set_handlers(PerlAuthzHandler => undef); | ||
| 381 | #} | ||
| 382 | } elsif ($req_group eq $user_group) { | ||
| 383 | # Password matches so end stage | ||
| 384 | # The required patch was not introduced in 1.26. It is no longer | ||
| 385 | # promised to be included in any timeframe. Commenting out. | ||
| 386 | # if ($mod_perl::VERSION < 1.26) { | ||
| 387 | # Since set_handlers() doesn't work properly until | ||
| 388 | # 1.26 (according to Doug MacEachern) I have to work | ||
| 389 | # around it by cobbling together cheat sheets for the | ||
| 390 | # subsequent handlers in this phase. I get the | ||
| 391 | # willies about the security implications in a | ||
| 392 | # general environment where you might be using | ||
| 393 | # someone else's handlers upstream or downstream... | ||
| 394 | $r->log->debug("handler: user in cache and case-sensitive ", | ||
| 395 | "groups $req_group and $user_group match; ", | ||
| 396 | "appending to success list"); | ||
| 397 | if ($requirement == 1) { | ||
| 398 | $r->subprocess_env(REMOTE_GROUP => $user_group); | ||
| 399 | $r->headers_in->{'REMOTE_GROUP'} = $user_group; | ||
| 400 | $r->notes('AuthzCache' => 'hit'); | ||
| 401 | return OK; | ||
| 402 | } else { | ||
| 403 | $success_groups .= "\"$user_group\" "; | ||
| 404 | $succeeded = 1; | ||
| 405 | last INNER; | ||
| 406 | } | ||
| 407 | # } else { | ||
| 408 | # $r->log->debug("handler: user in cache and case-insensitive ", | ||
| 409 | # "groups $req_group and $user_group match; ", | ||
| 410 | # "returning OK and clearing PerlAuthzHandler"); | ||
| 411 | # $r->set_handlers(PerlAuthzHandler => undef); | ||
| 412 | #} | ||
| 413 | } # IF | ||
| 414 | } # INNER | ||
| 415 | if ($requirement == 3 && !$succeeded) { | ||
| 416 | $r->log->debug("handler: group $req_group not in cache for ", | ||
| 417 | "inAllGroups requirement; returning DECLINED"); | ||
| 418 | return DECLINED; | ||
| 419 | } | ||
| 420 | $succeeded = 0; | ||
| 421 | } # OUTTER | ||
| 422 | if ($success_groups ne '') { | ||
| 423 | chop($success_groups); | ||
| 424 | $r->log->debug("handler: user in cache; returning OK and setting ", | ||
| 425 | "environment to $success_groups and notes"); | ||
| 426 | $r->subprocess_env(REMOTE_GROUP => $success_groups); | ||
| 427 | $r->headers_in->{'REMOTE_GROUP'} = $success_groups; | ||
| 428 | $r->notes('AuthzCache' => 'hit'); | ||
| 429 | return OK; | ||
| 430 | } | ||
| 431 | } # USER_GROUPS | ||
| 432 | |||
| 433 | # User not in cache | ||
| 434 | $r->log->debug("handler: user/group not in cache; returning DECLINED"); | ||
| 435 | return DECLINED; | ||
| 436 | } | ||
| 437 | |||
| 438 | ############################################################################### | ||
| 439 | ############################################################################### | ||
| 440 | # manage_cache: insert new entries into the cache | ||
| 441 | ############################################################################### | ||
| 442 | ############################################################################### | ||
| 443 | sub manage_cache { | ||
| 444 | my $r = shift; | ||
| 445 | return OK unless $r->is_initial_req; # only the first internal request | ||
| 446 | my $requires = $r->requires; | ||
| 447 | return OK unless $requires; | ||
| 448 | |||
| 449 | # Get username | ||
| 450 | my $user_sent = $r->connection->user; | ||
| 451 | |||
| 452 | # Get required groups and proceed with caching only if groups were required | ||
| 453 | for my $req (@$requires) { | ||
| 454 | my ($require, $rest) = split /\s+/, $req->{requirement}, 2; | ||
| 455 | if ($require eq "user") { return OK | ||
| 456 | if grep $user_sent eq $_, split /\s+/, $rest } | ||
| 457 | elsif ($require eq "valid-user") { return OK } | ||
| 458 | } | ||
| 459 | |||
| 460 | my ($group_sent, $cache_result) = undef; | ||
| 461 | # The required patch was not introduced in 1.26. It is no longer | ||
| 462 | # promised to be included in any timeframe. Commenting out. | ||
| 463 | # if ($mod_perl::VERSION < 1.26) { | ||
| 464 | # I shouldn't need to use the below lines as this module | ||
| 465 | # should never be called if there was a cache hit. Since | ||
| 466 | # set_handlers() doesn't work properly until 1.26 (according | ||
| 467 | # to Doug MacEachern) I have to work around it by cobbling | ||
| 468 | # together cheat sheets for the previous handlers in this | ||
| 469 | # phase. I get the willies about the security implications in | ||
| 470 | # a general environment where you might be using someone | ||
| 471 | # else's handlers upstream or downstream... | ||
| 472 | $group_sent = $r->subprocess_env("REMOTE_GROUP") || | ||
| 473 | $r->headers_in->{'REMOTE_GROUP'}; | ||
| 474 | $cache_result = $r->notes('AuthzCache'); | ||
| 475 | if ($group_sent && $cache_result eq 'hit') { | ||
| 476 | $r->log->debug("manage_cache: upstream cache hit for ", | ||
| 477 | "username=$user_sent, group=$group_sent"); | ||
| 478 | return OK; | ||
| 479 | # } | ||
| 480 | } | ||
| 481 | |||
| 482 | # Get configuration | ||
| 483 | my $casesensitive = $r->dir_config('AuthzCache_CaseSensitive') || 'on'; | ||
| 484 | my $cache_time_limit = $r->dir_config('AuthzCache_CacheTime') || | ||
| 485 | $r->dir_config('AuthzCache_Timeout') || $Cache::Cache::EXPIRES_NEVER; | ||
| 486 | my $cache_dir = $r->dir_config('AuthzCache_Directory') || '/tmp'; | ||
| 487 | my $cache_umask = $r->dir_config('AuthzCache_Umask') || '007'; | ||
| 488 | my $auth_name = $r->auth_name; | ||
| 489 | $r->log->debug("manage_cache: cache_time_limit=$cache_time_limit, ", | ||
| 490 | "cache_dir=$cache_dir, cache_umask=$cache_umask, ", | ||
| 491 | "auth_name=$auth_name"); | ||
| 492 | |||
| 493 | # Do we want Windows-like case-insensitivity? | ||
| 494 | if ($casesensitive eq 'off') { | ||
| 495 | $user_sent = lc($user_sent); | ||
| 496 | $group_sent = lc($group_sent); | ||
| 497 | } | ||
| 498 | |||
| 499 | # Add groups to the cache | ||
| 500 | my $groups = []; # perl-5.8 chokes on declaring an array dereference | ||
| 501 | @$groups = Text::ParseWords::parse_line('\s+', 0, $group_sent); | ||
| 502 | my $cache = Cache::FileCache->new({ namespace => $auth_name, | ||
| 503 | default_expires_in => $cache_time_limit, | ||
| 504 | cache_root => $cache_dir, | ||
| 505 | directory_umask => $cache_umask }); | ||
| 506 | my $user_groups = $cache->get($user_sent); | ||
| 507 | if (ref($user_groups)) { | ||
| 508 | $cache->set($user_sent, [(@{$groups}, @{$user_groups})], $cache_time_limit); | ||
| 509 | } else { | ||
| 510 | $cache->set($user_sent, $groups, $cache_time_limit); | ||
| 511 | } | ||
| 512 | $r->log->debug("manage_cache: added $user_sent:$group_sent to the cache"); | ||
| 513 | |||
| 514 | return OK; | ||
| 515 | } | ||
| 516 | |||
| 517 | if (Apache->module("Apache::Status")) { | ||
| 518 | Apache::Status->menu_item('AuthzCache' => 'AuthzCache Menu Item', | ||
| 519 | \&status_menu); | ||
| 520 | } | ||
| 521 | |||
| 522 | ############################################################################### | ||
| 523 | ############################################################################### | ||
| 524 | # status_menu: provide status via Apache::Status on cache | ||
| 525 | ############################################################################### | ||
| 526 | ############################################################################### | ||
| 527 | sub status_menu { | ||
| 528 | my ($r, $q) = @_; | ||
| 529 | my @s; | ||
| 530 | |||
| 531 | my $cache_dir = $r->dir_config('AuthzCache_Directory') || '/tmp'; | ||
| 532 | my $cache = Cache::FileCache->new({ cache_root => $cache_dir }); | ||
| 533 | my @nss = $cache->get_namespaces(); | ||
| 534 | |||
| 535 | push(@s, "<TABLE BORDER=\"1\">\n<TR>\n<TD><STRONG>Namespace</STRONG></TD>\n", | ||
| 536 | "<TD><STRONG>UserID</STRONG></TD>\n", | ||
| 537 | "<TD><STRONG>Authorized Groups</STRONG></TD>\n", | ||
| 538 | "<TD><STRONG>Creation Date</STRONG></TD>\n", | ||
| 539 | "<TD><STRONG>Expiration Date</STRONG></TD>\n</TR>\n"); | ||
| 540 | |||
| 541 | foreach my $ns (sort(@nss)) { | ||
| 542 | $cache = Cache::FileCache->new({ cache_root => $cache_dir, | ||
| 543 | namespace => $ns }); | ||
| 544 | my @keys = $cache->get_keys(); | ||
| 545 | foreach my $key (sort(@keys)) { | ||
| 546 | my $obj = $cache->get_object($key); | ||
| 547 | my $user_groups = $obj->get_data; | ||
| 548 | my $created = Time::Object->new($obj->get_created_at); | ||
| 549 | my $expires = Time::Object->new($obj->get_expires_at); | ||
| 550 | push(@s, "<TR><TD VALIGN=\"TOP\">$ns</TD>\n", | ||
| 551 | "<TD VALIGN=\"TOP\">$key</TD>\n<TD VALIGN=\"TOP\">"); | ||
| 552 | foreach my $user_group (sort(@$user_groups)) { | ||
| 553 | next if $user_group eq ''; | ||
| 554 | push(@s, "$user_group<BR>\n"); | ||
| 555 | } | ||
| 556 | push(@s, "</TD>\n<TD VALIGN=\"TOP\">$created</TD>\n", | ||
| 557 | "<TD VALIGN=\"TOP\">$expires</TD>\n</TR>\n"); | ||
| 558 | } | ||
| 559 | } | ||
| 560 | |||
| 561 | push(@s, '</TABLE>'); | ||
| 562 | |||
| 563 | return \@s; | ||
| 564 | } | ||
| 565 | |||
| 566 | 1; | ||
| 567 | |||
| 568 | __END__ |