25 #include "kpgpblock.h"
30 #include <sys/types.h>
34 #include <tqapplication.h>
40 : input(), output(), error(), errMsg(), status(OK)
62 Base::run( const char *cmd, const TQString &passphrase, bool onlyReadFromPGP )
68 char str[1025] = "\0";
69 int pin[2], pout[2], perr[2], ppass[2];
74 struct pollfd pollin, pollout, pollerr;
77 if (!passphrase.isEmpty())
79 if (pipe(ppass) < 0) {
82 printf( "Something went wrong in libkpgp/kpgpbase.cpp\n");
85 pass = fdopen(ppass[1], "w");
86 TQCString pass2 = passphrase.local8Bit();
87 fwrite(pass2, sizeof( char), pass2.length(), pass);
88 fwrite( "\n", sizeof( char), 1, pass);
94 tmp.sprintf( "%d",ppass[0]);
95 ::setenv( "PGPPASSFD",tmp.data(),1);
102 ::unsetenv( "PGPPASSFD");
105 kdDebug(5100) << "pgp cmd = " << cmd << endl;
115 printf( "Something went wrong in libkpgp/kpgpbase.cpp\n");
117 if (pipe(pout) < 0) {
120 printf( "Something went wrong in libkpgp/kpgpbase.cpp\n");
122 if (pipe(perr) < 0) {
125 printf( "Something went wrong in libkpgp/kpgpbase.cpp\n");
128 TQApplication::flushX();
129 if(!(child_pid = fork()))
144 execl( "/bin/sh", "sh", "-c", cmd, ( void *)0);
154 pollout.fd = pout[0];
155 pollout.events = POLLIN;
157 pollerr.fd = perr[0];
158 pollerr.events = POLLIN;
163 pollin.events = POLLOUT;
166 if (!onlyReadFromPGP) {
167 if (!input.isEmpty()) {
169 uint input_length = input.length();
170 for ( unsigned int i=0; i<input_length; i+=len2) {
175 pollstatus = poll(&pollin, 1, 5);
176 if (pollstatus == 1) {
178 if (pollin.revents & POLLERR) {
179 kdDebug(5100) << "PGP seems to have hung up" << endl;
182 else if (pollin.revents & POLLOUT) {
184 if ((len2 = input.find( '\n', i)) == -1)
185 len2 = input_length - i;
190 len2 = write(pin[1], input.data() + i, len2);
194 else if (!pollstatus) {
198 else if (pollstatus == -1) {
199 kdDebug(5100) << "Error while polling pin[1]: "
200 << pollin.revents << endl;
207 pollstatus = poll(&pollout, 1, 0);
208 if (pollstatus == 1) {
210 if (pollout.revents & POLLIN) {
212 if ((len = read(pout[0],str,1024))>0) {
221 else if (pollstatus == -1) {
222 kdDebug(5100) << "Error while polling pout[0]: "
223 << pollout.revents << endl;
225 } while ((pollstatus == 1) && (pollout.revents & POLLIN));
232 pollstatus = poll(&pollerr, 1, 0);
233 if (pollstatus == 1) {
235 if (pollerr.revents & POLLIN) {
237 if ((len = read(perr[0],str,1024))>0) {
246 else if (pollstatus == -1) {
247 kdDebug(5100) << "Error while polling perr[0]: "
248 << pollerr.revents << endl;
250 } while ((pollstatus == 1) && (pollerr.revents & POLLIN));
254 if ((pollstatus == 1) &&
255 ((pollout.revents & POLLHUP) || (pollerr.revents & POLLHUP))) {
256 kdDebug(5100) << "PGP hung up" << endl;
262 if (write(pin[1], "\n", 1) < 0) {
265 printf( "Something went wrong in libkpgp/kpgpbase.cpp\n");
277 waitpidRetVal = waitpid(child_pid, &childExiStatus, WNOHANG);
283 pollstatus = poll(&pollout, 1, 0);
284 if (pollstatus == 1) {
286 if (pollout.revents & POLLIN) {
288 if ((len = read(pout[0],str,1024))>0) {
311 pollout.revents |= POLLHUP;
316 else if (pollstatus == -1) {
317 kdDebug(5100) << "Error while polling pout[0]: "
318 << pollout.revents << endl;
320 } while ((pollstatus == 1) && (pollout.revents & POLLIN));
327 pollstatus = poll(&pollerr, 1, 0);
328 if (pollstatus == 1) {
330 if (pollerr.revents & POLLIN) {
332 if ((len = read(perr[0],str,1024))>0) {
355 pollerr.revents |= POLLHUP;
360 else if (pollstatus == -1) {
361 kdDebug(5100) << "Error while polling perr[0]: "
362 << pollerr.revents << endl;
364 } while ((pollstatus == 1) && (pollerr.revents & POLLIN));
366 } while (waitpidRetVal == 0);
371 unsetenv( "PGPPASSFD");
372 if (!passphrase.isEmpty())
376 if (WIFEXITED(childExiStatus) != 0) {
378 childExiStatus = WEXITSTATUS(childExiStatus);
379 kdDebug(5100) << "PGP exited with exit status " << childExiStatus
384 kdDebug(5100) << "PGP exited abnormally!" << endl;
394 kdDebug(5100) << error << endl;
396 return childExiStatus;
401 Base::runGpg( const char *cmd, const TQString &passphrase, bool onlyReadFromGnuPG )
407 char str[1025] = "\0";
408 int pin[2], pout[2], perr[2], ppass[2];
413 char gpgcmd[1024] = "\0";
414 struct pollfd poller[3];
416 const int STD_OUT = 0;
417 const int STD_ERR = 1;
418 const int STD_IN = 2;
421 if (!passphrase.isEmpty())
423 if (pipe(ppass) < 0) {
426 printf( "Something went wrong in libkpgp/kpgpbase.cpp\n");
429 pass = fdopen(ppass[1], "w");
430 TQCString pass2 = passphrase.local8Bit();
431 fwrite(pass2, sizeof( char), pass2.length(), pass);
432 fwrite( "\n", sizeof( char), 1, pass);
451 printf( "Something went wrong in libkpgp/kpgpbase.cpp\n");
453 if (pipe(pout) < 0) {
456 printf( "Something went wrong in libkpgp/kpgpbase.cpp\n");
458 if (pipe(perr) < 0) {
461 printf( "Something went wrong in libkpgp/kpgpbase.cpp\n");
464 if (!passphrase.isEmpty()) {
465 if( mVersion >= "1.0.7" ) {
467 if( 0 == getenv( "GPG_AGENT_INFO") ) {
469 snprintf( gpgcmd, 1023,
470 "LANGUAGE=C gpg --no-use-agent --passphrase-fd %d %s",
475 snprintf( gpgcmd, 1023,
476 "LANGUAGE=C gpg --use-agent %s",
482 snprintf( gpgcmd, 1023,
483 "LANGUAGE=C gpg --passphrase-fd %d %s",
488 snprintf(gpgcmd, 1023, "LANGUAGE=C gpg %s",cmd);
491 TQApplication::flushX();
492 if(!(child_pid = fork()))
509 if (!passphrase.isEmpty()) {
510 if( mVersion >= "1.0.7" ) {
512 if( 0 == getenv( "GPG_AGENT_INFO") ) {
514 snprintf( gpgcmd, 1023,
515 "LANGUAGE=C gpg --no-use-agent --passphrase-fd %d %s",
520 snprintf( gpgcmd, 1023,
521 "LANGUAGE=C gpg --use-agent %s",
527 snprintf( gpgcmd, 1023,
528 "LANGUAGE=C gpg --passphrase-fd %d %s",
533 snprintf(gpgcmd, 1023, "LANGUAGE=C gpg %s",cmd);
536 kdDebug(5100) << "pgp cmd = " << gpgcmd << endl;
538 execl( "/bin/sh", "sh", "-c", gpgcmd, ( void *)0);
549 poller[STD_OUT].fd = pout[0];
550 poller[STD_OUT].events = POLLIN;
551 poller[STD_ERR].fd = perr[0];
552 poller[STD_ERR].events = POLLIN;
555 if (!onlyReadFromGnuPG) {
557 poller[STD_IN].fd = pin[1];
558 poller[STD_IN].events = POLLOUT;
566 unsigned int input_pos = 0;
567 uint input_length = input.length();
572 waitpidRetVal = waitpid(child_pid, &childExiStatus, WNOHANG);
576 pollstatus = poll(poller, num_pollers, 10);
577 if( 0 < pollstatus ) {
579 if (poller[STD_OUT].revents & POLLIN) {
581 if ((len = read(pout[0],str,1024))>0) {
604 poller[STD_OUT].revents |= POLLHUP;
605 poller[STD_OUT].events = 0;
607 } else if (poller[STD_OUT].revents & POLLHUP) {
609 poller[STD_OUT].events = 0;
613 if (poller[STD_ERR].revents & POLLIN) {
615 if ((len = read(poller[STD_ERR].fd,str,1024))>0) {
622 poller[STD_ERR].revents |= POLLHUP;
623 poller[STD_ERR].events = 0;
625 } else if (poller[STD_ERR].revents & POLLHUP) {
627 poller[STD_ERR].events = 0;
630 if (num_pollers > 2) {
631 if (poller[STD_IN].revents & ( POLLERR | POLLHUP ) ) {
632 kdDebug(5100) << "GnuPG seems to have hung up" << endl;
637 else if (poller[STD_IN].revents & POLLOUT) {
638 if (!input.isEmpty()) {
640 if ((len2 = input.find( '\n', input_pos)) == -1)
641 len2 = input_length - input_pos;
643 len2 = len2 - input_pos + 1;
646 len2 = write(pin[1], input.data() + input_pos, len2 );
651 if (input_pos >= input_length) {
659 if (write(pin[1], "\n", 1) < 0) {
662 printf( "Something went wrong in libkpgp/kpgpbase.cpp\n");
672 } while ( (pollstatus > 0) && ( (num_pollers > 2)
673 || (poller[STD_OUT].events != 0)
674 || (poller[STD_ERR].events != 0) ) );
676 if (pollstatus == -1) {
677 kdDebug(5100) << "GnuPG poll failed, errno: " << errno << endl;
680 } while(waitpidRetVal == 0);
687 if (!passphrase.isEmpty())
691 if (WIFEXITED(childExiStatus) != 0) {
693 childExiStatus = WEXITSTATUS(childExiStatus);
694 kdDebug(5100) << "GnuPG exited with exit status " << childExiStatus
699 kdDebug(5100) << "GnuPG exited abnormally!" << endl;
707 kdDebug(5100) << "gpg stderr:\n" << error << endl;
709 return childExiStatus;
717 TQCString pgpUser = Module::getKpgp()->user();
719 if(!pgpUser.isEmpty())
|