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 02110-1301 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
53 #define DEFAULT_PORT 6466
56 static void show_error(gpg_error_t error
)
58 fprintf(stderr
, "ERR %i %s\n", gpg_err_code(error
), pwmd_strerror(error
));
61 static void usage(const char *pn
)
64 "Reads PWMD protocol commands from standard input.\n\n"
65 "Usage: pwmc [-hvX] [-s <socket>] "
69 "[-PTND <string>] [-p <passphrase>]\n"
70 " [-S [-i <iter>]] [-c <name>] [-t <n>] [-d <fd>] [-I <fd>]\n"
72 " [-H <hostname> [-R <port>] [-V] [-C <cipher>]] [filename]\n"
77 " -E pinentry method (0=pwmd, 1=pwmd async, 2=libpwmd nb)\n"
78 " -y number of pinentry tries before failing (3)\n"
81 " -H connect to hostname\n"
82 " -R alterate port (%i)\n"
83 " -V verify peer hostname\n"
84 " -C cipher suite to use (in priority format)\n"
86 " -t pinentry timeout\n"
87 " -X disable showing of status messages from the server\n"
88 " -c set the client name\n"
89 " -s socket path (~/.pwmd/socket)\n"
91 " -P path to the pinentry binary (server default)\n"
93 " -N pinentry terminal type\n"
94 " -D pinentry display\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 before exiting\n"
98 " -i encrypt with the specified number of iterations (-1 = 0 iterations)\n"
100 " -h this help text\n"), DEFAULT_PORT
);
109 static gpg_error_t
do_inquire(void *data
, const char *keyword
, gpg_error_t rc
,
110 char **result
, size_t *result_len
)
113 static char buf
[ASSUAN_LINELENGTH
];
116 struct inquire_s
*inq
= (struct inquire_s
*)data
;
119 memset(buf
, 0, sizeof(buf
));
127 snprintf(buf
, sizeof(buf
), "%s", inq
->data
);
134 while ((c
= fgetc(inq
->fp
)) != EOF
) {
135 if (len
== sizeof(buf
)) {
145 memset(buf
, 0, sizeof(buf
));
154 static int status_msg_cb(void *data
, const char *line
)
156 fprintf(stderr
, "%s\n", line
);
161 static gpg_error_t
do_nb_command(int fd
, int which
)
164 gpg_error_t error
= 0;
166 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
170 struct timeval tv
= {0, 50000};
174 n
= select(fd
+1, &fds
, NULL
, NULL
, &tv
);
177 if (FD_ISSET(fd
, &fds
)) {
178 pwmd_nb_status_t status
;
180 n
= read(fd
, &status
, sizeof(status
));
183 error
= gpg_error_from_errno(errno
);
188 error
= pwmd_open_nb_finalize(pwm
, &status
);
190 error
= pwmd_save_nb_finalize(pwm
, &status
);
205 int main(int argc
, char *argv
[])
208 char *password
= NULL
;
209 char *filename
= NULL
;
210 char *socketpath
= NULL
;
211 char command
[ASSUAN_LINELENGTH
], *p
;
212 int ret
= EXIT_SUCCESS
;
216 char *pinentry_path
= NULL
;
217 char *display
= NULL
, *tty
= NULL
, *ttytype
= NULL
;
218 int outfd
= STDOUT_FILENO
;
219 FILE *outfp
= stdout
;
220 int inquirefd
= STDIN_FILENO
;
221 FILE *inquirefp
= stdin
;
223 char *clientname
= NULL
;
224 char *inquire
= NULL
;
229 int port
= DEFAULT_PORT
;
239 setlocale(LC_ALL
, "");
240 bindtextdomain("libpwmd", LOCALEDIR
);
244 while ((opt
= getopt(argc
, argv
, "C:VH:R:y:t:E:c:I:XT:N:D:hvP:p:s:Si:d:")) != EOF
) {
246 while ((opt
= getopt(argc
, argv
, "y:t:E:c:I:XT:N:D:hvP:p:s:Si:d:")) != EOF
) {
250 while ((opt
= getopt(argc
, argv
, "C:VH:R:t:c:I:XT:N:D:hvP:p:s:Si:d:")) != EOF
) {
252 while ((opt
= getopt(argc
, argv
, "t:c:I:XT:N:D:hvP:p:s:Si:d:")) != EOF
) {
258 method
= atoi(optarg
);
264 tries
= atoi(optarg
);
269 host
= xstrdup(optarg
);
282 timeout
= atoi(optarg
);
285 clientname
= xstrdup(optarg
);
300 inquirefd
= atoi(optarg
);
301 inquirefp
= fdopen(inquirefd
, "r");
305 err(EXIT_FAILURE
, "%i", inquirefd
);
309 outfd
= atoi(optarg
);
310 outfp
= fdopen(outfd
, "w");
314 err(EXIT_FAILURE
, "%i", outfd
);
329 socketpath
= xstrdup(optarg
);
332 password
= xstrdup(optarg
);
333 memset(optarg
, 0, strlen(optarg
));
336 pinentry_path
= xstrdup(optarg
);
340 printf("%s (pwmc)\n%s\n", PACKAGE_STRING
, PACKAGE_BUGREPORT
);
349 filename
= argv
[optind
];
356 if ((pwm
= pwmd_tcp_connect_async(host
, port
, &error
, NULL
, NULL
,
357 NULL
, verify
, cipher
)) == NULL
) {
359 errx(EXIT_FAILURE
, "%s: %s", host
, pwmd_strerror(error
));
363 s
= pwmd_process(pwm
, &error
);
366 } while (s
== ASYNC_PROCESS
);
371 errx(EXIT_FAILURE
, "%s: %s", host
, pwmd_strerror(error
));
378 if ((pwm
= pwmd_tcp_connect(host
, port
, &error
, NULL
, NULL
, NULL
,
382 errx(EXIT_FAILURE
, "%s: %s", host
, pwmd_strerror(error
));
390 if ((pwm
= pwmd_connect(socketpath
, &error
)) == NULL
) {
392 errx(EXIT_FAILURE
, "pwmd_connect(): %s", pwmd_strerror(error
));
398 error
= pwmd_command(pwm
, &result
, "OPTION CLIENT NAME=%s", clientname
? clientname
: "pwmc");
403 errx(EXIT_FAILURE
, "pwmd_command(): %s", pwmd_strerror(error
));
407 error
= pwmd_command(pwm
, &result
, "OPTION TIMEOUT=%i", timeout
);
411 errx(EXIT_FAILURE
, "pwmd_command(): %s", pwmd_strerror(error
));
416 error
= pwmd_setopt(pwm
, PWMD_OPTION_PASSWORD
, password
);
427 error
= pwmd_command(pwm
, &result
, "OPTION PATH=%s", pinentry_path
);
434 error
= pwmd_command(pwm
, &result
, "OPTION DISPLAY=%s", display
);
441 error
= pwmd_command(pwm
, &result
, "OPTION TTYNAME=%s", tty
);
448 error
= pwmd_command(pwm
, &result
, "OPTION TTYTYPE=%s", ttytype
);
455 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY
, 1);
462 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TRIES
, tries
);
471 error
= pwmd_setopt(pwm
, PWMD_OPTION_STATUS_FUNC
, status_msg_cb
);
479 /* This method doesn't support PWMD_OPTION_PINENTRY_TRIES. */
481 error
= pwmd_open_async(pwm
, filename
);
485 s
= pwmd_process(pwm
, &error
);
488 } while (s
== ASYNC_PROCESS
);
493 else if (method
== 2) {
494 int fd
= pwmd_open_nb(pwm
, &error
, filename
, timeout
);
499 error
= do_nb_command(fd
, 0);
502 error
= pwmd_open(pwm
, filename
);
504 error
= pwmd_open(pwm
, filename
);
512 error
= pwmd_command(pwm
, &result
, "LOCK");
518 p
= fgets(command
, sizeof(command
), stdin
);
524 * This is a known INQUIRE command. We use pwmd_inquire() to send the
525 * data from the do_inquire() callback function.
527 if (strncasecmp(p
, "STORE ", 6) == 0) {
529 inquire
= (char *)"STORE";
531 else if (strncasecmp(p
, "IMPORT ", 7) == 0) {
533 inquire
= (char *)"IMPORT";
537 struct inquire_s
*inq
= (struct inquire_s
*)malloc(sizeof(struct inquire_s
));
540 error
= gpg_error_from_errno(ENOMEM
);
544 inq
->data
= xstrdup(p
);
546 error
= pwmd_inquire(pwm
, inquire
, do_inquire
, inq
);
551 if (strcasecmp(p
, "BYE") == 0)
554 error
= pwmd_command(pwm
, &result
, command
);
555 memset(command
, 0, sizeof(command
));
561 fwrite(result
, 1, strlen(result
), outfp
);
562 pwmd_free_result(result
);
566 memset(command
, 0, sizeof(command
));
568 if (!error
&& save
) {
570 error
= pwmd_command(pwm
, &result
, "OPTION ITERATIONS=%i", iter
);
578 error
= pwmd_save_async(pwm
);
582 s
= pwmd_process(pwm
, &error
);
585 } while (s
== ASYNC_PROCESS
);
590 else if (method
== 3) {
591 int fd
= pwmd_save_nb(pwm
, &error
);
596 error
= do_nb_command(fd
, 1);
599 error
= pwmd_save(pwm
);
601 error
= pwmd_save(pwm
);
605 if (!error
&& filename
)
606 error
= pwmd_command(pwm
, &result
, "UNLOCK");