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 struct client_s
*global_client
;
46 static gpg_error_t
set_pinentry_strings(struct pinentry_s
*pin
, int which
);
48 static int mem_realloc_cb(void *data
, const void *buffer
, size_t len
)
50 membuf_t
*mem
= (membuf_t
*)data
;
56 if ((p
= xrealloc(mem
->buf
, mem
->len
+ len
)) == NULL
)
60 memcpy((char *)mem
->buf
+ mem
->len
, buffer
, len
);
65 static gpg_error_t
assuan_command(struct pinentry_s
*pin
, gchar
**result
,
74 rc
= assuan_transact(pin
->ctx
, cmd
, mem_realloc_cb
, &data
, NULL
, NULL
,
85 mem_realloc_cb(&data
, "", 1);
86 *result
= (gchar
*)data
.buf
;
93 static gpg_error_t
set_pinentry_options(struct pinentry_s
*pin
)
95 gchar
*display
= getenv("DISPLAY");
96 gint have_display
= 0;
97 gchar
*tty
= NULL
, *ttytype
= NULL
;
100 gchar
*result
= NULL
;
101 gchar cmd
[ASSUAN_LINELENGTH
];
103 if (pin
->display
|| display
)
106 tty
= pin
->ttyname
? pin
->ttyname
: ttyname(STDOUT_FILENO
);
109 return GPG_ERR_CANCELED
;
112 if (!have_display
&& !tty
)
113 return GPG_ERR_CANCELED
;
116 gchar
*p
= getenv("TERM");
118 ttytype
= pin
->ttytype
? pin
->ttytype
: p
;
121 return GPG_ERR_CANCELED
;
124 opt
= have_display
? "DISPLAY" : "TTYNAME";
125 val
= have_display
? pin
->display
? pin
->display
: display
: tty
;
126 snprintf(cmd
, sizeof(cmd
), "OPTION %s=%s", g_ascii_strdown(opt
, strlen(opt
)), val
);
127 rc
= assuan_command(pin
, &result
, cmd
);
133 snprintf(cmd
, sizeof(cmd
), "OPTION ttytype=%s", ttytype
);
134 rc
= assuan_command(pin
, &result
, cmd
);
140 static gpg_error_t
launch_pinentry(struct pinentry_s
*pin
)
143 assuan_context_t ctx
;
144 gint child_list
[] = {-1};
145 const gchar
*argv
[] = { "pinentry", NULL
};
147 rc
= assuan_pipe_connect(&ctx
, pin
->path
? pin
->path
: PINENTRY_PATH
,
153 pin
->pid
= assuan_get_pid(ctx
);
155 rc
= set_pinentry_options(pin
);
156 return rc
? rc
: set_pinentry_strings(pin
, 0);
159 static gpg_error_t
pinentry_command(struct pinentry_s
*pin
, gchar
**result
,
162 gpg_error_t error
= 0;
165 error
= launch_pinentry(pin
);
167 return error
? error
: assuan_command(pin
, result
, cmd
);
170 static gpg_error_t
set_pinentry_strings(struct pinentry_s
*pin
, int which
)
177 title
= g_strdup(N_("Password mismatch, please try again."));
178 else if (!pin
->title
)
179 title
= pin
->title
= g_strdup(N_("Password Manager Daemon"));
184 pin
->prompt
= g_strdup(N_("Password:"));
186 if (!pin
->desc
&& !which
)
187 pin
->desc
= g_strdup_printf(pin
->which
== PINENTRY_OPEN
?
188 N_("A password is required to open the file \"%s\". Please%%0Aenter the password below.") :
189 N_("A password is required to save to the file \"%s\". Please%%0Aenter the password below."),
193 buf
= g_strdup_printf("SETERROR %s", N_("Please type the password again for confirmation."));
195 buf
= g_strdup_printf("SETERROR %s", pin
->desc
);
197 error
= pinentry_command(pin
, NULL
, buf
);
203 buf
= g_strdup_printf("SETPROMPT %s", pin
->prompt
);
204 error
= pinentry_command(pin
, NULL
, buf
);
210 buf
= g_strdup_printf("SETDESC %s", title
);
211 error
= pinentry_command(pin
, NULL
, buf
);
221 static void pinentry_disconnect(struct pinentry_s
*pin
)
227 assuan_disconnect(pin
->ctx
);
233 static gpg_error_t
do_getpin(struct pinentry_s
*pin
, char **result
)
238 error
= pinentry_command(pin
, result
, "GETPIN");
241 *result
= xstrdup("");
246 static gpg_error_t
getpin(struct pinentry_s
*pin
, gchar
**result
)
249 gpg_error_t error
= set_pinentry_strings(pin
, which
);
250 gchar
*result1
= NULL
;
253 pinentry_disconnect(pin
);
258 error
= do_getpin(pin
, result
);
263 if (pin
->which
== PINENTRY_SAVE
) {
265 error
= set_pinentry_strings(pin
, 2);
270 result1
= g_strdup(*result
);
274 if (strcmp(result1
, *result
)) {
277 result1
= *result
= NULL
;
278 error
= set_pinentry_strings(pin
, 1);
289 pinentry_disconnect(pin
);
293 static void catchsig(gint sig
)
300 memset(&pk
, 0, sizeof(pk
));
301 pk
.error
= sig
== SIGTERM
? GPG_ERR_ASS_CANCELED
: GPG_ERR_TIMEOUT
;
302 pth_write(send_fd
, &pk
, sizeof(pk
));
304 cleanup_pinentry(global_client
->pinentry
);
305 free_client(global_client
);
311 gpg_error_t
pinentry_fork(assuan_context_t ctx
)
313 struct client_s
*client
= assuan_get_pointer(ctx
);
319 gchar
*result
= NULL
;
322 return gpg_error_from_syserror();
328 error
= gpg_error_from_syserror();
333 signal(SIGALRM
, catchsig
);
334 signal(SIGTERM
, catchsig
);
337 global_client
= client
;
338 alarm(client
->pinentry
->timeout
);
339 pk
.error
= getpin(client
->pinentry
, &result
);
340 cleanup_pinentry(client
->pinentry
);
345 len
= pth_write(p
[1], &pk
, sizeof(pk
));
348 if (len
!= sizeof(pk
))
349 log_write("%s(%i): write: len != sizeof(pk)", __FUNCTION__
, __LINE__
);
355 strncpy(pk
.key
, result
, sizeof(pk
.key
));
357 len
= pth_write(p
[1], &pk
, sizeof(pk
));
358 memset(&pk
, 0, sizeof(pk
));
361 if (len
!= sizeof(pk
))
362 log_write("%s(%i): write: len != sizeof(pk)", __FUNCTION__
, __LINE__
);
367 client
->pinentry
->fd
= p
[0];
368 client
->pinentry
->pid
= pid
;
369 client
->pinentry
->status
= PINENTRY_INIT
;
374 * Don't call assuan_process_done() here. That should be done in
375 * open_command_finalize() after the key has been read().
380 void cleanup_pinentry(struct pinentry_s
*pin
)
385 if (pin
->ctx
&& pin
->pid
)
386 pinentry_disconnect(pin
);
388 g_free(pin
->ttyname
);
389 g_free(pin
->ttytype
);
394 g_free(pin
->display
);
398 void set_pinentry_defaults(struct pinentry_s
*pin
)
402 struct passwd
*pw
= getpwuid(getuid());
405 g_snprintf(buf
, sizeof(buf
), "%s/.pwmd/pinentry.conf", pw
->pw_dir
);
406 fp
= fopen(buf
, "r");
409 while ((p
= fgets(buf
, sizeof(buf
), fp
)) != NULL
) {
410 gchar name
[32] = {0}, value
[256] = {0};
415 if (p
[strlen(p
)-1] == '\n')
418 if (sscanf(p
, " %31[a-zA-Z] = %255s", name
, value
) != 2)
421 if (g_strcasecmp("TTYNAME", name
) == 0)
422 pin
->ttyname
= g_strdup(value
);
423 else if (g_strcasecmp("TTYTYPE", name
) == 0)
424 pin
->ttytype
= g_strdup(value
);
425 else if (g_strcasecmp("DISPLAY", name
) == 0)
426 pin
->display
= g_strdup(value
);
427 else if (g_strcasecmp("PATH", name
) == 0)
428 pin
->path
= g_strdup(value
);