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
25 struct shishi_error_msgs
31 static const struct shishi_error_msgs _shishi_error_messages
[] = {
33 N_("Shishi success")},
35 N_("Error in ASN.1 function (corrupt data?)")},
37 N_("Could not open file")},
39 N_("File input/output error")},
41 N_("Memory allocation error in shishi library.")},
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.")},
56 N_("Low-level cryptographic primitive failed. This usually indicates "
57 "bad password or data corruption.")},
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.")},
64 N_("The system call socket() failed. This usually indicates that "
65 "your system does not support the socket type.")},
67 N_("The system call bind() failed. This usually indicates "
68 "insufficient permissions.")},
70 N_("The system call sendto() failed.")},
72 N_("The system call close() failed.")},
74 N_("Server replied with an error message to request.")},
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.")},
85 N_("Failed to parse keytab file")},
87 N_("Failed to parse credential cache file")},
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.
102 shishi_strerror (int err
)
104 const char *p
= _("Unknown error");
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
);
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
126 * Return value: Returns pointer to error information string, that must
127 * not be deallocate by caller.
130 shishi_error (Shishi
* handle
)
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.
150 shishi_error_clear (Shishi
* handle
)
152 handle
->error
[0] = '\0';
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.
169 shishi_error_set (Shishi
* handle
, const char *errstr
)
173 strncpy (handle
->error
, errstr
, sizeof (handle
->error
));
175 if (VERBOSE (handle
))
176 puts (handle
->error
);
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.
194 shishi_error_printf (Shishi
* handle
, const char *format
, ...)
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';
206 if (VERBOSE (handle
))
207 puts (handle
->error
);
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.
236 shishi_error_set_outputtype (Shishi
* handle
, int type
)
238 handle
->outputtype
= type
;
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.
250 shishi_info (Shishi
* handle
, const char *format
, ...)
256 va_start (ap
, format
);
257 vasprintf (&out
, format
, ap
);
259 type
= shishi_error_outputtype (handle
);
262 case SHISHI_OUTPUTTYPE_STDERR
:
263 fprintf (stderr
, _("libshishi: info: %s\n"), out
);
265 case SHISHI_OUTPUTTYPE_SYSLOG
:
266 syslog (LOG_ERR
, _("libshishi: info: %s"), out
);
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.
285 shishi_warn (Shishi
* handle
, const char *format
, ...)
291 va_start (ap
, format
);
292 vasprintf (&out
, format
, ap
);
294 type
= shishi_error_outputtype (handle
);
297 case SHISHI_OUTPUTTYPE_STDERR
:
298 fprintf (stderr
, _("libshishi: warning: %s\n"), out
);
300 case SHISHI_OUTPUTTYPE_SYSLOG
:
301 syslog (LOG_ERR
, _("libshishi: warning: %s"), out
);
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.
320 shishi_verbose (Shishi
* handle
, const char *format
, ...)
326 if (!VERBOSE (handle
))
329 va_start (ap
, format
);
330 vasprintf (&out
, format
, ap
);
332 type
= shishi_error_outputtype (handle
);
335 case SHISHI_OUTPUTTYPE_STDERR
:
336 fprintf (stderr
, "%s\n", out
);
338 case SHISHI_OUTPUTTYPE_SYSLOG
:
339 syslog (LOG_DEBUG
, "%s", out
);