services.c 47.8 KB
Newer Older
1
2
/* services.c */

3
/* Synchronet Services */
4
5
6
7
8
9
10

/* $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 2003 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
38
39
40
41
42
43
44
45
46
47
48
 *																			*
 * 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.	*
 ****************************************************************************/

/* Platform-specific headers */
#ifdef _WIN32

	#include <io.h>			/* open/close */
	#include <share.h>		/* share open flags */
	#include <process.h>	/* _beginthread */
	#include <windows.h>	/* required for mmsystem.h */
	#include <mmsystem.h>	/* SND_ASYNC */

#endif

rswindell's avatar
rswindell committed
49
50
51
#ifdef __unix__
	#include <sys/param.h>	/* BSD? */
#endif
52
53
54
55
56
57
58
59
60
61
62

/* ANSI C Library headers */
#include <stdio.h>
#include <stdlib.h>			/* ltoa in GNU C lib */
#include <stdarg.h>			/* va_list */
#include <string.h>			/* strrchr */
#include <ctype.h>			/* isdigit */
#include <fcntl.h>			/* Open flags */
#include <errno.h>			/* errno */

/* Synchronet-specific headers */
rswindell's avatar
rswindell committed
63
#ifndef JAVASCRIPT
64
#define JAVASCRIPT	/* required to include JS API headers */
rswindell's avatar
rswindell committed
65
#endif
66
67
68
#include "sbbs.h"
#include "services.h"
#include "ident.h"	/* identify() */
69
#include "ini_file.h"
70
71
72
73

/* Constants */

#define MAX_SERVICES			128
74
#define TIMEOUT_THREAD_WAIT		60		/* Seconds */
rswindell's avatar
rswindell committed
75
#define MAX_UDP_BUF_LEN			8192	/* 8K */
76
77
78
79
80
81

static services_startup_t* startup=NULL;
static scfg_t	scfg;
static int		active_clients=0;
static DWORD	sockets=0;
static BOOL		terminated=FALSE;
82
static time_t	uptime=0;
83
static DWORD	served=0;
84
static char		revision[16];
85
86
87
88
89
90
91
92
93
94

typedef struct {
	/* These are sysop-configurable */
	WORD	port;
	char	protocol[34];
	char	cmd[128];
	DWORD	max_clients;
	DWORD	options;
	/* These are run-time state and stat vars */
	DWORD	clients;
95
	DWORD	served;
96
	SOCKET	socket;
97
98
	BOOL	running;
	BOOL	terminated;
99
100
} service_t;

101
102
103
104
105
106
107
108
109
110
111
112
113
114
static ini_bitdesc_t service_options[] = {

	{ BBS_OPT_NO_HOST_LOOKUP		,"NO_HOST_LOOKUP"		},
	{ BBS_OPT_GET_IDENT				,"GET_IDENT"			},
	{ BBS_OPT_NO_RECYCLE			,"NO_RECYCLE"			},
	{ BBS_OPT_MUTE					,"MUTE"					},
	{ SERVICE_OPT_UDP				,"UDP"					},
	{ SERVICE_OPT_STATIC			,"STATIC"				},
	{ SERVICE_OPT_STATIC_LOOP		,"LOOP"					},
	{ SERVICE_OPT_NATIVE			,"NATIVE"				},
	/* terminator */				
	{ -1							,NULL					}
};

115
116
117
typedef struct {
	SOCKET			socket;
	SOCKADDR_IN		addr;
118
119
	time_t			logintime;
	user_t			user;
120
	client_t*		client;
121
	service_t*		service;
122
	js_branch_t		branch;
rswindell's avatar
rswindell committed
123
124
125
	/* Initial UDP datagram */
	BYTE*			udp_buf;
	int				udp_len;
126
127
} service_client_t;

128
static service_t	*service=NULL;
129
130
131
132
133
134
135
136
137
138
static DWORD		services=0;

static int lprintf(char *fmt, ...)
{
	va_list argptr;
	char sbuf[1024];

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

139
140
141
142
143
#if defined(_WIN32)
	if(IsBadCodePtr((FARPROC)startup->lputs))
		return(0);
#endif

144
	va_start(argptr,fmt);
145
146
    vsnprintf(sbuf,sizeof(sbuf),fmt,argptr);
	sbuf[sizeof(sbuf)-1]=0;
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
    va_end(argptr);
    return(startup->lputs(sbuf));
}

#ifdef _WINSOCKAPI_

static WSADATA WSAData;
static BOOL WSAInitialized=FALSE;

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

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

    lprintf("!WinSock startup ERROR %d", status);
	return (FALSE);
}

#else /* No WINSOCK */

#define winsock_startup()	(TRUE)

#endif

static void update_clients(void)
{
	if(startup!=NULL && startup->clients!=NULL)
		startup->clients(active_clients);
}

182
static void client_on(SOCKET sock, client_t* client, BOOL update)
183
184
{
	if(startup!=NULL && startup->client_on!=NULL)
185
		startup->client_on(TRUE,sock,client,update);
186
187
188
189
190
}

static void client_off(SOCKET sock)
{
	if(startup!=NULL && startup->client_on!=NULL)
191
		startup->client_on(FALSE,sock,NULL,FALSE);
192
193
}

194
static void thread_up(BOOL setuid)
195
196
{
	if(startup!=NULL && startup->thread_up!=NULL)
197
		startup->thread_up(TRUE,setuid);
198
199
200
201
202
}

static void thread_down(void)
{
	if(startup!=NULL && startup->thread_up!=NULL)
203
		startup->thread_up(FALSE,FALSE);
204
205
206
207
}

