<array_length.h>: New array_length and array_end macros
[glibc.git] / sunrpc / clnt_perr.c
blobcac1dd9913d8ab3bdd77a6ca1485efaaf11c5133
1 /*
2 * clnt_perror.c
4 * Copyright (c) 2010, 2011, Oracle America, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
16 * * Neither the name of the "Oracle America, Inc." nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <stdio.h>
34 #include <string.h>
35 #include <libintl.h>
36 #include <rpc/rpc.h>
37 #include <wchar.h>
38 #include <libio/iolibio.h>
39 #include <shlib-compat.h>
41 static char *auth_errmsg (enum auth_stat stat);
43 #ifdef _RPC_THREAD_SAFE_
45 * Making buf a preprocessor macro requires renaming the local
46 * buf variable in a few functions. Overriding a global variable
47 * with a local variable of the same name is a bad idea, anyway.
49 #define buf RPC_THREAD_VARIABLE(clnt_perr_buf_s)
50 #else
51 static char *buf;
52 #endif
55 * Print reply error info
57 char *
58 clnt_sperror (CLIENT * rpch, const char *msg)
60 struct rpc_err e;
61 CLNT_GETERR (rpch, &e);
63 const char *errstr = clnt_sperrno (e.re_status);
65 char chrbuf[1024];
66 char *str;
67 char *tmpstr;
68 int res;
69 switch (e.re_status)
71 case RPC_SUCCESS:
72 case RPC_CANTENCODEARGS:
73 case RPC_CANTDECODERES:
74 case RPC_TIMEDOUT:
75 case RPC_PROGUNAVAIL:
76 case RPC_PROCUNAVAIL:
77 case RPC_CANTDECODEARGS:
78 case RPC_SYSTEMERROR:
79 case RPC_UNKNOWNHOST:
80 case RPC_UNKNOWNPROTO:
81 case RPC_PMAPFAILURE:
82 case RPC_PROGNOTREGISTERED:
83 case RPC_FAILED:
84 res = __asprintf (&str, "%s: %s\n", msg, errstr);
85 break;
87 case RPC_CANTSEND:
88 case RPC_CANTRECV:
89 res = __asprintf (&str, "%s: %s; errno = %s\n",
90 msg, errstr, __strerror_r (e.re_errno,
91 chrbuf, sizeof chrbuf));
92 break;
94 case RPC_VERSMISMATCH:
95 res = __asprintf (&str,
96 _("%s: %s; low version = %lu, high version = %lu"),
97 msg, errstr, e.re_vers.low, e.re_vers.high);
98 break;
100 case RPC_AUTHERROR:
101 tmpstr = auth_errmsg (e.re_why);
102 if (tmpstr != NULL)
103 res = __asprintf (&str, _("%s: %s; why = %s\n"), msg, errstr, tmpstr);
104 else
105 res = __asprintf (&str, _("\
106 %s: %s; why = (unknown authentication error - %d)\n"),
107 msg, errstr, (int) e.re_why);
108 break;
110 case RPC_PROGVERSMISMATCH:
111 res = __asprintf (&str,
112 _("%s: %s; low version = %lu, high version = %lu"),
113 msg, errstr, e.re_vers.low, e.re_vers.high);
114 break;
116 default: /* unknown */
117 res = __asprintf (&str, "%s: %s; s1 = %lu, s2 = %lu",
118 msg, errstr, e.re_lb.s1, e.re_lb.s2);
119 break;
122 if (res < 0)
123 return NULL;
125 char *oldbuf = buf;
126 buf = str;
127 free (oldbuf);
129 return str;
131 libc_hidden_nolink_sunrpc (clnt_sperror, GLIBC_2_0)
133 void
134 clnt_perror (CLIENT * rpch, const char *msg)
136 (void) __fxprintf (NULL, "%s", clnt_sperror (rpch, msg));
138 #ifdef EXPORT_RPC_SYMBOLS
139 libc_hidden_def (clnt_perror)
140 #else
141 libc_hidden_nolink_sunrpc (clnt_perror, GLIBC_2_0)
142 #endif
145 struct rpc_errtab
147 enum clnt_stat status;
148 unsigned int message_off;
151 static const char rpc_errstr[] =
153 #define RPC_SUCCESS_IDX 0
154 N_("RPC: Success")
155 "\0"
156 #define RPC_CANTENCODEARGS_IDX (RPC_SUCCESS_IDX + sizeof "RPC: Success")
157 N_("RPC: Can't encode arguments")
158 "\0"
159 #define RPC_CANTDECODERES_IDX (RPC_CANTENCODEARGS_IDX \
160 + sizeof "RPC: Can't encode arguments")
161 N_("RPC: Can't decode result")
162 "\0"
163 #define RPC_CANTSEND_IDX (RPC_CANTDECODERES_IDX \
164 + sizeof "RPC: Can't decode result")
165 N_("RPC: Unable to send")
166 "\0"
167 #define RPC_CANTRECV_IDX (RPC_CANTSEND_IDX \
168 + sizeof "RPC: Unable to send")
169 N_("RPC: Unable to receive")
170 "\0"
171 #define RPC_TIMEDOUT_IDX (RPC_CANTRECV_IDX \
172 + sizeof "RPC: Unable to receive")
173 N_("RPC: Timed out")
174 "\0"
175 #define RPC_VERSMISMATCH_IDX (RPC_TIMEDOUT_IDX \
176 + sizeof "RPC: Timed out")
177 N_("RPC: Incompatible versions of RPC")
178 "\0"
179 #define RPC_AUTHERROR_IDX (RPC_VERSMISMATCH_IDX \
180 + sizeof "RPC: Incompatible versions of RPC")
181 N_("RPC: Authentication error")
182 "\0"
183 #define RPC_PROGUNAVAIL_IDX (RPC_AUTHERROR_IDX \
184 + sizeof "RPC: Authentication error")
185 N_("RPC: Program unavailable")
186 "\0"
187 #define RPC_PROGVERSMISMATCH_IDX (RPC_PROGUNAVAIL_IDX \
188 + sizeof "RPC: Program unavailable")
189 N_("RPC: Program/version mismatch")
190 "\0"
191 #define RPC_PROCUNAVAIL_IDX (RPC_PROGVERSMISMATCH_IDX \
192 + sizeof "RPC: Program/version mismatch")
193 N_("RPC: Procedure unavailable")
194 "\0"
195 #define RPC_CANTDECODEARGS_IDX (RPC_PROCUNAVAIL_IDX \
196 + sizeof "RPC: Procedure unavailable")
197 N_("RPC: Server can't decode arguments")
198 "\0"
199 #define RPC_SYSTEMERROR_IDX (RPC_CANTDECODEARGS_IDX \
200 + sizeof "RPC: Server can't decode arguments")
201 N_("RPC: Remote system error")
202 "\0"
203 #define RPC_UNKNOWNHOST_IDX (RPC_SYSTEMERROR_IDX \
204 + sizeof "RPC: Remote system error")
205 N_("RPC: Unknown host")
206 "\0"
207 #define RPC_UNKNOWNPROTO_IDX (RPC_UNKNOWNHOST_IDX \
208 + sizeof "RPC: Unknown host")
209 N_("RPC: Unknown protocol")
210 "\0"
211 #define RPC_PMAPFAILURE_IDX (RPC_UNKNOWNPROTO_IDX \
212 + sizeof "RPC: Unknown protocol")
213 N_("RPC: Port mapper failure")
214 "\0"
215 #define RPC_PROGNOTREGISTERED_IDX (RPC_PMAPFAILURE_IDX \
216 + sizeof "RPC: Port mapper failure")
217 N_("RPC: Program not registered")
218 "\0"
219 #define RPC_FAILED_IDX (RPC_PROGNOTREGISTERED_IDX \
220 + sizeof "RPC: Program not registered")
221 N_("RPC: Failed (unspecified error)")
224 static const struct rpc_errtab rpc_errlist[] =
226 { RPC_SUCCESS, RPC_SUCCESS_IDX },
227 { RPC_CANTENCODEARGS, RPC_CANTENCODEARGS_IDX },
228 { RPC_CANTDECODERES, RPC_CANTDECODERES_IDX },
229 { RPC_CANTSEND, RPC_CANTSEND_IDX },
230 { RPC_CANTRECV, RPC_CANTRECV_IDX },
231 { RPC_TIMEDOUT, RPC_TIMEDOUT_IDX },
232 { RPC_VERSMISMATCH, RPC_VERSMISMATCH_IDX },
233 { RPC_AUTHERROR, RPC_AUTHERROR_IDX },
234 { RPC_PROGUNAVAIL, RPC_PROGUNAVAIL_IDX },
235 { RPC_PROGVERSMISMATCH, RPC_PROGVERSMISMATCH_IDX },
236 { RPC_PROCUNAVAIL, RPC_PROCUNAVAIL_IDX },
237 { RPC_CANTDECODEARGS, RPC_CANTDECODEARGS_IDX },
238 { RPC_SYSTEMERROR, RPC_SYSTEMERROR_IDX },
239 { RPC_UNKNOWNHOST, RPC_UNKNOWNHOST_IDX },
240 { RPC_UNKNOWNPROTO, RPC_UNKNOWNPROTO_IDX },
241 { RPC_PMAPFAILURE, RPC_PMAPFAILURE_IDX },
242 { RPC_PROGNOTREGISTERED, RPC_PROGNOTREGISTERED_IDX },
243 { RPC_FAILED, RPC_FAILED_IDX }
248 * This interface for use by clntrpc
250 char *
251 clnt_sperrno (enum clnt_stat stat)
253 size_t i;
255 for (i = 0; i < sizeof (rpc_errlist) / sizeof (struct rpc_errtab); i++)
257 if (rpc_errlist[i].status == stat)
259 return _(rpc_errstr + rpc_errlist[i].message_off);
262 return _("RPC: (unknown error code)");
264 libc_hidden_def (clnt_sperrno)
266 void
267 clnt_perrno (enum clnt_stat num)
269 (void) __fxprintf (NULL, "%s", clnt_sperrno (num));
271 #ifdef EXPORT_RPC_SYMBOLS
272 libc_hidden_def (clnt_perrno)
273 #else
274 libc_hidden_nolink_sunrpc (clnt_perrno, GLIBC_2_0)
275 #endif
277 char *
278 clnt_spcreateerror (const char *msg)
280 struct rpc_createerr *ce = &get_rpc_createerr ();
282 char chrbuf[1024];
283 const char *connector = "";
284 const char *errstr = "";
285 switch (ce->cf_stat)
287 case RPC_PMAPFAILURE:
288 connector = " - ";
289 errstr = clnt_sperrno (ce->cf_error.re_status);
290 break;
292 case RPC_SYSTEMERROR:
293 connector = " - ";
294 errstr = __strerror_r (ce->cf_error.re_errno, chrbuf, sizeof chrbuf);
295 break;
297 default:
298 break;
301 char *str;
302 if (__asprintf (&str, "%s: %s%s%s\n",
303 msg, clnt_sperrno (ce->cf_stat), connector, errstr) < 0)
304 return NULL;
306 char *oldbuf = buf;
307 buf = str;
308 free (oldbuf);
310 return str;
312 libc_hidden_nolink_sunrpc (clnt_spcreateerror, GLIBC_2_0)
314 void
315 clnt_pcreateerror (const char *msg)
317 (void) __fxprintf (NULL, "%s", clnt_spcreateerror (msg));
319 #ifdef EXPORT_RPC_SYMBOLS
320 libc_hidden_def (clnt_pcreateerror)
321 #else
322 libc_hidden_nolink_sunrpc (clnt_pcreateerror, GLIBC_2_0)
323 #endif
325 struct auth_errtab
327 enum auth_stat status;
328 unsigned int message_off;
331 static const char auth_errstr[] =
333 #define AUTH_OK_IDX 0
334 N_("Authentication OK")
335 "\0"
336 #define AUTH_BADCRED_IDX (AUTH_OK_IDX + sizeof "Authentication OK")
337 N_("Invalid client credential")
338 "\0"
339 #define AUTH_REJECTEDCRED_IDX (AUTH_BADCRED_IDX \
340 + sizeof "Invalid client credential")
341 N_("Server rejected credential")
342 "\0"
343 #define AUTH_BADVERF_IDX (AUTH_REJECTEDCRED_IDX \
344 + sizeof "Server rejected credential")
345 N_("Invalid client verifier")
346 "\0"
347 #define AUTH_REJECTEDVERF_IDX (AUTH_BADVERF_IDX \
348 + sizeof "Invalid client verifier")
349 N_("Server rejected verifier")
350 "\0"
351 #define AUTH_TOOWEAK_IDX (AUTH_REJECTEDVERF_IDX \
352 + sizeof "Server rejected verifier")
353 N_("Client credential too weak")
354 "\0"
355 #define AUTH_INVALIDRESP_IDX (AUTH_TOOWEAK_IDX \
356 + sizeof "Client credential too weak")
357 N_("Invalid server verifier")
358 "\0"
359 #define AUTH_FAILED_IDX (AUTH_INVALIDRESP_IDX \
360 + sizeof "Invalid server verifier")
361 N_("Failed (unspecified error)")
364 static const struct auth_errtab auth_errlist[] =
366 { AUTH_OK, AUTH_OK_IDX },
367 { AUTH_BADCRED, AUTH_BADCRED_IDX },
368 { AUTH_REJECTEDCRED, AUTH_REJECTEDCRED_IDX },
369 { AUTH_BADVERF, AUTH_BADVERF_IDX },
370 { AUTH_REJECTEDVERF, AUTH_REJECTEDVERF_IDX },
371 { AUTH_TOOWEAK, AUTH_TOOWEAK_IDX },
372 { AUTH_INVALIDRESP, AUTH_INVALIDRESP_IDX },
373 { AUTH_FAILED, AUTH_FAILED_IDX }
376 static char *
377 auth_errmsg (enum auth_stat stat)
379 size_t i;
381 for (i = 0; i < sizeof (auth_errlist) / sizeof (struct auth_errtab); i++)
383 if (auth_errlist[i].status == stat)
385 return _(auth_errstr + auth_errlist[i].message_off);
388 return NULL;
392 libc_freeres_fn (free_mem)
394 /* Not libc_freeres_ptr, since buf is a macro. */
395 free (buf);