~ubuntu-branches/ubuntu/vivid/smb4k/vivid

« back to all changes in this revision

Viewing changes to core/smb4kscanner_p.cpp

  • Committer: Package Import Robot
  • Author(s): Fathi Boudra
  • Date: 2012-05-19 18:54:34 UTC
  • mfrom: (1.1.20)
  • Revision ID: package-import@ubuntu.com-20120519185434-duffny2n87214n1n
Tags: 1.0.1-1
* New upstream release.
* Update debian/compat: bump to 9.
* Update debian/control:
  - bump debhelper to 9.
  - bump kdelibs5-dev build dependency to 4:4.4.0.
  - bump Standards-Version to 3.9.3 (no changes needed).
  - Replace smbfs dependency by cifs-utils. (Closes: #638162)
* Update debian/copyright:
  - update upstream URL.
  - update upstream e-mail.
* Update debian/smb4k.lintian-overrides file.
* Update debian/watch file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/***************************************************************************
2
 
    smb4kscanner_p  -  This is a private helper class for Smb4KScanner.
 
2
    smb4kscanner_p  -  Private helper classes for the scanner
3
3
                             -------------------
4
 
    begin                : Do Jul 19 2007
5
 
    copyright            : (C) 2007 by Alexander Reinholdt
6
 
    email                : dustpuppy@users.berlios.de
 
4
    begin                : So Mai 22 2011
 
5
    copyright            : (C) 2011 by Alexander Reinholdt
 
6
    email                : alexander.reinholdt@kdemail.net
7
7
 ***************************************************************************/
8
8
 
9
9
/***************************************************************************
19
19
 *                                                                         *
20
20
 *   You should have received a copy of the GNU General Public License     *
21
21
 *   along with this program; if not, write to the                         *
22
 
 *   Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,   *
23
 
 *   MA  02111-1307 USA                                                    *
 
22
 *   Free Software Foundation, 51 Franklin Street, Suite 500, Boston,      *
 
23
 *   MA 02110-1335, USA                                                    *
24
24
 ***************************************************************************/
25
25
 
 
26
// Qt includes
 
27
#include <QTimer>
 
28
#include <QDebug>
 
29
#include <QHostAddress>
 
30
#include <QAbstractSocket>
 
31
 
 
32
// KDE includes
 
33
#include <kstandarddirs.h>
 
34
#include <kshell.h>
 
35
 
26
36
// application specific includes
27
 
#include "smb4kscanner_p.h"
 
37
#include <smb4kscanner_p.h>
 
38
#include <smb4ksettings.h>
 
39
#include <smb4knotification.h>
 
40
#include <smb4kglobal.h>
 
41
#include <smb4kcustomoptionsmanager.h>
 
42
#include <smb4kcustomoptions.h>
 
43
#include <smb4kworkgroup.h>
 
44
#include <smb4khost.h>
 
45
#include <smb4kshare.h>
 
46
#include <smb4kwalletmanager.h>
 
47
 
 
48
using namespace Smb4KGlobal;
 
49
 
 
50
 
 
51
Smb4KLookupDomainsJob::Smb4KLookupDomainsJob( QObject *parent ) : KJob( parent ),
 
52
  m_started( false ), m_parent_widget( NULL ), m_proc( NULL )
 
53
{
 
54
}
 
55
 
 
56
 
 
57
Smb4KLookupDomainsJob::~Smb4KLookupDomainsJob()
 
58
{
 
59
}
 
60
 
 
61
 
 
62
void Smb4KLookupDomainsJob::start()
 
63
{
 
64
  m_started = true;
 
65
  QTimer::singleShot( 0, this, SLOT( slotStartLookup() ) );
 
66
}
 
67
 
 
68
 
 
69
void Smb4KLookupDomainsJob::setupLookup( QWidget *parent )
 
70
{
 
71
  m_parent_widget = parent;
 
72
}
 
73
 
 
74
 
 
75
bool Smb4KLookupDomainsJob::doKill()
 
76
{
 
77
  if ( m_proc && (m_proc->state() == KProcess::Running || m_proc->state() == KProcess::Starting) )
 
78
  {
 
79
    m_proc->abort();
 
80
  }
 
81
  else
 
82
  {
 
83
    // Do nothing
 
84
  }
 
85
 
 
86
  return KJob::doKill();
 
87
}
 
88
 
 
89
 
 
90
void Smb4KLookupDomainsJob::processWorkgroups()
 
91
{
 
92
  QStringList stdout = QString::fromUtf8( m_proc->readAllStandardOutput(), -1 ).split( "\n", QString::SkipEmptyParts );
 
93
 
 
94
  if ( !stdout.isEmpty() )
 
95
  {
 
96
    Smb4KWorkgroup workgroup;
 
97
 
 
98
    foreach ( const QString &line, stdout )
 
99
    {
 
100
      if ( line.startsWith( "Looking up status of" ) )
 
101
      {
 
102
        // Get the IP address of the master browser.
 
103
        workgroup.setMasterBrowserIP( line.section( "of", 1, 1 ).trimmed() );
 
104
        continue;
 
105
      }
 
106
      else if ( line.contains( "MAC Address", Qt::CaseSensitive ) )
 
107
      {
 
108
        // Add workgroup to the list. Ignore misconfigured master browsers,
 
109
        // that do not belong to a workgroup/domain, i.e. where the workgroup
 
110
        // name is empty.
 
111
        if ( !workgroup.workgroupName().isEmpty() && !workgroup.masterBrowserName().isEmpty() )
 
112
        {
 
113
          m_workgroups_list << workgroup;
 
114
        }
 
115
        else
 
116
        {
 
117
          // Do nothing
 
118
        }
 
119
 
 
120
        workgroup = Smb4KWorkgroup();
 
121
        continue;
 
122
      }
 
123
      else if ( line.contains( " <00> ", Qt::CaseSensitive ) )
 
124
      {
 
125
        // Set the name of the workgroup/host.
 
126
        if ( line.contains( " <GROUP> ", Qt::CaseSensitive ) )
 
127
        {
 
128
          // Avoid setting the workgroup name twice.
 
129
          if ( workgroup.workgroupName().isEmpty() )
 
130
          {
 
131
            workgroup.setWorkgroupName( line.section( "<00>", 0, 0 ).trimmed() );
 
132
          }
 
133
          else
 
134
          {
 
135
            // Do nothing
 
136
          }
 
137
        }
 
138
        else
 
139
        {
 
140
          // Avoid setting the name of the master browser twice.
 
141
          if ( workgroup.masterBrowserName().isEmpty() )
 
142
          {
 
143
            workgroup.setMasterBrowserName( line.section( "<00>", 0, 0 ).trimmed() );
 
144
          }
 
145
          else
 
146
          {
 
147
            // Do nothing
 
148
          }
 
149
        }
 
150
 
 
151
        continue;
 
152
      }
 
153
      else if ( line.contains( " <1d> ", Qt::CaseSensitive ) )
 
154
      {
 
155
        // Get the workgroup name.
 
156
        if ( workgroup.workgroupName().isEmpty() )
 
157
        {
 
158
          workgroup.setWorkgroupName( line.section( "<1d>", 0, 0 ).trimmed() );
 
159
        }
 
160
        else
 
161
        {
 
162
          // Do nothing
 
163
        }
 
164
 
 
165
        continue;
 
166
      }
 
167
      else if ( line.contains( "__MSBROWSE__", Qt::CaseSensitive ) &&
 
168
                line.contains( " <01> ", Qt::CaseSensitive ) )
 
169
      {
 
170
        // The host is a master browser.
 
171
        workgroup.setHasPseudoMasterBrowser( false );
 
172
        continue;
 
173
      }
 
174
      else
 
175
      {
 
176
        continue;
 
177
      }
 
178
    }
 
179
  }
 
180
  else
 
181
  {
 
182
    // Do nothing
 
183
  }
 
184
 
 
185
  emit workgroups( m_workgroups_list );
 
186
}
 
187
 
 
188
 
 
189
void Smb4KLookupDomainsJob::slotStartLookup()
 
190
{
 
191
  // Find nmblookup program.
 
192
  QString nmblookup = KStandardDirs::findExe( "nmblookup" );
 
193
 
 
194
  if ( nmblookup.isEmpty() )
 
195
  {
 
196
    Smb4KNotification *notification = new Smb4KNotification();
 
197
    notification->commandNotFound( "nmblookup" );
 
198
    emitResult();
 
199
    return;
 
200
  }
 
201
  else
 
202
  {
 
203
    // Do nothing
 
204
  }
 
205
 
 
206
  // Find grep program.
 
207
  QString grep = KStandardDirs::findExe( "grep" );
 
208
 
 
209
  if ( grep.isEmpty() )
 
210
  {
 
211
    Smb4KNotification *notification = new Smb4KNotification();
 
212
    notification->commandNotFound( "grep" );
 
213
    emitResult();
 
214
    return;
 
215
  }
 
216
  else
 
217
  {
 
218
    // Do nothing
 
219
  }
 
220
 
 
221
  // Find awk program
 
222
  QString awk = KStandardDirs::findExe( "awk" );
 
223
 
 
224
  if ( awk.isEmpty() )
 
225
  {
 
226
    Smb4KNotification *notification = new Smb4KNotification();
 
227
    notification->commandNotFound( "awk" );
 
228
    emitResult();
 
229
    return;
 
230
  }
 
231
  else
 
232
  {
 
233
    // Go ahead
 
234
  }
 
235
 
 
236
  // Find xargs program
 
237
  QString xargs = KStandardDirs::findExe( "xargs" );
 
238
 
 
239
  if ( xargs.isEmpty() )
 
240
  {
 
241
    Smb4KNotification *notification = new Smb4KNotification();
 
242
    notification->commandNotFound( "xargs" );
 
243
    emitResult();
 
244
    return;
 
245
  }
 
246
  else
 
247
  {
 
248
    // Go ahead
 
249
  }
 
250
 
 
251
  // Global Samba options
 
252
  QMap<QString,QString> samba_options = globalSambaOptions();
 
253
 
 
254
  // Compile command
 
255
  QStringList arguments;
 
256
 
 
257
  // nmblookup
 
258
  arguments << nmblookup;
 
259
 
 
260
  // Domain
 
261
  if ( !Smb4KSettings::domainName().isEmpty() &&
 
262
       QString::compare( Smb4KSettings::domainName(), samba_options["workgroup"] ) != 0 )
 
263
  {
 
264
    arguments << QString( "-W %1" ).arg( KShell::quoteArg( Smb4KSettings::domainName() ) );
 
265
  }
 
266
  else
 
267
  {
 
268
    // Do nothing
 
269
  }
 
270
 
 
271
  // NetBIOS name
 
272
  if ( !Smb4KSettings::netBIOSName().isEmpty() &&
 
273
       QString::compare( Smb4KSettings::netBIOSName(), samba_options["netbios name"] ) != 0 )
 
274
  {
 
275
    arguments << QString( "-n %1" ).arg( KShell::quoteArg( Smb4KSettings::netBIOSName() ) );
 
276
  }
 
277
  else
 
278
  {
 
279
    // Do nothing
 
280
  }
 
281
 
 
282
  // NetBIOS scope
 
283
  if ( !Smb4KSettings::netBIOSScope().isEmpty() &&
 
284
       QString::compare( Smb4KSettings::netBIOSScope(), samba_options["netbios scope"] ) != 0 )
 
285
  {
 
286
    arguments << QString( "-i %1" ).arg( KShell::quoteArg( Smb4KSettings::netBIOSScope() ) );
 
287
  }
 
288
  else
 
289
  {
 
290
    // Do nothing
 
291
  }
 
292
 
 
293
  // Socket options
 
294
  if ( !Smb4KSettings::socketOptions().isEmpty() &&
 
295
       QString::compare( Smb4KSettings::socketOptions(), samba_options["socket options"] ) != 0 )
 
296
  {
 
297
    arguments << QString( "-O %1" ).arg( KShell::quoteArg( Smb4KSettings::socketOptions() ) );
 
298
  }
 
299
  else
 
300
  {
 
301
    // Do nothing
 
302
  }
 
303
 
 
304
  // Port 137
 
305
  if ( Smb4KSettings::usePort137() )
 
306
  {
 
307
    arguments << "-r";
 
308
  }
 
309
  else
 
310
  {
 
311
    // Do nothing
 
312
  }
 
313
 
 
314
  // Broadcast address
 
315
  QHostAddress address( Smb4KSettings::broadcastAddress() );
 
316
 
 
317
  if ( !Smb4KSettings::broadcastAddress().isEmpty() &&
 
318
       address.protocol() != QAbstractSocket::UnknownNetworkLayerProtocol )
 
319
  {
 
320
    arguments << QString( "-B %1" ).arg( Smb4KSettings::broadcastAddress() );
 
321
  }
 
322
  else
 
323
  {
 
324
    // Do nothing
 
325
  }
 
326
 
 
327
  arguments << "-M";
 
328
  arguments << "--";
 
329
  arguments << "-";
 
330
  arguments << "|";
 
331
  arguments << grep;
 
332
  arguments << "'<01>'";
 
333
  arguments << "|";
 
334
  arguments << awk;
 
335
  arguments << "'{print $1}'";
 
336
  arguments << "|";
 
337
 
 
338
  if ( !winsServer().isEmpty() )
 
339
  {
 
340
    arguments << xargs;
 
341
    arguments << "-Iips";
 
342
    arguments << nmblookup;
 
343
    arguments << "-R";
 
344
    arguments << QString( "-U %1" ).arg( KShell::quoteArg( winsServer() ) );
 
345
    arguments << "-A ips";
 
346
  }
 
347
  else
 
348
  {
 
349
    arguments << xargs;
 
350
    arguments << "-Iips";
 
351
    arguments << nmblookup;
 
352
    arguments << "-A ips";
 
353
  }
 
354
 
 
355
  // Domain
 
356
  if ( !Smb4KSettings::domainName().isEmpty() &&
 
357
       QString::compare( Smb4KSettings::domainName(), samba_options["workgroup"] ) != 0 )
 
358
  {
 
359
    arguments << QString( "-W %1" ).arg( KShell::quoteArg( Smb4KSettings::domainName() ) );
 
360
  }
 
361
  else
 
362
  {
 
363
    // Do nothing
 
364
  }
 
365
 
 
366
  // NetBIOS name
 
367
  if ( !Smb4KSettings::netBIOSName().isEmpty() &&
 
368
       QString::compare( Smb4KSettings::netBIOSName(), samba_options["netbios name"] ) != 0 )
 
369
  {
 
370
    arguments << QString( "-n %1" ).arg( KShell::quoteArg( Smb4KSettings::netBIOSName() ) );
 
371
  }
 
372
  else
 
373
  {
 
374
    // Do nothing
 
375
  }
 
376
 
 
377
  // NetBIOS scope
 
378
  if ( !Smb4KSettings::netBIOSScope().isEmpty() &&
 
379
       QString::compare( Smb4KSettings::netBIOSScope(), samba_options["netbios scope"] ) != 0 )
 
380
  {
 
381
    arguments << QString( "-i %1" ).arg( KShell::quoteArg( Smb4KSettings::netBIOSScope() ) );
 
382
  }
 
383
  else
 
384
  {
 
385
    // Do nothing
 
386
  }
 
387
 
 
388
  // Socket options
 
389
  if ( !Smb4KSettings::socketOptions().isEmpty() &&
 
390
       QString::compare( Smb4KSettings::socketOptions(), samba_options["socket options"] ) != 0 )
 
391
  {
 
392
    arguments << QString( "-O %1" ).arg( KShell::quoteArg( Smb4KSettings::socketOptions() ) );
 
393
  }
 
394
  else
 
395
  {
 
396
    // Do nothing
 
397
  }
 
398
 
 
399
  // Port 137
 
400
  if ( Smb4KSettings::usePort137() )
 
401
  {
 
402
    arguments << "-r";
 
403
  }
 
404
  else
 
405
  {
 
406
    // Do nothing
 
407
  }
 
408
 
 
409
  m_proc = new Smb4KProcess( this );
 
410
  m_proc->setOutputChannelMode( KProcess::SeparateChannels );
 
411
  m_proc->setShellCommand( arguments.join( " " ) );
 
412
 
 
413
  connect( m_proc, SIGNAL( readyReadStandardError() ), this, SLOT( slotReadStandardError() ) );
 
414
  connect( m_proc, SIGNAL( finished( int, QProcess::ExitStatus ) ), this, SLOT( slotProcessFinished( int, QProcess::ExitStatus ) ) );
 
415
 
 
416
  emit aboutToStart();
 
417
 
 
418
  m_proc->start();  
 
419
}
 
420
 
 
421
 
 
422
void Smb4KLookupDomainsJob::slotReadStandardError()
 
423
{
 
424
  // Read from stderr and decide what to do.
 
425
  QString stderr = QString::fromUtf8( m_proc->readAllStandardError(), -1 ).trimmed();
 
426
 
 
427
  if ( !stderr.isEmpty() )
 
428
  {
 
429
    Smb4KNotification *notification = new Smb4KNotification();
 
430
    notification->retrievingDomainsFailed( stderr );
 
431
  }
 
432
  else
 
433
  {
 
434
    // Do nothing
 
435
  }
 
436
}
 
437
 
 
438
 
 
439
void Smb4KLookupDomainsJob::slotProcessFinished( int /*exitCode*/, QProcess::ExitStatus exitStatus )
 
440
{
 
441
  switch ( exitStatus )
 
442
  {
 
443
    case QProcess::CrashExit:
 
444
    {
 
445
      if ( !m_proc->isAborted() )
 
446
      {
 
447
        Smb4KNotification *notification = new Smb4KNotification();
 
448
        notification->processError( m_proc->error() );
 
449
      }
 
450
      else
 
451
      {
 
452
        // Do nothing
 
453
      }
 
454
      break;
 
455
    }
 
456
    default:
 
457
    {
 
458
      processWorkgroups();
 
459
      break;
 
460
    }
 
461
  }
 
462
 
 
463
  emitResult();
 
464
  emit finished();
 
465
}
 
466
 
 
467
 
 
468
 
 
469
Smb4KQueryMasterJob::Smb4KQueryMasterJob( QObject *parent ) : KJob( parent ),
 
470
  m_started( false ), m_parent_widget( NULL ), m_proc( NULL )
 
471
{
 
472
}
 
473
 
 
474
 
 
475
Smb4KQueryMasterJob::~Smb4KQueryMasterJob()
 
476
{
 
477
}
 
478
 
 
479
 
 
480
void Smb4KQueryMasterJob::start()
 
481
{
 
482
  m_started = true;
 
483
  QTimer::singleShot( 0, this, SLOT( slotStartLookup() ) );
 
484
}
 
485
 
 
486
 
 
487
void Smb4KQueryMasterJob::setupLookup( const QString &master, QWidget *parent )
 
488
{
 
489
  m_master_browser = master;
 
490
  m_parent_widget = parent;
 
491
}
 
492
 
 
493
 
 
494
bool Smb4KQueryMasterJob::doKill()
 
495
{
 
496
  if ( m_proc && (m_proc->state() == KProcess::Running || m_proc->state() == KProcess::Starting) )
 
497
  {
 
498
    m_proc->abort();
 
499
  }
 
500
  else
 
501
  {
 
502
    // Do nothing
 
503
  }
 
504
  
 
505
  return KJob::doKill();
 
506
}
 
507
 
 
508
 
 
509
void Smb4KQueryMasterJob::processWorkgroups()
 
510
{
 
511
  QStringList stdout = QString::fromUtf8( m_proc->readAllStandardOutput(), -1 ).split( "\n", QString::SkipEmptyParts );
 
512
 
 
513
  if ( !stdout.isEmpty() )
 
514
  {
 
515
    Smb4KWorkgroup workgroup;
 
516
 
 
517
    foreach ( const QString &line, stdout )
 
518
    {
 
519
      if ( line.trimmed().startsWith( "Enumerating" ) )
 
520
      {
 
521
        continue;
 
522
      }
 
523
      else if ( line.trimmed().startsWith( "Domain name" ) )
 
524
      {
 
525
        continue;
 
526
      }
 
527
      else if ( line.trimmed().startsWith( "-------------" ) )
 
528
      {
 
529
        continue;
 
530
      }
 
531
      else if ( line.trimmed().isEmpty() )
 
532
      {
 
533
        continue;
 
534
      }
 
535
      else
 
536
      {
 
537
        // This is the workgroup and master entry. Process it.
 
538
        workgroup.setWorkgroupName( line.section( "   ", 0, 0 ).trimmed() );
 
539
        workgroup.setMasterBrowserName( line.section( "   ", 1, -1 ).trimmed() );
 
540
        workgroup.setHasPseudoMasterBrowser( false );
 
541
 
 
542
        m_workgroups_list << workgroup;
 
543
 
 
544
        workgroup = Smb4KWorkgroup();
 
545
        continue;
 
546
      }
 
547
    }
 
548
  }
 
549
  else
 
550
  {
 
551
    // Do nothing
 
552
  }
 
553
 
 
554
  emit workgroups( m_workgroups_list );
 
555
}
 
556
 
 
557
 
 
558
void Smb4KQueryMasterJob::slotStartLookup()
 
559
{
 
560
  // Find net program
 
561
  QString net = KStandardDirs::findExe( "net" );
 
562
 
 
563
  if ( net.isEmpty() )
 
564
  {
 
565
    Smb4KNotification *notification = new Smb4KNotification();
 
566
    notification->commandNotFound( "net" );
 
567
    emitResult();
 
568
    return;
 
569
  }
 
570
  else
 
571
  {
 
572
    // Do nothing
 
573
  }
 
574
 
 
575
  // Find xargs program
 
576
  QString xargs = KStandardDirs::findExe( "xargs" );
 
577
 
 
578
  if ( xargs.isEmpty() )
 
579
  {
 
580
    Smb4KNotification *notification = new Smb4KNotification();
 
581
    notification->commandNotFound( "xargs" );
 
582
    emitResult();
 
583
    return;
 
584
  }
 
585
  else
 
586
  {
 
587
    // Do nothing
 
588
  }
 
589
 
 
590
  // Global Samba options
 
591
  QMap<QString,QString> samba_options = globalSambaOptions();
 
592
 
 
593
  // Workgroup
 
594
  Smb4KWorkgroup workgroup;
 
595
 
 
596
  if ( !Smb4KSettings::domainName().isEmpty() )
 
597
  {
 
598
    workgroup.setWorkgroupName( Smb4KSettings::domainName() );
 
599
  }
 
600
  else
 
601
  {
 
602
    workgroup.setWorkgroupName( samba_options["workgroup"] );
 
603
  }
 
604
 
 
605
  Smb4KCustomOptions *options = NULL;
 
606
  QStringList arguments;
 
607
  Smb4KHost host;
 
608
  
 
609
  if ( !m_master_browser.isEmpty() )
 
610
  {
 
611
    // We do not need to set the domain here, because neither
 
612
    // Smb4KCustomOptionsMangager nor Smb4KWalletManager need 
 
613
    // the domain entry to return correct data.    
 
614
    if ( QHostAddress( m_master_browser ).protocol() == QAbstractSocket::UnknownNetworkLayerProtocol )
 
615
    {
 
616
      host.setHostName( m_master_browser );
 
617
    }
 
618
    else
 
619
    {
 
620
      host.setIP( m_master_browser );
 
621
    }
 
622
 
 
623
    // Acquire the custom options for the master browser
 
624
    options = Smb4KCustomOptionsManager::self()->findOptions( &host );
 
625
 
 
626
    // Get authentication information for the host if needed
 
627
    if ( Smb4KSettings::masterBrowsersRequireAuth() )
 
628
    {
 
629
      Smb4KWalletManager::self()->readAuthInfo( &host );
 
630
    }
 
631
    else
 
632
    {
 
633
      // Do nothing
 
634
    }
 
635
    
 
636
    // Custom master lookup
 
637
    arguments << net;
 
638
    arguments << "lookup";
 
639
    arguments << "host";
 
640
    arguments << KShell::quoteArg( host.hostName() );
 
641
  }
 
642
  else
 
643
  {
 
644
    // Get authentication information for the host if needed
 
645
    if ( Smb4KSettings::masterBrowsersRequireAuth() && Smb4KSettings::useDefaultLogin() )
 
646
    {
 
647
      Smb4KWalletManager::self()->readAuthInfo( &host );
 
648
    }
 
649
    else
 
650
    {
 
651
      // Do nothing
 
652
    }
 
653
    
 
654
    // Master lookup
 
655
    arguments << net;
 
656
    arguments << "lookup";
 
657
    arguments << "master";
 
658
    arguments << KShell::quoteArg( workgroup.workgroupName() );
 
659
  }
 
660
 
 
661
  // The user's workgroup/domain name
 
662
  if ( !Smb4KSettings::domainName().isEmpty() &&
 
663
       QString::compare( Smb4KSettings::domainName(), samba_options["workgroup"] ) != 0 )
 
664
  {
 
665
    arguments << QString( "-W %1" ).arg( KShell::quoteArg( Smb4KSettings::domainName() ) );
 
666
  }
 
667
  else
 
668
  {
 
669
    // Do nothing
 
670
  }
 
671
 
 
672
  // The user's NetBIOS name
 
673
  if ( !Smb4KSettings::netBIOSName().isEmpty() &&
 
674
       QString::compare( Smb4KSettings::netBIOSName(), samba_options["netbios name"] ) != 0 )
 
675
  {
 
676
    arguments << QString( "-n %1" ).arg( KShell::quoteArg( Smb4KSettings::netBIOSName() ) );
 
677
  }
 
678
  else
 
679
  {
 
680
    // Do nothing
 
681
  }
 
682
 
 
683
  // Machine account
 
684
  if ( Smb4KSettings::machineAccount() )
 
685
  {
 
686
    arguments << "-P";
 
687
  }
 
688
  else
 
689
  {
 
690
    // Do nothing
 
691
  }
 
692
 
 
693
  // Port
 
694
  if ( options && options->smbPort() != Smb4KSettings::remoteSMBPort() )
 
695
  {
 
696
    arguments << QString( "-p %1" ).arg( options->smbPort() );
 
697
  }
 
698
  else
 
699
  {
 
700
    arguments << QString( "-p %1" ).arg( Smb4KSettings::remoteSMBPort() );
 
701
  }
 
702
 
 
703
  // User name and password if needed
 
704
  if ( Smb4KSettings::masterBrowsersRequireAuth() && !host.login().isEmpty() )
 
705
  {
 
706
    arguments << QString( "-U %1%" ).arg( host.login() );
 
707
  }
 
708
  else
 
709
  {
 
710
    // Do *not* change this to "-U guest%". This won't work.
 
711
    arguments << "-U %";
 
712
  }
 
713
  
 
714
  arguments << "|";
 
715
  
 
716
  // Domain lookup (via xargs)
 
717
  arguments << xargs;
 
718
  arguments << "-Iip";
 
719
  arguments << net;
 
720
 
 
721
  // Protocol & command. Since the domain lookup only works with the RAP
 
722
  // protocol, there is no point in using the 'Automatic' feature.
 
723
  arguments << "rap";
 
724
  arguments << "domain";
 
725
 
 
726
  // The user's workgroup/domain name
 
727
  if ( !Smb4KSettings::domainName().isEmpty() &&
 
728
       QString::compare( Smb4KSettings::domainName(), samba_options["workgroup"] ) != 0 )
 
729
  {
 
730
    arguments << QString( "-W %1" ).arg( KShell::quoteArg( Smb4KSettings::domainName() ) );
 
731
  }
 
732
  else
 
733
  {
 
734
    // Do nothing
 
735
  }
 
736
 
 
737
  // The user's NetBIOS name
 
738
  if ( !Smb4KSettings::netBIOSName().isEmpty() &&
 
739
       QString::compare( Smb4KSettings::netBIOSName(), samba_options["netbios name"] ) != 0 )
 
740
  {
 
741
    arguments << QString( "-n %1" ).arg( KShell::quoteArg( Smb4KSettings::netBIOSName() ) );
 
742
  }
 
743
  else
 
744
  {
 
745
    // Do nothing
 
746
  }
 
747
 
 
748
  // Machine account
 
749
  if ( Smb4KSettings::machineAccount() )
 
750
  {
 
751
    arguments << "-P";
 
752
  }
 
753
  else
 
754
  {
 
755
    // Do nothing
 
756
  }
 
757
 
 
758
  // Port
 
759
  if ( options && options->smbPort() != Smb4KSettings::remoteSMBPort() )
 
760
  {
 
761
    arguments << QString( "-p %1" ).arg( options->smbPort() );
 
762
  }
 
763
  else
 
764
  {
 
765
    arguments << QString( "-p %1" ).arg( Smb4KSettings::remoteSMBPort() );
 
766
  }
 
767
 
 
768
  // User name and password if needed
 
769
  if ( Smb4KSettings::masterBrowsersRequireAuth() && !host.login().isEmpty() )
 
770
  {
 
771
    arguments << QString( "-U %1%" ).arg( host.login() );
 
772
  }
 
773
  else
 
774
  {
 
775
    // Do *not* change this to "-U guest%". This won't work.
 
776
    arguments << "-U %";
 
777
  }
 
778
 
 
779
  // IP address (discovered by by previous net command)
 
780
  arguments << "-I ip";
 
781
 
 
782
  // Server name if available
 
783
  if ( !m_master_browser.isEmpty() )
 
784
  {
 
785
    arguments << QString( "-S %1" ).arg( KShell::quoteArg( m_master_browser ) );
 
786
  }
 
787
  else
 
788
  {
 
789
    // Do nothing
 
790
  }
 
791
 
 
792
  // Add debug level to get the IP of the master browser that we 
 
793
  // are connecting to.
 
794
  arguments << "-d3";
 
795
 
 
796
  m_proc = new Smb4KProcess( this );
 
797
  m_proc->setOutputChannelMode( KProcess::SeparateChannels );
 
798
  m_proc->setShellCommand( arguments.join( " " ) );
 
799
 
 
800
  if ( Smb4KSettings::masterBrowsersRequireAuth() && !host.password().isEmpty() )
 
801
  {
 
802
    m_proc->setEnv( "PASSWD", host.password(), true );
 
803
  }
 
804
  else
 
805
  {
 
806
    m_proc->unsetEnv( "PASSWD" );
 
807
  }
 
808
 
 
809
  connect( m_proc, SIGNAL( readyReadStandardError() ), this, SLOT( slotReadStandardError() ) );
 
810
  connect( m_proc, SIGNAL( finished( int, QProcess::ExitStatus ) ), this, SLOT( slotProcessFinished( int, QProcess::ExitStatus ) ) );
 
811
 
 
812
  emit aboutToStart();
 
813
 
 
814
  m_proc->start();  
 
815
}
 
816
 
 
817
 
 
818
void Smb4KQueryMasterJob::slotReadStandardError()
 
819
{
 
820
  // Read from stderr and decide what to do.
 
821
  QString stderr = QString::fromUtf8( m_proc->readAllStandardError(), -1 ).trimmed();
 
822
 
 
823
  if ( !stderr.isEmpty() )
 
824
  {
 
825
    if ( stderr.contains( "The username or password was not correct." ) ||
 
826
         stderr.contains( "NT_STATUS_ACCOUNT_DISABLED" ) /* AD error */ ||
 
827
         stderr.contains( "NT_STATUS_ACCESS_DENIED" ) ||
 
828
         stderr.contains( "NT_STATUS_LOGON_FAILURE" ) )
 
829
    {
 
830
      if ( m_master_browser.isEmpty() )
 
831
      {
 
832
        // Figure out the current master browser's IP address.
 
833
        QStringList stderr_list = stderr.split( "\n", QString::SkipEmptyParts );
 
834
 
 
835
        foreach ( const QString &line, stderr_list )
 
836
        {
 
837
          if ( line.contains( "Connecting to host=" ) )
 
838
          {
 
839
            m_master_browser = line.section( "=", 1, 1 ).trimmed();
 
840
            break;
 
841
          }
 
842
          else
 
843
          {
 
844
            continue;
 
845
          }
 
846
        }
 
847
      }
 
848
      else
 
849
      {
 
850
        // Do nothing
 
851
      }
 
852
 
 
853
      emit authError( this );
 
854
    }
 
855
    else
 
856
    {
 
857
      // Avoid reporting the debug output as error.
 
858
      if ( stderr.contains( "NT_STATUS" ) )
 
859
      {
 
860
        Smb4KNotification *notification = new Smb4KNotification();
 
861
        notification->retrievingDomainsFailed( stderr );
 
862
      }
 
863
      else
 
864
      {
 
865
        // Debug output. Do nothing
 
866
      }
 
867
    }
 
868
  }
 
869
  else
 
870
  {
 
871
    // Do nothing
 
872
  }
 
873
}
 
874
 
 
875
 
 
876
void Smb4KQueryMasterJob::slotProcessFinished( int /*exitCode*/, QProcess::ExitStatus exitStatus )
 
877
{
 
878
  switch ( exitStatus )
 
879
  {
 
880
    case QProcess::CrashExit:
 
881
    {
 
882
      if ( !m_proc->isAborted() )
 
883
      {
 
884
        Smb4KNotification *notification = new Smb4KNotification();
 
885
        notification->processError( m_proc->error() );
 
886
      }
 
887
      else
 
888
      {
 
889
        // Do nothing
 
890
      }
 
891
      break;
 
892
    }
 
893
    default:
 
894
    {
 
895
      processWorkgroups();
 
896
      break;
 
897
    }
 
898
  }
 
899
 
 
900
  emitResult();
 
901
  emit finished();
 
902
}
 
903
 
 
904
 
 
905
 
 
906
Smb4KScanBAreasJob::Smb4KScanBAreasJob( QObject *parent ) : KJob( parent ),
 
907
  m_started( false ), m_parent_widget( NULL ), m_proc( NULL )
 
908
{
 
909
}
 
910
 
 
911
 
 
912
Smb4KScanBAreasJob::~Smb4KScanBAreasJob()
 
913
{
 
914
}
 
915
 
 
916
 
 
917
void Smb4KScanBAreasJob::start()
 
918
{
 
919
  m_started = true;
 
920
  QTimer::singleShot( 0, this, SLOT( slotStartScan() ) );
 
921
}
 
922
 
 
923
 
 
924
void Smb4KScanBAreasJob::setupScan( QWidget *parent )
 
925
{
 
926
  m_parent_widget = parent;
 
927
}
 
928
 
 
929
 
 
930
bool Smb4KScanBAreasJob::doKill()
 
931
{
 
932
  if ( m_proc && (m_proc->state() == KProcess::Running || m_proc->state() == KProcess::Starting) )
 
933
  {
 
934
    m_proc->abort();
 
935
  }
 
936
  else
 
937
  {
 
938
    // Do nothing
 
939
  }
 
940
  
 
941
  return KJob::doKill();
 
942
}
 
943
 
 
944
 
 
945
void Smb4KScanBAreasJob::processScan()
 
946
{
 
947
  QStringList stdout = QString::fromUtf8( m_proc->readAllStandardOutput(), -1 ).trimmed().split( "\n", QString::SkipEmptyParts );
 
948
 
 
949
  if ( !stdout.isEmpty() )
 
950
  {
 
951
    Smb4KWorkgroup workgroup;
 
952
    Smb4KHost host;
 
953
    bool skip = false;
 
954
 
 
955
    foreach ( const QString &line, stdout )
 
956
    {
 
957
      // Check if we have to skip this host entry.
 
958
      // A host entry is skipped if the IP address is invalid, i.e.
 
959
      // 0.0.0.0 is returned.
 
960
      if ( line.startsWith( "Looking up status of" ) )
 
961
      {
 
962
        QString ip_address = line.section( "of", 1, 1 ).trimmed();
 
963
        skip = (QString::compare( ip_address, "0.0.0.0" ) == 0);
 
964
      }
 
965
      else
 
966
      {
 
967
        // Do nothing
 
968
      }
 
969
 
 
970
      // Now process the output if everything is OK. Otherwise
 
971
      // skip lines until there is a valid host entry.
 
972
      if ( !skip )
 
973
      {
 
974
        if ( line.startsWith( "Looking up status of" ) )
 
975
        {
 
976
          // Set the IP address of the host.
 
977
          QString ip_address = line.section( "of", 1, 1 ).trimmed();
 
978
 
 
979
          if ( QString::compare( ip_address, "0.0.0.0" ) != 0 )
 
980
          {
 
981
            host.setIP( ip_address );
 
982
          }
 
983
          else
 
984
          {
 
985
            // Do nothing
 
986
          }
 
987
          continue;
 
988
        }
 
989
        else if ( line.contains( "MAC Address", Qt::CaseSensitive ) )
 
990
        {
 
991
          // Check that the workgroup object carries a workgroup
 
992
          // name and a master browser name.
 
993
          if ( !workgroup.workgroupName().isEmpty() && !workgroup.masterBrowserName().isEmpty() )
 
994
          {
 
995
            // Check whether the workgroup has already been entered
 
996
            // into the list.
 
997
            bool workgroup_found = false;
 
998
 
 
999
            for ( int i = 0; i < m_workgroups_list.size(); ++i )
 
1000
            {
 
1001
              if ( QString::compare( m_workgroups_list.at( i ).workgroupName(), workgroup.workgroupName(), Qt::CaseInsensitive ) == 0 )
 
1002
              {
 
1003
                workgroup_found = true;
 
1004
                break;
 
1005
              }
 
1006
              else
 
1007
              {
 
1008
                continue;
 
1009
              }
 
1010
            }
 
1011
 
 
1012
            if ( !workgroup_found )
 
1013
            {
 
1014
              m_workgroups_list << workgroup;
 
1015
            }
 
1016
            else
 
1017
            {
 
1018
              // Do nothing
 
1019
            }
 
1020
          }
 
1021
          else
 
1022
          {
 
1023
            // Do nothing
 
1024
          }
 
1025
 
 
1026
          m_hosts_list << host;
 
1027
 
 
1028
          workgroup = Smb4KWorkgroup();
 
1029
          host = Smb4KHost();
 
1030
          continue;
 
1031
        }
 
1032
        else if ( line.contains( " <00> ", Qt::CaseSensitive ) )
 
1033
        {
 
1034
          // This is the name of the workgroup and/or host. Depending
 
1035
          // on if the <GROUP> label is present, it is either a host name
 
1036
          // or a workgroup name that we process here.
 
1037
          if ( line.contains( " <GROUP> ", Qt::CaseSensitive ) )
 
1038
          {
 
1039
            workgroup.setWorkgroupName( line.section( "<00>", 0, 0 ).trimmed() );
 
1040
            host.setWorkgroupName( line.section( "<00>", 0, 0 ).trimmed() );
 
1041
          }
 
1042
          else
 
1043
          {
 
1044
            host.setHostName( line.section( "<00>", 0, 0 ).trimmed() );
 
1045
          }
 
1046
          continue;
 
1047
        }
 
1048
        else if ( line.contains( "__MSBROWSE__", Qt::CaseSensitive ) )
 
1049
        {
 
1050
          // The __MSBROWSE__ label marks a host that offers a browse list.
 
1051
          // If also the <01> label is present, this is a master browser.
 
1052
          //
 
1053
          // If the host is not a master browser but offers a browse list,
 
1054
          // we use it temporarily as pseudo master browser for the workgroup
 
1055
          // until the right master browser has been set.
 
1056
          if ( line.contains( " <01> ", Qt::CaseSensitive ) )
 
1057
          {
 
1058
            workgroup.setMasterBrowserName( host.hostName() );
 
1059
            workgroup.setMasterBrowserIP( host.ip() );
 
1060
            workgroup.setHasPseudoMasterBrowser( false );
 
1061
            host.setIsMasterBrowser( true );
 
1062
          }
 
1063
          else
 
1064
          {
 
1065
            if ( workgroup.masterBrowserName().isEmpty() )
 
1066
            {
 
1067
              workgroup.setMasterBrowserName( host.hostName() );
 
1068
              workgroup.setMasterBrowserIP( host.ip() );
 
1069
              workgroup.setHasPseudoMasterBrowser( true );
 
1070
            }
 
1071
            else
 
1072
            {
 
1073
              // Do nothing
 
1074
            }
 
1075
          }
 
1076
          continue;
 
1077
        }
 
1078
        else
 
1079
        {
 
1080
          continue;
 
1081
        }
 
1082
      }
 
1083
      else
 
1084
      {
 
1085
        continue;
 
1086
      }
 
1087
    }
 
1088
  }
 
1089
  else
 
1090
  {
 
1091
    // Do nothing
 
1092
  }
 
1093
 
 
1094
  // Emit the list of workgroups and the list of hosts.
 
1095
  emit workgroups( m_workgroups_list );
 
1096
  emit hosts( m_hosts_list );
 
1097
}
 
1098
 
 
1099
 
 
1100
void Smb4KScanBAreasJob::slotStartScan()
 
1101
{
 
1102
  // Find nmblookup program.
 
1103
  QString nmblookup = KStandardDirs::findExe( "nmblookup" );
 
1104
 
 
1105
  if ( nmblookup.isEmpty() )
 
1106
  {
 
1107
    Smb4KNotification *notification = new Smb4KNotification();
 
1108
    notification->commandNotFound( "nmblookup" );
 
1109
    emitResult();
 
1110
    return;
 
1111
  }
 
1112
  else
 
1113
  {
 
1114
    // Do nothing
 
1115
  }
 
1116
 
 
1117
  // Find awk program
 
1118
  QString awk = KStandardDirs::findExe( "awk" );
 
1119
 
 
1120
  if ( awk.isEmpty() )
 
1121
  {
 
1122
    Smb4KNotification *notification = new Smb4KNotification();
 
1123
    notification->commandNotFound( "awk" );
 
1124
    emitResult();
 
1125
    return;
 
1126
  }
 
1127
  else
 
1128
  {
 
1129
    // Do nothing
 
1130
  }
 
1131
 
 
1132
  // Find sed program
 
1133
  QString sed = KStandardDirs::findExe( "sed" );
 
1134
 
 
1135
  if ( sed.isEmpty() )
 
1136
  {
 
1137
    Smb4KNotification *notification = new Smb4KNotification();
 
1138
    notification->commandNotFound( "sed" );
 
1139
    emitResult();
 
1140
    return;
 
1141
  }
 
1142
  else
 
1143
  {
 
1144
    // Do nothing
 
1145
  }
 
1146
 
 
1147
  // Find xargs program
 
1148
  QString xargs = KStandardDirs::findExe( "xargs" );
 
1149
 
 
1150
  if ( xargs.isEmpty() )
 
1151
  {
 
1152
    Smb4KNotification *notification = new Smb4KNotification();
 
1153
    notification->commandNotFound( "xargs" );
 
1154
    emitResult();
 
1155
    return;
 
1156
  }
 
1157
  else
 
1158
  {
 
1159
    // Do nothing
 
1160
  }
 
1161
 
 
1162
  // Global Samba options
 
1163
  QMap<QString,QString> samba_options = globalSambaOptions();
 
1164
 
 
1165
  // Broadcast areas/addresses
 
1166
 
 
1167
  // FIXME: Emit error message if the list is empty!!!
 
1168
  QStringList addresses = Smb4KSettings::broadcastAreas().split( ",", QString::SkipEmptyParts );
 
1169
 
 
1170
  // Assemble the command
 
1171
  QStringList arguments;
 
1172
  
 
1173
  for ( int i = 0; i < addresses.size(); ++i )
 
1174
  {
 
1175
    if ( !arguments.isEmpty() )
 
1176
    {
 
1177
      arguments << ";";
 
1178
    }
 
1179
    else
 
1180
    {
 
1181
      // Do nothing
 
1182
    }
 
1183
    
 
1184
    arguments << nmblookup;
 
1185
 
 
1186
    // Domain
 
1187
    if ( !Smb4KSettings::domainName().isEmpty() &&
 
1188
         QString::compare( Smb4KSettings::domainName(), samba_options["workgroup"] ) != 0 )
 
1189
    {
 
1190
      arguments << QString( "-W %1" ).arg( KShell::quoteArg( Smb4KSettings::domainName() ) );
 
1191
    }
 
1192
    else
 
1193
    {
 
1194
      // Do nothing
 
1195
    }
 
1196
 
 
1197
    // NetBIOS name
 
1198
    if ( !Smb4KSettings::netBIOSName().isEmpty() &&
 
1199
         QString::compare( Smb4KSettings::netBIOSName(), samba_options["netbios name"] ) != 0 )
 
1200
    {
 
1201
      arguments << QString( "-n %1" ).arg( KShell::quoteArg( Smb4KSettings::netBIOSName() ) );
 
1202
    }
 
1203
    else
 
1204
    {
 
1205
      // Do nothing
 
1206
    }
 
1207
 
 
1208
    // NetBIOS scope
 
1209
    if ( !Smb4KSettings::netBIOSScope().isEmpty() &&
 
1210
         QString::compare( Smb4KSettings::netBIOSScope(), samba_options["netbios scope"] ) != 0 )
 
1211
    {
 
1212
      arguments << QString( "-i %1" ).arg( KShell::quoteArg( Smb4KSettings::netBIOSScope() ) );
 
1213
    }
 
1214
    else
 
1215
    {
 
1216
      // Do nothing
 
1217
    }
 
1218
 
 
1219
    // Socket options
 
1220
    if ( !Smb4KSettings::socketOptions().isEmpty() &&
 
1221
         QString::compare( Smb4KSettings::socketOptions(), samba_options["socket options"] ) != 0 )
 
1222
    {
 
1223
      arguments << QString( "-O %1" ).arg( KShell::quoteArg( Smb4KSettings::socketOptions() ) );
 
1224
    }
 
1225
    else
 
1226
    {
 
1227
      // Do nothing
 
1228
    }
 
1229
 
 
1230
    // Port 137
 
1231
    if ( Smb4KSettings::usePort137() )
 
1232
    {
 
1233
      arguments << "-r";
 
1234
    }
 
1235
    else
 
1236
    {
 
1237
      // Do nothing
 
1238
    }
 
1239
 
 
1240
    // We do not want the globally defined broadcast address here, because the
 
1241
    // broadcast address option is needed for the IP scan.
 
1242
 
 
1243
    // FIXME: Emit error message if address is not IP4 or IP6!!!
 
1244
    arguments << QString( "-B %1" ).arg( addresses.at( i ) );
 
1245
    arguments << "--";
 
1246
    arguments << QString( "%1" ).arg( KShell::quoteArg( "*" ) );
 
1247
    arguments << sed;
 
1248
    arguments << "-e /querying/d";
 
1249
    arguments << "|";
 
1250
    arguments << awk;
 
1251
    arguments << QString( "%1" ).arg( KShell::quoteArg( "{print $1}" ) );
 
1252
    arguments << "|";
 
1253
    arguments << xargs;
 
1254
    arguments << "-Iip";
 
1255
    arguments << nmblookup;
 
1256
 
 
1257
    // Domain
 
1258
    if ( !Smb4KSettings::domainName().isEmpty() &&
 
1259
         QString::compare( Smb4KSettings::domainName(), samba_options["workgroup"] ) != 0 )
 
1260
    {
 
1261
      QString( "-W %1" ).arg( KShell::quoteArg( Smb4KSettings::domainName() ) );
 
1262
    }
 
1263
    else
 
1264
    {
 
1265
      // Do nothing
 
1266
    }
 
1267
 
 
1268
    // NetBIOS name
 
1269
    if ( !Smb4KSettings::netBIOSName().isEmpty() &&
 
1270
         QString::compare( Smb4KSettings::netBIOSName(), samba_options["netbios name"] ) != 0 )
 
1271
    {
 
1272
      arguments << QString( "-n %1" ).arg( KShell::quoteArg( Smb4KSettings::netBIOSName() ) );
 
1273
    }
 
1274
    else
 
1275
    {
 
1276
      // Do nothing
 
1277
    }
 
1278
 
 
1279
    // NetBIOS scope
 
1280
    if ( !Smb4KSettings::netBIOSScope().isEmpty() &&
 
1281
         QString::compare( Smb4KSettings::netBIOSScope(), samba_options["netbios scope"] ) != 0 )
 
1282
    {
 
1283
      arguments << QString( "-i %1" ).arg( KShell::quoteArg( Smb4KSettings::netBIOSScope() ) );
 
1284
    }
 
1285
    else
 
1286
    {
 
1287
      // Do nothing
 
1288
    }
 
1289
 
 
1290
    // Socket options
 
1291
    if ( !Smb4KSettings::socketOptions().isEmpty() &&
 
1292
         QString::compare( Smb4KSettings::socketOptions(), samba_options["socket options"] ) != 0 )
 
1293
    {
 
1294
      arguments << QString( "-O %1" ).arg( KShell::quoteArg( Smb4KSettings::socketOptions() ) );
 
1295
    }
 
1296
    else
 
1297
    {
 
1298
      // Do nothing
 
1299
    }
 
1300
 
 
1301
    // Port 137
 
1302
    if ( Smb4KSettings::usePort137() )
 
1303
    {
 
1304
      arguments << "-r";
 
1305
    }
 
1306
    else
 
1307
    {
 
1308
      // Do nothing
 
1309
    }
 
1310
 
 
1311
    // Broadcast address
 
1312
    // Note: This time we want to have the global one!
 
1313
    if ( !Smb4KSettings::broadcastAddress().isEmpty() &&
 
1314
         QHostAddress( Smb4KSettings::broadcastAddress() ).protocol() != QAbstractSocket::UnknownNetworkLayerProtocol )
 
1315
    {
 
1316
      arguments << QString( "-B %1" ).arg( Smb4KSettings::broadcastAddress() );
 
1317
    }
 
1318
    else
 
1319
    {
 
1320
      // Do nothing
 
1321
    }
 
1322
 
 
1323
    // Include the WINS server:
 
1324
    if ( !winsServer().isEmpty() )
 
1325
    {
 
1326
      arguments << "-R";
 
1327
      arguments << QString( "-U %1" ).arg( winsServer() );
 
1328
    }
 
1329
    else
 
1330
    {
 
1331
      // Do nothing
 
1332
    }
 
1333
      
 
1334
    arguments << "-A ip";
 
1335
  }
 
1336
 
 
1337
  m_proc = new Smb4KProcess( this );
 
1338
  m_proc->setOutputChannelMode( KProcess::SeparateChannels );
 
1339
  m_proc->setShellCommand( arguments.join( " " ) );
 
1340
 
 
1341
  connect( m_proc, SIGNAL( readyReadStandardError() ), SLOT( slotReadStandardError() ) );
 
1342
  connect( m_proc, SIGNAL( finished( int, QProcess::ExitStatus ) ), SLOT( slotProcessFinished( int, QProcess::ExitStatus ) ) );
 
1343
 
 
1344
  emit aboutToStart();
 
1345
 
 
1346
  m_proc->start(); 
 
1347
}
 
1348
 
 
1349
 
 
1350
void Smb4KScanBAreasJob::slotReadStandardError()
 
1351
{
 
1352
  QString stderr = QString::fromUtf8( m_proc->readAllStandardError(), -1 ).trimmed();
 
1353
 
 
1354
  if ( !stderr.isEmpty() )
 
1355
  {
 
1356
    Smb4KNotification *notification = new Smb4KNotification();
 
1357
    notification->scanningBroadcastAreaFailed( stderr );
 
1358
  }
 
1359
  else
 
1360
  {
 
1361
    // Do nothing
 
1362
  }
 
1363
}
 
1364
 
 
1365
 
 
1366
void Smb4KScanBAreasJob::slotProcessFinished( int /*exitCode*/, QProcess::ExitStatus exitStatus )
 
1367
{
 
1368
  switch ( exitStatus )
 
1369
  {
 
1370
    case QProcess::CrashExit:
 
1371
    {
 
1372
      if ( !m_proc->isAborted() )
 
1373
      {
 
1374
        Smb4KNotification *notification = new Smb4KNotification();
 
1375
        notification->processError( m_proc->error() );
 
1376
      }
 
1377
      else
 
1378
      {
 
1379
        // Do nothing
 
1380
      }
 
1381
      break;
 
1382
    }
 
1383
    default:
 
1384
    {
 
1385
      processScan();
 
1386
      break;
 
1387
    }
 
1388
  }
 
1389
 
 
1390
  emitResult();
 
1391
  emit finished();
 
1392
}
 
1393
 
 
1394
 
 
1395
 
 
1396
Smb4KLookupDomainMembersJob::Smb4KLookupDomainMembersJob( QObject *parent ) : KJob( parent ),
 
1397
  m_started( false ), m_parent_widget( NULL ), m_proc( NULL )
 
1398
{
 
1399
}
 
1400
 
 
1401
 
 
1402
Smb4KLookupDomainMembersJob::~Smb4KLookupDomainMembersJob()
 
1403
{
 
1404
}
 
1405
 
 
1406
 
 
1407
void Smb4KLookupDomainMembersJob::start()
 
1408
{
 
1409
  m_started = true;
 
1410
  QTimer::singleShot( 0, this, SLOT( slotStartLookup() ) );
 
1411
}
 
1412
 
 
1413
 
 
1414
void Smb4KLookupDomainMembersJob::setupLookup( Smb4KWorkgroup *workgroup, QWidget *parent )
 
1415
{
 
1416
  Q_ASSERT( workgroup );
 
1417
  m_workgroup = *workgroup;
 
1418
  m_parent_widget = parent;
 
1419
}
 
1420
 
 
1421
 
 
1422
bool Smb4KLookupDomainMembersJob::doKill()
 
1423
{
 
1424
  if ( m_proc && (m_proc->state() == KProcess::Running || m_proc->state() == KProcess::Starting) )
 
1425
  {
 
1426
    m_proc->abort();
 
1427
  }
 
1428
  else
 
1429
  {
 
1430
    // Do nothing
 
1431
  }
 
1432
 
 
1433
  return KJob::doKill();
 
1434
}
 
1435
 
 
1436
 
 
1437
void Smb4KLookupDomainMembersJob::processHosts()
 
1438
{
 
1439
  QStringList stdout = QString::fromUtf8( m_proc->readAllStandardOutput(), -1 ).split( "\n", QString::SkipEmptyParts );
 
1440
 
 
1441
  if ( !stdout.isEmpty() )
 
1442
  {
 
1443
    Smb4KHost host;
 
1444
 
 
1445
    foreach ( const QString &line, stdout )
 
1446
    {
 
1447
      if ( line.trimmed().startsWith( "Enumerating" ) )
 
1448
      {
 
1449
        continue;
 
1450
      }
 
1451
      else if ( line.trimmed().startsWith( "Server name" ) )
 
1452
      {
 
1453
        continue;
 
1454
      }
 
1455
      else if ( line.trimmed().startsWith( "-------------" ) )
 
1456
      {
 
1457
        continue;
 
1458
      }
 
1459
      else
 
1460
      {
 
1461
        // Omit host names that contain spaces since QUrl cannot handle them.
 
1462
        // And, they are wrong, anyway.
 
1463
        if ( !line.section( "   ", 0, 0 ).trimmed().contains( " " ) )
 
1464
        {
 
1465
          host.setHostName( line.section( "   ", 0, 0 ).trimmed() );
 
1466
          host.setWorkgroupName( m_workgroup.workgroupName() );
 
1467
          host.setComment( line.section( "   ", 1, -1 ).trimmed() );
 
1468
          
 
1469
          if ( QString::compare( host.hostName(), m_workgroup.masterBrowserName() ) == 0 )
 
1470
          {
 
1471
            host.setLogin( m_master_browser.login() );
 
1472
            host.setPassword( m_master_browser.password() );
 
1473
            host.setIsMasterBrowser( true );
 
1474
 
 
1475
            if ( m_workgroup.hasMasterBrowserIP() )
 
1476
            {
 
1477
              host.setIP( m_workgroup.masterBrowserIP() );
 
1478
            }
 
1479
            else
 
1480
            {
 
1481
              // Do nothing
 
1482
            }
 
1483
          }
 
1484
          else
 
1485
          {
 
1486
            host.setIsMasterBrowser( false );
 
1487
          }
 
1488
          
 
1489
          m_hosts_list << host;
 
1490
        }
 
1491
        else
 
1492
        {
 
1493
          qDebug() << "This host name contains a space. I cannot handle this...";
 
1494
        }
 
1495
        
 
1496
        host = Smb4KHost();
 
1497
        continue;
 
1498
      }
 
1499
    }
 
1500
  }
 
1501
  else
 
1502
  {
 
1503
    // Do nothing
 
1504
  }
 
1505
 
 
1506
  emit hosts( &m_workgroup, m_hosts_list );
 
1507
}
 
1508
 
 
1509
 
 
1510
void Smb4KLookupDomainMembersJob::slotStartLookup()
 
1511
{
 
1512
  // Find net program
 
1513
  QString net = KStandardDirs::findExe( "net" );
 
1514
 
 
1515
  if ( net.isEmpty() )
 
1516
  {
 
1517
    Smb4KNotification *notification = new Smb4KNotification();
 
1518
    notification->commandNotFound( "net" );
 
1519
    emitResult();
 
1520
    return;
 
1521
  }
 
1522
  else
 
1523
  {
 
1524
    // Go ahead
 
1525
  }
 
1526
 
 
1527
  // Get the master browser of the defined workgroup, so that we
 
1528
  // can connect to it.
 
1529
  Smb4KHost *host = findHost( m_workgroup.masterBrowserName(), m_workgroup.workgroupName() );
 
1530
 
 
1531
  if ( host )
 
1532
  {
 
1533
    // Copy host entry to private variable.
 
1534
    m_master_browser = *host;
 
1535
    
 
1536
    // If the master browsers need authentication, we read it now.
 
1537
    if ( Smb4KSettings::masterBrowsersRequireAuth() )
 
1538
    {
 
1539
      Smb4KWalletManager::self()->readAuthInfo( &m_master_browser );
 
1540
    }
 
1541
    else
 
1542
    {
 
1543
      // Do nothing
 
1544
    }
 
1545
 
 
1546
    // Global Samba options
 
1547
    QMap<QString,QString> samba_options = globalSambaOptions();
 
1548
 
 
1549
    // Custom options
 
1550
    Smb4KCustomOptions *options = Smb4KCustomOptionsManager::self()->findOptions( &m_master_browser );
 
1551
 
 
1552
    // Assemble the command.
 
1553
    QStringList arguments;
 
1554
 
 
1555
    // net command
 
1556
    arguments << net;
 
1557
 
 
1558
    // Protocol & command. Since the domain member lookup only works with
 
1559
    // the RAP protocol, there is no point in using the 'Automatic' feature.
 
1560
    arguments << "rap";
 
1561
    arguments << "server";
 
1562
    arguments << "domain";
 
1563
 
 
1564
    // The user's domain or workgroup
 
1565
    if ( !Smb4KSettings::domainName().isEmpty() &&
 
1566
         QString::compare( Smb4KSettings::domainName(), samba_options["workgroup"] ) != 0 )
 
1567
    {
 
1568
      arguments << QString( "-W %1" ).arg( KShell::quoteArg( Smb4KSettings::domainName() ) );
 
1569
    }
 
1570
    else
 
1571
    {
 
1572
      // Do nothing
 
1573
    }
 
1574
 
 
1575
    // The NetBIOS name of the user's machine
 
1576
    if ( !Smb4KSettings::netBIOSName().isEmpty() &&
 
1577
         QString::compare( Smb4KSettings::netBIOSName(), samba_options["netbios name"] ) != 0 )
 
1578
    {
 
1579
      arguments << QString( "-n %1" ).arg( KShell::quoteArg( Smb4KSettings::netBIOSName() ) );
 
1580
    }
 
1581
    else
 
1582
    {
 
1583
      // Do nothing
 
1584
    }
 
1585
 
 
1586
    // Machine account
 
1587
    if ( Smb4KSettings::machineAccount() )
 
1588
    {
 
1589
      arguments << "-P";
 
1590
    }
 
1591
    else
 
1592
    {
 
1593
      // Do nothing
 
1594
    }
 
1595
 
 
1596
    // Remote SMB port
 
1597
    if ( options && options->smbPort() != Smb4KSettings::remoteSMBPort() )
 
1598
    {
 
1599
      arguments << QString( "-p %1" ).arg( options->smbPort() );
 
1600
    }
 
1601
    else
 
1602
    {
 
1603
      arguments << QString( "-p %1" ).arg( Smb4KSettings::remoteSMBPort() );
 
1604
    }
 
1605
 
 
1606
    // IP address of the master browser
 
1607
    if ( m_workgroup.hasMasterBrowserIP() )
 
1608
    {
 
1609
      arguments << QString( "-I %1" ).arg( m_workgroup.masterBrowserIP() );
 
1610
    }
 
1611
    else
 
1612
    {
 
1613
      // Do nothing
 
1614
    }
 
1615
 
 
1616
    // Workgroup of the remote master browser that is to be 
 
1617
    // queried for the workgroup/domain members.
 
1618
    arguments << QString( "-w %1" ).arg( KShell::quoteArg( m_workgroup.workgroupName() ) );
 
1619
 
 
1620
    // Name of the remote master browser
 
1621
    arguments << QString( "-S %1" ).arg( KShell::quoteArg( m_workgroup.masterBrowserName() ) );
 
1622
 
 
1623
    // Authentication, if needed
 
1624
    if ( Smb4KSettings::masterBrowsersRequireAuth() && !m_master_browser.login().isEmpty() )
 
1625
    {
 
1626
      arguments << QString( "-U %1" ).arg( m_master_browser.login() );
 
1627
    }
 
1628
    else
 
1629
    {
 
1630
      // Do *not* change this to "-U guest%". This won't work.
 
1631
      arguments << "-U %";
 
1632
    }
 
1633
 
 
1634
    m_proc = new Smb4KProcess( this );
 
1635
    m_proc->setShellCommand( arguments.join( " " ) );
 
1636
    m_proc->setOutputChannelMode( KProcess::SeparateChannels );
 
1637
 
 
1638
    if ( Smb4KSettings::self()->masterBrowsersRequireAuth() && !m_master_browser.password().isEmpty() )
 
1639
    {
 
1640
      m_proc->setEnv( "PASSWD", m_master_browser.password(), true );
 
1641
    }
 
1642
    else
 
1643
    {
 
1644
      m_proc->unsetEnv( "PASSWD" );
 
1645
    }
 
1646
  
 
1647
    connect( m_proc, SIGNAL( readyReadStandardError() ), SLOT( slotReadStandardError() ) );
 
1648
    connect( m_proc, SIGNAL( finished( int, QProcess::ExitStatus ) ), SLOT( slotProcessFinished( int, QProcess::ExitStatus ) ) );
 
1649
 
 
1650
    emit aboutToStart( &m_workgroup );
 
1651
    
 
1652
    m_proc->start();
 
1653
  }
 
1654
  else
 
1655
  {
 
1656
    // The master browser could not be determined. End the
 
1657
    // job here and emit an empty hosts list.
 
1658
    emit hosts( &m_workgroup, m_hosts_list );
 
1659
    emitResult();
 
1660
  }
 
1661
}
 
1662
 
 
1663
 
 
1664
void Smb4KLookupDomainMembersJob::slotReadStandardError()
 
1665
{
 
1666
  QString stderr = QString::fromUtf8( m_proc->readAllStandardError(), -1 ).trimmed();
 
1667
 
 
1668
  if ( !stderr.isEmpty() )
 
1669
  {
 
1670
    if ( stderr.contains( "The username or password was not correct." ) ||
 
1671
         stderr.contains( "NT_STATUS_ACCOUNT_DISABLED" ) /* AD error */ ||
 
1672
         stderr.contains( "NT_STATUS_ACCESS_DENIED" ) ||
 
1673
         stderr.contains( "NT_STATUS_LOGON_FAILURE" ) )
 
1674
    {
 
1675
      emit authError( this );
 
1676
    }
 
1677
    else if ( stderr.contains( "tdb_transaction_recover:" ) )
 
1678
    {
 
1679
      // Suppress debug output/information sent to stderr
 
1680
      qDebug() << stderr;
 
1681
    }
 
1682
    else
 
1683
    {
 
1684
      // Notify the user that an error occurred.
 
1685
      Smb4KNotification *notification = new Smb4KNotification();
 
1686
      notification->retrievingServersFailed( &m_workgroup, stderr );
 
1687
    }
 
1688
  }
 
1689
  else
 
1690
  {
 
1691
    // Do nothing
 
1692
  }
 
1693
}
 
1694
 
 
1695
 
 
1696
void Smb4KLookupDomainMembersJob::slotProcessFinished( int /*exitCode*/, QProcess::ExitStatus exitStatus )
 
1697
{
 
1698
  switch ( exitStatus )
 
1699
  {
 
1700
    case QProcess::CrashExit:
 
1701
    {
 
1702
      if ( !m_proc->isAborted() )
 
1703
      {
 
1704
        Smb4KNotification *notification = new Smb4KNotification();
 
1705
        notification->processError( m_proc->error() );
 
1706
      }
 
1707
      else
 
1708
      {
 
1709
        // Do nothing
 
1710
      }
 
1711
      break;
 
1712
    }
 
1713
    default:
 
1714
    {
 
1715
      processHosts();
 
1716
      break;
 
1717
    }
 
1718
  }
 
1719
 
 
1720
  emitResult();
 
1721
  emit finished( &m_workgroup );
 
1722
}
 
1723
 
 
1724
 
 
1725
 
 
1726
Smb4KLookupSharesJob::Smb4KLookupSharesJob( QObject *parent ) : KJob( parent ),
 
1727
  m_started( false ), m_parent_widget( NULL ), m_proc( NULL )
 
1728
{
 
1729
}
 
1730
 
 
1731
 
 
1732
Smb4KLookupSharesJob::~Smb4KLookupSharesJob()
 
1733
{
 
1734
}
 
1735
 
 
1736
 
 
1737
void Smb4KLookupSharesJob::start()
 
1738
{
 
1739
  m_started = true;
 
1740
  QTimer::singleShot( 0, this, SLOT( slotStartLookup() ) );
 
1741
}
 
1742
 
 
1743
 
 
1744
void Smb4KLookupSharesJob::setupLookup( Smb4KHost *host, QWidget *parent )
 
1745
{
 
1746
  Q_ASSERT( host );
 
1747
  m_host = *host;
 
1748
  m_parent_widget = parent;
 
1749
}
 
1750
 
 
1751
 
 
1752
bool Smb4KLookupSharesJob::doKill()
 
1753
{
 
1754
  if ( m_proc && (m_proc->state() == KProcess::Running || m_proc->state() == KProcess::Starting) )
 
1755
  {
 
1756
    m_proc->abort();
 
1757
  }
 
1758
  else
 
1759
  {
 
1760
    // Do nothing
 
1761
  }
 
1762
  
 
1763
  return KJob::doKill();
 
1764
}
 
1765
 
 
1766
 
 
1767
void Smb4KLookupSharesJob::processShares()
 
1768
{
 
1769
  // Additional authentication error handling.
 
1770
  if ( m_proc->exitCode() == 104 /* access denied in W2k3 domain */ ||
 
1771
       m_proc->exitCode() == 235 /* wrong password in W2k3 domain */ )
 
1772
  {
 
1773
    emit authError( this );
 
1774
    // We can just return here, because this function is invoked
 
1775
    // in slotProcessFinished() and emitResult() will be emitted 
 
1776
    // at its end.
 
1777
    return;
 
1778
  }
 
1779
  else
 
1780
  {
 
1781
    // Do nothing
 
1782
  }
 
1783
 
 
1784
  QStringList stdout = QString::fromUtf8( m_proc->readAllStandardOutput(), -1 ).split( "\n", QString::SkipEmptyParts );
 
1785
  
 
1786
  if ( !stdout.isEmpty() )
 
1787
  {
 
1788
    Smb4KShare share;
 
1789
    
 
1790
    foreach ( const QString &line, stdout )
 
1791
    {
 
1792
      if ( line.trimmed().startsWith( "Enumerating" ) )
 
1793
      {
 
1794
        continue;
 
1795
      }
 
1796
      else if ( line.trimmed().startsWith( "Share name" ) )
 
1797
      {
 
1798
        continue;
 
1799
      }
 
1800
      else if ( line.trimmed().startsWith( "----------" ) )
 
1801
      {
 
1802
        continue;
 
1803
      }
 
1804
      else if ( line.contains( " Disk     ", Qt::CaseSensitive ) /* line has comment */ ||
 
1805
                (!line.contains( " Disk     ", Qt::CaseSensitive ) &&
 
1806
                line.trimmed().endsWith( " Disk", Qt::CaseSensitive ) /* line has no comment */) )
 
1807
      {
 
1808
        if ( !line.trimmed().endsWith( " Disk", Qt::CaseSensitive ) )
 
1809
        {
 
1810
          share.setShareName( line.section( " Disk     ", 0, 0 ).trimmed() );
 
1811
          share.setComment( line.section( " Disk     ", 1, 1 ).trimmed() );
 
1812
        }
 
1813
        else
 
1814
        {
 
1815
          share.setShareName( line.section( " Disk", 0, 0 ).trimmed() );
 
1816
          share.setComment( "" );
 
1817
        }
 
1818
            
 
1819
        share.setHostName( m_host.hostName() );
 
1820
        share.setWorkgroupName( m_host.workgroupName() );
 
1821
        share.setTypeString( "Disk" );
 
1822
        share.setLogin( m_host.login() );
 
1823
        share.setPassword( m_host.password() );
 
1824
 
 
1825
        if ( m_host.hasIP() )
 
1826
        {
 
1827
          share.setHostIP( m_host.ip() );
 
1828
        }
 
1829
        else
 
1830
        {
 
1831
          // Do nothing
 
1832
        }
 
1833
 
 
1834
        m_shares_list << share;
 
1835
        share = Smb4KShare();
 
1836
        continue;
 
1837
      }
 
1838
      else if ( line.contains( " IPC      ", Qt::CaseSensitive ) /* line has comment */ ||
 
1839
                (!line.contains( " IPC      ", Qt::CaseSensitive ) &&
 
1840
                line.trimmed().endsWith( " IPC", Qt::CaseSensitive ) /* line has no comment */) )
 
1841
      {
 
1842
        if ( !line.trimmed().endsWith( " IPC", Qt::CaseSensitive ) )
 
1843
        {
 
1844
          share.setShareName( line.section( " IPC      ", 0, 0 ).trimmed() );
 
1845
          share.setComment( line.section( " IPC      ", 1, 1 ).trimmed() );
 
1846
        }
 
1847
        else
 
1848
        {
 
1849
          share.setShareName( line.section( " IPC", 0, 0 ).trimmed() );
 
1850
          share.setComment( "" );
 
1851
        }
 
1852
            
 
1853
        share.setHostName( m_host.hostName() );
 
1854
        share.setWorkgroupName( m_host.workgroupName() );
 
1855
        share.setTypeString( "IPC" );
 
1856
        share.setLogin( m_host.login() );
 
1857
        share.setPassword( m_host.password() );
 
1858
 
 
1859
        if ( m_host.hasIP() )
 
1860
        {
 
1861
          share.setHostIP( m_host.ip() );
 
1862
        }
 
1863
        else
 
1864
        {
 
1865
          // Do nothing
 
1866
        }
 
1867
 
 
1868
        m_shares_list << share;
 
1869
        share = Smb4KShare();
 
1870
        continue;
 
1871
      }
 
1872
      else if ( line.contains( " Print    ", Qt::CaseSensitive ) /* line has comment */ ||
 
1873
                (!line.contains( " Print    ", Qt::CaseSensitive ) &&
 
1874
                line.trimmed().endsWith( " Print", Qt::CaseSensitive ) /* line has no comment */) )
 
1875
      {
 
1876
        if ( !line.trimmed().endsWith( " Print", Qt::CaseSensitive ) )
 
1877
        {
 
1878
          share.setShareName( line.section( " Print    ", 0, 0 ).trimmed() );
 
1879
          share.setComment( line.section( " Print    ", 1, 1 ).trimmed() );
 
1880
        }
 
1881
        else
 
1882
        {
 
1883
          share.setShareName( line.section( " Print", 0, 0 ).trimmed() );
 
1884
          share.setComment( "" );
 
1885
        }
 
1886
            
 
1887
        share.setHostName( m_host.hostName() );
 
1888
        share.setWorkgroupName( m_host.workgroupName() );
 
1889
        share.setTypeString( "Printer" );
 
1890
        share.setLogin( m_host.login() );
 
1891
        share.setPassword( m_host.password() );
 
1892
 
 
1893
        if ( m_host.hasIP() )
 
1894
        {
 
1895
          share.setHostIP( m_host.ip() );
 
1896
        }
 
1897
        else
 
1898
        {
 
1899
          // Do nothing
 
1900
        }
 
1901
 
 
1902
        m_shares_list << share;
 
1903
        share = Smb4KShare();
 
1904
        continue;
 
1905
      }
 
1906
      else
 
1907
      {
 
1908
        continue;
 
1909
      }
 
1910
    }
 
1911
  }
 
1912
  else
 
1913
  {
 
1914
    // Do nothing
 
1915
  }
 
1916
  
 
1917
  emit shares( &m_host, m_shares_list );
 
1918
}
 
1919
 
 
1920
 
 
1921
void Smb4KLookupSharesJob::slotStartLookup()
 
1922
{
 
1923
  // Find net program
 
1924
  QString net = KStandardDirs::findExe( "net" );
 
1925
 
 
1926
  if ( net.isEmpty() )
 
1927
  {
 
1928
    Smb4KNotification *notification = new Smb4KNotification();
 
1929
    notification->commandNotFound( "net" );
 
1930
    emitResult();
 
1931
    return;
 
1932
  }
 
1933
  else
 
1934
  {
 
1935
    // Go ahead
 
1936
  }
 
1937
  
 
1938
  // Authentication information.
 
1939
  Smb4KWalletManager::self()->readAuthInfo( &m_host );
 
1940
  
 
1941
  // Global Samba and custom options
 
1942
  QMap<QString,QString> samba_options = globalSambaOptions();
 
1943
  Smb4KCustomOptions *options = Smb4KCustomOptionsManager::self()->findOptions( &m_host );
 
1944
  
 
1945
  // Assemble the command.
 
1946
  QStringList arguments;
 
1947
  
 
1948
  // net program
 
1949
  arguments << net;
 
1950
  
 
1951
  // Protocol hint & command.
 
1952
  if ( options && options->protocolHint() != Smb4KCustomOptions::UndefinedProtocolHint )
 
1953
  {
 
1954
    switch ( options->protocolHint() )
 
1955
    {
 
1956
      case Smb4KCustomOptions::RAP:
 
1957
      {
 
1958
        arguments << "rap";
 
1959
        arguments << "share";
 
1960
        break;
 
1961
      }
 
1962
      default:
 
1963
      {
 
1964
        // Since version 1.0.0 we use the RPC protocol as a default
 
1965
        // when querying a server for its shares (ignoring the 'Automatic
 
1966
        // detection' choice of the user. Because this way we can avoid 
 
1967
        // the 'Invalid command: net rap share list' error, that occurred 
 
1968
        // under certain circumstances in earlier versions.
 
1969
        arguments << "rpc";
 
1970
        arguments << "share";
 
1971
        arguments << "list";
 
1972
        break;
 
1973
      }
 
1974
    }
 
1975
  }
 
1976
  else
 
1977
  {
 
1978
    switch ( Smb4KSettings::protocolHint() )
 
1979
    {
 
1980
      case Smb4KSettings::EnumProtocolHint::RAP:
 
1981
      {
 
1982
        arguments << "rap";
 
1983
        arguments << "share";
 
1984
        break;
 
1985
      }
 
1986
      default:
 
1987
      {
 
1988
        // Since version 1.0.0 we use the RPC protocol as a default
 
1989
        // when querying a server for its shares (ignoring the 'Automatic
 
1990
        // detection' choice of the user. Because this way we can avoid 
 
1991
        // the 'Invalid command: net rap share list' error, that occurred 
 
1992
        // under certain circumstances in earlier versions.
 
1993
        arguments << "rpc";
 
1994
        arguments << "share";
 
1995
        arguments << "list";
 
1996
        break;
 
1997
      }
 
1998
    }
 
1999
  }
 
2000
  
 
2001
  // Long output. We need this, because we want to know the type and
 
2002
  // the comment, too.
 
2003
  arguments << "-l";
 
2004
  
 
2005
  // The user's domain or workgroup
 
2006
  if ( !Smb4KSettings::domainName().isEmpty() &&
 
2007
       QString::compare( Smb4KSettings::domainName(), samba_options["workgroup"] ) != 0 )
 
2008
  {
 
2009
    arguments << QString( "-W %1" ).arg( KShell::quoteArg( Smb4KSettings::domainName() ) );
 
2010
  }
 
2011
  else
 
2012
  {
 
2013
    // Do nothing
 
2014
  }
 
2015
               
 
2016
  // The user's NetBIOS name
 
2017
  if ( !Smb4KSettings::netBIOSName().isEmpty() &&
 
2018
       QString::compare( Smb4KSettings::netBIOSName(), samba_options["netbios name"] ) != 0 )
 
2019
  {
 
2020
    arguments << QString( "-n %1" ).arg( KShell::quoteArg( Smb4KSettings::netBIOSName() ) );
 
2021
  }
 
2022
  else
 
2023
  {
 
2024
    // Do nothing
 
2025
  }
 
2026
  
 
2027
  // Machine account
 
2028
  if ( Smb4KSettings::machineAccount() )
 
2029
  {
 
2030
    arguments << "-P";
 
2031
  }
 
2032
  else
 
2033
  {
 
2034
    // Do nothing
 
2035
  }
 
2036
  
 
2037
  // Port
 
2038
  // If a port was defined for the host via Smb4KHost::port(), it will 
 
2039
  // overwrite the other options.
 
2040
  if ( m_host.port() != -1 )
 
2041
  {
 
2042
    arguments << QString( "-p %1" ).arg( m_host.port() );
 
2043
  }
 
2044
  else
 
2045
  {
 
2046
    if ( options && options->smbPort() != Smb4KSettings::remoteSMBPort() )
 
2047
    {
 
2048
      arguments << QString( "-p %1" ).arg( options->smbPort() );
 
2049
    }
 
2050
    else
 
2051
    {
 
2052
      arguments << QString( "-p %1" ).arg( Smb4KSettings::remoteSMBPort() );
 
2053
    }
 
2054
  }
 
2055
  
 
2056
  // Remote domain/workgroup name
 
2057
  arguments << QString( "-w %1" ).arg( KShell::quoteArg( m_host.workgroupName() ) );
 
2058
  
 
2059
  // Remote host name
 
2060
  arguments << QString( "-S %1" ).arg( KShell::quoteArg( m_host.hostName() ) );
 
2061
 
 
2062
  // IP address
 
2063
  if ( m_host.hasIP() )
 
2064
  {
 
2065
    arguments << QString( "-I %1" ).arg( m_host.ip() );
 
2066
  }
 
2067
  else
 
2068
  {
 
2069
    // Do nothing
 
2070
  }
 
2071
  
 
2072
  // Authentication data
 
2073
  if ( !m_host.login().isEmpty() && !m_host.login().isEmpty() )
 
2074
  {
 
2075
    arguments << QString( "-U %1" ).arg( m_host.login() );
 
2076
  }
 
2077
  else
 
2078
  {
 
2079
    // Under some circumstances you need under Windows the 'guest'
 
2080
    // account to be able to retrieve the list of shared resources.
 
2081
    arguments << "-U guest%";
 
2082
  }
 
2083
 
 
2084
 
 
2085
  m_proc = new Smb4KProcess( this );
 
2086
  m_proc->setOutputChannelMode( KProcess::SeparateChannels );
 
2087
  m_proc->setShellCommand( arguments.join( " " ) );
 
2088
  
 
2089
  if ( !m_host.password().isEmpty() )
 
2090
  {
 
2091
    m_proc->setEnv( "PASSWD", m_host.password(), true );
 
2092
  }
 
2093
  else
 
2094
  {
 
2095
    m_proc->unsetEnv( "PASSWD" );
 
2096
  }
 
2097
 
 
2098
  connect( m_proc, SIGNAL( readyReadStandardError() ), this, SLOT( slotReadStandardError() ) );
 
2099
  connect( m_proc, SIGNAL( finished( int, QProcess::ExitStatus ) ), this, SLOT( slotProcessFinished( int, QProcess::ExitStatus ) ) );
 
2100
 
 
2101
  emit aboutToStart( &m_host );
 
2102
 
 
2103
  m_proc->start();
 
2104
}
 
2105
 
 
2106
 
 
2107
void Smb4KLookupSharesJob::slotReadStandardError()
 
2108
{
 
2109
  QString stderr = QString::fromUtf8( m_proc->readAllStandardError(), -1 ).trimmed();
 
2110
 
 
2111
  if ( !stderr.isEmpty() )
 
2112
  {
 
2113
    if ( stderr.contains( "The username or password was not correct." ) ||
 
2114
         stderr.contains( "NT_STATUS_ACCOUNT_DISABLED" ) /* AD error */ ||
 
2115
         stderr.contains( "NT_STATUS_ACCESS_DENIED" ) ||
 
2116
         stderr.contains( "NT_STATUS_LOGON_FAILURE" ) )
 
2117
    {
 
2118
      emit authError( this );
 
2119
    }
 
2120
    else if ( stderr.contains( "could not obtain sid for domain", Qt::CaseSensitive ) )
 
2121
    {
 
2122
      // FIXME
 
2123
      qDebug() << "FIXME: Wrong protocol used for host " << m_host.hostName();
 
2124
    }
 
2125
    else
 
2126
    {
 
2127
      if ( !stderr.contains( "creating lame", Qt::CaseSensitive ) )
 
2128
      {
 
2129
        Smb4KNotification *notification = new Smb4KNotification();
 
2130
        notification->retrievingSharesFailed( &m_host, stderr );
 
2131
      }
 
2132
      else
 
2133
      {
 
2134
        // Do nothing
 
2135
      }
 
2136
    }
 
2137
  }
 
2138
  else
 
2139
  {
 
2140
    // Do nothing
 
2141
  }
 
2142
}
 
2143
 
 
2144
 
 
2145
void Smb4KLookupSharesJob::slotProcessFinished( int /*exitCode*/, QProcess::ExitStatus exitStatus )
 
2146
{
 
2147
  switch ( exitStatus )
 
2148
  {
 
2149
    case QProcess::CrashExit:
 
2150
    {
 
2151
      if ( !m_proc->isAborted() )
 
2152
      {
 
2153
        Smb4KNotification *notification = new Smb4KNotification();
 
2154
        notification->processError( m_proc->error() );
 
2155
      }
 
2156
      else
 
2157
      {
 
2158
        // Do nothing
 
2159
      }
 
2160
      break;
 
2161
    }
 
2162
    default:
 
2163
    {
 
2164
      processShares();
 
2165
      break;
 
2166
    }
 
2167
  }
 
2168
 
 
2169
  emitResult();
 
2170
  emit finished( &m_host );
 
2171
}
 
2172
 
 
2173
 
 
2174
 
 
2175
Smb4KLookupInfoJob::Smb4KLookupInfoJob( QObject *parent ) : KJob( parent ),
 
2176
  m_started( false ), m_parent_widget( NULL ), m_proc( NULL )
 
2177
{
 
2178
}
 
2179
 
 
2180
 
 
2181
Smb4KLookupInfoJob::~Smb4KLookupInfoJob()
 
2182
{
 
2183
}
 
2184
 
 
2185
 
 
2186
void Smb4KLookupInfoJob::start()
 
2187
{
 
2188
  m_started = true;
 
2189
  QTimer::singleShot( 0, this, SLOT( slotStartLookup() ) );
 
2190
}
 
2191
 
 
2192
 
 
2193
void Smb4KLookupInfoJob::setupLookup( Smb4KHost *host, QWidget* parent )
 
2194
{
 
2195
  Q_ASSERT( host );
 
2196
  m_host = *host;
 
2197
  m_parent_widget = parent;
 
2198
}
 
2199
 
 
2200
 
 
2201
bool Smb4KLookupInfoJob::doKill()
 
2202
{
 
2203
  if ( m_proc && (m_proc->state() == KProcess::Running || m_proc->state() == KProcess::Starting) )
 
2204
  {
 
2205
    m_proc->abort();
 
2206
  }
 
2207
  else
 
2208
  {
 
2209
    // Do nothing
 
2210
  }
 
2211
  
 
2212
  return KJob::doKill();
 
2213
}
 
2214
 
 
2215
 
 
2216
void Smb4KLookupInfoJob::processInfo()
 
2217
{
 
2218
  // First evaluate stdout and if we cannot find the appropriate 
 
2219
  // information also evaluate stderr.
 
2220
  QString stdout = QString::fromUtf8( m_proc->readAllStandardOutput(), -1 );
 
2221
  
 
2222
  if ( stdout.contains( "OS=" ) || stdout.contains( "Server=" ) )
 
2223
  {
 
2224
    QStringList stdout_list = stdout.split( "\n", QString::SkipEmptyParts );
 
2225
    
 
2226
    foreach ( const QString &line, stdout_list )
 
2227
    {
 
2228
      if ( line.contains( "OS=" ) || line.contains( "Server=" ) )
 
2229
      {
 
2230
        QString server = line.section( "Server=[", 1, 1 ).section( "]", 0, 0 ).trimmed();
 
2231
        QString os = line.section( "OS=[", 1, 1 ).section( "]", 0, 0 ).trimmed();
 
2232
        m_host.setInfo( server, os );
 
2233
        emit info( &m_host );
 
2234
        break;
 
2235
      }
 
2236
      else
 
2237
      {
 
2238
        continue;
 
2239
      }
 
2240
    }
 
2241
  }
 
2242
  else
 
2243
  {
 
2244
    QString stderr = QString::fromUtf8( m_proc->readAllStandardError(), -1 );
 
2245
    
 
2246
    if ( stderr.contains( "OS=" ) || stderr.contains( "Server=" ) )
 
2247
    {
 
2248
      QStringList stderr_list = stderr.split( "\n", QString::SkipEmptyParts );
 
2249
      
 
2250
      foreach ( const QString &line, stderr_list )
 
2251
      {
 
2252
        if ( line.contains( "OS=" ) || line.contains( "Server=" ) )
 
2253
        {
 
2254
          QString server = line.section( "Server=[", 1, 1 ).section( "]", 0, 0 ).trimmed();
 
2255
          QString os = line.section( "OS=[", 1, 1 ).section( "]", 0, 0 ).trimmed();
 
2256
          m_host.setInfo( server, os );
 
2257
          emit info( &m_host );
 
2258
          break;
 
2259
        }
 
2260
        else
 
2261
        {
 
2262
          continue;
 
2263
        }
 
2264
      }
 
2265
    }
 
2266
  }
 
2267
}
 
2268
 
 
2269
 
 
2270
void Smb4KLookupInfoJob::slotStartLookup()
 
2271
{
 
2272
  // Find the smbclient program
 
2273
  QString smbclient = KStandardDirs::findExe( "smbclient" );
 
2274
 
 
2275
  if ( smbclient.isEmpty() )
 
2276
  {
 
2277
    Smb4KNotification *notification = new Smb4KNotification();
 
2278
    notification->commandNotFound( "smbclient" );
 
2279
    emitResult();
 
2280
    return;
 
2281
  }
 
2282
  else
 
2283
  {
 
2284
    // Go ahead
 
2285
  }
 
2286
 
 
2287
  // Compile the command.
 
2288
  QStringList arguments;
 
2289
  arguments << smbclient;
 
2290
  arguments << "-d1";
 
2291
  arguments << "-N";
 
2292
  arguments << QString( "-W %1" ).arg( KShell::quoteArg( m_host.workgroupName() ) );
 
2293
  arguments << QString( "-L %1" ).arg( KShell::quoteArg( m_host.hostName() ) );
 
2294
 
 
2295
  if ( m_host.hasIP() )
 
2296
  {
 
2297
    arguments << QString( "-I %1" ).arg( m_host.ip() );
 
2298
  }
 
2299
  else
 
2300
  {
 
2301
    // Do nothing
 
2302
  }
 
2303
 
 
2304
  // Machine account
 
2305
  if ( Smb4KSettings::machineAccount() )
 
2306
  {
 
2307
    arguments << "-P";
 
2308
  }
 
2309
  else
 
2310
  {
 
2311
    // Do nothing
 
2312
  }
 
2313
  
 
2314
  // Signing state
 
2315
  switch ( Smb4KSettings::signingState() )
 
2316
  {
 
2317
    case Smb4KSettings::EnumSigningState::None:
 
2318
    {
 
2319
      break;
 
2320
    }
 
2321
    case Smb4KSettings::EnumSigningState::On:
 
2322
    {
 
2323
      arguments << "-S on";
 
2324
      break;
 
2325
    }
 
2326
    case Smb4KSettings::EnumSigningState::Off:
 
2327
    {
 
2328
      arguments << "-S off";
 
2329
      break;
 
2330
    }
 
2331
    case Smb4KSettings::EnumSigningState::Required:
 
2332
    {
 
2333
      arguments << "-S required";
 
2334
      break;
 
2335
    }
 
2336
    default:
 
2337
    {
 
2338
      break;
 
2339
    }
 
2340
  }
 
2341
  
 
2342
  // Buffer size
 
2343
  if ( Smb4KSettings::bufferSize() != 65520 )
 
2344
  {
 
2345
    arguments << QString( "-b %1" ).arg( Smb4KSettings::bufferSize() );
 
2346
  }
 
2347
  else
 
2348
  {
 
2349
    // Do nothing
 
2350
  }
 
2351
  
 
2352
  // Get global Samba and custom options
 
2353
  QMap<QString,QString> samba_options = globalSambaOptions();
 
2354
  Smb4KCustomOptions *options = Smb4KCustomOptionsManager::self()->findOptions( &m_host );
 
2355
  
 
2356
  // Port
 
2357
  // If a port was defined for the host via Smb4KHost::port(), it will 
 
2358
  // overwrite the other options.
 
2359
  if ( m_host.port() != -1 )
 
2360
  {
 
2361
    arguments << QString( "-p %1" ).arg( m_host.port() );
 
2362
  }
 
2363
  else
 
2364
  {
 
2365
    if ( options && options->smbPort() != Smb4KSettings::remoteSMBPort() )
 
2366
    {
 
2367
      arguments << QString( "-p %1" ).arg( options->smbPort() );
 
2368
    }
 
2369
    else
 
2370
    {
 
2371
      arguments << QString( "-p %1" ).arg( Smb4KSettings::remoteSMBPort() );
 
2372
    }
 
2373
  }
 
2374
  
 
2375
  // Kerberos
 
2376
  if ( options )
 
2377
  {
 
2378
    switch ( options->useKerberos() )
 
2379
    {
 
2380
      case Smb4KCustomOptions::UseKerberos:
 
2381
      {
 
2382
        arguments << "-k";
 
2383
        break;
 
2384
      }
 
2385
      case Smb4KCustomOptions::NoKerberos:
 
2386
      {
 
2387
        // No kerberos 
 
2388
        break;
 
2389
      }
 
2390
      case Smb4KCustomOptions::UndefinedKerberos:
 
2391
      {
 
2392
        if ( Smb4KSettings::useKerberos() )
 
2393
        {
 
2394
          arguments << "-k";
 
2395
        }
 
2396
        else
 
2397
        {
 
2398
          // Do nothing
 
2399
        }
 
2400
        break;
 
2401
      }
 
2402
      default:
 
2403
      {
 
2404
        break;
 
2405
      }
 
2406
    }
 
2407
  }
 
2408
  else
 
2409
  {
 
2410
    if ( Smb4KSettings::useKerberos() )
 
2411
    {
 
2412
      arguments << "-k";
 
2413
    }
 
2414
    else
 
2415
    {
 
2416
      // Do nothing
 
2417
    }
 
2418
  }
 
2419
  
 
2420
  // Resolve order
 
2421
  if ( !Smb4KSettings::nameResolveOrder().isEmpty() &&
 
2422
       QString::compare( Smb4KSettings::nameResolveOrder(), samba_options["name resolve order"] ) != 0 ) 
 
2423
  {
 
2424
    arguments << QString( "-R %1" ).arg( KShell::quoteArg( Smb4KSettings::nameResolveOrder() ) );
 
2425
  }
 
2426
  else
 
2427
  {
 
2428
    // Do nothing
 
2429
  }
 
2430
             
 
2431
  // NetBIOS name
 
2432
  if ( !Smb4KSettings::netBIOSName().isEmpty() &&
 
2433
       QString::compare( Smb4KSettings::netBIOSName(), samba_options["netbios name"] ) != 0 )
 
2434
  {
 
2435
    arguments << QString( "-n %1" ).arg( KShell::quoteArg( Smb4KSettings::netBIOSName() ) );
 
2436
  }
 
2437
  else
 
2438
  {
 
2439
    // Do nothing
 
2440
  }
 
2441
             
 
2442
  // NetBIOS scope
 
2443
  if ( !Smb4KSettings::netBIOSScope().isEmpty() &&
 
2444
        QString::compare( Smb4KSettings::netBIOSScope(), samba_options["netbios scope"] ) != 0 )
 
2445
  {
 
2446
    arguments << QString( "-i %1" ).arg( KShell::quoteArg( Smb4KSettings::netBIOSScope() ) );
 
2447
  }
 
2448
  else
 
2449
  {
 
2450
    // Do nothing
 
2451
  }
 
2452
  
 
2453
  // Socket options
 
2454
  if ( !Smb4KSettings::socketOptions().isEmpty() &&
 
2455
       QString::compare( Smb4KSettings::socketOptions(), samba_options["socket options"] ) != 0 )
 
2456
  {
 
2457
    arguments << QString( "-O %1" ).arg( KShell::quoteArg( Smb4KSettings::socketOptions() ) );
 
2458
  }
 
2459
  else
 
2460
  {
 
2461
    // Do nothing
 
2462
  }
 
2463
 
 
2464
  // Use Winbind CCache
 
2465
  if ( Smb4KSettings::useWinbindCCache() )
 
2466
  {
 
2467
    arguments << "-C";
 
2468
  }
 
2469
  else
 
2470
  {
 
2471
    // Do nothing
 
2472
  }
 
2473
 
 
2474
  // Use encryption
 
2475
  if ( Smb4KSettings::encryptSMBTransport() )
 
2476
  {
 
2477
    arguments << "-e";
 
2478
  }
 
2479
  else
 
2480
  {
 
2481
    // Do nothing
 
2482
  }
 
2483
  
 
2484
  m_proc = new Smb4KProcess( this );
 
2485
  m_proc->setOutputChannelMode( KProcess::SeparateChannels );
 
2486
  m_proc->setShellCommand( arguments.join( " " ) );
 
2487
 
 
2488
  connect( m_proc, SIGNAL( finished( int, QProcess::ExitStatus ) ), this, SLOT( slotProcessFinished( int, QProcess::ExitStatus ) ) );
 
2489
 
 
2490
  emit aboutToStart( &m_host );
 
2491
 
 
2492
  m_proc->start();  
 
2493
}
 
2494
 
 
2495
 
 
2496
void Smb4KLookupInfoJob::slotProcessFinished( int /*exitCode*/, QProcess::ExitStatus exitStatus )
 
2497
{
 
2498
  switch ( exitStatus )
 
2499
  {
 
2500
    case QProcess::CrashExit:
 
2501
    {
 
2502
      if ( !m_proc->isAborted() )
 
2503
      {
 
2504
        Smb4KNotification *notification = new Smb4KNotification();
 
2505
        notification->processError( m_proc->error() );
 
2506
      }
 
2507
      else
 
2508
      {
 
2509
        // Do nothing
 
2510
      }
 
2511
      break;
 
2512
    }
 
2513
    default:
 
2514
    {
 
2515
      processInfo();
 
2516
      break;
 
2517
    }
 
2518
  }
 
2519
 
 
2520
  emitResult();
 
2521
  emit finished( &m_host );
 
2522
}
 
2523
 
 
2524
 
28
2525
 
29
2526
 
30
2527
Smb4KScannerPrivate::Smb4KScannerPrivate()
31
2528
{
32
 
  retry = false;
33
 
  clearData();
34
2529
}
35
2530
 
36
2531
 
39
2534
}
40
2535
 
