source: trunk/src/sh_html.c@ 14

Last change on this file since 14 was 1, checked in by katerina, 19 years ago

Initial import

File size: 11.6 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, 511) > 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_filename(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, 511) > 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, 511) > 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);
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 + 1; }
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 strcat(&entry_orig[entry_size], line); /* known to fit */
314 entry_size += add_size;
315 }
316 sl_close(fd);
317 }
318 SL_RETURN( entry_size, _("sh_html_get_entry"));
319}
320
321static
322int sh_html_entry (SL_TICKET ticket,
323 char * host, char * status, char * timestamp, int flag)
324{
325 char outline[1024];
326 long retval = SL_ENONE;
327
328 char * formatted;
329
330 SL_ENTER(_("sh_html_entry"));
331
332 if (entry_size > 0 && entry_orig != NULL)
333 {
334 formatted = replace_tab(entry_orig, host, status, timestamp);
335 if (formatted)
336 {
337 retval = sl_write_line (ticket, formatted, sl_strlen(formatted));
338 SH_FREE(formatted);
339 }
340 }
341 else
342 {
343 sl_snprintf(outline, 1023,
344 _("<tr><td>%s</td><td>%s</td><td>%s</td></tr>"),
345 host, status, timestamp);
346 retval = sl_write_line (ticket, outline, sl_strlen(outline));
347 }
348
349 /* write a status line
350 */
351 if ((flag == 1) && (!SL_ISERROR(retval)))
352 {
353 sl_snprintf(outline, 1023,
354 _("<!-- \n[STATUS:] %s %s %s\n -->"),
355 host, status, timestamp);
356 retval = sl_write_line (ticket, outline, sl_strlen(outline));
357 }
358
359 if (SL_ISERROR(retval))
360 SL_RETURN((-1), _("sh_html_entry"));
361
362 SL_RETURN((0), _("sh_html_entry"));
363}
364
365typedef struct _sort_arr {
366 char msg[TIM_MAX];
367 char tim[TIM_MAX];
368} sort_arr;
369
370static sort_arr sort_stat[CLT_MAX];
371
372static
373int comp_arr (const void * ao, const void * bo)
374{
375 sort_arr * a;
376 sort_arr * b;
377
378 if (ao == NULL && bo == NULL)
379 return 0;
380 else if (ao == NULL && bo != NULL)
381 return (-1);
382 else if (ao != NULL && bo == NULL)
383 return (1);
384
385 a = (sort_arr *) ao;
386 b = (sort_arr *) bo;
387
388 return ((-1) * sl_strcmp(a->tim, b->tim));
389}
390
391static
392int sh_html_print_one (SL_TICKET ticket, client_t * top)
393{
394 int status;
395 int clt_status;
396 int i, n;
397
398 SL_ENTER(_("sh_html_print_one"));
399
400 if (top == NULL)
401 SL_RETURN((0), _("sh_html_print_one"));
402
403 clt_status = top->status_now;
404 status = sh_html_entry (ticket,
405 top->hostname,
406 _(clt_stat[clt_status]),
407 top->timestamp[clt_status],
408 1);
409
410 n = 0;
411
412 if (clt_status != CLT_INACTIVE)
413 {
414 for (i = 1; i < CLT_MAX; ++i)
415 {
416 if (top->status_arr[i] != CLT_INACTIVE)
417 {
418 clt_status = top->status_arr[i];
419 sl_strlcpy(sort_stat[n].msg, _(clt_stat[clt_status]), TIM_MAX);
420 sl_strlcpy(sort_stat[n].tim, top->timestamp[clt_status],TIM_MAX);
421 ++n;
422 }
423 }
424 }
425
426 if (n > 0)
427 {
428 qsort(&(sort_stat[0]), n, sizeof(sort_arr), comp_arr);
429
430 for (i = 1; i < n; ++i)
431 {
432 status = sh_html_entry (ticket,
433 " ",
434 sort_stat[i].msg,
435 sort_stat[i].tim,
436 0);
437 }
438 }
439
440 if (SL_ISERROR(status))
441 SL_RETURN((-1), _("sh_html_print_one"));
442
443 SL_RETURN((0), _("sh_html_print_one"));
444}
445
446#include "zAVLTree.h"
447
448int sh_html_write(void * inptr)
449{
450 long fd;
451 zAVLCursor avlcursor;
452 client_t * item;
453 zAVLTree * top = (zAVLTree *) inptr;
454
455 SL_ENTER(_("sh_html_write"));
456
457 if (0 != (fd = tf_trust_check (DEFAULT_HTML_FILE, SL_YESPRIV)))
458 {
459 sh_error_handle((-1), FIL__, __LINE__, fd, MSG_E_TRUST,
460 (long) sh.effective.uid,
461 DEFAULT_HTML_FILE);
462 SL_RETURN((-1), _("sh_html_write"));
463 }
464
465
466 fd = sl_open_write_trunc (DEFAULT_HTML_FILE, SL_YESPRIV);
467
468 if (SL_ISERROR(fd))
469 {
470 sh_error_handle((-1), FIL__, __LINE__, fd, MSG_E_ACCESS,
471 (long) sh.effective.uid,
472 DEFAULT_HTML_FILE);
473 SL_RETURN((-1), _("sh_html_write"));
474 }
475
476 sh_html_get_entry();
477
478 sh_html_head(fd);
479 for (item = (client_t *) zAVLFirst(&avlcursor, top); item;
480 item = (client_t *) zAVLNext(&avlcursor))
481 sh_html_print_one (fd, item);
482 sh_html_foot(fd);
483 sl_close(fd);
484
485 SL_RETURN((0), _("sh_html_write"));
486}
487
488/* SH_WITH_SERVER */
489#endif
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
Note: See TracBrowser for help on using the repository browser.