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=pwmd async, 2=libpwmd)\n"
72 " number of pinentry tries before failing (3)\n"
74 " --host, -h <hostname>\n"
75 " connect to the specified hostname\n"
77 " alterate port (22)\n"
79 " SSH username (default is the invoking user)\n"
80 " --identity, -i <filename>\n"
81 " SSH identity file\n"
82 " --known-hosts, -k <filename>\n"
83 " known host's file (for server validation)\n"
84 " --get-hostkey, -g\n"
85 " retrieve the remote SSH host key and exit\n"
87 " try connecting via IPv4 only\n"
89 " try connecting via IPv6 only\n"
91 " --timeout <seconds>\n"
94 " disable showing of status messages from the server\n"
96 " set the client name\n"
97 " --socket <filename>\n"
98 " local socket to connect to (~/.pwmd/socket)\n"
99 " --passphrase, -P <string>\n"
100 " passphrase to use (disables pinentry use)\n"
101 " --pinentry <path>\n"
102 " the full path to the pinentry binary (server default)\n"
103 " --ttyname, -y <path>\n"
104 " tty that pinentry will use\n"
105 " --ttytype, -t <string>\n"
106 " pinentry terminal type (default is TERM)\n"
108 " pinentry display (default is DISPLAY)\n"
109 " --lc-ctype <string>\n"
110 " locale setting for pinentry\n"
111 " --lc-messages <string>\n"
112 " locale setting for pinentry\n"
113 " --output-fd <FD>\n"
114 " redirect command output to the specified file descriptor\n"
115 " --inquire-fd <FD>\n"
116 " read inquire data from the specified file descriptor\n"
118 " send the SAVE command before exiting\n"
119 " --iterations, -I <N>\n"
120 " encrypt with the specified number of iterations when saving\n"
131 static gpg_error_t
do_inquire(void *data
, const char *keyword
, gpg_error_t rc
,
132 char **result
, size_t *result_len
)
135 static char buf
[ASSUAN_LINELENGTH
];
138 struct inquire_s
*inq
= (struct inquire_s
*)data
;
141 memset(buf
, 0, sizeof(buf
));
149 snprintf(buf
, sizeof(buf
), "%s", inq
->data
);
150 pwmd_free(inq
->data
);
156 while ((c
= fgetc(inq
->fp
)) != EOF
) {
157 if (len
== sizeof(buf
)) {
167 memset(buf
, 0, sizeof(buf
));
176 static int status_msg_cb(void *data
, const char *line
)
178 fprintf(stderr
, "%s\n", line
);
182 int main(int argc
, char *argv
[])
185 char *password
= NULL
;
186 char *filename
= NULL
;
187 char *socketpath
= NULL
;
188 char command
[ASSUAN_LINELENGTH
], *p
;
189 int ret
= EXIT_SUCCESS
;
193 char *pinentry_path
= NULL
;
194 char *display
= NULL
, *tty
= NULL
, *ttytype
= NULL
, *lcctype
= NULL
,
196 int outfd
= STDOUT_FILENO
;
197 FILE *outfp
= stdout
;
198 int inquirefd
= STDIN_FILENO
;
199 FILE *inquirefp
= stdin
;
201 char *clientname
= NULL
;
202 char *inquire
= NULL
;
208 int port
= DEFAULT_PORT
;
209 char *username
= NULL
;
211 char *known_hosts
= NULL
;
213 int prot
= PWMD_IP_ANY
;
220 /* The order is important. */
226 OPT_HOST
, OPT_PORT
, OPT_IDENTITY
, OPT_KNOWN_HOSTS
, OPT_USER
,
227 OPT_GET_HOSTKEY
, OPT_IPV4
, OPT_IPV6
,
229 OPT_TTYNAME
, OPT_TTYTYPE
, OPT_DISPLAY
, OPT_LC_CTYPE
, OPT_LC_MESSAGES
,
230 OPT_TIMEOUT
, OPT_TRIES
, OPT_PINENTRY
,
231 OPT_PASSPHRASE
, OPT_SOCKET
, OPT_SAVE
, OPT_ITERATIONS
, OPT_OUTPUT_FD
,
232 OPT_INQUIRE_FD
, OPT_NO_STATUS
, OPT_NAME
, OPT_VERSION
, OPT_HELP
,
234 const struct option long_opts
[] = {
236 { "debug", 1, 0, 0 },
239 { "host", 1, 0, 'h' },
240 { "port", 1, 0, 'p' },
241 { "identity", 1, 0, 'i' },
242 { "known-hosts", 1, 0, 'k' },
243 { "user", 1, 0, 'u' },
244 { "get-hostkey", 0, 0, 'g' },
245 { "ipv4", 0, 0, '4' },
246 { "ipv6", 0, 0, '6' },
248 { "ttyname", 1, 0, 'y' },
249 { "ttytype", 1, 0, 't' },
250 { "display", 1, 0, 'd' },
251 { "lc-ctype", 1, 0, 0 },
252 { "lc-messages", 1, 0, 0 },
253 { "timeout", 1, 0, 0 },
254 { "tries", 1, 0, 0 },
255 { "pinentry", 1, 0, 0 },
256 { "passphrase", 1, 0, 'P' },
257 { "socket", 1, 0, 0 },
258 { "save", 0, 0, 'S' },
259 { "iterations", 1, 0, 'I' },
260 { "output-fd", 1, 0, 0 },
261 { "inquire-fd", 1, 0, 0 },
262 { "no-status", 0, 0, 0 },
263 { "name", 1, 0, 'n' },
264 { "version", 0, 0, 0 },
269 const char *optstring
= "46h:p:i:k:u:gy:t:d:P:I:Sn:";
271 const char *optstring
= "y:t:d:P:I:Sn:";
276 setlocale(LC_ALL
, "");
277 bindtextdomain("libpwmd", LOCALEDIR
);
280 while ((opt
= getopt_long(argc
, argv
, optstring
, &long_opts
, &opt_index
)) != -1) {
282 /* Handle long options without a short option part. */
287 method
= atoi(optarg
);
294 lcctype
= pwmd_strdup(optarg
);
296 case OPT_LC_MESSAGES
:
297 lcmessages
= pwmd_strdup(optarg
);
300 timeout
= atoi(optarg
);
303 tries
= atoi(optarg
);
306 socketpath
= pwmd_strdup(optarg
);
309 inquirefd
= atoi(optarg
);
310 inquirefp
= fdopen(inquirefd
, "r");
314 err(EXIT_FAILURE
, "%i", inquirefd
);
318 outfd
= atoi(optarg
);
319 outfp
= fdopen(outfd
, "w");
323 err(EXIT_FAILURE
, "%i", outfd
);
331 printf("%s (pwmc)\n%s\n", PACKAGE_STRING
, PACKAGE_BUGREPORT
);
334 usage(argv
[0], EXIT_SUCCESS
);
336 usage(argv
[0], EXIT_FAILURE
);
348 host
= pwmd_strdup(optarg
);
354 ident
= pwmd_strdup(optarg
);
357 username
= pwmd_strdup(optarg
);
360 known_hosts
= pwmd_strdup(optarg
);
379 iter
= strtol(optarg
, NULL
, 10);
383 password
= pwmd_strdup(optarg
);
384 memset(optarg
, 0, strlen(optarg
));
387 clientname
= pwmd_strdup(optarg
);
391 usage(argv
[0], EXIT_FAILURE
);
396 if (host
&& !get
&& (!known_hosts
|| !ident
)) {
398 usage(argv
[0], EXIT_FAILURE
);
403 usage(argv
[0], EXIT_FAILURE
);
407 filename
= argv
[optind
];
409 pwm
= pwmd_new("pwmc");
413 if (prot
!= PWMD_IP_ANY
) {
414 error
= pwmd_setopt(pwm
, PWMD_OPTION_IP_VERSION
, prot
);
425 error
= pwmd_get_hostkey_async(pwm
, host
, port
);
428 errx(EXIT_FAILURE
, "%s: %s", host
, pwmd_strerror(error
));
431 s
= pwmd_process(pwm
, &error
, &hostkey
);
434 } while (s
== ASYNC_PROCESS
);
439 printf("%s\n", hostkey
);
446 error
= pwmd_ssh_connect_async(pwm
, host
, port
, ident
, username
, known_hosts
);
452 s
= pwmd_process(pwm
, &error
, &result
);
455 } while (s
== ASYNC_PROCESS
);
465 error
= pwmd_get_hostkey(host
, port
, &hostkey
);
470 printf("%s\n", hostkey
);
476 error
= pwmd_ssh_connect(pwm
, host
, port
, ident
, username
, known_hosts
);
486 error
= pwmd_connect(pwm
, socketpath
);
495 error
= pwmd_command(pwm
, &result
, "VERSION");
497 if (error
&& error
!= GPG_ERR_ASS_UNKNOWN_CMD
)
502 if (error
== GPG_ERR_ASS_UNKNOWN_CMD
) {
506 usage(argv
[0], EXIT_FAILURE
);
510 /* pwmd version 2 or later. */
514 usage(argv
[0], EXIT_FAILURE
);
520 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TIMEOUT
, timeout
);
527 error
= pwmd_setopt(pwm
, PWMD_OPTION_PASSPHRASE
, password
);
534 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_PATH
, pinentry_path
);
541 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_DISPLAY
, display
);
548 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TTY
, tty
);
555 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TERM
, ttytype
);
562 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_LC_CTYPE
, lcctype
);
569 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_LC_MESSAGES
,
578 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY
, 1);
586 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TRIES
, tries
);
594 error
= pwmd_setopt(pwm
, PWMD_OPTION_STATUS_CB
, status_msg_cb
);
604 error
= pwmd_open_async(pwm
, filename
);
606 error
= pwmd_open_async2(pwm
, filename
);
610 s
= pwmd_process(pwm
, &error
, &result
);
613 } while (s
== ASYNC_PROCESS
);
617 error
= pwmd_open(pwm
, filename
);
619 error
= pwmd_open(pwm
, filename
);
627 error
= pwmd_command(pwm
, &result
, "LOCK");
636 struct timeval tv
= {0, 100000};
641 FD_SET(STDIN_FILENO
, &rfds
);
642 n
= select(STDIN_FILENO
+1, &rfds
, NULL
, NULL
, &tv
);
645 s
= pwmd_process(pwm
, &error
, &result
);
650 fprintf(stderr
, ".");
655 error
= gpg_error_from_errno(errno
);
659 fprintf(stderr
, "\n");
660 n
= read(STDIN_FILENO
, command
, sizeof(command
));
663 error
= gpg_error_from_errno(errno
);
667 if (n
&& command
[strlen(command
)-1] == '\n')
668 command
[strlen(command
)-1] = 0;
676 p
= fgets(command
, sizeof(command
), stdin
);
678 p
= fgets(command
, sizeof(command
), stdin
);
685 * This is a known INQUIRE command. We use pwmd_inquire() to send the
686 * data from the do_inquire() callback function.
688 if (strncasecmp(p
, "STORE ", 6) == 0) {
690 inquire
= (char *)"STORE";
692 else if (strncasecmp(p
, "IMPORT ", 7) == 0) {
694 inquire
= (char *)"IMPORT";
698 struct inquire_s
*inq
= (struct inquire_s
*)pwmd_malloc(sizeof(struct inquire_s
));
701 error
= gpg_error_from_errno(ENOMEM
);
705 inq
->data
= pwmd_strdup(p
);
707 error
= pwmd_inquire(pwm
, inquire
, do_inquire
, inq
);
712 if (strcasecmp(p
, "BYE") == 0)
715 error
= pwmd_command(pwm
, &result
, command
);
716 memset(command
, 0, sizeof(command
));
722 fwrite(result
, 1, strlen(result
), outfp
);
727 memset(command
, 0, sizeof(command
));
730 if (!error
&& save
) {
732 error
= pwmd_command(pwm
, &result
, "OPTION ITERATIONS=%i", iter
);
741 error
= pwmd_save_async(pwm
);
743 error
= pwmd_save_async2(pwm
);
747 s
= pwmd_process(pwm
, &error
, &result
);
750 } while (s
== ASYNC_PROCESS
);
754 error
= pwmd_save(pwm
);
756 error
= pwmd_save(pwm
);
760 if (!error
&& filename
)
761 error
= pwmd_command(pwm
, &result
, "UNLOCK");
771 pwmd_free(socketpath
);