1 /*
2 * clnt_perror.c
3 *
4 * Copyright (c) 2010, 2011, Oracle America, Inc.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
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.
19 *
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.
32 */
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>
40
41 static char *auth_errmsg (enum auth_stat stat);
42
43 /*
44 * Making buf a preprocessor macro requires renaming the local
45 * buf variable in a few functions. Overriding a global variable
46 * with a local variable of the same name is a bad idea, anyway.
47 */
48 #define buf RPC_THREAD_VARIABLE(clnt_perr_buf_s)
49
50 /*
51 * Print reply error info
52 */
53 char *
clnt_sperror(CLIENT * rpch,const char * msg)54 clnt_sperror (CLIENT * rpch, const char *msg)
55 {
56 struct rpc_err e;
57 CLNT_GETERR (rpch, &e);
58
59 const char *errstr = clnt_sperrno (e.re_status);
60
61 char chrbuf[1024];
62 char *str;
63 char *tmpstr;
64 int res;
65 switch (e.re_status)
66 {
67 case RPC_SUCCESS:
68 case RPC_CANTENCODEARGS:
69 case RPC_CANTDECODERES:
70 case RPC_TIMEDOUT:
71 case RPC_PROGUNAVAIL:
72 case RPC_PROCUNAVAIL:
73 case RPC_CANTDECODEARGS:
74 case RPC_SYSTEMERROR:
75 case RPC_UNKNOWNHOST:
76 case RPC_UNKNOWNPROTO:
77 case RPC_PMAPFAILURE:
78 case RPC_PROGNOTREGISTERED:
79 case RPC_FAILED:
80 res = __asprintf (&str, "%s: %s\n", msg, errstr);
81 break;
82
83 case RPC_CANTSEND:
84 case RPC_CANTRECV:
85 res = __asprintf (&str, "%s: %s; errno = %s\n",
86 msg, errstr, __strerror_r (e.re_errno,
87 chrbuf, sizeof chrbuf));
88 break;
89
90 case RPC_VERSMISMATCH:
91 res = __asprintf (&str,
92 _("%s: %s; low version = %lu, high version = %lu"),
93 msg, errstr, e.re_vers.low, e.re_vers.high);
94 break;
95
96 case RPC_AUTHERROR:
97 tmpstr = auth_errmsg (e.re_why);
98 if (tmpstr != NULL)
99 res = __asprintf (&str, _("%s: %s; why = %s\n"), msg, errstr, tmpstr);
100 else
101 res = __asprintf (&str, _("\
102 %s: %s; why = (unknown authentication error - %d)\n"),
103 msg, errstr, (int) e.re_why);
104 break;
105
106 case RPC_PROGVERSMISMATCH:
107 res = __asprintf (&str,
108 _("%s: %s; low version = %lu, high version = %lu"),
109 msg, errstr, e.re_vers.low, e.re_vers.high);
110 break;
111
112 default: /* unknown */
113 res = __asprintf (&str, "%s: %s; s1 = %lu, s2 = %lu",
114 msg, errstr, e.re_lb.s1, e.re_lb.s2);
115 break;
116 }
117
118 if (res < 0)
119 return NULL;
120
121 char *oldbuf = buf;
122 buf = str;
123 free (oldbuf);
124
125 return str;
126 }
libc_hidden_nolink_sunrpc(clnt_sperror,GLIBC_2_0)127 libc_hidden_nolink_sunrpc (clnt_sperror, GLIBC_2_0)
128
129 void
130 clnt_perror (CLIENT * rpch, const char *msg)
131 {
132 (void) __fxprintf (NULL, "%s", clnt_sperror (rpch, msg));
133 }
134 #ifdef EXPORT_RPC_SYMBOLS
135 libc_hidden_def (clnt_perror)
136 #else
137 libc_hidden_nolink_sunrpc (clnt_perror, GLIBC_2_0)
138 #endif
139
140
141 struct rpc_errtab
142 {
143 enum clnt_stat status;
144 unsigned int message_off;
145 };
146
147 static const char rpc_errstr[] =
148 {
149 #define RPC_SUCCESS_IDX 0
150 N_("RPC: Success")
151 "\0"
152 #define RPC_CANTENCODEARGS_IDX (RPC_SUCCESS_IDX + sizeof "RPC: Success")
153 N_("RPC: Can't encode arguments")
154 "\0"
155 #define RPC_CANTDECODERES_IDX (RPC_CANTENCODEARGS_IDX \
156 + sizeof "RPC: Can't encode arguments")
157 N_("RPC: Can't decode result")
158 "\0"
159 #define RPC_CANTSEND_IDX (RPC_CANTDECODERES_IDX \
160 + sizeof "RPC: Can't decode result")
161 N_("RPC: Unable to send")
162 "\0"
163 #define RPC_CANTRECV_IDX (RPC_CANTSEND_IDX \
164 + sizeof "RPC: Unable to send")
165 N_("RPC: Unable to receive")
166 "\0"
167 #define RPC_TIMEDOUT_IDX (RPC_CANTRECV_IDX \
168 + sizeof "RPC: Unable to receive")
169 N_("RPC: Timed out")
170 "\0"
171 #define RPC_VERSMISMATCH_IDX (RPC_TIMEDOUT_IDX \
172 + sizeof "RPC: Timed out")
173 N_("RPC: Incompatible versions of RPC")
174 "\0"
175 #define RPC_AUTHERROR_IDX (RPC_VERSMISMATCH_IDX \
176 + sizeof "RPC: Incompatible versions of RPC")
177 N_("RPC: Authentication error")
178 "\0"
179 #define RPC_PROGUNAVAIL_IDX (RPC_AUTHERROR_IDX \
180 + sizeof "RPC: Authentication error")
181 N_("RPC: Program unavailable")
182 "\0"
183 #define RPC_PROGVERSMISMATCH_IDX (RPC_PROGUNAVAIL_IDX \
184 + sizeof "RPC: Program unavailable")
185 N_("RPC: Program/version mismatch")
186 "\0"
187 #define RPC_PROCUNAVAIL_IDX (RPC_PROGVERSMISMATCH_IDX \
188 + sizeof "RPC: Program/version mismatch")
189 N_("RPC: Procedure unavailable")
190 "\0"
191 #define RPC_CANTDECODEARGS_IDX (RPC_PROCUNAVAIL_IDX \
192 + sizeof "RPC: Procedure unavailable")
193 N_("RPC: Server can't decode arguments")
194 "\0"
195 #define RPC_SYSTEMERROR_IDX (RPC_CANTDECODEARGS_IDX \
196 + sizeof "RPC: Server can't decode arguments")
197 N_("RPC: Remote system error")
198 "\0"
199 #define RPC_UNKNOWNHOST_IDX (RPC_SYSTEMERROR_IDX \
200 + sizeof "RPC: Remote system error")
201 N_("RPC: Unknown host")
202 "\0"
203 #define RPC_UNKNOWNPROTO_IDX (RPC_UNKNOWNHOST_IDX \
204 + sizeof "RPC: Unknown host")
205 N_("RPC: Unknown protocol")
206 "\0"
207 #define RPC_PMAPFAILURE_IDX (RPC_UNKNOWNPROTO_IDX \
208 + sizeof "RPC: Unknown protocol")
209 N_("RPC: Port mapper failure")
210 "\0"
211 #define RPC_PROGNOTREGISTERED_IDX (RPC_PMAPFAILURE_IDX \
212 + sizeof "RPC: Port mapper failure")
213 N_("RPC: Program not registered")
214 "\0"
215 #define RPC_FAILED_IDX (RPC_PROGNOTREGISTERED_IDX \
216 + sizeof "RPC: Program not registered")
217 N_("RPC: Failed (unspecified error)")
218 };
219
220 static const struct rpc_errtab rpc_errlist[] =
221 {
222 { RPC_SUCCESS, RPC_SUCCESS_IDX },
223 { RPC_CANTENCODEARGS, RPC_CANTENCODEARGS_IDX },
224 { RPC_CANTDECODERES, RPC_CANTDECODERES_IDX },
225 { RPC_CANTSEND, RPC_CANTSEND_IDX },
226 { RPC_CANTRECV, RPC_CANTRECV_IDX },
227 { RPC_TIMEDOUT, RPC_TIMEDOUT_IDX },
228 { RPC_VERSMISMATCH, RPC_VERSMISMATCH_IDX },
229 { RPC_AUTHERROR, RPC_AUTHERROR_IDX },
230 { RPC_PROGUNAVAIL, RPC_PROGUNAVAIL_IDX },
231 { RPC_PROGVERSMISMATCH, RPC_PROGVERSMISMATCH_IDX },
232 { RPC_PROCUNAVAIL, RPC_PROCUNAVAIL_IDX },
233 { RPC_CANTDECODEARGS, RPC_CANTDECODEARGS_IDX },
234 { RPC_SYSTEMERROR, RPC_SYSTEMERROR_IDX },
235 { RPC_UNKNOWNHOST, RPC_UNKNOWNHOST_IDX },
236 { RPC_UNKNOWNPROTO, RPC_UNKNOWNPROTO_IDX },
237 { RPC_PMAPFAILURE, RPC_PMAPFAILURE_IDX },
238 { RPC_PROGNOTREGISTERED, RPC_PROGNOTREGISTERED_IDX },
239 { RPC_FAILED, RPC_FAILED_IDX }
240 };
241
242
243 /*
244 * This interface for use by clntrpc
245 */
246 char *
clnt_sperrno(enum clnt_stat stat)247 clnt_sperrno (enum clnt_stat stat)
248 {
249 size_t i;
250
251 for (i = 0; i < sizeof (rpc_errlist) / sizeof (struct rpc_errtab); i++)
252 {
253 if (rpc_errlist[i].status == stat)
254 {
255 return _(rpc_errstr + rpc_errlist[i].message_off);
256 }
257 }
258 return _("RPC: (unknown error code)");
259 }
260 #ifdef EXPORT_RPC_SYMBOLS
libc_hidden_def(clnt_sperrno)261 libc_hidden_def (clnt_sperrno)
262 #else
263 libc_hidden_nolink_sunrpc (clnt_sperrno, GLIBC_2_0)
264 #endif
265
266 void
267 clnt_perrno (enum clnt_stat num)
268 {
269 (void) __fxprintf (NULL, "%s", clnt_sperrno (num));
270 }
271 #ifdef EXPORT_RPC_SYMBOLS
libc_hidden_def(clnt_perrno)272 libc_hidden_def (clnt_perrno)
273 #else
274 libc_hidden_nolink_sunrpc (clnt_perrno, GLIBC_2_0)
275 #endif
276
277 char *
278 clnt_spcreateerror (const char *msg)
279 {
280 struct rpc_createerr *ce = &get_rpc_createerr ();
281
282 char chrbuf[1024];
283 const char *connector = "";
284 const char *errstr = "";
285 switch (ce->cf_stat)
286 {
287 case RPC_PMAPFAILURE:
288 connector = " - ";
289 errstr = clnt_sperrno (ce->cf_error.re_status);
290 break;
291
292 case RPC_SYSTEMERROR:
293 connector = " - ";
294 errstr = __strerror_r (ce->cf_error.re_errno, chrbuf, sizeof chrbuf);
295 break;
296
297 default:
298 break;
299 }
300
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;
305
306 char *oldbuf = buf;
307 buf = str;
308 free (oldbuf);
309
310 return str;
311 }
libc_hidden_nolink_sunrpc(clnt_spcreateerror,GLIBC_2_0)312 libc_hidden_nolink_sunrpc (clnt_spcreateerror, GLIBC_2_0)
313
314 void
315 clnt_pcreateerror (const char *msg)
316 {
317 (void) __fxprintf (NULL, "%s", clnt_spcreateerror (msg));
318 }
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
324
325 struct auth_errtab
326 {
327 enum auth_stat status;
328 unsigned int message_off;
329 };
330
331 static const char auth_errstr[] =
332 {
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)")
362 };
363
364 static const struct auth_errtab auth_errlist[] =
365 {
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 }
374 };
375
376 static char *
auth_errmsg(enum auth_stat stat)377 auth_errmsg (enum auth_stat stat)
378 {
379 size_t i;
380
381 for (i = 0; i < sizeof (auth_errlist) / sizeof (struct auth_errtab); i++)
382 {
383 if (auth_errlist[i].status == stat)
384 {
385 return _(auth_errstr + auth_errlist[i].message_off);
386 }
387 }
388 return NULL;
389 }
390
391
libc_freeres_fn(free_mem)392 libc_freeres_fn (free_mem)
393 {
394 /* Not libc_freeres_ptr, since buf is a macro. */
395 free (buf);
396 }
397