r5171: added support for "bind interfaces only" in nbtd. The solution was to
[amitay/samba.git] / source4 / nbt_server / register.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    register our names
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 "events.h"
25 #include "dlinklist.h"
26 #include "nbt_server/nbt_server.h"
27 #include "smbd/service_task.h"
28 #include "libcli/raw/libcliraw.h"
29 #include "libcli/composite/composite.h"
30
31
32 static void nbt_start_refresh_timer(struct nbt_iface_name *iname);
33
34 /*
35   a name refresh request has completed
36 */
37 static void refresh_completion_handler(struct nbt_name_request *req)
38 {
39         struct nbt_iface_name *iname = talloc_get_type(req->async.private, struct nbt_iface_name);
40         NTSTATUS status;
41         struct nbt_name_refresh io;
42         TALLOC_CTX *tmp_ctx = talloc_new(iname);
43
44         status = nbt_name_refresh_recv(req, tmp_ctx, &io);
45         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
46                 DEBUG(4,("Refreshed name %s<%02x> on %s\n", 
47                          iname->name.name, iname->name.type, iname->iface->ip_address));
48                 iname->registration_time = timeval_current();
49                 nbt_start_refresh_timer(iname);
50                 talloc_free(tmp_ctx);
51                 return;
52         }
53
54         iname->nb_flags |= NBT_NM_CONFLICT;
55         iname->nb_flags &= ~NBT_NM_ACTIVE;
56
57         if (NT_STATUS_IS_OK(status)) {
58                 DEBUG(1,("Name conflict from %s refreshing name %s<%02x> on %s - rcode %d\n", 
59                          io.out.reply_addr, iname->name.name, iname->name.type, 
60                          iname->iface->ip_address, io.out.rcode));
61         } else {
62                 DEBUG(1,("Error refreshing name %s<%02x> on %s - %s\n", 
63                          iname->name.name, iname->name.type, iname->iface->ip_address,
64                          nt_errstr(status)));
65         }
66
67         talloc_free(tmp_ctx);
68 }
69
70
71 /*
72   handle name refresh timer events
73 */
74 static void name_refresh_handler(struct event_context *ev, struct timed_event *te, 
75                                  struct timeval t)
76 {
77         struct nbt_iface_name *iname = talloc_get_type(te->private, struct nbt_iface_name);
78         struct nbt_interface *iface = iname->iface;
79         struct nbt_name_refresh io;
80         struct nbt_name_request *req;
81
82         /* setup a name refresh request */
83         io.in.name            = iname->name;
84         io.in.dest_addr       = iface->bcast_address;
85         io.in.address         = iface->ip_address;
86         io.in.nb_flags        = iname->nb_flags;
87         io.in.ttl             = iname->ttl;
88         io.in.broadcast       = True;
89         io.in.timeout         = 3;
90
91         req = nbt_name_refresh_send(iface->nbtsock, &io);
92         if (req == NULL) return;
93
94         req->async.fn = refresh_completion_handler;
95         req->async.private = iname;
96 }
97
98
99 /*
100   start a timer to refresh this name
101 */
102 static void nbt_start_refresh_timer(struct nbt_iface_name *iname)
103 {
104         struct timed_event te;
105         uint32_t refresh_time;
106         uint32_t max_refresh_time = lp_parm_int(-1, "nbtd", "max_refresh_time", 7200);
107
108         refresh_time = MIN(max_refresh_time, iname->ttl/2);
109         
110         te.next_event = timeval_current_ofs(refresh_time, 0);
111         te.handler    = name_refresh_handler;
112         te.private    = iname;
113
114         event_add_timed(iname->iface->nbtsrv->task->event_ctx, &te, iname);
115 }
116
117
118 /*
119   a name registration has completed
120 */
121 static void nbt_register_handler(struct composite_context *req)
122 {
123         struct nbt_iface_name *iname = talloc_get_type(req->async.private, struct nbt_iface_name);
124         NTSTATUS status;
125
126         status = nbt_name_register_bcast_recv(req);
127         if (NT_STATUS_IS_OK(status)) {
128                 /* good - nobody complained about our registration */
129                 iname->nb_flags |= NBT_NM_ACTIVE;
130                 DEBUG(3,("Registered %s<%02x> on interface %s\n",
131                          iname->name.name, iname->name.type, iname->iface->bcast_address));
132                 iname->registration_time = timeval_current();
133                 nbt_start_refresh_timer(iname);
134                 return;
135         }
136
137         /* someone must have replied with an objection! */
138         iname->nb_flags |= NBT_NM_CONFLICT;
139
140         DEBUG(1,("Error registering %s<%02x> on interface %s - %s\n",
141                  iname->name.name, iname->name.type, iname->iface->bcast_address,
142                  nt_errstr(status)));
143 }
144
145
146 /*
147   register a name on a network interface
148 */
149 static void nbt_register_name_iface(struct nbt_interface *iface,
150                                     const char *name, enum nbt_name_type type,
151                                     uint16_t nb_flags)
152 {
153         struct nbt_iface_name *iname;
154         const char *scope = lp_netbios_scope();
155         struct nbt_name_register_bcast io;
156         struct composite_context *req;
157
158         iname = talloc(iface, struct nbt_iface_name);
159         if (!iname) return;
160
161         iname->iface     = iface;
162         iname->name.name = talloc_strdup(iname, name);
163         iname->name.type = type;
164         if (scope && *scope) {
165                 iname->name.scope = talloc_strdup(iname, scope);
166         } else {
167                 iname->name.scope = NULL;
168         }
169         iname->nb_flags          = nb_flags;
170         iname->ttl               = lp_parm_int(-1, "nbtd", "bcast_ttl", 300000);
171         iname->registration_time = timeval_zero();
172
173         DLIST_ADD_END(iface->names, iname, struct nbt_iface_name *);
174
175         if (nb_flags & NBT_NM_PERMANENT) {
176                 /* permanent names are not announced and are immediately active */
177                 iname->nb_flags |= NBT_NM_ACTIVE;
178                 iname->ttl       = 0;
179                 return;
180         }
181
182         /* setup a broadcast name registration request */
183         io.in.name            = iname->name;
184         io.in.dest_addr       = iface->bcast_address;
185         io.in.address         = iface->ip_address;
186         io.in.nb_flags        = nb_flags;
187         io.in.ttl             = iname->ttl;
188
189         req = nbt_name_register_bcast_send(iface->nbtsock, &io);
190         if (req == NULL) return;
191
192         req->async.fn = nbt_register_handler;
193         req->async.private = iname;
194 }
195
196
197 /*
198   register one name on all our interfaces
199 */
200 static void nbt_register_name(struct nbt_server *nbtsrv, 
201                               const char *name, enum nbt_name_type type,
202                               uint16_t nb_flags)
203 {
204         struct nbt_interface *iface;
205         
206         /* register with all the local interfaces */
207         for (iface=nbtsrv->interfaces;iface;iface=iface->next) {
208                 nbt_register_name_iface(iface, name, type, nb_flags);
209         }
210
211         /* register on our general broadcast interface as a permanent name */
212         if (nbtsrv->bcast_interface) {
213                 nbt_register_name_iface(nbtsrv->bcast_interface, name, type, 
214                                         nb_flags | NBT_NM_PERMANENT);
215         }
216
217         /* TODO: register with our WINS servers */
218 }
219
220
221 /*
222   register our names on all interfaces
223 */
224 void nbt_register_names(struct nbt_server *nbtsrv)
225 {
226         uint16_t nb_flags = NBT_NODE_M;
227
228         /* note that we don't initially mark the names "ACTIVE". They are 
229            marked active once registration is successful */
230         nbt_register_name(nbtsrv, lp_netbios_name(), NBT_NAME_CLIENT, nb_flags);
231         nbt_register_name(nbtsrv, lp_netbios_name(), NBT_NAME_USER,   nb_flags);
232         nbt_register_name(nbtsrv, lp_netbios_name(), NBT_NAME_SERVER, nb_flags);
233
234         nb_flags |= NBT_NM_GROUP;
235         nbt_register_name(nbtsrv, lp_workgroup(),    NBT_NAME_CLIENT, nb_flags);
236
237         nb_flags |= NBT_NM_PERMANENT;
238         nbt_register_name(nbtsrv, "__SAMBA__",       NBT_NAME_CLIENT, nb_flags);
239         nbt_register_name(nbtsrv, "__SAMBA__",       NBT_NAME_SERVER, nb_flags);
240         nbt_register_name(nbtsrv, "*",               NBT_NAME_CLIENT, nb_flags);
241 }