1/*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License as
4 * published by the Free Software Foundation; either version 2 of
5 * the License, or (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
15 * MA 02111-1307 USA
16 */
17
18#include <stdio.h>
19#include <string.h>
20#include <stdlib.h>
21#include <dirent.h>
22#include <unistd.h>
23#ifndef APP_IPKG
24#include <bcmnvram.h>
25#include <shutils.h>
26#include <rtconfig.h>
27
28#include "usb_info.h"
29#include "disk_initial.h"
30#include "disk_share.h"
31#else
32typedef unsigned char u8;
33typedef unsigned short u16;
34typedef unsigned int u32;
35typedef unsigned long long u64;
36typedef struct disk_info disk_info_t;
37typedef struct partition_info partition_info_t;
38
39#pragma pack(1) // let struct be neat by byte.
40struct disk_info{
41        char *tag;
42        char *vendor;
43        char *model;
44        char *device;
45        u32 major;
46        u32 minor;
47        char *port;
48        u32 partition_number;
49        u32 mounted_number;
50        u64 size_in_kilobytes;
51        partition_info_t *partitions;
52        disk_info_t *next;
53} ;
54
55struct partition_info{
56        char *device;
57        char *label;
58        u32 partition_order;
59        char *mount_point;
60        char *file_system;
61        char *permission;
62        u64 size_in_kilobytes;
63        u64 used_kilobytes;
64        disk_info_t *disk;
65        partition_info_t *next;
66} ;
67
68#endif
69
70
71#define WEBDAV_CONF "/tmp/lighttpd.conf"
72#if 0
73#define PRODUCTID "productid"
74#endif
75#define WEBDAV_HTTP_PORT "webdav_http_port"
76#define WEBDAV_HTTPS_PORT "webdav_https_port"
77#if 0
78char* get_productid()
79{
80   nvram_get(PRODUCTID);
81}
82#endif
83#ifdef APP_IPKG
84char *port_get(char *name);
85
86char *get_productid(void)
87{
88        char *productid = port_get("productid");
89
90        char *odmpid = port_get("odmpid");
91    if(odmpid != NULL)
92    {
93        if (*odmpid)
94            productid = odmpid;
95    }
96        return productid;
97}
98
99char *port_get(char *name)
100{
101	char tmp_name[256]="/opt/etc/apps_asus_script/aicloud_nvram_check.sh";
102        //char tmp_name[256]="/tmp/aicloud_nvram_check.sh";
103        char *cmd_name;
104        cmd_name=(char *)malloc(sizeof(char)*(strlen(tmp_name)+strlen(name)+2));
105        memset(cmd_name,0,sizeof(cmd_name));
106        sprintf(cmd_name,"%s %s",tmp_name,name);
107        system(cmd_name);
108        free(cmd_name);
109
110        while(-1!=access("/tmp/aicloud_check.control",F_OK))
111            usleep(50);
112    printf("name = %s\n",name);
113    FILE *fp;
114    if((fp=fopen("/tmp/webDAV.conf","r+"))==NULL)
115    {
116        return NULL;
117    }
118    char *value;
119    value=(char *)malloc(256);
120    memset(value,'\0',sizeof(value));
121    char tmp[256]={0};
122    while(!feof(fp)){
123        memset(tmp,0,sizeof(tmp));
124        fgets(tmp,sizeof(tmp),fp);
125        if(strncmp(tmp,name,strlen(name))==0)
126        {
127            char *p=NULL;
128            p=strchr(tmp,'=');
129            p++;
130            strcpy(value,p);
131            printf("name = %s,len =%d\n",value,strlen(value));
132            if(value[strlen(value)-1]=='\n')
133                value[strlen(value)-1]='\0';
134        }
135    }
136    fclose(fp);
137    return value;
138}
139
140int webdav_match(char *name,int id)
141{
142	char tmp_name[256]="/opt/etc/apps_asus_script/aicloud_nvram_check.sh";
143        //char tmp_name[256]="/tmp/aicloud_nvram_check.sh";
144        char *cmd_name;
145        cmd_name=(char *)malloc(sizeof(char)*(strlen(tmp_name)+strlen(name)+2));
146        memset(cmd_name,0,sizeof(cmd_name));
147        sprintf(cmd_name,"%s %s",tmp_name,name);
148        system(cmd_name);
149        free(cmd_name);
150
151        while(-1!=access("/tmp/aicloud_check.control",F_OK))
152            usleep(50);
153
154    FILE *fp;
155    if((fp=fopen("/tmp/webDAV.conf","r+"))==NULL)
156    {
157        return 0;
158    }
159    printf("name = %s,id = %d\n",name,id);
160    char tmp[256]={0};
161    while(!feof(fp)){
162        memset(tmp,0,sizeof(tmp));
163        fgets(tmp,sizeof(tmp),fp);
164        if(strncmp(tmp,name,strlen(name))==0)
165        {
166            printf("tmp = %s\n",tmp);
167            int size;
168            char *p=NULL;
169            p=strchr(tmp,'=');
170            p++;
171            size=atoi(p);
172            if(size==id)
173            {
174                printf("return 1\n");
175                fclose(fp);
176                return 1;
177            }
178            else
179            {
180                fclose(fp);
181                return 0;
182            }
183        }
184    }
185    fclose(fp);
186    return 0;
187}
188#else	/* ! APP_IPKG */
189#include <shared.h>
190#endif
191
192char *get_webdav_http_port(void)
193{
194#ifndef APP_IPKG
195	return nvram_get(WEBDAV_HTTP_PORT);
196#else
197	return port_get(WEBDAV_HTTP_PORT);
198#endif
199}
200
201char *get_webdav_https_port(void)
202{
203#ifndef APP_IPKG
204	return nvram_get(WEBDAV_HTTPS_PORT);
205#else
206	return port_get(WEBDAV_HTTPS_PORT);
207#endif
208}
209
210int main(int argc, char *argv[]) {
211	FILE *fp;
212//	int n=0, sh_num=0;
213	disk_info_t /**follow_disk,*/ *disks_info = NULL;
214//	partition_info_t *follow_partition;
215
216	/*
217		st_webdav_mpde = 1 => share mode (default)
218		st_webdav_mode = 2 => account mode
219	*/
220
221	/* write /tmp/lighttpd.conf */
222	if ((fp=fopen(WEBDAV_CONF, "r"))) {
223		fclose(fp);
224		unlink(WEBDAV_CONF);
225	}
226
227	fp = fopen(WEBDAV_CONF, "w");
228	if (fp==NULL) return -1;
229
230	/* Load modules */
231	fprintf(fp, "server.modules+=(\"mod_aicloud_invite\")\n");
232	fprintf(fp, "server.modules+=(\"mod_aicloud_auth\")\n");
233#ifndef APP_IPKG
234	fprintf(fp, "server.modules+=(\"mod_alias\")\n");
235	//fprintf(fp, "server.modules+=(\"mod_userdir\")\n");
236	fprintf(fp, "server.modules+=(\"mod_aidisk_access\")\n");
237	fprintf(fp, "server.modules+=(\"mod_aicloud_sharelink\")\n");
238	//fprintf(fp, "server.modules+=(\"mod_create_captcha_image\")\n");
239	fprintf(fp, "server.modules+=(\"mod_query_field_json\")\n");
240	fprintf(fp, "server.modules+=(\"mod_webdav\")\n");
241	fprintf(fp, "server.modules+=(\"mod_smbdav\")\n");
242	//fprintf(fp, "server.modules+=(\"mod_redirect\")\n");
243	fprintf(fp, "server.modules+=(\"mod_compress\")\n");
244	//fprintf(fp, "server.modules+=(\"mod_usertrack\")\n");
245	//fprintf(fp, "server.modules+=(\"mod_rewrite\")\n");
246
247	if (nvram_match("st_webdav_mode", "2")){
248		fprintf(fp, "server.modules+=(\"mod_access\")\n");
249		fprintf(fp, "server.modules+=(\"mod_auth\")\n");
250	}
251#else
252	fprintf(fp, "server.modules+=(\"aicloud_mod_alias\")\n");
253    //fprintf(fp, "server.modules+=(\"aicloud_mod_userdir\")\n");
254    fprintf(fp, "server.modules+=(\"aicloud_mod_aidisk_access\")\n");
255    fprintf(fp, "server.modules+=(\"aicloud_mod_aicloud_sharelink\")\n");
256    //fprintf(fp, "server.modules+=(\"aicloud_mod_create_captcha_image\")\n");
257    fprintf(fp, "server.modules+=(\"aicloud_mod_query_field_json\")\n");
258	fprintf(fp, "server.modules+=(\"aicloud_mod_webdav\")\n");
259    fprintf(fp, "server.modules+=(\"aicloud_mod_smbdav\")\n");
260    //fprintf(fp, "server.modules+=(\"aicloud_mod_redirect\")\n");
261    fprintf(fp, "server.modules+=(\"aicloud_mod_compress\")\n");
262    //fprintf(fp, "server.modules+=(\"aicloud_mod_usertrack\")\n");
263    //fprintf(fp, "server.modules+=(\"aicloud_mod_rewrite\")\n");
264
265    if (webdav_match("st_webdav_mode", 2)){
266        fprintf(fp, "server.modules+=(\"aicloud_mod_access\")\n");
267        fprintf(fp, "server.modules+=(\"aicloud_mod_auth\")\n");
268	}
269#endif
270	/* Basic setting */
271	fprintf(fp, "server.port=%s\n",get_webdav_http_port()); // defult setting, but no use
272//	fprintf(fp, "server.port=8999\n"); // defult setting, but no use
273//	fprintf(fp, "server.document-root=\"/mnt/\"\n");
274#ifdef APP_IPKG
275	fprintf(fp, "server.event-handler = \"poll\"\n");
276#endif
277	fprintf(fp, "server.document-root=\"/tmp/lighttpd/www\"\n");
278	fprintf(fp, "server.upload-dirs=(\"/tmp/lighttpd/uploads\")\n");
279	fprintf(fp, "server.errorlog=\"/tmp/lighttpd/err.log\"\n");
280	fprintf(fp, "server.pid-file=\"/tmp/lighttpd/lighttpd.pid\"\n");
281	fprintf(fp, "server.arpping-interface=\"br0\"\n");
282#ifndef APP_IPKG
283	fprintf(fp, "server.errorfile-prefix=\"/usr/lighttpd/css/status-\"\n");
284#else
285	fprintf(fp, "server.errorfile-prefix=\"/opt/etc/aicloud_UI/css/status-\"\n");
286#endif
287	fprintf(fp, "dir-listing.activate=\"disable\"\n");
288    fprintf(fp, "server.syslog=\"/tmp/lighttpd/syslog.log\"\n");
289
290	//	**** Minetype setting **** //
291	fprintf(fp, "mimetype.assign = (\n");
292	fprintf(fp, "\".html\" => \"text/html\",\n");
293	fprintf(fp, "\".htm\" => \"text/html\",\n");
294	fprintf(fp, "\".css\" => \"text/css\",\n");
295	fprintf(fp, "\".js\" => \"text/javascript\",\n");
296
297	fprintf(fp, "\".swf\" => \"application/x-shockwave-flash\",\n");
298	//fprintf(fp, "\".txt\" => \"text/plain\",\n");
299	//fprintf(fp, "\".jpg\" => \"image/jpeg\",\n");
300	//fprintf(fp, "\".gif\" => \"image/gif\",\n");
301	//fprintf(fp, "\".png\" => \"image/png\",\n");
302	//fprintf(fp, "\".pdf\" => \"application/pdf\",\n");
303	//fprintf(fp, "\".mp4\" => \"video/mp4\",\n");
304	//fprintf(fp, "\".m4v\" => \"video/mp4\",\n");
305	//fprintf(fp, "\".wmv\" => \"video/wmv\",\n");
306	//fprintf(fp, "\".mp3\" => \"audio/mpeg\",\n");
307	//fprintf(fp, "\".avi\" => \"video/avi\",\n");
308	//fprintf(fp, "\".mov\" => \"video/mov\"");
309	fprintf(fp, "\"\" => \"application/x-octet-stream\"");
310	fprintf(fp, ")\n");
311
312	// **** Index file names **** //
313	fprintf(fp, "index-file.names = ( \"index.php\", \"index.html\",\n");
314	fprintf(fp, "\"index.htm\", \"default.htm\",\n");
315	fprintf(fp, " \" index.lighttpd.html\" )\n");
316
317	// **** url access deny
318	fprintf(fp, " url.access-deny             = ( \"~\", \".inc\" )\n");
319
320	// **** static-file.exclude extensions
321	fprintf(fp," static-file.exclude-extensions = ( \".php\", \".pl\", \".fcgi\" )\n");
322
323	// ****
324	fprintf(fp, "compress.cache-dir          = \"/tmp/lighttpd/compress/\"\n");
325	fprintf(fp, "compress.filetype           = ( \"application/x-javascript\", \"text/css\", \"text/html\", \"text/plain\" )\n");
326
327	fprintf(fp, "aicloud.max-sharelink             = 15\n");
328
329	// **** SambaDav setting
330	fprintf(fp, "$SERVER[\"socket\"]==\":%s\"{\n", get_webdav_http_port());
331//	fprintf(fp, "$SERVER[\"socket\"]==\":8999\"{\n");
332//	fprintf(fp, "	alias.url=(\"/webdav\"=>\"/mnt/\")\n");
333//	fprintf(fp, "   $HTTP[\"url\"]=~\"^/usbdisk($|/)\"{\n");
334
335	fprintf(fp, "   url.aicloud-auth-deny = (\"query_field.json\")\n");
336
337	fprintf(fp, "   $HTTP[\"url\"]=~\"^/%s($|/)\"{\n",get_productid());
338    fprintf(fp, "       server.document-root = \"/\"\n");
339//	fprintf(fp, "       alias.url=(\"/usbdisk\"=>\"/mnt\")\n");
340	fprintf(fp, "       alias.url=(\"/%s\"=>\"/mnt\")\n", get_productid());
341    fprintf(fp, "       webdav.activate=\"enable\"\n");
342    fprintf(fp, "       webdav.is-readonly=\"disable\"\n");
343    fprintf(fp, "       webdav.sqlite-db-name=\"/tmp/lighttpd/webdav.db\"\n");
344    fprintf(fp, "   }\n");
345	fprintf(fp, "	else $HTTP[\"url\"]=~\"^/smb($|/)\"{\n");
346	fprintf(fp, "		server.document-root = \"/\"\n");
347#ifndef APP_IPKG
348	fprintf(fp, "		alias.url=(\"/smb\"=>\"/usr/lighttpd\")\n");
349#else
350	fprintf(fp, "		alias.url=(\"/smb\"=>\"/opt/etc/aicloud_UI\")\n");
351#endif
352	fprintf(fp, "		smbdav.auth_ntlm = (\"Microsoft-WebDAV\",\"xxBitKinex\",\"WebDrive\")\n");
353	fprintf(fp, "		webdav.activate=\"enable\"\n");
354	fprintf(fp, "		webdav.is-readonly=\"disable\"\n");
355//	fprintf(fp, "		webdav.sqlite-db-name=\"/tmp/lighttpd/webdav.db\"\n");
356	fprintf(fp, "	}\n");
357	fprintf(fp, "	else $HTTP[\"url\"] =~ \"^/favicon.ico$\"{\n");
358    fprintf(fp, "		server.document-root = \"/\"\n");
359#ifndef APP_IPKG
360    fprintf(fp, "		alias.url = ( \"/favicon.ico\" => \"/usr/lighttpd/css/favicon.ico\" ) \n");
361#else
362	fprintf(fp, "		alias.url = ( \"/favicon.ico\" => \"/opt/etc/aicloud_UI/css/favicon.ico\" ) \n");
363#endif
364    fprintf(fp, "		webdav.activate = \"enable\" \n");
365    fprintf(fp, "		webdav.is-readonly = \"enable\"\n");
366	fprintf(fp, "	}\n");
367	fprintf(fp, "	else $HTTP[\"url\"] !~ \"^/smb($|/|.)\" { \n");
368	fprintf(fp, "	    server.document-root = \"smb://\" \n");
369   	fprintf(fp, "	    smbdav.activate = \"enable\" \n");
370    fprintf(fp, "	    smbdav.is-readonly = \"disable\" \n");
371    fprintf(fp, "	    smbdav.always-auth = \"enable\" \n");
372    fprintf(fp, "	    smbdav.sqlite-db-name = \"/tmp/lighttpd/smbdav.db\" \n");
373    fprintf(fp, "	    usertrack.cookie-name = \"SMBSESSID\" \n");
374	fprintf(fp, "	}\n");
375	fprintf(fp, "}\n");
376
377
378
379#if 0
380	/*** Webdav_setting ***/
381	/* default : http://192.168.1.1:8082/webdav */
382	fprintf(fp, "$SERVER[\"socket\"]==\":8082\"{\n");
383	fprintf(fp, "	alias.url=(\"/webdav\"=>\"/mnt/\")\n"); 	//mount point
384	fprintf(fp, "	$HTTP[\"url\"]=~\"^/webdav($|/)\"{\n");
385	fprintf(fp, "		webdav.activate=\"enable\"\n");
386	fprintf(fp, "		webdav.is-readonly=\"disable\"\n");
387	fprintf(fp, "		webdav.sqlite-db-name=\"/tmp/lighttpd/webdav.db\"\n");
388	fprintf(fp, "	}\n");
389
390	fprintf(fp, "	auth.backend=\"plain\"\n");
391	fprintf(fp, "	auth.backend.plain.userfile=\"/tmp/lighttpd/permissions\"\n");
392	fprintf(fp, "	auth.require= (\"\"=>(\"method\"=>\"basic\",\n");
393	fprintf(fp, "				\"realm\"=>\"webdav\",\n");
394	fprintf(fp, "				\"require\"=>\"valid-user\"))\n");
395
396
397	disks_info = read_disk_data();
398	if (disks_info == NULL) {
399		//printf("[webdav] fail in read disk data!!\n");
400		usb_dbg("Couldn't get disk list when writing smb.conf!\n");
401	//	goto confpage;
402		goto WEBDAV_SETTING;
403	}
404
405	/* account permissions */
406	if (nvram_match("st_webdav_mode", "2")){
407
408		disks_info = read_disk_data();
409		if (disks_info == NULL) {
410			usb_dbg("Couldn't get disk list when writing lighttpd.conf!\n");
411			//	goto confpage;
412			goto WEBDAV_SETTING;
413		}
414
415		int acc_num;
416		char **account_list;
417
418
419		/* folder permissions */
420		if (get_account_list(&acc_num, &account_list) < 0) {
421			usb_dbg("Can't read the account list.\n");
422			//printf("[webdav] fail in get account list\n");
423			free_2_dimension_list(&acc_num, &account_list);
424//			goto confpage;
425			goto WEBDAV_SETTING;
426		}
427
428		for (follow_disk = disks_info; follow_disk != NULL; follow_disk = follow_disk->next) {
429			for (follow_partition = follow_disk->partitions; follow_partition != NULL; follow_partition = follow_partition->next) {
430				if (follow_partition->mount_point == NULL)
431					continue;
432
433				char **folder_list;
434				int i;
435				char tmp1[32];
436				char *tmp2, *tmp_pre, *tmp_aft;
437
438				strcpy(tmp1, follow_partition->mount_point);
439
440				tmp2 = tmp1;
441				for (i=0; i<3; i++){
442					tmp_pre = strsep(&tmp2, "/");
443					tmp_aft = tmp2;
444				}
445				//printf("[webdav] tmp1=%s,tmp2=%s,tmp_pre=%s,tmp_aft=%s\n", tmp1, tmp2, tmp_pre, tmp_aft);
446
447				// 1. get the folder list
448				if (get_folder_list(follow_partition->mount_point, &sh_num, &folder_list) < 0) {
449					//printf("[webdav] fail in get folder list\n");
450					usb_dbg("Can't read the folder list in %s.\n", follow_partition->mount_point);
451					free_2_dimension_list(&sh_num, &folder_list);
452					continue;
453				}
454
455				/*
456					right = 0 => NO
457					right = 1 => R
458					right = 3 => R/W (default)
459				*/
460
461				// 2. start to get every share
462				for (n = 0; n < sh_num; ++n) {
463					int i, right;
464					for (i = 0; i < acc_num; ++i) {
465						right = get_permission(account_list[i], follow_partition->mount_point, folder_list[n], "webdav");
466	//printf("[webdav] (%d,%d) : right=%d, account=%s, folder=%s, mount=%s, tmp_aft=%s\n", n, i, right, account_list[i], folder_list[n], follow_partition->mount_point, tmp_aft);
467						if(right == 0){
468							/* forbid to access the folder */
469							fprintf(fp, "	$HTTP[\"url\"]=~\"^/webdav/%s/%s($|/)\"{\n", tmp_aft, folder_list[n]);
470							fprintf(fp, "		url.access-deny=(\"\")\n");
471							fprintf(fp, "	}\n");
472						}
473						else if (right == 1){
474							fprintf(fp, "	$HTTP[\"url\"]=~\"^/webdav/%s/%s($|/)\"{\n", tmp_aft, folder_list[n]);
475							fprintf(fp, "		webdav.is-readonly=\"enable\"\n");
476							fprintf(fp, "	}\n");
477						}
478					}
479				}
480				free_2_dimension_list(&sh_num, &folder_list);
481			}
482		}
483		/* folder permissions */
484		free_2_dimension_list(&acc_num, &account_list);
485	}/* account permissions */
486WEBDAV_SETTING:
487	fprintf(fp, "}\n"); /*** Webdav_setting ***/
488#endif
489	/*** Webdav_SSL ***/
490	/* default : https://192.168.1.1:443/webdav */
491	fprintf(fp, "$SERVER[\"socket\"]==\":%s\"{\n",get_webdav_https_port());
492	//fprintf(fp, "	ssl.pemfile=\"/tmp/lighttpd/server.pem\"\n");
493	fprintf(fp, "	ssl.pemfile=\"/etc/server.pem\"\n");
494
495	//FILE* fd;
496	//if (NULL != (fd = fopen("/etc/intermediate_cert.pem", "rb"))) {
497	if (nvram_match("https_intermediate_crt_save", "1")){
498		fprintf(fp, "   ssl.ca-file=\"/etc/intermediate_cert.pem\"\n");
499		//fclose(fd);
500	}
501
502	fprintf(fp, "	ssl.engine=\"enable\"\n");
503	fprintf(fp, "   ssl.use-compression=\"disable\"\n");
504	fprintf(fp, "   ssl.use-sslv2=\"disable\"\n");
505	fprintf(fp, "   ssl.use-sslv3=\"disable\"\n");
506	fprintf(fp, "   ssl.honor-cipher-order=\"enable\"\n");
507	//fprintf(fp, "   ssl.cipher-list=\"ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4\"\n");
508	fprintf(fp, "   ssl.cipher-list=\"ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:AES256-GCM-SHA384:AES256-SHA256:AES256-SHA:AES128-GCM-SHA256:AES128-SHA256:AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;\"\n");
509	fprintf(fp, "   url.aicloud-auth-deny = (\"query_field.json\")\n");
510	//fprintf(fp, "	alias.url=(\"/webdav\"=>\"/mnt/\")\n");
511//	fprintf(fp, "	$HTTP[\"url\"]=~\"^/usbdisk($|/)\"{\n");
512	fprintf(fp, "	$HTTP[\"url\"]=~\"^/%s($|/)\"{\n", get_productid());
513    fprintf(fp, "       server.document-root = \"/\"\n");
514//	fprintf(fp, "       alias.url=(\"/usbdisk\"=>\"/mnt\")\n");
515	fprintf(fp, "       alias.url=(\"/%s\"=>\"/mnt\")\n", get_productid());
516	fprintf(fp, "		webdav.activate=\"enable\"\n");
517	fprintf(fp, "		webdav.is-readonly=\"disable\"\n");
518	fprintf(fp, "		webdav.sqlite-db-name=\"/tmp/lighttpd/webdav.db\"\n");
519	fprintf(fp, "	}\n");
520	fprintf(fp, "	else $HTTP[\"url\"]=~\"^/smb($|/)\"{\n");
521	fprintf(fp, "		server.document-root = \"/\"\n");
522#ifndef APP_IPKG
523	fprintf(fp, "		alias.url=(\"/smb\"=>\"/usr/lighttpd\")\n");
524#else
525	fprintf(fp, "		alias.url=(\"/smb\"=>\"/opt/etc/aicloud_UI\")\n");
526#endif
527	fprintf(fp, "		smbdav.auth_ntlm = (\"Microsoft-WebDAV\",\"xxBitKinex\",\"WebDrive\")\n");
528	fprintf(fp, "		webdav.activate=\"enable\"\n");
529	fprintf(fp, "		webdav.is-readonly=\"disable\"\n");
530//	fprintf(fp, "		webdav.sqlite-db-name=\"/tmp/lighttpd/webdav.db\"\n");
531	fprintf(fp, "	}\n");
532	fprintf(fp, "	else $HTTP[\"url\"] =~ \"^/favicon.ico$\"{\n");
533    fprintf(fp, "		server.document-root = \"/\"\n");
534#ifndef APP_IPKG
535    fprintf(fp, "		alias.url = ( \"/favicon.ico\" => \"/usr/lighttpd/css/favicon.ico\" ) \n");
536#else
537	 fprintf(fp, "		alias.url = ( \"/favicon.ico\" => \"/opt/etc/aicloud_UI/css/favicon.ico\" ) \n");
538#endif
539    fprintf(fp, "		webdav.activate = \"enable\" \n");
540    fprintf(fp, "		webdav.is-readonly = \"enable\"\n");
541	fprintf(fp, "	}\n");
542	fprintf(fp, "   else $HTTP[\"url\"] =~ \"^/aicloud.crt$\"{\n");
543	fprintf(fp, "       server.document-root = \"/\"\n");
544	fprintf(fp, "       alias.url = ( \"/aicloud.crt\" => \"/etc/cert.pem\" ) \n");
545	fprintf(fp, "       webdav.activate = \"enable\" \n");
546	fprintf(fp, "       webdav.is-readonly = \"enable\"\n");
547	fprintf(fp, "   }\n");
548	fprintf(fp, "	else $HTTP[\"url\"] !~ \"^/smb($|/|.)\" { \n");
549	fprintf(fp, "	    server.document-root = \"smb://\" \n");
550   	fprintf(fp, "	    smbdav.activate = \"enable\" \n");
551    fprintf(fp, "	    smbdav.is-readonly = \"disable\" \n");
552    fprintf(fp, "	    smbdav.always-auth = \"enable\" \n");
553    fprintf(fp, "	    smbdav.sqlite-db-name = \"/tmp/lighttpd/smbdav.db\" \n");
554    fprintf(fp, "	    usertrack.cookie-name = \"SMBSESSID\" \n");
555	fprintf(fp, "	}\n");
556#if 0
557	/* account permissions */
558	if (nvram_match("st_webdav_mode", "2")){
559			fprintf(stderr,"=================================================>st mod =2");
560
561		disks_info = read_disk_data();
562		if (disks_info == NULL) {
563			usb_dbg("Couldn't get disk list when writing lighttpd.conf!\n");
564			fprintf(stderr, "=========================================================>error1");
565			goto confpage;
566		}
567
568		int acc_num;
569		char **account_list;
570
571		fprintf(fp, "	auth.backend=\"plain\"\n");
572		fprintf(fp, "	auth.backend.plain.userfile=\"/tmp/lighttpd/permissions\"\n");
573		fprintf(fp, "	auth.require= (\"\"=>(\"method\"=>\"basic\",\n");
574		fprintf(fp, "				\"realm\"=>\"webdav\",\n");
575		fprintf(fp, "				\"require\"=>\"valid-user\"))\n");
576
577		/* folder permissions */
578		if (get_account_list(&acc_num, &account_list) < 0) {
579			//usb_dbg("Can't read the account list.\n");
580			//printf("[webdav] fail in get account list\n");
581			free_2_dimension_list(&acc_num, &account_list);
582			fprintf(stderr, "================================================================>error2");
583			goto confpage;
584		}
585
586		for (follow_disk = disks_info; follow_disk != NULL; follow_disk = follow_disk->next) {
587			for (follow_partition = follow_disk->partitions; follow_partition != NULL; follow_partition = follow_partition->next) {
588				if (follow_partition->mount_point == NULL)
589					continue;
590
591				char **folder_list;
592				int i;
593				//charles: dont init the size of tmp1, the name of share foler may exceed the buffer.
594//				char tmp1[32];
595				char *tmp1;
596				char *tmp2, *tmp_pre, *tmp_aft;
597
598				int tmp1_size=0;
599				tmp1_size = strlen(follow_partition->mount_point)+1;
600				tmp1 = malloc(tmp1_size); memset(tmp1, 0, tmp1_size);
601				strcpy(tmp1, follow_partition->mount_point);
602
603				tmp2 = tmp1;
604				for (i=0; i<3; i++){
605					tmp_pre = strsep(&tmp2, "/");
606					tmp_aft = tmp2;
607				}
608
609				// 1. get the folder list
610				if (get_folder_list(follow_partition->mount_point, &sh_num, &folder_list) < 0) {
611					//printf("[webdav] fail in get folder list\n");
612					usb_dbg("Can't read the folder list in %s.\n", follow_partition->mount_point);
613					free_2_dimension_list(&sh_num, &folder_list);
614					if(tmp1) free(tmp1);
615					fprintf(stderr,"====================================================================>get folder list error");
616					continue;
617				}
618
619				// 2. start to get every share
620				for (n = 0; n < sh_num; ++n) {
621					int i, right;
622					for (i = 0; i < acc_num; ++i) {
623						right = get_permission(account_list[i], follow_partition->mount_point, folder_list[n], "webdav");
624	fprintf(stderr,"[webdav] (%d,%d) : right=%d, account=%s, folder=%s, mount=%s, tmp_aft=%s\n", n, i, right, account_list[i], folder_list[n], follow_partition->mount_point, tmp_aft);
625						if(right == 0){
626							/* forbid to access the folder */
627							fprintf(fp, "	$HTTP[\"url\"]=~\"^/webdav/%s/%s($|/)\"{\n", tmp_aft, folder_list[n]);
628							fprintf(fp, "		url.access-deny=(\"\")\n");
629							fprintf(fp, "	}\n");
630						}
631						else if (right == 1){
632						fprintf(fp, "	$HTTP[\"url\"]=~\"^/webdav/%s/%s($|/)\"{\n", tmp_aft, folder_list[n]);
633							fprintf(fp, "		webdav.is-readonly=\"enable\"\n");
634							fprintf(fp, "	}\n");
635						}
636					}
637				}
638				free_2_dimension_list(&sh_num, &folder_list);
639				if(tmp1) free(tmp1);
640			}
641		}
642		/* folder permissions */
643		free_2_dimension_list(&acc_num, &account_list);
644	}/* account permissions */
645#endif
646
647	goto confpage;
648
649confpage:
650	fprintf(fp, "}\n"); /*** Webdav_SSL ***/
651
652	/* debugging */
653	fprintf(fp, "debug.log-request-header=\"disable\"\n");
654	fprintf(fp, "debug.log-response-header=\"disable\"\n");
655	fprintf(fp, "debug.log-request-handling=\"disable\"\n");
656	fprintf(fp, "debug.log-file-not-found=\"disable\"\n");
657	fprintf(fp, "debug.log-condition-handling=\"disable\"\n");
658
659	fclose(fp);
660#ifndef APP_IPKG
661	free_disk_data(&disks_info);
662#endif
663	return 0;
664}
665