2 Unix SMB/CIFS implementation.
3 Main SMB server routines
4 Copyright (C) Jean François Micouleau 1998-2002.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "wins_repl.h"
24 extern fstring global_myworkgroup;
25 extern pstring global_myname;
27 extern pstring user_socket_options;
29 extern fstring remote_machine;
30 extern WINS_OWNER *global_wins_table;
31 extern int partner_count;
33 extern fd_set *listen_set;
34 extern int listen_number;
35 extern int *sock_array;
37 extern TALLOC_CTX *mem_ctx;
41 /****************************************************************************
42 when exiting, take the whole family
43 ****************************************************************************/
44 static void *dflt_sig(void)
46 exit_server("caught signal");
50 /****************************************************************************
51 reload the services file
52 **************************************************************************/
53 BOOL reload_services(BOOL test)
59 pstrcpy(fname,lp_configfile());
60 if (file_exist(fname,NULL) && !strcsequal(fname,dyn_CONFIGFILE)) {
61 pstrcpy(dyn_CONFIGFILE,fname);
68 if (test && !lp_file_list_changed())
71 ret = lp_load(dyn_CONFIGFILE,False,False,True);
74 /* perhaps the config filename is now set */
76 reload_services(True);
85 /****************************************************************************
87 ****************************************************************************/
89 VOLATILE sig_atomic_t reload_after_sighup = False;
91 static void sig_hup(int sig)
93 BlockSignals(True,SIGHUP);
94 DEBUG(0,("Got SIGHUP\n"));
97 reload_after_sighup = True;
98 BlockSignals(False,SIGHUP);
102 /*******************************************************************
103 prepare to dump a core file - carefully!
104 ********************************************************************/
105 static BOOL dump_core(void)
109 pstrcpy(dname,lp_logfile());
110 if ((p=strrchr_m(dname,'/'))) *p=0;
111 pstrcat(dname,"/corefiles");
113 sys_chown(dname,getuid(),getgid());
115 if (chdir(dname)) return(False);
118 #ifdef HAVE_GETRLIMIT
122 getrlimit(RLIMIT_CORE, &rlp);
123 rlp.rlim_cur = MAX(4*1024*1024,rlp.rlim_cur);
124 setrlimit(RLIMIT_CORE, &rlp);
125 getrlimit(RLIMIT_CORE, &rlp);
126 DEBUG(3,("Core limits now %d %d\n",
127 (int)rlp.rlim_cur,(int)rlp.rlim_max));
133 DEBUG(0,("Dumping core in %s\n",dname));
139 /****************************************************************************
141 ****************************************************************************/
142 void exit_server(char *reason)
144 static int firsttime=1;
150 DEBUG(2,("Closing connections\n"));
153 int oldlevel = DEBUGLEVEL;
155 DEBUGLEVEL = oldlevel;
156 DEBUG(0,("===============================================================\n"));
158 if (dump_core()) return;
162 DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
166 /****************************************************************************
167 initialise connect, service and file structs
168 ****************************************************************************/
169 static void init_structs(void )
172 * Set the machine NETBIOS name if not already
173 * set from the config file.
176 if (!*global_myname) {
178 fstrcpy( global_myname, myhostname() );
179 p = strchr_m( global_myname, '.' );
184 strupper( global_myname );
187 /****************************************************************************
189 ****************************************************************************/
190 static void usage(char *pname)
193 d_printf("Usage: %s [-DaioPh?V] [-d debuglevel] [-l log basename] [-p port]\n", pname);
194 d_printf(" [-O socket options] [-s services file]\n");
195 d_printf("\t-D Become a daemon (default)\n");
196 d_printf("\t-a Append to log file (default)\n");
197 d_printf("\t-i Run interactive (not a daemon)\n" );
198 d_printf("\t-o Overwrite log file, don't append\n");
199 d_printf("\t-h Print usage\n");
200 d_printf("\t-? Print usage\n");
201 d_printf("\t-V Print version\n");
202 d_printf("\t-d debuglevel Set the debuglevel\n");
203 d_printf("\t-l log basename. Basename for log/debug files\n");
204 d_printf("\t-p port Listen on the specified port\n");
205 d_printf("\t-O socket options Socket options\n");
206 d_printf("\t-s services file. Filename of services file\n");
210 /****************************************************************************
211 Create an fd_set containing all the sockets in the subnet structures,
212 plus the broadcast sockets.
213 ***************************************************************************/
215 static BOOL create_listen_fdset(void)
218 int num_interfaces = iface_count();
221 listen_set = (fd_set *)malloc(sizeof(fd_set));
222 if(listen_set == NULL) {
223 DEBUG(0,("create_listen_fdset: malloc fail !\n"));
229 static int atexit_set;
230 if(atexit_set == 0) {
238 if(lp_interfaces() && lp_bind_interfaces_only()) {
239 /* We have been given an interfaces line, and been
240 told to only bind to those interfaces. Create a
241 socket per interface and bind to only these.
244 if(num_interfaces > FD_SETSIZE) {
245 DEBUG(0,("create_listen_fdset: Too many interfaces specified to bind to. Number was %d max can be %d\n", num_interfaces, FD_SETSIZE));
249 /* Now open a listen socket for each of the interfaces. */
250 for(i = 0; i < num_interfaces; i++) {
251 struct in_addr *ifip = iface_n_ip(i);
254 DEBUG(0,("create_listen_fdset: interface %d has NULL IP address !\n", i));
257 s = open_socket_in(SOCK_STREAM, wins_port, 0, ifip->s_addr, True);
261 /* ready to listen */
262 set_socket_options(s,"SO_KEEPALIVE");
263 set_socket_options(s,user_socket_options);
265 if (listen(s, 5) == -1) {
266 DEBUG(0,("listen: %s\n",strerror(errno)));
270 add_fd_to_sock_array(s);
271 FD_SET(s, listen_set);
274 /* Just bind to 0.0.0.0 - accept connections from anywhere. */
277 /* open an incoming socket */
278 s = open_socket_in(SOCK_STREAM, wins_port, 0, interpret_addr(lp_socket_address()),True);
282 /* ready to listen */
283 set_socket_options(s,"SO_KEEPALIVE");
284 set_socket_options(s,user_socket_options);
286 if (listen(s, 5) == -1) {
287 DEBUG(0,("create_listen_fdset: listen: %s\n", strerror(errno)));
292 add_fd_to_sock_array(s);
293 FD_SET(s, listen_set);
299 /*******************************************************************
300 read a packet from a socket and parse it, returning a packet ready
301 to be used or put on the queue. This assumes a UDP socket
302 ******************************************************************/
303 static struct wins_packet_struct *read_wins_packet(int fd, int timeout)
305 struct wins_packet_struct *p;
309 if (!receive_smb(fd, buf, timeout))
312 q = (GENERIC_PACKET *)talloc(mem_ctx, sizeof(GENERIC_PACKET));
313 p = (struct wins_packet_struct *)talloc(mem_ctx, sizeof(*p));
314 if (q==NULL || p==NULL)
317 decode_generic_packet(buf, q);
323 p->stop_packet = False;
324 p->timestamp = time(NULL);
331 static struct wins_packet_struct *packet_queue = NULL;
333 /*******************************************************************
334 Queue a packet into a packet queue
335 ******************************************************************/
336 static void queue_packet(struct wins_packet_struct *packet)
338 struct wins_packet_struct *p;
343 packet_queue = packet;
347 /* find the bottom */
348 for (p=packet_queue;p->next;p=p->next)
356 /****************************************************************************
357 Listens for NMB or DGRAM packets, and queues them.
358 return True if the socket is dead
359 ***************************************************************************/
360 static BOOL listen_for_wins_packets(void)
362 int num_interfaces = iface_count();
364 int i, num, s, new_s;
365 struct timeval timeout;
367 if(listen_set == NULL) {
368 if(!create_listen_fdset()) {
369 DEBUG(0,("listen_for_packets: Fatal error. unable to create listen set. Exiting.\n"));
374 memcpy((char *)&fds, (char *)listen_set, sizeof(fd_set));
376 timeout.tv_sec = NMBD_SELECT_LOOP;
379 /* Prepare for the select - allow certain signals. */
381 BlockSignals(False, SIGTERM);
383 num = sys_select(FD_SETSIZE, &fds, &timeout);
385 /* We can only take signals when we are in the select - block them again here. */
387 BlockSignals(True, SIGTERM);
392 for (; num > 0; num--) {
394 /* check the sockets we are only listening on, waiting to accept */
395 for (i=0; i<num_interfaces; i++) {
396 struct sockaddr addr;
397 socklen_t in_addrlen = sizeof(addr);
399 if(FD_ISSET(sock_array[i], &fds)) {
401 /* Clear this so we don't look at it again. */
402 FD_CLR(sock_array[i], &fds);
404 /* accept and add the new socket to the listen set */
405 new_s=accept(s, &addr, &in_addrlen);
407 DEBUG(5,("listen_for_wins_packets: new connection, old: %d, new : %d\n", s, new_s));
409 set_socket_options(new_s, "SO_KEEPALIVE");
410 set_socket_options(new_s, user_socket_options);
411 FD_SET(new_s, listen_set);
412 add_fd_to_sock_array(new_s);
417 * check for the sockets we are waiting data from
418 * either client sending datas
419 * or reply to our requests
421 for (i=num_interfaces; i<listen_number; i++) {
422 if(FD_ISSET(sock_array[i], &fds)) {
423 struct wins_packet_struct *packet = read_wins_packet(sock_array[i], timeout.tv_sec);
425 packet->fd = sock_array[i];
426 queue_packet(packet);
428 DEBUG(2,("listen_for_wins_packets: some data on fd %d\n", sock_array[i]));
429 FD_CLR(sock_array[i], &fds);
441 /*******************************************************************
442 Run elements off the packet queue till its empty
443 ******************************************************************/
445 static void run_wins_packet_queue(void)
447 struct wins_packet_struct *p;
449 while ((p = packet_queue)) {
450 packet_queue = p->next;
452 packet_queue->prev = NULL;
453 p->next = p->prev = NULL;
457 /* if it was a stop assoc, close the connection */
458 if (p->stop_packet) {
459 FD_CLR(p->fd, listen_set);
460 remove_fd_from_sock_array(p->fd);
466 /**************************************************************************** **
467 The main select loop.
468 **************************************************************************** */
469 static void process(void)
473 time_t t = time(NULL);
475 /* check for internal messages */
478 if(listen_for_wins_packets())
481 run_wins_packet_queue();
483 run_pull_replication(t);
485 run_push_replication(t);
488 * Reload the services file if we got a sighup.
491 if(reload_after_sighup) {
492 reload_services( True );
494 reload_after_sighup = False;
497 /* free temp memory */
498 talloc_destroy_pool(mem_ctx);
500 /* free up temp memory */
505 /****************************************************************************
507 ****************************************************************************/
508 int main(int argc,char *argv[])
510 extern BOOL append_log;
512 /* shall I run as a daemon */
513 BOOL is_daemon = False;
514 BOOL interactive = False;
515 BOOL specified_logfile = False;
519 #ifdef HAVE_SET_AUTH_PARAMETERS
520 set_auth_parameters(argc,argv);
523 /* this is for people who can't start the program correctly */
524 while (argc > 1 && (*argv[1] != '-')) {
529 while ( EOF != (opt = getopt(argc, argv, "O:l:s:d:Dp:h?Vaiof:")) )
532 pstrcpy(user_socket_options,optarg);
536 pstrcpy(dyn_CONFIGFILE,optarg);
540 specified_logfile = True;
541 slprintf(logfile, sizeof(logfile)-1, "%s/log.wrepld", optarg);
542 lp_set_logfile(logfile);
565 DEBUGLEVEL = atoi(optarg);
569 wins_port = atoi(optarg);
579 d_printf("Version %s\n",VERSION);
583 DEBUG(0,("Incorrect program usage - are you sure the command line is correct?\n"));
589 /* needed for SecureWare on SCO */
599 if(!specified_logfile) {
600 slprintf(logfile, sizeof(logfile)-1, "%s/log.wrepld",
602 lp_set_logfile(logfile);
605 pstrcpy(remote_machine, "wrepld");
607 setup_logging(argv[0],interactive);
609 /* we want to re-seed early to prevent time delays causing
610 client problems at a later date. (tridge) */
611 generate_random_buffer(NULL, 0, False);
613 /* make absolutely sure we run as root - to handle cases where people
614 are crazy enough to have it setuid */
616 gain_root_privilege();
617 gain_root_group_privilege();
619 fault_setup((void (*)(void *))exit_server);
620 CatchSignal(SIGTERM , SIGNAL_CAST dflt_sig);
622 /* we are never interested in SIGPIPE */
623 BlockSignals(True,SIGPIPE);
626 /* we are never interested in SIGFPE */
627 BlockSignals(True,SIGFPE);
631 /* We are no longer interested in USR2 */
632 BlockSignals(True,SIGUSR2);
635 /* POSIX demands that signals are inherited. If the invoking process has
636 * these signals masked, we will have problems, as we won't recieve them. */
637 BlockSignals(False, SIGHUP);
638 BlockSignals(False, SIGUSR1);
640 /* we want total control over the permissions on created files,
641 so set our umask to 0 */
646 DEBUG(1,( "wrepld version %s started.\n", VERSION));
647 DEBUGADD(1,( "Copyright Andrew Tridgell and the Samba Team 1992-2002\n"));
649 DEBUG(2,("uid=%d gid=%d euid=%d egid=%d\n",
650 (int)getuid(),(int)getgid(),(int)geteuid(),(int)getegid()));
652 if (sizeof(uint16) < 2 || sizeof(uint32) < 4) {
653 DEBUG(0,("ERROR: Samba is not configured correctly for the word size on your machine\n"));
658 * Do this before reload_services.
661 if (!reload_services(False))
667 if (!profile_setup(False)) {
668 DEBUG(0,("ERROR: failed to setup profiling\n"));
673 fstrcpy(global_myworkgroup, lp_workgroup());
675 CatchSignal(SIGHUP,SIGNAL_CAST sig_hup);
677 DEBUG(3,( "loaded services\n"));
679 if (!is_daemon && !is_a_socket(0)) {
680 DEBUG(0,("standard input is not a socket, assuming -D option\n"));
684 if (is_daemon && !interactive) {
685 DEBUG( 3, ( "Becoming a daemon.\n" ) );
691 * If we're interactive we want to set our own process group for
695 setpgid( (pid_t)0, (pid_t)0);
698 if (!directory_exist(lp_lockdir(), NULL)) {
699 mkdir(lp_lockdir(), 0755);
703 pidfile_create("wrepld");
706 if (!message_init()) {
710 /* Initialise the memory context */
711 mem_ctx=talloc_init_named("wins repl talloc ctx");
713 /* initialise the global partners table */
714 partner_count=init_wins_partner_table();
716 /* We can only take signals in the select. */
717 BlockSignals( True, SIGTERM );
721 exit_server("normal exit");