Added pwmc command line option -I to read inquire data from the
[libpwmd.git] / pwmc.c
blob22d04c3b0e33d34de8ddcc19843a6b96607f64c9
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 <n>] [-I <n>] [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 int 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 goto done;
148 while ((c = fgetc(inq->fp)) != EOF) {
149 if (len >= sizeof(buf)) {
150 ungetc(c, stdin);
151 break;
154 *p++ = c;
155 len++;
158 if (!buf[0]) {
159 memset(buf, 0, sizeof(buf));
160 return GPG_ERR_EOF;
163 done:
164 *result = buf;
165 *result_len = len;
166 return 0;
169 static int status_msg_cb(void *data, const char *line)
171 fprintf(stderr, "%s\n", line);
172 return 0;
175 int main(int argc, char *argv[])
177 int opt;
178 int use_pinentry = 0;
179 char *password = NULL;
180 char *filename = NULL;
181 char *socketpath = NULL;
182 char command[1000], *p;
183 int ret = EXIT_SUCCESS;
184 gpg_error_t error;
185 char *result = NULL;
186 int save = 0;
187 char *pinentry_path = NULL;
188 char *display = NULL, *tty = NULL, *ttytype = NULL;
189 struct termios term;
190 int outfd = STDOUT_FILENO;
191 FILE *outfp = stdout;
192 int inquirefd = STDIN_FILENO;
193 FILE *inquirefp = stdin;
194 int show_status = 1;
196 setlocale(LC_ALL, "");
197 bindtextdomain("libpwmd", LOCALEDIR);
198 timeout = -1;
200 while ((opt = getopt(argc, argv, "I:XT:N:D:hvaP:t:p:s:Sd:")) != EOF) {
201 switch (opt) {
202 case 'X':
203 show_status = 0;
204 break;
205 case 'T':
206 tty = optarg;
207 break;
208 case 'N':
209 ttytype = optarg;
210 break;
211 case 'D':
212 display = optarg;
213 break;
214 case 'I':
215 inquirefd = atoi(optarg);
216 inquirefp = fdopen(inquirefd, "r");
218 if (!inquirefp) {
219 xfree(password);
220 err(EXIT_FAILURE, "%i", inquirefd);
222 break;
223 case 'd':
224 outfd = atoi(optarg);
225 outfp = fdopen(outfd, "w");
227 if (!outfp) {
228 xfree(password);
229 err(EXIT_FAILURE, "%i", outfd);
231 break;
232 case 'S':
233 save = 1;
234 break;
235 case 's':
236 socketpath = xstrdup(optarg);
237 break;
238 case 'p':
239 password = xstrdup(optarg);
240 break;
241 case 'a':
242 use_pinentry = 1;
243 break;
244 case 'P':
245 pinentry_path = xstrdup(optarg);
246 break;
247 case 't':
248 timeout = atoi(optarg);
249 break;
250 case 'v':
251 xfree(password);
252 printf("%s (pwmc)\n%s\n", PACKAGE_STRING, PACKAGE_BUGREPORT);
253 exit(EXIT_SUCCESS);
254 case 'h':
255 default:
256 xfree(password);
257 usage(argv[0]);
261 if (use_pinentry && password) {
262 xfree(password);
263 usage(argv[0]);
266 filename = argv[optind];
267 pwmd_init();
269 if ((pwm = pwmd_connect(socketpath, &error)) == NULL) {
270 xfree(password);
271 errx(EXIT_FAILURE, "pwmd_connect(): %s", pwmd_strerror(error));
274 if (password) {
275 if (set_pinentry_option(PWMD_OPTION_PASSWORD, password)) {
276 xfree(password);
277 exit(EXIT_FAILURE);
280 xfree(password);
282 else if (use_pinentry) {
283 error = pwmd_setopt(pwm, PWMD_OPTION_PINENTRY, 1);
285 if (error) {
286 show_error(error);
287 pwmd_close(pwm);
288 exit(EXIT_FAILURE);
291 if (set_pinentry_option(PWMD_OPTION_PINENTRY_PATH, pinentry_path))
292 exit(EXIT_FAILURE);
294 if (set_pinentry_option(PWMD_OPTION_PINENTRY_TITLE,
295 N_("Password Manager Daemon")))
296 exit(EXIT_FAILURE);
298 snprintf(command, sizeof(command), N_("A password is required for the "
299 "file \"%s\". Please\nenter the password below."), filename);
301 if (set_pinentry_option(PWMD_OPTION_PINENTRY_DESC, command))
302 exit(EXIT_FAILURE);
304 if (display) {
305 if (set_pinentry_option(PWMD_OPTION_PINENTRY_DISPLAY, display))
306 exit(EXIT_FAILURE);
309 if (tty) {
310 if (set_pinentry_option(PWMD_OPTION_PINENTRY_TTY, tty))
311 exit(EXIT_FAILURE);
314 if (ttytype) {
315 if (set_pinentry_option(PWMD_OPTION_PINENTRY_TERM, ttytype))
316 exit(EXIT_FAILURE);
320 if (set_pinentry_option(PWMD_OPTION_INQUIRE_FUNC, do_inquire)) {
321 show_error(error);
322 pwmd_close(pwm);
323 exit(EXIT_FAILURE);
326 if (show_status) {
327 if (set_pinentry_option(PWMD_OPTION_STATUS_FUNC, status_msg_cb)) {
328 show_error(error);
329 pwmd_close(pwm);
330 exit(EXIT_FAILURE);
334 if (filename) {
335 if (use_pinentry && timeout != -1) {
336 tcgetattr(STDOUT_FILENO, &term);
337 signal(SIGALRM, catchsig);
338 alarm(1);
341 error = pwmd_open(pwm, filename);
343 if (error) {
344 if (error == GPG_ERR_TIMEOUT) {
345 tcsetattr(STDOUT_FILENO, 0, &term);
346 printf("\r\n");
349 show_error(error);
350 pwmd_close(pwm);
351 exit(EXIT_FAILURE);
355 signal(SIGALRM, SIG_IGN);
356 elapsed = 0;
358 p = fgets(command, sizeof(command), stdin);
360 if (!p)
361 goto done;
363 if (strncasecmp(p, "STORE ", 6) == 0) {
364 struct inquire_s *inq = malloc(sizeof(struct inquire_s));
366 inq->data = xstrdup(p+6);
367 inq->fp = inquirefp;
369 if (set_pinentry_option(PWMD_OPTION_INQUIRE_DATA, inq)) {
370 xfree(inq->data);
371 free(inq);
372 pwmd_close(pwm);
373 exit(EXIT_FAILURE);
376 error = pwmd_command(pwm, &result, "STORE");
377 free(inq);
379 if (error) {
380 show_error(error);
381 ret = EXIT_FAILURE;
384 memset(command, 0, sizeof(command));
385 goto done;
388 if (p[strlen(p) - 1] == '\n')
389 p[strlen(p) - 1] = 0;
391 if (strcasecmp(p, "BYE") == 0)
392 goto done;
394 error = pwmd_command(pwm, &result, command);
395 memset(command, 0, sizeof(command));
397 if (error) {
398 show_error(error);
399 ret = EXIT_FAILURE;
400 goto fail;
403 if (result) {
404 if (isatty(outfd) && result[strlen(result) - 1] == '\n')
405 result[strlen(result) - 1] = 0;
407 fwrite(result, 1, strlen(result), outfp);
408 pwmd_free_result(result);
410 if (isatty(outfd))
411 fputc('\n', outfp);
414 done:
415 memset(command, 0, sizeof(command));
417 if (save) {
418 save_again:
419 error = pwmd_save(pwm);
421 if (error) {
422 if (use_pinentry && (error == EPWMD_BADKEY || error == EPWMD_KEY))
423 goto save_again;
425 show_error(error);
426 ret = EXIT_FAILURE;
427 goto fail;
431 fail:
432 pwmd_close(pwm);
434 if (socketpath)
435 xfree(socketpath);
437 exit(ret);