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]] [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"
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"
90 " -P path to the pinentry binary (server default)\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"
99 " -h this help text\n"), DEFAULT_PORT
);
108 static gpg_error_t
do_inquire(void *data
, const char *keyword
, gpg_error_t rc
,
109 char **result
, size_t *result_len
)
112 static char buf
[ASSUAN_LINELENGTH
];
115 struct inquire_s
*inq
= (struct inquire_s
*)data
;
118 memset(buf
, 0, sizeof(buf
));
126 snprintf(buf
, sizeof(buf
), "%s", inq
->data
);
133 while ((c
= fgetc(inq
->fp
)) != EOF
) {
134 if (len
== sizeof(buf
)) {
144 memset(buf
, 0, sizeof(buf
));
153 static int status_msg_cb(void *data
, const char *line
)
155 fprintf(stderr
, "%s\n", line
);
160 static gpg_error_t
do_nb_command(int fd
, int which
)
163 gpg_error_t error
= 0;
165 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
169 struct timeval tv
= {0, 50000};
173 n
= select(fd
+1, &fds
, NULL
, NULL
, &tv
);
176 if (FD_ISSET(fd
, &fds
)) {
177 pwmd_nb_status_t status
;
179 n
= read(fd
, &status
, sizeof(status
));
182 error
= gpg_error_from_errno(errno
);
187 error
= pwmd_open_nb_finalize(pwm
, &status
);
189 error
= pwmd_save_nb_finalize(pwm
, &status
);
204 int main(int argc
, char *argv
[])
207 char *password
= NULL
;
208 char *filename
= NULL
;
209 char *socketpath
= NULL
;
210 char command
[ASSUAN_LINELENGTH
], *p
;
211 int ret
= EXIT_SUCCESS
;
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
;
222 char *clientname
= NULL
;
223 char *inquire
= NULL
;
226 #ifdef HAVE_LIBGNUTLS
228 int port
= DEFAULT_PORT
;
229 int verify_hostname
= 0;
237 setlocale(LC_ALL
, "");
238 bindtextdomain("libpwmd", LOCALEDIR
);
241 #ifdef HAVE_LIBGNUTLS
242 while ((opt
= getopt(argc
, argv
, "VH:R:y:t:E:c:I:XT:N:D:hvP:p:s:Si:d:")) != EOF
) {
244 while ((opt
= getopt(argc
, argv
, "y:t:E:c:I:XT:N:D:hvP:p:s:Si:d:")) != EOF
) {
247 #ifdef HAVE_LIBGNUTLS
248 while ((opt
= getopt(argc
, argv
, "VH:R:t:c:I:XT:N:D:hvP:p:s:Si:d:")) != EOF
) {
250 while ((opt
= getopt(argc
, argv
, "t:c:I:XT:N:D:hvP:p:s:Si:d:")) != EOF
) {
256 method
= atoi(optarg
);
262 tries
= atoi(optarg
);
265 #ifdef HAVE_LIBGNUTLS
267 host
= xstrdup(optarg
);
277 timeout
= atoi(optarg
);
280 clientname
= xstrdup(optarg
);
295 inquirefd
= atoi(optarg
);
296 inquirefp
= fdopen(inquirefd
, "r");
300 err(EXIT_FAILURE
, "%i", inquirefd
);
304 outfd
= atoi(optarg
);
305 outfp
= fdopen(outfd
, "w");
309 err(EXIT_FAILURE
, "%i", outfd
);
324 socketpath
= xstrdup(optarg
);
327 password
= xstrdup(optarg
);
328 memset(optarg
, 0, strlen(optarg
));
331 pinentry_path
= xstrdup(optarg
);
335 printf("%s (pwmc)\n%s\n", PACKAGE_STRING
, PACKAGE_BUGREPORT
);
344 filename
= argv
[optind
];
347 #ifdef HAVE_LIBGNUTLS
349 if (verify_hostname
) {
350 error
= pwmd_setopt(pwm
, PWMD_OPTION_VERIFY_HOSTNAME
, 1);
360 if ((pwm
= pwmd_tcp_connect_async(host
, port
, &error
, NULL
, NULL
,
363 errx(EXIT_FAILURE
, "%s: %s", host
, pwmd_strerror(error
));
367 s
= pwmd_process(pwm
, &error
);
370 } while (s
== ASYNC_PROCESS
);
375 errx(EXIT_FAILURE
, "%s: %s", host
, pwmd_strerror(error
));
382 if ((pwm
= pwmd_tcp_connect(host
, port
, &error
, NULL
, NULL
, NULL
))
385 errx(EXIT_FAILURE
, "%s: %s", host
, pwmd_strerror(error
));
393 if ((pwm
= pwmd_connect(socketpath
, &error
)) == NULL
) {
395 errx(EXIT_FAILURE
, "pwmd_connect(): %s", pwmd_strerror(error
));
397 #ifdef HAVE_LIBGNUTLS
401 error
= pwmd_command(pwm
, &result
, "OPTION CLIENT NAME=%s", clientname
? clientname
: "pwmc");
406 errx(EXIT_FAILURE
, "pwmd_command(): %s", pwmd_strerror(error
));
410 error
= pwmd_command(pwm
, &result
, "OPTION TIMEOUT=%i", timeout
);
414 errx(EXIT_FAILURE
, "pwmd_command(): %s", pwmd_strerror(error
));
419 error
= pwmd_setopt(pwm
, PWMD_OPTION_PASSWORD
, password
);
430 error
= pwmd_command(pwm
, &result
, "OPTION PATH=%s", pinentry_path
);
437 error
= pwmd_command(pwm
, &result
, "OPTION DISPLAY=%s", display
);
444 error
= pwmd_command(pwm
, &result
, "OPTION TTYNAME=%s", tty
);
451 error
= pwmd_command(pwm
, &result
, "OPTION TTYTYPE=%s", ttytype
);
458 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY
, 1);
465 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TRIES
, tries
);
474 error
= pwmd_setopt(pwm
, PWMD_OPTION_STATUS_FUNC
, status_msg_cb
);
482 /* This method doesn't support PWMD_OPTION_PINENTRY_TRIES. */
484 error
= pwmd_open_async(pwm
, filename
);
488 s
= pwmd_process(pwm
, &error
);
491 } while (s
== ASYNC_PROCESS
);
496 else if (method
== 2) {
497 int fd
= pwmd_open_nb(pwm
, &error
, filename
, timeout
);
502 error
= do_nb_command(fd
, 0);
505 error
= pwmd_open(pwm
, filename
);
507 error
= pwmd_open(pwm
, filename
);
515 error
= pwmd_command(pwm
, &result
, "LOCK");
521 p
= fgets(command
, sizeof(command
), stdin
);
527 * This is a known INQUIRE command. We use pwmd_inquire() to send the
528 * data from the do_inquire() callback function.
530 if (strncasecmp(p
, "STORE ", 6) == 0) {
532 inquire
= (char *)"STORE";
534 else if (strncasecmp(p
, "IMPORT ", 7) == 0) {
536 inquire
= (char *)"IMPORT";
540 struct inquire_s
*inq
= (struct inquire_s
*)malloc(sizeof(struct inquire_s
));
543 error
= gpg_error_from_errno(ENOMEM
);
547 inq
->data
= xstrdup(p
);
549 error
= pwmd_inquire(pwm
, inquire
, do_inquire
, inq
);
554 if (strcasecmp(p
, "BYE") == 0)
557 error
= pwmd_command(pwm
, &result
, command
);
558 memset(command
, 0, sizeof(command
));
564 fwrite(result
, 1, strlen(result
), outfp
);
565 pwmd_free_result(result
);
569 memset(command
, 0, sizeof(command
));
571 if (!error
&& save
) {
573 error
= pwmd_command(pwm
, &result
, "OPTION ITERATIONS=%i", iter
);
581 error
= pwmd_save_async(pwm
);
585 s
= pwmd_process(pwm
, &error
);
588 } while (s
== ASYNC_PROCESS
);
593 else if (method
== 3) {
594 int fd
= pwmd_save_nb(pwm
, &error
);
599 error
= do_nb_command(fd
, 1);
602 error
= pwmd_save(pwm
);
604 error
= pwmd_save(pwm
);
608 if (!error
&& filename
)
609 error
= pwmd_command(pwm
, &result
, "UNLOCK");