ftpsrvr.c 137 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
/* ftpsrvr.c */

/* Synchronet FTP server */

/* $Id$ */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
11
 * Copyright 2008 Rob Swindell - http://www.synchro.net/copyright.html		*
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
 *																			*
 * This program is free software; you can redistribute it and/or			*
 * modify it under the terms of the GNU General Public License				*
 * as published by the Free Software Foundation; either version 2			*
 * of the License, or (at your option) any later version.					*
 * See the GNU General Public License for more details: gpl.txt or			*
 * http://www.fsf.org/copyleft/gpl.html										*
 *																			*
 * Anonymous FTP access to the most recent released source is available at	*
 * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net	*
 *																			*
 * Anonymous CVS access to the development source and modification history	*
 * is available at cvs.synchro.net:/cvsroot/sbbs, example:					*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs login			*
 *     (just hit return, no password is necessary)							*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs checkout src		*
 *																			*
 * For Synchronet coding style and modification guidelines, see				*
 * http://www.synchro.net/source.html										*
 *																			*
 * You are encouraged to submit any modifications (preferably in Unix diff	*
 * format) via e-mail to mods@synchro.net									*
 *																			*
 * Note: If this box doesn't appear square, then you need to fix your tabs.	*
 ****************************************************************************/

rswindell's avatar
rswindell committed
38
/* ANSI C Library headers */
rswindell's avatar
rswindell committed
39
#include <stdio.h>
40
41
42
43
44
45
46
#include <stdlib.h>			/* ltoa in GNU C lib */
#include <stdarg.h>			/* va_list, varargs */
#include <string.h>			/* strrchr */
#include <fcntl.h>			/* O_WRONLY, O_RDONLY, etc. */
#include <errno.h>			/* EACCES */
#include <ctype.h>			/* toupper */
#include <sys/stat.h>		/* S_IWRITE */
rswindell's avatar
rswindell committed
47
48

/* Synchronet-specific headers */
49
#undef SBBS	/* this shouldn't be defined unless building sbbs.dll/libsbbs.so */
50
#include "sbbs.h"
51
#include "text.h"			/* TOTAL_TEXT */
rswindell's avatar
rswindell committed
52
#include "ftpsrvr.h"
53
#include "telnet.h"
54
#include "js_rtpool.h"
55
#include "js_request.h"
56
57
58

/* Constants */

59
#define FTP_SERVER				"Synchronet FTP Server"
60
61

#define STATUS_WFC				"Listening"
62
#define ANONYMOUS				"anonymous"
63
64
65
66

#define BBS_VIRTUAL_PATH		"bbs:/""/"	/* this is actually bbs:<slash><slash> */
#define LOCAL_FSYS_DIR			"local:"
#define BBS_FSYS_DIR			"bbs:"
67
#define BBS_HIDDEN_ALIAS		"hidden"
68

69
#define TIMEOUT_THREAD_WAIT		60		/* Seconds */
70

71
72
#define TIMEOUT_SOCKET_LISTEN	30		/* Seconds */

73
74
75
76
77
78
#define XFER_REPORT_INTERVAL	60		/* Seconds */

#define INDEX_FNAME_LEN			15

#define	NAME_LEN				15		/* User name length for listings */

79
static ftp_startup_t*	startup=NULL;
80
81
82
83
static scfg_t	scfg;
static SOCKET	server_socket=INVALID_SOCKET;
static DWORD	active_clients=0;
static DWORD	sockets=0;
rswindell's avatar
rswindell committed
84
static DWORD	thread_count=0;
85
static time_t	uptime=0;
86
static DWORD	served=0;
87
static BOOL		terminate_server=FALSE;
88
static char		revision[16];
89
static char 	*text[TOTAL_TEXT];
90
91
static str_list_t recycle_semfiles;
static str_list_t shutdown_semfiles;
92

93
#ifdef SOCKET_DEBUG
94
	static BYTE 	socket_debug[0x10000]={0};
95

96
97
98
99
100
101
102
103
	#define	SOCKET_DEBUG_CTRL		(1<<0)	/* 0x01 */
	#define SOCKET_DEBUG_SEND		(1<<1)	/* 0x02 */
	#define SOCKET_DEBUG_READLINE	(1<<2)	/* 0x04 */
	#define SOCKET_DEBUG_ACCEPT		(1<<3)	/* 0x08 */
	#define SOCKET_DEBUG_SENDTHREAD	(1<<4)	/* 0x10 */
	#define SOCKET_DEBUG_TERMINATE	(1<<5)	/* 0x20 */
	#define SOCKET_DEBUG_RECV_CHAR	(1<<6)	/* 0x40 */
	#define SOCKET_DEBUG_FILEXFER	(1<<7)	/* 0x80 */
104
#endif
105

106
char* genvpath(int lib, int dir, char* str);
107
108
109
110
111
112

typedef struct {
	SOCKET			socket;
	SOCKADDR_IN		client_addr;
} ftp_t;

113

114
static const char *ftp_mon[]={"Jan","Feb","Mar","Apr","May","Jun"
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
            ,"Jul","Aug","Sep","Oct","Nov","Dec"};

