No longer set PWMD_OPTION_NO_PINENTRY for remote connections.
[libpwmd.git] / src / libpwmd.h.in
blob6cb329aafd8f7ae827b11c5395d144ab05b68373
1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
2 /*
3 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012
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
24 * server. 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: an SSH
36 * channel, or over TLS. Connections over SSH are less reliable than TLS
37 * connections since a proxy program is needed to connect to the pwmd UNIX
38 * Domain Socket. The kernel that pwmd runs under may buffer the UDS data
39 * before it reaches the proxy program and there isn't a portable way to
40 * modify kernel buffer settings. For larger transfers of data this is
41 * noticeable by the hanging of the command that initiated the transfer.
43 * If you really do need an SSH connection you can do so by using an SSH
44 * channel to spawn a shell that executes a proxy server to connect to the
45 * pwmd local Unix Domain Socket. Authentication is done by using SSH public
46 * key (see \ref ssh-keygen(1)) authentication and verifying the host key
47 * against a local OpenSSH known hosts formatted file.
49 * An unknown servers public key can be added to a known hosts file after user
50 * confirmation by setting the callback function \ref pwmd_knownhost_cb_t
51 * before connecting to the unknown host.
53 * On the server side you'll need a proxy server to connect to the pwmd
54 * server. Below is an example of an \ref authorized_keys(5) entry that will
55 * do this. The \a public_key portion should be the same as the contents of
56 * the \ref ssh-keygen(1) \a identity.pub file generated on the client
57 * machine. The private key should be passed as the \a identity parameter to
58 * \ref pwmd_connect():
60 * \code
61 * command="socat UNIX-CONNECT:$HOME/.pwmd/socket -" <public_key> ...
62 * \endcode
64 * \note Only an SSH identity without a passphrase is supported. For now
65 * anyway. This is a limitation of libssh2 (version 1.2.8 as of this writing).
67 * \version 6.0.3
68 * The first version to use the OpenSSH known hosts file format exclusively.
69 * Earlier versions used only an SHA1 hash of the host key.
71 * As mentioned, TLS connections are more reliable since there is no proxy
72 * program needed to connect to the pwmd server. pwmd itself can accept TLS
73 * connections and like SSH connections they are created with \ref
74 * pwmd_connect(). You'll need to generate a client key and X509 certificate
75 * then sign it with the same certificate authority (CA) that the pwmd server
76 * certificate was signed with.
78 * Certificates are similar to SSH public and private keys but a little harder
79 * to get setup correctly. See the \ref certtool(1) (recommended) and \ref
80 * openssl(1) manual pages for details.
83 /*! \section pinentry Pinentry Details
85 * \ref pinentry(1) is a program that prompts the user for input which is
86 * normally a passphrase or a confirmation. libpwmd can use this program
87 * either locally (X11 forwarding is not yet supported) or have the pwmd
88 * server (really gpg-agent) use it's pinentry to retrieve a passphrase when
89 * needed.
91 * There are a few options that tell pinentry how and where to prompt for a
92 * needed passphrase. See the \ref pwmd_option_t section for details. These
93 * options are only sent to pwmd in \ref pwmd_open() and only when pinentry
94 * use has not been disabled and when not over an remote connnection.
96 * Some pinentry options may also be specified in the local configuration file
97 * \a "~/.pwmd/pinentry.conf". These options are initial values for each
98 * pinentry invokation (not retries) and may be changed by setting the
99 * appropriate \ref pwmd_option_t. Options and values are separated with a '='
100 * on a single line. Unrecognized options are ignored. Here are the recognized
101 * options:
103 * \param PATH The full path to the location of the pinentry binary. Only
104 * useful for the local pinentry.
105 * \param DISPLAY The X11 display to use.
106 * \param TTYNAME The full path to the tty that pinentry should prompt on.
107 * \param TTYTYPE The terminal type of the tty which is required if DISPLAY is
108 * not set.
109 * \param LC_MESSAGES Adjust for the current locale.
110 * \param LC_CTYPE Adjust for the current locale.
112 * \filepath
114 * \note The initial values for the pinentry TTY, TERM and DISPLAY are set
115 * during \ref pwmd_new() depending on the current environment. They can be
116 * reset or changed as needed.
118 * \note After establishing an remote connection, pwmd's pinentry is disabled
119 * by passing the "--no-pinentry" option to the OPEN protocol command during
120 * \ref pwmd_open() since X11 forwarding has not been implemented. A local
121 * pinentry will be used in the case of a remote connection.
123 * \see \ref remote
126 /*! \section Errors
128 * libpwmd uses libgpg-error for all error codes. Error codes can be
129 * described by using \ref gpg_strerror(), or the thread-safe \ref
130 * gpg_strerror_r().
132 * \note libgpg-error returns an error code as a bitmask of an error source
133 * and the error code. Determining the error source can help you find where
134 * the error occured, beit from \ref pinentry(1), \ref gpg-agent(1), \ref
135 * libgcrypt, \ref pwmd(1) or \ref libpwmd(3). pwmd uses the \ref
136 * GPG_ERR_SOURCE_USER_1 error source and libpwmd uses \ref
137 * GPG_ERR_SOURCE_USER_2. To find the error source of an error code the \ref
138 * gpg-error(1) command line utility may be of use. Also see the \ref
139 * libgpg-error documentation for error code manipulation.
142 /*! \file */
143 #ifndef LIBPWMD_H
144 #define LIBPWMD_H
146 #ifndef LIBPWMD_API
147 #define LIBPWMD_API
148 #endif
150 #ifdef GPG_ERR_SOURCE_DEFAULT
151 #error "GPG_ERR_SOURCE_DEFAULT already defined."
152 #else
153 #define GPG_ERR_SOURCE_DEFAULT GPG_ERR_SOURCE_USER_2
154 #endif
155 #include <gpg-error.h>
156 #include <stdarg.h>
158 #ifdef __cplusplus
159 extern "C" {
160 #endif
162 /*! \def LIBPWMD_VERSION
163 * \brief Version information.
165 * The version of this library.
167 #define LIBPWMD_VERSION 0x@VER_MAJOR_HEX@@VER_MINOR_HEX@@VER_PATCH_HEX@
170 /*! \def LIBPWMD_VERSION_MAJOR
171 * \brief Version information.
173 * The major release number of this library.
175 #define LIBPWMD_VERSION_MAJOR @VER_MAJOR@
178 /*! \def LIBPWMD_VERSION_MINOR
179 * \brief Version information.
181 * The minor release number of this library.
183 #define LIBPWMD_VERSION_MINOR @VER_MINOR@
186 /*! \def LIBPWMD_VERSION_PATCH
187 * \brief Version information.
189 * The patch level of this library.
191 #define LIBPWMD_VERSION_PATCH @VER_PATCH@
194 /*! \def LIBPWMD_VERSION_STR
195 * \brief Version information.
197 * A string representation of the version of this library.
199 #define LIBPWMD_VERSION_STR @VER_STRING@
202 /*! \brief Returns this version of libpwmd.
204 * As a string.
205 * \return A string.
207 LIBPWMD_API const char *pwmd_version();
210 struct pwm_s;
211 /*! \typedef pwm_t
212 * \brief libpwmd handle.
214 * When a handle or context is mentioned in this documentation it is a pointer
215 * of this type. A new handle is created with \ref pwmd_new().
217 typedef struct pwm_s pwm_t;
220 /*! \typedef pwmd_socket_t
221 * \brief The type of socket a handle is connected to.
223 * For use with \ref pwmd_socket_type().
225 typedef enum {
226 /*! A local UNIX domain socket. */
227 PWMD_SOCKET_LOCAL,
229 /*! An SSH channel over a TCP socket. */
230 PWMD_SOCKET_SSH,
232 /*! A TLS connection. */
233 PWMD_SOCKET_TLS
234 } pwmd_socket_t;
237 /*! \typedef pwmd_pinentry_t
238 * \brief Local pinentry commands and not pwmd pinentry.
240 * These determine what prompt a local or fork()'ed pinentry uses. For use
241 * with \ref pwmd_getpin().
243 typedef enum {
244 /*! When opening a file. */
245 PWMD_PINENTRY_OPEN,
247 /*! When opening a file failed. */
248 PWMD_PINENTRY_OPEN_FAILED,
250 /*! When saving a file. */
251 PWMD_PINENTRY_SAVE,
253 /*! For passphrase confirmation. */
254 PWMD_PINENTRY_SAVE_CONFIRM,
256 /*! When saving a file and passphrase confirmation failed. */
257 PWMD_PINENTRY_SAVE_FAILED,
259 /*! For confirmation of a user-defined prompt. */
260 PWMD_PINENTRY_CONFIRM,
262 /*! For the default or user defined string set with \ref
263 * PWMD_OPTION_PINENTRY_DESC. */
264 PWMD_PINENTRY_USER,
266 /*! To terminate the pinentry process created with \ref pwmd_getpin(). */
267 PWMD_PINENTRY_CLOSE
268 } pwmd_pinentry_t;
271 /*! \typedef pwmd_status_cb_t
272 * \brief Process status messages.
274 * The value of the option \ref PWMD_OPTION_STATUS_CB which is set with \ref
275 * pwmd_setopt().
277 * \param user A user data pointer which is set with \ref
278 * PWMD_OPTION_STATUS_DATA.
279 * \param line The status message line received from the server.
280 * \return 0 on success or an error code which will cause a command to fail.
282 typedef gpg_error_t (*pwmd_status_cb_t)(void *user, const char *line);
285 /*! \typedef pwmd_inquire_cb_t
286 * \brief Send data to the pwmd server.
288 * This is a callback function that is used for sending data to the server for
289 * commands that need to reply to an INQUIRE server response. The reason for
290 * this callback is to let the client send as many bytes as it wants rather
291 * than the entire data at once. It gets called during an internal \ref
292 * assuan_transact() from an internal inquire callback function which in turn
293 * calls this function by looping over its return value.
295 * \param user The user data pointer passed to the libpwmd function specifying
296 * this callback.
297 * \param keyword The name of this inquire. Could be a command name or some
298 * keyword describing what needs to be sent. See the pwmd and \ref
299 * gpg-agent(1) documentation for details.
300 * \param rc The result of the last internal call to \ref assuan_send_data()
301 * which did the sending of the data to the pwmd server. On the first call to
302 * this callback it's value will always be 0 since no data has been sent yet.
303 * This should be checked during each call to this function and should return
304 * the same error code when set. Its purpose is to let the client clean up
305 * before letting the command fail.
306 * \param[out] data The next chunk of data to send or NULL.
307 * \param[out] len The length of \a data or 0.
309 * \retval 0 There is more data to be sent.
310 * \retval GPG_ERR_EOF No need to call this function again, the current
311 * \a data is the last to send.
312 * \retval code Any other error code which will cancel the INQUIRE.
314 * \note The sent data is processed line-per-line. The line is either newline
315 * terminated or is buffered until ASSUAN_LINELENGTH bytes have been
316 * allocated. Any remaining bytes are sent after the INQUIRE has finished.
318 typedef gpg_error_t (*pwmd_inquire_cb_t)(void *user, const char *keyword,
319 gpg_error_t rc, char **data, size_t *len);
322 /*! \typedef pwmd_knownhost_cb_t
323 * \brief Verify a remote SSH connection.
325 * When \ref PWMD_OPTION_KNOWNHOST_CB is set and a the current connections
326 * host key was not found in the known hosts file, then this callback function
327 * can be used to confirm the addition of the new host key to the known_hosts
328 * file.
330 * \param user User data which was set with \ref PWMD_OPTION_KNOWNHOST_DATA.
331 * \param host The hostname as passed to \ref pwmd_connect().
332 * \param hostkey The raw binary data of the host key.
333 * \param len The length of \a hostkey.
334 * \retval 0 Add the host key to the known hosts file.
335 * \retval GPG_ERR_NOT_CONFIRMED Do not add the host key and abort the
336 * connection. This is the recommended error code although any other non-zero
337 * return value will also abort the connection.
339 * \note If the known hosts file cannot be modified do to filesystem
340 * restrictions when trying to add the new host key, no error is returned.
341 * Instead, the host key is added to the current connections host key cache
342 * and the connection is accepted.
344 * \see \ref remote
346 typedef gpg_error_t (*pwmd_knownhost_cb_t)(void *user, const char *host,
347 const char *hostkey, size_t len);
350 /*! \enum pwmd_option_t
351 * \brief libpwmd options.
353 * Options are set with \ref pwmd_setopt(). Some options must be set before a
354 * connection is made to have any effect.
356 * \filepath
358 /* Note to self: dont change the order or insert. Only append. */
359 typedef enum {
360 /*! A string value which specifies the full path of the \ref pinentry(1)
361 * binary. The default is specified at compile time.
363 * \note This only affects the local pinentry.
364 * \see \ref pinentry
366 PWMD_OPTION_PINENTRY_PATH,
368 /*! A string value which specifies the full path to the TTY that \ref
369 * pinentry(1) will use to prompt on. When set and no DISPLAY is
370 * available, \ref PWMD_OPTION_PINENTRY_TERM must also be set.
372 * \see \ref pinentry
374 PWMD_OPTION_PINENTRY_TTY,
376 /*! A string value which specifies the terminal type (i.e., vt100) that
377 * \ref pinentry(1) will use when no X11 display is available.
379 * \see \ref pinentry
381 PWMD_OPTION_PINENTRY_TERM,
383 /*! A string value which specifies the X11 display that \ref pinentry(1)
384 * will use. \ref pinentry(1) seems to make DISPLAY have a higher priority
385 * than \ref PWMD_OPTION_PINENTRY_TTY.
387 * \see \ref pinentry
389 PWMD_OPTION_PINENTRY_DISPLAY,
391 /*! A character string that \ref pinentry(1) will use in it's dialog
392 * window.
394 PWMD_OPTION_PINENTRY_ERROR,
396 /*! \copydoc PWMD_OPTION_PINENTRY_ERROR*/
397 PWMD_OPTION_PINENTRY_PROMPT,
399 /*! \copydoc PWMD_OPTION_PINENTRY_ERROR*/
400 PWMD_OPTION_PINENTRY_DESC,
402 /*! For \ref pinentry(1) localization. */
403 PWMD_OPTION_PINENTRY_LC_CTYPE,
405 /*! \copydoc PWMD_OPTION_PINENTRY_LC_CTYPE */
406 PWMD_OPTION_PINENTRY_LC_MESSAGES,
408 /*! An integer value that specifies the number of seconds \ref pinentry(1)
409 * will wait for input before timing out and aborting the current command.
410 * If 0, then no timeout will be used. The default is 30.
412 * \note This only affects the local pinentry.
414 PWMD_OPTION_PINENTRY_TIMEOUT,
416 /*! An integer value that specifies the number of times to prompt for a
417 * passphrase before returning an error.
419 * \note This only affects the local pinentry.
421 PWMD_OPTION_PINENTRY_TRIES,
423 /*! A function of type \ref pwmd_status_cb_t that will process status
424 * messages received from the pwmd server.
426 PWMD_OPTION_STATUS_CB,
428 /*! A user data pointer which is passed to the status message function
429 * \ref PWMD_OPTION_STATUS_CB. */
430 PWMD_OPTION_STATUS_DATA,
432 /*! A function of type \ref pwmd_knownhost_cb_t that will be used to
433 * confirm a host key that was not found in the known hosts file.
435 * \see \ref remote
437 PWMD_OPTION_KNOWNHOST_CB,
439 /*! User supplied data which is passed to the known host function \ref
440 * PWMD_OPTION_KNOWNHOST_CB.
442 * \see \ref remote
444 PWMD_OPTION_KNOWNHOST_DATA,
446 /*! When the total number of bytes to be sent via an INQUIRE is known,
447 * this should be set so XFER status messages can be parsed correctly.
448 * When not known or unset, 0 is used as the total argument to the XFER
449 * status message. This option should be set before each call every
450 * function that uses an \ref pwmd_inquire_cb_t.
452 * \note During the INQUIRE, \ref PWMD_OPTION_STATUS_CB is called, when
453 * set, after every iteration of the \ref pwmd_inquire_cb_t.
455 * \note This is a libpwmd feature. pwmd itself does not send XFER status
456 * messages during an INQUIRE. Status messages can be parsed only when
457 * \ref PWMD_OPTION_STATUS_CB is set.
459 PWMD_OPTION_INQUIRE_TOTAL,
461 /*! When set to 1, lock the file mutex after opening a file as if the LOCK
462 * command had been sent.
464 PWMD_OPTION_LOCK_ON_OPEN,
466 /*! Use ssh-agent to retrieve the private key to authenticate when
467 * connecting to a remote pwmd server. */
468 PWMD_OPTION_SSH_AGENT,
470 /*! When 1, disable pinentry use. This will prevent both pwmd and libpwmd
471 * from using a pinentry program.
473 * \note This must be set before calling \ref pwmd_open().
475 PWMD_OPTION_NO_PINENTRY,
477 /*! When 1, libpwmd will disable pwmd's pinentry and use it's own
478 * fork(2)'ed pinentry.
480 * \note This must be set before calling \ref pwmd_open().
482 PWMD_OPTION_LOCAL_PINENTRY,
484 /*! When set, override libpwmd's handling of server inquires with the
485 * PASSPHRASE and NEW_PASSPHRASE keywords. This handling is done
486 * automatically when \ref PWMD_OPTION_NO_PINENTRY or \ref
487 * PWMD_OPTION_LOCAL_PINENTRY is set or when the connection is a remote
488 * one.
490 * \see \ref remote, \ref PWMD_OPTION_LOCAL_PINENTRY, \ref pwmd_password()
492 PWMD_OPTION_OVERRIDE_INQUIRE,
494 /*! An int specifying a timeout in seconds before a blocking ssh function
495 * will timeout causing a command to fail. */
496 PWMD_OPTION_SSH_TIMEOUT,
498 /*! An int specifying an interval in seconds that \ref pwmd_process() will
499 * send a keepalive message to the remote SSH server. */
500 PWMD_OPTION_SSH_KEEPALIVE,
502 /*! An int specifying whether to enable TLS hostname verification on the
503 * server certificate chain. Default is 0 or disabled. */
504 PWMD_OPTION_TLS_VERIFY,
505 } pwmd_option_t;
508 /*! \def PWMD_FEATURE_PINENTRY
509 * \hideinitializer
510 * \brief Pinentry support.
512 * This is for a local or fork()'ed pinentry.
514 #define PWMD_FEATURE_PINENTRY 0x0001
516 /*! \def PWMD_FEATURE_CRACK
517 * \hideinitializer
518 * \brief Password quality checking.
520 * A password quality meter is shown when the pinentry supports it.
522 #define PWMD_FEATURE_CRACK 0x0002
524 /*! \def PWMD_FEATURE_SSH
525 * \hideinitializer
526 * \brief Remote connections over an SSH channel.
528 * \see \ref remote
530 #define PWMD_FEATURE_SSH 0x0004
532 /*! \def PWMD_FEATURE_GNUTLS
533 * \hideinitializer
534 * \brief Remote connections over TLS.
536 * \see \ref remote
538 #define PWMD_FEATURE_GNUTLS 0x0008
541 /*! \brief libpwmd compile time features.
543 * Useful for clients to determine what features are compiled into libpwmd at
544 * runtime.
546 * \return A bitmask of features.
548 LIBPWMD_API unsigned int pwmd_features();
551 /*! \brief Initialize the library.
553 * This function must be the first function called in the library before any
554 * others. It sets up the memory allocators and internationalization among
555 * other things.
557 * \return 0 on success or an error code.
559 LIBPWMD_API gpg_error_t pwmd_init(void);
561 /*! \brief Deinitialize the library.
563 * This function is mainly for cleaning up other libraries that libpwmd links
564 * with to prevent leaks showing up a debugger. It should be the final libpwmd
565 * function call before your app exits.
567 LIBPWMD_API void pwmd_deinit(void);
570 /*! \brief Creates a new handle.
572 * Creates a new handle for use with the other functions.
574 * \param name If not NULL, the name of the application. The application name
575 * is sent to the pwmd server after successfully connecting and is used in
576 * pwmd logging.
577 * \param[in] pwm A new handle for use with the other functions.
579 * \return 0 on success or an error code.
581 LIBPWMD_API gpg_error_t pwmd_new(const char *name, pwm_t **pwm);
584 /*! \brief Establish a connection to a pwmd server.
586 * Connects to the pwmd server specified in \a url. The format of \a url is:
587 * \par
588 * file:///path/to/socket, or
589 * \par
590 * ssh[46]://[username@]hostname[:port], or
591 * \par
592 * tls[46]://hostname[:port]
594 * If \a url is NULL then the default local pwmd socket \a ~/.pwmd/socket
595 * will be used.
597 * The remaining arguments are parameters for the \a url.
599 * For SSH connections, the first of the positional parameters should be the
600 * identity file to use and is required to be non-NULL unless \ref
601 * PWMD_OPTION_SSH_AGENT is set. The final parameter is the known hosts file
602 * to use or NULL to use a default of \a ~/.ssh/knownhosts.
604 * For TLS connections, the first positional parameter should be the client
605 * certificate filename. The second parameter should be the client
606 * certificate key filename. The third parameter should be the Certificate
607 * Authority (CA) file that was used to sign the server certificate. The
608 * final parameter should be a TLS cipher priority string or NULL to use the
609 * default of "SECURE256".
611 * For local connections, any remaining parameters are ignored.
613 * \param pwm A handle.
614 * \param url The socket to connect to.
615 * \param ... Remaining parameters for the \a url.
616 * \return 0 on success or an error code.
617 * \filepath
618 * \see \ref PWMD_OPTION_SSH_AGENT, pwmd_socket_type(), pwmd_disconnect(),
619 * \ref remote
621 LIBPWMD_API gpg_error_t pwmd_connect(pwm_t *pwm, const char *url, ...);
624 /*! \brief Set handle options.
626 * See \ref pwmd_option_t for option specific details.
628 * \param pwm A handle.
629 * \param opt The option and following option value.
630 * \return 0 on success or an error code.
632 LIBPWMD_API gpg_error_t pwmd_setopt(pwm_t *pwm, pwmd_option_t opt, ...);
635 /*! \brief Launch a local pinentry.
637 * Does not send any command to the pwmd server. This maybe useful if a
638 * passphrase is needed while opening a file over a remote connection and
639 * during an \ref pwmd_open() server inquire.
641 * This function may also be used to display a user confirmation dialog with
642 * pinentry when \a which is \ref PWMD_PINENTRY_CONFIRM. The text to prompt
643 * with is set with \ref PWMD_OPTION_PINENTRY_DESC.
645 * \param pwm A handle.
646 * \param filename The filename to use in the pinentry dialog strings.
647 * \param[in] result The entered value in the pinentry dialog which should be
648 * freed with \ref pwmd_free().
649 * \param[in] len The length of \a result.
650 * \param which Determines the default strings shown in the pinentry
651 * dialog. \ref pwmd_setopt() may also be used to override the defaults. In
652 * this case \ref PWMD_PINENTRY_USER should be used. \ref PWMD_PINENTRY_CLOSE
653 * should be used to terminate the pinentry process when the pinentry is no
654 * longer needed.
656 * \return 0 on success or an error.
658 * \see pwmd_password()
660 LIBPWMD_API gpg_error_t pwmd_getpin(pwm_t *pwm, const char *filename,
661 char **result, size_t *len, pwmd_pinentry_t which);
663 /*! \brief Obtain a passphrase from a local pinentry.
665 * This is the same function that libpwmd uses during an inquire when using
666 * the local pinentry and the inquire keyword is either PASSPHRASE or
667 * NEW_PASSPHRASE. Provided for convenience since it sets proper pinentry
668 * strings and handles new passphrase confirmation.
670 * \param pwm A handle.
671 * \param keyword The keyword to determine pinentry strings. Usually
672 * PASSPHRASE or NEW_PASSPHRASE.
673 * \param[in] data The obtained passphrase which should be freed with \ref
674 * pwmd_free().
675 * \param[in] size The length of \a data.
677 * \return 0 on success or an error.
679 * \note It is an error for \a size to be zero when inquiring a \a keyword of
680 * PASSPHRASE. In this case GPG_ERR_CANCELED will be returned.
682 LIBPWMD_API gpg_error_t pwmd_password(pwm_t *pwm, const char *keyword,
683 char **data, size_t *size);
685 /*! \brief Open a file on the pwmd server.
687 * This will send the OPEN command to the server.
689 * The inquire \a callback function should be used when \ref
690 * PWMD_OPTION_NO_PINENTRY is set and is used to send the passphrase when
691 * needed.
693 * \param pwm A handle.
694 * \param filename The filename to open. The \a filename is not a full path
695 * but the data filename only.
696 * \param callback A callback function to invoke when pwmd inquires data from
697 * the client.
698 * \param data User data passed to the \a callback function.
699 * \return 0 on success or an error code.
700 * \see \ref pinentry, pwmd_password()
702 LIBPWMD_API gpg_error_t pwmd_open(pwm_t *pwm, const char *filename,
703 pwmd_inquire_cb_t callback, void *data);
706 /*! \brief Check for socket activity.
708 * This function should be called periodically to check for any pending status
709 * messages sent from the server and when \em not in a command.
711 * \param pwm A handle.
712 * \return 0 on success or an error code.
714 * \note This function makes use of \ref pwmd_status_cb_t.
716 LIBPWMD_API gpg_error_t pwmd_process(pwm_t *pwm);
719 /*! \brief Save a file on the pwmd server.
721 * This will send the SAVE command and write any changes to the document to
722 * disk.
724 * The inquire \a callback function should be used when \ref
725 * PWMD_OPTION_NO_PINENTRY is set or when a SAVE option specified in \a args
726 * requires more data.
728 * \param pwm A handle.
729 * \param args Any SAVE protocol command options or NULL.
730 * \param callback A callback function to invoke when pwmd inquires data from
731 * the client.
732 * \param user User data passed to the \a callback function.
733 * \return 0 on success or an error code.
734 * \see \ref pwmd_command(), \ref pinentry
736 LIBPWMD_API gpg_error_t pwmd_save(pwm_t *pwm, const char *args,
737 pwmd_inquire_cb_t callback, void *user);
740 /*! \brief Send a command to the pwmd server.
742 * You should avoid sending the BYE command here because the assuan context
743 * will be freed and bad things will happen. Use \ref pwmd_close() instead.
745 * For pwmd commands that use an INQUIRE to retrieve data from a client, the
746 * \a callback parameter must be non-NULL and will be used to send the data to
747 * pwmd.
749 * \param pwm A handle.
750 * \param[in] result The result of the command when successful which must be
751 * freed with \ref pwmd_free().
752 * \param[in] len The length of \a result.
753 * \param callback A callback function to invoke when pwmd inquires data from
754 * the client.
755 * \param user User data passed to the \a callback function.
756 * \param cmd The command to send and any following arguments.
757 * \return 0 on success or an error code.
759 * \note Not all commands return a \a result.
761 LIBPWMD_API gpg_error_t pwmd_command(pwm_t *pwm, char **result, size_t *len,
762 pwmd_inquire_cb_t callback, void *user, const char *cmd, ...);
765 /*! \brief Send a command to the pwmd server.
767 * Like \ref pwmd_command() but uses an argument pointer instead.
769 * \param pwm A handle.
770 * \param[in] result The result of the command when successful which must be
771 * freed with \ref pwmd_free().
772 * \param[in] len The length of \a result.
773 * \param callback A callback function to invoke when pwmd inquires data from
774 * the client.
775 * \param user User data passed to the \a callback function.
776 * \param cmd The command to send.
777 * \param ap The arguments to \a cmd.
778 * \return 0 on success or an error code.
780 * \note Not all commands return a \a result.
782 LIBPWMD_API gpg_error_t pwmd_command_ap(pwm_t *pwm, char **result, size_t *len,
783 pwmd_inquire_cb_t callback, void *user, const char *cmd, va_list ap);
786 /*! \brief Close a connection to the pwmd server.
788 * This will close the connection but keep any previously set options for the
789 * specified handle. Calling \ref pwmd_connect() will re-acquire an libassuan
790 * context.
792 * \param pwm A handle.
793 * \return 0 on success or an error code.
794 * \see \ref pwmd_close()
796 LIBPWMD_API gpg_error_t pwmd_disconnect(pwm_t *pwm);
799 /*! \brief Close a handle.
801 * This will close the connection to a pwmd server and free any resources
802 * associated with it.
804 * \param pwm A handle.
805 * \return Nothing.
806 * \see \ref pwmd_disconnect(), \ref pwmd_new()
808 LIBPWMD_API void pwmd_close(pwm_t *pwm);
811 /*! \brief The type of connection a handle has.
813 * Useful when you want to know what kind of connection a handle has.
815 * \param pwm A handle.
816 * \param[in] type The type of socket.
817 * \return 0 on success or an error code.
819 * \see \ref pwmd_socket_t
821 LIBPWMD_API gpg_error_t pwmd_socket_type(pwm_t *pwm, pwmd_socket_t *type);
823 /*! \brief Get the file descriptor associated with a handle.
825 * May be useful to determine whether a handle is ready for reading or
826 * writing by using select(2) or poll(2).
828 * \param pwm A handle.
829 * \param[out] fd Set to the file descriptor associated with \a pwm.
830 * \return 0 on success or an error code.
832 LIBPWMD_API gpg_error_t pwmd_fd(pwm_t *pwm, int *fd);
835 /*! \brief Free a previously allocated pointer.
837 * Use this function to free resources allocated by the other libpwmd memory
838 * functions. Do not use it to free allocations made by other allocators.
840 * The difference between the standard free() and this function is that
841 * this one will zero out the contents of the pointer before freeing it.
843 * \param ptr The pointer to deallocate.
844 * \return Nothing.
845 * \see pwmd_malloc(), pwmd_calloc(), pwmd_realloc(), pwmd_strdup(),
846 * pwmd_command()
848 LIBPWMD_API void pwmd_free(void *ptr);
851 /*! \brief A wrapper around malloc.
853 * Like malloc(), but lets libpwmd keep track of the pointer.
855 * \param size The number of bytes to allocate.
856 * \return A newly allocated pointer or NULL if there wasn't enough memory.
857 * \see malloc(3), pwmd_free()
859 LIBPWMD_API void *pwmd_malloc(size_t size);
862 /*! \brief A wrapper around calloc().
864 * Like calloc(), but lets libpwmd keep track of the pointer.
866 * \param nmemb The number of elements to allocate.
867 * \param size The number of bytes to allocate.
868 * \return A newly allocated pointer or NULL if there wasn't enough memory.
869 * \see calloc(3), pwmd_free()
871 LIBPWMD_API void *pwmd_calloc(size_t nmemb, size_t size);
874 /*! \brief A wrapper around realloc().
876 * Like realloc(), but lets libpwmd keep track of the pointer.
878 * \param ptr The pointer to reallocate.
879 * \param size The new number of bytes to allocate.
880 * \return A newly allocated pointer or NULL if there wasn't enough memory.
881 * \see realloc(3), pwmd_free()
883 LIBPWMD_API void *pwmd_realloc(void *ptr, size_t size);
886 /*! \brief A wrapper around strdup().
888 * Like strdup(), but lets libpwmd keep track of the pointer.
890 * \param str The string to duplicate.
891 * \return A newly allocated character pointer or NULL if there wasn't
892 * enough memory.
893 * \see strdup(3), pwmd_free()
895 LIBPWMD_API char *pwmd_strdup(const char *str);
898 /*! \brief Duplicate a formatted string.
900 * Like \ref asprintf(3), but lets libpwmd keep track of the pointer.
902 * \param fmt The formatted string and any following arguments.
903 * \return A newly allocated character pointer or NULL if there wasn't
904 * enough memory.
905 * \see pwmd_free()
907 LIBPWMD_API char *pwmd_strdup_printf(const char *fmt, ...);
909 /*! \brief Convenience declaration.
911 * This allows an application wanting both a TLS connection and libpwmd secure
912 * memory management in GnuTLS, not needing to have the GnuTLS development
913 * files installed. This function should be called before \ref pwmd_init().
915 * It is not done in \ref pwmd_init() itself since the application may have
916 * already initialized GnuTLS by some other means.
918 extern void gnutls_global_set_mem_functions(
919 void *(*)(size_t), // gnutls_alloc_function
920 void *(*)(size_t), // gnutls_alloc_function (secure)
921 int (*)(const void *), // gnutls_is_secure_function
922 void *(*)(void *, size_t), // gnutls_realloc_function
923 void (*)(void *)); // gnutls_free_function
925 #ifdef __cplusplus
927 #endif
929 #endif