static SOCKET open_socket(int type)
{
208
209
	char	error[256];
	SOCKET	sock;
210
211
212
213
214
215

	sock=socket(AF_INET, type, IPPROTO_IP);
	if(sock!=INVALID_SOCKET && startup!=NULL && startup->socket_open!=NULL) 
		startup->socket_open(TRUE);
	if(sock!=INVALID_SOCKET) {
		sockets++;
216
217
218
		if(set_socket_options(&scfg, sock, error))
			lprintf("%04d !ERROR %s",sock, error);

219
220
221
222
223
224
225
226
227
228
229
#if 0 /*def _DEBUG */
		lprintf("%04d Socket opened (%d sockets in use)",sock,sockets);
#endif
	}
	return(sock);
}

static int close_socket(SOCKET sock)
{
	int		result;

230
231
232
	if(sock==INVALID_SOCKET)
		return(-1);

233
234
	shutdown(sock,SHUT_RDWR);	/* required on Unix */
	result=closesocket(sock);
235
	if(result==0 && startup!=NULL && startup->socket_open!=NULL) 
236
237
		startup->socket_open(FALSE);
	sockets--;
238
239
240
	if(result!=0)
		lprintf("%04d !ERROR %d closing socket",sock, ERROR_VALUE);
#if 0 /*def _DEBUG */
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
	else 
		lprintf("%04d Socket closed (%d sockets in use)",sock,sockets);
#endif

	return(result);
}

static void status(char* str)
{
	if(startup!=NULL && startup->status!=NULL)
	    startup->status(str);
}

static time_t checktime(void)
{
	struct tm tm;

    memset(&tm,0,sizeof(tm));
    tm.tm_year=94;
    tm.tm_mday=1;
    return(mktime(&tm)-0x2D24BD00L);
}

rswindell's avatar
rswindell committed
264
/* Global JavaScript Methods */
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
static JSBool
js_read(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	char*		buf;
	int32		len=512;
	service_client_t* client;

	if((client=(service_client_t*)JS_GetContextPrivate(cx))==NULL)
		return(JS_FALSE);

	*rval = JSVAL_VOID;

	if(argc)
		JS_ValueToInt32(cx,argv[0],&len);
	
	if((buf=malloc(len))==NULL)
		return(JS_TRUE);

	len=recv(client->socket,buf,len,0);

	if(len>0)
		*rval = STRING_TO_JSVAL(JS_NewStringCopyN(cx,buf,len));

	free(buf);

	return(JS_TRUE);
}

static JSBool
js_readln(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	char*		buf;
	int32		len=512;
	service_client_t* client;

	if((client=(service_client_t*)JS_GetContextPrivate(cx))==NULL)
		return(JS_FALSE);

	*rval = JSVAL_VOID;

	if(argc)
		JS_ValueToInt32(cx,argv[0],&len);
	
	if((buf=malloc(len))==NULL)
		return(JS_TRUE);

	len=recv(client->socket,buf,len,0);	/* Need to switch to sockreadline */

	if(len>0)
		*rval = STRING_TO_JSVAL(JS_NewStringCopyN(cx,buf,len));

	free(buf);

	return(JS_TRUE);
}

322
323
324
325
326
327
328
329
330
331
332
static JSBool
js_write(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	uintN		i;
	char*		cp;
	JSString*	str;
	service_client_t* client;

	if((client=(service_client_t*)JS_GetContextPrivate(cx))==NULL)
		return(JS_FALSE);

333
334
	*rval = argv[0];

335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
	for(i=0; i<argc; i++) {
		if((str=JS_ValueToString(cx, argv[i]))==NULL)
			continue;
		if((cp=JS_GetStringBytes(str))==NULL)
			continue;
		sendsocket(client->socket,cp,strlen(cp));
	}

	return(JS_TRUE);
}

static JSBool
js_writeln(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	char*		cp;
	service_client_t* client;

	if((client=(service_client_t*)JS_GetContextPrivate(cx))==NULL)
		return(JS_FALSE);
354
355
	
	js_write(cx,obj,argc,argv,rval);
356
357
358
359
360
361
362

	cp="\r\n";
	sendsocket(client->socket,cp,2);

	return(JS_TRUE);
}

rswindell's avatar
rswindell committed
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
static JSBool
js_log(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	char		str[512];
    uintN		i;
    JSString*	js_str;
	service_client_t* client;

	if((client=(service_client_t*)JS_GetContextPrivate(cx))==NULL)
		return(JS_FALSE);

    if(startup==NULL || startup->lputs==NULL)
        return(JS_FALSE);

	str[0]=0;
    for(i=0;i<argc && strlen(str)<(sizeof(str)/2);i++) {
		if((js_str=JS_ValueToString(cx, argv[i]))==NULL)
		    return(JS_FALSE);
381
		strncat(str,JS_GetStringBytes(js_str),sizeof(str)/2);
rswindell's avatar
rswindell committed
382
383
384
		strcat(str," ");
	}

385
386
387
388
	if(service==NULL)
		lprintf("%04d %s",client->socket,str);
	else
		lprintf("%04d %s %s",client->socket,client->service->protocol,str);
rswindell's avatar
rswindell committed
389

390
391
	*rval = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, str));

rswindell's avatar
rswindell committed
392
393
394
    return(JS_TRUE);
}

