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>
40 #ifdef HAVE_GETOPT_LONG
45 #include "getopt_long.h"
49 #define N_(msgid) gettext(msgid)
53 #define DEFAULT_PORT 22
56 static void show_error(gpg_error_t error
)
58 fprintf(stderr
, "ERR %i %s\n", gpg_err_code(error
), pwmd_strerror(error
));
61 static void usage(const char *pn
, int status
)
63 fprintf(status
== EXIT_FAILURE
? stderr
: stdout
, N_(
64 "Read a PWMD protocol command from standard input.\n\n"
65 "Usage: pwmc [options] [file]\n"
68 " pinentry method (0=pwmd, 1=libpwmd, 2=pwmd async, "
73 " --host, -h <hostname>\n"
74 " connect to the specified hostname\n"
77 " alterate port (22)\n"
80 " SSH username (default is the invoking user)\n"
82 " --identity, -i <filename>\n"
83 " SSH identity file\n"
85 " --known-hosts, -k <filename>\n"
86 " known host's file (for server validation)\n"
88 " --get-hostkey, -g\n"
89 " retrieve the remote SSH host key and exit\n"
92 " try connecting via IPv4 only\n"
95 " try connecting via IPv6 only\n"
99 " a url string to parse\n"
102 " disable showing of status messages from the server\n"
104 " --name, -n <string>\n"
105 " set the client name\n"
107 " --socket <filename>\n"
108 " local socket to connect to (~/.pwmd/socket)\n"
110 " --passphrase, -P <string>\n"
111 " passphrase to use (disables pinentry use)\n"
113 " --timeout <seconds>\n"
114 " pinentry timeout\n"
117 " number of pinentry tries before failing (3)\n"
119 " --pinentry <path>\n"
120 " the full path to the pinentry binary (server default)\n"
122 " --ttyname, -y <path>\n"
123 " tty that pinentry will use\n"
125 " --ttytype, -t <string>\n"
126 " pinentry terminal type (default is TERM)\n"
129 " pinentry display (default is DISPLAY)\n"
131 " --lc-ctype <string>\n"
132 " locale setting for pinentry\n"
134 " --lc-messages <string>\n"
135 " locale setting for pinentry\n"
138 " --local-pinentry\n"
139 " force using a local pinentry\n"
141 " --output-fd <FD>\n"
142 " redirect command output to the specified file descriptor\n"
144 " --inquire-fd <FD>\n"
145 " read inquire data from the specified file descriptor\n"
148 " send the SAVE command before exiting\n"
151 " like --save, but ask for a passphrase\n"
153 " --iterations, -I <N>\n"
154 " encrypt with the specified number of iterations when saving\n"
158 fprintf(status
== EXIT_FAILURE
? stderr
: stdout
, N_(
160 "A url string (specified with --url) may be in the form of:\n"
161 " local://[path/to/socket]\n"
163 " ssh[46]://[username@]hostname[:port],identity,known_hosts\n"
174 static gpg_error_t
do_inquire(void *data
, const char *keyword
, gpg_error_t rc
,
175 char **result
, size_t *result_len
)
178 static char buf
[ASSUAN_LINELENGTH
];
181 struct inquire_s
*inq
= (struct inquire_s
*)data
;
184 memset(buf
, 0, sizeof(buf
));
192 snprintf(buf
, sizeof(buf
), "%s", inq
->data
);
193 pwmd_free(inq
->data
);
199 while ((c
= fgetc(inq
->fp
)) != EOF
) {
200 if (len
== sizeof(buf
)) {
210 memset(buf
, 0, sizeof(buf
));
219 static int status_msg_cb(void *data
, const char *line
)
221 fprintf(stderr
, "%s\n", line
);
225 static gpg_error_t
process_cmd(pwm_t
*pwm
, char **result
, int input
)
234 pwmd_fd_t pfds
[nfds
];
237 rc
= pwmd_get_fds(pwm
, pfds
, &nfds
);
245 for (i
= 0, n
= 0; i
< nfds
; i
++) {
246 FD_SET(pfds
[i
].fd
, &rfds
);
247 n
= pfds
[i
].fd
> n
? pfds
[i
].fd
: n
;
251 FD_SET(STDIN_FILENO
, &rfds
);
253 nfds
= select(n
+1, &rfds
, NULL
, NULL
, NULL
);
256 rc
= gpg_error_from_errno(errno
);
260 if (input
&& FD_ISSET(STDIN_FILENO
, &rfds
))
263 s
= pwmd_process(pwm
, &rc
, result
);
264 } while (s
== ASYNC_PROCESS
);
269 int main(int argc
, char *argv
[])
272 char *password
= NULL
;
273 char *filename
= NULL
;
274 char *socketpath
= NULL
;
275 char command
[ASSUAN_LINELENGTH
], *p
;
276 int ret
= EXIT_SUCCESS
;
280 char *pinentry_path
= NULL
;
281 char *display
= NULL
, *tty
= NULL
, *ttytype
= NULL
, *lcctype
= NULL
,
283 int outfd
= STDOUT_FILENO
;
284 FILE *outfp
= stdout
;
285 int inquirefd
= STDIN_FILENO
;
286 FILE *inquirefp
= stdin
;
288 char *clientname
= "pwmc";
289 char *inquire
= NULL
;
297 int port
= DEFAULT_PORT
;
298 char *username
= NULL
;
300 char *known_hosts
= NULL
;
302 int prot
= PWMD_IP_ANY
;
312 char *url_string
= NULL
;
313 /* The order is important. */
319 OPT_HOST
, OPT_PORT
, OPT_IDENTITY
, OPT_KNOWN_HOSTS
, OPT_USER
,
320 OPT_GET_HOSTKEY
, OPT_IPV4
, OPT_IPV6
,
322 OPT_URL
, OPT_LOCAL
, OPT_FORCE_SAVE
, OPT_TTYNAME
, OPT_TTYTYPE
,
323 OPT_DISPLAY
, OPT_LC_CTYPE
, OPT_LC_MESSAGES
, OPT_TIMEOUT
, OPT_TRIES
,
324 OPT_PINENTRY
, OPT_PASSPHRASE
, OPT_SOCKET
, OPT_SAVE
, OPT_ITERATIONS
,
325 OPT_OUTPUT_FD
, OPT_INQUIRE_FD
, OPT_NO_STATUS
, OPT_NAME
, OPT_VERSION
,
328 const struct option long_opts
[] = {
330 { "debug", 1, 0, 0 },
333 { "host", 1, 0, 'h' },
334 { "port", 1, 0, 'p' },
335 { "identity", 1, 0, 'i' },
336 { "known-hosts", 1, 0, 'k' },
337 { "user", 1, 0, 'u' },
338 { "get-hostkey", 0, 0, 'g' },
339 { "ipv4", 0, 0, '4' },
340 { "ipv6", 0, 0, '6' },
343 { "local-pinentry", 0, 0 },
344 { "force-save", 0, 0 },
345 { "ttyname", 1, 0, 'y' },
346 { "ttytype", 1, 0, 't' },
347 { "display", 1, 0, 'd' },
348 { "lc-ctype", 1, 0, 0 },
349 { "lc-messages", 1, 0, 0 },
350 { "timeout", 1, 0, 0 },
351 { "tries", 1, 0, 0 },
352 { "pinentry", 1, 0, 0 },
353 { "passphrase", 1, 0, 'P' },
354 { "socket", 1, 0, 0 },
355 { "save", 0, 0, 'S' },
356 { "iterations", 1, 0, 'I' },
357 { "output-fd", 1, 0, 0 },
358 { "inquire-fd", 1, 0, 0 },
359 { "no-status", 0, 0, 0 },
360 { "name", 1, 0, 'n' },
361 { "version", 0, 0, 0 },
366 const char *optstring
= "46h:p:i:k:u:gy:t:d:P:I:Sn:";
368 const char *optstring
= "y:t:d:P:I:Sn:";
373 setlocale(LC_ALL
, "");
374 bindtextdomain("libpwmd", LOCALEDIR
);
377 while ((opt
= getopt_long(argc
, argv
, optstring
, long_opts
, &opt_index
)) != -1) {
379 /* Handle long options without a short option part. */
384 method
= atoi(optarg
);
397 save
= force_save
= 1;
400 lcctype
= pwmd_strdup(optarg
);
402 case OPT_LC_MESSAGES
:
403 lcmessages
= pwmd_strdup(optarg
);
406 timeout
= atoi(optarg
);
409 tries
= atoi(optarg
);
413 socketpath
= pwmd_strdup(optarg
);
416 inquirefd
= atoi(optarg
);
417 inquirefp
= fdopen(inquirefd
, "r");
421 err(EXIT_FAILURE
, "%i", inquirefd
);
425 outfd
= atoi(optarg
);
426 outfp
= fdopen(outfd
, "w");
430 err(EXIT_FAILURE
, "%i", outfd
);
438 printf("%s (pwmc)\n%s\n", PACKAGE_STRING
, PACKAGE_BUGREPORT
);
441 pinentry_path
= optarg
;
444 usage(argv
[0], EXIT_SUCCESS
);
446 usage(argv
[0], EXIT_FAILURE
);
458 host
= pwmd_strdup(optarg
);
464 ident
= pwmd_strdup(optarg
);
467 username
= pwmd_strdup(optarg
);
470 known_hosts
= pwmd_strdup(optarg
);
489 iter
= strtol(optarg
, NULL
, 10);
493 password
= pwmd_strdup(optarg
);
494 memset(optarg
, 0, strlen(optarg
));
501 usage(argv
[0], EXIT_FAILURE
);
509 if (host
&& !get
&& (!known_hosts
|| !ident
)) {
511 usage(argv
[0], EXIT_FAILURE
);
516 usage(argv
[0], EXIT_FAILURE
);
523 filename
= argv
[optind
];
525 pwm
= pwmd_new(clientname
);
532 if (prot
!= PWMD_IP_ANY
) {
533 error
= pwmd_setopt(pwm
, PWMD_OPTION_IP_VERSION
, prot
);
544 error
= pwmd_get_hostkey_async(pwm
, host
, port
);
547 errx(EXIT_FAILURE
, "%s: %s", host
, pwmd_strerror(error
));
549 error
= process_cmd(pwm
, &hostkey
, 0);
554 printf("%s\n", hostkey
);
562 error
= pwmd_connect_url_async(pwm
, url_string
);
564 error
= pwmd_ssh_connect_async(pwm
, host
, port
, ident
, username
,
570 error
= process_cmd(pwm
, NULL
, 0);
580 error
= pwmd_get_hostkey(pwm
, host
, port
, &hostkey
);
585 printf("%s\n", hostkey
);
593 error
= pwmd_connect_url(pwm
, url_string
);
595 error
= pwmd_ssh_connect(pwm
, host
, port
, ident
, username
, known_hosts
);
606 error
= pwmd_connect_url(pwm
, url_string
);
608 error
= pwmd_connect(pwm
, socketpath
);
616 error
= pwmd_socket_type(pwm
, &s
);
621 if (s
== PWMD_SOCKET_SSH
&& force_save
&& !local_pin
) {
622 error
= GPG_ERR_WRONG_KEY_USAGE
;
627 error
= pwmd_command(pwm
, &result
, "VERSION");
637 usage(argv
[0], EXIT_FAILURE
);
642 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TIMEOUT
, timeout
);
648 if (local_pin
&& filename
) {
649 error
= pwmd_command(pwm
, NULL
, "ISCACHED %s", filename
);
651 if (error
== GPG_ERR_NOT_FOUND
) {
653 if (try++ == local_tries
)
662 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TIMEOUT
, 0);
664 error
= pwmd_getpin(pwm
, filename
, &password
,
665 try > 1 ? PWMD_PINENTRY_OPEN_FAILED
: PWMD_PINENTRY_OPEN
);
670 error
= pwmd_setopt(pwm
, PWMD_OPTION_PASSPHRASE
, password
);
678 else if (error
&& error
!= GPG_ERR_ENOENT
)
683 error
= pwmd_setopt(pwm
, PWMD_OPTION_PASSPHRASE
, password
);
690 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_PATH
, pinentry_path
);
697 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_DISPLAY
, display
);
704 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TTY
, tty
);
711 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TERM
, ttytype
);
718 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_LC_CTYPE
, lcctype
);
725 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_LC_MESSAGES
,
733 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TRIES
, tries
);
741 error
= pwmd_setopt(pwm
, PWMD_OPTION_STATUS_CB
, status_msg_cb
);
752 error
= pwmd_open(pwm
, filename
);
755 error
= pwmd_open2(pwm
, filename
);
758 error
= pwmd_open_async(pwm
, filename
);
762 error
= pwmd_open_async2(pwm
, filename
);
766 if (error
&& local_pin
&& error
== GPG_ERR_INV_PASSPHRASE
)
773 error
= process_cmd(pwm
, &result
, 0);
775 error
= pwmd_open(pwm
, filename
);
777 if (error
&& local_pin
&& error
== GPG_ERR_INV_PASSPHRASE
)
786 error
= pwmd_command(pwm
, &result
, "LOCK");
792 fcntl(STDIN_FILENO
, F_SETFL
, O_NONBLOCK
);
797 error
= process_cmd(pwm
, NULL
, 1);
802 n
= read(STDIN_FILENO
, command
, sizeof(command
));
808 error
= gpg_error_from_errno(errno
);
816 if (n
&& command
[strlen(command
)-1] == '\n')
817 command
[strlen(command
)-1] = 0;
827 * This is a known INQUIRE command. We use pwmd_inquire() to send the
828 * data from the do_inquire() callback function.
830 if (strncasecmp(p
, "STORE ", 6) == 0) {
832 inquire
= (char *)"STORE";
834 else if (strncasecmp(p
, "IMPORT ", 7) == 0) {
836 inquire
= (char *)"IMPORT";
840 struct inquire_s
*inq
= (struct inquire_s
*)pwmd_malloc(sizeof(struct inquire_s
));
843 error
= gpg_error_from_errno(ENOMEM
);
847 inq
->data
= pwmd_strdup(p
);
849 error
= pwmd_inquire(pwm
, inquire
, do_inquire
, inq
);
854 if (strcasecmp(p
, "BYE") == 0)
857 error
= pwmd_command(pwm
, &result
, command
);
858 memset(command
, 0, sizeof(command
));
864 fwrite(result
, 1, strlen(result
), outfp
);
869 memset(command
, 0, sizeof(command
));
873 if (!error
&& save
&& filename
) {
875 error
= pwmd_command(pwm
, &result
, "SET ITERATIONS=%i", iter
);
885 error
= pwmd_command(pwm
, NULL
, "ISCACHED %s", filename
);
887 if (error
&& error
!= GPG_ERR_NOT_FOUND
&&
888 error
!= GPG_ERR_ENOENT
)
894 error
= pwmd_getpin(pwm
, filename
, &p1
, PWMD_PINENTRY_SAVE
);
899 error
= pwmd_getpin(pwm
, filename
, &password
,
900 PWMD_PINENTRY_SAVE_CONFIRM
);
907 if ((p1
|| password
) && ((!p1
&& password
) || (!password
&& p1
) ||
908 strcmp(p1
, password
))) {
918 error
= pwmd_setopt(pwm
, PWMD_OPTION_PASSPHRASE
, password
);
928 error
= pwmd_command(pwm
, NULL
, "CLEARCACHE %s", filename
);
934 error
= pwmd_setopt(pwm
, PWMD_OPTION_PASSPHRASE
, NULL
);
945 error
= pwmd_save(pwm
);
948 error
= pwmd_save2(pwm
);
951 error
= pwmd_save_async(pwm
);
954 error
= pwmd_save_async2(pwm
);
958 if (!error
&& method
>= 2)
959 error
= process_cmd(pwm
, NULL
, 0);
962 error
= pwmd_save(pwm
);
966 if (!error
&& filename
)
967 error
= pwmd_command(pwm
, &result
, "UNLOCK");
977 pwmd_free(socketpath
);