Esempio n. 1
0
int srvclamav_end_of_data_handler(ci_request_t * req)
{
     av_req_data_t *data = ci_service_data(req);
     CL_ENGINE *vdb;
     ci_simple_file_t *body;
     const char *virname;
     int ret = 0;
     unsigned long scanned_data = 0;

     if (!data || !data->body)
          return CI_MOD_DONE;

     body = data->body;
     data->virus_check_done = 1;
     if (data->must_scanned == NO_SCAN) {       /*If exceeds the MAX_OBJECT_SIZE for example ......  */
          ci_simple_file_unlock_all(body);      /*Unlock all data to continue send them . Not really needed here.... */
          return CI_MOD_DONE;
     }


     ci_debug_printf(8, "Scan from file\n");
     lseek(body->fd, 0, SEEK_SET);
     vdb = get_virusdb();
#ifndef HAVE_LIBCLAMAV_095
     ret =
         cl_scandesc(body->fd, &virname, &scanned_data, vdb, &limits,
                     CL_SCAN_STDOPT);
#else
     ret =
         cl_scandesc(body->fd, &virname, &scanned_data, vdb,
                     CL_SCAN_STDOPT);
#endif

     if (ret == CL_VIRUS) {
	 data->virus_name = ci_buffer_alloc(strlen(virname)+1);
	 strcpy(data->virus_name, virname);
     }
     release_virusdb(vdb);

     ci_debug_printf(9,
                     "Clamav engine scanned %lu blocks of  data. Data size: %"
                     PRINTF_OFF_T "...\n", 
		     scanned_data, (CAST_OFF_T) body->endpos);

     if (ret == CL_VIRUS) {
          ci_debug_printf(5, "VIRUS DETECTED: %s.\n ",
                          data->virus_name);
          if (!ci_req_sent_data(req))   /*If no data had sent we can send an error page  */
               generate_error_page(data, req);
          else if (data->must_scanned == VIR_SCAN) {
               endof_data_vir_mode(data, req);
          }
          else
               ci_debug_printf(5, "Simply no other data sent\n");
          return CI_MOD_DONE;
     }
     else if (ret != CL_CLEAN) {
          ci_debug_printf(1,
                          "srvClamAv module: An error occured while scanning the data\n");
     }

     if (data->must_scanned == VIR_SCAN) {
          endof_data_vir_mode(data, req);
     }
     else if (data->allow204 && !ci_req_sent_data(req)) {
          ci_debug_printf(7, "srvClamAv module: Respond with allow 204\n");
          return CI_MOD_ALLOW204;
     }

     ci_simple_file_unlock_all(body);   /*Unlock all data to continue send them..... */
     ci_debug_printf(7,
                     "file unlocked, flags :%d (unlocked:%" PRINTF_OFF_T ")\n",
                     body->flags, (CAST_OFF_T) body->unlocked);
     return CI_MOD_DONE;
}
Esempio n. 2
0
void WebInterface::worker() {
	/* Backup the stdio streambufs */
	std::streambuf * cin_streambuf  = std::cin.rdbuf();
	std::streambuf * cout_streambuf = std::cout.rdbuf();
	std::streambuf * cerr_streambuf = std::cerr.rdbuf();

	const std::string kw_title(KW_TITLE);
	const std::string kw_head(KW_HEAD);
	const std::string kw_menu(KW_MENU);
	const std::string kw_content(KW_CONTENT);

	FCGX_Request request;

	/* Initialize FastCGI library and request */
	FCGX_Init();
	FCGX_InitRequest(&request, 0, FCGI_FAIL_ACCEPT_ON_INTR);

	LOG_DBG("FastCGI initialization success!");

	while (!stop_flag_) {
		if(FCGX_Accept_r(&request) >= 0) {

			fcgi_streambuf cin_fcgi_streambuf(request.in);
			fcgi_streambuf cout_fcgi_streambuf(request.out);
			fcgi_streambuf cerr_fcgi_streambuf(request.err);

			std::cin.rdbuf(&cin_fcgi_streambuf);
			std::cout.rdbuf(&cout_fcgi_streambuf);
			std::cerr.rdbuf(&cerr_fcgi_streambuf);

			/* getting the uri from the request */
			std::string uri;
			const char *uri_param = FCGX_GetParam("REQUEST_URI", request.envp);
			if(!uri_param) {
				LOG_ERR("Failed to retrieve the request URI environment value!");
				uri = URI_PAGE_ERROR;
			} else {
				uri = uri_param;
			}

			LOG_DBG("Request received: %s", uri.c_str());

			/* Check if URI is a file in the home folder and get the mime of
			 * that file (by extension) */
			std::string path;
			std::string mime = if_file_get_mime(uri, &path);

			if (!mime.empty()) {
				/* This is a file we need to serve */
				StringPtr file_data = Utils::read_file(path);
				std::cout << "Content-type: " << mime << "\r\n\r\n";
				std::cout << *(file_data);

				file_data.reset();
			} else {
				/* Parse the URI */
				std::map<std::string, std::string> uri_data = parseURI(uri);

				LOG_DBG("URI Parsed, page requested: %s",
						uri_data[URI_PAGE].c_str());

				/* Generate and serve the page depending on the URI */
				StringPtr page;
				std::string content_type = "text/html";

				/* Main page requested */
				if (uri_data[URI_PAGE].compare(URI_PAGE_MAIN) == 0) {
					bool success = false;

					/* Check if a command was sent from the client. */
					if (uri_data.find(URI_PAGE_COMMAND) != uri_data.end()) {
						success = add_command(uri_data[URI_PAGE_COMMAND]);
					}

					std::string s;
					/* Check if the request was sent from javascript or pure HTML */
					if(uri_data.find(URI_PAGE_SOURCE) != uri_data.end()) {
						LOG_DBG("This query's source IS javascript: %s, Source: %s",
								uri.c_str(), (uri_data[URI_PAGE_SOURCE]).c_str());
						content_type = "application/json";
						page = generate_command_json(success);
					} else {
						LOG_DBG("This query's source IS NOT javascript: %s", uri.c_str());
						/* Just generate a standard main page */
						page = generate_main_page();
					}
				/* Log page requested */
				} else if (uri_data[URI_PAGE].compare(URI_PAGE_LOG) == 0) {
					page = generate_log_page();

				/* Status page requested */
				} else if (uri_data[URI_PAGE].compare(URI_PAGE_STATUS) == 0) {
					page = generate_status_page();

				/* Console lines JSON page requested */
				} else if (uri_data[URI_PAGE].compare(URI_PAGE_CL) == 0) {
					if (uri_data.find(URI_PAGE_BEFORE) != uri_data.end()) {
						content_type = "application/json";
						page = generate_cljson_before(
								uri_data[URI_PAGE_BEFORE]);
					} else if (uri_data.find(URI_PAGE_AFTER)
							!= uri_data.end()) {
						content_type = "application/json";
						page = generate_cljson_after(uri_data[URI_PAGE_AFTER]);
					} else {
						page = generate_error_page();
					}

				/* Log lines JSON page requested */
				} else if (uri_data[URI_PAGE].compare(URI_PAGE_LL) == 0) {
					if (uri_data.find(URI_PAGE_BEFORE) != uri_data.end()) {
						content_type = "application/json";
						page = generate_lljson_before(
								uri_data[URI_PAGE_BEFORE]);
					} else if (uri_data.find(URI_PAGE_AFTER)
							!= uri_data.end()) {
						content_type = "application/json";
						page = generate_lljson_after(uri_data[URI_PAGE_AFTER]);
					} else {
						page = generate_error_page();
					}
				} else {
					page = generate_error_page();
				}

				/* Output the generated page with the correct content type */
				std::cout << "Content-type: " << content_type << "\r\n\r\n";
				std::cout << *(page.get());
			}

		}
		else {
			LOG_TRC("FCGX_Aceept_r returned less than 0!");
		}
	}

	LOG_TRC("Out of accept request loop!");

	// Free request strucure
	FCGX_Finish_r(&request);

	// Flag the thread as not running anymore.
	running_ = false;

	// restore stdio streambufs
	std::cin.rdbuf(cin_streambuf);
	std::cout.rdbuf(cout_streambuf);
	std::cerr.rdbuf(cerr_streambuf);
}