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 " file://[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 static gpg_error_t
knownhost_cb(void *data
, const char *host
, const char *key
,
273 char *buf
= pwmd_strdup_printf(N_("Password Manager Daemon: %s\n\nWhile attepting an SSH connection to %s, there was a problem verifying it's hostkey against the list of known and trusted hosts file. Would you like to treat this connection as trusted for this and future connections?"), (char *)data
, host
);
275 rc
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TITLE
, buf
);
281 return pwmd_getpin(pwm
, NULL
, NULL
, PWMD_PINENTRY_CONFIRM
);
284 int main(int argc
, char *argv
[])
287 char *password
= NULL
;
288 char *filename
= NULL
;
289 char *socketpath
= NULL
;
290 char command
[ASSUAN_LINELENGTH
], *p
;
291 int ret
= EXIT_SUCCESS
;
295 char *pinentry_path
= NULL
;
296 char *display
= NULL
, *tty
= NULL
, *ttytype
= NULL
, *lcctype
= NULL
,
298 int outfd
= STDOUT_FILENO
;
299 FILE *outfp
= stdout
;
300 int inquirefd
= STDIN_FILENO
;
301 FILE *inquirefp
= stdin
;
303 char *clientname
= "pwmc";
304 char *inquire
= NULL
;
312 int port
= DEFAULT_PORT
;
313 char *username
= NULL
;
315 char *known_hosts
= NULL
;
317 int prot
= PWMD_IP_ANY
;
327 char *url_string
= NULL
;
328 /* The order is important. */
334 OPT_HOST
, OPT_PORT
, OPT_IDENTITY
, OPT_KNOWN_HOSTS
, OPT_USER
,
335 OPT_GET_HOSTKEY
, OPT_IPV4
, OPT_IPV6
,
337 OPT_URL
, OPT_LOCAL
, OPT_FORCE_SAVE
, OPT_TTYNAME
, OPT_TTYTYPE
,
338 OPT_DISPLAY
, OPT_LC_CTYPE
, OPT_LC_MESSAGES
, OPT_TIMEOUT
, OPT_TRIES
,
339 OPT_PINENTRY
, OPT_PASSPHRASE
, OPT_SOCKET
, OPT_SAVE
, OPT_ITERATIONS
,
340 OPT_OUTPUT_FD
, OPT_INQUIRE_FD
, OPT_NO_STATUS
, OPT_NAME
, OPT_VERSION
,
343 const struct option long_opts
[] = {
345 { "debug", 1, 0, 0 },
348 { "host", 1, 0, 'h' },
349 { "port", 1, 0, 'p' },
350 { "identity", 1, 0, 'i' },
351 { "known-hosts", 1, 0, 'k' },
352 { "user", 1, 0, 'u' },
353 { "get-hostkey", 0, 0, 'g' },
354 { "ipv4", 0, 0, '4' },
355 { "ipv6", 0, 0, '6' },
358 { "local-pinentry", 0, 0 },
359 { "force-save", 0, 0 },
360 { "ttyname", 1, 0, 'y' },
361 { "ttytype", 1, 0, 't' },
362 { "display", 1, 0, 'd' },
363 { "lc-ctype", 1, 0, 0 },
364 { "lc-messages", 1, 0, 0 },
365 { "timeout", 1, 0, 0 },
366 { "tries", 1, 0, 0 },
367 { "pinentry", 1, 0, 0 },
368 { "passphrase", 1, 0, 'P' },
369 { "socket", 1, 0, 0 },
370 { "save", 0, 0, 'S' },
371 { "iterations", 1, 0, 'I' },
372 { "output-fd", 1, 0, 0 },
373 { "inquire-fd", 1, 0, 0 },
374 { "no-status", 0, 0, 0 },
375 { "name", 1, 0, 'n' },
376 { "version", 0, 0, 0 },
381 const char *optstring
= "46h:p:i:k:u:gy:t:d:P:I:Sn:";
383 const char *optstring
= "y:t:d:P:I:Sn:";
388 setlocale(LC_ALL
, "");
389 bindtextdomain("libpwmd", LOCALEDIR
);
392 while ((opt
= getopt_long(argc
, argv
, optstring
, long_opts
, &opt_index
)) != -1) {
394 /* Handle long options without a short option part. */
399 method
= atoi(optarg
);
412 save
= force_save
= 1;
415 lcctype
= pwmd_strdup(optarg
);
417 case OPT_LC_MESSAGES
:
418 lcmessages
= pwmd_strdup(optarg
);
421 timeout
= atoi(optarg
);
424 tries
= atoi(optarg
);
428 socketpath
= pwmd_strdup(optarg
);
431 inquirefd
= atoi(optarg
);
432 inquirefp
= fdopen(inquirefd
, "r");
436 err(EXIT_FAILURE
, "%i", inquirefd
);
440 outfd
= atoi(optarg
);
441 outfp
= fdopen(outfd
, "w");
445 err(EXIT_FAILURE
, "%i", outfd
);
453 printf("%s (pwmc)\n%s\n\n"
454 "Compile-time features:\n"
476 , PACKAGE_STRING
, PACKAGE_BUGREPORT
);
479 pinentry_path
= optarg
;
482 usage(argv
[0], EXIT_SUCCESS
);
484 usage(argv
[0], EXIT_FAILURE
);
496 host
= pwmd_strdup(optarg
);
502 ident
= pwmd_strdup(optarg
);
505 username
= pwmd_strdup(optarg
);
508 known_hosts
= pwmd_strdup(optarg
);
527 iter
= strtol(optarg
, NULL
, 10);
531 password
= pwmd_strdup(optarg
);
532 memset(optarg
, 0, strlen(optarg
));
539 usage(argv
[0], EXIT_FAILURE
);
547 if (host
&& !get
&& (!known_hosts
|| !ident
)) {
549 usage(argv
[0], EXIT_FAILURE
);
554 usage(argv
[0], EXIT_FAILURE
);
561 filename
= argv
[optind
];
563 pwm
= pwmd_new(clientname
);
570 if (prot
!= PWMD_IP_ANY
) {
571 error
= pwmd_setopt(pwm
, PWMD_OPTION_IP_VERSION
, prot
);
577 error
= pwmd_setopt(pwm
, PWMD_OPTION_KNOWNHOST_CB
, knownhost_cb
);
582 error
= pwmd_setopt(pwm
, PWMD_OPTION_KNOWNHOST_DATA
, clientname
);
592 error
= pwmd_get_hostkey_async(pwm
, host
, port
);
595 errx(EXIT_FAILURE
, "%s: %s", host
, pwmd_strerror(error
));
597 error
= process_cmd(pwm
, &hostkey
, 0);
602 printf("%s", hostkey
);
610 error
= pwmd_connect_url_async(pwm
, url_string
);
612 error
= pwmd_ssh_connect_async(pwm
, host
, port
, ident
, username
,
618 error
= process_cmd(pwm
, NULL
, 0);
628 error
= pwmd_get_hostkey(pwm
, host
, port
, &hostkey
);
633 printf("%s", hostkey
);
641 error
= pwmd_connect_url(pwm
, url_string
);
643 error
= pwmd_ssh_connect(pwm
, host
, port
, ident
, username
, known_hosts
);
654 error
= pwmd_connect_url(pwm
, url_string
);
656 error
= pwmd_connect(pwm
, socketpath
);
664 error
= pwmd_socket_type(pwm
, &s
);
669 if (s
== PWMD_SOCKET_SSH
&& force_save
&& !local_pin
) {
670 error
= GPG_ERR_WRONG_KEY_USAGE
;
675 error
= pwmd_command(pwm
, &result
, "VERSION");
685 usage(argv
[0], EXIT_FAILURE
);
690 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TIMEOUT
, timeout
);
696 if (local_pin
&& filename
) {
697 error
= pwmd_command(pwm
, NULL
, "ISCACHED %s", filename
);
699 if (error
== GPG_ERR_NOT_FOUND
) {
701 if (try++ == local_tries
)
710 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TIMEOUT
, 0);
712 error
= pwmd_getpin(pwm
, filename
, &password
,
713 try > 1 ? PWMD_PINENTRY_OPEN_FAILED
: PWMD_PINENTRY_OPEN
);
718 error
= pwmd_setopt(pwm
, PWMD_OPTION_PASSPHRASE
, password
);
726 else if (error
&& error
!= GPG_ERR_ENOENT
)
731 error
= pwmd_setopt(pwm
, PWMD_OPTION_PASSPHRASE
, password
);
738 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_PATH
, pinentry_path
);
745 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_DISPLAY
, display
);
752 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TTY
, tty
);
759 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TERM
, ttytype
);
766 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_LC_CTYPE
, lcctype
);
773 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_LC_MESSAGES
,
781 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TRIES
, tries
);
789 error
= pwmd_setopt(pwm
, PWMD_OPTION_STATUS_CB
, status_msg_cb
);
800 error
= pwmd_open(pwm
, filename
);
803 error
= pwmd_open2(pwm
, filename
);
806 error
= pwmd_open_async(pwm
, filename
);
810 error
= pwmd_open_async2(pwm
, filename
);
814 if (error
&& local_pin
&& error
== GPG_ERR_INV_PASSPHRASE
)
821 error
= process_cmd(pwm
, &result
, 0);
823 error
= pwmd_open(pwm
, filename
);
825 if (error
&& local_pin
&& error
== GPG_ERR_INV_PASSPHRASE
)
834 error
= pwmd_command(pwm
, &result
, "LOCK");
840 fcntl(STDIN_FILENO
, F_SETFL
, O_NONBLOCK
);
845 error
= process_cmd(pwm
, NULL
, 1);
850 n
= read(STDIN_FILENO
, command
, sizeof(command
));
856 error
= gpg_error_from_errno(errno
);
864 if (n
&& command
[strlen(command
)-1] == '\n')
865 command
[strlen(command
)-1] = 0;
875 * This is a known INQUIRE command. We use pwmd_inquire() to send the
876 * data from the do_inquire() callback function.
878 if (strncasecmp(p
, "STORE ", 6) == 0) {
880 inquire
= (char *)"STORE";
882 else if (strncasecmp(p
, "IMPORT ", 7) == 0) {
884 inquire
= (char *)"IMPORT";
888 struct inquire_s
*inq
= (struct inquire_s
*)pwmd_malloc(sizeof(struct inquire_s
));
891 error
= gpg_error_from_errno(ENOMEM
);
895 inq
->data
= pwmd_strdup(p
);
897 error
= pwmd_inquire(pwm
, inquire
, do_inquire
, inq
);
902 if (strcasecmp(p
, "BYE") == 0)
905 error
= pwmd_command(pwm
, &result
, command
);
906 memset(command
, 0, sizeof(command
));
912 fwrite(result
, 1, strlen(result
), outfp
);
917 memset(command
, 0, sizeof(command
));
921 if (!error
&& save
&& filename
) {
923 error
= pwmd_command(pwm
, &result
, "SET ITERATIONS=%i", iter
);
933 error
= pwmd_command(pwm
, NULL
, "ISCACHED %s", filename
);
935 if (error
&& error
!= GPG_ERR_NOT_FOUND
&&
936 error
!= GPG_ERR_ENOENT
)
942 error
= pwmd_getpin(pwm
, filename
, &p1
, PWMD_PINENTRY_SAVE
);
947 error
= pwmd_getpin(pwm
, filename
, &password
,
948 PWMD_PINENTRY_SAVE_CONFIRM
);
955 if ((p1
|| password
) && ((!p1
&& password
) || (!password
&& p1
) ||
956 strcmp(p1
, password
))) {
966 error
= pwmd_setopt(pwm
, PWMD_OPTION_PASSPHRASE
, password
);
976 error
= pwmd_command(pwm
, NULL
, "CLEARCACHE %s", filename
);
982 error
= pwmd_setopt(pwm
, PWMD_OPTION_PASSPHRASE
, NULL
);
993 error
= pwmd_save(pwm
);
996 error
= pwmd_save2(pwm
);
999 error
= pwmd_save_async(pwm
);
1002 error
= pwmd_save_async2(pwm
);
1006 if (!error
&& method
>= 2)
1007 error
= process_cmd(pwm
, NULL
, 0);
1010 error
= pwmd_save(pwm
);
1014 if (!error
&& filename
)
1015 error
= pwmd_command(pwm
, &result
, "UNLOCK");
1025 pwmd_free(socketpath
);