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