summaryrefslogtreecommitdiff
path: root/client
diff options
context:
space:
mode:
authorRuss Allbery <rra@stanford.edu>2007-10-05 02:15:27 +0000
committerRuss Allbery <rra@stanford.edu>2007-10-05 02:15:27 +0000
commitf21fba65f194ff26bf72e23f0db311314529720b (patch)
treece454a33292244ad130962ab9da7f667fe57d1ec /client
parentfe56c09a7feeb2d1c9cd699fda07e145c4c354a2 (diff)
Refactor the remctl calls in the wallet client to share a common routine.
Diffstat (limited to 'client')
-rw-r--r--client/internal.h8
-rw-r--r--client/keytab.c36
-rw-r--r--client/remctl.c74
-rw-r--r--client/wallet.c29
4 files changed, 88 insertions, 59 deletions
diff --git a/client/internal.h b/client/internal.h
index 092240e..960554e 100644
--- a/client/internal.h
+++ b/client/internal.h
@@ -27,6 +27,14 @@ struct remctl;
BEGIN_DECLS
+/* Given a remctl object, run a remctl command. If data is non-NULL, saves
+ the standard output from the command into data with the length in length.
+ Otherwise, prints it to standard output. Either way, prints standard error
+ output and errors to standard error and returns the exit status or 255 for
+ a remctl internal error. */
+int run_command(struct remctl *, const char **command, char **data,
+ size_t *length);
+
/* Given a remctl object, the type for the wallet interface, the name of a
keytab object, and a file name, call the correct wallet commands to
download a keytab and write it to that file. */
diff --git a/client/keytab.c b/client/keytab.c
index 3450766..b815e4a 100644
--- a/client/keytab.c
+++ b/client/keytab.c
@@ -26,49 +26,19 @@ get_keytab(struct remctl *r, const char *type, const char *name,
const char *file)
{
const char *command[5];
- struct remctl_output *output;
char *data = NULL;
size_t length = 0;
int status = 255;
- /* Run the command on the wallet server */
command[0] = type;
command[1] = "get";
command[2] = "keytab";
command[3] = name;
command[4] = NULL;
- if (!remctl_command(r, command))
- die("%s", remctl_error(r));
-
- /* Retrieve the results. */
- do {
- output = remctl_output(r);
- switch (output->type) {
- case REMCTL_OUT_OUTPUT:
- if (output->stream == 1) {
- data = xrealloc(data, length + output->length);
- memcpy(data + length, output->data, output->length);
- length += output->length;
- } else {
- fprintf(stderr, "wallet: ");
- fwrite(output->data, 1, output->length, stderr);
- }
- break;
- case REMCTL_OUT_STATUS:
- status = output->status;
- break;
- case REMCTL_OUT_ERROR:
- fprintf(stderr, "wallet: ");
- fwrite(output->data, 1, output->length, stderr);
- fputc('\n', stderr);
- exit(255);
- case REMCTL_OUT_DONE:
- break;
- }
- } while (output->type != REMCTL_OUT_DONE);
+ status = run_command(r, command, &data, &length);
if (status != 0)
exit(status);
-
- /* Okay, we now have the valid keytab data in data. Write it out. */
+ if (data == NULL)
+ die("no data returned by wallet server");
write_file(file, data, length);
}
diff --git a/client/remctl.c b/client/remctl.c
new file mode 100644
index 0000000..089d7f4
--- /dev/null
+++ b/client/remctl.c
@@ -0,0 +1,74 @@
+/* $Id$
+**
+** remctl interface for the wallet client.
+**
+** Written by Russ Allbery <rra@stanford.edu>
+** Copyright 2007 Board of Trustees, Leland Stanford Jr. University
+**
+** See README for licensing terms.
+*/
+
+#include <config.h>
+#include <system.h>
+
+#include <remctl.h>
+
+#include <client/internal.h>
+#include <util/util.h>
+
+
+/*
+** Given a remctl connection and a command, run the command.
+**
+** If data is non-NULL, save the output in it and return the length in
+** length. Otherwise, send any output to stdout. Either way, send error
+** output to stderr, and return the exit status (or 255 if there is an
+** error).
+*/
+int
+run_command(struct remctl *r, const char **command, char **data,
+ size_t *length)
+{
+ struct remctl_output *output;
+ int status = 255;
+
+ if (data != NULL)
+ *data = NULL;
+ if (length != NULL)
+ *length = 0;
+ if (!remctl_command(r, command)) {
+ warn("%s", remctl_error(r));
+ return 255;
+ }
+ do {
+ output = remctl_output(r);
+ switch (output->type) {
+ case REMCTL_OUT_OUTPUT:
+ if (output->stream == 1) {
+ if (data != NULL) {
+ *data = xrealloc(*data, *length + output->length);
+ memcpy(*data + *length, output->data, output->length);
+ *length += output->length;
+ } else {
+ fwrite(output->data, 1, output->length, stdout);
+ }
+ } else {
+ fprintf(stderr, "wallet: ");
+ fwrite(output->data, 1, output->length, stderr);
+ }
+ break;
+ case REMCTL_OUT_STATUS:
+ status = output->status;
+ break;
+ case REMCTL_OUT_ERROR:
+ fprintf(stderr, "wallet: ");
+ fwrite(output->data, 1, output->length, stderr);
+ fputc('\n', stderr);
+ status = 255;
+ break;
+ case REMCTL_OUT_DONE:
+ break;
+ }
+ } while (output->type != REMCTL_OUT_DONE);
+ return status;
+}
diff --git a/client/wallet.c b/client/wallet.c
index f0a0e4f..5e23503 100644
--- a/client/wallet.c
+++ b/client/wallet.c
@@ -51,7 +51,7 @@ usage(int status)
int
main(int argc, char *argv[])
{
- int option, i;
+ int option, i, status;
const char **command;
const char *type = "wallet";
const char *server = SERVER;
@@ -60,7 +60,6 @@ main(int argc, char *argv[])
const char *file = NULL;
const char *srvtab = NULL;
struct remctl *r;
- struct remctl_output *output;
long tmp;
char *end;
@@ -140,30 +139,8 @@ main(int argc, char *argv[])
for (i = 0; i < argc; i++)
command[i + 1] = argv[i];
command[argc + 1] = NULL;
- if (!remctl_command(r, command))
- die("%s", remctl_error(r));
- do {
- output = remctl_output(r);
- switch (output->type) {
- case REMCTL_OUT_OUTPUT:
- if (output->stream == 1)
- fwrite(output->data, 1, output->length, stdout);
- else {
- fprintf(stderr, "wallet: ");
- fwrite(output->data, 1, output->length, stderr);
- }
- break;
- case REMCTL_OUT_STATUS:
- exit(output->status);
- case REMCTL_OUT_ERROR:
- fprintf(stderr, "wallet: ");
- fwrite(output->data, 1, output->length, stderr);
- fputc('\n', stderr);
- exit(255);
- case REMCTL_OUT_DONE:
- break;
- }
- } while (output->type != REMCTL_OUT_DONE);
+ status = run_command(r, command, NULL, NULL);
+ exit(status);
}
/* This should never be reached. */