usbmodeswitch: Updated to v.1.2.6 from shibby's branch.
[tomato.git] / release / src / router / httpd / log.c
blobc1cb0e347efaa39a3d172297a95cadc019dabb4c
1 /*
3 Tomato Firmware
4 Copyright (C) 2006-2009 Jonathan Zarate
6 */
8 #include "tomato.h"
10 #include <ctype.h>
11 #include <netinet/in.h>
12 #include <arpa/inet.h>
13 #include <netdb.h>
15 /* Max number of log lines for GUI to display */
16 #define MAX_LOG_LINES 2000
18 static int logok(void)
20 if (nvram_match("log_file", "1")) return 1;
21 resmsg_set("Internal logging disabled");
22 redirect("error.asp");
23 return 0;
26 /* Figure out & return the logfile name. */
27 void get_logfilename(char *lfn)
29 char *p;
30 char cfg[256];
31 char *nv;
33 nv = "/var/log/messages";
34 if (f_read_string("/etc/syslogd.cfg", cfg, sizeof(cfg)) > 0) {
35 if ((p = strchr(cfg, '\n')))
36 *p = 0;
37 strtok(cfg, " \t"); // skip rotsize
38 strtok(NULL, " \t"); // Skip backup cnt
39 if ((p = strtok(NULL, " \t")) && (*p == '/')) {
40 // check if we can write to the file
41 if (f_write(p, cfg, 0, FW_APPEND, 0) >= 0) {
42 nv = p; // nv is the configured log filename
46 if (lfn)
47 strcpy(lfn, nv);
50 void wo_viewlog(char *url)
52 char *p;
53 char *c;
54 char s[128];
55 char t[128];
56 int n;
57 char lfn[256];
59 if (!logok()) return;
61 get_logfilename(lfn);
62 if ((p = webcgi_get("find")) != NULL) {
63 send_header(200, NULL, mime_plain, 0);
64 if (strlen(p) > 64) return;
65 c = t;
66 while (*p) {
67 switch (*p) {
68 case '<':
69 case '>':
70 case '|':
71 case '"':
72 case '\\':
73 *c++ = '\\';
74 *c++ = *p;
75 break;
76 default:
77 if (isprint(*p)) *c++ = *p;
78 break;
80 ++p;
82 *c = 0;
83 sprintf(s, "grep -ih \"%s\" $(ls -1rv %s %s.*)", t, lfn, lfn);
84 web_pipecmd(s, WOF_NONE);
85 return;
88 if ((p = webcgi_get("which")) == NULL) return;
90 if (strcmp(p, "all") == 0)
91 n = MAX_LOG_LINES;
92 else if ((n = atoi(p)) <= 0)
93 return;
95 send_header(200, NULL, mime_plain, 0);
96 sprintf(s, "cat $(ls -1rv %s %s.*) | tail -n %d", lfn, lfn, n);
97 web_pipecmd(s, WOF_NONE);
100 static void webmon_list(char *name, int webmon, int resolve, unsigned int maxcount)
102 FILE *f;
103 char s[512], ip[64], val[256];
104 char *js, *jh;
105 char comma;
106 unsigned long time;
107 unsigned int i;
108 char host[NI_MAXHOST];
110 web_printf("\nwm_%s = [", name);
112 if (webmon) {
113 sprintf(s, "/proc/webmon_recent_%s", name);
114 if ((f = fopen(s, "r")) != NULL) {
115 comma = ' ';
116 i = 0;
117 while ((!maxcount || i++ < maxcount) && fgets(s, sizeof(s), f)) {
118 if (sscanf(s, "%lu\t%s\t%s", &time, ip, val) != 3) continue;
119 jh = NULL;
120 if (resolve) {
121 if (resolve_addr(ip, host) == 0)
122 jh = js_string(host);
124 js = utf8_to_js_string(val);
125 web_printf("%c['%lu','%s','%s', '%s']", comma,
126 time, ip, js ? : "", jh ? : "");
127 free(js);
128 free(jh);
129 comma = ',';
131 fclose(f);
135 web_puts("];\n");
138 void asp_webmon(int argc, char **argv)
140 int webmon = nvram_get_int("log_wm");
141 int maxcount = (argc > 0) ? atoi(argv[0]) : 0;
142 int resolve = (argc > 1) ? atoi(argv[1]) : 0;
144 webmon_list("domains", webmon, resolve, maxcount);
145 webmon_list("searches", webmon, resolve, maxcount);
148 void wo_webmon(char *url)
150 nvram_set("log_wmclear", webcgi_get("clear"));
151 exec_service("firewall-restart");
152 nvram_unset("log_wmclear");
155 static int webmon_ok(int searches)
157 if (nvram_get_int("log_wm") && nvram_get_int(searches ? "log_wmsmax" : "log_wmdmax") > 0) return 1;
158 resmsg_set("Web Monitoring disabled");
159 redirect("error.asp");
160 return 0;
163 void wo_syslog(char *url)
165 char lfn[256];
166 char s[128];
168 get_logfilename(lfn);
169 if (strncmp(url, "webmon_", 7) == 0) {
170 // web monitor
171 char file[64];
172 snprintf(file, sizeof(file), "/proc/%s", url);
173 if (!webmon_ok(strstr(url, "searches") != NULL)) return;
174 send_header(200, NULL, mime_binary, 0);
175 do_file(file);
177 else {
178 // syslog
179 if (!logok()) return;
180 send_header(200, NULL, mime_binary, 0);
181 sprintf(s, "cat $(ls -1rv %s %s.*)", lfn, lfn);
182 web_pipecmd(s, WOF_NONE);