Version 5.0.0.
[libpwmd.git] / pwmc.c
blobbb533258aa096cdc93ec7dc14404d3e53de47f90
1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
2 /*
3 Copyright (C) 2007-2008 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
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <err.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <libpwmd.h>
26 #include <assuan.h>
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
32 #ifdef HAVE_LOCALE_H
33 #include <locale.h>
34 #endif
36 #include "gettext.h"
37 #define N_(msgid) gettext(msgid)
39 #ifndef MEM_DEBUG
40 #include "mem.h"
41 #else
42 #define xfree free
43 #define xrealloc realloc
44 #define xmalloc malloc
45 #define xstrdup strdup
46 #define xcalloc calloc
47 #endif
49 pwm_t *pwm;
51 static void show_error(gpg_error_t error)
53 fprintf(stderr, "ERR %i %s\n", gpg_err_code(error), pwmd_strerror(error));
56 static void usage(const char *pn)
58 fprintf(stderr, N_(
59 "Reads PWMD protocol commands from standard input.\n\n"
60 "Usage: pwmc [-hvX] [-s <socket>] [-P -T -N -D | -p <password>] [-S] \n"
61 " [-c <name>] [-d <fd>] [-I <fd>] [filename]\n"
62 " -X disable showing of status messages from the server\n"
63 " -c set the client name\n"
64 " -s socket path (~/.pwmd/socket)\n"
65 " -p password\n"
66 " -P path to the pinentry binary (server default)\n"
67 " -T pinentry tty\n"
68 " -N pinentry terminal type\n"
69 " -D pinentry display\n"
70 " -d redirect command output to the specified file descriptor\n"
71 " -I read inquire data from the specified file descriptor\n"
72 " -S send the SAVE command before exiting\n"
73 " -v version\n"
74 " -h this help text\n"));
75 exit(EXIT_FAILURE);
78 struct inquire_s {
79 FILE *fp;
80 char *data;
83 static gpg_error_t do_inquire(void *data, const char *keyword, gpg_error_t rc,
84 char **result, size_t *result_len)
86 int c;
87 static char buf[ASSUAN_LINELENGTH];
88 char *p;
89 size_t len = 0;
90 struct inquire_s *inq = (struct inquire_s *)data;
92 if (rc) {
93 memset(buf, 0, sizeof(buf));
94 return rc;
97 buf[0] = 0;
98 p = buf;
100 if (inq->data) {
101 snprintf(buf, sizeof(buf), "%s", inq->data);
102 xfree(inq->data);
103 inq->data = NULL;
104 len = strlen(buf);
105 p = buf + len;
108 while ((c = fgetc(inq->fp)) != EOF) {
109 if (len == sizeof(buf)) {
110 ungetc(c, inq->fp);
111 break;
114 *p++ = c;
115 len++;
118 if (!buf[0]) {
119 memset(buf, 0, sizeof(buf));
120 return GPG_ERR_EOF;
123 *result = buf;
124 *result_len = len;
125 return 0;
128 static int status_msg_cb(void *data, const char *line)
130 fprintf(stderr, "%s\n", line);
131 return 0;
134 int main(int argc, char *argv[])
136 int opt;
137 char *password = NULL;
138 char *filename = NULL;
139 char *socketpath = NULL;
140 char command[ASSUAN_LINELENGTH], *p;
141 int ret = EXIT_SUCCESS;
142 gpg_error_t error;
143 char *result = NULL;
144 int save = 0;
145 char *pinentry_path = NULL;
146 char *display = NULL, *tty = NULL, *ttytype = NULL;
147 int outfd = STDOUT_FILENO;
148 FILE *outfp = stdout;
149 int inquirefd = STDIN_FILENO;
150 FILE *inquirefp = stdin;
151 int show_status = 1;
152 char *clientname = NULL;
154 setlocale(LC_ALL, "");
155 bindtextdomain("libpwmd", LOCALEDIR);
157 while ((opt = getopt(argc, argv, "c:I:XT:N:D:hvP:p:s:Sd:")) != EOF) {
158 switch (opt) {
159 case 'c':
160 clientname = xstrdup(optarg);
161 break;
162 case 'X':
163 show_status = 0;
164 break;
165 case 'T':
166 tty = optarg;
167 break;
168 case 'N':
169 ttytype = optarg;
170 break;
171 case 'D':
172 display = optarg;
173 break;
174 case 'I':
175 inquirefd = atoi(optarg);
176 inquirefp = fdopen(inquirefd, "r");
178 if (!inquirefp) {
179 xfree(password);
180 err(EXIT_FAILURE, "%i", inquirefd);
182 break;
183 case 'd':
184 outfd = atoi(optarg);
185 outfp = fdopen(outfd, "w");
187 if (!outfp) {
188 xfree(password);
189 err(EXIT_FAILURE, "%i", outfd);
191 break;
192 case 'S':
193 save = 1;
194 break;
195 case 's':
196 socketpath = xstrdup(optarg);
197 break;
198 case 'p':
199 password = xstrdup(optarg);
200 memset(optarg, 0, strlen(optarg));
201 break;
202 case 'P':
203 pinentry_path = xstrdup(optarg);
204 break;
205 case 'v':
206 xfree(password);
207 printf("%s (pwmc)\n%s\n", PACKAGE_STRING, PACKAGE_BUGREPORT);
208 exit(EXIT_SUCCESS);
209 case 'h':
210 default:
211 xfree(password);
212 usage(argv[0]);
216 filename = argv[optind];
217 pwmd_init();
219 if ((pwm = pwmd_connect(socketpath, &error)) == NULL) {
220 xfree(password);
221 errx(EXIT_FAILURE, "pwmd_connect(): %s", pwmd_strerror(error));
224 error = pwmd_command(pwm, &result, "OPTION CLIENT NAME=%s", clientname ? clientname : "pwmc");
225 xfree(clientname);
227 if (error) {
228 xfree(password);
229 errx(EXIT_FAILURE, "pwmd_connect(): %s", pwmd_strerror(error));
232 if (password) {
233 error = pwmd_setopt(pwm, PWMD_OPTION_PASSWORD, password);
235 if (error) {
236 xfree(password);
237 goto done;
240 xfree(password);
242 else {
243 if (pinentry_path) {
244 error = pwmd_command(pwm, &result, "OPTION PATH=%s", pinentry_path);
246 if (error)
247 goto done;
250 if (display) {
251 error = pwmd_command(pwm, &result, "OPTION DISPLAY=%s", display);
253 if (error)
254 goto done;
257 if (tty) {
258 error = pwmd_command(pwm, &result, "OPTION TTYNAME=%s", tty);
260 if (error)
261 goto done;
264 if (ttytype) {
265 error = pwmd_command(pwm, &result, "OPTION TTYTYPE=%s", ttytype);
267 if (error)
268 goto done;
272 if (show_status) {
273 error = pwmd_setopt(pwm, PWMD_OPTION_STATUS_FUNC, status_msg_cb);
275 if (error)
276 goto done;
279 if (filename) {
280 error = pwmd_open(pwm, filename);
282 if (error)
283 goto done;
286 p = fgets(command, sizeof(command), stdin);
288 if (!p)
289 goto done;
292 * This is a known INQUIRE command. We use pwmd_inquire() to send the
293 * data from the do_inquire() callback function.
295 if (strncasecmp(p, "STORE ", 6) == 0) {
296 struct inquire_s *inq = (struct inquire_s *)malloc(sizeof(struct inquire_s));
298 if (!inq) {
299 error = gpg_error_from_errno(ENOMEM);
300 goto done;
303 inq->data = xstrdup(p+6);
304 inq->fp = inquirefp;
305 error = pwmd_inquire(pwm, "STORE", do_inquire, inq);
306 free(inq);
307 goto done;
310 if (strcasecmp(p, "BYE") == 0)
311 goto done;
313 error = pwmd_command(pwm, &result, command);
314 memset(command, 0, sizeof(command));
316 if (error)
317 goto done;
319 if (result) {
320 fwrite(result, 1, strlen(result), outfp);
321 pwmd_free_result(result);
324 done:
325 memset(command, 0, sizeof(command));
327 if (!error && save)
328 error = pwmd_save(pwm);
330 if (error) {
331 show_error(error);
332 ret = EXIT_FAILURE;
335 pwmd_close(pwm);
337 if (socketpath)
338 xfree(socketpath);
340 exit(ret);