--- parser3/src/classes/file.C 2009/05/25 08:11:40 1.192 +++ parser3/src/classes/file.C 2009/08/08 13:30:20 1.199 @@ -5,7 +5,7 @@ Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char * const IDENT_FILE_C="$Date: 2009/05/25 08:11:40 $"; +static const char * const IDENT_FILE_C="$Date: 2009/08/08 13:30:20 $"; #include "pa_config_includes.h" @@ -28,8 +28,6 @@ static const char * const IDENT_FILE_C=" // defines -#define TEXT_MODE_NAME "text" -#define BINARY_MODE_NAME "binary" #define STDIN_EXEC_PARAM_NAME "stdin" #define CHARSET_EXEC_PARAM_NAME "charset" @@ -45,7 +43,7 @@ extern String sql_offset_name; class MFile: public Methoded { public: // VStateless_class - Value* create_new_value(Pool&, HashStringValue&) { return new VFile(); } + Value* create_new_value(Pool&) { return new VFile(); } public: // Methoded bool used_directly() { return true; } @@ -114,13 +112,13 @@ static const String::Body cdate_name("cd // methods static bool is_valid_mode (const String& mode) { - return (mode==TEXT_MODE_NAME || mode==BINARY_MODE_NAME); + return (mode==text_mode_name || mode==binary_mode_name); } static bool is_text_mode(const String& mode) { - if(mode==TEXT_MODE_NAME) + if(mode==text_mode_name) return true; - if(mode==BINARY_MODE_NAME) + if(mode==binary_mode_name) return false; throw Exception(PARSER_RUNTIME, &mode, @@ -216,11 +214,11 @@ static void _load(Request& r, MethodPara if(param_index>1){ const String& luser_file_name=params.as_string(param_index, FILE_NAME_MUST_BE_STRING); if(!luser_file_name.is_empty()) - user_file_name=luser_file_name.cstr(String::L_FILE_SPEC); + user_file_name=luser_file_name.taint_cstr(String::L_FILE_SPEC); } } if(!user_file_name) - user_file_name=lfile_name.cstr(String::L_FILE_SPEC); + user_file_name=lfile_name.taint_cstr(String::L_FILE_SPEC); size_t offset=0; size_t limit=0; @@ -250,6 +248,8 @@ static void _load(Request& r, MethodPara VFile& self=GET_SELF(r, VFile); self.set(true/*tainted*/, file.str, file.length, user_file_name, vcontent_type); + self.set_mode(as_text); + if(file.headers){ file.headers->for_each(_load_pass_param, &self.fields()); } else { @@ -263,26 +263,27 @@ static void _load(Request& r, MethodPara ff.put(mdate_name, new VDate(mtime)); ff.put(cdate_name, new VDate(ctime)); } - } static void _create(Request& r, MethodParams& params) { - Value& vmode_name=params.as_no_junction(0, MODE_MUST_NOT_BE_CODE); - if(!is_text_mode(vmode_name.as_string())) + const String& mode_name=params.as_no_junction(0, MODE_MUST_NOT_BE_CODE).as_string(); + if(!is_text_mode(mode_name)) throw Exception(PARSER_RUNTIME, 0, "only text mode is currently supported"); const char* user_file_name_cstr=r.absolute( - params.as_no_junction(1, FILE_NAME_MUST_NOT_BE_CODE).as_string()).cstr(String::L_FILE_SPEC); + params.as_no_junction(1, FILE_NAME_MUST_NOT_BE_CODE).as_string()).taint_cstr(String::L_FILE_SPEC); const String& content=params.as_string(2, "content must be string"); - const char* content_cstr=content.cstr(String::L_UNSPECIFIED); // explode content, honor tainting changes + const String::Body content_body=content.cstr_to_string_body_untaint(String::L_AS_IS); // explode content, honor tainting changes VString* vcontent_type=new VString(r.mime_type_of(user_file_name_cstr)); VFile& self=GET_SELF(r, VFile); - self.set(true/*tainted*/, content_cstr, strlen(content_cstr), user_file_name_cstr, vcontent_type); + self.set(true/*tainted*/, content_body.cstr(), content_body.length(), user_file_name_cstr, vcontent_type); + + self.set_mode(true/*as_text*/); } static void _stat(Request& r, MethodParams& params) { @@ -296,7 +297,7 @@ static void _stat(Request& r, MethodPara size, atime, mtime, ctime); - const char* user_file_name=lfile_name.cstr(String::L_FILE_SPEC); + const char* user_file_name=lfile_name.taint_cstr(String::L_FILE_SPEC); VFile& self=GET_SELF(r, VFile); @@ -343,7 +344,7 @@ static void append_env_pair( throw Exception(PARSER_RUNTIME, new String(akey, String::L_TAINTED), "not safe environment variable"); - info->env->put(akey, avalue->as_string().cstr_to_string_body(String::L_UNSPECIFIED, 0, info->charsets)); + info->env->put(akey, avalue->as_string().cstr_to_string_body_untaint(String::L_AS_IS, 0, info->charsets)); } } #ifndef DOXYGEN @@ -369,21 +370,17 @@ static void pass_cgi_header_attribute( static void append_to_argv(Request& r, ArrayString& argv, const String* str){ if(!str->is_empty()) - argv+=new String(str->cstr_to_string_body(String::L_UNSPECIFIED, 0, &r.charsets), String::L_AS_IS); + argv+=new String(str->cstr_to_string_body_untaint(String::L_AS_IS, 0, &r.charsets), String::L_AS_IS); } /// @todo fix `` in perl - they produced flipping consoles and no output to perl -static void _exec_cgi(Request& r, MethodParams& params, - bool cgi) { - - Value& first_param=params.as_no_junction(0, FIRST_ARG_MUST_NOT_BE_CODE); - - bool is_mode_specified=is_valid_mode(first_param.as_string()); - const String& mode_name=(is_mode_specified) ? first_param.as_string() : *new String(TEXT_MODE_NAME); - - size_t param_index=1; - if(!is_mode_specified){ - --param_index; +static void _exec_cgi(Request& r, MethodParams& params, bool cgi) { + bool as_text=true; + size_t param_index=0; + const String& mode_name=params.as_no_junction(0, FIRST_ARG_MUST_NOT_BE_CODE).as_string(); + if(is_valid_mode(mode_name)){ + as_text=is_text_mode(mode_name); + param_index++; } if(param_index>=params.count()) @@ -455,7 +452,7 @@ static void _exec_cgi(Request& r, Method if(const String* sstdin=info.vstdin->get_string()) { in->append(*sstdin, String::L_CLEAN, true); } else - if(VFile* vfile=static_cast(info.vstdin->as("file", false))) + if(VFile* vfile=static_cast(info.vstdin->as("file"))) in->append_know_length((const char* )vfile->value_ptr(), vfile->value_size(), String::L_TAINTED); else throw Exception(PARSER_RUNTIME, @@ -469,7 +466,6 @@ static void _exec_cgi(Request& r, Method ArrayString argv; if(param_index < params.count()) { // influence tainting - // main target -- URLencoding of tainted pieces to String::L_URI lang Temp_client_charset temp(r.charsets, charset? *charset: r.charsets.source()); for(size_t i=param_index; ilength && is_text_mode(mode_name)){ + if(file_out->length && as_text){ fix_line_breaks(file_out->str, file_out->length); // treat output as string String *real_out = new String(file_out->str); @@ -555,8 +551,8 @@ static void _exec_cgi(Request& r, Method "output does not contain CGI header; " "exit status=%d; stdoutsize=%u; stdout: \"%s\"; stderrsize=%u; stderr: \"%s\"", execution.status, - (size_t)file_out->length, (file_out->length) ? (file_out->str) : "", - (size_t)real_err->length(), real_err->cstr()); + file_out->length, (file_out->length) ? (file_out->str) : "", + real_err->length(), real_err->cstr()); break; //never reached } @@ -581,7 +577,7 @@ static void _exec_cgi(Request& r, Method self.set(false/*not tainted*/, file_out->str, file_out->length); // $fields << header - if(header && eol_marker) { + if(header) { ArrayString rows; size_t pos_after=0; header->split(rows, pos_after, eol_marker); @@ -597,6 +593,8 @@ static void _exec_cgi(Request& r, Method self.set(false/*not tainted*/, file_out->str, file_out->length); } + self.set_mode(as_text); + // $status self.fields().put(file_status_name, new VInt(execution.status)); @@ -621,7 +619,7 @@ static void _list(Request& r, MethodPara if(params.count()>1){ Value& regexp=params.as_no_junction(1, "regexp must not be code"); if(regexp.is_defined()){ - if(Value* value=regexp.as(VREGEX_TYPE, false)){ + if(Value* value=regexp.as(VREGEX_TYPE)){ vregex=static_cast(value); } else { vregex=new VRegex(r.charsets.source(), ®exp.as_string(), 0/*options*/); @@ -631,7 +629,7 @@ static void _list(Request& r, MethodPara } } - const char* absolute_path_cstr=r.absolute(relative_path.as_string()).cstr(String::L_FILE_SPEC); + const char* absolute_path_cstr=r.absolute(relative_path.as_string()).taint_cstr(String::L_FILE_SPEC); Table::columns_type columns(new ArrayString); *columns+=new String("name"); @@ -850,8 +848,8 @@ static void _sql(Request& r, MethodParam Temp_lang temp_lang(r, String::L_SQL); const String& statement_string=r.process_to_string(statement); - const char* statement_cstr= - statement_string.cstr(String::L_UNSPECIFIED, r.connection()); + const char* statement_cstr=statement_string.untaint_cstr(r.flang, r.connection()); + File_sql_event_handlers handlers(statement_string, statement_cstr); ulong limit=SQL_NO_LIMIT; @@ -904,6 +902,7 @@ static void _sql(Request& r, MethodParam : 0; VFile& self=GET_SELF(r, VFile); self.set(true/*tainted*/, handlers.value.str, handlers.value.length, user_file_name_cstr, vcontent_type); + self.set_mode(false/*binary*/); } static void _base64(Request& r, MethodParams& params) {