diff --git a/nagios/cgi/extinfo.c b/nagios/cgi/extinfo.c index 5be3fb9..15deb3e 100644 --- a/nagios/cgi/extinfo.c +++ b/nagios/cgi/extinfo.c @@ -1831,7 +1831,96 @@ void show_servicegroup_info(){ return; } +#define HTML_TAB_STR " " +#define HTML_NL_STR "
" + +char *format_html(const char *comment) +{ + unsigned int c_len = strlen(comment); + const char *esc = index(comment, '\\'); + if (!esc) + return strdup(comment); + + unsigned int buf_len = c_len * 2; + char *html = malloc(buf_len); + if (!html) { + fprintf(stderr, "ERROR: Unable to allocate %d bytes!\n", buf_len); + return NULL; + } + + unsigned int h_pos = 0; + unsigned int pos = 0; + + const char *rep_str = NULL; + unsigned int rep_len = 0; + unsigned int skip = 0; + + while (esc) { + if ((esc - comment) < (c_len - 1)) { + switch (*(esc + 1)) { + case 't': + rep_str = HTML_TAB_STR; + rep_len = strlen(rep_str); + skip = 2; + break; + case 'n': + rep_str = HTML_NL_STR; + rep_len = strlen(rep_str); + skip = 2; + break; + default: + esc += 2; + skip = 0; + rep_str = NULL; + rep_len = 0; + } + } else { + esc++; + skip = 0; + rep_str = NULL; + rep_len = 0; + } + + if ((buf_len - h_pos) < (esc - comment - pos + rep_len)) { + buf_len += 2*(esc - comment - pos + rep_len); + html = realloc(html, buf_len); + if (!html) { + fprintf(stderr, "ERROR: Unable to reallocate %d bytes!\n", buf_len); + return NULL; + } + } + + memcpy(html + h_pos, comment + pos, esc - comment - pos); + h_pos += esc - comment - pos; + + if (rep_str && rep_len) { + memcpy(html + h_pos, rep_str, rep_len); + h_pos += rep_len; + } + + pos += esc - comment - pos + skip; + + if (pos < c_len) { + esc = index(comment + pos, '\\'); + } else + esc = NULL; + } + + if ((buf_len - h_pos) <= (c_len - pos)) { + buf_len += c_len - pos + 1; + html = realloc(html, buf_len); + if (!html) { + fprintf(stderr, "ERROR: Unable to reallocate %d bytes!\n", buf_len); + return NULL; + } + } + + memcpy(html + h_pos, comment + pos, c_len - pos); + h_pos += c_len - pos; + html[h_pos] = '\0'; + return html; +} /* shows all service and host comments */ void show_all_comments(void){ @@ -1844,6 +1933,7 @@ void show_all_comments(void){ service *temp_service; char *comment_type; char expire_time[MAX_DATETIME_LENGTH]; + char *html_comment = NULL; printf("

\n"); @@ -1902,13 +1992,21 @@ void show_all_comments(void){ comment_type="?"; } + html_comment = format_html(temp_comment->comment_data); + if (!html_comment) { + fprintf(stderr, "ERROR: Unable to format the comment data!\n"); + continue; + } + get_time_string(&temp_comment->entry_time,date_time,(int)sizeof(date_time),SHORT_DATE_TIME); get_time_string(&temp_comment->expire_time,expire_time,(int)sizeof(date_time),SHORT_DATE_TIME); printf("",bg_class); printf("%s",bg_class,EXTINFO_CGI,DISPLAY_HOST_INFO,url_encode(temp_comment->host_name),temp_comment->host_name); - printf("%s%s%s%ld%s%s%s",bg_class,date_time,bg_class,temp_comment->author,bg_class,temp_comment->comment_data,bg_class,temp_comment->comment_id,bg_class,(temp_comment->persistent)?"Yes":"No",bg_class,comment_type,bg_class,(temp_comment->expires==TRUE)?expire_time:"N/A"); + printf("%s%s

