Doc fix.
[shishi.git] / lib / error.c
bloba7d4b607d455b94f966d5613fbb85c2a302a9c04
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);
209 #define INFOSTR "libshishi: info: "
210 #define WARNSTR "libshishi: warning: "
213 * shishi_info:
214 * @handle: shishi handle as allocated by shishi_init().
215 * @format: printf style format string.
216 * @...: print style arguments.
218 * Print informational message to stderr.
220 void
221 shishi_info (Shishi * handle, const char *format, ...)
223 va_list ap;
224 va_start (ap, format);
226 fprintf (stderr, INFOSTR);
227 vfprintf (stderr, format, ap);
228 fprintf (stderr, "\n");
230 va_end (ap);
234 * shishi_warn:
235 * @handle: shishi handle as allocated by shishi_init().
236 * @format: printf style format string.
237 * @...: print style arguments.
239 * Print a warning to stderr.
241 void
242 shishi_warn (Shishi * handle, const char *format, ...)
244 va_list ap;
245 va_start (ap, format);
247 fprintf (stderr, WARNSTR);
248 vfprintf (stderr, format, ap);
249 fprintf (stderr, "\n");
251 va_end (ap);