Bug 113763

Summary: cannot browse SMB printers outside local network
Product: [Unmaintained] kdeprint Reporter: Gérald Macinenti <gmacinenti>
Component: generalAssignee: KDEPrint Devel Mailinglist <kde-print-devel>
Status: CLOSED FIXED    
Severity: wishlist CC: jlayt, web
Priority: NOR    
Version: unspecified   
Target Milestone: ---   
Platform: Fedora RPMs   
OS: Linux   
Latest Commit: Version Fixed In:
Sentry Crash Report:
Attachments: use wins server to browse smb printers outside of the local network
use wins server to browse smb printers outside of the local network (bis)

Description Gérald Macinenti 2005-10-03 10:18:20 UTC
Version:            (using KDE KDE 3.4.2)
Installed from:    Fedora RPMs
OS:                Linux

When trying to browse SMB printers from the Kaddprinterwizard from a Linux client registerd in a NetBIOS domain that doesn't match the client DNS domain, the "nmblookup" command called in background prints a Usage error (if Kaddprinterwizzard is launched from the console) and no domain nor printers are discovered.
Comment 1 Gérald Macinenti 2005-10-03 12:19:28 UTC
Created attachment 12826 [details]
use wins server to browse smb printers outside of the local network
Comment 2 Cristian Tibirna 2005-10-04 04:38:26 UTC
I won't be able to test this, as I can't create such a setup (not enough computers/routers etc.)

I have two quirks with the patch. Of course, better have it this way than not at all, but still:

a) isn't /etc/samba/smb.conf distribution dependent?
b) according to smb.conf(5) manpage, parameter name and value are not case sensitive (thus, looking for 'wins server' in the file matches only one in 2^10 possibilities -- of course, we're very lucky and most distributors will just use lowercase).

Anyways, I'm nitpicking. Thanks for the contribution and if I get a confirmation that the patch applied as given in the current code works, I will add it depending on release schedules.

Thanks
Comment 3 Gérald Macinenti 2005-10-10 15:35:37 UTC
Created attachment 12936 [details]
use wins server to browse smb printers outside of the local network  (bis)

here is an update of my previous patch based on Cristian Tibirna remarks:
1) /etc/samba/smb.conf is "standard" amongst mainstream distributions I know
such as Debian, Redhat Fedora, Suse, if anyone can give a significative
counter-example here, of course would I do the required adaptation
2) smb.conf parsing improved by a more attentiv re-read of man smb.conf ;)

note: this patch was diffed against KDE 3.5 svn sources (the previous used
3.4.2 sources from Fedora) 

hope this can help.
Comment 4 Cristian Tibirna 2005-10-10 20:20:37 UTC
SVN commit 469327 by tibirna:

use WINS for SMB printer detection. Thanks to Gerard Macinenti for
patch.
BUG:113763


 M  +42 -2     smbview.cpp  
 M  +1 -0      smbview.h  


--- trunk/KDE/kdelibs/kdeprint/management/smbview.cpp #469326:469327
@@ -31,6 +31,11 @@
 #include <kmessagebox.h>
 #include <kcursor.h>
 
+#include <qfile.h>
+#include <qtextstream.h>
+#include <cstdlib>
+
+
 //*********************************************************************************************
 
 SmbView::SmbView(QWidget *parent)
@@ -126,7 +131,42 @@
 
 void SmbView::init()
 {
-	QString	cmd("nmblookup -M -- - | grep '<01>' | awk '{print $1}' | xargs nmblookup -A | grep '<1d>'");
+	// Open Samba configuration file and check if a WINS server is defined
+	m_wins_server = QString::null;
+	QString wins_keyword("wins server");	
+	QFile smb_conf ("/etc/samba/smb.conf");
+	if (smb_conf.exists () && smb_conf.open (IO_ReadOnly))
+	{
+		QTextStream smb_stream (&smb_conf);
+		while (!smb_stream.atEnd ())
+		{
+			QString smb_line = smb_stream.readLine ();
+			if (smb_line.contains (wins_keyword, FALSE) > 0)
+			{
+				QString key = smb_line.section ('=', 0, 0);
+				key = key.stripWhiteSpace();
+				if (key.contains(wins_keyword, FALSE) * wins_keyword.contains(key, FALSE) != 1)
+				{
+					continue;
+				}
+				m_wins_server = smb_line.section ('=', 1, 1);
+				// take only the first declared WINS server
+				m_wins_server = m_wins_server.section(',', 0, 0);
+				m_wins_server = m_wins_server.stripWhiteSpace ();
+				m_wins_server = m_wins_server.section(' ', 0, 0);
+				// strip any server tag (see man smb.conf(5))
+				if (m_wins_server.section(':', 1, 1) != NULL)
+				{
+					m_wins_server = m_wins_server.section(':', 1, 1);
+				}
+				break;
+			}
+		}
+		smb_conf.close ();
+	}
+	m_wins_server = m_wins_server.isEmpty ()? " " : " -U " + m_wins_server + " ";
+	QString cmd ("nmblookup" + m_wins_server +
+					"-M -- - | grep '<01>' | awk '{print $1}' | xargs nmblookup -A | grep '<1d>'");
 	*m_proc << cmd;
 	startProcess(GroupListing);
 }
@@ -138,7 +178,7 @@
 		if (item->depth() == 0)
 		{ // opening group
 			m_current = item;
-			*m_proc << "nmblookup -M ";
+			*m_proc << "nmblookup"+m_wins_server+"-M ";
                         *m_proc << KProcess::quote(item->text(0));
                         *m_proc << " -S | grep '<20>' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*<20>.*//' | xargs -iserv_name smbclient -N -L 'serv_name' -W ";
                         *m_proc << KProcess::quote(item->text(0));
--- trunk/KDE/kdelibs/kdeprint/management/smbview.h #469326:469327
@@ -61,6 +61,7 @@
 	QString		m_buffer;
 	QString		m_login, m_password;
 	KTempFile	*m_passwdFile;
+	QString		m_wins_server;
 };
 
 #endif
Comment 5 Cristian Tibirna 2005-10-10 20:29:14 UTC
*** Bug 103428 has been marked as a duplicate of this bug. ***
Comment 6 John Layt 2008-12-31 19:05:05 UTC
Closing old Resolved status bug.