summaryrefslogtreecommitdiff
path: root/source3/wrepld/server.c
blob: 25de680982ce8722775c0feeeb70b68a53ca8dfb (plain)
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
/* 
   Unix SMB/CIFS implementation.
   Main SMB server routines
   Copyright (C) Jean François Micouleau      1998-2002.
   
   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.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"
#include "wins_repl.h"

extern pstring user_socket_options;

extern WINS_OWNER *global_wins_table;
extern int partner_count;

extern fd_set *listen_set;
extern int listen_number;
extern int *sock_array;

extern TALLOC_CTX *mem_ctx;

int wins_port = 42;

/****************************************************************************
  when exiting, take the whole family
****************************************************************************/
static void *dflt_sig(void)
{
	exit_server("caught signal");
	return NULL;
}

/****************************************************************************
  reload the services file
  **************************************************************************/
BOOL reload_services(BOOL test)
{
	BOOL ret;
	
	if (lp_loaded()) {
		pstring fname;
		pstrcpy(fname,lp_configfile());
		if (file_exist(fname,NULL) && !strcsequal(fname,dyn_CONFIGFILE)) {
			pstrcpy(dyn_CONFIGFILE,fname);
			test = False;
		}
	}

	reopen_logs();

	if (test && !lp_file_list_changed())
		return(True);

	ret = lp_load(dyn_CONFIGFILE,False,False,True);


	/* perhaps the config filename is now set */
	if (!test)
		reload_services(True);

	reopen_logs();

	load_interfaces();

	return(ret);
}

/****************************************************************************
 Catch a sighup.
****************************************************************************/

VOLATILE sig_atomic_t reload_after_sighup = False;

static void sig_hup(int sig)
{
	BlockSignals(True,SIGHUP);
	DEBUG(0,("Got SIGHUP\n"));

	sys_select_signal();
	reload_after_sighup = True;
	BlockSignals(False,SIGHUP);
}

#if DUMP_CORE
/*******************************************************************
prepare to dump a core file - carefully!
********************************************************************/
static BOOL dump_core(void)
{
	char *p;
	pstring dname;
	pstrcpy(dname,lp_logfile());
	if ((p=strrchr_m(dname,'/'))) *p=0;
	pstrcat(dname,"/corefiles");
	mkdir(dname,0700);
	sys_chown(dname,getuid(),getgid());
	chmod(dname,0700);
	if (chdir(dname)) return(False);
	umask(~(0700));

#ifdef HAVE_GETRLIMIT
#ifdef RLIMIT_CORE
	{
		struct rlimit rlp;
		getrlimit(RLIMIT_CORE, &rlp);
		rlp.rlim_cur = MAX(4*1024*1024,rlp.rlim_cur);
		setrlimit(RLIMIT_CORE, &rlp);
		getrlimit(RLIMIT_CORE, &rlp);
		DEBUG(3,("Core limits now %d %d\n",
			 (int)rlp.rlim_cur,(int)rlp.rlim_max));
	}
#endif
#endif


	DEBUG(0,("Dumping core in %s\n",dname));
	abort();
	return(True);
}
#endif

/****************************************************************************
exit the server
****************************************************************************/
void exit_server(const char *reason)
{
	static int firsttime=1;

	if (!firsttime)
		exit(0);
	firsttime = 0;

	DEBUG(2,("Closing connections\n"));

	if (!reason) {   
		int oldlevel = DEBUGLEVEL;
		DEBUGLEVEL = 10;
		DEBUGLEVEL = oldlevel;
		DEBUG(0,("===============================================================\n"));
#if DUMP_CORE
		if (dump_core()) return;
#endif
	}

	DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
	exit(0);
}

/****************************************************************************
  Create an fd_set containing all the sockets in the subnet structures,
  plus the broadcast sockets.
***************************************************************************/

