1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2006-2007 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 02111-1307 USA
23 #include <gpg-error.h>
25 //#define _ASSUAN_EXT_SYM_PREFIX pwmd_
36 typedef char *(pwmd_password_func
)(void *data
);
40 * PWMD_OPTION_PASSWORD_FUNC
42 * Function to retrieve a password. This function should return an
43 * allocated string which is the password or NULL.
45 PWMD_OPTION_PASSWORD_FUNC
,
48 * PWMD_OPTION_PASSWORD_DATA
50 * Data passed to the password function.
52 PWMD_OPTION_PASSWORD_DATA
,
55 * PWMD_OPTION_PINENTRY
57 * The following argument should be of type int and set to 1 to enable the
58 * use of pinentry(1) to retrieve passwords. Setting to 0 will disable
59 * using pinentry and the password must be set with PWMD_OPTION_PASSWORD
60 * or gotten from PWMD_OPTION_PASSWORD_FUNC.
65 * PWMD_OPTION_PINENTRY_PATH
67 * The full pathname to the pinentry program. If not specified,
68 * /usr/bin/pinentry will be used.
70 PWMD_OPTION_PINENTRY_PATH
,
73 * PWMD_OPTION_PINENTRY_TTY
77 PWMD_OPTION_PINENTRY_TTY
,
80 * PWMD_OPTION_PINENTRY_DISPLAY
84 PWMD_OPTION_PINENTRY_DISPLAY
,
87 * PWMD_OPTION_PINENTRY_TERM
91 PWMD_OPTION_PINENTRY_TERM
,
94 * PWMD_OPTION_PASSWORD
96 * The following argument should be of type char* which specifies the
97 * password to use when the PWMD_OPEN or PWMD_SAVE commands are issued and
98 * PWMD_OPTION_PINENTRY is 0.
100 PWMD_OPTION_PASSWORD
,
107 * The following argument is of type char* which specifies either the
108 * title, prompt or description in the pinentry program when
109 * PWMD_OPTION_PINENTRY is set.
117 assuan_context_t ctx
;
118 assuan_context_t pctx
; /* pinentry */
123 char *pinentry_display
;
129 pwmd_password_func
*passfunc
;
134 * Initialize the library.
139 * Connects to the socket specified by 'socket_path'. If socket_path is NULL,
140 * then a default of ~/.pwmd/socket will be used. Returns a new handle for use
141 * with the other functions or NULL if there was an error in which case
142 * 'error' is set to an error code which may be described by pwmd_strerror().
144 pwm_t
*pwmd_connect(const char *socket_path
, gpg_error_t
*error
);
147 * Opens a file 'filename' (the OPEN command). The password is gotten from the
148 * pinentry program if configured to do so and not already cached. Returnes
149 * PWMD_OK on success or PWMD_ERROR on error and sets 'error' to the error
152 int pwmd_open(pwm_t
*pwm
, gpg_error_t
*error
, const char *filename
);
155 * Send's the SAVE command and takes care of password requests.
157 int pwmd_save(pwm_t
*pwm
, gpg_error_t
*error
);
160 * Closes the connection to the socket and frees the resources of the handle.
163 void pwmd_close(pwm_t
*pwm
);
166 * Sends a protocol command 'cmd' to the daemon using handle 'pwm'. If the
167 * command fails PWMD_ERROR is returned with 'error' set to the error code
168 * which may be described by passing the error to pwmd_strerror(). If
169 * successful the function returns PWMD_OK and the 'result' is the character
170 * data of the command or NULL if there was none.
172 int pwmd_command(pwm_t
*pwm
, char **result
, gpg_error_t
*error
, const char *cmd
, ...);
175 * Free's the memory used by the result of pwmd_command() if any. It is
176 * important to use this function because libpwmd keeps track of all memory
179 void pwmd_free_result(void *);
182 * Sets a libpwmd option 'opt'. The next argument should be of the data type
183 * required for the option. Return PWMD_OK on success or PWMD_ERROR if 'opt'
184 * is an invalid option.
186 int pwmd_setopt(pwm_t
*pwm
, gpg_error_t
*error
, pwmd_option opt
, ...);
189 * This a nonblocking pinentry password retriever. It returns -2 when
190 * 'filename' is not NULL and is cached (ISCACHED) or if the file doesn't
191 * exist on the file system (a new file). If 'filename' is NULL or the file is
192 * not cached this function returns a file descriptor that select() can use.
193 * When ready for read(), read() should read a pwmd_password_s. If a pinentry
194 * error occurs, the structure member .error will be set to the error code. If
195 * theres a system error (pipe() or fork()), then -1 is returned and 'error'
196 * is set to an error code that pwmd_strerror() can describe.
199 char password
[ASSUAN_LINELENGTH
];
203 int pwmd_get_password(pwm_t
*pwm
, gpg_error_t
*error
, const char *filename
);
206 * Protocol error codes.
208 #define EPWMD_ERROR GPG_ERR_USER_1
209 #define EPWMD_MAX_SLOTS GPG_ERR_USER_2
210 #define EPWMD_ELEMENT_NOT_FOUND GPG_ERR_USER_3
211 #define EPWMD_TRAILING_ELEMENT GPG_ERR_USER_4
212 #define EPWMD_INVALID_ELEMENT GPG_ERR_USER_5
213 #define EPWMD_EMPTY_ELEMENT GPG_ERR_USER_6
214 #define EPWMD_ACCOUNT_EXISTS GPG_ERR_USER_7
215 #define EPWMD_FILE_NOT_FOUND GPG_ERR_USER_8
216 #define EPWMD_NO_FILE GPG_ERR_USER_9
217 #define EPWMD_LIBXML_ERROR GPG_ERR_USER_10
218 #define EPWMD_CACHE_NOT_FOUND GPG_ERR_USER_11
219 #define EPWMD_ATTR_NOT_FOUND GPG_ERR_USER_12
220 #define EPWMD_INVALID_FILENAME GPG_ERR_USER_13
221 #define EPWMD_FILE_MODIFIED GPG_ERR_USER_14
222 #define EPWMD_MAX GPG_ERR_USER_15
225 * Try to reuse GPG error codes when possible. There's only 16 user-defined
228 #define EPWMD_KEY GPG_ERR_WRONG_KEY_USAGE
229 #define EPWMD_BADKEY GPG_ERR_INV_PASSPHRASE
230 #define EPWMD_COMMAND_SYNTAX GPG_ERR_SYNTAX
231 #define EPWMD_ATTR_SYNTAX GPG_ERR_SYNTAX
234 * Return a string describing a pwmd protocol error code.
236 const char *pwmd_strerror(gpg_error_t pwmd_error
);