1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
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
37 #define N_(msgid) gettext(msgid)
43 #define xrealloc realloc
44 #define xmalloc malloc
45 #define xstrdup strdup
46 #define xcalloc calloc
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
)
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"
66 " -P path to the pinentry binary (server default)\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"
74 " -h this help text\n"));
83 static gpg_error_t
do_inquire(void *data
, const char *keyword
, gpg_error_t rc
,
84 char **result
, size_t *result_len
)
87 static char buf
[ASSUAN_LINELENGTH
];
90 struct inquire_s
*inq
= (struct inquire_s
*)data
;
93 memset(buf
, 0, sizeof(buf
));
101 snprintf(buf
, sizeof(buf
), "%s", inq
->data
);
108 while ((c
= fgetc(inq
->fp
)) != EOF
) {
109 if (len
== sizeof(buf
)) {
119 memset(buf
, 0, sizeof(buf
));
128 static int status_msg_cb(void *data
, const char *line
)
130 fprintf(stderr
, "%s\n", line
);
134 int main(int argc
, char *argv
[])
137 char *password
= NULL
;
138 char *filename
= NULL
;
139 char *socketpath
= NULL
;
140 char command
[ASSUAN_LINELENGTH
], *p
;
141 int ret
= EXIT_SUCCESS
;
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
;
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
) {
160 clientname
= xstrdup(optarg
);
175 inquirefd
= atoi(optarg
);
176 inquirefp
= fdopen(inquirefd
, "r");
180 err(EXIT_FAILURE
, "%i", inquirefd
);
184 outfd
= atoi(optarg
);
185 outfp
= fdopen(outfd
, "w");
189 err(EXIT_FAILURE
, "%i", outfd
);
196 socketpath
= xstrdup(optarg
);
199 password
= xstrdup(optarg
);
200 memset(optarg
, 0, strlen(optarg
));
203 pinentry_path
= xstrdup(optarg
);
207 printf("%s (pwmc)\n%s\n", PACKAGE_STRING
, PACKAGE_BUGREPORT
);
216 filename
= argv
[optind
];
219 if ((pwm
= pwmd_connect(socketpath
, &error
)) == NULL
) {
221 errx(EXIT_FAILURE
, "pwmd_connect(): %s", pwmd_strerror(error
));
224 error
= pwmd_command(pwm
, &result
, "OPTION CLIENT NAME=%s", clientname
? clientname
: "pwmc");
229 errx(EXIT_FAILURE
, "pwmd_connect(): %s", pwmd_strerror(error
));
233 error
= pwmd_setopt(pwm
, PWMD_OPTION_PASSWORD
, password
);
244 error
= pwmd_command(pwm
, &result
, "OPTION PATH=%s", pinentry_path
);
251 error
= pwmd_command(pwm
, &result
, "OPTION DISPLAY=%s", display
);
258 error
= pwmd_command(pwm
, &result
, "OPTION TTYNAME=%s", tty
);
265 error
= pwmd_command(pwm
, &result
, "OPTION TTYTYPE=%s", ttytype
);
273 error
= pwmd_setopt(pwm
, PWMD_OPTION_STATUS_FUNC
, status_msg_cb
);
280 error
= pwmd_open(pwm
, filename
);
286 p
= fgets(command
, sizeof(command
), stdin
);
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
));
299 error
= gpg_error_from_errno(ENOMEM
);
303 inq
->data
= xstrdup(p
+6);
305 error
= pwmd_inquire(pwm
, "STORE", do_inquire
, inq
);
310 if (strcasecmp(p
, "BYE") == 0)
313 error
= pwmd_command(pwm
, &result
, command
);
314 memset(command
, 0, sizeof(command
));
320 fwrite(result
, 1, strlen(result
), outfp
);
321 pwmd_free_result(result
);
325 memset(command
, 0, sizeof(command
));
328 error
= pwmd_save(pwm
);