1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
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
28 #include <sys/select.h>
41 #define N_(msgid) gettext(msgid)
47 #define xrealloc realloc
48 #define xmalloc malloc
49 #define xstrdup strdup
50 #define xcalloc calloc
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
)
63 "Reads PWMD protocol commands from standard input.\n\n"
65 "Usage: pwmc [-hvX] [-s <socket>] [-E <n> -P -T -N -D | -p <password>] [-S [-i <iter>]]\n"
67 "Usage: pwmc [-hvX] [-s <socket>] [-P -T -N -D | -p <password>]\n"
69 " [-S [-i <iter>]] [-c <name>] [-d <fd>] [-I <fd>] [filename]\n"
71 " -E pinentry method (0=pwmd, 1=pwmd async, 2=libpwmd, 3=libpwmd nb)\n"
72 " -M pinentry timeout (method 3 only)\n"
74 " -X disable showing of status messages from the server\n"
75 " -c set the client name\n"
76 " -s socket path (~/.pwmd/socket)\n"
78 " -P path to the pinentry binary (server default)\n"
80 " -N pinentry terminal type\n"
81 " -D pinentry display\n"
82 " -d redirect command output to the specified file descriptor\n"
83 " -I read inquire data from the specified file descriptor\n"
84 " -S send the SAVE command before exiting\n"
85 " -i encrypt with the specified number of iterations (-1 = 0 iterations)\n"
87 " -h this help text\n"));
96 static gpg_error_t
do_inquire(void *data
, const char *keyword
, gpg_error_t rc
,
97 char **result
, size_t *result_len
)
100 static char buf
[ASSUAN_LINELENGTH
];
103 struct inquire_s
*inq
= (struct inquire_s
*)data
;
106 memset(buf
, 0, sizeof(buf
));
114 snprintf(buf
, sizeof(buf
), "%s", inq
->data
);
121 while ((c
= fgetc(inq
->fp
)) != EOF
) {
122 if (len
== sizeof(buf
)) {
132 memset(buf
, 0, sizeof(buf
));
141 static int status_msg_cb(void *data
, const char *line
)
143 fprintf(stderr
, "%s\n", line
);
148 static gpg_error_t
do_nb_command(int fd
, int which
)
151 gpg_error_t error
= 0;
153 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
157 struct timeval tv
= {0, 50000};
161 n
= select(fd
+1, &fds
, NULL
, NULL
, &tv
);
164 if (FD_ISSET(fd
, &fds
)) {
165 pwmd_nb_status_t status
;
167 n
= read(fd
, &status
, sizeof(status
));
170 error
= gpg_error_from_errno(errno
);
175 error
= pwmd_open_nb_finalize(pwm
, &status
);
177 error
= pwmd_save_nb_finalize(pwm
, &status
);
184 fprintf(stderr
, "Waiting ...\n");
192 int main(int argc
, char *argv
[])
195 char *password
= NULL
;
196 char *filename
= NULL
;
197 char *socketpath
= NULL
;
198 char command
[ASSUAN_LINELENGTH
], *p
;
199 int ret
= EXIT_SUCCESS
;
203 char *pinentry_path
= NULL
;
204 char *display
= NULL
, *tty
= NULL
, *ttytype
= NULL
;
205 int outfd
= STDOUT_FILENO
;
206 FILE *outfp
= stdout
;
207 int inquirefd
= STDIN_FILENO
;
208 FILE *inquirefp
= stdin
;
210 char *clientname
= NULL
;
211 char *inquire
= NULL
;
219 setlocale(LC_ALL
, "");
220 bindtextdomain("libpwmd", LOCALEDIR
);
223 while ((opt
= getopt(argc
, argv
, "E:M:c:I:XT:N:D:hvP:p:s:Si:d:")) != EOF
) {
225 while ((opt
= getopt(argc
, argv
, "c:I:XT:N:D:hvP:p:s:Si:d:")) != EOF
) {
230 method
= atoi(optarg
);
233 timeout
= atoi(optarg
);
237 clientname
= xstrdup(optarg
);
252 inquirefd
= atoi(optarg
);
253 inquirefp
= fdopen(inquirefd
, "r");
257 err(EXIT_FAILURE
, "%i", inquirefd
);
261 outfd
= atoi(optarg
);
262 outfp
= fdopen(outfd
, "w");
266 err(EXIT_FAILURE
, "%i", outfd
);
281 socketpath
= xstrdup(optarg
);
284 password
= xstrdup(optarg
);
285 memset(optarg
, 0, strlen(optarg
));
288 pinentry_path
= xstrdup(optarg
);
292 printf("%s (pwmc)\n%s\n", PACKAGE_STRING
, PACKAGE_BUGREPORT
);
301 filename
= argv
[optind
];
304 if ((pwm
= pwmd_connect(socketpath
, &error
)) == NULL
) {
306 errx(EXIT_FAILURE
, "pwmd_connect(): %s", pwmd_strerror(error
));
309 error
= pwmd_command(pwm
, &result
, "OPTION CLIENT NAME=%s", clientname
? clientname
: "pwmc");
314 errx(EXIT_FAILURE
, "pwmd_connect(): %s", pwmd_strerror(error
));
318 error
= pwmd_setopt(pwm
, PWMD_OPTION_PASSWORD
, password
);
329 error
= pwmd_command(pwm
, &result
, "OPTION PATH=%s", pinentry_path
);
336 error
= pwmd_command(pwm
, &result
, "OPTION DISPLAY=%s", display
);
343 error
= pwmd_command(pwm
, &result
, "OPTION TTYNAME=%s", tty
);
350 error
= pwmd_command(pwm
, &result
, "OPTION TTYTYPE=%s", ttytype
);
357 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY
, 1);
366 error
= pwmd_setopt(pwm
, PWMD_OPTION_STATUS_FUNC
, status_msg_cb
);
375 error
= pwmd_open_async(pwm
, filename
);
379 s
= pwmd_process(pwm
, &error
);
380 fprintf(stderr
, "Waiting ...\n");
382 } while (s
== ASYNC_PROCESS
);
387 else if (method
== 3) {
388 int fd
= pwmd_open_nb(pwm
, &error
, filename
, timeout
);
393 error
= do_nb_command(fd
, 0);
396 error
= pwmd_open(pwm
, filename
);
398 error
= pwmd_open(pwm
, filename
);
405 p
= fgets(command
, sizeof(command
), stdin
);
411 * This is a known INQUIRE command. We use pwmd_inquire() to send the
412 * data from the do_inquire() callback function.
414 if (strncasecmp(p
, "STORE ", 6) == 0) {
416 inquire
= (char *)"STORE";
418 else if (strncasecmp(p
, "IMPORT ", 7) == 0) {
420 inquire
= (char *)"IMPORT";
424 struct inquire_s
*inq
= (struct inquire_s
*)malloc(sizeof(struct inquire_s
));
427 error
= gpg_error_from_errno(ENOMEM
);
431 inq
->data
= xstrdup(p
);
433 error
= pwmd_inquire(pwm
, inquire
, do_inquire
, inq
);
438 if (strcasecmp(p
, "BYE") == 0)
441 error
= pwmd_command(pwm
, &result
, command
);
442 memset(command
, 0, sizeof(command
));
448 fwrite(result
, 1, strlen(result
), outfp
);
449 pwmd_free_result(result
);
453 memset(command
, 0, sizeof(command
));
455 if (!error
&& save
) {
457 error
= pwmd_command(pwm
, &result
, "OPTION ITERATIONS=%i", iter
);
465 error
= pwmd_save_async(pwm
);
469 s
= pwmd_process(pwm
, &error
);
470 fprintf(stderr
, "Waiting ...\n");
472 } while (s
== ASYNC_PROCESS
);
477 else if (method
== 3) {
478 int fd
= pwmd_save_nb(pwm
, &error
);
483 error
= do_nb_command(fd
, 1);
486 error
= pwmd_save(pwm
);
488 error
= pwmd_save(pwm
);