1 /**************************************************************************
2  *
3  * STATUSWML.C -  Nagios Status CGI for WAP-enabled devices
4  *
5  *
6  * License:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *************************************************************************/
21 
22 #include "../include/config.h"
23 #include "../include/common.h"
24 #include "../include/objects.h"
25 #include "../include/statusdata.h"
26 
27 #include "../include/cgiutils.h"
28 #include "../include/getcgi.h"
29 #include "../include/cgiauth.h"
30 
31 extern char main_config_file[MAX_FILENAME_LENGTH];
32 extern char *status_file;
33 
34 extern hoststatus *hoststatus_list;
35 extern servicestatus *servicestatus_list;
36 
37 extern int	use_ssl_authentication;
38 extern int      nagios_process_state;
39 
40 extern char     *ping_syntax;
41 
42 #define DISPLAY_HOST		        0
43 #define DISPLAY_SERVICE                 1
44 #define DISPLAY_HOSTGROUP               2
45 #define DISPLAY_INDEX                   3
46 #define DISPLAY_PING                    4
47 #define DISPLAY_TRACEROUTE              5
48 #define DISPLAY_QUICKSTATS              6
49 #define DISPLAY_PROCESS                 7
50 #define DISPLAY_ALL_PROBLEMS            8
51 #define DISPLAY_UNHANDLED_PROBLEMS      9
52 
53 #define DISPLAY_HOSTGROUP_SUMMARY       0
54 #define DISPLAY_HOSTGROUP_OVERVIEW      1
55 
56 #define DISPLAY_HOST_SUMMARY            0
57 #define DISPLAY_HOST_SERVICES           1
58 
59 void document_header(void);
60 void document_footer(void);
61 int process_cgivars(void);
62 int validate_arguments(void);
63 int is_valid_hostip(char *hostip);
64 
65 int display_type = DISPLAY_INDEX;
66 int hostgroup_style = DISPLAY_HOSTGROUP_SUMMARY;
67 int host_style = DISPLAY_HOST_SUMMARY;
68 
69 void display_index(void);
70 void display_host(void);
71 void display_host_services(void);
72 void display_service(void);
73 void display_hostgroup_summary(void);
74 void display_hostgroup_overview(void);
75 void display_ping(void);
76 void display_traceroute(void);
77 void display_quick_stats(void);
78 void display_process(void);
79 void display_problems(void);
80 
81 char *host_name = "";
82 char *hostgroup_name = "";
83 char *service_desc = "";
84 char *ping_address = "";
85 char *traceroute_address = "";
86 
87 int show_all_hostgroups = TRUE;
88 
89 
90 authdata current_authdata;
91 
92 
93 
main(void)94 int main(void) {
95 	int result = OK;
96 
97 	/* get the arguments passed in the URL */
98 	process_cgivars();
99 
100 	/* reset internal variables */
101 	reset_cgi_vars();
102 
103 	/* Initialize shared configuration variables */
104 	init_shared_cfg_vars(1);
105 
106 	document_header();
107 
108 	/* validate arguments in URL */
109 	result = validate_arguments();
110 	if(result == ERROR) {
111 		document_footer();
112 		return ERROR;
113 		}
114 
115 	/* read the CGI configuration file */
116 	result = read_cgi_config_file(get_cgi_config_location(), NULL);
117 	if(result == ERROR) {
118 		printf("<P>Error: Could not open CGI configuration file '%s' for reading!</P>\n", get_cgi_config_location());
119 		document_footer();
120 		return ERROR;
121 		}
122 
123 	/* read the main configuration file */
124 	result = read_main_config_file(main_config_file);
125 	if(result == ERROR) {
126 		printf("<P>Error: Could not open main configuration file '%s' for reading!</P>\n", main_config_file);
127 		document_footer();
128 		return ERROR;
129 		}
130 
131 	/* read all object configuration data */
132 	result = read_all_object_configuration_data(main_config_file, READ_ALL_OBJECT_DATA);
133 	if(result == ERROR) {
134 		printf("<P>Error: Could not read some or all object configuration data!</P>\n");
135 		document_footer();
136 		return ERROR;
137 		}
138 
139 	/* read all status data */
140 	result = read_all_status_data(status_file, READ_ALL_STATUS_DATA);
141 	if(result == ERROR) {
142 		printf("<P>Error: Could not read host and service status information!</P>\n");
143 		document_footer();
144 		free_memory();
145 		return ERROR;
146 		}
147 
148 	/* get authentication information */
149 	get_authentication_information(&current_authdata);
150 
151 	/* decide what to display to the user */
152 	if(display_type == DISPLAY_HOST && host_style == DISPLAY_HOST_SERVICES)
153 		display_host_services();
154 	else if(display_type == DISPLAY_HOST)
155 		display_host();
156 	else if(display_type == DISPLAY_SERVICE)
157 		display_service();
158 	else if(display_type == DISPLAY_HOSTGROUP && hostgroup_style == DISPLAY_HOSTGROUP_OVERVIEW)
159 		display_hostgroup_overview();
160 	else if(display_type == DISPLAY_HOSTGROUP && hostgroup_style == DISPLAY_HOSTGROUP_SUMMARY)
161 		display_hostgroup_summary();
162 	else if(display_type == DISPLAY_PING)
163 		display_ping();
164 	else if(display_type == DISPLAY_TRACEROUTE)
165 		display_traceroute();
166 	else if(display_type == DISPLAY_QUICKSTATS)
167 		display_quick_stats();
168 	else if(display_type == DISPLAY_PROCESS)
169 		display_process();
170 	else if(display_type == DISPLAY_ALL_PROBLEMS || display_type == DISPLAY_UNHANDLED_PROBLEMS)
171 		display_problems();
172 	else
173 		display_index();
174 
175 	document_footer();
176 
177 	/* free all allocated memory */
178 	free_memory();
179 
180 	return OK;
181 	}
182 
183 
document_header(void)184 void document_header(void) {
185 	char date_time[MAX_DATETIME_LENGTH];
186 	time_t expire_time;
187 	time_t current_time;
188 
189 	time(&current_time);
190 
191 	printf("Cache-Control: no-store\r\n");
192 	printf("Pragma: no-cache\r\n");
193 
194 	get_time_string(&current_time, date_time, (int)sizeof(date_time), HTTP_DATE_TIME);
195 	printf("Last-Modified: %s\r\n", date_time);
196 
197 	expire_time = (time_t)0L;
198 	get_time_string(&expire_time, date_time, (int)sizeof(date_time), HTTP_DATE_TIME);
199 	printf("Expires: %s\r\n", date_time);
200 
201 	printf("Content-type: text/vnd.wap.wml\r\n\r\n");
202 
203 	printf("<?xml version=\"1.0\"?>\n");
204 	printf("<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\" \"http://www.wapforum.org/DTD/wml_1.1.xml\">\n");
205 
206 	printf("<wml>\n");
207 
208 	printf("<head>\n");
209 	printf("<meta forua=\"true\" http-equiv=\"Cache-Control\" content=\"max-age=0\"/>\n");
210 	printf("</head>\n");
211 
212 	return;
213 	}
214 
215 
document_footer(void)216 void document_footer(void) {
217 
218 	printf("</wml>\n");
219 
220 	return;
221 	}
222 
223 
process_cgivars(void)224 int process_cgivars(void) {
225 	char **variables;
226 	int error = FALSE;
227 	int x;
228 
229 	variables = getcgivars();
230 
231 	for(x = 0; variables[x]; x++) {
232 
233 		/* do some basic length checking on the variable identifier to prevent buffer overflows */
234 		if(strlen(variables[x]) >= MAX_INPUT_BUFFER - 1) {
235 			continue;
236 			}
237 
238 		/* we found the hostgroup argument */
239 		else if(!strcmp(variables[x], "hostgroup")) {
240 			display_type = DISPLAY_HOSTGROUP;
241 			x++;
242 			if(variables[x] == NULL) {
243 				error = TRUE;
244 				break;
245 				}
246 
247 			if((hostgroup_name = (char *)strdup(variables[x])) == NULL)
248 				hostgroup_name = "";
249 			strip_html_brackets(hostgroup_name);
250 
251 			if(!strcmp(hostgroup_name, "all"))
252 				show_all_hostgroups = TRUE;
253 			else
254 				show_all_hostgroups = FALSE;
255 			}
256 
257 		/* we found the host argument */
258 		else if(!strcmp(variables[x], "host")) {
259 			display_type = DISPLAY_HOST;
260 			x++;
261 			if(variables[x] == NULL) {
262 				error = TRUE;
263 				break;
264 				}
265 
266 			if((host_name = (char *)strdup(variables[x])) == NULL)
267 				host_name = "";
268 			strip_html_brackets(host_name);
269 			}
270 
271 		/* we found the service argument */
272 		else if(!strcmp(variables[x], "service")) {
273 			display_type = DISPLAY_SERVICE;
274 			x++;
275 			if(variables[x] == NULL) {
276 				error = TRUE;
277 				break;
278 				}
279 
280 			if((service_desc = (char *)strdup(variables[x])) == NULL)
281 				service_desc = "";
282 			strip_html_brackets(service_desc);
283 			}
284 
285 
286 		/* we found the hostgroup style argument */
287 		else if(!strcmp(variables[x], "style")) {
288 			x++;
289 			if(variables[x] == NULL) {
290 				error = TRUE;
291 				break;
292 				}
293 
294 			if(!strcmp(variables[x], "overview"))
295 				hostgroup_style = DISPLAY_HOSTGROUP_OVERVIEW;
296 			else if(!strcmp(variables[x], "summary"))
297 				hostgroup_style = DISPLAY_HOSTGROUP_SUMMARY;
298 			else if(!strcmp(variables[x], "servicedetail"))
299 				host_style = DISPLAY_HOST_SERVICES;
300 			else if(!strcmp(variables[x], "processinfo"))
301 				display_type = DISPLAY_PROCESS;
302 			else if(!strcmp(variables[x], "aprobs"))
303 				display_type = DISPLAY_ALL_PROBLEMS;
304 			else if(!strcmp(variables[x], "uprobs"))
305 				display_type = DISPLAY_UNHANDLED_PROBLEMS;
306 			else
307 				display_type = DISPLAY_QUICKSTATS;
308 			}
309 
310 		/* we found the ping argument */
311 		else if(!strcmp(variables[x], "ping")) {
312 			display_type = DISPLAY_PING;
313 			x++;
314 			if(variables[x] == NULL) {
315 				error = TRUE;
316 				break;
317 				}
318 
319 			if((ping_address = (char *)strdup(variables[x])) == NULL)
320 				ping_address = "";
321 			strip_html_brackets(ping_address);
322 			}
323 
324 		/* we found the traceroute argument */
325 		else if(!strcmp(variables[x], "traceroute")) {
326 			display_type = DISPLAY_TRACEROUTE;
327 			x++;
328 			if(variables[x] == NULL) {
329 				error = TRUE;
330 				break;
331 				}
332 
333 			if((traceroute_address = (char *)strdup(variables[x])) == NULL)
334 				traceroute_address = "";
335 			strip_html_brackets(traceroute_address);
336 			}
337 
338 		}
339 
340 	/* free memory allocated to the CGI variables */
341 	free_cgivars(variables);
342 
343 	return error;
344 	}
345 
validate_arguments(void)346 int validate_arguments(void) {
347 	int result = OK;
348 	if((strcmp(ping_address, "")) && !is_valid_hostip(ping_address)) {
349 		printf("<p>Invalid host name/ip</p>\n");
350 		result = ERROR;
351 		}
352 	if(strcmp(traceroute_address, "") && !is_valid_hostip(traceroute_address)) {
353 		printf("<p>Invalid host name/ip</p>\n");
354 		result = ERROR;
355 		}
356 	return result;
357 	}
358 
is_valid_hostip(char * hostip)359 int is_valid_hostip(char *hostip) {
360 	char *valid_domain_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-";
361 	if(strcmp(hostip, "") && strlen(hostip) == strspn(hostip, valid_domain_chars) && hostip[0] != '-' && hostip[strlen(hostip) - 1] != '-')
362 		return TRUE;
363 	return FALSE;
364 	}
365 
366 /* main intro screen */
display_index(void)367 void display_index(void) {
368 
369 
370 	/**** MAIN MENU SCREEN (CARD 1) ****/
371 	printf("<card id='card1' title='Nagios WAP Interface'>\n");
372 	printf("<p align='center' mode='nowrap'>\n");
373 
374 	printf("<b>Nagios</b><br/><b>WAP Interface</b><br/>\n");
375 
376 	printf("<b><anchor title='Quick Stats'>Quick Stats<go href='%s'><postfield name='style' value='quickstats'/></go></anchor></b><br/>\n", STATUSWML_CGI);
377 
378 	printf("<b><anchor title='Status Summary'>Status Summary<go href='%s'><postfield name='hostgroup' value='all'/><postfield name='style' value='summary'/></go></anchor></b><br/>\n", STATUSWML_CGI);
379 
380 	printf("<b><anchor title='Status Overview'>Status Overview<go href='%s'><postfield name='hostgroup' value='all'/><postfield name='style' value='overview'/></go></anchor></b><br/>\n", STATUSWML_CGI);
381 
382 	printf("<b><anchor title='All Problems'>All Problems<go href='%s'><postfield name='style' value='aprobs'/></go></anchor></b><br/>\n", STATUSWML_CGI);
383 
384 	printf("<b><anchor title='Unhandled Problems'>Unhandled Problems<go href='%s'><postfield name='style' value='uprobs'/></go></anchor></b><br/>\n", STATUSWML_CGI);
385 
386 	printf("<b><anchor title='Process Info'>Process Info<go href='%s'><postfield name='style' value='processinfo'/></go></anchor></b><br/>\n", STATUSWML_CGI);
387 
388 	printf("<b><anchor title='Network Tools'>Tools<go href='#card2'/></anchor></b><br/>\n");
389 
390 	printf("<b><anchor title='About'>About<go href='#card3'/></anchor></b><br/>\n");
391 
392 	printf("</p>\n");
393 	printf("</card>\n");
394 
395 
396 	/**** TOOLS SCREEN (CARD 2) ****/
397 	printf("<card id='card2' title='Network Tools'>\n");
398 	printf("<p align='center' mode='nowrap'>\n");
399 
400 	printf("<b>Network Tools:</b><br/>\n");
401 
402 	printf("<b><anchor title='Ping Host'>Ping<go href='%s'><postfield name='ping' value=''/></go></anchor></b><br/>\n", STATUSWML_CGI);
403 	printf("<b><anchor title='Traceroute'>Traceroute<go href='%s'><postfield name='traceroute' value=''/></go></anchor></b><br/>\n", STATUSWML_CGI);
404 	printf("<b><anchor title='View Host'>View Host<go href='#card4'/></anchor></b><br/>\n");
405 	printf("<b><anchor title='View Hostgroup'>View Hostgroup<go href='#card5'/></anchor></b><br/>\n");
406 
407 	printf("</p>\n");
408 	printf("</card>\n");
409 
410 
411 	/**** ABOUT SCREEN (CARD 3) ****/
412 	printf("<card id='card3' title='About'>\n");
413 	printf("<p align='center' mode='nowrap'>\n");
414 	printf("<b>About</b><br/>\n");
415 	printf("</p>\n");
416 
417 	printf("<p align='center' mode='wrap'>\n");
418 	printf("<b>Nagios %s</b><br/><b>WAP Interface</b><br/>\n", PROGRAM_VERSION);
419 	printf("Copyright (C) 2001 Ethan Galstad<br/>\n");
420 	printf("egalstad@nagios.org<br/><br/>\n");
421 	printf("License: <b>GPL</b><br/><br/>\n");
422 	printf("Based in part on features found in AskAround's Wireless Network Tools<br/>\n");
423 	printf("<b>www.askaround.com</b><br/>\n");
424 	printf("</p>\n");
425 
426 	printf("</card>\n");
427 
428 
429 
430 	/**** VIEW HOST SCREEN (CARD 4) ****/
431 	printf("<card id='card4' title='View Host'>\n");
432 	printf("<p align='center' mode='nowrap'>\n");
433 	printf("<b>View Host</b><br/>\n");
434 	printf("</p>\n");
435 
436 	printf("<p align='center' mode='wrap'>\n");
437 	printf("<b>Host Name:</b><br/>\n");
438 	printf("<input name='hname'/>\n");
439 	printf("<do type='accept'>\n");
440 	printf("<go href='%s' method='post'><postfield name='host' value='$(hname)'/></go>\n", STATUSWML_CGI);
441 	printf("</do>\n");
442 	printf("</p>\n");
443 
444 	printf("</card>\n");
445 
446 
447 
448 	/**** VIEW HOSTGROUP SCREEN (CARD 5) ****/
449 	printf("<card id='card5' title='View Hostgroup'>\n");
450 	printf("<p align='center' mode='nowrap'>\n");
451 	printf("<b>View Hostgroup</b><br/>\n");
452 	printf("</p>\n");
453 
454 	printf("<p align='center' mode='wrap'>\n");
455 	printf("<b>Hostgroup Name:</b><br/>\n");
456 	printf("<input name='gname'/>\n");
457 	printf("<do type='accept'>\n");
458 	printf("<go href='%s' method='post'><postfield name='hostgroup' value='$(gname)'/><postfield name='style' value='overview'/></go>\n", STATUSWML_CGI);
459 	printf("</do>\n");
460 	printf("</p>\n");
461 
462 	printf("</card>\n");
463 
464 
465 	return;
466 	}
467 
468 
469 /* displays process info */
display_process(void)470 void display_process(void) {
471 
472 
473 	/**** MAIN SCREEN (CARD 1) ****/
474 	printf("<card id='card1' title='Process Info'>\n");
475 	printf("<p align='center' mode='nowrap'>\n");
476 	printf("<b>Process Info</b><br/><br/>\n");
477 
478 	/* check authorization */
479 	if(is_authorized_for_system_information(&current_authdata) == FALSE) {
480 
481 		printf("<b>Error: Not authorized for process info!</b>\n");
482 		printf("</p>\n");
483 		printf("</card>\n");
484 		return;
485 		}
486 
487 	if(nagios_process_state == STATE_OK)
488 		printf("Nagios process is running<br/>\n");
489 	else
490 		printf("<b>Nagios process may not be running</b><br/>\n");
491 
492 	if(enable_notifications == TRUE)
493 		printf("Notifications are enabled<br/>\n");
494 	else
495 		printf("<b>Notifications are disabled</b><br/>\n");
496 
497 	if(execute_service_checks == TRUE)
498 		printf("Check execution is enabled<br/>\n");
499 	else
500 		printf("<b>Check execution is disabled</b><br/>\n");
501 
502 	printf("<br/>\n");
503 	printf("<b><anchor title='Process Commands'>Process Commands<go href='#card2'/></anchor></b>\n");
504 	printf("</p>\n");
505 
506 	printf("</card>\n");
507 
508 
509 	/**** COMMANDS SCREEN (CARD 2) ****/
510 	printf("<card id='card2' title='Process Commands'>\n");
511 	printf("<p align='center' mode='nowrap'>\n");
512 	printf("<b>Process Commands</b><br/>\n");
513 
514 	if(enable_notifications == FALSE)
515 		printf("<b><anchor title='Enable Notifications'>Enable Notifications<go href='%s' method='post'><postfield name='cmd_typ' value='%d'/><postfield name='cmd_mod' value='%d'/><postfield name='content' value='wml'/></go></anchor></b><br/>\n", COMMAND_CGI, CMD_ENABLE_NOTIFICATIONS, CMDMODE_COMMIT);
516 	else
517 		printf("<b><anchor title='Disable Notifications'>Disable Notifications<go href='%s' method='post'><postfield name='cmd_typ' value='%d'/><postfield name='cmd_mod' value='%d'/><postfield name='content' value='wml'/></go></anchor></b><br/>\n", COMMAND_CGI, CMD_DISABLE_NOTIFICATIONS, CMDMODE_COMMIT);
518 
519 	if(execute_service_checks == FALSE)
520 		printf("<b><anchor title='Enable Check Execution'>Enable Check Execution<go href='%s' method='post'><postfield name='cmd_typ' value='%d'/><postfield name='cmd_mod' value='%d'/><postfield name='content' value='wml'/></go></anchor></b><br/>\n", COMMAND_CGI, CMD_START_EXECUTING_SVC_CHECKS, CMDMODE_COMMIT);
521 	else
522 		printf("<b><anchor title='Disable Check Execution'>Disable Check Execution<go href='%s' method='post'><postfield name='cmd_typ' value='%d'/><postfield name='cmd_mod' value='%d'/><postfield name='content' value='wml'/></go></anchor></b><br/>\n", COMMAND_CGI, CMD_STOP_EXECUTING_SVC_CHECKS, CMDMODE_COMMIT);
523 
524 	printf("</p>\n");
525 
526 	printf("</card>\n");
527 
528 
529 	return;
530 	}
531 
532 
533 
534 /* displays quick stats */
display_quick_stats(void)535 void display_quick_stats(void) {
536 	host *temp_host;
537 	hoststatus *temp_hoststatus;
538 	service *temp_service;
539 	servicestatus *temp_servicestatus;
540 	int hosts_unreachable = 0;
541 	int hosts_down = 0;
542 	int hosts_up = 0;
543 	int hosts_pending = 0;
544 	int services_critical = 0;
545 	int services_unknown = 0;
546 	int services_warning = 0;
547 	int services_ok = 0;
548 	int services_pending = 0;
549 
550 
551 	/**** MAIN SCREEN (CARD 1) ****/
552 	printf("<card id='card1' title='Quick Stats'>\n");
553 	printf("<p align='center' mode='nowrap'>\n");
554 	printf("<b>Quick Stats</b><br/>\n");
555 	printf("</p>\n");
556 
557 	/* check all hosts */
558 	for(temp_host = host_list; temp_host != NULL; temp_host = temp_host->next) {
559 
560 		if(is_authorized_for_host(temp_host, &current_authdata) == FALSE)
561 			continue;
562 
563 		temp_hoststatus = find_hoststatus(temp_host->name);
564 		if(temp_hoststatus == NULL)
565 			continue;
566 
567 		if(temp_hoststatus->status == SD_HOST_UNREACHABLE)
568 			hosts_unreachable++;
569 		else if(temp_hoststatus->status == SD_HOST_DOWN)
570 			hosts_down++;
571 		else if(temp_hoststatus->status == HOST_PENDING)
572 			hosts_pending++;
573 		else
574 			hosts_up++;
575 		}
576 
577 	/* check all services */
578 	for(temp_service = service_list; temp_service != NULL; temp_service = temp_service->next) {
579 
580 		if(is_authorized_for_service(temp_service, &current_authdata) == FALSE)
581 			continue;
582 
583 		temp_servicestatus = find_servicestatus(temp_service->host_name, temp_service->description);
584 		if(temp_servicestatus == NULL)
585 			continue;
586 
587 		if(temp_servicestatus->status == SERVICE_CRITICAL)
588 			services_critical++;
589 		else if(temp_servicestatus->status == SERVICE_UNKNOWN)
590 			services_unknown++;
591 		else if(temp_servicestatus->status == SERVICE_WARNING)
592 			services_warning++;
593 		else if(temp_servicestatus->status == SERVICE_PENDING)
594 			services_pending++;
595 		else
596 			services_ok++;
597 		}
598 
599 	printf("<p align='left' mode='nowrap'>\n");
600 
601 	printf("<b>Host Totals</b>:<br/>\n");
602 	printf("%d UP<br/>\n", hosts_up);
603 	printf("%d DOWN<br/>\n", hosts_down);
604 	printf("%d UNREACHABLE<br/>\n", hosts_unreachable);
605 	printf("%d PENDING<br/>\n", hosts_pending);
606 
607 	printf("<br/>\n");
608 
609 	printf("<b>Service Totals:</b><br/>\n");
610 	printf("%d OK<br/>\n", services_ok);
611 	printf("%d WARNING<br/>\n", services_warning);
612 	printf("%d UNKNOWN<br/>\n", services_unknown);
613 	printf("%d CRITICAL<br/>\n", services_critical);
614 	printf("%d PENDING<br/>\n", services_pending);
615 
616 	printf("</p>\n");
617 
618 	printf("</card>\n");
619 
620 	return;
621 	}
622 
623 
624 
625 /* displays hostgroup status overview */
display_hostgroup_overview(void)626 void display_hostgroup_overview(void) {
627 	hostgroup *temp_hostgroup;
628 	hostsmember *temp_member;
629 	host *temp_host;
630 	hoststatus *temp_hoststatus;
631 
632 
633 	/**** MAIN SCREEN (CARD 1) ****/
634 	printf("<card id='card1' title='Status Overview'>\n");
635 	printf("<p align='center' mode='nowrap'>\n");
636 
637 	printf("<b><anchor title='Status Overview'>Status Overview<go href='%s' method='post'><postfield name='hostgroup' value='%s'/><postfield name='style' value='summary'/></go></anchor></b><br/><br/>\n", STATUSWML_CGI, escape_string(hostgroup_name));
638 
639 	/* check all hostgroups */
640 	for(temp_hostgroup = hostgroup_list; temp_hostgroup != NULL; temp_hostgroup = temp_hostgroup->next) {
641 
642 		if(show_all_hostgroups == FALSE && strcmp(temp_hostgroup->group_name, hostgroup_name))
643 			continue;
644 
645 		if(is_authorized_for_hostgroup(temp_hostgroup, &current_authdata) == FALSE)
646 			continue;
647 
648 		printf("<b>%s</b>\n", temp_hostgroup->alias);
649 
650 		printf("<table columns='2' align='LL'>\n");
651 
652 		/* check all hosts in this hostgroup */
653 		for(temp_member = temp_hostgroup->members; temp_member != NULL; temp_member = temp_member->next) {
654 
655 			temp_host = find_host(temp_member->host_name);
656 			if(temp_host == NULL)
657 				continue;
658 
659 			if(is_host_member_of_hostgroup(temp_hostgroup, temp_host) == FALSE)
660 				continue;
661 
662 			temp_hoststatus = find_hoststatus(temp_host->name);
663 			if(temp_hoststatus == NULL)
664 				continue;
665 
666 			printf("<tr><td><anchor title='%s'>", temp_host->name);
667 			if(temp_hoststatus->status == SD_HOST_UP)
668 				printf("UP");
669 			else if(temp_hoststatus->status == HOST_PENDING)
670 				printf("PND");
671 			else if(temp_hoststatus->status == SD_HOST_DOWN)
672 				printf("DWN");
673 			else if(temp_hoststatus->status == SD_HOST_UNREACHABLE)
674 				printf("UNR");
675 			else
676 				printf("???");
677 			printf("<go href='%s' method='post'><postfield name='host' value='%s'/></go></anchor></td>", STATUSWML_CGI, temp_host->name);
678 			printf("<td>%s</td></tr>\n", temp_host->name);
679 			}
680 
681 		printf("</table>\n");
682 
683 		printf("<br/>\n");
684 		}
685 
686 	if(show_all_hostgroups == FALSE)
687 		printf("<b><anchor title='View All Hostgroups'>View All Hostgroups<go href='%s' method='post'><postfield name='hostgroup' value='all'/><postfield name='style' value='overview'/></go></anchor></b>\n", STATUSWML_CGI);
688 
689 	printf("</p>\n");
690 	printf("</card>\n");
691 
692 	return;
693 	}
694 
695 
696 /* displays hostgroup status summary */
display_hostgroup_summary(void)697 void display_hostgroup_summary(void) {
698 	hostgroup *temp_hostgroup;
699 	hostsmember *temp_member;
700 	host *temp_host;
701 	hoststatus *temp_hoststatus;
702 	service *temp_service;
703 	servicestatus *temp_servicestatus;
704 	int hosts_unreachable = 0;
705 	int hosts_down = 0;
706 	int hosts_up = 0;
707 	int hosts_pending = 0;
708 	int services_critical = 0;
709 	int services_unknown = 0;
710 	int services_warning = 0;
711 	int services_ok = 0;
712 	int services_pending = 0;
713 	int found = 0;
714 
715 
716 	/**** MAIN SCREEN (CARD 1) ****/
717 	printf("<card id='card1' title='Status Summary'>\n");
718 	printf("<p align='center' mode='nowrap'>\n");
719 
720 	printf("<b><anchor title='Status Summary'>Status Summary<go href='%s' method='post'><postfield name='hostgroup' value='%s'/><postfield name='style' value='overview'/></go></anchor></b><br/><br/>\n", STATUSWML_CGI, escape_string(hostgroup_name));
721 
722 	/* check all hostgroups */
723 	for(temp_hostgroup = hostgroup_list; temp_hostgroup != NULL; temp_hostgroup = temp_hostgroup->next) {
724 
725 		if(show_all_hostgroups == FALSE && strcmp(temp_hostgroup->group_name, hostgroup_name))
726 			continue;
727 
728 		if(is_authorized_for_hostgroup(temp_hostgroup, &current_authdata) == FALSE)
729 			continue;
730 
731 		printf("<b><anchor title='%s'>%s<go href='%s' method='post'><postfield name='hostgroup' value='%s'/><postfield name='style' value='overview'/></go></anchor></b>\n", temp_hostgroup->group_name, temp_hostgroup->alias, STATUSWML_CGI, temp_hostgroup->group_name);
732 
733 		printf("<table columns='2' align='LL'>\n");
734 
735 		hosts_up = 0;
736 		hosts_pending = 0;
737 		hosts_down = 0;
738 		hosts_unreachable = 0;
739 
740 		services_ok = 0;
741 		services_pending = 0;
742 		services_warning = 0;
743 		services_unknown = 0;
744 		services_critical = 0;
745 
746 		/* check all hosts in this hostgroup */
747 		for(temp_member = temp_hostgroup->members; temp_member != NULL; temp_member = temp_member->next) {
748 
749 			temp_host = find_host(temp_member->host_name);
750 			if(temp_host == NULL)
751 				continue;
752 
753 			if(is_host_member_of_hostgroup(temp_hostgroup, temp_host) == FALSE)
754 				continue;
755 
756 			temp_hoststatus = find_hoststatus(temp_host->name);
757 			if(temp_hoststatus == NULL)
758 				continue;
759 
760 			if(temp_hoststatus->status == SD_HOST_UNREACHABLE)
761 				hosts_unreachable++;
762 			else if(temp_hoststatus->status == SD_HOST_DOWN)
763 				hosts_down++;
764 			else if(temp_hoststatus->status == HOST_PENDING)
765 				hosts_pending++;
766 			else
767 				hosts_up++;
768 
769 			/* check all services on this host */
770 			for(temp_service = service_list; temp_service != NULL; temp_service = temp_service->next) {
771 
772 				if(strcmp(temp_service->host_name, temp_host->name))
773 					continue;
774 
775 				if(is_authorized_for_service(temp_service, &current_authdata) == FALSE)
776 					continue;
777 
778 				temp_servicestatus = find_servicestatus(temp_service->host_name, temp_service->description);
779 				if(temp_servicestatus == NULL)
780 					continue;
781 
782 				if(temp_servicestatus->status == SERVICE_CRITICAL)
783 					services_critical++;
784 				else if(temp_servicestatus->status == SERVICE_UNKNOWN)
785 					services_unknown++;
786 				else if(temp_servicestatus->status == SERVICE_WARNING)
787 					services_warning++;
788 				else if(temp_servicestatus->status == SERVICE_PENDING)
789 					services_pending++;
790 				else
791 					services_ok++;
792 				}
793 			}
794 
795 		printf("<tr><td>Hosts:</td><td>");
796 		found = 0;
797 		if(hosts_unreachable > 0) {
798 			printf("%d UNR", hosts_unreachable);
799 			found = 1;
800 			}
801 		if(hosts_down > 0) {
802 			printf("%s%d DWN", (found == 1) ? ", " : "", hosts_down);
803 			found = 1;
804 			}
805 		if(hosts_pending > 0) {
806 			printf("%s%d PND", (found == 1) ? ", " : "", hosts_pending);
807 			found = 1;
808 			}
809 		printf("%s%d UP", (found == 1) ? ", " : "", hosts_up);
810 		printf("</td></tr>\n");
811 		printf("<tr><td>Services:</td><td>");
812 		found = 0;
813 		if(services_critical > 0) {
814 			printf("%d CRI", services_critical);
815 			found = 1;
816 			}
817 		if(services_warning > 0) {
818 			printf("%s%d WRN", (found == 1) ? ", " : "", services_warning);
819 			found = 1;
820 			}
821 		if(services_unknown > 0) {
822 			printf("%s%d UNK", (found == 1) ? ", " : "", services_unknown);
823 			found = 1;
824 			}
825 		if(services_pending > 0) {
826 			printf("%s%d PND", (found == 1) ? ", " : "", services_pending);
827 			found = 1;
828 			}
829 		printf("%s%d OK", (found == 1) ? ", " : "", services_ok);
830 		printf("</td></tr>\n");
831 
832 		printf("</table>\n");
833 
834 		printf("<br/>\n");
835 		}
836 
837 	if(show_all_hostgroups == FALSE)
838 		printf("<b><anchor title='View All Hostgroups'>View All Hostgroups<go href='%s' method='post'><postfield name='hostgroup' value='all'/><postfield name='style' value='summary'/></go></anchor></b>\n", STATUSWML_CGI);
839 
840 	printf("</p>\n");
841 
842 	printf("</card>\n");
843 
844 	return;
845 	}
846 
847 
848 
849 /* displays host status */
display_host(void)850 void display_host(void) {
851 	host *temp_host;
852 	hoststatus *temp_hoststatus;
853 	char last_check[MAX_DATETIME_LENGTH];
854 	int days;
855 	int hours;
856 	int minutes;
857 	int seconds;
858 	time_t current_time;
859 	time_t t;
860 	char state_duration[48];
861 	int found;
862 
863 	/**** MAIN SCREEN (CARD 1) ****/
864 	printf("<card id='card1' title='Host Status'>\n");
865 	printf("<p align='center' mode='nowrap'>\n");
866 	printf("<b>Host '%s'</b><br/>\n", host_name);
867 
868 	/* find the host */
869 	temp_host = find_host(host_name);
870 	temp_hoststatus = find_hoststatus(host_name);
871 	if(temp_host == NULL || temp_hoststatus == NULL) {
872 
873 		printf("<b>Error: Could not find host!</b>\n");
874 		printf("</p>\n");
875 		printf("</card>\n");
876 		return;
877 		}
878 
879 	/* check authorization */
880 	if(is_authorized_for_host(temp_host, &current_authdata) == FALSE) {
881 
882 		printf("<b>Error: Not authorized for host!</b>\n");
883 		printf("</p>\n");
884 		printf("</card>\n");
885 		return;
886 		}
887 
888 
889 	printf("<table columns='2' align='LL'>\n");
890 
891 	printf("<tr><td>Status:</td><td>");
892 	if(temp_hoststatus->status == SD_HOST_UP)
893 		printf("UP");
894 	else if(temp_hoststatus->status == HOST_PENDING)
895 		printf("PENDING");
896 	else if(temp_hoststatus->status == SD_HOST_DOWN)
897 		printf("DOWN");
898 	else if(temp_hoststatus->status == SD_HOST_UNREACHABLE)
899 		printf("UNREACHABLE");
900 	else
901 		printf("?");
902 	printf("</td></tr>\n");
903 
904 	printf("<tr><td>Info:</td><td>%s</td></tr>\n", temp_hoststatus->plugin_output);
905 
906 	get_time_string(&temp_hoststatus->last_check, last_check, sizeof(last_check) - 1, SHORT_DATE_TIME);
907 	printf("<tr><td>Last Check:</td><td>%s</td></tr>\n", last_check);
908 
909 	current_time = time(NULL);
910 	if(temp_hoststatus->last_state_change == (time_t)0)
911 		t = current_time - program_start;
912 	else
913 		t = current_time - temp_hoststatus->last_state_change;
914 	get_time_breakdown((unsigned long)t, &days, &hours, &minutes, &seconds);
915 	snprintf(state_duration, sizeof(state_duration) - 1, "%2dd %2dh %2dm %2ds%s", days, hours, minutes, seconds, (temp_hoststatus->last_state_change == (time_t)0) ? "+" : "");
916 	printf("<tr><td>Duration:</td><td>%s</td></tr>\n", state_duration);
917 
918 	printf("<tr><td>Properties:</td><td>");
919 	found = 0;
920 	if(temp_hoststatus->checks_enabled == FALSE) {
921 		printf("%sChecks disabled", (found == 1) ? ", " : "");
922 		found = 1;
923 		}
924 	if(temp_hoststatus->notifications_enabled == FALSE) {
925 		printf("%sNotifications disabled", (found == 1) ? ", " : "");
926 		found = 1;
927 		}
928 	if(temp_hoststatus->problem_has_been_acknowledged == TRUE) {
929 		printf("%sProblem acknowledged", (found == 1) ? ", " : "");
930 		found = 1;
931 		}
932 	if(temp_hoststatus->scheduled_downtime_depth > 0) {
933 		printf("%sIn scheduled downtime", (found == 1) ? ", " : "");
934 		found = 1;
935 		}
936 	if(found == 0)
937 		printf("N/A");
938 	printf("</td></tr>\n");
939 
940 	printf("</table>\n");
941 	printf("<br/>\n");
942 	printf("<b><anchor title='View Services'>View Services<go href='%s' method='post'><postfield name='host' value='%s'/><postfield name='style' value='servicedetail'/></go></anchor></b>\n", STATUSWML_CGI, escape_string(host_name));
943 	printf("<b><anchor title='Host Commands'>Host Commands<go href='#card2'/></anchor></b>\n");
944 	printf("</p>\n");
945 
946 	printf("</card>\n");
947 
948 
949 	/**** COMMANDS SCREEN (CARD 2) ****/
950 	printf("<card id='card2' title='Host Commands'>\n");
951 	printf("<p align='center' mode='nowrap'>\n");
952 	printf("<b>Host Commands</b><br/>\n");
953 
954 	printf("<b><anchor title='Ping Host'>Ping Host<go href='%s' method='post'><postfield name='ping' value='%s'/></go></anchor></b>\n", STATUSWML_CGI, temp_host->address);
955 	printf("<b><anchor title='Traceroute'>Traceroute<go href='%s' method='post'><postfield name='traceroute' value='%s'/></go></anchor></b>\n", STATUSWML_CGI, temp_host->address);
956 
957 	if(temp_hoststatus->status != SD_HOST_UP && temp_hoststatus->status != HOST_PENDING)
958 		printf("<b><anchor title='Acknowledge Problem'>Acknowledge Problem<go href='#card3'/></anchor></b>\n");
959 
960 	if(temp_hoststatus->checks_enabled == FALSE)
961 		printf("<b><anchor title='Enable Host Checks'>Enable Host Checks<go href='%s' method='post'><postfield name='host' value='%s'/><postfield name='cmd_typ' value='%d'/><postfield name='cmd_mod' value='%d'/><postfield name='content' value='wml'/></go></anchor></b><br/>\n", COMMAND_CGI, escape_string(host_name), CMD_ENABLE_HOST_CHECK, CMDMODE_COMMIT);
962 	else
963 		printf("<b><anchor title='Disable Host Checks'>Disable Host Checks<go href='%s' method='post'><postfield name='host' value='%s'/><postfield name='cmd_typ' value='%d'/><postfield name='cmd_mod' value='%d'/><postfield name='content' value='wml'/></go></anchor></b><br/>\n", COMMAND_CGI, escape_string(host_name), CMD_DISABLE_HOST_CHECK, CMDMODE_COMMIT);
964 
965 	if(temp_hoststatus->notifications_enabled == FALSE)
966 		printf("<b><anchor title='Enable Host Notifications'>Enable Host Notifications<go href='%s' method='post'><postfield name='host' value='%s'/><postfield name='cmd_typ' value='%d'/><postfield name='cmd_mod' value='%d'/><postfield name='content' value='wml'/></go></anchor></b><br/>\n", COMMAND_CGI, escape_string(host_name), CMD_ENABLE_HOST_NOTIFICATIONS, CMDMODE_COMMIT);
967 	else
968 		printf("<b><anchor title='Disable Host Notifications'>Disable Host Notifications<go href='%s' method='post'><postfield name='host' value='%s'/><postfield name='cmd_typ' value='%d'/><postfield name='cmd_mod' value='%d'/><postfield name='content' value='wml'/></go></anchor></b><br/>\n", COMMAND_CGI, escape_string(host_name), CMD_DISABLE_HOST_NOTIFICATIONS, CMDMODE_COMMIT);
969 
970 
971 	printf("<b><anchor title='Enable All Service Checks'>Enable All Service Checks<go href='%s' method='post'><postfield name='host' value='%s'/><postfield name='cmd_typ' value='%d'/><postfield name='cmd_mod' value='%d'/><postfield name='content' value='wml'/></go></anchor></b><br/>\n", COMMAND_CGI, escape_string(host_name), CMD_ENABLE_HOST_SVC_CHECKS, CMDMODE_COMMIT);
972 
973 	printf("<b><anchor title='Disable All Service Checks'>Disable All Service Checks<go href='%s' method='post'><postfield name='host' value='%s'/><postfield name='cmd_typ' value='%d'/><postfield name='cmd_mod' value='%d'/><postfield name='content' value='wml'/></go></anchor></b><br/>\n", COMMAND_CGI, escape_string(host_name), CMD_DISABLE_HOST_SVC_CHECKS, CMDMODE_COMMIT);
974 
975 	printf("<b><anchor title='Enable All Service Notifications'>Enable All Service Notifications<go href='%s' method='post'><postfield name='host' value='%s'/><postfield name='cmd_typ' value='%d'/><postfield name='cmd_mod' value='%d'/><postfield name='content' value='wml'/></go></anchor></b><br/>\n", COMMAND_CGI, escape_string(host_name), CMD_ENABLE_HOST_SVC_NOTIFICATIONS, CMDMODE_COMMIT);
976 
977 	printf("<b><anchor title='Disable All Service Notifications'>Disable All Service Notifications<go href='%s' method='post'><postfield name='host' value='%s'/><postfield name='cmd_typ' value='%d'/><postfield name='cmd_mod' value='%d'/><postfield name='content' value='wml'/></go></anchor></b><br/>\n", COMMAND_CGI, escape_string(host_name), CMD_DISABLE_HOST_SVC_NOTIFICATIONS, CMDMODE_COMMIT);
978 
979 	printf("</p>\n");
980 
981 	printf("</card>\n");
982 
983 
984 	/**** ACKNOWLEDGEMENT SCREEN (CARD 3) ****/
985 	printf("<card id='card3' title='Acknowledge Problem'>\n");
986 	printf("<p align='center' mode='nowrap'>\n");
987 	printf("<b>Acknowledge Problem</b><br/>\n");
988 	printf("</p>\n");
989 
990 	printf("<p align='center' mode='wrap'>\n");
991 	printf("<b>Your Name:</b><br/>\n");
992 	printf("<input name='name' value='%s' /><br/>\n", ((use_ssl_authentication) ? (getenv("SSL_CLIENT_S_DN_CN")) : (getenv("REMOTE_USER"))));
993 	printf("<b>Comment:</b><br/>\n");
994 	printf("<input name='comment' value='acknowledged by WAP'/>\n");
995 
996 	printf("<do type='accept'>\n");
997 	printf("<go href='%s' method='post'><postfield name='host' value='%s'/><postfield name='com_author' value='$(name)'/><postfield name='com_data' value='$(comment)'/><postfield name='persistent' value=''/><postfield name='send_notification' value=''/><postfield name='cmd_typ' value='%d'/><postfield name='cmd_mod' value='%d'/><postfield name='content' value='wml'/></go>\n", COMMAND_CGI, escape_string(host_name), CMD_ACKNOWLEDGE_HOST_PROBLEM, CMDMODE_COMMIT);
998 	printf("</do>\n");
999 
1000 	printf("</p>\n");
1001 
1002 	printf("</card>\n");
1003 
1004 	return;
1005 	}
1006 
1007 
1008 
1009 /* displays services on a host */
display_host_services(void)1010 void display_host_services(void) {
1011 	service *temp_service;
1012 	servicestatus *temp_servicestatus;
1013 
1014 	/**** MAIN SCREEN (CARD 1) ****/
1015 	printf("<card id='card1' title='Host Services'>\n");
1016 	printf("<p align='center' mode='nowrap'>\n");
1017 	printf("<b>Host <anchor title='%s'>", url_encode(host_name));
1018 	printf("'%s'<go href='%s' method='post'><postfield name='host' value='%s'/></go></anchor> Services</b><br/>\n", host_name, STATUSWML_CGI, escape_string(host_name));
1019 
1020 	printf("<table columns='2' align='LL'>\n");
1021 
1022 	/* check all services */
1023 	for(temp_service = service_list; temp_service != NULL; temp_service = temp_service->next) {
1024 
1025 		if(strcmp(temp_service->host_name, host_name))
1026 			continue;
1027 
1028 		if(is_authorized_for_service(temp_service, &current_authdata) == FALSE)
1029 			continue;
1030 
1031 		temp_servicestatus = find_servicestatus(temp_service->host_name, temp_service->description);
1032 		if(temp_servicestatus == NULL)
1033 			continue;
1034 
1035 		printf("<tr><td><anchor title='%s'>", temp_service->description);
1036 		if(temp_servicestatus->status == SERVICE_OK)
1037 			printf("OK");
1038 		else if(temp_servicestatus->status == SERVICE_PENDING)
1039 			printf("PND");
1040 		else if(temp_servicestatus->status == SERVICE_WARNING)
1041 			printf("WRN");
1042 		else if(temp_servicestatus->status == SERVICE_UNKNOWN)
1043 			printf("UNK");
1044 		else if(temp_servicestatus->status == SERVICE_CRITICAL)
1045 			printf("CRI");
1046 		else
1047 			printf("???");
1048 
1049 		printf("<go href='%s' method='post'><postfield name='host' value='%s'/><postfield name='service' value='%s'/></go></anchor></td>", STATUSWML_CGI, temp_service->host_name, temp_service->description);
1050 		printf("<td>%s</td></tr>\n", temp_service->description);
1051 		}
1052 
1053 	printf("</table>\n");
1054 
1055 	printf("</p>\n");
1056 
1057 	printf("</card>\n");
1058 
1059 	return;
1060 	}
1061 
1062 
1063 
1064 /* displays service status */
display_service(void)1065 void display_service(void) {
1066 	service *temp_service;
1067 	servicestatus *temp_servicestatus;
1068 	char last_check[MAX_DATETIME_LENGTH];
1069 	int days;
1070 	int hours;
1071 	int minutes;
1072 	int seconds;
1073 	time_t current_time;
1074 	time_t t;
1075 	char state_duration[48];
1076 	int found;
1077 
1078 	/**** MAIN SCREEN (CARD 1) ****/
1079 	printf("<card id='card1' title='Service Status'>\n");
1080 	printf("<p align='center' mode='nowrap'>\n");
1081 	printf("<b>Service '%s' on host '%s'</b><br/>\n", service_desc, host_name);
1082 
1083 	/* find the service */
1084 	temp_service = find_service(host_name, service_desc);
1085 	temp_servicestatus = find_servicestatus(host_name, service_desc);
1086 	if(temp_service == NULL || temp_servicestatus == NULL) {
1087 
1088 		printf("<b>Error: Could not find service!</b>\n");
1089 		printf("</p>\n");
1090 		printf("</card>\n");
1091 		return;
1092 		}
1093 
1094 	/* check authorization */
1095 	if(is_authorized_for_service(temp_service, &current_authdata) == FALSE) {
1096 
1097 		printf("<b>Error: Not authorized for service!</b>\n");
1098 		printf("</p>\n");
1099 		printf("</card>\n");
1100 		return;
1101 		}
1102 
1103 
1104 	printf("<table columns='2' align='LL'>\n");
1105 
1106 	printf("<tr><td>Status:</td><td>");
1107 	if(temp_servicestatus->status == SERVICE_OK)
1108 		printf("OK");
1109 	else if(temp_servicestatus->status == SERVICE_PENDING)
1110 		printf("PENDING");
1111 	else if(temp_servicestatus->status == SERVICE_WARNING)
1112 		printf("WARNING");
1113 	else if(temp_servicestatus->status == SERVICE_UNKNOWN)
1114 		printf("UNKNOWN");
1115 	else if(temp_servicestatus->status == SERVICE_CRITICAL)
1116 		printf("CRITICAL");
1117 	else
1118 		printf("?");
1119 	printf("</td></tr>\n");
1120 
1121 	printf("<tr><td>Info:</td><td>%s</td></tr>\n", temp_servicestatus->plugin_output);
1122 
1123 	get_time_string(&temp_servicestatus->last_check, last_check, sizeof(last_check) - 1, SHORT_DATE_TIME);
1124 	printf("<tr><td>Last Check:</td><td>%s</td></tr>\n", last_check);
1125 
1126 	current_time = time(NULL);
1127 	if(temp_servicestatus->last_state_change == (time_t)0)
1128 		t = current_time - program_start;
1129 	else
1130 		t = current_time - temp_servicestatus->last_state_change;
1131 	get_time_breakdown((unsigned long)t, &days, &hours, &minutes, &seconds);
1132 	snprintf(state_duration, sizeof(state_duration) - 1, "%2dd %2dh %2dm %2ds%s", days, hours, minutes, seconds, (temp_servicestatus->last_state_change == (time_t)0) ? "+" : "");
1133 	printf("<tr><td>Duration:</td><td>%s</td></tr>\n", state_duration);
1134 
1135 	printf("<tr><td>Properties:</td><td>");
1136 	found = 0;
1137 	if(temp_servicestatus->checks_enabled == FALSE) {
1138 		printf("%sChecks disabled", (found == 1) ? ", " : "");
1139 		found = 1;
1140 		}
1141 	if(temp_servicestatus->notifications_enabled == FALSE) {
1142 		printf("%sNotifications disabled", (found == 1) ? ", " : "");
1143 		found = 1;
1144 		}
1145 	if(temp_servicestatus->problem_has_been_acknowledged == TRUE) {
1146 		printf("%sProblem acknowledged", (found == 1) ? ", " : "");
1147 		found = 1;
1148 		}
1149 	if(temp_servicestatus->scheduled_downtime_depth > 0) {
1150 		printf("%sIn scheduled downtime", (found == 1) ? ", " : "");
1151 		found = 1;
1152 		}
1153 	if(found == 0)
1154 		printf("N/A");
1155 	printf("</td></tr>\n");
1156 
1157 	printf("</table>\n");
1158 	printf("<br/>\n");
1159 	printf("<b><anchor title='View Host'>View Host<go href='%s' method='post'><postfield name='host' value='%s'/></go></anchor></b>\n", STATUSWML_CGI, escape_string(host_name));
1160 	printf("<b><anchor title='Service Commands'>Svc. Commands<go href='#card2'/></anchor></b>\n");
1161 	printf("</p>\n");
1162 
1163 	printf("</card>\n");
1164 
1165 
1166 	/**** COMMANDS SCREEN (CARD 2) ****/
1167 	printf("<card id='card2' title='Service Commands'>\n");
1168 	printf("<p align='center' mode='nowrap'>\n");
1169 	printf("<b>Service Commands</b><br/>\n");
1170 
1171 	if(temp_servicestatus->status != SERVICE_OK && temp_servicestatus->status != SERVICE_PENDING)
1172 		printf("<b><anchor title='Acknowledge Problem'>Acknowledge Problem<go href='#card3'/></anchor></b>\n");
1173 
1174 	if(temp_servicestatus->checks_enabled == FALSE) {
1175 		printf("<b><anchor title='Enable Checks'>Enable Checks<go href='%s' method='post'><postfield name='host' value='%s'/>", COMMAND_CGI, escape_string(host_name));
1176 		printf("<postfield name='service' value='%s'/><postfield name='cmd_typ' value='%d'/><postfield name='cmd_mod' value='%d'/><postfield name='content' value='wml'/></go></anchor></b><br/>\n", escape_string(service_desc), CMD_ENABLE_SVC_CHECK, CMDMODE_COMMIT);
1177 		}
1178 	else {
1179 		printf("<b><anchor title='Disable Checks'>Disable Checks<go href='%s' method='post'><postfield name='host' value='%s'/>", COMMAND_CGI, escape_string(host_name));
1180 		printf("<postfield name='service' value='%s'/><postfield name='cmd_typ' value='%d'/><postfield name='cmd_mod' value='%d'/><postfield name='content' value='wml'/></go></anchor></b><br/>\n", escape_string(service_desc), CMD_DISABLE_SVC_CHECK, CMDMODE_COMMIT);
1181 
1182 		printf("<b><anchor title='Schedule Immediate Check'>Schedule Immediate Check<go href='%s' method='post'><postfield name='host' value='%s'/>", COMMAND_CGI, escape_string(host_name));
1183 		printf("<postfield name='service' value='%s'/><postfield name='start_time' value='%llu'/><postfield name='cmd_typ' value='%d'/><postfield name='cmd_mod' value='%d'/><postfield name='content' value='wml'/></go></anchor></b><br/>\n", escape_string(service_desc), (unsigned long long)current_time, CMD_SCHEDULE_SVC_CHECK, CMDMODE_COMMIT);
1184 		}
1185 
1186 	if(temp_servicestatus->notifications_enabled == FALSE) {
1187 		printf("<b><anchor title='Enable Notifications'>Enable Notifications<go href='%s' method='post'><postfield name='host' value='%s'/>", COMMAND_CGI, escape_string(host_name));
1188 		printf("<postfield name='service' value='%s'/><postfield name='cmd_typ' value='%d'/><postfield name='cmd_mod' value='%d'/><postfield name='content' value='wml'/></go></anchor></b><br/>\n", escape_string(service_desc), CMD_ENABLE_SVC_NOTIFICATIONS, CMDMODE_COMMIT);
1189 		}
1190 	else {
1191 		printf("<b><anchor title='Disable Notifications'>Disable Notifications<go href='%s' method='post'><postfield name='host' value='%s'/>", COMMAND_CGI, escape_string(host_name));
1192 		printf("<postfield name='service' value='%s'/><postfield name='cmd_typ' value='%d'/><postfield name='cmd_mod' value='%d'/><postfield name='content' value='wml'/></go></anchor></b><br/>\n", escape_string(service_desc), CMD_DISABLE_SVC_NOTIFICATIONS, CMDMODE_COMMIT);
1193 		}
1194 
1195 	printf("</p>\n");
1196 
1197 	printf("</card>\n");
1198 
1199 
1200 	/**** ACKNOWLEDGEMENT SCREEN (CARD 3) ****/
1201 	printf("<card id='card3' title='Acknowledge Problem'>\n");
1202 	printf("<p align='center' mode='nowrap'>\n");
1203 	printf("<b>Acknowledge Problem</b><br/>\n");
1204 	printf("</p>\n");
1205 
1206 	printf("<p align='center' mode='wrap'>\n");
1207 	printf("<b>Your Name:</b><br/>\n");
1208 	printf("<input name='name' value='%s' /><br/>\n", ((use_ssl_authentication) ? (getenv("SSL_CLIENT_S_DN_CN")) : (getenv("REMOTE_USER"))));
1209 	printf("<b>Comment:</b><br/>\n");
1210 	printf("<input name='comment' value='acknowledged by WAP'/>\n");
1211 
1212 	printf("<do type='accept'>\n");
1213 	printf("<go href='%s' method='post'><postfield name='host' value='%s'/>", COMMAND_CGI, escape_string(host_name));
1214 	printf("<postfield name='service' value='%s'/><postfield name='com_author' value='$(name)'/><postfield name='com_data' value='$(comment)'/><postfield name='persistent' value=''/><postfield name='send_notification' value=''/><postfield name='cmd_typ' value='%d'/><postfield name='cmd_mod' value='%d'/><postfield name='content' value='wml'/></go>\n", escape_string(service_desc), CMD_ACKNOWLEDGE_SVC_PROBLEM, CMDMODE_COMMIT);
1215 	printf("</do>\n");
1216 
1217 	printf("</p>\n");
1218 
1219 	printf("</card>\n");
1220 
1221 	return;
1222 	}
1223 
1224 
1225 /* displays ping results */
display_ping(void)1226 void display_ping(void) {
1227 	char input_buffer[MAX_INPUT_BUFFER];
1228 	char buffer[MAX_INPUT_BUFFER];
1229 	char *temp_ptr;
1230 	FILE *fp;
1231 	int odd = 0;
1232 	int in_macro = FALSE;
1233 
1234 	/**** MAIN SCREEN (CARD 1) ****/
1235 	printf("<card id='card1' title='Ping'>\n");
1236 
1237 	if(!strcmp(ping_address, "")) {
1238 
1239 		printf("<p align='center' mode='nowrap'>\n");
1240 		printf("<b>Ping Host</b><br/>\n");
1241 		printf("</p>\n");
1242 
1243 		printf("<p align='center' mode='wrap'>\n");
1244 		printf("<b>Host Name/Address:</b><br/>\n");
1245 		printf("<input name='address'/>\n");
1246 		printf("<do type='accept'>\n");
1247 		printf("<go href='%s'><postfield name='ping' value='$(address)'/></go>\n", STATUSWML_CGI);
1248 		printf("</do>\n");
1249 		printf("</p>\n");
1250 		}
1251 
1252 	else {
1253 
1254 		printf("<p align='center' mode='nowrap'>\n");
1255 		printf("<b>Results For Ping Of %s:</b><br/>\n", ping_address);
1256 		printf("</p>\n");
1257 
1258 		printf("<p mode='nowrap'>\n");
1259 
1260 		if(ping_syntax == NULL)
1261 			printf("ping_syntax in CGI config file is NULL!\n");
1262 
1263 		else {
1264 
1265 			/* process macros in the ping syntax */
1266 			strcpy(buffer, "");
1267 			strncpy(input_buffer, ping_syntax, sizeof(input_buffer) - 1);
1268 			input_buffer[strlen(ping_syntax) - 1] = '\x0';
1269 			for(temp_ptr = my_strtok(input_buffer, "$"); temp_ptr != NULL; temp_ptr = my_strtok(NULL, "$")) {
1270 
1271 				if(in_macro == FALSE) {
1272 					if(strlen(buffer) + strlen(temp_ptr) < sizeof(buffer) - 1) {
1273 						strncat(buffer, temp_ptr, sizeof(buffer) - strlen(buffer) - 1);
1274 						buffer[sizeof(buffer) - 1] = '\x0';
1275 						}
1276 					in_macro = TRUE;
1277 					}
1278 				else {
1279 
1280 					if(strlen(buffer) + strlen(temp_ptr) < sizeof(buffer) - 1) {
1281 
1282 						if(!strcmp(temp_ptr, "HOSTADDRESS"))
1283 							strncat(buffer, ping_address, sizeof(buffer) - strlen(buffer) - 1);
1284 						}
1285 
1286 					in_macro = FALSE;
1287 					}
1288 				}
1289 
1290 			/* run the ping command */
1291 			fp = popen(buffer, "r");
1292 			if(fp) {
1293 				while(1) {
1294 					fgets(buffer, sizeof(buffer) - 1, fp);
1295 					if(feof(fp))
1296 						break;
1297 
1298 					strip(buffer);
1299 
1300 					if(odd) {
1301 						odd = 0;
1302 						printf("%s<br/>\n", buffer);
1303 						}
1304 					else {
1305 						odd = 1;
1306 						printf("<b>%s</b><br/>\n", buffer);
1307 						}
1308 					}
1309 				}
1310 			else
1311 				printf("Error executing ping!\n");
1312 
1313 			pclose(fp);
1314 			}
1315 
1316 		printf("</p>\n");
1317 		}
1318 
1319 	printf("</card>\n");
1320 
1321 	return;
1322 	}
1323 
1324 
1325 /* displays traceroute results */
display_traceroute(void)1326 void display_traceroute(void) {
1327 	char buffer[MAX_INPUT_BUFFER];
1328 	FILE *fp;
1329 	int odd = 0;
1330 
1331 	/**** MAIN SCREEN (CARD 1) ****/
1332 	printf("<card id='card1' title='Traceroute'>\n");
1333 
1334 	if(!strcmp(traceroute_address, "")) {
1335 
1336 		printf("<p align='center' mode='nowrap'>\n");
1337 		printf("<b>Traceroute</b><br/>\n");
1338 		printf("</p>\n");
1339 
1340 		printf("<p align='center' mode='wrap'>\n");
1341 		printf("<b>Host Name/Address:</b><br/>\n");
1342 		printf("<input name='address'/>\n");
1343 		printf("<do type='accept'>\n");
1344 		printf("<go href='%s'><postfield name='traceroute' value='$(address)'/></go>\n", STATUSWML_CGI);
1345 		printf("</do>\n");
1346 		printf("</p>\n");
1347 		}
1348 
1349 	else {
1350 
1351 		printf("<p align='center' mode='nowrap'>\n");
1352 		printf("<b>Results For Traceroute To %s:</b><br/>\n", traceroute_address);
1353 		printf("</p>\n");
1354 
1355 		printf("<p mode='nowrap'>\n");
1356 
1357 		snprintf(buffer, sizeof(buffer) - 1, "%s %s", TRACEROUTE_COMMAND, traceroute_address);
1358 		buffer[sizeof(buffer) - 1] = '\x0';
1359 
1360 		fp = popen(buffer, "r");
1361 		if(fp) {
1362 			while(1) {
1363 				fgets(buffer, sizeof(buffer) - 1, fp);
1364 				if(feof(fp))
1365 					break;
1366 
1367 				strip(buffer);
1368 
1369 				if(odd) {
1370 					odd = 0;
1371 					printf("%s<br/>\n", buffer);
1372 					}
1373 				else {
1374 					odd = 1;
1375 					printf("<b>%s</b><br/>\n", buffer);
1376 					}
1377 				}
1378 			}
1379 		else
1380 			printf("Error executing traceroute!\n");
1381 
1382 		pclose(fp);
1383 
1384 		printf("</p>\n");
1385 		}
1386 
1387 	printf("</card>\n");
1388 
1389 	return;
1390 	}
1391 
1392 
1393 
1394 /* displays problems */
display_problems(void)1395 void display_problems(void) {
1396 	host *temp_host;
1397 	service *temp_service;
1398 	hoststatus *temp_hoststatus;
1399 	int total_host_problems = 0;
1400 	servicestatus *temp_servicestatus;
1401 	int total_service_problems = 0;
1402 
1403 	/**** MAIN SCREEN (CARD 1) ****/
1404 	printf("<card id='card1' title='%s Problems'>\n", (display_type == DISPLAY_ALL_PROBLEMS) ? "All" : "Unhandled");
1405 	printf("<p align='center' mode='nowrap'>\n");
1406 	printf("<b>%s Problems</b><br/><br/>\n", (display_type == DISPLAY_ALL_PROBLEMS) ? "All" : "Unhandled");
1407 
1408 	printf("<b>Host Problems:</b>\n");
1409 
1410 	printf("<table columns='2' align='LL'>\n");
1411 
1412 	/* check all hosts */
1413 	for(temp_hoststatus = hoststatus_list; temp_hoststatus != NULL; temp_hoststatus = temp_hoststatus->next) {
1414 
1415 		temp_host = find_host(temp_hoststatus->host_name);
1416 		if(temp_host == NULL)
1417 			continue;
1418 
1419 		if(is_authorized_for_host(temp_host, &current_authdata) == FALSE)
1420 			continue;
1421 
1422 		if(temp_hoststatus->status == SD_HOST_UP || temp_hoststatus->status == HOST_PENDING)
1423 			continue;
1424 
1425 		if(display_type == DISPLAY_UNHANDLED_PROBLEMS) {
1426 			if(temp_hoststatus->problem_has_been_acknowledged == TRUE)
1427 				continue;
1428 			if(temp_hoststatus->notifications_enabled == FALSE)
1429 				continue;
1430 			if(temp_hoststatus->scheduled_downtime_depth > 0)
1431 				continue;
1432 			}
1433 
1434 		total_host_problems++;
1435 
1436 		printf("<tr><td><anchor title='%s'>", temp_host->name);
1437 		if(temp_hoststatus->status == SD_HOST_DOWN)
1438 			printf("DWN");
1439 		else if(temp_hoststatus->status == SD_HOST_UNREACHABLE)
1440 			printf("UNR");
1441 		else
1442 			printf("???");
1443 		printf("<go href='%s' method='post'><postfield name='host' value='%s'/></go></anchor></td>", STATUSWML_CGI, temp_host->name);
1444 		printf("<td>%s</td></tr>\n", temp_host->name);
1445 		}
1446 
1447 	if(total_host_problems == 0)
1448 		printf("<tr><td>No problems</td></tr>\n");
1449 
1450 	printf("</table>\n");
1451 
1452 	printf("<br/>\n");
1453 
1454 
1455 	printf("<b>Svc Problems:</b>\n");
1456 
1457 	printf("<table columns='2' align='LL'>\n");
1458 
1459 	/* check all services */
1460 	for(temp_servicestatus = servicestatus_list; temp_servicestatus != NULL; temp_servicestatus = temp_servicestatus->next) {
1461 
1462 		temp_service = find_service(temp_servicestatus->host_name, temp_servicestatus->description);
1463 		if(temp_service == NULL)
1464 			continue;
1465 
1466 		if(is_authorized_for_service(temp_service, &current_authdata) == FALSE)
1467 			continue;
1468 
1469 		if(temp_servicestatus->status == SERVICE_OK || temp_servicestatus->status == SERVICE_PENDING)
1470 			continue;
1471 
1472 		if(display_type == DISPLAY_UNHANDLED_PROBLEMS) {
1473 			if(temp_servicestatus->problem_has_been_acknowledged == TRUE)
1474 				continue;
1475 			if(temp_servicestatus->notifications_enabled == FALSE)
1476 				continue;
1477 			if(temp_servicestatus->scheduled_downtime_depth > 0)
1478 				continue;
1479 			if((temp_hoststatus = find_hoststatus(temp_service->host_name))) {
1480 				if(temp_hoststatus->scheduled_downtime_depth > 0)
1481 					continue;
1482 				if(temp_hoststatus->problem_has_been_acknowledged == TRUE)
1483 					continue;
1484 				}
1485 			}
1486 
1487 		total_service_problems++;
1488 
1489 		printf("<tr><td><anchor title='%s'>", temp_servicestatus->description);
1490 		if(temp_servicestatus->status == SERVICE_CRITICAL)
1491 			printf("CRI");
1492 		else if(temp_servicestatus->status == SERVICE_WARNING)
1493 			printf("WRN");
1494 		else if(temp_servicestatus->status == SERVICE_UNKNOWN)
1495 			printf("UNK");
1496 		else
1497 			printf("???");
1498 		printf("<go href='%s' method='post'><postfield name='host' value='%s'/><postfield name='service' value='%s'/></go></anchor></td>", STATUSWML_CGI, temp_service->host_name, temp_service->description);
1499 		printf("<td>%s/%s</td></tr>\n", temp_service->host_name, temp_service->description);
1500 		}
1501 
1502 	if(total_service_problems == 0)
1503 		printf("<tr><td>No problems</td></tr>\n");
1504 
1505 	printf("</table>\n");
1506 
1507 	printf("</p>\n");
1508 
1509 	printf("</card>\n");
1510 
1511 	return;
1512 	}
1513