xref: /freebsd/lib/libc/rpc/clnt_perror.c (revision e627b39baccd1ec9129690167cf5e6d860509655)
1 /*
2  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3  * unrestricted use provided that this legend is included on all tape
4  * media and as a part of the software program in whole or part.  Users
5  * may copy or modify Sun RPC without charge, but are not authorized
6  * to license or distribute it to anyone else except as part of a product or
7  * program developed by the user.
8  *
9  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12  *
13  * Sun RPC is provided with no support and without any obligation on the
14  * part of Sun Microsystems, Inc. to assist in its use, correction,
15  * modification or enhancement.
16  *
17  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19  * OR ANY PART THEREOF.
20  *
21  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22  * or profits or other special, indirect and consequential damages, even if
23  * Sun has been advised of the possibility of such damages.
24  *
25  * Sun Microsystems, Inc.
26  * 2550 Garcia Avenue
27  * Mountain View, California  94043
28  */
29 
30 #if defined(LIBC_SCCS) && !defined(lint)
31 /*static char *sccsid = "from: @(#)clnt_perror.c 1.15 87/10/07 Copyr 1984 Sun Micro";*/
32 /*static char *sccsid = "from: @(#)clnt_perror.c	2.1 88/07/29 4.0 RPCSRC";*/
33 static char *rcsid = "$Id: clnt_perror.c,v 1.4 1995/12/10 17:40:18 wpaul Exp $";
34 #endif
35 
36 /*
37  * clnt_perror.c
38  *
39  * Copyright (C) 1984, Sun Microsystems, Inc.
40  *
41  */
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <rpc/rpc.h>
46 #include <rpc/types.h>
47 #include <rpc/auth.h>
48 #include <rpc/clnt.h>
49 
50 static char *auth_errmsg();
51 
52 static char *buf;
53 
54 static char *
55 _buf()
56 {
57 
58 	if (buf == 0)
59 		buf = (char *)malloc(256);
60 	return (buf);
61 }
62 
63 /*
64  * Print reply error info
65  */
66 char *
67 clnt_sperror(rpch, s)
68 	CLIENT *rpch;
69 	char *s;
70 {
71 	struct rpc_err e;
72 	void clnt_perrno();
73 	char *err;
74 	char *str = _buf();
75 	char *strstart = str;
76 
77 	if (str == 0)
78 		return (0);
79 	CLNT_GETERR(rpch, &e);
80 
81 	(void) sprintf(str, "%s: ", s);
82 	str += strlen(str);
83 
84 	(void) strcpy(str, clnt_sperrno(e.re_status));
85 	str += strlen(str);
86 
87 	switch (e.re_status) {
88 	case RPC_SUCCESS:
89 	case RPC_CANTENCODEARGS:
90 	case RPC_CANTDECODERES:
91 	case RPC_TIMEDOUT:
92 	case RPC_PROGUNAVAIL:
93 	case RPC_PROCUNAVAIL:
94 	case RPC_CANTDECODEARGS:
95 	case RPC_SYSTEMERROR:
96 	case RPC_UNKNOWNHOST:
97 	case RPC_UNKNOWNPROTO:
98 	case RPC_PMAPFAILURE:
99 	case RPC_PROGNOTREGISTERED:
100 	case RPC_FAILED:
101 		break;
102 
103 	case RPC_CANTSEND:
104 	case RPC_CANTRECV:
105 		(void) sprintf(str, "; errno = %s",
106 		    strerror(e.re_errno));
107 		str += strlen(str);
108 		break;
109 
110 	case RPC_VERSMISMATCH:
111 		(void) sprintf(str,
112 			"; low version = %lu, high version = %lu",
113 			e.re_vers.low, e.re_vers.high);
114 		str += strlen(str);
115 		break;
116 
117 	case RPC_AUTHERROR:
118 		err = auth_errmsg(e.re_why);
119 		(void) sprintf(str,"; why = ");
120 		str += strlen(str);
121 		if (err != NULL) {
122 			(void) sprintf(str, "%s",err);
123 		} else {
124 			(void) sprintf(str,
125 				"(unknown authentication error - %d)",
126 				(int) e.re_why);
127 		}
128 		str += strlen(str);
129 		break;
130 
131 	case RPC_PROGVERSMISMATCH:
132 		(void) sprintf(str,
133 			"; low version = %lu, high version = %lu",
134 			e.re_vers.low, e.re_vers.high);
135 		str += strlen(str);
136 		break;
137 
138 	default:	/* unknown */
139 		(void) sprintf(str,
140 			"; s1 = %lu, s2 = %lu",
141 			e.re_lb.s1, e.re_lb.s2);
142 		str += strlen(str);
143 		break;
144 	}
145 	(void) sprintf(str, "\n");
146 	return(strstart) ;
147 }
148 
149 void
150 clnt_perror(rpch, s)
151 	CLIENT *rpch;
152 	char *s;
153 {
154 	(void) fprintf(stderr,"%s\n",clnt_sperror(rpch,s));
155 }
156 
157 
158 struct rpc_errtab {
159 	enum clnt_stat status;
160 	char *message;
161 };
162 
163 static struct rpc_errtab  rpc_errlist[] = {
164 	{ RPC_SUCCESS,
165 		"RPC: Success" },
166 	{ RPC_CANTENCODEARGS,
167 		"RPC: Can't encode arguments" },
168 	{ RPC_CANTDECODERES,
169 		"RPC: Can't decode result" },
170 	{ RPC_CANTSEND,
171 		"RPC: Unable to send" },
172 	{ RPC_CANTRECV,
173 		"RPC: Unable to receive" },
174 	{ RPC_TIMEDOUT,
175 		"RPC: Timed out" },
176 	{ RPC_VERSMISMATCH,
177 		"RPC: Incompatible versions of RPC" },
178 	{ RPC_AUTHERROR,
179 		"RPC: Authentication error" },
180 	{ RPC_PROGUNAVAIL,
181 		"RPC: Program unavailable" },
182 	{ RPC_PROGVERSMISMATCH,
183 		"RPC: Program/version mismatch" },
184 	{ RPC_PROCUNAVAIL,
185 		"RPC: Procedure unavailable" },
186 	{ RPC_CANTDECODEARGS,
187 		"RPC: Server can't decode arguments" },
188 	{ RPC_SYSTEMERROR,
189 		"RPC: Remote system error" },
190 	{ RPC_UNKNOWNHOST,
191 		"RPC: Unknown host" },
192 	{ RPC_UNKNOWNPROTO,
193 		"RPC: Unknown protocol" },
194 	{ RPC_PMAPFAILURE,
195 		"RPC: Port mapper failure" },
196 	{ RPC_PROGNOTREGISTERED,
197 		"RPC: Program not registered"},
198 	{ RPC_FAILED,
199 		"RPC: Failed (unspecified error)"}
200 };
201 
202 
203 /*
204  * This interface for use by clntrpc
205  */
206 char *
207 clnt_sperrno(stat)
208 	enum clnt_stat stat;
209 {
210 	int i;
211 
212 	for (i = 0; i < sizeof(rpc_errlist)/sizeof(struct rpc_errtab); i++) {
213 		if (rpc_errlist[i].status == stat) {
214 			return (rpc_errlist[i].message);
215 		}
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 	extern int sys_nerr;
233 	char *str = _buf();
234 
235 	if (str == 0)
236 		return(0);
237 	(void) sprintf(str, "%s: ", s);
238 	(void) strcat(str, clnt_sperrno(rpc_createerr.cf_stat));
239 	switch (rpc_createerr.cf_stat) {
240 	case RPC_PMAPFAILURE:
241 		(void) strcat(str, " - ");
242 		(void) strcat(str,
243 		    clnt_sperrno(rpc_createerr.cf_error.re_status));
244 		break;
245 
246 	case RPC_SYSTEMERROR:
247 		(void) strcat(str, " - ");
248 		if (rpc_createerr.cf_error.re_errno > 0
249 		    && rpc_createerr.cf_error.re_errno < sys_nerr)
250 			(void) strcat(str,
251 			    strerror(rpc_createerr.cf_error.re_errno));
252 		else
253 			(void) sprintf(&str[strlen(str)], "Error %d",
254 			    rpc_createerr.cf_error.re_errno);
255 		break;
256 	default:
257 		break;
258 	}
259 	(void) strcat(str, "\n");
260 	return (str);
261 }
262 
263 void
264 clnt_pcreateerror(s)
265 	char *s;
266 {
267 	(void) fprintf(stderr,"%s\n",clnt_spcreateerror(s));
268 }
269 
270 struct auth_errtab {
271 	enum auth_stat status;
272 	char *message;
273 };
274 
275 static struct auth_errtab auth_errlist[] = {
276 	{ AUTH_OK,
277 		"Authentication OK" },
278 	{ AUTH_BADCRED,
279 		"Invalid client credential" },
280 	{ AUTH_REJECTEDCRED,
281 		"Server rejected credential" },
282 	{ AUTH_BADVERF,
283 		"Invalid client verifier" },
284 	{ AUTH_REJECTEDVERF,
285 		"Server rejected verifier" },
286 	{ AUTH_TOOWEAK,
287 		"Client credential too weak" },
288 	{ AUTH_INVALIDRESP,
289 		"Invalid server verifier" },
290 	{ AUTH_FAILED,
291 		"Failed (unspecified error)" },
292 };
293 
294 static char *
295 auth_errmsg(stat)
296 	enum auth_stat stat;
297 {
298 	int i;
299 
300 	for (i = 0; i < sizeof(auth_errlist)/sizeof(struct auth_errtab); i++) {
301 		if (auth_errlist[i].status == stat) {
302 			return(auth_errlist[i].message);
303 		}
304 	}
305 	return(NULL);
306 }
307