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
;
41 while ((opt
= getopt(argc
, argv
, "hs:a")) != EOF
) {
51 fprintf(stderr
, "Usage: %s [-h] [-a] [-s <socketpath>]\n"
53 " -s specify an alternate socket path\n"
54 " -h this help text\n", argv
[0]);
59 fprintf(stderr
, "Connecting to socket...\n");
60 if ((pwm
= pwmd_connect(socketpath
, &error
)) == NULL
)
61 errx(EXIT_FAILURE
, "pwmd_connect(): %s", strerror(error
));
63 fprintf(stderr
, "Connected. fd=%i\n", pwm
->fd
);
66 fprintf(stderr
, "Setting password 'test'...\n");
68 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_SETOPT
,
69 PWMD_OPTION_PASSWORD
, "test")) != PWMD_OK
) {
72 if (ret
== PWMD_ERROR
)
73 errx(EXIT_FAILURE
, "PWMD_SETOPT: %s", strerror(error
));
75 errx(EXIT_FAILURE
, "PWMD_SETOPT: %s", pwmd_strerror(error
));
79 fprintf(stderr
, "Setting PWMD_OPTION_USEAGENT...\n");
80 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_SETOPT
,
81 PWMD_OPTION_USEAGENT
, 1)) != PWMD_OK
) {
84 if (ret
== PWMD_ERROR
)
85 errx(EXIT_FAILURE
, "PWMD_SETOPT: %s", strerror(error
));
87 errx(EXIT_FAILURE
, "PWMD_SETOPT: %s", pwmd_strerror(error
));
90 fprintf(stderr
, "Setting PWMD_OPTION_TITLE...\n");
91 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_SETOPT
,
92 PWMD_OPTION_TITLE
, "PWMD Client Test")) != PWMD_OK
) {
95 if (ret
== PWMD_ERROR
)
96 errx(EXIT_FAILURE
, "PWMD_SETOPT: %s", strerror(error
));
98 errx(EXIT_FAILURE
, "PWMD_SETOPT: %s", pwmd_strerror(error
));
101 fprintf(stderr
, "Setting PWMD_OPTION_PROMPT...\n");
102 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_SETOPT
,
103 PWMD_OPTION_PROMPT
, "Password please:")) != PWMD_OK
) {
106 if (ret
== PWMD_ERROR
)
107 errx(EXIT_FAILURE
, "PWMD_SETOPT: %s", strerror(error
));
109 errx(EXIT_FAILURE
, "PWMD_SETOPT: %s", pwmd_strerror(error
));
112 fprintf(stderr
, "Setting PWMD_OPTION_DESC...\n");
113 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_SETOPT
,
114 PWMD_OPTION_DESC
, "Enter the password for file \"testfile\".")) != PWMD_OK
) {
117 if (ret
== PWMD_ERROR
)
118 errx(EXIT_FAILURE
, "PWMD_SETOPT: %s", strerror(error
));
120 errx(EXIT_FAILURE
, "PWMD_SETOPT: %s", pwmd_strerror(error
));
124 fprintf(stderr
, "Opening 'testfile'...\n");
125 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_OPEN
, "testfile")) != PWMD_OK
) {
128 if (ret
== PWMD_ERROR
)
129 errx(EXIT_FAILURE
, "PWMD_OPEN: %s", strerror(error
));
130 else if (ret
== PWMD_PERROR
)
131 errx(EXIT_FAILURE
, "PWMD_OPEN: %s", pwmd_strerror(error
));
133 errx(EXIT_FAILURE
, "PWMD_OPEN: gpg-agent failed. Is GPG_AGENT_INFO set?");
136 fprintf(stderr
, "File opened. Creating 'newaccount' and test element...\n");
137 snprintf(buf
, sizeof(buf
), "STORE newaccount\tsmtp\thostname\tsomehost.com\n");
139 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_COMMAND
, buf
)) != PWMD_OK
) {
142 if (ret
== PWMD_ERROR
)
143 errx(EXIT_FAILURE
, "STORE: %s", strerror(error
));
145 errx(EXIT_FAILURE
, "STORE: %s", pwmd_strerror(error
));
148 fprintf(stderr
, "OK. Testing invalid command syntax...\n");
149 snprintf(buf
, sizeof(buf
), "GET newaccount\tnop\n");
151 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_COMMAND
, buf
)) != PWMD_OK
) {
152 if (ret
== PWMD_ERROR
) {
154 errx(EXIT_FAILURE
, "GET: %s", strerror(error
));
157 fprintf(stderr
, "OK, the error was: %s\n", pwmd_strerror(error
));
160 fprintf(stderr
, "OK. Creating another element...\n");
161 snprintf(buf
, sizeof(buf
), "STORE newaccount\tsmtp\tport\t25\n");
162 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_COMMAND
, buf
)) != PWMD_OK
) {
165 if (ret
== PWMD_ERROR
)
166 errx(EXIT_FAILURE
, "STORE: %s", strerror(error
));
168 errx(EXIT_FAILURE
, "STORE: %s", pwmd_strerror(error
));
171 fprintf(stderr
, "OK. Testing LIST for account list...\n");
172 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_COMMAND
, "LIST\n")) != PWMD_OK
) {
175 if (ret
== PWMD_ERROR
)
176 errx(EXIT_FAILURE
, "LIST: %s", strerror(error
));
178 errx(EXIT_FAILURE
, "LIST: %s", pwmd_strerror(error
));
183 while ((p
= strsep(&result
, "\n")))
184 fprintf(stderr
, "%s\n", p
);
188 fprintf(stderr
, "OK. Testing LIST for specific account 'newaccount'...\n");
189 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_COMMAND
, "LIST newaccount\n")) != PWMD_OK
) {
192 if (ret
== PWMD_ERROR
)
193 errx(EXIT_FAILURE
, "LIST: %s", strerror(error
));
195 errx(EXIT_FAILURE
, "LIST: %s", pwmd_strerror(error
));
200 while ((p
= strsep(&result
, "\n")))
201 fprintf(stderr
, "%s\n", p
);
204 fprintf(stderr
, "OK. GET'ing a single element from 'newaccount'...\n");
205 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_COMMAND
,
206 "GET newaccount\tsmtp\thostname\n")) != PWMD_OK
) {
209 if (ret
== PWMD_ERROR
)
210 errx(EXIT_FAILURE
, "GET: %s", strerror(error
));
212 errx(EXIT_FAILURE
, "GET: %s", pwmd_strerror(error
));
215 fprintf(stderr
, "%s\n", result
);
217 snprintf(buf
, sizeof(buf
), "ATTR SET name newaccount namechange\n");
218 fprintf(stderr
, "OK. Changing account 'newaccount' to 'namechange'...\n");
219 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_COMMAND
, buf
)) != PWMD_OK
) {
222 if (ret
== PWMD_ERROR
)
223 errx(EXIT_FAILURE
, "ATTR: %s", strerror(error
));
225 errx(EXIT_FAILURE
, "ATTR: %s", pwmd_strerror(error
));
228 fprintf(stderr
, "OK. see!\n");
229 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_COMMAND
, "LIST\n")) != PWMD_OK
) {
232 if (ret
== PWMD_ERROR
)
233 errx(EXIT_FAILURE
, "LIST: %s", strerror(error
));
235 errx(EXIT_FAILURE
, "LIST: %s", pwmd_strerror(error
));
240 while ((p
= strsep(&result
, "\n")))
241 fprintf(stderr
, "%s\n", p
);
245 fprintf(stderr
, "No really!\n");
246 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_COMMAND
, "LIST namechange\n")) != PWMD_OK
) {
249 if (ret
== PWMD_ERROR
)
250 errx(EXIT_FAILURE
, "LIST: %s", strerror(error
));
252 errx(EXIT_FAILURE
, "LIST: %s", pwmd_strerror(error
));
257 while ((p
= strsep(&result
, "\n")))
258 fprintf(stderr
, "%s\n", p
);
261 fprintf(stderr
, "OK. New pop3 tree (STORE)...\n");
262 snprintf(buf
, sizeof(buf
), "STORE namechange\tpop3\thostname\tpophost.com\n");
263 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_COMMAND
, buf
)) != PWMD_OK
) {
266 if (ret
== PWMD_ERROR
)
267 errx(EXIT_FAILURE
, "STORE: %s", strerror(error
));
269 errx(EXIT_FAILURE
, "STORE: %s", pwmd_strerror(error
));
272 fprintf(stderr
, "OK. Testing DELETE of 'namechange\\tsmtp'...\n");
273 snprintf(buf
, sizeof(buf
), "DELETE namechange\tsmtp\n");
274 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_COMMAND
, buf
)) != PWMD_OK
) {
277 if (ret
== PWMD_ERROR
)
278 errx(EXIT_FAILURE
, "DELETE: %s", strerror(error
));
280 errx(EXIT_FAILURE
, "DELETE: %s", pwmd_strerror(error
));
283 fprintf(stderr
, "OK. LIST of account 'namechange'...\n");
284 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_COMMAND
, "LIST namechange\n")) != PWMD_OK
) {
287 if (ret
== PWMD_ERROR
)
288 errx(EXIT_FAILURE
, "LIST: %s", strerror(error
));
290 errx(EXIT_FAILURE
, "LIST: %s", pwmd_strerror(error
));
295 while ((p
= strsep(&result
, "\n")))
296 fprintf(stderr
, "%s\n", p
);
299 fprintf(stderr
, "OK. ATTR failure...\n");
300 snprintf(buf
, sizeof(buf
), "ATTR GET someattr namechange\n");
301 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_COMMAND
, buf
)) != PWMD_OK
) {
302 if (ret
== PWMD_ERROR
)
303 warnx("ATTR: %s", strerror(error
));
305 warnx("ATTR: %s", pwmd_strerror(error
));
309 fprintf(stderr
, "Setting PWMD_OPTION_DESC...\n");
310 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_SETOPT
,
311 PWMD_OPTION_DESC
, "Enter the new password for file \"testfile\".")) != PWMD_OK
) {
314 if (ret
== PWMD_ERROR
)
315 errx(EXIT_FAILURE
, "PWMD_SETOPT: %s", strerror(error
));
317 errx(EXIT_FAILURE
, "PWMD_SETOPT: %s", pwmd_strerror(error
));
321 fprintf(stderr
, "OK. SAVE...\n");
322 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_SAVE
)) != PWMD_OK
) {
325 if (ret
== PWMD_ERROR
)
326 errx(EXIT_FAILURE
, "SAVE: %s", strerror(error
));
327 else if (ret
== PWMD_PERROR
)
328 errx(EXIT_FAILURE
, "SAVE: %s", pwmd_strerror(error
));
330 errx(EXIT_FAILURE
, "SAVE: gpg-agent failed. Is GPG_AGENT_INFO set?");
335 fprintf(stderr
, "Done! delete the file 'testfile' in your PWMD data directory\n");