static BOOL create_listen_fdset(void)
{
	int i;
	int num_interfaces = iface_count();
	int s;

	listen_set = (fd_set *)malloc(sizeof(fd_set));
	if(listen_set == NULL) {
		DEBUG(0,("create_listen_fdset: malloc fail !\n"));
		return True;
	}

#ifdef HAVE_ATEXIT
	{
		static int atexit_set;
		if(atexit_set == 0) {
			atexit_set=1;
		}
	}
#endif

	FD_ZERO(listen_set);

	if(lp_interfaces() && lp_bind_interfaces_only()) {
		/* We have been given an interfaces line, and been 
		   told to only bind to those interfaces. Create a
		   socket per interface and bind to only these.
		*/
		
		if(num_interfaces > FD_SETSIZE) {
			DEBUG(0,("create_listen_fdset: Too many interfaces specified to bind to. Number was %d max can be %d\n", num_interfaces, FD_SETSIZE));
			return False;
		}

		/* Now open a listen socket for each of the interfaces. */
		for(i = 0; i < num_interfaces; i++) {
			struct in_addr *ifip = iface_n_ip(i);
			
			if(ifip == NULL) {
				DEBUG(0,("create_listen_fdset: interface %d has NULL IP address !\n", i));
				continue;
			}
			s = open_socket_in(SOCK_STREAM, wins_port, 0, ifip->s_addr, True);
			if(s == -1)
				return False;

			/* ready to listen */
			set_socket_options(s,"SO_KEEPALIVE"); 
			set_socket_options(s,user_socket_options);
      
			if (listen(s, 5) == -1) {
				DEBUG(5,("listen: %s\n",strerror(errno)));
				close(s);
				return False;
			}
			add_fd_to_sock_array(s);
			FD_SET(s, listen_set);
		}
	} else {
		/* Just bind to 0.0.0.0 - accept connections from anywhere. */
		num_interfaces = 1;
		
		/* open an incoming socket */
		s = open_socket_in(SOCK_STREAM, wins_port, 0, interpret_addr(lp_socket_address()),True);
		if (s == -1)
			return(False);
		
		/* ready to listen */
		set_socket_options(s,"SO_KEEPALIVE"); 
		set_socket_options(s,user_socket_options);

		if (listen(s, 5) == -1) {
			DEBUG(0,("create_listen_fdset: listen: %s\n", strerror(errno)));
			close(s);
			return False;
		}
		
		add_fd_to_sock_array(s);
		FD_SET(s, listen_set);
	} 

	return True;
}

/*******************************************************************
  read a packet from a socket and parse it, returning a packet ready
  to be used or put on the queue. This assumes a UDP socket
  ******************************************************************/
static struct wins_packet_struct *read_wins_packet(int fd, int timeout)
{
	struct wins_packet_struct *p;
	GENERIC_PACKET *q;
	struct BUFFER inbuf;
	ssize_t len=0;
	size_t total=0;  
	ssize_t ret;
	BOOL ok = False;

	inbuf.buffer=NULL;
	inbuf.length=0;
	inbuf.offset=0;

	if(!grow_buffer(&inbuf, 4))
		return NULL;

	ok = (read(fd, inbuf.buffer,4) == 4);
	if (!ok)
		return NULL;
	len = smb_len(inbuf.buffer);

	if (len<=0)
		return NULL;

	if(!grow_buffer(&inbuf, len))
		return NULL;
		
	while (total < len) {
		ret = read(fd, inbuf.buffer + total + 4, len - total);
		if (ret == 0) {
			DEBUG(10,("read_socket_data: recv of %d returned 0. Error = %s\n", (int)(len - total), strerror(errno) ));
			return NULL;
		}
		if (ret == -1) {
			DEBUG(0,("read_socket_data: recv failure for %d. Error = %s\n", (int)(len - total), strerror(errno) ));
			return NULL;
		}
		total += ret;
	}

	q = (GENERIC_PACKET *)talloc(mem_ctx, sizeof(GENERIC_PACKET));
	p = (struct wins_packet_struct *)talloc(mem_ctx, sizeof(*p));
	if (q==NULL || p==NULL)
		return NULL;

	decode_generic_packet(&inbuf, q);

	q->fd=fd;
	
	p->next = NULL;
	p->prev = NULL;
	p->stop_packet = False;
	p->timestamp = time(NULL);
	p->fd = fd;
	p->packet=q;
	
	return p;
}

static struct wins_packet_struct *packet_queue = NULL;

/*******************************************************************
  Queue a packet into a packet queue
******************************************************************/
static void queue_packet(struct wins_packet_struct *packet)
{
	struct wins_packet_struct *p;

	if (!packet_queue) {
		packet->prev = NULL;
		packet->next = NULL;
		packet_queue = packet;
		return;
	}
  
	/* find the bottom */
	for (p=packet_queue;p->next;p=p->next) 
		;

	p->next = packet;
	packet->next = NULL;
	packet->prev = p;
}

