Update.
[glibc.git] / sunrpc / clnt_perr.c
blob4fa5e62c0f8dc0cb440fd3c0bf1097ce7ea73d8b
1 /* @(#)clnt_perror.c 2.1 88/07/29 4.0 RPCSRC */
2 /*
3 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4 * unrestricted use provided that this legend is included on all tape
5 * media and as a part of the software program in whole or part. Users
6 * may copy or modify Sun RPC without charge, but are not authorized
7 * to license or distribute it to anyone else except as part of a product or
8 * program developed by the user.
10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 * Sun RPC is provided with no support and without any obligation on the
15 * part of Sun Microsystems, Inc. to assist in its use, correction,
16 * modification or enhancement.
18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20 * OR ANY PART THEREOF.
22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23 * or profits or other special, indirect and consequential damages, even if
24 * Sun has been advised of the possibility of such damages.
26 * Sun Microsystems, Inc.
27 * 2550 Garcia Avenue
28 * Mountain View, California 94043
30 #if !defined(lint) && defined(SCCSIDS)
31 static char sccsid[] = "@(#)clnt_perror.c 1.15 87/10/07 Copyr 1984 Sun Micro";
32 #endif
35 * clnt_perror.c
37 * Copyright (C) 1984, Sun Microsystems, Inc.
40 #include <stdio.h>
41 #include <string.h>
42 #include <libintl.h>
43 #include <rpc/rpc.h>
45 #ifdef USE_IN_LIBIO
46 # include <wchar.h>
47 # include <libio/iolibio.h>
48 #endif
50 static char *auth_errmsg (enum auth_stat stat) internal_function;
52 #ifdef _RPC_THREAD_SAFE_
54 * Making buf a preprocessor macro requires renaming the local
55 * buf variable in a few functions. Overriding a global variable
56 * with a local variable of the same name is a bad idea, anyway.
58 #define buf RPC_THREAD_VARIABLE(clnt_perr_buf_s)
59 #else
60 static char *buf;
61 #endif
63 static char *
64 _buf (void)
66 if (buf == NULL)
67 buf = (char *) malloc (256);
68 return buf;
72 * Print reply error info
74 char *
75 clnt_sperror (CLIENT * rpch, const char *msg)
77 char chrbuf[1024];
78 struct rpc_err e;
79 char *err;
80 char *str = _buf ();
81 char *strstart = str;
82 int len;
84 if (str == NULL)
85 return NULL;
86 CLNT_GETERR (rpch, &e);
88 len = sprintf (str, "%s: ", msg);
89 str += len;
91 str = stpcpy (str, clnt_sperrno (e.re_status));
93 switch (e.re_status)
95 case RPC_SUCCESS:
96 case RPC_CANTENCODEARGS:
97 case RPC_CANTDECODERES:
98 case RPC_TIMEDOUT:
99 case RPC_PROGUNAVAIL:
100 case RPC_PROCUNAVAIL:
101 case RPC_CANTDECODEARGS:
102 case RPC_SYSTEMERROR:
103 case RPC_UNKNOWNHOST:
104 case RPC_UNKNOWNPROTO:
105 case RPC_PMAPFAILURE:
106 case RPC_PROGNOTREGISTERED:
107 case RPC_FAILED:
108 break;
110 case RPC_CANTSEND:
111 case RPC_CANTRECV:
112 len = sprintf (str, "; errno = %s", __strerror_r (e.re_errno,
113 chrbuf, sizeof chrbuf));
114 str += len;
115 break;
117 case RPC_VERSMISMATCH:
118 len= sprintf (str, _("; low version = %lu, high version = %lu"),
119 e.re_vers.low, e.re_vers.high);
120 str += len;
121 break;
123 case RPC_AUTHERROR:
124 err = auth_errmsg (e.re_why);
125 str = stpcpy (str, _ ("; why = "));
126 if (err != NULL)
128 str = stpcpy (str, err);
130 else
132 len = sprintf (str, _("(unknown authentication error - %d)"),
133 (int) e.re_why);
134 str += len;
136 break;
138 case RPC_PROGVERSMISMATCH:
139 len = sprintf (str, _("; low version = %lu, high version = %lu"),
140 e.re_vers.low, e.re_vers.high);
141 str += len;
142 break;
144 default: /* unknown */
145 len = sprintf (str, "; s1 = %lu, s2 = %lu", e.re_lb.s1, e.re_lb.s2);
146 str += len;
147 break;
149 *str = '\n';
150 *++str = '\0';
151 return (strstart);
153 libc_hidden_def (clnt_sperror)
155 void
156 clnt_perror (CLIENT * rpch, const char *msg)
158 #ifdef USE_IN_LIBIO
159 if (_IO_fwide (stderr, 0) > 0)
160 (void) __fwprintf (stderr, L"%s", clnt_sperror (rpch, msg));
161 else
162 #endif
163 (void) fputs (clnt_sperror (rpch, msg), stderr);
165 libc_hidden_def (clnt_perror)
168 struct rpc_errtab
170 enum clnt_stat status;
171 unsigned int message_off;
174 static const char rpc_errstr[] =
176 #define RPC_SUCCESS_IDX 0
177 N_("RPC: Success")
178 "\0"
179 #define RPC_CANTENCODEARGS_IDX (RPC_SUCCESS_IDX + sizeof "RPC: Success")
180 N_("RPC: Can't encode arguments")
181 "\0"
182 #define RPC_CANTDECODERES_IDX (RPC_CANTENCODEARGS_IDX \
183 + sizeof "RPC: Can't encode arguments")
184 N_("RPC: Can't decode result")
185 "\0"
186 #define RPC_CANTSEND_IDX (RPC_CANTDECODERES_IDX \
187 + sizeof "RPC: Can't decode result")
188 N_("RPC: Unable to send")
189 "\0"
190 #define RPC_CANTRECV_IDX (RPC_CANTSEND_IDX \
191 + sizeof "RPC: Unable to send")
192 N_("RPC: Unable to receive")
193 "\0"
194 #define RPC_TIMEDOUT_IDX (RPC_CANTRECV_IDX \
195 + sizeof "RPC: Unable to receive")
196 N_("RPC: Timed out")
197 "\0"
198 #define RPC_VERSMISMATCH_IDX (RPC_TIMEDOUT_IDX \
199 + sizeof "RPC: Timed out")
200 N_("RPC: Incompatible versions of RPC")
201 "\0"
202 #define RPC_AUTHERROR_IDX (RPC_VERSMISMATCH_IDX \
203 + sizeof "RPC: Incompatible versions of RPC")
204 N_("RPC: Authentication error")
205 "\0"
206 #define RPC_PROGUNAVAIL_IDX (RPC_AUTHERROR_IDX \
207 + sizeof "RPC: Authentication error")
208 N_("RPC: Program unavailable")
209 "\0"
210 #define RPC_PROGVERSMISMATCH_IDX (RPC_PROGUNAVAIL_IDX \
211 + sizeof "RPC: Program unavailable")
212 N_("RPC: Program/version mismatch")
213 "\0"
214 #define RPC_PROCUNAVAIL_IDX (RPC_PROGVERSMISMATCH_IDX \
215 + sizeof "RPC: Program/version mismatch")
216 N_("RPC: Procedure unavailable")
217 "\0"
218 #define RPC_CANTDECODEARGS_IDX (RPC_PROCUNAVAIL_IDX \
219 + sizeof "RPC: Procedure unavailable")
220 N_("RPC: Server can't decode arguments")
221 "\0"
222 #define RPC_SYSTEMERROR_IDX (RPC_CANTDECODEARGS_IDX \
223 + sizeof "RPC: Server can't decode arguments")
224 N_("RPC: Remote system error")
225 "\0"
226 #define RPC_UNKNOWNHOST_IDX (RPC_SYSTEMERROR_IDX \
227 + sizeof "RPC: Remote system error")
228 N_("RPC: Unknown host")
229 "\0"
230 #define RPC_UNKNOWNPROTO_IDX (RPC_UNKNOWNHOST_IDX \
231 + sizeof "RPC: Unknown host")
232 N_("RPC: Unknown protocol")
233 "\0"
234 #define RPC_PMAPFAILURE_IDX (RPC_UNKNOWNPROTO_IDX \
235 + sizeof "RPC: Unknown protocol")
236 N_("RPC: Port mapper failure")
237 "\0"
238 #define RPC_PROGNOTREGISTERED_IDX (RPC_PMAPFAILURE_IDX \
239 + sizeof "RPC: Port mapper failure")
240 N_("RPC: Program not registered")
241 "\0"
242 #define RPC_FAILED_IDX (RPC_PROGNOTREGISTERED_IDX \
243 + sizeof "RPC: Program not registered")
244 N_("RPC: Failed (unspecified error)")
247 static const struct rpc_errtab rpc_errlist[] =
249 { RPC_SUCCESS, RPC_SUCCESS_IDX },
250 { RPC_CANTENCODEARGS, RPC_CANTENCODEARGS_IDX },
251 { RPC_CANTDECODERES, RPC_CANTDECODERES_IDX },
252 { RPC_CANTSEND, RPC_CANTSEND_IDX },
253 { RPC_CANTRECV, RPC_CANTRECV_IDX },
254 { RPC_TIMEDOUT, RPC_TIMEDOUT_IDX },
255 { RPC_VERSMISMATCH, RPC_VERSMISMATCH_IDX },
256 { RPC_AUTHERROR, RPC_AUTHERROR_IDX },
257 { RPC_PROGUNAVAIL, RPC_PROGUNAVAIL_IDX },
258 { RPC_PROGVERSMISMATCH, RPC_PROGVERSMISMATCH_IDX },
259 { RPC_PROCUNAVAIL, RPC_PROCUNAVAIL_IDX },
260 { RPC_CANTDECODEARGS, RPC_CANTDECODEARGS_IDX },
261 { RPC_SYSTEMERROR, RPC_SYSTEMERROR_IDX },
262 { RPC_UNKNOWNHOST, RPC_UNKNOWNHOST_IDX },
263 { RPC_UNKNOWNPROTO, RPC_UNKNOWNPROTO_IDX },
264 { RPC_PMAPFAILURE, RPC_PMAPFAILURE_IDX },
265 { RPC_PROGNOTREGISTERED, RPC_PROGNOTREGISTERED_IDX },
266 { RPC_FAILED, RPC_FAILED_IDX }
271 * This interface for use by clntrpc
273 char *
274 clnt_sperrno (enum clnt_stat stat)
276 size_t i;
278 for (i = 0; i < sizeof (rpc_errlist) / sizeof (struct rpc_errtab); i++)
280 if (rpc_errlist[i].status == stat)
282 return _(rpc_errstr + rpc_errlist[i].message_off);
285 return _("RPC: (unknown error code)");
287 libc_hidden_def (clnt_sperrno)
289 void
290 clnt_perrno (enum clnt_stat num)
292 #ifdef USE_IN_LIBIO
293 if (_IO_fwide (stderr, 0) > 0)
294 (void) __fwprintf (stderr, L"%s", clnt_sperrno (num));
295 else
296 #endif
297 (void) fputs (clnt_sperrno (num), stderr);
301 char *
302 clnt_spcreateerror (const char *msg)
304 char chrbuf[1024];
305 char *str = _buf ();
306 char *cp;
307 int len;
308 struct rpc_createerr *ce;
310 if (str == NULL)
311 return NULL;
312 ce = &get_rpc_createerr ();
313 len = sprintf (str, "%s: ", msg);
314 cp = str + len;
315 cp = stpcpy (cp, clnt_sperrno (ce->cf_stat));
316 switch (ce->cf_stat)
318 case RPC_PMAPFAILURE:
319 cp = stpcpy (stpcpy (cp, " - "),
320 clnt_sperrno (ce->cf_error.re_status));
321 break;
323 case RPC_SYSTEMERROR:
324 cp = stpcpy (stpcpy (cp, " - "),
325 __strerror_r (ce->cf_error.re_errno,
326 chrbuf, sizeof chrbuf));
327 break;
328 default:
329 break;
331 *cp = '\n';
332 *++cp = '\0';
333 return str;
335 libc_hidden_def (clnt_spcreateerror)
337 void
338 clnt_pcreateerror (const char *msg)
340 #ifdef USE_IN_LIBIO
341 if (_IO_fwide (stderr, 0) > 0)
342 (void) __fwprintf (stderr, L"%s", clnt_spcreateerror (msg));
343 else
344 #endif
345 (void) fputs (clnt_spcreateerror (msg), stderr);
348 struct auth_errtab
350 enum auth_stat status;
351 unsigned int message_off;
354 static const char auth_errstr[] =
356 #define AUTH_OK_IDX 0
357 N_("Authentication OK")
358 "\0"
359 #define AUTH_BADCRED_IDX (AUTH_OK_IDX + sizeof "Authentication OK")
360 N_("Invalid client credential")
361 "\0"
362 #define AUTH_REJECTEDCRED_IDX (AUTH_BADCRED_IDX \
363 + sizeof "Invalid client credential")
364 N_("Server rejected credential")
365 "\0"
366 #define AUTH_BADVERF_IDX (AUTH_REJECTEDCRED_IDX \
367 + sizeof "Server rejected credential")
368 N_("Invalid client verifier")
369 "\0"
370 #define AUTH_REJECTEDVERF_IDX (AUTH_BADVERF_IDX \
371 + sizeof "Invalid client verifier")
372 N_("Server rejected verifier")
373 "\0"
374 #define AUTH_TOOWEAK_IDX (AUTH_REJECTEDVERF_IDX \
375 + sizeof "Server rejected verifier")
376 N_("Client credential too weak")
377 "\0"
378 #define AUTH_INVALIDRESP_IDX (AUTH_TOOWEAK_IDX \
379 + sizeof "Client credential too weak")
380 N_("Invalid server verifier")
381 "\0"
382 #define AUTH_FAILED_IDX (AUTH_INVALIDRESP_IDX \
383 + sizeof "Invalid server verifier")
384 N_("Failed (unspecified error)")
387 static const struct auth_errtab auth_errlist[] =
389 { AUTH_OK, AUTH_OK_IDX },
390 { AUTH_BADCRED, AUTH_BADCRED_IDX },
391 { AUTH_REJECTEDCRED, AUTH_REJECTEDCRED_IDX },
392 { AUTH_BADVERF, AUTH_BADVERF_IDX },
393 { AUTH_REJECTEDVERF, AUTH_REJECTEDVERF_IDX },
394 { AUTH_TOOWEAK, AUTH_TOOWEAK_IDX },
395 { AUTH_INVALIDRESP, AUTH_INVALIDRESP_IDX },
396 { AUTH_FAILED, AUTH_FAILED_IDX }
399 static char *
400 internal_function
401 auth_errmsg (enum auth_stat stat)
403 size_t i;
405 for (i = 0; i < sizeof (auth_errlist) / sizeof (struct auth_errtab); i++)
407 if (auth_errlist[i].status == stat)
409 return _(auth_errstr + auth_errlist[i].message_off);
412 return NULL;
416 libc_freeres_fn (free_mem)
418 /* Not libc_freeres_ptr, since buf is a macro. */
419 free (buf);