Merge branch 'master' into exp
[libpwmd.git] / pwmc.c
blob96fa2ca815baf134134fbe9c3aa323fcb3593b90
1 #define DEBUG 1
2 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 /*
4 Copyright (C) 2007-2008 Ben Kibbey <bjk@luxsci.net>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02110-1301 USA
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <err.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <libpwmd.h>
27 #include <assuan.h>
28 #ifdef DEBUG
29 #include <sys/select.h>
30 #include <fcntl.h>
31 #endif
33 #ifdef HAVE_CONFIG_H
34 #include <config.h>
35 #endif
37 #ifdef HAVE_LOCALE_H
38 #include <locale.h>
39 #endif
41 #include "gettext.h"
42 #define N_(msgid) gettext(msgid)
44 #ifndef MEM_DEBUG
45 #include "mem.h"
46 #else
47 #define xfree free
48 #define xrealloc realloc
49 #define xmalloc malloc
50 #define xstrdup strdup
51 #define xcalloc calloc
52 #endif
54 #define DEFAULT_PORT 6466
55 pwm_t *pwm;
57 static void show_error(gpg_error_t error)
59 fprintf(stderr, "ERR %i %s\n", gpg_err_code(error), pwmd_strerror(error));
62 static void usage(const char *pn)
64 fprintf(stderr, N_(
65 "Reads PWMD protocol commands from standard input.\n\n"
66 "Usage: pwmc [-hvX] [-s <socket>] "
67 #ifdef DEBUG
68 "[-E <n>] [-y <n>]"
69 #endif
70 "[-PTND <string>] [-p <password>]\n"
71 " [-S [-i <iter>]] [-c <name>] [-t <n>] [-d <fd>] [-I <fd>]\n"
72 #ifdef HAVE_LIBGNUTLS
73 " [-H <hostname> [-R <port>]] [filename]\n"
74 #else
75 " [filename]\n"
76 #endif
77 #ifdef DEBUG
78 " -E pinentry method (0=pwmd, 1=pwmd async, 2=libpwmd nb)\n"
79 " -y number of pinentry tries before failing (3)\n"
80 #endif
81 #ifdef HAVE_LIBGNUTLS
82 " -H connect to hostname\n"
83 " -R alterate port (%i)\n"
84 #endif
85 " -t pinentry timeout\n"
86 " -X disable showing of status messages from the server\n"
87 " -c set the client name\n"
88 " -s socket path (~/.pwmd/socket)\n"
89 " -p password\n"
90 " -P path to the pinentry binary (server default)\n"
91 " -T pinentry tty\n"
92 " -N pinentry terminal type\n"
93 " -D pinentry display\n"
94 " -d redirect command output to the specified file descriptor\n"
95 " -I read inquire data from the specified file descriptor\n"
96 " -S send the SAVE command before exiting\n"
97 " -i encrypt with the specified number of iterations (-1 = 0 iterations)\n"
98 " -v version\n"
99 " -h this help text\n"), DEFAULT_PORT);
100 exit(EXIT_FAILURE);
103 struct inquire_s {
104 FILE *fp;
105 char *data;
108 static gpg_error_t do_inquire(void *data, const char *keyword, gpg_error_t rc,
109 char **result, size_t *result_len)
111 int c;
112 static char buf[ASSUAN_LINELENGTH];
113 char *p;
114 size_t len = 0;
115 struct inquire_s *inq = (struct inquire_s *)data;
117 if (rc) {
118 memset(buf, 0, sizeof(buf));
119 return rc;
122 buf[0] = 0;
123 p = buf;
125 if (inq->data) {
126 snprintf(buf, sizeof(buf), "%s", inq->data);
127 xfree(inq->data);
128 inq->data = NULL;
129 len = strlen(buf);
130 p = buf + len;
133 while ((c = fgetc(inq->fp)) != EOF) {
134 if (len == sizeof(buf)) {
135 ungetc(c, inq->fp);
136 break;
139 *p++ = c;
140 len++;
143 if (!buf[0]) {
144 memset(buf, 0, sizeof(buf));
145 return GPG_ERR_EOF;
148 *result = buf;
149 *result_len = len;
150 return 0;
153 static int status_msg_cb(void *data, const char *line)
155 fprintf(stderr, "%s\n", line);
156 return 0;
159 #ifdef DEBUG
160 static gpg_error_t do_nb_command(int fd, int which)
162 int n;
163 gpg_error_t error = 0;
165 fcntl(fd, F_SETFL, O_NONBLOCK);
167 do {
168 fd_set fds;
169 struct timeval tv = {0, 50000};
171 FD_ZERO(&fds);
172 FD_SET(fd, &fds);
173 n = select(fd+1, &fds, NULL, NULL, &tv);
175 if (n > 0) {
176 if (FD_ISSET(fd, &fds)) {
177 pwmd_nb_status_t status;
179 n = read(fd, &status, sizeof(status));
181 if (n == -1) {
182 error = gpg_error_from_errno(errno);
183 goto done;
186 if (!which)
187 error = pwmd_open_nb_finalize(pwm, &status);
188 else
189 error = pwmd_save_nb_finalize(pwm, &status);
191 if (error)
192 goto done;
195 else
196 fputc('.', stderr);
197 } while (n == 0);
199 done:
200 return error;
202 #endif
204 int main(int argc, char *argv[])
206 int opt;
207 char *password = NULL;
208 char *filename = NULL;
209 char *socketpath = NULL;
210 char command[ASSUAN_LINELENGTH], *p;
211 int ret = EXIT_SUCCESS;
212 gpg_error_t error;
213 char *result = NULL;
214 int save = 0;
215 char *pinentry_path = NULL;
216 char *display = NULL, *tty = NULL, *ttytype = NULL;
217 int outfd = STDOUT_FILENO;
218 FILE *outfp = stdout;
219 int inquirefd = STDIN_FILENO;
220 FILE *inquirefp = stdin;
221 int show_status = 1;
222 char *clientname = NULL;
223 char *inquire = NULL;
224 int iter = -2;
225 int timeout = 0;
226 #ifdef HAVE_LIBGNUTLS
227 char *host = NULL;
228 int port = DEFAULT_PORT;
229 #endif
230 #ifdef DEBUG
231 int tries = 0;
232 int method = 0;
233 pwmd_async_t s;
234 #endif
236 setlocale(LC_ALL, "");
237 bindtextdomain("libpwmd", LOCALEDIR);
239 #ifdef DEBUG
240 #ifdef HAVE_LIBGNUTLS
241 while ((opt = getopt(argc, argv, "H:R:y:t:E:c:I:XT:N:D:hvP:p:s:Si:d:")) != EOF) {
242 #else
243 while ((opt = getopt(argc, argv, "y:t:E:c:I:XT:N:D:hvP:p:s:Si:d:")) != EOF) {
244 #endif
245 #else
246 #ifdef HAVE_LIBGNUTLS
247 while ((opt = getopt(argc, argv, "H:R:t:c:I:XT:N:D:hvP:p:s:Si:d:")) != EOF) {
248 #else
249 while ((opt = getopt(argc, argv, "t:c:I:XT:N:D:hvP:p:s:Si:d:")) != EOF) {
250 #endif
251 #endif
252 switch (opt) {
253 #ifdef DEBUG
254 case 'E':
255 method = atoi(optarg);
257 if (method > 2)
258 method = 2;
259 break;
260 case 'y':
261 tries = atoi(optarg);
262 break;
263 #endif
264 #ifdef HAVE_LIBGNUTLS
265 case 'H':
266 host = xstrdup(optarg);
267 break;
268 case 'R':
269 port = atoi(optarg);
270 break;
271 #endif
272 case 't':
273 timeout = atoi(optarg);
274 break;
275 case 'c':
276 clientname = xstrdup(optarg);
277 break;
278 case 'X':
279 show_status = 0;
280 break;
281 case 'T':
282 tty = optarg;
283 break;
284 case 'N':
285 ttytype = optarg;
286 break;
287 case 'D':
288 display = optarg;
289 break;
290 case 'I':
291 inquirefd = atoi(optarg);
292 inquirefp = fdopen(inquirefd, "r");
294 if (!inquirefp) {
295 xfree(password);
296 err(EXIT_FAILURE, "%i", inquirefd);
298 break;
299 case 'd':
300 outfd = atoi(optarg);
301 outfp = fdopen(outfd, "w");
303 if (!outfp) {
304 xfree(password);
305 err(EXIT_FAILURE, "%i", outfd);
307 break;
308 case 'S':
309 save = 1;
310 break;
311 case 'i':
312 iter = atoi(optarg);
314 if (iter < -1) {
315 xfree(password);
316 usage(argv[0]);
318 break;
319 case 's':
320 socketpath = xstrdup(optarg);
321 break;
322 case 'p':
323 password = xstrdup(optarg);
324 memset(optarg, 0, strlen(optarg));
325 break;
326 case 'P':
327 pinentry_path = xstrdup(optarg);
328 break;
329 case 'v':
330 xfree(password);
331 printf("%s (pwmc)\n%s\n", PACKAGE_STRING, PACKAGE_BUGREPORT);
332 exit(EXIT_SUCCESS);
333 case 'h':
334 default:
335 xfree(password);
336 usage(argv[0]);
340 filename = argv[optind];
341 pwmd_init();
343 #ifdef HAVE_LIBGNUTLS
344 if (host) {
345 #ifdef DEBUG
346 if (method == 1) {
347 if ((pwm = pwmd_tcp_connect_async(host, port, &error, NULL, NULL,
348 NULL)) == NULL) {
349 xfree(password);
350 errx(EXIT_FAILURE, "pwmd_tcp_connect_async(): %s", pwmd_strerror(error));
353 do {
354 s = pwmd_process(pwm, &error);
355 fputc('.', stderr);
356 usleep(50000);
357 } while (s == ASYNC_PROCESS);
359 if (error) {
360 pwmd_close(pwm);
361 xfree(password);
362 errx(EXIT_FAILURE, "pwmd_tcp_connect_async(): %s", pwmd_strerror(error));
365 pwmd_finalize(pwm);
367 else {
368 #endif
369 if ((pwm = pwmd_tcp_connect(host, port, &error, NULL, NULL, NULL))
370 == NULL) {
371 xfree(password);
372 errx(EXIT_FAILURE, "pwmd_tcp_connect(): %s", pwmd_strerror(error));
374 #ifdef DEBUG
376 #endif
378 else {
379 #endif
380 if ((pwm = pwmd_connect(socketpath, &error)) == NULL) {
381 xfree(password);
382 errx(EXIT_FAILURE, "pwmd_connect(): %s", pwmd_strerror(error));
384 #ifdef HAVE_LIBGNUTLS
386 #endif
388 error = pwmd_command(pwm, &result, "OPTION CLIENT NAME=%s", clientname ? clientname : "pwmc");
389 xfree(clientname);
391 if (error) {
392 xfree(password);
393 errx(EXIT_FAILURE, "pwmd_command(): %s", pwmd_strerror(error));
396 if (timeout > 0) {
397 error = pwmd_command(pwm, &result, "OPTION TIMEOUT=%i", timeout);
399 if (error) {
400 xfree(password);
401 errx(EXIT_FAILURE, "pwmd_command(): %s", pwmd_strerror(error));
405 if (password) {
406 error = pwmd_setopt(pwm, PWMD_OPTION_PASSWORD, password);
408 if (error) {
409 xfree(password);
410 goto done;
413 xfree(password);
415 else {
416 if (pinentry_path) {
417 error = pwmd_command(pwm, &result, "OPTION PATH=%s", pinentry_path);
419 if (error)
420 goto done;
423 if (display) {
424 error = pwmd_command(pwm, &result, "OPTION DISPLAY=%s", display);
426 if (error)
427 goto done;
430 if (tty) {
431 error = pwmd_command(pwm, &result, "OPTION TTYNAME=%s", tty);
433 if (error)
434 goto done;
437 if (ttytype) {
438 error = pwmd_command(pwm, &result, "OPTION TTYTYPE=%s", ttytype);
440 if (error)
441 goto done;
443 #ifdef DEBUG
444 if (method >= 2) {
445 error = pwmd_setopt(pwm, PWMD_OPTION_PINENTRY, 1);
447 if (error)
448 goto done;
451 if (tries > 0) {
452 error = pwmd_setopt(pwm, PWMD_OPTION_PINENTRY_TRIES, tries);
454 if (error)
455 goto done;
457 #endif
460 if (show_status) {
461 error = pwmd_setopt(pwm, PWMD_OPTION_STATUS_FUNC, status_msg_cb);
463 if (error)
464 goto done;
467 if (filename) {
468 #ifdef DEBUG
469 /* This method doesn't support PWMD_OPTION_PINENTRY_TRIES. */
470 if (method == 1) {
471 error = pwmd_open_async(pwm, filename);
473 if (!error) {
474 do {
475 s = pwmd_process(pwm, &error);
476 fputc('.', stderr);
477 usleep(50000);
478 } while (s == ASYNC_PROCESS);
480 pwmd_finalize(pwm);
483 else if (method == 2) {
484 int fd = pwmd_open_nb(pwm, &error, filename, timeout);
486 if (fd == -1)
487 goto done;
488 else if (fd >= 0)
489 error = do_nb_command(fd, 0);
491 else
492 error = pwmd_open(pwm, filename);
493 #else
494 error = pwmd_open(pwm, filename);
495 #endif
497 if (error)
498 goto done;
501 if (filename) {
502 error = pwmd_command(pwm, &result, "LOCK");
504 if (error)
505 goto done;
508 p = fgets(command, sizeof(command), stdin);
510 if (!p)
511 goto done;
514 * This is a known INQUIRE command. We use pwmd_inquire() to send the
515 * data from the do_inquire() callback function.
517 if (strncasecmp(p, "STORE ", 6) == 0) {
518 p += 6;
519 inquire = (char *)"STORE";
521 else if (strncasecmp(p, "IMPORT ", 7) == 0) {
522 p += 7;
523 inquire = (char *)"IMPORT";
526 if (inquire) {
527 struct inquire_s *inq = (struct inquire_s *)malloc(sizeof(struct inquire_s));
529 if (!inq) {
530 error = gpg_error_from_errno(ENOMEM);
531 goto done;
534 inq->data = xstrdup(p);
535 inq->fp = inquirefp;
536 error = pwmd_inquire(pwm, inquire, do_inquire, inq);
537 free(inq);
538 goto done;
541 if (strcasecmp(p, "BYE") == 0)
542 goto done;
544 error = pwmd_command(pwm, &result, command);
545 memset(command, 0, sizeof(command));
547 if (error)
548 goto done;
550 if (result) {
551 fwrite(result, 1, strlen(result), outfp);
552 pwmd_free_result(result);
555 done:
556 memset(command, 0, sizeof(command));
558 if (!error && save) {
559 if (iter != -2) {
560 error = pwmd_command(pwm, &result, "OPTION ITERATIONS=%i", iter);
562 if (error)
563 goto done;
566 #ifdef DEBUG
567 if (method == 1) {
568 error = pwmd_save_async(pwm);
570 if (!error) {
571 do {
572 s = pwmd_process(pwm, &error);
573 fputc('.', stderr);
574 usleep(50000);
575 } while (s == ASYNC_PROCESS);
577 pwmd_finalize(pwm);
580 else if (method == 3) {
581 int fd = pwmd_save_nb(pwm, &error);
583 if (fd == -1)
584 goto done;
585 else if (fd >= 0)
586 error = do_nb_command(fd, 1);
588 else
589 error = pwmd_save(pwm);
590 #else
591 error = pwmd_save(pwm);
592 #endif
595 if (!error && filename)
596 error = pwmd_command(pwm, &result, "UNLOCK");
598 if (error) {
599 show_error(error);
600 ret = EXIT_FAILURE;
603 pwmd_close(pwm);
605 if (socketpath)
606 xfree(socketpath);
608 exit(ret);