Fix.
[shishi.git] / lib / error.c
blobdaec1b929fa259bc0eed34d1dbfff09cddbd0ca2
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 const char *
25 shishi_strerror_details (Shishi * handle)
27 return handle->error ? handle->
28 error :
29 "Internal application error: shishi_strerror() called without an "
30 "error condition";
33 struct shishi_error_msgs
35 int errorcode;
36 char *message;
39 struct shishi_error_msgs _shishi_error_messages[] = {
40 { SHISHI_OK,
41 "Shishi success" },
42 { SHISHI_ASN1_ERROR,
43 "Error in ASN.1 data, probably due to corrupt data." },
44 { SHISHI_FOPEN_ERROR,
45 "Could not open file." },
46 { SHISHI_FCLOSE_ERROR,
47 "Could not close file." },
48 { SHISHI_MALLOC_ERROR,
49 "Memory allocation error in shishi library." },
50 { SHISHI_BASE64_ERROR,
51 "Base64 encoding or decoding failed. Data corrupt?" },
52 { SHISHI_REALM_MISMATCH,
53 "Client realm value differ between request and reply." },
54 { SHISHI_CNAME_MISMATCH,
55 "Client name value differ between request and reply." },
56 { SHISHI_NONCE_MISMATCH,
57 "Replay protection value (nonce) differ between request and reply." },
58 { SHISHI_TICKET_BAD_KEYTYPE,
59 "Keytype used to encrypt ticket doesn't match provided key. "
60 "This usually indicates an internal application error." },
61 { SHISHI_CRYPTO_INTERNAL_ERROR,
62 "Internal error in low-level crypto routines." },
63 { SHISHI_CRYPTO_ERROR,
64 "Low-level cryptographic primitive failed. This usually indicates "
65 "bad password or data corruption." },
66 { SHISHI_KDC_TIMEOUT,
67 "Timedout talking to KDC. This usually indicates a network "
68 "or KDC address problem." },
69 { SHISHI_KDC_NOT_KNOWN_FOR_REALM,
70 "No KDC for realm known." },
71 { SHISHI_SOCKET_ERROR,
72 "The system call socket() failed. This usually indicates that "
73 "your system does not support the socket type." },
74 { SHISHI_BIND_ERROR,
75 "The system call bind() failed. This usually indicates "
76 "insufficient permissions." },
77 { SHISHI_SENDTO_ERROR,
78 "The system call sendto() failed." },
79 { SHISHI_CLOSE_ERROR,
80 "The system call close() failed." },
81 { SHISHI_GOT_KRBERROR,
82 "Server replied with an error message to request." },
83 { SHISHI_INVALID_TKTS,
84 "Ticketset not initialized. This usually indicates an internal "
85 "application error." },
86 { SHISHI_APREQ_DECRYPT_FAILED,
87 "Could not decrypt AP-REQ using provided key. "
88 "This usually indicates an internal application error." },
89 { SHISHI_TICKET_DECRYPT_FAILED,
90 "Could not decrypt Ticket using provided key. "
91 "This usually indicates an internal application error." },
92 { -1, NULL }
95 /**
96 * shishi_strerror:
97 * @err: shishi error code
99 * Return value: Returns a pointer to a statically allocated string
100 * containing a description of the error with the error value @err.
101 * This string can be used to output a diagnostic message to the user.
103 const char *
104 shishi_strerror (int err)
106 char *p = NULL;
107 size_t i;
109 for (i = 0; _shishi_error_messages[i].errorcode != -1; i++)
110 if (_shishi_error_messages[i].errorcode == err)
112 p = _(_shishi_error_messages[i].message);
113 break;
116 if (!p)
117 /* XXX mem leak */
118 asprintf(&p, _("Unknown shishi error: %d"), err);
120 return p;
124 void
125 shishi_error_clear (Shishi * handle)
127 handle->error[0] = '\0';
130 void
131 shishi_error_set (Shishi * handle, const char *error)
133 if (error)
135 strncpy (handle->error, error, sizeof (handle->error));
137 if (VERBOSE (handle))
138 puts (handle->error);
140 else
141 shishi_error_clear (handle);
144 void
145 shishi_error_printf (Shishi * handle, char *format, ...)
147 va_list ap;
148 char *s;
150 va_start (ap, format);
152 vasprintf (&s, format, ap);
153 strncpy (handle->error, s, sizeof (handle->error));
154 handle->error[sizeof (handle->error) - 1] = '\0';
155 free (s);
157 if (VERBOSE (handle))
158 puts (handle->error);
160 va_end (ap);
163 #define INFOSTR "libshishi: info: "
164 #define WARNSTR "libshishi: warning: "
167 * shishi_info:
168 * @handle: shishi handle as allocated by shishi_init().
169 * @fmt: printf style format string.
170 * @...: print style arguments.
172 * Print informational message to stderr.
174 void
175 shishi_info (Shishi * handle, const char *fmt, ...)
177 va_list ap;
178 va_start (ap, fmt);
180 fprintf (stderr, INFOSTR);
181 vfprintf (stderr, fmt, ap);
182 fprintf (stderr, "\n");
184 va_end (ap);
188 * shishi_warn:
189 * @handle: shishi handle as allocated by shishi_init().
190 * @fmt: printf style format string.
191 * @...: print style arguments.
193 * Print a warning to stderr.
195 void
196 shishi_warn (Shishi * handle, const char *fmt, ...)
198 va_list ap;
199 va_start (ap, fmt);
201 fprintf (stderr, WARNSTR);
202 vfprintf (stderr, fmt, ap);
203 fprintf (stderr, "\n");
205 va_end (ap);