(_shishi_crypto_init): Fix prototype, from Nicolas Pouvesle
[shishi.git] / lib / error.c
blob20500de0b4f2eb695a6ac49dc842960b51213154
1 /* error.c error handling functions
2 * Copyright (C) 2002, 2003 Simon Josefsson
4 * This file is part of Shishi.
6 * Shishi is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Shishi is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Shishi; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "internal.h"
24 struct shishi_error_msgs
26 int errorcode;
27 char *message;
30 struct shishi_error_msgs _shishi_error_messages[] = {
31 {SHISHI_OK,
32 "Shishi success"},
33 {SHISHI_ASN1_ERROR,
34 "Error in ASN.1 data, probably due to corrupt data."},
35 {SHISHI_FOPEN_ERROR,
36 "Could not open file."},
37 {SHISHI_FCLOSE_ERROR,
38 "Could not close file."},
39 {SHISHI_MALLOC_ERROR,
40 "Memory allocation error in shishi library."},
41 {SHISHI_BASE64_ERROR,
42 "Base64 encoding or decoding failed. Data corrupt?"},
43 {SHISHI_REALM_MISMATCH,
44 "Client realm value differ between request and reply."},
45 {SHISHI_CNAME_MISMATCH,
46 "Client name value differ between request and reply."},
47 {SHISHI_NONCE_MISMATCH,
48 "Replay protection value (nonce) differ between request and reply."},
49 {SHISHI_TICKET_BAD_KEYTYPE,
50 "Keytype used to encrypt ticket doesn't match provided key. "
51 "This usually indicates an internal application error."},
52 {SHISHI_CRYPTO_INTERNAL_ERROR,
53 "Internal error in low-level crypto routines."},
54 {SHISHI_CRYPTO_ERROR,
55 "Low-level cryptographic primitive failed. This usually indicates "
56 "bad password or data corruption."},
57 {SHISHI_KDC_TIMEOUT,
58 "Timedout talking to KDC. This usually indicates a network "
59 "or KDC address problem."},
60 {SHISHI_KDC_NOT_KNOWN_FOR_REALM,
61 "No KDC for realm known."},
62 {SHISHI_SOCKET_ERROR,
63 "The system call socket() failed. This usually indicates that "
64 "your system does not support the socket type."},
65 {SHISHI_BIND_ERROR,
66 "The system call bind() failed. This usually indicates "
67 "insufficient permissions."},
68 {SHISHI_SENDTO_ERROR,
69 "The system call sendto() failed."},
70 {SHISHI_CLOSE_ERROR,
71 "The system call close() failed."},
72 {SHISHI_GOT_KRBERROR,
73 "Server replied with an error message to request."},
74 {SHISHI_INVALID_TKTS,
75 "Ticketset not initialized. This usually indicates an internal "
76 "application error."},
77 {SHISHI_APREQ_DECRYPT_FAILED,
78 "Could not decrypt AP-REQ using provided key. "
79 "This usually indicates an internal application error."},
80 {SHISHI_TICKET_DECRYPT_FAILED,
81 "Could not decrypt Ticket using provided key. "
82 "This usually indicates an internal application error."},
83 {-1, NULL}
86 /**
87 * shishi_strerror:
88 * @err: shishi error code
90 * Return value: Returns a pointer to a statically allocated string
91 * containing a description of the error with the error value @err.
92 * This string can be used to output a diagnostic message to the user.
93 **/
94 const char *
95 shishi_strerror (int err)
97 char *p = NULL;
98 size_t i;
100 for (i = 0; _shishi_error_messages[i].errorcode != -1; i++)
101 if (_shishi_error_messages[i].errorcode == err)
103 p = _(_shishi_error_messages[i].message);
104 break;
107 if (!p)
108 /* XXX mem leak */
109 asprintf (&p, _("Unknown shishi error: %d"), err);
111 return p;
116 * shishi_error:
117 * @handle: shishi handle as allocated by shishi_init().
119 * Extract detailed error information string. Note that the memory is
120 * managed by the Shishi library, so you must not deallocate the
121 * string.
123 * Return value: Returns pointer to error information string, that must
124 * not be deallocate by caller.
126 const char *
127 shishi_error (Shishi * handle)
129 if (handle->error)
130 return handle->error;
132 return "No error";
136 * shishi_error_clear:
137 * @handle: shishi handle as allocated by shishi_init().
139 * Clear the detailed error information string. See shishi_error()
140 * for how to access the error string, and shishi_error_set() and
141 * shishi_error_printf() for how to set the error string. This
142 * function is mostly for Shishi internal use, but if you develop an
143 * extension of Shishi, it may be useful to use the same error
144 * handling infrastructure.
146 void
147 shishi_error_clear (Shishi * handle)
149 handle->error[0] = '\0';
153 * shishi_error_set:
154 * @handle: shishi handle as allocated by shishi_init().
155 * @error: Zero terminated character array containing error description,
156 * or NULL to clear the error description string.
158 * Set the detailed error information string to specified string. The
159 * string is copied into the Shishi internal structure, so you can
160 * deallocate the string passed to this function after the call. This
161 * function is mostly for Shishi internal use, but if you develop an
162 * extension of Shishi, it may be useful to use the same error
163 * handling infrastructure.
165 void
166 shishi_error_set (Shishi * handle, const char *error)
168 if (error)
170 strncpy (handle->error, error, sizeof (handle->error));
172 if (VERBOSE (handle))
173 puts (handle->error);
175 else
176 shishi_error_clear (handle);
180 * shishi_error_printf:
181 * @handle: shishi handle as allocated by shishi_init().
182 * @format: printf style format string.
183 * @...: print style arguments.
185 * Set the detailed error information string to a printf formatted
186 * string. This function is mostly for Shishi internal use, but if
187 * you develop an extension of Shishi, it may be useful to use the
188 * same error handling infrastructure.
190 void
191 shishi_error_printf (Shishi * handle, const char *format, ...)
193 va_list ap;
194 char *s;
196 va_start (ap, format);
198 vasprintf (&s, format, ap);
199 strncpy (handle->error, s, sizeof (handle->error));
200 handle->error[sizeof (handle->error) - 1] = '\0';
201 free (s);
203 if (VERBOSE (handle))
204 puts (handle->error);
206 va_end (ap);
210 * shishi_outputtype:
211 * @handle: shishi handle as allocated by shishi_init().
213 * Return output type (NULL, stderr or syslog) for informational
214 * and warning messages.
217 shishi_outputtype (Shishi * handle)
219 return handle->outputtype;
223 * shishi_set_outputtype:
224 * @handle: shishi handle as allocated by shishi_init().
225 * @type: output type.
227 * Set output type (NULL, stderr or syslog) for informational
228 * and warning messages.
230 void
231 shishi_set_outputtype (Shishi * handle, int type)
233 handle->outputtype = type;
236 #define INFOSTR "libshishi: info: "
237 #define WARNSTR "libshishi: warning: "
240 * shishi_info:
241 * @handle: shishi handle as allocated by shishi_init().
242 * @format: printf style format string.
243 * @...: print style arguments.
245 * Print informational message to output as defined in handle.
247 void
248 shishi_info (Shishi * handle, const char *format, ...)
250 va_list ap;
251 char * out;
252 int type;
254 va_start (ap, format);
255 vasprintf (&out, format, ap);
257 type = shishi_outputtype (handle);
258 switch (type)
260 case SHISHI_OUTPUTTYPE_STDERR:
261 fprintf (stderr, "%s%s\n", INFOSTR, out);
262 break;
263 case SHISHI_OUTPUTTYPE_SYSLOG:
264 syslog (LOG_ERR, "%s%s", INFOSTR, out);
265 break;
266 default:
267 break;
270 free (out);
271 va_end (ap);
275 * shishi_warn:
276 * @handle: shishi handle as allocated by shishi_init().
277 * @format: printf style format string.
278 * @...: print style arguments.
280 * Print a warning to output as defined in handle.
282 void
283 shishi_warn (Shishi * handle, const char *format, ...)
285 va_list ap;
286 char * out;
287 int type;
289 va_start (ap, format);
290 vasprintf (&out, format, ap);
292 type = shishi_outputtype (handle);
293 switch (type)
295 case SHISHI_OUTPUTTYPE_STDERR:
296 fprintf (stderr, "%s%s\n", WARNSTR, out);
297 break;
298 case SHISHI_OUTPUTTYPE_SYSLOG:
299 syslog (LOG_ERR, "%s%s", WARNSTR, out);
300 break;
301 default:
302 break;
305 free (out);
306 va_end (ap);