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 22
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>] -Y <identity> -K <known_hosts>\n"
73 " -U <username> [-G]] [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 " -Y SSH identity file\n"
86 " -K known host's file (for server validation)\n"
87 " -G retrieve the remote SSH host key and exit\n"
89 " -t pinentry timeout\n"
90 " -X disable showing of status messages from the server\n"
91 " -c set the client name\n"
92 " -s socket path (~/.pwmd/socket)\n"
94 " -P path to the pinentry binary (server default)\n"
96 " -N pinentry terminal type\n"
97 " -D pinentry display\n"
98 " -d redirect command output to the specified file descriptor\n"
99 " -I read inquire data from the specified file descriptor\n"
100 " -S send the SAVE command before exiting\n"
101 " -i encrypt with the specified number of iterations\n"
103 " -h this help text\n"), DEFAULT_PORT
);
112 static gpg_error_t
do_inquire(void *data
, const char *keyword
, gpg_error_t rc
,
113 char **result
, size_t *result_len
)
116 static char buf
[ASSUAN_LINELENGTH
];
119 struct inquire_s
*inq
= (struct inquire_s
*)data
;
122 memset(buf
, 0, sizeof(buf
));
130 snprintf(buf
, sizeof(buf
), "%s", inq
->data
);
137 while ((c
= fgetc(inq
->fp
)) != EOF
) {
138 if (len
== sizeof(buf
)) {
148 memset(buf
, 0, sizeof(buf
));
157 static int status_msg_cb(void *data
, const char *line
)
159 fprintf(stderr
, "%s\n", line
);
164 static gpg_error_t
do_nb_command(int fd
, int which
)
167 gpg_error_t error
= 0;
169 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
173 struct timeval tv
= {0, 50000};
177 n
= select(fd
+1, &fds
, NULL
, NULL
, &tv
);
180 if (FD_ISSET(fd
, &fds
)) {
181 pwmd_nb_status_t status
;
183 n
= read(fd
, &status
, sizeof(status
));
186 error
= gpg_error_from_errno(errno
);
191 error
= pwmd_open_nb_finalize(pwm
, &status
);
193 error
= pwmd_save_nb_finalize(pwm
, &status
);
208 int main(int argc
, char *argv
[])
211 char *password
= NULL
;
212 char *filename
= NULL
;
213 char *socketpath
= NULL
;
214 char command
[ASSUAN_LINELENGTH
], *p
;
215 int ret
= EXIT_SUCCESS
;
219 char *pinentry_path
= NULL
;
220 char *display
= NULL
, *tty
= NULL
, *ttytype
= NULL
;
221 int outfd
= STDOUT_FILENO
;
222 FILE *outfp
= stdout
;
223 int inquirefd
= STDIN_FILENO
;
224 FILE *inquirefp
= stdin
;
226 char *clientname
= NULL
;
227 char *inquire
= NULL
;
233 int port
= DEFAULT_PORT
;
234 char *username
= NULL
;
236 char *known_hosts
= NULL
;
245 setlocale(LC_ALL
, "");
246 bindtextdomain("libpwmd", LOCALEDIR
);
250 while ((opt
= getopt(argc
, argv
, "GK:U:Y:H:R:y:t:E:c:I:XT:N:D:hvP:p:s:Si:d:")) != EOF
) {
252 while ((opt
= getopt(argc
, argv
, "y:t:E:c:I:XT:N:D:hvP:p:s:Si:d:")) != EOF
) {
256 while ((opt
= getopt(argc
, argv
, "GK:U:Y:H:R:t:c:I:XT:N:D:hvP:p:s:Si:d:")) != EOF
) {
258 while ((opt
= getopt(argc
, argv
, "t:c:I:XT:N:D:hvP:p:s:Si:d:")) != EOF
) {
264 method
= atoi(optarg
);
270 tries
= atoi(optarg
);
275 host
= xstrdup(optarg
);
281 ident
= xstrdup(optarg
);
284 username
= xstrdup(optarg
);
287 known_hosts
= xstrdup(optarg
);
294 timeout
= atoi(optarg
);
297 clientname
= xstrdup(optarg
);
312 inquirefd
= atoi(optarg
);
313 inquirefp
= fdopen(inquirefd
, "r");
317 err(EXIT_FAILURE
, "%i", inquirefd
);
321 outfd
= atoi(optarg
);
322 outfp
= fdopen(outfd
, "w");
326 err(EXIT_FAILURE
, "%i", outfd
);
333 iter
= strtol(optarg
, NULL
, 10);
337 socketpath
= xstrdup(optarg
);
340 password
= xstrdup(optarg
);
341 memset(optarg
, 0, strlen(optarg
));
344 pinentry_path
= xstrdup(optarg
);
348 printf("%s (pwmc)\n%s\n", PACKAGE_STRING
, PACKAGE_BUGREPORT
);
357 filename
= argv
[optind
];
368 pwm
= pwmd_get_hostkey_async(host
, port
, &error
);
371 errx(EXIT_FAILURE
, "%s: %s", host
, pwmd_strerror(error
));
374 s
= pwmd_process(pwm
, &error
);
377 } while (s
== ASYNC_PROCESS
);
381 errx(EXIT_FAILURE
, "%s: %s", host
, pwmd_strerror(error
));
384 error
= pwmd_get_result(pwm
, &hostkey
);
388 errx(EXIT_FAILURE
, "%s: %s", host
, pwmd_strerror(error
));
391 printf("%s\n", hostkey
);
397 if ((pwm
= pwmd_tcp_connect_async(host
, port
, ident
, username
, known_hosts
, &error
)) == NULL
) {
399 errx(EXIT_FAILURE
, "%s: %s", host
, pwmd_strerror(error
));
403 s
= pwmd_process(pwm
, &error
);
406 } while (s
== ASYNC_PROCESS
);
411 errx(EXIT_FAILURE
, "%s: %s", host
, pwmd_strerror(error
));
420 char *hostkey
= pwmd_get_hostkey(host
, port
, &error
);
423 errx(EXIT_FAILURE
, "%s: %s", host
, pwmd_strerror(error
));
425 printf("%s\n", hostkey
);
426 pwmd_free_result(hostkey
);
430 if ((pwm
= pwmd_tcp_connect(host
, port
, ident
, username
, known_hosts
, &error
)) == NULL
) {
432 errx(EXIT_FAILURE
, "%s: %s", host
, pwmd_strerror(error
));
440 if ((pwm
= pwmd_connect(socketpath
, &error
)) == NULL
) {
442 errx(EXIT_FAILURE
, "pwmd_connect(): %s", pwmd_strerror(error
));
448 error
= pwmd_command(pwm
, &result
, "OPTION CLIENT NAME=%s", clientname
? clientname
: "pwmc");
457 error
= pwmd_command(pwm
, &result
, "VERSION");
459 if (error
&& error
!= GPG_ERR_ASS_UNKNOWN_CMD
) {
464 pwmd_free_result(result
);
466 if (error
== GPG_ERR_ASS_UNKNOWN_CMD
) {
474 /* pwmd version 2 or later. */
484 error
= pwmd_command(pwm
, &result
, "OPTION TIMEOUT=%i", timeout
);
493 error
= pwmd_setopt(pwm
, PWMD_OPTION_PASSWORD
, password
);
504 error
= pwmd_command(pwm
, &result
, "OPTION PATH=%s", pinentry_path
);
511 error
= pwmd_command(pwm
, &result
, "OPTION DISPLAY=%s", display
);
518 error
= pwmd_command(pwm
, &result
, "OPTION TTYNAME=%s", tty
);
525 error
= pwmd_command(pwm
, &result
, "OPTION TTYTYPE=%s", ttytype
);
532 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY
, 1);
539 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TRIES
, tries
);
548 error
= pwmd_setopt(pwm
, PWMD_OPTION_STATUS_FUNC
, status_msg_cb
);
556 /* This method doesn't support PWMD_OPTION_PINENTRY_TRIES. */
558 error
= pwmd_open_async(pwm
, filename
);
562 s
= pwmd_process(pwm
, &error
);
565 } while (s
== ASYNC_PROCESS
);
570 else if (method
== 2) {
571 int fd
= pwmd_open_nb(pwm
, &error
, filename
, timeout
);
576 error
= do_nb_command(fd
, 0);
579 error
= pwmd_open(pwm
, filename
);
581 error
= pwmd_open(pwm
, filename
);
589 error
= pwmd_command(pwm
, &result
, "LOCK");
595 p
= fgets(command
, sizeof(command
), stdin
);
601 * This is a known INQUIRE command. We use pwmd_inquire() to send the
602 * data from the do_inquire() callback function.
604 if (strncasecmp(p
, "STORE ", 6) == 0) {
606 inquire
= (char *)"STORE";
608 else if (strncasecmp(p
, "IMPORT ", 7) == 0) {
610 inquire
= (char *)"IMPORT";
614 struct inquire_s
*inq
= (struct inquire_s
*)malloc(sizeof(struct inquire_s
));
617 error
= gpg_error_from_errno(ENOMEM
);
621 inq
->data
= xstrdup(p
);
623 error
= pwmd_inquire(pwm
, inquire
, do_inquire
, inq
);
628 if (strcasecmp(p
, "BYE") == 0)
631 error
= pwmd_command(pwm
, &result
, command
);
632 memset(command
, 0, sizeof(command
));
638 fwrite(result
, 1, strlen(result
), outfp
);
639 pwmd_free_result(result
);
643 memset(command
, 0, sizeof(command
));
645 if (!error
&& save
) {
647 error
= pwmd_command(pwm
, &result
, "OPTION ITERATIONS=%i", iter
);
655 error
= pwmd_save_async(pwm
);
659 s
= pwmd_process(pwm
, &error
);
662 } while (s
== ASYNC_PROCESS
);
667 else if (method
== 3) {
668 int fd
= pwmd_save_nb(pwm
, &error
);
673 error
= do_nb_command(fd
, 1);
676 error
= pwmd_save(pwm
);
678 error
= pwmd_save(pwm
);
682 if (!error
&& filename
)
683 error
= pwmd_command(pwm
, &result
, "UNLOCK");