1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011
4 Ben Kibbey <bjk@luxsci.net>
6 This file is part of libpwmd.
8 Libpwmd is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
11 (at your option) any later version.
13 Libpwmd is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Libpwmd. If not, see <http://www.gnu.org/licenses/>.
32 #include <sys/socket.h>
41 #include <sys/types.h>
43 #include <sys/select.h>
63 #define FINISH(rc) (gpg_err_source(rc) == GPG_ERR_SOURCE_UNKNOWN) \
71 ssize_t
hook_read(assuan_context_t ctx
, assuan_fd_t fd
, void *data
,
75 pwm_t
*pwm
= assuan_get_pointer(ctx
);
77 if (pwm
&& pwm
->tcp_conn
)
78 return read_hook_ssh(pwm
, fd
, data
, len
);
81 return read((int)fd
, data
, len
);
84 ssize_t
hook_write(assuan_context_t ctx
, assuan_fd_t fd
, const void *data
,
89 pwm_t
*pwm
= assuan_get_pointer(ctx
);
91 if (pwm
&& pwm
->tcp_conn
)
92 return write_hook_ssh(pwm
, fd
, data
, len
);
95 /* libassuan cannot handle EAGAIN when doing writes. */
97 wrote
= write((int)fd
, data
, len
);
99 if (wrote
== -1 && errno
== EAGAIN
) {
102 } while (wrote
== -1 && errno
== EAGAIN
);
107 pid_t
hook_waitpid(assuan_context_t ctx
, pid_t pid
, int action
, int *status
,
110 return waitpid(pid
, status
, options
);
113 gpg_error_t
pwmd_init()
115 static int initialized
;
124 bindtextdomain("libpwmd", LOCALEDIR
);
134 gpg_error_t
_connect_finalize(pwm_t
*pwm
)
138 int n
= assuan_get_active_fds(pwm
->ctx
, 0, active
, N_ARRAY(active
));
141 return GPG_ERR_EBADFD
;
149 rc
= pwmd_command(pwm
, NULL
, NULL
, NULL
, NULL
, "OPTION NAME=%s", pwm
->name
);
154 static gpg_error_t
connect_uds(pwm_t
*pwm
, const char *path
)
156 char *socketpath
= NULL
;
162 return GPG_ERR_INV_ARG
;
164 pwbuf
= _getpwuid(&pw
);
166 return gpg_error_from_syserror();
169 socketpath
= pwmd_strdup_printf("%s/.pwmd/socket", pw
.pw_dir
);
171 socketpath
= _expand_homedir((char *)path
, &pw
);
175 return GPG_ERR_ENOMEM
;
177 rc
= assuan_socket_connect(pwm
->ctx
, socketpath
, ASSUAN_INVALID_FD
, 0);
178 pwmd_free(socketpath
);
179 return rc
? rc
: _connect_finalize(pwm
);
182 gpg_error_t
pwmd_connect(pwm_t
*pwm
, const char *url
, const char *identity
,
183 const char *knownhosts
)
185 char *p
= (char *)url
;
186 gpg_error_t rc
= GPG_ERR_UNSUPPORTED_PROTOCOL
;
189 return FINISH(GPG_ERR_INV_ARG
);
192 rc
= connect_uds(pwm
, p
);
193 else if (!p
|| !strncmp(p
, "file://", 7)) {
196 rc
= connect_uds(pwm
, p
);
198 else if (!strncmp(p
, "ssh://", 6) || !strncmp(p
, "ssh6://", 7) ||
199 !strncmp(p
, "ssh4://", 7)) {
201 return FINISH(GPG_ERR_NOT_IMPLEMENTED
);
205 char *username
= NULL
;
207 if (!strncmp(p
, "ssh6://", 7)) {
208 pwm
->prot
= PWMD_IPV6
;
211 else if (!strncmp(p
, "ssh4://", 7)) {
212 pwm
->prot
= PWMD_IPV4
;
216 pwm
->prot
= PWMD_IP_ANY
;
220 /* X11 forwarding is not supported. */
221 pwmd_setopt(pwm
, PWMD_OPTION_NO_PINENTRY
, 1);
222 rc
= _parse_ssh_url(p
, &host
, &port
, &username
);
224 rc
= _do_ssh_connect(pwm
, host
, port
, identity
, username
, knownhosts
);
226 rc
= _connect_finalize(pwm
);
228 _free_ssh_conn(pwm
->tcp_conn
);
229 pwm
->tcp_conn
= NULL
;
243 static void disconnect(pwm_t
*pwm
)
245 if (!pwm
|| !pwm
->ctx
)
248 assuan_release(pwm
->ctx
);
250 _ssh_disconnect(pwm
);
256 void pwmd_close(pwm_t
*pwm
)
262 pwmd_free(pwm
->error
);
263 pwmd_free(pwm
->desc
);
264 pwmd_free(pwm
->prompt
);
265 pwmd_free(pwm
->pinentry_tty
);
266 pwmd_free(pwm
->pinentry_display
);
267 pwmd_free(pwm
->pinentry_term
);
268 pwmd_free(pwm
->lcctype
);
269 pwmd_free(pwm
->lcmessages
);
270 pwmd_free(pwm
->filename
);
271 pwmd_free(pwm
->name
);
275 _free_ssh_conn(pwm
->tcp_conn
);
280 _pinentry_disconnect(pwm
);
286 static gpg_error_t
inquire_realloc_cb(void *data
, const void *buffer
,
289 membuf_t
*mem
= (membuf_t
*)data
;
295 if ((p
= pwmd_realloc(mem
->buf
, mem
->len
+ len
)) == NULL
)
296 return gpg_error(GPG_ERR_ENOMEM
);
299 memcpy((char *)mem
->buf
+ mem
->len
, buffer
, len
);
304 static gpg_error_t
get_password(pwm_t
*pwm
, char **result
, pwmd_pinentry_t w
,
307 char buf
[LINE_MAX
] = {0}, *p
;
308 struct termios told
, tnew
;
313 if (!isatty(STDIN_FILENO
)) {
314 fprintf(stderr
, N_("Input is not from a terminal! Failing.\n"));
315 return GPG_ERR_ENOTTY
;
319 if (tcgetattr(STDIN_FILENO
, &told
) == -1)
320 return gpg_error_from_syserror();
322 memcpy(&tnew
, &told
, sizeof(struct termios
));
323 tnew
.c_lflag
&= ~(ECHO
);
324 tnew
.c_lflag
|= ICANON
|ECHONL
;
326 if (tcsetattr(STDIN_FILENO
, TCSANOW
, &tnew
) == -1) {
329 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
330 return gpg_error_from_errno(n
);
335 case PWMD_PINENTRY_OPEN
:
336 fprintf(stderr
, N_("Password for %s: "), pwm
->filename
);
338 case PWMD_PINENTRY_OPEN_FAILED
:
339 fprintf(stderr
, N_("Invalid password. Password for %s: "),
342 case PWMD_PINENTRY_SAVE
:
343 fprintf(stderr
, N_("New password for %s: "), pwm
->filename
);
345 case PWMD_PINENTRY_SAVE_CONFIRM
:
346 fprintf(stderr
, N_("Confirm password: "));
352 if ((p
= fgets(buf
, sizeof(buf
), stdin
)) == NULL
) {
353 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
358 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
362 return GPG_ERR_CANCELED
;
365 p
[strlen(p
) - 1] = 0;
368 key
= pwmd_strdup_printf("%s", p
);
369 memset(&buf
, 0, sizeof(buf
));
372 return GPG_ERR_ENOMEM
;
379 static gpg_error_t
inquire_password(pwm_t
*pwm
, const char *keyword
,
380 char **data
, size_t *size
)
383 int new_password
= 0;
385 char *password
= NULL
, *newpass
= NULL
;
387 if (!strcmp(keyword
, "NEW_PASSPHRASE"))
390 if (pwm
->disable_pinentry
&& !pwm
->local_pinentry
) {
391 rc
= get_password(pwm
, &password
,
392 new_password
? PWMD_PINENTRY_SAVE
: PWMD_PINENTRY_OPEN
, 0);
393 if (!rc
&& new_password
)
394 rc
= get_password(pwm
, &newpass
, PWMD_PINENTRY_SAVE_CONFIRM
, 0);
397 rc
= pwmd_getpin(pwm
, pwm
->filename
, &password
, &len
,
398 new_password
? PWMD_PINENTRY_SAVE
: PWMD_PINENTRY_OPEN
);
399 if (!rc
&& new_password
)
400 rc
= pwmd_getpin(pwm
, pwm
->filename
, &newpass
, &len
,
401 PWMD_PINENTRY_SAVE_CONFIRM
);
403 (void)pwmd_getpin(pwm
, pwm
->filename
, NULL
, NULL
, PWMD_PINENTRY_CLOSE
);
406 if (!rc
&& newpass
) {
407 if ((!password
&& newpass
) || (!newpass
&& password
)
408 || strcmp(newpass
, password
)) {
409 fprintf(stderr
, N_("Passphrases do not match.\n"));
411 rc
= GPG_ERR_CANCELED
;
419 *size
= password
? strlen(password
) : 0;
425 static gpg_error_t
inquire_cb(void *data
, const char *keyword
)
427 pwm_t
*pwm
= (pwm_t
*)data
;
432 /* Shouldn't get this far without a callback. */
433 if (!pwm
->override_inquire
&& !pwm
->inquire_func
)
434 return gpg_error(GPG_ERR_ASS_NO_INQUIRE_CB
);
440 int new_password
= 0;
444 if (!strcmp(keyword
, "PASSPHRASE"))
446 else if (!strcmp(keyword
, "NEW_PASSPHRASE"))
449 if (!pwm
->override_inquire
&& (is_password
|| new_password
)) {
451 rc
= inquire_password(data
, keyword
, &result
, &len
);
454 rc
= pwm
->inquire_func(pwm
->inquire_data
, keyword
, rc
, &result
,
457 if (gpg_err_code(rc
) == GPG_ERR_CANCELED
) {
458 rc
= assuan_send_data(pwm
->ctx
, NULL
, 1);
463 /* There is a bug (or feature?) in assuan_send_data() that
464 * when cancelling an inquire the next read from the server is
465 * not done until the next command making the next command
468 rc
= assuan_read_line(pwm
->ctx
, &line
, &len
);
470 rc
= gpg_error(GPG_ERR_CANCELED
);
476 if (gpg_err_code(rc
) == GPG_ERR_EOF
|| !rc
) {
477 if (len
<= 0 && !result
) {
481 else if ((len
<= 0 && result
) || (len
&& !result
)) {
482 rc
= gpg_error(GPG_ERR_INV_ARG
);
486 arc
= assuan_send_data(pwm
->ctx
, result
, len
);
487 if (gpg_err_code(rc
) == GPG_ERR_EOF
) {
498 pwm
->inquire_sent
+= len
;
500 if (pwm
->status_func
) {
501 char buf
[ASSUAN_LINELENGTH
];
503 snprintf(buf
, sizeof(buf
), "XFER %u %u", pwm
->inquire_sent
,
505 rc
= pwm
->status_func(pwm
->status_data
, buf
);
518 static gpg_error_t
parse_assuan_line(pwm_t
*pwm
)
524 rc
= assuan_read_line(pwm
->ctx
, &line
, &len
);
526 if (line
[0] == 'O' && line
[1] == 'K' &&
527 (line
[2] == 0 || line
[2] == ' ')) {
529 else if (line
[0] == '#') {
531 else if (line
[0] == 'S' && (line
[1] == 0 || line
[1] == ' ')) {
532 if (pwm
->status_func
) {
533 rc
= pwm
->status_func(pwm
->status_data
,
534 line
[1] == 0 ? line
+1 : line
+2);
537 else if (line
[0] == 'E' && line
[1] == 'R' && line
[2] == 'R' &&
538 (line
[3] == 0 || line
[3] == ' ')) {
547 static void reset_handle_state(pwm_t
*pwm
, int done
)
551 pwm
->tcp_conn
->rc
= 0;
553 if (done
&& pwm
->tcp_conn
) {
554 _free_ssh_conn(pwm
->tcp_conn
);
555 pwm
->tcp_conn
= NULL
;
560 static void reset_handle(pwm_t
*h
)
565 _pinentry_disconnect(h
);
568 reset_handle_state(h
, 0);
571 gpg_error_t
pwmd_disconnect(pwm_t
*pwm
)
574 return FINISH(GPG_ERR_INV_ARG
);
577 if (pwm
->fd
== -1 && pwm
->tcp_conn
&& pwm
->tcp_conn
->fd
== -1)
581 return FINISH(GPG_ERR_INV_STATE
);
587 _ssh_disconnect(pwm
);
594 gpg_error_t
pwmd_process(pwm_t
*pwm
)
598 struct timeval tv
= {0, 0};
602 return FINISH(GPG_ERR_INV_ARG
);
604 return FINISH(GPG_ERR_INV_STATE
);
607 FD_SET(pwm
->fd
, &fds
);
608 n
= select(pwm
->fd
+1, &fds
, NULL
, NULL
, &tv
);
611 return FINISH(gpg_error_from_syserror());
614 if (FD_ISSET(pwm
->fd
, &fds
))
615 rc
= parse_assuan_line(pwm
);
618 while (!rc
&& assuan_pending_line(pwm
->ctx
))
619 rc
= parse_assuan_line(pwm
);
624 gpg_error_t
_assuan_command(pwm_t
*pwm
, assuan_context_t ctx
,
625 char **result
, size_t *len
, const char *cmd
)
631 return FINISH(GPG_ERR_INV_ARG
);
633 if (strlen(cmd
) >= ASSUAN_LINELENGTH
+1)
634 return FINISH(GPG_ERR_LINE_TOO_LONG
);
638 rc
= assuan_transact(ctx
, cmd
, inquire_realloc_cb
, &data
,
640 pwm
->pctx
== ctx
? pwm
->_inquire_func
: inquire_cb
,
641 pwm
->pctx
== ctx
? pwm
->_inquire_data
: pwm
,
645 pwm
->status_func
, pwm
->status_data
);
655 inquire_realloc_cb(&data
, "", 1);
658 *result
= (char *)data
.buf
;
670 gpg_error_t
pwmd_command_ap(pwm_t
*pwm
, char **result
, size_t *rlen
,
671 pwmd_inquire_cb_t func
, void *user
, const char *cmd
, va_list ap
)
678 return FINISH(GPG_ERR_INV_ARG
);
680 return FINISH(GPG_ERR_INV_STATE
);
683 * C99 allows the dst pointer to be null which will calculate the length
684 * of the would-be result and return it.
687 len
= vsnprintf(NULL
, 0, cmd
, ap
)+1;
688 buf
= (char *)pwmd_malloc(len
);
691 return FINISH(GPG_ERR_ENOMEM
);
694 len
= vsnprintf(buf
, len
, cmd
, ap2
);
697 if (buf
[strlen(buf
)-1] == '\n')
698 buf
[strlen(buf
)-1] = 0;
699 if (buf
[strlen(buf
)-1] == '\r')
700 buf
[strlen(buf
)-1] = 0;
702 pwm
->inquire_func
= func
;
703 pwm
->inquire_data
= user
;
704 pwm
->inquire_sent
= 0;
705 gpg_error_t rc
= _assuan_command(pwm
, pwm
->ctx
, result
, rlen
, buf
);
710 gpg_error_t
pwmd_command(pwm_t
*pwm
, char **result
, size_t *len
,
711 pwmd_inquire_cb_t func
, void *user
, const char *cmd
, ...)
716 return FINISH(GPG_ERR_INV_ARG
);
718 return FINISH(GPG_ERR_INV_STATE
);
724 gpg_error_t rc
= pwmd_command_ap(pwm
, result
, len
, func
, user
, cmd
, ap
);
729 static gpg_error_t
send_pinentry_options(pwm_t
*pwm
)
733 if (pwm
->pinentry_tty
) {
734 rc
= pwmd_command(pwm
, NULL
, NULL
, NULL
, NULL
, "OPTION TTYNAME=%s", pwm
->pinentry_tty
);
739 if (pwm
->pinentry_term
) {
740 rc
= pwmd_command(pwm
, NULL
, NULL
, NULL
, NULL
, "OPTION TTYTYPE=%s", pwm
->pinentry_term
);
745 if (pwm
->pinentry_display
) {
746 rc
= pwmd_command(pwm
, NULL
, NULL
, NULL
, NULL
, "OPTION DISPLAY=%s",
747 pwm
->pinentry_display
);
753 rc
= pwmd_command(pwm
, NULL
, NULL
, NULL
, NULL
, "OPTION DESC=%s", pwm
->desc
);
759 rc
= pwmd_command(pwm
, NULL
, NULL
, NULL
, NULL
, "OPTION LC_CTYPE=%s", pwm
->lcctype
);
764 if (pwm
->lcmessages
) {
765 rc
= pwmd_command(pwm
, NULL
, NULL
, NULL
, NULL
, "OPTION LC_MESSAGES=%s", pwm
->lcmessages
);
773 gpg_error_t
pwmd_socket_type(pwm_t
*pwm
, pwmd_socket_t
*result
)
776 return FINISH(GPG_ERR_INV_ARG
);
779 if ((pwm
->fd
== -1 && !pwm
->tcp_conn
) ||
780 (pwm
->fd
== -1 && pwm
->tcp_conn
&& pwm
->tcp_conn
->fd
== -1))
784 return FINISH(GPG_ERR_INV_STATE
);
787 *result
= pwm
->tcp_conn
? PWMD_SOCKET_SSH
: PWMD_SOCKET_LOCAL
;
789 *result
= PWMD_SOCKET_LOCAL
;
794 gpg_error_t
pwmd_open(pwm_t
*pwm
, const char *filename
, pwmd_inquire_cb_t cb
,
799 int no_pinentry
= pwm
->disable_pinentry
|| pwm
->tcp_conn
|| pwm
->local_pinentry
;
801 int no_pinentry
= pwm
->disable_pinentry
|| pwm
->local_pinentry
;
804 if (!pwm
|| !filename
|| !*filename
)
805 return FINISH(GPG_ERR_INV_ARG
);
808 return FINISH(GPG_ERR_INV_STATE
);
811 rc
= send_pinentry_options(pwm
);
814 pwmd_free(pwm
->filename
);
815 pwm
->filename
= pwmd_strdup(filename
);
816 rc
= pwmd_command(pwm
, NULL
, NULL
, cb
, data
, "OPEN %s%s%s",
817 (pwm
->opts
& OPT_LOCK_ON_OPEN
) ? "--lock " : "",
818 no_pinentry
? "--no-pinentry " : "", filename
);
820 pwmd_free(pwm
->filename
);
821 pwm
->filename
= NULL
;
828 gpg_error_t
pwmd_save(pwm_t
*pwm
, const char *args
, pwmd_inquire_cb_t cb
,
834 return FINISH(GPG_ERR_INV_ARG
);
836 return FINISH(GPG_ERR_INV_STATE
);
838 rc
= pwmd_command(pwm
, NULL
, NULL
, cb
, data
, "SAVE %s",
843 gpg_error_t
pwmd_setopt(pwm_t
*pwm
, pwmd_option_t opt
, ...)
851 return FINISH(GPG_ERR_INV_ARG
);
856 case PWMD_OPTION_LOCK_ON_OPEN
:
860 rc
= GPG_ERR_INV_VALUE
;
863 pwm
->opts
|= OPT_LOCK_ON_OPEN
;
865 pwm
->opts
&= ~OPT_LOCK_ON_OPEN
;
868 case PWMD_OPTION_INQUIRE_TOTAL
:
869 pwm
->inquire_total
= va_arg(ap
, size_t);
871 case PWMD_OPTION_STATUS_CB
:
872 pwm
->status_func
= va_arg(ap
, pwmd_status_cb_t
);
874 case PWMD_OPTION_STATUS_DATA
:
875 pwm
->status_data
= va_arg(ap
, void *);
877 case PWMD_OPTION_NO_PINENTRY
:
881 rc
= GPG_ERR_INV_VALUE
;
883 pwm
->disable_pinentry
= n
;
886 case PWMD_OPTION_LOCAL_PINENTRY
:
890 rc
= GPG_ERR_INV_VALUE
;
892 pwm
->local_pinentry
= n
;
895 case PWMD_OPTION_PINENTRY_TIMEOUT
:
899 rc
= GPG_ERR_INV_VALUE
;
901 pwm
->pinentry_timeout
= n
;
904 case PWMD_OPTION_PINENTRY_PATH
:
905 if (pwm
->pinentry_path
)
906 pwmd_free(pwm
->pinentry_path
);
908 pwm
->pinentry_path
= _expand_homedir(va_arg(ap
, char *), NULL
);
910 case PWMD_OPTION_PINENTRY_TTY
:
911 arg1
= va_arg(ap
, char *);
913 if (pwm
->pinentry_tty
)
914 pwmd_free(pwm
->pinentry_tty
);
916 pwm
->pinentry_tty
= arg1
? pwmd_strdup(arg1
) : NULL
;
918 case PWMD_OPTION_PINENTRY_DISPLAY
:
919 if (pwm
->pinentry_display
)
920 pwmd_free(pwm
->pinentry_display
);
922 pwm
->pinentry_display
= pwmd_strdup(va_arg(ap
, char *));
924 case PWMD_OPTION_PINENTRY_TERM
:
925 arg1
= va_arg(ap
, char *);
927 if (pwm
->pinentry_term
)
928 pwmd_free(pwm
->pinentry_term
);
930 pwm
->pinentry_term
= arg1
? pwmd_strdup(arg1
) : NULL
;
932 case PWMD_OPTION_PINENTRY_ERROR
:
934 pwmd_free(pwm
->error
);
936 pwm
->error
= _percent_escape(va_arg(ap
, char *));
938 case PWMD_OPTION_PINENTRY_PROMPT
:
940 pwmd_free(pwm
->prompt
);
942 pwm
->prompt
= _percent_escape(va_arg(ap
, char *));
944 case PWMD_OPTION_PINENTRY_DESC
:
946 pwmd_free(pwm
->desc
);
948 pwm
->desc
= _percent_escape(va_arg(ap
, char *));
950 case PWMD_OPTION_PINENTRY_LC_CTYPE
:
951 arg1
= va_arg(ap
, char *);
954 pwmd_free(pwm
->lcctype
);
956 pwm
->lcctype
= arg1
? pwmd_strdup(arg1
) : NULL
;
958 case PWMD_OPTION_PINENTRY_LC_MESSAGES
:
959 arg1
= va_arg(ap
, char *);
962 pwmd_free(pwm
->lcmessages
);
964 pwm
->lcmessages
= arg1
? pwmd_strdup(arg1
) : NULL
;
967 case PWMD_OPTION_KNOWNHOST_CB
:
968 pwm
->kh_cb
= va_arg(ap
, pwmd_knownhost_cb_t
);
970 case PWMD_OPTION_KNOWNHOST_DATA
:
971 pwm
->kh_data
= va_arg(ap
, void *);
973 case PWMD_OPTION_SSH_AGENT
:
974 pwm
->use_agent
= va_arg(ap
, int);
976 if (pwm
->use_agent
< 0 || pwm
->use_agent
> 1) {
978 rc
= GPG_ERR_INV_VALUE
;
983 case PWMD_OPTION_KNOWNHOST_CB
:
984 case PWMD_OPTION_KNOWNHOST_DATA
:
985 case PWMD_OPTION_SSH_AGENT
:
986 rc
= GPG_ERR_NOT_IMPLEMENTED
;
989 case PWMD_OPTION_OVERRIDE_INQUIRE
:
990 pwm
->override_inquire
= va_arg(ap
, int);
992 if (pwm
->override_inquire
< 0 || pwm
->override_inquire
> 1) {
993 pwm
->override_inquire
= 0;
994 rc
= GPG_ERR_INV_VALUE
;
998 rc
= GPG_ERR_UNKNOWN_OPTION
;
1006 gpg_error_t
pwmd_new(const char *name
, pwm_t
**pwm
)
1008 pwm_t
*h
= pwmd_calloc(1, sizeof(pwm_t
));
1010 static struct assuan_malloc_hooks mhooks
= {
1011 pwmd_malloc
, pwmd_realloc
, pwmd_free
1013 static struct assuan_system_hooks shooks
= {
1014 ASSUAN_SYSTEM_HOOKS_VERSION
,
1022 NULL
, //sendmsg both are used for FD passing
1025 __assuan_socketpair
,
1031 return FINISH(GPG_ERR_ENOMEM
);
1034 h
->name
= pwmd_strdup(name
);
1037 return FINISH(GPG_ERR_ENOMEM
);
1042 h
->pinentry_timeout
= -30;
1044 h
->prot
= PWMD_IP_ANY
;
1047 if (ttyname(STDOUT_FILENO
)) {
1050 ttyname_r(STDOUT_FILENO
, buf
, sizeof(buf
));
1051 h
->pinentry_tty
= pwmd_strdup(buf
);
1052 if (!h
->pinentry_tty
) {
1053 rc
= GPG_ERR_ENOMEM
;
1058 if (getenv("TERM") && h
->pinentry_tty
) {
1059 h
->pinentry_term
= pwmd_strdup(getenv("TERM"));
1060 if (!h
->pinentry_term
) {
1061 rc
= GPG_ERR_ENOMEM
;
1066 if (getenv("DISPLAY")) {
1067 h
->pinentry_display
= pwmd_strdup(getenv("DISPLAY"));
1068 if (!h
->pinentry_display
) {
1069 rc
= GPG_ERR_ENOMEM
;
1074 update_pinentry_settings(h
);
1075 rc
= assuan_new_ext(&h
->ctx
, GPG_ERR_SOURCE_DEFAULT
, &mhooks
, NULL
, NULL
);
1079 assuan_set_pointer(h
->ctx
, h
);
1080 assuan_ctx_set_system_hooks(h
->ctx
, &shooks
);
1089 void pwmd_free(void *ptr
)
1094 void *pwmd_malloc(size_t size
)
1096 return _xmalloc(size
);
1099 void *pwmd_calloc(size_t nmemb
, size_t size
)
1101 return _xcalloc(nmemb
, size
);
1104 void *pwmd_realloc(void *ptr
, size_t size
)
1106 return _xrealloc(ptr
, size
);
1109 char *pwmd_strdup(const char *str
)
1111 return _xstrdup(str
);
1114 char *pwmd_strdup_printf(const char *fmt
, ...)
1125 len
= vsnprintf(NULL
, 0, fmt
, ap
);
1127 buf
= pwmd_malloc(++len
);
1129 vsnprintf(buf
, len
, fmt
, ap2
);
1135 gpg_error_t
pwmd_getpin(pwm_t
*pwm
, const char *filename
, char **result
,
1136 size_t *len
, pwmd_pinentry_t which
)
1138 #ifndef WITH_PINENTRY
1139 return FINISH(GPG_ERR_NOT_IMPLEMENTED
);
1141 gpg_error_t rc
= _pwmd_getpin(pwm
, filename
, result
, len
, which
);
1147 const char *pwmd_version()
1149 return LIBPWMD_VERSION_STR
;
1152 unsigned int pwmd_features()
1156 #ifdef WITH_PINENTRY
1157 n
|= PWMD_FEATURE_PINENTRY
;
1160 n
|= PWMD_FEATURE_SSH
;
1163 n
|= PWMD_FEATURE_CRACK
;