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
34 #define N_(msgid) gettext(msgid)
40 #define xrealloc realloc
41 #define xmalloc malloc
42 #define xstrdup strdup
43 #define xcalloc calloc
49 static void show_error(gpg_error_t error
)
51 fprintf(stderr
, "ERR %i %s\n", gpg_err_code(error
), pwmd_strerror(error
));
54 void catchsig(int sig
)
60 if (pwm
&& timeout
> 0 && elapsed
++ >= timeout
) {
61 error
= pwmd_terminate_pinentry(pwm
);
76 static void usage(const char *pn
)
78 fprintf(stderr
, "%s", N_(
79 "Reads PWMD protocol commands from standard input.\n\n"
80 "Usage: pwmc [-hv] [-E] [-s <socket>] [[-a [-P pinentry] [-t <seconds>]] |\n"
81 " [-p <password>]] [-S] [filename]\n"
82 " -E exit after a command failure\n"
85 " -a use pinentry(1) for password retrieval\n"
86 " -P path to the pinentry binary (/usr/bin/pinentry)\n"
87 " -t pinentry timeout\n"
88 " -S send the SAVE command after all others\n"
90 " -h this help text\n"));
94 int main(int argc
, char *argv
[])
98 char *password
= NULL
;
99 char *filename
= NULL
;
100 char *socketpath
= NULL
;
101 char command
[8196], *p
;
102 int ret
= EXIT_SUCCESS
;
109 char *pinentry_path
= NULL
;
112 setlocale(LC_ALL
, "");
113 bindtextdomain("libpwmd", LOCALEDIR
);
116 while ((opt
= getopt(argc
, argv
, "EhvaP:t:p:s:S")) != EOF
) {
125 socketpath
= xstrdup(optarg
);
128 password
= xstrdup(optarg
);
134 pinentry_path
= xstrdup(optarg
);
137 timeout
= atoi(optarg
);
140 printf("%s (pwmc)\n%s\n", PACKAGE_STRING
, PACKAGE_BUGREPORT
);
148 if (use_pinentry
&& password
) {
153 filename
= argv
[optind
];
156 if ((pwm
= pwmd_connect(socketpath
, &error
)) == NULL
) {
158 errx(EXIT_FAILURE
, "pwmd_connect(): %s", pwmd_strerror(error
));
162 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY
, 1);
172 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_PATH
, pinentry_path
);
182 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TITLE
, N_("Password Manager Daemon"));
191 snprintf(command
, sizeof(command
), N_("A password is required for the "
192 "file \"%s\". Please\nenter the password below."), filename
);
194 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_DESC
, command
);
204 error
= pwmd_setopt(pwm
, PWMD_OPTION_PASSWORD
, password
);
217 if (use_pinentry
&& timeout
!= -1) {
218 tcgetattr(STDOUT_FILENO
, &term
);
219 signal(SIGALRM
, catchsig
);
223 error
= pwmd_open(pwm
, filename
);
226 if (error
== GPG_ERR_TIMEOUT
) {
227 tcsetattr(STDOUT_FILENO
, 0, &term
);
237 signal(SIGALRM
, SIG_IGN
);
240 while ((p
= fgets(command
, sizeof(command
), stdin
)) != NULL
) {
244 if (p
[len
- 1] != '\n' && feof(stdin
) != 1) {
245 if ((t
= (char *)xrealloc(buf
, (total
+ len
+ 1) * sizeof(char))) == NULL
) {
249 memset(&command
, 0, sizeof(command
));
250 err(EXIT_FAILURE
, "xrealloc()");
254 memcpy(&buf
[total
], p
, len
);
261 if ((t
= (char *)xrealloc(buf
, (total
+ len
+ 1) * sizeof(char))) == NULL
) {
265 memset(&command
, 0, sizeof(command
));
266 err(EXIT_FAILURE
, "xrealloc()");
270 memcpy(&buf
[total
], p
, len
);
276 p
= buf
? buf
: command
;
278 if (p
[strlen(p
) - 1] == '\n')
279 p
[strlen(p
) - 1] = 0;
281 if (strcasecmp(p
, "BYE") == 0)
284 error
= pwmd_command(pwm
, &result
, p
);
293 memset(&command
, 0, sizeof(command
));
311 memset(&command
, 0, sizeof(command
));
314 if (result
[strlen(result
) - 1] == '\n')
315 result
[strlen(result
) - 1] = 0;
317 fwrite(result
, 1, strlen(result
), stdout
);
318 pwmd_free_result(result
);
326 memset(&command
, 0, sizeof(command
));
330 error
= pwmd_save(pwm
);
333 if (use_pinentry
&& (error
== EPWMD_BADKEY
|| error
== EPWMD_KEY
))