- we now specify the object files in the subsystems config.m4 file
[tprouty/samba.git] / source4 / smbd / server.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Main SMB server routines
4    Copyright (C) Andrew Tridgell                1992-1998
5    Copyright (C) Martin Pool                    2002
6    Copyright (C) Jelmer Vernooij                2002
7    Copyright (C) James J Myers                  2003 <myersjj@samba.org>
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25
26
27 /*
28   called on a fatal error that should cause this server to terminate
29 */
30 void exit_server(struct server_context *smb, const char *reason)
31 {
32         smb->model_ops->terminate_connection(smb, reason);
33 }
34
35
36 /*
37   setup a single listener of any type
38  */
39 static void setup_listen(struct event_context *events,
40                          struct model_ops *model_ops, 
41                          void (*accept_handler)(struct event_context *,struct fd_event *,time_t,uint16),
42                          struct in_addr *ifip, unsigned port)
43 {
44         struct fd_event fde;
45         fde.fd = open_socket_in(SOCK_STREAM, port, 0, ifip->s_addr, True);
46         if (fde.fd == -1) {
47                 DEBUG(0,("Failed to open socket on %s:%u - %s\n",
48                          inet_ntoa(*ifip), port, strerror(errno)));
49                 return;
50         }
51
52         /* ready to listen */
53         set_socket_options(fde.fd, "SO_KEEPALIVE"); 
54         set_socket_options(fde.fd, lp_socket_options());
55       
56         if (listen(fde.fd, SMBD_LISTEN_BACKLOG) == -1) {
57                 DEBUG(0,("Failed to listen on %s:%d - %s\n",
58                          inet_ntoa(*ifip), port, strerror(errno)));
59                 close(fde.fd);
60                 return;
61         }
62
63         /* we are only interested in read events on the listen socket */
64         fde.flags = EVENT_FD_READ;
65         fde.private = model_ops;
66         fde.handler = accept_handler;
67         
68         event_add_fd(events, &fde);
69 }
70
71 /*
72   add a socket address to the list of events, one event per port
73 */
74 static void add_socket(struct event_context *events, 
75                        struct model_ops *model_ops, 
76                        struct in_addr *ifip)
77 {
78         char *ptr, *tok;
79         const char *delim = ", ";
80
81         for (tok=strtok_r(lp_smb_ports(), delim, &ptr); 
82              tok; 
83              tok=strtok_r(NULL, delim, &ptr)) {
84                 unsigned port = atoi(tok);
85                 if (port == 0) continue;
86                 setup_listen(events, model_ops, model_ops->accept_connection, ifip, port);
87         }
88 }
89
90 /****************************************************************************
91  Open the socket communication.
92 ****************************************************************************/
93 static void open_sockets_smbd(struct event_context *events,
94                               const struct model_ops *model_ops)
95 {
96         if (lp_interfaces() && lp_bind_interfaces_only()) {
97                 int num_interfaces = iface_count();
98                 int i;
99
100                 /* We have been given an interfaces line, and been 
101                    told to only bind to those interfaces. Create a
102                    socket per interface and bind to only these.
103                 */
104                 for(i = 0; i < num_interfaces; i++) {
105                         struct in_addr *ifip = iface_n_ip(i);
106
107                         if (ifip == NULL) {
108                                 DEBUG(0,("open_sockets_smbd: interface %d has NULL IP address !\n", i));
109                                 continue;
110                         }
111
112                         add_socket(events, model_ops, ifip);
113                 }
114         } else {
115                 TALLOC_CTX *mem_ctx = talloc_init("open_sockets_smbd");
116                 
117                 struct in_addr *ifip = interpret_addr2(mem_ctx, lp_socket_address());
118                 /* Just bind to lp_socket_address() (usually 0.0.0.0) */
119                 if (!mem_ctx) {
120                         smb_panic("No memory");
121                 }
122                 add_socket(events, model_ops, ifip);
123                 talloc_destroy(mem_ctx);
124         } 
125 }
126
127 /****************************************************************************
128  Reload the services file.
129 **************************************************************************/
130 BOOL reload_services(struct server_context *smb, BOOL test)
131 {
132         BOOL ret;
133         
134         if (lp_loaded()) {
135                 pstring fname;
136                 pstrcpy(fname,lp_configfile());
137                 if (file_exist(fname, NULL) &&
138                     !strcsequal(fname, dyn_CONFIGFILE)) {
139                         pstrcpy(dyn_CONFIGFILE, fname);
140                         test = False;
141                 }
142         }
143
144         reopen_logs();
145
146         if (test && !lp_file_list_changed())
147                 return(True);
148
149         if (smb) {
150                 lp_killunused(smb, conn_snum_used);
151         }
152         
153         ret = lp_load(dyn_CONFIGFILE, False, False, True);
154
155         load_printers();
156
157         /* perhaps the config filename is now set */
158         if (!test)
159                 reload_services(smb, True);
160
161         reopen_logs();
162
163         load_interfaces();
164
165         mangle_reset_cache();
166         reset_stat_cache();
167
168         /* this forces service parameters to be flushed */
169         set_current_service(NULL,True);
170
171         return(ret);
172 }
173
174 /****************************************************************************
175  Initialise connect, service and file structs.
176 ****************************************************************************/
177 static BOOL init_structs(void)
178 {
179         init_names();
180         file_init();
181         secrets_init();
182
183         /* we want to re-seed early to prevent time delays causing
184            client problems at a later date. (tridge) */
185         generate_random_buffer(NULL, 0, False);
186
187         return True;
188 }
189
190
191 /*
192   setup the events for the chosen process model
193 */
194 static void setup_process_model(struct event_context *events, 
195                                 const char *model)
196 {
197         const struct model_ops *ops;
198
199         process_model_init();
200
201         ops = process_model_byname(model);
202         if (!ops) {
203                 DEBUG(0,("Unknown process model '%s'\n", model));
204                 exit(-1);
205         }
206
207         ops->model_startup();
208
209         /* now setup the listening sockets, adding 
210            event handlers to the events structure */
211         open_sockets_smbd(events, ops);
212
213         /* setup any sockets we need to listen on for RPC over TCP */
214         open_sockets_rpc(events, ops);
215 }
216
217 /****************************************************************************
218  main program.
219 ****************************************************************************/
220  int main(int argc,const char *argv[])
221 {
222         BOOL is_daemon = False;
223         BOOL interactive = False;
224         BOOL Fork = True;
225         BOOL log_stdout = False;
226         int opt;
227         poptContext pc;
228         struct event_context *events;
229         const char *model = "standard";
230         struct poptOption long_options[] = {
231                 POPT_AUTOHELP
232         {"daemon", 'D', POPT_ARG_VAL, &is_daemon, True, "Become a daemon (default)" },
233         {"interactive", 'i', POPT_ARG_VAL, &interactive, True, "Run interactive (not a daemon)"},
234         {"foreground", 'F', POPT_ARG_VAL, &Fork, False, "Run daemon in foreground (for daemontools & etc)" },
235         {"log-stdout", 'S', POPT_ARG_VAL, &log_stdout, True, "Log to stdout" },
236         {"build-options", 'b', POPT_ARG_NONE, NULL, 'b', "Print build options" },
237         {"port", 'p', POPT_ARG_STRING, NULL, 0, "Listen on the specified ports"},
238         {"model", 'M', POPT_ARG_STRING, &model, 0, "select process model"},
239         POPT_COMMON_SAMBA
240         { NULL }
241         };
242         
243         pc = poptGetContext("smbd", argc, argv, long_options, 0);
244         
245         while((opt = poptGetNextOpt(pc)) != -1) {
246                 switch (opt)  {
247                 case 'b':
248                         /* Display output to screen as well as debug */
249                         build_options(True); 
250                         exit(0);
251                         break;
252                 case 'p':
253                         lp_set_cmdline("smb ports", poptGetOptArg(pc));
254                         break;
255                 }
256         }
257         poptFreeContext(pc);
258
259         events = event_context_init();
260
261         load_case_tables();
262
263         if (interactive) {
264                 Fork = False;
265                 log_stdout = True;
266         }
267
268         if (log_stdout && Fork) {
269                 DEBUG(0,("ERROR: Can't log to stdout (-S) unless daemon is in foreground (-F) or interactive (-i)\n"));
270                 exit(1);
271         }
272         setup_logging(argv[0], log_stdout?DEBUG_STDOUT:DEBUG_FILE);
273
274         fault_setup((void (*)(void *))exit_server);
275         
276         /* we are never interested in SIGPIPE */
277         BlockSignals(True,SIGPIPE);
278
279 #if defined(SIGFPE)
280         /* we are never interested in SIGFPE */
281         BlockSignals(True,SIGFPE);
282 #endif
283
284 #if defined(SIGUSR2)
285         /* We are no longer interested in USR2 */
286         BlockSignals(True,SIGUSR2);
287 #endif
288
289         /* POSIX demands that signals are inherited. If the invoking process has
290          * these signals masked, we will have problems, as we won't recieve them. */
291         BlockSignals(False, SIGHUP);
292         BlockSignals(False, SIGUSR1);
293         BlockSignals(False, SIGTERM);
294
295         /* we want total control over the permissions on created files,
296            so set our umask to 0 */
297         umask(0);
298
299         reopen_logs();
300
301         DEBUG(0,("smbd version %s started.\n", SAMBA_VERSION_STRING));
302         DEBUGADD(0,("Copyright Andrew Tridgell and the Samba Team 1992-2004\n"));
303
304         /* Output the build options to the debug log */ 
305         build_options(False);
306
307         if (sizeof(uint16) < 2 || sizeof(uint32) < 4) {
308                 DEBUG(0,("ERROR: Samba is not configured correctly for the word size on your machine\n"));
309                 exit(1);
310         }
311         DEBUG(0,("Using %s process model\n", model));
312                         
313         if (!reload_services(NULL, False))
314                 return(-1);     
315
316         init_structs();
317
318         if (!is_daemon && !is_a_socket(0)) {
319                 if (!interactive)
320                         DEBUG(0,("standard input is not a socket, assuming -D option\n"));
321
322                 /*
323                  * Setting is_daemon here prevents us from eventually calling
324                  * the open_sockets_inetd()
325                  */
326
327                 is_daemon = True;
328         }
329
330         if (is_daemon && !interactive) {
331                 DEBUG(3,("Becoming a daemon.\n"));
332                 become_daemon(Fork);
333         }
334
335         if (!directory_exist(lp_lockdir(), NULL)) {
336                 mkdir(lp_lockdir(), 0755);
337         }
338
339         if (is_daemon) {
340                 pidfile_create("smbd");
341         }
342
343         register_msg_pool_usage();
344         register_dmalloc_msgs();
345
346         setup_process_model(events, model);
347
348         /* wait for events */
349         return event_loop_wait(events);
350 }