BOOL direxist(char *dir)
{
	if(access(dir,0)==0)
		return(TRUE);
	else
		return(FALSE);
}

BOOL dir_op(scfg_t* cfg, user_t* user, uint dirnum)
{
	return(user->level>=SYSOP_LEVEL || user->exempt&FLAG('R')
		|| (cfg->dir[dirnum]->op_ar[0] && chk_ar(cfg,cfg->dir[dirnum]->op_ar,user)));
}

131
static int lprintf(int level, const char *fmt, ...)
132
133
134
135
136
{
	int		result;
	va_list argptr;
	char sbuf[1024];

137
    if(startup==NULL || startup->lputs==NULL)
138
        return(0);
139

140
    va_start(argptr,fmt);
141
142
    vsnprintf(sbuf,sizeof(sbuf),fmt,argptr);
	sbuf[sizeof(sbuf)-1]=0;
143
    va_end(argptr);
144
    result=startup->lputs(startup->cbdata,level,sbuf);
145
146
147
148
149
150
151

	return(result);
}

#ifdef _WINSOCKAPI_

static WSADATA WSAData;
152
153
#define SOCKLIB_DESC WSAData.szDescription

154
static BOOL WSAInitialized=FALSE;
155
156
157
158
159
160

static BOOL winsock_startup(void)
{
	int		status;             /* Status Code */

    if((status = WSAStartup(MAKEWORD(1,1), &WSAData))==0) {
161
		lprintf(LOG_INFO,"%s %s",WSAData.szDescription, WSAData.szSystemStatus);
162
		WSAInitialized=TRUE;
163
164
165
		return (TRUE);
	}

166
    lprintf(LOG_ERR,"!WinSock startup ERROR %d", status);
167
168
169
	return (FALSE);
}

170
#else /* No WINSOCK */
171

172
#define winsock_startup()	(TRUE)
173
#define SOCKLIB_DESC		NULL
174
175
176
177
178
179

#endif

static void status(char* str)
{
	if(startup!=NULL && startup->status!=NULL)
180
	    startup->status(startup->cbdata,str);
181
182
183
184
185
}

static void update_clients(void)
{
	if(startup!=NULL && startup->clients!=NULL)
186
		startup->clients(startup->cbdata,active_clients);
187
188
}

189
static void client_on(SOCKET sock, client_t* client, BOOL update)
190
191
{
	if(startup!=NULL && startup->client_on!=NULL)
192
		startup->client_on(startup->cbdata,TRUE,sock,client,update);
193
194
195
196
197
}

static void client_off(SOCKET sock)
{
	if(startup!=NULL && startup->client_on!=NULL)
198
		startup->client_on(startup->cbdata,FALSE,sock,NULL,FALSE);
199
200
}

201
static void thread_up(BOOL setuid)
202
{
rswindell's avatar
rswindell committed
203
	thread_count++;
204
	if(startup!=NULL && startup->thread_up!=NULL)
205
		startup->thread_up(startup->cbdata,TRUE, setuid);
206
207
208
209
}

static void thread_down(void)
{
rswindell's avatar
rswindell committed
210
211
	if(thread_count>0)
		thread_count--;
212
	if(startup!=NULL && startup->thread_up!=NULL)
213
		startup->thread_up(startup->cbdata,FALSE, FALSE);
214
215
}

216
static SOCKET ftp_open_socket(int type)
217
{
218
219
	SOCKET	sock;
	char	error[256];
220
221
222

	sock=socket(AF_INET, type, IPPROTO_IP);
	if(sock!=INVALID_SOCKET && startup!=NULL && startup->socket_open!=NULL) 
223
		startup->socket_open(startup->cbdata,TRUE);
224
	if(sock!=INVALID_SOCKET) {
225
		if(set_socket_options(&scfg, sock, "FTP", error, sizeof(error)))
226
			lprintf(LOG_ERR,"%04d !ERROR %s",sock, error);
227
		sockets++;
228
#ifdef SOCKET_DEBUG
229
		lprintf(LOG_DEBUG,"%04d Socket opened (%u sockets in use)",sock,sockets);
230
231
232
233
234
235
236
237
#endif
	}
	return(sock);
}

#ifdef __BORLANDC__
#pragma argsused
#endif
238
static int ftp_close_socket(SOCKET* sock, int line)
239
240
241
242
{
	int		result;

	if((*sock)==INVALID_SOCKET) {
243
		lprintf(LOG_WARNING,"0000 !INVALID_SOCKET in close_socket from line %u",line);
244
245
246
		return(-1);
	}

247
248
	shutdown(*sock,SHUT_RDWR);	/* required on Unix */

249
	result=closesocket(*sock);
250
	if(startup!=NULL && startup->socket_open!=NULL) 
251
		startup->socket_open(startup->cbdata,FALSE);
252
253
254

	sockets--;

255
256
	if(result!=0) {
		if(ERROR_VALUE!=ENOTSOCK)
257
			lprintf(LOG_WARNING,"%04d !ERROR %d closing socket from line %u",*sock,ERROR_VALUE,line);
258
	} else if(sock==&server_socket || *sock==server_socket)
259
		lprintf(LOG_DEBUG,"%04d Server socket closed (%u sockets in use) from line %u",*sock,sockets,line);
260
#ifdef SOCKET_DEBUG
261
	else 
262
		lprintf(LOG_DEBUG,"%04d Socket closed (%u sockets in use) from line %u",*sock,sockets,line);
263
264
265
266
267
268
269
270
271
#endif
	*sock=INVALID_SOCKET;

	return(result);
}

