1dd934cc92bea18f2878e6c286a7f553b292f1cc
[bbaumbach/samba-autobuild/.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 event_context *ev, struct timed_event *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 event_context *ev, struct timed_event *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(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 event_context *ev, struct timed_event *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    = str_list_make(tmp_ctx, iname->wins_server, NULL);
144         io.in.addresses       = nbtd_address_list(iface, tmp_ctx);
145         io.in.nb_flags        = iname->nb_flags;
146         io.in.ttl             = iname->ttl;
147
148         if (!io.in.addresses) {
149                 talloc_free(tmp_ctx);
150                 return;
151         }
152
153         c = nbt_name_refresh_wins_send(wins_socket(iface), &io);
154         if (c == NULL) {
155                 talloc_free(tmp_ctx);
156                 return;
157         }
158         talloc_steal(c, io.in.addresses);
159
160         c->async.fn = nbtd_wins_refresh_handler;
161         c->async.private_data = iname;
162
163         talloc_free(tmp_ctx);
164 }
165
166
167 /*
168   called when a wins name register has completed
169 */
170 static void nbtd_wins_register_handler(struct composite_context *c)
171 {
172         NTSTATUS status;
173         struct nbt_name_register_wins io;
174         struct nbtd_iface_name *iname = talloc_get_type(c->async.private_data, 
175                                                         struct nbtd_iface_name);
176         TALLOC_CTX *tmp_ctx = talloc_new(iname);
177
178         status = nbt_name_register_wins_recv(c, tmp_ctx, &io);
179         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
180                 /* none of the WINS servers responded - try again 
181                    periodically */
182                 int wins_retry_time = lp_parm_int(NULL, "nbtd", "wins_retry", 300);
183                 event_add_timed(iname->iface->nbtsrv->task->event_ctx, 
184                                 iname,
185                                 timeval_current_ofs(wins_retry_time, 0),
186                                 nbtd_wins_register_retry,
187                                 iname);
188                 talloc_free(tmp_ctx);
189                 return;
190         }
191
192         if (!NT_STATUS_IS_OK(status)) {
193                 DEBUG(1,("Name register failure with WINS for %s - %s\n", 
194                          nbt_name_string(tmp_ctx, &iname->name), nt_errstr(status)));
195                 talloc_free(tmp_ctx);
196                 return;
197         }       
198
199         if (io.out.rcode != 0) {
200                 DEBUG(1,("WINS server %s rejected name register of %s - %s\n", 
201                          io.out.wins_server, nbt_name_string(tmp_ctx, &iname->name), 
202                          nt_errstr(nbt_rcode_to_ntstatus(io.out.rcode))));
203                 iname->nb_flags |= NBT_NM_CONFLICT;
204                 talloc_free(tmp_ctx);
205                 return;
206         }       
207
208         /* success - start a periodic name refresh */
209         iname->nb_flags |= NBT_NM_ACTIVE;
210         if (iname->wins_server) {
211                 /*
212                  * talloc_free() would generate a warning,
213                  * so steal it into the tmp context
214                  */
215                 talloc_steal(tmp_ctx, iname->wins_server);
216         }
217         iname->wins_server = talloc_steal(iname, io.out.wins_server);
218
219         iname->registration_time = timeval_current();
220         nbtd_wins_start_refresh_timer(iname);
221
222         DEBUG(3,("Registered %s with WINS server %s\n",
223                  nbt_name_string(tmp_ctx, &iname->name), iname->wins_server));
224
225         talloc_free(tmp_ctx);
226 }
227
228 /*
229   register a name with our WINS servers
230 */
231 void nbtd_winsclient_register(struct nbtd_iface_name *iname)
232 {
233         struct nbtd_interface *iface = iname->iface;
234         struct nbt_name_register_wins io;
235         struct composite_context *c;
236
237         /* setup a wins name register request */
238         io.in.name            = iname->name;
239         io.in.wins_servers    = lp_wins_server_list(global_loadparm);
240         io.in.addresses       = nbtd_address_list(iface, iname);
241         io.in.nb_flags        = iname->nb_flags;
242         io.in.ttl             = iname->ttl;
243
244         if (!io.in.addresses) {
245                 return;
246         }
247
248         c = nbt_name_register_wins_send(wins_socket(iface), &io);
249         if (c == NULL) {
250                 talloc_free(io.in.addresses);
251                 return;
252         }
253         talloc_steal(c, io.in.addresses);
254
255         c->async.fn = nbtd_wins_register_handler;
256         c->async.private_data = iname;
257 }