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
31 static void usage(const char *pn
)
34 "Usage: %s [-hv] [-E] [-s <socket>] [[-a] | [-p <password>]] [-S] <filename>\n"
35 " -E exit after a command failure\n"
39 " -S send the SAVE command after all others\n"
41 " -h this help text\n",
46 static void pwmd_show_error(int ret
, int error
)
48 if (ret
== PWMD_PERROR
)
49 fprintf(stderr
, "ERR %03i %s\n", error
, pwmd_strerror(error
));
50 else if (ret
== PWMD_ERROR
)
51 fprintf(stderr
, "ERR 000 %s\n", strerror(error
));
53 fprintf(stderr
, "ERR 000 Is gpg-agent running? Is GPG_AGENT_INFO set?\n");
56 int main(int argc
, char *argv
[])
60 char *password
= NULL
;
61 char *filename
= NULL
;
62 char *socketpath
= NULL
;
63 char command
[8196], *p
;
72 while ((opt
= getopt(argc
, argv
, "Ehvap:s:S")) != EOF
) {
81 socketpath
= strdup(optarg
);
84 password
= strdup(optarg
);
90 printf("%s (pwmc)\n%s\n", PACKAGE_STRING
, PACKAGE_BUGREPORT
);
98 if (use_agent
&& password
)
101 if (argc
- optind
!= 1)
104 filename
= argv
[optind
];
106 if ((pwm
= pwmd_connect(socketpath
, &error
)) == NULL
) {
110 memset(password
, 0, strlen(password
));
114 err(EXIT_FAILURE
, "pwmd_connect()");
118 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_SETOPT
, PWMD_OPTION_USEAGENT
, 1))
120 pwmd_show_error(ret
, error
);
125 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_SETOPT
, PWMD_OPTION_TITLE
,
126 "Password Manager Daemon")) != PWMD_OK
) {
127 pwmd_show_error(ret
, error
);
132 snprintf(command
, sizeof(command
), "A password is required for the "
133 "file \"%s\". Please\nenter the password below.", filename
);
135 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_SETOPT
, PWMD_OPTION_DESC
,
136 command
)) != PWMD_OK
) {
137 pwmd_show_error(ret
, error
);
143 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_SETOPT
,
144 PWMD_OPTION_PASSWORD
, password
)) != PWMD_OK
) {
145 memset(password
, 0, strlen(password
));
147 pwmd_show_error(ret
, error
);
152 memset(password
, 0, strlen(password
));
156 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_OPEN
, filename
)) != PWMD_OK
) {
157 pwmd_show_error(ret
, error
);
162 while ((p
= fgets(command
, sizeof(command
), stdin
)) != NULL
) {
166 if (p
[len
- 1] != '\n') {
167 if ((t
= (char *)realloc(buf
, (total
+ len
+ 1) * sizeof(char))) == NULL
) {
169 memset(buf
, 0, total
);
173 err(EXIT_FAILURE
, "realloc()");
177 memcpy(&buf
[total
], p
, len
);
184 if ((t
= (char *)realloc(buf
, (total
+ len
+ 1) * sizeof(char))) == NULL
) {
186 memset(buf
, 0, total
);
190 err(EXIT_FAILURE
, "realloc()");
194 memcpy(&buf
[total
], p
, len
);
200 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_COMMAND
,
201 (buf
) ? buf
: command
)) != PWMD_OK
) {
203 memset(buf
, 0, total
);
207 memset(&command
, 0, sizeof(command
));
208 pwmd_show_error(ret
, error
);
217 memset(buf
, 0, total
);
224 printf("%s\n", (char *)result
);
230 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_SAVE
)) != PWMD_OK
) {
231 memset(&command
, 0, sizeof(command
));
232 pwmd_show_error(ret
, error
);