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, "
74 " number of pinentry tries before failing (3)\n"
77 " --host, -h <hostname>\n"
78 " connect to the specified hostname\n"
81 " alterate port (22)\n"
84 " SSH username (default is the invoking user)\n"
86 " --identity, -i <filename>\n"
87 " SSH identity file\n"
89 " --known-hosts, -k <filename>\n"
90 " known host's file (for server validation)\n"
92 " --get-hostkey, -g\n"
93 " retrieve the remote SSH host key and exit\n"
96 " try connecting via IPv4 only\n"
99 " try connecting via IPv6 only\n"
103 " a url string to parse\n"
105 " --timeout <seconds>\n"
106 " pinentry timeout\n"
109 " disable showing of status messages from the server\n"
111 " --name, -n <string>\n"
112 " set the client name\n"
114 " --socket <filename>\n"
115 " local socket to connect to (~/.pwmd/socket)\n"
117 " --passphrase, -P <string>\n"
118 " passphrase to use (disables pinentry use)\n"
120 " --pinentry <path>\n"
121 " the full path to the pinentry binary (server default)\n"
123 " --ttyname, -y <path>\n"
124 " tty that pinentry will use\n"
126 " --ttytype, -t <string>\n"
127 " pinentry terminal type (default is TERM)\n"
130 " pinentry display (default is DISPLAY)\n"
132 " --lc-ctype <string>\n"
133 " locale setting for pinentry\n"
135 " --lc-messages <string>\n"
136 " locale setting for pinentry\n"
138 " --output-fd <FD>\n"
139 " redirect command output to the specified file descriptor\n"
141 " --inquire-fd <FD>\n"
142 " read inquire data from the specified file descriptor\n"
145 " send the SAVE command before exiting\n"
147 " --iterations, -I <N>\n"
148 " encrypt with the specified number of iterations when saving\n"
152 fprintf(status
== EXIT_FAILURE
? stderr
: stdout
, N_(
154 "A url string (specified with --url) may be in the form of:\n"
155 " socket://[path/to/socket]\n"
157 " ssh[46]://[username@]hostname[:port],identity,known_hosts\n"
168 static gpg_error_t
do_inquire(void *data
, const char *keyword
, gpg_error_t rc
,
169 char **result
, size_t *result_len
)
172 static char buf
[ASSUAN_LINELENGTH
];
175 struct inquire_s
*inq
= (struct inquire_s
*)data
;
178 memset(buf
, 0, sizeof(buf
));
186 snprintf(buf
, sizeof(buf
), "%s", inq
->data
);
187 pwmd_free(inq
->data
);
193 while ((c
= fgetc(inq
->fp
)) != EOF
) {
194 if (len
== sizeof(buf
)) {
204 memset(buf
, 0, sizeof(buf
));
213 static int status_msg_cb(void *data
, const char *line
)
215 fprintf(stderr
, "%s\n", line
);
219 static gpg_error_t
process_cmd(pwm_t
*pwm
, char **result
, int input
)
228 pwmd_fd_t pfds
[nfds
];
231 rc
= pwmd_get_fds(pwm
, pfds
, &nfds
);
239 for (i
= 0, n
= 0; i
< nfds
; i
++) {
240 FD_SET(pfds
[i
].fd
, &rfds
);
241 n
= pfds
[i
].fd
> n
? pfds
[i
].fd
: n
;
245 FD_SET(STDIN_FILENO
, &rfds
);
247 nfds
= select(n
+1, &rfds
, NULL
, NULL
, NULL
);
250 rc
= gpg_error_from_errno(errno
);
254 if (input
&& FD_ISSET(STDIN_FILENO
, &rfds
))
257 s
= pwmd_process(pwm
, &rc
, result
);
258 } while (s
== ASYNC_PROCESS
);
263 int main(int argc
, char *argv
[])
266 char *password
= NULL
;
267 char *filename
= NULL
;
268 char *socketpath
= NULL
;
269 char command
[ASSUAN_LINELENGTH
], *p
;
270 int ret
= EXIT_SUCCESS
;
274 char *pinentry_path
= NULL
;
275 char *display
= NULL
, *tty
= NULL
, *ttytype
= NULL
, *lcctype
= NULL
,
277 int outfd
= STDOUT_FILENO
;
278 FILE *outfp
= stdout
;
279 int inquirefd
= STDIN_FILENO
;
280 FILE *inquirefp
= stdin
;
282 char *clientname
= "pwmc";
283 char *inquire
= NULL
;
289 int port
= DEFAULT_PORT
;
290 char *username
= NULL
;
292 char *known_hosts
= NULL
;
294 int prot
= PWMD_IP_ANY
;
301 char *url_string
= NULL
;
302 /* The order is important. */
308 OPT_HOST
, OPT_PORT
, OPT_IDENTITY
, OPT_KNOWN_HOSTS
, OPT_USER
,
309 OPT_GET_HOSTKEY
, OPT_IPV4
, OPT_IPV6
,
311 OPT_URL
, OPT_TTYNAME
, OPT_TTYTYPE
, OPT_DISPLAY
, OPT_LC_CTYPE
,
312 OPT_LC_MESSAGES
, OPT_TIMEOUT
, OPT_TRIES
, OPT_PINENTRY
, OPT_PASSPHRASE
,
313 OPT_SOCKET
, OPT_SAVE
, OPT_ITERATIONS
, OPT_OUTPUT_FD
, OPT_INQUIRE_FD
,
314 OPT_NO_STATUS
, OPT_NAME
, OPT_VERSION
, OPT_HELP
,
316 const struct option long_opts
[] = {
318 { "debug", 1, 0, 0 },
321 { "host", 1, 0, 'h' },
322 { "port", 1, 0, 'p' },
323 { "identity", 1, 0, 'i' },
324 { "known-hosts", 1, 0, 'k' },
325 { "user", 1, 0, 'u' },
326 { "get-hostkey", 0, 0, 'g' },
327 { "ipv4", 0, 0, '4' },
328 { "ipv6", 0, 0, '6' },
331 { "ttyname", 1, 0, 'y' },
332 { "ttytype", 1, 0, 't' },
333 { "display", 1, 0, 'd' },
334 { "lc-ctype", 1, 0, 0 },
335 { "lc-messages", 1, 0, 0 },
336 { "timeout", 1, 0, 0 },
337 { "tries", 1, 0, 0 },
338 { "pinentry", 1, 0, 0 },
339 { "passphrase", 1, 0, 'P' },
340 { "socket", 1, 0, 0 },
341 { "save", 0, 0, 'S' },
342 { "iterations", 1, 0, 'I' },
343 { "output-fd", 1, 0, 0 },
344 { "inquire-fd", 1, 0, 0 },
345 { "no-status", 0, 0, 0 },
346 { "name", 1, 0, 'n' },
347 { "version", 0, 0, 0 },
352 const char *optstring
= "46h:p:i:k:u:gy:t:d:P:I:Sn:";
354 const char *optstring
= "y:t:d:P:I:Sn:";
359 setlocale(LC_ALL
, "");
360 bindtextdomain("libpwmd", LOCALEDIR
);
363 while ((opt
= getopt_long(argc
, argv
, optstring
, long_opts
, &opt_index
)) != -1) {
365 /* Handle long options without a short option part. */
370 method
= atoi(optarg
);
380 lcctype
= pwmd_strdup(optarg
);
382 case OPT_LC_MESSAGES
:
383 lcmessages
= pwmd_strdup(optarg
);
386 timeout
= atoi(optarg
);
389 tries
= atoi(optarg
);
392 socketpath
= pwmd_strdup(optarg
);
395 inquirefd
= atoi(optarg
);
396 inquirefp
= fdopen(inquirefd
, "r");
400 err(EXIT_FAILURE
, "%i", inquirefd
);
404 outfd
= atoi(optarg
);
405 outfp
= fdopen(outfd
, "w");
409 err(EXIT_FAILURE
, "%i", outfd
);
417 printf("%s (pwmc)\n%s\n", PACKAGE_STRING
, PACKAGE_BUGREPORT
);
420 pinentry_path
= optarg
;
423 usage(argv
[0], EXIT_SUCCESS
);
425 usage(argv
[0], EXIT_FAILURE
);
437 host
= pwmd_strdup(optarg
);
443 ident
= pwmd_strdup(optarg
);
446 username
= pwmd_strdup(optarg
);
449 known_hosts
= pwmd_strdup(optarg
);
468 iter
= strtol(optarg
, NULL
, 10);
472 password
= pwmd_strdup(optarg
);
473 memset(optarg
, 0, strlen(optarg
));
480 usage(argv
[0], EXIT_FAILURE
);
488 if (host
&& !get
&& (!known_hosts
|| !ident
)) {
490 usage(argv
[0], EXIT_FAILURE
);
495 usage(argv
[0], EXIT_FAILURE
);
502 filename
= argv
[optind
];
504 pwm
= pwmd_new(clientname
);
511 if (prot
!= PWMD_IP_ANY
) {
512 error
= pwmd_setopt(pwm
, PWMD_OPTION_IP_VERSION
, prot
);
523 error
= pwmd_get_hostkey_async(pwm
, host
, port
);
526 errx(EXIT_FAILURE
, "%s: %s", host
, pwmd_strerror(error
));
528 error
= process_cmd(pwm
, &hostkey
, 0);
533 printf("%s\n", hostkey
);
541 error
= pwmd_connect_url_async(pwm
, url_string
);
543 error
= pwmd_ssh_connect_async(pwm
, host
, port
, ident
, username
,
549 error
= process_cmd(pwm
, NULL
, 0);
559 error
= pwmd_get_hostkey(pwm
, host
, port
, &hostkey
);
564 printf("%s\n", hostkey
);
572 error
= pwmd_connect_url(pwm
, url_string
);
574 error
= pwmd_ssh_connect(pwm
, host
, port
, ident
, username
, known_hosts
);
585 error
= pwmd_connect_url(pwm
, url_string
);
587 error
= pwmd_connect(pwm
, socketpath
);
596 error
= pwmd_command(pwm
, &result
, "VERSION");
598 if (error
&& error
!= GPG_ERR_ASS_UNKNOWN_CMD
)
603 if (error
== GPG_ERR_ASS_UNKNOWN_CMD
) {
607 usage(argv
[0], EXIT_FAILURE
);
611 /* pwmd version 2 or later. */
615 usage(argv
[0], EXIT_FAILURE
);
621 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TIMEOUT
, timeout
);
628 error
= pwmd_setopt(pwm
, PWMD_OPTION_PASSPHRASE
, password
);
635 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_PATH
, pinentry_path
);
642 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_DISPLAY
, display
);
649 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TTY
, tty
);
656 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TERM
, ttytype
);
663 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_LC_CTYPE
, lcctype
);
670 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_LC_MESSAGES
,
678 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TRIES
, tries
);
686 error
= pwmd_setopt(pwm
, PWMD_OPTION_STATUS_CB
, status_msg_cb
);
696 error
= pwmd_open(pwm
, filename
);
699 error
= pwmd_open2(pwm
, filename
);
702 error
= pwmd_open_async(pwm
, filename
);
706 error
= pwmd_open_async2(pwm
, filename
);
714 error
= process_cmd(pwm
, &result
, 0);
716 error
= pwmd_open(pwm
, filename
);
724 error
= pwmd_command(pwm
, &result
, "LOCK");
730 fcntl(STDIN_FILENO
, F_SETFL
, O_NONBLOCK
);
735 error
= process_cmd(pwm
, NULL
, 1);
740 n
= read(STDIN_FILENO
, command
, sizeof(command
));
746 error
= gpg_error_from_errno(errno
);
754 if (n
&& command
[strlen(command
)-1] == '\n')
755 command
[strlen(command
)-1] = 0;
765 * This is a known INQUIRE command. We use pwmd_inquire() to send the
766 * data from the do_inquire() callback function.
768 if (strncasecmp(p
, "STORE ", 6) == 0) {
770 inquire
= (char *)"STORE";
772 else if (strncasecmp(p
, "IMPORT ", 7) == 0) {
774 inquire
= (char *)"IMPORT";
778 struct inquire_s
*inq
= (struct inquire_s
*)pwmd_malloc(sizeof(struct inquire_s
));
781 error
= gpg_error_from_errno(ENOMEM
);
785 inq
->data
= pwmd_strdup(p
);
787 error
= pwmd_inquire(pwm
, inquire
, do_inquire
, inq
);
792 if (strcasecmp(p
, "BYE") == 0)
795 error
= pwmd_command(pwm
, &result
, command
);
796 memset(command
, 0, sizeof(command
));
802 fwrite(result
, 1, strlen(result
), outfp
);
807 memset(command
, 0, sizeof(command
));
810 if (!error
&& save
) {
812 error
= pwmd_command(pwm
, &result
, "OPTION ITERATIONS=%i", iter
);
821 error
= pwmd_save(pwm
);
824 error
= pwmd_save2(pwm
);
827 error
= pwmd_save_async(pwm
);
830 error
= pwmd_save_async2(pwm
);
834 if (!error
&& method
>= 2)
835 error
= process_cmd(pwm
, NULL
, 0);
838 error
= pwmd_save(pwm
);
842 if (!error
&& filename
)
843 error
= pwmd_command(pwm
, &result
, "UNLOCK");
853 pwmd_free(socketpath
);