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>
53 #define PINENTRY_PATH "/usr/local/bin/pinentry"
55 static gpg_error_t
pinentry_command(pwm_t
*pwm
, char **result
, const char *cmd
);
69 const char *pwmd_strerror(gpg_error_t pwmd_errno
)
71 gpg_err_code_t code
= gpg_err_code(pwmd_errno
);
73 if (code
>= GPG_ERR_USER_1
&& code
< gpg_err_code(EPWMD_MAX
)) {
77 return "Unknown error";
79 return "No cache slots available";
81 return "Element not found";
83 return "Trailing element";
85 return "Invalid character in element";
89 return "Will not overwrite existing account";
91 return "File not found";
93 return "No file is open";
95 return "General LibXML error";
97 return "File not found in cache";
99 return "Attribute not found";
100 case GPG_ERR_USER_13
:
101 return "Invalid filename or link";
102 case GPG_ERR_USER_14
:
103 return "File modified";
107 return gpg_strerror(pwmd_errno
);
113 assuan_set_malloc_hooks(xmalloc
, xrealloc
, xfree
);
114 assuan_set_assuan_err_source(GPG_ERR_SOURCE_DEFAULT
);
119 pwm_t
*pwmd_connect(const char *path
, gpg_error_t
*error
)
122 char *socketpath
= NULL
;
125 assuan_context_t ctx
;
129 pw
= getpwuid(getuid());
130 socketpath
= (char *)xmalloc(strlen(pw
->pw_dir
) + strlen("/.pwmd/socket") + 1);
131 sprintf(socketpath
, "%s/.pwmd/socket", pw
->pw_dir
);
134 socketpath
= xstrdup(path
);
136 rc
= assuan_socket_connect_ext(&ctx
, socketpath
, -1, 0);
144 if ((pwm
= (pwm_t
*)xcalloc(1, sizeof(pwm_t
))) == NULL
) {
146 assuan_disconnect(ctx
);
156 void pwmd_close(pwm_t
*pwm
)
162 assuan_disconnect(pwm
->ctx
);
165 xfree(pwm
->password
);
176 if (pwm
->pinentry_tty
)
177 xfree(pwm
->pinentry_tty
);
179 if (pwm
->pinentry_display
)
180 xfree(pwm
->pinentry_display
);
182 if (pwm
->pinentry_term
)
183 xfree(pwm
->pinentry_term
);
186 xfree(pwm
->filename
);
191 static int mem_realloc_cb(void *data
, const void *buffer
, size_t len
)
193 membuf_t
*mem
= (membuf_t
*)data
;
199 if ((p
= xrealloc(mem
->buf
, mem
->len
+ len
)) == NULL
)
203 memcpy((char *)mem
->buf
+ mem
->len
, buffer
, len
);
208 static int inquire_cb(void *data
, const char *keyword
)
210 struct inquire_s
*inquire
= (struct inquire_s
*)data
;
212 return assuan_send_data(inquire
->ctx
, inquire
->buf
, inquire
->len
);
215 void pwmd_free_result(void *data
)
220 static gpg_error_t
assuan_command(assuan_context_t ctx
, char **result
, const char *cmd
)
229 * This is needed because assuan only accepts 1000 byte command strings.
230 * If the line is more than this the command will fail. So we use an
231 * INQUIRE on the server which waits for an END that assuan_transact()
234 * Other commands shouldn't need the INQUIRE. Let me know if you have an
235 * element path that's greater than 1000 bytes and I'll fix it.
237 if (strncasecmp(cmd
, "STORE", 5) == 0) {
238 const char *p
= cmd
+ 5;
239 struct inquire_s
*inq
= (struct inquire_s
*)xmalloc(sizeof(struct inquire_s
));
246 inq
->len
= strlen(p
);
247 rc
= assuan_transact(ctx
, "STORE", mem_realloc_cb
, &data
, inquire_cb
, inq
, NULL
, NULL
);
251 rc
= assuan_transact(ctx
, cmd
, mem_realloc_cb
, &data
, NULL
, NULL
, NULL
, NULL
);
253 // FIXME error codes are wrong for returned (failed) commands.
262 mem_realloc_cb(&data
, "", 1);
263 *result
= (char *)data
.buf
;
271 static gpg_error_t
launch_pinentry(pwm_t
*pwm
)
274 assuan_context_t ctx
;
275 int child_list
[] = {-1};
276 char *display
= getenv("DISPLAY");
279 int have_display
= 0;
282 if (pwm
->pinentry_display
|| display
)
285 tty
= pwm
->pinentry_tty
? pwm
->pinentry_tty
: ttyname(STDOUT_FILENO
);
288 return gpg_error_from_errno(errno
);
291 if (!display
&& !tty
)
292 return GPG_ERR_ENOTTY
;
294 argv
[0] = "pinentry";
295 argv
[1] = have_display
? "--display" : "--ttyname";
296 argv
[2] = have_display
? pwm
->pinentry_display
? pwm
->pinentry_display
: display
: tty
;
300 argv
[3] = "--ttytype";
301 argv
[4] = pwm
->pinentry_term
? pwm
->pinentry_term
: getenv("TERM");
305 rc
= assuan_pipe_connect(&ctx
, pwm
->pinentry_path
? pwm
->pinentry_path
: PINENTRY_PATH
, argv
, child_list
);
313 pwm
->title
= xstrdup("LibPWMD");
316 pwm
->prompt
= xstrdup("Password:");
319 pwm
->desc
= xstrdup("Enter a password.");
321 buf
= (char *)xmalloc(strlen("SETPROMPT ") + strlen(pwm
->prompt
) + 1);
322 sprintf(buf
, "SETPROMPT %s", pwm
->prompt
);
323 pinentry_command(pwm
, NULL
, buf
);
326 buf
= (char *)xmalloc(strlen("SETDESC ") + strlen(pwm
->title
) + 1);
327 sprintf(buf
, "SETDESC %s", pwm
->title
);
328 pinentry_command(pwm
, NULL
, buf
);
331 buf
= (char *)xmalloc(strlen("SETERROR ") + strlen(pwm
->desc
) + 1);
332 sprintf(buf
, "SETERROR %s", pwm
->desc
);
333 pinentry_command(pwm
, NULL
, buf
);
338 static gpg_error_t
pinentry_command(pwm_t
*pwm
, char **result
, const char *cmd
)
343 n
= launch_pinentry(pwm
);
349 return assuan_command(pwm
->pctx
, result
, cmd
);
352 static void pinentry_disconnect(pwm_t
*pwm
)
354 assuan_disconnect(pwm
->pctx
);
358 static char *percent_escape(const char *atext
)
360 const unsigned char *s
;
361 int len
= strlen(atext
) * 3 + 1;
362 char *buf
= (char *)xmalloc(len
), *p
= buf
;
367 for (s
=(const unsigned char *)atext
; *s
; s
++) {
369 sprintf (p
, "%%%02X", *s
);
381 static gpg_error_t
send_command(pwm_t
*pwm
, char **result
, const char *cmd
)
386 return assuan_command(pwm
->ctx
, result
, cmd
);
389 int pwmd_command(pwm_t
*pwm
, char **result
, gpg_error_t
*error
, const char *cmd
, ...)
399 *error
= EPWMD_COMMAND_SYNTAX
;
404 *error
= EPWMD_ERROR
;
410 len
= vsnprintf(NULL
, 0, cmd
, ap
);
411 buf
= xmalloc(len
+ 1);
412 len
= vsnprintf(buf
, len
+ 1, cmd
, ap
);
414 *error
= send_command(pwm
, result
, buf
);
416 return *error
? PWMD_ERROR
: 0;
419 int pwmd_open(pwm_t
*pwm
, gpg_error_t
*error
, const char *filename
)
423 char *password
= NULL
;
425 static char buf
[1000];
427 if (!pwm
|| !filename
|| !*filename
) {
428 *error
= EPWMD_ERROR
;
433 * Avoid calling pinentry if the password is cached on the server.
435 n
= pwmd_command(pwm
, &result
, error
, "ISCACHED %s", filename
);
437 if (n
&& (*error
== EPWMD_CACHE_NOT_FOUND
|| *error
== EPWMD_FILE_NOT_FOUND
)) {
439 password
= (*pwm
->passfunc
)(pwm
->passdata
);
441 if (!password
|| !*password
) {
446 password
= xstrdup(password
);
450 if (*error
== EPWMD_CACHE_NOT_FOUND
) {
453 * Get the password from pinentry.
455 if (pwm
->use_pinentry
) {
456 n
= pinentry_command(pwm
, &password
, "GETPIN");
462 pinentry_disconnect(pwm
);
467 pinentry_disconnect(pwm
);
474 p
= xstrdup(password
);
481 * Not using pinentry and the file was not found
484 if (pwm
->password
== NULL
) {
489 password
= pwm
->password
;
494 else if (*error
!= EPWMD_FILE_NOT_FOUND
)
499 snprintf(buf
, sizeof(buf
), "OPEN %s %s", filename
, password
? password
: "");
500 *error
= send_command(pwm
, NULL
, buf
);
501 memset(&buf
, 0, sizeof(buf
));
504 if (!pwm
->use_pinentry
&& pwm
->password
)
505 pwm
->password
= NULL
;
509 xfree(pwm
->filename
);
511 pwm
->filename
= xstrdup(filename
);
514 return *error
? PWMD_ERROR
: PWMD_OK
;
517 int pwmd_save(pwm_t
*pwm
, gpg_error_t
*error
)
521 char *password
= NULL
;
523 static char buf
[1000];
529 if (pwm
->use_pinentry
|| pwm
->passfunc
) {
533 n
= pwmd_command(pwm
, &result
, error
, "ISCACHED %s", pwm
->filename
);
535 if (n
== PWMD_ERROR
&& (*error
== EPWMD_CACHE_NOT_FOUND
|| *error
== EPWMD_FILE_NOT_FOUND
)) {
537 if (pwm
->use_pinentry
) {
538 n
= pinentry_command(pwm
, &password
, "GETPIN");
544 pinentry_disconnect(pwm
);
549 pinentry_disconnect(pwm
);
554 password
= (*pwm
->passfunc
)(pwm
->passdata
);
565 if (!password
|| !*password
) {
570 p
= xstrdup(password
);
573 if (!pwm
->use_pinentry
) {
574 memset(password
, 0, strlen(password
));
581 memset(password
, 0, strlen(password
));
592 password
= pwm
->password
;
594 snprintf(buf
, sizeof(buf
), "SAVE %s", password
? password
: "");
595 *error
= send_command(pwm
, NULL
, buf
);
596 memset(&buf
, 0, sizeof(buf
));
598 pwm
->password
= NULL
;
599 return *error
? PWMD_ERROR
: 0;
602 int pwmd_setopt(pwm_t
*pwm
, gpg_error_t
*error
, pwmd_option opt
, ...)
606 int n
= va_arg(ap
, int);
616 case PWMD_OPTION_PASSWORD_FUNC
:
617 pwm
->passfunc
= va_arg(ap
, pwmd_password_func
*);
619 case PWMD_OPTION_PASSWORD_DATA
:
620 pwm
->passdata
= va_arg(ap
, void *);
623 case PWMD_OPTION_PINENTRY
:
626 if (n
!= 0 && n
!= 1) {
631 pwm
->use_pinentry
= n
;
633 case PWMD_OPTION_PINENTRY_PATH
:
634 if (pwm
->pinentry_path
)
635 xfree(pwm
->pinentry_path
);
637 pwm
->pinentry_path
= xstrdup(va_arg(ap
, char *));
639 case PWMD_OPTION_PINENTRY_TTY
:
640 if (pwm
->pinentry_tty
)
641 xfree(pwm
->pinentry_tty
);
643 pwm
->pinentry_tty
= xstrdup(va_arg(ap
, char *));
645 case PWMD_OPTION_PINENTRY_DISPLAY
:
646 if (pwm
->pinentry_display
)
647 xfree(pwm
->pinentry_display
);
649 pwm
->pinentry_display
= xstrdup(va_arg(ap
, char *));
651 case PWMD_OPTION_PINENTRY_TERM
:
652 if (pwm
->pinentry_term
)
653 xfree(pwm
->pinentry_term
);
655 pwm
->pinentry_term
= xstrdup(va_arg(ap
, char *));
658 case PWMD_OPTION_PINENTRY
:
659 case PWMD_OPTION_PINENTRY_PATH
:
660 case PWMD_OPTION_PINENTRY_TTY
:
661 case PWMD_OPTION_PINENTRY_DISPLAY
:
662 case PWMD_OPTION_PINENTRY_TERM
:
663 *error
= EPWMD_ERROR
;
666 case PWMD_OPTION_PASSWORD
:
667 arg1
= va_arg(ap
, char *);
670 xfree(pwm
->password
);
672 pwm
->password
= xstrdup(arg1
);
675 case PWMD_OPTION_TITLE
:
678 pwm
->title
= percent_escape(va_arg(ap
, char *));
680 case PWMD_OPTION_PROMPT
:
683 pwm
->prompt
= percent_escape(va_arg(ap
, char *));
685 case PWMD_OPTION_DESC
:
688 pwm
->desc
= percent_escape(va_arg(ap
, char *));
692 *error
= EPWMD_ERROR
;
701 int pwmd_get_password(pwm_t
*pwm
, gpg_error_t
*error
, const char *filename
)
705 char *password
= NULL
;
710 memset(&pw
, 0, sizeof(pw
));
713 *error
= EPWMD_ERROR
;
718 n
= pwmd_command(pwm
, &result
, error
, "ISCACHED %s", filename
);
720 if (n
!= PWMD_OK
&& *error
!= EPWMD_CACHE_NOT_FOUND
&& *error
!= EPWMD_FILE_NOT_FOUND
)
723 * Don't need a password for non-existant files.
725 else if ((n
!= PWMD_OK
&& *error
== EPWMD_FILE_NOT_FOUND
) || n
== PWMD_OK
) {
732 *error
= gpg_error_from_syserror();
741 n
= pinentry_command(pwm
, &password
, "GETPIN");
747 pinentry_disconnect(pwm
);
750 write(p
[1], &pw
, sizeof(pw
));
754 if (!password
|| !*password
)
755 pw
.error
= EPWMD_KEY
;
757 strncpy(pw
.password
, password
, sizeof(pw
.password
));
759 write(p
[1], &pw
, sizeof(pw
));
760 memset(&pw
.password
, 0, sizeof(pw
.password
));
765 *error
= gpg_error_from_syserror();