r962: convert 'unsigned' and 'unsigned int' to uint_t
[bbaumbach/samba-autobuild/.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                          const struct model_ops *model_ops, 
41                          void (*accept_handler)(struct event_context *,struct fd_event *,time_t,uint16_t),
42                          struct in_addr *ifip, uint_t 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                        const 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                 uint_t 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         ops = process_model_byname(model);
200         if (!ops) {
201                 DEBUG(0,("Unknown process model '%s'\n", model));
202                 exit(-1);
203         }
204
205         ops->model_startup();
206
207         /* now setup the listening sockets, adding 
208            event handlers to the events structure */
209         open_sockets_smbd(events, ops);
210
211         /* setup any sockets we need to listen on for RPC over TCP */
212         open_sockets_rpc(events, ops);
213 }
214
215 /****************************************************************************
216  main program.
217 ****************************************************************************/
218  int main(int argc,const char *argv[])
219 {
220         BOOL is_daemon = False;
221         BOOL interactive = False;
222         BOOL Fork = True;
223         BOOL log_stdout = False;
224         int opt;
225         poptContext pc;
226         struct event_context *events;
227         const char *model = "standard";
228         struct poptOption long_options[] = {
229                 POPT_AUTOHELP
230         {"daemon", 'D', POPT_ARG_VAL, &is_daemon, True, "Become a daemon (default)" },
231         {"interactive", 'i', POPT_ARG_VAL, &interactive, True, "Run interactive (not a daemon)"},
232         {"foreground", 'F', POPT_ARG_VAL, &Fork, False, "Run daemon in foreground (for daemontools & etc)" },
233         {"log-stdout", 'S', POPT_ARG_VAL, &log_stdout, True, "Log to stdout" },
234         {"build-options", 'b', POPT_ARG_NONE, NULL, 'b', "Print build options" },
235         {"port", 'p', POPT_ARG_STRING, NULL, 0, "Listen on the specified ports"},
236         {"model", 'M', POPT_ARG_STRING, &model, 0, "select process model"},
237         POPT_COMMON_SAMBA
238         { NULL }
239         };
240         
241         pc = poptGetContext("smbd", argc, argv, long_options, 0);
242         
243         while((opt = poptGetNextOpt(pc)) != -1) {
244                 switch (opt)  {
245                 case 'b':
246                         /* Display output to screen as well as debug */
247                         build_options(True); 
248                         exit(0);
249                         break;
250                 case 'p':
251                         lp_set_cmdline("smb ports", poptGetOptArg(pc));
252                         break;
253                 }
254         }
255         poptFreeContext(pc);
256
257         events = event_context_init();
258
259         load_case_tables();
260
261         if (interactive) {
262                 Fork = False;
263                 log_stdout = True;
264         }
265
266         if (log_stdout && Fork) {
267                 DEBUG(0,("ERROR: Can't log to stdout (-S) unless daemon is in foreground (-F) or interactive (-i)\n"));
268                 exit(1);
269         }
270         setup_logging(argv[0], log_stdout?DEBUG_STDOUT:DEBUG_FILE);
271
272         fault_setup((void (*)(void *))exit_server);
273         
274         /* we are never interested in SIGPIPE */
275         BlockSignals(True,SIGPIPE);
276
277 #if defined(SIGFPE)
278         /* we are never interested in SIGFPE */
279         BlockSignals(True,SIGFPE);
280 #endif
281
282 #if defined(SIGUSR2)
283         /* We are no longer interested in USR2 */
284         BlockSignals(True,SIGUSR2);
285 #endif
286
287         /* POSIX demands that signals are inherited. If the invoking process has
288          * these signals masked, we will have problems, as we won't recieve them. */
289         BlockSignals(False, SIGHUP);
290         BlockSignals(False, SIGUSR1);
291         BlockSignals(False, SIGTERM);
292
293         /* we want total control over the permissions on created files,
294            so set our umask to 0 */
295         umask(0);
296
297         reopen_logs();
298
299         DEBUG(0,("smbd version %s started.\n", SAMBA_VERSION_STRING));
300         DEBUGADD(0,("Copyright Andrew Tridgell and the Samba Team 1992-2004\n"));
301
302         /* Output the build options to the debug log */ 
303         build_options(False);
304
305         if (sizeof(uint16_t) < 2 || sizeof(uint32_t) < 4) {
306                 DEBUG(0,("ERROR: Samba is not configured correctly for the word size on your machine\n"));
307                 exit(1);
308         }
309         DEBUG(0,("Using %s process model\n", model));
310                         
311         if (!reload_services(NULL, False))
312                 return(-1);     
313
314         init_structs();
315
316         if (!is_daemon && !is_a_socket(0)) {
317                 if (!interactive)
318                         DEBUG(0,("standard input is not a socket, assuming -D option\n"));
319
320                 /*
321                  * Setting is_daemon here prevents us from eventually calling
322                  * the open_sockets_inetd()
323                  */
324
325                 is_daemon = True;
326         }
327
328         if (is_daemon && !interactive) {
329                 DEBUG(3,("Becoming a daemon.\n"));
330                 become_daemon(Fork);
331         }
332
333         if (!directory_exist(lp_lockdir(), NULL)) {
334                 mkdir(lp_lockdir(), 0755);
335         }
336
337         if (is_daemon) {
338                 pidfile_create("smbd");
339         }
340
341         register_msg_pool_usage();
342         register_dmalloc_msgs();
343
344         init_subsystems();
345
346         setup_process_model(events, model);
347
348         /* wait for events */
349         return event_loop_wait(events);
350 }