| 1 |
/* |
| 2 |
Unix SMB/CIFS implementation. |
| 3 |
|
| 4 |
NBT WINS server testing |
| 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 "lib/util/dlinklist.h" |
| 24 |
#include "lib/events/events.h" |
| 25 |
#include "lib/socket/socket.h" |
| 26 |
#include "libcli/resolve/resolve.h" |
| 27 |
#include "system/network.h" |
| 28 |
#include "lib/socket/netif.h" |
| 29 |
#include "librpc/gen_ndr/ndr_nbt.h" |
| 30 |
#include "torture/torture.h" |
| 31 |
#include "torture/nbt/proto.h" |
| 32 |
#include "param/param.h" |
| 33 |
|
| 34 |
/* rcode used when you don't want to check the rcode */ |
| 35 |
#define WINS_TEST_RCODE_WE_DONT_CARE 255 |
| 36 |
|
| 37 |
|
| 38 |
#define CHECK_VALUE(tctx, v, correct) \ |
| 39 |
torture_assert_int_equal(tctx, v, correct, "Incorrect value") |
| 40 |
|
| 41 |
#define CHECK_STRING(tctx, v, correct) \ |
| 42 |
torture_assert_casestr_equal(tctx, v, correct, "Incorrect value") |
| 43 |
|
| 44 |
#define CHECK_NAME(tctx, _name, correct) do { \ |
| 45 |
CHECK_STRING(tctx, (_name).name, (correct).name); \ |
| 46 |
CHECK_VALUE(tctx, (uint8_t)(_name).type, (uint8_t)(correct).type); \ |
| 47 |
CHECK_STRING(tctx, (_name).scope, (correct).scope); \ |
| 48 |
} while (0) |
| 49 |
|
| 50 |
|
| 51 |
/* |
| 52 |
test operations against a WINS server |
| 53 |
*/ |
| 54 |
static bool nbt_test_wins_name(struct torture_context *tctx, const char *address, |
| 55 |
struct nbt_name *name, uint16_t nb_flags, |
| 56 |
bool try_low_port, |
| 57 |
uint8_t register_rcode) |
| 58 |
{ |
| 59 |
struct nbt_name_register_wins io; |
| 60 |
struct nbt_name_register name_register; |
| 61 |
struct nbt_name_query query; |
| 62 |
struct nbt_name_refresh_wins refresh; |
| 63 |
struct nbt_name_release release; |
| 64 |
struct nbt_name_request *req; |
| 65 |
NTSTATUS status; |
| 66 |
struct nbt_name_socket *nbtsock = torture_init_nbt_socket(tctx); |
| 67 |
const char *myaddress; |
| 68 |
struct socket_address *socket_address; |
| 69 |
struct interface *ifaces; |
| 70 |
bool low_port = try_low_port; |
| 71 |
char **l; |
| 72 |
|
| 73 |
load_interface_list(tctx, tctx->lp_ctx, &ifaces); |
| 74 |
|
| 75 |
myaddress = talloc_strdup(tctx, iface_list_best_ip(ifaces, address)); |
| 76 |
|
| 77 |
socket_address = socket_address_from_strings(tctx, |
| 78 |
nbtsock->sock->backend_name, |
| 79 |
myaddress, lpcfg_nbt_port(tctx->lp_ctx)); |
| 80 |
torture_assert(tctx, socket_address != NULL, |
| 81 |
"Error getting address"); |
| 82 |
|
| 83 |
/* we do the listen here to ensure the WINS server receives the packets from |
| 84 |
the right IP */ |
| 85 |
status = socket_listen(nbtsock->sock, socket_address, 0, 0); |
| 86 |
talloc_free(socket_address); |
| 87 |
if (!NT_STATUS_IS_OK(status)) { |
| 88 |
low_port = false; |
| 89 |
socket_address = socket_address_from_strings(tctx, |
| 90 |
nbtsock->sock->backend_name, |
| 91 |
myaddress, 0); |
| 92 |
torture_assert(tctx, socket_address != NULL, |
| 93 |
"Error getting address"); |
| 94 |
|
| 95 |
status = socket_listen(nbtsock->sock, socket_address, 0, 0); |
| 96 |
talloc_free(socket_address); |
| 97 |
torture_assert_ntstatus_ok(tctx, status, |
| 98 |
"socket_listen for WINS failed"); |
| 99 |
} |
| 100 |
|
| 101 |
torture_comment(tctx, "Testing name registration to WINS with name %s at %s nb_flags=0x%x\n", |
| 102 |
nbt_name_string(tctx, name), myaddress, nb_flags); |
| 103 |
|
| 104 |
torture_comment(tctx, "release the name\n"); |
| 105 |
release.in.name = *name; |
| 106 |
release.in.dest_port = lpcfg_nbt_port(tctx->lp_ctx); |
| 107 |
release.in.dest_addr = address; |
| 108 |
release.in.address = myaddress; |
| 109 |
release.in.nb_flags = nb_flags; |
| 110 |
release.in.broadcast = false; |
| 111 |
release.in.timeout = 3; |
| 112 |
release.in.retries = 0; |
| 113 |
|
| 114 |
status = nbt_name_release(nbtsock, tctx, &release); |
| 115 |
torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name query", address)); |
| 116 |
CHECK_VALUE(tctx, release.out.rcode, 0); |
| 117 |
|
| 118 |
if (nb_flags & NBT_NM_GROUP) { |
| 119 |
/* ignore this for group names */ |
| 120 |
} else if (!low_port) { |
| 121 |
torture_comment(tctx, "no low port - skip: register the name with a wrong address\n"); |
| 122 |
} else { |
| 123 |
torture_comment(tctx, "register the name with a wrong address (makes the next request slow!)\n"); |
| 124 |
io.in.name = *name; |
| 125 |
io.in.wins_port = lpcfg_nbt_port(tctx->lp_ctx); |
| 126 |
io.in.wins_servers = const_str_list( |
| 127 |
str_list_make_single(tctx, address)); |
| 128 |
io.in.addresses = const_str_list( |
| 129 |
str_list_make_single(tctx, "127.64.64.1")); |
| 130 |
io.in.nb_flags = nb_flags; |
| 131 |
io.in.ttl = 300000; |
| 132 |
|
| 133 |
status = nbt_name_register_wins(nbtsock, tctx, &io); |
| 134 |
if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) { |
| 135 |
torture_assert_ntstatus_ok(tctx, status, |
| 136 |
talloc_asprintf(tctx, "No response from %s for name register\n", |
| 137 |
address)); |
| 138 |
} |
| 139 |
torture_assert_ntstatus_ok(tctx, status, |
| 140 |
talloc_asprintf(tctx, "Bad response from %s for name register\n", |
| 141 |
address)); |
| 142 |
|
| 143 |
CHECK_STRING(tctx, io.out.wins_server, address); |
| 144 |
if (register_rcode != WINS_TEST_RCODE_WE_DONT_CARE) { |
| 145 |
CHECK_VALUE(tctx, io.out.rcode, 0); |
| 146 |
} |
| 147 |
|
| 148 |
torture_comment(tctx, "register the name correct address\n"); |
| 149 |
name_register.in.name = *name; |
| 150 |
name_register.in.dest_port = lpcfg_nbt_port(tctx->lp_ctx); |
| 151 |
name_register.in.dest_addr = address; |
| 152 |
name_register.in.address = myaddress; |
| 153 |
name_register.in.nb_flags = nb_flags; |
| 154 |
name_register.in.register_demand= false; |
| 155 |
name_register.in.broadcast = false; |
| 156 |
name_register.in.multi_homed = true; |
| 157 |
name_register.in.ttl = 300000; |
| 158 |
name_register.in.timeout = 3; |
| 159 |
name_register.in.retries = 2; |
| 160 |
|
| 161 |
/* |
| 162 |
* test if the server ignores resent requests |
| 163 |
*/ |
| 164 |
req = nbt_name_register_send(nbtsock, &name_register); |
| 165 |
while (true) { |
| 166 |
tevent_loop_once(nbtsock->event_ctx); |
| 167 |
if (req->state != NBT_REQUEST_WAIT) { |
| 168 |
break; |
| 169 |
} |
| 170 |
if (req->received_wack) { |
| 171 |
/* |
| 172 |
* if we received the wack response |
| 173 |
* we resend the request and the |
| 174 |
* server should ignore that |
| 175 |
* and not handle it as new request |
| 176 |
*/ |
| 177 |
req->state = NBT_REQUEST_SEND; |
| 178 |
DLIST_ADD_END(nbtsock->send_queue, req); |
| 179 |
TEVENT_FD_WRITEABLE(nbtsock->fde); |
| 180 |
break; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
status = nbt_name_register_recv(req, tctx, &name_register); |
| 185 |
if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) { |
| 186 |
torture_assert_ntstatus_ok(tctx, status, |
| 187 |
talloc_asprintf(tctx, "No response from %s for name register\n", |
| 188 |
address)); |
| 189 |
} |
| 190 |
torture_assert_ntstatus_ok(tctx, status, |
| 191 |
talloc_asprintf(tctx, "Bad response from %s for name register\n", |
| 192 |
address)); |
| 193 |
|
| 194 |
if (register_rcode != WINS_TEST_RCODE_WE_DONT_CARE) { |
| 195 |
CHECK_VALUE(tctx, name_register.out.rcode, 0); |
| 196 |
} |
| 197 |
CHECK_STRING(tctx, name_register.out.reply_addr, myaddress); |
| 198 |
} |
| 199 |
|
| 200 |
torture_comment(tctx, "register the name correct address\n"); |
| 201 |
io.in.name = *name; |
| 202 |
io.in.wins_port = lpcfg_nbt_port(tctx->lp_ctx); |
| 203 |
l = str_list_make_single(tctx, address); |
| 204 |
io.in.wins_servers = discard_const_p(const char *, l); |
| 205 |
l = str_list_make_single(tctx, myaddress); |
| 206 |
io.in.addresses = discard_const_p(const char *, l); |
| 207 |
io.in.nb_flags = nb_flags; |
| 208 |
io.in.ttl = 300000; |
| 209 |
|
| 210 |
status = nbt_name_register_wins(nbtsock, tctx, &io); |
| 211 |
torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name register", address)); |
| 212 |
|
| 213 |
CHECK_STRING(tctx, io.out.wins_server, address); |
| 214 |
if (register_rcode != WINS_TEST_RCODE_WE_DONT_CARE) { |
| 215 |
CHECK_VALUE(tctx, io.out.rcode, register_rcode); |
| 216 |
} |
| 217 |
|
| 218 |
if (register_rcode != NBT_RCODE_OK) { |
| 219 |
return true; |
| 220 |
} |
| 221 |
|
| 222 |
if (name->type != NBT_NAME_MASTER && |
| 223 |
name->type != NBT_NAME_LOGON && |
| 224 |
name->type != NBT_NAME_BROWSER && |
| 225 |
(nb_flags & NBT_NM_GROUP)) { |
| 226 |
torture_comment(tctx, "Try to register as non-group\n"); |
| 227 |
io.in.nb_flags &= ~NBT_NM_GROUP; |
| 228 |
status = nbt_name_register_wins(nbtsock, tctx, &io); |
| 229 |
torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name register\n", |
| 230 |
address)); |
| 231 |
CHECK_VALUE(tctx, io.out.rcode, NBT_RCODE_ACT); |
| 232 |
} |
| 233 |
|
| 234 |
torture_comment(tctx, "query the name to make sure its there\n"); |
| 235 |
query.in.name = *name; |
| 236 |
query.in.dest_addr = address; |
| 237 |
query.in.dest_port = lpcfg_nbt_port(tctx->lp_ctx); |
| 238 |
query.in.broadcast = false; |
| 239 |
query.in.wins_lookup = true; |
| 240 |
query.in.timeout = 3; |
| 241 |
query.in.retries = 0; |
| 242 |
|
| 243 |
status = nbt_name_query(nbtsock, tctx, &query); |
| 244 |
if (name->type == NBT_NAME_MASTER) { |
| 245 |
torture_assert_ntstatus_equal( |
| 246 |
tctx, status, NT_STATUS_OBJECT_NAME_NOT_FOUND, |
| 247 |
talloc_asprintf(tctx, "Bad response from %s for name query", address)); |
| 248 |
return true; |
| 249 |
} |
| 250 |
torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name query", address)); |
| 251 |
|
| 252 |
CHECK_NAME(tctx, query.out.name, *name); |
| 253 |
CHECK_VALUE(tctx, query.out.num_addrs, 1); |
| 254 |
if (name->type != NBT_NAME_LOGON && |
| 255 |
(nb_flags & NBT_NM_GROUP)) { |
| 256 |
CHECK_STRING(tctx, query.out.reply_addrs[0], "255.255.255.255"); |
| 257 |
} else { |
| 258 |
CHECK_STRING(tctx, query.out.reply_addrs[0], myaddress); |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
query.in.name.name = strupper_talloc(tctx, name->name); |
| 263 |
if (query.in.name.name && |
| 264 |
strcmp(query.in.name.name, name->name) != 0) { |
| 265 |
torture_comment(tctx, "check case sensitivity\n"); |
| 266 |
status = nbt_name_query(nbtsock, tctx, &query); |
| 267 |
torture_assert_ntstatus_equal(tctx, status, NT_STATUS_OBJECT_NAME_NOT_FOUND, talloc_asprintf(tctx, "Bad response from %s for name query", address)); |
| 268 |
} |
| 269 |
|
| 270 |
query.in.name = *name; |
| 271 |
if (name->scope) { |
| 272 |
query.in.name.scope = strupper_talloc(tctx, name->scope); |
| 273 |
} |
| 274 |
if (query.in.name.scope && |
| 275 |
strcmp(query.in.name.scope, name->scope) != 0) { |
| 276 |
torture_comment(tctx, "check case sensitivity on scope\n"); |
| 277 |
status = nbt_name_query(nbtsock, tctx, &query); |
| 278 |
torture_assert_ntstatus_equal(tctx, status, NT_STATUS_OBJECT_NAME_NOT_FOUND, talloc_asprintf(tctx, "Bad response from %s for name query", address)); |
| 279 |
} |
| 280 |
|
| 281 |
torture_comment(tctx, "refresh the name\n"); |
| 282 |
refresh.in.name = *name; |
| 283 |
refresh.in.wins_port = lpcfg_nbt_port(tctx->lp_ctx); |
| 284 |
l = str_list_make_single(tctx, address); |
| 285 |
refresh.in.wins_servers = discard_const_p(const char *, l); |
| 286 |
l = str_list_make_single(tctx, myaddress); |
| 287 |
refresh.in.addresses = discard_const_p(const char *, l); |
| 288 |
refresh.in.nb_flags = nb_flags; |
| 289 |
refresh.in.ttl = 12345; |
| 290 |
|
| 291 |
status = nbt_name_refresh_wins(nbtsock, tctx, &refresh); |
| 292 |
if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) { |
| 293 |
torture_assert_ntstatus_ok(tctx, status, |
| 294 |
talloc_asprintf(tctx, "No response from %s for name refresh", |
| 295 |
address)); |
| 296 |
} |
| 297 |
torture_assert_ntstatus_ok(tctx, status, |
| 298 |
talloc_asprintf(tctx, "Bad response from %s for name refresh", |
| 299 |
address)); |
| 300 |
|
| 301 |
CHECK_STRING(tctx, refresh.out.wins_server, address); |
| 302 |
CHECK_VALUE(tctx, refresh.out.rcode, 0); |
| 303 |
|
| 304 |
printf("release the name\n"); |
| 305 |
release.in.name = *name; |
| 306 |
release.in.dest_port = lpcfg_nbt_port(tctx->lp_ctx); |
| 307 |
release.in.dest_addr = address; |
| 308 |
release.in.address = myaddress; |
| 309 |
release.in.nb_flags = nb_flags; |
| 310 |
release.in.broadcast = false; |
| 311 |
release.in.timeout = 3; |
| 312 |
release.in.retries = 0; |
| 313 |
|
| 314 |
status = nbt_name_release(nbtsock, tctx, &release); |
| 315 |
if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) { |
| 316 |
torture_assert_ntstatus_ok(tctx, status, |
| 317 |
talloc_asprintf(tctx, "No response from %s for name release", |
| 318 |
address)); |
| 319 |
} |
| 320 |
torture_assert_ntstatus_ok(tctx, status, |
| 321 |
talloc_asprintf(tctx, "Bad response from %s for name release", |
| 322 |
address)); |
| 323 |
|
| 324 |
CHECK_NAME(tctx, release.out.name, *name); |
| 325 |
CHECK_VALUE(tctx, release.out.rcode, 0); |
| 326 |
|
| 327 |
if (nb_flags & NBT_NM_GROUP) { |
| 328 |
/* ignore this for group names */ |
| 329 |
} else if (!low_port) { |
| 330 |
torture_comment(tctx, "no low port - skip: register the name with a wrong address\n"); |
| 331 |
} else { |
| 332 |
torture_comment(tctx, "register the name with a wrong address (makes the next request slow!)\n"); |
| 333 |
io.in.name = *name; |
| 334 |
io.in.wins_port = lpcfg_nbt_port(tctx->lp_ctx); |
| 335 |
io.in.wins_servers = const_str_list( |
| 336 |
str_list_make_single(tctx, address)); |
| 337 |
io.in.addresses = const_str_list( |
| 338 |
str_list_make_single(tctx, "127.64.64.1")); |
| 339 |
io.in.nb_flags = nb_flags; |
| 340 |
io.in.ttl = 300000; |
| 341 |
|
| 342 |
status = nbt_name_register_wins(nbtsock, tctx, &io); |
| 343 |
if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) { |
| 344 |
torture_assert_ntstatus_ok(tctx, status, |
| 345 |
talloc_asprintf(tctx, "No response from %s for name register\n", |
| 346 |
address)); |
| 347 |
} |
| 348 |
torture_assert_ntstatus_ok(tctx, status, |
| 349 |
talloc_asprintf(tctx, "Bad response from %s for name register\n", |
| 350 |
address)); |
| 351 |
|
| 352 |
CHECK_STRING(tctx, io.out.wins_server, address); |
| 353 |
CHECK_VALUE(tctx, io.out.rcode, 0); |
| 354 |
} |
| 355 |
|
| 356 |
torture_comment(tctx, "refresh the name with the correct address\n"); |
| 357 |
refresh.in.name = *name; |
| 358 |
refresh.in.wins_port = lpcfg_nbt_port(tctx->lp_ctx); |
| 359 |
refresh.in.wins_servers = const_str_list( |
| 360 |
str_list_make_single(tctx, address)); |
| 361 |
refresh.in.addresses = const_str_list( |
| 362 |
str_list_make_single(tctx, myaddress)); |
| 363 |
refresh.in.nb_flags = nb_flags; |
| 364 |
refresh.in.ttl = 12345; |
| 365 |
|
| 366 |
status = nbt_name_refresh_wins(nbtsock, tctx, &refresh); |
| 367 |
if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) { |
| 368 |
torture_assert_ntstatus_ok(tctx, status, |
| 369 |
talloc_asprintf(tctx, "No response from %s for name refresh", |
| 370 |
address)); |
| 371 |
} |
| 372 |
torture_assert_ntstatus_ok(tctx, status, |
| 373 |
talloc_asprintf(tctx, "Bad response from %s for name refresh", |
| 374 |
address)); |
| 375 |
|
| 376 |
CHECK_STRING(tctx, refresh.out.wins_server, address); |
| 377 |
CHECK_VALUE(tctx, refresh.out.rcode, 0); |
| 378 |
|
| 379 |
torture_comment(tctx, "release the name\n"); |
| 380 |
release.in.name = *name; |
| 381 |
release.in.dest_port = lpcfg_nbt_port(tctx->lp_ctx); |
| 382 |
release.in.dest_addr = address; |
| 383 |
release.in.address = myaddress; |
| 384 |
release.in.nb_flags = nb_flags; |
| 385 |
release.in.broadcast = false; |
| 386 |
release.in.timeout = 3; |
| 387 |
release.in.retries = 0; |
| 388 |
|
| 389 |
status = nbt_name_release(nbtsock, tctx, &release); |
| 390 |
torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name query", address)); |
| 391 |
|
| 392 |
CHECK_NAME(tctx, release.out.name, *name); |
| 393 |
CHECK_VALUE(tctx, release.out.rcode, 0); |
| 394 |
|
| 395 |
torture_comment(tctx, "release again\n"); |
| 396 |
status = nbt_name_release(nbtsock, tctx, &release); |
| 397 |
torture_assert_ntstatus_ok(tctx, status, |
| 398 |
talloc_asprintf(tctx, "Bad response from %s for name query", |
| 399 |
address)); |
| 400 |
|
| 401 |
CHECK_NAME(tctx, release.out.name, *name); |
| 402 |
CHECK_VALUE(tctx, release.out.rcode, 0); |
| 403 |
|
| 404 |
|
| 405 |
torture_comment(tctx, "query the name to make sure its gone\n"); |
| 406 |
query.in.name = *name; |
| 407 |
status = nbt_name_query(nbtsock, tctx, &query); |
| 408 |
if (name->type != NBT_NAME_LOGON && |
| 409 |
(nb_flags & NBT_NM_GROUP)) { |
| 410 |
torture_assert_ntstatus_ok(tctx, status, |
| 411 |
"ERROR: Name query failed after group release"); |
| 412 |
} else { |
| 413 |
torture_assert_ntstatus_equal(tctx, status, |
| 414 |
NT_STATUS_OBJECT_NAME_NOT_FOUND, |
| 415 |
"Incorrect response to name query"); |
| 416 |
} |
| 417 |
|
| 418 |
return true; |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
static char *test_nbt_wins_scope_string(TALLOC_CTX *mem_ctx, uint8_t count) |
| 423 |
{ |
| 424 |
char *res; |
| 425 |
uint8_t i; |
| 426 |
|
| 427 |
res = talloc_array(mem_ctx, char, count+1); |
| 428 |
if (res == NULL) { |
| 429 |
return NULL; |
| 430 |
} |
| 431 |
|
| 432 |
for (i=0; i < count; i++) { |
| 433 |
switch (i) { |
| 434 |
case 63: |
| 435 |
case 63 + 1 + 63: |
| 436 |
case 63 + 1 + 63 + 1 + 63: |
| 437 |
res[i] = '.'; |
| 438 |
break; |
| 439 |
default: |
| 440 |
res[i] = '0' + (i%10); |
| 441 |
break; |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
res[count] = '\0'; |
| 446 |
|
| 447 |
talloc_set_name_const(res, res); |
| 448 |
|
| 449 |
return res; |
| 450 |
} |
| 451 |
|
| 452 |
/* |
| 453 |
test operations against a WINS server |
| 454 |
*/ |
| 455 |
static bool nbt_test_wins(struct torture_context *tctx) |
| 456 |
{ |
| 457 |
struct nbt_name name; |
| 458 |
uint32_t r = (uint32_t)(random() % (100000)); |
| 459 |
const char *address; |
| 460 |
bool ret = true; |
| 461 |
|
| 462 |
if (!torture_nbt_get_name(tctx, &name, &address)) |
| 463 |
return false; |
| 464 |
|
| 465 |
name.name = talloc_asprintf(tctx, "_TORTURE-%5u", r); |
| 466 |
|
| 467 |
name.type = NBT_NAME_CLIENT; |
| 468 |
name.scope = NULL; |
| 469 |
ret &= nbt_test_wins_name(tctx, address, &name, |
| 470 |
NBT_NODE_H, true, NBT_RCODE_OK); |
| 471 |
|
| 472 |
name.type = NBT_NAME_MASTER; |
| 473 |
ret &= nbt_test_wins_name(tctx, address, &name, |
| 474 |
NBT_NODE_H, false, NBT_RCODE_OK); |
| 475 |
|
| 476 |
ret &= nbt_test_wins_name(tctx, address, &name, |
| 477 |
NBT_NODE_H | NBT_NM_GROUP, false, NBT_RCODE_OK); |
| 478 |
|
| 479 |
name.type = NBT_NAME_SERVER; |
| 480 |
ret &= nbt_test_wins_name(tctx, address, &name, |
| 481 |
NBT_NODE_H, true, NBT_RCODE_OK); |
| 482 |
|
| 483 |
name.type = NBT_NAME_LOGON; |
| 484 |
ret &= nbt_test_wins_name(tctx, address, &name, |
| 485 |
NBT_NODE_H | NBT_NM_GROUP, false, NBT_RCODE_OK); |
| 486 |
|
| 487 |
name.type = NBT_NAME_BROWSER; |
| 488 |
ret &= nbt_test_wins_name(tctx, address, &name, |
| 489 |
NBT_NODE_H | NBT_NM_GROUP, false, NBT_RCODE_OK); |
| 490 |
|
| 491 |
name.type = NBT_NAME_PDC; |
| 492 |
ret &= nbt_test_wins_name(tctx, address, &name, |
| 493 |
NBT_NODE_H, true, NBT_RCODE_OK); |
| 494 |
|
| 495 |
name.type = 0xBF; |
| 496 |
ret &= nbt_test_wins_name(tctx, address, &name, |
| 497 |
NBT_NODE_H, true, NBT_RCODE_OK); |
| 498 |
|
| 499 |
name.type = 0xBE; |
| 500 |
ret &= nbt_test_wins_name(tctx, address, &name, |
| 501 |
NBT_NODE_H, false, NBT_RCODE_OK); |
| 502 |
|
| 503 |
name.scope = "example"; |
| 504 |
name.type = 0x72; |
| 505 |
ret &= nbt_test_wins_name(tctx, address, &name, |
| 506 |
NBT_NODE_H, true, NBT_RCODE_OK); |
| 507 |
|
| 508 |
name.scope = "example"; |
| 509 |
name.type = 0x71; |
| 510 |
ret &= nbt_test_wins_name(tctx, address, &name, |
| 511 |
NBT_NODE_H | NBT_NM_GROUP, false, NBT_RCODE_OK); |
| 512 |
|
| 513 |
name.scope = "foo.example.com"; |
| 514 |
name.type = 0x72; |
| 515 |
ret &= nbt_test_wins_name(tctx, address, &name, |
| 516 |
NBT_NODE_H, false, NBT_RCODE_OK); |
| 517 |
|
| 518 |
name.name = talloc_asprintf(tctx, "_T\01-%5u.foo", r); |
| 519 |
ret &= nbt_test_wins_name(tctx, address, &name, |
| 520 |
NBT_NODE_H, false, NBT_RCODE_OK); |
| 521 |
|
| 522 |
name.name = ""; |
| 523 |
ret &= nbt_test_wins_name(tctx, address, &name, |
| 524 |
NBT_NODE_H, false, NBT_RCODE_OK); |
| 525 |
|
| 526 |
name.name = talloc_asprintf(tctx, "."); |
| 527 |
ret &= nbt_test_wins_name(tctx, address, &name, |
| 528 |
NBT_NODE_H, false, NBT_RCODE_OK); |
| 529 |
|
| 530 |
name.name = talloc_asprintf(tctx, "%5u-\377\200\300FOO", r); |
| 531 |
ret &= nbt_test_wins_name(tctx, address, &name, |
| 532 |
NBT_NODE_H, false, NBT_RCODE_OK); |
| 533 |
|
| 534 |
name.scope = test_nbt_wins_scope_string(tctx, 237); |
| 535 |
ret &= nbt_test_wins_name(tctx, address, &name, |
| 536 |
NBT_NODE_H, false, NBT_RCODE_OK); |
| 537 |
|
| 538 |
name.scope = test_nbt_wins_scope_string(tctx, 238); |
| 539 |
ret &= nbt_test_wins_name(tctx, address, &name, |
| 540 |
NBT_NODE_H, false, NBT_RCODE_SVR); |
| 541 |
|
| 542 |
return ret; |
| 543 |
} |
| 544 |
|
| 545 |
/* |
| 546 |
* Test that the WINS server does not call 'wins hook' when the name |
| 547 |
* contains dodgy characters. |
| 548 |
*/ |
| 549 |
static bool nbt_test_wins_bad_names(struct torture_context *tctx) |
| 550 |
{ |
| 551 |
const char *address = NULL; |
| 552 |
const char *wins_hook_file = NULL; |
| 553 |
bool ret = true; |
| 554 |
int err; |
| 555 |
bool ok; |
| 556 |
struct nbt_name name = {}; |
| 557 |
size_t i, j; |
| 558 |
FILE *fh = NULL; |
| 559 |
|
| 560 |
struct { |
| 561 |
const char *name; |
| 562 |
bool should_succeed; |
| 563 |
} test_cases[] = { |
| 564 |
{"NORMAL", true}, |
| 565 |
{"|look|", false}, |
| 566 |
{"look&true", false}, |
| 567 |
{"look\\;false", false}, |
| 568 |
{"&ls>foo", false}, /* already fails due to DN syntax */ |
| 569 |
{"has spaces", false}, |
| 570 |
{"hyphen-dot.0", true}, |
| 571 |
}; |
| 572 |
|
| 573 |
wins_hook_file = talloc_asprintf(tctx, "%s/wins_hook_writes_here", |
| 574 |
getenv("SELFTEST_TMPDIR")); |
| 575 |
|
| 576 |
if (!torture_nbt_get_name(tctx, &name, &address)) { |
| 577 |
return false; |
| 578 |
} |
| 579 |
|
| 580 |
for (i = 0; i < ARRAY_SIZE(test_cases); i++) { |
| 581 |
err = unlink(wins_hook_file); |
| 582 |
if (err != 0 && errno != ENOENT) { |
| 583 |
/* we expect ENOENT, but nothing else */ |
| 584 |
torture_comment(tctx, |
| 585 |
"unlink %zu of '%s' failed\n", |
| 586 |
i, wins_hook_file); |
| 587 |
} |
| 588 |
|
| 589 |
name.name = test_cases[i].name; |
| 590 |
name.type = NBT_NAME_CLIENT; |
| 591 |
ok = nbt_test_wins_name(tctx, address, |
| 592 |
&name, |
| 593 |
NBT_NODE_H, |
| 594 |
true, |
| 595 |
WINS_TEST_RCODE_WE_DONT_CARE |
| 596 |
); |
| 597 |
if (ok == false) { |
| 598 |
/* |
| 599 |
* This happens when the name interferes with |
| 600 |
* the DN syntax when it is put in winsdb. |
| 601 |
* |
| 602 |
* The wins hook will not be reached. |
| 603 |
*/ |
| 604 |
torture_comment(tctx, "tests for '%s' failed\n", |
| 605 |
name.name); |
| 606 |
} |
| 607 |
|
| 608 |
/* |
| 609 |
* poll for the file being created by the wins hook. |
| 610 |
*/ |
| 611 |
for (j = 0; j < 10; j++) { |
| 612 |
usleep(200000); |
| 613 |
fh = fopen(wins_hook_file, "r"); |
| 614 |
if (fh != NULL) { |
| 615 |
break; |
| 616 |
} |
| 617 |
} |
| 618 |
|
| 619 |
if (fh == NULL) { |
| 620 |
if (errno == ENOENT) { |
| 621 |
if (test_cases[i].should_succeed) { |
| 622 |
torture_comment( |
| 623 |
tctx, |
| 624 |
"wins hook for '%s' failed\n", |
| 625 |
test_cases[i].name); |
| 626 |
ret = false; |
| 627 |
} |
| 628 |
} else { |
| 629 |
torture_comment( |
| 630 |
tctx, |
| 631 |
"wins hook for '%s' unexpectedly failed with %d\n", |
| 632 |
test_cases[i].name, |
| 633 |
errno); |
| 634 |
ret = false; |
| 635 |
} |
| 636 |
} else { |
| 637 |
char readback[17] = {0}; |
| 638 |
size_t n = fread(readback, 1, 16, fh); |
| 639 |
torture_comment(tctx, |
| 640 |
"wins hook wrote '%s' read '%.*s'\n", |
| 641 |
test_cases[i].name, |
| 642 |
(int)n, readback); |
| 643 |
|
| 644 |
if (! test_cases[i].should_succeed) { |
| 645 |
torture_comment(tctx, |
| 646 |
"wins hook for '%s' should fail\n", |
| 647 |
test_cases[i].name); |
| 648 |
ret = false; |
| 649 |
} |
| 650 |
fclose(fh); |
| 651 |
} |
| 652 |
} |
| 653 |
err = unlink(wins_hook_file); |
| 654 |
if (err != 0 && errno != ENOENT) { |
| 655 |
torture_comment(tctx, "final unlink of '%s' failed\n", |
| 656 |
wins_hook_file); |
| 657 |
} |
| 658 |
torture_assert(tctx, ret, "wins hook failure\n"); |
| 659 |
return ret; |
| 660 |
} |
| 661 |
|
| 662 |
|
| 663 |
/* |
| 664 |
test WINS operations |
| 665 |
*/ |
| 666 |
struct torture_suite *torture_nbt_wins(TALLOC_CTX *mem_ctx) |
| 667 |
{ |
| 668 |
struct torture_suite *suite = torture_suite_create(mem_ctx, "wins"); |
| 669 |
|
| 670 |
torture_suite_add_simple_test(suite, "wins", nbt_test_wins); |
| 671 |
torture_suite_add_simple_test(suite, "wins_bad_names", |
| 672 |
nbt_test_wins_bad_names); |
| 673 |
|
| 674 |
return suite; |
| 675 |
} |