File: freepops.c

package info (click to toggle)
freepops 0.2.7-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 5,464 kB
  • ctags: 2,677
  • sloc: ansic: 16,571; sh: 1,781; makefile: 1,127; cpp: 350
file content (768 lines) | stat: -rw-r--r-- 18,762 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
/******************************************************************************
 * $Id: freepops.c,v 1.41 2007/10/27 13:17:24 gareuselesinge Exp $
 * This file is part of FreePOPs (http://www.freepops.org)                    *
 * This file is distributed under the terms of GNU GPL license.               *
 ******************************************************************************/

/******************************************************************************
 * File description:
 *	The main function is here
 * Notes:
 * 	Options --kill added by Stefano Falsetto <falsetto@gnu.org>
 *
 * Authors:
 * 	Alessio Caprari <alessio.caprari@tiscali.it>
 * 	Nicola Cocchiaro <ncocchiaro@users.sourceforge.net>
 * 	Enrico Tassi <gareuselesinge@users.sourceforge.net>
 ******************************************************************************/

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include <curl/curl.h>

#if !(defined(WIN32) && !defined(CYGWIN)) // && !defined(BEOS)
	#include <sys/types.h>
	#include <sys/stat.h>
	#include <netinet/in.h>
	#include <arpa/inet.h>
	#include <netdb.h>
	#include <signal.h>
	#include <grp.h>
	#include <pwd.h>
	#include <errno.h>
#endif

#if defined(WIN32) && !defined(CYGWIN)
	#include <winsock.h>
	#include "winsystray.h"
#endif

#ifdef MACOSX
	#include "getopt1.h"
#else
	#include <getopt.h>
#endif

#ifdef HAVE_CONFIG_H
#      include "config.h"
#endif

#if CRYPTO_IMPLEMENTATION == 1
	#include <pthread.h>
	#include <gcrypt.h>
#endif

#include "popserver.h"
#include "engine.h"
#include "altsocklib.h"
#include "regularexp.h"
#include "stats.h"
#include "session.h"

#include "lua.h"
#include "luay.h"
#include "luabox.h"
#include "lauxlib.h"
#include "luay.h"

#include "log.h"
#define LOG_ZONE "INTERNAL"

#include "pid.h"

#define HIDDEN static

#define PLUGIN2XML "plugins2xml.lua"

/*** typedefs *****************************************************************/
typedef void (*sighandler_t)(int);

/*** globals set trough command line options *******************************/

int verbose_output=0;
int daemonize = 0;
char *logfile = NULL;
char **args = NULL;
int args_len = 0;

#if !(defined(WIN32) && !defined(CYGWIN)) && !defined(BEOS)
uid_t uid;
gid_t gid; 
#endif

/* extra arguement pool handling */
HIDDEN void add_to_args(const char * arg){
	args_len++;
	args = realloc(args,args_len * sizeof(void*));
	args[args_len - 1] = strdup(arg);
}

/*** usage ********************************************************************/
#define GETOPT_STRING "-b:p:P:A:u:t:l:s:dhVvwknx:e:c:"
HIDDEN  struct option opts[] = { 
	{ "bind", required_argument, NULL, 'b' },
	{ "port", required_argument, NULL, 'p' },
	{ "proxy", required_argument, NULL, 'P' },
	{ "auth", required_argument, NULL, 'A' },
	{ "useragent", required_argument, NULL, 'u' },
	{ "threads", required_argument, NULL, 't' },
	{ "help" , no_argument , NULL, 'h'},
	{ "version" , no_argument , NULL , 'V'},
	{ "verbose", no_argument, NULL, 'v' },
	{ "veryverbose", no_argument, NULL, 'w' },
	{ "logmode", required_argument, NULL, 'l' },
	{ "daemonize", no_argument, NULL, 'd' },
	{ "suid", required_argument, NULL, 's' },
	{ "kill", no_argument, NULL, 'k' },
	{ "no-pid-file", no_argument, NULL, 'n' },
	{ "toxml", required_argument, NULL, 'x' },
	{ "force-proxy-auth-type", required_argument, NULL, 1000 },
	{ "fpat", required_argument, NULL, 1000 },
	{ "no-icon",no_argument, NULL, 1001},
	{ "execute",required_argument, NULL, 'e'},
	{ "conffile",required_argument, NULL, 'c'},
	{ "statistics-all",no_argument, NULL, 1002},
	{ "statistics-session-created",no_argument, NULL, 1003},
	{ "statistics-session-ok",no_argument, NULL, 1004},
	{ "statistics-session-err",no_argument, NULL, 1005},
	{ "statistics-connection-established",no_argument, NULL, 1006},
	{ "statistics-cookies",no_argument, NULL, 1007},
	{ "statistics-pwd-file",required_argument, NULL, 1008},
	{ NULL, 0, NULL, 0 } };

