17 #include "../../bscript/berror.h" 18 #include "../../bscript/bobject.h" 19 #include "../../bscript/impstr.h" 20 #include "../../clib/cfgelem.h" 21 #include "../../clib/cfgfile.h" 22 #include "../../clib/clib.h" 23 #include "../../clib/dirlist.h" 24 #include "../../clib/fileutil.h" 25 #include "../../clib/logfacility.h" 26 #include "../../plib/pkg.h" 27 #include "../binaryfilescrobj.h" 29 #include "../globals/ucfg.h" 30 #include "../xmlfilescrobj.h" 38 using namespace Module;
55 using namespace Bscript;
113 : AllowWrite( elem.remove_bool(
"AllowWrite", false ) ),
114 AllowAppend( elem.remove_bool(
"AllowAppend", false ) ),
115 AllowRead( elem.remove_bool(
"AllowRead", false ) ),
116 AllowRemote( elem.remove_bool(
"AllowRemote", false ) ),
117 AllPackages( false ),
118 AllDirectories( false ),
119 AllExtensions( false )
129 if ( pkg ==
nullptr )
130 ERROR_PRINT <<
"Fileaccess package entry not found: " << tmp <<
"\n";
143 Directories.push_back( std::make_pair( cfgpkg, cfgpath ) );
145 ERROR_PRINT <<
"Invalid fileaccess Directory entry: " << tmp <<
"\n";
164 if ( pkg == filepackage )
175 if ( pkg ==
nullptr )
188 std::string filepath = path;
195 const auto& cfgpkg = dir.first;
196 const auto& cfgpath = dir.second;
197 if ( cfgpkg == filepkg )
199 if ( filepath.size() < cfgpath.size() )
205 for (
size_t i = 0; i < filepath.size() && i < cfgpath.size(); ++i )
207 if ( filepath[i] != cfgpath[i] )
224 if ( path.find(
'\0' ) != std::string::npos )
226 if ( path.find(
".." ) != std::string::npos )
230 if ( path.size() >= ext.size() )
232 std::string path_ext = path.substr( path.size() - ext.size() );
245 size +=
Packages.size() * (
sizeof(
void* ) + 3 *
sizeof(
void* ) );
248 size +=
sizeof( decltype( Directories )::value_type ) + d.second.capacity();
250 size += e.capacity();
255 const std::string& path )
259 if ( fa.AllowRead && fa.AllowsAccessTo( pkg, filepackage ) && fa.AppliesToPackage( pkg ) &&
260 fa.AppliesToPath( path, filepackage ) )
268 const std::string& path )
272 if ( fa.AllowWrite && fa.AllowsAccessTo( pkg, filepackage ) && fa.AppliesToPackage( pkg ) &&
273 fa.AppliesToPath( path, filepackage ) )
281 const std::string& path )
285 if ( fa.AllowAppend && fa.AllowsAccessTo( pkg, filepackage ) && fa.AppliesToPackage( pkg ) &&
286 fa.AppliesToPath( path, filepackage ) )
308 return new BError(
"Invalid parameter type." );
313 return new BError(
"Error in filename descriptor." );
315 if ( path.find(
".." ) != std::string::npos )
316 return new BError(
"No parent path traversal allowed." );
318 std::string filepath;
319 if ( outpkg ==
nullptr )
322 filepath = outpkg->
dir() + path;
331 return new BError(
"Invalid parameter type" );
336 return new BError(
"Error in filename descriptor" );
338 if ( path.find(
".." ) != std::string::npos )
339 return new BError(
"No parent path traversal please." );
342 return new BError(
"Access denied" );
344 std::string filepath;
345 if ( outpkg ==
nullptr )
348 filepath = outpkg->
dir() + path;
350 std::ifstream ifs( filepath.c_str() );
351 if ( !ifs.is_open() )
352 return new BError(
"File not found: " + filepath );
357 while ( getline( ifs, line ) )
358 arr->addElement(
new String( line ) );
360 return arr.release();
369 return new BError(
"Invalid parameter type" );
374 return new BError(
"Error in filename descriptor" );
376 if ( path.find(
".." ) != std::string::npos )
377 return new BError(
"No parent path traversal please." );
379 return new BError(
"Access denied" );
381 std::string filepath;
382 if ( outpkg ==
nullptr )
385 filepath = outpkg->
dir() + path;
387 std::string bakpath = filepath +
".bak";
388 std::string tmppath = filepath +
".tmp";
390 std::ofstream ofs( tmppath.c_str(), std::ios::out | std::ios::trunc );
392 if ( !ofs.is_open() )
393 return new BError(
"File not found: " + filepath );
395 for (
unsigned i = 0; i < contents->
ref_arr.size(); ++i )
399 if ( obj !=
nullptr )
401 ofs << ( *obj )->getStringRep();
406 return new BError(
"Error during write." );
411 if ( unlink( bakpath.c_str() ) )
414 std::string message =
"Unable to remove " + filepath +
": " + strerror( err );
416 return new BError( message );
421 if ( rename( filepath.c_str(), bakpath.c_str() ) )
424 std::string message =
425 "Unable to rename " + filepath +
" to " + bakpath +
": " + strerror( err );
426 return new BError( message );
429 if ( rename( tmppath.c_str(), filepath.c_str() ) )
432 std::string message =
433 "Unable to rename " + tmppath +
" to " + filepath +
": " + strerror( err );
434 return new BError( message );
437 return new BLong( 1 );
446 return new BError(
"Invalid parameter type" );
452 return new BError(
"Error in filename descriptor" );
454 if ( path.find(
".." ) != std::string::npos )
455 return new BError(
"No parent path traversal please." );
458 return new BError(
"Access denied" );
460 std::string filepath;
461 if ( outpkg ==
nullptr )
464 filepath = outpkg->
dir() + path;
466 std::ofstream ofs( filepath.c_str(), std::ios::out | std::ios::app );
468 if ( !ofs.is_open() )
469 return new BError(
"Unable to open file: " + filepath );
471 for (
unsigned i = 0; i < contents->
ref_arr.size(); ++i )
475 if ( obj !=
nullptr )
477 ofs << ( *obj )->getStringRep();
482 return new BError(
"Error during write." );
484 return new BLong( 1 );
497 return new BError(
"Invalid parameter type" );
507 return new BError(
"Error in filename descriptor" );
509 if ( path.find(
".." ) != std::string::npos )
510 return new BError(
"No parent path traversal please." );
513 return new BError(
"Access denied" );
515 std::string filepath;
516 if ( outpkg ==
nullptr )
519 filepath = outpkg->
dir() + path;
521 std::ofstream ofs( filepath.c_str(), std::ios::out | std::ios::app );
523 if ( !ofs.is_open() )
524 return new BError(
"Unable to open file: " + filepath );
531 if ( strftime( buffer,
sizeof buffer,
"%m/%d %H:%M:%S", &time_tm ) > 0 )
532 ofs <<
"[" << buffer <<
"] ";
535 ofs << textline->
value() << std::endl;
538 return new BError(
"Error during write." );
540 return new BLong( 1 );
543 return new BError(
"Invalid parameter type" );
549 unsigned short mode, bigendian;
552 return new BError(
"Invalid parameter type" );
557 return new BError(
"Error in filename descriptor" );
559 if ( path.find(
".." ) != std::string::npos )
560 return new BError(
"No parent path traversal please." );
565 return new BError(
"Access denied" );
570 return new BError(
"Access denied" );
573 std::string filepath;
574 if ( outpkg ==
nullptr )
577 filepath = outpkg->
dir() + path;
586 return new BError(
"Invalid parameter type" );
591 return new BError(
"Error in dirname descriptor" );
592 if ( path.find(
".." ) != std::string::npos )
593 return new BError(
"No parent path traversal please." );
595 if ( outpkg !=
nullptr )
596 path = outpkg->
dir() + path;
599 return new BError(
"Directory already exists." );
602 return new BError(
"Could not create directory." );
603 return new BLong( 1 );
613 return new BError(
"Invalid parameter type" );
618 return new BError(
"Error in dirname descriptor" );
619 if ( path.find(
".." ) != std::string::npos )
620 return new BError(
"No parent path traversal please." );
622 if ( outpkg !=
nullptr )
623 path = outpkg->
dir() + path;
626 return new BError(
"Directory not found." );
627 bool asterisk =
false;
628 bool nofiles =
false;
629 if ( extension->
getStringRep().find(
'*', 0 ) != std::string::npos )
631 else if ( extension->
length() == 0 )
638 std::string
name = dl.name();
639 if ( name[0] ==
'.' )
649 else if ( !asterisk )
651 std::string::size_type extensionPointPos = name.rfind(
'.' );
652 if ( extensionPointPos == std::string::npos )
654 if ( name.substr( extensionPointPos + 1 ) != extension->
value() )
668 return new BError(
"Invalid parameter type" );
673 return new BError(
"Error in filename descriptor" );
675 if ( path.find(
".." ) != std::string::npos )
676 return new BError(
"No parent path traversal please." );
679 return new BError(
"Access denied" );
681 std::string filepath;
682 if ( outpkg ==
nullptr )
685 filepath = outpkg->
dir() + path;
706 while ( cf.
read( elem ) )
bool AppliesToPackage(const Plib::Package *pkg) const
bool HasWriteAccess(const Plib::Package *pkg, const Plib::Package *filepackage, const std::string &path)
Bscript::BObjectImp * mf_OpenXMLFile()
const EScriptProgram * prog() const
const std::string & value() const
Bscript::BObjectImp * mf_CreateXMLFile()
std::vector< FunctionDef > FunctionTable
int strip_one(std::string &direc)
std::vector< std::string > Extensions
std::tm localtime(const std::time_t &t)
threadsafe version of localtime
bool getParam(unsigned param, int &value)
Bscript::BObjectImp * mf_LogToFile()
FileAccessExecutorModule(Bscript::Executor &exec)
virtual std::string getStringRep() const POL_OVERRIDE
std::vector< Module::FileAccess > file_access_rules
Bscript::BObjectImp * mf_CreateDirectory()
Package * find_package(const std::string &pkgname)
bool AllowsAccessTo(const Plib::Package *pkg, const Plib::Package *filepackage) const
bool pkgdef_split(const std::string &spec, const Package *inpkg, const Package **outpkg, std::string *path)
bool IsDirectory(const char *dir)
Bscript::BObjectImp * mf_WriteFile()
std::vector< BObjectRef > fparams
ConfigurationBuffer configurationbuffer
bool HasAppendAccess(const Plib::Package *pkg, const Plib::Package *filepackage, const std::string &path)
void load_fileaccess_cfg()
Bscript::BObjectImp * mf_FileExists()
std::set< const Plib::Package * > Packages
Bscript::BObjectImp * mf_OpenBinaryFile()
const String * getStringParam(unsigned param)
size_t estimateSize() const
bool AppliesToPath(const std::string &path, const Plib::Package *filepkg) const
Bscript::BObjectImp * mf_AppendToFile()
FileAccess(Clib::ConfigElem &elem)
std::vector< std::pair< const Plib::Package *, std::string > > Directories
bool remove_prop(const char *propname, std::string *value)
int stringicmp(const S1 &str1, const S2 &str2)
void addElement(BObjectImp *imp)
Plib::Package const * pkg
Bscript::BObjectImp * mf_ListDirectory()
ExecutorModule * CreateFileAccessExecutorModule(Executor &exec)
bool getObjArrayParam(unsigned param, ObjArray *&pobjarr)
Bscript::BObjectImp * mf_ReadFile()
bool HasReadAccess(const Plib::Package *pkg, const Plib::Package *filepackage, const std::string &path)
std::string normalized_dir_form(const std::string &istr)
bool FileExists(const char *filename)
bool read(ConfigElem &elem)
const std::string & dir() const
int make_dir(const char *dir)