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