HIDDEN void usage(const char *progname) {
	fprintf(stderr, 
"Usage:  %s\t[-b|--bind address] \n"
"\t\t\t[-p|--port portnumber] \n"
"\t\t\t[-P|--proxy proxyaddress:proxyport] \n"
"\t\t\t[-A|--auth username:password] \n"
"\t\t\t[-u|--useragent useragent] \n"
"\t\t\t[-v|--verbose [-v| --verbose]]\n"
"\t\t\t[-w|--veryverbose]\n"
"\t\t\t[-t|--threads num]\n"
"\t\t\t[-d|--daemonize]\n"
"\t\t\t[-l|--logmode (syslog|filename|stdout)]\n"
"\t\t\t[-x|--toxml file]\n"
"\t\t\t[-e|--execute scriptfile [args...]]\n"
"\t\t\t[-c|--conffile configurationfile]\n"
"\t\t\t[--fpat|--force-proxy-auth-type (basic|digest|ntlm|gss)]\n"
"\t\t\t[--statistics-all]\n"
"\t\t\t[--statistics-session-created]\n"
"\t\t\t[--statistics-session-ok]\n"
"\t\t\t[--statistics-session-err]\n"
"\t\t\t[--statistics-connection-established]\n"
"\t\t\t[--statistics-cookies]\n"
"\t\t\t[--statistics-pwd-file filename]\n"
#if defined(WIN32)
"\t\t\t[--no-icon]\n"
#endif
#if !defined(WIN32) && !defined(BEOS)			
"\t\t\t[-s|--suid user.group]\n"
"\t\t\t[-k|--kill]\n"
"\t\t\t[-n|--no-pid-file]\n"
#endif
"        %s\t[-V|--version]\n"
"        %s\t[-h|--help]\n\n", progname, progname, progname);
}

/*** WIN32 only functions *****************************************************/
#if defined(WIN32) && !defined(CYGWIN)

HIDDEN void win32_init(int *argc,char ***argv,LPSTR lpszCmdLine) {
	char *lastslash;
	  
	if(stderr != freopen("stderr.txt","w",stderr)) 
		fprintf(stderr,"Unable to redirect stderr\n");

	if(stdout != freopen("stdout.txt","w",stdout)) 
		fprintf(stderr,"Unable to redirect stdout\n");		
	*argc = parse_commandline(argv, lpszCmdLine);

	/* Try to change working directory if command line provides a path */
	if ((lastslash = strrchr((*argv)[0], '\\')) != NULL) {
		char *dir;
		int length = lastslash - (*argv)[0] + 2;
		if (length <= MAX_PATH &&
		    (dir = (char *)calloc(length, sizeof(char))) != NULL) {
			strncpy(dir, (*argv)[0], length - 1);
			dir[length - 1] = '\0';
			SetCurrentDirectory(dir);
			free(dir);
		}
	}

	sockinit();
}

HIDDEN void win32_exit(){
	fclose(stdout);
	fclose(stderr);
}
#endif

/*** unix only functions ******************************************************/
#if !(defined(WIN32) && !defined(CYGWIN)) && !defined(BEOS)
HIDDEN int *sighandler(int sig){
	if (sig == SIGINT || sig == SIGTERM) {
		remove_pid_file();
		SAY("%s killed by %d\n\n",PROGRAMNAME,sig);
		LOG_END();
		exit(0);
	} else {
		SAY("what?\n");
	}

	return 0;
}

