2003-03-15 21:20:41 +01:00
|
|
|
/*
|
|
|
|
* PROGRAM: Client/Server Common Code
|
|
|
|
* MODULE: dir_list.cpp
|
|
|
|
* DESCRIPTION: Directory listing config file operation
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Interbase Public
|
|
|
|
* License Version 1.0 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy
|
|
|
|
* of the License at http://www.Inprise.com/IPL.html
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an
|
|
|
|
* "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
|
|
|
|
* or implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
|
|
|
*
|
|
|
|
* Created by: Alex Peshkov <AlexPeshkov@users.sourceforge.net>
|
|
|
|
*
|
|
|
|
* All Rights Reserved.
|
|
|
|
* Contributor(s): ______________________________________.
|
|
|
|
*/
|
|
|
|
|
2003-03-15 21:20:42 +01:00
|
|
|
#include "firebird.h"
|
2003-03-15 21:20:41 +01:00
|
|
|
#include "../common/config/config.h"
|
|
|
|
#include "../common/config/dir_list.h"
|
|
|
|
#include "../jrd/os/path_utils.h"
|
|
|
|
|
|
|
|
#define New FB_NEW(*getDefaultMemoryPool())
|
2003-04-02 17:14:14 +02:00
|
|
|
#define NONE "-"
|
|
|
|
#define ROOT "/"
|
2003-03-15 21:20:41 +01:00
|
|
|
|
|
|
|
ParsedPath::ParsedPath(void) {
|
|
|
|
PathElem = 0;
|
|
|
|
nElem = 0;
|
|
|
|
}
|
|
|
|
|
2003-03-23 17:50:54 +01:00
|
|
|
ParsedPath::ParsedPath(const Firebird::string& path) {
|
2003-03-15 21:20:41 +01:00
|
|
|
PathElem = 0;
|
|
|
|
Parse(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
ParsedPath::~ParsedPath() {
|
|
|
|
delete[] PathElem;
|
|
|
|
}
|
|
|
|
|
2003-03-23 17:50:54 +01:00
|
|
|
void ParsedPath::Parse(const Firebird::string& path) {
|
2003-03-15 21:20:41 +01:00
|
|
|
delete PathElem;
|
2003-04-02 17:14:14 +02:00
|
|
|
|
|
|
|
if (path.length() == 1) {
|
|
|
|
nElem = 1;
|
|
|
|
PathElem = New Firebird::string[1];
|
|
|
|
PathElem[0] = path;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-03-15 21:20:41 +01:00
|
|
|
nElem = 0;
|
|
|
|
Firebird::string oldpath = path;
|
|
|
|
do {
|
|
|
|
Firebird::string newpath, elem;
|
|
|
|
PathUtils::splitLastComponent(newpath, elem, oldpath);
|
|
|
|
oldpath = newpath;
|
|
|
|
nElem++;
|
|
|
|
} while (oldpath.length() > 0);
|
|
|
|
|
|
|
|
PathElem = New Firebird::string[nElem];
|
|
|
|
oldpath = path;
|
2003-03-23 17:50:54 +01:00
|
|
|
for (int i = nElem; i--; ) {
|
2003-03-15 21:20:41 +01:00
|
|
|
Firebird::string newpath;
|
|
|
|
PathUtils::splitLastComponent(newpath, PathElem[i], oldpath);
|
|
|
|
oldpath = newpath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-03-23 17:50:54 +01:00
|
|
|
bool ParsedPath::operator==(const char* path) const {
|
2003-03-15 21:20:41 +01:00
|
|
|
return (Firebird::string(*this) == Firebird::string(path));
|
|
|
|
}
|
|
|
|
|
|
|
|
Firebird::string ParsedPath::SubPath(int n) const {
|
2003-03-23 17:50:54 +01:00
|
|
|
Firebird::string rc = PathElem[0];
|
2003-04-02 17:14:14 +02:00
|
|
|
if (PathUtils::isRelative(rc + PathUtils::dir_sep) && rc != NONE)
|
2003-03-23 17:50:54 +01:00
|
|
|
rc = PathUtils::dir_sep + rc;
|
|
|
|
for (int i = 1; i < n; i++) {
|
2003-03-15 21:20:41 +01:00
|
|
|
Firebird::string newpath;
|
|
|
|
PathUtils::concatPath(newpath, rc, PathElem[i]);
|
|
|
|
rc = newpath;
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2003-03-23 17:50:54 +01:00
|
|
|
ParsedPath::operator Firebird::string() const {
|
|
|
|
if (!PathElem)
|
2003-03-15 21:20:41 +01:00
|
|
|
return "";
|
|
|
|
return SubPath(nElem);
|
|
|
|
}
|
|
|
|
|
2003-03-23 17:50:54 +01:00
|
|
|
bool ParsedPath::Contains(const ParsedPath& pPath) const {
|
2003-04-02 17:14:14 +02:00
|
|
|
int nFullElem = nElem;
|
|
|
|
if (nFullElem > 1 && PathElem[nFullElem - 1].length() == 0)
|
|
|
|
nFullElem--;
|
|
|
|
|
|
|
|
if (pPath.nElem < nFullElem) {
|
2003-03-15 21:20:41 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
int i;
|
2003-04-02 17:14:14 +02:00
|
|
|
for (i = 0; i < nFullElem; i++) {
|
2003-03-23 17:50:54 +01:00
|
|
|
if (!PathUtils::comparePaths(pPath.PathElem[i], PathElem[i])) {
|
2003-03-15 21:20:41 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2003-04-02 17:14:14 +02:00
|
|
|
for (i = nFullElem + 1; i <= pPath.nElem; i++) {
|
2003-03-15 21:20:41 +01:00
|
|
|
Firebird::string x = pPath.SubPath(i);
|
|
|
|
if (PathUtils::isSymLink(x)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
DirectoryList::DirectoryList() {
|
|
|
|
ConfigDirs = 0;
|
|
|
|
nDirs = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
DirectoryList::~DirectoryList() {
|
|
|
|
delete[] ConfigDirs;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DirectoryList::Initialize(void) {
|
|
|
|
const Firebird::string val = GetConfigString();
|
|
|
|
nDirs = 1;
|
2003-04-02 13:14:29 +02:00
|
|
|
unsigned int i;
|
2003-03-23 17:50:54 +01:00
|
|
|
for (i = 0; i < val.length(); i++) {
|
2003-03-15 21:20:41 +01:00
|
|
|
if (val[i] == ';') {
|
|
|
|
nDirs++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ConfigDirs = New ParsedPath[nDirs];
|
2003-04-02 13:14:29 +02:00
|
|
|
unsigned int Last = 0;
|
2003-03-15 21:20:41 +01:00
|
|
|
nDirs = 0;
|
|
|
|
Firebird::string Root = Config::getRootDirectory();
|
2003-03-23 17:50:54 +01:00
|
|
|
for (i = 0; i < val.length(); i++) {
|
2003-03-15 21:20:41 +01:00
|
|
|
if (val[i] == ';') {
|
|
|
|
Firebird::string dir = "";
|
|
|
|
if (i > Last) {
|
|
|
|
dir = val.substr(Last, i-Last);
|
|
|
|
}
|
2003-04-02 17:14:14 +02:00
|
|
|
if (PathUtils::isRelative(dir) && dir != NONE && dir != ROOT) {
|
2003-03-15 21:20:41 +01:00
|
|
|
Firebird::string newdir;
|
|
|
|
PathUtils::concatPath(newdir, Root, dir);
|
|
|
|
dir = newdir;
|
|
|
|
}
|
|
|
|
ConfigDirs[nDirs++].Parse(dir);
|
|
|
|
Last = i + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Firebird::string dir = "";
|
|
|
|
if (i > Last) {
|
2003-03-23 17:50:54 +01:00
|
|
|
dir = val.substr(Last, i - Last);
|
2003-03-15 21:20:41 +01:00
|
|
|
}
|
2003-04-02 17:14:14 +02:00
|
|
|
if (PathUtils::isRelative(dir) && dir != NONE && dir != ROOT) {
|
2003-03-15 21:20:41 +01:00
|
|
|
Firebird::string newdir;
|
|
|
|
PathUtils::concatPath(newdir, Root, dir);
|
|
|
|
dir = newdir;
|
|
|
|
}
|
|
|
|
ConfigDirs[nDirs++].Parse(dir);
|
|
|
|
}
|
|
|
|
|
2003-03-23 17:50:54 +01:00
|
|
|
bool DirectoryList::IsPathInList(const Firebird::string& path) {
|
|
|
|
if (!ConfigDirs)
|
2003-03-15 21:20:41 +01:00
|
|
|
Initialize();
|
|
|
|
|
2003-04-03 15:32:28 +02:00
|
|
|
// Handle special case 1
|
|
|
|
if (ConfigDirs[0] == ROOT) // all open
|
|
|
|
return true;
|
|
|
|
|
2003-03-15 21:20:41 +01:00
|
|
|
// Disable any up-dir(..) references - in case our path_utils
|
|
|
|
// and OS handle paths in slightly different ways,
|
|
|
|
// this is "wonderful" potential hole for hacks
|
|
|
|
// Example of IIS attack attempt:
|
|
|
|
// "GET /scripts/..%252f../winnt/system32/cmd.exe?/c+dir HTTP/1.0"
|
|
|
|
// (live from apache access log :)
|
|
|
|
if (path.find(PathUtils::up_dir_link) != Firebird::string::npos)
|
|
|
|
return false;
|
|
|
|
|
2003-04-03 15:32:28 +02:00
|
|
|
// Handle special case 2
|
2003-04-02 17:14:14 +02:00
|
|
|
if (nDirs == 1 && ConfigDirs[0] == NONE) // all closed
|
2003-03-23 17:50:54 +01:00
|
|
|
return false;
|
2003-03-15 21:20:41 +01:00
|
|
|
|
|
|
|
Firebird::string varpath = path;
|
|
|
|
if (PathUtils::isRelative(path)) {
|
|
|
|
PathUtils::concatPath(varpath,
|
|
|
|
Firebird::string(Config::getRootDirectory()), path);
|
|
|
|
}
|
|
|
|
|
|
|
|
ParsedPath pPath = path;
|
|
|
|
bool rc = 0;
|
2003-03-23 17:50:54 +01:00
|
|
|
for (int i = 0; i < nDirs; i++) {
|
2003-03-15 21:20:41 +01:00
|
|
|
if (ConfigDirs[i].Contains(pPath)) {
|
|
|
|
rc = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
2003-03-23 17:50:54 +01:00
|
|
|
|
|
|
|
void DirectoryList::ExpandFileName (
|
|
|
|
Firebird::string & Path,
|
|
|
|
const Firebird::string & Name,
|
|
|
|
int Access
|
|
|
|
) {
|
|
|
|
if (!ConfigDirs)
|
|
|
|
Initialize();
|
|
|
|
for (int i = 0; i < nDirs; i++) {
|
|
|
|
PathUtils::concatPath(Path, ConfigDirs[i], Name);
|
|
|
|
if (PathUtils::canAccess(Path, Access)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Path = Name;
|
|
|
|
}
|