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
38 #define N_(msgid) gettext(msgid)
44 #define xrealloc realloc
45 #define xmalloc malloc
46 #define xstrdup strdup
47 #define xcalloc calloc
53 static void show_error(gpg_error_t error
)
55 fprintf(stderr
, "ERR %i %s\n", gpg_err_code(error
), pwmd_strerror(error
));
58 void catchsig(int sig
)
64 if (pwm
&& timeout
> 0 && elapsed
++ >= timeout
) {
65 error
= pwmd_terminate_pinentry(pwm
);
80 static void usage(const char *pn
)
83 "Reads PWMD protocol commands from standard input.\n\n"
84 "Usage: pwmc [-hv] [-E] [-s <socket>] [[-a [-P -T -N -D] [-t <seconds>]] |\n"
85 " [-p <password>]] [-S] [-d <descriptor>] [filename]\n"
86 " -E exit after a command failure\n"
89 " -a use pinentry(1) for password retrieval\n"
90 " -P path to the pinentry binary (%s)\n"
92 " -N pinentry terminal type\n"
93 " -D pinentry display\n"
94 " -t pinentry timeout\n"
95 " -d redirect command output to the specified file descriptor\n"
96 " -S send the SAVE command after all others\n"
98 " -h this help text\n"), PINENTRY_PATH
);
102 int set_pinentry_option(pwmd_option_t option
, char *value
)
106 error
= pwmd_setopt(pwm
, option
, value
);
117 int main(int argc
, char *argv
[])
120 int use_pinentry
= 0;
121 char *password
= NULL
;
122 char *filename
= NULL
;
123 char *socketpath
= NULL
;
124 char command
[8196], *p
;
125 int ret
= EXIT_SUCCESS
;
132 char *pinentry_path
= NULL
;
133 char *display
= NULL
, *tty
= NULL
, *ttytype
= NULL
;
135 int outfd
= STDOUT_FILENO
;
136 FILE *outfp
= stdout
;
138 setlocale(LC_ALL
, "");
139 bindtextdomain("libpwmd", LOCALEDIR
);
142 while ((opt
= getopt(argc
, argv
, "T:N:D:EhvaP:t:p:s:Sd:")) != EOF
) {
154 outfd
= atoi(optarg
);
155 outfp
= fdopen(outfd
, "w");
158 err(EXIT_FAILURE
, "%i", outfd
);
167 socketpath
= xstrdup(optarg
);
170 password
= xstrdup(optarg
);
176 pinentry_path
= xstrdup(optarg
);
179 timeout
= atoi(optarg
);
182 printf("%s (pwmc)\n%s\n", PACKAGE_STRING
, PACKAGE_BUGREPORT
);
190 if (use_pinentry
&& password
) {
195 filename
= argv
[optind
];
198 if ((pwm
= pwmd_connect(socketpath
, &error
)) == NULL
) {
200 errx(EXIT_FAILURE
, "pwmd_connect(): %s", pwmd_strerror(error
));
204 if (set_pinentry_option(PWMD_OPTION_PASSWORD
, password
)) {
211 else if (use_pinentry
) {
212 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY
, 1);
220 if (set_pinentry_option(PWMD_OPTION_PINENTRY_PATH
, pinentry_path
))
223 if (set_pinentry_option(PWMD_OPTION_PINENTRY_TITLE
,
224 N_("Password Manager Daemon")))
227 snprintf(command
, sizeof(command
), N_("A password is required for the "
228 "file \"%s\". Please\nenter the password below."), filename
);
230 if (set_pinentry_option(PWMD_OPTION_PINENTRY_DESC
, command
))
234 if (set_pinentry_option(PWMD_OPTION_PINENTRY_DISPLAY
, display
))
239 if (set_pinentry_option(PWMD_OPTION_PINENTRY_TTY
, tty
))
244 if (set_pinentry_option(PWMD_OPTION_PINENTRY_TERM
, ttytype
))
250 if (use_pinentry
&& timeout
!= -1) {
251 tcgetattr(STDOUT_FILENO
, &term
);
252 signal(SIGALRM
, catchsig
);
256 error
= pwmd_open(pwm
, filename
);
259 if (error
== GPG_ERR_TIMEOUT
) {
260 tcsetattr(STDOUT_FILENO
, 0, &term
);
270 signal(SIGALRM
, SIG_IGN
);
273 while ((p
= fgets(command
, sizeof(command
), stdin
)) != NULL
) {
277 if (p
[len
- 1] != '\n' && feof(stdin
) != 1) {
278 if ((t
= (char *)xrealloc(buf
, (total
+ len
+ 1) * sizeof(char))) == NULL
) {
282 memset(&command
, 0, sizeof(command
));
283 err(EXIT_FAILURE
, "xrealloc()");
287 memcpy(&buf
[total
], p
, len
);
294 if ((t
= (char *)xrealloc(buf
, (total
+ len
+ 1) * sizeof(char))) == NULL
) {
298 memset(&command
, 0, sizeof(command
));
299 err(EXIT_FAILURE
, "xrealloc()");
303 memcpy(&buf
[total
], p
, len
);
309 p
= buf
? buf
: command
;
311 if (p
[strlen(p
) - 1] == '\n')
312 p
[strlen(p
) - 1] = 0;
314 if (strcasecmp(p
, "BYE") == 0)
317 error
= pwmd_command(pwm
, &result
, p
);
326 memset(&command
, 0, sizeof(command
));
344 memset(&command
, 0, sizeof(command
));
347 if (result
[strlen(result
) - 1] == '\n')
348 result
[strlen(result
) - 1] = 0;
350 fwrite(result
, 1, strlen(result
), outfp
);
351 pwmd_free_result(result
);
359 memset(&command
, 0, sizeof(command
));
363 error
= pwmd_save(pwm
);
366 if (use_pinentry
&& (error
== EPWMD_BADKEY
|| error
== EPWMD_KEY
))