1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2006-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 #include <sys/socket.h>
53 #define PINENTRY_PATH "/usr/local/bin/pinentry"
54 static gpg_error_t
pinentry_command(pwm_t
*pwm
, char **result
, const char *cmd
);
60 assuan_context_t pctx
; /* pinentry */
67 char *pinentry_display
;
73 pwmd_password_fn passfunc
;
75 pwmd_status_fn status_func
;
79 struct pwmd_nb_status_s
{
80 char filename
[FILENAME_MAX
];
95 const char *pwmd_strerror(gpg_error_t pwmd_errno
)
97 gpg_err_code_t code
= gpg_err_code(pwmd_errno
);
99 if (code
>= GPG_ERR_USER_1
&& code
< gpg_err_code(EPWMD_MAX
)) {
103 return "Unknown error";
105 return "No cache slots available";
107 return "Element not found";
109 return "Trailing element";
111 return "Invalid character in element";
115 return "Will not overwrite existing account";
117 return "File not found";
119 return "No file is open";
120 case GPG_ERR_USER_10
:
121 return "General LibXML error";
122 case GPG_ERR_USER_11
:
123 return "File not found in cache";
124 case GPG_ERR_USER_12
:
125 return "Attribute not found";
126 case GPG_ERR_USER_13
:
127 return "Invalid filename or link";
128 case GPG_ERR_USER_14
:
129 return "File modified";
133 return gpg_strerror(pwmd_errno
);
136 pwmd_error_t
pwmd_init()
139 assuan_set_malloc_hooks(xmalloc
, xrealloc
, xfree
);
140 assuan_set_assuan_err_source(GPG_ERR_SOURCE_DEFAULT
);
145 pwm_t
*pwmd_connect(const char *path
, gpg_error_t
*error
)
148 char *socketpath
= NULL
;
151 assuan_context_t ctx
;
155 pw
= getpwuid(getuid());
156 socketpath
= (char *)xmalloc(strlen(pw
->pw_dir
) + strlen("/.pwmd/socket") + 1);
157 sprintf(socketpath
, "%s/.pwmd/socket", pw
->pw_dir
);
160 socketpath
= xstrdup(path
);
162 rc
= assuan_socket_connect_ext(&ctx
, socketpath
, -1, 0);
170 if ((pwm
= (pwm_t
*)xcalloc(1, sizeof(pwm_t
))) == NULL
) {
171 *error
= gpg_error_from_errno(errno
);
172 assuan_disconnect(ctx
);
178 pwm
->pinentry_tries
= 3;
185 void pwmd_close(pwm_t
*pwm
)
191 assuan_disconnect(pwm
->ctx
);
194 xfree(pwm
->password
);
205 if (pwm
->pinentry_tty
)
206 xfree(pwm
->pinentry_tty
);
208 if (pwm
->pinentry_display
)
209 xfree(pwm
->pinentry_display
);
211 if (pwm
->pinentry_term
)
212 xfree(pwm
->pinentry_term
);
215 xfree(pwm
->filename
);
220 static int mem_realloc_cb(void *data
, const void *buffer
, size_t len
)
222 membuf_t
*mem
= (membuf_t
*)data
;
228 if ((p
= xrealloc(mem
->buf
, mem
->len
+ len
)) == NULL
)
232 memcpy((char *)mem
->buf
+ mem
->len
, buffer
, len
);
237 static int inquire_cb(void *data
, const char *keyword
)
239 struct inquire_s
*inquire
= (struct inquire_s
*)data
;
241 return assuan_send_data(inquire
->ctx
, inquire
->buf
, inquire
->len
);
244 void pwmd_free_result(void *data
)
249 static gpg_error_t
assuan_command(pwm_t
*pwm
, assuan_context_t ctx
,
250 char **result
, const char *cmd
)
259 * This is needed because assuan only accepts 1000 byte command strings.
260 * If the line is more than this the command will fail. So we use an
261 * INQUIRE on the server which waits for an END that assuan_transact()
264 * Other commands shouldn't need the INQUIRE. Let me know if you have an
265 * element path that's greater than 1000 bytes and I'll fix it.
267 if (strncasecmp(cmd
, "STORE", 5) == 0) {
268 const char *p
= cmd
+ 5;
269 struct inquire_s
*inq
= (struct inquire_s
*)xmalloc(sizeof(struct inquire_s
));
276 inq
->len
= strlen(p
);
277 rc
= assuan_transact(ctx
, "STORE", mem_realloc_cb
, &data
, inquire_cb
,
278 inq
, pwm
->status_func
, pwm
->status_data
);
284 * Ignore any status callback function for pinentry.
286 if (ctx
== pwm
->pctx
)
287 rc
= assuan_transact(ctx
, cmd
, mem_realloc_cb
, &data
, NULL
, NULL
, NULL
, NULL
);
290 rc
= assuan_transact(ctx
, cmd
, mem_realloc_cb
, &data
, NULL
, NULL
,
291 pwm
->status_func
, pwm
->status_data
);
302 mem_realloc_cb(&data
, "", 1);
303 *result
= (char *)data
.buf
;
307 return gpg_err_code(rc
);
311 static gpg_error_t
set_pinentry_strings(pwm_t
*pwm
, int which
)
317 pwm
->title
= xstrdup("LibPWMD");
320 pwm
->prompt
= xstrdup("Password:");
322 if (!pwm
->desc
&& !which
)
323 pwm
->desc
= xstrdup("Enter a password.");
326 buf
= xstrdup("SETERROR Invalid password, please try again.");
328 buf
= xstrdup("SETERROR Please type the password again for confirmation.");
330 buf
= (char *)xmalloc(strlen("SETERROR ") + strlen(pwm
->desc
) + 1);
331 sprintf(buf
, "SETERROR %s", pwm
->desc
);
334 error
= pinentry_command(pwm
, NULL
, buf
);
340 buf
= (char *)xmalloc(strlen("SETPROMPT ") + strlen(pwm
->prompt
) + 1);
341 sprintf(buf
, "SETPROMPT %s", pwm
->prompt
);
342 error
= pinentry_command(pwm
, NULL
, buf
);
348 buf
= (char *)xmalloc(strlen("SETDESC ") + strlen(pwm
->title
) + 1);
349 sprintf(buf
, "SETDESC %s", pwm
->title
);
350 error
= pinentry_command(pwm
, NULL
, buf
);
355 static gpg_error_t
launch_pinentry(pwm_t
*pwm
)
358 assuan_context_t ctx
;
359 int child_list
[] = {-1};
360 char *display
= getenv("DISPLAY");
362 int have_display
= 0;
365 if (pwm
->pinentry_display
|| display
)
368 tty
= pwm
->pinentry_tty
? pwm
->pinentry_tty
: ttyname(STDOUT_FILENO
);
371 return gpg_error_from_errno(errno
);
374 if (!display
&& !tty
)
375 return GPG_ERR_ENOTTY
;
377 argv
[0] = "pinentry";
378 argv
[1] = have_display
? "--display" : "--ttyname";
379 argv
[2] = have_display
? pwm
->pinentry_display
? pwm
->pinentry_display
: display
: tty
;
383 argv
[3] = "--ttytype";
384 argv
[4] = pwm
->pinentry_term
? pwm
->pinentry_term
: getenv("TERM");
388 rc
= assuan_pipe_connect(&ctx
, pwm
->pinentry_path
? pwm
->pinentry_path
: PINENTRY_PATH
, argv
, child_list
);
394 return set_pinentry_strings(pwm
, 0);
397 static gpg_error_t
pinentry_command(pwm_t
*pwm
, char **result
, const char *cmd
)
402 n
= launch_pinentry(pwm
);
408 return assuan_command(pwm
, pwm
->pctx
, result
, cmd
);
411 static void pinentry_disconnect(pwm_t
*pwm
)
413 assuan_disconnect(pwm
->pctx
);
417 static char *percent_escape(const char *atext
)
419 const unsigned char *s
;
420 int len
= strlen(atext
) * 3 + 1;
421 char *buf
= (char *)xmalloc(len
), *p
= buf
;
426 for (s
=(const unsigned char *)atext
; *s
; s
++) {
428 sprintf (p
, "%%%02X", *s
);
440 static gpg_error_t
send_command(pwm_t
*pwm
, char **result
, const char *cmd
)
445 return assuan_command(pwm
, pwm
->ctx
, result
, cmd
);
449 * Avoid sending the BYE command here. libassuan will close the file
450 * descriptor and release the assuan context. Use pwmd_close() instead.
452 pwmd_error_t
pwmd_command(pwm_t
*pwm
, char **result
, gpg_error_t
*error
, const char *cmd
, ...)
462 *error
= EPWMD_COMMAND_SYNTAX
;
467 *error
= EPWMD_ERROR
;
473 len
= vsnprintf(NULL
, 0, cmd
, ap
);
474 buf
= (char *)xmalloc(len
+ 1);
475 len
= vsnprintf(buf
, len
+ 1, cmd
, ap
);
477 *error
= send_command(pwm
, result
, buf
);
479 return *error
? PWMD_ERROR
: PWMD_OK
;
483 static gpg_error_t
do_getpin(pwm_t
*pwm
, char **result
)
488 error
= pinentry_command(pwm
, result
, "GETPIN");
490 if (error
== GPG_ERR_ASS_CANCELED
)
499 static pwmd_error_t
getpin(pwm_t
*pwm
, char **result
, gpg_error_t
*error
,
508 *error
= set_pinentry_strings(pwm
, which
);
511 pinentry_disconnect(pwm
);
516 if (pwm
->pinentry_tries
-1 != pin_try
) {
517 *error
= set_pinentry_strings(pwm
, 1);
520 pinentry_disconnect(pwm
);
526 *error
= do_getpin(pwm
, result
);
529 if (pin_try
!= -1 && pin_try
-- && *error
== EPWMD_KEY
)
533 pinentry_disconnect(pwm
);
542 pwmd_error_t
pwmd_open_nb_finalize(pwm_t
*pwm
, gpg_error_t
*error
,
543 pwmd_nb_status_t
*pw
)
547 if (!pwm
|| !pw
|| !pw
->filename
[0])
555 if (pwmd_command(pwm
, &result
, error
, "ISCACHED %s", pw
->filename
) != PWMD_OK
)
559 xfree(pwm
->filename
);
561 pwm
->filename
= xstrdup(pw
->filename
);
562 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
566 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
571 static gpg_error_t
do_open_command(pwm_t
*pwm
, const char *filename
, char *password
)
576 snprintf(buf
, sizeof(buf
), "OPEN %s %s", filename
, password
? password
: "");
577 error
= send_command(pwm
, NULL
, buf
);
578 memset(&buf
, 0, sizeof(buf
));
583 static int do_pwmd_open(pwm_t
*pwm
, gpg_error_t
*error
, const char *filename
,
587 char *password
= NULL
;
589 int pin_try
= pwm
->pinentry_tries
- 1;
592 if (!pwm
|| !filename
|| !*filename
) {
593 *error
= EPWMD_ERROR
;
598 * Avoid calling pinentry if the password is cached on the server.
600 pwmd_command(pwm
, &result
, error
, "ISCACHED %s", filename
);
602 if (*error
== EPWMD_CACHE_NOT_FOUND
) {
604 password
= pwm
->passfunc(pwm
, pwm
->passdata
);
606 if (!password
|| !*password
) {
611 password
= xstrdup(password
);
615 if (*error
== EPWMD_CACHE_NOT_FOUND
) {
618 * Get the password from pinentry.
620 if (pwm
->use_pinentry
) {
622 * Nonblocking is wanted. fork() then return a file descriptor
623 * that the client can use to read() from.
631 *error
= gpg_error_from_syserror();
640 strncpy(pw
.filename
, filename
, sizeof(pw
.filename
));
642 getpin(pwm
, &password
, error
, &pin_try
, 0);
647 pinentry_disconnect(pwm
);
650 write(p
[1], &pw
, sizeof(pw
));
655 *error
= do_open_command(pwm
, filename
, password
);
657 if (pwm
->pctx
&& *error
== EPWMD_BADKEY
) {
659 goto getpin_nb_again
;
664 pinentry_disconnect(pwm
);
666 write(p
[1], &pw
, sizeof(pw
));
671 *error
= gpg_error_from_syserror();
684 getpin(pwm
, &password
, error
, &pin_try
, 1);
688 pinentry_disconnect(pwm
);
696 * Not using pinentry and the file was not found
699 if (pwm
->password
== NULL
) {
704 password
= pwm
->password
;
710 else if (*error
&& *error
!= EPWMD_FILE_NOT_FOUND
)
714 *error
= do_open_command(pwm
, filename
, password
);
717 if (pwm
->pctx
&& *error
== EPWMD_BADKEY
) {
721 pinentry_disconnect(pwm
);
726 if (!pwm
->use_pinentry
&& pwm
->password
)
727 pwm
->password
= NULL
;
731 xfree(pwm
->filename
);
733 pwm
->filename
= xstrdup(filename
);
737 * The file is cached or the file is a new file.
740 return *error
? -1 : -2;
742 return *error
? PWMD_ERROR
: PWMD_OK
;
745 pwmd_error_t
pwmd_open(pwm_t
*pwm
, gpg_error_t
*error
, const char *filename
)
747 return (pwmd_error_t
)do_pwmd_open(pwm
, error
, filename
, 0);
750 int pwmd_open_nb(pwm_t
*pwm
, gpg_error_t
*error
, const char *filename
)
753 *error
= GPG_ERR_NOT_IMPLEMENTED
;
756 return do_pwmd_open(pwm
, error
, filename
, 1);
761 static gpg_error_t
do_save_getpin(pwm_t
*pwm
, char **password
)
769 getpin(pwm
, &result
, &error
, &pin_try
, confirm
? 2 : 0);
773 pinentry_disconnect(pwm
);
786 if (strcmp(*password
, result
)) {
789 pinentry_disconnect(pwm
);
790 error
= EPWMD_BADKEY
;
795 pinentry_disconnect(pwm
);
800 static gpg_error_t
do_save_command(pwm_t
*pwm
, char *password
)
802 char buf
[ASSUAN_LINELENGTH
];
805 snprintf(buf
, sizeof(buf
), "SAVE %s", password
? password
: "");
806 error
= send_command(pwm
, NULL
, buf
);
807 memset(&buf
, 0, sizeof(buf
));
809 pwm
->password
= NULL
;
813 pwmd_error_t
pwmd_save_nb_finalize(pwm_t
*pwm
, gpg_error_t
*error
,
814 pwmd_nb_status_t
*pw
)
816 if (!pwm
|| !pw
|| !pw
->filename
[0])
821 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
825 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
829 static int do_pwmd_save(pwm_t
*pwm
, gpg_error_t
*error
, int nb
)
832 char *password
= NULL
;
837 if (pwm
->use_pinentry
|| pwm
->passfunc
) {
838 pwmd_command(pwm
, &result
, error
, "ISCACHED %s", pwm
->filename
);
840 if (*error
== EPWMD_CACHE_NOT_FOUND
) {
842 if (pwm
->use_pinentry
) {
849 *error
= gpg_error_from_syserror();
858 strncpy(pw
.filename
, pwm
->filename
, sizeof(pw
.filename
));
862 *error
= do_save_getpin(pwm
, &password
);
863 } while (*error
== EPWMD_KEY
|| *error
== EPWMD_BADKEY
);
867 pinentry_disconnect(pwm
);
870 write(p
[1], &pw
, sizeof(pw
));
875 *error
= do_save_command(pwm
, password
);
876 pinentry_disconnect(pwm
);
878 write(p
[1], &pw
, sizeof(pw
));
883 *error
= gpg_error_from_syserror();
895 *error
= do_save_getpin(pwm
, &password
);
903 char *tmp
= (*pwm
->passfunc
)(pwm
, pwm
->passdata
);
910 password
= xstrdup(tmp
);
916 if (!password
|| !*password
) {
922 if (*error
&& *error
!= EPWMD_FILE_NOT_FOUND
)
927 password
= pwm
->password
;
929 *error
= do_save_command(pwm
, password
);
932 return *error
? -1 : -2;
934 return *error
? PWMD_ERROR
: PWMD_OK
;
937 int pwmd_save_nb(pwm_t
*pwm
, gpg_error_t
*error
)
940 *error
= GPG_ERR_NOT_IMPLEMENTED
;
943 return do_pwmd_save(pwm
, error
, 1);
947 pwmd_error_t
pwmd_save(pwm_t
*pwm
, gpg_error_t
*error
)
949 return (pwmd_error_t
)do_pwmd_save(pwm
, error
, 0);
952 pwmd_error_t
pwmd_setopt(pwm_t
*pwm
, gpg_error_t
*error
, pwmd_option_t opt
, ...)
956 int n
= va_arg(ap
, int);
966 case PWMD_OPTION_STATUS_FUNC
:
967 pwm
->status_func
= va_arg(ap
, pwmd_status_fn
);
969 case PWMD_OPTION_STATUS_DATA
:
970 pwm
->status_data
= va_arg(ap
, void *);
972 case PWMD_OPTION_PASSWORD_FUNC
:
973 pwm
->passfunc
= va_arg(ap
, pwmd_password_fn
);
975 case PWMD_OPTION_PASSWORD_DATA
:
976 pwm
->passdata
= va_arg(ap
, void *);
978 case PWMD_OPTION_PASSWORD
:
979 arg1
= va_arg(ap
, char *);
982 xfree(pwm
->password
);
984 pwm
->password
= xstrdup(arg1
);
987 case PWMD_OPTION_PINENTRY
:
990 if (n
!= 0 && n
!= 1) {
995 pwm
->use_pinentry
= n
;
997 case PWMD_OPTION_PINENTRY_TRIES
:
1005 pwm
->pinentry_tries
= n
;
1007 case PWMD_OPTION_PINENTRY_PATH
:
1008 if (pwm
->pinentry_path
)
1009 xfree(pwm
->pinentry_path
);
1011 pwm
->pinentry_path
= xstrdup(va_arg(ap
, char *));
1013 case PWMD_OPTION_PINENTRY_TTY
:
1014 if (pwm
->pinentry_tty
)
1015 xfree(pwm
->pinentry_tty
);
1017 pwm
->pinentry_tty
= xstrdup(va_arg(ap
, char *));
1019 case PWMD_OPTION_PINENTRY_DISPLAY
:
1020 if (pwm
->pinentry_display
)
1021 xfree(pwm
->pinentry_display
);
1023 pwm
->pinentry_display
= xstrdup(va_arg(ap
, char *));
1025 case PWMD_OPTION_PINENTRY_TERM
:
1026 if (pwm
->pinentry_term
)
1027 xfree(pwm
->pinentry_term
);
1029 pwm
->pinentry_term
= xstrdup(va_arg(ap
, char *));
1031 case PWMD_OPTION_PINENTRY_TITLE
:
1034 pwm
->title
= percent_escape(va_arg(ap
, char *));
1036 case PWMD_OPTION_PINENTRY_PROMPT
:
1039 pwm
->prompt
= percent_escape(va_arg(ap
, char *));
1041 case PWMD_OPTION_PINENTRY_DESC
:
1044 pwm
->desc
= percent_escape(va_arg(ap
, char *));
1047 case PWMD_OPTION_PINENTRY
:
1048 case PWMD_OPTION_PINENTRY_TRIES
:
1049 case PWMD_OPTION_PINENTRY_PATH
:
1050 case PWMD_OPTION_PINENTRY_TTY
:
1051 case PWMD_OPTION_PINENTRY_DISPLAY
:
1052 case PWMD_OPTION_PINENTRY_TERM
:
1053 case PWMD_OPTION_PINENTRY_TITLE
:
1054 case PWMD_OPTION_PINENTRY_PROMPT
:
1055 case PWMD_OPTION_PINENTRY_DESC
:
1056 *error
= GPG_ERR_NOT_IMPLEMENTED
;
1060 *error
= GPG_ERR_NOT_IMPLEMENTED
;