root/source4/torture/nbt/wins.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. nbt_test_wins_name
  2. nbt_test_wins
  3. torture_nbt_wins

   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 "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 #define CHECK_VALUE(tctx, v, correct) \
  35         torture_assert_int_equal(tctx, v, correct, "Incorrect value")
  36 
  37 #define CHECK_STRING(tctx, v, correct) \
  38         torture_assert_casestr_equal(tctx, v, correct, "Incorrect value")
  39 
  40 #define CHECK_NAME(tctx, _name, correct) do { \
  41         CHECK_STRING(tctx, (_name).name, (correct).name); \
  42         CHECK_VALUE(tctx, (uint8_t)(_name).type, (uint8_t)(correct).type); \
  43         CHECK_STRING(tctx, (_name).scope, (correct).scope); \
  44 } while (0)
  45 
  46 
  47 /*
  48   test operations against a WINS server
  49 */
  50 static bool nbt_test_wins_name(struct torture_context *tctx, const char *address,
     /* [<][>][^][v][top][bottom][index][help] */
  51                                struct nbt_name *name, uint16_t nb_flags,
  52                                bool try_low_port)
  53 {
  54         struct nbt_name_register_wins io;
  55         struct nbt_name_register name_register;
  56         struct nbt_name_query query;
  57         struct nbt_name_refresh_wins refresh;
  58         struct nbt_name_release release;
  59         struct nbt_name_request *req;
  60         NTSTATUS status;
  61         struct nbt_name_socket *nbtsock = torture_init_nbt_socket(tctx);
  62         const char *myaddress;
  63         struct socket_address *socket_address;
  64         struct interface *ifaces;
  65         bool low_port = try_low_port;
  66 
  67         load_interfaces(tctx, lp_interfaces(tctx->lp_ctx), &ifaces);
  68 
  69         myaddress = talloc_strdup(tctx, iface_best_ip(ifaces, address));
  70 
  71         socket_address = socket_address_from_strings(tctx, 
  72                                                      nbtsock->sock->backend_name,
  73                                                      myaddress, lp_nbt_port(tctx->lp_ctx));
  74         torture_assert(tctx, socket_address != NULL, 
  75                                    "Error getting address");
  76 
  77         /* we do the listen here to ensure the WINS server receives the packets from
  78            the right IP */
  79         status = socket_listen(nbtsock->sock, socket_address, 0, 0);
  80         talloc_free(socket_address);
  81         if (!NT_STATUS_IS_OK(status)) {
  82                 low_port = false;
  83                 socket_address = socket_address_from_strings(tctx,
  84                                                              nbtsock->sock->backend_name,
  85                                                              myaddress, 0);
  86                 torture_assert(tctx, socket_address != NULL,
  87                                "Error getting address");
  88 
  89                 status = socket_listen(nbtsock->sock, socket_address, 0, 0);
  90                 talloc_free(socket_address);
  91                 torture_assert_ntstatus_ok(tctx, status,
  92                                            "socket_listen for WINS failed");
  93         }
  94 
  95         torture_comment(tctx, "Testing name registration to WINS with name %s at %s nb_flags=0x%x\n", 
  96                nbt_name_string(tctx, name), myaddress, nb_flags);
  97 
  98         torture_comment(tctx, "release the name\n");
  99         release.in.name = *name;
 100         release.in.dest_port = lp_nbt_port(tctx->lp_ctx);
 101         release.in.dest_addr = address;
 102         release.in.address = myaddress;
 103         release.in.nb_flags = nb_flags;
 104         release.in.broadcast = false;
 105         release.in.timeout = 3;
 106         release.in.retries = 0;
 107 
 108         status = nbt_name_release(nbtsock, tctx, &release);
 109         torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name query", address));
 110         CHECK_VALUE(tctx, release.out.rcode, 0);
 111 
 112         if (nb_flags & NBT_NM_GROUP) {
 113                 /* ignore this for group names */
 114         } else if (!low_port) {
 115                 torture_comment(tctx, "no low port - skip: register the name with a wrong address\n");
 116         } else {
 117                 torture_comment(tctx, "register the name with a wrong address (makes the next request slow!)\n");
 118                 io.in.name = *name;
 119                 io.in.wins_port = lp_nbt_port(tctx->lp_ctx);
 120                 io.in.wins_servers = str_list_make(tctx, address, NULL);
 121                 io.in.addresses = str_list_make(tctx, "127.64.64.1", NULL);
 122                 io.in.nb_flags = nb_flags;
 123                 io.in.ttl = 300000;
 124 
 125                 status = nbt_name_register_wins(nbtsock, tctx, &io);
 126                 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
 127                         torture_assert_ntstatus_ok(tctx, status,
 128                                 talloc_asprintf(tctx, "No response from %s for name register\n",
 129                                                 address));
 130                 }
 131                 torture_assert_ntstatus_ok(tctx, status,
 132                         talloc_asprintf(tctx, "Bad response from %s for name register\n",
 133                                         address));
 134 
 135                 CHECK_STRING(tctx, io.out.wins_server, address);
 136                 CHECK_VALUE(tctx, io.out.rcode, 0);
 137 
 138                 torture_comment(tctx, "register the name correct address\n");
 139                 name_register.in.name           = *name;
 140                 name_register.in.dest_port      = lp_nbt_port(tctx->lp_ctx);
 141                 name_register.in.dest_addr      = address;
 142                 name_register.in.address        = myaddress;
 143                 name_register.in.nb_flags       = nb_flags;
 144                 name_register.in.register_demand= false;
 145                 name_register.in.broadcast      = false;
 146                 name_register.in.multi_homed    = true;
 147                 name_register.in.ttl            = 300000;
 148                 name_register.in.timeout        = 3;
 149                 name_register.in.retries        = 2;
 150 
 151                 /*
 152                  * test if the server ignores resent requests
 153                  */
 154                 req = nbt_name_register_send(nbtsock, &name_register);
 155                 while (true) {
 156                         event_loop_once(nbtsock->event_ctx);
 157                         if (req->state != NBT_REQUEST_WAIT) {
 158                                 break;
 159                         }
 160                         if (req->received_wack) {
 161                                 /*
 162                                  * if we received the wack response
 163                                  * we resend the request and the
 164                                  * server should ignore that
 165                                  * and not handle it as new request
 166                                  */
 167                                 req->state = NBT_REQUEST_SEND;
 168                                 DLIST_ADD_END(nbtsock->send_queue, req,
 169                                               struct nbt_name_request *);
 170                                 EVENT_FD_WRITEABLE(nbtsock->fde);
 171                                 break;
 172                         }
 173                 }
 174 
 175                 status = nbt_name_register_recv(req, tctx, &name_register);
 176                 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
 177                         torture_assert_ntstatus_ok(tctx, status,
 178                                 talloc_asprintf(tctx, "No response from %s for name register\n",
 179                                                 address));
 180                 }
 181                 torture_assert_ntstatus_ok(tctx, status,
 182                         talloc_asprintf(tctx, "Bad response from %s for name register\n",
 183                                         address));
 184 
 185                 CHECK_VALUE(tctx, name_register.out.rcode, 0);
 186                 CHECK_STRING(tctx, name_register.out.reply_addr, myaddress);
 187         }
 188 
 189         torture_comment(tctx, "register the name correct address\n");
 190         io.in.name = *name;
 191         io.in.wins_port = lp_nbt_port(tctx->lp_ctx);
 192         io.in.wins_servers = (const char **)str_list_make(tctx, address, NULL);
 193         io.in.addresses = (const char **)str_list_make(tctx, myaddress, NULL);
 194         io.in.nb_flags = nb_flags;
 195         io.in.ttl = 300000;
 196         
 197         status = nbt_name_register_wins(nbtsock, tctx, &io);
 198         torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name register", address));
 199         
 200         CHECK_STRING(tctx, io.out.wins_server, address);
 201         CHECK_VALUE(tctx, io.out.rcode, 0);
 202 
 203         if (name->type != NBT_NAME_MASTER &&
 204             name->type != NBT_NAME_LOGON && 
 205             name->type != NBT_NAME_BROWSER && 
 206             (nb_flags & NBT_NM_GROUP)) {
 207                 torture_comment(tctx, "Try to register as non-group\n");
 208                 io.in.nb_flags &= ~NBT_NM_GROUP;
 209                 status = nbt_name_register_wins(nbtsock, tctx, &io);
 210                 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name register\n",
 211                         address));
 212                 CHECK_VALUE(tctx, io.out.rcode, NBT_RCODE_ACT);
 213         }
 214 
 215         torture_comment(tctx, "query the name to make sure its there\n");
 216         query.in.name = *name;
 217         query.in.dest_addr = address;
 218         query.in.dest_port = lp_nbt_port(tctx->lp_ctx);
 219         query.in.broadcast = false;
 220         query.in.wins_lookup = true;
 221         query.in.timeout = 3;
 222         query.in.retries = 0;
 223 
 224         status = nbt_name_query(nbtsock, tctx, &query);
 225         if (name->type == NBT_NAME_MASTER) {
 226                 torture_assert_ntstatus_equal(
 227                           tctx, status, NT_STATUS_OBJECT_NAME_NOT_FOUND, 
 228                           talloc_asprintf(tctx, "Bad response from %s for name query", address));
 229                 return true;
 230         }
 231         torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name query", address));
 232         
 233         CHECK_NAME(tctx, query.out.name, *name);
 234         CHECK_VALUE(tctx, query.out.num_addrs, 1);
 235         if (name->type != NBT_NAME_LOGON &&
 236             (nb_flags & NBT_NM_GROUP)) {
 237                 CHECK_STRING(tctx, query.out.reply_addrs[0], "255.255.255.255");
 238         } else {
 239                 CHECK_STRING(tctx, query.out.reply_addrs[0], myaddress);
 240         }
 241 
 242 
 243         query.in.name.name = strupper_talloc(tctx, name->name);
 244         if (query.in.name.name &&
 245             strcmp(query.in.name.name, name->name) != 0) {
 246                 torture_comment(tctx, "check case sensitivity\n");
 247                 status = nbt_name_query(nbtsock, tctx, &query);
 248                 torture_assert_ntstatus_equal(tctx, status, NT_STATUS_OBJECT_NAME_NOT_FOUND, talloc_asprintf(tctx, "Bad response from %s for name query", address));
 249         }
 250 
 251         query.in.name = *name;
 252         if (name->scope) {
 253                 query.in.name.scope = strupper_talloc(tctx, name->scope);
 254         }
 255         if (query.in.name.scope &&
 256             strcmp(query.in.name.scope, name->scope) != 0) {
 257                 torture_comment(tctx, "check case sensitivity on scope\n");
 258                 status = nbt_name_query(nbtsock, tctx, &query);
 259                 torture_assert_ntstatus_equal(tctx, status, NT_STATUS_OBJECT_NAME_NOT_FOUND, talloc_asprintf(tctx, "Bad response from %s for name query", address));
 260         }
 261 
 262         torture_comment(tctx, "refresh the name\n");
 263         refresh.in.name = *name;
 264         refresh.in.wins_port = lp_nbt_port(tctx->lp_ctx);
 265         refresh.in.wins_servers = (const char **)str_list_make(tctx, address, NULL);
 266         refresh.in.addresses = (const char **)str_list_make(tctx, myaddress, NULL);
 267         refresh.in.nb_flags = nb_flags;
 268         refresh.in.ttl = 12345;
 269         
 270         status = nbt_name_refresh_wins(nbtsock, tctx, &refresh);
 271         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
 272                 torture_assert_ntstatus_ok(tctx, status,
 273                         talloc_asprintf(tctx, "No response from %s for name refresh",
 274                                         address));
 275         }
 276         torture_assert_ntstatus_ok(tctx, status,
 277                 talloc_asprintf(tctx, "Bad response from %s for name refresh",
 278                                 address));
 279 
 280         CHECK_STRING(tctx, refresh.out.wins_server, address);
 281         CHECK_VALUE(tctx, refresh.out.rcode, 0);
 282 
 283         printf("release the name\n");
 284         release.in.name = *name;
 285         release.in.dest_port = lp_nbt_port(tctx->lp_ctx);
 286         release.in.dest_addr = address;
 287         release.in.address = myaddress;
 288         release.in.nb_flags = nb_flags;
 289         release.in.broadcast = false;
 290         release.in.timeout = 3;
 291         release.in.retries = 0;
 292 
 293         status = nbt_name_release(nbtsock, tctx, &release);
 294         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
 295                 torture_assert_ntstatus_ok(tctx, status,
 296                         talloc_asprintf(tctx, "No response from %s for name release",
 297                                         address));
 298         }
 299         torture_assert_ntstatus_ok(tctx, status,
 300                 talloc_asprintf(tctx, "Bad response from %s for name release",
 301                                 address));
 302 
 303         CHECK_NAME(tctx, release.out.name, *name);
 304         CHECK_VALUE(tctx, release.out.rcode, 0);
 305 
 306         if (nb_flags & NBT_NM_GROUP) {
 307                 /* ignore this for group names */
 308         } else if (!low_port) {
 309                 torture_comment(tctx, "no low port - skip: register the name with a wrong address\n");
 310         } else {
 311                 torture_comment(tctx, "register the name with a wrong address (makes the next request slow!)\n");
 312                 io.in.name = *name;
 313                 io.in.wins_port = lp_nbt_port(tctx->lp_ctx);
 314                 io.in.wins_servers = str_list_make(tctx, address, NULL);
 315                 io.in.addresses = str_list_make(tctx, "127.64.64.1", NULL);
 316                 io.in.nb_flags = nb_flags;
 317                 io.in.ttl = 300000;
 318         
 319                 status = nbt_name_register_wins(nbtsock, tctx, &io);
 320                 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
 321                         torture_assert_ntstatus_ok(tctx, status,
 322                                 talloc_asprintf(tctx, "No response from %s for name register\n",
 323                                                 address));
 324                 }
 325                 torture_assert_ntstatus_ok(tctx, status,
 326                         talloc_asprintf(tctx, "Bad response from %s for name register\n",
 327                                         address));
 328 
 329                 CHECK_STRING(tctx, io.out.wins_server, address);
 330                 CHECK_VALUE(tctx, io.out.rcode, 0);
 331         }
 332 
 333         torture_comment(tctx, "refresh the name with the correct address\n");
 334         refresh.in.name = *name;
 335         refresh.in.wins_port = lp_nbt_port(tctx->lp_ctx);
 336         refresh.in.wins_servers = str_list_make(tctx, address, NULL);
 337         refresh.in.addresses = str_list_make(tctx, myaddress, NULL);
 338         refresh.in.nb_flags = nb_flags;
 339         refresh.in.ttl = 12345;
 340 
 341         status = nbt_name_refresh_wins(nbtsock, tctx, &refresh);
 342         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
 343                 torture_assert_ntstatus_ok(tctx, status,
 344                         talloc_asprintf(tctx, "No response from %s for name refresh",
 345                                         address));
 346         }
 347         torture_assert_ntstatus_ok(tctx, status,
 348                 talloc_asprintf(tctx, "Bad response from %s for name refresh",
 349                                 address));
 350 
 351         CHECK_STRING(tctx, refresh.out.wins_server, address);
 352         CHECK_VALUE(tctx, refresh.out.rcode, 0);
 353 
 354         torture_comment(tctx, "release the name\n");
 355         release.in.name = *name;
 356         release.in.dest_port = lp_nbt_port(tctx->lp_ctx);
 357         release.in.dest_addr = address;
 358         release.in.address = myaddress;
 359         release.in.nb_flags = nb_flags;
 360         release.in.broadcast = false;
 361         release.in.timeout = 3;
 362         release.in.retries = 0;
 363 
 364         status = nbt_name_release(nbtsock, tctx, &release);
 365         torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name query", address));
 366         
 367         CHECK_NAME(tctx, release.out.name, *name);
 368         CHECK_VALUE(tctx, release.out.rcode, 0);
 369 
 370         torture_comment(tctx, "release again\n");
 371         status = nbt_name_release(nbtsock, tctx, &release);
 372         torture_assert_ntstatus_ok(tctx, status, 
 373                                 talloc_asprintf(tctx, "Bad response from %s for name query",
 374                        address));
 375         
 376         CHECK_NAME(tctx, release.out.name, *name);
 377         CHECK_VALUE(tctx, release.out.rcode, 0);
 378 
 379 
 380         torture_comment(tctx, "query the name to make sure its gone\n");
 381         query.in.name = *name;
 382         status = nbt_name_query(nbtsock, tctx, &query);
 383         if (name->type != NBT_NAME_LOGON &&
 384             (nb_flags & NBT_NM_GROUP)) {
 385                 torture_assert_ntstatus_ok(tctx, status, 
 386                                 "ERROR: Name query failed after group release");
 387         } else {
 388                 torture_assert_ntstatus_equal(tctx, status, 
 389                                                                           NT_STATUS_OBJECT_NAME_NOT_FOUND,
 390                                 "Incorrect response to name query");
 391         }
 392         
 393         return true;
 394 }
 395 
 396 
 397 
 398 /*
 399   test operations against a WINS server
 400 */
 401 static bool nbt_test_wins(struct torture_context *tctx)
     /* [<][>][^][v][top][bottom][index][help] */
 402 {
 403         struct nbt_name name;
 404         uint32_t r = (uint32_t)(random() % (100000));
 405         const char *address;
 406         bool ret = true;
 407 
 408         if (!torture_nbt_get_name(tctx, &name, &address))
 409                 return false;
 410 
 411         name.name = talloc_asprintf(tctx, "_TORTURE-%5u", r);
 412 
 413         name.type = NBT_NAME_CLIENT;
 414         name.scope = NULL;
 415         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H, true);
 416 
 417         name.type = NBT_NAME_MASTER;
 418         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H, false);
 419 
 420         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H | NBT_NM_GROUP, false);
 421 
 422         name.type = NBT_NAME_SERVER;
 423         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H, true);
 424 
 425         name.type = NBT_NAME_LOGON;
 426         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H | NBT_NM_GROUP, false);
 427 
 428         name.type = NBT_NAME_BROWSER;
 429         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H | NBT_NM_GROUP, false);
 430 
 431         name.type = NBT_NAME_PDC;
 432         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H, true);
 433 
 434         name.type = 0xBF;
 435         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H, true);
 436 
 437         name.type = 0xBE;
 438         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H, false);
 439 
 440         name.scope = "example";
 441         name.type = 0x72;
 442         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H, true);
 443 
 444         name.scope = "example";
 445         name.type = 0x71;
 446         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H | NBT_NM_GROUP, false);
 447 
 448         name.scope = "foo.example.com";
 449         name.type = 0x72;
 450         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H, false);
 451 
 452         name.name = talloc_asprintf(tctx, "_T\01-%5u.foo", r);
 453         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H, false);
 454 
 455         name.name = "";
 456         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H, false);
 457 
 458         name.name = talloc_asprintf(tctx, ".");
 459         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H, false);
 460 
 461         name.name = talloc_asprintf(tctx, "%5u-\377\200\300FOO", r);
 462         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H, false);
 463 
 464         return ret;
 465 }
 466 
 467 /*
 468   test WINS operations
 469 */
 470 struct torture_suite *torture_nbt_wins(TALLOC_CTX *mem_ctx)
     /* [<][>][^][v][top][bottom][index][help] */
 471 {
 472         struct torture_suite *suite = torture_suite_create(mem_ctx, "WINS");
 473 
 474         torture_suite_add_simple_test(suite, "wins", nbt_test_wins);
 475 
 476         return suite;
 477 }

/* [<][>][^][v][top][bottom][index][help] */