1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2007-2008 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 02111-1307 USA
28 #include <sys/select.h>
41 #define N_(msgid) gettext(msgid)
47 #define xrealloc realloc
48 #define xmalloc malloc
49 #define xstrdup strdup
50 #define xcalloc calloc
55 static void show_error(gpg_error_t error
)
57 fprintf(stderr
, "ERR %i %s\n", gpg_err_code(error
), pwmd_strerror(error
));
60 static void usage(const char *pn
)
63 "Reads PWMD protocol commands from standard input.\n\n"
65 "Usage: pwmc [-hvX] [-s <socket>] [-E <n> -P -T -N -D | -p <password>] [-S [-i <iter>]]\n"
67 "Usage: pwmc [-hvX] [-s <socket>] [-P -T -N -D | -p <password>]\n"
69 " [-S [-i <iter>]] [-c <name>] [-t <n>] [-d <fd>] [-I <fd>] [filename]\n"
71 " -E pinentry method (0=pwmd, 1=pwmd async, 2=libpwmd nb)\n"
73 " -t pinentry timeout\n"
74 " -X disable showing of status messages from the server\n"
75 " -c set the client name\n"
76 " -s socket path (~/.pwmd/socket)\n"
78 " -P path to the pinentry binary (server default)\n"
80 " -N pinentry terminal type\n"
81 " -D pinentry display\n"
82 " -d redirect command output to the specified file descriptor\n"
83 " -I read inquire data from the specified file descriptor\n"
84 " -S send the SAVE command before exiting\n"
85 " -i encrypt with the specified number of iterations (-1 = 0 iterations)\n"
87 " -h this help text\n"));
96 static gpg_error_t
do_inquire(void *data
, const char *keyword
, gpg_error_t rc
,
97 char **result
, size_t *result_len
)
100 static char buf
[ASSUAN_LINELENGTH
];
103 struct inquire_s
*inq
= (struct inquire_s
*)data
;
106 memset(buf
, 0, sizeof(buf
));
114 snprintf(buf
, sizeof(buf
), "%s", inq
->data
);
121 while ((c
= fgetc(inq
->fp
)) != EOF
) {
122 if (len
== sizeof(buf
)) {
132 memset(buf
, 0, sizeof(buf
));
141 static int status_msg_cb(void *data
, const char *line
)
143 fprintf(stderr
, "%s\n", line
);
148 static gpg_error_t
do_nb_command(int fd
, int which
)
151 gpg_error_t error
= 0;
153 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
157 struct timeval tv
= {0, 50000};
161 n
= select(fd
+1, &fds
, NULL
, NULL
, &tv
);
164 if (FD_ISSET(fd
, &fds
)) {
165 pwmd_nb_status_t status
;
167 n
= read(fd
, &status
, sizeof(status
));
170 error
= gpg_error_from_errno(errno
);
175 error
= pwmd_open_nb_finalize(pwm
, &status
);
177 error
= pwmd_save_nb_finalize(pwm
, &status
);
192 int main(int argc
, char *argv
[])
195 char *password
= NULL
;
196 char *filename
= NULL
;
197 char *socketpath
= NULL
;
198 char command
[ASSUAN_LINELENGTH
], *p
;
199 int ret
= EXIT_SUCCESS
;
203 char *pinentry_path
= NULL
;
204 char *display
= NULL
, *tty
= NULL
, *ttytype
= NULL
;
205 int outfd
= STDOUT_FILENO
;
206 FILE *outfp
= stdout
;
207 int inquirefd
= STDIN_FILENO
;
208 FILE *inquirefp
= stdin
;
210 char *clientname
= NULL
;
211 char *inquire
= NULL
;
219 setlocale(LC_ALL
, "");
220 bindtextdomain("libpwmd", LOCALEDIR
);
223 while ((opt
= getopt(argc
, argv
, "t:E:c:I:XT:N:D:hvP:p:s:Si:d:")) != EOF
) {
225 while ((opt
= getopt(argc
, argv
, "t:c:I:XT:N:D:hvP:p:s:Si:d:")) != EOF
) {
230 method
= atoi(optarg
);
234 timeout
= atoi(optarg
);
237 clientname
= xstrdup(optarg
);
252 inquirefd
= atoi(optarg
);
253 inquirefp
= fdopen(inquirefd
, "r");
257 err(EXIT_FAILURE
, "%i", inquirefd
);
261 outfd
= atoi(optarg
);
262 outfp
= fdopen(outfd
, "w");
266 err(EXIT_FAILURE
, "%i", outfd
);
281 socketpath
= xstrdup(optarg
);
284 password
= xstrdup(optarg
);
285 memset(optarg
, 0, strlen(optarg
));
288 pinentry_path
= xstrdup(optarg
);
292 printf("%s (pwmc)\n%s\n", PACKAGE_STRING
, PACKAGE_BUGREPORT
);
301 filename
= argv
[optind
];
304 if ((pwm
= pwmd_connect(socketpath
, &error
)) == NULL
) {
306 errx(EXIT_FAILURE
, "pwmd_connect(): %s", pwmd_strerror(error
));
309 error
= pwmd_command(pwm
, &result
, "OPTION CLIENT NAME=%s", clientname
? clientname
: "pwmc");
314 errx(EXIT_FAILURE
, "pwmd_connect(): %s", pwmd_strerror(error
));
318 error
= pwmd_command(pwm
, &result
, "OPTION TIMEOUT=%i", timeout
);
322 errx(EXIT_FAILURE
, "pwmd_connect(): %s", pwmd_strerror(error
));
327 error
= pwmd_setopt(pwm
, PWMD_OPTION_PASSWORD
, password
);
338 error
= pwmd_command(pwm
, &result
, "OPTION PATH=%s", pinentry_path
);
345 error
= pwmd_command(pwm
, &result
, "OPTION DISPLAY=%s", display
);
352 error
= pwmd_command(pwm
, &result
, "OPTION TTYNAME=%s", tty
);
359 error
= pwmd_command(pwm
, &result
, "OPTION TTYTYPE=%s", ttytype
);
366 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY
, 1);
375 error
= pwmd_setopt(pwm
, PWMD_OPTION_STATUS_FUNC
, status_msg_cb
);
383 /* This method doesn't support PWMD_OPTION_PINENTRY_TRIES. */
385 error
= pwmd_open_async(pwm
, filename
);
389 s
= pwmd_process(pwm
, &error
);
392 } while (s
== ASYNC_PROCESS
);
397 else if (method
== 2) {
398 int fd
= pwmd_open_nb(pwm
, &error
, filename
, timeout
);
403 error
= do_nb_command(fd
, 0);
406 error
= pwmd_open(pwm
, filename
);
408 error
= pwmd_open(pwm
, filename
);
416 error
= pwmd_command(pwm
, &result
, "LOCK");
422 p
= fgets(command
, sizeof(command
), stdin
);
428 * This is a known INQUIRE command. We use pwmd_inquire() to send the
429 * data from the do_inquire() callback function.
431 if (strncasecmp(p
, "STORE ", 6) == 0) {
433 inquire
= (char *)"STORE";
435 else if (strncasecmp(p
, "IMPORT ", 7) == 0) {
437 inquire
= (char *)"IMPORT";
441 struct inquire_s
*inq
= (struct inquire_s
*)malloc(sizeof(struct inquire_s
));
444 error
= gpg_error_from_errno(ENOMEM
);
448 inq
->data
= xstrdup(p
);
450 error
= pwmd_inquire(pwm
, inquire
, do_inquire
, inq
);
455 if (strcasecmp(p
, "BYE") == 0)
458 error
= pwmd_command(pwm
, &result
, command
);
459 memset(command
, 0, sizeof(command
));
465 fwrite(result
, 1, strlen(result
), outfp
);
466 pwmd_free_result(result
);
470 memset(command
, 0, sizeof(command
));
472 if (!error
&& save
) {
474 error
= pwmd_command(pwm
, &result
, "OPTION ITERATIONS=%i", iter
);
482 error
= pwmd_save_async(pwm
);
486 s
= pwmd_process(pwm
, &error
);
489 } while (s
== ASYNC_PROCESS
);
494 else if (method
== 3) {
495 int fd
= pwmd_save_nb(pwm
, &error
);
500 error
= do_nb_command(fd
, 1);
503 error
= pwmd_save(pwm
);
505 error
= pwmd_save(pwm
);
509 if (!error
&& filename
)
510 error
= pwmd_command(pwm
, &result
, "UNLOCK");