Add PWMD_OPTION_SERVER_VERSION.
[libpwmd.git] / src / libpwmd.h.in
blob11d5a673ad77c6a9ede8dbf57c7c835690eeca7d
1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
2 /*
3 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
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: over SSH
36 * and over TLS.
38 * Connections over SSH are done by creating an SSH channel to spawn a
39 * shell that executes a proxy server to connect to the pwmd local
40 * Unix Domain Socket. Authentication is done by using SSH public key
41 * (see \ref ssh-keygen(1)) authentication and verifying the host key
42 * against a local OpenSSH known hosts formatted file.
44 * An unknown servers public key can be added to a known hosts file after user
45 * confirmation by setting the callback function \ref pwmd_knownhost_cb_t
46 * before connecting to the unknown host.
48 * On the server side you'll need a proxy server to connect to the pwmd
49 * server. Below is an example of an \ref authorized_keys(5) entry that will
50 * do this. The \a public_key portion should be the same as the contents of
51 * the \ref ssh-keygen(1) \a identity.pub file generated on the client
52 * machine. The private key should be passed as the \a identity parameter to
53 * \ref pwmd_connect():
55 * \code
56 * command="socat UNIX-CONNECT:$HOME/.pwmd/socket -" <public_key> ...
57 * \endcode
59 * \note Only an SSH identity without a passphrase is supported. For now
60 * anyway. This is a limitation of libssh2 (version 1.2.8 as of this writing).
62 * \version 6.0.3
63 * The first version to use the OpenSSH known hosts file format exclusively.
64 * Earlier versions used only an SHA1 hash of the host key.
66 * Pwmd itself can accept TLS connections so no proxy program is
67 * needed as is when using SSH. Like SSH connections, TLS connections
68 * are created with \ref pwmd_connect(). You will need to generate a
69 * client key and X509 certificate and then sign it with the same
70 * certificate authority (CA) that the pwmd server certificate was
71 * signed with.
73 * Certificates are similar to SSH public and private keys but a little harder
74 * to setup correctly. See the \ref certtool(1) (recommended) and \ref
75 * openssl(1) manual pages for details.
78 /*! \section pinentry Pinentry Details
80 * \ref pinentry(1) is a program that prompts the user for input which is
81 * normally a passphrase or a confirmation. libpwmd can use this program
82 * either locally (X11 forwarding is not yet supported) or have the pwmd
83 * server or gpg-agent use it's pinentry to retrieve a passphrase when
84 * needed.
86 * There are a few options that tell pinentry how and where to prompt for a
87 * needed passphrase. See the \ref pwmd_option_t section for details. These
88 * options are only sent to pwmd in \ref pwmd_open() and only when pinentry
89 * use has not been disabled and when not over an remote connnection.
91 * Some pinentry options may also be specified in the local configuration file
92 * \a "~/.pwmd/pinentry.conf". These options are initial values for each
93 * pinentry invokation (not retries) and may be changed by setting the
94 * appropriate \ref pwmd_option_t. Options and values are separated with a '='
95 * on a single line. Unrecognized options are ignored. Here are the recognized
96 * options:
98 * \param PATH The full path to the location of the pinentry binary. Only
99 * useful for the local pinentry.
100 * \param DISPLAY The X11 display to use.
101 * \param TTYNAME The full path to the tty that pinentry should prompt on.
102 * \param TTYTYPE The terminal type of the tty which is required if DISPLAY is
103 * not set.
104 * \param LC_MESSAGES Adjust for the current locale.
105 * \param LC_CTYPE Adjust for the current locale.
107 * \filepath
109 * \note The initial values for the pinentry TTY, TERM and DISPLAY are set
110 * during \ref pwmd_new() depending on the current environment. They can be
111 * reset or changed as needed.
113 * \note After establishing a remote connection, pwmd's pinentry is disabled
114 * by passing the "--no-pinentry" option to the OPEN protocol command during
115 * \ref pwmd_open() since X11 forwarding has not been implemented. A local
116 * pinentry will be used in the case of a remote connection.
118 * \see \ref remote
121 /*! \section Errors
123 * libpwmd uses libgpg-error for all error codes. Error codes can be
124 * described by using \ref gpg_strerror(), or the thread-safe \ref
125 * gpg_strerror_r().
127 * \note libgpg-error returns an error code as a bitmask of an error source
128 * and the error code. Determining the error source can help you find where
129 * the error occured, beit from \ref pinentry(1), \ref gpg-agent(1), \ref
130 * libgcrypt, \ref pwmd(1) or \ref libpwmd(3). pwmd uses the \ref
131 * GPG_ERR_SOURCE_USER_1 error source and libpwmd uses \ref
132 * GPG_ERR_SOURCE_USER_2. To find the error source of an error code the \ref
133 * gpg-error(1) command line utility may be of use. Also see the \ref
134 * libgpg-error documentation for error code manipulation.
137 /*! \file */
138 #ifndef LIBPWMD_H
139 #define LIBPWMD_H
141 #ifndef LIBPWMD_API
142 #define LIBPWMD_API
143 #endif
145 #ifdef GPG_ERR_SOURCE_DEFAULT
146 #error "GPG_ERR_SOURCE_DEFAULT already defined."
147 #else
148 #define GPG_ERR_SOURCE_DEFAULT GPG_ERR_SOURCE_USER_2
149 #endif
150 #include <gpg-error.h>
151 #include <stdarg.h>
153 #ifdef __cplusplus
154 extern "C"
156 #endif
158 /*! \def LIBPWMD_VERSION
159 * \brief Version information.
161 * The version of this library.
163 #define LIBPWMD_VERSION 0x@VER_MAJOR_HEX@@VER_MINOR_HEX@@VER_PATCH_HEX@
166 /*! \def LIBPWMD_VERSION_MAJOR
167 * \brief Version information.
169 * The major release number of this library.
171 #define LIBPWMD_VERSION_MAJOR @VER_MAJOR@
174 /*! \def LIBPWMD_VERSION_MINOR
175 * \brief Version information.
177 * The minor release number of this library.
179 #define LIBPWMD_VERSION_MINOR @VER_MINOR@
182 /*! \def LIBPWMD_VERSION_PATCH
183 * \brief Version information.
185 * The patch level of this library.
187 #define LIBPWMD_VERSION_PATCH @VER_PATCH@
190 /*! \def LIBPWMD_VERSION_STR
191 * \brief Version information.
193 * A string representation of the version of this library.
195 #define LIBPWMD_VERSION_STR @VER_STRING@
198 /*! \brief Returns this version of libpwmd.
200 * As a string.
201 * \return A string.
203 LIBPWMD_API const char *pwmd_version ();
206 struct pwm_s;
207 /*! \typedef pwm_t
208 * \brief A libpwmd handle.
210 * When a pwmd handle or context is mentioned in this documentation it
211 * is a pointer of this type. A new handle is created with \ref
212 * pwmd_new().
214 typedef struct pwm_s pwm_t;
217 /*! \typedef pwmd_socket_t
218 * \brief The type of socket a handle is connected to.
220 * For use with \ref pwmd_socket_type().
222 typedef enum
224 /*! A local UNIX domain socket. */
225 PWMD_SOCKET_LOCAL,
227 /*! An SSH channel over a TCP socket. */
228 PWMD_SOCKET_SSH,
230 /*! A TLS connection over a TCP socket. */
231 PWMD_SOCKET_TLS
232 } pwmd_socket_t;
235 /*! \typedef pwmd_pinentry_t
236 * \brief Local pinentry commands and not pwmd pinentry.
238 * These determine what prompt a local or fork()'ed pinentry uses. For use
239 * with \ref pwmd_getpin().
241 typedef enum
243 /*! When opening a file. */
244 PWMD_PINENTRY_OPEN,
246 /*! When opening a file failed due to a bad passphrase. */
247 PWMD_PINENTRY_OPEN_FAILED,
249 /*! When saving a file. */
250 PWMD_PINENTRY_SAVE,
252 /*! For passphrase confirmation. */
253 PWMD_PINENTRY_SAVE_CONFIRM,
255 /*! When saving a file and passphrase confirmation failed. */
256 PWMD_PINENTRY_SAVE_FAILED,
258 /*! For confirmation of a user-defined prompt. */
259 PWMD_PINENTRY_CONFIRM,
261 /*! For the default or user defined string set with \ref
262 * PWMD_OPTION_PINENTRY_DESC. */
263 PWMD_PINENTRY_USER,
265 /*! To terminate the pinentry process created with \ref pwmd_getpin(). */
266 PWMD_PINENTRY_CLOSE
267 } pwmd_pinentry_t;
270 /*! \typedef pwmd_status_cb_t
271 * \brief Process status messages.
273 * The value of the option \ref PWMD_OPTION_STATUS_CB which is set with \ref
274 * pwmd_setopt().
276 * \param user A user data pointer which is set with \ref
277 * PWMD_OPTION_STATUS_DATA.
278 * \param line The status message line received from the server.
279 * \return 0 on success or an error code which will cause a command to fail.
281 typedef gpg_error_t (*pwmd_status_cb_t) (void *user, const char *line);
284 /*! \typedef pwmd_inquire_cb_t
285 * \brief Send data to the pwmd server.
287 * This is a callback function that is used for sending data to the server for
288 * commands that need to reply to an INQUIRE server response. The reason for
289 * this callback is to let the client send as many bytes as it wants rather
290 * than the entire data at once. It gets called during an internal \ref
291 * assuan_transact() from an internal inquire callback function which in turn
292 * calls this function by looping over its return value.
294 * \param user The user data pointer passed to the libpwmd function specifying
295 * this callback.
296 * \param keyword The name of this inquire. Could be a command name or some
297 * keyword describing what needs to be sent. See the pwmd and \ref
298 * gpg-agent(1) documentation for details.
299 * \param rc The result of the last internal call to \ref assuan_send_data()
300 * which did the sending of the data to the pwmd server. On the first call to
301 * this callback it's value will always be 0 since no data has been sent yet.
302 * This should be checked during each call to this function and should return
303 * the same error code when set. Its purpose is to let the client clean up
304 * before letting the command fail.
305 * \param[out] data The next chunk of data to send or NULL.
306 * \param[out] len The length of \a data or 0.
308 * \retval 0 There is more data to be sent.
309 * \retval GPG_ERR_EOF No need to call this function again, the current
310 * \a data is the last to send.
311 * \retval code Any other error code which will cancel the INQUIRE.
313 * \note The sent data is processed line-per-line. The line is either newline
314 * terminated or is buffered until ASSUAN_LINELENGTH bytes have been
315 * allocated. Any remaining bytes are sent after the INQUIRE has finished.
317 typedef gpg_error_t (*pwmd_inquire_cb_t) (void *user, const char *keyword,
318 gpg_error_t rc, char **data,
319 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,
348 size_t len);
351 /*! \enum pwmd_option_t
352 * \brief libpwmd options.
354 * Options are set with \ref pwmd_setopt(). Some options must be set before a
355 * connection is made to have any effect.
357 * \filepath
359 /* Note to self: dont change the order or insert. Only append. */
360 typedef enum
362 /*! A string value which specifies the full path of the \ref pinentry(1)
363 * binary. The default is specified at compile time.
365 * \note This only affects the local pinentry.
366 * \see \ref pinentry
368 PWMD_OPTION_PINENTRY_PATH,
370 /*! A string value which specifies the full path to the TTY that \ref
371 * pinentry(1) will use to prompt on. When set and no DISPLAY is
372 * available, \ref PWMD_OPTION_PINENTRY_TERM must also be set.
374 * \see \ref pinentry
376 PWMD_OPTION_PINENTRY_TTY,
378 /*! A string value which specifies the terminal type (i.e., vt100) that
379 * \ref pinentry(1) will use when no X11 display is available.
381 * \see \ref pinentry
383 PWMD_OPTION_PINENTRY_TERM,
385 /*! A string value which specifies the X11 display that \ref pinentry(1)
386 * will use. \ref pinentry(1) seems to make DISPLAY have a higher priority
387 * than \ref PWMD_OPTION_PINENTRY_TTY.
389 * \see \ref pinentry
391 PWMD_OPTION_PINENTRY_DISPLAY,
393 /*! A character string that \ref pinentry(1) will use in it's dialog
394 * window.
396 PWMD_OPTION_PINENTRY_ERROR,
398 /*! \copydoc PWMD_OPTION_PINENTRY_ERROR */
399 PWMD_OPTION_PINENTRY_PROMPT,
401 /*! \copydoc PWMD_OPTION_PINENTRY_ERROR */
402 PWMD_OPTION_PINENTRY_DESC,
404 /*! For \ref pinentry(1) localization. */
405 PWMD_OPTION_PINENTRY_LC_CTYPE,
407 /*! \copydoc PWMD_OPTION_PINENTRY_LC_CTYPE */
408 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,
418 /*! An integer value that specifies the number of times to prompt for a
419 * passphrase before returning an error.
421 * \note This only affects the local pinentry.
423 PWMD_OPTION_PINENTRY_TRIES,
425 /*! A function of type \ref pwmd_status_cb_t that will process status
426 * messages received from the pwmd server.
428 PWMD_OPTION_STATUS_CB,
430 /*! A user data pointer which is passed to the status message function
431 * \ref PWMD_OPTION_STATUS_CB. */
432 PWMD_OPTION_STATUS_DATA,
434 /*! A function of type \ref pwmd_knownhost_cb_t that will be used to
435 * confirm a host key that was not found in the known hosts file.
437 * \see \ref remote
439 PWMD_OPTION_KNOWNHOST_CB,
441 /*! User supplied data which is passed to the known host function \ref
442 * PWMD_OPTION_KNOWNHOST_CB.
444 * \see \ref remote
446 PWMD_OPTION_KNOWNHOST_DATA,
448 /*! When the total number of bytes to be sent via an INQUIRE is known,
449 * this should be set so XFER status messages can be parsed correctly.
450 * When not known or unset, 0 is used as the total argument to the XFER
451 * status message. This option should be reset in every function that
452 * uses an \ref pwmd_inquire_cb_t.
454 * \note During the INQUIRE, \ref PWMD_OPTION_STATUS_CB is called, when
455 * set, after every iteration of the \ref pwmd_inquire_cb_t.
457 * \note This is a libpwmd feature. pwmd itself does not send XFER status
458 * messages during an INQUIRE. Status messages can be parsed only when
459 * \ref PWMD_OPTION_STATUS_CB is set.
461 PWMD_OPTION_INQUIRE_TOTAL,
463 /*! When set to 1, lock the file mutex after opening a file as if the LOCK
464 * command had been sent.
466 PWMD_OPTION_LOCK_ON_OPEN,
468 /*! Use ssh-agent to retrieve the private key to authenticate when
469 * connecting to a remote pwmd server. */
470 PWMD_OPTION_SSH_AGENT,
472 /*! When 1, disable pinentry use. This will prevent both pwmd and libpwmd
473 * from using a pinentry program and will prompt from a terminal if
474 * available.
476 * \note This must be set before calling \ref pwmd_open().
477 * \see \ref PWMD_OPTION_OVERRIDE_INQUIRE.
479 PWMD_OPTION_NO_PINENTRY,
481 /*! When 1, libpwmd will disable pwmd's pinentry and use it's own
482 * fork(2)'ed pinentry.
484 * \note This must be set before calling \ref pwmd_open().
486 PWMD_OPTION_LOCAL_PINENTRY,
488 /*! When set, override libpwmd's handling of server inquires with the
489 * PASSPHRASE and NEW_PASSPHRASE keywords. Handling of these keywords is
490 * done automatically when \ref PWMD_OPTION_NO_PINENTRY or \ref
491 * PWMD_OPTION_LOCAL_PINENTRY is set or when the connection is a remote
492 * one.
494 * \see \ref remote, \ref PWMD_OPTION_LOCAL_PINENTRY, \ref pwmd_password()
496 PWMD_OPTION_OVERRIDE_INQUIRE,
498 /*! An int specifying a timeout in seconds before a TCP connection or
499 * transfer function will timeout causing a connection or command to
500 * fail. This option can be specified both before and after a connection
501 * has been established. When 0, no timeout is used.
503 PWMD_OPTION_SOCKET_TIMEOUT,
505 /*! An int specifying whether to enable TLS hostname verification on the
506 * server certificate chain. Default is 0 or disabled. */
507 PWMD_OPTION_TLS_VERIFY,
509 /*! Set to 1 to not modify the signal handler to ignore SIGPIPE. The default
510 * is 0, or to ignore this signal. */
511 PWMD_OPTION_SIGPIPE,
513 /*! Get the version of the pwmd server as an unsigned integer. This is a
514 * read-only setting and is only available after connecting. */
515 PWMD_OPTION_SERVER_VERSION,
516 } pwmd_option_t;
519 /*! \def PWMD_FEATURE_PINENTRY
520 * \hideinitializer
521 * \brief Pinentry support.
523 * This is for a local or fork()'ed pinentry.
525 #define PWMD_FEATURE_PINENTRY 0x0001
527 /*! \def PWMD_FEATURE_CRACK
528 * \hideinitializer
529 * \brief Password quality checking.
531 * A password quality meter is shown when the pinentry supports it.
533 #define PWMD_FEATURE_CRACK 0x0002
535 /*! \def PWMD_FEATURE_SSH
536 * \hideinitializer
537 * \brief Remote connections over an SSH channel.
539 * \see \ref remote
541 #define PWMD_FEATURE_SSH 0x0004
543 /*! \def PWMD_FEATURE_GNUTLS
544 * \hideinitializer
545 * \brief Remote connections over TLS.
547 * \see \ref remote
549 #define PWMD_FEATURE_GNUTLS 0x0008
552 /*! \brief libpwmd compile time features.
554 * Useful for clients to determine what features are compiled into libpwmd at
555 * runtime.
557 * \return A bitmask of features.
559 LIBPWMD_API unsigned int pwmd_features ();
562 /*! \brief Initialize the library.
564 * This function must be the first function called in the library before any
565 * others. It sets up the memory allocators and internationalization among
566 * other things.
568 * \return 0 on success or an error code.
570 LIBPWMD_API gpg_error_t pwmd_init (void);
572 /*! \brief Deinitialize the library.
574 * This function is mainly for cleaning up other libraries that
575 * libpwmd links with to prevent memory and other leaks showing up in
576 * a debugger. It should be the final libpwmd function call before
577 * your app exits.
579 LIBPWMD_API void pwmd_deinit (void);
582 /*! \brief Creates a new handle.
584 * Creates a new handle for use with the other functions.
586 * \param name If not NULL, the name of the application. The application name
587 * is sent to the pwmd server after successfully connecting and is used in
588 * pwmd logging.
589 * \param[in] pwm A new handle for use with the other functions.
591 * \return 0 on success or an error code.
593 LIBPWMD_API gpg_error_t pwmd_new (const char *name, pwm_t ** pwm);
596 /*! \brief Set user data for a handle.
598 * Use this function to associate user data with a handle that can later be
599 * retrieved with \ref pwmd_get_pointer().
601 * \param pwm A handle.
602 * \param data A pointer to the user data.
603 * \returns Nothing.
605 LIBPWMD_API void pwmd_set_pointer (pwm_t *pwm, void *data);
608 /*! \brief Get user data for a handle.
610 * Return the user data pointer previously set with \ref pwmd_set_pointer().
612 * \param pwm A handle.
613 * \returns A pointer to the user data.
615 LIBPWMD_API void *pwmd_get_pointer (pwm_t *pwm);
618 /*! \brief Establish a connection to a pwmd server.
620 * Connects to the pwmd server specified in \a url. The format of \a url is:
621 * \par
622 * file:///path/to/socket, or
623 * \par
624 * ssh[46]://[username@]hostname[:port], or
625 * \par
626 * tls[46]://hostname[:port]
628 * If \a url is NULL then the default local pwmd socket \a ~/.pwmd/socket
629 * will be used.
631 * The remaining arguments are parameters for the \a url.
633 * For SSH connections, the first of the positional parameters should be the
634 * identity file to use and is required to be non-NULL unless \ref
635 * PWMD_OPTION_SSH_AGENT is set. The final parameter is the known hosts file
636 * to use or NULL to use a default of \a ~/.ssh/knownhosts.
638 * For TLS connections, the first positional parameter should be the client
639 * certificate filename. The second parameter should be the client
640 * certificate key filename. The third parameter should be the Certificate
641 * Authority (CA) file that was used to sign the server certificate. The
642 * fourth parameter should be a TLS cipher priority string or NULL to use the
643 * default of "SECURE256". The final parameter is an SHA-256 hash of the
644 * server fingerprint or NULL.
646 * For local connections, any remaining parameters are ignored.
648 * \param pwm A handle.
649 * \param url The socket to connect to.
650 * \param ... Remaining parameters for the \a url.
651 * \return 0 on success or an error code.
652 * \filepath
653 * \see \ref PWMD_OPTION_SSH_AGENT, \ref PWMD_OPTION_SOCKET_TIMEOUT,
654 * pwmd_socket_type(), pwmd_disconnect(),
655 * \ref remote
657 LIBPWMD_API gpg_error_t pwmd_connect (pwm_t * pwm, const char *url, ...);
659 /*! \brief Get the error code of a failed TLS function.
661 * When an TLS error occurs while connecting or during a command, this function
662 * can be used to get the error code.
664 * \param pwm A handle.
665 * \return 0 on success or an \ref gnutls error code.
667 LIBPWMD_API int pwmd_tls_error (pwm_t * pwm);
669 /*! \brief Set handle options.
671 * See \ref pwmd_option_t for option specific details.
673 * \param pwm A handle.
674 * \param opt The option.
675 * \param ... The option value.
676 * \return 0 on success or an error code.
678 LIBPWMD_API gpg_error_t pwmd_setopt (pwm_t * pwm, pwmd_option_t opt, ...);
681 /*! \brief Get the value for a handle option.
683 * Retrieves the default or previously set value for a handle option. See
684 * \ref pwmd_option_t for option specific details.
686 * \param pwm A handle.
687 * \param opt The option.
688 * \param ... A pointer to the option value type to store the value.
689 * \return 0 on success or an error code.
690 * \note The value is returned as a pointer and not duplicated.
692 LIBPWMD_API gpg_error_t pwmd_getopt (pwm_t * pwm, pwmd_option_t opt, ...);
695 /*! \brief Launch a local pinentry.
697 * Does not send any command to the pwmd server. This maybe useful if a
698 * passphrase is needed while opening a file over a remote connection and
699 * during an \ref pwmd_open() server inquire.
701 * This function may also be used to display a user confirmation dialog with
702 * pinentry when \a which is \ref PWMD_PINENTRY_CONFIRM. The text to prompt
703 * with is set with \ref PWMD_OPTION_PINENTRY_DESC.
705 * \param pwm A handle.
706 * \param filename The filename to use in the pinentry dialog strings.
707 * \param[in] result The entered value in the pinentry dialog which should be
708 * freed with \ref pwmd_free().
709 * \param[in] len The length of \a result.
710 * \param which Determines the default strings shown in the pinentry
711 * dialog. \ref pwmd_setopt() may also be used to override the defaults. In
712 * this case \ref PWMD_PINENTRY_USER should be used. \ref PWMD_PINENTRY_CLOSE
713 * should be used to terminate the pinentry process when the pinentry is no
714 * longer needed.
716 * \return 0 on success or an error.
718 * \see pwmd_password()
720 LIBPWMD_API gpg_error_t pwmd_getpin (pwm_t * pwm, const char *filename,
721 char **result, size_t * len,
722 pwmd_pinentry_t which);
724 /*! \brief Obtain a passphrase from a local pinentry.
726 * This is the same function that libpwmd uses during an inquire when using
727 * the local pinentry and the inquire keyword is either PASSPHRASE or
728 * NEW_PASSPHRASE. Provided for convenience since it sets proper pinentry
729 * strings and handles new passphrase confirmation.
731 * \param pwm A handle.
732 * \param keyword The keyword to determine pinentry strings. Usually
733 * PASSPHRASE or NEW_PASSPHRASE.
734 * \param[in] data The obtained passphrase which should be freed with \ref
735 * pwmd_free().
736 * \param[in] size The length of \a data.
738 * \return 0 on success or an error.
740 LIBPWMD_API gpg_error_t pwmd_password (pwm_t * pwm, const char *keyword,
741 char **data, size_t * size);
743 /*! \brief Open a file on the pwmd server.
745 * This will send the OPEN command to the server.
747 * The inquire \a callback function should be used when \ref
748 * PWMD_OPTION_NO_PINENTRY is set and is used to send the passphrase when
749 * needed.
751 * \param pwm A handle.
752 * \param filename The filename to open. The \a filename is not a full path
753 * but the data filename only.
754 * \param callback A callback function to invoke when pwmd inquires data from
755 * the client.
756 * \param data User data passed to the \a callback function.
757 * \return 0 on success or an error code.
758 * \see \ref PWMD_OPTION_OVERRIDE_INQUIRE, \ref pinentry, pwmd_password()
760 LIBPWMD_API gpg_error_t pwmd_open (pwm_t * pwm, const char *filename,
761 pwmd_inquire_cb_t callback, void *data);
764 /*! \brief Check for socket activity.
766 * This function should be called periodically to check for any pending status
767 * messages sent from the server and when \em not in a command.
769 * \param pwm A handle.
770 * \return 0 on success or an error code.
772 * \note This function makes use of \ref pwmd_status_cb_t.
774 LIBPWMD_API gpg_error_t pwmd_process (pwm_t * pwm);
777 /*! \brief Save a file on the pwmd server.
779 * This will send the SAVE command and write any changes to the document to
780 * disk.
782 * The inquire \a callback function should be used when \ref
783 * PWMD_OPTION_NO_PINENTRY is set or when a SAVE option specified in \a args
784 * inquires more data. This function will also disable the pwmd pinentry when
785 * \ref PWMD_OPTION_NO_PINENTRY or \ref PWMD_OPTION_LOCAL_PINENTRY is set. It
786 * will not restore the pwmd pinentry mode that was previously set before
787 * calling this function.
789 * \param pwm A handle.
790 * \param args Any SAVE protocol command options or NULL.
791 * \param callback A callback function to invoke when pwmd inquires data from
792 * the client.
793 * \param user User data passed to the \a callback function.
794 * \return 0 on success or an error code.
795 * \see \ref PWMD_OPTION_OVERRIDE_INQUIRE, \ref PWMD_OPTION_NO_PINENTRY,
796 * \ref PWMD_OPTION_LOCAL_PINENTRY, \ref pwmd_command(), \ref pinentry
798 LIBPWMD_API gpg_error_t pwmd_save (pwm_t * pwm, const char *args,
799 pwmd_inquire_cb_t callback, void *user);
802 /*! \brief Change the passphrase for a data file.
804 * This will send the PASSWD command to the server taking care of pinentry
805 * settings.
807 * The inquire \a callback function should be used when \ref
808 * PWMD_OPTION_OVERRIDE_INQUIRE is set. This function will disable the pwmd
809 * pinentry when \ref PWMD_OPTION_NO_PINENTRY or \ref
810 * PWMD_OPTION_LOCAL_PINENTRY is set. It will not restore the pwmd pinentry
811 * mode that was previously set before calling this function.
813 * \param pwm A handle.
814 * \param args Any PASSWD protocol command options or NULL.
815 * \param callback A callback function to invoke when pwmd inquires data from
816 * the client.
817 * \param user User data passed to the \a callback function.
818 * \return 0 on success or an error code.
819 * \see \ref PWMD_OPTION_OVERRIDE_INQUIRE, \ref PWMD_OPTION_NO_PINENTRY,
820 * \ref PWMD_OPTION_LOCAL_PINENTRY, \ref pwmd_command(), \ref pinentry
822 LIBPWMD_API gpg_error_t pwmd_passwd (pwm_t * pwm, const char *args,
823 pwmd_inquire_cb_t callback, void *user);
826 /*! \brief Send a command to the pwmd server.
828 * You should avoid sending the BYE command here because the assuan context
829 * will be freed and bad things will happen. Use \ref pwmd_close() instead.
831 * For pwmd commands that use an INQUIRE to retrieve data from a client, the
832 * \a callback parameter must be non-NULL and will be used to send the data to
833 * pwmd.
835 * \param pwm A handle.
836 * \param[in] result The result of the command when successful which must be
837 * freed with \ref pwmd_free().
838 * \param[in] len The length of \a result.
839 * \param callback A callback function to invoke when pwmd inquires data from
840 * the client.
841 * \param user User data passed to the \a callback function.
842 * \param cmd The command to send and any following arguments.
843 * \return 0 on success or an error code.
845 * \note Not all commands return a \a result.
847 LIBPWMD_API gpg_error_t pwmd_command (pwm_t * pwm, char **result,
848 size_t * len,
849 pwmd_inquire_cb_t callback,
850 void *user, const char *cmd, ...);
853 /*! \brief Send a command to the pwmd server.
855 * Like \ref pwmd_command() but uses an argument pointer instead.
857 * \param pwm A handle.
858 * \param[in] result The result of the command when successful which must be
859 * freed with \ref pwmd_free().
860 * \param[in] len The length of \a result.
861 * \param callback A callback function to invoke when pwmd inquires data from
862 * the client.
863 * \param user User data passed to the \a callback function.
864 * \param cmd The command to send.
865 * \param ap The arguments to \a cmd.
866 * \return 0 on success or an error code.
868 * \note Not all commands return a \a result.
870 LIBPWMD_API gpg_error_t pwmd_command_ap (pwm_t * pwm, char **result,
871 size_t * len,
872 pwmd_inquire_cb_t callback,
873 void *user, const char *cmd,
874 va_list ap);
877 /*! \brief Close a connection to the pwmd server.
879 * This will close the connection but keep any previously set options for the
880 * specified handle. Calling \ref pwmd_connect() will re-acquire an libassuan
881 * context.
883 * \param pwm A handle.
884 * \return 0 on success or an error code.
885 * \see \ref pwmd_close()
887 LIBPWMD_API gpg_error_t pwmd_disconnect (pwm_t * pwm);
890 /*! \brief Close a handle.
892 * This will close the connection to a pwmd server and free any resources
893 * associated with it.
895 * \param pwm A handle.
896 * \return Nothing.
897 * \see \ref pwmd_disconnect(), \ref pwmd_new()
899 LIBPWMD_API void pwmd_close (pwm_t * pwm);
902 /*! \brief The type of connection a handle has.
904 * Useful when you want to know what kind of connection a handle has.
906 * \param pwm A handle.
907 * \param[out] type The type of socket.
908 * \return 0 on success or an error code.
910 * \see \ref pwmd_socket_t
912 LIBPWMD_API gpg_error_t pwmd_socket_type (pwm_t * pwm,
913 pwmd_socket_t * type);
915 /*! \brief Get the file descriptor associated with a handle.
917 * May be useful to determine whether a handle is ready for reading or
918 * writing by using select(2) or poll(2).
920 * \param pwm A handle.
921 * \param[out] fd Set to the file descriptor associated with \a pwm.
922 * \return 0 on success or an error code.
924 LIBPWMD_API gpg_error_t pwmd_fd (pwm_t * pwm, int *fd);
927 /*! \brief Free a previously allocated pointer.
929 * Use this function to free resources allocated by the other libpwmd memory
930 * functions. Do not use it to free allocations made by other allocators.
932 * The difference between the standard free() and this function is that
933 * this one will zero out the contents of the pointer before freeing it.
935 * \param ptr The pointer to deallocate.
936 * \return Nothing.
937 * \see pwmd_malloc(), pwmd_calloc(), pwmd_realloc(), pwmd_strdup(),
938 * pwmd_command()
940 LIBPWMD_API void pwmd_free (void *ptr);
943 /*! \brief A wrapper around malloc.
945 * Like malloc(), but lets libpwmd keep track of the pointer.
947 * \param size The number of bytes to allocate.
948 * \return A newly allocated pointer or NULL if there wasn't enough memory.
949 * \see malloc(3), pwmd_free()
951 LIBPWMD_API void *pwmd_malloc (size_t size);
954 /*! \brief A wrapper around calloc().
956 * Like calloc(), but lets libpwmd keep track of the pointer.
958 * \param nmemb The number of elements to allocate.
959 * \param size The number of bytes to allocate.
960 * \return A newly allocated pointer or NULL if there wasn't enough memory.
961 * \see calloc(3), pwmd_free()
963 LIBPWMD_API void *pwmd_calloc (size_t nmemb, size_t size);
966 /*! \brief A wrapper around realloc().
968 * Like realloc(), but lets libpwmd keep track of the pointer.
970 * \param ptr The pointer to reallocate.
971 * \param size The new number of bytes to allocate.
972 * \return A newly allocated pointer or NULL if there wasn't enough memory.
973 * \see realloc(3), pwmd_free()
975 LIBPWMD_API void *pwmd_realloc (void *ptr, size_t size);
978 /*! \brief A wrapper around strdup().
980 * Like strdup(), but lets libpwmd keep track of the pointer.
982 * \param str The string to duplicate.
983 * \return A newly allocated character pointer or NULL if there wasn't
984 * enough memory.
985 * \see strdup(3), pwmd_free()
987 LIBPWMD_API char *pwmd_strdup (const char *str);
990 /*! \brief Duplicate a formatted string.
992 * Like \ref asprintf(3), but lets libpwmd keep track of the pointer.
994 * \param fmt The formatted string and any following arguments.
995 * \return A newly allocated character pointer or NULL if there wasn't
996 * enough memory.
997 * \see pwmd_free()
999 LIBPWMD_API char *pwmd_strdup_printf (const char *fmt, ...);
1001 /*! \brief Convenience declaration.
1003 * This allows an application wanting both a TLS connection and libpwmd secure
1004 * memory management in GnuTLS, not needing to have the GnuTLS development
1005 * files installed. This function should be called before \ref pwmd_init().
1007 * It is not done in \ref pwmd_init() itself since the application may have
1008 * already initialized GnuTLS by some other means.
1010 extern void gnutls_global_set_mem_functions (
1011 // gnutls_alloc_function
1012 void *(*)(size_t),
1013 // gnutls_alloc_function (secure)
1014 void *(*)(size_t),
1015 // gnutls_is_secure_function
1016 int (*)(const void *),
1017 // gnutls_realloc_function
1018 void *(*)(void *, size_t),
1019 // gnutls_free_function
1020 void (*)(void *));
1022 #ifdef __cplusplus
1024 #endif
1026 #endif