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 PINENTRY_PATH "/usr/bin/pinentry"
60 static int gelapsed
, gtimeout
;
61 static gpg_error_t
pinentry_command(pwm_t
*pwm
, char **result
, const char *cmd
);
64 static gpg_error_t global_error
;
66 const char *pwmd_strerror(gpg_error_t error
)
68 gpg_err_code_t code
= gpg_err_code(error
);
70 if (code
>= GPG_ERR_USER_1
&& code
< gpg_err_code(EPWMD_MAX
)) {
74 return N_("Unknown error");
76 return N_("No cache slots available");
78 return N_("Element not found");
80 return N_("Trailing element");
82 return N_("Invalid character in element");
86 return N_("Will not overwrite existing account");
88 return N_("File not found");
90 return N_("No file is open");
92 return N_("General LibXML error");
94 return N_("File not found in cache");
96 return N_("Attribute not found");
98 return N_("Invalid filename or link");
100 return N_("File modified");
104 return gpg_strerror(error
);
107 gpg_error_t
pwmd_init()
110 bindtextdomain("libpwmd", LOCALEDIR
);
113 assuan_set_malloc_hooks(xmalloc
, xrealloc
, xfree
);
114 assuan_set_assuan_err_source(GPG_ERR_SOURCE_DEFAULT
);
118 pwm_t
*pwmd_connect(const char *path
, gpg_error_t
*error
)
121 char *socketpath
= NULL
;
124 assuan_context_t ctx
;
128 pw
= getpwuid(getuid());
129 socketpath
= (char *)xmalloc(strlen(pw
->pw_dir
) + strlen("/.pwmd/socket") + 1);
130 sprintf(socketpath
, "%s/.pwmd/socket", pw
->pw_dir
);
133 socketpath
= xstrdup(path
);
135 rc
= assuan_socket_connect_ext(&ctx
, socketpath
, -1, 0);
143 if ((pwm
= (pwm_t
*)xcalloc(1, sizeof(pwm_t
))) == NULL
) {
144 *error
= gpg_error_from_errno(errno
);
145 assuan_disconnect(ctx
);
152 pwm
->pinentry_tries
= 3;
159 void pwmd_close(pwm_t
*pwm
)
165 assuan_disconnect(pwm
->ctx
);
168 xfree(pwm
->password
);
179 if (pwm
->pinentry_tty
)
180 xfree(pwm
->pinentry_tty
);
182 if (pwm
->pinentry_display
)
183 xfree(pwm
->pinentry_display
);
185 if (pwm
->pinentry_term
)
186 xfree(pwm
->pinentry_term
);
189 xfree(pwm
->filename
);
194 static int mem_realloc_cb(void *data
, const void *buffer
, size_t len
)
196 membuf_t
*mem
= (membuf_t
*)data
;
202 if ((p
= xrealloc(mem
->buf
, mem
->len
+ len
)) == NULL
)
206 memcpy((char *)mem
->buf
+ mem
->len
, buffer
, len
);
211 static int inquire_cb(void *data
, const char *keyword
)
213 struct inquire_s
*inquire
= (struct inquire_s
*)data
;
215 return assuan_send_data(inquire
->ctx
, inquire
->buf
, inquire
->len
);
218 void pwmd_free_result(void *data
)
223 static gpg_error_t
assuan_command(pwm_t
*pwm
, assuan_context_t ctx
,
224 char **result
, const char *cmd
)
233 * This is needed because assuan only accepts 1000 byte command strings.
234 * If the line is more than this the command will fail. So we use an
235 * INQUIRE on the server which waits for an END that assuan_transact()
238 * Other commands shouldn't need the INQUIRE. Let me know if you have an
239 * element path that's greater than 1000 bytes and I'll fix it.
241 if (strncasecmp(cmd
, "STORE", 5) == 0) {
242 const char *p
= cmd
+ 5;
243 struct inquire_s
*inq
= (struct inquire_s
*)xmalloc(sizeof(struct inquire_s
));
250 inq
->len
= strlen(p
);
251 rc
= assuan_transact(ctx
, "STORE", mem_realloc_cb
, &data
, inquire_cb
,
252 inq
, pwm
->status_func
, pwm
->status_data
);
258 * Ignore any status callback function for pinentry.
260 if (ctx
== pwm
->pctx
)
261 rc
= assuan_transact(ctx
, cmd
, mem_realloc_cb
, &data
, NULL
, NULL
, NULL
, NULL
);
264 rc
= assuan_transact(ctx
, cmd
, mem_realloc_cb
, &data
, NULL
, NULL
,
265 pwm
->status_func
, pwm
->status_data
);
276 mem_realloc_cb(&data
, "", 1);
277 *result
= (char *)data
.buf
;
281 return gpg_err_code(rc
);
284 gpg_error_t
pwmd_terminate_pinentry(pwm_t
*pwm
)
287 return GPG_ERR_NOT_IMPLEMENTED
;
290 if (!pwm
|| pwm
->pid
== -1)
291 return GPG_ERR_INV_ARG
;
293 if (kill(pwm
->pid
, 0) == 0) {
294 if (kill(pwm
->pid
, SIGTERM
) == -1) {
295 if (kill(pwm
->pid
, SIGKILL
) == -1)
296 return gpg_error_from_errno(errno
);
299 global_error
= GPG_ERR_TIMEOUT
;
302 return gpg_error_from_errno(errno
);
308 static gpg_error_t
set_pinentry_strings(pwm_t
*pwm
, int which
)
311 char tmp
[ASSUAN_LINELENGTH
];
315 pwm
->title
= xstrdup(N_("LibPWMD"));
318 pwm
->prompt
= xstrdup(N_("Password:"));
320 if (!pwm
->desc
&& !which
)
321 pwm
->desc
= xstrdup(N_("Enter a password."));
324 snprintf(tmp
, sizeof(tmp
), "SETERROR %s", N_("Invalid password, please try again."));
327 else if (which
== 2) {
328 snprintf(tmp
, sizeof(tmp
), "SETERROR %s", N_("Please type the password again for confirmation."));
332 buf
= (char *)xmalloc(strlen("SETERROR ") + strlen(pwm
->desc
) + 1);
333 sprintf(buf
, "SETERROR %s", pwm
->desc
);
336 error
= pinentry_command(pwm
, NULL
, buf
);
342 buf
= (char *)xmalloc(strlen("SETPROMPT ") + strlen(pwm
->prompt
) + 1);
343 sprintf(buf
, "SETPROMPT %s", pwm
->prompt
);
344 error
= pinentry_command(pwm
, NULL
, buf
);
350 buf
= (char *)xmalloc(strlen("SETDESC ") + strlen(pwm
->title
) + 1);
351 sprintf(buf
, "SETDESC %s", pwm
->title
);
352 error
= pinentry_command(pwm
, NULL
, buf
);
357 static void update_pinentry_settings(pwm_t
*pwm
)
361 struct passwd
*pw
= getpwuid(getuid());
364 snprintf(buf
, sizeof(buf
), "%s/.pwmd/env", pw
->pw_dir
);
366 if ((fp
= fopen(buf
, "r")) == NULL
)
369 while ((p
= fgets(buf
, sizeof(buf
), fp
)) != NULL
) {
370 char name
[32], val
[256];
372 if (sscanf(p
, "%[a-zA-Z]=%s", name
, val
) != 2)
375 if (strcasecmp(name
, "TTY") == 0) {
376 if (!pwm
->pinentry_tty
) {
377 xfree(pwm
->pinentry_tty
);
378 pwm
->pinentry_tty
= xstrdup(val
);
381 else if (strcasecmp(name
, "TERM") == 0) {
382 if (!pwm
->pinentry_term
) {
383 xfree(pwm
->pinentry_term
);
384 pwm
->pinentry_term
= xstrdup(val
);
387 else if (strcasecmp(name
, "DISPLAY") == 0) {
388 if (!pwm
->pinentry_display
) {
389 xfree(pwm
->pinentry_display
);
390 pwm
->pinentry_display
= xstrdup(val
);
398 static gpg_error_t
launch_pinentry(pwm_t
*pwm
)
401 assuan_context_t ctx
;
402 int child_list
[] = {-1};
403 char *display
= getenv("DISPLAY");
405 int have_display
= 0;
408 update_pinentry_settings(pwm
);
410 if (pwm
->pinentry_display
|| display
)
413 tty
= pwm
->pinentry_tty
? pwm
->pinentry_tty
: ttyname(STDOUT_FILENO
);
416 return gpg_error_from_errno(errno
);
419 if (!display
&& !tty
)
420 return GPG_ERR_ENOTTY
;
422 argv
[0] = "pinentry";
423 argv
[1] = have_display
? "--display" : "--ttyname";
424 argv
[2] = have_display
? pwm
->pinentry_display
? pwm
->pinentry_display
: display
: tty
;
428 argv
[3] = "--ttytype";
429 argv
[4] = pwm
->pinentry_term
? pwm
->pinentry_term
: getenv("TERM");
433 rc
= assuan_pipe_connect(&ctx
, pwm
->pinentry_path
? pwm
->pinentry_path
: PINENTRY_PATH
, argv
, child_list
);
438 pwm
->pid
= assuan_get_pid(ctx
);
440 return set_pinentry_strings(pwm
, 0);
443 static gpg_error_t
pinentry_command(pwm_t
*pwm
, char **result
, const char *cmd
)
448 n
= launch_pinentry(pwm
);
454 return assuan_command(pwm
, pwm
->pctx
, result
, cmd
);
457 static void pinentry_disconnect(pwm_t
*pwm
)
460 assuan_disconnect(pwm
->pctx
);
467 * Only called from a child process.
469 static void catchsig(int sig
)
473 if (gelapsed
++ >= gtimeout
) {
474 global_error
= pwmd_terminate_pinentry(gpwm
);
477 global_error
= GPG_ERR_TIMEOUT
;
489 static char *percent_escape(const char *atext
)
491 const unsigned char *s
;
492 int len
= strlen(atext
) * 3 + 1;
493 char *buf
= (char *)xmalloc(len
), *p
= buf
;
498 for (s
=(const unsigned char *)atext
; *s
; s
++) {
500 sprintf (p
, "%%%02X", *s
);
512 static gpg_error_t
send_command(pwm_t
*pwm
, char **result
, const char *cmd
)
515 return GPG_ERR_INV_ARG
;
517 return assuan_command(pwm
, pwm
->ctx
, result
, cmd
);
521 * Avoid sending the BYE command here. libassuan will close the file
522 * descriptor and release the assuan context. Use pwmd_close() instead.
524 gpg_error_t
pwmd_command(pwm_t
*pwm
, char **result
, const char *cmd
, ...)
532 return GPG_ERR_INV_ARG
;
539 len
= vsnprintf(NULL
, 0, cmd
, ap
);
540 buf
= (char *)xmalloc(len
+ 1);
541 len
= vsnprintf(buf
, len
+ 1, cmd
, ap
);
543 error
= send_command(pwm
, result
, buf
);
549 static gpg_error_t
do_getpin(pwm_t
*pwm
, char **result
)
554 signal(SIGALRM
, catchsig
);
559 error
= pinentry_command(pwm
, result
, "GETPIN");
561 if (error
== GPG_ERR_ASS_CANCELED
)
570 static gpg_error_t
getpin(pwm_t
*pwm
, char **result
, int *try_n
, int which
)
572 int pin_try
= *try_n
;
579 error
= set_pinentry_strings(pwm
, which
);
582 pinentry_disconnect(pwm
);
587 if (pwm
->pinentry_tries
-1 != pin_try
) {
588 error
= set_pinentry_strings(pwm
, 1);
591 pinentry_disconnect(pwm
);
597 error
= do_getpin(pwm
, result
);
600 if (pin_try
!= -1 && pin_try
-- && error
== EPWMD_KEY
)
604 pinentry_disconnect(pwm
);
614 gpg_error_t
pwmd_open_nb_finalize(pwm_t
*pwm
, pwmd_nb_status_t
*pw
)
620 return GPG_ERR_NOT_IMPLEMENTED
;
623 if (!pwm
|| !pw
|| !pw
->filename
[0])
624 return GPG_ERR_INV_ARG
;
633 error
= pwmd_command(pwm
, &result
, "ISCACHED %s", pw
->filename
);
639 xfree(pwm
->filename
);
641 pwm
->filename
= xstrdup(pw
->filename
);
642 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
646 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
650 static gpg_error_t
do_open_command(pwm_t
*pwm
, const char *filename
, char *password
)
652 char buf
[ASSUAN_LINELENGTH
];
655 snprintf(buf
, sizeof(buf
), "OPEN %s %s", filename
, password
? password
: "");
656 error
= send_command(pwm
, NULL
, buf
);
657 memset(buf
, 0, sizeof(buf
));
661 static int do_pwmd_open(pwm_t
*pwm
, gpg_error_t
*error
, const char *filename
,
665 char *password
= NULL
;
670 if (!pwm
|| !filename
|| !*filename
) {
671 *error
= GPG_ERR_INV_ARG
;
676 pin_try
= pwm
->pinentry_tries
- 1;
680 * Avoid calling pinentry if the password is cached on the server or if
681 * this is a new file.
683 *error
= pwmd_command(pwm
, &result
, "EXISTS %s", filename
);
686 *error
= pwmd_command(pwm
, &result
, "ISCACHED %s", filename
);
688 if (gpg_err_code(*error
) != gpg_err_code_from_errno(ENOENT
))
694 if (*error
== EPWMD_CACHE_NOT_FOUND
) {
696 password
= pwm
->passfunc(pwm
, pwm
->passdata
);
698 if (!password
|| !*password
) {
703 password
= xstrdup(password
);
707 if (*error
== EPWMD_CACHE_NOT_FOUND
) {
710 * Get the password from pinentry.
712 if (pwm
->use_pinentry
) {
714 * Nonblocking is wanted. fork() then return a file descriptor
715 * that the client can use to read() from.
723 *error
= gpg_error_from_syserror();
732 strncpy(pw
.filename
, filename
, sizeof(pw
.filename
));
742 *error
= getpin(pwm
, &password
, &pin_try
, 0);
747 pinentry_disconnect(pwm
);
749 if (gtimeout
&& gelapsed
>= gtimeout
)
750 *error
= GPG_ERR_TIMEOUT
;
753 write(p
[1], &pw
, sizeof(pw
));
759 * Don't count the time it takes to open the file
760 * which may have many iterations.
762 signal(SIGALRM
, SIG_DFL
);
763 *error
= do_open_command(pwm
, filename
, password
);
766 signal(SIGALRM
, catchsig
);
768 if (pwm
->pctx
&& *error
== EPWMD_BADKEY
) {
770 goto getpin_nb_again
;
775 pinentry_disconnect(pwm
);
777 write(p
[1], &pw
, sizeof(pw
));
782 *error
= gpg_error_from_syserror();
795 *error
= getpin(pwm
, &password
, &pin_try
, 1);
799 pinentry_disconnect(pwm
);
802 *error
= global_error
;
812 * Not using pinentry and the file was not found
815 if (pwm
->password
== NULL
) {
820 password
= pwm
->password
;
830 *error
= do_open_command(pwm
, filename
, password
);
833 * Keep the user defined password set with pwmd_setopt(). The password may
834 * be needed later (pwmd_save()) depending on the pwmd file cache settings.
836 if (password
&& password
!= pwm
->password
)
840 if (pwm
->pctx
&& *error
== EPWMD_BADKEY
) {
844 pinentry_disconnect(pwm
);
851 xfree(pwm
->filename
);
853 pwm
->filename
= xstrdup(filename
);
857 * The file is cached or the file is a new file.
860 return *error
? -1 : -2;
862 return *error
? 1 : 0;
865 gpg_error_t
pwmd_open(pwm_t
*pwm
, const char *filename
)
869 do_pwmd_open(pwm
, &error
, filename
, 0, 0);
873 int pwmd_open_nb(pwm_t
*pwm
, gpg_error_t
*error
, const char *filename
,
877 *error
= GPG_ERR_NOT_IMPLEMENTED
;
880 return do_pwmd_open(pwm
, error
, filename
, 1, timeout
);
885 static gpg_error_t
do_save_getpin(pwm_t
*pwm
, char **password
)
893 error
= getpin(pwm
, &result
, &pin_try
, confirm
? 2 : 0);
897 pinentry_disconnect(pwm
);
910 if (strcmp(*password
, result
)) {
913 pinentry_disconnect(pwm
);
914 error
= EPWMD_BADKEY
;
919 pinentry_disconnect(pwm
);
924 static gpg_error_t
do_save_command(pwm_t
*pwm
, char *password
)
926 char buf
[ASSUAN_LINELENGTH
];
929 snprintf(buf
, sizeof(buf
), "SAVE %s", password
? password
: "");
930 error
= send_command(pwm
, NULL
, buf
);
931 memset(&buf
, 0, sizeof(buf
));
935 gpg_error_t
pwmd_save_nb_finalize(pwm_t
*pwm
, pwmd_nb_status_t
*pw
)
940 return GPG_ERR_NOT_IMPLEMENTED
;
943 if (!pwm
|| !pw
|| !pw
->filename
[0])
944 return GPG_ERR_INV_ARG
;
950 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
954 memset(pw
, 0, sizeof(pwmd_nb_status_t
));
958 static int do_pwmd_save(pwm_t
*pwm
, gpg_error_t
*error
, int nb
)
961 char *password
= NULL
;
964 *error
= GPG_ERR_INV_ARG
;
968 if (pwm
->use_pinentry
|| pwm
->passfunc
) {
969 *error
= pwmd_command(pwm
, &result
, "ISCACHED %s", pwm
->filename
);
971 if (*error
== EPWMD_CACHE_NOT_FOUND
) {
973 if (pwm
->use_pinentry
) {
980 *error
= gpg_error_from_syserror();
989 strncpy(pw
.filename
, pwm
->filename
, sizeof(pw
.filename
));
994 *error
= do_save_getpin(pwm
, &password
);
995 } while (*error
== EPWMD_KEY
|| *error
== EPWMD_BADKEY
);
999 pinentry_disconnect(pwm
);
1002 write(p
[1], &pw
, sizeof(pw
));
1007 *error
= do_save_command(pwm
, password
);
1008 pinentry_disconnect(pwm
);
1010 write(p
[1], &pw
, sizeof(pw
));
1015 *error
= gpg_error_from_syserror();
1027 *error
= do_save_getpin(pwm
, &password
);
1034 if (pwm
->passfunc
) {
1035 char *tmp
= (*pwm
->passfunc
)(pwm
, pwm
->passdata
);
1037 if (!tmp
|| !*tmp
) {
1042 password
= xstrdup(tmp
);
1048 if (!password
|| !*password
) {
1059 password
= pwm
->password
;
1061 *error
= do_save_command(pwm
, password
);
1063 if (password
&& password
!= pwm
->password
)
1067 return *error
? -1 : -2;
1069 return *error
? 1 : 0;
1072 int pwmd_save_nb(pwm_t
*pwm
, gpg_error_t
*error
)
1074 #ifndef USE_PINENTRY
1075 *error
= GPG_ERR_NOT_IMPLEMENTED
;
1078 return do_pwmd_save(pwm
, error
, 1);
1082 gpg_error_t
pwmd_save(pwm_t
*pwm
)
1086 do_pwmd_save(pwm
, &error
, 0);
1090 gpg_error_t
pwmd_setopt(pwm_t
*pwm
, pwmd_option_t opt
, ...)
1094 int n
= va_arg(ap
, int);
1099 return GPG_ERR_INV_ARG
;
1104 case PWMD_OPTION_STATUS_FUNC
:
1105 pwm
->status_func
= va_arg(ap
, pwmd_status_fn
);
1107 case PWMD_OPTION_STATUS_DATA
:
1108 pwm
->status_data
= va_arg(ap
, void *);
1110 case PWMD_OPTION_PASSWORD_FUNC
:
1111 pwm
->passfunc
= va_arg(ap
, pwmd_password_fn
);
1113 case PWMD_OPTION_PASSWORD_DATA
:
1114 pwm
->passdata
= va_arg(ap
, void *);
1116 case PWMD_OPTION_PASSWORD
:
1117 arg1
= va_arg(ap
, char *);
1120 xfree(pwm
->password
);
1122 pwm
->password
= xstrdup(arg1
);
1125 case PWMD_OPTION_PINENTRY
:
1126 n
= va_arg(ap
, int);
1128 if (n
!= 0 && n
!= 1) {
1130 return GPG_ERR_INV_VALUE
;
1133 pwm
->use_pinentry
= n
;
1135 case PWMD_OPTION_PINENTRY_TRIES
:
1136 n
= va_arg(ap
, int);
1140 return GPG_ERR_INV_VALUE
;
1143 pwm
->pinentry_tries
= n
;
1145 case PWMD_OPTION_PINENTRY_PATH
:
1146 if (pwm
->pinentry_path
)
1147 xfree(pwm
->pinentry_path
);
1149 pwm
->pinentry_path
= xstrdup(va_arg(ap
, char *));
1151 case PWMD_OPTION_PINENTRY_TTY
:
1152 if (pwm
->pinentry_tty
)
1153 xfree(pwm
->pinentry_tty
);
1155 pwm
->pinentry_tty
= xstrdup(va_arg(ap
, char *));
1157 case PWMD_OPTION_PINENTRY_DISPLAY
:
1158 if (pwm
->pinentry_display
)
1159 xfree(pwm
->pinentry_display
);
1161 pwm
->pinentry_display
= xstrdup(va_arg(ap
, char *));
1163 case PWMD_OPTION_PINENTRY_TERM
:
1164 if (pwm
->pinentry_term
)
1165 xfree(pwm
->pinentry_term
);
1167 pwm
->pinentry_term
= xstrdup(va_arg(ap
, char *));
1169 case PWMD_OPTION_PINENTRY_TITLE
:
1172 pwm
->title
= percent_escape(va_arg(ap
, char *));
1174 case PWMD_OPTION_PINENTRY_PROMPT
:
1177 pwm
->prompt
= percent_escape(va_arg(ap
, char *));
1179 case PWMD_OPTION_PINENTRY_DESC
:
1182 pwm
->desc
= percent_escape(va_arg(ap
, char *));
1185 case PWMD_OPTION_PINENTRY
:
1186 case PWMD_OPTION_PINENTRY_TRIES
:
1187 case PWMD_OPTION_PINENTRY_PATH
:
1188 case PWMD_OPTION_PINENTRY_TTY
:
1189 case PWMD_OPTION_PINENTRY_DISPLAY
:
1190 case PWMD_OPTION_PINENTRY_TERM
:
1191 case PWMD_OPTION_PINENTRY_TITLE
:
1192 case PWMD_OPTION_PINENTRY_PROMPT
:
1193 case PWMD_OPTION_PINENTRY_DESC
:
1194 return GPG_ERR_NOT_IMPLEMENTED
;
1198 return GPG_ERR_NOT_IMPLEMENTED
;