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