Debian Codegraph @

extra/tirpc/libtirpc-1.3.5/src/clnt_perror.c

raw symbols SCIP
1 /*
2 * Copyright (c) 2009, Sun Microsystems, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of Sun Microsystems, Inc. nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * clnt_perror.c
31 *
32 * Copyright (C) 1984, Sun Microsystems, Inc.
33 *
34 */
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39 #include <rpc/rpc.h>
40 #include <rpc/types.h>
41 #include <rpc/auth.h>
42 #include <rpc/clnt.h>
43
44 static char *buf;
45
46 static char *_buf(void);
47 static char *auth_errmsg(enum auth_stat);
48 #define CLNT_PERROR_BUFLEN 256
49
50 static char *
51 _buf()
52 {
53
54 if (buf == 0)
55 buf = (char *)malloc(CLNT_PERROR_BUFLEN);
56 return (buf);
57 }
58
59 /*
60 * Print reply error info
61 */
62 char *
63 clnt_sperror(rpch, s)
64 CLIENT *rpch;
65 const char *s;
66 {
67 struct rpc_err e;
68 char *err;
69 char *str;
70 char *strstart;
71 size_t len, i;
72
73 if (rpch == NULL || s == NULL)
74 return(0);
75
76 str = _buf(); /* side effect: sets CLNT_PERROR_BUFLEN */
77 if (str == 0)
78 return (0);
79 len = CLNT_PERROR_BUFLEN;
80 strstart = str;
81 CLNT_GETERR(rpch, &e);
82
83 if (snprintf(str, len, "%s: ", s) > 0) {
84 i = strlen(str);
85 str += i;
86 len -= i;
87 }
88
89 (void)strncpy(str, clnt_sperrno(e.re_status), len - 1);
90 i = strlen(str);
91 str += i;
92 len -= i;
93
94 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;
109
110 case RPC_CANTSEND:
111 case RPC_CANTRECV:
112 snprintf(str, len, "; errno = %s", strerror(e.re_errno));
113 i = strlen(str);
114 if (i > 0) {
115 str += i;
116 len -= i;
117 }
118 break;
119
120 case RPC_VERSMISMATCH:
121 snprintf(str, len, "; low version = %u, high version = %u",
122 e.re_vers.low, e.re_vers.high);
123 i = strlen(str);
124 if (i > 0) {
125 str += i;
126 len -= i;
127 }
128 break;
129
130 case RPC_AUTHERROR:
131 err = auth_errmsg(e.re_why);
132 snprintf(str, len, "; why = ");
133 i = strlen(str);
134 if (i > 0) {
135 str += i;
136 len -= i;
137 }
138 if (err != NULL) {
139 snprintf(str, len, "%s",err);
140 } else {
141 snprintf(str, len,
142 "(unknown authentication error - %d)",
143 (int) e.re_why);
144 }
145 i = strlen(str);
146 if (i > 0) {
147 str += i;
148 len -= i;
149 }
150 break;
151
152 case RPC_PROGVERSMISMATCH:
153 snprintf(str, len, "; low version = %u, high version = %u",
154 e.re_vers.low, e.re_vers.high);
155 i = strlen(str);
156 if (i > 0) {
157 str += i;
158 len -= i;
159 }
160 break;
161
162 default: /* unknown */
163 snprintf(str, len, "; s1 = %u, s2 = %u",
164 e.re_lb.s1, e.re_lb.s2);
165 i = strlen(str);
166 if (i > 0) {
167 str += i;
168 len -= i;
169 }
170 break;
171 }
172 strstart[CLNT_PERROR_BUFLEN-1] = '\0';
173 return(strstart) ;
174 }
175
176 void
177 clnt_perror(rpch, s)
178 CLIENT *rpch;
179 const char *s;
180 {
181
182 if (rpch == NULL || s == NULL)
183 return;
184
185 (void) fprintf(stderr, "%s\n", clnt_sperror(rpch,s));
186 }
187
188 static const char *const rpc_errlist[] = {
189 "RPC: Success", /* 0 - RPC_SUCCESS */
190 "RPC: Can't encode arguments", /* 1 - RPC_CANTENCODEARGS */
191 "RPC: Can't decode result", /* 2 - RPC_CANTDECODERES */
192 "RPC: Unable to send", /* 3 - RPC_CANTSEND */
193 "RPC: Unable to receive", /* 4 - RPC_CANTRECV */
194 "RPC: Timed out", /* 5 - RPC_TIMEDOUT */
195 "RPC: Incompatible versions of RPC", /* 6 - RPC_VERSMISMATCH */
196 "RPC: Authentication error", /* 7 - RPC_AUTHERROR */
197 "RPC: Program unavailable", /* 8 - RPC_PROGUNAVAIL */
198 "RPC: Program/version mismatch", /* 9 - RPC_PROGVERSMISMATCH */
199 "RPC: Procedure unavailable", /* 10 - RPC_PROCUNAVAIL */
200 "RPC: Server can't decode arguments", /* 11 - RPC_CANTDECODEARGS */
201 "RPC: Remote system error", /* 12 - RPC_SYSTEMERROR */
202 "RPC: Unknown host", /* 13 - RPC_UNKNOWNHOST */
203 "RPC: Port mapper failure", /* 14 - RPC_PMAPFAILURE */
204 "RPC: Program not registered", /* 15 - RPC_PROGNOTREGISTERED */
205 "RPC: Failed (unspecified error)", /* 16 - RPC_FAILED */
206 "RPC: Unknown protocol" /* 17 - RPC_UNKNOWNPROTO */
207 };
208
209
210 /*
211 * This interface for use by clntrpc
212 */
213 char *
214 clnt_sperrno(stat)
215 enum clnt_stat stat;
216 {
217 unsigned int errnum = stat;
218
219 if (errnum < (sizeof(rpc_errlist)/sizeof(rpc_errlist[0])))
220 /* LINTED interface problem */
221 return (char *)rpc_errlist[errnum];
222
223 return ("RPC: (unknown error code)");
224 }
225
226 void
227 clnt_perrno(num)
228 enum clnt_stat num;
229 {
230 (void) fprintf(stderr, "%s\n", clnt_sperrno(num));
231 }
232
233
234 char *
235 clnt_spcreateerror(s)
236 const char *s;
237 {
238 char *str, *err;
239 size_t len, i;
240
241 if (s == NULL)
242 return(0);
243
244 str = _buf(); /* side effect: sets CLNT_PERROR_BUFLEN */
245 if (str == 0)
246 return(0);
247 len = CLNT_PERROR_BUFLEN;
248 snprintf(str, len, "%s: ", s);
249 i = strlen(str);
250 if (i > 0)
251 len -= i;
252 (void)strncat(str, clnt_sperrno(rpc_createerr.cf_stat), len - 1);
253 switch (rpc_createerr.cf_stat) {
254 case RPC_PMAPFAILURE:
255 (void) strncat(str, " - ", len - 1);
256 err = clnt_sperrno(rpc_createerr.cf_error.re_status);
257 if (err)
258 (void) strncat(str, err+5, len-5);
259 switch(rpc_createerr.cf_error.re_status) {
260 case RPC_CANTSEND:
261 case RPC_CANTRECV:
262 i = strlen(str);
263 len -= i;
264 snprintf(str+i, len, ": errno %d (%s)",
265 rpc_createerr.cf_error.re_errno,
266 strerror(rpc_createerr.cf_error.re_errno));
267 break;
268 default:
269 break;
270 }
271 break;
272
273 case RPC_SYSTEMERROR:
274 (void)strncat(str, " - ", len - 1);
275 (void)strncat(str, strerror(rpc_createerr.cf_error.re_errno),
276 len - 4);
277 break;
278
279 case RPC_CANTSEND:
280 case RPC_CANTDECODERES:
281 case RPC_CANTENCODEARGS:
282 case RPC_SUCCESS:
283 case RPC_UNKNOWNPROTO:
284 case RPC_PROGNOTREGISTERED:
285 case RPC_FAILED:
286 case RPC_UNKNOWNHOST:
287 case RPC_CANTDECODEARGS:
288 case RPC_PROCUNAVAIL:
289 case RPC_PROGVERSMISMATCH:
290 case RPC_PROGUNAVAIL:
291 case RPC_AUTHERROR:
292 case RPC_VERSMISMATCH:
293 case RPC_TIMEDOUT:
294 case RPC_CANTRECV:
295 default:
296 break;
297 }
298 str[CLNT_PERROR_BUFLEN-1] = '\0';
299 return (str);
300 }
301
302 void
303 clnt_pcreateerror(s)
304 const char *s;
305 {
306
307 if (s == NULL)
308 return;
309
310 (void) fprintf(stderr, "%s\n", clnt_spcreateerror(s));
311 }
312
313 static const char *const auth_errlist[] = {
314 "Authentication OK", /* 0 - AUTH_OK */
315 "Invalid client credential", /* 1 - AUTH_BADCRED */
316 "Server rejected credential", /* 2 - AUTH_REJECTEDCRED */
317 "Invalid client verifier", /* 3 - AUTH_BADVERF */
318 "Server rejected verifier", /* 4 - AUTH_REJECTEDVERF */
319 "Client credential too weak", /* 5 - AUTH_TOOWEAK */
320 "Invalid server verifier", /* 6 - AUTH_INVALIDRESP */
321 "Failed (unspecified error)" /* 7 - AUTH_FAILED */
322 };
323
324 static char *
325 auth_errmsg(stat)
326 enum auth_stat stat;
327 {
328 unsigned int errnum = stat;
329
330 if (errnum < (sizeof(auth_errlist)/sizeof(auth_errlist[0])))
331 /* LINTED interface problem */
332 return (char *)auth_errlist[errnum];
333
334 return(NULL);
335 }