395
396
397
398
static JSBool
js_login(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	char*		p;
399
	JSBool		inc_logons=JS_FALSE;
400
	user_t		user;
401
	jsval		val;
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
	JSString*	js_str;
	service_client_t* client;

	*rval = BOOLEAN_TO_JSVAL(JS_FALSE);

	if((client=(service_client_t*)JS_GetContextPrivate(cx))==NULL)
		return(JS_FALSE);

	/* User name */
	if((js_str=JS_ValueToString(cx, argv[0]))==NULL) 
		return(JS_FALSE);

	if((p=JS_GetStringBytes(js_str))==NULL) 
		return(JS_FALSE);

	if(isdigit(*p))
		user.number=atoi(p);
	else
420
		user.number=matchuser(&scfg,p,FALSE);
421
422
423
424
425
426
427

	if(getuserdat(&scfg,&user)!=0) {
		lprintf("%04d %s !USER NOT FOUND: '%s'"
			,client->socket,client->service->protocol,p);
		return(JS_TRUE);
	}

428
429
430
431
432
433
	if(user.misc&(DELETED|INACTIVE)) {
		lprintf("%04d %s !DELETED OR INACTIVE USER #%d: %s"
			,client->socket,client->service->protocol,user.number,p);
		return(JS_TRUE);
	}

434
435
436
437
438
439
440
441
442
443
444
445
446
447
	/* Password */
	if(user.pass[0]) {
		if((js_str=JS_ValueToString(cx, argv[1]))==NULL) 
			return(JS_FALSE);

		if((p=JS_GetStringBytes(js_str))==NULL) 
			return(JS_FALSE);

		if(stricmp(user.pass,p)) { /* Wrong password */
			lprintf("%04d %s !INVALID PASSWORD ATTEMPT FOR USER: %s"
				,client->socket,client->service->protocol,user.alias);
			return(JS_TRUE);
		}
	}
448
449

	if(argc>2)
450
		JS_ValueToBoolean(cx,argv[2],&inc_logons);
451

452
453
454
455
456
	if(client->client!=NULL) {
		SAFECOPY(user.note,client->client->addr);
		SAFECOPY(user.comp,client->client->host);
		SAFECOPY(user.modem,client->service->protocol);
	}
457
458
459
460
461
462

	if(inc_logons) {
		user.logons++;
		user.ltoday++;
	}	

463
464
	putuserdat(&scfg,&user);

465
466
467
	/* user-specific objects */
	if(!js_CreateUserObjects(cx, obj, &scfg, &user, NULL, NULL)) 
		lprintf("%04d %s !JavaScript ERROR creating user objects"
468
469
			,client->socket,client->service->protocol);

470
471
472
473
	if(client->client!=NULL) {
		client->client->user=user.alias;
		client_on(client->socket,client->client,TRUE /* update */);
	}
474

475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
	memcpy(&client->user,&user,sizeof(user));

	client->logintime=time(NULL);

	lprintf("%04d %s Logging in %s"
		,client->socket,client->service->protocol,user.alias);

	val = BOOLEAN_TO_JSVAL(JS_TRUE);
	JS_SetProperty(cx, obj, "logged_in", &val);

	*rval=BOOLEAN_TO_JSVAL(JS_TRUE);

	return(JS_TRUE);
}

static JSBool
js_logout(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	jsval val;
	service_client_t* client;

	*rval = BOOLEAN_TO_JSVAL(JS_FALSE);

	if((client=(service_client_t*)JS_GetContextPrivate(cx))==NULL)
		return(JS_FALSE);

	if(client->user.number<1)	/* Not logged in */
		return(JS_TRUE);

	logoutuserdat(&scfg,&client->user,time(NULL),client->logintime);

	lprintf("%04d %s Logging out %s"
		,client->socket,client->service->protocol,client->user.alias);

	memset(&client->user,0,sizeof(client->user));

	val = BOOLEAN_TO_JSVAL(JS_FALSE);
	JS_SetProperty(cx, obj, "logged_in", &val);

514
515
516
517
	*rval=BOOLEAN_TO_JSVAL(JS_TRUE);

	return(JS_TRUE);
}
rswindell's avatar
rswindell committed
518
519

static JSFunctionSpec js_global_functions[] = {
520
521
	{"read",			js_read,			0},		/* read from client socket */
	{"readln",			js_readln,			0},		/* read line from client socket */
522
523
524
525
	{"write",			js_write,			0},		/* write to client socket */
	{"writeln",			js_writeln,			0},		/* write line to client socket */
	{"print",			js_writeln,			0},		/* write line to client socket */
	{"log",				js_log,				0},		/* Log a string */
526
 	{"login",			js_login,			2},		/* Login specified username and password */
527
	{"logout",			js_logout,			0},		/* Logout user */
rswindell's avatar
rswindell committed
528
529
530
    {0}
};

531
532
533
534
535
static void
js_ErrorReporter(JSContext *cx, const char *message, JSErrorReport *report)
{
	char	line[64];
	char	file[MAX_PATH+1];
536
537
	char*	prot="???";
	SOCKET	sock=0;
538
	char*	warning;
539
540
541
542
543
544
	service_client_t* client;

	if((client=(service_client_t*)JS_GetContextPrivate(cx))!=NULL) {
		prot=client->service->protocol;
		sock=client->socket;
	}
545
546

	if(report==NULL) {
547
		lprintf("%04d %s !JavaScript: %s", sock,prot,message);
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
		return;
    }

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

	if(report->lineno)
		sprintf(line," line %d",report->lineno);
	else
		line[0]=0;

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

569
	lprintf("%04d %s !JavaScript %s%s%s: %s",sock,prot,warning,file,line,message);
570
571
}

572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
/* Server Object Properites */
enum {
	 SERVER_PROP_TERMINATED
};


