2 /* vim:tw=78:ts=8:sw=4:set ft=c: */
4 Copyright (C) 2007-2009 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>
41 #ifdef HAVE_GETOPT_LONG
46 #include "getopt_long.h"
50 #define N_(msgid) gettext(msgid)
54 #define DEFAULT_PORT 22
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
, int status
)
64 fprintf(status
== EXIT_FAILURE
? stderr
: stdout
, N_(
65 "Read a PWMD protocol command from standard input.\n\n"
66 "Usage: pwmc [options] [file]\n"
69 " pinentry method (0=pwmd, 1=libpwmd, 2=pwmd async, "
73 " number of pinentry tries before failing (3)\n"
75 " --host, -h <hostname>\n"
76 " connect to the specified hostname\n"
78 " alterate port (22)\n"
80 " SSH username (default is the invoking user)\n"
81 " --identity, -i <filename>\n"
82 " SSH identity file\n"
83 " --known-hosts, -k <filename>\n"
84 " known host's file (for server validation)\n"
85 " --get-hostkey, -g\n"
86 " retrieve the remote SSH host key and exit\n"
88 " try connecting via IPv4 only\n"
90 " try connecting via IPv6 only\n"
92 " --timeout <seconds>\n"
95 " disable showing of status messages from the server\n"
96 " --name, -n <string>\n"
97 " set the client name\n"
98 " --socket <filename>\n"
99 " local socket to connect to (~/.pwmd/socket)\n"
100 " --passphrase, -P <string>\n"
101 " passphrase to use (disables pinentry use)\n"
102 " --pinentry <path>\n"
103 " the full path to the pinentry binary (server default)\n"
104 " --ttyname, -y <path>\n"
105 " tty that pinentry will use\n"
106 " --ttytype, -t <string>\n"
107 " pinentry terminal type (default is TERM)\n"
109 " pinentry display (default is DISPLAY)\n"
110 " --lc-ctype <string>\n"
111 " locale setting for pinentry\n"
112 " --lc-messages <string>\n"
113 " locale setting for pinentry\n"
114 " --output-fd <FD>\n"
115 " redirect command output to the specified file descriptor\n"
116 " --inquire-fd <FD>\n"
117 " read inquire data from the specified file descriptor\n"
119 " send the SAVE command before exiting\n"
120 " --iterations, -I <N>\n"
121 " encrypt with the specified number of iterations when saving\n"
132 static gpg_error_t
do_inquire(void *data
, const char *keyword
, gpg_error_t rc
,
133 char **result
, size_t *result_len
)
136 static char buf
[ASSUAN_LINELENGTH
];
139 struct inquire_s
*inq
= (struct inquire_s
*)data
;
142 memset(buf
, 0, sizeof(buf
));
150 snprintf(buf
, sizeof(buf
), "%s", inq
->data
);
151 pwmd_free(inq
->data
);
157 while ((c
= fgetc(inq
->fp
)) != EOF
) {
158 if (len
== sizeof(buf
)) {
168 memset(buf
, 0, sizeof(buf
));
177 static int status_msg_cb(void *data
, const char *line
)
179 fprintf(stderr
, "%s\n", line
);
183 static gpg_error_t
process_cmd(pwm_t
*pwm
, char **result
, int input
)
192 pwmd_fd_t pfds
[nfds
];
195 rc
= pwmd_get_fds(pwm
, pfds
, &nfds
);
203 for (i
= 0, n
= 0; i
< nfds
; i
++) {
204 FD_SET(pfds
[i
].fd
, &rfds
);
205 n
= pfds
[i
].fd
> n
? pfds
[i
].fd
: n
;
209 FD_SET(STDIN_FILENO
, &rfds
);
211 nfds
= select(n
+1, &rfds
, NULL
, NULL
, NULL
);
214 rc
= gpg_error_from_errno(errno
);
218 if (input
&& FD_ISSET(STDIN_FILENO
, &rfds
))
221 s
= pwmd_process(pwm
, &rc
, result
);
222 } while (s
== ASYNC_PROCESS
);
227 int main(int argc
, char *argv
[])
230 char *password
= NULL
;
231 char *filename
= NULL
;
232 char *socketpath
= NULL
;
233 char command
[ASSUAN_LINELENGTH
], *p
;
234 int ret
= EXIT_SUCCESS
;
238 char *pinentry_path
= NULL
;
239 char *display
= NULL
, *tty
= NULL
, *ttytype
= NULL
, *lcctype
= NULL
,
241 int outfd
= STDOUT_FILENO
;
242 FILE *outfp
= stdout
;
243 int inquirefd
= STDIN_FILENO
;
244 FILE *inquirefp
= stdin
;
246 char *clientname
= "pwmc";
247 char *inquire
= NULL
;
253 int port
= DEFAULT_PORT
;
254 char *username
= NULL
;
256 char *known_hosts
= NULL
;
258 int prot
= PWMD_IP_ANY
;
265 /* The order is important. */
271 OPT_HOST
, OPT_PORT
, OPT_IDENTITY
, OPT_KNOWN_HOSTS
, OPT_USER
,
272 OPT_GET_HOSTKEY
, OPT_IPV4
, OPT_IPV6
,
274 OPT_TTYNAME
, OPT_TTYTYPE
, OPT_DISPLAY
, OPT_LC_CTYPE
, OPT_LC_MESSAGES
,
275 OPT_TIMEOUT
, OPT_TRIES
, OPT_PINENTRY
,
276 OPT_PASSPHRASE
, OPT_SOCKET
, OPT_SAVE
, OPT_ITERATIONS
, OPT_OUTPUT_FD
,
277 OPT_INQUIRE_FD
, OPT_NO_STATUS
, OPT_NAME
, OPT_VERSION
, OPT_HELP
,
279 const struct option long_opts
[] = {
281 { "debug", 1, 0, 0 },
284 { "host", 1, 0, 'h' },
285 { "port", 1, 0, 'p' },
286 { "identity", 1, 0, 'i' },
287 { "known-hosts", 1, 0, 'k' },
288 { "user", 1, 0, 'u' },
289 { "get-hostkey", 0, 0, 'g' },
290 { "ipv4", 0, 0, '4' },
291 { "ipv6", 0, 0, '6' },
293 { "ttyname", 1, 0, 'y' },
294 { "ttytype", 1, 0, 't' },
295 { "display", 1, 0, 'd' },
296 { "lc-ctype", 1, 0, 0 },
297 { "lc-messages", 1, 0, 0 },
298 { "timeout", 1, 0, 0 },
299 { "tries", 1, 0, 0 },
300 { "pinentry", 1, 0, 0 },
301 { "passphrase", 1, 0, 'P' },
302 { "socket", 1, 0, 0 },
303 { "save", 0, 0, 'S' },
304 { "iterations", 1, 0, 'I' },
305 { "output-fd", 1, 0, 0 },
306 { "inquire-fd", 1, 0, 0 },
307 { "no-status", 0, 0, 0 },
308 { "name", 1, 0, 'n' },
309 { "version", 0, 0, 0 },
314 const char *optstring
= "46h:p:i:k:u:gy:t:d:P:I:Sn:";
316 const char *optstring
= "y:t:d:P:I:Sn:";
321 setlocale(LC_ALL
, "");
322 bindtextdomain("libpwmd", LOCALEDIR
);
325 while ((opt
= getopt_long(argc
, argv
, optstring
, long_opts
, &opt_index
)) != -1) {
327 /* Handle long options without a short option part. */
332 method
= atoi(optarg
);
339 lcctype
= pwmd_strdup(optarg
);
341 case OPT_LC_MESSAGES
:
342 lcmessages
= pwmd_strdup(optarg
);
345 timeout
= atoi(optarg
);
348 tries
= atoi(optarg
);
351 socketpath
= pwmd_strdup(optarg
);
354 inquirefd
= atoi(optarg
);
355 inquirefp
= fdopen(inquirefd
, "r");
359 err(EXIT_FAILURE
, "%i", inquirefd
);
363 outfd
= atoi(optarg
);
364 outfp
= fdopen(outfd
, "w");
368 err(EXIT_FAILURE
, "%i", outfd
);
376 printf("%s (pwmc)\n%s\n", PACKAGE_STRING
, PACKAGE_BUGREPORT
);
379 usage(argv
[0], EXIT_SUCCESS
);
381 usage(argv
[0], EXIT_FAILURE
);
393 host
= pwmd_strdup(optarg
);
399 ident
= pwmd_strdup(optarg
);
402 username
= pwmd_strdup(optarg
);
405 known_hosts
= pwmd_strdup(optarg
);
424 iter
= strtol(optarg
, NULL
, 10);
428 password
= pwmd_strdup(optarg
);
429 memset(optarg
, 0, strlen(optarg
));
436 usage(argv
[0], EXIT_FAILURE
);
441 if (host
&& !get
&& (!known_hosts
|| !ident
)) {
443 usage(argv
[0], EXIT_FAILURE
);
448 usage(argv
[0], EXIT_FAILURE
);
452 filename
= argv
[optind
];
454 pwm
= pwmd_new(clientname
);
461 if (prot
!= PWMD_IP_ANY
) {
462 error
= pwmd_setopt(pwm
, PWMD_OPTION_IP_VERSION
, prot
);
473 error
= pwmd_get_hostkey_async(pwm
, host
, port
);
476 errx(EXIT_FAILURE
, "%s: %s", host
, pwmd_strerror(error
));
478 error
= process_cmd(pwm
, &hostkey
, 0);
483 printf("%s\n", hostkey
);
490 error
= pwmd_ssh_connect_async(pwm
, host
, port
, ident
, username
,
496 error
= process_cmd(pwm
, NULL
, 0);
506 error
= pwmd_get_hostkey(pwm
, host
, port
, &hostkey
);
511 printf("%s\n", hostkey
);
518 error
= pwmd_ssh_connect(pwm
, host
, port
, ident
, username
, known_hosts
);
528 error
= pwmd_connect(pwm
, socketpath
);
537 error
= pwmd_command(pwm
, &result
, "VERSION");
539 if (error
&& error
!= GPG_ERR_ASS_UNKNOWN_CMD
)
544 if (error
== GPG_ERR_ASS_UNKNOWN_CMD
) {
548 usage(argv
[0], EXIT_FAILURE
);
552 /* pwmd version 2 or later. */
556 usage(argv
[0], EXIT_FAILURE
);
562 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TIMEOUT
, timeout
);
569 error
= pwmd_setopt(pwm
, PWMD_OPTION_PASSPHRASE
, password
);
576 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_PATH
, pinentry_path
);
583 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_DISPLAY
, display
);
590 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TTY
, tty
);
597 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TERM
, ttytype
);
604 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_LC_CTYPE
, lcctype
);
611 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_LC_MESSAGES
,
619 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TRIES
, tries
);
627 error
= pwmd_setopt(pwm
, PWMD_OPTION_STATUS_CB
, status_msg_cb
);
637 error
= pwmd_open(pwm
, filename
);
640 error
= pwmd_open2(pwm
, filename
);
643 error
= pwmd_open_async(pwm
, filename
);
647 error
= pwmd_open_async2(pwm
, filename
);
655 error
= process_cmd(pwm
, &result
, 0);
657 error
= pwmd_open(pwm
, filename
);
665 error
= pwmd_command(pwm
, &result
, "LOCK");
673 fcntl(STDIN_FILENO
, F_SETFL
, O_NONBLOCK
);
678 error
= process_cmd(pwm
, NULL
, 1);
683 n
= read(STDIN_FILENO
, command
, sizeof(command
));
689 error
= gpg_error_from_errno(errno
);
693 if (n
&& command
[strlen(command
)-1] == '\n')
694 command
[strlen(command
)-1] = 0;
702 p
= fgets(command
, sizeof(command
), stdin
);
704 p
= fgets(command
, sizeof(command
), stdin
);
711 * This is a known INQUIRE command. We use pwmd_inquire() to send the
712 * data from the do_inquire() callback function.
714 if (strncasecmp(p
, "STORE ", 6) == 0) {
716 inquire
= (char *)"STORE";
718 else if (strncasecmp(p
, "IMPORT ", 7) == 0) {
720 inquire
= (char *)"IMPORT";
724 struct inquire_s
*inq
= (struct inquire_s
*)pwmd_malloc(sizeof(struct inquire_s
));
727 error
= gpg_error_from_errno(ENOMEM
);
731 inq
->data
= pwmd_strdup(p
);
733 error
= pwmd_inquire(pwm
, inquire
, do_inquire
, inq
);
738 if (strcasecmp(p
, "BYE") == 0)
741 error
= pwmd_command(pwm
, &result
, command
);
742 memset(command
, 0, sizeof(command
));
748 fwrite(result
, 1, strlen(result
), outfp
);
753 memset(command
, 0, sizeof(command
));
756 if (!error
&& save
) {
758 error
= pwmd_command(pwm
, &result
, "OPTION ITERATIONS=%i", iter
);
767 error
= pwmd_save(pwm
);
770 error
= pwmd_save2(pwm
);
773 error
= pwmd_save_async(pwm
);
776 error
= pwmd_save_async2(pwm
);
780 if (!error
&& method
>= 2)
781 error
= process_cmd(pwm
, NULL
, 0);
784 error
= pwmd_save(pwm
);
788 if (!error
&& filename
)
789 error
= pwmd_command(pwm
, &result
, "UNLOCK");
799 pwmd_free(socketpath
);