Line data Source code
1 : /*
2 : Unix SMB/CIFS implementation.
3 :
4 : NBT datagram server
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 "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"
35 :
36 : /*
37 : a list of mailslots that we have static handlers for
38 : */
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 }
48 : };
49 :
50 : /*
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
54 : */
55 15 : void dgram_request_handler(struct nbt_dgram_socket *dgmsock,
56 : struct nbt_dgram_packet *packet,
57 : struct socket_address *src)
58 : {
59 0 : struct nbtd_interface *iface =
60 15 : talloc_get_type_abort(dgmsock->incoming.private_data,
61 : struct nbtd_interface);
62 15 : struct nbtd_server *nbtsrv = iface->nbtsrv;
63 15 : const char *mailslot_name = NULL;
64 15 : struct packet_struct *pstruct = NULL;
65 15 : DATA_BLOB blob = { .length = 0, };
66 0 : enum ndr_err_code ndr_err;
67 :
68 15 : mailslot_name = dgram_mailslot_name(packet);
69 15 : if (mailslot_name != NULL) {
70 15 : 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 0 : DBG_DEBUG("Unexpected general datagram request from %s:%d\n",
75 : src->addr, src->port);
76 : }
77 :
78 15 : if (CHECK_DEBUGLVL(DBGLVL_DEBUG)) {
79 0 : NDR_PRINT_DEBUG(nbt_dgram_packet, packet);
80 : }
81 :
82 : /*
83 : * For now we only pass DGRAM_DIRECT_UNIQUE
84 : * messages via nb_packet_dispatch() to
85 : * nbtsrv->unexpected_server
86 : */
87 15 : if (packet->msg_type != DGRAM_DIRECT_UNIQUE) {
88 0 : return;
89 : }
90 :
91 15 : ndr_err = ndr_push_struct_blob(&blob, packet, packet,
92 : (ndr_push_flags_fn_t)ndr_push_nbt_dgram_packet);
93 15 : if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
94 0 : DBG_ERR("ndr_push_nbt_dgram_packet - %s\n",
95 : ndr_errstr(ndr_err));
96 0 : return;
97 : }
98 :
99 30 : pstruct = parse_packet((char *)blob.data,
100 15 : blob.length,
101 : DGRAM_PACKET,
102 15 : interpret_addr2(src->addr),
103 : src->port);
104 15 : if (pstruct != NULL) {
105 15 : nb_packet_dispatch(nbtsrv->unexpected_server, pstruct);
106 15 : free_packet(pstruct);
107 : }
108 : }
109 :
110 :
111 : /*
112 : setup the port 138 datagram listener for a given interface
113 : */
114 130 : NTSTATUS nbtd_dgram_setup(struct nbtd_interface *iface, const char *bind_address)
115 : {
116 130 : struct nbt_dgram_socket *bcast_dgmsock = NULL;
117 130 : struct nbtd_server *nbtsrv = iface->nbtsrv;
118 4 : struct socket_address *bcast_addr, *bind_addr;
119 4 : NTSTATUS status;
120 130 : TALLOC_CTX *tmp_ctx = talloc_new(iface);
121 : /* the list of mailslots that we are interested in */
122 4 : size_t i;
123 :
124 130 : if (!tmp_ctx) {
125 0 : return NT_STATUS_NO_MEMORY;
126 : }
127 :
128 130 : if (strcmp("0.0.0.0", iface->netmask) != 0) {
129 : /* listen for broadcasts on port 138 */
130 65 : bcast_dgmsock = nbt_dgram_socket_init(iface, nbtsrv->task->event_ctx);
131 65 : if (!bcast_dgmsock) {
132 0 : talloc_free(tmp_ctx);
133 0 : return NT_STATUS_NO_MEMORY;
134 : }
135 :
136 65 : bcast_addr = socket_address_from_strings(tmp_ctx, bcast_dgmsock->sock->backend_name,
137 : iface->bcast_address,
138 65 : lpcfg_dgram_port(iface->nbtsrv->task->lp_ctx));
139 65 : if (!bcast_addr) {
140 0 : talloc_free(tmp_ctx);
141 0 : return NT_STATUS_NO_MEMORY;
142 : }
143 :
144 65 : status = socket_listen(bcast_dgmsock->sock, bcast_addr, 0, 0);
145 65 : if (!NT_STATUS_IS_OK(status)) {
146 0 : talloc_free(tmp_ctx);
147 0 : 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 0 : return status;
151 : }
152 :
153 65 : dgram_set_incoming_handler(bcast_dgmsock, dgram_request_handler, iface);
154 : }
155 :
156 : /* listen for unicasts on port 138 */
157 130 : iface->dgmsock = nbt_dgram_socket_init(iface, nbtsrv->task->event_ctx);
158 130 : if (!iface->dgmsock) {
159 0 : talloc_free(tmp_ctx);
160 0 : return NT_STATUS_NO_MEMORY;
161 : }
162 :
163 130 : bind_addr = socket_address_from_strings(tmp_ctx, iface->dgmsock->sock->backend_name,
164 130 : bind_address, lpcfg_dgram_port(iface->nbtsrv->task->lp_ctx));
165 130 : if (!bind_addr) {
166 0 : talloc_free(tmp_ctx);
167 0 : return NT_STATUS_NO_MEMORY;
168 : }
169 :
170 130 : status = socket_listen(iface->dgmsock->sock, bind_addr, 0, 0);
171 130 : if (!NT_STATUS_IS_OK(status)) {
172 0 : talloc_free(tmp_ctx);
173 0 : 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 0 : return status;
176 : }
177 :
178 130 : dgram_set_incoming_handler(iface->dgmsock, dgram_request_handler, iface);
179 :
180 130 : talloc_free(tmp_ctx);
181 :
182 524 : 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 12 : struct dgram_mailslot_handler *dgmslot;
187 :
188 390 : if (bcast_dgmsock) {
189 201 : dgmslot = dgram_mailslot_listen(bcast_dgmsock,
190 195 : mailslot_handlers[i].mailslot_name,
191 195 : mailslot_handlers[i].handler, iface);
192 195 : NT_STATUS_HAVE_NO_MEMORY(dgmslot);
193 : }
194 :
195 402 : dgmslot = dgram_mailslot_listen(iface->dgmsock,
196 390 : mailslot_handlers[i].mailslot_name,
197 390 : mailslot_handlers[i].handler, iface);
198 390 : NT_STATUS_HAVE_NO_MEMORY(dgmslot);
199 : }
200 :
201 130 : return NT_STATUS_OK;
202 : }
|