Revert "Added support for a passphrase in the url string."
[libpwmd.git] / src / libpwmd.h.in
blob9ffb36588d3a768c9ea0c43bc66d57c7ddd01405
1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
2 /*
3 Copyright (C) 2006-2009 Ben Kibbey <bjk@luxsci.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02110-1301 USA
19 /*! \headerfile libpwmd.h
21 * libpwmd is a library making it easy for applications to use the pwmd
22 * server.
25 /*! \section ssh SSH Details
27 * A remote connection to a pwmd server is possible by using an SSH channel
28 * which spawns a shell and executes a proxy server that connects to the pwmd
29 * local unix domain socket. Authenication is done by using SSH public key
30 * (see \ref ssh-keygen(1)) authentication and verifying the host key against
31 * a local file containing SHA1 hashes of known hosts. It's alot like how the
32 * standard OpenSSH does things only the known_hosts file is in a different
33 * format.
35 * The server hash can be had by using \ref pwmd_get_hostkey() and storing the
36 * result in a file. This file is then used as the \a known_hosts argument to
37 * the SSH connection functions.
39 * Here's an example \ref authorized_keys(5) entry. The hash portion should be
40 * the same as the contents of the \a identity.pub file which is passed as a
41 * parameter to the SSH connection functions:
43 * \code
44 * command="socat gopen:$HOME/.pwmd/socket -" <hash> ...
45 * \endcode
47 * \todo X11 port forwarding so a remote pinentry can use the local display.
50 /*! \section pinentry Pinentry Details
52 * \ref pinentry(1) is a program that prompts the user for input which is
53 * normally a passphrase or a confirmation. libpwmd can use this program
54 * either locally (the connection is to a remote server not on this host) or
55 * have the pwmd server use it's pinentry to retrieve a passphrase when
56 * needed.
58 * There are a few options that tell pinentry how and where to prompt for a
59 * passphrase. See the \ref pwmd_option_t section for details. These options
60 * are not sent (when using pwmd's pinentry) until the pinentry is needed.
62 * If using a local pinentry by calling \ref pwmd_open2(), \ref pwmd_save2(),
63 * \ref pwmd_open_async2() or pwmd_save_async2(), libpwmd will send the
64 * command "OPTION PINENTRY=0" to the server. This is needed for pinentry
65 * retries (passphrase or confirmation failure). So if you need to change
66 * pinentry methods, then set this option as needed.
68 * Some pinentry options can also be specified in a local configuration file
69 * \a "~/.pwmd/pinentry.conf". These options are initial values for each
70 * invokation and may be changed by setting the appropriate \ref
71 * pwmd_option_t. Each option and value is separated with a '=' on a single
72 * line. Unrecognized options are ignored. Here are the recognized options:
74 * \param PATH The full path to the location of the pinentry binary.
75 * \param DISPLAY The X11 display to use.
76 * \param TTYNAME The full path to the tty that pinentry should prompt on.
77 * \param TTYTYPE The terminal type of the tty which is required if DISPLAY is
78 * not set.
80 * \see \ref ssh
83 /*! \section example Example Client
85 * The following example will list the element tree of the data file specified
86 * in the first command line argument.
88 * \code
89 * #include <stdio.h>
90 * #include <libpwmd.h>
92 * int main()
93 * {
94 * pwm_t *pwm = pwmd_new(NULL);
95 * gpg_error_t rc = pwmd_connect(pwm, NULL);
96 * char *result;
98 * if (!rc) {
99 * rc = pwmd_open(pwm, argv[1]);
101 * if (!rc) {
102 * rc = pwmd_command(pwm, &result, "%s", "LIST");
104 * if (!rc) {
105 * printf("%s", result);
106 * pwmd_free(result);
111 * pwmd_close(pwm);
113 * if (rc)
114 * fprintf(stderr, "ERR: %s\n", pwmd_strerror(rc));
116 * exit(rc ? 1 : 0);
118 * \endcode
121 /*! \file */
122 #ifndef LIBPWMD_H
123 #define LIBPWMD_H
125 #include <gpg-error.h>
126 #include <stdarg.h>
128 #ifdef __cplusplus
129 extern "C" {
130 #endif
132 /*! \def LIBPWMD_VERSION
133 * \hideinitializer
135 * The version of this library.
137 #define LIBPWMD_VERSION 0x@VER_MAJOR@@VER_COMPAT@@VER_PATCH@
140 struct pwm_s;
141 /*! \typedef pwm_t
143 * When a handle is mentioned in this documentation it is a pointer of this
144 * type. A new handle is created with \ref pwmd_new().
146 typedef struct pwm_s pwm_t;
149 /*! \typedef pwmd_async_t
151 * The return code of \ref pwmd_process() which is used for all asynchronous
152 * commands.
154 typedef enum {
155 /*! \internal */
156 ASYNC_INIT,
158 /*! \ref pwmd_process() should be called again. */
159 ASYNC_PROCESS,
161 /*! The command has completed. The result code should be checked for an
162 * error. */
163 ASYNC_DONE,
164 } pwmd_async_t;
167 /*! \typedef pwmd_ip_version_t
169 * The value of the option \ref PWMD_OPTION_IP_VERSION which is set with \ref
170 * pwmd_setopt().
172 typedef enum {
173 /*! Try both IPv6 and IPv4 addresses. Note that IPv6 is tried first. */
174 PWMD_IP_ANY,
176 /*! Only try IPv4. */
177 PWMD_IPV4,
179 /*! Only try IPv6. */
180 PWMD_IPV6
181 } pwmd_ip_version_t;
184 /*! \def PWMD_FD_READABLE
185 * \hideinitializer
187 * Set when the file descriptor is readable.
189 #define PWMD_FD_READABLE 0x01
192 /*! \def PWMD_FD_WRITABLE
193 * \hideinitializer
195 * Set when the file descriptor is writable.
197 #define PWMD_FD_WRITABLE 0x02
200 /*! \typedef pwmd_fd_t
202 * For use with \ref pwmd_get_fds().
204 typedef struct {
205 /*! The file descriptor. */
206 int fd;
208 /*! A bitmask of \ref PWMD_FD_READABLE and \ref PWMD_FD_WRITABLE. */
209 unsigned flags;
210 } pwmd_fd_t;
213 /*! \typedef pwmd_passphrase_cb_t
215 * The value of the option \ref PWMD_OPTION_PASSPHRASE_CB which is set with
216 * \ref pwmd_setopt().
218 * \param user A user data pointer which is set with \ref
219 * PWMD_OPTION_PASSPHRASE_DATA.
220 * \param[out] passphrase The passphrase which may be an empty string or NULL.
221 * \return 0 on success or an error code which will cause a command to fail.
223 typedef gpg_error_t (*pwmd_passphrase_cb_t)(void *user, char **passphrase);
226 /*! \typedef pwmd_status_cb_t
228 * The value of the option \ref PWMD_OPTION_STATUS_CB which is set with \ref
229 * pwmd_setopt().
231 * \param user A user data pointer which is set with \ref
232 * PWMD_OPTION_STATUS_DATA.
233 * \param line The status message line.
234 * \return 0 on success or an error code which will cause a command to fail.
236 typedef int (*pwmd_status_cb_t)(void *user, const char *line);
239 /*! \typedef pwmd_inquire_cb_t
241 * This is a callback function that gets passed to \ref pwmd_inquire(). It is
242 * used for sending data to the server for commands that return an INQUIRE
243 * response (STORE and IMPORT). The reason for this callback is to let the
244 * client send as many bytes as it wants rather than the entire chunk at once.
245 * It gets called during \ref assuan_transact() from an internal inquire
246 * callback function which in turn calls this function by looping over its
247 * return value.
249 * \param user The user data pointer passed to \ref pwmd_inquire().
250 * \param cmd The same as the \a cmd argument to \ref pwmd_inquire().
251 * \param rc The result of the last internal call to \ref assuan_send_data()
252 * which did the sending of the data to the pwmd server. On the first call to
253 * this callback it will always be 0 because no data has been sent yet.
254 * \param[out] data The next chunk of data to send or NULL.
255 * \param[out] len The length of \a data or 0.
257 * \retval 0 There is more data to be sent.
258 * \retval GPG_ERR_EOF No need to call this function again, the current
259 * \a line is the last to send.
260 * \retval code Any other error code which will terminate the INQUIRE.
262 * \note The sent data is processed line-per-line. The line is either newline
263 * terminated or is buffered until ASSUAN_LINELENGTH bytes have been
264 * allocated. Any remaining bytes are sent after the INQUIRE has finished.
266 typedef gpg_error_t (*pwmd_inquire_cb_t)(void *user, const char *cmd,
267 gpg_error_t rc, char **data, size_t *len);
270 /*! \enum pwmd_option_t
272 * libpwmd options which are set with \ref pwmd_setopt().
274 typedef enum {
275 /*! A custom passphrase retrieval function which, when set, will be used
276 * instead of \ref pinentry(1). This function will not be used when the
277 * passphrase is cached on the server or the file is a new one. The value
278 * of this option should be a \ref pwmd_passphrase_cb_t function pointer.
280 PWMD_OPTION_PASSPHRASE_CB,
282 /*! User supplied data which is passed to the custom password function. */
283 PWMD_OPTION_PASSPHRASE_DATA,
285 /*! A string to use as the passphrase when doing an open or save. When not
286 * NULL, this option has precedence over \ref PWMD_OPTION_PASSPHRASE_CB.
288 PWMD_OPTION_PASSPHRASE,
290 /*! An integer value that specifies the specified the number of tries
291 * before \ref pinentry(1) will give up when opening a file with the wrong
292 * supplied passphrase. The default is 3.
294 * \note This option has no effect when trying to save a file. The user
295 * must either cancel the pinentry causing the save to fail or enter the
296 * correct passphrase during passphrase confirmation.
298 PWMD_OPTION_PINENTRY_TRIES,
300 /*! A character string value which specifies the full path of the \ref
301 * pinentry(1) binary. For the local \ref pinentry(1) method, the default
302 * is specified at compile time.
304 * \see \ref pinentry
306 PWMD_OPTION_PINENTRY_PATH,
308 /*! A value which specifies the full path to the tty that \ref pinentry(1)
309 * will use. When set and no DISPLAY is available, \ref
310 * PWMD_OPTION_PINENTRY_TERM must also be set.
312 * \see \ref pinentry
314 PWMD_OPTION_PINENTRY_TTY,
316 /*! A value which specifies the terminal type (e.g., vt100) that \ref
317 * pinentry(1) will use when no X11 display is available.
319 * \see \ref pinentry
321 PWMD_OPTION_PINENTRY_TERM,
323 /*! A value which specifies the X11 display that \ref pinentry(1) will
324 * use.
326 * \see \ref pinentry
328 PWMD_OPTION_PINENTRY_DISPLAY,
330 /*! A character string that \ref pinentry(1) will use in it's dialog
331 * window.
333 PWMD_OPTION_PINENTRY_TITLE,
335 /*! \copydoc PWMD_OPTION_PINENTRY_TITLE */
336 PWMD_OPTION_PINENTRY_PROMPT,
338 /*! \copydoc PWMD_OPTION_PINENTRY_TITLE */
339 PWMD_OPTION_PINENTRY_DESC,
341 /*! For \ref pinentry(1) localization. */
342 PWMD_OPTION_PINENTRY_LC_CTYPE,
344 /*! \copydoc PWMD_OPTION_PINENTRY_LC_CTYPE */
345 PWMD_OPTION_PINENTRY_LC_MESSAGES,
347 /*! An integer value that specifies the number of seconds \ref pinentry(1)
348 * will wait for input before timing out and aborting the current command.
349 * If 0, then no timeout will be used. The default is 30.
351 PWMD_OPTION_PINENTRY_TIMEOUT,
353 /*! A function pointer of type \ref pwmd_status_cb_t that will process
354 * status messages received from the pwmd server.
356 PWMD_OPTION_STATUS_CB,
358 /*! A user data pointer which is passed to the status message function. */
359 PWMD_OPTION_STATUS_DATA,
361 /*! The IP version of type \ref pwmd_ip_version_t that \ref
362 * pwmd_ssh_connect() and \ref pwmd_ssh_connect_async() will use when
363 * connecting to the remote pwmd server.
365 * \pre_conn_req
367 PWMD_OPTION_IP_VERSION,
368 } pwmd_option_t;
371 /*! \brief Initialize the library.
373 * This function must be the first function called in the library before any
374 * others. It sets up internationalization among other things.
376 * \return 0 Success.
378 gpg_error_t pwmd_init(void);
381 /*! \brief Creates a new handle.
383 * Creates a new handle for use with the other functions.
385 * \param name If not NULL, the name of the application. The application name
386 * is sent to the pwmd server after successfully connecting.
388 * \param name The application name or NULL.
389 * \return a new handle or NULL if there was not enough memory.
391 pwm_t *pwmd_new(const char *name)
392 __attribute__ ((warn_unused_result));
395 /*! \brief Connect to a local pwmd server.
397 * Connects to a local unix domain socket.
399 * \param pwm A handle.
400 * \param path The socket path to connect to. If NULL, then a default of
401 * \a "~/.pwmd/socket" will be used.
402 * \return 0 on success or an error code.
403 * \see pwmd_ssh_connect(), pwmd_ssh_connect_async()
405 gpg_error_t pwmd_connect(pwm_t *pwm, const char *path)
406 __attribute__ ((warn_unused_result));
409 /*! \brief Establish a remote connection to a pwmd server.
411 * Connects to a pwmd server over an SSH channel.
413 * \param pwm A handle.
414 * \param host The hostname to connect to.
415 * \param port The port or -1 for the default.
416 * \param identity The SSH identity file to use for authentication. This
417 * should specify the private key. The public key is assumed to be \a
418 * identity.pub.
419 * \param user The username on the SSH server to login as. If NULL then
420 * invoking user will be used.
421 * \param known_hosts A file containing the public SSH server key hash in SHA1
422 * format.
423 * \return 0 on success or an error code.
424 * \see pwmd_ssh_connect_async(), pwmd_process(), \ref ssh
426 gpg_error_t pwmd_ssh_connect(pwm_t *pwm, const char *host, int port,
427 const char *identity, const char *user, const char *known_hosts)
428 __attribute__ ((warn_unused_result));
431 /*! \brief Establish a remote connection to a pwmd server asynchronously.
433 * This is a variant of \ref pwmd_ssh_connect() that will not block while doing
434 * DNS lookups or while connecting.
436 * \process
438 * \param pwm A handle.
439 * \param host The hostname to connect to.
440 * \param port The port or -1 for the default.
441 * \param identity The SSH identity file to use for authentication. This
442 * should specify the private key. The public key is assumed to be \a
443 * identity.pub.
444 * \param user The username on the SSH server to login as. If NULL, the
445 * invoking username will be used.
446 * \param known_hosts A file containing the public SSH server key hash in SHA1
447 * format.
448 * \return 0 on success or an error code.
449 * \see pwmd_process(), \ref ssh
451 gpg_error_t pwmd_ssh_connect_async(pwm_t *pwm, const char *host, int port,
452 const char *identity, const char *user, const char *known_hosts)
453 __attribute__ ((warn_unused_result));
456 /*! \brief Establish a connection by parsing a URL.
458 * This allows for connecting to a pwmd server by parsing the given URL
459 * string. Whether the connection is to a remote or local server depends on
460 * the contents:
461 * \code
463 * socket://[path/to/local/socket]
465 * or
467 * ssh[46]://[username@]hostname[:port],identity,known_hosts
468 * \endcode
470 * The parameters in square brackets are optional and if not specified the
471 * defaults will be used.
473 * \param pwm A handle.
474 * \param url The string to parse.
475 * \return 0 on success or an error code.
477 gpg_error_t pwmd_connect_url(pwm_t *pwm, const char *url)
478 __attribute__ ((warn_unused_result));
481 /*! \brief Establish a connection asynchronously by parsing a URL.
483 * This allows for connecting to a pwmd server by parsing the given URL
484 * string. Whether the connection is to a remote or local server depends on
485 * the contents:
486 * \code
488 * socket://[path/to/local/socket]
490 * or
492 * ssh[46]://[username@]hostname[:port],identity,known_hosts
493 * \endcode
495 * The parameters in square brackets are optional and if not specified the
496 * defaults will be used.
498 * \process
500 * \param pwm A handle.
501 * \param url The string to parse.
502 * \return 0 on success or an error code.
504 gpg_error_t pwmd_connect_url_async(pwm_t *pwm, const char *url)
505 __attribute__ ((warn_unused_result));
508 /*! \brief Retrieve a remote SSH host key.
510 * This key is needed for host verification of the remote pwmd server.
512 * \param pwm A handle.
513 * \param host The hostname to connect to.
514 * \param port The port.
515 * \param[out] result The server host key which must be freed with \ref
516 * pwmd_free().
517 * \return 0 on success or an error code.
518 * \see pwmd_get_hostkey_async(), \ref ssh
520 gpg_error_t pwmd_get_hostkey(pwm_t *pwm, const char *host, int port,
521 char **result)
522 __attribute__ ((warn_unused_result));
525 /*! \brief Retrieve a remote SSH host key asynchronously.
527 * This key is needed for host verification of the remote pwmd server.
529 * \process
531 * \param pwm A handle.
532 * \param host The hostname to connect to.
533 * \param port The port or a default if set to -1.
534 * \return 0 on success or an error code.
535 * \see pwmd_get_hostkey(), \ref pwmd_process(), \ref ssh
537 gpg_error_t pwmd_get_hostkey_async(pwm_t *pwm, const char *host, int port)
538 __attribute__ ((warn_unused_result));
541 /*! \brief Get the associated file descriptor(s) for a handle.
543 * This function lets the application poll the available file descriptors for
544 * the specified handle. It should be called after each asynchronous function
545 * and after each call to \ref pwmd_process() since the polled file
546 * descriptors may have been closed since the last call. It should also be
547 * called periodically to determine when to call \ref pwmd_process() to parse
548 * any pending status messages.
550 * After returning, \a n_fds is set to the number of available file
551 * descriptors which are stored in \a fds. The .flags member of \ref pwmd_fd_t
552 * specifies what can be monitored and is a bitmask of \ref PWMD_FD_READABLE
553 * and \ref PWMD_FD_WRITABLE. When ready, \ref pwmd_process() should be
554 * called.
556 * \param pwm A handle.
557 * \param[out] fds Set to the file descriptor(s) of the associated handle.
558 * \param[out] n_fds Initially the size of \a fds then updated to the number
559 * of available file descriptors which are stored in \a fds.
560 * \retval 0 on success or an error code.
561 * \retval GPG_ERR_ERANGE There are more file descriptors than the amount
562 * specified in \a n_fds. \a fds and \a n_fds are still usable though.
563 * \see pwmd_process()
565 gpg_error_t pwmd_get_fds(pwm_t *pwm, pwmd_fd_t *fds, int *n_fds)
566 __attribute__ ((warn_unused_result));
569 /*! \brief Check for a unparsed buffered line.
571 * A buffered line is a line that was read from the server but was not
572 * processed. This function determines if there is such a line.
574 * \param pwm A handle.
575 * \retval 0 if there is pending data.
576 * \retval GPG_ERR_NO_DATA if there is no pending data.
577 * \see pwmd_process()
579 gpg_error_t pwmd_pending_line(pwm_t *pwm)
580 __attribute__ ((warn_unused_result));
583 /*! \brief Set library options.
585 * See \ref pwmd_option_t for option specific details.
587 * \param pwm A handle.
588 * \param opt The option.
589 * \param ... The option value.
590 * \return 0 on success or an error code.
592 gpg_error_t pwmd_setopt(pwm_t *pwm, pwmd_option_t opt, ...)
593 __attribute__ ((warn_unused_result));
596 /*! \brief Open a file on the pwmd server.
598 * This will send the OPEN command to the server.
600 * \param pwm A handle.
601 * \param filename The filename to open.
602 * \return 0 on success or an error code.
603 * \see \ref pinentry
605 gpg_error_t pwmd_open(pwm_t *pwm, const char *filename)
606 __attribute__ ((warn_unused_result));
608 /*! \brief Open a file on the pwmd server using a local pinentry.
610 * This will send the OPEN command to the server like \ref pwmd_open() but
611 * will use the local pinentry and not pwmd's pinentry.
613 * \sigalrm
615 * \param pwm A handle.
616 * \param filename The filename to open.
617 * \return 0 on success or an error code.
618 * \see \ref pinentry
620 gpg_error_t pwmd_open2(pwm_t *pwm, const char *filename)
621 __attribute__ ((warn_unused_result));
624 /*! \brief Open a file on the pwmd server asynchronously (fork method).
626 * This will send the OPEN command to the pwmd server. The difference from
627 * \ref pwmd_open() is that it will not block if a pinentry is needed for
628 * passphrase input.
630 * The difference from \ref pwmd_open_async() is that libpwmd will \ref fork()
631 * a pinentry process rather than have pwmd use it's pinentry method. This may
632 * be useful if the passphrase isn't cached on a remote pwmd server and a
633 * remote \ref pinentry(1) is not possible.
635 * \process
637 * \sigalrm
639 * \param pwm A handle.
640 * \param filename The filename to open.
641 * \return 0 on success or an error code.
642 * \see pwmd_process(), \ref pinentry
644 gpg_error_t pwmd_open_async2(pwm_t *pwm, const char *filename)
645 __attribute__ ((warn_unused_result));
648 /*! \brief Open a file on the pwmd server asynchronously.
650 * This will send the OPEN command to the pwmd server. The difference from
651 * \ref pwmd_open() is that it will not block if a pinentry is needed for
652 * passphrase input.
654 * \process
656 * \param pwm A handle.
657 * \param filename The filename to open.
658 * \return 0 on success or an error code.
659 * \see pwmd_process(), \ref pinentry
661 gpg_error_t pwmd_open_async(pwm_t *pwm, const char *filename)
662 __attribute__ ((warn_unused_result));
665 /*! \brief Process an asynchronous function.
667 * After an asynchronous function has been called and has returned
668 * successfully, this function must be called to process the command to
669 * retrieve the result or return value.
671 * This function may also be called when not in a command to check for pending
672 * status messages sent from the server or to process a pending line.
674 * \param pwm A handle.
675 * \param rc Set to the return code of the command after ASYNC_DONE is
676 * returned. This value must be checked to determine if the command succeeded.
677 * \param[out] result Set to the result of the command when \a rc is 0. Note
678 * that not all commands return a result.
679 * \retval ASYNC_DONE The command has completed. \a rc should be checked to
680 * determine if the command was successful or not.
681 * \retval ASYNC_PROCESS The command is still running and this function should
682 * be called again.
683 * \see pwmd_get_fds(), pwmd_pending_line()
685 pwmd_async_t pwmd_process(pwm_t *pwm, gpg_error_t *rc, char **result)
686 __attribute__ ((warn_unused_result));
689 /*! \brief Save a file on the pwmd server.
691 * This will send the SAVE command.
693 * \param pwm A handle.
694 * \return 0 on success or an error code.
695 * \see \ref pinentry
697 gpg_error_t pwmd_save(pwm_t *pwm)
698 __attribute__ ((warn_unused_result));
701 /*! \brief Save a file on the pwmd server using the local pinentry.
703 * This will send the SAVE command like \ref pwmd_save() but will use a local
704 * pinentry and not pwmd's pinentry.
706 * \param pwm A handle.
707 * \return 0 on success or an error code.
708 * \see \ref pinentry
710 gpg_error_t pwmd_save2(pwm_t *pwm)
711 __attribute__ ((warn_unused_result));
714 /*! \brief Save a file on the pwmd server asynchronously (fork method).
716 * This will send the SAVE command to the pwmd server. The difference from
717 * \ref pwmd_save() is that it will not block if a pinentry is needed for
718 * passphrase input.
720 * The difference from \ref pwmd_save_async() is that libpwmd will \ref fork()
721 * a pinentry process rather than have pwmd use it's pinentry method. This may
722 * be useful if the passphrase isn't cached on a remote pwmd server and a
723 * remote \ref pinentry(1) is not possible.
725 * \process
727 * \param pwm A handle.
728 * \return 0 on success or an error code.
729 * \see pwmd_process(), \ref pinentry
731 gpg_error_t pwmd_save_async2(pwm_t *pwm)
732 __attribute__ ((warn_unused_result));
735 /*! \brief Save changes to a file on the pwmd server asynchronously.
737 * This will send the SAVE command to the pwmd server. The difference from
738 * \ref pwmd_save() is that it will not block if a pinentry is needed for
739 * passphrase input.
741 * \process
743 * \param pwm A handle.
744 * \return 0 on success or an error code.
745 * \see pwmd_process(), \ref pinentry
747 gpg_error_t pwmd_save_async(pwm_t *pwm)
748 __attribute__ ((warn_unused_result));
751 /*! \brief Send a command to the pwmd server.
753 * Sends a command to the pwmd server. You should avoid sending the BYE
754 * command here because the assuan context will be freed and bad things will
755 * happen. Use \ref pwmd_close() instead. For commands that use an INQUIRE to
756 * send data to the server (STORE and IMPORT), \ref pwmd_inquire() should be
757 * used and not this function.
759 * \param pwm A handle.
760 * \param[out] result The result of the command when successful and which must
761 * be freed with \ref pwmd_free(). Note that not all commands return a result.
762 * \param cmd The command to send.
763 * \param ... The arguments to \a cmd.
764 * \return 0 on success or an error code.
766 gpg_error_t pwmd_command(pwm_t *pwm, char **result, const char *cmd, ...)
767 __attribute__ ((warn_unused_result));
770 /*! \brief Send a command to the pwmd server.
772 * Like \ref pwmd_command() but uses an argument pointer instead.
774 * \param pwm A handle.
775 * \param[out] result The result of the command when successful which must be
776 * freed with \ref pwmd_free(). Note that not all commands return a result.
777 * \param cmd The command to send.
778 * \param ap The arguments to \a cmd.
779 * \return 0 on success or an error code.
781 gpg_error_t pwmd_command_ap(pwm_t *pwm, char **result, const char *cmd,
782 va_list ap)
783 __attribute__ ((warn_unused_result));
786 /*! \brief Send data to a pwmd server.
788 * This lets commands that use an INQUIRE (STORE and IMPORT) send the data
789 * to the server. Use this function rather than \ref pwmd_command() for these
790 * commands.
792 * \param pwm A handle.
793 * \param cmd The command to send that uses an INQUIRE.
794 * \param func A callback function which sets the data to be sent.
795 * \param user A user data pointer passed to the callback function \a func.
796 * \return 0 on success or an error code.
798 * \see pwmd_inquire_cb_t
800 gpg_error_t pwmd_inquire(pwm_t *pwm, const char *cmd, pwmd_inquire_cb_t func,
801 void *user)
802 __attribute__ ((warn_unused_result));
805 /*! \brief Close a handle.
807 * This will close the connection to a pwmd server and free any resources
808 * associated with it.
810 * \param pwm A handle.
811 * \return Nothing.
813 void pwmd_close(pwm_t *pwm);
816 /*! \brief Free a previously allocated pointer.
818 * Use this function to free resources allocated by the other libpwmd memory
819 * functions. Do not use it to free your own allocations.
821 * The difference between the standard free() and this function is that
822 * this one will zero out the contents of the pointer before freeing it.
824 * \param ptr The pointer to deallocate.
825 * \return Nothing.
826 * \see pwmd_malloc(), pwmd_calloc(), pwmd_realloc(), pwmd_strdup(),
827 * pwmd_process(), pwmd_command()
829 void pwmd_free(void *ptr);
832 /*! \brief A wrapper around malloc.
834 * Like malloc(), but lets libpwmd keep track of the pointer.
836 * \param size The number of bytes to allocate.
837 * \return A newly allocated pointer or NULL if there wasn't enough memory.
838 * \see malloc(3), pwmd_free()
840 void *pwmd_malloc(size_t size)
841 __attribute__ ((warn_unused_result));
844 /*! \brief A wrapper around calloc().
846 * Like calloc(), but lets libpwmd keep track of the pointer.
848 * \param nmemb The number of bytes to allocate.
849 * \param size The number of bytes to allocate.
850 * \return A newly allocated pointer or NULL if there wasn't enough memory.
851 * \see calloc(3), pwmd_free()
853 void *pwmd_calloc(size_t nmemb, size_t size)
854 __attribute__ ((warn_unused_result));
857 /*! \brief A wrapper around realloc().
859 * Like realloc(), but lets libpwmd keep track of the pointer. Note that this
860 * function will try and allocate the entire \a size before freeing the
861 * original pointer and returning the new one.
863 * \param ptr The pointer to reallocate.
864 * \param size The new number of bytes to allocate.
865 * \return A newly allocated pointer or NULL if there wasn't enough memory.
866 * \see realloc(3), pwmd_free()
868 void *pwmd_realloc(void *ptr, size_t size)
869 __attribute__ ((warn_unused_result));
872 /*! \brief A wrapper around strdup().
874 * Like strdup(), but lets libpwmd keep track of the pointer.
876 * \param str The string to duplicate.
877 * \return A newly allocated character pointer or NULL if there wasn't
878 * enough memory.
879 * \see strdup(3), pwmd_free()
881 char *pwmd_strdup(const char *str)
882 __attribute__ ((warn_unused_result));
884 /*! \brief Duplicate a formatted string.
886 * Like sprintf() but returns an allocated string.
888 * \param fmt The formatted string.
889 * \param ... Any format arguments to the string.
890 * \return A newly allocated character pointer or NULL if there wasn't
891 * enough memory.
892 * \see pwmd_free()
894 char *pwmd_strdup_printf(const char *fmt, ...)
895 __attribute__ ((warn_unused_result));
897 /*! \def EPWMD_ERROR
898 * \hideinitializer
900 * A general pwmd error with no suitable description.
902 #define EPWMD_ERROR GPG_ERR_USER_1
905 /*! \def EPWMD_MAX_SLOTS
906 * \hideinitializer
908 * The maximum number of cache slots has been reached. There is no available
909 * slot for a new file.
911 #define EPWMD_MAX_SLOTS GPG_ERR_USER_2
914 /*! \def EPWMD_LOOP
915 * \hideinitializer
917 * A recursion loop was detected while processing a "target" attribute.
919 #define EPWMD_LOOP GPG_ERR_USER_3
922 /*! \def EPWMD_NO_FILE
923 * \hideinitializer
925 * A command required an open file, but no file has yet been opened.
927 #define EPWMD_NO_FILE GPG_ERR_USER_4
930 /*! \def EPWMD_LIBXML_ERROR
931 * \hideinitializer
933 * An XML parse or other libxml2 error occurred.
935 #define EPWMD_LIBXML_ERROR GPG_ERR_USER_5
938 /*! \def EPWMD_FILE_MODIFIED
939 * \hideinitializer
941 * The data file was modified either externally or by another another client
942 * while trying to process a command.
944 #define EPWMD_FILE_MODIFIED GPG_ERR_USER_6
947 /*! \def EPWMD_MAX
948 * \hideinitializer
949 * \if cond1
950 * \endif
952 #define EPWMD_MAX GPG_ERR_USER_7
955 /*! \brief Return a description of an error code.
957 * \param code The error code to describe.
958 * \return A character description of the error code.
959 * \see pwmd_strerror_r()
961 const char *pwmd_strerror(gpg_error_t code)
962 __attribute__ ((warn_unused_result));
965 /*! \brief Return a description of an error code (thread-safe).
967 * This is a thread-safe version of \ref pwmd_strerror().
969 * \param code The error code to describe.
970 * \param[out] buf An allocated buffer to hold the error description.
971 * \param size The size of the allocated buffer \a buf.
973 * \retval 0 Success.
974 * \retval ERANGE \a size was not large enough to hold the entire description
975 * and \a buf is set to the truncated error string.
977 int pwmd_strerror_r(gpg_error_t code, char *buf, size_t size);
979 #ifdef __cplusplus
981 #endif
983 #endif