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 02110-1301 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"
64 "Usage: pwmc [-hvX] [-s <socket>] "
68 "[-PTND <string>] [-p <passphrase>]\n"
69 " [-S [-i <iter>]] [-c <name>] [-t <n>] [-d <fd>] [-I <fd>]\n"
72 " -E pinentry method (0=pwmd, 1=pwmd async, 2=libpwmd nb)\n"
73 " -y number of pinentry tries before failing (3)\n"
75 " -t pinentry timeout\n"
76 " -X disable showing of status messages from the server\n"
77 " -c set the client name\n"
78 " -s socket path (~/.pwmd/socket)\n"
80 " -P path to the pinentry binary (server default)\n"
82 " -N pinentry terminal type\n"
83 " -D pinentry display\n"
84 " -d redirect command output to the specified file descriptor\n"
85 " -I read inquire data from the specified file descriptor\n"
86 " -S send the SAVE command before exiting\n"
87 " -i encrypt with the specified number of iterations (-1 = 0 iterations)\n"
89 " -h this help text\n"));
98 static gpg_error_t
do_inquire(void *data
, const char *keyword
, gpg_error_t rc
,
99 char **result
, size_t *result_len
)
102 static char buf
[ASSUAN_LINELENGTH
];
105 struct inquire_s
*inq
= (struct inquire_s
*)data
;
108 memset(buf
, 0, sizeof(buf
));
116 snprintf(buf
, sizeof(buf
), "%s", inq
->data
);
123 while ((c
= fgetc(inq
->fp
)) != EOF
) {
124 if (len
== sizeof(buf
)) {
134 memset(buf
, 0, sizeof(buf
));
143 static int status_msg_cb(void *data
, const char *line
)
145 fprintf(stderr
, "%s\n", line
);
150 static gpg_error_t
do_nb_command(int fd
, int which
)
153 gpg_error_t error
= 0;
155 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
159 struct timeval tv
= {0, 50000};
163 n
= select(fd
+1, &fds
, NULL
, NULL
, &tv
);
166 if (FD_ISSET(fd
, &fds
)) {
167 pwmd_nb_status_t status
;
169 n
= read(fd
, &status
, sizeof(status
));
172 error
= gpg_error_from_errno(errno
);
177 error
= pwmd_open_nb_finalize(pwm
, &status
);
179 error
= pwmd_save_nb_finalize(pwm
, &status
);
194 int main(int argc
, char *argv
[])
197 char *password
= NULL
;
198 char *filename
= NULL
;
199 char *socketpath
= NULL
;
200 char command
[ASSUAN_LINELENGTH
], *p
;
201 int ret
= EXIT_SUCCESS
;
205 char *pinentry_path
= NULL
;
206 char *display
= NULL
, *tty
= NULL
, *ttytype
= NULL
;
207 int outfd
= STDOUT_FILENO
;
208 FILE *outfp
= stdout
;
209 int inquirefd
= STDIN_FILENO
;
210 FILE *inquirefp
= stdin
;
212 char *clientname
= NULL
;
213 char *inquire
= NULL
;
222 setlocale(LC_ALL
, "");
223 bindtextdomain("libpwmd", LOCALEDIR
);
226 while ((opt
= getopt(argc
, argv
, "y:t:E:c:I:XT:N:D:hvP:p:s:Si:d:")) != EOF
) {
228 while ((opt
= getopt(argc
, argv
, "t:c:I:XT:N:D:hvP:p:s:Si:d:")) != EOF
) {
233 method
= atoi(optarg
);
236 tries
= atoi(optarg
);
240 timeout
= atoi(optarg
);
243 clientname
= xstrdup(optarg
);
258 inquirefd
= atoi(optarg
);
259 inquirefp
= fdopen(inquirefd
, "r");
263 err(EXIT_FAILURE
, "%i", inquirefd
);
267 outfd
= atoi(optarg
);
268 outfp
= fdopen(outfd
, "w");
272 err(EXIT_FAILURE
, "%i", outfd
);
287 socketpath
= xstrdup(optarg
);
290 password
= xstrdup(optarg
);
291 memset(optarg
, 0, strlen(optarg
));
294 pinentry_path
= xstrdup(optarg
);
298 printf("%s (pwmc)\n%s\n", PACKAGE_STRING
, PACKAGE_BUGREPORT
);
307 filename
= argv
[optind
];
310 if ((pwm
= pwmd_connect(socketpath
, &error
)) == NULL
) {
312 errx(EXIT_FAILURE
, "pwmd_connect(): %s", pwmd_strerror(error
));
315 error
= pwmd_command(pwm
, &result
, "OPTION CLIENT NAME=%s", clientname
? clientname
: "pwmc");
320 errx(EXIT_FAILURE
, "pwmd_command(): %s", pwmd_strerror(error
));
324 error
= pwmd_command(pwm
, &result
, "OPTION TIMEOUT=%i", timeout
);
328 errx(EXIT_FAILURE
, "pwmd_command(): %s", pwmd_strerror(error
));
333 error
= pwmd_setopt(pwm
, PWMD_OPTION_PASSWORD
, password
);
344 error
= pwmd_command(pwm
, &result
, "OPTION PATH=%s", pinentry_path
);
351 error
= pwmd_command(pwm
, &result
, "OPTION DISPLAY=%s", display
);
358 error
= pwmd_command(pwm
, &result
, "OPTION TTYNAME=%s", tty
);
365 error
= pwmd_command(pwm
, &result
, "OPTION TTYTYPE=%s", ttytype
);
372 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY
, 1);
379 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TRIES
, tries
);
388 error
= pwmd_setopt(pwm
, PWMD_OPTION_STATUS_FUNC
, status_msg_cb
);
396 /* This method doesn't support PWMD_OPTION_PINENTRY_TRIES. */
398 error
= pwmd_open_async(pwm
, filename
);
402 s
= pwmd_process(pwm
, &error
);
405 } while (s
== ASYNC_PROCESS
);
410 else if (method
== 2) {
411 int fd
= pwmd_open_nb(pwm
, &error
, filename
, timeout
);
416 error
= do_nb_command(fd
, 0);
419 error
= pwmd_open(pwm
, filename
);
421 error
= pwmd_open(pwm
, filename
);
429 error
= pwmd_command(pwm
, &result
, "LOCK");
435 p
= fgets(command
, sizeof(command
), stdin
);
441 * This is a known INQUIRE command. We use pwmd_inquire() to send the
442 * data from the do_inquire() callback function.
444 if (strncasecmp(p
, "STORE ", 6) == 0) {
446 inquire
= (char *)"STORE";
448 else if (strncasecmp(p
, "IMPORT ", 7) == 0) {
450 inquire
= (char *)"IMPORT";
454 struct inquire_s
*inq
= (struct inquire_s
*)malloc(sizeof(struct inquire_s
));
457 error
= gpg_error_from_errno(ENOMEM
);
461 inq
->data
= xstrdup(p
);
463 error
= pwmd_inquire(pwm
, inquire
, do_inquire
, inq
);
468 if (strcasecmp(p
, "BYE") == 0)
471 error
= pwmd_command(pwm
, &result
, command
);
472 memset(command
, 0, sizeof(command
));
478 fwrite(result
, 1, strlen(result
), outfp
);
479 pwmd_free_result(result
);
483 memset(command
, 0, sizeof(command
));
485 if (!error
&& save
) {
487 error
= pwmd_command(pwm
, &result
, "OPTION ITERATIONS=%i", iter
);
495 error
= pwmd_save_async(pwm
);
499 s
= pwmd_process(pwm
, &error
);
502 } while (s
== ASYNC_PROCESS
);
507 else if (method
== 3) {
508 int fd
= pwmd_save_nb(pwm
, &error
);
513 error
= do_nb_command(fd
, 1);
516 error
= pwmd_save(pwm
);
518 error
= pwmd_save(pwm
);
522 if (!error
&& filename
)
523 error
= pwmd_command(pwm
, &result
, "UNLOCK");