When DEBUG is defined, pwmc has a new option -E to specify the
[libpwmd.git] / pwmc.c
blobbba31a7a96892a76fb8a234f74dca26f1ec89247
1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
2 /*
3 Copyright (C) 2007-2008 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 <libpwmd.h>
26 #include <assuan.h>
27 #ifdef DEBUG
28 #include <sys/select.h>
29 #include <fcntl.h>
30 #endif
32 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
36 #ifdef HAVE_LOCALE_H
37 #include <locale.h>
38 #endif
40 #include "gettext.h"
41 #define N_(msgid) gettext(msgid)
43 #ifndef MEM_DEBUG
44 #include "mem.h"
45 #else
46 #define xfree free
47 #define xrealloc realloc
48 #define xmalloc malloc
49 #define xstrdup strdup
50 #define xcalloc calloc
51 #endif
53 pwm_t *pwm;
55 static void show_error(gpg_error_t error)
57 fprintf(stderr, "ERR %i %s\n", gpg_err_code(error), pwmd_strerror(error));
60 static void usage(const char *pn)
62 fprintf(stderr, N_(
63 "Reads PWMD protocol commands from standard input.\n\n"
64 #ifdef DEBUG
65 "Usage: pwmc [-hvX] [-s <socket>] [-E <n> -P -T -N -D | -p <password>] [-S] \n"
66 #else
67 "Usage: pwmc [-hvX] [-s <socket>] [-P -T -N -D | -p <password>] [-S] \n"
68 #endif
69 " [-c <name>] [-d <fd>] [-I <fd>] [filename]\n"
70 #ifdef DEBUG
71 " -E pinentry method (0=pwmd, 1=pwmd async, 2=libpwmd, 3=libpwmd nb)\n"
72 #endif
73 " -X disable showing of status messages from the server\n"
74 " -c set the client name\n"
75 " -s socket path (~/.pwmd/socket)\n"
76 " -p password\n"
77 " -P path to the pinentry binary (server default)\n"
78 " -T pinentry tty\n"
79 " -N pinentry terminal type\n"
80 " -D pinentry display\n"
81 " -d redirect command output to the specified file descriptor\n"
82 " -I read inquire data from the specified file descriptor\n"
83 " -S send the SAVE command before exiting\n"
84 " -v version\n"
85 " -h this help text\n"));
86 exit(EXIT_FAILURE);
89 struct inquire_s {
90 FILE *fp;
91 char *data;
94 static gpg_error_t do_inquire(void *data, const char *keyword, gpg_error_t rc,
95 char **result, size_t *result_len)
97 int c;
98 static char buf[ASSUAN_LINELENGTH];
99 char *p;
100 size_t len = 0;
101 struct inquire_s *inq = (struct inquire_s *)data;
103 if (rc) {
104 memset(buf, 0, sizeof(buf));
105 return rc;
108 buf[0] = 0;
109 p = buf;
111 if (inq->data) {
112 snprintf(buf, sizeof(buf), "%s", inq->data);
113 xfree(inq->data);
114 inq->data = NULL;
115 len = strlen(buf);
116 p = buf + len;
119 while ((c = fgetc(inq->fp)) != EOF) {
120 if (len == sizeof(buf)) {
121 ungetc(c, inq->fp);
122 break;
125 *p++ = c;
126 len++;
129 if (!buf[0]) {
130 memset(buf, 0, sizeof(buf));
131 return GPG_ERR_EOF;
134 *result = buf;
135 *result_len = len;
136 return 0;
139 static int status_msg_cb(void *data, const char *line)
141 fprintf(stderr, "%s\n", line);
142 return 0;
145 #ifdef DEBUG
146 static gpg_error_t do_nb_command(int fd, int which)
148 int n;
149 gpg_error_t error = 0;
151 fcntl(fd, F_SETFL, O_NONBLOCK);
153 do {
154 fd_set fds;
155 struct timeval tv = {0, 50000};
157 FD_ZERO(&fds);
158 FD_SET(fd, &fds);
159 n = select(fd+1, &fds, NULL, NULL, &tv);
161 if (n > 0) {
162 if (FD_ISSET(fd, &fds)) {
163 pwmd_nb_status_t status;
165 n = read(fd, &status, sizeof(status));
167 if (n == -1) {
168 error = gpg_error_from_errno(errno);
169 goto done;
172 if (!which)
173 error = pwmd_open_nb_finalize(pwm, &status);
174 else
175 error = pwmd_save_nb_finalize(pwm, &status);
177 if (error)
178 goto done;
181 else
182 fprintf(stderr, "Waiting ...\n");
183 } while (n == 0);
185 done:
186 return error;
188 #endif
190 int main(int argc, char *argv[])
192 int opt;
193 char *password = NULL;
194 char *filename = NULL;
195 char *socketpath = NULL;
196 char command[ASSUAN_LINELENGTH], *p;
197 int ret = EXIT_SUCCESS;
198 gpg_error_t error;
199 char *result = NULL;
200 int save = 0;
201 char *pinentry_path = NULL;
202 char *display = NULL, *tty = NULL, *ttytype = NULL;
203 int outfd = STDOUT_FILENO;
204 FILE *outfp = stdout;
205 int inquirefd = STDIN_FILENO;
206 FILE *inquirefp = stdin;
207 int show_status = 1;
208 char *clientname = NULL;
209 char *inquire = NULL;
210 #ifdef DEBUG
211 int method = 0;
212 pwmd_async_t s;
213 #endif
215 setlocale(LC_ALL, "");
216 bindtextdomain("libpwmd", LOCALEDIR);
218 #ifdef DEBUG
219 while ((opt = getopt(argc, argv, "E:c:I:XT:N:D:hvP:p:s:Sd:")) != EOF) {
220 #else
221 while ((opt = getopt(argc, argv, "c:I:XT:N:D:hvP:p:s:Sd:")) != EOF) {
222 #endif
223 switch (opt) {
224 #ifdef DEBUG
225 case 'E':
226 method = atoi(optarg);
227 break;
228 #endif
229 case 'c':
230 clientname = xstrdup(optarg);
231 break;
232 case 'X':
233 show_status = 0;
234 break;
235 case 'T':
236 tty = optarg;
237 break;
238 case 'N':
239 ttytype = optarg;
240 break;
241 case 'D':
242 display = optarg;
243 break;
244 case 'I':
245 inquirefd = atoi(optarg);
246 inquirefp = fdopen(inquirefd, "r");
248 if (!inquirefp) {
249 xfree(password);
250 err(EXIT_FAILURE, "%i", inquirefd);
252 break;
253 case 'd':
254 outfd = atoi(optarg);
255 outfp = fdopen(outfd, "w");
257 if (!outfp) {
258 xfree(password);
259 err(EXIT_FAILURE, "%i", outfd);
261 break;
262 case 'S':
263 save = 1;
264 break;
265 case 's':
266 socketpath = xstrdup(optarg);
267 break;
268 case 'p':
269 password = xstrdup(optarg);
270 memset(optarg, 0, strlen(optarg));
271 break;
272 case 'P':
273 pinentry_path = xstrdup(optarg);
274 break;
275 case 'v':
276 xfree(password);
277 printf("%s (pwmc)\n%s\n", PACKAGE_STRING, PACKAGE_BUGREPORT);
278 exit(EXIT_SUCCESS);
279 case 'h':
280 default:
281 xfree(password);
282 usage(argv[0]);
286 filename = argv[optind];
287 pwmd_init();
289 if ((pwm = pwmd_connect(socketpath, &error)) == NULL) {
290 xfree(password);
291 errx(EXIT_FAILURE, "pwmd_connect(): %s", pwmd_strerror(error));
294 error = pwmd_command(pwm, &result, "OPTION CLIENT NAME=%s", clientname ? clientname : "pwmc");
295 xfree(clientname);
297 if (error) {
298 xfree(password);
299 errx(EXIT_FAILURE, "pwmd_connect(): %s", pwmd_strerror(error));
302 if (password) {
303 error = pwmd_setopt(pwm, PWMD_OPTION_PASSWORD, password);
305 if (error) {
306 xfree(password);
307 goto done;
310 xfree(password);
312 else {
313 if (pinentry_path) {
314 error = pwmd_command(pwm, &result, "OPTION PATH=%s", pinentry_path);
316 if (error)
317 goto done;
320 if (display) {
321 error = pwmd_command(pwm, &result, "OPTION DISPLAY=%s", display);
323 if (error)
324 goto done;
327 if (tty) {
328 error = pwmd_command(pwm, &result, "OPTION TTYNAME=%s", tty);
330 if (error)
331 goto done;
334 if (ttytype) {
335 error = pwmd_command(pwm, &result, "OPTION TTYTYPE=%s", ttytype);
337 if (error)
338 goto done;
340 #ifdef DEBUG
341 if (method >= 2) {
342 error = pwmd_setopt(pwm, PWMD_OPTION_PINENTRY, 1);
344 if (error)
345 goto done;
347 #endif
350 if (show_status) {
351 error = pwmd_setopt(pwm, PWMD_OPTION_STATUS_FUNC, status_msg_cb);
353 if (error)
354 goto done;
357 if (filename) {
358 #ifdef DEBUG
359 if (method == 1) {
360 error = pwmd_open_async(pwm, filename);
362 if (!error) {
363 do {
364 s = pwmd_process(pwm, &error);
365 fprintf(stderr, "Waiting ...\n");
366 usleep(50000);
367 } while (s == ASYNC_PROCESS);
369 pwmd_finalize(pwm);
372 else if (method == 3) {
373 int fd = pwmd_open_nb(pwm, &error, filename, 0);
375 if (fd == -1)
376 goto done;
377 else if (fd >= 0)
378 error = do_nb_command(fd, 0);
380 else
381 error = pwmd_open(pwm, filename);
382 #else
383 error = pwmd_open(pwm, filename);
384 #endif
386 if (error)
387 goto done;
390 p = fgets(command, sizeof(command), stdin);
392 if (!p)
393 goto done;
396 * This is a known INQUIRE command. We use pwmd_inquire() to send the
397 * data from the do_inquire() callback function.
399 if (strncasecmp(p, "STORE ", 6) == 0) {
400 p += 6;
401 inquire = "STORE";
403 else if (strncasecmp(p, "IMPORT ", 7) == 0) {
404 p += 7;
405 inquire = "IMPORT";
408 if (inquire) {
409 struct inquire_s *inq = (struct inquire_s *)malloc(sizeof(struct inquire_s));
411 if (!inq) {
412 error = gpg_error_from_errno(ENOMEM);
413 goto done;
416 inq->data = xstrdup(p);
417 inq->fp = inquirefp;
418 error = pwmd_inquire(pwm, inquire, do_inquire, inq);
419 free(inq);
420 goto done;
423 if (strcasecmp(p, "BYE") == 0)
424 goto done;
426 error = pwmd_command(pwm, &result, command);
427 memset(command, 0, sizeof(command));
429 if (error)
430 goto done;
432 if (result) {
433 fwrite(result, 1, strlen(result), outfp);
434 pwmd_free_result(result);
437 done:
438 memset(command, 0, sizeof(command));
440 if (!error && save) {
441 #ifdef DEBUG
442 if (method == 1) {
443 error = pwmd_save_async(pwm);
445 if (!error) {
446 do {
447 s = pwmd_process(pwm, &error);
448 fprintf(stderr, "Waiting ...\n");
449 usleep(50000);
450 } while (s == ASYNC_PROCESS);
452 pwmd_finalize(pwm);
455 else if (method == 3) {
456 int fd = pwmd_save_nb(pwm, &error);
458 if (fd == -1)
459 goto done;
460 else if (fd >= 0)
461 error = do_nb_command(fd, 1);
463 else
464 error = pwmd_save(pwm);
465 #else
466 error = pwmd_save(pwm);
467 #endif
470 if (error) {
471 show_error(error);
472 ret = EXIT_FAILURE;
475 pwmd_close(pwm);
477 if (socketpath)
478 xfree(socketpath);
480 exit(ret);