r21584: Support for tagged types has landed!
[Samba/ekacnet.git] / source / nbt_server / register.c
blob6e65b753b1442f0f0a07331babdb59e9596e7934
1 /*
2 Unix SMB/CIFS implementation.
4 register our names
6 Copyright (C) Andrew Tridgell 2005
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.
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.
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.
23 #include "includes.h"
24 #include "lib/events/events.h"
25 #include "lib/util/dlinklist.h"
26 #include "nbt_server/nbt_server.h"
27 #include "smbd/service_task.h"
28 #include "libcli/composite/composite.h"
29 #include "librpc/gen_ndr/ndr_samr.h"
30 #include "nbt_server/wins/winsserver.h"
31 #include "librpc/gen_ndr/ndr_nbt.h"
32 #include "dsdb/samdb/samdb.h"
35 static void nbtd_start_refresh_timer(struct nbtd_iface_name *iname);
38 a name refresh request has completed
40 static void refresh_completion_handler(struct nbt_name_request *req)
42 struct nbtd_iface_name *iname = talloc_get_type(req->async.private,
43 struct nbtd_iface_name);
44 NTSTATUS status;
45 struct nbt_name_refresh io;
46 TALLOC_CTX *tmp_ctx = talloc_new(iname);
48 status = nbt_name_refresh_recv(req, tmp_ctx, &io);
49 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
50 DEBUG(4,("Refreshed name %s with %s on interface %s\n",
51 nbt_name_string(tmp_ctx, &iname->name),
52 iname->iface->ip_address, iname->iface->bcast_address));
53 iname->registration_time = timeval_current();
54 nbtd_start_refresh_timer(iname);
55 talloc_free(tmp_ctx);
56 return;
59 iname->nb_flags |= NBT_NM_CONFLICT;
60 iname->nb_flags &= ~NBT_NM_ACTIVE;
62 if (NT_STATUS_IS_OK(status)) {
63 DEBUG(1,("Name conflict from %s refreshing name %s with %s on interface %s - %s\n",
64 io.out.reply_addr, nbt_name_string(tmp_ctx, &iname->name),
65 iname->iface->ip_address, iname->iface->bcast_address,
66 nt_errstr(nbt_rcode_to_ntstatus(io.out.rcode))));
67 } else {
68 DEBUG(1,("Error refreshing name %s with %s on interface %s - %s\n",
69 nbt_name_string(tmp_ctx, &iname->name),
70 iname->iface->ip_address, iname->iface->bcast_address,
71 nt_errstr(status)));
74 talloc_free(tmp_ctx);
79 handle name refresh timer events
81 static void name_refresh_handler(struct event_context *ev, struct timed_event *te,
82 struct timeval t, void *private_data)
84 struct nbtd_iface_name *iname = talloc_get_type(private_data, struct nbtd_iface_name);
85 struct nbtd_interface *iface = iname->iface;
86 struct nbt_name_register io;
87 struct nbt_name_request *req;
88 struct nbtd_server *nbtsrv = iface->nbtsrv;
90 /* setup a single name register request. Notice that we don't
91 use a name refresh request, as Windows and Samba3 do not
92 defend against broadcast name refresh packets. So for this
93 to be of any use at all, we need to refresh using name
94 registration packets */
95 io.in.name = iname->name;
96 io.in.dest_addr = iface->bcast_address;
97 io.in.address = iface->ip_address;
98 io.in.nb_flags = iname->nb_flags;
99 io.in.ttl = iname->ttl;
100 io.in.register_demand = False;
101 io.in.broadcast = True;
102 io.in.multi_homed = False;
103 io.in.timeout = 3;
104 io.in.retries = 0;
106 nbtsrv->stats.total_sent++;
107 req = nbt_name_register_send(iface->nbtsock, &io);
108 if (req == NULL) return;
110 req->async.fn = refresh_completion_handler;
111 req->async.private = iname;
116 start a timer to refresh this name
118 static void nbtd_start_refresh_timer(struct nbtd_iface_name *iname)
120 uint32_t refresh_time;
121 uint32_t max_refresh_time = lp_parm_int(-1, "nbtd", "max_refresh_time", 7200);
123 refresh_time = MIN(max_refresh_time, iname->ttl/2);
125 event_add_timed(iname->iface->nbtsrv->task->event_ctx,
126 iname,
127 timeval_add(&iname->registration_time, refresh_time, 0),
128 name_refresh_handler, iname);
133 a name registration has completed
135 static void nbtd_register_handler(struct composite_context *creq)
137 struct nbtd_iface_name *iname = talloc_get_type(creq->async.private_data,
138 struct nbtd_iface_name);
139 NTSTATUS status;
140 TALLOC_CTX *tmp_ctx = talloc_new(iname);
142 status = nbt_name_register_bcast_recv(creq);
143 if (NT_STATUS_IS_OK(status)) {
144 /* good - nobody complained about our registration */
145 iname->nb_flags |= NBT_NM_ACTIVE;
146 DEBUG(3,("Registered %s with %s on interface %s\n",
147 nbt_name_string(tmp_ctx, &iname->name),
148 iname->iface->ip_address, iname->iface->bcast_address));
149 iname->registration_time = timeval_current();
150 talloc_free(tmp_ctx);
151 nbtd_start_refresh_timer(iname);
152 return;
155 /* someone must have replied with an objection! */
156 iname->nb_flags |= NBT_NM_CONFLICT;
158 DEBUG(1,("Error registering %s with %s on interface %s - %s\n",
159 nbt_name_string(tmp_ctx, &iname->name),
160 iname->iface->ip_address, iname->iface->bcast_address,
161 nt_errstr(status)));
162 talloc_free(tmp_ctx);
167 register a name on a network interface
169 static void nbtd_register_name_iface(struct nbtd_interface *iface,
170 const char *name, enum nbt_name_type type,
171 uint16_t nb_flags)
173 struct nbtd_iface_name *iname;
174 const char *scope = lp_netbios_scope();
175 struct nbt_name_register_bcast io;
176 struct composite_context *creq;
177 struct nbtd_server *nbtsrv = iface->nbtsrv;
179 iname = talloc(iface, struct nbtd_iface_name);
180 if (!iname) return;
182 iname->iface = iface;
183 iname->name.name = strupper_talloc(iname, name);
184 iname->name.type = type;
185 if (scope && *scope) {
186 iname->name.scope = strupper_talloc(iname, scope);
187 } else {
188 iname->name.scope = NULL;
190 iname->nb_flags = nb_flags;
191 iname->ttl = lp_parm_int(-1, "nbtd", "bcast_ttl", 300000);
192 iname->registration_time = timeval_zero();
193 iname->wins_server = NULL;
195 DLIST_ADD_END(iface->names, iname, struct nbtd_iface_name *);
197 if (nb_flags & NBT_NM_PERMANENT) {
198 /* permanent names are not announced and are immediately active */
199 iname->nb_flags |= NBT_NM_ACTIVE;
200 iname->ttl = 0;
201 return;
204 /* if this is the wins interface, then we need to do a special
205 wins name registration */
206 if (iface == iface->nbtsrv->wins_interface) {
207 nbtd_winsclient_register(iname);
208 return;
211 /* setup a broadcast name registration request */
212 io.in.name = iname->name;
213 io.in.dest_addr = iface->bcast_address;
214 io.in.address = iface->ip_address;
215 io.in.nb_flags = nb_flags;
216 io.in.ttl = iname->ttl;
218 nbtsrv->stats.total_sent++;
219 creq = nbt_name_register_bcast_send(iface->nbtsock, &io);
220 if (creq == NULL) return;
222 creq->async.fn = nbtd_register_handler;
223 creq->async.private_data = iname;
228 register one name on all our interfaces
230 static void nbtd_register_name(struct nbtd_server *nbtsrv,
231 const char *name, enum nbt_name_type type,
232 uint16_t nb_flags)
234 struct nbtd_interface *iface;
236 /* register with all the local interfaces */
237 for (iface=nbtsrv->interfaces;iface;iface=iface->next) {
238 nbtd_register_name_iface(iface, name, type, nb_flags);
241 /* register on our general broadcast interface as a permanent name */
242 if (nbtsrv->bcast_interface) {
243 nbtd_register_name_iface(nbtsrv->bcast_interface, name, type,
244 nb_flags | NBT_NM_PERMANENT);
247 /* register with our WINS servers */
248 if (nbtsrv->wins_interface) {
249 nbtd_register_name_iface(nbtsrv->wins_interface, name, type, nb_flags);
255 register our names on all interfaces
257 void nbtd_register_names(struct nbtd_server *nbtsrv)
259 uint16_t nb_flags = NBT_NODE_M;
260 const char **aliases;
262 /* note that we don't initially mark the names "ACTIVE". They are
263 marked active once registration is successful */
264 nbtd_register_name(nbtsrv, lp_netbios_name(), NBT_NAME_CLIENT, nb_flags);
265 nbtd_register_name(nbtsrv, lp_netbios_name(), NBT_NAME_USER, nb_flags);
266 nbtd_register_name(nbtsrv, lp_netbios_name(), NBT_NAME_SERVER, nb_flags);
268 aliases = lp_netbios_aliases();
269 while (aliases && aliases[0]) {
270 nbtd_register_name(nbtsrv, aliases[0], NBT_NAME_CLIENT, nb_flags);
271 nbtd_register_name(nbtsrv, aliases[0], NBT_NAME_SERVER, nb_flags);
272 aliases++;
275 if (lp_server_role() == ROLE_DOMAIN_CONTROLLER) {
276 BOOL is_pdc = samdb_is_pdc(nbtsrv->sam_ctx);
277 if (is_pdc) {
278 nbtd_register_name(nbtsrv, lp_workgroup(),
279 NBT_NAME_PDC, nb_flags);
281 nbtd_register_name(nbtsrv, lp_workgroup(),
282 NBT_NAME_LOGON, nb_flags | NBT_NM_GROUP);
285 nb_flags |= NBT_NM_GROUP;
286 nbtd_register_name(nbtsrv, lp_workgroup(), NBT_NAME_CLIENT, nb_flags);
288 nb_flags |= NBT_NM_PERMANENT;
289 nbtd_register_name(nbtsrv, "__SAMBA__", NBT_NAME_CLIENT, nb_flags);
290 nbtd_register_name(nbtsrv, "__SAMBA__", NBT_NAME_SERVER, nb_flags);
291 nbtd_register_name(nbtsrv, "*", NBT_NAME_CLIENT, nb_flags);