1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 | NBT netbios routines and daemon - version 2
|
---|
4 | Copyright (C) Andrew Tridgell 1994-1998
|
---|
5 | Copyright (C) Jeremy Allison 1997-2002
|
---|
6 | Copyright (C) Jelmer Vernooij 2002,2003 (Conversion to popt)
|
---|
7 |
|
---|
8 | This program is free software; you can redistribute it and/or modify
|
---|
9 | it under the terms of the GNU General Public License as published by
|
---|
10 | the Free Software Foundation; either version 3 of the License, or
|
---|
11 | (at your option) any later version.
|
---|
12 |
|
---|
13 | This program is distributed in the hope that it will be useful,
|
---|
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
16 | GNU General Public License for more details.
|
---|
17 |
|
---|
18 | You should have received a copy of the GNU General Public License
|
---|
19 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
20 | */
|
---|
21 |
|
---|
22 | #include "includes.h"
|
---|
23 | #include "system/filesys.h"
|
---|
24 | #include "popt_common.h"
|
---|
25 | #include "nmbd/nmbd.h"
|
---|
26 | #include "serverid.h"
|
---|
27 | #include "messages.h"
|
---|
28 |
|
---|
29 | int ClientNMB = -1;
|
---|
30 | int ClientDGRAM = -1;
|
---|
31 | int global_nmb_port = -1;
|
---|
32 |
|
---|
33 | extern bool rescan_listen_set;
|
---|
34 | extern bool global_in_nmbd;
|
---|
35 |
|
---|
36 | #ifdef __OS2__
|
---|
37 | extern int global_Sem32Add;
|
---|
38 | #endif
|
---|
39 |
|
---|
40 | extern bool override_logfile;
|
---|
41 |
|
---|
42 | /* have we found LanMan clients yet? */
|
---|
43 | bool found_lm_clients = False;
|
---|
44 |
|
---|
45 | /* what server type are we currently */
|
---|
46 |
|
---|
47 | time_t StartupTime = 0;
|
---|
48 |
|
---|
49 | struct event_context *nmbd_event_context(void)
|
---|
50 | {
|
---|
51 | return server_event_context();
|
---|
52 | }
|
---|
53 |
|
---|
54 | struct messaging_context *nmbd_messaging_context(void)
|
---|
55 | {
|
---|
56 | struct messaging_context *msg_ctx = server_messaging_context();
|
---|
57 | if (likely(msg_ctx != NULL)) {
|
---|
58 | return msg_ctx;
|
---|
59 | }
|
---|
60 | smb_panic("Could not init nmbd's messaging context.\n");
|
---|
61 | return NULL;
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**************************************************************************** **
|
---|
65 | Handle a SIGTERM in band.
|
---|
66 | **************************************************************************** */
|
---|
67 |
|
---|
68 | static void terminate(void)
|
---|
69 | {
|
---|
70 | DEBUG(0,("Got SIGTERM: going down...\n"));
|
---|
71 |
|
---|
72 | /* Write out wins.dat file if samba is a WINS server */
|
---|
73 | wins_write_database(0,False);
|
---|
74 |
|
---|
75 | /* Remove all SELF registered names from WINS */
|
---|
76 | release_wins_names();
|
---|
77 |
|
---|
78 | /* Announce all server entries as 0 time-to-live, 0 type. */
|
---|
79 | announce_my_servers_removed();
|
---|
80 |
|
---|
81 | /* If there was an async dns child - kill it. */
|
---|
82 | kill_async_dns_child();
|
---|
83 |
|
---|
84 | gencache_stabilize();
|
---|
85 | serverid_deregister(procid_self());
|
---|
86 |
|
---|
87 | pidfile_unlink();
|
---|
88 |
|
---|
89 | exit(0);
|
---|
90 | }
|
---|
91 |
|
---|
92 | static void nmbd_sig_term_handler(struct tevent_context *ev,
|
---|
93 | struct tevent_signal *se,
|
---|
94 | int signum,
|
---|
95 | int count,
|
---|
96 | void *siginfo,
|
---|
97 | void *private_data)
|
---|
98 | {
|
---|
99 | terminate();
|
---|
100 | }
|
---|
101 |
|
---|
102 | static bool nmbd_setup_sig_term_handler(void)
|
---|
103 | {
|
---|
104 | struct tevent_signal *se;
|
---|
105 |
|
---|
106 | se = tevent_add_signal(nmbd_event_context(),
|
---|
107 | nmbd_event_context(),
|
---|
108 | SIGTERM, 0,
|
---|
109 | nmbd_sig_term_handler,
|
---|
110 | NULL);
|
---|
111 | if (!se) {
|
---|
112 | DEBUG(0,("failed to setup SIGTERM handler"));
|
---|
113 | return false;
|
---|
114 | }
|
---|
115 |
|
---|
116 | return true;
|
---|
117 | }
|
---|
118 |
|
---|
119 | static void msg_reload_nmbd_services(struct messaging_context *msg,
|
---|
120 | void *private_data,
|
---|
121 | uint32_t msg_type,
|
---|
122 | struct server_id server_id,
|
---|
123 | DATA_BLOB *data);
|
---|
124 |
|
---|
125 | static void nmbd_sig_hup_handler(struct tevent_context *ev,
|
---|
126 | struct tevent_signal *se,
|
---|
127 | int signum,
|
---|
128 | int count,
|
---|
129 | void *siginfo,
|
---|
130 | void *private_data)
|
---|
131 | {
|
---|
132 | DEBUG(0,("Got SIGHUP dumping debug info.\n"));
|
---|
133 | msg_reload_nmbd_services(nmbd_messaging_context(),
|
---|
134 | NULL, MSG_SMB_CONF_UPDATED,
|
---|
135 | procid_self(), NULL);
|
---|
136 | }
|
---|
137 |
|
---|
138 | static bool nmbd_setup_sig_hup_handler(void)
|
---|
139 | {
|
---|
140 | struct tevent_signal *se;
|
---|
141 |
|
---|
142 | se = tevent_add_signal(nmbd_event_context(),
|
---|
143 | nmbd_event_context(),
|
---|
144 | SIGHUP, 0,
|
---|
145 | nmbd_sig_hup_handler,
|
---|
146 | NULL);
|
---|
147 | if (!se) {
|
---|
148 | DEBUG(0,("failed to setup SIGHUP handler"));
|
---|
149 | return false;
|
---|
150 | }
|
---|
151 |
|
---|
152 | return true;
|
---|
153 | }
|
---|
154 |
|
---|
155 | /**************************************************************************** **
|
---|
156 | Handle a SHUTDOWN message from smbcontrol.
|
---|
157 | **************************************************************************** */
|
---|
158 |
|
---|
159 | static void nmbd_terminate(struct messaging_context *msg,
|
---|
160 | void *private_data,
|
---|
161 | uint32_t msg_type,
|
---|
162 | struct server_id server_id,
|
---|
163 | DATA_BLOB *data)
|
---|
164 | {
|
---|
165 | terminate();
|
---|
166 | }
|
---|
167 |
|
---|
168 | /**************************************************************************** **
|
---|
169 | Possibly continue after a fault.
|
---|
170 | **************************************************************************** */
|
---|
171 |
|
---|
172 | static void fault_continue(void)
|
---|
173 | {
|
---|
174 | dump_core();
|
---|
175 | }
|
---|
176 |
|
---|
177 | /**************************************************************************** **
|
---|
178 | Expire old names from the namelist and server list.
|
---|
179 | **************************************************************************** */
|
---|
180 |
|
---|
181 | static void expire_names_and_servers(time_t t)
|
---|
182 | {
|
---|
183 | static time_t lastrun = 0;
|
---|
184 |
|
---|
185 | if ( !lastrun )
|
---|
186 | lastrun = t;
|
---|
187 | if ( t < (lastrun + 5) )
|
---|
188 | return;
|
---|
189 | lastrun = t;
|
---|
190 |
|
---|
191 | /*
|
---|
192 | * Expire any timed out names on all the broadcast
|
---|
193 | * subnets and those registered with the WINS server.
|
---|
194 | * (nmbd_namelistdb.c)
|
---|
195 | */
|
---|
196 |
|
---|
197 | expire_names(t);
|
---|
198 |
|
---|
199 | /*
|
---|
200 | * Go through all the broadcast subnets and for each
|
---|
201 | * workgroup known on that subnet remove any expired
|
---|
202 | * server names. If a workgroup has an empty serverlist
|
---|
203 | * and has itself timed out then remove the workgroup.
|
---|
204 | * (nmbd_workgroupdb.c)
|
---|
205 | */
|
---|
206 |
|
---|
207 | expire_workgroups_and_servers(t);
|
---|
208 | }
|
---|
209 |
|
---|
210 | /************************************************************************** **
|
---|
211 | Reload the list of network interfaces.
|
---|
212 | Doesn't return until a network interface is up.
|
---|
213 | ************************************************************************** */
|
---|
214 |
|
---|
215 | static void reload_interfaces(time_t t)
|
---|
216 | {
|
---|
217 | static time_t lastt;
|
---|
218 | int n;
|
---|
219 | bool print_waiting_msg = true;
|
---|
220 | struct subnet_record *subrec;
|
---|
221 |
|
---|
222 | if (t && ((t - lastt) < NMBD_INTERFACES_RELOAD)) {
|
---|
223 | return;
|
---|
224 | }
|
---|
225 |
|
---|
226 | lastt = t;
|
---|
227 |
|
---|
228 | if (!interfaces_changed()) {
|
---|
229 | return;
|
---|
230 | }
|
---|
231 |
|
---|
232 | try_again:
|
---|
233 |
|
---|
234 | /* the list of probed interfaces has changed, we may need to add/remove
|
---|
235 | some subnets */
|
---|
236 | load_interfaces();
|
---|
237 |
|
---|
238 | /* find any interfaces that need adding */
|
---|
239 | for (n=iface_count() - 1; n >= 0; n--) {
|
---|
240 | char str[INET6_ADDRSTRLEN];
|
---|
241 | const struct interface *iface = get_interface(n);
|
---|
242 | struct in_addr ip, nmask;
|
---|
243 |
|
---|
244 | if (!iface) {
|
---|
245 | DEBUG(2,("reload_interfaces: failed to get interface %d\n", n));
|
---|
246 | continue;
|
---|
247 | }
|
---|
248 |
|
---|
249 | /* Ensure we're only dealing with IPv4 here. */
|
---|
250 | if (iface->ip.ss_family != AF_INET) {
|
---|
251 | DEBUG(2,("reload_interfaces: "
|
---|
252 | "ignoring non IPv4 interface.\n"));
|
---|
253 | continue;
|
---|
254 | }
|
---|
255 |
|
---|
256 | ip = ((struct sockaddr_in *)(void *)&iface->ip)->sin_addr;
|
---|
257 | nmask = ((struct sockaddr_in *)(void *)
|
---|
258 | &iface->netmask)->sin_addr;
|
---|
259 |
|
---|
260 | /*
|
---|
261 | * We don't want to add a loopback interface, in case
|
---|
262 | * someone has added 127.0.0.1 for smbd, nmbd needs to
|
---|
263 | * ignore it here. JRA.
|
---|
264 | */
|
---|
265 |
|
---|
266 | if (is_loopback_addr((struct sockaddr *)(void *)&iface->ip)) {
|
---|
267 | DEBUG(2,("reload_interfaces: Ignoring loopback "
|
---|
268 | "interface %s\n",
|
---|
269 | print_sockaddr(str, sizeof(str), &iface->ip) ));
|
---|
270 | continue;
|
---|
271 | }
|
---|
272 |
|
---|
273 | for (subrec=subnetlist; subrec; subrec=subrec->next) {
|
---|
274 | if (ip_equal_v4(ip, subrec->myip) &&
|
---|
275 | ip_equal_v4(nmask, subrec->mask_ip)) {
|
---|
276 | break;
|
---|
277 | }
|
---|
278 | }
|
---|
279 |
|
---|
280 | if (!subrec) {
|
---|
281 | /* it wasn't found! add it */
|
---|
282 | DEBUG(2,("Found new interface %s\n",
|
---|
283 | print_sockaddr(str,
|
---|
284 | sizeof(str), &iface->ip) ));
|
---|
285 | subrec = make_normal_subnet(iface);
|
---|
286 | if (subrec)
|
---|
287 | register_my_workgroup_one_subnet(subrec);
|
---|
288 | }
|
---|
289 | }
|
---|
290 |
|
---|
291 | /* find any interfaces that need deleting */
|
---|
292 | for (subrec=subnetlist; subrec; subrec=subrec->next) {
|
---|
293 | for (n=iface_count() - 1; n >= 0; n--) {
|
---|
294 | struct interface *iface = get_interface(n);
|
---|
295 | struct in_addr ip, nmask;
|
---|
296 | if (!iface) {
|
---|
297 | continue;
|
---|
298 | }
|
---|
299 | /* Ensure we're only dealing with IPv4 here. */
|
---|
300 | if (iface->ip.ss_family != AF_INET) {
|
---|
301 | DEBUG(2,("reload_interfaces: "
|
---|
302 | "ignoring non IPv4 interface.\n"));
|
---|
303 | continue;
|
---|
304 | }
|
---|
305 | ip = ((struct sockaddr_in *)(void *)
|
---|
306 | &iface->ip)->sin_addr;
|
---|
307 | nmask = ((struct sockaddr_in *)(void *)
|
---|
308 | &iface->netmask)->sin_addr;
|
---|
309 | if (ip_equal_v4(ip, subrec->myip) &&
|
---|
310 | ip_equal_v4(nmask, subrec->mask_ip)) {
|
---|
311 | break;
|
---|
312 | }
|
---|
313 | }
|
---|
314 | if (n == -1) {
|
---|
315 | /* oops, an interface has disapeared. This is
|
---|
316 | tricky, we don't dare actually free the
|
---|
317 | interface as it could be being used, so
|
---|
318 | instead we just wear the memory leak and
|
---|
319 | remove it from the list of interfaces without
|
---|
320 | freeing it */
|
---|
321 | DEBUG(2,("Deleting dead interface %s\n",
|
---|
322 | inet_ntoa(subrec->myip)));
|
---|
323 | close_subnet(subrec);
|
---|
324 | }
|
---|
325 | }
|
---|
326 |
|
---|
327 | rescan_listen_set = True;
|
---|
328 |
|
---|
329 | /* We need to wait if there are no subnets... */
|
---|
330 | if (FIRST_SUBNET == NULL) {
|
---|
331 | void (*saved_handler)(int);
|
---|
332 |
|
---|
333 | if (print_waiting_msg) {
|
---|
334 | DEBUG(0,("reload_interfaces: "
|
---|
335 | "No subnets to listen to. Waiting..\n"));
|
---|
336 | print_waiting_msg = false;
|
---|
337 | }
|
---|
338 |
|
---|
339 | /*
|
---|
340 | * Whilst we're waiting for an interface, allow SIGTERM to
|
---|
341 | * cause us to exit.
|
---|
342 | */
|
---|
343 | saved_handler = CatchSignal(SIGTERM, SIG_DFL);
|
---|
344 |
|
---|
345 | /* We only count IPv4, non-loopback interfaces here. */
|
---|
346 | while (iface_count_v4_nl() == 0) {
|
---|
347 | sleep(5);
|
---|
348 | load_interfaces();
|
---|
349 | }
|
---|
350 |
|
---|
351 | CatchSignal(SIGTERM, saved_handler);
|
---|
352 |
|
---|
353 | /*
|
---|
354 | * We got an interface, go back to blocking term.
|
---|
355 | */
|
---|
356 |
|
---|
357 | goto try_again;
|
---|
358 | }
|
---|
359 | }
|
---|
360 |
|
---|
361 | /**************************************************************************** **
|
---|
362 | Reload the services file.
|
---|
363 | **************************************************************************** */
|
---|
364 |
|
---|
365 | static bool reload_nmbd_services(bool test)
|
---|
366 | {
|
---|
367 | bool ret;
|
---|
368 |
|
---|
369 | set_remote_machine_name("nmbd", False);
|
---|
370 |
|
---|
371 | if ( lp_loaded() ) {
|
---|
372 | const char *fname = lp_configfile();
|
---|
373 | if (file_exist(fname) && !strcsequal(fname,get_dyn_CONFIGFILE())) {
|
---|
374 | set_dyn_CONFIGFILE(fname);
|
---|
375 | test = False;
|
---|
376 | }
|
---|
377 | }
|
---|
378 |
|
---|
379 | if ( test && !lp_file_list_changed() )
|
---|
380 | return(True);
|
---|
381 |
|
---|
382 | ret = lp_load(get_dyn_CONFIGFILE(), True , False, False, True);
|
---|
383 |
|
---|
384 | /* perhaps the config filename is now set */
|
---|
385 | if ( !test ) {
|
---|
386 | DEBUG( 3, ( "services not loaded\n" ) );
|
---|
387 | reload_nmbd_services( True );
|
---|
388 | }
|
---|
389 |
|
---|
390 | #ifdef __OS2__
|
---|
391 | /* restore the logfile to log.nmbd always */
|
---|
392 | char *lfile = NULL;
|
---|
393 | if (asprintf(&lfile, "%s/log.nmbd", get_dyn_LOGFILEBASE()) < 0) {
|
---|
394 | exit(1);
|
---|
395 | }
|
---|
396 | lp_set_logfile(lfile);
|
---|
397 | SAFE_FREE(lfile);
|
---|
398 | #endif
|
---|
399 |
|
---|
400 | return(ret);
|
---|
401 | }
|
---|
402 |
|
---|
403 | /**************************************************************************** **
|
---|
404 | * React on 'smbcontrol nmbd reload-config' in the same way as to SIGHUP
|
---|
405 | **************************************************************************** */
|
---|
406 |
|
---|
407 | static void msg_reload_nmbd_services(struct messaging_context *msg,
|
---|
408 | void *private_data,
|
---|
409 | uint32_t msg_type,
|
---|
410 | struct server_id server_id,
|
---|
411 | DATA_BLOB *data)
|
---|
412 | {
|
---|
413 | write_browse_list( 0, True );
|
---|
414 | dump_all_namelists();
|
---|
415 | reload_nmbd_services( True );
|
---|
416 | reopen_logs();
|
---|
417 | reload_interfaces(0);
|
---|
418 | }
|
---|
419 |
|
---|
420 | static void msg_nmbd_send_packet(struct messaging_context *msg,
|
---|
421 | void *private_data,
|
---|
422 | uint32_t msg_type,
|
---|
423 | struct server_id src,
|
---|
424 | DATA_BLOB *data)
|
---|
425 | {
|
---|
426 | struct packet_struct *p = (struct packet_struct *)data->data;
|
---|
427 | struct subnet_record *subrec;
|
---|
428 | struct sockaddr_storage ss;
|
---|
429 | const struct sockaddr_storage *pss;
|
---|
430 | const struct in_addr *local_ip;
|
---|
431 |
|
---|
432 | DEBUG(10, ("Received send_packet from %u\n", (unsigned int)procid_to_pid(&src)));
|
---|
433 |
|
---|
434 | if (data->length != sizeof(struct packet_struct)) {
|
---|
435 | DEBUG(2, ("Discarding invalid packet length from %u\n",
|
---|
436 | (unsigned int)procid_to_pid(&src)));
|
---|
437 | return;
|
---|
438 | }
|
---|
439 |
|
---|
440 | if ((p->packet_type != NMB_PACKET) &&
|
---|
441 | (p->packet_type != DGRAM_PACKET)) {
|
---|
442 | DEBUG(2, ("Discarding invalid packet type from %u: %d\n",
|
---|
443 | (unsigned int)procid_to_pid(&src), p->packet_type));
|
---|
444 | return;
|
---|
445 | }
|
---|
446 |
|
---|
447 | in_addr_to_sockaddr_storage(&ss, p->ip);
|
---|
448 | pss = iface_ip((struct sockaddr *)(void *)&ss);
|
---|
449 |
|
---|
450 | if (pss == NULL) {
|
---|
451 | DEBUG(2, ("Could not find ip for packet from %u\n",
|
---|
452 | (unsigned int)procid_to_pid(&src)));
|
---|
453 | return;
|
---|
454 | }
|
---|
455 |
|
---|
456 | local_ip = &((const struct sockaddr_in *)pss)->sin_addr;
|
---|
457 | subrec = FIRST_SUBNET;
|
---|
458 |
|
---|
459 | p->recv_fd = -1;
|
---|
460 | p->send_fd = (p->packet_type == NMB_PACKET) ?
|
---|
461 | subrec->nmb_sock : subrec->dgram_sock;
|
---|
462 |
|
---|
463 | for (subrec = FIRST_SUBNET; subrec != NULL;
|
---|
464 | subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
|
---|
465 | if (ip_equal_v4(*local_ip, subrec->myip)) {
|
---|
466 | p->send_fd = (p->packet_type == NMB_PACKET) ?
|
---|
467 | subrec->nmb_sock : subrec->dgram_sock;
|
---|
468 | break;
|
---|
469 | }
|
---|
470 | }
|
---|
471 |
|
---|
472 | if (p->packet_type == DGRAM_PACKET) {
|
---|
473 | p->port = 138;
|
---|
474 | p->packet.dgram.header.source_ip.s_addr = local_ip->s_addr;
|
---|
475 | p->packet.dgram.header.source_port = 138;
|
---|
476 | }
|
---|
477 |
|
---|
478 | send_packet(p);
|
---|
479 | }
|
---|
480 |
|
---|
481 | /**************************************************************************** **
|
---|
482 | The main select loop.
|
---|
483 | **************************************************************************** */
|
---|
484 |
|
---|
485 | static void process(void)
|
---|
486 | {
|
---|
487 | bool run_election;
|
---|
488 |
|
---|
489 | while( True ) {
|
---|
490 | time_t t = time(NULL);
|
---|
491 | TALLOC_CTX *frame = talloc_stackframe();
|
---|
492 |
|
---|
493 | /*
|
---|
494 | * Check all broadcast subnets to see if
|
---|
495 | * we need to run an election on any of them.
|
---|
496 | * (nmbd_elections.c)
|
---|
497 | */
|
---|
498 |
|
---|
499 | run_election = check_elections();
|
---|
500 |
|
---|
501 | /*
|
---|
502 | * Read incoming UDP packets.
|
---|
503 | * (nmbd_packets.c)
|
---|
504 | */
|
---|
505 |
|
---|
506 | if(listen_for_packets(run_election)) {
|
---|
507 | TALLOC_FREE(frame);
|
---|
508 | return;
|
---|
509 | }
|
---|
510 |
|
---|
511 | /*
|
---|
512 | * Process all incoming packets
|
---|
513 | * read above. This calls the success and
|
---|
514 | * failure functions registered when response
|
---|
515 | * packets arrrive, and also deals with request
|
---|
516 | * packets from other sources.
|
---|
517 | * (nmbd_packets.c)
|
---|
518 | */
|
---|
519 |
|
---|
520 | run_packet_queue();
|
---|
521 |
|
---|
522 | /*
|
---|
523 | * Run any elections - initiate becoming
|
---|
524 | * a local master browser if we have won.
|
---|
525 | * (nmbd_elections.c)
|
---|
526 | */
|
---|
527 |
|
---|
528 | run_elections(t);
|
---|
529 |
|
---|
530 | /*
|
---|
531 | * Send out any broadcast announcements
|
---|
532 | * of our server names. This also announces
|
---|
533 | * the workgroup name if we are a local
|
---|
534 | * master browser.
|
---|
535 | * (nmbd_sendannounce.c)
|
---|
536 | */
|
---|
537 |
|
---|
538 | announce_my_server_names(t);
|
---|
539 |
|
---|
540 | /*
|
---|
541 | * Send out any LanMan broadcast announcements
|
---|
542 | * of our server names.
|
---|
543 | * (nmbd_sendannounce.c)
|
---|
544 | */
|
---|
545 |
|
---|
546 | announce_my_lm_server_names(t);
|
---|
547 |
|
---|
548 | /*
|
---|
549 | * If we are a local master browser, periodically
|
---|
550 | * announce ourselves to the domain master browser.
|
---|
551 | * This also deals with syncronising the domain master
|
---|
552 | * browser server lists with ourselves as a local
|
---|
553 | * master browser.
|
---|
554 | * (nmbd_sendannounce.c)
|
---|
555 | */
|
---|
556 |
|
---|
557 | announce_myself_to_domain_master_browser(t);
|
---|
558 |
|
---|
559 | /*
|
---|
560 | * Fullfill any remote announce requests.
|
---|
561 | * (nmbd_sendannounce.c)
|
---|
562 | */
|
---|
563 |
|
---|
564 | announce_remote(t);
|
---|
565 |
|
---|
566 | /*
|
---|
567 | * Fullfill any remote browse sync announce requests.
|
---|
568 | * (nmbd_sendannounce.c)
|
---|
569 | */
|
---|
570 |
|
---|
571 | browse_sync_remote(t);
|
---|
572 |
|
---|
573 | /*
|
---|
574 | * Scan the broadcast subnets, and WINS client
|
---|
575 | * namelists and refresh any that need refreshing.
|
---|
576 | * (nmbd_mynames.c)
|
---|
577 | */
|
---|
578 |
|
---|
579 | refresh_my_names(t);
|
---|
580 |
|
---|
581 | /*
|
---|
582 | * Scan the subnet namelists and server lists and
|
---|
583 | * expire thos that have timed out.
|
---|
584 | * (nmbd.c)
|
---|
585 | */
|
---|
586 |
|
---|
587 | expire_names_and_servers(t);
|
---|
588 |
|
---|
589 | /*
|
---|
590 | * Write out a snapshot of our current browse list into
|
---|
591 | * the browse.dat file. This is used by smbd to service
|
---|
592 | * incoming NetServerEnum calls - used to synchronise
|
---|
593 | * browse lists over subnets.
|
---|
594 | * (nmbd_serverlistdb.c)
|
---|
595 | */
|
---|
596 |
|
---|
597 | write_browse_list(t, False);
|
---|
598 |
|
---|
599 | /*
|
---|
600 | * If we are a domain master browser, we have a list of
|
---|
601 | * local master browsers we should synchronise browse
|
---|
602 | * lists with (these are added by an incoming local
|
---|
603 | * master browser announcement packet). Expire any of
|
---|
604 | * these that are no longer current, and pull the server
|
---|
605 | * lists from each of these known local master browsers.
|
---|
606 | * (nmbd_browsesync.c)
|
---|
607 | */
|
---|
608 |
|
---|
609 | dmb_expire_and_sync_browser_lists(t);
|
---|
610 |
|
---|
611 | /*
|
---|
612 | * Check that there is a local master browser for our
|
---|
613 | * workgroup for all our broadcast subnets. If one
|
---|
614 | * is not found, start an election (which we ourselves
|
---|
615 | * may or may not participate in, depending on the
|
---|
616 | * setting of the 'local master' parameter.
|
---|
617 | * (nmbd_elections.c)
|
---|
618 | */
|
---|
619 |
|
---|
620 | check_master_browser_exists(t);
|
---|
621 |
|
---|
622 | /*
|
---|
623 | * If we are configured as a logon server, attempt to
|
---|
624 | * register the special NetBIOS names to become such
|
---|
625 | * (WORKGROUP<1c> name) on all broadcast subnets and
|
---|
626 | * with the WINS server (if used). If we are configured
|
---|
627 | * to become a domain master browser, attempt to register
|
---|
628 | * the special NetBIOS name (WORKGROUP<1b> name) to
|
---|
629 | * become such.
|
---|
630 | * (nmbd_become_dmb.c)
|
---|
631 | */
|
---|
632 |
|
---|
633 | add_domain_names(t);
|
---|
634 |
|
---|
635 | /*
|
---|
636 | * If we are a WINS server, do any timer dependent
|
---|
637 | * processing required.
|
---|
638 | * (nmbd_winsserver.c)
|
---|
639 | */
|
---|
640 |
|
---|
641 | initiate_wins_processing(t);
|
---|
642 |
|
---|
643 | /*
|
---|
644 | * If we are a domain master browser, attempt to contact the
|
---|
645 | * WINS server to get a list of all known WORKGROUPS/DOMAINS.
|
---|
646 | * This will only work to a Samba WINS server.
|
---|
647 | * (nmbd_browsesync.c)
|
---|
648 | */
|
---|
649 |
|
---|
650 | if (lp_enhanced_browsing())
|
---|
651 | collect_all_workgroup_names_from_wins_server(t);
|
---|
652 |
|
---|
653 | /*
|
---|
654 | * Go through the response record queue and time out or re-transmit
|
---|
655 | * and expired entries.
|
---|
656 | * (nmbd_packets.c)
|
---|
657 | */
|
---|
658 |
|
---|
659 | retransmit_or_expire_response_records(t);
|
---|
660 |
|
---|
661 | /*
|
---|
662 | * check to see if any remote browse sync child processes have completed
|
---|
663 | */
|
---|
664 |
|
---|
665 | sync_check_completion();
|
---|
666 |
|
---|
667 | /*
|
---|
668 | * regularly sync with any other DMBs we know about
|
---|
669 | */
|
---|
670 |
|
---|
671 | if (lp_enhanced_browsing())
|
---|
672 | sync_all_dmbs(t);
|
---|
673 |
|
---|
674 | /* check for new network interfaces */
|
---|
675 |
|
---|
676 | reload_interfaces(t);
|
---|
677 |
|
---|
678 | /* free up temp memory */
|
---|
679 | TALLOC_FREE(frame);
|
---|
680 | }
|
---|
681 | }
|
---|
682 |
|
---|
683 | /**************************************************************************** **
|
---|
684 | Open the socket communication.
|
---|
685 | **************************************************************************** */
|
---|
686 |
|
---|
687 | static bool open_sockets(bool isdaemon, int port)
|
---|
688 | {
|
---|
689 | struct sockaddr_storage ss;
|
---|
690 | const char *sock_addr = lp_socket_address();
|
---|
691 |
|
---|
692 | /*
|
---|
693 | * The sockets opened here will be used to receive broadcast
|
---|
694 | * packets *only*. Interface specific sockets are opened in
|
---|
695 | * make_subnet() in namedbsubnet.c. Thus we bind to the
|
---|
696 | * address "0.0.0.0". The parameter 'socket address' is
|
---|
697 | * now deprecated.
|
---|
698 | */
|
---|
699 |
|
---|
700 | if (!interpret_string_addr(&ss, sock_addr,
|
---|
701 | AI_NUMERICHOST|AI_PASSIVE)) {
|
---|
702 | DEBUG(0,("open_sockets: unable to get socket address "
|
---|
703 | "from string %s", sock_addr));
|
---|
704 | return false;
|
---|
705 | }
|
---|
706 | if (ss.ss_family != AF_INET) {
|
---|
707 | DEBUG(0,("open_sockets: unable to use IPv6 socket"
|
---|
708 | "%s in nmbd\n",
|
---|
709 | sock_addr));
|
---|
710 | return false;
|
---|
711 | }
|
---|
712 |
|
---|
713 | if (isdaemon) {
|
---|
714 | ClientNMB = open_socket_in(SOCK_DGRAM, port,
|
---|
715 | 0, &ss,
|
---|
716 | true);
|
---|
717 | } else {
|
---|
718 | ClientNMB = 0;
|
---|
719 | }
|
---|
720 |
|
---|
721 | if (ClientNMB == -1) {
|
---|
722 | return false;
|
---|
723 | }
|
---|
724 |
|
---|
725 | ClientDGRAM = open_socket_in(SOCK_DGRAM, DGRAM_PORT,
|
---|
726 | 3, &ss,
|
---|
727 | true);
|
---|
728 |
|
---|
729 | if (ClientDGRAM == -1) {
|
---|
730 | if (ClientNMB != 0) {
|
---|
731 | close(ClientNMB);
|
---|
732 | }
|
---|
733 | return false;
|
---|
734 | }
|
---|
735 |
|
---|
736 | /* we are never interested in SIGPIPE */
|
---|
737 | BlockSignals(True,SIGPIPE);
|
---|
738 |
|
---|
739 | set_socket_options( ClientNMB, "SO_BROADCAST" );
|
---|
740 | set_socket_options( ClientDGRAM, "SO_BROADCAST" );
|
---|
741 |
|
---|
742 | /* Ensure we're non-blocking. */
|
---|
743 | set_blocking( ClientNMB, False);
|
---|
744 | set_blocking( ClientDGRAM, False);
|
---|
745 |
|
---|
746 | DEBUG( 3, ( "open_sockets: Broadcast sockets opened.\n" ) );
|
---|
747 | return( True );
|
---|
748 | }
|
---|
749 |
|
---|
750 | /**************************************************************************** **
|
---|
751 | main program
|
---|
752 | **************************************************************************** */
|
---|
753 |
|
---|
754 | int main(int argc, const char *argv[])
|
---|
755 | {
|
---|
756 | static bool is_daemon;
|
---|
757 | static bool opt_interactive;
|
---|
758 | static bool Fork = true;
|
---|
759 | static bool no_process_group;
|
---|
760 | static bool log_stdout;
|
---|
761 | poptContext pc;
|
---|
762 | char *p_lmhosts = NULL;
|
---|
763 | int opt;
|
---|
764 | enum {
|
---|
765 | OPT_DAEMON = 1000,
|
---|
766 | OPT_INTERACTIVE,
|
---|
767 | OPT_FORK,
|
---|
768 | OPT_NO_PROCESS_GROUP,
|
---|
769 | OPT_LOG_STDOUT
|
---|
770 | };
|
---|
771 | struct poptOption long_options[] = {
|
---|
772 | POPT_AUTOHELP
|
---|
773 | {"daemon", 'D', POPT_ARG_NONE, NULL, OPT_DAEMON, "Become a daemon(default)" },
|
---|
774 | {"interactive", 'i', POPT_ARG_NONE, NULL, OPT_INTERACTIVE, "Run interactive (not a daemon)" },
|
---|
775 | {"foreground", 'F', POPT_ARG_NONE, NULL, OPT_FORK, "Run daemon in foreground (for daemontools & etc)" },
|
---|
776 | {"no-process-group", 0, POPT_ARG_NONE, NULL, OPT_NO_PROCESS_GROUP, "Don't create a new process group" },
|
---|
777 | {"log-stdout", 'S', POPT_ARG_NONE, NULL, OPT_LOG_STDOUT, "Log to stdout" },
|
---|
778 | {"hosts", 'H', POPT_ARG_STRING, &p_lmhosts, 0, "Load a netbios hosts file"},
|
---|
779 | {"port", 'p', POPT_ARG_INT, &global_nmb_port, 0, "Listen on the specified port" },
|
---|
780 | POPT_COMMON_SAMBA
|
---|
781 | { NULL }
|
---|
782 | };
|
---|
783 | TALLOC_CTX *frame;
|
---|
784 | NTSTATUS status;
|
---|
785 |
|
---|
786 | /*
|
---|
787 | * Do this before any other talloc operation
|
---|
788 | */
|
---|
789 | talloc_enable_null_tracking();
|
---|
790 | frame = talloc_stackframe();
|
---|
791 |
|
---|
792 | #ifdef __OS2__
|
---|
793 | global_Sem32Add = 1;
|
---|
794 | #endif
|
---|
795 |
|
---|
796 | load_case_tables();
|
---|
797 |
|
---|
798 | global_nmb_port = NMB_PORT;
|
---|
799 |
|
---|
800 | pc = poptGetContext("nmbd", argc, argv, long_options, 0);
|
---|
801 | while ((opt = poptGetNextOpt(pc)) != -1) {
|
---|
802 | switch (opt) {
|
---|
803 | case OPT_DAEMON:
|
---|
804 | is_daemon = true;
|
---|
805 | break;
|
---|
806 | case OPT_INTERACTIVE:
|
---|
807 | opt_interactive = true;
|
---|
808 | break;
|
---|
809 | case OPT_FORK:
|
---|
810 | Fork = false;
|
---|
811 | break;
|
---|
812 | case OPT_NO_PROCESS_GROUP:
|
---|
813 | no_process_group = true;
|
---|
814 | break;
|
---|
815 | case OPT_LOG_STDOUT:
|
---|
816 | log_stdout = true;
|
---|
817 | break;
|
---|
818 | default:
|
---|
819 | d_fprintf(stderr, "\nInvalid option %s: %s\n\n",
|
---|
820 | poptBadOption(pc, 0), poptStrerror(opt));
|
---|
821 | poptPrintUsage(pc, stderr, 0);
|
---|
822 | exit(1);
|
---|
823 | }
|
---|
824 | };
|
---|
825 | poptFreeContext(pc);
|
---|
826 |
|
---|
827 | global_in_nmbd = true;
|
---|
828 |
|
---|
829 | StartupTime = time(NULL);
|
---|
830 |
|
---|
831 | sys_srandom(time(NULL) ^ sys_getpid());
|
---|
832 |
|
---|
833 | if (!override_logfile) {
|
---|
834 | char *lfile = NULL;
|
---|
835 | if (asprintf(&lfile, "%s/log.nmbd", get_dyn_LOGFILEBASE()) < 0) {
|
---|
836 | exit(1);
|
---|
837 | }
|
---|
838 | lp_set_logfile(lfile);
|
---|
839 | SAFE_FREE(lfile);
|
---|
840 | }
|
---|
841 |
|
---|
842 | fault_setup((void (*)(void *))fault_continue );
|
---|
843 | dump_core_setup("nmbd");
|
---|
844 |
|
---|
845 | /* POSIX demands that signals are inherited. If the invoking process has
|
---|
846 | * these signals masked, we will have problems, as we won't receive them. */
|
---|
847 | BlockSignals(False, SIGHUP);
|
---|
848 | BlockSignals(False, SIGUSR1);
|
---|
849 | BlockSignals(False, SIGTERM);
|
---|
850 |
|
---|
851 | #if defined(SIGFPE)
|
---|
852 | /* we are never interested in SIGFPE */
|
---|
853 | BlockSignals(True,SIGFPE);
|
---|
854 | #endif
|
---|
855 |
|
---|
856 | /* We no longer use USR2... */
|
---|
857 | #if defined(SIGUSR2)
|
---|
858 | BlockSignals(True, SIGUSR2);
|
---|
859 | #endif
|
---|
860 |
|
---|
861 | if ( opt_interactive ) {
|
---|
862 | Fork = False;
|
---|
863 | log_stdout = True;
|
---|
864 | }
|
---|
865 |
|
---|
866 | if ( log_stdout && Fork ) {
|
---|
867 | DEBUG(0,("ERROR: Can't log to stdout (-S) unless daemon is in foreground (-F) or interactive (-i)\n"));
|
---|
868 | exit(1);
|
---|
869 | }
|
---|
870 | if (log_stdout) {
|
---|
871 | setup_logging( argv[0], DEBUG_STDOUT);
|
---|
872 | } else {
|
---|
873 | setup_logging( argv[0], DEBUG_FILE);
|
---|
874 | }
|
---|
875 |
|
---|
876 | reopen_logs();
|
---|
877 |
|
---|
878 | DEBUG(0,("nmbd version %s started.\n", samba_version_string()));
|
---|
879 | DEBUGADD(0,("%s\n", COPYRIGHT_STARTUP_MESSAGE));
|
---|
880 |
|
---|
881 | if (!lp_load_initial_only(get_dyn_CONFIGFILE())) {
|
---|
882 | DEBUG(0, ("error opening config file\n"));
|
---|
883 | exit(1);
|
---|
884 | }
|
---|
885 |
|
---|
886 | if (nmbd_messaging_context() == NULL) {
|
---|
887 | return 1;
|
---|
888 | }
|
---|
889 |
|
---|
890 | if ( !reload_nmbd_services(False) )
|
---|
891 | return(-1);
|
---|
892 |
|
---|
893 | if(!init_names())
|
---|
894 | return -1;
|
---|
895 |
|
---|
896 | reload_nmbd_services( True );
|
---|
897 |
|
---|
898 | if (strequal(lp_workgroup(),"*")) {
|
---|
899 | DEBUG(0,("ERROR: a workgroup name of * is no longer supported\n"));
|
---|
900 | exit(1);
|
---|
901 | }
|
---|
902 |
|
---|
903 | set_samba_nb_type();
|
---|
904 |
|
---|
905 | if (!is_daemon && !is_a_socket(0)) {
|
---|
906 | DEBUG(0,("standard input is not a socket, assuming -D option\n"));
|
---|
907 | is_daemon = True;
|
---|
908 | }
|
---|
909 |
|
---|
910 | if (is_daemon && !opt_interactive) {
|
---|
911 | DEBUG( 2, ( "Becoming a daemon.\n" ) );
|
---|
912 | become_daemon(Fork, no_process_group, log_stdout);
|
---|
913 | }
|
---|
914 |
|
---|
915 | #if HAVE_SETPGID
|
---|
916 | /*
|
---|
917 | * If we're interactive we want to set our own process group for
|
---|
918 | * signal management.
|
---|
919 | */
|
---|
920 | if (opt_interactive && !no_process_group)
|
---|
921 | setpgid( (pid_t)0, (pid_t)0 );
|
---|
922 | #endif
|
---|
923 |
|
---|
924 | if (nmbd_messaging_context() == NULL) {
|
---|
925 | return 1;
|
---|
926 | }
|
---|
927 |
|
---|
928 | #ifndef SYNC_DNS
|
---|
929 | /* Setup the async dns. We do it here so it doesn't have all the other
|
---|
930 | stuff initialised and thus chewing memory and sockets */
|
---|
931 | if(lp_we_are_a_wins_server() && lp_dns_proxy()) {
|
---|
932 | start_async_dns();
|
---|
933 | }
|
---|
934 | #endif
|
---|
935 |
|
---|
936 | if (!directory_exist(lp_lockdir())) {
|
---|
937 | mkdir(lp_lockdir(), 0755);
|
---|
938 | }
|
---|
939 |
|
---|
940 | pidfile_create("nmbd");
|
---|
941 |
|
---|
942 | status = reinit_after_fork(nmbd_messaging_context(),
|
---|
943 | nmbd_event_context(),
|
---|
944 | procid_self(), false);
|
---|
945 |
|
---|
946 | if (!NT_STATUS_IS_OK(status)) {
|
---|
947 | DEBUG(0,("reinit_after_fork() failed\n"));
|
---|
948 | exit(1);
|
---|
949 | }
|
---|
950 |
|
---|
951 | if (!nmbd_setup_sig_term_handler())
|
---|
952 | exit(1);
|
---|
953 | if (!nmbd_setup_sig_hup_handler())
|
---|
954 | exit(1);
|
---|
955 |
|
---|
956 | /* get broadcast messages */
|
---|
957 |
|
---|
958 | if (!serverid_register(procid_self(),
|
---|
959 | FLAG_MSG_GENERAL|FLAG_MSG_DBWRAP)) {
|
---|
960 | DEBUG(1, ("Could not register myself in serverid.tdb\n"));
|
---|
961 | exit(1);
|
---|
962 | }
|
---|
963 |
|
---|
964 | messaging_register(nmbd_messaging_context(), NULL,
|
---|
965 | MSG_FORCE_ELECTION, nmbd_message_election);
|
---|
966 | #if 0
|
---|
967 | /* Until winsrepl is done. */
|
---|
968 | messaging_register(nmbd_messaging_context(), NULL,
|
---|
969 | MSG_WINS_NEW_ENTRY, nmbd_wins_new_entry);
|
---|
970 | #endif
|
---|
971 | messaging_register(nmbd_messaging_context(), NULL,
|
---|
972 | MSG_SHUTDOWN, nmbd_terminate);
|
---|
973 | messaging_register(nmbd_messaging_context(), NULL,
|
---|
974 | MSG_SMB_CONF_UPDATED, msg_reload_nmbd_services);
|
---|
975 | messaging_register(nmbd_messaging_context(), NULL,
|
---|
976 | MSG_SEND_PACKET, msg_nmbd_send_packet);
|
---|
977 |
|
---|
978 | TimeInit();
|
---|
979 |
|
---|
980 | DEBUG( 3, ( "Opening sockets %d\n", global_nmb_port ) );
|
---|
981 |
|
---|
982 | if ( !open_sockets( is_daemon, global_nmb_port ) ) {
|
---|
983 | kill_async_dns_child();
|
---|
984 | return 1;
|
---|
985 | }
|
---|
986 |
|
---|
987 | /* Determine all the IP addresses we have. */
|
---|
988 | load_interfaces();
|
---|
989 |
|
---|
990 | /* Create an nmbd subnet record for each of the above. */
|
---|
991 | if( False == create_subnets() ) {
|
---|
992 | DEBUG(0,("ERROR: Failed when creating subnet lists. Exiting.\n"));
|
---|
993 | kill_async_dns_child();
|
---|
994 | exit(1);
|
---|
995 | }
|
---|
996 |
|
---|
997 | /* Load in any static local names. */
|
---|
998 | if (p_lmhosts) {
|
---|
999 | set_dyn_LMHOSTSFILE(p_lmhosts);
|
---|
1000 | }
|
---|
1001 | load_lmhosts_file(get_dyn_LMHOSTSFILE());
|
---|
1002 | DEBUG(3,("Loaded hosts file %s\n", get_dyn_LMHOSTSFILE()));
|
---|
1003 |
|
---|
1004 | /* If we are acting as a WINS server, initialise data structures. */
|
---|
1005 | if( !initialise_wins() ) {
|
---|
1006 | DEBUG( 0, ( "nmbd: Failed when initialising WINS server.\n" ) );
|
---|
1007 | kill_async_dns_child();
|
---|
1008 | exit(1);
|
---|
1009 | }
|
---|
1010 |
|
---|
1011 | /*
|
---|
1012 | * Register nmbd primary workgroup and nmbd names on all
|
---|
1013 | * the broadcast subnets, and on the WINS server (if specified).
|
---|
1014 | * Also initiate the startup of our primary workgroup (start
|
---|
1015 | * elections if we are setup as being able to be a local
|
---|
1016 | * master browser.
|
---|
1017 | */
|
---|
1018 |
|
---|
1019 | if( False == register_my_workgroup_and_names() ) {
|
---|
1020 | DEBUG(0,("ERROR: Failed when creating my my workgroup. Exiting.\n"));
|
---|
1021 | kill_async_dns_child();
|
---|
1022 | exit(1);
|
---|
1023 | }
|
---|
1024 |
|
---|
1025 | if (!initialize_nmbd_proxy_logon()) {
|
---|
1026 | DEBUG(0,("ERROR: Failed setup nmbd_proxy_logon.\n"));
|
---|
1027 | kill_async_dns_child();
|
---|
1028 | exit(1);
|
---|
1029 | }
|
---|
1030 |
|
---|
1031 | if (!nmbd_init_packet_server()) {
|
---|
1032 | kill_async_dns_child();
|
---|
1033 | exit(1);
|
---|
1034 | }
|
---|
1035 |
|
---|
1036 | TALLOC_FREE(frame);
|
---|
1037 | process();
|
---|
1038 |
|
---|
1039 | kill_async_dns_child();
|
---|
1040 | return(0);
|
---|
1041 | }
|
---|