HIDDEN void daemonize_process(){
	if(fork()!=0)
		{
		exit(0);
		}

	setsid();

	setpgid(0, 0);

	close(0);
	if(logfile != NULL && !strcmp(logfile,"stdout") && verbose_output != 0)
		{
		SAY("Can't log to %s and daemonize!\n",logfile);
		ERROR_ABORT("Bailing out!\n");
		}
	close(1);
	close(2);
}

HIDDEN int loose_rights(uid_t set_uid,gid_t set_gid){
	int rc=0;

	rc = setegid(set_gid);
	if(rc == -1)
		{
		SAY("Unable to setegid(%d)",set_gid);
		return 1;	
		}

	rc = seteuid(set_uid);
	if(rc == -1)
		{
		SAY("Unable to seteuid(%d)",set_uid);
		return 1;	
		}

	rc = setregid(getegid(),getegid());
	if(rc == -1)
		{
		SAY("Unable to setregid(%d)",getegid(),getegid());
		return 1;	
		}

	rc = setreuid(geteuid(),geteuid());
	if(rc == -1)
		{
		SAY("Unable to setreuid(%d,%d)",geteuid(),geteuid());
		return 1;	
		}

	return rc;
}

HIDDEN void set_signals(){
	// signal for ctrl+c 	
	signal(SIGINT,  (sighandler_t) sighandler);
	// signal for debian start-stop-daemon
	signal(SIGTERM, (sighandler_t) sighandler);

	//probably needed only by MACOSX and FreeBSD systems
	signal(SIGPIPE,SIG_IGN);
}

HIDDEN void parse_suid(const char* optarg){
	int rc;

	if(optarg == NULL)
		fprintf(stderr,"%s : %s : %d : optarg cant be NULL\n"
			,__FILE__,__FUNCTION__,__LINE__);

	//fprintf(stderr,"optarg = ^%s$\n",optarg);

	if( (rc = regfind_start(optarg,"^[A-Za-z_]+\\.[A-Za-z_]+$")) != -1 )
		{
		//alphabetic form
		struct passwd *pw;
		struct group *gr;
		char *tmp = strdup(optarg);

		rc = regfind_end(tmp,"^[A-Za-z_]+\\.");
		rc--;

		tmp[rc] = '\0';
		pw = getpwnam(tmp);
		if(pw == NULL)
			{
			fprintf(stderr,"Unable to getpwnam(\"%s\")\n",tmp);
			fprintf(stderr,"getpwnam: %s\n",strerror(errno));
			goto error;
			}
		uid = pw->pw_uid;

		rc++;
		gr = getgrnam(&tmp[rc]);
		if(gr == NULL)
			{
			fprintf(stderr,"Unable to getgrnam(\"%s\")\n",&tmp[rc]);
			fprintf(stderr,"getgrnam: %s\n",strerror(errno));
			goto error;
			}
		gid = gr->gr_gid;

		//fprintf(stderr,"uid = %d gid = %d\n",uid,gid);
		
		free(tmp);
		}
	else if ( (rc = regfind_start(optarg,"^[0-9]+\\.[0-9]+$")) != -1 )
		{
		//numeric form
		char *tmp = strdup(optarg);

		rc = regfind_end(tmp,"^[0-9]+\\.");
		rc--;

		tmp[rc] = '\0';
		uid = strtol(tmp,NULL,10);
		if(errno == ERANGE)
			goto error;			
		//printf("-> ^%s$\n",tmp);

		rc++;
		gid = strtol(&tmp[rc],NULL,10);
		if(errno == ERANGE)
			goto error;	
		//printf("-> ^%s$\n",&tmp[rc]);
		
		//fprintf(stderr,"uid = %d gid = %d\n",uid,gid);
		
		free(tmp);	
		}
	else
		goto error;

	return;

	error:
		fprintf(stderr,"Invalid parameter for -s --suid.\n");
		fprintf(stderr,"usage: -s username.group\n");
		perror("bailing out");
		exit(1);	
}

#endif


