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)
47 static void show_error(gpg_error_t error
)
49 fprintf(stderr
, "ERR %i %s\n", gpg_err_code(error
), pwmd_strerror(error
));
52 static void usage(const char *pn
)
55 "Reads PWMD protocol commands from standard input.\n\n"
56 "Usage: pwmc [-hvX] [-s <socket>] "
60 "[-PTND <string>] [-p <passphrase>]\n"
61 " [-S [-i <iter>]] [-c <name>] [-t <n>] [-d <fd>] [-I <fd>]\n"
64 " -E pinentry method (0=pwmd, 1=pwmd async, 2=libpwmd nb)\n"
65 " -y number of pinentry tries before failing (3)\n"
67 " -t pinentry timeout\n"
68 " -X disable showing of status messages from the server\n"
69 " -c set the client name\n"
70 " -s socket path (~/.pwmd/socket)\n"
72 " -P path to the pinentry binary (server default)\n"
74 " -N pinentry terminal type\n"
75 " -D pinentry display\n"
76 " -d redirect command output to the specified file descriptor\n"
77 " -I read inquire data from the specified file descriptor\n"
78 " -S send the SAVE command before exiting\n"
79 " -i encrypt with the specified number of iterations (-1 = 0 iterations)\n"
81 " -h this help text\n"));
90 static gpg_error_t
do_inquire(void *data
, const char *keyword
, gpg_error_t rc
,
91 char **result
, size_t *result_len
)
94 static char buf
[ASSUAN_LINELENGTH
];
97 struct inquire_s
*inq
= (struct inquire_s
*)data
;
100 memset(buf
, 0, sizeof(buf
));
108 snprintf(buf
, sizeof(buf
), "%s", inq
->data
);
115 while ((c
= fgetc(inq
->fp
)) != EOF
) {
116 if (len
== sizeof(buf
)) {
126 memset(buf
, 0, sizeof(buf
));
135 static int status_msg_cb(void *data
, const char *line
)
137 fprintf(stderr
, "%s\n", line
);
142 static gpg_error_t
do_nb_command(int fd
, int which
)
145 gpg_error_t error
= 0;
147 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
151 struct timeval tv
= {0, 50000};
155 n
= select(fd
+1, &fds
, NULL
, NULL
, &tv
);
158 if (FD_ISSET(fd
, &fds
)) {
159 pwmd_nb_status_t status
;
161 n
= read(fd
, &status
, sizeof(status
));
164 error
= gpg_error_from_errno(errno
);
169 error
= pwmd_open_nb_finalize(pwm
, &status
);
171 error
= pwmd_save_nb_finalize(pwm
, &status
);
186 int main(int argc
, char *argv
[])
189 char *password
= NULL
;
190 char *filename
= NULL
;
191 char *socketpath
= NULL
;
192 char command
[ASSUAN_LINELENGTH
], *p
;
193 int ret
= EXIT_SUCCESS
;
197 char *pinentry_path
= NULL
;
198 char *display
= NULL
, *tty
= NULL
, *ttytype
= NULL
;
199 int outfd
= STDOUT_FILENO
;
200 FILE *outfp
= stdout
;
201 int inquirefd
= STDIN_FILENO
;
202 FILE *inquirefp
= stdin
;
204 char *clientname
= NULL
;
205 char *inquire
= NULL
;
214 setlocale(LC_ALL
, "");
215 bindtextdomain("libpwmd", LOCALEDIR
);
218 while ((opt
= getopt(argc
, argv
, "y:t:E:c:I:XT:N:D:hvP:p:s:Si:d:")) != EOF
) {
220 while ((opt
= getopt(argc
, argv
, "t:c:I:XT:N:D:hvP:p:s:Si:d:")) != EOF
) {
225 method
= atoi(optarg
);
228 tries
= atoi(optarg
);
232 timeout
= atoi(optarg
);
235 clientname
= xstrdup(optarg
);
250 inquirefd
= atoi(optarg
);
251 inquirefp
= fdopen(inquirefd
, "r");
255 err(EXIT_FAILURE
, "%i", inquirefd
);
259 outfd
= atoi(optarg
);
260 outfp
= fdopen(outfd
, "w");
264 err(EXIT_FAILURE
, "%i", outfd
);
279 socketpath
= xstrdup(optarg
);
282 password
= xstrdup(optarg
);
283 memset(optarg
, 0, strlen(optarg
));
286 pinentry_path
= xstrdup(optarg
);
290 printf("%s (pwmc)\n%s\n", PACKAGE_STRING
, PACKAGE_BUGREPORT
);
299 filename
= argv
[optind
];
302 if ((pwm
= pwmd_connect(socketpath
, &error
)) == NULL
) {
304 errx(EXIT_FAILURE
, "pwmd_connect(): %s", pwmd_strerror(error
));
307 error
= pwmd_command(pwm
, &result
, "OPTION CLIENT NAME=%s", clientname
? clientname
: "pwmc");
312 errx(EXIT_FAILURE
, "pwmd_command(): %s", pwmd_strerror(error
));
316 error
= pwmd_command(pwm
, &result
, "OPTION TIMEOUT=%i", timeout
);
320 errx(EXIT_FAILURE
, "pwmd_command(): %s", pwmd_strerror(error
));
325 error
= pwmd_setopt(pwm
, PWMD_OPTION_PASSWORD
, password
);
336 error
= pwmd_command(pwm
, &result
, "OPTION PATH=%s", pinentry_path
);
343 error
= pwmd_command(pwm
, &result
, "OPTION DISPLAY=%s", display
);
350 error
= pwmd_command(pwm
, &result
, "OPTION TTYNAME=%s", tty
);
357 error
= pwmd_command(pwm
, &result
, "OPTION TTYTYPE=%s", ttytype
);
364 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY
, 1);
371 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TRIES
, tries
);
380 error
= pwmd_setopt(pwm
, PWMD_OPTION_STATUS_FUNC
, status_msg_cb
);
388 /* This method doesn't support PWMD_OPTION_PINENTRY_TRIES. */
390 error
= pwmd_open_async(pwm
, filename
);
394 s
= pwmd_process(pwm
, &error
);
397 } while (s
== ASYNC_PROCESS
);
402 else if (method
== 2) {
403 int fd
= pwmd_open_nb(pwm
, &error
, filename
, timeout
);
408 error
= do_nb_command(fd
, 0);
411 error
= pwmd_open(pwm
, filename
);
413 error
= pwmd_open(pwm
, filename
);
421 error
= pwmd_command(pwm
, &result
, "LOCK");
427 p
= fgets(command
, sizeof(command
), stdin
);
433 * This is a known INQUIRE command. We use pwmd_inquire() to send the
434 * data from the do_inquire() callback function.
436 if (strncasecmp(p
, "STORE ", 6) == 0) {
438 inquire
= (char *)"STORE";
440 else if (strncasecmp(p
, "IMPORT ", 7) == 0) {
442 inquire
= (char *)"IMPORT";
446 struct inquire_s
*inq
= (struct inquire_s
*)malloc(sizeof(struct inquire_s
));
449 error
= gpg_error_from_errno(ENOMEM
);
453 inq
->data
= xstrdup(p
);
455 error
= pwmd_inquire(pwm
, inquire
, do_inquire
, inq
);
460 if (strcasecmp(p
, "BYE") == 0)
463 error
= pwmd_command(pwm
, &result
, command
);
464 memset(command
, 0, sizeof(command
));
470 fwrite(result
, 1, strlen(result
), outfp
);
471 pwmd_free_result(result
);
475 memset(command
, 0, sizeof(command
));
477 if (!error
&& save
) {
479 error
= pwmd_command(pwm
, &result
, "OPTION ITERATIONS=%i", iter
);
487 error
= pwmd_save_async(pwm
);
491 s
= pwmd_process(pwm
, &error
);
494 } while (s
== ASYNC_PROCESS
);
499 else if (method
== 3) {
500 int fd
= pwmd_save_nb(pwm
, &error
);
505 error
= do_nb_command(fd
, 1);
508 error
= pwmd_save(pwm
);
510 error
= pwmd_save(pwm
);
514 if (!error
&& filename
)
515 error
= pwmd_command(pwm
, &result
, "UNLOCK");