new WHATSNEW for 2.2.7
[Samba.git] / source / nmbd / nmbd_nameregister.c
blobc3361cec60b4f98751776c1ecdbc656089eefb17
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 NBT netbios routines and daemon - version 2
5 Copyright (C) Andrew Tridgell 1994-1998
6 Copyright (C) Luke Kenneth Casson Leighton 1994-1998
7 Copyright (C) Jeremy Allison 1994-1998
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 2 of the License, or
12 (at your option) any later version.
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.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "includes.h"
27 extern fstring global_myworkgroup;
29 /****************************************************************************
30 Deal with a response packet when registering one of our names.
31 ****************************************************************************/
33 static void register_name_response(struct subnet_record *subrec,
34 struct response_record *rrec, struct packet_struct *p)
36 /*
37 * If we are registering broadcast, then getting a response is an
38 * error - we do not have the name. If we are registering unicast,
39 * then we expect to get a response.
42 struct nmb_packet *nmb = &p->packet.nmb;
43 BOOL bcast = nmb->header.nm_flags.bcast;
44 BOOL success = True;
45 struct nmb_name *question_name = &rrec->packet->packet.nmb.question.question_name;
46 struct nmb_name *answer_name = &nmb->answers->rr_name;
47 int ttl = 0;
48 uint16 nb_flags = 0;
49 struct in_addr registered_ip;
51 /* Sanity check. Ensure that the answer name in the incoming packet is the
52 same as the requested name in the outgoing packet. */
54 if(!question_name || !answer_name)
56 DEBUG(0,("register_name_response: malformed response (%s is NULL).\n",
57 question_name ? "question_name" : "answer_name" ));
58 return;
61 if(!nmb_name_equal(question_name, answer_name))
63 DEBUG(0,("register_name_response: Answer name %s differs from question \
64 name %s.\n", nmb_namestr(answer_name), nmb_namestr(question_name)));
65 return;
68 if(bcast)
71 * Special hack to cope with old Samba nmbd's.
72 * Earlier versions of Samba (up to 1.9.16p11) respond
73 * to a broadcast name registration of WORKGROUP<1b> when
74 * they should not. Hence, until these versions are gone,
75 * we should treat such errors as success for this particular
76 * case only. jallison@whistle.com.
79 #if 1 /* OLD_SAMBA_SERVER_HACK */
80 if((nmb->header.rcode == ACT_ERR) && strequal(global_myworkgroup, answer_name->name) &&
81 (answer_name->name_type == 0x1b))
83 /* Pretend we did not get this. */
84 rrec->num_msgs--;
86 DEBUG(5,("register_name_response: Ignoring broadcast response to \
87 registration of name %s due to old Samba server bug.\n", nmb_namestr(answer_name)));
88 return;
90 #endif /* OLD_SAMBA_SERVER_HACK */
92 /* Someone else has the name. Log the problem. */
93 DEBUG(1,("register_name_response: Failed to register \
94 name %s on subnet %s via broadcast. Error code was %d. Reject came from IP %s\n",
95 nmb_namestr(answer_name),
96 subrec->subnet_name, nmb->header.rcode, inet_ntoa(p->ip)));
97 success = False;
99 else
101 /* Unicast - check to see if the response allows us to have the name. */
102 if(nmb->header.rcode != 0)
104 /* Error code - we didn't get the name. */
105 success = False;
107 DEBUG(0,("register_name_response: server at IP %s rejected our \
108 name registration of %s with error code %d.\n", inet_ntoa(p->ip),
109 nmb_namestr(answer_name), nmb->header.rcode));
112 else if(nmb->header.opcode == NMB_WACK_OPCODE)
114 /* WINS server is telling us to wait. Pretend we didn't get
115 the response but don't send out any more register requests. */
117 DEBUG(5,("register_name_response: WACK from WINS server %s in registering \
118 name %s on subnet %s.\n", inet_ntoa(p->ip), nmb_namestr(answer_name), subrec->subnet_name));
120 rrec->repeat_count = 0;
121 /* How long we should wait for. */
122 rrec->repeat_time = p->timestamp + nmb->answers->ttl;
123 rrec->num_msgs--;
124 return;
126 else
128 success = True;
129 /* Get the data we need to pass to the success function. */
130 nb_flags = get_nb_flags(nmb->answers->rdata);
131 putip((char*)&registered_ip,&nmb->answers->rdata[2]);
132 ttl = nmb->answers->ttl;
136 DEBUG(5,("register_name_response: %s in registering name %s on subnet %s.\n",
137 success ? "success" : "failure", nmb_namestr(answer_name), subrec->subnet_name));
139 if(success)
141 /* Enter the registered name into the subnet name database before calling
142 the success function. */
143 standard_success_register(subrec, rrec->userdata, answer_name, nb_flags, ttl, registered_ip);
144 if( rrec->success_fn)
145 (*(register_name_success_function)rrec->success_fn)(subrec, rrec->userdata, answer_name, nb_flags, ttl, registered_ip);
147 else
149 if( rrec->fail_fn)
150 (*(register_name_fail_function)rrec->fail_fn)(subrec, rrec, question_name);
151 /* Remove the name. */
152 standard_fail_register( subrec, rrec, question_name);
155 /* Ensure we don't retry. */
156 remove_response_record(subrec, rrec);
159 /****************************************************************************
160 Deal with a timeout when registering one of our names.
161 ****************************************************************************/
163 static void register_name_timeout_response(struct subnet_record *subrec,
164 struct response_record *rrec)
167 * If we are registering unicast, then NOT getting a response is an
168 * error - we do not have the name. If we are registering broadcast,
169 * then we don't expect to get a response.
172 struct nmb_packet *sent_nmb = &rrec->packet->packet.nmb;
173 BOOL bcast = sent_nmb->header.nm_flags.bcast;
174 BOOL success = False;
175 struct nmb_name *question_name = &sent_nmb->question.question_name;
176 uint16 nb_flags = 0;
177 int ttl = 0;
178 struct in_addr registered_ip;
180 if(bcast)
182 if(rrec->num_msgs == 0)
184 /* Not receiving a message is success for broadcast registration. */
185 success = True;
187 /* Pull the success values from the original request packet. */
188 nb_flags = get_nb_flags(sent_nmb->additional->rdata);
189 ttl = sent_nmb->additional->ttl;
190 putip(&registered_ip,&sent_nmb->additional->rdata[2]);
193 else
195 /* Unicast - if no responses then it's an error. */
196 if(rrec->num_msgs == 0)
198 DEBUG(2,("register_name_timeout_response: WINS server at address %s is not \
199 responding.\n", inet_ntoa(rrec->packet->ip)));
201 /* Keep trying to contact the WINS server periodically. This allows
202 us to work correctly if the WINS server is down temporarily when
203 we come up. */
205 /* Reset the number of attempts to zero and double the interval between
206 retries. Max out at 5 minutes. */
207 rrec->repeat_count = 3;
208 rrec->repeat_interval *= 2;
209 if(rrec->repeat_interval > (5 * 60))
210 rrec->repeat_interval = (5 * 60);
211 rrec->repeat_time = time(NULL) + rrec->repeat_interval;
212 rrec->in_expiration_processing = False;
214 DEBUG(5,("register_name_timeout_response: increasing WINS timeout to %d seconds.\n",
215 (int)rrec->repeat_interval));
216 return; /* Don't remove the response record. */
220 DEBUG(5,("register_name_timeout_response: %s in registering name %s on subnet %s.\n",
221 success ? "success" : "failure", nmb_namestr(question_name), subrec->subnet_name));
222 if(success)
224 /* Enter the registered name into the subnet name database before calling
225 the success function. */
226 standard_success_register(subrec, rrec->userdata, question_name, nb_flags, ttl, registered_ip);
227 if( rrec->success_fn)
228 (*(register_name_success_function)rrec->success_fn)(subrec, rrec->userdata, question_name, nb_flags, ttl, registered_ip);
230 else
232 if( rrec->fail_fn)
233 (*(register_name_fail_function)rrec->fail_fn)(subrec, rrec, question_name);
234 /* Remove the name. */
235 standard_fail_register( subrec, rrec, question_name);
238 /* Ensure we don't retry. */
239 remove_response_record(subrec, rrec);
242 /****************************************************************************
243 Try and register one of our names on the unicast subnet - multihomed.
244 ****************************************************************************/
246 static BOOL multihomed_register_name( struct nmb_name *nmbname, uint16 nb_flags,
247 register_name_success_function success_fn,
248 register_name_fail_function fail_fn,
249 struct userdata_struct *userdata)
252 If we are adding a group name, we just send multiple
253 register name packets to the WINS server (this is an
254 internet group name.
256 If we are adding a unique name, We need first to add
257 our names to the unicast subnet namelist. This is
258 because when a WINS server receives a multihomed
259 registration request, the first thing it does is to
260 send a name query to the registering machine, to see
261 if it has put the name in it's local namelist.
262 We need the name there so the query response code in
263 nmbd_incomingrequests.c will find it.
265 We are adding this name prematurely (we don't really
266 have it yet), but as this is on the unicast subnet
267 only we will get away with this (only the WINS server
268 will ever query names from us on this subnet).
271 int num_ips=0;
272 int i;
273 struct in_addr *ip_list = NULL;
274 struct subnet_record *subrec;
276 for(subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec) )
277 num_ips++;
279 if((ip_list = (struct in_addr *)malloc(num_ips * sizeof(struct in_addr)))==NULL)
281 DEBUG(0,("multihomed_register_name: malloc fail !\n"));
282 return True;
285 for( subrec = FIRST_SUBNET, i = 0;
286 subrec;
287 subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec), i++ )
288 ip_list[i] = subrec->myip;
290 (void)add_name_to_subnet( unicast_subnet, nmbname->name, nmbname->name_type,
291 nb_flags, lp_max_ttl(), SELF_NAME,
292 num_ips, ip_list);
294 /* Now try and register the name, num_ips times. On the last time use
295 the given success and fail functions. */
297 for( i = 0; i < num_ips; i++)
299 if(queue_register_multihomed_name( unicast_subnet,
300 register_name_response,
301 register_name_timeout_response,
302 (i == num_ips - 1) ? success_fn : NULL,
303 (i == num_ips - 1) ? fail_fn : NULL,
304 (i == num_ips - 1) ? userdata : NULL,
305 nmbname,
306 nb_flags,
307 ip_list[i]) == NULL)
309 DEBUG(0,("multihomed_register_name: Failed to send packet trying to \
310 register name %s IP %s\n", nmb_namestr(nmbname), inet_ntoa(ip_list[i]) ));
312 SAFE_FREE(ip_list);
313 return True;
317 SAFE_FREE(ip_list);
319 return False;
322 /****************************************************************************
323 Try and register one of our names.
324 ****************************************************************************/
326 BOOL register_name(struct subnet_record *subrec,
327 char *name, int type, uint16 nb_flags,
328 register_name_success_function success_fn,
329 register_name_fail_function fail_fn,
330 struct userdata_struct *userdata)
332 struct nmb_name nmbname;
334 make_nmb_name(&nmbname, name, type);
336 /* Always set the NB_ACTIVE flag on the name we are
337 registering. Doesn't make sense without it.
340 nb_flags |= NB_ACTIVE;
342 /* If this is the unicast subnet, and we are a multi-homed
343 host, then register a multi-homed name. */
345 if( (subrec == unicast_subnet) && we_are_multihomed())
346 return multihomed_register_name(&nmbname, nb_flags,
347 success_fn, fail_fn,
348 userdata);
350 if(queue_register_name( subrec,
351 register_name_response,
352 register_name_timeout_response,
353 success_fn,
354 fail_fn,
355 userdata,
356 &nmbname,
357 nb_flags) == NULL)
359 DEBUG(0,("register_name: Failed to send packet trying to register name %s\n",
360 nmb_namestr(&nmbname)));
361 return True;
363 return False;
366 /****************************************************************************
367 Try and refresh one of our names.
368 ****************************************************************************/
370 BOOL refresh_name(struct subnet_record *subrec, struct name_record *namerec,
371 refresh_name_success_function success_fn,
372 refresh_name_fail_function fail_fn,
373 struct userdata_struct *userdata)
375 int i;
378 * Go through and refresh the name for all known ip addresses.
379 * Only call the success/fail function on the last one (it should
380 * only be done once).
383 for( i = 0; i < namerec->data.num_ips; i++)
385 if(queue_refresh_name( subrec,
386 register_name_response,
387 register_name_timeout_response,
388 (i == (namerec->data.num_ips - 1)) ? success_fn : NULL,
389 (i == (namerec->data.num_ips - 1)) ? fail_fn : NULL,
390 (i == (namerec->data.num_ips - 1)) ? userdata : NULL,
391 namerec,
392 namerec->data.ip[i]) == NULL)
394 DEBUG(0,("refresh_name: Failed to send packet trying to refresh name %s\n",
395 nmb_namestr(&namerec->name)));
396 return True;
399 return False;