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 274 : uint32_t wins_server_ttl(struct wins_server *winssrv, uint32_t ttl)
44 : {
45 274 : ttl = MIN(ttl, winssrv->config.max_renew_interval);
46 274 : ttl = MAX(ttl, winssrv->config.min_renew_interval);
47 274 : return ttl;
48 : }
49 :
50 265 : 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 265 : if (name->type == NBT_NAME_LOGON) {
54 47 : return WREPL_TYPE_SGROUP;
55 : }
56 218 : if (nb_flags & NBT_NM_GROUP) {
57 39 : return WREPL_TYPE_GROUP;
58 : }
59 179 : if (mhomed) {
60 115 : return WREPL_TYPE_MHOMED;
61 : }
62 64 : return WREPL_TYPE_UNIQUE;
63 : }
64 :
65 : /*
66 : register a new name with WINS
67 : */
68 205 : 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 205 : struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private_data,
74 : struct nbtd_interface);
75 205 : struct wins_server *winssrv = iface->nbtsrv->winssrv;
76 205 : struct nbt_name *name = &packet->questions[0].name;
77 205 : uint32_t ttl = wins_server_ttl(winssrv, packet->additional[0].ttl);
78 205 : uint16_t nb_flags = packet->additional[0].rdata.netbios.addresses[0].nb_flags;
79 205 : const char *address = packet->additional[0].rdata.netbios.addresses[0].ipaddr;
80 : struct winsdb_record rec;
81 : enum wrepl_name_node node;
82 :
83 : #define WREPL_NODE_NBT_FLAGS(nb_flags) \
84 : ((nb_flags & NBT_NM_OWNER_TYPE)>>13)
85 :
86 205 : node = WREPL_NODE_NBT_FLAGS(nb_flags);
87 :
88 205 : rec.name = name;
89 205 : rec.type = type;
90 205 : rec.state = WREPL_STATE_ACTIVE;
91 205 : rec.node = node;
92 205 : rec.is_static = false;
93 205 : rec.expire_time = time(NULL) + ttl;
94 205 : rec.version = 0; /* will be allocated later */
95 205 : rec.wins_owner = NULL; /* will be set later */
96 205 : rec.registered_by = src->addr;
97 205 : rec.addresses = winsdb_addr_list_make(packet);
98 205 : if (rec.addresses == NULL) return NBT_RCODE_SVR;
99 :
100 410 : rec.addresses = winsdb_addr_list_add(winssrv->wins_db,
101 : &rec, rec.addresses,
102 : address,
103 205 : winssrv->wins_db->local_owner,
104 : rec.expire_time,
105 : true);
106 205 : if (rec.addresses == NULL) return NBT_RCODE_SVR;
107 :
108 205 : DEBUG(4,("WINS: accepted registration of %s with address %s\n",
109 : nbt_name_string(packet, name), rec.addresses[0]->address));
110 :
111 205 : 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 35 : 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 35 : struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private_data,
125 : struct nbtd_interface);
126 35 : struct wins_server *winssrv = iface->nbtsrv->winssrv;
127 35 : uint32_t ttl = wins_server_ttl(winssrv, packet->additional[0].ttl);
128 35 : const char *address = packet->additional[0].rdata.netbios.addresses[0].ipaddr;
129 35 : uint32_t modify_flags = 0;
130 :
131 35 : rec->expire_time = time(NULL) + ttl;
132 35 : rec->registered_by = src->addr;
133 :
134 35 : if (winsdb_addr) {
135 66 : rec->addresses = winsdb_addr_list_add(winssrv->wins_db,
136 : rec, rec->addresses,
137 : winsdb_addr->address,
138 33 : winssrv->wins_db->local_owner,
139 : rec->expire_time,
140 : true);
141 33 : if (rec->addresses == NULL) return NBT_RCODE_SVR;
142 : }
143 :
144 35 : 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 35 : DEBUG(5,("WINS: refreshed registration of %s at %s\n",
149 : nbt_name_string(packet, rec->name), address));
150 :
151 35 : 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 26 : static int nbtd_wins_wack_state_destructor(struct nbtd_wins_wack_state *s)
200 : {
201 26 : DLIST_REMOVE(s->iface->wack_queue, s);
202 26 : return 0;
203 : }
204 :
205 263 : static bool wins_check_wack_queue(struct nbtd_interface *iface,
206 : struct nbt_name_packet *packet,
207 : struct socket_address *src)
208 : {
209 : struct nbtd_wins_wack_state *s;
210 :
211 263 : 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 263 : return false;
229 : }
230 :
231 : /*
232 : deny a registration request
233 : */
234 3 : static void wins_wack_deny(struct nbtd_wins_wack_state *s)
235 : {
236 3 : nbtd_name_registration_reply(s->nbtsock, s->request_packet,
237 : s->src, NBT_RCODE_ACT);
238 3 : 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 3 : talloc_free(s);
241 3 : }
242 :
243 : /*
244 : allow a registration request
245 : */
246 26 : static void wins_wack_allow(struct nbtd_wins_wack_state *s)
247 : {
248 : NTSTATUS status;
249 26 : uint32_t ttl = wins_server_ttl(s->winssrv, s->request_packet->additional[0].ttl);
250 26 : struct winsdb_record *rec = s->rec, *rec2;
251 : uint32_t i,j;
252 :
253 26 : status = winsdb_lookup(s->winssrv->wins_db, rec->name, s, &rec2);
254 49 : if (!NT_STATUS_IS_OK(status) ||
255 46 : rec2->version != rec->version ||
256 23 : strcmp(rec2->wins_owner, rec->wins_owner) != 0) {
257 3 : DEBUG(5,("WINS: record %s changed during WACK - failing registration\n",
258 : nbt_name_string(s, rec->name)));
259 3 : wins_wack_deny(s);
260 3 : 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 23 : if (!NT_STATUS_IS_OK(s->status)) {
268 : uint8_t rcode;
269 :
270 15 : winsdb_delete(s->winssrv->wins_db, rec);
271 15 : rcode = wins_register_new(s->nbtsock, s->request_packet, s->src, s->new_type);
272 15 : 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 15 : 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 48 : 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 23 : done:
329 23 : nbtd_name_registration_reply(s->nbtsock, s->request_packet,
330 : s->src, NBT_RCODE_OK);
331 23 : failed:
332 23 : talloc_free(s);
333 : }
334 :
335 : /*
336 : called when a name query to a current owner completes
337 : */
338 26 : static void wack_wins_challenge_handler(struct composite_context *c_req)
339 : {
340 26 : struct nbtd_wins_wack_state *s = talloc_get_type(c_req->async.private_data,
341 : struct nbtd_wins_wack_state);
342 : bool found;
343 : uint32_t i;
344 :
345 26 : 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 26 : if (!NT_STATUS_IS_OK(s->status)) {
352 18 : wins_wack_allow(s);
353 18 : 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 32 : 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 26 : 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 26 : struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private_data,
397 : struct nbtd_interface);
398 26 : struct wins_server *winssrv = iface->nbtsrv->winssrv;
399 : struct nbtd_wins_wack_state *s;
400 : struct composite_context *c_req;
401 : uint32_t ttl;
402 :
403 26 : s = talloc_zero(nbtsock, struct nbtd_wins_wack_state);
404 26 : if (s == NULL) goto failed;
405 :
406 : /* package up the state variables for this wack request */
407 26 : s->winssrv = winssrv;
408 26 : s->nbtsock = nbtsock;
409 26 : s->iface = iface;
410 26 : s->request_packet = talloc_steal(s, packet);
411 26 : s->rec = talloc_steal(s, rec);
412 26 : s->reg_address = packet->additional[0].rdata.netbios.addresses[0].ipaddr;
413 26 : s->new_type = new_type;
414 26 : s->src = socket_address_copy(s, src);
415 26 : if (s->src == NULL) goto failed;
416 :
417 26 : s->io.in.nbtd_server = iface->nbtsrv;
418 26 : s->io.in.nbt_port = lpcfg_nbt_port(iface->nbtsrv->task->lp_ctx);
419 26 : s->io.in.event_ctx = iface->nbtsrv->task->event_ctx;
420 26 : s->io.in.name = rec->name;
421 26 : s->io.in.num_addresses = winsdb_addr_list_length(rec->addresses);
422 26 : s->io.in.addresses = winsdb_addr_string_list(s, rec->addresses);
423 26 : if (s->io.in.addresses == NULL) goto failed;
424 :
425 26 : DLIST_ADD_END(iface->wack_queue, s);
426 :
427 26 : 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 26 : ttl = 5 + 4 * winsdb_addr_list_length(rec->addresses);
434 26 : nbtd_wack_reply(nbtsock, packet, src, ttl);
435 :
436 : /*
437 : * send the challenge to the old addresses
438 : */
439 26 : c_req = wins_challenge_send(s, &s->io);
440 26 : if (c_req == NULL) goto failed;
441 :
442 26 : c_req->async.fn = wack_wins_challenge_handler;
443 26 : c_req->async.private_data = s;
444 26 : 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 265 : static void nbtd_winsserver_register(struct nbt_name_socket *nbtsock,
455 : struct nbt_name_packet *packet,
456 : struct socket_address *src)
457 : {
458 : NTSTATUS status;
459 265 : struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private_data,
460 : struct nbtd_interface);
461 265 : struct wins_server *winssrv = iface->nbtsrv->winssrv;
462 265 : struct nbt_name *name = NULL;
463 : struct winsdb_record *rec;
464 265 : uint8_t rcode = NBT_RCODE_OK;
465 265 : struct nbt_res_rec *additional = NULL;
466 : uint16_t nb_flags;
467 265 : const char *address = NULL;
468 265 : struct nbt_rdata_address *addresses = NULL;
469 265 : bool mhomed = ((packet->operation & NBT_OPCODE) == NBT_OPCODE_MULTI_HOME_REG);
470 : enum wrepl_name_type new_type;
471 265 : struct winsdb_addr *winsdb_addr = NULL;
472 : bool duplicate_packet;
473 :
474 291 : NBTD_ASSERT_PACKET(packet, src, packet->qdcount > 0);
475 265 : NBTD_ASSERT_PACKET(packet, src, packet->arcount > 0);
476 :
477 265 : name = &packet->questions[0].name;
478 265 : additional = packet->additional;
479 :
480 265 : NBTD_ASSERT_PACKET(packet,
481 : src,
482 : additional[0].rdata.netbios.length > 0);
483 :
484 265 : addresses = additional[0].rdata.netbios.addresses;
485 :
486 265 : nb_flags = addresses[0].nb_flags;
487 265 : address = addresses[0].ipaddr;
488 265 : new_type = wrepl_type(nb_flags, name, mhomed);
489 :
490 : /*
491 : * as a special case, the local master browser name is always accepted
492 : * for registration, but never stored, but w2k3 stores it if it's registered
493 : * as a group name, (but a query for the 0x1D name still returns not found!)
494 : */
495 265 : if (name->type == NBT_NAME_MASTER && !(nb_flags & NBT_NM_GROUP)) {
496 1 : rcode = NBT_RCODE_OK;
497 1 : goto done;
498 : }
499 :
500 : /* w2k3 refuses 0x1B names with marked as group */
501 264 : if (name->type == NBT_NAME_PDC && (nb_flags & NBT_NM_GROUP)) {
502 0 : rcode = NBT_RCODE_RFS;
503 0 : goto done;
504 : }
505 :
506 : /* w2k3 refuses 0x1C names with out marked as group */
507 264 : if (name->type == NBT_NAME_LOGON && !(nb_flags & NBT_NM_GROUP)) {
508 0 : rcode = NBT_RCODE_RFS;
509 0 : goto done;
510 : }
511 :
512 : /* w2k3 refuses 0x1E names with out marked as group */
513 264 : if (name->type == NBT_NAME_BROWSER && !(nb_flags & NBT_NM_GROUP)) {
514 0 : rcode = NBT_RCODE_RFS;
515 0 : goto done;
516 : }
517 :
518 264 : if (name->scope && strlen(name->scope) > 237) {
519 1 : rcode = NBT_RCODE_SVR;
520 1 : goto done;
521 : }
522 :
523 263 : duplicate_packet = wins_check_wack_queue(iface, packet, src);
524 263 : if (duplicate_packet) {
525 : /* just ignore the packet */
526 0 : DEBUG(5,("Ignoring duplicate packet while WACK is pending from %s:%d\n",
527 : src->addr, src->port));
528 0 : return;
529 : }
530 :
531 263 : status = winsdb_lookup(winssrv->wins_db, name, packet, &rec);
532 263 : if (NT_STATUS_EQUAL(NT_STATUS_OBJECT_NAME_NOT_FOUND, status)) {
533 174 : rcode = wins_register_new(nbtsock, packet, src, new_type);
534 174 : goto done;
535 89 : } else if (!NT_STATUS_IS_OK(status)) {
536 3 : rcode = NBT_RCODE_SVR;
537 3 : goto done;
538 86 : } else if (rec->is_static) {
539 0 : if (rec->type == WREPL_TYPE_GROUP || rec->type == WREPL_TYPE_SGROUP) {
540 0 : rcode = NBT_RCODE_OK;
541 0 : goto done;
542 : }
543 0 : rcode = NBT_RCODE_ACT;
544 0 : goto done;
545 : }
546 :
547 86 : if (rec->type == WREPL_TYPE_GROUP) {
548 5 : if (new_type != WREPL_TYPE_GROUP) {
549 1 : DEBUG(2,("WINS: Attempt to register name %s as non normal group(%u)"
550 : " while a normal group is already there\n",
551 : nbt_name_string(packet, name), new_type));
552 1 : rcode = NBT_RCODE_ACT;
553 1 : goto done;
554 : }
555 :
556 4 : if (rec->state == WREPL_STATE_ACTIVE) {
557 : /* TODO: is this correct? */
558 2 : rcode = wins_update_ttl(nbtsock, packet, rec, NULL, src);
559 2 : goto done;
560 : }
561 :
562 : /* TODO: is this correct? */
563 2 : winsdb_delete(winssrv->wins_db, rec);
564 2 : rcode = wins_register_new(nbtsock, packet, src, new_type);
565 2 : goto done;
566 : }
567 :
568 81 : if (rec->state != WREPL_STATE_ACTIVE) {
569 14 : winsdb_delete(winssrv->wins_db, rec);
570 14 : rcode = wins_register_new(nbtsock, packet, src, new_type);
571 14 : goto done;
572 : }
573 :
574 67 : switch (rec->type) {
575 58 : case WREPL_TYPE_UNIQUE:
576 : case WREPL_TYPE_MHOMED:
577 : /*
578 : * if its an active unique name, and the registration is for a group, then
579 : * see if the unique name owner still wants the name
580 : * TODO: is this correct?
581 : */
582 58 : if (new_type == WREPL_TYPE_GROUP || new_type == WREPL_TYPE_GROUP) {
583 0 : wins_register_wack(nbtsock, packet, rec, src, new_type);
584 0 : return;
585 : }
586 :
587 : /*
588 : * if the registration is for an address that is currently active, then
589 : * just update the expiry time of the record and the address
590 : */
591 58 : winsdb_addr = winsdb_addr_list_check(rec->addresses, address);
592 58 : if (winsdb_addr) {
593 32 : rcode = wins_update_ttl(nbtsock, packet, rec, winsdb_addr, src);
594 32 : goto done;
595 : }
596 :
597 : /*
598 : * we have to do a WACK to see if the current owner is willing
599 : * to give up its claim
600 : */
601 26 : wins_register_wack(nbtsock, packet, rec, src, new_type);
602 26 : return;
603 :
604 0 : case WREPL_TYPE_GROUP:
605 : /* this should not be reached as normal groups are handled above */
606 0 : DEBUG(0,("BUG at %s\n",__location__));
607 0 : rcode = NBT_RCODE_ACT;
608 0 : goto done;
609 :
610 9 : case WREPL_TYPE_SGROUP:
611 : /* if the new record isn't also a special group, refuse the registration */
612 9 : if (new_type != WREPL_TYPE_SGROUP) {
613 0 : DEBUG(2,("WINS: Attempt to register name %s as non special group(%u)"
614 : " while a special group is already there\n",
615 : nbt_name_string(packet, name), new_type));
616 0 : rcode = NBT_RCODE_ACT;
617 0 : goto done;
618 : }
619 :
620 : /*
621 : * if the registration is for an address that is currently active, then
622 : * just update the expiry time of the record and the address
623 : */
624 9 : winsdb_addr = winsdb_addr_list_check(rec->addresses, address);
625 9 : if (winsdb_addr) {
626 1 : rcode = wins_update_ttl(nbtsock, packet, rec, winsdb_addr, src);
627 1 : goto done;
628 : }
629 :
630 8 : rcode = wins_sgroup_merge(nbtsock, packet, rec, address, src);
631 8 : goto done;
632 : }
633 :
634 239 : done:
635 239 : nbtd_name_registration_reply(nbtsock, packet, src, rcode);
636 : }
637 :
638 0 : static uint32_t ipv4_match_bits(struct in_addr ip1, struct in_addr ip2)
639 : {
640 0 : uint32_t i, j, match=0;
641 : uint8_t *p1, *p2;
642 :
643 0 : p1 = (uint8_t *)&ip1.s_addr;
644 0 : p2 = (uint8_t *)&ip2.s_addr;
645 :
646 0 : for (i=0; i<4; i++) {
647 0 : if (p1[i] != p2[i]) break;
648 0 : match += 8;
649 : }
650 :
651 0 : if (i==4) return match;
652 :
653 0 : for (j=0; j<8; j++) {
654 0 : if ((p1[i] & (1<<(7-j))) != (p2[i] & (1<<(7-j))))
655 0 : break;
656 0 : match++;
657 : }
658 :
659 0 : return match;
660 : }
661 :
662 0 : static int nbtd_wins_randomize1Clist_sort(void *p1,/* (const char **) */
663 : void *p2,/* (const char **) */
664 : struct socket_address *src)
665 : {
666 0 : const char *a1 = (const char *)*(const char **)p1;
667 0 : const char *a2 = (const char *)*(const char **)p2;
668 : uint32_t match_bits1;
669 : uint32_t match_bits2;
670 :
671 0 : match_bits1 = ipv4_match_bits(interpret_addr2(a1), interpret_addr2(src->addr));
672 0 : match_bits2 = ipv4_match_bits(interpret_addr2(a2), interpret_addr2(src->addr));
673 :
674 0 : return match_bits2 - match_bits1;
675 : }
676 :
677 0 : static void nbtd_wins_randomize1Clist(struct loadparm_context *lp_ctx,
678 : const char **addresses, struct socket_address *src)
679 : {
680 : const char *mask;
681 : const char *tmp;
682 : uint32_t num_addrs;
683 : uint32_t idx, sidx;
684 : int r;
685 :
686 0 : for (num_addrs=0; addresses[num_addrs]; num_addrs++) { /* noop */ }
687 :
688 0 : if (num_addrs <= 1) return; /* nothing to do */
689 :
690 : /* first sort the addresses depending on the matching to the client */
691 0 : LDB_TYPESAFE_QSORT(addresses, num_addrs, src, nbtd_wins_randomize1Clist_sort);
692 :
693 0 : mask = lpcfg_parm_string(lp_ctx, NULL, "nbtd", "wins_randomize1Clist_mask");
694 0 : if (!mask) {
695 0 : mask = "255.255.255.0";
696 : }
697 :
698 : /*
699 : * choose a random address to be the first in the response to the client,
700 : * prefer the addresses inside the nbtd:wins_randomize1Clist_mask netmask
701 : */
702 0 : r = random();
703 0 : idx = sidx = r % num_addrs;
704 :
705 0 : while (1) {
706 : bool same;
707 :
708 : /* if the current one is in the same subnet, use it */
709 0 : same = iface_list_same_net(addresses[idx], src->addr, mask);
710 0 : if (same) {
711 0 : sidx = idx;
712 0 : break;
713 : }
714 :
715 : /* we need to check for idx == 0, after checking for the same net */
716 0 : if (idx == 0) break;
717 : /*
718 : * if we haven't found an address in the same subnet, search in ones
719 : * which match the client more
720 : *
721 : * some notes:
722 : *
723 : * it's not "idx = idx % r" but "idx = r % idx"
724 : * because in "a % b" b is the allowed range
725 : * and b-1 is the maximum possible result, so it must be decreasing
726 : * and the above idx == 0 check breaks the while(1) loop.
727 : */
728 0 : idx = r % idx;
729 : }
730 :
731 : /* note sidx == 0 is also valid here ... */
732 0 : tmp = addresses[0];
733 0 : addresses[0] = addresses[sidx];
734 0 : addresses[sidx] = tmp;
735 : }
736 :
737 : /*
738 : query a name
739 : */
740 40 : static void nbtd_winsserver_query(struct loadparm_context *lp_ctx,
741 : struct nbt_name_socket *nbtsock,
742 : struct nbt_name_packet *packet,
743 : struct socket_address *src)
744 : {
745 : NTSTATUS status;
746 40 : struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private_data,
747 : struct nbtd_interface);
748 40 : struct wins_server *winssrv = iface->nbtsrv->winssrv;
749 40 : struct nbt_name *name = NULL;
750 : struct winsdb_record *rec;
751 40 : struct winsdb_record *rec_1b = NULL;
752 : const char **addresses;
753 40 : const char **addresses_1b = NULL;
754 40 : uint16_t nb_flags = 0;
755 :
756 57 : NBTD_ASSERT_PACKET(packet, src, packet->qdcount > 0);
757 :
758 40 : name = &packet->questions[0].name;
759 :
760 40 : if (name->type == NBT_NAME_MASTER) {
761 2 : goto notfound;
762 : }
763 :
764 : /*
765 : * w2k3 returns the first address of the 0x1B record as first address
766 : * to a 0x1C query
767 : *
768 : * since Windows 2000 Service Pack 2 there's on option to trigger this behavior:
769 : *
770 : * HKEY_LOCAL_MACHINE\System\CurrentControlset\Services\WINS\Parameters\Prepend1BTo1CQueries
771 : * Typ: Daten REG_DWORD
772 : * Value: 0 = deactivated, 1 = activated
773 : */
774 40 : if (name->type == NBT_NAME_LOGON &&
775 2 : lpcfg_parm_bool(lp_ctx, NULL, "nbtd", "wins_prepend1Bto1Cqueries", true)) {
776 : struct nbt_name name_1b;
777 :
778 2 : name_1b = *name;
779 2 : name_1b.type = NBT_NAME_PDC;
780 :
781 2 : status = winsdb_lookup(winssrv->wins_db, &name_1b, packet, &rec_1b);
782 2 : if (NT_STATUS_IS_OK(status)) {
783 0 : addresses_1b = winsdb_addr_string_list(packet, rec_1b->addresses);
784 : }
785 : }
786 :
787 38 : status = winsdb_lookup(winssrv->wins_db, name, packet, &rec);
788 38 : if (!NT_STATUS_IS_OK(status)) {
789 8 : if (!lpcfg_wins_dns_proxy(lp_ctx)) {
790 0 : goto notfound;
791 : }
792 :
793 8 : if (name->type != NBT_NAME_CLIENT && name->type != NBT_NAME_SERVER) {
794 8 : goto notfound;
795 : }
796 :
797 0 : nbtd_wins_dns_proxy_query(nbtsock, packet, src);
798 0 : return;
799 : }
800 :
801 : /*
802 : * for group's we always reply with
803 : * 255.255.255.255 as address, even if
804 : * the record is released or tombstoned
805 : */
806 30 : if (rec->type == WREPL_TYPE_GROUP) {
807 4 : addresses = str_list_add(NULL, "255.255.255.255");
808 4 : talloc_steal(packet, addresses);
809 4 : if (!addresses) {
810 0 : goto notfound;
811 : }
812 4 : nb_flags |= NBT_NM_GROUP;
813 4 : goto found;
814 : }
815 :
816 26 : if (rec->state != WREPL_STATE_ACTIVE) {
817 13 : goto notfound;
818 : }
819 :
820 13 : addresses = winsdb_addr_string_list(packet, rec->addresses);
821 13 : if (!addresses) {
822 0 : goto notfound;
823 : }
824 :
825 : /*
826 : * if addresses_1b isn't NULL, we have a 0x1C query and need to return the
827 : * first 0x1B address as first address
828 : */
829 13 : if (addresses_1b && addresses_1b[0]) {
830 0 : const char **addresses_1c = addresses;
831 : uint32_t i;
832 : uint32_t num_addrs;
833 :
834 0 : addresses = str_list_add(NULL, addresses_1b[0]);
835 0 : if (!addresses) {
836 0 : goto notfound;
837 : }
838 0 : talloc_steal(packet, addresses);
839 0 : num_addrs = 1;
840 :
841 0 : for (i=0; addresses_1c[i]; i++) {
842 0 : if (strcmp(addresses_1b[0], addresses_1c[i]) == 0) continue;
843 :
844 : /*
845 : * stop when we already have 25 addresses
846 : */
847 0 : if (num_addrs >= 25) break;
848 :
849 0 : num_addrs++;
850 0 : addresses = str_list_add(addresses, addresses_1c[i]);
851 0 : if (!addresses) {
852 0 : goto notfound;
853 : }
854 : }
855 : }
856 :
857 13 : if (rec->type == WREPL_TYPE_SGROUP) {
858 1 : nb_flags |= NBT_NM_GROUP;
859 : } else {
860 12 : nb_flags |= (rec->node <<13);
861 : }
862 :
863 : /*
864 : * since Windows 2000 Service Pack 2 there's on option to trigger this behavior:
865 : *
866 : * HKEY_LOCAL_MACHINE\System\CurrentControlset\Services\WINS\Parameters\Randomize1CList
867 : * Typ: Daten REG_DWORD
868 : * Value: 0 = deactivated, 1 = activated
869 : */
870 14 : if (name->type == NBT_NAME_LOGON &&
871 1 : lpcfg_parm_bool(lp_ctx, NULL, "nbtd", "wins_randomize1Clist", false)) {
872 0 : nbtd_wins_randomize1Clist(lp_ctx, addresses, src);
873 : }
874 :
875 30 : found:
876 17 : nbtd_name_query_reply(nbtsock, packet, src, name,
877 : 0, nb_flags, addresses);
878 17 : return;
879 :
880 23 : notfound:
881 23 : nbtd_negative_name_query_reply(nbtsock, packet, src);
882 : }
883 :
884 : /*
885 : release a name
886 : */
887 217 : static void nbtd_winsserver_release(struct nbt_name_socket *nbtsock,
888 : struct nbt_name_packet *packet,
889 : struct socket_address *src)
890 : {
891 : NTSTATUS status;
892 217 : struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private_data,
893 : struct nbtd_interface);
894 217 : struct wins_server *winssrv = iface->nbtsrv->winssrv;
895 217 : struct nbt_name *name = NULL;
896 : struct winsdb_record *rec;
897 217 : uint32_t modify_flags = 0;
898 : uint8_t ret;
899 :
900 217 : NBTD_ASSERT_PACKET(packet, src, packet->qdcount > 0);
901 :
902 217 : name = &packet->questions[0].name;
903 :
904 217 : if (name->type == NBT_NAME_MASTER) {
905 2 : goto done;
906 : }
907 :
908 215 : if (name->scope && strlen(name->scope) > 237) {
909 1 : goto done;
910 : }
911 :
912 214 : status = winsdb_lookup(winssrv->wins_db, name, packet, &rec);
913 214 : if (!NT_STATUS_IS_OK(status)) {
914 22 : goto done;
915 : }
916 :
917 192 : if (rec->is_static) {
918 0 : if (rec->type == WREPL_TYPE_UNIQUE || rec->type == WREPL_TYPE_MHOMED) {
919 0 : goto done;
920 : }
921 0 : nbtd_name_release_reply(nbtsock, packet, src, NBT_RCODE_ACT);
922 0 : return;
923 : }
924 :
925 192 : if (rec->state != WREPL_STATE_ACTIVE) {
926 27 : goto done;
927 : }
928 :
929 : /*
930 : * TODO: do we need to check if
931 : * src->addr matches packet->additional[0].rdata.netbios.addresses[0].ipaddr
932 : * here?
933 : */
934 :
935 : /*
936 : * we only allow releases from an owner - other releases are
937 : * silently ignored
938 : */
939 165 : if (!winsdb_addr_list_check(rec->addresses, src->addr)) {
940 : int i;
941 0 : DEBUG(4,("WINS: silently ignoring attempted name release on %s from %s\n", nbt_name_string(rec, rec->name), src->addr));
942 0 : DEBUGADD(4, ("Registered Addresses: \n"));
943 0 : for (i=0; rec->addresses && rec->addresses[i]; i++) {
944 0 : DEBUGADD(4, ("%s\n", rec->addresses[i]->address));
945 : }
946 0 : goto done;
947 : }
948 :
949 165 : DEBUG(4,("WINS: released name %s from %s\n", nbt_name_string(rec, rec->name), src->addr));
950 :
951 165 : switch (rec->type) {
952 41 : case WREPL_TYPE_UNIQUE:
953 41 : rec->state = WREPL_STATE_RELEASED;
954 41 : break;
955 :
956 34 : case WREPL_TYPE_GROUP:
957 34 : rec->state = WREPL_STATE_RELEASED;
958 34 : break;
959 :
960 46 : case WREPL_TYPE_SGROUP:
961 46 : winsdb_addr_list_remove(rec->addresses, src->addr);
962 : /* TODO: do we need to take the ownership here? */
963 46 : if (winsdb_addr_list_length(rec->addresses) == 0) {
964 36 : rec->state = WREPL_STATE_RELEASED;
965 : }
966 46 : break;
967 :
968 44 : case WREPL_TYPE_MHOMED:
969 44 : winsdb_addr_list_remove(rec->addresses, src->addr);
970 : /* TODO: do we need to take the ownership here? */
971 44 : if (winsdb_addr_list_length(rec->addresses) == 0) {
972 42 : rec->state = WREPL_STATE_RELEASED;
973 : }
974 44 : break;
975 : }
976 :
977 165 : if (rec->state == WREPL_STATE_ACTIVE) {
978 : /*
979 : * If the record is still active, we need to update the
980 : * expire_time.
981 : *
982 : * if we're not the owner, we need to take the ownership.
983 : */
984 12 : rec->expire_time= time(NULL) + winssrv->config.max_renew_interval;
985 12 : if (strcmp(rec->wins_owner, winssrv->wins_db->local_owner) != 0) {
986 0 : modify_flags = WINSDB_FLAG_ALLOC_VERSION | WINSDB_FLAG_TAKE_OWNERSHIP;
987 : }
988 12 : if (lpcfg_parm_bool(iface->nbtsrv->task->lp_ctx, NULL, "wreplsrv", "propagate name releases", false)) {
989 : /*
990 : * We have an option to propagate every name release,
991 : * this is off by default to match windows servers
992 : */
993 0 : modify_flags = WINSDB_FLAG_ALLOC_VERSION | WINSDB_FLAG_TAKE_OWNERSHIP;
994 : }
995 153 : } else if (rec->state == WREPL_STATE_RELEASED) {
996 : /*
997 : * if we're not the owner, we need to take the owner ship
998 : * and make the record tombstone, but expire after
999 : * tombstone_interval + tombstone_timeout and not only after tombstone_timeout
1000 : * like for normal tombstone records.
1001 : * This is to replicate the record directly to the original owner,
1002 : * where the record is still active
1003 : */
1004 153 : if (strcmp(rec->wins_owner, winssrv->wins_db->local_owner) == 0) {
1005 153 : rec->expire_time= time(NULL) + winssrv->config.tombstone_interval;
1006 : } else {
1007 0 : rec->state = WREPL_STATE_TOMBSTONE;
1008 0 : rec->expire_time= time(NULL) +
1009 0 : winssrv->config.tombstone_interval +
1010 0 : winssrv->config.tombstone_timeout;
1011 0 : modify_flags = WINSDB_FLAG_ALLOC_VERSION | WINSDB_FLAG_TAKE_OWNERSHIP;
1012 : }
1013 : }
1014 :
1015 165 : ret = winsdb_modify(winssrv->wins_db, rec, modify_flags);
1016 165 : if (ret != NBT_RCODE_OK) {
1017 0 : DEBUG(1,("WINS: FAILED: released name %s at %s: error:%u\n",
1018 : nbt_name_string(rec, rec->name), src->addr, ret));
1019 : }
1020 382 : done:
1021 : /* we match w2k3 by always giving a positive reply to name releases. */
1022 217 : nbtd_name_release_reply(nbtsock, packet, src, NBT_RCODE_OK);
1023 : }
1024 :
1025 :
1026 : /*
1027 : answer a name query
1028 : */
1029 4948 : void nbtd_winsserver_request(struct nbt_name_socket *nbtsock,
1030 : struct nbt_name_packet *packet,
1031 : struct socket_address *src)
1032 : {
1033 4948 : struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private_data,
1034 : struct nbtd_interface);
1035 4948 : struct wins_server *winssrv = iface->nbtsrv->winssrv;
1036 4948 : if ((packet->operation & NBT_FLAG_BROADCAST) || winssrv == NULL) {
1037 4426 : return;
1038 : }
1039 :
1040 522 : switch (packet->operation & NBT_OPCODE) {
1041 40 : case NBT_OPCODE_QUERY:
1042 40 : nbtd_winsserver_query(iface->nbtsrv->task->lp_ctx, nbtsock, packet, src);
1043 40 : break;
1044 :
1045 265 : case NBT_OPCODE_REGISTER:
1046 : case NBT_OPCODE_REFRESH:
1047 : case NBT_OPCODE_REFRESH2:
1048 : case NBT_OPCODE_MULTI_HOME_REG:
1049 265 : nbtd_winsserver_register(nbtsock, packet, src);
1050 265 : break;
1051 :
1052 217 : case NBT_OPCODE_RELEASE:
1053 217 : nbtd_winsserver_release(nbtsock, packet, src);
1054 217 : break;
1055 : }
1056 :
1057 : }
1058 :
1059 : /*
1060 : startup the WINS server, if configured
1061 : */
1062 53 : NTSTATUS nbtd_winsserver_init(struct nbtd_server *nbtsrv)
1063 : {
1064 : uint32_t tmp;
1065 : const char *owner;
1066 :
1067 53 : if (!lpcfg_we_are_a_wins_server(nbtsrv->task->lp_ctx)) {
1068 0 : nbtsrv->winssrv = NULL;
1069 0 : return NT_STATUS_OK;
1070 : }
1071 :
1072 53 : nbtsrv->winssrv = talloc_zero(nbtsrv, struct wins_server);
1073 53 : NT_STATUS_HAVE_NO_MEMORY(nbtsrv->winssrv);
1074 :
1075 53 : nbtsrv->winssrv->config.max_renew_interval = lpcfg_max_wins_ttl(nbtsrv->task->lp_ctx);
1076 53 : nbtsrv->winssrv->config.min_renew_interval = lpcfg_min_wins_ttl(nbtsrv->task->lp_ctx);
1077 53 : tmp = lpcfg_parm_int(nbtsrv->task->lp_ctx, NULL, "wreplsrv", "tombstone_interval", 6*24*60*60);
1078 53 : nbtsrv->winssrv->config.tombstone_interval = tmp;
1079 53 : tmp = lpcfg_parm_int(nbtsrv->task->lp_ctx, NULL, "wreplsrv"," tombstone_timeout", 1*24*60*60);
1080 53 : nbtsrv->winssrv->config.tombstone_timeout = tmp;
1081 :
1082 53 : owner = lpcfg_parm_string(nbtsrv->task->lp_ctx, NULL, "winsdb", "local_owner");
1083 :
1084 53 : if (owner == NULL) {
1085 : struct interface *ifaces;
1086 53 : load_interface_list(nbtsrv->task, nbtsrv->task->lp_ctx, &ifaces);
1087 53 : owner = iface_list_first_v4(ifaces);
1088 : }
1089 :
1090 68 : nbtsrv->winssrv->wins_db = winsdb_connect(nbtsrv->winssrv, nbtsrv->task->event_ctx,
1091 53 : nbtsrv->task->lp_ctx,
1092 : owner, WINSDB_HANDLE_CALLER_NBTD);
1093 53 : if (!nbtsrv->winssrv->wins_db) {
1094 0 : return NT_STATUS_INTERNAL_DB_ERROR;
1095 : }
1096 :
1097 53 : irpc_add_name(nbtsrv->task->msg_ctx, "wins_server");
1098 :
1099 53 : return NT_STATUS_OK;
1100 : }
|