static int sockprintf(SOCKET sock, char *fmt, ...)
{
	int		len;
272
	int		maxlen;
273
274
275
	int		result;
	va_list argptr;
	char	sbuf[1024];
276
277
	fd_set	socket_set;
	struct timeval tv;
278
279

    va_start(argptr,fmt);
280
281
282
    len=vsnprintf(sbuf,maxlen=sizeof(sbuf)-2,fmt,argptr);
    va_end(argptr);

283
	if(len<0 || len>maxlen) /* format error or output truncated */
284
		len=maxlen;
285
	if(startup!=NULL && startup->options&FTP_OPT_DEBUG_TX)
286
287
		lprintf(LOG_DEBUG,"%04d TX: %.*s", sock, len, sbuf);
	memcpy(sbuf+len,"\r\n",2);
288
	len+=2;
289

290
	if(sock==INVALID_SOCKET) {
291
		lprintf(LOG_WARNING,"!INVALID SOCKET in call to sockprintf");
292
293
294
		return(0);
	}

295
	/* Check socket for writability (using select) */
296
	tv.tv_sec=300;
297
298
299
300
301
302
	tv.tv_usec=0;

	FD_ZERO(&socket_set);
	FD_SET(sock,&socket_set);

	if((result=select(sock+1,NULL,&socket_set,NULL,&tv))<1) {
303
		if(result==0)
304
			lprintf(LOG_WARNING,"%04d !TIMEOUT selecting socket for send"
305
306
				,sock);
		else
307
			lprintf(LOG_WARNING,"%04d !ERROR %d selecting socket for send"
308
				,sock, ERROR_VALUE);
309
310
		return(0);
	}
311
	while((result=sendsocket(sock,sbuf,len))!=len) {
312
		if(result==SOCKET_ERROR) {
rswindell's avatar
rswindell committed
313
			if(ERROR_VALUE==EWOULDBLOCK) {
314
				YIELD();
315
316
				continue;
			}
rswindell's avatar
rswindell committed
317
			if(ERROR_VALUE==ECONNRESET) 
318
				lprintf(LOG_WARNING,"%04d Connection reset by peer on send",sock);
319
			else if(ERROR_VALUE==ECONNABORTED)
320
				lprintf(LOG_WARNING,"%04d Connection aborted by peer on send",sock);
321
			else
322
				lprintf(LOG_WARNING,"%04d !ERROR %d sending",sock,ERROR_VALUE);
323
324
			return(0);
		}
325
		lprintf(LOG_WARNING,"%04d !ERROR: short send: %u instead of %u",sock,result,len);
326
327
328
329
330
	}
	return(len);
}


331
332
333
334
335
336
337
338
/* Returns the directory index of a virtual lib/dir path (e.g. main/games/filename) */
int getdir(char* p, user_t* user)
{
	char*	tp;
	char	path[MAX_PATH+1];
	int		dir;
	int		lib;

rswindell's avatar
rswindell committed
339
	SAFECOPY(path,p);
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
	p=path;

	if(*p=='/') 
		p++;
	else if(!strncmp(p,"./",2))
		p+=2;

	tp=strchr(p,'/');
	if(tp) *tp=0;
	for(lib=0;lib<scfg.total_libs;lib++) {
		if(!chk_ar(&scfg,scfg.lib[lib]->ar,user))
			continue;
		if(!stricmp(scfg.lib[lib]->sname,p))
			break;
	}
	if(lib>=scfg.total_libs) 
		return(-1);

	if(tp!=NULL)
		p=tp+1;

	tp=strchr(p,'/');
	if(tp) *tp=0;
	for(dir=0;dir<scfg.total_dirs;dir++) {
		if(scfg.dir[dir]->lib!=lib)
			continue;
		if(dir!=scfg.sysop_dir && dir!=scfg.upload_dir 
			&& !chk_ar(&scfg,scfg.dir[dir]->ar,user))
			continue;
369
		if(!stricmp(scfg.dir[dir]->code_suffix,p))
370
371
372
373
374
375
376
377
			break;
	}
	if(dir>=scfg.total_dirs) 
		return(-1);

	return(dir);
}

378
379
380
381
382
/*********************************/
/* JavaScript Data and Functions */
/*********************************/
#ifdef JAVASCRIPT

383
384
js_server_props_t js_server_props;

