aboutsummaryrefslogtreecommitdiffstats
path: root/sm/passphrase.c
diff options
context:
space:
mode:
authorJustus Winter <[email protected]>2016-03-07 17:09:41 +0000
committerJustus Winter <[email protected]>2016-03-07 17:16:37 +0000
commiteea139c56ef55081d8cd8df2a35ce507386e0f17 (patch)
tree1cda1181beca082d53b439311b84b7639e940a8c /sm/passphrase.c
parentsm: Remove unused argument '--fixed-passphrase'. (diff)
downloadgnupg-eea139c56ef55081d8cd8df2a35ce507386e0f17.tar.gz
gnupg-eea139c56ef55081d8cd8df2a35ce507386e0f17.zip
sm: Implement pinentry loopback and reading passphrases from fd.
* doc/gpgsm.texi: Document '--pinentry-mode' and '--passphrase-fd'. * sm/Makefile.am (gpgsm_SOURCES): Add new files * sm/call-agent.c (struct default_inq_parm_s): New definition. (start_agent): Pass in the pinentry mode. (default_inq_cb): Handle 'PASSPHRASE' and 'NEW_PASSPHRASE' inquiries. Adapt all call sites to the new callback cookie. * sm/gpgsm.c (cmd_and_opt_values): Add new values. (opts): Add new options. (main): Handle new options. * sm/gpgsm.h (struct opt): Add field 'pinentry_mode'. * sm/passphrase.c: New file. * sm/passphrase.h: Likewise. GnuPG-bug-id: 1970 Signed-off-by: Justus Winter <[email protected]>
Diffstat (limited to 'sm/passphrase.c')
-rw-r--r--sm/passphrase.c90
1 files changed, 90 insertions, 0 deletions
diff --git a/sm/passphrase.c b/sm/passphrase.c
new file mode 100644
index 000000000..6ad2b0a20
--- /dev/null
+++ b/sm/passphrase.c
@@ -0,0 +1,90 @@
+/* passphrase.c - Get a passphrase
+ * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
+ * 2005, 2006, 2007, 2009, 2011 Free Software Foundation, Inc.
+ *
+ * This file is part of GnuPG.
+ *
+ * GnuPG is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * GnuPG is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include <unistd.h>
+
+#include "passphrase.h"
+#include "gpgsm.h"
+#include "../common/shareddefs.h"
+#include "../common/ttyio.h"
+
+static char *fd_passwd = NULL;
+
+int
+have_static_passphrase ()
+{
+ return (!!fd_passwd
+ && (opt.batch || opt.pinentry_mode == PINENTRY_MODE_LOOPBACK));
+}
+
+/* Return a static passphrase. The returned value is only valid as
+ long as no other passphrase related function is called. NULL may
+ be returned if no passphrase has been set; better use
+ have_static_passphrase first. */
+const char *
+get_static_passphrase (void)
+{
+ return fd_passwd;
+}
+
+void
+read_passphrase_from_fd (int fd)
+{
+ int i, len;
+ char *pw;
+
+ if (!opt.batch && opt.pinentry_mode != PINENTRY_MODE_LOOPBACK)
+ { /* Not used but we have to do a dummy read, so that it won't end
+ up at the begin of the message if the quite usual trick to
+ prepend the passphtrase to the message is used. */
+ char buf[1];
+
+ while (!(read (fd, buf, 1) != 1 || *buf == '\n'))
+ ;
+ *buf = 0;
+ return;
+ }
+
+ for (pw = NULL, i = len = 100; ; i++)
+ {
+ if (i >= len-1)
+ {
+ char *pw2 = pw;
+ len += 100;
+ pw = xmalloc_secure (len);
+ if (pw2)
+ {
+ memcpy (pw, pw2, i);
+ xfree (pw2);
+ }
+ else
+ i = 0;
+ }
+ if (read (fd, pw+i, 1) != 1 || pw[i] == '\n')
+ break;
+ }
+ pw[i] = 0;
+ if (!opt.batch && opt.pinentry_mode != PINENTRY_MODE_LOOPBACK)
+ tty_printf("\b\b\b \n" );
+
+ xfree (fd_passwd);
+ fd_passwd = pw;
+}