* grp/compat-initgroups.c [NOT_IN_libc] (__libc_use_alloca): Define.
[glibc.git] / sunrpc / clnt_perr.c
blobc15796fa0e47d58dc92b161bf1f921fb866151fe
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
64 * Print reply error info
66 char *
67 clnt_sperror (CLIENT * rpch, const char *msg)
69 struct rpc_err e;
70 CLNT_GETERR (rpch, &e);
72 const char *errstr = clnt_sperrno (e.re_status);
74 char chrbuf[1024];
75 char *str;
76 char *tmpstr;
77 int res;
78 switch (e.re_status)
80 case RPC_SUCCESS:
81 case RPC_CANTENCODEARGS:
82 case RPC_CANTDECODERES:
83 case RPC_TIMEDOUT:
84 case RPC_PROGUNAVAIL:
85 case RPC_PROCUNAVAIL:
86 case RPC_CANTDECODEARGS:
87 case RPC_SYSTEMERROR:
88 case RPC_UNKNOWNHOST:
89 case RPC_UNKNOWNPROTO:
90 case RPC_PMAPFAILURE:
91 case RPC_PROGNOTREGISTERED:
92 case RPC_FAILED:
93 res = __asprintf (&str, "%s: %s\n", msg, errstr);
94 break;
96 case RPC_CANTSEND:
97 case RPC_CANTRECV:
98 res = __asprintf (&str, "%s: %s; errno = %s\n",
99 msg, errstr, __strerror_r (e.re_errno,
100 chrbuf, sizeof chrbuf));
101 break;
103 case RPC_VERSMISMATCH:
104 res = __asprintf (&str,
105 _("%s: %s; low version = %lu, high version = %lu"),
106 msg, errstr, e.re_vers.low, e.re_vers.high);
107 break;
109 case RPC_AUTHERROR:
110 tmpstr = auth_errmsg (e.re_why);
111 if (tmpstr != NULL)
112 res = __asprintf (&str, _("%s: %s; why = %s\n"), msg, errstr, tmpstr);
113 else
114 res = __asprintf (&str, _("\
115 %s: %s; why = (unknown authentication error - %d)\n"),
116 msg, errstr, (int) e.re_why);
117 break;
119 case RPC_PROGVERSMISMATCH:
120 res = __asprintf (&str,
121 _("%s: %s; low version = %lu, high version = %lu"),
122 msg, errstr, e.re_vers.low, e.re_vers.high);
123 break;
125 default: /* unknown */
126 res = __asprintf (&str, "%s: %s; s1 = %lu, s2 = %lu",
127 msg, errstr, e.re_lb.s1, e.re_lb.s2);
128 break;
131 if (res < 0)
132 return NULL;
134 char *oldbuf = buf;
135 buf = str;
136 free (oldbuf);
138 return str;
140 libc_hidden_def (clnt_sperror)
142 void
143 clnt_perror (CLIENT * rpch, const char *msg)
145 (void) __fxprintf (NULL, "%s", clnt_sperror (rpch, msg));
147 libc_hidden_def (clnt_perror)
150 struct rpc_errtab
152 enum clnt_stat status;
153 unsigned int message_off;
156 static const char rpc_errstr[] =
158 #define RPC_SUCCESS_IDX 0
159 N_("RPC: Success")
160 "\0"
161 #define RPC_CANTENCODEARGS_IDX (RPC_SUCCESS_IDX + sizeof "RPC: Success")
162 N_("RPC: Can't encode arguments")
163 "\0"
164 #define RPC_CANTDECODERES_IDX (RPC_CANTENCODEARGS_IDX \
165 + sizeof "RPC: Can't encode arguments")
166 N_("RPC: Can't decode result")
167 "\0"
168 #define RPC_CANTSEND_IDX (RPC_CANTDECODERES_IDX \
169 + sizeof "RPC: Can't decode result")
170 N_("RPC: Unable to send")
171 "\0"
172 #define RPC_CANTRECV_IDX (RPC_CANTSEND_IDX \
173 + sizeof "RPC: Unable to send")
174 N_("RPC: Unable to receive")
175 "\0"
176 #define RPC_TIMEDOUT_IDX (RPC_CANTRECV_IDX \
177 + sizeof "RPC: Unable to receive")
178 N_("RPC: Timed out")
179 "\0"
180 #define RPC_VERSMISMATCH_IDX (RPC_TIMEDOUT_IDX \
181 + sizeof "RPC: Timed out")
182 N_("RPC: Incompatible versions of RPC")
183 "\0"
184 #define RPC_AUTHERROR_IDX (RPC_VERSMISMATCH_IDX \
185 + sizeof "RPC: Incompatible versions of RPC")
186 N_("RPC: Authentication error")
187 "\0"
188 #define RPC_PROGUNAVAIL_IDX (RPC_AUTHERROR_IDX \
189 + sizeof "RPC: Authentication error")
190 N_("RPC: Program unavailable")
191 "\0"
192 #define RPC_PROGVERSMISMATCH_IDX (RPC_PROGUNAVAIL_IDX \
193 + sizeof "RPC: Program unavailable")
194 N_("RPC: Program/version mismatch")
195 "\0"
196 #define RPC_PROCUNAVAIL_IDX (RPC_PROGVERSMISMATCH_IDX \
197 + sizeof "RPC: Program/version mismatch")
198 N_("RPC: Procedure unavailable")
199 "\0"
200 #define RPC_CANTDECODEARGS_IDX (RPC_PROCUNAVAIL_IDX \
201 + sizeof "RPC: Procedure unavailable")
202 N_("RPC: Server can't decode arguments")
203 "\0"
204 #define RPC_SYSTEMERROR_IDX (RPC_CANTDECODEARGS_IDX \
205 + sizeof "RPC: Server can't decode arguments")
206 N_("RPC: Remote system error")
207 "\0"
208 #define RPC_UNKNOWNHOST_IDX (RPC_SYSTEMERROR_IDX \
209 + sizeof "RPC: Remote system error")
210 N_("RPC: Unknown host")
211 "\0"
212 #define RPC_UNKNOWNPROTO_IDX (RPC_UNKNOWNHOST_IDX \
213 + sizeof "RPC: Unknown host")
214 N_("RPC: Unknown protocol")
215 "\0"
216 #define RPC_PMAPFAILURE_IDX (RPC_UNKNOWNPROTO_IDX \
217 + sizeof "RPC: Unknown protocol")
218 N_("RPC: Port mapper failure")
219 "\0"
220 #define RPC_PROGNOTREGISTERED_IDX (RPC_PMAPFAILURE_IDX \
221 + sizeof "RPC: Port mapper failure")
222 N_("RPC: Program not registered")
223 "\0"
224 #define RPC_FAILED_IDX (RPC_PROGNOTREGISTERED_IDX \
225 + sizeof "RPC: Program not registered")
226 N_("RPC: Failed (unspecified error)")
229 static const struct rpc_errtab rpc_errlist[] =
231 { RPC_SUCCESS, RPC_SUCCESS_IDX },
232 { RPC_CANTENCODEARGS, RPC_CANTENCODEARGS_IDX },
233 { RPC_CANTDECODERES, RPC_CANTDECODERES_IDX },
234 { RPC_CANTSEND, RPC_CANTSEND_IDX },
235 { RPC_CANTRECV, RPC_CANTRECV_IDX },
236 { RPC_TIMEDOUT, RPC_TIMEDOUT_IDX },
237 { RPC_VERSMISMATCH, RPC_VERSMISMATCH_IDX },
238 { RPC_AUTHERROR, RPC_AUTHERROR_IDX },
239 { RPC_PROGUNAVAIL, RPC_PROGUNAVAIL_IDX },
240 { RPC_PROGVERSMISMATCH, RPC_PROGVERSMISMATCH_IDX },
241 { RPC_PROCUNAVAIL, RPC_PROCUNAVAIL_IDX },
242 { RPC_CANTDECODEARGS, RPC_CANTDECODEARGS_IDX },
243 { RPC_SYSTEMERROR, RPC_SYSTEMERROR_IDX },
244 { RPC_UNKNOWNHOST, RPC_UNKNOWNHOST_IDX },
245 { RPC_UNKNOWNPROTO, RPC_UNKNOWNPROTO_IDX },
246 { RPC_PMAPFAILURE, RPC_PMAPFAILURE_IDX },
247 { RPC_PROGNOTREGISTERED, RPC_PROGNOTREGISTERED_IDX },
248 { RPC_FAILED, RPC_FAILED_IDX }
253 * This interface for use by clntrpc
255 char *
256 clnt_sperrno (enum clnt_stat stat)
258 size_t i;
260 for (i = 0; i < sizeof (rpc_errlist) / sizeof (struct rpc_errtab); i++)
262 if (rpc_errlist[i].status == stat)
264 return _(rpc_errstr + rpc_errlist[i].message_off);
267 return _("RPC: (unknown error code)");
269 libc_hidden_def (clnt_sperrno)
271 void
272 clnt_perrno (enum clnt_stat num)
274 (void) __fxprintf (NULL, "%s", clnt_sperrno (num));
278 char *
279 clnt_spcreateerror (const char *msg)
281 struct rpc_createerr *ce = &get_rpc_createerr ();
283 char chrbuf[1024];
284 const char *connector = "";
285 const char *errstr = "";
286 switch (ce->cf_stat)
288 case RPC_PMAPFAILURE:
289 connector = " - ";
290 errstr = clnt_sperrno (ce->cf_error.re_status);
291 break;
293 case RPC_SYSTEMERROR:
294 connector = " - ";
295 errstr = __strerror_r (ce->cf_error.re_errno, chrbuf, sizeof chrbuf);
296 break;
298 default:
299 break;
302 char *str;
303 if (__asprintf (&str, "%s: %s%s%s\n",
304 msg, clnt_sperrno (ce->cf_stat), connector, errstr) < 0)
305 return NULL;
307 char *oldbuf = buf;
308 buf = str;
309 free (oldbuf);
311 return str;
313 libc_hidden_def (clnt_spcreateerror)
315 void
316 clnt_pcreateerror (const char *msg)
318 (void) __fxprintf (NULL, "%s", clnt_spcreateerror (msg));
321 struct auth_errtab
323 enum auth_stat status;
324 unsigned int message_off;
327 static const char auth_errstr[] =
329 #define AUTH_OK_IDX 0
330 N_("Authentication OK")
331 "\0"
332 #define AUTH_BADCRED_IDX (AUTH_OK_IDX + sizeof "Authentication OK")
333 N_("Invalid client credential")
334 "\0"
335 #define AUTH_REJECTEDCRED_IDX (AUTH_BADCRED_IDX \
336 + sizeof "Invalid client credential")
337 N_("Server rejected credential")
338 "\0"
339 #define AUTH_BADVERF_IDX (AUTH_REJECTEDCRED_IDX \
340 + sizeof "Server rejected credential")
341 N_("Invalid client verifier")
342 "\0"
343 #define AUTH_REJECTEDVERF_IDX (AUTH_BADVERF_IDX \
344 + sizeof "Invalid client verifier")
345 N_("Server rejected verifier")
346 "\0"
347 #define AUTH_TOOWEAK_IDX (AUTH_REJECTEDVERF_IDX \
348 + sizeof "Server rejected verifier")
349 N_("Client credential too weak")
350 "\0"
351 #define AUTH_INVALIDRESP_IDX (AUTH_TOOWEAK_IDX \
352 + sizeof "Client credential too weak")
353 N_("Invalid server verifier")
354 "\0"
355 #define AUTH_FAILED_IDX (AUTH_INVALIDRESP_IDX \
356 + sizeof "Invalid server verifier")
357 N_("Failed (unspecified error)")
360 static const struct auth_errtab auth_errlist[] =
362 { AUTH_OK, AUTH_OK_IDX },
363 { AUTH_BADCRED, AUTH_BADCRED_IDX },
364 { AUTH_REJECTEDCRED, AUTH_REJECTEDCRED_IDX },
365 { AUTH_BADVERF, AUTH_BADVERF_IDX },
366 { AUTH_REJECTEDVERF, AUTH_REJECTEDVERF_IDX },
367 { AUTH_TOOWEAK, AUTH_TOOWEAK_IDX },
368 { AUTH_INVALIDRESP, AUTH_INVALIDRESP_IDX },
369 { AUTH_FAILED, AUTH_FAILED_IDX }
372 static char *
373 internal_function
374 auth_errmsg (enum auth_stat stat)
376 size_t i;
378 for (i = 0; i < sizeof (auth_errlist) / sizeof (struct auth_errtab); i++)
380 if (auth_errlist[i].status == stat)
382 return _(auth_errstr + auth_errlist[i].message_off);
385 return NULL;
389 libc_freeres_fn (free_mem)
391 /* Not libc_freeres_ptr, since buf is a macro. */
392 free (buf);