385
386
387
388
static JSBool
js_write(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
    uintN		i;
389
    JSString*	str=NULL;
390
	FILE*	fp;
391
	jsrefcount	rc;
392
393
394
395
396
397
398
399

	if((fp=(FILE*)JS_GetContextPrivate(cx))==NULL)
		return(JS_FALSE);

    for (i = 0; i < argc; i++) {
		str = JS_ValueToString(cx, argv[i]);
		if (!str)
		    return JS_FALSE;
400
		rc=JS_SUSPENDREQUEST(cx);
401
		fprintf(fp,"%s",JS_GetStringBytes(str));
402
		JS_RESUMEREQUEST(cx, rc);
403
404
	}

405
406
407
408
409
410
411
412
413
414
415
	if(str==NULL)
		*rval = JSVAL_VOID;
	else
		*rval = STRING_TO_JSVAL(str);
	return(JS_TRUE);
}

static JSBool
js_writeln(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	FILE*	fp;
416
	jsrefcount	rc;
417
418
419
420
421

	if((fp=(FILE*)JS_GetContextPrivate(cx))==NULL)
		return(JS_FALSE);

	js_write(cx,obj,argc,argv,rval);
422
	rc=JS_SUSPENDREQUEST(cx);
423
	fprintf(fp,"\r\n");
424
	JS_RESUMEREQUEST(cx, rc);
425
426
427
428
429
	return(JS_TRUE);
}

static JSFunctionSpec js_global_functions[] = {
	{"write",           js_write,           1},		/* write to HTML file */
430
431
	{"writeln",         js_writeln,         1},		/* write to HTML file */
	{"print",			js_writeln,         1},		/* alias for writeln */
432
433
434
435
436
437
438
439
440
	{0}
};

static void
js_ErrorReporter(JSContext *cx, const char *message, JSErrorReport *report)
{
	char	line[64];
	char	file[MAX_PATH+1];
	char*	warning;
rswindell's avatar
rswindell committed
441
442
443
	FILE*	fp;

	fp=(FILE*)JS_GetContextPrivate(cx);
444
445
	
	if(report==NULL) {
446
		lprintf(LOG_ERR,"!JavaScript: %s", message);
rswindell's avatar
rswindell committed
447
448
		if(fp!=NULL)
			fprintf(fp,"!JavaScript: %s", message);
449
450
451
452
453
454
455
456
457
		return;
    }

	if(report->filename)
		sprintf(file," %s",report->filename);
	else
		file[0]=0;

	if(report->lineno)
458
		sprintf(line," line %u",report->lineno);
459
460
461
462
463
464
465
466
467
468
469
	else
		line[0]=0;

	if(JSREPORT_IS_WARNING(report->flags)) {
		if(JSREPORT_IS_STRICT(report->flags))
			warning="strict warning";
		else
			warning="warning";
	} else
		warning="";

470
	lprintf(LOG_ERR,"!JavaScript %s%s%s: %s",warning,file,line,message);
rswindell's avatar
rswindell committed
471
472
	if(fp!=NULL)
		fprintf(fp,"!JavaScript %s%s%s: %s",warning,file,line,message);
473
474
}

475
static JSContext* 
476
js_initcx(JSRuntime* runtime, SOCKET sock, JSObject** glob, JSObject** ftp)
477
478
479
{
	JSContext*	js_cx;
	JSObject*	js_glob;
480
	BOOL		success=FALSE;
481

482
	lprintf(LOG_DEBUG,"%04d JavaScript: Initializing context (stack: %lu bytes)"
483
		,sock,startup->js.cx_stack);
484

485
    if((js_cx = JS_NewContext(runtime, startup->js.cx_stack))==NULL)
486
		return(NULL);
487
	JS_BEGINREQUEST(js_cx);
488

489
	lprintf(LOG_DEBUG,"%04d JavaScript: Context created",sock);
rswindell's avatar
rswindell committed
490

491
492
    JS_SetErrorReporter(js_cx, js_ErrorReporter);

493
	do {
494

495
		lprintf(LOG_DEBUG,"%04d JavaScript: Initializing Global object",sock);
496
		if((js_glob=js_CreateGlobalObject(js_cx, &scfg, NULL))==NULL) 
497
			break;
498

499
500
		if (!JS_DefineFunctions(js_cx, js_glob, js_global_functions)) 
			break;
501

502
		lprintf(LOG_DEBUG,"%04d JavaScript: Initializing System object",sock);
503
		if(js_CreateSystemObject(js_cx, js_glob, &scfg, uptime, startup->host_name, SOCKLIB_DESC)==NULL) 
504
505
			break;

506
		if((*ftp=JS_DefineObject(js_cx, js_glob, "ftp", NULL
507
			,NULL,JSPROP_ENUMERATE|JSPROP_READONLY))==NULL)
508
509
			break;

510
511
512
		if(js_CreateServerObject(js_cx,js_glob,&js_server_props)==NULL)
			break;

513
514
515
516
517
518
		if(glob!=NULL)
			*glob=js_glob;

		success=TRUE;

	} while(0);
519

520
	if(!success) {
521
		JS_ENDREQUEST(js_cx);
522
523
524
		JS_DestroyContext(js_cx);
		return(NULL);
	}
525
526
527
528
529

	return(js_cx);
}

