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;
168 void pwmd_close(pwm_t
*pwm
)
174 assuan_disconnect(pwm
->ctx
);
177 xfree(pwm
->password
);
188 if (pwm
->pinentry_tty
)
189 xfree(pwm
->pinentry_tty
);
191 if (pwm
->pinentry_display
)
192 xfree(pwm
->pinentry_display
);
194 if (pwm
->pinentry_term
)
195 xfree(pwm
->pinentry_term
);
198 xfree(pwm
->filename
);
203 static int mem_realloc_cb(void *data
, const void *buffer
, size_t len
)
205 membuf_t
*mem
= (membuf_t
*)data
;
211 if ((p
= xrealloc(mem
->buf
, mem
->len
+ len
)) == NULL
)
215 memcpy((char *)mem
->buf
+ mem
->len
, buffer
, len
);
220 static int inquire_cb(void *data
, const char *keyword
)
222 struct inquire_s
*inquire
= (struct inquire_s
*)data
;
224 return assuan_send_data(inquire
->ctx
, inquire
->buf
, inquire
->len
);
227 void pwmd_free_result(void *data
)
232 static gpg_error_t
assuan_command(pwm_t
*pwm
, assuan_context_t ctx
,
233 char **result
, const char *cmd
)
242 * This is needed because assuan only accepts 1000 byte command strings.
243 * If the line is more than this the command will fail. So we use an
244 * INQUIRE on the server which waits for an END that assuan_transact()
247 * Other commands shouldn't need the INQUIRE. Let me know if you have an
248 * element path that's greater than 1000 bytes and I'll fix it.
250 if (strncasecmp(cmd
, "STORE", 5) == 0) {
251 const char *p
= cmd
+ 5;
252 struct inquire_s
*inq
= (struct inquire_s
*)xmalloc(sizeof(struct inquire_s
));
259 inq
->len
= strlen(p
);
260 rc
= assuan_transact(ctx
, "STORE", mem_realloc_cb
, &data
, inquire_cb
,
261 inq
, pwm
->status_func
, pwm
->status_data
);
267 * Ignore any status callback function for pinentry.
269 if (ctx
== pwm
->pctx
)
270 rc
= assuan_transact(ctx
, cmd
, mem_realloc_cb
, &data
, NULL
, NULL
, NULL
, NULL
);
273 rc
= assuan_transact(ctx
, cmd
, mem_realloc_cb
, &data
, NULL
, NULL
,
274 pwm
->status_func
, pwm
->status_data
);
285 mem_realloc_cb(&data
, "", 1);
286 *result
= (char *)data
.buf
;
290 return gpg_err_code(rc
);
293 gpg_error_t
pwmd_terminate_pinentry(pwm_t
*pwm
)
296 return GPG_ERR_NOT_IMPLEMENTED
;
299 if (!pwm
|| pwm
->pid
== -1)
300 return GPG_ERR_INV_ARG
;
302 if (kill(pwm
->pid
, 0) == 0) {
303 if (kill(pwm
->pid
, SIGTERM
) == -1) {
304 if (kill(pwm
->pid
, SIGKILL
) == -1)
305 return gpg_error_from_errno(errno
);
308 global_error
= GPG_ERR_TIMEOUT
;
311 return gpg_error_from_errno(errno
);
317 static gpg_error_t
set_pinentry_strings(pwm_t
*pwm
, int which
)
320 char tmp
[ASSUAN_LINELENGTH
];
324 pwm
->title
= xstrdup(N_("LibPWMD"));
327 pwm
->prompt
= xstrdup(N_("Password:"));
329 if (!pwm
->desc
&& !which
)
330 pwm
->desc
= xstrdup(N_("Enter a password."));
333 snprintf(tmp
, sizeof(tmp
), "SETERROR %s", N_("Invalid password, please try again."));
336 else if (which
== 2) {
337 snprintf(tmp
, sizeof(tmp
), "SETERROR %s", N_("Please type the password again for confirmation."));
341 buf
= (char *)xmalloc(strlen("SETERROR ") + strlen(pwm
->desc
) + 1);
342 sprintf(buf
, "SETERROR %s", pwm
->desc
);
345 error
= pinentry_command(pwm
, NULL
, buf
);
351 buf
= (char *)xmalloc(strlen("SETPROMPT ") + strlen(pwm
->prompt
) + 1);
352 sprintf(buf
, "SETPROMPT %s", pwm
->prompt
);
353 error
= pinentry_command(pwm
, NULL
, buf
);
359 buf
= (char *)xmalloc(strlen("SETDESC ") + strlen(pwm
->title
) + 1);
360 sprintf(buf
, "SETDESC %s", pwm
->title
);
361 error
= pinentry_command(pwm
, NULL
, buf
);
366 static void update_pinentry_settings(pwm_t
*pwm
)
370 struct passwd
*pw
= getpwuid(getuid());
373 snprintf(buf
, sizeof(buf
), "%s/.pwmd/env", pw
->pw_dir
);
375 if ((fp
= fopen(buf
, "r")) == NULL
)
378 while ((p
= fgets(buf
, sizeof(buf
), fp
)) != NULL
) {
379 char name
[32], val
[256];
381 if (sscanf(p
, "%[a-zA-Z]=%s", name
, val
) != 2)
384 if (strcasecmp(name
, "TTY") == 0) {
385 if (!pwm
->pinentry_tty
) {
386 xfree(pwm
->pinentry_tty
);
387 pwm
->pinentry_tty
= xstrdup(val
);
390 else if (strcasecmp(name
, "TERM") == 0) {
391 if (!pwm
->pinentry_term
) {
392 xfree(pwm
->pinentry_term
);
393 pwm
->pinentry_term
= xstrdup(val
);
396 else if (strcasecmp(name
, "DISPLAY") == 0) {
397 if (!pwm
->pinentry_display
) {
398 xfree(pwm
->pinentry_display
);
399 pwm
->pinentry_display
= xstrdup(val
);
407 static gpg_error_t
launch_pinentry(pwm_t
*pwm
)
410 assuan_context_t ctx
;
411 int child_list
[] = {-1};
412 char *display
= getenv("DISPLAY");
414 int have_display
= 0;
417 update_pinentry_settings(pwm
);
419 if (pwm
->pinentry_display
|| display
)
422 tty
= pwm
->pinentry_tty
? pwm
->pinentry_tty
: ttyname(STDOUT_FILENO
);
425 return gpg_error_from_errno(errno
);
428 if (!display
&& !tty
)
429 return GPG_ERR_ENOTTY
;
431 argv
[0] = "pinentry";
432 argv
[1] = have_display
? "--display" : "--ttyname";
433 argv
[2] = have_display
? pwm
->pinentry_display
? pwm
->pinentry_display
: display
: tty
;
437 argv
[3] = "--ttytype";
438 argv
[4] = pwm
->pinentry_term
? pwm
->pinentry_term
: getenv("TERM");
442 rc
= assuan_pipe_connect(&ctx
, pwm
->pinentry_path
? pwm
->pinentry_path
: PINENTRY_PATH
, argv
, child_list
);
447 pwm
->pid
= assuan_get_pid(ctx
);
449 return set_pinentry_strings(pwm
, 0);
452 static gpg_error_t
pinentry_command(pwm_t
*pwm
, char **result
, const char *cmd
)
457 n
= launch_pinentry(pwm
);
463 return assuan_command(pwm
, pwm
->pctx
, result
, cmd
);
466 static void pinentry_disconnect(pwm_t
*pwm
)
469 assuan_disconnect(pwm
->pctx
);
476 * Only called from a child process.
478 static void catchsig(int sig
)
482 if (gelapsed
++ >= gtimeout
) {
483 global_error
= pwmd_terminate_pinentry(gpwm
);
486 global_error
= GPG_ERR_TIMEOUT
;
498 static char *percent_escape(const char *atext
)
500 const unsigned char *s
;
501 int len
= strlen(atext
) * 3 + 1;
502 char *buf
= (char *)xmalloc(len
), *p
= buf
;
507 for (s
=(const unsigned char *)atext
; *s
; s
++) {
509 sprintf (p
, "%%%02X", *s
);
521 static gpg_error_t
send_command(pwm_t
*pwm
, char **result
, const char *cmd
)
524 return GPG_ERR_INV_ARG
;
526 return assuan_command(pwm
, pwm
->ctx
, result
, cmd
);
530 * Avoid sending the BYE command here. libassuan will close the file
531 * descriptor and release the assuan context. Use pwmd_close() instead.
533 gpg_error_t
pwmd_command(pwm_t
*pwm
, char **result
, const char *cmd
, ...)
541 return GPG_ERR_INV_ARG
;
548 len
= vsnprintf(NULL
, 0, cmd
, ap
);
549 buf
= (char *)xmalloc(len
+ 1);
550 len
= vsnprintf(buf
, len
+ 1, cmd
, ap
);
552 error
= send_command(pwm
, result
, buf
);
558 static gpg_error_t
do_getpin(pwm_t
*pwm
, char **result
)
563 signal(SIGALRM
, catchsig
);
568 error
= pinentry_command(pwm
, result
, "GETPIN");
570 if (error
== GPG_ERR_ASS_CANCELED
)
579 static gpg_error_t
getpin(pwm_t
*pwm
, char **result
, int *try_n
, int which
)
581 int pin_try
= *try_n
;
588 error
= set_pinentry_strings(pwm
, which
);
591 pinentry_disconnect(pwm
);
596 if (pwm
->pinentry_tries
-1 != pin_try
) {
597 error
= set_pinentry_strings(pwm
, 1);
600 pinentry_disconnect(pwm
);
606 error
= do_getpin(pwm
, result
);
609 * Since there was input cancel any timeout.
614 if (pin_try
!= -1 && pin_try
-- && error
== EPWMD_KEY
)
618 pinentry_disconnect(pwm
);
628 gpg_error_t
pwmd_open_nb_finalize(pwm_t
*pwm
, pwmd_nb_status_t
*pw
)
634 return GPG_ERR_NOT_IMPLEMENTED
;
637 if (!pwm
|| !pw
|| !pw
->filename
[0])
638 return GPG_ERR_INV_ARG
;
647 error
= pwmd_command(pwm
, &result
, "ISCACHED %s", pw
->filename
);
653 xfree(pwm
->filename
);
655 pwm
->filename
= xstrdup(pw
->filename
);
656 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
660 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
664 static gpg_error_t
do_open_command(pwm_t
*pwm
, const char *filename
, char *password
)
666 char buf
[ASSUAN_LINELENGTH
];
669 snprintf(buf
, sizeof(buf
), "OPEN %s %s", filename
, password
? password
: "");
670 error
= send_command(pwm
, NULL
, buf
);
671 memset(buf
, 0, sizeof(buf
));
675 static int do_pwmd_open(pwm_t
*pwm
, gpg_error_t
*error
, const char *filename
,
679 char *password
= NULL
;
685 if (!pwm
|| !filename
|| !*filename
) {
686 *error
= GPG_ERR_INV_ARG
;
691 pin_try
= pwm
->pinentry_tries
- 1;
695 * Avoid calling pinentry if the password is cached on the server or if
696 * this is a new file.
698 *error
= pwmd_command(pwm
, &result
, "GETCONFIG data_directory");
703 snprintf(path
, sizeof(path
), "%s/%s", result
, filename
);
704 pwmd_free_result(result
);
706 if (access(path
, R_OK
) == -1) {
710 *error
= gpg_error_from_errno(errno
);
714 *error
= pwmd_command(pwm
, &result
, "ISCACHED %s", filename
);
716 if (*error
== EPWMD_CACHE_NOT_FOUND
) {
718 password
= pwm
->passfunc(pwm
, pwm
->passdata
);
720 if (!password
|| !*password
) {
725 password
= xstrdup(password
);
729 if (*error
== EPWMD_CACHE_NOT_FOUND
) {
732 * Get the password from pinentry.
734 if (pwm
->use_pinentry
) {
736 * Nonblocking is wanted. fork() then return a file descriptor
737 * that the client can use to read() from.
745 *error
= gpg_error_from_syserror();
754 strncpy(pw
.filename
, filename
, sizeof(pw
.filename
));
764 *error
= getpin(pwm
, &password
, &pin_try
, 0);
769 pinentry_disconnect(pwm
);
771 if (gtimeout
&& gelapsed
>= gtimeout
)
772 *error
= GPG_ERR_TIMEOUT
;
775 write(p
[1], &pw
, sizeof(pw
));
781 * Don't count the time it takes to open the file
782 * which may have many iterations.
784 signal(SIGALRM
, SIG_DFL
);
785 *error
= do_open_command(pwm
, filename
, password
);
788 signal(SIGALRM
, catchsig
);
790 if (pwm
->pctx
&& *error
== EPWMD_BADKEY
) {
792 goto getpin_nb_again
;
797 pinentry_disconnect(pwm
);
799 write(p
[1], &pw
, sizeof(pw
));
804 *error
= gpg_error_from_syserror();
817 *error
= getpin(pwm
, &password
, &pin_try
, 1);
821 pinentry_disconnect(pwm
);
824 *error
= global_error
;
834 * Not using pinentry and the file was not found
837 if (pwm
->password
== NULL
) {
842 password
= pwm
->password
;
852 *error
= do_open_command(pwm
, filename
, password
);
855 * Keep the user defined password set with pwmd_setopt(). The password may
856 * be needed later (pwmd_save()) depending on the pwmd file cache settings.
858 if (password
&& password
!= pwm
->password
)
862 if (pwm
->pctx
&& *error
== EPWMD_BADKEY
) {
866 pinentry_disconnect(pwm
);
873 xfree(pwm
->filename
);
875 pwm
->filename
= xstrdup(filename
);
879 * The file is cached or the file is a new file.
882 return *error
? -1 : -2;
884 return *error
? 1 : 0;
887 gpg_error_t
pwmd_open(pwm_t
*pwm
, const char *filename
)
891 do_pwmd_open(pwm
, &error
, filename
, 0, 0);
895 int pwmd_open_nb(pwm_t
*pwm
, gpg_error_t
*error
, const char *filename
,
899 *error
= GPG_ERR_NOT_IMPLEMENTED
;
902 return do_pwmd_open(pwm
, error
, filename
, 1, timeout
);
907 static gpg_error_t
do_save_getpin(pwm_t
*pwm
, char **password
)
915 error
= getpin(pwm
, &result
, &pin_try
, confirm
? 2 : 0);
919 pinentry_disconnect(pwm
);
932 if (strcmp(*password
, result
)) {
935 pinentry_disconnect(pwm
);
936 error
= EPWMD_BADKEY
;
941 pinentry_disconnect(pwm
);
946 static gpg_error_t
do_save_command(pwm_t
*pwm
, char *password
)
948 char buf
[ASSUAN_LINELENGTH
];
951 snprintf(buf
, sizeof(buf
), "SAVE %s", password
? password
: "");
952 error
= send_command(pwm
, NULL
, buf
);
953 memset(&buf
, 0, sizeof(buf
));
957 gpg_error_t
pwmd_save_nb_finalize(pwm_t
*pwm
, pwmd_nb_status_t
*pw
)
962 return GPG_ERR_NOT_IMPLEMENTED
;
965 if (!pwm
|| !pw
|| !pw
->filename
[0])
966 return GPG_ERR_INV_ARG
;
972 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
976 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
980 static int do_pwmd_save(pwm_t
*pwm
, gpg_error_t
*error
, int nb
)
983 char *password
= NULL
;
986 *error
= GPG_ERR_INV_ARG
;
990 if (pwm
->use_pinentry
|| pwm
->passfunc
) {
991 *error
= pwmd_command(pwm
, &result
, "ISCACHED %s", pwm
->filename
);
993 if (*error
== EPWMD_CACHE_NOT_FOUND
) {
995 if (pwm
->use_pinentry
) {
1001 if (pipe(p
) == -1) {
1002 *error
= gpg_error_from_syserror();
1011 strncpy(pw
.filename
, pwm
->filename
, sizeof(pw
.filename
));
1016 *error
= do_save_getpin(pwm
, &password
);
1017 } while (*error
== EPWMD_KEY
|| *error
== EPWMD_BADKEY
);
1021 pinentry_disconnect(pwm
);
1024 write(p
[1], &pw
, sizeof(pw
));
1029 *error
= do_save_command(pwm
, password
);
1030 pinentry_disconnect(pwm
);
1032 write(p
[1], &pw
, sizeof(pw
));
1037 *error
= gpg_error_from_syserror();
1049 *error
= do_save_getpin(pwm
, &password
);
1056 if (pwm
->passfunc
) {
1057 char *tmp
= (*pwm
->passfunc
)(pwm
, pwm
->passdata
);
1059 if (!tmp
|| !*tmp
) {
1064 password
= xstrdup(tmp
);
1070 if (!password
|| !*password
) {
1081 password
= pwm
->password
;
1083 *error
= do_save_command(pwm
, password
);
1085 if (password
&& password
!= pwm
->password
)
1089 return *error
? -1 : -2;
1091 return *error
? 1 : 0;
1094 int pwmd_save_nb(pwm_t
*pwm
, gpg_error_t
*error
)
1096 #ifndef USE_PINENTRY
1097 *error
= GPG_ERR_NOT_IMPLEMENTED
;
1100 return do_pwmd_save(pwm
, error
, 1);
1104 gpg_error_t
pwmd_save(pwm_t
*pwm
)
1108 do_pwmd_save(pwm
, &error
, 0);
1112 gpg_error_t
pwmd_setopt(pwm_t
*pwm
, pwmd_option_t opt
, ...)
1116 int n
= va_arg(ap
, int);
1121 return GPG_ERR_INV_ARG
;
1126 case PWMD_OPTION_STATUS_FUNC
:
1127 pwm
->status_func
= va_arg(ap
, pwmd_status_fn
);
1129 case PWMD_OPTION_STATUS_DATA
:
1130 pwm
->status_data
= va_arg(ap
, void *);
1132 case PWMD_OPTION_PASSWORD_FUNC
:
1133 pwm
->passfunc
= va_arg(ap
, pwmd_password_fn
);
1135 case PWMD_OPTION_PASSWORD_DATA
:
1136 pwm
->passdata
= va_arg(ap
, void *);
1138 case PWMD_OPTION_PASSWORD
:
1139 arg1
= va_arg(ap
, char *);
1142 xfree(pwm
->password
);
1144 pwm
->password
= xstrdup(arg1
);
1147 case PWMD_OPTION_PINENTRY
:
1148 n
= va_arg(ap
, int);
1150 if (n
!= 0 && n
!= 1) {
1152 return GPG_ERR_INV_VALUE
;
1155 pwm
->use_pinentry
= n
;
1157 case PWMD_OPTION_PINENTRY_TRIES
:
1158 n
= va_arg(ap
, int);
1162 return GPG_ERR_INV_VALUE
;
1165 pwm
->pinentry_tries
= n
;
1167 case PWMD_OPTION_PINENTRY_PATH
:
1168 if (pwm
->pinentry_path
)
1169 xfree(pwm
->pinentry_path
);
1171 pwm
->pinentry_path
= xstrdup(va_arg(ap
, char *));
1173 case PWMD_OPTION_PINENTRY_TTY
:
1174 if (pwm
->pinentry_tty
)
1175 xfree(pwm
->pinentry_tty
);
1177 pwm
->pinentry_tty
= xstrdup(va_arg(ap
, char *));
1179 case PWMD_OPTION_PINENTRY_DISPLAY
:
1180 if (pwm
->pinentry_display
)
1181 xfree(pwm
->pinentry_display
);
1183 pwm
->pinentry_display
= xstrdup(va_arg(ap
, char *));
1185 case PWMD_OPTION_PINENTRY_TERM
:
1186 if (pwm
->pinentry_term
)
1187 xfree(pwm
->pinentry_term
);
1189 pwm
->pinentry_term
= xstrdup(va_arg(ap
, char *));
1191 case PWMD_OPTION_PINENTRY_TITLE
:
1194 pwm
->title
= percent_escape(va_arg(ap
, char *));
1196 case PWMD_OPTION_PINENTRY_PROMPT
:
1199 pwm
->prompt
= percent_escape(va_arg(ap
, char *));
1201 case PWMD_OPTION_PINENTRY_DESC
:
1204 pwm
->desc
= percent_escape(va_arg(ap
, char *));
1207 case PWMD_OPTION_PINENTRY
:
1208 case PWMD_OPTION_PINENTRY_TRIES
:
1209 case PWMD_OPTION_PINENTRY_PATH
:
1210 case PWMD_OPTION_PINENTRY_TTY
:
1211 case PWMD_OPTION_PINENTRY_DISPLAY
:
1212 case PWMD_OPTION_PINENTRY_TERM
:
1213 case PWMD_OPTION_PINENTRY_TITLE
:
1214 case PWMD_OPTION_PINENTRY_PROMPT
:
1215 case PWMD_OPTION_PINENTRY_DESC
:
1216 return GPG_ERR_NOT_IMPLEMENTED
;
1220 return GPG_ERR_NOT_IMPLEMENTED
;