xref: /netbsd/lib/libc/rpc/clnt_perror.c (revision bf9ec67e)
1 /*	$NetBSD: clnt_perror.c,v 1.25 2001/02/13 01:00:21 cgd 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.25 2001/02/13 01:00:21 cgd 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 	const char *s;
92 {
93 	struct rpc_err e;
94 	char *err;
95 	char *str;
96 	char *strstart;
97 	size_t len, i;
98 
99 	_DIAGASSERT(rpch != NULL);
100 	_DIAGASSERT(s != NULL);
101 
102 	str = _buf(); /* side effect: sets "buflen" */
103 	if (str == 0)
104 		return (0);
105 	len = buflen;
106 	strstart = str;
107 	CLNT_GETERR(rpch, &e);
108 
109 	i = snprintf(str, len, "%s: ", s);
110 	str += i;
111 	len -= i;
112 
113 	(void)strncpy(str, clnt_sperrno(e.re_status), len - 1);
114 	i = strlen(str);
115 	str += i;
116 	len -= i;
117 
118 	switch (e.re_status) {
119 	case RPC_SUCCESS:
120 	case RPC_CANTENCODEARGS:
121 	case RPC_CANTDECODERES:
122 	case RPC_TIMEDOUT:
123 	case RPC_PROGUNAVAIL:
124 	case RPC_PROCUNAVAIL:
125 	case RPC_CANTDECODEARGS:
126 	case RPC_SYSTEMERROR:
127 	case RPC_UNKNOWNHOST:
128 	case RPC_UNKNOWNPROTO:
129 	case RPC_PMAPFAILURE:
130 	case RPC_PROGNOTREGISTERED:
131 	case RPC_FAILED:
132 		break;
133 
134 	case RPC_CANTSEND:
135 	case RPC_CANTRECV:
136 		i = snprintf(str, len, "; errno = %s", strerror(e.re_errno));
137 		str += i;
138 		len -= i;
139 		break;
140 
141 	case RPC_VERSMISMATCH:
142 		i = snprintf(str, len, "; low version = %u, high version = %u",
143 			e.re_vers.low, e.re_vers.high);
144 		str += i;
145 		len -= i;
146 		break;
147 
148 	case RPC_AUTHERROR:
149 		err = auth_errmsg(e.re_why);
150 		i = snprintf(str, len, "; why = ");
151 		str += i;
152 		len -= i;
153 		if (err != NULL) {
154 			i = snprintf(str, len, "%s",err);
155 		} else {
156 			i = snprintf(str, len,
157 				"(unknown authentication error - %d)",
158 				(int) e.re_why);
159 		}
160 		str += i;
161 		len -= i;
162 		break;
163 
164 	case RPC_PROGVERSMISMATCH:
165 		i = snprintf(str, len, "; low version = %u, high version = %u",
166 			e.re_vers.low, e.re_vers.high);
167 		str += i;
168 		len -= i;
169 		break;
170 
171 	default:	/* unknown */
172 		i = snprintf(str, len, "; s1 = %u, s2 = %u",
173 			e.re_lb.s1, e.re_lb.s2);
174 		str += i;
175 		len -= i;
176 		break;
177 	}
178 	return(strstart) ;
179 }
180 
181 void
182 clnt_perror(rpch, s)
183 	CLIENT *rpch;
184 	const char *s;
185 {
186 
187 	_DIAGASSERT(rpch != NULL);
188 	_DIAGASSERT(s != NULL);
189 
190 	(void) fprintf(stderr, "%s\n", clnt_sperror(rpch,s));
191 }
192 
193 static const char *const rpc_errlist[] = {
194 	"RPC: Success",				/*  0 - RPC_SUCCESS */
195 	"RPC: Can't encode arguments",		/*  1 - RPC_CANTENCODEARGS */
196 	"RPC: Can't decode result",		/*  2 - RPC_CANTDECODERES */
197 	"RPC: Unable to send",			/*  3 - RPC_CANTSEND */
198 	"RPC: Unable to receive",		/*  4 - RPC_CANTRECV */
199 	"RPC: Timed out",			/*  5 - RPC_TIMEDOUT */
200 	"RPC: Incompatible versions of RPC",	/*  6 - RPC_VERSMISMATCH */
201 	"RPC: Authentication error",		/*  7 - RPC_AUTHERROR */
202 	"RPC: Program unavailable",		/*  8 - RPC_PROGUNAVAIL */
203 	"RPC: Program/version mismatch",	/*  9 - RPC_PROGVERSMISMATCH */
204 	"RPC: Procedure unavailable",		/* 10 - RPC_PROCUNAVAIL */
205 	"RPC: Server can't decode arguments",	/* 11 - RPC_CANTDECODEARGS */
206 	"RPC: Remote system error",		/* 12 - RPC_SYSTEMERROR */
207 	"RPC: Unknown host",			/* 13 - RPC_UNKNOWNHOST */
208 	"RPC: Port mapper failure",		/* 14 - RPC_PMAPFAILURE */
209 	"RPC: Program not registered",		/* 15 - RPC_PROGNOTREGISTERED */
210 	"RPC: Failed (unspecified error)",	/* 16 - RPC_FAILED */
211 	"RPC: Unknown protocol"			/* 17 - RPC_UNKNOWNPROTO */
212 };
213 
214 
215 /*
216  * This interface for use by clntrpc
217  */
218 char *
219 clnt_sperrno(stat)
220 	enum clnt_stat stat;
221 {
222 	unsigned int errnum = stat;
223 
224 	if (errnum < (sizeof(rpc_errlist)/sizeof(rpc_errlist[0])))
225 		/* LINTED interface problem */
226 		return (char *)rpc_errlist[errnum];
227 
228 	return ("RPC: (unknown error code)");
229 }
230 
231 void
232 clnt_perrno(num)
233 	enum clnt_stat num;
234 {
235 	(void) fprintf(stderr, "%s\n", clnt_sperrno(num));
236 }
237 
238 
239 char *
240 clnt_spcreateerror(s)
241 	const char *s;
242 {
243 	char *str;
244 	size_t len, i;
245 
246 	_DIAGASSERT(s != NULL);
247 
248 	str = _buf(); /* side effect: sets "buflen" */
249 	if (str == 0)
250 		return(0);
251 	len = buflen;
252 	i = snprintf(str, len, "%s: ", s);
253 	len -= i;
254 	(void)strncat(str, clnt_sperrno(rpc_createerr.cf_stat), len - 1);
255 	switch (rpc_createerr.cf_stat) {
256 	case RPC_PMAPFAILURE:
257 		(void) strncat(str, " - ", len - 1);
258 		(void) strncat(str,
259 		    clnt_sperrno(rpc_createerr.cf_error.re_status), len - 4);
260 		break;
261 
262 	case RPC_SYSTEMERROR:
263 		(void)strncat(str, " - ", len - 1);
264 		(void)strncat(str, strerror(rpc_createerr.cf_error.re_errno),
265 		    len - 4);
266 		break;
267 
268 	case RPC_CANTSEND:
269 	case RPC_CANTDECODERES:
270 	case RPC_CANTENCODEARGS:
271 	case RPC_SUCCESS:
272 	case RPC_UNKNOWNPROTO:
273 	case RPC_PROGNOTREGISTERED:
274 	case RPC_FAILED:
275 	case RPC_UNKNOWNHOST:
276 	case RPC_CANTDECODEARGS:
277 	case RPC_PROCUNAVAIL:
278 	case RPC_PROGVERSMISMATCH:
279 	case RPC_PROGUNAVAIL:
280 	case RPC_AUTHERROR:
281 	case RPC_VERSMISMATCH:
282 	case RPC_TIMEDOUT:
283 	case RPC_CANTRECV:
284 	default:
285 		break;
286 	}
287 	return (str);
288 }
289 
290 void
291 clnt_pcreateerror(s)
292 	const char *s;
293 {
294 
295 	_DIAGASSERT(s != NULL);
296 
297 	(void) fprintf(stderr, "%s\n", clnt_spcreateerror(s));
298 }
299 
300 static const char *const auth_errlist[] = {
301 	"Authentication OK",			/* 0 - AUTH_OK */
302 	"Invalid client credential",		/* 1 - AUTH_BADCRED */
303 	"Server rejected credential",		/* 2 - AUTH_REJECTEDCRED */
304 	"Invalid client verifier", 		/* 3 - AUTH_BADVERF */
305 	"Server rejected verifier", 		/* 4 - AUTH_REJECTEDVERF */
306 	"Client credential too weak",		/* 5 - AUTH_TOOWEAK */
307 	"Invalid server verifier",		/* 6 - AUTH_INVALIDRESP */
308 	"Failed (unspecified error)"		/* 7 - AUTH_FAILED */
309 };
310 
311 static char *
312 auth_errmsg(stat)
313 	enum auth_stat stat;
314 {
315 	unsigned int errnum = stat;
316 
317 	if (errnum < (sizeof(auth_errlist)/sizeof(auth_errlist[0])))
318 		/* LINTED interface problem */
319 		return (char *)auth_errlist[errnum];
320 
321 	return(NULL);
322 }
323