smbd: fix breaking leases on rename
[Samba.git] / source4 / nbt_server / dgram / request.c
blob76145147ad0cefcbf39380c7d263940adb27a840
1 /*
2 Unix SMB/CIFS implementation.
4 NBT datagram server
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 3 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, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "nbt_server/nbt_server.h"
24 #include "samba/service_task.h"
25 #include "lib/socket/socket.h"
26 #include "libcli/resolve/resolve.h"
27 #include "nbt_server/dgram/proto.h"
28 #include "librpc/gen_ndr/ndr_nbt.h"
29 #include "param/param.h"
30 #include "lib/util/util_str_escape.h"
31 #include "lib/util/util_net.h"
32 #include "../source3/include/fstring.h"
33 #include "../source3/libsmb/nmblib.h"
34 #include "../source3/libsmb/unexpected.h"
37 a list of mailslots that we have static handlers for
39 static const struct {
40 const char *mailslot_name;
41 dgram_mailslot_handler_t handler;
42 } mailslot_handlers[] = {
43 /* Handle both NTLOGON and NETLOGON in the same function, as
44 * they are very similar */
45 { NBT_MAILSLOT_NETLOGON, nbtd_mailslot_netlogon_handler },
46 { NBT_MAILSLOT_NTLOGON, nbtd_mailslot_netlogon_handler },
47 { NBT_MAILSLOT_BROWSE, nbtd_mailslot_browse_handler }
51 receive an incoming dgram request. This is used for general datagram
52 requests. Mailslot requests for our listening mailslots
53 are handled in the specific mailslot handlers
55 void dgram_request_handler(struct nbt_dgram_socket *dgmsock,
56 struct nbt_dgram_packet *packet,
57 struct socket_address *src)
59 struct nbtd_interface *iface =
60 talloc_get_type_abort(dgmsock->incoming.private_data,
61 struct nbtd_interface);
62 struct nbtd_server *nbtsrv = iface->nbtsrv;
63 const char *mailslot_name = NULL;
64 struct packet_struct *pstruct = NULL;
65 DATA_BLOB blob = { .length = 0, };
66 enum ndr_err_code ndr_err;
68 mailslot_name = dgram_mailslot_name(packet);
69 if (mailslot_name != NULL) {
70 DBG_DEBUG("Unexpected mailslot[%s] datagram request from %s:%d\n",
71 log_escape(packet, mailslot_name),
72 src->addr, src->port);
73 } else {
74 DBG_DEBUG("Unexpected general datagram request from %s:%d\n",
75 src->addr, src->port);
78 if (CHECK_DEBUGLVL(DBGLVL_DEBUG)) {
79 NDR_PRINT_DEBUG(nbt_dgram_packet, packet);
83 * For now we only pass DGRAM_DIRECT_UNIQUE
84 * messages via nb_packet_dispatch() to
85 * nbtsrv->unexpected_server
87 if (packet->msg_type != DGRAM_DIRECT_UNIQUE) {
88 return;
91 ndr_err = ndr_push_struct_blob(&blob, packet, packet,
92 (ndr_push_flags_fn_t)ndr_push_nbt_dgram_packet);
93 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
94 DBG_ERR("ndr_push_nbt_dgram_packet - %s\n",
95 ndr_errstr(ndr_err));
96 return;
99 pstruct = parse_packet((char *)blob.data,
100 blob.length,
101 DGRAM_PACKET,
102 interpret_addr2(src->addr),
103 src->port);
104 if (pstruct != NULL) {
105 nb_packet_dispatch(nbtsrv->unexpected_server, pstruct);
106 free_packet(pstruct);
112 setup the port 138 datagram listener for a given interface
114 NTSTATUS nbtd_dgram_setup(struct nbtd_interface *iface, const char *bind_address)
116 struct nbt_dgram_socket *bcast_dgmsock = NULL;
117 struct nbtd_server *nbtsrv = iface->nbtsrv;
118 struct socket_address *bcast_addr, *bind_addr;
119 NTSTATUS status;
120 TALLOC_CTX *tmp_ctx = talloc_new(iface);
121 /* the list of mailslots that we are interested in */
122 size_t i;
124 if (!tmp_ctx) {
125 return NT_STATUS_NO_MEMORY;
128 if (strcmp("0.0.0.0", iface->netmask) != 0) {
129 /* listen for broadcasts on port 138 */
130 bcast_dgmsock = nbt_dgram_socket_init(iface, nbtsrv->task->event_ctx);
131 if (!bcast_dgmsock) {
132 talloc_free(tmp_ctx);
133 return NT_STATUS_NO_MEMORY;
136 bcast_addr = socket_address_from_strings(tmp_ctx, bcast_dgmsock->sock->backend_name,
137 iface->bcast_address,
138 lpcfg_dgram_port(iface->nbtsrv->task->lp_ctx));
139 if (!bcast_addr) {
140 talloc_free(tmp_ctx);
141 return NT_STATUS_NO_MEMORY;
144 status = socket_listen(bcast_dgmsock->sock, bcast_addr, 0, 0);
145 if (!NT_STATUS_IS_OK(status)) {
146 talloc_free(tmp_ctx);
147 DEBUG(0,("Failed to bind to %s:%d - %s\n",
148 iface->bcast_address, lpcfg_dgram_port(iface->nbtsrv->task->lp_ctx),
149 nt_errstr(status)));
150 return status;
153 dgram_set_incoming_handler(bcast_dgmsock, dgram_request_handler, iface);
156 /* listen for unicasts on port 138 */
157 iface->dgmsock = nbt_dgram_socket_init(iface, nbtsrv->task->event_ctx);
158 if (!iface->dgmsock) {
159 talloc_free(tmp_ctx);
160 return NT_STATUS_NO_MEMORY;
163 bind_addr = socket_address_from_strings(tmp_ctx, iface->dgmsock->sock->backend_name,
164 bind_address, lpcfg_dgram_port(iface->nbtsrv->task->lp_ctx));
165 if (!bind_addr) {
166 talloc_free(tmp_ctx);
167 return NT_STATUS_NO_MEMORY;
170 status = socket_listen(iface->dgmsock->sock, bind_addr, 0, 0);
171 if (!NT_STATUS_IS_OK(status)) {
172 talloc_free(tmp_ctx);
173 DEBUG(0,("Failed to bind to %s:%d - %s\n",
174 bind_address, lpcfg_dgram_port(iface->nbtsrv->task->lp_ctx), nt_errstr(status)));
175 return status;
178 dgram_set_incoming_handler(iface->dgmsock, dgram_request_handler, iface);
180 talloc_free(tmp_ctx);
182 for (i=0;i<ARRAY_SIZE(mailslot_handlers);i++) {
183 /* note that we don't need to keep the pointer
184 to the dgmslot around - the callback is all
185 we need */
186 struct dgram_mailslot_handler *dgmslot;
188 if (bcast_dgmsock) {
189 dgmslot = dgram_mailslot_listen(bcast_dgmsock,
190 mailslot_handlers[i].mailslot_name,
191 mailslot_handlers[i].handler, iface);
192 NT_STATUS_HAVE_NO_MEMORY(dgmslot);
195 dgmslot = dgram_mailslot_listen(iface->dgmsock,
196 mailslot_handlers[i].mailslot_name,
197 mailslot_handlers[i].handler, iface);
198 NT_STATUS_HAVE_NO_MEMORY(dgmslot);
201 return NT_STATUS_OK;