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 * Since there was input cancel any timeout.
613 if (pin_try
!= -1 && pin_try
-- && error
== EPWMD_KEY
)
617 pinentry_disconnect(pwm
);
627 gpg_error_t
pwmd_open_nb_finalize(pwm_t
*pwm
, pwmd_nb_status_t
*pw
)
633 return GPG_ERR_NOT_IMPLEMENTED
;
636 if (!pwm
|| !pw
|| !pw
->filename
[0])
637 return GPG_ERR_INV_ARG
;
646 error
= pwmd_command(pwm
, &result
, "ISCACHED %s", pw
->filename
);
652 xfree(pwm
->filename
);
654 pwm
->filename
= xstrdup(pw
->filename
);
655 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
659 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
663 static gpg_error_t
do_open_command(pwm_t
*pwm
, const char *filename
, char *password
)
665 char buf
[ASSUAN_LINELENGTH
];
668 snprintf(buf
, sizeof(buf
), "OPEN %s %s", filename
, password
? password
: "");
669 error
= send_command(pwm
, NULL
, buf
);
670 memset(buf
, 0, sizeof(buf
));
674 static int do_pwmd_open(pwm_t
*pwm
, gpg_error_t
*error
, const char *filename
,
678 char *password
= NULL
;
684 if (!pwm
|| !filename
|| !*filename
) {
685 *error
= GPG_ERR_INV_ARG
;
690 pin_try
= pwm
->pinentry_tries
- 1;
694 * Avoid calling pinentry if the password is cached on the server or if
695 * this is a new file.
697 *error
= pwmd_command(pwm
, &result
, "GETCONFIG data_directory");
702 snprintf(path
, sizeof(path
), "%s/%s", result
, filename
);
703 pwmd_free_result(result
);
705 if (access(path
, R_OK
) == -1) {
709 *error
= gpg_error_from_errno(errno
);
713 *error
= pwmd_command(pwm
, &result
, "ISCACHED %s", filename
);
715 if (*error
== EPWMD_CACHE_NOT_FOUND
) {
717 password
= pwm
->passfunc(pwm
, pwm
->passdata
);
719 if (!password
|| !*password
) {
724 password
= xstrdup(password
);
728 if (*error
== EPWMD_CACHE_NOT_FOUND
) {
731 * Get the password from pinentry.
733 if (pwm
->use_pinentry
) {
735 * Nonblocking is wanted. fork() then return a file descriptor
736 * that the client can use to read() from.
744 *error
= gpg_error_from_syserror();
753 strncpy(pw
.filename
, filename
, sizeof(pw
.filename
));
763 *error
= getpin(pwm
, &password
, &pin_try
, 0);
768 pinentry_disconnect(pwm
);
770 if (gtimeout
&& gelapsed
>= gtimeout
)
771 *error
= GPG_ERR_TIMEOUT
;
774 write(p
[1], &pw
, sizeof(pw
));
780 * Don't count the time it takes to open the file
781 * which may have many iterations.
783 signal(SIGALRM
, SIG_DFL
);
784 *error
= do_open_command(pwm
, filename
, password
);
787 signal(SIGALRM
, catchsig
);
789 if (pwm
->pctx
&& *error
== EPWMD_BADKEY
) {
791 goto getpin_nb_again
;
796 pinentry_disconnect(pwm
);
798 write(p
[1], &pw
, sizeof(pw
));
803 *error
= gpg_error_from_syserror();
816 *error
= getpin(pwm
, &password
, &pin_try
, 1);
820 pinentry_disconnect(pwm
);
823 *error
= global_error
;
833 * Not using pinentry and the file was not found
836 if (pwm
->password
== NULL
) {
841 password
= pwm
->password
;
851 *error
= do_open_command(pwm
, filename
, password
);
854 * Keep the user defined password set with pwmd_setopt(). The password may
855 * be needed later (pwmd_save()) depending on the pwmd file cache settings.
857 if (password
&& password
!= pwm
->password
)
861 if (pwm
->pctx
&& *error
== EPWMD_BADKEY
) {
865 pinentry_disconnect(pwm
);
872 xfree(pwm
->filename
);
874 pwm
->filename
= xstrdup(filename
);
878 * The file is cached or the file is a new file.
881 return *error
? -1 : -2;
883 return *error
? 1 : 0;
886 gpg_error_t
pwmd_open(pwm_t
*pwm
, const char *filename
)
890 do_pwmd_open(pwm
, &error
, filename
, 0, 0);
894 int pwmd_open_nb(pwm_t
*pwm
, gpg_error_t
*error
, const char *filename
,
898 *error
= GPG_ERR_NOT_IMPLEMENTED
;
901 return do_pwmd_open(pwm
, error
, filename
, 1, timeout
);
906 static gpg_error_t
do_save_getpin(pwm_t
*pwm
, char **password
)
914 error
= getpin(pwm
, &result
, &pin_try
, confirm
? 2 : 0);
918 pinentry_disconnect(pwm
);
931 if (strcmp(*password
, result
)) {
934 pinentry_disconnect(pwm
);
935 error
= EPWMD_BADKEY
;
940 pinentry_disconnect(pwm
);
945 static gpg_error_t
do_save_command(pwm_t
*pwm
, char *password
)
947 char buf
[ASSUAN_LINELENGTH
];
950 snprintf(buf
, sizeof(buf
), "SAVE %s", password
? password
: "");
951 error
= send_command(pwm
, NULL
, buf
);
952 memset(&buf
, 0, sizeof(buf
));
956 gpg_error_t
pwmd_save_nb_finalize(pwm_t
*pwm
, pwmd_nb_status_t
*pw
)
961 return GPG_ERR_NOT_IMPLEMENTED
;
964 if (!pwm
|| !pw
|| !pw
->filename
[0])
965 return GPG_ERR_INV_ARG
;
971 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
975 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
979 static int do_pwmd_save(pwm_t
*pwm
, gpg_error_t
*error
, int nb
)
982 char *password
= NULL
;
985 *error
= GPG_ERR_INV_ARG
;
989 if (pwm
->use_pinentry
|| pwm
->passfunc
) {
990 *error
= pwmd_command(pwm
, &result
, "ISCACHED %s", pwm
->filename
);
992 if (*error
== EPWMD_CACHE_NOT_FOUND
) {
994 if (pwm
->use_pinentry
) {
1000 if (pipe(p
) == -1) {
1001 *error
= gpg_error_from_syserror();
1010 strncpy(pw
.filename
, pwm
->filename
, sizeof(pw
.filename
));
1015 *error
= do_save_getpin(pwm
, &password
);
1016 } while (*error
== EPWMD_KEY
|| *error
== EPWMD_BADKEY
);
1020 pinentry_disconnect(pwm
);
1023 write(p
[1], &pw
, sizeof(pw
));
1028 *error
= do_save_command(pwm
, password
);
1029 pinentry_disconnect(pwm
);
1031 write(p
[1], &pw
, sizeof(pw
));
1036 *error
= gpg_error_from_syserror();
1048 *error
= do_save_getpin(pwm
, &password
);
1055 if (pwm
->passfunc
) {
1056 char *tmp
= (*pwm
->passfunc
)(pwm
, pwm
->passdata
);
1058 if (!tmp
|| !*tmp
) {
1063 password
= xstrdup(tmp
);
1069 if (!password
|| !*password
) {
1080 password
= pwm
->password
;
1082 *error
= do_save_command(pwm
, password
);
1084 if (password
&& password
!= pwm
->password
)
1088 return *error
? -1 : -2;
1090 return *error
? 1 : 0;
1093 int pwmd_save_nb(pwm_t
*pwm
, gpg_error_t
*error
)
1095 #ifndef USE_PINENTRY
1096 *error
= GPG_ERR_NOT_IMPLEMENTED
;
1099 return do_pwmd_save(pwm
, error
, 1);
1103 gpg_error_t
pwmd_save(pwm_t
*pwm
)
1107 do_pwmd_save(pwm
, &error
, 0);
1111 gpg_error_t
pwmd_setopt(pwm_t
*pwm
, pwmd_option_t opt
, ...)
1115 int n
= va_arg(ap
, int);
1120 return GPG_ERR_INV_ARG
;
1125 case PWMD_OPTION_STATUS_FUNC
:
1126 pwm
->status_func
= va_arg(ap
, pwmd_status_fn
);
1128 case PWMD_OPTION_STATUS_DATA
:
1129 pwm
->status_data
= va_arg(ap
, void *);
1131 case PWMD_OPTION_PASSWORD_FUNC
:
1132 pwm
->passfunc
= va_arg(ap
, pwmd_password_fn
);
1134 case PWMD_OPTION_PASSWORD_DATA
:
1135 pwm
->passdata
= va_arg(ap
, void *);
1137 case PWMD_OPTION_PASSWORD
:
1138 arg1
= va_arg(ap
, char *);
1141 xfree(pwm
->password
);
1143 pwm
->password
= xstrdup(arg1
);
1146 case PWMD_OPTION_PINENTRY
:
1147 n
= va_arg(ap
, int);
1149 if (n
!= 0 && n
!= 1) {
1151 return GPG_ERR_INV_VALUE
;
1154 pwm
->use_pinentry
= n
;
1156 case PWMD_OPTION_PINENTRY_TRIES
:
1157 n
= va_arg(ap
, int);
1161 return GPG_ERR_INV_VALUE
;
1164 pwm
->pinentry_tries
= n
;
1166 case PWMD_OPTION_PINENTRY_PATH
:
1167 if (pwm
->pinentry_path
)
1168 xfree(pwm
->pinentry_path
);
1170 pwm
->pinentry_path
= xstrdup(va_arg(ap
, char *));
1172 case PWMD_OPTION_PINENTRY_TTY
:
1173 if (pwm
->pinentry_tty
)
1174 xfree(pwm
->pinentry_tty
);
1176 pwm
->pinentry_tty
= xstrdup(va_arg(ap
, char *));
1178 case PWMD_OPTION_PINENTRY_DISPLAY
:
1179 if (pwm
->pinentry_display
)
1180 xfree(pwm
->pinentry_display
);
1182 pwm
->pinentry_display
= xstrdup(va_arg(ap
, char *));
1184 case PWMD_OPTION_PINENTRY_TERM
:
1185 if (pwm
->pinentry_term
)
1186 xfree(pwm
->pinentry_term
);
1188 pwm
->pinentry_term
= xstrdup(va_arg(ap
, char *));
1190 case PWMD_OPTION_PINENTRY_TITLE
:
1193 pwm
->title
= percent_escape(va_arg(ap
, char *));
1195 case PWMD_OPTION_PINENTRY_PROMPT
:
1198 pwm
->prompt
= percent_escape(va_arg(ap
, char *));
1200 case PWMD_OPTION_PINENTRY_DESC
:
1203 pwm
->desc
= percent_escape(va_arg(ap
, char *));
1206 case PWMD_OPTION_PINENTRY
:
1207 case PWMD_OPTION_PINENTRY_TRIES
:
1208 case PWMD_OPTION_PINENTRY_PATH
:
1209 case PWMD_OPTION_PINENTRY_TTY
:
1210 case PWMD_OPTION_PINENTRY_DISPLAY
:
1211 case PWMD_OPTION_PINENTRY_TERM
:
1212 case PWMD_OPTION_PINENTRY_TITLE
:
1213 case PWMD_OPTION_PINENTRY_PROMPT
:
1214 case PWMD_OPTION_PINENTRY_DESC
:
1215 return GPG_ERR_NOT_IMPLEMENTED
;
1219 return GPG_ERR_NOT_IMPLEMENTED
;