41
2536
 
42
 
void Smb4KScannerPrivate::clearData()
43
 
{
44
 
  m_workgroup = Smb4KWorkgroup();
45
 
  m_host = Smb4KHost();
46
 
}
47
 
 
48
 
 
49
 
void Smb4KScannerPrivate::setWorkgroup( const Smb4KWorkgroup &workgroup )
50
 
{
51
 
  m_workgroup = workgroup;
52
 
}
53
 
 
54
 
 
55
 
void Smb4KScannerPrivate::setHost( const Smb4KHost &host )
56
 
{
57
 
  m_host = host;
58
 
}
59
 
 
60
 
 
61
 
Smb4KScannerQueueContainer::Smb4KScannerQueueContainer( int todo, const Smb4KWorkgroup &wg )
62
 
: m_todo( todo ), m_workgroup( wg )
63
 
{
64
 
}
65
 
 
66
 
 
67
 
Smb4KScannerQueueContainer::Smb4KScannerQueueContainer( int todo, const Smb4KHost &item )
68
 
: m_todo( todo ), m_host( item )
69
 
{
70
 
}
71
 
 
72
 
 
73
 
Smb4KScannerQueueContainer::Smb4KScannerQueueContainer( int todo, const QString &string )
74
 
: m_todo( todo ), m_string( string )
75
 
{
76
 
}
77
 
 
78
 
 
79
 
Smb4KScannerQueueContainer::Smb4KScannerQueueContainer( int todo )
80
 
: m_todo( todo )
81
 
{
82
 
}
83
 
 
84
 
 
85
 
Smb4KScannerQueueContainer::~Smb4KScannerQueueContainer()
86
 
{
87
 
}
 
2537
#include "smb4kscanner_p.moc"