Smb4K – Samba (SMB) share advanced browser
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1002 lines
25KB

  1. /***************************************************************************
  2. smb4kpasswordhandler - This class handles the passwords for Smb4K.
  3. -------------------
  4. begin : So Jan 16 2005
  5. copyright : (C) 2005-2007 by Alexander Reinholdt
  6. email : dustpuppy@users.berlios.de
  7. ***************************************************************************/
  8. /***************************************************************************
  9. * This program is free software; you can redistribute it and/or modify *
  10. * it under the terms of the GNU General Public License as published by *
  11. * the Free Software Foundation; either version 2 of the License, or *
  12. * (at your option) any later version. *
  13. * *
  14. * This program is distributed in the hope that it will be useful, but *
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of *
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
  17. * General Public License for more details. *
  18. * *
  19. * You should have received a copy of the GNU General Public License *
  20. * along with this program; if not, write to the *
  21. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, *
  22. * MA 02110-1301 USA *
  23. ***************************************************************************/
  24. // TQt includes
  25. #include <tqframe.h>
  26. #include <tqlayout.h>
  27. #include <tqstring.h>
  28. #include <tqgroupbox.h>
  29. #include <tqlabel.h>
  30. #include <tqdir.h>
  31. #include <tqfile.h>
  32. #include <tqstringlist.h>
  33. // KDE includes
  34. #include <tdelocale.h>
  35. #include <kstandarddirs.h>
  36. #include <tdemessagebox.h>
  37. #include <kiconloader.h>
  38. #include <klineedit.h>
  39. #include <kcombobox.h>
  40. #include <kdebug.h>
  41. #include <tdeapplication.h>
  42. #include <dcopclient.h>
  43. #ifdef __FreeBSD__
  44. #include <sys/stat.h>
  45. #include <fcntl.h>
  46. #include <errno.h>
  47. #endif
  48. // application specific includes
  49. #include "smb4kpasswordhandler.h"
  50. #include "smb4kdefs.h"
  51. #include "smb4kerror.h"
  52. #include "smb4kauthinfo.h"
  53. #include "smb4ksettings.h"
  54. #ifndef __FreeBSD__
  55. Smb4KPasswordHandler::Smb4KPasswordHandler( Smb4KHomesSharesHandler *handler, TQObject *parent, const char *name )
  56. : TQObject( parent, name ), m_handler( handler ), m_wallet_support_disabled( false )
  57. #else
  58. Smb4KPasswordHandler::Smb4KPasswordHandler( Smb4KHomesSharesHandler *s_handler, Smb4KSambaOptionsHandler *o_handler, TQObject *parent, const char *name )
  59. : TQObject( parent, name ), m_handler( s_handler ), m_options_handler( o_handler ),
  60. m_wallet_support_disabled( false )
  61. #endif
  62. {
  63. if ( !m_handler )
  64. {
  65. kdFatal() << "Smb4KPasswordHandler: No Smb4KHomesSharesHandler object" << endl;
  66. }
  67. #ifdef __FreeBSD__
  68. if ( !m_options_handler )
  69. {
  70. kdFatal() << "Smb4KPasswordHandler: No Smb4KSambaOptionsHandler object" << endl;
  71. }
  72. #endif
  73. m_auth = NULL;
  74. m_dlg = NULL;
  75. m_wallet = NULL;
  76. m_temp_auth = NULL;
  77. }
  78. Smb4KPasswordHandler::~Smb4KPasswordHandler()
  79. {
  80. for ( TQValueList<Smb4KAuthInfo *>::Iterator it = m_auth_list.begin(); it != m_auth_list.end(); ++it )
  81. {
  82. delete *it;
  83. }
  84. m_auth_list.clear();
  85. delete m_wallet;
  86. }
  87. void Smb4KPasswordHandler::open_close_wallet()
  88. {
  89. if ( Smb4KSettings::useWallet() && !walletSupportIsDisabled() )
  90. {
  91. if ( !walletIsOpen() )
  92. {
  93. // Start the wallet manager before accessing the wallet. We
  94. // do not care about the return value, because we do not do
  95. // error handling here.
  96. if ( kapp )
  97. {
  98. (void) kapp->tdeinitExec( "tdewalletmanager" );
  99. }
  100. else
  101. {
  102. // Do nothing. --- Good luck!
  103. }
  104. m_wallet = TDEWallet::Wallet::openWallet( TDEWallet::Wallet::NetworkWallet(), 0, TDEWallet::Wallet::Synchronous );
  105. if ( m_wallet )
  106. {
  107. if ( !m_wallet->hasFolder( "Smb4K" ) )
  108. {
  109. m_wallet->createFolder( "Smb4K" );
  110. m_wallet->setFolder( "Smb4K" );
  111. }
  112. else
  113. {
  114. m_wallet->setFolder( "Smb4K" );
  115. convert_old_entries();
  116. }
  117. }
  118. else
  119. {
  120. Smb4KError::error( ERROR_OPENING_WALLET_FAILED, TDEWallet::Wallet::NetworkWallet(), TQString() );
  121. delete m_wallet;
  122. m_wallet = NULL;
  123. m_wallet_support_disabled = true;
  124. }
  125. }
  126. else
  127. {
  128. convert_old_entries();
  129. }
  130. }
  131. else
  132. {
  133. if ( m_wallet )
  134. {
  135. delete m_wallet;
  136. m_wallet = NULL;
  137. }
  138. }
  139. }
  140. void Smb4KPasswordHandler::convert_old_entries()
  141. {
  142. // Convert old entries (Smb4K 0.9.2 and earlier) to the
  143. // new map based format.
  144. if ( !m_wallet->entryList().isEmpty() )
  145. {
  146. TQStringList entries = m_wallet->entryList();
  147. // Since in the old format the keys contained a string like
  148. // this: HOST:USER, we only need to test whether the first
  149. // key contains a ":". If that's the case, we need to convert
  150. // the entries, otherwise we don't.
  151. if ( entries.first().contains( ":" ) != 0 )
  152. {
  153. for ( TQStringList::Iterator it = entries.begin(); it != entries.end(); ++it )
  154. {
  155. // Get the password.
  156. TQString pass;
  157. m_wallet->readPassword( *it, pass );
  158. if ( (*it).startsWith( "DEFAULT:" ) )
  159. {
  160. // Convert the data to the new format.
  161. TQMap<TQString,TQString> map;
  162. map["Login"] = (*it).section( ":", 1, 1 );
  163. map["Password"] = pass;
  164. m_wallet->writeMap( "DEFAULT_LOGIN", map );
  165. }
  166. else
  167. {
  168. // Convert the data to the new format.
  169. TQMap<TQString,TQString> map;
  170. map["Login"] = (*it).section( ":", 3, 3 );
  171. map["Password"] = pass;
  172. if ( TQString::compare( (*it).section( ":", 0, 0 ), "*" ) != 0 )
  173. {
  174. map["Workgroup"] = (*it).section( ":", 0, 0 ).upper();
  175. }
  176. if ( TQString::compare( (*it).section( ":", 2, 2 ), "*" ) == 0 )
  177. {
  178. m_wallet->writeMap( (*it).section( ":", 1, 1 ).upper(), map );
  179. }
  180. else
  181. {
  182. m_wallet->writeMap( "//"+(*it).section( ":", 1, 1 ).upper()+
  183. "/"+(*it).section( ":", 2, 2 ).upper(), map );
  184. }
  185. }
  186. // Delete the old entry.
  187. m_wallet->removeEntry( *it );
  188. }
  189. }
  190. else
  191. {
  192. // Do nothing.
  193. }
  194. }
  195. else
  196. {
  197. // Do nothing
  198. }
  199. }
  200. bool Smb4KPasswordHandler::askpass( const TQString &workgroup, const TQString &host, const TQString &share, int desc, TQWidget *parent, const char *name )
  201. {
  202. // m_auth is NULL:
  203. m_auth = readAuth( new Smb4KAuthInfo( workgroup, host, share ) );
  204. // Set up the askpass dialog:
  205. m_dlg = new KDialogBase( KDialogBase::Plain, i18n( "Authentication" ), KDialogBase::Ok|KDialogBase::Cancel,
  206. KDialogBase::Ok, parent, name, true, true );
  207. TQFrame *frame = m_dlg->plainPage();
  208. TQGridLayout *layout = new TQGridLayout( frame );
  209. layout->setSpacing( 5 );
  210. layout->setMargin( 0 );
  211. TQLabel *pixmap_label = new TQLabel( frame );
  212. pixmap_label->setPixmap( DesktopIcon( "identity" ) );
  213. pixmap_label->adjustSize();
  214. layout->addWidget( pixmap_label, 0, 0, TQt::AlignCenter );
  215. TQString message;
  216. switch ( desc )
  217. {
  218. case NewData:
  219. break;
  220. case AccessDenied:
  221. message = i18n( "The access was denied. " );
  222. break;
  223. case BadPassword:
  224. message = i18n( "The password is not correct. " );
  225. break;
  226. case PermDenied:
  227. message = i18n( "The permission was denied. " );
  228. break;
  229. case AuthError:
  230. message = i18n( "An authentication error occurred. " );
  231. break;
  232. case LogonFailure:
  233. message = i18n( "The logon failed. " );
  234. break;
  235. default:
  236. break;
  237. }
  238. if ( m_auth->share().stripWhiteSpace().isEmpty() )
  239. {
  240. message.append( i18n( "Please enter authentication data for server %1." ).arg( m_auth->host() ) );
  241. }
  242. else
  243. {
  244. message.append( i18n( "Please enter authentication data for share %1." ).arg( "//"+m_auth->host()+"/"+m_auth->share() ) );
  245. }
  246. TQLabel *message_label = new TQLabel( frame );
  247. message_label->setText( message.stripWhiteSpace() );
  248. message_label->setTextFormat( TQt::RichText );
  249. layout->addWidget( message_label, 0, 1, 0 );
  250. TQLabel *user_label = new TQLabel( i18n( "User:" ), frame );
  251. layout->addWidget( user_label, 1, 0, 0 );
  252. KLineEdit *user_edit = NULL;
  253. KComboBox *user_combo = NULL;
  254. if ( TQString::compare( m_auth->share(), "homes" ) != 0 )
  255. {
  256. user_edit = new KLineEdit( frame, "AskPassUserEdit" );
  257. user_edit->setMinimumWidth( 200 );
  258. layout->addWidget( user_edit, 1, 1, 0 );
  259. }
  260. else
  261. {
  262. user_combo = new KComboBox( frame, "AskPassUserCombo" );
  263. user_combo->setEditable( true );
  264. user_combo->setMinimumWidth( 200 );
  265. layout->addWidget( user_combo, 1, 1, 0 );
  266. }
  267. TQLabel *password_label = new TQLabel( i18n( "Password:" ), frame );
  268. layout->addWidget( password_label, 2, 0, 0 );
  269. KLineEdit *pass_edit = new KLineEdit( frame, "AskPassPasswordEdit" );
  270. pass_edit->setEchoMode( KLineEdit::Password );
  271. layout->addWidget( pass_edit, 2, 1, 0 );
  272. m_dlg->setMainWidget( frame );
  273. m_dlg->setFixedSize( 350, m_dlg->sizeHint().height() );
  274. m_dlg->enableButtonOK( false );
  275. // Since we have to allow empty passwords, we will only connect
  276. // the edit line for the user to enable/disable the OK button.
  277. if ( user_edit )
  278. {
  279. connect( user_edit, TQT_SIGNAL( textChanged( const TQString & ) ),
  280. this, TQT_SLOT( slotEnableOKButton( const TQString& ) ) );
  281. }
  282. else
  283. {
  284. connect( user_combo, TQT_SIGNAL( textChanged( const TQString & ) ),
  285. this, TQT_SLOT( slotEnableOKButton( const TQString& ) ) );
  286. }
  287. // Process the authentication data:
  288. if ( TQString::compare( share, "homes" ) != 0 )
  289. {
  290. user_edit->setText( m_auth->user() );
  291. pass_edit->setText( m_auth->password() );
  292. if ( m_auth->user().isEmpty() )
  293. {
  294. user_edit->setFocus();
  295. }
  296. else
  297. {
  298. pass_edit->setFocus();
  299. }
  300. }
  301. else
  302. {
  303. TQStringList list = m_handler->homesUsers( host );
  304. user_combo->insertStringList( list );
  305. user_combo->setCurrentText( TQString() );
  306. connect( user_combo, TQT_SIGNAL( activated( const TQString & ) ),
  307. this, TQT_SLOT( slotGetPassword( const TQString & ) ) );
  308. user_combo->setFocus();
  309. }
  310. bool ok = false;
  311. if ( m_dlg->exec() == KDialogBase::Accepted )
  312. {
  313. if ( TQString::compare( share, "homes" ) != 0 )
  314. {
  315. TQString user = user_edit->text();
  316. TQString pass = pass_edit->text();
  317. m_auth->setUser( user );
  318. m_auth->setPassword( pass );
  319. writeAuth( m_auth );
  320. }
  321. else
  322. {
  323. TQString user = user_combo->currentText();
  324. TQString pass = pass_edit->text();
  325. m_auth->setUser( user );
  326. m_auth->setPassword( pass );
  327. writeAuth( m_auth );
  328. }
  329. ok = true;
  330. }
  331. // Clean up:
  332. delete m_dlg;
  333. m_dlg = NULL;
  334. delete m_auth;
  335. m_auth = NULL;
  336. return ok;
  337. }
  338. Smb4KAuthInfo *Smb4KPasswordHandler::readAuth( Smb4KAuthInfo *authInfo )
  339. {
  340. // Do nothing, if authInfo is NULL:
  341. if ( !authInfo )
  342. {
  343. return authInfo;
  344. }
  345. open_close_wallet();
  346. if ( walletIsOpen() )
  347. {
  348. // Get the authentication information from the wallet.
  349. // Always prefer either the exact match or the information
  350. // that was provided for the host.
  351. TQMap<TQString,TQString> map;
  352. if ( !authInfo->share().isEmpty() )
  353. {
  354. m_wallet->readMap( "//"+authInfo->host().upper()+"/"+authInfo->share().upper(), map );
  355. if ( map.isEmpty() )
  356. {
  357. m_wallet->readMap( authInfo->host().upper(), map );
  358. if ( map.isEmpty() )
  359. {
  360. if ( Smb4KSettings::useDefaultLogin() )
  361. {
  362. m_wallet->readMap( "DEFAULT_LOGIN", map );
  363. if ( map.isEmpty() )
  364. {
  365. return authInfo;
  366. }
  367. else
  368. {
  369. // We have authentication information.
  370. // So, do nothing here.
  371. }
  372. }
  373. else
  374. {
  375. return authInfo;
  376. }
  377. }
  378. else
  379. {
  380. // Check that the workgroup is correct.
  381. if ( map.contains( "Workgroup" ) && !authInfo->workgroup().isEmpty() )
  382. {
  383. if ( TQString::compare( map["Workgroup"].upper(), authInfo->workgroup().upper() ) != 0 )
  384. {
  385. return authInfo;
  386. }
  387. else
  388. {
  389. // Everything is OK. Do nothing.
  390. }
  391. }
  392. else
  393. {
  394. // We cannot check if the workgroup is OK. So, just return
  395. // the authentication information we just collected. If it
  396. // should be wrong, the user will be asked to provide the
  397. // correct data anyway.
  398. }
  399. }
  400. }
  401. else
  402. {
  403. // Check that the workgroup is correct.
  404. if ( map.contains( "Workgroup" ) && !authInfo->workgroup().isEmpty() )
  405. {
  406. if ( TQString::compare( map["Workgroup"].upper(), authInfo->workgroup().upper() ) != 0 )
  407. {
  408. return authInfo;
  409. }
  410. else
  411. {
  412. // Everything is OK. Do nothing.
  413. }
  414. }
  415. else
  416. {
  417. // We cannot check if the workgroup is OK. So, just process
  418. // the authentication information we just collected. If it
  419. // should be wrong, the user will be asked to provide the
  420. // correct data anyway.
  421. }
  422. }
  423. }
  424. else
  425. {
  426. m_wallet->readMap( authInfo->host().upper(), map );
  427. if ( map.isEmpty() )
  428. {
  429. if ( Smb4KSettings::useDefaultLogin() )
  430. {
  431. m_wallet->readMap( "DEFAULT_LOGIN", map );
  432. if ( map.isEmpty() )
  433. {
  434. return authInfo;
  435. }
  436. }
  437. else
  438. {
  439. return authInfo;
  440. }
  441. }
  442. else
  443. {
  444. // Check that the workgroup is correct.
  445. if ( map.contains( "Workgroup" ) && !authInfo->workgroup().isEmpty() )
  446. {
  447. if ( TQString::compare( map["Workgroup"].upper(), authInfo->workgroup().upper() ) != 0 )
  448. {
  449. return authInfo;
  450. }
  451. else
  452. {
  453. // Everything is OK. Do nothing.
  454. }
  455. }
  456. else
  457. {
  458. // We cannot check if the workgroup is OK. So, just process
  459. // the authentication information we just collected. If it
  460. // should be wrong, the user will be asked to provide the
  461. // correct data anyway.
  462. }
  463. }
  464. }
  465. // We have authentication information. Put the data into the
  466. // Smb4KAuthInfo object.
  467. authInfo->setUser( map["Login"] );
  468. authInfo->setPassword( map["Password"] );
  469. }
  470. else
  471. {
  472. // Either the opening of the wallet failed or the user does
  473. // not want to use a wallet.
  474. // Let's see if we can get the authentication information from
  475. // the temporary list.
  476. if ( Smb4KSettings::rememberPasswords() )
  477. {
  478. if ( !m_auth_list.isEmpty() )
  479. {
  480. for ( TQValueList<Smb4KAuthInfo *>::Iterator it = m_auth_list.begin(); it != m_auth_list.end(); ++it )
  481. {
  482. if ( !authInfo->share().isEmpty() )
  483. {
  484. if ( TQString::compare( authInfo->host().upper(), (*it)->host().upper() ) == 0 &&
  485. TQString::compare( authInfo->share().upper(), (*it)->share().upper() ) == 0 )
  486. {
  487. if ( TQString::compare( authInfo->workgroup().upper(), (*it)->workgroup().upper() ) == 0 ||
  488. (*it)->workgroup().isEmpty() )
  489. {
  490. // Either the workgroup is OK, or we cannot check if it is OK.
  491. // In both cases we process the data at hand.
  492. authInfo->setUser( (*it)->user() );
  493. authInfo->setPassword( (*it)->password() );
  494. // Exact match. Stop here.
  495. break;
  496. }
  497. else
  498. {
  499. continue;
  500. }
  501. }
  502. else if ( TQString::compare( authInfo->host().upper(), (*it)->host().upper() ) == 0 )
  503. {
  504. if ( TQString::compare( authInfo->workgroup().upper(), (*it)->workgroup().upper() ) == 0 ||
  505. (*it)->workgroup().isEmpty() )
  506. {
  507. // Either the workgroup is OK, or we cannot check if it is OK.
  508. // In both cases we process the data at hand.
  509. authInfo->setUser( (*it)->user() );
  510. authInfo->setPassword( (*it)->password() );
  511. // Because we always prefer the exact match, we keep
  512. // on searching.
  513. continue;
  514. }
  515. else
  516. {
  517. continue;
  518. }
  519. }
  520. else
  521. {
  522. continue;
  523. }
  524. }
  525. else
  526. {
  527. if ( TQString::compare( authInfo->host().upper(), (*it)->host().upper() ) == 0 )
  528. {
  529. if ( TQString::compare( authInfo->workgroup().upper(), (*it)->workgroup().upper() ) == 0 ||
  530. (*it)->workgroup().isEmpty() )
  531. {
  532. // Either the workgroup is OK, or we cannot check if it is OK.
  533. // In both cases we process the data at hand.
  534. authInfo->setUser( (*it)->user() );
  535. authInfo->setPassword( (*it)->password() );
  536. break;
  537. }
  538. else
  539. {
  540. continue;
  541. }
  542. }
  543. else
  544. {
  545. continue;
  546. }
  547. }
  548. }
  549. }
  550. else
  551. {
  552. // Do nothing
  553. }
  554. }
  555. else
  556. {
  557. if ( m_temp_auth )
  558. {
  559. authInfo->setUser( m_temp_auth->user() );
  560. authInfo->setPassword( m_temp_auth->password() );
  561. delete m_temp_auth;
  562. m_temp_auth = NULL;
  563. }
  564. }
  565. }
  566. #ifdef __FreeBSD__
  567. writeToSMBConfFile( authInfo );
  568. #endif
  569. return authInfo;
  570. }
  571. void Smb4KPasswordHandler::writeAuth( Smb4KAuthInfo *authInfo )
  572. {
  573. open_close_wallet();
  574. if ( walletIsOpen() )
  575. {
  576. TQMap<TQString,TQString> map;
  577. map["Login"] = authInfo->user();
  578. map["Password"] = authInfo->password();
  579. if ( !authInfo->workgroup().isEmpty() )
  580. {
  581. map["Workgroup"] = authInfo->workgroup().upper();
  582. }
  583. if ( !authInfo->share().isEmpty() )
  584. {
  585. m_wallet->writeMap( "//"+authInfo->host().upper()+"/"+authInfo->share().upper(), map );
  586. }
  587. else
  588. {
  589. m_wallet->writeMap( authInfo->host().upper(), map );
  590. }
  591. m_wallet->sync();
  592. }
  593. else
  594. {
  595. if ( Smb4KSettings::rememberPasswords() )
  596. {
  597. Smb4KAuthInfo *tmp = NULL;
  598. for ( TQValueList<Smb4KAuthInfo *>::Iterator it = m_auth_list.begin(); it != m_auth_list.end(); ++it )
  599. {
  600. if ( ((*it)->workgroup().isEmpty() ||
  601. TQString::compare( (*it)->workgroup().upper(), authInfo->workgroup().upper() ) == 0) &&
  602. TQString::compare( (*it)->host().upper(), authInfo->host().upper() ) == 0 &&
  603. TQString::compare( (*it)->host().upper(), authInfo->share().upper() ) == 0 )
  604. {
  605. tmp = *it;
  606. break;
  607. }
  608. else
  609. {
  610. continue;
  611. }
  612. }
  613. if ( tmp )
  614. {
  615. delete tmp;
  616. }
  617. m_auth_list.append( new Smb4KAuthInfo( *authInfo ) );
  618. }
  619. else
  620. {
  621. if ( !m_temp_auth )
  622. {
  623. m_temp_auth = new Smb4KAuthInfo( *authInfo );
  624. }
  625. }
  626. }
  627. #ifdef __FreeBSD__
  628. writeToSMBConfFile( authInfo );
  629. #endif
  630. }
  631. Smb4KAuthInfo *Smb4KPasswordHandler::readDefaultAuth( Smb4KAuthInfo *authInfo )
  632. {
  633. if ( !authInfo )
  634. {
  635. return authInfo;
  636. }
  637. open_close_wallet();
  638. if ( walletIsOpen() )
  639. {
  640. // Read the default authentication information.
  641. TQMap<TQString,TQString> map;
  642. m_wallet->readMap( "DEFAULT_LOGIN", map );
  643. if ( !map.isEmpty() )
  644. {
  645. authInfo->setUser( map["Login"] );
  646. authInfo->setPassword( map["Password"] );
  647. }
  648. else
  649. {
  650. // Do nothing
  651. }
  652. }
  653. else
  654. {
  655. // Nothing to do.
  656. }
  657. return authInfo;
  658. }
  659. void Smb4KPasswordHandler::writeDefaultAuth( Smb4KAuthInfo *authInfo )
  660. {
  661. open_close_wallet();
  662. if ( walletIsOpen() )
  663. {
  664. TQMap<TQString,TQString> map;
  665. map["Login"] = authInfo->user();
  666. map["Password"] = authInfo->password();
  667. m_wallet->writeMap( "DEFAULT_LOGIN", map );
  668. m_wallet->sync();
  669. }
  670. }
  671. #ifdef __FreeBSD__
  672. void Smb4KPasswordHandler::writeToSMBConfFile( Smb4KAuthInfo *authInfo )
  673. {
  674. m_nsmbrc_auth = *authInfo;
  675. TDEProcess *p = new TDEProcess();
  676. p->setUseShell( true );
  677. connect( p, TQT_SIGNAL( receivedStdout( TDEProcess *, char *, int ) ),
  678. this, TQT_SLOT( slotReceivePassword( TDEProcess *, char *, int ) ) );
  679. connect( p, TQT_SIGNAL( processExited( TDEProcess * ) ),
  680. this, TQT_SLOT( slotWritePassword( TDEProcess * ) ) );
  681. *p << TQString( "smbutil crypt %1" ).arg( m_nsmbrc_auth.password() );
  682. p->start( TDEProcess::NotifyOnExit, TDEProcess::AllOutput );
  683. }
  684. #endif
  685. /////////////////////////////////////////////////////////////////////////////
  686. // TQT_SLOT IMPLEMENTATIONS
  687. /////////////////////////////////////////////////////////////////////////////
  688. void Smb4KPasswordHandler::slotGetPassword( const TQString &username )
  689. {
  690. if ( m_dlg && m_auth )
  691. {
  692. // We need to re-read the auth data here, because
  693. // of the homes shares:
  694. Smb4KAuthInfo *auth = readAuth( new Smb4KAuthInfo( m_auth->workgroup().upper(), m_auth->host().upper(), username ) );
  695. KLineEdit *lineEdit = static_cast<KLineEdit *>( TQT_TQWIDGET(m_dlg->child( "AskPassPasswordEdit", "KLineEdit", true )) );
  696. lineEdit->setText( auth->password() );
  697. delete auth;
  698. m_auth->setShare( username );
  699. }
  700. }
  701. void Smb4KPasswordHandler::slotEnableOKButton( const TQString &text )
  702. {
  703. if ( m_dlg )
  704. {
  705. m_dlg->enableButtonOK( !text.isEmpty() );
  706. }
  707. }
  708. #ifdef __FreeBSD__
  709. void Smb4KPasswordHandler::slotReceivePassword( TDEProcess *, char *buffer, int buflen )
  710. {
  711. m_buffer.append( TQString::fromLocal8Bit( buffer, buflen ) );
  712. #else
  713. void Smb4KPasswordHandler::slotReceivePassword( TDEProcess *, char *, int )
  714. {
  715. #endif
  716. }
  717. void Smb4KPasswordHandler::slotWritePassword( TDEProcess *proc )
  718. {
  719. delete proc;
  720. #ifdef __FreeBSD__
  721. TQString pass = m_buffer.remove( "\n" ).stripWhiteSpace();
  722. m_buffer = TQString();
  723. TQDir::setCurrent( TQDir::homeDirPath() );
  724. TQFile file( ".nsmbrc" );
  725. TQStringList contents;
  726. if ( file.exists() )
  727. {
  728. if ( file.open( IO_ReadOnly ) )
  729. {
  730. TQTextStream ts( &file );
  731. ts.setEncoding( TQTextStream::Locale );
  732. while ( !ts.atEnd() )
  733. {
  734. contents.append( ts.readLine().stripWhiteSpace() );
  735. }
  736. file.close();
  737. }
  738. }
  739. // Check if the [default] section is present.
  740. if ( contents.find( "[default]" ) == contents.end() &&
  741. contents.find( "[DEFAULT]" ) == contents.end() )
  742. {
  743. TQMap<TQString,TQString> map = m_options_handler->globalSambaOptions();
  744. TQString workgroup = map["workgroup"];
  745. TQString wins = m_options_handler->winsServer();
  746. TQStringList::Iterator it = contents.prepend( "[default]" );
  747. it++;
  748. if ( !workgroup.isEmpty() )
  749. {
  750. it = contents.insert( it, "workgroup="+workgroup );
  751. }
  752. if ( !wins.isEmpty() )
  753. {
  754. it = contents.insert( it, "nbns="+wins );
  755. }
  756. // FIXME: Should we write the charsets here, too??
  757. }
  758. TQString section;
  759. if ( m_nsmbrc_auth.share().isEmpty() )
  760. {
  761. section.append( TQString( "[%1:%2]" ).arg( m_nsmbrc_auth.host().upper(), m_nsmbrc_auth.user().isEmpty() ? "GUEST" : m_nsmbrc_auth.user().upper() ) );
  762. }
  763. else
  764. {
  765. section.append( TQString( "[%1:%2:%3]" ).arg( m_nsmbrc_auth.host().upper(), m_nsmbrc_auth.user().isEmpty() ? "GUEST" : m_nsmbrc_auth.user().upper(), m_nsmbrc_auth.share().upper() ) );
  766. }
  767. TQStringList::Iterator it = contents.find( section );
  768. if ( it == contents.end() )
  769. {
  770. if ( contents.last() != TQString() )
  771. {
  772. contents.append( TQString() );
  773. }
  774. contents.append( section );
  775. contents.append( "password="+pass );
  776. if ( !m_nsmbrc_auth.workgroup().isEmpty() )
  777. {
  778. contents.append( "workgroup="+m_nsmbrc_auth.workgroup() );
  779. }
  780. }
  781. else
  782. {
  783. for ( TQStringList::Iterator i = ++it; i != contents.end(); ++i )
  784. {
  785. if ( (*i).contains( "password=" ) )
  786. {
  787. TQString old_pass = (*i).section( "password=", 1, 1 ).stripWhiteSpace();
  788. if ( TQString::compare( pass, old_pass ) == 0 )
  789. {
  790. // The passwords are identical, so stop here.
  791. return;
  792. }
  793. else
  794. {
  795. *i = "password="+pass;
  796. break;
  797. }
  798. }
  799. else if ( (*i).startsWith( "[" ) )
  800. {
  801. break;
  802. }
  803. else
  804. {
  805. continue;
  806. }
  807. }
  808. }
  809. TQDir::setCurrent( TQDir::homeDirPath() );
  810. if ( file.open( IO_WriteOnly ) )
  811. {
  812. TQTextStream ts( &file );
  813. ts.setEncoding( TQTextStream::Locale );
  814. for ( TQStringList::ConstIterator it = contents.begin(); it != contents.end(); ++it )
  815. {
  816. ts << *it << endl;
  817. }
  818. file.close();
  819. }
  820. else
  821. {
  822. Smb4KError::error( ERROR_WRITING_FILE, "~/.nsmbrc", TQString() );
  823. return;
  824. }
  825. // Get minimal security: Fix permissions.
  826. TQString path = TQDir::homeDirPath()+"/"+file.name();
  827. int fd;
  828. if ( (fd = open( path.ascii(), O_WRONLY )) == -1 )
  829. {
  830. int err_no = errno;
  831. Smb4KError::error( ERROR_OPENING_FILE, path, strerror( err_no ) );
  832. return;
  833. }
  834. else
  835. {
  836. if ( fchmod( fd, 00600 ) == -1 )
  837. {
  838. // FIXME: Do we need to emit an error here at all?
  839. }
  840. }
  841. #endif
  842. }
  843. #include "smb4kpasswordhandler.moc"