1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2015, 2016
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/>.
21 /*! \headerfile libpwmd.h
23 * libpwmd is a library making it easy for applications to use the pwmd server.
24 * Pwmd version 3.0 or later is required; either locally or remotely.
27 /*! \section threads Threads
29 * \ref libpwmd should be thread-safe on a per handle bases. Meaning that only
30 * one thread should access a \ref pwm_t handle at a time.
33 /*! \section remote Remote Connection Details
35 * There are two methods of connecting to a remote pwmd server: over SSH and
38 * Connections over SSH are done by creating an SSH channel to spawn a shell
39 * that executes a proxy server to connect to the pwmd local Unix Domain
40 * Socket. Authentication is done by using SSH public key (see \ref
41 * ssh-keygen(1)) authentication and verifying the host key against a local
42 * OpenSSH known hosts formatted file. A passphrase protected private SSH key
43 * is supported when \ref PWMD_OPTION_SSH_NEEDS_PASSPHRASE is set.
45 * An unknown servers public key can be added to a known hosts file after user
46 * confirmation by setting the callback function \ref pwmd_knownhost_cb_t
47 * before connecting to the unknown host.
49 * On the server side you'll need a proxy server to connect to the pwmd server.
50 * Below is an example of an \ref authorized_keys(5) entry that will do this.
51 * The \a public_key portion should be the same as the contents of the \ref
52 * ssh-keygen(1) \a identity.pub file generated on the client machine. The
53 * private key should be passed as the \a identity parameter to \ref
57 * command="socat UNIX-CONNECT:$HOME/.pwmd/socket -" <public_key> ...
60 * Pwmd itself can accept TLS connections so no proxy program is needed as is
61 * when using SSH. Like SSH connections, TLS connections are created with \ref
62 * pwmd_connect(). You will need to generate a client key and X509 certificate
63 * and then sign it with the same certificate authority (CA) that the pwmd
64 * server certificate was signed with.
66 * Certificates are similar to SSH public and private keys but a little harder
67 * to setup correctly. See the \ref certtool(1) (recommended) and \ref
68 * openssl(1) manual pages for details.
71 /*! \section pinentry Pinentry Details
73 * \ref pinentry(1) is a program that prompts the user for input which is
74 * normally a passphrase or a confirmation. libpwmd can use this program either
75 * locally (X11 forwarding is not yet supported) or have the pwmd server (read:
76 * \ref gpg-agent(1)) call pinentry to retrieve a passphrase when needed.
78 * There are a few options that tell pinentry how and where to prompt for a
79 * needed passphrase. See the \ref pwmd_option_t section for details. These
80 * options are sent to pwmd during \ref pwmd_open(), \ref pwmd_save() and \ref
81 * pwmd_passwd() only when pinentry use has not been disabled (\see \ref
82 * PWMD_OPTION_NO_PINENTRY) and when not over a remote connection.
84 * Some pinentry options may also be specified in the local configuration file
85 * \a "~/.pwmd/pinentry.conf". These options are initial values for each
86 * pinentry invocation (not retries) and may be changed by setting the
87 * appropriate \ref pwmd_option_t. Options and values are separated with a '='
88 * on a single line. Unrecognized options are ignored. Here are the recognized
91 * \param PATH The full path to the location of the pinentry binary. Only
92 * useful for the local pinentry.
93 * \param DISPLAY The X11 display to use.
94 * \param TTYNAME The full path to the tty that pinentry should prompt on.
95 * \param TTYTYPE The terminal type of the tty which is required if DISPLAY is
97 * \param LC_MESSAGES Adjust for the current locale.
98 * \param LC_CTYPE Adjust for the current locale.
102 * \note The initial values for the pinentry TTY, TERM and DISPLAY are set
103 * during \ref pwmd_new() depending on the current environment. They are then
104 * updated to the values specified in \a ~/.pwmd/pinentry.conf. They can then
105 * be reset or changed as needed by using \ref pwmd_setopt().
107 * \note After establishing a remote connection, pwmd's pinentry is disabled by
108 * sending the pwmd option \a "disable-pinentry" during \ref pwmd_open(). A
109 * local pinentry will be used instead.
116 * libpwmd uses libgpg-error for all error codes. Error codes can be described
117 * by using \ref gpg_strerror(), or the thread-safe \ref gpg_strerror_r().
119 * \note libgpg-error returns an error code as a bitmask of an error source and
120 * the error code. Determining the error source can help you determine where
121 * the error occurred, be it from \ref pinentry(1), \ref gpg-agent(1), \ref
122 * libgcrypt, \ref pwmd(1) or \ref libpwmd(3). pwmd uses the \ref
123 * GPG_ERR_SOURCE_USER_1 error source and libpwmd uses \ref
124 * GPG_ERR_SOURCE_USER_2. To view the error source and description of an error
125 * code the \ref gpg-error(1) command line utility may be of use. Also see the
126 * \ref libgpg-error documentation for error code manipulation.
137 #ifdef GPG_ERR_SOURCE_DEFAULT
138 #error "GPG_ERR_SOURCE_DEFAULT already defined."
140 #define GPG_ERR_SOURCE_DEFAULT GPG_ERR_SOURCE_USER_2
142 #include <gpg-error.h>
150 /*! \def LIBPWMD_VERSION
151 * \brief Version information.
153 * The version of this library.
155 #define LIBPWMD_VERSION 0x@VER_MAJOR_HEX@@VER_MINOR_HEX@@VER_PATCH_HEX@
158 /*! \def LIBPWMD_VERSION_MAJOR
159 * \brief Version information.
161 * The major release number of this library.
163 #define LIBPWMD_VERSION_MAJOR @VER_MAJOR@
166 /*! \def LIBPWMD_VERSION_MINOR
167 * \brief Version information.
169 * The minor release number of this library.
171 #define LIBPWMD_VERSION_MINOR @VER_MINOR@
174 /*! \def LIBPWMD_VERSION_PATCH
175 * \brief Version information.
177 * The patch level of this library.
179 #define LIBPWMD_VERSION_PATCH @VER_PATCH@
182 /*! \def LIBPWMD_VERSION_STR
183 * \brief Version information.
185 * A string representation of the version of this library.
187 #define LIBPWMD_VERSION_STR @VER_STRING@
190 /*! \brief Returns this version of libpwmd.
195 LIBPWMD_API
const char *pwmd_version ();
200 * \brief A libpwmd handle.
202 * When a pwmd handle or context is mentioned in this documentation it
203 * is a pointer of this type. A new handle is created with \ref
206 typedef struct pwm_s pwm_t
;
209 /*! \typedef pwmd_socket_t
210 * \brief The type of socket a handle is connected to.
212 * For use with \ref pwmd_socket_type().
216 /*! A local UNIX domain socket. */
219 /*! An SSH channel over a TCP socket. */
222 /*! A TLS connection over a TCP socket. */
227 /*! \typedef pwmd_pinentry_t
228 * \brief Local pinentry commands and not pwmd pinentry.
230 * These determine what prompt a local pinentry uses. For use with \ref
235 /*! When opening a file. */
238 /*! When opening a file failed due to a bad passphrase. */
239 PWMD_PINENTRY_OPEN_FAILED
,
241 /*! When saving a file. */
244 /*! For passphrase confirmation. */
245 PWMD_PINENTRY_SAVE_CONFIRM
,
247 /*! When saving a file and passphrase confirmation failed. */
248 PWMD_PINENTRY_SAVE_FAILED
,
250 /*! For confirmation of a user-defined prompt. */
251 PWMD_PINENTRY_CONFIRM
,
253 /*! For the default or user defined string set with \ref
254 * PWMD_OPTION_PINENTRY_DESC. */
257 /*! To terminate the pinentry process created with \ref pwmd_getpin(). */
262 /*! \typedef pwmd_status_cb_t
263 * \brief Process status messages.
265 * The value of the option \ref PWMD_OPTION_STATUS_CB which is set with \ref
268 * \param user A user data pointer which is set with \ref
269 * PWMD_OPTION_STATUS_DATA.
270 * \param line The status message line received from the server.
271 * \return 0 on success or an error code which will cause a command to fail.
273 typedef gpg_error_t (*pwmd_status_cb_t
) (void *user
, const char *line
);
276 /*! \typedef pwmd_inquire_cb_t
277 * \brief Send data to the pwmd server.
279 * This is a callback function that is used for sending data to the server for
280 * commands that need to reply to an INQUIRE server response. The reason for
281 * this callback is to let the client send as many bytes as it wants rather
282 * than the entire data at once. It gets called during an internal \ref
283 * assuan_transact() from an internal inquire callback function which in turn
284 * calls this function by looping over its return value.
286 * \param user The user data pointer passed to the libpwmd function specifying
288 * \param keyword The name of this inquire. Could be a command name or some
289 * keyword describing what needs to be sent. See the pwmd and \ref
290 * gpg-agent(1) documentation for details.
291 * \param rc The result of the last internal call to \ref assuan_send_data()
292 * which did the sending of the data to the pwmd server. On the first call to
293 * this callback it's value will always be 0 since no data has been sent yet.
294 * This should be checked during each call to this function and should return
295 * the same error code when set. Its purpose is to let the client clean up
296 * before letting the command fail.
297 * \param[out] data The next chunk of data to send or NULL.
298 * \param[out] len The length of \a data or 0.
300 * \retval 0 There is more data to be sent.
301 * \retval GPG_ERR_EOF No need to call this function again, the current
302 * \a data is the last to send.
303 * \retval code Any other error code which will cancel the INQUIRE.
305 * \note The sent data is processed line-per-line. The line is either newline
306 * terminated or is buffered until ASSUAN_LINELENGTH bytes have been
307 * allocated. Any remaining bytes are sent after the INQUIRE has finished.
309 typedef gpg_error_t (*pwmd_inquire_cb_t
) (void *user
, const char *keyword
,
310 gpg_error_t rc
, char **data
,
314 /*! \typedef pwmd_knownhost_cb_t
315 * \brief Verify a remote SSH connection.
317 * When \ref PWMD_OPTION_KNOWNHOST_CB is set and the current connections host
318 * key was not found in the known hosts file, then this callback function can
319 * be used to confirm the addition of the new host key to the known_hosts file.
321 * \param user User data which was set with \ref PWMD_OPTION_KNOWNHOST_DATA.
322 * \param host The hostname as passed to \ref pwmd_connect().
323 * \param hostkey The raw binary data of the host key.
324 * \param len The length of \a hostkey.
325 * \retval 0 Add the host key to the known hosts file.
326 * \retval GPG_ERR_NOT_CONFIRMED Do not add the host key and abort the
327 * connection. This is the recommended error code although any other non-zero
328 * return value will also abort the connection.
330 * \note If the known hosts file cannot be modified do to filesystem
331 * restrictions when adding the new host key, no error is returned. Instead,
332 * the host key is added to the current connections host key cache and the
333 * connection is accepted.
337 typedef gpg_error_t (*pwmd_knownhost_cb_t
) (void *user
, const char *host
,
342 /*! \enum pwmd_option_t
343 * \brief libpwmd options.
345 * Options are set with \ref pwmd_setopt(). Some options must be set before a
346 * connection is made to have any effect.
350 /* Note to self: dont change the order or insert. Only append. */
353 /*! A string value which specifies the full path of the \ref pinentry(1)
354 * binary. The default is specified at compile time.
356 * \note This only affects the local pinentry.
359 PWMD_OPTION_PINENTRY_PATH
,
362 /*! A string value which specifies the full path to the TTY that \ref
363 * pinentry(1) will use to prompt on. When set and no DISPLAY is
364 * available, \ref PWMD_OPTION_PINENTRY_TERM must also be set.
368 PWMD_OPTION_PINENTRY_TTY
,
371 /*! A string value which specifies the terminal type (i.e., vt100) that
372 * \ref pinentry(1) will use when no X11 display is available.
376 PWMD_OPTION_PINENTRY_TERM
,
379 /*! A string value which specifies the X11 display that \ref pinentry(1)
380 * will use. \ref pinentry(1) seems to make DISPLAY have a higher priority
381 * than \ref PWMD_OPTION_PINENTRY_TTY.
385 PWMD_OPTION_PINENTRY_DISPLAY
,
388 /*! A character string that \ref pinentry(1) will use in it's dialog
391 PWMD_OPTION_PINENTRY_ERROR
,
394 /*! \copydoc PWMD_OPTION_PINENTRY_ERROR */
395 PWMD_OPTION_PINENTRY_PROMPT
,
398 /*! \copydoc PWMD_OPTION_PINENTRY_ERROR */
399 PWMD_OPTION_PINENTRY_DESC
,
402 /*! For \ref pinentry(1) localization. */
403 PWMD_OPTION_PINENTRY_LC_CTYPE
,
406 /*! \copydoc PWMD_OPTION_PINENTRY_LC_CTYPE */
407 PWMD_OPTION_PINENTRY_LC_MESSAGES
,
410 /*! An integer value that specifies the number of seconds \ref pinentry(1)
411 * will wait for input before timing out and aborting the current command.
412 * If 0, then no timeout will be used. The default is 30.
414 * \note This only affects the local pinentry.
416 PWMD_OPTION_PINENTRY_TIMEOUT
,
419 /*! An integer value that specifies the number of times to prompt for a
420 * passphrase before returning an error.
422 * \note This only affects the local pinentry.
424 PWMD_OPTION_PINENTRY_TRIES
,
427 /*! A function of type \ref pwmd_status_cb_t that will process status
428 * messages received from the pwmd server.
430 PWMD_OPTION_STATUS_CB
,
433 /*! A user data pointer which is passed to the status message function
434 * \ref PWMD_OPTION_STATUS_CB. */
435 PWMD_OPTION_STATUS_DATA
,
438 /*! A function of type \ref pwmd_knownhost_cb_t that will be used to
439 * confirm a host key that was not found in the known hosts file.
443 PWMD_OPTION_KNOWNHOST_CB
,
446 /*! User supplied data which is passed to the known host function \ref
447 * PWMD_OPTION_KNOWNHOST_CB.
451 PWMD_OPTION_KNOWNHOST_DATA
,
454 /*! When the total number of bytes to be sent via an INQUIRE is known,
455 * this should be set so XFER status messages can be parsed correctly.
456 * When not known or unset, 0 is used as the total argument to the XFER
457 * status message. This option should be reset in every function that
458 * uses an \ref pwmd_inquire_cb_t.
460 * \note During the INQUIRE, \ref PWMD_OPTION_STATUS_CB is called, when
461 * set, after every iteration of the \ref pwmd_inquire_cb_t.
463 * \note This is a libpwmd feature. pwmd itself does not send XFER status
464 * messages during an INQUIRE. Status messages can be parsed only when
465 * \ref PWMD_OPTION_STATUS_CB is set.
467 PWMD_OPTION_INQUIRE_TOTAL
,
470 /*! When set to 1, lock the file mutex during \ref pwmd_open() as if the LOCK
471 * command had been sent.
473 PWMD_OPTION_LOCK_ON_OPEN
,
476 /*! Use ssh-agent to retrieve the private key to authenticate with when
477 * connecting to a remote pwmd server. */
478 PWMD_OPTION_SSH_AGENT
,
481 /*! When 1, disable pinentry use. This will prevent both pwmd and libpwmd
482 * from using a pinentry program and will prompt from the terminal if
485 * \note This must be set before calling \ref pwmd_open().
486 * \see \ref PWMD_OPTION_OVERRIDE_INQUIRE, \ref PWMD_OPTION_LOCAL_PINENTRY
488 PWMD_OPTION_NO_PINENTRY
,
491 /*! When 1, libpwmd will disable pwmd's pinentry and use it's own
492 * pinentry. This option is set by default when the connection is a remote
495 * \note This must be set before calling \ref pwmd_open().
497 PWMD_OPTION_LOCAL_PINENTRY
,
500 /*! When set, override libpwmd's internal handling of server inquires with
501 * the PASSPHRASE, NEW_PASSPHRASE and SIGN_PASSPHRASE keywords. Handling of
502 * these keywords is done automatically when \ref PWMD_OPTION_NO_PINENTRY or
503 * \ref PWMD_OPTION_LOCAL_PINENTRY is set or when the connection is a remote
506 * \see \ref remote, \ref PWMD_OPTION_LOCAL_PINENTRY, \ref pwmd_password()
508 PWMD_OPTION_OVERRIDE_INQUIRE
,
511 /*! An int specifying a timeout in seconds before a TCP connection or
512 * data transfer function will timeout causing a connection or command to
513 * fail. This option can be specified both before and after a connection
514 * has been established. When 0, no timeout is used.
516 PWMD_OPTION_SOCKET_TIMEOUT
,
519 /*! An int specifying whether to enable TLS hostname verification against the
520 * server certificate chain. Default is 0 or disabled. */
521 PWMD_OPTION_TLS_VERIFY
,
524 /*! Set to 1 to not modify the signal handler to ignore SIGPIPE. The default
525 * is 0, or to ignore this signal. */
529 /*! Get the version of the pwmd server as an unsigned integer. This is a
530 * read-only setting and is only available after connecting. */
531 PWMD_OPTION_SERVER_VERSION
,
534 /*! The SSH identity file requires a passphrase. When set, prompt for the
535 * passphrase after connecting. Note that only libssh2 built with OpenSSL
536 * supports this feature as of libssh2-1.7.0. */
537 PWMD_OPTION_SSH_NEEDS_PASSPHRASE
,
540 /*! Set SSH private identity passphrase. When set there is no need to set
541 * \ref PWMD_OPTION_SSH_NEEDS_PASSPHRASE. The value of this option is
542 * duplicated then freed after the call to \ref pwmd_connect(). Note that
543 * only libssh2 built with OpenSSL supports this feature as of
545 PWMD_OPTION_SSH_PASSPHRASE
,
549 /*! \def PWMD_FEATURE_PINENTRY
551 * \brief Pinentry support.
553 * This is for a local or fork()'ed pinentry.
555 #define PWMD_FEATURE_PINENTRY 0x0001
557 /*! \def PWMD_FEATURE_QUALITY
559 * \brief Password quality checking.
561 * A password quality meter is shown when the pinentry supports it.
563 #define PWMD_FEATURE_QUALITY 0x0002
565 /*! \def PWMD_FEATURE_SSH
567 * \brief Remote connections over an SSH channel.
571 #define PWMD_FEATURE_SSH 0x0004
573 /*! \def PWMD_FEATURE_GNUTLS
575 * \brief Remote connections over TLS.
579 #define PWMD_FEATURE_GNUTLS 0x0008
582 /*! \brief libpwmd compile time features.
584 * Useful for clients to determine what features are compiled into libpwmd at
587 * \return A bitmask of features.
589 LIBPWMD_API
unsigned int pwmd_features ();
592 /*! \brief Initialize the library.
594 * This function must be the first function called in the library before any
595 * others. It sets up the memory allocators and internationalization among
598 * \return 0 on success or an error code.
600 LIBPWMD_API gpg_error_t
pwmd_init (void);
602 /*! \brief Deinitialize the library.
604 * This function is mainly for cleaning up other libraries that
605 * libpwmd links with to prevent memory and other leaks showing up in
606 * a debugger. It should be the final libpwmd function call before
609 LIBPWMD_API
void pwmd_deinit (void);
612 /*! \brief Creates a new handle.
614 * Creates a new handle or context for use with the other functions.
616 * \param name If not NULL, the name of the application. The application name
617 * is sent to the pwmd server after successfully connecting and is used in
619 * \param[out] pwm A new handle for use with the other functions.
621 * \return 0 on success or an error code.
623 LIBPWMD_API gpg_error_t
pwmd_new (const char *name
, pwm_t
**pwm
);
626 /*! \brief Set user data for a handle.
628 * Use this function to associate user data with a handle that can later be
629 * retrieved with \ref pwmd_get_pointer().
631 * \param pwm A handle.
632 * \param data A pointer to the user data.
635 LIBPWMD_API
void pwmd_set_pointer (pwm_t
*pwm
, void *data
);
638 /*! \brief Get user data for a handle.
640 * Return the user data pointer previously set with \ref pwmd_set_pointer().
642 * \param pwm A handle.
643 * \returns A pointer to the user data.
645 LIBPWMD_API
void *pwmd_get_pointer (pwm_t
*pwm
);
648 /*! \brief Cancel an operation.
650 * This is limited in functionality as it will only cancel an initiating
651 * connection to the pwmd server. Command cancellation is not yet supported.
653 * \param pwm A handle.
654 * \return 0 on success or an error code.
656 LIBPWMD_API gpg_error_t
pwmd_cancel (pwm_t
*pwm
);
659 /*! \brief Establish a connection to a pwmd server.
661 * Connects to the pwmd server specified in \a url. The format of \a url is:
663 * file:///path/to/socket, or
665 * ssh[46]://[username@]hostname[:port], or
667 * tls[46]://hostname[:port]
669 * If \a url is NULL then the default local pwmd socket \a ~/.pwmd/socket
672 * The remaining arguments are parameters for the \a url.
674 * For SSH connections, the first of the positional parameters should be the
675 * identity file to use and is required to be non-NULL unless \ref
676 * PWMD_OPTION_SSH_AGENT is set. The final parameter is the known hosts file
677 * to use or NULL to use a default of \a ~/.ssh/knownhosts.
679 * For TLS connections, the first positional parameter should be the client
680 * certificate filename. The second parameter should be the client
681 * certificate key filename. The third parameter should be the Certificate
682 * Authority (CA) file that was used to sign the server certificate. The
683 * fourth parameter should be a TLS cipher priority string or NULL to use the
684 * default of "SECURE256:SECURE192:SECURE128:-VERS-SSL3.0". The final
685 * parameter is an SHA-256 hash of the server fingerprint to verify against or
688 * For local connections, any remaining parameters are ignored.
690 * \param pwm A handle.
691 * \param url The socket to connect to.
692 * \param ... Remaining parameters for the \a url.
693 * \return 0 on success or an error code.
695 * \see \ref PWMD_OPTION_SSH_AGENT, \ref PWMD_OPTION_SOCKET_TIMEOUT,
696 * pwmd_socket_type(), pwmd_disconnect(), \ref remote
698 LIBPWMD_API gpg_error_t
pwmd_connect (pwm_t
*pwm
, const char *url
, ...);
700 /*! \brief Get the error code of a failed GnuTLS function.
702 * When an TLS error occurs while connecting or during a command, this function
703 * can be used to get the error code and error description.
705 * \param pwm A handle.
706 * \param[out] str The textual representation of the gnutls error code.
707 * \return 0 on success or a GnuTLS error code.
709 LIBPWMD_API
int pwmd_gnutls_error (pwm_t
*pwm
, const char **str
);
711 /*! \brief Set handle options.
713 * See \ref pwmd_option_t for option specific details.
715 * \param pwm A handle.
716 * \param opt The option.
717 * \param ... The option value.
718 * \return 0 on success or an error code.
720 LIBPWMD_API gpg_error_t
pwmd_setopt (pwm_t
*pwm
, int opt
, ...);
723 /*! \brief Get the value for a handle option.
725 * Retrieves the default or previously set value for a handle option. See
726 * \ref pwmd_option_t for option specific details.
728 * \param pwm A handle.
729 * \param opt The option.
730 * \param ... A pointer to the option value type to store the value.
731 * \return 0 on success or an error code.
732 * \note The value is returned as a pointer and not duplicated.
734 LIBPWMD_API gpg_error_t
pwmd_getopt (pwm_t
*pwm
, int opt
, ...);
737 /*! \brief Launch a local pinentry.
739 * Does not send any command to the pwmd server. This maybe useful if a
740 * passphrase is needed while opening a file over a remote connection and
741 * during an \ref pwmd_open() server inquire.
743 * This function may also be used to display a user confirmation dialog with
744 * pinentry when \a which is \ref PWMD_PINENTRY_CONFIRM. The text show in the
745 * pinentry is set with \ref pwmd_setopt().
747 * \param pwm A handle.
748 * \param filename The filename to use in the pinentry dialog strings when
749 * using the default pinentry strings.
750 * \param[out] result The entered value in the pinentry dialog which should be
751 * freed with \ref pwmd_free().
752 * \param[out] len The length of \a result.
753 * \param which Determines the default strings shown in the pinentry
754 * dialog. \ref pwmd_setopt() may also be used to override the defaults. In
755 * this case \ref PWMD_PINENTRY_USER should be used. \ref PWMD_PINENTRY_CLOSE
756 * should be used to terminate the pinentry process when the pinentry is no
759 * \return 0 on success or an error.
761 * \see pwmd_password()
763 LIBPWMD_API gpg_error_t
pwmd_getpin (pwm_t
*pwm
, const char *filename
,
764 char **result
, size_t *len
,
765 pwmd_pinentry_t which
);
767 /*! \brief Obtain a passphrase from a local pinentry.
769 * This is the same function that libpwmd uses during an inquire when using
770 * the local pinentry and the inquire keyword is one of PASSPHRASE,
771 * NEW_PASSPHRASE or SIGN_PASSPHRASE. Provided for convenience since it sets
772 * proper pinentry strings and handles new passphrase confirmation.
774 * \param pwm A handle.
775 * \param keyword The keyword to determine pinentry strings. Usually one of
776 * PASSPHRASE, NEW_PASSPHRASE or SIGN_PASSPHRASE as sent by pwmd.
777 * \param[out] result The obtained passphrase which should be freed with \ref
779 * \param[out] size The length of \a result.
781 * \return 0 on success or an error.
783 LIBPWMD_API gpg_error_t
pwmd_password (pwm_t
*pwm
, const char *keyword
,
784 char **result
, size_t *size
);
786 /*! \brief Test the quality of a passphrase.
788 * When built with passphrase quality checking, you can test the amount of
789 * entropy a passphrase contains with this function. It simply wraps around the
790 * zxcvbn-c quality checking function.
792 * \param passphrase The passphrase to test.
793 * \param[out] result The entropy of the passphrase in bits.
795 * \return 0 on success or an error code.
797 LIBPWMD_API gpg_error_t
pwmd_test_quality (const char *passphrase
,
800 /*! \brief Open a file on the pwmd server.
802 * This will send the OPEN command to the server.
804 * To make use of \a callback, \ref PWMD_OPTION_OVERRIDE_INQUIRE must also be
807 * \param pwm A handle.
808 * \param filename The filename to open. The \a filename is not a full path
809 * but the data filename only.
810 * \param callback A callback function to invoke when pwmd inquires data from
812 * \param data User data passed to the \a callback function.
813 * \return 0 on success or an error code.
814 * \see \ref PWMD_OPTION_OVERRIDE_INQUIRE, \ref pinentry, \ref pwmd_password()
816 LIBPWMD_API gpg_error_t
pwmd_open (pwm_t
*pwm
, const char *filename
,
817 pwmd_inquire_cb_t callback
, void *data
);
820 /*! \brief Check for socket activity.
822 * This function should be called periodically to check for any pending status
823 * messages sent from the server and when \em not in a command.
825 * \param pwm A handle.
826 * \return 0 on success or an error code.
828 * \note This function makes use of \ref pwmd_status_cb_t.
830 LIBPWMD_API gpg_error_t
pwmd_process (pwm_t
*pwm
);
833 /*! \brief Save a file on the pwmd server.
835 * This will send the SAVE command and write any changes to the document to
838 * The inquire \a callback function should be used when a SAVE option specified
839 * in \a args inquires data.
841 * \param pwm A handle.
842 * \param args Any SAVE protocol command options or NULL.
843 * \param callback A callback function to invoke when pwmd inquires data from
845 * \param user User data passed to the \a callback function.
846 * \return 0 on success or an error code.
847 * \see \ref PWMD_OPTION_OVERRIDE_INQUIRE, \ref PWMD_OPTION_NO_PINENTRY,
848 * \ref PWMD_OPTION_LOCAL_PINENTRY, \ref pwmd_command(), \ref pinentry
850 LIBPWMD_API gpg_error_t
pwmd_save (pwm_t
*pwm
, const char *args
,
851 pwmd_inquire_cb_t callback
, void *user
);
854 /*! \brief Change the passphrase for a data file.
856 * This will send the PASSWD command to the server taking care of pinentry
859 * The inquire \a callback function should be used when a PASSWD option
860 * specified in \a args inquires data.
862 * \param pwm A handle.
863 * \param args Any PASSWD protocol command options or NULL.
864 * \param callback A callback function to invoke when pwmd inquires data from
866 * \param user User data passed to the \a callback function.
867 * \return 0 on success or an error code.
868 * \see \ref PWMD_OPTION_OVERRIDE_INQUIRE, \ref PWMD_OPTION_NO_PINENTRY,
869 * \ref PWMD_OPTION_LOCAL_PINENTRY, \ref pwmd_command(), \ref pinentry
871 LIBPWMD_API gpg_error_t
pwmd_passwd (pwm_t
*pwm
, const char *args
,
872 pwmd_inquire_cb_t callback
, void *user
);
875 /*! \brief Send a command to the pwmd server.
877 * You should avoid sending the BYE command here because the assuan context
878 * will be freed and bad things will happen. Use \ref pwmd_close() instead.
880 * For pwmd commands that use an INQUIRE to retrieve data from a client, the
881 * \a callback parameter must be non-NULL and will be used to send the data to
884 * \param pwm A handle.
885 * \param[out] result The result of the command when successful which must be
886 * freed with \ref pwmd_free().
887 * \param[out] len The length of \a result.
888 * \param callback A callback function to invoke when pwmd inquires data from
890 * \param user User data passed to the \a callback function.
891 * \param cmd The command to send and any command arguments.
892 * \return 0 on success or an error code.
894 * \note Not all commands return a \a result.
896 LIBPWMD_API gpg_error_t
pwmd_command (pwm_t
*pwm
, char **result
,
898 pwmd_inquire_cb_t callback
,
899 void *user
, const char *cmd
, ...);
902 /*! \brief Send a command to the pwmd server.
904 * Like \ref pwmd_command() but uses an argument pointer instead.
906 * \param pwm A handle.
907 * \param[out] result The result of the command when successful which must be
908 * freed with \ref pwmd_free().
909 * \param[out] len The length of \a result.
910 * \param callback A callback function to invoke when pwmd inquires data from
912 * \param user User data passed to the \a callback function.
913 * \param cmd The command to send.
914 * \param ap The arguments to \a cmd.
915 * \return 0 on success or an error code.
917 * \note Not all commands return a \a result.
919 LIBPWMD_API gpg_error_t
pwmd_command_ap (pwm_t
*pwm
, char **result
,
921 pwmd_inquire_cb_t callback
,
922 void *user
, const char *cmd
,
926 /*! \brief Close a connection to the pwmd server.
928 * This will close the connection but keep any previously set options for the
929 * specified handle \a pwm. Calling \ref pwmd_connect() will re-acquire an
932 * \param pwm A handle.
933 * \return 0 on success or an error code.
934 * \see \ref pwmd_close()
936 LIBPWMD_API gpg_error_t
pwmd_disconnect (pwm_t
*pwm
);
939 /*! \brief Close a handle.
941 * This will close the connection to a pwmd server and free any resources
942 * associated with it.
944 * \param pwm A handle.
946 * \see \ref pwmd_disconnect(), \ref pwmd_new()
948 LIBPWMD_API
void pwmd_close (pwm_t
*pwm
);
951 /*! \brief The type of connection a handle has.
953 * Useful when you need to know what kind of connection a handle has.
955 * \param pwm A handle.
956 * \param[out] type The type of socket.
957 * \return 0 on success or an error code.
959 * \see \ref pwmd_socket_t
961 LIBPWMD_API gpg_error_t
pwmd_socket_type (pwm_t
*pwm
,
962 pwmd_socket_t
* type
);
964 /*! \brief Get the file descriptor associated with a handle.
966 * May be useful to determine whether a handle is ready for reading or
967 * writing by using select(2) or poll(2).
969 * \param pwm A handle.
970 * \param[out] fd Set to the file descriptor associated with \a pwm.
971 * \return 0 on success or an error code.
973 LIBPWMD_API gpg_error_t
pwmd_fd (pwm_t
*pwm
, int *fd
);
976 /*! \brief Free a previously allocated pointer.
978 * Use this function to free resources allocated by the other libpwmd memory
979 * functions. Do not use it to free allocations made by other allocators.
981 * The difference between the standard free() and this function is that
982 * this one will zero out the contents of the pointer before freeing it.
984 * \param ptr The pointer to deallocate.
986 * \see pwmd_malloc(), pwmd_calloc(), pwmd_realloc(), pwmd_strdup(),
989 LIBPWMD_API
void pwmd_free (void *ptr
);
992 /*! \brief A wrapper around malloc.
994 * Like malloc(), but lets libpwmd keep track of the pointer.
996 * \param size The number of bytes to allocate.
997 * \return A newly allocated pointer or NULL if there wasn't enough memory.
998 * \see malloc(3), pwmd_free()
1000 LIBPWMD_API
void *pwmd_malloc (size_t size
);
1003 /*! \brief A wrapper around calloc().
1005 * Like calloc(), but lets libpwmd keep track of the pointer.
1007 * \param nmemb The number of elements to allocate.
1008 * \param size The number of bytes to allocate.
1009 * \return A newly allocated pointer or NULL if there wasn't enough memory.
1010 * \see calloc(3), pwmd_free()
1012 LIBPWMD_API
void *pwmd_calloc (size_t nmemb
, size_t size
);
1015 /*! \brief A wrapper around realloc().
1017 * Like realloc(), but lets libpwmd keep track of the pointer.
1019 * \param ptr The pointer to reallocate.
1020 * \param size The new number of bytes to allocate.
1021 * \return A newly allocated pointer or NULL if there wasn't enough memory.
1022 * \see realloc(3), pwmd_free()
1024 LIBPWMD_API
void *pwmd_realloc (void *ptr
, size_t size
);
1027 /*! \brief A wrapper around strdup().
1029 * Like strdup(), but lets libpwmd keep track of the pointer.
1031 * \param str The string to duplicate.
1032 * \return A newly allocated character pointer or NULL if there wasn't
1034 * \see strdup(3), pwmd_free()
1036 LIBPWMD_API
char *pwmd_strdup (const char *str
);
1039 /*! \brief Duplicate a formatted string.
1041 * Like \ref asprintf(3), but lets libpwmd keep track of the pointer.
1043 * \param fmt The formatted string and any following arguments.
1044 * \return A newly allocated character pointer or NULL if there wasn't
1048 LIBPWMD_API
char *pwmd_strdup_printf (const char *fmt
, ...);