1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2007 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
39 #define N_(msgid) gettext(msgid)
45 #define xrealloc realloc
46 #define xmalloc malloc
47 #define xstrdup strdup
48 #define xcalloc calloc
54 static void show_error(gpg_error_t error
)
56 fprintf(stderr
, "ERR %i %s\n", gpg_err_code(error
), pwmd_strerror(error
));
59 void catchsig(int sig
)
65 if (pwm
&& timeout
> 0 && elapsed
++ >= timeout
) {
66 error
= pwmd_terminate_pinentry(pwm
);
81 static void usage(const char *pn
)
84 "Reads PWMD protocol commands from standard input.\n\n"
85 "Usage: pwmc [-hvX] [-s <socket>] [[-a [-P -T -N -D] [-t <seconds>]] |\n"
86 " [-p <password>]] [-S] [-d <fd>] [-I <fd>] [filename]\n"
87 " -X disable showing of status messages from the server\n"
88 " -s socket path (~/.pwmd/socket)\n"
90 " -a use pinentry(1) for password retrieval\n"
91 " -P path to the pinentry binary (%s)\n"
93 " -N pinentry terminal type\n"
94 " -D pinentry display\n"
95 " -t pinentry timeout\n"
96 " -d redirect command output to the specified file descriptor\n"
97 " -I read inquire data from the specified file descriptor\n"
98 " -S send the SAVE command before exiting\n"
100 " -h this help text\n"), PINENTRY_PATH
);
109 static gpg_error_t
do_inquire(void *data
, const char *keyword
, gpg_error_t rc
,
110 char **result
, size_t *result_len
)
113 static char buf
[ASSUAN_LINELENGTH
];
116 struct inquire_s
*inq
= (struct inquire_s
*)data
;
119 memset(buf
, 0, sizeof(buf
));
127 snprintf(buf
, sizeof(buf
), "%s", inq
->data
);
134 while ((c
= fgetc(inq
->fp
)) != EOF
) {
135 if (len
== sizeof(buf
)) {
145 memset(buf
, 0, sizeof(buf
));
154 static int status_msg_cb(void *data
, const char *line
)
156 fprintf(stderr
, "%s\n", line
);
160 int main(int argc
, char *argv
[])
163 int use_pinentry
= 0;
164 char *password
= NULL
;
165 char *filename
= NULL
;
166 char *socketpath
= NULL
;
167 char command
[ASSUAN_LINELENGTH
], *p
;
168 int ret
= EXIT_SUCCESS
;
172 char *pinentry_path
= NULL
;
173 char *display
= NULL
, *tty
= NULL
, *ttytype
= NULL
;
175 int outfd
= STDOUT_FILENO
;
176 FILE *outfp
= stdout
;
177 int inquirefd
= STDIN_FILENO
;
178 FILE *inquirefp
= stdin
;
181 setlocale(LC_ALL
, "");
182 bindtextdomain("libpwmd", LOCALEDIR
);
185 while ((opt
= getopt(argc
, argv
, "I:XT:N:D:hvaP:t:p:s:Sd:")) != EOF
) {
200 inquirefd
= atoi(optarg
);
201 inquirefp
= fdopen(inquirefd
, "r");
205 err(EXIT_FAILURE
, "%i", inquirefd
);
209 outfd
= atoi(optarg
);
210 outfp
= fdopen(outfd
, "w");
214 err(EXIT_FAILURE
, "%i", outfd
);
221 socketpath
= xstrdup(optarg
);
224 password
= xstrdup(optarg
);
225 memset(optarg
, 0, strlen(optarg
));
231 pinentry_path
= xstrdup(optarg
);
234 timeout
= atoi(optarg
);
238 printf("%s (pwmc)\n%s\n", PACKAGE_STRING
, PACKAGE_BUGREPORT
);
247 if (use_pinentry
&& password
) {
252 filename
= argv
[optind
];
255 if ((pwm
= pwmd_connect(socketpath
, &error
)) == NULL
) {
257 errx(EXIT_FAILURE
, "pwmd_connect(): %s", pwmd_strerror(error
));
261 error
= pwmd_setopt(pwm
, PWMD_OPTION_PASSWORD
, password
);
270 else if (use_pinentry
) {
271 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY
, 1);
276 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_PATH
, pinentry_path
);
281 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TITLE
,
282 N_("Password Manager Daemon"));
287 snprintf(command
, sizeof(command
), N_("A password is required for the "
288 "file \"%s\". Please\nenter the password below."), filename
);
290 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_DESC
, command
);
296 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_DISPLAY
, display
);
303 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TTY
, tty
);
310 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TERM
, ttytype
);
318 error
= pwmd_setopt(pwm
, PWMD_OPTION_STATUS_FUNC
, status_msg_cb
);
325 if (use_pinentry
&& timeout
!= -1) {
326 tcgetattr(STDOUT_FILENO
, &term
);
327 signal(SIGALRM
, catchsig
);
331 error
= pwmd_open(pwm
, filename
);
334 if (error
== GPG_ERR_TIMEOUT
) {
335 tcsetattr(STDOUT_FILENO
, 0, &term
);
343 signal(SIGALRM
, SIG_IGN
);
345 p
= fgets(command
, sizeof(command
), stdin
);
351 * This is a known INQUIRE command. We use pwmd_inquire() to send the
352 * data from the do_inquire() callback function.
354 if (strncasecmp(p
, "STORE ", 6) == 0) {
355 struct inquire_s
*inq
= (struct inquire_s
*)malloc(sizeof(struct inquire_s
));
358 error
= gpg_error_from_errno(ENOMEM
);
362 inq
->data
= xstrdup(p
+6);
364 error
= pwmd_inquire(pwm
, "STORE", do_inquire
, inq
);
369 if (strcasecmp(p
, "BYE") == 0)
372 error
= pwmd_command(pwm
, &result
, command
);
373 memset(command
, 0, sizeof(command
));
379 fwrite(result
, 1, strlen(result
), outfp
);
380 pwmd_free_result(result
);
384 memset(command
, 0, sizeof(command
));
386 if (!error
&& save
) {
388 error
= pwmd_save(pwm
);
391 if (use_pinentry
&& (error
== EPWMD_BADKEY
|| error
== EPWMD_KEY
))