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
24 struct shishi_error_msgs
30 static const struct shishi_error_msgs _shishi_error_messages
[] = {
32 N_("Shishi success")},
34 N_("Error in ASN.1 function (corrupt data?)")},
36 N_("Could not open file")},
38 N_("File input/output error")},
40 N_("Memory allocation error in shishi library.")},
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.")},
55 N_("Low-level cryptographic primitive failed. This usually indicates "
56 "bad password or data corruption.")},
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.")},
63 N_("The system call socket() failed. This usually indicates that "
64 "your system does not support the socket type.")},
66 N_("The system call bind() failed. This usually indicates "
67 "insufficient permissions.")},
69 N_("The system call sendto() failed.")},
71 N_("The system call close() failed.")},
73 N_("Server replied with an error message to request.")},
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.")},
84 N_("Failed to parse keytab file")},
86 N_("Failed to parse credential cache file")},
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.
101 shishi_strerror (int err
)
103 const char *p
= _("Unknown error");
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
);
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
125 * Return value: Returns pointer to error information string, that must
126 * not be deallocate by caller.
129 shishi_error (Shishi
* handle
)
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.
149 shishi_error_clear (Shishi
* handle
)
151 handle
->error
[0] = '\0';
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.
168 shishi_error_set (Shishi
* handle
, const char *errstr
)
172 strncpy (handle
->error
, errstr
, sizeof (handle
->error
));
174 if (VERBOSE (handle
))
175 puts (handle
->error
);
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.
193 shishi_error_printf (Shishi
* handle
, const char *format
, ...)
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';
205 if (VERBOSE (handle
))
206 puts (handle
->error
);
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.
235 shishi_error_set_outputtype (Shishi
* handle
, int type
)
237 handle
->outputtype
= type
;
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.
249 shishi_info (Shishi
* handle
, const char *format
, ...)
255 va_start (ap
, format
);
256 vasprintf (&out
, format
, ap
);
258 type
= shishi_error_outputtype (handle
);
261 case SHISHI_OUTPUTTYPE_STDERR
:
262 fprintf (stderr
, _("libshishi: info: %s\n"), out
);
264 case SHISHI_OUTPUTTYPE_SYSLOG
:
265 syslog (LOG_ERR
, _("libshishi: info: %s"), out
);
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.
284 shishi_warn (Shishi
* handle
, const char *format
, ...)
290 va_start (ap
, format
);
291 vasprintf (&out
, format
, ap
);
293 type
= shishi_error_outputtype (handle
);
296 case SHISHI_OUTPUTTYPE_STDERR
:
297 fprintf (stderr
, _("libshishi: warning: %s\n"), out
);
299 case SHISHI_OUTPUTTYPE_SYSLOG
:
300 syslog (LOG_ERR
, _("libshishi: warning: %s"), out
);
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.
319 shishi_verbose (Shishi
* handle
, const char *format
, ...)
325 if (!VERBOSE (handle
))
328 va_start (ap
, format
);
329 vasprintf (&out
, format
, ap
);
331 type
= shishi_error_outputtype (handle
);
334 case SHISHI_OUTPUTTYPE_STDERR
:
335 fprintf (stderr
, "%s\n", out
);
337 case SHISHI_OUTPUTTYPE_SYSLOG
:
338 syslog (LOG_DEBUG
, "%s", out
);