/*** helpers ******************************************************************/
HIDDEN void start_logging(char* logfile,int verbosity) {
	log_set_verbosity(verbosity);
	LOG_INIT(logfile);
	SAY("freepops started with loglevel %d on a %s machine.\n",verbosity,
		((unsigned short)1 != htons(1))?"little endian":"big endian");
}
/*** LUA INTERPRETER BOOTSTRAPPING (for using FP as luafull) *****************/

/* start the interpreter */
HIDDEN int execute(const char* scriptfile, const char* stdoutname){
	int rc, e;
	
	// boot
	lua_State* l = bootstrap(NULL,NULL);
	if(l == NULL){
		ERROR_PRINT("Unable to bootstrap\n");
		return -1;
	}
	
	// load the script 
	e = luay_call(l, "s|b", "freepops.dofile", scriptfile, &rc);
	if(!rc || e){
		ERROR_PRINT("Unable to load %s\n",scriptfile);
		return -1;
	}
	
	// begin redirection
	if (stdoutname != NULL) {
		if(stdout != freopen(stdoutname,"w",stdout)) {
			fprintf(stderr,"Unable to redirect stdout to %s\n",
					stdoutname);	
		}
	}
	
	// main 
	e = luay_callv(l, "|d", "main", args, args_len , &rc);
	if (e) {
		fprintf(stderr,"The main function raised an error\n");
		luay_printstack(l);
		return e;
	}
	// end redirection 
	if (stdoutname != NULL) {
		fclose(stdout);
	}
	
	return rc;
}

void my_putenv(const char* a, const char* b) {
	char * tmp = calloc(1024,sizeof(char));

	if(b == NULL)
		return;

	snprintf(tmp,1024,"%s=%s",a,b);

	/* on some system this is not a proper leak since tmp is used (not
	 * copyed) in the environment.
	 *
	 * valgrind says this is the correct thing to do, so I prefer calling
	 * this memory leak function only in the main, outside loops. so, even
	 * memory is "lost" it is not a memory leak.
	 */
	putenv(tmp);
}

void load_pwd_file(const char *varname, const char *pwdfilename){
	FILE* f = fopen(pwdfilename,"r");
	if (f != NULL){
		char buff[100];
		size_t got;
		got = fread(buff,1,99,f);
		if (! feof(f)) {
			fprintf(stderr, "Error reading %s.\n", pwdfilename);
			perror("fread");
			exit(1);
		} else {
			buff[got] = '\0';
			my_putenv(varname,buff);
		}
	} else {
		fprintf(stderr, "Unable to open file '%s' for reading.\n", pwdfilename);
		perror("fopen");
		exit(1);	
	}
}

/*** THE MAIN HAS YOU *********************************************************/
#if CRYPTO_IMPLEMENTATION == 1
	GCRY_THREAD_OPTION_PTHREAD_IMPL;
#endif

