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