• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/router/lighttpd-1.4.39/src/
1#include "base.h"
2#include "log.h"
3#include "buffer.h"
4
5#include "plugin.h"
6
7#include <ctype.h>
8#include <stdlib.h>
9#include <string.h>
10#include <dlinklist.h>
11
12#if EMBEDDED_EANBLE
13#ifndef APP_IPKG
14#include "disk_share.h"
15#endif
16#endif
17
18#define DBE 1
19
20typedef struct {
21	array *alias_url;
22} plugin_config;
23
24typedef struct {
25	PLUGIN_DATA;
26
27	plugin_config **config_storage;
28
29	plugin_config conf;
30} plugin_data;
31
32INIT_FUNC(mod_aidisk_access_init) {
33	plugin_data *p;
34
35	p = calloc(1, sizeof(*p));
36
37	return p;
38}
39
40FREE_FUNC(mod_aidisk_access_free) {
41	plugin_data *p = p_d;
42
43	UNUSED(srv);
44
45	if (!p) return HANDLER_GO_ON;
46
47	if (p->config_storage) {
48		size_t i;
49		for (i = 0; i < srv->config_context->used; i++) {
50			plugin_config *s = p->config_storage[i];
51
52			array_free(s->alias_url);
53
54			free(s);
55		}
56		free(p->config_storage);
57	}
58
59	free(p);
60
61	return HANDLER_GO_ON;
62}
63
64SETDEFAULTS_FUNC(mod_aidisk_access_set_defaults) {
65	plugin_data *p = p_d;
66	size_t i = 0;
67
68	config_values_t cv[] = {
69		{ "alias.url",                  NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION },
70		{ NULL,                          NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
71	};
72
73	p->config_storage = calloc(1, srv->config_context->used * sizeof(specific_config *));
74
75	for (i = 0; i < srv->config_context->used; i++) {
76		plugin_config *s;
77
78		s = calloc(1, sizeof(plugin_config));
79		s->alias_url    = array_init();
80
81		cv[0].destination = s->alias_url;
82
83		p->config_storage[i] = s;
84
85		if (0 != config_insert_values_global(srv, ((data_config *)srv->config_context->data[i])->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) {
86			return HANDLER_ERROR;
87		}
88	}
89
90	return HANDLER_GO_ON;
91}
92
93#define PATCH(x) \
94	p->conf.x = s->x;
95static int mod_aidisk_access_patch_connection(server *srv, connection *con, plugin_data *p) {
96	size_t i, j;
97	plugin_config *s = p->config_storage[0];
98
99	PATCH(alias_url);
100
101#if EMBEDDED_EANBLE
102	char *usbdiskname = nvram_get_productid();
103#else
104	char *usbdiskname = "usbdisk";
105#endif
106
107	/* skip the first, the global context */
108	for (i = 1; i < srv->config_context->used; i++) {
109		data_config *dc = (data_config *)srv->config_context->data[i];
110		s = p->config_storage[i];
111
112		if( dc->comp != COMP_HTTP_URL )
113			continue;
114
115		if(strstr(dc->string->ptr, usbdiskname)==NULL)
116			continue;
117
118		for (j = 0; j < dc->value->used; j++) {
119			data_unset *du = dc->value->data[j];
120
121			if (buffer_is_equal_string(du->key, CONST_STR_LEN("alias.url"))) {
122				PATCH(alias_url);
123			}
124		}
125	}
126#if EMBEDDED_EANBLE
127#ifdef APP_IPKG
128	free(usbdiskname);
129#endif
130#endif
131
132	return 0;
133}
134#undef PATCH
135
136URIHANDLER_FUNC(mod_aidisk_access_physical_handler){
137	plugin_data *p = p_d;
138	int s_len;
139	size_t k;
140
141	if (con->mode != SMB_BASIC&&con->mode != DIRECT) return HANDLER_GO_ON;
142	if (con->uri.path->used == 0) return HANDLER_GO_ON;
143
144	mod_aidisk_access_patch_connection(srv, con, p);
145
146	buffer* buffer_mnt_path = buffer_init();
147
148#if EMBEDDED_EANBLE
149	char *usbdiskname = nvram_get_productid();
150#else
151	char *usbdiskname = "usbdisk";
152#endif
153
154	int bFound = 0;
155	for (k = 0; k < p->conf.alias_url->used; k++) {
156		data_string *ds = (data_string *)p->conf.alias_url->data[k];
157
158		if(strstr(ds->key->ptr, usbdiskname)){
159			buffer_copy_buffer(buffer_mnt_path, ds->value);
160			buffer_append_string(buffer_mnt_path, "/");
161			bFound = 1;
162			break;
163		}
164	}
165
166#if EMBEDDED_EANBLE
167#ifdef APP_IPKG
168	free(usbdiskname);
169#endif
170#endif
171
172	//- Check is the mount path
173	if( bFound == 0 || strncmp( con->physical.path->ptr, buffer_mnt_path->ptr, buffer_mnt_path->used-1) != 0 ){
174		buffer_free(buffer_mnt_path);
175		return HANDLER_GO_ON;
176	}
177
178	char *auth_username = NULL;
179	char *auth_password = NULL;
180
181	data_string *ds = (data_string *)array_get_element(con->request.headers, "user-Agent");
182
183	smb_info_t *smb_info = NULL;
184	int b_save_smb_info = 0;
185
186#if 1
187	if( isBrowser(con) == 1 ){
188
189		for (smb_info = srv->smb_srv_info_list; smb_info; smb_info = smb_info->next) {
190
191			Cdbg(DBE, "smb_info->user_agent=[%s], ds->value=[%s]",
192					smb_info->user_agent->ptr,
193					ds->value->ptr);
194
195			Cdbg(DBE, "smb_info->src_ip=[%s], con->dst_addr_buf=[%s]",
196					smb_info->src_ip->ptr,
197					con->dst_addr_buf->ptr);
198
199			Cdbg(DBE, "smb_info->server=[%s]",
200					smb_info->server->ptr);
201
202			Cdbg(DBE, "smb_info->asus_token=[%s]",
203					smb_info->asus_token->ptr);
204
205			if( buffer_is_equal(smb_info->asus_token, con->asus_token) &&
206				buffer_is_equal(smb_info->user_agent, ds->value) &&
207			    buffer_is_equal(smb_info->src_ip, con->dst_addr_buf) &&
208			    ( buffer_is_equal_string(smb_info->server, CONST_STR_LEN("aidisk")) ||
209			      buffer_is_empty(smb_info->server) )){
210
211				Cdbg(DBE, "server=[%s], user=[%s], pass=[%s]",
212					smb_info->server->ptr, smb_info->username->ptr, smb_info->password->ptr);
213
214				break;
215			}
216
217		}
218
219	}
220#endif
221
222	if( smbc_parser_basic_authentication(srv, con, &auth_username, &auth_password) != 1 ){
223		if(smb_info==NULL){
224			Cdbg(DBE, "smbc_parser_basic_authentication fail 1");
225			buffer_free(buffer_mnt_path);
226			smbc_wrapper_response_401(srv, con);
227			return HANDLER_FINISHED;
228		}
229
230		if(smb_info->username->ptr==NULL || smb_info->password->ptr==NULL){
231			Cdbg(DBE, "smbc_parser_basic_authentication fail 2");
232			buffer_free(buffer_mnt_path);
233			smbc_wrapper_response_401(srv, con);
234			return HANDLER_FINISHED;
235		}
236
237		auth_username = (char*)malloc(smb_info->username->used);
238		strcpy(auth_username, smb_info->username->ptr);
239
240		auth_password = (char*)malloc(smb_info->password->used);
241		strcpy(auth_password, smb_info->password->ptr);
242	}
243
244
245	int st_samba_mode = 0;
246
247#if EMBEDDED_EANBLE
248	st_samba_mode = nvram_get_st_samba_mode();
249#endif
250
251	//- Share All, use guest account, no need account and password.
252	if(st_samba_mode==1){
253		buffer_copy_string(con->aidisk_username, "guest");
254		buffer_copy_string(con->aidisk_passwd, "");
255	}
256	else{
257		//if( smbc_acc_account_authentication(con, auth_username, auth_password) != 1 ){
258		if(do_account_authentication(auth_username, auth_password) != 1){
259			Cdbg(DBE, "smbc_acc_account_authentication fail");
260			free(auth_username);
261			free(auth_password);
262			buffer_free(buffer_mnt_path);
263			smbc_wrapper_response_401(srv, con);
264			return HANDLER_FINISHED;
265		}
266
267		buffer_copy_string(con->aidisk_username, auth_username);
268		buffer_copy_string(con->aidisk_passwd, auth_password);
269	}
270
271	//- Save username password
272	if( smb_info == NULL ){
273		smb_info = calloc(1, sizeof(smb_info_t));
274		smb_info->username = buffer_init();
275		smb_info->password = buffer_init();
276		smb_info->server = buffer_init();
277		smb_info->user_agent = buffer_init();
278		smb_info->src_ip = buffer_init();
279		smb_info->asus_token = buffer_init();
280
281		if(ds)
282			buffer_copy_buffer(smb_info->user_agent, ds->value);
283		else
284			buffer_copy_string(smb_info->user_agent, "");
285
286		buffer_copy_buffer(smb_info->src_ip, con->dst_addr_buf);
287		buffer_copy_string(smb_info->server, "aidisk");
288		buffer_copy_string(smb_info->username, auth_username);
289		buffer_copy_string(smb_info->password, auth_password);
290		buffer_copy_buffer(smb_info->asus_token, con->asus_token);
291
292		//DLIST_ADD(srv->aidisk_info_list, smb_info);
293		DLIST_ADD(srv->smb_srv_info_list, smb_info);
294	}
295
296	int denied = 0;
297	int permission = -1;
298	char* usbdisk_path = NULL;
299	char* usbdisk_share_folder = NULL;
300	smbc_parse_mnt_path(con->physical.path->ptr,
301						buffer_mnt_path->ptr,
302						buffer_mnt_path->used-1,
303						&usbdisk_path,
304						&usbdisk_share_folder);
305
306	Cdbg(DBE, "con->aidisk_username=%s, usbdisk_path=%s, usbdisk_share_folder=%s", con->aidisk_username->ptr, usbdisk_path, usbdisk_share_folder);
307#if EMBEDDED_EANBLE
308	permission = smbc_get_usbdisk_permission(con->aidisk_username->ptr, usbdisk_path, usbdisk_share_folder);
309#else
310	//- For test
311	permission = 3;
312#endif
313
314	if( ( permission == 1 && con->request.http_method == HTTP_METHOD_GET ) ||
315	      permission == 3 ||
316	      con->request.http_method == HTTP_METHOD_PROPFIND ||
317	      con->request.http_method == HTTP_METHOD_GSL )
318		denied = 0;
319	else
320		denied = 1;
321
322#if EMBEDDED_EANBLE
323	if( con->share_link_type==1 && !con->srv_socket->is_ssl){
324		Cdbg(DBE, "con->share_link_type=%d", con->share_link_type);
325		denied = 1;
326	}
327#endif
328
329	if(strstr(con->physical.path->ptr, "asusware")!=NULL){
330		Cdbg(DBE, "2");
331		denied = 1;
332	}
333
334	Cdbg(DBE, "con->physical.path = %s, usbdisk_path = %s, usbdisk_share_folder=%s, permission=%d, denied=%d, con->request.http_method=%s, aidisk_username=%s",
335		con->physical.path->ptr,
336		usbdisk_path,
337		usbdisk_share_folder,
338		permission, denied,
339		get_http_method_name(con->request.http_method),
340		con->aidisk_username->ptr);
341
342	buffer_free(buffer_mnt_path);
343	free(usbdisk_path);
344	free(usbdisk_share_folder);
345	free(auth_username);
346	free(auth_password);
347
348	if (denied) {
349		con->http_status = 403;
350		con->mode = DIRECT;
351		return HANDLER_FINISHED;
352	}
353
354	if(con->request.http_method == HTTP_METHOD_GET){
355		log_sys_write(srv, "sbss", "Download cloud disk file", con->url.rel_path, "from ip", con->dst_addr_buf->ptr);
356	}
357
358	Cdbg(DBE, "Allow to access this file!");
359
360	/* not found */
361	return HANDLER_GO_ON;
362}
363#ifndef APP_IPKG
364int mod_aidisk_access_plugin_init(plugin *p);
365int mod_aidisk_access_plugin_init(plugin *p) {
366	p->version     = LIGHTTPD_VERSION_ID;
367	p->name        = buffer_init_string("smbdav_access");
368
369	p->init        = mod_aidisk_access_init;
370	p->set_defaults = mod_aidisk_access_set_defaults;
371	//p->handle_uri_clean = mod_aidisk_access_uri_handler;
372	p->handle_physical = mod_aidisk_access_physical_handler;
373	//p->handle_subrequest_start  = mod_aidisk_access_uri_handler;
374	p->cleanup     = mod_aidisk_access_free;
375
376	p->data        = NULL;
377
378	return 0;
379}
380#else
381int aicloud_mod_aidisk_access_plugin_init(plugin *p);
382int aicloud_mod_aidisk_access_plugin_init(plugin *p) {
383	p->version     = LIGHTTPD_VERSION_ID;
384	p->name        = buffer_init_string("smbdav_access");
385
386	p->init        = mod_aidisk_access_init;
387	p->set_defaults = mod_aidisk_access_set_defaults;
388	//p->handle_uri_clean = mod_aidisk_access_uri_handler;
389	p->handle_physical = mod_aidisk_access_physical_handler;
390	//p->handle_subrequest_start  = mod_aidisk_access_uri_handler;
391	p->cleanup     = mod_aidisk_access_free;
392
393	p->data        = NULL;
394
395	return 0;
396}
397#endif