/****************************************************************************
  Listens for NMB or DGRAM packets, and queues them.
  return True if the socket is dead
***************************************************************************/
static BOOL listen_for_wins_packets(void)
{
	int num_interfaces = iface_count();
	fd_set fds;
	int i, num, s, new_s;
	struct timeval timeout;

	if(listen_set == NULL) {
		if(!create_listen_fdset()) {
			DEBUG(0,("listen_for_packets: Fatal error. unable to create listen set. Exiting.\n"));
			return True;
		}
	}

	memcpy((char *)&fds, (char *)listen_set, sizeof(fd_set));

	timeout.tv_sec = NMBD_SELECT_LOOP;
	timeout.tv_usec = 0;

	/* Prepare for the select - allow certain signals. */

	BlockSignals(False, SIGTERM);

	num = sys_select(FD_SETSIZE, &fds, NULL, NULL, &timeout);

	/* We can only take signals when we are in the select - block them again here. */

	BlockSignals(True, SIGTERM);

	if(num == -1)
		return False;

	for (; num > 0; num--) {
		s = -1;
		/* check the sockets we are only listening on, waiting to accept */		
		for (i=0; i<num_interfaces; i++) {
			struct sockaddr addr;
			socklen_t in_addrlen = sizeof(addr);
		
			if(FD_ISSET(sock_array[i], &fds)) {
				s = sock_array[i];
				/* Clear this so we don't look at it again. */
				FD_CLR(sock_array[i], &fds);

				/* accept and add the new socket to the listen set */
				new_s=accept(s, &addr, &in_addrlen);

				if (new_s < 0)
					continue;
	
				DEBUG(5,("listen_for_wins_packets: new connection, old: %d, new : %d\n", s, new_s));
				
				set_socket_options(new_s, "SO_KEEPALIVE");
				set_socket_options(new_s, user_socket_options);
				FD_SET(new_s, listen_set);
				add_fd_to_sock_array(new_s);
			}
		}

		/*
		 * check for the sockets we are waiting data from
		 * either client sending datas
		 * or reply to our requests
		 */
		for (i=num_interfaces; i<listen_number; i++) {
			if(FD_ISSET(sock_array[i], &fds)) {
				struct wins_packet_struct *packet = read_wins_packet(sock_array[i], timeout.tv_sec);
				if (packet) {
					packet->fd = sock_array[i];
					queue_packet(packet);
				}
				DEBUG(2,("listen_for_wins_packets: some data on fd %d\n", sock_array[i]));
				FD_CLR(sock_array[i], &fds);
				break;
			}
	
		}

	}

	return False;
}


/*******************************************************************
  Run elements off the packet queue till its empty
******************************************************************/

static void run_wins_packet_queue(void)
{
	struct wins_packet_struct *p;

	while ((p = packet_queue)) {
		packet_queue = p->next;
		if (packet_queue)
			packet_queue->prev = NULL;
		p->next = p->prev = NULL;

		construct_reply(p);

		/* if it was a stop assoc, close the connection */
		if (p->stop_packet) {
			FD_CLR(p->fd, listen_set);
			remove_fd_from_sock_array(p->fd);
			close(p->fd);
		}
	}
} 

/**************************************************************************** **
 The main select loop.
 **************************************************************************** */
static void process(void)
{

	while( True ) {
		time_t t = time(NULL);

		/* check for internal messages */
		message_dispatch();

		if(listen_for_wins_packets())
			return;

		run_wins_packet_queue();

		run_pull_replication(t);
		
		run_push_replication(t);
		
		/*
		 * Reload the services file if we got a sighup.
		 */

		if(reload_after_sighup) {
			reload_services( True );
			reopen_logs();
			reload_after_sighup = False;
		}

		/* free temp memory */
		talloc_destroy_pool(mem_ctx);

		/* free up temp memory */
		lp_talloc_free();
	}
} /* process */

