void asp_version(int argc, char **argv) { #if 0 if (argc != 0) { web_puts(tomato_version); } else { web_write(tomato_version, strrchr(tomato_version, '.') - tomato_version); } #else if (argc != 0) { switch (atoi(argv[0])) { case 2: // kernel version web_pipecmd("uname -r", WOF_NONE); break; case 3: // wl driver version web_puts(EPI_VERSION_STR); break; default: // tomato version web_puts(tomato_version); break; } } else { web_puts(tomato_shortver); } #endif }
void asp_arplist(int argc, char **argv) { FILE *f; char s[512]; char ip[16]; char mac[18]; char dev[17]; char comma; unsigned int flags; /* cat /proc/net/arp IP address HW type Flags HW address Mask Device 192.168.0.1 0x1 0x2 00:01:02:03:04:05 * vlan1 */ web_puts("\narplist = ["); comma = ' '; if ((f = fopen("/proc/net/arp", "r")) != NULL) { while (fgets(s, sizeof(s), f)) { if (sscanf(s, "%15s %*s 0x%X %17s %*s %16s", ip, &flags, mac, dev) != 4) continue; if ((strlen(mac) != 17) || (strcmp(mac, "00:00:00:00:00:00") == 0)) continue; if (flags == 0) continue; // if ((nvram_match("wan_ifname", dev)) && (!nvram_match("wan_ipaddr", ip))) continue; // half web_printf("%c['%s','%s','%s']", comma, ip, mac, dev); comma = ','; } fclose(f); } web_puts("];\n"); }
void asp_pptpd_userol(int argc, char **argv) { char comma; char line[128]; FILE *fp; char clientusername[32+1]; char clientlocalip[INET6_ADDRSTRLEN+1]; char clientremoteip[INET6_ADDRSTRLEN+1]; char interface[IF_SIZE+1]; int ppppid; int clientuptime; web_puts("\n\npptpd_online=["); comma = ' '; fp = fopen(PPTP_CONNECTED, "r"); if (fp) { while (fgets(line, sizeof(line), fp) != NULL) { if (sscanf(line, "%d %s %s %s %s %d", &ppppid, interface, clientlocalip, clientremoteip, clientusername, &clientuptime) != 6) continue; web_printf("%c['%d', '%s', '%s', '%s', '%s', '%d']", comma, ppppid, interface, clientlocalip, clientremoteip, clientusername, clientuptime); comma = ','; } fclose(fp); } web_puts("];\n"); }
void asp_netdev(int argc, char **argv) { FILE *f; char buf[256]; int64_t rx, tx; char *p; char *ifname; char comma; char *exclude; int sfd; struct ifreq ifr; exclude = nvram_safe_get("rstats_exclude"); web_puts("\n\nnetdev={"); if ((f = fopen("/proc/net/dev", "r")) != NULL) { fgets(buf, sizeof(buf), f); // header fgets(buf, sizeof(buf), f); // " comma = ' '; if ((sfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) { _dprintf("[%s %d]: error opening socket %m\n", __FUNCTION__, __LINE__); } while (fgets(buf, sizeof(buf), f)) { if ((p = strchr(buf, ':')) == NULL) continue; *p = 0; if ((ifname = strrchr(buf, ' ')) == NULL) ifname = buf; else ++ifname; // if (strncmp(ifname, "ppp", 3) == 0) ifname = "ppp"; if ((strcmp(ifname, "lo") == 0) || (find_word(exclude, ifname))) continue; // skip down interfaces if (sfd >= 0) { strcpy(ifr.ifr_name, ifname); if (ioctl(sfd, SIOCGIFFLAGS, &ifr) != 0) continue; if ((ifr.ifr_flags & IFF_UP) == 0) continue; } // <rx bytes, packets, errors, dropped, fifo errors, frame errors, compressed, multicast><tx ...> if (sscanf(p + 1, "%llu%*u%*u%*u%*u%*u%*u%*u%llu", &rx, &tx) != 2) continue; if (!strcmp(ifname, "imq1")) web_printf("%c'%s':{rx:0x0,tx:0x%llx}", comma, ifname, rx, tx); else if (!strcmp(ifname, "imq2")) web_printf("%c'%s':{rx:0x%llx,tx:0x0}", comma, ifname, rx, tx); else web_printf("%c'%s':{rx:0x%llx,tx:0x%llx}", comma, ifname, rx, tx); comma = ','; } if (sfd >= 0) close(sfd); fclose(f); } web_puts("};\n"); }
void asp_upnpinfo(int argc, char **argv) { if (nvram_get_int("upnp_enable")) { f_write_string("/etc/upnp/info", "", 0, 0); if (killall("miniupnpd", SIGUSR2) == 0) { f_wait_notexists("/etc/upnp/info", 5); } web_puts("\nmupnp_data = '"); web_putfile("/etc/upnp/data.info", WOF_JAVASCRIPT); web_puts("';\n"); } }
void wo_pptpdcmd(char *url) { char *p; int n = 10; // do we really need to output anything? web_puts("\npptd_result = [\n"); if ((p = webcgi_get("disconnect")) != NULL) { while ((kill(atoi(p), SIGTERM) == 0) && (n > 1)) { sleep(1); n--; } } web_puts("];\n"); }
void wo_ping(char *url) { char cmd[256]; const char *addr; addr = webcgi_get("addr"); if (!check_addr(addr, 64)) return; killall("ping", SIGTERM); web_puts("\npingdata = '"); sprintf(cmd, "ping -c %d -s %d %s", atoi(webcgi_safeget("count", "0")), atoi(webcgi_safeget("size", "0")), addr); web_pipecmd(cmd, WOF_JAVASCRIPT); web_puts("';"); }
void wo_trace(char *url) { char cmd[256]; const char *addr; addr = webcgi_get("addr"); if (!check_addr(addr, 64)) return; killall("traceroute", SIGTERM); web_puts("\ntracedata = '"); sprintf(cmd, "traceroute -I -m %u -w %u %s", atoi(webcgi_safeget("hops", "0")), atoi(webcgi_safeget("wait", "0")), addr); web_pipecmd(cmd, WOF_JAVASCRIPT); web_puts("';"); }
// <% nvramseq('foo', 'bar%d', 5, 8); %> -> foo = ['a','b','c']; void asp_nvramseq(int argc, char **argv) { int i, e; char s[256]; if (argc != 4) return; web_printf("\n%s = [\n", argv[0]); e = atoi(argv[3]); for (i = atoi(argv[2]); i <= e; ++i) { snprintf(s, sizeof(s), argv[1], i); web_puts("'"); web_putj(nvram_safe_get(s)); web_puts((i == e) ? "'" : "',"); } web_puts("];\n"); }
// <% nvram("x,y,z"); %> -> nvram = {'x': '1','y': '2','z': '3'}; void asp_nvram(int argc, char **argv) { char *list; char *p, *k; char *value; if ((argc != 1) || ((list = strdup(argv[0])) == NULL)) return; web_puts("\nnvram = {\n"); p = list; while ((k = strsep(&p, ",")) != NULL) { if (*k == 0) continue; if (strcmp(k, "wl_unit") == 0) continue; web_printf("\t'%s': '", k); // AB multiSSID // web_putj(nvram_safe_get(k)); value = nvram_safe_get(k); web_putj_utf8(value); web_puts("',\n"); if (strncmp(k, "wl_", 3) == 0) { foreach_wif(1, k, print_wlnv); } } free(list); web_puts("\t'wl_unit': '"); // AB multiSSID web_putj(nvram_safe_get("wl_unit")); web_puts("',\n"); web_puts("\t'http_id': '"); // AB multiSSID web_putj(nvram_safe_get("http_id")); web_puts("',\n"); web_puts("\t'web_mx': '"); // AB multiSSID web_putj(nvram_safe_get("web_mx")); web_puts("',\n"); web_puts("\t'web_pb': '"); // AB multiSSID web_putj(nvram_safe_get("web_pb")); web_puts("'};\n"); }
void common_redirect(void) { if (atoi(webcgi_safeget("_ajax", ""))) { send_header(200, NULL, mime_html, 0); web_puts("OK"); } else { redirect(webcgi_safeget("_redirect", "/")); } }
static int print_wlnv(int idx, int unit, int subunit, void *param) { char *k = param; char *nv; nv = wl_nvname(k + 3, unit, subunit); web_printf("\t'%s': '", nv); // AB multiSSID web_putj(nvram_safe_get(nv)); web_puts("',\n"); return 1; }
void asp_iptraffic(int argc, char **argv) { char comma; char sa[256]; FILE *a; char ip[INET_ADDRSTRLEN]; char *exclude; unsigned long tx_bytes, rx_bytes; unsigned long tp_tcp, rp_tcp; unsigned long tp_udp, rp_udp; unsigned long tp_icmp, rp_icmp; unsigned int ct_tcp, ct_udp; exclude = nvram_safe_get("cstats_exclude"); Node tmp; Node *ptr; iptraffic_conntrack_init(); char br; char name[] = "/proc/net/ipt_account/lanX"; web_puts("\n\niptraffic=["); comma = ' '; for(br=0 ; br<=3 ; br++) { char bridge[2] = "0"; if (br!=0) bridge[0]+=br; else strcpy(bridge, ""); sprintf(name, "/proc/net/ipt_account/lan%s", bridge); if ((a = fopen(name, "r")) == NULL) continue; fgets(sa, sizeof(sa), a); // network while (fgets(sa, sizeof(sa), a)) { if(sscanf(sa, "ip = %s bytes_src = %lu %*u %*u %*u %*u packets_src = %*u %lu %lu %lu %*u bytes_dst = %lu %*u %*u %*u %*u packets_dst = %*u %lu %lu %lu %*u time = %*u", ip, &tx_bytes, &tp_tcp, &tp_udp, &tp_icmp, &rx_bytes, &rp_tcp, &rp_udp, &rp_icmp) != 9 ) continue; if (find_word(exclude, ip)) continue ; if ((tx_bytes > 0) || (rx_bytes > 0)){ strncpy(tmp.ipaddr, ip, INET_ADDRSTRLEN); ptr = TREE_FIND(&tree, _Node, linkage, &tmp); if (!ptr) { ct_tcp = 0; ct_udp = 0; } else { ct_tcp = ptr->tcp_conn; ct_udp = ptr->udp_conn; } web_printf("%c['%s', %lu, %lu, %lu, %lu, %lu, %lu, %lu, %lu, %lu, %lu]", comma, ip, rx_bytes, tx_bytes, rp_tcp, tp_tcp, rp_udp, tp_udp, rp_icmp, tp_icmp, ct_tcp, ct_udp); comma = ','; } } fclose(a); } web_puts("];\n"); TREE_FORWARD_APPLY(&tree, _Node, linkage, Node_housekeeping, NULL); TREE_INIT(&tree, Node_compare); }
void asp_build_time(int argc, char **argv) { web_puts(tomato_buildtime); }
/* <% ident(arg, "arg", 'arg'); %> Syntax checking is very relaxed and all arguments are considered a string. Example, the following are the same: <% ident(foo); %> <% ident('foo'); %> */ int parse_asp(const char *path) { char *buffer; char *cp; char *a, *b, *c; char x; int argc; char *argv[32]; char *ident; const aspapi_t *api; if (f_read_alloc_string(path, &buffer, 128 * 1024) < 0) { free(buffer); if (!header_sent) send_error(500, NULL, "Read error"); return 0; } if (!header_sent) send_header(200, NULL, mime_html, 0); // <% id(arg, arg); %> cp = buffer; while (*cp) { if ((b = strstr(cp, "%>")) == NULL) { web_puts(cp); break; } *b = 0; //xx <% <% %> //xx %> a = cp; while ((c = strstr(a, "<%")) != NULL) { a = c + 2; } if (a == cp) { *b = '%'; b += 2; web_write(cp, b - cp); cp = b; continue; } web_write(cp, (a - cp) - 2); cp = b + 2; while (*a == ' ') ++a; ident = a; while (((*a >= 'a') && (*a <= 'z')) || ((*a >= 'A') && (*a <= 'Z')) || ((*a >= '0') && (*a <= '9')) || (*a == '_')) { ++a; } if (ident == a) { #ifdef DEBUG syslog(LOG_WARNING, "Identifier not found in %s @%u", path, a - buffer); #endif continue; } b = a; while (*a == ' ') ++a; if (*a++ != '(') { #ifdef DEBUG syslog(LOG_WARNING, "Expecting ( in %s @%u", path, a - buffer); #endif continue; } *b = 0; // <% foo(123, "arg"); %> // a -----^ ^--- null // printf("\n[[['%s'\n", ident); argc = 0; while (*a) { while (*a == ' ') ++a; if (*a == ')') { FINAL: ++a; while ((*a == ' ') || (*a == ';')) ++a; if (*a != 0) break; for (api = aspapi; api->name; ++api) { if (strcmp(api->name, ident) == 0) { api->exec(argc, argv); break; } } a = NULL; /* int z; for (z = 0; z < argc; ++z) { printf(" %d '%s'\n", z, argv[z]); } */ break; } if (argc >= 32) { #ifdef DEBUG syslog(LOG_WARNING, "Error while parsing arguments in %s @%u", path, a - buffer); #endif break; } if ((*a == '"') || (*a == '\'')) { x = *a; argv[argc++] = a + 1; while ((*++a != x) && (*a != 0)) { if (*a == '\\') { if (*++a == 0) break; *(a - 1) = *a; } } if (*a == 0) break; *a++ = 0; } else { argv[argc++] = a; while ((*a != ',') && (*a != ')') && (*a != ' ') && (*a != 0)) ++a; } while (*a == ' ') ++a; if (*a == ')') { *a = 0; goto FINAL; } if (*a != ',') break; *a++ = 0; } #ifdef DEBUG if (a != NULL) syslog(LOG_WARNING, "Error while parsing arguments in %s @%u", path, a - buffer); #endif // printf("argc=%d]]]\n", argc); } free(buffer); return 1; }
static void wo_blank(char *url) { web_puts("\n\n\n\n"); }
void asp_devlist(int argc, char **argv) { char *p; FILE *f; char buf[1024]; char comma; // must be here for easier call via update.cgi. arg is ignored asp_arplist(0, NULL); asp_wlnoise(0, NULL); // p = js_string(nvram_safe_get("dhcpd_static")); web_printf("dhcpd_static = '%s'.split('>');\n", p ? p : ""); free(p); // web_puts("wldev = ["); comma = ' '; foreach_wif(1, &comma, get_wl_clients); web_puts("];\n"); // unsigned long expires; char mac[32]; char ip[32]; char hostname[256]; char *host; web_puts("dhcpd_lease = ["); #ifdef TCONFIG_VLAN if ((nvram_match("lan_proto", "dhcp")) || (nvram_match("lan1_proto", "dhcp")) || (nvram_match("lan2_proto", "dhcp")) || (nvram_match("lan3_proto", "dhcp")) ) { #else if (nvram_match("lan_proto", "dhcp")) { #endif f_write("/var/tmp/dhcp/leases.!", NULL, 0, 0, 0666); // dump the leases to a file if (killall("dnsmasq", SIGUSR2) == 0) { // helper in dnsmasq will remove this when it's done f_wait_notexists("/var/tmp/dhcp/leases.!", 5); } if ((f = fopen("/var/tmp/dhcp/leases", "r")) != NULL) { comma = ' '; while (fgets(buf, sizeof(buf), f)) { if (sscanf(buf, "%lu %17s %15s %255s", &expires, mac, ip, hostname) != 4) continue; host = js_string((hostname[0] == '*') ? "" : hostname); web_printf("%c['%s','%s','%s','%s']", comma, (host ? host : ""), ip, mac, ((expires == 0) ? "non-expiring" : reltime(buf, expires))); free(host); comma = ','; } fclose(f); } unlink("/var/tmp/dhcp/leases"); } web_puts("];"); }
void asp_iptmon(int argc, char **argv) { char comma; char sa[256]; FILE *a; char *exclude; char *include; char ip[INET6_ADDRSTRLEN]; int64_t tx, rx; exclude = nvram_safe_get("cstats_exclude"); include = nvram_safe_get("cstats_include"); char br; char name[] = "/proc/net/ipt_account/lanX"; web_puts("\n\niptmon={"); comma = ' '; for(br=0 ; br<=3 ; br++) { char wholenetstatsline = 1; char bridge[2] = "0"; if (br!=0) bridge[0]+=br; else strcpy(bridge, ""); sprintf(name, "/proc/net/ipt_account/lan%s", bridge); if ((a = fopen(name, "r")) == NULL) continue; if (!wholenetstatsline) fgets(sa, sizeof(sa), a); // network while (fgets(sa, sizeof(sa), a)) { if(sscanf(sa, "ip = %s bytes_src = %llu %*u %*u %*u %*u packets_src = %*u %*u %*u %*u %*u bytes_dst = %llu %*u %*u %*u %*u packets_dst = %*u %*u %*u %*u %*u time = %*u", ip, &tx, &rx) != 3 ) continue; if (find_word(exclude, ip)) { wholenetstatsline = 0; continue; } if (((find_word(include, ip)) || (wholenetstatsline == 1)) || ((nvram_get_int("cstats_all")) && ((rx > 0) || (tx > 0)) )) { // if ((find_word(include, ip)) || (wholenetstatsline == 1)) { // if ((tx > 0) || (rx > 0) || (wholenetstatsline == 1)) { // if ((tx > 0) || (rx > 0)) { web_printf("%c'%s':{rx:0x%llx,tx:0x%llx}", comma, ip, rx, tx); comma = ','; } wholenetstatsline = 0; } fclose(a); } web_puts("};\n"); }
void asp_nv(int argc, char **argv) { if (argc == 1) { web_puts(nvram_safe_get(argv[0])); } }