Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / samba / source / smbd / server.c
blob8e0adcc03ef1202d0ae36d2f5a24da2c3cdf420f
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 Main SMB server routines
5 Copyright (C) Andrew Tridgell 1992-1998
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "includes.h"
23 #include "trans2.h"
25 pstring servicesf = CONFIGFILE;
26 extern pstring debugf;
27 extern fstring global_myworkgroup;
28 extern pstring global_myname;
30 int am_parent = 1;
32 /* the last message the was processed */
33 int last_message = -1;
35 /* a useful macro to debug the last message processed */
36 #define LAST_MESSAGE() smb_fn_name(last_message)
38 extern int DEBUGLEVEL;
40 extern pstring user_socket_options;
42 #ifdef WITH_DFS
43 extern int dcelogin_atmost_once;
44 #endif /* WITH_DFS */
47 extern fstring remote_machine;
48 extern pstring OriginalDir;
50 /****************************************************************************
51 when exiting, take the whole family
52 ****************************************************************************/
53 static void *dflt_sig(void)
55 exit_server("caught signal");
56 return NULL;
59 /****************************************************************************
60 Send a SIGTERM to our process group.
61 *****************************************************************************/
62 static void killkids(void)
64 if(am_parent) kill(0,SIGTERM);
68 /****************************************************************************
69 open the socket communication
70 ****************************************************************************/
71 static BOOL open_sockets_inetd(void)
73 extern int Client;
75 /* Started from inetd. fd 0 is the socket. */
76 /* We will abort gracefully when the client or remote system
77 goes away */
78 Client = dup(0);
80 /* close our standard file descriptors */
81 close_low_fds();
83 set_socket_options(Client,"SO_KEEPALIVE");
84 set_socket_options(Client,user_socket_options);
86 return True;
90 /****************************************************************************
91 open the socket communication
92 ****************************************************************************/
93 static BOOL open_sockets(BOOL is_daemon,int port)
95 extern int Client;
96 int num_interfaces = iface_count();
97 int fd_listenset[FD_SETSIZE];
98 fd_set listen_set;
99 int s;
100 int i;
102 if (!is_daemon) {
103 return open_sockets_inetd();
107 #ifdef HAVE_ATEXIT
109 static int atexit_set;
110 if(atexit_set == 0) {
111 atexit_set=1;
112 atexit(killkids);
115 #endif
117 /* Stop zombies */
118 CatchChild();
121 FD_ZERO(&listen_set);
123 if(lp_interfaces() && lp_bind_interfaces_only()) {
124 /* We have been given an interfaces line, and been
125 told to only bind to those interfaces. Create a
126 socket per interface and bind to only these.
129 if(num_interfaces > FD_SETSIZE) {
130 DEBUG(0,("open_sockets: Too many interfaces specified to bind to. Number was %d \
131 max can be %d\n",
132 num_interfaces, FD_SETSIZE));
133 return False;
136 /* Now open a listen socket for each of the
137 interfaces. */
138 for(i = 0; i < num_interfaces; i++) {
139 struct in_addr *ifip = iface_n_ip(i);
141 if(ifip == NULL) {
142 DEBUG(0,("open_sockets: interface %d has NULL IP address !\n", i));
143 continue;
145 s = fd_listenset[i] = open_socket_in(SOCK_STREAM, port, 0, ifip->s_addr, True);
146 if(s == -1)
147 return False;
148 /* ready to listen */
149 if (listen(s, 5) == -1) {
150 DEBUG(0,("listen: %s\n",strerror(errno)));
151 close(s);
152 return False;
154 FD_SET(s,&listen_set);
156 } else {
157 /* Just bind to 0.0.0.0 - accept connections
158 from anywhere. */
159 num_interfaces = 1;
161 /* open an incoming socket */
162 s = open_socket_in(SOCK_STREAM, port, 0,
163 interpret_addr(lp_socket_address()),True);
164 if (s == -1)
165 return(False);
167 /* ready to listen */
168 if (listen(s, 5) == -1) {
169 DEBUG(0,("open_sockets: listen: %s\n",
170 strerror(errno)));
171 close(s);
172 return False;
175 fd_listenset[0] = s;
176 FD_SET(s,&listen_set);
179 /* now accept incoming connections - forking a new process
180 for each incoming connection */
181 DEBUG(2,("waiting for a connection\n"));
182 while (1) {
183 fd_set lfds;
184 int num;
186 memcpy((char *)&lfds, (char *)&listen_set,
187 sizeof(listen_set));
189 num = sys_select(FD_SETSIZE,&lfds,NULL);
191 if (num == -1 && errno == EINTR)
192 continue;
194 /* check if we need to reload services */
195 check_reload(time(NULL));
197 /* Find the sockets that are read-ready -
198 accept on these. */
199 for( ; num > 0; num--) {
200 struct sockaddr addr;
201 int in_addrlen = sizeof(addr);
203 s = -1;
204 for(i = 0; i < num_interfaces; i++) {
205 if(FD_ISSET(fd_listenset[i],&lfds)) {
206 s = fd_listenset[i];
207 /* Clear this so we don't look
208 at it again. */
209 FD_CLR(fd_listenset[i],&lfds);
210 break;
214 Client = accept(s,&addr,&in_addrlen);
216 if (Client == -1 && errno == EINTR)
217 continue;
219 if (Client == -1) {
220 DEBUG(0,("open_sockets: accept: %s\n",
221 strerror(errno)));
222 continue;
225 if (Client != -1 && fork()==0) {
226 /* Child code ... */
228 /* close the listening socket(s) */
229 for(i = 0; i < num_interfaces; i++)
230 close(fd_listenset[i]);
232 /* close our standard file
233 descriptors */
234 close_low_fds();
235 am_parent = 0;
237 set_socket_options(Client,"SO_KEEPALIVE");
238 set_socket_options(Client,user_socket_options);
240 /* Reset global variables in util.c so
241 that client substitutions will be
242 done correctly in the process. */
243 reset_globals_after_fork();
246 * Ensure this child has kernel oplock
247 * capabilities, but not it's children.
249 set_process_capability(KERNEL_OPLOCK_CAPABILITY, True);
250 set_inherited_process_capability(KERNEL_OPLOCK_CAPABILITY, False);
252 return True;
254 /* The parent doesn't need this socket */
255 close(Client);
257 /* Sun May 6 18:56:14 2001 ackley@cs.unm.edu:
258 Clear the closed fd info out of Client in
259 util_sock.c to avoid a possible
260 getpeername failure if we reopen the logs
261 and use %I in the filename.
263 Client = -1;
265 /* Force parent to check log size after
266 * spawning child. Fix from
267 * klausr@ITAP.Physik.Uni-Stuttgart.De. The
268 * parent smbd will log to logserver.smb. It
269 * writes only two messages for each child
270 * started/finished. But each child writes,
271 * say, 50 messages also in logserver.smb,
272 * begining with the debug_count of the
273 * parent, before the child opens its own log
274 * file logserver.client. In a worst case
275 * scenario the size of logserver.smb would be
276 * checked after about 50*50=2500 messages
277 * (ca. 100kb).
278 * */
279 force_check_log_size();
281 } /* end for num */
282 } /* end while 1 */
284 /* NOTREACHED return True; */
287 /****************************************************************************
288 reload the services file
289 **************************************************************************/
290 BOOL reload_services(BOOL test)
292 BOOL ret;
294 if (lp_loaded()) {
295 pstring fname;
296 pstrcpy(fname,lp_configfile());
297 if (file_exist(fname,NULL) && !strcsequal(fname,servicesf)) {
298 pstrcpy(servicesf,fname);
299 test = False;
303 reopen_logs();
305 if (test && !lp_file_list_changed())
306 return(True);
308 lp_killunused(conn_snum_used);
310 ret = lp_load(servicesf,False,False,True);
311 #ifdef PRINTING
312 load_printers();
313 #endif
314 /* perhaps the config filename is now set */
315 if (!test)
316 reload_services(True);
318 reopen_logs();
320 load_interfaces();
323 extern int Client;
324 if (Client != -1) {
325 set_socket_options(Client,"SO_KEEPALIVE");
326 set_socket_options(Client,user_socket_options);
330 reset_mangled_cache();
331 reset_stat_cache();
333 /* this forces service parameters to be flushed */
334 become_service(NULL,True);
336 return(ret);
341 /****************************************************************************
342 Catch a sighup.
343 ****************************************************************************/
345 VOLATILE SIG_ATOMIC_T reload_after_sighup = False;
347 static void sig_hup(int sig)
349 BlockSignals(True,SIGHUP);
350 DEBUG(1,("Got SIGHUP\n"));
353 * Fix from <branko.cibej@hermes.si> here.
354 * We used to reload in the signal handler - this
355 * is a *BIG* no-no.
358 reload_after_sighup = True;
359 BlockSignals(False,SIGHUP);
364 #if DUMP_CORE
365 /*******************************************************************
366 prepare to dump a core file - carefully!
367 ********************************************************************/
368 static BOOL dump_core(void)
370 char *p;
371 pstring dname;
372 pstrcpy(dname,debugf);
373 if ((p=strrchr(dname,'/'))) *p=0;
374 pstrcat(dname,"/corefiles");
375 mkdir(dname,0700);
376 sys_chown(dname,getuid(),getgid());
377 chmod(dname,0700);
378 if (chdir(dname)) return(False);
379 umask(~(0700));
381 #ifdef HAVE_GETRLIMIT
382 #ifdef RLIMIT_CORE
384 struct rlimit rlp;
385 getrlimit(RLIMIT_CORE, &rlp);
386 rlp.rlim_cur = MAX(4*1024*1024,rlp.rlim_cur);
387 setrlimit(RLIMIT_CORE, &rlp);
388 getrlimit(RLIMIT_CORE, &rlp);
389 DEBUG(3,("Core limits now %d %d\n",
390 (int)rlp.rlim_cur,(int)rlp.rlim_max));
392 #endif
393 #endif
396 DEBUG(0,("Dumping core in %s\n",dname));
397 abort();
398 return(True);
400 #endif
403 /****************************************************************************
404 exit the server
405 ****************************************************************************/
406 void exit_server(char *reason)
408 static int firsttime=1;
409 extern char *last_inbuf;
412 if (!firsttime) exit(0);
413 firsttime = 0;
415 unbecome_user();
416 DEBUG(2,("Closing connections\n"));
418 conn_close_all();
420 respond_to_all_remaining_local_messages();
422 #ifdef WITH_DFS
423 if (dcelogin_atmost_once) {
424 dfs_unlogin();
426 #endif
428 if (!reason) {
429 int oldlevel = DEBUGLEVEL;
430 DEBUGLEVEL = 10;
431 DEBUG(0,("Last message was %s\n",smb_fn_name(last_message)));
432 if (last_inbuf)
433 show_msg(last_inbuf);
434 DEBUGLEVEL = oldlevel;
435 DEBUG(0,("===============================================================\n"));
436 #if DUMP_CORE
437 if (dump_core()) return;
438 #endif
441 locking_end();
443 DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
444 exit(0);
449 /****************************************************************************
450 initialise connect, service and file structs
451 ****************************************************************************/
452 static void init_structs(void )
455 * Set the machine NETBIOS name if not already
456 * set from the config file.
459 if (!*global_myname) {
460 char *p;
461 fstrcpy( global_myname, myhostname() );
462 p = strchr( global_myname, '.' );
463 if (p)
464 *p = 0;
467 strupper( global_myname );
469 conn_init();
471 file_init();
473 /* for RPC pipes */
474 init_rpc_pipe_hnd();
476 /* for LSA handles */
477 init_lsa_policy_hnd();
479 init_dptrs();
482 /****************************************************************************
483 usage on the program
484 ****************************************************************************/
485 static void usage(char *pname)
488 printf("Usage: %s [-DaoPh?V] [-d debuglevel] [-l log basename] [-p port]\n", pname);
489 printf(" [-O socket options] [-s services file]\n");
490 printf("\t-D Become a daemon\n");
491 printf("\t-a Append to log file (default)\n");
492 printf("\t-o Overwrite log file, don't append\n");
493 printf("\t-P Passive only\n");
494 printf("\t-h Print usage\n");
495 printf("\t-? Print usage\n");
496 printf("\t-V Print version\n");
497 printf("\t-d debuglevel Set the debuglevel\n");
498 printf("\t-l log basename. Basename for log/debug files\n");
499 printf("\t-p port Listen on the specified port\n");
500 printf("\t-O socket options Socket options\n");
501 printf("\t-s services file. Filename of services file\n");
502 printf("\n");
506 /****************************************************************************
507 main program
508 ****************************************************************************/
509 int main(int argc,char *argv[])
511 extern BOOL append_log;
512 /* shall I run as a daemon */
513 BOOL is_daemon = False;
514 BOOL specified_logfile = False;
515 int port = SMB_PORT;
516 int opt;
517 extern char *optarg;
519 #ifdef HAVE_SET_AUTH_PARAMETERS
520 set_auth_parameters(argc,argv);
521 #endif
523 /* this is for people who can't start the program correctly */
524 while (argc > 1 && (*argv[1] != '-')) {
525 argv++;
526 argc--;
529 while ( EOF != (opt = getopt(argc, argv, "O:i:l:s:d:Dp:h?VPaof:")) )
530 switch (opt) {
531 case 'O':
532 pstrcpy(user_socket_options,optarg);
533 break;
535 case 'P':
537 extern BOOL passive;
538 passive = True;
540 break;
542 case 's':
543 pstrcpy(servicesf,optarg);
544 break;
546 case 'l':
547 specified_logfile = True;
548 pstrcpy(debugf,optarg);
549 break;
551 case 'a':
552 append_log = True;
553 break;
555 case 'o':
556 append_log = False;
557 break;
559 case 'D':
560 is_daemon = True;
561 break;
563 case 'd':
564 if (*optarg == 'A')
565 DEBUGLEVEL = 10000;
566 else
567 DEBUGLEVEL = atoi(optarg);
568 break;
570 case 'p':
571 port = atoi(optarg);
572 break;
574 case 'h':
575 case '?':
576 usage(argv[0]);
577 exit(0);
578 break;
580 case 'V':
581 printf("Version %s\n",VERSION);
582 exit(0);
583 break;
584 default:
585 DEBUG(0,("Incorrect program usage - are you sure the command line is correct?\n"));
586 usage(argv[0]);
587 exit(1);
590 #ifdef HAVE_SETLUID
591 /* needed for SecureWare on SCO */
592 setluid(0);
593 #endif
596 * gain_root_privilege uses an assert than will cause a core
597 * dump if euid != 0. Ensure this is the case.
600 if(geteuid() != (uid_t)0) {
601 fprintf(stderr, "%s: Version %s : Must have effective user id of zero to run.\n", argv[0], VERSION);
602 exit(1);
605 append_log = True;
607 TimeInit();
609 if(!specified_logfile)
610 pstrcpy(debugf,SMBLOGFILE);
612 pstrcpy(remote_machine, "smb");
614 setup_logging(argv[0],False);
616 charset_initialise();
618 /* we want to re-seed early to prevent time delays causing
619 client problems at a later date. (tridge) */
620 generate_random_buffer(NULL, 0, False);
622 /* make absolutely sure we run as root - to handle cases where people
623 are crazy enough to have it setuid */
625 gain_root_privilege();
626 gain_root_group_privilege();
628 fault_setup((void (*)(void *))exit_server);
629 CatchSignal(SIGTERM , SIGNAL_CAST dflt_sig);
631 /* we are never interested in SIGPIPE */
632 BlockSignals(True,SIGPIPE);
634 #if defined(SIGFPE)
635 /* we are never interested in SIGFPE */
636 BlockSignals(True,SIGFPE);
637 #endif
639 /* we want total control over the permissions on created files,
640 so set our umask to 0 */
641 umask(0);
643 dos_GetWd(OriginalDir);
645 init_uid();
647 reopen_logs();
649 DEBUG(1,( "smbd version %s started.\n", VERSION));
650 DEBUGADD(1,( "Copyright Andrew Tridgell 1992-1998\n"));
652 DEBUG(2,("uid=%d gid=%d euid=%d egid=%d\n",
653 (int)getuid(),(int)getgid(),(int)geteuid(),(int)getegid()));
655 if (sizeof(uint16) < 2 || sizeof(uint32) < 4) {
656 DEBUG(0,("ERROR: Samba is not configured correctly for the word size on your machine\n"));
657 exit(1);
661 * Do this before reload_services.
664 if (!reload_services(False))
665 return(-1);
667 init_structs();
669 #ifdef WITH_PROFILE
670 if (!profile_setup(False)) {
671 DEBUG(0,("ERROR: failed to setup profiling\n"));
672 return -1;
674 #endif
676 #ifdef WITH_SSL
678 extern BOOL sslEnabled;
679 sslEnabled = lp_ssl_enabled();
680 if(sslEnabled)
681 sslutil_init(True);
683 #endif /* WITH_SSL */
685 codepage_initialise(lp_client_code_page());
687 fstrcpy(global_myworkgroup, lp_workgroup());
689 if(!pdb_generate_sam_sid()) {
690 DEBUG(0,("ERROR: Samba cannot create a SAM SID.\n"));
691 exit(1);
694 CatchSignal(SIGHUP,SIGNAL_CAST sig_hup);
696 /* Setup the signals that allow the debug log level
697 to by dynamically changed. */
699 /* If we are using the malloc debug code we can't use
700 SIGUSR1 and SIGUSR2 to do debug level changes. */
702 #ifndef MEM_MAN
703 #if defined(SIGUSR1)
704 CatchSignal( SIGUSR1, SIGNAL_CAST sig_usr1 );
705 #endif /* SIGUSR1 */
707 #if defined(SIGUSR2)
708 CatchSignal( SIGUSR2, SIGNAL_CAST sig_usr2 );
709 #endif /* SIGUSR2 */
710 #endif /* MEM_MAN */
712 DEBUG(3,( "loaded services\n"));
714 if (!is_daemon && !is_a_socket(0)) {
715 DEBUG(0,("standard input is not a socket, assuming -D option\n"));
716 is_daemon = True;
719 if (is_daemon) {
720 DEBUG( 3, ( "Becoming a daemon.\n" ) );
721 become_daemon();
724 check_kernel_oplocks();
726 if (!directory_exist(lp_lockdir(), NULL)) {
727 mkdir(lp_lockdir(), 0755);
730 if (is_daemon) {
731 pidfile_create("smbd");
734 if (!open_sockets(is_daemon,port))
735 exit(1);
738 * Note that this call should be done after the fork() call
739 * in open_sockets(), as some versions of the locking shared
740 * memory code register openers in a flat file.
743 if (!locking_init(0))
744 exit(1);
746 if(!initialize_password_db())
747 exit(1);
749 /* possibly reload the services file. */
750 reload_services(True);
752 if (*lp_rootdir()) {
753 if (sys_chroot(lp_rootdir()) == 0)
754 DEBUG(2,("Changed root to %s\n", lp_rootdir()));
757 /* Setup the oplock IPC socket. */
758 if( !open_oplock_ipc() )
759 exit(1);
761 smbd_process();
762 close_sockets();
764 exit_server("normal exit");
765 return(0);