/* Simple encoded string test utility. Makes sure that the encoded string * functionality I wrote works. */ int main(int argc, char *argv[]) { if(argv[1]) { printf("Before html_encode: %s\n", argv[1]); char *c; html_encode(argv[1], &c); printf("After html_encode: %s\n", c); char *d; html_encode(argv[1], &d); printf("Repetitive html_enocode test: %s\n", d); } return 0; }
int CAimProto::aim_set_away(HANDLE hServerConn, unsigned short &seqno, const char *amsg, bool set)//user info { unsigned short offset=0; char* html_msg = NULL; size_t msg_size = 0; if (set) { if (!amsg) return -1; setDword(AIM_KEY_LA, (DWORD)time(NULL)); html_msg = html_encode(amsg && amsg[0] ? amsg : DEFAULT_AWAY_MSG); msg_size = strlen(html_msg); } aimString str(html_msg); const char *charset = str.isUnicode() ? AIM_MSG_TYPE_UNICODE : AIM_MSG_TYPE; const unsigned short charset_len = (unsigned short)strlen(charset); const char* msg = str.getBuf(); const unsigned short msg_len = str.getSize(); char* buf=(char*)alloca(SNAC_SIZE+TLV_HEADER_SIZE*3+charset_len+msg_len+1); aim_writesnac(0x02,0x04,offset,buf); aim_writetlv(0x03,charset_len,charset,offset,buf); aim_writetlv(0x04,(unsigned short)msg_len,msg,offset,buf); // aim_writetlvchar(0x0f,2,offset,buf); mir_free(html_msg); return aim_sendflap(hServerConn,0x02,offset,buf,seqno); }
/* int analyze_server_response(int socket) return 0 or 1 when the cond regex is matched return -1 if no response from server */ int analyze_server_response(int s) { int runs = 0; while ((buf = hydra_receive_line(s)) != NULL) { runs++; //check for http redirection if (strstr(buf, "HTTP/1.1 3") != NULL || strstr(buf, "HTTP/1.0 3") != NULL || strstr(buf, "Status: 3") != NULL) { redirected_flag = 1; } else if (strstr(buf, "HTTP/1.1 401") != NULL || strstr(buf, "HTTP/1.0 401") != NULL) { auth_flag = 1; } else if ((strstr(buf, "HTTP/1.1 403") != NULL) || (strstr(buf, "HTTP/1.1 404") != NULL) || (strstr(buf, "HTTP/1.0 403") != NULL) || (strstr(buf, "HTTP/1.0 404") != NULL)) { return 0; } if (hydra_strcasestr(buf, "Location: ") != NULL) { char *startloc, *endloc; char str[2048]; startloc = hydra_strcasestr(buf, "Location: ") + strlen("Location: "); strncpy(str, startloc, sizeof(str) - 1); str[sizeof(str) - 1] = 0; endloc = strchr(str, '\n'); if (endloc != NULL) *endloc = 0; endloc = strchr(str, '\r'); if (endloc != NULL) *endloc = 0; strcpy(redirected_url_buff, str); } //there can be multiple cookies if (hydra_strcasestr(buf, "Set-Cookie: ") != NULL) { char *cookiebuf = buf; do { char *startcookie, *endcookie1, *endcookie2; char str[1024], tmpcookie[4096] = "", tmpname[128] = "", *ptr, *ptr2; memset(str, 0, sizeof(str)); startcookie = hydra_strcasestr(cookiebuf, "Set-Cookie: ") + strlen("Set-Cookie: "); strncpy(str, startcookie, sizeof(str) - 1); str[sizeof(str) - 1] = 0; endcookie1 = strchr(str, '\n'); endcookie2 = strchr(str, ';'); //terminate string after cookie data if (endcookie1 != NULL && ((endcookie1 < endcookie2) || (endcookie2 == NULL))) { if (*(endcookie1 - 1) == '\r') endcookie1--; *endcookie1 = 0; } else if (endcookie2 != NULL) *endcookie2 = 0; // is the cookie already there? if yes, remove it! if (index(startcookie, '=') != NULL && (ptr = index(startcookie, '=')) - startcookie + 1 <= sizeof(tmpname)) { strncpy(tmpname, startcookie, sizeof(tmpname) - 2); tmpname[sizeof(tmpname) - 2] = 0; ptr = index(tmpname, '='); *(++ptr) = 0; // is the cookie already in the cookiejar? (so, does it have to be replaced?) if ((ptr = hydra_strcasestr(cookie, tmpname)) != NULL) { // yes it is. // if the cookie is not in the beginning of the cookiejar, copy the ones before if (ptr != cookie && *(ptr - 1) == ' ') { strncpy(tmpcookie, cookie, ptr - cookie - 2); tmpcookie[ptr - cookie - 2] = 0; } ptr += strlen(tmpname); // if there are any cookies after this one in the cookiejar, copy them over if ((ptr2 = strstr(ptr, "; ")) != NULL) { ptr2 += 2; strncat(tmpcookie, ptr2, sizeof(tmpcookie) - strlen(tmpcookie) - 1); } if (debug) printf("[DEBUG] removing cookie %s in jar\n before: %s\n after: %s\n", tmpname, cookie, tmpcookie); strcpy(cookie, tmpcookie); } } ptr = index(str, '='); // only copy the cookie if it has a value (otherwise the server wants to delete the cookie) if (ptr != NULL && *(ptr + 1) != ';' && *(ptr + 1) != 0 && *(ptr + 1) != '\n' && *(ptr + 1) != '\r') { if (strlen(cookie) > 0) strncat(cookie, "; ", sizeof(cookie) - strlen(cookie) - 1); strncat(cookie, str, sizeof(cookie) - strlen(cookie) - 1); } cookiebuf = startcookie; } while (hydra_strcasestr(cookiebuf, "Set-Cookie: ") != NULL); } #ifdef HAVE_PCRE if (hydra_string_match(buf, cond) == 1) { #else if (strstr(buf, cond) != NULL) { #endif free(buf); // printf("DEBUG: STRING %s FOUND!!:\n%s\n", cond, buf); return 1; } // else printf("DEBUG: STRING %s NOT FOUND:\n%s\n", cond, buf); free(buf); } if (runs == 0) { if (debug) hydra_report(stderr, "DEBUG: no response from server\n"); return -1; } return 0; } void hydra_reconnect(int s, char *ip, int port, unsigned char options) { if (s >= 0) s = hydra_disconnect(s); if ((options & OPTION_SSL) == 0) { s = hydra_connect_tcp(ip, port); } else { s = hydra_connect_ssl(ip, port); } } int start_http_form(int s, char *ip, int port, unsigned char options, char *miscptr, FILE * fp, char *type, ptr_header_node ptr_head) { char *empty = ""; char *login, *pass, clogin[256], cpass[256]; char header[8096], *upd3variables; char *http_request; int found = !success_cond, i, j; char content_length[MAX_CONTENT_LENGTH], proxy_string[MAX_PROXY_LENGTH]; memset(header, 0, sizeof(header)); cookie[0] = 0; // reset cookies from potential previous attempt // Take the next login/pass pair if (strlen(login = hydra_get_next_login()) == 0) login = empty; if (strlen(pass = hydra_get_next_password()) == 0) pass = empty; strncpy(clogin, html_encode(login), sizeof(clogin) - 1); clogin[sizeof(clogin) - 1] = 0; strncpy(cpass, html_encode(pass), sizeof(cpass) - 1); cpass[sizeof(cpass) - 1] = 0; upd3variables = hydra_strrep(variables, "^USER^", clogin); upd3variables = hydra_strrep(upd3variables, "^PASS^", cpass); // Replace the user/pass placeholders in the user-supplied headers hdrrep(&ptr_head, "^USER^", clogin); hdrrep(&ptr_head, "^PASS^", cpass); /* again: no snprintf to be portable. dont worry, buffer cant overflow */ if (use_proxy == 1 && proxy_authentication != NULL) { if (getcookie) { memset(proxy_string, 0, sizeof(proxy_string)); snprintf(proxy_string, MAX_PROXY_LENGTH - 1, "http://%s:%d%.600s", webtarget, webport, cookieurl); http_request = prepare_http_request("GET", proxy_string, NULL, cookie_request); if (hydra_send(s, http_request, strlen(http_request), 0) < 0) return 1; i = analyze_server_response(s); // ignore result if (strlen(cookie) > 0) add_header(&ptr_head, "Cookie", cookie, HEADER_TYPE_DEFAULT_REPL); hydra_reconnect(s, ip, port, options); } // now prepare for the "real" request if (strcmp(type, "POST") == 0) { memset(proxy_string, 0, sizeof(proxy_string)); snprintf(proxy_string, MAX_PROXY_LENGTH - 1, "http://%s:%d%.600s", webtarget, webport, url); snprintf(content_length, MAX_CONTENT_LENGTH - 1, "%d", (int) strlen(upd3variables)); if (header_exists(&ptr_head, "Content-Length", HEADER_TYPE_DEFAULT)) hdrrepv(&ptr_head, "Content-Length", content_length); else add_header(&ptr_head, "Content-Length", content_length, HEADER_TYPE_DEFAULT); if (!header_exists(&ptr_head, "Content-Type", HEADER_TYPE_DEFAULT)) add_header(&ptr_head, "Content-Type", "application/x-www-form-urlencoded", HEADER_TYPE_DEFAULT); normal_request = stringify_headers(&ptr_head); http_request = prepare_http_request("POST", proxy_string, upd3variables, normal_request); if (hydra_send(s, http_request, strlen(http_request), 0) < 0) return 1; } else { normal_request = stringify_headers(&ptr_head); http_request = prepare_http_request("GET", url, upd3variables, normal_request); if (hydra_send(s, http_request, strlen(http_request), 0) < 0) return 1; } } else { if (use_proxy == 1) { // proxy without authentication if (getcookie) { //doing a GET to get cookies memset(proxy_string, 0, sizeof(proxy_string)); snprintf(proxy_string, MAX_PROXY_LENGTH - 1, "http://%s:%d%.600s", webtarget, webport, cookieurl); http_request = prepare_http_request("GET", proxy_string, NULL, cookie_request); if (hydra_send(s, http_request, strlen(http_request), 0) < 0) return 1; i = analyze_server_response(s); // ignore result if (strlen(cookie) > 0) add_header(&ptr_head, "Cookie", cookie, HEADER_TYPE_DEFAULT_REPL); hydra_reconnect(s, ip, port, options); } // now prepare for the "real" request if (strcmp(type, "POST") == 0) { memset(proxy_string, 0, sizeof(proxy_string)); snprintf(proxy_string, MAX_PROXY_LENGTH - 1, "http://%s:%d%.600s", webtarget, webport, url); snprintf(content_length, MAX_CONTENT_LENGTH - 1, "%d", (int) strlen(upd3variables)); if (header_exists(&ptr_head, "Content-Length", HEADER_TYPE_DEFAULT)) hdrrepv(&ptr_head, "Content-Length", content_length); else add_header(&ptr_head, "Content-Length", content_length, HEADER_TYPE_DEFAULT); if (!header_exists(&ptr_head, "Content-Type", HEADER_TYPE_DEFAULT)) add_header(&ptr_head, "Content-Type", "application/x-www-form-urlencoded", HEADER_TYPE_DEFAULT); normal_request = stringify_headers(&ptr_head); http_request = prepare_http_request("POST", proxy_string, upd3variables, normal_request); if (hydra_send(s, http_request, strlen(http_request), 0) < 0) return 1; } else { normal_request = stringify_headers(&ptr_head); http_request = prepare_http_request("GET", url, upd3variables, normal_request); if (hydra_send(s, http_request, strlen(http_request), 0) < 0) return 1; } } else { // direct web server, no proxy if (getcookie) { //doing a GET to save cookies http_request = prepare_http_request("GET", cookieurl, NULL, cookie_request); if (hydra_send(s, http_request, strlen(http_request), 0) < 0) return 1; i = analyze_server_response(s); // ignore result if (strlen(cookie) > 0) { add_header(&ptr_head, "Cookie", cookie, HEADER_TYPE_DEFAULT_REPL); normal_request = stringify_headers(&ptr_head); } hydra_reconnect(s, ip, port, options); } // now prepare for the "real" request if (strcmp(type, "POST") == 0) { snprintf(content_length, MAX_CONTENT_LENGTH - 1, "%d", (int) strlen(upd3variables)); if (header_exists(&ptr_head, "Content-Length", HEADER_TYPE_DEFAULT)) hdrrepv(&ptr_head, "Content-Length", content_length); else add_header(&ptr_head, "Content-Length", content_length, HEADER_TYPE_DEFAULT); if (!header_exists(&ptr_head, "Content-Type", HEADER_TYPE_DEFAULT)) add_header(&ptr_head, "Content-Type", "application/x-www-form-urlencoded", HEADER_TYPE_DEFAULT); normal_request = stringify_headers(&ptr_head); http_request = prepare_http_request("POST", url, upd3variables, normal_request); if (hydra_send(s, http_request, strlen(http_request), 0) < 0) return 1; } else { normal_request = stringify_headers(&ptr_head); http_request = prepare_http_request("GET", url, upd3variables, normal_request); if (hydra_send(s, http_request, strlen(http_request), 0) < 0) return 1; } } } if (debug) hydra_report_debug(stdout, "HTTP request sent:\n%s\n", http_request); found = analyze_server_response(s); if (auth_flag) { // we received a 401 error - user using wrong module hydra_report(stderr, "[ERROR] the target is using HTTP auth, not a web form, received HTTP error code 401. Use module \"http%s-get\" instead.\n", (options & OPTION_SSL) > 0 ? "s" : ""); return 4; } if (strlen(cookie) > 0) add_header(&ptr_head, "Cookie", cookie, HEADER_TYPE_DEFAULT_REPL); //if page was redirected, follow the location header redirected_cpt = MAX_REDIRECT; if (debug) printf("[DEBUG] attempt result: found %d, redirect %d, location: %s\n", found, redirected_flag, redirected_url_buff); while (found == 0 && redirected_flag && (redirected_url_buff[0] != 0) && (redirected_cpt > 0)) { //we have to split the location char *startloc, *endloc; char str[2048]; char str2[2048]; char str3[2048]; redirected_cpt--; redirected_flag = 0; //check if the redirect page contains the fail/success condition #ifdef HAVE_PCRE if (hydra_string_match(redirected_url_buff, cond) == 1) { #else if (strstr(redirected_url_buff, cond) != NULL) { #endif found = success_cond; } else { //location could be either absolute http(s):// or / something //or relative startloc = strstr(redirected_url_buff, "://"); if (startloc != NULL) { startloc += strlen("://"); if ((endloc = strchr(startloc, '\r')) != NULL) { startloc[endloc - startloc] = 0; } if ((endloc = strchr(startloc, '\n')) != NULL) { startloc[endloc - startloc] = 0; } strcpy(str, startloc); endloc = strchr(str, '/'); if (endloc != NULL) { strncpy(str2, str, endloc - str); str2[endloc - str] = 0; } else strncpy(str2, str, sizeof(str)); if (strlen(str) - strlen(str2) == 0) { strcpy(str3, "/"); } else { strncpy(str3, str + strlen(str2), strlen(str) - strlen(str2) - 1); str3[strlen(str) - strlen(str2) - 1] = 0; } } else { strncpy(str2, webtarget, sizeof(str2)); if (redirected_url_buff[0] != '/') { //it's a relative path, so we have to concatenate it //with the path from the first url given char *urlpath; char urlpath_extracted[2048]; memset(urlpath_extracted, 0, sizeof(urlpath_extracted)); urlpath = strrchr(url, '/'); if (urlpath != NULL) { strncpy(urlpath_extracted, url, urlpath - url); sprintf(str3, "%.1000s/%.1000s", urlpath_extracted, redirected_url_buff); } else { sprintf(str3, "%.1000s/%.1000s", url, redirected_url_buff); } } else strncpy(str3, redirected_url_buff, sizeof(str3)); if (debug) hydra_report(stderr, "[DEBUG] host=%s redirect=%s origin=%s\n", str2, str3, url); } if (str3[0] != '/') { j = strlen(str3); str3[j + 1] = 0; for (i = j; i > 0; i--) str3[i] = str3[i - 1]; str3[0] = '/'; } if (verbose) hydra_report(stderr, "[VERBOSE] Page redirected to http://%s%s\n", str2, str3); //re-use the code above to check for proxy use if (use_proxy == 1 && proxy_authentication != NULL) { // proxy with authentication hdrrepv(&ptr_head, "Host", str2); memset(proxy_string, 0, sizeof(proxy_string)); snprintf(proxy_string, MAX_PROXY_LENGTH - 1, "http://%s:%d%.600s", webtarget, webport, str3); normal_request = stringify_headers(&ptr_head); http_request = prepare_http_request("GET", proxy_string, NULL, normal_request); } else { if (use_proxy == 1) { // proxy without authentication hdrrepv(&ptr_head, "Host", str2); memset(proxy_string, 0, sizeof(proxy_string)); snprintf(proxy_string, MAX_PROXY_LENGTH - 1, "http://%s:%d%.600s", webtarget, webport, str3); normal_request = stringify_headers(&ptr_head); http_request = prepare_http_request("GET", proxy_string, NULL, normal_request); } else { //direct web server, no proxy hdrrepv(&ptr_head, "Host", str2); normal_request = stringify_headers(&ptr_head); http_request = prepare_http_request("GET", str3, NULL, normal_request); } } hydra_reconnect(s, ip, port, options); if (hydra_send(s, http_request, strlen(http_request), 0) < 0) return 1; found = analyze_server_response(s); if (strlen(cookie) > 0) add_header(&ptr_head, "Cookie", cookie, HEADER_TYPE_DEFAULT_REPL); } } //if the last status is still 3xx, set it as a false if (found != -1 && found == success_cond && (redirected_flag == 0 || success_cond == 1) && redirected_cpt >= 0) { hydra_report_found_host(port, ip, "www-form", fp); hydra_completed_pair_found(); } else { hydra_completed_pair(); } return 1; } void service_http_form(char *ip, int sp, unsigned char options, char *miscptr, FILE * fp, int port, char *type, ptr_header_node * ptr_head) { int run = 1, next_run = 1, sock = -1; int myport = PORT_HTTP, mysslport = PORT_HTTP_SSL; // register our socket descriptor hydra_register_socket(sp); /* * Iterate through the runs. Possible values are the following: * - 1 -> Open connection to remote server. * - 2 -> Run password attempts. * - 3 -> Disconnect and end with success. * - 4 -> Disconnect and end with error. */ while (1) { if (run == 2) { if (memcmp(hydra_get_next_pair(), &HYDRA_EXIT, sizeof(HYDRA_EXIT)) == 0) { if (freemischttpform) free(miscptr); freemischttpform = 0; hydra_child_exit(1); } } switch (run) { case 1: /* connect and service init function */ { if (sock >= 0) sock = hydra_disconnect(sock); if ((options & OPTION_SSL) == 0) { if (port != 0) myport = port; sock = hydra_connect_tcp(ip, myport); port = myport; } else { if (port != 0) mysslport = port; sock = hydra_connect_ssl(ip, mysslport); port = mysslport; } if (sock < 0) { hydra_report(stderr, "[ERROR] Child with pid %d terminating, cannot connect\n", (int) getpid()); if (freemischttpform) free(miscptr); freemischttpform = 0; hydra_child_exit(1); } next_run = 2; break; } case 2: /* run the cracking function */ next_run = start_http_form(sock, ip, port, options, miscptr, fp, type, *ptr_head); break; case 3: /* clean exit */ if (sock >= 0) sock = hydra_disconnect(sock); if (freemischttpform) free(miscptr); freemischttpform = 0; hydra_child_exit(0); break; case 4: /* silent error exit */ if (sock >= 0) sock = hydra_disconnect(sock); if (freemischttpform) free(miscptr); freemischttpform = 0; hydra_child_exit(1); break; default: if (freemischttpform) free(miscptr); freemischttpform = 0; hydra_report(stderr, "[ERROR] Caught unknown return code, exiting!\n"); hydra_child_exit(0); } run = next_run; } if (freemischttpform) free(miscptr); }
void show_filter(void) { char buffer[MAX_INPUT_BUFFER]; int temp_htmlencode = escape_html_tags; // escape all characters, otherwise they won't show up in search box escape_html_tags = TRUE; printf("<table border=0 cellspacing=0 cellpadding=0 width='100%%'>\n"); printf("<tr><td valign=top align=right style='padding-right:21.5em;'>Filters: "); printf("<img id='expand_image' src='%s%s' border=0 onClick=\"if (document.getElementById('filters').style.display == 'none') { document.getElementById('display_filter').value = 'true'; document.getElementById('filters').style.display = ''; document.getElementById('expand_image').src = '%s%s'; } else { document.getElementById('display_filter').value = 'false'; document.getElementById('filters').style.display = 'none'; document.getElementById('expand_image').src = '%s%s'; }\">", url_images_path, (display_filter == TRUE) ? COLLAPSE_ICON : EXPAND_ICON, url_images_path, COLLAPSE_ICON, url_images_path, EXPAND_ICON); printf("<input type='hidden' name='display_filter' id='display_filter' value='true'>\n"); printf("</td></tr></table>"); printf("<table id='filters' border=0 cellspacing=2 cellpadding=2 style='margin-bottom:0.5em;display:%s;'>\n", (display_filter == TRUE) ? "" : "none"); /* search box */ printf("<tr><td align=right width='10%%'>Search:</td>"); printf("<td nowrap><input type='text' name='query_string' id='query_string' size='15' class='NavBarSearchItem' value='%s'>", (query_string == NULL) ? "" : html_encode(query_string, TRUE)); printf(" <input type='button' value='Clear' onClick=\"document.getElementById('query_string').value = '';\"></td></tr>"); /* Order */ printf("<tr><td align=right>Order:</td>"); printf("<td nowrap><input type=radio name='order' value='new2old' %s> Newer Entries First | <input type=radio name='order' value='old2new' %s> Older Entries First</td></tr>", (reverse == TRUE) ? "" : "checked", (reverse == TRUE) ? "checked" : ""); /* Timeperiod */ printf("<tr><td align=left>Timeperiod:</td>"); printf("<td align=left>"); printf("<select id='selecttp' name='timeperiod' onChange=\"var i=document.getElementById('selecttp').selectedIndex; if (document.getElementById('selecttp').options[i].value == 'custom') { document.getElementById('custtime').style.display = ''; } else { document.getElementById('custtime').style.display = 'none';}\">\n"); printf("<option value=singleday %s>Single Day\n", (timeperiod_type == TIMEPERIOD_SINGLE_DAY) ? "selected" : ""); printf("<option value=today %s>Today\n", (timeperiod_type == TIMEPERIOD_TODAY) ? "selected" : ""); printf("<option value=last24hours %s>Last 24 Hours\n", (timeperiod_type == TIMEPERIOD_LAST24HOURS) ? "selected" : ""); printf("<option value=thisweek %s>This Week\n", (timeperiod_type == TIMEPERIOD_THISWEEK) ? "selected" : ""); printf("<option value=last7days %s>Last 7 Days\n", (timeperiod_type == TIMEPERIOD_LAST7DAYS) ? "selected" : ""); printf("<option value=lastweek %s>Last Week\n", (timeperiod_type == TIMEPERIOD_LASTWEEK) ? "selected" : ""); printf("<option value=thismonth %s>This Month\n", (timeperiod_type == TIMEPERIOD_THISMONTH) ? "selected" : ""); printf("<option value=last31days %s>Last 31 Days\n", (timeperiod_type == TIMEPERIOD_LAST31DAYS) ? "selected" : ""); printf("<option value=lastmonth %s>Last Month\n", (timeperiod_type == TIMEPERIOD_LASTMONTH) ? "selected" : ""); printf("<option value=thisyear %s>This Year\n", (timeperiod_type == TIMEPERIOD_THISYEAR) ? "selected" : ""); printf("<option value=lastyear %s>Last Year\n", (timeperiod_type == TIMEPERIOD_LASTYEAR) ? "selected" : ""); printf("<option value=custom %s>* CUSTOM PERIOD *\n", (timeperiod_type == TIMEPERIOD_CUSTOM) ? "selected" : ""); printf("</select>\n"); printf("<div id='custtime' style='display:%s;'>", (timeperiod_type == TIMEPERIOD_CUSTOM) ? "" : "none"); printf("<br><table border=0 cellspacing=0 cellpadding=0>\n"); get_time_string(&ts_start, buffer, sizeof(buffer) - 1, SHORT_DATE_TIME); printf("<tr><td>Start: </td><td><INPUT TYPE='TEXT' class='timepicker' NAME='start_time' VALUE='%s' SIZE=\"25\"></td></tr>", buffer); get_time_string(&ts_end, buffer, sizeof(buffer) - 1, SHORT_DATE_TIME); printf("<tr><td>End: </td><td><INPUT TYPE='TEXT' class='timepicker' NAME='end_time' VALUE='%s' SIZE=\"25\"></td></tr></table></div>", buffer); printf("</td></tr>\n"); /* Filter Entry types */ printf("<tr><td>Entry Type:</td><td>\n"); printf("<table border=0 cellspacing=0 cellpadding=0>\n"); printf("<tr><td align=center>on</td><td align=center>off</td><td>Type</td></tr>\n"); printf("<tr><td><input type=radio name='noti' value=on %s></td><td><input type=radio name='noti' value=off %s></td><td>Notifications</td></tr>\n", (show_notifications == TRUE) ? "checked" : "", (show_notifications == FALSE) ? "checked" : ""); printf("<tr><td><input type=radio name='hst' value=on %s></td><td><input type=radio name='hst' value=off %s></td><td>Host Status</td></tr>\n", (show_host_status == TRUE) ? "checked" : "", (show_host_status == FALSE) ? "checked" : ""); printf("<tr><td><input type=radio name='sst' value=on %s></td><td><input type=radio name='sst' value=off %s></td><td>Service Status</td></tr>\n", (show_service_status == TRUE) ? "checked" : "", (show_service_status == FALSE) ? "checked" : ""); printf("<tr><td><input type=radio name='cmd' value=on %s></td><td><input type=radio name='cmd' value=off %s></td><td>External Commands</td></tr>\n", (show_external_commands == TRUE) ? "checked" : "", (show_external_commands == FALSE) ? "checked" : ""); printf("<tr><td><input type=radio name='sms' value=on %s></td><td><input type=radio name='sms' value=off %s></td><td>System Messages</td></tr>\n", (show_system_messages == TRUE) ? "checked" : "", (show_system_messages == FALSE) ? "checked" : ""); printf("<tr><td><input type=radio name='evh' value=on %s></td><td><input type=radio name='evh' value=off %s></td><td>Event Handler</td></tr>\n", (show_event_handler == TRUE) ? "checked" : "", (show_event_handler == FALSE) ? "checked" : ""); printf("<tr><td><input type=radio name='flp' value=on %s></td><td><input type=radio name='flp' value=off %s></td><td>Flapping</td></tr>\n", (show_flapping == TRUE) ? "checked" : "", (show_flapping == FALSE) ? "checked" : ""); printf("<tr><td><input type=radio name='dwn' value=on %s></td><td><input type=radio name='dwn' value=off %s></td><td>Downtime</td></tr>\n", (show_downtime == TRUE) ? "checked" : "", (show_downtime == FALSE) ? "checked" : ""); printf("</table>\n"); printf("</td></tr>\n"); /* submit Button */ printf("<tr><td><input type='submit' value='Apply'></td><td align=right><input type='reset' value='Reset' onClick=\"window.location.href='%s?order=new2old&timeperiod=singleday&limit=%d'\"> </td></tr>\n", SHOWLOG_CGI, result_limit); printf("</table>\n"); escape_html_tags = temp_htmlencode; return; }
int __cdecl CAimProto::SendMsg(MCONTACT hContact, int flags, const char* pszSrc) { if (pszSrc == NULL) return 0; if (state != 1) { msg_ack_param *msg_ack = (msg_ack_param*)mir_calloc(sizeof(msg_ack_param)); msg_ack->hContact = hContact; msg_ack->msg = "Message cannot be sent, when protocol offline"; ForkThread(&CAimProto::msg_ack_success, msg_ack); } char *sn = getStringA(hContact, AIM_KEY_SN); if (sn == NULL) { msg_ack_param *msg_ack = (msg_ack_param*)mir_calloc(sizeof(msg_ack_param)); msg_ack->hContact = hContact; msg_ack->msg = "Screen Name for the contact not available"; ForkThread(&CAimProto::msg_ack_success, msg_ack); } char* msg; if (flags & PREF_UNICODE) { const char* p = strchr(pszSrc, '\0'); if (p != pszSrc) { while (*(++p) == '\0'); } msg = mir_utf8encodeW((wchar_t*)p); } else if (flags & PREF_UTF) msg = mir_strdup(pszSrc); else msg = mir_utf8encode(pszSrc); char* smsg = html_encode(msg); mir_free(msg); if (getByte(AIM_KEY_FO, 1)) { msg = bbcodes_to_html(smsg); mir_free(smsg); } else msg = smsg; bool blast = getBool(hContact, AIM_KEY_BLS, false); int res = aim_send_message(hServerConn, seqno, sn, msg, false, blast); mir_free(msg); mir_free(sn); if (!res || blast || 0 == getByte(AIM_KEY_DC, 1)) { msg_ack_param *msg_ack = (msg_ack_param*)mir_alloc(sizeof(msg_ack_param)); msg_ack->hContact = hContact; msg_ack->msg = NULL; msg_ack->id = res; msg_ack->success = res != 0; ForkThread(&CAimProto::msg_ack_success, msg_ack); } return res; }
void display_notifications(void) { char *temp_buffer; char *error_text = NULL; char date_time[MAX_DATETIME_LENGTH]; char alert_level[MAX_INPUT_BUFFER]; char alert_level_class[MAX_INPUT_BUFFER]; char contact_name[MAX_INPUT_BUFFER]; char service_name[MAX_INPUT_BUFFER]; char host_name[MAX_INPUT_BUFFER]; char method_name[MAX_INPUT_BUFFER]; char displayed_host_name[MAX_INPUT_BUFFER]; char displayed_service_desc[MAX_INPUT_BUFFER]; int show_entry; int total_notifications = 0; int displayed_entries = 0; int notification_detail_type = NOTIFICATION_SERVICE_CRITICAL; int status = READLOG_OK; int odd = 0; int json_start = TRUE; host *temp_host = NULL; service *temp_service = NULL; hostgroup *temp_hostgroup = NULL; servicegroup *temp_servicegroup = NULL; logentry *temp_entry = NULL; logentry *entry_list = NULL; logfilter *filter_list = NULL; if (query_type == DISPLAY_HOSTGROUPS) { temp_hostgroup = find_hostgroup(query_hostgroup_name); if (temp_hostgroup == NULL) { print_generic_error_message("There are no host groups with this name defined.", NULL, 0); return; } /* make sure the user is authorized to view this hostgroup */ if (show_partial_hostgroups == FALSE && is_authorized_for_hostgroup(temp_hostgroup, ¤t_authdata) == FALSE) { print_generic_error_message("It appears as though you do not have permission to view information for the host group you requested...", "If you believe this is an error, check the HTTP server authentication requirements for accessing this CGI and check the authorization options in your CGI configuration file.", 0); return; } } if (query_type == DISPLAY_SERVICEGROUPS) { temp_servicegroup = find_servicegroup(query_servicegroup_name); if (temp_servicegroup == NULL) { print_generic_error_message("There are no service groups with this name defined.", NULL, 0); return; } /* make sure the user is authorized to view this servicegroup */ if (show_partial_servicegroups == FALSE && is_authorized_for_servicegroup(temp_servicegroup, ¤t_authdata) == FALSE) { print_generic_error_message("It appears as though you do not have permission to view information for the service group you requested...", "If you believe this is an error, check the HTTP server authentication requirements for accessing this CGI and check the authorization options in your CGI configuration file.", 0); return; } } add_log_filter(&filter_list, LOGENTRY_HOST_NOTIFICATION, LOGFILTER_INCLUDE); add_log_filter(&filter_list, LOGENTRY_SERVICE_NOTIFICATION, LOGFILTER_INCLUDE); /* scan the log file for notification data */ status = get_log_entries(&entry_list, &filter_list, &error_text, NULL, reverse, ts_start, ts_end); free_log_filters(&filter_list); /* dealing with errors */ if (status == READLOG_ERROR_WARNING) { if (error_text != NULL) { print_generic_error_message(error_text, NULL, 0); my_free(error_text); } else print_generic_error_message("Unkown error!", NULL, 0); } if (status == READLOG_ERROR_MEMORY) print_generic_error_message("Out of memory...", "showing all I could get!", 0); if (status == READLOG_ERROR_FATAL) { if (error_text != NULL) { print_generic_error_message(error_text, NULL, 0); my_free(error_text); } return; /* now we start displaying the notification entries */ } else { if (content_type == JSON_CONTENT) { if (status != READLOG_OK) printf(",\n"); printf("\"notifications\": [\n"); } else if (content_type == CSV_CONTENT) { printf("%sHOST%s%s", csv_data_enclosure, csv_data_enclosure, csv_delimiter); printf("%sSERVICE%s%s", csv_data_enclosure, csv_data_enclosure, csv_delimiter); printf("%sTYPE%s%s", csv_data_enclosure, csv_data_enclosure, csv_delimiter); printf("%sTIME%s%s", csv_data_enclosure, csv_data_enclosure, csv_delimiter); printf("%sCONTACT%s%s", csv_data_enclosure, csv_data_enclosure, csv_delimiter); printf("%sNOTIFICATION_COMMAND%s%s", csv_data_enclosure, csv_data_enclosure, csv_delimiter); printf("%sINFORMATION%s\n", csv_data_enclosure, csv_data_enclosure); } else { printf("<table border='0' class='notifications' align='center'>\n"); /* add export to csv, json, link */ printf("<tr><td colspan='7'>"); printf("<table width='100%%' cellspacing='0' cellpadding='0'><tr><td width='33%%'></td><td width='33%%' nowrap>"); printf("<div class='page_selector'>\n"); printf("<div id='page_navigation_copy'></div>"); page_limit_selector(result_start); printf("</div>\n"); printf("</td><td width='33%%' align='right' style='padding-right:2px'>\n"); printf("<div class='csv_export_link'>"); print_export_link(CSV_CONTENT, NOTIFICATIONS_CGI, NULL); print_export_link(JSON_CONTENT, NOTIFICATIONS_CGI, NULL); print_export_link(HTML_CONTENT, NOTIFICATIONS_CGI, NULL); printf("</div></td></tr></table>"); printf("<tr>\n"); printf("<th class='notifications'>Host</th>\n"); printf("<th class='notifications'>Service</th>\n"); printf("<th class='notifications'>Type</th>\n"); printf("<th class='notifications'>Time</th>\n"); printf("<th class='notifications'>Contact</th>\n"); printf("<th class='notifications'>Notification Command</th>\n"); printf("<th class='notifications'>Information</th>\n"); printf("</tr>\n"); } /* check all entries */ for (temp_entry = entry_list; temp_entry != NULL; temp_entry = temp_entry->next) { /* get the date/time */ get_time_string(&temp_entry->timestamp, date_time, (int)sizeof(date_time), SHORT_DATE_TIME); strip(date_time); /* get the contact name */ temp_buffer = (char *)strtok(temp_entry->entry_text, ":"); temp_buffer = (char *)strtok(NULL, ";"); snprintf(contact_name, sizeof(contact_name), "%s", (temp_buffer == NULL) ? "" : temp_buffer + 1); contact_name[sizeof(contact_name) - 1] = '\x0'; /* get the host name */ temp_buffer = (char *)strtok(NULL, ";"); snprintf(host_name, sizeof(host_name), "%s", (temp_buffer == NULL) ? "" : temp_buffer); host_name[sizeof(host_name) - 1] = '\x0'; /* get the service name */ service_name[0] = '\x0'; if (temp_entry->type == LOGENTRY_SERVICE_NOTIFICATION) { temp_buffer = (char *)strtok(NULL, ";"); snprintf(service_name, sizeof(service_name), "%s", (temp_buffer == NULL) ? "" : temp_buffer); service_name[sizeof(service_name) - 1] = '\x0'; } /* get the alert level */ temp_buffer = (char *)strtok(NULL, ";"); snprintf(alert_level, sizeof(alert_level), "%s", (temp_buffer == NULL) ? "" : temp_buffer); alert_level[sizeof(alert_level) - 1] = '\x0'; if (temp_entry->type == LOGENTRY_SERVICE_NOTIFICATION) { if (!strcmp(alert_level, "CRITICAL")) { notification_detail_type = NOTIFICATION_SERVICE_CRITICAL; strcpy(alert_level_class, "CRITICAL"); } else if (!strcmp(alert_level, "WARNING")) { notification_detail_type = NOTIFICATION_SERVICE_WARNING; strcpy(alert_level_class, "WARNING"); } else if (!strcmp(alert_level, "RECOVERY") || !strcmp(alert_level, "OK")) { strcpy(alert_level, "OK"); notification_detail_type = NOTIFICATION_SERVICE_RECOVERY; strcpy(alert_level_class, "OK"); } else if (strstr(alert_level, "CUSTOM (")) { notification_detail_type = NOTIFICATION_SERVICE_CUSTOM; strcpy(alert_level_class, "CUSTOM"); } else if (strstr(alert_level, "ACKNOWLEDGEMENT (")) { notification_detail_type = NOTIFICATION_SERVICE_ACK; strcpy(alert_level_class, "ACKNOWLEDGEMENT"); } else if (strstr(alert_level, "FLAPPINGSTART (")) { strcpy(alert_level, "FLAPPING START"); notification_detail_type = NOTIFICATION_SERVICE_FLAP; strcpy(alert_level_class, "UNKNOWN"); } else if (strstr(alert_level, "FLAPPINGSTOP (")) { strcpy(alert_level, "FLAPPING STOP"); notification_detail_type = NOTIFICATION_SERVICE_FLAP; strcpy(alert_level_class, "UNKNOWN"); } else { strcpy(alert_level, "UNKNOWN"); notification_detail_type = NOTIFICATION_SERVICE_UNKNOWN; strcpy(alert_level_class, "UNKNOWN"); } } else { if (!strcmp(alert_level, "DOWN")) { strncpy(alert_level, "HOST DOWN", sizeof(alert_level)); strcpy(alert_level_class, "HOSTDOWN"); notification_detail_type = NOTIFICATION_HOST_DOWN; } else if (!strcmp(alert_level, "UNREACHABLE")) { strncpy(alert_level, "HOST UNREACHABLE", sizeof(alert_level)); strcpy(alert_level_class, "HOSTUNREACHABLE"); notification_detail_type = NOTIFICATION_HOST_UNREACHABLE; } else if (!strcmp(alert_level, "RECOVERY") || !strcmp(alert_level, "UP")) { strncpy(alert_level, "HOST UP", sizeof(alert_level)); strcpy(alert_level_class, "HOSTUP"); notification_detail_type = NOTIFICATION_HOST_RECOVERY; } else if (strstr(alert_level, "CUSTOM (")) { strcpy(alert_level_class, "HOSTCUSTOM"); notification_detail_type = NOTIFICATION_HOST_CUSTOM; } else if (strstr(alert_level, "ACKNOWLEDGEMENT (")) { strcpy(alert_level_class, "HOSTACKNOWLEDGEMENT"); notification_detail_type = NOTIFICATION_HOST_ACK; } else if (strstr(alert_level, "FLAPPINGSTART (")) { strcpy(alert_level, "FLAPPING START"); strcpy(alert_level_class, "UNKNOWN"); notification_detail_type = NOTIFICATION_HOST_FLAP; } else if (strstr(alert_level, "FLAPPINGSTOP (")) { strcpy(alert_level, "FLAPPING STOP"); strcpy(alert_level_class, "UNKNOWN"); notification_detail_type = NOTIFICATION_HOST_FLAP; } } /* get the method name */ temp_buffer = (char *)strtok(NULL, ";"); snprintf(method_name, sizeof(method_name), "%s", (temp_buffer == NULL) ? "" : temp_buffer); method_name[sizeof(method_name) - 1] = '\x0'; /* move to the informational message */ temp_buffer = strtok(NULL, ";"); show_entry = FALSE; /* if we're searching by contact, filter out unwanted contact */ if (query_type == DISPLAY_CONTACTS) { if (find_all == TRUE) show_entry = TRUE; else if (!strcmp(query_contact_name, contact_name)) show_entry = TRUE; } /* search host */ else if (query_type == DISPLAY_HOSTS) { if (find_all == TRUE) show_entry = TRUE; else if (!strcmp(query_host_name, host_name)) show_entry = TRUE; } /* searching service */ else if (query_type == DISPLAY_SERVICES) { if (!strcmp(query_host_name, host_name) && !strcmp(query_svc_description, service_name)) show_entry = TRUE; } /* Set TRUE here, get's checked later on */ else if (query_type == DISPLAY_HOSTGROUPS || query_type == DISPLAY_SERVICEGROUPS) { show_entry = TRUE; } if (show_entry == TRUE) { if (notification_options == NOTIFICATION_ALL) show_entry = TRUE; else if (notification_options == NOTIFICATION_HOST_ALL && temp_entry->type == LOGENTRY_HOST_NOTIFICATION) show_entry = TRUE; else if (notification_options == NOTIFICATION_SERVICE_ALL && temp_entry->type == LOGENTRY_SERVICE_NOTIFICATION) show_entry = TRUE; else if (notification_detail_type & notification_options) show_entry = TRUE; else show_entry = FALSE; } /* make sure user has authorization to view this notification */ temp_host = find_host(host_name); if (temp_entry->type == LOGENTRY_SERVICE_NOTIFICATION) temp_service = find_service(host_name, service_name); if (temp_host != NULL) { snprintf(displayed_host_name, sizeof(displayed_host_name), "%s", (temp_host->display_name != NULL && content_type == HTML_CONTENT) ? temp_host->display_name : temp_host->name); displayed_host_name[sizeof(displayed_host_name) - 1] = '\x0'; if (temp_entry->type == LOGENTRY_HOST_NOTIFICATION) { if (is_authorized_for_host(temp_host, ¤t_authdata) == FALSE) show_entry = FALSE; else if (query_type == DISPLAY_HOSTGROUPS && is_host_member_of_hostgroup(temp_hostgroup, temp_host) == FALSE) show_entry = FALSE; else if (query_type == DISPLAY_SERVICEGROUPS && is_host_member_of_servicegroup(temp_servicegroup, temp_host) == FALSE) show_entry = FALSE; } else { if (temp_service != NULL) { snprintf(displayed_service_desc, sizeof(displayed_service_desc), "%s", (temp_service->display_name != NULL && content_type == HTML_CONTENT) ? temp_service->display_name : temp_service->description); displayed_service_desc[sizeof(displayed_service_desc) - 1] = '\x0'; if (is_authorized_for_service(temp_service, ¤t_authdata) == FALSE) show_entry = FALSE; else if (query_type == DISPLAY_HOSTGROUPS && is_host_member_of_hostgroup(temp_hostgroup, temp_host) == FALSE) show_entry = FALSE; else if (query_type == DISPLAY_SERVICEGROUPS && is_service_member_of_servicegroup(temp_servicegroup, temp_service) == FALSE) show_entry = FALSE; } else { if (is_authorized_for_all_services(¤t_authdata) == FALSE) show_entry = FALSE; snprintf(displayed_service_desc, sizeof(displayed_service_desc), "%s", service_name); displayed_service_desc[sizeof(displayed_service_desc) - 1] = '\x0'; } } } else { if (temp_entry->type == LOGENTRY_HOST_NOTIFICATION) { if (is_authorized_for_all_hosts(¤t_authdata) == FALSE) show_entry = FALSE; } else { if (is_authorized_for_all_services(¤t_authdata) == FALSE) show_entry = FALSE; snprintf(displayed_service_desc, sizeof(displayed_service_desc), "%s", service_name); displayed_service_desc[sizeof(displayed_service_desc) - 1] = '\x0'; } snprintf(displayed_host_name, sizeof(displayed_host_name), "%s", host_name); displayed_host_name[sizeof(displayed_host_name) - 1] = '\x0'; } if (show_entry == TRUE) { if (result_limit != 0 && (((total_notifications + 1) < result_start) || (total_notifications >= ((result_start + result_limit) - 1)))) { total_notifications++; continue; } displayed_entries++; total_notifications++; if (odd) odd = 0; else odd = 1; if (content_type == JSON_CONTENT) { if (json_start == FALSE) printf(",\n"); printf("{\"host_name\": \"%s\", ", json_encode(temp_host->name)); printf("\"host_display_name\": \"%s\", ", (temp_host->display_name != NULL) ? json_encode(temp_host->display_name) : json_encode(temp_host->name)); if (temp_entry->type == LOGENTRY_SERVICE_NOTIFICATION) { printf("\"service_description\": \"%s\", ", json_encode(temp_service->description)); printf("\"service_display_name\": \"%s\", ", (temp_service->display_name != NULL) ? json_encode(temp_service->display_name) : json_encode(temp_service->description)); } else { printf("\"service_description\": null, "); printf("\"service_display_name\": null, "); } printf("\"type\": \"%s\", ", alert_level); printf("\"time\": \"%s\", ", date_time); printf("\"contact\": \"%s\", ", json_encode(contact_name)); printf("\"notification_command\": \"%s\", ", json_encode(method_name)); printf("\"information\": \"%s\"}", json_encode(escape_newlines(temp_buffer))); } else if (content_type == CSV_CONTENT) { printf("%s%s%s%s", csv_data_enclosure, displayed_host_name, csv_data_enclosure, csv_delimiter); if (temp_entry->type == LOGENTRY_SERVICE_NOTIFICATION) printf("%s%s%s%s", csv_data_enclosure, displayed_service_desc, csv_data_enclosure, csv_delimiter); else printf("%sN/A%s%s", csv_data_enclosure, csv_data_enclosure, csv_delimiter); printf("%s%s%s%s", csv_data_enclosure, alert_level, csv_data_enclosure, csv_delimiter); printf("%s%s%s%s", csv_data_enclosure, date_time, csv_data_enclosure, csv_delimiter); printf("%s%s%s%s", csv_data_enclosure, contact_name, csv_data_enclosure, csv_delimiter); printf("%s%s%s%s", csv_data_enclosure, method_name, csv_data_enclosure, csv_delimiter); printf("%s%s%s\n", csv_data_enclosure, escape_newlines(temp_buffer), csv_data_enclosure); } else { printf("<tr class='notifications%s'>\n", (odd) ? "Even" : "Odd"); if (temp_host != NULL) printf("<td class='notifications%s'><a href='%s?type=%d&host=%s'>%s</a></td>\n", (odd) ? "Even" : "Odd", EXTINFO_CGI, DISPLAY_HOST_INFO, url_encode(host_name), displayed_host_name); else printf("<td class='notifications%s'>%s</td>\n", (odd) ? "Even" : "Odd", displayed_host_name); if (temp_entry->type == LOGENTRY_SERVICE_NOTIFICATION) { if (temp_service != NULL) { printf("<td class='notifications%s'><a href='%s?type=%d&host=%s", (odd) ? "Even" : "Odd", EXTINFO_CGI, DISPLAY_SERVICE_INFO, url_encode(host_name)); printf("&service=%s'>%s</a></td>\n", url_encode(service_name), displayed_service_desc); } else printf("<td class='notifications%s'>%s</td>\n", (odd) ? "Even" : "Odd", displayed_service_desc); } else printf("<td class='notifications%s'>N/A</td>\n", (odd) ? "Even" : "Odd"); printf("<td class='notifications%s'>%s</td>\n", alert_level_class, alert_level); printf("<td class='notifications%s'>%s</td>\n", (odd) ? "Even" : "Odd", date_time); printf("<td class='notifications%s'><a href='%s?type=contacts#%s'>%s</a></td>\n", (odd) ? "Even" : "Odd", CONFIG_CGI, url_encode(contact_name), contact_name); printf("<td class='notifications%s'><a href='%s?type=commands#%s'>%s</a></td>\n", (odd) ? "Even" : "Odd", CONFIG_CGI, url_encode(method_name), method_name); printf("<td class='notifications%s'>%s</td>\n", (odd) ? "Even" : "Odd", html_encode(temp_buffer, FALSE)); printf("</tr>\n"); } if (json_start == TRUE) json_start = FALSE; } } } free_log_entries(&entry_list); if (content_type != CSV_CONTENT && content_type != JSON_CONTENT) { printf("</table>\n"); if (total_notifications == 0) { printf("<div class='errorMessage' style='text-align:center;'>No notifications have been recorded"); if (find_all == FALSE) { if (query_type == DISPLAY_SERVICES) printf(" for this service"); else if (query_type == DISPLAY_CONTACTS) printf(" for this contact"); else printf(" for this host"); } printf(" in log files for selected date.</div>"); } page_num_selector(result_start, total_notifications, displayed_entries); } else if (content_type == JSON_CONTENT) { printf("\n]\n"); } return; }
/* int analyze_server_response(int socket) return 0 or 1 when the cond regex is matched return -1 if no response from server */ int analyze_server_response(int s) { int runs = 0; while ((buf = hydra_receive_line(s)) != NULL) { runs++; //check for http redirection if (strstr(buf, "HTTP/1.1 3") != NULL || strstr(buf, "HTTP/1.0 3") != NULL || strstr(buf, "Status: 3") != NULL) { redirected_flag = 1; } else if (strstr(buf, "HTTP/1.1 401") != NULL || strstr(buf, "HTTP/1.0 401") != NULL) { auth_flag = 1; } else if ((strstr(buf, "HTTP/1.1 403") != NULL) || (strstr(buf, "HTTP/1.1 404") != NULL) || (strstr(buf, "HTTP/1.0 403") != NULL) || (strstr(buf, "HTTP/1.0 404") != NULL)) { return 0; } if (hydra_strcasestr(buf, "Location: ") != NULL) { char *startloc, *endloc; char str[2048]; startloc = hydra_strcasestr(buf, "Location: ") + strlen("Location: "); strncpy(str, startloc, sizeof(str) - 1); str[sizeof(str) - 1] = 0; endloc = strchr(str, '\n'); if (endloc != NULL) *endloc = 0; endloc = strchr(str, '\r'); if (endloc != NULL) *endloc = 0; strcpy(redirected_url_buff, str); } //there can be multiple cookies if (hydra_strcasestr(buf, "Set-Cookie: ") != NULL) { char *cookiebuf = buf; do { char *startcookie, *endcookie1, *endcookie2; char str[1024], tmpcookie[4096] = "", tmpname[128] = "", *ptr, *ptr2; memset(str, 0, sizeof(str)); startcookie = hydra_strcasestr(cookiebuf, "Set-Cookie: ") + strlen("Set-Cookie: "); strncpy(str, startcookie, sizeof(str) - 1); str[sizeof(str) - 1] = 0; endcookie1 = strchr(str, '\n'); endcookie2 = strchr(str, ';'); //terminate string after cookie data if (endcookie1 != NULL && endcookie1 < endcookie2) *endcookie1 = 0; else if (endcookie2 != NULL) *endcookie2 = 0; // is the cookie already there? if yes, remove it! if (index(startcookie, '=') != NULL && (ptr = index(startcookie, '=')) - startcookie + 1 <= sizeof(tmpname)) { strncpy(tmpname, startcookie, sizeof(tmpname) - 2); tmpname[sizeof(tmpname) - 2] = 0; ptr = index(tmpname, '='); *(++ptr) = 0; // is the cookie already in the cookiejar? (so, does it have to be replaced?) if ((ptr = hydra_strcasestr(cookie, tmpname)) != NULL) { // yes it is. // if the cookie is not in the beginning of the cookiejar, copy the ones before if (ptr != cookie && *(ptr - 1) == ' ') { strncpy(tmpcookie, cookie, ptr - cookie - 2); tmpcookie[ptr - cookie - 2] = 0; } ptr += strlen(tmpname); // if there are any cookies after this one in the cookiejar, copy them over if ((ptr2 = strstr(ptr, "; ")) != NULL) { ptr2 += 2; strncat(tmpcookie, ptr2, sizeof(tmpcookie) - strlen(tmpcookie) - 1); } if (debug) printf("[DEBUG] removing cookie %s in jar\n before: %s\n after: %s\n", tmpname, cookie, tmpcookie); strcpy(cookie, tmpcookie); } } ptr = index(str, '='); // only copy the cookie if it has a value (otherwise the server wants to delete the cookie if (ptr != NULL && *(ptr + 1) != ';' && *(ptr + 1) != 0 && *(ptr + 1) != '\n' && *(ptr + 1) != '\r') { if (strlen(cookie) > 0) strncat(cookie, "; ", sizeof(cookie) - strlen(cookie) - 1); strncat(cookie, str, sizeof(cookie) - strlen(cookie) - 1); } cookiebuf = startcookie; } while (hydra_strcasestr(cookiebuf, "Set-Cookie: ") != NULL); } #ifdef HAVE_PCRE if (hydra_string_match(buf, cond) == 1) { #else if (strstr(buf, cond) != NULL) { #endif free(buf); // printf("DEBUG: STRING %s FOUND!!:\n%s\n", cond, buf); return 1; } // else printf("DEBUG: STRING %s NOT FOUND:\n%s\n", cond, buf); free(buf); } if (runs == 0) { if (debug) hydra_report(stderr, "DEBUG: no response from server\n"); return -1; } return 0; } void hydra_reconnect(int s, char *ip, int port, unsigned char options) { if (s >= 0) s = hydra_disconnect(s); if ((options & OPTION_SSL) == 0) { s = hydra_connect_tcp(ip, port); } else { s = hydra_connect_ssl(ip, port); } } int start_http_form(int s, char *ip, int port, unsigned char options, char *miscptr, FILE * fp, char *type) { char *empty = ""; char *login, *pass, buffer[9000], clogin[256], cpass[256]; char header[8096], *upd3variables, cuserheader[1024]; int found = !success_cond, i, j; memset(header, 0, sizeof(header)); cookie[0] = 0; // reset cookies from potential previous attempt if (strlen(login = hydra_get_next_login()) == 0) login = empty; if (strlen(pass = hydra_get_next_password()) == 0) pass = empty; strncpy(clogin, html_encode(login), sizeof(clogin) - 1); clogin[sizeof(clogin) - 1] = 0; strncpy(cpass, html_encode(pass), sizeof(cpass) - 1); cpass[sizeof(cpass) - 1] = 0; upd3variables = hydra_strrep(variables, "^USER^", clogin); upd3variables = hydra_strrep(upd3variables, "^PASS^", cpass); if (strstr(userheader, "^USER^") == NULL && strstr(userheader, "^PASS^") == NULL) { strcpy(cuserheader, userheader); } else { // we use the encoded version strncpy(cuserheader, hydra_strrep(userheader, "^USER^", clogin), sizeof(cuserheader) - 1); cuserheader[sizeof(cuserheader) - 1] = 0; strncpy(cuserheader, hydra_strrep(cuserheader, "^PASS^", cpass), sizeof(cuserheader) - 1); cuserheader[sizeof(cuserheader) - 1] = 0; } /* again: no snprintf to be portable. dont worry, buffer cant overflow */ if (use_proxy == 1 && proxy_authentication != NULL) { // proxy with authentication if (getcookie) { //doing a GET to save cookies sprintf(buffer, "GET http://%s:%d%.600s HTTP/1.0\r\nHost: %s\r\nProxy-Authorization: Basic %s\r\nUser-Agent: Mozilla 5.0 (Hydra Proxy Auth)\r\n%s%s\r\n", webtarget, webport, cookieurl, webtarget, proxy_authentication, header, cuserheader); if (hydra_send(s, buffer, strlen(buffer), 0) < 0) { return 1; } i = analyze_server_response(s); // return value ignored if (strlen(cookie) > 0) { sprintf(header, "Cookie: %s\r\n", cookie); } hydra_reconnect(s, ip, port, options); } if (strcmp(type, "POST") == 0) { sprintf(buffer, "POST http://%s:%d%.600s HTTP/1.0\r\nHost: %s\r\nProxy-Authorization: Basic %s\r\nUser-Agent: Mozilla/5.0 (Hydra Proxy Auth)\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: %d\r\n%s%s\r\n%s", webtarget, webport, url, webtarget, proxy_authentication, (int) strlen(upd3variables), header, cuserheader, upd3variables); if (hydra_send(s, buffer, strlen(buffer), 0) < 0) { return 1; } } else { sprintf(buffer, "GET http://%s:%d%.600s?%s HTTP/1.0\r\nHost: %s\r\nProxy-Authorization: Basic %s\r\nUser-Agent: Mozilla/5.0 (Hydra Proxy Auth)\r\n%s%s\r\n", webtarget, webport, url, upd3variables, webtarget, proxy_authentication, header, cuserheader); if (hydra_send(s, buffer, strlen(buffer), 0) < 0) { return 1; } } } else { if (use_proxy == 1) { // proxy without authentication if (getcookie) { //doing a GET to get cookies sprintf(buffer, "GET http://%s:%d%.600s HTTP/1.0\r\nHost: %s\r\nUser-Agent: Mozilla/5.0 (Hydra Proxy)\r\n%s%s\r\n", webtarget, webport, cookieurl, webtarget, header, cuserheader); if (hydra_send(s, buffer, strlen(buffer), 0) < 0) { return 1; } i = analyze_server_response(s); // ignore result if (strlen(cookie) > 0) { sprintf(header, "Cookie: %s\r\n", cookie); } hydra_reconnect(s, ip, port, options); } if (strcmp(type, "POST") == 0) { sprintf(buffer, "POST http://%s:%d%.600s HTTP/1.0\r\nHost: %s\r\nUser-Agent: Mozilla/5.0 (Hydra)\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: %d\r\n%s%s\r\n%s", webtarget, webport, url, webtarget, (int) strlen(upd3variables), header, cuserheader, upd3variables); if (hydra_send(s, buffer, strlen(buffer), 0) < 0) { return 1; } } else { sprintf(buffer, "GET http://%s:%d%.600s?%s HTTP/1.0\r\nHost: %s\r\nUser-Agent: Mozilla/5.0 (Hydra)\r\n%s%s\r\n", webtarget, webport, url, upd3variables, webtarget, header, cuserheader); if (hydra_send(s, buffer, strlen(buffer), 0) < 0) { return 1; } } } else { // direct web server, no proxy if (getcookie) { //doing a GET to save cookies sprintf(buffer, "GET %.600s HTTP/1.0\r\nHost: %s\r\nUser-Agent: Mozilla/5.0 (Hydra)\r\n%s\r\n", cookieurl, webtarget, cuserheader); if (hydra_send(s, buffer, strlen(buffer), 0) < 0) { return 1; } i = analyze_server_response(s); // ignore result if (strlen(cookie) > 0) { sprintf(header, "Cookie: %s\r\n", cookie); } hydra_reconnect(s, ip, port, options); } if (strcmp(type, "POST") == 0) { sprintf(buffer, "POST %.600s HTTP/1.0\r\nHost: %s\r\nUser-Agent: Mozilla/5.0 (Hydra)\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: %d\r\n%s%s\r\n%s", url, webtarget, (int) strlen(upd3variables), header, cuserheader, upd3variables); if (hydra_send(s, buffer, strlen(buffer), 0) < 0) { return 1; } } else { sprintf(buffer, "GET %.600s?%s HTTP/1.0\r\nHost: %s\r\nUser-Agent: Mozilla/5.0 (Hydra)\r\n%s%s\r\n", url, upd3variables, webtarget, header, cuserheader); if (hydra_send(s, buffer, strlen(buffer), 0) < 0) { return 1; } } } } found = analyze_server_response(s); if (auth_flag) { // we received a 401 error - user using wrong module hydra_report(stderr, "[ERROR] the target is using HTTP auth, not a web form, received HTTP error code 401. Use module \"http%s-get\" instead.\n", (options & OPTION_SSL) > 0 ? "s" : ""); return 4; } if (strlen(cookie) > 0) { sprintf(header, "Cookie: %.1000s\r\n", cookie); } //if page was redirected, follow the location header redirected_cpt = MAX_REDIRECT; if (debug) printf("[DEBUG] attempt result: found %d, redirect %d, location: %s\n", found, redirected_flag, redirected_url_buff); while (found == 0 && redirected_flag && (redirected_url_buff[0] != 0) && (redirected_cpt > 0)) { //we have to split the location char *startloc, *endloc; char str[2048]; char str2[2048]; char str3[2048]; redirected_cpt--; redirected_flag = 0; //check if the redirect page contains the fail/success condition #ifdef HAVE_PCRE if (hydra_string_match(redirected_url_buff, cond) == 1) { #else if (strstr(redirected_url_buff, cond) != NULL) { #endif found = success_cond; } else { //location could be either absolute http(s):// or / something //or relative startloc = strstr(redirected_url_buff, "://"); if (startloc != NULL) { startloc += strlen("://"); if ((endloc = strchr(startloc, '\r')) != NULL) { startloc[endloc - startloc] = 0; } if ((endloc = strchr(startloc, '\n')) != NULL) { startloc[endloc - startloc] = 0; } strncpy(str, startloc, sizeof(str)); str[sizeof(str) - 1] = 0; endloc = strchr(str, '/'); if (endloc != NULL) { strncpy(str2, str, endloc - str); str2[endloc - str] = 0; } else strncpy(str2, str, sizeof(str)); if (strlen(str) - strlen(str2) == 0) { strcpy(str3, "/"); } else { strncpy(str3, str + strlen(str2), strlen(str) - strlen(str2) - 1); str3[strlen(str) - strlen(str2) - 1] = 0; } } else { strncpy(str2, webtarget, sizeof(str2) - 1); str2[sizeof(str2) - 1] = 0; if (redirected_url_buff[0] != '/') { //it's a relative path, so we have to concatenate it //with the path from the first url given char *urlpath; char urlpath_extracted[2048]; memset(urlpath_extracted, 0, sizeof(urlpath_extracted)); urlpath = strrchr(url, '/'); if (urlpath != NULL) { strncpy(urlpath_extracted, url, urlpath - url); sprintf(str3, "%.1000s/%.1000s", urlpath_extracted, redirected_url_buff); } else { sprintf(str3, "%.1000s/%.1000s", url, redirected_url_buff); } } else strncpy(str3, redirected_url_buff, sizeof(str3)); if (debug) hydra_report(stderr, "[DEBUG] host=%s redirect=%s origin=%s\n", str2, str3, url); } if (str3[0] != '/') { j = strlen(str3); str3[j + 1] = 0; for (i = j; i > 0; i--) str3[i] = str3[i - 1]; str3[0] = '/'; } if (verbose) hydra_report(stderr, "[VERBOSE] Page redirected to http://%s%s\n", str2, str3); //re-use the code above to check for proxy use if (use_proxy == 1 && proxy_authentication != NULL) { // proxy with authentication sprintf(buffer, "GET http://%s:%d%.600s HTTP/1.0\r\nHost: %s\r\nProxy-Authorization: Basic %s\r\nUser-Agent: Mozilla/4.0 (Hydra)\r\n%s\r\n", webtarget, webport, str3, str2, proxy_authentication, header); } else { if (use_proxy == 1) { // proxy without authentication sprintf(buffer, "GET http://%s:%d%.600s HTTP/1.0\r\nHost: %s\r\nUser-Agent: Mozilla/4.0 (Hydra)\r\n%s\r\n", webtarget, webport, str3, str2, header); } else { //direct web server, no proxy sprintf(buffer, "GET %.600s HTTP/1.0\r\nHost: %s\r\nUser-Agent: Mozilla/4.0 (Hydra)\r\n%s\r\n", str3, str2, header); } } hydra_reconnect(s, ip, port, options); if (hydra_send(s, buffer, strlen(buffer), 0) < 0) { return 1; } found = analyze_server_response(s); if (strlen(cookie) > 0) { sprintf(header, "Cookie: %s\r\n", cookie); } } } //if the last status is still 3xx, set it as a false if (found != -1 && found == success_cond && redirected_flag == 0 && redirected_cpt >= 0) { hydra_report_found_host(port, ip, "www-form", fp); hydra_completed_pair_found(); } else { hydra_completed_pair(); } return 1; } void service_http_form(char *ip, int sp, unsigned char options, char *miscptr, FILE * fp, int port, char *type) { int run = 1, next_run = 1, sock = -1; int myport = PORT_HTTP, mysslport = PORT_HTTP_SSL; char *ptr, *ptr2; hydra_register_socket(sp); if (webtarget != NULL && (webtarget = strstr(miscptr, "://")) != NULL) { webtarget += strlen("://"); if ((ptr2 = index(webtarget, ':')) != NULL) { /* step over port if present */ *ptr2 = 0; ptr2++; ptr = ptr2; if (*ptr == '/' || (ptr = index(ptr2, '/')) != NULL) miscptr = ptr; else miscptr = slash; /* to make things easier to user */ } else if ((ptr2 = index(webtarget, '/')) != NULL) { if (freemischttpform == 0) { freemischttpform = 1; miscptr = malloc(strlen(ptr2) + 1); strcpy(miscptr, ptr2); *ptr2 = 0; } } else webtarget = NULL; } if (cmdlinetarget != NULL && webtarget == NULL) webtarget = cmdlinetarget; else if (webtarget == NULL && cmdlinetarget == NULL) webtarget = hydra_address2string(ip); if (port != 0) webport = port; else if ((options & OPTION_SSL) == 0) webport = myport; else webport = mysslport; sprintf(bufferurl, "%.1000s", miscptr); url = bufferurl; ptr = url; while (*ptr != 0 && (*ptr != ':' || *(ptr - 1) == '\\')) ptr++; if (*ptr != 0) *ptr++ = 0; variables = ptr; while (*ptr != 0 && (*ptr != ':' || *(ptr - 1) == '\\')) ptr++; if (*ptr != 0) *ptr++ = 0; cond = ptr; while (*ptr != 0 && (*ptr != ':' || *(ptr - 1) == '\\')) ptr++; if (*ptr != 0) *ptr++ = 0; optional1 = ptr; if (strstr(url, "\\:") != NULL) { if ((ptr = malloc(strlen(url))) != NULL) { // no need for +1 strcpy(ptr, hydra_strrep(url, "\\:", ":")); url = ptr; } } if (strstr(variables, "\\:") != NULL) { if ((ptr = malloc(strlen(variables))) != NULL) { // no need for +1 strcpy(ptr, hydra_strrep(variables, "\\:", ":")); variables = ptr; } } if (strstr(cond, "\\:") != NULL) { if ((ptr = malloc(strlen(cond))) != NULL) { // no need for +1 strcpy(ptr, hydra_strrep(cond, "\\:", ":")); cond = ptr; } } if (url == NULL || variables == NULL || cond == NULL /*|| optional1 == NULL */ ) hydra_child_exit(2); //printf("url: %s, var: %s, cond: %s, opt: %s\n", url, variables, cond, optional1); if (*cond == 0) { fprintf(stderr, "[ERROR] invalid number of parameters in module option\n"); hydra_child_exit(2); } sprintf(cookieurl, "%.1000s", url); //conditions now have to contain F or S to set the fail or success condition if (*cond != 0 && (strpos(cond, "F=") == 0)) { success_cond = 0; cond += 2; } else if (*cond != 0 && (strpos(cond, "S=") == 0)) { success_cond = 1; cond += 2; } else { //by default condition is a fail success_cond = 0; } while ( /*(optional1 = strtok(NULL, ":")) != NULL */ *optional1 != 0) { switch (optional1[0]) { case 'c': // fall through case 'C': ptr = optional1 + 2; while (*ptr != 0 && (*ptr != ':' || *(ptr - 1) == '\\')) ptr++; if (*ptr != 0) *ptr++ = 0; sprintf(cookieurl, "%.1000s", hydra_strrep(optional1 + 2, "\\:", ":")); optional1 = ptr; break; case 'h': // fall through case 'H': ptr = optional1 + 2; while (*ptr != 0 && (*ptr != ':' || *(ptr - 1) == '\\')) ptr++; if (*ptr != 0) *ptr++ = 0; ptr2 = ptr; while (*ptr2 != 0 && (*ptr2 != ':' || *(ptr2 - 1) == '\\')) ptr2++; if (*ptr2 != 0) *ptr2++ = 0; if (sizeof(userheader) - strlen(userheader) > 4) { strncat(userheader, optional1 + 2, sizeof(userheader) - strlen(userheader) - 4); strcat(userheader, ":"); strncat(userheader, hydra_strrep(ptr, "\\:", ":"), sizeof(userheader) - strlen(userheader) - 3); strcat(userheader, "\r\n"); } optional1 = ptr2; break; // no default } } while (1) { if (run == 2) { if (memcmp(hydra_get_next_pair(), &HYDRA_EXIT, sizeof(HYDRA_EXIT)) == 0) { if (freemischttpform) free(miscptr); freemischttpform = 0; hydra_child_exit(1); } } switch (run) { case 1: /* connect and service init function */ { if (sock >= 0) sock = hydra_disconnect(sock); if ((options & OPTION_SSL) == 0) { if (port != 0) myport = port; sock = hydra_connect_tcp(ip, myport); port = myport; } else { if (port != 0) mysslport = port; sock = hydra_connect_ssl(ip, mysslport); port = mysslport; } if (sock < 0) { hydra_report(stderr, "[ERROR] Child with pid %d terminating, can not connect\n", (int) getpid()); if (freemischttpform) free(miscptr); freemischttpform = 0; hydra_child_exit(1); } next_run = 2; break; } case 2: /* run the cracking function */ next_run = start_http_form(sock, ip, port, options, miscptr, fp, type); break; case 3: /* clean exit */ if (sock >= 0) sock = hydra_disconnect(sock); if (freemischttpform) free(miscptr); freemischttpform = 0; hydra_child_exit(0); break; case 4: /* silent error exit */ if (sock >= 0) sock = hydra_disconnect(sock); if (freemischttpform) free(miscptr); freemischttpform = 0; hydra_child_exit(1); break; default: if (freemischttpform) free(miscptr); freemischttpform = 0; hydra_report(stderr, "[ERROR] Caught unknown return code, exiting!\n"); hydra_child_exit(0); } run = next_run; } if (freemischttpform) free(miscptr); }
void get_history(void) { mmapfile *thefile = NULL; char image[MAX_INPUT_BUFFER]; char image_alt[MAX_INPUT_BUFFER]; char *input = NULL; char *input2 = NULL; char match1[MAX_INPUT_BUFFER]; char match2[MAX_INPUT_BUFFER]; int found_line = FALSE; int system_message = FALSE; int display_line = FALSE; time_t t; char date_time[MAX_DATETIME_LENGTH]; char *temp_buffer = NULL; int history_type = SERVICE_HISTORY; int history_detail_type = HISTORY_SERVICE_CRITICAL; char *entry_host_name = NULL; char *entry_service_desc = NULL; host *temp_host = NULL; service *temp_service = NULL; int result = 0; char last_message_date[MAX_INPUT_BUFFER] = ""; char current_message_date[MAX_INPUT_BUFFER] = ""; struct tm *time_ptr = NULL; if(use_lifo == TRUE) { result = read_file_into_lifo(log_file_to_use); if(result != LIFO_OK) { if(result == LIFO_ERROR_MEMORY) { printf("<P><DIV CLASS='warningMessage'>Not enough memory to reverse log file - displaying history in natural order...</DIV></P>\n"); } else if(result == LIFO_ERROR_FILE) { printf("<HR><P><DIV CLASS='errorMessage'>Error: Cannot open log file '%s' for reading!</DIV></P><HR>", log_file_to_use); return; } use_lifo = FALSE; } } if(use_lifo == FALSE) { if((thefile = mmap_fopen(log_file_to_use)) == NULL) { printf("<HR><P><DIV CLASS='errorMessage'>Error: Cannot open log file '%s' for reading!</DIV></P><HR>", log_file_to_use); return; } } printf("<P><DIV CLASS='logEntries'>\n"); while(1) { my_free(input); my_free(input2); if(use_lifo == TRUE) { if((input = pop_lifo()) == NULL) break; } else { if((input = mmap_fgets(thefile)) == NULL) break; } strip(input); strcpy(image, ""); strcpy(image_alt, ""); system_message = FALSE; if((input2 = (char *)strdup(input)) == NULL) continue; /* service state alerts */ if(strstr(input, "SERVICE ALERT:")) { history_type = SERVICE_HISTORY; /* get host and service names */ temp_buffer = my_strtok(input2, "]"); temp_buffer = my_strtok(NULL, ":"); temp_buffer = my_strtok(NULL, ";"); if(temp_buffer) entry_host_name = strdup(temp_buffer + 1); else entry_host_name = NULL; temp_buffer = my_strtok(NULL, ";"); if(temp_buffer) entry_service_desc = strdup(temp_buffer); else entry_service_desc = NULL; if(strstr(input, ";CRITICAL;")) { strncpy(image, CRITICAL_ICON, sizeof(image)); strncpy(image_alt, CRITICAL_ICON_ALT, sizeof(image_alt)); history_detail_type = HISTORY_SERVICE_CRITICAL; } else if(strstr(input, ";WARNING;")) { strncpy(image, WARNING_ICON, sizeof(image)); strncpy(image_alt, WARNING_ICON_ALT, sizeof(image_alt)); history_detail_type = HISTORY_SERVICE_WARNING; } else if(strstr(input, ";UNKNOWN;")) { strncpy(image, UNKNOWN_ICON, sizeof(image)); strncpy(image_alt, UNKNOWN_ICON_ALT, sizeof(image_alt)); history_detail_type = HISTORY_SERVICE_UNKNOWN; } else if(strstr(input, ";RECOVERY;") || strstr(input, ";OK;")) { strncpy(image, OK_ICON, sizeof(image)); strncpy(image_alt, OK_ICON_ALT, sizeof(image_alt)); history_detail_type = HISTORY_SERVICE_RECOVERY; } } /* service flapping alerts */ else if(strstr(input, "SERVICE FLAPPING ALERT:")) { if(display_flapping_alerts == FALSE) continue; history_type = SERVICE_FLAPPING_HISTORY; /* get host and service names */ temp_buffer = my_strtok(input2, "]"); temp_buffer = my_strtok(NULL, ":"); temp_buffer = my_strtok(NULL, ";"); if(temp_buffer) entry_host_name = strdup(temp_buffer + 1); else entry_host_name = NULL; temp_buffer = my_strtok(NULL, ";"); if(temp_buffer) entry_service_desc = strdup(temp_buffer); else entry_service_desc = NULL; strncpy(image, FLAPPING_ICON, sizeof(image)); if(strstr(input, ";STARTED;")) strncpy(image_alt, "Service started flapping", sizeof(image_alt)); else if(strstr(input, ";STOPPED;")) strncpy(image_alt, "Service stopped flapping", sizeof(image_alt)); else if(strstr(input, ";DISABLED;")) strncpy(image_alt, "Service flap detection disabled", sizeof(image_alt)); } /* service downtime alerts */ else if(strstr(input, "SERVICE DOWNTIME ALERT:")) { if(display_downtime_alerts == FALSE) continue; history_type = SERVICE_DOWNTIME_HISTORY; /* get host and service names */ temp_buffer = my_strtok(input2, "]"); temp_buffer = my_strtok(NULL, ":"); temp_buffer = my_strtok(NULL, ";"); if(temp_buffer) entry_host_name = strdup(temp_buffer + 1); else entry_host_name = NULL; temp_buffer = my_strtok(NULL, ";"); if(temp_buffer) entry_service_desc = strdup(temp_buffer); else entry_service_desc = NULL; strncpy(image, SCHEDULED_DOWNTIME_ICON, sizeof(image)); if(strstr(input, ";STARTED;")) strncpy(image_alt, "Service entered a period of scheduled downtime", sizeof(image_alt)); else if(strstr(input, ";STOPPED;")) strncpy(image_alt, "Service exited from a period of scheduled downtime", sizeof(image_alt)); else if(strstr(input, ";CANCELLED;")) strncpy(image_alt, "Service scheduled downtime has been cancelled", sizeof(image_alt)); } /* host state alerts */ else if(strstr(input, "HOST ALERT:")) { history_type = HOST_HISTORY; /* get host name */ temp_buffer = my_strtok(input2, "]"); temp_buffer = my_strtok(NULL, ":"); temp_buffer = my_strtok(NULL, ";"); if(temp_buffer) entry_host_name = strdup(temp_buffer + 1); else entry_host_name = NULL; if(strstr(input, ";DOWN;")) { strncpy(image, HOST_DOWN_ICON, sizeof(image)); strncpy(image_alt, HOST_DOWN_ICON_ALT, sizeof(image_alt)); history_detail_type = HISTORY_HOST_DOWN; } else if(strstr(input, ";UNREACHABLE;")) { strncpy(image, HOST_UNREACHABLE_ICON, sizeof(image)); strncpy(image_alt, HOST_UNREACHABLE_ICON_ALT, sizeof(image_alt)); history_detail_type = HISTORY_HOST_UNREACHABLE; } else if(strstr(input, ";RECOVERY") || strstr(input, ";UP;")) { strncpy(image, HOST_UP_ICON, sizeof(image)); strncpy(image_alt, HOST_UP_ICON_ALT, sizeof(image_alt)); history_detail_type = HISTORY_HOST_RECOVERY; } } /* host flapping alerts */ else if(strstr(input, "HOST FLAPPING ALERT:")) { if(display_flapping_alerts == FALSE) continue; history_type = HOST_FLAPPING_HISTORY; /* get host name */ temp_buffer = my_strtok(input2, "]"); temp_buffer = my_strtok(NULL, ":"); temp_buffer = my_strtok(NULL, ";"); if(temp_buffer) entry_host_name = strdup(temp_buffer + 1); else entry_host_name = NULL; strncpy(image, FLAPPING_ICON, sizeof(image)); if(strstr(input, ";STARTED;")) strncpy(image_alt, "Host started flapping", sizeof(image_alt)); else if(strstr(input, ";STOPPED;")) strncpy(image_alt, "Host stopped flapping", sizeof(image_alt)); else if(strstr(input, ";DISABLED;")) strncpy(image_alt, "Host flap detection disabled", sizeof(image_alt)); } /* host downtime alerts */ else if(strstr(input, "HOST DOWNTIME ALERT:")) { if(display_downtime_alerts == FALSE) continue; history_type = HOST_DOWNTIME_HISTORY; /* get host name */ temp_buffer = my_strtok(input2, "]"); temp_buffer = my_strtok(NULL, ":"); temp_buffer = my_strtok(NULL, ";"); if(temp_buffer) entry_host_name = strdup(temp_buffer + 1); else entry_host_name = NULL; strncpy(image, SCHEDULED_DOWNTIME_ICON, sizeof(image)); if(strstr(input, ";STARTED;")) strncpy(image_alt, "Host entered a period of scheduled downtime", sizeof(image_alt)); else if(strstr(input, ";STOPPED;")) strncpy(image_alt, "Host exited from a period of scheduled downtime", sizeof(image_alt)); else if(strstr(input, ";CANCELLED;")) strncpy(image_alt, "Host scheduled downtime has been cancelled", sizeof(image_alt)); } else if(display_system_messages == FALSE) continue; /* program start */ else if(strstr(input, " starting...")) { strncpy(image, START_ICON, sizeof(image)); strncpy(image_alt, START_ICON_ALT, sizeof(image_alt)); system_message = TRUE; } /* normal program termination */ else if(strstr(input, " shutting down...")) { strncpy(image, STOP_ICON, sizeof(image)); strncpy(image_alt, STOP_ICON_ALT, sizeof(image_alt)); system_message = TRUE; } /* abnormal program termination */ else if(strstr(input, "Bailing out")) { strncpy(image, STOP_ICON, sizeof(image)); strncpy(image_alt, STOP_ICON_ALT, sizeof(image_alt)); system_message = TRUE; } /* program restart */ else if(strstr(input, " restarting...")) { strncpy(image, RESTART_ICON, sizeof(image)); strncpy(image_alt, RESTART_ICON_ALT, sizeof(image_alt)); system_message = TRUE; } image[sizeof(image) - 1] = '\x0'; image_alt[sizeof(image_alt) - 1] = '\x0'; /* get the timestamp */ temp_buffer = strtok(input, "]"); t = (temp_buffer == NULL) ? 0L : strtoul(temp_buffer + 1, NULL, 10); time_ptr = localtime(&t); strftime(current_message_date, sizeof(current_message_date), "%B %d, %Y %H:00\n", time_ptr); current_message_date[sizeof(current_message_date) - 1] = '\x0'; get_time_string(&t, date_time, sizeof(date_time), SHORT_DATE_TIME); strip(date_time); temp_buffer = strtok(NULL, "\n"); if(strcmp(image, "")) { display_line = FALSE; if(system_message == TRUE) display_line = TRUE; else if(display_type == DISPLAY_HOSTS) { if(history_type == HOST_HISTORY || history_type == SERVICE_HISTORY) { snprintf(match1, sizeof( match1), " HOST ALERT: %s;", host_name); snprintf(match2, sizeof( match2), " SERVICE ALERT: %s;", host_name); } else if(history_type == HOST_FLAPPING_HISTORY || history_type == SERVICE_FLAPPING_HISTORY) { snprintf(match1, sizeof( match1), " HOST FLAPPING ALERT: %s;", host_name); snprintf(match2, sizeof( match2), " SERVICE FLAPPING ALERT: %s;", host_name); } else if(history_type == HOST_DOWNTIME_HISTORY || history_type == SERVICE_DOWNTIME_HISTORY) { snprintf(match1, sizeof( match1), " HOST DOWNTIME ALERT: %s;", host_name); snprintf(match2, sizeof( match2), " SERVICE DOWNTIME ALERT: %s;", host_name); } if(show_all_hosts == TRUE) display_line = TRUE; else if(strstr(temp_buffer, match1)) display_line = TRUE; else if(strstr(temp_buffer, match2)) display_line = TRUE; if(display_line == TRUE) { if(history_options == HISTORY_ALL) display_line = TRUE; else if(history_options == HISTORY_HOST_ALL && (history_type == HOST_HISTORY || history_type == HOST_FLAPPING_HISTORY || history_type == HOST_DOWNTIME_HISTORY)) display_line = TRUE; else if(history_options == HISTORY_SERVICE_ALL && (history_type == SERVICE_HISTORY || history_type == SERVICE_FLAPPING_HISTORY || history_type == SERVICE_DOWNTIME_HISTORY)) display_line = TRUE; else if((history_type == HOST_HISTORY || history_type == SERVICE_HISTORY) && (history_detail_type & history_options)) display_line = TRUE; else display_line = FALSE; } /* check alert state types */ if(display_line == TRUE && (history_type == HOST_HISTORY || history_type == SERVICE_HISTORY)) { if(state_options == STATE_ALL) display_line = TRUE; else if((state_options & STATE_SOFT) && strstr(temp_buffer, ";SOFT;")) display_line = TRUE; else if((state_options & STATE_HARD) && strstr(temp_buffer, ";HARD;")) display_line = TRUE; else display_line = FALSE; } } else if(display_type == DISPLAY_SERVICES) { if(history_type == SERVICE_HISTORY) snprintf(match1, sizeof( match1), " SERVICE ALERT: %s;%s;", host_name, svc_description); else if(history_type == SERVICE_FLAPPING_HISTORY) snprintf(match1, sizeof( match1), " SERVICE FLAPPING ALERT: %s;%s;", host_name, svc_description); else if(history_type == SERVICE_DOWNTIME_HISTORY) snprintf(match1, sizeof( match1), " SERVICE DOWNTIME ALERT: %s;%s;", host_name, svc_description); if(strstr(temp_buffer, match1) && (history_type == SERVICE_HISTORY || history_type == SERVICE_FLAPPING_HISTORY || history_type == SERVICE_DOWNTIME_HISTORY)) display_line = TRUE; if(display_line == TRUE) { if(history_options == HISTORY_ALL || history_options == HISTORY_SERVICE_ALL) display_line = TRUE; else if(history_options & history_detail_type) display_line = TRUE; else display_line = FALSE; } /* check alert state type */ if(display_line == TRUE && history_type == SERVICE_HISTORY) { if(state_options == STATE_ALL) display_line = TRUE; else if((state_options & STATE_SOFT) && strstr(temp_buffer, ";SOFT;")) display_line = TRUE; else if((state_options & STATE_HARD) && strstr(temp_buffer, ";HARD;")) display_line = TRUE; else display_line = FALSE; } } /* make sure user is authorized to view this host or service information */ if(system_message == FALSE) { if(history_type == HOST_HISTORY || history_type == HOST_FLAPPING_HISTORY || history_type == HOST_DOWNTIME_HISTORY) { temp_host = find_host(entry_host_name); if(is_authorized_for_host(temp_host, ¤t_authdata) == FALSE) display_line = FALSE; } else { temp_service = find_service(entry_host_name, entry_service_desc); if(is_authorized_for_service(temp_service, ¤t_authdata) == FALSE) display_line = FALSE; } } /* display the entry if we should... */ if(display_line == TRUE) { if(strcmp(last_message_date, current_message_date) != 0 && display_timebreaks == TRUE) { printf("</DIV><BR CLEAR='all' />\n"); printf("<DIV CLASS='dateTimeBreak'>\n"); printf("<table border=0 width=95%%><tr>"); printf("<td width=40%%><hr width=100%%></td>"); printf("<td align=center CLASS='dateTimeBreak'>%s</td>", current_message_date); printf("<td width=40%%><hr width=100%%></td>"); printf("</tr></table>\n"); printf("</DIV>\n"); printf("<BR CLEAR='all' /><DIV CLASS='logEntries'>\n"); strncpy(last_message_date, current_message_date, sizeof(last_message_date)); last_message_date[sizeof(last_message_date) - 1] = '\x0'; } if(display_frills == TRUE) printf("<img align='left' src='%s%s' alt='%s' title='%s' />", url_images_path, image, image_alt, image_alt); printf("[%s] %s", date_time, html_encode(temp_buffer, FALSE)); if(enable_splunk_integration == TRUE) { printf(" "); display_splunk_generic_url(temp_buffer, 2); } printf("<br clear='all' />\n"); found_line = TRUE; } } /* free memory */ free(entry_host_name); entry_host_name = NULL; free(entry_service_desc); entry_service_desc = NULL; } printf("</DIV></P>\n"); if(found_line == FALSE) { printf("<HR>\n"); printf("<P><DIV CLASS='warningMessage'>No history information was found "); if(display_type == DISPLAY_HOSTS) printf("%s", (show_all_hosts == TRUE) ? "" : "for this host "); else printf("for this service "); printf("in %s log file</DIV></P>", (log_archive == 0) ? "the current" : "this archived"); } printf("<HR>\n"); my_free(input); my_free(input2); if(use_lifo == TRUE) free_lifo_memory(); else mmap_fclose(thefile); return; }
void xmlr_listdir(struct _info **dir, char *d, int *dt, int *ft, u_long lev) { char *path; long pathsize = 0; struct _info **sav = dir; bool nlf = FALSE; int mt, t; if (dir == NULL) return; dirs[lev] = 1; if (!*(dir+1)) dirs[lev] = 2; fprintf(outfile,"\n"); path = malloc(pathsize=4096); while(*dir) { if (!noindent) xml_indent(lev); if ((*dir)->lnk) mt = (*dir)->mode & S_IFMT; else mt = (*dir)->mode & S_IFMT; for(t=0;ifmt[t];t++) if (ifmt[t] == mt) break; fprintf(outfile,"<%s", ftype[t]); if (fflag) { if (sizeof(char) * (strlen(d)+strlen((*dir)->name)+2) > pathsize) path=xrealloc(path,pathsize=(sizeof(char) * (strlen(d)+strlen((*dir)->name)+1024))); if (!strcmp(d,"/")) sprintf(path,"%s%s",d,(*dir)->name); else sprintf(path,"%s/%s",d,(*dir)->name); } else { if (sizeof(char) * (strlen((*dir)->name)+1) > pathsize) path=xrealloc(path,pathsize=(sizeof(char) * (strlen((*dir)->name)+1024))); sprintf(path,"%s",(*dir)->name); } fprintf(outfile, " name=\""); html_encode(outfile,path); fputc('"',outfile); if ((*dir)->lnk) { fprintf(outfile, " target=\""); html_encode(outfile,(*dir)->lnk); fputc('"',outfile); } xml_fillinfo(*dir); if (mt != S_IFDIR && mt != S_IFLNK && (*dir)->err == NULL) fprintf(outfile,"/>"); else fputc('>',outfile); if ((*dir)->err) { fprintf(outfile,"<error>%s</error>", (*dir)->err); free((*dir)->err); (*dir)->err = NULL; } if ((*dir)->child) { if (fflag) { if (strlen(d)+strlen((*dir)->name)+2 > pathsize) path=xrealloc(path,pathsize=(strlen(d)+strlen((*dir)->name)+1024)); if (!strcmp(d,"/")) sprintf(path,"%s%s",d,(*dir)->name); else sprintf(path,"%s/%s",d,(*dir)->name); } xmlr_listdir((*dir)->child, fflag? path : NULL, dt, ft, lev+1); nlf = TRUE; *dt += 1; } else { if ((*dir)->isdir) *dt += 1; else *ft += 1; } if (*(dir+1) && !*(dir+2)) dirs[lev] = 2; if (nlf) { nlf = FALSE; if (!noindent) xml_indent(lev); } if (mt == S_IFDIR || mt == S_IFLNK || (*dir)->err != NULL) fprintf(outfile,"</%s>\n",ftype[t]); else putc('\n',outfile); dir++; } dirs[lev] = 0; free(path); free_dir(sav); }
int do_search( REQUEST *r, RESPONSE *resp ) { char *filtertype, **search_attrs, *search_filter; char *print_filter, *human_filter; int scope, count = 0, rc, i = 0, j, in_home; int option_param; struct timeval timeout; LDAPFiltInfo *fi; LDAPMessage *e, *res; static char *def_attrs[] = {"objectClass", "sn", "aliasedObjectName", "labeledURI", 0}; struct ldap_disptmpl *tmpl; char **oc; char *result_dn, *doc, *foc, *url_dn; char *base_dn, *base_ufn, *result_ufn, *alias_ufn, *sortstring, *cp, *bp; char **sn, href[10 * BUFSIZ], *temp, **val, *server = NULL; int counter = 0, base_len, isalias = 0; #ifdef WEB500GW_DEBUG Web500gw_debug(WEB500GW_DEBUG_TRACE, "do_search (%s, %s, (", r->r_dn, flag2string(r->r_flags), 0, 0); while (r->r_attrs && r->r_attrs[i]) { Web500gw_debug(WEB500GW_DEBUG_TRACE, "%s ", r->r_attrs[i], 0, 0, 0); i++; } Web500gw_debug(WEB500GW_DEBUG_TRACE, "), %s)\n", (r->r_filter && *r->r_filter) ? r->r_filter : "No filter", 0, 0, 0); #endif if (r->r_ld == (LDAP *)0 && (r->r_ld = web500gw_ldap_init(r, resp, NULL, NULL, 1)) == NULL) return NOTOK; base_dn = r->r_dn; if (strlen(base_dn) > 0) { base_ufn = friendly_dn(resp, base_dn); } else { base_ufn = NULL; } base_len = base_ufn ? strlen(base_ufn) : 0; if (r->r_filter == NULL) { return NOTOK; } print_filter = r->r_filter; #if defined(OWN_STR_TRANSLATION) search_filter = strdup(web500gw_isotot61(r->r_filter)); #else search_filter = r->r_filter; #endif if (*search_filter == '\0' || *(search_filter+1) != '=') { scope = LDAP_SCOPE_ONELEVEL; } else { if (*search_filter == 'S') { scope = LDAP_SCOPE_SUBTREE; } else { scope = LDAP_SCOPE_ONELEVEL; } search_filter += 2; print_filter += 2; } /* make a "human readable filter" - for now, no artistic things are done */ human_filter = print_filter; if ((cp = strchr(print_filter, '=')) != NULL) { if ((cp = strchr(cp+1, '=')) != NULL) { /* two '=' in it - must be a complex filter - isn't readable... */ human_filter = MSG_COMPLEX_FILTER; } } if (*search_filter) { /* not the default filter */ r->r_flags |= FLAG_FILTER; /* skip leading white spaces */ while (isspace(*search_filter)) ++search_filter; } if (r->r_attrs == NULL || *r->r_attrs == NULL) { /* No attributes requested - read default attrs for sorting */ search_attrs = def_attrs; } else { j = 0; while (def_attrs[j]) j++; search_attrs = (char **) calloc(r->r_attrnumb + j + 1, sizeof(char *)); i = j = 0; while (r->r_attrs && r->r_attrs[i]) { search_attrs[i] = r->r_attrs[i]; i++; } while (def_attrs[j]) search_attrs[i++] = def_attrs[j++]; search_attrs[i] = NULL; } if ((in_home = isinhome(base_dn))) /* ACCESS sizelimit if searching below HOME DN */ ldap_set_option(r->r_ld, LDAP_OPT_SIZELIMIT, &(r->r_access->a_sizelimit)); else ldap_set_option(r->r_ld, LDAP_OPT_SIZELIMIT, &sizelimit); /* A simple filter (not starting with '(') with a comma in it * -> UFN assumed */ /* if (ufnsearch && search_filter[0] != '(' && strchr(search_filter, ',') != NULL && strlen(search_filter) > 3) { if (strlen(base_dn) > 0) ldap_ufn_setprefix(r->r_ld, base_dn); timeout.tv_sec = timelimit; timeout.tv_usec = 0; ldap_ufn_timeout((void *) &timeout); option_param = LDAP_DEREF_FINDING; ldap_set_option(r->r_ld, LDAP_OPT_DEREF, (void *) &option_param); #ifdef WEB500GW_DEBUG Web500gw_debug(WEB500GW_DEBUG_TRACE, "ldap_ufn_search_s (%s)\n", search_filter, 0, 0, 0); #endif if ((rc = ldap_ufn_search_s(r->r_ld, search_filter, search_attrs, 0, &res)) != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED) { do_error(r, resp, rc, 0, get_ldap_result_code(r->r_ld), get_ldap_matched_str(r->r_ld)); return NOTOK; } count = ldap_count_entries(r->r_ld, res); #ifdef WEB500GW_DEBUG Web500gw_debug(WEB500GW_DEBUG_TRACE, "UFN ready: %d results!\n", count, 0, 0, 0); #endif // Reset DN, because this UFN search was probably not below DN base_dn = base_ufn = ""; in_home = 0; friendlyDesc = MSG_UFN; } else { */ /* let's do the search */ filtertype = (scope == LDAP_SCOPE_ONELEVEL ? "web500gw onelevel" : "web500gw subtree"); option_param = (scope == LDAP_SCOPE_ONELEVEL ? LDAP_DEREF_FINDING : LDAP_DEREF_ALWAYS); ldap_set_option(r->r_ld, LDAP_OPT_DEREF, (void *) &option_param); timeout.tv_sec = timelimit; timeout.tv_usec = 0; friendlyDesc = NULL; /* try all filters til we have success */ for (fi = ldap_getfirstfilter(r->r_access->a_filtd, filtertype, search_filter); fi != NULL; fi = ldap_getnextfilter(r->r_access->a_filtd)) { #ifdef WEB500GW_DEBUG Web500gw_debug(WEB500GW_DEBUG_FILTER, " search %s: %s -- %s\n", scope == LDAP_SCOPE_ONELEVEL ? "onelevel" : "subtree", fi->lfi_filter, fi->lfi_desc, 0); #endif if ((rc = ldap_search_st(r->r_ld, base_dn, scope, fi->lfi_filter, search_attrs, 0, &timeout, &res)) != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED && rc != LDAP_INSUFFICIENT_ACCESS && rc != LDAP_TIMELIMIT_EXCEEDED && rc != LDAP_TIMEOUT && rc != LDAP_PARTIAL_RESULTS) { do_error(r, resp, rc, 0, get_ldap_result_code(r->r_ld), get_ldap_matched_str(r->r_ld)); return NOTOK; } if ((count = ldap_count_entries(r->r_ld, res)) > 0) { /* found something */ friendlyDesc = friendly_label(resp, fi->lfi_desc); #ifdef WEB500GW_DEBUG Web500gw_debug(WEB500GW_DEBUG_FILTER, " searched ... and found %d results!\n", count, 0, 0, 0); #endif break; } #ifdef WEB500GW_DEBUG Web500gw_debug(WEB500GW_DEBUG_FILTER, " searched ... and found no results!\n", 0, 0, 0, 0); #endif } option_param = LDAP_DEREF_ALWAYS; ldap_set_option(r->r_ld, LDAP_OPT_DEREF, (void *) &option_param); /* }*/ if (count < 1) { /* nothing found :-( */ if (r->r_flags & FLAG_SEARCHACT) { /* in a search action we silently ignore this */ return OK; } resp->resp_status = NOT_FOUND; if (resp->resp_httpheader == 0) { if (r->r_httpversion == 1) { /* Expires now */ resp->resp_expires = 0; http_header(r, resp); } if (r->r_method == HEAD) { return OK; } } if (resp->resp_htmlheader == 0) { msg_fprintf(fp, in_home && *MSG_HTML_START_NO_SEARCH_RESULTS_HOME ? MSG_HTML_START_NO_SEARCH_RESULTS_HOME : MSG_HTML_START_NO_SEARCH_RESULTS, "ss", print_filter, base_ufn ? base_ufn : MSG_ROOT); resp->resp_htmlheader = 1; if (!(r->r_flags & FLAG_ENTRYONLY)) { if (in_home && MSG_HOMEBANNER) { msg_fprintf(fp, MSG_HOMEBANNER, "sss", r->r_query, other_lang_select, other_lang_string); } else { msg_fprintf(fp, MSG_BANNER, "sss", r->r_query, other_lang_select, other_lang_string); } } if (rc == LDAP_INSUFFICIENT_ACCESS) { fputs(MSG_NO_LIST_ACCESS, fp); } else if (rc == LDAP_TIMELIMIT_EXCEEDED || rc == LDAP_TIMEOUT) { fputs(MSG_TIMELIMIT, fp); } else { if (print_filter && *print_filter) { msg_fprintf(fp, MSG_NO_SEARCH_RESULTS, "ssss", (scope == LDAP_SCOPE_ONELEVEL ? MSG_ONELEVEL : MSG_SUBTREE), print_filter, human_filter, base_ufn ? base_ufn : MSG_ROOT); } else { fputs(MSG_NOTHING_FOUND_READORSEARCH, fp); } } if (!(r->r_flags & FLAG_ENTRYONLY)) { make_upsearch(r->r_ld, r, resp, 1); if (in_home && MSG_HOMETRAILER) { fputs(MSG_HOMETRAILER, fp); } else { fputs(MSG_TRAILER, fp); } } fputs(MSG_HTML_END, fp); fputs("\n", fp); } else { /* resp->resp_htmlheader != 0 -> within a document (searchaction) */ if (rc == LDAP_INSUFFICIENT_ACCESS) { fputs(MSG_NO_LIST_ACCESS, fp); } else if (rc == LDAP_TIMELIMIT_EXCEEDED || rc == LDAP_TIMEOUT) { fputs(MSG_TIMELIMIT, fp); } else { fputs(MSG_NOTHING_FOUND_READORSEARCH, fp); fputs(MSG_HTML_END, fp); fputs("\n", fp); } } return OK; } if (showonematch == 1 && count == 1 && r->r_attrnumb == 0 && !(r->r_flags & FLAG_SEARCHACT)) { /* header != 0 ? */ /* only one result and not searching for special attrs (searchaction) */ e = ldap_first_entry(r->r_ld, res); if (e != NULL) { char **oc, **aliasdn; oc = ldap_get_values(r->r_ld, e, objectclass_attr); aliasdn = ldap_get_values(r->r_ld, e, "aliasedObjectName"); if (aliasdn && *aliasdn) result_dn = *aliasdn; else result_dn = ldap_get_dn(r->r_ld, e); if (result_dn) { result_ufn = friendly_dn(resp, result_dn); if (gwswitch) { server = gw_switch(r->r_ld, resp, e); } r->r_flags &= ~FLAG_FILTER; /* it's not a search anymore */ url_dn = dn2url(r, result_dn, server ? r->r_flags & ~FLAG_LANGUAGE : r->r_flags, 0, NULL, server); /* Redirect */ #ifdef WEB500GW_DEBUG Web500gw_debug(WEB500GW_DEBUG_TRACE, "do_search: found one entry -> REDIRECT: %s\n", url_dn, 0, 0, 0); #endif resp->resp_status = REDIRECT; if (resp->resp_httpheader == 0 && r->r_httpversion == 1) { resp->resp_location = malloc(strlen(url_dn) + 11); strcpy(resp->resp_location, url_dn); http_header(r, resp); /* free(resp->resp_location); */ } if (r->r_method == HEAD) { return OK; } msg_fprintf(fp, in_home && *MSG_HTML_START_SEARCH_RESULTS_HOME ? MSG_HTML_START_SEARCH_RESULTS_HOME : MSG_HTML_START_SEARCH_RESULTS, "ss", print_filter, result_ufn); msg_fprintf(fp, MSG_REDIRECT, "ss", url_dn, result_ufn); fputs(MSG_HTML_END, fp); fputs("\n", fp); return OK; } } } /* Found something */ /* Prepare HTTP + HTML header */ if (resp->resp_httpheader == 0 && r->r_httpversion == 1) { http_header(r, resp); if (r->r_method == HEAD) return OK; } if (resp->resp_htmlheader == 0) { msg_fprintf(fp, in_home && *MSG_HTML_START_SEARCH_RESULTS_HOME ? MSG_HTML_START_SEARCH_RESULTS_HOME : MSG_HTML_START_SEARCH_RESULTS, "ss", print_filter, base_ufn ? base_ufn : MSG_ROOT); if (!(r->r_flags & FLAG_ENTRYONLY)) { if (in_home && MSG_HOMEBANNER) { msg_fprintf(fp, MSG_HOMEBANNER, "sss", r->r_query, other_lang_select, other_lang_string); } else { msg_fprintf(fp, MSG_BANNER, "sss", r->r_query, other_lang_select, other_lang_string); } } resp->resp_htmlheader = 1; } if ((!(r->r_flags & FLAG_ENTRYONLY)) && (r->r_browser->b_upsearch & UPSEARCH_ON_TOP)) { make_upsearch(r->r_ld, r, resp, 1); } if (r->r_flags & FLAG_FILTER && (!(r->r_flags & FLAG_ENTRYONLY))) { /* Not the default filter and not entryonly */ msg_fprintf(fp, MSG_SEARCH_RESULTS, "ssssisss", (scope == LDAP_SCOPE_ONELEVEL ? MSG_ONELEVEL : MSG_SUBTREE), print_filter, human_filter, base_ufn && *base_ufn ? base_ufn : MSG_ROOT, count, count == 1 ? MSG_ENTRY : MSG_ENTRIES, friendlyDesc, rc == LDAP_PARTIAL_RESULTS ? resp->resp_language->l_conf->c_errmsg[LDAP_PARTIAL_RESULTS] : ""); } if (ldap_result2error(r->r_ld, res, 0) == LDAP_SIZELIMIT_EXCEEDED) { fprintf(fp, r->r_flags & FLAG_FILTER && r->r_access->a_sizelimit == 0 ? MSG_SIZELIMIT_1 : MSG_SIZELIMIT_0, count); } /* Now prepare the result for sorting */ #ifdef WEB500GW_DEBUG Web500gw_debug(WEB500GW_DEBUG_TRACE, "do_search: preparing for sort\n", 0, 0, 0, 0); #endif /* for all the results ... */ for (e = ldap_first_entry(r->r_ld, res); e != NULL && counter < MAX_LISTSIZE; e = ldap_next_entry(r->r_ld, e)) { result_dn = ldap_get_dn(r->r_ld, e); result_ufn = friendly_dn(resp, result_dn); if ((val = ldap_get_values(r->r_ld, e, "aliasedObjectName"))) { /* entry is alias */ free(result_dn); result_dn = strdup(*val); alias_ufn = friendly_dn(resp, result_dn); ldap_value_free(val); isalias = 1; } else { isalias = 0; } if (isalias && r->r_flags & FLAG_DEREFALIAS) { /* if alias, look for objectClass of real entry */ oc = deref(r->r_ld, result_dn); } else { oc = ldap_get_values(r->r_ld, e, objectclass_attr); } /* find template to see the plural name of the objectclass */ tmpl = ldap_oc2template(oc, r->r_access->a_tmpllist); doc = pick_oc(oc); if (tmpl && tmpl->dt_pluralname) { foc = strdup(friendly_label(resp, tmpl->dt_pluralname)); } else { foc = strdup(friendly_label(resp, doc)); if (strcmp(foc, doc) == 0) { /* "no friendly objectclass" found -> last in the list (a hack) */ foc = (char *)malloc(strlen(doc)+9); sprintf(foc, " %s", doc); } } if (strncasecmp(result_ufn, "{ASN}", 5) == 0) { /* ASNs are not something we want to display! */ free(result_dn); ldap_value_free(oc); continue; } if (base_len > 0) { /* strip search base from result */ result_ufn = strip_ufn_dn(result_ufn, base_ufn); } #ifdef nodef cp = result_ufn + strlen(result_ufn) - 1; bp = base_ufn + base_len - 1; while (bp >= base_ufn && *cp-- && *bp-- && *cp == *bp); if ((bp+1) == base_ufn && cp != NULL) { /* Complete match on base */ /* strip trailing whitespaces and ',' */ while (*cp && isspace(*cp)) cp--; if (cp != NULL && *cp == ',') *cp = '\0'; } } #endif sortstring = strdup(result_ufn); if (strcasecmp(doc, "country") == 0) { /* a country */ sn = NULL; } else { /* not a country */ /* DNs may have components in '"', ignored when sorting */ if (*sortstring == '"') { sortstring++; cp = strchr(sortstring+1, '"'); } else { cp = sortstring; } if ((cp = strchr(cp,','))) *cp = '\0'; if (isalias) { /* aliases don't have surnames */ if (isoc (oc, "person")) { } } sn = ldap_get_values(r->r_ld, e, "sn"); /* Delete spaces ??? if (sn) { cp = *sn; while ((cp = strchr(cp,' '))) { cp ++; spaces ++; } } while (spaces > 0) { if ((cp = strrchr(sortstring,' '))) { *cp = '\0'; spaces --; } else break; } */ if ((cp = strchr(sortstring,'+'))) { cp --; *cp = '\0'; } } if (gwswitch) { server = gw_switch(r->r_ld, resp, e); } /* build the link (HREF) */ if (isalias) { if (isnonleaf(r->r_ld, oc, result_dn)) { if (r->r_flags & FLAG_NOHREFDN) msg_snprintf(href, sizeof(href), MSG_DN_ALIAS_TO_NONLEAF, "sss", result_ufn, alias_ufn, html_encode(result_dn)); else msg_snprintf(href, sizeof(href), MSG_HREF_ALIAS_TO_NONLEAF, "ssssss", dn2url(r, result_dn, server ? 0 : FLAG_LANGUAGE, 0, NULL, server), result_ufn, alias_ufn, html_encode(result_dn), string_encode(result_ufn),string_encode(result_dn)); } else { if (r->r_flags & FLAG_NOHREFDN) msg_snprintf(href, sizeof(href), MSG_DN_ALIAS_TO_LEAF, "sss", result_ufn, alias_ufn, html_encode(result_dn)); else msg_snprintf(href, sizeof(href), MSG_HREF_ALIAS_TO_LEAF, "ssssss", dn2url(r, result_dn, server ? 0 : FLAG_LANGUAGE, 0, NULL, server), result_ufn, alias_ufn, html_encode(result_dn), string_encode(result_ufn),string_encode(result_dn)); } } else if (isnonleaf(r->r_ld, oc, result_dn)) { if (r->r_flags & FLAG_NOHREFDN) msg_snprintf(href, sizeof(href), MSG_DN_NON_LEAF, "ss", result_ufn, html_encode(result_dn)); else msg_snprintf(href, sizeof(href), MSG_HREF_NON_LEAF, "sssss", dn2url(r, result_dn, server ? 0 : FLAG_LANGUAGE, 0, NULL, server), result_ufn, html_encode(result_dn), string_encode(result_ufn), string_encode(result_dn)); } else { if (r->r_flags & FLAG_NOHREFDN) msg_snprintf(href, sizeof(href), MSG_DN_LEAF, "ss", result_ufn, html_encode(result_dn)); else msg_snprintf(href, sizeof(href), MSG_HREF_LEAF, "sssss", dn2url(r, result_dn, server ? 0 : FLAG_LANGUAGE, 0, NULL, server), result_ufn, html_encode(result_dn), string_encode(result_ufn), string_encode(result_dn)); } /* build the sortstring: foc[sn]sortstring */ if (sn) { temp = (char *) malloc(strlen(foc)+strlen(*sn)+strlen(sortstring)+1); } else { temp = (char *) malloc(strlen(foc)+strlen(sortstring)+1); } strcpy(temp, foc); if (sn) { strcat(temp, *sn); } strcat(temp, sortstring); if (!dnlist[counter]) { dnlist[counter] = (struct dncompare *) malloc(sizeof(struct dncompare)); } dnlist[counter]->sortstring = temp; dnlist[counter]->href = strdup(href); dnlist[counter]->friendly_oc = foc; dnlist[counter]->oc = doc; dnlist[counter]->tmpl = tmpl; dnlist[counter]->entry = e; dnlist[counter]->dn = strdup(result_dn); counter++; ldap_value_free(oc); free(result_dn); if (sn) ldap_value_free(sn); }
/********************************************************************* * * Function : trust_url FIXME: I should be called distrust_url * * Description : Calls is_untrusted_url to determine if the URL is trusted * and if not, returns a HTTP 304 response with a reject message. * * Parameters : * 1 : csp = Current client state (buffers, headers, etc...) * * Returns : NULL => trusted, else http_response. * *********************************************************************/ struct http_response *trust_url(struct client_state *csp) { struct http_response *rsp; struct map * exports; char buf[BUFFER_SIZE]; char *p; struct url_spec **tl; struct url_spec *t; jb_err err; /* * Don't bother to work on trusted URLs */ if (!is_untrusted_url(csp)) { return NULL; } /* * Else, prepare a response: */ if (NULL == (rsp = alloc_http_response())) { return cgi_error_memory(); } exports = default_exports(csp, NULL); if (exports == NULL) { free_http_response(rsp); return cgi_error_memory(); } /* * Export the protocol, host, port, and referrer information */ err = map(exports, "hostport", 1, csp->http->hostport, 1); if (!err) err = map(exports, "protocol", 1, csp->http->ssl ? "https://" : "http://", 1); if (!err) err = map(exports, "path", 1, csp->http->path, 1); if (NULL != (p = get_header_value(csp->headers, "Referer:"))) { if (!err) err = map(exports, "referrer", 1, html_encode(p), 0); } else { if (!err) err = map(exports, "referrer", 1, "unknown", 1); } if (err) { free_map(exports); free_http_response(rsp); return cgi_error_memory(); } /* * Export the trust list */ p = strdup(""); for (tl = csp->config->trust_list; (t = *tl) != NULL ; tl++) { sprintf(buf, "<li>%s</li>\n", t->spec); string_append(&p, buf); } err = map(exports, "trusted-referrers", 1, p, 0); if (err) { free_map(exports); free_http_response(rsp); return cgi_error_memory(); } /* * Export the trust info, if available */ if (csp->config->trust_info->first) { struct list_entry *l; p = strdup(""); for (l = csp->config->trust_info->first; l ; l = l->next) { sprintf(buf, "<li> <a href=\"%s\">%s</a><br>\n",l->str, l->str); string_append(&p, buf); } err = map(exports, "trust-info", 1, p, 0); } else { err = map_block_killer(exports, "have-trust-info"); } if (err) { free_map(exports); free_http_response(rsp); return cgi_error_memory(); } /* * Export the force prefix or the force conditional block killer */ #ifdef FEATURE_FORCE_LOAD err = map(exports, "force-prefix", 1, FORCE_PREFIX, 1); #else /* ifndef FEATURE_FORCE_LOAD */ err = map_block_killer(exports, "force-support"); #endif /* ndef FEATURE_FORCE_LOAD */ if (err) { free_map(exports); free_http_response(rsp); return cgi_error_memory(); } /* * Build the response */ err = template_fill_for_cgi(csp, "untrusted", exports, rsp); if (err) { free_http_response(rsp); return cgi_error_memory(); } return finish_http_response(rsp); }
/********************************************************************* * * Function : block_url * * Description : Called from `chat'. Check to see if we need to block this. * * Parameters : * 1 : csp = Current client state (buffers, headers, etc...) * * Returns : NULL => unblocked, else HTTP block response * *********************************************************************/ struct http_response *block_url(struct client_state *csp) { #ifdef FEATURE_IMAGE_BLOCKING char *p; #endif /* def FEATURE_IMAGE_BLOCKING */ struct http_response *rsp; /* * If it's not blocked, don't block it ;-) */ if ((csp->action->flags & ACTION_BLOCK) == 0) { return NULL; } /* * Else, prepare a response */ if (NULL == (rsp = alloc_http_response())) { return cgi_error_memory(); } /* * If it's an image-url, send back an image or redirect * as specified by the relevant +image action */ #ifdef FEATURE_IMAGE_BLOCKING if (((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0) && is_imageurl(csp)) { /* determine HOW images should be blocked */ p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER]; #if 1 /* Two alternative strategies, use this one for now: */ /* and handle accordingly: */ if ((p == NULL) || (0 == strcmpic(p, "pattern"))) { rsp->body = bindup(image_pattern_data, image_pattern_length); if (rsp->body == NULL) { free_http_response(rsp); return cgi_error_memory(); } rsp->content_length = image_pattern_length; if (enlist_unique_header(rsp->headers, "Content-Type", BUILTIN_IMAGE_MIMETYPE)) { free_http_response(rsp); return cgi_error_memory(); } } else if (0 == strcmpic(p, "blank")) { rsp->body = bindup(image_blank_data, image_blank_length); if (rsp->body == NULL) { free_http_response(rsp); return cgi_error_memory(); } rsp->content_length = image_blank_length; if (enlist_unique_header(rsp->headers, "Content-Type", BUILTIN_IMAGE_MIMETYPE)) { free_http_response(rsp); return cgi_error_memory(); } } else { rsp->status = strdup("302 Local Redirect from Privoxy"); if (rsp->status == NULL) { free_http_response(rsp); return cgi_error_memory(); } if (enlist_unique_header(rsp->headers, "Location", p)) { free_http_response(rsp); return cgi_error_memory(); } } #else /* Following code is disabled for now */ /* and handle accordingly: */ if ((p == NULL) || (0 == strcmpic(p, "pattern"))) { p = CGI_PREFIX "send-banner?type=pattern"; } else if (0 == strcmpic(p, "blank")) { p = CGI_PREFIX "send-banner?type=blank"; } rsp->status = strdup("302 Local Redirect from Privoxy"); if (rsp->status == NULL) { free_http_response(rsp); return cgi_error_memory(); } if (enlist_unique_header(rsp->headers, "Location", p)) { free_http_response(rsp); return cgi_error_memory(); } #endif /* Preceeding code is disabled for now */ } else #endif /* def FEATURE_IMAGE_BLOCKING */ /* * Else, generate an HTML "blocked" message: */ { jb_err err; struct map * exports; /* * Workaround for stupid Netscape bug which prevents * pages from being displayed if loading a referenced * JavaScript or style sheet fails. So make it appear * as if it succeeded. */ if ( NULL != (p = get_header_value(csp->headers, "User-Agent:")) && !strncmpic(p, "mozilla", 7) /* Catch Netscape but */ && !strstr(p, "Gecko") /* save Mozilla, */ && !strstr(p, "compatible") /* MSIE */ && !strstr(p, "Opera")) /* and Opera. */ { rsp->status = strdup("200 Request for blocked URL"); } else { rsp->status = strdup("404 Request for blocked URL"); } if (rsp->status == NULL) { free_http_response(rsp); return cgi_error_memory(); } exports = default_exports(csp, NULL); if (exports == NULL) { free_http_response(rsp); return cgi_error_memory(); } #ifdef FEATURE_FORCE_LOAD err = map(exports, "force-prefix", 1, FORCE_PREFIX, 1); if (csp->http->ssl != 0) #endif /* ndef FEATURE_FORCE_LOAD */ { err = map_block_killer(exports, "force-support"); } if (!err) err = map(exports, "protocol", 1, csp->http->ssl ? "https://" : "http://", 1); if (!err) err = map(exports, "hostport", 1, html_encode(csp->http->hostport), 0); if (!err) err = map(exports, "path", 1, html_encode(csp->http->path), 0); if (!err) err = map(exports, "path-ue", 1, url_encode(csp->http->path), 0); if (err) { free_map(exports); free_http_response(rsp); return cgi_error_memory(); } err = template_fill_for_cgi(csp, "blocked", exports, rsp); if (err) { free_http_response(rsp); return cgi_error_memory(); } } return finish_http_response(rsp); }
/** * 将txt格式转换为html格式 */ static QString convert_txt_to_html(const QString& s) { const char *head = // 头部 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">" "<html>" "<head><meta name=\"qrichtext\" content=\"1\" />" "<style type=\"text/css\">p, li { white-space: pre-wrap; }</style>" "</head>" "<body style=\" font-family:'SimSun'; font-size:14pt; font-weight:400; font-style:normal;\">"; const char *para_head = // 段落开始 "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">"; const char *para_end = // 段落结束 "</p>"; const char *empty_para = // 空白段落(空白行) "<p style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">" "</p>"; const char *tail = // 尾部 "</body>" "</html>"; QString ret(head); const int len = s.length(); int para_start = 0; for (int i = 0; i <= len; ++i) { // 判断段尾 bool ln = false; char c; if (i < len) { #if QT_VERSION < 0x050000 c = s.at(i).toAscii(); #else c = s.at(i).toLatin1(); #endif if ('\r' == c || '\n' == c) ln = true; } else { ln = true; } // 新增一段 if (ln) { if (i == para_start) { // 空段落 ret += empty_para; } else { ret += para_head; ret += html_encode(s.mid(para_start, i - para_start)); ret += para_end; } // 匹配 "\r\n", "\n\r" if (i + 1 < len) { #if QT_VERSION < 0x050000 const char cc = s.at(i + 1).toAscii(); #else const char cc = s.at(i + 1).toLatin1(); #endif if (cc != c /* 要避免吃掉连续的回车 */ && ('\r' == cc || '\n' == cc)) ++i; } // 移动段首标记 para_start = i + 1; } } ret += tail; return ret; }
/********************************************************************* * * Function : load_one_re_filterfile * * Description : Load a re_filterfile. * Generate a chained list of re_filterfile_spec's from * the "FILTER: " blocks, compiling all their substitutions * into chained lists of pcrs_job structs. * * Parameters : * 1 : csp = Current client state (buffers, headers, etc...) * * Returns : 0 => Ok, everything else is an error. * *********************************************************************/ int load_one_re_filterfile(struct client_state *csp, int fileid) { FILE *fp; struct re_filterfile_spec *new_bl, *bl = NULL; struct file_list *fs; char buf[BUFFER_SIZE]; int error; unsigned long linenum = 0; pcrs_job *dummy, *lastjob = NULL; /* * No need to reload if unchanged */ if (!check_file_changed(current_re_filterfile[fileid], csp->config->re_filterfile[fileid], &fs)) { if (csp) { csp->rlist[fileid] = current_re_filterfile[fileid]; } return(0); } if (!fs) { goto load_re_filterfile_error; } /* * Open the file or fail */ if ((fp = fopen(csp->config->re_filterfile[fileid], "r")) == NULL) { goto load_re_filterfile_error; } /* * Read line by line */ while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL) { int new_filter = NO_NEW_FILTER; if (strncmp(buf, "FILTER:", 7) == 0) { new_filter = FT_CONTENT_FILTER; } else if (strncmp(buf, "SERVER-HEADER-FILTER:", 21) == 0) { new_filter = FT_SERVER_HEADER_FILTER; } else if (strncmp(buf, "CLIENT-HEADER-FILTER:", 21) == 0) { new_filter = FT_CLIENT_HEADER_FILTER; } else if (strncmp(buf, "CLIENT-HEADER-TAGGER:", 21) == 0) { new_filter = FT_CLIENT_HEADER_TAGGER; } else if (strncmp(buf, "SERVER-HEADER-TAGGER:", 21) == 0) { new_filter = FT_SERVER_HEADER_TAGGER; } /* * If this is the head of a new filter block, make it a * re_filterfile spec of its own and chain it to the list: */ if (new_filter != NO_NEW_FILTER) { new_bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl)); if (new_bl == NULL) { goto load_re_filterfile_error; } if (new_filter == FT_CONTENT_FILTER) { new_bl->name = chomp(buf + 7); } else { new_bl->name = chomp(buf + 21); } new_bl->type = new_filter; /* * If a filter description is available, * encode it to HTML and save it. */ if (NULL != (new_bl->description = strpbrk(new_bl->name, " \t"))) { *new_bl->description++ = '\0'; new_bl->description = html_encode(chomp(new_bl->description)); if (NULL == new_bl->description) { new_bl->description = strdup("Out of memory while encoding this filter's description to HTML"); } } else { new_bl->description = strdup("No description available for this filter"); } new_bl->name = strdup(chomp(new_bl->name)); /* * If this is the first filter block, chain it * to the file_list rather than its (nonexistant) * predecessor */ if (fs->f == NULL) { fs->f = new_bl; } else { assert(NULL != bl); bl->next = new_bl; } bl = new_bl; log_error(LOG_LEVEL_RE_FILTER, "Reading in filter \"%s\" (\"%s\")", bl->name, bl->description); continue; } /* * Else, save the expression, make it a pcrs_job * and chain it into the current filter's joblist */ if (bl != NULL) { error = enlist(bl->patterns, buf); if (JB_ERR_MEMORY == error) { log_error(LOG_LEVEL_FATAL, "Out of memory while enlisting re_filter job \'%s\' for filter %s.", buf, bl->name); } assert(JB_ERR_OK == error); if (pcrs_job_is_dynamic(buf)) { /* * Dynamic pattern that might contain variables * and has to be recompiled for every request */ if (bl->joblist != NULL) { pcrs_free_joblist(bl->joblist); bl->joblist = NULL; } bl->dynamic = 1; log_error(LOG_LEVEL_RE_FILTER, "Adding dynamic re_filter job \'%s\' to filter %s succeeded.", buf, bl->name); continue; } else if (bl->dynamic) { /* * A previous job was dynamic and as we * recompile the whole filter anyway, it * makes no sense to compile this job now. */ log_error(LOG_LEVEL_RE_FILTER, "Adding static re_filter job \'%s\' to dynamic filter %s succeeded.", buf, bl->name); continue; } if ((dummy = pcrs_compile_command(buf, &error)) == NULL) { log_error(LOG_LEVEL_ERROR, "Adding re_filter job \'%s\' to filter %s failed with error %d.", buf, bl->name, error); continue; } else { if (bl->joblist == NULL) { bl->joblist = dummy; } else if (NULL != lastjob) { lastjob->next = dummy; } lastjob = dummy; log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job \'%s\' to filter %s succeeded.", buf, bl->name); } } else { log_error(LOG_LEVEL_ERROR, "Ignoring job %s outside filter block in %s, line %d", buf, csp->config->re_filterfile[fileid], linenum); } } fclose(fp); /* * Schedule the now-obsolete old data for unloading */ if ( NULL != current_re_filterfile[fileid] ) { current_re_filterfile[fileid]->unloader = unload_re_filterfile; } /* * Chain this file into the global list of loaded files */ fs->next = files->next; files->next = fs; current_re_filterfile[fileid] = fs; if (csp) { csp->rlist[fileid] = fs; } return( 0 ); load_re_filterfile_error: log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E", csp->config->re_filterfile[fileid]); return(-1); }
void show_history(void) { char image[MAX_INPUT_BUFFER]; char image_alt[MAX_INPUT_BUFFER]; char match1[MAX_INPUT_BUFFER]; char match2[MAX_INPUT_BUFFER]; char date_time[MAX_DATETIME_LENGTH]; char last_message_date[MAX_INPUT_BUFFER] = ""; char current_message_date[MAX_INPUT_BUFFER] = ""; char *temp_buffer = NULL; char *entry_host_name = NULL; char *entry_service_desc = NULL; char *error_text = NULL; int system_message = FALSE; int display_line = FALSE; int history_type = SERVICE_HISTORY; int history_detail_type = HISTORY_SERVICE_CRITICAL; int status = READLOG_OK; int displayed_entries = 0; int total_entries = 0; host *temp_host = NULL; service *temp_service = NULL; hostgroup *temp_hostgroup = NULL; servicegroup *temp_servicegroup = NULL; logentry *temp_entry = NULL; struct tm *time_ptr = NULL; logentry *entry_list = NULL; logfilter *filter_list = NULL; if (display_type == DISPLAY_HOSTGROUPS) { temp_hostgroup = find_hostgroup(hostgroup_name); if (temp_hostgroup == NULL) { print_generic_error_message("There are no host groups with this name defined.", NULL, 0); return; } /* make sure the user is authorized to view this hostgroup */ if (show_partial_hostgroups == FALSE && is_authorized_for_hostgroup(temp_hostgroup, ¤t_authdata) == FALSE) { print_generic_error_message("It appears as though you do not have permission to view information for the host group you requested...", "If you believe this is an error, check the HTTP server authentication requirements for accessing this CGI and check the authorization options in your CGI configuration file.", 0); return; } } if (display_type == DISPLAY_SERVICEGROUPS) { temp_servicegroup = find_servicegroup(servicegroup_name); if (temp_servicegroup == NULL) { print_generic_error_message("There are no service groups with this name defined.", NULL, 0); return; } /* make sure the user is authorized to view this servicegroup */ if (is_authorized_for_servicegroup(temp_servicegroup, ¤t_authdata) == FALSE) { print_generic_error_message("It appears as though you do not have permission to view information for the service group you requested...", "If you believe this is an error, check the HTTP server authentication requirements for accessing this CGI and check the authorization options in your CGI configuration file.", 0); return; } } add_log_filter(&filter_list, LOGENTRY_SERVICE_CRITICAL, LOGFILTER_INCLUDE); add_log_filter(&filter_list, LOGENTRY_SERVICE_WARNING, LOGFILTER_INCLUDE); add_log_filter(&filter_list, LOGENTRY_SERVICE_UNKNOWN, LOGFILTER_INCLUDE); add_log_filter(&filter_list, LOGENTRY_SERVICE_RECOVERY, LOGFILTER_INCLUDE); add_log_filter(&filter_list, LOGENTRY_SERVICE_OK, LOGFILTER_INCLUDE); add_log_filter(&filter_list, LOGENTRY_SERVICE_FLAPPING_STARTED, LOGFILTER_INCLUDE); add_log_filter(&filter_list, LOGENTRY_SERVICE_FLAPPING_STOPPED, LOGFILTER_INCLUDE); add_log_filter(&filter_list, LOGENTRY_SERVICE_FLAPPING_DISABLED, LOGFILTER_INCLUDE); add_log_filter(&filter_list, LOGENTRY_SERVICE_DOWNTIME_STARTED, LOGFILTER_INCLUDE); add_log_filter(&filter_list, LOGENTRY_SERVICE_DOWNTIME_STOPPED, LOGFILTER_INCLUDE); add_log_filter(&filter_list, LOGENTRY_SERVICE_DOWNTIME_CANCELLED, LOGFILTER_INCLUDE); if (display_type == DISPLAY_HOSTS || display_type == DISPLAY_HOSTGROUPS) { add_log_filter(&filter_list, LOGENTRY_HOST_DOWN, LOGFILTER_INCLUDE); add_log_filter(&filter_list, LOGENTRY_HOST_UNREACHABLE, LOGFILTER_INCLUDE); add_log_filter(&filter_list, LOGENTRY_HOST_RECOVERY, LOGFILTER_INCLUDE); add_log_filter(&filter_list, LOGENTRY_HOST_UP, LOGFILTER_INCLUDE); add_log_filter(&filter_list, LOGENTRY_HOST_FLAPPING_STARTED, LOGFILTER_INCLUDE); add_log_filter(&filter_list, LOGENTRY_HOST_FLAPPING_STOPPED, LOGFILTER_INCLUDE); add_log_filter(&filter_list, LOGENTRY_HOST_FLAPPING_DISABLED, LOGFILTER_INCLUDE); add_log_filter(&filter_list, LOGENTRY_HOST_FLAPPING_DISABLED, LOGFILTER_INCLUDE); add_log_filter(&filter_list, LOGENTRY_HOST_FLAPPING_STOPPED, LOGFILTER_INCLUDE); add_log_filter(&filter_list, LOGENTRY_HOST_FLAPPING_DISABLED, LOGFILTER_INCLUDE); } /* system log entries */ add_log_filter(&filter_list, LOGENTRY_STARTUP, LOGFILTER_INCLUDE); add_log_filter(&filter_list, LOGENTRY_SHUTDOWN, LOGFILTER_INCLUDE); add_log_filter(&filter_list, LOGENTRY_BAILOUT, LOGFILTER_INCLUDE); add_log_filter(&filter_list, LOGENTRY_RESTART, LOGFILTER_INCLUDE); /* scan the log file for archived state data */ status = get_log_entries(&entry_list, &filter_list, &error_text, NULL, reverse, ts_start, ts_end); /* dealing with errors */ if (status == READLOG_ERROR_WARNING) { if (error_text != NULL) { print_generic_error_message(error_text, NULL, 0); my_free(error_text); } else print_generic_error_message("Unkown error!", NULL, 0); } if (status == READLOG_ERROR_MEMORY) print_generic_error_message("Out of memory...", "showing all I could get!", 0); if (status == READLOG_ERROR_FATAL) { if (error_text != NULL) { print_generic_error_message(error_text, NULL, 0); my_free(error_text); } return; /* now we start displaying the log entries */ } else { printf("<table width='100%%' cellspacing=0 cellpadding=0><tr><td width='33%%'></td><td width='33%%' nowrap>"); printf("<div class='page_selector' id='hist_page_selector'>\n"); printf("<div id='page_navigation_copy'></div>"); page_limit_selector(result_start); printf("</div>\n"); printf("</td><td width='33%%' align='right' style='padding-right:2px'>\n"); print_export_link(HTML_CONTENT, HISTORY_CGI, NULL); printf("</td></tr></table>"); printf("<DIV CLASS='logEntries'>\n"); for (temp_entry = entry_list; temp_entry != NULL; temp_entry = temp_entry->next) { strcpy(image, ""); strcpy(image_alt, ""); system_message = FALSE; switch (temp_entry->type) { /* service state alerts */ case LOGENTRY_SERVICE_CRITICAL: case LOGENTRY_SERVICE_WARNING: case LOGENTRY_SERVICE_UNKNOWN: case LOGENTRY_SERVICE_RECOVERY: case LOGENTRY_SERVICE_OK: history_type = SERVICE_HISTORY; /* get host and service names */ temp_buffer = my_strtok(temp_entry->entry_text, ":"); temp_buffer = my_strtok(NULL, ";"); if (temp_buffer) entry_host_name = strdup(temp_buffer + 1); else entry_host_name = NULL; temp_buffer = my_strtok(NULL, ";"); if (temp_buffer) entry_service_desc = strdup(temp_buffer); else entry_service_desc = NULL; if (temp_entry->type == LOGENTRY_SERVICE_CRITICAL) { strcpy(image, CRITICAL_ICON); strcpy(image_alt, CRITICAL_ICON_ALT); history_detail_type = HISTORY_SERVICE_CRITICAL; } else if (temp_entry->type == LOGENTRY_SERVICE_WARNING) { strcpy(image, WARNING_ICON); strcpy(image_alt, WARNING_ICON_ALT); history_detail_type = HISTORY_SERVICE_WARNING; } else if (temp_entry->type == LOGENTRY_SERVICE_UNKNOWN) { strcpy(image, UNKNOWN_ICON); strcpy(image_alt, UNKNOWN_ICON_ALT); history_detail_type = HISTORY_SERVICE_UNKNOWN; } else if (temp_entry->type == LOGENTRY_SERVICE_RECOVERY || temp_entry->type == LOGENTRY_SERVICE_OK) { strcpy(image, OK_ICON); strcpy(image_alt, OK_ICON_ALT); history_detail_type = HISTORY_SERVICE_RECOVERY; } break; /* service flapping alerts */ case LOGENTRY_SERVICE_FLAPPING_STARTED: case LOGENTRY_SERVICE_FLAPPING_STOPPED: case LOGENTRY_SERVICE_FLAPPING_DISABLED: if (display_flapping_alerts == FALSE) continue; history_type = SERVICE_FLAPPING_HISTORY; /* get host and service names */ temp_buffer = my_strtok(temp_entry->entry_text, ":"); temp_buffer = my_strtok(NULL, ";"); if (temp_buffer) entry_host_name = strdup(temp_buffer + 1); else entry_host_name = NULL; temp_buffer = my_strtok(NULL, ";"); if (temp_buffer) entry_service_desc = strdup(temp_buffer); else entry_service_desc = NULL; strcpy(image, FLAPPING_ICON); if (temp_entry->type == LOGENTRY_SERVICE_FLAPPING_STARTED) strcpy(image_alt, "Service started flapping"); else if (temp_entry->type == LOGENTRY_SERVICE_FLAPPING_STOPPED) strcpy(image_alt, "Service stopped flapping"); else if (temp_entry->type == LOGENTRY_SERVICE_FLAPPING_DISABLED) strcpy(image_alt, "Service flap detection disabled"); break; /* service downtime alerts */ case LOGENTRY_SERVICE_DOWNTIME_STARTED: case LOGENTRY_SERVICE_DOWNTIME_STOPPED: case LOGENTRY_SERVICE_DOWNTIME_CANCELLED: if (display_downtime_alerts == FALSE) continue; history_type = SERVICE_DOWNTIME_HISTORY; /* get host and service names */ temp_buffer = my_strtok(temp_entry->entry_text, ":"); temp_buffer = my_strtok(NULL, ";"); if (temp_buffer) entry_host_name = strdup(temp_buffer + 1); else entry_host_name = NULL; temp_buffer = my_strtok(NULL, ";"); if (temp_buffer) entry_service_desc = strdup(temp_buffer); else entry_service_desc = NULL; strcpy(image, DOWNTIME_ICON); if (temp_entry->type == LOGENTRY_SERVICE_DOWNTIME_STARTED) strcpy(image_alt, "Service entered a period of scheduled downtime"); else if (temp_entry->type == LOGENTRY_SERVICE_DOWNTIME_STOPPED) strcpy(image_alt, "Service exited from a period of scheduled downtime"); else if (temp_entry->type == LOGENTRY_SERVICE_DOWNTIME_CANCELLED) strcpy(image_alt, "Service scheduled downtime has been cancelled"); break; /* host state alerts */ case LOGENTRY_HOST_DOWN: case LOGENTRY_HOST_UNREACHABLE: case LOGENTRY_HOST_RECOVERY: case LOGENTRY_HOST_UP: history_type = HOST_HISTORY; /* get host name */ temp_buffer = my_strtok(temp_entry->entry_text, ":"); temp_buffer = my_strtok(NULL, ";"); if (temp_buffer) entry_host_name = strdup(temp_buffer + 1); else entry_host_name = NULL; if (temp_entry->type == LOGENTRY_HOST_DOWN) { strcpy(image, HOST_DOWN_ICON); strcpy(image_alt, HOST_DOWN_ICON_ALT); history_detail_type = HISTORY_HOST_DOWN; } else if (temp_entry->type == LOGENTRY_HOST_UNREACHABLE) { strcpy(image, HOST_UNREACHABLE_ICON); strcpy(image_alt, HOST_UNREACHABLE_ICON_ALT); history_detail_type = HISTORY_HOST_UNREACHABLE; } else if (temp_entry->type == LOGENTRY_HOST_RECOVERY || temp_entry->type == LOGENTRY_HOST_UP) { strcpy(image, HOST_UP_ICON); strcpy(image_alt, HOST_UP_ICON_ALT); history_detail_type = HISTORY_HOST_RECOVERY; } break; /* host flapping alerts */ case LOGENTRY_HOST_FLAPPING_STARTED: case LOGENTRY_HOST_FLAPPING_STOPPED: case LOGENTRY_HOST_FLAPPING_DISABLED: if (display_flapping_alerts == FALSE) continue; history_type = HOST_FLAPPING_HISTORY; /* get host name */ temp_buffer = my_strtok(temp_entry->entry_text, ":"); temp_buffer = my_strtok(NULL, ";"); if (temp_buffer) entry_host_name = strdup(temp_buffer + 1); else entry_host_name = NULL; strcpy(image, FLAPPING_ICON); if (temp_entry->type == LOGENTRY_HOST_FLAPPING_STARTED) strcpy(image_alt, "Host started flapping"); else if (temp_entry->type == LOGENTRY_HOST_FLAPPING_STOPPED) strcpy(image_alt, "Host stopped flapping"); else if (temp_entry->type == LOGENTRY_HOST_FLAPPING_DISABLED) strcpy(image_alt, "Host flap detection disabled"); break; /* host downtime alerts */ case LOGENTRY_HOST_DOWNTIME_STARTED: case LOGENTRY_HOST_DOWNTIME_STOPPED: case LOGENTRY_HOST_DOWNTIME_CANCELLED: if (display_downtime_alerts == FALSE) continue; history_type = HOST_DOWNTIME_HISTORY; /* get host name */ temp_buffer = my_strtok(temp_entry->entry_text, ":"); temp_buffer = my_strtok(NULL, ";"); if (temp_buffer) entry_host_name = strdup(temp_buffer + 1); else entry_host_name = NULL; strcpy(image, DOWNTIME_ICON); if (temp_entry->type == LOGENTRY_HOST_DOWNTIME_STARTED) strcpy(image_alt, "Host entered a period of scheduled downtime"); else if (temp_entry->type == LOGENTRY_HOST_DOWNTIME_STOPPED) strcpy(image_alt, "Host exited from a period of scheduled downtime"); else if (temp_entry->type == LOGENTRY_HOST_DOWNTIME_CANCELLED) strcpy(image_alt, "Host scheduled downtime has been cancelled"); break; /* program start */ case LOGENTRY_STARTUP: if (display_system_messages == FALSE) continue; strcpy(image, START_ICON); strcpy(image_alt, START_ICON_ALT); system_message = TRUE; break; /* program termination */ case LOGENTRY_SHUTDOWN: case LOGENTRY_BAILOUT: if (display_system_messages == FALSE) continue; strcpy(image, STOP_ICON); strcpy(image_alt, STOP_ICON_ALT); system_message = TRUE; break; /* program restart */ case LOGENTRY_RESTART: if (display_system_messages == FALSE) continue; strcpy(image, RESTART_ICON); strcpy(image_alt, RESTART_ICON_ALT); system_message = TRUE; break; } image[sizeof(image) - 1] = '\x0'; image_alt[sizeof(image_alt) - 1] = '\x0'; /* get the timestamp */ time_ptr = localtime(&temp_entry->timestamp); strftime(current_message_date, sizeof(current_message_date), "%B %d, %Y %H:00\n", time_ptr); current_message_date[sizeof(current_message_date) - 1] = '\x0'; get_time_string(&temp_entry->timestamp, date_time, sizeof(date_time), SHORT_DATE_TIME); strip(date_time); if (strcmp(image, "")) { display_line = FALSE; if (system_message == TRUE) display_line = TRUE; else if (display_type == DISPLAY_HOSTS || display_type == DISPLAY_HOSTGROUPS) { if (history_type == HOST_HISTORY || history_type == SERVICE_HISTORY) { snprintf(match1, sizeof(match1), " HOST ALERT: %s;", host_name); snprintf(match2, sizeof(match2), " SERVICE ALERT: %s;", host_name); } else if (history_type == HOST_FLAPPING_HISTORY || history_type == SERVICE_FLAPPING_HISTORY) { snprintf(match1, sizeof(match1), " HOST FLAPPING ALERT: %s;", host_name); snprintf(match2, sizeof(match2), " SERVICE FLAPPING ALERT: %s;", host_name); } else if (history_type == HOST_DOWNTIME_HISTORY || history_type == SERVICE_DOWNTIME_HISTORY) { snprintf(match1, sizeof(match1), " HOST DOWNTIME ALERT: %s;", host_name); snprintf(match2, sizeof(match2), " SERVICE DOWNTIME ALERT: %s;", host_name); } if (show_all_hosts == TRUE) display_line = TRUE; else if (strstr(temp_entry->entry_text, match1)) display_line = TRUE; else if (strstr(temp_entry->entry_text, match2)) display_line = TRUE; if (display_line == TRUE) { if (history_options == HISTORY_ALL) display_line = TRUE; else if (history_options == HISTORY_HOST_ALL && (history_type == HOST_HISTORY || history_type == HOST_FLAPPING_HISTORY || history_type == HOST_DOWNTIME_HISTORY)) display_line = TRUE; else if (history_options == HISTORY_SERVICE_ALL && (history_type == SERVICE_HISTORY || history_type == SERVICE_FLAPPING_HISTORY || history_type == SERVICE_DOWNTIME_HISTORY)) display_line = TRUE; else if ((history_type == HOST_HISTORY || history_type == SERVICE_HISTORY) && (history_detail_type & history_options)) display_line = TRUE; else display_line = FALSE; } /* check alert state types */ if (display_line == TRUE && (history_type == HOST_HISTORY || history_type == SERVICE_HISTORY)) { if (state_options == STATE_ALL) display_line = TRUE; else if ((state_options & STATE_SOFT) && strstr(temp_entry->entry_text, ";SOFT;")) display_line = TRUE; else if ((state_options & STATE_HARD) && strstr(temp_entry->entry_text, ";HARD;")) display_line = TRUE; else display_line = FALSE; } } else if (display_type == DISPLAY_SERVICES || display_type == DISPLAY_SERVICEGROUPS) { if (history_type == SERVICE_HISTORY) snprintf(match1, sizeof(match1), " SERVICE ALERT: %s;%s;", host_name, service_desc); else if (history_type == SERVICE_FLAPPING_HISTORY) snprintf(match1, sizeof(match1), " SERVICE FLAPPING ALERT: %s;%s;", host_name, service_desc); else if (history_type == SERVICE_DOWNTIME_HISTORY) snprintf(match1, sizeof(match1), " SERVICE DOWNTIME ALERT: %s;%s;", host_name, service_desc); if (display_type == DISPLAY_SERVICEGROUPS) display_line = TRUE; else if (strstr(temp_entry->entry_text, match1)) display_line = TRUE; if (history_type != SERVICE_HISTORY && history_type != SERVICE_FLAPPING_HISTORY && history_type != SERVICE_DOWNTIME_HISTORY) display_line = FALSE; if (display_line == TRUE) { if (history_options == HISTORY_ALL || history_options == HISTORY_SERVICE_ALL) display_line = TRUE; else if (history_options & history_detail_type) display_line = TRUE; else display_line = FALSE; } /* check alert state type */ if (display_line == TRUE && history_type == SERVICE_HISTORY) { if (state_options == STATE_ALL) display_line = TRUE; else if ((state_options & STATE_SOFT) && strstr(temp_entry->entry_text, ";SOFT;")) display_line = TRUE; else if ((state_options & STATE_HARD) && strstr(temp_entry->entry_text, ";HARD;")) display_line = TRUE; else display_line = FALSE; } } /* make sure user is authorized to view this log entry */ if (display_line == TRUE) { if (system_message == TRUE) { if (is_authorized_for_system_information(¤t_authdata) == FALSE) display_line = FALSE; } else { temp_host = find_host(entry_host_name); if (history_type == HOST_HISTORY || history_type == HOST_FLAPPING_HISTORY || history_type == HOST_DOWNTIME_HISTORY) { if (is_authorized_for_host(temp_host, ¤t_authdata) == FALSE) display_line = FALSE; else if (display_type == DISPLAY_HOSTGROUPS && is_host_member_of_hostgroup(temp_hostgroup, temp_host) == FALSE) display_line = FALSE; } else { temp_service = find_service(entry_host_name, entry_service_desc); if (is_authorized_for_service(temp_service, ¤t_authdata) == FALSE) display_line = FALSE; else if (display_type == DISPLAY_HOSTGROUPS && is_host_member_of_hostgroup(temp_hostgroup, temp_host) == FALSE) display_line = FALSE; else if (display_type == DISPLAY_SERVICEGROUPS && is_service_member_of_servicegroup(temp_servicegroup, temp_service) == FALSE) display_line = FALSE; } } } /* display the entry if we should... */ if (display_line == TRUE) { if (result_limit != 0 && (((total_entries + 1) < result_start) || (total_entries >= ((result_start + result_limit) - 1)))) { total_entries++; continue; } displayed_entries++; total_entries++; if (strcmp(last_message_date, current_message_date) != 0 && display_timebreaks == TRUE) { printf("</DIV><BR CLEAR='all' />\n"); printf("<DIV CLASS='dateTimeBreak'>\n"); printf("<table border=0 width=95%%><tr>"); printf("<td width=40%%><hr width=100%%></td>"); printf("<td align=center CLASS='dateTimeBreak'>%s</td>", current_message_date); printf("<td width=40%%><hr width=100%%></td>"); printf("</tr></table>\n"); printf("</DIV>\n"); printf("<BR CLEAR='all' /><DIV CLASS='logEntries'>\n"); strncpy(last_message_date, current_message_date, sizeof(last_message_date)); last_message_date[sizeof(last_message_date) - 1] = '\x0'; } if (display_frills == TRUE) printf("<img align='left' src='%s%s' alt='%s' title='%s' />", url_images_path, image, image_alt, image_alt); printf("[%s] %s", date_time, html_encode(temp_entry->entry_text, FALSE)); if (enable_splunk_integration == TRUE) { printf(" "); display_splunk_generic_url(temp_entry->entry_text, 2); } printf("<br clear='all' />\n"); } } /* free memory */ free(entry_host_name); entry_host_name = NULL; free(entry_service_desc); entry_service_desc = NULL; } } free_log_entries(&entry_list); printf("</DIV>\n"); if (total_entries == 0) { printf("<HR>\n"); printf("<DIV CLASS='errorMessage' style='text-align:center'>No history information was found "); if (display_type == DISPLAY_HOSTS) printf("%s", (show_all_hosts == TRUE) ? "" : "for this host "); else printf("for this service "); printf("in log files for selected date.</DIV>"); printf("<script type='text/javascript'>document.getElementById('hist_page_selector').style.display='none';</script>"); } else { printf("<HR>\n"); page_num_selector(result_start, total_entries, displayed_entries); } return; }
off_t xml_listdir(char *d, int *dt, int *ft, u_long lev, dev_t dev) { char *path; bool nlf = FALSE; long pathsize = 0; struct _info **dir, **sav; struct stat sb; int t, n, mt; if ((Level >= 0) && (lev > Level)) { if (!noindent) fputc('\n',outfile); return 0; } if (xdev && lev == 0) { stat(d,&sb); dev = sb.st_dev; } sav = dir = read_dir(d,&n); if (!dir && n) { fprintf(outfile,"<error>opening dir</error>\n"); return 0; } if (!n) { if (!noindent) fputc('\n', outfile); free_dir(sav); return 0; } if (flimit > 0 && n > flimit) { fprintf(outfile,"<error>%d entries exceeds filelimit, not opening dir</error>%s",n,noindent?"":"\n"); free_dir(sav); return 0; } if (cmpfunc) qsort(dir,n,sizeof(struct _info *), cmpfunc); if (lev >= maxdirs-1) { dirs = xrealloc(dirs,sizeof(int) * (maxdirs += 1024)); memset(dirs+(maxdirs-1024), 0, sizeof(int) * 1024); } dirs[lev] = 1; if (!*(dir+1)) dirs[lev] = 2; if (!noindent) fprintf(outfile,"\n"); path = malloc(pathsize=4096); while(*dir) { if (!noindent) xml_indent(lev); if ((*dir)->lnk) mt = (*dir)->mode & S_IFMT; else mt = (*dir)->mode & S_IFMT; for(t=0;ifmt[t];t++) if (ifmt[t] == mt) break; fprintf(outfile,"<%s", ftype[t]); if (fflag) { if (sizeof(char) * (strlen(d)+strlen((*dir)->name)+2) > pathsize) path=xrealloc(path,pathsize=(sizeof(char) * (strlen(d)+strlen((*dir)->name)+1024))); if (!strcmp(d,"/")) sprintf(path,"%s%s",d,(*dir)->name); else sprintf(path,"%s/%s",d,(*dir)->name); } else { if (sizeof(char) * (strlen((*dir)->name)+1) > pathsize) path=xrealloc(path,pathsize=(sizeof(char) * (strlen((*dir)->name)+1024))); sprintf(path,"%s",(*dir)->name); } fprintf(outfile, " name=\""); html_encode(outfile,path); fputc('"',outfile); if ((*dir)->lnk) { fprintf(outfile, " target=\""); html_encode(outfile,(*dir)->lnk); fputc('"',outfile); } xml_fillinfo(*dir); fputc('>',outfile); if ((*dir)->isdir) { if ((*dir)->lnk) { if (lflag && !(xdev && dev != (*dir)->dev)) { if (findino((*dir)->inode,(*dir)->dev)) { fprintf(outfile,"<error>recursive, not followed</error>"); } else { saveino((*dir)->inode, (*dir)->dev); if (*(*dir)->lnk == '/') listdir((*dir)->lnk,dt,ft,lev+1,dev); else { if (strlen(d)+strlen((*dir)->lnk)+2 > pathsize) path=xrealloc(path,pathsize=(strlen(d)+strlen((*dir)->name)+1024)); if (fflag && !strcmp(d,"/")) sprintf(path,"%s%s",d,(*dir)->lnk); else sprintf(path,"%s/%s",d,(*dir)->lnk); listdir(path,dt,ft,lev+1,dev); } nlf = TRUE; } } } else if (!(xdev && dev != (*dir)->dev)) { if (strlen(d)+strlen((*dir)->name)+2 > pathsize) path=xrealloc(path,pathsize=(strlen(d)+strlen((*dir)->name)+1024)); if (fflag && !strcmp(d,"/")) sprintf(path,"%s%s",d,(*dir)->name); else sprintf(path,"%s/%s",d,(*dir)->name); saveino((*dir)->inode, (*dir)->dev); listdir(path,dt,ft,lev+1,dev); nlf = TRUE; } *dt += 1; } else *ft += 1; if (*(dir+1) && !*(dir+2)) dirs[lev] = 2; if (nlf) { nlf = FALSE; if (!noindent) xml_indent(lev); } fprintf(outfile,"</%s>%s",ftype[t],noindent?"":"\n"); dir++; } dirs[lev] = 0; free(path); free_dir(sav); return 0; }
/** @brief Yes we need a main function **/ int main(void) { int result = OK; /* get the variables passed to us */ process_cgivars(); /* reset internal CGI variables */ reset_cgi_vars(); /* read the CGI configuration file */ result = read_cgi_config_file(get_cgi_config_location()); if (result == ERROR) { document_header(CGI_ID, FALSE, "Error"); print_error(get_cgi_config_location(), ERROR_CGI_CFG_FILE, FALSE); document_footer(CGI_ID); return ERROR; } /* read the main configuration file */ result = read_main_config_file(main_config_file); if (result == ERROR) { document_header(CGI_ID, FALSE, "Error"); print_error(main_config_file, ERROR_CGI_MAIN_CFG, FALSE); document_footer(CGI_ID); return ERROR; } /* read all object configuration data */ result = read_all_object_configuration_data(main_config_file, READ_ALL_OBJECT_DATA); if (result == ERROR) { document_header(CGI_ID, FALSE, "Error"); print_error(NULL, ERROR_CGI_OBJECT_DATA, FALSE); document_footer(CGI_ID); return ERROR; } /* overwrite config value with amount we got via GET */ result_limit = (get_result_limit != -1) ? get_result_limit : result_limit; document_header(CGI_ID, TRUE, "History"); /* get authentication information */ get_authentication_information(¤t_authdata); /* calculate timestamps for reading logs */ convert_timeperiod_to_times(TIMEPERIOD_SINGLE_DAY, &ts_start, &ts_end); if (display_header == TRUE) { /* begin top table */ printf("<table border=0 width=100%%>\n"); printf("<tr>\n"); /* left column of the first row */ printf("<td align=left valign=top width=33%%>\n"); if (display_type == DISPLAY_HOSTS) display_info_table("Host Alert History", ¤t_authdata, daemon_check); else if (display_type == DISPLAY_SERVICES) display_info_table("Service Alert History", ¤t_authdata, daemon_check); else if (display_type == DISPLAY_HOSTGROUPS) display_info_table("Host Group Alert History", ¤t_authdata, daemon_check); else if (display_type == DISPLAY_SERVICEGROUPS) display_info_table("Service Group Alert History", ¤t_authdata, daemon_check); else display_info_table("Alert History", ¤t_authdata, daemon_check); printf("<TABLE BORDER=1 CELLPADDING=0 CELLSPACING=0 CLASS='linkBox'>\n"); printf("<TR><TD CLASS='linkBox'>\n"); if (display_type == DISPLAY_HOSTS) { printf("<a href='%s?host=%s'>View <b>Service Status Detail</b> For <b>%s</b></a><br>\n", STATUS_CGI, (show_all_hosts == TRUE) ? "all" : url_encode(host_name), (show_all_hosts == TRUE) ? "All Hosts" : "This Host"); printf("<a href='%s?host=%s'>View <b>Notifications</b> For <b>%s</b></a><br>\n", NOTIFICATIONS_CGI, (show_all_hosts == TRUE) ? "all" : url_encode(host_name), (show_all_hosts == TRUE) ? "All Hosts" : "This Host"); printf("<a href='%s?type=%d&host=%s'>View <b>Information</b> For <b>This Host</b></a><br>\n", EXTINFO_CGI, DISPLAY_HOST_INFO, url_encode(host_name)); #ifdef USE_TRENDS if (show_all_hosts == FALSE) printf("<a href='%s?host=%s'>View <b>Trends</b> For <b>This Host</b></a>\n", TRENDS_CGI, url_encode(host_name)); #endif } else if (display_type == DISPLAY_SERVICES) { printf("<a href='%s?host=%s&service=%s'>View <b>Notifications</b> For <b>This Service</b></a><br>\n", NOTIFICATIONS_CGI, url_encode(host_name), url_encode(service_desc)); printf("<a href='%s?type=%d&host=%s&service=%s'>View <b>Information</b> For <b>This Service</b></a><br>\n", EXTINFO_CGI, DISPLAY_SERVICE_INFO, url_encode(host_name), url_encode(service_desc)); #ifdef USE_TRENDS printf("<a href='%s?host=%s&service=%s'>View <b>Trends</b> For <b>This Service</b></a><br>\n", TRENDS_CGI, url_encode(host_name), url_encode(service_desc)); #endif printf("<a href='%s?host=%s'>View <b>Alert History</b> For <b>This Host</b></a>\n", HISTORY_CGI, url_encode(host_name)); } else if (display_type == DISPLAY_HOSTGROUPS) { printf("<a href='%s?hostgroup=%s&style=hostdetail'>View <b>Host Status Detail</b> For <b>This Hostgroup</b></a><br>\n", STATUS_CGI, url_encode(hostgroup_name)); printf("<a href='%s?hostgroup=%s&style=detail'>View <b>Service Status Detail</b> For <b>This Hostgroup</b></a><br>\n", STATUS_CGI, url_encode(hostgroup_name)); printf("<a href='%s?hostgroup=%s'>View <b>Notifications</b> For <b>This Hostgroup</b></a>\n", NOTIFICATIONS_CGI, url_encode(hostgroup_name)); } else if (display_type == DISPLAY_SERVICEGROUPS) { printf("<a href='%s?servicegroup=%s&style=hostdetail'>View <b>Host Status Detail</b> For <b>This Servicegroup</b></a><br>\n", STATUS_CGI, url_encode(servicegroup_name)); printf("<a href='%s?servicegroup=%s&style=detail'>View <b>Service Status Detail</b> For <b>This Servicegroup</b></a><br>\n", STATUS_CGI, url_encode(servicegroup_name)); printf("<a href='%s?servicegroup=%s'>View <b>Notifications</b> For <b>This Servicegroup</b></a>\n", NOTIFICATIONS_CGI, url_encode(servicegroup_name)); } printf("</TD></TR>\n"); printf("</TABLE>\n"); printf("</td>\n"); /* middle column of top row */ printf("<td align=center valign=top width=33%%>\n"); printf("<DIV ALIGN=CENTER CLASS='dataTitle'>\n"); if (display_type == DISPLAY_SERVICES) printf("Service '%s' On Host '%s'", html_encode(service_desc, TRUE), html_encode(host_name, TRUE)); else if (display_type == DISPLAY_HOSTS) { if (show_all_hosts == TRUE) printf("All Hosts and Services"); else printf("Host '%s'", html_encode(host_name, TRUE)); } else if (display_type == DISPLAY_HOSTGROUPS) printf("Host Group '%s'", html_encode(hostgroup_name, TRUE)); else if (display_type == DISPLAY_SERVICEGROUPS) printf("Service Group '%s'", html_encode(servicegroup_name, TRUE)); printf("</DIV>\n"); printf("<BR />\n"); display_nav_table(ts_start, ts_end); printf("</td>\n"); /* right hand column of top row */ printf("<td align=right valign=top width=33%%>\n"); printf("<form method=\"GET\" action=\"%s\">\n", HISTORY_CGI); printf("<input type='hidden' name='ts_start' value='%lu'>\n", ts_start); printf("<input type='hidden' name='ts_end' value='%lu'>\n", ts_end); printf("<input type='hidden' name='limit' value='%d'>\n", result_limit); if (display_type == DISPLAY_HOSTGROUPS) printf("<input type='hidden' name='hostgroup' value='%s'>\n", escape_string(hostgroup_name)); else if (display_type == DISPLAY_SERVICEGROUPS) printf("<input type='hidden' name='servicegroup' value='%s'>\n", escape_string(servicegroup_name)); else { printf("<input type='hidden' name='host' value='%s'>\n", (show_all_hosts == TRUE) ? "all" : escape_string(host_name)); if (display_type == DISPLAY_SERVICES) printf("<input type='hidden' name='service' value='%s'>\n", escape_string(service_desc)); } printf("<table border=0 CLASS='optBox'>\n"); printf("<tr>\n"); printf("<td align=left CLASS='optBoxItem'>State type options:</td>\n"); printf("</tr>\n"); printf("<tr>\n"); printf("<td align=left CLASS='optBoxItem'><select name='statetype'>\n"); printf("<option value=%d %s>All state types</option>\n", STATE_ALL, (state_options == STATE_ALL) ? "selected" : ""); printf("<option value=%d %s>Soft states</option>\n", STATE_SOFT, (state_options == STATE_SOFT) ? "selected" : ""); printf("<option value=%d %s>Hard states</option>\n", STATE_HARD, (state_options == STATE_HARD) ? "selected" : ""); printf("</select></td>\n"); printf("</tr>\n"); printf("<tr>\n"); printf("<td align=left CLASS='optBoxItem'>History detail level for "); if (display_type == DISPLAY_HOSTGROUPS || display_type == DISPLAY_SERVICEGROUPS) printf("this %sgroup", (display_type == DISPLAY_HOSTGROUPS) ? "host" : "service"); else if (display_type == DISPLAY_HOSTS) printf("%s host%s", (show_all_hosts == TRUE) ? "all" : "this", (show_all_hosts == TRUE) ? "s" : ""); else printf("service"); printf(":</td>\n"); printf("</tr>\n"); printf("<tr>\n"); printf("<td align=left CLASS='optBoxItem'><select name='type'>\n"); if (display_type == DISPLAY_HOSTS || display_type == DISPLAY_HOSTGROUPS) printf("<option value=%d %s>All alerts</option>\n", HISTORY_ALL, (history_options == HISTORY_ALL) ? "selected" : ""); printf("<option value=%d %s>All service alerts</option>\n", HISTORY_SERVICE_ALL, (history_options == HISTORY_SERVICE_ALL) ? "selected" : ""); if (display_type == DISPLAY_HOSTS || display_type == DISPLAY_HOSTGROUPS) printf("<option value=%d %s>All host alerts</option>\n", HISTORY_HOST_ALL, (history_options == HISTORY_HOST_ALL) ? "selected" : ""); printf("<option value=%d %s>Service warning</option>\n", HISTORY_SERVICE_WARNING, (history_options == HISTORY_SERVICE_WARNING) ? "selected" : ""); printf("<option value=%d %s>Service unknown</option>\n", HISTORY_SERVICE_UNKNOWN, (history_options == HISTORY_SERVICE_UNKNOWN) ? "selected" : ""); printf("<option value=%d %s>Service critical</option>\n", HISTORY_SERVICE_CRITICAL, (history_options == HISTORY_SERVICE_CRITICAL) ? "selected" : ""); printf("<option value=%d %s>Service recovery</option>\n", HISTORY_SERVICE_RECOVERY, (history_options == HISTORY_SERVICE_RECOVERY) ? "selected" : ""); if (display_type == DISPLAY_HOSTS || display_type == DISPLAY_HOSTGROUPS) { printf("<option value=%d %s>Host down</option>\n", HISTORY_HOST_DOWN, (history_options == HISTORY_HOST_DOWN) ? "selected" : ""); printf("<option value=%d %s>Host unreachable</option>\n", HISTORY_HOST_UNREACHABLE, (history_options == HISTORY_HOST_UNREACHABLE) ? "selected" : ""); printf("<option value=%d %s>Host recovery</option>\n", HISTORY_HOST_RECOVERY, (history_options == HISTORY_HOST_RECOVERY) ? "selected" : ""); } printf("</select></td>\n"); printf("</tr>\n"); printf("<tr>\n"); printf("<td align=left valign=bottom CLASS='optBoxItem'><input type='checkbox' name='noflapping' %s> Hide Flapping Alerts</td>", (display_flapping_alerts == FALSE) ? "checked" : ""); printf("</tr>\n"); printf("<tr>\n"); printf("<td align=left valign=bottom CLASS='optBoxItem'><input type='checkbox' name='nodowntime' %s> Hide Downtime Alerts</td>", (display_downtime_alerts == FALSE) ? "checked" : ""); printf("</tr>\n"); printf("<tr>\n"); printf("<td align=left valign=bottom CLASS='optBoxItem'><input type='checkbox' name='nosystem' %s> Hide Process Messages</td>", (display_system_messages == FALSE) ? "checked" : ""); printf("</tr>\n"); printf("<tr>\n"); printf("<td align=left valign=bottom CLASS='optBoxItem'><input type='checkbox' name='order' value='old2new' %s> Older Entries First</td>", (reverse == TRUE) ? "checked" : ""); printf("</tr>\n"); printf("<tr>\n"); printf("<td align=left CLASS='optBoxItem'><input type='submit' value='Update'></td>\n"); printf("</tr>\n"); printf("</table>\n"); printf("</form>\n"); printf("</td>\n"); /* end of top table */ printf("</tr>\n"); printf("</table>\n"); } /* display history */ show_history(); document_footer(CGI_ID); /* free allocated memory */ free_memory(); return OK; }
void display_notifications(void) { mmapfile *thefile=NULL; char *input=NULL; char *temp_buffer; char date_time[MAX_DATETIME_LENGTH]; char alert_level[MAX_INPUT_BUFFER]; char alert_level_class[MAX_INPUT_BUFFER]; char contact_name[MAX_INPUT_BUFFER]; char service_name[MAX_INPUT_BUFFER]; char host_name[MAX_INPUT_BUFFER]; char method_name[MAX_INPUT_BUFFER]; int show_entry; int total_notifications; int notification_type=SERVICE_NOTIFICATION; int notification_detail_type=NOTIFICATION_SERVICE_CRITICAL; int odd=0; time_t t; host *temp_host; service *temp_service; int result; if(use_lifo==TRUE) { result=read_file_into_lifo(log_file_to_use); if(result!=LIFO_OK) { if(result==LIFO_ERROR_MEMORY) { printf("<P><DIV CLASS='warningMessage'>Not enough memory to reverse log file - displaying notifications in natural order...</DIV></P>"); } else if(result==LIFO_ERROR_FILE) { printf("<P><DIV CLASS='errorMessage'>Error: Cannot open log file '%s' for reading!</DIV></P>",log_file_to_use); return; } use_lifo=FALSE; } } if(use_lifo==FALSE) { if((thefile=mmap_fopen(log_file_to_use))==NULL) { printf("<P><DIV CLASS='errorMessage'>Error: Cannot open log file '%s' for reading!</DIV></P>",log_file_to_use); return; } } printf("<p>\n"); printf("<div align='center'>\n"); printf("<table border=0 CLASS='notifications'>\n"); printf("<tr>\n"); printf("<th CLASS='notifications'>Host</th>\n"); printf("<th CLASS='notifications'>Service</th>\n"); printf("<th CLASS='notifications'>Type</th>\n"); printf("<th CLASS='notifications'>Time</th>\n"); printf("<th CLASS='notifications'>Contact</th>\n"); printf("<th CLASS='notifications'>Notification Command</th>\n"); printf("<th CLASS='notifications'>Information</th>\n"); printf("</tr>\n"); total_notifications=0; while(1) { free(input); if(use_lifo==TRUE) { if((input=pop_lifo())==NULL) break; } else { if((input=mmap_fgets(thefile))==NULL) break; } strip(input); /* see if this line contains the notification event string */ if(strstr(input,HOST_NOTIFICATION_STRING)||strstr(input,SERVICE_NOTIFICATION_STRING)) { if(strstr(input,HOST_NOTIFICATION_STRING)) notification_type=HOST_NOTIFICATION; else notification_type=SERVICE_NOTIFICATION; /* get the date/time */ temp_buffer=(char *)strtok(input,"]"); t=(time_t)(temp_buffer==NULL)?0L:strtoul(temp_buffer+1,NULL,10); get_time_string(&t,date_time,(int)sizeof(date_time),SHORT_DATE_TIME); strip(date_time); /* get the contact name */ temp_buffer=(char *)strtok(NULL,":"); temp_buffer=(char *)strtok(NULL,";"); snprintf(contact_name,sizeof(contact_name),"%s",(temp_buffer==NULL)?"":temp_buffer+1); contact_name[sizeof(contact_name)-1]='\x0'; /* get the host name */ temp_buffer=(char *)strtok(NULL,";"); snprintf(host_name,sizeof(host_name),"%s",(temp_buffer==NULL)?"":temp_buffer); host_name[sizeof(host_name)-1]='\x0'; /* get the service name */ if(notification_type==SERVICE_NOTIFICATION) { temp_buffer=(char *)strtok(NULL,";"); snprintf(service_name,sizeof(service_name),"%s",(temp_buffer==NULL)?"":temp_buffer); service_name[sizeof(service_name)-1]='\x0'; } /* get the alert level */ temp_buffer=(char *)strtok(NULL,";"); snprintf(alert_level,sizeof(alert_level),"%s",(temp_buffer==NULL)?"":temp_buffer); alert_level[sizeof(alert_level)-1]='\x0'; if(notification_type==SERVICE_NOTIFICATION) { if(!strcmp(alert_level,"CRITICAL")) { notification_detail_type=NOTIFICATION_SERVICE_CRITICAL; strcpy(alert_level_class,"CRITICAL"); } else if(!strcmp(alert_level,"WARNING")) { notification_detail_type=NOTIFICATION_SERVICE_WARNING; strcpy(alert_level_class,"WARNING"); } else if(!strcmp(alert_level,"RECOVERY") || !strcmp(alert_level,"OK")) { strcpy(alert_level,"OK"); notification_detail_type=NOTIFICATION_SERVICE_RECOVERY; strcpy(alert_level_class,"OK"); } else if(strstr(alert_level,"CUSTOM (")) { notification_detail_type=NOTIFICATION_SERVICE_CUSTOM; strcpy(alert_level_class,"CUSTOM"); } else if(strstr(alert_level,"ACKNOWLEDGEMENT (")) { notification_detail_type=NOTIFICATION_SERVICE_ACK; strcpy(alert_level_class,"ACKNOWLEDGEMENT"); } else if(strstr(alert_level,"FLAPPINGSTART (")) { strcpy(alert_level,"FLAPPING START"); notification_detail_type=NOTIFICATION_SERVICE_FLAP; strcpy(alert_level_class,"UNKNOWN"); } else if(strstr(alert_level,"FLAPPINGSTOP (")) { strcpy(alert_level,"FLAPPING STOP"); notification_detail_type=NOTIFICATION_SERVICE_FLAP; strcpy(alert_level_class,"UNKNOWN"); } else { strcpy(alert_level,"UNKNOWN"); notification_detail_type=NOTIFICATION_SERVICE_UNKNOWN; strcpy(alert_level_class,"UNKNOWN"); } } else { if(!strcmp(alert_level,"DOWN")) { strncpy(alert_level,"HOST DOWN",sizeof(alert_level)); strcpy(alert_level_class,"HOSTDOWN"); notification_detail_type=NOTIFICATION_HOST_DOWN; } else if(!strcmp(alert_level,"UNREACHABLE")) { strncpy(alert_level,"HOST UNREACHABLE",sizeof(alert_level)); strcpy(alert_level_class,"HOSTUNREACHABLE"); notification_detail_type=NOTIFICATION_HOST_UNREACHABLE; } else if(!strcmp(alert_level,"RECOVERY") || !strcmp(alert_level,"UP")) { strncpy(alert_level,"HOST UP",sizeof(alert_level)); strcpy(alert_level_class,"HOSTUP"); notification_detail_type=NOTIFICATION_HOST_RECOVERY; } else if(strstr(alert_level,"CUSTOM (")) { strcpy(alert_level_class,"HOSTCUSTOM"); notification_detail_type=NOTIFICATION_HOST_CUSTOM; } else if(strstr(alert_level,"ACKNOWLEDGEMENT (")) { strcpy(alert_level_class,"HOSTACKNOWLEDGEMENT"); notification_detail_type=NOTIFICATION_HOST_ACK; } else if(strstr(alert_level,"FLAPPINGSTART (")) { strcpy(alert_level,"FLAPPING START"); strcpy(alert_level_class,"UNKNOWN"); notification_detail_type=NOTIFICATION_HOST_FLAP; } else if(strstr(alert_level,"FLAPPINGSTOP (")) { strcpy(alert_level,"FLAPPING STOP"); strcpy(alert_level_class,"UNKNOWN"); notification_detail_type=NOTIFICATION_HOST_FLAP; } } /* get the method name */ temp_buffer=(char *)strtok(NULL,";"); snprintf(method_name,sizeof(method_name),"%s",(temp_buffer==NULL)?"":temp_buffer); method_name[sizeof(method_name)-1]='\x0'; /* move to the informational message */ temp_buffer=strtok(NULL,";"); show_entry=FALSE; /* if we're searching by contact, filter out unwanted contact */ if(query_type==FIND_CONTACT) { if(find_all==TRUE) show_entry=TRUE; else if(!strcmp(query_contact_name,contact_name)) show_entry=TRUE; } else if(query_type==FIND_HOST) { if(find_all==TRUE) show_entry=TRUE; else if(!strcmp(query_host_name,host_name)) show_entry=TRUE; } else if(query_type==FIND_SERVICE) { if(!strcmp(query_host_name,host_name) && !strcmp(query_svc_description,service_name)) show_entry=TRUE; } if(show_entry==TRUE) { if(notification_options==NOTIFICATION_ALL) show_entry=TRUE; else if(notification_options==NOTIFICATION_HOST_ALL && notification_type==HOST_NOTIFICATION) show_entry=TRUE; else if(notification_options==NOTIFICATION_SERVICE_ALL && notification_type==SERVICE_NOTIFICATION) show_entry=TRUE; else if(notification_detail_type & notification_options) show_entry=TRUE; else show_entry=FALSE; } /* make sure user has authorization to view this notification */ if(notification_type==HOST_NOTIFICATION) { temp_host=find_host(host_name); if(is_authorized_for_host(temp_host,¤t_authdata)==FALSE) show_entry=FALSE; } else { temp_service=find_service(host_name,service_name); if(is_authorized_for_service(temp_service,¤t_authdata)==FALSE) show_entry=FALSE; } if(show_entry==TRUE) { total_notifications++; if(odd) { odd=0; printf("<tr CLASS='notificationsOdd'>\n"); } else { odd=1; printf("<tr CLASS='notificationsEven'>\n"); } printf("<td CLASS='notifications%s'><a href='%s?type=%d&host=%s'>%s</a></td>\n",(odd)?"Even":"Odd",EXTINFO_CGI,DISPLAY_HOST_INFO,url_encode(host_name),host_name); if(notification_type==SERVICE_NOTIFICATION) { printf("<td CLASS='notifications%s'><a href='%s?type=%d&host=%s",(odd)?"Even":"Odd",EXTINFO_CGI,DISPLAY_SERVICE_INFO,url_encode(host_name)); printf("&service=%s'>%s</a></td>\n",url_encode(service_name),service_name); } else printf("<td CLASS='notifications%s'>N/A</td>\n",(odd)?"Even":"Odd"); printf("<td CLASS='notifications%s'>%s</td>\n",alert_level_class,alert_level); printf("<td CLASS='notifications%s'>%s</td>\n",(odd)?"Even":"Odd",date_time); printf("<td CLASS='notifications%s'><a href='%s?type=contacts#%s'>%s</a></td>\n",(odd)?"Even":"Odd",CONFIG_CGI,url_encode(contact_name),contact_name); printf("<td CLASS='notifications%s'><a href='%s?type=commands#%s'>%s</a></td>\n",(odd)?"Even":"Odd",CONFIG_CGI,url_encode(method_name),method_name); printf("<td CLASS='notifications%s'>%s</td>\n",(odd)?"Even":"Odd",html_encode(temp_buffer,FALSE)); printf("</tr>\n"); } } } printf("</table>\n"); printf("</div>\n"); printf("</p>\n"); if(total_notifications==0) { printf("<P><DIV CLASS='errorMessage'>No notifications have been recorded"); if(find_all==FALSE) { if(query_type==FIND_SERVICE) printf(" for this service"); else if(query_type==FIND_CONTACT) printf(" for this contact"); else printf(" for this host"); } printf(" in %s log file</DIV></P>",(log_archive==0)?"the current":"this archived"); } free(input); if(use_lifo==TRUE) free_lifo_memory(); else mmap_fclose(thefile); return; }
/* display the contents of the log file */ int display_log(void) { char *input = NULL; char image[MAX_INPUT_BUFFER]; char image_alt[MAX_INPUT_BUFFER]; time_t t; char *temp_buffer = NULL; char date_time[MAX_DATETIME_LENGTH]; int error = FALSE; mmapfile *thefile = NULL; char last_message_date[MAX_INPUT_BUFFER] = ""; char current_message_date[MAX_INPUT_BUFFER] = ""; struct tm *time_ptr = NULL; /* check to see if the user is authorized to view the log file */ if(is_authorized_for_system_information(¤t_authdata) == FALSE) { printf("<HR>\n"); printf("<DIV CLASS='errorMessage'>%s</DIV><br><br>\n",_("It appears as though you do not have permission to view the log file...")); printf("<DIV CLASS='errorDescription'>%s</DIV>\n",_("If you believe this is an error, check the HTTP server authentication requirements for accessing this CGI<br>and check the authorization options in your CGI configuration file.")); printf("<HR>\n"); return ERROR; } error = FALSE; if(use_lifo == TRUE) { error = read_file_into_lifo(log_file_to_use); if(error != LIFO_OK) { if(error == LIFO_ERROR_MEMORY) { printf("<P><DIV CLASS='warningMessage'>Not enough memory to reverse log file - displaying log in natural order...</DIV></P>"); error = FALSE; } else error = TRUE; use_lifo = FALSE; } else error = FALSE; } if(use_lifo == FALSE) { if((thefile = mmap_fopen(log_file_to_use)) == NULL) { printf("<HR>\n"); printf("<P><DIV CLASS='errorMessage'>%s</DIV></P>", log_file_to_use,_("Error: Could not open log file '%s' for reading!")); printf("<HR>\n"); error = TRUE; } } if(error == FALSE) { printf("<P><DIV CLASS='logEntries'>\n"); while(1) { free(input); if(use_lifo == TRUE) { if((input = pop_lifo()) == NULL) break; } else if((input = mmap_fgets(thefile)) == NULL) break; strip(input); if(strstr(input, " starting...")) { strcpy(image, START_ICON); strcpy(image_alt, START_ICON_ALT); } else if(strstr(input, " shutting down...")) { strcpy(image, STOP_ICON); strcpy(image_alt, STOP_ICON_ALT); } else if(strstr(input, "Bailing out")) { strcpy(image, STOP_ICON); strcpy(image_alt, STOP_ICON_ALT); } else if(strstr(input, " restarting...")) { strcpy(image, RESTART_ICON); strcpy(image_alt, RESTART_ICON_ALT); } else if(strstr(input, "HOST ALERT:") && strstr(input, ";DOWN;")) { strcpy(image, HOST_DOWN_ICON); strcpy(image_alt, HOST_DOWN_ICON_ALT); } else if(strstr(input, "HOST ALERT:") && strstr(input, ";UNREACHABLE;")) { strcpy(image, HOST_UNREACHABLE_ICON); strcpy(image_alt, HOST_UNREACHABLE_ICON_ALT); } else if(strstr(input, "HOST ALERT:") && (strstr(input, ";RECOVERY;") || strstr(input, ";UP;"))) { strcpy(image, HOST_UP_ICON); strcpy(image_alt, HOST_UP_ICON_ALT); } else if(strstr(input, "HOST NOTIFICATION:")) { strcpy(image, HOST_NOTIFICATION_ICON); strcpy(image_alt, HOST_NOTIFICATION_ICON_ALT); } else if(strstr(input, "SERVICE ALERT:") && strstr(input, ";CRITICAL;")) { strcpy(image, CRITICAL_ICON); strcpy(image_alt, CRITICAL_ICON_ALT); } else if(strstr(input, "SERVICE ALERT:") && strstr(input, ";WARNING;")) { strcpy(image, WARNING_ICON); strcpy(image_alt, WARNING_ICON_ALT); } else if(strstr(input, "SERVICE ALERT:") && strstr(input, ";UNKNOWN;")) { strcpy(image, UNKNOWN_ICON); strcpy(image_alt, UNKNOWN_ICON_ALT); } else if(strstr(input, "SERVICE ALERT:") && (strstr(input, ";RECOVERY;") || strstr(input, ";OK;"))) { strcpy(image, OK_ICON); strcpy(image_alt, OK_ICON_ALT); } else if(strstr(input, "SERVICE NOTIFICATION:")) { strcpy(image, NOTIFICATION_ICON); strcpy(image_alt, NOTIFICATION_ICON_ALT); } else if(strstr(input, "SERVICE EVENT HANDLER:")) { strcpy(image, SERVICE_EVENT_ICON); strcpy(image_alt, SERVICE_EVENT_ICON_ALT); } else if(strstr(input, "HOST EVENT HANDLER:")) { strcpy(image, HOST_EVENT_ICON); strcpy(image_alt, HOST_EVENT_ICON_ALT); } else if(strstr(input, "EXTERNAL COMMAND:")) { strcpy(image, EXTERNAL_COMMAND_ICON); strcpy(image_alt, EXTERNAL_COMMAND_ICON_ALT); } else if(strstr(input, "PASSIVE SERVICE CHECK:")) { strcpy(image, PASSIVE_ICON); strcpy(image_alt, "Passive Service Check"); } else if(strstr(input, "PASSIVE HOST CHECK:")) { strcpy(image, PASSIVE_ICON); strcpy(image_alt, "Passive Host Check"); } else if(strstr(input, "LOG ROTATION:")) { strcpy(image, LOG_ROTATION_ICON); strcpy(image_alt, LOG_ROTATION_ICON_ALT); } else if(strstr(input, "active mode...")) { strcpy(image, ACTIVE_ICON); strcpy(image_alt, ACTIVE_ICON_ALT); } else if(strstr(input, "standby mode...")) { strcpy(image, STANDBY_ICON); strcpy(image_alt, STANDBY_ICON_ALT); } else if(strstr(input, "SERVICE FLAPPING ALERT:") && strstr(input, ";STARTED;")) { strcpy(image, FLAPPING_ICON); strcpy(image_alt, "Service started flapping"); } else if(strstr(input, "SERVICE FLAPPING ALERT:") && strstr(input, ";STOPPED;")) { strcpy(image, FLAPPING_ICON); strcpy(image_alt, "Service stopped flapping"); } else if(strstr(input, "SERVICE FLAPPING ALERT:") && strstr(input, ";DISABLED;")) { strcpy(image, FLAPPING_ICON); strcpy(image_alt, "Service flap detection disabled"); } else if(strstr(input, "HOST FLAPPING ALERT:") && strstr(input, ";STARTED;")) { strcpy(image, FLAPPING_ICON); strcpy(image_alt, "Host started flapping"); } else if(strstr(input, "HOST FLAPPING ALERT:") && strstr(input, ";STOPPED;")) { strcpy(image, FLAPPING_ICON); strcpy(image_alt, "Host stopped flapping"); } else if(strstr(input, "HOST FLAPPING ALERT:") && strstr(input, ";DISABLED;")) { strcpy(image, FLAPPING_ICON); strcpy(image_alt, "Host flap detection disabled"); } else if(strstr(input, "SERVICE DOWNTIME ALERT:") && strstr(input, ";STARTED;")) { strcpy(image, SCHEDULED_DOWNTIME_ICON); strcpy(image_alt, "Service entered a period of scheduled downtime"); } else if(strstr(input, "SERVICE DOWNTIME ALERT:") && strstr(input, ";STOPPED;")) { strcpy(image, SCHEDULED_DOWNTIME_ICON); strcpy(image_alt, "Service exited a period of scheduled downtime"); } else if(strstr(input, "SERVICE DOWNTIME ALERT:") && strstr(input, ";CANCELLED;")) { strcpy(image, SCHEDULED_DOWNTIME_ICON); strcpy(image_alt, "Service scheduled downtime has been cancelled"); } else if(strstr(input, "HOST DOWNTIME ALERT:") && strstr(input, ";STARTED;")) { strcpy(image, SCHEDULED_DOWNTIME_ICON); strcpy(image_alt, "Host entered a period of scheduled downtime"); } else if(strstr(input, "HOST DOWNTIME ALERT:") && strstr(input, ";STOPPED;")) { strcpy(image, SCHEDULED_DOWNTIME_ICON); strcpy(image_alt, "Host exited a period of scheduled downtime"); } else if(strstr(input, "HOST DOWNTIME ALERT:") && strstr(input, ";CANCELLED;")) { strcpy(image, SCHEDULED_DOWNTIME_ICON); strcpy(image_alt, "Host scheduled downtime has been cancelled"); } else { strcpy(image, INFO_ICON); strcpy(image_alt, INFO_ICON_ALT); } temp_buffer = strtok(input, "]"); t = (temp_buffer == NULL) ? 0L : strtoul(temp_buffer + 1, NULL, 10); time_ptr = localtime(&t); strftime(current_message_date, sizeof(current_message_date), "%B %d, %Y %H:00\n", time_ptr); current_message_date[sizeof(current_message_date) - 1] = '\x0'; if(strcmp(last_message_date, current_message_date) != 0 && display_timebreaks == TRUE) { printf("<BR CLEAR='all'>\n"); printf("<DIV CLASS='dateTimeBreak'>\n"); printf("<table border=0 width=95%%><tr>"); printf("<td width=40%%><hr width=100%%></td>"); printf("<td align=center CLASS='dateTimeBreak'>%s</td>", current_message_date); printf("<td width=40%%><hr width=100%%></td>"); printf("</tr></table>\n"); printf("</DIV>\n"); printf("<BR CLEAR='all'><DIV CLASS='logEntries'>\n"); strncpy(last_message_date, current_message_date, sizeof(last_message_date)); last_message_date[sizeof(last_message_date) - 1] = '\x0'; } get_time_string(&t, date_time, (int)sizeof(date_time), SHORT_DATE_TIME); strip(date_time); temp_buffer = strtok(NULL, "\n"); if(display_frills == TRUE) printf("<img align='left' src='%s%s' alt='%s' title='%s'>", url_images_path, image, image_alt, image_alt); printf("[%s] %s", date_time, (temp_buffer == NULL) ? "" : html_encode(temp_buffer, FALSE)); if(enable_splunk_integration == TRUE) { printf(" "); display_splunk_generic_url(temp_buffer, 2); } printf("<br clear='all'>\n"); } printf("</DIV></P>\n"); printf("<HR>\n"); free(input); if(use_lifo == FALSE) mmap_fclose(thefile); } if(use_lifo == TRUE) free_lifo_memory(); return OK; }
int do_addform( REQUEST *r, RESPONSE *resp ) { #ifndef MODIFY do_error(r, resp, LDAP_OTHER, NOT_IMPLEMENTED, MSG_NOT_SUPPORTED, NULL); return NOTOK; #else /* defined MODIFY */ int rc, in_home; char *bind_as, *pw, *args, *tmpl, *basedn, *rdn, *ufn, *cp; LDAPMessage *res; struct timeval timeout; struct ldap_disptmpl *templ; /* * query = default Base-DN, * args = dn=BindDN&userPassword=PASSWD&tmpl=template&basedn=DN */ args = r->r_method == POST ? r->r_postdata : r->r_filter; #ifdef WEB500GW_DEBUG Web500gw_debug(WEB500GW_DEBUG_TRACE, "do_addform (%s, %s)\n", r->r_dn, args ? args : "", 0, 0); #endif if (args == NULL) { do_error(r, resp, LDAP_OTHER, BAD_REQUEST, NULL, NULL); return NOTOK; } bind_as = pw = tmpl = basedn = NULL; cp = args; while (cp && *cp) { if (strncmp(cp, "dn=", 3) == 0) { bind_as = cp + 3; } else if (strncmp(cp, "userPassword="******"tmpl=", 5) == 0) { tmpl = cp + 5; } else if (strncmp(cp, "basedn=", 7) == 0) { basedn = cp + 7; } /* else ignore */ cp = strchr(cp, '&'); if (cp && *cp) *cp++ = '\0'; } if (!bind_as) { do_error(r, resp, LDAP_OTHER, BAD_REQUEST, MSG_NO_BIND_DN, NULL); return NOTOK; } hex_qdecode(bind_as); if (! pw) { do_error(r, resp, LDAP_OTHER, BAD_REQUEST, MSG_MISSING_PASSWORD, NULL); return NOTOK; } hex_qdecode(pw); if (basedn && *basedn) { hex_qdecode(basedn); } else { basedn = r->r_dn; } hex_qdecode(tmpl); if (tmpl && *tmpl) /* overrides the template in URL */ r->r_template = tmpl; if (! (r->r_template && *r->r_template)) { /* need a objectclass template */ do_error(r, resp, LDAP_OTHER, BAD_REQUEST, MSG_TEMPLATE_MISSING, NULL); return NOTOK; } /* No bind necessary at this stage but checking it anyway to block * non-admins */ if ((!bind_as) || strlen(bind_as) == 0) { /* No DN to bind as */ do_error (r, resp, LDAP_INAPPROPRIATE_AUTH, BAD_REQUEST, MSG_UNKNOWN_ARGUMENT, NULL); return NOTOK; } r->r_binddn = bind_as; if ((!pw) || strlen(pw) == 0) { /* we need a password for simple auth */ do_error (r, resp, LDAP_INAPPROPRIATE_AUTH, 0, MSG_NULL_PASSWORD, NULL); return NOTOK; } #ifdef WEB500GW_DEBUG Web500gw_debug(WEB500GW_DEBUG_TRACE, "BINDING as %s ... ", bind_as, 0, 0, 0); #endif if ((r->r_ld = web500gw_ldap_init(r, resp, bind_as, pw, 1)) == (LDAP *)0) return NOTOK; #ifdef WEB500GW_DEBUG Web500gw_debug(WEB500GW_DEBUG_TRACE, "BOUND\n", 0, 0, 0, 0); #endif /* let's see if basedn exists ... */ timeout.tv_sec = timelimit; timeout.tv_usec = 0; if ((rc = ldap_search_st(r->r_ld, basedn, LDAP_SCOPE_BASE, default_filter, NULL, 0, &timeout, &res)) != LDAP_SUCCESS) { /* better error description here ??? */ do_error(r, resp, rc, 0, get_ldap_error_str(r->r_ld), get_ldap_matched_str(r->r_ld)); return NOTOK; } /* check if nonleaf here ??? */ ufn = friendly_dn(resp, basedn); rdn = friendly_rdn(resp, basedn, 1); in_home = isinhome(basedn); /* find the requested template */ templ = ldap_name2template(r->r_template, r->r_access->a_tmpllist); if (templ == NULL) { /* template not found */ do_error(r, resp, LDAP_OTHER, BAD_REQUEST, MSG_TEMPLATE_MISSING, NULL); return NOTOK; } /* .. and see if it's allowed to add such an entry */ if (!LDAP_IS_DISPTMPL_OPTION_SET(templ, LDAP_DTMPL_OPT_ADDABLE)) { do_error(r, resp, LDAP_OTHER, BAD_REQUEST, MSG_ADD_OC_NOT_ADDABLE, NULL); return NOTOK; } if (r->r_httpversion == 1 && resp->resp_httpheader == 0) { /* Expires now - don't allow to cache */ /* resp->resp_expires = 0; */ http_header(r, resp); } if (resp->resp_htmlheader == 0) { msg_fprintf(fp, in_home && *MSG_HTML_START_MISC_HOME ? MSG_HTML_START_MISC_HOME : MSG_HTML_START_MISC, "sss", MSG_ADD, rdn, ufn); resp->resp_htmlheader = 1; } msg_fprintf(fp, MSG_EXPLAIN_ADD, "ss", rdn, ufn); fprintf(fp, "<FORM METHOD=POST ACTION=\"%s\">\n", dn2url(r, basedn, FLAG_LANGUAGE|FLAG_TMPL, ACTION_ADD, NULL, NULL)); if (r->r_browser->b_opts & B_HIDDEN) { fprintf(fp, "<INPUT TYPE=\"hidden\" NAME=\"adder_dn\" VALUE=\"%s\">\n\ <INPUT TYPE=\"hidden\" NAME=\"adder_pw\" VALUE=\"%s\">\n", html_encode(bind_as, strlen(bind_as)), html_encode(pw, strlen(pw))); } else {
int get_data(struct DESKTOP_ENTRY *data, char *path) { FILE *fp; char s[3000]; int len; bool is_link = false, is_app = false; int i; int result = 0; fp = fopen(path, "r"); if (fp == NULL) return 0; while (fgets(s, 3000, fp) != NULL) { /* chomp */ len = strlen(s); i = 2; while (i > 0) { if (s[len - i] == '\n' || s[len - i] == '\r') { s[len - i] = '\0'; break; } i--; } len -= i; /* Check the file is actually a .desktop file */ if (strstr(s, "[InternetShortcut]") != NULL) { is_link = true; is_app = false; data->type = LINK; result = 1; } if (strstr(s, "[Desktop Entry]") != NULL && !is_link) { is_app = true; data->type = APPLICATION; result = 1; } if (is_link) { /* Copy the URL found in the file */ if (strncmp(s, "URL=", 4) == 0) { data->url = html_encode(s + 4); } } else if (is_app) { if (strncmp(s, "Name=", 5) == 0 && data->name == NULL) { data->name = html_encode(s + 5); } else if (strncmp(s, "Exec=", 5) == 0 && data->command == NULL) { char *temp = html_encode(s + 5); data->command = delete_argument(temp); free(temp); } else if (strncmp(s, "Comment[ja]=", 12) == 0) { if (data->comment != NULL) free(data->comment); data->comment = html_encode(s + 12); } else if (strncmp(s, "Comment=", 8) == 0 && data->comment == NULL) { data->comment = html_encode(s + 8); } else if (strncmp(s, "Terminal=", 9) == 0) { for (i = 0; i < strlen(s); i++) { s[i] = tolower(s[i]); } if (strstr(s, "true") != NULL) { data->has_gui = false; } else if (strstr(s, "false") != NULL) { data->has_gui = true; } } } } fclose(fp); if (data->name == NULL) { char *a = strrchr(path, '/') + 1; char *b = strrchr(path, '.'); data->name = malloc(b - a + 1); strncpy(data->name, a, b - a); data->name[b - a] = '\0'; } if (result == 1 && data->type == APPLICATION) { char *fn = strrchr(path, '/') + 1; char *ex = strrchr(path, '.'); len = ex - fn; data->icon = malloc(len + 5); char *a = data->icon, *b = fn; while (*b != '\0' && b < ex) { if (*b == ' ') { *a = '-'; } else { *a = tolower(*b); } a++; b++; } *a = '\0'; } return result; }
void display_logentries() { char image[MAX_INPUT_BUFFER]; char image_alt[MAX_INPUT_BUFFER]; char last_message_date[MAX_INPUT_BUFFER] = ""; char current_message_date[MAX_INPUT_BUFFER] = ""; char date_time[MAX_DATETIME_LENGTH]; char *error_text = NULL; int status = READLOG_OK; int i = 0; int user_has_seen_something = FALSE; int json_start = TRUE; int total_entries = 0; int displayed_entries = 0; struct tm *time_ptr = NULL; logentry *entry_list = NULL; logentry *temp_entry = NULL; logfilter *filter_list = NULL; /* Add default filters */ if (showlog_initial_states == FALSE) { add_log_filter(&filter_list, LOGENTRY_SERVICE_INITIAL_STATE, LOGFILTER_EXCLUDE); add_log_filter(&filter_list, LOGENTRY_HOST_INITIAL_STATE, LOGFILTER_EXCLUDE); } if (showlog_current_states == FALSE) { add_log_filter(&filter_list, LOGENTRY_SERVICE_CURRENT_STATE, LOGFILTER_EXCLUDE); add_log_filter(&filter_list, LOGENTRY_HOST_CURRENT_STATE, LOGFILTER_EXCLUDE); } /* Add requested filters */ if (show_notifications == FALSE) { add_log_filter(&filter_list, LOGENTRY_HOST_NOTIFICATION, LOGFILTER_EXCLUDE); add_log_filter(&filter_list, LOGENTRY_SERVICE_NOTIFICATION, LOGFILTER_EXCLUDE); } if (show_host_status == FALSE) { add_log_filter(&filter_list, LOGENTRY_HOST_UP, LOGFILTER_EXCLUDE); add_log_filter(&filter_list, LOGENTRY_HOST_DOWN, LOGFILTER_EXCLUDE); add_log_filter(&filter_list, LOGENTRY_HOST_UNREACHABLE, LOGFILTER_EXCLUDE); add_log_filter(&filter_list, LOGENTRY_HOST_RECOVERY, LOGFILTER_EXCLUDE); add_log_filter(&filter_list, LOGENTRY_PASSIVE_HOST_CHECK, LOGFILTER_EXCLUDE); } if (show_service_status == FALSE) { add_log_filter(&filter_list, LOGENTRY_SERVICE_OK, LOGFILTER_EXCLUDE); add_log_filter(&filter_list, LOGENTRY_SERVICE_WARNING, LOGFILTER_EXCLUDE); add_log_filter(&filter_list, LOGENTRY_SERVICE_CRITICAL, LOGFILTER_EXCLUDE); add_log_filter(&filter_list, LOGENTRY_SERVICE_UNKNOWN, LOGFILTER_EXCLUDE); add_log_filter(&filter_list, LOGENTRY_SERVICE_RECOVERY, LOGFILTER_EXCLUDE); add_log_filter(&filter_list, LOGENTRY_PASSIVE_SERVICE_CHECK, LOGFILTER_EXCLUDE); } if (show_external_commands == FALSE) add_log_filter(&filter_list, LOGENTRY_EXTERNAL_COMMAND, LOGFILTER_EXCLUDE); if (show_system_messages == FALSE) { add_log_filter(&filter_list, LOGENTRY_SYSTEM_WARNING, LOGFILTER_EXCLUDE); add_log_filter(&filter_list, LOGENTRY_STARTUP, LOGFILTER_EXCLUDE); add_log_filter(&filter_list, LOGENTRY_SHUTDOWN, LOGFILTER_EXCLUDE); add_log_filter(&filter_list, LOGENTRY_BAILOUT, LOGFILTER_EXCLUDE); add_log_filter(&filter_list, LOGENTRY_RESTART, LOGFILTER_EXCLUDE); add_log_filter(&filter_list, LOGENTRY_LOG_ROTATION, LOGFILTER_EXCLUDE); add_log_filter(&filter_list, LOGENTRY_AUTOSAVE, LOGFILTER_EXCLUDE); add_log_filter(&filter_list, LOGENTRY_IDOMOD, LOGFILTER_EXCLUDE); } if (show_event_handler == FALSE) { add_log_filter(&filter_list, LOGENTRY_SERVICE_EVENT_HANDLER, LOGFILTER_EXCLUDE); add_log_filter(&filter_list, LOGENTRY_HOST_EVENT_HANDLER, LOGFILTER_EXCLUDE); } if (show_flapping == FALSE) { add_log_filter(&filter_list, LOGENTRY_SERVICE_FLAPPING_STARTED, LOGFILTER_EXCLUDE); add_log_filter(&filter_list, LOGENTRY_SERVICE_FLAPPING_STOPPED, LOGFILTER_EXCLUDE); add_log_filter(&filter_list, LOGENTRY_SERVICE_FLAPPING_DISABLED, LOGFILTER_EXCLUDE); add_log_filter(&filter_list, LOGENTRY_HOST_FLAPPING_STARTED, LOGFILTER_EXCLUDE); add_log_filter(&filter_list, LOGENTRY_HOST_FLAPPING_STOPPED, LOGFILTER_EXCLUDE); add_log_filter(&filter_list, LOGENTRY_HOST_FLAPPING_DISABLED, LOGFILTER_EXCLUDE); } if (show_downtime == FALSE) { add_log_filter(&filter_list, LOGENTRY_SERVICE_DOWNTIME_STARTED, LOGFILTER_EXCLUDE); add_log_filter(&filter_list, LOGENTRY_SERVICE_DOWNTIME_STOPPED, LOGFILTER_EXCLUDE); add_log_filter(&filter_list, LOGENTRY_SERVICE_DOWNTIME_CANCELLED, LOGFILTER_EXCLUDE); add_log_filter(&filter_list, LOGENTRY_HOST_DOWNTIME_STARTED, LOGFILTER_EXCLUDE); add_log_filter(&filter_list, LOGENTRY_HOST_DOWNTIME_STOPPED, LOGFILTER_EXCLUDE); add_log_filter(&filter_list, LOGENTRY_HOST_DOWNTIME_CANCELLED, LOGFILTER_EXCLUDE); } /* scan the log file for archived state data */ status = get_log_entries(&entry_list, &filter_list, &error_text, query_string, reverse, ts_start, ts_end); free_log_filters(&filter_list); /* dealing with errors */ if (status == READLOG_ERROR_WARNING) { if (error_text != NULL) { print_generic_error_message(error_text, NULL, 0); my_free(error_text); } else print_generic_error_message("Unkown error!", NULL, 0); } if (status == READLOG_ERROR_MEMORY) print_generic_error_message("Out of memory...", "showing all I could get!", 0); if (status == READLOG_ERROR_FATAL) { if (error_text != NULL) { print_generic_error_message(error_text, NULL, 0); my_free(error_text); } user_has_seen_something = TRUE; /* now we start displaying the log entries */ } else { if (content_type == JSON_CONTENT) { display_timebreaks = FALSE; if (status != READLOG_OK) printf(",\n"); printf("\"log_entries\": [\n"); } else if (content_type == CSV_CONTENT) { display_timebreaks = FALSE; printf("%sTimestamp%s%s", csv_data_enclosure, csv_data_enclosure, csv_delimiter); printf("%sDate Time%s%s", csv_data_enclosure, csv_data_enclosure, csv_delimiter); printf("%sLog Entry%s\n", csv_data_enclosure, csv_data_enclosure); } else { /* add export to csv, json, link */ printf("<table width='100%%' cellspacing=0 cellpadding=0 border=0><tr><td width='33%%'></td><td width='33%%' align=center nowrap>"); printf("<div class='page_selector' id='log_page_selector'>\n"); printf("<div id='page_navigation_copy'></div>"); page_limit_selector(result_start); printf("</div>\n"); printf("</td><td width='33%%' align='right'>\n"); printf("<div class='csv_export_link' style='margin-right:1em;'>"); print_export_link(CSV_CONTENT, SHOWLOG_CGI, NULL); print_export_link(JSON_CONTENT, SHOWLOG_CGI, NULL); print_export_link(HTML_CONTENT, SHOWLOG_CGI, NULL); printf("</div></td></tr></table>"); printf("</div>\n"); printf("<DIV CLASS='logEntries'>\n"); } for (temp_entry = entry_list; temp_entry != NULL; temp_entry = temp_entry->next) { if (result_limit != 0 && (((total_entries + 1) < result_start) || (total_entries >= ((result_start + result_limit) - 1)))) { total_entries++; continue; } total_entries++; displayed_entries++; /* set the correct icon and icon alt text for current log entry */ if (temp_entry->type == LOGENTRY_STARTUP) { strcpy(image, START_ICON); strcpy(image_alt, START_ICON_ALT); } else if (temp_entry->type == LOGENTRY_SHUTDOWN || temp_entry->type == LOGENTRY_BAILOUT) { strcpy(image, STOP_ICON); strcpy(image_alt, STOP_ICON_ALT); } else if (temp_entry->type == LOGENTRY_RESTART) { strcpy(image, RESTART_ICON); strcpy(image_alt, RESTART_ICON_ALT); } else if (temp_entry->type == LOGENTRY_HOST_DOWN) { strcpy(image, HOST_DOWN_ICON); strcpy(image_alt, HOST_DOWN_ICON_ALT); } else if (temp_entry->type == LOGENTRY_HOST_UNREACHABLE) { strcpy(image, HOST_UNREACHABLE_ICON); strcpy(image_alt, HOST_UNREACHABLE_ICON_ALT); } else if (temp_entry->type == LOGENTRY_HOST_RECOVERY || temp_entry->type == LOGENTRY_HOST_UP) { strcpy(image, HOST_UP_ICON); strcpy(image_alt, HOST_UP_ICON_ALT); } else if (temp_entry->type == LOGENTRY_HOST_NOTIFICATION) { strcpy(image, HOST_NOTIFICATION_ICON); strcpy(image_alt, HOST_NOTIFICATION_ICON_ALT); } else if (temp_entry->type == LOGENTRY_SERVICE_CRITICAL) { strcpy(image, CRITICAL_ICON); strcpy(image_alt, CRITICAL_ICON_ALT); } else if (temp_entry->type == LOGENTRY_SERVICE_WARNING) { strcpy(image, WARNING_ICON); strcpy(image_alt, WARNING_ICON_ALT); } else if (temp_entry->type == LOGENTRY_SERVICE_UNKNOWN) { strcpy(image, UNKNOWN_ICON); strcpy(image_alt, UNKNOWN_ICON_ALT); } else if (temp_entry->type == LOGENTRY_SERVICE_RECOVERY || temp_entry->type == LOGENTRY_SERVICE_OK) { strcpy(image, OK_ICON); strcpy(image_alt, OK_ICON_ALT); } else if (temp_entry->type == LOGENTRY_SERVICE_NOTIFICATION) { strcpy(image, NOTIFICATION_ICON); strcpy(image_alt, NOTIFICATION_ICON_ALT); } else if (temp_entry->type == LOGENTRY_SERVICE_EVENT_HANDLER) { strcpy(image, SERVICE_EVENT_ICON); strcpy(image_alt, SERVICE_EVENT_ICON_ALT); } else if (temp_entry->type == LOGENTRY_HOST_EVENT_HANDLER) { strcpy(image, HOST_EVENT_ICON); strcpy(image_alt, HOST_EVENT_ICON_ALT); } else if (temp_entry->type == LOGENTRY_EXTERNAL_COMMAND) { strcpy(image, EXTERNAL_COMMAND_ICON); strcpy(image_alt, EXTERNAL_COMMAND_ICON_ALT); } else if (temp_entry->type == LOGENTRY_PASSIVE_SERVICE_CHECK) { strcpy(image, PASSIVE_ICON); strcpy(image_alt, "Passive Service Check"); } else if (temp_entry->type == LOGENTRY_PASSIVE_HOST_CHECK) { strcpy(image, PASSIVE_ICON); strcpy(image_alt, "Passive Host Check"); } else if (temp_entry->type == LOGENTRY_LOG_ROTATION) { strcpy(image, LOG_ROTATION_ICON); strcpy(image_alt, LOG_ROTATION_ICON_ALT); } else if (temp_entry->type == LOGENTRY_ACTIVE_MODE) { strcpy(image, ACTIVE_ICON); strcpy(image_alt, ACTIVE_ICON_ALT); } else if (temp_entry->type == LOGENTRY_STANDBY_MODE) { strcpy(image, STANDBY_ICON); strcpy(image_alt, STANDBY_ICON_ALT); } else if (temp_entry->type == LOGENTRY_SERVICE_FLAPPING_STARTED) { strcpy(image, FLAPPING_ICON); strcpy(image_alt, "Service started flapping"); } else if (temp_entry->type == LOGENTRY_SERVICE_FLAPPING_STOPPED) { strcpy(image, FLAPPING_ICON); strcpy(image_alt, "Service stopped flapping"); } else if (temp_entry->type == LOGENTRY_SERVICE_FLAPPING_DISABLED) { strcpy(image, FLAPPING_ICON); strcpy(image_alt, "Service flap detection disabled"); } else if (temp_entry->type == LOGENTRY_HOST_FLAPPING_STARTED) { strcpy(image, FLAPPING_ICON); strcpy(image_alt, "Host started flapping"); } else if (temp_entry->type == LOGENTRY_HOST_FLAPPING_STOPPED) { strcpy(image, FLAPPING_ICON); strcpy(image_alt, "Host stopped flapping"); } else if (temp_entry->type == LOGENTRY_HOST_FLAPPING_DISABLED) { strcpy(image, FLAPPING_ICON); strcpy(image_alt, "Host flap detection disabled"); } else if (temp_entry->type == LOGENTRY_SERVICE_DOWNTIME_STARTED) { strcpy(image, DOWNTIME_ICON); strcpy(image_alt, "Service entered a period of scheduled downtime"); } else if (temp_entry->type == LOGENTRY_SERVICE_DOWNTIME_STOPPED) { strcpy(image, DOWNTIME_ICON); strcpy(image_alt, "Service exited a period of scheduled downtime"); } else if (temp_entry->type == LOGENTRY_SERVICE_DOWNTIME_CANCELLED) { strcpy(image, DOWNTIME_ICON); strcpy(image_alt, "Service scheduled downtime has been cancelled"); } else if (temp_entry->type == LOGENTRY_HOST_DOWNTIME_STARTED) { strcpy(image, DOWNTIME_ICON); strcpy(image_alt, "Host entered a period of scheduled downtime"); } else if (temp_entry->type == LOGENTRY_HOST_DOWNTIME_STOPPED) { strcpy(image, DOWNTIME_ICON); strcpy(image_alt, "Host exited a period of scheduled downtime"); } else if (temp_entry->type == LOGENTRY_HOST_DOWNTIME_CANCELLED) { strcpy(image, DOWNTIME_ICON); strcpy(image_alt, "Host scheduled downtime has been cancelled"); } else if (temp_entry->type == LOGENTRY_IDOMOD) { strcpy(image, DATABASE_ICON); strcpy(image_alt, "IDOMOD Information"); } else if (temp_entry->type == LOGENTRY_NPCDMOD) { strcpy(image, STATS_ICON); strcpy(image_alt, "NPCDMOD Information"); } else if (temp_entry->type == LOGENTRY_AUTOSAVE) { strcpy(image, AUTOSAVE_ICON); strcpy(image_alt, "Auto-save retention data"); } else if (temp_entry->type == LOGENTRY_SYSTEM_WARNING) { strcpy(image, DAEMON_WARNING_ICON); strcpy(image_alt, "Icinga warning message"); } else { strcpy(image, INFO_ICON); strcpy(image_alt, INFO_ICON_ALT); } time_ptr = localtime(&temp_entry->timestamp); strftime(current_message_date, sizeof(current_message_date), "%B %d, %Y %H:00", time_ptr); current_message_date[sizeof(current_message_date) - 1] = '\x0'; if (strcmp(last_message_date, current_message_date) != 0 && display_timebreaks == TRUE) { printf("</DIV>\n"); printf("<BR>\n"); printf("<DIV>\n"); printf("<table border=0 width=99%% CLASS='dateTimeBreak' align=center><tr>"); printf("<td width=40%%><hr width=100%%></td>"); printf("<td align=center CLASS='dateTimeBreak'>%s</td>", current_message_date); printf("<td width=40%%><hr width=100%%></td>"); printf("</tr></table>\n"); printf("</DIV>\n"); printf("<BR><DIV CLASS='logEntries'>\n"); strncpy(last_message_date, current_message_date, sizeof(last_message_date)); last_message_date[sizeof(last_message_date) - 1] = '\x0'; } get_time_string(&temp_entry->timestamp, date_time, (int)sizeof(date_time), SHORT_DATE_TIME); strip(date_time); /* preparing logentries for json and csv output */ if (content_type == CSV_CONTENT || content_type == JSON_CONTENT) { for (i = 0; i < strlen(temp_entry->entry_text) - 1; i++) *(temp_entry->entry_text + i) = *(temp_entry->entry_text + i + 1); temp_entry->entry_text[strlen(temp_entry->entry_text) - 1] = '\x0'; } /* displays log entry depending on requested content type */ if (content_type == JSON_CONTENT) { // always add a comma, except for the first line if (json_start == FALSE) printf(",\n"); json_start = FALSE; printf("{ \"timestamp\": %lu, ", temp_entry->timestamp); printf(" \"date_time\": \"%s\", ", date_time); printf(" \"log_entry\": \"%s\"}", json_encode(temp_entry->entry_text)); } else if (content_type == CSV_CONTENT) { printf("%s%lu%s%s", csv_data_enclosure, temp_entry->timestamp, csv_data_enclosure, csv_delimiter); printf("%s%s%s%s", csv_data_enclosure, date_time, csv_data_enclosure, csv_delimiter); printf("%s%s%s\n", csv_data_enclosure, temp_entry->entry_text, csv_data_enclosure); } else { if (display_frills == TRUE) printf("<img align=left src='%s%s' alt='%s' title='%s'>", url_images_path, image, image_alt, image_alt); printf("[%s] %s", date_time, (temp_entry->entry_text == NULL) ? "" : html_encode(temp_entry->entry_text, FALSE)); if (enable_splunk_integration == TRUE) { printf(" "); display_splunk_generic_url(temp_entry->entry_text, 2); } printf("<br clear=all>\n"); } user_has_seen_something = TRUE; } if (content_type != CSV_CONTENT && content_type != JSON_CONTENT) { if (user_has_seen_something == TRUE) { printf("</DIV><hr>\n"); page_num_selector(result_start, total_entries, displayed_entries); } else { printf("<script type='text/javascript'>document.getElementById('log_page_selector').style.display='none';</script>"); } } else if (content_type == JSON_CONTENT) printf("\n]\n"); } free_log_entries(&entry_list); if (user_has_seen_something == FALSE && content_type != CSV_CONTENT && content_type != JSON_CONTENT) printf("<DIV CLASS='warningMessage'>No log entries found!</DIV>"); return; }
/** @brief Yes we need a main function **/ int main(void) { char buffer[MAX_DATETIME_LENGTH]; int result = OK; /* get the arguments passed in the URL */ process_cgivars(); /* reset internal variables */ reset_cgi_vars(); /* read the CGI configuration file */ result = read_cgi_config_file(get_cgi_config_location()); if (result == ERROR) { document_header(CGI_ID, FALSE, "Error"); print_error(get_cgi_config_location(), ERROR_CGI_CFG_FILE, FALSE); document_footer(CGI_ID); return ERROR; } /* read the main configuration file */ result = read_main_config_file(main_config_file); if (result == ERROR) { document_header(CGI_ID, FALSE, "Error"); print_error(main_config_file, ERROR_CGI_MAIN_CFG, FALSE); document_footer(CGI_ID); return ERROR; } /* read all object configuration data */ result = read_all_object_configuration_data(main_config_file, READ_ALL_OBJECT_DATA); if (result == ERROR) { document_header(CGI_ID, FALSE, "Error"); print_error(NULL, ERROR_CGI_OBJECT_DATA, FALSE); document_footer(CGI_ID); return ERROR; } /* This requires the date_format parameter in the main config file */ if (timeperiod_type == TIMEPERIOD_CUSTOM) { if (strcmp(start_time_string, "")) string_to_time(start_time_string, &ts_start); if (strcmp(end_time_string, "")) string_to_time(end_time_string, &ts_end); } /* overwrite config value with amount we got via GET */ result_limit = (get_result_limit != -1) ? get_result_limit : result_limit; /* for json and csv output return all by default */ if (get_result_limit == -1 && (content_type == JSON_CONTENT || content_type == CSV_CONTENT)) result_limit = 0; document_header(CGI_ID, TRUE, "Alert Notifications"); /* get authentication information */ get_authentication_information(¤t_authdata); /* calculate timestamps for reading logs */ convert_timeperiod_to_times(timeperiod_type, &ts_start, &ts_end); if (display_header == TRUE) { /* begin top table */ printf("<table border='0' width='100%%'>\n"); printf("<tr>\n"); /* left column of top row */ printf("<td align='left' valign='top' width='33%%'>\n"); if (query_type == DISPLAY_SERVICES) display_info_table("Service Notifications", ¤t_authdata, daemon_check); else if (query_type == DISPLAY_HOSTGROUPS) display_info_table("Hostgroup Notifications", ¤t_authdata, daemon_check); else if (query_type == DISPLAY_SERVICEGROUPS) display_info_table("Servicegroup Notifications", ¤t_authdata, daemon_check); else if (query_type == DISPLAY_HOSTS) { if (find_all == TRUE) display_info_table("Notifications", ¤t_authdata, daemon_check); else display_info_table("Host Notifications", ¤t_authdata, daemon_check); } else display_info_table("Contact Notifications", ¤t_authdata, daemon_check); if (query_type == DISPLAY_HOSTS || query_type == DISPLAY_SERVICES || query_type == DISPLAY_HOSTGROUPS || query_type == DISPLAY_SERVICEGROUPS) { printf("<table border='1' cellpadding='0' cellspacing='0' class='linkBox'>\n"); printf("<tr><td class='linkBox'>\n"); if (query_type == DISPLAY_HOSTS) { printf("<a href='%s?host=%s'>View <b>Status Detail</b> For <b>%s</b></a><br>\n", STATUS_CGI, (find_all == TRUE) ? "all" : url_encode(query_host_name), (find_all == TRUE) ? "All Hosts" : "This Host"); printf("<a href='%s?host=%s'>View <b>Alert History</b> For <b>%s</b></a><br>\n", HISTORY_CGI, (find_all == TRUE) ? "all" : url_encode(query_host_name), (find_all == TRUE) ? "All Hosts" : "This Host"); #ifdef USE_TRENDS if (find_all == FALSE) printf("<a href='%s?host=%s'>View <b>Trends</b> For <b>This Host</b></a><br>\n", TRENDS_CGI, url_encode(query_host_name)); #endif printf("<a href='%s?type=%d&host=%s'>View <b>Information</b> For <b>This Host</b></a><br>\n", EXTINFO_CGI, DISPLAY_HOST_INFO, url_encode(query_host_name)); printf("<a href='%s?host=%s&show_log_entries'>View <b>Availability Report</b> For <b>This Host</b></a><br>\n", AVAIL_CGI, url_encode(query_host_name)); } else if (query_type == DISPLAY_SERVICES) { printf("<a href='%s?host=%s&", HISTORY_CGI, (find_all == TRUE) ? "all" : url_encode(query_host_name)); printf("service=%s'>View <b>Alert History</b> For <b>This Service</b></a><br>\n", url_encode(query_svc_description)); #ifdef USE_TRENDS printf("<a href='%s?host=%s&", TRENDS_CGI, (find_all == TRUE) ? "all" : url_encode(query_host_name)); printf("service=%s'>View <b>Trends</b> For <b>This Service</b></a><br>\n", url_encode(query_svc_description)); #endif printf("<a href='%s?type=%d&host=%s&service=%s'>View <b>Information</b> For <b>This Service</b></a><br>\n", EXTINFO_CGI, DISPLAY_SERVICE_INFO, url_encode(query_host_name), url_encode(query_svc_description)); printf("<a href='%s?host=%s&service=%s&show_log_entries'>View <b>Availability Report</b> For <b>This Service</b></a><br>\n", AVAIL_CGI, url_encode(query_host_name), url_encode(query_svc_description)); printf("<a href='%s?host=%s'>View <b>Notifications</b> For <b>This Host</b></a><br>\n", NOTIFICATIONS_CGI, url_encode(query_host_name)); } else if (query_type == DISPLAY_HOSTGROUPS) { printf("<a href='%s?hostgroup=%s&style=hostdetail'>View <b>Host Status Detail</b> For <b>This Hostgroup</b></a><br>\n", STATUS_CGI, url_encode(query_hostgroup_name)); printf("<a href='%s?hostgroup=%s&style=detail'>View <b>Service Status Detail</b> For <b>This Hostgroup</b></a><br>\n", STATUS_CGI, url_encode(query_hostgroup_name)); printf("<a href='%s?hostgroup=%s'>View <b>Alert History</b> For <b>This Hostgroup</b></a><br>\n", HISTORY_CGI, url_encode(query_hostgroup_name)); } else if (query_type == DISPLAY_SERVICEGROUPS) { printf("<a href='%s?servicegroup=%s&style=hostdetail'>View <b>Host Status Detail</b> For <b>This Servicegroup</b></a><br>\n", STATUS_CGI, url_encode(query_servicegroup_name)); printf("<a href='%s?servicegroup=%s&style=detail'>View <b>Service Status Detail</b> For <b>This Servicegroup</b></a><br>\n", STATUS_CGI, url_encode(query_servicegroup_name)); printf("<a href='%s?servicegroup=%s'>View <b>Alert History</b> For <b>This Servicegroup</b></a><br>\n", HISTORY_CGI, url_encode(query_servicegroup_name)); } printf("</td></tr>\n"); printf("</table>\n"); } printf("</td>\n"); /* middle column of top row */ printf("<td align='center' valign='top' width='33%%'>\n"); printf("<div align='center' class='dataTitle'>\n"); if (query_type == DISPLAY_SERVICES) printf("Service '%s' on Host '%s'", query_svc_description, query_host_name); else if (query_type == DISPLAY_HOSTS) { if (find_all == TRUE) printf("All Hosts and Services"); else printf("Host '%s'", html_encode(query_host_name, TRUE)); } else if (query_type == DISPLAY_HOSTGROUPS) { printf("Host Group '%s'", html_encode(query_hostgroup_name, TRUE)); } else if (query_type == DISPLAY_SERVICEGROUPS) { printf("Service Group '%s'", html_encode(query_servicegroup_name, TRUE)); } else { if (find_all == TRUE) printf("All Contacts"); else printf("Contact '%s'", html_encode(query_contact_name, TRUE)); } printf("</div>\n"); printf("<br>\n"); display_nav_table(ts_start, ts_end); printf("</td>\n"); /* right hand column of top row */ printf("<td align='right' valign='top' width='33%%'>\n"); printf("<form method='GET' action='%s'>\n", NOTIFICATIONS_CGI); if (query_type == DISPLAY_SERVICES) { printf("<input type='hidden' name='host' value='%s'>\n", escape_string(query_host_name)); printf("<input type='hidden' name='service' value='%s'>\n", escape_string(query_svc_description)); } else if (query_type == DISPLAY_HOSTGROUPS) { printf("<input type='hidden' name='hostgroup' value='%s'>\n", escape_string(query_hostgroup_name)); } else if (query_type == DISPLAY_SERVICEGROUPS) { printf("<input type='hidden' name='servicegroup' value='%s'>\n", escape_string(query_servicegroup_name)); } else printf("<input type='hidden' name='%s' value='%s'>\n", (query_type == DISPLAY_HOSTS) ? "host" : "contact", (query_type == DISPLAY_HOSTS) ? escape_string(query_host_name) : escape_string(query_contact_name)); printf("<input type='hidden' name='ts_start' value='%lu'>\n", ts_start); printf("<input type='hidden' name='ts_end' value='%lu'>\n", ts_end); printf("<input type='hidden' name='limit' value='%d'>\n", result_limit); printf("<table border='0' class='optBox'>\n"); printf("<tr>\n"); if (query_type == DISPLAY_SERVICES) printf("<td align='left' colspan='2' class='optBoxItem'>Notification detail level for this service:</td>"); if (query_type == DISPLAY_HOSTGROUPS || query_type == DISPLAY_SERVICEGROUPS) printf("<td align='left' colspan='2' class='optBoxItem'>Notification detail level for this %sgroup:</td>", (query_type == DISPLAY_HOSTGROUPS) ? "host" : "service"); else printf("<td align='left' colspan='2' class='optBoxItem'>Notification detail level for %s %s%s:</td>", (find_all == TRUE) ? "all" : "this", (query_type == DISPLAY_HOSTS) ? "host" : "contact", (find_all == TRUE) ? "s" : ""); printf("</tr>\n"); printf("<tr><td></td>\n"); printf("<td align='left' class='optBoxItem'><select name='type'>\n"); printf("<option value='%d' %s>All notifications\n", NOTIFICATION_ALL, (notification_options == NOTIFICATION_ALL) ? "selected" : ""); if (query_type != DISPLAY_SERVICES) { printf("<option value='%d' %s>All service notifications\n", NOTIFICATION_SERVICE_ALL, (notification_options == NOTIFICATION_SERVICE_ALL) ? "selected" : ""); printf("<option value='%d' %s>All host notifications\n", NOTIFICATION_HOST_ALL, (notification_options == NOTIFICATION_HOST_ALL) ? "selected" : ""); } printf("<option value='%d' %s>Service custom\n", NOTIFICATION_SERVICE_CUSTOM, (notification_options == NOTIFICATION_SERVICE_CUSTOM) ? "selected" : ""); printf("<option value='%d' %s>Service acknowledgements\n", NOTIFICATION_SERVICE_ACK, (notification_options == NOTIFICATION_SERVICE_ACK) ? "selected" : ""); printf("<option value='%d' %s>Service warning\n", NOTIFICATION_SERVICE_WARNING, (notification_options == NOTIFICATION_SERVICE_WARNING) ? "selected" : ""); printf("<option value='%d' %s>Service unknown\n", NOTIFICATION_SERVICE_UNKNOWN, (notification_options == NOTIFICATION_SERVICE_UNKNOWN) ? "selected" : ""); printf("<option value='%d' %s>Service critical\n", NOTIFICATION_SERVICE_CRITICAL, (notification_options == NOTIFICATION_SERVICE_CRITICAL) ? "selected" : ""); printf("<option value='%d' %s>Service recovery\n", NOTIFICATION_SERVICE_RECOVERY, (notification_options == NOTIFICATION_SERVICE_RECOVERY) ? "selected" : ""); printf("<option value='%d' %s>Service flapping\n", NOTIFICATION_SERVICE_FLAP, (notification_options == NOTIFICATION_SERVICE_FLAP) ? "selected" : ""); if (query_type != DISPLAY_SERVICES) { printf("<option value='%d' %s>Host custom\n", NOTIFICATION_HOST_CUSTOM, (notification_options == NOTIFICATION_HOST_CUSTOM) ? "selected" : ""); printf("<option value='%d' %s>Host acknowledgements\n", NOTIFICATION_HOST_ACK, (notification_options == NOTIFICATION_HOST_ACK) ? "selected" : ""); printf("<option value='%d' %s>Host down\n", NOTIFICATION_HOST_DOWN, (notification_options == NOTIFICATION_HOST_DOWN) ? "selected" : ""); printf("<option value='%d' %s>Host unreachable\n", NOTIFICATION_HOST_UNREACHABLE, (notification_options == NOTIFICATION_HOST_UNREACHABLE) ? "selected" : ""); printf("<option value='%d' %s>Host recovery\n", NOTIFICATION_HOST_RECOVERY, (notification_options == NOTIFICATION_HOST_RECOVERY) ? "selected" : ""); printf("<option value='%d' %s>Host flapping\n", NOTIFICATION_HOST_FLAP, (notification_options == NOTIFICATION_HOST_FLAP) ? "selected" : ""); } printf("</select></td>\n"); printf("</tr>\n"); /* Order */ printf("<tr><td align='right'>Order:</td>"); printf("<td nowrap><input type='radio' name='order' value='new2old' %s> Newer Entries First | <input type='radio' name='order' value='old2new' %s> Older Entries First</td></tr>\n", (reverse == TRUE) ? "" : "checked", (reverse == TRUE) ? "checked" : ""); /* Timeperiod */ printf("<tr><td align='left'>Timeperiod:</td>"); printf("<td align='left'>\n"); printf("<select id='selecttp' name='timeperiod' onChange=\"var i=document.getElementById('selecttp').selectedIndex; if (document.getElementById('selecttp').options[i].value == 'custom') { $( '#custtime' ).toggle( 'blind', {}, 200 ); } else { $( '#custtime' ).toggle( 'blind', {}, 200 ); }\">\n"); printf("<option value='singleday' %s>Single Day\n", (timeperiod_type == TIMEPERIOD_SINGLE_DAY) ? "selected" : ""); printf("<option value='today' %s>Today\n", (timeperiod_type == TIMEPERIOD_TODAY) ? "selected" : ""); printf("<option value='last24hours' %s>Last 24 Hours\n", (timeperiod_type == TIMEPERIOD_LAST24HOURS) ? "selected" : ""); printf("<option value='thisweek' %s>This Week\n", (timeperiod_type == TIMEPERIOD_THISWEEK) ? "selected" : ""); printf("<option value='last7days' %s>Last 7 Days\n", (timeperiod_type == TIMEPERIOD_LAST7DAYS) ? "selected" : ""); printf("<option value='lastweek' %s>Last Week\n", (timeperiod_type == TIMEPERIOD_LASTWEEK) ? "selected" : ""); printf("<option value='thismonth' %s>This Month\n", (timeperiod_type == TIMEPERIOD_THISMONTH) ? "selected" : ""); printf("<option value='last31days' %s>Last 31 Days\n", (timeperiod_type == TIMEPERIOD_LAST31DAYS) ? "selected" : ""); printf("<option value='lastmonth' %s>Last Month\n", (timeperiod_type == TIMEPERIOD_LASTMONTH) ? "selected" : ""); printf("<option value='thisyear' %s>This Year\n", (timeperiod_type == TIMEPERIOD_THISYEAR) ? "selected" : ""); printf("<option value='lastyear' %s>Last Year\n", (timeperiod_type == TIMEPERIOD_LASTYEAR) ? "selected" : ""); printf("<option value='custom' %s>* CUSTOM PERIOD *\n", (timeperiod_type == TIMEPERIOD_CUSTOM) ? "selected" : ""); printf("</select>\n"); printf("<div id='custtime' style='display:%s;'>", (timeperiod_type == TIMEPERIOD_CUSTOM) ? "" : "none"); printf("<br><table border='0' cellspacing='0' cellpadding='0'>\n"); get_time_string(&ts_start, buffer, sizeof(buffer) - 1, SHORT_DATE_TIME); printf("<tr><td>Start: </td><td><input type='text' class='timepicker' name='start_time' value='%s' size='25'></td></tr>", buffer); get_time_string(&ts_end, buffer, sizeof(buffer) - 1, SHORT_DATE_TIME); printf("<tr><td>End: </td><td><input type='text' class='timepicker' name='end_time' value='%s' size='25'></td></tr></table></div>", buffer); printf("</td></tr>\n"); /* submit Button */ printf("<tr><td><input type='submit' value='Update'></td><td align='right'><input type='reset' value='Reset' onClick=\"window.location.href='%s?order=new2old&timeperiod=singleday&limit=%d'\"> </td></tr>\n", NOTIFICATIONS_CGI, result_limit); printf("</table>\n"); printf("</form>\n"); printf("</td>\n"); /* end of top table */ printf("</tr>\n"); printf("</table>\n"); } /* display notifications */ display_notifications(); /* display footer */ document_footer(CGI_ID); /* free allocated memory */ free_memory(); return OK; }
static void start_handler(void *data, const XML_Char *tagname, const XML_Char **attributes) { const char *title; const char *name; const char *filepath; const char *srcpath; const char *destpath; const char *datfile_foldername; struct system_info *sysinfo_array; int sys_count; char buf[512]; const char *sysname; char sysfilename[512]; char *datfile_path = NULL; char *s; FILE *datfile = NULL; FILE *sysfile = NULL; int lineno = 0; int i; int ul = FALSE; struct messdocs_state *state = (struct messdocs_state *) data; if (state->m_depth == 0) { /* help tag */ if (strcmp(tagname, "help")) { process_error(state, NULL, "Expected tag 'help'"); return; } title = find_attribute(attributes, "title"); if (title) state->m_title = pool_strdup_lib(state->m_pool, title); } else if (!strcmp(tagname, "topic")) { /* topic tag */ name = find_attribute(attributes, "text"); filepath = find_attribute(attributes, "filepath"); /* output TOC info */ fprintf(state->m_chm_toc, "\t<LI> <OBJECT type=\"text/sitemap\">\n"); fprintf(state->m_chm_toc, "\t\t<param name=\"Name\" value=\"%s\">\n", name); fprintf(state->m_chm_toc, "\t\t<param name=\"Local\" value=\"%s\">\n", filepath); fprintf(state->m_chm_toc, "\t\t</OBJECT>\n"); /* copy file */ copy_file_to_dest(state->m_dest_dir, state->m_toc_dir, filepath); if (!state->m_default_topic) state->m_default_topic = pool_strdup_lib(state->m_pool, filepath); } else if (!strcmp(tagname, "folder")) { /* folder tag */ name = find_attribute(attributes, "text"); fprintf(state->m_chm_toc, "\t<LI> <OBJECT type=\"text/sitemap\">\n"); fprintf(state->m_chm_toc, "\t\t<param name=\"Name\" value=\"%s\">\n", name); fprintf(state->m_chm_toc, "\t\t</OBJECT>\n"); fprintf(state->m_chm_toc, "\t\t<UL>\n"); } else if (!strcmp(tagname, "file")) { /* file tag */ filepath = find_attribute(attributes, "filepath"); copy_file_to_dest(state->m_dest_dir, state->m_toc_dir, filepath); } else if (!strcmp(tagname, "datfile")) { /* datfile tag */ srcpath = find_attribute(attributes, "srcpath"); destpath = find_attribute(attributes, "destpath"); datfile_foldername = find_attribute(attributes, "text"); datfile_path = make_path(state->m_toc_dir, srcpath); datfile = fopen(datfile_path, "r"); if (!datfile) { process_error(state, NULL, "Cannot open datfile '%s'\n", datfile_path); return; } snprintf(buf, ARRAY_LENGTH(buf), "%s", state->m_dest_dir); combine_path(buf, ARRAY_LENGTH(buf), destpath); osd_mkdir(buf); sysinfo_array = NULL; sys_count = 0; sysname = NULL; while(!feof(datfile)) { fgets(buf, ARRAY_LENGTH(buf), datfile); s = strchr(buf, '\n'); if (s) *s = '\0'; rtrim(buf); switch(buf[0]) { case '$': if (!strncmp(buf, "$info", 5)) { /* $info */ s = strchr(buf, '='); s = s ? s + 1 : &buf[strlen(buf)]; sysinfo_array = (system_info*)pool_realloc_lib(state->m_pool, sysinfo_array, sizeof(*sysinfo_array) * (sys_count + 1)); if (!sysinfo_array) goto outofmemory; sysinfo_array[sys_count].name = pool_strdup_lib(state->m_pool, s); sysinfo_array[sys_count].desc = NULL; sysname = sysinfo_array[sys_count].name; sys_count++; snprintf(sysfilename, sizeof(sysfilename), "%s%s%s%s%s.htm", state->m_dest_dir, PATH_SEPARATOR, destpath, PATH_SEPARATOR, s); if (sysfile) fclose(sysfile); sysfile = fopen(sysfilename, "w"); lineno = 0; } else if (!strncmp(buf, "$bio", 4)) { /* $bio */ } else if (!strncmp(buf, "$end", 4)) { /* $end */ } break; case '#': case '\0': /* comments */ break; default: /* normal line */ if (!sysfile) break; html_encode(buf, ARRAY_LENGTH(buf)); if (!strncmp(buf, "======", 6) && lineno == 0) { char *heading = (char*)malloc(strlen(buf) + 1); memset(heading, 0, strlen(buf) + 1); strncpy(heading, buf + 6, strlen(buf) - 12); fprintf(sysfile, "<h1>%s</h1>\n", heading); fprintf(sysfile, "<p><i>(directory: %s)</i></p>\n", sysname); if (!sysinfo_array[sys_count-1].desc) sysinfo_array[sys_count-1].desc = pool_strdup_lib(state->m_pool, heading); free(heading); } else if (buf[0] == '=') { int count; char *heading = (char*)malloc(strlen(buf) + 1); memset(heading, 0, strlen(buf) + 1); if (ul) { fprintf(sysfile, "</ul>"); ul = FALSE; } for (count = 0; buf[count] == '='; count++); strncpy(heading, buf + count, strlen(buf) - count*2); fprintf(sysfile, "<h%d>%s</h%d>\n", 7-count, heading, 7-count); free(heading); } else if (!strncmp(buf, " * ", 4)) { if (!ul) { fprintf(sysfile, "<ul>"); } ul = TRUE; fprintf(sysfile, "<li>%s</li>", buf + 4); } else { if (ul) { fprintf(sysfile, "</ul>"); ul = FALSE; } fprintf(sysfile, "%s\n", buf); } lineno++; break; } } /* now write out all toc */ qsort(sysinfo_array, sys_count, sizeof(*sysinfo_array), str_compare); fprintf(state->m_chm_toc, "\t<LI> <OBJECT type=\"text/sitemap\">\n"); fprintf(state->m_chm_toc, "\t\t<param name=\"Name\" value=\"%s\">\n", datfile_foldername); fprintf(state->m_chm_toc, "\t\t</OBJECT>\n"); fprintf(state->m_chm_toc, "\t\t<UL>\n"); for (i = 0; i < sys_count; i++) { fprintf(state->m_chm_toc, "\t<LI> <OBJECT type=\"text/sitemap\">\n"); fprintf(state->m_chm_toc, "\t\t<param name=\"Name\" value=\"%s\">\n", sysinfo_array[i].desc); fprintf(state->m_chm_toc, "\t\t<param name=\"Local\" value=\"%s%s%s.htm\">\n", destpath, PATH_SEPARATOR, sysinfo_array[i].name); fprintf(state->m_chm_toc, "\t\t</OBJECT>\n"); } fprintf(state->m_chm_toc, "\t\t</UL>\n"); } state->m_depth++; outofmemory: if (datfile_path) free(datfile_path); if (datfile) fclose(datfile); if (sysfile) fclose(sysfile); }