| 1 | /* |
|---|
| 2 | * Error handling for the remctl client library. |
|---|
| 3 | * |
|---|
| 4 | * A set of helper routines to do error handling inside the remctl client |
|---|
| 5 | * library. These mostly involve setting the error parameter in the remctl |
|---|
| 6 | * struct to something appropriate so that the next call to remctl_error will |
|---|
| 7 | * return the appropriate details. |
|---|
| 8 | * |
|---|
| 9 | * Written by Russ Allbery <rra@stanford.edu> |
|---|
| 10 | * Copyright 2006, 2007, 2008 |
|---|
| 11 | * Board of Trustees, Leland Stanford Jr. University |
|---|
| 12 | * |
|---|
| 13 | * See LICENSE for licensing terms. |
|---|
| 14 | */ |
|---|
| 15 | |
|---|
| 16 | #include <config.h> |
|---|
| 17 | #include <portable/system.h> |
|---|
| 18 | #include <portable/gssapi.h> |
|---|
| 19 | #include <portable/socket.h> |
|---|
| 20 | |
|---|
| 21 | #include <errno.h> |
|---|
| 22 | |
|---|
| 23 | #include <client/internal.h> |
|---|
| 24 | #include <util/util.h> |
|---|
| 25 | |
|---|
| 26 | /* |
|---|
| 27 | * Internal function to set the error message, freeing an old error message if |
|---|
| 28 | * one is present. |
|---|
| 29 | */ |
|---|
| 30 | void |
|---|
| 31 | internal_set_error(struct remctl *r, const char *format, ...) |
|---|
| 32 | { |
|---|
| 33 | va_list args; |
|---|
| 34 | int status; |
|---|
| 35 | |
|---|
| 36 | if (r->error != NULL) |
|---|
| 37 | free(r->error); |
|---|
| 38 | va_start(args, format); |
|---|
| 39 | status = vasprintf(&r->error, format, args); |
|---|
| 40 | va_end(args); |
|---|
| 41 | |
|---|
| 42 | /* |
|---|
| 43 | * If vasprintf fails, there isn't much we can do, but make sure that at |
|---|
| 44 | * least the error is in a consistent state. |
|---|
| 45 | */ |
|---|
| 46 | if (status < 0) |
|---|
| 47 | r->error = NULL; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | /* |
|---|
| 52 | * Internal function to set the remctl error message from a GSS-API error |
|---|
| 53 | * message. |
|---|
| 54 | */ |
|---|
| 55 | void |
|---|
| 56 | internal_gssapi_error(struct remctl *r, const char *error, OM_uint32 major, |
|---|
| 57 | OM_uint32 minor) |
|---|
| 58 | { |
|---|
| 59 | if (r->error != NULL) |
|---|
| 60 | free(r->error); |
|---|
| 61 | r->error = gssapi_error_string(error, major, minor); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | /* |
|---|
| 66 | * Internal function to set the remctl error message from a token error. |
|---|
| 67 | * Handles the various token failure codes from the token_send and token_recv |
|---|
| 68 | * functions and their *_priv counterparts. |
|---|
| 69 | */ |
|---|
| 70 | void |
|---|
| 71 | internal_token_error(struct remctl *r, const char *error, int status, |
|---|
| 72 | OM_uint32 major, OM_uint32 minor) |
|---|
| 73 | { |
|---|
| 74 | switch (status) { |
|---|
| 75 | case TOKEN_OK: |
|---|
| 76 | internal_set_error(r, "error %s", error); |
|---|
| 77 | break; |
|---|
| 78 | case TOKEN_FAIL_SYSTEM: |
|---|
| 79 | internal_set_error(r, "error %s: %s", error, strerror(errno)); |
|---|
| 80 | break; |
|---|
| 81 | case TOKEN_FAIL_SOCKET: |
|---|
| 82 | internal_set_error(r, "error %s: %s", error, |
|---|
| 83 | socket_strerror(socket_errno)); |
|---|
| 84 | break; |
|---|
| 85 | case TOKEN_FAIL_INVALID: |
|---|
| 86 | internal_set_error(r, "error %s: invalid token format", error); |
|---|
| 87 | break; |
|---|
| 88 | case TOKEN_FAIL_LARGE: |
|---|
| 89 | internal_set_error(r, "error %s: token too larger", error); |
|---|
| 90 | break; |
|---|
| 91 | case TOKEN_FAIL_EOF: |
|---|
| 92 | internal_set_error(r, "error %s: unexpected end of file", error); |
|---|
| 93 | break; |
|---|
| 94 | case TOKEN_FAIL_GSSAPI: |
|---|
| 95 | internal_gssapi_error(r, error, major, minor); |
|---|
| 96 | break; |
|---|
| 97 | default: |
|---|
| 98 | internal_set_error(r, "error %s: unknown error", error); |
|---|
| 99 | break; |
|---|
| 100 | } |
|---|
| 101 | } |
|---|