1/*
2 * Copyright 2004, ASUSTek Inc.
3 * All Rights Reserved.
4 *
5 * THIS SOFTWARE IS OFFERED "AS IS", AND ASUS GRANTS NO WARRANTIES OF ANY
6 * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
7 * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
8 * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
9 *
10 * $Id: bigpond.c,v 1.1.1.1 2008/10/15 03:28:48 james26_jang Exp $
11 */
12
13#include <stdio.h>
14#include <errno.h>
15#include <syslog.h>
16#include <string.h>
17#include <stdlib.h>
18#include <unistd.h>
19#include <sys/stat.h>
20#include <sys/types.h>
21#include <signal.h>
22#include <shutils.h>
23#include <bcmnvram.h>
24#include <rc.h>
25#include <netconf.h>
26#include <nvparse.h>
27
28int
29start_bpalogin(void)
30{
31	FILE *fp;
32	int ret;
33	char authserver[20];
34	char authdomain[80];
35	char buf[254];
36
37	if(nvram_invmatch("wan_heartbeat_x", "")){
38		snprintf(authserver, sizeof(authserver), "%s", nvram_safe_get("wan_heartbeat_x"));
39		snprintf(authdomain, sizeof(authdomain), "%s", "");
40	}
41	else if(nvram_invmatch("wan0_domain", " ") && nvram_invmatch("wan0_domain", ""))
42	{
43		snprintf(authserver, sizeof(authserver), "%s", "sm-server");
44		snprintf(authdomain, sizeof(authdomain), "%s", nvram_safe_get("wan0_domain"));
45	}
46#ifdef REMOVE
47	else if(nvram_match("wan0_domain", "nsw.bigpond.net.au")){            // NSW
48		snprintf(authserver, sizeof(authserver), "%s", "spr3");
49		snprintf(authdomain, sizeof(authdomain), "%s", "nsw-remote.bigpond.net.au");
50	}
51	else if(nvram_match("wan0_domain", "vic.bigpond.net.au")){           // Victoria
52		snprintf(authserver, sizeof(authserver), "%s", "mer3");
53		snprintf(authdomain, sizeof(authdomain), "%s", "vic-remote.bigpond.net.au");
54	}
55	else if(nvram_match("wan0_domain","qld.bigpond.net.au")){            // Queensland
56		snprintf(authserver, sizeof(authserver), "%s", "bcr3");
57		snprintf(authdomain, sizeof(authdomain), "%s", "qld-remote.bigpond.net.au");
58	}
59	else if(nvram_match("wan0_domain", "sa.bigpond.net.au")){            // South Austrialia
60		snprintf(authserver, sizeof(authserver), "%s", "afr3");
61		snprintf(authdomain, sizeof(authdomain), "%s", "sa-remote.bigpond.net.au");
62	}
63	else if(nvram_match("wan0_domain", "wa.bigpond.net.au")){            // Western Austrialia
64		snprintf(authserver, sizeof(authserver), "%s", "pwr3");
65		snprintf(authdomain, sizeof(authdomain), "%s", "wa-remote.bigpond.net.au");
66	}
67#endif
68	else{
69		snprintf(authserver, sizeof(authserver), "%s", nvram_safe_get("wan0_gateway"));
70		snprintf(authdomain, sizeof(authdomain), "%s", "");
71	}
72
73	snprintf(buf, sizeof(buf), "%s%s%s", authserver, !strcmp(authdomain,"") ? "" : ".", authdomain);
74
75	nvram_set("hb_server_name", buf);
76
77	if (!(fp = fopen("/tmp/bpalogin.conf", "w"))) {
78		perror("/tmp/bpalogin.conf");
79		return errno;
80	}
81	fprintf(fp, "username %s\n", nvram_safe_get("wan_pppoe_username"));
82	fprintf(fp, "password %s\n", nvram_safe_get("wan_pppoe_passwd"));
83	fprintf(fp, "authserver %s\n", authserver);
84	if (strcmp(authdomain, "")) {
85		fprintf(fp, "authdomain %s\n", authdomain);
86	}
87	fprintf(fp, "localport 5050\n");
88	fprintf(fp, "logging syslog\n");
89	fprintf(fp, "debuglevel 0\n");
90	fprintf(fp, "minheartbeatinterval 60\n");
91	fprintf(fp, "maxheartbeatinterval 420\n");
92	fprintf(fp, "connectedprog bpa_connect\n");
93	fprintf(fp, "disconnectedprog bpa_disconnect\n");
94	fclose(fp);
95
96	//mkdir("/tmp/ppp", 0777);
97	if((fp = fopen("/tmp/bpa_connect_success", "r"))){
98		ret = eval("bpalogin", "-c", "/tmp/bpalogin.conf", "-t");
99		fclose(fp);
100	}
101	else
102		ret = eval("bpalogin", "-c", "/tmp/bpalogin.conf");
103
104	return ret;
105}
106
107int
108stop_bpalogin(void)
109{
110	int ret;
111
112	ret = eval("killall", "bpalogin");
113	ret += eval("killall", "-9", "bpalogin");
114	dprintf("done\n");
115
116	return ret;
117}
118
119int
120bpa_connect_main(int argc, char **argv)
121{
122	FILE *fp;
123	char buf[254];
124#ifdef NOIPTABLES
125	netconf_filter_t filter;
126	int log_level, log_accept;
127	char tmp[100], prefix[] = "wanXXXXXXXXXX_";
128	int i;
129
130	// Optionally log connections
131	log_level = atoi(nvram_safe_get("log_level"));
132	log_accept = (log_level & 2) ? NETCONF_LOG_ACCEPT : NETCONF_ACCEPT;
133
134	memset(&filter, 0, sizeof(filter));
135	strcpy(filter.match.in.name, nvram_safe_get("wan0_ifname"));
136	filter.target = log_accept;
137	filter.match.ipproto = IPPROTO_UDP;
138	inet_aton(nvram_safe_get("wan0_ipaddr"), &filter.match.dst.ipaddr);
139	inet_aton(nvram_safe_get("wan0_netmask"), &filter.match.dst.netmask);
140	filter.match.dst.ports[0] = htons(atoi("5050"));
141	add_forward(&filter, NETCONF_IN, log_accept);
142	nvram_set("wan_auth_t", "OK");
143#else
144	nvram_set("wan_auth_t", "OK");
145	snprintf(buf, sizeof(buf), "iptables -I INPUT -d %s -i %s -p udp --dport %d -j %s",
146		   nvram_safe_get("wan0_ipaddr"),
147		   nvram_safe_get("wan0_ifname"),
148		   5050,
149		   "ACCEPT");
150
151	system(buf);
152#endif
153	return TRUE;
154}
155
156int
157bpa_disconnect_main(int argc, char **argv)
158{
159	char tmp[100], *str;
160	int pid;
161
162#if 1 // Thanks for Oleg
163	nvram_set("wan_auth_t", "FAIL");
164#else
165
166	snprintf(tmp, sizeof(tmp), "/var/run/udhcpc%d.pid", 0);
167	if ((str = file2str(tmp))) {
168		pid = atoi(str);
169		free(str);
170		kill(pid, SIGUSR2);
171	}
172#endif
173	return TRUE;
174}
175
176