| 1 | /* |
|---|
| 2 | * Error handling |
|---|
| 3 | * |
|---|
| 4 | * Copyright (C) 2003, Olaf Kirch <okir@suse.de> |
|---|
| 5 | */ |
|---|
| 6 | |
|---|
| 7 | #ifdef HAVE_CONFIG_H |
|---|
| 8 | #include <config.h> |
|---|
| 9 | #endif |
|---|
| 10 | #include <stdio.h> |
|---|
| 11 | #include <stdarg.h> |
|---|
| 12 | #include <syslog.h> |
|---|
| 13 | #include <string.h> |
|---|
| 14 | #include <openct/error.h> |
|---|
| 15 | #include <openct/logging.h> |
|---|
| 16 | |
|---|
| 17 | enum { |
|---|
| 18 | DST_STDERR, |
|---|
| 19 | DST_SYSLOG |
|---|
| 20 | }; |
|---|
| 21 | |
|---|
| 22 | static int log_open = 0; |
|---|
| 23 | static int log_dest = DST_STDERR; |
|---|
| 24 | |
|---|
| 25 | static void ct_log_init(void) |
|---|
| 26 | { |
|---|
| 27 | if (!log_open) { |
|---|
| 28 | openlog("ifdhandler", LOG_PID, LOG_DAEMON); |
|---|
| 29 | log_open = 1; |
|---|
| 30 | } |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | void ct_log_destination(const char *dest) |
|---|
| 34 | { |
|---|
| 35 | ct_log_init(); |
|---|
| 36 | if (!strcmp(dest, "@stderr")) { |
|---|
| 37 | log_dest = DST_STDERR; |
|---|
| 38 | } else if (!strcmp(dest, "@syslog")) { |
|---|
| 39 | log_dest = DST_SYSLOG; |
|---|
| 40 | } else { |
|---|
| 41 | log_dest = DST_STDERR; |
|---|
| 42 | ct_error("log destination %s not implemented yet", dest); |
|---|
| 43 | } |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | void ct_error(const char *fmt, ...) |
|---|
| 47 | { |
|---|
| 48 | va_list ap; |
|---|
| 49 | int n; |
|---|
| 50 | |
|---|
| 51 | va_start(ap, fmt); |
|---|
| 52 | if (log_dest == DST_STDERR) { |
|---|
| 53 | fprintf(stderr, "Error: "); |
|---|
| 54 | vfprintf(stderr, fmt, ap); |
|---|
| 55 | if (!(n = strlen(fmt)) || fmt[n - 1] != '\n') |
|---|
| 56 | fprintf(stderr, "\n"); |
|---|
| 57 | } else { |
|---|
| 58 | vsyslog(LOG_WARNING, fmt, ap); |
|---|
| 59 | } |
|---|
| 60 | va_end(ap); |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | void ct_debug(const char *fmt, ...) |
|---|
| 64 | { |
|---|
| 65 | va_list ap; |
|---|
| 66 | |
|---|
| 67 | va_start(ap, fmt); |
|---|
| 68 | if (log_dest == DST_STDERR) { |
|---|
| 69 | fprintf(stderr, "Debug: "); |
|---|
| 70 | vfprintf(stderr, fmt, ap); |
|---|
| 71 | fprintf(stderr, "\n"); |
|---|
| 72 | } else { |
|---|
| 73 | vsyslog(LOG_DEBUG, fmt, ap); |
|---|
| 74 | } |
|---|
| 75 | va_end(ap); |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | const char *ct_hexdump(const void *data, size_t len) |
|---|
| 79 | { |
|---|
| 80 | static char string[1024]; |
|---|
| 81 | unsigned char *d = (unsigned char *)data; |
|---|
| 82 | unsigned int i, left; |
|---|
| 83 | |
|---|
| 84 | string[0] = '\0'; |
|---|
| 85 | left = sizeof(string); |
|---|
| 86 | for (i = 0; len--; i += 3) { |
|---|
| 87 | if (i >= sizeof(string) - 4) |
|---|
| 88 | break; |
|---|
| 89 | snprintf(string + i, 4, " %02x", *d++); |
|---|
| 90 | } |
|---|
| 91 | return string; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | #define DIM(v) (sizeof(v)/(sizeof((v)[0]))) |
|---|
| 95 | |
|---|
| 96 | const char *ct_strerror(int rc) |
|---|
| 97 | { |
|---|
| 98 | const char *proto_errors[] = { |
|---|
| 99 | "Invalid message", |
|---|
| 100 | "Invalid command", |
|---|
| 101 | "Missing argument", |
|---|
| 102 | "Not connected to IFD handler", |
|---|
| 103 | }; |
|---|
| 104 | const int proto_base = -IFD_ERROR_INVALID_MSG; |
|---|
| 105 | const char *gen_errors[] = { |
|---|
| 106 | "Success", |
|---|
| 107 | "Generic error", |
|---|
| 108 | "Command timed out", |
|---|
| 109 | "Invalid slot", |
|---|
| 110 | "Operation not supported", |
|---|
| 111 | "Communication error", |
|---|
| 112 | "No card present", |
|---|
| 113 | "Reader already locked", |
|---|
| 114 | "Reader not locked", |
|---|
| 115 | "Invalid argument", |
|---|
| 116 | "Out of memory", |
|---|
| 117 | "Buffer too small", |
|---|
| 118 | "Timeout on user input", |
|---|
| 119 | "Operation aborted by user", |
|---|
| 120 | "PIN mismatch", |
|---|
| 121 | "Unable to reset card", |
|---|
| 122 | "Device cannot perform requested operation", |
|---|
| 123 | "Device was disconnected", |
|---|
| 124 | "Card returned invalid ATR", |
|---|
| 125 | }; |
|---|
| 126 | const int gen_base = -IFD_SUCCESS; |
|---|
| 127 | const char *proxy_errors[] = { |
|---|
| 128 | "Device already claimed", |
|---|
| 129 | "Device busy", |
|---|
| 130 | "Device not known", |
|---|
| 131 | }; |
|---|
| 132 | const int proxy_base = -IFD_ERROR_ALREADY_CLAIMED; |
|---|
| 133 | const char **errors = NULL, *msg = NULL; |
|---|
| 134 | int count = 0, err_base = 0, error = rc; |
|---|
| 135 | static char message[64]; |
|---|
| 136 | |
|---|
| 137 | if (error < 0) |
|---|
| 138 | error = -error; |
|---|
| 139 | if (error >= proto_base) { |
|---|
| 140 | errors = proto_errors; |
|---|
| 141 | count = DIM(proto_errors); |
|---|
| 142 | err_base = proto_base; |
|---|
| 143 | } else if (error >= gen_base) { |
|---|
| 144 | errors = gen_errors; |
|---|
| 145 | count = DIM(gen_errors); |
|---|
| 146 | err_base = gen_base; |
|---|
| 147 | } else if (error >= proxy_base) { |
|---|
| 148 | errors = proxy_errors; |
|---|
| 149 | count = DIM(proxy_errors); |
|---|
| 150 | err_base = proxy_base; |
|---|
| 151 | } |
|---|
| 152 | error -= err_base; |
|---|
| 153 | if (error >= count || count == 0) { |
|---|
| 154 | msg = message; |
|---|
| 155 | snprintf(message, sizeof(message), |
|---|
| 156 | "Unknown OpenCT error %d", -rc); |
|---|
| 157 | } else { |
|---|
| 158 | msg = errors[error]; |
|---|
| 159 | } |
|---|
| 160 | return msg; |
|---|
| 161 | } |
|---|