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>
52 #define N_(msgid) dgettext("libpwmd", msgid)
58 #define xrealloc realloc
59 #define xmalloc malloc
60 #define xstrdup strdup
61 #define xcalloc calloc
68 static int gelapsed
, gtimeout
;
69 static gpg_error_t
pinentry_command(pwm_t
*pwm
, char **result
, const char *cmd
);
72 static gpg_error_t global_error
;
74 const char *pwmd_strerror(gpg_error_t error
)
76 gpg_err_code_t code
= gpg_err_code(error
);
78 if (code
>= GPG_ERR_USER_1
&& code
< gpg_err_code(EPWMD_MAX
)) {
82 return N_("Unknown error");
84 return N_("No cache slots available");
86 return N_("Element not found");
88 return N_("Trailing element");
90 return N_("Invalid character in element");
94 return N_("Will not overwrite existing account");
96 return N_("File not found");
98 return N_("No file is open");
100 return N_("General LibXML error");
101 case GPG_ERR_USER_11
:
102 return N_("File not found in cache");
103 case GPG_ERR_USER_12
:
104 return N_("Attribute not found");
105 case GPG_ERR_USER_13
:
106 return N_("Invalid filename or link");
107 case GPG_ERR_USER_14
:
108 return N_("File modified");
112 return gpg_strerror(error
);
115 gpg_error_t
pwmd_init()
118 bindtextdomain("libpwmd", LOCALEDIR
);
121 assuan_set_malloc_hooks(xmalloc
, xrealloc
, xfree
);
122 assuan_set_assuan_err_source(GPG_ERR_SOURCE_DEFAULT
);
126 pwm_t
*pwmd_connect(const char *path
, gpg_error_t
*error
)
129 char *socketpath
= NULL
;
132 assuan_context_t ctx
;
136 pw
= getpwuid(getuid());
137 socketpath
= (char *)xmalloc(strlen(pw
->pw_dir
) + strlen("/.pwmd/socket") + 1);
138 sprintf(socketpath
, "%s/.pwmd/socket", pw
->pw_dir
);
141 socketpath
= xstrdup(path
);
143 rc
= assuan_socket_connect_ext(&ctx
, socketpath
, -1, 0);
151 if ((pwm
= (pwm_t
*)xcalloc(1, sizeof(pwm_t
))) == NULL
) {
152 *error
= gpg_error_from_errno(errno
);
153 assuan_disconnect(ctx
);
160 pwm
->pinentry_tries
= 3;
167 void pwmd_close(pwm_t
*pwm
)
173 assuan_disconnect(pwm
->ctx
);
176 xfree(pwm
->password
);
187 if (pwm
->pinentry_tty
)
188 xfree(pwm
->pinentry_tty
);
190 if (pwm
->pinentry_display
)
191 xfree(pwm
->pinentry_display
);
193 if (pwm
->pinentry_term
)
194 xfree(pwm
->pinentry_term
);
197 xfree(pwm
->filename
);
202 static int mem_realloc_cb(void *data
, const void *buffer
, size_t len
)
204 membuf_t
*mem
= (membuf_t
*)data
;
210 if ((p
= xrealloc(mem
->buf
, mem
->len
+ len
)) == NULL
)
214 memcpy((char *)mem
->buf
+ mem
->len
, buffer
, len
);
219 static int inquire_cb(void *data
, const char *keyword
)
221 struct inquire_s
*inquire
= (struct inquire_s
*)data
;
223 return assuan_send_data(inquire
->ctx
, inquire
->buf
, inquire
->len
);
226 void pwmd_free_result(void *data
)
231 static gpg_error_t
assuan_command(pwm_t
*pwm
, assuan_context_t ctx
,
232 char **result
, const char *cmd
)
241 * This is needed because assuan only accepts 1000 byte command strings.
242 * If the line is more than this the command will fail. So we use an
243 * INQUIRE on the server which waits for an END that assuan_transact()
246 * Other commands shouldn't need the INQUIRE. Let me know if you have an
247 * element path that's greater than 1000 bytes and I'll fix it.
249 if (strncasecmp(cmd
, "STORE", 5) == 0) {
250 const char *p
= cmd
+ 5;
251 struct inquire_s
*inq
= (struct inquire_s
*)xmalloc(sizeof(struct inquire_s
));
258 inq
->len
= strlen(p
);
259 rc
= assuan_transact(ctx
, "STORE", mem_realloc_cb
, &data
, inquire_cb
,
260 inq
, pwm
->status_func
, pwm
->status_data
);
266 * Ignore any status callback function for pinentry.
268 if (ctx
== pwm
->pctx
)
269 rc
= assuan_transact(ctx
, cmd
, mem_realloc_cb
, &data
, NULL
, NULL
, NULL
, NULL
);
272 rc
= assuan_transact(ctx
, cmd
, mem_realloc_cb
, &data
, NULL
, NULL
,
273 pwm
->status_func
, pwm
->status_data
);
284 mem_realloc_cb(&data
, "", 1);
285 *result
= (char *)data
.buf
;
289 return gpg_err_code(rc
);
292 gpg_error_t
pwmd_terminate_pinentry(pwm_t
*pwm
)
295 return GPG_ERR_NOT_IMPLEMENTED
;
298 if (!pwm
|| pwm
->pid
== -1)
299 return GPG_ERR_INV_ARG
;
301 if (kill(pwm
->pid
, 0) == 0) {
302 if (kill(pwm
->pid
, SIGTERM
) == -1) {
303 if (kill(pwm
->pid
, SIGKILL
) == -1)
304 return gpg_error_from_errno(errno
);
307 global_error
= GPG_ERR_TIMEOUT
;
310 return gpg_error_from_errno(errno
);
316 static gpg_error_t
set_pinentry_strings(pwm_t
*pwm
, int which
)
319 char tmp
[ASSUAN_LINELENGTH
];
323 pwm
->title
= xstrdup(N_("LibPWMD"));
326 pwm
->prompt
= xstrdup(N_("Password:"));
328 if (!pwm
->desc
&& !which
)
329 pwm
->desc
= xstrdup(N_("Enter a password."));
332 snprintf(tmp
, sizeof(tmp
), "SETERROR %s", N_("Invalid password, please try again."));
335 else if (which
== 2) {
336 snprintf(tmp
, sizeof(tmp
), "SETERROR %s", N_("Please type the password again for confirmation."));
340 buf
= (char *)xmalloc(strlen("SETERROR ") + strlen(pwm
->desc
) + 1);
341 sprintf(buf
, "SETERROR %s", pwm
->desc
);
344 error
= pinentry_command(pwm
, NULL
, buf
);
350 buf
= (char *)xmalloc(strlen("SETPROMPT ") + strlen(pwm
->prompt
) + 1);
351 sprintf(buf
, "SETPROMPT %s", pwm
->prompt
);
352 error
= pinentry_command(pwm
, NULL
, buf
);
358 buf
= (char *)xmalloc(strlen("SETDESC ") + strlen(pwm
->title
) + 1);
359 sprintf(buf
, "SETDESC %s", pwm
->title
);
360 error
= pinentry_command(pwm
, NULL
, buf
);
365 static void update_pinentry_settings(pwm_t
*pwm
)
369 struct passwd
*pw
= getpwuid(getuid());
372 snprintf(buf
, sizeof(buf
), "%s/.pwmd/env", pw
->pw_dir
);
374 if ((fp
= fopen(buf
, "r")) == NULL
)
377 while ((p
= fgets(buf
, sizeof(buf
), fp
)) != NULL
) {
378 char name
[32], val
[256];
380 if (sscanf(p
, "%[a-zA-Z]=%s", name
, val
) != 2)
383 if (strcasecmp(name
, "TTY") == 0) {
384 if (!pwm
->pinentry_tty
) {
385 xfree(pwm
->pinentry_tty
);
386 pwm
->pinentry_tty
= xstrdup(val
);
389 else if (strcasecmp(name
, "TERM") == 0) {
390 if (!pwm
->pinentry_term
) {
391 xfree(pwm
->pinentry_term
);
392 pwm
->pinentry_term
= xstrdup(val
);
395 else if (strcasecmp(name
, "DISPLAY") == 0) {
396 if (!pwm
->pinentry_display
) {
397 xfree(pwm
->pinentry_display
);
398 pwm
->pinentry_display
= xstrdup(val
);
406 static gpg_error_t
launch_pinentry(pwm_t
*pwm
)
409 assuan_context_t ctx
;
410 int child_list
[] = {-1};
411 char *display
= getenv("DISPLAY");
413 int have_display
= 0;
416 update_pinentry_settings(pwm
);
418 if (pwm
->pinentry_display
|| display
)
421 tty
= pwm
->pinentry_tty
? pwm
->pinentry_tty
: ttyname(STDOUT_FILENO
);
424 return gpg_error_from_errno(errno
);
427 if (!display
&& !tty
)
428 return GPG_ERR_ENOTTY
;
430 argv
[0] = "pinentry";
431 argv
[1] = have_display
? "--display" : "--ttyname";
432 argv
[2] = have_display
? pwm
->pinentry_display
? pwm
->pinentry_display
: display
: tty
;
436 argv
[3] = "--ttytype";
437 argv
[4] = pwm
->pinentry_term
? pwm
->pinentry_term
: getenv("TERM");
441 rc
= assuan_pipe_connect(&ctx
, pwm
->pinentry_path
? pwm
->pinentry_path
: PINENTRY_PATH
, argv
, child_list
);
446 pwm
->pid
= assuan_get_pid(ctx
);
448 return set_pinentry_strings(pwm
, 0);
451 static gpg_error_t
pinentry_command(pwm_t
*pwm
, char **result
, const char *cmd
)
456 n
= launch_pinentry(pwm
);
462 return assuan_command(pwm
, pwm
->pctx
, result
, cmd
);
465 static void pinentry_disconnect(pwm_t
*pwm
)
468 assuan_disconnect(pwm
->pctx
);
475 * Only called from a child process.
477 static void catchsig(int sig
)
481 if (gelapsed
++ >= gtimeout
) {
482 global_error
= pwmd_terminate_pinentry(gpwm
);
485 global_error
= GPG_ERR_TIMEOUT
;
497 static char *percent_escape(const char *atext
)
499 const unsigned char *s
;
500 int len
= strlen(atext
) * 3 + 1;
501 char *buf
= (char *)xmalloc(len
), *p
= buf
;
506 for (s
=(const unsigned char *)atext
; *s
; s
++) {
508 sprintf (p
, "%%%02X", *s
);
520 static gpg_error_t
send_command(pwm_t
*pwm
, char **result
, const char *cmd
)
523 return GPG_ERR_INV_ARG
;
525 return assuan_command(pwm
, pwm
->ctx
, result
, cmd
);
529 * Avoid sending the BYE command here. libassuan will close the file
530 * descriptor and release the assuan context. Use pwmd_close() instead.
532 gpg_error_t
pwmd_command(pwm_t
*pwm
, char **result
, const char *cmd
, ...)
540 return GPG_ERR_INV_ARG
;
547 len
= vsnprintf(NULL
, 0, cmd
, ap
);
548 buf
= (char *)xmalloc(len
+ 1);
549 len
= vsnprintf(buf
, len
+ 1, cmd
, ap
);
551 error
= send_command(pwm
, result
, buf
);
557 static gpg_error_t
do_getpin(pwm_t
*pwm
, char **result
)
562 signal(SIGALRM
, catchsig
);
567 error
= pinentry_command(pwm
, result
, "GETPIN");
569 if (error
== GPG_ERR_ASS_CANCELED
)
578 static gpg_error_t
getpin(pwm_t
*pwm
, char **result
, int *try_n
, int which
)
580 int pin_try
= *try_n
;
587 error
= set_pinentry_strings(pwm
, which
);
590 pinentry_disconnect(pwm
);
595 if (pwm
->pinentry_tries
-1 != pin_try
) {
596 error
= set_pinentry_strings(pwm
, 1);
599 pinentry_disconnect(pwm
);
605 error
= do_getpin(pwm
, result
);
608 if (pin_try
!= -1 && pin_try
-- && error
== EPWMD_KEY
)
612 pinentry_disconnect(pwm
);
622 gpg_error_t
pwmd_open_nb_finalize(pwm_t
*pwm
, pwmd_nb_status_t
*pw
)
628 return GPG_ERR_NOT_IMPLEMENTED
;
631 if (!pwm
|| !pw
|| !pw
->filename
[0])
632 return GPG_ERR_INV_ARG
;
641 error
= pwmd_command(pwm
, &result
, "ISCACHED %s", pw
->filename
);
647 xfree(pwm
->filename
);
649 pwm
->filename
= xstrdup(pw
->filename
);
650 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
654 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
658 static gpg_error_t
do_open_command(pwm_t
*pwm
, const char *filename
, char *password
)
660 char buf
[ASSUAN_LINELENGTH
];
663 snprintf(buf
, sizeof(buf
), "OPEN %s %s", filename
, password
? password
: "");
664 error
= send_command(pwm
, NULL
, buf
);
665 memset(buf
, 0, sizeof(buf
));
669 static int do_pwmd_open(pwm_t
*pwm
, gpg_error_t
*error
, const char *filename
,
673 char *password
= NULL
;
679 if (!pwm
|| !filename
|| !*filename
) {
680 *error
= GPG_ERR_INV_ARG
;
685 pin_try
= pwm
->pinentry_tries
- 1;
689 * Avoid calling pinentry if the password is cached on the server or if
690 * this is a new file.
692 *error
= pwmd_command(pwm
, &result
, "GETCONFIG data_directory");
697 snprintf(path
, sizeof(path
), "%s/%s", result
, filename
);
698 pwmd_free_result(result
);
700 if (access(path
, R_OK
) == -1) {
704 *error
= gpg_error_from_errno(errno
);
708 *error
= pwmd_command(pwm
, &result
, "ISCACHED %s", filename
);
710 if (*error
== EPWMD_CACHE_NOT_FOUND
) {
712 password
= pwm
->passfunc(pwm
, pwm
->passdata
);
714 if (!password
|| !*password
) {
719 password
= xstrdup(password
);
723 if (*error
== EPWMD_CACHE_NOT_FOUND
) {
726 * Get the password from pinentry.
728 if (pwm
->use_pinentry
) {
730 * Nonblocking is wanted. fork() then return a file descriptor
731 * that the client can use to read() from.
739 *error
= gpg_error_from_syserror();
748 strncpy(pw
.filename
, filename
, sizeof(pw
.filename
));
758 *error
= getpin(pwm
, &password
, &pin_try
, 0);
763 pinentry_disconnect(pwm
);
765 if (gtimeout
&& gelapsed
>= gtimeout
)
766 *error
= GPG_ERR_TIMEOUT
;
769 write(p
[1], &pw
, sizeof(pw
));
775 * Don't count the time it takes to open the file
776 * which may have many iterations.
778 signal(SIGALRM
, SIG_DFL
);
779 *error
= do_open_command(pwm
, filename
, password
);
782 signal(SIGALRM
, catchsig
);
784 if (pwm
->pctx
&& *error
== EPWMD_BADKEY
) {
786 goto getpin_nb_again
;
791 pinentry_disconnect(pwm
);
793 write(p
[1], &pw
, sizeof(pw
));
798 *error
= gpg_error_from_syserror();
811 *error
= getpin(pwm
, &password
, &pin_try
, 1);
815 pinentry_disconnect(pwm
);
818 *error
= global_error
;
828 * Not using pinentry and the file was not found
831 if (pwm
->password
== NULL
) {
836 password
= pwm
->password
;
846 *error
= do_open_command(pwm
, filename
, password
);
849 * Keep the user defined password set with pwmd_setopt(). The password may
850 * be needed later (pwmd_save()) depending on the pwmd file cache settings.
852 if (password
&& password
!= pwm
->password
)
856 if (pwm
->pctx
&& *error
== EPWMD_BADKEY
) {
860 pinentry_disconnect(pwm
);
867 xfree(pwm
->filename
);
869 pwm
->filename
= xstrdup(filename
);
873 * The file is cached or the file is a new file.
876 return *error
? -1 : -2;
878 return *error
? 1 : 0;
881 gpg_error_t
pwmd_open(pwm_t
*pwm
, const char *filename
)
885 do_pwmd_open(pwm
, &error
, filename
, 0, 0);
889 int pwmd_open_nb(pwm_t
*pwm
, gpg_error_t
*error
, const char *filename
,
893 *error
= GPG_ERR_NOT_IMPLEMENTED
;
896 return do_pwmd_open(pwm
, error
, filename
, 1, timeout
);
901 static gpg_error_t
do_save_getpin(pwm_t
*pwm
, char **password
)
909 error
= getpin(pwm
, &result
, &pin_try
, confirm
? 2 : 0);
913 pinentry_disconnect(pwm
);
926 if (strcmp(*password
, result
)) {
929 pinentry_disconnect(pwm
);
930 error
= EPWMD_BADKEY
;
935 pinentry_disconnect(pwm
);
940 static gpg_error_t
do_save_command(pwm_t
*pwm
, char *password
)
942 char buf
[ASSUAN_LINELENGTH
];
945 snprintf(buf
, sizeof(buf
), "SAVE %s", password
? password
: "");
946 error
= send_command(pwm
, NULL
, buf
);
947 memset(&buf
, 0, sizeof(buf
));
951 gpg_error_t
pwmd_save_nb_finalize(pwm_t
*pwm
, pwmd_nb_status_t
*pw
)
956 return GPG_ERR_NOT_IMPLEMENTED
;
959 if (!pwm
|| !pw
|| !pw
->filename
[0])
960 return GPG_ERR_INV_ARG
;
966 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
970 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
974 static int do_pwmd_save(pwm_t
*pwm
, gpg_error_t
*error
, int nb
)
977 char *password
= NULL
;
980 *error
= GPG_ERR_INV_ARG
;
984 if (pwm
->use_pinentry
|| pwm
->passfunc
) {
985 *error
= pwmd_command(pwm
, &result
, "ISCACHED %s", pwm
->filename
);
987 if (*error
== EPWMD_CACHE_NOT_FOUND
) {
989 if (pwm
->use_pinentry
) {
996 *error
= gpg_error_from_syserror();
1005 strncpy(pw
.filename
, pwm
->filename
, sizeof(pw
.filename
));
1010 *error
= do_save_getpin(pwm
, &password
);
1011 } while (*error
== EPWMD_KEY
|| *error
== EPWMD_BADKEY
);
1015 pinentry_disconnect(pwm
);
1018 write(p
[1], &pw
, sizeof(pw
));
1023 *error
= do_save_command(pwm
, password
);
1024 pinentry_disconnect(pwm
);
1026 write(p
[1], &pw
, sizeof(pw
));
1031 *error
= gpg_error_from_syserror();
1043 *error
= do_save_getpin(pwm
, &password
);
1050 if (pwm
->passfunc
) {
1051 char *tmp
= (*pwm
->passfunc
)(pwm
, pwm
->passdata
);
1053 if (!tmp
|| !*tmp
) {
1058 password
= xstrdup(tmp
);
1064 if (!password
|| !*password
) {
1075 password
= pwm
->password
;
1077 *error
= do_save_command(pwm
, password
);
1079 if (password
&& password
!= pwm
->password
)
1083 return *error
? -1 : -2;
1085 return *error
? 1 : 0;
1088 int pwmd_save_nb(pwm_t
*pwm
, gpg_error_t
*error
)
1090 #ifndef USE_PINENTRY
1091 *error
= GPG_ERR_NOT_IMPLEMENTED
;
1094 return do_pwmd_save(pwm
, error
, 1);
1098 gpg_error_t
pwmd_save(pwm_t
*pwm
)
1102 do_pwmd_save(pwm
, &error
, 0);
1106 gpg_error_t
pwmd_setopt(pwm_t
*pwm
, pwmd_option_t opt
, ...)
1110 int n
= va_arg(ap
, int);
1115 return GPG_ERR_INV_ARG
;
1120 case PWMD_OPTION_STATUS_FUNC
:
1121 pwm
->status_func
= va_arg(ap
, pwmd_status_fn
);
1123 case PWMD_OPTION_STATUS_DATA
:
1124 pwm
->status_data
= va_arg(ap
, void *);
1126 case PWMD_OPTION_PASSWORD_FUNC
:
1127 pwm
->passfunc
= va_arg(ap
, pwmd_password_fn
);
1129 case PWMD_OPTION_PASSWORD_DATA
:
1130 pwm
->passdata
= va_arg(ap
, void *);
1132 case PWMD_OPTION_PASSWORD
:
1133 arg1
= va_arg(ap
, char *);
1136 xfree(pwm
->password
);
1138 pwm
->password
= xstrdup(arg1
);
1141 case PWMD_OPTION_PINENTRY
:
1142 n
= va_arg(ap
, int);
1144 if (n
!= 0 && n
!= 1) {
1146 return GPG_ERR_INV_VALUE
;
1149 pwm
->use_pinentry
= n
;
1151 case PWMD_OPTION_PINENTRY_TRIES
:
1152 n
= va_arg(ap
, int);
1156 return GPG_ERR_INV_VALUE
;
1159 pwm
->pinentry_tries
= n
;
1161 case PWMD_OPTION_PINENTRY_PATH
:
1162 if (pwm
->pinentry_path
)
1163 xfree(pwm
->pinentry_path
);
1165 pwm
->pinentry_path
= xstrdup(va_arg(ap
, char *));
1167 case PWMD_OPTION_PINENTRY_TTY
:
1168 if (pwm
->pinentry_tty
)
1169 xfree(pwm
->pinentry_tty
);
1171 pwm
->pinentry_tty
= xstrdup(va_arg(ap
, char *));
1173 case PWMD_OPTION_PINENTRY_DISPLAY
:
1174 if (pwm
->pinentry_display
)
1175 xfree(pwm
->pinentry_display
);
1177 pwm
->pinentry_display
= xstrdup(va_arg(ap
, char *));
1179 case PWMD_OPTION_PINENTRY_TERM
:
1180 if (pwm
->pinentry_term
)
1181 xfree(pwm
->pinentry_term
);
1183 pwm
->pinentry_term
= xstrdup(va_arg(ap
, char *));
1185 case PWMD_OPTION_PINENTRY_TITLE
:
1188 pwm
->title
= percent_escape(va_arg(ap
, char *));
1190 case PWMD_OPTION_PINENTRY_PROMPT
:
1193 pwm
->prompt
= percent_escape(va_arg(ap
, char *));
1195 case PWMD_OPTION_PINENTRY_DESC
:
1198 pwm
->desc
= percent_escape(va_arg(ap
, char *));
1201 case PWMD_OPTION_PINENTRY
:
1202 case PWMD_OPTION_PINENTRY_TRIES
:
1203 case PWMD_OPTION_PINENTRY_PATH
:
1204 case PWMD_OPTION_PINENTRY_TTY
:
1205 case PWMD_OPTION_PINENTRY_DISPLAY
:
1206 case PWMD_OPTION_PINENTRY_TERM
:
1207 case PWMD_OPTION_PINENTRY_TITLE
:
1208 case PWMD_OPTION_PINENTRY_PROMPT
:
1209 case PWMD_OPTION_PINENTRY_DESC
:
1210 return GPG_ERR_NOT_IMPLEMENTED
;
1214 return GPG_ERR_NOT_IMPLEMENTED
;