summaryrefslogtreecommitdiff
path: root/release/src/router/shared/ddns.c
blob: 63ff4fedea13e64764d1ebebfee897e252d44a1c (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332

/*
 *********************************************************
 *   Copyright 2003, CyberTAN  Inc.  All Rights Reserved *
 *********************************************************

 This is UNPUBLISHED PROPRIETARY SOURCE CODE of CyberTAN Inc.
 the contents of this file may not be disclosed to third parties,
 copied or duplicated in any form without the prior written
 permission of CyberTAN Inc.

 This software should be used as a reference only, and it not
 intended for production use!


 THIS SOFTWARE IS OFFERED "AS IS", AND CYBERTAN GRANTS NO WARRANTIES OF ANY
 KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE.  CYBERTAN
 SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
 FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <sys/socket.h>

#include <broadcom.h>

struct ddns_message {
	char *name;
	char *desc;
};


#if LANGUAGE == JAPANESE
struct ddns_message ddns_messages[] = {
	// Below is DynDNS error code
        {"dyn_good",	"DDNS の更新は完了しました"},
        {"dyn_noupdate","DDNS の更新は完了しました"},
	{"dyn_nohost",	"ホスト名が存在しません"},
	{"dyn_notfqdn",	"ドメイン名が正しくありません"},
	{"dyn_!yours",	"ユーザー名が正しくありません"},
	{"dyn_abuse",	"ホスト名は DDNS サーバによりブロックされています"},
	{"dyn_nochg",	"DDNS の更新は完了しました"},
	{"dyn_badauth",	"認証に失敗しました (ユーザー名またはパスワード)"},
	{"dyn_badsys",	"システム パラメータが不正です"},
	{"dyn_badagent","このユーザー エージェントはブロックされています"},
	{"dyn_numhost",	"ホストが多すぎるか少なすぎます"},
	{"dyn_dnserr",	"DNS エラー発生"},
	{"dyn_911",	"予期せぬエラーです。 (1)"},
	{"dyn_999",	"予期せぬエラーです。 (2)"},
	{"dyn_!donator","リクエストされた機能は寄付した場合にのみ有効です。寄付をしてください。"},
	{"dyn_strange",	"応答が不正です。接続先サーバが正しいかどうか、確認ください。"},
	{"dyn_uncode",	"DynDns からの不明なリターン コード"},

	// Below is TZO error code
        {"tzo_good",	"Operation Complete"},
        {"tzo_noupdate","Operation Complete"},
	{"tzo_notfqdn",	"Invalid Domain Name"},
        {"tzo_notmail",	"Invalis Email"},
        {"tzo_notact",	"Invalid Action"},
        {"tzo_notkey",	"Invalid Key"},
        {"tzo_notip",	"Invalid IP address"},
        {"tzo_dupfqdn",	"Duplicate Domain Name"},
        {"tzo_fqdncre",	"Domain Name has already been created for this domain name"},
        {"tzo_expired",	"The account has expired"},
        {"tzo_error",	"An unexpected server error"},

	// Below is for all
	{"all_closed",	"DDNS サーバは現在クローズしています"},
	{"all_resolving",	"ドメイン名を解決中"},
	{"all_errresolv",	"ドメイン名の解決に失敗しました。"},
	{"all_connecting",	"サーバへ接続中"},
	{"all_connectfail",	"サーバへの接続に失敗しました。"},
	{"all_disabled",	"DDNS は無効です"},
	{"all_noip",		"インターネット接続がありません"},
};
#else
struct ddns_message ddns_messages[] = {
	// Below is DynDNS error code
        {"dyn_good",	"DDNS is updated successfully"},
        {"dyn_noupdate","DDNS is updated successfully"},
	{"dyn_nohost",	"The hostname does not exist"},
	{"dyn_notfqdn",	"Domain Name is not correct"},
	{"dyn_!yours",	"Username is not correct"},
	{"dyn_abuse",	"The hostname is blocked by the DDNS server"},
	{"dyn_nochg",	"DDNS is updated successfully"},
	{"dyn_badauth",	"Authorization fails (username or passwords)"},
	{"dyn_badsys",	"The system parameters are invalid"},
	{"dyn_badagent","This useragent has been blocked"},
	{"dyn_numhost",	"Too many or too few hosts found"},
	{"dyn_dnserr",	"DNS error encountered"},
	{"dyn_911",	"An unexpected error (1)"},
	{"dyn_999",	"An unexpected error (2)"},
	{"dyn_!donator","A feature requested is only available to donators, please donate"},
	{"dyn_strange",	"Strange server response, are you connecting to the right server?"},
	{"dyn_uncode",	"Unknown return code"},

	// Below is TZO error code
        {"tzo_good",	"Operation Complete"},
	{"tzo_noupdate","Operation Complete"},
	{"tzo_notfqdn",	"Invalid Domain Name"},
        {"tzo_notmail",	"Invalis Email"},
        {"tzo_notact",	"Invalid Action"},
        {"tzo_notkey",	"Invalid Key"},
        {"tzo_notip",	"Invalid IP address"},
        {"tzo_dupfqdn",	"Duplicate Domain Name"},
        {"tzo_fqdncre",	"Domain Name has already been created for this domain name"},
        {"tzo_expired",	"The account has expired"},
        {"tzo_error",	"An unexpected server error"},

	// Below is for all
	{"all_closed",	"DDNS server is currently closed"},
	{"all_resolving",	"Resolving domain name"},
	{"all_errresolv",	"Domain name resolv fail"},
	{"all_connecting",	"Connecting to server"},
	{"all_connectfail",	"Connect to server fail"},
	{"all_disabled",	"DDNS function is disabled"},
	{"all_noip",	"No Internet connection"},
};
#endif

char *
convert(char *msg)
{
	static char buf[200];
//dyn_!yours       dyn_!donator
	cprintf("931021 Amin ddns.c for multilang support\r\n");
	if( !strcmp(msg, "dyn_!yours"))
		snprintf(buf, sizeof(buf), "ddnsm.dyn_yours");
	else if( !strcmp(msg, "dyn_!donator"))
		snprintf(buf, sizeof(buf), "ddnsm.dyn_donator");
	else
		snprintf(buf, sizeof(buf), "ddnsm.%s", msg);
	return buf;
}

int
ej_show_ddns_status(int eid, webs_t wp, int argc, char_t **argv)
{
	char string[80]="";
	int ret = 0;
	char *enable = websGetVar(wp, "ddns_enable", NULL);
	
	if(!enable)
		enable = nvram_safe_get("ddns_enable");		// for first time

	if(strcmp(nvram_safe_get("ddns_enable"),enable))	// change service
		return websWrite(wp," ");

	if(nvram_match("ddns_enable","0")) // only for no hidden page
		return websWrite(wp, "%s", convert("all_disabled"));

	if(!check_wan_link(0))
		return websWrite(wp, "%s", convert("all_noip"));

	if(file_to_buf("/tmp/ddns_msg", string, sizeof(string))){
		if(!strcmp(string, "")){
			if(nvram_match("ddns_status","1")){
				if(nvram_match("ddns_enable","1"))
					ret = websWrite(wp, "%s", convert("dyn_good"));	// dyndns
				else
					ret = websWrite(wp, "%s", convert("tzo_good"));	// TZO
			}
			else
				ret = websWrite(wp, "%s", convert("all_closed"));
		}
		else
			ret = websWrite(wp, "%s", convert(string));
	}

	return ret;
}

int
ddns_save_value(webs_t wp)
{
	char *enable, *username, *passwd, *hostname;
	char *mx=NULL, *backmx=NULL, *wildcard=NULL, *service=NULL;     // Only for DynDNS
	struct variable ddns_variables[] = {
		{ longname: "DDNS enable", argv: ARGV("0","1","2") },
		{ longname: "DDNS password", argv: ARGV("30") },
		{ longname: "DDNS hostname", argv: ARGV("128") },
		{ longname: "DDNS MX", argv: ARGV("63") },	// Only for DynDNS
		{ longname: "DDNS BACKMX", argv: ARGV("YES","NO") },	// Only for DynDNS
		{ longname: "DDNS wildcard", argv: ARGV("ON","OFF") },	// Only for DynDNS
		{ longname: "DDNS service", argv: ARGV("dyndns","dyndns-static","dyndns-custom") },	// Only for DynDNS
	}, *which;

	int ret=-1;
	char _username[] = "ddns_username_X";
	char _passwd[] = "ddns_passwd_X";
	char _hostname[] = "ddns_hostname_X";
			
	which = &ddns_variables[0];

	enable = websGetVar(wp, "ddns_enable", NULL);
	if(!enable && !valid_choice(wp, enable, &which[0])){
		error_value = 1;
		return 1;
	}

	if(atoi(enable) == 0)	{	// Disable
		nvram_set("ddns_enable_buf", nvram_safe_get("ddns_enable"));
		nvram_set("ddns_enable", enable);
		return 1;
	}
	else if(atoi(enable) == 1){	// dyndns
		snprintf(_username, sizeof(_username),"%s","ddns_username");
		snprintf(_passwd, sizeof(_passwd),"%s","ddns_passwd");
		snprintf(_hostname, sizeof(_hostname),"%s","ddns_hostname");
		mx = websGetVar(wp, "ddns_mx", NULL);
		backmx = websGetVar(wp, "ddns_backmx", NULL);
		wildcard = websGetVar(wp, "ddns_wildcard", NULL);
		service = websGetVar(wp, "ddns_service", NULL);
	}
	else {	// tzo
		snprintf(_username, sizeof(_username),"ddns_username_%s",enable);
		snprintf(_passwd, sizeof(_passwd),"ddns_passwd_%s",enable);
		snprintf(_hostname, sizeof(_hostname),"ddns_hostname_%s",enable);
	}

	username = websGetVar(wp, _username, NULL);
	passwd = websGetVar(wp, _passwd, NULL);
	hostname = websGetVar(wp, _hostname, NULL);

	if(!username || !passwd || !hostname){
		error_value = 1;
		return 1;
	}

	if(hostname && *hostname && !valid_name(wp, hostname, &which[2])) {
		error_value = 1;
		return 1;
	}

	if(mx && *mx && !valid_name(wp, mx, &which[3])) {
		error_value = 1;
		return 1;
	}

	if(backmx && !valid_choice(wp, backmx, &which[4])) {
		error_value = 1;
		return 1;
	}

	if(wildcard && !valid_choice(wp, wildcard, &which[5])) {
		error_value = 1;
		return 1;
	}

	if(service && !valid_choice(wp, service, &which[6])) {
		error_value = 1;
		return 1;
	}
	
	

	nvram_set("ddns_enable_buf", nvram_safe_get("ddns_enable"));
	nvram_set("ddns_enable", enable);
	nvram_set("ddns_username_buf", nvram_safe_get(_username));
	nvram_set("ddns_passwd_buf", nvram_safe_get(_passwd));
	nvram_set("ddns_hostname_buf", nvram_safe_get(_hostname));
	if (strcmp(passwd, TMP_PASSWD))
		nvram_set(_passwd, passwd);
	nvram_set(_username, username);

	if(hostname)	nvram_set(_hostname, hostname);

	if(mx)		nvram_set("ddns_mx", mx);
	if(backmx)	nvram_set("ddns_backmx", backmx);
	if(wildcard)	nvram_set("ddns_wildcard", wildcard);
	if(service)	nvram_set("ddns_service", service);

	return ret;
}

int
ddns_update_value(webs_t wp)
{
	return 1;
}

int
ej_show_ddns_ip(int eid, webs_t wp, int argc, char_t **argv)
{
	
	if(check_wan_link(0)) {
		char *ddns_enable = GOZILA_GET("ddns_enable");
		
		if(!strcmp(ddns_enable, "2")) {	// TZO
			if(!nvram_get("public_ip") || nvram_match("public_ip", ""))
				system("ddns_checkip -t tzo-echo -n public_ip");
			if(nvram_match("public_ip", ""))
				return websWrite(wp,"0.0.0.0");
			else
				return websWrite(wp,"%s",nvram_safe_get("public_ip"));
		}
		else {
			if(nvram_match("wan_proto","l2tp"))
				return websWrite(wp,"%s",nvram_safe_get("l2tp_get_ip"));
			else if(nvram_match("wan_proto","pptp"))
				return websWrite(wp,"%s",nvram_safe_get("pptp_get_ip"));
			else
				return websWrite(wp,"%s",nvram_safe_get("wan_ipaddr"));
		}
	}
	else
		return websWrite(wp,"0.0.0.0");

}

int
ej_show_ddns_setting(int eid, webs_t wp, int argc, char_t **argv)
{
	int ret = 0;

        char *ddns_enable = GOZILA_GET("ddns_enable"); 

	if(!strcmp(ddns_enable, "1"))
		do_ej("dyndns.asp", wp);

	else if(!strcmp(ddns_enable, "2"))
		do_ej("tzo.asp", wp);

	return ret;
}