From 49197f39fe724e5b742869901d594f2858605a1c Mon Sep 17 00:00:00 2001 From: Ben Kibbey Date: Tue, 16 Jan 2007 22:45:20 -0500 Subject: [PATCH] Added a simpler client than example client. It's more of a utility. It reads stdin for protocol commands and prints the result (if any). Installs to PREFIX/bin. --- libpwmd/Makefile.am | 4 ++ libpwmd/pwmc.c | 171 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 175 insertions(+) create mode 100644 libpwmd/pwmc.c diff --git a/libpwmd/Makefile.am b/libpwmd/Makefile.am index a6339280..ba9e1336 100644 --- a/libpwmd/Makefile.am +++ b/libpwmd/Makefile.am @@ -11,5 +11,9 @@ exampleclient_SOURCES = exampleclient.c exampleclient_LDADD= -lpwmd exampleclient_LDFLAGS = -static +bin_PROGRAMS = pwmc +pwmc_SOURCES = pwmc.c +pwmc_LDADD= -lpwmd + changelog: git-log -- . > ChangeLog || exit 1 diff --git a/libpwmd/pwmc.c b/libpwmd/pwmc.c new file mode 100644 index 00000000..5c398a1d --- /dev/null +++ b/libpwmd/pwmc.c @@ -0,0 +1,171 @@ +/* vim:tw=78:ts=8:sw=4:set ft=c: */ +/* + Copyright (C) 2007 Ben Kibbey + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ +#include +#include +#include +#include +#include +#include +#include + +#ifdef HAVE_CONFIG_H +#include +#endif + +static void usage(const char *pn) +{ + fprintf(stderr, + "Usage: %s [-hv] [-s ] [-a | -p ] [-S] \n" + " -s socket path\n" + " -p password\n" + " -a use gpg-agent\n" + " -S send the SAVE command after all others\n" + " -v version\n" + " -h this help text\n", + pn); + exit(EXIT_FAILURE); +} + +static void pwmd_show_error(int ret, int error) +{ + if (ret == PWMD_PERROR) + warnx("%s", pwmd_strerror(error)); + else if (ret == PWMD_ERROR) + warnx("%s", strerror(error)); + else + warnx("Is gpg-agent running? Is GPG_AGENT_INFO set?"); +} + +int main(int argc, char *argv[]) +{ + int opt; + int use_agent = 0; + char *password = NULL; + char *filename = NULL; + char *socketpath = NULL; + char command[8196], *p; + int ret, error; + pwm_t *pwm; + void *result = NULL; + int save = 0; + + while ((opt = getopt(argc, argv, "hvap:s:S")) != EOF) { + switch (opt) { + case 'S': + save = 1; + break; + case 's': + socketpath = strdup(optarg); + break; + case 'p': + password = strdup(optarg); + break; + case 'a': + use_agent = 1; + break; + case 'v': + printf("%s (pwmc)\n%s\n", PACKAGE_STRING, PACKAGE_BUGREPORT); + exit(EXIT_SUCCESS); + case 'h': + default: + usage(argv[0]); + } + } + + if (!use_agent && !password) + usage(argv[0]); + + if (use_agent && password) + usage(argv[0]); + + if (argc - optind != 1) + usage(argv[0]); + + filename = argv[optind]; + + if ((pwm = pwmd_connect(socketpath, &error)) == NULL) { + errno = error; + + if (password) { + memset(password, 0, strlen(password)); + free(password); + } + + err(EXIT_FAILURE, "pwmd_connect()"); + } + + if (use_agent) { + if ((ret = pwmd_command(pwm, &result, &error, PWMD_SETOPT, PWMD_OPTION_USEAGENT, 1)) + != PWMD_OK) { + pwmd_show_error(ret, error); + pwmd_close(pwm); + exit(EXIT_FAILURE); + } + } + else { + if ((ret = pwmd_command(pwm, &result, &error, PWMD_SETOPT, + PWMD_OPTION_PASSWORD, password)) != PWMD_OK) { + memset(password, 0, strlen(password)); + free(password); + pwmd_show_error(ret, error); + pwmd_close(pwm); + exit(EXIT_FAILURE); + } + + memset(password, 0, strlen(password)); + free(password); + } + + if ((ret = pwmd_command(pwm, &result, &error, PWMD_OPEN, filename)) != PWMD_OK) { + memset(&command, 0, sizeof(command)); + pwmd_show_error(ret, error); + pwmd_close(pwm); + exit(EXIT_FAILURE); + } + + while ((p = fgets(command, sizeof(command), stdin)) != NULL) { + if ((ret = pwmd_command(pwm, &result, &error, PWMD_RAW, command)) != PWMD_OK) { + memset(&command, 0, sizeof(command)); + pwmd_show_error(ret, error); + pwmd_close(pwm); + exit(EXIT_FAILURE); + } + + if (result) { + printf("%s", (char *)result); + free(result); + } + } + + if (save) { + if ((ret = pwmd_command(pwm, &result, &error, PWMD_SAVE)) != PWMD_OK) { + memset(&command, 0, sizeof(command)); + pwmd_show_error(ret, error); + pwmd_close(pwm); + exit(EXIT_FAILURE); + } + } + + pwmd_close(pwm); + + if (socketpath) + free(socketpath); + + exit(EXIT_SUCCESS); +} -- 2.11.4.GIT