/****************************************************************************
  main program
****************************************************************************/
 int main(int argc,char *argv[])
{
	/* shall I run as a daemon */
	static BOOL is_daemon = False;
	static BOOL interactive = False;
	static BOOL Fork = True;
	static BOOL log_stdout = False;
	struct poptOption long_options[] = {
		POPT_AUTOHELP
		{ "daemon", 'D', POPT_ARG_VAL, &is_daemon, True, "Become a daemon (default)" },
		{ "foreground", 'F', POPT_ARG_VAL, &Fork, False, "Run daemon in foreground (for daemontools, etc)" },
		{ "stdout", 'S', POPT_ARG_VAL, &log_stdout, True, "Log to stdout" },
		{ "interactive", 'i', POPT_ARG_NONE, NULL, 'i', "Run interactive (not a daemon)" },
		{ "port", 'p', POPT_ARG_INT, &wins_port, 'p', "Listen on the specified port" },
		POPT_COMMON_SAMBA
		POPT_TABLEEND
	};
	int opt;
	poptContext pc;

#ifdef HAVE_SET_AUTH_PARAMETERS
	set_auth_parameters(argc,argv);
#endif

	pc = poptGetContext("wrepld", argc, (const char **)argv, long_options, 
						POPT_CONTEXT_KEEP_FIRST);

	while ((opt = poptGetNextOpt(pc)) != -1) {
		switch (opt)  {
		case 'i':
			interactive = True;
			Fork = False;
			log_stdout = True;
			break;
		}
	}


	if (log_stdout && Fork) {
		d_printf("Can't log to stdout (-S) unless daemon is in foreground (-F) or interactive (-i)\n");
		poptPrintUsage(pc, stderr, 0);
		exit(1);
	}

#ifdef HAVE_SETLUID
	/* needed for SecureWare on SCO */
	setluid(0);
#endif

	sec_init();

	load_case_tables();

	set_remote_machine_name("wrepld", False);

	setup_logging(argv[0],log_stdout);

	/* we want to re-seed early to prevent time delays causing
           client problems at a later date. (tridge) */
	generate_random_buffer(NULL, 0, False);

	/* make absolutely sure we run as root - to handle cases where people
	   are crazy enough to have it setuid */

	gain_root_privilege();
	gain_root_group_privilege();

	fault_setup((void (*)(void *))exit_server);
	CatchSignal(SIGTERM , SIGNAL_CAST dflt_sig);

	/* we are never interested in SIGPIPE */
	BlockSignals(True,SIGPIPE);

#if defined(SIGFPE)
	/* we are never interested in SIGFPE */
	BlockSignals(True,SIGFPE);
#endif

#if defined(SIGUSR2)
	/* We are no longer interested in USR2 */
	BlockSignals(True,SIGUSR2);
#endif

	/* POSIX demands that signals are inherited. If the invoking process has
	 * these signals masked, we will have problems, as we won't recieve them. */
	BlockSignals(False, SIGHUP);
	BlockSignals(False, SIGUSR1);

	/* we want total control over the permissions on created files,
	   so set our umask to 0 */
	umask(0);

	reopen_logs();

	DEBUG(1,( "wrepld version %s started.\n", SAMBA_VERSION_STRING));
	DEBUGADD(1,( "Copyright Andrew Tridgell and the Samba Team 1992-2002\n"));

	DEBUG(2,("uid=%d gid=%d euid=%d egid=%d\n",
		 (int)getuid(),(int)getgid(),(int)geteuid(),(int)getegid()));

	if (sizeof(uint16) < 2 || sizeof(uint32) < 4) {
		DEBUG(0,("ERROR: Samba is not configured correctly for the word size on your machine\n"));
		exit(1);
	}

	/*
	 * Do this before reload_services.
	 */

	if (!reload_services(False))
		return(-1);	

	if (!init_names())
		return -1;
	
#ifdef WITH_PROFILE
	if (!profile_setup(False)) {
		DEBUG(0,("ERROR: failed to setup profiling\n"));
		return -1;
	}
#endif

	CatchSignal(SIGHUP,SIGNAL_CAST sig_hup);
	
	DEBUG(3,( "loaded services\n"));

	if (!is_daemon && !is_a_socket(0)) {
		DEBUG(0,("standard input is not a socket, assuming -D option\n"));
		is_daemon = True;
	}

	if (is_daemon && !interactive) {
		DEBUG( 3, ( "Becoming a daemon.\n" ) );
		become_daemon(Fork);
	}

#if HAVE_SETPGID
	/*
	 * If we're interactive we want to set our own process group for
	 * signal management.
	 */
	if (interactive)
		setpgid( (pid_t)0, (pid_t)0);
#endif

	if (!directory_exist(lp_lockdir(), NULL)) {
		mkdir(lp_lockdir(), 0755);
	}

	if (is_daemon) {
		pidfile_create("wrepld");
	}

	if (!message_init()) {
		exit(1);
	}

	/* Initialise the memory context */
	mem_ctx=talloc_init("wins repl talloc ctx");

	/* initialise the global partners table */
	partner_count=init_wins_partner_table();

	/* We can only take signals in the select. */
	BlockSignals( True, SIGTERM );

	process();

	poptFreeContext(pc);
	exit_server("normal exit");
	return(0);
}