e941d77e2882d178c5523870e4c63b2b71bb123f
[obnox/samba/samba-obnox.git] / source4 / nbt_server / 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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "nbt_server/nbt_server.h"
25 #include "libcli/raw/libcliraw.h"
26 #include "libcli/composite/composite.h"
27 #include "lib/events/events.h"
28 #include "smbd/service_task.h"
29
30
31 /* we send WINS client requests using our primary network interface 
32 */
33 static struct nbt_name_socket *wins_socket(struct nbtd_interface *iface)
34 {
35         struct nbtd_server *nbtsrv = iface->nbtsrv;
36         return nbtsrv->interfaces->nbtsock;
37 }
38
39
40 static void nbtd_wins_refresh(struct event_context *ev, struct timed_event *te,
41                               struct timeval t, void *private);
42
43 /*
44   retry a WINS name registration
45 */
46 static void nbtd_wins_register_retry(struct event_context *ev, struct timed_event *te,
47                                      struct timeval t, void *private)
48 {
49         struct nbtd_iface_name *iname = talloc_get_type(private, struct nbtd_iface_name);
50         nbtd_winsclient_register(iname);
51 }
52
53
54 /*
55   called when a wins name refresh has completed
56 */
57 static void nbtd_wins_refresh_handler(struct composite_context *c)
58 {
59         NTSTATUS status;
60         struct nbt_name_refresh_wins io;
61         struct nbtd_iface_name *iname = talloc_get_type(c->async.private, 
62                                                         struct nbtd_iface_name);
63         TALLOC_CTX *tmp_ctx = talloc_new(iname);
64
65         status = nbt_name_refresh_wins_recv(c, tmp_ctx, &io);
66         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
67                 /* our WINS server is dead - start registration over
68                    from scratch */
69                 DEBUG(2,("Failed to refresh %s<%02x> with WINS server %s\n",
70                          iname->name.name, iname->name.type, iname->wins_server));
71                 nbtd_winsclient_register(iname);
72                 return;
73         }
74
75         if (!NT_STATUS_IS_OK(status)) {
76                 DEBUG(1,("Name refresh failure with WINS for %s<%02x> - %s\n", 
77                          iname->name.name, iname->name.type, nt_errstr(status)));
78                 talloc_free(tmp_ctx);
79                 return;
80         }       
81
82         if (io.out.rcode != 0) {
83                 DEBUG(1,("WINS server %s rejected name refresh of %s<%02x> - %s\n", 
84                          io.out.wins_server, iname->name.name, iname->name.type, 
85                          nt_errstr(nbt_rcode_to_ntstatus(io.out.rcode))));
86                 iname->nb_flags |= NBT_NM_CONFLICT;
87                 talloc_free(tmp_ctx);
88                 return;
89         }       
90
91         DEBUG(4,("Refreshed name %s<%02x> with WINS server %s\n",
92                  iname->name.name, iname->name.type, iname->wins_server));
93         /* success - start a periodic name refresh */
94         iname->nb_flags |= NBT_NM_ACTIVE;
95         if (iname->wins_server) {
96                 talloc_free(iname->wins_server);
97         }
98         iname->wins_server = talloc_steal(iname, io.out.wins_server);
99
100         iname->registration_time = timeval_current();
101         event_add_timed(iname->iface->nbtsrv->task->event_ctx, 
102                         iname,
103                         timeval_add(&iname->registration_time, iname->ttl/2, 0),
104                         nbtd_wins_refresh,
105                         iname);
106
107         talloc_free(tmp_ctx);
108 }
109
110
111 /*
112   refresh a WINS name registration
113 */
114 static void nbtd_wins_refresh(struct event_context *ev, struct timed_event *te,
115                               struct timeval t, void *private)
116 {
117         struct nbtd_iface_name *iname = talloc_get_type(private, struct nbtd_iface_name);
118         struct nbtd_interface *iface = iname->iface;
119         struct nbt_name_refresh_wins io;
120         struct composite_context *c;
121         TALLOC_CTX *tmp_ctx = talloc_new(iname);
122
123         /* setup a wins name refresh request */
124         io.in.name            = iname->name;
125         io.in.wins_servers    = str_list_make(tmp_ctx, iname->wins_server, NULL);
126         io.in.addresses       = nbtd_address_list(iface, tmp_ctx);
127         io.in.nb_flags        = iname->nb_flags;
128         io.in.ttl             = iname->ttl;
129
130         c = nbt_name_refresh_wins_send(wins_socket(iface), &io);
131         if (c == NULL) {
132                 talloc_free(tmp_ctx);
133                 return;
134         }
135         talloc_steal(c, io.in.addresses);
136
137         c->async.fn = nbtd_wins_refresh_handler;
138         c->async.private = iname;
139
140         talloc_free(tmp_ctx);
141 }
142
143
144 /*
145   called when a wins name register has completed
146 */
147 static void nbtd_wins_register_handler(struct composite_context *c)
148 {
149         NTSTATUS status;
150         struct nbt_name_register_wins io;
151         struct nbtd_iface_name *iname = talloc_get_type(c->async.private, 
152                                                         struct nbtd_iface_name);
153         TALLOC_CTX *tmp_ctx = talloc_new(iname);
154
155         status = nbt_name_register_wins_recv(c, tmp_ctx, &io);
156         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
157                 /* none of the WINS servers responded - try again 
158                    periodically */
159                 int wins_retry_time = lp_parm_int(-1, "nbtd", "wins_retry", 300);
160                 event_add_timed(iname->iface->nbtsrv->task->event_ctx, 
161                                 iname,
162                                 timeval_current_ofs(wins_retry_time, 0),
163                                 nbtd_wins_register_retry,
164                                 iname);
165                 talloc_free(tmp_ctx);
166                 return;
167         }
168
169         if (!NT_STATUS_IS_OK(status)) {
170                 DEBUG(1,("Name register failure with WINS for %s<%02x> - %s\n", 
171                          iname->name.name, iname->name.type, nt_errstr(status)));
172                 talloc_free(tmp_ctx);
173                 return;
174         }       
175
176         if (io.out.rcode != 0) {
177                 DEBUG(1,("WINS server %s rejected name register of %s<%02x> - %s\n", 
178                          io.out.wins_server, iname->name.name, iname->name.type, 
179                          nt_errstr(nbt_rcode_to_ntstatus(io.out.rcode))));
180                 iname->nb_flags |= NBT_NM_CONFLICT;
181                 talloc_free(tmp_ctx);
182                 return;
183         }       
184
185         /* success - start a periodic name refresh */
186         iname->nb_flags |= NBT_NM_ACTIVE;
187         if (iname->wins_server) {
188                 talloc_free(iname->wins_server);
189         }
190         iname->wins_server = talloc_steal(iname, io.out.wins_server);
191
192         iname->registration_time = timeval_current();
193         event_add_timed(iname->iface->nbtsrv->task->event_ctx, 
194                         iname,
195                         timeval_add(&iname->registration_time, iname->ttl/2, 0),
196                         nbtd_wins_refresh,
197                         iname);
198
199         DEBUG(3,("Registered %s<%02x> with WINS server %s\n",
200                  iname->name.name, iname->name.type, iname->wins_server));
201
202         talloc_free(tmp_ctx);
203 }
204
205 /*
206   register a name with our WINS servers
207 */
208 void nbtd_winsclient_register(struct nbtd_iface_name *iname)
209 {
210         struct nbtd_interface *iface = iname->iface;
211         struct nbt_name_register_wins io;
212         struct composite_context *c;
213
214         /* setup a wins name register request */
215         io.in.name            = iname->name;
216         io.in.wins_servers    = lp_wins_server_list();
217         io.in.addresses       = nbtd_address_list(iface, iname);
218         io.in.nb_flags        = iname->nb_flags;
219         io.in.ttl             = iname->ttl;
220
221         c = nbt_name_register_wins_send(wins_socket(iface), &io);
222         if (c == NULL) {
223                 talloc_free(io.in.addresses);
224                 return;
225         }
226         talloc_steal(c, io.in.addresses);
227
228         c->async.fn = nbtd_wins_register_handler;
229         c->async.private = iname;
230 }