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
);
200 for (i
= 0, n
= 0; i
< nfds
; i
++) {
201 FD_SET(pfds
[i
].fd
, &rfds
);
202 n
= pfds
[i
].fd
> n
? pfds
[i
].fd
: n
;
206 FD_SET(STDIN_FILENO
, &rfds
);
208 nfds
= select(n
+1, &rfds
, NULL
, NULL
, NULL
);
211 rc
= gpg_error_from_errno(errno
);
215 if (input
&& FD_ISSET(STDIN_FILENO
, &rfds
))
218 s
= pwmd_process(pwm
, &rc
, result
);
219 } while (s
== ASYNC_PROCESS
);
224 int main(int argc
, char *argv
[])
227 char *password
= NULL
;
228 char *filename
= NULL
;
229 char *socketpath
= NULL
;
230 char command
[ASSUAN_LINELENGTH
], *p
;
231 int ret
= EXIT_SUCCESS
;
235 char *pinentry_path
= NULL
;
236 char *display
= NULL
, *tty
= NULL
, *ttytype
= NULL
, *lcctype
= NULL
,
238 int outfd
= STDOUT_FILENO
;
239 FILE *outfp
= stdout
;
240 int inquirefd
= STDIN_FILENO
;
241 FILE *inquirefp
= stdin
;
243 char *clientname
= "pwmc";
244 char *inquire
= NULL
;
250 int port
= DEFAULT_PORT
;
251 char *username
= NULL
;
253 char *known_hosts
= NULL
;
255 int prot
= PWMD_IP_ANY
;
262 /* The order is important. */
268 OPT_HOST
, OPT_PORT
, OPT_IDENTITY
, OPT_KNOWN_HOSTS
, OPT_USER
,
269 OPT_GET_HOSTKEY
, OPT_IPV4
, OPT_IPV6
,
271 OPT_TTYNAME
, OPT_TTYTYPE
, OPT_DISPLAY
, OPT_LC_CTYPE
, OPT_LC_MESSAGES
,
272 OPT_TIMEOUT
, OPT_TRIES
, OPT_PINENTRY
,
273 OPT_PASSPHRASE
, OPT_SOCKET
, OPT_SAVE
, OPT_ITERATIONS
, OPT_OUTPUT_FD
,
274 OPT_INQUIRE_FD
, OPT_NO_STATUS
, OPT_NAME
, OPT_VERSION
, OPT_HELP
,
276 const struct option long_opts
[] = {
278 { "debug", 1, 0, 0 },
281 { "host", 1, 0, 'h' },
282 { "port", 1, 0, 'p' },
283 { "identity", 1, 0, 'i' },
284 { "known-hosts", 1, 0, 'k' },
285 { "user", 1, 0, 'u' },
286 { "get-hostkey", 0, 0, 'g' },
287 { "ipv4", 0, 0, '4' },
288 { "ipv6", 0, 0, '6' },
290 { "ttyname", 1, 0, 'y' },
291 { "ttytype", 1, 0, 't' },
292 { "display", 1, 0, 'd' },
293 { "lc-ctype", 1, 0, 0 },
294 { "lc-messages", 1, 0, 0 },
295 { "timeout", 1, 0, 0 },
296 { "tries", 1, 0, 0 },
297 { "pinentry", 1, 0, 0 },
298 { "passphrase", 1, 0, 'P' },
299 { "socket", 1, 0, 0 },
300 { "save", 0, 0, 'S' },
301 { "iterations", 1, 0, 'I' },
302 { "output-fd", 1, 0, 0 },
303 { "inquire-fd", 1, 0, 0 },
304 { "no-status", 0, 0, 0 },
305 { "name", 1, 0, 'n' },
306 { "version", 0, 0, 0 },
311 const char *optstring
= "46h:p:i:k:u:gy:t:d:P:I:Sn:";
313 const char *optstring
= "y:t:d:P:I:Sn:";
318 setlocale(LC_ALL
, "");
319 bindtextdomain("libpwmd", LOCALEDIR
);
322 while ((opt
= getopt_long(argc
, argv
, optstring
, long_opts
, &opt_index
)) != -1) {
324 /* Handle long options without a short option part. */
329 method
= atoi(optarg
);
336 lcctype
= pwmd_strdup(optarg
);
338 case OPT_LC_MESSAGES
:
339 lcmessages
= pwmd_strdup(optarg
);
342 timeout
= atoi(optarg
);
345 tries
= atoi(optarg
);
348 socketpath
= pwmd_strdup(optarg
);
351 inquirefd
= atoi(optarg
);
352 inquirefp
= fdopen(inquirefd
, "r");
356 err(EXIT_FAILURE
, "%i", inquirefd
);
360 outfd
= atoi(optarg
);
361 outfp
= fdopen(outfd
, "w");
365 err(EXIT_FAILURE
, "%i", outfd
);
373 printf("%s (pwmc)\n%s\n", PACKAGE_STRING
, PACKAGE_BUGREPORT
);
376 usage(argv
[0], EXIT_SUCCESS
);
378 usage(argv
[0], EXIT_FAILURE
);
390 host
= pwmd_strdup(optarg
);
396 ident
= pwmd_strdup(optarg
);
399 username
= pwmd_strdup(optarg
);
402 known_hosts
= pwmd_strdup(optarg
);
421 iter
= strtol(optarg
, NULL
, 10);
425 password
= pwmd_strdup(optarg
);
426 memset(optarg
, 0, strlen(optarg
));
433 usage(argv
[0], EXIT_FAILURE
);
438 if (host
&& !get
&& (!known_hosts
|| !ident
)) {
440 usage(argv
[0], EXIT_FAILURE
);
445 usage(argv
[0], EXIT_FAILURE
);
449 filename
= argv
[optind
];
451 pwm
= pwmd_new(clientname
);
458 if (prot
!= PWMD_IP_ANY
) {
459 error
= pwmd_setopt(pwm
, PWMD_OPTION_IP_VERSION
, prot
);
470 error
= pwmd_get_hostkey_async(pwm
, host
, port
);
473 errx(EXIT_FAILURE
, "%s: %s", host
, pwmd_strerror(error
));
475 error
= process_cmd(pwm
, &hostkey
, 0);
480 printf("%s\n", hostkey
);
487 error
= pwmd_ssh_connect_async(pwm
, host
, port
, ident
, username
,
493 error
= process_cmd(pwm
, NULL
, 0);
503 error
= pwmd_get_hostkey(pwm
, host
, port
, &hostkey
);
508 printf("%s\n", hostkey
);
515 error
= pwmd_ssh_connect(pwm
, host
, port
, ident
, username
, known_hosts
);
525 error
= pwmd_connect(pwm
, socketpath
);
534 error
= pwmd_command(pwm
, &result
, "VERSION");
536 if (error
&& error
!= GPG_ERR_ASS_UNKNOWN_CMD
)
541 if (error
== GPG_ERR_ASS_UNKNOWN_CMD
) {
545 usage(argv
[0], EXIT_FAILURE
);
549 /* pwmd version 2 or later. */
553 usage(argv
[0], EXIT_FAILURE
);
559 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TIMEOUT
, timeout
);
566 error
= pwmd_setopt(pwm
, PWMD_OPTION_PASSPHRASE
, password
);
573 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_PATH
, pinentry_path
);
580 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_DISPLAY
, display
);
587 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TTY
, tty
);
594 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TERM
, ttytype
);
601 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_LC_CTYPE
, lcctype
);
608 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_LC_MESSAGES
,
616 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TRIES
, tries
);
624 error
= pwmd_setopt(pwm
, PWMD_OPTION_STATUS_CB
, status_msg_cb
);
634 error
= pwmd_open(pwm
, filename
);
637 error
= pwmd_open2(pwm
, filename
);
640 error
= pwmd_open_async(pwm
, filename
);
644 error
= pwmd_open_async2(pwm
, filename
);
652 error
= process_cmd(pwm
, &result
, 0);
654 error
= pwmd_open(pwm
, filename
);
662 error
= pwmd_command(pwm
, &result
, "LOCK");
673 error
= process_cmd(pwm
, NULL
, 1);
678 n
= read(STDIN_FILENO
, command
, sizeof(command
));
681 error
= gpg_error_from_errno(errno
);
685 if (n
&& command
[strlen(command
)-1] == '\n')
686 command
[strlen(command
)-1] = 0;
694 p
= fgets(command
, sizeof(command
), stdin
);
696 p
= fgets(command
, sizeof(command
), stdin
);
703 * This is a known INQUIRE command. We use pwmd_inquire() to send the
704 * data from the do_inquire() callback function.
706 if (strncasecmp(p
, "STORE ", 6) == 0) {
708 inquire
= (char *)"STORE";
710 else if (strncasecmp(p
, "IMPORT ", 7) == 0) {
712 inquire
= (char *)"IMPORT";
716 struct inquire_s
*inq
= (struct inquire_s
*)pwmd_malloc(sizeof(struct inquire_s
));
719 error
= gpg_error_from_errno(ENOMEM
);
723 inq
->data
= pwmd_strdup(p
);
725 error
= pwmd_inquire(pwm
, inquire
, do_inquire
, inq
);
730 if (strcasecmp(p
, "BYE") == 0)
733 error
= pwmd_command(pwm
, &result
, command
);
734 memset(command
, 0, sizeof(command
));
740 fwrite(result
, 1, strlen(result
), outfp
);
745 memset(command
, 0, sizeof(command
));
748 if (!error
&& save
) {
750 error
= pwmd_command(pwm
, &result
, "OPTION ITERATIONS=%i", iter
);
759 error
= pwmd_save(pwm
);
762 error
= pwmd_save2(pwm
);
765 error
= pwmd_save_async(pwm
);
768 error
= pwmd_save_async2(pwm
);
772 if (!error
&& method
>= 2)
773 error
= process_cmd(pwm
, NULL
, 0);
776 error
= pwmd_save(pwm
);
780 if (!error
&& filename
)
781 error
= pwmd_command(pwm
, &result
, "UNLOCK");
791 pwmd_free(socketpath
);