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>
41 #define N_(msgid) gettext(msgid)
45 #define DEFAULT_PORT 22
48 static void show_error(gpg_error_t error
)
50 fprintf(stderr
, "ERR %i %s\n", gpg_err_code(error
), pwmd_strerror(error
));
53 static void usage(const char *pn
)
56 "Reads PWMD protocol commands from standard input.\n\n"
57 "Usage: pwmc [-hvX] [-s <socket>] "
61 "[-PTNDCM <string>] [-p <passphrase>]\n"
62 " [-S [-i <iter>]] [-c <name>] [-t <n>] [-d <fd>] [-I <fd>]\n"
64 " [-H <hostname> [-R <port>] -Y <identity> -K <known_hosts>\n"
65 " -U <username> [-G]] [filename]\n"
70 " -E pinentry method (0=pwmd, 1=pwmd async, 2=libpwmd nb)\n"
71 " -y number of pinentry tries before failing (3)\n"
74 " -H connect to hostname\n"
75 " -R alterate port (%i)\n"
77 " -Y SSH identity file\n"
78 " -K known host's file (for server validation)\n"
79 " -G retrieve the remote SSH host key and exit\n"
81 " -t pinentry timeout\n"
82 " -X disable showing of status messages from the server\n"
83 " -c set the client name\n"
84 " -s socket path (~/.pwmd/socket)\n"
86 " -P path to the pinentry binary (server default)\n"
88 " -N pinentry terminal type\n"
89 " -D pinentry display\n"
90 " -C pinentry LC_CTYPE\n"
91 " -M pinentry LC_MESSAGES\n"
92 " -d redirect command output to the specified file descriptor\n"
93 " -I read inquire data from the specified file descriptor\n"
94 " -S send the SAVE command before exiting\n"
95 " -i encrypt with the specified number of iterations\n"
97 " -h this help text\n"), DEFAULT_PORT
);
106 static gpg_error_t
do_inquire(void *data
, const char *keyword
, gpg_error_t rc
,
107 char **result
, size_t *result_len
)
110 static char buf
[ASSUAN_LINELENGTH
];
113 struct inquire_s
*inq
= (struct inquire_s
*)data
;
116 memset(buf
, 0, sizeof(buf
));
124 snprintf(buf
, sizeof(buf
), "%s", inq
->data
);
131 while ((c
= fgetc(inq
->fp
)) != EOF
) {
132 if (len
== sizeof(buf
)) {
142 memset(buf
, 0, sizeof(buf
));
151 static int status_msg_cb(void *data
, const char *line
)
153 fprintf(stderr
, "%s\n", line
);
158 static gpg_error_t
do_nb_command(int fd
, int which
)
161 gpg_error_t error
= 0;
163 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
167 struct timeval tv
= {0, 50000};
171 n
= select(fd
+1, &fds
, NULL
, NULL
, &tv
);
174 if (FD_ISSET(fd
, &fds
)) {
175 pwmd_nb_status_t status
;
177 n
= read(fd
, &status
, sizeof(status
));
180 error
= gpg_error_from_errno(errno
);
185 error
= pwmd_open_nb_finalize(pwm
, &status
);
187 error
= pwmd_save_nb_finalize(pwm
, &status
);
202 int main(int argc
, char *argv
[])
205 char *password
= NULL
;
206 char *filename
= NULL
;
207 char *socketpath
= NULL
;
208 char command
[ASSUAN_LINELENGTH
], *p
;
209 int ret
= EXIT_SUCCESS
;
213 char *pinentry_path
= NULL
;
214 char *display
= NULL
, *tty
= NULL
, *ttytype
= NULL
, *lcctype
= NULL
,
216 int outfd
= STDOUT_FILENO
;
217 FILE *outfp
= stdout
;
218 int inquirefd
= STDIN_FILENO
;
219 FILE *inquirefp
= stdin
;
221 char *clientname
= NULL
;
222 char *inquire
= NULL
;
228 int port
= DEFAULT_PORT
;
229 char *username
= NULL
;
231 char *known_hosts
= NULL
;
241 setlocale(LC_ALL
, "");
242 bindtextdomain("libpwmd", LOCALEDIR
);
247 while ((opt
= getopt(argc
, argv
, "C:M:GK:U:Y:H:R:y:t:E:c:I:XT:N:D:hvP:p:s:Si:d:")) != EOF
) {
249 while ((opt
= getopt(argc
, argv
, "C:M:y:t:E:c:I:XT:N:D:hvP:p:s:Si:d:")) != EOF
) {
253 while ((opt
= getopt(argc
, argv
, "C:M:GK:U:Y:H:R:t:c:I:XT:N:D:hvP:p:s:Si:d:")) != EOF
) {
255 while ((opt
= getopt(argc
, argv
, "C:M:t:c:I:XT:N:D:hvP:p:s:Si:d:")) != EOF
) {
261 method
= atoi(optarg
);
267 tries
= atoi(optarg
);
272 host
= xstrdup(optarg
);
278 ident
= xstrdup(optarg
);
281 username
= xstrdup(optarg
);
284 known_hosts
= xstrdup(optarg
);
291 lcctype
= xstrdup(optarg
);
294 lcmessages
= xstrdup(optarg
);
297 timeout
= atoi(optarg
);
300 clientname
= xstrdup(optarg
);
315 inquirefd
= atoi(optarg
);
316 inquirefp
= fdopen(inquirefd
, "r");
320 err(EXIT_FAILURE
, "%i", inquirefd
);
324 outfd
= atoi(optarg
);
325 outfp
= fdopen(outfd
, "w");
329 err(EXIT_FAILURE
, "%i", outfd
);
336 iter
= strtol(optarg
, NULL
, 10);
340 socketpath
= xstrdup(optarg
);
343 password
= xstrdup(optarg
);
344 memset(optarg
, 0, strlen(optarg
));
347 pinentry_path
= xstrdup(optarg
);
351 printf("%s (pwmc)\n%s\n", PACKAGE_STRING
, PACKAGE_BUGREPORT
);
360 filename
= argv
[optind
];
371 pwm
= pwmd_get_hostkey_async(host
, port
, &error
);
374 errx(EXIT_FAILURE
, "%s: %s", host
, pwmd_strerror(error
));
377 s
= pwmd_process(pwm
, &error
);
380 } while (s
== ASYNC_PROCESS
);
384 errx(EXIT_FAILURE
, "%s: %s", host
, pwmd_strerror(error
));
387 error
= pwmd_get_result(pwm
, &hostkey
);
391 errx(EXIT_FAILURE
, "%s: %s", host
, pwmd_strerror(error
));
394 printf("%s\n", hostkey
);
400 if ((pwm
= pwmd_tcp_connect_async(host
, port
, ident
, username
, known_hosts
, &error
)) == NULL
) {
402 errx(EXIT_FAILURE
, "%s: %s", host
, pwmd_strerror(error
));
406 s
= pwmd_process(pwm
, &error
);
409 } while (s
== ASYNC_PROCESS
);
414 errx(EXIT_FAILURE
, "%s: %s", host
, pwmd_strerror(error
));
423 char *hostkey
= pwmd_get_hostkey(host
, port
, &error
);
426 errx(EXIT_FAILURE
, "%s: %s", host
, pwmd_strerror(error
));
428 printf("%s\n", hostkey
);
429 pwmd_free_result(hostkey
);
433 if ((pwm
= pwmd_tcp_connect(host
, port
, ident
, username
, known_hosts
, &error
)) == NULL
) {
435 errx(EXIT_FAILURE
, "%s: %s", host
, pwmd_strerror(error
));
443 if ((pwm
= pwmd_connect(socketpath
, &error
)) == NULL
) {
445 errx(EXIT_FAILURE
, "pwmd_connect(): %s", pwmd_strerror(error
));
451 error
= pwmd_command(pwm
, &result
, "OPTION CLIENT NAME=%s", clientname
? clientname
: "pwmc");
460 error
= pwmd_command(pwm
, &result
, "VERSION");
462 if (error
&& error
!= GPG_ERR_ASS_UNKNOWN_CMD
) {
467 pwmd_free_result(result
);
469 if (error
== GPG_ERR_ASS_UNKNOWN_CMD
) {
477 /* pwmd version 2 or later. */
487 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TIMEOUT
, timeout
);
496 error
= pwmd_setopt(pwm
, PWMD_OPTION_PASSWORD
, password
);
507 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_PATH
, pinentry_path
);
514 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_DISPLAY
, display
);
521 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TTY
, tty
);
528 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TERM
, ttytype
);
535 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_LC_CTYPE
, lcctype
);
542 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_LC_MESSAGES
,
551 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY
, 1);
558 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TRIES
, tries
);
567 error
= pwmd_setopt(pwm
, PWMD_OPTION_STATUS_FUNC
, status_msg_cb
);
575 /* This method doesn't support PWMD_OPTION_PINENTRY_TRIES. */
577 error
= pwmd_open_async(pwm
, filename
);
581 s
= pwmd_process(pwm
, &error
);
584 } while (s
== ASYNC_PROCESS
);
589 else if (method
== 2) {
590 int fd
= pwmd_open_nb(pwm
, &error
, filename
, timeout
);
595 error
= do_nb_command(fd
, 0);
598 error
= pwmd_open(pwm
, filename
);
600 error
= pwmd_open(pwm
, filename
);
608 error
= pwmd_command(pwm
, &result
, "LOCK");
614 p
= fgets(command
, sizeof(command
), stdin
);
620 * This is a known INQUIRE command. We use pwmd_inquire() to send the
621 * data from the do_inquire() callback function.
623 if (strncasecmp(p
, "STORE ", 6) == 0) {
625 inquire
= (char *)"STORE";
627 else if (strncasecmp(p
, "IMPORT ", 7) == 0) {
629 inquire
= (char *)"IMPORT";
633 struct inquire_s
*inq
= (struct inquire_s
*)malloc(sizeof(struct inquire_s
));
636 error
= gpg_error_from_errno(ENOMEM
);
640 inq
->data
= xstrdup(p
);
642 error
= pwmd_inquire(pwm
, inquire
, do_inquire
, inq
);
647 if (strcasecmp(p
, "BYE") == 0)
650 error
= pwmd_command(pwm
, &result
, command
);
651 memset(command
, 0, sizeof(command
));
657 fwrite(result
, 1, strlen(result
), outfp
);
658 pwmd_free_result(result
);
662 memset(command
, 0, sizeof(command
));
664 if (!error
&& save
) {
666 error
= pwmd_command(pwm
, &result
, "OPTION ITERATIONS=%i", iter
);
674 error
= pwmd_save_async(pwm
);
678 s
= pwmd_process(pwm
, &error
);
681 } while (s
== ASYNC_PROCESS
);
686 else if (method
== 3) {
687 int fd
= pwmd_save_nb(pwm
, &error
);
692 error
= do_nb_command(fd
, 1);
695 error
= pwmd_save(pwm
);
697 error
= pwmd_save(pwm
);
701 if (!error
&& filename
)
702 error
= pwmd_command(pwm
, &result
, "UNLOCK");