summaryrefslogtreecommitdiff
path: root/source3/nmbd/nmbd_namerelease.c
blob: b58bab3a1d6191fd0df5c27427bdb30dd624fbfd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/* 
   Unix SMB/Netbios implementation.
   Version 1.9.
   NBT netbios routines and daemon - version 2
   Copyright (C) Andrew Tridgell 1994-1998
   Copyright (C) Luke Kenneth Casson Leighton 1994-1998
   Copyright (C) Jeremy Allison 1994-1998
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   
*/

#include "includes.h"

extern int DEBUGLEVEL;

extern pstring scope;

/****************************************************************************
 Deal with a response packet when releasing one of our names.
****************************************************************************/

static void release_name_response(struct subnet_record *subrec,
                       struct response_record *rrec, struct packet_struct *p)
{
  /* 
   * If we are releasing broadcast, then getting a response is an
   * error. If we are releasing unicast, then we expect to get a response.
   */

  struct nmb_packet *nmb = &p->packet.nmb;
  BOOL bcast = nmb->header.nm_flags.bcast;
  BOOL success = True;
  struct nmb_name *question_name = &rrec->packet->packet.nmb.question.question_name;
  struct nmb_name *answer_name = &nmb->answers->rr_name;
  struct in_addr released_ip;

  /* Sanity check. Ensure that the answer name in the incoming packet is the
     same as the requested name in the outgoing packet. */

  if(!nmb_name_equal(question_name, answer_name))
  {
    DEBUG(0,("release_name_response: Answer name %s differs from question \
name %s.\n", namestr(answer_name), namestr(question_name)));
    return;
  }

  if(bcast)
  {
    /* Someone sent a response. This shouldn't happen/ */
    DEBUG(1,("release_name_response: A response for releasing name %s was received on a \
broadcast subnet %s. This should not happen !\n", namestr(answer_name), subrec->subnet_name));
    return;
  }
  else
  {
    /* Unicast - check to see if the response allows us to release the name. */
    if(nmb->header.rcode != 0)
    {
      /* Error code - we were told not to release the name ! What now ! */
      success = False;

      DEBUG(0,("release_name_response: WINS server at IP %s rejected our \
name release of name %s with error code %d.\n", inet_ntoa(p->ip), 
                  namestr(answer_name), nmb->header.rcode));

    }
    else if(nmb->header.opcode == NMB_WACK_OPCODE)
    {
      /* WINS server is telling us to wait. Pretend we didn't get
         the response but don't send out any more release requests. */

      DEBUG(5,("release_name_response: WACK from WINS server %s in releasing \
name %s on subnet %s.\n", inet_ntoa(p->ip), namestr(answer_name), subrec->subnet_name));

      rrec->repeat_count = 0;
      /* How long we should wait for. */
      rrec->repeat_time = p->timestamp + nmb->answers->ttl;
      rrec->num_msgs--;
      return;
    }
  } 

  DEBUG(5,("release_name_response: %s in releasing name %s on subnet %s.\n",
        success ? "success" : "failure", namestr(answer_name), subrec->subnet_name));

  if(success)
  {
    putip((char*)&released_ip ,&nmb->answers->rdata[2]);

    if(rrec->success_fn)
      (*rrec->success_fn)(subrec, rrec->userdata, answer_name, released_ip);
    standard_success_release( subrec, rrec->userdata, answer_name, released_ip);
  }
  else
  {
    /* We have no standard_fail_release - maybe we should add one ? */
    if(rrec->fail_fn)
      (*rrec->fail_fn)(subrec, rrec, answer_name);
  }

  remove_response_record(subrec, rrec);
}

/****************************************************************************
 Deal with a timeout when releasing one of our names.
****************************************************************************/

static void release_name_timeout_response(struct subnet_record *subrec,
                       struct response_record *rrec)
{
  /*
   * If we are releasing unicast, then NOT getting a response is an
   * error - we could not release the name. If we are releasing broadcast,
   * then we don't expect to get a response.
   */

