aboutsummaryrefslogtreecommitdiff
path: root/client
diff options
context:
space:
mode:
authorRuss Allbery <rra@stanford.edu>2007-10-04 23:40:35 +0000
committerRuss Allbery <rra@stanford.edu>2007-10-04 23:40:35 +0000
commita78a2615ae535839700b48d200a097c1c62021be (patch)
tree08a1f3ead1323a8de4823396bff91c2004a1283f /client
parentf09ec713c68c4c9b0c6dd89090fc9ce4e9d93042 (diff)
Use die, warn, and xmalloc, now that we have them.
Diffstat (limited to 'client')
-rw-r--r--client/srvtab.c28
-rw-r--r--client/wallet.c63
2 files changed, 33 insertions, 58 deletions
diff --git a/client/srvtab.c b/client/srvtab.c
index 573840a..b50193e 100644
--- a/client/srvtab.c
+++ b/client/srvtab.c
@@ -14,10 +14,9 @@
#include <errno.h>
#include <fcntl.h>
#include <krb5.h>
-#include <string.h>
-#include <unistd.h>
#include <client/internal.h>
+#include <util/util.h>
#ifndef KRB5_KRB4_COMPAT
# define ANAME_SZ 40
@@ -75,7 +74,7 @@ die_krb5(krb5_context ctx, const char *message, krb5_error_code code)
const char *k5_msg = NULL;
k5_msg = strerror_krb5(ctx, code);
- fprintf(stderr, "%s: %s\n", message, k5_msg);
+ warn("%s: %s\n", message, k5_msg);
strerror_krb5_free(ctx, k5_msg);
exit(1);
}
@@ -150,20 +149,13 @@ write_srvtab(const char *srvtab, const char *principal, const char *keytab)
/* Write out the srvtab file. */
fd = open(srvtab, O_WRONLY | O_CREAT | O_TRUNC, 0600);
- if (fd < 0) {
- fprintf(stderr, "open of %s failed: %s", srvtab, strerror(errno));
- exit(1);
- }
+ if (fd < 0)
+ sysdie("open of %s failed", srvtab);
status = write(fd, data, length);
- if (status < 0) {
- fprintf(stderr, "write to %s failed: %s", srvtab, strerror(errno));
- exit(1);
- } else if (status != (ssize_t) length) {
- fprintf(stderr, "write to %s truncated", srvtab);
- exit(1);
- }
- if (close(fd) < 0) {
- fprintf(stderr, "close of %s failed: %s", srvtab, strerror(errno));
- exit(1);
- }
+ if (status < 0)
+ sysdie("write to %s failed", srvtab);
+ else if (status != (ssize_t) length)
+ die("write to %s truncated", srvtab);
+ if (close(fd) < 0)
+ sysdie("close of %s failed (file probably truncated)", srvtab);
}
diff --git a/client/wallet.c b/client/wallet.c
index 8d8bb58..e3ca4dd 100644
--- a/client/wallet.c
+++ b/client/wallet.c
@@ -16,6 +16,7 @@
#include <remctl.h>
#include <client/internal.h>
+#include <util/util.h>
/* Usage message. */
static const char usage_message[] = "\
@@ -65,6 +66,9 @@ main(int argc, char *argv[])
long tmp;
char *end;
+ /* Set up logging and identity. */
+ message_program_name = "wallet";
+
while ((option = getopt(argc, argv, "c:f:k:hp:S:s:v")) != EOF) {
switch (option) {
case 'c':
@@ -82,10 +86,8 @@ main(int argc, char *argv[])
case 'p':
errno = 0;
tmp = strtol(optarg, &end, 10);
- if (tmp <= 0 || tmp > 65535 || *end != '\0') {
- fprintf(stderr, "Invalid port number %s\n", optarg);
- exit(1);
- }
+ if (tmp <= 0 || tmp > 65535 || *end != '\0')
+ die("invalid port number %s", optarg);
port = tmp;
break;
case 'S':
@@ -109,27 +111,17 @@ main(int argc, char *argv[])
usage(1);
/* -f is only supported for get and -S with get keytab. */
- if (file != NULL && strcmp(argv[0], "get") != 0) {
- fprintf(stderr, "wallet: -f only supported for get\n");
- exit(1);
- }
+ if (file != NULL && strcmp(argv[0], "get") != 0)
+ die("-f only supported for get");
if (srvtab != NULL) {
- if (strcmp(argv[0], "get") != 0 || strcmp(argv[1], "keytab") != 0) {
- fprintf(stderr, "wallet: -S only supported for get keytab\n");
- exit(1);
- }
- if (file == NULL) {
- fprintf(stderr, "wallet: -S requires -f\n");
- exit(1);
- }
+ if (strcmp(argv[0], "get") != 0 || strcmp(argv[1], "keytab") != 0)
+ die("-S only supported for get keytab");
+ if (file == NULL)
+ die("-S option requires -f also be used");
}
/* Allocate space for the command to send to the server. */
- command = malloc(sizeof(char *) * (argc + 2));
- if (command == NULL) {
- fprintf(stderr, "wallet: cannot allocate memory: %s", strerror(errno));
- exit(1);
- }
+ command = xmalloc(sizeof(char *) * (argc + 2));
command[0] = type;
for (i = 0; i < argc; i++)
command[i + 1] = argv[i];
@@ -137,11 +129,9 @@ main(int argc, char *argv[])
/* Run the command. */
result = remctl(server, port, principal, command);
+ if (result == NULL)
+ sysdie("cannot allocate memory");
free(command);
- if (result == NULL) {
- fprintf(stderr, "wallet: cannot allocate memory: %s", strerror(errno));
- exit(1);
- }
/* Display the results. */
if (result->error != NULL) {
@@ -151,22 +141,15 @@ main(int argc, char *argv[])
fwrite(result->stderr_buf, 1, result->stderr_len, stderr);
} else if (file != NULL && strcmp(command[1], "get") == 0) {
fd = open(file, O_WRONLY | O_CREAT | O_TRUNC, 0600);
- if (fd < 0) {
- fprintf(stderr, "open of %s failed: %s", file, strerror(errno));
- exit(1);
- }
+ if (fd < 0)
+ sysdie("open of %s failed", file);
status = write(fd, result->stdout_buf, result->stdout_len);
- if (status < 0) {
- fprintf(stderr, "write to %s failed: %s", file, strerror(errno));
- exit(1);
- } else if (status != (ssize_t) result->stdout_len) {
- fprintf(stderr, "write to %s truncated", file);
- exit(1);
- }
- if (close(fd) < 0) {
- fprintf(stderr, "close of %s failed: %s", file, strerror(errno));
- exit(1);
- }
+ if (status < 0)
+ sysdie("write to %s failed", file);
+ else if (status != (ssize_t) result->stdout_len)
+ die("write to %s truncated", file);
+ if (close(fd) < 0)
+ sysdie("close of %s failed (file probably truncated)", file);
if (srvtab != NULL)
write_srvtab(srvtab, command[3], file);
} else {