2 /* vim:tw=78:ts=8:sw=4:set ft=c: */
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
29 #include <sys/select.h>
42 #define N_(msgid) gettext(msgid)
48 #define xrealloc realloc
49 #define xmalloc malloc
50 #define xstrdup strdup
51 #define xcalloc calloc
54 #define DEFAULT_PORT 6466
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
)
65 "Reads PWMD protocol commands from standard input.\n\n"
66 "Usage: pwmc [-hvX] [-s <socket>] "
70 "[-PTND <string>] [-p <password>]\n"
71 " [-S [-i <iter>]] [-c <name>] [-t <n>] [-d <fd>] [-I <fd>]\n"
73 " [-H <hostname> [-R <port>]] [filename]\n"
78 " -E pinentry method (0=pwmd, 1=pwmd async, 2=libpwmd nb)\n"
79 " -y number of pinentry tries before failing (3)\n"
82 " -H connect to hostname\n"
83 " -R alterate port (%i)\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
;
236 setlocale(LC_ALL
, "");
237 bindtextdomain("libpwmd", LOCALEDIR
);
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
) {
243 while ((opt
= getopt(argc
, argv
, "y:t:E:c:I:XT:N:D:hvP:p:s:Si:d:")) != EOF
) {
246 #ifdef HAVE_LIBGNUTLS
247 while ((opt
= getopt(argc
, argv
, "H:R:t:c:I:XT:N:D:hvP:p:s:Si:d:")) != EOF
) {
249 while ((opt
= getopt(argc
, argv
, "t:c:I:XT:N:D:hvP:p:s:Si:d:")) != EOF
) {
255 method
= atoi(optarg
);
261 tries
= atoi(optarg
);
264 #ifdef HAVE_LIBGNUTLS
266 host
= xstrdup(optarg
);
273 timeout
= atoi(optarg
);
276 clientname
= xstrdup(optarg
);
291 inquirefd
= atoi(optarg
);
292 inquirefp
= fdopen(inquirefd
, "r");
296 err(EXIT_FAILURE
, "%i", inquirefd
);
300 outfd
= atoi(optarg
);
301 outfp
= fdopen(outfd
, "w");
305 err(EXIT_FAILURE
, "%i", outfd
);
320 socketpath
= xstrdup(optarg
);
323 password
= xstrdup(optarg
);
324 memset(optarg
, 0, strlen(optarg
));
327 pinentry_path
= xstrdup(optarg
);
331 printf("%s (pwmc)\n%s\n", PACKAGE_STRING
, PACKAGE_BUGREPORT
);
340 filename
= argv
[optind
];
343 #ifdef HAVE_LIBGNUTLS
347 if ((pwm
= pwmd_tcp_connect_async(host
, port
, &error
, NULL
, NULL
,
350 errx(EXIT_FAILURE
, "pwmd_tcp_connect_async(): %s", pwmd_strerror(error
));
354 s
= pwmd_process(pwm
, &error
);
357 } while (s
== ASYNC_PROCESS
);
362 errx(EXIT_FAILURE
, "pwmd_tcp_connect_async(): %s", pwmd_strerror(error
));
369 if ((pwm
= pwmd_tcp_connect(host
, port
, &error
, NULL
, NULL
, NULL
))
372 errx(EXIT_FAILURE
, "pwmd_tcp_connect(): %s", pwmd_strerror(error
));
380 if ((pwm
= pwmd_connect(socketpath
, &error
)) == NULL
) {
382 errx(EXIT_FAILURE
, "pwmd_connect(): %s", pwmd_strerror(error
));
384 #ifdef HAVE_LIBGNUTLS
388 error
= pwmd_command(pwm
, &result
, "OPTION CLIENT NAME=%s", clientname
? clientname
: "pwmc");
393 errx(EXIT_FAILURE
, "pwmd_command(): %s", pwmd_strerror(error
));
397 error
= pwmd_command(pwm
, &result
, "OPTION TIMEOUT=%i", timeout
);
401 errx(EXIT_FAILURE
, "pwmd_command(): %s", pwmd_strerror(error
));
406 error
= pwmd_setopt(pwm
, PWMD_OPTION_PASSWORD
, password
);
417 error
= pwmd_command(pwm
, &result
, "OPTION PATH=%s", pinentry_path
);
424 error
= pwmd_command(pwm
, &result
, "OPTION DISPLAY=%s", display
);
431 error
= pwmd_command(pwm
, &result
, "OPTION TTYNAME=%s", tty
);
438 error
= pwmd_command(pwm
, &result
, "OPTION TTYTYPE=%s", ttytype
);
445 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY
, 1);
452 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TRIES
, tries
);
461 error
= pwmd_setopt(pwm
, PWMD_OPTION_STATUS_FUNC
, status_msg_cb
);
469 /* This method doesn't support PWMD_OPTION_PINENTRY_TRIES. */
471 error
= pwmd_open_async(pwm
, filename
);
475 s
= pwmd_process(pwm
, &error
);
478 } while (s
== ASYNC_PROCESS
);
483 else if (method
== 2) {
484 int fd
= pwmd_open_nb(pwm
, &error
, filename
, timeout
);
489 error
= do_nb_command(fd
, 0);
492 error
= pwmd_open(pwm
, filename
);
494 error
= pwmd_open(pwm
, filename
);
502 error
= pwmd_command(pwm
, &result
, "LOCK");
508 p
= fgets(command
, sizeof(command
), stdin
);
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) {
519 inquire
= (char *)"STORE";
521 else if (strncasecmp(p
, "IMPORT ", 7) == 0) {
523 inquire
= (char *)"IMPORT";
527 struct inquire_s
*inq
= (struct inquire_s
*)malloc(sizeof(struct inquire_s
));
530 error
= gpg_error_from_errno(ENOMEM
);
534 inq
->data
= xstrdup(p
);
536 error
= pwmd_inquire(pwm
, inquire
, do_inquire
, inq
);
541 if (strcasecmp(p
, "BYE") == 0)
544 error
= pwmd_command(pwm
, &result
, command
);
545 memset(command
, 0, sizeof(command
));
551 fwrite(result
, 1, strlen(result
), outfp
);
552 pwmd_free_result(result
);
556 memset(command
, 0, sizeof(command
));
558 if (!error
&& save
) {
560 error
= pwmd_command(pwm
, &result
, "OPTION ITERATIONS=%i", iter
);
568 error
= pwmd_save_async(pwm
);
572 s
= pwmd_process(pwm
, &error
);
575 } while (s
== ASYNC_PROCESS
);
580 else if (method
== 3) {
581 int fd
= pwmd_save_nb(pwm
, &error
);
586 error
= do_nb_command(fd
, 1);
589 error
= pwmd_save(pwm
);
591 error
= pwmd_save(pwm
);
595 if (!error
&& filename
)
596 error
= pwmd_command(pwm
, &result
, "UNLOCK");