r5352: added a function nbt_name_string() that formats a nbt_name structure
authorAndrew Tridgell <tridge@samba.org>
Sat, 12 Feb 2005 01:00:15 +0000 (01:00 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 18:09:45 +0000 (13:09 -0500)
as a human readable string. The format is designed to be able to be
used as the DN for the WINS database as well, while coping with
arbitrary bytes in the name (except nul bytes)
(This used to be commit aac3090e3504ba07124a9d480322a98efb97175e)

source4/libcli/nbt/nameregister.c
source4/libcli/nbt/nbtname.c
source4/nbt_server/defense.c
source4/nbt_server/nodestatus.c
source4/nbt_server/packet.c
source4/nbt_server/query.c
source4/nbt_server/register.c
source4/nbt_server/winsclient.c
source4/nbt_server/winsserver.c
source4/torture/nbt/wins.c

index 9c1db385298614121a3da839d9d9deed27729bb0..276b8e4ac3c22c8f474a14821d1495e1f677319a 100644 (file)
@@ -185,10 +185,9 @@ static void name_register_bcast_handler(struct nbt_name_request *req)
        } else {
                c->state = SMBCLI_REQUEST_ERROR;
                c->status = NT_STATUS_CONFLICTING_ADDRESSES;
-               DEBUG(3,("Name registration conflict from %s for %s<%02x> with ip %s - rcode %d\n",
+               DEBUG(3,("Name registration conflict from %s for %s with ip %s - rcode %d\n",
                         state->io->out.reply_from, 
-                        state->io->out.name.name,
-                        state->io->out.name.type,
+                        nbt_name_string(state, &state->io->out.name),
                         state->io->out.reply_addr,
                         state->io->out.rcode));
        }
index 0d2840e0b5b36d802ae66cabfc04624fe1db13c5..da5205d818cb67d6e71dbee11a4d986dd7e3f046 100644 (file)
@@ -25,6 +25,7 @@
 */
 
 #include "includes.h"
+#include "system/iconv.h"
 #include "librpc/gen_ndr/ndr_nbt.h"
 
 /* don't allow an unlimited number of name components */
@@ -320,3 +321,59 @@ void nbt_choose_called_name(TALLOC_CTX *mem_ctx,
 
        n->name = talloc_strdup(mem_ctx, name);
 }
+
+
+/*
+  escape a string into a form containing only a small set of characters,
+  the rest is hex encoded. This is similar to URL encoding
+*/
+static const char *nbt_hex_encode(TALLOC_CTX *mem_ctx, const char *s)
+{
+       int i, len;
+       char *ret;
+       const char *valid_chars = "_-.$@";
+
+       for (len=i=0;s[i];i++,len++) {
+               if (!isalnum(s[i]) && !strchr(valid_chars, s[i])) {
+                       len += 2;
+               }
+       }
+
+       ret = talloc_array(mem_ctx, char, len+1);
+       if (ret == NULL) return NULL;
+
+       for (len=i=0;s[i];i++) {
+               if (isalnum(s[i]) || strchr(valid_chars, s[i])) {
+                       ret[len++] = s[i];
+               } else {
+                       snprintf(&ret[len], 3, "%02x", s[i]);
+                       len += 3;
+               }
+       }
+       ret[len] = 0;
+
+       return ret;
+}
+
+
+/*
+  form a string for a NBT name
+*/
+const char *nbt_name_string(TALLOC_CTX *mem_ctx, const struct nbt_name *name)
+{
+       TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
+       const char *ret;
+       if (name->scope) {              
+               ret = talloc_asprintf(mem_ctx, "%s<%02x>-%s",
+                                     nbt_hex_encode(tmp_ctx, name->name),
+                                     name->type, 
+                                     nbt_hex_encode(tmp_ctx, name->scope));
+       } else {
+               ret = talloc_asprintf(mem_ctx, "%s<%02x>", 
+                                     nbt_hex_encode(tmp_ctx, name->name), 
+                                     name->type);
+       }
+       talloc_free(tmp_ctx);
+       return ret;
+}
+
index bce72d805f6fdea5f5accec90cd8f87df5e1beb0..00e0e740afb16bb8be9f222b91effe338c481533 100644 (file)
@@ -57,8 +57,9 @@ void nbtd_request_defense(struct nbt_name_socket *nbtsock,
 
        iname = nbtd_find_iname(iface, name, NBT_NM_ACTIVE);
        if (iname != NULL && !(iname->nb_flags & NBT_NM_GROUP)) {
-               DEBUG(2,("Defending name %s<%02x> on %s against %s\n",
-                        name->name, name->type, iface->bcast_address, src_address));
+               DEBUG(2,("Defending name %s on %s against %s\n",
+                        nbt_name_string(packet, name), 
+                        iface->bcast_address, src_address));
                nbtd_negative_name_registration_reply(nbtsock, packet, 
                                                      src_address, src_port);
        } else {
index 5b79bf315f42ba7d7b886545ac0167842ed57d78..7f8e6d4a24ac38ddd6214aee243fdd2dbc3af3c5 100644 (file)
@@ -82,8 +82,8 @@ static void nbtd_node_status_reply(struct nbt_name_socket *nbtsock,
           it could lead to giving attackers too much information */
        ZERO_STRUCT(packet->answers[0].rdata.status.statistics);
 
-       DEBUG(7,("Sending node status reply for %s<%02x> to %s:%d\n", 
-                name->name, name->type, src_address, src_port));
+       DEBUG(7,("Sending node status reply for %s to %s:%d\n", 
+                nbt_name_string(packet, name), src_address, src_port));
        
        nbt_name_reply_send(nbtsock, src_address, src_port, packet);
 
@@ -113,8 +113,8 @@ void nbtd_query_status(struct nbt_name_socket *nbtsock,
 
        iname = nbtd_find_iname(iface, name, NBT_NM_ACTIVE);
        if (iname == NULL) {
-               DEBUG(7,("Node status query for %s<%02x> from %s - not found on %s\n",
-                        name->name, name->type, src_address, iface->ip_address));
+               DEBUG(7,("Node status query for %s from %s - not found on %s\n",
+                        nbt_name_string(packet, name), src_address, iface->ip_address));
                return;
        }
 
index edca4ecb0077c501d1de5dc759b18319e4be1ebe..e6eec27fdcdd85d06125c53a79e38987fae872f5 100644 (file)
@@ -128,8 +128,8 @@ void nbtd_name_query_reply(struct nbt_name_socket *nbtsock,
                if (addr->ipaddr == NULL) goto failed;
        }
 
-       DEBUG(7,("Sending name query reply for %s<%02x> at %s to %s:%d\n", 
-                name->name, name->type, addresses[0], src_address, src_port));
+       DEBUG(7,("Sending name query reply for %s at %s to %s:%d\n", 
+                nbt_name_string(packet, name), addresses[0], src_address, src_port));
        
        nbt_name_reply_send(nbtsock, src_address, src_port, packet);
 
@@ -168,8 +168,8 @@ void nbtd_negative_name_query_reply(struct nbt_name_socket *nbtsock,
        packet->answers[0].ttl       = 0;
        ZERO_STRUCT(packet->answers[0].rdata);
 
-       DEBUG(7,("Sending negative name query reply for %s<%02x> to %s:%d\n", 
-                name->name, name->type, src_address, src_port));
+       DEBUG(7,("Sending negative name query reply for %s to %s:%d\n", 
+                nbt_name_string(packet, name), src_address, src_port));
        
        nbt_name_reply_send(nbtsock, src_address, src_port, packet);
 
@@ -209,12 +209,11 @@ void nbtd_negative_name_registration_reply(struct nbt_name_socket *nbtsock,
        packet->answers[0].ttl      = 0;
        packet->answers[0].rdata    = request_packet->additional[0].rdata;
 
-       DEBUG(7,("Sending negative name registration reply for %s<%02x> to %s:%d\n", 
-                name->name, name->type, src_address, src_port));
+       DEBUG(7,("Sending negative name registration reply for %s to %s:%d\n", 
+                nbt_name_string(packet, name), src_address, src_port));
        
        nbt_name_reply_send(nbtsock, src_address, src_port, packet);
 
 failed:
        talloc_free(packet);
 }
-
index cc14a762daf14e1ef2cfde6f9e93ddf01fb5ecb1..c51a146adf8d7760561b7342ab94b634677cc033 100644 (file)
@@ -79,8 +79,8 @@ void nbtd_request_query(struct nbt_name_socket *nbtsock,
           ignore it for now */
        if (!(iname->nb_flags & NBT_NM_ACTIVE) && 
            (packet->operation & NBT_FLAG_BROADCAST)) {
-               DEBUG(7,("Query for %s<%02x> from %s - name not active yet on %s\n",
-                        name->name, name->type, src_address, iface->ip_address));
+               DEBUG(7,("Query for %s from %s - name not active yet on %s\n",
+                        nbt_name_string(packet, name), src_address, iface->ip_address));
                return;
        }
 
index c28ba0764c2f681eb32294ca72c6cdd56d5df387..bec316cdea3b8d813492b9882e732cef9c79a1cb 100644 (file)
@@ -44,8 +44,9 @@ static void refresh_completion_handler(struct nbt_name_request *req)
 
        status = nbt_name_refresh_recv(req, tmp_ctx, &io);
        if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
-               DEBUG(4,("Refreshed name %s<%02x> on %s\n", 
-                        iname->name.name, iname->name.type, iname->iface->ip_address));
+               DEBUG(4,("Refreshed name %s on %s\n", 
+                        nbt_name_string(tmp_ctx, &iname->name), 
+                        iname->iface->ip_address));
                iname->registration_time = timeval_current();
                nbtd_start_refresh_timer(iname);
                talloc_free(tmp_ctx);
@@ -56,13 +57,14 @@ static void refresh_completion_handler(struct nbt_name_request *req)
        iname->nb_flags &= ~NBT_NM_ACTIVE;
 
        if (NT_STATUS_IS_OK(status)) {
-               DEBUG(1,("Name conflict from %s refreshing name %s<%02x> on %s - %s\n", 
-                        io.out.reply_addr, iname->name.name, iname->name.type, 
+               DEBUG(1,("Name conflict from %s refreshing name %s on %s - %s\n", 
+                        io.out.reply_addr, nbt_name_string(tmp_ctx, &iname->name),
                         iname->iface->ip_address, 
                         nt_errstr(nbt_rcode_to_ntstatus(io.out.rcode))));
        } else {
-               DEBUG(1,("Error refreshing name %s<%02x> on %s - %s\n", 
-                        iname->name.name, iname->name.type, iname->iface->ip_address,
+               DEBUG(1,("Error refreshing name %s on %s - %s\n", 
+                        nbt_name_string(tmp_ctx, &iname->name), 
+                        iname->iface->ip_address,
                         nt_errstr(status)));
        }
 
@@ -130,14 +132,17 @@ static void nbtd_register_handler(struct composite_context *req)
        struct nbtd_iface_name *iname = talloc_get_type(req->async.private, 
                                                        struct nbtd_iface_name);
        NTSTATUS status;
+       TALLOC_CTX *tmp_ctx = talloc_new(iname);
 
        status = nbt_name_register_bcast_recv(req);
        if (NT_STATUS_IS_OK(status)) {
                /* good - nobody complained about our registration */
                iname->nb_flags |= NBT_NM_ACTIVE;
-               DEBUG(3,("Registered %s<%02x> on interface %s\n",
-                        iname->name.name, iname->name.type, iname->iface->bcast_address));
+               DEBUG(3,("Registered %s on interface %s\n",
+                        nbt_name_string(tmp_ctx, &iname->name), 
+                        iname->iface->bcast_address));
                iname->registration_time = timeval_current();
+               talloc_free(tmp_ctx);
                nbtd_start_refresh_timer(iname);
                return;
        }
@@ -145,9 +150,10 @@ static void nbtd_register_handler(struct composite_context *req)
        /* someone must have replied with an objection! */
        iname->nb_flags |= NBT_NM_CONFLICT;
 
-       DEBUG(1,("Error registering %s<%02x> on interface %s - %s\n",
-                iname->name.name, iname->name.type, iname->iface->bcast_address,
+       DEBUG(1,("Error registering %s on interface %s - %s\n",
+                nbt_name_string(tmp_ctx, &iname->name), iname->iface->bcast_address,
                 nt_errstr(status)));
+       talloc_free(tmp_ctx);
 }
 
 
index e941d77e2882d178c5523870e4c63b2b71bb123f..cfb68a3aaf26928fa28b44307f7bcc116f81f646 100644 (file)
@@ -66,30 +66,31 @@ static void nbtd_wins_refresh_handler(struct composite_context *c)
        if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
                /* our WINS server is dead - start registration over
                   from scratch */
-               DEBUG(2,("Failed to refresh %s<%02x> with WINS server %s\n",
-                        iname->name.name, iname->name.type, iname->wins_server));
+               DEBUG(2,("Failed to refresh %s with WINS server %s\n",
+                        nbt_name_string(tmp_ctx, &iname->name), iname->wins_server));
+               talloc_free(tmp_ctx);
                nbtd_winsclient_register(iname);
                return;
        }
 
        if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(1,("Name refresh failure with WINS for %s<%02x> - %s\n", 
-                        iname->name.name, iname->name.type, nt_errstr(status)));
+               DEBUG(1,("Name refresh failure with WINS for %s - %s\n", 
+                        nbt_name_string(tmp_ctx, &iname->name), nt_errstr(status)));
                talloc_free(tmp_ctx);
                return;
        }       
 
        if (io.out.rcode != 0) {
-               DEBUG(1,("WINS server %s rejected name refresh of %s<%02x> - %s\n", 
-                        io.out.wins_server, iname->name.name, iname->name.type
+               DEBUG(1,("WINS server %s rejected name refresh of %s - %s\n", 
+                        io.out.wins_server, nbt_name_string(tmp_ctx, &iname->name)
                         nt_errstr(nbt_rcode_to_ntstatus(io.out.rcode))));
                iname->nb_flags |= NBT_NM_CONFLICT;
                talloc_free(tmp_ctx);
                return;
        }       
 
-       DEBUG(4,("Refreshed name %s<%02x> with WINS server %s\n",
-                iname->name.name, iname->name.type, iname->wins_server));
+       DEBUG(4,("Refreshed name %s with WINS server %s\n",
+                nbt_name_string(tmp_ctx, &iname->name), iname->wins_server));
        /* success - start a periodic name refresh */
        iname->nb_flags |= NBT_NM_ACTIVE;
        if (iname->wins_server) {
@@ -167,15 +168,15 @@ static void nbtd_wins_register_handler(struct composite_context *c)
        }
 
        if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(1,("Name register failure with WINS for %s<%02x> - %s\n", 
-                        iname->name.name, iname->name.type, nt_errstr(status)));
+               DEBUG(1,("Name register failure with WINS for %s - %s\n", 
+                        nbt_name_string(tmp_ctx, &iname->name), nt_errstr(status)));
                talloc_free(tmp_ctx);
                return;
        }       
 
        if (io.out.rcode != 0) {
-               DEBUG(1,("WINS server %s rejected name register of %s<%02x> - %s\n", 
-                        io.out.wins_server, iname->name.name, iname->name.type
+               DEBUG(1,("WINS server %s rejected name register of %s - %s\n", 
+                        io.out.wins_server, nbt_name_string(tmp_ctx, &iname->name)
                         nt_errstr(nbt_rcode_to_ntstatus(io.out.rcode))));
                iname->nb_flags |= NBT_NM_CONFLICT;
                talloc_free(tmp_ctx);
@@ -196,8 +197,8 @@ static void nbtd_wins_register_handler(struct composite_context *c)
                        nbtd_wins_refresh,
                        iname);
 
-       DEBUG(3,("Registered %s<%02x> with WINS server %s\n",
-                iname->name.name, iname->name.type, iname->wins_server));
+       DEBUG(3,("Registered %s with WINS server %s\n",
+                nbt_name_string(tmp_ctx, &iname->name), iname->wins_server));
 
        talloc_free(tmp_ctx);
 }
index c7720b6ec7ce7085768668a6eb7df0620048f296..22cfee415c743757c0b3ff91ef8d9a059dc492ec 100644 (file)
@@ -24,6 +24,7 @@
 #include "nbt_server/nbt_server.h"
 
 
+
 static void nbtd_winsserver_query(struct nbt_name_socket *nbtsock, 
                                  struct nbt_name_packet *packet, 
                                  const char *src_address, int src_port)
index 3b5338147b7dfda2829c54bc3706970eca7b0cb8..afbe91f6ec8d582e4ef2522fce37f93e61148786 100644 (file)
@@ -65,11 +65,8 @@ static BOOL nbt_test_wins_name(TALLOC_CTX *mem_ctx, const char *address,
           the right IP */
        socket_listen(nbtsock->sock, myaddress, 0, 0, 0);
 
-       printf("Testing name registration to WINS with name %s<%02x> at %s\n", 
-              name->name, name->type, myaddress);
-       if (name->scope) {
-               printf("scope is %s\n", name->scope);
-       }
+       printf("Testing name registration to WINS with name %s at %s\n", 
+              nbt_name_string(mem_ctx, name), myaddress);
 
        printf("release the name\n");
        release.in.name = *name;