From 3f9a5d130d317274b9a6dbf1a2ce4723e50d3de1 Mon Sep 17 00:00:00 2001 From: Eric Stern Date: Wed, 3 Jul 2024 16:07:52 -0700 Subject: [PATCH] fixed action_web_request passing the wrong pointer to save_schedules_js fixed pass_mg_body segfaulting if body->len is greater than the size of buf[] cleaned up code in save_schedules_js --- source/aq_scheduler.c | 24 ++++++++---------------- source/aq_scheduler.h | 2 +- source/net_services.c | 22 ++++++++++++---------- 3 files changed, 21 insertions(+), 27 deletions(-) diff --git a/source/aq_scheduler.c b/source/aq_scheduler.c index 6bf76df..eeb1e60 100644 --- a/source/aq_scheduler.c +++ b/source/aq_scheduler.c @@ -60,7 +60,7 @@ bool remount_root_ro(bool readonly) { } } -bool passJson_scObj(char* line, int length, aqs_cron *values) +bool passJson_scObj(const char* line, int length, aqs_cron *values) { int keystart=0; //int keyend=0; @@ -130,9 +130,8 @@ bool passJson_scObj(char* line, int length, aqs_cron *values) return (captured >= 7)?true:false; } -int save_schedules_js(char* inBuf, int inSize, char* outBuf, int outSize) +int save_schedules_js(const char* inBuf, int inSize, char* outBuf, int outSize) { - int length=0; FILE *fp; int i; bool inarray = false; @@ -141,8 +140,7 @@ int save_schedules_js(char* inBuf, int inSize, char* outBuf, int outSize) if ( !_aqconfig_.enable_scheduler) { LOG(SCHD_LOG,LOG_WARNING, "Schedules are disabled\n"); - length += sprintf(outBuf, "{\"message\":\"Error Schedules disabled\"}"); - return length; + return sprintf(outBuf, "{\"message\":\"Error Schedules disabled\"}"); } LOG(SCHD_LOG,LOG_NOTICE, "Saving Schedule:\n"); @@ -154,16 +152,14 @@ int save_schedules_js(char* inBuf, int inSize, char* outBuf, int outSize) if (fp == NULL) { LOG(SCHD_LOG,LOG_ERR, "Open file failed '%s'\n", CRON_FILE); remount_root_ro(true); - length += sprintf(outBuf, "{\"message\":\"Error Saving Schedules\"}"); - return length; + return sprintf(outBuf, "{\"message\":\"Error Saving Schedules\"}"); } + fprintf(fp, "#***** AUTO GENERATED DO NOT EDIT *****\n"); fprintf(fp, "PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin\n"); LOG(SCHD_LOG,LOG_DEBUG, "Schedules Message body:\n'%.*s'\n", inSize, inBuf); - length += sprintf(outBuf, "{\"message\":\"Saved Schedules\"}"); - for (i=0; i < inSize; i++) { if ( inBuf[i] == '[' ) { inarray=true; @@ -174,24 +170,20 @@ int save_schedules_js(char* inBuf, int inSize, char* outBuf, int outSize) LOG(SCHD_LOG,LOG_DEBUG, "Write to cron Min:%s Hour:%s DayM:%s Month:%s DayW:%s URL:%s Value:%s\n",cline.minute,cline.hour,cline.daym,cline.month,cline.dayw,cline.url,cline.value); LOG(SCHD_LOG,LOG_INFO, "%s%s %s %s %s %s curl -s -S --show-error -o /dev/null localhost:%s%s -d value=%s -X PUT\n",(cline.enabled?"":"#"),cline.minute, cline.hour, cline.daym, cline.month, cline.dayw, _aqconfig_.socket_port, cline.url, cline.value); fprintf(fp, "%s%s %s %s %s %s root curl -s -S --show-error -o /dev/null localhost:%s%s -d value=%s -X PUT\n",(cline.enabled?"":"#"),cline.minute, cline.hour, cline.daym, cline.month, cline.dayw, _aqconfig_.socket_port, cline.url, cline.value); - } else if ( inarray && inBuf[i] == '}') { - //inobj=false; - //objed=i; } } - fprintf(fp, "#***** AUTO GENERATED DO NOT EDIT *****\n"); fclose(fp); - // if we created file, change the permisions + // if we created file, change the permissions if (!fileexists) if ( chmod(CRON_FILE, S_IRUSR | S_IWUSR ) < 0 ) - LOG(SCHD_LOG,LOG_ERR, "Could not change permitions on cron file %s, scheduling may not work\n",CRON_FILE); + LOG(SCHD_LOG,LOG_ERR, "Could not change permissions on cron file %s, scheduling may not work\n",CRON_FILE); remount_root_ro(fs); - return length; + return sprintf(outBuf, "{\"message\":\"Saved Schedules\"}"); } int build_schedules_js(char* buffer, int size) diff --git a/source/aq_scheduler.h b/source/aq_scheduler.h index 974e090..8e28780 100644 --- a/source/aq_scheduler.h +++ b/source/aq_scheduler.h @@ -24,7 +24,7 @@ typedef struct aqs_cron } aqs_cron; int build_schedules_js(char* buffer, int size); -int save_schedules_js(char* inBuf, int inSize, char* outBuf, int outSize); +int save_schedules_js(const char* inBuf, int inSize, char* outBuf, int outSize); //void read_schedules(); //void write_schedules(); diff --git a/source/net_services.c b/source/net_services.c index 5d0f1dc..604a2ab 100644 --- a/source/net_services.c +++ b/source/net_services.c @@ -1417,22 +1417,25 @@ float pass_mg_body(struct mg_str *body) { int i; char buf[10]; + int len = sizeof(buf); + if (body->len < len) { + len = body->len; + } + // NSF Really need to come back and clean this up - for (i=0; i < body->len; i++) { + for (i=0; i < len; i++) { if ( body->p[i] == '=' || body->p[i] == ':' ) { - while (!isdigit((unsigned char) body->p[i]) && body->p[i] != '-' && i < body->len) {i++;} - if(i < body->len) { + while (!isdigit((unsigned char) body->p[i]) && body->p[i] != '-' && i < len) {i++;} + if(i < len) { // Need to copy to buffer so we can terminate correctly. - strncpy(buf, &body->p[i], body->len - i); - buf[body->len - i] = '\0'; - //printf ("RETURN\n"); - //return atof(&body->p[i]); + strncpy(buf, &body->p[i], len - i); + buf[len - i] = '\0'; return atof(buf); } } } - //printf ("RETURN UNKNOWN\n"); + return TEMP_UNKNOWN; } @@ -1554,8 +1557,7 @@ void action_web_request(struct mg_connection *nc, struct http_message *http_msg) { char message[JSON_BUFFER_SIZE]; DEBUG_TIMER_START(&tid2); - //int size = save_schedules_js(_aqualink_data, &http_msg->body, message, JSON_BUFFER_SIZE); - int size = save_schedules_js((char *)&http_msg->body, http_msg->body.len, message, JSON_BUFFER_SIZE); + int size = save_schedules_js(http_msg->body.p, http_msg->body.len, message, JSON_BUFFER_SIZE); DEBUG_TIMER_STOP(tid2, NET_LOG, "action_web_request() save_schedules_js took"); mg_send_head(nc, 200, size, CONTENT_JS); mg_send(nc, message, size);