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>
35 #include <sys/types.h>
54 #define PINENTRY_PATH "/usr/bin/pinentry"
56 static int gelapsed
, gtimeout
;
57 static gpg_error_t
pinentry_command(pwm_t
*pwm
, char **result
, const char *cmd
);
60 const char *pwmd_strerror(gpg_error_t error
)
62 gpg_err_code_t code
= gpg_err_code(error
);
64 if (code
>= GPG_ERR_USER_1
&& code
< gpg_err_code(EPWMD_MAX
)) {
68 return "Unknown error";
70 return "No cache slots available";
72 return "Element not found";
74 return "Trailing element";
76 return "Invalid character in element";
80 return "Will not overwrite existing account";
82 return "File not found";
84 return "No file is open";
86 return "General LibXML error";
88 return "File not found in cache";
90 return "Attribute not found";
92 return "Invalid filename or link";
94 return "File modified";
98 return gpg_strerror(error
);
101 gpg_error_t
pwmd_init()
104 assuan_set_malloc_hooks(xmalloc
, xrealloc
, xfree
);
105 assuan_set_assuan_err_source(GPG_ERR_SOURCE_DEFAULT
);
109 pwm_t
*pwmd_connect(const char *path
, gpg_error_t
*error
)
112 char *socketpath
= NULL
;
115 assuan_context_t ctx
;
119 pw
= getpwuid(getuid());
120 socketpath
= (char *)xmalloc(strlen(pw
->pw_dir
) + strlen("/.pwmd/socket") + 1);
121 sprintf(socketpath
, "%s/.pwmd/socket", pw
->pw_dir
);
124 socketpath
= xstrdup(path
);
126 rc
= assuan_socket_connect_ext(&ctx
, socketpath
, -1, 0);
134 if ((pwm
= (pwm_t
*)xcalloc(1, sizeof(pwm_t
))) == NULL
) {
135 *error
= gpg_error_from_errno(errno
);
136 assuan_disconnect(ctx
);
143 pwm
->pinentry_tries
= 3;
150 void pwmd_close(pwm_t
*pwm
)
156 assuan_disconnect(pwm
->ctx
);
159 xfree(pwm
->password
);
170 if (pwm
->pinentry_tty
)
171 xfree(pwm
->pinentry_tty
);
173 if (pwm
->pinentry_display
)
174 xfree(pwm
->pinentry_display
);
176 if (pwm
->pinentry_term
)
177 xfree(pwm
->pinentry_term
);
180 xfree(pwm
->filename
);
185 static int mem_realloc_cb(void *data
, const void *buffer
, size_t len
)
187 membuf_t
*mem
= (membuf_t
*)data
;
193 if ((p
= xrealloc(mem
->buf
, mem
->len
+ len
)) == NULL
)
197 memcpy((char *)mem
->buf
+ mem
->len
, buffer
, len
);
202 static int inquire_cb(void *data
, const char *keyword
)
204 struct inquire_s
*inquire
= (struct inquire_s
*)data
;
206 return assuan_send_data(inquire
->ctx
, inquire
->buf
, inquire
->len
);
209 void pwmd_free_result(void *data
)
214 static gpg_error_t
assuan_command(pwm_t
*pwm
, assuan_context_t ctx
,
215 char **result
, const char *cmd
)
224 * This is needed because assuan only accepts 1000 byte command strings.
225 * If the line is more than this the command will fail. So we use an
226 * INQUIRE on the server which waits for an END that assuan_transact()
229 * Other commands shouldn't need the INQUIRE. Let me know if you have an
230 * element path that's greater than 1000 bytes and I'll fix it.
232 if (strncasecmp(cmd
, "STORE", 5) == 0) {
233 const char *p
= cmd
+ 5;
234 struct inquire_s
*inq
= (struct inquire_s
*)xmalloc(sizeof(struct inquire_s
));
241 inq
->len
= strlen(p
);
242 rc
= assuan_transact(ctx
, "STORE", mem_realloc_cb
, &data
, inquire_cb
,
243 inq
, pwm
->status_func
, pwm
->status_data
);
249 * Ignore any status callback function for pinentry.
251 if (ctx
== pwm
->pctx
)
252 rc
= assuan_transact(ctx
, cmd
, mem_realloc_cb
, &data
, NULL
, NULL
, NULL
, NULL
);
255 rc
= assuan_transact(ctx
, cmd
, mem_realloc_cb
, &data
, NULL
, NULL
,
256 pwm
->status_func
, pwm
->status_data
);
267 mem_realloc_cb(&data
, "", 1);
268 *result
= (char *)data
.buf
;
272 return gpg_err_code(rc
);
275 gpg_error_t
pwmd_terminate_pinentry(pwm_t
*pwm
)
278 return GPG_ERR_NOT_IMPLEMENTED
;
281 if (!pwm
|| pwm
->pid
== -1)
282 return GPG_ERR_INV_ARG
;
284 if (kill(pwm
->pid
, 0) == 0) {
285 if (kill(pwm
->pid
, SIGTERM
) == -1) {
286 if (kill(pwm
->pid
, SIGKILL
) == -1)
287 return gpg_error_from_errno(errno
);
291 return gpg_error_from_errno(errno
);
297 static gpg_error_t
set_pinentry_strings(pwm_t
*pwm
, int which
)
303 pwm
->title
= xstrdup("LibPWMD");
306 pwm
->prompt
= xstrdup("Password:");
308 if (!pwm
->desc
&& !which
)
309 pwm
->desc
= xstrdup("Enter a password.");
312 buf
= xstrdup("SETERROR Invalid password, please try again.");
314 buf
= xstrdup("SETERROR Please type the password again for confirmation.");
316 buf
= (char *)xmalloc(strlen("SETERROR ") + strlen(pwm
->desc
) + 1);
317 sprintf(buf
, "SETERROR %s", pwm
->desc
);
320 error
= pinentry_command(pwm
, NULL
, buf
);
326 buf
= (char *)xmalloc(strlen("SETPROMPT ") + strlen(pwm
->prompt
) + 1);
327 sprintf(buf
, "SETPROMPT %s", pwm
->prompt
);
328 error
= pinentry_command(pwm
, NULL
, buf
);
334 buf
= (char *)xmalloc(strlen("SETDESC ") + strlen(pwm
->title
) + 1);
335 sprintf(buf
, "SETDESC %s", pwm
->title
);
336 error
= pinentry_command(pwm
, NULL
, buf
);
341 static void update_pinentry_settings(pwm_t
*pwm
)
345 struct passwd
*pw
= getpwuid(getuid());
348 snprintf(buf
, sizeof(buf
), "%s/.pwmd/env", pw
->pw_dir
);
350 if ((fp
= fopen(buf
, "r")) == NULL
)
353 while ((p
= fgets(buf
, sizeof(buf
), fp
)) != NULL
) {
354 char name
[32], val
[256];
356 if (sscanf(p
, "%[a-zA-Z]=%s", name
, val
) != 2)
359 if (strcasecmp(name
, "TTY") == 0) {
360 if (!pwm
->pinentry_tty
) {
361 xfree(pwm
->pinentry_tty
);
362 pwm
->pinentry_tty
= xstrdup(val
);
365 else if (strcasecmp(name
, "TERM") == 0) {
366 if (!pwm
->pinentry_term
) {
367 xfree(pwm
->pinentry_term
);
368 pwm
->pinentry_term
= xstrdup(val
);
371 else if (strcasecmp(name
, "DISPLAY") == 0) {
372 if (!pwm
->pinentry_display
) {
373 xfree(pwm
->pinentry_display
);
374 pwm
->pinentry_display
= xstrdup(val
);
382 static gpg_error_t
launch_pinentry(pwm_t
*pwm
)
385 assuan_context_t ctx
;
386 int child_list
[] = {-1};
387 char *display
= getenv("DISPLAY");
389 int have_display
= 0;
392 update_pinentry_settings(pwm
);
394 if (pwm
->pinentry_display
|| display
)
397 tty
= pwm
->pinentry_tty
? pwm
->pinentry_tty
: ttyname(STDOUT_FILENO
);
400 return gpg_error_from_errno(errno
);
403 if (!display
&& !tty
)
404 return GPG_ERR_ENOTTY
;
406 argv
[0] = "pinentry";
407 argv
[1] = have_display
? "--display" : "--ttyname";
408 argv
[2] = have_display
? pwm
->pinentry_display
? pwm
->pinentry_display
: display
: tty
;
412 argv
[3] = "--ttytype";
413 argv
[4] = pwm
->pinentry_term
? pwm
->pinentry_term
: getenv("TERM");
417 rc
= assuan_pipe_connect(&ctx
, pwm
->pinentry_path
? pwm
->pinentry_path
: PINENTRY_PATH
, argv
, child_list
);
422 pwm
->pid
= assuan_get_pid(ctx
);
424 return set_pinentry_strings(pwm
, 0);
427 static gpg_error_t
pinentry_command(pwm_t
*pwm
, char **result
, const char *cmd
)
432 n
= launch_pinentry(pwm
);
438 return assuan_command(pwm
, pwm
->pctx
, result
, cmd
);
441 static void pinentry_disconnect(pwm_t
*pwm
)
444 assuan_disconnect(pwm
->pctx
);
451 * Only called from a child process.
453 static void catchsig(int sig
)
457 if (gelapsed
++ >= gtimeout
) {
458 pwmd_terminate_pinentry(gpwm
);
469 static char *percent_escape(const char *atext
)
471 const unsigned char *s
;
472 int len
= strlen(atext
) * 3 + 1;
473 char *buf
= (char *)xmalloc(len
), *p
= buf
;
478 for (s
=(const unsigned char *)atext
; *s
; s
++) {
480 sprintf (p
, "%%%02X", *s
);
492 static gpg_error_t
send_command(pwm_t
*pwm
, char **result
, const char *cmd
)
495 return GPG_ERR_INV_ARG
;
497 return assuan_command(pwm
, pwm
->ctx
, result
, cmd
);
501 * Avoid sending the BYE command here. libassuan will close the file
502 * descriptor and release the assuan context. Use pwmd_close() instead.
504 gpg_error_t
pwmd_command(pwm_t
*pwm
, char **result
, const char *cmd
, ...)
512 return GPG_ERR_INV_ARG
;
519 len
= vsnprintf(NULL
, 0, cmd
, ap
);
520 buf
= (char *)xmalloc(len
+ 1);
521 len
= vsnprintf(buf
, len
+ 1, cmd
, ap
);
523 error
= send_command(pwm
, result
, buf
);
529 static gpg_error_t
do_getpin(pwm_t
*pwm
, char **result
)
534 signal(SIGALRM
, catchsig
);
539 error
= pinentry_command(pwm
, result
, "GETPIN");
541 if (error
== GPG_ERR_ASS_CANCELED
)
550 static gpg_error_t
getpin(pwm_t
*pwm
, char **result
, int *try_n
, int which
)
552 int pin_try
= *try_n
;
559 error
= set_pinentry_strings(pwm
, which
);
562 pinentry_disconnect(pwm
);
567 if (pwm
->pinentry_tries
-1 != pin_try
) {
568 error
= set_pinentry_strings(pwm
, 1);
571 pinentry_disconnect(pwm
);
577 error
= do_getpin(pwm
, result
);
580 if (pin_try
!= -1 && pin_try
-- && error
== EPWMD_KEY
)
584 pinentry_disconnect(pwm
);
594 gpg_error_t
pwmd_open_nb_finalize(pwm_t
*pwm
, pwmd_nb_status_t
*pw
)
600 return GPG_ERR_NOT_IMPLEMENTED
;
603 if (!pwm
|| !pw
|| !pw
->filename
[0])
604 return GPG_ERR_INV_ARG
;
613 error
= pwmd_command(pwm
, &result
, "ISCACHED %s", pw
->filename
);
619 xfree(pwm
->filename
);
621 pwm
->filename
= xstrdup(pw
->filename
);
622 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
626 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
630 static gpg_error_t
do_open_command(pwm_t
*pwm
, const char *filename
, char *password
)
635 snprintf(buf
, sizeof(buf
), "OPEN %s %s", filename
, password
? password
: "");
636 error
= send_command(pwm
, NULL
, buf
);
637 memset(&buf
, 0, sizeof(buf
));
642 static int do_pwmd_open(pwm_t
*pwm
, gpg_error_t
*error
, const char *filename
,
646 char *password
= NULL
;
648 int pin_try
= pwm
->pinentry_tries
- 1;
651 if (!pwm
|| !filename
|| !*filename
) {
652 *error
= GPG_ERR_INV_ARG
;
657 * Avoid calling pinentry if the password is cached on the server.
659 *error
= pwmd_command(pwm
, &result
, "ISCACHED %s", filename
);
661 if (*error
== EPWMD_CACHE_NOT_FOUND
) {
663 password
= pwm
->passfunc(pwm
, pwm
->passdata
);
665 if (!password
|| !*password
) {
670 password
= xstrdup(password
);
674 if (*error
== EPWMD_CACHE_NOT_FOUND
) {
677 * Get the password from pinentry.
679 if (pwm
->use_pinentry
) {
681 * Nonblocking is wanted. fork() then return a file descriptor
682 * that the client can use to read() from.
690 *error
= gpg_error_from_syserror();
699 strncpy(pw
.filename
, filename
, sizeof(pw
.filename
));
709 *error
= getpin(pwm
, &password
, &pin_try
, 0);
714 pinentry_disconnect(pwm
);
716 if (gtimeout
&& gelapsed
>= gtimeout
)
717 *error
= GPG_ERR_TIMEOUT
;
720 write(p
[1], &pw
, sizeof(pw
));
726 * Don't count the time it takes to open the file
727 * which may have many iterations.
729 signal(SIGALRM
, SIG_DFL
);
730 *error
= do_open_command(pwm
, filename
, password
);
733 signal(SIGALRM
, catchsig
);
735 if (pwm
->pctx
&& *error
== EPWMD_BADKEY
) {
737 goto getpin_nb_again
;
742 pinentry_disconnect(pwm
);
744 write(p
[1], &pw
, sizeof(pw
));
749 *error
= gpg_error_from_syserror();
762 *error
= getpin(pwm
, &password
, &pin_try
, 1);
766 pinentry_disconnect(pwm
);
774 * Not using pinentry and the file was not found
777 if (pwm
->password
== NULL
) {
782 password
= pwm
->password
;
788 else if (*error
&& *error
!= EPWMD_FILE_NOT_FOUND
)
792 *error
= do_open_command(pwm
, filename
, password
);
795 if (pwm
->pctx
&& *error
== EPWMD_BADKEY
) {
799 pinentry_disconnect(pwm
);
804 if (!pwm
->use_pinentry
&& pwm
->password
)
805 pwm
->password
= NULL
;
809 xfree(pwm
->filename
);
811 pwm
->filename
= xstrdup(filename
);
815 * The file is cached or the file is a new file.
818 return *error
? -1 : -2;
820 return *error
? 1 : 0;
823 gpg_error_t
pwmd_open(pwm_t
*pwm
, const char *filename
)
827 do_pwmd_open(pwm
, &error
, filename
, 0, 0);
831 int pwmd_open_nb(pwm_t
*pwm
, gpg_error_t
*error
, const char *filename
,
835 *error
= GPG_ERR_NOT_IMPLEMENTED
;
838 return do_pwmd_open(pwm
, error
, filename
, 1, timeout
);
843 static gpg_error_t
do_save_getpin(pwm_t
*pwm
, char **password
)
851 error
= getpin(pwm
, &result
, &pin_try
, confirm
? 2 : 0);
855 pinentry_disconnect(pwm
);
868 if (strcmp(*password
, result
)) {
871 pinentry_disconnect(pwm
);
872 error
= EPWMD_BADKEY
;
877 pinentry_disconnect(pwm
);
882 static gpg_error_t
do_save_command(pwm_t
*pwm
, char *password
)
884 char buf
[ASSUAN_LINELENGTH
];
887 snprintf(buf
, sizeof(buf
), "SAVE %s", password
? password
: "");
888 error
= send_command(pwm
, NULL
, buf
);
889 memset(&buf
, 0, sizeof(buf
));
891 pwm
->password
= NULL
;
895 gpg_error_t
pwmd_save_nb_finalize(pwm_t
*pwm
, pwmd_nb_status_t
*pw
)
900 return GPG_ERR_NOT_IMPLEMENTED
;
903 if (!pwm
|| !pw
|| !pw
->filename
[0])
904 return GPG_ERR_INV_ARG
;
910 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
914 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
918 static int do_pwmd_save(pwm_t
*pwm
, gpg_error_t
*error
, int nb
)
921 char *password
= NULL
;
924 *error
= GPG_ERR_INV_ARG
;
928 if (pwm
->use_pinentry
|| pwm
->passfunc
) {
929 *error
= pwmd_command(pwm
, &result
, "ISCACHED %s", pwm
->filename
);
931 if (*error
== EPWMD_CACHE_NOT_FOUND
) {
933 if (pwm
->use_pinentry
) {
940 *error
= gpg_error_from_syserror();
949 strncpy(pw
.filename
, pwm
->filename
, sizeof(pw
.filename
));
954 *error
= do_save_getpin(pwm
, &password
);
955 } while (*error
== EPWMD_KEY
|| *error
== EPWMD_BADKEY
);
959 pinentry_disconnect(pwm
);
962 write(p
[1], &pw
, sizeof(pw
));
967 *error
= do_save_command(pwm
, password
);
968 pinentry_disconnect(pwm
);
970 write(p
[1], &pw
, sizeof(pw
));
975 *error
= gpg_error_from_syserror();
987 *error
= do_save_getpin(pwm
, &password
);
995 char *tmp
= (*pwm
->passfunc
)(pwm
, pwm
->passdata
);
1002 password
= xstrdup(tmp
);
1008 if (!password
|| !*password
) {
1014 if (*error
&& *error
!= EPWMD_FILE_NOT_FOUND
)
1019 password
= pwm
->password
;
1021 *error
= do_save_command(pwm
, password
);
1024 return *error
? -1 : -2;
1026 return *error
? 1 : 0;
1029 int pwmd_save_nb(pwm_t
*pwm
, gpg_error_t
*error
)
1031 #ifndef USE_PINENTRY
1032 *error
= GPG_ERR_NOT_IMPLEMENTED
;
1035 return do_pwmd_save(pwm
, error
, 1);
1039 gpg_error_t
pwmd_save(pwm_t
*pwm
)
1043 do_pwmd_save(pwm
, &error
, 0);
1047 gpg_error_t
pwmd_setopt(pwm_t
*pwm
, pwmd_option_t opt
, ...)
1051 int n
= va_arg(ap
, int);
1056 return GPG_ERR_INV_ARG
;
1061 case PWMD_OPTION_STATUS_FUNC
:
1062 pwm
->status_func
= va_arg(ap
, pwmd_status_fn
);
1064 case PWMD_OPTION_STATUS_DATA
:
1065 pwm
->status_data
= va_arg(ap
, void *);
1067 case PWMD_OPTION_PASSWORD_FUNC
:
1068 pwm
->passfunc
= va_arg(ap
, pwmd_password_fn
);
1070 case PWMD_OPTION_PASSWORD_DATA
:
1071 pwm
->passdata
= va_arg(ap
, void *);
1073 case PWMD_OPTION_PASSWORD
:
1074 arg1
= va_arg(ap
, char *);
1077 xfree(pwm
->password
);
1079 pwm
->password
= xstrdup(arg1
);
1082 case PWMD_OPTION_PINENTRY
:
1083 n
= va_arg(ap
, int);
1085 if (n
!= 0 && n
!= 1) {
1087 return GPG_ERR_INV_VALUE
;
1090 pwm
->use_pinentry
= n
;
1092 case PWMD_OPTION_PINENTRY_TRIES
:
1093 n
= va_arg(ap
, int);
1097 return GPG_ERR_INV_VALUE
;
1100 pwm
->pinentry_tries
= n
;
1102 case PWMD_OPTION_PINENTRY_PATH
:
1103 if (pwm
->pinentry_path
)
1104 xfree(pwm
->pinentry_path
);
1106 pwm
->pinentry_path
= xstrdup(va_arg(ap
, char *));
1108 case PWMD_OPTION_PINENTRY_TTY
:
1109 if (pwm
->pinentry_tty
)
1110 xfree(pwm
->pinentry_tty
);
1112 pwm
->pinentry_tty
= xstrdup(va_arg(ap
, char *));
1114 case PWMD_OPTION_PINENTRY_DISPLAY
:
1115 if (pwm
->pinentry_display
)
1116 xfree(pwm
->pinentry_display
);
1118 pwm
->pinentry_display
= xstrdup(va_arg(ap
, char *));
1120 case PWMD_OPTION_PINENTRY_TERM
:
1121 if (pwm
->pinentry_term
)
1122 xfree(pwm
->pinentry_term
);
1124 pwm
->pinentry_term
= xstrdup(va_arg(ap
, char *));
1126 case PWMD_OPTION_PINENTRY_TITLE
:
1129 pwm
->title
= percent_escape(va_arg(ap
, char *));
1131 case PWMD_OPTION_PINENTRY_PROMPT
:
1134 pwm
->prompt
= percent_escape(va_arg(ap
, char *));
1136 case PWMD_OPTION_PINENTRY_DESC
:
1139 pwm
->desc
= percent_escape(va_arg(ap
, char *));
1142 case PWMD_OPTION_PINENTRY
:
1143 case PWMD_OPTION_PINENTRY_TRIES
:
1144 case PWMD_OPTION_PINENTRY_PATH
:
1145 case PWMD_OPTION_PINENTRY_TTY
:
1146 case PWMD_OPTION_PINENTRY_DISPLAY
:
1147 case PWMD_OPTION_PINENTRY_TERM
:
1148 case PWMD_OPTION_PINENTRY_TITLE
:
1149 case PWMD_OPTION_PINENTRY_PROMPT
:
1150 case PWMD_OPTION_PINENTRY_DESC
:
1151 return GPG_ERR_NOT_IMPLEMENTED
;
1155 return GPG_ERR_NOT_IMPLEMENTED
;