From 9d62bacac7826cb73bb18fac7268f3d2df662d7b Mon Sep 17 00:00:00 2001 From: Ben Kibbey Date: Sat, 3 Sep 2016 19:06:36 -0400 Subject: [PATCH] core: Check for GPG_TTY as well as DISPLAY. * src/engine-assuan.c (llass_new): Update --ttyname from GPG_TTY. * src/engine-g13.c (g13_new): Ditto. * src/engine-gpg.c (gpg_new): Ditto. * src/engine-uiserver.c (uiserver_new): Ditto. * src/engine-gpgsm.c (gpgsm_new): Ditto. -- Useful when no DISPLAY is set and there is no attached terminal to the current process. Signed-off-by: Ben Kibbey --- src/engine-assuan.c | 18 +++++++++++++++--- src/engine-g13.c | 16 +++++++++++++--- src/engine-gpg.c | 16 +++++++++++++--- src/engine-gpgsm.c | 16 +++++++++++++--- src/engine-uiserver.c | 16 +++++++++++++--- 5 files changed, 67 insertions(+), 15 deletions(-) diff --git a/src/engine-assuan.c b/src/engine-assuan.c index f5e202a9..65924eb2 100644 --- a/src/engine-assuan.c +++ b/src/engine-assuan.c @@ -219,6 +219,7 @@ llass_new (void **engine, const char *file_name, const char *home_dir, gpgme_error_t err = 0; engine_llass_t llass; char *optstr; + char *env_tty = NULL; (void)version; /* Not yet used. */ @@ -280,13 +281,24 @@ llass_new (void **engine, const char *file_name, const char *home_dir, } } - if (llass->opt.gpg_agent && isatty (1)) + if (llass->opt.gpg_agent) + err = _gpgme_getenv ("GPG_TTY", &env_tty); + + if (llass->opt.gpg_agent && (isatty (1) || env_tty || err)) { - int rc; + int rc = 0; char dft_ttyname[64]; char *dft_ttytype = NULL; - rc = ttyname_r (1, dft_ttyname, sizeof (dft_ttyname)); + if (err) + goto leave; + else if (env_tty) + { + snprintf (dft_ttyname, sizeof (dft_ttyname), "%s", env_tty); + free (env_tty); + } + else + rc = ttyname_r (1, dft_ttyname, sizeof (dft_ttyname)); /* Even though isatty() returns 1, ttyname_r() may fail in many ways, e.g., when /dev/pts is not accessible under chroot. */ diff --git a/src/engine-g13.c b/src/engine-g13.c index 313e2ad3..d34db829 100644 --- a/src/engine-g13.c +++ b/src/engine-g13.c @@ -222,6 +222,7 @@ g13_new (void **engine, const char *file_name, const char *home_dir, const char *argv[5]; char *dft_display = NULL; char dft_ttyname[64]; + char *env_tty = NULL; char *dft_ttytype = NULL; char *optstr; @@ -284,11 +285,20 @@ g13_new (void **engine, const char *file_name, const char *home_dir, goto leave; } - if (isatty (1)) + err = _gpgme_getenv ("GPG_TTY", &env_tty); + if (isatty (1) || env_tty || err) { - int rc; + int rc = 0; - rc = ttyname_r (1, dft_ttyname, sizeof (dft_ttyname)); + if (err) + goto leave; + else if (env_tty) + { + snprintf (dft_ttyname, sizeof (dft_ttyname), "%s", env_tty); + free (env_tty); + } + else + rc = ttyname_r (1, dft_ttyname, sizeof (dft_ttyname)); /* Even though isatty() returns 1, ttyname_r() may fail in many ways, e.g., when /dev/pts is not accessible under chroot. */ diff --git a/src/engine-gpg.c b/src/engine-gpg.c index 025657a9..8bb348f9 100644 --- a/src/engine-gpg.c +++ b/src/engine-gpg.c @@ -454,6 +454,7 @@ gpg_new (void **engine, const char *file_name, const char *home_dir, char *dft_display = NULL; char dft_ttyname[64]; char *dft_ttytype = NULL; + char *env_tty = NULL; gpg = calloc (1, sizeof *gpg); if (!gpg) @@ -560,11 +561,20 @@ gpg_new (void **engine, const char *file_name, const char *home_dir, goto leave; } - if (isatty (1)) + rc = _gpgme_getenv ("GPG_TTY", &env_tty); + if (isatty (1) || env_tty || rc) { - int err; + int err = 0; - err = ttyname_r (1, dft_ttyname, sizeof (dft_ttyname)); + if (rc) + goto leave; + else if (env_tty) + { + snprintf (dft_ttyname, sizeof (dft_ttyname), "%s", env_tty); + free (env_tty); + } + else + err = ttyname_r (1, dft_ttyname, sizeof (dft_ttyname)); /* Even though isatty() returns 1, ttyname_r() may fail in many ways, e.g., when /dev/pts is not accessible under chroot. */ diff --git a/src/engine-gpgsm.c b/src/engine-gpgsm.c index 49cf061d..e7e2a20d 100644 --- a/src/engine-gpgsm.c +++ b/src/engine-gpgsm.c @@ -253,6 +253,7 @@ gpgsm_new (void **engine, const char *file_name, const char *home_dir, #endif char *dft_display = NULL; char dft_ttyname[64]; + char *env_tty = NULL; char *dft_ttytype = NULL; char *optstr; @@ -410,11 +411,20 @@ gpgsm_new (void **engine, const char *file_name, const char *home_dir, goto leave; } - if (isatty (1)) + err = _gpgme_getenv ("GPG_TTY", &env_tty); + if (isatty (1) || env_tty || err) { - int rc; + int rc = 0; - rc = ttyname_r (1, dft_ttyname, sizeof (dft_ttyname)); + if (err) + goto leave; + else if (env_tty) + { + snprintf (dft_ttyname, sizeof (dft_ttyname), "%s", env_tty); + free (env_tty); + } + else + rc = ttyname_r (1, dft_ttyname, sizeof (dft_ttyname)); /* Even though isatty() returns 1, ttyname_r() may fail in many ways, e.g., when /dev/pts is not accessible under chroot. */ diff --git a/src/engine-uiserver.c b/src/engine-uiserver.c index d855c742..63e77de6 100644 --- a/src/engine-uiserver.c +++ b/src/engine-uiserver.c @@ -248,6 +248,7 @@ uiserver_new (void **engine, const char *file_name, const char *home_dir, engine_uiserver_t uiserver; char *dft_display = NULL; char dft_ttyname[64]; + char *env_tty = NULL; char *dft_ttytype = NULL; char *optstr; @@ -330,11 +331,20 @@ uiserver_new (void **engine, const char *file_name, const char *home_dir, goto leave; } - if (isatty (1)) + err = _gpgme_getenv ("GPG_TTY", &env_tty); + if (isatty (1) || env_tty || err) { - int rc; + int rc = 0; - rc = ttyname_r (1, dft_ttyname, sizeof (dft_ttyname)); + if (err) + goto leave; + else if (env_tty) + { + snprintf (dft_ttyname, sizeof (dft_ttyname), "%s", env_tty); + free (env_tty); + } + else + rc = ttyname_r (1, dft_ttyname, sizeof (dft_ttyname)); /* Even though isatty() returns 1, ttyname_r() may fail in many ways, e.g., when /dev/pts is not accessible under chroot. */