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 static void show_error(gpg_error_t error
)
40 fprintf(stderr
, "ERR %i %s\n", gpg_err_code(error
), pwmd_strerror(error
));
43 void catchsig(int sig
)
49 if (pwm
&& timeout
> 0 && elapsed
++ >= timeout
) {
50 error
= pwmd_terminate_pinentry(pwm
);
65 static void usage(const char *pn
)
68 "Reads PWMD protocol commands from standard input.\n\n"
69 "Usage: %s [-hv] [-E] [-s <socket>] [[-a [-P pinentry] [-t <seconds>]] |\n"
70 " [-p <password>]] [-S] [filename]\n"
71 " -E exit after a command failure\n"
74 " -a use pinentry(1) for password retrieval\n"
75 " -P path to the pinentry binary (/usr/bin/pinentry)\n"
76 " -t pinentry timeout\n"
77 " -S send the SAVE command after all others\n"
79 " -h this help text\n",
84 int main(int argc
, char *argv
[])
88 char *password
= NULL
;
89 char *filename
= NULL
;
90 char *socketpath
= NULL
;
91 char command
[8196], *p
;
92 int ret
= EXIT_SUCCESS
;
99 char *pinentry_path
= NULL
;
104 while ((opt
= getopt(argc
, argv
, "EhvaP:t:p:s:S")) != EOF
) {
113 socketpath
= xstrdup(optarg
);
116 password
= xstrdup(optarg
);
122 pinentry_path
= xstrdup(optarg
);
125 timeout
= atoi(optarg
);
128 printf("%s (pwmc)\n%s\n", PACKAGE_STRING
, PACKAGE_BUGREPORT
);
136 if (use_pinentry
&& password
) {
141 if (argc
- optind
== 1)
142 filename
= argv
[optind
];
146 if ((pwm
= pwmd_connect(socketpath
, &error
)) == NULL
) {
148 errx(EXIT_FAILURE
, "pwmd_connect(): %s", pwmd_strerror(error
));
152 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY
, 1);
162 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_PATH
, pinentry_path
);
172 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_TITLE
, "Password Manager Daemon");
181 snprintf(command
, sizeof(command
), "A password is required for the "
182 "file \"%s\". Please\nenter the password below.", filename
);
184 error
= pwmd_setopt(pwm
, PWMD_OPTION_PINENTRY_DESC
, command
);
194 error
= pwmd_setopt(pwm
, PWMD_OPTION_PASSWORD
, password
);
207 if (use_pinentry
&& timeout
!= -1) {
208 tcgetattr(STDOUT_FILENO
, &term
);
209 signal(SIGALRM
, catchsig
);
213 error
= pwmd_open(pwm
, filename
);
216 if (use_pinentry
&& timeout
!= -1 && elapsed
>= timeout
) {
217 error
= GPG_ERR_TIMEOUT
;
218 tcsetattr(STDOUT_FILENO
, 0, &term
);
228 signal(SIGALRM
, SIG_IGN
);
231 while ((p
= fgets(command
, sizeof(command
), stdin
)) != NULL
) {
235 if (p
[len
- 1] != '\n' && feof(stdin
) != 1) {
236 if ((t
= (char *)xrealloc(buf
, (total
+ len
+ 1) * sizeof(char))) == NULL
) {
240 memset(&command
, 0, sizeof(command
));
241 err(EXIT_FAILURE
, "xrealloc()");
245 memcpy(&buf
[total
], p
, len
);
252 if ((t
= (char *)xrealloc(buf
, (total
+ len
+ 1) * sizeof(char))) == NULL
) {
256 memset(&command
, 0, sizeof(command
));
257 err(EXIT_FAILURE
, "xrealloc()");
261 memcpy(&buf
[total
], p
, len
);
267 p
= buf
? buf
: command
;
269 if (p
[strlen(p
) - 1] == '\n')
270 p
[strlen(p
) - 1] = 0;
272 if (strcasecmp(p
, "BYE") == 0)
275 error
= pwmd_command(pwm
, &result
, p
);
284 memset(&command
, 0, sizeof(command
));
302 memset(&command
, 0, sizeof(command
));
305 if (result
[strlen(result
) - 1] == '\n')
306 result
[strlen(result
) - 1] = 0;
308 fwrite(result
, 1, strlen(result
), stdout
);
309 pwmd_free_result(result
);
317 memset(&command
, 0, sizeof(command
));
321 error
= pwmd_save(pwm
);
324 if (error
== EPWMD_BADKEY
|| error
== EPWMD_KEY
)