1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2006-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
30 int main(int argc
, char *argv
[])
36 char *socketpath
= NULL
;
38 char buf
[LINE_MAX
], *b64
;
42 while ((opt
= getopt(argc
, argv
, "hs:a")) != EOF
) {
52 fprintf(stderr
, "Usage: %s [-h] [-a] [-s <socketpath>]\n"
54 " -s specify an alternate socket path\n"
55 " -h this help text\n", argv
[0]);
60 fprintf(stderr
, "Connecting to socket...\n");
61 if ((pwm
= pwmd_connect(socketpath
, &error
)) == NULL
)
62 errx(EXIT_FAILURE
, "pwmd_connect(): %s", strerror(error
));
64 fprintf(stderr
, "Connected. fd=%i\n", pwm
->fd
);
67 fprintf(stderr
, "Setting password 'test'...\n");
69 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_SETOPT
,
70 PWMD_OPTION_PASSWORD
, "test")) != PWMD_OK
) {
73 if (ret
== PWMD_ERROR
)
74 errx(EXIT_FAILURE
, "PWMD_SETOPT: %s", strerror(error
));
76 errx(EXIT_FAILURE
, "PWMD_SETOPT: %s", pwmd_strerror(error
));
80 fprintf(stderr
, "Setting PWMD_OPTION_USEAGENT...\n");
81 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_SETOPT
,
82 PWMD_OPTION_USEAGENT
, 1)) != PWMD_OK
) {
85 if (ret
== PWMD_ERROR
)
86 errx(EXIT_FAILURE
, "PWMD_SETOPT: %s", strerror(error
));
88 errx(EXIT_FAILURE
, "PWMD_SETOPT: %s", pwmd_strerror(error
));
91 fprintf(stderr
, "Setting PWMD_OPTION_TITLE...\n");
92 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_SETOPT
,
93 PWMD_OPTION_TITLE
, "PWMD Client Test")) != PWMD_OK
) {
96 if (ret
== PWMD_ERROR
)
97 errx(EXIT_FAILURE
, "PWMD_SETOPT: %s", strerror(error
));
99 errx(EXIT_FAILURE
, "PWMD_SETOPT: %s", pwmd_strerror(error
));
102 fprintf(stderr
, "Setting PWMD_OPTION_PROMPT...\n");
103 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_SETOPT
,
104 PWMD_OPTION_PROMPT
, "Password please:")) != PWMD_OK
) {
107 if (ret
== PWMD_ERROR
)
108 errx(EXIT_FAILURE
, "PWMD_SETOPT: %s", strerror(error
));
110 errx(EXIT_FAILURE
, "PWMD_SETOPT: %s", pwmd_strerror(error
));
113 fprintf(stderr
, "Setting PWMD_OPTION_DESC...\n");
114 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_SETOPT
,
115 PWMD_OPTION_DESC
, "Enter the password for file \"testfile\".")) != PWMD_OK
) {
118 if (ret
== PWMD_ERROR
)
119 errx(EXIT_FAILURE
, "PWMD_SETOPT: %s", strerror(error
));
121 errx(EXIT_FAILURE
, "PWMD_SETOPT: %s", pwmd_strerror(error
));
125 fprintf(stderr
, "Opening 'testfile'...\n");
126 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_OPEN
, "testfile")) != PWMD_OK
) {
129 if (ret
== PWMD_ERROR
)
130 errx(EXIT_FAILURE
, "PWMD_OPEN: %s", strerror(error
));
131 else if (ret
== PWMD_PERROR
)
132 errx(EXIT_FAILURE
, "PWMD_OPEN: %s", pwmd_strerror(error
));
134 errx(EXIT_FAILURE
, "PWMD_OPEN: gpg-agent failed. Is GPG_AGENT_INFO set?");
137 fprintf(stderr
, "File opened. Creating 'newaccount' and test element...\n");
138 b64
= pwmd_base64_encode("somehost.com", 12);
139 snprintf(buf
, sizeof(buf
), "STORE newaccount\tsmtp\thostname\t%s\n", b64
);
142 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_COMMAND
, buf
)) != PWMD_OK
) {
145 if (ret
== PWMD_ERROR
)
146 errx(EXIT_FAILURE
, "STORE: %s", strerror(error
));
148 errx(EXIT_FAILURE
, "STORE: %s", pwmd_strerror(error
));
151 fprintf(stderr
, "OK. Testing invalid command syntax...\n");
152 snprintf(buf
, sizeof(buf
), "GET newaccount\tnop\n");
154 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_COMMAND
, buf
)) != PWMD_OK
) {
155 if (ret
== PWMD_ERROR
) {
157 errx(EXIT_FAILURE
, "GET: %s", strerror(error
));
160 fprintf(stderr
, "OK, the error was: %s\n", pwmd_strerror(error
));
163 fprintf(stderr
, "OK. Creating another element...\n");
164 b64
= pwmd_base64_encode("25", 2);
165 snprintf(buf
, sizeof(buf
), "STORE newaccount\tsmtp\tport\t%s\n", b64
);
167 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_COMMAND
, buf
)) != PWMD_OK
) {
170 if (ret
== PWMD_ERROR
)
171 errx(EXIT_FAILURE
, "STORE: %s", strerror(error
));
173 errx(EXIT_FAILURE
, "STORE: %s", pwmd_strerror(error
));
176 fprintf(stderr
, "OK. Testing LIST for account list...\n");
177 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_COMMAND
, "LIST\n")) != PWMD_OK
) {
180 if (ret
== PWMD_ERROR
)
181 errx(EXIT_FAILURE
, "LIST: %s", strerror(error
));
183 errx(EXIT_FAILURE
, "LIST: %s", pwmd_strerror(error
));
188 while ((p
= strsep(&result
, "\n")))
189 fprintf(stderr
, "%s\n", p
);
193 fprintf(stderr
, "OK. Testing LIST for specific account 'newaccount'...\n");
194 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_COMMAND
, "LIST newaccount\n")) != PWMD_OK
) {
197 if (ret
== PWMD_ERROR
)
198 errx(EXIT_FAILURE
, "LIST: %s", strerror(error
));
200 errx(EXIT_FAILURE
, "LIST: %s", pwmd_strerror(error
));
205 while ((p
= strsep(&result
, "\n")))
206 fprintf(stderr
, "%s\n", p
);
209 fprintf(stderr
, "OK. GET'ing a single element from 'newaccount'...\n");
210 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_COMMAND
,
211 "GET newaccount\tsmtp\thostname\n")) != PWMD_OK
) {
214 if (ret
== PWMD_ERROR
)
215 errx(EXIT_FAILURE
, "GET: %s", strerror(error
));
217 errx(EXIT_FAILURE
, "GET: %s", pwmd_strerror(error
));
220 b64
= (char *)pwmd_base64_decode(result
, &len
);
221 fprintf(stderr
, "%s (decoded: '", result
);
222 fwrite(b64
, 1, len
, stderr
);
223 fprintf(stderr
, "')\n");
226 snprintf(buf
, sizeof(buf
), "ATTR SET name newaccount namechange\n");
227 fprintf(stderr
, "OK. Changing account 'newaccount' to 'namechange'...\n");
228 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_COMMAND
, buf
)) != PWMD_OK
) {
231 if (ret
== PWMD_ERROR
)
232 errx(EXIT_FAILURE
, "ATTR: %s", strerror(error
));
234 errx(EXIT_FAILURE
, "ATTR: %s", pwmd_strerror(error
));
237 fprintf(stderr
, "OK. see!\n");
238 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_COMMAND
, "LIST\n")) != PWMD_OK
) {
241 if (ret
== PWMD_ERROR
)
242 errx(EXIT_FAILURE
, "LIST: %s", strerror(error
));
244 errx(EXIT_FAILURE
, "LIST: %s", pwmd_strerror(error
));
249 while ((p
= strsep(&result
, "\n")))
250 fprintf(stderr
, "%s\n", p
);
254 fprintf(stderr
, "No really!\n");
255 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_COMMAND
, "LIST namechange\n")) != PWMD_OK
) {
258 if (ret
== PWMD_ERROR
)
259 errx(EXIT_FAILURE
, "LIST: %s", strerror(error
));
261 errx(EXIT_FAILURE
, "LIST: %s", pwmd_strerror(error
));
266 while ((p
= strsep(&result
, "\n")))
267 fprintf(stderr
, "%s\n", p
);
270 fprintf(stderr
, "OK. New pop3 tree (STORE)...\n");
271 b64
= pwmd_base64_encode("pophost.com", 11);
272 snprintf(buf
, sizeof(buf
), "STORE namechange\tpop3\thostname\t%s\n", b64
);
274 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_COMMAND
, buf
)) != PWMD_OK
) {
277 if (ret
== PWMD_ERROR
)
278 errx(EXIT_FAILURE
, "STORE: %s", strerror(error
));
280 errx(EXIT_FAILURE
, "STORE: %s", pwmd_strerror(error
));
283 fprintf(stderr
, "OK. Testing DELETE of 'namechange\\tsmtp'...\n");
284 snprintf(buf
, sizeof(buf
), "DELETE namechange\tsmtp\n");
285 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_COMMAND
, buf
)) != PWMD_OK
) {
288 if (ret
== PWMD_ERROR
)
289 errx(EXIT_FAILURE
, "DELETE: %s", strerror(error
));
291 errx(EXIT_FAILURE
, "DELETE: %s", pwmd_strerror(error
));
294 fprintf(stderr
, "OK. LIST of account 'namechange'...\n");
295 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_COMMAND
, "LIST namechange\n")) != PWMD_OK
) {
298 if (ret
== PWMD_ERROR
)
299 errx(EXIT_FAILURE
, "LIST: %s", strerror(error
));
301 errx(EXIT_FAILURE
, "LIST: %s", pwmd_strerror(error
));
306 while ((p
= strsep(&result
, "\n")))
307 fprintf(stderr
, "%s\n", p
);
310 fprintf(stderr
, "OK. ATTR failure...\n");
311 snprintf(buf
, sizeof(buf
), "ATTR GET someattr namechange\n");
312 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_COMMAND
, buf
)) != PWMD_OK
) {
313 if (ret
== PWMD_ERROR
)
314 warnx("ATTR: %s", strerror(error
));
316 warnx("ATTR: %s", pwmd_strerror(error
));
320 fprintf(stderr
, "Setting PWMD_OPTION_DESC...\n");
321 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_SETOPT
,
322 PWMD_OPTION_DESC
, "Enter the new password for file \"testfile\".")) != PWMD_OK
) {
325 if (ret
== PWMD_ERROR
)
326 errx(EXIT_FAILURE
, "PWMD_SETOPT: %s", strerror(error
));
328 errx(EXIT_FAILURE
, "PWMD_SETOPT: %s", pwmd_strerror(error
));
332 fprintf(stderr
, "OK. SAVE...\n");
333 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_SAVE
)) != PWMD_OK
) {
336 if (ret
== PWMD_ERROR
)
337 errx(EXIT_FAILURE
, "SAVE: %s", strerror(error
));
338 else if (ret
== PWMD_PERROR
)
339 errx(EXIT_FAILURE
, "SAVE: %s", pwmd_strerror(error
));
341 errx(EXIT_FAILURE
, "SAVE: gpg-agent failed. Is GPG_AGENT_INFO set?");
346 fprintf(stderr
, "Done! delete the file 'testfile' in your PWMD data directory\n");