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
25 #define _ASSUAN_ONLY_GPG_ERRORS 1
35 #include "pwmd_error.h"
36 #include "assuan-errors.h"
43 static struct client_s
*global_client
;
46 static gpg_error_t
set_pinentry_strings(struct pinentry_s
*pin
, int which
);
48 /* Borrowed from libassuan 1.0.4 svn 277 */
49 /* Helper to map old style Assuan error codes to gpg-error codes.
50 This is used internally to keep an compatible ABI. */
52 _assuan_error (int oldcode
)
59 case ASSUAN_General_Error
: n
= 257; break;
60 case ASSUAN_Accept_Failed
: n
= 258; break;
61 case ASSUAN_Connect_Failed
: n
= 259; break;
62 case ASSUAN_Invalid_Response
: n
= 260; break;
63 case ASSUAN_Invalid_Value
: n
= 261; break;
64 case ASSUAN_Line_Not_Terminated
: n
= 262; break;
65 case ASSUAN_Line_Too_Long
: n
= 263; break;
66 case ASSUAN_Nested_Commands
: n
= 264; break;
67 case ASSUAN_No_Data_Callback
: n
= 265; break;
68 case ASSUAN_No_Inquire_Callback
: n
= 266; break;
69 case ASSUAN_Not_A_Server
: n
= 267; break;
70 case ASSUAN_Not_Implemented
: n
= 69; break;
71 case ASSUAN_Parameter_Conflict
: n
= 280; break;
72 case ASSUAN_Problem_Starting_Server
: n
= 269; break;
73 case ASSUAN_Server_Fault
: n
= 80; break;
74 case ASSUAN_Syntax_Error
: n
= 276; break;
75 case ASSUAN_Too_Much_Data
: n
= 273; break;
76 case ASSUAN_Unexpected_Command
: n
= 274; break;
77 case ASSUAN_Unknown_Command
: n
= 275; break;
78 case ASSUAN_Canceled
: n
= 277; break;
79 case ASSUAN_No_Secret_Key
: n
= 17; break;
80 case ASSUAN_Not_Confirmed
: n
= 114; break;
82 case ASSUAN_Read_Error
:
85 case 0: n
= 16381; /*GPG_ERR_MISSING_ERRNO*/ break;
89 default: n
= 270; /*GPG_ERR_ASS_READ_ERROR*/ break;
93 case ASSUAN_Write_Error
:
96 case 0: n
= 16381; /*GPG_ERR_MISSING_ERRNO*/ break;
100 default: n
= 271; /*GPG_ERR_ASS_WRITE_ERROR*/ break;
104 case ASSUAN_Out_Of_Core
:
107 case 0: /* Should not happen but a user might have provided
108 an incomplete implemented malloc function. Give
109 him a chance to correct this fault but make sure
110 an error is indeed returned. */
111 n
= 16381; /*GPG_ERR_MISSING_ERRNO*/
114 n
= (86 | (1 << 15));
117 n
= 16382; /*GPG_ERR_UNKNOWN_ERRNO*/
122 case -1: n
= 16383 /*GPG_ERR_EOF*/; break;
129 return ((err_source
<< 24) | (n
& 0x00ffffff));
132 static int mem_realloc_cb(void *data
, const void *buffer
, size_t len
)
134 membuf_t
*mem
= (membuf_t
*)data
;
140 if ((p
= xrealloc(mem
->buf
, mem
->len
+ len
)) == NULL
)
144 memcpy((char *)mem
->buf
+ mem
->len
, buffer
, len
);
149 static gpg_error_t
assuan_command(struct pinentry_s
*pin
, char **result
,
158 rc
= assuan_transact(pin
->ctx
, cmd
, mem_realloc_cb
, &data
, NULL
, NULL
,
169 mem_realloc_cb(&data
, "", 1);
170 *result
= (gchar
*)data
.buf
;
177 static gpg_error_t
launch_pinentry(struct pinentry_s
*pin
)
180 assuan_context_t ctx
;
181 int child_list
[] = {-1};
182 char *display
= getenv("DISPLAY");
184 int have_display
= 0;
187 //update_pinentry_settings(pin);
189 if (pin
->display
|| display
)
192 tty
= pin
->ttyname
? pin
->ttyname
: ttyname(STDOUT_FILENO
);
195 return _assuan_error(-1);
199 if (!display
&& !tty
)
200 return _assuan_error(-1);
202 argv
[0] = "pinentry";
203 argv
[1] = have_display
? "--display" : "--ttyname";
204 argv
[2] = have_display
? pin
->display
? pin
->display
: display
: tty
;
208 argv
[3] = "--ttytype";
209 argv
[4] = pin
->ttytype
? pin
->ttytype
: getenv("TERM");
213 rc
= assuan_pipe_connect(&ctx
, pin
->path
? pin
->path
: PINENTRY_PATH
,
217 return _assuan_error(rc
);
219 pin
->pid
= assuan_get_pid(ctx
);
221 return set_pinentry_strings(pin
, 0);
224 static gpg_error_t
pinentry_command(struct pinentry_s
*pin
, gchar
**result
,
227 gpg_error_t error
= 0;
230 error
= launch_pinentry(pin
);
232 return error
? error
: assuan_command(pin
, result
, cmd
);
235 static gpg_error_t
set_pinentry_strings(struct pinentry_s
*pin
, int which
)
238 char tmp
[ASSUAN_LINELENGTH
];
242 pin
->title
= xstrdup(N_("Password Manager Daemon"));
245 pin
->prompt
= xstrdup(N_("Password:"));
247 if (!pin
->desc
&& !which
)
248 pin
->desc
= xstrdup(N_("Enter a password."));
251 snprintf(tmp
, sizeof(tmp
), "SETERROR %s", N_("Invalid password, please try again."));
254 else if (which
== 2) {
255 snprintf(tmp
, sizeof(tmp
), "SETERROR %s", N_("Please type the password again for confirmation."));
259 buf
= (char *)xmalloc(strlen("SETERROR ") + strlen(pin
->desc
) + 1);
260 sprintf(buf
, "SETERROR %s", pin
->desc
);
263 error
= pinentry_command(pin
, NULL
, buf
);
269 buf
= (char *)xmalloc(strlen("SETPROMPT ") + strlen(pin
->prompt
) + 1);
270 sprintf(buf
, "SETPROMPT %s", pin
->prompt
);
271 error
= pinentry_command(pin
, NULL
, buf
);
277 buf
= (char *)xmalloc(strlen("SETDESC ") + strlen(pin
->title
) + 1);
278 sprintf(buf
, "SETDESC %s", pin
->title
);
279 error
= pinentry_command(pin
, NULL
, buf
);
284 static void pinentry_disconnect(struct pinentry_s
*pin
)
290 assuan_disconnect(pin
->ctx
);
296 static gpg_error_t
do_getpin(struct pinentry_s
*pin
, char **result
)
301 error
= pinentry_command(pin
, result
, "GETPIN");
304 error
= _assuan_error(error
);
306 if (!error
&& !*result
)
312 static gpg_error_t
getpin(struct pinentry_s
*pin
, gchar
**result
, int which
)
314 gpg_error_t error
= set_pinentry_strings(pin
, which
);
317 pinentry_disconnect(pin
);
321 error
= do_getpin(pin
, result
);
322 pinentry_disconnect(pin
);
326 static void catchsig(gint sig
)
333 if (global_client
->pinentry
->pid
) {
334 kill(SIGTERM
, global_client
->pinentry
->pid
);
335 global_client
->pinentry
->pid
= 0;
338 memset(&pk
, 0, sizeof(pk
));
339 pk
.error
= sig
== SIGTERM
? GPG_ERR_ASS_CANCELED
: GPG_ERR_TIMEOUT
;
340 pth_write(send_fd
, &pk
, sizeof(pk
));
341 cleanup_pinentry(global_client
->pinentry
);
342 free_client(global_client
);
348 gpg_error_t
pinentry_fork(assuan_context_t ctx
)
350 struct client_s
*client
= assuan_get_pointer(ctx
);
359 error
= gpg_error_from_syserror();
360 return send_error(ctx
, error
);
367 error
= gpg_error_from_syserror();
370 return send_error(ctx
, error
);
372 signal(SIGALRM
, catchsig
);
373 signal(SIGTERM
, catchsig
);
376 global_client
= client
;
377 alarm(client
->pinentry
->timeout
);
378 pk
.error
= getpin(client
->pinentry
, &result
, 0);
379 cleanup_pinentry(client
->pinentry
);
383 len
= pth_write(p
[1], &pk
, sizeof(pk
));
384 memset(&pk
, 0, sizeof(pk
));
387 if (len
!= sizeof(pk
))
388 log_write("%s(%i): write: len != sizeof(pk)", __FUNCTION__
, __LINE__
);
394 strncpy(pk
.key
, result
, sizeof(pk
.key
));
396 len
= pth_write(p
[1], &pk
, sizeof(pk
));
397 memset(&pk
, 0, sizeof(pk
));
400 if (len
!= sizeof(pk
))
401 log_write("%s(%i): write: len != sizeof(pk)", __FUNCTION__
, __LINE__
);
405 client
->pinentry
->fd
= p
[0];
406 client
->pinentry
->pid
= pid
;
407 client
->pinentry
->status
= PINENTRY_INIT
;
412 * Don't call assuan_process_done() here. That should be done in
413 * open_command_finalize() after the key has been read().
418 void cleanup_pinentry(struct pinentry_s
*pin
)
423 if (pin
->ctx
&& pin
->pid
)
424 pinentry_disconnect(pin
);