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>
55 #define PINENTRY_PATH "/usr/bin/pinentry"
57 static int gelapsed
, gtimeout
;
58 static gpg_error_t
pinentry_command(pwm_t
*pwm
, char **result
, const char *cmd
);
61 const char *pwmd_strerror(gpg_error_t error
)
63 gpg_err_code_t code
= gpg_err_code(error
);
65 if (code
>= GPG_ERR_USER_1
&& code
< gpg_err_code(EPWMD_MAX
)) {
69 return "Unknown error";
71 return "No cache slots available";
73 return "Element not found";
75 return "Trailing element";
77 return "Invalid character in element";
81 return "Will not overwrite existing account";
83 return "File not found";
85 return "No file is open";
87 return "General LibXML error";
89 return "File not found in cache";
91 return "Attribute not found";
93 return "Invalid filename or link";
95 return "File modified";
99 return gpg_strerror(error
);
102 gpg_error_t
pwmd_init()
105 assuan_set_malloc_hooks(xmalloc
, xrealloc
, xfree
);
106 assuan_set_assuan_err_source(GPG_ERR_SOURCE_DEFAULT
);
110 pwm_t
*pwmd_connect(const char *path
, gpg_error_t
*error
)
113 char *socketpath
= NULL
;
116 assuan_context_t ctx
;
120 pw
= getpwuid(getuid());
121 socketpath
= (char *)xmalloc(strlen(pw
->pw_dir
) + strlen("/.pwmd/socket") + 1);
122 sprintf(socketpath
, "%s/.pwmd/socket", pw
->pw_dir
);
125 socketpath
= xstrdup(path
);
127 rc
= assuan_socket_connect_ext(&ctx
, socketpath
, -1, 0);
135 if ((pwm
= (pwm_t
*)xcalloc(1, sizeof(pwm_t
))) == NULL
) {
136 *error
= gpg_error_from_errno(errno
);
137 assuan_disconnect(ctx
);
144 pwm
->pinentry_tries
= 3;
151 void pwmd_close(pwm_t
*pwm
)
157 assuan_disconnect(pwm
->ctx
);
160 xfree(pwm
->password
);
171 if (pwm
->pinentry_tty
)
172 xfree(pwm
->pinentry_tty
);
174 if (pwm
->pinentry_display
)
175 xfree(pwm
->pinentry_display
);
177 if (pwm
->pinentry_term
)
178 xfree(pwm
->pinentry_term
);
181 xfree(pwm
->filename
);
186 static int mem_realloc_cb(void *data
, const void *buffer
, size_t len
)
188 membuf_t
*mem
= (membuf_t
*)data
;
194 if ((p
= xrealloc(mem
->buf
, mem
->len
+ len
)) == NULL
)
198 memcpy((char *)mem
->buf
+ mem
->len
, buffer
, len
);
203 static int inquire_cb(void *data
, const char *keyword
)
205 struct inquire_s
*inquire
= (struct inquire_s
*)data
;
207 return assuan_send_data(inquire
->ctx
, inquire
->buf
, inquire
->len
);
210 void pwmd_free_result(void *data
)
215 static gpg_error_t
assuan_command(pwm_t
*pwm
, assuan_context_t ctx
,
216 char **result
, const char *cmd
)
225 * This is needed because assuan only accepts 1000 byte command strings.
226 * If the line is more than this the command will fail. So we use an
227 * INQUIRE on the server which waits for an END that assuan_transact()
230 * Other commands shouldn't need the INQUIRE. Let me know if you have an
231 * element path that's greater than 1000 bytes and I'll fix it.
233 if (strncasecmp(cmd
, "STORE", 5) == 0) {
234 const char *p
= cmd
+ 5;
235 struct inquire_s
*inq
= (struct inquire_s
*)xmalloc(sizeof(struct inquire_s
));
242 inq
->len
= strlen(p
);
243 rc
= assuan_transact(ctx
, "STORE", mem_realloc_cb
, &data
, inquire_cb
,
244 inq
, pwm
->status_func
, pwm
->status_data
);
250 * Ignore any status callback function for pinentry.
252 if (ctx
== pwm
->pctx
)
253 rc
= assuan_transact(ctx
, cmd
, mem_realloc_cb
, &data
, NULL
, NULL
, NULL
, NULL
);
256 rc
= assuan_transact(ctx
, cmd
, mem_realloc_cb
, &data
, NULL
, NULL
,
257 pwm
->status_func
, pwm
->status_data
);
268 mem_realloc_cb(&data
, "", 1);
269 *result
= (char *)data
.buf
;
273 return gpg_err_code(rc
);
276 gpg_error_t
pwmd_terminate_pinentry(pwm_t
*pwm
)
279 return GPG_ERR_NOT_IMPLEMENTED
;
282 if (!pwm
|| pwm
->pid
== -1)
283 return GPG_ERR_INV_ARG
;
285 if (kill(pwm
->pid
, 0) == 0) {
286 if (kill(pwm
->pid
, SIGTERM
) == -1) {
287 if (kill(pwm
->pid
, SIGKILL
) == -1)
288 return gpg_error_from_errno(errno
);
292 return gpg_error_from_errno(errno
);
298 static gpg_error_t
set_pinentry_strings(pwm_t
*pwm
, int which
)
304 pwm
->title
= xstrdup("LibPWMD");
307 pwm
->prompt
= xstrdup("Password:");
309 if (!pwm
->desc
&& !which
)
310 pwm
->desc
= xstrdup("Enter a password.");
313 buf
= xstrdup("SETERROR Invalid password, please try again.");
315 buf
= xstrdup("SETERROR Please type the password again for confirmation.");
317 buf
= (char *)xmalloc(strlen("SETERROR ") + strlen(pwm
->desc
) + 1);
318 sprintf(buf
, "SETERROR %s", pwm
->desc
);
321 error
= pinentry_command(pwm
, NULL
, buf
);
327 buf
= (char *)xmalloc(strlen("SETPROMPT ") + strlen(pwm
->prompt
) + 1);
328 sprintf(buf
, "SETPROMPT %s", pwm
->prompt
);
329 error
= pinentry_command(pwm
, NULL
, buf
);
335 buf
= (char *)xmalloc(strlen("SETDESC ") + strlen(pwm
->title
) + 1);
336 sprintf(buf
, "SETDESC %s", pwm
->title
);
337 error
= pinentry_command(pwm
, NULL
, buf
);
342 static void update_pinentry_settings(pwm_t
*pwm
)
346 struct passwd
*pw
= getpwuid(getuid());
349 snprintf(buf
, sizeof(buf
), "%s/.pwmd/env", pw
->pw_dir
);
351 if ((fp
= fopen(buf
, "r")) == NULL
)
354 while ((p
= fgets(buf
, sizeof(buf
), fp
)) != NULL
) {
355 char name
[32], val
[256];
357 if (sscanf(p
, "%[a-zA-Z]=%s", name
, val
) != 2)
360 if (strcasecmp(name
, "TTY") == 0) {
361 if (!pwm
->pinentry_tty
) {
362 xfree(pwm
->pinentry_tty
);
363 pwm
->pinentry_tty
= xstrdup(val
);
366 else if (strcasecmp(name
, "TERM") == 0) {
367 if (!pwm
->pinentry_term
) {
368 xfree(pwm
->pinentry_term
);
369 pwm
->pinentry_term
= xstrdup(val
);
372 else if (strcasecmp(name
, "DISPLAY") == 0) {
373 if (!pwm
->pinentry_display
) {
374 xfree(pwm
->pinentry_display
);
375 pwm
->pinentry_display
= xstrdup(val
);
383 static gpg_error_t
launch_pinentry(pwm_t
*pwm
)
386 assuan_context_t ctx
;
387 int child_list
[] = {-1};
388 char *display
= getenv("DISPLAY");
390 int have_display
= 0;
393 update_pinentry_settings(pwm
);
395 if (pwm
->pinentry_display
|| display
)
398 tty
= pwm
->pinentry_tty
? pwm
->pinentry_tty
: ttyname(STDOUT_FILENO
);
401 return gpg_error_from_errno(errno
);
404 if (!display
&& !tty
)
405 return GPG_ERR_ENOTTY
;
407 argv
[0] = "pinentry";
408 argv
[1] = have_display
? "--display" : "--ttyname";
409 argv
[2] = have_display
? pwm
->pinentry_display
? pwm
->pinentry_display
: display
: tty
;
413 argv
[3] = "--ttytype";
414 argv
[4] = pwm
->pinentry_term
? pwm
->pinentry_term
: getenv("TERM");
418 rc
= assuan_pipe_connect(&ctx
, pwm
->pinentry_path
? pwm
->pinentry_path
: PINENTRY_PATH
, argv
, child_list
);
423 pwm
->pid
= assuan_get_pid(ctx
);
425 return set_pinentry_strings(pwm
, 0);
428 static gpg_error_t
pinentry_command(pwm_t
*pwm
, char **result
, const char *cmd
)
433 n
= launch_pinentry(pwm
);
439 return assuan_command(pwm
, pwm
->pctx
, result
, cmd
);
442 static void pinentry_disconnect(pwm_t
*pwm
)
445 assuan_disconnect(pwm
->pctx
);
452 * Only called from a child process.
454 static void catchsig(int sig
)
458 if (gelapsed
++ >= gtimeout
) {
459 pwmd_terminate_pinentry(gpwm
);
470 static char *percent_escape(const char *atext
)
472 const unsigned char *s
;
473 int len
= strlen(atext
) * 3 + 1;
474 char *buf
= (char *)xmalloc(len
), *p
= buf
;
479 for (s
=(const unsigned char *)atext
; *s
; s
++) {
481 sprintf (p
, "%%%02X", *s
);
493 static gpg_error_t
send_command(pwm_t
*pwm
, char **result
, const char *cmd
)
496 return GPG_ERR_INV_ARG
;
498 return assuan_command(pwm
, pwm
->ctx
, result
, cmd
);
502 * Avoid sending the BYE command here. libassuan will close the file
503 * descriptor and release the assuan context. Use pwmd_close() instead.
505 gpg_error_t
pwmd_command(pwm_t
*pwm
, char **result
, const char *cmd
, ...)
513 return GPG_ERR_INV_ARG
;
520 len
= vsnprintf(NULL
, 0, cmd
, ap
);
521 buf
= (char *)xmalloc(len
+ 1);
522 len
= vsnprintf(buf
, len
+ 1, cmd
, ap
);
524 error
= send_command(pwm
, result
, buf
);
530 static gpg_error_t
do_getpin(pwm_t
*pwm
, char **result
)
535 signal(SIGALRM
, catchsig
);
540 error
= pinentry_command(pwm
, result
, "GETPIN");
542 if (error
== GPG_ERR_ASS_CANCELED
)
551 static gpg_error_t
getpin(pwm_t
*pwm
, char **result
, int *try_n
, int which
)
553 int pin_try
= *try_n
;
560 error
= set_pinentry_strings(pwm
, which
);
563 pinentry_disconnect(pwm
);
568 if (pwm
->pinentry_tries
-1 != pin_try
) {
569 error
= set_pinentry_strings(pwm
, 1);
572 pinentry_disconnect(pwm
);
578 error
= do_getpin(pwm
, result
);
581 if (pin_try
!= -1 && pin_try
-- && error
== EPWMD_KEY
)
585 pinentry_disconnect(pwm
);
595 gpg_error_t
pwmd_open_nb_finalize(pwm_t
*pwm
, pwmd_nb_status_t
*pw
)
601 return GPG_ERR_NOT_IMPLEMENTED
;
604 if (!pwm
|| !pw
|| !pw
->filename
[0])
605 return GPG_ERR_INV_ARG
;
614 error
= pwmd_command(pwm
, &result
, "ISCACHED %s", pw
->filename
);
620 xfree(pwm
->filename
);
622 pwm
->filename
= xstrdup(pw
->filename
);
623 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
627 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
631 static gpg_error_t
do_open_command(pwm_t
*pwm
, const char *filename
, char *password
)
636 snprintf(buf
, sizeof(buf
), "OPEN %s %s", filename
, password
? password
: "");
637 error
= send_command(pwm
, NULL
, buf
);
638 memset(&buf
, 0, sizeof(buf
));
643 static int do_pwmd_open(pwm_t
*pwm
, gpg_error_t
*error
, const char *filename
,
647 char *password
= NULL
;
649 int pin_try
= pwm
->pinentry_tries
- 1;
652 if (!pwm
|| !filename
|| !*filename
) {
653 *error
= GPG_ERR_INV_ARG
;
658 * Avoid calling pinentry if the password is cached on the server.
660 *error
= pwmd_command(pwm
, &result
, "ISCACHED %s", filename
);
662 if (*error
== EPWMD_CACHE_NOT_FOUND
) {
664 password
= pwm
->passfunc(pwm
, pwm
->passdata
);
666 if (!password
|| !*password
) {
671 password
= xstrdup(password
);
675 if (*error
== EPWMD_CACHE_NOT_FOUND
) {
678 * Get the password from pinentry.
680 if (pwm
->use_pinentry
) {
682 * Nonblocking is wanted. fork() then return a file descriptor
683 * that the client can use to read() from.
691 *error
= gpg_error_from_syserror();
700 strncpy(pw
.filename
, filename
, sizeof(pw
.filename
));
710 *error
= getpin(pwm
, &password
, &pin_try
, 0);
715 pinentry_disconnect(pwm
);
717 if (gtimeout
&& gelapsed
>= gtimeout
)
718 *error
= GPG_ERR_TIMEOUT
;
721 write(p
[1], &pw
, sizeof(pw
));
727 * Don't count the time it takes to open the file
728 * which may have many iterations.
730 signal(SIGALRM
, SIG_DFL
);
731 *error
= do_open_command(pwm
, filename
, password
);
734 signal(SIGALRM
, catchsig
);
736 if (pwm
->pctx
&& *error
== EPWMD_BADKEY
) {
738 goto getpin_nb_again
;
743 pinentry_disconnect(pwm
);
745 write(p
[1], &pw
, sizeof(pw
));
750 *error
= gpg_error_from_syserror();
763 *error
= getpin(pwm
, &password
, &pin_try
, 1);
767 pinentry_disconnect(pwm
);
775 * Not using pinentry and the file was not found
778 if (pwm
->password
== NULL
) {
783 password
= pwm
->password
;
789 else if (*error
&& *error
!= EPWMD_FILE_NOT_FOUND
)
793 *error
= do_open_command(pwm
, filename
, password
);
796 if (pwm
->pctx
&& *error
== EPWMD_BADKEY
) {
800 pinentry_disconnect(pwm
);
805 if (!pwm
->use_pinentry
&& pwm
->password
)
806 pwm
->password
= NULL
;
810 xfree(pwm
->filename
);
812 pwm
->filename
= xstrdup(filename
);
816 * The file is cached or the file is a new file.
819 return *error
? -1 : -2;
821 return *error
? 1 : 0;
824 gpg_error_t
pwmd_open(pwm_t
*pwm
, const char *filename
)
828 do_pwmd_open(pwm
, &error
, filename
, 0, 0);
832 int pwmd_open_nb(pwm_t
*pwm
, gpg_error_t
*error
, const char *filename
,
836 *error
= GPG_ERR_NOT_IMPLEMENTED
;
839 return do_pwmd_open(pwm
, error
, filename
, 1, timeout
);
844 static gpg_error_t
do_save_getpin(pwm_t
*pwm
, char **password
)
852 error
= getpin(pwm
, &result
, &pin_try
, confirm
? 2 : 0);
856 pinentry_disconnect(pwm
);
869 if (strcmp(*password
, result
)) {
872 pinentry_disconnect(pwm
);
873 error
= EPWMD_BADKEY
;
878 pinentry_disconnect(pwm
);
883 static gpg_error_t
do_save_command(pwm_t
*pwm
, char *password
)
885 char buf
[ASSUAN_LINELENGTH
];
888 snprintf(buf
, sizeof(buf
), "SAVE %s", password
? password
: "");
889 error
= send_command(pwm
, NULL
, buf
);
890 memset(&buf
, 0, sizeof(buf
));
892 pwm
->password
= NULL
;
896 gpg_error_t
pwmd_save_nb_finalize(pwm_t
*pwm
, pwmd_nb_status_t
*pw
)
901 return GPG_ERR_NOT_IMPLEMENTED
;
904 if (!pwm
|| !pw
|| !pw
->filename
[0])
905 return GPG_ERR_INV_ARG
;
911 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
915 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
919 static int do_pwmd_save(pwm_t
*pwm
, gpg_error_t
*error
, int nb
)
922 char *password
= NULL
;
925 *error
= GPG_ERR_INV_ARG
;
929 if (pwm
->use_pinentry
|| pwm
->passfunc
) {
930 *error
= pwmd_command(pwm
, &result
, "ISCACHED %s", pwm
->filename
);
932 if (*error
== EPWMD_CACHE_NOT_FOUND
) {
934 if (pwm
->use_pinentry
) {
941 *error
= gpg_error_from_syserror();
950 strncpy(pw
.filename
, pwm
->filename
, sizeof(pw
.filename
));
955 *error
= do_save_getpin(pwm
, &password
);
956 } while (*error
== EPWMD_KEY
|| *error
== EPWMD_BADKEY
);
960 pinentry_disconnect(pwm
);
963 write(p
[1], &pw
, sizeof(pw
));
968 *error
= do_save_command(pwm
, password
);
969 pinentry_disconnect(pwm
);
971 write(p
[1], &pw
, sizeof(pw
));
976 *error
= gpg_error_from_syserror();
988 *error
= do_save_getpin(pwm
, &password
);
996 char *tmp
= (*pwm
->passfunc
)(pwm
, pwm
->passdata
);
1003 password
= xstrdup(tmp
);
1009 if (!password
|| !*password
) {
1015 if (*error
&& *error
!= EPWMD_FILE_NOT_FOUND
)
1020 password
= pwm
->password
;
1022 *error
= do_save_command(pwm
, password
);
1025 return *error
? -1 : -2;
1027 return *error
? 1 : 0;
1030 int pwmd_save_nb(pwm_t
*pwm
, gpg_error_t
*error
)
1032 #ifndef USE_PINENTRY
1033 *error
= GPG_ERR_NOT_IMPLEMENTED
;
1036 return do_pwmd_save(pwm
, error
, 1);
1040 gpg_error_t
pwmd_save(pwm_t
*pwm
)
1044 do_pwmd_save(pwm
, &error
, 0);
1048 gpg_error_t
pwmd_setopt(pwm_t
*pwm
, pwmd_option_t opt
, ...)
1052 int n
= va_arg(ap
, int);
1057 return GPG_ERR_INV_ARG
;
1062 case PWMD_OPTION_STATUS_FUNC
:
1063 pwm
->status_func
= va_arg(ap
, pwmd_status_fn
);
1065 case PWMD_OPTION_STATUS_DATA
:
1066 pwm
->status_data
= va_arg(ap
, void *);
1068 case PWMD_OPTION_PASSWORD_FUNC
:
1069 pwm
->passfunc
= va_arg(ap
, pwmd_password_fn
);
1071 case PWMD_OPTION_PASSWORD_DATA
:
1072 pwm
->passdata
= va_arg(ap
, void *);
1074 case PWMD_OPTION_PASSWORD
:
1075 arg1
= va_arg(ap
, char *);
1078 xfree(pwm
->password
);
1080 pwm
->password
= xstrdup(arg1
);
1083 case PWMD_OPTION_PINENTRY
:
1084 n
= va_arg(ap
, int);
1086 if (n
!= 0 && n
!= 1) {
1088 return GPG_ERR_INV_VALUE
;
1091 pwm
->use_pinentry
= n
;
1093 case PWMD_OPTION_PINENTRY_TRIES
:
1094 n
= va_arg(ap
, int);
1098 return GPG_ERR_INV_VALUE
;
1101 pwm
->pinentry_tries
= n
;
1103 case PWMD_OPTION_PINENTRY_PATH
:
1104 if (pwm
->pinentry_path
)
1105 xfree(pwm
->pinentry_path
);
1107 pwm
->pinentry_path
= xstrdup(va_arg(ap
, char *));
1109 case PWMD_OPTION_PINENTRY_TTY
:
1110 if (pwm
->pinentry_tty
)
1111 xfree(pwm
->pinentry_tty
);
1113 pwm
->pinentry_tty
= xstrdup(va_arg(ap
, char *));
1115 case PWMD_OPTION_PINENTRY_DISPLAY
:
1116 if (pwm
->pinentry_display
)
1117 xfree(pwm
->pinentry_display
);
1119 pwm
->pinentry_display
= xstrdup(va_arg(ap
, char *));
1121 case PWMD_OPTION_PINENTRY_TERM
:
1122 if (pwm
->pinentry_term
)
1123 xfree(pwm
->pinentry_term
);
1125 pwm
->pinentry_term
= xstrdup(va_arg(ap
, char *));
1127 case PWMD_OPTION_PINENTRY_TITLE
:
1130 pwm
->title
= percent_escape(va_arg(ap
, char *));
1132 case PWMD_OPTION_PINENTRY_PROMPT
:
1135 pwm
->prompt
= percent_escape(va_arg(ap
, char *));
1137 case PWMD_OPTION_PINENTRY_DESC
:
1140 pwm
->desc
= percent_escape(va_arg(ap
, char *));
1143 case PWMD_OPTION_PINENTRY
:
1144 case PWMD_OPTION_PINENTRY_TRIES
:
1145 case PWMD_OPTION_PINENTRY_PATH
:
1146 case PWMD_OPTION_PINENTRY_TTY
:
1147 case PWMD_OPTION_PINENTRY_DISPLAY
:
1148 case PWMD_OPTION_PINENTRY_TERM
:
1149 case PWMD_OPTION_PINENTRY_TITLE
:
1150 case PWMD_OPTION_PINENTRY_PROMPT
:
1151 case PWMD_OPTION_PINENTRY_DESC
:
1152 return GPG_ERR_NOT_IMPLEMENTED
;
1156 return GPG_ERR_NOT_IMPLEMENTED
;