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_func passfunc
;
75 pwmd_status_func status_func
;
90 const char *pwmd_strerror(gpg_error_t pwmd_errno
)
92 gpg_err_code_t code
= gpg_err_code(pwmd_errno
);
94 if (code
>= GPG_ERR_USER_1
&& code
< gpg_err_code(EPWMD_MAX
)) {
98 return "Unknown error";
100 return "No cache slots available";
102 return "Element not found";
104 return "Trailing element";
106 return "Invalid character in element";
110 return "Will not overwrite existing account";
112 return "File not found";
114 return "No file is open";
115 case GPG_ERR_USER_10
:
116 return "General LibXML error";
117 case GPG_ERR_USER_11
:
118 return "File not found in cache";
119 case GPG_ERR_USER_12
:
120 return "Attribute not found";
121 case GPG_ERR_USER_13
:
122 return "Invalid filename or link";
123 case GPG_ERR_USER_14
:
124 return "File modified";
128 return gpg_strerror(pwmd_errno
);
131 pwmd_error_t
pwmd_init()
134 assuan_set_malloc_hooks(xmalloc
, xrealloc
, xfree
);
135 assuan_set_assuan_err_source(GPG_ERR_SOURCE_DEFAULT
);
140 pwm_t
*pwmd_connect(const char *path
, gpg_error_t
*error
)
143 char *socketpath
= NULL
;
146 assuan_context_t ctx
;
150 pw
= getpwuid(getuid());
151 socketpath
= (char *)xmalloc(strlen(pw
->pw_dir
) + strlen("/.pwmd/socket") + 1);
152 sprintf(socketpath
, "%s/.pwmd/socket", pw
->pw_dir
);
155 socketpath
= xstrdup(path
);
157 rc
= assuan_socket_connect_ext(&ctx
, socketpath
, -1, 0);
165 if ((pwm
= (pwm_t
*)xcalloc(1, sizeof(pwm_t
))) == NULL
) {
166 *error
= gpg_error_from_errno(errno
);
167 assuan_disconnect(ctx
);
173 pwm
->pinentry_tries
= 3;
180 void pwmd_close(pwm_t
*pwm
)
186 assuan_disconnect(pwm
->ctx
);
189 xfree(pwm
->password
);
200 if (pwm
->pinentry_tty
)
201 xfree(pwm
->pinentry_tty
);
203 if (pwm
->pinentry_display
)
204 xfree(pwm
->pinentry_display
);
206 if (pwm
->pinentry_term
)
207 xfree(pwm
->pinentry_term
);
210 xfree(pwm
->filename
);
215 static int mem_realloc_cb(void *data
, const void *buffer
, size_t len
)
217 membuf_t
*mem
= (membuf_t
*)data
;
223 if ((p
= xrealloc(mem
->buf
, mem
->len
+ len
)) == NULL
)
227 memcpy((char *)mem
->buf
+ mem
->len
, buffer
, len
);
232 static int inquire_cb(void *data
, const char *keyword
)
234 struct inquire_s
*inquire
= (struct inquire_s
*)data
;
236 return assuan_send_data(inquire
->ctx
, inquire
->buf
, inquire
->len
);
239 void pwmd_free_result(void *data
)
244 static gpg_error_t
assuan_command(pwm_t
*pwm
, assuan_context_t ctx
,
245 char **result
, const char *cmd
)
254 * This is needed because assuan only accepts 1000 byte command strings.
255 * If the line is more than this the command will fail. So we use an
256 * INQUIRE on the server which waits for an END that assuan_transact()
259 * Other commands shouldn't need the INQUIRE. Let me know if you have an
260 * element path that's greater than 1000 bytes and I'll fix it.
262 if (strncasecmp(cmd
, "STORE", 5) == 0) {
263 const char *p
= cmd
+ 5;
264 struct inquire_s
*inq
= (struct inquire_s
*)xmalloc(sizeof(struct inquire_s
));
271 inq
->len
= strlen(p
);
272 rc
= assuan_transact(ctx
, "STORE", mem_realloc_cb
, &data
, inquire_cb
,
273 inq
, pwm
->status_func
, pwm
->status_data
);
279 * Ignore any status callback function for pinentry.
281 if (ctx
== pwm
->pctx
)
282 rc
= assuan_transact(ctx
, cmd
, mem_realloc_cb
, &data
, NULL
, NULL
, NULL
, NULL
);
285 rc
= assuan_transact(ctx
, cmd
, mem_realloc_cb
, &data
, NULL
, NULL
,
286 pwm
->status_func
, pwm
->status_data
);
297 mem_realloc_cb(&data
, "", 1);
298 *result
= (char *)data
.buf
;
302 return gpg_err_code(rc
);
307 * which = 0 - normal description from the default or pwm->desc
308 * which = 1 - invalid password
309 * which = 2 - confirm
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
getpin(pwm_t
*pwm
, char **result
)
488 error
= pinentry_command(pwm
, result
, "GETPIN");
490 if (error
== GPG_ERR_ASS_CANCELED
)
499 pwmd_error_t
pwmd_open(pwm_t
*pwm
, gpg_error_t
*error
, const char *filename
)
503 char *password
= NULL
;
504 static char buf
[1000];
506 int pin_try
= pwm
->pinentry_tries
- 1;
510 if (!pwm
|| !filename
|| !*filename
) {
511 *error
= EPWMD_ERROR
;
516 * Avoid calling pinentry if the password is cached on the server.
518 n
= pwmd_command(pwm
, &result
, error
, "ISCACHED %s", filename
);
520 if (n
&& (*error
== EPWMD_CACHE_NOT_FOUND
|| *error
== EPWMD_FILE_NOT_FOUND
)) {
522 password
= pwm
->passfunc(pwm
->passdata
);
524 if (!password
|| !*password
) {
529 password
= xstrdup(password
);
533 if (*error
== EPWMD_CACHE_NOT_FOUND
) {
536 * Get the password from pinentry.
538 if (pwm
->use_pinentry
) {
541 *error
= set_pinentry_strings(pwm
, 1);
544 pinentry_disconnect(pwm
);
549 *error
= getpin(pwm
, &password
);
552 if (pin_try
-- && *error
== EPWMD_KEY
)
556 pinentry_disconnect(pwm
);
564 * Not using pinentry and the file was not found
567 if (pwm
->password
== NULL
) {
572 password
= pwm
->password
;
577 else if (*error
!= EPWMD_FILE_NOT_FOUND
)
582 snprintf(buf
, sizeof(buf
), "OPEN %s %s", filename
, password
? password
: "");
583 *error
= send_command(pwm
, NULL
, buf
);
584 memset(&buf
, 0, sizeof(buf
));
588 if (pwm
->pctx
&& *error
== EPWMD_BADKEY
) {
592 pinentry_disconnect(pwm
);
597 pinentry_disconnect(pwm
);
600 if (!pwm
->use_pinentry
&& pwm
->password
)
601 pwm
->password
= NULL
;
605 xfree(pwm
->filename
);
607 pwm
->filename
= xstrdup(filename
);
610 return *error
? PWMD_ERROR
: PWMD_OK
;
613 pwmd_error_t
pwmd_save(pwm_t
*pwm
, gpg_error_t
*error
)
617 char *password
= NULL
;
619 static char buf
[1000];
625 if (pwm
->use_pinentry
|| pwm
->passfunc
) {
629 n
= pwmd_command(pwm
, &result
, error
, "ISCACHED %s", pwm
->filename
);
631 if (n
== PWMD_ERROR
&& (*error
== EPWMD_CACHE_NOT_FOUND
|| *error
== EPWMD_FILE_NOT_FOUND
)) {
635 if (pwm
->use_pinentry
) {
638 *error
= set_pinentry_strings(pwm
, 2);
642 pinentry_disconnect(pwm
);
651 *error
= getpin(pwm
, &result
);
655 pinentry_disconnect(pwm
);
668 if (strcmp(password
, result
)) {
671 pinentry_disconnect(pwm
);
672 *error
= EPWMD_BADKEY
;
677 pinentry_disconnect(pwm
);
682 password
= (*pwm
->passfunc
)(pwm
->passdata
);
693 if (!password
|| !*password
) {
698 p
= xstrdup(password
);
701 if (!pwm
->use_pinentry
) {
702 memset(password
, 0, strlen(password
));
709 memset(password
, 0, strlen(password
));
720 password
= pwm
->password
;
722 snprintf(buf
, sizeof(buf
), "SAVE %s", password
? password
: "");
723 *error
= send_command(pwm
, NULL
, buf
);
724 memset(&buf
, 0, sizeof(buf
));
726 pwm
->password
= NULL
;
727 return *error
? PWMD_ERROR
: PWMD_OK
;
730 pwmd_error_t
pwmd_setopt(pwm_t
*pwm
, gpg_error_t
*error
, pwmd_option_t opt
, ...)
734 int n
= va_arg(ap
, int);
744 case PWMD_OPTION_STATUS_FUNC
:
745 pwm
->status_func
= va_arg(ap
, pwmd_status_func
);
747 case PWMD_OPTION_STATUS_DATA
:
748 pwm
->status_data
= va_arg(ap
, void *);
750 case PWMD_OPTION_PASSWORD_FUNC
:
751 pwm
->passfunc
= va_arg(ap
, pwmd_password_func
);
753 case PWMD_OPTION_PASSWORD_DATA
:
754 pwm
->passdata
= va_arg(ap
, void *);
756 case PWMD_OPTION_PASSWORD
:
757 arg1
= va_arg(ap
, char *);
760 xfree(pwm
->password
);
762 pwm
->password
= xstrdup(arg1
);
765 case PWMD_OPTION_PINENTRY
:
768 if (n
!= 0 && n
!= 1) {
773 pwm
->use_pinentry
= n
;
775 case PWMD_OPTION_PINENTRY_TRIES
:
783 pwm
->pinentry_tries
= n
;
785 case PWMD_OPTION_PINENTRY_PATH
:
786 if (pwm
->pinentry_path
)
787 xfree(pwm
->pinentry_path
);
789 pwm
->pinentry_path
= xstrdup(va_arg(ap
, char *));
791 case PWMD_OPTION_PINENTRY_TTY
:
792 if (pwm
->pinentry_tty
)
793 xfree(pwm
->pinentry_tty
);
795 pwm
->pinentry_tty
= xstrdup(va_arg(ap
, char *));
797 case PWMD_OPTION_PINENTRY_DISPLAY
:
798 if (pwm
->pinentry_display
)
799 xfree(pwm
->pinentry_display
);
801 pwm
->pinentry_display
= xstrdup(va_arg(ap
, char *));
803 case PWMD_OPTION_PINENTRY_TERM
:
804 if (pwm
->pinentry_term
)
805 xfree(pwm
->pinentry_term
);
807 pwm
->pinentry_term
= xstrdup(va_arg(ap
, char *));
809 case PWMD_OPTION_PINENTRY_TITLE
:
812 pwm
->title
= percent_escape(va_arg(ap
, char *));
814 case PWMD_OPTION_PINENTRY_PROMPT
:
817 pwm
->prompt
= percent_escape(va_arg(ap
, char *));
819 case PWMD_OPTION_PINENTRY_DESC
:
822 pwm
->desc
= percent_escape(va_arg(ap
, char *));
825 case PWMD_OPTION_PINENTRY
:
826 case PWMD_OPTION_PINENTRY_TRIES
:
827 case PWMD_OPTION_PINENTRY_PATH
:
828 case PWMD_OPTION_PINENTRY_TTY
:
829 case PWMD_OPTION_PINENTRY_DISPLAY
:
830 case PWMD_OPTION_PINENTRY_TERM
:
831 case PWMD_OPTION_PINENTRY_TITLE
:
832 case PWMD_OPTION_PINENTRY_PROMPT
:
833 case PWMD_OPTION_PINENTRY_DESC
:
834 *error
= GPG_ERR_NOT_IMPLEMENTED
;
838 *error
= GPG_ERR_NOT_IMPLEMENTED
;
847 int pwmd_get_password(pwm_t
*pwm
, gpg_error_t
*error
, const char *filename
)
854 char *password
= NULL
;
857 memset(&pw
, 0, sizeof(pw
));
861 *error
= EPWMD_ERROR
;
866 n
= pwmd_command(pwm
, &result
, error
, "ISCACHED %s", filename
);
868 if (n
!= PWMD_OK
&& *error
!= EPWMD_CACHE_NOT_FOUND
&& *error
!= EPWMD_FILE_NOT_FOUND
)
871 * Don't need a password for non-existant files.
873 else if ((n
!= PWMD_OK
&& *error
== EPWMD_FILE_NOT_FOUND
) || n
== PWMD_OK
) {
881 *error
= gpg_error_from_syserror();
890 n
= pinentry_command(pwm
, &password
, "GETPIN");
896 pinentry_disconnect(pwm
);
899 write(p
[1], &pw
, sizeof(pw
));
903 if (!password
|| !*password
)
904 pw
.error
= EPWMD_KEY
;
906 strncpy(pw
.password
, password
, sizeof(pw
.password
));
908 write(p
[1], &pw
, sizeof(pw
));
909 memset(&pw
.password
, 0, sizeof(pw
.password
));
914 *error
= gpg_error_from_syserror();
925 *error
= GPG_ERR_NOT_IMPLEMENTED
;