xref: /freebsd/lib/libc/rpc/clnt_perror.c (revision 4f29da19bd44f0e99f021510460a81bf754c21d2)
1 /*	$NetBSD: clnt_perror.c,v 1.24 2000/06/02 23:11:07 fvdl Exp $	*/
2 
3 
4 /*
5  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
6  * unrestricted use provided that this legend is included on all tape
7  * media and as a part of the software program in whole or part.  Users
8  * may copy or modify Sun RPC without charge, but are not authorized
9  * to license or distribute it to anyone else except as part of a product or
10  * program developed by the user.
11  *
12  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
13  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
14  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
15  *
16  * Sun RPC is provided with no support and without any obligation on the
17  * part of Sun Microsystems, Inc. to assist in its use, correction,
18  * modification or enhancement.
19  *
20  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
21  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
22  * OR ANY PART THEREOF.
23  *
24  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
25  * or profits or other special, indirect and consequential damages, even if
26  * Sun has been advised of the possibility of such damages.
27  *
28  * Sun Microsystems, Inc.
29  * 2550 Garcia Avenue
30  * Mountain View, California  94043
31  */
32 
33 #if defined(LIBC_SCCS) && !defined(lint)
34 static char *sccsid2 = "@(#)clnt_perror.c 1.15 87/10/07 Copyr 1984 Sun Micro";
35 static char *sccsid = "@(#)clnt_perror.c	2.1 88/07/29 4.0 RPCSRC";
36 #endif
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 /*
41  * clnt_perror.c
42  *
43  * Copyright (C) 1984, Sun Microsystems, Inc.
44  *
45  */
46 #include "namespace.h"
47 #include <assert.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 
52 #include <rpc/rpc.h>
53 #include <rpc/types.h>
54 #include <rpc/auth.h>
55 #include <rpc/clnt.h>
56 #include "un-namespace.h"
57 
58 static char *buf;
59 
60 static char *_buf(void);
61 static char *auth_errmsg(enum auth_stat);
62 #define CLNT_PERROR_BUFLEN 256
63 
64 static char *
65 _buf()
66 {
67 
68 	if (buf == 0)
69 		buf = (char *)malloc(CLNT_PERROR_BUFLEN);
70 	return (buf);
71 }
72 
73 /*
74  * Print reply error info
75  */
76 char *
77 clnt_sperror(rpch, s)
78 	CLIENT *rpch;
79 	const char *s;
80 {
81 	struct rpc_err e;
82 	char *err;
83 	char *str;
84 	char *strstart;
85 	size_t len, i;
86 
87 	assert(rpch != NULL);
88 	assert(s != NULL);
89 
90 	str = _buf(); /* side effect: sets CLNT_PERROR_BUFLEN */
91 	if (str == 0)
92 		return (0);
93 	len = CLNT_PERROR_BUFLEN;
94 	strstart = str;
95 	CLNT_GETERR(rpch, &e);
96 
97 	if ((i = snprintf(str, len, "%s: ", s)) > 0) {
98 		str += i;
99 		len -= i;
100 	}
101 
102 	(void)strncpy(str, clnt_sperrno(e.re_status), len - 1);
103 	i = strlen(str);
104 	str += i;
105 	len -= i;
106 
107 	switch (e.re_status) {
108 	case RPC_SUCCESS:
109 	case RPC_CANTENCODEARGS:
110 	case RPC_CANTDECODERES:
111 	case RPC_TIMEDOUT:
112 	case RPC_PROGUNAVAIL:
113 	case RPC_PROCUNAVAIL:
114 	case RPC_CANTDECODEARGS:
115 	case RPC_SYSTEMERROR:
116 	case RPC_UNKNOWNHOST:
117 	case RPC_UNKNOWNPROTO:
118 	case RPC_PMAPFAILURE:
119 	case RPC_PROGNOTREGISTERED:
120 	case RPC_FAILED:
121 		break;
122 
123 	case RPC_CANTSEND:
124 	case RPC_CANTRECV:
125 		i = snprintf(str, len, "; errno = %s", strerror(e.re_errno));
126 		if (i > 0) {
127 			str += i;
128 			len -= i;
129 		}
130 		break;
131 
132 	case RPC_VERSMISMATCH:
133 		i = snprintf(str, len, "; low version = %u, high version = %u",
134 			e.re_vers.low, e.re_vers.high);
135 		if (i > 0) {
136 			str += i;
137 			len -= i;
138 		}
139 		break;
140 
141 	case RPC_AUTHERROR:
142 		err = auth_errmsg(e.re_why);
143 		i = snprintf(str, len, "; why = ");
144 		if (i > 0) {
145 			str += i;
146 			len -= i;
147 		}
148 		if (err != NULL) {
149 			i = snprintf(str, len, "%s",err);
150 		} else {
151 			i = snprintf(str, len,
152 				"(unknown authentication error - %d)",
153 				(int) e.re_why);
154 		}
155 		if (i > 0) {
156 			str += i;
157 			len -= i;
158 		}
159 		break;
160 
161 	case RPC_PROGVERSMISMATCH:
162 		i = snprintf(str, len, "; low version = %u, high version = %u",
163 			e.re_vers.low, e.re_vers.high);
164 		if (i > 0) {
165 			str += i;
166 			len -= i;
167 		}
168 		break;
169 
170 	default:	/* unknown */
171 		i = snprintf(str, len, "; s1 = %u, s2 = %u",
172 			e.re_lb.s1, e.re_lb.s2);
173 		if (i > 0) {
174 			str += i;
175 			len -= i;
176 		}
177 		break;
178 	}
179 	strstart[CLNT_PERROR_BUFLEN-1] = '\0';
180 	return(strstart) ;
181 }
182 
183 void
184 clnt_perror(rpch, s)
185 	CLIENT *rpch;
186 	const char *s;
187 {
188 
189 	assert(rpch != NULL);
190 	assert(s != NULL);
191 
192 	(void) fprintf(stderr, "%s\n", clnt_sperror(rpch,s));
193 }
194 
195 static const char *const rpc_errlist[] = {
196 	"RPC: Success",				/*  0 - RPC_SUCCESS */
197 	"RPC: Can't encode arguments",		/*  1 - RPC_CANTENCODEARGS */
198 	"RPC: Can't decode result",		/*  2 - RPC_CANTDECODERES */
199 	"RPC: Unable to send",			/*  3 - RPC_CANTSEND */
200 	"RPC: Unable to receive",		/*  4 - RPC_CANTRECV */
201 	"RPC: Timed out",			/*  5 - RPC_TIMEDOUT */
202 	"RPC: Incompatible versions of RPC",	/*  6 - RPC_VERSMISMATCH */
203 	"RPC: Authentication error",		/*  7 - RPC_AUTHERROR */
204 	"RPC: Program unavailable",		/*  8 - RPC_PROGUNAVAIL */
205 	"RPC: Program/version mismatch",	/*  9 - RPC_PROGVERSMISMATCH */
206 	"RPC: Procedure unavailable",		/* 10 - RPC_PROCUNAVAIL */
207 	"RPC: Server can't decode arguments",	/* 11 - RPC_CANTDECODEARGS */
208 	"RPC: Remote system error",		/* 12 - RPC_SYSTEMERROR */
209 	"RPC: Unknown host",			/* 13 - RPC_UNKNOWNHOST */
210 	"RPC: Port mapper failure",		/* 14 - RPC_PMAPFAILURE */
211 	"RPC: Program not registered",		/* 15 - RPC_PROGNOTREGISTERED */
212 	"RPC: Failed (unspecified error)",	/* 16 - RPC_FAILED */
213 	"RPC: Unknown protocol"			/* 17 - RPC_UNKNOWNPROTO */
214 };
215 
216 
217 /*
218  * This interface for use by clntrpc
219  */
220 char *
221 clnt_sperrno(stat)
222 	enum clnt_stat stat;
223 {
224 	unsigned int errnum = stat;
225 
226 	if (errnum < (sizeof(rpc_errlist)/sizeof(rpc_errlist[0])))
227 		/* LINTED interface problem */
228 		return (char *)rpc_errlist[errnum];
229 
230 	return ("RPC: (unknown error code)");
231 }
232 
233 void
234 clnt_perrno(num)
235 	enum clnt_stat num;
236 {
237 	(void) fprintf(stderr, "%s\n", clnt_sperrno(num));
238 }
239 
240 
241 char *
242 clnt_spcreateerror(s)
243 	const char *s;
244 {
245 	char *str;
246 	size_t len, i;
247 
248 	assert(s != NULL);
249 
250 	str = _buf(); /* side effect: sets CLNT_PERROR_BUFLEN */
251 	if (str == 0)
252 		return(0);
253 	len = CLNT_PERROR_BUFLEN;
254 	i = snprintf(str, len, "%s: ", s);
255 	if (i > 0)
256 		len -= i;
257 	(void)strncat(str, clnt_sperrno(rpc_createerr.cf_stat), len - 1);
258 	switch (rpc_createerr.cf_stat) {
259 	case RPC_PMAPFAILURE:
260 		(void) strncat(str, " - ", len - 1);
261 		(void) strncat(str,
262 		    clnt_sperrno(rpc_createerr.cf_error.re_status), len - 4);
263 		break;
264 
265 	case RPC_SYSTEMERROR:
266 		(void)strncat(str, " - ", len - 1);
267 		(void)strncat(str, strerror(rpc_createerr.cf_error.re_errno),
268 		    len - 4);
269 		break;
270 
271 	case RPC_CANTSEND:
272 	case RPC_CANTDECODERES:
273 	case RPC_CANTENCODEARGS:
274 	case RPC_SUCCESS:
275 	case RPC_UNKNOWNPROTO:
276 	case RPC_PROGNOTREGISTERED:
277 	case RPC_FAILED:
278 	case RPC_UNKNOWNHOST:
279 	case RPC_CANTDECODEARGS:
280 	case RPC_PROCUNAVAIL:
281 	case RPC_PROGVERSMISMATCH:
282 	case RPC_PROGUNAVAIL:
283 	case RPC_AUTHERROR:
284 	case RPC_VERSMISMATCH:
285 	case RPC_TIMEDOUT:
286 	case RPC_CANTRECV:
287 	default:
288 		break;
289 	}
290 	str[CLNT_PERROR_BUFLEN-1] = '\0';
291 	return (str);
292 }
293 
294 void
295 clnt_pcreateerror(s)
296 	const char *s;
297 {
298 
299 	assert(s != NULL);
300 
301 	(void) fprintf(stderr, "%s\n", clnt_spcreateerror(s));
302 }
303 
304 static const char *const auth_errlist[] = {
305 	"Authentication OK",			/* 0 - AUTH_OK */
306 	"Invalid client credential",		/* 1 - AUTH_BADCRED */
307 	"Server rejected credential",		/* 2 - AUTH_REJECTEDCRED */
308 	"Invalid client verifier", 		/* 3 - AUTH_BADVERF */
309 	"Server rejected verifier", 		/* 4 - AUTH_REJECTEDVERF */
310 	"Client credential too weak",		/* 5 - AUTH_TOOWEAK */
311 	"Invalid server verifier",		/* 6 - AUTH_INVALIDRESP */
312 	"Failed (unspecified error)"		/* 7 - AUTH_FAILED */
313 };
314 
315 static char *
316 auth_errmsg(stat)
317 	enum auth_stat stat;
318 {
319 	unsigned int errnum = stat;
320 
321 	if (errnum < (sizeof(auth_errlist)/sizeof(auth_errlist[0])))
322 		/* LINTED interface problem */
323 		return (char *)auth_errlist[errnum];
324 
325 	return(NULL);
326 }
327