static JSBool js_server_get(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
{
    jsint       tiny;
	service_client_t* client;

	if((client=(service_client_t*)JS_GetContextPrivate(cx))==NULL)
		return(JS_FALSE);

    tiny = JSVAL_TO_INT(id);

	switch(tiny) {
		case SERVER_PROP_TERMINATED:
#if 0
			lprintf("%s client->service->terminated=%d"
				,client->service->protocol, client->service->terminated);
#endif
			*vp = BOOLEAN_TO_JSVAL(client->service->terminated);
			break;
	}

	return(JS_TRUE);
}

#define SERVER_PROP_FLAGS JSPROP_ENUMERATE|JSPROP_READONLY

static struct JSPropertySpec js_server_properties[] = {
/*		 name				,tinyid					,flags,				getter,	setter	*/

	{	"terminated"		,SERVER_PROP_TERMINATED	,SERVER_PROP_FLAGS,	NULL,NULL},
	{0}
};

610
static JSClass js_server_class = {
611
612
613
614
615
616
617
618
619
620
         "Server"			/* name			*/
		,0					/* flags		*/
        ,JS_PropertyStub	/* addProperty	*/
		,JS_PropertyStub	/* delProperty	*/
		,js_server_get		/* getProperty	*/
		,JS_PropertyStub	/* setProperty	*/
		,JS_EnumerateStub	/* enumerate	*/
		,JS_ResolveStub		/* resolve		*/
		,JS_ConvertStub		/* convert		*/
		,JS_FinalizeStub	/* finalize		*/
621
622
}; 

623
static JSContext* 
624
js_initcx(JSRuntime* js_runtime, SOCKET sock, service_client_t* service_client, JSObject** glob)
625
{
626
	char		ver[256];
627
628
	JSContext*	js_cx;
	JSObject*	js_glob;
629
	JSObject*	server;
630
	JSString*	js_str;
631
	jsval		val;
632
633
634
635
636
637
638
639
640
	BOOL		success=FALSE;

    if((js_cx = JS_NewContext(js_runtime, JAVASCRIPT_CONTEXT_STACK))==NULL)
		return(NULL);

    JS_SetErrorReporter(js_cx, js_ErrorReporter);

	do {

rswindell's avatar
rswindell committed
641
642
		JS_SetContextPrivate(js_cx, service_client);

643
		if((js_glob=js_CreateGlobalObject(js_cx, &scfg, NULL))==NULL) 
644
645
			break;

rswindell's avatar
rswindell committed
646
647
648
		if (!JS_DefineFunctions(js_cx, js_glob, js_global_functions))
			break;

649
650
651
652
		/* Branch Object */
		if(js_CreateBranchObject(js_cx, js_glob, &service_client->branch)==NULL)
			break;

653
		/* Client Object */
654
655
656
		if(service_client->client!=NULL)
			if(js_CreateClientObject(js_cx, js_glob, "client", service_client->client, sock)==NULL)
				break;
657
658
659
660
661
662
663
664
665

		/* User Class */
		if(js_CreateUserClass(js_cx, js_glob, &scfg)==NULL) 
			break;

		/* Socket Class */
		if(js_CreateSocketClass(js_cx, js_glob)==NULL)
			break;

666
667
668
669
		/* MsgBase Class */
		if(js_CreateMsgBaseClass(js_cx, js_glob, &scfg)==NULL)
			break;

670
671
672
673
		/* File Class */
		if(js_CreateFileClass(js_cx, js_glob)==NULL)
			break;

674
675
676
677
		/* user-specific objects */
		if(!js_CreateUserObjects(js_cx, js_glob, &scfg, NULL, NULL, NULL)) 
			break;

678
		if(js_CreateSystemObject(js_cx, js_glob, &scfg, uptime, startup->host_name)==NULL) 
679
			break;
680
681
682
		
		/* server object */
		if((server=JS_DefineObject(js_cx, js_glob, "server", &js_server_class
683
			,NULL,JSPROP_ENUMERATE|JSPROP_READONLY))==NULL)
684
685
			break;

686
687
688
689
		if(!JS_DefineProperties(js_cx, server, js_server_properties))
			break;

		if(service_client->client==NULL)	/* static service */
690
691
692
			if(js_CreateSocketObject(js_cx, server, "socket", service_client->socket)==NULL)
				break;

693
		sprintf(ver,"Synchronet Services %s",revision);
694
695
696
		if((js_str=JS_NewStringCopyZ(js_cx, ver))==NULL)
			break;
		val = STRING_TO_JSVAL(js_str);
697
698
699
		if(!JS_SetProperty(js_cx, server, "version", &val))
			break;

700
701
702
		if((js_str=JS_NewStringCopyZ(js_cx, services_ver()))==NULL)
			break;
		val = STRING_TO_JSVAL(js_str);
703
704
		if(!JS_SetProperty(js_cx, server, "version_detail", &val))
			break;
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721

		if(glob!=NULL)
			*glob=js_glob;

		success=TRUE;

	} while(0);


	if(!success) {
		JS_DestroyContext(js_cx);
		return(NULL);
	}

	return(js_cx);
}

722
723
724
725
726
727
728
729
static JSBool
js_BranchCallback(JSContext *cx, JSScript *script)
{
	service_client_t* client;

	if((client=(service_client_t*)JS_GetContextPrivate(cx))==NULL)
		return(JS_FALSE);

730
	client->branch.counter++;
731
732
733

	/* Terminated? */ 
	if(terminated) {
734
		JS_ReportError(cx,"Terminated");
735
		client->branch.counter=0;
736
737
		return(JS_FALSE);
	}
738
	/* Infinite loop? */
739
740
741
	if(client->branch.limit && client->branch.counter > client->branch.limit) {
		JS_ReportError(cx,"Infinite loop (%lu branches) detected",client->branch.counter);
		client->branch.counter=0;
742
743
744
		return(JS_FALSE);
	}
	/* Give up timeslices every once in a while */
745
	if(client->branch.yield_freq && (client->branch.counter%client->branch.yield_freq)==0)
746
		YIELD();
747

748
	if(client->branch.gc_freq && (client->branch.counter%client->branch.gc_freq)==0)
749
750
		JS_MaybeGC(cx);

751
752
753
    return(JS_TRUE);
}

754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
static void js_init_cmdline(JSContext* js_cx, JSObject* js_obj, char* spath)
{
	char*					p;
	char*					args=NULL;
	int						argc=0;
	JSString*				arg_str;
	JSObject*				argv;
	jsval					val;

	p=spath;
	while(*p && *p>' ') p++;
	if(*p) {
		*p=0;	/* truncate arguments */
		args=p+1;
	}
	argv=JS_NewArrayObject(js_cx, 0, NULL);

	if(args!=NULL && argv!=NULL) {
		while(*args) {
			p=strchr(args,' ');
			if(p!=NULL)
				*p=0;
			while(*args && *args<=' ') args++; /* Skip spaces */
			arg_str = JS_NewStringCopyZ(js_cx, args);
			if(arg_str==NULL)
				break;
			val=STRING_TO_JSVAL(arg_str);
			if(!JS_SetElement(js_cx, argv, argc, &val))
				break;
			argc++;
			if(p==NULL)	/* last arg */
				break;
			args+=(strlen(args)+1);
		}
	}
	JS_DefineProperty(js_cx, js_obj, "argv", OBJECT_TO_JSVAL(argv)
790
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
791
	JS_DefineProperty(js_cx, js_obj, "argc", INT_TO_JSVAL(argc)
792
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
793
}
794
795
796

static void js_service_thread(void* arg)
{
797
	int						i;
798
799
800
801
802
	char*					host_name;
	HOSTENT*				host;
	SOCKET					socket;
	client_t				client;
	service_t*				service;
803
	service_client_t		service_client;
804
	/* JavaScript-specific */
805
	char					spath[MAX_PATH+1];
rswindell's avatar
rswindell committed
806
	JSString*				datagram;
807
808
	JSObject*				js_glob;
	JSScript*				js_script;
809
	JSRuntime*				js_runtime;
810
	JSContext*				js_cx;
rswindell's avatar
rswindell committed
811
	jsval					val;
812
813
	jsval					rval;

814
815
816
	// Copy service_client arg
	service_client=*(service_client_t*)arg;
	// Free original
817
818
819
820
821
	free(arg);

	socket=service_client.socket;
	service=service_client.service;

822
	lprintf("%04d %s JavaScript service thread started", socket, service->protocol);
823

824
	thread_up(TRUE /* setuid */);
825
826
827
828
829
830
831
832
833
834
835
836
837
838

	/* Host name lookup and filtering */
	if(service->options&BBS_OPT_NO_HOST_LOOKUP 
		|| startup->options&BBS_OPT_NO_HOST_LOOKUP)
		host=NULL;
	else
		host=gethostbyaddr((char *)&service_client.addr.sin_addr
			,sizeof(service_client.addr.sin_addr),AF_INET);

	if(host!=NULL && host->h_name!=NULL)
		host_name=host->h_name;
	else
		host_name="<no name>";

839
	if(!(service->options&BBS_OPT_NO_HOST_LOOKUP)
840
841
842
843
844
845
846
847
		&& !(startup->options&BBS_OPT_NO_HOST_LOOKUP)) {
		lprintf("%04d %s Hostname: %s"
			,socket, service->protocol, host_name);
		for(i=0;host!=NULL && host->h_aliases!=NULL 
			&& host->h_aliases[i]!=NULL;i++)
			lprintf("%04d %s HostAlias: %s"
				,socket, service->protocol, host->h_aliases[i]);
	}
848
849
850
851
852

	if(trashcan(&scfg,host_name,"host")) {
		lprintf("%04d !%s CLIENT BLOCKED in host.can: %s"
			,socket, service->protocol, host_name);
		close_socket(socket);
853
854
		if(service->clients)
			service->clients--;
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
		thread_down();
		return;
	}


#if 0	/* Need to export from SBBS.DLL */
	identity=NULL;
	if(service->options&BBS_OPT_GET_IDENT 
		&& startup->options&BBS_OPT_GET_IDENT) {
		identify(&service_client.addr, service->port, str, sizeof(str)-1);
		identity=strrchr(str,':');
		if(identity!=NULL) {
			identity++;	/* skip colon */
			while(*identity && *identity<=SP) /* point to user name */
				identity++;
			lprintf("%04d Identity: %s",socket, identity);
		}
	}
#endif

	client.size=sizeof(client);
	client.time=time(NULL);
877
878
	SAFECOPY(client.addr,inet_ntoa(service_client.addr.sin_addr));
	SAFECOPY(client.host,host_name);
879
880
881
	client.port=ntohs(service_client.addr.sin_port);
	client.protocol=service->protocol;
	client.user="<unknown>";
882
	service_client.client=&client;
883

884
885
886
887
	active_clients++;
	update_clients();

	/* Initialize client display */
888
	client_on(socket,&client,FALSE /* update */);
889

890
	if((js_runtime=JS_NewRuntime(startup->js_max_bytes))==NULL
891
		|| (js_cx=js_initcx(js_runtime,socket,&service_client,&js_glob))==NULL) {
892
893
		lprintf("%04d !%s ERROR initializing JavaScript context"
			,socket,service->protocol);
894
		client_off(socket);
895
		close_socket(socket);
896
897
898
899
		active_clients--;
		update_clients();
		if(service->clients)
			service->clients--;
900
901
902
903
904
		thread_down();
		return;
	}

	/* RUN SCRIPT */
905
906
907
	sprintf(spath,"%s%s",scfg.mods_dir,service->cmd);
	if(scfg.mods_dir[0]==0 || !fexist(spath))
		sprintf(spath,"%s%s",scfg.exec_dir,service->cmd);
908

909
	js_init_cmdline(js_cx, js_glob, spath);
rswindell's avatar
rswindell committed
910

911
912
913
	val = BOOLEAN_TO_JSVAL(JS_FALSE);
	JS_SetProperty(js_cx, js_glob, "logged_in", &val);

rswindell's avatar
rswindell committed
914
915
916
917
	if(service->options&SERVICE_OPT_UDP 
		&& service_client.udp_buf != NULL
		&& service_client.udp_len > 0) {
		datagram = JS_NewStringCopyN(js_cx, service_client.udp_buf, service_client.udp_len);
918
919
920
921
		if(datagram==NULL)
			val=JSVAL_VOID;
		else
			val = STRING_TO_JSVAL(datagram);
rswindell's avatar
rswindell committed
922
923
924
925
926
	} else
		val = JSVAL_VOID;
	JS_SetProperty(js_cx, js_glob, "datagram", &val);
	FREE_AND_NULL(service_client.udp_buf);

927
928
	JS_ClearPendingException(js_cx);

929
930
931
932
933
	js_script=JS_CompileFile(js_cx, js_glob, spath);

	if(js_script==NULL) 
		lprintf("%04d !JavaScript FAILED to compile script (%s)",socket,spath);
	else  {
934
		JS_SetBranchCallback(js_cx, js_BranchCallback);
rswindell's avatar
rswindell committed
935
		JS_ExecuteScript(js_cx, js_glob, js_script, &rval);
936
937
		JS_DestroyScript(js_cx, js_script);
	}
rswindell's avatar
rswindell committed
938
	JS_DestroyContext(js_cx);	/* Free Context */
939

940
941
942
943
944
945
946
947
	JS_DestroyRuntime(js_runtime);

	if(service_client.user.number) {
		lprintf("%04d %s Logging out %s"
			,socket, service->protocol, service_client.user.alias);
		logoutuserdat(&scfg,&service_client.user,time(NULL),service_client.logintime);
	}

948
949
	if(service->clients)
		service->clients--;
950
951
	active_clients--;
	update_clients();
952

953
954
955
956
957
958
#ifdef _WIN32
	if(startup->hangup_sound[0] && !(startup->options&BBS_OPT_MUTE)
		&& !(service->options&BBS_OPT_MUTE))
		PlaySound(startup->hangup_sound, NULL, SND_ASYNC|SND_FILENAME);
#endif

959
	thread_down();
960
961
	lprintf("%04d %s JavaScript service thread terminated (%u clients remain, %u total, %lu served)"
		, socket, service->protocol, service->clients, active_clients, service->served);
962

963
	client_off(socket);
964
	close_socket(socket);
965
966
}

967
static void js_static_service_thread(void* arg)
968
969
970
971
{
	char					spath[MAX_PATH+1];
	service_t*				service;
	service_client_t		service_client;
972
	SOCKET					socket;
973
974
975
976
977
978
979
980
981
982
983
	/* JavaScript-specific */
	JSObject*				js_glob;
	JSScript*				js_script;
	JSRuntime*				js_runtime;
	JSContext*				js_cx;
	jsval					val;
	jsval					rval;

	// Copy service_client arg
	service=(service_t*)arg;

984
985
986
987
	service->running=TRUE;
	socket = service->socket;

	lprintf("%04d %s static JavaScript service thread started", service->socket, service->protocol);
988
989
990
991
992
993

	thread_up(TRUE /* setuid */);

	memset(&service_client,0,sizeof(service_client));
	service_client.socket = service->socket;
	service_client.service = service;
994
995
996
	service_client.branch.limit = JAVASCRIPT_BRANCH_LIMIT;
	service_client.branch.gc_freq = JAVASCRIPT_GC_FREQUENCY;
	service_client.branch.yield_freq = JAVASCRIPT_YIELD_FREQUENCY;
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007

	if((js_runtime=JS_NewRuntime(startup->js_max_bytes))==NULL
		|| (js_cx=js_initcx(js_runtime,service->socket,&service_client,&js_glob))==NULL) {
		lprintf("%04d !%s ERROR initializing JavaScript context"
			,service->socket,service->protocol);
		close_socket(service->socket);
		service->socket=INVALID_SOCKET;
		thread_down();
		return;
	}

1008
1009
1010
1011
	sprintf(spath,"%s%s",scfg.mods_dir,service->cmd);
	if(scfg.mods_dir[0]==0 || !fexist(spath))
		sprintf(spath,"%s%s",scfg.exec_dir,service->cmd);

1012
1013
1014
1015
1016
	js_init_cmdline(js_cx, js_glob, spath);

	val = BOOLEAN_TO_JSVAL(JS_FALSE);
	JS_SetProperty(js_cx, js_glob, "logged_in", &val);

1017
1018
	JS_ClearPendingException(js_cx);

1019
1020
1021
1022
1023
1024
	js_script=JS_CompileFile(js_cx, js_glob, spath);

	if(js_script==NULL) 
		lprintf("%04d !JavaScript FAILED to compile script (%s)",service->socket,spath);
	else  {
		JS_SetBranchCallback(js_cx, js_BranchCallback);
1025
1026
1027
1028
1029

		do {
			JS_ExecuteScript(js_cx, js_glob, js_script, &rval);
		} while(!service->terminated && service->options&SERVICE_OPT_STATIC_LOOP);

1030
1031
1032
1033
1034
1035
1036
		JS_DestroyScript(js_cx, js_script);
	}
	JS_DestroyContext(js_cx);	/* Free Context */

	JS_DestroyRuntime(js_runtime);

	thread_down();
1037
1038
	lprintf("%04d %s static JavaScript service thread terminated (%lu clients served)"
		,socket, service->protocol, service->served);
1039
1040
1041

	close_socket(service->socket);
	service->socket=INVALID_SOCKET;
1042
1043

	service->running=FALSE;
1044
1045
}

1046
static void native_static_service_thread(void* arg)
1047
1048
1049
{
	char					cmd[MAX_PATH];
	char					fullcmd[MAX_PATH*2];
1050
	SOCKET					socket;
1051
1052
1053
1054
1055
	SOCKET					socket_dup;
	service_t*				service;

	service = (service_t*)arg;

1056
1057
1058
1059
	service->running=TRUE;
	socket = service->socket;

	lprintf("%04d %s static service thread started", socket, service->protocol);
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071

	thread_up(TRUE /* setuid */);

#ifdef _WIN32
	if(!DuplicateHandle(GetCurrentProcess(),
		(HANDLE)socket,
		GetCurrentProcess(),
		(HANDLE*)&socket_dup,
		0,
		TRUE, // Inheritable
		DUPLICATE_SAME_ACCESS)) {
		lprintf("%04d !%s ERROR %d duplicating socket descriptor"
1072
			,socket,service->protocol,GetLastError());
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
		close_socket(service->socket);
		service->socket=INVALID_SOCKET;
		thread_down();
		return;
	}
#else
	socket_dup = dup(service->socket);
#endif

	/* RUN SCRIPT */
	if(strpbrk(service->cmd,"/\\")==NULL)
		sprintf(cmd,"%s%s",scfg.exec_dir,service->cmd);
	else
		strcpy(cmd,service->cmd);
	sprintf(fullcmd,cmd,socket_dup);
1088
1089
1090
1091
	
	do {
		system(fullcmd);
	} while(!service->terminated && service->options&SERVICE_OPT_STATIC_LOOP);
1092
1093

	thread_down();
1094
1095
	lprintf("%04d %s static service thread terminated (%lu clients served)"
		,socket, service->protocol, service->served);
1096
1097
1098
1099

	close_socket(service->socket);
	service->socket=INVALID_SOCKET;
	closesocket(socket_dup);	/* close duplicate handle */
1100
1101

	service->running=FALSE;
1102
1103
}

1104
1105
static void native_service_thread(void* arg)
{
1106
	int						i;
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
	char					cmd[MAX_PATH];
	char					fullcmd[MAX_PATH*2];
	char*					host_name;
	HOSTENT*				host;
	SOCKET					socket;
	SOCKET					socket_dup;
	client_t				client;
	service_t*				service;
	service_client_t		service_client=*(service_client_t*)arg;

	free(arg);

	socket=service_client.socket;
	service=service_client.service;

	lprintf("%04d %s service thread started", socket, service->protocol);

1124
	thread_up(TRUE /* setuid */);
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138

	/* Host name lookup and filtering */
	if(service->options&BBS_OPT_NO_HOST_LOOKUP 
		|| startup->options&BBS_OPT_NO_HOST_LOOKUP)
		host=NULL;
	else
		host=gethostbyaddr((char *)&service_client.addr.sin_addr
			,sizeof(service_client.addr.sin_addr),AF_INET);

	if(host!=NULL && host->h_name!=NULL)
		host_name=host->h_name;
	else
		host_name="<no name>";

1139
	if(!(service->options&BBS_OPT_NO_HOST_LOOKUP)
1140
1141
1142
1143
1144
1145
1146
1147
		&& !(startup->options&BBS_OPT_NO_HOST_LOOKUP)) {
		lprintf("%04d %s Hostname: %s"
			,socket, service->protocol, host_name);
		for(i=0;host!=NULL && host->h_aliases!=NULL 
			&& host->h_aliases[i]!=NULL;i++)
			lprintf("%04d %s HostAlias: %s"
				,socket, service->protocol, host->h_aliases[i]);
	}
1148
1149
1150
1151
1152

	if(trashcan(&scfg,host_name,"host")) {
		lprintf("%04d !%s CLIENT BLOCKED in host.can: %s"
			,socket, service->protocol, host_name);
		close_socket(socket);
1153
1154
		if(service->clients)
			service->clients--;
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
		thread_down();
		return;
	}


#if 0	/* Need to export from SBBS.DLL */
	identity=NULL;
	if(service->options&BBS_OPT_GET_IDENT 
		&& startup->options&BBS_OPT_GET_IDENT) {
		identify(&service_client.addr, service->port, str, sizeof(str)-1);
		identity=strrchr(str,':');
		if(identity!=NULL) {
			identity++;	/* skip colon */
			while(*identity && *identity<=SP) /* point to user name */
				identity++;
			lprintf("%04d Identity: %s",socket, identity);
		}
	}
#endif

	client.size=sizeof(client);
	client.time=time(NULL);
1177
1178
	SAFECOPY(client.addr,inet_ntoa(service_client.addr.sin_addr));
	SAFECOPY(client.host,host_name);
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
	client.port=ntohs(service_client.addr.sin_port);
	client.protocol=service->protocol;
	client.user="<unknown>";

#ifdef _WIN32
	if(!DuplicateHandle(GetCurrentProcess(),
		(HANDLE)socket,
		GetCurrentProcess(),
		(HANDLE*)&socket_dup,
		0,
		TRUE, // Inheritable
		DUPLICATE_SAME_ACCESS)) {
		lprintf("%04d !%s ERROR %d duplicating socket descriptor"
			,socket,service->protocol,GetLastError());
		close_socket(socket);
		thread_down();
		return;
	}
#else
	socket_dup = dup(socket);
#endif

	active_clients++;
	update_clients();

	/* Initialize client display */
1205
	client_on(socket,&client,FALSE /* update */);
1206
1207

	/* RUN SCRIPT */
1208
	if(strpbrk(service->cmd,"/\\")==NULL)
1209
1210
1211
1212
1213
1214
1215
		sprintf(cmd,"%s%s",scfg.exec_dir,service->cmd);
	else
		strcpy(cmd,service->cmd);
	sprintf(fullcmd,cmd,socket_dup);

	system(fullcmd);

1216
1217
	if(service->clients)
		service->clients--;
1218
1219
	active_clients--;
	update_clients();
1220

1221
1222
1223
1224
1225
1226
#ifdef _WIN32
	if(startup->hangup_sound[0] && !(startup->options&BBS_OPT_MUTE)
		&& !(service->options&BBS_OPT_MUTE))
		PlaySound(startup->hangup_sound, NULL, SND_ASYNC|SND_FILENAME);
#endif

1227
	thread_down();
1228
1229
	lprintf("%04d %s service thread terminated (%u clients remain, %u total, %lu served)"
		,socket, service->protocol, service->clients, active_clients, service->served);
1230

1231
	client_off(socket);
1232
1233
	close_socket(socket);
	closesocket(socket_dup);	/* close duplicate handle */
1234
1235
}

1236

1237
1238
void DLLCALL services_terminate(void)
{
1239
1240
	DWORD i;

1241
	terminated=TRUE;
1242
1243
1244
1245
1246
1247
	for(i=0;i<services;i++) {
		if(service[i].socket==INVALID_SOCKET)
			continue;
		close_socket(service[i].socket);
		service[i].socket=INVALID_SOCKET;
	}
1248
1249
1250
1251
}

#define NEXT_FIELD(p)	while(*p && *p>' ') p++; while(*p && *p<=' ') p++;

1252
static service_t* read_services_cfg(service_t* service, char* services_cfg, DWORD* services)
1253
1254
1255
1256
1257
{
	char*	p;
	char*	tp;
	char	line[1024];
	FILE*	fp;
1258
1259
1260
1261
	service_t*	np;
	
	if((fp=fopen(services_cfg,"r"))==NULL)
		return(service);
1262

1263
	lprintf("Reading %s",services_cfg);
1264
	for((*services)=0;!feof(fp) && (*services)<MAX_SERVICES;) {
1265
		if(!fgets(line,sizeof(line),fp))
1266
1267
1268
1269
1270
1271
			break;
		p=line;
		while(*p && *p<=' ') p++;
		if(*p==0 || *p==';')	/* ignore blank lines or comments */
			continue;
		
1272
		if((np=(service_t*)realloc(service,sizeof(service_t)*((*services)+1)))==NULL) {
1273
			lprintf("!MALLOC FAILURE");
1274
			return(service);
1275
		}
1276
		service=np;
1277
1278
		memset(&service[*services],0,sizeof(service_t));
		service[*services].socket=INVALID_SOCKET;
1279
1280
1281
1282

		tp=p; 
		while(*tp && *tp>' ') tp++; 
		*tp=0;
1283
		sprintf(service[*services].protocol,"%.*s",(int)sizeof(service[0].protocol)-1,p);
1284
		p=tp+1;
1285
		while(*p && *p<=' ') p++;
1286
		service[*services].port=atoi(p);
1287
		NEXT_FIELD(p);
1288
		service[*services].max_clients=strtol(p,NULL,10);
1289
		NEXT_FIELD(p);
1290
		service[*services].options=strtol(p,NULL,16);
1291
1292
		NEXT_FIELD(p);

1293
		sprintf(service[*services].cmd,"%.*s",(int)sizeof(service[0].cmd)-1,p);
1294
		truncsp(service[*services].cmd);
1295

1296
		(*services)++;
1297
1298
1299
	}
	fclose(fp);

1300
	return(service);
1301
1302
}

1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
static service_t* read_services_ini(service_t* service, char* services_ini, DWORD* services)
{
	uint		i,j;
	FILE*		fp;
	char		cmd[MAX_LINE_LEN+1];
	char**		sec_list;
	service_t*	np;
	service_t	serv;

	if((fp=fopen(services_ini,"r"))==NULL)
		return(service);

	lprintf("Reading %s",services_ini);
	sec_list = iniGetSectionList(fp,"");
    for(i=0; sec_list!=NULL && sec_list[i]!=NULL; i++) {
		memset(&serv,0,sizeof(service_t));
		SAFECOPY(serv.protocol,sec_list[i]);
		serv.socket=INVALID_SOCKET;
		serv.port=iniGetShortInt(fp,sec_list[i],"Port",0);
		serv.max_clients=iniGetInteger(fp,sec_list[i],"MaxClients",0);
		serv.options=iniGetBitField(fp,sec_list[i],"Options",service_options,0);
		SAFECOPY(serv.cmd,iniGetString(fp,sec_list[i],"Command","",cmd));

		for(j=0;j<*services;j++)
			if(service[j].port==serv.port && service[j].options==serv.options)
				break;
		if(j<*services)	{ /* ignore duplicate services */
			lprintf("Ignoring duplicate service: %s",sec_list[i]);
			continue;
		}
		if((np=(service_t*)realloc(service,sizeof(service_t)*((*services)+1)))==NULL) {
			fclose(fp);
			lprintf("!MALLOC FAILURE");
			return(service);
		}
		service=np;
		service[*services]=serv;
		(*services)++;
	}
	iniFreeStringList(sec_list);

	fclose(fp);

	return(service);
}


1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
static void cleanup(int code)
{
	free_cfg(&scfg);

	update_clients();

#ifdef _WINSOCKAPI_	
	if(WSAInitialized && WSACleanup()!=0) 
		lprintf("0000 !WSACleanup ERROR %d",ERROR_VALUE);
#endif

1361
	thread_down();
1362
    lprintf("#### Services thread terminated (%lu clients served)",served);
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
	status("Down");
	if(startup!=NULL && startup->terminated!=NULL)
		startup->terminated(code);
}

const char* DLLCALL services_ver(void)
{
	static char ver[256];
	char compiler[32];

1373
	DESCRIBE_COMPILER(compiler);
1374

1375
	sscanf("$Revision$", "%*s %s", revision);
1376
1377

	sprintf(ver,"Synchronet Services %s%s  "
1378
		"Compiled %s %s with %s"
1379
		,revision
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
#ifdef _DEBUG
		," Debug"
#else
		,""
#endif
		,__DATE__, __TIME__, compiler
		);

	return(ver);
}

void DLLCALL services_thread(void* arg)
{
1393
	char			path[MAX_PATH+1];
1394
	char			error[256];
1395
1396
	char			host_ip[32];
	char			compiler[32];
1397
	char			str[128];
1398
	char			cmd[128];
1399
1400
	SOCKADDR_IN		addr;
	SOCKADDR_IN		client_addr;
1401
	socklen_t		client_addr_len;