clnt_perror.c revision 1.12
1/*	$NetBSD: clnt_perror.c,v 1.12 1998/02/10 04:54:28 lukem 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.12 1998/02/10 04:54:28 lukem Exp $");
39#endif
40#endif
41
42/*
43 * clnt_perror.c
44 *
45 * Copyright (C) 1984, Sun Microsystems, Inc.
46 *
47 */
48
49#include "namespace.h"
50
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
69static char *buf;
70static int buflen;
71
72static char *_buf __P((void));
73static char *auth_errmsg __P((enum auth_stat));
74
75static char *
76_buf()
77{
78
79	if (buf == NULL)
80		buf = (char *)malloc(256);
81	buflen = 256;
82	return (buf);
83}
84
85/*
86 * Print reply error info
87 */
88char *
89clnt_sperror(rpch, s)
90	CLIENT		*rpch;
91	const char	*s;
92{
93	struct rpc_err e;
94	char *err;
95	char *str = _buf();
96	char *strstart = str;
97	int len = buflen, i;
98
99	if (str == NULL)
100		return (NULL);
101	CLNT_GETERR(rpch, &e);
102
103	i = snprintf(str, len, "%s: ", s);
104	str += i;
105	len -= i;
106
107	(void)strncpy(str, clnt_sperrno(e.re_status), len - 1);
108	i = strlen(str);
109	str += i;
110	len -= i;
111
112	switch (e.re_status) {
113	case RPC_SUCCESS:
114	case RPC_CANTENCODEARGS:
115	case RPC_CANTDECODERES:
116	case RPC_TIMEDOUT:
117	case RPC_PROGUNAVAIL:
118	case RPC_PROCUNAVAIL:
119	case RPC_CANTDECODEARGS:
120	case RPC_SYSTEMERROR:
121	case RPC_UNKNOWNHOST:
122	case RPC_UNKNOWNPROTO:
123	case RPC_PMAPFAILURE:
124	case RPC_PROGNOTREGISTERED:
125	case RPC_FAILED:
126		break;
127
128	case RPC_CANTSEND:
129	case RPC_CANTRECV:
130		i = snprintf(str, len, "; errno = %s",
131		    strerror(e.re_errno));
132		str += i;
133		len -= i;
134		break;
135
136	case RPC_VERSMISMATCH:
137		i = snprintf(str, len,
138			"; low version = %u, high version = %u",
139			e.re_vers.low, e.re_vers.high);
140		str += i;
141		len -= i;
142		break;
143
144	case RPC_AUTHERROR:
145		err = auth_errmsg(e.re_why);
146		i = snprintf(str, len, "; why = ");
147		str += i;
148		len -= i;
149		if (err != NULL) {
150			i = snprintf(str, len, "%s",err);
151		} else {
152			i = snprintf(str, len,
153				"(unknown authentication error - %d)",
154				(int) e.re_why);
155		}
156		str += i;
157		len -= i;
158		break;
159
160	case RPC_PROGVERSMISMATCH:
161		i = snprintf(str, len,
162			"; low version = %u, high version = %u",
163			e.re_vers.low, e.re_vers.high);
164		str += i;
165		len -= i;
166		break;
167
168	default:	/* unknown */
169		i = snprintf(str, len,
170			"; s1 = %u, s2 = %u",
171			e.re_lb.s1, e.re_lb.s2);
172		str += i;
173		len -= i;
174		break;
175	}
176	return(strstart) ;
177}
178
179void
180clnt_perror(rpch, s)
181	CLIENT		*rpch;
182	const char	*s;
183{
184	(void) fprintf(stderr, "%s\n", clnt_sperror(rpch,s));
185}
186
187static const char *const rpc_errlist[] = {
188	"RPC: Success",				/*  0 - RPC_SUCCESS */
189	"RPC: Can't encode arguments",		/*  1 - RPC_CANTENCODEARGS */
190	"RPC: Can't decode result",		/*  2 - RPC_CANTDECODERES */
191	"RPC: Unable to send",			/*  3 - RPC_CANTSEND */
192	"RPC: Unable to receive",		/*  4 - RPC_CANTRECV */
193	"RPC: Timed out",			/*  5 - RPC_TIMEDOUT */
194	"RPC: Incompatible versions of RPC",	/*  6 - RPC_VERSMISMATCH */
195	"RPC: Authentication error",		/*  7 - RPC_AUTHERROR */
196	"RPC: Program unavailable",		/*  8 - RPC_PROGUNAVAIL */
197	"RPC: Program/version mismatch",	/*  9 - RPC_PROGVERSMISMATCH */
198	"RPC: Procedure unavailable",		/* 10 - RPC_PROCUNAVAIL */
199	"RPC: Server can't decode arguments",	/* 11 - RPC_CANTDECODEARGS */
200	"RPC: Remote system error",		/* 12 - RPC_SYSTEMERROR */
201	"RPC: Unknown host",			/* 13 - RPC_UNKNOWNHOST */
202	"RPC: Port mapper failure",		/* 14 - RPC_PMAPFAILURE */
203	"RPC: Program not registered",		/* 15 - RPC_PROGNOTREGISTERED */
204	"RPC: Failed (unspecified error)",	/* 16 - RPC_FAILED */
205	"RPC: Unknown protocol"			/* 17 - RPC_UNKNOWNPROTO */
206};
207
208
209/*
210 * This interface for use by clntrpc
211 */
212char *
213clnt_sperrno(stat)
214	enum clnt_stat stat;
215{
216	unsigned int errnum = stat;
217
218	if (errnum < (sizeof(rpc_errlist)/sizeof(rpc_errlist[0])))
219		return (char *)rpc_errlist[errnum];
220
221	return ("RPC: (unknown error code)");
222}
223
224void
225clnt_perrno(num)
226	enum clnt_stat num;
227{
228	(void) fprintf(stderr, "%s\n", clnt_sperrno(num));
229}
230
231
232char *
233clnt_spcreateerror(s)
234	const char *s;
235{
236	char *str = _buf();
237	int len = buflen, i;
238
239	if (str == NULL)
240		return(NULL);
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		break;
274	}
275	return (str);
276}
277
278void
279clnt_pcreateerror(s)
280	const char *s;
281{
282	(void) fprintf(stderr, "%s\n", clnt_spcreateerror(s));
283}
284
285static const char *const auth_errlist[] = {
286	"Authentication OK",			/* 0 - AUTH_OK */
287	"Invalid client credential",		/* 1 - AUTH_BADCRED */
288	"Server rejected credential",		/* 2 - AUTH_REJECTEDCRED */
289	"Invalid client verifier", 		/* 3 - AUTH_BADVERF */
290	"Server rejected verifier", 		/* 4 - AUTH_REJECTEDVERF */
291	"Client credential too weak",		/* 5 - AUTH_TOOWEAK */
292	"Invalid server verifier",		/* 6 - AUTH_INVALIDRESP */
293	"Failed (unspecified error)"		/* 7 - AUTH_FAILED */
294};
295
296static char *
297auth_errmsg(stat)
298	enum auth_stat stat;
299{
300	unsigned int errnum = stat;
301
302	if (errnum < (sizeof(auth_errlist)/sizeof(auth_errlist[0])))
303		return (char *)auth_errlist[errnum];
304
305	return(NULL);
306}
307