#if !(defined(WIN32) && !defined(CYGWIN))
int main(int argc, char *argv[]) {
#else
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    LPSTR lpszCmdLine, int nCmdShow){
#endif
	int res;
	unsigned long int active_stats=0;
	int threads_num = MAXTHREADS;
	unsigned short port = POP3PORT;
	struct in_addr address;
	char *useragent = NULL, *proxy = NULL, *proxyauth = NULL, *fpat = NULL;
	char *script = NULL, *execute_stdout = NULL;
	char *conffile = NULL, *stats_pwd = NULL;

#if defined(WIN32)	
	int tray_icon = 1;
#endif

#if !(defined(WIN32) && !defined(CYGWIN)) && !defined(BEOS)	
        pid_t this_pid;
	int no_pid = 0; /* by default we want the pid file */
#endif

#if defined(WIN32) && !defined(CYGWIN)
	int argc;
	char **argv;

	win32_init(&argc,&argv,lpszCmdLine);
#endif


#if CRYPTO_IMPLEMENTATION == 1
       if (!gcry_check_version("1.2.2")) {
	       fprintf(stderr, "gcrypt version older than 1.2.2\n");
	       exit(1);
	}
	gcry_control(GCRYCTL_SET_THREAD_CBS,&gcry_threads_pthread);
	gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
#endif
	
	curl_global_init(CURL_GLOBAL_ALL);
	
#if !(defined(WIN32) && !defined(CYGWIN)) && !defined(BEOS)	
	uid = geteuid();
	gid = getegid();
#endif

	address.s_addr = htonl(BINDADDRESS);
	logfile = strdup(LOGFILE); //means stdout
	
/*** ARGUMENTS PARSING ***/
	while (script == NULL && /* all arguments left are for the script*/
	(res=getopt_long(argc,argv,GETOPT_STRING,opts,NULL))!= -1){
		if (res == 'p') {
			/* --port */
#if defined(MACOSX)
			/* ignore .app parameter */
			if (strncmp(optarg, "sn_", 3))
			{
#endif
			if ((port = atoi(optarg)) == 0) {
				fprintf(stderr, "Invalid port number\n");
				usage(argv[0]);
				exit(1);
			}
#if defined(MACOSX)
			}
#endif
		} else if (res == 'b') {
			/* --bind */
			struct hostent *host;
			
			host = gethostbyname(optarg);
			if (host == NULL) {
				usage(argv[0]);
				exit(1);
			} else {
				address = *(struct in_addr*)(host->h_addr);
			}
		} else if (res == 'P') {
			/* --proxy */
			if (proxy != NULL) {
				usage(argv[0]);
				exit(1);
			}
			proxy = strdup(optarg);
		} else if (res == 'A') {
			/* --auth */
			if (proxyauth != NULL) {
				//usage(argv[0]);
				
				exit(1);
			}
			proxyauth = strdup(optarg);
		} else if (res == 'u') {
			/* --useragent */
			if (useragent != NULL) {
				usage(argv[0]);
				exit(1);
			}
			useragent = strdup(optarg);
		} else if (res == 't'){
			/*--threads */
			if ((threads_num = atoi(optarg)) == 0) {
				fprintf(stderr, 
					"Invalid max-threads-number number\n");
				usage(argv[0]);
				exit(1);
			}
		} else if (res == 'V') {
			/* --version */
			fprintf(stderr, "%s %s\n",
				PROGRAMNAME,VERSION);
			return 0;
		} else if (res == 'h') {	
			/* --help */
			usage(argv[0]);
			return 0;
		} else if (res == 'v') {
			/* --verbose */
			verbose_output++;
		} else if (res == 'l') {
			/* --logmode */
			free(logfile);
			if ((optarg != NULL) && (optarg[0] == '-'))
				fprintf(stderr, "Warning: using %s as logfile"
					" name, which is probably not what"
					" you want.\n", optarg);
			logfile = strdup(optarg);
		} else if (res == 'd') {
			/* --daemonize */
			daemonize = 1;
			free(logfile);
			logfile = NULL; 
		} else if (res == 'w') {
			/* --veryverbose */
			verbose_output+=2;
	#if defined(WIN32)
		} else if (res == 1001) {
			tray_icon = 0;		
	#endif
		} else if (res == 1000) {
			/* --fpat */
			free(fpat);
			
			if (optarg != NULL)
				fpat = strdup(optarg);
			else
				fprintf(stderr,"fpat has NULL arg");
			
			if ( strcmp(fpat,"gss") && 
			     strcmp(fpat,"ntlm") &&
			     strcmp(fpat,"basic") && 
			     strcmp(fpat,"digest") ) {
				fprintf(stderr, "--fpath accepts only one of"
					"these: gss, ntlm, basic, digest\n\n");
				usage(argv[0]);
				exit(1);
			}
	#if !(defined(WIN32) && !defined(CYGWIN)) && !defined(BEOS)
		} else if (res == 'k') {
			/* --kill */
			/* Kill freepopsd with pid contained in .pid file */
			this_pid = retrieve_pid_file(PIDFILE);
			if (this_pid != PIDERROR) {
				kill((pid_t)this_pid, SIGINT);
                                exit(0);
                        } else {
				printf("Warning: can't find a pid file.\n");
				exit(1);
			}
		} else if (res == 's') {
			/* --suid */
			parse_suid(optarg);
		} else if (res == 'n') {
			/* --no-pid-file */
			no_pid = 1;
	#endif			
		} else if (res == 'x') {
			/* --toxml */
			int i;
		#ifdef WIN32
			int len = strlen(optarg) + 5;
			char *outname = calloc(len,sizeof(char));
			if ( outname == NULL) {
				fprintf(stderr,"Unable to malloc\n");
				exit(1);
			}
			snprintf(outname,len,"%s.xml",optarg);
			free(execute_stdout);
			execute_stdout=outname;
		#endif			
			free(script);
			script=strdup(PLUGIN2XML);
			add_to_args(optarg);
			for (i=optind;i<argc;i++){
				add_to_args(argv[i]);
			}
		} else if (res == 'e'){
			/* --execute */
			int i;
			free(script);
			script=strdup(optarg);
			free(execute_stdout);
			execute_stdout=NULL;
			for (i=optind;i<argc;i++){
				add_to_args(argv[i]);
			}
		} else if (res == 'c') {
			free(conffile);
			conffile=strdup(optarg);
		} else if (res == 1){
			/* extra arguments */
			add_to_args(optarg);
		} else if (res == 1002) {
			/* statistics-all */
			active_stats |= STATS_ALL;
		} else if (res == 1003) {
			/* statistics-session-created */
			active_stats |= STATS_SESSION_CREATED;
		} else if (res == 1004) {
			/* statistics-session-ok */
			active_stats |= STATS_SESSION_OK;
		} else if (res == 1005) {
			/* statistics-session-err */
			active_stats |= STATS_SESSION_ERR;
		} else if (res == 1006) {
			/* statistics-connection-established */
			active_stats |= STATS_CONNECTION_ESTABLISHED;
		} else if (res == 1007) {
			/* statistics-cookies */
			active_stats |= STATS_COOKIES;
		} else if (res == 1008) {
			/* statistics-pwd-file */
			free(stats_pwd);
			stats_pwd = strdup(optarg);
		} else {
			/* unknown param */
			usage(argv[0]);
			exit(1);
		}
	}
	
/*** INITIALIZATION ALL ***/
	
	srand(time(NULL) + getpid());
	
	if (stats_pwd != NULL)
		load_pwd_file("FREEPOPSLUA_STATS_PWD",stats_pwd);
	
	start_logging(logfile,verbose_output);
	session_init();
	
	if(useragent == NULL)
		useragent = strdup(DEFAULT_USERAGENT);
	
	my_putenv("FREEPOPSLUA_HTTP_USERAGENT",useragent);
	my_putenv("FREEPOPSLUA_HTTP_PROXY",proxy);
	my_putenv("FREEPOPSLUA_HTTP_PROXYAUTH",proxyauth);
	my_putenv("FREEPOPSLUA_FORCE_PROXY_AUTH_TYPE",fpat);
	
	if (conffile != NULL)
		my_putenv("FREEPOPSLUA_CONFFILE",conffile);

	stats_activate(active_stats);
	
/*** INTERPRETER MODE ***/
	if (script != NULL){
		exit(execute(script,execute_stdout));
	}
	
/*** INITIALIZATION UNIX ***/
#if !(defined(WIN32) && !defined(CYGWIN)) && ! defined(BEOS)
	if(daemonize)
		daemonize_process();

	if ( ! no_pid ) 
		create_pid_file(PIDFILE);
	
	set_signals();
#endif

/*** INITIALIZATION WIN32 ***/
#if defined(WIN32) && !defined(CYGWIN)
	if(tray_icon)
		create_tray_icon(hInstance,hPrevInstance,lpszCmdLine,nCmdShow);
#endif

/*** GO! ***/
#if !(defined(WIN32) && !defined(CYGWIN)) && ! defined(BEOS)
	popserver_start(&freepops_functions, address, port, threads_num,
		loose_rights,uid,gid);
#else
	popserver_start(&freepops_functions, address, port, threads_num,
		NULL,0,0);
#endif
	
/*** EXIT ***/
#if defined(WIN32) && !defined(CYGWIN)
	win32_exit();
#endif

	return EXIT_SUCCESS;
}