@@ -811,6 +811,7 @@ def __init__(self, out_file=None, out_file_type=None):
811811 self .out_file = out_file
812812 self .out_file_type = out_file_type
813813 self .out_dict = OrderedDict ({"errors" :{}})
814+ self ._validate ()
814815
815816 def update (self , content ):
816817 # The following is needed, since python3 does not support nested merge of
@@ -863,6 +864,21 @@ def _write_yaml(self):
863864 return False
864865 return True
865866
867+ def _validate (self ):
868+ basename = os .path .basename (self .out_file )
869+ directory = os .path .dirname (self .out_file )
870+
871+ if not os .path .isdir (directory ):
872+ raise ValueError ( f'The given output directory { directory } does not exist' )
873+ if not os .access (directory , os .W_OK ):
874+ raise ValueError ( f'The given output directory { directory } is not writable' )
875+ if not basename :
876+ raise ValueError (f'Please add an output file name, only an output directory { directory } was specified' )
877+ if 'json' in self .out_file_type and os .path .exists (f'{ self .out_file } .json' ) and not os .access (f'{ self .out_file } .json' , os .W_OK ):
878+ raise ValueError (f'The given output file { self .out_file } .json exists, but is not writable' )
879+ if 'yaml' in self .out_file_type and os .path .exists (f'{ self .out_file } .yaml' ) and not os .access (f'{ self .out_file } .yaml' , os .W_OK ):
880+ raise ValueError (f'The given output file { self .out_file } .yaml exists, but is not writable' )
881+
866882 def as_dict (self ):
867883 return self .out_dict
868884
@@ -2822,14 +2838,17 @@ class Enumerator():
28222838 def __init__ (self , args ):
28232839
28242840 # Init output files
2825- if args .out_json_file :
2826- output = Output (args .out_json_file , "json" )
2827- elif args .out_yaml_file :
2828- output = Output (args .out_yaml_file , "yaml" )
2829- elif args .out_file :
2830- output = Output (args .out_file , "json_yaml" )
2831- else :
2832- output = Output ()
2841+ try :
2842+ if args .out_json_file :
2843+ output = Output (args .out_json_file , "json" )
2844+ elif args .out_yaml_file :
2845+ output = Output (args .out_yaml_file , "yaml" )
2846+ elif args .out_file :
2847+ output = Output (args .out_file , "json_yaml" )
2848+ else :
2849+ output = Output ()
2850+ except Exception as e :
2851+ raise RuntimeError (str (e ))
28332852
28342853 # Init target and creds
28352854 try :
@@ -3151,16 +3170,6 @@ def valid_domain(domain):
31513170 return True
31523171 return False
31533172
3154- def valid_path (path ):
3155- basename = os .path .basename (path )
3156- directory = os .path .dirname (path )
3157- if os .path .isdir (directory ) and os .access (directory , os .W_OK ):
3158- if basename :
3159- return Result (True , '' )
3160- else :
3161- return Result (False , f'Please add an output file name, only an output directory { path } was specified' )
3162- return Result (False , f'The given output directory { path } does not exist or is not writable' )
3163-
31643173def valid_file (file , mode = None ):
31653174 if not os .path .exists (file ):
31663175 return Result (False , f'File { file } does not exist' )
@@ -3381,12 +3390,6 @@ def check_arguments():
33813390 raise RuntimeError ("Timeout must be a valid integer in the range 1-600" )
33823391 args .timeout = int (args .timeout )
33833392
3384- # Write permission check when using -oY and -oJ
3385- if args .out_file or args .out_yaml_file or args .out_json_file :
3386- result = valid_path (args .out_file or args .out_yaml_file or args .out_json_file )
3387- if not result .retval :
3388- raise RuntimeError (result .retmsg )
3389-
33903393 # Perform Samba version checks - TODO: Can be removed in the future
33913394 samba_version = re .match (r".*(\d+\.\d+\.\d+).*" , check_output (["smbclient" , "--version" ]).decode ()).group (1 )
33923395 samba_version = tuple (int (x ) for x in samba_version .split ('.' ))
0 commit comments