4 Copyright (C) 2006-2009 Jonathan Zarate
11 #include <netinet/in.h>
12 #include <arpa/inet.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");
26 /* Figure out & return the logfile name. */
27 void get_logfilename(char *lfn
)
33 nv
= "/var/log/messages";
34 if (f_read_string("/etc/syslogd.cfg", cfg
, sizeof(cfg
)) > 0) {
35 if ((p
= strchr(cfg
, '\n')))
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
50 void wo_viewlog(char *url
)
62 if ((p
= webcgi_get("find")) != NULL
) {
63 send_header(200, NULL
, mime_plain
, 0);
64 if (strlen(p
) > 64) return;
77 if (isprint(*p
)) *c
++ = *p
;
83 sprintf(s
, "grep -ih \"%s\" $(ls -1rv %s %s.*)", t
, lfn
, lfn
);
84 web_pipecmd(s
, WOF_NONE
);
88 if ((p
= webcgi_get("which")) == NULL
) return;
90 if (strcmp(p
, "all") == 0)
92 else if ((n
= atoi(p
)) <= 0)
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
)
103 char s
[512], ip
[64], val
[256];
108 char host
[NI_MAXHOST
];
110 web_printf("\nwm_%s = [", name
);
113 sprintf(s
, "/proc/webmon_recent_%s", name
);
114 if ((f
= fopen(s
, "r")) != NULL
) {
117 while ((!maxcount
|| i
++ < maxcount
) && fgets(s
, sizeof(s
), f
)) {
118 if (sscanf(s
, "%lu\t%s\t%s", &time
, ip
, val
) != 3) continue;
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
? : "");
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");
163 void wo_syslog(char *url
)
168 get_logfilename(lfn
);
169 if (strncmp(url
, "webmon_", 7) == 0) {
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);
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
);