BOOL js_add_file(JSContext* js_cx, JSObject* array, 
530
531
532
533
534
				 char* name, char* desc, char* ext_desc,
				 ulong size, ulong credits, 
				 time_t time, time_t uploaded, time_t last_downloaded, 
				 ulong times_downloaded, ulong misc, 
				 char* uploader, char* link)
535
536
{
	JSObject*	file;
537
	JSString*	js_str;
538
	jsval		val;
539
	jsuint		index;
540

541
	if((file=JS_NewObject(js_cx, NULL, NULL, NULL))==NULL)
542
543
		return(FALSE);

544
545
546
	if((js_str=JS_NewStringCopyZ(js_cx, name))==NULL)
		return(FALSE);
	val=STRING_TO_JSVAL(js_str);
547
548
549
	if(!JS_SetProperty(js_cx, file, "name", &val))
		return(FALSE);

550
551
552
	if((js_str=JS_NewStringCopyZ(js_cx, desc))==NULL)
		return(FALSE);
	val=STRING_TO_JSVAL(js_str);
553
554
555
	if(!JS_SetProperty(js_cx, file, "description", &val))
		return(FALSE);

556
557
558
	if((js_str=JS_NewStringCopyZ(js_cx, ext_desc))==NULL)
		return(FALSE);
	val=STRING_TO_JSVAL(js_str);
559
	if(!JS_SetProperty(js_cx, file, "extended_description", &val))
560
561
562
563
564
565
566
567
568
569
		return(FALSE);

	val=INT_TO_JSVAL(size);
	if(!JS_SetProperty(js_cx, file, "size", &val))
		return(FALSE);

	val=INT_TO_JSVAL(credits);
	if(!JS_SetProperty(js_cx, file, "credits", &val))
		return(FALSE);

570
	JS_NewNumberValue(js_cx,time,&val);
571
572
573
574
575
576
577
	if(!JS_SetProperty(js_cx, file, "time", &val))
		return(FALSE);

	val=INT_TO_JSVAL(uploaded);
	if(!JS_SetProperty(js_cx, file, "uploaded", &val))
		return(FALSE);

578
579
580
581
582
583
584
585
	val=INT_TO_JSVAL(last_downloaded);
	if(!JS_SetProperty(js_cx, file, "last_downloaded", &val))
		return(FALSE);

	val=INT_TO_JSVAL(times_downloaded);
	if(!JS_SetProperty(js_cx, file, "times_downloaded", &val))
		return(FALSE);

586
587
588
	if((js_str=JS_NewStringCopyZ(js_cx, uploader))==NULL)
		return(FALSE);
	val=STRING_TO_JSVAL(js_str);
589
590
591
	if(!JS_SetProperty(js_cx, file, "uploader", &val))
		return(FALSE);

592
	val=INT_TO_JSVAL(misc);
rswindell's avatar
rswindell committed
593
	if(!JS_SetProperty(js_cx, file, "settings", &val))
594
595
		return(FALSE);

596
597
598
	if((js_str=JS_NewStringCopyZ(js_cx, link))==NULL)
		return(FALSE);
	val=STRING_TO_JSVAL(js_str);
599
600
601
602
603
604
605
606
607
608
	if(!JS_SetProperty(js_cx, file, "link", &val))
		return(FALSE);

	if(!JS_GetArrayLength(js_cx, array, &index))
		return(FALSE);

	val=OBJECT_TO_JSVAL(file);
	return(JS_SetElement(js_cx, array, index, &val));
}

