summaryrefslogtreecommitdiff
path: root/noncore/net/opie-smb/qsmb.cpp
blob: d74f240548fc264cc57a75c483ba39f7808c1cab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
               =.            This file is part of the OPIE Project
             .=l.            Copyright (c)  2002-2005  Kurt Korbatits <support@midget.net.au>
           .>+-=             Copyright (c)  2005  L. Potter <lpotter@trolltech.com>
 _;:,     .>    :=|.         This program is free software; you can 
.> <`_,   >  .   <=          redistribute it and/or  modify it under
:`=1 )Y*s>-.--   :           the terms of the GNU General Public
.="- .-=="i,     .._         License as published by the Free Software
 - .   .-<_>     .<>         Foundation; either version 2 of the License,
     ._= =}       :          or (at your option) any later version.
    .%`+i>       _;_.        
    .i_,=:_.      -<s.       This program is distributed in the hope that  
     +  .  -:.       =       it will be useful,  but WITHOUT ANY WARRANTY;
    : ..    .:,     . . .    without even the implied warranty of
    =_        +     =;=|`    MERCHANTABILITY or FITNESS FOR A
  _.=:.       :    :=>`:     PARTICULAR PURPOSE. See the GNU
..}^=.=       =       ;      General Public License for more
++=   -.     .`     .:       details.
 :     =  ...= . :.=-        
 -.   .:....=;==+<;          You should have received a copy of the GNU
  -_. . .   )=.  =           General Public License along with
    --        :-=`           this library; see the file COPYING.BIN. 
                             If not, write to the Free Software Foundation,
                             Inc., 59 Temple Place - Suite 330,
                             Boston, MA 02111-1307, USA.

*/

#include "qsmb.h"
#include <qpushbutton.h>
#include <qpe/qpeapplication.h>

#include <string.h>
#include <qstring.h>
#include <qstringlist.h>
#include <qdir.h>
#include <qfileinfo.h>
#include <qtabwidget.h>

#include <qpe/process.h>
#include <qlabel.h>
#include <qlineedit.h>
#include <qcombobox.h>
#include <qstringlist.h>
#include <qcheckbox.h>
#include <qtextview.h>
#include <qmessagebox.h>
#include <qtextstream.h>


#include <pthread.h>
#include <signal.h>
#include <ctype.h>


#include <netinet/in.h>
#include <arpa/inet.h>
#include <rpc/clnt.h>

#include <sys/vfs.h>
#include <mntent.h>

// #include <opie2/odebug.h>
// using namespace Opie::Core;


Qsmb::Qsmb( QWidget* parent,  const char* name, WFlags fl )
   : FormQPESMBBase( parent, name, fl )
{
   connect(CBHost, SIGNAL(activated(int)), this, SLOT(hostSelected(int)));
   connect(DoItBtn, SIGNAL(clicked()), this, SLOT(DoItClicked()));
   connect(UnmountBtn, SIGNAL(clicked()), this, SLOT(umountIt()));
   connect(BtnScan, SIGNAL(clicked()), this, SLOT(scanClicked()));
   connect(BtnClear, SIGNAL(clicked()), this, SLOT(clear()));
   connect(ListViewScan, SIGNAL(clicked(QListViewItem*)), this, SLOT(TextViewClicked(QListViewItem*)));

   mountpt->setEditable(true);
   mountpt->insertItem("/mnt/samba1",-1);
   mountpt->insertItem("/mnt/samba2",-1);
   mountpt->insertItem("/mnt/samba3",-1);
   
   setTabOrder(BtnScan, username);
   setTabOrder(username, password);
   setTabOrder(password, CBHost);
   setTabOrder(CBHost, TextViewOutput);
   setTabOrder(TextViewOutput, mountpt);
   setTabOrder(mountpt, DoItBtn);
   setTabOrder(DoItBtn, UnmountBtn);
   
   top_element = NULL;
   scanning = false;
}

Qsmb::~Qsmb()
{
}

void Qsmb::clear() 
{
   if (scanning) return;
   ListViewScan->clear();	
   TextViewOutput->setText("");
   CBHost->clear();
   top_element = NULL;	
}

void Qsmb::scanClicked() 
{
   if (scanning) return;
   pthread_create(&tpid, NULL, runit, (void *)this);
}

void Qsmb::DoItClicked() 
{

   if( !ListViewScan->selectedItem()) {
      QMessageBox::warning(this, tr("Error"),tr("<p>No share selected!</p>"));
      return;
   }
   if (scanning) return;
   pthread_create(&tpid, NULL, runitm, (void *)this);
}

void* runit(void* arg) 
{
   Qsmb* caller = (Qsmb*)arg;
   caller->scan();
   return(0);
}

void* runitm(void* arg) 
{
   Qsmb* caller = (Qsmb*)arg;
   caller->DoIt();
   return(0);
}

void Qsmb::scan() 
{
   clear();
//   if (scanning) return;
   scanning = true;

   QString match;
   QString cmd;
   LScan->setText("Scanning...");
   qApp->processEvents();

   sockaddr_in my_addr;
   get_myaddress( &my_addr);

   QString ip = inet_ntoa( my_addr.sin_addr);
   qWarning("IP Address : "+ip);

   match = ip.left(5);

   QStringList ccmd;
   TextViewOutput->append("smbfind");

   QFile lmhosts("/etc/samba/lmhosts");
   QTextStream lms(&lmhosts);
   lmhosts.open(IO_WriteOnly);
   lms << "127.0.0.1 localhost\n";
    
   /* parse output and display in ListViewScan */
   ccmd = "smbfind";
	 runCommand(ccmd);

   QTextStream s(&out, IO_ReadOnly);

   while ( !s.atEnd() ) {
      QString ip_addr, host, output;
      QString tmp = s.readLine();
      bool ok;
      tmp.left(1).toInt( &ok, 10 );
      if(ok) {
         QStringList token = QStringList::split(' ',  tmp );
         ip_addr =  token[0];
         host = token[1];
         CBHost->insertItem( host, -1);
         lms <<  ip_addr+" "+host+"\n";
      }
   }
   lmhosts.close();

   TextViewOutput->append("\n\n============================================\n");
   LScan->setText("");
   scanning = false;
}

void Qsmb::hostSelected(int /*index*/ )
{
   QListViewItem *element;
//   QListViewItem *parent;

   QString text = CBHost->currentText();
   ListViewScan->clear();

   if (scanning) return;
   scanning = true;

   QString cmd;
   QStringList ccmd;

   LScan->setText("Scanning...");

   ccmd << "/usr/bin/smbclient";
   ccmd << "-L";
   ccmd << CBHost->currentText();
   ccmd << "-N";
   
   if(username->text().isEmpty()) {
      //do nothing
   } else {
      ccmd << "-U";
      ccmd << username->text()+"\%"+ password->text();
   }
   runCommand(ccmd);
   QTextStream s(&out, IO_ReadOnly);

   while ( !s.atEnd() ) {
      QString share;
      QString comment;
      QString mount;
      QString tmp = s.readLine();
      
      if( tmp.find("$") == -1 && tmp.find("Disk") != -1) {
         QStringList token = QStringList::split(' ',  tmp );
         share = token[0];
         comment = token[2];
         share = share.stripWhiteSpace();
         comment = comment.stripWhiteSpace();
//         if(isMounted(share))

         mount = getMount(share);
         element = new QListViewItem(ListViewScan, share, comment, mount);
         element->setOpen(true);
//             top_element = element;
//             parent = element;
      }

   }
//   owarn << "i="<< index << "cmd:" <<  cmd << oendl;

   TextViewOutput->append(cmd);

   /* run smbclient & read output */
//    if ((pipe = popen(cmd.latin1(), "r")) == NULL) {
//       snprintf(result, 256, "Error: Can't run %s", cmd.latin1());
// //      cmd = "Error: Can't run "+cmd;
//       TextViewOutput->append(result);
//       return;
//    }

   /* parse output and display in ListViewScan */
//    while(fgets(result, 256, pipe) != NULL) {
//       /* put result into TextViewOutput */
//       TextViewOutput->append(result);

//       if( strchr(result, '$') == NULL ) { 
//          char share[256], *ptr1;

//          strcpy(share,result);
//          ptr1 = strchr(share,' ');
//          share[ptr1 - share]='\0';

//          owarn<< "add share: " << share << oendl;

//          if(top_element != NULL) {
//             bool found = false;
//             element = top_element;

//             while(element != NULL && !found) {
//                if(strcmp( element->text(0).ascii(), share)==0) {
//                   parent = element;
//                   found = true;
//                }
//                element = element->nextSibling();
//             }

//             if(!found) {
//                element = new QListViewItem(ListViewScan,share);
//                element->setOpen(true);
//                parent=element;
//             }
//          } else {
//             element = new QListViewItem(ListViewScan,share);
//             element->setOpen(true);
//             top_element = element;
//             parent = element;
//          }
//       }
//    }

   TextViewOutput->append("\n\n============================================\n");
   LScan->setText("");
   scanning = false;
}


void Qsmb::DoIt()
{
   
   QListViewItem *element;
   element = ListViewScan->selectedItem();
   if(!element) {
      return;
   }

   if (scanning) return;
   scanning = true;

   QString mount = mountpt->currentText();
   if(isMounted(mount)) {
      qWarning(mount +" is already mounted");
      TextViewOutput->append(mount +" is already mounted");
      return;
   }
   
   bool noerr = false;

   QString share;
   QString cmd;
   QString cmd2;
   QString text = mountpt->currentText();
   QStringList ccmd;

   LScan->setText("Mounting...");
   qApp->processEvents();

   if( !QFileInfo(text).exists()) {
      ccmd << "mkdir";
      ccmd << "-p";
      ccmd << text;

      qWarning( "cmd: "+ ccmd.join(" "));
      runCommand(ccmd);
   }

   share = element->text(0);
   qWarning("selected share is "+share);

   QString service = CBHost->currentText();
   service = service.stripWhiteSpace();
   if(mount.left(1) != "/")
      mount = QDir::currentDirPath()+"/"+mount;
   mount = mount.stripWhiteSpace();
   ccmd.clear();
   
   ccmd << "/usr/bin/smbmount";
   ccmd << "//"+ service+"/"+share;
   ccmd << mount;
   ccmd << "-o";
   ccmd << "username="+username->text()+",password="+password->text()+"";

   if(onbootBtn->isChecked()) {
      qWarning("Saving Setting permanently...");
      QFile sambenv("/opt/QtPalmtop/etc/samba.env");
      QTextStream smbv(&sambenv);
      sambenv.open(IO_WriteOnly);
      smbv << ccmd.join(" ") ;
      sambenv.close();
   }

   noerr = runCommand(ccmd);
   
   LScan->setText("");

   if(noerr && isMounted(mount)) {
      element->setText(2, mount);
      TextViewOutput->append("\n\n================CheckMounts==================\n");
      ccmd = "/bin/mount";
      runCommand(ccmd);
      TextViewOutput->append("\n\n============================================\n");
      qApp->processEvents();
   } else {
   //do nothing
 }

   scanning = false;
}

void Qsmb::umountIt()
{
   QListViewItem *element;
   element = ListViewScan->selectedItem();
   if(!element) {
      return;
   }

   QString mount = mountpt->currentText();
   if(!isMounted(mount)) {
      qWarning(mount +" is not mounted");
      TextViewOutput->append(mount +" is not mounted");
      return;
   }

   QStringList ccmd;
   QString share;
   share = element->text(0);
   qWarning("selected share is "+share);

   if(mount.left(1) != "/")
      mount = QDir::currentDirPath()+"/"+mount;
   mount = mount.stripWhiteSpace();

   ccmd << "/usr/bin/smbumount";
   ccmd << mount;
   runCommand(ccmd);

   element->setText(2, "");

   ccmd = "/bin/mount";
   runCommand(ccmd);
}

bool Qsmb::runCommand(const QStringList & command) {
   qWarning( "runCommand " + command.join(" ") );
   TextViewOutput->append(command.join(" "));
   out = "";
   Process ipkg_status( command);
   bool r = ipkg_status.exec("",out);

   qWarning("result is %d"+ r );
   qWarning("Output " + out );
   TextViewOutput->append(out);

//very hacky
   if(out.find("failed") !=-1) {
      r = false;
   }
   return r;
}


bool Qsmb::isMounted(const QString &mountPoint)
{
    struct mntent *me;
    bool mounted = false;
    FILE *mntfp = setmntent( "/etc/mtab", "r" );
    if ( mntfp ){
        while ( (me = getmntent( mntfp )) != 0 ) {
            QString deviceName = me->mnt_fsname;
            QString mountDir = me->mnt_dir;
            QString fsType = me->mnt_type;
            if( fsType == "smbfs" && (mountDir.find(mountPoint) != -1 | deviceName.find(mountPoint) != -1))
               mounted = true;
        }
    }
    endmntent( mntfp );
    return mounted;
}

QString Qsmb::getMount(const QString &shareName)
{
   struct mntent *me;
    QString mount;
    FILE *mntfp = setmntent( "/etc/mtab", "r" );
    if ( mntfp ){
        while ( (me = getmntent( mntfp )) != 0 ) {
            QString deviceName = me->mnt_fsname;
            QString mountDir = me->mnt_dir;
            QString fsType = me->mnt_type;
            if( fsType == "smbfs" &&  deviceName.find(shareName) != -1)
               mount = mountDir;
        }
    }
    endmntent( mntfp );
    return mount;
}

void Qsmb::TextViewClicked(QListViewItem* item)
{
   if(item == NULL) return;

   QString text = item->text(2);
   qWarning(text);
   if( !text.isEmpty())
      mountpt->insertItem(text,0);
}