94c354a22a19f0a176cde6aa34f581262ae7efd8
[samba.git] / source4 / nbt_server / wins / winsclient.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    wins client name registration and refresh
5
6    Copyright (C) Andrew Tridgell        2005
7    
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 3 of the License, or
11    (at your option) any later version.
12    
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.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "nbt_server/nbt_server.h"
24 #include "nbt_server/wins/winsserver.h"
25 #include "libcli/composite/composite.h"
26 #include "lib/events/events.h"
27 #include "librpc/gen_ndr/ndr_nbt.h"
28 #include "smbd/service_task.h"
29 #include "param/param.h"
30
31 static void nbtd_wins_refresh_handler(struct composite_context *c);
32
33 /* we send WINS client requests using our primary network interface 
34 */
35 static struct nbt_name_socket *wins_socket(struct nbtd_interface *iface)
36 {
37         struct nbtd_server *nbtsrv = iface->nbtsrv;
38         return nbtsrv->interfaces->nbtsock;
39 }
40
41
42 static void nbtd_wins_refresh(struct tevent_context *ev, struct tevent_timer *te,
43                               struct timeval t, void *private_data);
44
45 /*
46   retry a WINS name registration
47 */
48 static void nbtd_wins_register_retry(struct tevent_context *ev, struct tevent_timer *te,
49                                      struct timeval t, void *private_data)
50 {
51         struct nbtd_iface_name *iname = talloc_get_type(private_data, struct nbtd_iface_name);
52         nbtd_winsclient_register(iname);
53 }
54
55 /*
56   start a timer to refresh this name
57 */
58 static void nbtd_wins_start_refresh_timer(struct nbtd_iface_name *iname)
59 {
60         uint32_t refresh_time;
61         uint32_t max_refresh_time = lp_parm_int(iname->iface->nbtsrv->task->lp_ctx, NULL, "nbtd", "max_refresh_time", 7200);
62
63         refresh_time = MIN(max_refresh_time, iname->ttl/2);
64         
65         event_add_timed(iname->iface->nbtsrv->task->event_ctx, 
66                         iname, 
67                         timeval_add(&iname->registration_time, refresh_time, 0),
68                         nbtd_wins_refresh, iname);
69 }
70
71 /*
72   called when a wins name refresh has completed
73 */
74 static void nbtd_wins_refresh_handler(struct composite_context *c)
75 {
76         NTSTATUS status;
77         struct nbt_name_refresh_wins io;
78         struct nbtd_iface_name *iname = talloc_get_type(c->async.private_data, 
79                                                         struct nbtd_iface_name);
80         TALLOC_CTX *tmp_ctx = talloc_new(iname);
81
82         status = nbt_name_refresh_wins_recv(c, tmp_ctx, &io);
83         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
84                 /* our WINS server is dead - start registration over
85                    from scratch */
86                 DEBUG(2,("Failed to refresh %s with WINS server %s\n",
87                          nbt_name_string(tmp_ctx, &iname->name), iname->wins_server));
88                 talloc_free(tmp_ctx);
89                 nbtd_winsclient_register(iname);
90                 return;
91         }
92
93         if (!NT_STATUS_IS_OK(status)) {
94                 DEBUG(1,("Name refresh failure with WINS for %s - %s\n", 
95                          nbt_name_string(tmp_ctx, &iname->name), nt_errstr(status)));
96                 talloc_free(tmp_ctx);
97                 return;
98         }
99
100         if (io.out.rcode != 0) {
101                 DEBUG(1,("WINS server %s rejected name refresh of %s - %s\n", 
102                          io.out.wins_server, nbt_name_string(tmp_ctx, &iname->name), 
103                          nt_errstr(nbt_rcode_to_ntstatus(io.out.rcode))));
104                 iname->nb_flags |= NBT_NM_CONFLICT;
105                 talloc_free(tmp_ctx);
106                 return;
107         }       
108
109         DEBUG(4,("Refreshed name %s with WINS server %s\n",
110                  nbt_name_string(tmp_ctx, &iname->name), iname->wins_server));
111         /* success - start a periodic name refresh */
112         iname->nb_flags |= NBT_NM_ACTIVE;
113         if (iname->wins_server) {
114                 /*
115                  * talloc_free() would generate a warning,
116                  * so steal it into the tmp context
117                  */
118                 talloc_steal(tmp_ctx, iname->wins_server);
119         }
120         iname->wins_server = talloc_steal(iname, io.out.wins_server);
121
122         iname->registration_time = timeval_current();
123         nbtd_wins_start_refresh_timer(iname);
124
125         talloc_free(tmp_ctx);
126 }
127
128
129 /*
130   refresh a WINS name registration
131 */
132 static void nbtd_wins_refresh(struct tevent_context *ev, struct tevent_timer *te,
133                               struct timeval t, void *private_data)
134 {
135         struct nbtd_iface_name *iname = talloc_get_type(private_data, struct nbtd_iface_name);
136         struct nbtd_interface *iface = iname->iface;
137         struct nbt_name_refresh_wins io;
138         struct composite_context *c;
139         TALLOC_CTX *tmp_ctx = talloc_new(iname);
140
141         /* setup a wins name refresh request */
142         io.in.name            = iname->name;
143         io.in.wins_servers    = (const char **)str_list_make_single(tmp_ctx, iname->wins_server);
144         io.in.wins_port       = lp_nbt_port(iface->nbtsrv->task->lp_ctx);
145         io.in.addresses       = nbtd_address_list(iface, tmp_ctx);
146         io.in.nb_flags        = iname->nb_flags;
147         io.in.ttl             = iname->ttl;
148
149         if (!io.in.addresses) {
150                 talloc_free(tmp_ctx);
151                 return;
152         }
153
154         c = nbt_name_refresh_wins_send(wins_socket(iface), &io);
155         if (c == NULL) {
156                 talloc_free(tmp_ctx);
157                 return;
158         }
159         talloc_steal(c, io.in.addresses);
160
161         c->async.fn = nbtd_wins_refresh_handler;
162         c->async.private_data = iname;
163
164         talloc_free(tmp_ctx);
165 }
166
167
168 /*
169   called when a wins name register has completed
170 */
171 static void nbtd_wins_register_handler(struct composite_context *c)
172 {
173         NTSTATUS status;
174         struct nbt_name_register_wins io;
175         struct nbtd_iface_name *iname = talloc_get_type(c->async.private_data, 
176                                                         struct nbtd_iface_name);
177         TALLOC_CTX *tmp_ctx = talloc_new(iname);
178
179         status = nbt_name_register_wins_recv(c, tmp_ctx, &io);
180         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
181                 /* none of the WINS servers responded - try again 
182                    periodically */
183                 int wins_retry_time = lp_parm_int(iname->iface->nbtsrv->task->lp_ctx, NULL, "nbtd", "wins_retry", 300);
184                 event_add_timed(iname->iface->nbtsrv->task->event_ctx, 
185                                 iname,
186                                 timeval_current_ofs(wins_retry_time, 0),
187                                 nbtd_wins_register_retry,
188                                 iname);
189                 talloc_free(tmp_ctx);
190                 return;
191         }
192
193         if (!NT_STATUS_IS_OK(status)) {
194                 DEBUG(1,("Name register failure with WINS for %s - %s\n", 
195                          nbt_name_string(tmp_ctx, &iname->name), nt_errstr(status)));
196                 talloc_free(tmp_ctx);
197                 return;
198         }       
199
200         if (io.out.rcode != 0) {
201                 DEBUG(1,("WINS server %s rejected name register of %s - %s\n", 
202                          io.out.wins_server, nbt_name_string(tmp_ctx, &iname->name), 
203                          nt_errstr(nbt_rcode_to_ntstatus(io.out.rcode))));
204                 iname->nb_flags |= NBT_NM_CONFLICT;
205                 talloc_free(tmp_ctx);
206                 return;
207         }       
208
209         /* success - start a periodic name refresh */
210         iname->nb_flags |= NBT_NM_ACTIVE;
211         if (iname->wins_server) {
212                 /*
213                  * talloc_free() would generate a warning,
214                  * so steal it into the tmp context
215                  */
216                 talloc_steal(tmp_ctx, iname->wins_server);
217         }
218         iname->wins_server = talloc_steal(iname, io.out.wins_server);
219
220         iname->registration_time = timeval_current();
221         nbtd_wins_start_refresh_timer(iname);
222
223         DEBUG(3,("Registered %s with WINS server %s\n",
224                  nbt_name_string(tmp_ctx, &iname->name), iname->wins_server));
225
226         talloc_free(tmp_ctx);
227 }
228
229 /*
230   register a name with our WINS servers
231 */
232 void nbtd_winsclient_register(struct nbtd_iface_name *iname)
233 {
234         struct nbtd_interface *iface = iname->iface;
235         struct nbt_name_register_wins io;
236         struct composite_context *c;
237
238         /* setup a wins name register request */
239         io.in.name            = iname->name;
240         io.in.wins_port       = lp_nbt_port(iname->iface->nbtsrv->task->lp_ctx);
241         io.in.wins_servers    = lp_wins_server_list(iname->iface->nbtsrv->task->lp_ctx);
242         io.in.addresses       = nbtd_address_list(iface, iname);
243         io.in.nb_flags        = iname->nb_flags;
244         io.in.ttl             = iname->ttl;
245
246         if (!io.in.addresses) {
247                 return;
248         }
249
250         c = nbt_name_register_wins_send(wins_socket(iface), &io);
251         if (c == NULL) {
252                 talloc_free(io.in.addresses);
253                 return;
254         }
255         talloc_steal(c, io.in.addresses);
256
257         c->async.fn = nbtd_wins_register_handler;
258         c->async.private_data = iname;
259 }