19 #include <config-libkpgp.h>
28 #include <QApplication>
35 #ifdef HAVE_SYS_POLL_H
39 #include <sys/types.h>
45 : input(), output(), error(), errMsg(), status(
OK)
67 Base::run(
const char *cmd,
const char *passphrase,
bool onlyReadFromPGP )
69 #ifdef HAVE_SYS_POLL_H
74 char str[1025] =
"\0";
75 int pin[2], pout[2], perr[2], ppass[2];
80 struct pollfd pollin, pollout, pollerr;
87 pass = fdopen(ppass[1],
"w");
88 fwrite(passphrase,
sizeof(
char), strlen(passphrase), pass);
89 fwrite(
"\n",
sizeof(
char), 1, pass);
95 tmp.sprintf(
"%d",ppass[0]);
96 ::setenv(
"PGPPASSFD",tmp.toUtf8(),1);
103 ::unsetenv(
"PGPPASSFD");
106 kDebug( 5326 ) <<
"pgp cmd =" << cmd;
117 QApplication::flush();
118 if(!(child_pid = fork()))
133 execl(
"/bin/sh",
"sh",
"-c", cmd, (
void *)0);
143 pollout.fd = pout[0];
144 pollout.events = POLLIN;
146 pollerr.fd = perr[0];
147 pollerr.events = POLLIN;
152 pollin.events = POLLOUT;
155 if (!onlyReadFromPGP) {
156 if (!
input.isEmpty()) {
158 int input_length =
input.length();
159 for (
int i=0; i<input_length; i+=len2) {
164 pollstatus = poll(&pollin, 1, 5);
165 if (pollstatus == 1) {
167 if (pollin.revents & POLLERR) {
168 kDebug( 5326 ) <<
"PGP seems to have hung up";
171 else if (pollin.revents & POLLOUT) {
173 if ((len2 =
input.indexOf(
'\n', i)) == -1)
174 len2 = input_length - i;
179 len2 = write(pin[1],
input.data() + i, len2);
183 else if (!pollstatus) {
187 else if (pollstatus == -1) {
188 kDebug( 5326 ) <<
"Error while polling pin[1]:"
196 pollstatus = poll(&pollout, 1, 0);
197 if (pollstatus == 1) {
199 if (pollout.revents & POLLIN) {
201 if ((len = read(pout[0],str,1024))>0) {
210 else if (pollstatus == -1) {
211 kDebug( 5326 ) <<
"Error while polling pout[0]:"
214 }
while ((pollstatus == 1) && (pollout.revents & POLLIN));
221 pollstatus = poll(&pollerr, 1, 0);
222 if (pollstatus == 1) {
224 if (pollerr.revents & POLLIN) {
226 if ((len = read(perr[0],str,1024))>0) {
235 else if (pollstatus == -1) {
236 kDebug( 5326 ) <<
"Error while polling perr[0]:"
239 }
while ((pollstatus == 1) && (pollerr.revents & POLLIN));
243 if ((pollstatus == 1) &&
244 ((pollout.revents & POLLHUP) || (pollerr.revents & POLLHUP))) {
245 kDebug( 5326 ) <<
"PGP hung up";
251 write(pin[1],
"\n", 1);
261 waitpidRetVal = waitpid(child_pid, &childExitStatus, WNOHANG);
267 pollstatus = poll(&pollout, 1, 0);
268 if (pollstatus == 1) {
270 if (pollout.revents & POLLIN) {
272 if ((len = read(pout[0],str,1024))>0) {
295 pollout.revents |= POLLHUP;
300 else if (pollstatus == -1) {
301 kDebug( 5326 ) <<
"Error while polling pout[0]:"
304 }
while ((pollstatus == 1) && (pollout.revents & POLLIN));
311 pollstatus = poll(&pollerr, 1, 0);
312 if (pollstatus == 1) {
314 if (pollerr.revents & POLLIN) {
316 if ((len = read(perr[0],str,1024))>0) {
339 pollerr.revents |= POLLHUP;
344 else if (pollstatus == -1) {
345 kDebug( 5326 ) <<
"Error while polling perr[0]:"
348 }
while ((pollstatus == 1) && (pollerr.revents & POLLIN));
350 }
while (waitpidRetVal == 0);
355 unsetenv(
"PGPPASSFD");
360 if (WIFEXITED(childExitStatus) != 0) {
362 childExitStatus = WEXITSTATUS(childExitStatus);
363 kDebug( 5326 ) <<
"PGP exited with exit status" << childExitStatus;
366 childExitStatus = -1;
367 kDebug( 5326 ) <<
"PGP exited abnormally!";
377 kDebug( 5326 ) <<
error;
379 return childExitStatus;
380 #else // HAVE_SYS_POLL_H
382 #warning WIN32 libkpgp: PGP support not ported to win32!
390 Base::runGpg(
const char *cmd,
const char *passphrase,
bool onlyReadFromGnuPG )
392 #ifdef HAVE_SYS_POLL_H
397 char str[1025] =
"\0";
398 int pin[2], pout[2], perr[2], ppass[2];
403 char gpgcmd[1024] =
"\0";
404 struct pollfd poller[3];
406 const int STD_OUT = 0;
407 const int STD_ERR = 1;
408 const int STD_IN = 2;
415 pass = fdopen(ppass[1],
"w");
416 fwrite(passphrase,
sizeof(
char), strlen(passphrase), pass);
417 fwrite(
"\n",
sizeof(
char), 1, pass);
440 if( 0 == getenv(
"GPG_AGENT_INFO") ) {
442 snprintf( gpgcmd, 1023,
443 "LANGUAGE=C gpg --no-use-agent --passphrase-fd %d %s",
448 snprintf( gpgcmd, 1023,
449 "LANGUAGE=C gpg --use-agent %s",
455 snprintf( gpgcmd, 1023,
456 "LANGUAGE=C gpg --passphrase-fd %d %s",
461 snprintf(gpgcmd, 1023,
"LANGUAGE=C gpg %s",cmd);
464 QApplication::flush();
465 if(!(child_pid = fork()))
485 if( 0 == getenv(
"GPG_AGENT_INFO") ) {
487 snprintf( gpgcmd, 1023,
488 "LANGUAGE=C gpg --no-use-agent --passphrase-fd %d %s",
493 snprintf( gpgcmd, 1023,
494 "LANGUAGE=C gpg --use-agent %s",
500 snprintf( gpgcmd, 1023,
501 "LANGUAGE=C gpg --passphrase-fd %d %s",
506 snprintf(gpgcmd, 1023,
"LANGUAGE=C gpg %s",cmd);
509 kDebug( 5326 ) <<
"pgp cmd =" << gpgcmd;
511 execl(
"/bin/sh",
"sh",
"-c", gpgcmd, (
void *)0);
522 poller[STD_OUT].fd = pout[0];
523 poller[STD_OUT].events = POLLIN;
524 poller[STD_ERR].fd = perr[0];
525 poller[STD_ERR].events = POLLIN;
528 if (!onlyReadFromGnuPG) {
530 poller[STD_IN].fd = pin[1];
531 poller[STD_IN].events = POLLOUT;
540 int input_length =
input.length();
545 waitpidRetVal = waitpid(child_pid, &childExitStatus, WNOHANG);
549 pollstatus = poll(poller, num_pollers, 10);
550 if( 0 < pollstatus ) {
552 if (poller[STD_OUT].revents & POLLIN) {
554 if ((len = read(pout[0],str,1024))>0) {
577 poller[STD_OUT].revents |= POLLHUP;
578 poller[STD_OUT].events = 0;
580 }
else if (poller[STD_OUT].revents & POLLHUP) {
582 poller[STD_OUT].events = 0;
586 if (poller[STD_ERR].revents & POLLIN) {
588 if ((len = read(poller[STD_ERR].fd,str,1024))>0) {
595 poller[STD_ERR].revents |= POLLHUP;
596 poller[STD_ERR].events = 0;
598 }
else if (poller[STD_ERR].revents & POLLHUP) {
600 poller[STD_ERR].events = 0;
603 if (num_pollers > 2) {
604 if (poller[STD_IN].revents & ( POLLERR | POLLHUP ) ) {
605 kDebug( 5326 ) <<
"GnuPG seems to have hung up";
610 else if (poller[STD_IN].revents & POLLOUT) {
611 if (!
input.isEmpty()) {
613 if ((len2 =
input.indexOf(
'\n', input_pos)) == -1)
614 len2 = input_length - input_pos;
616 len2 = len2 - input_pos + 1;
619 len2 = write(pin[1],
input.data() + input_pos, len2 );
624 if (input_pos >= input_length) {
632 write(pin[1],
"\n", 1);
641 }
while ( (pollstatus > 0) && ( (num_pollers > 2)
642 || (poller[STD_OUT].events != 0)
643 || (poller[STD_ERR].events != 0) ) );
645 if (pollstatus == -1) {
646 kDebug( 5326 ) <<
"GnuPG poll failed, errno:" << errno;
649 }
while(waitpidRetVal == 0);
660 if (WIFEXITED(childExitStatus) != 0) {
662 childExitStatus = WEXITSTATUS(childExitStatus);
663 kDebug( 5326 ) <<
"GnuPG exited with exit status" << childExitStatus;
666 childExitStatus = -1;
667 kDebug( 5326 ) <<
"GnuPG exited abnormally!";
675 kDebug( 5326 ) <<
"gpg stderr:" <<
error;
677 return childExitStatus;
678 #else // HAVE_SYS_POLL_H
680 #warning WIN32 libkpgp: GnuPG support not ported to WIN32!
693 if(!pgpUser.isEmpty())
const KeyID user() const
Returns the actual key ID of the currently set key.
static Kpgp::Module * getKpgp()
return the actual pgp object
virtual int run(const char *cmd, const char *passphrase=0, bool onlyReadFromPGP=false)
Base()
virtual class used internally by kpgp
virtual int runGpg(const char *cmd, const char *passphrase=0, bool onlyReadFromGnuPG=false)