Updated the manual page.
[libpwmd.git] / src / libpwmd.h.in
blobca61a412088bfdc9ad1687498aa1ea40d39b9525
1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
2 /*
3 Copyright (C) 2006-2009 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 02110-1301 USA
19 #ifndef LIBPWMD_API
20 #define LIBPWMD_API
21 #endif
23 /*! \headerfile libpwmd.h
25 * libpwmd is a library making it easy for applications to use the pwmd
26 * server. Pwmd version 2.0 or later is required; either locally or remotely.
29 /*! \section ssh SSH Details
31 * A remote connection to a pwmd server is possible by using an SSH channel
32 * which spawns a shell and executes a proxy server that connects to the pwmd
33 * local Unix Domain Socket. Authentication is done by using SSH public key
34 * (see \ref ssh-keygen(1)) authentication and verifying the host key against
35 * a local OpenSSH known hosts formatted file.
37 * The servers public key can be had by using \ref pwmd_get_hostkey() and
38 * storing the result in a file or done automatially by using a callback
39 * function \ref pwmd_knownhost_cb_t while connecting to the unknown host.
41 * On the server side you'll need a proxy server to connect to the real pwmd
42 * server. Here's an example \ref authorized_keys(5) entry that will do this.
43 * The hash portion should be the same as the contents of the generated \ref
44 * ssh-keygen(1) \a identity.pub file which is passed as a parameter to the
45 * SSH connection functions:
47 * \code
48 * command="socat UNIX-CONNECT:$HOME/.pwmd/socket -" <hash> ...
49 * \endcode
51 * \note Only an SSH identity without a passphrase is supported. For now
52 * anyway. This is a limitation of libssh2 (version 1.1 as of this writing).
54 * \version 6.0.3
55 * The first version to use the OpenSSH known hosts file format exclusively.
56 * Earlier versions used only an SHA1 hash of the host key.
58 * \x11
61 /*! \section pinentry Pinentry Details
63 * \ref pinentry(1) is a program that prompts the user for input which is
64 * normally a passphrase or a confirmation. libpwmd can use this program
65 * either locally (X11 forwarding is not yet supported) or have the pwmd
66 * server use it's pinentry to retrieve a passphrase when needed. How this is
67 * done depends what function gets called and whether the pwmd connection is
68 * over an SSH channel.
70 * There are a few options that tell pinentry how and where to prompt for a
71 * needed passphrase. See the \ref pwmd_option_t section for details. These
72 * options are not sent (when using pwmd's pinentry, not the local one) until
73 * the pinentry is needed.
75 * If using a local pinentry by calling \ref pwmd_open2(), \ref pwmd_save2(),
76 * \ref pwmd_open_async2() or pwmd_save_async2(), libpwmd will send the
77 * command "SET ENABLE_PINENTRY=0" to the pwmd server. This is needed so pwmd
78 * wont try to launch it's own pinentry on passphrase or confirmation failure.
79 * So you may need to reset this option manually depending on your needs;
80 * especially when changing pinentry methods when doing a save (the passphrase
81 * may be set as empty since the remote pinentry is disabled!).
83 * Some pinentry options can also be specified in a local configuration file
84 * \a "~/.pwmd/pinentry.conf". These options are initial values for each
85 * pinentry invokation (not retries) and may be changed by setting the
86 * appropriate \ref pwmd_option_t. Each option and value is separated with a
87 * '=' on a single line. Unrecognized options are ignored. Here are the
88 * recognized options:
90 * \param PATH The full path to the location of the pinentry binary.
91 * \param DISPLAY The X11 display to use.
92 * \param TTYNAME The full path to the tty that pinentry should prompt on.
93 * \param TTYTYPE The terminal type of the tty which is required if DISPLAY is
94 * not set.
96 * \filepath
98 * \note The initial values for the pinentry TTY, TERM and DISPLAY are set
99 * during \ref pwmd_new() depending on the current environment. They may need
100 * to be reset as needed.
102 * \note After establishing an SSH connection, the pwmd pinentry is disabled
103 * by sending the command "SET ENABLE_PINENTRY=0". This is needed because
104 * there currently isn't a way to have the remote pinentry use the local
105 * display. You must be careful to use either a local pinentry or set a
106 * passphrase manually with \ref pwmd_setopt() when a passphrase is required
107 * or needed.
109 * \x11
111 * \see \ref ssh
114 /*! \section Errors
116 * libpwmd uses libgpg-error for all error codes. Some are user defined
117 * GPG_ERR_USER_N codes, but most are reused from the existing ones. Error
118 * codes can be described by using \ref pwmd_strerror(), or the thread-safe
119 * \ref pwmd_strerror_r().
121 * \note libgpg-error normally returns an error code as a bitmask of an error
122 * source and the actual error code. In order to simplify the result codes,
123 * libpwmd strips any error source from the error code before returning it.
126 /*! \file */
127 #ifndef LIBPWMD_H
128 #define LIBPWMD_H
130 #include <gpg-error.h>
131 #include <stdarg.h>
133 #ifdef __cplusplus
134 extern "C" {
135 #endif
137 /*! \def LIBPWMD_VERSION
138 * \brief Version information.
140 * The version of this library.
142 #define LIBPWMD_VERSION 0x@VER_MAJOR@@VER_MINOR@@VER_PATCH@
145 /*! \def LIBPWMD_VERSION_MAJOR
146 * \brief Version information.
148 * The major release number of this library.
150 #define LIBPWMD_VERSION_MAJOR @VER_MAJOR@
153 /*! \def LIBPWMD_VERSION_MINOR
154 * \brief Version information.
156 * The minor release number of this library.
158 #define LIBPWMD_VERSION_MINOR @VER_MINOR@
161 /*! \def LIBPWMD_VERSION_PATCH
162 * \brief Version information.
164 * The patch level of this library.
166 #define LIBPWMD_VERSION_PATCH @VER_PATCH@
169 /*! \def LIBPWMD_VERSION_STR
170 * \brief Version information.
172 * A string representation of the version of this library.
174 #define LIBPWMD_VERSION_STR @VER_STRING@
177 /*! \brief Returns this version of libpwmd.
179 * As a string.
181 LIBPWMD_API const char *pwmd_version();
184 struct pwm_s;
185 /*! \typedef pwm_t
186 * \brief libpwmd handle.
188 * When a handle is mentioned in this documentation it is a pointer of this
189 * type. A new handle is created with \ref pwmd_new().
191 typedef struct pwm_s pwm_t;
194 /*! \typedef pwmd_async_t
195 * \brief Asynchronous return value.
197 * The return code of \ref pwmd_process() which is used for all asynchronous
198 * commands.
200 typedef enum {
201 /*! \internal */
202 ASYNC_INIT,
204 /*! \ref pwmd_process() should be called again. */
205 ASYNC_PROCESS,
207 /*! The command has completed. The result code should be checked for an
208 * error. */
209 ASYNC_DONE,
210 } pwmd_async_t;
213 /*! \typedef pwmd_ip_version_t
214 * \brief IP protocol version for remote SSH connections.
216 * The value of the option \ref PWMD_OPTION_IP_VERSION which is set with \ref
217 * pwmd_setopt().
219 typedef enum {
220 /*! Try both IPv4 and IPv6 addresses. Note that IPv4 is tried first and
221 * that \ref PWMD_IP_ANY only affects a hostname and not an IP address in
222 * the address specification. */
223 PWMD_IP_ANY,
225 /*! Only try IPv4. */
226 PWMD_IPV4,
228 /*! Only try IPv6. */
229 PWMD_IPV6
230 } pwmd_ip_version_t;
233 /*! \def PWMD_FD_READABLE
234 * \brief For use with \ref pwmd_get_fds().
236 * Set when the file descriptor is readable.
238 #define PWMD_FD_READABLE 0x01
241 /*! \def PWMD_FD_WRITABLE
242 * \brief For use with \ref pwmd_get_fds().
244 * Set when the file descriptor is writable.
246 #define PWMD_FD_WRITABLE 0x02
249 /*! For use with \ref pwmd_get_fds(). */
250 typedef struct {
251 /*! The file descriptor. */
252 int fd;
254 /*! A bitmask of \ref PWMD_FD_READABLE and \ref PWMD_FD_WRITABLE. */
255 unsigned flags;
256 } pwmd_fd_t;
259 /*! \typedef pwmd_socket_t
260 * \brief The type of socket a handle is connected to.
262 * For use with \ref pwmd_socket_type().
264 typedef enum {
265 /*! A local domain socket. */
266 PWMD_SOCKET_LOCAL,
268 /*! An SSH connection over a TCP socket. */
269 PWMD_SOCKET_SSH
270 } pwmd_socket_t;
273 /*! \typedef pwmd_pinentry_t
274 * \brief Local pinentry commands and not pwmd pinentry.
276 * For use with \ref pwmd_getpin().
278 typedef enum {
279 /*! When opening a file. */
280 PWMD_PINENTRY_OPEN,
282 /*! When opening a file failed. */
283 PWMD_PINENTRY_OPEN_FAILED,
285 /*! When saving a file. */
286 PWMD_PINENTRY_SAVE,
288 /*! For passphrase confirmation. */
289 PWMD_PINENTRY_SAVE_CONFIRM,
291 /*! For confirmation of a user-defined prompt. */
292 PWMD_PINENTRY_CONFIRM,
294 /*! For the default or user defined string set with \ref
295 * PWMD_OPTION_PINENTRY_DESC. */
296 PWMD_PINENTRY_DEFAULT,
298 /*! To terminate the pinentry process created with \ref pwmd_getpin(). */
299 PWMD_PINENTRY_CLOSE
300 } pwmd_pinentry_t;
303 /*! \typedef pwmd_passphrase_cb_t
304 * \brief Custom passphrase retrieval function.
306 * The value of the option \ref PWMD_OPTION_PASSPHRASE_CB which is set with
307 * \ref pwmd_setopt().
309 * \param user A user data pointer which is set with \ref
310 * PWMD_OPTION_PASSPHRASE_DATA.
311 * \param[out] passphrase The passphrase which may be an empty string or NULL.
312 * It is not modified by libpwmd but must remain allocated for as long as it
313 * is needed.
314 * \return 0 on success or an error code which will cause a command to fail.
316 typedef gpg_error_t (*pwmd_passphrase_cb_t)(void *user, char **passphrase);
319 /*! \typedef pwmd_status_cb_t
320 * \brief Process status messages.
322 * The value of the option \ref PWMD_OPTION_STATUS_CB which is set with \ref
323 * pwmd_setopt().
325 * \param user A user data pointer which is set with \ref
326 * PWMD_OPTION_STATUS_DATA.
327 * \param line The status message line received from the server.
328 * \return 0 on success or an error code which will cause a command to fail.
330 typedef int (*pwmd_status_cb_t)(void *user, const char *line);
333 /*! \typedef pwmd_inquire_cb_t
334 * \brief Send data to the pwmd server.
336 * This is a callback function that gets passed to \ref pwmd_inquire(). It is
337 * used for sending data to the server for commands that need to reply to an
338 * INQUIRE server response (STORE and IMPORT). The reason for this callback is
339 * to let the client send as many bytes as it wants rather than the entire
340 * chunk at once. It gets called during an internal \ref assuan_transact()
341 * from an internal inquire callback function which in turn calls this
342 * function by looping over its return value.
344 * \param user The user data pointer passed to \ref pwmd_inquire().
345 * \param cmd The same as the \a cmd argument to \ref pwmd_inquire().
346 * \param rc The result of the last internal call to \ref assuan_send_data()
347 * which did the sending of the data to the pwmd server. On the first call to
348 * this callback it's value will always be 0 since no data has been sent yet.
349 * \param[out] data The next chunk of data to send or NULL.
350 * \param[out] len The length of \a data or 0.
352 * \retval 0 There is more data to be sent.
353 * \retval GPG_ERR_EOF No need to call this function again, the current
354 * \a line is the last to send.
355 * \retval code Any other error code which will terminate the INQUIRE.
357 * \note The sent data is processed line-per-line. The line is either newline
358 * terminated or is buffered until ASSUAN_LINELENGTH bytes have been
359 * allocated. Any remaining bytes are sent after the INQUIRE has finished.
361 typedef gpg_error_t (*pwmd_inquire_cb_t)(void *user, const char *cmd,
362 gpg_error_t rc, char **data, size_t *len);
364 /*! \typedef pwmd_knownhost_cb_t
365 * \brief Verify a remote SSH connection.
367 * When \ref PWMD_OPTION_KNOWNHOST_CB is set and a the current connections
368 * host key was not found in the known hosts file, then this callback function
369 * can be used to confirm the addition of the new host key to the known_hosts
370 * file.
372 * \param user User data which was set with \ref PWMD_OPTION_KNOWNHOST_DATA.
373 * \param host The hostname as passed to the connecting function.
374 * \param key The raw host key. Note that this differs from the format
375 * returned from \ref pwmd_get_hostkey().
376 * \param len The host key length.
377 * \retval 0 Add the host key to the known hosts file.
378 * \retval GPG_ERR_NOT_CONFIRMED Do not add the host key and abort the
379 * connection.
381 * \note If the known hosts file cannot be modified do to filesystem
382 * restrictions when trying to add the new host key, no error is returned.
383 * Instead the host key is added to the current connections host key cache and
384 * the connection is considered verified.
386 * \see \ref ssh
388 typedef gpg_error_t (*pwmd_knownhost_cb_t)(void *user, const char *host,
389 const char *key, size_t len);
391 /*! \enum pwmd_option_t
392 * \brief libpwmd options.
394 * Options are set with \ref pwmd_setopt().
396 * \filepath
398 typedef enum {
399 /*! A custom passphrase retrieval function which, when set, will be used
400 * instead of \ref pinentry(1). This function will not be used if opening
401 * a file and the passphrase is cached on the server or the file is a new
402 * one. The value of this option should be a \ref pwmd_passphrase_cb_t.
404 * \note An empty string as the passphrase is allowed.
406 PWMD_OPTION_PASSPHRASE_CB,
408 /*! User supplied data which is passed to the custom passphrase function.
409 * */
410 PWMD_OPTION_PASSPHRASE_DATA,
412 /*! A string to use as the passphrase when doing an open or save. When not
413 * NULL, this option has precedence over \ref PWMD_OPTION_PASSPHRASE_CB.
415 * \note An empty string as the passphrase is allowed.
417 PWMD_OPTION_PASSPHRASE,
419 /*! An integer value that specifies the number of tries before \ref
420 * pinentry(1) will give up when opening a file with the wrong supplied
421 * passphrase. The default is 3.
423 * \note This option has no effect when trying to save a file. The user
424 * must either cancel the pinentry causing the save to fail or enter the
425 * correct passphrase during passphrase confirmation.
427 PWMD_OPTION_PINENTRY_TRIES,
429 /*! A character string value which specifies the full path of the \ref
430 * pinentry(1) binary. For the local \ref pinentry(1) method, the default
431 * is specified at compile time.
433 * \see \ref pinentry
435 PWMD_OPTION_PINENTRY_PATH,
437 /*! A value which specifies the full path to the TTY that \ref pinentry(1)
438 * will use to prompt on. When set and no DISPLAY is available, \ref
439 * PWMD_OPTION_PINENTRY_TERM must also be set.
441 * \see \ref pinentry
443 PWMD_OPTION_PINENTRY_TTY,
445 /*! A value which specifies the terminal type (e.g., vt100) that \ref
446 * pinentry(1) will use when no X11 display is available.
448 * \see \ref pinentry
450 PWMD_OPTION_PINENTRY_TERM,
452 /*! A value which specifies the X11 display that \ref pinentry(1) will
453 * use.
455 * \x11
457 * \see \ref pinentry
459 PWMD_OPTION_PINENTRY_DISPLAY,
461 /*! A character string that \ref pinentry(1) will use in it's dialog
462 * window.
464 PWMD_OPTION_PINENTRY_TITLE,
466 /*! \copydoc PWMD_OPTION_PINENTRY_TITLE */
467 PWMD_OPTION_PINENTRY_PROMPT,
469 /*! \copydoc PWMD_OPTION_PINENTRY_TITLE */
470 PWMD_OPTION_PINENTRY_DESC,
472 /*! For \ref pinentry(1) localization. */
473 PWMD_OPTION_PINENTRY_LC_CTYPE,
475 /*! \copydoc PWMD_OPTION_PINENTRY_LC_CTYPE */
476 PWMD_OPTION_PINENTRY_LC_MESSAGES,
478 /*! An integer value that specifies the number of seconds \ref pinentry(1)
479 * will wait for input before timing out and aborting the current command.
480 * If 0, then no timeout will be used. The default is 30.
482 PWMD_OPTION_PINENTRY_TIMEOUT,
484 /*! A function of type \ref pwmd_status_cb_t that will process status
485 * messages received from the pwmd server.
487 PWMD_OPTION_STATUS_CB,
489 /*! A user data pointer which is passed to the status message function. */
490 PWMD_OPTION_STATUS_DATA,
492 /*! The IP version of type \ref pwmd_ip_version_t that \ref
493 * pwmd_ssh_connect() and \ref pwmd_ssh_connect_async() will use when
494 * connecting to the remote pwmd server. The default is \ref PWMD_IP_ANY.
496 * \pre_conn_req
498 PWMD_OPTION_IP_VERSION,
500 /*! A function to confirm an unknown host hash which wasn't found in the
501 * known hosts file.
503 * \see \ref ssh
505 PWMD_OPTION_KNOWNHOST_CB,
507 /*! User supplied data which is passed to the known host function.
509 * \see \ref ssh
511 PWMD_OPTION_KNOWNHOST_DATA,
513 /*! When the total number of bytes to be sent via an INQUIRE is known,
514 * this should be set so XFER status messages can be parsed correctly.
515 * When not known or unset, 0 is used as the total argument to the XFER
516 * status message. This option should be set before each call to
517 * \ref pwmd_inquire().
519 * \note During the INQUIRE, PWMD_OPTION_STATUS_CB is called after every
520 * ASSUAN_LINELENGTH bytes have been successfully transferred.
522 * \note This is a libpwmd feature. pwmd itself does not send XFER status
523 * messages during an INQUIRE. Status messages can be parsed only when
524 * PWMD_OPTION_STATUS_CB is set.
526 PWMD_OPTION_INQUIRE_TOTAL,
527 } pwmd_option_t;
530 /*! \brief Initialize the library.
532 * This function must be the first function called in the library before any
533 * others. It sets up the memory allocators and internationalization among
534 * other things.
536 * \return 0 on success or an error code.
538 LIBPWMD_API gpg_error_t pwmd_init(void);
541 /*! \brief Creates a new handle.
543 * Creates a new handle for use with the other functions.
545 * \param name If not NULL, the name of the application. The application name
546 * is sent to the pwmd server after successfully connecting.
548 * \return a new handle or NULL if there was not enough memory.
550 LIBPWMD_API pwm_t *pwmd_new(const char *name);
553 /*! \brief Connect to a local pwmd server.
555 * Connects to a local unix domain socket.
557 * \param pwm A handle.
558 * \param path The socket path to connect to. If NULL, then a default of
559 * \a "~/.pwmd/socket" will be used.
560 * \return 0 on success or an error code.
561 * \filepath
562 * \see pwmd_ssh_connect(), pwmd_ssh_connect_async(), pwmd_disconnect()
564 LIBPWMD_API gpg_error_t pwmd_connect(pwm_t *pwm, const char *path);
567 /*! \brief Establish a remote connection to a pwmd server.
569 * Connects to a pwmd server over an SSH channel.
571 * \param pwm A handle.
572 * \param host The hostname to connect to or NULL to resume a connection
573 * previously started with \ref pwmd_get_hostkey().
574 * \param port The port or -1 for the default of 22.
575 * \param identity The SSH identity file to use for authentication. This
576 * should specify the private key. The public key is assumed to be \a
577 * identity.pub.
578 * \param user The username on the SSH server to login as. If NULL then
579 * invoking username will be used.
580 * \param known_hosts An OpenSSH known hosts formatted file containing public
581 * SSH server hashes which may be obtained with \ref pwmd_get_hostkey() or via
582 * \ref pwmd_knownhost_cb_t during a connection.
583 * \return 0 on success or an error code.
584 * \filepath
585 * \see pwmd_ssh_connect_async(), \ref PWMD_OPTION_IP_VERSION,
586 * pwmd_disconnect(), \ref ssh
588 LIBPWMD_API gpg_error_t pwmd_ssh_connect(pwm_t *pwm, const char *host,
589 int port, const char *identity, const char *user, const char
590 *known_hosts);
593 /*! \brief Establish a remote connection to a pwmd server (asynchronously).
595 * This is a variant of \ref pwmd_ssh_connect() that will not block while doing
596 * DNS lookups or while connecting.
598 * \process
600 * \param pwm A handle.
601 * \param host The hostname to connect to or NULL to resume a connection
602 * previously started with \ref pwmd_get_hostkey().
603 * \param port The port or -1 for the default of 22.
604 * \param identity The SSH identity file to use for authentication. This
605 * should specify the private key. The public key is assumed to be \a
606 * identity.pub.
607 * \param user The username on the SSH server to login as. If NULL, the
608 * invoking username will be used.
609 * \param known_hosts An OpenSSH known hosts formatted file containing public
610 * SSH server hashes which may be obtained with \ref pwmd_get_hostkey() or via
611 * \ref pwmd_knownhost_cb_t during a connection.
612 * \return 0 on success or an error code.
613 * \filepath
614 * \see pwmd_process(), \ref PWMD_OPTION_IP_VERSION, pwmd_disconnect(),
615 * \ref ssh
617 LIBPWMD_API gpg_error_t pwmd_ssh_connect_async(pwm_t *pwm, const char *host,
618 int port, const char *identity, const char *user, const char
619 *known_hosts);
622 /*! \brief Establish a connection by parsing a URL.
624 * This allows for connecting to a pwmd server by parsing the given URL
625 * string. Whether the connection is to a remote or local server depends on
626 * the contents:
627 * \code
628 * file://[path/to/local/socket]
630 * or
632 * ssh[46]://[username@]hostname[:port],identity,known_hosts
633 * \endcode
635 * The parameters in square brackets are optional and if not specified then
636 * defaults will be used. If neither socket specification is matched, the
637 * \a url is assumed to be a file://.
639 * \param pwm A handle.
640 * \param url The string to parse.
641 * \filepath
642 * \return 0 on success or an error code.
643 * \see \ref pwmd_socket_type(), pwmd_disconnect()
645 LIBPWMD_API gpg_error_t pwmd_connect_url(pwm_t *pwm, const char *url);
648 /*! \brief Establish a connection by parsing a URL (asynchronously).
650 * This allows for connecting to a pwmd server by parsing the given URL
651 * string. Whether the connection is to a remote or local server depends on
652 * the contents:
653 * \code
654 * file://[path/to/local/socket]
656 * or
658 * ssh[46]://[username@]hostname[:port],identity,known_hosts
659 * \endcode
661 * The parameters in square brackets are optional and if not specified then
662 * defaults will be used. If neither socket specification is matched, the
663 * \a url is assumed to be a file://.
665 * \process
667 * \param pwm A handle.
668 * \param url The string to parse.
669 * \filepath
670 * \return 0 on success or an error code.
671 * \see \ref pwmd_socket_type(), pwmd_disconnect()
673 LIBPWMD_API gpg_error_t pwmd_connect_url_async(pwm_t *pwm, const char *url);
676 /*! \brief Retrieve a remote SSH public host key.
678 * This key is needed for host verification of the remote pwmd server. You
679 * should be sure that the remote host is really the host that your wanting to
680 * connect to and not subject to a man-in-the-middle attack.
682 * \param pwm A handle.
683 * \param host The hostname or IP to connect to.
684 * \param port The port or -1 for the default of 22.
685 * \param[out] result An OpenSSH known hosts formatted string containing the
686 * servers public key which should be freed with \ref pwmd_free(). If the
687 * \a host was a hostname then two newline separated known host entries will
688 * be returned; one for the hostname and one for the resolved IP address. The
689 * IP known host entry will always be the second in the string.
690 * \return 0 on success or an error code.
692 * \version 6.0.3
693 * The connection is kept open but not verified after returning. It can be
694 * resumed from one of the SSH connection functions.
696 * \see pwmd_get_hostkey_async(), pwmd_ssh_connect(), \ref ssh
698 LIBPWMD_API gpg_error_t pwmd_get_hostkey(pwm_t *pwm, const char *host, int port,
699 char **result);
702 /*! \brief Retrieve a remote SSH host key (asynchronously).
704 * This key is needed for host verification of the remote pwmd server. You
705 * should be sure that the remote host is really the host that your wanting to
706 * connect to and not subject to a man-in-the-middle attack.
708 * \process
710 * \param pwm A handle.
711 * \param host The hostname or IP to connect to.
712 * \param port The port or -1 for the default of 22.
713 * \return 0 on success or an error code. The result is obtained from \ref
714 * pwmd_process() should be freed with \ref pwmd_free(). It has the same
715 * format as the result from \ref pwmd_get_hostkey().
717 * \version 6.0.3
718 * The connection is kept open but not verified after returning. It can be
719 * resumed from one of the SSH connection functions.
721 * \see pwmd_get_hostkey(), pwmd_ssh_connect_async(), \ref pwmd_process(),
722 * \ref ssh
724 LIBPWMD_API gpg_error_t pwmd_get_hostkey_async(pwm_t *pwm, const char *host,
725 int port);
728 /*! \brief Get the associated file descriptor(s) for a handle.
730 * This function lets the application manually poll the available file
731 * descriptors for the specified handle. It should be called after each
732 * asynchronous function call and after each call to \ref pwmd_process() since
733 * the polled file descriptors may have been closed since the previous call.
735 * After returning, \a n_fds is set to the number of available file
736 * descriptors which are stored in \a fds. The .flags member of \ref pwmd_fd_t
737 * specifies what can be monitored and is a bitmask of \ref PWMD_FD_READABLE
738 * and \ref PWMD_FD_WRITABLE. When ready, \ref pwmd_process() should be
739 * called.
741 * \param pwm A handle.
742 * \param[out] fds Set to the file descriptor(s) of the associated handle.
743 * \param[out] n_fds Initially the size of \a fds then updated to the number
744 * of available file descriptors which are stored in \a fds.
745 * \retval 0 on success or an error code.
746 * \retval GPG_ERR_ERANGE There are more file descriptors than the amount
747 * specified in \a n_fds. \a fds and \a n_fds are still usable though.
748 * \see pwmd_process()
750 LIBPWMD_API gpg_error_t pwmd_get_fds(pwm_t *pwm, pwmd_fd_t *fds, int *n_fds);
753 /*! \brief Check for a unparsed buffered line.
755 * A buffered line is a line that was read from the server but has not yet
756 * been processed. This function determines if there is such a line.
758 * \param pwm A handle.
759 * \retval 0 if there is a pending line.
760 * \retval GPG_ERR_NO_DATA if there is no pending line.
761 * \see pwmd_process()
763 LIBPWMD_API gpg_error_t pwmd_pending_line(pwm_t *pwm);
766 /*! \brief Set handle options.
768 * See \ref pwmd_option_t for option specific details.
770 * \param pwm A handle.
771 * \param opt The option and following value.
772 * \return 0 on success or an error code.
774 LIBPWMD_API gpg_error_t pwmd_setopt(pwm_t *pwm, pwmd_option_t opt, ...);
777 /*! \brief Launch a local pinentry.
779 * Does not send any command to the server. Maybe useful if a passphrase is
780 * needed before opening a file over a remote connection. This passphrase can
781 * then be set with \ref pwmd_setopt().
783 * This function may also be used to display a user confirmation dialog with
784 * pinentry when \a which is \ref PWMD_PINENTRY_CONFIRM. The text to prompt
785 * with is set with \ref PWMD_OPTION_PINENTRY_TITLE.
787 * \param pwm A handle.
788 * \param filename The filename to use in the pinentry dialog strings.
789 * \param[out] result The entered value in the pinentry dialog which should be
790 * freed with \ref pwmd_free().
791 * \param which Determines the default strings shown in the pinentry
792 * dialog. \ref pwmd_setopt() may also be used to override the defaults. In
793 * this case \ref PWMD_PINENTRY_DEFAULT should be used. \ref
794 * PWMD_PINENTRY_CLOSE should be used to terminate the pinentry process when
795 * the pinentry is no longer needed.
797 * \return 0 on success or an error.
799 LIBPWMD_API gpg_error_t pwmd_getpin(pwm_t *pwm, const char *filename,
800 char **result, pwmd_pinentry_t which);
803 /*! \brief Open a file on the pwmd server.
805 * This will send the OPEN command to the server.
807 * \param pwm A handle.
808 * \param filename The filename to open. The \a filename is not a full path
809 * but the data file only.
810 * \return 0 on success or an error code.
811 * \see \ref pinentry
813 LIBPWMD_API gpg_error_t pwmd_open(pwm_t *pwm, const char *filename);
816 /*! \brief Open a file on the pwmd server using a local pinentry.
818 * This will send the OPEN command to the server like \ref pwmd_open() but
819 * will use the local pinentry and not pwmd's pinentry.
821 * \sigalrm
823 * \note This pinentry method is not thread safe. It needs to set a couple of
824 * global variables for the pinentry timeout to work properly.
826 * \param pwm A handle.
827 * \param filename The filename to open. The \a filename is not a full path
828 * but the data file only.
829 * \return 0 on success or an error code.
830 * \retval GPG_ERR_PIN_BLOCKED Another handle is using the local pinentry.
831 * \see \ref pinentry
833 LIBPWMD_API gpg_error_t pwmd_open2(pwm_t *pwm, const char *filename);
836 /*! \brief Open a file on the pwmd server asynchronously (fork method).
838 * This is kind of a hybrid of \ref pwmd_open2() and \ref pwmd_open_async().
839 * It will use the local pinentry asynchronously and also do the OPEN command
840 * asynchronously.
842 * \process
844 * \sigalrm
846 * \param pwm A handle.
847 * \param filename The filename to open. The \a filename is not a full path
848 * but the data file only.
849 * \return 0 on success or an error code.
850 * \see pwmd_process(), \ref pinentry
852 LIBPWMD_API gpg_error_t pwmd_open_async2(pwm_t *pwm, const char *filename);
855 /*! \brief Open a file on the pwmd server (asynchronously).
857 * This will send the OPEN command to the pwmd server. The difference from
858 * \ref pwmd_open() is that it will not block if a pinentry is needed for
859 * passphrase input.
861 * \process
863 * \param pwm A handle.
864 * \param filename The filename to open. The \a filename is not a full path
865 * but the data file only.
866 * \return 0 on success or an error code.
867 * \see pwmd_process(), \ref pinentry
869 LIBPWMD_API gpg_error_t pwmd_open_async(pwm_t *pwm, const char *filename);
872 /*! \brief Process an asynchronous function.
874 * After an asynchronous function has been called and has returned
875 * successfully, this function must be called to process the command and
876 * retrieve the result and return value.
878 * This function may also be called when not in a command to check for pending
879 * status messages sent from the server or to process a pending line.
881 * \param pwm A handle.
882 * \param[out] rc Set to the return code of the original command after
883 * ASYNC_DONE has been returned. This value must be checked to determine if
884 * the command succeeded.
885 * \param[out] result Set to the result of the command when \a rc is 0. Note
886 * that not all commands return a result.
887 * \retval ASYNC_DONE The command has completed. \a rc should be checked to
888 * determine if the command was successful or not.
889 * \retval ASYNC_PROCESS The command is still running and this function should
890 * be called again.
891 * \see pwmd_get_fds(), pwmd_pending_line()
893 LIBPWMD_API pwmd_async_t pwmd_process(pwm_t *pwm, gpg_error_t *rc,
894 char **result);
897 /*! \brief Save a file on the pwmd server.
899 * This will send the SAVE command.
901 * \param pwm A handle.
902 * \return 0 on success or an error code.
903 * \see \ref pinentry
905 LIBPWMD_API gpg_error_t pwmd_save(pwm_t *pwm);
908 /*! \brief Save a file on the pwmd server using the local pinentry.
910 * This will send the SAVE command like \ref pwmd_save() but will use a local
911 * pinentry and not pwmd's pinentry.
913 * \param pwm A handle.
914 * \return 0 on success or an error code.
915 * \see \ref pinentry
917 LIBPWMD_API gpg_error_t pwmd_save2(pwm_t *pwm);
920 /*! \brief Save a file on the pwmd server asynchronously (fork method).
922 * This is kind of a hybrid of \ref pwmd_save2() and \ref pwmd_save_async().
923 * It will use the local pinentry asynchronously and also do the SAVE command
924 * asynchronously.
926 * \process
928 * \param pwm A handle.
929 * \return 0 on success or an error code.
930 * \see pwmd_process(), \ref pinentry
932 LIBPWMD_API gpg_error_t pwmd_save_async2(pwm_t *pwm);
935 /*! \brief Save changes to a file on the pwmd server (asynchronously).
937 * This will send the SAVE command to the pwmd server. The difference from
938 * \ref pwmd_save() is that it will not block if a pinentry is needed for
939 * passphrase input.
941 * \process
943 * \param pwm A handle.
944 * \return 0 on success or an error code.
945 * \see pwmd_process(), \ref pinentry
947 LIBPWMD_API gpg_error_t pwmd_save_async(pwm_t *pwm);
950 /*! \brief Send a command to the pwmd server.
952 * Sends a command to the pwmd server. You should avoid sending the BYE
953 * command here because the assuan context will be freed and bad things will
954 * happen. Use \ref pwmd_close() instead. For commands that use an INQUIRE to
955 * send data to the server (STORE and IMPORT), \ref pwmd_inquire() must be
956 * used and not this function.
958 * \param pwm A handle.
959 * \param[out] result The result of the command when successful which must be
960 * freed with \ref pwmd_free().
961 * \param cmd The command to send and any following arguments.
962 * \return 0 on success or an error code.
964 * \note Not all commands return a result.
966 LIBPWMD_API gpg_error_t pwmd_command(pwm_t *pwm, char **result,
967 const char *cmd, ...);
970 /*! \brief Send a command to the pwmd server.
972 * Like \ref pwmd_command() but uses an argument pointer instead.
974 * \param pwm A handle.
975 * \param[out] result The result of the command when successful which must be
976 * freed with \ref pwmd_free().
977 * \param cmd The command to send.
978 * \param ap The arguments to \a cmd.
979 * \return 0 on success or an error code.
981 * \note Not all commands return a result.
983 LIBPWMD_API gpg_error_t pwmd_command_ap(pwm_t *pwm, char **result,
984 const char *cmd, va_list ap);
987 /*! \brief Send data to a pwmd server.
989 * This lets commands that use an INQUIRE (STORE and IMPORT) send the data
990 * to the server. Use this function rather than \ref pwmd_command() for these
991 * pwmd commands.
993 * \param pwm A handle.
994 * \param cmd The command (without arguments) to send that uses an INQUIRE.
995 * \param func A callback function of type \ref pwmd_inquire_cb_t which sets
996 * the data to be sent.
997 * \param user A user data pointer passed to the callback function \a func.
998 * \return 0 on success or an error code.
1000 * \see pwmd_inquire_cb_t
1002 LIBPWMD_API gpg_error_t pwmd_inquire(pwm_t *pwm, const char *cmd,
1003 pwmd_inquire_cb_t func, void *user);
1006 /*! \brief Close a connection to the pwmd server.
1008 * This will close the connection but keep any previously set options for the
1009 * specified handle.
1011 * \param pwm A handle.
1012 * \return 0 on success or an error code.
1013 * \see \ref pwmd_close()
1015 LIBPWMD_API gpg_error_t pwmd_disconnect(pwm_t *pwm);
1018 /*! \brief Close a handle.
1020 * This will close the connection to a pwmd server and free any resources
1021 * associated with it.
1023 * \param pwm A handle.
1024 * \return Nothing.
1025 * \see \ref pwmd_disconnect(), \ref pwmd_new()
1027 LIBPWMD_API void pwmd_close(pwm_t *pwm);
1030 /*! \brief The type of connection a handle has.
1032 * Useful when you want to know what kind of connection a handle has.
1034 * \param pwm A handle.
1035 * \param[out] type The type of socket.
1036 * \return 0 on success or an error code.
1037 * \see pwmd_connect_url()
1039 LIBPWMD_API gpg_error_t pwmd_socket_type(pwm_t *pwm, pwmd_socket_t *type);
1042 /*! \brief Free a previously allocated pointer.
1044 * Use this function to free resources allocated by the other libpwmd memory
1045 * functions. Do not use it to free allocations made by other allocators.
1047 * The difference between the standard free() and this function is that
1048 * this one will zero out the contents of the pointer before freeing it.
1050 * \param ptr The pointer to deallocate.
1051 * \return Nothing.
1052 * \see pwmd_malloc(), pwmd_calloc(), pwmd_realloc(), pwmd_strdup(),
1053 * pwmd_process(), pwmd_command()
1055 LIBPWMD_API void pwmd_free(void *ptr);
1058 /*! \brief A wrapper around malloc.
1060 * Like malloc(), but lets libpwmd keep track of the pointer.
1062 * \param size The number of bytes to allocate.
1063 * \return A newly allocated pointer or NULL if there wasn't enough memory.
1064 * \see malloc(3), pwmd_free()
1066 LIBPWMD_API void *pwmd_malloc(size_t size);
1069 /*! \brief A wrapper around calloc().
1071 * Like calloc(), but lets libpwmd keep track of the pointer.
1073 * \param nmemb The number of elements to allocate.
1074 * \param size The number of bytes to allocate.
1075 * \return A newly allocated pointer or NULL if there wasn't enough memory.
1076 * \see calloc(3), pwmd_free()
1078 LIBPWMD_API void *pwmd_calloc(size_t nmemb, size_t size);
1081 /*! \brief A wrapper around realloc().
1083 * Like realloc(), but lets libpwmd keep track of the pointer.
1085 * \note This function will try and allocate the entire \a size before freeing
1086 * the original pointer and returning the new one.
1088 * \param ptr The pointer to reallocate.
1089 * \param size The new number of bytes to allocate.
1090 * \return A newly allocated pointer or NULL if there wasn't enough memory.
1091 * \see realloc(3), pwmd_free()
1093 LIBPWMD_API void *pwmd_realloc(void *ptr, size_t size);
1096 /*! \brief A wrapper around strdup().
1098 * Like strdup(), but lets libpwmd keep track of the pointer.
1100 * \param str The string to duplicate.
1101 * \return A newly allocated character pointer or NULL if there wasn't
1102 * enough memory.
1103 * \see strdup(3), pwmd_free()
1105 LIBPWMD_API char *pwmd_strdup(const char *str);
1108 /*! \brief Duplicate a formatted string.
1110 * Like \ref sprintf(3) but returns an allocated string.
1112 * \param fmt The formatted string and any following arguments.
1113 * \return A newly allocated character pointer or NULL if there wasn't
1114 * enough memory.
1115 * \see pwmd_free()
1117 LIBPWMD_API char *pwmd_strdup_printf(const char *fmt, ...);
1120 /*! \def EPWMD_NO_FILE
1121 * \hideinitializer
1122 * \brief No data file has been opened.
1124 * Some commands don't require an open data file but most do.
1126 #define EPWMD_NO_FILE GPG_ERR_USER_1
1129 /*! \def EPWMD_LIBXML_ERROR
1130 * \hideinitializer
1131 * \brief libxml2 error.
1133 * This can be a memory allocation error or a parse error. The details of the
1134 * error cannot be obtained with libpwmd. You'd have to connect to the pwmd
1135 * socket and do the command directly to get the actual error.
1137 #define EPWMD_LIBXML_ERROR GPG_ERR_USER_2
1140 /*! \def EPWMD_FILE_MODIFIED
1141 * \hideinitializer
1142 * \brief The data file has been modified.
1144 * Rather than process the next command this error is returned to prevent
1145 * overwriting new data which may have been saved by another client.
1147 #define EPWMD_FILE_MODIFIED GPG_ERR_USER_3
1150 /*! \def EPWMD_MAX
1151 * \hideinitializer
1152 * \if cond1
1153 * \internal
1154 * \endif
1155 * \brief libgpg-error error code offset.
1157 * If you use your own libgpg-error codes then this should be the base of
1158 * them.
1160 #define EPWMD_MAX GPG_ERR_USER_4
1163 /*! \brief Return a description of an error code.
1165 * \param code The error code to describe.
1166 * \return A character description of the error code.
1167 * \see pwmd_strerror_r()
1169 LIBPWMD_API const char *pwmd_strerror(gpg_error_t code);
1172 /*! \brief Return a description of an error code (thread-safe).
1174 * This is a thread-safe version of \ref pwmd_strerror().
1176 * \param code The error code to describe.
1177 * \param[out] buf An allocated buffer to hold the error description.
1178 * \param size The size of the allocated buffer \a buf.
1180 * \retval 0 Success.
1181 * \retval ERANGE \a size was not large enough to hold the entire description
1182 * and \a buf is set to the truncated error string.
1184 LIBPWMD_API int pwmd_strerror_r(gpg_error_t code, char *buf, size_t size);
1186 #ifdef __cplusplus
1188 #endif
1190 #endif