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>
52 #define PINENTRY_PATH "/usr/bin/pinentry"
53 static gpg_error_t
pinentry_command(pwm_t
*pwm
, char **result
, const char *cmd
);
59 assuan_context_t pctx
; /* pinentry */
66 char *pinentry_display
;
72 pwmd_password_fn passfunc
;
74 pwmd_status_fn status_func
;
89 const char *pwmd_strerror(gpg_error_t pwmd_errno
)
91 gpg_err_code_t code
= gpg_err_code(pwmd_errno
);
93 if (code
>= GPG_ERR_USER_1
&& code
< gpg_err_code(EPWMD_MAX
)) {
97 return "Unknown error";
99 return "No cache slots available";
101 return "Element not found";
103 return "Trailing element";
105 return "Invalid character in element";
109 return "Will not overwrite existing account";
111 return "File not found";
113 return "No file is open";
114 case GPG_ERR_USER_10
:
115 return "General LibXML error";
116 case GPG_ERR_USER_11
:
117 return "File not found in cache";
118 case GPG_ERR_USER_12
:
119 return "Attribute not found";
120 case GPG_ERR_USER_13
:
121 return "Invalid filename or link";
122 case GPG_ERR_USER_14
:
123 return "File modified";
127 return gpg_strerror(pwmd_errno
);
130 pwmd_error_t
pwmd_init()
133 assuan_set_malloc_hooks(xmalloc
, xrealloc
, xfree
);
134 assuan_set_assuan_err_source(GPG_ERR_SOURCE_DEFAULT
);
139 pwm_t
*pwmd_connect(const char *path
, gpg_error_t
*error
)
142 char *socketpath
= NULL
;
145 assuan_context_t ctx
;
149 pw
= getpwuid(getuid());
150 socketpath
= (char *)xmalloc(strlen(pw
->pw_dir
) + strlen("/.pwmd/socket") + 1);
151 sprintf(socketpath
, "%s/.pwmd/socket", pw
->pw_dir
);
154 socketpath
= xstrdup(path
);
156 rc
= assuan_socket_connect_ext(&ctx
, socketpath
, -1, 0);
164 if ((pwm
= (pwm_t
*)xcalloc(1, sizeof(pwm_t
))) == NULL
) {
165 *error
= gpg_error_from_errno(errno
);
166 assuan_disconnect(ctx
);
172 pwm
->pinentry_tries
= 3;
179 void pwmd_close(pwm_t
*pwm
)
185 assuan_disconnect(pwm
->ctx
);
188 xfree(pwm
->password
);
199 if (pwm
->pinentry_tty
)
200 xfree(pwm
->pinentry_tty
);
202 if (pwm
->pinentry_display
)
203 xfree(pwm
->pinentry_display
);
205 if (pwm
->pinentry_term
)
206 xfree(pwm
->pinentry_term
);
209 xfree(pwm
->filename
);
214 static int mem_realloc_cb(void *data
, const void *buffer
, size_t len
)
216 membuf_t
*mem
= (membuf_t
*)data
;
222 if ((p
= xrealloc(mem
->buf
, mem
->len
+ len
)) == NULL
)
226 memcpy((char *)mem
->buf
+ mem
->len
, buffer
, len
);
231 static int inquire_cb(void *data
, const char *keyword
)
233 struct inquire_s
*inquire
= (struct inquire_s
*)data
;
235 return assuan_send_data(inquire
->ctx
, inquire
->buf
, inquire
->len
);
238 void pwmd_free_result(void *data
)
243 static gpg_error_t
assuan_command(pwm_t
*pwm
, assuan_context_t ctx
,
244 char **result
, const char *cmd
)
253 * This is needed because assuan only accepts 1000 byte command strings.
254 * If the line is more than this the command will fail. So we use an
255 * INQUIRE on the server which waits for an END that assuan_transact()
258 * Other commands shouldn't need the INQUIRE. Let me know if you have an
259 * element path that's greater than 1000 bytes and I'll fix it.
261 if (strncasecmp(cmd
, "STORE", 5) == 0) {
262 const char *p
= cmd
+ 5;
263 struct inquire_s
*inq
= (struct inquire_s
*)xmalloc(sizeof(struct inquire_s
));
270 inq
->len
= strlen(p
);
271 rc
= assuan_transact(ctx
, "STORE", mem_realloc_cb
, &data
, inquire_cb
,
272 inq
, pwm
->status_func
, pwm
->status_data
);
278 * Ignore any status callback function for pinentry.
280 if (ctx
== pwm
->pctx
)
281 rc
= assuan_transact(ctx
, cmd
, mem_realloc_cb
, &data
, NULL
, NULL
, NULL
, NULL
);
284 rc
= assuan_transact(ctx
, cmd
, mem_realloc_cb
, &data
, NULL
, NULL
,
285 pwm
->status_func
, pwm
->status_data
);
296 mem_realloc_cb(&data
, "", 1);
297 *result
= (char *)data
.buf
;
301 return gpg_err_code(rc
);
305 static gpg_error_t
set_pinentry_strings(pwm_t
*pwm
, int which
)
311 pwm
->title
= xstrdup("LibPWMD");
314 pwm
->prompt
= xstrdup("Password:");
316 if (!pwm
->desc
&& !which
)
317 pwm
->desc
= xstrdup("Enter a password.");
320 buf
= xstrdup("SETERROR Invalid password, please try again.");
322 buf
= xstrdup("SETERROR Please type the password again for confirmation.");
324 buf
= (char *)xmalloc(strlen("SETERROR ") + strlen(pwm
->desc
) + 1);
325 sprintf(buf
, "SETERROR %s", pwm
->desc
);
328 error
= pinentry_command(pwm
, NULL
, buf
);
334 buf
= (char *)xmalloc(strlen("SETPROMPT ") + strlen(pwm
->prompt
) + 1);
335 sprintf(buf
, "SETPROMPT %s", pwm
->prompt
);
336 error
= pinentry_command(pwm
, NULL
, buf
);
342 buf
= (char *)xmalloc(strlen("SETDESC ") + strlen(pwm
->title
) + 1);
343 sprintf(buf
, "SETDESC %s", pwm
->title
);
344 error
= pinentry_command(pwm
, NULL
, buf
);
349 static gpg_error_t
launch_pinentry(pwm_t
*pwm
)
352 assuan_context_t ctx
;
353 int child_list
[] = {-1};
354 char *display
= getenv("DISPLAY");
356 int have_display
= 0;
359 if (pwm
->pinentry_display
|| display
)
362 tty
= pwm
->pinentry_tty
? pwm
->pinentry_tty
: ttyname(STDOUT_FILENO
);
365 return gpg_error_from_errno(errno
);
368 if (!display
&& !tty
)
369 return GPG_ERR_ENOTTY
;
371 argv
[0] = "pinentry";
372 argv
[1] = have_display
? "--display" : "--ttyname";
373 argv
[2] = have_display
? pwm
->pinentry_display
? pwm
->pinentry_display
: display
: tty
;
377 argv
[3] = "--ttytype";
378 argv
[4] = pwm
->pinentry_term
? pwm
->pinentry_term
: getenv("TERM");
382 rc
= assuan_pipe_connect(&ctx
, pwm
->pinentry_path
? pwm
->pinentry_path
: PINENTRY_PATH
, argv
, child_list
);
388 return set_pinentry_strings(pwm
, 0);
391 static gpg_error_t
pinentry_command(pwm_t
*pwm
, char **result
, const char *cmd
)
396 n
= launch_pinentry(pwm
);
402 return assuan_command(pwm
, pwm
->pctx
, result
, cmd
);
405 static void pinentry_disconnect(pwm_t
*pwm
)
407 assuan_disconnect(pwm
->pctx
);
411 static char *percent_escape(const char *atext
)
413 const unsigned char *s
;
414 int len
= strlen(atext
) * 3 + 1;
415 char *buf
= (char *)xmalloc(len
), *p
= buf
;
420 for (s
=(const unsigned char *)atext
; *s
; s
++) {
422 sprintf (p
, "%%%02X", *s
);
434 static gpg_error_t
send_command(pwm_t
*pwm
, char **result
, const char *cmd
)
439 return assuan_command(pwm
, pwm
->ctx
, result
, cmd
);
443 * Avoid sending the BYE command here. libassuan will close the file
444 * descriptor and release the assuan context. Use pwmd_close() instead.
446 pwmd_error_t
pwmd_command(pwm_t
*pwm
, char **result
, gpg_error_t
*error
, const char *cmd
, ...)
456 *error
= EPWMD_COMMAND_SYNTAX
;
461 *error
= EPWMD_ERROR
;
467 len
= vsnprintf(NULL
, 0, cmd
, ap
);
468 buf
= (char *)xmalloc(len
+ 1);
469 len
= vsnprintf(buf
, len
+ 1, cmd
, ap
);
471 *error
= send_command(pwm
, result
, buf
);
473 return *error
? PWMD_ERROR
: PWMD_OK
;
477 static gpg_error_t
do_getpin(pwm_t
*pwm
, char **result
)
482 error
= pinentry_command(pwm
, result
, "GETPIN");
484 if (error
== GPG_ERR_ASS_CANCELED
)
493 static pwmd_error_t
getpin(pwm_t
*pwm
, char **result
, gpg_error_t
*error
,
502 *error
= set_pinentry_strings(pwm
, which
);
505 pinentry_disconnect(pwm
);
510 if (pwm
->pinentry_tries
-1 != pin_try
) {
511 *error
= set_pinentry_strings(pwm
, 1);
514 pinentry_disconnect(pwm
);
520 *error
= do_getpin(pwm
, result
);
523 if (pin_try
!= -1 && pin_try
-- && *error
== EPWMD_KEY
)
527 pinentry_disconnect(pwm
);
536 pwmd_error_t
pwmd_open_nb_finalize(pwm_t
*pwm
, gpg_error_t
*error
,
537 pwmd_nb_status_t
*pw
)
541 if (!pwm
|| !pw
|| !pw
->filename
[0])
551 if (pwmd_command(pwm
, &result
, error
, "ISCACHED %s", pw
->filename
) != PWMD_OK
)
555 xfree(pwm
->filename
);
557 pwm
->filename
= xstrdup(pw
->filename
);
558 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
562 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
567 static gpg_error_t
do_open_command(pwm_t
*pwm
, const char *filename
, char *password
)
572 snprintf(buf
, sizeof(buf
), "OPEN %s %s", filename
, password
? password
: "");
573 error
= send_command(pwm
, NULL
, buf
);
574 memset(&buf
, 0, sizeof(buf
));
579 static int do_pwmd_open(pwm_t
*pwm
, gpg_error_t
*error
, const char *filename
,
583 char *password
= NULL
;
585 int pin_try
= pwm
->pinentry_tries
- 1;
588 if (!pwm
|| !filename
|| !*filename
) {
589 *error
= EPWMD_ERROR
;
594 * Avoid calling pinentry if the password is cached on the server.
596 pwmd_command(pwm
, &result
, error
, "ISCACHED %s", filename
);
598 if (*error
== EPWMD_CACHE_NOT_FOUND
) {
600 password
= pwm
->passfunc(pwm
, pwm
->passdata
);
602 if (!password
|| !*password
) {
607 password
= xstrdup(password
);
611 if (*error
== EPWMD_CACHE_NOT_FOUND
) {
614 * Get the password from pinentry.
616 if (pwm
->use_pinentry
) {
618 * Nonblocking is wanted. fork() then return a file descriptor
619 * that the client can use to read() from.
627 *error
= gpg_error_from_syserror();
636 strncpy(pw
.filename
, filename
, sizeof(pw
.filename
));
639 getpin(pwm
, &password
, error
, &pin_try
, 0);
644 pinentry_disconnect(pwm
);
647 write(p
[1], &pw
, sizeof(pw
));
652 *error
= do_open_command(pwm
, filename
, password
);
654 if (pwm
->pctx
&& *error
== EPWMD_BADKEY
) {
656 goto getpin_nb_again
;
661 pinentry_disconnect(pwm
);
663 write(p
[1], &pw
, sizeof(pw
));
668 *error
= gpg_error_from_syserror();
681 getpin(pwm
, &password
, error
, &pin_try
, 1);
685 pinentry_disconnect(pwm
);
693 * Not using pinentry and the file was not found
696 if (pwm
->password
== NULL
) {
701 password
= pwm
->password
;
707 else if (*error
&& *error
!= EPWMD_FILE_NOT_FOUND
)
711 *error
= do_open_command(pwm
, filename
, password
);
714 if (pwm
->pctx
&& *error
== EPWMD_BADKEY
) {
718 pinentry_disconnect(pwm
);
723 if (!pwm
->use_pinentry
&& pwm
->password
)
724 pwm
->password
= NULL
;
728 xfree(pwm
->filename
);
730 pwm
->filename
= xstrdup(filename
);
734 * The file is cached or the file is a new file.
737 return *error
? -1 : -2;
739 return *error
? PWMD_ERROR
: PWMD_OK
;
742 pwmd_error_t
pwmd_open(pwm_t
*pwm
, gpg_error_t
*error
, const char *filename
)
744 return (pwmd_error_t
)do_pwmd_open(pwm
, error
, filename
, 0);
747 int pwmd_open_nb(pwm_t
*pwm
, gpg_error_t
*error
, const char *filename
)
750 *error
= GPG_ERR_NOT_IMPLEMENTED
;
753 return do_pwmd_open(pwm
, error
, filename
, 1);
758 static gpg_error_t
do_save_getpin(pwm_t
*pwm
, char **password
)
766 getpin(pwm
, &result
, &error
, &pin_try
, confirm
? 2 : 0);
770 pinentry_disconnect(pwm
);
783 if (strcmp(*password
, result
)) {
786 pinentry_disconnect(pwm
);
787 error
= EPWMD_BADKEY
;
792 pinentry_disconnect(pwm
);
797 static gpg_error_t
do_save_command(pwm_t
*pwm
, char *password
)
799 char buf
[ASSUAN_LINELENGTH
];
802 snprintf(buf
, sizeof(buf
), "SAVE %s", password
? password
: "");
803 error
= send_command(pwm
, NULL
, buf
);
804 memset(&buf
, 0, sizeof(buf
));
806 pwm
->password
= NULL
;
810 pwmd_error_t
pwmd_save_nb_finalize(pwm_t
*pwm
, gpg_error_t
*error
,
811 pwmd_nb_status_t
*pw
)
813 if (!pwm
|| !pw
|| !pw
->filename
[0])
820 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
824 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
828 static int do_pwmd_save(pwm_t
*pwm
, gpg_error_t
*error
, int nb
)
831 char *password
= NULL
;
836 if (pwm
->use_pinentry
|| pwm
->passfunc
) {
837 pwmd_command(pwm
, &result
, error
, "ISCACHED %s", pwm
->filename
);
839 if (*error
== EPWMD_CACHE_NOT_FOUND
) {
841 if (pwm
->use_pinentry
) {
848 *error
= gpg_error_from_syserror();
857 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
;