@@ -398,51 +398,24 @@ void DiskEncryptMenuScene::doReencryptDevice(const DeviceEncryptParam ¶m)
398398 kDaemonBusPath ,
399399 kDaemonBusIface ,
400400 QDBusConnection::systemBus ());
401- if (iface.isValid ()) {
402- // Create anonymous pipe for secure credential transmission
403- int pipefd[2 ];
404- if (pipe (pipefd) == -1 ) {
405- fmCritical () << " Failed to create anonymous pipe for credentials" ;
406- return ;
407- }
408-
409- // Prepare credentials data using QDataStream for reliable serialization
410- QByteArray credentials;
411- QDataStream stream (&credentials, QIODevice::WriteOnly);
412- QVariantMap params {
413- { encrypt_param_keys::kKeyDevice , param.devDesc },
414- { encrypt_param_keys::kKeyPassphrase , toBase64 (param.key ) },
415- { encrypt_param_keys::kKeyExportToPath , param.exportPath },
416- };
417- if (!tpmToken.isEmpty ()) params.insert (encrypt_param_keys::kKeyTPMToken , tpmToken);
418- stream << params;
419-
420- // Write credentials to pipe and close write end immediately
421- ssize_t written = write (pipefd[1 ], credentials.constData (), credentials.size ());
422- close (pipefd[1 ]); // Close write end immediately after writing
423-
424- if (written != credentials.size ()) {
425- fmCritical () << " Failed to write credentials to pipe, written:" << written << " expected:" << credentials.size ();
426- close (pipefd[0 ]);
427- return ;
428- }
401+ if (!iface.isValid ()) {
402+ fmCritical () << " Failed to create D-Bus interface for re-encryption" ;
403+ return ;
404+ }
429405
430- // Create file descriptor for D-Bus transmission
431- QDBusUnixFileDescriptor fd (pipefd[0 ]);
432- if (!fd.isValid ()) {
433- fmCritical () << " Failed to create valid file descriptor from pipe" ;
434- close (pipefd[0 ]);
435- return ;
436- }
406+ // Prepare credentials data
407+ QVariantMap params {
408+ { encrypt_param_keys::kKeyDevice , param.devDesc },
409+ { encrypt_param_keys::kKeyPassphrase , toBase64 (param.key ) },
410+ { encrypt_param_keys::kKeyExportToPath , param.exportPath },
411+ };
412+ if (!tpmToken.isEmpty ())
413+ params.insert (encrypt_param_keys::kKeyTPMToken , tpmToken);
437414
438- fmDebug () << " Starting device re-encryption via fd" ;
439- iface.asyncCall (" SetupAuthArgs" , QVariant::fromValue (fd));
415+ // Send credentials via fd
416+ fmDebug () << " Starting device re-encryption via fd" ;
417+ if (sendCredentialsViaFd (iface, " SetupAuthArgs" , params, true )) {
440418 QApplication::setOverrideCursor (Qt::WaitCursor);
441-
442- // Close read end (D-Bus service will have its own copy)
443- close (pipefd[0 ]);
444- } else {
445- fmCritical () << " Failed to create D-Bus interface for re-encryption" ;
446419 }
447420}
448421
@@ -452,57 +425,26 @@ void DiskEncryptMenuScene::doDecryptDevice(const DeviceEncryptParam ¶m)
452425 kDaemonBusPath ,
453426 kDaemonBusIface ,
454427 QDBusConnection::systemBus ());
455- if (iface.isValid ()) {
456- // Create anonymous pipe for secure credential transmission
457- int pipefd[2 ];
458- if (pipe (pipefd) == -1 ) {
459- fmCritical () << " Failed to create anonymous pipe for credentials" ;
460- return ;
461- }
462-
463- // Prepare credentials data using QDataStream for reliable serialization
464- QByteArray credentials;
465- QDataStream stream (&credentials, QIODevice::WriteOnly);
466- QVariantMap params {
467- { encrypt_param_keys::kKeyJobType , param.jobType },
468- { encrypt_param_keys::kKeyDevice , param.devDesc },
469- { encrypt_param_keys::kKeyDeviceName , param.deviceDisplayName },
470- { encrypt_param_keys::kKeyPassphrase , toBase64 (param.key ) }
471- };
472- stream << params;
473-
474- // Write credentials to pipe and close write end immediately
475- ssize_t written = write (pipefd[1 ], credentials.constData (), credentials.size ());
476- close (pipefd[1 ]); // Close write end immediately after writing
477-
478- if (written != credentials.size ()) {
479- fmCritical () << " Failed to write credentials to pipe, written:" << written << " expected:" << credentials.size ();
480- close (pipefd[0 ]);
481- return ;
482- }
483-
484- // Create file descriptor for D-Bus transmission
485- QDBusUnixFileDescriptor fd (pipefd[0 ]);
486- if (!fd.isValid ()) {
487- fmCritical () << " Failed to create valid file descriptor from pipe" ;
488- close (pipefd[0 ]);
489- return ;
490- }
491-
492- fmDebug () << " Calling Decryption D-Bus method via fd" ;
493- QDBusReply<bool > ret = iface.call (" Decryption" , QVariant::fromValue (fd));
494- if (ret.value ()) {
495- QApplication::setOverrideCursor (Qt::WaitCursor);
496- } else {
497- fmCritical () << " Decryption failed to start" ;
498- }
428+ if (!iface.isValid ()) {
429+ fmCritical () << " Failed to create D-Bus interface for decryption" ;
430+ return ;
431+ }
499432
500- // Close read end (D-Bus service will have its own copy)
501- close (pipefd[0 ]);
433+ // Prepare credentials data
434+ QVariantMap params {
435+ { encrypt_param_keys::kKeyJobType , param.jobType },
436+ { encrypt_param_keys::kKeyDevice , param.devDesc },
437+ { encrypt_param_keys::kKeyDeviceName , param.deviceDisplayName },
438+ { encrypt_param_keys::kKeyPassphrase , toBase64 (param.key ) }
439+ };
502440
441+ // Send credentials via fd
442+ fmDebug () << " Calling Decryption D-Bus method via fd" ;
443+ if (sendCredentialsViaFd (iface, " Decryption" , params, false )) {
444+ QApplication::setOverrideCursor (Qt::WaitCursor);
503445 EventsHandler::instance ()->autoStartDFM ();
504446 } else {
505- fmCritical () << " Failed to create D-Bus interface for decryption " ;
447+ fmCritical () << " Decryption failed to start " ;
506448 }
507449}
508450
@@ -658,6 +600,54 @@ QString DiskEncryptMenuScene::getBase64Of(const QString &fileName)
658600 return QString (contents.toBase64 ());
659601}
660602
603+ bool DiskEncryptMenuScene::sendCredentialsViaFd (QDBusInterface &iface, const QString &method,
604+ const QVariantMap ¶ms, bool asyncCall)
605+ {
606+ // Create anonymous pipe for secure credential transmission
607+ int pipefd[2 ];
608+ if (pipe (pipefd) == -1 ) {
609+ fmCritical () << " [sendCredentialsViaFd] Failed to create anonymous pipe for credentials" ;
610+ return false ;
611+ }
612+
613+ // Prepare credentials data using QDataStream for reliable serialization
614+ QByteArray credentials;
615+ QDataStream stream (&credentials, QIODevice::WriteOnly);
616+ stream << params;
617+
618+ // Write credentials to pipe and close write end immediately
619+ ssize_t written = write (pipefd[1 ], credentials.constData (), credentials.size ());
620+ close (pipefd[1 ]); // Close write end immediately after writing
621+
622+ if (written != credentials.size ()) {
623+ fmCritical () << " [sendCredentialsViaFd] Failed to write credentials to pipe, written:" << written << " expected:" << credentials.size ();
624+ close (pipefd[0 ]);
625+ return false ;
626+ }
627+
628+ // Create file descriptor for D-Bus transmission
629+ QDBusUnixFileDescriptor fd (pipefd[0 ]);
630+ if (!fd.isValid ()) {
631+ fmCritical () << " [sendCredentialsViaFd] Failed to create valid file descriptor from pipe" ;
632+ close (pipefd[0 ]);
633+ return false ;
634+ }
635+
636+ // Call D-Bus method with file descriptor
637+ fmDebug () << " [sendCredentialsViaFd] Calling D-Bus method:" << method << " via fd" ;
638+ if (asyncCall) {
639+ iface.asyncCall (method, QVariant::fromValue (fd));
640+ } else {
641+ QDBusReply<bool > reply = iface.call (method, QVariant::fromValue (fd));
642+ close (pipefd[0 ]);
643+ return reply.value ();
644+ }
645+
646+ // Close read end (D-Bus service will have its own copy)
647+ close (pipefd[0 ]);
648+ return true ;
649+ }
650+
661651void DiskEncryptMenuScene::onUnlocked (bool ok, dfmmount::OperationErrorInfo info, QString clearDev)
662652{
663653 QApplication::restoreOverrideCursor ();
0 commit comments