2 Unix SMB/Netbios implementation.
4 NBT netbios routines and daemon - version 2
6 Copyright (C) Jeremy Allison 1994-1998
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.
26 #define WINS_LIST "wins.dat"
28 extern int DEBUGLEVEL;
29 extern struct in_addr ipzero;
31 /****************************************************************************
32 Determine if this packet should be allocated to the WINS server.
33 *****************************************************************************/
35 BOOL packet_is_for_wins_server(struct packet_struct *packet)
37 struct nmb_packet *nmb = &packet->packet.nmb;
39 /* Only unicast packets go to a WINS server. */
40 if((wins_server_subnet == NULL) || (nmb->header.nm_flags.bcast == True))
42 DEBUG(10, ("packet_is_for_wins_server: failing WINS test #1.\n"));
46 /* Check for node status requests. */
47 if (nmb->question.question_type != QUESTION_TYPE_NB_QUERY)
50 switch(nmb->header.opcode)
53 * A WINS server issues WACKS, not receives them.
56 DEBUG(10, ("packet_is_for_wins_server: failing WINS test #2 (WACK).\n"));
59 * A WINS server only processes registration and
60 * release requests, not responses.
62 case NMB_NAME_REG_OPCODE:
63 case NMB_NAME_MULTIHOMED_REG_OPCODE:
64 case NMB_NAME_REFRESH_OPCODE_8: /* ambiguity in rfc1002 about which is correct. */
65 case NMB_NAME_REFRESH_OPCODE_9: /* WinNT uses 8 by default. */
66 if(nmb->header.response)
68 DEBUG(10, ("packet_is_for_wins_server: failing WINS test #3 (response = 1).\n"));
73 case NMB_NAME_RELEASE_OPCODE:
74 if(nmb->header.response)
76 DEBUG(10, ("packet_is_for_wins_server: failing WINS test #4 (response = 1).\n"));
82 * Only process unicast name queries with rd = 1.
84 case NMB_NAME_QUERY_OPCODE:
85 if(!nmb->header.response && !nmb->header.nm_flags.recursion_desired)
87 DEBUG(10, ("packet_is_for_wins_server: failing WINS test #5 (response = 1).\n"));
96 /****************************************************************************
97 Utility function to decide what ttl to give a register/refresh request.
98 *****************************************************************************/
100 static int get_ttl_from_packet(struct nmb_packet *nmb)
102 int ttl = nmb->additional->ttl;
104 if(ttl < lp_min_wins_ttl() )
105 ttl = lp_min_wins_ttl();
107 if(ttl > lp_max_wins_ttl() )
108 ttl = lp_max_wins_ttl();
113 /****************************************************************************
114 Load or create the WINS database.
115 *****************************************************************************/
117 BOOL initialise_wins(void)
120 time_t time_now = time(NULL);
124 if(!lp_we_are_a_wins_server())
127 add_samba_names_to_subnet(wins_server_subnet);
130 /* Setup the async dns. */
134 fstrcpy(fname,lp_lockdir());
135 trim_string(fname,NULL,"/");
137 strcat(fname,WINS_LIST);
139 if((fp = fopen(fname,"r")) == NULL)
141 DEBUG(2,("initialise_wins: Can't open wins database file %s. Error was %s\n",
142 fname, strerror(errno) ));
148 pstring name_str, ip_str, ttl_str, nb_flags_str;
149 unsigned int num_ips;
151 struct in_addr *ip_list;
155 enum name_source source;
162 /* Read a line from the wins.dat file. Strips whitespace
163 from the beginning and end of the line.
165 if (!fgets_slash(line,sizeof(pstring),fp))
174 * Now we handle multiple IP addresses per name we need
175 * to iterate over the line twice. The first time to
176 * determine how many IP addresses there are, the second
177 * time to actually parse them into the ip_list array.
180 if (!next_token(&ptr,name_str,NULL))
182 DEBUG(0,("initialise_wins: Failed to parse name when parsing line %s\n", line ));
186 if (!next_token(&ptr,ttl_str,NULL))
188 DEBUG(0,("initialise_wins: Failed to parse time to live when parsing line %s\n", line ));
193 * Determine the number of IP addresses per line.
198 got_token = next_token(&ptr,ip_str,NULL);
201 if(got_token && strchr(ip_str, '.'))
206 } while( got_token && was_ip);
210 DEBUG(0,("initialise_wins: Missing IP address when parsing line %s\n", line ));
216 DEBUG(0,("initialise_wins: Missing nb_flags when parsing line %s\n", line ));
220 /* Allocate the space for the ip_list. */
221 if((ip_list = (struct in_addr *)malloc( num_ips * sizeof(struct in_addr))) == NULL)
223 DEBUG(0,("initialise_wins: Malloc fail !\n"));
227 /* Reset and re-parse the line. */
229 next_token(&ptr,name_str,NULL);
230 next_token(&ptr,ttl_str,NULL);
231 for(i = 0; i < num_ips; i++)
233 next_token(&ptr, ip_str, NULL);
234 ip_list[i] = *interpret_addr2(ip_str);
235 if (ip_equal(ip_list[i], ipzero))
238 next_token(&ptr,nb_flags_str,NULL);
241 * Deal with SELF or REGISTER name encoding. Default is REGISTER
242 * for compatibility with old nmbds.
245 if(nb_flags_str[strlen(nb_flags_str)-1] == 'S')
247 DEBUG(5,("initialise_wins: Ignoring SELF name %s\n", line));
248 free((char *)ip_list);
252 if(nb_flags_str[strlen(nb_flags_str)-1] == 'R')
253 nb_flags_str[strlen(nb_flags_str)-1] = '\0';
255 /* Netbios name. # divides the name from the type (hex): netbios#xx */
256 pstrcpy(name,name_str);
258 if((p = strchr(name,'#')) != NULL)
261 sscanf(p+1,"%x",&type);
264 /* Decode the netbios flags (hex) and the time-to-live (in seconds). */
265 sscanf(nb_flags_str,"%x",&nb_flags);
266 sscanf(ttl_str,"%d",&ttl);
268 /* add all entries that have 60 seconds or more to live */
269 if ((ttl - 60) > time_now || ttl == PERMANENT_TTL)
271 struct name_record *namerec;
273 if(ttl != PERMANENT_TTL)
276 DEBUG(4, ("initialise_wins: add name: %s#%02x ttl = %d first IP %s flags = %2x\n",
277 name, type, ttl, inet_ntoa(ip_list[0]), nb_flags));
279 namerec = add_name_to_subnet(wins_server_subnet, name, type, nb_flags,
280 ttl, REGISTER_NAME, num_ips, ip_list);
285 DEBUG(4, ("initialise_wins: not adding name (ttl problem) %s#%02x ttl = %d first IP %s flags = %2x\n",
286 name, type, ttl, inet_ntoa(ip_list[0]), nb_flags));
289 free((char *)ip_list);
296 /****************************************************************************
297 Send a WINS WACK (Wait ACKnowledgement) response.
298 **************************************************************************/
300 static void send_wins_wack_response(int ttl, struct packet_struct *p)
302 struct nmb_packet *nmb = &p->packet.nmb;
303 unsigned char rdata[2];
305 rdata[0] = rdata[1] = 0;
307 /* Taken from nmblib.c - we need to send back almost
308 identical bytes from the requesting packet header. */
310 rdata[0] = (nmb->header.opcode & 0xF) << 3;
311 if (nmb->header.nm_flags.authoritative &&
312 nmb->header.response) rdata[0] |= 0x4;
313 if (nmb->header.nm_flags.trunc) rdata[0] |= 0x2;
314 if (nmb->header.nm_flags.recursion_desired) rdata[0] |= 0x1;
315 if (nmb->header.nm_flags.recursion_available &&
316 nmb->header.response) rdata[1] |= 0x80;
317 if (nmb->header.nm_flags.bcast) rdata[1] |= 0x10;
319 reply_netbios_packet(p, /* Packet to reply to. */
320 0, /* Result code. */
321 NMB_WAIT_ACK, /* nmbd type code. */
322 NMB_WACK_OPCODE, /* opcode. */
324 (char *)rdata, /* data to send. */
325 2); /* data length. */
328 /****************************************************************************
329 Send a WINS name registration response.
330 **************************************************************************/
332 static void send_wins_name_registration_response(int rcode, int ttl, struct packet_struct *p)
334 struct nmb_packet *nmb = &p->packet.nmb;
337 memcpy(&rdata[0], &nmb->additional->rdata[0], 6);
339 reply_netbios_packet(p, /* Packet to reply to. */
340 rcode, /* Result code. */
341 WINS_REG, /* nmbd type code. */
342 NMB_NAME_REG_OPCODE, /* opcode. */
344 rdata, /* data to send. */
345 6); /* data length. */
348 /***********************************************************************
349 Deal with a name refresh request to a WINS server.
350 ************************************************************************/
352 void wins_process_name_refresh_request(struct subnet_record *subrec,
353 struct packet_struct *p)
355 struct nmb_packet *nmb = &p->packet.nmb;
356 struct nmb_name *question = &nmb->question.question_name;
357 BOOL bcast = nmb->header.nm_flags.bcast;
358 uint16 nb_flags = get_nb_flags(nmb->additional->rdata);
359 BOOL group = (nb_flags & NB_GROUP) ? True : False;
360 struct name_record *namerec = NULL;
361 int ttl = get_ttl_from_packet(nmb);
362 struct in_addr from_ip;
364 putip((char *)&from_ip,&nmb->additional->rdata[2]);
369 * We should only get unicast name refresh packets here.
370 * Anyone trying to refresh broadcast should not be going to a WINS
371 * server. Log an error here.
374 DEBUG(0,("wins_process_name_refresh_request: broadcast name refresh request \
375 received for name %s from IP %s on subnet %s. Error - should not be sent to WINS server\n",
376 namestr(question), inet_ntoa(from_ip), subrec->subnet_name));
380 DEBUG(3,("wins_process_name_refresh_request: Name refresh for name %s \
381 IP %s\n", namestr(question), inet_ntoa(from_ip) ));
384 * See if the name already exists.
387 namerec = find_name_on_subnet(subrec, question, FIND_ANY_NAME);
390 * If this is a refresh request and the name doesn't exist then
396 DEBUG(3,("wins_process_name_refresh_request: Name refresh for name %s and \
397 the name does not exist.\n", namestr(question) ));
398 send_wins_name_registration_response(NAM_ERR, 0, p);
403 * Check that the group bits for the refreshing name and the
404 * name in our database match.
407 if((namerec != NULL) && ((group && !NAME_GROUP(namerec)) || (!group && NAME_GROUP(namerec))) )
409 DEBUG(3,("wins_process_name_refresh_request: Name %s group bit = %s \
410 does not match group bit in WINS for this name.\n", namestr(question), group ? "True" : "False" ));
411 send_wins_name_registration_response(RFS_ERR, 0, p);
416 * For a unique name check that the person refreshing the name is one of the registered IP
417 * addresses. If not - fail the refresh. Do the same for group names with a type of 0x1c.
418 * Just return success for unique 0x1d refreshes. For normal group names update the ttl
419 * and return success.
422 if((!group || (group && (question->name_type == 0x1c))) && find_ip_in_name_record(namerec, from_ip ))
427 update_name_ttl(namerec, ttl);
428 send_wins_name_registration_response(0, ttl, p);
434 * Normal groups are all registered with an IP address of 255.255.255.255
435 * so we can't search for the IP address.
437 update_name_ttl(namerec, ttl);
438 send_wins_name_registration_response(0, ttl, p);
441 else if(!group && (question->name_type == 0x1d))
444 * Special name type - just pretend the refresh succeeded.
446 send_wins_name_registration_response(0, ttl, p);
455 DEBUG(3,("wins_process_name_refresh_request: Name refresh for name %s with IP %s and \
456 is IP is not known to the name.\n", namestr(question), inet_ntoa(from_ip) ));
457 send_wins_name_registration_response(RFS_ERR, 0, p);
462 /***********************************************************************
463 Deal with a name registration request query success to a client that
466 We have a locked pointer to the original packet stashed away in the
467 userdata pointer. The success here is actually a failure as it means
468 the client we queried wants to keep the name, so we must return
469 a registration failure to the original requestor.
470 ************************************************************************/
472 static void wins_register_query_success(struct subnet_record *subrec,
473 struct userdata_struct *userdata,
474 struct nmb_name *question_name,
476 struct res_rec *answers)
478 struct packet_struct *orig_reg_packet;
480 memcpy((char *)&orig_reg_packet, userdata->data, sizeof(struct packet_struct *));
482 DEBUG(3,("wins_register_query_success: Original client at IP %s still wants the \
483 name %s. Rejecting registration request.\n", inet_ntoa(ip), namestr(question_name) ));
485 send_wins_name_registration_response(RFS_ERR, 0, orig_reg_packet);
487 orig_reg_packet->locked = False;
488 free_packet(orig_reg_packet);
491 /***********************************************************************
492 Deal with a name registration request query failure to a client that
495 We have a locked pointer to the original packet stashed away in the
496 userdata pointer. The failure here is actually a success as it means
497 the client we queried didn't want to keep the name, so we can remove
498 the old name record and then successfully add the new name.
499 ************************************************************************/
501 static void wins_register_query_fail(struct subnet_record *subrec,
502 struct response_record *rrec,
503 struct nmb_name *question_name,
506 struct userdata_struct *userdata = rrec->userdata;
507 struct packet_struct *orig_reg_packet;
508 struct nmb_packet *nmb;
509 struct name_record *namerec = NULL;
513 memcpy((char *)&orig_reg_packet, userdata->data, sizeof(struct packet_struct *));
515 nmb = &orig_reg_packet->packet.nmb;
517 nb_flags = get_nb_flags(nmb->additional->rdata);
518 group = (nb_flags & NB_GROUP) ? True : False;
521 * We want to just add the name, as we now know the original owner
522 * didn't want it. But we can't just do that as an arbitary
523 * amount of time may have taken place between the name query
524 * request and this timeout/error response. So we check that
525 * the name still exists and is in the same state - if so
526 * we remove it and call wins_process_name_registration_request()
527 * as we know it will do the right thing now.
530 namerec = find_name_on_subnet(subrec, question_name, FIND_ANY_NAME);
532 if((namerec != NULL) && (namerec->source == REGISTER_NAME) &&
533 ip_equal(rrec->packet->ip, *namerec->ip) )
535 remove_name_from_namelist( subrec, namerec);
540 wins_process_name_registration_request(subrec, orig_reg_packet);
542 DEBUG(2,("wins_register_query_fail: The state of the WINS database changed between \
543 querying for name %s in order to replace it and this reply.\n", namestr(question_name) ));
545 orig_reg_packet->locked = False;
546 free_packet(orig_reg_packet);
549 /***********************************************************************
550 Deal with a name registration request to a WINS server.
552 Use the following pseudocode :
560 | +--- existing name is group
563 | | +--- add name (return).
566 | +--- exiting name is unique
569 | +--- query existing owner (return).
572 +--------name doesn't exist
575 +--- add name (return).
583 | +--- existing name is group
586 | | +--- fail add (return).
589 | +--- exiting name is unique
592 | +--- query existing owner (return).
595 +--------name doesn't exist
598 +--- add name (return).
600 As can be seen from the above, the two cases may be collapsed onto each
601 other with the exception of the case where the name already exists and
602 is a group name. This case we handle with an if statement.
604 ************************************************************************/
606 void wins_process_name_registration_request(struct subnet_record *subrec,
607 struct packet_struct *p)
609 struct nmb_packet *nmb = &p->packet.nmb;
610 struct nmb_name *question = &nmb->question.question_name;
611 BOOL bcast = nmb->header.nm_flags.bcast;
612 uint16 nb_flags = get_nb_flags(nmb->additional->rdata);
613 int ttl = get_ttl_from_packet(nmb);
614 struct name_record *namerec = NULL;
615 struct in_addr from_ip;
616 BOOL registering_group_name = (nb_flags & NB_GROUP) ? True : False;;
618 putip((char *)&from_ip,&nmb->additional->rdata[2]);
623 * We should only get unicast name registration packets here.
624 * Anyone trying to register broadcast should not be going to a WINS
625 * server. Log an error here.
628 DEBUG(0,("wins_process_name_registration_request: broadcast name registration request \
629 received for name %s from IP %s on subnet %s. Error - should not be sent to WINS server\n",
630 namestr(question), inet_ntoa(from_ip), subrec->subnet_name));
634 DEBUG(3,("wins_process_name_registration_request: %s name registration for name %s \
635 IP %s\n", registering_group_name ? "Group" : "Unique", namestr(question), inet_ntoa(from_ip) ));
638 * See if the name already exists.
641 namerec = find_name_on_subnet(subrec, question, FIND_ANY_NAME);
644 * Deal with the case where the name found was a dns entry.
645 * Remove it as we now have a NetBIOS client registering the
649 if((namerec != NULL) && ((namerec->source == DNS_NAME) || (namerec->source == DNSFAIL_NAME)))
651 DEBUG(5,("wins_process_name_registration_request: Name (%s) in WINS was a dns lookup \
652 - removing it.\n", namestr(question) ));
653 remove_name_from_namelist( subrec, namerec);
658 * Reject if the name exists and is not a REGISTER_NAME.
659 * (ie. Don't allow any static names to be overwritten.
662 if((namerec != NULL) && (namerec->source != REGISTER_NAME))
664 DEBUG(3,("wins_process_name_registration_request: Attempt to register name %s. Name \
665 already exists in WINS with source type %d.\n", namestr(question), namerec->source ));
666 send_wins_name_registration_response(RFS_ERR, 0, p);
671 * Special policy decisions based on MS documentation.
672 * 1). All group names (except names ending in 0x1c) are added as 255.255.255.255.
673 * 2). All unique names ending in 0x1d are ignored, although a positive response is sent.
677 * A group name is always added as the local broadcast address, except
678 * for group names ending in 0x1c.
679 * Group names with type 0x1c are registered with individual IP addresses.
682 if(registering_group_name && (question->name_type != 0x1c))
683 from_ip = *interpret_addr2("255.255.255.255");
686 * Ignore all attempts to register a unique 0x1d name, although return success.
689 if(!registering_group_name && (question->name_type == 0x1d))
691 DEBUG(3,("wins_process_name_registration_request: Ignoring request \
692 to register name %s from IP %s.", namestr(question), inet_ntoa(p->ip) ));
693 send_wins_name_registration_response(0, ttl, p);
698 * Next two cases are the 'if statement' mentioned above.
701 if((namerec != NULL) && NAME_GROUP(namerec))
703 if(registering_group_name)
706 * If we are adding a group name, the name exists and is also a group entry just add this
707 * IP address to it and update the ttl.
710 DEBUG(3,("wins_process_name_registration_request: Adding IP %s to group name %s.\n",
711 inet_ntoa(from_ip), namestr(question) ));
713 * Check the ip address is not already in the group.
715 if(!find_ip_in_name_record(namerec, from_ip))
716 add_ip_to_name_record(namerec, from_ip);
717 update_name_ttl(namerec, ttl);
718 send_wins_name_registration_response(0, ttl, p);
724 * If we are adding a unique name, the name exists in the WINS db
725 * and is a group name then reject the registration.
728 DEBUG(3,("wins_process_name_registration_request: Attempt to register name %s. Name \
729 already exists in WINS as a GROUP name.\n", namestr(question) ));
730 send_wins_name_registration_response(RFS_ERR, 0, p);
736 * From here on down we know that if the name exists in the WINS db it is
737 * a unique name, not a group name.
741 * If the name exists and is one of our names then check the
742 * registering IP address. If it's not one of ours then automatically
743 * reject without doing the query - we know we will reject it.
746 if((namerec != NULL) && (is_myname(namerec->name.name)) )
750 DEBUG(3,("wins_process_name_registration_request: Attempt to register name %s. Name \
751 is one of our (WINS server) names. Denying registration.\n", namestr(question) ));
752 send_wins_name_registration_response(RFS_ERR, 0, p);
758 * It's one of our names and one of our IP's - update the ttl.
760 update_name_ttl(namerec, ttl);
761 send_wins_name_registration_response(0, ttl, p);
767 * If the name exists and it is a unique registration and the registering IP
768 * is the same as the the (single) already registered IP then just update the ttl.
771 if(!registering_group_name && (namerec != NULL) && (namerec->num_ips == 1) &&
772 ip_equal(namerec->ip[0], from_ip))
774 update_name_ttl(namerec, ttl);
775 send_wins_name_registration_response(0, ttl, p);
780 * Finally if the name exists do a query to the registering machine
781 * to see if they still claim to have the name.
786 char ud[sizeof(struct userdata_struct) + sizeof(struct packet_struct *)];
787 struct userdata_struct *userdata = (struct userdata_struct *)ud;
790 * First send a WACK to the registering machine.
793 send_wins_wack_response(60, p);
796 * When the reply comes back we need the original packet.
797 * Lock this so it won't be freed and then put it into
798 * the userdata structure.
803 userdata = (struct userdata_struct *)ud;
805 userdata->copy_fn = NULL;
806 userdata->free_fn = NULL;
807 userdata->userdata_len = sizeof(struct packet_struct *);
808 memcpy(userdata->data, (char *)&p, sizeof(struct packet_struct *) );
811 * Use the new call to send a query directly to an IP address.
812 * This sends the query directly to the IP address, and ensures
813 * the recursion desired flag is not set (you were right Luke :-).
814 * This function should *only* be called from the WINS server
818 query_name_from_wins_server( *namerec->ip, question->name, question->name_type,
819 wins_register_query_success,
820 wins_register_query_fail,
826 * Name did not exist - add it.
829 add_name_to_subnet(subrec, question->name, question->name_type,
830 nb_flags, ttl, REGISTER_NAME, 1, &from_ip);
832 send_wins_name_registration_response(0, ttl, p);
835 /***********************************************************************
836 Deal with a mutihomed name query success to the machine that
837 requested the multihomed name registration.
839 We have a locked pointer to the original packet stashed away in the
841 ************************************************************************/
843 static void wins_multihomed_register_query_success(struct subnet_record *subrec,
844 struct userdata_struct *userdata,
845 struct nmb_name *question_name,
847 struct res_rec *answers)
849 struct packet_struct *orig_reg_packet;
850 struct nmb_packet *nmb;
851 struct name_record *namerec = NULL;
852 struct in_addr from_ip;
855 memcpy((char *)&orig_reg_packet, userdata->data, sizeof(struct packet_struct *));
857 nmb = &orig_reg_packet->packet.nmb;
859 putip((char *)&from_ip,&nmb->additional->rdata[2]);
860 ttl = get_ttl_from_packet(nmb);
863 * We want to just add the new IP, as we now know the requesting
864 * machine claims to own it. But we can't just do that as an arbitary
865 * amount of time may have taken place between the name query
866 * request and this response. So we check that
867 * the name still exists and is in the same state - if so
868 * we just add the extra IP and update the ttl.
871 namerec = find_name_on_subnet(subrec, question_name, FIND_ANY_NAME);
873 if( (namerec == NULL) || (namerec->source != REGISTER_NAME) )
875 DEBUG(3,("wins_multihomed_register_query_success: name %s is not in the correct state to add \
876 a subsequent IP addess.\n", namestr(question_name) ));
877 send_wins_name_registration_response(RFS_ERR, 0, orig_reg_packet);
881 if(!find_ip_in_name_record(namerec, from_ip))
882 add_ip_to_name_record(namerec, from_ip);
883 update_name_ttl(namerec, ttl);
884 send_wins_name_registration_response(0, ttl, orig_reg_packet);
888 /***********************************************************************
889 Deal with a name registration request query failure to a client that
892 We have a locked pointer to the original packet stashed away in the
894 ************************************************************************/
896 static void wins_multihomed_register_query_fail(struct subnet_record *subrec,
897 struct response_record *rrec,
898 struct nmb_name *question_name,
901 struct userdata_struct *userdata = rrec->userdata;
902 struct packet_struct *orig_reg_packet;
904 memcpy((char *)&orig_reg_packet, userdata->data, sizeof(struct packet_struct *));
906 DEBUG(3,("wins_multihomed_register_query_fail: Registering machine at IP %s failed to answer \
907 query successfully for name %s.\n", inet_ntoa(orig_reg_packet->ip), namestr(question_name) ));
908 send_wins_name_registration_response(RFS_ERR, 0, orig_reg_packet);
912 /***********************************************************************
913 Deal with a multihomed name registration request to a WINS server.
914 These cannot be group name registrations.
915 ***********************************************************************/
917 void wins_process_multihomed_name_registration_request( struct subnet_record *subrec,
918 struct packet_struct *p)
920 struct nmb_packet *nmb = &p->packet.nmb;
921 struct nmb_name *question = &nmb->question.question_name;
922 BOOL bcast = nmb->header.nm_flags.bcast;
923 uint16 nb_flags = get_nb_flags(nmb->additional->rdata);
924 int ttl = get_ttl_from_packet(nmb);
925 struct name_record *namerec = NULL;
926 struct in_addr from_ip;
927 BOOL group = (nb_flags & NB_GROUP) ? True : False;;
929 putip((char *)&from_ip,&nmb->additional->rdata[2]);
934 * We should only get unicast name registration packets here.
935 * Anyone trying to register broadcast should not be going to a WINS
936 * server. Log an error here.
939 DEBUG(0,("wins_process_multihomed_name_registration_request: broadcast name registration request \
940 received for name %s from IP %s on subnet %s. Error - should not be sent to WINS server\n",
941 namestr(question), inet_ntoa(from_ip), subrec->subnet_name));
946 * Only unique names should be registered multihomed.
951 DEBUG(0,("wins_process_multihomed_name_registration_request: group name registration request \
952 received for name %s from IP %s on subnet %s. Errror - group names should not be multihomed.\n",
953 namestr(question), inet_ntoa(from_ip), subrec->subnet_name));
957 DEBUG(3,("wins_process_multihomed_name_registration_request: name registration for name %s \
958 IP %s\n", namestr(question), inet_ntoa(from_ip) ));
961 * Deal with policy regarding 0x1d names.
964 if(question->name_type == 0x1d)
966 DEBUG(3,("wins_process_multihomed_name_registration_request: Ignoring request \
967 to register name %s from IP %s.", namestr(question), inet_ntoa(p->ip) ));
968 send_wins_name_registration_response(0, ttl, p);
973 * See if the name already exists.
976 namerec = find_name_on_subnet(subrec, question, FIND_ANY_NAME);
979 * Deal with the case where the name found was a dns entry.
980 * Remove it as we now have a NetBIOS client registering the
984 if((namerec != NULL) && ((namerec->source == DNS_NAME) || (namerec->source == DNSFAIL_NAME)))
986 DEBUG(5,("wins_process_multihomed_name_registration_request: Name (%s) in WINS was a dns lookup \
987 - removing it.\n", namestr(question) ));
988 remove_name_from_namelist( subrec, namerec);
993 * Reject if the name exists and is not a REGISTER_NAME.
994 * (ie. Don't allow any static names to be overwritten.
997 if((namerec != NULL) && (namerec->source != REGISTER_NAME))
999 DEBUG(3,("wins_process_multihomed_name_registration_request: Attempt to register name %s. Name \
1000 already exists in WINS with source type %d.\n", namestr(question), namerec->source ));
1001 send_wins_name_registration_response(RFS_ERR, 0, p);
1006 * Reject if the name exists and is a GROUP name.
1009 if((namerec != NULL) && NAME_GROUP(namerec))
1011 DEBUG(3,("wins_process_multihomed_name_registration_request: Attempt to register name %s. Name \
1012 already exists in WINS as a GROUP name.\n", namestr(question) ));
1013 send_wins_name_registration_response(RFS_ERR, 0, p);
1018 * From here on down we know that if the name exists in the WINS db it is
1019 * a unique name, not a group name.
1023 * If the name exists and is one of our names then check the
1024 * registering IP address. If it's not one of ours then automatically
1025 * reject without doing the query - we know we will reject it.
1028 if((namerec != NULL) && (is_myname(namerec->name.name)) )
1030 if(!ismyip(from_ip))
1032 DEBUG(3,("wins_process_multihomed_name_registration_request: Attempt to register name %s. Name \
1033 is one of our (WINS server) names. Denying registration.\n", namestr(question) ));
1034 send_wins_name_registration_response(RFS_ERR, 0, p);
1040 * It's one of our names and one of our IP's. Ensure the IP is in the record and
1043 if(!find_ip_in_name_record(namerec, from_ip))
1044 add_ip_to_name_record(namerec, from_ip);
1045 update_name_ttl(namerec, ttl);
1046 send_wins_name_registration_response(0, ttl, p);
1052 * If the name exists check if the IP address is already registered
1053 * to that name. If so then update the ttl and reply success.
1056 if((namerec != NULL) && find_ip_in_name_record(namerec, from_ip))
1058 update_name_ttl(namerec, ttl);
1059 send_wins_name_registration_response(0, ttl, p);
1064 * If the name exists do a query to the owner
1065 * to see if they still want the name.
1070 char ud[sizeof(struct userdata_struct) + sizeof(struct packet_struct *)];
1071 struct userdata_struct *userdata = (struct userdata_struct *)ud;
1074 * First send a WACK to the registering machine.
1077 send_wins_wack_response(60, p);
1080 * When the reply comes back we need the original packet.
1081 * Lock this so it won't be freed and then put it into
1082 * the userdata structure.
1087 userdata = (struct userdata_struct *)ud;
1089 userdata->copy_fn = NULL;
1090 userdata->free_fn = NULL;
1091 userdata->userdata_len = sizeof(struct packet_struct *);
1092 memcpy(userdata->data, (char *)&p, sizeof(struct packet_struct *) );
1095 * As query_name uses the subnet broadcast address as the destination
1096 * of the packet we temporarily change the subnet broadcast address to
1097 * be the IP address of the requesting machine and send the packet. This
1098 * is a *horrible* hack but the alternative is to add the destination
1099 * address parameter to all query_name() calls. I hate this code :-).
1102 subrec->bcast_ip = p->ip;
1103 query_name( subrec, question->name, question->name_type,
1104 wins_multihomed_register_query_success,
1105 wins_multihomed_register_query_fail,
1107 subrec->bcast_ip = ipzero;
1112 * Name did not exist - add it.
1115 add_name_to_subnet(subrec, question->name, question->name_type,
1116 nb_flags, ttl, REGISTER_NAME, 1, &from_ip);
1118 send_wins_name_registration_response(0, ttl, p);
1121 /***********************************************************************
1122 Deal with the special name query for *<1b>.
1123 ***********************************************************************/
1125 static void process_wins_dmb_query_request(struct subnet_record *subrec,
1126 struct packet_struct *p)
1128 struct name_record *namerec = NULL;
1133 * Go through all the names in the WINS db looking for those
1134 * ending in <1b>. Use this to calculate the number of IP
1135 * addresses we need to return.
1139 for(namerec = subrec->namelist; namerec; namerec = namerec->next)
1141 if(namerec->name.name_type == 0x1b)
1142 num_ips += namerec->num_ips;
1148 * There are no 0x1b names registered. Return name query fail.
1150 send_wins_name_query_response(NAM_ERR, p, NULL);
1154 if((prdata = (char *)malloc( num_ips * 6 )) == NULL)
1156 DEBUG(0,("process_wins_dmb_query_request: Malloc fail !.\n"));
1161 * Go through all the names again in the WINS db looking for those
1162 * ending in <1b>. Add their IP addresses into the list we will
1167 for(namerec = subrec->namelist; namerec; namerec = namerec->next)
1169 if(namerec->name.name_type == 0x1b)
1172 for(i = 0; i < namerec->num_ips; i++)
1174 set_nb_flags(&prdata[num_ips * 6],namerec->nb_flags);
1175 putip((char *)&prdata[(num_ips * 6) + 2], &namerec->ip[i]);
1182 * Send back the reply containing the IP list.
1185 reply_netbios_packet(p, /* Packet to reply to. */
1186 0, /* Result code. */
1187 WINS_QUERY, /* nmbd type code. */
1188 NMB_NAME_QUERY_OPCODE, /* opcode. */
1189 lp_min_wins_ttl(), /* ttl. */
1190 prdata, /* data to send. */
1191 num_ips*6); /* data length. */
1196 /****************************************************************************
1197 Send a WINS name query response.
1198 **************************************************************************/
1200 void send_wins_name_query_response(int rcode, struct packet_struct *p,
1201 struct name_record *namerec)
1204 char *prdata = rdata;
1205 int reply_data_len = 0;
1214 int same_net_index = -1;
1216 ttl = (namerec->death_time != PERMANENT_TTL) ?
1217 namerec->death_time - p->timestamp : lp_max_wins_ttl();
1219 /* Copy all known ip addresses into the return data. */
1220 /* Optimise for the common case of one IP address so
1221 we don't need a malloc. */
1223 if(namerec->num_ips == 1 )
1227 if((prdata = (char *)malloc( namerec->num_ips * 6 )) == NULL)
1229 DEBUG(0,("send_wins_name_query_response: malloc fail !\n"));
1234 * Look over the known IP addresses and see if one of them
1235 * is on the same (local) net as the requesting IP address. If so then
1236 * put that IP address into the packet as the first IP.
1237 * We can only do this for local nets as they're the only
1238 * ones we know the netmask for.
1243 if(is_local_net(p->ip))
1245 struct in_addr *n_mask = iface_nmask(p->ip);
1247 for( j = 0; j < namerec->num_ips; j++)
1249 if(same_net( namerec->ip[j], p->ip, *n_mask))
1251 set_nb_flags(&prdata[0],namerec->nb_flags);
1252 putip((char *)&prdata[2], &namerec->ip[j]);
1260 for(j = 0; j < namerec->num_ips; j++)
1262 if(j == same_net_index)
1264 set_nb_flags(&prdata[i*6],namerec->nb_flags);
1265 putip((char *)&prdata[2+(i*6)], &namerec->ip[j]);
1268 reply_data_len = namerec->num_ips * 6;
1272 reply_netbios_packet(p, /* Packet to reply to. */
1273 rcode, /* Result code. */
1274 WINS_QUERY, /* nmbd type code. */
1275 NMB_NAME_QUERY_OPCODE, /* opcode. */
1277 prdata, /* data to send. */
1278 reply_data_len); /* data length. */
1280 if((prdata != rdata) && (prdata != NULL))
1284 /***********************************************************************
1285 Deal with a name query.
1286 ***********************************************************************/
1288 void wins_process_name_query_request(struct subnet_record *subrec,
1289 struct packet_struct *p)
1291 struct nmb_packet *nmb = &p->packet.nmb;
1292 struct nmb_name *question = &nmb->question.question_name;
1293 struct name_record *namerec = NULL;
1295 DEBUG(3,("wins_process_name_query: name query for name %s from IP %s\n",
1296 namestr(question), inet_ntoa(p->ip) ));
1299 * Special name code. If the queried name is *<1b> then search
1300 * the entire WINS database and return a list of all the IP addresses
1301 * registered to any <1b> name. This is to allow domain master browsers
1302 * to discover other domains that may not have a presence on their subnet.
1305 if(strequal( question->name, "*") && (question->name_type == 0x1b))
1307 process_wins_dmb_query_request( subrec, p);
1311 namerec = find_name_on_subnet(subrec, question, FIND_ANY_NAME);
1316 * If it's a DNSFAIL_NAME then reply name not found.
1319 if(namerec->source == DNSFAIL_NAME)
1321 DEBUG(3,("wins_process_name_query: name query for name %s returning DNS fail.\n",
1322 namestr(question) ));
1323 send_wins_name_query_response(NAM_ERR, p, namerec);
1328 * If the name has expired then reply name not found.
1331 if((namerec->death_time != PERMANENT_TTL) && (namerec->death_time < p->timestamp))
1333 DEBUG(3,("wins_process_name_query: name query for name %s - name expired. Returning fail.\n",
1334 namestr(question) ));
1335 send_wins_name_query_response(NAM_ERR, p, namerec);
1339 DEBUG(3,("wins_process_name_query: name query for name %s returning first IP %s.\n",
1340 namestr(question), inet_ntoa(namerec->ip[0]) ));
1342 send_wins_name_query_response(0, p, namerec);
1347 * Name not found in WINS - try a dns query if it's a 0x20 name.
1350 if(lp_dns_proxy() &&
1351 ((question->name_type == 0x20) || question->name_type == 0))
1354 DEBUG(3,("wins_process_name_query: name query for name %s not found - doing dns lookup.\n",
1355 namestr(question) ));
1357 queue_dns_query(p, question, &namerec);
1362 * Name not found - return error.
1365 send_wins_name_query_response(NAM_ERR, p, NULL);
1368 /****************************************************************************
1369 Send a WINS name release response.
1370 **************************************************************************/
1372 static void send_wins_name_release_response(int rcode, struct packet_struct *p)
1374 struct nmb_packet *nmb = &p->packet.nmb;
1377 memcpy(&rdata[0], &nmb->additional->rdata[0], 6);
1379 reply_netbios_packet(p, /* Packet to reply to. */
1380 rcode, /* Result code. */
1381 NMB_REL, /* nmbd type code. */
1382 NMB_NAME_RELEASE_OPCODE, /* opcode. */
1384 rdata, /* data to send. */
1385 6); /* data length. */
1388 /***********************************************************************
1389 Deal with a name release.
1390 ***********************************************************************/
1392 void wins_process_name_release_request(struct subnet_record *subrec,
1393 struct packet_struct *p)
1395 struct nmb_packet *nmb = &p->packet.nmb;
1396 struct nmb_name *question = &nmb->question.question_name;
1397 BOOL bcast = nmb->header.nm_flags.bcast;
1398 uint16 nb_flags = get_nb_flags(nmb->additional->rdata);
1399 struct name_record *namerec = NULL;
1400 struct in_addr from_ip;
1401 BOOL releasing_group_name = (nb_flags & NB_GROUP) ? True : False;;
1403 putip((char *)&from_ip,&nmb->additional->rdata[2]);
1408 * We should only get unicast name registration packets here.
1409 * Anyone trying to register broadcast should not be going to a WINS
1410 * server. Log an error here.
1413 DEBUG(0,("wins_process_name_release_request: broadcast name registration request \
1414 received for name %s from IP %s on subnet %s. Error - should not be sent to WINS server\n",
1415 namestr(question), inet_ntoa(from_ip), subrec->subnet_name));
1419 DEBUG(3,("wins_process_name_release_request: %s name release for name %s \
1420 IP %s\n", releasing_group_name ? "Group" : "Unique", namestr(question), inet_ntoa(from_ip) ));
1423 * Deal with policy regarding 0x1d names.
1426 if(!releasing_group_name && (question->name_type == 0x1d))
1428 DEBUG(3,("wins_process_name_release_request: Ignoring request \
1429 to release name %s from IP %s.", namestr(question), inet_ntoa(p->ip) ));
1430 send_wins_name_release_response(0, p);
1435 * See if the name already exists.
1438 namerec = find_name_on_subnet(subrec, question, FIND_ANY_NAME);
1440 if((namerec == NULL) || ((namerec != NULL) && (namerec->source != REGISTER_NAME)) )
1442 send_wins_name_release_response(NAM_ERR, p);
1447 * Check that the sending machine has permission to release this name.
1448 * If it's a group name not ending in 0x1c then just say yes and let
1449 * the group time out.
1452 if(releasing_group_name && (question->name_type != 0x1c))
1454 send_wins_name_release_response(0, p);
1459 * Check that the releasing node is on the list of IP addresses
1460 * for this name. Disallow the release if not.
1463 if(!find_ip_in_name_record(namerec, from_ip))
1465 DEBUG(3,("wins_process_name_release_request: Refusing request to \
1466 release name %s as IP %s is not one of the known IP's for this name.\n",
1467 namestr(question), inet_ntoa(from_ip) ));
1468 send_wins_name_release_response(NAM_ERR, p);
1473 * Release the name and then remove the IP from the known list.
1476 send_wins_name_release_response(0, p);
1477 remove_ip_from_name_record(namerec, from_ip);
1480 * Remove the name entirely if no IP addresses left.
1482 if (namerec->num_ips == 0)
1483 remove_name_from_namelist(subrec, namerec);
1487 /*******************************************************************
1488 WINS time dependent processing.
1489 ******************************************************************/
1491 void initiate_wins_processing(time_t t)
1493 static time_t lasttime = 0;
1497 if (t - lasttime < 5)
1500 if(!lp_we_are_a_wins_server())
1503 expire_names_on_subnet(wins_server_subnet, t);
1505 if(wins_server_subnet->namelist_changed)
1506 wins_write_database();
1508 wins_server_subnet->namelist_changed = False;
1511 /*******************************************************************
1512 Write out the current WINS database.
1513 ******************************************************************/
1515 void wins_write_database(void)
1517 struct name_record *namerec;
1518 fstring fname, fnamenew;
1522 if(!lp_we_are_a_wins_server())
1525 fstrcpy(fname,lp_lockdir());
1526 trim_string(fname,NULL,"/");
1528 strcat(fname,WINS_LIST);
1529 fstrcpy(fnamenew,fname);
1530 strcat(fnamenew,".");
1532 if((fp = fopen(fnamenew,"w")) == NULL)
1534 DEBUG(0,("wins_write_database: Can't open %s. Error was %s\n", fnamenew, strerror(errno)));
1538 DEBUG(4,("wins_write_database: Dump of WINS name list.\n"));
1540 for (namerec = wins_server_subnet->namelist; namerec; namerec = namerec->next)
1545 DEBUG(4,("%-19s ", namestr(&namerec->name) ));
1547 if(namerec->death_time != PERMANENT_TTL)
1549 tm = LocalTime(&namerec->death_time);
1550 DEBUG(4,("TTL = %s", asctime(tm) ));
1553 DEBUG(4,("TTL = PERMANENT\t"));
1555 for (i = 0; i < namerec->num_ips; i++)
1556 DEBUG(4,("%15s ", inet_ntoa(namerec->ip[i]) ));
1557 DEBUG(4,("%2x\n", namerec->nb_flags ));
1559 if (namerec->source == REGISTER_NAME)
1561 fprintf(fp, "\"%s#%02x\" %d ",
1562 namerec->name.name,namerec->name.name_type, /* Ignore scope. */
1563 (int)namerec->death_time);
1565 for (i = 0; i < namerec->num_ips; i++)
1566 fprintf(fp, "%s ", inet_ntoa(namerec->ip[i]));
1567 fprintf(fp, "%2xR\n", namerec->nb_flags);
1573 chmod(fnamenew,0644);
1574 rename(fnamenew,fname);