1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2007-2009 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)
45 #define DEFAULT_PORT 22
48 static void show_error(gpg_error_t error
)
50 fprintf(stderr
, "ERR %i %s\n", gpg_err_code(error
), pwmd_strerror(error
));
53 static void usage(const char *pn
)
56 "Read a PWMD protocol command from standard input.\n\n"
57 "Usage: pwmc [-hvX] [-s <socket>] "
61 "[-PTNDCM <string>] [-p <passphrase>]\n"
62 " [-S [-i <iter>]] [-c <name>] [-t <n>] [-d <fd>] [-I <fd>]\n"
64 " [-H <hostname> [-R <port>] -Y <identity> -K <known_hosts>\n"
65 " -U <username> [-G]] [filename]\n"
70 " -E pinentry method (0=pwmd, 1=pwmd async, 2=libpwmd nb)\n"
72 " -y number of pinentry tries before failing (3)\n"
74 " -H connect to hostname\n"
75 " -R alterate port (%i)\n"
77 " -Y SSH identity file\n"
78 " -K known host's file (for server validation)\n"
79 " -G retrieve the remote SSH host key and exit\n"
81 " -t pinentry timeout\n"
82 " -X disable showing of status messages from the server\n"
83 " -c set the client name\n"
84 " -s socket path (~/.pwmd/socket)\n"
86 " -P path to the pinentry binary (server default)\n"
88 " -N pinentry terminal type\n"
89 " -D pinentry display\n"
90 " -C pinentry LC_CTYPE\n"
91 " -M pinentry LC_MESSAGES\n"
92 " -d redirect command output to the specified file descriptor\n"
93 " -I read inquire data from the specified file descriptor\n"
94 " -S send the SAVE command before exiting\n"
95 " -i encrypt with the specified number of iterations\n"
98 " -h this help text\n"), DEFAULT_PORT
);
100 " -h this help text\n"));
110 static gpg_error_t
do_inquire(void *data
, const char *keyword
, gpg_error_t rc
,
111 char **result
, size_t *result_len
)
114 static char buf
[ASSUAN_LINELENGTH
];
117 struct inquire_s
*inq
= (struct inquire_s
*)data
;
120 memset(buf
, 0, sizeof(buf
));
128 snprintf(buf
, sizeof(buf
), "%s", inq
->data
);
135 while ((c
= fgetc(inq
->fp
)) != EOF
) {
136 if (len
== sizeof(buf
)) {
146 memset(buf
, 0, sizeof(buf
));
155 static int status_msg_cb(void *data
, const char *line
)
157 fprintf(stderr
, "%s\n", line
);
162 static gpg_error_t
do_nb_command(int fd
, int which
)
165 gpg_error_t error
= 0;
167 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
171 struct timeval tv
= {0, 50000};
175 n
= select(fd
+1, &fds
, NULL
, NULL
, &tv
);
178 if (FD_ISSET(fd
, &fds
)) {
179 pwmd_nb_status_t status
;
181 n
= read(fd
, &status
, sizeof(status
));
184 error
= gpg_error_from_errno(errno
);
189 error
= pwmd_open_nb_finalize(pwm
, &status
);
191 error
= pwmd_save_nb_finalize(pwm
, &status
);
206 int main(int argc
, char *argv
[])
209 char *password
= NULL
;
210 char *filename
= NULL
;
211 char *socketpath
= NULL
;
212 char command
[ASSUAN_LINELENGTH
], *p
;
213 int ret
= EXIT_SUCCESS
;
217 char *pinentry_path
= NULL
;
218 char *display
= NULL
, *tty
= NULL
, *ttytype
= NULL
, *lcctype
= NULL
,
220 int outfd
= STDOUT_FILENO
;
221 FILE *outfp
= stdout
;
222 int inquirefd
= STDIN_FILENO
;
223 FILE *inquirefp
= stdin
;
225 char *clientname
= NULL
;
226 char *inquire
= NULL
;
232 int port
= DEFAULT_PORT
;
233 char *username
= NULL
;
235 char *known_hosts
= NULL
;
245 setlocale(LC_ALL
, "");
246 bindtextdomain("libpwmd", LOCALEDIR
);
251 while ((opt
= getopt(argc
, argv
, "C:M:GK:U:Y:H:R:y:t:E:c:I:XT:N:D:hvP:p:s:Si:d:")) != EOF
) {
253 while ((opt
= getopt(argc
, argv
, "C:M:y:t:E:c:I:XT:N:D:hvP:p:s:Si:d:")) != EOF
) {
257 while ((opt
= getopt(argc
, argv
, "y:C:M:GK:U:Y:H:R:t:c:I:XT:N:D:hvP:p:s:Si:d:")) != EOF
) {
259 while ((opt
= getopt(argc
, argv
, "y:C:M:t:c:I:XT:N:D:hvP:p:s:Si:d:")) != EOF
) {
265 method
= atoi(optarg
);
272 tries
= atoi(optarg
);
276 host
= xstrdup(optarg
);
282 ident
= xstrdup(optarg
);
285 username
= xstrdup(optarg
);
288 known_hosts
= xstrdup(optarg
);
295 lcctype
= xstrdup(optarg
);
298 lcmessages
= xstrdup(optarg
);
301 timeout
= atoi(optarg
);
304 clientname
= xstrdup(optarg
);
319 inquirefd
= atoi(optarg
);
320 inquirefp
= fdopen(inquirefd
, "r");
324 err(EXIT_FAILURE
, "%i", inquirefd
);
328 outfd
= atoi(optarg
);
329 outfp
= fdopen(outfd
, "w");
333 err(EXIT_FAILURE
, "%i", outfd
);
340 iter
= strtol(optarg
, NULL
, 10);
344 socketpath
= xstrdup(optarg
);
347 password
= xstrdup(optarg
);
348 memset(optarg
, 0, strlen(optarg
));
351 pinentry_path
= xstrdup(optarg
);
355 printf("%s (pwmc)\n%s\n", PACKAGE_STRING
, PACKAGE_BUGREPORT
);
364 filename
= argv
[optind
];
375 pwm
= pwmd_get_hostkey_async(host
, port
, &error
);
378 errx(EXIT_FAILURE
, "%s: %s", host
, pwmd_strerror(error
));
381 s
= pwmd_process(pwm
, &error
);
384 } while (s
== ASYNC_PROCESS
);
388 errx(EXIT_FAILURE
, "%s: %s", host
, pwmd_strerror(error
));
391 error
= pwmd_get_result(pwm
, &hostkey
);
395 errx(EXIT_FAILURE
, "%s: %s", host
, pwmd_strerror(error
));
398 printf("%s\n", hostkey
);
404 if ((pwm
= pwmd_tcp_connect_async(host
, port
, ident
, username
, known_hosts
, &error
)) == NULL
) {
406 errx(EXIT_FAILURE
, "%s: %s", host
, pwmd_strerror(error
));
410 s
= pwmd_process(pwm
, &error
);
413 } while (s
== ASYNC_PROCESS
);
418 errx(EXIT_FAILURE
, "%s: %s", host
, pwmd_strerror(error
));
427 char *hostkey
= pwmd_get_hostkey(host
, port
, &error
);
430 errx(EXIT_FAILURE
, "%s: %s", host
, pwmd_strerror(error
));
432 printf("%s\n", hostkey
);
433 pwmd_free_result(hostkey
);
437 if ((pwm
= pwmd_tcp_connect(host
, port
, ident
, username
, known_hosts
, &error
)) == NULL
) {
439 errx(EXIT_FAILURE
, "%s: %s", host
, pwmd_strerror(error
));
447 if ((pwm
= pwmd_connect(socketpath
, &error
)) == NULL
) {
449 errx(EXIT_FAILURE
, "pwmd_connect(): %s", pwmd_strerror(error
));
455 error
= pwmd_command(pwm
, &result
, "OPTION CLIENT NAME=%s", clientname
? clientname
: "pwmc");
464 error
= pwmd_command(pwm
, &result
, "VERSION");
466 if (error
&& error
!= GPG_ERR_ASS_UNKNOWN_CMD
) {
471 pwmd_free_result(result
);
473 if (error
== GPG_ERR_ASS_UNKNOWN_CMD
) {
481 /* pwmd version 2 or later. */
491 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TIMEOUT
, timeout
);
500 error
= pwmd_setopt(pwm
, PWMD_OPTION_PASSWORD
, password
);
511 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_PATH
, pinentry_path
);
518 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_DISPLAY
, display
);
525 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TTY
, tty
);
532 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TERM
, ttytype
);
539 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_LC_CTYPE
, lcctype
);
546 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_LC_MESSAGES
,
555 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY
, 1);
563 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TRIES
, tries
);
571 error
= pwmd_setopt(pwm
, PWMD_OPTION_STATUS_FUNC
, status_msg_cb
);
579 /* This method doesn't support PWMD_OPTION_PINENTRY_TRIES. */
581 error
= pwmd_open_async(pwm
, filename
);
585 s
= pwmd_process(pwm
, &error
);
588 } while (s
== ASYNC_PROCESS
);
593 else if (method
== 2) {
594 int fd
= pwmd_open_nb(pwm
, &error
, filename
, timeout
);
599 error
= do_nb_command(fd
, 0);
602 error
= pwmd_open(pwm
, filename
);
604 error
= pwmd_open(pwm
, filename
);
612 error
= pwmd_command(pwm
, &result
, "LOCK");
621 struct timeval tv
= {0, 100000};
626 FD_SET(STDIN_FILENO
, &rfds
);
627 n
= select(STDIN_FILENO
+1, &rfds
, NULL
, NULL
, &tv
);
630 s
= pwmd_process(pwm
, &error
);
635 fprintf(stderr
, ".");
640 error
= gpg_error_from_errno(errno
);
644 fprintf(stderr
, "\n");
645 n
= read(STDIN_FILENO
, command
, sizeof(command
));
648 error
= gpg_error_from_errno(errno
);
652 if (n
&& command
[strlen(command
)-1] == '\n')
653 command
[strlen(command
)-1] = 0;
661 p
= fgets(command
, sizeof(command
), stdin
);
663 p
= fgets(command
, sizeof(command
), stdin
);
670 * This is a known INQUIRE command. We use pwmd_inquire() to send the
671 * data from the do_inquire() callback function.
673 if (strncasecmp(p
, "STORE ", 6) == 0) {
675 inquire
= (char *)"STORE";
677 else if (strncasecmp(p
, "IMPORT ", 7) == 0) {
679 inquire
= (char *)"IMPORT";
683 struct inquire_s
*inq
= (struct inquire_s
*)malloc(sizeof(struct inquire_s
));
686 error
= gpg_error_from_errno(ENOMEM
);
690 inq
->data
= xstrdup(p
);
692 error
= pwmd_inquire(pwm
, inquire
, do_inquire
, inq
);
697 if (strcasecmp(p
, "BYE") == 0)
700 error
= pwmd_command(pwm
, &result
, command
);
701 memset(command
, 0, sizeof(command
));
707 fwrite(result
, 1, strlen(result
), outfp
);
708 pwmd_free_result(result
);
712 memset(command
, 0, sizeof(command
));
714 if (!error
&& save
) {
716 error
= pwmd_command(pwm
, &result
, "OPTION ITERATIONS=%i", iter
);
724 error
= pwmd_save_async(pwm
);
728 s
= pwmd_process(pwm
, &error
);
731 } while (s
== ASYNC_PROCESS
);
736 else if (method
== 3) {
737 int fd
= pwmd_save_nb(pwm
, &error
);
742 error
= do_nb_command(fd
, 1);
745 error
= pwmd_save(pwm
);
747 error
= pwmd_save(pwm
);
751 if (!error
&& filename
)
752 error
= pwmd_command(pwm
, &result
, "UNLOCK");