1
/***************************************************************************
2
smb4kscanner.cpp - The network scan core class of Smb4K.
4
begin : Sam Mai 31 2003
5
copyright : (C) 2003 by Alexander Reinholdt
6
email : dustpuppy@mail.berlios.de
7
***************************************************************************/
9
/***************************************************************************
11
* This program is free software; you can redistribute it and/or modify *
12
* it under the terms of the GNU General Public License as published by *
13
* the Free Software Foundation; either version 2 of the License, or *
14
* (at your option) any later version. *
16
***************************************************************************/
19
#include <qapplication.h>
24
#include <kapplication.h>
26
// Application specific includes.
27
#include "smb4kscanner.h"
28
#include "smb4kcore.h"
31
Smb4KScanner::Smb4KScanner( QObject *parent, const char *name ) : QObject( parent, name )
33
m_proc = new KProcess( this, "ScannerProcess" );
34
m_proc->setUseShell( true );
37
m_workgroup = QString::null;
38
m_host = QString::null;
39
m_share = QString::null;
40
m_path = QString::null;
42
m_config = kapp->config();
44
m_timer = new QTimer( this );
45
m_timer->start( 50, false );
49
m_queue.setAutoDelete( true );
51
QString *input = new QString( QString( "%1:" ).arg( Init ) );
52
m_queue.enqueue( input );
54
connect( m_proc, SIGNAL( receivedStdout( KProcess *, char *, int ) ), this, SLOT( slotReceivedStdout( KProcess *, char *, int ) ) );
55
connect( m_proc, SIGNAL( processExited( KProcess* ) ), this, SLOT( slotProcessExited( KProcess * ) ) );
56
connect( m_proc, SIGNAL( receivedStderr( KProcess *, char *, int ) ), this, SLOT( slotReceivedStderr( KProcess *, char *, int ) ) );
57
connect( m_timer, SIGNAL( timeout() ), this, SLOT( start() ) );
61
Smb4KScanner::~Smb4KScanner()
63
// Do not use the abort() function here, because this
64
// could cause crashes due to the emitted killed()
66
if ( m_proc->isRunning() )
69
m_workgroupList.clear();
74
/****************************************************************************
75
These function initializes/aborts any action, that's done by the scanner.
76
****************************************************************************/
78
void Smb4KScanner::m_init()
80
m_config->setGroup( "Browse Options" );
81
bool auth_for_browse_list = m_config->readBoolEntry( "Auth For Browse List", false );
82
QString method = m_config->readEntry( "Browse List", "nmblookup" );
83
QString host_to_query = m_config->readEntry( "Query Host", QString::null );
85
QString wins = ((Smb4KCore *)parent())->getWINSServer();
86
QString nmblookup_options = getNmblookupOptions();
87
QString smbclient_options = getSmbclientOptions();
91
// Use nmblookup or query host to obtain the browse list?
92
if ( method == "nmblookup" )
94
command = QString( "nmblookup -M" );
96
if ( !nmblookup_options.stripWhiteSpace().isEmpty() )
97
command.append( nmblookup_options );
99
command.append( " -- - | grep '<01>' | awk '{print $1}'" );
101
// Query the WINS server, if available
102
if ( wins.isEmpty() )
103
command.append( " | xargs nmblookup -A" );
105
command.append( QString( " | xargs nmblookup -R -U %1 -A" ).arg( wins ) );
108
startProcess( Groups );
110
else if ( method == "host" )
112
command = QString( "smbclient -d1" );
114
if ( !smbclient_options.stripWhiteSpace().isEmpty() )
116
command.append( smbclient_options );
119
if ( auth_for_browse_list )
121
Smb4KAuthInfo *auth = ((Smb4KCore *)parent())->passwdReader()->getAuth( QString::null, host_to_query, QString::null );
123
if ( !auth->user().isEmpty() )
125
command.append( QString( " -U %1" ).arg( KProcess::quote( auth->user() ) ) );
127
if ( !auth->password().isEmpty() )
129
m_proc->setEnvironment( "PASSWD", auth->password() );
134
command.append( " -U %" );
139
command.append( " -U %" );
142
command.append( QString( " -L %1" ).arg( KProcess::quote( host_to_query ) ) );
146
startProcess( QueryHost );
151
/****************************************************************************
152
Aborts any process that is running.
153
****************************************************************************/
155
void Smb4KScanner::abort()
157
if ( m_proc->isRunning() )
166
/****************************************************************************
167
Gets the preview of a share. (public part)
168
****************************************************************************/
170
void Smb4KScanner::getPreview( const QString &workgroup, const QString &host, const QString &ip, const QString &share, const QString &path )
172
m_queue.enqueue( new QString( QString( "%1:%2:%3:%4:%5:%6" ).arg( Preview ).arg( workgroup ).arg( host ).arg( ip ).arg( share ).arg( path ) ) );
176
/****************************************************************************
177
Gets the preview of a share. (private part)
178
****************************************************************************/
180
void Smb4KScanner::m_previewShare( const QString &workgroup, const QString &host, const QString &ip, const QString &share, const QString &path )
182
QString smbclient_options = getSmbclientOptions();
184
m_workgroup = workgroup;
191
Smb4KAuthInfo *auth = ((Smb4KCore *)parent())->passwdReader()->getAuth( workgroup, host, share );
194
command = QString( "smbclient //%1/%2 -d1 -W %3 -c 'ls" ).arg( KProcess::quote( host ) ).arg( KProcess::quote( share ) ).arg( KProcess::quote( workgroup ) );
196
if ( !path.isEmpty() )
198
// To be able to display directories, that contain umlauts, we have to do
200
// (1) replace all slashes by backslashes;
201
// (2) convert the directory name to local 8 bit.
202
QString proc_path = path;
203
proc_path = proc_path.replace( QChar( '/' ), QChar( '\\' ) ).local8Bit();
204
command.append( " \"" ).append( proc_path ).append( "*\"" );
207
command.append( "'" );
211
command.append( QString( " -I %1" ).arg( KProcess::quote( ip ) ) );
214
if ( !smbclient_options.stripWhiteSpace().isEmpty() )
216
command.append( smbclient_options );
219
if ( !auth->user().isEmpty() )
221
command.append( QString( " -U %1" ).arg( KProcess::quote( auth->user() ) ) );
223
if ( !auth->password().isEmpty() )
225
m_proc->setEnvironment( "PASSWD", auth->password() );
230
command.append( " -U %" );
233
m_proc->operator<<( command );
234
startProcess( Preview );
238
/****************************************************************************
239
Scans for workgroup members. (public part)
240
****************************************************************************/
242
void Smb4KScanner::getWorkgroupMembers( const QString &workgroup, const QString &master, const QString &ip )
244
m_queue.enqueue( new QString( QString( "%1:%2:%3:%4" ).arg( Hosts ).arg( workgroup ).arg( master ).arg( ip ) ) );
248
/****************************************************************************
249
Scans for workgroup members. (private part)
250
****************************************************************************/
252
void Smb4KScanner::m_scanForWorkgroupMembers( const QString &workgroup, const QString &master, const QString &ip )
254
m_config->setGroup( "Browse Options" );
255
bool master_auth = m_config->readBoolEntry( "Master Browser Auth", false );
257
QString smbclient_options = getSmbclientOptions();
259
m_workgroup = workgroup;
263
// Scan for the host list.
264
QString command = QString( "smbclient -d1" );
266
if ( !smbclient_options.stripWhiteSpace().isEmpty() )
268
command.append( smbclient_options );
273
Smb4KAuthInfo *auth = ((Smb4KCore *)parent())->passwdReader()->getAuth( workgroup, master, QString::null );
275
if ( !auth->user().isEmpty() )
277
command.append( QString( " -U %1" ).arg( KProcess::quote( auth->user() ) ) );
279
if ( !auth->password().isEmpty() )
281
m_proc->setEnvironment( "PASSWD", auth->password() );
286
command.append( " -U %" );
291
command.append( " -U %" );
296
command.append( QString( " -I %1" ).arg( ip ) );
299
command.append( QString( " -W %1 -L %2" ).arg( KProcess::quote( workgroup ) ).arg( KProcess::quote( master ) ) );
301
m_proc->operator<<( command );
302
startProcess( Hosts );
306
/****************************************************************************
307
Scans for shares on a selected host. (public part)
308
****************************************************************************/
310
void Smb4KScanner::getShares( const QString &workgroup, const QString &host, const QString &ip )
312
m_queue.enqueue( new QString( QString( "%1:%2:%3:%4" ).arg( Shares ).arg( workgroup ).arg( host ).arg( ip ) ) );
316
/****************************************************************************
317
Scans for shares on a selected host. (private part)
318
****************************************************************************/
320
void Smb4KScanner::m_scanForShares( const QString &workgroup, const QString &host, const QString &ip )
322
QString smbclient_options = getSmbclientOptions();
324
m_workgroup = workgroup;
328
Smb4KAuthInfo *auth = ((Smb4KCore *)parent())->passwdReader()->getAuth( workgroup, host, QString::null );
330
// Assemble the scan command.
331
QString command = QString( "smbclient -d1 -W %1" ).arg( KProcess::quote( workgroup ) );
335
command.append( QString( " -I %1" ).arg( KProcess::quote( ip ) ) );
338
if ( !smbclient_options.stripWhiteSpace().isEmpty() )
340
command.append( smbclient_options );
343
if ( !auth->user().isEmpty() )
345
command.append( QString( " -U %1" ).arg( KProcess::quote( auth->user() ) ) );
347
if ( !auth->password().isEmpty() )
349
m_proc->setEnvironment( "PASSWD", auth->password() );
354
command.append( " -U guest%" );
357
command.append( " -L " ).append( KProcess::quote( host ) );
359
m_proc->operator<<( command );
360
startProcess( Shares );
364
/****************************************************************************
365
Gets more info on a selected host. (public part)
366
****************************************************************************/
368
void Smb4KScanner::getInfo( const QString &workgroup, const QString &host, const QString &ip )
370
m_queue.enqueue( new QString( QString( "%1:%2:%3:%4" ).arg( Info ).arg( workgroup ).arg( host ).arg( ip ) ) );
374
/****************************************************************************
375
Gets more info on a selected host. (private part)
376
****************************************************************************/
378
void Smb4KScanner::m_scanForInfo( const QString &workgroup, const QString &host, const QString &ip )
380
QString smbclient_options = getSmbclientOptions();
382
m_workgroup = workgroup;
385
QString command = QString( "smbclient -d1 -U % -W %1 -L %2" ).arg( KProcess::quote( workgroup ) ).arg( KProcess::quote( host ) );
389
command.append( QString( " -I %1" ).arg( KProcess::quote( ip ) ) );
392
if ( !smbclient_options.stripWhiteSpace().isEmpty() )
394
command.append( smbclient_options );
397
m_proc->operator<<( command );
399
startProcess( Info );
403
/****************************************************************************
404
Searches for a host. (public part)
405
****************************************************************************/
407
void Smb4KScanner::makeSearch( const QString &host )
409
m_queue.enqueue( new QString( QString( "%1:%2" ).arg( Search ).arg( host ) ) );
413
/****************************************************************************
414
Searches for a host. (private part)
415
****************************************************************************/
417
void Smb4KScanner::m_searchForHost( const QString &host )
419
m_config->setGroup( "Browse Options" );
420
QString search_method = m_config->readEntry( "Network Search", "nmblookup" );
422
if ( search_method == "smbclient" && host.stripWhiteSpace().contains( "." ) == 3 )
424
emit error( ERROR_IP_CANNOT_BE_USED, QString::null );
426
emit running( SCANNER_STOP, m_working );
430
QString wins = ((Smb4KCore *)parent())->getWINSServer();
431
QString nmblookup_options = getNmblookupOptions();
432
QString smbclient_options = getSmbclientOptions();
438
if ( search_method == "nmblookup" )
440
command = QString( "nmblookup" );
442
if ( !nmblookup_options.stripWhiteSpace().isEmpty() )
444
command.append( nmblookup_options );
447
if ( host.contains( '.', true ) != 3 )
449
if ( !wins.isEmpty() )
451
command.append( QString( " -R -U %1 %2 -S | grep '<00>' | sed -e 's/<00>.*//'" ).arg( wins ).arg( m_host ) );
455
command.append( QString( " %1 -S | grep '<00>' | sed -e 's/<00>.*//'" ).arg( m_host ) );
460
if ( !wins.isEmpty() )
462
command.append( QString( " -R -U %1 %2 -A | grep '<00>' | sed -e 's/<00>.*//'" ).arg( wins ).arg( m_host ) );
466
command.append( QString( " %1 -A | grep '<00>' | sed -e 's/<00>.*//'" ).arg( m_host ) );
472
command = QString( "smbclient -d2 -U % -L %1" ).arg( m_host );
474
if ( !smbclient_options.stripWhiteSpace().isEmpty() )
476
command.append( smbclient_options );
480
m_proc->operator<<( command );
482
startProcess( Search );
486
/****************************************************************************
488
****************************************************************************/
490
void Smb4KScanner::startProcess( int state )
493
m_buffer = QString::null;
494
QApplication::setOverrideCursor( waitCursor );
495
m_proc->start( KProcess::NotifyOnExit, KProcess::AllOutput );
499
/****************************************************************************
500
Tell the program what it has to do.
501
****************************************************************************/
503
void Smb4KScanner::endProcess()
532
m_workgroup = QString::null;
533
m_host = QString::null;
534
m_ip = QString::null;
535
m_share = QString::null;
536
m_path = QString::null;
537
QApplication::restoreOverrideCursor();
538
m_proc->clearArguments();
541
emit running( SCANNER_STOP, m_working );
545
/****************************************************************************
546
Process the list of workgroups.
547
****************************************************************************/
549
void Smb4KScanner::processWorkgroups()
551
QStringList list = QStringList::split( '\n', m_buffer, false );
553
m_workgroupList.clear();
555
QString group, master, ip;
557
for ( QStringList::Iterator it = list.begin(); it != list.end(); ++it )
559
if ( (*it).stripWhiteSpace().startsWith( "Looking" ) )
561
ip = (*it).stripWhiteSpace().section( " ", -1, -1 );
564
else if ( (*it).contains( "<00>" ) != 0 && (*it).contains( "<GROUP>" ) == 0 )
566
if ( group.isEmpty() && master.isEmpty() && !ip.isEmpty() )
567
master = (*it).section( "<00>", 0, 0 ).stripWhiteSpace();
571
else if ( (*it).contains( "<00>" ) != 0 && (*it).contains( "<GROUP>" ) != 0 )
573
if ( group.isEmpty() && !master.isEmpty() && !ip.isEmpty() )
575
group = (*it).left( (*it).find( "<00>" ) ).stripWhiteSpace();
577
m_workgroupList.append( new Smb4KWorkgroupItem( group, master, ip ) );
579
group = QString::null;
580
master = QString::null;
588
emit workgroups( m_workgroupList );
592
void Smb4KScanner::processQueryHost()
594
m_config->setGroup( "Browse Options" );
595
QString host_to_query = m_config->readEntry( "Query Host", QString::null );
597
QStringList list = QStringList::split( '\n', m_buffer, false );
599
m_workgroupList.clear();
601
if ( list.grep( "tree connect failed: NT_STATUS_ACCESS_DENIED" ).count() == 0 &&
602
list.grep( "NT_STATUS_LOGON_FAILURE" ).count() == 0 &&
603
list.grep( "Connection to "+host_to_query+" failed" ).count() == 0 )
605
// Get the line starting with Workgroup.
608
while( index < list.count() )
610
if ( list[index++].stripWhiteSpace().startsWith( "Workgroup" ) )
618
while ( index < list.count() )
620
QString line = list[index].stripWhiteSpace();
621
QString master, workgroup;
622
master = line.section( " ", -1, -1 ).stripWhiteSpace();
623
workgroup = line.section( " ", 0, -2 ).stripWhiteSpace();
625
if ( workgroup.isEmpty() )
627
if ( master.isEmpty() )
633
m_workgroupList.append( new Smb4KWorkgroupItem( workgroup, master ) );
638
emit workgroups( m_workgroupList );
642
emit workgroups( m_workgroupList );
644
if ( list.grep( "tree connect failed: NT_STATUS_ACCESS_DENIED" ).count() != 0 )
646
// We should find an entry for the workgroup here:
647
QString workgroup = list.grep( "Domain" ).first().section( "Domain=[", 1, 1 ).section( "]", 0, 0 ).stripWhiteSpace();
649
if ( !workgroup.isEmpty() )
651
if ( ((Smb4KCore *)parent())->passwdReader()->askpass( workgroup, host_to_query, QString::null, Smb4KPasswdReader::AccessDenied ) )
653
QString *input = new QString( QString( "%1:" ).arg( Init ) );
654
m_queue.enqueue( input );
659
emit error( ERROR_GETTING_BROWSELIST, list.grep( "NT_STATUS" ).first() );
662
else if ( list.grep( "NT_STATUS_LOGON_FAILURE" ).count() != 0 )
664
emit error( ERROR_GETTING_BROWSELIST, m_buffer );
666
else if ( list.grep( "Connection to "+host_to_query+" failed" ).count() != 0 )
668
emit error( ERROR_GETTING_BROWSELIST, m_buffer );
674
/****************************************************************************
675
Process the memeber list of a workgroup.
676
****************************************************************************/
678
void Smb4KScanner::processHosts()
680
QStringList list = QStringList::split( '\n', m_buffer, false );
682
if ( list.grep( "Connection to" ).count() != 0 )
684
QString errmsg = list.grep( "Connection to" ).first().stripWhiteSpace();
685
emit error( ERROR_GETTING_MEMBERS, errmsg );
687
else if ( m_buffer.contains( "tree connect failed: NT_STATUS_ACCESS_DENIED" ) == 0
688
&& m_buffer.contains( "NT_STATUS_LOGON_FAILURE" ) != 0 )
690
if ( ((Smb4KCore *)parent())->passwdReader()->askpass( m_workgroup, m_host, QString::null, Smb4KPasswdReader::LogonFailure ) )
692
QString *input = new QString( QString( "%1:%2:%3" ).arg( Hosts ).arg( m_workgroup ).arg( m_host ) );
693
m_queue.enqueue( input );
698
// Get the begin of the host listing.
700
for ( ; index < list.count(); index++ )
701
if ( list[index].stripWhiteSpace().startsWith( "Server" ) )
706
// Process list of hosts:
707
QValueList<Smb4KHostItem *> hosts;
711
while ( index < list.count() )
713
line = list[index++].stripWhiteSpace();
715
if ( line.startsWith( "Workgroup" ) )
719
else if ( line.contains( " " ) == 0 )
721
hosts.append( new Smb4KHostItem( m_workgroup, line ) );
725
hosts.append( new Smb4KHostItem( m_workgroup, line.section( " ", 0, 0 ).stripWhiteSpace(), line.section( " ", 1, -1 ).stripWhiteSpace() ) );
729
// If the list is empty, put the master in.
730
if ( hosts.isEmpty() )
731
hosts.append( new Smb4KHostItem( m_workgroup, m_host ) );
733
emit members( hosts );
738
/****************************************************************************
739
Process the share list of a host.
740
****************************************************************************/
742
void Smb4KScanner::processShares()
744
QStringList list = QStringList::split( "\n", m_buffer, false );
746
// Get the OS and Server strings.
747
Smb4KHostItem *host = new Smb4KHostItem( m_workgroup, m_host );
750
for ( ; index < list.count(); index++ )
752
if ( list[index].stripWhiteSpace().startsWith( "Domain" ) || list[index].stripWhiteSpace().startsWith( "OS" ) )
754
host->setOSString( list[index].section( "OS=[", 1, 1 ).section( "]", 0, 0 ) );
755
host->setServerString( list[index].section( "Server=[", 1, 1 ).section( "]", 0, 0 ) );
757
else if ( list[index].stripWhiteSpace().startsWith( "Sharename" ) )
772
if ( list.grep( "NT_STATUS" ).count() != 0 || list.grep( "Connection to" ).count() != 0 || list.grep( "tree connect failed:" ).count() != 0 )
774
if ( list.grep( "NT_STATUS" ).count() != 0 )
776
QString errmsg = list.grep( "NT_STATUS" ).first().stripWhiteSpace().section( " ", -1, -1 );
778
// The error output of smbclient is a little bit inconsistent:
779
if ( errmsg.contains( "NT_STATUS" ) == 0 )
781
errmsg = list.grep( "NT_STATUS" ).first().stripWhiteSpace().section( " ", 0, 0 );
784
if ( errmsg == "NT_STATUS_ACCESS_DENIED" || errmsg == "NT_STATUS_LOGON_FAILURE" )
786
int state = Smb4KPasswdReader::None;
788
if ( errmsg == "NT_STATUS_ACCESS_DENIED" )
790
state = Smb4KPasswdReader::AccessDenied;
792
else if ( errmsg == "NT_STATUS_LOGON_FAILURE" )
794
state = Smb4KPasswdReader::LogonFailure;
797
if ( ((Smb4KCore *)parent())->passwdReader()->askpass( m_workgroup, m_host, QString::null, state )
799
m_queue.enqueue( new QString( QString( "%1:%2:%3:%4" ).arg( Shares ).arg( m_workgroup ).arg( m_host ).arg( m_ip ) ) );
804
emit error( ERROR_GETTING_SHARES, list.grep( "NT_STATUS" ).first().stripWhiteSpace() );
807
else if ( list.grep( "tree connect failed:" ).count() != 0 )
809
if ( ((Smb4KCore *)parent())->passwdReader()->askpass( m_workgroup, m_host, QString::null, Smb4KPasswdReader::AccessDenied ) )
811
m_queue.enqueue( new QString( QString( "%1:%2:%3:%4" ).arg( Shares ).arg( m_workgroup ).arg( m_host ).arg( m_ip ) ) );
816
emit error( ERROR_GETTING_SHARES, list.grep( "Connection to" ).first().stripWhiteSpace() );
819
else if ( list.grep( "Error returning browse list:" ).count() != 0 && list.grep( "NT_STATUS" ).count() == 0 )
821
emit error( ERROR_GETTING_SHARES, list.grep( "Error returning browse list:" ).first().stripWhiteSpace() );
827
// Process the share listing.
828
QValueList<Smb4KShareItem *> sharesList;
832
for ( ; index < list.count(); index++ )
834
line = list[index].stripWhiteSpace();
836
QString name, type, comment;
838
if ( line.contains( "Disk" ) )
840
name = line.section( "Disk", 0, 0 ).stripWhiteSpace();
842
comment = line.section( "Disk", 1, 1 ).stripWhiteSpace();
844
else if ( line.contains( "Printer" ) )
846
name = line.section( "Printer", 0, 0 ).stripWhiteSpace();
848
comment = line.section( "Printer", 1, 1 ).stripWhiteSpace();
850
else if ( line.contains( " IPC " ) )
852
name = line.section( " IPC ", 0, 0 ).stripWhiteSpace();
854
comment = line.section( " IPC ", 1, 1 ).stripWhiteSpace();
856
else if ( line.startsWith( "Anonymous" ) )
860
else if ( line.startsWith( "Server" ) )
865
sharesList.append( new Smb4KShareItem( m_workgroup, m_host, name, type, comment ) );
868
emit shares( sharesList );
873
/****************************************************************************
874
Process the information about a host.
875
****************************************************************************/
877
void Smb4KScanner::processInfo()
879
QStringList list = QStringList::split( '\n', m_buffer, false );
881
Smb4KHostItem *host = new Smb4KHostItem( m_workgroup, m_host );
883
for ( QStringList::ConstIterator it = list.begin(); it != list.end(); ++it )
885
if ( (*it).stripWhiteSpace().startsWith( "Domain" ) || (*it).stripWhiteSpace().startsWith( "OS" ) )
888
host->setOSString( (*it).section( "OS=[", 1, 1 ).section( "]", 0, 0 ).stripWhiteSpace() );
890
// The Server string.
891
host->setServerString( (*it).section( "Server=[", 1, 1 ).section( "]", 0, 0 ).stripWhiteSpace() );
895
else if ( (*it).contains( "Connection to", true ) != 0 )
907
/****************************************************************************
909
****************************************************************************/
911
void Smb4KScanner::processPreview()
913
QStringList list = QStringList::split( '\n', m_buffer, false );
915
QValueList<Smb4KPreviewItem *> preview;
918
if ( list.grep( "NT_STATUS" ).count() != 0 || list.grep( "Connection to" ).count() != 0 )
920
if ( list.grep( "NT_STATUS" ).count() != 0 )
922
QString errmsg = list.grep( "NT_STATUS" ).first().stripWhiteSpace().section( " ", 0, 0 );
924
// The error output of smbclient is a little bit inconsistent:
925
if ( errmsg.contains( "NT_STATUS" ) == 0 )
927
errmsg = list.grep( "NT_STATUS" ).first().stripWhiteSpace().section( " ", -1, -1 );
930
if ( errmsg == "NT_STATUS_ACCESS_DENIED" || errmsg == "NT_STATUS_LOGON_FAILURE" )
932
int state = Smb4KPasswdReader::None;
934
if ( errmsg == "NT_STATUS_ACCESS_DENIED" )
936
state = Smb4KPasswdReader::AccessDenied;
938
else if ( errmsg == "NT_STATUS_LOGON_FAILURE" )
940
state = Smb4KPasswdReader::LogonFailure;
943
if ( ((Smb4KCore *)parent())->passwdReader()->askpass( m_workgroup, m_host, m_share, state ) )
945
QString *input = new QString( QString( "%1:%2:%3:%4:%5:%6" ).arg( Preview ).arg( m_workgroup ).arg( m_host ).arg( m_ip ).arg( m_share ).arg( m_path ) );
946
m_queue.enqueue( input );
951
emit error( ERROR_GETTING_PREVIEW, list.grep( "NT_STATUS" ).first().stripWhiteSpace() );
956
emit error( ERROR_GETTING_PREVIEW, list.grep( "Connection to" ).first().stripWhiteSpace() );
959
else if ( list.grep( "Error returning browse list:" ).count() != 0 && list.grep( "NT_STATUS" ).count() == 0 )
961
emit error( ERROR_GETTING_PREVIEW, list.grep( "Error returning browse list:" ).first().stripWhiteSpace() );
965
for ( QStringList::ConstIterator it = list.begin(); it != list.end(); ++it )
967
if ( (*it).stripWhiteSpace().startsWith( "Domain" ) ||
968
(*it).stripWhiteSpace().startsWith( "OS" ) ||
969
(*it).stripWhiteSpace().startsWith( "Anonymous" ) )
973
else if ( (*it).contains( "blocks of size" ) != 0 )
979
QString tmp = (*it).stripWhiteSpace().section( " ", 0, -9 ).stripWhiteSpace();
981
QString item = tmp.section( " ", 0, -2 ).stripWhiteSpace();
985
if ( !item.isEmpty() && tmp.section( " ", -1, -1 ).contains( "D" ) != 0 )
987
else if ( item.isEmpty() )
990
Smb4KPreviewItem *i = new Smb4KPreviewItem( m_host, m_share, m_path, item, isFile );
997
emit previewResult( preview );
1001
/****************************************************************************
1002
Process the search data.
1003
****************************************************************************/
1005
void Smb4KScanner::processSearch()
1007
m_config->setGroup( "Browse Options" );
1008
QString search_method = m_config->readEntry( "Network Search", "nmblookup" );
1010
QStringList data = QStringList::split( "\n", m_buffer.stripWhiteSpace(), false );
1012
if ( search_method == "nmblookup" )
1014
if ( !data.isEmpty() )
1016
// The last entry in the list is the workgroup:
1017
QString workgroup = data.last().stripWhiteSpace();
1020
if ( m_host.contains( ".", true ) != 3 )
1022
// The IP address is in the first entry:
1023
ip = data.first().stripWhiteSpace().section( " ", 0, 0 );
1025
host = m_host.upper();
1030
host = data[0].stripWhiteSpace();
1033
emit searchResult( new Smb4KHostItem( workgroup, host, QString::null, ip ) );
1036
emit searchResult( new Smb4KHostItem() );
1040
if ( data.count() > 1 && !data[1].isEmpty() )
1042
if ( m_buffer.contains( QString( "Connection to %1 failed" ).arg( m_host ) ) != 0 )
1044
emit searchResult( new Smb4KHostItem() );
1048
QString workgroup = data.grep( "Domain" ).first().section( "Domain=[", 1, 1 ).section( "]", 0, 0 );
1049
QString ip = data.grep( "Got a positive name query" ).first().section( "(", 1, 1 ).section( ")", 0, 0 ).stripWhiteSpace();
1051
emit searchResult( new Smb4KHostItem( workgroup, m_host.upper(), QString::null, ip ) );
1055
emit searchResult( new Smb4KHostItem() );
1060
/****************************************************************************
1061
Appends an item to the list of workgroups.
1062
****************************************************************************/
1064
void Smb4KScanner::appendWorkgroup( Smb4KWorkgroupItem *item )
1068
if ( getWorkgroup( item->workgroup() ) == 0 )
1070
m_workgroupList.append( item );
1075
/****************************************************************************
1076
Get a workgroup item out of the workgroup list.
1077
****************************************************************************/
1079
Smb4KWorkgroupItem *Smb4KScanner::getWorkgroup( const QString &workgroup )
1081
QValueListIterator<Smb4KWorkgroupItem *> it;
1083
for ( it = m_workgroupList.begin(); it != m_workgroupList.end(); ++it )
1085
if ( (*it)->workgroup() == workgroup )
1091
return it == m_workgroupList.end() ? 0 : *it;
1095
/****************************************************************************
1096
Returns the options for nmblookup
1097
****************************************************************************/
1099
const QString Smb4KScanner::getNmblookupOptions()
1103
m_config->setGroup( "Samba" );
1104
if ( !m_config->readEntry( "NetBIOS Name", QString::null ).isEmpty() )
1105
options.append( QString( " -n \"%1\"" ).arg( m_config->readEntry( "NetBIOS Name", QString::null ) ) );
1107
if ( !m_config->readEntry( "NetBIOS Scope", QString::null ).isEmpty() )
1108
options.append( QString( " -i \"%1\"" ).arg( m_config->readEntry( "NetBIOS Scope", QString::null ) ) );
1110
if ( !m_config->readEntry( "Socket Options", QString::null ).isEmpty() )
1111
options.append( QString( " -O \"%1\"" ).arg( m_config->readEntry( "Socket Options", QString::null ) ) );
1113
if ( !m_config->readEntry( "NMB Domain", QString::null ).isEmpty() )
1115
if ( ((Smb4KCore *)parent())->getSambaVersion().startsWith( "3" ) )
1117
options.append( QString( " -W \"%1\"" ).arg( m_config->readEntry( "NMB Domain", QString::null ) ) );
1121
if ( !m_config->readEntry( "NMB Broadcast", QString::null ).isEmpty() )
1122
options.append( QString( " -B \"%1\"" ).arg( m_config->readEntry( "NMB Broadcast", QString::null ) ) );
1124
if ( m_config->readBoolEntry( "NMB Port 137", false ) )
1125
options.append( " -r" );
1131
/****************************************************************************
1132
Returns the options for smbclient
1133
****************************************************************************/
1135
const QString Smb4KScanner::getSmbclientOptions()
1139
m_config->setGroup( "Samba" );
1141
if ( !m_config->readEntry( "Client Resolve Order", QString::null ).isEmpty() )
1142
options.append( QString( " -R \"%1\"" ).arg( m_config->readEntry( "Client Resolve Order", QString::null ) ) );
1144
if ( !m_config->readEntry( "Port", QString::null ).isEmpty() )
1145
options.append( QString( " -p %1" ).arg( m_config->readNumEntry( "Port", 139 ) ) );
1147
if ( !m_config->readEntry( "Client Buffer Size", QString::null ).isEmpty() )
1148
options.append( QString( " -b %1" ).arg( m_config->readNumEntry( "Client Buffer Size", 65520 ) ) );
1150
if ( m_config->readBoolEntry( "Use Kerberos", false ) )
1152
if ( ((Smb4KCore *)parent())->getSambaVersion().startsWith( "3" ) )
1153
options.append( " -k" );
1156
if ( !m_config->readEntry( "NetBIOS Name", QString::null ).isEmpty() )
1157
options.append( QString( " -n \"%1\"" ).arg( m_config->readEntry( "NetBIOS Name", QString::null ) ) );
1159
if ( !m_config->readEntry( "NetBIOS Scope", QString::null ).isEmpty() )
1160
options.append( QString( " -i \"%1\"" ).arg( m_config->readEntry( "NetBIOS Scope", QString::null ) ) );
1162
if ( !m_config->readEntry( "Socket Options", QString::null ).isEmpty() )
1163
options.append( QString( " -O \"%1\"" ).arg( m_config->readEntry( "Socket Options", QString::null ) ) );
1169
/////////////////////////////////////////////////////////////////////////////
1170
// SLOT IMPLEMENTATIONS
1171
/////////////////////////////////////////////////////////////////////////////
1174
/****************************************************************************
1176
****************************************************************************/
1178
void Smb4KScanner::slotReceivedStdout( KProcess *, char *buf, int len )
1180
m_buffer.append( QString::fromLocal8Bit( buf, len ) );
1184
void Smb4KScanner::slotProcessExited( KProcess * )
1190
void Smb4KScanner::slotReceivedStderr( KProcess *, char *buf, int len )
1192
m_buffer.append( QString::fromLocal8Bit( buf, len ) );
1196
void Smb4KScanner::start()
1198
if ( !m_working && !m_queue.isEmpty() )
1200
// Start processing...
1201
QString *item = m_queue.dequeue();
1203
int todo = item->section( ":", 0, 0 ).toInt();
1205
// Tell the program, that the scanner is running again.
1208
QString workgroup, host;
1213
emit running( SCANNER_INIT, m_working );
1217
emit running( SCANNER_OPENING_WORKGROUP, m_working );
1218
m_scanForWorkgroupMembers( item->section( ":", 1, 1 ), item->section( ":", 2, 2 ), item->section( ":", 3, 3 ) );
1221
emit running( SCANNER_OPENING_HOST, m_working );
1222
workgroup = item->section( ":", 1, 1 );
1223
host = item->section( ":", 2, 2 );
1224
m_scanForShares( workgroup, host, item->section( ":", 3, 3) );
1227
emit running( SCANNER_RETRIEVING_INFO, m_working );
1228
workgroup = item->section( ":", 1, 1 );
1229
host = item->section( ":", 2, 2 );
1230
m_scanForInfo( workgroup, host, item->section( ":", 3, 3 ) );
1233
emit running( SCANNER_RETRIEVING_PREVIEW, m_working );
1234
m_previewShare( item->section( ":", 1, 1 ), item->section( ":", 2, 2 ), item->section( ":", 3, 3 ), item->section( ":", 4, 4 ), item->section( ":", 5, 5 ) );
1237
emit running( SCANNER_SEARCHING, m_working );
1238
m_searchForHost( item->section( ":", 1, 1 ) );
1244
// FIXME: The browser window sends todo == Shares and todo == GetInfo
1245
// requests back-on-back. The latter are redundant, because the necessary
1246
// info is also provided by processShares(). In the following we will remove
1248
if ( !m_queue.isEmpty() )
1250
if ( m_queue.head()->contains( QString( "%1" ).arg( Info ) ) && m_queue.head()->contains( workgroup ) && m_queue.head()->contains( host ) )
1261
/****************************************************************************
1263
****************************************************************************/
1265
void Smb4KScanner::slotInit()
1267
QString *input = new QString( QString( "%1:" ).arg( Init ) );
1268
m_queue.enqueue( input );
1272
#include "smb4kscanner.moc"