source: trunk/src/sh_html.c@ 94

Last change on this file since 94 was 34, checked in by rainer, 19 years ago

Code cleanup and minor fixes

File size: 11.7 KB
Line 
1/* SAMHAIN file system integrity testing */
2/* Copyright (C) 1999, 2000 Rainer Wichmann */
3/* */
4/* This program is free software; you can redistribute it */
5/* and/or modify */
6/* it under the terms of the GNU General Public License as */
7/* published by */
8/* the Free Software Foundation; either version 2 of the License, or */
9/* (at your option) any later version. */
10/* */
11/* This program is distributed in the hope that it will be useful, */
12/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14/* GNU General Public License for more details. */
15/* */
16/* You should have received a copy of the GNU General Public License */
17/* along with this program; if not, write to the Free Software */
18/* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20#include "config_xor.h"
21
22#include <string.h>
23#include <stdio.h>
24#include <stdlib.h>
25
26#if TIME_WITH_SYS_TIME
27#include <sys/time.h>
28#include <time.h>
29#else
30#if HAVE_SYS_TIME_H
31#include <sys/time.h>
32#else
33#include <time.h>
34#endif
35#endif
36
37
38#ifdef SH_WITH_SERVER
39
40
41#include "samhain.h"
42#include "sh_forward.h"
43#include "sh_error.h"
44#include "sh_unix.h"
45#include "sh_utils.h"
46#include "sh_html.h"
47
48#undef FIL__
49#define FIL__ _("sh_html.c")
50
51
52s_stat server_status;
53
54
55
56static
57char * replace_stat (char * line)
58{
59 st_format rep_serv_tab[] = {
60 { 'T', S_FMT_TIME, 0, 0, NULL},
61 { 'S', S_FMT_TIME, 0, 0, NULL},
62 { 'L', S_FMT_TIME, 0, 0, NULL},
63 { 'O', S_FMT_ULONG, 0, 0, NULL},
64 { 'A', S_FMT_ULONG, 0, 0, NULL},
65 { 'M', S_FMT_ULONG, 0, 0, NULL},
66 {'\0', S_FMT_ULONG, 0, 0, NULL},
67 };
68
69 rep_serv_tab[0].data_ulong = (unsigned long) time(NULL);
70 rep_serv_tab[1].data_ulong = server_status.start;
71 rep_serv_tab[2].data_ulong = server_status.last;
72 rep_serv_tab[3].data_ulong = server_status.conn_open;
73 rep_serv_tab[4].data_ulong = server_status.conn_total;
74 rep_serv_tab[5].data_ulong = server_status.conn_max;
75
76 return (sh_util_formatted(line, rep_serv_tab));
77}
78
79
80static
81int sh_html_head(SL_TICKET ticket)
82{
83 long status = SL_ENONE;
84 SL_TICKET fd = (-1);
85 char line[512];
86 char endhead[512];
87 char outline[1024];
88 char ts1[81];
89 char ts2[81];
90 time_t now;
91 struct tm * time_ptr;
92
93 char * formatted;
94 char * qstr;
95
96 char * p;
97
98 SL_ENTER(_("sh_html_head"));
99
100 p = sh_util_strconcat(DEFAULT_DATAROOT, _("/head.html"), NULL);
101
102 if (p)
103 {
104 fd = sl_open_read (p, SL_YESPRIV);
105 SH_FREE(p);
106 }
107
108 if (!SL_ISERROR(fd))
109 {
110 while (!SL_ISERROR(status) && sh_unix_getline (fd, line, sizeof(line)) > 0)
111 {
112 formatted = replace_stat (line);
113 if (formatted)
114 {
115 status = sl_write_line (ticket, formatted, sl_strlen(formatted));
116 SH_FREE(formatted);
117 }
118 }
119 sl_close(fd);
120 }
121 else
122 {
123 qstr = sh_util_basename(DEFAULT_HTML_FILE);
124 if (qstr != NULL)
125 {
126 sl_snprintf(endhead, 511,
127 _("<meta http-equiv=%crefresh%c content=%c120; URL=./%s%c></HEAD><BODY>"),
128 34, 34, 34, qstr, 34);
129 SH_FREE(qstr);
130 }
131 else
132 {
133 sl_snprintf(endhead, 511, _("</HEAD><BODY>"));
134 }
135
136 status =
137 sl_write_line (ticket,
138 _("<HTML><HEAD><TITLE>Report</TITLE>"),
139 sizeof("<HTML><HEAD><TITLE>Report</TITLE>")-1);
140 if (!SL_ISERROR(status))
141 status =
142 sl_write_line (ticket, endhead, strlen(endhead));
143 if (!SL_ISERROR(status))
144 status =
145 sl_write_line (ticket,
146 _("<H1>Samhain Server Report</H1>"),
147 sizeof("<H1>Samhain Server Report</H1>")-1);
148 if (!SL_ISERROR(status))
149 {
150 time_ptr = localtime (&(server_status.start));
151 if (time_ptr != NULL)
152 status = strftime (ts1, 80, _("%d-%m-%Y %H:%M:%S"), time_ptr);
153 now = time(NULL);
154 time_ptr = localtime (&now);
155 if (time_ptr != NULL)
156 status = strftime (ts2, 80, _("%d-%m-%Y %H:%M:%S"), time_ptr);
157
158 sl_snprintf(outline, 1023,
159 _("<p>Time:<BR>Now: %s<BR>Start: %s</p>"),
160 ts2, ts1);
161 status =
162 sl_write_line (ticket, outline, sl_strlen(outline));
163 }
164 if (!SL_ISERROR(status))
165 {
166 sl_snprintf(outline, 1023,
167 _("<p>Connections (max. %d simultaneous):"\
168 "<BR>Now: %d<BR>Total: %ld</p>"),
169 server_status.conn_max,
170 server_status.conn_open,
171 server_status.conn_total);
172 status =
173 sl_write_line (ticket, outline, sl_strlen(outline));
174 if (server_status.last > (time_t) 0)
175 {
176 time_ptr = localtime (&(server_status.last));
177 if (time_ptr != NULL)
178 status = strftime (ts1, 80, _("%d-%m-%Y %H:%M:%S"), time_ptr);
179 sl_snprintf(outline, 1023,
180 _("<p>Last connection at %s</p>"),
181 ts1);
182 status =
183 sl_write_line (ticket, outline, sl_strlen(outline));
184 }
185 }
186 if (!SL_ISERROR(status))
187 status =
188 sl_write_line (ticket,
189 _("<center><table cellpadding=5 cellspacing=2 border=2>"),
190 sizeof("<center><table cellpadding=5 cellspacing=2 border=2>")-1);
191 }
192
193 if (SL_ISERROR(status))
194 SL_RETURN((-1), _("sh_html_head"));
195
196 SL_RETURN((0), _("sh_html_head"));
197}
198
199static
200int sh_html_foot(SL_TICKET ticket)
201{
202 long status = SL_ENONE;
203 SL_TICKET fd = (-1);
204 char line[512];
205 char * p;
206
207 SL_ENTER(_("sh_html_foot"));
208
209 p = sh_util_strconcat(DEFAULT_DATAROOT, _("/foot.html"), NULL);
210
211 if (p)
212 {
213 fd = sl_open_read (p, SL_YESPRIV);
214 SH_FREE(p);
215 }
216
217 if (!SL_ISERROR(fd))
218 {
219 while (!SL_ISERROR(status) && sh_unix_getline (fd, line, sizeof(line)) > 0)
220 {
221 status = sl_write_line (ticket, line, sl_strlen(line));
222 }
223 sl_close(fd);
224 }
225 else
226 {
227 status = sl_write_line (ticket, _("</table></center></BODY></HTML>"),
228 sizeof("</table></center></BODY></HTML>")-1);
229 }
230 if (SL_ISERROR(status))
231 SL_RETURN((-1), _("sh_html_foot"));
232
233 SL_RETURN((0), _("sh_html_foot"));
234}
235
236
237static
238char * replace_tab (const char * line, char * host, char * status,
239 char * timestamp)
240{
241 st_format rep_serv_tab[] = {
242 { 'H', S_FMT_STRING, 0, 0, NULL},
243 { 'S', S_FMT_STRING, 0, 0, NULL},
244 { 'T', S_FMT_STRING, 0, 0, NULL},
245 {'\0', S_FMT_ULONG, 0, 0, NULL},
246 };
247 char * p;
248
249 SL_ENTER(_("replace_tab"));
250
251 rep_serv_tab[0].data_str = host;
252 rep_serv_tab[1].data_str = status;
253 rep_serv_tab[2].data_str = timestamp;
254
255 p = sh_util_formatted(line, rep_serv_tab);
256 SL_RETURN(p, _("replace_tab"));
257}
258
259static char * entry_orig = NULL;
260static size_t entry_size = 0;
261
262static
263int sh_html_get_entry ()
264{
265 long retval = SL_ENONE;
266 SL_TICKET fd = (-1);
267 char line[512];
268 size_t line_size;
269 size_t add_size = 0;
270 char * p;
271
272 SL_ENTER(_("sh_html_get_entry"));
273
274 p = sh_util_strconcat(DEFAULT_DATAROOT, _("/entry.html"), NULL);
275
276 entry_size = 0;
277 if (entry_orig != NULL)
278 {
279 free (entry_orig);
280 entry_orig = NULL;
281 entry_size = 0;
282 }
283
284 if (p)
285 {
286 fd = sl_open_read (p, SL_YESPRIV);
287 SH_FREE(p);
288 }
289 if (!SL_ISERROR(fd))
290 {
291 while (!SL_ISERROR(retval) && sh_unix_getline (fd, line, sizeof(line)) > 0)
292 {
293 line_size = sl_strlen(line);
294 add_size = 0;
295 if (entry_orig != NULL)
296 {
297 entry_orig = realloc(entry_orig, /* free() ok */
298 entry_size + line_size + 1);
299 if (entry_orig) { add_size = line_size; }
300 }
301 else
302 {
303 entry_orig = malloc(line_size + 1); /* free() ok */
304 if (entry_orig) { entry_orig[0] = '\0'; add_size = line_size; }
305 }
306 if (!entry_orig)
307 {
308 entry_size = 0;
309 add_size = 0;
310 SL_RETURN( 0, _("sh_html_get_entry"));
311 }
312
313 sl_strlcat(&entry_orig[entry_size], line, line_size + 1);
314 entry_size += add_size;
315 SH_VALIDATE_EQ(entry_orig[entry_size], '\0');
316 }
317 sl_close(fd);
318 }
319 SL_RETURN( entry_size, _("sh_html_get_entry"));
320}
321
322static
323int sh_html_entry (SL_TICKET ticket,
324 char * host, char * status, char * timestamp, int flag)
325{
326 char outline[1024];
327 long retval = SL_ENONE;
328
329 char * formatted;
330
331 SL_ENTER(_("sh_html_entry"));
332
333 if (entry_size > 0 && entry_orig != NULL)
334 {
335 formatted = replace_tab(entry_orig, host, status, timestamp);
336 if (formatted)
337 {
338 retval = sl_write_line (ticket, formatted, sl_strlen(formatted));
339 SH_FREE(formatted);
340 }
341 }
342 else
343 {
344 sl_snprintf(outline, 1023,
345 _("<tr><td>%s</td><td>%s</td><td>%s</td></tr>"),
346 host, status, timestamp);
347 retval = sl_write_line (ticket, outline, sl_strlen(outline));
348 }
349
350 /* write a status line
351 */
352 if ((flag == 1) && (!SL_ISERROR(retval)))
353 {
354 sl_snprintf(outline, 1023,
355 _("<!-- \n[STATUS:] %s %s %s\n -->"),
356 host, status, timestamp);
357 retval = sl_write_line (ticket, outline, sl_strlen(outline));
358 }
359
360 if (SL_ISERROR(retval))
361 SL_RETURN((-1), _("sh_html_entry"));
362
363 SL_RETURN((0), _("sh_html_entry"));
364}
365
366typedef struct _sort_arr {
367 char msg[TIM_MAX];
368 char tim[TIM_MAX];
369} sort_arr;
370
371static sort_arr sort_stat[CLT_MAX];
372
373static
374int comp_arr (const void * ao, const void * bo)
375{
376 sort_arr * a;
377 sort_arr * b;
378
379 if (ao == NULL && bo == NULL)
380 return 0;
381 else if (ao == NULL && bo != NULL)
382 return (-1);
383 else if (ao != NULL && bo == NULL)
384 return (1);
385
386 a = (sort_arr *) ao;
387 b = (sort_arr *) bo;
388
389 return ((-1) * sl_strcmp(a->tim, b->tim));
390}
391
392static
393int sh_html_print_one (SL_TICKET ticket, client_t * top)
394{
395 int status;
396 int clt_status;
397 int i, n;
398
399 SL_ENTER(_("sh_html_print_one"));
400
401 if (top == NULL)
402 SL_RETURN((0), _("sh_html_print_one"));
403
404 clt_status = top->status_now;
405 status = sh_html_entry (ticket,
406 top->hostname,
407 _(clt_stat[clt_status]),
408 top->timestamp[clt_status],
409 1);
410
411 n = 0;
412
413 if (clt_status != CLT_INACTIVE)
414 {
415 for (i = 1; i < CLT_MAX; ++i)
416 {
417 if (top->status_arr[i] != CLT_INACTIVE)
418 {
419 clt_status = top->status_arr[i];
420 sl_strlcpy(sort_stat[n].msg, _(clt_stat[clt_status]), TIM_MAX);
421 sl_strlcpy(sort_stat[n].tim, top->timestamp[clt_status],TIM_MAX);
422 ++n;
423 }
424 }
425 }
426
427 if (n > 0)
428 {
429 qsort(&(sort_stat[0]), n, sizeof(sort_arr), comp_arr);
430
431 for (i = 1; i < n; ++i)
432 {
433 status = sh_html_entry (ticket,
434 " ",
435 sort_stat[i].msg,
436 sort_stat[i].tim,
437 0);
438 }
439 }
440
441 if (SL_ISERROR(status))
442 SL_RETURN((-1), _("sh_html_print_one"));
443
444 SL_RETURN((0), _("sh_html_print_one"));
445}
446
447#include "zAVLTree.h"
448
449int sh_html_write(void * inptr)
450{
451 long fd;
452 zAVLCursor avlcursor;
453 client_t * item;
454 zAVLTree * top = (zAVLTree *) inptr;
455
456 SL_ENTER(_("sh_html_write"));
457
458 if (0 != (fd = tf_trust_check (DEFAULT_HTML_FILE, SL_YESPRIV)))
459 {
460 sh_error_handle((-1), FIL__, __LINE__, fd, MSG_E_TRUST,
461 (long) sh.effective.uid,
462 DEFAULT_HTML_FILE);
463 SL_RETURN((-1), _("sh_html_write"));
464 }
465
466
467 fd = sl_open_write_trunc (DEFAULT_HTML_FILE, SL_YESPRIV);
468
469 if (SL_ISERROR(fd))
470 {
471 sh_error_handle((-1), FIL__, __LINE__, fd, MSG_E_ACCESS,
472 (long) sh.effective.uid,
473 DEFAULT_HTML_FILE);
474 SL_RETURN((-1), _("sh_html_write"));
475 }
476
477 sh_html_get_entry();
478
479 sh_html_head(fd);
480 for (item = (client_t *) zAVLFirst(&avlcursor, top); item;
481 item = (client_t *) zAVLNext(&avlcursor))
482 sh_html_print_one (fd, item);
483 sh_html_foot(fd);
484 sl_close(fd);
485
486 SL_RETURN((0), _("sh_html_write"));
487}
488
489/* SH_WITH_SERVER */
490#endif
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
Note: See TracBrowser for help on using the repository browser.