%s
%ld%s%s%s",bg_class,date_time,bg_class,temp_comment->author,bg_class,html_comment,bg_class,temp_comment->comment_id,bg_class,(temp_comment->persistent)?"Yes":"No",bg_class,comment_type,bg_class,(temp_comment->expires==TRUE)?expire_time:"N/A"); printf("Delete This Comment",COMMAND_CGI,CMD_DEL_HOST_COMMENT,temp_comment->comment_id,url_images_path,DELETE_ICON); printf("\n"); + + free(html_comment); } if(total_comments==0) @@ -1972,15 +2070,23 @@ void show_all_comments(void){ comment_type="?"; } + html_comment = format_html(temp_comment->comment_data); + if (!html_comment) { + fprintf(stderr, "ERROR: Unable to format the comment data!\n"); + continue; + } + get_time_string(&temp_comment->entry_time,date_time,(int)sizeof(date_time),SHORT_DATE_TIME); get_time_string(&temp_comment->expire_time,expire_time,(int)sizeof(date_time),SHORT_DATE_TIME); printf("",bg_class); printf("%s",bg_class,EXTINFO_CGI,DISPLAY_HOST_INFO,url_encode(temp_comment->host_name),temp_comment->host_name); printf("%s",url_encode(temp_comment->service_description),temp_comment->service_description); - printf("%s%s%s%ld%s%s%s",bg_class,date_time,bg_class,temp_comment->author,bg_class,temp_comment->comment_data,bg_class,temp_comment->comment_id,bg_class,(temp_comment->persistent)?"Yes":"No",bg_class,comment_type,bg_class,(temp_comment->expires==TRUE)?expire_time:"N/A"); + printf("%s%s
%s
%ld%s%s%s",bg_class,date_time,bg_class,temp_comment->author,bg_class,html_comment,bg_class,temp_comment->comment_id,bg_class,(temp_comment->persistent)?"Yes":"No",bg_class,comment_type,bg_class,(temp_comment->expires==TRUE)?expire_time:"N/A"); printf("Delete This Comment",COMMAND_CGI,CMD_DEL_SVC_COMMENT,temp_comment->comment_id,url_images_path,DELETE_ICON); printf("\n"); + + free(html_comment); } if(total_comments==0) @@ -2517,6 +2623,7 @@ void display_comments(int type){ comment *temp_comment; char *comment_type; char expire_time[MAX_DATETIME_LENGTH]; + char *html_comment = NULL; /* find the host or service */ @@ -2603,13 +2710,20 @@ void display_comments(int type){ comment_type="?"; } + html_comment = format_html(temp_comment->comment_data); + if (!html_comment) { + fprintf(stderr, "ERROR: Unable to format the comment data!\n"); + continue; + } + get_time_string(&temp_comment->entry_time,date_time,(int)sizeof(date_time),SHORT_DATE_TIME); get_time_string(&temp_comment->expire_time,expire_time,(int)sizeof(date_time),SHORT_DATE_TIME); printf("",bg_class); - printf("%s%s%s%lu%s%s%s",bg_class,date_time,bg_class,temp_comment->author,bg_class,temp_comment->comment_data,bg_class,temp_comment->comment_id,bg_class,(temp_comment->persistent)?"Yes":"No",bg_class,comment_type,bg_class,(temp_comment->expires==TRUE)?expire_time:"N/A"); + printf("%s%s
%s
%lu%s%s%s",bg_class,date_time,bg_class,temp_comment->author,bg_class,html_comment,bg_class,temp_comment->comment_id,bg_class,(temp_comment->persistent)?"Yes":"No",bg_class,comment_type,bg_class,(temp_comment->expires==TRUE)?expire_time:"N/A"); printf("
Delete This Comment",COMMAND_CGI,(type==HOST_COMMENT)?CMD_DEL_HOST_COMMENT:CMD_DEL_SVC_COMMENT,temp_comment->comment_id,url_images_path,DELETE_ICON); printf("\n"); + free(html_comment); total_comments++; } }