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