abb4593b2cb76dfebbbf4dd4a6b5743e6a92598f
[samba.git] / source4 / nbt_server / nbt_server.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    NBT server task
5
6    Copyright (C) Andrew Tridgell        2005
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 "smbd/service_task.h"
24 #include "smbd/service.h"
25 #include "nbt_server/nbt_server.h"
26 #include "nbt_server/wins/winsserver.h"
27 #include "system/network.h"
28 #include "lib/socket/netif.h"
29 #include "auth/auth.h"
30 #include "dsdb/samdb/samdb.h"
31
32 /*
33   startup the nbtd task
34 */
35 static void nbtd_task_init(struct task_server *task)
36 {
37         struct nbtd_server *nbtsrv;
38         NTSTATUS status;
39
40         if (iface_count() == 0) {
41                 task_server_terminate(task, "nbtd: no network interfaces configured");
42                 return;
43         }
44
45         task_server_set_title(task, "task[nbtd]");
46
47         nbtsrv = talloc(task, struct nbtd_server);
48         if (nbtsrv == NULL) {
49                 task_server_terminate(task, "nbtd: out of memory");
50                 return;
51         }
52
53         nbtsrv->task            = task;
54         nbtsrv->interfaces      = NULL;
55         nbtsrv->bcast_interface = NULL;
56         nbtsrv->wins_interface  = NULL;
57
58         /* start listening on the configured network interfaces */
59         status = nbtd_startup_interfaces(nbtsrv);
60         if (!NT_STATUS_IS_OK(status)) {
61                 task_server_terminate(task, "nbtd failed to setup interfaces");
62                 return;
63         }
64
65         nbtsrv->sam_ctx = samdb_connect(nbtsrv, anonymous_session(nbtsrv));
66         if (nbtsrv->sam_ctx == NULL) {
67                 task_server_terminate(task, "nbtd failed to open samdb");
68                 return;
69         }
70
71         /* start the WINS server, if appropriate */
72         status = nbtd_winsserver_init(nbtsrv);
73         if (!NT_STATUS_IS_OK(status)) {
74                 task_server_terminate(task, "nbtd failed to start WINS server");
75                 return;
76         }
77
78         nbtd_register_irpc(nbtsrv);
79
80         /* start the process of registering our names on all interfaces */
81         nbtd_register_names(nbtsrv);
82
83         irpc_add_name(task->msg_ctx, "nbt_server");
84
85
86 }
87
88
89 /*
90   initialise the nbt server
91  */
92 static NTSTATUS nbtd_init(struct event_context *event_ctx, const struct model_ops *model_ops)
93 {
94         return task_server_startup(event_ctx, model_ops, nbtd_task_init);
95 }
96
97
98 /*
99   register ourselves as a available server
100 */
101 NTSTATUS server_service_nbtd_init(void)
102 {
103         return register_server_service("nbt", nbtd_init);
104 }