Add.
[shishi.git] / lib / error.c
blob07c58f34e59467d288d3d14b4a2f27f6df8470a0
1 /* error.c --- Error handling functions.
2 * Copyright (C) 2002, 2003, 2004, 2006 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "internal.h"
24 struct shishi_error_msgs
26 int errorcode;
27 const char *message;
30 static const struct shishi_error_msgs _shishi_error_messages[] = {
31 {SHISHI_OK,
32 N_("Shishi success")},
33 {SHISHI_ASN1_ERROR,
34 N_("Error in ASN.1 function (corrupt data?)")},
35 {SHISHI_FOPEN_ERROR,
36 N_("Could not open file")},
37 {SHISHI_IO_ERROR,
38 N_("File input/output error")},
39 {SHISHI_MALLOC_ERROR,
40 N_("Memory allocation error in shishi library.")},
41 {SHISHI_BASE64_ERROR,
42 N_("Base64 encoding or decoding failed. Data corrupt?")},
43 {SHISHI_REALM_MISMATCH,
44 N_("Client realm value differ between request and reply.")},
45 {SHISHI_CNAME_MISMATCH,
46 N_("Client name value differ between request and reply.")},
47 {SHISHI_NONCE_MISMATCH,
48 N_("Replay protection value (nonce) differ between request and reply.")},
49 {SHISHI_TICKET_BAD_KEYTYPE,
50 N_("Keytype used to encrypt ticket doesn't match provided key. "
51 "This usually indicates an internal application error.")},
52 {SHISHI_CRYPTO_INTERNAL_ERROR,
53 N_("Internal error in low-level crypto routines.")},
54 {SHISHI_CRYPTO_ERROR,
55 N_("Low-level cryptographic primitive failed. This usually indicates "
56 "bad password or data corruption.")},
57 {SHISHI_KDC_TIMEOUT,
58 N_("Timedout talking to KDC. This usually indicates a network "
59 "or KDC address problem.")},
60 {SHISHI_KDC_NOT_KNOWN_FOR_REALM,
61 N_("No KDC for realm known.")},
62 {SHISHI_SOCKET_ERROR,
63 N_("The system call socket() failed. This usually indicates that "
64 "your system does not support the socket type.")},
65 {SHISHI_BIND_ERROR,
66 N_("The system call bind() failed. This usually indicates "
67 "insufficient permissions.")},
68 {SHISHI_SENDTO_ERROR,
69 N_("The system call sendto() failed.")},
70 {SHISHI_CLOSE_ERROR,
71 N_("The system call close() failed.")},
72 {SHISHI_GOT_KRBERROR,
73 N_("Server replied with an error message to request.")},
74 {SHISHI_INVALID_TKTS,
75 N_("Ticketset not initialized. This usually indicates an internal "
76 "application error.")},
77 {SHISHI_APREQ_DECRYPT_FAILED,
78 N_("Could not decrypt AP-REQ using provided key. "
79 "This usually indicates an internal application error.")},
80 {SHISHI_TICKET_DECRYPT_FAILED,
81 N_("Could not decrypt Ticket using provided key. "
82 "This usually indicates an internal application error.")},
83 {SHISHI_KEYTAB_ERROR,
84 N_("Failed to parse keytab file")},
85 {SHISHI_CCACHE_ERROR,
86 N_("Failed to parse credential cache file")},
87 {-1, NULL}
90 /**
91 * shishi_strerror:
92 * @err: shishi error code.
94 * Convert return code to human readable string.
96 * Return value: Returns a pointer to a statically allocated string
97 * containing a description of the error with the error value @err.
98 * This string can be used to output a diagnostic message to the user.
99 **/
100 const char *
101 shishi_strerror (int err)
103 const char *p = _("Unknown error");
104 size_t i;
106 for (i = 0; _shishi_error_messages[i].errorcode != -1; i++)
107 if (_shishi_error_messages[i].errorcode == err)
109 p = _(_shishi_error_messages[i].message);
110 break;
113 return p;
118 * shishi_error:
119 * @handle: shishi handle as allocated by shishi_init().
121 * Extract detailed error information string. Note that the memory is
122 * managed by the Shishi library, so you must not deallocate the
123 * string.
125 * Return value: Returns pointer to error information string, that must
126 * not be deallocate by caller.
128 const char *
129 shishi_error (Shishi * handle)
131 if (handle->error)
132 return handle->error;
134 return _("No error");
138 * shishi_error_clear:
139 * @handle: shishi handle as allocated by shishi_init().
141 * Clear the detailed error information string. See shishi_error()
142 * for how to access the error string, and shishi_error_set() and
143 * shishi_error_printf() for how to set the error string. This
144 * function is mostly for Shishi internal use, but if you develop an
145 * extension of Shishi, it may be useful to use the same error
146 * handling infrastructure.
148 void
149 shishi_error_clear (Shishi * handle)
151 handle->error[0] = '\0';
155 * shishi_error_set:
156 * @handle: shishi handle as allocated by shishi_init().
157 * @errstr: Zero terminated character array containing error description,
158 * or NULL to clear the error description string.
160 * Set the detailed error information string to specified string. The
161 * string is copied into the Shishi internal structure, so you can
162 * deallocate the string passed to this function after the call. This
163 * function is mostly for Shishi internal use, but if you develop an
164 * extension of Shishi, it may be useful to use the same error
165 * handling infrastructure.
167 void
168 shishi_error_set (Shishi * handle, const char *errstr)
170 if (errstr)
172 strncpy (handle->error, errstr, sizeof (handle->error));
174 if (VERBOSE (handle))
175 puts (handle->error);
177 else
178 shishi_error_clear (handle);
182 * shishi_error_printf:
183 * @handle: shishi handle as allocated by shishi_init().
184 * @format: printf style format string.
185 * @...: print style arguments.
187 * Set the detailed error information string to a printf formatted
188 * string. This function is mostly for Shishi internal use, but if
189 * you develop an extension of Shishi, it may be useful to use the
190 * same error handling infrastructure.
192 void
193 shishi_error_printf (Shishi * handle, const char *format, ...)
195 va_list ap;
196 char *s;
198 va_start (ap, format);
200 vasprintf (&s, format, ap);
201 strncpy (handle->error, s, sizeof (handle->error));
202 handle->error[sizeof (handle->error) - 1] = '\0';
203 free (s);
205 if (VERBOSE (handle))
206 puts (handle->error);
208 va_end (ap);
212 * shishi_error_outputtype:
213 * @handle: shishi handle as allocated by shishi_init().
215 * Get the current output type for logging messages.
217 * Return value: Return output type (NULL, stderr or syslog) for
218 * informational and warning messages.
221 shishi_error_outputtype (Shishi * handle)
223 return handle->outputtype;
227 * shishi_error_set_outputtype:
228 * @handle: shishi handle as allocated by shishi_init().
229 * @type: output type.
231 * Set output type (NULL, stderr or syslog) for informational
232 * and warning messages.
234 void
235 shishi_error_set_outputtype (Shishi * handle, int type)
237 handle->outputtype = type;
241 * shishi_info:
242 * @handle: shishi handle as allocated by shishi_init().
243 * @format: printf style format string.
244 * @...: print style arguments.
246 * Print informational message to output as defined in handle.
248 void
249 shishi_info (Shishi * handle, const char *format, ...)
251 va_list ap;
252 char *out;
253 int type;
255 va_start (ap, format);
256 vasprintf (&out, format, ap);
258 type = shishi_error_outputtype (handle);
259 switch (type)
261 case SHISHI_OUTPUTTYPE_STDERR:
262 fprintf (stderr, _("libshishi: info: %s\n"), out);
263 break;
264 case SHISHI_OUTPUTTYPE_SYSLOG:
265 syslog (LOG_ERR, _("libshishi: info: %s"), out);
266 break;
267 default:
268 break;
271 free (out);
272 va_end (ap);
276 * shishi_warn:
277 * @handle: shishi handle as allocated by shishi_init().
278 * @format: printf style format string.
279 * @...: print style arguments.
281 * Print a warning to output as defined in handle.
283 void
284 shishi_warn (Shishi * handle, const char *format, ...)
286 va_list ap;
287 char *out;
288 int type;
290 va_start (ap, format);
291 vasprintf (&out, format, ap);
293 type = shishi_error_outputtype (handle);
294 switch (type)
296 case SHISHI_OUTPUTTYPE_STDERR:
297 fprintf (stderr, _("libshishi: warning: %s\n"), out);
298 break;
299 case SHISHI_OUTPUTTYPE_SYSLOG:
300 syslog (LOG_ERR, _("libshishi: warning: %s"), out);
301 break;
302 default:
303 break;
306 free (out);
307 va_end (ap);
311 * shishi_verbose:
312 * @handle: shishi handle as allocated by shishi_init().
313 * @format: printf style format string.
314 * @...: print style arguments.
316 * Print a diagnostic message to output as defined in handle.
318 void
319 shishi_verbose (Shishi * handle, const char *format, ...)
321 va_list ap;
322 char *out;
323 int type;
325 if (!VERBOSE (handle))
326 return;
328 va_start (ap, format);
329 vasprintf (&out, format, ap);
331 type = shishi_error_outputtype (handle);
332 switch (type)
334 case SHISHI_OUTPUTTYPE_STDERR:
335 fprintf (stderr, "%s\n", out);
336 break;
337 case SHISHI_OUTPUTTYPE_SYSLOG:
338 syslog (LOG_DEBUG, "%s", out);
339 break;
340 default:
341 break;
344 free (out);
345 va_end (ap);