1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
<?php
// WebSVN - Subversion repository viewing via the web using PHP
// Copyright (C) 2004-2006 Tim Armes
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// --
//
// auth.php
//
// Handle reading and interpretation of an SVN auth file
require_once 'include/accessfile.php';
define('UNDEFINED', 0);
define('ALLOW', 1);
define('DENY', 2);
class Authentication {
var $rights;
var $user = null;
var $usersGroups = array();
var $basicRealm = 'WebSVN';
// {{{ __construct
function Authentication($accessfile, $basicRealm = false) {
$this->rights = new IniFile();
$this->rights->readIniFile($accessfile);
$this->setUsername();
$this->identifyGroups();
if ($basicRealm !== false) {
$this->basicRealm = $basicRealm;
}
}
// }}}
function hasUsername() {
return $this->user !== null;
}
function getBasicRealm() {
return $this->basicRealm;
}
// {{{ setUsername()
//
// Set the username from the current http session
function setUsername() {
if (isset($_SERVER['REMOTE_USER'])) {
$this->user = $_SERVER['REMOTE_USER'];
} else if (isset($_SERVER['REDIRECT_REMOTE_USER'])) {
$this->user = $_SERVER['REDIRECT_REMOTE_USER'];
} else if (isset($_SERVER['PHP_AUTH_USER'])) {
$this->user = $_SERVER['PHP_AUTH_USER'];
}
}
// }}}
// {{{ identifyGroups()
//
// Checks to see which groups and aliases the user belongs to
function identifyGroups() {
$this->usersGroups[] = '*';
$aliases = $this->rights->getValues('aliases');
if (is_array($aliases)) {
foreach ($aliases as $alias => $user) {
if ($user == strtolower($this->user)) {
$this->usersGroups[] = '&'.$alias;
}
}
}
$groups = $this->rights->getValues('groups');
if (is_array($groups)) {
foreach ($groups as $group => $names) {
if (empty($names))
continue;
if (in_array(strtolower($this->user), preg_split('/\s*,\s*/', $names))) {
$this->usersGroups[] = '@'.$group;
}
foreach ($this->usersGroups as $users_group) {
if (in_array($users_group, preg_split('/\s*,\s*/', $names))) {
$this->usersGroups[] = '@'.$group;
}
}
}
}
}
// }}}
// {{{ inList
//
// Check if the user is in the given list and return their read status
// if they are (UNDEFINED, ALLOW or DENY)
function inList($accessors, $user) {
$output = UNDEFINED;
foreach ($accessors as $key => $rights) {
if (in_array($key, $this->usersGroups) || strcasecmp($key, $user) === 0) {
if (strpos($rights, 'r') !== false) {
return ALLOW;
} else {
$output = DENY;
}
}
}
return $output;
}
// }}}
// {{{ hasReadAccess
//
// Returns true if the user has read access to the given path
function hasReadAccess($repos, $path, $checkSubFolders = false) {
$access = UNDEFINED;
$repos = strtolower($repos); // .ini parser converts groups to lower-case
$path = strtolower($path);
if ($path == '' || $path{0} != '/') {
$path = '/'.$path;
}
// If were told to, we should check sub folders of the path to see if there's
// a read access below this level. This is used to display the folders needed
// to get to the folder to which read access is granted.
if ($checkSubFolders) {
$sections = $this->rights->getSections();
foreach ($sections as $section => $accessers) {
$qualified = $repos.':'.$path;
$len = strlen($qualified);
if ($len < strlen($section) && strncmp($section, $qualified, $len) == 0) {
$access = $this->inList($accessers, $this->user);
}
if ($access != ALLOW) {
$len = strlen($path);
if ($len < strlen($section) && strncmp($section, $path, $len) == 0) {
$access = $this->inList($accessers, $this->user);
}
}
if ($access == ALLOW) {
break;
}
}
}
// If we still don't have access, check each subpath of the path until we find an
// access level...
if ($access != ALLOW) {
$access = UNDEFINED;
if ($path != '/' && substr($path, -1) == '/') {
$path = substr($path, 0, -1);
}
do {
$accessers = $this->rights->getValues($repos.':'.$path);
if (!empty($accessers)) {
$access = $this->inList($accessers, $this->user);
}
if ($access == UNDEFINED) {
$accessers = $this->rights->getValues($path);
if (!empty($accessers)) {
$access = $this->inList($accessers, $this->user);
}
}
// If we've not got a match, remove the sub directory and start again
if ($access == UNDEFINED) {
if ($path == '/') {
break;
}
$path = substr($path, 0, strrpos($path, '/'));
if ($path == '') $path = '/';
}
} while ($access == UNDEFINED && $path != '');
}
return $access == ALLOW;
}
// }}}
// {{{ hasUnrestrictedReadAccess
//
// Returns true if the user has read access to the given path and too
// all subfolders
function hasUnrestrictedReadAccess($repos, $path) {
// First make sure that we have full read access at this level
if (!$this->hasReadAccess($repos, $path, false)) {
return false;
}
// Now check to see if there is a sub folder that's protected
$repos = strtolower($repos); // .ini parser converts groups to lower-case
$path = strtolower($path);
if ($path != '/' && substr($path, -1) == '/') {
$path = substr($path, 0, -1);
}
$sections = $this->rights->getSections();
foreach ($sections as $section => $accessers) {
$qualified = $repos.':'.$path;
$len = strlen($qualified);
$access = UNDEFINED;
if ($len <= strlen($section) && strncmp($section, $qualified, $len) == 0) {
$access = $this->inList($accessers, $this->user);
}
if ($access != DENY) {
$len = strlen($path);
if ($len <= strlen($section) && strncmp($section, $path, $len) == 0) {
$access = $this->inList($accessers, $this->user);
}
}
if ($access == DENY) {
return false;
}
}
return true;
}
// }}}
}
|