clnt_perror.c revision 1.11
1/*	$NetBSD: clnt_perror.c,v 1.11 1997/07/21 14:08:23 jtc 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
35static char *sccsid = "@(#)clnt_perror.c 1.15 87/10/07 Copyr 1984 Sun Micro";
36static char *sccsid = "@(#)clnt_perror.c	2.1 88/07/29 4.0 RPCSRC";
37#else
38__RCSID("$NetBSD: clnt_perror.c,v 1.11 1997/07/21 14:08:23 jtc 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#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
52#include <rpc/rpc.h>
53#include <rpc/types.h>
54#include <rpc/auth.h>
55#include <rpc/clnt.h>
56
57#ifdef __weak_alias
58__weak_alias(clnt_pcreateerror,_clnt_pcreateerror);
59__weak_alias(clnt_perrno,_clnt_perrno);
60__weak_alias(clnt_perror,_clnt_perror);
61__weak_alias(clnt_spcreateerror,_clnt_spcreateerror);
62__weak_alias(clnt_sperrno,_clnt_sperrno);
63__weak_alias(clnt_sperror,_clnt_sperror);
64#endif
65
66static char *buf;
67static int buflen;
68
69static char *_buf __P((void));
70static char *auth_errmsg __P((enum auth_stat));
71
72static char *
73_buf()
74{
75
76	if (buf == 0)
77		buf = (char *)malloc(256);
78	buflen = 256;
79	return (buf);
80}
81
82/*
83 * Print reply error info
84 */
85char *
86clnt_sperror(rpch, s)
87	CLIENT *rpch;
88	char *s;
89{
90	struct rpc_err e;
91	char *err;
92	char *str = _buf();
93	char *strstart = str;
94	int len = buflen, i;
95
96	if (str == 0)
97		return (0);
98	CLNT_GETERR(rpch, &e);
99
100	i = snprintf(str, len, "%s: ", s);
101	str += i;
102	len -= i;
103
104	(void)strncpy(str, clnt_sperrno(e.re_status), len - 1);
105	i = strlen(str);
106	str += i;
107	len -= i;
108
109	switch (e.re_status) {
110	case RPC_SUCCESS:
111	case RPC_CANTENCODEARGS:
112	case RPC_CANTDECODERES:
113	case RPC_TIMEDOUT:
114	case RPC_PROGUNAVAIL:
115	case RPC_PROCUNAVAIL:
116	case RPC_CANTDECODEARGS:
117	case RPC_SYSTEMERROR:
118	case RPC_UNKNOWNHOST:
119	case RPC_UNKNOWNPROTO:
120	case RPC_PMAPFAILURE:
121	case RPC_PROGNOTREGISTERED:
122	case RPC_FAILED:
123		break;
124
125	case RPC_CANTSEND:
126	case RPC_CANTRECV:
127		i = snprintf(str, len, "; errno = %s",
128		    strerror(e.re_errno));
129		str += i;
130		len -= i;
131		break;
132
133	case RPC_VERSMISMATCH:
134		i = snprintf(str, len,
135			"; low version = %u, high version = %u",
136			e.re_vers.low, e.re_vers.high);
137		str += i;
138		len -= i;
139		break;
140
141	case RPC_AUTHERROR:
142		err = auth_errmsg(e.re_why);
143		i = snprintf(str, len, "; why = ");
144		str += i;
145		len -= i;
146		if (err != NULL) {
147			i = snprintf(str, len, "%s",err);
148		} else {
149			i = snprintf(str, len,
150				"(unknown authentication error - %d)",
151				(int) e.re_why);
152		}
153		str += i;
154		len -= i;
155		break;
156
157	case RPC_PROGVERSMISMATCH:
158		i = snprintf(str, len,
159			"; low version = %u, high version = %u",
160			e.re_vers.low, e.re_vers.high);
161		str += i;
162		len -= i;
163		break;
164
165	default:	/* unknown */
166		i = snprintf(str, len,
167			"; s1 = %u, s2 = %u",
168			e.re_lb.s1, e.re_lb.s2);
169		str += i;
170		len -= i;
171		break;
172	}
173	return(strstart) ;
174}
175
176void
177clnt_perror(rpch, s)
178	CLIENT *rpch;
179	char *s;
180{
181	(void) fprintf(stderr,"%s\n",clnt_sperror(rpch,s));
182}
183
184static const char *const rpc_errlist[] = {
185	"RPC: Success",				/*  0 - RPC_SUCCESS */
186	"RPC: Can't encode arguments",		/*  1 - RPC_CANTENCODEARGS */
187	"RPC: Can't decode result",		/*  2 - RPC_CANTDECODERES */
188	"RPC: Unable to send",			/*  3 - RPC_CANTSEND */
189	"RPC: Unable to receive",		/*  4 - RPC_CANTRECV */
190	"RPC: Timed out",			/*  5 - RPC_TIMEDOUT */
191	"RPC: Incompatible versions of RPC",	/*  6 - RPC_VERSMISMATCH */
192	"RPC: Authentication error",		/*  7 - RPC_AUTHERROR */
193	"RPC: Program unavailable",		/*  8 - RPC_PROGUNAVAIL */
194	"RPC: Program/version mismatch",	/*  9 - RPC_PROGVERSMISMATCH */
195	"RPC: Procedure unavailable",		/* 10 - RPC_PROCUNAVAIL */
196	"RPC: Server can't decode arguments",	/* 11 - RPC_CANTDECODEARGS */
197	"RPC: Remote system error",		/* 12 - RPC_SYSTEMERROR */
198	"RPC: Unknown host",			/* 13 - RPC_UNKNOWNHOST */
199	"RPC: Port mapper failure",		/* 14 - RPC_PMAPFAILURE */
200	"RPC: Program not registered",		/* 15 - RPC_PROGNOTREGISTERED */
201	"RPC: Failed (unspecified error)",	/* 16 - RPC_FAILED */
202	"RPC: Unknown protocol"			/* 17 - RPC_UNKNOWNPROTO */
203};
204
205
206/*
207 * This interface for use by clntrpc
208 */
209char *
210clnt_sperrno(stat)
211	enum clnt_stat stat;
212{
213	unsigned int errnum = stat;
214
215	if (errnum < (sizeof(rpc_errlist)/sizeof(rpc_errlist[0])))
216		return (char *)rpc_errlist[errnum];
217
218	return ("RPC: (unknown error code)");
219}
220
221void
222clnt_perrno(num)
223	enum clnt_stat num;
224{
225	(void) fprintf(stderr,"%s\n",clnt_sperrno(num));
226}
227
228
229char *
230clnt_spcreateerror(s)
231	char *s;
232{
233	char *str = _buf();
234	int len = buflen, i;
235
236	if (str == 0)
237		return(0);
238	i = snprintf(str, len, "%s: ", s);
239	len -= i;
240	(void)strncat(str, clnt_sperrno(rpc_createerr.cf_stat), len - 1);
241	switch (rpc_createerr.cf_stat) {
242	case RPC_PMAPFAILURE:
243		(void) strncat(str, " - ", len - 1);
244		(void) strncat(str,
245		    clnt_sperrno(rpc_createerr.cf_error.re_status), len - 4);
246		break;
247
248	case RPC_SYSTEMERROR:
249		(void)strncat(str, " - ", len - 1);
250		(void)strncat(str, strerror(rpc_createerr.cf_error.re_errno),
251		    len - 4);
252		break;
253
254	case RPC_CANTSEND:
255	case RPC_CANTDECODERES:
256	case RPC_CANTENCODEARGS:
257	case RPC_SUCCESS:
258	case RPC_UNKNOWNPROTO:
259	case RPC_PROGNOTREGISTERED:
260	case RPC_FAILED:
261	case RPC_UNKNOWNHOST:
262	case RPC_CANTDECODEARGS:
263	case RPC_PROCUNAVAIL:
264	case RPC_PROGVERSMISMATCH:
265	case RPC_PROGUNAVAIL:
266	case RPC_AUTHERROR:
267	case RPC_VERSMISMATCH:
268	case RPC_TIMEDOUT:
269	case RPC_CANTRECV:
270		break;
271	}
272	return (str);
273}
274
275void
276clnt_pcreateerror(s)
277	char *s;
278{
279	(void) fprintf(stderr,"%s\n",clnt_spcreateerror(s));
280}
281
282static const char *const auth_errlist[] = {
283	"Authentication OK",			/* 0 - AUTH_OK */
284	"Invalid client credential",		/* 1 - AUTH_BADCRED */
285	"Server rejected credential",		/* 2 - AUTH_REJECTEDCRED */
286	"Invalid client verifier", 		/* 3 - AUTH_BADVERF */
287	"Server rejected verifier", 		/* 4 - AUTH_REJECTEDVERF */
288	"Client credential too weak",		/* 5 - AUTH_TOOWEAK */
289	"Invalid server verifier",		/* 6 - AUTH_INVALIDRESP */
290	"Failed (unspecified error)"		/* 7 - AUTH_FAILED */
291};
292
293static char *
294auth_errmsg(stat)
295	enum auth_stat stat;
296{
297	unsigned int errnum = stat;
298
299	if (errnum < (sizeof(auth_errlist)/sizeof(auth_errlist[0])))
300		return (char *)auth_errlist[errnum];
301
302	return(NULL);
303}
304