From 490f483b9540d485f9623e89add779709bc845b0 Mon Sep 17 00:00:00 2001 From: MrDave Date: Wed, 23 Jun 2021 22:04:25 -0600 Subject: [PATCH] Revise webcontrol headers param to be generic --- src/conf.cpp | 29 +++++++---------------------- src/conf.hpp | 2 +- src/motionplus.hpp | 2 +- src/util.cpp | 1 + src/webu.cpp | 16 ++++++++++++---- src/webu_stream.cpp | 22 ++++++++++++++++------ 6 files changed, 38 insertions(+), 34 deletions(-) diff --git a/src/conf.cpp b/src/conf.cpp index 742ec571..b6ee0a08 100644 --- a/src/conf.cpp +++ b/src/conf.cpp @@ -17,7 +17,6 @@ * Copyright 2020-2021 MotionMrDave@gmail.com */ -#include #include #include #include "motionplus.hpp" @@ -160,7 +159,7 @@ struct ctx_parm config_parms[] = { {"webcontrol_tls", PARM_TYP_BOOL, PARM_CAT_13, WEBUI_LEVEL_RESTRICTED }, {"webcontrol_cert", PARM_TYP_STRING, PARM_CAT_13, WEBUI_LEVEL_RESTRICTED }, {"webcontrol_key", PARM_TYP_STRING, PARM_CAT_13, WEBUI_LEVEL_RESTRICTED }, - {"webcontrol_cors_header", PARM_TYP_STRING, PARM_CAT_13, WEBUI_LEVEL_ADVANCED }, + {"webcontrol_headers", PARM_TYP_STRING, PARM_CAT_13, WEBUI_LEVEL_ADVANCED }, {"webcontrol_html", PARM_TYP_STRING, PARM_CAT_13, WEBUI_LEVEL_ADVANCED }, {"stream_preview_scale", PARM_TYP_INT, PARM_CAT_14, WEBUI_LEVEL_LIMITED }, @@ -2210,30 +2209,17 @@ static void conf_edit_webcontrol_key(struct ctx_cam *cam, std::string &parm, enu MOTION_LOG(DBG, TYPE_ALL, NO_ERRNO,"%s:%s","webcontrol_key",_("webcontrol_key")); } -static void conf_edit_webcontrol_cors_header(struct ctx_cam *cam, std::string &parm, enum PARM_ACT pact) +static void conf_edit_webcontrol_headers(struct ctx_cam *cam, std::string &parm, enum PARM_ACT pact) { - int retcd; if (pact == PARM_ACT_DFLT) { - cam->conf->webcontrol_cors_header = ""; + cam->conf->webcontrol_headers = ""; } else if (pact == PARM_ACT_SET) { - const char *regex_str = "(http|https)://(((.*):(.*))@)?([^/:]|[-_.a-z0-9]+)(:([0-9]+))?($|(/[^*]*))"; - regex_t regex; - if (regcomp(®ex, regex_str, REG_EXTENDED) != 0) { - MOTION_LOG(ERR, TYPE_STREAM, NO_ERRNO,_("Error compiling regex in copy_uri")); - return; - } - /* We only warn on this since regex may not perfectly edit uris */ - retcd = regexec(®ex, parm.c_str(), 0, NULL, 0); - if ((parm != "*") && (parm != "") && (retcd == REG_NOMATCH)) { - MOTION_LOG(ERR, TYPE_STREAM, NO_ERRNO,_("Possibly invalid webcontrol_cors_header")); - } - cam->conf->webcontrol_cors_header = parm; - regfree(®ex); + cam->conf->webcontrol_headers = parm; } else if (pact == PARM_ACT_GET) { - parm = cam->conf->webcontrol_cors_header; + parm = cam->conf->webcontrol_headers; } return; - MOTION_LOG(DBG, TYPE_ALL, NO_ERRNO,"%s:%s","webcontrol_cors_header",_("webcontrol_cors_header")); + MOTION_LOG(DBG, TYPE_ALL, NO_ERRNO,"%s:%s","webcontrol_headers",_("webcontrol_headers")); } static void conf_edit_webcontrol_html(struct ctx_cam *cam, std::string &parm, enum PARM_ACT pact) @@ -2949,7 +2935,7 @@ static void conf_edit_cat13(struct ctx_cam *cam, std::string parm_nm } else if (parm_nm == "webcontrol_tls") { conf_edit_webcontrol_tls(cam, parm_val, pact); } else if (parm_nm == "webcontrol_cert") { conf_edit_webcontrol_cert(cam, parm_val, pact); } else if (parm_nm == "webcontrol_key") { conf_edit_webcontrol_key(cam, parm_val, pact); - } else if (parm_nm == "webcontrol_cors_header") { conf_edit_webcontrol_cors_header(cam, parm_val, pact); + } else if (parm_nm == "webcontrol_headers") { conf_edit_webcontrol_headers(cam, parm_val, pact); } else if (parm_nm == "webcontrol_html") { conf_edit_webcontrol_html(cam, parm_val, pact); } @@ -3711,7 +3697,6 @@ void conf_parms_log(struct ctx_cam **cam_list) (config_parms[i].parm_name == "netcam_userpass") || (config_parms[i].parm_name == "netcam_high_url") || (config_parms[i].parm_name == "webcontrol_authentication") || - (config_parms[i].parm_name == "webcontrol_cors_header") || (config_parms[i].parm_name == "webcontrol_key") || (config_parms[i].parm_name == "webcontrol_cert") || (config_parms[i].parm_name == "database_user") || diff --git a/src/conf.hpp b/src/conf.hpp index ec98309a..4a21ed61 100644 --- a/src/conf.hpp +++ b/src/conf.hpp @@ -148,7 +148,7 @@ bool webcontrol_tls; std::string webcontrol_cert; std::string webcontrol_key; - std::string webcontrol_cors_header; + std::string webcontrol_headers; std::string webcontrol_html; /* Live stream configuration parameters */ diff --git a/src/motionplus.hpp b/src/motionplus.hpp index 93a3b797..58868234 100644 --- a/src/motionplus.hpp +++ b/src/motionplus.hpp @@ -414,7 +414,7 @@ struct ctx_motapp { struct MHD_Daemon *webcontrol_daemon; char webcontrol_digest_rand[12]; std::list webcontrol_failauth; /* C++ list of ips that failed authentication */ - + struct ctx_params *webcontrol_headers; /* parameters for header */ bool parms_changed; /*bool indicating if the parms have changed */ pthread_mutex_t mutex_parms; /* mutex used to lock when changing parms */ diff --git a/src/util.cpp b/src/util.cpp index c0d7d846..cac6ddeb 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -1104,6 +1104,7 @@ void util_parms_parse_comma(struct ctx_params *params, std::string &parmline) } +/* Parse through the config line and put into the array */ void util_parms_parse(struct ctx_params *params, std::string confline) { /* Parse through the configuration option to get values diff --git a/src/webu.cpp b/src/webu.cpp index 2350ac41..9cb1fbed 100644 --- a/src/webu.cpp +++ b/src/webu.cpp @@ -652,6 +652,7 @@ static mhdrslt webu_mhd_send(struct ctx_webui *webui) { mhdrslt retcd; struct MHD_Response *response; + int indx; response = MHD_create_response_from_buffer(webui->resp_page.length() ,(void *)webui->resp_page.c_str(), MHD_RESPMEM_PERSISTENT); @@ -661,11 +662,14 @@ static mhdrslt webu_mhd_send(struct ctx_webui *webui) } if (webui->cam != NULL) { - if (webui->motapp->cam_list[0]->conf->webcontrol_cors_header != "") { - MHD_add_response_header (response, MHD_HTTP_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN - , webui->motapp->cam_list[0]->conf->webcontrol_cors_header.c_str()); + if (webui->motapp->webcontrol_headers->params_count > 0) { + for (indx = 0; indx < webui->motapp->webcontrol_headers->params_count; indx++) { + MHD_add_response_header (response + , webui->motapp->webcontrol_headers->params_array[indx].param_name + , webui->motapp->webcontrol_headers->params_array[indx].param_value + ); + } } - if (webui->resp_type == WEBUI_RESP_TEXT) { MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE, "text/plain;"); } else if (webui->resp_type == WEBUI_RESP_JSON) { @@ -1291,6 +1295,10 @@ static void webu_init_webcontrol(struct ctx_motapp *motapp) , _("Starting webcontrol on port %d") , motapp->cam_list[0]->conf->webcontrol_port); + motapp->webcontrol_headers = (ctx_params*)mymalloc(sizeof(struct ctx_params)); + motapp->webcontrol_headers->update_params = true; + util_parms_parse(motapp->webcontrol_headers, motapp->cam_list[0]->conf->webcontrol_headers); + mhdst.tls_cert = webu_mhd_loadfile(motapp->cam_list[0]->conf->webcontrol_cert); mhdst.tls_key = webu_mhd_loadfile(motapp->cam_list[0]->conf->webcontrol_key); mhdst.motapp = motapp; diff --git a/src/webu_stream.cpp b/src/webu_stream.cpp index 582f1060..bcedee26 100644 --- a/src/webu_stream.cpp +++ b/src/webu_stream.cpp @@ -344,6 +344,7 @@ static mhdrslt webu_stream_mjpeg(struct ctx_webui *webui) /* Create the stream for the motion jpeg */ mhdrslt retcd; struct MHD_Response *response; + int indx; if (webu_stream_checks(webui) == -1) { return MHD_NO; @@ -362,9 +363,13 @@ static mhdrslt webu_stream_mjpeg(struct ctx_webui *webui) return MHD_NO; } - if (webui->motapp->cam_list[0]->conf->webcontrol_cors_header != "") { - MHD_add_response_header(response, MHD_HTTP_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN - , webui->motapp->cam_list[0]->conf->webcontrol_cors_header.c_str()); + if (webui->motapp->webcontrol_headers->params_count > 0) { + for (indx = 0; indx < webui->motapp->webcontrol_headers->params_count; indx++) { + MHD_add_response_header (response + , webui->motapp->webcontrol_headers->params_array[indx].param_name + , webui->motapp->webcontrol_headers->params_array[indx].param_value + ); + } } MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE @@ -383,6 +388,7 @@ static mhdrslt webu_stream_static(struct ctx_webui *webui) mhdrslt retcd; struct MHD_Response *response; char resp_used[20]; + int indx; if (webu_stream_checks(webui) == -1) { return MHD_NO; @@ -406,9 +412,13 @@ static mhdrslt webu_stream_static(struct ctx_webui *webui) return MHD_NO; } - if (webui->motapp->cam_list[0]->conf->webcontrol_cors_header != "") { - MHD_add_response_header (response, MHD_HTTP_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN - , webui->motapp->cam_list[0]->conf->webcontrol_cors_header.c_str()); + if (webui->motapp->webcontrol_headers->params_count > 0) { + for (indx = 0; indx < webui->motapp->webcontrol_headers->params_count; indx++) { + MHD_add_response_header (response + , webui->motapp->webcontrol_headers->params_array[indx].param_name + , webui->motapp->webcontrol_headers->params_array[indx].param_value + ); + } } MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE, "image/jpeg");