Update gnulib files.
[shishi.git] / lib / error.c
blob0b8368d21ca543fbc60303cfdbd48bd8723a4f1b
1 /* error.c --- Error handling functions.
2 * Copyright (C) 2002, 2003, 2004, 2006, 2007 Simon Josefsson
4 * This file is part of Shishi.
6 * Shishi is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * Shishi is distributed in the hope that it will be useful, but
12 * 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, see http://www.gnu.org/licenses or write
18 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
19 * Floor, Boston, MA 02110-1301, USA
23 #include "internal.h"
25 struct shishi_error_msgs
27 int errorcode;
28 const char *message;
31 static const struct shishi_error_msgs _shishi_error_messages[] = {
32 {SHISHI_OK,
33 N_("Shishi success")},
34 {SHISHI_ASN1_ERROR,
35 N_("Error in ASN.1 function (corrupt data?)")},
36 {SHISHI_FOPEN_ERROR,
37 N_("Could not open file")},
38 {SHISHI_IO_ERROR,
39 N_("File input/output error")},
40 {SHISHI_MALLOC_ERROR,
41 N_("Memory allocation error in shishi library.")},
42 {SHISHI_BASE64_ERROR,
43 N_("Base64 encoding or decoding failed. Data corrupt?")},
44 {SHISHI_REALM_MISMATCH,
45 N_("Client realm value differ between request and reply.")},
46 {SHISHI_CNAME_MISMATCH,
47 N_("Client name value differ between request and reply.")},
48 {SHISHI_NONCE_MISMATCH,
49 N_("Replay protection value (nonce) differ between request and reply.")},
50 {SHISHI_TICKET_BAD_KEYTYPE,
51 N_("Keytype used to encrypt ticket doesn't match provided key. "
52 "This usually indicates an internal application error.")},
53 {SHISHI_CRYPTO_INTERNAL_ERROR,
54 N_("Internal error in low-level crypto routines.")},
55 {SHISHI_CRYPTO_ERROR,
56 N_("Low-level cryptographic primitive failed. This usually indicates "
57 "bad password or data corruption.")},
58 {SHISHI_KDC_TIMEOUT,
59 N_("Timedout talking to KDC. This usually indicates a network "
60 "or KDC address problem.")},
61 {SHISHI_KDC_NOT_KNOWN_FOR_REALM,
62 N_("No KDC for realm known.")},
63 {SHISHI_SOCKET_ERROR,
64 N_("The system call socket() failed. This usually indicates that "
65 "your system does not support the socket type.")},
66 {SHISHI_BIND_ERROR,
67 N_("The system call bind() failed. This usually indicates "
68 "insufficient permissions.")},
69 {SHISHI_SENDTO_ERROR,
70 N_("The system call sendto() failed.")},
71 {SHISHI_CLOSE_ERROR,
72 N_("The system call close() failed.")},
73 {SHISHI_GOT_KRBERROR,
74 N_("Server replied with an error message to request.")},
75 {SHISHI_INVALID_TKTS,
76 N_("Ticketset not initialized. This usually indicates an internal "
77 "application error.")},
78 {SHISHI_APREQ_DECRYPT_FAILED,
79 N_("Could not decrypt AP-REQ using provided key. "
80 "This usually indicates an internal application error.")},
81 {SHISHI_TICKET_DECRYPT_FAILED,
82 N_("Could not decrypt Ticket using provided key. "
83 "This usually indicates an internal application error.")},
84 {SHISHI_KEYTAB_ERROR,
85 N_("Failed to parse keytab file")},
86 {SHISHI_CCACHE_ERROR,
87 N_("Failed to parse credential cache file")},
88 {-1, NULL}
91 /**
92 * shishi_strerror:
93 * @err: shishi error code.
95 * Convert return code to human readable string.
97 * Return value: Returns a pointer to a statically allocated string
98 * containing a description of the error with the error value @err.
99 * This string can be used to output a diagnostic message to the user.
101 const char *
102 shishi_strerror (int err)
104 const char *p = _("Unknown error");
105 size_t i;
107 for (i = 0; _shishi_error_messages[i].errorcode != -1; i++)
108 if (_shishi_error_messages[i].errorcode == err)
110 p = _(_shishi_error_messages[i].message);
111 break;
114 return p;
119 * shishi_error:
120 * @handle: shishi handle as allocated by shishi_init().
122 * Extract detailed error information string. Note that the memory is
123 * managed by the Shishi library, so you must not deallocate the
124 * string.
126 * Return value: Returns pointer to error information string, that must
127 * not be deallocate by caller.
129 const char *
130 shishi_error (Shishi * handle)
132 if (handle->error)
133 return handle->error;
135 return _("No error");
139 * shishi_error_clear:
140 * @handle: shishi handle as allocated by shishi_init().
142 * Clear the detailed error information string. See shishi_error()
143 * for how to access the error string, and shishi_error_set() and
144 * shishi_error_printf() for how to set the error string. This
145 * function is mostly for Shishi internal use, but if you develop an
146 * extension of Shishi, it may be useful to use the same error
147 * handling infrastructure.
149 void
150 shishi_error_clear (Shishi * handle)
152 handle->error[0] = '\0';
156 * shishi_error_set:
157 * @handle: shishi handle as allocated by shishi_init().
158 * @errstr: Zero terminated character array containing error description,
159 * or NULL to clear the error description string.
161 * Set the detailed error information string to specified string. The
162 * string is copied into the Shishi internal structure, so you can
163 * deallocate the string passed to this function after the call. This
164 * function is mostly for Shishi internal use, but if you develop an
165 * extension of Shishi, it may be useful to use the same error
166 * handling infrastructure.
168 void
169 shishi_error_set (Shishi * handle, const char *errstr)
171 if (errstr)
173 strncpy (handle->error, errstr, sizeof (handle->error));
175 if (VERBOSE (handle))
176 puts (handle->error);
178 else
179 shishi_error_clear (handle);
183 * shishi_error_printf:
184 * @handle: shishi handle as allocated by shishi_init().
185 * @format: printf style format string.
186 * @...: print style arguments.
188 * Set the detailed error information string to a printf formatted
189 * string. This function is mostly for Shishi internal use, but if
190 * you develop an extension of Shishi, it may be useful to use the
191 * same error handling infrastructure.
193 void
194 shishi_error_printf (Shishi * handle, const char *format, ...)
196 va_list ap;
197 char *s;
199 va_start (ap, format);
201 vasprintf (&s, format, ap);
202 strncpy (handle->error, s, sizeof (handle->error));
203 handle->error[sizeof (handle->error) - 1] = '\0';
204 free (s);
206 if (VERBOSE (handle))
207 puts (handle->error);
209 va_end (ap);
213 * shishi_error_outputtype:
214 * @handle: shishi handle as allocated by shishi_init().
216 * Get the current output type for logging messages.
218 * Return value: Return output type (NULL, stderr or syslog) for
219 * informational and warning messages.
222 shishi_error_outputtype (Shishi * handle)
224 return handle->outputtype;
228 * shishi_error_set_outputtype:
229 * @handle: shishi handle as allocated by shishi_init().
230 * @type: output type.
232 * Set output type (NULL, stderr or syslog) for informational
233 * and warning messages.
235 void
236 shishi_error_set_outputtype (Shishi * handle, int type)
238 handle->outputtype = type;
242 * shishi_info:
243 * @handle: shishi handle as allocated by shishi_init().
244 * @format: printf style format string.
245 * @...: print style arguments.
247 * Print informational message to output as defined in handle.
249 void
250 shishi_info (Shishi * handle, const char *format, ...)
252 va_list ap;
253 char *out;
254 int type;
256 va_start (ap, format);
257 vasprintf (&out, format, ap);
259 type = shishi_error_outputtype (handle);
260 switch (type)
262 case SHISHI_OUTPUTTYPE_STDERR:
263 fprintf (stderr, _("libshishi: info: %s\n"), out);
264 break;
265 case SHISHI_OUTPUTTYPE_SYSLOG:
266 syslog (LOG_ERR, _("libshishi: info: %s"), out);
267 break;
268 default:
269 break;
272 free (out);
273 va_end (ap);
277 * shishi_warn:
278 * @handle: shishi handle as allocated by shishi_init().
279 * @format: printf style format string.
280 * @...: print style arguments.
282 * Print a warning to output as defined in handle.
284 void
285 shishi_warn (Shishi * handle, const char *format, ...)
287 va_list ap;
288 char *out;
289 int type;
291 va_start (ap, format);
292 vasprintf (&out, format, ap);
294 type = shishi_error_outputtype (handle);
295 switch (type)
297 case SHISHI_OUTPUTTYPE_STDERR:
298 fprintf (stderr, _("libshishi: warning: %s\n"), out);
299 break;
300 case SHISHI_OUTPUTTYPE_SYSLOG:
301 syslog (LOG_ERR, _("libshishi: warning: %s"), out);
302 break;
303 default:
304 break;
307 free (out);
308 va_end (ap);
312 * shishi_verbose:
313 * @handle: shishi handle as allocated by shishi_init().
314 * @format: printf style format string.
315 * @...: print style arguments.
317 * Print a diagnostic message to output as defined in handle.
319 void
320 shishi_verbose (Shishi * handle, const char *format, ...)
322 va_list ap;
323 char *out;
324 int type;
326 if (!VERBOSE (handle))
327 return;
329 va_start (ap, format);
330 vasprintf (&out, format, ap);
332 type = shishi_error_outputtype (handle);
333 switch (type)
335 case SHISHI_OUTPUTTYPE_STDERR:
336 fprintf (stderr, "%s\n", out);
337 break;
338 case SHISHI_OUTPUTTYPE_SYSLOG:
339 syslog (LOG_DEBUG, "%s", out);
340 break;
341 default:
342 break;
345 free (out);
346 va_end (ap);