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
33 static void usage(const char *pn
)
36 "Usage: %s [-hv] [-E] [-s <socket>] [[-a] | [-p <password>]] [-S] <filename>\n"
37 " -E exit after a command failure\n"
40 " -a use pinentry(1) for password retrieval\n"
41 " -S send the SAVE command after all others\n"
43 " -h this help text\n\n"
44 "Reads protocol commands from standard input.\n",
49 static void show_error(gpg_error_t error
)
51 fprintf(stderr
, "ERR %i %s\n", gpg_err_code(error
), pwmd_strerror(error
));
54 int main(int argc
, char *argv
[])
58 char *password
= NULL
;
59 char *filename
= NULL
;
60 char *socketpath
= NULL
;
61 char command
[8196], *p
;
62 int ret
= EXIT_SUCCESS
;
72 while ((opt
= getopt(argc
, argv
, "Ehvap:s:S")) != EOF
) {
81 socketpath
= xstrdup(optarg
);
84 password
= xstrdup(optarg
);
90 printf("%s (pwmc)\n%s\n", PACKAGE_STRING
, PACKAGE_BUGREPORT
);
98 if (use_pinentry
&& password
)
101 if (argc
- optind
!= 1)
104 filename
= argv
[optind
];
107 if ((pwm
= pwmd_connect(socketpath
, &error
)) == NULL
) {
111 errx(EXIT_FAILURE
, "pwmd_connect(): %s", pwmd_strerror(error
));
115 if (pwmd_setopt(pwm
, &error
, PWMD_OPTION_PINENTRY
, 1) != PWMD_OK
) {
121 if (pwmd_setopt(pwm
, &error
, PWMD_OPTION_TITLE
, "Password Manager Daemon") != PWMD_OK
) {
127 snprintf(command
, sizeof(command
), "A password is required for the "
128 "file \"%s\". Please\nenter the password below.", filename
);
130 if (pwmd_setopt(pwm
, &error
, PWMD_OPTION_DESC
, command
) != PWMD_OK
) {
137 if (pwmd_setopt(pwm
, &error
, PWMD_OPTION_PASSWORD
, password
) != PWMD_OK
) {
147 if (pwmd_open(pwm
, &error
, filename
) != PWMD_OK
) {
153 while (!quit
&& (p
= fgets(command
, sizeof(command
), stdin
)) != NULL
) {
157 if (p
[len
- 1] != '\n') {
158 if ((t
= (char *)xrealloc(buf
, (total
+ len
+ 1) * sizeof(char))) == NULL
) {
162 memset(&command
, 0, sizeof(command
));
163 err(EXIT_FAILURE
, "xrealloc()");
167 memcpy(&buf
[total
], p
, len
);
174 if ((t
= (char *)xrealloc(buf
, (total
+ len
+ 1) * sizeof(char))) == NULL
) {
178 memset(&command
, 0, sizeof(command
));
179 err(EXIT_FAILURE
, "xrealloc()");
183 memcpy(&buf
[total
], p
, len
);
189 p
= buf
? buf
: command
;
191 if (p
[strlen(p
) - 1] == '\n')
192 p
[strlen(p
) - 1] = 0;
194 if (strcasecmp(p
, "BYE") == 0)
197 if (pwmd_command(pwm
, &result
, &error
, p
) != PWMD_OK
) {
204 memset(&command
, 0, sizeof(command
));
222 memset(&command
, 0, sizeof(command
));
225 if (result
[strlen(result
) - 1] == '\n')
226 result
[strlen(result
) - 1] = 0;
228 fwrite(result
, 1, strlen(result
), stdout
);
229 pwmd_free_result(result
);
237 memset(&command
, 0, sizeof(command
));
240 if (pwmd_save(pwm
, &error
) != PWMD_OK
) {