From 36f1098aed548651a32a2c15cc9ad80c4330b4d9 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Wed, 12 Jan 2011 17:09:04 +0100 Subject: * apt-pkg/contrib/fileutl.cc: - add a RealFileExists method and check that your configuration files are real files to avoid endless loops if not (Closes: #604401) --- apt-pkg/contrib/fileutl.cc | 20 +++++++++++++++++++- apt-pkg/contrib/fileutl.h | 1 + apt-pkg/depcache.cc | 4 ++-- apt-pkg/init.cc | 6 +++--- apt-pkg/policy.cc | 2 +- apt-pkg/sourcelist.cc | 6 +++--- apt-pkg/vendorlist.cc | 4 ++-- 7 files changed, 31 insertions(+), 12 deletions(-) (limited to 'apt-pkg') diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index f4ab066d7..db6057ea3 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -191,7 +191,7 @@ int GetLock(string File,bool Errors) /*}}}*/ // FileExists - Check if a file exists /*{{{*/ // --------------------------------------------------------------------- -/* */ +/* Beware: Directories are also files! */ bool FileExists(string File) { struct stat Buf; @@ -200,6 +200,17 @@ bool FileExists(string File) return true; } /*}}}*/ +// RealFileExists - Check if a file exists and if it is really a file /*{{{*/ +// --------------------------------------------------------------------- +/* */ +bool RealFileExists(string File) +{ + struct stat Buf; + if (stat(File.c_str(),&Buf) != 0) + return false; + return ((Buf.st_mode & S_IFREG) != 0); +} + /*}}}*/ // DirectoryExists - Check if a directory exists and is really one /*{{{*/ // --------------------------------------------------------------------- /* */ @@ -304,6 +315,13 @@ std::vector GetListOfFilesInDir(string const &Dir, std::vector c } std::vector List; + + if (DirectoryExists(Dir.c_str()) == false) + { + _error->Error(_("List of files can't be created as '%s' is not a directory"), Dir.c_str()); + return List; + } + Configuration::MatchAgainstConfig SilentIgnore("Dir::Ignore-Files-Silently"); DIR *D = opendir(Dir.c_str()); if (D == 0) diff --git a/apt-pkg/contrib/fileutl.h b/apt-pkg/contrib/fileutl.h index 1380f06b4..146d917d8 100644 --- a/apt-pkg/contrib/fileutl.h +++ b/apt-pkg/contrib/fileutl.h @@ -93,6 +93,7 @@ bool RunScripts(const char *Cnf); bool CopyFile(FileFd &From,FileFd &To); int GetLock(string File,bool Errors = true); bool FileExists(string File); +bool RealFileExists(string File); bool DirectoryExists(string const &Path) __attrib_const; bool CreateDirectory(string const &Parent, string const &Path); diff --git a/apt-pkg/depcache.cc b/apt-pkg/depcache.cc index 594c085a5..d2557386d 100644 --- a/apt-pkg/depcache.cc +++ b/apt-pkg/depcache.cc @@ -167,7 +167,7 @@ bool pkgDepCache::readStateFile(OpProgress *Prog) /*{{{*/ { FileFd state_file; string const state = _config->FindFile("Dir::State::extended_states"); - if(FileExists(state)) { + if(RealFileExists(state)) { state_file.Open(state, FileFd::ReadOnly); int const file_size = state_file.Size(); if(Prog != NULL) @@ -226,7 +226,7 @@ bool pkgDepCache::writeStateFile(OpProgress *prog, bool InstalledOnly) /*{{{*/ string const state = _config->FindFile("Dir::State::extended_states"); // if it does not exist, create a empty one - if(!FileExists(state)) + if(!RealFileExists(state)) { StateFile.Open(state, FileFd::WriteAtomic); StateFile.Close(); diff --git a/apt-pkg/init.cc b/apt-pkg/init.cc index f0bad78df..734f5b2c4 100644 --- a/apt-pkg/init.cc +++ b/apt-pkg/init.cc @@ -94,10 +94,10 @@ bool pkgInitConfig(Configuration &Cnf) const char *Cfg = getenv("APT_CONFIG"); if (Cfg != 0) { - if (FileExists(Cfg) == true) + if (RealFileExists(Cfg) == true) Res &= ReadConfigFile(Cnf,Cfg); else - _error->WarningE("FileExists",_("Unable to read %s"),Cfg); + _error->WarningE("RealFileExists",_("Unable to read %s"),Cfg); } // Read the configuration parts dir @@ -109,7 +109,7 @@ bool pkgInitConfig(Configuration &Cnf) // Read the main config file string FName = Cnf.FindFile("Dir::Etc::main"); - if (FileExists(FName) == true) + if (RealFileExists(FName) == true) Res &= ReadConfigFile(Cnf,FName); if (Res == false) diff --git a/apt-pkg/policy.cc b/apt-pkg/policy.cc index 4f9d56775..f05b6ca49 100644 --- a/apt-pkg/policy.cc +++ b/apt-pkg/policy.cc @@ -328,7 +328,7 @@ bool ReadPinFile(pkgPolicy &Plcy,string File) if (File.empty() == true) File = _config->FindFile("Dir::Etc::Preferences"); - if (FileExists(File) == false) + if (RealFileExists(File) == false) return true; FileFd Fd(File,FileFd::ReadOnly); diff --git a/apt-pkg/sourcelist.cc b/apt-pkg/sourcelist.cc index c3ec9865a..851eefdfe 100644 --- a/apt-pkg/sourcelist.cc +++ b/apt-pkg/sourcelist.cc @@ -197,7 +197,7 @@ bool pkgSourceList::ReadMainList() string Main = _config->FindFile("Dir::Etc::sourcelist"); string Parts = _config->FindDir("Dir::Etc::sourceparts"); - if (FileExists(Main) == true) + if (RealFileExists(Main) == true) Res &= ReadAppend(Main); else if (DirectoryExists(Parts) == false) // Only warn if there are no sources.list.d. @@ -205,9 +205,9 @@ bool pkgSourceList::ReadMainList() if (DirectoryExists(Parts) == true) Res &= ReadSourceDir(Parts); - else if (FileExists(Main) == false) + else if (RealFileExists(Main) == false) // Only warn if there is no sources.list file. - _error->WarningE("FileExists", _("Unable to read %s"), Main.c_str()); + _error->WarningE("RealFileExists", _("Unable to read %s"), Main.c_str()); return Res; } diff --git a/apt-pkg/vendorlist.cc b/apt-pkg/vendorlist.cc index 589997081..92ff38894 100644 --- a/apt-pkg/vendorlist.cc +++ b/apt-pkg/vendorlist.cc @@ -21,11 +21,11 @@ bool pkgVendorList::ReadMainList() Configuration Cnf; string CnfFile = _config->FindDir("Dir::Etc::vendorparts"); - if (FileExists(CnfFile) == true) + if (DirectoryExists(CnfFile) == true) if (ReadConfigDir(Cnf,CnfFile,true) == false) return false; CnfFile = _config->FindFile("Dir::Etc::vendorlist"); - if (FileExists(CnfFile) == true) + if (RealFileExists(CnfFile) == true) if (ReadConfigFile(Cnf,CnfFile,true) == false) return false; -- cgit v1.2.3