Added pwmc.1.
[pwmd.git] / libpwmd / pwmc.c
blob597f3e0413b37134839505b0d4faa810fcdabb4b
1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
2 /*
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
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>
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
31 static void usage(const char *pn)
33 fprintf(stderr,
34 "Usage: %s [-hv] [-E] [-s <socket>] [-a | -p <password>] [-S] <filename>\n"
35 " -E exit after a command failure\n"
36 " -s socket path\n"
37 " -p password\n"
38 " -a use gpg-agent\n"
39 " -S send the SAVE command after all others\n"
40 " -v version\n"
41 " -h this help text\n",
42 pn);
43 exit(EXIT_FAILURE);
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));
52 else
53 fprintf(stderr, "ERR 000 Is gpg-agent running? Is GPG_AGENT_INFO set?\n");
56 int main(int argc, char *argv[])
58 int opt;
59 int use_agent = 0;
60 char *password = NULL;
61 char *filename = NULL;
62 char *socketpath = NULL;
63 char command[8196], *p;
64 int ret, error;
65 pwm_t *pwm;
66 char *result = NULL;
67 int save = 0;
68 char *buf = NULL;
69 int total = 0;
70 int do_exit = 0;
72 while ((opt = getopt(argc, argv, "Ehvap:s:S")) != EOF) {
73 switch (opt) {
74 case 'E':
75 do_exit = 1;
76 break;
77 case 'S':
78 save = 1;
79 break;
80 case 's':
81 socketpath = strdup(optarg);
82 break;
83 case 'p':
84 password = strdup(optarg);
85 break;
86 case 'a':
87 use_agent = 1;
88 break;
89 case 'v':
90 printf("%s (pwmc)\n%s\n", PACKAGE_STRING, PACKAGE_BUGREPORT);
91 exit(EXIT_SUCCESS);
92 case 'h':
93 default:
94 usage(argv[0]);
98 if (!use_agent && !password)
99 usage(argv[0]);
101 if (use_agent && password)
102 usage(argv[0]);
104 if (argc - optind != 1)
105 usage(argv[0]);
107 filename = argv[optind];
109 if ((pwm = pwmd_connect(socketpath, &error)) == NULL) {
110 errno = error;
112 if (password) {
113 memset(password, 0, strlen(password));
114 free(password);
117 err(EXIT_FAILURE, "pwmd_connect()");
120 if (use_agent) {
121 if ((ret = pwmd_command(pwm, &result, &error, PWMD_SETOPT, PWMD_OPTION_USEAGENT, 1))
122 != PWMD_OK) {
123 pwmd_show_error(ret, error);
124 pwmd_close(pwm);
125 exit(EXIT_FAILURE);
128 else {
129 if ((ret = pwmd_command(pwm, &result, &error, PWMD_SETOPT,
130 PWMD_OPTION_PASSWORD, password)) != PWMD_OK) {
131 memset(password, 0, strlen(password));
132 free(password);
133 pwmd_show_error(ret, error);
134 pwmd_close(pwm);
135 exit(EXIT_FAILURE);
138 memset(password, 0, strlen(password));
139 free(password);
142 if ((ret = pwmd_command(pwm, &result, &error, PWMD_OPEN, filename)) != PWMD_OK) {
143 pwmd_show_error(ret, error);
144 pwmd_close(pwm);
145 exit(EXIT_FAILURE);
148 while ((p = fgets(command, sizeof(command), stdin)) != NULL) {
149 int len = strlen(p);
150 char *t;
152 if (p[len - 1] != '\n') {
153 if ((t = realloc(buf, (total + len + 1) * sizeof(char))) == NULL) {
154 if (buf) {
155 memset(buf, 0, total);
156 free(buf);
159 err(EXIT_FAILURE, "realloc()");
162 buf = t;
163 memcpy(&buf[total], p, len);
164 total += len;
165 buf[total] = 0;
166 continue;
168 else {
169 if (buf) {
170 if ((t = realloc(buf, (total + len + 1) * sizeof(char))) == NULL) {
171 if (buf) {
172 memset(buf, 0, total);
173 free(buf);
176 err(EXIT_FAILURE, "realloc()");
179 buf = t;
180 memcpy(&buf[total], p, len);
181 total += len;
182 buf[total] = 0;
186 if ((ret = pwmd_command(pwm, &result, &error, PWMD_COMMAND,
187 (buf) ? buf : command)) != PWMD_OK) {
188 if (buf) {
189 memset(buf, 0, total);
190 free(buf);
193 memset(&command, 0, sizeof(command));
194 pwmd_show_error(ret, error);
196 if (do_exit) {
197 pwmd_close(pwm);
198 exit(EXIT_FAILURE);
202 if (buf) {
203 memset(buf, 0, total);
204 free(buf);
205 buf = NULL;
206 total = 0;
209 if (result) {
210 printf("%s\n", (char *)result);
211 free(result);
215 if (save) {
216 if ((ret = pwmd_command(pwm, &result, &error, PWMD_SAVE)) != PWMD_OK) {
217 memset(&command, 0, sizeof(command));
218 pwmd_show_error(ret, error);
219 pwmd_close(pwm);
220 exit(EXIT_FAILURE);
224 pwmd_close(pwm);
226 if (socketpath)
227 free(socketpath);
229 exit(EXIT_SUCCESS);