609
BOOL js_generate_index(JSContext* js_cx, JSObject* parent, 
610
611
612
613
614
615
					   SOCKET sock, FILE* fp, int lib, int dir, user_t* user)
{
	char		str[256];
	char		path[MAX_PATH+1];
	char		spath[MAX_PATH+1];	/* script path */
	char		vpath[MAX_PATH+1];	/* virtual path */
616
617
	char		aliasfile[MAX_PATH+1];
	char		extdesc[513];
618
619
620
	char*		p;
	char*		tp;
	char*		np;
621
	char*		dp;
622
	char		aliasline[512];
623
	BOOL		alias_dir;
624
625
626
627
628
629
630
	BOOL		success=FALSE;
	FILE*		alias_fp;
	uint		i;
	file_t		f;
	glob_t		g;
	jsval		val;
	jsval		rval;
631
632
	JSObject*	lib_obj=NULL;
	JSObject*	dir_obj=NULL;
633
634
635
	JSObject*	file_array=NULL;
	JSObject*	dir_array=NULL;
	JSScript*	js_script=NULL;
636
	JSString*	js_str;
637
	long double		start=xp_timer();
638
	jsrefcount	rc;
639
640
641

	lprintf(LOG_DEBUG,"%04d JavaScript: Generating HTML Index for %s"
		,sock, genvpath(lib,dir,str));
642
643
644
645
646
647

	JS_SetContextPrivate(js_cx, fp);

	do {	/* pseudo try/catch */

		if((file_array=JS_NewArrayObject(js_cx, 0, NULL))==NULL) {
648
			lprintf(LOG_ERR,"%04d !JavaScript FAILED to create file_array",sock);
649
650
651
			break;
		}

652
653
654
		/* file[] */
		val=OBJECT_TO_JSVAL(file_array);
		if(!JS_SetProperty(js_cx, parent, "file_list", &val)) {
655
			lprintf(LOG_ERR,"%04d !JavaScript FAILED to set file property",sock);
656
657
658
			break;
		}

659
		if((dir_array=JS_NewArrayObject(js_cx, 0, NULL))==NULL) {
660
			lprintf(LOG_ERR,"%04d !JavaScript FAILED to create dir_array",sock);
661
662
663
			break;
		}

664
665
666
		/* dir[] */
		val=OBJECT_TO_JSVAL(dir_array);
		if(!JS_SetProperty(js_cx, parent, "dir_list", &val)) {
667
			lprintf(LOG_ERR,"%04d !JavaScript FAILED to set dir property",sock);
668
669
670
			break;
		}

671
		rc=JS_SUSPENDREQUEST(js_cx);
672
673
674
675
676
		if(strcspn(startup->html_index_script,"/\\")==strlen(startup->html_index_script)) {
			sprintf(spath,"%s%s",scfg.mods_dir,startup->html_index_script);
			if(scfg.mods_dir[0]==0 || !fexist(spath))
				sprintf(spath,"%s%s",scfg.exec_dir,startup->html_index_script);
		} else
rswindell's avatar
rswindell committed
677
			sprintf(spath,"%.*s",(int)sizeof(spath)-4,startup->html_index_script);
678
		/* Add extension if not specified */
679
680
681
682
		if(!strchr(spath,'.'))
			strcat(spath,".js");

		if(!fexist(spath)) {
683
			JS_RESUMEREQUEST(js_cx, rc);
684
			lprintf(LOG_ERR,"%04d !HTML JavaScript (%s) doesn't exist",sock,spath);
685
686
			break;
		}
687
		JS_RESUMEREQUEST(js_cx, rc);
688

689
690
691
		if((js_str=JS_NewStringCopyZ(js_cx, startup->html_index_file))==NULL)
			break;
		val=STRING_TO_JSVAL(js_str);
692
		if(!JS_SetProperty(js_cx, parent, "html_index_file", &val)) {
693
			lprintf(LOG_ERR,"%04d !JavaScript FAILED to set html_index_file property",sock);
694
695
696
697
			break;
		}

		/* curlib */
698
		if((lib_obj=JS_NewObject(js_cx, NULL, 0, NULL))==NULL) {
699
			lprintf(LOG_ERR,"%04d !JavaScript FAILED to create lib_obj",sock);
700
701
			break;
		}
702

703
		val=OBJECT_TO_JSVAL(lib_obj);
704
		if(!JS_SetProperty(js_cx, parent, "curlib", &val)) {
705
			lprintf(LOG_ERR,"%04d !JavaScript FAILED to set curlib property",sock);
706
			break;
707
		}
708
709

		/* curdir */
710
		if((dir_obj=JS_NewObject(js_cx, NULL, 0, NULL))==NULL) {
711
			lprintf(LOG_ERR,"%04d !JavaScript FAILED to create dir_obj",sock);
712
713
			break;
		}
714
715

		val=OBJECT_TO_JSVAL(dir_obj);
716
		if(!JS_SetProperty(js_cx, parent, "curdir", &val)) {
717
			lprintf(LOG_ERR,"%04d !JavaScript FAILED to set curdir property",sock);
718
719
720
			break;
		}

721
		SAFECOPY(vpath,"/");
722
723
724
725

		if(lib>=0) { /* root */

			strcat(vpath,scfg.lib[lib]->sname);
726
			strcat(vpath,"/");
727

728
729
730
			if((js_str=JS_NewStringCopyZ(js_cx, scfg.lib[lib]->sname))==NULL)
				break;
			val=STRING_TO_JSVAL(js_str);
731
			if(!JS_SetProperty(js_cx, lib_obj, "name", &val)) {
732
				lprintf(LOG_ERR,"%04d !JavaScript FAILED to set curlib.name property",sock);
733
734
735
				break;
			}

736
737
738
			if((js_str=JS_NewStringCopyZ(js_cx, scfg.lib[lib]->lname))==NULL)
				break;
			val=STRING_TO_JSVAL(js_str);
739
			if(!JS_SetProperty(js_cx, lib_obj, "description", &val)) {
740
				lprintf(LOG_ERR,"%04d !JavaScript FAILED to set curlib.desc property",sock);
741
742
				break;
			}
743
		}
744
745

		if(dir>=0) { /* 1st level */
746
			strcat(vpath,scfg.dir[dir]->code_suffix);
747
748
			strcat(vpath,"/");

749
750
751
			if((js_str=JS_NewStringCopyZ(js_cx, scfg.dir[dir]->code))==NULL)
				break;
			val=STRING_TO_JSVAL(js_str);
752
			if(!JS_SetProperty(js_cx, dir_obj, "code", &val)) {
753
				lprintf(LOG_ERR,"%04d !JavaScript FAILED to set curdir.code property",sock);
754
755
756
				break;
			}

757
758
759
			if((js_str=JS_NewStringCopyZ(js_cx, scfg.dir[dir]->sname))==NULL)
				break;
			val=STRING_TO_JSVAL(js_str);
760
			if(!JS_SetProperty(js_cx, dir_obj, "name", &val)) {
761
				lprintf(LOG_ERR,"%04d !JavaScript FAILED to set curdir.name property",sock);
762
763
764
				break;
			}

765
766
767
			if((js_str=JS_NewStringCopyZ(js_cx, scfg.dir[dir]->lname))==NULL)
				break;
			val=STRING_TO_JSVAL(js_str);
768
			if(!JS_SetProperty(js_cx, dir_obj, "description", &val)) {
769
				lprintf(LOG_ERR,"%04d !JavaScript FAILED to set curdir.desc property",sock);
770
771
772
773
				break;
			}

			val=INT_TO_JSVAL(scfg.dir[dir]->misc);
rswindell's avatar
rswindell committed
774
			if(!JS_SetProperty(js_cx, dir_obj, "settings", &val)) {
775
				lprintf(LOG_ERR,"%04d !JavaScript FAILED to set curdir.misc property",sock);
776
777
				break;
			}
778
779
		}

780
781
782
		if((js_str=JS_NewStringCopyZ(js_cx, vpath))==NULL)
			break;
		val=STRING_TO_JSVAL(js_str);
783
		if(!JS_SetProperty(js_cx, parent, "path", &val)) {
784
			lprintf(LOG_ERR,"%04d !JavaScript FAILED to set curdir property",sock);
785
786
787
788
789
			break;
		}

		if(lib<0) {	/* root dir */

790
			rc=JS_SUSPENDREQUEST(js_cx);
791
792
793
794
795
			/* File Aliases */
			sprintf(path,"%sftpalias.cfg",scfg.ctrl_dir);
			if((alias_fp=fopen(path,"r"))!=NULL) {

				while(!feof(alias_fp)) {
796
					if(!fgets(aliasline,sizeof(aliasline),alias_fp))
797
798
799
						break;

					p=aliasline;	/* alias pointer */
800
					SKIP_WHITESPACE(p);
801
802
803
804
805

					if(*p==';')	/* comment */
						continue;

					tp=p;		/* terminator pointer */
806
					FIND_WHITESPACE(tp);
807
808
809
					if(*tp) *tp=0;

					np=tp+1;	/* filename pointer */
810
					SKIP_WHITESPACE(np);
811

812
					tp=np;		/* terminator pointer */
813
					FIND_WHITESPACE(tp);
814
					if(*tp) *tp=0;
815

816
					dp=tp+1;	/* description pointer */
817
					SKIP_WHITESPACE(dp);
818
819
					truncsp(dp);

820
821
822
					if(stricmp(dp,BBS_HIDDEN_ALIAS)==0)
						continue;

823
824
825
826
827
828
829
830
831
832
833
					alias_dir=FALSE;

					/* Virtual Path? */
					if(!strnicmp(np,BBS_VIRTUAL_PATH,strlen(BBS_VIRTUAL_PATH))) {
						if((dir=getdir(np+strlen(BBS_VIRTUAL_PATH),user))<0)
							continue; /* No access or invalid virtual path */
						tp=strrchr(np,'/');
						if(tp==NULL) 
							continue;
						tp++;
						if(*tp) {
834
							SAFEPRINTF2(aliasfile,"%s%s",scfg.dir[dir]->path,tp);
835
836
837
838
839
840
841
842
							np=aliasfile;
						}
						else 
							alias_dir=TRUE;
					}

					if(!alias_dir && !fexist(np))
						continue;
843

844
845
846
					if(alias_dir) {
						if(!chk_ar(&scfg,scfg.dir[dir]->ar,user))
							continue;
847
						SAFEPRINTF2(vpath,"/%s/%s",p,startup->html_index_file);
848
					} else
849
						SAFECOPY(vpath,p);
850
					JS_RESUMEREQUEST(js_cx, rc);
851
					js_add_file(js_cx
852
						,alias_dir ? dir_array : file_array
853
						,p				/* filename */
854
855
856
						,dp				/* description */
						,NULL			/* extdesc */
						,flength(np)	/* size */
857
						,0				/* credits */
858
859
						,fdate(np)		/* time */
						,fdate(np)		/* uploaded */
860
861
						,0				/* last downloaded */
						,0				/* times downloaded */
862
						,0				/* misc */
863
						,scfg.sys_id	/* uploader */
864
						,vpath			/* link */
865
						);
866
					rc=JS_SUSPENDREQUEST(js_cx);
867
868
869
870
				}

				fclose(alias_fp);
			}
871
			JS_RESUMEREQUEST(js_cx, rc);
872
873
874
875

			/* QWK Packet */
			if(startup->options&FTP_OPT_ALLOW_QWK /* && fexist(qwkfile) */) {
				sprintf(str,"%s.qwk",scfg.sys_id);
876
				SAFEPRINTF(vpath,"/%s",str);
877
878
879
880
				js_add_file(js_cx
					,file_array 
					,str						/* filename */
					,"QWK Message Packet"		/* description */
881
					,NULL						/* extdesc */
882
883
					,10240						/* size */
					,0							/* credits */
884
885
					,time(NULL)					/* time */
					,0							/* uploaded */
886
887
					,0							/* last downloaded */
					,0							/* times downloaded */
888
					,0							/* misc */
889
					,scfg.sys_id				/* uploader */
890
					,str						/* link */
891
892
893
894
895
896
897
					);
			}

			/* Library Folders */
			for(i=0;i<scfg.total_libs;i++) {
				if(!chk_ar(&scfg,scfg.lib[i]->ar,user))
					continue;
898
				SAFEPRINTF2(vpath,"/%s/%s",scfg.lib[i]->sname,startup->html_index_file);
899
900
901
902
				js_add_file(js_cx
					,dir_array 
					,scfg.lib[i]->sname			/* filename */
					,scfg.lib[i]->lname			/* description */
903
904
					,NULL						/* extdesc */
					,0,0,0,0,0,0,0				/* unused */
905
					,scfg.sys_id				/* uploader */
906
					,vpath						/* link */
907
908
909
					);
			}
		} else if(dir<0) {
910
			/* Directories */
911
912
913
			for(i=0;i<scfg.total_dirs;i++) {
				if(scfg.dir[i]->lib!=lib)
					continue;
914
915
				if(/* i!=scfg.sysop_dir && i!=scfg.upload_dir && */
					!chk_ar(&scfg,scfg.dir[i]->ar,user))
916
					continue;
917
				SAFEPRINTF3(vpath,"/%s/%s/%s"
918
					,scfg.lib[scfg.dir[i]->lib]->sname
919
					,scfg.dir[i]->code_suffix
920
921
922
					,startup->html_index_file);
				js_add_file(js_cx
					,dir_array 
923
					,scfg.dir[i]->sname			/* filename */
924
					,scfg.dir[i]->lname			/* description */
925
					,NULL						/* extdesc */
926
927
928
					,getfiles(&scfg,i)			/* size */
					,0,0,0,0,0					/* unused */
					,scfg.dir[i]->misc			/* misc */
929
					,scfg.sys_id				/* uploader */
930
					,vpath						/* link */
931
932
933
934
					);

			}
		} else if(chk_ar(&scfg,scfg.dir[dir]->ar,user)){
935
			SAFEPRINTF(path,"%s*",scfg.dir[dir]->path);
936
			rc=JS_SUSPENDREQUEST(js_cx);
937
938
939
940
941
942
943
			glob(path,0,NULL,&g);
			for(i=0;i<(int)g.gl_pathc;i++) {
				if(isdir(g.gl_pathv[i]))
					continue;
	#ifdef _WIN32
				GetShortPathName(g.gl_pathv[i], str, sizeof(str));
	#else
944
				SAFECOPY(str,g.gl_pathv[i]);
945
946
947
948
949
950
	#endif
				padfname(getfname(str),f.name);
				f.dir=dir;
				if(getfileixb(&scfg,&f)) {
					f.size=0; /* flength(g.gl_pathv[i]); */
					getfiledat(&scfg,&f);
rswindell's avatar
rswindell committed
951
952
					if(f.misc&FM_EXTDESC) {
						extdesc[0]=0;
953
						getextdesc(&scfg, dir, f.datoffset, extdesc);
rswindell's avatar
rswindell committed
954
955
956
						/* Remove Ctrl-A Codes and Ex-ASCII code */
						remove_ctrl_a(extdesc,NULL);
					}
957
					SAFEPRINTF3(vpath,"/%s/%s/%s"
958
						,scfg.lib[scfg.dir[dir]->lib]->sname
959
						,scfg.dir[dir]->code_suffix
960
						,getfname(g.gl_pathv[i]));
961
					JS_RESUMEREQUEST(js_cx, rc);
962
963
964
965
					js_add_file(js_cx
						,file_array 
						,getfname(g.gl_pathv[i])	/* filename */
						,f.desc						/* description */
966
						,f.misc&FM_EXTDESC ? extdesc : NULL
967
968
						,f.size						/* size */
						,f.cdt						/* credits */
969
970
						,f.date						/* time */
						,f.dateuled					/* uploaded */
971
972
						,f.datedled					/* last downloaded */
						,f.timesdled				/* times downloaded */
973
						,f.misc						/* misc */
974
						,f.uler						/* uploader */
975
						,getfname(g.gl_pathv[i])	/* link */
976
						);
977
					rc=JS_SUSPENDREQUEST(js_cx);
978
979
980
				}
			}
			globfree(&g);
981
			JS_RESUMEREQUEST(js_cx, rc);
982
983
		}

984

985
		/* RUN SCRIPT */