Added pwmd_inquire(). This removes PWMD_OPTION_INQUIRE_FUNC and
[libpwmd.git] / pwmc.c
bloba9c571b8d9609590e52a22bd1de5d1117f75c410
1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
2 /*
3 Copyright (C) 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
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <err.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <signal.h>
26 #include <termios.h>
27 #include <libpwmd.h>
29 #ifdef HAVE_CONFIG_H
30 #include <config.h>
31 #endif
33 #ifdef HAVE_LOCALE_H
34 #include <locale.h>
35 #endif
37 #include "gettext.h"
38 #define N_(msgid) gettext(msgid)
40 #ifndef MEM_DEBUG
41 #include "mem.h"
42 #else
43 #define xfree free
44 #define xrealloc realloc
45 #define xmalloc malloc
46 #define xstrdup strdup
47 #define xcalloc calloc
48 #endif
50 int timeout, elapsed;
51 pwm_t *pwm;
53 static void show_error(gpg_error_t error)
55 fprintf(stderr, "ERR %i %s\n", gpg_err_code(error), pwmd_strerror(error));
58 void catchsig(int sig)
60 gpg_error_t error;
62 switch (sig) {
63 case SIGALRM:
64 if (pwm && timeout > 0 && elapsed++ >= timeout) {
65 error = pwmd_terminate_pinentry(pwm);
67 if (error)
68 show_error(error);
70 break;
73 alarm(1);
74 break;
75 default:
76 break;
80 static void usage(const char *pn)
82 fprintf(stderr, N_(
83 "Reads PWMD protocol commands from standard input.\n\n"
84 "Usage: pwmc [-hvX] [-s <socket>] [[-a [-P -T -N -D] [-t <seconds>]] |\n"
85 " [-p <password>]] [-S] [-d <fd>] [-I <fd>] [filename]\n"
86 " -X disable showing of status messages from the server\n"
87 " -s socket path\n"
88 " -p password\n"
89 " -a use pinentry(1) for password retrieval\n"
90 " -P path to the pinentry binary (%s)\n"
91 " -T pinentry tty\n"
92 " -N pinentry terminal type\n"
93 " -D pinentry display\n"
94 " -t pinentry timeout\n"
95 " -d redirect command output to the specified file descriptor\n"
96 " -I read inquire data from the specified file descriptor\n"
97 " -S send the SAVE command after all others\n"
98 " -v version\n"
99 " -h this help text\n"), PINENTRY_PATH);
100 exit(EXIT_FAILURE);
103 int set_pinentry_option(pwmd_option_t option, void *value)
105 gpg_error_t error;
107 error = pwmd_setopt(pwm, option, value);
109 if (error) {
110 show_error(error);
111 pwmd_close(pwm);
112 return 1;
115 return 0;
118 struct inquire_s {
119 FILE *fp;
120 char *data;
123 static gpg_error_t do_inquire(void *data, const char *keyword, gpg_error_t rc,
124 char **result, size_t *result_len)
126 int c;
127 static char buf[1000]; /* Assuan protocol line length limit. */
128 char *p;
129 size_t len = 0;
130 struct inquire_s *inq = data;
132 if (rc) {
133 memset(buf, 0, sizeof(buf));
134 return rc;
137 buf[0] = 0;
138 p = buf;
140 if (inq->data) {
141 snprintf(buf, sizeof(buf), "%s", inq->data);
142 xfree(inq->data);
143 inq->data = NULL;
144 len = strlen(buf);
145 p = buf + len;
148 while ((c = fgetc(inq->fp)) != EOF) {
149 if (len == sizeof(buf)) {
150 ungetc(c, inq->fp);
151 break;
154 *p++ = c;
155 len++;
158 if (!buf[0]) {
159 memset(buf, 0, sizeof(buf));
160 return GPG_ERR_EOF;
163 *result = buf;
164 *result_len = len;
165 return 0;
168 static int status_msg_cb(void *data, const char *line)
170 fprintf(stderr, "%s\n", line);
171 return 0;
174 int main(int argc, char *argv[])
176 int opt;
177 int use_pinentry = 0;
178 char *password = NULL;
179 char *filename = NULL;
180 char *socketpath = NULL;
181 char command[1000], *p;
182 int ret = EXIT_SUCCESS;
183 gpg_error_t error;
184 char *result = NULL;
185 int save = 0;
186 char *pinentry_path = NULL;
187 char *display = NULL, *tty = NULL, *ttytype = NULL;
188 struct termios term;
189 int outfd = STDOUT_FILENO;
190 FILE *outfp = stdout;
191 int inquirefd = STDIN_FILENO;
192 FILE *inquirefp = stdin;
193 int show_status = 1;
195 setlocale(LC_ALL, "");
196 bindtextdomain("libpwmd", LOCALEDIR);
197 timeout = -1;
199 while ((opt = getopt(argc, argv, "I:XT:N:D:hvaP:t:p:s:Sd:")) != EOF) {
200 switch (opt) {
201 case 'X':
202 show_status = 0;
203 break;
204 case 'T':
205 tty = optarg;
206 break;
207 case 'N':
208 ttytype = optarg;
209 break;
210 case 'D':
211 display = optarg;
212 break;
213 case 'I':
214 inquirefd = atoi(optarg);
215 inquirefp = fdopen(inquirefd, "r");
217 if (!inquirefp) {
218 xfree(password);
219 err(EXIT_FAILURE, "%i", inquirefd);
221 break;
222 case 'd':
223 outfd = atoi(optarg);
224 outfp = fdopen(outfd, "w");
226 if (!outfp) {
227 xfree(password);
228 err(EXIT_FAILURE, "%i", outfd);
230 break;
231 case 'S':
232 save = 1;
233 break;
234 case 's':
235 socketpath = xstrdup(optarg);
236 break;
237 case 'p':
238 password = xstrdup(optarg);
239 break;
240 case 'a':
241 use_pinentry = 1;
242 break;
243 case 'P':
244 pinentry_path = xstrdup(optarg);
245 break;
246 case 't':
247 timeout = atoi(optarg);
248 break;
249 case 'v':
250 xfree(password);
251 printf("%s (pwmc)\n%s\n", PACKAGE_STRING, PACKAGE_BUGREPORT);
252 exit(EXIT_SUCCESS);
253 case 'h':
254 default:
255 xfree(password);
256 usage(argv[0]);
260 if (use_pinentry && password) {
261 xfree(password);
262 usage(argv[0]);
265 filename = argv[optind];
266 pwmd_init();
268 if ((pwm = pwmd_connect(socketpath, &error)) == NULL) {
269 xfree(password);
270 errx(EXIT_FAILURE, "pwmd_connect(): %s", pwmd_strerror(error));
273 if (password) {
274 if (set_pinentry_option(PWMD_OPTION_PASSWORD, password)) {
275 xfree(password);
276 exit(EXIT_FAILURE);
279 xfree(password);
281 else if (use_pinentry) {
282 error = pwmd_setopt(pwm, PWMD_OPTION_PINENTRY, 1);
284 if (error) {
285 show_error(error);
286 pwmd_close(pwm);
287 exit(EXIT_FAILURE);
290 if (set_pinentry_option(PWMD_OPTION_PINENTRY_PATH, pinentry_path))
291 exit(EXIT_FAILURE);
293 if (set_pinentry_option(PWMD_OPTION_PINENTRY_TITLE,
294 N_("Password Manager Daemon")))
295 exit(EXIT_FAILURE);
297 snprintf(command, sizeof(command), N_("A password is required for the "
298 "file \"%s\". Please\nenter the password below."), filename);
300 if (set_pinentry_option(PWMD_OPTION_PINENTRY_DESC, command))
301 exit(EXIT_FAILURE);
303 if (display) {
304 if (set_pinentry_option(PWMD_OPTION_PINENTRY_DISPLAY, display))
305 exit(EXIT_FAILURE);
308 if (tty) {
309 if (set_pinentry_option(PWMD_OPTION_PINENTRY_TTY, tty))
310 exit(EXIT_FAILURE);
313 if (ttytype) {
314 if (set_pinentry_option(PWMD_OPTION_PINENTRY_TERM, ttytype))
315 exit(EXIT_FAILURE);
319 if (show_status) {
320 if (set_pinentry_option(PWMD_OPTION_STATUS_FUNC, status_msg_cb)) {
321 show_error(error);
322 pwmd_close(pwm);
323 exit(EXIT_FAILURE);
327 if (filename) {
328 if (use_pinentry && timeout != -1) {
329 tcgetattr(STDOUT_FILENO, &term);
330 signal(SIGALRM, catchsig);
331 alarm(1);
334 error = pwmd_open(pwm, filename);
336 if (error) {
337 if (error == GPG_ERR_TIMEOUT) {
338 tcsetattr(STDOUT_FILENO, 0, &term);
339 printf("\r\n");
342 show_error(error);
343 pwmd_close(pwm);
344 exit(EXIT_FAILURE);
348 signal(SIGALRM, SIG_IGN);
349 elapsed = 0;
351 p = fgets(command, sizeof(command), stdin);
353 if (!p)
354 goto done;
357 * This is a known INQUIRE command. We use pwmd_inquire() to send the
358 * data from the do_inquire() callback function.
360 if (strncasecmp(p, "STORE ", 6) == 0) {
361 struct inquire_s *inq = malloc(sizeof(struct inquire_s));
363 if (!inq) {
364 show_error(gpg_error_from_errno(ENOMEM));
365 ret = EXIT_FAILURE;
366 goto done;
369 inq->data = xstrdup(p+6);
370 inq->fp = inquirefp;
371 error = pwmd_inquire(pwm, "STORE", do_inquire, inq);
372 free(inq);
374 if (error) {
375 show_error(error);
376 ret = EXIT_FAILURE;
379 goto done;
382 if (p[strlen(p) - 1] == '\n')
383 p[strlen(p) - 1] = 0;
385 if (strcasecmp(p, "BYE") == 0)
386 goto done;
388 error = pwmd_command(pwm, &result, command);
389 memset(command, 0, sizeof(command));
391 if (error) {
392 show_error(error);
393 ret = EXIT_FAILURE;
394 goto fail;
397 if (result) {
398 if (isatty(outfd) && result[strlen(result) - 1] == '\n')
399 result[strlen(result) - 1] = 0;
401 fwrite(result, 1, strlen(result), outfp);
402 pwmd_free_result(result);
404 if (isatty(outfd))
405 fputc('\n', outfp);
408 done:
409 memset(command, 0, sizeof(command));
411 if (!error && save) {
412 save_again:
413 error = pwmd_save(pwm);
415 if (error) {
416 if (use_pinentry && (error == EPWMD_BADKEY || error == EPWMD_KEY))
417 goto save_again;
419 show_error(error);
420 ret = EXIT_FAILURE;
421 goto fail;
425 fail:
426 pwmd_close(pwm);
428 if (socketpath)
429 xfree(socketpath);
431 exit(ret);