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