167d4b
From 170166b8a0076089c6a8505f53a22f5b72c15786 Mon Sep 17 00:00:00 2001
167d4b
From: Jeremy Allison <jra@samba.org>
167d4b
Date: Tue, 28 Oct 2014 11:55:30 -0700
167d4b
Subject: [PATCH] s3-nmbd: Fix netbios name truncation.
167d4b
167d4b
Try and cope with truncation more intelligently.
167d4b
167d4b
BUG: https://bugzilla.samba.org/show_bug.cgi?id=10896
167d4b
167d4b
Signed-off-by: Jeremy Allison <jra@samba.org>
167d4b
Reviewed-by: Andreas Schneider <asn@samba.org>
167d4b
(cherry picked from commit 6adcc7bffd5e1474ecba04d2328955c0b208cabc)
167d4b
Signed-off-by: Andreas Schneider <asn@samba.org>
167d4b
---
167d4b
 source3/nmbd/nmbd_nameregister.c | 76 +++++++++++++++++++++++++++++++++++-----
167d4b
 1 file changed, 68 insertions(+), 8 deletions(-)
167d4b
167d4b
diff --git a/source3/nmbd/nmbd_nameregister.c b/source3/nmbd/nmbd_nameregister.c
167d4b
index 71c4751..8b078e6 100644
167d4b
--- a/source3/nmbd/nmbd_nameregister.c
167d4b
+++ b/source3/nmbd/nmbd_nameregister.c
167d4b
@@ -482,17 +482,77 @@ void register_name(struct subnet_record *subrec,
167d4b
 {
167d4b
 	struct nmb_name nmbname;
167d4b
 	nstring nname;
167d4b
+	size_t converted_size;
167d4b
 
167d4b
 	errno = 0;
167d4b
-	push_ascii_nstring(nname, name);
167d4b
-        if (errno == E2BIG) {
167d4b
-		unstring tname;
167d4b
-		pull_ascii_nstring(tname, sizeof(tname), nname);
167d4b
-		DEBUG(0,("register_name: NetBIOS name %s is too long. Truncating to %s\n",
167d4b
-			name, tname));
167d4b
-		make_nmb_name(&nmbname, tname, type);
167d4b
-	} else {
167d4b
+	converted_size = push_ascii_nstring(nname, name);
167d4b
+	if (converted_size != (size_t)-1) {
167d4b
+		/* Success. */
167d4b
 		make_nmb_name(&nmbname, name, type);
167d4b
+	} else if (errno == E2BIG) {
167d4b
+		/*
167d4b
+		 * Name converted to CH_DOS is too large.
167d4b
+		 * try to truncate.
167d4b
+		 */
167d4b
+		char *converted_str_dos = NULL;
167d4b
+		char *converted_str_unix = NULL;
167d4b
+		bool ok;
167d4b
+
167d4b
+		converted_size = 0;
167d4b
+
167d4b
+		ok = convert_string_talloc(talloc_tos(),
167d4b
+				CH_UNIX,
167d4b
+				CH_DOS,
167d4b
+				name,
167d4b
+				strlen(name)+1,
167d4b
+				&converted_str_dos,
167d4b
+				&converted_size);
167d4b
+		if (!ok) {
167d4b
+			DEBUG(0,("register_name: NetBIOS name %s cannot be "
167d4b
+				"converted. Failing to register name.\n",
167d4b
+				name));
167d4b
+			return;
167d4b
+		}
167d4b
+
167d4b
+		/*
167d4b
+		 * As it's now CH_DOS codepage
167d4b
+		 * we truncate by writing '\0' at
167d4b
+		 * MAX_NETBIOSNAME_LEN-1 and then
167d4b
+		 * convert back to CH_UNIX which we
167d4b
+		 * need for the make_nmb_name() call.
167d4b
+		 */
167d4b
+		if (converted_size >= MAX_NETBIOSNAME_LEN) {
167d4b
+			converted_str_dos[MAX_NETBIOSNAME_LEN-1] = '\0';
167d4b
+		}
167d4b
+
167d4b
+		ok = convert_string_talloc(talloc_tos(),
167d4b
+				CH_DOS,
167d4b
+				CH_UNIX,
167d4b
+				converted_str_dos,
167d4b
+				strlen(converted_str_dos)+1,
167d4b
+				&converted_str_unix,
167d4b
+				&converted_size);
167d4b
+		if (!ok) {
167d4b
+			DEBUG(0,("register_name: NetBIOS name %s cannot be "
167d4b
+				"converted back to CH_UNIX. "
167d4b
+				"Failing to register name.\n",
167d4b
+				converted_str_dos));
167d4b
+			TALLOC_FREE(converted_str_dos);
167d4b
+			return;
167d4b
+		}
167d4b
+
167d4b
+		make_nmb_name(&nmbname, converted_str_unix, type);
167d4b
+
167d4b
+		TALLOC_FREE(converted_str_dos);
167d4b
+		TALLOC_FREE(converted_str_unix);
167d4b
+	} else {
167d4b
+		/*
167d4b
+		 * Generic conversion error. Fail to register.
167d4b
+		 */
167d4b
+		DEBUG(0,("register_name: NetBIOS name %s cannot be "
167d4b
+			"converted (%s). Failing to register name.\n",
167d4b
+			name, strerror(errno)));
167d4b
+		return;
167d4b
 	}
167d4b
 
167d4b
 	/* Always set the NB_ACTIVE flag on the name we are
167d4b
-- 
167d4b
2.1.2
167d4b
167d4b
From 653a1c312e6b85f1d8113beec52a27e0ba71ef79 Mon Sep 17 00:00:00 2001
167d4b
From: Jeremy Allison <jra@samba.org>
167d4b
Date: Fri, 31 Oct 2014 11:01:26 -0700
167d4b
Subject: [PATCH] s3: nmbd: Ensure NetBIOS names are only 15 characters stored.
167d4b
167d4b
This screws up if the name is greater than MAX_NETBIOSNAME_LEN-1 in the
167d4b
unix charset, but less than or equal to MAX_NETBIOSNAME_LEN-1 in the DOS
167d4b
charset, but this is so old we have to live with that.
167d4b
167d4b
BUG: https://bugzilla.samba.org/show_bug.cgi?id=10920
167d4b
167d4b
Signed-off-by: Jeremy Allison <jra@samba.org>
167d4b
Reviewed-by: Andreas Schneider <asn@samba.org>
167d4b
167d4b
(cherry picked from commit 7467f6e72cba214eeca75c34e9d9fba354c7ef31)
167d4b
Signed-off-by: Andreas Schneider <asn@samba.org>
167d4b
---
167d4b
 source3/lib/util_names.c | 10 +++++++++-
167d4b
 1 file changed, 9 insertions(+), 1 deletion(-)
167d4b
167d4b
diff --git a/source3/lib/util_names.c b/source3/lib/util_names.c
167d4b
index cf54a0e..1392b48 100644
167d4b
--- a/source3/lib/util_names.c
167d4b
+++ b/source3/lib/util_names.c
167d4b
@@ -60,7 +60,15 @@ static bool set_my_netbios_names(const char *name, int i)
167d4b
 {
167d4b
 	SAFE_FREE(smb_my_netbios_names[i]);
167d4b
 
167d4b
-	smb_my_netbios_names[i] = SMB_STRDUP(name);
167d4b
+	/*
167d4b
+	 * Don't include space for terminating '\0' in strndup,
167d4b
+	 * it is automatically added. This screws up if the name
167d4b
+	 * is greater than MAX_NETBIOSNAME_LEN-1 in the unix
167d4b
+	 * charset, but less than or equal to MAX_NETBIOSNAME_LEN-1
167d4b
+	 * in the DOS charset, but this is so old we have to live
167d4b
+	 * with that.
167d4b
+	 */
167d4b
+	smb_my_netbios_names[i] = SMB_STRNDUP(name, MAX_NETBIOSNAME_LEN-1);
167d4b
 	if (!smb_my_netbios_names[i])
167d4b
 		return False;
167d4b
 	return strupper_m(smb_my_netbios_names[i]);
167d4b
-- 
167d4b
2.1.2
167d4b