Line data Source code
1 : /*
2 : Unix SMB/CIFS implementation.
3 :
4 : core wins server handling
5 :
6 : Copyright (C) Andrew Tridgell 2005
7 : Copyright (C) Stefan Metzmacher 2005
8 :
9 : This program is free software; you can redistribute it and/or modify
10 : it under the terms of the GNU General Public License as published by
11 : the Free Software Foundation; either version 3 of the License, or
12 : (at your option) any later version.
13 :
14 : This program is distributed in the hope that it will be useful,
15 : but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : GNU General Public License for more details.
18 :
19 : You should have received a copy of the GNU General Public License
20 : along with this program. If not, see <http://www.gnu.org/licenses/>.
21 : */
22 :
23 : #include "includes.h"
24 : #include "lib/util/dlinklist.h"
25 : #include "nbt_server/nbt_server.h"
26 : #include "nbt_server/wins/winsdb.h"
27 : #include "nbt_server/wins/winsserver.h"
28 : #include "librpc/gen_ndr/ndr_nbt.h"
29 : #include "system/time.h"
30 : #include "libcli/composite/composite.h"
31 : #include "samba/service_task.h"
32 : #include "system/network.h"
33 : #include "lib/socket/socket.h"
34 : #include "lib/socket/netif.h"
35 : #include <ldb.h>
36 : #include "param/param.h"
37 : #include "libcli/resolve/resolve.h"
38 : #include "lib/util/util_net.h"
39 :
40 : /*
41 : work out the ttl we will use given a client requested ttl
42 : */
43 245 : uint32_t wins_server_ttl(struct wins_server *winssrv, uint32_t ttl)
44 : {
45 245 : ttl = MIN(ttl, winssrv->config.max_renew_interval);
46 245 : ttl = MAX(ttl, winssrv->config.min_renew_interval);
47 245 : return ttl;
48 : }
49 :
50 238 : static enum wrepl_name_type wrepl_type(uint16_t nb_flags, struct nbt_name *name, bool mhomed)
51 : {
52 : /* this copes with the nasty hack that is the type 0x1c name */
53 238 : if (name->type == NBT_NAME_LOGON) {
54 47 : return WREPL_TYPE_SGROUP;
55 : }
56 191 : if (nb_flags & NBT_NM_GROUP) {
57 39 : return WREPL_TYPE_GROUP;
58 : }
59 152 : if (mhomed) {
60 88 : return WREPL_TYPE_MHOMED;
61 : }
62 64 : return WREPL_TYPE_UNIQUE;
63 : }
64 :
65 : /*
66 : register a new name with WINS
67 : */
68 194 : static uint8_t wins_register_new(struct nbt_name_socket *nbtsock,
69 : struct nbt_name_packet *packet,
70 : const struct socket_address *src,
71 : enum wrepl_name_type type)
72 : {
73 194 : struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private_data,
74 : struct nbtd_interface);
75 194 : struct wins_server *winssrv = iface->nbtsrv->winssrv;
76 194 : struct nbt_name *name = &packet->questions[0].name;
77 194 : uint32_t ttl = wins_server_ttl(winssrv, packet->additional[0].ttl);
78 194 : uint16_t nb_flags = packet->additional[0].rdata.netbios.addresses[0].nb_flags;
79 194 : const char *address = packet->additional[0].rdata.netbios.addresses[0].ipaddr;
80 0 : struct winsdb_record rec;
81 0 : enum wrepl_name_node node;
82 :
83 : #define WREPL_NODE_NBT_FLAGS(nb_flags) \
84 : ((nb_flags & NBT_NM_OWNER_TYPE)>>13)
85 :
86 194 : node = WREPL_NODE_NBT_FLAGS(nb_flags);
87 :
88 194 : rec.name = name;
89 194 : rec.type = type;
90 194 : rec.state = WREPL_STATE_ACTIVE;
91 194 : rec.node = node;
92 194 : rec.is_static = false;
93 194 : rec.expire_time = time(NULL) + ttl;
94 194 : rec.version = 0; /* will be allocated later */
95 194 : rec.wins_owner = NULL; /* will be set later */
96 194 : rec.registered_by = src->addr;
97 194 : rec.addresses = winsdb_addr_list_make(packet);
98 194 : if (rec.addresses == NULL) return NBT_RCODE_SVR;
99 :
100 388 : rec.addresses = winsdb_addr_list_add(winssrv->wins_db,
101 : &rec, rec.addresses,
102 : address,
103 194 : winssrv->wins_db->local_owner,
104 : rec.expire_time,
105 : true);
106 194 : if (rec.addresses == NULL) return NBT_RCODE_SVR;
107 :
108 194 : DEBUG(4,("WINS: accepted registration of %s with address %s\n",
109 : nbt_name_string(packet, name), rec.addresses[0]->address));
110 :
111 194 : return winsdb_add(winssrv->wins_db, &rec, WINSDB_FLAG_ALLOC_VERSION | WINSDB_FLAG_TAKE_OWNERSHIP);
112 : }
113 :
114 :
115 : /*
116 : update the ttl on an existing record
117 : */
118 25 : static uint8_t wins_update_ttl(struct nbt_name_socket *nbtsock,
119 : struct nbt_name_packet *packet,
120 : struct winsdb_record *rec,
121 : struct winsdb_addr *winsdb_addr,
122 : const struct socket_address *src)
123 : {
124 25 : struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private_data,
125 : struct nbtd_interface);
126 25 : struct wins_server *winssrv = iface->nbtsrv->winssrv;
127 25 : uint32_t ttl = wins_server_ttl(winssrv, packet->additional[0].ttl);
128 25 : const char *address = packet->additional[0].rdata.netbios.addresses[0].ipaddr;
129 25 : uint32_t modify_flags = 0;
130 :
131 25 : rec->expire_time = time(NULL) + ttl;
132 25 : rec->registered_by = src->addr;
133 :
134 25 : if (winsdb_addr) {
135 46 : rec->addresses = winsdb_addr_list_add(winssrv->wins_db,
136 : rec, rec->addresses,
137 : winsdb_addr->address,
138 23 : winssrv->wins_db->local_owner,
139 : rec->expire_time,
140 : true);
141 23 : if (rec->addresses == NULL) return NBT_RCODE_SVR;
142 : }
143 :
144 25 : if (strcmp(winssrv->wins_db->local_owner, rec->wins_owner) != 0) {
145 0 : modify_flags = WINSDB_FLAG_ALLOC_VERSION | WINSDB_FLAG_TAKE_OWNERSHIP;
146 : }
147 :
148 25 : DEBUG(5,("WINS: refreshed registration of %s at %s\n",
149 : nbt_name_string(packet, rec->name), address));
150 :
151 25 : return winsdb_modify(winssrv->wins_db, rec, modify_flags);
152 : }
153 :
154 : /*
155 : do a sgroup merge
156 : */
157 8 : static uint8_t wins_sgroup_merge(struct nbt_name_socket *nbtsock,
158 : struct nbt_name_packet *packet,
159 : struct winsdb_record *rec,
160 : const char *address,
161 : const struct socket_address *src)
162 : {
163 8 : struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private_data,
164 : struct nbtd_interface);
165 8 : struct wins_server *winssrv = iface->nbtsrv->winssrv;
166 8 : uint32_t ttl = wins_server_ttl(winssrv, packet->additional[0].ttl);
167 :
168 8 : rec->expire_time = time(NULL) + ttl;
169 8 : rec->registered_by = src->addr;
170 :
171 16 : rec->addresses = winsdb_addr_list_add(winssrv->wins_db,
172 : rec, rec->addresses,
173 : address,
174 8 : winssrv->wins_db->local_owner,
175 : rec->expire_time,
176 : true);
177 8 : if (rec->addresses == NULL) return NBT_RCODE_SVR;
178 :
179 8 : DEBUG(5,("WINS: sgroup merge of %s at %s\n",
180 : nbt_name_string(packet, rec->name), address));
181 :
182 8 : return winsdb_modify(winssrv->wins_db, rec, WINSDB_FLAG_ALLOC_VERSION | WINSDB_FLAG_TAKE_OWNERSHIP);
183 : }
184 :
185 : struct nbtd_wins_wack_state {
186 : struct nbtd_wins_wack_state *prev, *next;
187 : struct wins_server *winssrv;
188 : struct nbt_name_socket *nbtsock;
189 : struct nbtd_interface *iface;
190 : struct nbt_name_packet *request_packet;
191 : struct winsdb_record *rec;
192 : struct socket_address *src;
193 : const char *reg_address;
194 : enum wrepl_name_type new_type;
195 : struct wins_challenge_io io;
196 : NTSTATUS status;
197 : };
198 :
199 18 : static int nbtd_wins_wack_state_destructor(struct nbtd_wins_wack_state *s)
200 : {
201 18 : DLIST_REMOVE(s->iface->wack_queue, s);
202 18 : return 0;
203 : }
204 :
205 236 : static bool wins_check_wack_queue(struct nbtd_interface *iface,
206 : struct nbt_name_packet *packet,
207 : struct socket_address *src)
208 : {
209 0 : struct nbtd_wins_wack_state *s;
210 :
211 236 : for (s= iface->wack_queue; s; s = s->next) {
212 0 : if (packet->name_trn_id != s->request_packet->name_trn_id) {
213 0 : continue;
214 : }
215 0 : if (packet->operation != s->request_packet->operation) {
216 0 : continue;
217 : }
218 0 : if (src->port != s->src->port) {
219 0 : continue;
220 : }
221 0 : if (strcmp(src->addr, s->src->addr) != 0) {
222 0 : continue;
223 : }
224 :
225 0 : return true;
226 : }
227 :
228 236 : return false;
229 : }
230 :
231 : /*
232 : deny a registration request
233 : */
234 0 : static void wins_wack_deny(struct nbtd_wins_wack_state *s)
235 : {
236 0 : nbtd_name_registration_reply(s->nbtsock, s->request_packet,
237 : s->src, NBT_RCODE_ACT);
238 0 : DEBUG(4,("WINS: denied name registration request for %s from %s:%d\n",
239 : nbt_name_string(s, s->rec->name), s->src->addr, s->src->port));
240 0 : talloc_free(s);
241 0 : }
242 :
243 : /*
244 : allow a registration request
245 : */
246 18 : static void wins_wack_allow(struct nbtd_wins_wack_state *s)
247 : {
248 0 : NTSTATUS status;
249 18 : uint32_t ttl = wins_server_ttl(s->winssrv, s->request_packet->additional[0].ttl);
250 18 : struct winsdb_record *rec = s->rec, *rec2;
251 0 : uint32_t i,j;
252 :
253 18 : status = winsdb_lookup(s->winssrv->wins_db, rec->name, s, &rec2);
254 18 : if (!NT_STATUS_IS_OK(status) ||
255 18 : rec2->version != rec->version ||
256 18 : strcmp(rec2->wins_owner, rec->wins_owner) != 0) {
257 0 : DEBUG(5,("WINS: record %s changed during WACK - failing registration\n",
258 : nbt_name_string(s, rec->name)));
259 0 : wins_wack_deny(s);
260 0 : return;
261 : }
262 :
263 : /*
264 : * if the old name owner doesn't hold the name anymore
265 : * handle the request as new registration for the new name owner
266 : */
267 18 : if (!NT_STATUS_IS_OK(s->status)) {
268 0 : uint8_t rcode;
269 :
270 10 : winsdb_delete(s->winssrv->wins_db, rec);
271 10 : rcode = wins_register_new(s->nbtsock, s->request_packet, s->src, s->new_type);
272 10 : if (rcode != NBT_RCODE_OK) {
273 0 : DEBUG(1,("WINS: record %s failed to register as new during WACK\n",
274 : nbt_name_string(s, rec->name)));
275 0 : wins_wack_deny(s);
276 0 : return;
277 : }
278 10 : goto done;
279 : }
280 :
281 8 : rec->expire_time = time(NULL) + ttl;
282 8 : rec->registered_by = s->src->addr;
283 :
284 : /*
285 : * now remove all addresses that the client doesn't hold anymore
286 : * and update the time stamp and owner for the ones that are still there
287 : */
288 24 : for (i=0; rec->addresses[i]; i++) {
289 16 : bool found = false;
290 24 : for (j=0; j < s->io.out.num_addresses; j++) {
291 24 : if (strcmp(rec->addresses[i]->address, s->io.out.addresses[j]) != 0) continue;
292 :
293 16 : found = true;
294 16 : break;
295 : }
296 16 : if (found) {
297 32 : rec->addresses = winsdb_addr_list_add(s->winssrv->wins_db,
298 : rec, rec->addresses,
299 : s->reg_address,
300 16 : s->winssrv->wins_db->local_owner,
301 : rec->expire_time,
302 : true);
303 16 : if (rec->addresses == NULL) goto failed;
304 16 : continue;
305 : }
306 :
307 0 : winsdb_addr_list_remove(rec->addresses, rec->addresses[i]->address);
308 : }
309 :
310 16 : rec->addresses = winsdb_addr_list_add(s->winssrv->wins_db,
311 : rec, rec->addresses,
312 : s->reg_address,
313 8 : s->winssrv->wins_db->local_owner,
314 : rec->expire_time,
315 : true);
316 8 : if (rec->addresses == NULL) goto failed;
317 :
318 : /* if we have more than one address, this becomes implicit a MHOMED record */
319 8 : if (winsdb_addr_list_length(rec->addresses) > 1) {
320 8 : rec->type = WREPL_TYPE_MHOMED;
321 : }
322 :
323 8 : winsdb_modify(s->winssrv->wins_db, rec, WINSDB_FLAG_ALLOC_VERSION | WINSDB_FLAG_TAKE_OWNERSHIP);
324 :
325 8 : DEBUG(4,("WINS: accepted registration of %s with address %s\n",
326 : nbt_name_string(s, rec->name), s->reg_address));
327 :
328 18 : done:
329 18 : nbtd_name_registration_reply(s->nbtsock, s->request_packet,
330 : s->src, NBT_RCODE_OK);
331 18 : failed:
332 18 : talloc_free(s);
333 : }
334 :
335 : /*
336 : called when a name query to a current owner completes
337 : */
338 18 : static void wack_wins_challenge_handler(struct composite_context *c_req)
339 : {
340 18 : struct nbtd_wins_wack_state *s = talloc_get_type(c_req->async.private_data,
341 : struct nbtd_wins_wack_state);
342 0 : bool found;
343 0 : uint32_t i;
344 :
345 18 : s->status = wins_challenge_recv(c_req, s, &s->io);
346 :
347 : /*
348 : * if the owner denies it holds the name, then allow
349 : * the registration
350 : */
351 18 : if (!NT_STATUS_IS_OK(s->status)) {
352 10 : wins_wack_allow(s);
353 10 : return;
354 : }
355 :
356 8 : if (s->new_type == WREPL_TYPE_GROUP || s->new_type == WREPL_TYPE_SGROUP) {
357 0 : DEBUG(1,("WINS: record %s failed to register as group type(%u) during WACK, it's still type(%u)\n",
358 : nbt_name_string(s, s->rec->name), s->new_type, s->rec->type));
359 0 : wins_wack_deny(s);
360 0 : return;
361 : }
362 :
363 : /*
364 : * if the owner still wants the name and doesn't reply
365 : * with the address trying to be registered, then deny
366 : * the registration
367 : */
368 8 : found = false;
369 16 : for (i=0; i < s->io.out.num_addresses; i++) {
370 16 : if (strcmp(s->reg_address, s->io.out.addresses[i]) != 0) continue;
371 :
372 8 : found = true;
373 8 : break;
374 : }
375 8 : if (!found) {
376 0 : wins_wack_deny(s);
377 0 : return;
378 : }
379 :
380 8 : wins_wack_allow(s);
381 8 : return;
382 : }
383 :
384 :
385 : /*
386 : a client has asked to register a unique name that someone else owns. We
387 : need to ask each of the current owners if they still want it. If they do
388 : then reject the registration, otherwise allow it
389 : */
390 18 : static void wins_register_wack(struct nbt_name_socket *nbtsock,
391 : struct nbt_name_packet *packet,
392 : struct winsdb_record *rec,
393 : struct socket_address *src,
394 : enum wrepl_name_type new_type)
395 : {
396 18 : struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private_data,
397 : struct nbtd_interface);
398 18 : struct wins_server *winssrv = iface->nbtsrv->winssrv;
399 0 : struct nbtd_wins_wack_state *s;
400 0 : struct composite_context *c_req;
401 0 : uint32_t ttl;
402 :
403 18 : s = talloc_zero(nbtsock, struct nbtd_wins_wack_state);
404 18 : if (s == NULL) goto failed;
405 :
406 : /* package up the state variables for this wack request */
407 18 : s->winssrv = winssrv;
408 18 : s->nbtsock = nbtsock;
409 18 : s->iface = iface;
410 18 : s->request_packet = talloc_steal(s, packet);
411 18 : s->rec = talloc_steal(s, rec);
412 18 : s->reg_address = packet->additional[0].rdata.netbios.addresses[0].ipaddr;
413 18 : s->new_type = new_type;
414 18 : s->src = socket_address_copy(s, src);
415 18 : if (s->src == NULL) goto failed;
416 :
417 18 : s->io.in.nbtd_server = iface->nbtsrv;
418 18 : s->io.in.nbt_port = lpcfg_nbt_port(iface->nbtsrv->task->lp_ctx);
419 18 : s->io.in.event_ctx = iface->nbtsrv->task->event_ctx;
420 18 : s->io.in.name = rec->name;
421 18 : s->io.in.num_addresses = winsdb_addr_list_length(rec->addresses);
422 18 : s->io.in.addresses = winsdb_addr_string_list(s, rec->addresses);
423 18 : if (s->io.in.addresses == NULL) goto failed;
424 :
425 18 : DLIST_ADD_END(iface->wack_queue, s);
426 :
427 18 : talloc_set_destructor(s, nbtd_wins_wack_state_destructor);
428 :
429 : /*
430 : * send a WACK to the client, specifying the maximum time it could
431 : * take to check with the owner, plus some slack
432 : */
433 18 : ttl = 5 + 4 * winsdb_addr_list_length(rec->addresses);
434 18 : nbtd_wack_reply(nbtsock, packet, src, ttl);
435 :
436 : /*
437 : * send the challenge to the old addresses
438 : */
439 18 : c_req = wins_challenge_send(s, &s->io);
440 18 : if (c_req == NULL) goto failed;
441 :
442 18 : c_req->async.fn = wack_wins_challenge_handler;
443 18 : c_req->async.private_data = s;
444 18 : return;
445 :
446 0 : failed:
447 0 : talloc_free(s);
448 0 : nbtd_name_registration_reply(nbtsock, packet, src, NBT_RCODE_SVR);
449 : }
450 :
451 : /*
452 : register a name
453 : */
454 238 : static void nbtd_winsserver_register(struct nbt_name_socket *nbtsock,
455 : struct nbt_name_packet *packet,
456 : struct socket_address *src)
457 : {
458 0 : NTSTATUS status;
459 238 : struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private_data,
460 : struct nbtd_interface);
461 238 : struct wins_server *winssrv = iface->nbtsrv->winssrv;
462 238 : struct nbt_name *name = &packet->questions[0].name;
463 0 : struct winsdb_record *rec;
464 238 : uint8_t rcode = NBT_RCODE_OK;
465 238 : uint16_t nb_flags = packet->additional[0].rdata.netbios.addresses[0].nb_flags;
466 238 : const char *address = packet->additional[0].rdata.netbios.addresses[0].ipaddr;
467 238 : bool mhomed = ((packet->operation & NBT_OPCODE) == NBT_OPCODE_MULTI_HOME_REG);
468 238 : enum wrepl_name_type new_type = wrepl_type(nb_flags, name, mhomed);
469 238 : struct winsdb_addr *winsdb_addr = NULL;
470 0 : bool duplicate_packet;
471 :
472 : /*
473 : * as a special case, the local master browser name is always accepted
474 : * for registration, but never stored, but w2k3 stores it if it's registered
475 : * as a group name, (but a query for the 0x1D name still returns not found!)
476 : */
477 238 : if (name->type == NBT_NAME_MASTER && !(nb_flags & NBT_NM_GROUP)) {
478 1 : rcode = NBT_RCODE_OK;
479 1 : goto done;
480 : }
481 :
482 : /* w2k3 refuses 0x1B names with marked as group */
483 237 : if (name->type == NBT_NAME_PDC && (nb_flags & NBT_NM_GROUP)) {
484 0 : rcode = NBT_RCODE_RFS;
485 0 : goto done;
486 : }
487 :
488 : /* w2k3 refuses 0x1C names with out marked as group */
489 237 : if (name->type == NBT_NAME_LOGON && !(nb_flags & NBT_NM_GROUP)) {
490 0 : rcode = NBT_RCODE_RFS;
491 0 : goto done;
492 : }
493 :
494 : /* w2k3 refuses 0x1E names with out marked as group */
495 237 : if (name->type == NBT_NAME_BROWSER && !(nb_flags & NBT_NM_GROUP)) {
496 0 : rcode = NBT_RCODE_RFS;
497 0 : goto done;
498 : }
499 :
500 237 : if (name->scope && strlen(name->scope) > 237) {
501 1 : rcode = NBT_RCODE_SVR;
502 1 : goto done;
503 : }
504 :
505 236 : duplicate_packet = wins_check_wack_queue(iface, packet, src);
506 236 : if (duplicate_packet) {
507 : /* just ignore the packet */
508 0 : DEBUG(5,("Ignoring duplicate packet while WACK is pending from %s:%d\n",
509 : src->addr, src->port));
510 18 : return;
511 : }
512 :
513 236 : status = winsdb_lookup(winssrv->wins_db, name, packet, &rec);
514 236 : if (NT_STATUS_EQUAL(NT_STATUS_OBJECT_NAME_NOT_FOUND, status)) {
515 168 : rcode = wins_register_new(nbtsock, packet, src, new_type);
516 168 : goto done;
517 68 : } else if (!NT_STATUS_IS_OK(status)) {
518 0 : rcode = NBT_RCODE_SVR;
519 0 : goto done;
520 68 : } else if (rec->is_static) {
521 0 : if (rec->type == WREPL_TYPE_GROUP || rec->type == WREPL_TYPE_SGROUP) {
522 0 : rcode = NBT_RCODE_OK;
523 0 : goto done;
524 : }
525 0 : rcode = NBT_RCODE_ACT;
526 0 : goto done;
527 : }
528 :
529 68 : if (rec->type == WREPL_TYPE_GROUP) {
530 5 : if (new_type != WREPL_TYPE_GROUP) {
531 1 : DEBUG(2,("WINS: Attempt to register name %s as non normal group(%u)"
532 : " while a normal group is already there\n",
533 : nbt_name_string(packet, name), new_type));
534 1 : rcode = NBT_RCODE_ACT;
535 1 : goto done;
536 : }
537 :
538 4 : if (rec->state == WREPL_STATE_ACTIVE) {
539 : /* TODO: is this correct? */
540 2 : rcode = wins_update_ttl(nbtsock, packet, rec, NULL, src);
541 2 : goto done;
542 : }
543 :
544 : /* TODO: is this correct? */
545 2 : winsdb_delete(winssrv->wins_db, rec);
546 2 : rcode = wins_register_new(nbtsock, packet, src, new_type);
547 2 : goto done;
548 : }
549 :
550 63 : if (rec->state != WREPL_STATE_ACTIVE) {
551 14 : winsdb_delete(winssrv->wins_db, rec);
552 14 : rcode = wins_register_new(nbtsock, packet, src, new_type);
553 14 : goto done;
554 : }
555 :
556 49 : switch (rec->type) {
557 40 : case WREPL_TYPE_UNIQUE:
558 : case WREPL_TYPE_MHOMED:
559 : /*
560 : * if its an active unique name, and the registration is for a group, then
561 : * see if the unique name owner still wants the name
562 : * TODO: is this correct?
563 : */
564 40 : if (new_type == WREPL_TYPE_GROUP || new_type == WREPL_TYPE_GROUP) {
565 0 : wins_register_wack(nbtsock, packet, rec, src, new_type);
566 0 : return;
567 : }
568 :
569 : /*
570 : * if the registration is for an address that is currently active, then
571 : * just update the expiry time of the record and the address
572 : */
573 40 : winsdb_addr = winsdb_addr_list_check(rec->addresses, address);
574 40 : if (winsdb_addr) {
575 22 : rcode = wins_update_ttl(nbtsock, packet, rec, winsdb_addr, src);
576 22 : goto done;
577 : }
578 :
579 : /*
580 : * we have to do a WACK to see if the current owner is willing
581 : * to give up its claim
582 : */
583 18 : wins_register_wack(nbtsock, packet, rec, src, new_type);
584 18 : return;
585 :
586 0 : case WREPL_TYPE_GROUP:
587 : /* this should not be reached as normal groups are handled above */
588 0 : DEBUG(0,("BUG at %s\n",__location__));
589 0 : rcode = NBT_RCODE_ACT;
590 0 : goto done;
591 :
592 9 : case WREPL_TYPE_SGROUP:
593 : /* if the new record isn't also a special group, refuse the registration */
594 9 : if (new_type != WREPL_TYPE_SGROUP) {
595 0 : DEBUG(2,("WINS: Attempt to register name %s as non special group(%u)"
596 : " while a special group is already there\n",
597 : nbt_name_string(packet, name), new_type));
598 0 : rcode = NBT_RCODE_ACT;
599 0 : goto done;
600 : }
601 :
602 : /*
603 : * if the registration is for an address that is currently active, then
604 : * just update the expiry time of the record and the address
605 : */
606 9 : winsdb_addr = winsdb_addr_list_check(rec->addresses, address);
607 9 : if (winsdb_addr) {
608 1 : rcode = wins_update_ttl(nbtsock, packet, rec, winsdb_addr, src);
609 1 : goto done;
610 : }
611 :
612 8 : rcode = wins_sgroup_merge(nbtsock, packet, rec, address, src);
613 8 : goto done;
614 : }
615 :
616 220 : done:
617 220 : nbtd_name_registration_reply(nbtsock, packet, src, rcode);
618 : }
619 :
620 0 : static uint32_t ipv4_match_bits(struct in_addr ip1, struct in_addr ip2)
621 : {
622 0 : uint32_t i, j, match=0;
623 0 : uint8_t *p1, *p2;
624 :
625 0 : p1 = (uint8_t *)&ip1.s_addr;
626 0 : p2 = (uint8_t *)&ip2.s_addr;
627 :
628 0 : for (i=0; i<4; i++) {
629 0 : if (p1[i] != p2[i]) break;
630 0 : match += 8;
631 : }
632 :
633 0 : if (i==4) return match;
634 :
635 0 : for (j=0; j<8; j++) {
636 0 : if ((p1[i] & (1<<(7-j))) != (p2[i] & (1<<(7-j))))
637 0 : break;
638 0 : match++;
639 : }
640 :
641 0 : return match;
642 : }
643 :
644 0 : static int nbtd_wins_randomize1Clist_sort(void *p1,/* (const char **) */
645 : void *p2,/* (const char **) */
646 : struct socket_address *src)
647 : {
648 0 : const char *a1 = (const char *)*(const char **)p1;
649 0 : const char *a2 = (const char *)*(const char **)p2;
650 0 : uint32_t match_bits1;
651 0 : uint32_t match_bits2;
652 :
653 0 : match_bits1 = ipv4_match_bits(interpret_addr2(a1), interpret_addr2(src->addr));
654 0 : match_bits2 = ipv4_match_bits(interpret_addr2(a2), interpret_addr2(src->addr));
655 :
656 0 : return match_bits2 - match_bits1;
657 : }
658 :
659 0 : static void nbtd_wins_randomize1Clist(struct loadparm_context *lp_ctx,
660 : const char **addresses, struct socket_address *src)
661 : {
662 0 : const char *mask;
663 0 : const char *tmp;
664 0 : uint32_t num_addrs;
665 0 : uint32_t idx, sidx;
666 0 : int r;
667 :
668 0 : for (num_addrs=0; addresses[num_addrs]; num_addrs++) { /* noop */ }
669 :
670 0 : if (num_addrs <= 1) return; /* nothing to do */
671 :
672 : /* first sort the addresses depending on the matching to the client */
673 0 : LDB_TYPESAFE_QSORT(addresses, num_addrs, src, nbtd_wins_randomize1Clist_sort);
674 :
675 0 : mask = lpcfg_parm_string(lp_ctx, NULL, "nbtd", "wins_randomize1Clist_mask");
676 0 : if (!mask) {
677 0 : mask = "255.255.255.0";
678 : }
679 :
680 : /*
681 : * choose a random address to be the first in the response to the client,
682 : * prefer the addresses inside the nbtd:wins_randomize1Clist_mask netmask
683 : */
684 0 : r = random();
685 0 : idx = sidx = r % num_addrs;
686 :
687 0 : while (1) {
688 0 : bool same;
689 :
690 : /* if the current one is in the same subnet, use it */
691 0 : same = iface_list_same_net(addresses[idx], src->addr, mask);
692 0 : if (same) {
693 0 : sidx = idx;
694 0 : break;
695 : }
696 :
697 : /* we need to check for idx == 0, after checking for the same net */
698 0 : if (idx == 0) break;
699 : /*
700 : * if we haven't found an address in the same subnet, search in ones
701 : * which match the client more
702 : *
703 : * some notes:
704 : *
705 : * it's not "idx = idx % r" but "idx = r % idx"
706 : * because in "a % b" b is the allowed range
707 : * and b-1 is the maximum possible result, so it must be decreasing
708 : * and the above idx == 0 check breaks the while(1) loop.
709 : */
710 0 : idx = r % idx;
711 : }
712 :
713 : /* note sidx == 0 is also valid here ... */
714 0 : tmp = addresses[0];
715 0 : addresses[0] = addresses[sidx];
716 0 : addresses[sidx] = tmp;
717 : }
718 :
719 : /*
720 : query a name
721 : */
722 56 : static void nbtd_winsserver_query(struct loadparm_context *lp_ctx,
723 : struct nbt_name_socket *nbtsock,
724 : struct nbt_name_packet *packet,
725 : struct socket_address *src)
726 : {
727 0 : NTSTATUS status;
728 56 : struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private_data,
729 : struct nbtd_interface);
730 56 : struct wins_server *winssrv = iface->nbtsrv->winssrv;
731 56 : struct nbt_name *name = &packet->questions[0].name;
732 0 : struct winsdb_record *rec;
733 56 : struct winsdb_record *rec_1b = NULL;
734 0 : const char **addresses;
735 56 : const char **addresses_1b = NULL;
736 56 : uint16_t nb_flags = 0;
737 :
738 56 : if (name->type == NBT_NAME_MASTER) {
739 2 : goto notfound;
740 : }
741 :
742 : /*
743 : * w2k3 returns the first address of the 0x1B record as first address
744 : * to a 0x1C query
745 : *
746 : * since Windows 2000 Service Pack 2 there's on option to trigger this behavior:
747 : *
748 : * HKEY_LOCAL_MACHINE\System\CurrentControlset\Services\WINS\Parameters\Prepend1BTo1CQueries
749 : * Typ: Daten REG_DWORD
750 : * Value: 0 = deactivated, 1 = activated
751 : */
752 56 : if (name->type == NBT_NAME_LOGON &&
753 2 : lpcfg_parm_bool(lp_ctx, NULL, "nbtd", "wins_prepend1Bto1Cqueries", true)) {
754 0 : struct nbt_name name_1b;
755 :
756 2 : name_1b = *name;
757 2 : name_1b.type = NBT_NAME_PDC;
758 :
759 2 : status = winsdb_lookup(winssrv->wins_db, &name_1b, packet, &rec_1b);
760 2 : if (NT_STATUS_IS_OK(status)) {
761 0 : addresses_1b = winsdb_addr_string_list(packet, rec_1b->addresses);
762 : }
763 : }
764 :
765 54 : status = winsdb_lookup(winssrv->wins_db, name, packet, &rec);
766 54 : if (!NT_STATUS_IS_OK(status)) {
767 24 : if (!lpcfg_wins_dns_proxy(lp_ctx)) {
768 0 : goto notfound;
769 : }
770 :
771 24 : if (name->type != NBT_NAME_CLIENT && name->type != NBT_NAME_SERVER) {
772 12 : goto notfound;
773 : }
774 :
775 12 : nbtd_wins_dns_proxy_query(nbtsock, packet, src);
776 29 : return;
777 : }
778 :
779 : /*
780 : * for group's we always reply with
781 : * 255.255.255.255 as address, even if
782 : * the record is released or tombstoned
783 : */
784 30 : if (rec->type == WREPL_TYPE_GROUP) {
785 4 : addresses = str_list_add(NULL, "255.255.255.255");
786 4 : talloc_steal(packet, addresses);
787 4 : if (!addresses) {
788 0 : goto notfound;
789 : }
790 4 : nb_flags |= NBT_NM_GROUP;
791 4 : goto found;
792 : }
793 :
794 26 : if (rec->state != WREPL_STATE_ACTIVE) {
795 13 : goto notfound;
796 : }
797 :
798 13 : addresses = winsdb_addr_string_list(packet, rec->addresses);
799 13 : if (!addresses) {
800 0 : goto notfound;
801 : }
802 :
803 : /*
804 : * if addresses_1b isn't NULL, we have a 0x1C query and need to return the
805 : * first 0x1B address as first address
806 : */
807 13 : if (addresses_1b && addresses_1b[0]) {
808 0 : const char **addresses_1c = addresses;
809 0 : uint32_t i;
810 0 : uint32_t num_addrs;
811 :
812 0 : addresses = str_list_add(NULL, addresses_1b[0]);
813 0 : if (!addresses) {
814 0 : goto notfound;
815 : }
816 0 : talloc_steal(packet, addresses);
817 0 : num_addrs = 1;
818 :
819 0 : for (i=0; addresses_1c[i]; i++) {
820 0 : if (strcmp(addresses_1b[0], addresses_1c[i]) == 0) continue;
821 :
822 : /*
823 : * stop when we already have 25 addresses
824 : */
825 0 : if (num_addrs >= 25) break;
826 :
827 0 : num_addrs++;
828 0 : addresses = str_list_add(addresses, addresses_1c[i]);
829 0 : if (!addresses) {
830 0 : goto notfound;
831 : }
832 : }
833 : }
834 :
835 13 : if (rec->type == WREPL_TYPE_SGROUP) {
836 1 : nb_flags |= NBT_NM_GROUP;
837 : } else {
838 12 : nb_flags |= (rec->node <<13);
839 : }
840 :
841 : /*
842 : * since Windows 2000 Service Pack 2 there's on option to trigger this behavior:
843 : *
844 : * HKEY_LOCAL_MACHINE\System\CurrentControlset\Services\WINS\Parameters\Randomize1CList
845 : * Typ: Daten REG_DWORD
846 : * Value: 0 = deactivated, 1 = activated
847 : */
848 14 : if (name->type == NBT_NAME_LOGON &&
849 1 : lpcfg_parm_bool(lp_ctx, NULL, "nbtd", "wins_randomize1Clist", false)) {
850 0 : nbtd_wins_randomize1Clist(lp_ctx, addresses, src);
851 : }
852 :
853 13 : found:
854 17 : nbtd_name_query_reply(nbtsock, packet, src, name,
855 : 0, nb_flags, addresses);
856 17 : return;
857 :
858 27 : notfound:
859 27 : nbtd_negative_name_query_reply(nbtsock, packet, src);
860 : }
861 :
862 : /*
863 : release a name
864 : */
865 210 : static void nbtd_winsserver_release(struct nbt_name_socket *nbtsock,
866 : struct nbt_name_packet *packet,
867 : struct socket_address *src)
868 : {
869 0 : NTSTATUS status;
870 210 : struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private_data,
871 : struct nbtd_interface);
872 210 : struct wins_server *winssrv = iface->nbtsrv->winssrv;
873 210 : struct nbt_name *name = &packet->questions[0].name;
874 0 : struct winsdb_record *rec;
875 210 : uint32_t modify_flags = 0;
876 0 : uint8_t ret;
877 :
878 210 : if (name->type == NBT_NAME_MASTER) {
879 2 : goto done;
880 : }
881 :
882 208 : if (name->scope && strlen(name->scope) > 237) {
883 1 : goto done;
884 : }
885 :
886 207 : status = winsdb_lookup(winssrv->wins_db, name, packet, &rec);
887 207 : if (!NT_STATUS_IS_OK(status)) {
888 15 : goto done;
889 : }
890 :
891 192 : if (rec->is_static) {
892 0 : if (rec->type == WREPL_TYPE_UNIQUE || rec->type == WREPL_TYPE_MHOMED) {
893 0 : goto done;
894 : }
895 0 : nbtd_name_release_reply(nbtsock, packet, src, NBT_RCODE_ACT);
896 0 : return;
897 : }
898 :
899 192 : if (rec->state != WREPL_STATE_ACTIVE) {
900 27 : goto done;
901 : }
902 :
903 : /*
904 : * TODO: do we need to check if
905 : * src->addr matches packet->additional[0].rdata.netbios.addresses[0].ipaddr
906 : * here?
907 : */
908 :
909 : /*
910 : * we only allow releases from an owner - other releases are
911 : * silently ignored
912 : */
913 165 : if (!winsdb_addr_list_check(rec->addresses, src->addr)) {
914 0 : int i;
915 0 : DEBUG(4,("WINS: silently ignoring attempted name release on %s from %s\n", nbt_name_string(rec, rec->name), src->addr));
916 0 : DEBUGADD(4, ("Registered Addresses: \n"));
917 0 : for (i=0; rec->addresses && rec->addresses[i]; i++) {
918 0 : DEBUGADD(4, ("%s\n", rec->addresses[i]->address));
919 : }
920 0 : goto done;
921 : }
922 :
923 165 : DEBUG(4,("WINS: released name %s from %s\n", nbt_name_string(rec, rec->name), src->addr));
924 :
925 165 : switch (rec->type) {
926 41 : case WREPL_TYPE_UNIQUE:
927 41 : rec->state = WREPL_STATE_RELEASED;
928 41 : break;
929 :
930 34 : case WREPL_TYPE_GROUP:
931 34 : rec->state = WREPL_STATE_RELEASED;
932 34 : break;
933 :
934 46 : case WREPL_TYPE_SGROUP:
935 46 : winsdb_addr_list_remove(rec->addresses, src->addr);
936 : /* TODO: do we need to take the ownership here? */
937 46 : if (winsdb_addr_list_length(rec->addresses) == 0) {
938 36 : rec->state = WREPL_STATE_RELEASED;
939 : }
940 46 : break;
941 :
942 44 : case WREPL_TYPE_MHOMED:
943 44 : winsdb_addr_list_remove(rec->addresses, src->addr);
944 : /* TODO: do we need to take the ownership here? */
945 44 : if (winsdb_addr_list_length(rec->addresses) == 0) {
946 42 : rec->state = WREPL_STATE_RELEASED;
947 : }
948 44 : break;
949 : }
950 :
951 165 : if (rec->state == WREPL_STATE_ACTIVE) {
952 : /*
953 : * If the record is still active, we need to update the
954 : * expire_time.
955 : *
956 : * if we're not the owner, we need to take the ownership.
957 : */
958 12 : rec->expire_time= time(NULL) + winssrv->config.max_renew_interval;
959 12 : if (strcmp(rec->wins_owner, winssrv->wins_db->local_owner) != 0) {
960 0 : modify_flags = WINSDB_FLAG_ALLOC_VERSION | WINSDB_FLAG_TAKE_OWNERSHIP;
961 : }
962 12 : if (lpcfg_parm_bool(iface->nbtsrv->task->lp_ctx, NULL, "wreplsrv", "propagate name releases", false)) {
963 : /*
964 : * We have an option to propagate every name release,
965 : * this is off by default to match windows servers
966 : */
967 0 : modify_flags = WINSDB_FLAG_ALLOC_VERSION | WINSDB_FLAG_TAKE_OWNERSHIP;
968 : }
969 153 : } else if (rec->state == WREPL_STATE_RELEASED) {
970 : /*
971 : * if we're not the owner, we need to take the owner ship
972 : * and make the record tombstone, but expire after
973 : * tombstone_interval + tombstone_timeout and not only after tombstone_timeout
974 : * like for normal tombstone records.
975 : * This is to replicate the record directly to the original owner,
976 : * where the record is still active
977 : */
978 153 : if (strcmp(rec->wins_owner, winssrv->wins_db->local_owner) == 0) {
979 153 : rec->expire_time= time(NULL) + winssrv->config.tombstone_interval;
980 : } else {
981 0 : rec->state = WREPL_STATE_TOMBSTONE;
982 0 : rec->expire_time= time(NULL) +
983 0 : winssrv->config.tombstone_interval +
984 0 : winssrv->config.tombstone_timeout;
985 0 : modify_flags = WINSDB_FLAG_ALLOC_VERSION | WINSDB_FLAG_TAKE_OWNERSHIP;
986 : }
987 : }
988 :
989 165 : ret = winsdb_modify(winssrv->wins_db, rec, modify_flags);
990 165 : if (ret != NBT_RCODE_OK) {
991 0 : DEBUG(1,("WINS: FAILED: released name %s at %s: error:%u\n",
992 : nbt_name_string(rec, rec->name), src->addr, ret));
993 : }
994 165 : done:
995 : /* we match w2k3 by always giving a positive reply to name releases. */
996 210 : nbtd_name_release_reply(nbtsock, packet, src, NBT_RCODE_OK);
997 : }
998 :
999 :
1000 : /*
1001 : answer a name query
1002 : */
1003 5845 : void nbtd_winsserver_request(struct nbt_name_socket *nbtsock,
1004 : struct nbt_name_packet *packet,
1005 : struct socket_address *src)
1006 : {
1007 5845 : struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private_data,
1008 : struct nbtd_interface);
1009 5845 : struct wins_server *winssrv = iface->nbtsrv->winssrv;
1010 5845 : if ((packet->operation & NBT_FLAG_BROADCAST) || winssrv == NULL) {
1011 5276 : return;
1012 : }
1013 :
1014 504 : switch (packet->operation & NBT_OPCODE) {
1015 56 : case NBT_OPCODE_QUERY:
1016 56 : nbtd_winsserver_query(iface->nbtsrv->task->lp_ctx, nbtsock, packet, src);
1017 56 : break;
1018 :
1019 238 : case NBT_OPCODE_REGISTER:
1020 : case NBT_OPCODE_REFRESH:
1021 : case NBT_OPCODE_REFRESH2:
1022 : case NBT_OPCODE_MULTI_HOME_REG:
1023 238 : nbtd_winsserver_register(nbtsock, packet, src);
1024 238 : break;
1025 :
1026 210 : case NBT_OPCODE_RELEASE:
1027 210 : nbtd_winsserver_release(nbtsock, packet, src);
1028 210 : break;
1029 : }
1030 :
1031 : }
1032 :
1033 : /*
1034 : startup the WINS server, if configured
1035 : */
1036 65 : NTSTATUS nbtd_winsserver_init(struct nbtd_server *nbtsrv)
1037 : {
1038 2 : uint32_t tmp;
1039 2 : const char *owner;
1040 :
1041 65 : if (!lpcfg_we_are_a_wins_server(nbtsrv->task->lp_ctx)) {
1042 0 : nbtsrv->winssrv = NULL;
1043 0 : return NT_STATUS_OK;
1044 : }
1045 :
1046 65 : nbtsrv->winssrv = talloc_zero(nbtsrv, struct wins_server);
1047 65 : NT_STATUS_HAVE_NO_MEMORY(nbtsrv->winssrv);
1048 :
1049 65 : nbtsrv->winssrv->config.max_renew_interval = lpcfg_max_wins_ttl(nbtsrv->task->lp_ctx);
1050 65 : nbtsrv->winssrv->config.min_renew_interval = lpcfg_min_wins_ttl(nbtsrv->task->lp_ctx);
1051 65 : tmp = lpcfg_parm_int(nbtsrv->task->lp_ctx, NULL, "wreplsrv", "tombstone_interval", 6*24*60*60);
1052 65 : nbtsrv->winssrv->config.tombstone_interval = tmp;
1053 65 : tmp = lpcfg_parm_int(nbtsrv->task->lp_ctx, NULL, "wreplsrv"," tombstone_timeout", 1*24*60*60);
1054 65 : nbtsrv->winssrv->config.tombstone_timeout = tmp;
1055 :
1056 65 : owner = lpcfg_parm_string(nbtsrv->task->lp_ctx, NULL, "winsdb", "local_owner");
1057 :
1058 65 : if (owner == NULL) {
1059 2 : struct interface *ifaces;
1060 65 : load_interface_list(nbtsrv->task, nbtsrv->task->lp_ctx, &ifaces);
1061 65 : owner = iface_list_first_v4(ifaces);
1062 : }
1063 :
1064 130 : nbtsrv->winssrv->wins_db = winsdb_connect(nbtsrv->winssrv, nbtsrv->task->event_ctx,
1065 65 : nbtsrv->task->lp_ctx,
1066 : owner, WINSDB_HANDLE_CALLER_NBTD);
1067 65 : if (!nbtsrv->winssrv->wins_db) {
1068 0 : return NT_STATUS_INTERNAL_DB_ERROR;
1069 : }
1070 :
1071 65 : irpc_add_name(nbtsrv->task->msg_ctx, "wins_server");
1072 :
1073 65 : return NT_STATUS_OK;
1074 : }
|