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