source: trunk/src/sh_html.c@ 583

Last change on this file since 583 was 583, checked in by katerina, 32 hours ago

Fix for ticket #471 (autoreconf throws warnings/errors).

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