summaryrefslogtreecommitdiff
path: root/release/src/router/httpd/log.c
blob: 2aca2b232ebbce25bb8aff971df953423bcdbc5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*

	Tomato Firmware
	Copyright (C) 2006-2009 Jonathan Zarate

*/

#include "tomato.h"

#include <ctype.h>

static int logok(void)
{
	if (nvram_match("log_file", "1")) return 1;
	resmsg_set("Internal logging disabled");
	redirect("error.asp");
	return 0;
}

void wo_viewlog(char *url)
{
	char *p;
	char *c;
	char s[128];
	char t[128];
	int n;

	if (!logok()) return;

	if ((p = webcgi_get("find")) != NULL) {
		send_header(200, NULL, mime_plain, 0);
		if (strlen(p) > 64) return;
		c = t;
		while (*p) {
			switch (*p) {
			case '<':
			case '>':
			case '|':
			case '"':
			case '\\':
				*c++ = '\\';
				*c++ = *p;
				break;
			default:
				if (isprint(*p)) *c++ = *p;
				break;
			}
			++p;
		}
		*c = 0;
		sprintf(s, "cat %s %s | grep -i \"%s\"", "/var/log/messages.0", "/var/log/messages", t);
		web_pipecmd(s, WOF_NONE);
		return;
	}

	if ((p = webcgi_get("which")) == NULL) return;
	if (strcmp(p, "all") == 0) {
		send_header(200, NULL, mime_plain, 0);
		do_file("/var/log/messages.0");
		do_file("/var/log/messages");
		return;
	}
	if ((n = atoi(p)) > 0) {
		send_header(200, NULL, mime_plain, 0);
		sprintf(s, "cat %s %s | tail -n %d", "/var/log/messages.0", "/var/log/messages", n);
		web_pipecmd(s, WOF_NONE);
	}
}

void wo_syslog(char *url)
{
	if (!logok()) return;
	send_header(200, NULL, mime_binary, 0);
	do_file("/var/log/messages.0");
	do_file("/var/log/messages");
}