1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2007 Ben Kibbey <bjk@luxsci.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #define _ASSUAN_ONLY_GPG_ERRORS 1
36 #include "pwmd_error.h"
43 static gpg_error_t
set_pinentry_strings(struct pinentry_s
*pin
, int which
);
45 static int mem_realloc_cb(void *data
, const void *buffer
, size_t len
)
47 membuf_t
*mem
= (membuf_t
*)data
;
53 if ((p
= xrealloc(mem
->buf
, mem
->len
+ len
)) == NULL
)
57 memcpy((char *)mem
->buf
+ mem
->len
, buffer
, len
);
62 static gpg_error_t
assuan_command(struct pinentry_s
*pin
, gchar
**result
,
71 rc
= assuan_transact(pin
->ctx
, cmd
, mem_realloc_cb
, &data
, NULL
, NULL
,
82 mem_realloc_cb(&data
, "", 1);
83 *result
= (gchar
*)data
.buf
;
90 static gpg_error_t
set_pinentry_options(struct pinentry_s
*pin
)
92 gchar
*display
= getenv("DISPLAY");
93 gint have_display
= 0;
94 gchar
*tty
= NULL
, *ttytype
= NULL
;
98 gchar cmd
[ASSUAN_LINELENGTH
];
100 if (pin
->display
|| display
)
103 tty
= pin
->ttyname
? pin
->ttyname
: ttyname(STDOUT_FILENO
);
106 return GPG_ERR_CANCELED
;
109 if (!have_display
&& !tty
)
110 return GPG_ERR_CANCELED
;
113 gchar
*p
= getenv("TERM");
115 ttytype
= pin
->ttytype
? pin
->ttytype
: p
;
118 return GPG_ERR_CANCELED
;
121 opt
= have_display
? "DISPLAY" : "TTYNAME";
122 val
= have_display
? pin
->display
? pin
->display
: display
: tty
;
123 snprintf(cmd
, sizeof(cmd
), "OPTION %s=%s", g_ascii_strdown(opt
, strlen(opt
)), val
);
124 rc
= assuan_command(pin
, &result
, cmd
);
130 snprintf(cmd
, sizeof(cmd
), "OPTION ttytype=%s", ttytype
);
131 rc
= assuan_command(pin
, &result
, cmd
);
137 static gpg_error_t
launch_pinentry(struct pinentry_s
*pin
)
140 assuan_context_t ctx
;
141 gint child_list
[] = {-1};
142 const gchar
*argv
[] = { "pinentry", NULL
};
144 rc
= assuan_pipe_connect(&ctx
, pin
->path
? pin
->path
: PINENTRY_PATH
,
150 pin
->pid
= assuan_get_pid(ctx
);
152 rc
= set_pinentry_options(pin
);
153 return rc
? rc
: set_pinentry_strings(pin
, 0);
156 static gpg_error_t
pinentry_command(struct pinentry_s
*pin
, gchar
**result
,
159 gpg_error_t error
= 0;
162 error
= launch_pinentry(pin
);
164 return error
? error
: assuan_command(pin
, result
, cmd
);
167 static gpg_error_t
set_pinentry_strings(struct pinentry_s
*pin
, int which
)
174 title
= g_strdup(N_("Password mismatch, please try again."));
175 else if (!pin
->title
)
176 title
= pin
->title
= g_strdup(N_("Password Manager Daemon"));
181 pin
->prompt
= g_strdup(N_("Password:"));
183 if (!pin
->desc
&& !which
)
184 pin
->desc
= g_strdup_printf(pin
->which
== PINENTRY_OPEN
?
185 N_("A password is required to open the file \"%s\". Please%%0Aenter the password below.") :
186 N_("A password is required to save to the file \"%s\". Please%%0Aenter the password below."),
190 buf
= g_strdup_printf("SETERROR %s", N_("Please type the password again for confirmation."));
192 buf
= g_strdup_printf("SETERROR %s", pin
->desc
);
194 error
= pinentry_command(pin
, NULL
, buf
);
200 buf
= g_strdup_printf("SETPROMPT %s", pin
->prompt
);
201 error
= pinentry_command(pin
, NULL
, buf
);
207 buf
= g_strdup_printf("SETDESC %s", title
);
208 error
= pinentry_command(pin
, NULL
, buf
);
218 static void pinentry_disconnect(struct pinentry_s
*pin
)
224 assuan_disconnect(pin
->ctx
);
230 static gpg_error_t
do_getpin(struct pinentry_s
*pin
, char **result
)
235 error
= pinentry_command(pin
, result
, "GETPIN");
238 *result
= xstrdup("");
243 gpg_error_t
pinentry_getpin(struct pinentry_s
*pin
, gchar
**result
)
246 gpg_error_t error
= set_pinentry_strings(pin
, which
);
247 gchar
*result1
= NULL
;
250 pinentry_disconnect(pin
);
255 error
= do_getpin(pin
, result
);
260 if (pin
->which
== PINENTRY_SAVE
) {
262 error
= set_pinentry_strings(pin
, 2);
267 result1
= g_strdup(*result
);
271 if (strcmp(result1
, *result
)) {
274 result1
= *result
= NULL
;
275 error
= set_pinentry_strings(pin
, 1);
286 pinentry_disconnect(pin
);
290 gpg_error_t
pinentry_fork(assuan_context_t ctx
)
292 struct client_s
*client
= assuan_get_pointer(ctx
);
293 struct pinentry_s
*pin
= client
->pinentry
;
299 gchar
*result
= NULL
;
302 return gpg_error_from_syserror();
308 error
= gpg_error_from_syserror();
315 pk
.error
= pinentry_getpin(pin
, &result
);
319 len
= pth_write(p
[1], &pk
, sizeof(pk
));
322 if (len
!= sizeof(pk
))
323 log_write("%s(%i): write: len != sizeof(pk)", __FUNCTION__
, __LINE__
);
329 strncpy(pk
.key
, result
, sizeof(pk
.key
));
331 len
= pth_write(p
[1], &pk
, sizeof(pk
));
332 memset(&pk
, 0, sizeof(pk
));
335 if (len
!= sizeof(pk
))
336 log_write("%s(%i): write: len != sizeof(pk)", __FUNCTION__
, __LINE__
);
341 client
->pinentry
->fd
= p
[0];
342 client
->pinentry
->pid
= pid
;
343 client
->pinentry
->status
= PINENTRY_INIT
;
348 * Don't call assuan_process_done() here. That should be done in
349 * open_command_finalize() after the key has been read().
354 void cleanup_pinentry(struct pinentry_s
*pin
)
359 if (pin
->ctx
&& pin
->pid
)
360 pinentry_disconnect(pin
);
362 g_free(pin
->ttyname
);
363 g_free(pin
->ttytype
);
368 g_free(pin
->display
);
369 g_free(pin
->filename
);
373 void set_pinentry_defaults(struct pinentry_s
*pin
)
377 struct passwd
*pw
= getpwuid(getuid());
380 g_snprintf(buf
, sizeof(buf
), "%s/.pwmd/pinentry.conf", pw
->pw_dir
);
381 fp
= fopen(buf
, "r");
384 while ((p
= fgets(buf
, sizeof(buf
), fp
)) != NULL
) {
385 gchar name
[32] = {0}, value
[256] = {0};
390 if (p
[strlen(p
)-1] == '\n')
393 if (sscanf(p
, " %31[a-zA-Z] = %255s", name
, value
) != 2)
396 if (g_strcasecmp("TTYNAME", name
) == 0)
397 pin
->ttyname
= g_strdup(value
);
398 else if (g_strcasecmp("TTYTYPE", name
) == 0)
399 pin
->ttytype
= g_strdup(value
);
400 else if (g_strcasecmp("DISPLAY", name
) == 0)
401 pin
->display
= g_strdup(value
);
402 else if (g_strcasecmp("PATH", name
) == 0)
403 pin
->path
= g_strdup(value
);