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 char *passphrase, bool onlyReadFromPGP )
68 char str[1025] = "\0";
69 int pin[2], pout[2], perr[2], ppass[2];
74 struct pollfd pollin, pollout, pollerr;
79 if (pipe(ppass) < 0) {
82 printf( "Something went wrong in libkpgp/kpgpbase.cpp\n");
85 pass = fdopen(ppass[1], "w");
86 fwrite(passphrase, sizeof( char), strlen(passphrase), pass);
87 fwrite( "\n", sizeof( char), 1, pass);
93 tmp.sprintf( "%d",ppass[0]);
94 ::setenv( "PGPPASSFD",tmp.data(),1);
101 ::unsetenv( "PGPPASSFD");
104 kdDebug(5100) << "pgp cmd = " << cmd << endl;
114 printf( "Something went wrong in libkpgp/kpgpbase.cpp\n");
116 if (pipe(pout) < 0) {
119 printf( "Something went wrong in libkpgp/kpgpbase.cpp\n");
121 if (pipe(perr) < 0) {
124 printf( "Something went wrong in libkpgp/kpgpbase.cpp\n");
127 TQApplication::flushX();
128 if(!(child_pid = fork()))
143 execl( "/bin/sh", "sh", "-c", cmd, ( void *)0);
153 pollout.fd = pout[0];
154 pollout.events = POLLIN;
156 pollerr.fd = perr[0];
157 pollerr.events = POLLIN;
162 pollin.events = POLLOUT;
165 if (!onlyReadFromPGP) {
166 if (!input.isEmpty()) {
168 uint input_length = input.length();
169 for ( unsigned int i=0; i<input_length; i+=len2) {
174 pollstatus = poll(&pollin, 1, 5);
175 if (pollstatus == 1) {
177 if (pollin.revents & POLLERR) {
178 kdDebug(5100) << "PGP seems to have hung up" << endl;
181 else if (pollin.revents & POLLOUT) {
183 if ((len2 = input.find( '\n', i)) == -1)
184 len2 = input_length - i;
189 len2 = write(pin[1], input.data() + i, len2);
193 else if (!pollstatus) {
197 else if (pollstatus == -1) {
198 kdDebug(5100) << "Error while polling pin[1]: "
199 << pollin.revents << endl;
206 pollstatus = poll(&pollout, 1, 0);
207 if (pollstatus == 1) {
209 if (pollout.revents & POLLIN) {
211 if ((len = read(pout[0],str,1024))>0) {
220 else if (pollstatus == -1) {
221 kdDebug(5100) << "Error while polling pout[0]: "
222 << pollout.revents << endl;
224 } while ((pollstatus == 1) && (pollout.revents & POLLIN));
231 pollstatus = poll(&pollerr, 1, 0);
232 if (pollstatus == 1) {
234 if (pollerr.revents & POLLIN) {
236 if ((len = read(perr[0],str,1024))>0) {
245 else if (pollstatus == -1) {
246 kdDebug(5100) << "Error while polling perr[0]: "
247 << pollerr.revents << endl;
249 } while ((pollstatus == 1) && (pollerr.revents & POLLIN));
253 if ((pollstatus == 1) &&
254 ((pollout.revents & POLLHUP) || (pollerr.revents & POLLHUP))) {
255 kdDebug(5100) << "PGP hung up" << endl;
261 if (write(pin[1], "\n", 1) < 0) {
264 printf( "Something went wrong in libkpgp/kpgpbase.cpp\n");
276 waitpidRetVal = waitpid(child_pid, &childExiStatus, WNOHANG);
282 pollstatus = poll(&pollout, 1, 0);
283 if (pollstatus == 1) {
285 if (pollout.revents & POLLIN) {
287 if ((len = read(pout[0],str,1024))>0) {
310 pollout.revents |= POLLHUP;
315 else if (pollstatus == -1) {
316 kdDebug(5100) << "Error while polling pout[0]: "
317 << pollout.revents << endl;
319 } while ((pollstatus == 1) && (pollout.revents & POLLIN));
326 pollstatus = poll(&pollerr, 1, 0);
327 if (pollstatus == 1) {
329 if (pollerr.revents & POLLIN) {
331 if ((len = read(perr[0],str,1024))>0) {
354 pollerr.revents |= POLLHUP;
359 else if (pollstatus == -1) {
360 kdDebug(5100) << "Error while polling perr[0]: "
361 << pollerr.revents << endl;
363 } while ((pollstatus == 1) && (pollerr.revents & POLLIN));
365 } while (waitpidRetVal == 0);
370 unsetenv( "PGPPASSFD");
375 if (WIFEXITED(childExiStatus) != 0) {
377 childExiStatus = WEXITSTATUS(childExiStatus);
378 kdDebug(5100) << "PGP exited with exit status " << childExiStatus
383 kdDebug(5100) << "PGP exited abnormally!" << endl;
393 kdDebug(5100) << error << endl;
395 return childExiStatus;
400 Base::runGpg( const char *cmd, const char *passphrase, bool onlyReadFromGnuPG )
406 char str[1025] = "\0";
407 int pin[2], pout[2], perr[2], ppass[2];
412 char gpgcmd[1024] = "\0";
413 struct pollfd poller[3];
415 const int STD_OUT = 0;
416 const int STD_ERR = 1;
417 const int STD_IN = 2;
422 if (pipe(ppass) < 0) {
425 printf( "Something went wrong in libkpgp/kpgpbase.cpp\n");
428 pass = fdopen(ppass[1], "w");
429 fwrite(passphrase, sizeof( char), strlen(passphrase), pass);
430 fwrite( "\n", sizeof( char), 1, pass);
449 printf( "Something went wrong in libkpgp/kpgpbase.cpp\n");
451 if (pipe(pout) < 0) {
454 printf( "Something went wrong in libkpgp/kpgpbase.cpp\n");
456 if (pipe(perr) < 0) {
459 printf( "Something went wrong in libkpgp/kpgpbase.cpp\n");
463 if( mVersion >= "1.0.7" ) {
465 if( 0 == getenv( "GPG_AGENT_INFO") ) {
467 snprintf( gpgcmd, 1023,
468 "LANGUAGE=C gpg --no-use-agent --passphrase-fd %d %s",
473 snprintf( gpgcmd, 1023,
474 "LANGUAGE=C gpg --use-agent %s",
480 snprintf( gpgcmd, 1023,
481 "LANGUAGE=C gpg --passphrase-fd %d %s",
486 snprintf(gpgcmd, 1023, "LANGUAGE=C gpg %s",cmd);
489 TQApplication::flushX();
490 if(!(child_pid = fork()))
508 if( mVersion >= "1.0.7" ) {
510 if( 0 == getenv( "GPG_AGENT_INFO") ) {
512 snprintf( gpgcmd, 1023,
513 "LANGUAGE=C gpg --no-use-agent --passphrase-fd %d %s",
518 snprintf( gpgcmd, 1023,
519 "LANGUAGE=C gpg --use-agent %s",
525 snprintf( gpgcmd, 1023,
526 "LANGUAGE=C gpg --passphrase-fd %d %s",
531 snprintf(gpgcmd, 1023, "LANGUAGE=C gpg %s",cmd);
534 kdDebug(5100) << "pgp cmd = " << gpgcmd << endl;
536 execl( "/bin/sh", "sh", "-c", gpgcmd, ( void *)0);
547 poller[STD_OUT].fd = pout[0];
548 poller[STD_OUT].events = POLLIN;
549 poller[STD_ERR].fd = perr[0];
550 poller[STD_ERR].events = POLLIN;
553 if (!onlyReadFromGnuPG) {
555 poller[STD_IN].fd = pin[1];
556 poller[STD_IN].events = POLLOUT;
564 unsigned int input_pos = 0;
565 uint input_length = input.length();
570 waitpidRetVal = waitpid(child_pid, &childExiStatus, WNOHANG);
574 pollstatus = poll(poller, num_pollers, 10);
575 if( 0 < pollstatus ) {
577 if (poller[STD_OUT].revents & POLLIN) {
579 if ((len = read(pout[0],str,1024))>0) {
602 poller[STD_OUT].revents |= POLLHUP;
603 poller[STD_OUT].events = 0;
605 } else if (poller[STD_OUT].revents & POLLHUP) {
607 poller[STD_OUT].events = 0;
611 if (poller[STD_ERR].revents & POLLIN) {
613 if ((len = read(poller[STD_ERR].fd,str,1024))>0) {
620 poller[STD_ERR].revents |= POLLHUP;
621 poller[STD_ERR].events = 0;
623 } else if (poller[STD_ERR].revents & POLLHUP) {
625 poller[STD_ERR].events = 0;
628 if (num_pollers > 2) {
629 if (poller[STD_IN].revents & ( POLLERR | POLLHUP ) ) {
630 kdDebug(5100) << "GnuPG seems to have hung up" << endl;
635 else if (poller[STD_IN].revents & POLLOUT) {
636 if (!input.isEmpty()) {
638 if ((len2 = input.find( '\n', input_pos)) == -1)
639 len2 = input_length - input_pos;
641 len2 = len2 - input_pos + 1;
644 len2 = write(pin[1], input.data() + input_pos, len2 );
649 if (input_pos >= input_length) {
657 if (write(pin[1], "\n", 1) < 0) {
660 printf( "Something went wrong in libkpgp/kpgpbase.cpp\n");
670 } while ( (pollstatus > 0) && ( (num_pollers > 2)
671 || (poller[STD_OUT].events != 0)
672 || (poller[STD_ERR].events != 0) ) );
674 if (pollstatus == -1) {
675 kdDebug(5100) << "GnuPG poll failed, errno: " << errno << endl;
678 } while(waitpidRetVal == 0);
689 if (WIFEXITED(childExiStatus) != 0) {
691 childExiStatus = WEXITSTATUS(childExiStatus);
692 kdDebug(5100) << "GnuPG exited with exit status " << childExiStatus
697 kdDebug(5100) << "GnuPG exited abnormally!" << endl;
705 kdDebug(5100) << "gpg stderr:\n" << error << endl;
707 return childExiStatus;
715 TQCString pgpUser = Module::getKpgp()->user();
717 if(!pgpUser.isEmpty())
|