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] \n"
67 "Usage: pwmc [-hvX] [-s <socket>] [-P -T -N -D | -p <password>] [-S] \n"
69 " [-c <name>] [-d <fd>] [-I <fd>] [filename]\n"
71 " -E pinentry method (0=pwmd, 1=pwmd async, 2=libpwmd, 3=libpwmd nb)\n"
73 " -X disable showing of status messages from the server\n"
74 " -c set the client name\n"
75 " -s socket path (~/.pwmd/socket)\n"
77 " -P path to the pinentry binary (server default)\n"
79 " -N pinentry terminal type\n"
80 " -D pinentry display\n"
81 " -d redirect command output to the specified file descriptor\n"
82 " -I read inquire data from the specified file descriptor\n"
83 " -S send the SAVE command before exiting\n"
85 " -h this help text\n"));
94 static gpg_error_t
do_inquire(void *data
, const char *keyword
, gpg_error_t rc
,
95 char **result
, size_t *result_len
)
98 static char buf
[ASSUAN_LINELENGTH
];
101 struct inquire_s
*inq
= (struct inquire_s
*)data
;
104 memset(buf
, 0, sizeof(buf
));
112 snprintf(buf
, sizeof(buf
), "%s", inq
->data
);
119 while ((c
= fgetc(inq
->fp
)) != EOF
) {
120 if (len
== sizeof(buf
)) {
130 memset(buf
, 0, sizeof(buf
));
139 static int status_msg_cb(void *data
, const char *line
)
141 fprintf(stderr
, "%s\n", line
);
146 static gpg_error_t
do_nb_command(int fd
, int which
)
149 gpg_error_t error
= 0;
151 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
155 struct timeval tv
= {0, 50000};
159 n
= select(fd
+1, &fds
, NULL
, NULL
, &tv
);
162 if (FD_ISSET(fd
, &fds
)) {
163 pwmd_nb_status_t status
;
165 n
= read(fd
, &status
, sizeof(status
));
168 error
= gpg_error_from_errno(errno
);
173 error
= pwmd_open_nb_finalize(pwm
, &status
);
175 error
= pwmd_save_nb_finalize(pwm
, &status
);
182 fprintf(stderr
, "Waiting ...\n");
190 int main(int argc
, char *argv
[])
193 char *password
= NULL
;
194 char *filename
= NULL
;
195 char *socketpath
= NULL
;
196 char command
[ASSUAN_LINELENGTH
], *p
;
197 int ret
= EXIT_SUCCESS
;
201 char *pinentry_path
= NULL
;
202 char *display
= NULL
, *tty
= NULL
, *ttytype
= NULL
;
203 int outfd
= STDOUT_FILENO
;
204 FILE *outfp
= stdout
;
205 int inquirefd
= STDIN_FILENO
;
206 FILE *inquirefp
= stdin
;
208 char *clientname
= NULL
;
209 char *inquire
= NULL
;
215 setlocale(LC_ALL
, "");
216 bindtextdomain("libpwmd", LOCALEDIR
);
219 while ((opt
= getopt(argc
, argv
, "E:c:I:XT:N:D:hvP:p:s:Sd:")) != EOF
) {
221 while ((opt
= getopt(argc
, argv
, "c:I:XT:N:D:hvP:p:s:Sd:")) != EOF
) {
226 method
= atoi(optarg
);
230 clientname
= xstrdup(optarg
);
245 inquirefd
= atoi(optarg
);
246 inquirefp
= fdopen(inquirefd
, "r");
250 err(EXIT_FAILURE
, "%i", inquirefd
);
254 outfd
= atoi(optarg
);
255 outfp
= fdopen(outfd
, "w");
259 err(EXIT_FAILURE
, "%i", outfd
);
266 socketpath
= xstrdup(optarg
);
269 password
= xstrdup(optarg
);
270 memset(optarg
, 0, strlen(optarg
));
273 pinentry_path
= xstrdup(optarg
);
277 printf("%s (pwmc)\n%s\n", PACKAGE_STRING
, PACKAGE_BUGREPORT
);
286 filename
= argv
[optind
];
289 if ((pwm
= pwmd_connect(socketpath
, &error
)) == NULL
) {
291 errx(EXIT_FAILURE
, "pwmd_connect(): %s", pwmd_strerror(error
));
294 error
= pwmd_command(pwm
, &result
, "OPTION CLIENT NAME=%s", clientname
? clientname
: "pwmc");
299 errx(EXIT_FAILURE
, "pwmd_connect(): %s", pwmd_strerror(error
));
303 error
= pwmd_setopt(pwm
, PWMD_OPTION_PASSWORD
, password
);
314 error
= pwmd_command(pwm
, &result
, "OPTION PATH=%s", pinentry_path
);
321 error
= pwmd_command(pwm
, &result
, "OPTION DISPLAY=%s", display
);
328 error
= pwmd_command(pwm
, &result
, "OPTION TTYNAME=%s", tty
);
335 error
= pwmd_command(pwm
, &result
, "OPTION TTYTYPE=%s", ttytype
);
342 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY
, 1);
351 error
= pwmd_setopt(pwm
, PWMD_OPTION_STATUS_FUNC
, status_msg_cb
);
360 error
= pwmd_open_async(pwm
, filename
);
364 s
= pwmd_process(pwm
, &error
);
365 fprintf(stderr
, "Waiting ...\n");
367 } while (s
== ASYNC_PROCESS
);
372 else if (method
== 3) {
373 int fd
= pwmd_open_nb(pwm
, &error
, filename
, 0);
378 error
= do_nb_command(fd
, 0);
381 error
= pwmd_open(pwm
, filename
);
383 error
= pwmd_open(pwm
, filename
);
390 p
= fgets(command
, sizeof(command
), stdin
);
396 * This is a known INQUIRE command. We use pwmd_inquire() to send the
397 * data from the do_inquire() callback function.
399 if (strncasecmp(p
, "STORE ", 6) == 0) {
403 else if (strncasecmp(p
, "IMPORT ", 7) == 0) {
409 struct inquire_s
*inq
= (struct inquire_s
*)malloc(sizeof(struct inquire_s
));
412 error
= gpg_error_from_errno(ENOMEM
);
416 inq
->data
= xstrdup(p
);
418 error
= pwmd_inquire(pwm
, inquire
, do_inquire
, inq
);
423 if (strcasecmp(p
, "BYE") == 0)
426 error
= pwmd_command(pwm
, &result
, command
);
427 memset(command
, 0, sizeof(command
));
433 fwrite(result
, 1, strlen(result
), outfp
);
434 pwmd_free_result(result
);
438 memset(command
, 0, sizeof(command
));
440 if (!error
&& save
) {
443 error
= pwmd_save_async(pwm
);
447 s
= pwmd_process(pwm
, &error
);
448 fprintf(stderr
, "Waiting ...\n");
450 } while (s
== ASYNC_PROCESS
);
455 else if (method
== 3) {
456 int fd
= pwmd_save_nb(pwm
, &error
);
461 error
= do_nb_command(fd
, 1);
464 error
= pwmd_save(pwm
);
466 error
= pwmd_save(pwm
);