  struct nmb_packet *sent_nmb = &rrec->packet->packet.nmb;
  BOOL bcast = sent_nmb->header.nm_flags.bcast;
  BOOL success = False;
  struct nmb_name *question_name = &sent_nmb->question.question_name;
  struct in_addr released_ip;

  if(bcast)
  {
    if(rrec->num_msgs == 0)
    {
      /* Not receiving a message is success for broadcast release. */
      success = True; 

      /* Get the ip address we were trying to release. */
      putip((char*)&released_ip ,&sent_nmb->additional->rdata[2]);
    }
  }
  else
  {
    /* Unicast - if no responses then it's an error. */
    if(rrec->num_msgs == 0)
    {
      DEBUG(2,("release_name_timeout_response: WINS server at address %s is not \
responding.\n", inet_ntoa(rrec->packet->ip)));

      /* Keep trying to contact the WINS server periodically. This allows
         us to work correctly if the WINS server is down temporarily when
         we want to delete the name. */

      /* Reset the number of attempts to zero and double the interval between
         retries. Max out at 5 minutes. */
      rrec->repeat_count = 3;
      rrec->repeat_interval *= 2;
      if(rrec->repeat_interval > (5 * 60))
        rrec->repeat_interval = (5 * 60);
      rrec->repeat_time = time(NULL) + rrec->repeat_interval;

      DEBUG(5,("release_name_timeout_response: increasing WINS timeout to %d seconds.\n",
              rrec->repeat_interval));
      return; /* Don't remove the response record. */
    }
  }

  DEBUG(5,("release_name_timeout_response: %s in releasing name %s on subnet %s.\n",
        success ? "success" : "failure", namestr(question_name), subrec->subnet_name));

  if(success && rrec->success_fn)
  {
    if(rrec->success_fn)
      (*rrec->success_fn)(subrec, rrec->userdata, question_name, released_ip);
    standard_success_release( subrec, rrec->userdata, question_name, released_ip);
  }
  else 
  {
    /* We have no standard_fail_release - maybe we should add one ? */
    if( rrec->fail_fn)
      (*rrec->fail_fn)(subrec, rrec, question_name);
  }

  remove_response_record(subrec, rrec);
}

/****************************************************************************
 Try and release one of our names.
****************************************************************************/

BOOL release_name(struct subnet_record *subrec, struct name_record *namerec,
                   release_name_success_function success_fn,
                   release_name_fail_function fail_fn,
                   struct userdata_struct *userdata)
{
  int i;

  /* Ensure it's a SELF name, and in the ACTIVE state. */
  if((namerec->data.source != SELF_NAME) || !NAME_IS_ACTIVE(namerec))
  {
    DEBUG(0,("release_name: Cannot release name %s from subnet %s. Source was %d \n",
          namestr(&namerec->name), subrec->subnet_name, namerec->data.source)); 
    return True;
  }

  /* Set the name into the deregistering state. */
  namerec->data.nb_flags |= NB_DEREG;

  /*  
   * Go through and release the name for all known ip addresses.
   * Only call the success/fail function on the last one (it should
   * only be done once).
   */

  for( i = 0; i < namerec->data.num_ips; i++)
  {
    if(queue_release_name( subrec,
        release_name_response,
        release_name_timeout_response,
        (i == (namerec->data.num_ips - 1)) ? success_fn : NULL,
        (i == (namerec->data.num_ips - 1)) ? fail_fn : NULL,
        (i == (namerec->data.num_ips - 1)) ? userdata : NULL,
        &namerec->name,
        namerec->data.nb_flags,
        namerec->data.ip[i]) == NULL)
    {
      DEBUG(0,("release_name: Failed to send packet trying to release name %s IP %s\n",
            namestr(&namerec->name), inet_ntoa(namerec->data.ip[i]) ));
      return True;
    }
  }
  return False;
}