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 pstrcpy(fname,lp_lockdir());
135 trim_string(fname,NULL,"/");
137 pstrcat(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 if(ttl != PERMANENT_TTL)
274 DEBUG( 4, ("initialise_wins: add name: %s#%02x ttl = %d first IP %s flags = %2x\n",
275 name, type, ttl, inet_ntoa(ip_list[0]), nb_flags));
277 (void)add_name_to_subnet( wins_server_subnet, name, type, nb_flags,
278 ttl, REGISTER_NAME, num_ips, ip_list );
283 DEBUG(4, ("initialise_wins: not adding name (ttl problem) %s#%02x ttl = %d first IP %s flags = %2x\n",
284 name, type, ttl, inet_ntoa(ip_list[0]), nb_flags));
287 free((char *)ip_list);
294 /****************************************************************************
295 Send a WINS WACK (Wait ACKnowledgement) response.
296 **************************************************************************/
298 static void send_wins_wack_response(int ttl, struct packet_struct *p)
300 struct nmb_packet *nmb = &p->packet.nmb;
301 unsigned char rdata[2];
303 rdata[0] = rdata[1] = 0;
305 /* Taken from nmblib.c - we need to send back almost
306 identical bytes from the requesting packet header. */
308 rdata[0] = (nmb->header.opcode & 0xF) << 3;
309 if (nmb->header.nm_flags.authoritative &&
310 nmb->header.response) rdata[0] |= 0x4;
311 if (nmb->header.nm_flags.trunc) rdata[0] |= 0x2;
312 if (nmb->header.nm_flags.recursion_desired) rdata[0] |= 0x1;
313 if (nmb->header.nm_flags.recursion_available &&
314 nmb->header.response) rdata[1] |= 0x80;
315 if (nmb->header.nm_flags.bcast) rdata[1] |= 0x10;
317 reply_netbios_packet(p, /* Packet to reply to. */
318 0, /* Result code. */
319 NMB_WAIT_ACK, /* nmbd type code. */
320 NMB_WACK_OPCODE, /* opcode. */
322 (char *)rdata, /* data to send. */
323 2); /* data length. */
326 /****************************************************************************
327 Send a WINS name registration response.
328 **************************************************************************/
330 static void send_wins_name_registration_response(int rcode, int ttl, struct packet_struct *p)
332 struct nmb_packet *nmb = &p->packet.nmb;
335 memcpy(&rdata[0], &nmb->additional->rdata[0], 6);
337 reply_netbios_packet(p, /* Packet to reply to. */
338 rcode, /* Result code. */
339 WINS_REG, /* nmbd type code. */
340 NMB_NAME_REG_OPCODE, /* opcode. */
342 rdata, /* data to send. */
343 6); /* data length. */
346 /***********************************************************************
347 Deal with a name refresh request to a WINS server.
348 ************************************************************************/
350 void wins_process_name_refresh_request(struct subnet_record *subrec,
351 struct packet_struct *p)
353 struct nmb_packet *nmb = &p->packet.nmb;
354 struct nmb_name *question = &nmb->question.question_name;
355 BOOL bcast = nmb->header.nm_flags.bcast;
356 uint16 nb_flags = get_nb_flags(nmb->additional->rdata);
357 BOOL group = (nb_flags & NB_GROUP) ? True : False;
358 struct name_record *namerec = NULL;
359 int ttl = get_ttl_from_packet(nmb);
360 struct in_addr from_ip;
362 putip((char *)&from_ip,&nmb->additional->rdata[2]);
367 * We should only get unicast name refresh packets here.
368 * Anyone trying to refresh broadcast should not be going to a WINS
369 * server. Log an error here.
372 DEBUG(0,("wins_process_name_refresh_request: broadcast name refresh request \
373 received for name %s from IP %s on subnet %s. Error - should not be sent to WINS server\n",
374 namestr(question), inet_ntoa(from_ip), subrec->subnet_name));
378 DEBUG(3,("wins_process_name_refresh_request: Name refresh for name %s \
379 IP %s\n", namestr(question), inet_ntoa(from_ip) ));
382 * See if the name already exists.
385 namerec = find_name_on_subnet(subrec, question, FIND_ANY_NAME);
388 * If this is a refresh request and the name doesn't exist then
394 DEBUG(3,("wins_process_name_refresh_request: Name refresh for name %s and \
395 the name does not exist.\n", namestr(question) ));
396 send_wins_name_registration_response(NAM_ERR, 0, p);
401 * Check that the group bits for the refreshing name and the
402 * name in our database match.
405 if((namerec != NULL) && ((group && !NAME_GROUP(namerec)) || (!group && NAME_GROUP(namerec))) )
407 DEBUG(3,("wins_process_name_refresh_request: Name %s group bit = %s \
408 does not match group bit in WINS for this name.\n", namestr(question), group ? "True" : "False" ));
409 send_wins_name_registration_response(RFS_ERR, 0, p);
414 * For a unique name check that the person refreshing the name is one of the registered IP
415 * addresses. If not - fail the refresh. Do the same for group names with a type of 0x1c.
416 * Just return success for unique 0x1d refreshes. For normal group names update the ttl
417 * and return success.
420 if((!group || (group && (question->name_type == 0x1c))) && find_ip_in_name_record(namerec, from_ip ))
425 update_name_ttl(namerec, ttl);
426 send_wins_name_registration_response(0, ttl, p);
432 * Normal groups are all registered with an IP address of 255.255.255.255
433 * so we can't search for the IP address.
435 update_name_ttl(namerec, ttl);
436 send_wins_name_registration_response(0, ttl, p);
439 else if(!group && (question->name_type == 0x1d))
442 * Special name type - just pretend the refresh succeeded.
444 send_wins_name_registration_response(0, ttl, p);
453 DEBUG(3,("wins_process_name_refresh_request: Name refresh for name %s with IP %s and \
454 is IP is not known to the name.\n", namestr(question), inet_ntoa(from_ip) ));
455 send_wins_name_registration_response(RFS_ERR, 0, p);
460 /***********************************************************************
461 Deal with a name registration request query success to a client that
464 We have a locked pointer to the original packet stashed away in the
465 userdata pointer. The success here is actually a failure as it means
466 the client we queried wants to keep the name, so we must return
467 a registration failure to the original requestor.
468 ************************************************************************/
470 static void wins_register_query_success(struct subnet_record *subrec,
471 struct userdata_struct *userdata,
472 struct nmb_name *question_name,
474 struct res_rec *answers)
476 struct packet_struct *orig_reg_packet;
478 memcpy((char *)&orig_reg_packet, userdata->data, sizeof(struct packet_struct *));
480 DEBUG(3,("wins_register_query_success: Original client at IP %s still wants the \
481 name %s. Rejecting registration request.\n", inet_ntoa(ip), namestr(question_name) ));
483 send_wins_name_registration_response(RFS_ERR, 0, orig_reg_packet);
485 orig_reg_packet->locked = False;
486 free_packet(orig_reg_packet);
489 /***********************************************************************
490 Deal with a name registration request query failure to a client that
493 We have a locked pointer to the original packet stashed away in the
494 userdata pointer. The failure here is actually a success as it means
495 the client we queried didn't want to keep the name, so we can remove
496 the old name record and then successfully add the new name.
497 ************************************************************************/
499 static void wins_register_query_fail(struct subnet_record *subrec,
500 struct response_record *rrec,
501 struct nmb_name *question_name,
504 struct userdata_struct *userdata = rrec->userdata;
505 struct packet_struct *orig_reg_packet;
506 struct nmb_packet *nmb;
507 struct name_record *namerec = NULL;
511 memcpy((char *)&orig_reg_packet, userdata->data, sizeof(struct packet_struct *));
513 nmb = &orig_reg_packet->packet.nmb;
515 nb_flags = get_nb_flags(nmb->additional->rdata);
516 group = (nb_flags & NB_GROUP) ? True : False;
519 * We want to just add the name, as we now know the original owner
520 * didn't want it. But we can't just do that as an arbitary
521 * amount of time may have taken place between the name query
522 * request and this timeout/error response. So we check that
523 * the name still exists and is in the same state - if so
524 * we remove it and call wins_process_name_registration_request()
525 * as we know it will do the right thing now.
528 namerec = find_name_on_subnet(subrec, question_name, FIND_ANY_NAME);
530 if( (namerec != NULL)
531 && (namerec->data.source == REGISTER_NAME)
532 && ip_equal(rrec->packet->ip, *namerec->data.ip) )
534 remove_name_from_namelist( subrec, namerec);
539 wins_process_name_registration_request(subrec, orig_reg_packet);
541 DEBUG(2,("wins_register_query_fail: The state of the WINS database changed between \
542 querying for name %s in order to replace it and this reply.\n", namestr(question_name) ));
544 orig_reg_packet->locked = False;
545 free_packet(orig_reg_packet);
548 /***********************************************************************
549 Deal with a name registration request to a WINS server.
551 Use the following pseudocode :
559 | +--- existing name is group
562 | | +--- add name (return).
565 | +--- exiting name is unique
568 | +--- query existing owner (return).
571 +--------name doesn't exist
574 +--- add name (return).
582 | +--- existing name is group
585 | | +--- fail add (return).
588 | +--- exiting name is unique
591 | +--- query existing owner (return).
594 +--------name doesn't exist
597 +--- add name (return).
599 As can be seen from the above, the two cases may be collapsed onto each
600 other with the exception of the case where the name already exists and
601 is a group name. This case we handle with an if statement.
603 ************************************************************************/
605 void wins_process_name_registration_request(struct subnet_record *subrec,
606 struct packet_struct *p)
608 struct nmb_packet *nmb = &p->packet.nmb;
609 struct nmb_name *question = &nmb->question.question_name;
610 BOOL bcast = nmb->header.nm_flags.bcast;
611 uint16 nb_flags = get_nb_flags(nmb->additional->rdata);
612 int ttl = get_ttl_from_packet(nmb);
613 struct name_record *namerec = NULL;
614 struct in_addr from_ip;
615 BOOL registering_group_name = (nb_flags & NB_GROUP) ? True : False;;
617 putip((char *)&from_ip,&nmb->additional->rdata[2]);
622 * We should only get unicast name registration packets here.
623 * Anyone trying to register broadcast should not be going to a WINS
624 * server. Log an error here.
627 DEBUG(0,("wins_process_name_registration_request: broadcast name registration request \
628 received for name %s from IP %s on subnet %s. Error - should not be sent to WINS server\n",
629 namestr(question), inet_ntoa(from_ip), subrec->subnet_name));
633 DEBUG(3,("wins_process_name_registration_request: %s name registration for name %s \
634 IP %s\n", registering_group_name ? "Group" : "Unique", namestr(question), inet_ntoa(from_ip) ));
637 * See if the name already exists.
640 namerec = find_name_on_subnet(subrec, question, FIND_ANY_NAME);
643 * Deal with the case where the name found was a dns entry.
644 * Remove it as we now have a NetBIOS client registering the
648 if( (namerec != NULL)
649 && ( (namerec->data.source == DNS_NAME)
650 || (namerec->data.source == DNSFAIL_NAME) ) )
652 DEBUG(5,("wins_process_name_registration_request: Name (%s) in WINS was \
653 a dns lookup - removing it.\n", namestr(question) ));
654 remove_name_from_namelist( subrec, namerec );
659 * Reject if the name exists and is not a REGISTER_NAME.
660 * (ie. Don't allow any static names to be overwritten.
663 if((namerec != NULL) && (namerec->data.source != REGISTER_NAME))
665 DEBUG( 3, ( "wins_process_name_registration_request: Attempt \
666 to register name %s. Name already exists in WINS with source type %d.\n",
667 namestr(question), namerec->data.source ));
668 send_wins_name_registration_response(RFS_ERR, 0, p);
673 * Special policy decisions based on MS documentation.
674 * 1). All group names (except names ending in 0x1c) are added as 255.255.255.255.
675 * 2). All unique names ending in 0x1d are ignored, although a positive response is sent.
679 * A group name is always added as the local broadcast address, except
680 * for group names ending in 0x1c.
681 * Group names with type 0x1c are registered with individual IP addresses.
684 if(registering_group_name && (question->name_type != 0x1c))
685 from_ip = *interpret_addr2("255.255.255.255");
688 * Ignore all attempts to register a unique 0x1d name, although return success.
691 if(!registering_group_name && (question->name_type == 0x1d))
693 DEBUG(3,("wins_process_name_registration_request: Ignoring request \
694 to register name %s from IP %s.", namestr(question), inet_ntoa(p->ip) ));
695 send_wins_name_registration_response(0, ttl, p);
700 * Next two cases are the 'if statement' mentioned above.
703 if((namerec != NULL) && NAME_GROUP(namerec))
705 if(registering_group_name)
708 * If we are adding a group name, the name exists and is also a group entry just add this
709 * IP address to it and update the ttl.
712 DEBUG(3,("wins_process_name_registration_request: Adding IP %s to group name %s.\n",
713 inet_ntoa(from_ip), namestr(question) ));
715 * Check the ip address is not already in the group.
717 if(!find_ip_in_name_record(namerec, from_ip))
718 add_ip_to_name_record(namerec, from_ip);
719 update_name_ttl(namerec, ttl);
720 send_wins_name_registration_response(0, ttl, p);
726 * If we are adding a unique name, the name exists in the WINS db
727 * and is a group name then reject the registration.
730 DEBUG(3,("wins_process_name_registration_request: Attempt to register name %s. Name \
731 already exists in WINS as a GROUP name.\n", namestr(question) ));
732 send_wins_name_registration_response(RFS_ERR, 0, p);
738 * From here on down we know that if the name exists in the WINS db it is
739 * a unique name, not a group name.
743 * If the name exists and is one of our names then check the
744 * registering IP address. If it's not one of ours then automatically
745 * reject without doing the query - we know we will reject it.
748 if((namerec != NULL) && (is_myname(namerec->name.name)) )
752 DEBUG(3,("wins_process_name_registration_request: Attempt to register name %s. Name \
753 is one of our (WINS server) names. Denying registration.\n", namestr(question) ));
754 send_wins_name_registration_response(RFS_ERR, 0, p);
760 * It's one of our names and one of our IP's - update the ttl.
762 update_name_ttl(namerec, ttl);
763 send_wins_name_registration_response(0, ttl, p);
769 * If the name exists and it is a unique registration and the registering IP
770 * is the same as the the (single) already registered IP then just update the ttl.
773 if( !registering_group_name
775 && (namerec->data.num_ips == 1)
776 && ip_equal( namerec->data.ip[0], from_ip ) )
778 update_name_ttl( namerec, ttl );
779 send_wins_name_registration_response( 0, ttl, p );
784 * Finally if the name exists do a query to the registering machine
785 * to see if they still claim to have the name.
788 if( namerec != NULL )
790 char ud[sizeof(struct userdata_struct) + sizeof(struct packet_struct *)];
791 struct userdata_struct *userdata = (struct userdata_struct *)ud;
794 * First send a WACK to the registering machine.
797 send_wins_wack_response(60, p);
800 * When the reply comes back we need the original packet.
801 * Lock this so it won't be freed and then put it into
802 * the userdata structure.
807 userdata = (struct userdata_struct *)ud;
809 userdata->copy_fn = NULL;
810 userdata->free_fn = NULL;
811 userdata->userdata_len = sizeof(struct packet_struct *);
812 memcpy(userdata->data, (char *)&p, sizeof(struct packet_struct *) );
815 * Use the new call to send a query directly to an IP address.
816 * This sends the query directly to the IP address, and ensures
817 * the recursion desired flag is not set (you were right Luke :-).
818 * This function should *only* be called from the WINS server
822 query_name_from_wins_server( *namerec->data.ip,
825 wins_register_query_success,
826 wins_register_query_fail,
832 * Name did not exist - add it.
835 (void)add_name_to_subnet( subrec, question->name, question->name_type,
836 nb_flags, ttl, REGISTER_NAME, 1, &from_ip );
838 send_wins_name_registration_response(0, ttl, p);
841 /***********************************************************************
842 Deal with a mutihomed name query success to the machine that
843 requested the multihomed name registration.
845 We have a locked pointer to the original packet stashed away in the
847 ************************************************************************/
849 static void wins_multihomed_register_query_success(struct subnet_record *subrec,
850 struct userdata_struct *userdata,
851 struct nmb_name *question_name,
853 struct res_rec *answers)
855 struct packet_struct *orig_reg_packet;
856 struct nmb_packet *nmb;
857 struct name_record *namerec = NULL;
858 struct in_addr from_ip;
861 memcpy((char *)&orig_reg_packet, userdata->data, sizeof(struct packet_struct *));
863 nmb = &orig_reg_packet->packet.nmb;
865 putip((char *)&from_ip,&nmb->additional->rdata[2]);
866 ttl = get_ttl_from_packet(nmb);
869 * We want to just add the new IP, as we now know the requesting
870 * machine claims to own it. But we can't just do that as an arbitary
871 * amount of time may have taken place between the name query
872 * request and this response. So we check that
873 * the name still exists and is in the same state - if so
874 * we just add the extra IP and update the ttl.
877 namerec = find_name_on_subnet(subrec, question_name, FIND_ANY_NAME);
879 if( (namerec == NULL) || (namerec->data.source != REGISTER_NAME) )
881 DEBUG(3,("wins_multihomed_register_query_success: name %s is not in the correct state to add \
882 a subsequent IP addess.\n", namestr(question_name) ));
883 send_wins_name_registration_response(RFS_ERR, 0, orig_reg_packet);
887 if(!find_ip_in_name_record(namerec, from_ip))
888 add_ip_to_name_record(namerec, from_ip);
889 update_name_ttl(namerec, ttl);
890 send_wins_name_registration_response(0, ttl, orig_reg_packet);
894 /***********************************************************************
895 Deal with a name registration request query failure to a client that
898 We have a locked pointer to the original packet stashed away in the
900 ************************************************************************/
902 static void wins_multihomed_register_query_fail(struct subnet_record *subrec,
903 struct response_record *rrec,
904 struct nmb_name *question_name,
907 struct userdata_struct *userdata = rrec->userdata;
908 struct packet_struct *orig_reg_packet;
910 memcpy((char *)&orig_reg_packet, userdata->data, sizeof(struct packet_struct *));
912 DEBUG(3,("wins_multihomed_register_query_fail: Registering machine at IP %s failed to answer \
913 query successfully for name %s.\n", inet_ntoa(orig_reg_packet->ip), namestr(question_name) ));
914 send_wins_name_registration_response(RFS_ERR, 0, orig_reg_packet);
918 /***********************************************************************
919 Deal with a multihomed name registration request to a WINS server.
920 These cannot be group name registrations.
921 ***********************************************************************/
923 void wins_process_multihomed_name_registration_request( struct subnet_record *subrec,
924 struct packet_struct *p)
926 struct nmb_packet *nmb = &p->packet.nmb;
927 struct nmb_name *question = &nmb->question.question_name;
928 BOOL bcast = nmb->header.nm_flags.bcast;
929 uint16 nb_flags = get_nb_flags(nmb->additional->rdata);
930 int ttl = get_ttl_from_packet(nmb);
931 struct name_record *namerec = NULL;
932 struct in_addr from_ip;
933 BOOL group = (nb_flags & NB_GROUP) ? True : False;;
935 putip((char *)&from_ip,&nmb->additional->rdata[2]);
940 * We should only get unicast name registration packets here.
941 * Anyone trying to register broadcast should not be going to a WINS
942 * server. Log an error here.
945 DEBUG(0,("wins_process_multihomed_name_registration_request: broadcast name registration request \
946 received for name %s from IP %s on subnet %s. Error - should not be sent to WINS server\n",
947 namestr(question), inet_ntoa(from_ip), subrec->subnet_name));
952 * Only unique names should be registered multihomed.
957 DEBUG(0,("wins_process_multihomed_name_registration_request: group name registration request \
958 received for name %s from IP %s on subnet %s. Errror - group names should not be multihomed.\n",
959 namestr(question), inet_ntoa(from_ip), subrec->subnet_name));
963 DEBUG(3,("wins_process_multihomed_name_registration_request: name registration for name %s \
964 IP %s\n", namestr(question), inet_ntoa(from_ip) ));
967 * Deal with policy regarding 0x1d names.
970 if(question->name_type == 0x1d)
972 DEBUG(3,("wins_process_multihomed_name_registration_request: Ignoring request \
973 to register name %s from IP %s.", namestr(question), inet_ntoa(p->ip) ));
974 send_wins_name_registration_response(0, ttl, p);
979 * See if the name already exists.
982 namerec = find_name_on_subnet(subrec, question, FIND_ANY_NAME);
985 * Deal with the case where the name found was a dns entry.
986 * Remove it as we now have a NetBIOS client registering the
990 if( (namerec != NULL)
991 && ( (namerec->data.source == DNS_NAME)
992 || (namerec->data.source == DNSFAIL_NAME) ) )
994 DEBUG(5,("wins_process_multihomed_name_registration_request: Name (%s) in WINS was a dns lookup \
995 - removing it.\n", namestr(question) ));
996 remove_name_from_namelist( subrec, namerec);
1001 * Reject if the name exists and is not a REGISTER_NAME.
1002 * (ie. Don't allow any static names to be overwritten.
1005 if( (namerec != NULL) && (namerec->data.source != REGISTER_NAME) )
1007 DEBUG( 3, ( "wins_process_multihomed_name_registration_request: Attempt \
1008 to register name %s. Name already exists in WINS with source type %d.\n",
1009 namestr(question), namerec->data.source ));
1010 send_wins_name_registration_response(RFS_ERR, 0, p);
1015 * Reject if the name exists and is a GROUP name.
1018 if((namerec != NULL) && NAME_GROUP(namerec))
1020 DEBUG(3,("wins_process_multihomed_name_registration_request: Attempt to register name %s. Name \
1021 already exists in WINS as a GROUP name.\n", namestr(question) ));
1022 send_wins_name_registration_response(RFS_ERR, 0, p);
1027 * From here on down we know that if the name exists in the WINS db it is
1028 * a unique name, not a group name.
1032 * If the name exists and is one of our names then check the
1033 * registering IP address. If it's not one of ours then automatically
1034 * reject without doing the query - we know we will reject it.
1037 if((namerec != NULL) && (is_myname(namerec->name.name)) )
1039 if(!ismyip(from_ip))
1041 DEBUG(3,("wins_process_multihomed_name_registration_request: Attempt to register name %s. Name \
1042 is one of our (WINS server) names. Denying registration.\n", namestr(question) ));
1043 send_wins_name_registration_response(RFS_ERR, 0, p);
1049 * It's one of our names and one of our IP's. Ensure the IP is in the record and
1052 if(!find_ip_in_name_record(namerec, from_ip))
1053 add_ip_to_name_record(namerec, from_ip);
1054 update_name_ttl(namerec, ttl);
1055 send_wins_name_registration_response(0, ttl, p);
1061 * If the name exists check if the IP address is already registered
1062 * to that name. If so then update the ttl and reply success.
1065 if((namerec != NULL) && find_ip_in_name_record(namerec, from_ip))
1067 update_name_ttl(namerec, ttl);
1068 send_wins_name_registration_response(0, ttl, p);
1073 * If the name exists do a query to the owner
1074 * to see if they still want the name.
1079 char ud[sizeof(struct userdata_struct) + sizeof(struct packet_struct *)];
1080 struct userdata_struct *userdata = (struct userdata_struct *)ud;
1083 * First send a WACK to the registering machine.
1086 send_wins_wack_response(60, p);
1089 * When the reply comes back we need the original packet.
1090 * Lock this so it won't be freed and then put it into
1091 * the userdata structure.
1096 userdata = (struct userdata_struct *)ud;
1098 userdata->copy_fn = NULL;
1099 userdata->free_fn = NULL;
1100 userdata->userdata_len = sizeof(struct packet_struct *);
1101 memcpy(userdata->data, (char *)&p, sizeof(struct packet_struct *) );
1104 * As query_name uses the subnet broadcast address as the destination
1105 * of the packet we temporarily change the subnet broadcast address to
1106 * be the IP address of the requesting machine and send the packet. This
1107 * is a *horrible* hack but the alternative is to add the destination
1108 * address parameter to all query_name() calls. I hate this code :-).
1111 subrec->bcast_ip = p->ip;
1112 query_name( subrec, question->name, question->name_type,
1113 wins_multihomed_register_query_success,
1114 wins_multihomed_register_query_fail,
1116 subrec->bcast_ip = ipzero;
1121 * Name did not exist - add it.
1124 (void)add_name_to_subnet( subrec, question->name, question->name_type,
1125 nb_flags, ttl, REGISTER_NAME, 1, &from_ip );
1127 send_wins_name_registration_response(0, ttl, p);
1130 /***********************************************************************
1131 Deal with the special name query for *<1b>.
1132 ***********************************************************************/
1134 static void process_wins_dmb_query_request(struct subnet_record *subrec,
1135 struct packet_struct *p)
1137 struct name_record *namerec = NULL;
1142 * Go through all the names in the WINS db looking for those
1143 * ending in <1b>. Use this to calculate the number of IP
1144 * addresses we need to return.
1148 for( namerec = (struct name_record *)ubi_trFirst( subrec->namelist );
1150 namerec = (struct name_record *)ubi_trNext( namerec ) )
1152 if( namerec->name.name_type == 0x1b )
1153 num_ips += namerec->data.num_ips;
1159 * There are no 0x1b names registered. Return name query fail.
1161 send_wins_name_query_response(NAM_ERR, p, NULL);
1165 if((prdata = (char *)malloc( num_ips * 6 )) == NULL)
1167 DEBUG(0,("process_wins_dmb_query_request: Malloc fail !.\n"));
1172 * Go through all the names again in the WINS db looking for those
1173 * ending in <1b>. Add their IP addresses into the list we will
1178 for( namerec = (struct name_record *)ubi_trFirst( subrec->namelist );
1180 namerec = (struct name_record *)ubi_trNext( namerec ) )
1182 if(namerec->name.name_type == 0x1b)
1185 for(i = 0; i < namerec->data.num_ips; i++)
1187 set_nb_flags(&prdata[num_ips * 6],namerec->data.nb_flags);
1188 putip((char *)&prdata[(num_ips * 6) + 2], &namerec->data.ip[i]);
1195 * Send back the reply containing the IP list.
1198 reply_netbios_packet(p, /* Packet to reply to. */
1199 0, /* Result code. */
1200 WINS_QUERY, /* nmbd type code. */
1201 NMB_NAME_QUERY_OPCODE, /* opcode. */
1202 lp_min_wins_ttl(), /* ttl. */
1203 prdata, /* data to send. */
1204 num_ips*6); /* data length. */
1209 /****************************************************************************
1210 Send a WINS name query response.
1211 **************************************************************************/
1213 void send_wins_name_query_response(int rcode, struct packet_struct *p,
1214 struct name_record *namerec)
1217 char *prdata = rdata;
1218 int reply_data_len = 0;
1227 int same_net_index = -1;
1229 ttl = (namerec->data.death_time != PERMANENT_TTL) ?
1230 namerec->data.death_time - p->timestamp : lp_max_wins_ttl();
1232 /* Copy all known ip addresses into the return data. */
1233 /* Optimise for the common case of one IP address so
1234 we don't need a malloc. */
1236 if( namerec->data.num_ips == 1 )
1240 if((prdata = (char *)malloc( namerec->data.num_ips * 6 )) == NULL)
1242 DEBUG(0,("send_wins_name_query_response: malloc fail !\n"));
1247 * Look over the known IP addresses and see if one of them
1248 * is on the same (local) net as the requesting IP address. If so then
1249 * put that IP address into the packet as the first IP.
1250 * We can only do this for local nets as they're the only
1251 * ones we know the netmask for.
1256 if(is_local_net(p->ip))
1258 struct in_addr *n_mask = iface_nmask(p->ip);
1260 for( j = 0; j < namerec->data.num_ips; j++)
1262 if(same_net( namerec->data.ip[j], p->ip, *n_mask))
1264 set_nb_flags(&prdata[0],namerec->data.nb_flags);
1265 putip((char *)&prdata[2], &namerec->data.ip[j]);
1273 for(j = 0; j < namerec->data.num_ips; j++)
1275 if(j == same_net_index)
1277 set_nb_flags(&prdata[i*6],namerec->data.nb_flags);
1278 putip((char *)&prdata[2+(i*6)], &namerec->data.ip[j]);
1281 reply_data_len = namerec->data.num_ips * 6;
1285 reply_netbios_packet(p, /* Packet to reply to. */
1286 rcode, /* Result code. */
1287 WINS_QUERY, /* nmbd type code. */
1288 NMB_NAME_QUERY_OPCODE, /* opcode. */
1290 prdata, /* data to send. */
1291 reply_data_len); /* data length. */
1293 if((prdata != rdata) && (prdata != NULL))
1297 /***********************************************************************
1298 Deal with a name query.
1299 ***********************************************************************/
1301 void wins_process_name_query_request(struct subnet_record *subrec,
1302 struct packet_struct *p)
1304 struct nmb_packet *nmb = &p->packet.nmb;
1305 struct nmb_name *question = &nmb->question.question_name;
1306 struct name_record *namerec = NULL;
1308 DEBUG(3,("wins_process_name_query: name query for name %s from IP %s\n",
1309 namestr(question), inet_ntoa(p->ip) ));
1312 * Special name code. If the queried name is *<1b> then search
1313 * the entire WINS database and return a list of all the IP addresses
1314 * registered to any <1b> name. This is to allow domain master browsers
1315 * to discover other domains that may not have a presence on their subnet.
1318 if(strequal( question->name, "*") && (question->name_type == 0x1b))
1320 process_wins_dmb_query_request( subrec, p);
1324 namerec = find_name_on_subnet(subrec, question, FIND_ANY_NAME);
1329 * If it's a DNSFAIL_NAME then reply name not found.
1332 if( namerec->data.source == DNSFAIL_NAME )
1334 DEBUG(3,("wins_process_name_query: name query for name %s returning DNS fail.\n",
1335 namestr(question) ));
1336 send_wins_name_query_response(NAM_ERR, p, namerec);
1341 * If the name has expired then reply name not found.
1344 if( (namerec->data.death_time != PERMANENT_TTL)
1345 && (namerec->data.death_time < p->timestamp) )
1347 DEBUG(3,("wins_process_name_query: name query for name %s - name expired. Returning fail.\n",
1348 namestr(question) ));
1349 send_wins_name_query_response(NAM_ERR, p, namerec);
1353 DEBUG(3,("wins_process_name_query: name query for name %s returning first IP %s.\n",
1354 namestr(question), inet_ntoa(namerec->data.ip[0]) ));
1356 send_wins_name_query_response(0, p, namerec);
1361 * Name not found in WINS - try a dns query if it's a 0x20 name.
1364 if(lp_dns_proxy() &&
1365 ((question->name_type == 0x20) || question->name_type == 0))
1368 DEBUG(3,("wins_process_name_query: name query for name %s not found - doing dns lookup.\n",
1369 namestr(question) ));
1371 queue_dns_query(p, question, &namerec);
1376 * Name not found - return error.
1379 send_wins_name_query_response(NAM_ERR, p, NULL);
1382 /****************************************************************************
1383 Send a WINS name release response.
1384 **************************************************************************/
1386 static void send_wins_name_release_response(int rcode, struct packet_struct *p)
1388 struct nmb_packet *nmb = &p->packet.nmb;
1391 memcpy(&rdata[0], &nmb->additional->rdata[0], 6);
1393 reply_netbios_packet(p, /* Packet to reply to. */
1394 rcode, /* Result code. */
1395 NMB_REL, /* nmbd type code. */
1396 NMB_NAME_RELEASE_OPCODE, /* opcode. */
1398 rdata, /* data to send. */
1399 6); /* data length. */
1402 /***********************************************************************
1403 Deal with a name release.
1404 ***********************************************************************/
1406 void wins_process_name_release_request(struct subnet_record *subrec,
1407 struct packet_struct *p)
1409 struct nmb_packet *nmb = &p->packet.nmb;
1410 struct nmb_name *question = &nmb->question.question_name;
1411 BOOL bcast = nmb->header.nm_flags.bcast;
1412 uint16 nb_flags = get_nb_flags(nmb->additional->rdata);
1413 struct name_record *namerec = NULL;
1414 struct in_addr from_ip;
1415 BOOL releasing_group_name = (nb_flags & NB_GROUP) ? True : False;;
1417 putip((char *)&from_ip,&nmb->additional->rdata[2]);
1422 * We should only get unicast name registration packets here.
1423 * Anyone trying to register broadcast should not be going to a WINS
1424 * server. Log an error here.
1427 DEBUG(0,("wins_process_name_release_request: broadcast name registration request \
1428 received for name %s from IP %s on subnet %s. Error - should not be sent to WINS server\n",
1429 namestr(question), inet_ntoa(from_ip), subrec->subnet_name));
1433 DEBUG(3,("wins_process_name_release_request: %s name release for name %s \
1434 IP %s\n", releasing_group_name ? "Group" : "Unique", namestr(question), inet_ntoa(from_ip) ));
1437 * Deal with policy regarding 0x1d names.
1440 if(!releasing_group_name && (question->name_type == 0x1d))
1442 DEBUG(3,("wins_process_name_release_request: Ignoring request \
1443 to release name %s from IP %s.", namestr(question), inet_ntoa(p->ip) ));
1444 send_wins_name_release_response(0, p);
1449 * See if the name already exists.
1452 namerec = find_name_on_subnet(subrec, question, FIND_ANY_NAME);
1454 if( (namerec == NULL)
1455 || ((namerec != NULL) && (namerec->data.source != REGISTER_NAME)) )
1457 send_wins_name_release_response(NAM_ERR, p);
1462 * Check that the sending machine has permission to release this name.
1463 * If it's a group name not ending in 0x1c then just say yes and let
1464 * the group time out.
1467 if(releasing_group_name && (question->name_type != 0x1c))
1469 send_wins_name_release_response(0, p);
1474 * Check that the releasing node is on the list of IP addresses
1475 * for this name. Disallow the release if not.
1478 if(!find_ip_in_name_record(namerec, from_ip))
1480 DEBUG(3,("wins_process_name_release_request: Refusing request to \
1481 release name %s as IP %s is not one of the known IP's for this name.\n",
1482 namestr(question), inet_ntoa(from_ip) ));
1483 send_wins_name_release_response(NAM_ERR, p);
1488 * Release the name and then remove the IP from the known list.
1491 send_wins_name_release_response(0, p);
1492 remove_ip_from_name_record(namerec, from_ip);
1495 * Remove the name entirely if no IP addresses left.
1497 if (namerec->data.num_ips == 0)
1498 remove_name_from_namelist(subrec, namerec);
1502 /*******************************************************************
1503 WINS time dependent processing.
1504 ******************************************************************/
1506 void initiate_wins_processing(time_t t)
1508 static time_t lasttime = 0;
1512 if (t - lasttime < 5)
1515 if(!lp_we_are_a_wins_server())
1518 expire_names_on_subnet(wins_server_subnet, t);
1520 if(wins_server_subnet->namelist_changed)
1521 wins_write_database();
1523 wins_server_subnet->namelist_changed = False;
1526 /*******************************************************************
1527 Write out the current WINS database.
1528 ******************************************************************/
1530 void wins_write_database(void)
1532 struct name_record *namerec;
1533 pstring fname, fnamenew;
1537 if(!lp_we_are_a_wins_server())
1540 pstrcpy(fname,lp_lockdir());
1541 trim_string(fname,NULL,"/");
1543 pstrcat(fname,WINS_LIST);
1544 pstrcpy(fnamenew,fname);
1545 pstrcat(fnamenew,".");
1547 if((fp = fopen(fnamenew,"w")) == NULL)
1549 DEBUG(0,("wins_write_database: Can't open %s. Error was %s\n", fnamenew, strerror(errno)));
1553 DEBUG(4,("wins_write_database: Dump of WINS name list.\n"));
1556 = (struct name_record *)ubi_trFirst( wins_server_subnet->namelist );
1558 namerec = (struct name_record *)ubi_trNext( namerec ) )
1563 DEBUG(4,("%-19s ", namestr(&namerec->name) ));
1565 if( namerec->data.death_time != PERMANENT_TTL )
1567 tm = LocalTime(&namerec->data.death_time);
1568 DEBUG(4,("TTL = %s", asctime(tm) ));
1571 DEBUG(4,("TTL = PERMANENT\t"));
1573 for (i = 0; i < namerec->data.num_ips; i++)
1574 DEBUG(4,("%15s ", inet_ntoa(namerec->data.ip[i]) ));
1575 DEBUG(4,("%2x\n", namerec->data.nb_flags ));
1577 if( namerec->data.source == REGISTER_NAME )
1579 fprintf(fp, "\"%s#%02x\" %d ",
1580 namerec->name.name,namerec->name.name_type, /* Ignore scope. */
1581 (int)namerec->data.death_time);
1583 for (i = 0; i < namerec->data.num_ips; i++)
1584 fprintf( fp, "%s ", inet_ntoa( namerec->data.ip[i] ) );
1585 fprintf( fp, "%2xR\n", namerec->data.nb_flags );
1591 chmod(fnamenew,0644);
1592 rename(fnamenew,fname);