2 Unix SMB/CIFS implementation.
6 Copyright (C) Andrew Tridgell 2005
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 2 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "dlinklist.h"
25 #include "nbt_server/nbt_server.h"
26 #include "smbd/service_task.h"
27 #include "lib/socket/socket.h"
28 #include "nbt_server/wins/winsserver.h"
32 receive an incoming request and dispatch it to the right place
34 static void nbtd_request_handler(struct nbt_name_socket *nbtsock,
35 struct nbt_name_packet *packet,
36 struct socket_address *src)
38 struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private,
39 struct nbtd_interface);
40 struct nbtd_server *nbtsrv = iface->nbtsrv;
42 nbtsrv->stats.total_received++;
44 /* see if its from one of our own interfaces - if so, then ignore it */
45 if (nbtd_self_packet_and_bcast(nbtsock, packet, src)) {
46 DEBUG(10,("Ignoring bcast self packet from %s:%d\n", src->addr, src->port));
50 switch (packet->operation & NBT_OPCODE) {
51 case NBT_OPCODE_QUERY:
52 nbtsrv->stats.query_count++;
53 nbtd_request_query(nbtsock, packet, src);
56 case NBT_OPCODE_REGISTER:
57 case NBT_OPCODE_REFRESH:
58 case NBT_OPCODE_REFRESH2:
59 nbtsrv->stats.register_count++;
60 nbtd_request_defense(nbtsock, packet, src);
63 case NBT_OPCODE_RELEASE:
64 case NBT_OPCODE_MULTI_HOME_REG:
65 nbtsrv->stats.release_count++;
66 nbtd_winsserver_request(nbtsock, packet, src);
70 nbtd_bad_packet(packet, src, "Unexpected opcode");
77 find a registered name on an interface
79 struct nbtd_iface_name *nbtd_find_iname(struct nbtd_interface *iface,
80 struct nbt_name *name,
83 struct nbtd_iface_name *iname;
84 for (iname=iface->names;iname;iname=iname->next) {
85 if (iname->name.type == name->type &&
86 strcmp(name->name, iname->name.name) == 0 &&
87 ((iname->nb_flags & nb_flags) == nb_flags)) {
95 start listening on the given address
97 static NTSTATUS nbtd_add_socket(struct nbtd_server *nbtsrv,
98 const char *bind_address,
103 struct nbtd_interface *iface;
105 struct socket_address *bcast_address;
106 struct socket_address *unicast_address;
109 we actually create two sockets. One listens on the broadcast address
110 for the interface, and the other listens on our specific address. This
111 allows us to run with "bind interfaces only" while still receiving
112 broadcast addresses, and also simplifies matching incoming requests
116 iface = talloc(nbtsrv, struct nbtd_interface);
117 NT_STATUS_HAVE_NO_MEMORY(iface);
119 iface->nbtsrv = nbtsrv;
120 iface->bcast_address = talloc_steal(iface, bcast);
121 iface->ip_address = talloc_steal(iface, address);
122 iface->netmask = talloc_steal(iface, netmask);
125 if (strcmp(netmask, "0.0.0.0") != 0) {
126 struct nbt_name_socket *bcast_nbtsock;
128 /* listen for broadcasts on port 137 */
129 bcast_nbtsock = nbt_name_socket_init(iface, nbtsrv->task->event_ctx);
130 if (!bcast_nbtsock) {
132 return NT_STATUS_NO_MEMORY;
135 bcast_address = socket_address_from_strings(bcast_nbtsock, bcast_nbtsock->sock->backend_name,
136 bcast, lp_nbt_port());
137 if (!bcast_address) {
139 return NT_STATUS_NO_MEMORY;
142 status = socket_listen(bcast_nbtsock->sock, bcast_address, 0, 0);
143 if (!NT_STATUS_IS_OK(status)) {
144 DEBUG(0,("Failed to bind to %s:%d - %s\n",
145 bcast, lp_nbt_port(), nt_errstr(status)));
149 talloc_free(bcast_address);
151 nbt_set_incoming_handler(bcast_nbtsock, nbtd_request_handler, iface);
154 /* listen for unicasts on port 137 */
155 iface->nbtsock = nbt_name_socket_init(iface, nbtsrv->task->event_ctx);
156 if (!iface->nbtsock) {
158 return NT_STATUS_NO_MEMORY;
161 unicast_address = socket_address_from_strings(iface->nbtsock, iface->nbtsock->sock->backend_name,
162 bind_address, lp_nbt_port());
164 status = socket_listen(iface->nbtsock->sock, unicast_address, 0, 0);
165 if (!NT_STATUS_IS_OK(status)) {
166 DEBUG(0,("Failed to bind to %s:%d - %s\n",
167 bind_address, lp_nbt_port(), nt_errstr(status)));
171 talloc_free(unicast_address);
173 nbt_set_incoming_handler(iface->nbtsock, nbtd_request_handler, iface);
175 /* also setup the datagram listeners */
176 status = nbtd_dgram_setup(iface, bind_address);
177 if (!NT_STATUS_IS_OK(status)) {
178 DEBUG(0,("Failed to setup dgram listen on %s - %s\n",
179 bind_address, nt_errstr(status)));
184 if (strcmp(netmask, "0.0.0.0") == 0) {
185 DLIST_ADD(nbtsrv->bcast_interface, iface);
187 DLIST_ADD(nbtsrv->interfaces, iface);
195 setup a socket for talking to our WINS servers
197 static NTSTATUS nbtd_add_wins_socket(struct nbtd_server *nbtsrv)
199 struct nbtd_interface *iface;
201 iface = talloc_zero(nbtsrv, struct nbtd_interface);
202 NT_STATUS_HAVE_NO_MEMORY(iface);
204 iface->nbtsrv = nbtsrv;
206 DLIST_ADD(nbtsrv->wins_interface, iface);
213 setup our listening sockets on the configured network interfaces
215 NTSTATUS nbtd_startup_interfaces(struct nbtd_server *nbtsrv)
217 int num_interfaces = iface_count();
219 TALLOC_CTX *tmp_ctx = talloc_new(nbtsrv);
222 /* if we are allowing incoming packets from any address, then
223 we also need to bind to the wildcard address */
224 if (!lp_bind_interfaces_only()) {
225 const char *primary_address;
227 /* the primary address is the address we will return
228 for non-WINS queries not made on a specific
230 if (num_interfaces > 0) {
231 primary_address = iface_n_ip(0);
233 primary_address = sys_inet_ntoa(interpret_addr2(
236 primary_address = talloc_strdup(tmp_ctx, primary_address);
237 NT_STATUS_HAVE_NO_MEMORY(primary_address);
239 status = nbtd_add_socket(nbtsrv,
242 talloc_strdup(tmp_ctx, "255.255.255.255"),
243 talloc_strdup(tmp_ctx, "0.0.0.0"));
244 NT_STATUS_NOT_OK_RETURN(status);
247 for (i=0; i<num_interfaces; i++) {
248 const char *address = talloc_strdup(tmp_ctx, iface_n_ip(i));
249 const char *bcast = talloc_strdup(tmp_ctx, iface_n_bcast(i));
250 const char *netmask = talloc_strdup(tmp_ctx, iface_n_netmask(i));
252 status = nbtd_add_socket(nbtsrv, address, address, bcast, netmask);
253 NT_STATUS_NOT_OK_RETURN(status);
256 if (lp_wins_server_list()) {
257 status = nbtd_add_wins_socket(nbtsrv);
258 NT_STATUS_NOT_OK_RETURN(status);
261 talloc_free(tmp_ctx);
268 form a list of addresses that we should use in name query replies
269 we always place the IP in the given interface first
271 const char **nbtd_address_list(struct nbtd_interface *iface, TALLOC_CTX *mem_ctx)
273 struct nbtd_server *nbtsrv = iface->nbtsrv;
274 const char **ret = NULL;
275 struct nbtd_interface *iface2;
276 BOOL is_loopback = False;
278 if (iface->ip_address) {
279 is_loopback = iface_same_net(iface->ip_address, "127.0.0.1", "255.0.0.0");
280 ret = str_list_add(ret, iface->ip_address);
283 for (iface2=nbtsrv->interfaces;iface2;iface2=iface2->next) {
284 if (iface2 == iface) continue;
286 if (!iface2->ip_address) continue;
289 if (iface_same_net(iface2->ip_address, "127.0.0.1", "255.0.0.0")) {
294 ret = str_list_add(ret, iface2->ip_address);
297 talloc_steal(mem_ctx, ret);
304 find the interface to use for sending a outgoing request
306 struct nbtd_interface *nbtd_find_interface(struct nbtd_server *nbtd_server,
309 struct nbtd_interface *iface;
310 /* try to find a exact match */
311 for (iface=nbtd_server->interfaces;iface;iface=iface->next) {
312 if (iface_same_net(address, iface->ip_address, iface->netmask)) {
317 /* no exact match, if we have the broadcast interface, use that */
318 if (nbtd_server->bcast_interface) {
319 return nbtd_server->bcast_interface;
322 /* fallback to first interface */
323 return nbtd_server->interfaces;