xref: /freebsd/lib/libc/rpc/clnt_perror.c (revision 5521ff5a4d1929056e7ffc982fac3341ca54df7c)
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 #include <sys/cdefs.h>
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char *sccsid = "@(#)clnt_perror.c 1.15 87/10/07 Copyr 1984 Sun Micro";
36 static char *sccsid = "@(#)clnt_perror.c	2.1 88/07/29 4.0 RPCSRC";
37 static char *rcsid = "$FreeBSD$";
38 #endif
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 __P((void));
61 static char *auth_errmsg __P((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 	i = snprintf(str, len, "%s: ", s);
98 	str += i;
99 	len -= i;
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 		str += i;
126 		len -= i;
127 		break;
128 
129 	case RPC_VERSMISMATCH:
130 		i = snprintf(str, len, "; low version = %u, high version = %u",
131 			e.re_vers.low, e.re_vers.high);
132 		str += i;
133 		len -= i;
134 		break;
135 
136 	case RPC_AUTHERROR:
137 		err = auth_errmsg(e.re_why);
138 		i = snprintf(str, len, "; why = ");
139 		str += i;
140 		len -= i;
141 		if (err != NULL) {
142 			i = snprintf(str, len, "%s",err);
143 		} else {
144 			i = snprintf(str, len,
145 				"(unknown authentication error - %d)",
146 				(int) e.re_why);
147 		}
148 		str += i;
149 		len -= i;
150 		break;
151 
152 	case RPC_PROGVERSMISMATCH:
153 		i = snprintf(str, len, "; low version = %u, high version = %u",
154 			e.re_vers.low, e.re_vers.high);
155 		str += i;
156 		len -= i;
157 		break;
158 
159 	default:	/* unknown */
160 		i = snprintf(str, len, "; s1 = %u, s2 = %u",
161 			e.re_lb.s1, e.re_lb.s2);
162 		str += i;
163 		len -= i;
164 		break;
165 	}
166 	strstart[CLNT_PERROR_BUFLEN-1] = '\0';
167 	return(strstart) ;
168 }
169 
170 void
171 clnt_perror(rpch, s)
172 	CLIENT *rpch;
173 	const char *s;
174 {
175 
176 	assert(rpch != NULL);
177 	assert(s != NULL);
178 
179 	(void) fprintf(stderr, "%s\n", clnt_sperror(rpch,s));
180 }
181 
182 static const char *const rpc_errlist[] = {
183 	"RPC: Success",				/*  0 - RPC_SUCCESS */
184 	"RPC: Can't encode arguments",		/*  1 - RPC_CANTENCODEARGS */
185 	"RPC: Can't decode result",		/*  2 - RPC_CANTDECODERES */
186 	"RPC: Unable to send",			/*  3 - RPC_CANTSEND */
187 	"RPC: Unable to receive",		/*  4 - RPC_CANTRECV */
188 	"RPC: Timed out",			/*  5 - RPC_TIMEDOUT */
189 	"RPC: Incompatible versions of RPC",	/*  6 - RPC_VERSMISMATCH */
190 	"RPC: Authentication error",		/*  7 - RPC_AUTHERROR */
191 	"RPC: Program unavailable",		/*  8 - RPC_PROGUNAVAIL */
192 	"RPC: Program/version mismatch",	/*  9 - RPC_PROGVERSMISMATCH */
193 	"RPC: Procedure unavailable",		/* 10 - RPC_PROCUNAVAIL */
194 	"RPC: Server can't decode arguments",	/* 11 - RPC_CANTDECODEARGS */
195 	"RPC: Remote system error",		/* 12 - RPC_SYSTEMERROR */
196 	"RPC: Unknown host",			/* 13 - RPC_UNKNOWNHOST */
197 	"RPC: Port mapper failure",		/* 14 - RPC_PMAPFAILURE */
198 	"RPC: Program not registered",		/* 15 - RPC_PROGNOTREGISTERED */
199 	"RPC: Failed (unspecified error)",	/* 16 - RPC_FAILED */
200 	"RPC: Unknown protocol"			/* 17 - RPC_UNKNOWNPROTO */
201 };
202 
203 
204 /*
205  * This interface for use by clntrpc
206  */
207 char *
208 clnt_sperrno(stat)
209 	enum clnt_stat stat;
210 {
211 	unsigned int errnum = stat;
212 
213 	if (errnum < (sizeof(rpc_errlist)/sizeof(rpc_errlist[0])))
214 		/* LINTED interface problem */
215 		return (char *)rpc_errlist[errnum];
216 
217 	return ("RPC: (unknown error code)");
218 }
219 
220 void
221 clnt_perrno(num)
222 	enum clnt_stat num;
223 {
224 	(void) fprintf(stderr, "%s\n", clnt_sperrno(num));
225 }
226 
227 
228 char *
229 clnt_spcreateerror(s)
230 	const char *s;
231 {
232 	char *str;
233 	size_t len, i;
234 
235 	assert(s != NULL);
236 
237 	str = _buf(); /* side effect: sets CLNT_PERROR_BUFLEN */
238 	if (str == 0)
239 		return(0);
240 	len = CLNT_PERROR_BUFLEN;
241 	i = snprintf(str, len, "%s: ", s);
242 	len -= i;
243 	(void)strncat(str, clnt_sperrno(rpc_createerr.cf_stat), len - 1);
244 	switch (rpc_createerr.cf_stat) {
245 	case RPC_PMAPFAILURE:
246 		(void) strncat(str, " - ", len - 1);
247 		(void) strncat(str,
248 		    clnt_sperrno(rpc_createerr.cf_error.re_status), len - 4);
249 		break;
250 
251 	case RPC_SYSTEMERROR:
252 		(void)strncat(str, " - ", len - 1);
253 		(void)strncat(str, strerror(rpc_createerr.cf_error.re_errno),
254 		    len - 4);
255 		break;
256 
257 	case RPC_CANTSEND:
258 	case RPC_CANTDECODERES:
259 	case RPC_CANTENCODEARGS:
260 	case RPC_SUCCESS:
261 	case RPC_UNKNOWNPROTO:
262 	case RPC_PROGNOTREGISTERED:
263 	case RPC_FAILED:
264 	case RPC_UNKNOWNHOST:
265 	case RPC_CANTDECODEARGS:
266 	case RPC_PROCUNAVAIL:
267 	case RPC_PROGVERSMISMATCH:
268 	case RPC_PROGUNAVAIL:
269 	case RPC_AUTHERROR:
270 	case RPC_VERSMISMATCH:
271 	case RPC_TIMEDOUT:
272 	case RPC_CANTRECV:
273 	default:
274 		break;
275 	}
276 	str[CLNT_PERROR_BUFLEN-1] = '\0';
277 	return (str);
278 }
279 
280 void
281 clnt_pcreateerror(s)
282 	const char *s;
283 {
284 
285 	assert(s != NULL);
286 
287 	(void) fprintf(stderr, "%s\n", clnt_spcreateerror(s));
288 }
289 
290 static const char *const auth_errlist[] = {
291 	"Authentication OK",			/* 0 - AUTH_OK */
292 	"Invalid client credential",		/* 1 - AUTH_BADCRED */
293 	"Server rejected credential",		/* 2 - AUTH_REJECTEDCRED */
294 	"Invalid client verifier", 		/* 3 - AUTH_BADVERF */
295 	"Server rejected verifier", 		/* 4 - AUTH_REJECTEDVERF */
296 	"Client credential too weak",		/* 5 - AUTH_TOOWEAK */
297 	"Invalid server verifier",		/* 6 - AUTH_INVALIDRESP */
298 	"Failed (unspecified error)"		/* 7 - AUTH_FAILED */
299 };
300 
301 static char *
302 auth_errmsg(stat)
303 	enum auth_stat stat;
304 {
305 	unsigned int errnum = stat;
306 
307 	if (errnum < (sizeof(auth_errlist)/sizeof(auth_errlist[0])))
308 		/* LINTED interface problem */
309 		return (char *)auth_errlist[errnum];
310 
311 	return(NULL);
312 }
313